From patchwork Sun Jun 5 18:47:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 16323 X-Patchwork-Delegate: sw@simonwunderlich.de Return-Path: X-Original-To: patchwork@open-mesh.org Delivered-To: patchwork@open-mesh.org Received: from open-mesh.org (localhost [IPv6:::1]) by open-mesh.org (Postfix) with ESMTP id 35E1681B40; Sun, 5 Jun 2016 20:48:29 +0200 (CEST) Authentication-Results: open-mesh.org; dmarc=none header.from=narfation.org Authentication-Results: open-mesh.org; dkim=fail reason="verification failed; unprotected key" header.d=narfation.org header.i=@narfation.org header.b=b/Th8a8Y; dkim-adsp=fail (unprotected policy); dkim-atps=neutral Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=2001:4d88:2000:7::2; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver=b.a.t.m.a.n@lists.open-mesh.org Authentication-Results: open-mesh.org; dmarc=pass header.from=narfation.org Received: from v3-1039.vlinux.de (narfation.org [IPv6:2001:4d88:2000:7::2]) by open-mesh.org (Postfix) with ESMTPS id BE3D281AC9 for ; Sun, 5 Jun 2016 20:47:25 +0200 (CEST) Received: from sven-desktop.home.narfation.org (p4FCB293D.dip0.t-ipconnect.de [79.203.41.61]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id 418AA1100E8; Sun, 5 Jun 2016 20:47:25 +0200 (CEST) Authentication-Results: v3-1039.vlinux.de; dmarc=none header.from=narfation.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=narfation.org; s=20121; t=1465152445; bh=OORmVJmiWA8+WpF3Sk1IxQLxylJ2zmn3IF/w9gNA/Cs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b/Th8a8Ye4KaRLHKXYh806hQ5TMlMZKBNEVHqThmgBQf8VwBqOSPOrIKdvMb+C2qK 0MDgbzXBgtHsRSsAsiunRzgvIOq+yGTxP0n+/u1o6+v0MItclMFBU+7jiOl8koRX7Z v8ZsW/1uunaL607+lYm5fi6A+tRM1M6FByL3g/60= From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Sun, 5 Jun 2016 20:47:06 +0200 Message-Id: <1465152428-17299-8-git-send-email-sven@narfation.org> X-Mailer: git-send-email 2.8.1 In-Reply-To: <1465152428-17299-1-git-send-email-sven@narfation.org> References: <1465152428-17299-1-git-send-email-sven@narfation.org> Subject: [B.A.T.M.A.N.] [PATCH 08/10] alfred: Query TQ of originators via netlink X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking Errors-To: b.a.t.m.a.n-bounces@lists.open-mesh.org Sender: "B.A.T.M.A.N" The debugfs entries are only available on the main network namespace. All other network namespaces have to fall back to netlink to read the originator table. alfred has therefore try to access the originator via netlink and try to fall back to the debugfs table in case the batman-adv module doesn't support BATADV_CMD_GET_ORIGINATORS. Signed-off-by: Sven Eckelmann --- batadv_query.c | 8 +++++- netlink.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ netlink.h | 2 ++ 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/batadv_query.c b/batadv_query.c index a893995..a671b79 100644 --- a/batadv_query.c +++ b/batadv_query.c @@ -366,13 +366,19 @@ uint8_t get_tq(const char *mesh_iface, struct ether_addr *mac) { struct ether_addr in_mac; uint8_t tq = 0; + int ret; /* input mac has to be copied because it could be in the shared * ether_aton buffer */ memcpy(&in_mac, mac, sizeof(in_mac)); - get_tq_debugfs(mesh_iface, &in_mac, &tq); + enable_net_admin_capability(1); + ret = get_tq_netlink(mesh_iface, &in_mac, &tq); + enable_net_admin_capability(0); + + if (ret == -EOPNOTSUPP) + get_tq_debugfs(mesh_iface, &in_mac, &tq); return tq; } diff --git a/netlink.c b/netlink.c index e1ebc07..1b5695c 100644 --- a/netlink.c +++ b/netlink.c @@ -280,3 +280,88 @@ int translate_mac_netlink(const char *mesh_iface, const struct ether_addr *mac, 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; +} diff --git a/netlink.h b/netlink.h index 8e54235..b08e872 100644 --- a/netlink.h +++ b/netlink.h @@ -47,6 +47,8 @@ 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); extern struct nla_policy batadv_netlink_policy[];