batctl: Userspace batadv_icmp endpoints
cover batctl: Userspace batadv_icmp endpoints
Hi,
The current endpoint for batadv_icmp* is implemented in the kernel module and can be accessed via debugfs. But the debugfs cannot be accessed from non- default netns or when debugfs is disabled. Thus it has be possible to use it via the netlink infrastructure to make it compatible with future setups.
The use of the socket file is completely removed and instead raw sockets with BPF filters are used to send/receive batadv_icmp_packet* directly. All information about interfaces and available originators are received via rtnetlink and the batman-adv netlink.
The originators debugfs file is used when the batman-adv netlink commands are not available. The routing of batadv_icmp_packets is still done inside the kernel module.
The patchset is based on the netlink and rtnl patchset for batctl.
Kind regards,
Sven
Sven Eckelmann (4):
batctl: Replace list implementation with linux-like-list
batctl: Use monotonic time source for icmp timing
batctl: Add helper to generate instant random bytes
batctl: Implement non-routing batadv_icmp in userspace
Makefile | 4 +- bisect_iv.c | 40 ++- bisect_iv.h | 10 +- functions.c | 82 +++++- functions.h | 2 + icmp_helper.c | 633 ++++++++++++++++++++++++++++++++++++++++++++ icmp_helper.h | 58 ++++ list-batman.c | 123 --------- list-batman.h | 120 --------- list.h | 834 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ netlink.c | 178 ++++++++++++- netlink.h | 3 + ping.c | 42 +-- tcpdump.c | 7 +- tcpdump.h | 2 +- traceroute.c | 42 +-- 16 files changed, 1832 insertions(+), 348 deletions(-) create mode 100644 icmp_helper.c create mode 100644 icmp_helper.h delete mode 100644 list-batman.c delete mode 100644 list-batman.h create mode 100644 list.h
PATCH 1/4 batctl: Replace list implementation with linux-like-list
The list-batman implementation of linked lists has an interface which tries to resample the linux double linked list implementation but in reality is incompatible. This resulted in code which moves most of the pointer arithmetic back to the actual code. This is rather unfortunate because this abstraction should reduce the pointer arithmetic and not increase it.
Also the incompatibilities between the list implementation used in batman-adv and the one used by batctl made coding features in both components error prone because each component required a slightly different implementation for the same list manipulation.
--- Makefile | 1 - bisect_iv.c | 40 ++- bisect_iv.h | 10 +- list-batman.c | 123 --------- list-batman.h | 120 --------- list.h | 834 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tcpdump.c | 7 +- tcpdump.h | 2 +- 8 files changed, 860 insertions(+), 277 deletions(-) delete mode 100644 list-batman.c delete mode 100644 list-batman.h create mode 100644 list.h
Makefile +0 -1
@@ -32,7 +32,6 @@ OBJ += genl.o OBJ += hash.o OBJ += interface.o OBJ += ioctl.o -OBJ += list-batman.o OBJ += main.o OBJ += netlink.o OBJ += ping.o
bisect_iv.c +17 -23
@@ -92,8 +92,8 @@ static struct bat_node *node_get(char *name)strncpy (bat_node->name, name,NAME_LEN ); bat_node->name[NAME_LEN -1 ] ='\0' ; -INIT_LIST_HEAD_FIRST (bat_node->orig_event_list); -INIT_LIST_HEAD_FIRST (bat_node->rt_table_list); +INIT_LIST_HEAD (&bat_node->orig_event_list); +INIT_LIST_HEAD (&bat_node->rt_table_list);memset (bat_node->loop_magic,0 ,sizeof (bat_node->loop_magic));memset (bat_node->loop_magic2,0 ,sizeof (bat_node->loop_magic2));hash_add (node_hash, bat_node);
@@ -112,9 +112,8 @@ static struct orig_event *orig_event_new(struct bat_node *bat_node, struct bat_nreturn NULL ; } -INIT_LIST_HEAD (&orig_event->list); -INIT_LIST_HEAD_FIRST (orig_event->event_list); -INIT_LIST_HEAD_FIRST (orig_event->rt_hist_list); +INIT_LIST_HEAD (&orig_event->event_list); +INIT_LIST_HEAD (&orig_event->rt_hist_list); orig_event->orig_node = orig_node;list_add_tail (&orig_event->list, &bat_node->orig_event_list);
@@ -166,21 +165,21 @@ static void node_free(void *data)list_for_each_entry_safe (orig_event, orig_event_tmp, &bat_node->orig_event_list, list) {list_for_each_entry_safe (seqno_event, seqno_event_tmp, &orig_event->event_list, list) { -list_del ((struct list_head *)&orig_event->event_list, &seqno_event->list, &orig_event->event_list); +list_del (&seqno_event->list);free (seqno_event); }list_for_each_entry_safe (rt_hist, rt_hist_tmp, &orig_event->rt_hist_list, list) { -list_del ((struct list_head *)&orig_event->rt_hist_list, &rt_hist->list, &orig_event->rt_hist_list); +list_del (&rt_hist->list);free (rt_hist); } -list_del ((struct list_head *)&bat_node->orig_event_list, &orig_event->list, &bat_node->orig_event_list); +list_del (&orig_event->list);free (orig_event); }list_for_each_entry_safe (rt_table, rt_table_tmp, &bat_node->rt_table_list, list) { -list_del ((struct list_head *)&bat_node->rt_table_list, &rt_table->list, &bat_node->rt_table_list); +list_del (&rt_table->list);free (rt_table->entries);free (rt_table);
@@ -248,10 +247,8 @@ static int routing_table_new(char *orig, char *next_hop, char *old_next_hop, cha goto table_free; } -INIT_LIST_HEAD (&rt_table->list); rt_table->num_entries =1 ; -INIT_LIST_HEAD (&rt_hist->list); rt_hist->prev_rt_hist =NULL ; rt_hist->next_hop = next_hop_node; rt_hist->flags = rt_flag;
@@ -310,7 +307,6 @@ static int routing_table_new(char *orig, char *next_hop, char *old_next_hop, cha goto rt_hist_free; } -INIT_LIST_HEAD (&seqno_event->list); seqno_event->orig =node_get (orig); seqno_event->neigh =NULL ; seqno_event->prev_sender =NULL ;
@@ -449,7 +445,6 @@ static int seqno_event_new(char *iface_addr, char *orig, char *prev_sender, char goto err; } -INIT_LIST_HEAD (&seqno_event->list); seqno_event->orig = orig_node; seqno_event->neigh = neigh_node; seqno_event->prev_sender = prev_sender_node;
@@ -986,7 +981,7 @@ static void seqno_trace_print_neigh(struct seqno_trace_neigh *seqno_trace_neigh, } } -static void seqno_trace_print (struct list_head_first *trace_list,char *trace_orig, +static void seqno_trace_print (struct list_head *trace_list,char *trace_orig,long long seqno_min,long long seqno_max,char *filter_orig,int read_opt) {struct seqno_trace *seqno_trace;
@@ -1180,7 +1175,6 @@ static struct seqno_trace *seqno_trace_new(struct seqno_event *seqno_event)return NULL ; } -INIT_LIST_HEAD (&seqno_trace->list); seqno_trace->seqno = seqno_event->seqno; seqno_trace->print =0 ;
@@ -1199,7 +1193,7 @@ static void seqno_trace_free(struct seqno_trace *seqno_trace)free (seqno_trace); } -static int seqno_trace_add (struct list_head_first *trace_list,struct bat_node *bat_node, +static int seqno_trace_add (struct list_head *trace_list,struct bat_node *bat_node,struct seqno_event *seqno_event,char print_trace) {struct seqno_trace *seqno_trace =NULL , *seqno_trace_tmp =NULL , *seqno_trace_prev =NULL ;
@@ -1223,12 +1217,12 @@ static int seqno_trace_add(struct list_head_first *trace_list, struct bat_node * goto err;if ((list_empty (trace_list)) || - (seqno_event->seqno > ((struct seqno_trace *)trace_list->prev)->seqno)) + (seqno_event->seqno > list_last_entry(trace_list, struct seqno_trace, list)->seqno))list_add_tail (&seqno_trace->list, trace_list); -else if (seqno_event->seqno < ((struct seqno_trace *)trace_list->next)->seqno) -list_add_before ((struct list_head *)trace_list, trace_list->next, &seqno_trace->list); + else if (seqno_event->seqno < list_first_entry(trace_list, struct seqno_trace, list)->seqno) +list_add (&seqno_trace->list, trace_list);else -list_add_before (&seqno_trace_prev->list, &seqno_trace_tmp->list, &seqno_trace->list); +list_add_behind (&seqno_trace->list, &seqno_trace_prev->list); }if (print_trace)
@@ -1260,14 +1254,14 @@ static void trace_seqnos(char *trace_orig, long long seqno_min, long long seqno_struct orig_event *orig_event;struct seqno_event *seqno_event;struct hash_it_t *hashit =NULL ; -struct list_head_first trace_list; +struct list_head trace_list;struct seqno_trace *seqno_trace, *seqno_trace_tmp;char check_orig[NAME_LEN ], print_trace;int res;/* if no option was given filter_orig is empty */ memset (check_orig,0 ,NAME_LEN ); -INIT_LIST_HEAD_FIRST (trace_list); +INIT_LIST_HEAD (&trace_list);while (NULL != (hashit =hash_iterate (node_hash, hashit))) { bat_node = hashit->bucket->data;
@@ -1313,7 +1307,7 @@ static void trace_seqnos(char *trace_orig, long long seqno_min, long long seqno_ out:list_for_each_entry_safe (seqno_trace, seqno_trace_tmp, &trace_list, list) { -list_del ((struct list_head *)&trace_list, &seqno_trace->list, &trace_list); +list_del (&seqno_trace->list);seqno_trace_free (seqno_trace); }
bisect_iv.h +5 -5
@@ -22,7 +22,7 @@#ifndef _BATCTL_BISECT_IV_H#define _BATCTL_BISECT_IV_H -#include "list-batman.h" +#include "list.h" #define NAME_LEN 18#define MAX_LINE 256
@@ -38,8 +38,8 @@ int bisect_iv(int argc, char **argv);struct bat_node {char name[NAME_LEN ]; -struct list_head_first orig_event_list; -struct list_head_first rt_table_list; +struct list_head orig_event_list; +struct list_head rt_table_list;char loop_magic[LOOP_MAGIC_LEN ];char loop_magic2[LOOP_MAGIC_LEN ]; };
@@ -47,8 +47,8 @@ struct bat_node {struct orig_event {struct list_head list;struct bat_node *orig_node; -struct list_head_first event_list; -struct list_head_first rt_hist_list; +struct list_head event_list; +struct list_head rt_hist_list; };struct rt_table {
list-batman.c deleted +0 -123
123 lines not on this page · raw message
list-batman.h deleted +0 -120
120 lines not on this page · raw message
list.h new file +834 -0
834 lines not on this page · raw message
tcpdump.c +3 -4
@@ -1118,7 +1118,7 @@ int tcpdump(int argc, char **argv)struct ifreq req;struct timeval tv;struct dump_if *dump_if, *dump_if_tmp; -struct list_head_first dump_if_list; +struct list_head dump_if_list;fd_set wait_sockets, tmp_wait_sockets;ssize_t read_len;int ret =EXIT_FAILURE , res, optchar, found_args =1 , max_sock =0 , tmp;
@@ -1168,7 +1168,7 @@ int tcpdump(int argc, char **argv)bat_hosts_init (read_opt);/* init interfaces list */ -INIT_LIST_HEAD_FIRST (dump_if_list); +INIT_LIST_HEAD (&dump_if_list);FD_ZERO (&wait_sockets);while (argc > found_args) {
@@ -1176,7 +1176,6 @@ int tcpdump(int argc, char **argv) dump_if =malloc (sizeof (struct dump_if ));memset (dump_if,0 ,sizeof (struct dump_if )); dump_if->raw_sock =-1 ; -INIT_LIST_HEAD (&dump_if->list); dump_if->dev = argv[found_args];
@@ -1306,7 +1305,7 @@ out:if (dump_if->raw_sock >=0 )close (dump_if->raw_sock); -list_del ((struct list_head *)&dump_if_list, &dump_if->list, &dump_if_list); +list_del (&dump_if->list);free (dump_if); }
tcpdump.h +1 -1
@@ -27,7 +27,7 @@#include <net/if_arp.h> #include <sys/types.h> #include "main.h" -#include "list-batman.h" +#include "list.h" #ifndef ARPHRD_IEEE80211_PRISM #define ARPHRD_IEEE80211_PRISM 802
Signature · 1 line
2.8.1
PATCH 2/4 batctl: Use monotonic time source for icmp timing
In reply to Sven Eckelmanngettimeofday is not monotonic. It would give wrong results in some situation like leap seconds, when switching between daylight saving time and non-DST, NTP changes the time or when the administrator of the system changes it manually. The function is also obsolete since POSIX.1-2008.
clock_gettime is recommended to get a monotonic timesource and as general replacement of gettimeofday.
--- Makefile | 2 +- functions.c | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-)
Makefile +1 -1
@@ -46,7 +46,7 @@ MANPAGE = man/batctl.8 # batctl flags and options CFLAGS += -Wall -W -std=gnu99 -fno-strict-aliasing -MD -MP CPPFLAGS += -D_GNU_SOURCE -LDLIBS += -lm +LDLIBS += -lm -lrt # disable verbose output ifneq ($(findstring $(MAKEFLAGS),s),s)
functions.c +9 -8
@@ -48,6 +48,7 @@#include <netlink/handlers.h> #include <netlink/msg.h> #include <netlink/attr.h> +#include <time.h> #include "main.h" #include "functions.h"
@@ -59,7 +60,7 @@#define PATH_BUFF_LEN 200 -static struct timeval start_time; +static struct timespec start_time;static char *host_name;char *line_ptr =NULL ;
@@ -80,23 +81,23 @@ const char *fs_compile_out_param[] = {void start_timer (void ) { -gettimeofday (&start_time,NULL ); +clock_gettime (CLOCK_MONOTONIC , &start_time); }double end_timer (void ) { -struct timeval end_time, diff; +struct timespec end_time, diff; -gettimeofday (&end_time,NULL ); +clock_gettime (CLOCK_MONOTONIC , &end_time); diff.tv_sec = end_time.tv_sec - start_time.tv_sec; - diff.tv_usec = end_time.tv_usec - start_time.tv_usec; + diff.tv_nsec = end_time.tv_nsec - start_time.tv_nsec; -if (diff.tv_usec <0 ) { +if (diff.tv_nsec <0 ) { diff.tv_sec--; - diff.tv_usec +=1000000 ; + diff.tv_nsec +=1000000000 ; } -return (((double )diff.tv_sec *1000 ) + ((double )diff.tv_usec /1000 )); +return (((double )diff.tv_sec *1000 ) + ((double )diff.tv_nsec /1000000 )); }char *ether_ntoa_long (const struct ether_addr *addr)
Signature · 1 line
2.8.1
PATCH 3/4 batctl: Add helper to generate instant random bytes
In reply to Sven EckelmannLinux provides different ways to get instant random bytes. These are not all supported on all systems and thus a fallback may have to be used. Abstract all this in a single function which can be used from different parts of the code.
The current implementations are
* get random data from urandom pool via SYS_getrandom syscall
* get random data from reading /dev/urandom
* fallback to per-program prng initialized via the current time (seconds + nanoseconds)
All are tried in this order in hope to get a high quality random number source before falling back to some really low quality one.
--- functions.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ functions.h | 2 ++ 2 files changed, 67 insertions(+)
functions.c +65 -0
@@ -41,6 +41,7 @@#include <linux/if_link.h> #include <linux/rtnetlink.h> #include <linux/neighbour.h> +#include <sys/syscall.h> #include <errno.h> #include <net/if.h> #include <netlink/socket.h>
@@ -1071,3 +1072,67 @@ int check_mesh_iface_ownership(char *mesh_iface, char *hard_iface)return EXIT_SUCCESS ; } + +static int get_random_bytes_syscall(void *buf __unused, size_t buflen __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) +{ +int fd; +ssize_t r; + + 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) +{ +struct timespec now; +static int initialized =0 ; +size_t i; +uint8_t *bufc = buf; + +/* 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); +}
functions.h +2 -0
@@ -53,6 +53,8 @@ int netlink_simple_request(struct nl_msg *msg);int check_mesh_iface (char *mesh_iface);int check_mesh_iface_ownership (char *mesh_iface,char *hard_iface); +void get_random_bytes (void *buf,size_t buflen); +int print_routing_algos (void );extern char *line_ptr;
Signature · 1 line
2.8.1
PATCH 4/4 batctl: Implement non-routing batadv_icmp in userspace
In reply to Sven EckelmannThe current endpoint for batadv_icmp* is implemented in the kernel module and can be accessed via debugfs. But the debugfs cannot be accessed from non-default netns or when debugfs is disabled. Thus it has be possible to use it via the netlink infrastructure to make it compatible with future setups.
The use of the socket file is completely removed and instead raw sockets with BPF filters are used to send/receive batadv_icmp_packet* directly. All information about interfaces and available originators are received via rtnetlink and the batman-adv netlink.
The originators debugfs file is used when the batman-adv netlink commands are not available. The routing of batadv_icmp_packets is still done inside the kernel module.
--- Makefile | 1 + icmp_helper.c | 633 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ icmp_helper.h | 58 ++++++ netlink.c | 178 ++++++++++++++++- netlink.h | 3 + ping.c | 42 ++-- traceroute.c | 42 ++-- 7 files changed, 895 insertions(+), 62 deletions(-) create mode 100644 icmp_helper.c create mode 100644 icmp_helper.h
Makefile +1 -0
@@ -30,6 +30,7 @@ OBJ += debug.o OBJ += functions.o OBJ += genl.o OBJ += hash.o +OBJ += icmp_helper.o OBJ += interface.o OBJ += ioctl.o OBJ += main.o
icmp_helper.c new file +633 -0
633 lines not on this page · raw message
icmp_helper.h new file +58 -0
58 lines not on this page · raw message
netlink.c +175 -3
199 lines not on this page · raw message
netlink.h +3 -0
@@ -47,6 +47,9 @@ int netlink_print_bla_backbone(char *mesh_iface, char *orig_iface, int read_opt,int translate_mac_netlink (const char *mesh_iface,const struct ether_addr *mac,struct ether_addr *mac_out); +int get_nexthop_netlink (const char *mesh_iface,const struct ether_addr *mac, +uint8_t *nexthop,char *ifname); +int get_primarymac_netlink (const char *mesh_iface,uint8_t *primarymac);extern struct nla_policy batadv_netlink_policy[];
ping.c +13 -29
95 lines not on this page · raw message
traceroute.c +12 -30
85 lines not on this page · raw message
Signature · 1 line
2.8.1
Sven Eckelmann
AppliedRe: [B.A.T.M.A.N.] [1/4] batctl: Replace list implementation with linux-like-list
In reply to Sven EckelmannQuoted 12 lines · On Dienstag, 19. Juli 2016 22:44:08 CEST Sven Eckelmann wrote:
The list-batman implementation of linked lists has an interface which tries to resample the linux double linked list implementation but in reality is incompatible. This resulted in code which moves most of the pointer arithmetic back to the actual code. This is rather unfortunate because this abstraction should reduce the pointer arithmetic and not increase it.
Also the incompatibilities between the list implementation used in batman-adv and the one used by batctl made coding features in both components error prone because each component required a slightly different implementation for the same list manipulation.
Signed-off-by Sven Eckelmann <[email protected]>
Quoted 14 lines
--- Makefile | 1 - bisect_iv.c | 40 ++- bisect_iv.h | 10 +- list-batman.c | 123 --------- list-batman.h | 120 --------- list.h | 834 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tcpdump.c | 7 +- tcpdump.h | 2 +- 8 files changed, 860 insertions(+), 277 deletions(-) delete mode 100644 list-batman.c delete mode 100644 list-batman.h create mode 100644 list.h
Applied in 315b3b0fac147b9d8dc8f83a6409fde91eca683a [1].
Kind regards,
Sven
[1] https://git.open-mesh.org/batctl.git/commit/315b3b0fac147b9d8dc8f83a6409fde91eca683a
Sven Eckelmann
AppliedRe: [B.A.T.M.A.N.] [2/4] batctl: Use monotonic time source for icmp timing
In reply to Sven EckelmannQuoted 5 lines · On Dienstag, 19. Juli 2016 22:44:09 CEST Sven Eckelmann wrote:
gettimeofday is not monotonic. It would give wrong results in some situation like leap seconds, when switching between daylight saving time and non-DST, NTP changes the time or when the administrator of the system changes it manually. The function is also obsolete since POSIX.1-2008.
Quoted 8 lines
clock_gettimeis recommended to get a monotonic timesource and as general replacement of gettimeofday.Signed-off-by Sven Eckelmann <[email protected]>--- Makefile | 2 +- functions.c | 17 +++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-)
Applied in ef7cfaa81f72292f67afe50303ac657f41280853 [1].
Kind regards,
Sven
[1] https://git.open-mesh.org/batctl.git/commit/ef7cfaa81f72292f67afe50303ac657f41280853