Blame SOURCES/0055-json-tcp-add-raw-tcp-option-match-support.patch

0341a5
From ad566e27398e81ed803c4225179bb8df4718a2e9 Mon Sep 17 00:00:00 2001
0341a5
From: Phil Sutter <psutter@redhat.com>
0341a5
Date: Mon, 12 Jul 2021 17:44:08 +0200
0341a5
Subject: [PATCH] json: tcp: add raw tcp option match support
0341a5
0341a5
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1979334
0341a5
Upstream Status: nftables commit cb21869649208
0341a5
0341a5
commit cb21869649208118ed61354e2674858e4ff6c23c
0341a5
Author: Florian Westphal <fw@strlen.de>
0341a5
Date:   Tue Nov 3 12:04:20 2020 +0100
0341a5
0341a5
    json: tcp: add raw tcp option match support
0341a5
0341a5
    To similar change as in previous one, this time for the
0341a5
    jason (de)serialization.
0341a5
0341a5
    Re-uses the raw payload match syntax, i.e. base,offset,length.
0341a5
0341a5
    Signed-off-by: Florian Westphal <fw@strlen.de>
0341a5
---
0341a5
 src/json.c                 | 22 ++++++++--------
0341a5
 src/parser_json.c          | 52 ++++++++++++++++++++++++++------------
0341a5
 tests/py/any/tcpopt.t.json | 34 +++++++++++++++++++++++++
0341a5
 3 files changed, 82 insertions(+), 26 deletions(-)
0341a5
0341a5
diff --git a/src/json.c b/src/json.c
0341a5
index 1906e7d..b77c6d2 100644
0341a5
--- a/src/json.c
0341a5
+++ b/src/json.c
0341a5
@@ -656,30 +656,32 @@ json_t *map_expr_json(const struct expr *expr, struct output_ctx *octx)
0341a5
 json_t *exthdr_expr_json(const struct expr *expr, struct output_ctx *octx)
