Blame SOURCES/0120-tc-flower-support-matching-on-ICMP-type-and-code.patch

4aca6e
From e5ada6e0c92ee29337cec1117e1d6761ca6d4880 Mon Sep 17 00:00:00 2001
4aca6e
From: Phil Sutter <psutter@redhat.com>
4aca6e
Date: Fri, 17 Mar 2017 13:23:33 +0100
4aca6e
Subject: [PATCH] tc: flower: support matching on ICMP type and code
4aca6e
4aca6e
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1422629
4aca6e
Upstream Status: iproute2.git commit eb3b5696f1633
4aca6e
4aca6e
commit eb3b5696f16334b4513ad42882ca6bc35b78144d
4aca6e
Author: Simon Horman <simon.horman@netronome.com>
4aca6e
Date:   Wed Dec 7 14:54:03 2016 +0100
4aca6e
4aca6e
    tc: flower: support matching on ICMP type and code
4aca6e
4aca6e
    Support matching on ICMP type and code.
4aca6e
4aca6e
    Example usage:
4aca6e
4aca6e
    tc qdisc add dev eth0 ingress
4aca6e
4aca6e
    tc filter add dev eth0 protocol ip parent ffff: flower \
4aca6e
            indev eth0 ip_proto icmp type 8 code 0 action drop
4aca6e
4aca6e
    tc filter add dev eth0 protocol ipv6 parent ffff: flower \
4aca6e
            indev eth0 ip_proto icmpv6 type 128 code 0 action drop
4aca6e
4aca6e
    Signed-off-by: Simon Horman <simon.horman@netronome.com>
4aca6e
---
4aca6e
 man/man8/tc-flower.8 | 20 ++++++++---
4aca6e
 tc/f_flower.c        | 96 +++++++++++++++++++++++++++++++++++++++++++++++++---
4aca6e
 2 files changed, 106 insertions(+), 10 deletions(-)
4aca6e
4aca6e
diff --git a/man/man8/tc-flower.8 b/man/man8/tc-flower.8
4aca6e
index 744c450..90fdfba 100644
4aca6e
--- a/man/man8/tc-flower.8
4aca6e
+++ b/man/man8/tc-flower.8
4aca6e
@@ -29,7 +29,7 @@ flower \- flow based traffic control filter
4aca6e
 .IR PRIORITY " | "
4aca6e
 .BR vlan_eth_type " { " ipv4 " | " ipv6 " | "
4aca6e
 .IR ETH_TYPE " } | "
4aca6e
-.BR ip_proto " { " tcp " | " udp " | " sctp " | "
4aca6e
+.BR ip_proto " { " tcp " | " udp " | " sctp " | " icmp " | " icmpv6 " | "
4aca6e
 .IR IP_PROTO " } | { "
4aca6e
 .BR dst_ip " | " src_ip " } { "
4aca6e
 .IR ipv4_address " | " ipv6_address " } | { "
4aca6e
@@ -98,7 +98,7 @@ or an unsigned 16bit value in hexadecimal format.
4aca6e
 Match on layer four protocol.
4aca6e
 .I IP_PROTO
4aca6e
 may be
4aca6e
-.BR tcp ", " udp ", " sctp
4aca6e
+.BR tcp ", " udp ", " sctp ", " icmp ", " icmpv6
4aca6e
 or an unsigned 8bit value in hexadecimal format.
4aca6e
 .TP
4aca6e
 .BI dst_ip " ADDRESS"
4aca6e
@@ -117,6 +117,13 @@ Match on layer 4 protocol source or destination port number. Only available for
4aca6e
 .BR ip_proto " values " udp ", " tcp  " and " sctp
4aca6e
 which have to be specified in beforehand.
4aca6e
 .TP
4aca6e
+.BI type " NUMBER"
4aca6e
+.TQ
4aca6e
+.BI code " NUMBER"
4aca6e
+Match on ICMP type or code. Only available for
4aca6e
+.BR ip_proto " values " icmp  " and " icmpv6
4aca6e
+which have to be specified in beforehand.
4aca6e
+.TP
4aca6e
 .BI enc_key_id " NUMBER"
