Repository : ssh://git@open-mesh.org/alfred
On branch : master
commit 576d03874e6f342af4881f4615a37d9fe291478a Author: Sven Eckelmann sven.eckelmann@openmesh.com Date: Wed May 24 12:32:07 2017 +0200
alfred: Move alfred specific netlink code in separate file
The vis daemon doesn't require the same set of netlink functions as alfred daemon. But the netlink.c file is shared between both. Split the file to avoid a lot of dead code in vis.
Signed-off-by: Sven Eckelmann sven.eckelmann@openmesh.com Signed-off-by: Simon Wunderlich simon.wunderlich@openmesh.com
576d03874e6f342af4881f4615a37d9fe291478a Makefile | 1 + batadv_query.c | 2 +- netlink.c => batadv_querynl.c | 168 ++---------------------------------- netlink.h => batadv_querynl.h | 29 +------ netlink.c | 195 ------------------------------------------ netlink.h | 7 -- 6 files changed, 12 insertions(+), 390 deletions(-)
diff --git a/Makefile b/Makefile index 3c88e96..4c1c6b5 100755 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ # alfred build BINARY_NAME = alfred OBJ += batadv_query.o +OBJ += batadv_querynl.o OBJ += client.o OBJ += debugfs.o OBJ += hash.o diff --git a/batadv_query.c b/batadv_query.c index 6ec086b..e68052b 100644 --- a/batadv_query.c +++ b/batadv_query.c @@ -34,7 +34,7 @@ #endif #include <sys/types.h>
-#include "netlink.h" +#include "batadv_querynl.h" #include "debugfs.h"
#define DEBUG_BATIF_PATH_FMT "%s/batman_adv/%s" diff --git a/netlink.c b/batadv_querynl.c similarity index 53% copy from netlink.c copy to batadv_querynl.c index 1964ab8..8dab96e 100644 --- a/netlink.c +++ b/batadv_querynl.c @@ -19,7 +19,7 @@ * */
-#include "netlink.h" +#include "batadv_querynl.h"
#include <stdbool.h> #include <stddef.h> @@ -35,168 +35,12 @@ #include <net/ethernet.h>
#include "batman_adv.h" +#include "netlink.h"
#ifndef __unused #define __unused __attribute__((unused)) #endif
-struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { - [BATADV_ATTR_VERSION] = { .type = NLA_STRING }, - [BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING }, - [BATADV_ATTR_MESH_IFINDEX] = { .type = NLA_U32 }, - [BATADV_ATTR_MESH_IFNAME] = { .type = NLA_STRING, - .maxlen = IFNAMSIZ }, - [BATADV_ATTR_MESH_ADDRESS] = { .type = NLA_UNSPEC, - .minlen = ETH_ALEN, - .maxlen = ETH_ALEN }, - [BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 }, - [BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING, - .maxlen = IFNAMSIZ }, - [BATADV_ATTR_HARD_ADDRESS] = { .type = NLA_UNSPEC, - .minlen = ETH_ALEN, - .maxlen = ETH_ALEN }, - [BATADV_ATTR_ORIG_ADDRESS] = { .type = NLA_UNSPEC, - .minlen = ETH_ALEN, - .maxlen = ETH_ALEN }, - [BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 }, - [BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 }, - [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 }, -}; - -int missing_mandatory_attrs(struct nlattr *attrs[], const int mandatory[], - size_t num) -{ - size_t i; - - for (i = 0; i < num; i++) - if (!attrs[mandatory[i]]) - return -EINVAL; - - return 0; -} - -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; -} - -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, @@ -210,7 +54,7 @@ struct translate_mac_netlink_opts {
static int translate_mac_netlink_cb(struct nl_msg *msg, void *arg) { - struct nlattr *attrs[BATADV_ATTR_MAX+1]; + 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; @@ -269,7 +113,7 @@ int translate_mac_netlink(const char *mesh_iface, const struct ether_addr *mac,
ret = netlink_query_common(mesh_iface, BATADV_CMD_GET_TRANSTABLE_GLOBAL, - translate_mac_netlink_cb, &opts.query_opts); + translate_mac_netlink_cb, &opts.query_opts); if (ret < 0) return ret;
@@ -295,7 +139,7 @@ struct get_tq_netlink_opts {
static int get_tq_netlink_cb(struct nl_msg *msg, void *arg) { - struct nlattr *attrs[BATADV_ATTR_MAX+1]; + struct nlattr *attrs[BATADV_ATTR_MAX + 1]; struct nlmsghdr *nlh = nlmsg_hdr(msg); struct nlquery_opts *query_opts = arg; struct get_tq_netlink_opts *opts; @@ -354,7 +198,7 @@ int get_tq_netlink(const char *mesh_iface, const struct ether_addr *mac, memcpy(&opts.mac, mac, ETH_ALEN);
ret = netlink_query_common(mesh_iface, BATADV_CMD_GET_ORIGINATORS, - get_tq_netlink_cb, &opts.query_opts); + get_tq_netlink_cb, &opts.query_opts); if (ret < 0) return ret;
diff --git a/netlink.h b/batadv_querynl.h similarity index 59% copy from netlink.h copy to batadv_querynl.h index 1c87695..9b93a47 100644 --- a/netlink.h +++ b/batadv_querynl.h @@ -19,38 +19,17 @@ * */
-#ifndef _ALFRED_NETLINK_H -#define _ALFRED_NETLINK_H +#ifndef _BATADV_QUERYNL_H +#define _BATADV_QUERYNL_H
-#include <netlink/genl/genl.h> -#include <netlink/genl/ctrl.h> -#include <stddef.h> +#include <stdint.h>
struct ether_addr;
-struct nlquery_opts { - int err; -}; - -#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 - -int netlink_query_common(const char *mesh_iface, uint8_t nl_cmd, - nl_recvmsg_msg_cb_t callback, - struct nlquery_opts *query_opts); -int missing_mandatory_attrs(struct nlattr *attrs[], const int mandatory[], - size_t num); int translate_mac_netlink(const char *mesh_iface, const struct ether_addr *mac, struct ether_addr *mac_out); int get_tq_netlink(const char *mesh_iface, const struct ether_addr *mac, uint8_t *tq); int batadv_interface_check_netlink(const char *mesh_iface);
-extern struct nla_policy batadv_netlink_policy[]; - -#endif /* _ALFRED_NETLINK_H */ +#endif /* _BATADV_QUERYNL_H */ diff --git a/netlink.c b/netlink.c index 1964ab8..7ef4308 100644 --- a/netlink.c +++ b/netlink.c @@ -27,7 +27,6 @@ #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> @@ -196,197 +195,3 @@ err_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; -} - -static const int get_tq_netlink_mandatory[] = { - BATADV_ATTR_ORIG_ADDRESS, - BATADV_ATTR_TQ, -}; - -struct get_tq_netlink_opts { - struct ether_addr mac; - uint8_t tq; - bool found; - struct nlquery_opts query_opts; -}; - -static int get_tq_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 get_tq_netlink_opts *opts; - struct genlmsghdr *ghdr; - uint8_t *orig; - uint8_t tq; - - opts = container_of(query_opts, struct get_tq_netlink_opts, - query_opts); - - if (!genlmsg_valid_hdr(nlh, 0)) - return NL_OK; - - 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)) { - return NL_OK; - } - - if (missing_mandatory_attrs(attrs, get_tq_netlink_mandatory, - ARRAY_SIZE(get_tq_netlink_mandatory))) - return NL_OK; - - orig = nla_data(attrs[BATADV_ATTR_ORIG_ADDRESS]); - tq = nla_get_u8(attrs[BATADV_ATTR_TQ]); - - if (!attrs[BATADV_ATTR_FLAG_BEST]) - return NL_OK; - - if (memcmp(&opts->mac, orig, ETH_ALEN) != 0) - return NL_OK; - - opts->tq = tq; - opts->found = true; - opts->query_opts.err = 0; - - return NL_STOP; -} - -int get_tq_netlink(const char *mesh_iface, const struct ether_addr *mac, - uint8_t *tq) -{ - struct get_tq_netlink_opts opts = { - .tq = 0, - .found = false, - .query_opts = { - .err = 0, - }, - }; - int ret; - - memcpy(&opts.mac, mac, ETH_ALEN); - - ret = netlink_query_common(mesh_iface, BATADV_CMD_GET_ORIGINATORS, - get_tq_netlink_cb, &opts.query_opts); - if (ret < 0) - return ret; - - if (!opts.found) - return -ENOENT; - - *tq = opts.tq; - - return 0; -} - -static int check_nlcmd_cb(struct nl_msg *msg __unused, void *arg __unused) -{ - return NL_STOP; -} - -int batadv_interface_check_netlink(const char *mesh_iface) -{ - struct nlquery_opts opts = { - .err = 0, - }; - int ret; - - ret = netlink_query_common(mesh_iface, BATADV_CMD_GET_ORIGINATORS, - check_nlcmd_cb, &opts); - if (ret < 0) - return ret; - - ret = netlink_query_common(mesh_iface, BATADV_CMD_GET_TRANSTABLE_GLOBAL, - check_nlcmd_cb, &opts); - if (ret < 0) - return ret; - - return 0; -} diff --git a/netlink.h b/netlink.h index 1c87695..a4471a1 100644 --- a/netlink.h +++ b/netlink.h @@ -26,8 +26,6 @@ #include <netlink/genl/ctrl.h> #include <stddef.h>
-struct ether_addr; - struct nlquery_opts { int err; }; @@ -45,11 +43,6 @@ int netlink_query_common(const char *mesh_iface, uint8_t nl_cmd, struct nlquery_opts *query_opts); int missing_mandatory_attrs(struct nlattr *attrs[], const int mandatory[], size_t num); -int translate_mac_netlink(const char *mesh_iface, const struct ether_addr *mac, - struct ether_addr *mac_out); -int get_tq_netlink(const char *mesh_iface, const struct ether_addr *mac, - uint8_t *tq); -int batadv_interface_check_netlink(const char *mesh_iface);
extern struct nla_policy batadv_netlink_policy[];