Skip to contentSkip to conversation

batctl: netlink bugfixes

PATCH New Sven Eckelmann 1 participant 13 messages Jul 8

lore
Share & tools
lore
#0 lore raw

cover batctl: netlink bugfixes

I have now splitted the bugfixes for batctl in topic branches to make it a little bit easier to review. Similar to the "random bugfixes 2026-06-21" patchset, they are just various (mostly minor) problems which should be tackled at some point.

---
Sven Eckelmann (12):
batctl: handle netlink callback object on error
batctl: netlink: abort netlink_print_common when message allocation fails
batctl: netlink: don't format NULL extra_info in info_callback
batctl: netlink: report dump errors signalled in the NLMSG_DONE message
batctl: netlink: detect receive errors in netlink_print_common
batctl: netlink: report kernel errors when writing settings
batctl: netlink: report send errors in netlink_simple_request
batctl: netlink: detect receive errors in query_rtnl_link
batctl: netlink: detect receive errors in netlink_simple_request
batctl: netlink: detect receive errors in query_rtnl_link_single
batctl: netlink: report error on query_rtnl_link send failure
batctl: netlink: use kernel-style error codes for nl_recvmsgs

 functions.c       | 67 ++++++++++++++++++++++++++++++++++++++++++-------------
 netlink.c         | 27 ++++++++++++++++------
 routing_algo.c    |  1 +
 sys.c             | 10 +++++++--
 throughputmeter.c | 10 ++++++++-
 5 files changed, 90 insertions(+), 25 deletions(-)

---
base-commit: eb9597d4ca6db17c579f5ae9443c51b013e2fe65
change-id: 20260704-bugfixes-netlink-dff56d4f1c79

Best regards,

Signature · 1 line
Sven Eckelmann <[email protected]>
#1 lore raw

PATCH 1/12 batctl: handle netlink callback object on error

The libnl callback object is allocated and must be deallocated when it is no longer used. This is especially important when an error happened.

Fixes d8dd1ff1a0fe ("batctl: Use netlink to replace some of debugfs")
Fixes f109b3473f86 ("batctl: introduce throughput meter support")
---
 netlink.c         |  6 +++++-
 routing_algo.c    |  1 +
 throughputmeter.c | 10 +++++++++-
 3 files changed, 15 insertions(+), 2 deletions(-)
netlink.c +5 -1
index 46fb993..4a3ab60 100644
@@ -489,11 +489,15 @@ char *netlink_get_info(struct state *state, uint8_t nl_cmd, const char *header)
 	nl_cb_err(cb, NL_CB_CUSTOM, netlink_print_error, NULL);
 
 	ret = nl_recvmsgs(state->sock, cb);
-	if (ret < 0)
+	if (ret < 0) {
+		nl_cb_put(cb);
 		return opts.remaining_header;
+	}
 
 	nl_wait_for_ack(state->sock);
 
+	nl_cb_put(cb);
+
 	return opts.remaining_header;
 }
 
routing_algo.c +1 -0
index 1c2c2b4..167bd22 100644
@@ -107,6 +107,7 @@ static int print_routing_algos(struct state *state)
 	nl_cb_err(cb, NL_CB_CUSTOM, netlink_print_error, NULL);
 
 	nl_recvmsgs(state->sock, cb);
+	nl_cb_put(cb);
 
 	if (!last_err) {
 		netlink_print_remaining_header(&opts);
throughputmeter.c +9 -1
index 98a9490..1690bda 100644
@@ -158,13 +158,18 @@ static int tp_meter_start(struct state *state, struct ether_addr *dst_mac,
 	int err = 0;
 
 	cb = nl_cb_alloc(NL_CB_DEFAULT);
+	if (!cb)
+		return -ENOMEM;
+
 	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, tp_meter_cookie_callback,
 		  cookie);
 	nl_cb_err(cb, NL_CB_CUSTOM, tpmeter_nl_print_error, cookie);
 
 	msg = nlmsg_alloc();
-	if (!msg)
+	if (!msg) {
+		nl_cb_put(cb);
 		return -ENOMEM;
+	}
 
 	genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, state->batadv_family, 0,
 		    0, BATADV_CMD_TP_METER, 1);
