From: Matthias Schiffer <mschif...@universe-factory.net>

This adds the commands BATADV_CMD_GET_TRANSTABLE_LOCAL and
BATADV_CMD_GET_TRANSTABLE_GLOBAL, which correspond to the transtable_local
and transtable_global debugfs files.

The batadv_tt_client_flags enum is moved to the UAPI to expose it as part
of the netlink API.

Signed-off-by: Matthias Schiffer <mschif...@universe-factory.net>
Signed-off-by: Andrew Lunn <and...@lunn.ch>
---
v2: Add missing header files.
---
 include/uapi/linux/batman_adv.h    |  47 ++++++
 net/batman-adv/netlink.c           |  13 ++
 net/batman-adv/packet.h            |  36 -----
 net/batman-adv/translation-table.c | 308 +++++++++++++++++++++++++++++++++++++
 net/batman-adv/translation-table.h |   4 +
 5 files changed, 372 insertions(+), 36 deletions(-)

diff --git a/include/uapi/linux/batman_adv.h b/include/uapi/linux/batman_adv.h
index b9b7dfd..25dee3c 100644
--- a/include/uapi/linux/batman_adv.h
+++ b/include/uapi/linux/batman_adv.h
@@ -20,6 +20,42 @@
 
 #define BATADV_NL_NAME "batadv"
 
