Skip to contentSkip to conversation

batctl: random bugfixes 2026-06-21

PATCH New Sven Eckelmann 1 participant 29 messages Jun 21

lore
Share & tools
lore
#0 lore raw

cover batctl: random bugfixes 2026-06-21

This patchset batctl is just a bunch fixes without any specific topic in mind.

---
Sven Eckelmann (28):
batctl: fix parsing of parameters with arguments
batctl: originators: fix throughput lines with bat_hosts
batctl: free header lines after error
batctl: use strict interval/timeout parsing
batctl: improve number parsing error handling
batctl: traceroute: handle fast replies
batctl: ping: count sent and not received pings
batctl: return only initialized icmp destination unreached bytes
batctl: tcpdump: fix coded packet mac dest mac addresses
batctl: tcpdump: fix endianness of fragmentation sequence number
batctl: tcpdump: correct output of VLAN IDs
batctl: tcpdump: drop hardcoded IPv6 buffer sizes
batctl: tcpdump: fix reported length for ICMP6_TIME_EXCEEDED
batctl: tcpdump: handle TCP packet with bogus data offset
batctl: tpmeter: fix Gbps output
batctl: handle netlink callback object on error
batctl: netlink: check for BATADV_ATTR_MESH_ADDRESS before accessing it
batctl: netlink: consume until acks even on errors
batctl: netlink: always 0-terminate hardif name
batctl: version: don't strip newline for empty buffer
batctl: rtnl: report correct error on kernel communication problem
batct: bat-hosts: compare full path for deduplication
batctl: mark file read only succesful on read line
batctl: bisect_iv: avoid access of uninitialized loop magic
batctl: bisect_iv: handle failed node_get for RT_FLAG_DELETE
batctl: bisect_iv: avoid write before buffer whole tokenizing log
batctl: bisect_iv: avoid out of bound access on short lines
batctl: bisect_iv: drop dangling phantom event on OOM error

 bat-hosts.c        |  2 +-
 bisect_iv.c        | 44 ++++++++++++++++++++++++++------------------
 debug.c            |  4 ++--
 elp_interval.c     |  7 +++++--
 functions.c        | 11 ++++++-----
 gw_mode.c          |  8 ++++++--
 hop_penalty.c      |  7 +++++--
 icmp_helper.c      |  6 ++++++
 isolation_mark.c   |  8 ++++----
 main.c             |  4 ++--
 multicast_fanout.c |  7 +++++--
 netlink.c          | 16 ++++++++++------
 orig_interval.c    |  7 +++++--
 originators.c      |  4 ++--
 ping.c             | 14 ++++----------
 routing_algo.c     |  7 ++++++-
 sys.c              |  2 +-
 tcpdump.c          | 33 ++++++++++++++++-----------------
 throughputmeter.c  | 19 ++++++++++++-------
 traceroute.c       | 12 ++++++------
 20 files changed, 130 insertions(+), 92 deletions(-)

---
base-commit: ea57436f665805a6e93f496617e44dab8a2f33a4
change-id: 20260621-bugfixes-875a8d4d9dbc

Best regards,

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

PATCH 1/28 batctl: fix parsing of parameters with arguments

If using a getopt argument with an argument, it can be supplied via:

* -m asd
* -m=asd
* -masd

Only the first version is correctly handled by the found_args implementation (without causing undefined behavior). But it can be instead simplified and fixed at the same time by directly using the optind variable from getopt.

Fixes 87ade2869cf3 ("add interval and loop count options")
Fixes 3bdfc388e74b ("implement simple tcpdump, first only batman packets")
Fixes ece05e1c4c1f ("[batctl] bisect (a tool to analyze logfiles) added")
Fixes f3c9cf9e730e ("source out calculation of round trip time to functions.c add abbreviation for modules, example ping = p add traceroute module, not complete")
Fixes f109b3473f86 ("batctl: introduce throughput meter support")
---
 bisect_iv.c       | 15 ++++-----------
 main.c            |  2 +-
 ping.c            | 10 ++--------
 tcpdump.c         | 13 ++++---------
 throughputmeter.c |  7 ++-----
 traceroute.c      |  7 ++-----
 6 files changed, 15 insertions(+), 39 deletions(-)
bisect_iv.c +4 -11
index d4d507a..5da5bf1 100644
@@ -1495,7 +1495,6 @@ static int bisect_iv(struct state *state __maybe_unused, int argc, char **argv)
 	int num_parsed_files;
 	long long tmp_seqno;
 	char orig[NAME_LEN];
-	int found_args = 1;
 	char *dash_ptr;
 	int optchar;
 	int res;
@@ -1510,19 +1509,15 @@ static int bisect_iv(struct state *state __maybe_unused, int argc, char **argv)
 			return EXIT_SUCCESS;
 		case 'l':
 			loop_orig_ptr = optarg;
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		case 'n':
 			read_opt &= ~USE_BAT_HOSTS;
-			found_args += 1;
 			break;
 		case 'o':
 			filter_orig_ptr = optarg;
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		case 'r':
 			rt_orig_ptr = optarg;
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		case 's':
 			dash_ptr = strchr(optarg, '-');
@@ -1549,11 +1544,9 @@ static int bisect_iv(struct state *state __maybe_unused, int argc, char **argv)
 				*dash_ptr = '-';
 			}
 
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		case 't':
 			trace_orig_ptr = optarg;
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		default:
 			bisect_iv_usage();
@@ -1561,7 +1554,7 @@ static int bisect_iv(struct state *state __maybe_unused, int argc, char **argv)
 		}
 	}
 
