2 * f_flower.c Flower Classifier
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.
9 * Authors: Jiri Pirko <jiri@resnulli.us>
18 #include <linux/if_arp.h>
19 #include <linux/if_ether.h>
21 #include <linux/tc_act/tc_vlan.h>
27 enum flower_matching_flags
{
31 enum flower_endpoint
{
36 enum flower_icmp_field
{
37 FLOWER_ICMP_FIELD_TYPE
,
38 FLOWER_ICMP_FIELD_CODE
41 static void explain(void)
44 "Usage: ... flower [ MATCH-LIST ]\n"
45 " [ skip_sw | skip_hw ]\n"
46 " [ action ACTION-SPEC ] [ classid CLASSID ]\n"
48 "Where: MATCH-LIST := [ MATCH-LIST ] MATCH\n"
49 " MATCH := { indev DEV-NAME |\n"
51 " vlan_prio PRIORITY |\n"
52 " vlan_ethtype [ ipv4 | ipv6 | ETH-TYPE ] |\n"
53 " dst_mac MASKED-LLADDR |\n"
54 " src_mac MASKED-LLADDR |\n"
55 " ip_proto [tcp | udp | sctp | icmp | icmpv6 | IP-PROTO ] |\n"
58 " dst_port PORT-NUMBER |\n"
59 " src_port PORT-NUMBER |\n"
60 " type MASKED-ICMP-TYPE |\n"
61 " code MASKED-ICMP-CODE |\n"
62 " arp_tip IPV4-PREFIX |\n"
63 " arp_sip IPV4-PREFIX |\n"
64 " arp_op [ request | reply | OP ] |\n"
65 " arp_tha MASKED-LLADDR |\n"
66 " arp_sha MASKED-LLADDR |\n"
67 " enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
68 " enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
69 " enc_key_id [ KEY-ID ] |\n"
70 " ip_flags IP-FLAGS | \n"
71 " enc_dst_port [ port_number ] }\n"
72 " FILTERID := X:Y:Z\n"
73 " MASKED_LLADDR := { LLADDR | LLADDR/MASK | LLADDR/BITS }\n"
74 " ACTION-SPEC := ... look at individual actions\n"
76 "NOTE: CLASSID, IP-PROTO are parsed as hexadecimal input.\n"
77 "NOTE: There can be only used one mask per one prio. If user needs\n"
78 " to specify different mask, he has to use different prio.\n");
81 static int flower_parse_eth_addr(char *str
, int addr_type
, int mask_type
,
85 char addr
[ETH_ALEN
], *slash
;
87 slash
= strchr(str
, '/');
91 ret
= ll_addr_a2n(addr
, sizeof(addr
), str
);
94 addattr_l(n
, MAX_MSG
, addr_type
, addr
, sizeof(addr
));
99 if (!get_unsigned(&bits
, slash
+ 1, 10)) {
102 /* Extra 16 bit shift to push mac address into
103 * high bits of uint64_t
105 mask
= htonll(0xffffffffffffULL
<< (16 + 48 - bits
));
106 memcpy(addr
, &mask
, ETH_ALEN
);
108 ret
= ll_addr_a2n(addr
, sizeof(addr
), slash
+ 1);
113 memset(addr
, 0xff, ETH_ALEN
);
115 addattr_l(n
, MAX_MSG
, mask_type
, addr
, sizeof(addr
));
124 static int flower_parse_vlan_eth_type(char *str
, __be16 eth_type
, int type
,
125 __be16
*p_vlan_eth_type
,
128 __be16 vlan_eth_type
;
130 if (eth_type
!= htons(ETH_P_8021Q
)) {
132 "Can't set \"vlan_ethtype\" if ethertype isn't 802.1Q\n");
136 if (ll_proto_a2n(&vlan_eth_type
, str
))
137 invarg("invalid vlan_ethtype", str
);
138 addattr16(n
, MAX_MSG
, type
, vlan_eth_type
);
139 *p_vlan_eth_type
= vlan_eth_type
;
143 struct flag_to_string
{
145 enum flower_matching_flags type
;
149 static struct flag_to_string flags_str
[] = {
150 { TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT
, FLOWER_IP_FLAGS
, "frag" },
153 static int flower_parse_matching_flags(char *str
,
154 enum flower_matching_flags type
,
155 __u32
*mtf
, __u32
*mtf_mask
)
162 token
= strtok(str
, "/");
165 if (!strncmp(token
, "no", 2)) {
172 for (i
= 0; i
< ARRAY_SIZE(flags_str
); i
++) {
173 if (type
!= flags_str
[i
].type
)
176 if (!strcmp(token
, flags_str
[i
].string
)) {
178 *mtf
&= ~flags_str
[i
].flag
;
180 *mtf
|= flags_str
[i
].flag
;
182 *mtf_mask
|= flags_str
[i
].flag
;
190 token
= strtok(NULL
, "/");
196 static int flower_parse_ip_proto(char *str
, __be16 eth_type
, int type
,
197 __u8
*p_ip_proto
, struct nlmsghdr
*n
)
202 if (eth_type
!= htons(ETH_P_IP
) && eth_type
!= htons(ETH_P_IPV6
))
205 if (matches(str
, "tcp") == 0) {
206 ip_proto
= IPPROTO_TCP
;
207 } else if (matches(str
, "udp") == 0) {
208 ip_proto
= IPPROTO_UDP
;
209 } else if (matches(str
, "sctp") == 0) {
210 ip_proto
= IPPROTO_SCTP
;
211 } else if (matches(str
, "icmp") == 0) {
212 if (eth_type
!= htons(ETH_P_IP
))
214 ip_proto
= IPPROTO_ICMP
;
215 } else if (matches(str
, "icmpv6") == 0) {
216 if (eth_type
!= htons(ETH_P_IPV6
))
218 ip_proto
= IPPROTO_ICMPV6
;
220 ret
= get_u8(&ip_proto
, str
, 16);
224 addattr8(n
, MAX_MSG
, type
, ip_proto
);
225 *p_ip_proto
= ip_proto
;
229 fprintf(stderr
, "Illegal \"eth_type\" for ip proto\n");
233 static int __flower_parse_ip_addr(char *str
, int family
,
234 int addr4_type
, int mask4_type
,
235 int addr6_type
, int mask6_type
,
243 ret
= get_prefix(&addr
, str
, family
);
247 if (family
&& (addr
.family
!= family
)) {
248 fprintf(stderr
, "Illegal \"eth_type\" for ip address\n");
252 addattr_l(n
, MAX_MSG
, addr
.family
== AF_INET
? addr4_type
: addr6_type
,
253 addr
.data
, addr
.bytelen
);
255 memset(addr
.data
, 0xff, addr
.bytelen
);
257 for (i
= 0; i
< addr
.bytelen
/ 4; i
++) {
260 } else if (bits
/ 32 >= 1) {
263 addr
.data
[i
] <<= 32 - bits
;
264 addr
.data
[i
] = htonl(addr
.data
[i
]);
269 addattr_l(n
, MAX_MSG
, addr
.family
== AF_INET
? mask4_type
: mask6_type
,
270 addr
.data
, addr
.bytelen
);
275 static int flower_parse_ip_addr(char *str
, __be16 eth_type
,
276 int addr4_type
, int mask4_type
,
277 int addr6_type
, int mask6_type
,
282 if (eth_type
== htons(ETH_P_IP
)) {
284 } else if (eth_type
== htons(ETH_P_IPV6
)) {
286 } else if (!eth_type
) {
292 return __flower_parse_ip_addr(str
, family
, addr4_type
, mask4_type
,
293 addr6_type
, mask6_type
, n
);
296 static bool flower_eth_type_arp(__be16 eth_type
)
298 return eth_type
== htons(ETH_P_ARP
) || eth_type
== htons(ETH_P_RARP
);
301 static int flower_parse_arp_ip_addr(char *str
, __be16 eth_type
,
302 int addr_type
, int mask_type
,
305 if (!flower_eth_type_arp(eth_type
))
308 return __flower_parse_ip_addr(str
, AF_INET
, addr_type
, mask_type
,
309 TCA_FLOWER_UNSPEC
, TCA_FLOWER_UNSPEC
, n
);
312 static int flower_parse_u8(char *str
, int value_type
, int mask_type
,
313 int (*value_from_name
)(const char *str
,
315 bool (*value_validate
)(__u8 value
),
322 slash
= strchr(str
, '/');
326 ret
= value_from_name
? value_from_name(str
, &value
) : -1;
328 ret
= get_u8(&value
, str
, 10);
333 if (value_validate
&& !value_validate(value
))
337 ret
= get_u8(&mask
, slash
+ 1, 10);
345 addattr8(n
, MAX_MSG
, value_type
, value
);
346 addattr8(n
, MAX_MSG
, mask_type
, mask
);
355 static const char *flower_print_arp_op_to_name(__u8 op
)
367 static int flower_arp_op_from_name(const char *name
, __u8
*op
)
369 if (!strcmp(name
, "request"))
371 else if (!strcmp(name
, "reply"))
379 static bool flow_arp_op_validate(__u8 op
)
381 return !op
|| op
== ARPOP_REQUEST
|| op
== ARPOP_REPLY
;
384 static int flower_parse_arp_op(char *str
, __be16 eth_type
,
385 int op_type
, int mask_type
,
388 if (!flower_eth_type_arp(eth_type
))
391 return flower_parse_u8(str
, op_type
, mask_type
, flower_arp_op_from_name
,
392 flow_arp_op_validate
, n
);
395 static int flower_icmp_attr_type(__be16 eth_type
, __u8 ip_proto
,
396 enum flower_icmp_field field
)
398 if (eth_type
== htons(ETH_P_IP
) && ip_proto
== IPPROTO_ICMP
)
399 return field
== FLOWER_ICMP_FIELD_CODE
?
400 TCA_FLOWER_KEY_ICMPV4_CODE
:
401 TCA_FLOWER_KEY_ICMPV4_TYPE
;
402 else if (eth_type
== htons(ETH_P_IPV6
) && ip_proto
== IPPROTO_ICMPV6
)
403 return field
== FLOWER_ICMP_FIELD_CODE
?
404 TCA_FLOWER_KEY_ICMPV6_CODE
:
405 TCA_FLOWER_KEY_ICMPV6_TYPE
;
410 static int flower_icmp_attr_mask_type(__be16 eth_type
, __u8 ip_proto
,
411 enum flower_icmp_field field
)
413 if (eth_type
== htons(ETH_P_IP
) && ip_proto
== IPPROTO_ICMP
)
414 return field
== FLOWER_ICMP_FIELD_CODE
?
415 TCA_FLOWER_KEY_ICMPV4_CODE_MASK
:
416 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK
;
417 else if (eth_type
== htons(ETH_P_IPV6
) && ip_proto
== IPPROTO_ICMPV6
)
418 return field
== FLOWER_ICMP_FIELD_CODE
?
419 TCA_FLOWER_KEY_ICMPV6_CODE_MASK
:
420 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK
;
425 static int flower_parse_icmp(char *str
, __u16 eth_type
, __u8 ip_proto
,
426 enum flower_icmp_field field
, struct nlmsghdr
*n
)
428 int value_type
, mask_type
;
430 value_type
= flower_icmp_attr_type(eth_type
, ip_proto
, field
);
431 mask_type
= flower_icmp_attr_mask_type(eth_type
, ip_proto
, field
);
432 if (value_type
< 0 || mask_type
< 0)
435 return flower_parse_u8(str
, value_type
, mask_type
, NULL
, NULL
, n
);
438 static int flower_port_attr_type(__u8 ip_proto
, enum flower_endpoint endpoint
)
440 if (ip_proto
== IPPROTO_TCP
)
441 return endpoint
== FLOWER_ENDPOINT_SRC
?
442 TCA_FLOWER_KEY_TCP_SRC
:
443 TCA_FLOWER_KEY_TCP_DST
;
444 else if (ip_proto
== IPPROTO_UDP
)
445 return endpoint
== FLOWER_ENDPOINT_SRC
?
446 TCA_FLOWER_KEY_UDP_SRC
:
447 TCA_FLOWER_KEY_UDP_DST
;
448 else if (ip_proto
== IPPROTO_SCTP
)
449 return endpoint
== FLOWER_ENDPOINT_SRC
?
450 TCA_FLOWER_KEY_SCTP_SRC
:
451 TCA_FLOWER_KEY_SCTP_DST
;
456 static int flower_parse_port(char *str
, __u8 ip_proto
,
457 enum flower_endpoint endpoint
,
464 type
= flower_port_attr_type(ip_proto
, endpoint
);
468 ret
= get_be16(&port
, str
, 10);
472 addattr16(n
, MAX_MSG
, type
, port
);
477 static int flower_parse_key_id(const char *str
, int type
, struct nlmsghdr
*n
)
482 ret
= get_be32(&key_id
, str
, 10);
484 addattr32(n
, MAX_MSG
, type
, key_id
);
489 static int flower_parse_enc_port(char *str
, int type
, struct nlmsghdr
*n
)
494 ret
= get_be16(&port
, str
, 10);
498 addattr16(n
, MAX_MSG
, type
, port
);
503 static int flower_parse_opt(struct filter_util
*qu
, char *handle
,
504 int argc
, char **argv
, struct nlmsghdr
*n
)
507 struct tcmsg
*t
= NLMSG_DATA(n
);
509 __be16 eth_type
= TC_H_MIN(t
->tcm_info
);
510 __be16 vlan_ethtype
= 0;
511 __u8 ip_proto
= 0xff;
517 ret
= get_u32(&t
->tcm_handle
, handle
, 0);
519 fprintf(stderr
, "Illegal \"handle\"\n");
524 tail
= (struct rtattr
*) (((void *) n
) + NLMSG_ALIGN(n
->nlmsg_len
));
525 addattr_l(n
, MAX_MSG
, TCA_OPTIONS
, NULL
, 0);
528 /*at minimal we will match all ethertype packets */
533 if (matches(*argv
, "classid") == 0 ||
534 matches(*argv
, "flowid") == 0) {
538 ret
= get_tc_classid(&handle
, *argv
);
540 fprintf(stderr
, "Illegal \"classid\"\n");
543 addattr_l(n
, MAX_MSG
, TCA_FLOWER_CLASSID
, &handle
, 4);
544 } else if (matches(*argv
, "ip_flags") == 0) {
546 ret
= flower_parse_matching_flags(*argv
,
551 fprintf(stderr
, "Illegal \"ip_flags\"\n");
554 } else if (matches(*argv
, "skip_hw") == 0) {
555 flags
|= TCA_CLS_FLAGS_SKIP_HW
;
556 } else if (matches(*argv
, "skip_sw") == 0) {
557 flags
|= TCA_CLS_FLAGS_SKIP_SW
;
558 } else if (matches(*argv
, "indev") == 0) {
559 char ifname
[IFNAMSIZ
] = {};
562 strncpy(ifname
, *argv
, sizeof(ifname
) - 1);
563 addattrstrz(n
, MAX_MSG
, TCA_FLOWER_INDEV
, ifname
);
564 } else if (matches(*argv
, "vlan_id") == 0) {
568 if (eth_type
!= htons(ETH_P_8021Q
)) {
570 "Can't set \"vlan_id\" if ethertype isn't 802.1Q\n");
573 ret
= get_u16(&vid
, *argv
, 10);
574 if (ret
< 0 || vid
& ~0xfff) {
575 fprintf(stderr
, "Illegal \"vlan_id\"\n");
578 addattr16(n
, MAX_MSG
, TCA_FLOWER_KEY_VLAN_ID
, vid
);
579 } else if (matches(*argv
, "vlan_prio") == 0) {
583 if (eth_type
!= htons(ETH_P_8021Q
)) {
585 "Can't set \"vlan_prio\" if ethertype isn't 802.1Q\n");
588 ret
= get_u8(&vlan_prio
, *argv
, 10);
589 if (ret
< 0 || vlan_prio
& ~0x7) {
590 fprintf(stderr
, "Illegal \"vlan_prio\"\n");
594 TCA_FLOWER_KEY_VLAN_PRIO
, vlan_prio
);
595 } else if (matches(*argv
, "vlan_ethtype") == 0) {
597 ret
= flower_parse_vlan_eth_type(*argv
, eth_type
,
598 TCA_FLOWER_KEY_VLAN_ETH_TYPE
,
602 } else if (matches(*argv
, "dst_mac") == 0) {
604 ret
= flower_parse_eth_addr(*argv
,
605 TCA_FLOWER_KEY_ETH_DST
,
606 TCA_FLOWER_KEY_ETH_DST_MASK
,
609 fprintf(stderr
, "Illegal \"dst_mac\"\n");
612 } else if (matches(*argv
, "src_mac") == 0) {
614 ret
= flower_parse_eth_addr(*argv
,
615 TCA_FLOWER_KEY_ETH_SRC
,
616 TCA_FLOWER_KEY_ETH_SRC_MASK
,
619 fprintf(stderr
, "Illegal \"src_mac\"\n");
622 } else if (matches(*argv
, "ip_proto") == 0) {
624 ret
= flower_parse_ip_proto(*argv
, vlan_ethtype
?
625 vlan_ethtype
: eth_type
,
626 TCA_FLOWER_KEY_IP_PROTO
,
629 fprintf(stderr
, "Illegal \"ip_proto\"\n");
632 } else if (matches(*argv
, "dst_ip") == 0) {
634 ret
= flower_parse_ip_addr(*argv
, vlan_ethtype
?
635 vlan_ethtype
: eth_type
,
636 TCA_FLOWER_KEY_IPV4_DST
,
637 TCA_FLOWER_KEY_IPV4_DST_MASK
,
638 TCA_FLOWER_KEY_IPV6_DST
,
639 TCA_FLOWER_KEY_IPV6_DST_MASK
,
642 fprintf(stderr
, "Illegal \"dst_ip\"\n");
645 } else if (matches(*argv
, "src_ip") == 0) {
647 ret
= flower_parse_ip_addr(*argv
, vlan_ethtype
?
648 vlan_ethtype
: eth_type
,
649 TCA_FLOWER_KEY_IPV4_SRC
,
650 TCA_FLOWER_KEY_IPV4_SRC_MASK
,
651 TCA_FLOWER_KEY_IPV6_SRC
,
652 TCA_FLOWER_KEY_IPV6_SRC_MASK
,
655 fprintf(stderr
, "Illegal \"src_ip\"\n");
658 } else if (matches(*argv
, "dst_port") == 0) {
660 ret
= flower_parse_port(*argv
, ip_proto
,
661 FLOWER_ENDPOINT_DST
, n
);
663 fprintf(stderr
, "Illegal \"dst_port\"\n");
666 } else if (matches(*argv
, "src_port") == 0) {
668 ret
= flower_parse_port(*argv
, ip_proto
,
669 FLOWER_ENDPOINT_SRC
, n
);
671 fprintf(stderr
, "Illegal \"src_port\"\n");
674 } else if (matches(*argv
, "type") == 0) {
676 ret
= flower_parse_icmp(*argv
, eth_type
, ip_proto
,
677 FLOWER_ICMP_FIELD_TYPE
, n
);
679 fprintf(stderr
, "Illegal \"icmp type\"\n");
682 } else if (matches(*argv
, "code") == 0) {
684 ret
= flower_parse_icmp(*argv
, eth_type
, ip_proto
,
685 FLOWER_ICMP_FIELD_CODE
, n
);
687 fprintf(stderr
, "Illegal \"icmp code\"\n");
690 } else if (matches(*argv
, "arp_tip") == 0) {
692 ret
= flower_parse_arp_ip_addr(*argv
, vlan_ethtype
?
693 vlan_ethtype
: eth_type
,
694 TCA_FLOWER_KEY_ARP_TIP
,
695 TCA_FLOWER_KEY_ARP_TIP_MASK
,
698 fprintf(stderr
, "Illegal \"arp_tip\"\n");
701 } else if (matches(*argv
, "arp_sip") == 0) {
703 ret
= flower_parse_arp_ip_addr(*argv
, vlan_ethtype
?
704 vlan_ethtype
: eth_type
,
705 TCA_FLOWER_KEY_ARP_SIP
,
706 TCA_FLOWER_KEY_ARP_SIP_MASK
,
709 fprintf(stderr
, "Illegal \"arp_sip\"\n");
712 } else if (matches(*argv
, "arp_op") == 0) {
714 ret
= flower_parse_arp_op(*argv
, vlan_ethtype
?
715 vlan_ethtype
: eth_type
,
716 TCA_FLOWER_KEY_ARP_OP
,
717 TCA_FLOWER_KEY_ARP_OP_MASK
,
720 fprintf(stderr
, "Illegal \"arp_op\"\n");
723 } else if (matches(*argv
, "arp_tha") == 0) {
725 ret
= flower_parse_eth_addr(*argv
,
726 TCA_FLOWER_KEY_ARP_THA
,
727 TCA_FLOWER_KEY_ARP_THA_MASK
,
730 fprintf(stderr
, "Illegal \"arp_tha\"\n");
733 } else if (matches(*argv
, "arp_sha") == 0) {
735 ret
= flower_parse_eth_addr(*argv
,
736 TCA_FLOWER_KEY_ARP_SHA
,
737 TCA_FLOWER_KEY_ARP_SHA_MASK
,
740 fprintf(stderr
, "Illegal \"arp_sha\"\n");
743 } else if (matches(*argv
, "enc_dst_ip") == 0) {
745 ret
= flower_parse_ip_addr(*argv
, 0,
746 TCA_FLOWER_KEY_ENC_IPV4_DST
,
747 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
,
748 TCA_FLOWER_KEY_ENC_IPV6_DST
,
749 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK
,
752 fprintf(stderr
, "Illegal \"enc_dst_ip\"\n");
755 } else if (matches(*argv
, "enc_src_ip") == 0) {
757 ret
= flower_parse_ip_addr(*argv
, 0,
758 TCA_FLOWER_KEY_ENC_IPV4_SRC
,
759 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
,
760 TCA_FLOWER_KEY_ENC_IPV6_SRC
,
761 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK
,
764 fprintf(stderr
, "Illegal \"enc_src_ip\"\n");
767 } else if (matches(*argv
, "enc_key_id") == 0) {
769 ret
= flower_parse_key_id(*argv
,
770 TCA_FLOWER_KEY_ENC_KEY_ID
, n
);
772 fprintf(stderr
, "Illegal \"enc_key_id\"\n");
775 } else if (matches(*argv
, "enc_dst_port") == 0) {
777 ret
= flower_parse_enc_port(*argv
,
778 TCA_FLOWER_KEY_ENC_UDP_DST_PORT
, n
);
780 fprintf(stderr
, "Illegal \"enc_dst_port\"\n");
783 } else if (matches(*argv
, "action") == 0) {
785 ret
= parse_action(&argc
, &argv
, TCA_FLOWER_ACT
, n
);
787 fprintf(stderr
, "Illegal \"action\"\n");
791 } else if (strcmp(*argv
, "help") == 0) {
795 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
803 ret
= addattr32(n
, MAX_MSG
, TCA_FLOWER_FLAGS
, flags
);
808 ret
= addattr32(n
, MAX_MSG
, TCA_FLOWER_KEY_FLAGS
, htonl(mtf
));
812 ret
= addattr32(n
, MAX_MSG
, TCA_FLOWER_KEY_FLAGS_MASK
, htonl(mtf_mask
));
817 if (eth_type
!= htons(ETH_P_ALL
)) {
818 ret
= addattr16(n
, MAX_MSG
, TCA_FLOWER_KEY_ETH_TYPE
, eth_type
);
823 tail
->rta_len
= (((void *)n
)+n
->nlmsg_len
) - (void *)tail
;
828 static int __mask_bits(char *addr
, size_t len
)
835 for (i
= 0; i
< len
; i
++, addr
++) {
836 for (j
= 7; j
>= 0; j
--) {
837 if (((*addr
) >> j
) & 0x1) {
851 static void flower_print_eth_addr(FILE *f
, char *name
,
852 struct rtattr
*addr_attr
,
853 struct rtattr
*mask_attr
)
858 if (!addr_attr
|| RTA_PAYLOAD(addr_attr
) != ETH_ALEN
)
860 fprintf(f
, "\n %s %s", name
, ll_addr_n2a(RTA_DATA(addr_attr
), ETH_ALEN
,
862 if (!mask_attr
|| RTA_PAYLOAD(mask_attr
) != ETH_ALEN
)
864 bits
= __mask_bits(RTA_DATA(mask_attr
), ETH_ALEN
);
866 fprintf(f
, "/%s", ll_addr_n2a(RTA_DATA(mask_attr
), ETH_ALEN
,
868 else if (bits
< ETH_ALEN
* 8)
869 fprintf(f
, "/%d", bits
);
872 static void flower_print_eth_type(FILE *f
, __be16
*p_eth_type
,
873 struct rtattr
*eth_type_attr
)
880 eth_type
= rta_getattr_u16(eth_type_attr
);
881 fprintf(f
, "\n eth_type ");
882 if (eth_type
== htons(ETH_P_IP
))
884 else if (eth_type
== htons(ETH_P_IPV6
))
886 else if (eth_type
== htons(ETH_P_ARP
))
888 else if (eth_type
== htons(ETH_P_RARP
))
891 fprintf(f
, "%04x", ntohs(eth_type
));
892 *p_eth_type
= eth_type
;
895 static void flower_print_ip_proto(FILE *f
, __u8
*p_ip_proto
,
896 struct rtattr
*ip_proto_attr
)
903 ip_proto
= rta_getattr_u8(ip_proto_attr
);
904 fprintf(f
, "\n ip_proto ");
905 if (ip_proto
== IPPROTO_TCP
)
907 else if (ip_proto
== IPPROTO_UDP
)
909 else if (ip_proto
== IPPROTO_SCTP
)
911 else if (ip_proto
== IPPROTO_ICMP
)
913 else if (ip_proto
== IPPROTO_ICMPV6
)
914 fprintf(f
, "icmpv6");
916 fprintf(f
, "%02x", ip_proto
);
917 *p_ip_proto
= ip_proto
;
920 static void flower_print_matching_flags(FILE *f
, char *name
,
921 enum flower_matching_flags type
,
923 struct rtattr
*mask_attr
)
930 if (!mask_attr
|| RTA_PAYLOAD(mask_attr
) != 4)
933 mtf
= ntohl(rta_getattr_u32(attr
));
934 mtf_mask
= ntohl(rta_getattr_u32(mask_attr
));
936 for (i
= 0; i
< ARRAY_SIZE(flags_str
); i
++) {
937 if (type
!= flags_str
[i
].type
)
939 if (mtf_mask
& flags_str
[i
].flag
) {
941 fprintf(f
, "\n %s ", name
);
945 if (mtf
& flags_str
[i
].flag
)
946 fprintf(f
, "%s", flags_str
[i
].string
);
948 fprintf(f
, "no%s", flags_str
[i
].string
);
953 static void flower_print_ip_addr(FILE *f
, char *name
, __be16 eth_type
,
954 struct rtattr
*addr4_attr
,
955 struct rtattr
*mask4_attr
,
956 struct rtattr
*addr6_attr
,
957 struct rtattr
*mask6_attr
)
959 struct rtattr
*addr_attr
;
960 struct rtattr
*mask_attr
;
965 if (eth_type
== htons(ETH_P_IP
)) {
967 addr_attr
= addr4_attr
;
968 mask_attr
= mask4_attr
;
970 } else if (eth_type
== htons(ETH_P_IPV6
)) {
972 addr_attr
= addr6_attr
;
973 mask_attr
= mask6_attr
;
978 if (!addr_attr
|| RTA_PAYLOAD(addr_attr
) != len
)
980 fprintf(f
, "\n %s %s", name
, rt_addr_n2a_rta(family
, addr_attr
));
981 if (!mask_attr
|| RTA_PAYLOAD(mask_attr
) != len
)
983 bits
= __mask_bits(RTA_DATA(mask_attr
), len
);
985 fprintf(f
, "/%s", rt_addr_n2a_rta(family
, mask_attr
));
986 else if (bits
< len
* 8)
987 fprintf(f
, "/%d", bits
);
989 static void flower_print_ip4_addr(FILE *f
, char *name
,
990 struct rtattr
*addr_attr
,
991 struct rtattr
*mask_attr
)
993 return flower_print_ip_addr(f
, name
, htons(ETH_P_IP
),
994 addr_attr
, mask_attr
, 0, 0);
997 static void flower_print_port(FILE *f
, char *name
, struct rtattr
*attr
)
1000 fprintf(f
, "\n %s %d", name
, rta_getattr_be16(attr
));
1003 static void flower_print_key_id(FILE *f
, const char *name
,
1004 struct rtattr
*attr
)
1007 fprintf(f
, "\n %s %d", name
, rta_getattr_be32(attr
));
1010 static void flower_print_masked_u8(FILE *f
, const char *name
,
1011 struct rtattr
*attr
,
1012 struct rtattr
*mask_attr
,
1013 const char *(*value_to_str
)(__u8 value
))
1015 const char *value_str
= NULL
;
1021 value
= rta_getattr_u8(attr
);
1022 mask
= mask_attr
? rta_getattr_u8(mask_attr
) : UINT8_MAX
;
1023 if (mask
== UINT8_MAX
&& value_to_str
)
1024 value_str
= value_to_str(value
);
1026 fprintf(f
, "\n %s ", name
);
1029 fputs(value_str
, f
);
1031 fprintf(f
, "%d", value
);
1033 if (mask
!= UINT8_MAX
)
1034 fprintf(f
, "/%d", mask
);
1037 static void flower_print_arp_op(FILE *f
, const char *name
,
1038 struct rtattr
*op_attr
,
1039 struct rtattr
*mask_attr
)
1041 flower_print_masked_u8(f
, name
, op_attr
, mask_attr
,
1042 flower_print_arp_op_to_name
);
1045 static int flower_print_opt(struct filter_util
*qu
, FILE *f
,
1046 struct rtattr
*opt
, __u32 handle
)
1048 struct rtattr
*tb
[TCA_FLOWER_MAX
+ 1];
1049 int nl_type
, nl_mask_type
;
1050 __be16 eth_type
= 0;
1051 __u8 ip_proto
= 0xff;
1056 parse_rtattr_nested(tb
, TCA_FLOWER_MAX
, opt
);
1059 fprintf(f
, "handle 0x%x ", handle
);
1061 if (tb
[TCA_FLOWER_CLASSID
]) {
1063 fprintf(f
, "classid %s ",
1064 sprint_tc_classid(rta_getattr_u32(tb
[TCA_FLOWER_CLASSID
]),
1068 if (tb
[TCA_FLOWER_INDEV
]) {
1069 struct rtattr
*attr
= tb
[TCA_FLOWER_INDEV
];
1071 fprintf(f
, "\n indev %s", rta_getattr_str(attr
));
1074 if (tb
[TCA_FLOWER_KEY_VLAN_ID
]) {
1075 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_VLAN_ID
];
1077 fprintf(f
, "\n vlan_id %d", rta_getattr_u16(attr
));
1080 if (tb
[TCA_FLOWER_KEY_VLAN_PRIO
]) {
1081 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_VLAN_PRIO
];
1083 fprintf(f
, "\n vlan_prio %d", rta_getattr_u8(attr
));
1086 flower_print_eth_addr(f
, "dst_mac", tb
[TCA_FLOWER_KEY_ETH_DST
],
1087 tb
[TCA_FLOWER_KEY_ETH_DST_MASK
]);
1088 flower_print_eth_addr(f
, "src_mac", tb
[TCA_FLOWER_KEY_ETH_SRC
],
1089 tb
[TCA_FLOWER_KEY_ETH_SRC_MASK
]);
1091 flower_print_eth_type(f
, ð_type
, tb
[TCA_FLOWER_KEY_ETH_TYPE
]);
1092 flower_print_ip_proto(f
, &ip_proto
, tb
[TCA_FLOWER_KEY_IP_PROTO
]);
1094 flower_print_ip_addr(f
, "dst_ip", eth_type
,
1095 tb
[TCA_FLOWER_KEY_IPV4_DST
],
1096 tb
[TCA_FLOWER_KEY_IPV4_DST_MASK
],
1097 tb
[TCA_FLOWER_KEY_IPV6_DST
],
1098 tb
[TCA_FLOWER_KEY_IPV6_DST_MASK
]);
1100 flower_print_ip_addr(f
, "src_ip", eth_type
,
1101 tb
[TCA_FLOWER_KEY_IPV4_SRC
],
1102 tb
[TCA_FLOWER_KEY_IPV4_SRC_MASK
],
1103 tb
[TCA_FLOWER_KEY_IPV6_SRC
],
1104 tb
[TCA_FLOWER_KEY_IPV6_SRC_MASK
]);
1106 nl_type
= flower_port_attr_type(ip_proto
, FLOWER_ENDPOINT_DST
);
1108 flower_print_port(f
, "dst_port", tb
[nl_type
]);
1109 nl_type
= flower_port_attr_type(ip_proto
, FLOWER_ENDPOINT_SRC
);
1111 flower_print_port(f
, "src_port", tb
[nl_type
]);
1113 nl_type
= flower_icmp_attr_type(eth_type
, ip_proto
,
1114 FLOWER_ICMP_FIELD_TYPE
);
1115 nl_mask_type
= flower_icmp_attr_mask_type(eth_type
, ip_proto
,
1116 FLOWER_ICMP_FIELD_TYPE
);
1117 if (nl_type
>= 0 && nl_mask_type
>= 0)
1118 flower_print_masked_u8(f
, "icmp_type", tb
[nl_type
],
1119 tb
[nl_mask_type
], NULL
);
1121 nl_type
= flower_icmp_attr_type(eth_type
, ip_proto
,
1122 FLOWER_ICMP_FIELD_CODE
);
1123 nl_mask_type
= flower_icmp_attr_mask_type(eth_type
, ip_proto
,
1124 FLOWER_ICMP_FIELD_CODE
);
1125 if (nl_type
>= 0 && nl_mask_type
>= 0)
1126 flower_print_masked_u8(f
, "icmp_code", tb
[nl_type
],
1127 tb
[nl_mask_type
], NULL
);
1129 flower_print_ip4_addr(f
, "arp_sip", tb
[TCA_FLOWER_KEY_ARP_SIP
],
1130 tb
[TCA_FLOWER_KEY_ARP_SIP_MASK
]);
1131 flower_print_ip4_addr(f
, "arp_tip", tb
[TCA_FLOWER_KEY_ARP_TIP
],
1132 tb
[TCA_FLOWER_KEY_ARP_TIP_MASK
]);
1133 flower_print_arp_op(f
, "arp_op", tb
[TCA_FLOWER_KEY_ARP_OP
],
1134 tb
[TCA_FLOWER_KEY_ARP_OP_MASK
]);
1135 flower_print_eth_addr(f
, "arp_sha", tb
[TCA_FLOWER_KEY_ARP_SHA
],
1136 tb
[TCA_FLOWER_KEY_ARP_SHA_MASK
]);
1137 flower_print_eth_addr(f
, "arp_tha", tb
[TCA_FLOWER_KEY_ARP_THA
],
1138 tb
[TCA_FLOWER_KEY_ARP_THA_MASK
]);
1140 flower_print_ip_addr(f
, "enc_dst_ip",
1141 tb
[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
] ?
1142 htons(ETH_P_IP
) : htons(ETH_P_IPV6
),
1143 tb
[TCA_FLOWER_KEY_ENC_IPV4_DST
],
1144 tb
[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
],
1145 tb
[TCA_FLOWER_KEY_ENC_IPV6_DST
],
1146 tb
[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK
]);
1148 flower_print_ip_addr(f
, "enc_src_ip",
1149 tb
[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
] ?
1150 htons(ETH_P_IP
) : htons(ETH_P_IPV6
),
1151 tb
[TCA_FLOWER_KEY_ENC_IPV4_SRC
],
1152 tb
[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
],
1153 tb
[TCA_FLOWER_KEY_ENC_IPV6_SRC
],
1154 tb
[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK
]);
1156 flower_print_key_id(f
, "enc_key_id",
1157 tb
[TCA_FLOWER_KEY_ENC_KEY_ID
]);
1159 flower_print_port(f
, "enc_dst_port",
1160 tb
[TCA_FLOWER_KEY_ENC_UDP_DST_PORT
]);
1162 flower_print_matching_flags(f
, "ip_flags",
1164 tb
[TCA_FLOWER_KEY_FLAGS
],
1165 tb
[TCA_FLOWER_KEY_FLAGS_MASK
]);
1167 if (tb
[TCA_FLOWER_FLAGS
]) {
1168 __u32 flags
= rta_getattr_u32(tb
[TCA_FLOWER_FLAGS
]);
1170 if (flags
& TCA_CLS_FLAGS_SKIP_HW
)
1171 fprintf(f
, "\n skip_hw");
1172 if (flags
& TCA_CLS_FLAGS_SKIP_SW
)
1173 fprintf(f
, "\n skip_sw");
1175 if (flags
& TCA_CLS_FLAGS_IN_HW
)
1176 fprintf(f
, "\n in_hw");
1177 else if (flags
& TCA_CLS_FLAGS_NOT_IN_HW
)
1178 fprintf(f
, "\n not_in_hw");
1181 if (tb
[TCA_FLOWER_ACT
])
1182 tc_print_action(f
, tb
[TCA_FLOWER_ACT
]);
1187 struct filter_util flower_filter_util
= {
1189 .parse_fopt
= flower_parse_opt
,
1190 .print_fopt
= flower_print_opt
,