Blame SOURCES/0036-tc-flower-Add-matching-on-conntrack-info.patch

07a51b
From b60d960c65c6aa5695ecf1c71b18790ca0cb475c Mon Sep 17 00:00:00 2001
07a51b
From: Andrea Claudi <aclaudi@redhat.com>
07a51b
Date: Tue, 9 Jun 2020 15:45:56 +0200
07a51b
Subject: [PATCH] tc: flower: Add matching on conntrack info
07a51b
07a51b
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1844637
07a51b
Upstream Status: iproute2.git commit 2fffb1c03056e
07a51b
07a51b
commit 2fffb1c03056e71d49d623f7ca460883fa6110a6
07a51b
Author: Paul Blakey <paulb@mellanox.com>
07a51b
Date:   Thu Jul 11 11:14:27 2019 +0300
07a51b
07a51b
    tc: flower: Add matching on conntrack info
07a51b
07a51b
    Matches on conntrack state, zone, mark, and label.
07a51b
07a51b
    Signed-off-by: Paul Blakey <paulb@mellanox.com>
07a51b
    Signed-off-by: Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>
07a51b
    Signed-off-by: Yossi Kuperman <yossiku@mellanox.com>
07a51b
    Acked-by: Jiri Pirko <jiri@mellanox.com>
07a51b
    Acked-by: Roi Dayan <roid@mellanox.com>
07a51b
    Signed-off-by: David Ahern <dsahern@gmail.com>
07a51b
---
07a51b
 man/man8/tc-flower.8 |  35 ++++++
07a51b
 tc/f_flower.c        | 276 ++++++++++++++++++++++++++++++++++++++++++-
07a51b
 2 files changed, 310 insertions(+), 1 deletion(-)
07a51b
07a51b
diff --git a/man/man8/tc-flower.8 b/man/man8/tc-flower.8
07a51b
index f41b0f7f1ef13..0f95f303f23b7 100644
07a51b
--- a/man/man8/tc-flower.8
07a51b
+++ b/man/man8/tc-flower.8
07a51b
@@ -295,6 +295,41 @@ bits is assumed.
07a51b
 .TQ
07a51b
 .BI enc_ttl " NUMBER"
07a51b
 .TQ
07a51b
+.BR
07a51b
+.TP
07a51b
+.BI ct_state " CT_STATE"
07a51b
+.TQ
07a51b
+.BI ct_zone " CT_MASKED_ZONE"
07a51b
+.TQ
07a51b
+.BI ct_mark " CT_MASKED_MARK"
07a51b
+.TQ
07a51b
+.BI ct_label " CT_MASKED_LABEL"
07a51b
+Matches on connection tracking info
07a51b
+.RS
07a51b
+.TP
07a51b
+.I CT_STATE
07a51b
+Match the connection state, and can ne combination of [{+|-}flag] flags, where flag can be one of
07a51b
+.RS
07a51b
+.TP
07a51b
+trk - Tracked connection.
07a51b
+.TP
07a51b
+new - New connection.
07a51b
+.TP
07a51b
+est - Established connection.
07a51b
+.TP
07a51b
+Example: +trk+est
07a51b
+.RE
07a51b
+.TP
07a51b
+.I CT_MASKED_ZONE
07a51b
+Match the connection zone, and can be masked.
07a51b
+.TP
07a51b
+.I CT_MASKED_MARK
07a51b
+32bit match on the connection mark, and can be masked.
07a51b
+.TP
07a51b
+.I CT_MASKED_LABEL
07a51b
+128bit match on the connection label, and can be masked.
07a51b
+.RE
07a51b
+.TP
07a51b
 .BI geneve_opts " OPTIONS"
07a51b
 .TQ
07a51b
 .BI vxlan_opts " OPTIONS"
07a51b
diff --git a/tc/f_flower.c b/tc/f_flower.c
07a51b
index 691541ec59d4c..0a6eaa0df94ce 100644
07a51b
--- a/tc/f_flower.c
07a51b
+++ b/tc/f_flower.c
07a51b
@@ -84,9 +84,14 @@ static void explain(void)
07a51b
 		"			vxlan_opts MASKED-OPTIONS |\n"
07a51b
 		"                       erspan_opts MASKED-OPTIONS |\n"
07a51b
 		"			ip_flags IP-FLAGS | \n"
07a51b
-		"			enc_dst_port [ port_number ] }\n"
07a51b
+		"			enc_dst_port [ port_number ] |\n"
07a51b
+		"			ct_state MASKED_CT_STATE |\n"
07a51b
+		"			ct_label MASKED_CT_LABEL |\n"
07a51b
+		"			ct_mark MASKED_CT_MARK |\n"
07a51b
+		"			ct_zone MASKED_CT_ZONE }\n"
07a51b
 		"	FILTERID := X:Y:Z\n"