@@ -200,6 +205,9 @@ static int tp_recv_result(struct nl_sock *sock, struct tp_result *result)
 	int err = 0;
 
 	cb = nl_cb_alloc(NL_CB_DEFAULT);
+	if (!cb)
+		return -ENOMEM;
+
 	nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
 	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, tp_meter_result_callback,
 		  result);
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/12 batctl: netlink: abort netlink_print_common when message allocation fails

In reply to Sven Eckelmann

When nlmsg_alloc() fails, netlink_print_common() just continues. In one-shot mode this leaves the loop with the stale last_err of 0 and the command reports success even though nothing was queried or printed. In watch mode the loop retries immediately without the usleep() pause and leaks the header that netlink_get_info() prepared in the previous iteration.

Treat the allocation failure as -ENOMEM and leave the query loop.

Fixes d8dd1ff1a0fe ("batctl: Use netlink to replace some of debugfs")
---
 netlink.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
netlink.c +4 -2
index 4a3ab60..cb65655 100644
@@ -567,8 +567,10 @@ int netlink_print_common(struct state *state, char *orig_iface, int read_opt,
 								 header);
 
 		msg = nlmsg_alloc();
-		if (!msg)
-			continue;
+		if (!msg) {
+			last_err = -ENOMEM;
+			break;
+		}
 
 		genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, state->batadv_family,
 			    0, NLM_F_DUMP, nl_cmd, 1);
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/12 batctl: netlink: don't format NULL extra_info in info_callback

In reply to Sven Eckelmann

When asprintf() or strdup() fail to create the extra_info string, info_callback() continues with extra_info set to NULL and passes it as %s argument to the following asprintf(). Passing NULL to %s is undefined behavior.

Fall back to an empty string when extra_info could not be allocated.

Fixes d8dd1ff1a0fe ("batctl: Use netlink to replace some of debugfs")
---
 netlink.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
netlink.c +2 -1
index cb65655..dd0275e 100644
@@ -440,7 +440,8 @@ static int info_callback(struct nl_msg *msg, void *arg)
 			       mesh_name,
 			       mesh_mac[0], mesh_mac[1], mesh_mac[2],
 			       mesh_mac[3], mesh_mac[4], mesh_mac[5],
-			       algo_name, extra_info, extra_header);
+			       algo_name, extra_info ? extra_info : "",
+			       extra_header);
 		if (ret < 0)
 			opts->remaining_header = NULL;
 
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/12 batctl: netlink: report dump errors signalled in the NLMSG_DONE message

In reply to Sven Eckelmann

When a dump is terminated with a non-zero error code in its NLMSG_DONE message, "Error received" is printed but it is missed to update the global error state (last_err). netlink_print_common() therefore returns 0 and the command exits EXIT_SUCCESS despite the error just printed.

Fixes 854835788001 ("batctl: Move routing_algo specific code it command source file")
---
 netlink.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
netlink.c +3 -1
index dd0275e..e8b41cf 100644
@@ -316,8 +316,10 @@ int netlink_stop_callback(struct nl_msg *msg, void *arg __maybe_unused)
 	struct nlmsghdr *nlh = nlmsg_hdr(msg);
 	int *error = nlmsg_data(nlh);
 
-	if (*error)
+	if (*error) {
 		fprintf(stderr, "Error received: %s\n", strerror(-*error));
+		last_err = *error;
+	}
 
 	return NL_STOP;
 }
Signature · 1 line
2.47.3
#5 lore raw

PATCH 5/12 batctl: netlink: detect receive errors in netlink_print_common

In reply to Sven Eckelmann

netlink_print_common() clears last_err to 0 before each dump and then ignores the return value of nl_recvmsgs(). It will therefore only print errors which were received in netlink messages - but not the major errors when communicating with the kernel.

Capture the nl_recvmsgs() return value, matching the sibling helper netlink_query_common().

Fixes d8dd1ff1a0fe ("batctl: Use netlink to replace some of debugfs")
---
 netlink.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
netlink.c +4 -1
index e8b41cf..6e0d35a 100644
@@ -537,6 +537,7 @@ int netlink_print_common(struct state *state, char *orig_iface, int read_opt,
 	};
 	int hardifindex = 0;
 	struct nl_msg *msg;
