summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2018-06-08 17:27:18 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2018-06-11 11:31:49 +0200
commitf63b54623fcd1ab7d2f51928571c164409f00175 (patch)
tree49acf0abdbeb4552f66a720fb5ca76a8ce9930d2 /src
parent78f8d8127eac64abb14e1d4a4309b353ba03bdb6 (diff)
JSON: Support latest enhancements of fwd statement
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>
Diffstat (limited to 'src')
-rw-r--r--src/json.c13
-rw-r--r--src/parser_json.c40
2 files changed, 49 insertions, 4 deletions
diff --git a/src/json.c b/src/json.c
index a871c934..c1cd0fbf 100644
--- a/src/json.c
+++ b/src/json.c
@@ -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);
}
diff --git a/src/parser_json.c b/src/parser_json.c
index bc36136f..2fd0ef83 100644
--- a/src/parser_json.c
+++ b/src/parser_json.c
@@ -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,