Skip to contentSkip to conversation

utils: Add helpers for interface name wildcards

PATCH Superseded Phil Sutter 3 participants 10 messages Jul 15, 2025 – Jul 16, 2025 netfilter

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Phil Sutter

author reviewer
#0 lore raw

PATCH v2 1/1 utils: Add helpers for interface name wildcards

Support simple (suffix) wildcards in NFTNL_CHAIN_DEV(ICES) and NFTA_FLOWTABLE_HOOK_DEVS identified by non-NUL-terminated strings. Add helpers converting to and from the human-readable asterisk-suffix notation.

---
No changes since v1 apart from a rebase onto current HEAD. This single patch supersedes the previous series "Support wildcard netdev hooks and events" as without NEWDEV/DELDEV kernel notifications, this is the only change needed by libnftnl to support ifname-based hooks.

---
 include/utils.h |  4 ++++
 src/chain.c     |  8 +++++---
 src/flowtable.c |  2 +-
 src/str_array.c |  2 +-
 src/utils.c     | 30 ++++++++++++++++++++++++++++++
 5 files changed, 41 insertions(+), 5 deletions(-)
include/utils.h +4 -0
index 247d99d19dd7f..c8e890eae2ffd 100644
@@ -6,6 +6,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <stdlib.h>
+#include <libmnl/libmnl.h>
 #include <libnftnl/common.h>
 
 #include "config.h"
@@ -83,4 +84,7 @@ int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type,
 int nftnl_set_str_attr(const char **dptr, uint32_t *flags,
 		       uint16_t attr, const void *data, uint32_t data_len);
 
+void mnl_attr_put_ifname(struct nlmsghdr *nlh, int attr, const char *ifname);
+const char *mnl_attr_get_ifname(struct nlattr *attr);
+
 #endif
