Blame SOURCES/0022-JSON-Support-latest-enhancements-of-fwd-statement.patch

34625f
From 48d688ddb20d4594ee946014a661d30c82c9247d Mon Sep 17 00:00:00 2001
34625f
From: Phil Sutter <phil@nwl.cc>
34625f
Date: Fri, 8 Jun 2018 17:27:18 +0200
34625f
Subject: [PATCH] JSON: Support latest enhancements of fwd statement
34625f
34625f
JSON equivalent of fwd statement was too primitive to support the added
34625f
address and family parameters, so make its value an object and accept
34625f
the device expression as value of a "dev" property in there. Then add
34625f
optional "addr" and "family" properties to it.
34625f
34625f
While being at it, add a testcase to make sure the extended syntax works
34625f
right.
34625f
34625f
Signed-off-by: Phil Sutter <phil@nwl.cc>
34625f
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
34625f
(cherry picked from commit f63b54623fcd1ab7d2f51928571c164409f00175)
34625f
Signed-off-by: Phil Sutter <psutter@redhat.com>
34625f
---
34625f
 src/json.c                     | 13 ++++++++--
34625f
 src/parser_json.c              | 40 ++++++++++++++++++++++++++++--
34625f
 tests/py/any/fwd.t             |  1 +
34625f
 tests/py/any/fwd.t.json        | 45 ++++++++++++++++++++++------------
34625f
 tests/py/any/fwd.t.json.output | 30 ++++++++++++-----------
34625f
 tests/py/any/fwd.t.payload     |  6 +++++
34625f
 6 files changed, 102 insertions(+), 33 deletions(-)
34625f
34625f
diff --git a/src/json.c b/src/json.c
34625f
index 7d89754bd666d..eeba90e266f75 100644
34625f
--- a/src/json.c
34625f
+++ b/src/json.c
34625f
@@ -1008,9 +1008,18 @@ json_t *limit_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
34625f
 
34625f
 json_t *fwd_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
34625f
 {
34625f
-	json_t *root;
34625f
+	json_t *root, *tmp;
34625f
+
34625f
+	root = json_pack("{s:o}", "dev", expr_print_json(stmt->fwd.dev, octx));
34625f
+
34625f
+	if (stmt->fwd.addr) {
34625f
+		tmp = json_string(family2str(stmt->fwd.family));
34625f
+		json_object_set_new(root, "family", tmp);
34625f
+
34625f
+		tmp = expr_print_json(stmt->fwd.addr, octx);
34625f
+		json_object_set_new(root, "addr", tmp);
34625f
+	}
34625f
 
34625f
-	root = expr_print_json(stmt->fwd.dev, octx);
34625f
 	return json_pack("{s:o}", "fwd", root);
34625f
 }
34625f
 
