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

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