+/**
+ * enum batadv_tt_client_flags - TT client specific flags
+ * @BATADV_TT_CLIENT_DEL: the client has to be deleted from the table
+ * @BATADV_TT_CLIENT_ROAM: the client roamed to/from another node and the new
+ *  update telling its new real location has not been received/sent yet
+ * @BATADV_TT_CLIENT_WIFI: this client is connected through a wifi interface.
+ *  This information is used by the "AP Isolation" feature
+ * @BATADV_TT_CLIENT_ISOLA: this client is considered "isolated". This
+ *  information is used by the Extended Isolation feature
+ * @BATADV_TT_CLIENT_NOPURGE: this client should never be removed from the 
table
+ * @BATADV_TT_CLIENT_NEW: this client has been added to the local table but has
+ *  not been announced yet
+ * @BATADV_TT_CLIENT_PENDING: this client is marked for removal but it is kept
+ *  in the table for one more originator interval for consistency purposes
+ * @BATADV_TT_CLIENT_TEMP: this global client has been detected to be part of
+ *  the network but no nnode has already announced it
+ *
+ * Bits from 0 to 7 are called _remote flags_ because they are sent on the 
wire.
+ * Bits from 8 to 15 are called _local flags_ because they are used for local
+ * computations only.
+ *
+ * Bits from 4 to 7 - a subset of remote flags - are ensured to be in sync with
+ * the other nodes in the network. To achieve this goal these flags are 
included
+ * in the TT CRC computation.
+ */
+enum batadv_tt_client_flags {
+       BATADV_TT_CLIENT_DEL     = (1 << 0),
+       BATADV_TT_CLIENT_ROAM    = (1 << 1),
+       BATADV_TT_CLIENT_WIFI    = (1 << 4),
+       BATADV_TT_CLIENT_ISOLA   = (1 << 5),
+       BATADV_TT_CLIENT_NOPURGE = (1 << 8),
+       BATADV_TT_CLIENT_NEW     = (1 << 9),
+       BATADV_TT_CLIENT_PENDING = (1 << 10),
+       BATADV_TT_CLIENT_TEMP    = (1 << 11),
+};
+
 enum {
        BATADV_ATTR_UNSPEC,
        BATADV_ATTR_VERSION,
@@ -31,6 +67,15 @@ enum {
        BATADV_ATTR_HARD_IFNAME,
        BATADV_ATTR_HARD_ADDRESS,
        BATADV_ATTR_ACTIVE,
+       BATADV_ATTR_ORIG_ADDRESS,
+       BATADV_ATTR_TT_ADDRESS,
+       BATADV_ATTR_TT_TTVN,
+       BATADV_ATTR_TT_LAST_TTVN,
+       BATADV_ATTR_TT_CRC32,
+       BATADV_ATTR_TT_VID,
+       BATADV_ATTR_TT_FLAGS,
+       BATADV_ATTR_FLAG_BEST,
+       BATADV_ATTR_LAST_SEEN_MSECS,
        __BATADV_ATTR_MAX,
 };
 
@@ -41,6 +86,8 @@ enum {
        BATADV_CMD_GET_ROUTING_ALGOS,
        BATADV_CMD_GET_MESH_INFO,
        BATADV_CMD_GET_HARDIFS,
+       BATADV_CMD_GET_TRANSTABLE_LOCAL,
+       BATADV_CMD_GET_TRANSTABLE_GLOBAL,
        __BATADV_CMD_MAX,
 };
 
diff --git a/net/batman-adv/netlink.c b/net/batman-adv/netlink.c
index e302de3..3fb1c1f 100644
--- a/net/batman-adv/netlink.c
+++ b/net/batman-adv/netlink.c
@@ -35,6 +35,7 @@
 
 #include "hard-interface.h"
 #include "soft-interface.h"
+#include "translation-table.h"
 
 struct genl_family batadv_netlink_family = {
        .id = GENL_ID_GENERATE,
@@ -242,6 +243,18 @@ static struct genl_ops batadv_netlink_ops[] = {
                .policy = batadv_netlink_policy,
                .dumpit = batadv_netlink_dump_hardifs,
        },
+       {
+               .cmd = BATADV_CMD_GET_TRANSTABLE_LOCAL,
+               .flags = GENL_ADMIN_PERM,
+               .policy = batadv_netlink_policy,
+               .dumpit = batadv_tt_local_dump,
+       },
+       {
+               .cmd = BATADV_CMD_GET_TRANSTABLE_GLOBAL,
+               .flags = GENL_ADMIN_PERM,
+               .policy = batadv_netlink_policy,
+               .dumpit = batadv_tt_global_dump,
+       },
 };
 
 void __init batadv_netlink_register(void)
diff --git a/net/batman-adv/packet.h b/net/batman-adv/packet.h
index 372128d..b45460d 100644
--- a/net/batman-adv/packet.h
+++ b/net/batman-adv/packet.h
@@ -126,42 +126,6 @@ enum batadv_tt_data_flags {
 };
 
 /**
- * enum batadv_tt_client_flags - TT client specific flags
- * @BATADV_TT_CLIENT_DEL: the client has to be deleted from the table
- * @BATADV_TT_CLIENT_ROAM: the client roamed to/from another node and the new
- *  update telling its new real location has not been received/sent yet
- * @BATADV_TT_CLIENT_WIFI: this client is connected through a wifi interface.
- *  This information is used by the "AP Isolation" feature
- * @BATADV_TT_CLIENT_ISOLA: this client is considered "isolated". This
- *  information is used by the Extended Isolation feature
- * @BATADV_TT_CLIENT_NOPURGE: this client should never be removed from the 
table
- * @BATADV_TT_CLIENT_NEW: this client has been added to the local table but has
- *  not been announced yet
- * @BATADV_TT_CLIENT_PENDING: this client is marked for removal but it is kept
- *  in the table for one more originator interval for consistency purposes
- * @BATADV_TT_CLIENT_TEMP: this global client has been detected to be part of
- *  the network but no nnode has already announced it
- *
- * Bits from 0 to 7 are called _remote flags_ because they are sent on the 
wire.
- * Bits from 8 to 15 are called _local flags_ because they are used for local
- * computations only.
- *
- * Bits from 4 to 7 - a subset of remote flags - are ensured to be in sync with
- * the other nodes in the network. To achieve this goal these flags are 
included
- * in the TT CRC computation.
- */
-enum batadv_tt_client_flags {
-       BATADV_TT_CLIENT_DEL     = BIT(0),
-       BATADV_TT_CLIENT_ROAM    = BIT(1),
-       BATADV_TT_CLIENT_WIFI    = BIT(4),
-       BATADV_TT_CLIENT_ISOLA   = BIT(5),
-       BATADV_TT_CLIENT_NOPURGE = BIT(8),
-       BATADV_TT_CLIENT_NEW     = BIT(9),
-       BATADV_TT_CLIENT_PENDING = BIT(10),
-       BATADV_TT_CLIENT_TEMP    = BIT(11),
-};
-
-/**
  * enum batadv_vlan_flags - flags for the four MSB of any vlan ID field
  * @BATADV_VLAN_HAS_TAG: whether the field contains a valid vlan tag or not
  */
diff --git a/net/batman-adv/translation-table.c 
b/net/batman-adv/translation-table.c
index feaf492..be42b12 100644
--- a/net/batman-adv/translation-table.c
+++ b/net/batman-adv/translation-table.c
@@ -35,19 +35,26 @@
 #include <linux/list.h>
 #include <linux/lockdep.h>
 #include <linux/netdevice.h>
+#include <linux/netlink.h>
 #include <linux/rculist.h>
 #include <linux/rcupdate.h>
 #include <linux/seq_file.h>
+#include <linux/skbuff.h>
 #include <linux/slab.h>
 #include <linux/spinlock.h>
 #include <linux/stddef.h>
 #include <linux/string.h>
 #include <linux/workqueue.h>
+#include <net/genetlink.h>
+#include <net/netlink.h>
+#include <net/sock.h>
+#include <uapi/linux/batman_adv.h>
 
 #include "bridge_loop_avoidance.h"
 #include "hard-interface.h"
 #include "hash.h"
 #include "multicast.h"
+#include "netlink.h"
 #include "originator.h"
 #include "packet.h"
 #include "soft-interface.h"
@@ -1056,6 +1063,138 @@ out:
        return 0;
 }
 
