Skip to content

Commit 41aeefc

Browse files
T-Xsimonwunderlich
authored andcommitted
batman-adv: add DAT cache netlink support
Dump the list of DAT cache entries via the netlink socket. Signed-off-by: Linus Lüssing <[email protected]> Signed-off-by: Sven Eckelmann <[email protected]> Signed-off-by: Simon Wunderlich <[email protected]>
1 parent 6b25360 commit 41aeefc

File tree

4 files changed

+223
-33
lines changed

4 files changed

+223
-33
lines changed

include/uapi/linux/batman_adv.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,21 @@ enum batadv_nl_attrs {
272272
*/
273273
BATADV_ATTR_BLA_CRC,
274274

275+
/**
276+
* @BATADV_ATTR_DAT_CACHE_IP4ADDRESS: Client IPv4 address
277+
*/
278+
BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
279+
280+
/**
281+
* @BATADV_ATTR_DAT_CACHE_HWADDRESS: Client MAC address
282+
*/
283+
BATADV_ATTR_DAT_CACHE_HWADDRESS,
284+
285+
/**
286+
* @BATADV_ATTR_DAT_CACHE_VID: VLAN ID
287+
*/
288+
BATADV_ATTR_DAT_CACHE_VID,
289+
275290
/* add attributes above here, update the policy in netlink.c */
276291

277292
/**
@@ -361,6 +376,11 @@ enum batadv_nl_commands {
361376
*/
362377
BATADV_CMD_GET_BLA_BACKBONE,
363378

379+
/**
380+
* @BATADV_CMD_GET_DAT_CACHE: Query list of DAT cache entries
381+
*/
382+
BATADV_CMD_GET_DAT_CACHE,
383+
364384
/* add new commands above here */
365385

366386
/**

net/batman-adv/distributed-arp-table.c

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <linux/kernel.h>
3434
#include <linux/kref.h>
3535
#include <linux/list.h>
36+
#include <linux/netlink.h>
3637
#include <linux/rculist.h>
3738
#include <linux/rcupdate.h>
3839
#include <linux/seq_file.h>
@@ -43,13 +44,19 @@
4344
#include <linux/string.h>
4445
#include <linux/workqueue.h>
4546
#include <net/arp.h>
47+
#include <net/genetlink.h>
48+
#include <net/netlink.h>
49+
#include <net/sock.h>
50+
#include <uapi/linux/batman_adv.h>
4651

4752
#include "bridge_loop_avoidance.h"
4853
#include "hard-interface.h"
4954
#include "hash.h"
5055
#include "log.h"
56+
#include "netlink.h"
5157
#include "originator.h"
5258
#include "send.h"
59+
#include "soft-interface.h"
5360
#include "translation-table.h"
5461
#include "tvlv.h"
5562

@@ -851,6 +858,151 @@ int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset)
851858
}
852859
#endif
853860

861+
/**
862+
* batadv_dat_cache_dump_entry() - dump one entry of the DAT cache table to a
863+
* netlink socket
864+
* @msg: buffer for the message
865+
* @portid: netlink port
866+
* @seq: Sequence number of netlink message
867+
* @dat_entry: entry to dump
868+
*
869+
* Return: 0 or error code.
870+
*/
871+
static int
872+
batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
873+
struct batadv_dat_entry *dat_entry)
874+
{
875+
int msecs;
876+
void *hdr;
877+
878+
hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
879+
NLM_F_MULTI, BATADV_CMD_GET_DAT_CACHE);
880+
if (!hdr)
881+
return -ENOBUFS;
882+
883+
msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
884+
885+
if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
886+
dat_entry->ip) ||
887+
nla_put(msg, BATADV_ATTR_DAT_CACHE_HWADDRESS, ETH_ALEN,
888+
dat_entry->mac_addr) ||
889+
nla_put_u16(msg, BATADV_ATTR_DAT_CACHE_VID, dat_entry->vid) ||
890+
nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, msecs)) {
891+
genlmsg_cancel(msg, hdr);
892+
return -EMSGSIZE;
893+
}
894+
895+
genlmsg_end(msg, hdr);
896+
return 0;
897+
}
898+
899+
/**
900+
* batadv_dat_cache_dump_bucket() - dump one bucket of the DAT cache table to
901+
* a netlink socket
902+
* @msg: buffer for the message
903+
* @portid: netlink port
904+
* @seq: Sequence number of netlink message
905+
* @head: bucket to dump
906+
* @idx_skip: How many entries to skip
907+
*
908+
* Return: 0 or error code.
909+
*/
910+
static int
911+
batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
912+
struct hlist_head *head, int *idx_skip)
913+
{
914+
struct batadv_dat_entry *dat_entry;
915+
int idx = 0;
916+
917+
rcu_read_lock();
918+
hlist_for_each_entry_rcu(dat_entry, head, hash_entry) {
919+
if (idx < *idx_skip)
920+
goto skip;
921+
922+
if (batadv_dat_cache_dump_entry(msg, portid, seq,
923+
dat_entry)) {
924+
rcu_read_unlock();
925+
*idx_skip = idx;
926+
927+
return -EMSGSIZE;
928+
}
929+
930+
skip:
931+
idx++;
932+
}
933+
rcu_read_unlock();
934+
935+
return 0;
936+
}
937+
938+
/**
939+
* batadv_dat_cache_dump() - dump DAT cache table to a netlink socket
940+
* @msg: buffer for the message
941+
* @cb: callback structure containing arguments
942+
*
943+
* Return: message length.
944+
*/
945+
int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
946+
{
947+
struct batadv_hard_iface *primary_if = NULL;
948+
int portid = NETLINK_CB(cb->skb).portid;
949+
struct net *net = sock_net(cb->skb->sk);
950+
struct net_device *soft_iface;
951+
struct batadv_hashtable *hash;
952+
struct batadv_priv *bat_priv;
953+
int bucket = cb->args[0];
954+
struct hlist_head *head;
955+
int idx = cb->args[1];
956+
int ifindex;
957+
int ret = 0;
958+
959+
ifindex = batadv_netlink_get_ifindex(cb->nlh,
960+
BATADV_ATTR_MESH_IFINDEX);
961+
if (!ifindex)
962+
return -EINVAL;
963+
964+
soft_iface = dev_get_by_index(net, ifindex);
965+
if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
966+
ret = -ENODEV;
967+
goto out;
968+
}
969+
970+
bat_priv = netdev_priv(soft_iface);
971+
hash = bat_priv->dat.hash;
972+
973+
primary_if = batadv_primary_if_get_selected(bat_priv);
974+
if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
975+
ret = -ENOENT;
976+
goto out;
977+
}
978+
979+
while (bucket < hash->size) {
980+
head = &hash->table[bucket];
981+
982+
if (batadv_dat_cache_dump_bucket(msg, portid,
983+
cb->nlh->nlmsg_seq, head,
984+
&idx))
985+
break;
986+
987+
bucket++;
988+
idx = 0;
989+
}
990+
991+
cb->args[0] = bucket;
992+
cb->args[1] = idx;
993+
994+
ret = msg->len;
995+
996+
out:
997+
if (primary_if)
998+
batadv_hardif_put(primary_if);
999+
1000+
if (soft_iface)
1001+
dev_put(soft_iface);
1002+
1003+
return ret;
1004+
}
1005+
8541006
/**
8551007
* batadv_arp_get_type() - parse an ARP packet and gets the type
8561008
* @bat_priv: the bat priv with all the soft interface information

net/batman-adv/distributed-arp-table.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
#include "originator.h"
3030

31+
struct netlink_callback;
3132
struct seq_file;
3233
struct sk_buff;
3334

@@ -81,6 +82,7 @@ batadv_dat_init_own_addr(struct batadv_priv *bat_priv,
8182
int batadv_dat_init(struct batadv_priv *bat_priv);
8283
void batadv_dat_free(struct batadv_priv *bat_priv);
8384
int batadv_dat_cache_seq_print_text(struct seq_file *seq, void *offset);
85+
int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb);
8486

8587
/**
8688
* batadv_dat_inc_counter() - increment the correct DAT packet counter
@@ -169,6 +171,12 @@ static inline void batadv_dat_free(struct batadv_priv *bat_priv)
169171
{
170172
}
171173

174+
static inline int
175+
batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
176+
{
177+
return -EOPNOTSUPP;
178+
}
179+
172180
static inline void batadv_dat_inc_counter(struct batadv_priv *bat_priv,
173181
u8 subtype)
174182
{

net/batman-adv/netlink.c

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545

4646
#include "bat_algo.h"
4747
#include "bridge_loop_avoidance.h"
48+
#include "distributed-arp-table.h"
4849
#include "gateway_client.h"
4950
#include "hard-interface.h"
5051
#include "originator.h"
@@ -64,39 +65,42 @@ static const struct genl_multicast_group batadv_netlink_mcgrps[] = {
6465
};
6566

6667
static const struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = {
67-
[BATADV_ATTR_VERSION] = { .type = NLA_STRING },
68-
[BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING },
69-
[BATADV_ATTR_MESH_IFINDEX] = { .type = NLA_U32 },
70-
[BATADV_ATTR_MESH_IFNAME] = { .type = NLA_STRING },
71-
[BATADV_ATTR_MESH_ADDRESS] = { .len = ETH_ALEN },
72-
[BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 },
73-
[BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING },
74-
[BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN },
75-
[BATADV_ATTR_ORIG_ADDRESS] = { .len = ETH_ALEN },
76-
[BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 },
77-
[BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 },
78-
[BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 },
79-
[BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 },
80-
[BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG },
81-
[BATADV_ATTR_TT_ADDRESS] = { .len = ETH_ALEN },
82-
[BATADV_ATTR_TT_TTVN] = { .type = NLA_U8 },
83-
[BATADV_ATTR_TT_LAST_TTVN] = { .type = NLA_U8 },
84-
[BATADV_ATTR_TT_CRC32] = { .type = NLA_U32 },
85-
[BATADV_ATTR_TT_VID] = { .type = NLA_U16 },
86-
[BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 },
87-
[BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG },
88-
[BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 },
89-
[BATADV_ATTR_NEIGH_ADDRESS] = { .len = ETH_ALEN },
90-
[BATADV_ATTR_TQ] = { .type = NLA_U8 },
91-
[BATADV_ATTR_THROUGHPUT] = { .type = NLA_U32 },
92-
[BATADV_ATTR_BANDWIDTH_UP] = { .type = NLA_U32 },
93-
[BATADV_ATTR_BANDWIDTH_DOWN] = { .type = NLA_U32 },
94-
[BATADV_ATTR_ROUTER] = { .len = ETH_ALEN },
95-
[BATADV_ATTR_BLA_OWN] = { .type = NLA_FLAG },
96-
[BATADV_ATTR_BLA_ADDRESS] = { .len = ETH_ALEN },
97-
[BATADV_ATTR_BLA_VID] = { .type = NLA_U16 },
98-
[BATADV_ATTR_BLA_BACKBONE] = { .len = ETH_ALEN },
99-
[BATADV_ATTR_BLA_CRC] = { .type = NLA_U16 },
68+
[BATADV_ATTR_VERSION] = { .type = NLA_STRING },
69+
[BATADV_ATTR_ALGO_NAME] = { .type = NLA_STRING },
70+
[BATADV_ATTR_MESH_IFINDEX] = { .type = NLA_U32 },
71+
[BATADV_ATTR_MESH_IFNAME] = { .type = NLA_STRING },
72+
[BATADV_ATTR_MESH_ADDRESS] = { .len = ETH_ALEN },
73+
[BATADV_ATTR_HARD_IFINDEX] = { .type = NLA_U32 },
74+
[BATADV_ATTR_HARD_IFNAME] = { .type = NLA_STRING },
75+
[BATADV_ATTR_HARD_ADDRESS] = { .len = ETH_ALEN },
76+
[BATADV_ATTR_ORIG_ADDRESS] = { .len = ETH_ALEN },
77+
[BATADV_ATTR_TPMETER_RESULT] = { .type = NLA_U8 },
78+
[BATADV_ATTR_TPMETER_TEST_TIME] = { .type = NLA_U32 },
79+
[BATADV_ATTR_TPMETER_BYTES] = { .type = NLA_U64 },
80+
[BATADV_ATTR_TPMETER_COOKIE] = { .type = NLA_U32 },
81+
[BATADV_ATTR_ACTIVE] = { .type = NLA_FLAG },
82+
[BATADV_ATTR_TT_ADDRESS] = { .len = ETH_ALEN },
83+
[BATADV_ATTR_TT_TTVN] = { .type = NLA_U8 },
84+
[BATADV_ATTR_TT_LAST_TTVN] = { .type = NLA_U8 },
85+
[BATADV_ATTR_TT_CRC32] = { .type = NLA_U32 },
86+
[BATADV_ATTR_TT_VID] = { .type = NLA_U16 },
87+
[BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 },
88+
[BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG },
89+
[BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 },
90+
[BATADV_ATTR_NEIGH_ADDRESS] = { .len = ETH_ALEN },
91+
[BATADV_ATTR_TQ] = { .type = NLA_U8 },
92+
[BATADV_ATTR_THROUGHPUT] = { .type = NLA_U32 },
93+
[BATADV_ATTR_BANDWIDTH_UP] = { .type = NLA_U32 },
94+
[BATADV_ATTR_BANDWIDTH_DOWN] = { .type = NLA_U32 },
95+
[BATADV_ATTR_ROUTER] = { .len = ETH_ALEN },
96+
[BATADV_ATTR_BLA_OWN] = { .type = NLA_FLAG },
97+
[BATADV_ATTR_BLA_ADDRESS] = { .len = ETH_ALEN },
98+
[BATADV_ATTR_BLA_VID] = { .type = NLA_U16 },
99+
[BATADV_ATTR_BLA_BACKBONE] = { .len = ETH_ALEN },
100+
[BATADV_ATTR_BLA_CRC] = { .type = NLA_U16 },
101+
[BATADV_ATTR_DAT_CACHE_IP4ADDRESS] = { .type = NLA_U32 },
102+
[BATADV_ATTR_DAT_CACHE_HWADDRESS] = { .len = ETH_ALEN },
103+
[BATADV_ATTR_DAT_CACHE_VID] = { .type = NLA_U16 },
100104
};
101105

102106
/**
@@ -604,6 +608,12 @@ static const struct genl_ops batadv_netlink_ops[] = {
604608
.policy = batadv_netlink_policy,
605609
.dumpit = batadv_bla_backbone_dump,
606610
},
611+
{
612+
.cmd = BATADV_CMD_GET_DAT_CACHE,
613+
.flags = GENL_ADMIN_PERM,
614+
.policy = batadv_netlink_policy,
615+
.dumpit = batadv_dat_cache_dump,
616+
},
607617

608618
};
609619

0 commit comments

Comments
 (0)