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/if_arp.h>
18 #include <linux/if_ether.h>
20 #include <linux/tc_act/tc_vlan.h>
21 #include <linux/mpls.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"
56 " ip_tos MASKED-IP_TOS |\n"
57 " ip_ttl MASKED-IP_TTL |\n"
58 " mpls_label LABEL |\n"
64 " dst_port PORT-NUMBER |\n"
65 " src_port PORT-NUMBER |\n"
66 " tcp_flags MASKED-TCP_FLAGS |\n"
67 " type MASKED-ICMP-TYPE |\n"
68 " code MASKED-ICMP-CODE |\n"
69 " arp_tip IPV4-PREFIX |\n"
70 " arp_sip IPV4-PREFIX |\n"
71 " arp_op [ request | reply | OP ] |\n"
72 " arp_tha MASKED-LLADDR |\n"
73 " arp_sha MASKED-LLADDR |\n"
74 " enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
75 " enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
76 " enc_key_id [ KEY-ID ] |\n"
77 " ip_flags IP-FLAGS | \n"
78 " enc_dst_port [ port_number ] }\n"
79 " FILTERID := X:Y:Z\n"
80 " MASKED_LLADDR := { LLADDR | LLADDR/MASK | LLADDR/BITS }\n"
81 " ACTION-SPEC := ... look at individual actions\n"
83 "NOTE: CLASSID, IP-PROTO are parsed as hexadecimal input.\n"
84 "NOTE: There can be only used one mask per one prio. If user needs\n"
85 " to specify different mask, he has to use different prio.\n");
88 static int flower_parse_eth_addr(char *str
, int addr_type
, int mask_type
,
92 char addr
[ETH_ALEN
], *slash
;
94 slash
= strchr(str
, '/');
98 ret
= ll_addr_a2n(addr
, sizeof(addr
), str
);
101 addattr_l(n
, MAX_MSG
, addr_type
, addr
, sizeof(addr
));
106 if (!get_unsigned(&bits
, slash
+ 1, 10)) {
109 /* Extra 16 bit shift to push mac address into
110 * high bits of uint64_t
112 mask
= htonll(0xffffffffffffULL
<< (16 + 48 - bits
));
113 memcpy(addr
, &mask
, ETH_ALEN
);
115 ret
= ll_addr_a2n(addr
, sizeof(addr
), slash
+ 1);
120 memset(addr
, 0xff, ETH_ALEN
);
122 addattr_l(n
, MAX_MSG
, mask_type
, addr
, sizeof(addr
));
131 static int flower_parse_vlan_eth_type(char *str
, __be16 eth_type
, int type
,
132 __be16
*p_vlan_eth_type
,
135 __be16 vlan_eth_type
;
137 if (eth_type
!= htons(ETH_P_8021Q
)) {
139 "Can't set \"vlan_ethtype\" if ethertype isn't 802.1Q\n");
143 if (ll_proto_a2n(&vlan_eth_type
, str
))
144 invarg("invalid vlan_ethtype", str
);
145 addattr16(n
, MAX_MSG
, type
, vlan_eth_type
);
146 *p_vlan_eth_type
= vlan_eth_type
;
150 struct flag_to_string
{
152 enum flower_matching_flags type
;
156 static struct flag_to_string flags_str
[] = {
157 { TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT
, FLOWER_IP_FLAGS
, "frag" },
160 static int flower_parse_matching_flags(char *str
,
161 enum flower_matching_flags type
,
162 __u32
*mtf
, __u32
*mtf_mask
)
169 token
= strtok(str
, "/");
172 if (!strncmp(token
, "no", 2)) {
179 for (i
= 0; i
< ARRAY_SIZE(flags_str
); i
++) {
180 if (type
!= flags_str
[i
].type
)
183 if (!strcmp(token
, flags_str
[i
].string
)) {
185 *mtf
&= ~flags_str
[i
].flag
;
187 *mtf
|= flags_str
[i
].flag
;
189 *mtf_mask
|= flags_str
[i
].flag
;
197 token
= strtok(NULL
, "/");
203 static int flower_parse_ip_proto(char *str
, __be16 eth_type
, int type
,
204 __u8
*p_ip_proto
, struct nlmsghdr
*n
)
209 if (eth_type
!= htons(ETH_P_IP
) && eth_type
!= htons(ETH_P_IPV6
))
212 if (matches(str
, "tcp") == 0) {
213 ip_proto
= IPPROTO_TCP
;
214 } else if (matches(str
, "udp") == 0) {
215 ip_proto
= IPPROTO_UDP
;
216 } else if (matches(str
, "sctp") == 0) {
217 ip_proto
= IPPROTO_SCTP
;
218 } else if (matches(str
, "icmp") == 0) {
219 if (eth_type
!= htons(ETH_P_IP
))
221 ip_proto
= IPPROTO_ICMP
;
222 } else if (matches(str
, "icmpv6") == 0) {
223 if (eth_type
!= htons(ETH_P_IPV6
))
225 ip_proto
= IPPROTO_ICMPV6
;
227 ret
= get_u8(&ip_proto
, str
, 16);
231 addattr8(n
, MAX_MSG
, type
, ip_proto
);
232 *p_ip_proto
= ip_proto
;
236 fprintf(stderr
, "Illegal \"eth_type\" for ip proto\n");
240 static int __flower_parse_ip_addr(char *str
, int family
,
241 int addr4_type
, int mask4_type
,
242 int addr6_type
, int mask6_type
,
250 ret
= get_prefix(&addr
, str
, family
);
254 if (family
&& (addr
.family
!= family
)) {
255 fprintf(stderr
, "Illegal \"eth_type\" for ip address\n");
259 addattr_l(n
, MAX_MSG
, addr
.family
== AF_INET
? addr4_type
: addr6_type
,
260 addr
.data
, addr
.bytelen
);
262 memset(addr
.data
, 0xff, addr
.bytelen
);
264 for (i
= 0; i
< addr
.bytelen
/ 4; i
++) {
267 } else if (bits
/ 32 >= 1) {
270 addr
.data
[i
] <<= 32 - bits
;
271 addr
.data
[i
] = htonl(addr
.data
[i
]);
276 addattr_l(n
, MAX_MSG
, addr
.family
== AF_INET
? mask4_type
: mask6_type
,
277 addr
.data
, addr
.bytelen
);
282 static int flower_parse_ip_addr(char *str
, __be16 eth_type
,
283 int addr4_type
, int mask4_type
,
284 int addr6_type
, int mask6_type
,
289 if (eth_type
== htons(ETH_P_IP
)) {
291 } else if (eth_type
== htons(ETH_P_IPV6
)) {
293 } else if (!eth_type
) {
299 return __flower_parse_ip_addr(str
, family
, addr4_type
, mask4_type
,
300 addr6_type
, mask6_type
, n
);
303 static bool flower_eth_type_arp(__be16 eth_type
)
305 return eth_type
== htons(ETH_P_ARP
) || eth_type
== htons(ETH_P_RARP
);
308 static int flower_parse_arp_ip_addr(char *str
, __be16 eth_type
,
309 int addr_type
, int mask_type
,
312 if (!flower_eth_type_arp(eth_type
))
315 return __flower_parse_ip_addr(str
, AF_INET
, addr_type
, mask_type
,
316 TCA_FLOWER_UNSPEC
, TCA_FLOWER_UNSPEC
, n
);
319 static int flower_parse_u8(char *str
, int value_type
, int mask_type
,
320 int (*value_from_name
)(const char *str
,
322 bool (*value_validate
)(__u8 value
),
329 slash
= strchr(str
, '/');
333 ret
= value_from_name
? value_from_name(str
, &value
) : -1;
335 ret
= get_u8(&value
, str
, 10);
340 if (value_validate
&& !value_validate(value
))
344 ret
= get_u8(&mask
, slash
+ 1, 10);
352 addattr8(n
, MAX_MSG
, value_type
, value
);
353 addattr8(n
, MAX_MSG
, mask_type
, mask
);
362 static const char *flower_print_arp_op_to_name(__u8 op
)
374 static int flower_arp_op_from_name(const char *name
, __u8
*op
)
376 if (!strcmp(name
, "request"))
378 else if (!strcmp(name
, "reply"))
386 static bool flow_arp_op_validate(__u8 op
)
388 return !op
|| op
== ARPOP_REQUEST
|| op
== ARPOP_REPLY
;
391 static int flower_parse_arp_op(char *str
, __be16 eth_type
,
392 int op_type
, int mask_type
,
395 if (!flower_eth_type_arp(eth_type
))
398 return flower_parse_u8(str
, op_type
, mask_type
, flower_arp_op_from_name
,
399 flow_arp_op_validate
, n
);
402 static int flower_icmp_attr_type(__be16 eth_type
, __u8 ip_proto
,
403 enum flower_icmp_field field
)
405 if (eth_type
== htons(ETH_P_IP
) && ip_proto
== IPPROTO_ICMP
)
406 return field
== FLOWER_ICMP_FIELD_CODE
?
407 TCA_FLOWER_KEY_ICMPV4_CODE
:
408 TCA_FLOWER_KEY_ICMPV4_TYPE
;
409 else if (eth_type
== htons(ETH_P_IPV6
) && ip_proto
== IPPROTO_ICMPV6
)
410 return field
== FLOWER_ICMP_FIELD_CODE
?
411 TCA_FLOWER_KEY_ICMPV6_CODE
:
412 TCA_FLOWER_KEY_ICMPV6_TYPE
;
417 static int flower_icmp_attr_mask_type(__be16 eth_type
, __u8 ip_proto
,
418 enum flower_icmp_field field
)
420 if (eth_type
== htons(ETH_P_IP
) && ip_proto
== IPPROTO_ICMP
)
421 return field
== FLOWER_ICMP_FIELD_CODE
?
422 TCA_FLOWER_KEY_ICMPV4_CODE_MASK
:
423 TCA_FLOWER_KEY_ICMPV4_TYPE_MASK
;
424 else if (eth_type
== htons(ETH_P_IPV6
) && ip_proto
== IPPROTO_ICMPV6
)
425 return field
== FLOWER_ICMP_FIELD_CODE
?
426 TCA_FLOWER_KEY_ICMPV6_CODE_MASK
:
427 TCA_FLOWER_KEY_ICMPV6_TYPE_MASK
;
432 static int flower_parse_icmp(char *str
, __u16 eth_type
, __u8 ip_proto
,
433 enum flower_icmp_field field
, struct nlmsghdr
*n
)
435 int value_type
, mask_type
;
437 value_type
= flower_icmp_attr_type(eth_type
, ip_proto
, field
);
438 mask_type
= flower_icmp_attr_mask_type(eth_type
, ip_proto
, field
);
439 if (value_type
< 0 || mask_type
< 0)
442 return flower_parse_u8(str
, value_type
, mask_type
, NULL
, NULL
, n
);
445 static int flower_port_attr_type(__u8 ip_proto
, enum flower_endpoint endpoint
)
447 if (ip_proto
== IPPROTO_TCP
)
448 return endpoint
== FLOWER_ENDPOINT_SRC
?
449 TCA_FLOWER_KEY_TCP_SRC
:
450 TCA_FLOWER_KEY_TCP_DST
;
451 else if (ip_proto
== IPPROTO_UDP
)
452 return endpoint
== FLOWER_ENDPOINT_SRC
?
453 TCA_FLOWER_KEY_UDP_SRC
:
454 TCA_FLOWER_KEY_UDP_DST
;
455 else if (ip_proto
== IPPROTO_SCTP
)
456 return endpoint
== FLOWER_ENDPOINT_SRC
?
457 TCA_FLOWER_KEY_SCTP_SRC
:
458 TCA_FLOWER_KEY_SCTP_DST
;
463 static int flower_parse_port(char *str
, __u8 ip_proto
,
464 enum flower_endpoint endpoint
,
471 type
= flower_port_attr_type(ip_proto
, endpoint
);
475 ret
= get_be16(&port
, str
, 10);
479 addattr16(n
, MAX_MSG
, type
, port
);
484 #define TCP_FLAGS_MAX_MASK 0xfff
486 static int flower_parse_tcp_flags(char *str
, int flags_type
, int mask_type
,
493 slash
= strchr(str
, '/');
497 ret
= get_u16(&flags
, str
, 16);
498 if (ret
< 0 || flags
& ~TCP_FLAGS_MAX_MASK
)
501 addattr16(n
, MAX_MSG
, flags_type
, htons(flags
));
504 ret
= get_u16(&flags
, slash
+ 1, 16);
505 if (ret
< 0 || flags
& ~TCP_FLAGS_MAX_MASK
)
508 flags
= TCP_FLAGS_MAX_MASK
;
510 addattr16(n
, MAX_MSG
, mask_type
, htons(flags
));
519 static int flower_parse_ip_tos_ttl(char *str
, int key_type
, int mask_type
,
526 slash
= strchr(str
, '/');
530 ret
= get_u8(&tos_ttl
, str
, 10);
532 ret
= get_u8(&tos_ttl
, str
, 16);
536 addattr8(n
, MAX_MSG
, key_type
, tos_ttl
);
539 ret
= get_u8(&tos_ttl
, slash
+ 1, 16);
545 addattr8(n
, MAX_MSG
, mask_type
, tos_ttl
);
554 static int flower_parse_key_id(const char *str
, int type
, struct nlmsghdr
*n
)
559 ret
= get_be32(&key_id
, str
, 10);
561 addattr32(n
, MAX_MSG
, type
, key_id
);
566 static int flower_parse_enc_port(char *str
, int type
, struct nlmsghdr
*n
)
571 ret
= get_be16(&port
, str
, 10);
575 addattr16(n
, MAX_MSG
, type
, port
);
580 static int flower_parse_opt(struct filter_util
*qu
, char *handle
,
581 int argc
, char **argv
, struct nlmsghdr
*n
)
584 struct tcmsg
*t
= NLMSG_DATA(n
);
586 __be16 eth_type
= TC_H_MIN(t
->tcm_info
);
587 __be16 vlan_ethtype
= 0;
588 __u8 ip_proto
= 0xff;
594 ret
= get_u32(&t
->tcm_handle
, handle
, 0);
596 fprintf(stderr
, "Illegal \"handle\"\n");
601 tail
= (struct rtattr
*) (((void *) n
) + NLMSG_ALIGN(n
->nlmsg_len
));
602 addattr_l(n
, MAX_MSG
, TCA_OPTIONS
, NULL
, 0);
605 /*at minimal we will match all ethertype packets */
610 if (matches(*argv
, "classid") == 0 ||
611 matches(*argv
, "flowid") == 0) {
615 ret
= get_tc_classid(&handle
, *argv
);
617 fprintf(stderr
, "Illegal \"classid\"\n");
620 addattr_l(n
, MAX_MSG
, TCA_FLOWER_CLASSID
, &handle
, 4);
621 } else if (matches(*argv
, "hw_tc") == 0) {
627 tc
= strtoul(*argv
, &end
, 0);
629 fprintf(stderr
, "Illegal TC index\n");
632 if (tc
>= TC_QOPT_MAX_QUEUE
) {
633 fprintf(stderr
, "TC index exceeds max range\n");
636 handle
= TC_H_MAKE(TC_H_MAJ(t
->tcm_parent
),
637 TC_H_MIN(tc
+ TC_H_MIN_PRIORITY
));
638 addattr_l(n
, MAX_MSG
, TCA_FLOWER_CLASSID
, &handle
,
640 } else if (matches(*argv
, "ip_flags") == 0) {
642 ret
= flower_parse_matching_flags(*argv
,
647 fprintf(stderr
, "Illegal \"ip_flags\"\n");
650 } else if (matches(*argv
, "skip_hw") == 0) {
651 flags
|= TCA_CLS_FLAGS_SKIP_HW
;
652 } else if (matches(*argv
, "skip_sw") == 0) {
653 flags
|= TCA_CLS_FLAGS_SKIP_SW
;
654 } else if (matches(*argv
, "indev") == 0) {
656 if (check_ifname(*argv
))
657 invarg("\"indev\" not a valid ifname", *argv
);
658 addattrstrz(n
, MAX_MSG
, TCA_FLOWER_INDEV
, *argv
);
659 } else if (matches(*argv
, "vlan_id") == 0) {
663 if (eth_type
!= htons(ETH_P_8021Q
)) {
665 "Can't set \"vlan_id\" if ethertype isn't 802.1Q\n");
668 ret
= get_u16(&vid
, *argv
, 10);
669 if (ret
< 0 || vid
& ~0xfff) {
670 fprintf(stderr
, "Illegal \"vlan_id\"\n");
673 addattr16(n
, MAX_MSG
, TCA_FLOWER_KEY_VLAN_ID
, vid
);
674 } else if (matches(*argv
, "vlan_prio") == 0) {
678 if (eth_type
!= htons(ETH_P_8021Q
)) {
680 "Can't set \"vlan_prio\" if ethertype isn't 802.1Q\n");
683 ret
= get_u8(&vlan_prio
, *argv
, 10);
684 if (ret
< 0 || vlan_prio
& ~0x7) {
685 fprintf(stderr
, "Illegal \"vlan_prio\"\n");
689 TCA_FLOWER_KEY_VLAN_PRIO
, vlan_prio
);
690 } else if (matches(*argv
, "vlan_ethtype") == 0) {
692 ret
= flower_parse_vlan_eth_type(*argv
, eth_type
,
693 TCA_FLOWER_KEY_VLAN_ETH_TYPE
,
697 } else if (matches(*argv
, "mpls_label") == 0) {
701 if (eth_type
!= htons(ETH_P_MPLS_UC
) &&
702 eth_type
!= htons(ETH_P_MPLS_MC
)) {
704 "Can't set \"mpls_label\" if ethertype isn't MPLS\n");
707 ret
= get_u32(&label
, *argv
, 10);
708 if (ret
< 0 || label
& ~(MPLS_LS_LABEL_MASK
>> MPLS_LS_LABEL_SHIFT
)) {
709 fprintf(stderr
, "Illegal \"mpls_label\"\n");
712 addattr32(n
, MAX_MSG
, TCA_FLOWER_KEY_MPLS_LABEL
, label
);
713 } else if (matches(*argv
, "mpls_tc") == 0) {
717 if (eth_type
!= htons(ETH_P_MPLS_UC
) &&
718 eth_type
!= htons(ETH_P_MPLS_MC
)) {
720 "Can't set \"mpls_tc\" if ethertype isn't MPLS\n");
723 ret
= get_u8(&tc
, *argv
, 10);
724 if (ret
< 0 || tc
& ~(MPLS_LS_TC_MASK
>> MPLS_LS_TC_SHIFT
)) {
725 fprintf(stderr
, "Illegal \"mpls_tc\"\n");
728 addattr8(n
, MAX_MSG
, TCA_FLOWER_KEY_MPLS_TC
, tc
);
729 } else if (matches(*argv
, "mpls_bos") == 0) {
733 if (eth_type
!= htons(ETH_P_MPLS_UC
) &&
734 eth_type
!= htons(ETH_P_MPLS_MC
)) {
736 "Can't set \"mpls_bos\" if ethertype isn't MPLS\n");
739 ret
= get_u8(&bos
, *argv
, 10);
740 if (ret
< 0 || bos
& ~(MPLS_LS_S_MASK
>> MPLS_LS_S_SHIFT
)) {
741 fprintf(stderr
, "Illegal \"mpls_bos\"\n");
744 addattr8(n
, MAX_MSG
, TCA_FLOWER_KEY_MPLS_BOS
, bos
);
745 } else if (matches(*argv
, "mpls_ttl") == 0) {
749 if (eth_type
!= htons(ETH_P_MPLS_UC
) &&
750 eth_type
!= htons(ETH_P_MPLS_MC
)) {
752 "Can't set \"mpls_ttl\" if ethertype isn't MPLS\n");
755 ret
= get_u8(&ttl
, *argv
, 10);
756 if (ret
< 0 || ttl
& ~(MPLS_LS_TTL_MASK
>> MPLS_LS_TTL_SHIFT
)) {
757 fprintf(stderr
, "Illegal \"mpls_ttl\"\n");
760 addattr8(n
, MAX_MSG
, TCA_FLOWER_KEY_MPLS_TTL
, ttl
);
761 } else if (matches(*argv
, "dst_mac") == 0) {
763 ret
= flower_parse_eth_addr(*argv
,
764 TCA_FLOWER_KEY_ETH_DST
,
765 TCA_FLOWER_KEY_ETH_DST_MASK
,
768 fprintf(stderr
, "Illegal \"dst_mac\"\n");
771 } else if (matches(*argv
, "src_mac") == 0) {
773 ret
= flower_parse_eth_addr(*argv
,
774 TCA_FLOWER_KEY_ETH_SRC
,
775 TCA_FLOWER_KEY_ETH_SRC_MASK
,
778 fprintf(stderr
, "Illegal \"src_mac\"\n");
781 } else if (matches(*argv
, "ip_proto") == 0) {
783 ret
= flower_parse_ip_proto(*argv
, vlan_ethtype
?
784 vlan_ethtype
: eth_type
,
785 TCA_FLOWER_KEY_IP_PROTO
,
788 fprintf(stderr
, "Illegal \"ip_proto\"\n");
791 } else if (matches(*argv
, "ip_tos") == 0) {
793 ret
= flower_parse_ip_tos_ttl(*argv
,
794 TCA_FLOWER_KEY_IP_TOS
,
795 TCA_FLOWER_KEY_IP_TOS_MASK
,
798 fprintf(stderr
, "Illegal \"ip_tos\"\n");
801 } else if (matches(*argv
, "ip_ttl") == 0) {
803 ret
= flower_parse_ip_tos_ttl(*argv
,
804 TCA_FLOWER_KEY_IP_TTL
,
805 TCA_FLOWER_KEY_IP_TTL_MASK
,
808 fprintf(stderr
, "Illegal \"ip_ttl\"\n");
811 } else if (matches(*argv
, "dst_ip") == 0) {
813 ret
= flower_parse_ip_addr(*argv
, vlan_ethtype
?
814 vlan_ethtype
: eth_type
,
815 TCA_FLOWER_KEY_IPV4_DST
,
816 TCA_FLOWER_KEY_IPV4_DST_MASK
,
817 TCA_FLOWER_KEY_IPV6_DST
,
818 TCA_FLOWER_KEY_IPV6_DST_MASK
,
821 fprintf(stderr
, "Illegal \"dst_ip\"\n");
824 } else if (matches(*argv
, "src_ip") == 0) {
826 ret
= flower_parse_ip_addr(*argv
, vlan_ethtype
?
827 vlan_ethtype
: eth_type
,
828 TCA_FLOWER_KEY_IPV4_SRC
,
829 TCA_FLOWER_KEY_IPV4_SRC_MASK
,
830 TCA_FLOWER_KEY_IPV6_SRC
,
831 TCA_FLOWER_KEY_IPV6_SRC_MASK
,
834 fprintf(stderr
, "Illegal \"src_ip\"\n");
837 } else if (matches(*argv
, "dst_port") == 0) {
839 ret
= flower_parse_port(*argv
, ip_proto
,
840 FLOWER_ENDPOINT_DST
, n
);
842 fprintf(stderr
, "Illegal \"dst_port\"\n");
845 } else if (matches(*argv
, "src_port") == 0) {
847 ret
= flower_parse_port(*argv
, ip_proto
,
848 FLOWER_ENDPOINT_SRC
, n
);
850 fprintf(stderr
, "Illegal \"src_port\"\n");
853 } else if (matches(*argv
, "tcp_flags") == 0) {
855 ret
= flower_parse_tcp_flags(*argv
,
856 TCA_FLOWER_KEY_TCP_FLAGS
,
857 TCA_FLOWER_KEY_TCP_FLAGS_MASK
,
860 fprintf(stderr
, "Illegal \"tcp_flags\"\n");
863 } else if (matches(*argv
, "type") == 0) {
865 ret
= flower_parse_icmp(*argv
, eth_type
, ip_proto
,
866 FLOWER_ICMP_FIELD_TYPE
, n
);
868 fprintf(stderr
, "Illegal \"icmp type\"\n");
871 } else if (matches(*argv
, "code") == 0) {
873 ret
= flower_parse_icmp(*argv
, eth_type
, ip_proto
,
874 FLOWER_ICMP_FIELD_CODE
, n
);
876 fprintf(stderr
, "Illegal \"icmp code\"\n");
879 } else if (matches(*argv
, "arp_tip") == 0) {
881 ret
= flower_parse_arp_ip_addr(*argv
, vlan_ethtype
?
882 vlan_ethtype
: eth_type
,
883 TCA_FLOWER_KEY_ARP_TIP
,
884 TCA_FLOWER_KEY_ARP_TIP_MASK
,
887 fprintf(stderr
, "Illegal \"arp_tip\"\n");
890 } else if (matches(*argv
, "arp_sip") == 0) {
892 ret
= flower_parse_arp_ip_addr(*argv
, vlan_ethtype
?
893 vlan_ethtype
: eth_type
,
894 TCA_FLOWER_KEY_ARP_SIP
,
895 TCA_FLOWER_KEY_ARP_SIP_MASK
,
898 fprintf(stderr
, "Illegal \"arp_sip\"\n");
901 } else if (matches(*argv
, "arp_op") == 0) {
903 ret
= flower_parse_arp_op(*argv
, vlan_ethtype
?
904 vlan_ethtype
: eth_type
,
905 TCA_FLOWER_KEY_ARP_OP
,
906 TCA_FLOWER_KEY_ARP_OP_MASK
,
909 fprintf(stderr
, "Illegal \"arp_op\"\n");
912 } else if (matches(*argv
, "arp_tha") == 0) {
914 ret
= flower_parse_eth_addr(*argv
,
915 TCA_FLOWER_KEY_ARP_THA
,
916 TCA_FLOWER_KEY_ARP_THA_MASK
,
919 fprintf(stderr
, "Illegal \"arp_tha\"\n");
922 } else if (matches(*argv
, "arp_sha") == 0) {
924 ret
= flower_parse_eth_addr(*argv
,
925 TCA_FLOWER_KEY_ARP_SHA
,
926 TCA_FLOWER_KEY_ARP_SHA_MASK
,
929 fprintf(stderr
, "Illegal \"arp_sha\"\n");
932 } else if (matches(*argv
, "enc_dst_ip") == 0) {
934 ret
= flower_parse_ip_addr(*argv
, 0,
935 TCA_FLOWER_KEY_ENC_IPV4_DST
,
936 TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
,
937 TCA_FLOWER_KEY_ENC_IPV6_DST
,
938 TCA_FLOWER_KEY_ENC_IPV6_DST_MASK
,
941 fprintf(stderr
, "Illegal \"enc_dst_ip\"\n");
944 } else if (matches(*argv
, "enc_src_ip") == 0) {
946 ret
= flower_parse_ip_addr(*argv
, 0,
947 TCA_FLOWER_KEY_ENC_IPV4_SRC
,
948 TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
,
949 TCA_FLOWER_KEY_ENC_IPV6_SRC
,
950 TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK
,
953 fprintf(stderr
, "Illegal \"enc_src_ip\"\n");
956 } else if (matches(*argv
, "enc_key_id") == 0) {
958 ret
= flower_parse_key_id(*argv
,
959 TCA_FLOWER_KEY_ENC_KEY_ID
, n
);
961 fprintf(stderr
, "Illegal \"enc_key_id\"\n");
964 } else if (matches(*argv
, "enc_dst_port") == 0) {
966 ret
= flower_parse_enc_port(*argv
,
967 TCA_FLOWER_KEY_ENC_UDP_DST_PORT
, n
);
969 fprintf(stderr
, "Illegal \"enc_dst_port\"\n");
972 } else if (matches(*argv
, "action") == 0) {
974 ret
= parse_action(&argc
, &argv
, TCA_FLOWER_ACT
, n
);
976 fprintf(stderr
, "Illegal \"action\"\n");
980 } else if (strcmp(*argv
, "help") == 0) {
984 fprintf(stderr
, "What is \"%s\"?\n", *argv
);
992 ret
= addattr32(n
, MAX_MSG
, TCA_FLOWER_FLAGS
, flags
);
997 ret
= addattr32(n
, MAX_MSG
, TCA_FLOWER_KEY_FLAGS
, htonl(mtf
));
1001 ret
= addattr32(n
, MAX_MSG
, TCA_FLOWER_KEY_FLAGS_MASK
, htonl(mtf_mask
));
1006 if (eth_type
!= htons(ETH_P_ALL
)) {
1007 ret
= addattr16(n
, MAX_MSG
, TCA_FLOWER_KEY_ETH_TYPE
, eth_type
);
1012 tail
->rta_len
= (((void *)n
)+n
->nlmsg_len
) - (void *)tail
;
1017 static int __mask_bits(char *addr
, size_t len
)
1024 for (i
= 0; i
< len
; i
++, addr
++) {
1025 for (j
= 7; j
>= 0; j
--) {
1026 if (((*addr
) >> j
) & 0x1) {
1040 static void flower_print_eth_addr(FILE *f
, char *name
,
1041 struct rtattr
*addr_attr
,
1042 struct rtattr
*mask_attr
)
1047 if (!addr_attr
|| RTA_PAYLOAD(addr_attr
) != ETH_ALEN
)
1049 fprintf(f
, "\n %s %s", name
, ll_addr_n2a(RTA_DATA(addr_attr
), ETH_ALEN
,
1050 0, b1
, sizeof(b1
)));
1051 if (!mask_attr
|| RTA_PAYLOAD(mask_attr
) != ETH_ALEN
)
1053 bits
= __mask_bits(RTA_DATA(mask_attr
), ETH_ALEN
);
1055 fprintf(f
, "/%s", ll_addr_n2a(RTA_DATA(mask_attr
), ETH_ALEN
,
1056 0, b1
, sizeof(b1
)));
1057 else if (bits
< ETH_ALEN
* 8)
1058 fprintf(f
, "/%d", bits
);
1061 static void flower_print_eth_type(FILE *f
, __be16
*p_eth_type
,
1062 struct rtattr
*eth_type_attr
)
1069 eth_type
= rta_getattr_u16(eth_type_attr
);
1070 fprintf(f
, "\n eth_type ");
1071 if (eth_type
== htons(ETH_P_IP
))
1073 else if (eth_type
== htons(ETH_P_IPV6
))
1075 else if (eth_type
== htons(ETH_P_ARP
))
1077 else if (eth_type
== htons(ETH_P_RARP
))
1080 fprintf(f
, "%04x", ntohs(eth_type
));
1081 *p_eth_type
= eth_type
;
1084 static void flower_print_ip_proto(FILE *f
, __u8
*p_ip_proto
,
1085 struct rtattr
*ip_proto_attr
)
1092 ip_proto
= rta_getattr_u8(ip_proto_attr
);
1093 fprintf(f
, "\n ip_proto ");
1094 if (ip_proto
== IPPROTO_TCP
)
1096 else if (ip_proto
== IPPROTO_UDP
)
1098 else if (ip_proto
== IPPROTO_SCTP
)
1100 else if (ip_proto
== IPPROTO_ICMP
)
1102 else if (ip_proto
== IPPROTO_ICMPV6
)
1103 fprintf(f
, "icmpv6");
1105 fprintf(f
, "%02x", ip_proto
);
1106 *p_ip_proto
= ip_proto
;
1109 static void flower_print_ip_attr(FILE *f
, char *name
,
1110 struct rtattr
*key_attr
,
1111 struct rtattr
*mask_attr
)
1116 fprintf(f
, "\n %s %x", name
, rta_getattr_u8(key_attr
));
1119 fprintf(f
, "/%x", rta_getattr_u8(mask_attr
));
1122 static void flower_print_matching_flags(FILE *f
, char *name
,
1123 enum flower_matching_flags type
,
1124 struct rtattr
*attr
,
1125 struct rtattr
*mask_attr
)
1132 if (!mask_attr
|| RTA_PAYLOAD(mask_attr
) != 4)
1135 mtf
= ntohl(rta_getattr_u32(attr
));
1136 mtf_mask
= ntohl(rta_getattr_u32(mask_attr
));
1138 for (i
= 0; i
< ARRAY_SIZE(flags_str
); i
++) {
1139 if (type
!= flags_str
[i
].type
)
1141 if (mtf_mask
& flags_str
[i
].flag
) {
1143 fprintf(f
, "\n %s ", name
);
1147 if (mtf
& flags_str
[i
].flag
)
1148 fprintf(f
, "%s", flags_str
[i
].string
);
1150 fprintf(f
, "no%s", flags_str
[i
].string
);
1155 static void flower_print_ip_addr(FILE *f
, char *name
, __be16 eth_type
,
1156 struct rtattr
*addr4_attr
,
1157 struct rtattr
*mask4_attr
,
1158 struct rtattr
*addr6_attr
,
1159 struct rtattr
*mask6_attr
)
1161 struct rtattr
*addr_attr
;
1162 struct rtattr
*mask_attr
;
1167 if (eth_type
== htons(ETH_P_IP
)) {
1169 addr_attr
= addr4_attr
;
1170 mask_attr
= mask4_attr
;
1172 } else if (eth_type
== htons(ETH_P_IPV6
)) {
1174 addr_attr
= addr6_attr
;
1175 mask_attr
= mask6_attr
;
1180 if (!addr_attr
|| RTA_PAYLOAD(addr_attr
) != len
)
1182 fprintf(f
, "\n %s %s", name
, rt_addr_n2a_rta(family
, addr_attr
));
1183 if (!mask_attr
|| RTA_PAYLOAD(mask_attr
) != len
)
1185 bits
= __mask_bits(RTA_DATA(mask_attr
), len
);
1187 fprintf(f
, "/%s", rt_addr_n2a_rta(family
, mask_attr
));
1188 else if (bits
< len
* 8)
1189 fprintf(f
, "/%d", bits
);
1191 static void flower_print_ip4_addr(FILE *f
, char *name
,
1192 struct rtattr
*addr_attr
,
1193 struct rtattr
*mask_attr
)
1195 return flower_print_ip_addr(f
, name
, htons(ETH_P_IP
),
1196 addr_attr
, mask_attr
, 0, 0);
1199 static void flower_print_port(FILE *f
, char *name
, struct rtattr
*attr
)
1202 fprintf(f
, "\n %s %d", name
, rta_getattr_be16(attr
));
1205 static void flower_print_tcp_flags(FILE *f
, char *name
,
1206 struct rtattr
*flags_attr
,
1207 struct rtattr
*mask_attr
)
1211 fprintf(f
, "\n %s %x", name
, rta_getattr_be16(flags_attr
));
1214 fprintf(f
, "/%x", rta_getattr_be16(mask_attr
));
1218 static void flower_print_key_id(FILE *f
, const char *name
,
1219 struct rtattr
*attr
)
1222 fprintf(f
, "\n %s %d", name
, rta_getattr_be32(attr
));
1225 static void flower_print_masked_u8(FILE *f
, const char *name
,
1226 struct rtattr
*attr
,
1227 struct rtattr
*mask_attr
,
1228 const char *(*value_to_str
)(__u8 value
))
1230 const char *value_str
= NULL
;
1236 value
= rta_getattr_u8(attr
);
1237 mask
= mask_attr
? rta_getattr_u8(mask_attr
) : UINT8_MAX
;
1238 if (mask
== UINT8_MAX
&& value_to_str
)
1239 value_str
= value_to_str(value
);
1241 fprintf(f
, "\n %s ", name
);
1244 fputs(value_str
, f
);
1246 fprintf(f
, "%d", value
);
1248 if (mask
!= UINT8_MAX
)
1249 fprintf(f
, "/%d", mask
);
1252 static void flower_print_u8(FILE *f
, const char *name
, struct rtattr
*attr
)
1254 flower_print_masked_u8(f
, name
, attr
, NULL
, NULL
);
1257 static void flower_print_u32(FILE *f
, const char *name
, struct rtattr
*attr
)
1264 value
= rta_getattr_u32(attr
);
1266 fprintf(f
, "\n %s %d", name
, value
);
1269 static void flower_print_arp_op(FILE *f
, const char *name
,
1270 struct rtattr
*op_attr
,
1271 struct rtattr
*mask_attr
)
1273 flower_print_masked_u8(f
, name
, op_attr
, mask_attr
,
1274 flower_print_arp_op_to_name
);
1277 static int flower_print_opt(struct filter_util
*qu
, FILE *f
,
1278 struct rtattr
*opt
, __u32 handle
)
1280 struct rtattr
*tb
[TCA_FLOWER_MAX
+ 1];
1281 int nl_type
, nl_mask_type
;
1282 __be16 eth_type
= 0;
1283 __u8 ip_proto
= 0xff;
1288 parse_rtattr_nested(tb
, TCA_FLOWER_MAX
, opt
);
1291 fprintf(f
, "handle 0x%x ", handle
);
1293 if (tb
[TCA_FLOWER_CLASSID
]) {
1294 __u32 h
= rta_getattr_u32(tb
[TCA_FLOWER_CLASSID
]);
1296 if (TC_H_MIN(h
) < TC_H_MIN_PRIORITY
||
1297 TC_H_MIN(h
) > (TC_H_MIN_PRIORITY
+ TC_QOPT_MAX_QUEUE
- 1)) {
1299 fprintf(f
, "classid %s ", sprint_tc_classid(h
, b1
));
1301 fprintf(f
, "hw_tc %u ",
1302 TC_H_MIN(h
) - TC_H_MIN_PRIORITY
);
1306 if (tb
[TCA_FLOWER_INDEV
]) {
1307 struct rtattr
*attr
= tb
[TCA_FLOWER_INDEV
];
1309 fprintf(f
, "\n indev %s", rta_getattr_str(attr
));
1312 if (tb
[TCA_FLOWER_KEY_VLAN_ID
]) {
1313 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_VLAN_ID
];
1315 fprintf(f
, "\n vlan_id %d", rta_getattr_u16(attr
));
1318 if (tb
[TCA_FLOWER_KEY_VLAN_PRIO
]) {
1319 struct rtattr
*attr
= tb
[TCA_FLOWER_KEY_VLAN_PRIO
];
1321 fprintf(f
, "\n vlan_prio %d", rta_getattr_u8(attr
));
1324 flower_print_eth_addr(f
, "dst_mac", tb
[TCA_FLOWER_KEY_ETH_DST
],
1325 tb
[TCA_FLOWER_KEY_ETH_DST_MASK
]);
1326 flower_print_eth_addr(f
, "src_mac", tb
[TCA_FLOWER_KEY_ETH_SRC
],
1327 tb
[TCA_FLOWER_KEY_ETH_SRC_MASK
]);
1329 flower_print_eth_type(f
, ð_type
, tb
[TCA_FLOWER_KEY_ETH_TYPE
]);
1330 flower_print_ip_proto(f
, &ip_proto
, tb
[TCA_FLOWER_KEY_IP_PROTO
]);
1332 flower_print_ip_attr(f
, "ip_tos", tb
[TCA_FLOWER_KEY_IP_TOS
],
1333 tb
[TCA_FLOWER_KEY_IP_TOS_MASK
]);
1334 flower_print_ip_attr(f
, "ip_ttl", tb
[TCA_FLOWER_KEY_IP_TTL
],
1335 tb
[TCA_FLOWER_KEY_IP_TTL_MASK
]);
1337 flower_print_u32(f
, "mpls_label", tb
[TCA_FLOWER_KEY_MPLS_LABEL
]);
1338 flower_print_u8(f
, "mpls_tc", tb
[TCA_FLOWER_KEY_MPLS_TC
]);
1339 flower_print_u8(f
, "mpls_bos", tb
[TCA_FLOWER_KEY_MPLS_BOS
]);
1340 flower_print_u8(f
, "mpls_ttl", tb
[TCA_FLOWER_KEY_MPLS_TTL
]);
1342 flower_print_ip_addr(f
, "dst_ip", eth_type
,
1343 tb
[TCA_FLOWER_KEY_IPV4_DST
],
1344 tb
[TCA_FLOWER_KEY_IPV4_DST_MASK
],
1345 tb
[TCA_FLOWER_KEY_IPV6_DST
],
1346 tb
[TCA_FLOWER_KEY_IPV6_DST_MASK
]);
1348 flower_print_ip_addr(f
, "src_ip", eth_type
,
1349 tb
[TCA_FLOWER_KEY_IPV4_SRC
],
1350 tb
[TCA_FLOWER_KEY_IPV4_SRC_MASK
],
1351 tb
[TCA_FLOWER_KEY_IPV6_SRC
],
1352 tb
[TCA_FLOWER_KEY_IPV6_SRC_MASK
]);
1354 nl_type
= flower_port_attr_type(ip_proto
, FLOWER_ENDPOINT_DST
);
1356 flower_print_port(f
, "dst_port", tb
[nl_type
]);
1357 nl_type
= flower_port_attr_type(ip_proto
, FLOWER_ENDPOINT_SRC
);
1359 flower_print_port(f
, "src_port", tb
[nl_type
]);
1361 flower_print_tcp_flags(f
, "tcp_flags", tb
[TCA_FLOWER_KEY_TCP_FLAGS
],
1362 tb
[TCA_FLOWER_KEY_TCP_FLAGS_MASK
]);
1364 nl_type
= flower_icmp_attr_type(eth_type
, ip_proto
,
1365 FLOWER_ICMP_FIELD_TYPE
);
1366 nl_mask_type
= flower_icmp_attr_mask_type(eth_type
, ip_proto
,
1367 FLOWER_ICMP_FIELD_TYPE
);
1368 if (nl_type
>= 0 && nl_mask_type
>= 0)
1369 flower_print_masked_u8(f
, "icmp_type", tb
[nl_type
],
1370 tb
[nl_mask_type
], NULL
);
1372 nl_type
= flower_icmp_attr_type(eth_type
, ip_proto
,
1373 FLOWER_ICMP_FIELD_CODE
);
1374 nl_mask_type
= flower_icmp_attr_mask_type(eth_type
, ip_proto
,
1375 FLOWER_ICMP_FIELD_CODE
);
1376 if (nl_type
>= 0 && nl_mask_type
>= 0)
1377 flower_print_masked_u8(f
, "icmp_code", tb
[nl_type
],
1378 tb
[nl_mask_type
], NULL
);
1380 flower_print_ip4_addr(f
, "arp_sip", tb
[TCA_FLOWER_KEY_ARP_SIP
],
1381 tb
[TCA_FLOWER_KEY_ARP_SIP_MASK
]);
1382 flower_print_ip4_addr(f
, "arp_tip", tb
[TCA_FLOWER_KEY_ARP_TIP
],
1383 tb
[TCA_FLOWER_KEY_ARP_TIP_MASK
]);
1384 flower_print_arp_op(f
, "arp_op", tb
[TCA_FLOWER_KEY_ARP_OP
],
1385 tb
[TCA_FLOWER_KEY_ARP_OP_MASK
]);
1386 flower_print_eth_addr(f
, "arp_sha", tb
[TCA_FLOWER_KEY_ARP_SHA
],
1387 tb
[TCA_FLOWER_KEY_ARP_SHA_MASK
]);
1388 flower_print_eth_addr(f
, "arp_tha", tb
[TCA_FLOWER_KEY_ARP_THA
],
1389 tb
[TCA_FLOWER_KEY_ARP_THA_MASK
]);
1391 flower_print_ip_addr(f
, "enc_dst_ip",
1392 tb
[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
] ?
1393 htons(ETH_P_IP
) : htons(ETH_P_IPV6
),
1394 tb
[TCA_FLOWER_KEY_ENC_IPV4_DST
],
1395 tb
[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK
],
1396 tb
[TCA_FLOWER_KEY_ENC_IPV6_DST
],
1397 tb
[TCA_FLOWER_KEY_ENC_IPV6_DST_MASK
]);
1399 flower_print_ip_addr(f
, "enc_src_ip",
1400 tb
[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
] ?
1401 htons(ETH_P_IP
) : htons(ETH_P_IPV6
),
1402 tb
[TCA_FLOWER_KEY_ENC_IPV4_SRC
],
1403 tb
[TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK
],
1404 tb
[TCA_FLOWER_KEY_ENC_IPV6_SRC
],
1405 tb
[TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK
]);
1407 flower_print_key_id(f
, "enc_key_id",
1408 tb
[TCA_FLOWER_KEY_ENC_KEY_ID
]);
1410 flower_print_port(f
, "enc_dst_port",
1411 tb
[TCA_FLOWER_KEY_ENC_UDP_DST_PORT
]);
1413 flower_print_matching_flags(f
, "ip_flags",
1415 tb
[TCA_FLOWER_KEY_FLAGS
],
1416 tb
[TCA_FLOWER_KEY_FLAGS_MASK
]);
1418 if (tb
[TCA_FLOWER_FLAGS
]) {
1419 __u32 flags
= rta_getattr_u32(tb
[TCA_FLOWER_FLAGS
]);
1421 if (flags
& TCA_CLS_FLAGS_SKIP_HW
)
1422 fprintf(f
, "\n skip_hw");
1423 if (flags
& TCA_CLS_FLAGS_SKIP_SW
)
1424 fprintf(f
, "\n skip_sw");
1426 if (flags
& TCA_CLS_FLAGS_IN_HW
)
1427 fprintf(f
, "\n in_hw");
1428 else if (flags
& TCA_CLS_FLAGS_NOT_IN_HW
)
1429 fprintf(f
, "\n not_in_hw");
1432 if (tb
[TCA_FLOWER_ACT
])
1433 tc_print_action(f
, tb
[TCA_FLOWER_ACT
], 0);
1438 struct filter_util flower_filter_util
= {
1440 .parse_fopt
= flower_parse_opt
,
1441 .print_fopt
= flower_print_opt
,