4aca6e
 .TQ
4aca6e
 .BI enc_dst_ip " ADDRESS"
4aca6e
@@ -135,13 +142,16 @@ have no dependency, layer three matches
4aca6e
 (\fBip_proto\fR, \fBdst_ip\fR and \fBsrc_ip\fR)
4aca6e
 depend on the
4aca6e
 .B protocol
4aca6e
-option of tc filter
4aca6e
-and finally layer four matches
4aca6e
+option of tc filter, layer four port matches
4aca6e
 (\fBdst_port\fR and \fBsrc_port\fR)
4aca6e
 depend on
4aca6e
 .B ip_proto
4aca6e
 being set to
4aca6e
-.BR tcp ", " udp " or " sctp.
4aca6e
+.BR tcp ", " udp " or " sctp,
4aca6e
+and finally ICMP matches (\fBcode\fR and \fBtype\fR) depend on
4aca6e
+.B ip_proto
4aca6e
+being set to
4aca6e
+.BR icmp " or " icmpv6.
4aca6e
 .P
4aca6e
 There can be only used one mask per one prio. If user needs to specify different
4aca6e
 mask, he has to use different prio.
4aca6e
diff --git a/tc/f_flower.c b/tc/f_flower.c
4aca6e
index 523b82b..adf7164 100644
4aca6e
--- a/tc/f_flower.c
4aca6e
+++ b/tc/f_flower.c
4aca6e
@@ -28,6 +28,11 @@ enum flower_endpoint {
4aca6e
 	FLOWER_ENDPOINT_DST
4aca6e
 };
4aca6e
 
4aca6e
+enum flower_icmp_field {
4aca6e
+	FLOWER_ICMP_FIELD_TYPE,
4aca6e
+	FLOWER_ICMP_FIELD_CODE
4aca6e
+};
4aca6e
+
4aca6e
 static void explain(void)
