summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Westphal <fw@strlen.de>2024-08-21 00:12:26 +0200
committerPablo Neira Ayuso <pablo@netfilter.org>2025-09-01 22:47:56 +0200
commitd66b9d9c887c17b3d7233cf72b17489867e1e11f (patch)
tree0f30250b497f484f7ee5612a8ca28f4984c06f08
parentbac8a1395581178570ce9ea9952d386a250b4c39 (diff)
src: mnl: prepare for listing all device netdev device hooks
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>
-rw-r--r--src/mnl.c29
1 files changed, 26 insertions, 3 deletions
diff --git a/src/mnl.c b/src/mnl.c
index 776057fb..311c920d 100644
--- 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;