Hi,
This patchset adds netns/netlink support to batctl. It complements the batman-adv netns/netlink support v10 from July 3th. This series is an update from the v10 sent me on July 3th with the following changes:
* rebased on current master (didn't apply anymore because batman_adv.h changes were extracted from batman-adv and these changes collided with the patchset) * batman_adv.h changes are now separate patches (based on batman-adv patches) * asprintf return code is evaluated for systems not initializing *strp
Kind regards, Sven
Add BATADV_CMD_GET_GATEWAYS commands, using handlers bat_gw_dump in batadv_algo_ops. Will always return -EOPNOTSUPP for now, as no implementations exist yet.
Signed-off-by: Sven Eckelmann sven@narfation.org --- batman_adv.h | 8 ++++++++ 1 file changed, 8 insertions(+)
diff --git a/batman_adv.h b/batman_adv.h index 2e2747f..a13fc09 100644 --- a/batman_adv.h +++ b/batman_adv.h @@ -88,6 +88,9 @@ enum batadv_tt_client_flags { * @BATADV_ATTR_NEIGH_ADDRESS: Neighbour MAC address * @BATADV_ATTR_TQ: TQ to neighbour * @BATADV_ATTR_THROUGHPUT: Estimated throughput to Neighbour + * @BATADV_ATTR_BANDWIDTH_UP: Reported uplink bandwidth + * @BATADV_ATTR_BANDWIDTH_DOWN: Reported downlink bandwidth + * @BATADV_ATTR_ROUTER: Gateway router MAC address * @__BATADV_ATTR_AFTER_LAST: internal use * @NUM_BATADV_ATTR: total number of batadv_nl_attrs available * @BATADV_ATTR_MAX: highest attribute number currently defined @@ -120,6 +123,9 @@ enum batadv_nl_attrs { BATADV_ATTR_NEIGH_ADDRESS, BATADV_ATTR_TQ, BATADV_ATTR_THROUGHPUT, + BATADV_ATTR_BANDWIDTH_UP, + BATADV_ATTR_BANDWIDTH_DOWN, + BATADV_ATTR_ROUTER, /* add attributes above here, update the policy in netlink.c */ __BATADV_ATTR_AFTER_LAST, NUM_BATADV_ATTR = __BATADV_ATTR_AFTER_LAST, @@ -139,6 +145,7 @@ enum batadv_nl_attrs { * @BATADV_CMD_GET_TRANSTABLE_GLOBAL Query list of global translations * @BATADV_CMD_GET_ORIGINATORS: Query list of originators * @BATADV_CMD_GET_NEIGHBORS: Query list of neighbours + * @BATADV_CMD_GET_GATEWAYS: Query list of gateways * @__BATADV_CMD_AFTER_LAST: internal use * @BATADV_CMD_MAX: highest used command number */ @@ -153,6 +160,7 @@ enum batadv_nl_commands { BATADV_CMD_GET_TRANSTABLE_GLOBAL, BATADV_CMD_GET_ORIGINATORS, BATADV_CMD_GET_NEIGHBORS, + BATADV_CMD_GET_GATEWAYS, /* add new commands above here */ __BATADV_CMD_AFTER_LAST, BATADV_CMD_MAX = __BATADV_CMD_AFTER_LAST - 1
From: Andrew Lunn andrew@lunn.ch
Dump the list of bridge loop avoidance claims via the netlink socket.
Signed-off-by: Andrew Lunn andrew@lunn.ch [sven.eckelmann@open-mesh.com: add policy for attributes, fix includes, fix soft_iface reference leak] Signed-off-by: Sven Eckelmann sven.eckelmann@open-mesh.com [sw@simonwunderlich.de: fix kerneldoc, fix error reporting] Signed-off-by: Simon Wunderlich sw@simonwunderlich.de --- batman_adv.h | 12 ++++++++++++ 1 file changed, 12 insertions(+)
diff --git a/batman_adv.h b/batman_adv.h index a13fc09..96b37ab 100644 --- a/batman_adv.h +++ b/batman_adv.h @@ -91,6 +91,11 @@ enum batadv_tt_client_flags { * @BATADV_ATTR_BANDWIDTH_UP: Reported uplink bandwidth * @BATADV_ATTR_BANDWIDTH_DOWN: Reported downlink bandwidth * @BATADV_ATTR_ROUTER: Gateway router MAC address + * @BATADV_ATTR_BLA_OWN: Flag indicating own originator + * @BATADV_ATTR_BLA_ADDRESS: Bridge loop avoidance claim MAC address + * @BATADV_ATTR_BLA_VID: BLA VLAN ID + * @BATADV_ATTR_BLA_BACKBONE: BLA gateway originator MAC address + * @BATADV_ATTR_BLA_CRC: BLA CRC * @__BATADV_ATTR_AFTER_LAST: internal use * @NUM_BATADV_ATTR: total number of batadv_nl_attrs available * @BATADV_ATTR_MAX: highest attribute number currently defined @@ -126,6 +131,11 @@ enum batadv_nl_attrs { BATADV_ATTR_BANDWIDTH_UP, BATADV_ATTR_BANDWIDTH_DOWN, BATADV_ATTR_ROUTER, + BATADV_ATTR_BLA_OWN, + BATADV_ATTR_BLA_ADDRESS, + BATADV_ATTR_BLA_VID, + BATADV_ATTR_BLA_BACKBONE, + BATADV_ATTR_BLA_CRC, /* add attributes above here, update the policy in netlink.c */ __BATADV_ATTR_AFTER_LAST, NUM_BATADV_ATTR = __BATADV_ATTR_AFTER_LAST, @@ -146,6 +156,7 @@ enum batadv_nl_attrs { * @BATADV_CMD_GET_ORIGINATORS: Query list of originators * @BATADV_CMD_GET_NEIGHBORS: Query list of neighbours * @BATADV_CMD_GET_GATEWAYS: Query list of gateways + * @BATADV_CMD_GET_BLA_CLAIM: Query list of bridge loop avoidance claims * @__BATADV_CMD_AFTER_LAST: internal use * @BATADV_CMD_MAX: highest used command number */ @@ -161,6 +172,7 @@ enum batadv_nl_commands { BATADV_CMD_GET_ORIGINATORS, BATADV_CMD_GET_NEIGHBORS, BATADV_CMD_GET_GATEWAYS, + BATADV_CMD_GET_BLA_CLAIM, /* add new commands above here */ __BATADV_CMD_AFTER_LAST, BATADV_CMD_MAX = __BATADV_CMD_AFTER_LAST - 1
From: Simon Wunderlich sw@simonwunderlich.de
Dump the list of bridge loop avoidance backbones via the netlink socket.
Signed-off-by: Simon Wunderlich sw@simonwunderlich.de Signed-off-by: Sven Eckelmann sven@narfation.org --- batman_adv.h | 2 ++ 1 file changed, 2 insertions(+)
diff --git a/batman_adv.h b/batman_adv.h index 96b37ab..734fe83 100644 --- a/batman_adv.h +++ b/batman_adv.h @@ -157,6 +157,7 @@ enum batadv_nl_attrs { * @BATADV_CMD_GET_NEIGHBORS: Query list of neighbours * @BATADV_CMD_GET_GATEWAYS: Query list of gateways * @BATADV_CMD_GET_BLA_CLAIM: Query list of bridge loop avoidance claims + * @BATADV_CMD_GET_BLA_BACKBONE: Query list of bridge loop avoidance backbones * @__BATADV_CMD_AFTER_LAST: internal use * @BATADV_CMD_MAX: highest used command number */ @@ -173,6 +174,7 @@ enum batadv_nl_commands { BATADV_CMD_GET_NEIGHBORS, BATADV_CMD_GET_GATEWAYS, BATADV_CMD_GET_BLA_CLAIM, + BATADV_CMD_GET_BLA_BACKBONE, /* add new commands above here */ __BATADV_CMD_AFTER_LAST, BATADV_CMD_MAX = __BATADV_CMD_AFTER_LAST - 1
From: Andrew Lunn andrew@lunn.ch
The kernel has gained support for exporting some information via netlink. Use this when available, rather than debugfs.
If netlink is not available, or the information is not yet available via netlink, batctl will fall back to debugfs.
Signed-off-by: Andrew Lunn andrew@lunn.ch [sven@narfation.org: Add missing local TTVN in transtable_global, add missing local group id to claim table, fix use-after-free of algo_name, fix alignment of the originator entries] Signed-off-by: Sven Eckelmann sven@narfation.org [sw@simonwunderlich.de: fix claimtable output format, fix name of mandatory claim table attributes, fix printing of VIDs] Signed-off-by: Simon Wunderlich sw@simonwunderlich.de --- debug.c | 22 +- debug.h | 4 +- functions.c | 11 + functions.h | 2 + netlink.c | 1069 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- netlink.h | 18 +- 6 files changed, 1121 insertions(+), 5 deletions(-)
diff --git a/debug.c b/debug.c index 5b280cb..46f03da 100644 --- a/debug.c +++ b/debug.c @@ -23,10 +23,12 @@ #include <unistd.h> #include <stdio.h> #include <stdlib.h> +#include <errno.h>
#include "debug.h" #include "debugfs.h" #include "functions.h" +#include "netlink.h" #include "sys.h"
const struct debug_table_data batctl_debug_tables[BATCTL_TABLE_NUM] = { @@ -35,36 +37,42 @@ const struct debug_table_data batctl_debug_tables[BATCTL_TABLE_NUM] = { .opt_short = "n", .debugfs_name = "neighbors", .header_lines = 2, + .netlink_fn = netlink_print_neighbors, }, { .opt_long = "originators", .opt_short = "o", .debugfs_name = "originators", .header_lines = 2, + .netlink_fn = netlink_print_originators, }, { .opt_long = "gateways", .opt_short = "gwl", .debugfs_name = "gateways", .header_lines = 1, + .netlink_fn = netlink_print_gateways, }, { .opt_long = "translocal", .opt_short = "tl", .debugfs_name = "transtable_local", .header_lines = 2, + .netlink_fn = netlink_print_translocal, }, { .opt_long = "transglobal", .opt_short = "tg", .debugfs_name = "transtable_global", .header_lines = 2, + .netlink_fn = netlink_print_transglobal, }, { .opt_long = "claimtable", .opt_short = "cl", .debugfs_name = "bla_claim_table", .header_lines = 2, + .netlink_fn = netlink_print_bla_claim, }, { .opt_long = "backbonetable", @@ -121,7 +129,7 @@ int handle_debug_table(char *mesh_iface, int debug_table, int argc, char **argv) char *orig_iface = NULL; float orig_timeout = 0.0f; float watch_interval = 1; - opterr = 0; + int err;
while ((optchar = getopt(argc, argv, "hnw:t:Humi:")) != -1) { switch (optchar) { @@ -222,16 +230,26 @@ int handle_debug_table(char *mesh_iface, int debug_table, int argc, char **argv) return EXIT_FAILURE; }
+ if (batctl_debug_tables[debug_table].netlink_fn) { + err = batctl_debug_tables[debug_table].netlink_fn( + mesh_iface, orig_iface, read_opt, orig_timeout, + watch_interval); + if (err != -EOPNOTSUPP) + return err; + } + if (orig_iface) debugfs_make_path(DEBUG_BATIF_PATH_FMT "/", orig_iface, full_path, sizeof(full_path)); else debugfs_make_path(DEBUG_BATIF_PATH_FMT "/", mesh_iface, full_path, sizeof(full_path)); + return read_file(full_path, (char *)batctl_debug_tables[debug_table].debugfs_name, read_opt, orig_timeout, watch_interval, batctl_debug_tables[debug_table].header_lines); }
-int print_routing_algos(void) { +int debug_print_routing_algos(void) +{ char full_path[MAX_PATH+1]; char *debugfs_mnt;
diff --git a/debug.h b/debug.h index 46e8c3c..1c9374a 100644 --- a/debug.h +++ b/debug.h @@ -49,13 +49,15 @@ struct debug_table_data { const char opt_short[OPT_SHORT_MAX_LEN]; const char debugfs_name[DEBUG_TABLE_PATH_MAX_LEN]; size_t header_lines; + int (*netlink_fn)(char *mesh_iface, char *hard_iface, int read_opt, + float orig_timeout, float watch_interval); };
extern const struct debug_table_data batctl_debug_tables[BATCTL_TABLE_NUM];
int handle_debug_table(char *mesh_iface, int debug_table, int argc, char **argv); int log_print(char *mesh_iface, int argc, char **argv); -int print_routing_algos(void); +int debug_print_routing_algos(void); int print_vis_info(char *mesh_iface);
#endif diff --git a/functions.c b/functions.c index 97a6dbd..5b76062 100644 --- a/functions.c +++ b/functions.c @@ -52,6 +52,7 @@ #include "sys.h" #include "debug.h" #include "debugfs.h" +#include "netlink.h"
static struct timeval start_time; static char *host_name; @@ -850,3 +851,13 @@ err:
return arg.vid; } + +int print_routing_algos(void) +{ + int err; + + err = netlink_print_routing_algos(); + if (err == -EOPNOTSUPP) + err = debug_print_routing_algos(); + return err; +} diff --git a/functions.h b/functions.h index d0b05b9..2d29d52 100644 --- a/functions.h +++ b/functions.h @@ -44,6 +44,7 @@ struct ether_addr *translate_mac(char *mesh_iface, struct ether_addr *mac); struct ether_addr *resolve_mac(const char *asc); int vlan_get_link(const char *ifname, char **parent);
+int print_routing_algos(void); extern char *line_ptr;
enum { @@ -59,6 +60,7 @@ enum { SKIP_HEADER = 0x100, UNICAST_ONLY = 0x200, MULTICAST_ONLY = 0x400, + PARSE_ONLY = 0x800, };
#endif diff --git a/netlink.c b/netlink.c index 365cda1..b87964a 100644 --- a/netlink.c +++ b/netlink.c @@ -1,7 +1,7 @@ /* * Copyright (C) 2009-2016 B.A.T.M.A.N. contributors: * - * Marek Lindner mareklindner@neomailbox.ch + * Marek Lindner mareklindner@neomailbox.ch, Andrew Lunn andrew@lunn.ch * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public @@ -22,9 +22,33 @@ #include "netlink.h" #include "main.h"
+#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> #include <net/ethernet.h> +#include <net/if.h> +#include <netlink/netlink.h> +#include <netlink/genl/genl.h> +#include <netlink/genl/ctrl.h> +#include <netinet/in.h> +#include <arpa/inet.h>
+#include "bat-hosts.h" #include "batman_adv.h" +#include "netlink.h" +#include "functions.h" +#include "main.h" +#include "packet.h" + +#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x))) + +struct print_opts { + int read_opt; + float orig_timeout; + float watch_interval; + uint8_t nl_cmd; +};
struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { [BATADV_ATTR_VERSION] = { .type = NLA_STRING }, @@ -49,4 +73,1047 @@ struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { [BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 }, [BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 }, [BATADV_ATTR_PAD] = { .type = NLA_UNSPEC }, + [BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG }, + [BATADV_ATTR_TT_ADDRESS] = { .type = NLA_UNSPEC, + .minlen = ETH_ALEN, + .maxlen = ETH_ALEN }, + [BATADV_ATTR_TT_TTVN] = { .type = NLA_U8 }, + [BATADV_ATTR_TT_LAST_TTVN] = { .type = NLA_U8 }, + [BATADV_ATTR_TT_CRC32] = { .type = NLA_U32 }, + [BATADV_ATTR_TT_VID] = { .type = NLA_U16 }, + [BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 }, + [BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG }, + [BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 }, + [BATADV_ATTR_NEIGH_ADDRESS] = { .type = NLA_UNSPEC, + .minlen = ETH_ALEN, + .maxlen = ETH_ALEN }, + [BATADV_ATTR_TQ] = { .type = NLA_U8 }, + [BATADV_ATTR_THROUGHPUT] = { .type = NLA_U32 }, + [BATADV_ATTR_BANDWIDTH_UP] = { .type = NLA_U32 }, + [BATADV_ATTR_BANDWIDTH_DOWN] = { .type = NLA_U32 }, + [BATADV_ATTR_ROUTER] = { .type = NLA_UNSPEC, + .minlen = ETH_ALEN, + .maxlen = ETH_ALEN }, + [BATADV_ATTR_BLA_OWN] = { .type = NLA_FLAG }, + [BATADV_ATTR_BLA_ADDRESS] = { .type = NLA_UNSPEC, + .minlen = ETH_ALEN, + .maxlen = ETH_ALEN }, + [BATADV_ATTR_BLA_VID] = { .type = NLA_U16 }, + [BATADV_ATTR_BLA_BACKBONE] = { .type = NLA_UNSPEC, + .minlen = ETH_ALEN, + .maxlen = ETH_ALEN }, + [BATADV_ATTR_BLA_CRC] = { .type = NLA_U16 }, +}; + +static int last_err; +static char algo_name_buf[256] = ""; + +static int missing_mandatory_attrs(struct nlattr *attrs[], + const int mandatory[], int num) +{ + int i; + + for (i = 0; i < num; i++) + if (!attrs[mandatory[i]]) + return -EINVAL; + + return 0; +} + +static int print_error(struct sockaddr_nl *nla __unused, + struct nlmsgerr *nlerr, + void *arg __unused) +{ + if (nlerr->error != -EOPNOTSUPP) + fprintf(stderr, "Error received: %s\n", + strerror(-nlerr->error)); + + last_err = -nlerr->error; + + return NL_STOP; +} + +static int stop_callback(struct nl_msg *msg, void *arg __unused) +{ + struct nlmsghdr *nlh = nlmsg_hdr(msg); + int *error = nlmsg_data(nlh); + + if (*error) + fprintf(stderr, "Error received: %s\n", strerror(-*error)); + + return NL_STOP; +} + +static const int info_mandatory[] = { + BATADV_ATTR_MESH_IFINDEX, + BATADV_ATTR_MESH_IFNAME, +}; + +static const int info_hard_mandatory[] = { + BATADV_ATTR_VERSION, + BATADV_ATTR_ALGO_NAME, + BATADV_ATTR_HARD_IFNAME, + BATADV_ATTR_HARD_ADDRESS, +}; + +static int info_callback(struct nl_msg *msg, void *arg) +{ + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct print_opts *opts = arg; + const uint8_t *primary_mac; + struct genlmsghdr *ghdr; + const uint8_t *mesh_mac; + const char *primary_if; + const char *mesh_name; + const char *version; + char *extra_info = NULL; + uint8_t ttvn = 0; + uint16_t bla_group_id = 0; + const char *algo_name; + int ret; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_MESH_INFO) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + if (missing_mandatory_attrs(attrs, info_mandatory, + ARRAY_SIZE(info_mandatory))) { + fputs("Missing attributes from kernel\n", stderr); + exit(1); + } + + mesh_name = nla_get_string(attrs[BATADV_ATTR_MESH_IFNAME]); + mesh_mac = nla_data(attrs[BATADV_ATTR_MESH_ADDRESS]); + + if (attrs[BATADV_ATTR_HARD_IFNAME]) { + if (missing_mandatory_attrs(attrs, info_hard_mandatory, + ARRAY_SIZE(info_hard_mandatory))) { + fputs("Missing attributes from kernel\n", + stderr); + exit(1); + } + + version = nla_get_string(attrs[BATADV_ATTR_VERSION]); + algo_name = nla_get_string(attrs[BATADV_ATTR_ALGO_NAME]); + primary_if = nla_get_string(attrs[BATADV_ATTR_HARD_IFNAME]); + primary_mac = nla_data(attrs[BATADV_ATTR_HARD_ADDRESS]); + + snprintf(algo_name_buf, sizeof(algo_name_buf), "%s", algo_name); + + if (attrs[BATADV_ATTR_TT_TTVN]) + ttvn = nla_get_u8(attrs[BATADV_ATTR_TT_TTVN]); + + if (attrs[BATADV_ATTR_BLA_CRC]) + bla_group_id = nla_get_u16(attrs[BATADV_ATTR_BLA_CRC]); + + if (!(opts->read_opt & PARSE_ONLY)) { + switch (opts->nl_cmd) { + case BATADV_CMD_GET_TRANSTABLE_LOCAL: + ret = asprintf(&extra_info, ", TTVN: %u", ttvn); + if (ret < 0) + extra_info = NULL; + break; + case BATADV_CMD_GET_BLA_CLAIM: + ret = asprintf(&extra_info, ", group id: 0x%04x", + bla_group_id); + if (ret < 0) + extra_info = NULL; + break; + default: + extra_info = strdup(""); + break; + } + + printf("[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%02x:%02x:%02x:%02x:%02x:%02x (%s/%02x:%02x:%02x:%02x:%02x:%02x %s)%s]\n", + version, primary_if, + primary_mac[0], primary_mac[1], primary_mac[2], + primary_mac[3], primary_mac[4], primary_mac[5], + mesh_name, + mesh_mac[0], mesh_mac[1], mesh_mac[2], + mesh_mac[3], mesh_mac[4], mesh_mac[5], + algo_name, extra_info); + + if (extra_info) + free(extra_info); + } + } else { + if (!(opts->read_opt & PARSE_ONLY)) + printf("BATMAN mesh %s disabled\n", mesh_name); + } + + return NL_STOP; +} + +static void netlink_print_info(int ifindex, uint8_t nl_cmd, int read_opt) +{ + struct nl_sock *sock; + struct nl_msg *msg; + struct nl_cb *cb; + int family; + struct print_opts opts = { + .read_opt = read_opt, + .nl_cmd = nl_cmd, + }; + + sock = nl_socket_alloc(); + genl_connect(sock); + + family = genl_ctrl_resolve(sock, BATADV_NL_NAME); + if (family < 0) + return; + + msg = nlmsg_alloc(); + genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, 0, + BATADV_CMD_GET_MESH_INFO, 1); + + nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, ifindex); + + nl_send_auto_complete(sock, msg); + + nlmsg_free(msg); + + cb = nl_cb_alloc(NL_CB_DEFAULT); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, info_callback, &opts); + nl_cb_err(cb, NL_CB_CUSTOM, print_error, NULL); + + nl_recvmsgs(sock, cb); + + nl_socket_free(sock); +} + +static const int routing_algos_mandatory[] = { + BATADV_ATTR_ALGO_NAME, +}; + +static int routing_algos_callback(struct nl_msg *msg, void *arg __unused) +{ + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct genlmsghdr *ghdr; + const char *algo_name; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_ROUTING_ALGOS) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + if (missing_mandatory_attrs(attrs, routing_algos_mandatory, + ARRAY_SIZE(routing_algos_mandatory))) { + fputs("Missing attributes from kernel\n", stderr); + exit(1); + } + + algo_name = nla_get_string(attrs[BATADV_ATTR_ALGO_NAME]); + + printf(" * %s\n", algo_name); + + return NL_OK; +} + +int netlink_print_routing_algos(void) +{ + struct nl_sock *sock; + struct nl_msg *msg; + struct nl_cb *cb; + int family; + + sock = nl_socket_alloc(); + genl_connect(sock); + + family = genl_ctrl_resolve(sock, BATADV_NL_NAME); + if (family < 0) + return -EOPNOTSUPP; + + msg = nlmsg_alloc(); + genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_DUMP, + BATADV_CMD_GET_ROUTING_ALGOS, 1); + + nl_send_auto_complete(sock, msg); + + nlmsg_free(msg); + + cb = nl_cb_alloc(NL_CB_DEFAULT); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, routing_algos_callback, + NULL); + nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, stop_callback, NULL); + nl_cb_err(cb, NL_CB_CUSTOM, print_error, NULL); + + printf("Available routing algorithms:\n"); + + nl_recvmsgs(sock, cb); + + nl_socket_free(sock); + + return 0; +} + +static const int originators_mandatory[] = { + BATADV_ATTR_ORIG_ADDRESS, + BATADV_ATTR_NEIGH_ADDRESS, + BATADV_ATTR_HARD_IFINDEX, + BATADV_ATTR_LAST_SEEN_MSECS, +}; + +static int originators_callback(struct nl_msg *msg, void *arg) +{ + unsigned throughput_mbits, throughput_kbits; + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + int last_seen_msecs, last_seen_secs; + struct print_opts *opts = arg; + struct bat_host *bat_host; + struct genlmsghdr *ghdr; + char ifname[IF_NAMESIZE]; + float last_seen; + uint8_t *neigh; + uint8_t *orig; + char c = ' '; + uint8_t tq; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_ORIGINATORS) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + if (missing_mandatory_attrs(attrs, originators_mandatory, + ARRAY_SIZE(originators_mandatory))) { + fputs("Missing attributes from kernel\n", stderr); + exit(1); + } + + orig = nla_data(attrs[BATADV_ATTR_ORIG_ADDRESS]); + neigh = nla_data(attrs[BATADV_ATTR_NEIGH_ADDRESS]); + + if (!if_indextoname(nla_get_u32(attrs[BATADV_ATTR_HARD_IFINDEX]), + ifname)) + ifname[0] = '\0'; + + if (attrs[BATADV_ATTR_FLAG_BEST]) + c = '*'; + + last_seen_msecs = nla_get_u32(attrs[BATADV_ATTR_LAST_SEEN_MSECS]); + last_seen = (float)last_seen_msecs / 1000.0; + last_seen_secs = last_seen_msecs / 1000; + last_seen_msecs = last_seen_msecs % 1000; + + /* skip timed out originators */ + if (opts->read_opt & NO_OLD_ORIGS) + if (last_seen > opts->orig_timeout) + return NL_OK; + + if (attrs[BATADV_ATTR_THROUGHPUT]) { + throughput_kbits = nla_get_u32(attrs[BATADV_ATTR_THROUGHPUT]); + throughput_mbits = throughput_kbits / 1000; + throughput_kbits = throughput_kbits % 1000; + + if (!(opts->read_opt & USE_BAT_HOSTS)) { + printf(" %c %02x:%02x:%02x:%02x:%02x:%02x %4i.%03is (%9u.%1u) %02x:%02x:%02x:%02x:%02x:%02x [%10s]\n", + c, + orig[0], orig[1], orig[2], + orig[3], orig[4], orig[5], + last_seen_secs, last_seen_msecs, + throughput_mbits, throughput_kbits / 100, + neigh[0], neigh[1], neigh[2], + neigh[3], neigh[4], neigh[5], + ifname); + } else { + bat_host = bat_hosts_find_by_mac((char *)orig); + if (bat_host) + printf(" %c %17s ", c, bat_host->name); + else + printf(" %c %02x:%02x:%02x:%02x:%02x:%02x ", + c, + orig[0], orig[1], orig[2], + orig[3], orig[4], orig[5]); + printf("%4i.%03is (%9u.%1u) ", + last_seen_secs, last_seen_msecs, + throughput_mbits, throughput_kbits / 100); + bat_host = bat_hosts_find_by_mac((char *)neigh); + if (bat_host) + printf(" %c %17s ", c, bat_host->name); + else + printf(" %02x:%02x:%02x:%02x:%02x:%02x ", + neigh[0], neigh[1], neigh[2], + neigh[3], neigh[4], neigh[5]); + printf("[%10s]\n", ifname); + } + } + if (attrs[BATADV_ATTR_TQ]) { + tq = nla_get_u8(attrs[BATADV_ATTR_TQ]); + + if (!(opts->read_opt & USE_BAT_HOSTS)) { + printf(" %c %02x:%02x:%02x:%02x:%02x:%02x %4i.%03is (%3i) %02x:%02x:%02x:%02x:%02x:%02x [%10s]\n", + c, + orig[0], orig[1], orig[2], + orig[3], orig[4], orig[5], + last_seen_secs, last_seen_msecs, tq, + neigh[0], neigh[1], neigh[2], + neigh[3], neigh[4], neigh[5], + ifname); + } else { + bat_host = bat_hosts_find_by_mac((char *)orig); + if (bat_host) + printf(" %c %17s ", c, bat_host->name); + else + printf(" %c %02x:%02x:%02x:%02x:%02x:%02x ", + c, + orig[0], orig[1], orig[2], + orig[3], orig[4], orig[5]); + printf("%4i.%03is (%3i) ", + last_seen_secs, last_seen_msecs, tq); + bat_host = bat_hosts_find_by_mac((char *)neigh); + if (bat_host) + printf("%17s ", bat_host->name); + else + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + neigh[0], neigh[1], neigh[2], + neigh[3], neigh[4], neigh[5]); + printf("[%10s]\n", ifname); + } + } + + return NL_OK; +} + +static const int neighbors_mandatory[] = { + BATADV_ATTR_NEIGH_ADDRESS, + BATADV_ATTR_HARD_IFINDEX, + BATADV_ATTR_LAST_SEEN_MSECS, }; + +static int neighbors_callback(struct nl_msg *msg, void *arg) +{ + unsigned throughput_mbits, throughput_kbits; + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + int last_seen_msecs, last_seen_secs; + struct print_opts *opts = arg; + struct bat_host *bat_host; + char ifname[IF_NAMESIZE]; + struct genlmsghdr *ghdr; + uint8_t *neigh; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_NEIGHBORS) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + if (missing_mandatory_attrs(attrs, neighbors_mandatory, + ARRAY_SIZE(neighbors_mandatory))) { + fputs("Missing attributes from kernel\n", stderr); + exit(1); + } + + neigh = nla_data(attrs[BATADV_ATTR_NEIGH_ADDRESS]); + bat_host = bat_hosts_find_by_mac((char *)neigh); + + if (!if_indextoname(nla_get_u32(attrs[BATADV_ATTR_HARD_IFINDEX]), + ifname)) + ifname[0] = '\0'; + + last_seen_msecs = nla_get_u32(attrs[BATADV_ATTR_LAST_SEEN_MSECS]); + last_seen_secs = last_seen_msecs / 1000; + last_seen_msecs = last_seen_msecs % 1000; + + if (attrs[BATADV_ATTR_THROUGHPUT]) { + throughput_kbits = nla_get_u32(attrs[BATADV_ATTR_THROUGHPUT]); + throughput_mbits = throughput_kbits / 1000; + throughput_kbits = throughput_kbits % 1000; + + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + neigh[0], neigh[1], neigh[2], + neigh[3], neigh[4], neigh[5]); + else + printf("%17s ", bat_host->name); + + printf("%4i.%03is (%9u.%1u) [%10s]\n", + last_seen_secs, last_seen_msecs, + throughput_mbits, throughput_kbits / 100, + ifname); + } else { + printf(" %10s ", ifname); + + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + neigh[0], neigh[1], neigh[2], + neigh[3], neigh[4], neigh[5]); + else + printf("%17s ", bat_host->name); + + printf("%4i.%03is\n", last_seen_secs, last_seen_msecs); + } + + return NL_OK; +} + +static const int transglobal_mandatory[] = { + BATADV_ATTR_TT_ADDRESS, + BATADV_ATTR_ORIG_ADDRESS, + BATADV_ATTR_TT_VID, + BATADV_ATTR_TT_TTVN, + BATADV_ATTR_TT_LAST_TTVN, + BATADV_ATTR_TT_CRC32, + BATADV_ATTR_TT_FLAGS, +}; + +static int transglobal_callback(struct nl_msg *msg, void *arg) +{ + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct print_opts *opts = arg; + struct bat_host *bat_host; + struct genlmsghdr *ghdr; + char c, r, w, i, t; + uint8_t last_ttvn; + uint32_t crc32; + uint32_t flags; + uint8_t *addr; + uint8_t *orig; + uint8_t ttvn; + int16_t vid; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_TRANSTABLE_GLOBAL) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + if (missing_mandatory_attrs(attrs, transglobal_mandatory, + ARRAY_SIZE(transglobal_mandatory))) { + fputs("Missing attributes from kernel\n", stderr); + exit(1); + } + + addr = nla_data(attrs[BATADV_ATTR_TT_ADDRESS]); + orig = nla_data(attrs[BATADV_ATTR_ORIG_ADDRESS]); + vid = nla_get_u16(attrs[BATADV_ATTR_TT_VID]); + ttvn = nla_get_u8(attrs[BATADV_ATTR_TT_TTVN]); + last_ttvn = nla_get_u8(attrs[BATADV_ATTR_TT_LAST_TTVN]); + crc32 = nla_get_u32(attrs[BATADV_ATTR_TT_CRC32]); + flags = nla_get_u32(attrs[BATADV_ATTR_TT_FLAGS]); + + if (opts->read_opt & MULTICAST_ONLY && !(addr[0] & 0x01)) + return NL_OK; + + if (opts->read_opt & UNICAST_ONLY && (addr[0] & 0x01)) + return NL_OK; + + c = ' ', r = '.', w = '.', i = '.', t = '.'; + if (attrs[BATADV_ATTR_FLAG_BEST]) + c = '*'; + if (flags & BATADV_TT_CLIENT_ROAM) + r = 'R'; + if (flags & BATADV_TT_CLIENT_WIFI) + w = 'W'; + if (flags & BATADV_TT_CLIENT_ISOLA) + i = 'I'; + if (flags & BATADV_TT_CLIENT_TEMP) + t = 'T'; + + printf(" %c ", c); + + bat_host = bat_hosts_find_by_mac((char *)addr); + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + addr[0], addr[1], addr[2], + addr[3], addr[4], addr[5]); + else + printf("%17s ", bat_host->name); + + printf("%4i [%c%c%c%c] (%3u) ", + BATADV_PRINT_VID(vid), r, w, i, t, ttvn); + + bat_host = bat_hosts_find_by_mac((char *)orig); + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + orig[0], orig[1], orig[2], + orig[3], orig[4], orig[5]); + else + printf("%17s ", bat_host->name); + + printf("(%3u) (0x%.8x)\n", + last_ttvn, crc32); + + return NL_OK; +} + +static const int translocal_mandatory[] = { + BATADV_ATTR_TT_ADDRESS, + BATADV_ATTR_TT_VID, + BATADV_ATTR_TT_CRC32, + BATADV_ATTR_TT_FLAGS, +}; + +static int translocal_callback(struct nl_msg *msg, void *arg) +{ + int last_seen_msecs = 0, last_seen_secs = 0; + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct print_opts *opts = arg; + struct bat_host *bat_host; + struct genlmsghdr *ghdr; + char r, p, n, x, w, i; + uint8_t *addr; + int16_t vid; + uint32_t crc32; + uint32_t flags; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_TRANSTABLE_LOCAL) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + if (missing_mandatory_attrs(attrs, translocal_mandatory, + ARRAY_SIZE(translocal_mandatory))) { + fputs("Missing attributes from kernel\n", stderr); + exit(1); + } + + addr = nla_data(attrs[BATADV_ATTR_TT_ADDRESS]); + vid = nla_get_u16(attrs[BATADV_ATTR_TT_VID]); + crc32 = nla_get_u32(attrs[BATADV_ATTR_TT_CRC32]); + flags = nla_get_u32(attrs[BATADV_ATTR_TT_FLAGS]); + last_seen_msecs = 0, last_seen_secs = 0; + + if (opts->read_opt & MULTICAST_ONLY && !(addr[0] & 0x01)) + return NL_OK; + + if (opts->read_opt & UNICAST_ONLY && (addr[0] & 0x01)) + return NL_OK; + + r = '.', p = '.', n = '.', x = '.', w = '.', i = '.'; + if (flags & BATADV_TT_CLIENT_ROAM) + r = 'R'; + if (flags & BATADV_TT_CLIENT_NEW) + n = 'N'; + if (flags & BATADV_TT_CLIENT_PENDING) + x = 'X'; + if (flags & BATADV_TT_CLIENT_WIFI) + w = 'W'; + if (flags & BATADV_TT_CLIENT_ISOLA) + i = 'I'; + + if (flags & BATADV_TT_CLIENT_NOPURGE) { + p = 'P'; + } else { + if (!attrs[BATADV_ATTR_LAST_SEEN_MSECS]) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + last_seen_msecs = nla_get_u32( + attrs[BATADV_ATTR_LAST_SEEN_MSECS]); + last_seen_secs = last_seen_msecs / 1000; + last_seen_msecs = last_seen_msecs % 1000; + } + + bat_host = bat_hosts_find_by_mac((char *)addr); + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + addr[0], addr[1], addr[2], + addr[3], addr[4], addr[5]); + else + printf("%17s ", bat_host->name); + + printf("%4i [%c%c%c%c%c%c] %3u.%03u (0x%.8x)\n", + BATADV_PRINT_VID(vid), r, p, n, x, w, i, + last_seen_secs, last_seen_msecs, + crc32); + + return NL_OK; +} + +static const int gateways_mandatory[] = { + BATADV_ATTR_ORIG_ADDRESS, + BATADV_ATTR_TQ, + BATADV_ATTR_ROUTER, + BATADV_ATTR_HARD_IFNAME, + BATADV_ATTR_BANDWIDTH_DOWN, + BATADV_ATTR_BANDWIDTH_UP, +}; + +static int gateways_callback(struct nl_msg *msg, void *arg) +{ + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct print_opts *opts = arg; + struct bat_host *bat_host; + struct genlmsghdr *ghdr; + const char *primary_if; + uint32_t bandwidth_down; + uint32_t bandwidth_up; + uint8_t *router; + uint8_t *orig; + char c = ' '; + uint8_t tq; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_GATEWAYS) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + if (missing_mandatory_attrs(attrs, gateways_mandatory, + ARRAY_SIZE(gateways_mandatory))) { + fputs("Missing attributes from kernel\n", stderr); + exit(1); + } + + if (attrs[BATADV_ATTR_FLAG_BEST]) + c = '*'; + + orig = nla_data(attrs[BATADV_ATTR_ORIG_ADDRESS]); + tq = nla_get_u8(attrs[BATADV_ATTR_TQ]); + router = nla_data(attrs[BATADV_ATTR_ROUTER]); + primary_if = nla_get_string(attrs[BATADV_ATTR_HARD_IFNAME]); + bandwidth_down = nla_get_u32(attrs[BATADV_ATTR_BANDWIDTH_DOWN]); + bandwidth_up = nla_get_u32(attrs[BATADV_ATTR_BANDWIDTH_UP]); + + printf("%c ", c); + + bat_host = bat_hosts_find_by_mac((char *)orig); + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + orig[0], orig[1], orig[2], + orig[3], orig[4], orig[5]); + else + printf("%17s ", bat_host->name); + + printf("(%3i) ", tq); + + bat_host = bat_hosts_find_by_mac((char *)router); + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + router[0], router[1], router[2], + router[3], router[4], router[5]); + else + printf("%17s ", bat_host->name); + + printf("[%10s]: %u.%u/%u.%u MBit\n", + primary_if, bandwidth_down / 10, bandwidth_down % 10, + bandwidth_up / 10, bandwidth_up % 10); + + return NL_OK; +} + +static const int bla_claim_mandatory[] = { + BATADV_ATTR_BLA_ADDRESS, + BATADV_ATTR_BLA_VID, + BATADV_ATTR_BLA_BACKBONE, + BATADV_ATTR_BLA_CRC, +}; + +static int bla_claim_callback(struct nl_msg *msg, void *arg) +{ + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct print_opts *opts = arg; + struct bat_host *bat_host; + struct genlmsghdr *ghdr; + uint16_t backbone_crc; + uint8_t *backbone; + uint8_t *client; + uint16_t vid; + char c = ' '; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_BLA_CLAIM) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + if (missing_mandatory_attrs(attrs, bla_claim_mandatory, + ARRAY_SIZE(bla_claim_mandatory))) { + fputs("Missing attributes from kernel\n", stderr); + exit(1); + } + + if (attrs[BATADV_ATTR_BLA_OWN]) + c = '*'; + + client = nla_data(attrs[BATADV_ATTR_BLA_ADDRESS]); + vid = nla_get_u16(attrs[BATADV_ATTR_BLA_VID]); + backbone = nla_data(attrs[BATADV_ATTR_BLA_BACKBONE]); + backbone_crc = nla_get_u16(attrs[BATADV_ATTR_BLA_CRC]); + + bat_host = bat_hosts_find_by_mac((char *)client); + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + client[0], client[1], client[2], + client[3], client[4], client[5]); + else + printf("%17s ", bat_host->name); + + printf("on %5d by ", BATADV_PRINT_VID(vid)); + + bat_host = bat_hosts_find_by_mac((char *)backbone); + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + backbone[0], backbone[1], backbone[2], + backbone[3], backbone[4], backbone[5]); + else + printf("%17s ", bat_host->name); + + printf("[%c] (0x%04x)\n", c, backbone_crc); + + return NL_OK; +} + +static int netlink_print_common(char *mesh_iface, char *orig_iface, + int read_opt, float orig_timeout, + float watch_interval, const char *header, + uint8_t nl_cmd, nl_recvmsg_msg_cb_t callback) +{ + struct print_opts opts = { + .read_opt = read_opt, + .orig_timeout = orig_timeout, + .watch_interval = watch_interval + }; + int hardifindex = 0; + struct nl_sock *sock; + struct nl_msg *msg; + struct nl_cb *cb; + int ifindex; + int family; + + sock = nl_socket_alloc(); + genl_connect(sock); + + family = genl_ctrl_resolve(sock, BATADV_NL_NAME); + if (family < 0) + return -EOPNOTSUPP; + + ifindex = if_nametoindex(mesh_iface); + if (!ifindex) { + fprintf(stderr, "Interface %s is unknown\n", mesh_iface); + return -ENODEV; + } + + if (orig_iface) { + hardifindex = if_nametoindex(orig_iface); + if (!hardifindex) { + fprintf(stderr, "Interface %s is unknown\n", + orig_iface); + return -ENODEV; + } + } + + bat_hosts_init(read_opt); + + cb = nl_cb_alloc(NL_CB_DEFAULT); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, callback, &opts); + nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, stop_callback, NULL); + nl_cb_err(cb, NL_CB_CUSTOM, print_error, NULL); + + do { + if (read_opt & CLR_CONT_READ) + /* clear screen, set cursor back to 0,0 */ + printf("\033[2J\033[0;0f"); + + if (!(read_opt & SKIP_HEADER)) { + netlink_print_info(ifindex, nl_cmd, 0); + if (header) + printf("%s", header); + } + + msg = nlmsg_alloc(); + genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, + NLM_F_DUMP, nl_cmd, 1); + + nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, ifindex); + if (hardifindex) + nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX, + hardifindex); + + nl_send_auto_complete(sock, msg); + + nlmsg_free(msg); + + last_err = 0; + nl_recvmsgs(sock, cb); + if (!last_err && read_opt & (CONT_READ|CLR_CONT_READ)) + usleep(1000000 * watch_interval); + + } while (!last_err && read_opt & (CONT_READ|CLR_CONT_READ)); + + bat_hosts_free(); + + nl_socket_free(sock); + + return last_err; +} + +int netlink_print_originators(char *mesh_iface, char *orig_iface, + int read_opts, float orig_timeout, + float watch_interval) +{ + char *header; + int ifindex; + + ifindex = if_nametoindex(mesh_iface); + if (!ifindex) { + fprintf(stderr, "Interface %s is unknown\n", mesh_iface); + return -ENODEV; + } + + netlink_print_info(ifindex, BATADV_CMD_GET_ORIGINATORS, PARSE_ONLY); + + if (strlen(algo_name_buf) == 0) + return -EINVAL; + + if (!strcmp("BATMAN_IV", algo_name_buf)) + header = " Originator last-seen (#/255) Nexthop [outgoingIF]\n"; + if (!strcmp("BATMAN_V", algo_name_buf)) + header = " Originator last-seen ( throughput) Nexthop [outgoingIF]\n"; + + if (!header) + return -EINVAL; + + return netlink_print_common(mesh_iface, orig_iface, read_opts, + orig_timeout, watch_interval, header, + BATADV_CMD_GET_ORIGINATORS, + originators_callback); +} + +int netlink_print_neighbors(char *mesh_iface, char *orig_iface, int read_opts, + float orig_timeout, + float watch_interval) +{ + return netlink_print_common(mesh_iface, orig_iface, read_opts, + orig_timeout, watch_interval, + "IF Neighbor last-seen\n", + BATADV_CMD_GET_NEIGHBORS, + neighbors_callback); +} + +int netlink_print_transglobal(char *mesh_iface, char *orig_iface, + int read_opts, float orig_timeout, + float watch_interval) +{ + return netlink_print_common(mesh_iface, orig_iface, read_opts, + orig_timeout, watch_interval, + " Client VID Flags Last ttvn Via ttvn (CRC )\n", + BATADV_CMD_GET_TRANSTABLE_GLOBAL, + transglobal_callback); +} + +int netlink_print_translocal(char *mesh_iface, char *orig_iface, int read_opts, + float orig_timeout, + float watch_interval) +{ + return netlink_print_common(mesh_iface, orig_iface, read_opts, + orig_timeout, watch_interval, + "Client VID Flags Last seen (CRC )\n", + BATADV_CMD_GET_TRANSTABLE_LOCAL, + translocal_callback); +} + +int netlink_print_gateways(char *mesh_iface, char *orig_iface, int read_opts, + float orig_timeout, + float watch_interval) +{ + return netlink_print_common(mesh_iface, orig_iface, read_opts, + orig_timeout, watch_interval, + " Router TQ Next Hop outgoingIf Bandwidth\n", + BATADV_CMD_GET_GATEWAYS, + gateways_callback); +} + +int netlink_print_bla_claim(char *mesh_iface, char *orig_iface, int read_opts, + float orig_timeout, + float watch_interval) +{ + return netlink_print_common(mesh_iface, orig_iface, read_opts, + orig_timeout, watch_interval, + "Client VID Originator [o] (CRC )\n", + BATADV_CMD_GET_BLA_CLAIM, + bla_claim_callback); +} diff --git a/netlink.h b/netlink.h index 0a4d3dd..69f0958 100644 --- a/netlink.h +++ b/netlink.h @@ -1,7 +1,7 @@ /* * Copyright (C) 2009-2016 B.A.T.M.A.N. contributors: * - * Marek Lindner mareklindner@neomailbox.ch + * Marek Lindner mareklindner@neomailbox.ch, Andrew Lunn andrew@lunn.ch * * This program is free software; you can redistribute it and/or * modify it under the terms of version 2 of the GNU General Public @@ -25,6 +25,22 @@ #include <netlink/genl/genl.h> #include <netlink/genl/ctrl.h>
+int netlink_print_routing_algos(void); +int netlink_print_originators(char *mesh_iface, char *orig_iface, int read_opt, + float orig_timeout, float watch_interval); +int netlink_print_neighbors(char *mesh_iface, char *orig_iface, int read_opt, + float orig_timeout, float watch_interval); +int netlink_print_gateways(char *mesh_iface, char *orig_iface, int read_opt, + float orig_timeout, float watch_interval); +int netlink_print_transglobal(char *mesh_iface, char *orig_iface, int read_opt, + float orig_timeout, float watch_interval); +int netlink_print_translocal(char *mesh_iface, char *orig_iface, int read_opt, + float orig_timeout, float watch_interval); +int netlink_print_gateways(char *mesh_iface, char *orig_iface, int read_opt, + float orig_timeout, float watch_interval); +int netlink_print_bla_claim(char *mesh_iface, char *orig_iface, int read_opt, + float orig_timeout, float watch_interval); + extern struct nla_policy batadv_netlink_policy[];
#endif /* _BATCTL_NETLINK_H */
On Monday, July 18, 2016 12:12:01 Sven Eckelmann wrote:
From: Andrew Lunn andrew@lunn.ch
The kernel has gained support for exporting some information via netlink. Use this when available, rather than debugfs.
If netlink is not available, or the information is not yet available via netlink, batctl will fall back to debugfs.
Signed-off-by: Andrew Lunn andrew@lunn.ch [sven@narfation.org: Add missing local TTVN in transtable_global, add missing local group id to claim table, fix use-after-free of algo_name, fix alignment of the originator entries] Signed-off-by: Sven Eckelmann sven@narfation.org [sw@simonwunderlich.de: fix claimtable output format, fix name of mandatory claim table attributes, fix printing of VIDs] Signed-off-by: Simon Wunderlich sw@simonwunderlich.de
debug.c | 22 +- debug.h | 4 +- functions.c | 11 + functions.h | 2 + netlink.c | 1069 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- netlink.h | 18 +- 6 files changed, 1121 insertions(+), 5 deletions(-)
Applied in revision d8dd1ff.
Thanks, Marek
From: Simon Wunderlich sw@simonwunderlich.de
Signed-off-by: Simon Wunderlich sw@simonwunderlich.de Signed-off-by: Sven Eckelmann sven@narfation.org --- debug.c | 1 + netlink.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ netlink.h | 2 ++ 3 files changed, 82 insertions(+)
diff --git a/debug.c b/debug.c index 46f03da..ddabbfb 100644 --- a/debug.c +++ b/debug.c @@ -79,6 +79,7 @@ const struct debug_table_data batctl_debug_tables[BATCTL_TABLE_NUM] = { .opt_short = "bbt", .debugfs_name = "bla_backbone_table", .header_lines = 2, + .netlink_fn = netlink_print_bla_backbone, }, { .opt_long = "dat_cache", diff --git a/netlink.c b/netlink.c index b87964a..1e406a9 100644 --- a/netlink.c +++ b/netlink.c @@ -226,6 +226,7 @@ static int info_callback(struct nl_msg *msg, void *arg) if (ret < 0) extra_info = NULL; break; + case BATADV_CMD_GET_BLA_BACKBONE: case BATADV_CMD_GET_BLA_CLAIM: ret = asprintf(&extra_info, ", group id: 0x%04x", bla_group_id); @@ -947,6 +948,74 @@ static int bla_claim_callback(struct nl_msg *msg, void *arg) return NL_OK; }
+static const int bla_backbone_mandatory[] = { + BATADV_ATTR_BLA_VID, + BATADV_ATTR_BLA_BACKBONE, + BATADV_ATTR_BLA_CRC, + BATADV_ATTR_LAST_SEEN_MSECS, +}; + +static int bla_backbone_callback(struct nl_msg *msg, void *arg) +{ + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + int last_seen_msecs, last_seen_secs; + struct print_opts *opts = arg; + struct bat_host *bat_host; + struct genlmsghdr *ghdr; + uint16_t backbone_crc; + uint8_t *backbone; + uint16_t vid; + + if (!genlmsg_valid_hdr(nlh, 0)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_BLA_BACKBONE) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + fputs("Received invalid data from kernel.\n", stderr); + exit(1); + } + + if (missing_mandatory_attrs(attrs, bla_backbone_mandatory, + ARRAY_SIZE(bla_backbone_mandatory))) { + fputs("Missing attributes from kernel\n", stderr); + exit(1); + } + + /* don't show own backbones */ + if (attrs[BATADV_ATTR_BLA_OWN]) + return NL_OK; + + vid = nla_get_u16(attrs[BATADV_ATTR_BLA_VID]); + backbone = nla_data(attrs[BATADV_ATTR_BLA_BACKBONE]); + backbone_crc = nla_get_u16(attrs[BATADV_ATTR_BLA_CRC]); + + last_seen_msecs = nla_get_u32(attrs[BATADV_ATTR_LAST_SEEN_MSECS]); + last_seen_secs = last_seen_msecs / 1000; + last_seen_msecs = last_seen_msecs % 1000; + + bat_host = bat_hosts_find_by_mac((char *)backbone); + if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) + printf("%02x:%02x:%02x:%02x:%02x:%02x ", + backbone[0], backbone[1], backbone[2], + backbone[3], backbone[4], backbone[5]); + else + printf("%17s ", bat_host->name); + + printf("on %5d %4i.%03is (0x%04x)\n", + BATADV_PRINT_VID(vid), last_seen_secs, last_seen_msecs, + backbone_crc); + + return NL_OK; +} + static int netlink_print_common(char *mesh_iface, char *orig_iface, int read_opt, float orig_timeout, float watch_interval, const char *header, @@ -1117,3 +1186,13 @@ int netlink_print_bla_claim(char *mesh_iface, char *orig_iface, int read_opts, BATADV_CMD_GET_BLA_CLAIM, bla_claim_callback); } + +int netlink_print_bla_backbone(char *mesh_iface, char *orig_iface, int read_opts, + float orig_timeout, float watch_interval) +{ + return netlink_print_common(mesh_iface, orig_iface, read_opts, + orig_timeout, watch_interval, + "Originator VID last seen (CRC )\n", + BATADV_CMD_GET_BLA_BACKBONE, + bla_backbone_callback); +} diff --git a/netlink.h b/netlink.h index 69f0958..5ff7ace 100644 --- a/netlink.h +++ b/netlink.h @@ -40,6 +40,8 @@ int netlink_print_gateways(char *mesh_iface, char *orig_iface, int read_opt, float orig_timeout, float watch_interval); int netlink_print_bla_claim(char *mesh_iface, char *orig_iface, int read_opt, float orig_timeout, float watch_interval); +int netlink_print_bla_backbone(char *mesh_iface, char *orig_iface, int read_opt, + float orig_timeout, float watch_interval);
extern struct nla_policy batadv_netlink_policy[];
On Monday, July 18, 2016 12:12:02 Sven Eckelmann wrote:
From: Simon Wunderlich sw@simonwunderlich.de
Signed-off-by: Simon Wunderlich sw@simonwunderlich.de Signed-off-by: Sven Eckelmann sven@narfation.org
debug.c | 1 + netlink.c | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ netlink.h | 2 ++ 3 files changed, 82 insertions(+)
Applied in revision 39c1441.
Thanks, Marek
Signed-off-by: Sven Eckelmann sven@narfation.org --- netlink.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-)
diff --git a/netlink.c b/netlink.c index 1e406a9..43b680e 100644 --- a/netlink.c +++ b/netlink.c @@ -794,7 +794,6 @@ static int translocal_callback(struct nl_msg *msg, void *arg)
static const int gateways_mandatory[] = { BATADV_ATTR_ORIG_ADDRESS, - BATADV_ATTR_TQ, BATADV_ATTR_ROUTER, BATADV_ATTR_HARD_IFNAME, BATADV_ATTR_BANDWIDTH_DOWN, @@ -811,6 +810,7 @@ static int gateways_callback(struct nl_msg *msg, void *arg) const char *primary_if; uint32_t bandwidth_down; uint32_t bandwidth_up; + uint32_t throughput; uint8_t *router; uint8_t *orig; char c = ' '; @@ -842,7 +842,6 @@ static int gateways_callback(struct nl_msg *msg, void *arg) c = '*';
orig = nla_data(attrs[BATADV_ATTR_ORIG_ADDRESS]); - tq = nla_get_u8(attrs[BATADV_ATTR_TQ]); router = nla_data(attrs[BATADV_ATTR_ROUTER]); primary_if = nla_get_string(attrs[BATADV_ATTR_HARD_IFNAME]); bandwidth_down = nla_get_u32(attrs[BATADV_ATTR_BANDWIDTH_DOWN]); @@ -858,7 +857,13 @@ static int gateways_callback(struct nl_msg *msg, void *arg) else printf("%17s ", bat_host->name);
- printf("(%3i) ", tq); + if (attrs[BATADV_ATTR_THROUGHPUT]) { + throughput = nla_get_u32(attrs[BATADV_ATTR_THROUGHPUT]); + printf("(%9u.%1u) ", throughput / 10, throughput % 10); + } else if (attrs[BATADV_ATTR_TQ]) { + tq = nla_get_u8(attrs[BATADV_ATTR_TQ]); + printf("(%3i) ", tq); + }
bat_host = bat_hosts_find_by_mac((char *)router); if (!(opts->read_opt & USE_BAT_HOSTS) || !bat_host) @@ -1168,10 +1173,31 @@ int netlink_print_translocal(char *mesh_iface, char *orig_iface, int read_opts, int netlink_print_gateways(char *mesh_iface, char *orig_iface, int read_opts, float orig_timeout, float watch_interval) -{ +{ char *header; + int ifindex; + + ifindex = if_nametoindex(mesh_iface); + if (!ifindex) { + fprintf(stderr, "Interface %s is unknown\n", mesh_iface); + return -ENODEV; + } + + netlink_print_info(ifindex, BATADV_CMD_GET_ORIGINATORS, PARSE_ONLY); + + if (strlen(algo_name_buf) == 0) + return -EINVAL; + + if (!strcmp("BATMAN_IV", algo_name_buf)) + header = " Router ( TQ) Next Hop [outgoingIf] Bandwidth\n"; + if (!strcmp("BATMAN_V", algo_name_buf)) + header = " Router ( throughput) Next Hop [outgoingIf] Bandwidth\n"; + + if (!header) + return -EINVAL; + return netlink_print_common(mesh_iface, orig_iface, read_opts, orig_timeout, watch_interval, - " Router TQ Next Hop outgoingIf Bandwidth\n", + header, BATADV_CMD_GET_GATEWAYS, gateways_callback); }
On Monday, July 18, 2016 12:12:03 Sven Eckelmann wrote:
Signed-off-by: Sven Eckelmann sven@narfation.org
netlink.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-)
Applied in revision 02775f1.
Thanks, Marek
The alfred version of debugfs.c and debugfs.h contains several cleanups regarding error message output, removal of unused declarations and usage of const buffers.
Signed-off-by: Sven Eckelmann sven@narfation.org --- debugfs.c | 17 ++++++++++------- debugfs.h | 4 +--- 2 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/debugfs.c b/debugfs.c index 3c58195..fc39322 100644 --- a/debugfs.c +++ b/debugfs.c @@ -33,6 +33,9 @@ static int debugfs_premounted; static char debugfs_mountpoint[MAX_PATH+1];
+static const char *debugfs_find_mountpoint(void); +static int debugfs_valid_mountpoint(const char *debugfs); + static const char *debugfs_known_mountpoints[] = { "/sys/kernel/debug/", "/debug/", @@ -40,7 +43,8 @@ static const char *debugfs_known_mountpoints[] = { };
/* construct a full path to a debugfs element */ -int debugfs_make_path(const char *fmt, char *mesh_iface, char *buffer, int size) +int debugfs_make_path(const char *fmt, const char *mesh_iface, char *buffer, + int size) { if (strlen(debugfs_mountpoint) == 0) { buffer[0] = '\0'; @@ -53,14 +57,14 @@ int debugfs_make_path(const char *fmt, char *mesh_iface, char *buffer, int size) static int debugfs_found;
/* find the path to the mounted debugfs */ -const char *debugfs_find_mountpoint(void) +static const char *debugfs_find_mountpoint(void) { const char **ptr; char type[100]; FILE *fp;
if (debugfs_found) - return (const char *) debugfs_mountpoint; + return (const char *)debugfs_mountpoint;
ptr = debugfs_known_mountpoints; while (*ptr) { @@ -68,7 +72,7 @@ const char *debugfs_find_mountpoint(void) debugfs_found = 1; strncpy(debugfs_mountpoint, *ptr, sizeof(debugfs_mountpoint)); - debugfs_mountpoint[sizeof(debugfs_mountpoint) - 1] = '\0'; + debugfs_mountpoint[sizeof(debugfs_mountpoint) - 1] = 0; return debugfs_mountpoint; } ptr++; @@ -77,8 +81,7 @@ const char *debugfs_find_mountpoint(void) /* give up and parse /proc/mounts */ fp = fopen("/proc/mounts", "r"); if (fp == NULL) { - fprintf(stderr, "Error - can't open /proc/mounts for read: %s\n", - strerror(errno)); + perror("Error - can't open /proc/mounts for read"); return NULL; }
@@ -101,7 +104,7 @@ const char *debugfs_find_mountpoint(void)
/* verify that a mountpoint is actually a debugfs instance */
-int debugfs_valid_mountpoint(const char *debugfs) +static int debugfs_valid_mountpoint(const char *debugfs) { struct statfs st_fs;
diff --git a/debugfs.h b/debugfs.h index e608902..b4dc6bb 100644 --- a/debugfs.h +++ b/debugfs.h @@ -30,11 +30,9 @@ # define STR(x) _STR(x) #endif
-extern const char *debugfs_find_mountpoint(void); -extern int debugfs_valid_mountpoint(const char *debugfs); extern int debugfs_valid_entry(const char *path); extern char *debugfs_mount(const char *mountpoint); -extern int debugfs_make_path(const char *fmt, char *mesh_iface, +extern int debugfs_make_path(const char *fmt, const char *mesh_iface, char *buffer, int size);
#endif /* __DEBUGFS_H__ */
On Monday, July 18, 2016 12:12:04 Sven Eckelmann wrote:
The alfred version of debugfs.c and debugfs.h contains several cleanups regarding error message output, removal of unused declarations and usage of const buffers.
Signed-off-by: Sven Eckelmann sven@narfation.org
debugfs.c | 17 ++++++++++------- debugfs.h | 4 +--- 2 files changed, 11 insertions(+), 10 deletions(-)
Applied in revision 23c3c6d.
Thanks, Marek
The debugfs tables cannot be used by the batctl in network namespaces. These have to use netlink. Thus the translate_mac should be less tightly linked to the debugfs tables to implement optional netlink support.
Signed-off-by: Sven Eckelmann sven@narfation.org --- functions.c | 46 +++++++++++++++++++++++++++++++++++----------- functions.h | 3 ++- 2 files changed, 37 insertions(+), 12 deletions(-)
diff --git a/functions.c b/functions.c index 5b76062..f994ced 100644 --- a/functions.c +++ b/functions.c @@ -26,6 +26,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> +#include <stdbool.h> #include <stdlib.h> #include <stdio.h> #include <string.h> @@ -371,7 +372,9 @@ out: return res; }
-struct ether_addr *translate_mac(char *mesh_iface, struct ether_addr *mac) +static int translate_mac_debugfs(const char *mesh_iface, + const struct ether_addr *mac, + struct ether_addr *mac_out) { enum { tg_start, @@ -381,26 +384,23 @@ struct ether_addr *translate_mac(char *mesh_iface, struct ether_addr *mac) } pos; char full_path[MAX_PATH+1]; char *debugfs_mnt; - static struct ether_addr in_mac; - struct ether_addr *mac_result, *mac_tmp; + struct ether_addr *mac_tmp; FILE *f = NULL; size_t len = 0; char *line = NULL; char *input, *saveptr, *token; int line_invalid; - - memcpy(&in_mac, mac, sizeof(in_mac)); - mac_result = &in_mac; + bool found = false;
debugfs_mnt = debugfs_mount(NULL); if (!debugfs_mnt) - goto out; + return -EOPNOTSUPP;
debugfs_make_path(DEBUG_BATIF_PATH_FMT "/" DEBUG_TRANSTABLE_GLOBAL, mesh_iface, full_path, sizeof(full_path));
f = fopen(full_path, "r"); if (!f) - goto out; + return -EOPNOTSUPP;
while (getline(&line, &len, f) != -1) { line_invalid = 0; @@ -419,8 +419,8 @@ struct ether_addr *translate_mac(char *mesh_iface, struct ether_addr *mac) break; case tg_mac: mac_tmp = ether_aton(token); - if (!mac_tmp || memcmp(mac_tmp, &in_mac, - sizeof(in_mac)) != 0) + if (!mac_tmp || memcmp(mac_tmp, mac, + ETH_ALEN) != 0) line_invalid = 1; else pos = tg_via; @@ -434,7 +434,8 @@ struct ether_addr *translate_mac(char *mesh_iface, struct ether_addr *mac) if (!mac_tmp) { line_invalid = 1; } else { - mac_result = mac_tmp; + memcpy(mac_out, mac_tmp, ETH_ALEN); + found = true; goto out; } break; @@ -449,6 +450,29 @@ out: if (f) fclose(f); free(line); + + if (found) + return 0; + else + return -ENOENT; +} + +struct ether_addr *translate_mac(const char *mesh_iface, + const struct ether_addr *mac) +{ + struct ether_addr in_mac; + static struct ether_addr out_mac; + struct ether_addr *mac_result; + + /* input mac has to be copied because it could be in the shared + * ether_aton buffer + */ + memcpy(&in_mac, mac, sizeof(in_mac)); + memcpy(&out_mac, mac, sizeof(out_mac)); + mac_result = &out_mac; + + translate_mac_debugfs(mesh_iface, &in_mac, mac_result); + return mac_result; }
diff --git a/functions.h b/functions.h index 2d29d52..1f311ca 100644 --- a/functions.h +++ b/functions.h @@ -40,7 +40,8 @@ int read_file(const char *dir, const char *path, int read_opt, float orig_timeout, float watch_interval, size_t header_lines); int write_file(const char *dir, const char *fname, const char *arg1, const char *arg2); -struct ether_addr *translate_mac(char *mesh_iface, struct ether_addr *mac); +struct ether_addr *translate_mac(const char *mesh_iface, + const struct ether_addr *mac); struct ether_addr *resolve_mac(const char *asc); int vlan_get_link(const char *ifname, char **parent);
On Monday, July 18, 2016 12:12:05 Sven Eckelmann wrote:
The debugfs tables cannot be used by the batctl in network namespaces. These have to use netlink. Thus the translate_mac should be less tightly linked to the debugfs tables to implement optional netlink support.
Signed-off-by: Sven Eckelmann sven@narfation.org
functions.c | 46 +++++++++++++++++++++++++++++++++++----------- functions.h | 3 ++- 2 files changed, 37 insertions(+), 12 deletions(-)
Applied in revision c8434de.
Thanks, Marek
The debugfs entries are only available on the main network namespace. All other network namespaces have to fall back to netlink to read the global translation table.
batctl has therefore try to access the translation table via netlink and try to fall back to the debugfs table in case the batman-adv module doesn't support BATADV_CMD_GET_TRANSTABLE_GLOBAL.
Signed-off-by: Sven Eckelmann sven@narfation.org --- functions.c | 6 +- netlink.c | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ netlink.h | 5 ++ 3 files changed, 195 insertions(+), 1 deletion(-)
diff --git a/functions.c b/functions.c index f994ced..d236d64 100644 --- a/functions.c +++ b/functions.c @@ -463,6 +463,7 @@ struct ether_addr *translate_mac(const char *mesh_iface, struct ether_addr in_mac; static struct ether_addr out_mac; struct ether_addr *mac_result; + int ret;
/* input mac has to be copied because it could be in the shared * ether_aton buffer @@ -471,7 +472,10 @@ struct ether_addr *translate_mac(const char *mesh_iface, memcpy(&out_mac, mac, sizeof(out_mac)); mac_result = &out_mac;
- translate_mac_debugfs(mesh_iface, &in_mac, mac_result); + ret = translate_mac_netlink(mesh_iface, &in_mac, mac_result); + + if (ret == -EOPNOTSUPP) + translate_mac_debugfs(mesh_iface, &in_mac, mac_result);
return mac_result; } diff --git a/netlink.c b/netlink.c index 43b680e..423fc9b 100644 --- a/netlink.c +++ b/netlink.c @@ -22,6 +22,7 @@ #include "netlink.h" #include "main.h"
+#include <stdbool.h> #include <stdio.h> #include <string.h> #include <unistd.h> @@ -43,6 +44,12 @@
#define ARRAY_SIZE(x) (sizeof(x) / sizeof(*(x)))
+#ifndef container_of +#define container_of(ptr, type, member) __extension__ ({ \ + const __typeof__(((type *)0)->member) *__pmember = (ptr); \ + (type *)((char *)__pmember - offsetof(type, member)); }) +#endif + struct print_opts { int read_opt; float orig_timeout; @@ -50,6 +57,10 @@ struct print_opts { uint8_t nl_cmd; };
+struct nlquery_opts { + int err; +}; + struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { [BATADV_ATTR_VERSION] = { .type = NLA_STRING }, [BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING }, @@ -1222,3 +1233,177 @@ int netlink_print_bla_backbone(char *mesh_iface, char *orig_iface, int read_opts BATADV_CMD_GET_BLA_BACKBONE, bla_backbone_callback); } + +static int nlquery_error_cb(struct sockaddr_nl *nla __unused, + struct nlmsgerr *nlerr, void *arg) +{ + struct nlquery_opts *query_opts = arg; + + query_opts->err = nlerr->error; + + return NL_STOP; +} + +static int nlquery_stop_cb(struct nl_msg *msg, void *arg) +{ + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct nlquery_opts *query_opts = arg; + int *error = nlmsg_data(nlh); + + if (*error) + query_opts->err = *error; + + return NL_STOP; +} + +static int netlink_query_common(const char *mesh_iface, uint8_t nl_cmd, + nl_recvmsg_msg_cb_t callback, + struct nlquery_opts *query_opts) +{ + struct nl_sock *sock; + struct nl_msg *msg; + struct nl_cb *cb; + int ifindex; + int family; + int ret; + + query_opts->err = 0; + + sock = nl_socket_alloc(); + if (!sock) + return -ENOMEM; + + ret = genl_connect(sock); + if (ret < 0) { + query_opts->err = ret; + goto err_free_sock; + } + + family = genl_ctrl_resolve(sock, BATADV_NL_NAME); + if (family < 0) { + query_opts->err = -EOPNOTSUPP; + goto err_free_sock; + } + + ifindex = if_nametoindex(mesh_iface); + if (!ifindex) { + query_opts->err = -ENODEV; + goto err_free_sock; + } + + cb = nl_cb_alloc(NL_CB_DEFAULT); + if (!cb) { + query_opts->err = -ENOMEM; + goto err_free_sock; + } + + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, callback, query_opts); + nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, nlquery_stop_cb, query_opts); + nl_cb_err(cb, NL_CB_CUSTOM, nlquery_error_cb, query_opts); + + msg = nlmsg_alloc(); + if (!msg) { + query_opts->err = -ENOMEM; + goto err_free_cb; + } + + genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, NLM_F_DUMP, + nl_cmd, 1); + + nla_put_u32(msg, BATADV_ATTR_MESH_IFINDEX, ifindex); + nl_send_auto_complete(sock, msg); + nlmsg_free(msg); + + nl_recvmsgs(sock, cb); + +err_free_cb: + nl_cb_put(cb); +err_free_sock: + nl_socket_free(sock); + + return query_opts->err; +} + +static const int translate_mac_netlink_mandatory[] = { + BATADV_ATTR_TT_ADDRESS, + BATADV_ATTR_ORIG_ADDRESS, +}; + +struct translate_mac_netlink_opts { + struct ether_addr mac; + bool found; + struct nlquery_opts query_opts; +}; + +static int translate_mac_netlink_cb(struct nl_msg *msg, void *arg) +{ + struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlmsghdr *nlh = nlmsg_hdr(msg); + struct nlquery_opts *query_opts = arg; + struct translate_mac_netlink_opts *opts; + struct genlmsghdr *ghdr; + uint8_t *addr; + uint8_t *orig; + + opts = container_of(query_opts, struct translate_mac_netlink_opts, + query_opts); + + if (!genlmsg_valid_hdr(nlh, 0)) + return NL_OK; + + ghdr = nlmsg_data(nlh); + + if (ghdr->cmd != BATADV_CMD_GET_TRANSTABLE_GLOBAL) + return NL_OK; + + if (nla_parse(attrs, BATADV_ATTR_MAX, genlmsg_attrdata(ghdr, 0), + genlmsg_len(ghdr), batadv_netlink_policy)) { + return NL_OK; + } + + if (missing_mandatory_attrs(attrs, translate_mac_netlink_mandatory, + ARRAY_SIZE(translate_mac_netlink_mandatory))) + return NL_OK; + + addr = nla_data(attrs[BATADV_ATTR_TT_ADDRESS]); + orig = nla_data(attrs[BATADV_ATTR_ORIG_ADDRESS]); + + if (!attrs[BATADV_ATTR_FLAG_BEST]) + return NL_OK; + + if (memcmp(&opts->mac, addr, ETH_ALEN) != 0) + return NL_OK; + + memcpy(&opts->mac, orig, ETH_ALEN); + opts->found = true; + opts->query_opts.err = 0; + + return NL_STOP; +} + +int translate_mac_netlink(const char *mesh_iface, const struct ether_addr *mac, + struct ether_addr *mac_out) +{ + struct translate_mac_netlink_opts opts = { + .found = false, + .query_opts = { + .err = 0, + }, + }; + int ret; + + memcpy(&opts.mac, mac, ETH_ALEN); + + ret = netlink_query_common(mesh_iface, + BATADV_CMD_GET_TRANSTABLE_GLOBAL, + translate_mac_netlink_cb, &opts.query_opts); + if (ret < 0) + return ret; + + if (!opts.found) + return -ENOENT; + + memcpy(mac_out, &opts.mac, ETH_ALEN); + + return 0; +} diff --git a/netlink.h b/netlink.h index 5ff7ace..b2084fb 100644 --- a/netlink.h +++ b/netlink.h @@ -25,6 +25,8 @@ #include <netlink/genl/genl.h> #include <netlink/genl/ctrl.h>
+struct ether_addr; + int netlink_print_routing_algos(void); int netlink_print_originators(char *mesh_iface, char *orig_iface, int read_opt, float orig_timeout, float watch_interval); @@ -43,6 +45,9 @@ int netlink_print_bla_claim(char *mesh_iface, char *orig_iface, int read_opt, int netlink_print_bla_backbone(char *mesh_iface, char *orig_iface, int read_opt, float orig_timeout, float watch_interval);
+int translate_mac_netlink(const char *mesh_iface, const struct ether_addr *mac, + struct ether_addr *mac_out); + extern struct nla_policy batadv_netlink_policy[];
#endif /* _BATCTL_NETLINK_H */
On Monday, July 18, 2016 12:12:06 Sven Eckelmann wrote:
The debugfs entries are only available on the main network namespace. All other network namespaces have to fall back to netlink to read the global translation table.
batctl has therefore try to access the translation table via netlink and try to fall back to the debugfs table in case the batman-adv module doesn't support BATADV_CMD_GET_TRANSTABLE_GLOBAL.
Signed-off-by: Sven Eckelmann sven@narfation.org
functions.c | 6 +- netlink.c | 185 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ netlink.h | 5 ++ 3 files changed, 195 insertions(+), 1 deletion(-)
Applied in revision c2eb7a6.
Thanks, Marek
From: Andrew Lunn andrew@lunn.ch
Add example of has BATMAN can be used with network name spaces in the README.
Signed-off-by: Andrew Lunn andrew@lunn.ch Signed-off-by: Sven Eckelmann sven@narfation.org --- README | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
diff --git a/README b/README index 6df669a..ce049d4 100644 --- a/README +++ b/README @@ -555,3 +555,60 @@ where: - IPv4 is the IP address of a client in the mesh network - MAC is the MAC address associated to that IP - last-seen is the amount of time since last refresh of this entry + +batctl and network name spaces +============================== + +The batman-adv kernel module is netns aware. Mesh instances can be +created in name spaces, and interfaces in that name space added to the +mesh. The mesh interface cannot be moved between name spaces, as is +typical for virtual interfaces. + +The following example creates two network namespaces, and uses veth +pairs to connect them together into a mesh of three nodes. + +EMU1="ip netns exec emu1" +EMU2="ip netns exec emu2" + +ip netns add emu1 +ip netns add emu2 + +ip link add emu1-veth1 type veth peer name emu2-veth1 +ip link set emu1-veth1 netns emu1 +ip link set emu2-veth1 netns emu2 + +$EMU1 ip link set emu1-veth1 name veth1 +$EMU2 ip link set emu2-veth1 name veth1 + +$EMU1 ip link set veth1 up +$EMU2 ip link set veth1 up + +ip link add emu1-veth2 type veth peer name veth2 +ip link set emu1-veth2 netns emu1 +$EMU1 ip link set emu1-veth2 name veth2 + +$EMU1 ip link set veth2 up +ip link set veth2 up + +$EMU1 batctl if add veth1 +$EMU1 batctl if add veth2 +$EMU1 ip link set bat0 up + +$EMU2 batctl if add veth1 +$EMU2 ip link set bat0 up + +batctl if add veth2 +ip link set bat0 up + +alfred and batadv-vis can also be used with name spaces. In this +example, only netns has been used, so there are no filesystem name +spaces. Hence the unix domain socket used by alfred needs to be given +a unique name per instance. + +($EMU1 alfred -m -i bat0 -u /var/run/emu1-alfred.soc) & +($EMU2 alfred -m -i bat0 -u /var/run/emu2-alfred.soc) & +alfred -m -i bat0 & + +($EMU1 batadv-vis -s -u /var/run/emu1-alfred.soc) & +($EMU2 batadv-vis -s -u /var/run/emu2-alfred.soc) & +batadv-vis -s &
On Monday, July 18, 2016 12:12:07 Sven Eckelmann wrote:
From: Andrew Lunn andrew@lunn.ch
Add example of has BATMAN can be used with network name spaces in the README.
Signed-off-by: Andrew Lunn andrew@lunn.ch Signed-off-by: Sven Eckelmann sven@narfation.org
README | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+)
Applied in revision 30c2e89.
Thanks, Marek
The batman-adv release v2016.2 already has the batadv netlink family . Thus the fallback to debugfs not only has to be triggered when the the netlink family doesn't exist but also when the cmd returns a -EOPNOTSUPP. This still has the problem that the header is generated via a different command then the actual table entries. Falling back to debugfs would cause a second header entry for the table.
The header print functionality is therefore refactored to only gather the data for the header and print the actual header either with the first received entry or when the CMD finished without any result.
Signed-off-by: Sven Eckelmann sven@narfation.org --- functions.h | 1 - netlink.c | 146 +++++++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 99 insertions(+), 48 deletions(-)
diff --git a/functions.h b/functions.h index 1f311ca..e24dea0 100644 --- a/functions.h +++ b/functions.h @@ -61,7 +61,6 @@ enum { SKIP_HEADER = 0x100, UNICAST_ONLY = 0x200, MULTICAST_ONLY = 0x400, - PARSE_ONLY = 0x800, };
#endif diff --git a/netlink.c b/netlink.c index 423fc9b..54d4cc2 100644 --- a/netlink.c +++ b/netlink.c @@ -54,6 +54,9 @@ struct print_opts { int read_opt; float orig_timeout; float watch_interval; + nl_recvmsg_msg_cb_t callback; + char *remaining_header; + const char *static_header; uint8_t nl_cmd; };
@@ -139,7 +142,7 @@ static int print_error(struct sockaddr_nl *nla __unused, fprintf(stderr, "Error received: %s\n", strerror(-nlerr->error));
- last_err = -nlerr->error; + last_err = nlerr->error;
return NL_STOP; } @@ -182,6 +185,7 @@ static int info_callback(struct nl_msg *msg, void *arg) uint8_t ttvn = 0; uint16_t bla_group_id = 0; const char *algo_name; + const char *extra_header; int ret;
if (!genlmsg_valid_hdr(nlh, 0)) { @@ -230,54 +234,64 @@ static int info_callback(struct nl_msg *msg, void *arg) if (attrs[BATADV_ATTR_BLA_CRC]) bla_group_id = nla_get_u16(attrs[BATADV_ATTR_BLA_CRC]);
- if (!(opts->read_opt & PARSE_ONLY)) { - switch (opts->nl_cmd) { - case BATADV_CMD_GET_TRANSTABLE_LOCAL: - ret = asprintf(&extra_info, ", TTVN: %u", ttvn); - if (ret < 0) - extra_info = NULL; - break; - case BATADV_CMD_GET_BLA_BACKBONE: - case BATADV_CMD_GET_BLA_CLAIM: - ret = asprintf(&extra_info, ", group id: 0x%04x", - bla_group_id); - if (ret < 0) - extra_info = NULL; - break; - default: - extra_info = strdup(""); - break; - } - - printf("[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%02x:%02x:%02x:%02x:%02x:%02x (%s/%02x:%02x:%02x:%02x:%02x:%02x %s)%s]\n", + switch (opts->nl_cmd) { + case BATADV_CMD_GET_TRANSTABLE_LOCAL: + ret = asprintf(&extra_info, ", TTVN: %u", ttvn); + if (ret < 0) + extra_info = NULL; + break; + case BATADV_CMD_GET_BLA_BACKBONE: + case BATADV_CMD_GET_BLA_CLAIM: + ret = asprintf(&extra_info, ", group id: 0x%04x", + bla_group_id); + if (ret < 0) + extra_info = NULL; + break; + default: + extra_info = strdup(""); + break; + } + + if (opts->static_header) + extra_header = opts->static_header; + else + extra_header = ""; + + ret = asprintf(&opts->remaining_header, + "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%02x:%02x:%02x:%02x:%02x:%02x (%s/%02x:%02x:%02x:%02x:%02x:%02x %s)%s]\n%s", version, primary_if, primary_mac[0], primary_mac[1], primary_mac[2], primary_mac[3], primary_mac[4], primary_mac[5], mesh_name, mesh_mac[0], mesh_mac[1], mesh_mac[2], mesh_mac[3], mesh_mac[4], mesh_mac[5], - algo_name, extra_info); + algo_name, extra_info, extra_header); + if (ret < 0) + opts->remaining_header = NULL;
- if (extra_info) - free(extra_info); - } + if (extra_info) + free(extra_info); } else { - if (!(opts->read_opt & PARSE_ONLY)) - printf("BATMAN mesh %s disabled\n", mesh_name); + ret = asprintf(&opts->remaining_header, + "BATMAN mesh %s disabled\n", mesh_name); + if (ret < 0) + opts->remaining_header = NULL; }
return NL_STOP; }
-static void netlink_print_info(int ifindex, uint8_t nl_cmd, int read_opt) +static char *netlink_get_info(int ifindex, uint8_t nl_cmd, const char *header) { struct nl_sock *sock; struct nl_msg *msg; struct nl_cb *cb; int family; struct print_opts opts = { - .read_opt = read_opt, + .read_opt = 0, .nl_cmd = nl_cmd, + .remaining_header = NULL, + .static_header = header, };
sock = nl_socket_alloc(); @@ -285,7 +299,7 @@ static void netlink_print_info(int ifindex, uint8_t nl_cmd, int read_opt)
family = genl_ctrl_resolve(sock, BATADV_NL_NAME); if (family < 0) - return; + return NULL;
msg = nlmsg_alloc(); genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, 0, @@ -304,6 +318,27 @@ static void netlink_print_info(int ifindex, uint8_t nl_cmd, int read_opt) nl_recvmsgs(sock, cb);
nl_socket_free(sock); + + return opts.remaining_header; +} + +static void netlink_print_remaining_header(struct print_opts *opts) +{ + if (!opts->remaining_header) + return; + + fputs(opts->remaining_header, stdout); + free(opts->remaining_header); + opts->remaining_header = NULL; +} + +static int netlink_print_common_cb(struct nl_msg *msg, void *arg) +{ + struct print_opts *opts = arg; + + netlink_print_remaining_header(opts); + + return opts->callback(msg, arg); }
static const int routing_algos_mandatory[] = { @@ -352,6 +387,9 @@ int netlink_print_routing_algos(void) struct nl_msg *msg; struct nl_cb *cb; int family; + struct print_opts opts = { + .callback = routing_algos_callback, + };
sock = nl_socket_alloc(); genl_connect(sock); @@ -368,19 +406,21 @@ int netlink_print_routing_algos(void)
nlmsg_free(msg);
+ opts.remaining_header = strdup("Available routing algorithms:\n"); + cb = nl_cb_alloc(NL_CB_DEFAULT); - nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, routing_algos_callback, - NULL); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, netlink_print_common_cb, + &opts); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, stop_callback, NULL); nl_cb_err(cb, NL_CB_CUSTOM, print_error, NULL);
- printf("Available routing algorithms:\n"); - nl_recvmsgs(sock, cb); - nl_socket_free(sock);
- return 0; + if (!last_err) + netlink_print_remaining_header(&opts); + + return last_err; }
static const int originators_mandatory[] = { @@ -1040,7 +1080,9 @@ static int netlink_print_common(char *mesh_iface, char *orig_iface, struct print_opts opts = { .read_opt = read_opt, .orig_timeout = orig_timeout, - .watch_interval = watch_interval + .watch_interval = watch_interval, + .remaining_header = NULL, + .callback = callback, }; int hardifindex = 0; struct nl_sock *sock; @@ -1074,7 +1116,7 @@ static int netlink_print_common(char *mesh_iface, char *orig_iface, bat_hosts_init(read_opt);
cb = nl_cb_alloc(NL_CB_DEFAULT); - nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, callback, &opts); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, netlink_print_common_cb, &opts); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, stop_callback, NULL); nl_cb_err(cb, NL_CB_CUSTOM, print_error, NULL);
@@ -1083,11 +1125,10 @@ static int netlink_print_common(char *mesh_iface, char *orig_iface, /* clear screen, set cursor back to 0,0 */ printf("\033[2J\033[0;0f");
- if (!(read_opt & SKIP_HEADER)) { - netlink_print_info(ifindex, nl_cmd, 0); - if (header) - printf("%s", header); - } + if (!(read_opt & SKIP_HEADER)) + opts.remaining_header = netlink_get_info(ifindex, + nl_cmd, + header);
msg = nlmsg_alloc(); genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, @@ -1104,6 +1145,11 @@ static int netlink_print_common(char *mesh_iface, char *orig_iface,
last_err = 0; nl_recvmsgs(sock, cb); + + /* the header should still be printed when no entry was received */ + if (!last_err) + netlink_print_remaining_header(&opts); + if (!last_err && read_opt & (CONT_READ|CLR_CONT_READ)) usleep(1000000 * watch_interval);
@@ -1120,7 +1166,8 @@ int netlink_print_originators(char *mesh_iface, char *orig_iface, int read_opts, float orig_timeout, float watch_interval) { - char *header; + char *header = NULL; + char *info_header; int ifindex;
ifindex = if_nametoindex(mesh_iface); @@ -1129,7 +1176,9 @@ int netlink_print_originators(char *mesh_iface, char *orig_iface, return -ENODEV; }
- netlink_print_info(ifindex, BATADV_CMD_GET_ORIGINATORS, PARSE_ONLY); + /* only parse routing algorithm name */ + info_header = netlink_get_info(ifindex, BATADV_CMD_GET_ORIGINATORS, NULL); + free(info_header);
if (strlen(algo_name_buf) == 0) return -EINVAL; @@ -1184,7 +1233,8 @@ int netlink_print_translocal(char *mesh_iface, char *orig_iface, int read_opts, int netlink_print_gateways(char *mesh_iface, char *orig_iface, int read_opts, float orig_timeout, float watch_interval) -{ char *header; +{ char *header = NULL; + char *info_header; int ifindex;
ifindex = if_nametoindex(mesh_iface); @@ -1193,7 +1243,9 @@ int netlink_print_gateways(char *mesh_iface, char *orig_iface, int read_opts, return -ENODEV; }
- netlink_print_info(ifindex, BATADV_CMD_GET_ORIGINATORS, PARSE_ONLY); + /* only parse routing algorithm name */ + info_header = netlink_get_info(ifindex, BATADV_CMD_GET_ORIGINATORS, NULL); + free(info_header);
if (strlen(algo_name_buf) == 0) return -EINVAL;
The batman-adv release v2016.2 already has the batadv netlink family . Thus the fallback to debugfs not only has to be triggered when the the netlink family doesn't exist but also when the cmd returns a -EOPNOTSUPP. This still has the problem that the header is generated via a different command then the actual table entries. Falling back to debugfs would cause a second header entry for the table.
The header print functionality is therefore refactored to only gather the data for the header and print the actual header either with the first received entry or when the CMD finished without any result.
Signed-off-by: Sven Eckelmann sven@narfation.org --- v12: - also use debugfs for originators/gwl when get_mesh_info is not supported --- functions.h | 1 - netlink.c | 150 ++++++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 101 insertions(+), 50 deletions(-)
diff --git a/functions.h b/functions.h index 1f311ca..e24dea0 100644 --- a/functions.h +++ b/functions.h @@ -61,7 +61,6 @@ enum { SKIP_HEADER = 0x100, UNICAST_ONLY = 0x200, MULTICAST_ONLY = 0x400, - PARSE_ONLY = 0x800, };
#endif diff --git a/netlink.c b/netlink.c index 423fc9b..e8e94b2 100644 --- a/netlink.c +++ b/netlink.c @@ -54,6 +54,9 @@ struct print_opts { int read_opt; float orig_timeout; float watch_interval; + nl_recvmsg_msg_cb_t callback; + char *remaining_header; + const char *static_header; uint8_t nl_cmd; };
@@ -139,7 +142,7 @@ static int print_error(struct sockaddr_nl *nla __unused, fprintf(stderr, "Error received: %s\n", strerror(-nlerr->error));
- last_err = -nlerr->error; + last_err = nlerr->error;
return NL_STOP; } @@ -182,6 +185,7 @@ static int info_callback(struct nl_msg *msg, void *arg) uint8_t ttvn = 0; uint16_t bla_group_id = 0; const char *algo_name; + const char *extra_header; int ret;
if (!genlmsg_valid_hdr(nlh, 0)) { @@ -230,54 +234,64 @@ static int info_callback(struct nl_msg *msg, void *arg) if (attrs[BATADV_ATTR_BLA_CRC]) bla_group_id = nla_get_u16(attrs[BATADV_ATTR_BLA_CRC]);
- if (!(opts->read_opt & PARSE_ONLY)) { - switch (opts->nl_cmd) { - case BATADV_CMD_GET_TRANSTABLE_LOCAL: - ret = asprintf(&extra_info, ", TTVN: %u", ttvn); - if (ret < 0) - extra_info = NULL; - break; - case BATADV_CMD_GET_BLA_BACKBONE: - case BATADV_CMD_GET_BLA_CLAIM: - ret = asprintf(&extra_info, ", group id: 0x%04x", - bla_group_id); - if (ret < 0) - extra_info = NULL; - break; - default: - extra_info = strdup(""); - break; - } - - printf("[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%02x:%02x:%02x:%02x:%02x:%02x (%s/%02x:%02x:%02x:%02x:%02x:%02x %s)%s]\n", + switch (opts->nl_cmd) { + case BATADV_CMD_GET_TRANSTABLE_LOCAL: + ret = asprintf(&extra_info, ", TTVN: %u", ttvn); + if (ret < 0) + extra_info = NULL; + break; + case BATADV_CMD_GET_BLA_BACKBONE: + case BATADV_CMD_GET_BLA_CLAIM: + ret = asprintf(&extra_info, ", group id: 0x%04x", + bla_group_id); + if (ret < 0) + extra_info = NULL; + break; + default: + extra_info = strdup(""); + break; + } + + if (opts->static_header) + extra_header = opts->static_header; + else + extra_header = ""; + + ret = asprintf(&opts->remaining_header, + "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%02x:%02x:%02x:%02x:%02x:%02x (%s/%02x:%02x:%02x:%02x:%02x:%02x %s)%s]\n%s", version, primary_if, primary_mac[0], primary_mac[1], primary_mac[2], primary_mac[3], primary_mac[4], primary_mac[5], mesh_name, mesh_mac[0], mesh_mac[1], mesh_mac[2], mesh_mac[3], mesh_mac[4], mesh_mac[5], - algo_name, extra_info); + algo_name, extra_info, extra_header); + if (ret < 0) + opts->remaining_header = NULL;
- if (extra_info) - free(extra_info); - } + if (extra_info) + free(extra_info); } else { - if (!(opts->read_opt & PARSE_ONLY)) - printf("BATMAN mesh %s disabled\n", mesh_name); + ret = asprintf(&opts->remaining_header, + "BATMAN mesh %s disabled\n", mesh_name); + if (ret < 0) + opts->remaining_header = NULL; }
return NL_STOP; }
-static void netlink_print_info(int ifindex, uint8_t nl_cmd, int read_opt) +static char *netlink_get_info(int ifindex, uint8_t nl_cmd, const char *header) { struct nl_sock *sock; struct nl_msg *msg; struct nl_cb *cb; int family; struct print_opts opts = { - .read_opt = read_opt, + .read_opt = 0, .nl_cmd = nl_cmd, + .remaining_header = NULL, + .static_header = header, };
sock = nl_socket_alloc(); @@ -285,7 +299,7 @@ static void netlink_print_info(int ifindex, uint8_t nl_cmd, int read_opt)
family = genl_ctrl_resolve(sock, BATADV_NL_NAME); if (family < 0) - return; + return NULL;
msg = nlmsg_alloc(); genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, 0, @@ -304,6 +318,27 @@ static void netlink_print_info(int ifindex, uint8_t nl_cmd, int read_opt) nl_recvmsgs(sock, cb);
nl_socket_free(sock); + + return opts.remaining_header; +} + +static void netlink_print_remaining_header(struct print_opts *opts) +{ + if (!opts->remaining_header) + return; + + fputs(opts->remaining_header, stdout); + free(opts->remaining_header); + opts->remaining_header = NULL; +} + +static int netlink_print_common_cb(struct nl_msg *msg, void *arg) +{ + struct print_opts *opts = arg; + + netlink_print_remaining_header(opts); + + return opts->callback(msg, arg); }
static const int routing_algos_mandatory[] = { @@ -352,6 +387,9 @@ int netlink_print_routing_algos(void) struct nl_msg *msg; struct nl_cb *cb; int family; + struct print_opts opts = { + .callback = routing_algos_callback, + };
sock = nl_socket_alloc(); genl_connect(sock); @@ -368,19 +406,21 @@ int netlink_print_routing_algos(void)
nlmsg_free(msg);
+ opts.remaining_header = strdup("Available routing algorithms:\n"); + cb = nl_cb_alloc(NL_CB_DEFAULT); - nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, routing_algos_callback, - NULL); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, netlink_print_common_cb, + &opts); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, stop_callback, NULL); nl_cb_err(cb, NL_CB_CUSTOM, print_error, NULL);
- printf("Available routing algorithms:\n"); - nl_recvmsgs(sock, cb); - nl_socket_free(sock);
- return 0; + if (!last_err) + netlink_print_remaining_header(&opts); + + return last_err; }
static const int originators_mandatory[] = { @@ -1040,7 +1080,9 @@ static int netlink_print_common(char *mesh_iface, char *orig_iface, struct print_opts opts = { .read_opt = read_opt, .orig_timeout = orig_timeout, - .watch_interval = watch_interval + .watch_interval = watch_interval, + .remaining_header = NULL, + .callback = callback, }; int hardifindex = 0; struct nl_sock *sock; @@ -1074,7 +1116,7 @@ static int netlink_print_common(char *mesh_iface, char *orig_iface, bat_hosts_init(read_opt);
cb = nl_cb_alloc(NL_CB_DEFAULT); - nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, callback, &opts); + nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, netlink_print_common_cb, &opts); nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, stop_callback, NULL); nl_cb_err(cb, NL_CB_CUSTOM, print_error, NULL);
@@ -1083,11 +1125,10 @@ static int netlink_print_common(char *mesh_iface, char *orig_iface, /* clear screen, set cursor back to 0,0 */ printf("\033[2J\033[0;0f");
- if (!(read_opt & SKIP_HEADER)) { - netlink_print_info(ifindex, nl_cmd, 0); - if (header) - printf("%s", header); - } + if (!(read_opt & SKIP_HEADER)) + opts.remaining_header = netlink_get_info(ifindex, + nl_cmd, + header);
msg = nlmsg_alloc(); genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, @@ -1104,6 +1145,11 @@ static int netlink_print_common(char *mesh_iface, char *orig_iface,
last_err = 0; nl_recvmsgs(sock, cb); + + /* the header should still be printed when no entry was received */ + if (!last_err) + netlink_print_remaining_header(&opts); + if (!last_err && read_opt & (CONT_READ|CLR_CONT_READ)) usleep(1000000 * watch_interval);
@@ -1120,7 +1166,8 @@ int netlink_print_originators(char *mesh_iface, char *orig_iface, int read_opts, float orig_timeout, float watch_interval) { - char *header; + char *header = NULL; + char *info_header; int ifindex;
ifindex = if_nametoindex(mesh_iface); @@ -1129,10 +1176,12 @@ int netlink_print_originators(char *mesh_iface, char *orig_iface, return -ENODEV; }
- netlink_print_info(ifindex, BATADV_CMD_GET_ORIGINATORS, PARSE_ONLY); + /* only parse routing algorithm name */ + info_header = netlink_get_info(ifindex, BATADV_CMD_GET_ORIGINATORS, NULL); + free(info_header);
if (strlen(algo_name_buf) == 0) - return -EINVAL; + return -EOPNOTSUPP;
if (!strcmp("BATMAN_IV", algo_name_buf)) header = " Originator last-seen (#/255) Nexthop [outgoingIF]\n"; @@ -1184,7 +1233,8 @@ int netlink_print_translocal(char *mesh_iface, char *orig_iface, int read_opts, int netlink_print_gateways(char *mesh_iface, char *orig_iface, int read_opts, float orig_timeout, float watch_interval) -{ char *header; +{ char *header = NULL; + char *info_header; int ifindex;
ifindex = if_nametoindex(mesh_iface); @@ -1193,10 +1243,12 @@ int netlink_print_gateways(char *mesh_iface, char *orig_iface, int read_opts, return -ENODEV; }
- netlink_print_info(ifindex, BATADV_CMD_GET_ORIGINATORS, PARSE_ONLY); + /* only parse routing algorithm name */ + info_header = netlink_get_info(ifindex, BATADV_CMD_GET_ORIGINATORS, NULL); + free(info_header);
if (strlen(algo_name_buf) == 0) - return -EINVAL; + return -EOPNOTSUPP;
if (!strcmp("BATMAN_IV", algo_name_buf)) header = " Router ( TQ) Next Hop [outgoingIf] Bandwidth\n";
On Tuesday, July 19, 2016 22:09:52 Sven Eckelmann wrote:
The batman-adv release v2016.2 already has the batadv netlink family . Thus the fallback to debugfs not only has to be triggered when the the netlink family doesn't exist but also when the cmd returns a -EOPNOTSUPP. This still has the problem that the header is generated via a different command then the actual table entries. Falling back to debugfs would cause a second header entry for the table.
The header print functionality is therefore refactored to only gather the data for the header and print the actual header either with the first received entry or when the CMD finished without any result.
Signed-off-by: Sven Eckelmann sven@narfation.org
v12:
- also use debugfs for originators/gwl when get_mesh_info is not supported
functions.h | 1 - netlink.c | 150 ++++++++++++++++++++++++++++++++++++++++-------------------- 2 files changed, 101 insertions(+), 50 deletions(-)
Applied in revision 5401c71.
Thanks, Marek
b.a.t.m.a.n@lists.open-mesh.org