-	if (argc <= found_args + 1) {
+	if (argc <= optind + 1) {
 		fprintf(stderr, "Error - need at least 2 log files to compare\n");
 		bisect_iv_usage();
 		goto err;
@@ -1624,13 +1617,13 @@ static int bisect_iv(struct state *state __maybe_unused, int argc, char **argv)
 			goto err;
 	}
 
-	while (argc > found_args) {
-		res = parse_log_file(argv[found_args]);
+	while (argc > optind) {
+		res = parse_log_file(argv[optind]);
 
 		if (res > 0)
 			num_parsed_files++;
 
-		found_args++;
+		optind++;
 	}
 
 	if (num_parsed_files < 2) {
main.c +1 -1
index da4cbb6..79ed4ef 100644
@@ -357,7 +357,7 @@ int main(int argc, char **argv)
 			fprintf(stderr,
 				"Warning - option -m was deprecated and will be removed in the future\n");
 
-			state.arg_iface = argv[2];
+			state.arg_iface = optarg;
 			break;
 		case 'v':
 			version();
ping.c +2 -8
index b61bca4..4dbbada 100644
@@ -75,7 +75,6 @@ static int ping(struct state *state, int argc, char **argv)
 	uint8_t last_rr_cur = 0;
 	int ret = EXIT_FAILURE;
 	int loop_count = -1;
-	int found_args = 1;
 	size_t packet_len;
 	struct timeval tv;
 	double time_delta;
@@ -100,7 +99,6 @@ static int ping(struct state *state, int argc, char **argv)
 			loop_count = strtol(optarg, NULL, 10);
 			if (loop_count < 1)
 				loop_count = -1;
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		case 'h':
 			ping_usage();
@@ -117,21 +115,17 @@ static int ping(struct state *state, int argc, char **argv)
 			fractional_part = modf(ping_interval, &integral_part);
 			loop_interval.tv_sec = (time_t)integral_part;
 			loop_interval.tv_nsec = (long)(fractional_part * 1000000000l);
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		case 't':
 			timeout = strtol(optarg, NULL, 10);
 			if (timeout < 1)
 				timeout = 1;
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		case 'R':
 			rr = 1;
-			found_args++;
 			break;
 		case 'T':
 			disable_translate_mac = 1;
-			found_args += 1;
 			break;
 		default:
 			ping_usage();
@@ -139,13 +133,13 @@ static int ping(struct state *state, int argc, char **argv)
 		}
 	}
 
-	if (argc <= found_args) {
+	if (optind >= argc) {
 		fprintf(stderr, "Error - target mac address or bat-host name not specified\n");
 		ping_usage();
 		return EXIT_FAILURE;
 	}
 
-	dst_string = argv[found_args];
+	dst_string = argv[optind];
 	bat_hosts_init(0);
 	bat_host = bat_hosts_find_by_name(dst_string);
 
tcpdump.c +4 -9
index eb7524e..3409e59 100644
@@ -1493,7 +1493,6 @@ static int tcpdump(struct state *state __maybe_unused, int argc, char **argv)
 	fd_set tmp_wait_sockets;
 	int ret = EXIT_FAILURE;
 	fd_set wait_sockets;
-	int found_args = 1;
 	struct timeval tv;
 	int max_sock = 0;
 	ssize_t read_len;
@@ -1507,26 +1506,22 @@ static int tcpdump(struct state *state __maybe_unused, int argc, char **argv)
 		switch (optchar) {
 		case 'c':
 			read_opt |= COMPAT_FILTER;
-			found_args += 1;
 			break;
 		case 'h':
 			tcpdump_usage();
 			return EXIT_SUCCESS;
 		case 'n':
 			read_opt &= ~USE_BAT_HOSTS;
-			found_args += 1;
 			break;
 		case 'p':
 			tmp = strtol(optarg, NULL, 10);
 			if (tmp > 0 && tmp <= dump_level_all)
 				dump_level = tmp;
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		case 'x':
 			tmp = strtol(optarg, NULL, 10);
 			if (tmp > 0 && tmp <= dump_level_all)
 				dump_level &= ~tmp;
-			found_args += ((*((char *)(optarg - 1)) == optchar) ? 1 : 2);
 			break;
 		default:
 			tcpdump_usage();
@@ -1534,7 +1529,7 @@ static int tcpdump(struct state *state __maybe_unused, int argc, char **argv)
 		}
 	}
 
-	if (argc <= found_args) {
+	if (optind >= argc) {
 		fprintf(stderr, "Error - target interface not specified\n");
 		tcpdump_usage();
 		return EXIT_FAILURE;
@@ -1549,8 +1544,8 @@ static int tcpdump(struct state *state __maybe_unused, int argc, char **argv)
 	INIT_LIST_HEAD(&dump_if_list);
 	FD_ZERO(&wait_sockets);
 
-	while (argc > found_args) {
-		dump_if = create_dump_interface(argv[found_args]);
+	while (optind < argc) {
+		dump_if = create_dump_interface(argv[optind]);
 		if (!dump_if)
 			goto out;
 
@@ -1559,7 +1554,7 @@ static int tcpdump(struct state *state __maybe_unused, int argc, char **argv)
 
 		FD_SET(dump_if->raw_sock, &wait_sockets);
 		list_add_tail(&dump_if->list, &dump_if_list);
-		found_args++;
+		optind++;
 	}
 
 	while (!is_aborted) {
throughputmeter.c +2 -5
index bfd59a5..d670c51 100644
@@ -326,7 +326,6 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 	struct bat_host *bat_host;
 	int ret = EXIT_FAILURE;
 	uint64_t throughput;
-	int found_args = 1;
 	uint32_t time = 0;
 	char *dst_string;
 	int optchar;
@@ -334,12 +333,10 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 	while ((optchar = getopt(argc, argv, "t:n")) != -1) {
 		switch (optchar) {
 		case 't':
-			found_args += 2;
 			time = strtoul(optarg, NULL, 10);
 			break;
 		case 'n':
 			read_opt &= ~USE_BAT_HOSTS;
-			found_args += 1;
 			break;
 		default:
 			tp_meter_usage();
@@ -347,12 +344,12 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 		}
 	}
 
-	if (argc <= found_args) {
+	if (optind >= argc) {
 		tp_meter_usage();
 		return EXIT_FAILURE;
 	}
 
-	dst_string = argv[found_args];
+	dst_string = argv[optind];
 	bat_hosts_init(read_opt);
 	bat_host = bat_hosts_find_by_name(dst_string);
 
traceroute.c +2 -5
index a0fb925..8f23128 100644
@@ -48,7 +48,6 @@ static int traceroute(struct state *state, int argc, char **argv)
 	int ret = EXIT_FAILURE;
 	char dst_reached = 0;
 	int seq_counter = 0;
-	int found_args = 1;
 	struct timeval tv;
 	ssize_t read_len;
 	char *dst_string;
@@ -65,11 +64,9 @@ static int traceroute(struct state *state, int argc, char **argv)
 			return EXIT_SUCCESS;
 		case 'n':
 			read_opt &= ~USE_BAT_HOSTS;
-			found_args += 1;
 			break;
 		case 'T':
 			disable_translate_mac = 1;
-			found_args += 1;
 			break;
 		default:
 			traceroute_usage();
@@ -77,13 +74,13 @@ static int traceroute(struct state *state, int argc, char **argv)
 		}
 	}
 
-	if (argc <= found_args) {
+	if (optind >= argc) {
 		fprintf(stderr, "Error - target mac address or bat-host name not specified\n");
 		traceroute_usage();
 		return EXIT_FAILURE;
 	}
 
-	dst_string = argv[found_args];
+	dst_string = argv[optind];
 	bat_hosts_init(read_opt);
 	bat_host = bat_hosts_find_by_name(dst_string);
 
Signature · 1 line
2.47.3
#2 lore raw

PATCH 2/28 batctl: originators: fix throughput lines with bat_hosts

In reply to Sven Eckelmann

When bat_hosts is used, the output should be the same as without it. But for B.A.T.M.A.N. V, there were two major differences:

* when no hostname was found: extra space before the nexthop neighbor
* when hostname was found: best-flag character before the hostname

Sync the implementation with the B.A.T.M.A.N. IV implementation to get rid of the unexpected differences.

Fixes d8dd1ff1a0fe ("batctl: Use netlink to replace some of debugfs")
---
 originators.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
originators.c +2 -2
index 8059bdd..7f06b09 100644
@@ -130,9 +130,9 @@ static int originators_callback(struct nl_msg *msg, void *arg)
 			       throughput_mbits, throughput_kbits / 100);
 			bat_host = bat_hosts_find_by_mac((char *)neigh);
 			if (bat_host)
-				printf(" %c %17s ", c, bat_host->name);
+				printf("%17s ", bat_host->name);
 			else
-				printf(" %02x:%02x:%02x:%02x:%02x:%02x ",
+				printf("%02x:%02x:%02x:%02x:%02x:%02x ",
 				       neigh[0], neigh[1], neigh[2],
 				       neigh[3], neigh[4], neigh[5]);
 			printf("[%10s]\n", ifname);
Signature · 1 line
2.47.3
#3 lore raw

PATCH 3/28 batctl: free header lines after error

In reply to Sven Eckelmann

If the common netlink helper or the routing algorithm code never printed the header, nothing would clean up the remaining_headers. Explicitly free it up to avoid a minor memory leak.

Fixes 5401c71adfad ("batctl: Use debugfs fallback when netlink not supported")
---
 netlink.c      | 4 ++++
 routing_algo.c | 6 +++++-
 2 files changed, 9 insertions(+), 1 deletion(-)
netlink.c +4 -0
index 9d3d3c6..1121956 100644
@@ -589,6 +589,10 @@ int netlink_print_common(struct state *state, char *orig_iface, int read_opt,
 
 	} while (!last_err && read_opt & (CONT_READ | CLR_CONT_READ));
 
+	/* free a header that was prepared but never printed (e.g. on error) */
+	free(opts.remaining_header);
+	opts.remaining_header = NULL;
+
 	bat_hosts_free();
 
 	return last_err;
routing_algo.c +5 -1
index c3f4580..1c2c2b4 100644
@@ -108,8 +108,12 @@ static int print_routing_algos(struct state *state)
 
 	nl_recvmsgs(state->sock, cb);
 
-	if (!last_err)
+	if (!last_err) {
 		netlink_print_remaining_header(&opts);
+	} else {
+		free(opts.remaining_header);
+		opts.remaining_header = NULL;
+	}
 
 	return last_err;
 }
Signature · 1 line
2.47.3
#4 lore raw

PATCH 4/28 batctl: use strict interval/timeout parsing

In reply to Sven Eckelmann

The sscanf() function does not only return 0 when the value could not be parsed from the string. Also EOF is a potential return value. To make the check more strict, just compare the returned value with the expected number of scanned/parsed items.

Fixes 302a41a73915 ("batctl: Add timeout filtering option for originators")
Fixes e4a7b7733faf ("batctl: Add an optional interval for watch-mode")
---
 debug.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
debug.c +2 -2
index 163a46b..5bf6d5f 100644
@@ -68,7 +68,7 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 				break;
 			}
 
-			if (!sscanf(optarg, "%f", &watch_interval)) {
+			if (sscanf(optarg, "%f", &watch_interval) != 1) {
 				fprintf(stderr,
 					"Error - provided argument of '-%c' is not a number\n",
 					optchar);
@@ -83,7 +83,7 @@ int handle_debug_table(struct state *state, int argc, char **argv)
 			}
 
 			read_opt |= NO_OLD_ORIGS;
-			if (!sscanf(optarg, "%f", &orig_timeout)) {
+			if (sscanf(optarg, "%f", &orig_timeout) != 1) {
 				fprintf(stderr,
 					"Error - provided argument of '-%c' is not a number\n",
 					optchar);
Signature · 1 line
2.47.3
#5 lore raw

PATCH 5/28 batctl: improve number parsing error handling

In reply to Sven Eckelmann

The strtoul error handling is rather complicated and it is not only about checking the return value. The possible error indicators are:

* endptr is NULL
* endptr is not pointing to end delimiter
* endptr is pointing at nptr (because it might have been an empty string)
* returned value is larger than the expected maximum value range

THe last two conditions were not checked even when it is a potential problem for multiple places.

Fixes df5c452a4469 ("batctl: Add elp_interval setting command")
Fixes 74b6d3bd7763 ("batctl: Parse the arguments for gw_mode")
Fixes cde0af829351 ("batctl: Add hop_penalty setting command")
Fixes a319ec4dcbff ("batctl: Support generic netlink for isolation_mark command")
Fixes 1ca604d5a0f2 ("batctl: add switch for setting multicast_fanout")
Fixes c49893119205 ("batctl: Support generic netlink for orig_interval command")
---
 elp_interval.c     | 7 +++++--
 functions.c        | 2 +-
 gw_mode.c          | 8 ++++++--
 hop_penalty.c      | 7 +++++--
 isolation_mark.c   | 8 ++++----
 multicast_fanout.c | 7 +++++--
 orig_interval.c    | 7 +++++--
 7 files changed, 31 insertions(+), 15 deletions(-)
elp_interval.c +5 -2
index 7dcfa5f..71edf7f 100644
@@ -22,6 +22,7 @@ static int parse_elp_interval(struct state *state, int argc, char *argv[])
 {
 	struct settings_data *settings = state->cmd->arg;
 	struct elp_interval_data *data = settings->data;
+	unsigned long elp_interval;
 	char *endptr;
 
 	if (argc != 2) {
@@ -29,12 +30,14 @@ static int parse_elp_interval(struct state *state, int argc, char *argv[])
 		return -EINVAL;
 	}
 
-	data->elp_interval = strtoul(argv[1], &endptr, 0);
-	if (!endptr || *endptr != '\0') {
+	elp_interval = strtoul(argv[1], &endptr, 0);
+	if (!endptr || *endptr != '\0' || endptr == argv[1] || elp_interval > UINT32_MAX) {
 		fprintf(stderr, "Error - the supplied argument is invalid: %s\n", argv[1]);
 		return -EINVAL;
 	}
 
+	data->elp_interval = elp_interval;
+
 	return 0;
 }
 
functions.c +1 -1
index 5e1cb40..dc6bf75 100644
@@ -970,7 +970,7 @@ bool parse_throughput(char *buff, const char *description, uint32_t *throughput)
 	}
 
 	lthroughput = strtoull(buff, &endptr, 10);
-	if (!endptr || *endptr != '\0') {
+	if (!endptr || *endptr != '\0' || endptr == buff) {
 		fprintf(stderr, "Invalid throughput speed for %s: %s\n",
 			description, buff);
 		return false;
gw_mode.c +6 -2
index 767a7f8..a7cc9a0 100644
@@ -90,6 +90,7 @@ static int parse_gw_limit(char *buff)
 
 static int parse_gw(struct state *state, int argc, char *argv[])
 {
+	unsigned long sel_class;
 	char buff[256];
 	char *endptr;
 	int ret;
@@ -131,13 +132,16 @@ static int parse_gw(struct state *state, int argc, char *argv[])
 					      &gw_globals.sel_class))
 				return -EINVAL;
 		} else {
-			gw_globals.sel_class = strtoul(buff, &endptr, 0);
-			if (!endptr || *endptr != '\0') {
+			sel_class = strtoul(buff, &endptr, 0);
+			if (!endptr || *endptr != '\0' || endptr == buff ||
+			    sel_class > UINT32_MAX) {
 				fprintf(stderr,
 					"Error - unexpected argument for mode \"client\": %s\n",
 					buff);
 				return -EINVAL;
 			}
+
+			gw_globals.sel_class = sel_class;
 		}
 
 		gw_globals.sel_class_found = 1;
hop_penalty.c +5 -2
index f22d36c..db727ac 100644
@@ -22,6 +22,7 @@ static int parse_hop_penalty(struct state *state, int argc, char *argv[])
 {
 	struct settings_data *settings = state->cmd->arg;
 	struct hop_penalty_data *data = settings->data;
+	unsigned long hop_penalty;
 	char *endptr;
 
 	if (argc != 2) {
@@ -29,12 +30,14 @@ static int parse_hop_penalty(struct state *state, int argc, char *argv[])
 		return -EINVAL;
 	}
 
-	data->hop_penalty = strtoul(argv[1], &endptr, 0);
-	if (!endptr || *endptr != '\0') {
+	hop_penalty = strtoul(argv[1], &endptr, 0);
+	if (!endptr || *endptr != '\0' || endptr == argv[1] || hop_penalty > UINT8_MAX) {
 		fprintf(stderr, "Error - the supplied argument is invalid: %s\n", argv[1]);
 		return -EINVAL;
 	}
 
+	data->hop_penalty = hop_penalty;
+
 	return 0;
 }
 
isolation_mark.c +4 -4
index cef4de0..a13e3db 100644
@@ -23,10 +23,10 @@ static int parse_isolation_mark(struct state *state, int argc, char *argv[])
 {
 	struct settings_data *settings = state->cmd->arg;
 	struct isolation_mark_data *data;
+	unsigned long mark;
+	unsigned long mask;
 	char *mask_ptr;
 	char buff[256];
-	uint32_t mark;
-	uint32_t mask;
 	char *endptr;
 
 	if (argc != 2) {
@@ -50,13 +50,13 @@ static int parse_isolation_mark(struct state *state, int argc, char *argv[])
 		 * bitmask and not a prefix length
 		 */
 		mask = strtoul(mask_ptr, &endptr, 16);
-		if (!endptr || *endptr != '\0')
+		if (!endptr || *endptr != '\0' || endptr == mask_ptr || mask > UINT32_MAX)
 			goto inval_format;
 	}
 
 	/* the mark can be entered in any base */
 	mark = strtoul(buff, &endptr, 0);
-	if (!endptr || *endptr != '\0')
+	if (!endptr || *endptr != '\0' || endptr == buff || mark > UINT32_MAX)
 		goto inval_format;
 
 	data = settings->data;
multicast_fanout.c +5 -2
index 97d5e0a..6894266 100644
@@ -22,6 +22,7 @@ static int parse_multicast_fanout(struct state *state, int argc, char *argv[])
 {
 	struct settings_data *settings = state->cmd->arg;
 	struct multicast_fanout_data *data;
+	unsigned long multicast_fanout;
 	char *endptr;
 
 	if (argc != 2) {
@@ -30,12 +31,14 @@ static int parse_multicast_fanout(struct state *state, int argc, char *argv[])
 	}
 
 	data = settings->data;
-	data->multicast_fanout = strtoul(argv[1], &endptr, 0);
-	if (!endptr || *endptr != '\0') {
+	multicast_fanout = strtoul(argv[1], &endptr, 0);
+	if (!endptr || *endptr != '\0' || endptr == argv[1] || multicast_fanout > UINT32_MAX) {
 		fprintf(stderr, "Error - the supplied argument is invalid: %s\n", argv[1]);
 		return -EINVAL;
 	}
 
+	data->multicast_fanout = multicast_fanout;
+
 	return 0;
 }
 
orig_interval.c +5 -2
index 970c752..e678d34 100644
@@ -22,6 +22,7 @@ static int parse_orig_interval(struct state *state, int argc, char *argv[])
 {
 	struct settings_data *settings = state->cmd->arg;
 	struct orig_interval_data *data = settings->data;
+	unsigned long orig_interval;
 	char *endptr;
 
 	if (argc != 2) {
@@ -29,12 +30,14 @@ static int parse_orig_interval(struct state *state, int argc, char *argv[])
 		return -EINVAL;
 	}
 
-	data->orig_interval = strtoul(argv[1], &endptr, 0);
-	if (!endptr || *endptr != '\0') {
+	orig_interval = strtoul(argv[1], &endptr, 0);
+	if (!endptr || *endptr != '\0' || endptr == argv[1] || orig_interval > UINT32_MAX) {
 		fprintf(stderr, "Error - the supplied argument is invalid: %s\n", argv[1]);
 		return -EINVAL;
 	}
 
+	data->orig_interval = orig_interval;
+
 	return 0;
 }
 
Signature · 1 line
2.47.3
#6 lore raw

PATCH 6/28 batctl: traceroute: handle fast replies

In reply to Sven Eckelmann

If a reply for a traceroute is received faster than the CLOCK_MONOTONIC precision or (in the rather unlikely scenario) the double precision, time_delta[i] would be 0. In this case, the packet would be counted as lost. Introduce a new array "received" to only track whether the packet was received or not.

Fixes 0641511400dc ("batctl: traceroute - use all received packets to retrieve neighbor mac")
---
 traceroute.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
traceroute.c +4 -1
index 8f23128..d9db618 100644
@@ -42,6 +42,7 @@ static int traceroute(struct state *state, int argc, char **argv)
 	struct batadv_icmp_packet icmp_packet_in;
 	struct ether_addr *dst_mac = NULL;
 	double time_delta[NUM_PACKETS];
+	uint8_t received[NUM_PACKETS];
 	int disable_translate_mac = 0;
 	int read_opt = USE_BAT_HOSTS;
 	struct bat_host *bat_host;
@@ -125,6 +126,7 @@ static int traceroute(struct state *state, int argc, char **argv)
 		for (i = 0; i < NUM_PACKETS; i++) {
 			icmp_packet_out.seqno = htons(++seq_counter);
 			time_delta[i] = 0.0;
+			received[i] = 0;
 
 			res = icmp_interface_write(state,
 						   (struct batadv_icmp_header *)&icmp_packet_out,
@@ -162,6 +164,7 @@ static int traceroute(struct state *state, int argc, char **argv)
 				/* fall through */
 			case BATADV_TTL_EXCEEDED:
 				time_delta[i] = end_timer();
+				received[i] = 1;
 
 				if (!return_mac) {
 					return_mac = ether_ntoa_long((struct ether_addr *)&icmp_packet_in.orig);
@@ -195,7 +198,7 @@ static int traceroute(struct state *state, int argc, char **argv)
 			       bat_host->name, return_mac);
 
 		for (i = 0; i < NUM_PACKETS; i++) {
-			if (time_delta[i])
+			if (received[i])
 				printf("  %.3f ms", time_delta[i]);
 			else
 				printf("   *");
Signature · 1 line
2.47.3
#7 lore raw

PATCH 7/28 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 4dbbada..98a4570 100644
@@ -207,6 +207,8 @@ static int ping(struct state *state, int argc, char **argv)
 			goto sleep;
 		}
 
+		packets_out++;
+
 read_packet:
 		start_timer();
 
@@ -216,8 +218,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
#8 lore raw

PATCH 8/28 batctl: return only initialized icmp destination unreached bytes

In reply to Sven Eckelmann

When icmp_interface_write() fails with an unreachable destination, it prepares a reply packet using original packet but changes the message type to BATADV_DESTINATION_UNREACHABLE. But when icmp_interface_read() tries to retrieve this data, the requested packet len could be higher than the length of the original packet. In this case, the returned packet length must be truncated to the number of bytes of the original one.

Fixes 4bd751eed4dc ("batctl: Implement non-routing batadv_icmp in userspace")
---
 icmp_helper.c | 6 ++++++
 1 file changed, 6 insertions(+)
icmp_helper.c +6 -0
index 8b4fad7..64be9b2 100644
@@ -471,6 +471,12 @@ ssize_t icmp_interface_read(struct batadv_icmp_header *icmp_packet, size_t len,
 		packet_len = len;
 
 	if (direct_reply_len > 0) {
+		/* never deliver more than was actually stored by the
+		 * dst_unreachable path, nor more than the caller's buffer holds
+		 */
+		if (packet_len > direct_reply_len)
+			packet_len = direct_reply_len;
+
 		memcpy(icmp_packet, icmp_buffer, packet_len);
 		direct_reply_len = 0;
 		return (ssize_t)packet_len;
Signature · 1 line
2.47.3
#9 lore raw

PATCH 9/28 batctl: tcpdump: fix coded packet mac dest mac addresses

In reply to Sven Eckelmann

get_name_by_macaddr uses returns shared buffer. The content of the buffer must therefore be processed first before the next get_name_by_macaddr() call is performed.

Fixes 9aae3c9f27b8 ("batctl: tcpdump: Add support for coded packets")
---
 tcpdump.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tcpdump.c +3 -2
index 3409e59..aced4d3 100644
@@ -1152,9 +1152,10 @@ static void dump_batman_coded(unsigned char *packet_buff, ssize_t buff_len,
 	       get_name_by_macaddr((struct ether_addr *)ether_header->ether_shost,
 				   read_opt));
 
-	printf("%s|%s: CODED, ttvn %d|%d, ttl %hhu\n",
+	printf("%s|",
 	       get_name_by_macaddr((struct ether_addr *)coded_packet->first_orig_dest,
-				   read_opt),
+				   read_opt));
+	printf("%s: CODED, ttvn %d|%d, ttl %hhu\n",
 	       get_name_by_macaddr((struct ether_addr *)coded_packet->second_dest,
 				   read_opt),
 	       coded_packet->first_ttvn, coded_packet->second_ttvn,
Signature · 1 line
2.47.3
#10 lore raw

PATCH 10/28 batctl: tcpdump: fix endianness of fragmentation sequence number

In reply to Sven Eckelmann

The kernel returns the unicast fragmentation packet in network (big endian) byte order. But the system might be little endian. In this case, the bytes must be first reordered before they can be printed.

Fixes 30f8fe9c105a ("batctl: tcpdump: Add support for unicast fragmentation")
---
 tcpdump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tcpdump.c +1 -1
index aced4d3..ddcee03 100644
@@ -1050,7 +1050,7 @@ static void dump_batman_ucast_frag(unsigned char *packet_buff, ssize_t buff_len,
 	printf("%s: UCAST FRAG, seqno %d, no %d, ttl %hhu\n",
 	       get_name_by_macaddr((struct ether_addr *)frag_packet->dest,
 				   read_opt),
-	       frag_packet->seqno, frag_packet->no, frag_packet->ttl);
+	       ntohs(frag_packet->seqno), frag_packet->no, frag_packet->ttl);
 }
 
 static void dump_batman_bcast(unsigned char *packet_buff, ssize_t buff_len,
Signature · 1 line
2.47.3
#11 lore raw

PATCH 11/28 batctl: tcpdump: correct output of VLAN IDs

In reply to Sven Eckelmann

The 16 bit TCI header for VLAN tagging (IEEE 802.1Q) stores 12 bit for the 12 bit VID, 1 bit DEI and 3 bit PCP. It is therefore unexpected to print the 16 bit as vlan identifier without masking out the upper 4 bit.

At the same time, also split the DEI ( Drop Eligible Indicator) and the PCP (Priority Code Point) in separate output fields.

Fixes 7738b534fb07 ("batctl: tcpdump - add vlan support")
---
 tcpdump.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tcpdump.c +2 -1
index ddcee03..e417cd1 100644
@@ -823,7 +823,8 @@ static void dump_vlan(unsigned char *packet_buff, ssize_t buff_len, int read_opt
 		time_printed = print_time();
 
 	vlanhdr->vid = ntohs(vlanhdr->vid);
-	printf("vlan %u, p %u, ", vlanhdr->vid, vlanhdr->vid >> 12);
+	printf("vlan %u, d %u, p %u, ", vlanhdr->vid & 0x0fff, (vlanhdr->vid >> 12) & 0x1,
+	       vlanhdr->vid >> 13);
 
 	/* overwrite vlan tags */
 	memmove(packet_buff + 4, packet_buff, 2 * ETH_ALEN);
Signature · 1 line
2.47.3
#12 lore raw

PATCH 12/28 batctl: tcpdump: drop hardcoded IPv6 buffer sizes

In reply to Sven Eckelmann

The inet_ntop expects to know the dst size buffer. But instead of specifying how to calculate the size of this buffer, just a value was hardcoded - which might not fulfill the "INET6_ADDRSTRLEN" size requirements.

Evaluate the size of the buffer at compile time to avoid potential discrepancies.

Fixes 35b37756f4a3 ("add IPv6 support to tcpdump parser")
---
 tcpdump.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tcpdump.c +2 -2
index e417cd1..0895c61 100644
@@ -676,7 +676,7 @@ static void dump_ipv6(unsigned char *packet_buff, ssize_t buff_len,
 				  sizeof(*nd_neigh_sol), "ICMPv6 Neighbor Solicitation");
 			nd_neigh_sol = (struct nd_neighbor_solicit *)icmphdr;
 			inet_ntop(AF_INET6, &nd_neigh_sol->nd_ns_target,
-				  nd_nas_target, 40);
+				  nd_nas_target, sizeof(nd_nas_target));
 			printf(" neighbor solicitation, who has %s, length %zd\n",
 			       nd_nas_target, buff_len);
 			break;
@@ -685,7 +685,7 @@ static void dump_ipv6(unsigned char *packet_buff, ssize_t buff_len,
 				  sizeof(*nd_advert), "ICMPv6 Neighbor Advertisement");
 			nd_advert = (struct nd_neighbor_advert *)icmphdr;
 			inet_ntop(AF_INET6, &nd_advert->nd_na_target,
-				  nd_nas_target, 40);
+				  nd_nas_target, sizeof(nd_nas_target));
 			printf(" neighbor advertisement, tgt is %s, length %zd\n",
 			       nd_nas_target, buff_len);
 			break;
Signature · 1 line
2.47.3
#13 lore raw

PATCH 13/28 batctl: tcpdump: fix reported length for ICMP6_TIME_EXCEEDED

In reply to Sven Eckelmann

The remaining buffer size for ICMP6_TIME_EXCEEDED is only the buffer length minus the IPv6 header. It could now be discussed whether the output length should now be with or without the icmp6_hdr length. The length with icmp6_hdr was chosen because the code looks like a simple copy and paste error and ip6_hdr's length was most likely the actual intent for the calculation.

Fixes 35b37756f4a3 ("add IPv6 support to tcpdump parser")
---
 tcpdump.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tcpdump.c +1 -1
index 0895c61..9c90f36 100644
@@ -669,7 +669,7 @@ static void dump_ipv6(unsigned char *packet_buff, ssize_t buff_len,
 			break;
 		case ICMP6_TIME_EXCEEDED:
 			printf(" time exceeded in-transit, length %zu\n",
-			       (size_t)buff_len - sizeof(struct icmp6_hdr));
+			       (size_t)buff_len - sizeof(struct ip6_hdr));
 			break;
 		case ND_NEIGHBOR_SOLICIT:
 			LEN_CHECK((size_t)buff_len - (size_t)(sizeof(struct ip6_hdr)),
Signature · 1 line
2.47.3
#14 lore raw

PATCH 14/28 batctl: tcpdump: handle TCP packet with bogus data offset

In reply to Sven Eckelmann

dump_tcp() computes length as (buff_len - ip6_header_len - tcp_header_len), but the only bounds guard ensures 20 bytes, while doff allows a higher header length (60 bytes). With a doff of 15 (60 bytes) and only 20 bytes available in the buffer, the calculation would underflow and show a bugus length of the TCP payload. For now, set the payload length to zero for such a packet.

Fixes 35b37756f4a3 ("add IPv6 support to tcpdump parser")
---
 tcpdump.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
tcpdump.c +3 -1
index 9c90f36..7769cbf 100644
@@ -537,18 +537,20 @@ static void dump_tcp(const char ip_string[], unsigned char *packet_buff,
 {
 	uint16_t tcp_header_len;
 	struct tcphdr *tcphdr;
+	size_t tcp_len;
 
 	LEN_CHECK((size_t)buff_len - ip6_header_len,
 		  sizeof(struct tcphdr), "TCP");
 	tcphdr = (struct tcphdr *)(packet_buff + ip6_header_len);
 	tcp_header_len = tcphdr->doff * 4;
+	tcp_len = (size_t)buff_len - ip6_header_len;
 	printf("%s %s.%i > ", ip_string, src_addr, ntohs(tcphdr->source));
 	printf("%s.%i: TCP, Flags [%c%c%c%c%c%c], length %zu\n",
 	       dst_addr, ntohs(tcphdr->dest),
 	       (tcphdr->fin ? 'F' : '.'), (tcphdr->syn ? 'S' : '.'),
 	       (tcphdr->rst ? 'R' : '.'), (tcphdr->psh ? 'P' : '.'),
 	       (tcphdr->ack ? 'A' : '.'), (tcphdr->urg ? 'U' : '.'),
-	       (size_t)buff_len - ip6_header_len - tcp_header_len);
+	       tcp_len > tcp_header_len ? tcp_len - tcp_header_len : 0);
 }
 
 static void dump_udp(const char ip_string[], unsigned char *packet_buff,
Signature · 1 line
2.47.3
#15 lore raw

PATCH 15/28 batctl: tpmeter: fix Gbps output

In reply to Sven Eckelmann

The formatter for the Gbps result should print 2 digits after the dot. But the format specifier was different than in all other branches and didn't specify the precision after the dot. Instead a minimal number of digits (or spaces) before the dot was defined.

Fixes f109b3473f86 ("batctl: introduce throughput meter support")
---
 throughputmeter.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
throughputmeter.c +1 -1
index d670c51..a45eb08 100644
@@ -431,7 +431,7 @@ static int throughputmeter(struct state *state, int argc, char **argv)
 		if (throughput == UINT64_MAX)
 			printf("inf\n");
 		else if (throughput > (1UL << 30))
-			printf("%.2f GB/s (%2.f Gbps)\n",
+			printf("%.2f GB/s (%.2f Gbps)\n",
 			       (float)throughput / (1 << 30),
 			       (float)throughput * 8 / 1000000000);
 		else if (throughput > (1UL << 20))
Signature · 1 line
2.47.3
#16 lore raw

PATCH 16/28 batctl: handle netlink callback object on error

In reply to Sven Eckelmann

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 1121956..5f78602 100644
@@ -488,11 +488,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 a45eb08..23a6027 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
#17 lore raw

PATCH 17/28 batctl: netlink: check for BATADV_ATTR_MESH_ADDRESS before accessing it

In reply to Sven Eckelmann

info_callback expects the BATADV_ATTR_MESH_ADDRESS attribute to be set. But it never checked if it is not NULL.

Fixes d8dd1ff1a0fe ("batctl: Use netlink to replace some of debugfs")
---
 netlink.c | 1 +
 1 file changed, 1 insertion(+)
netlink.c +1 -0
index 5f78602..be92a9c 100644
@@ -325,6 +325,7 @@ int netlink_stop_callback(struct nl_msg *msg, void *arg __maybe_unused)
 static const int info_mandatory[] = {
 	BATADV_ATTR_MESH_IFINDEX,
 	BATADV_ATTR_MESH_IFNAME,
+	BATADV_ATTR_MESH_ADDRESS,
 };
 
 static const int info_hard_mandatory[] = {
Signature · 1 line
2.47.3
#18 lore raw

PATCH 18/28 batctl: netlink: consume until acks even on errors

In reply to Sven Eckelmann

THe sys_simple_nlquery() function is used on a shared socket. The data until the ack must therefore _always_ be consumed to avoid receive confusions when another command is started on the same socket.

Fixes 0b81e8fbaed5 ("batctl: Consume genl ACKs after setting reads")
---
 netlink.c | 8 +-------
 sys.c     | 2 +-
 2 files changed, 2 insertions(+), 8 deletions(-)
netlink.c +1 -7
index be92a9c..3a1c3a5 100644
@@ -466,7 +466,6 @@ char *netlink_get_info(struct state *state, uint8_t nl_cmd, const char *header)
 	};
 	struct nl_msg *msg;
 	struct nl_cb *cb;
-	int ret;
 
 	msg = nlmsg_alloc();
 	if (!msg)
@@ -488,12 +487,7 @@ char *netlink_get_info(struct state *state, uint8_t nl_cmd, const char *header)
 	nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, info_callback, &opts);
 	nl_cb_err(cb, NL_CB_CUSTOM, netlink_print_error, NULL);
 
-	ret = nl_recvmsgs(state->sock, cb);
-	if (ret < 0) {
-		nl_cb_put(cb);
-		return opts.remaining_header;
-	}
-
+	nl_recvmsgs(state->sock, cb);
 	nl_wait_for_ack(state->sock);
 
 	nl_cb_put(cb);
sys.c +1 -1
index 35a026e..3018c09 100644
@@ -110,7 +110,7 @@ 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;
+			result = ret;
 	}
 
 	nl_wait_for_ack(state->sock);
Signature · 1 line
2.47.3
#19 lore raw

PATCH 19/28 batctl: netlink: always 0-terminate hardif name

In reply to Sven Eckelmann

The kernel should not send non-0 terminated interface name name or an interface name larger than IFNAMSIZ. Just to be on the safe side, still force the last byte to the 0-delimiter.

Fixes 426e48c8d9ca ("batctl: ping: Get outgoing ifname from netlink")
---
 netlink.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
netlink.c +2 -1
index 3a1c3a5..14100c3 100644
@@ -822,7 +822,8 @@ static int get_nexthop_netlink_cb(struct nl_msg *msg, void *arg)
 
 	if (attrs[BATADV_ATTR_HARD_IFNAME]) {
 		ifname = nla_get_string(attrs[BATADV_ATTR_HARD_IFNAME]);
-		strncpy(opts->ifname, ifname, IFNAMSIZ);
+		strncpy(opts->ifname, ifname, IFNAMSIZ - 1);
+		opts->ifname[IFNAMSIZ - 1] = '\0';
 	} else {
 		/* compatibility for Linux < 5.14/batman-adv < 2021.2 */
 		ifname = if_indextoname(index, opts->ifname);
Signature · 1 line
2.47.3
#20 lore raw

PATCH 20/28 batctl: version: don't strip newline for empty buffer

In reply to Sven Eckelmann

When read_file() would return an empty buffer, then version() must not strip the last byte. Otherwise it would try to access 1 byte before the start of the buffer.

Fixes dbc4a8c8e585 ("batctl: version also prints the kernel module version if available")
---
 main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
main.c +1 -1
index 79ed4ef..94db4a1 100644
@@ -132,7 +132,7 @@ static void version(void)
 	printf("batctl %s [batman-adv: ", SOURCE_VERSION);
 
 	ret = read_file(module_ver_path, USE_READ_BUFF | SILENCE_ERRORS);
-	if ((line_ptr) && (line_ptr[strlen(line_ptr) - 1] == '\n'))
+	if (line_ptr && strlen(line_ptr) > 0 && line_ptr[strlen(line_ptr) - 1] == '\n')
 		line_ptr[strlen(line_ptr) - 1] = '\0';
 
 	if (ret == EXIT_SUCCESS)
Signature · 1 line
2.47.3
#21 lore raw

PATCH 21/28 batctl: rtnl: report correct error on kernel communication problem

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 dc6bf75..ae1c5e2 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;
+	}
 
 	nl_recvmsgs(sock, cb);
 
Signature · 1 line
2.47.3
#22 lore raw

PATCH 22/28 batct: bat-hosts: compare full path for deduplication

In reply to Sven Eckelmann

The deduplication code uses realpath to store the real path of a provided configuration file. The code then tries to compare these paths but accidentally only comparse only the first CONF_DIR_LEN (256) bytes of the path.

The number of bytes must be set to the same length as the path size slot in the normalized buffer.

Fixes db97c2a77e81 ("batctl: fix crash in bat-hosts parser on embedded systems")
---
 bat-hosts.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
bat-hosts.c +1 -1
index 57e7d69..6ad8247 100644
@@ -203,7 +203,7 @@ void bat_hosts_init(int read_opt)
 		for (j = 0; j < i; j++) {
 			if (strncmp(normalized + (i * PATH_MAX),
 				    normalized + (j * PATH_MAX),
-				    CONF_DIR_LEN) == 0) {
+				    PATH_MAX) == 0) {
 				parse = 0;
 				break;
 			}
Signature · 1 line
2.47.3
#23 lore raw

PATCH 23/28 batctl: mark file read only succesful on read line

In reply to Sven Eckelmann

The line_ptr is shared globally. It can happen that another function like parse_hosts_file() allocated the buffer successfully. But the next getline() in read_file() fails - but keeps the line_ptr valid. In this case, the function would return a success - even when the buffer contains stale data.

Instead only set the return value to EXIT_SUCCESS when a single line could be read.

Fixes deb324e65044 ("batctl: buffer based reading replaced by line-by-line reading")
---
 functions.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
functions.c +2 -3
index ae1c5e2..bb0dc18 100644
@@ -147,6 +147,8 @@ int read_file(const char *full_path, int read_opt)
 	}
 
 	while (getline(&line_ptr, &len, fp) != -1) {
+		res = EXIT_SUCCESS;
+
 		/* the buffer will be handled elsewhere */
 		if (read_opt & USE_READ_BUFF)
 			break;
@@ -154,9 +156,6 @@ int read_file(const char *full_path, int read_opt)
 		printf("%s", line_ptr);
 	}
 
-	if (line_ptr)
-		res = EXIT_SUCCESS;
-
 	fclose(fp);
 	return res;
 }
Signature · 1 line
2.47.3
#24 lore raw

PATCH 24/28 batctl: bisect_iv: avoid access of uninitialized loop magic

In reply to Sven Eckelmann

The loop magic string is initialized with snprintf - which can be a partial initialization. But the rest of the access assumes that all of the loop magic is initialized because it is then only access with memcmp and memcpy.

Initialize everything to 0 at the beginning of the function to avoid uninitialized bytes.

Fixes ece05e1c4c1f ("[batctl] bisect (a tool to analyze logfiles) added")
---
 bisect_iv.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
bisect_iv.c +2 -2
index 5da5bf1..f1ba94e 100644
@@ -658,7 +658,7 @@ static int print_rt_path_at_seqno(struct bat_node *src_node, struct bat_node *ds
 				  struct bat_node *next_hop, long long seqno,
 				  long long seqno_rand, int read_opt)
 {
-	char curr_loop_magic[LOOP_MAGIC_LEN];
+	char curr_loop_magic[LOOP_MAGIC_LEN] = {};
 	struct bat_node *next_hop_tmp;
 	struct orig_event *orig_event;
 	struct rt_hist *rt_hist;
@@ -720,7 +720,7 @@ static int find_rt_table_change(struct bat_node *src_node, struct bat_node *dst_
 				struct bat_node *curr_node, long long seqno_min,
 				long long seqno_max, long long seqno_rand, int read_opt)
 {
-	char curr_loop_magic[LOOP_MAGIC_LEN];
+	char curr_loop_magic[LOOP_MAGIC_LEN] = {};
 	long long seqno_min_tmp = seqno_min;
 	struct orig_event *orig_event;
 	struct rt_hist *rt_hist_tmp;
Signature · 1 line
2.47.3
#25 lore raw

PATCH 25/28 batctl: bisect_iv: handle failed node_get for RT_FLAG_DELETE

In reply to Sven Eckelmann

When a node could not be found or allocated, then node_get() will return NULL. If this is not checked then it will cause NULL-ptr dereferences in the seqno_event->orig is accessed.

Fixes 700749a4c2f4 ("[batctl] add recursive check for routing loops")
---
 bisect_iv.c | 5 +++++
 1 file changed, 5 insertions(+)
bisect_iv.c +5 -0
index f1ba94e..0e34d37 100644
@@ -311,6 +311,11 @@ static int routing_table_new(char *orig, char *next_hop, char *old_next_hop, cha
 			}
 
 			seqno_event->orig = node_get(orig);
+			if (!seqno_event->orig) {
+				free(seqno_event);
+				goto rt_hist_free;
+			}
+
 			seqno_event->neigh = NULL;
 			seqno_event->prev_sender = NULL;
 			seqno_event->seqno = -1;
Signature · 1 line
2.47.3
#26 lore raw

PATCH 26/28 batctl: bisect_iv: avoid write before buffer whole tokenizing log

In reply to Sven Eckelmann

When neigh and prev_sender are tokenized, their string length is removed by 2 characters. But strtok_r only guarantees 1 character. A malformed log would therefore cause a write before the start of the actual data.

Fixes c3b15dbec883 ("[batctl] bisect - better routing table handling due to add/update/delete detection")
Fixes 8a3d4fed1067 ("[batctl] bisect - fix handling of large bat-host files")
---
 bisect_iv.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
bisect_iv.c +4 -2
index 0e34d37..4420be9 100644
@@ -593,7 +593,8 @@ static int parse_log_file(char *file_path)
 				case 4:
 					if (rt_flag == RT_FLAG_ADD) {
 						neigh = tok_ptr;
-						neigh[strlen(neigh) - 2] = 0;
+						if (strlen(neigh) >= 2)
+							neigh[strlen(neigh) - 2] = 0;
 					}
 					break;
 				case 5:
@@ -601,7 +602,8 @@ static int parse_log_file(char *file_path)
 					break;
 				case 9:
 					prev_sender = tok_ptr;
-					prev_sender[strlen(prev_sender) - 2] = 0;
+					if (strlen(prev_sender) >= 2)
+						prev_sender[strlen(prev_sender) - 2] = 0;
 					break;
 				}
 			}
Signature · 1 line
2.47.3
#27 lore raw

PATCH 27/28 batctl: bisect_iv: avoid out of bound access on short lines

In reply to Sven Eckelmann

The parse_log_file assumes that it can jump over the first 13 characters without checking if the line is actually 13 bytes long. A short log line would therefore cause a jump outside of the initialized buffer area.

To avoid this, ignore all files smaller than 14 bytes.

Fixes ece05e1c4c1f ("[batctl] bisect (a tool to analyze logfiles) added")
---
 bisect_iv.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
bisect_iv.c +6 -2
index 4420be9..6c3f5d6 100644
@@ -501,10 +501,14 @@ static int parse_log_file(char *file_path)
 	}
 
 	while (fgets(line_buff, sizeof(line_buff), fd)) {
-		/* ignore the timestamp at the beginning of each line */
-		start_ptr = line_buff + 13;
 		line_count++;
 
+		/* ignore the timestamp at the beginning of each line */
+		if (strlen(line_buff) <= 13)
+			continue;
+
+		start_ptr = line_buff + 13;
+
 		if (strstr(start_ptr, "Received BATMAN packet via NB")) {
 			strtok_r(start_ptr, " ", &start_ptr_safe);
 			neigh = NULL;
Signature · 1 line
2.47.3
#28 lore raw

PATCH 28/28 batctl: bisect_iv: drop dangling phantom event on OOM error

In reply to Sven Eckelmann

When routing_table_new() fails to allocate rt_table->entries, it might alrady have allocated the seqno_event for RT_FLAG_DELETE. This seqno_event is also already part of the orig_event->event_list.

To avoid this dangling DELETE event, it must also be unlinked and freed.

Fixes ece05e1c4c1f ("[batctl] bisect (a tool to analyze logfiles) added")
---
 bisect_iv.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
bisect_iv.c +5 -1
index 6c3f5d6..43b97e4 100644
@@ -187,8 +187,8 @@ static void node_free(void *data)
 
 static int routing_table_new(char *orig, char *next_hop, char *old_next_hop, char rt_flag)
 {
+	struct seqno_event *seqno_event = NULL;
 	struct rt_table *prev_rt_table = NULL;
-	struct seqno_event *seqno_event;
 	struct bat_node *next_hop_node;
 	struct orig_event *orig_event;
 	struct rt_table *rt_table;
@@ -334,6 +334,10 @@ static int routing_table_new(char *orig, char *next_hop, char *old_next_hop, cha
 	if (!rt_table->entries) {
 		fprintf(stderr,
 			"Could not allocate memory for routing table entries (out of mem?) - skipping");
+		if (rt_flag == RT_FLAG_DELETE && seqno_event) {
+			list_del(&seqno_event->list);
+			free(seqno_event);
+		}
 		goto rt_hist_free;
 	}
 
Signature · 1 line
2.47.3