Skip to contentSkip to conversation

batctl: traceroute/ping bugfixes

PATCH New Sven Eckelmann 1 participant 12 messages Jul 7

lore
Share & tools
lore
#0 lore raw

cover batctl: traceroute/ping 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 (11):
batctl: traceroute: return EXIT_NOSUCCESS when destination not reached
batctl: icmp_helper: return proper errno on syscall failures
batctl: ping: count sent and not received pings
batctl: ping/traceroute: don't restart RTT timer on stray replies
batctl: traceroute: probe the advertised maximum number of hops
batctl: ping: keep huge '-i' intervals from turning into a flood ping
batctl: ping: reject invalid packet count argument
batctl: ping: reject invalid timeout argument
batctl: ping: fix rtt minimum tracking when a sample is 0.0
batctl: icmp_helper: fail send when the primary mac is unknown
batctl: icmp_helper: attach socket filter before packets can arrive

 icmp_helper.c | 47 ++++++++++++++++++++++++++++++-----------------
 ping.c        | 44 +++++++++++++++++++++++++++++++++-----------
 traceroute.c  |  9 ++++++---
 3 files changed, 69 insertions(+), 31 deletions(-)

---
base-commit: eb9597d4ca6db17c579f5ae9443c51b013e2fe65
change-id: 20260704-bugfixes-icmp-851d34f9821d

Best regards,

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

PATCH 1/11 batctl: traceroute: return EXIT_NOSUCCESS when destination not reached

traceroute sets ret to EXIT_SUCCESS unconditionally once the probe loop finishes. dst_reached is only set on an actual echo reply, so when the target never answers within TTL_MAX hops, it is unreachable or all probes time out. But this information is ignored when traceroute exits. A script will not be able to distinguish a failed and working traceroute.

Use the same error codes as the ping but make it depend on the dst_reached variable.

Fixes e1c83d9260e8 ("[batctl] traceroute utility updated to latest batman adv")
---
 traceroute.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
traceroute.c +4 -1
index 441cd77..94443d7 100644
@@ -212,7 +212,10 @@ static int traceroute(struct state *state, int argc, char **argv)
 		printf("\n");
 	}
 
-	ret = EXIT_SUCCESS;
+	if (dst_reached)
+		ret = EXIT_SUCCESS;
+	else
+		ret = EXIT_NOSUCCESS;
 
 out:
 	icmp_interfaces_clean();
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/11 batctl: icmp_helper: return proper errno on syscall failures

In reply to Sven Eckelmann

icmp_interface_send(), icmp_interface_read() and icmp_interface_filter() return the raw syscall results. On failure this is -1 while the callers interpret negative values as negative errno codes: ping and traceroute print strerror(-res) and thus always report "Operation not permitted" no matter why the send, receive or filter setup actually failed (for example ENETDOWN when the interface goes down mid-ping or ENOMEM when attaching the socket filter). The other error paths of these functions already return proper negative errno values.

Convert the syscall failures to -errno before returning them.

---
 icmp_helper.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)
icmp_helper.c +14 -6
index 15bae9a..65a2005 100644
@@ -154,7 +154,7 @@ static int icmp_interface_filter(int sock, int uid)
 
 	if (setsockopt(sock, SOL_SOCKET, SO_ATTACH_FILTER, &filter,
 		       sizeof(filter)))
-		return -1;
+		return -errno;
 
 	return 0;
 }
