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

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