+static int
+batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
+                          struct batadv_priv *bat_priv,
+                          struct batadv_tt_common_entry *common)
+{
+       void *hdr;
+       struct batadv_softif_vlan *vlan;
+       struct batadv_tt_local_entry *local;
+       unsigned int last_seen_msecs;
+       u32 crc;
+
+       local = container_of(common, struct batadv_tt_local_entry, common);
+       last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
+
+       vlan = batadv_softif_vlan_get(bat_priv, common->vid);
+       if (!vlan)
+               return 0;
+
+       crc = vlan->tt.crc;
+
+       batadv_softif_vlan_put(vlan);
+
+       hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
+                         BATADV_CMD_GET_TRANSTABLE_LOCAL);
+       if (!hdr)
+               return -ENOBUFS;
+
+       if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
+           nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
+           nla_put_u16(msg, BATADV_ATTR_TT_VID,
+                       BATADV_PRINT_VID(common->vid)) ||
+           nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
+               goto nla_put_failure;
+
+       if (!(common->flags & BATADV_TT_CLIENT_NOPURGE)) {
+               if (nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
+                               last_seen_msecs))
+                       goto nla_put_failure;
+       }
+
+       genlmsg_end(msg, hdr);
+       return 0;
+
+ nla_put_failure:
+       genlmsg_cancel(msg, hdr);
+       return -EMSGSIZE;
+}
+
+static int
+batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
+                           struct batadv_priv *bat_priv,
+                           struct hlist_head *head, int *idx_s)
+{
+       struct batadv_tt_common_entry *common;
+       int idx = 0;
+
+       rcu_read_lock();
+       hlist_for_each_entry_rcu(common, head, hash_entry) {
+               if (idx++ < *idx_s)
+                       continue;
+
+               if (batadv_tt_local_dump_entry(msg, portid, seq, bat_priv,
+                                              common)) {
+                       rcu_read_unlock();
+                       *idx_s = idx - 1;
+                       return -EMSGSIZE;
+               }
+       }
+       rcu_read_unlock();
+
+       *idx_s = 0;
+       return 0;
+}
+
+int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
+{
+       struct net *net = sock_net(cb->skb->sk);
+       struct net_device *soft_iface = NULL;
+       struct batadv_priv *bat_priv;
+       struct batadv_hard_iface *primary_if = NULL;
+       struct batadv_hashtable *hash;
+       struct hlist_head *head;
+       int ret;
+       int ifindex;
+       int bucket = cb->args[0];
+       int idx = cb->args[1];
+       int portid = NETLINK_CB(cb->skb).portid;
+
+       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 || !batadv_softif_is_valid(soft_iface)) {
+               ret = -ENODEV;
+               goto out;
+       }
+
+       bat_priv = netdev_priv(soft_iface);
+
+       primary_if = batadv_primary_if_get_selected(bat_priv);
+       if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
+               ret = -ENOENT;
+               goto out;
+       }
+
+       hash = bat_priv->tt.local_hash;
+
+       while (bucket < hash->size) {
+               head = &hash->table[bucket];
+
+               if (batadv_tt_local_dump_bucket(msg, portid, cb->nlh->nlmsg_seq,
+                                               bat_priv, head, &idx))
+                       break;
+
+               bucket++;
+       }
+
+       ret = msg->len;
+
+ out:
+       if (primary_if)
+               batadv_hardif_put(primary_if);
+       if (soft_iface)
+               dev_put(soft_iface);
+
+       cb->args[0] = bucket;
+       cb->args[1] = idx;
+
+       return ret;
+}
+
 static void
 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
                            struct batadv_tt_local_entry *tt_local_entry,
@@ -1702,6 +1841,175 @@ out:
        return 0;
 }
 