@@ -348,6 +348,7 @@ static int icmp_interface_send(struct batadv_icmp_header *icmp_packet,
 {
 	struct ether_header header;
 	struct iovec vector[2];
+	ssize_t ret;
 
 	header.ether_type = htons(ETH_P_BATMAN);
 	memcpy(header.ether_shost, iface->mac, ETH_ALEN);
@@ -358,7 +359,11 @@ static int icmp_interface_send(struct batadv_icmp_header *icmp_packet,
 	vector[1].iov_base = icmp_packet;
 	vector[1].iov_len  = packet_len;
 
-	return (int)writev(iface->sock, vector, 2);
+	ret = writev(iface->sock, vector, 2);
+	if (ret < 0)
+		return -errno;
+
+	return (int)ret;
 }
 
 int icmp_interface_write(struct state *state,
@@ -494,9 +499,12 @@ ssize_t icmp_interface_read(struct batadv_icmp_header *icmp_packet, size_t len,
 	max_sock = icmp_interface_preselect(&read_sockets);
 
 	res = select(max_sock, &read_sockets, NULL, NULL, tv);
-	/* timeout, or < 0 error */
-	if (res <= 0)
-		return res;
+	if (res < 0)
+		return -errno;
+
+	/* timeout */
+	if (res == 0)
+		return 0;
 
 	read_sock = icmp_interface_get_read_sock(&read_sockets, &iface);
 	if (read_sock < 0)
@@ -509,7 +517,7 @@ ssize_t icmp_interface_read(struct batadv_icmp_header *icmp_packet, size_t len,
 
 	read_len = readv(read_sock, vector, 2);
 	if (read_len < 0)
-		return read_len;
+		return -errno;
 
 	if (read_len < ETH_HLEN)
 		goto retry;
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/11 batctl: ping: count sent and not received pings

In reply to Sven Eckelmann

packets_out was increased whenever a read of a packet was started. But the read was repeated whenever the sequence number of a received packet was incorrect. The packets_out would then be higher then the actual sent ones.

Fixes 2474249a6312 ("[batctl] ping utility updated to latest batman adv")
Fixes 2ecb2c8b060b ("batctl: tr / ping - ignore packets with wrong sequence number")
---
 ping.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
ping.c +2 -2
index 9eb73c1..5eafdad 100644
@@ -213,6 +213,8 @@ static int ping(struct state *state, int argc, char **argv)
 			goto sleep;
 		}
 
+		packets_out++;
+
 read_packet:
 		start_timer();
 
@@ -222,8 +224,6 @@ static int ping(struct state *state, int argc, char **argv)
 		if (is_aborted)
 			break;
 
-		packets_out++;
-
 		if (read_len == 0) {
 			printf("Reply from host %s timed out\n", dst_string);
 			goto sleep;
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/11 batctl: ping/traceroute: don't restart RTT timer on stray replies

In reply to Sven Eckelmann

When a reply with an unexpected seqno arrives (for example a late answer to an already timed out request), ping and traceroute jump back to read_packet to keep waiting for the real answer. The jump target also re-runs start_timer(), so the reported round trip time only measures from the arrival of the stray packet instead of from the transmission of the request, underreporting the RTT and polluting the min/avg/max statistics.

Start the timer (and initialize the timeout in traceroute) once per transmitted request. At the same time, let select in icmp_interface_read() the wait time instead of restarting it each time.

Fixes 2ecb2c8b060b ("batctl: tr / ping - ignore packets with wrong sequence number")
---
 ping.c       | 2 +-
 traceroute.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
ping.c +1 -1
index 5eafdad..fcb8d5e 100644
@@ -215,9 +215,9 @@ static int ping(struct state *state, int argc, char **argv)
 
 		packets_out++;
 
-read_packet:
 		start_timer();
 
+read_packet:
 		read_len = icmp_interface_read((struct batadv_icmp_header *)&icmp_packet_in,
 					       packet_len, &tv);
 
traceroute.c +1 -1
index 94443d7..975c9ff 100644
@@ -142,12 +142,12 @@ static int traceroute(struct state *state, int argc, char **argv)
 				continue;
 			}
 
-read_packet:
 			start_timer();
 
 			tv.tv_sec = 2;
 			tv.tv_usec = 0;
 
+read_packet:
 			read_len = icmp_interface_read((struct batadv_icmp_header *)&icmp_packet_in,
 						       sizeof(icmp_packet_in), &tv);
 			if (read_len <= 0)
Signature · 1 line
2.47.3
#5 lore raw

PATCH 5/11 batctl: traceroute: probe the advertised maximum number of hops

In reply to Sven Eckelmann

The hop loop stops when the TTL reaches TTL_MAX, so only 49 hops are probed while the header line announces "50 hops max". A destination which is exactly TTL_MAX hops away is reported as unreachable even though the last probe was never sent.

Run the loop up to and including TTL_MAX.

Fixes e1c83d9260e8 ("[batctl] traceroute utility updated to latest batman adv")
---
 traceroute.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
traceroute.c +1 -1
index 975c9ff..76f0f80 100644
@@ -123,7 +123,7 @@ static int traceroute(struct state *state, int argc, char **argv)
 	       dst_string, mac_string, TTL_MAX, sizeof(icmp_packet_out));
 
 	for (icmp_packet_out.ttl = 1;
-	     !dst_reached && icmp_packet_out.ttl < TTL_MAX;
+	     !dst_reached && icmp_packet_out.ttl <= TTL_MAX;
 	     icmp_packet_out.ttl++) {
 		return_mac = NULL;
 		bat_host = NULL;
Signature · 1 line
2.47.3
#6 lore raw

PATCH 6/11 batctl: ping: keep huge '-i' intervals from turning into a flood ping

In reply to Sven Eckelmann

strtod() happily accepts "inf" or values like 1e300 as ping interval. Converting such a value to time_t for the nanosleep interval is undefined behavior.

Clamp the interval to a well representable maximum before splitting it into seconds and nanoseconds.

Fixes 4ebe4fb7b08d ("batctl: ping: Add subsecond precision to ping interval")
---
 ping.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
ping.c +3 -1
index fcb8d5e..c875c38 100644
@@ -106,12 +106,14 @@ static int ping(struct state *state, int argc, char **argv)
 		case 'i':
 			errno = 0;
 			ping_interval = strtod(optarg, &endptr);
-			if (errno || *endptr != '\0') {
+			if (errno || *endptr != '\0' || endptr == optarg ||
+			    !isfinite(ping_interval) || ping_interval <= 0) {
 				fprintf(stderr, "Error - invalid ping interval '%s'\n", optarg);
 				goto out;
 			}
 
 			ping_interval = fmax(ping_interval, 0.001);
+			ping_interval = fmin(ping_interval, 1000000000.0);
 			fractional_part = modf(ping_interval, &integral_part);
 			loop_interval.tv_sec = (time_t)integral_part;
 			loop_interval.tv_nsec = (long)(fractional_part * 1000000000l);
Signature · 1 line
2.47.3
#7 lore raw

PATCH 7/11 batctl: ping: reject invalid packet count argument

In reply to Sven Eckelmann

The -c option parsed its argument with strtol and only mapped the result to the internal "endless" value when it was smaller than 1. But it ignores any invalid parameter silently. The manpage also only documented the "missing -c" as valid selection of the endless loop and not an invalid or smaller than 1 value.

Use the same strto* validation as the rest of batctl and print a user readable error in case of an parsing error.

Fixes 87ade2869cf3 ("add interval and loop count options")
---
 ping.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
ping.c +12 -3
index c875c38..5551cd4 100644
@@ -8,6 +8,7 @@
 
 #include <netinet/in.h>
 #include <errno.h>
+#include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -90,15 +91,23 @@ static int ping(struct state *state, int argc, char **argv)
 	char *endptr;
 	int optchar;
 	int rr = 0;
+	long tmp;
 	int res;
 	int i;
 
 	while ((optchar = getopt(argc, argv, "hc:i:t:RT")) != -1) {
 		switch (optchar) {
 		case 'c':
-			loop_count = strtol(optarg, NULL, 10);
-			if (loop_count < 1)
-				loop_count = -1;
+			tmp = strtol(optarg, &endptr, 10);
+			if (!endptr || *endptr != '\0' || endptr == optarg ||
+			    tmp < 1 || tmp > INT_MAX) {
+				fprintf(stderr,
+					"Error - the supplied packet count is invalid: %s\n",
+					optarg);
+				ping_usage();
+				return EXIT_FAILURE;
+			}
+			loop_count = tmp;
 			break;
 		case 'h':
 			ping_usage();
Signature · 1 line
2.47.3
#8 lore raw

PATCH 8/11 batctl: ping: reject invalid timeout argument

In reply to Sven Eckelmann

The -t option parses its argument with strtol and defines a lower limit of
1. But it ignores any invalid parameter silently.

Use the same strto* validation as the rest of batctl and print a user readable error in case of an parsing error.

Fixes 2474249a6312 ("[batctl] ping utility updated to latest batman adv")
---
 ping.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
ping.c +10 -3
index 5551cd4..7ba872a 100644
@@ -128,9 +128,16 @@ static int ping(struct state *state, int argc, char **argv)
 			loop_interval.tv_nsec = (long)(fractional_part * 1000000000l);
 			break;
 		case 't':
-			timeout = strtol(optarg, NULL, 10);
-			if (timeout < 1)
-				timeout = 1;
+			tmp = strtol(optarg, &endptr, 10);
+			if (!endptr || *endptr != '\0' || endptr == optarg ||
+			    tmp < 1 || tmp > INT_MAX) {
+				fprintf(stderr,
+					"Error - the supplied timeout is invalid: %s\n",
+					optarg);
+				ping_usage();
+				return EXIT_FAILURE;
+			}
+			timeout = tmp;
 			break;
 		case 'R':
 			rr = 1;
Signature · 1 line
2.47.3
#9 lore raw

PATCH 9/11 batctl: ping: fix rtt minimum tracking when a sample is 0.0

In reply to Sven Eckelmann

The rtt minimum is tracked with "if (time_delta < min || min == 0.0)", using min's initial value of 0.0 as a "not set yet" sentinel. But 0.0 is also a valid measurement which must be accepted as minimum.

packets_in is still 0 while the first reply is processed (it is incremented afterwards), so use it to detect the first sample instead of overloading min == 0.0.

Fixes 2474249a6312 ("[batctl] ping utility updated to latest batman adv")
---
 ping.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
ping.c +5 -1
index 7ba872a..b5c641b 100644
@@ -300,10 +300,14 @@ static int ping(struct state *state, int argc, char **argv)
 
 			printf("\n");
 
-			if (time_delta < min || min == 0.0)
+			if (packets_in == 0)
 				min = time_delta;
+
+			min = fmin(time_delta, min);
+
 			if (time_delta > max)
 				max = time_delta;
+
 			avg += time_delta;
 			mdev += time_delta * time_delta;
 			packets_in++;
Signature · 1 line
2.47.3
#10 lore raw

PATCH 10/11 batctl: icmp_helper: fail send when the primary mac is unknown

In reply to Sven Eckelmann

icmp_interface_update() calls get_primarymac_netlink() but discards its return value. That helper only writes the output buffer on success; on failure (netlink error, or BATADV_ATTR_HARD_ADDRESS missing from the BATADV_CMD_GET_MESH_INFO reply) it returns a negative errno and leaves the buffer untouched. primary_mac is a zero-initialised static and this is the only place that ever writes it, so a failed query leaves it as 00:00:00:00:00:00. And address which the receiver cannot use to send a reply.

Propagate the failure out of icmp_interface_update() and abort the send.

Fixes 4bd751eed4dc ("batctl: Implement non-routing batadv_icmp in userspace")
---
 icmp_helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
icmp_helper.c +4 -4
index 65a2005..52ac22d 100644
@@ -337,9 +337,7 @@ static int icmp_interface_update(struct state *state)
 	/* remove old interfaces */
 	icmp_interface_sweep();
 
-	get_primarymac_netlink(state, primary_mac);
-
-	return 0;
+	return get_primarymac_netlink(state, primary_mac);
 }
 
 static int icmp_interface_send(struct batadv_icmp_header *icmp_packet,
@@ -391,7 +389,9 @@ int icmp_interface_write(struct state *state,
 	if (icmp_packet->msg_type != BATADV_ECHO_REQUEST)
 		return -EINVAL;
 
-	icmp_interface_update(state);
+	ret = icmp_interface_update(state);
+	if (ret < 0)
+		return ret;
 
 	if (list_empty(&interface_list))
 		return -EFAULT;
Signature · 1 line
2.47.3
#11 lore raw

PATCH 11/11 batctl: icmp_helper: attach socket filter before packets can arrive

In reply to Sven Eckelmann

The raw packet socket is created with ETH_P_ALL as protocol, which registers the ethernet packet hook immediately. Frames from every interface start queuing the moment the socket exists. The BPF filter that restricts the socket to batman-adv ICMP packets with our uid is only attached after bind(). Frames received in the window between socket() and SO_ATTACH_FILTER are therefore queued unfiltered.

Delay the start of the capture by:

* create the socket with protocol 0 (no capture)
* attach the filter while the queue is guaranteed empty
* then bind() with sll_protocol = htons(ETH_P_ALL)

Only after the bind, packets will be captured.

Fixes 4bd751eed4dc ("batctl: Implement non-routing batadv_icmp in userspace")
---
 icmp_helper.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
icmp_helper.c +12 -7
index 52ac22d..570f0b3 100644
@@ -176,7 +176,12 @@ static int icmp_interface_add(const char *ifname, const uint8_t mac[ETH_ALEN])
 	strncpy(iface->name, ifname, IFNAMSIZ);
 	iface->name[sizeof(iface->name) - 1] = '\0';
 
-	iface->sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
+	/* create the socket with protocol 0 so the kernel does not start
+	 * capturing yet - otherwise frames would queue unfiltered between
+	 * socket() and SO_ATTACH_FILTER. Delivery only starts at bind() below,
+	 * by which time the filter is already installed.
+	 */
+	iface->sock = socket(PF_PACKET, SOCK_RAW, 0);
 	if (iface->sock < 0) {
 		perror("Error - can't create raw socket");
 		ret = -errno;
@@ -194,6 +199,12 @@ static int icmp_interface_add(const char *ifname, const uint8_t mac[ETH_ALEN])
 		goto close_sock;
 	}
 
+	ret = icmp_interface_filter(iface->sock, uid);
+	if (ret < 0) {
+		fprintf(stderr, "Error - can't add filter to raw socket: %s\n", strerror(-ret));
+		goto close_sock;
+	}
+
 	memset(&sll, 0, sizeof(sll));
 	sll.sll_family = AF_PACKET;
 	sll.sll_protocol = htons(ETH_P_ALL);
@@ -207,12 +218,6 @@ static int icmp_interface_add(const char *ifname, const uint8_t mac[ETH_ALEN])
 		goto close_sock;
 	}
 
-	ret = icmp_interface_filter(iface->sock, uid);
-	if (ret < 0) {
-		fprintf(stderr, "Error - can't add filter to raw socket: %s\n", strerror(-ret));
-		goto close_sock;
-	}
-
 	list_add(&iface->list, &interface_list);
 
 	return 0;
Signature · 1 line
2.47.3