batctl: switch to native getrandom
PATCH 1/1 batctl: switch to native getrandom
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(-)
functions.c +0 -65
@@ -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 ||
functions.h +0 -2
@@ -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);
icmp_helper.c +9 -1
@@ -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 ; }
ping.c +7 -1
@@ -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));
traceroute.c +6 -1
@@ -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,
Signature · 1 line
Sven Eckelmann <[email protected]>