+static int
+batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
+                              struct batadv_tt_common_entry *common,
+                              struct batadv_tt_orig_list_entry *orig,
+                              bool best)
+{
+       void *hdr;
+       struct batadv_orig_node_vlan *vlan;
+       u8 last_ttvn;
+       u32 crc;
+
+       vlan = batadv_orig_node_vlan_get(orig->orig_node,
+                                        common->vid);
+       if (!vlan)
+               return 0;
+
+       crc = vlan->tt.crc;
+
+       batadv_orig_node_vlan_put(vlan);
+
+       hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, NLM_F_MULTI,
+                         BATADV_CMD_GET_TRANSTABLE_GLOBAL);
+       if (!hdr)
+               return -ENOBUFS;
+
+       last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
+
+       if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
+           nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
+                   orig->orig_node->orig) ||
+           nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
+           nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
+           nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
+           nla_put_u16(msg, BATADV_ATTR_TT_VID,
+                       BATADV_PRINT_VID(common->vid)) ||
+           nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
+               goto nla_put_failure;
+
+       if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
+               goto nla_put_failure;
+
+       genlmsg_end(msg, hdr);
+       return 0;
+
+ nla_put_failure:
+       genlmsg_cancel(msg, hdr);
+       return -EMSGSIZE;
+}
+
+static int
+batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
+                           struct batadv_priv *bat_priv,
+                           struct batadv_tt_common_entry *common, int *sub_s)
+{
+       struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
+       struct batadv_tt_global_entry *global;
+       struct hlist_head *head;
+       int sub = 0;
+       bool best;
+
+       global = container_of(common, struct batadv_tt_global_entry, common);
+       best_entry = batadv_transtable_best_orig(bat_priv, global);
+       head = &global->orig_list;
+
+       hlist_for_each_entry_rcu(orig_entry, head, list) {
+               if (sub++ < *sub_s)
+                       continue;
+
+               best = (orig_entry == best_entry);
+
+               if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
+                                                  orig_entry, best)) {
+                       *sub_s = sub - 1;
+                       return -EMSGSIZE;
+               }
+       }
+
+       *sub_s = 0;
+       return 0;
+}
+
+static int
+batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
+                            struct batadv_priv *bat_priv,
+                            struct hlist_head *head, int *idx_s, int *sub)
+{
+       struct batadv_tt_common_entry *common;
+       int idx = 0;
+
+       rcu_read_lock();
+       hlist_for_each_entry_rcu(common, head, hash_entry) {
+               if (idx++ < *idx_s)
+                       continue;
+
+               if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
+                                               common, sub)) {
+                       rcu_read_unlock();
+                       *idx_s = idx - 1;
+                       return -EMSGSIZE;
+               }
+       }
+       rcu_read_unlock();
+
+       *idx_s = 0;
+       *sub = 0;
+       return 0;
+}
+
+int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
+{
+       struct net *net = sock_net(cb->skb->sk);
+       struct net_device *soft_iface = NULL;
+       struct batadv_priv *bat_priv;
+       struct batadv_hard_iface *primary_if = NULL;
+       struct batadv_hashtable *hash;
+       struct hlist_head *head;
+       int ret;
+       int ifindex;
+       int bucket = cb->args[0];
+       int idx = cb->args[1];
+       int sub = cb->args[2];
+       int portid = NETLINK_CB(cb->skb).portid;
+
+       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 || !batadv_softif_is_valid(soft_iface)) {
+               ret = -ENODEV;
+               goto out;
+       }
+
+       bat_priv = netdev_priv(soft_iface);
+
+       primary_if = batadv_primary_if_get_selected(bat_priv);
+       if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
+               ret = -ENOENT;
+               goto out;
+       }
+
+       hash = bat_priv->tt.global_hash;
+
+       while (bucket < hash->size) {
+               head = &hash->table[bucket];
+
+               if (batadv_tt_global_dump_bucket(msg, portid,
+                                                cb->nlh->nlmsg_seq, bat_priv,
+                                                head, &idx, &sub))
+                       break;
+
+               bucket++;
+       }
+
+       ret = msg->len;
+
+ out:
+       if (primary_if)
+               batadv_hardif_put(primary_if);
+       if (soft_iface)
+               dev_put(soft_iface);
+
+       cb->args[0] = bucket;
+       cb->args[1] = idx;
+       cb->args[2] = sub;
+
+       return ret;
+}
+
 /**
  * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
  * @tt_global_entry: the global entry to remove the orig_entry from
diff --git a/net/batman-adv/translation-table.h 
b/net/batman-adv/translation-table.h
index 7c7e2c0..4697af6 100644
--- a/net/batman-adv/translation-table.h
+++ b/net/batman-adv/translation-table.h
@@ -22,8 +22,10 @@
 
 #include <linux/types.h>
 
+struct netlink_callback;
 struct net_device;
 struct seq_file;
+struct sk_buff;
 
 int batadv_tt_init(struct batadv_priv *bat_priv);
 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
@@ -33,6 +35,8 @@ u16 batadv_tt_local_remove(struct batadv_priv *bat_priv,
                           const char *message, bool roaming);
 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset);
 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset);
+int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb);
+int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb);
 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
                               struct batadv_orig_node *orig_node,
                               s32 match_vid, const char *message);
-- 
2.8.0.rc3

Reply via email to