]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
JSON: Support latest enhancements of fwd statement
authorPhil Sutter <phil@nwl.cc>
Fri, 8 Jun 2018 15:27:18 +0000 (17:27 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 11 Jun 2018 09:31:49 +0000 (11:31 +0200)
JSON equivalent of fwd statement was too primitive to support the added
address and family parameters, so make its value an object and accept
the device expression as value of a "dev" property in there. Then add
optional "addr" and "family" properties to it.

While being at it, add a testcase to make sure the extended syntax works
right.

Signed-off-by: Phil Sutter <phil@nwl.cc>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
src/json.c
src/parser_json.c
tests/py/any/fwd.t
tests/py/any/fwd.t.json
tests/py/any/fwd.t.json.output
tests/py/any/fwd.t.payload

index a871c934f020cfa53d64f4c52227cf8bc69a7972..c1cd0fbfa07f857c5ef76a8293d338dcab0f437b 100644 (file)
@@ -1008,9 +1008,18 @@ json_t *limit_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
 
 json_t *fwd_stmt_json(const struct stmt *stmt, struct output_ctx *octx)
 {
-       json_t *root;
+       json_t *root, *tmp;
+
+       root = json_pack("{s:o}", "dev", expr_print_json(stmt->fwd.dev, octx));
+
+       if (stmt->fwd.addr) {
+               tmp = json_string(family2str(stmt->fwd.family));
+               json_object_set_new(root, "family", tmp);
+
+               tmp = expr_print_json(stmt->fwd.addr, octx);
+               json_object_set_new(root, "addr", tmp);
+       }
 
-       root = expr_print_json(stmt->fwd.dev, octx);
        return json_pack("{s:o}", "fwd", root);
 }
 
index bc36136f825fc098589310c6af26643ae343dec0..2fd0ef832bd0e46df45d33ce4bcc8a4a1bce6dee 100644 (file)
@@ -1584,11 +1584,47 @@ static struct stmt *json_parse_limit_stmt(struct json_ctx *ctx,
 static struct stmt *json_parse_fwd_stmt(struct json_ctx *ctx,
                                        const char *key, json_t *value)
 {
-       struct stmt *stmt = fwd_stmt_alloc(int_loc);
+       json_t *jaddr, *jdev;
+       const char *family;
+       struct stmt *stmt;
+       int familyval;
+
+       if (json_unpack_err(ctx, value, "{s:o}", "dev", &jdev))
+               return NULL;
 
-       stmt->fwd.dev = json_parse_expr(ctx, value);
+       stmt = fwd_stmt_alloc(int_loc);
+
+       stmt->fwd.dev = json_parse_stmt_expr(ctx, jdev);
+       if (!stmt->fwd.dev) {
+               json_error(ctx, "Invalid fwd dev value.");
+               goto out_err;
+       }
+
+       if (json_unpack(value, "{s:s, s:o}",
+                       "family", &family, "addr", &jaddr))
+               return stmt;
+
+       familyval = parse_family(family);
+       switch (familyval) {
+       case NFPROTO_IPV4:
+       case NFPROTO_IPV6:
+               stmt->fwd.family = familyval;
+               break;
+       default:
+               json_error(ctx, "Invalid fwd family value '%s'.", family);
+               goto out_err;
+       }
+
+       stmt->fwd.addr = json_parse_stmt_expr(ctx, jaddr);
+       if (!stmt->fwd.addr) {
+               json_error(ctx, "Invalid fwd addr value.");
+               goto out_err;
+       }
 
        return stmt;
+out_err:
+       stmt_free(stmt);
+       return NULL;
 }
 
 static struct stmt *json_parse_notrack_stmt(struct json_ctx *ctx,
index d9b4514ee29a1cefa181f58e16839d95b458f25a..986a16d9e2c8c40977bcc56c5c400043b6c30811 100644 (file)
@@ -5,3 +5,4 @@
 fwd to "lo";ok
 fwd to mark map { 0x00000001 : "lo", 0x00000002 : "lo"};ok
 
+fwd ip to 192.168.2.200 device "lo";ok
index 644d6d48c2a19c228e8fef22051caf3fe2f4122c..e58a8ad25829b7d933ea0ed4bdc69220e291b30f 100644 (file)
@@ -1,7 +1,9 @@
 # fwd to "lo"
 [
     {
-        "fwd": "lo"
+        "fwd": {
+            "dev": "lo"
+       }
     }
 ]
 
 [
     {
         "fwd": {
-            "map": {
-                "left": {
-                    "meta": "mark"
-                },
-                "right": {
-                    "set": [
-                        [
-                            "0x00000001",
-                            "lo"
-                        ],
-                        [
-                            "0x00000002",
-                            "lo"
+            "dev": {
+                "map": {
+                    "left": {
+                        "meta": "mark"
+                    },
+                    "right": {
+                        "set": [
+                            [
+                                "0x00000001",
+                                "lo"
+                            ],
+                            [
+                                "0x00000002",
+                                "lo"
+                            ]
                         ]
-                    ]
+                    }
                 }
             }
         }
     }
 ]
 
+# fwd ip to 192.168.2.200 device "lo"
+[
+    {
+        "fwd": {
+            "addr": "192.168.2.200",
+            "dev": "lo",
+            "family": "ip"
+        }
+    }
+]
+
index 5a943567adb0cbb9183c99661e5115b6832adbf2..e4bad620b22d44bd5f6a2de8544df17ed112a92d 100644 (file)
@@ -2,21 +2,23 @@
 [
     {
         "fwd": {
-            "map": {
-                "left": {
-                    "meta": "mark"
-                },
-                "right": {
-                    "set": [
-                        [
-                            1,
-                            "lo"
-                        ],
-                        [
-                            2,
-                            "lo"
+            "dev": {
+                "map": {
+                    "left": {
+                        "meta": "mark"
+                    },
+                    "right": {
+                        "set": [
+                            [
+                                1,
+                                "lo"
+                            ],
+                            [
+                                2,
+                                "lo"
+                            ]
                         ]
-                    ]
+                    }
                 }
             }
         }
index 696b55efe82078ad8f479236a00b8d78189d8db9..966c08b0959c3862e4aed244c714f94cf9e4d61c 100644 (file)
@@ -12,3 +12,9 @@ netdev test-netdev ingress
   [ lookup reg 1 set __map%d dreg 1 ]
   [ fwd sreg_dev 1 ]
 
+# fwd ip to 192.168.2.200 device "lo"
+netdev test-netdev ingress 
+  [ immediate reg 1 0x00000001 ]
+  [ immediate reg 2 0xc802a8c0 ]
+  [ fwd sreg_dev 1 sreg_addr 2 nfproto 2 ]
+