]> git.proxmox.com Git - mirror_iproute2.git/blob - tc/f_flower.c
Merge branch 'master' into net-next
[mirror_iproute2.git] / tc / f_flower.c
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>
15 #include <syslog.h>
16 #include <string.h>
17 #include <net/if.h>
18 #include <linux/if_arp.h>
19 #include <linux/if_ether.h>
20 #include <linux/ip.h>
21 #include <linux/tc_act/tc_vlan.h>
22 #include <linux/mpls.h>
23
24 #include "utils.h"
25 #include "tc_util.h"
26 #include "rt_names.h"
27
28 enum flower_matching_flags {
29 FLOWER_IP_FLAGS,
30 };
31
32 enum flower_endpoint {
33 FLOWER_ENDPOINT_SRC,
34 FLOWER_ENDPOINT_DST
35 };
36
37 enum flower_icmp_field {
38 FLOWER_ICMP_FIELD_TYPE,
39 FLOWER_ICMP_FIELD_CODE
40 };
41
42 static void explain(void)
43 {
44 fprintf(stderr,
45 "Usage: ... flower [ MATCH-LIST ]\n"
46 " [ skip_sw | skip_hw ]\n"
47 " [ action ACTION-SPEC ] [ classid CLASSID ]\n"
48 "\n"
49 "Where: MATCH-LIST := [ MATCH-LIST ] MATCH\n"
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 " dst_mac MASKED-LLADDR |\n"
55 " src_mac MASKED-LLADDR |\n"
56 " ip_proto [tcp | udp | sctp | icmp | icmpv6 | IP-PROTO ] |\n"
57 " ip_tos MASKED-IP_TOS |\n"
58 " ip_ttl MASKED-IP_TTL |\n"
59 " mpls_label LABEL |\n"
60 " mpls_tc TC |\n"
61 " mpls_bos BOS |\n"
62 " mpls_ttl TTL |\n"
63 " dst_ip PREFIX |\n"
64 " src_ip PREFIX |\n"
65 " dst_port PORT-NUMBER |\n"
66 " src_port PORT-NUMBER |\n"
67 " tcp_flags MASKED-TCP_FLAGS |\n"
68 " type MASKED-ICMP-TYPE |\n"
69 " code MASKED-ICMP-CODE |\n"
70 " arp_tip IPV4-PREFIX |\n"
71 " arp_sip IPV4-PREFIX |\n"
72 " arp_op [ request | reply | OP ] |\n"
73 " arp_tha MASKED-LLADDR |\n"
74 " arp_sha MASKED-LLADDR |\n"
75 " enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
76 " enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
77 " enc_key_id [ KEY-ID ] |\n"
78 " ip_flags IP-FLAGS | \n"
79 " enc_dst_port [ port_number ] }\n"
80 " FILTERID := X:Y:Z\n"
81 " MASKED_LLADDR := { LLADDR | LLADDR/MASK | LLADDR/BITS }\n"
82 " ACTION-SPEC := ... look at individual actions\n"
83 "\n"
84 "NOTE: CLASSID, IP-PROTO are parsed as hexadecimal input.\n"
85 "NOTE: There can be only used one mask per one prio. If user needs\n"
86 " to specify different mask, he has to use different prio.\n");
87 }
88
89 static int flower_parse_eth_addr(char *str, int addr_type, int mask_type,
90 struct nlmsghdr *n)
91 {
92 int ret, err = -1;
93 char addr[ETH_ALEN], *slash;
94
95 slash = strchr(str, '/');
96 if (slash)
97 *slash = '\0';
98
99 ret = ll_addr_a2n(addr, sizeof(addr), str);
100 if (ret < 0)
101 goto err;
102 addattr_l(n, MAX_MSG, addr_type, addr, sizeof(addr));
103
104 if (slash) {
105 unsigned bits;
106
107 if (!get_unsigned(&bits, slash + 1, 10)) {
108 uint64_t mask;
109
110 /* Extra 16 bit shift to push mac address into
111 * high bits of uint64_t
112 */
113 mask = htonll(0xffffffffffffULL << (16 + 48 - bits));
114 memcpy(addr, &mask, ETH_ALEN);
115 } else {
116 ret = ll_addr_a2n(addr, sizeof(addr), slash + 1);
117 if (ret < 0)
118 goto err;
119 }
120 } else {
121 memset(addr, 0xff, ETH_ALEN);
122 }
123 addattr_l(n, MAX_MSG, mask_type, addr, sizeof(addr));
124
125 err = 0;
126 err:
127 if (slash)
128 *slash = '/';
129 return err;
130 }
131
132 static int flower_parse_vlan_eth_type(char *str, __be16 eth_type, int type,
133 __be16 *p_vlan_eth_type,
134 struct nlmsghdr *n)
135 {
136 __be16 vlan_eth_type;
137
138 if (eth_type != htons(ETH_P_8021Q)) {
139 fprintf(stderr,
140 "Can't set \"vlan_ethtype\" if ethertype isn't 802.1Q\n");
141 return -1;
142 }
143
144 if (ll_proto_a2n(&vlan_eth_type, str))
145 invarg("invalid vlan_ethtype", str);
146 addattr16(n, MAX_MSG, type, vlan_eth_type);
147 *p_vlan_eth_type = vlan_eth_type;
148 return 0;
149 }
150
151 struct flag_to_string {
152 int flag;
153 enum flower_matching_flags type;
154 char *string;
155 };
156
157 static struct flag_to_string flags_str[] = {
158 { TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOWER_IP_FLAGS, "frag" },
159 };
160
161 static int flower_parse_matching_flags(char *str,
162 enum flower_matching_flags type,
163 __u32 *mtf, __u32 *mtf_mask)
164 {
165 char *token;
166 bool no;
167 bool found;
168 int i;
169
170 token = strtok(str, "/");
171
172 while (token) {
173 if (!strncmp(token, "no", 2)) {
174 no = true;
175 token += 2;
176 } else
177 no = false;
178
179 found = false;
180 for (i = 0; i < ARRAY_SIZE(flags_str); i++) {
181 if (type != flags_str[i].type)
182 continue;
183
184 if (!strcmp(token, flags_str[i].string)) {
185 if (no)
186 *mtf &= ~flags_str[i].flag;
187 else
188 *mtf |= flags_str[i].flag;
189
190 *mtf_mask |= flags_str[i].flag;
191 found = true;
192 break;
193 }
194 }
195 if (!found)
196 return -1;
197
198 token = strtok(NULL, "/");
199 }
200
201 return 0;
202 }
203
204 static int flower_parse_ip_proto(char *str, __be16 eth_type, int type,
205 __u8 *p_ip_proto, struct nlmsghdr *n)
206 {
207 int ret;
208 __u8 ip_proto;
209
210 if (eth_type != htons(ETH_P_IP) && eth_type != htons(ETH_P_IPV6))
211 goto err;
212
213 if (matches(str, "tcp") == 0) {
214 ip_proto = IPPROTO_TCP;
215 } else if (matches(str, "udp") == 0) {
216 ip_proto = IPPROTO_UDP;
217 } else if (matches(str, "sctp") == 0) {
218 ip_proto = IPPROTO_SCTP;
219 } else if (matches(str, "icmp") == 0) {
220 if (eth_type != htons(ETH_P_IP))
221 goto err;
222 ip_proto = IPPROTO_ICMP;
223 } else if (matches(str, "icmpv6") == 0) {
224 if (eth_type != htons(ETH_P_IPV6))
225 goto err;
226 ip_proto = IPPROTO_ICMPV6;
227 } else {
228 ret = get_u8(&ip_proto, str, 16);
229 if (ret)
230 return -1;
231 }
232 addattr8(n, MAX_MSG, type, ip_proto);
233 *p_ip_proto = ip_proto;
234 return 0;
235
236 err:
237 fprintf(stderr, "Illegal \"eth_type\" for ip proto\n");
238 return -1;
239 }
240
241 static int __flower_parse_ip_addr(char *str, int family,
242 int addr4_type, int mask4_type,
243 int addr6_type, int mask6_type,
244 struct nlmsghdr *n)
245 {
246 int ret;
247 inet_prefix addr;
248 int bits;
249 int i;
250
251 ret = get_prefix(&addr, str, family);
252 if (ret)
253 return -1;
254
255 if (family && (addr.family != family)) {
256 fprintf(stderr, "Illegal \"eth_type\" for ip address\n");
257 return -1;
258 }
259
260 addattr_l(n, MAX_MSG, addr.family == AF_INET ? addr4_type : addr6_type,
261 addr.data, addr.bytelen);
262
263 memset(addr.data, 0xff, addr.bytelen);
264 bits = addr.bitlen;
265 for (i = 0; i < addr.bytelen / 4; i++) {
266 if (!bits) {
267 addr.data[i] = 0;
268 } else if (bits / 32 >= 1) {
269 bits -= 32;
270 } else {
271 addr.data[i] <<= 32 - bits;
272 addr.data[i] = htonl(addr.data[i]);
273 bits = 0;
274 }
275 }
276
277 addattr_l(n, MAX_MSG, addr.family == AF_INET ? mask4_type : mask6_type,
278 addr.data, addr.bytelen);
279
280 return 0;
281 }
282
283 static int flower_parse_ip_addr(char *str, __be16 eth_type,
284 int addr4_type, int mask4_type,
285 int addr6_type, int mask6_type,
286 struct nlmsghdr *n)
287 {
288 int family;
289
290 if (eth_type == htons(ETH_P_IP)) {
291 family = AF_INET;
292 } else if (eth_type == htons(ETH_P_IPV6)) {
293 family = AF_INET6;
294 } else if (!eth_type) {
295 family = AF_UNSPEC;
296 } else {
297 return -1;
298 }
299
300 return __flower_parse_ip_addr(str, family, addr4_type, mask4_type,
301 addr6_type, mask6_type, n);
302 }
303
304 static bool flower_eth_type_arp(__be16 eth_type)
305 {
306 return eth_type == htons(ETH_P_ARP) || eth_type == htons(ETH_P_RARP);
307 }
308
309 static int flower_parse_arp_ip_addr(char *str, __be16 eth_type,
310 int addr_type, int mask_type,
311 struct nlmsghdr *n)
312 {
313 if (!flower_eth_type_arp(eth_type))
314 return -1;
315
316 return __flower_parse_ip_addr(str, AF_INET, addr_type, mask_type,
317 TCA_FLOWER_UNSPEC, TCA_FLOWER_UNSPEC, n);
318 }
319
320 static int flower_parse_u8(char *str, int value_type, int mask_type,
321 int (*value_from_name)(const char *str,
322 __u8 *value),
323 bool (*value_validate)(__u8 value),
324 struct nlmsghdr *n)
325 {
326 char *slash;
327 int ret, err = -1;
328 __u8 value, mask;
329
330 slash = strchr(str, '/');
331 if (slash)
332 *slash = '\0';
333
334 ret = value_from_name ? value_from_name(str, &value) : -1;
335 if (ret < 0) {
336 ret = get_u8(&value, str, 10);
337 if (ret)
338 goto err;
339 }
340
341 if (value_validate && !value_validate(value))
342 goto err;
343
344 if (slash) {
345 ret = get_u8(&mask, slash + 1, 10);
346 if (ret)
347 goto err;
348 }
349 else {
350 mask = UINT8_MAX;
351 }
352
353 addattr8(n, MAX_MSG, value_type, value);
354 addattr8(n, MAX_MSG, mask_type, mask);
355
356 err = 0;
357 err:
358 if (slash)
359 *slash = '/';
360 return err;
361 }
362
363 static const char *flower_print_arp_op_to_name(__u8 op)
364 {
365 switch (op) {
366 case ARPOP_REQUEST:
367 return "request";
368 case ARPOP_REPLY:
369 return "reply";
370 default:
371 return NULL;
372 }
373 }
374
375 static int flower_arp_op_from_name(const char *name, __u8 *op)
376 {
377 if (!strcmp(name, "request"))
378 *op = ARPOP_REQUEST;
379 else if (!strcmp(name, "reply"))
380 *op = ARPOP_REPLY;
381 else
382 return -1;
383
384 return 0;
385 }
386
387 static bool flow_arp_op_validate(__u8 op)
388 {
389 return !op || op == ARPOP_REQUEST || op == ARPOP_REPLY;
390 }
391
392 static int flower_parse_arp_op(char *str, __be16 eth_type,
393 int op_type, int mask_type,
394 struct nlmsghdr *n)
395 {
396 if (!flower_eth_type_arp(eth_type))
397 return -1;
398
399 return flower_parse_u8(str, op_type, mask_type, flower_arp_op_from_name,
400 flow_arp_op_validate, n);
401 }
402
403 static int flower_icmp_attr_type(__be16 eth_type, __u8 ip_proto,
404 enum flower_icmp_field field)
405 {
406 if (eth_type == htons(ETH_P_IP) && ip_proto == IPPROTO_ICMP)
407 return field == FLOWER_ICMP_FIELD_CODE ?
408 TCA_FLOWER_KEY_ICMPV4_CODE :
409 TCA_FLOWER_KEY_ICMPV4_TYPE;
410 else if (eth_type == htons(ETH_P_IPV6) && ip_proto == IPPROTO_ICMPV6)
411 return field == FLOWER_ICMP_FIELD_CODE ?
412 TCA_FLOWER_KEY_ICMPV6_CODE :
413 TCA_FLOWER_KEY_ICMPV6_TYPE;
414
415 return -1;
416 }
417
418 static int flower_icmp_attr_mask_type(__be16 eth_type, __u8 ip_proto,
419 enum flower_icmp_field field)
420 {
421 if (eth_type == htons(ETH_P_IP) && ip_proto == IPPROTO_ICMP)
422 return field == FLOWER_ICMP_FIELD_CODE ?
423 TCA_FLOWER_KEY_ICMPV4_CODE_MASK :
424 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK;
425 else if (eth_type == htons(ETH_P_IPV6) && ip_proto == IPPROTO_ICMPV6)
426 return field == FLOWER_ICMP_FIELD_CODE ?
427 TCA_FLOWER_KEY_ICMPV6_CODE_MASK :
428 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK;
429
430 return -1;
431 }
432
433 static int flower_parse_icmp(char *str, __u16 eth_type, __u8 ip_proto,
434 enum flower_icmp_field field, struct nlmsghdr *n)
435 {
436 int value_type, mask_type;
437
438 value_type = flower_icmp_attr_type(eth_type, ip_proto, field);
439 mask_type = flower_icmp_attr_mask_type(eth_type, ip_proto, field);
440 if (value_type < 0 || mask_type < 0)
441 return -1;
442
443 return flower_parse_u8(str, value_type, mask_type, NULL, NULL, n);
444 }
445
446 static int flower_port_attr_type(__u8 ip_proto, enum flower_endpoint endpoint)
447 {
448 if (ip_proto == IPPROTO_TCP)
449 return endpoint == FLOWER_ENDPOINT_SRC ?
450 TCA_FLOWER_KEY_TCP_SRC :
451 TCA_FLOWER_KEY_TCP_DST;
452 else if (ip_proto == IPPROTO_UDP)
453 return endpoint == FLOWER_ENDPOINT_SRC ?
454 TCA_FLOWER_KEY_UDP_SRC :
455 TCA_FLOWER_KEY_UDP_DST;
456 else if (ip_proto == IPPROTO_SCTP)
457 return endpoint == FLOWER_ENDPOINT_SRC ?
458 TCA_FLOWER_KEY_SCTP_SRC :
459 TCA_FLOWER_KEY_SCTP_DST;
460 else
461 return -1;
462 }
463
464 static int flower_parse_port(char *str, __u8 ip_proto,
465 enum flower_endpoint endpoint,
466 struct nlmsghdr *n)
467 {
468 int ret;
469 int type;
470 __be16 port;
471
472 type = flower_port_attr_type(ip_proto, endpoint);
473 if (type < 0)
474 return -1;
475
476 ret = get_be16(&port, str, 10);
477 if (ret)
478 return -1;
479
480 addattr16(n, MAX_MSG, type, port);
481
482 return 0;
483 }
484
485 #define TCP_FLAGS_MAX_MASK 0xfff
486
487 static int flower_parse_tcp_flags(char *str, int flags_type, int mask_type,
488 struct nlmsghdr *n)
489 {
490 char *slash;
491 int ret, err = -1;
492 __u16 flags;
493
494 slash = strchr(str, '/');
495 if (slash)
496 *slash = '\0';
497
498 ret = get_u16(&flags, str, 16);
499 if (ret < 0 || flags & ~TCP_FLAGS_MAX_MASK)
500 goto err;
501
502 addattr16(n, MAX_MSG, flags_type, htons(flags));
503
504 if (slash) {
505 ret = get_u16(&flags, slash + 1, 16);
506 if (ret < 0 || flags & ~TCP_FLAGS_MAX_MASK)
507 goto err;
508 } else {
509 flags = TCP_FLAGS_MAX_MASK;
510 }
511 addattr16(n, MAX_MSG, mask_type, htons(flags));
512
513 err = 0;
514 err:
515 if (slash)
516 *slash = '/';
517 return err;
518 }
519
520 static int flower_parse_ip_tos_ttl(char *str, int key_type, int mask_type,
521 struct nlmsghdr *n)
522 {
523 char *slash;
524 int ret, err = -1;
525 __u8 tos_ttl;
526
527 slash = strchr(str, '/');
528 if (slash)
529 *slash = '\0';
530
531 ret = get_u8(&tos_ttl, str, 10);
532 if (ret < 0)
533 ret = get_u8(&tos_ttl, str, 16);
534 if (ret < 0)
535 goto err;
536
537 addattr8(n, MAX_MSG, key_type, tos_ttl);
538
539 if (slash) {
540 ret = get_u8(&tos_ttl, slash + 1, 16);
541 if (ret < 0)
542 goto err;
543 } else {
544 tos_ttl = 0xff;
545 }
546 addattr8(n, MAX_MSG, mask_type, tos_ttl);
547
548 err = 0;
549 err:
550 if (slash)
551 *slash = '/';
552 return err;
553 }
554
555 static int flower_parse_key_id(const char *str, int type, struct nlmsghdr *n)
556 {
557 int ret;
558 __be32 key_id;
559
560 ret = get_be32(&key_id, str, 10);
561 if (!ret)
562 addattr32(n, MAX_MSG, type, key_id);
563
564 return ret;
565 }
566
567 static int flower_parse_enc_port(char *str, int type, struct nlmsghdr *n)
568 {
569 int ret;
570 __be16 port;
571
572 ret = get_be16(&port, str, 10);
573 if (ret)
574 return -1;
575
576 addattr16(n, MAX_MSG, type, port);
577
578 return 0;
579 }
580
581 static int flower_parse_opt(struct filter_util *qu, char *handle,
582 int argc, char **argv, struct nlmsghdr *n)
583 {
584 int ret;
585 struct tcmsg *t = NLMSG_DATA(n);
586 struct rtattr *tail;
587 __be16 eth_type = TC_H_MIN(t->tcm_info);
588 __be16 vlan_ethtype = 0;
589 __u8 ip_proto = 0xff;
590 __u32 flags = 0;
591 __u32 mtf = 0;
592 __u32 mtf_mask = 0;
593
594 if (handle) {
595 ret = get_u32(&t->tcm_handle, handle, 0);
596 if (ret) {
597 fprintf(stderr, "Illegal \"handle\"\n");
598 return -1;
599 }
600 }
601
602 tail = (struct rtattr *) (((void *) n) + NLMSG_ALIGN(n->nlmsg_len));
603 addattr_l(n, MAX_MSG, TCA_OPTIONS, NULL, 0);
604
605 if (argc == 0) {
606 /*at minimal we will match all ethertype packets */
607 goto parse_done;
608 }
609
610 while (argc > 0) {
611 if (matches(*argv, "classid") == 0 ||
612 matches(*argv, "flowid") == 0) {
613 unsigned int handle;
614
615 NEXT_ARG();
616 ret = get_tc_classid(&handle, *argv);
617 if (ret) {
618 fprintf(stderr, "Illegal \"classid\"\n");
619 return -1;
620 }
621 addattr_l(n, MAX_MSG, TCA_FLOWER_CLASSID, &handle, 4);
622 } else if (matches(*argv, "ip_flags") == 0) {
623 NEXT_ARG();
624 ret = flower_parse_matching_flags(*argv,
625 FLOWER_IP_FLAGS,
626 &mtf,
627 &mtf_mask);
628 if (ret < 0) {
629 fprintf(stderr, "Illegal \"ip_flags\"\n");
630 return -1;
631 }
632 } else if (matches(*argv, "skip_hw") == 0) {
633 flags |= TCA_CLS_FLAGS_SKIP_HW;
634 } else if (matches(*argv, "skip_sw") == 0) {
635 flags |= TCA_CLS_FLAGS_SKIP_SW;
636 } else if (matches(*argv, "indev") == 0) {
637 char ifname[IFNAMSIZ] = {};
638
639 NEXT_ARG();
640 strncpy(ifname, *argv, sizeof(ifname) - 1);
641 addattrstrz(n, MAX_MSG, TCA_FLOWER_INDEV, ifname);
642 } else if (matches(*argv, "vlan_id") == 0) {
643 __u16 vid;
644
645 NEXT_ARG();
646 if (eth_type != htons(ETH_P_8021Q)) {
647 fprintf(stderr,
648 "Can't set \"vlan_id\" if ethertype isn't 802.1Q\n");
649 return -1;
650 }
651 ret = get_u16(&vid, *argv, 10);
652 if (ret < 0 || vid & ~0xfff) {
653 fprintf(stderr, "Illegal \"vlan_id\"\n");
654 return -1;
655 }
656 addattr16(n, MAX_MSG, TCA_FLOWER_KEY_VLAN_ID, vid);
657 } else if (matches(*argv, "vlan_prio") == 0) {
658 __u8 vlan_prio;
659
660 NEXT_ARG();
661 if (eth_type != htons(ETH_P_8021Q)) {
662 fprintf(stderr,
663 "Can't set \"vlan_prio\" if ethertype isn't 802.1Q\n");
664 return -1;
665 }
666 ret = get_u8(&vlan_prio, *argv, 10);
667 if (ret < 0 || vlan_prio & ~0x7) {
668 fprintf(stderr, "Illegal \"vlan_prio\"\n");
669 return -1;
670 }
671 addattr8(n, MAX_MSG,
672 TCA_FLOWER_KEY_VLAN_PRIO, vlan_prio);
673 } else if (matches(*argv, "vlan_ethtype") == 0) {
674 NEXT_ARG();
675 ret = flower_parse_vlan_eth_type(*argv, eth_type,
676 TCA_FLOWER_KEY_VLAN_ETH_TYPE,
677 &vlan_ethtype, n);
678 if (ret < 0)
679 return -1;
680 } else if (matches(*argv, "mpls_label") == 0) {
681 __u32 label;
682
683 NEXT_ARG();
684 if (eth_type != htons(ETH_P_MPLS_UC) &&
685 eth_type != htons(ETH_P_MPLS_MC)) {
686 fprintf(stderr,
687 "Can't set \"mpls_label\" if ethertype isn't MPLS\n");
688 return -1;
689 }
690 ret = get_u32(&label, *argv, 10);
691 if (ret < 0 || label & ~(MPLS_LS_LABEL_MASK >> MPLS_LS_LABEL_SHIFT)) {
692 fprintf(stderr, "Illegal \"mpls_label\"\n");
693 return -1;
694 }
695 addattr32(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_LABEL, label);
696 } else if (matches(*argv, "mpls_tc") == 0) {
697 __u8 tc;
698
699 NEXT_ARG();
700 if (eth_type != htons(ETH_P_MPLS_UC) &&
701 eth_type != htons(ETH_P_MPLS_MC)) {
702 fprintf(stderr,
703 "Can't set \"mpls_tc\" if ethertype isn't MPLS\n");
704 return -1;
705 }
706 ret = get_u8(&tc, *argv, 10);
707 if (ret < 0 || tc & ~(MPLS_LS_TC_MASK >> MPLS_LS_TC_SHIFT)) {
708 fprintf(stderr, "Illegal \"mpls_tc\"\n");
709 return -1;
710 }
711 addattr8(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_TC, tc);
712 } else if (matches(*argv, "mpls_bos") == 0) {
713 __u8 bos;
714
715 NEXT_ARG();
716 if (eth_type != htons(ETH_P_MPLS_UC) &&
717 eth_type != htons(ETH_P_MPLS_MC)) {
718 fprintf(stderr,
719 "Can't set \"mpls_bos\" if ethertype isn't MPLS\n");
720 return -1;
721 }
722 ret = get_u8(&bos, *argv, 10);
723 if (ret < 0 || bos & ~(MPLS_LS_S_MASK >> MPLS_LS_S_SHIFT)) {
724 fprintf(stderr, "Illegal \"mpls_bos\"\n");
725 return -1;
726 }
727 addattr8(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_BOS, bos);
728 } else if (matches(*argv, "mpls_ttl") == 0) {
729 __u8 ttl;
730
731 NEXT_ARG();
732 if (eth_type != htons(ETH_P_MPLS_UC) &&
733 eth_type != htons(ETH_P_MPLS_MC)) {
734 fprintf(stderr,
735 "Can't set \"mpls_ttl\" if ethertype isn't MPLS\n");
736 return -1;
737 }
738 ret = get_u8(&ttl, *argv, 10);
739 if (ret < 0 || ttl & ~(MPLS_LS_TTL_MASK >> MPLS_LS_TTL_SHIFT)) {
740 fprintf(stderr, "Illegal \"mpls_ttl\"\n");
741 return -1;
742 }
743 addattr8(n, MAX_MSG, TCA_FLOWER_KEY_MPLS_TTL, ttl);
744 } else if (matches(*argv, "dst_mac") == 0) {
745 NEXT_ARG();
746 ret = flower_parse_eth_addr(*argv,
747 TCA_FLOWER_KEY_ETH_DST,
748 TCA_FLOWER_KEY_ETH_DST_MASK,
749 n);
750 if (ret < 0) {
751 fprintf(stderr, "Illegal \"dst_mac\"\n");
752 return -1;
753 }
754 } else if (matches(*argv, "src_mac") == 0) {
755 NEXT_ARG();
756 ret = flower_parse_eth_addr(*argv,
757 TCA_FLOWER_KEY_ETH_SRC,
758 TCA_FLOWER_KEY_ETH_SRC_MASK,
759 n);
760 if (ret < 0) {
761 fprintf(stderr, "Illegal \"src_mac\"\n");
762 return -1;
763 }
764 } else if (matches(*argv, "ip_proto") == 0) {
765 NEXT_ARG();
766 ret = flower_parse_ip_proto(*argv, vlan_ethtype ?
767 vlan_ethtype : eth_type,
768 TCA_FLOWER_KEY_IP_PROTO,
769 &ip_proto, n);
770 if (ret < 0) {
771 fprintf(stderr, "Illegal \"ip_proto\"\n");
772 return -1;
773 }
774 } else if (matches(*argv, "ip_tos") == 0) {
775 NEXT_ARG();
776 ret = flower_parse_ip_tos_ttl(*argv,
777 TCA_FLOWER_KEY_IP_TOS,
778 TCA_FLOWER_KEY_IP_TOS_MASK,
779 n);
780 if (ret < 0) {
781 fprintf(stderr, "Illegal \"ip_tos\"\n");
782 return -1;
783 }
784 } else if (matches(*argv, "ip_ttl") == 0) {
785 NEXT_ARG();
786 ret = flower_parse_ip_tos_ttl(*argv,
787 TCA_FLOWER_KEY_IP_TTL,
788 TCA_FLOWER_KEY_IP_TTL_MASK,
789 n);
790 if (ret < 0) {
791 fprintf(stderr, "Illegal \"ip_ttl\"\n");
792 return -1;
793 }
794 } else if (matches(*argv, "dst_ip") == 0) {
795 NEXT_ARG();
796 ret = flower_parse_ip_addr(*argv, vlan_ethtype ?
797 vlan_ethtype : eth_type,
798 TCA_FLOWER_KEY_IPV4_DST,
799 TCA_FLOWER_KEY_IPV4_DST_MASK,
800 TCA_FLOWER_KEY_IPV6_DST,
801 TCA_FLOWER_KEY_IPV6_DST_MASK,
802 n);
803 if (ret < 0) {
804 fprintf(stderr, "Illegal \"dst_ip\"\n");
805 return -1;
806 }
807 } else if (matches(*argv, "src_ip") == 0) {
808 NEXT_ARG();
809 ret = flower_parse_ip_addr(*argv, vlan_ethtype ?
810 vlan_ethtype : eth_type,
811 TCA_FLOWER_KEY_IPV4_SRC,
812 TCA_FLOWER_KEY_IPV4_SRC_MASK,
813 TCA_FLOWER_KEY_IPV6_SRC,
814 TCA_FLOWER_KEY_IPV6_SRC_MASK,
815 n);
816 if (ret < 0) {
817 fprintf(stderr, "Illegal \"src_ip\"\n");
818 return -1;
819 }
820 } else if (matches(*argv, "dst_port") == 0) {
821 NEXT_ARG();
822 ret = flower_parse_port(*argv, ip_proto,
823 FLOWER_ENDPOINT_DST, n);
824 if (ret < 0) {
825 fprintf(stderr, "Illegal \"dst_port\"\n");
826 return -1;
827 }
828 } else if (matches(*argv, "src_port") == 0) {
829 NEXT_ARG();
830 ret = flower_parse_port(*argv, ip_proto,
831 FLOWER_ENDPOINT_SRC, n);
832 if (ret < 0) {
833 fprintf(stderr, "Illegal \"src_port\"\n");
834 return -1;
835 }
836 } else if (matches(*argv, "tcp_flags") == 0) {
837 NEXT_ARG();
838 ret = flower_parse_tcp_flags(*argv,
839 TCA_FLOWER_KEY_TCP_FLAGS,
840 TCA_FLOWER_KEY_TCP_FLAGS_MASK,
841 n);
842 if (ret < 0) {
843 fprintf(stderr, "Illegal \"tcp_flags\"\n");
844 return -1;
845 }
846 } else if (matches(*argv, "type") == 0) {
847 NEXT_ARG();
848 ret = flower_parse_icmp(*argv, eth_type, ip_proto,
849 FLOWER_ICMP_FIELD_TYPE, n);
850 if (ret < 0) {
851 fprintf(stderr, "Illegal \"icmp type\"\n");
852 return -1;
853 }
854 } else if (matches(*argv, "code") == 0) {
855 NEXT_ARG();
856 ret = flower_parse_icmp(*argv, eth_type, ip_proto,
857 FLOWER_ICMP_FIELD_CODE, n);
858 if (ret < 0) {
859 fprintf(stderr, "Illegal \"icmp code\"\n");
860 return -1;
861 }
862 } else if (matches(*argv, "arp_tip") == 0) {
863 NEXT_ARG();
864 ret = flower_parse_arp_ip_addr(*argv, vlan_ethtype ?
865 vlan_ethtype : eth_type,
866 TCA_FLOWER_KEY_ARP_TIP,
867 TCA_FLOWER_KEY_ARP_TIP_MASK,
868 n);
869 if (ret < 0) {
870 fprintf(stderr, "Illegal \"arp_tip\"\n");
871 return -1;
872 }
873 } else if (matches(*argv, "arp_sip") == 0) {
874 NEXT_ARG();
875 ret = flower_parse_arp_ip_addr(*argv, vlan_ethtype ?
876 vlan_ethtype : eth_type,
877 TCA_FLOWER_KEY_ARP_SIP,
878 TCA_FLOWER_KEY_ARP_SIP_MASK,
879 n);
880 if (ret < 0) {
881 fprintf(stderr, "Illegal \"arp_sip\"\n");
882 return -1;
883 }
884 } else if (matches(*argv, "arp_op") == 0) {
885 NEXT_ARG();
886 ret = flower_parse_arp_op(*argv, vlan_ethtype ?
887 vlan_ethtype : eth_type,
888 TCA_FLOWER_KEY_ARP_OP,
889 TCA_FLOWER_KEY_ARP_OP_MASK,
890 n);
891 if (ret < 0) {
892 fprintf(stderr, "Illegal \"arp_op\"\n");
893 return -1;
894 }
895 } else if (matches(*argv, "arp_tha") == 0) {
896 NEXT_ARG();
897 ret = flower_parse_eth_addr(*argv,
898 TCA_FLOWER_KEY_ARP_THA,
899 TCA_FLOWER_KEY_ARP_THA_MASK,
900 n);
901 if (ret < 0) {
902 fprintf(stderr, "Illegal \"arp_tha\"\n");
903 return -1;
904 }
905 } else if (matches(*argv, "arp_sha") == 0) {
906 NEXT_ARG();
907 ret = flower_parse_eth_addr(*argv,
908 TCA_FLOWER_KEY_ARP_SHA,
909 TCA_FLOWER_KEY_ARP_SHA_MASK,
910 n);
911 if (ret < 0) {
912 fprintf(stderr, "Illegal \"arp_sha\"\n");
913 return -1;
914 }
915 } else if (matches(*argv, "enc_dst_ip") == 0) {
916 NEXT_ARG();
917 ret = flower_parse_ip_addr(*argv, 0,
918 TCA_FLOWER_KEY_ENC_IPV4_DST,
919 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
920 TCA_FLOWER_KEY_ENC_IPV6_DST,
921 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
922 n);
923 if (ret < 0) {
924 fprintf(stderr, "Illegal \"enc_dst_ip\"\n");
925 return -1;
926 }
927 } else if (matches(*argv, "enc_src_ip") == 0) {
928 NEXT_ARG();
929 ret = flower_parse_ip_addr(*argv, 0,
930 TCA_FLOWER_KEY_ENC_IPV4_SRC,
931 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
932 TCA_FLOWER_KEY_ENC_IPV6_SRC,
933 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
934 n);
935 if (ret < 0) {
936 fprintf(stderr, "Illegal \"enc_src_ip\"\n");
937 return -1;
938 }
939 } else if (matches(*argv, "enc_key_id") == 0) {
940 NEXT_ARG();
941 ret = flower_parse_key_id(*argv,
942 TCA_FLOWER_KEY_ENC_KEY_ID, n);
943 if (ret < 0) {
944 fprintf(stderr, "Illegal \"enc_key_id\"\n");
945 return -1;
946 }
947 } else if (matches(*argv, "enc_dst_port") == 0) {
948 NEXT_ARG();
949 ret = flower_parse_enc_port(*argv,
950 TCA_FLOWER_KEY_ENC_UDP_DST_PORT, n);
951 if (ret < 0) {
952 fprintf(stderr, "Illegal \"enc_dst_port\"\n");
953 return -1;
954 }
955 } else if (matches(*argv, "action") == 0) {
956 NEXT_ARG();
957 ret = parse_action(&argc, &argv, TCA_FLOWER_ACT, n);
958 if (ret) {
959 fprintf(stderr, "Illegal \"action\"\n");
960 return -1;
961 }
962 continue;
963 } else if (strcmp(*argv, "help") == 0) {
964 explain();
965 return -1;
966 } else {
967 fprintf(stderr, "What is \"%s\"?\n", *argv);
968 explain();
969 return -1;
970 }
971 argc--; argv++;
972 }
973
974 parse_done:
975 ret = addattr32(n, MAX_MSG, TCA_FLOWER_FLAGS, flags);
976 if (ret)
977 return ret;
978
979 if (mtf_mask) {
980 ret = addattr32(n, MAX_MSG, TCA_FLOWER_KEY_FLAGS, htonl(mtf));
981 if (ret)
982 return ret;
983
984 ret = addattr32(n, MAX_MSG, TCA_FLOWER_KEY_FLAGS_MASK, htonl(mtf_mask));
985 if (ret)
986 return ret;
987 }
988
989 if (eth_type != htons(ETH_P_ALL)) {
990 ret = addattr16(n, MAX_MSG, TCA_FLOWER_KEY_ETH_TYPE, eth_type);
991 if (ret)
992 return ret;
993 }
994
995 tail->rta_len = (((void *)n)+n->nlmsg_len) - (void *)tail;
996
997 return 0;
998 }
999
1000 static int __mask_bits(char *addr, size_t len)
1001 {
1002 int bits = 0;
1003 bool hole = false;
1004 int i;
1005 int j;
1006
1007 for (i = 0; i < len; i++, addr++) {
1008 for (j = 7; j >= 0; j--) {
1009 if (((*addr) >> j) & 0x1) {
1010 if (hole)
1011 return -1;
1012 bits++;
1013 } else if (bits) {
1014 hole = true;
1015 } else{
1016 return -1;
1017 }
1018 }
1019 }
1020 return bits;
1021 }
1022
1023 static void flower_print_eth_addr(FILE *f, char *name,
1024 struct rtattr *addr_attr,
1025 struct rtattr *mask_attr)
1026 {
1027 SPRINT_BUF(b1);
1028 int bits;
1029
1030 if (!addr_attr || RTA_PAYLOAD(addr_attr) != ETH_ALEN)
1031 return;
1032 fprintf(f, "\n %s %s", name, ll_addr_n2a(RTA_DATA(addr_attr), ETH_ALEN,
1033 0, b1, sizeof(b1)));
1034 if (!mask_attr || RTA_PAYLOAD(mask_attr) != ETH_ALEN)
1035 return;
1036 bits = __mask_bits(RTA_DATA(mask_attr), ETH_ALEN);
1037 if (bits < 0)
1038 fprintf(f, "/%s", ll_addr_n2a(RTA_DATA(mask_attr), ETH_ALEN,
1039 0, b1, sizeof(b1)));
1040 else if (bits < ETH_ALEN * 8)
1041 fprintf(f, "/%d", bits);
1042 }
1043
1044 static void flower_print_eth_type(FILE *f, __be16 *p_eth_type,
1045 struct rtattr *eth_type_attr)
1046 {
1047 __be16 eth_type;
1048
1049 if (!eth_type_attr)
1050 return;
1051
1052 eth_type = rta_getattr_u16(eth_type_attr);
1053 fprintf(f, "\n eth_type ");
1054 if (eth_type == htons(ETH_P_IP))
1055 fprintf(f, "ipv4");
1056 else if (eth_type == htons(ETH_P_IPV6))
1057 fprintf(f, "ipv6");
1058 else if (eth_type == htons(ETH_P_ARP))
1059 fprintf(f, "arp");
1060 else if (eth_type == htons(ETH_P_RARP))
1061 fprintf(f, "rarp");
1062 else
1063 fprintf(f, "%04x", ntohs(eth_type));
1064 *p_eth_type = eth_type;
1065 }
1066
1067 static void flower_print_ip_proto(FILE *f, __u8 *p_ip_proto,
1068 struct rtattr *ip_proto_attr)
1069 {
1070 __u8 ip_proto;
1071
1072 if (!ip_proto_attr)
1073 return;
1074
1075 ip_proto = rta_getattr_u8(ip_proto_attr);
1076 fprintf(f, "\n ip_proto ");
1077 if (ip_proto == IPPROTO_TCP)
1078 fprintf(f, "tcp");
1079 else if (ip_proto == IPPROTO_UDP)
1080 fprintf(f, "udp");
1081 else if (ip_proto == IPPROTO_SCTP)
1082 fprintf(f, "sctp");
1083 else if (ip_proto == IPPROTO_ICMP)
1084 fprintf(f, "icmp");
1085 else if (ip_proto == IPPROTO_ICMPV6)
1086 fprintf(f, "icmpv6");
1087 else
1088 fprintf(f, "%02x", ip_proto);
1089 *p_ip_proto = ip_proto;
1090 }
1091
1092 static void flower_print_ip_attr(FILE *f, char *name,
1093 struct rtattr *key_attr,
1094 struct rtattr *mask_attr)
1095 {
1096 if (!key_attr)
1097 return;
1098
1099 fprintf(f, "\n %s %x", name, rta_getattr_u8(key_attr));
1100 if (!mask_attr)
1101 return;
1102 fprintf(f, "/%x", rta_getattr_u8(mask_attr));
1103 }
1104
1105 static void flower_print_matching_flags(FILE *f, char *name,
1106 enum flower_matching_flags type,
1107 struct rtattr *attr,
1108 struct rtattr *mask_attr)
1109 {
1110 int i;
1111 int count = 0;
1112 __u32 mtf;
1113 __u32 mtf_mask;
1114
1115 if (!mask_attr || RTA_PAYLOAD(mask_attr) != 4)
1116 return;
1117
1118 mtf = ntohl(rta_getattr_u32(attr));
1119 mtf_mask = ntohl(rta_getattr_u32(mask_attr));
1120
1121 for (i = 0; i < ARRAY_SIZE(flags_str); i++) {
1122 if (type != flags_str[i].type)
1123 continue;
1124 if (mtf_mask & flags_str[i].flag) {
1125 if (++count == 1)
1126 fprintf(f, "\n %s ", name);
1127 else
1128 fprintf(f, "/");
1129
1130 if (mtf & flags_str[i].flag)
1131 fprintf(f, "%s", flags_str[i].string);
1132 else
1133 fprintf(f, "no%s", flags_str[i].string);
1134 }
1135 }
1136 }
1137
1138 static void flower_print_ip_addr(FILE *f, char *name, __be16 eth_type,
1139 struct rtattr *addr4_attr,
1140 struct rtattr *mask4_attr,
1141 struct rtattr *addr6_attr,
1142 struct rtattr *mask6_attr)
1143 {
1144 struct rtattr *addr_attr;
1145 struct rtattr *mask_attr;
1146 int family;
1147 size_t len;
1148 int bits;
1149
1150 if (eth_type == htons(ETH_P_IP)) {
1151 family = AF_INET;
1152 addr_attr = addr4_attr;
1153 mask_attr = mask4_attr;
1154 len = 4;
1155 } else if (eth_type == htons(ETH_P_IPV6)) {
1156 family = AF_INET6;
1157 addr_attr = addr6_attr;
1158 mask_attr = mask6_attr;
1159 len = 16;
1160 } else {
1161 return;
1162 }
1163 if (!addr_attr || RTA_PAYLOAD(addr_attr) != len)
1164 return;
1165 fprintf(f, "\n %s %s", name, rt_addr_n2a_rta(family, addr_attr));
1166 if (!mask_attr || RTA_PAYLOAD(mask_attr) != len)
1167 return;
1168 bits = __mask_bits(RTA_DATA(mask_attr), len);
1169 if (bits < 0)
1170 fprintf(f, "/%s", rt_addr_n2a_rta(family, mask_attr));
1171 else if (bits < len * 8)
1172 fprintf(f, "/%d", bits);
1173 }
1174 static void flower_print_ip4_addr(FILE *f, char *name,
1175 struct rtattr *addr_attr,
1176 struct rtattr *mask_attr)
1177 {
1178 return flower_print_ip_addr(f, name, htons(ETH_P_IP),
1179 addr_attr, mask_attr, 0, 0);
1180 }
1181
1182 static void flower_print_port(FILE *f, char *name, struct rtattr *attr)
1183 {
1184 if (attr)
1185 fprintf(f, "\n %s %d", name, rta_getattr_be16(attr));
1186 }
1187
1188 static void flower_print_tcp_flags(FILE *f, char *name,
1189 struct rtattr *flags_attr,
1190 struct rtattr *mask_attr)
1191 {
1192 if (!flags_attr)
1193 return;
1194 fprintf(f, "\n %s %x", name, rta_getattr_be16(flags_attr));
1195 if (!mask_attr)
1196 return;
1197 fprintf(f, "/%x", rta_getattr_be16(mask_attr));
1198 }
1199
1200
1201 static void flower_print_key_id(FILE *f, const char *name,
1202 struct rtattr *attr)
1203 {
1204 if (attr)
1205 fprintf(f, "\n %s %d", name, rta_getattr_be32(attr));
1206 }
1207
1208 static void flower_print_masked_u8(FILE *f, const char *name,
1209 struct rtattr *attr,
1210 struct rtattr *mask_attr,
1211 const char *(*value_to_str)(__u8 value))
1212 {
1213 const char *value_str = NULL;
1214 __u8 value, mask;
1215
1216 if (!attr)
1217 return;
1218
1219 value = rta_getattr_u8(attr);
1220 mask = mask_attr ? rta_getattr_u8(mask_attr) : UINT8_MAX;
1221 if (mask == UINT8_MAX && value_to_str)
1222 value_str = value_to_str(value);
1223
1224 fprintf(f, "\n %s ", name);
1225
1226 if (value_str)
1227 fputs(value_str, f);
1228 else
1229 fprintf(f, "%d", value);
1230
1231 if (mask != UINT8_MAX)
1232 fprintf(f, "/%d", mask);
1233 }
1234
1235 static void flower_print_u8(FILE *f, const char *name, struct rtattr *attr)
1236 {
1237 flower_print_masked_u8(f, name, attr, NULL, NULL);
1238 }
1239
1240 static void flower_print_u32(FILE *f, const char *name, struct rtattr *attr)
1241 {
1242 __u32 value;
1243
1244 if (!attr)
1245 return;
1246
1247 value = rta_getattr_u32(attr);
1248
1249 fprintf(f, "\n %s %d", name, value);
1250 }
1251
1252 static void flower_print_arp_op(FILE *f, const char *name,
1253 struct rtattr *op_attr,
1254 struct rtattr *mask_attr)
1255 {
1256 flower_print_masked_u8(f, name, op_attr, mask_attr,
1257 flower_print_arp_op_to_name);
1258 }
1259
1260 static int flower_print_opt(struct filter_util *qu, FILE *f,
1261 struct rtattr *opt, __u32 handle)
1262 {
1263 struct rtattr *tb[TCA_FLOWER_MAX + 1];
1264 int nl_type, nl_mask_type;
1265 __be16 eth_type = 0;
1266 __u8 ip_proto = 0xff;
1267
1268 if (!opt)
1269 return 0;
1270
1271 parse_rtattr_nested(tb, TCA_FLOWER_MAX, opt);
1272
1273 if (handle)
1274 fprintf(f, "handle 0x%x ", handle);
1275
1276 if (tb[TCA_FLOWER_CLASSID]) {
1277 SPRINT_BUF(b1);
1278 fprintf(f, "classid %s ",
1279 sprint_tc_classid(rta_getattr_u32(tb[TCA_FLOWER_CLASSID]),
1280 b1));
1281 }
1282
1283 if (tb[TCA_FLOWER_INDEV]) {
1284 struct rtattr *attr = tb[TCA_FLOWER_INDEV];
1285
1286 fprintf(f, "\n indev %s", rta_getattr_str(attr));
1287 }
1288
1289 if (tb[TCA_FLOWER_KEY_VLAN_ID]) {
1290 struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_ID];
1291
1292 fprintf(f, "\n vlan_id %d", rta_getattr_u16(attr));
1293 }
1294
1295 if (tb[TCA_FLOWER_KEY_VLAN_PRIO]) {
1296 struct rtattr *attr = tb[TCA_FLOWER_KEY_VLAN_PRIO];
1297
1298 fprintf(f, "\n vlan_prio %d", rta_getattr_u8(attr));
1299 }
1300
1301 flower_print_eth_addr(f, "dst_mac", tb[TCA_FLOWER_KEY_ETH_DST],
1302 tb[TCA_FLOWER_KEY_ETH_DST_MASK]);
1303 flower_print_eth_addr(f, "src_mac", tb[TCA_FLOWER_KEY_ETH_SRC],
1304 tb[TCA_FLOWER_KEY_ETH_SRC_MASK]);
1305
1306 flower_print_eth_type(f, &eth_type, tb[TCA_FLOWER_KEY_ETH_TYPE]);
1307 flower_print_ip_proto(f, &ip_proto, tb[TCA_FLOWER_KEY_IP_PROTO]);
1308
1309 flower_print_ip_attr(f, "ip_tos", tb[TCA_FLOWER_KEY_IP_TOS],
1310 tb[TCA_FLOWER_KEY_IP_TOS_MASK]);
1311 flower_print_ip_attr(f, "ip_ttl", tb[TCA_FLOWER_KEY_IP_TTL],
1312 tb[TCA_FLOWER_KEY_IP_TTL_MASK]);
1313
1314 flower_print_u32(f, "mpls_label", tb[TCA_FLOWER_KEY_MPLS_LABEL]);
1315 flower_print_u8(f, "mpls_tc", tb[TCA_FLOWER_KEY_MPLS_TC]);
1316 flower_print_u8(f, "mpls_bos", tb[TCA_FLOWER_KEY_MPLS_BOS]);
1317 flower_print_u8(f, "mpls_ttl", tb[TCA_FLOWER_KEY_MPLS_TTL]);
1318
1319 flower_print_ip_addr(f, "dst_ip", eth_type,
1320 tb[TCA_FLOWER_KEY_IPV4_DST],
1321 tb[TCA_FLOWER_KEY_IPV4_DST_MASK],
1322 tb[TCA_FLOWER_KEY_IPV6_DST],
1323 tb[TCA_FLOWER_KEY_IPV6_DST_MASK]);
1324
1325 flower_print_ip_addr(f, "src_ip", eth_type,
1326 tb[TCA_FLOWER_KEY_IPV4_SRC],
1327 tb[TCA_FLOWER_KEY_IPV4_SRC_MASK],
1328 tb[TCA_FLOWER_KEY_IPV6_SRC],
1329 tb[TCA_FLOWER_KEY_IPV6_SRC_MASK]);
1330
1331 nl_type = flower_port_attr_type(ip_proto, FLOWER_ENDPOINT_DST);
1332 if (nl_type >= 0)
1333 flower_print_port(f, "dst_port", tb[nl_type]);
1334 nl_type = flower_port_attr_type(ip_proto, FLOWER_ENDPOINT_SRC);
1335 if (nl_type >= 0)
1336 flower_print_port(f, "src_port", tb[nl_type]);
1337
1338 flower_print_tcp_flags(f, "tcp_flags", tb[TCA_FLOWER_KEY_TCP_FLAGS],
1339 tb[TCA_FLOWER_KEY_TCP_FLAGS_MASK]);
1340
1341 nl_type = flower_icmp_attr_type(eth_type, ip_proto,
1342 FLOWER_ICMP_FIELD_TYPE);
1343 nl_mask_type = flower_icmp_attr_mask_type(eth_type, ip_proto,
1344 FLOWER_ICMP_FIELD_TYPE);
1345 if (nl_type >= 0 && nl_mask_type >= 0)
1346 flower_print_masked_u8(f, "icmp_type", tb[nl_type],
1347 tb[nl_mask_type], NULL);
1348
1349 nl_type = flower_icmp_attr_type(eth_type, ip_proto,
1350 FLOWER_ICMP_FIELD_CODE);
1351 nl_mask_type = flower_icmp_attr_mask_type(eth_type, ip_proto,
1352 FLOWER_ICMP_FIELD_CODE);
1353 if (nl_type >= 0 && nl_mask_type >= 0)
1354 flower_print_masked_u8(f, "icmp_code", tb[nl_type],
1355 tb[nl_mask_type], NULL);
1356
1357 flower_print_ip4_addr(f, "arp_sip", tb[TCA_FLOWER_KEY_ARP_SIP],
1358 tb[TCA_FLOWER_KEY_ARP_SIP_MASK]);
1359 flower_print_ip4_addr(f, "arp_tip", tb[TCA_FLOWER_KEY_ARP_TIP],
1360 tb[TCA_FLOWER_KEY_ARP_TIP_MASK]);
1361 flower_print_arp_op(f, "arp_op", tb[TCA_FLOWER_KEY_ARP_OP],
1362 tb[TCA_FLOWER_KEY_ARP_OP_MASK]);
1363 flower_print_eth_addr(f, "arp_sha", tb[TCA_FLOWER_KEY_ARP_SHA],
1364 tb[TCA_FLOWER_KEY_ARP_SHA_MASK]);
1365 flower_print_eth_addr(f, "arp_tha", tb[TCA_FLOWER_KEY_ARP_THA],
1366 tb[TCA_FLOWER_KEY_ARP_THA_MASK]);
1367
1368 flower_print_ip_addr(f, "enc_dst_ip",
1369 tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] ?
1370 htons(ETH_P_IP) : htons(ETH_P_IPV6),
1371 tb[TCA_FLOWER_KEY_ENC_IPV4_DST],
1372 tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK],
1373 tb[TCA_FLOWER_KEY_ENC_IPV6_DST],
1374 tb[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK]);
1375
1376 flower_print_ip_addr(f, "enc_src_ip",
1377 tb[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] ?
1378 htons(ETH_P_IP) : htons(ETH_P_IPV6),
1379 tb[TCA_FLOWER_KEY_ENC_IPV4_SRC],
1380 tb[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK],
1381 tb[TCA_FLOWER_KEY_ENC_IPV6_SRC],
1382 tb[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK]);
1383
1384 flower_print_key_id(f, "enc_key_id",
1385 tb[TCA_FLOWER_KEY_ENC_KEY_ID]);
1386
1387 flower_print_port(f, "enc_dst_port",
1388 tb[TCA_FLOWER_KEY_ENC_UDP_DST_PORT]);
1389
1390 flower_print_matching_flags(f, "ip_flags",
1391 FLOWER_IP_FLAGS,
1392 tb[TCA_FLOWER_KEY_FLAGS],
1393 tb[TCA_FLOWER_KEY_FLAGS_MASK]);
1394
1395 if (tb[TCA_FLOWER_FLAGS]) {
1396 __u32 flags = rta_getattr_u32(tb[TCA_FLOWER_FLAGS]);
1397
1398 if (flags & TCA_CLS_FLAGS_SKIP_HW)
1399 fprintf(f, "\n skip_hw");
1400 if (flags & TCA_CLS_FLAGS_SKIP_SW)
1401 fprintf(f, "\n skip_sw");
1402
1403 if (flags & TCA_CLS_FLAGS_IN_HW)
1404 fprintf(f, "\n in_hw");
1405 else if (flags & TCA_CLS_FLAGS_NOT_IN_HW)
1406 fprintf(f, "\n not_in_hw");
1407 }
1408
1409 if (tb[TCA_FLOWER_ACT])
1410 tc_print_action(f, tb[TCA_FLOWER_ACT], 0);
1411
1412 return 0;
1413 }
1414
1415 struct filter_util flower_filter_util = {
1416 .id = "flower",
1417 .parse_fopt = flower_parse_opt,
1418 .print_fopt = flower_print_opt,
1419 };