Blame SOURCES/0053-tcpopt-allow-to-check-for-presence-of-any-tcp-option.patch

4e0227
From 8a4b6cbf58e965d67b0337ba1736bd3691a49890 Mon Sep 17 00:00:00 2001
4e0227
From: Phil Sutter <psutter@redhat.com>
4e0227
Date: Mon, 12 Jul 2021 17:44:08 +0200
4e0227
Subject: [PATCH] tcpopt: allow to check for presence of any tcp option
4e0227
4e0227
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1979334
4e0227
Upstream Status: nftables commit 24d8da3083422
4e0227
4e0227
commit 24d8da3083422da8336eeed2ee23b2ccf598ba5a
4e0227
Author: Florian Westphal <fw@strlen.de>
4e0227
Date:   Wed Oct 21 23:54:17 2020 +0200
4e0227
4e0227
    tcpopt: allow to check for presence of any tcp option
4e0227
4e0227
    nft currently doesn't allow to check for presence of arbitrary tcp options.
4e0227
    Only known options where nft provides a template can be tested for.
4e0227
4e0227
    This allows to test for presence of raw protocol values as well.
4e0227
4e0227
    Example:
4e0227
4e0227
    tcp option 42 exists
4e0227
4e0227
    Signed-off-by: Florian Westphal <fw@strlen.de>
4e0227
---
4e0227
 include/expression.h          |  3 +-
4e0227
 src/exthdr.c                  | 12 ++++++++
4e0227
 src/ipopt.c                   |  1 +
4e0227
 src/netlink_linearize.c       |  2 +-
4e0227
 src/parser_bison.y            |  7 +++++
4e0227
 src/tcpopt.c                  | 42 +++++++++++++++++++++++----
4e0227
 tests/py/any/tcpopt.t         |  2 ++
4e0227
 tests/py/any/tcpopt.t.payload | 53 +++--------------------------------
4e0227
 8 files changed, 65 insertions(+), 57 deletions(-)
4e0227
4e0227
diff --git a/include/expression.h b/include/expression.h
4e0227
index 2e41aa0..b50183d 100644
4e0227
--- a/include/expression.h
4e0227
+++ b/include/expression.h
4e0227
@@ -299,7 +299,8 @@ struct expr {
4e0227
 			/* EXPR_EXTHDR */
4e0227
 			const struct exthdr_desc	*desc;
4e0227
 			const struct proto_hdr_template	*tmpl;
4e0227
-			unsigned int			offset;
4e0227
+			uint16_t			offset;
4e0227
+			uint8_t				raw_type;
4e0227
 			enum nft_exthdr_op		op;
4e0227
 			unsigned int			flags;
4e0227
 		} exthdr;
4e0227
diff --git a/src/exthdr.c b/src/exthdr.c
4e0227
index c28213f..68d5aa5 100644
4e0227
--- a/src/exthdr.c
4e0227
+++ b/src/exthdr.c
4e0227
@@ -32,6 +32,13 @@ static void exthdr_expr_print(const struct expr *expr, struct output_ctx *octx)
4e0227
 		 */
4e0227
 		unsigned int offset = expr->exthdr.offset / 64;
4e0227
 
4e0227
+		if (expr->exthdr.desc == NULL &&
4e0227
+		    expr->exthdr.offset == 0 &&
4e0227
+		    expr->exthdr.flags & NFT_EXTHDR_F_PRESENT) {
4e0227
+			nft_print(octx, "tcp option %d", expr->exthdr.raw_type);
4e0227
+			return;
4e0227
+		}
4e0227
+
4e0227
 		nft_print(octx, "tcp option %s", expr->exthdr.desc->name);
4e0227
 		if (expr->exthdr.flags & NFT_EXTHDR_F_PRESENT)
4e0227
 			return;
4e0227
@@ -59,6 +66,7 @@ static bool exthdr_expr_cmp(const struct expr *e1, const struct expr *e2)
4e0227
 	return e1->exthdr.desc == e2->exthdr.desc &&
4e0227
 	       e1->exthdr.tmpl == e2->exthdr.tmpl &&
4e0227
 	       e1->exthdr.op == e2->exthdr.op &&
4e0227
+	       e1->exthdr.raw_type == e2->exthdr.raw_type &&
4e0227
 	       e1->exthdr.flags == e2->exthdr.flags;
4e0227
 }
4e0227
 
4e0227
@@ -69,6 +77,7 @@ static void exthdr_expr_clone(struct expr *new, const struct expr *expr)
4e0227
 	new->exthdr.offset = expr->exthdr.offset;
4e0227
 	new->exthdr.op = expr->exthdr.op;
4e0227
 	new->exthdr.flags = expr->exthdr.flags;
4e0227
+	new->exthdr.raw_type = expr->exthdr.raw_type;
4e0227
 }
