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>
17 #include <linux/limits.h>
18 #include <linux/if_arp.h>
19 #include <linux/if_ether.h>
21 #include <linux/tc_act/tc_vlan.h>
22 #include <linux/mpls.h>
28 enum flower_matching_flags
{
32 enum flower_endpoint
{
37 enum flower_icmp_field
{
38 FLOWER_ICMP_FIELD_TYPE
,
39 FLOWER_ICMP_FIELD_CODE
42 static void explain(void)
45 "Usage: ... flower [ MATCH-LIST ] [ verbose ]\n"
46 " [ skip_sw | skip_hw ]\n"
47 " [ action ACTION-SPEC ] [ classid CLASSID ]\n"
49 "Where: MATCH-LIST := [ MATCH-LIST ] MATCH\n"
50 " MATCH := { indev DEV-NAME |\n"
52 " vlan_prio PRIORITY |\n"
53 " vlan_ethtype [ ipv4 | ipv6 | ETH-TYPE ] |\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"
62 " mpls_label LABEL |\n"
68 " dst_port PORT-NUMBER |\n"
69 " src_port PORT-NUMBER |\n"
70 " tcp_flags MASKED-TCP_FLAGS |\n"
71 " type MASKED-ICMP-TYPE |\n"
72 " code MASKED-ICMP-CODE |\n"
73 " arp_tip IPV4-PREFIX |\n"
74 " arp_sip IPV4-PREFIX |\n"
75 " arp_op [ request | reply | OP ] |\n"
76 " arp_tha MASKED-LLADDR |\n"
77 " arp_sha MASKED-LLADDR |\n"
78 " enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
79 " enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
80 " enc_key_id [ KEY-ID ] |\n"
81 " enc_tos MASKED-IP_TOS |\n"
82 " enc_ttl MASKED-IP_TTL |\n"
83 " geneve_opts MASKED-OPTIONS |\n"
84 " ip_flags IP-FLAGS | \n"
85 " enc_dst_port [ port_number ] }\n"
86 " FILTERID := X:Y:Z\n"
87 " MASKED_LLADDR := { LLADDR | LLADDR/MASK | LLADDR/BITS }\n"
88 " ACTION-SPEC := ... look at individual actions\n"
90 "NOTE: CLASSID, IP-PROTO are parsed as hexadecimal input.\n"
91 "NOTE: There can be only used one mask per one prio. If user needs\n"
92 " to specify different mask, he has to use different prio.\n");
95 static int flower_parse_eth_addr(char *str
, int addr_type
, int mask_type
,
99 char addr
[ETH_ALEN
], *slash
;
101 slash
= strchr(str
, '/');
105 ret
= ll_addr_a2n(addr
, sizeof(addr
), str
);
108 addattr_l(n
, MAX_MSG
, addr_type
, addr
, sizeof(addr
));
113 if (!get_unsigned(&bits
, slash
+ 1, 10)) {
116 /* Extra 16 bit shift to push mac address into
117 * high bits of uint64_t
119 mask
= htonll(0xffffffffffffULL
<< (16 + 48 - bits
));
120 memcpy(addr
, &mask
, ETH_ALEN
);
122 ret
= ll_addr_a2n(addr
, sizeof(addr
), slash
+ 1);
127 memset(addr
, 0xff, ETH_ALEN
);
129 addattr_l(n
, MAX_MSG
, mask_type
, addr
, sizeof(addr
));
138 static bool eth_type_vlan(__be16 ethertype
)
140 return ethertype
== htons(ETH_P_8021Q
) ||
141 ethertype
== htons(ETH_P_8021AD
);
144 static int flower_parse_vlan_eth_type(char *str
, __be16 eth_type
, int type
,
145 __be16
*p_vlan_eth_type
,
148 __be16 vlan_eth_type
;
150 if (!eth_type_vlan(eth_type
)) {
151 fprintf(stderr
, "Can't set \"%s\" if ethertype isn't 802.1Q or 802.1AD\n",
152 type
== TCA_FLOWER_KEY_VLAN_ETH_TYPE
? "vlan_ethtype" : "cvlan_ethtype");
156 if (ll_proto_a2n(&vlan_eth_type
, str
))
157 invarg("invalid vlan_ethtype", str
);
158 addattr16(n
, MAX_MSG
, type
, vlan_eth_type
);
159 *p_vlan_eth_type
= vlan_eth_type
;
163 struct flag_to_string
{
165 enum flower_matching_flags type
;
169 static struct flag_to_string flags_str
[] = {
170 { TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT
, FLOWER_IP_FLAGS
, "frag" },
171 { TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST
, FLOWER_IP_FLAGS
, "firstfrag" },
174 static int flower_parse_matching_flags(char *str
,
175 enum flower_matching_flags type
,
176 __u32
*mtf
, __u32
*mtf_mask
)
183 token
= strtok(str
, "/");
186 if (!strncmp(token
, "no", 2)) {
193 for (i
= 0; i
< ARRAY_SIZE(flags_str
); i
++) {
194 if (type
!= flags_str
[i
].type
)
197 if (!strcmp(token
, flags_str
[i
].string
)) {
199 *mtf
&= ~flags_str
[i
].flag
;
201 *mtf
|= flags_str
[i
].flag
;
203 *mtf_mask
|= flags_str
[i
].flag
;
211 token
= strtok(NULL
, "/");
217 static int flower_parse_ip_proto(char *str
, __be16 eth_type
, int type
,
218 __u8
*p_ip_proto
, struct nlmsghdr
*n
)
223 if (eth_type
!= htons(ETH_P_IP
) && eth_type
!= htons(ETH_P_IPV6
))
226 if (matches(str
, "tcp") == 0) {
227 ip_proto
= IPPROTO_TCP
;
228 } else if (matches(str
, "udp") == 0) {
229 ip_proto
= IPPROTO_UDP
;
230 } else if (matches(str
, "sctp") == 0) {
231 ip_proto
= IPPROTO_SCTP
;
232 } else if (matches(str
, "icmp") == 0) {
233 if (eth_type
!= htons(ETH_P_IP
))
235 ip_proto
= IPPROTO_ICMP
;
236 } else if (matches(str
, "icmpv6") == 0) {
237 if (eth_type
!= htons(ETH_P_IPV6
))
239 ip_proto
= IPPROTO_ICMPV6
;
241 ret
= get_u8(&ip_proto
, str
, 16);
245 addattr8(n
, MAX_MSG
, type
, ip_proto
);
246 *p_ip_proto
= ip_proto
;
250 fprintf(stderr
, "Illegal \"eth_type\" for ip proto\n");
254 static int __flower_parse_ip_addr(char *str
, int family
,
255 int addr4_type
, int mask4_type
,
256 int addr6_type
, int mask6_type
,
264 ret
= get_prefix(&addr
, str
, family
);
268 if (family
&& (addr
.family
!= family
)) {
269 fprintf(stderr
, "Illegal \"eth_type\" for ip address\n");
273 addattr_l(n
, MAX_MSG
, addr
.family
== AF_INET
? addr4_type
: addr6_type
,
274 addr
.data
, addr
.bytelen
);
276 memset(addr
.data
, 0xff, addr
.bytelen
);
278 for (i
= 0; i
< addr
.bytelen
/ 4; i
++) {
281 } else if (bits
/ 32 >= 1) {
284 addr
.data
[i
] <<= 32 - bits
;
285 addr
.data
[i
] = htonl(addr
.data
[i
]);
290 addattr_l(n
, MAX_MSG
, addr
.family
== AF_INET
? mask4_type
: mask6_type
,
291 addr
.data
, addr
.bytelen
);
296 static int flower_parse_ip_addr(char *str
, __be16 eth_type
,
297 int addr4_type
, int mask4_type
,
298 int addr6_type
, int mask6_type
,
303 if (eth_type
== htons(ETH_P_IP
)) {
305 } else if (eth_type
== htons(ETH_P_IPV6
)) {
307 } else if (!eth_type
) {
313 return __flower_parse_ip_addr(str
, family
, addr4_type
, mask4_type
,
314 addr6_type
, mask6_type
, n
);
317 static bool flower_eth_type_arp(__be16 eth_type
)
319 return eth_type
== htons(ETH_P_ARP
) || eth_type
== htons(ETH_P_RARP
);
322 static int flower_parse_arp_ip_addr(char *str
, __be16 eth_type
,
323 int addr_type
, int mask_type
,
326 if (!flower_eth_type_arp(eth_type
))
329 return __flower_parse_ip_addr(str
, AF_INET
, addr_type
, mask_type
,
330 TCA_FLOWER_UNSPEC
, TCA_FLOWER_UNSPEC
, n
);
333 static int flower_parse_u8(char *str
, int value_type
, int mask_type
,
334 int (*value_from_name
)(const char *str
,
336 bool (*value_validate
)(__u8 value
),
343 slash
= strchr(str
, '/');
347 ret
= value_from_name
? value_from_name(str
, &value
) : -1;
349 ret
= get_u8(&value
, str
, 10);
354 if (value_validate
&& !value_validate(value
))
358 ret
= get_u8(&mask
, slash
+ 1, 10);
366 addattr8(n
, MAX_MSG
, value_type
, value
);
367 addattr8(n
, MAX_MSG
, mask_type
, mask
);
376 static const char *flower_print_arp_op_to_name(__u8 op
)
388 static int flower_arp_op_from_name(const char *name
, __u8
*op
)
390 if (!strcmp(name
, "request"))
392 else if (!strcmp(name
, "reply"))
400 static bool flow_arp_op_validate(__u8 op
)
402 return !op
|| op
== ARPOP_REQUEST
|| op
== ARPOP_REPLY
;
405 static int flower_parse_arp_op(char *str
, __be16 eth_type
,
406 int op_type
, int mask_type
,
409 if (!flower_eth_type_arp(eth_type
))
412 return flower_parse_u8(str
, op_type
, mask_type
, flower_arp_op_from_name
,
413 flow_arp_op_validate
, n
);
416 static int flower_icmp_attr_type(__be16 eth_type
, __u8 ip_proto
,
417 enum flower_icmp_field field
)
419 if (eth_type
== htons(ETH_P_IP
) && ip_proto
== IPPROTO_ICMP
)
420 return field
== FLOWER_ICMP_FIELD_CODE
?
421 TCA_FLOWER_KEY_ICMPV4_CODE
:
422 TCA_FLOWER_KEY_ICMPV4_TYPE
;
423 else if (eth_type
== htons(ETH_P_IPV6
) && ip_proto
== IPPROTO_ICMPV6
)
424 return field
== FLOWER_ICMP_FIELD_CODE
?
425 TCA_FLOWER_KEY_ICMPV6_CODE
:
426 TCA_FLOWER_KEY_ICMPV6_TYPE
;
431 static int flower_icmp_attr_mask_type(__be16 eth_type
, __u8 ip_proto
,
432 enum flower_icmp_field field
)
434 if (eth_type
== htons(ETH_P_IP
) && ip_proto
== IPPROTO_ICMP
)
435 return field
== FLOWER_ICMP_FIELD_CODE
?
436 TCA_FLOWER_KEY_ICMPV4_CODE_MASK
:
437 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK
;
438 else if (eth_type
== htons(ETH_P_IPV6
) && ip_proto
== IPPROTO_ICMPV6
)
439 return field
== FLOWER_ICMP_FIELD_CODE
?
440 TCA_FLOWER_KEY_ICMPV6_CODE_MASK
:
441 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK
;
446 static int flower_parse_icmp(char *str
, __u16 eth_type
, __u8 ip_proto
,
447 enum flower_icmp_field field
, struct nlmsghdr
*n
)
449 int value_type
, mask_type
;
451 value_type
= flower_icmp_attr_type(eth_type
, ip_proto
, field
);
452 mask_type
= flower_icmp_attr_mask_type(eth_type
, ip_proto
, field
);
453 if (value_type
< 0 || mask_type
< 0)
456 return flower_parse_u8(str
, value_type
, mask_type
, NULL
, NULL
, n
);
459 static int flower_port_attr_type(__u8 ip_proto
, enum flower_endpoint endpoint
)
461 if (ip_proto
== IPPROTO_TCP
)
462 return endpoint
== FLOWER_ENDPOINT_SRC
?
463 TCA_FLOWER_KEY_TCP_SRC
:
464 TCA_FLOWER_KEY_TCP_DST
;
465 else if (ip_proto
== IPPROTO_UDP
)
466 return endpoint
== FLOWER_ENDPOINT_SRC
?
467 TCA_FLOWER_KEY_UDP_SRC
:
468 TCA_FLOWER_KEY_UDP_DST
;
469 else if (ip_proto
== IPPROTO_SCTP
)
470 return endpoint
== FLOWER_ENDPOINT_SRC
?
471 TCA_FLOWER_KEY_SCTP_SRC
:
472 TCA_FLOWER_KEY_SCTP_DST
;
477 static int flower_port_range_attr_type(__u8 ip_proto
, enum flower_endpoint type
,
478 __be16
*min_port_type
,
479 __be16
*max_port_type
)
481 if (ip_proto
== IPPROTO_TCP
|| ip_proto
== IPPROTO_UDP
||
482 ip_proto
== IPPROTO_SCTP
) {
483 if (type
== FLOWER_ENDPOINT_SRC
) {
484 *min_port_type
= TCA_FLOWER_KEY_PORT_SRC_MIN
;
485 *max_port_type
= TCA_FLOWER_KEY_PORT_SRC_MAX
;
487 *min_port_type
= TCA_FLOWER_KEY_PORT_DST_MIN
;
488 *max_port_type
= TCA_FLOWER_KEY_PORT_DST_MAX
;
496 static int flower_parse_port(char *str
, __u8 ip_proto
,
497 enum flower_endpoint endpoint
,
503 ret
= sscanf(str
, "%hu-%hu", &min
, &max
);
508 type
= flower_port_attr_type(ip_proto
, endpoint
);
511 addattr16(n
, MAX_MSG
, type
, htons(min
));
512 } else if (ret
== 2) {
513 __be16 min_port_type
, max_port_type
;
516 fprintf(stderr
, "max value should be greater than min value\n");
519 if (flower_port_range_attr_type(ip_proto
, endpoint
,
520 &min_port_type
, &max_port_type
))
523 addattr16(n
, MAX_MSG
, min_port_type
, htons(min
));
524 addattr16(n
, MAX_MSG
, max_port_type
, htons(max
));
531 #define TCP_FLAGS_MAX_MASK 0xfff
533 static int flower_parse_tcp_flags(char *str
, int flags_type
, int mask_type
,
540 slash
= strchr(str
, '/');
544 ret
= get_u16(&flags
, str
, 16);
545 if (ret
< 0 || flags
& ~TCP_FLAGS_MAX_MASK
)
548 addattr16(n
, MAX_MSG
, flags_type
, htons(flags
));
551 ret
= get_u16(&flags
, slash
+ 1, 16);
552 if (ret
< 0 || flags
& ~TCP_FLAGS_MAX_MASK
)
555 flags
= TCP_FLAGS_MAX_MASK
;
557 addattr16(n
, MAX_MSG
, mask_type
, htons(flags
));
566 static int flower_parse_ip_tos_ttl(char *str
, int key_type
, int mask_type
,
573 slash
= strchr(str
, '/');
577 ret
= get_u8(&tos_ttl
, str
, 10);
579 ret
= get_u8(&tos_ttl
, str
, 16);
583 addattr8(n
, MAX_MSG
, key_type
, tos_ttl
);
586 ret
= get_u8(&tos_ttl
, slash
+ 1, 16);
592 addattr8(n
, MAX_MSG
, mask_type
, tos_ttl
);
601 static int flower_parse_key_id(const char *str
, int type
, struct nlmsghdr
*n
)
606 ret
= get_be32(&key_id
, str
, 10);
608 addattr32(n
, MAX_MSG
, type
, key_id
);
613 static int flower_parse_enc_port(char *str
, int type
, struct nlmsghdr
*n
)
618 ret
= get_be16(&port
, str
, 10);
622 addattr16(n
, MAX_MSG
, type
, port
);
627 static int flower_parse_geneve_opts(char *str
, struct nlmsghdr
*n
)
633 nest
= addattr_nest(n
, MAX_MSG
, TCA_FLOWER_KEY_ENC_OPTS_GENEVE
);
636 token
= strsep(&str
, ":");
639 case TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS
:
645 err
= get_be16(&opt_class
, token
, 16);
649 addattr16(n
, MAX_MSG
, i
, opt_class
);
652 case TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE
:
658 err
= get_u8(&opt_type
, token
, 16);
662 addattr8(n
, MAX_MSG
, i
, opt_type
);
665 case TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA
:
667 size_t token_len
= strlen(token
);
672 opts
= malloc(token_len
/ 2);
675 if (hex2mem(token
, opts
, token_len
/ 2) < 0) {
679 addattr_l(n
, MAX_MSG
, i
, opts
, token_len
/ 2);
685 fprintf(stderr
, "Unknown \"geneve_opts\" type\n");
689 token
= strsep(&str
, ":");
692 addattr_nest_end(n
, nest
);
697 static int flower_parse_enc_opt_part(char *str
, struct nlmsghdr
*n
)
702 token
= strsep(&str
, ",");
704 err
= flower_parse_geneve_opts(token
, n
);
708 token
= strsep(&str
, ",");
714 static int flower_check_enc_opt_key(char *key
)
716 int key_len
, col_cnt
= 0;
718 key_len
= strlen(key
);
719 while ((key
= strchr(key
, ':'))) {
720 if (strlen(key
) == key_len
)
723 key_len
= strlen(key
) - 1;
728 if (col_cnt
!= 2 || !key_len
)
734 static int flower_parse_enc_opts(char *str
, struct nlmsghdr
*n
)
736 char key
[XATTR_SIZE_MAX
], mask
[XATTR_SIZE_MAX
];
737 int data_len
, key_len
, mask_len
, err
;
743 token
= strsep(&str
, ",");
745 slash
= strchr(token
, '/');
749 if ((key_len
+ strlen(token
) > XATTR_SIZE_MAX
) ||
750 flower_check_enc_opt_key(token
))
753 strcpy(&key
[key_len
], token
);
754 key_len
+= strlen(token
) + 1;
755 key
[key_len
- 1] = ',';
758 /* Pad out mask when not provided */
759 if (mask_len
+ strlen(token
) > XATTR_SIZE_MAX
)
762 data_len
= strlen(rindex(token
, ':'));
763 sprintf(&mask
[mask_len
], "ffff:ff:");
765 memset(&mask
[mask_len
], 'f', data_len
- 1);
766 mask_len
+= data_len
;
767 mask
[mask_len
- 1] = ',';
768 token
= strsep(&str
, ",");
772 if (mask_len
+ strlen(slash
+ 1) > XATTR_SIZE_MAX
)
775 strcpy(&mask
[mask_len
], slash
+ 1);
776 mask_len
+= strlen(slash
+ 1) + 1;
777 mask
[mask_len
- 1] = ',';
780 token
= strsep(&str
, ",");
782 key
[key_len
- 1] = '\0';
783 mask
[mask_len
- 1] = '\0';
785 nest
= addattr_nest(n
, MAX_MSG
, TCA_FLOWER_KEY_ENC_OPTS
);
786 err
= flower_parse_enc_opt_part(key
, n
);
789 addattr_nest_end(n
, nest
);
791 nest
= addattr_nest(n
, MAX_MSG
, TCA_FLOWER_KEY_ENC_OPTS_MASK
);
792 err
= flower_parse_enc_opt_part(mask
, n
);
795 addattr_nest_end(n
, nest
);
800 static int flower_parse_opt(struct filter_util
*qu
, char *handle
,
801 int argc
, char **argv
, struct nlmsghdr
*n
)
804 struct tcmsg
*t
= NLMSG_DATA(n
);
806 __be16 eth_type
= TC_H_MIN(t
->tcm_info
);
807 __be16 vlan_ethtype
= 0;
808 __be16 cvlan_ethtype
= 0;
809 __u8 ip_proto
= 0xff;
815 ret
= get_u32(&t
->tcm_handle
, handle
, 0);
817 fprintf(stderr
, "Illegal \"handle\"\n");
822 tail
= (struct rtattr
*) (((void *) n
) + NLMSG_ALIGN(n
->nlmsg_len
));
823 addattr_l(n
, MAX_MSG
, TCA_OPTIONS
, NULL
, 0);
826 /*at minimal we will match all ethertype packets */
831 if (matches(*argv
, "classid") == 0 ||
832 matches(*argv
, "flowid") == 0) {
836 ret
= get_tc_classid(&handle
, *argv
);
838 fprintf(stderr
, "Illegal \"classid\"\n");
841 addattr_l(n
, MAX_MSG
, TCA_FLOWER_CLASSID
, &handle
, 4);
842 } else if (matches(*argv
, "hw_tc") == 0) {
848 tc
= strtoul(*argv
, &end
, 0);
850 fprintf(stderr
, "Illegal TC index\n");
853 if (tc
>= TC_QOPT_MAX_QUEUE
) {
854 fprintf(stderr
, "TC index exceeds max range\n");
857 handle
= TC_H_MAKE(TC_H_MAJ(t
->tcm_parent
),
858 TC_H_MIN(tc
+ TC_H_MIN_PRIORITY
));
859 addattr_l(n
, MAX_MSG
, TCA_FLOWER_CLASSID
, &handle
,
861 } else if (matches(*argv
, "ip_flags") == 0) {
863 ret
= flower_parse_matching_flags(*argv
,
868 fprintf(stderr
, "Illegal \"ip_flags\"\n");
871 } else if (matches(*argv
, "verbose") == 0) {
872 flags
|= TCA_CLS_FLAGS_VERBOSE
;
873 } else if (matches(*argv
, "skip_hw") == 0) {
874 flags
|= TCA_CLS_FLAGS_SKIP_HW
;
875 } else if (matches(*argv
, "skip_sw") == 0) {
876 flags
|= TCA_CLS_FLAGS_SKIP_SW
;
877 } else if (matches(*argv
, "indev") == 0) {
879 if (check_ifname(*argv
))
880 invarg("\"indev\" not a valid ifname", *argv
);
881 addattrstrz(n
, MAX_MSG
, TCA_FLOWER_INDEV
, *argv
);
882 } else if (matches(*argv
, "vlan_id") == 0) {
886 if (!eth_type_vlan(eth_type
)) {
887 fprintf(stderr
, "Can't set \"vlan_id\" if ethertype isn't 802.1Q or 802.1AD\n");
890 ret
= get_u16(&vid
, *argv
, 10);
891 if (ret
< 0 || vid
& ~0xfff) {
892 fprintf(stderr
, "Illegal \"vlan_id\"\n");
895 addattr16(n
, MAX_MSG
, TCA_FLOWER_KEY_VLAN_ID
, vid
);
896 } else if (matches(*argv
, "vlan_prio") == 0) {
900 if (!eth_type_vlan(eth_type
)) {
901 fprintf(stderr
, "Can't set \"vlan_prio\" if ethertype isn't 802.1Q or 802.1AD\n");
904 ret
= get_u8(&vlan_prio
, *argv
, 10);
905 if (ret
< 0 || vlan_prio
& ~0x7) {
906 fprintf(stderr
, "Illegal \"vlan_prio\"\n");
910 TCA_FLOWER_KEY_VLAN_PRIO
, vlan_prio
);
911 } else if (matches(*argv
, "vlan_ethtype") == 0) {
913 ret
= flower_parse_vlan_eth_type(*argv
, eth_type
,
914 TCA_FLOWER_KEY_VLAN_ETH_TYPE
,
918 } else if (matches(*argv
, "cvlan_id") == 0) {
922 if (!eth_type_vlan(vlan_ethtype
)) {
923 fprintf(stderr
, "Can't set \"cvlan_id\" if inner vlan ethertype isn't 802.1Q or 802.1AD\n");
926 ret
= get_u16(&vid
, *argv
, 10);
927 if (ret
< 0 || vid
& ~0xfff) {
928 fprintf(stderr
, "Illegal \"cvlan_id\"\n");
931 addattr16(n
, MAX_MSG
, TCA_FLOWER_KEY_CVLAN_ID
, vid
);
932 } else if (matches(*argv
, "cvlan_prio") == 0) {
936 if (!eth_type_vlan(vlan_ethtype
)) {
937 fprintf(stderr
, "Can't set \"cvlan_prio\" if inner vlan ethertype isn't 802.1Q or 802.1AD\n");
940 ret
= get_u8(&cvlan_prio
, *argv
, 10);
941 if (ret
< 0 || cvlan_prio
& ~0x7) {
942 fprintf(stderr
, "Illegal \"cvlan_prio\"\n");
946 TCA_FLOWER_KEY_CVLAN_PRIO
, cvlan_prio
);
947 } else if (matches(*argv
, "cvlan_ethtype") == 0) {
949 ret
= flower_parse_vlan_eth_type(*argv
, vlan_ethtype
,
950 TCA_FLOWER_KEY_CVLAN_ETH_TYPE
,
954 } else if (matches(*argv
, "mpls_label") == 0) {
958 if (eth_type
!= htons(ETH_P_MPLS_UC
) &&
959 eth_type
!= htons(ETH_P_MPLS_MC
)) {
961 "Can't set \"mpls_label\" if ethertype isn't MPLS\n");
964 ret
= get_u32(&label
, *argv
, 10);
965 if (ret
< 0 || label
& ~(MPLS_LS_LABEL_MASK
>> MPLS_LS_LABEL_SHIFT
)) {
966 fprintf(stderr
, "Illegal \"mpls_label\"\n");
969 addattr32(n
, MAX_MSG
, TCA_FLOWER_KEY_MPLS_LABEL
, label
);
970 } else if (matches(*argv
, "mpls_tc") == 0) {
974 if (eth_type
!= htons(ETH_P_MPLS_UC
) &&
975 eth_type
!= htons(ETH_P_MPLS_MC
)) {
977 "Can't set \"mpls_tc\" if ethertype isn't MPLS\n");
980 ret
= get_u8(&tc
, *argv
, 10);
981 if (ret
< 0 || tc
& ~(MPLS_LS_TC_MASK
>> MPLS_LS_TC_SHIFT
)) {
982 fprintf(stderr
, "Illegal \"mpls_tc\"\n");
985 addattr8(n
, MAX_MSG
, TCA_FLOWER_KEY_MPLS_TC
, tc
);
986 } else if (matches(*argv
, "mpls_bos") == 0) {
990 if (eth_type
!= htons(ETH_P_MPLS_UC
) &&
991 eth_type
!= htons(ETH_P_MPLS_MC
)) {
993 "Can't set \"mpls_bos\" if ethertype isn't MPLS\n");
996 ret
= get_u8(&bos
, *argv
, 10);
997 if (ret
< 0 || bos
& ~(MPLS_LS_S_MASK
>> MPLS_LS_S_SHIFT
)) {
998 fprintf(stderr
, "Illegal \"mpls_bos\"\n");
1001 addattr8(n
, MAX_MSG
, TCA_FLOWER_KEY_MPLS_BOS
, bos
);
1002 } else if (matches(*argv
, "mpls_ttl") == 0) {
1006 if (eth_type
!= htons(ETH_P_MPLS_UC
) &&
1007 eth_type
!= htons(ETH_P_MPLS_MC
)) {
1009 "Can't set \"mpls_ttl\" if ethertype isn't MPLS\n");
1012 ret
= get_u8(&ttl
, *argv
, 10);
1013 if (ret
< 0 || ttl
& ~(MPLS_LS_TTL_MASK
>> MPLS_LS_TTL_SHIFT
)) {
1014 fprintf(stderr
, "Illegal \"mpls_ttl\"\n");
1017 addattr8(n
, MAX_MSG
, TCA_FLOWER_KEY_MPLS_TTL
, ttl
);
1018 } else if (matches(*argv
, "dst_mac") == 0) {
1020 ret
= flower_parse_eth_addr(*argv
,
1021 TCA_FLOWER_KEY_ETH_DST
,
1022 TCA_FLOWER_KEY_ETH_DST_MASK
,
1025 fprintf(stderr
, "Illegal \"dst_mac\"\n");
1028 } else if (matches(*argv
, "src_mac") == 0) {
1030 ret
= flower_parse_eth_addr(*argv
,
1031 TCA_FLOWER_KEY_ETH_SRC
,
1032 TCA_FLOWER_KEY_ETH_SRC_MASK
,
1035 fprintf(stderr
, "Illegal \"src_mac\"\n");
1038 } else if (matches(*argv
, "ip_proto") == 0) {
1040 ret
= flower_parse_ip_proto(*argv
, cvlan_ethtype
?
1041 cvlan_ethtype
: vlan_ethtype
?
1042 vlan_ethtype
: eth_type
,
1043 TCA_FLOWER_KEY_IP_PROTO
,
1046 fprintf(stderr
, "Illegal \"ip_proto\"\n");
1049 } else if (matches(*argv
, "ip_tos") == 0) {
1051 ret
= flower_parse_ip_tos_ttl(*argv
,
1052 TCA_FLOWER_KEY_IP_TOS
,
1053 TCA_FLOWER_KEY_IP_TOS_MASK
,
1056 fprintf(stderr
, "Illegal \"ip_tos\"\n");
1059 } else if (matches(*argv
, "ip_ttl") == 0) {
1061 ret
= flower_parse_ip_tos_ttl(*argv
,
1062 TCA_FLOWER_KEY_IP_TTL
,
1063 TCA_FLOWER_KEY_IP_TTL_MASK
,
1066 fprintf(stderr
, "Illegal \"ip_ttl\"\n");
1069 } else if (matches(*argv
, "dst_ip") == 0) {
1071 ret
= flower_parse_ip_addr(*argv
, cvlan_ethtype
?
1072 cvlan_ethtype
: vlan_ethtype
?
1073 vlan_ethtype
: eth_type
,
1074 TCA_FLOWER_KEY_IPV4_DST
,
1075 TCA_FLOWER_KEY_IPV4_DST_MASK
,
1076 TCA_FLOWER_KEY_IPV6_DST
,
1077 TCA_FLOWER_KEY_IPV6_DST_MASK
,
1080 fprintf(stderr
, "Illegal \"dst_ip\"\n");
1083 } else if (matches(*argv
, "src_ip") == 0) {
1085 ret
= flower_parse_ip_addr(*argv
, cvlan_ethtype
?
1086 cvlan_ethtype
: vlan_ethtype
?
1087 vlan_ethtype
: eth_type
,
1088 TCA_FLOWER_KEY_IPV4_SRC
,
1089 TCA_FLOWER_KEY_IPV4_SRC_MASK
,
1090 TCA_FLOWER_KEY_IPV6_SRC
,
1091 TCA_FLOWER_KEY_IPV6_SRC_MASK
,
1094 fprintf(stderr
, "Illegal \"src_ip\"\n");
1097 } else if (matches(*argv
, "dst_port") == 0) {
1099 ret
= flower_parse_port(*argv
, ip_proto
,
1100 FLOWER_ENDPOINT_DST
, n
);
1102 fprintf(stderr
, "Illegal \"dst_port\"\n");
1105 } else if (matches(*argv
, "src_port") == 0) {
1107 ret
= flower_parse_port(*argv
, ip_proto
,
1108 FLOWER_ENDPOINT_SRC
, n
);
1110 fprintf(stderr
, "Illegal \"src_port\"\n");
1113 } else if (matches(*argv
, "tcp_flags") == 0) {
1115 ret
= flower_parse_tcp_flags(*argv
,
1116 TCA_FLOWER_KEY_TCP_FLAGS
,
1117 TCA_FLOWER_KEY_TCP_FLAGS_MASK
,
1120 fprintf(stderr
, "Illegal \"tcp_flags\"\n");
1123 } else if (matches(*argv
, "type") == 0) {
1125 ret
= flower_parse_icmp(*argv
, eth_type
, ip_proto
,
1126 FLOWER_ICMP_FIELD_TYPE
, n
);
1128 fprintf(stderr
, "Illegal \"icmp type\"\n");
1131 } else if (matches(*argv
, "code") == 0) {
1133 ret
= flower_parse_icmp(*argv
, eth_type
, ip_proto
,
1134 FLOWER_ICMP_FIELD_CODE
, n
);
1136 fprintf(stderr
, "Illegal \"icmp code\"\n");
1139 } else if (matches(*argv
, "arp_tip") == 0) {
1141 ret
= flower_parse_arp_ip_addr(*argv
, vlan_ethtype
?
1142 vlan_ethtype
: eth_type
,
1143 TCA_FLOWER_KEY_ARP_TIP
,
1144 TCA_FLOWER_KEY_ARP_TIP_MASK
,
1147 fprintf(stderr
, "Illegal \"arp_tip\"\n");
1150 } else if (matches(*argv
, "arp_sip") == 0) {
1152 ret
= flower_parse_arp_ip_addr(*argv
, vlan_ethtype
?
1153 vlan_ethtype
: eth_type
,
1154 TCA_FLOWER_KEY_ARP_SIP
,
1155 TCA_FLOWER_KEY_ARP_SIP_MASK
,
1158 fprintf(stderr
, "Illegal \"arp_sip\"\n");
1161 } else if (matches(*argv
, "arp_op") == 0) {
1163 ret
= flower_parse_arp_op(*argv
, vlan_ethtype
?
1164 vlan_ethtype
: eth_type
,
1165 TCA_FLOWER_KEY_ARP_OP
,
1166 TCA_FLOWER_KEY_ARP_OP_MASK
,
1169 fprintf(stderr
, "Illegal \"arp_op\"\n");
1172 } else if (matches(*argv
, "arp_tha") == 0) {
1174 ret
= flower_parse_eth_addr(*argv
,
1175 TCA_FLOWER_KEY_ARP_THA
,
1176 TCA_FLOWER_KEY_ARP_THA_MASK
,
1179 fprintf(stderr
, "Illegal \"arp_tha\"\n");
1182 } else if (matches(*argv
, "arp_sha") == 0) {
1184 ret
= flower_parse_eth_addr(*argv
,
1185 TCA_FLOWER_KEY_ARP_SHA
,
1186 TCA_FLOWER_KEY_ARP_SHA_MASK
,
1189 fprintf(stderr
, "Illegal \"arp_sha\"\n");
1192 } else if (matches(*argv
, "enc_dst_ip") == 0) {
1194 ret
= flower_parse_ip_addr(*argv
, 0,
1195 TCA_FLOWER_KEY_ENC_IPV4_DST
,
1196 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
,
1197 TCA_FLOWER_KEY_ENC_IPV6_DST
,
1198 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK
,
1201 fprintf(stderr
, "Illegal \"enc_dst_ip\"\n");
1204 } else if (matches(*argv
, "enc_src_ip") == 0) {
1206 ret
= flower_parse_ip_addr(*argv
, 0,
1207 TCA_FLOWER_KEY_ENC_IPV4_SRC
,
1208 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
,
1209 TCA_FLOWER_KEY_ENC_IPV6_SRC
,
1210 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK
,
1213 fprintf(stderr
, "Illegal \"enc_src_ip\"\n");
1216 } else if (matches(*argv
, "enc_key_id") == 0) {
1218 ret
= flower_parse_key_id(*argv
,
1219 TCA_FLOWER_KEY_ENC_KEY_ID
, n
);
1221 fprintf(stderr
, "Illegal \"enc_key_id\"\n");
1224 } else if (matches(*argv
, "enc_dst_port") == 0) {
1226 ret
= flower_parse_enc_port(*argv
,
1227 TCA_FLOWER_KEY_ENC_UDP_DST_PORT
, n
);
1229 fprintf(stderr
, "Illegal \"enc_dst_port\"\n");
1232 } else if (matches(*argv
, "enc_tos") == 0) {
1234 ret
= flower_parse_ip_tos_ttl(*argv
,
1235 TCA_FLOWER_KEY_ENC_IP_TOS
,
1236 TCA_FLOWER_KEY_ENC_IP_TOS_MASK
,
1239 fprintf(stderr
, "Illegal \"enc_tos\"\n");
1242 } else if (matches(*argv
, "enc_ttl") == 0) {
1244 ret
= flower_parse_ip_tos_ttl(*argv
,
1245 TCA_FLOWER_KEY_ENC_IP_TTL
,
1246 TCA_FLOWER_KEY_ENC_IP_TTL_MASK
,
1249 fprintf(stderr
, "Illegal \"enc_ttl\"\n");
1252 } else if (matches(*argv
, "geneve_opts") == 0) {
1254 ret
= flower_parse_enc_opts(*argv
, n
);
1256 fprintf(stderr
, "Illegal \"geneve_opts\"\n");
1259 } else if (matches(*argv
, "action") == 0) {
1261 ret
= parse_action(&argc
, &argv
, TCA_FLOWER_ACT
, n
);
1263 fprintf(stderr
, "Illegal \"action\"\n");
1267 } else if (strcmp(*argv
, "help") == 0) {
1271 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
1279 ret
= addattr32(n
, MAX_MSG
, TCA_FLOWER_FLAGS
, flags
);
1284 ret
= addattr32(n
, MAX_MSG
, TCA_FLOWER_KEY_FLAGS
, htonl(mtf
));
1288 ret
= addattr32(n
, MAX_MSG
, TCA_FLOWER_KEY_FLAGS_MASK
, htonl(mtf_mask
));
1293 if (eth_type
!= htons(ETH_P_ALL
)) {
1294 ret
= addattr16(n
, MAX_MSG
, TCA_FLOWER_KEY_ETH_TYPE
, eth_type
);
1299 tail
->rta_len
= (((void *)n
)+n
->nlmsg_len
) - (void *)tail
;
1304 static int __mask_bits(char *addr
, size_t len
)
1311 for (i
= 0; i
< len
; i
++, addr
++) {
1312 for (j
= 7; j
>= 0; j
--) {
1313 if (((*addr
) >> j
) & 0x1) {
1327 static void flower_print_eth_addr(char *name
, struct rtattr
*addr_attr
,
1328 struct rtattr
*mask_attr
)
1330 SPRINT_BUF(namefrm
);
1336 if (!addr_attr
|| RTA_PAYLOAD(addr_attr
) != ETH_ALEN
)
1338 done
= sprintf(out
, "%s",
1339 ll_addr_n2a(RTA_DATA(addr_attr
), ETH_ALEN
,
1340 0, b1
, sizeof(b1
)));
1341 if (mask_attr
&& RTA_PAYLOAD(mask_attr
) == ETH_ALEN
) {
1342 bits
= __mask_bits(RTA_DATA(mask_attr
), ETH_ALEN
);
1344 sprintf(out
+ done
, "/%s",
1345 ll_addr_n2a(RTA_DATA(mask_attr
), ETH_ALEN
,
1346 0, b1
, sizeof(b1
)));
1347 else if (bits
< ETH_ALEN
* 8)
1348 sprintf(out
+ done
, "/%d", bits
);
1351 sprintf(namefrm
, "\n %s %%s", name
);
1352 print_string(PRINT_ANY
, name
, namefrm
, out
);
1355 static void flower_print_eth_type(__be16
*p_eth_type
,
1356 struct rtattr
*eth_type_attr
)
1364 eth_type
= rta_getattr_u16(eth_type_attr
);
1365 if (eth_type
== htons(ETH_P_IP
))
1366 sprintf(out
, "ipv4");
1367 else if (eth_type
== htons(ETH_P_IPV6
))
1368 sprintf(out
, "ipv6");
1369 else if (eth_type
== htons(ETH_P_ARP
))
1370 sprintf(out
, "arp");
1371 else if (eth_type
== htons(ETH_P_RARP
))
1372 sprintf(out
, "rarp");
1374 sprintf(out
, "%04x", ntohs(eth_type
));
1376 print_string(PRINT_ANY
, "eth_type", "\n eth_type %s", out
);
1377 *p_eth_type
= eth_type
;
1380 static void flower_print_ip_proto(__u8
*p_ip_proto
,
1381 struct rtattr
*ip_proto_attr
)
1389 ip_proto
= rta_getattr_u8(ip_proto_attr
);
1390 if (ip_proto
== IPPROTO_TCP
)
1391 sprintf(out
, "tcp");
1392 else if (ip_proto
== IPPROTO_UDP
)
1393 sprintf(out
, "udp");
1394 else if (ip_proto
== IPPROTO_SCTP
)
1395 sprintf(out
, "sctp");
1396 else if (ip_proto
== IPPROTO_ICMP
)
1397 sprintf(out
, "icmp");
1398 else if (ip_proto
== IPPROTO_ICMPV6
)
1399 sprintf(out
, "icmpv6");
1401 sprintf(out
, "%02x", ip_proto
);
1403 print_string(PRINT_ANY
, "ip_proto", "\n ip_proto %s", out
);
1404 *p_ip_proto
= ip_proto
;
1407 static void flower_print_ip_attr(const char *name
, struct rtattr
*key_attr
,
1408 struct rtattr
*mask_attr
)
1410 SPRINT_BUF(namefrm
);
1417 done
= sprintf(out
, "0x%x", rta_getattr_u8(key_attr
));
1419 sprintf(out
+ done
, "/%x", rta_getattr_u8(mask_attr
));
1421 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
1422 sprintf(namefrm
, "%s %%s", name
);
1423 print_string(PRINT_ANY
, name
, namefrm
, out
);
1426 static void flower_print_matching_flags(char *name
,
1427 enum flower_matching_flags type
,
1428 struct rtattr
*attr
,
1429 struct rtattr
*mask_attr
)
1436 if (!mask_attr
|| RTA_PAYLOAD(mask_attr
) != 4)
1439 mtf
= ntohl(rta_getattr_u32(attr
));
1440 mtf_mask
= ntohl(rta_getattr_u32(mask_attr
));
1442 for (i
= 0; i
< ARRAY_SIZE(flags_str
); i
++) {
1443 if (type
!= flags_str
[i
].type
)
1445 if (mtf_mask
& flags_str
[i
].flag
) {
1447 print_string(PRINT_FP
, NULL
, "\n %s ", name
);
1448 open_json_object(name
);
1450 print_string(PRINT_FP
, NULL
, "/", NULL
);
1453 print_bool(PRINT_JSON
, flags_str
[i
].string
, NULL
,
1454 mtf
& flags_str
[i
].flag
);
1455 if (mtf
& flags_str
[i
].flag
)
1456 print_string(PRINT_FP
, NULL
, "%s",
1457 flags_str
[i
].string
);
1459 print_string(PRINT_FP
, NULL
, "no%s",
1460 flags_str
[i
].string
);
1464 close_json_object();
1467 static void flower_print_ip_addr(char *name
, __be16 eth_type
,
1468 struct rtattr
*addr4_attr
,
1469 struct rtattr
*mask4_attr
,
1470 struct rtattr
*addr6_attr
,
1471 struct rtattr
*mask6_attr
)
1473 struct rtattr
*addr_attr
;
1474 struct rtattr
*mask_attr
;
1475 SPRINT_BUF(namefrm
);
1482 if (eth_type
== htons(ETH_P_IP
)) {
1484 addr_attr
= addr4_attr
;
1485 mask_attr
= mask4_attr
;
1487 } else if (eth_type
== htons(ETH_P_IPV6
)) {
1489 addr_attr
= addr6_attr
;
1490 mask_attr
= mask6_attr
;
1495 if (!addr_attr
|| RTA_PAYLOAD(addr_attr
) != len
)
1497 if (!mask_attr
|| RTA_PAYLOAD(mask_attr
) != len
)
1499 done
= sprintf(out
, "%s", rt_addr_n2a_rta(family
, addr_attr
));
1500 bits
= __mask_bits(RTA_DATA(mask_attr
), len
);
1502 sprintf(out
+ done
, "/%s", rt_addr_n2a_rta(family
, mask_attr
));
1503 else if (bits
< len
* 8)
1504 sprintf(out
+ done
, "/%d", bits
);
1506 sprintf(namefrm
, "\n %s %%s", name
);
1507 print_string(PRINT_ANY
, name
, namefrm
, out
);
1509 static void flower_print_ip4_addr(char *name
, struct rtattr
*addr_attr
,
1510 struct rtattr
*mask_attr
)
1512 return flower_print_ip_addr(name
, htons(ETH_P_IP
),
1513 addr_attr
, mask_attr
, 0, 0);
1516 static void flower_print_port(char *name
, struct rtattr
*attr
)
1518 SPRINT_BUF(namefrm
);
1523 sprintf(namefrm
,"\n %s %%u", name
);
1524 print_hu(PRINT_ANY
, name
, namefrm
, rta_getattr_be16(attr
));
1527 static void flower_print_port_range(char *name
, struct rtattr
*min_attr
,
1528 struct rtattr
*max_attr
)
1530 if (!min_attr
|| !max_attr
)
1533 if (is_json_context()) {
1534 open_json_object(name
);
1535 print_hu(PRINT_JSON
, "start", NULL
, rta_getattr_be16(min_attr
));
1536 print_hu(PRINT_JSON
, "end", NULL
, rta_getattr_be16(max_attr
));
1537 close_json_object();
1539 SPRINT_BUF(namefrm
);
1543 done
= sprintf(out
, "%u", rta_getattr_be16(min_attr
));
1544 sprintf(out
+ done
, "-%u", rta_getattr_be16(max_attr
));
1545 sprintf(namefrm
, "\n %s %%s", name
);
1546 print_string(PRINT_ANY
, name
, namefrm
, out
);
1550 static void flower_print_tcp_flags(const char *name
, struct rtattr
*flags_attr
,
1551 struct rtattr
*mask_attr
)
1553 SPRINT_BUF(namefrm
);
1560 done
= sprintf(out
, "0x%x", rta_getattr_be16(flags_attr
));
1562 sprintf(out
+ done
, "/%x", rta_getattr_be16(mask_attr
));
1564 print_string(PRINT_FP
, NULL
, "%s ", _SL_
);
1565 sprintf(namefrm
, "%s %%s", name
);
1566 print_string(PRINT_ANY
, name
, namefrm
, out
);
1570 static void flower_print_key_id(const char *name
, struct rtattr
*attr
)
1572 SPRINT_BUF(namefrm
);
1577 sprintf(namefrm
,"\n %s %%u", name
);
1578 print_uint(PRINT_ANY
, name
, namefrm
, rta_getattr_be32(attr
));
1581 static void flower_print_geneve_opts(const char *name
, struct rtattr
*attr
,
1584 struct rtattr
*tb
[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX
+ 1];
1585 int ii
, data_len
, offset
= 0, slen
= 0;
1586 struct rtattr
*i
= RTA_DATA(attr
);
1587 int rem
= RTA_PAYLOAD(attr
);
1588 __u8 type
, data_r
[rem
];
1589 char data
[rem
* 2 + 1];
1592 open_json_array(PRINT_JSON
, name
);
1594 parse_rtattr(tb
, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX
, i
, rem
);
1595 class = rta_getattr_be16(tb
[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS
]);
1596 type
= rta_getattr_u8(tb
[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE
]);
1597 data_len
= RTA_PAYLOAD(tb
[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA
]);
1598 hexstring_n2a(RTA_DATA(tb
[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA
]),
1599 data_len
, data
, sizeof(data
));
1600 hex2mem(data
, data_r
, data_len
);
1601 offset
+= data_len
+ 20;
1602 rem
-= data_len
+ 20;
1603 i
= RTA_DATA(attr
) + offset
;
1605 open_json_object(NULL
);
1606 print_uint(PRINT_JSON
, "class", NULL
, class);
1607 print_uint(PRINT_JSON
, "type", NULL
, type
);
1608 open_json_array(PRINT_JSON
, "data");
1609 for (ii
= 0; ii
< data_len
; ii
++)
1610 print_uint(PRINT_JSON
, NULL
, NULL
, data_r
[ii
]);
1611 close_json_array(PRINT_JSON
, "data");
1612 close_json_object();
1614 slen
+= sprintf(strbuf
+ slen
, "%04x:%02x:%s",
1617 slen
+= sprintf(strbuf
+ slen
, ",");
1619 close_json_array(PRINT_JSON
, name
);
1622 static void flower_print_geneve_parts(const char *name
, struct rtattr
*attr
,
1623 char *key
, char *mask
)
1625 char *namefrm
= "\n geneve_opt %s";
1626 char *key_token
, *mask_token
, *out
;
1629 out
= malloc(RTA_PAYLOAD(attr
) * 4 + 3);
1634 key_token
= strsep(&key
, ",");
1635 mask_token
= strsep(&mask
, ",");
1637 len
+= sprintf(&out
[len
], "%s/%s,", key_token
, mask_token
);
1638 mask_token
= strsep(&mask
, ",");
1639 key_token
= strsep(&key
, ",");
1642 out
[len
- 1] = '\0';
1643 print_string(PRINT_FP
, name
, namefrm
, out
);
1647 static void flower_print_enc_opts(const char *name
, struct rtattr
*attr
,
1648 struct rtattr
*mask_attr
)
1650 struct rtattr
*key_tb
[TCA_FLOWER_KEY_ENC_OPTS_MAX
+ 1];
1651 struct rtattr
*msk_tb
[TCA_FLOWER_KEY_ENC_OPTS_MAX
+ 1];
1657 key
= malloc(RTA_PAYLOAD(attr
) * 2 + 1);
1661 msk
= malloc(RTA_PAYLOAD(attr
) * 2 + 1);
1665 parse_rtattr_nested(key_tb
, TCA_FLOWER_KEY_ENC_OPTS_MAX
, attr
);
1666 flower_print_geneve_opts("geneve_opt_key",
1667 key_tb
[TCA_FLOWER_KEY_ENC_OPTS_GENEVE
], key
);
1669 parse_rtattr_nested(msk_tb
, TCA_FLOWER_KEY_ENC_OPTS_MAX
, mask_attr
);
1670 flower_print_geneve_opts("geneve_opt_mask",
1671 msk_tb
[TCA_FLOWER_KEY_ENC_OPTS_GENEVE
], msk
);
1673 flower_print_geneve_parts(name
, attr
, key
, msk
);
1680 static void flower_print_masked_u8(const char *name
, struct rtattr
*attr
,
1681 struct rtattr
*mask_attr
,
1682 const char *(*value_to_str
)(__u8 value
))
1684 const char *value_str
= NULL
;
1686 SPRINT_BUF(namefrm
);
1693 value
= rta_getattr_u8(attr
);
1694 mask
= mask_attr
? rta_getattr_u8(mask_attr
) : UINT8_MAX
;
1695 if (mask
== UINT8_MAX
&& value_to_str
)
1696 value_str
= value_to_str(value
);
1699 done
= sprintf(out
, "%s", value_str
);
1701 done
= sprintf(out
, "%d", value
);
1703 if (mask
!= UINT8_MAX
)
1704 sprintf(out
+ done
, "/%d", mask
);
1706 sprintf(namefrm
,"\n %s %%s", name
);
1707 print_string(PRINT_ANY
, name
, namefrm
, out
);
1710 static void flower_print_u8(const char *name
, struct rtattr
*attr
)
1712 flower_print_masked_u8(name
, attr
, NULL
, NULL
);
1715 static void flower_print_u32(const char *name
, struct rtattr
*attr
)
1717 SPRINT_BUF(namefrm
);
1722 sprintf(namefrm
,"\n %s %%u", name
);
1723 print_uint(PRINT_ANY
, name
, namefrm
, rta_getattr_u32(attr
));
1726 static void flower_print_arp_op(const char *name
,
1727 struct rtattr
*op_attr
,
1728 struct rtattr
*mask_attr
)
1730 flower_print_masked_u8(name
, op_attr
, mask_attr
,
1731 flower_print_arp_op_to_name
);
1734 static int flower_print_opt(struct filter_util
*qu
, FILE *f
,
1735 struct rtattr
*opt
, __u32 handle
)
1737 struct rtattr
*tb
[TCA_FLOWER_MAX
+ 1];
1738 __be16 min_port_type
, max_port_type
;
1739 int nl_type
, nl_mask_type
;
1740 __be16 eth_type
= 0;
1741 __u8 ip_proto
= 0xff;
1746 parse_rtattr_nested(tb
, TCA_FLOWER_MAX
, opt
);
1749 print_uint(PRINT_ANY
, "handle", "handle 0x%x ", handle
);
1751 if (tb
[TCA_FLOWER_CLASSID
]) {
1752 __u32 h
= rta_getattr_u32(tb
[TCA_FLOWER_CLASSID
]);
1754 if (TC_H_MIN(h
) < TC_H_MIN_PRIORITY
||
1755 TC_H_MIN(h
) > (TC_H_MIN_PRIORITY
+ TC_QOPT_MAX_QUEUE
- 1)) {
1757 print_string(PRINT_ANY
, "classid", "classid %s ",
1758 sprint_tc_classid(h
, b1
));
1760 print_uint(PRINT_ANY
, "hw_tc", "hw_tc %u ",
1761 TC_H_MIN(h
) - TC_H_MIN_PRIORITY
);
1765 if (tb
[TCA_FLOWER_INDEV
]) {
1766 struct rtattr
*attr
= tb
[TCA_FLOWER_INDEV
];
1768 print_string(PRINT_ANY
, "indev", "\n indev %s",
1769 rta_getattr_str(attr
));
1772 open_json_object("keys");
1774 if (tb
[TCA_FLOWER_KEY_VLAN_ID
]) {
1775 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_VLAN_ID
];
1777 print_uint(PRINT_ANY
, "vlan_id", "\n vlan_id %u",
1778 rta_getattr_u16(attr
));
1781 if (tb
[TCA_FLOWER_KEY_VLAN_PRIO
]) {
1782 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_VLAN_PRIO
];
1784 print_uint(PRINT_ANY
, "vlan_prio", "\n vlan_prio %d",
1785 rta_getattr_u8(attr
));
1788 if (tb
[TCA_FLOWER_KEY_VLAN_ETH_TYPE
]) {
1790 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_VLAN_ETH_TYPE
];
1792 print_string(PRINT_ANY
, "vlan_ethtype", "\n vlan_ethtype %s",
1793 ll_proto_n2a(rta_getattr_u16(attr
),
1797 if (tb
[TCA_FLOWER_KEY_CVLAN_ID
]) {
1798 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_CVLAN_ID
];
1800 print_uint(PRINT_ANY
, "cvlan_id", "\n cvlan_id %u",
1801 rta_getattr_u16(attr
));
1804 if (tb
[TCA_FLOWER_KEY_CVLAN_PRIO
]) {
1805 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_CVLAN_PRIO
];
1807 print_uint(PRINT_ANY
, "cvlan_prio", "\n cvlan_prio %d",
1808 rta_getattr_u8(attr
));
1811 if (tb
[TCA_FLOWER_KEY_CVLAN_ETH_TYPE
]) {
1813 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_CVLAN_ETH_TYPE
];
1815 print_string(PRINT_ANY
, "cvlan_ethtype", "\n cvlan_ethtype %s",
1816 ll_proto_n2a(rta_getattr_u16(attr
),
1820 flower_print_eth_addr("dst_mac", tb
[TCA_FLOWER_KEY_ETH_DST
],
1821 tb
[TCA_FLOWER_KEY_ETH_DST_MASK
]);
1822 flower_print_eth_addr("src_mac", tb
[TCA_FLOWER_KEY_ETH_SRC
],
1823 tb
[TCA_FLOWER_KEY_ETH_SRC_MASK
]);
1825 flower_print_eth_type(ð_type
, tb
[TCA_FLOWER_KEY_ETH_TYPE
]);
1826 flower_print_ip_proto(&ip_proto
, tb
[TCA_FLOWER_KEY_IP_PROTO
]);
1828 flower_print_ip_attr("ip_tos", tb
[TCA_FLOWER_KEY_IP_TOS
],
1829 tb
[TCA_FLOWER_KEY_IP_TOS_MASK
]);
1830 flower_print_ip_attr("ip_ttl", tb
[TCA_FLOWER_KEY_IP_TTL
],
1831 tb
[TCA_FLOWER_KEY_IP_TTL_MASK
]);
1833 flower_print_u32("mpls_label", tb
[TCA_FLOWER_KEY_MPLS_LABEL
]);
1834 flower_print_u8("mpls_tc", tb
[TCA_FLOWER_KEY_MPLS_TC
]);
1835 flower_print_u8("mpls_bos", tb
[TCA_FLOWER_KEY_MPLS_BOS
]);
1836 flower_print_u8("mpls_ttl", tb
[TCA_FLOWER_KEY_MPLS_TTL
]);
1838 flower_print_ip_addr("dst_ip", eth_type
,
1839 tb
[TCA_FLOWER_KEY_IPV4_DST
],
1840 tb
[TCA_FLOWER_KEY_IPV4_DST_MASK
],
1841 tb
[TCA_FLOWER_KEY_IPV6_DST
],
1842 tb
[TCA_FLOWER_KEY_IPV6_DST_MASK
]);
1844 flower_print_ip_addr("src_ip", eth_type
,
1845 tb
[TCA_FLOWER_KEY_IPV4_SRC
],
1846 tb
[TCA_FLOWER_KEY_IPV4_SRC_MASK
],
1847 tb
[TCA_FLOWER_KEY_IPV6_SRC
],
1848 tb
[TCA_FLOWER_KEY_IPV6_SRC_MASK
]);
1850 nl_type
= flower_port_attr_type(ip_proto
, FLOWER_ENDPOINT_DST
);
1852 flower_print_port("dst_port", tb
[nl_type
]);
1853 nl_type
= flower_port_attr_type(ip_proto
, FLOWER_ENDPOINT_SRC
);
1855 flower_print_port("src_port", tb
[nl_type
]);
1857 if (!flower_port_range_attr_type(ip_proto
, FLOWER_ENDPOINT_DST
,
1858 &min_port_type
, &max_port_type
))
1859 flower_print_port_range("dst_port",
1860 tb
[min_port_type
], tb
[max_port_type
]);
1862 if (!flower_port_range_attr_type(ip_proto
, FLOWER_ENDPOINT_SRC
,
1863 &min_port_type
, &max_port_type
))
1864 flower_print_port_range("src_port",
1865 tb
[min_port_type
], tb
[max_port_type
]);
1867 flower_print_tcp_flags("tcp_flags", tb
[TCA_FLOWER_KEY_TCP_FLAGS
],
1868 tb
[TCA_FLOWER_KEY_TCP_FLAGS_MASK
]);
1870 nl_type
= flower_icmp_attr_type(eth_type
, ip_proto
,
1871 FLOWER_ICMP_FIELD_TYPE
);
1872 nl_mask_type
= flower_icmp_attr_mask_type(eth_type
, ip_proto
,
1873 FLOWER_ICMP_FIELD_TYPE
);
1874 if (nl_type
>= 0 && nl_mask_type
>= 0)
1875 flower_print_masked_u8("icmp_type", tb
[nl_type
],
1876 tb
[nl_mask_type
], NULL
);
1878 nl_type
= flower_icmp_attr_type(eth_type
, ip_proto
,
1879 FLOWER_ICMP_FIELD_CODE
);
1880 nl_mask_type
= flower_icmp_attr_mask_type(eth_type
, ip_proto
,
1881 FLOWER_ICMP_FIELD_CODE
);
1882 if (nl_type
>= 0 && nl_mask_type
>= 0)
1883 flower_print_masked_u8("icmp_code", tb
[nl_type
],
1884 tb
[nl_mask_type
], NULL
);
1886 flower_print_ip4_addr("arp_sip", tb
[TCA_FLOWER_KEY_ARP_SIP
],
1887 tb
[TCA_FLOWER_KEY_ARP_SIP_MASK
]);
1888 flower_print_ip4_addr("arp_tip", tb
[TCA_FLOWER_KEY_ARP_TIP
],
1889 tb
[TCA_FLOWER_KEY_ARP_TIP_MASK
]);
1890 flower_print_arp_op("arp_op", tb
[TCA_FLOWER_KEY_ARP_OP
],
1891 tb
[TCA_FLOWER_KEY_ARP_OP_MASK
]);
1892 flower_print_eth_addr("arp_sha", tb
[TCA_FLOWER_KEY_ARP_SHA
],
1893 tb
[TCA_FLOWER_KEY_ARP_SHA_MASK
]);
1894 flower_print_eth_addr("arp_tha", tb
[TCA_FLOWER_KEY_ARP_THA
],
1895 tb
[TCA_FLOWER_KEY_ARP_THA_MASK
]);
1897 flower_print_ip_addr("enc_dst_ip",
1898 tb
[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
] ?
1899 htons(ETH_P_IP
) : htons(ETH_P_IPV6
),
1900 tb
[TCA_FLOWER_KEY_ENC_IPV4_DST
],
1901 tb
[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
],
1902 tb
[TCA_FLOWER_KEY_ENC_IPV6_DST
],
1903 tb
[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK
]);
1905 flower_print_ip_addr("enc_src_ip",
1906 tb
[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
] ?
1907 htons(ETH_P_IP
) : htons(ETH_P_IPV6
),
1908 tb
[TCA_FLOWER_KEY_ENC_IPV4_SRC
],
1909 tb
[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
],
1910 tb
[TCA_FLOWER_KEY_ENC_IPV6_SRC
],
1911 tb
[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK
]);
1913 flower_print_key_id("enc_key_id", tb
[TCA_FLOWER_KEY_ENC_KEY_ID
]);
1915 flower_print_port("enc_dst_port", tb
[TCA_FLOWER_KEY_ENC_UDP_DST_PORT
]);
1917 flower_print_ip_attr("enc_tos", tb
[TCA_FLOWER_KEY_ENC_IP_TOS
],
1918 tb
[TCA_FLOWER_KEY_ENC_IP_TOS_MASK
]);
1919 flower_print_ip_attr("enc_ttl", tb
[TCA_FLOWER_KEY_ENC_IP_TTL
],
1920 tb
[TCA_FLOWER_KEY_ENC_IP_TTL_MASK
]);
1921 flower_print_enc_opts("enc_opt", tb
[TCA_FLOWER_KEY_ENC_OPTS
],
1922 tb
[TCA_FLOWER_KEY_ENC_OPTS_MASK
]);
1924 flower_print_matching_flags("ip_flags", FLOWER_IP_FLAGS
,
1925 tb
[TCA_FLOWER_KEY_FLAGS
],
1926 tb
[TCA_FLOWER_KEY_FLAGS_MASK
]);
1928 close_json_object();
1930 if (tb
[TCA_FLOWER_FLAGS
]) {
1931 __u32 flags
= rta_getattr_u32(tb
[TCA_FLOWER_FLAGS
]);
1933 if (flags
& TCA_CLS_FLAGS_SKIP_HW
)
1934 print_bool(PRINT_ANY
, "skip_hw", "\n skip_hw", true);
1935 if (flags
& TCA_CLS_FLAGS_SKIP_SW
)
1936 print_bool(PRINT_ANY
, "skip_sw", "\n skip_sw", true);
1938 if (flags
& TCA_CLS_FLAGS_IN_HW
) {
1939 print_bool(PRINT_ANY
, "in_hw", "\n in_hw", true);
1941 if (tb
[TCA_FLOWER_IN_HW_COUNT
]) {
1942 __u32 count
= rta_getattr_u32(tb
[TCA_FLOWER_IN_HW_COUNT
]);
1944 print_uint(PRINT_ANY
, "in_hw_count",
1945 " in_hw_count %u", count
);
1948 else if (flags
& TCA_CLS_FLAGS_NOT_IN_HW
)
1949 print_bool(PRINT_ANY
, "not_in_hw", "\n not_in_hw", true);
1952 if (tb
[TCA_FLOWER_ACT
])
1953 tc_print_action(f
, tb
[TCA_FLOWER_ACT
], 0);
1958 struct filter_util flower_filter_util
= {
1960 .parse_fopt
= flower_parse_opt
,
1961 .print_fopt
= flower_print_opt
,