src/chain.c +5 -3
index 895108cddad51..17ae5568609c4 100644
@@ -457,14 +457,14 @@ void nftnl_chain_nlmsg_build_payload(struct nlmsghdr *nlh, const struct nftnl_ch
 		mnl_attr_put_u32(nlh, NFTA_HOOK_PRIORITY, htonl(c->prio));
 
 	if (c->flags & (1 << NFTNL_CHAIN_DEV))
-		mnl_attr_put_strz(nlh, NFTA_HOOK_DEV, c->dev);
+		mnl_attr_put_ifname(nlh, NFTA_HOOK_DEV, c->dev);
 	else if (c->flags & (1 << NFTNL_CHAIN_DEVICES)) {
 		struct nlattr *nest_dev;
 		const char *dev;
 
 		nest_dev = mnl_attr_nest_start(nlh, NFTA_HOOK_DEVS);
 		nftnl_str_array_foreach(dev, &c->dev_array, i)
-			mnl_attr_put_strz(nlh, NFTA_DEVICE_NAME, dev);
+			mnl_attr_put_ifname(nlh, NFTA_DEVICE_NAME, dev);
 		mnl_attr_nest_end(nlh, nest_dev);
 	}
 
@@ -648,7 +648,9 @@ static int nftnl_chain_parse_hook(struct nlattr *attr, struct nftnl_chain *c)
 		c->flags |= (1 << NFTNL_CHAIN_PRIO);
 	}
 	if (tb[NFTA_HOOK_DEV]) {
-		c->dev = strdup(mnl_attr_get_str(tb[NFTA_HOOK_DEV]));
+		if (c->flags & (1 << NFTNL_CHAIN_DEV))
+			xfree(c->dev);
+		c->dev = strdup(mnl_attr_get_ifname(tb[NFTA_HOOK_DEV]));
 		if (!c->dev)
 			return -1;
 		c->flags |= (1 << NFTNL_CHAIN_DEV);
src/flowtable.c +1 -1
index fbbe0a866368d..75e86fb6d1e55 100644
@@ -299,7 +299,7 @@ void nftnl_flowtable_nlmsg_build_payload(struct nlmsghdr *nlh,
 
 		nest_dev = mnl_attr_nest_start(nlh, NFTA_FLOWTABLE_HOOK_DEVS);
 		nftnl_str_array_foreach(dev, &c->dev_array, i)
-			mnl_attr_put_strz(nlh, NFTA_DEVICE_NAME, dev);
+			mnl_attr_put_ifname(nlh, NFTA_DEVICE_NAME, dev);
 		mnl_attr_nest_end(nlh, nest_dev);
 	}
 
src/str_array.c +1 -1
index 5669c6154d394..bb75e04cb20da 100644
@@ -56,7 +56,7 @@ int nftnl_parse_devs(struct nftnl_str_array *sa, const struct nlattr *nest)
 		return -1;
 
 	mnl_attr_for_each_nested(attr, nest) {
-		sa->array[sa->len] = strdup(mnl_attr_get_str(attr));
+		sa->array[sa->len] = strdup(mnl_attr_get_ifname(attr));
 		if (!sa->array[sa->len]) {
 			nftnl_str_array_clear(sa);
 			return -1;
src/utils.c +30 -0
index 5f2c5bff7c650..bcace0cd72788 100644
@@ -13,8 +13,11 @@
 #include <errno.h>
 #include <inttypes.h>
 
+#include <libmnl/libmnl.h>
+
 #include <libnftnl/common.h>
 
+#include <linux/if.h>
 #include <linux/netfilter.h>
 #include <linux/netfilter/nf_tables.h>
 
@@ -146,3 +149,30 @@ int nftnl_set_str_attr(const char **dptr, uint32_t *flags,
 	*flags |= (1 << attr);
 	return 0;
 }
+
+void mnl_attr_put_ifname(struct nlmsghdr *nlh, int attr, const char *ifname)
+{
+	int len = strlen(ifname) + 1;
+
+	if (ifname[len - 2] == '*')
+		len -= 2;
+
+	mnl_attr_put(nlh, attr, len, ifname);
+}
+
+const char *mnl_attr_get_ifname(struct nlattr *attr)
+{
+	size_t slen = mnl_attr_get_payload_len(attr);
+	const char *dev = mnl_attr_get_str(attr);
+	static char buf[IFNAMSIZ];
+
+	if (dev[slen - 1] == '\0')
+		return dev;
+
+	if (slen > IFNAMSIZ - 2)
+		slen = IFNAMSIZ - 2;
+
+	memcpy(buf, dev, slen);
+	memcpy(buf + slen, "*\0", 2);
+	return buf;
+}
Signature · 1 line
2.49.0
maintainer
#1 lore raw
Quoted 3 lines · Phil Sutter

#include <string.h>
#include <stdlib.h>
+#include <libmnl/libmnl.h>

Why is this include needed?

include/utils.h int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type, · line 89

Quoted 10 lines

#include <libnftnl/common.h>

#include "config.h"

include/utils.h +3 -0
@@ -83,4 +84,7 @@ int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type,
 int nftnl_set_str_attr(const char **dptr, uint32_t *flags,
 		       uint16_t attr, const void *data, uint32_t data_len);
 
+void mnl_attr_put_ifname(struct nlmsghdr *nlh, int attr, const char *ifname);
+const char *mnl_attr_get_ifname(struct nlattr *attr);
+

nftnl_attr_put_ifname, nftnl_attr_get_ifname?
Using mnl_ prefix seems wrong, that should be reserved for libmnl.

author reviewer
#2 lore raw

Hi,

Quoted 6 lines · Florian Westphal
Quoted 3 lines · Phil Sutter

#include <string.h>
#include <stdlib.h>
+#include <libmnl/libmnl.h>

Why is this include needed?

Because of:

| In file included from udata.c:9:
| ../include/utils.h:88:40: warning: 'struct nlattr' declared inside parameter list will not be visible outside of this definition or declaration
|    88 | const char *mnl_attr_get_ifname(struct nlattr *attr);
|       |                                        ^~~~~~
Quoted 7 lines
Quoted 7 lines

#include <libnftnl/common.h>

#include "config.h"

include/utils.h +0 -0
@@ -83,4 +84,7 @@ int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type,
 int nftnl_set_str_attr(const char **dptr, uint32_t *flags,
 		       uint16_t attr, const void *data, uint32_t data_len);
 
Quoted 6 lines

include/utils.h int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type, · line 89

Quoted 3 lines
include/utils.h +3 -0
+void mnl_attr_put_ifname(struct nlmsghdr *nlh, int attr, const char *ifname);
+const char *mnl_attr_get_ifname(struct nlattr *attr);
+

nftnl_attr_put_ifname, nftnl_attr_get_ifname?
Using mnl_ prefix seems wrong, that should be reserved for libmnl.

I chose the prefix since they 1:1 replace calls to mnl_attr_put_strz() and mnl_attr_get_str(). You're right, since they are implemented by libnftnl the prefix is wrong. I was a bit undecided whether to put them into libmnl, but they are a bit too specific for that. Will fix the prefix and respin!

Thanks, Phil

maintainer
#3 lore raw
Quoted 6 lines · Phil Sutter
Quoted 5 lines · Florian Westphal
Quoted 3 lines · Phil Sutter

#include <string.h>
#include <stdlib.h>
+#include <libmnl/libmnl.h>

Quoted 8 lines
Quoted 1 line

Why is this include needed?

Because of:

| In file included from udata.c:9:
| ../include/utils.h:88:40: warning: 'struct nlattr' declared inside parameter list will not be visible outside of this definition or declaration
|    88 | const char *mnl_attr_get_ifname(struct nlattr *attr);
|       |                                        ^~~~~~

That struct is defined in linux/netlink.h.

But you can add a foward declaration for this, i.e.:

struct nlattr;

As the layout isn't needed for the function declaration.

author reviewer
#4 lore raw
Quoted 16 lines · Florian Westphal
Quoted 6 lines · Phil Sutter
Quoted 5 lines · Florian Westphal
Quoted 3 lines · Phil Sutter

#include <string.h>
#include <stdlib.h>
+#include <libmnl/libmnl.h>

Quoted 8 lines
Quoted 1 line

Why is this include needed?

Because of:

| In file included from udata.c:9:
| ../include/utils.h:88:40: warning: 'struct nlattr' declared inside parameter list will not be visible outside of this definition or declaration
|    88 | const char *mnl_attr_get_ifname(struct nlattr *attr);
|       |                                        ^~~~~~
Quoted 7 lines

That struct is defined in linux/netlink.h.

But you can add a foward declaration for this, i.e.:

struct nlattr;

As the layout isn't needed for the function declaration.

ACK, will do!

#5 lore raw
In reply to Phil Sutter
Quoted 8 lines · Phil Sutter

Hi,

Quoted 5 lines · Florian Westphal
Quoted 3 lines · Phil Sutter

#include <string.h>
#include <stdlib.h>
+#include <libmnl/libmnl.h>

Quoted 8 lines
Quoted 1 line

Why is this include needed?

Because of:

| In file included from udata.c:9:
| ../include/utils.h:88:40: warning: 'struct nlattr' declared inside parameter list will not be visible outside of this definition or declaration
|    88 | const char *mnl_attr_get_ifname(struct nlattr *attr);
|       |                                        ^~~~~~

I think this helper belongs to src/netlink.c

Quoted 15 lines
Quoted 8 lines
Quoted 8 lines

#include <libnftnl/common.h>

#include "config.h"

include/utils.h +0 -0
@@ -83,4 +84,7 @@ int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type,
 int nftnl_set_str_attr(const char **dptr, uint32_t *flags,
 		       uint16_t attr, const void *data, uint32_t data_len);
 
Quoted 6 lines

include/utils.h int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type, · line 89

Quoted 3 lines
include/utils.h +3 -0
+void mnl_attr_put_ifname(struct nlmsghdr *nlh, int attr, const char *ifname);
+const char *mnl_attr_get_ifname(struct nlattr *attr);
+

nftnl_attr_put_ifname, nftnl_attr_get_ifname?
Using mnl_ prefix seems wrong, that should be reserved for libmnl.

Quoted 4 lines

I chose the prefix since they 1:1 replace calls to mnl_attr_put_strz() and mnl_attr_get_str(). You're right, since they are implemented by libnftnl the prefix is wrong. I was a bit undecided whether to put them into libmnl, but they are a bit too specific for that. Will fix the prefix and respin!

I think they are fine in nftables only.

#6 lore raw
Quoted 11 lines · Pablo Neira Ayuso
Quoted 10 lines · Phil Sutter

Hi,

Quoted 6 lines · Florian Westphal
Quoted 3 lines · Phil Sutter

#include <string.h>
#include <stdlib.h>
+#include <libmnl/libmnl.h>

Why is this include needed?

Quoted 8 lines
Quoted 6 lines

Because of:

| In file included from udata.c:9:
| ../include/utils.h:88:40: warning: 'struct nlattr' declared inside parameter list will not be visible outside of this definition or declaration
|    88 | const char *mnl_attr_get_ifname(struct nlattr *attr);
|       |                                        ^~~~~~

I think this helper belongs to src/netlink.c

Not a deal breaker, Florian's proposal is also fine.

#7 lore raw
Quoted 14 lines · Pablo Neira Ayuso
Quoted 13 lines · Pablo Neira Ayuso
Quoted 12 lines · Phil Sutter

Hi,

Quoted 6 lines · Florian Westphal
Quoted 3 lines · Phil Sutter

#include <string.h>
#include <stdlib.h>
+#include <libmnl/libmnl.h>

Why is this include needed?

Because of:

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
| In file included from udata.c:9:
| ../include/utils.h:88:40: warning: 'struct nlattr' declared inside parameter list will not be visible outside of this definition or declaration
|    88 | const char *mnl_attr_get_ifname(struct nlattr *attr);
|       |                                        ^~~~~~

I think this helper belongs to src/netlink.c

Not a deal breaker, Florian's proposal is also fine.

Actually, my suggestion would be to add it to nftables/src/mnl.c with mnl_nft_ prefix (not to src/netlink.c), if it turns out this is useful to other projects, it could be moved there to libnftnl.

author reviewer
#8 lore raw
Quoted 13 lines · Pablo Neira Ayuso
Quoted 12 lines · Pablo Neira Ayuso
Quoted 11 lines · Pablo Neira Ayuso
Quoted 10 lines · Phil Sutter

Hi,

Quoted 6 lines · Florian Westphal
Quoted 3 lines · Phil Sutter

#include <string.h>
#include <stdlib.h>
+#include <libmnl/libmnl.h>

Why is this include needed?

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 6 lines

Because of:

| In file included from udata.c:9:
| ../include/utils.h:88:40: warning: 'struct nlattr' declared inside parameter list will not be visible outside of this definition or declaration
|    88 | const char *mnl_attr_get_ifname(struct nlattr *attr);
|       |                                        ^~~~~~

I think this helper belongs to src/netlink.c

This was about libnftnl, I put the helpers into src/utils.c there with nftnl_ prefix.

Quoted 5 lines
Quoted 1 line

Not a deal breaker, Florian's proposal is also fine.

Actually, my suggestion would be to add it to nftables/src/mnl.c with mnl_nft_ prefix (not to src/netlink.c), if it turns out this is useful to other projects, it could be moved there to libnftnl.

I also added it to nftables' src/mnl.c with mnl_ prefix, will change that to mnl_nft_ as suggested.

Just realized nft.8 needs an update, too. Luckily, documentation is pretty forgiving when it comes to typos and other goofs. ;)

Cheers, Phil

#9 lore raw
In reply to Florian Westphal
Quoted 14 lines · Florian Westphal
Quoted 3 lines · Phil Sutter

#include <string.h>
#include <stdlib.h>
+#include <libmnl/libmnl.h>

Why is this include needed?

Quoted 7 lines

#include <libnftnl/common.h>

#include "config.h"

include/utils.h +0 -0
@@ -83,4 +84,7 @@ int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type,
 int nftnl_set_str_attr(const char **dptr, uint32_t *flags,
 		       uint16_t attr, const void *data, uint32_t data_len);
 
Quoted 6 lines

include/utils.h int nftnl_fprintf(FILE *fpconst, const void *obj, uint32_t cmd, uint32_t type, · line 89

Quoted 3 lines
include/utils.h +3 -0
+void mnl_attr_put_ifname(struct nlmsghdr *nlh, int attr, const char *ifname);
+const char *mnl_attr_get_ifname(struct nlattr *attr);
+

nftnl_attr_put_ifname, nftnl_attr_get_ifname?
Using mnl_ prefix seems wrong, that should be reserved for libmnl.

Agreed, mnl_nft_put_ifname, I would suggest, for consistency.