4e0227
 
4e0227
 const struct expr_ops exthdr_expr_ops = {
4e0227
@@ -98,6 +107,7 @@ struct expr *exthdr_expr_alloc(const struct location *loc,
4e0227
 	expr = expr_alloc(loc, EXPR_EXTHDR, tmpl->dtype,
4e0227
 			  BYTEORDER_BIG_ENDIAN, tmpl->len);
4e0227
 	expr->exthdr.desc = desc;
4e0227
+	expr->exthdr.raw_type = desc ? desc->type : 0;
4e0227
 	expr->exthdr.tmpl = tmpl;
4e0227
 	expr->exthdr.offset = tmpl->offset;
4e0227
 	return expr;
4e0227
@@ -176,6 +186,8 @@ void exthdr_init_raw(struct expr *expr, uint8_t type,
4e0227
 	unsigned int i;
4e0227
 
4e0227
 	assert(expr->etype == EXPR_EXTHDR);
4e0227
+	expr->exthdr.raw_type = type;
4e0227
+
4e0227
 	if (op == NFT_EXTHDR_OP_TCPOPT)
4e0227
 		return tcpopt_init_raw(expr, type, offset, len, flags);
4e0227
 	if (op == NFT_EXTHDR_OP_IPV4)
4e0227
diff --git a/src/ipopt.c b/src/ipopt.c
4e0227
index 7ecb8b9..5f9f908 100644
4e0227
--- a/src/ipopt.c
4e0227
+++ b/src/ipopt.c
4e0227
@@ -103,6 +103,7 @@ struct expr *ipopt_expr_alloc(const struct location *loc, uint8_t type,
4e0227
 	expr->exthdr.tmpl   = tmpl;
4e0227
 	expr->exthdr.op     = NFT_EXTHDR_OP_IPV4;
4e0227
 	expr->exthdr.offset = tmpl->offset + calc_offset(desc, tmpl, ptr);
4e0227
+	expr->exthdr.raw_type = desc->type;
4e0227
 
4e0227
 	return expr;
4e0227
 }
4e0227
diff --git a/src/netlink_linearize.c b/src/netlink_linearize.c
4e0227
index 9d1a064..28b0e6a 100644
4e0227
--- a/src/netlink_linearize.c
4e0227
+++ b/src/netlink_linearize.c
4e0227
@@ -174,7 +174,7 @@ static void netlink_gen_exthdr(struct netlink_linearize_ctx *ctx,
4e0227
 	nle = alloc_nft_expr("exthdr");
4e0227
 	netlink_put_register(nle, NFTNL_EXPR_EXTHDR_DREG, dreg);
4e0227
 	nftnl_expr_set_u8(nle, NFTNL_EXPR_EXTHDR_TYPE,
4e0227
-			  expr->exthdr.desc->type);
4e0227
+			  expr->exthdr.raw_type);
4e0227
 	nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_OFFSET, offset / BITS_PER_BYTE);
4e0227
 	nftnl_expr_set_u32(nle, NFTNL_EXPR_EXTHDR_LEN,
4e0227
 			   div_round_up(expr->len, BITS_PER_BYTE));
4e0227
diff --git a/src/parser_bison.y b/src/parser_bison.y
4e0227
index 114b289..4ea9364 100644
4e0227
--- a/src/parser_bison.y
4e0227
+++ b/src/parser_bison.y
4e0227
@@ -4744,6 +4744,13 @@ tcp_hdr_option_type	:	EOL		{ $$ = TCPOPT_KIND_EOL; }
4e0227
 			|	SACK3		{ $$ = TCPOPT_KIND_SACK3; }
4e0227
 			|	ECHO		{ $$ = TCPOPT_KIND_ECHO; }
4e0227
 			|	TIMESTAMP	{ $$ = TCPOPT_KIND_TIMESTAMP; }
4e0227
+			|	NUM		{
4e0227
+				if ($1 > 255) {
4e0227
+					erec_queue(error(&@1, "value too large"), state->msgs);
4e0227
+					YYERROR;
4e0227
+				}
4e0227
+				$$ = $1;
4e0227
+			}
4e0227
 			;
4e0227
 
4e0227
 tcp_hdr_option_field	:	KIND		{ $$ = TCPOPT_COMMON_KIND; }
4e0227
diff --git a/src/tcpopt.c b/src/tcpopt.c
4e0227
index d1dd13b..1cf97a5 100644
4e0227
--- a/src/tcpopt.c
4e0227
+++ b/src/tcpopt.c
4e0227
@@ -103,6 +103,19 @@ const struct exthdr_desc *tcpopt_protocols[] = {
4e0227
 	[TCPOPT_KIND_TIMESTAMP]		= &tcpopt_timestamp,
4e0227
 };
4e0227
 
4e0227
+/**
4e0227
+ * tcpopt_expr_alloc - allocate tcp option extension expression
4e0227
+ *
4e0227
+ * @loc: location from parser
4e0227
+ * @kind: raw tcp option value to find in packet
4e0227
+ * @field: highlevel field to find in the option if @kind is present in packet
4e0227
+ *
4e0227
+ * Allocate a new tcp option expression.
4e0227
+ * @kind is the raw option value to find in the packet.
4e0227
+ * Exception: SACK may use extra OOB data that is mangled here.
4e0227
+ *
4e0227
+ * @field is the optional field to extract from the @type option.
4e0227
+ */
4e0227
 struct expr *tcpopt_expr_alloc(const struct location *loc,
4e0227
 			       unsigned int kind,
4e0227
 			       unsigned int field)
4e0227
@@ -138,8 +151,22 @@ struct expr *tcpopt_expr_alloc(const struct location *loc,
4e0227
 	if (kind < array_size(tcpopt_protocols))
4e0227
 		desc = tcpopt_protocols[kind];
4e0227
 
4e0227
-	if (!desc)
4e0227
-		return NULL;
4e0227
+	if (!desc) {
4e0227
+		if (field != TCPOPT_COMMON_KIND || kind > 255)
4e0227
+			return NULL;
4e0227
+
4e0227
+		expr = expr_alloc(loc, EXPR_EXTHDR, &integer_type,
4e0227
+				  BYTEORDER_BIG_ENDIAN, 8);
4e0227
+
4e0227
+		desc = tcpopt_protocols[TCPOPT_NOP];
4e0227
+		tmpl = &desc->templates[field];
4e0227
+		expr->exthdr.desc   = desc;
4e0227
+		expr->exthdr.tmpl   = tmpl;
4e0227
+		expr->exthdr.op = NFT_EXTHDR_OP_TCPOPT;
4e0227
+		expr->exthdr.raw_type = kind;
4e0227
+		return expr;
4e0227
+	}
4e0227
+
4e0227
 	tmpl = &desc->templates[field];
4e0227
 	if (!tmpl)
4e0227
 		return NULL;
4e0227
@@ -149,6 +176,7 @@ struct expr *tcpopt_expr_alloc(const struct location *loc,
4e0227
 	expr->exthdr.desc   = desc;
4e0227
 	expr->exthdr.tmpl   = tmpl;
4e0227
 	expr->exthdr.op     = NFT_EXTHDR_OP_TCPOPT;
4e0227
+	expr->exthdr.raw_type = desc->type;
4e0227
 	expr->exthdr.offset = tmpl->offset;
4e0227
 
4e0227
 	return expr;
4e0227
@@ -165,6 +193,10 @@ void tcpopt_init_raw(struct expr *expr, uint8_t type, unsigned int off,
4e0227
 	expr->len = len;
4e0227
 	expr->exthdr.flags = flags;
4e0227
 	expr->exthdr.offset = off;
4e0227
+	expr->exthdr.op = NFT_EXTHDR_OP_TCPOPT;
4e0227
+
4e0227
+	if (flags & NFT_EXTHDR_F_PRESENT)
4e0227
+		datatype_set(expr, &boolean_type);
4e0227
 
4e0227
 	if (type >= array_size(tcpopt_protocols))
4e0227
 		return;
4e0227
@@ -178,12 +210,10 @@ void tcpopt_init_raw(struct expr *expr, uint8_t type, unsigned int off,
4e0227
 		if (tmpl->offset != off || tmpl->len != len)
4e0227
 			continue;
4e0227
 
4e0227
-		if (flags & NFT_EXTHDR_F_PRESENT)
4e0227
-			datatype_set(expr, &boolean_type);
4e0227
-		else
4e0227
+		if ((flags & NFT_EXTHDR_F_PRESENT) == 0)
4e0227
 			datatype_set(expr, tmpl->dtype);
4e0227
+
4e0227
 		expr->exthdr.tmpl = tmpl;
4e0227
-		expr->exthdr.op   = NFT_EXTHDR_OP_TCPOPT;
4e0227
 		break;
4e0227
 	}
4e0227
 }
4e0227
diff --git a/tests/py/any/tcpopt.t b/tests/py/any/tcpopt.t
4e0227
index 1d42de8..7b17014 100644
4e0227
--- a/tests/py/any/tcpopt.t
4e0227
+++ b/tests/py/any/tcpopt.t
4e0227
@@ -30,6 +30,7 @@ tcp option timestamp kind 1;ok
4e0227
 tcp option timestamp length 1;ok
4e0227
 tcp option timestamp tsval 1;ok
4e0227
 tcp option timestamp tsecr 1;ok
4e0227
+tcp option 255 missing;ok
4e0227
 
4e0227
 tcp option foobar;fail
4e0227
 tcp option foo bar;fail
4e0227
@@ -38,6 +39,7 @@ tcp option eol left 1;fail
4e0227
 tcp option eol left 1;fail
4e0227
 tcp option sack window;fail
4e0227
 tcp option sack window 1;fail
4e0227
+tcp option 256 exists;fail
4e0227
 
4e0227
 tcp option window exists;ok
4e0227
 tcp option window missing;ok
4e0227
diff --git a/tests/py/any/tcpopt.t.payload b/tests/py/any/tcpopt.t.payload
4e0227
index 9c480c8..34f8e26 100644
4e0227
--- a/tests/py/any/tcpopt.t.payload
4e0227
+++ b/tests/py/any/tcpopt.t.payload
4e0227
@@ -509,20 +509,6 @@ inet
4e0227
   [ exthdr load tcpopt 4b @ 8 + 2 => reg 1 ]
4e0227
   [ cmp eq reg 1 0x01000000 ]
4e0227
 
4e0227
-# tcp option timestamp tsecr 1
4e0227
-ip 
4e0227
-  [ meta load l4proto => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x00000006 ]
4e0227
-  [ exthdr load tcpopt 4b @ 8 + 6 => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x01000000 ]
4e0227
-
4e0227
-# tcp option timestamp tsecr 1
4e0227
-ip6 
4e0227
-  [ meta load l4proto => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x00000006 ]
4e0227
-  [ exthdr load tcpopt 4b @ 8 + 6 => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x01000000 ]
4e0227
-
4e0227
 # tcp option timestamp tsecr 1
4e0227
 inet 
4e0227
   [ meta load l4proto => reg 1 ]
4e0227
@@ -530,19 +516,12 @@ inet
4e0227
   [ exthdr load tcpopt 4b @ 8 + 6 => reg 1 ]
4e0227
   [ cmp eq reg 1 0x01000000 ]
4e0227
 
4e0227
-# tcp option window exists
4e0227
-ip 
4e0227
-  [ meta load l4proto => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x00000006 ]
4e0227
-  [ exthdr load tcpopt 1b @ 3 + 0 present => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x00000001 ]
4e0227
-
4e0227
-# tcp option window exists
4e0227
-ip6 
4e0227
+# tcp option 255 missing
4e0227
+inet
4e0227
   [ meta load l4proto => reg 1 ]
4e0227
   [ cmp eq reg 1 0x00000006 ]
4e0227
-  [ exthdr load tcpopt 1b @ 3 + 0 present => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x00000001 ]
4e0227
+  [ exthdr load tcpopt 1b @ 255 + 0 present => reg 1 ]
4e0227
+  [ cmp eq reg 1 0x00000000 ]
4e0227
 
4e0227
 # tcp option window exists
4e0227
 inet 
4e0227
@@ -551,20 +530,6 @@ inet
4e0227
   [ exthdr load tcpopt 1b @ 3 + 0 present => reg 1 ]
4e0227
   [ cmp eq reg 1 0x00000001 ]
4e0227
 
4e0227
-# tcp option window missing
4e0227
-ip 
4e0227
-  [ meta load l4proto => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x00000006 ]
4e0227
-  [ exthdr load tcpopt 1b @ 3 + 0 present => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x00000000 ]
4e0227
-
4e0227
-# tcp option window missing
4e0227
-ip6 
4e0227
-  [ meta load l4proto => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x00000006 ]
4e0227
-  [ exthdr load tcpopt 1b @ 3 + 0 present => reg 1 ]
4e0227
-  [ cmp eq reg 1 0x00000000 ]
4e0227
-
4e0227
 # tcp option window missing
4e0227
 inet 
4e0227
   [ meta load l4proto => reg 1 ]
4e0227
@@ -572,16 +537,6 @@ inet
4e0227
   [ exthdr load tcpopt 1b @ 3 + 0 present => reg 1 ]
4e0227
   [ cmp eq reg 1 0x00000000 ]
4e0227
 
4e0227
-# tcp option maxseg size set 1360
4e0227
-ip 
4e0227
-  [ immediate reg 1 0x00005005 ]
4e0227
-  [ exthdr write tcpopt reg 1 => 2b @ 2 + 2 ]
4e0227
-
4e0227
-# tcp option maxseg size set 1360
4e0227
-ip6 
4e0227
-  [ immediate reg 1 0x00005005 ]
4e0227
-  [ exthdr write tcpopt reg 1 => 2b @ 2 + 2 ]
4e0227
-
4e0227
 # tcp option maxseg size set 1360
4e0227
 inet 
4e0227
   [ immediate reg 1 0x00005005 ]
4e0227
-- 
4e0227
2.31.1
4e0227