[PATCH] batctl: switch to native getrandom

Sven Eckelmann <[email protected]> Sun, 21 Jun 2026 13:02:09 +0200
Newsgroups org.open-mesh.lists.batman
Message-ID <[email protected]>
When the commit 98541a89ff99 ("batctl: Add helper to generate instant
random bytes") was added, getrandom() was not available on all systems
which batctl should support. This changed and all the fallback code can now
be removed and getrandom() can be used directly

Signed-off-by: Sven Eckelmann <[email protected]>
---
 functions.c   | 65 -----------------------------------------------------------
 functions.h   |  2 --
 icmp_helper.c | 10 ++++++++-
 ping.c        |  8 +++++++-
 traceroute.c  |  7 ++++++-
 5 files changed, 22 insertions(+), 70 deletions(-)

diff --git a/functions.c b/functions.c
index 5e1cb40..170a1ff 100644
--- a/functions.c
+++ b/functions.c
@@ -869,71 +869,6 @@ int check_mesh_iface_ownership(struct state *state, char *hard_iface)
 	return EXIT_SUCCESS;
 }
 
-static int get_random_bytes_syscall(void *buf __maybe_unused,
-				    size_t buflen __maybe_unused)
-{
-#ifdef SYS_getrandom
-	return syscall(SYS_getrandom, buf, buflen, 0);
-#else
-	return -EOPNOTSUPP;
-#endif
-}
-
-static int get_random_bytes_urandom(void *buf, size_t buflen)
-{
-	ssize_t r;
-	int fd;
-
-	fd = open("/dev/urandom", O_RDONLY);
-	if (fd < 0)
-		return -EOPNOTSUPP;
-
-	r = read(fd, buf, buflen);
-	close(fd);
-	if (r < 0)
-		return -EOPNOTSUPP;
-
-	if ((size_t)r != buflen)
-		return -EOPNOTSUPP;
-
-	return 0;
-}
-
-static int get_random_bytes_fallback(void *buf, size_t buflen)
-{
-	static int initialized;
-	struct timespec now;
-	uint8_t *bufc = buf;
-	size_t i;
-
-	/* this is not a good source for randomness */
-	if (!initialized) {
-		clock_gettime(CLOCK_MONOTONIC, &now);
-		srand(now.tv_sec ^ now.tv_nsec);
-		initialized = 1;
-	}
-
-	for (i = 0; i < buflen; i++)
-		bufc[i] = rand() & 0xff;
-
-	return 0;
-}
-
-void get_random_bytes(void *buf, size_t buflen)
-{
-	int ret;
-
-	ret = get_random_bytes_syscall(buf, buflen);
-	if (ret != -EOPNOTSUPP)
-		return;
-
-	ret = get_random_bytes_urandom(buf, buflen);
-	if (ret != -EOPNOTSUPP)
-		return;
-
-	get_random_bytes_fallback(buf, buflen);
-}
-
 int parse_bool(const char *val, bool *res)
 {
 	if (strcasecmp(val, "0") == 0 ||
diff --git a/functions.h b/functions.h
index c2ce1b8..19334df 100644
--- a/functions.h
+++ b/functions.h
@@ -58,8 +58,6 @@ int get_algoname(struct state *state, unsigned int mesh_ifindex,
 int check_mesh_iface(struct state *state);
 int check_mesh_iface_ownership(struct state *state, char *hard_iface);
 
-void get_random_bytes(void *buf, size_t buflen);
-
 int parse_bool(const char *val, bool *res);
 bool parse_throughput(char *buff, const char *description,
 		      uint32_t *throughput);
diff --git a/icmp_helper.c b/icmp_helper.c
index 8b4fad7..33fe535 100644
--- a/icmp_helper.c
+++ b/icmp_helper.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <sys/ioctl.h>
+#include <sys/random.h>
 #include <sys/select.h>
 #include <sys/socket.h>
 #include <sys/uio.h>
@@ -226,7 +227,14 @@ static int icmp_interface_add(const char *ifname, const uint8_t mac[ETH_ALEN])
 
 int icmp_interfaces_init(void)
 {
-	get_random_bytes(&uid, 1);
+	ssize_t r;
+
+	r = getrandom(&uid, sizeof(uid), 0);
+	if (r < 0)
+		return -errno;
+
+	if (r != sizeof(uid))
+		return -EIO;
 
 	return 0;
 }
diff --git a/ping.c b/ping.c
index b61bca4..dfe99c4 100644
--- a/ping.c
+++ b/ping.c
@@ -170,7 +170,13 @@ static int ping(struct state *state, int argc, char **argv)
 	signal(SIGINT, sig_handler);
 	signal(SIGTERM, sig_handler);
 
-	icmp_interfaces_init();
+	res = icmp_interfaces_init();
+	if (res < 0) {
+		fprintf(stderr, "Error - unable to initialize ICMP interface: %s\n",
+			strerror(-res));
+		goto out;
+	}
+
 	packet_len = sizeof(struct batadv_icmp_packet);
 
 	memset(&icmp_packet_out, 0, sizeof(icmp_packet_out));
diff --git a/traceroute.c b/traceroute.c
index a0fb925..9748151 100644
--- a/traceroute.c
+++ b/traceroute.c
@@ -106,7 +106,12 @@ static int traceroute(struct state *state, int argc, char **argv)
 
 	mac_string = ether_ntoa_long(dst_mac);
 
-	icmp_interfaces_init();
+	res = icmp_interfaces_init();
+	if (res < 0) {
+		fprintf(stderr, "Error - unable to initialize ICMP interface: %s\n",
+			strerror(-res));
+		goto out;
+	}
 
 	memset(&icmp_packet_out, 0, sizeof(icmp_packet_out));
 	memcpy(&icmp_packet_out.dst, dst_mac, ETH_ALEN);

---
base-commit: ea57436f665805a6e93f496617e44dab8a2f33a4
change-id: 20260621-native-random-ccff841a555d

Best regards,
--  
Sven Eckelmann <[email protected]>