]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
json: catchall element support
authorFlorian Westphal <fw@strlen.de>
Wed, 2 Jun 2021 18:38:46 +0000 (20:38 +0200)
committerFlorian Westphal <fw@strlen.de>
Wed, 2 Jun 2021 21:46:11 +0000 (23:46 +0200)
Treat '*' as catchall element, not as a symbol.
Also add missing json test cases for wildcard set support.

Signed-off-by: Florian Westphal <fw@strlen.de>
include/json.h
src/expression.c
src/json.c
src/parser_json.c
tests/py/ip/sets.t.json

index dd594bd03e1c54bfae717c721ff3c3cc540b400c..015e3ac780f819927d7306cd2d356ca2281f0e5f 100644 (file)
@@ -37,6 +37,7 @@ json_t *concat_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *set_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *set_ref_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *set_elem_expr_json(const struct expr *expr, struct output_ctx *octx);
+json_t *set_elem_catchall_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *prefix_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *list_expr_json(const struct expr *expr, struct output_ctx *octx);
 json_t *unary_expr_json(const struct expr *expr, struct output_ctx *octx);
index c91333631ad03a010edea6d21414c49ce039f195..c6be000107f26da2431ede514eb71736929816d6 100644 (file)
@@ -1341,6 +1341,7 @@ static const struct expr_ops set_elem_catchall_expr_ops = {
        .type           = EXPR_SET_ELEM_CATCHALL,
        .name           = "catch-all set element",
        .print          = set_elem_catchall_expr_print,
+       .json           = set_elem_catchall_expr_json,
 };
 
 struct expr *set_elem_catchall_expr_alloc(const struct location *loc)
index e588ef4c1722a03711a30a2a7d3339e32cdc8059..b08c004ae9c94e71ba4ecce1a5da3e6b24c90605 100644 (file)
@@ -889,6 +889,11 @@ static json_t *symbolic_constant_json(const struct symbol_table *tbl,
                return json_string(s->identifier);
 }
 
+json_t *set_elem_catchall_expr_json(const struct expr *expr, struct output_ctx *octx)
+{
+       return json_string("*");
+}
+
 static json_t *datatype_json(const struct expr *expr, struct output_ctx *octx)
 {
        const struct datatype *dtype = expr->dtype;
index 2e791807cce61b51bd59f0f437450050f1473520..e6a0233ab6ce347acab7e895865a3a67a74392e9 100644 (file)
@@ -315,15 +315,6 @@ static struct expr *json_parse_constant(struct json_ctx *ctx, const char *name)
        return NULL;
 }
 
-static struct expr *wildcard_expr_alloc(void)
-{
-       struct expr *expr;
-
-       expr = constant_expr_alloc(int_loc, &integer_type,
-                                  BYTEORDER_HOST_ENDIAN, 0, NULL);
-       return prefix_expr_alloc(int_loc, expr, 0);
-}
-
 /* this is a combination of symbol_expr, integer_expr, boolean_expr ... */
 static struct expr *json_parse_immediate(struct json_ctx *ctx, json_t *root)
 {
@@ -338,7 +329,7 @@ static struct expr *json_parse_immediate(struct json_ctx *ctx, json_t *root)
                        symtype = SYMBOL_SET;
                        str++;
                } else if (str[0] == '*' && str[1] == '\0') {
-                       return wildcard_expr_alloc();
+                       return set_elem_catchall_expr_alloc(int_loc);
                } else if (is_keyword(str)) {
                        return symbol_expr_alloc(int_loc,
                                                 SYMBOL_VALUE, NULL, str);
index 65d2df873623b0a39e4f571c36081a8c4055d6e5..d24b3918dc6de32a9387f8ed126fb044edbe85f9 100644 (file)
     }
 ]
 
+# ip saddr @set6 drop
+[
+    {
+        "match": {
+            "left": {
+                "payload": {
+                    "field": "saddr",
+                    "protocol": "ip"
+                }
+            },
+            "op": "==",
+            "right": "@set6"
+        }
+    },
+    {
+        "drop": null
+    }
+]
+
+# ip saddr vmap { 1.1.1.1 : drop, * : accept }
+[
+    {
+        "vmap": {
+            "data": {
+                "set": [
+                    [
+                        "1.1.1.1",
+                        {
+                            "drop": null
+                        }
+                    ],
+                    [
+                        "*",
+                        {
+                            "accept": null
+                        }
+                    ]
+                ]
+            },
+            "key": {
+                "payload": {
+                    "field": "saddr",
+                    "protocol": "ip"
+                }
+            }
+        }
+    }
+]
+
+# meta mark set ip saddr map { 1.1.1.1 : 0x00000001, * : 0x00000002 }
+[
+    {
+        "mangle": {
+            "key": {
+                "meta": {
+                    "key": "mark"
+                }
+            },
+            "value": {
+                "map": {
+                    "data": {
+                        "set": [
+                            [
+                                "1.1.1.1",
+                                1
+                            ],
+                            [
+                                "*",
+                                2
+                            ]
+                        ]
+                    },
+                    "key": {
+                        "payload": {
+                            "field": "saddr",
+                            "protocol": "ip"
+                        }
+                    }
+                }
+            }
+        }
+    }
+]
+