0341a5
 {
0341a5
 	const char *desc = expr->exthdr.desc ?
0341a5
-			   expr->exthdr.desc->name :
0341a5
-			   "unknown-exthdr";
0341a5
+			   expr->exthdr.desc->name : NULL;
0341a5
 	const char *field = expr->exthdr.tmpl->token;
0341a5
 	json_t *root;
0341a5
 	bool is_exists = expr->exthdr.flags & NFT_EXTHDR_F_PRESENT;
0341a5
 
0341a5
 	if (expr->exthdr.op == NFT_EXTHDR_OP_TCPOPT) {
0341a5
+		static const char *offstrs[] = { "", "1", "2", "3" };
0341a5
 		unsigned int offset = expr->exthdr.offset / 64;
0341a5
+		const char *offstr = "";
0341a5
 
0341a5
-		if (offset) {
0341a5
-			const char *offstrs[] = { "0", "1", "2", "3" };
0341a5
-			const char *offstr = "";
0341a5
-
0341a5
+		if (desc) {
0341a5
 			if (offset < 4)
0341a5
 				offstr = offstrs[offset];
0341a5
 
0341a5
 			root = json_pack("{s:s+}", "name", desc, offstr);
0341a5
+
0341a5
+			if (!is_exists)
0341a5
+				json_object_set_new(root, "field", json_string(field));
0341a5
 		} else {
0341a5
-			root = json_pack("{s:s}", "name", desc);
0341a5
+			root = json_pack("{s:i, s:i, s:i}",
0341a5
+					 "base", expr->exthdr.raw_type,
0341a5
+					 "offset", expr->exthdr.offset,
0341a5
+					 "len", expr->len);
0341a5
+			is_exists = false;
0341a5
 		}
0341a5
 
0341a5
-		if (!is_exists)
0341a5
-			json_object_set_new(root, "field", json_string(field));
0341a5
-
0341a5
 		return json_pack("{s:o}", "tcp option", root);
0341a5
 	}
0341a5
 	if (expr->exthdr.op == NFT_EXTHDR_OP_IPV4) {
0341a5
diff --git a/src/parser_json.c b/src/parser_json.c
0341a5
index ab2375f..fbf7db5 100644
0341a5
--- a/src/parser_json.c
0341a5
+++ b/src/parser_json.c
0341a5
@@ -500,6 +500,8 @@ static int json_parse_tcp_option_field(int type, const char *name, int *val)
0341a5
 		return 1;
0341a5
 
0341a5
 	desc = tcpopt_protocols[type];
0341a5
+	if (!desc)
0341a5
+		return 1;
0341a5
 
0341a5
 	for (i = 0; i < array_size(desc->templates); i++) {
0341a5
 		if (desc->templates[i].token &&
0341a5
@@ -599,30 +601,48 @@ static struct expr *json_parse_payload_expr(struct json_ctx *ctx,
0341a5
 static struct expr *json_parse_tcp_option_expr(struct json_ctx *ctx,
0341a5
 					       const char *type, json_t *root)
0341a5
 {
0341a5
+	int fieldval, kind, offset, len;
0341a5
 	const char *desc, *field;
0341a5
-	int descval, fieldval;
0341a5
 	struct expr *expr;
0341a5
 
0341a5
-	if (json_unpack_err(ctx, root, "{s:s}", "name", &desc))
0341a5
-		return NULL;
0341a5
-
0341a5
-	if (json_parse_tcp_option_type(desc, &descval)) {
0341a5
-		json_error(ctx, "Unknown tcp option name '%s'.", desc);
0341a5
-		return NULL;
0341a5
-	}
0341a5
+	if (!json_unpack(root, "{s:i, s:i, s:i}",
0341a5
+			"base", &kind, "offset", &offset, "len", &len)) {
0341a5
+		uint32_t flag = 0;
0341a5
 
0341a5
-	if (json_unpack(root, "{s:s}", "field", &field)) {
0341a5
-		expr = tcpopt_expr_alloc(int_loc, descval,
0341a5
+		expr = tcpopt_expr_alloc(int_loc, kind,
0341a5
 					 TCPOPT_COMMON_KIND);
0341a5
-		expr->exthdr.flags = NFT_EXTHDR_F_PRESENT;
0341a5
 
0341a5
+		if (kind < 0 || kind > 255)
0341a5
+			return NULL;
0341a5
+
0341a5
+		if (offset == TCPOPT_COMMON_KIND && len == 8)
0341a5
+			flag = NFT_EXTHDR_F_PRESENT;
0341a5
+
0341a5
+		tcpopt_init_raw(expr, kind, offset, len, flag);
0341a5
 		return expr;
0341a5
+	} else if (!json_unpack(root, "{s:s}", "name", &desc)) {
0341a5
+		if (json_parse_tcp_option_type(desc, &kind)) {
0341a5
+			json_error(ctx, "Unknown tcp option name '%s'.", desc);
0341a5
+			return NULL;
0341a5
+		}
0341a5
+
0341a5
+		if (json_unpack(root, "{s:s}", "field", &field)) {
0341a5
+			expr = tcpopt_expr_alloc(int_loc, kind,
0341a5
+						 TCPOPT_COMMON_KIND);
0341a5
+			expr->exthdr.flags = NFT_EXTHDR_F_PRESENT;
0341a5
+			return expr;
0341a5
+		}
0341a5
+
0341a5
+		if (json_parse_tcp_option_field(kind, field, &fieldval)) {
0341a5
+			json_error(ctx, "Unknown tcp option field '%s'.", field);
0341a5
+			return NULL;
0341a5
+		}
0341a5
+
0341a5
+		return tcpopt_expr_alloc(int_loc, kind, fieldval);
0341a5
 	}
0341a5
-	if (json_parse_tcp_option_field(descval, field, &fieldval)) {
0341a5
-		json_error(ctx, "Unknown tcp option field '%s'.", field);
0341a5
-		return NULL;
0341a5
-	}
0341a5
-	return tcpopt_expr_alloc(int_loc, descval, fieldval);
0341a5
+
0341a5
+	json_error(ctx, "Invalid tcp option expression properties.");
0341a5
+	return NULL;
0341a5
 }
0341a5
 
0341a5
 static int json_parse_ip_option_type(const char *name, int *val)
0341a5
diff --git a/tests/py/any/tcpopt.t.json b/tests/py/any/tcpopt.t.json
0341a5
index b15e36e..139e97d 100644
0341a5
--- a/tests/py/any/tcpopt.t.json
0341a5
+++ b/tests/py/any/tcpopt.t.json
0341a5
@@ -414,6 +414,40 @@
0341a5
     }
0341a5
 ]
0341a5
 
0341a5
+# tcp option 255 missing
0341a5
+[
0341a5
+    {
0341a5
+        "match": {
0341a5
+            "left": {
0341a5
+                "tcp option": {
0341a5
+                    "base": 255,
0341a5
+                    "len": 8,
0341a5
+                    "offset": 0
0341a5
+                }
0341a5
+            },
0341a5
+            "op": "==",
0341a5
+            "right": false
0341a5
+        }
0341a5
+    }
0341a5
+]
0341a5
+
0341a5
+# tcp option @255,8,8 255
0341a5
+[
0341a5
+    {
0341a5
+        "match": {
0341a5
+            "left": {
0341a5
+                "tcp option": {
0341a5
+                    "base": 255,
0341a5
+                    "len": 8,
0341a5
+                    "offset": 8
0341a5
+                }
0341a5
+            },
0341a5
+            "op": "==",
0341a5
+            "right": 255
0341a5
+        }
0341a5
+    }
0341a5
+]
0341a5
+
0341a5
 # tcp option window exists
0341a5
 [
0341a5
     {
0341a5
-- 
0341a5
2.31.1
0341a5