07a51b
 		"	MASKED_LLADDR := { LLADDR | LLADDR/MASK | LLADDR/BITS }\n"
07a51b
+		"	MASKED_CT_STATE := combination of {+|-} and flags trk,est,new\n"
07a51b
 		"	ACTION-SPEC := ... look at individual actions\n"
07a51b
 		"\n"
07a51b
 		"NOTE:	CLASSID, IP-PROTO are parsed as hexadecimal input.\n"
07a51b
@@ -216,6 +221,159 @@ static int flower_parse_matching_flags(char *str,
07a51b
 	return 0;
07a51b
 }
07a51b
 
07a51b
+static int flower_parse_u16(char *str, int value_type, int mask_type,
07a51b
+			    struct nlmsghdr *n)
07a51b
+{
07a51b
+	__u16 value, mask;
07a51b
+	char *slash;
07a51b
+
07a51b
+	slash = strchr(str, '/');
07a51b
+	if (slash)
07a51b
+		*slash = '\0';
07a51b
+
07a51b
+	if (get_u16(&value, str, 0))
07a51b
+		return -1;
07a51b
+
07a51b
+	if (slash) {
07a51b
+		if (get_u16(&mask, slash + 1, 0))
07a51b
+			return -1;
07a51b
+	} else {
07a51b
+		mask = UINT16_MAX;
07a51b
+	}
07a51b
+
07a51b
+	addattr16(n, MAX_MSG, value_type, value);
07a51b
+	addattr16(n, MAX_MSG, mask_type, mask);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+static int flower_parse_u32(char *str, int value_type, int mask_type,
07a51b
+			    struct nlmsghdr *n)
07a51b
+{
07a51b
+	__u32 value, mask;
07a51b
+	char *slash;
07a51b
+
07a51b
+	slash = strchr(str, '/');
07a51b
+	if (slash)
07a51b
+		*slash = '\0';
07a51b
+
07a51b
+	if (get_u32(&value, str, 0))
07a51b
+		return -1;
07a51b
+
07a51b
+	if (slash) {
07a51b
+		if (get_u32(&mask, slash + 1, 0))
07a51b
+			return -1;
07a51b
+	} else {
07a51b
+		mask = UINT32_MAX;
07a51b
+	}
07a51b
+
07a51b
+	addattr32(n, MAX_MSG, value_type, value);
07a51b
+	addattr32(n, MAX_MSG, mask_type, mask);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+static int flower_parse_ct_mark(char *str, struct nlmsghdr *n)
07a51b
+{
07a51b
+	return flower_parse_u32(str,
07a51b
+				TCA_FLOWER_KEY_CT_MARK,
07a51b
+				TCA_FLOWER_KEY_CT_MARK_MASK,
07a51b
+				n);
07a51b
+}
07a51b
+
07a51b
+static int flower_parse_ct_zone(char *str, struct nlmsghdr *n)
07a51b
+{
07a51b
+	return flower_parse_u16(str,
07a51b
+				TCA_FLOWER_KEY_CT_ZONE,
07a51b
+				TCA_FLOWER_KEY_CT_ZONE_MASK,
07a51b
+				n);
07a51b
+}
07a51b
+
07a51b
+static int flower_parse_ct_labels(char *str, struct nlmsghdr *n)
07a51b
+{
07a51b
+#define LABELS_SIZE	16
07a51b
+	uint8_t labels[LABELS_SIZE], lmask[LABELS_SIZE];
07a51b
+	char *slash, *mask = NULL;
07a51b
+	size_t slen, slen_mask = 0;
07a51b
+
07a51b
+	slash = index(str, '/');
07a51b
+	if (slash) {
07a51b
+		*slash = 0;
07a51b
+		mask = slash + 1;
07a51b
+		slen_mask = strlen(mask);
07a51b
+	}
07a51b
+
07a51b
+	slen = strlen(str);
07a51b
+	if (slen > LABELS_SIZE * 2 || slen_mask > LABELS_SIZE * 2) {
07a51b
+		char errmsg[128];
07a51b
+
07a51b
+		snprintf(errmsg, sizeof(errmsg),
07a51b
+				"%zd Max allowed size %d",
07a51b
+				slen, LABELS_SIZE*2);
07a51b
+		invarg(errmsg, str);
07a51b
+	}
07a51b
+
07a51b
+	if (hex2mem(str, labels, slen / 2) < 0)
07a51b
+		invarg("labels must be a hex string\n", str);
07a51b
+	addattr_l(n, MAX_MSG, TCA_FLOWER_KEY_CT_LABELS, labels, slen / 2);
07a51b
+
07a51b
+	if (mask) {
07a51b
+		if (hex2mem(mask, lmask, slen_mask / 2) < 0)
07a51b
+			invarg("labels mask must be a hex string\n", mask);
07a51b
+	} else {
07a51b
+		memset(lmask, 0xff, sizeof(lmask));
07a51b
+		slen_mask = sizeof(lmask) * 2;
07a51b
+	}
07a51b
+	addattr_l(n, MAX_MSG, TCA_FLOWER_KEY_CT_LABELS_MASK, lmask,
07a51b
+		  slen_mask / 2);
07a51b
+
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
+static struct flower_ct_states {
07a51b
+	char *str;
07a51b
+	int flag;
07a51b
+} flower_ct_states[] = {
07a51b
+	{ "trk", TCA_FLOWER_KEY_CT_FLAGS_TRACKED },
07a51b
+	{ "new", TCA_FLOWER_KEY_CT_FLAGS_NEW },
07a51b
+	{ "est", TCA_FLOWER_KEY_CT_FLAGS_ESTABLISHED },
07a51b
+};
07a51b
+
07a51b
+static int flower_parse_ct_state(char *str, struct nlmsghdr *n)
07a51b
+{
07a51b
+	int flags = 0, mask = 0,  len, i;
07a51b
+	bool p;
07a51b
+
07a51b
+	while (*str != '\0') {
07a51b
+		if (*str == '+')
07a51b
+			p = true;
07a51b
+		else if (*str == '-')
07a51b
+			p = false;
07a51b
+		else
07a51b
+			return -1;
07a51b
+
07a51b
+		for (i = 0; i < ARRAY_SIZE(flower_ct_states); i++) {
07a51b
+			len = strlen(flower_ct_states[i].str);
07a51b
+			if (strncmp(str + 1, flower_ct_states[i].str, len))
07a51b
+				continue;
07a51b
+
07a51b
+			if (p)
07a51b
+				flags |= flower_ct_states[i].flag;
07a51b
+			mask |= flower_ct_states[i].flag;
07a51b
+			break;
07a51b
+		}
07a51b
+
07a51b
+		if (i == ARRAY_SIZE(flower_ct_states))
07a51b
+			return -1;
07a51b
+
07a51b
+		str += len + 1;
07a51b
+	}
07a51b
+
07a51b
+	addattr16(n, MAX_MSG, TCA_FLOWER_KEY_CT_STATE, flags);
07a51b
+	addattr16(n, MAX_MSG, TCA_FLOWER_KEY_CT_STATE_MASK, mask);
07a51b
+	return 0;
07a51b
+}
07a51b
+
07a51b
 static int flower_parse_ip_proto(char *str, __be16 eth_type, int type,
07a51b
 				 __u8 *p_ip_proto, struct nlmsghdr *n)
07a51b
 {
07a51b
@@ -1077,6 +1235,34 @@ static int flower_parse_opt(struct filter_util *qu, char *handle,
07a51b
 			flags |= TCA_CLS_FLAGS_SKIP_HW;
07a51b
 		} else if (matches(*argv, "skip_sw") == 0) {
07a51b
 			flags |= TCA_CLS_FLAGS_SKIP_SW;
07a51b
+		} else if (matches(*argv, "ct_state") == 0) {
07a51b
+			NEXT_ARG();
07a51b
+			ret = flower_parse_ct_state(*argv, n);
07a51b
+			if (ret < 0) {
07a51b
+				fprintf(stderr, "Illegal \"ct_state\"\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
+		} else if (matches(*argv, "ct_zone") == 0) {
07a51b
+			NEXT_ARG();
07a51b
+			ret = flower_parse_ct_zone(*argv, n);
07a51b
+			if (ret < 0) {
07a51b
+				fprintf(stderr, "Illegal \"ct_zone\"\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
+		} else if (matches(*argv, "ct_mark") == 0) {
07a51b
+			NEXT_ARG();
07a51b
+			ret = flower_parse_ct_mark(*argv, n);
07a51b
+			if (ret < 0) {
07a51b
+				fprintf(stderr, "Illegal \"ct_mark\"\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
+		} else if (matches(*argv, "ct_label") == 0) {
07a51b
+			NEXT_ARG();
07a51b
+			ret = flower_parse_ct_labels(*argv, n);
07a51b
+			if (ret < 0) {
07a51b
+				fprintf(stderr, "Illegal \"ct_label\"\n");
07a51b
+				return -1;
07a51b
+			}
07a51b
 		} else if (matches(*argv, "indev") == 0) {
07a51b
 			NEXT_ARG();
07a51b
 			if (check_ifname(*argv))
07a51b
@@ -1783,6 +1969,85 @@ static void flower_print_tcp_flags(const char *name, struct rtattr *flags_attr,
07a51b
 	print_string(PRINT_ANY, name, namefrm, out);
07a51b
 }
07a51b
 
07a51b
+static void flower_print_ct_state(struct rtattr *flags_attr,
07a51b
+				  struct rtattr *mask_attr)
07a51b
+{
07a51b
+	SPRINT_BUF(out);
07a51b
+	uint16_t state;
07a51b
+	uint16_t state_mask;
07a51b
+	size_t done = 0;
07a51b
+	int i;
07a51b
+
07a51b
+	if (!flags_attr)
07a51b
+		return;
07a51b
+
07a51b
+	state = rta_getattr_u16(flags_attr);
07a51b
+	if (mask_attr)
07a51b
+		state_mask = rta_getattr_u16(mask_attr);
07a51b
+	else
07a51b
+		state_mask = UINT16_MAX;
07a51b
+
07a51b
+	for (i = 0; i < ARRAY_SIZE(flower_ct_states); i++) {
07a51b
+		if (!(state_mask & flower_ct_states[i].flag))
07a51b
+			continue;
07a51b
+
07a51b
+		if (state & flower_ct_states[i].flag)
07a51b
+			done += sprintf(out + done, "+%s",
07a51b
+					flower_ct_states[i].str);
07a51b
+		else
07a51b
+			done += sprintf(out + done, "-%s",
07a51b
+					flower_ct_states[i].str);
07a51b
+	}
07a51b
+
07a51b
+	print_string(PRINT_ANY, "ct_state", "\n  ct_state %s", out);
07a51b
+}
07a51b
+
07a51b
+static void flower_print_ct_label(struct rtattr *attr,
07a51b
+				  struct rtattr *mask_attr)
07a51b
+{
07a51b
+	const unsigned char *str;
07a51b
+	bool print_mask = false;
07a51b
+	int data_len, i;
07a51b
+	SPRINT_BUF(out);
07a51b
+	char *p;
07a51b
+
07a51b
+	if (!attr)
07a51b
+		return;
07a51b
+
07a51b
+	data_len = RTA_PAYLOAD(attr);
07a51b
+	hexstring_n2a(RTA_DATA(attr), data_len, out, sizeof(out));
07a51b
+	p = out + data_len*2;
07a51b
+
07a51b
+	data_len = RTA_PAYLOAD(attr);
07a51b
+	str = RTA_DATA(mask_attr);
07a51b
+	if (data_len != 16)
07a51b
+		print_mask = true;
07a51b
+	for (i = 0; !print_mask && i < data_len; i++) {
07a51b
+		if (str[i] != 0xff)
07a51b
+			print_mask = true;
07a51b
+	}
07a51b
+	if (print_mask) {
07a51b
+		*p++ = '/';
07a51b
+		hexstring_n2a(RTA_DATA(mask_attr), data_len, p,
07a51b
+			      sizeof(out)-(p-out));
07a51b
+		p += data_len*2;
07a51b
+	}
07a51b
+	*p = '\0';
07a51b
+
07a51b
+	print_string(PRINT_ANY, "ct_label", "\n  ct_label %s", out);
07a51b
+}
07a51b
+
07a51b
+static void flower_print_ct_zone(struct rtattr *attr,
07a51b
+				 struct rtattr *mask_attr)
07a51b
+{
07a51b
+	print_masked_u16("ct_zone", attr, mask_attr);
07a51b
+}
07a51b
+
07a51b
+static void flower_print_ct_mark(struct rtattr *attr,
07a51b
+				 struct rtattr *mask_attr)
07a51b
+{
07a51b
+	print_masked_u32("ct_mark", attr, mask_attr);
07a51b
+}
07a51b
 
07a51b
 static void flower_print_key_id(const char *name, struct rtattr *attr)
07a51b
 {
07a51b
@@ -2218,6 +2483,15 @@ static int flower_print_opt(struct filter_util *qu, FILE *f,
07a51b
 				    tb[TCA_FLOWER_KEY_FLAGS],
07a51b
 				    tb[TCA_FLOWER_KEY_FLAGS_MASK]);
07a51b
 
07a51b
+	flower_print_ct_state(tb[TCA_FLOWER_KEY_CT_STATE],
07a51b
+			      tb[TCA_FLOWER_KEY_CT_STATE_MASK]);
07a51b
+	flower_print_ct_zone(tb[TCA_FLOWER_KEY_CT_ZONE],
07a51b
+			     tb[TCA_FLOWER_KEY_CT_ZONE_MASK]);
07a51b
+	flower_print_ct_mark(tb[TCA_FLOWER_KEY_CT_MARK],
07a51b
+			     tb[TCA_FLOWER_KEY_CT_MARK_MASK]);
07a51b
+	flower_print_ct_label(tb[TCA_FLOWER_KEY_CT_LABELS],
07a51b
+			      tb[TCA_FLOWER_KEY_CT_LABELS_MASK]);
07a51b
+
07a51b
 	close_json_object();
07a51b
 
07a51b
 	if (tb[TCA_FLOWER_FLAGS]) {
07a51b
-- 
07a51b
2.26.2
07a51b