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

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