]> git.ipfire.org Git - thirdparty/nftables.git/commitdiff
src: mnl: prepare for listing all device netdev device hooks
authorFlorian Westphal <fw@strlen.de>
Tue, 20 Aug 2024 22:12:26 +0000 (00:12 +0200)
committerPablo Neira Ayuso <pablo@netfilter.org>
Mon, 1 Sep 2025 20:47:56 +0000 (22:47 +0200)
commit b8872b83eb365fcc921f2c59ac3ea055ca22c7e7 upstream.

Change output foramt slightly so device name is included for netdev
family.

% nft list hooks netdev device eth0
family netdev {
        hook ingress device eth0 {
                 0000000000 chain inet ingress in_public [nf_tables]
                 0000000000 chain netdev ingress in_public [nf_tables]
        }
        hook egress device eth0 {
                 0000000000 chain netdev ingress out_public [nf_tables]
        }
}

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

index 776057fb1e2b095ee92a9ba7c29f3292a85fdbd5..311c920defc5e14fbb4dbaec04bb611c9d820f1e 100644 (file)
--- a/src/mnl.c
+++ b/src/mnl.c
@@ -44,6 +44,7 @@ struct basehook {
        const char *hookfn;
        const char *table;
        const char *chain;
+       const char *devname;
        int family;
        int chain_family;
        uint32_t num;
@@ -2133,9 +2134,24 @@ static void basehook_free(struct basehook *b)
        xfree(b->hookfn);
        xfree(b->chain);
        xfree(b->table);
+       xfree(b->devname);
        xfree(b);
 }
 
+static bool basehook_eq(const struct basehook *prev, const struct basehook *hook)
+{
+       if (prev->num != hook->num)
+               return false;
+
+       if (prev->devname != NULL && hook->devname != NULL)
+               return strcmp(prev->devname, hook->devname) == 0;
+
+       if (prev->devname == NULL && prev->devname == NULL)
+               return true;
+
+       return false;
+}
+
 static void basehook_list_add_tail(struct basehook *b, struct list_head *head)
 {
        struct basehook *hook;
@@ -2243,6 +2259,7 @@ static int dump_nf_attr_chain_cb(const struct nlattr *attr, void *data)
 
 struct dump_nf_hook_data {
        struct list_head *hook_list;
+       const char *devname;
        int family;
 };
 
@@ -2264,6 +2281,7 @@ static int dump_nf_hooks(const struct nlmsghdr *nlh, void *_data)
 
        hook = basehook_alloc();
        hook->prio = ntohl(mnl_attr_get_u32(tb[NFNLA_HOOK_PRIORITY]));
+       hook->devname = data->devname ? xstrdup(data->devname) : NULL;
 
        if (tb[NFNLA_HOOK_FUNCTION_NAME])
                hook->hookfn = xstrdup(mnl_attr_get_str(tb[NFNLA_HOOK_FUNCTION_NAME]));
@@ -2336,6 +2354,7 @@ static int __mnl_nft_dump_nf_hooks(struct netlink_ctx *ctx, uint8_t query_family
        char buf[MNL_SOCKET_BUFFER_SIZE];
        struct dump_nf_hook_data data = {
                .hook_list      = hook_list,
+               .devname        = devname,
                .family         = query_family,
        };
        struct nlmsghdr *nlh;
@@ -2375,7 +2394,7 @@ static void print_hooks(struct netlink_ctx *ctx, int family, struct list_head *h
                        continue;
 
                if (prev) {
-                       if (prev->num == hook->num) {
+                       if (basehook_eq(prev, hook)) {
                                fprintf(fp, "\n");
                                same = true;
                        } else {
@@ -2388,8 +2407,12 @@ static void print_hooks(struct netlink_ctx *ctx, int family, struct list_head *h
                prev = hook;
 
                if (!same) {
-                       fprintf(fp, "\thook %s {\n",
-                               hooknum2str(family, hook->num));
+                       if (hook->devname)
+                               fprintf(fp, "\thook %s device %s {\n",
+                                       hooknum2str(family, hook->num), hook->devname);
+                       else
+                               fprintf(fp, "\thook %s {\n",
+                                       hooknum2str(family, hook->num));
                }
 
                prio = hook->prio;