+	int ret;
 
 	if (!state->sock) {
 		last_err = -EOPNOTSUPP;
@@ -588,7 +589,9 @@ int netlink_print_common(struct state *state, char *orig_iface, int read_opt,
 		nlmsg_free(msg);
 
 		last_err = 0;
-		nl_recvmsgs(state->sock, state->cb);
+		ret = nl_recvmsgs(state->sock, state->cb);
+		if (ret < 0)
+			last_err = -EIO;
 
 		/* the header should still be printed when no entry was received */
 		if (!last_err)
Signature · 1 line
2.47.3
#6 lore raw

PATCH 6/12 batctl: netlink: report kernel errors when writing settings

In reply to Sven Eckelmann

When writing a setting, sys_simple_nlquery() consumes the kernel reply via nl_wait_for_ack(). This helper uses the socket's default callbacks instead of state->cb, so sys_simple_nlerror() never runs and its (negative) return value is discarded. A set request rejected by the kernel therefore prints no error message and exits with EXIT_SUCCESS.

This can be seen easily by calling: "batctl meshif bat0 aggregation 1" without CAP_NET_ADMIN.

Receive the reply through state->cb instead, like the read path already does. The error handler then prints the kernel error, stores it in result and the command exits with EXIT_FAILURE.

Fixes 0b81e8fbaed5 ("batctl: Consume genl ACKs after setting reads")
---
 sys.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
sys.c +8 -2
index 35a026e..a4d531f 100644
@@ -110,10 +110,16 @@ int sys_simple_nlquery(struct state *state, enum batadv_nl_commands nl_cmd,
 	if (callback) {
 		ret = nl_recvmsgs(state->sock, state->cb);
 		if (ret < 0)
-			return ret;
+			return -EIO;
+
+		nl_wait_for_ack(state->sock);
+
+		return result;
 	}
 
-	nl_wait_for_ack(state->sock);
+	ret = nl_recvmsgs(state->sock, state->cb);
+	if (ret < 0 && result >= 0)
+		result = -EIO;
 
 	return result;
 }
Signature · 1 line
2.47.3
#7 lore raw

PATCH 7/12 batctl: netlink: report send errors in netlink_simple_request

In reply to Sven Eckelmann

netlink_simple_request() initializes err to 0 and only lets ack_errno_handler() update it while receiving. When nl_send_auto_complete() fails, the function jumps to the cleanup path with err still set to 0 and reports success even though the request never reached the kernel. Callers like the interface create/destroy handling then exit without any error indication.

Set err to -EIO on the send error path to let the caller handle it as actual error.

Fixes dd2bbe182780 ("batctl: Add command to create/destroy batman-adv interface")
---
 functions.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
functions.c +3 -1
index 00dbd3d..a3f6639 100644
@@ -552,8 +552,10 @@ int netlink_simple_request(struct nl_msg *msg)
 	nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_wait_handler, NULL);
 
 	ret = nl_send_auto_complete(sock, msg);
-	if (ret < 0)
+	if (ret < 0) {
+		err = -EIO;
 		goto err_free_cb;
+	}
 
 	// ack_errno_handler sets err on errors
 	err = 0;
Signature · 1 line
2.47.3
#8 lore raw

PATCH 8/12 batctl: netlink: detect receive errors in query_rtnl_link

In reply to Sven Eckelmann

query_rtnl_link() ignores the return value of nl_recvmsgs(). Once nl_send_auto_complete() succeeds, the local err stays 0 no matter what happens while receiving the RTM_GETLINK dump, so a failed or truncated dump is reported as success.

Set err to -EIO on an receive error to let the caller handle it as actual error.

Fixes 45548f578683 ("batctl: Use rtnl to query list of softif devices")
---
 functions.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
functions.c +3 -1
index a3f6639..f3ad560 100644
@@ -496,7 +496,9 @@ int query_rtnl_link(int ifindex, nl_recvmsg_msg_cb_t func, void *arg)
 	if (ret < 0)
 		goto err_free_msg;
 
-	nl_recvmsgs(sock, cb);
+	ret = nl_recvmsgs(sock, cb);
+	if (ret < 0)
+		err = -EIO;
 
 err_free_msg:
 	nlmsg_free(msg);
Signature · 1 line
2.47.3
#9 lore raw

PATCH 9/12 batctl: netlink: detect receive errors in netlink_simple_request

In reply to Sven Eckelmann

netlink_simple_request() ignores the return value of nl_recvmsgs(). Once nl_send_auto_complete() succeeds, the local err stays 0 no matter what happens while receiving the RTM_GETLINK dump, so a failed or truncated dump is reported as success.

Set err to -EIO on an receive error to let the caller handle it as actual error.

Fixes dd2bbe182780 ("batctl: Add command to create/destroy batman-adv interface")
---
 functions.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
functions.c +3 -1
index f3ad560..36cae49 100644
@@ -561,7 +561,9 @@ int netlink_simple_request(struct nl_msg *msg)
 
 	// ack_errno_handler sets err on errors
 	err = 0;
-	nl_recvmsgs(sock, cb);
+	ret = nl_recvmsgs(sock, cb);
+	if (ret < 0)
+		err = ret;
 
 err_free_cb:
 	nl_cb_put(cb);
Signature · 1 line
2.47.3
#10 lore raw

PATCH 10/12 batctl: netlink: detect receive errors in query_rtnl_link_single

In reply to Sven Eckelmann

With the exception of nl_socket_alloc(), query_rtnl_link_single() always returns 0, no matter what happens. All callers rely solely on the *_found flags in link_data to decide what an interface is.

Return proper negative errno codes from the setup error paths, mirroring the sibling helper query_rtnl_link(). These need to be handled also by the callers.

Fixes 07967cd19702 ("batctl: Support checking of meshif without sysfs")
---
 functions.c | 51 ++++++++++++++++++++++++++++++++++++++++-----------
 1 file changed, 40 insertions(+), 11 deletions(-)
functions.c +40 -11
index 36cae49..56bacdf 100644
@@ -667,6 +667,7 @@ static int query_rtnl_link_single(int mesh_ifindex,
 	};
 	struct nl_cb *cb = NULL;
 	struct nl_sock *sock;
+	int err = 0;
 	int ret;
 
 	link_data->kind_found = false;
@@ -676,42 +677,55 @@ static int query_rtnl_link_single(int mesh_ifindex,
 
 	sock = nl_socket_alloc();
 	if (!sock)
-		return -1;
+		return -ENOMEM;
 
 	ret = nl_connect(sock, NETLINK_ROUTE);
-	if (ret < 0)
+	if (ret < 0) {
+		err = -ENOMEM;
 		goto free_sock;
+	}
 
 	ret = nl_send_simple(sock, RTM_GETLINK, NLM_F_REQUEST,
 			     &ifinfo, sizeof(ifinfo));
-	if (ret < 0)
+	if (ret < 0) {
+		err = -EIO;
 		goto free_sock;
+	}
 
 	cb = nl_cb_alloc(NL_CB_DEFAULT);
-	if (!cb)
+	if (!cb) {
+		err = -ENOMEM;
 		goto free_sock;
+	}
 
 	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, query_rtnl_link_single_parse,
 		  link_data);
-	nl_recvmsgs(sock, cb);
+
+	ret = nl_recvmsgs(sock, cb);
+	if (ret < 0)
+		err = -EIO;
 
 	nl_cb_put(cb);
 free_sock:
 	nl_socket_free(sock);
 
-	return 0;
+	return err;
 }
 
 int translate_vlan_iface(struct state *state, const char *vlandev)
 {
 	struct rtnl_link_iface_data link_data;
 	unsigned int arg_ifindex;
+	int ret;
 
 	arg_ifindex = if_nametoindex(vlandev);
 	if (arg_ifindex == 0)
 		return -ENODEV;
 
-	query_rtnl_link_single(arg_ifindex, &link_data);
+	ret = query_rtnl_link_single(arg_ifindex, &link_data);
+	if (ret < 0)
+		return ret;
+
 	if (!link_data.vid_found)
 		return -ENODEV;
 
@@ -785,12 +799,16 @@ int translate_hard_iface(struct state *state, const char *hardif)
 {
 	struct rtnl_link_iface_data link_data;
 	unsigned int arg_ifindex;
+	int ret;
 
 	arg_ifindex = if_nametoindex(hardif);
 	if (arg_ifindex == 0)
 		return -ENODEV;
 
-	query_rtnl_link_single(arg_ifindex, &link_data);
+	ret = query_rtnl_link_single(arg_ifindex, &link_data);
+	if (ret < 0)
+		return ret;
+
 	if (!link_data.master_found)
 		return -ENOLINK;
 
@@ -806,8 +824,12 @@ int translate_hard_iface(struct state *state, const char *hardif)
 static int check_mesh_iface_netlink(unsigned int ifindex)
 {
 	struct rtnl_link_iface_data link_data;
+	int ret;
+
+	ret = query_rtnl_link_single(ifindex, &link_data);
+	if (ret < 0)
+		return ret;
 
-	query_rtnl_link_single(ifindex, &link_data);
 	if (!link_data.kind_found)
 		return -1;
 
@@ -821,12 +843,15 @@ int guess_netdev_type(const char *netdev, enum selector_prefix *type)
 {
 	struct rtnl_link_iface_data link_data;
 	unsigned int netdev_ifindex;
+	int ret;
 
 	netdev_ifindex = if_nametoindex(netdev);
 	if (netdev_ifindex == 0)
 		return -ENODEV;
 
-	query_rtnl_link_single(netdev_ifindex, &link_data);
+	ret = query_rtnl_link_single(netdev_ifindex, &link_data);
+	if (ret < 0)
+		return ret;
 
 	if (link_data.kind_found && strcmp(link_data.kind, "batadv") == 0) {
 		*type = SP_MESHIF;
@@ -860,12 +885,16 @@ int check_mesh_iface_ownership(struct state *state, char *hard_iface)
 {
 	struct rtnl_link_iface_data link_data;
 	unsigned int hardif_index;
+	int ret;
 
 	hardif_index = if_nametoindex(hard_iface);
 	if (hardif_index == 0)
 		return EXIT_FAILURE;
 
-	query_rtnl_link_single(hardif_index, &link_data);
+	ret = query_rtnl_link_single(hardif_index, &link_data);
+	if (ret < 0)
+		return EXIT_FAILURE;
+
 	if (!link_data.master_found)
 		return EXIT_FAILURE;
 
Signature · 1 line
2.47.3
#11 lore raw

PATCH 11/12 batctl: netlink: report error on query_rtnl_link send failure

In reply to Sven Eckelmann

When nl_send_auto_complete() fails, an explicit error must be set or otherwise query_rtnl_link() will return 0 (no error).

Fixes 60e519bfeaa3 ("batctl: Use rtnl to query list of softif devices")
---
 functions.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
functions.c +3 -1
index 56bacdf..213ba83 100644
@@ -493,8 +493,10 @@ int query_rtnl_link(int ifindex, nl_recvmsg_msg_cb_t func, void *arg)
 	}
 
 	ret = nl_send_auto_complete(sock, msg);
-	if (ret < 0)
+	if (ret < 0) {
+		err = -EIO;
 		goto err_free_msg;
+	}
 
 	ret = nl_recvmsgs(sock, cb);
 	if (ret < 0)
Signature · 1 line
2.47.3
#12 lore raw

PATCH 12/12 batctl: netlink: use kernel-style error codes for nl_recvmsgs

In reply to Sven Eckelmann

When nl_recvmsgs() fails, it doesn't return a kernel/errno style error codes. It is instead using negative NLE_* return codes. Since the rest of the batctl code uses kernel style codes, just handle it as EIO error.

---
 functions.c | 2 +-
 netlink.c   | 3 ++-
 2 files changed, 3 insertions(+), 2 deletions(-)
functions.c +1 -1
index 213ba83..ba5ef83 100644
@@ -565,7 +565,7 @@ int netlink_simple_request(struct nl_msg *msg)
 	err = 0;
 	ret = nl_recvmsgs(sock, cb);
 	if (ret < 0)
-		err = ret;
+		err = -EIO;
 
 err_free_cb:
 	nl_cb_put(cb);
netlink.c +2 -1
index 6e0d35a..fb6fdae 100644
@@ -677,7 +677,8 @@ int netlink_query_common(struct state *state,
 
 	ret = nl_recvmsgs(state->sock, cb);
 	if (ret < 0) {
-		query_opts->err = ret;
+		if (query_opts->err == 0)
+			query_opts->err = -EIO;
 		goto err_free_cb;
 	}
 
Signature · 1 line
2.47.3