4aca6e
 {
4aca6e
 	fprintf(stderr,
4aca6e
@@ -42,11 +47,13 @@ static void explain(void)
4aca6e
 		"                       vlan_ethtype [ ipv4 | ipv6 | ETH-TYPE ] |\n"
4aca6e
 		"                       dst_mac MAC-ADDR |\n"
4aca6e
 		"                       src_mac MAC-ADDR |\n"
4aca6e
-		"                       ip_proto [tcp | udp | sctp | IP-PROTO ] |\n"
4aca6e
+		"                       ip_proto [tcp | udp | sctp | icmp | icmpv6 | IP-PROTO ] |\n"
4aca6e
 		"                       dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
4aca6e
 		"                       src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
4aca6e
 		"                       dst_port PORT-NUMBER |\n"
4aca6e
 		"                       src_port PORT-NUMBER |\n"
4aca6e
+		"                       type ICMP-TYPE |\n"
4aca6e
+		"                       code ICMP-CODE }\n"
4aca6e
 		"                       enc_dst_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
4aca6e
 		"                       enc_src_ip [ IPV4-ADDR | IPV6-ADDR ] |\n"
4aca6e
 		"                       enc_key_id [ KEY-ID ] }\n"
4aca6e
@@ -98,16 +105,23 @@ static int flower_parse_ip_proto(char *str, __be16 eth_type, int type,
4aca6e
 	int ret;
4aca6e
 	__u8 ip_proto;
4aca6e
 
4aca6e
-	if (eth_type != htons(ETH_P_IP) && eth_type != htons(ETH_P_IPV6)) {
4aca6e
-		fprintf(stderr, "Illegal \"eth_type\" for ip proto\n");
4aca6e
-		return -1;
4aca6e
-	}
4aca6e
+	if (eth_type != htons(ETH_P_IP) && eth_type != htons(ETH_P_IPV6))
4aca6e
+		goto err;
4aca6e
+
4aca6e
 	if (matches(str, "tcp") == 0) {
4aca6e
 		ip_proto = IPPROTO_TCP;
4aca6e
 	} else if (matches(str, "udp") == 0) {
4aca6e
 		ip_proto = IPPROTO_UDP;
4aca6e
 	} else if (matches(str, "sctp") == 0) {
4aca6e
 		ip_proto = IPPROTO_SCTP;
4aca6e
+	} else if (matches(str, "icmp") == 0) {
4aca6e
+		if (eth_type != htons(ETH_P_IP))
4aca6e
+			goto err;
4aca6e
+		ip_proto = IPPROTO_ICMP;
4aca6e
+	} else if (matches(str, "icmpv6") == 0) {
4aca6e
+		if (eth_type != htons(ETH_P_IPV6))
4aca6e
+			goto err;
4aca6e
+		ip_proto = IPPROTO_ICMPV6;
4aca6e
 	} else {
4aca6e
 		ret = get_u8(&ip_proto, str, 16);
4aca6e
 		if (ret)
4aca6e
@@ -116,6 +130,10 @@ static int flower_parse_ip_proto(char *str, __be16 eth_type, int type,
4aca6e
 	addattr8(n, MAX_MSG, type, ip_proto);
4aca6e
 	*p_ip_proto = ip_proto;
4aca6e
 	return 0;
4aca6e
+
4aca6e
+err:
4aca6e
+	fprintf(stderr, "Illegal \"eth_type\" for ip proto\n");
4aca6e
+	return -1;
4aca6e
 }
4aca6e
 
4aca6e
 static int flower_parse_ip_addr(char *str, __be16 eth_type,
4aca6e
@@ -171,6 +189,41 @@ static int flower_parse_ip_addr(char *str, __be16 eth_type,
4aca6e
 	return 0;
4aca6e
 }
4aca6e
 
4aca6e
+static int flower_icmp_attr_type(__be16 eth_type, __u8 ip_proto,
4aca6e
+				 enum flower_icmp_field field)
4aca6e
+{
4aca6e
+	if (eth_type == htons(ETH_P_IP) && ip_proto == IPPROTO_ICMP)
4aca6e
+		return field == FLOWER_ICMP_FIELD_CODE ?
4aca6e
+			TCA_FLOWER_KEY_ICMPV4_CODE :
4aca6e
+			TCA_FLOWER_KEY_ICMPV4_TYPE;
4aca6e
+	else if (eth_type == htons(ETH_P_IPV6) && ip_proto == IPPROTO_ICMPV6)
4aca6e
+		return field == FLOWER_ICMP_FIELD_CODE ?
4aca6e
+			TCA_FLOWER_KEY_ICMPV6_CODE :
4aca6e
+			TCA_FLOWER_KEY_ICMPV6_TYPE;
4aca6e
+
4aca6e
+	return -1;
4aca6e
+}
4aca6e
+
4aca6e
+static int flower_parse_icmp(char *str, __u16 eth_type, __u8 ip_proto,
4aca6e
+			     enum flower_icmp_field field, struct nlmsghdr *n)
4aca6e
+{
4aca6e
+	int ret;
4aca6e
+	int type;
4aca6e
+	uint8_t value;
4aca6e
+
4aca6e
+	type = flower_icmp_attr_type(eth_type, ip_proto, field);
4aca6e
+	if (type < 0)
4aca6e
+		return -1;
4aca6e
+
4aca6e
+	ret = get_u8(&value, str, 10);
4aca6e
+	if (ret)
4aca6e
+		return -1;
4aca6e
+
4aca6e
+	addattr8(n, MAX_MSG, type, value);
4aca6e
+
4aca6e
+	return 0;
4aca6e
+}
4aca6e
+
4aca6e
 static int flower_port_attr_type(__u8 ip_proto, enum flower_endpoint endpoint)
4aca6e
 {
4aca6e
 	if (ip_proto == IPPROTO_TCP)
4aca6e
@@ -382,6 +435,22 @@ static int flower_parse_opt(struct filter_util *qu, char *handle,
4aca6e
 				fprintf(stderr, "Illegal \"src_port\"\n");
4aca6e
 				return -1;
4aca6e
 			}
4aca6e
+		} else if (matches(*argv, "type") == 0) {
4aca6e
+			NEXT_ARG();
4aca6e
+			ret = flower_parse_icmp(*argv, eth_type, ip_proto,
4aca6e
+						FLOWER_ICMP_FIELD_TYPE, n);
4aca6e
+			if (ret < 0) {
4aca6e
+				fprintf(stderr, "Illegal \"icmp type\"\n");
4aca6e
+				return -1;
4aca6e
+			}
4aca6e
+		} else if (matches(*argv, "code") == 0) {
4aca6e
+			NEXT_ARG();
4aca6e
+			ret = flower_parse_icmp(*argv, eth_type, ip_proto,
4aca6e
+						FLOWER_ICMP_FIELD_CODE, n);
4aca6e
+			if (ret < 0) {
4aca6e
+				fprintf(stderr, "Illegal \"icmp code\"\n");
4aca6e
+				return -1;
4aca6e
+			}
4aca6e
 		} else if (matches(*argv, "enc_dst_ip") == 0) {
4aca6e
 			NEXT_ARG();
4aca6e
 			ret = flower_parse_ip_addr(*argv, 0,
4aca6e
@@ -527,6 +596,10 @@ static void flower_print_ip_proto(FILE *f, __u8 *p_ip_proto,
4aca6e
 		fprintf(f, "udp");
4aca6e
 	else if (ip_proto == IPPROTO_SCTP)
4aca6e
 		fprintf(f, "sctp");
4aca6e
+	else if (ip_proto == IPPROTO_ICMP)
4aca6e
+		fprintf(f, "icmp");
4aca6e
+	else if (ip_proto == IPPROTO_ICMPV6)
4aca6e
+		fprintf(f, "icmpv6");
4aca6e
 	else
4aca6e
 		fprintf(f, "%02x", ip_proto);
4aca6e
 	*p_ip_proto = ip_proto;
4aca6e
@@ -582,6 +655,12 @@ static void flower_print_key_id(FILE *f, const char *name,
4aca6e
 		fprintf(f, "\n  %s %d", name, rta_getattr_be32(attr));
4aca6e
 }
4aca6e
 
4aca6e
+static void flower_print_icmp(FILE *f, char *name, struct rtattr *attr)
4aca6e
+{
4aca6e
+	if (attr)
4aca6e
+		fprintf(f, "\n  %s %d", name, rta_getattr_u8(attr));
4aca6e
+}
4aca6e
+
4aca6e
 static int flower_print_opt(struct filter_util *qu, FILE *f,
4aca6e
 			    struct rtattr *opt, __u32 handle)
4aca6e
 {
4aca6e
@@ -650,6 +729,13 @@ static int flower_print_opt(struct filter_util *qu, FILE *f,
4aca6e
 	if (nl_type >= 0)
4aca6e
 		flower_print_port(f, "src_port", tb[nl_type]);
4aca6e
 
4aca6e
+	nl_type = flower_icmp_attr_type(eth_type, ip_proto, false);
4aca6e
+	if (nl_type >= 0)
4aca6e
+		flower_print_icmp(f, "icmp_type", tb[nl_type]);
4aca6e
+	nl_type = flower_icmp_attr_type(eth_type, ip_proto, true);
4aca6e
+	if (nl_type >= 0)
4aca6e
+		flower_print_icmp(f, "icmp_code", tb[nl_type]);
4aca6e
+
4aca6e
 	flower_print_ip_addr(f, "enc_dst_ip",
4aca6e
 			     tb[TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] ?
4aca6e
 			     htons(ETH_P_IP) : htons(ETH_P_IPV6),
4aca6e
-- 
4aca6e
1.8.3.1
4aca6e