34625f
diff --git a/src/parser_json.c b/src/parser_json.c
34625f
index 6e14fb7278fb0..af57b3025a104 100644
34625f
--- a/src/parser_json.c
34625f
+++ b/src/parser_json.c
34625f
@@ -1561,11 +1561,47 @@ static struct stmt *json_parse_limit_stmt(struct json_ctx *ctx,
34625f
 static struct stmt *json_parse_fwd_stmt(struct json_ctx *ctx,
34625f
 					const char *key, json_t *value)
34625f
 {
34625f
-	struct stmt *stmt = fwd_stmt_alloc(int_loc);
34625f
+	json_t *jaddr, *jdev;
34625f
+	const char *family;
34625f
+	struct stmt *stmt;
34625f
+	int familyval;
34625f
+
34625f
+	if (json_unpack_err(ctx, value, "{s:o}", "dev", &jdev))
34625f
+		return NULL;
34625f
 
34625f
-	stmt->fwd.dev = json_parse_expr(ctx, value);
34625f
+	stmt = fwd_stmt_alloc(int_loc);
34625f
+
34625f
+	stmt->fwd.dev = json_parse_stmt_expr(ctx, jdev);
34625f
+	if (!stmt->fwd.dev) {
34625f
+		json_error(ctx, "Invalid fwd dev value.");
34625f
+		goto out_err;
34625f
+	}
34625f
+
34625f
+	if (json_unpack(value, "{s:s, s:o}",
34625f
+			"family", &family, "addr", &jaddr))
34625f
+		return stmt;
34625f
+
34625f
+	familyval = parse_family(family);
34625f
+	switch (familyval) {
34625f
+	case NFPROTO_IPV4:
34625f
+	case NFPROTO_IPV6:
34625f
+		stmt->fwd.family = familyval;
34625f
+		break;
34625f
+	default:
34625f
+		json_error(ctx, "Invalid fwd family value '%s'.", family);
34625f
+		goto out_err;
34625f
+	}
34625f
+
34625f
+	stmt->fwd.addr = json_parse_stmt_expr(ctx, jaddr);
34625f
+	if (!stmt->fwd.addr) {
34625f
+		json_error(ctx, "Invalid fwd addr value.");
34625f
+		goto out_err;
34625f
+	}
34625f
 
34625f
 	return stmt;
34625f
+out_err:
34625f
+	stmt_free(stmt);
34625f
+	return NULL;
34625f
 }
34625f
 
34625f
 static struct stmt *json_parse_notrack_stmt(struct json_ctx *ctx,
34625f
diff --git a/tests/py/any/fwd.t b/tests/py/any/fwd.t
34625f
index d9b4514ee29a1..986a16d9e2c8c 100644
34625f
--- a/tests/py/any/fwd.t
34625f
+++ b/tests/py/any/fwd.t
34625f
@@ -5,3 +5,4 @@
34625f
 fwd to "lo";ok
34625f
 fwd to mark map { 0x00000001 : "lo", 0x00000002 : "lo"};ok
34625f
 
34625f
+fwd ip to 192.168.2.200 device "lo";ok
34625f
diff --git a/tests/py/any/fwd.t.json b/tests/py/any/fwd.t.json
34625f
index 644d6d48c2a19..e58a8ad25829b 100644
34625f
--- a/tests/py/any/fwd.t.json
34625f
+++ b/tests/py/any/fwd.t.json
34625f
@@ -1,7 +1,9 @@
34625f
 # fwd to "lo"
34625f
 [
34625f
     {
34625f
-        "fwd": "lo"
34625f
+        "fwd": {
34625f
+            "dev": "lo"
34625f
+	}
34625f
     }
34625f
 ]
34625f
 
34625f
@@ -9,24 +11,37 @@
34625f
 [
34625f
     {
34625f
         "fwd": {
34625f
-            "map": {
34625f
-                "left": {
34625f
-                    "meta": "mark"
34625f
-                },
34625f
-                "right": {
34625f
-                    "set": [
34625f
-                        [
34625f
-                            "0x00000001",
34625f
-                            "lo"
34625f
-                        ],
34625f
-                        [
34625f
-                            "0x00000002",
34625f
-                            "lo"
34625f
+            "dev": {
34625f
+                "map": {
34625f
+                    "left": {
34625f
+                        "meta": "mark"
34625f
+                    },
34625f
+                    "right": {
34625f
+                        "set": [
34625f
+                            [
34625f
+                                "0x00000001",
34625f
+                                "lo"
34625f
+                            ],
34625f
+                            [
34625f
+                                "0x00000002",
34625f
+                                "lo"
34625f
+                            ]
34625f
                         ]
34625f
-                    ]
34625f
+                    }
34625f
                 }
34625f
             }
34625f
         }
34625f
     }
34625f
 ]
34625f
 
34625f
+# fwd ip to 192.168.2.200 device "lo"
34625f
+[
34625f
+    {
34625f
+        "fwd": {
34625f
+            "addr": "192.168.2.200",
34625f
+            "dev": "lo",
34625f
+            "family": "ip"
34625f
+        }
34625f
+    }
34625f
+]
34625f
+
34625f
diff --git a/tests/py/any/fwd.t.json.output b/tests/py/any/fwd.t.json.output
34625f
index 5a943567adb0c..e4bad620b22d4 100644
34625f
--- a/tests/py/any/fwd.t.json.output
34625f
+++ b/tests/py/any/fwd.t.json.output
34625f
@@ -2,21 +2,23 @@
34625f
 [
34625f
     {
34625f
         "fwd": {
34625f
-            "map": {
34625f
-                "left": {
34625f
-                    "meta": "mark"
34625f
-                },
34625f
-                "right": {
34625f
-                    "set": [
34625f
-                        [
34625f
-                            1,
34625f
-                            "lo"
34625f
-                        ],
34625f
-                        [
34625f
-                            2,
34625f
-                            "lo"
34625f
+            "dev": {
34625f
+                "map": {
34625f
+                    "left": {
34625f
+                        "meta": "mark"
34625f
+                    },
34625f
+                    "right": {
34625f
+                        "set": [
34625f
+                            [
34625f
+                                1,
34625f
+                                "lo"
34625f
+                            ],
34625f
+                            [
34625f
+                                2,
34625f
+                                "lo"
34625f
+                            ]
34625f
                         ]
34625f
-                    ]
34625f
+                    }
34625f
                 }
34625f
             }
34625f
         }
34625f
diff --git a/tests/py/any/fwd.t.payload b/tests/py/any/fwd.t.payload
34625f
index 696b55efe8207..966c08b0959c3 100644
34625f
--- a/tests/py/any/fwd.t.payload
34625f
+++ b/tests/py/any/fwd.t.payload
34625f
@@ -12,3 +12,9 @@ netdev test-netdev ingress
34625f
   [ lookup reg 1 set __map%d dreg 1 ]
34625f
   [ fwd sreg_dev 1 ]
34625f
 
34625f
+# fwd ip to 192.168.2.200 device "lo"
34625f
+netdev test-netdev ingress 
34625f
+  [ immediate reg 1 0x00000001 ]
34625f
+  [ immediate reg 2 0xc802a8c0 ]
34625f
+  [ fwd sreg_dev 1 sreg_addr 2 nfproto 2 ]
34625f
+
34625f
-- 
34625f
2.19.0
34625f