From: Andrew Lunn <andrew@lunn.ch>
To: "B.A.T.M.A.N" <b.a.t.m.a.n@lists.open-mesh.org>
Cc: Sven Eckelmann <sven.eckelmann@open-mesh.com>
Subject: [B.A.T.M.A.N.] [PATCH v6 04/11] batman-adv: netlink: hardif query
Date: Mon, 16 May 2016 19:28:02 +0200 [thread overview]
Message-ID: <1463419689-3580-5-git-send-email-andrew@lunn.ch> (raw)
In-Reply-To: <1463419689-3580-1-git-send-email-andrew@lunn.ch>
From: Matthias Schiffer <mschiffer@universe-factory.net>
BATADV_CMD_GET_HARDIFS will return the list of hardifs (including index,
name and MAC address) of all hardifs for a given softif.
Signed-off-by: Matthias Schiffer <mschiffer@universe-factory.net>
Signed-off-by: Andrew Lunn <andrew@lunn.ch>
[sven.eckelmann@open-mesh.com: Reduce the number of changes to
BATADV_CMD_GET_HARDIFS, add policy for attributes]
Signed-off-by: Sven Eckelmann <sven.eckelmann@open-mesh.com>
---
include/uapi/linux/batman_adv.h | 4 ++
net/batman-adv/netlink.c | 129 +++++++++++++++++++++++++++++++++++++++-
2 files changed, 131 insertions(+), 2 deletions(-)
diff --git a/include/uapi/linux/batman_adv.h b/include/uapi/linux/batman_adv.h
index d2a9740..9e7d77a 100644
--- a/include/uapi/linux/batman_adv.h
+++ b/include/uapi/linux/batman_adv.h
@@ -32,6 +32,7 @@
* @BATADV_ATTR_HARD_IFINDEX: index of the non-batman-adv interface
* @BATADV_ATTR_HARD_IFNAME: name of the non-batman-adv interface
* @BATADV_ATTR_HARD_ADDRESS: mac address of the non-batman-adv interface
+ * @BATADV_ATTR_ACTIVE: Flag indicating if the hard interface is active
* @__BATADV_ATTR_AFTER_LAST: internal use
* @NUM_BATADV_ATTR: total number of batadv_nl_attrs available
* @BATADV_ATTR_MAX: highest attribute number currently defined
@@ -46,6 +47,7 @@ enum batadv_nl_attrs {
BATADV_ATTR_HARD_IFINDEX,
BATADV_ATTR_HARD_IFNAME,
BATADV_ATTR_HARD_ADDRESS,
+ BATADV_ATTR_ACTIVE,
/* add attributes above here, update the policy in netlink.c */
__BATADV_ATTR_AFTER_LAST,
NUM_BATADV_ATTR = __BATADV_ATTR_AFTER_LAST,
@@ -58,6 +60,7 @@ enum batadv_nl_attrs {
* @BATADV_CMD_UNSPEC: unspecified command to catch errors
* @BATADV_CMD_GET_MESH_INFO: Query basic information about batman-adv device
* @BATADV_CMD_GET_ROUTING_ALGOS: Query the list of routing algorithms.
+ * @BATADV_CMD_GET_HARDIFS: Query list of hard interfaces
* @__BATADV_CMD_AFTER_LAST: internal use
* @BATADV_CMD_MAX: highest used command number
*/
@@ -65,6 +68,7 @@ enum batadv_nl_commands {
BATADV_CMD_UNSPEC,
BATADV_CMD_GET_MESH_INFO,
BATADV_CMD_GET_ROUTING_ALGOS,
+ BATADV_CMD_GET_HARDIFS,
/* add new commands above here */
__BATADV_CMD_AFTER_LAST,
BATADV_CMD_MAX = __BATADV_CMD_AFTER_LAST - 1
diff --git a/net/batman-adv/netlink.c b/net/batman-adv/netlink.c
index 717d7d0..bc4fcc2 100644
--- a/net/batman-adv/netlink.c
+++ b/net/batman-adv/netlink.c
@@ -26,16 +26,19 @@
#include <linux/netdevice.h>
#include <linux/netlink.h>
#include <linux/printk.h>
+#include <linux/rculist.h>
+#include <linux/rcupdate.h>
+#include <linux/skbuff.h>
#include <linux/stddef.h>
+#include <linux/types.h>
#include <net/genetlink.h>
#include <net/netlink.h>
+#include <net/sock.h>
#include <uapi/linux/batman_adv.h>
#include "hard-interface.h"
#include "soft-interface.h"
-struct sk_buff;
-
struct genl_family batadv_netlink_family = {
.id = GENL_ID_GENERATE,
.hdrsize = 0,
@@ -53,9 +56,25 @@ static struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
[BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 },
[BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING },
[BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN },
+ [BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG },
};
/**
+ * batadv_netlink_get_ifindex - Extract an interface index from a message
+ * @nlh: Message header
+ * @attrtype: Attribute which holds an interface index
+ *
+ * Return: interface index, or 0.
+ */
+static int
+batadv_netlink_get_ifindex(const struct nlmsghdr *nlh, int attrtype)
+{
+ struct nlattr *attr = nlmsg_find_attr(nlh, GENL_HDRLEN, attrtype);
+
+ return attr ? nla_get_u32(attr) : 0;
+}
+
+/**
* batadv_netlink_mesh_info_put - fill in generic information about mesh
* interface
* @msg: netlink message to be sent back
@@ -163,6 +182,106 @@ batadv_netlink_get_mesh_info(struct sk_buff *skb, struct genl_info *info)
return genlmsg_reply(msg, info);
}
+/**
+ * batadv_netlink_dump_hardif_entry - Dump one hard interface into a message
+ * @msg: Netlink message to dump into
+ * @portid: Port making netlink request
+ * @seq: Sequence number of netlink message
+ * @hard_iface: Hard interface to dump
+ *
+ * Return: error code, or 0 on success
+ */
+static int
+batadv_netlink_dump_hardif_entry(struct sk_buff *msg, u32 portid, u32 seq,
+ struct batadv_hard_iface *hard_iface)
+{
+ struct net_device *net_dev = hard_iface->net_dev;
+ void *hdr;
+
+ hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
+ BATADV_CMD_GET_HARDIFS);
+ if (!hdr)
+ return -EMSGSIZE;
+
+ if (nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
+ net_dev->ifindex) ||
+ nla_put_string(msg, BATADV_ATTR_HARD_IFNAME,
+ net_dev->name) ||
+ nla_put(msg, BATADV_ATTR_HARD_ADDRESS, ETH_ALEN,
+ net_dev->dev_addr))
+ goto nla_put_failure;
+
+ if (hard_iface->if_status == BATADV_IF_ACTIVE) {
+ if (nla_put_flag(msg, BATADV_ATTR_ACTIVE))
+ goto nla_put_failure;
+ }
+
+ genlmsg_end(msg, hdr);
+ return 0;
+
+ nla_put_failure:
+ genlmsg_cancel(msg, hdr);
+ return -EMSGSIZE;
+}
+
+/**
+ * batadv_netlink_dump_hardifs - Dump all hard interface into a messages
+ * @msg: Netlink message to dump into
+ * @cb: Parameters from query
+ *
+ * Return: error code, or length of reply message on success
+ */
+static int
+batadv_netlink_dump_hardifs(struct sk_buff *msg, struct netlink_callback *cb)
+{
+ struct net *net = sock_net(cb->skb->sk);
+ struct net_device *soft_iface;
+ struct batadv_hard_iface *hard_iface;
+ int ifindex;
+ int portid = NETLINK_CB(cb->skb).portid;
+ int seq = cb->nlh->nlmsg_seq;
+ int skip = cb->args[0];
+ int i = 0;
+
+ ifindex = batadv_netlink_get_ifindex(cb->nlh,
+ BATADV_ATTR_MESH_IFINDEX);
+ if (!ifindex)
+ return -EINVAL;
+
+ soft_iface = dev_get_by_index(net, ifindex);
+ if (!soft_iface)
+ return -ENODEV;
+
+ if (!batadv_softif_is_valid(soft_iface)) {
+ dev_put(soft_iface);
+ return -ENODEV;
+ }
+
+ rcu_read_lock();
+
+ list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
+ if (hard_iface->soft_iface != soft_iface)
+ continue;
+
+ if (i++ < skip)
+ continue;
+
+ if (batadv_netlink_dump_hardif_entry(msg, portid, seq,
+ hard_iface)) {
+ i--;
+ break;
+ }
+ }
+
+ rcu_read_unlock();
+
+ dev_put(soft_iface);
+
+ cb->args[0] = i;
+
+ return msg->len;
+}
+
static struct genl_ops batadv_netlink_ops[] = {
{
.cmd = BATADV_CMD_GET_MESH_INFO,
@@ -176,6 +295,12 @@ static struct genl_ops batadv_netlink_ops[] = {
.policy = batadv_netlink_policy,
.dumpit = batadv_algo_dump,
},
+ {
+ .cmd = BATADV_CMD_GET_HARDIFS,
+ .flags = GENL_ADMIN_PERM,
+ .policy = batadv_netlink_policy,
+ .dumpit = batadv_netlink_dump_hardifs,
+ },
};
/**
--
2.8.0.rc3
next prev parent reply other threads:[~2016-05-16 17:28 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-16 17:27 [B.A.T.M.A.N.] [PATCH v6 00/11] netns and netlink support Andrew Lunn
2016-05-16 17:27 ` [B.A.T.M.A.N.] [PATCH v6 01/11] batman-adv: Handle parent interfaces in a different netns Andrew Lunn
2016-05-17 6:33 ` Sven Eckelmann
2016-05-17 12:18 ` Andrew Lunn
2016-05-17 12:22 ` Sven Eckelmann
2016-05-17 12:38 ` Andrew Lunn
2016-05-16 17:28 ` [B.A.T.M.A.N.] [PATCH v6 02/11] batman-adv: Suppress debugfs entries for netns's Andrew Lunn
2016-05-16 17:28 ` [B.A.T.M.A.N.] [PATCH v6 03/11] batman-adv: netlink: add routing_algo query Andrew Lunn
2016-05-16 17:28 ` Andrew Lunn [this message]
2016-05-16 17:28 ` [B.A.T.M.A.N.] [PATCH v6 05/11] batman-adv: netlink: add translation table query Andrew Lunn
2016-05-16 17:28 ` [B.A.T.M.A.N.] [PATCH v6 06/11] batman-adv: netlink: add originator and neighbor table queries Andrew Lunn
2016-05-16 17:28 ` [B.A.T.M.A.N.] [PATCH v6 07/11] batman-adv: add B.A.T.M.A.N. IV bat_{orig, neigh}_dump implementations Andrew Lunn
2016-05-16 17:28 ` [B.A.T.M.A.N.] [PATCH v6 08/11] batman-adv: add B.A.T.M.A.N. V " Andrew Lunn
2016-05-16 17:28 ` [B.A.T.M.A.N.] [PATCH v6 09/11] batman-adv: add B.A.T.M.A.N. Dump gateways via netlink Andrew Lunn
2016-05-16 17:28 ` [B.A.T.M.A.N.] [PATCH v6 10/11] batman-adv: add B.A.T.M.A.N. Dump BLA claims " Andrew Lunn
2016-05-16 17:28 ` [B.A.T.M.A.N.] [PATCH v6 11/11] batman-adv: Indicate netlink socket can be used with netns Andrew Lunn
2016-05-16 18:58 ` [B.A.T.M.A.N.] [PATCH v6 00/11] netns and netlink support Sven Eckelmann
2016-05-16 20:06 ` Andrew Lunn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1463419689-3580-5-git-send-email-andrew@lunn.ch \
--to=andrew@lunn.ch \
--cc=b.a.t.m.a.n@lists.open-mesh.org \
--cc=sven.eckelmann@open-mesh.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox