]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
nftables: dump raw element info from libnftnl when netlink debugging is on
authorFlorian Westphal <fw@strlen.de>
Thu, 20 Aug 2020 15:23:57 +0000 (17:23 +0200)
committerFlorian Westphal <fw@strlen.de>
Thu, 20 Aug 2020 19:00:21 +0000 (21:00 +0200)
Example: nft --debug=netlink list ruleset
inet firewall @knock_candidates_ipv4
        element 0100007f 00007b00  : 0 [end]
        element 0200007f 0000f1ff  : 0 [end]
        element 0100007f 00007a00  : 0 [end]
inet firewall @__set0
        element 00000100  : 0 [end]
        element 00000200  : 0 [end]
inet firewall knock-input 3
  [ meta load l4proto => reg 1 ]
  ...

Signed-off-by: Florian Westphal <fw@strlen.de>
src/netlink.c

index 20b3cdf5e46928d1cbe3042a54f46d94e1e463fb..77e0d41e2f0722909504d36f8bf22705729a7ae2 100644 (file)
@@ -1194,6 +1194,42 @@ static int list_setelem_cb(struct nftnl_set_elem *nlse, void *arg)
        return netlink_delinearize_setelem(nlse, ctx->set, &ctx->nft->cache);
 }
 
+static int list_setelem_debug_cb(struct nftnl_set_elem *nlse, void *arg)
+{
+       int r;
+
+       r = list_setelem_cb(nlse, arg);
+       if (r == 0) {
+               struct netlink_ctx *ctx = arg;
+               FILE *fp = ctx->nft->output.output_fp;
+
+               fprintf(fp, "\t");
+               nftnl_set_elem_fprintf(fp, nlse, 0, 0);
+               fprintf(fp, "\n");
+       }
+
+       return r;
+}
+
+static int list_setelements(struct nftnl_set *s, struct netlink_ctx *ctx)
+{
+       FILE *fp = ctx->nft->output.output_fp;
+
+       if (fp && (ctx->nft->debug_mask & NFT_DEBUG_NETLINK)) {
+               const char *table, *name;
+               uint32_t family = nftnl_set_get_u32(s, NFTNL_SET_FAMILY);
+
+               table = nftnl_set_get_str(s, NFTNL_SET_TABLE);
+               name = nftnl_set_get_str(s, NFTNL_SET_NAME);
+
+               fprintf(fp, "%s %s @%s\n", family2str(family), table, name);
+
+               return nftnl_set_elem_foreach(s, list_setelem_debug_cb, ctx);
+       }
+
+       return nftnl_set_elem_foreach(s, list_setelem_cb, ctx);
+}
+
 int netlink_list_setelems(struct netlink_ctx *ctx, const struct handle *h,
                          struct set *set)
 {
@@ -1221,7 +1257,7 @@ int netlink_list_setelems(struct netlink_ctx *ctx, const struct handle *h,
 
        ctx->set = set;
        set->init = set_expr_alloc(&internal_location, set);
-       nftnl_set_elem_foreach(nls, list_setelem_cb, ctx);
+       list_setelements(nls, ctx);
 
        if (set->flags & NFT_SET_INTERVAL && set->desc.field_count > 1)
                concat_range_aggregate(set->init);
@@ -1265,7 +1301,7 @@ int netlink_get_setelem(struct netlink_ctx *ctx, const struct handle *h,
 
        ctx->set = set;
        set->init = set_expr_alloc(loc, set);
-       nftnl_set_elem_foreach(nls_out, list_setelem_cb, ctx);
+       list_setelements(nls_out, ctx);
 
        if (set->flags & NFT_SET_INTERVAL && set->desc.field_count > 1)
                concat_range_aggregate(set->init);