batman-adv: Add inconsistent dat netlink dump detection

The netlink dump functionality transfers a large number of entries from the
kernel to userspace. It is rather likely that the transfer has to
interrupted and later continued. During that time, it can happen that
either new entries are added or removed. The userspace could than either
receive some entries multiple times or miss entries.

Commit 670dc2833d ("netlink: advertise incomplete dumps") introduced a
mechanism to inform userspace about this problem. Userspace can then decide
whether it is necessary or not to retry dumping the information again.

The netlink dump functions have to be switched to exclusive locks to avoid
changes while the current message is prepared. The already existing
generation sequence counter from the hash helper can be used for this
simple hash.

Reported-by: Matthias Schiffer <mschiffer@universe-factory.net>
Signed-off-by: Sven Eckelmann <sven@narfation.org>
Signed-off-by: Simon Wunderlich <sw@simonwunderlich.de>
This commit is contained in:
Sven Eckelmann 2018-10-30 22:01:28 +01:00 committed by Simon Wunderlich
parent 24d71b9232
commit 6f81652a47

View File

@ -863,23 +863,27 @@ out:
* netlink socket * netlink socket
* @msg: buffer for the message * @msg: buffer for the message
* @portid: netlink port * @portid: netlink port
* @seq: Sequence number of netlink message * @cb: Control block containing additional options
* @dat_entry: entry to dump * @dat_entry: entry to dump
* *
* Return: 0 or error code. * Return: 0 or error code.
*/ */
static int static int
batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid, u32 seq, batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid,
struct netlink_callback *cb,
struct batadv_dat_entry *dat_entry) struct batadv_dat_entry *dat_entry)
{ {
int msecs; int msecs;
void *hdr; void *hdr;
hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family, hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
NLM_F_MULTI, BATADV_CMD_GET_DAT_CACHE); &batadv_netlink_family, NLM_F_MULTI,
BATADV_CMD_GET_DAT_CACHE);
if (!hdr) if (!hdr)
return -ENOBUFS; return -ENOBUFS;
genl_dump_check_consistent(cb, hdr);
msecs = jiffies_to_msecs(jiffies - dat_entry->last_update); msecs = jiffies_to_msecs(jiffies - dat_entry->last_update);
if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS, if (nla_put_in_addr(msg, BATADV_ATTR_DAT_CACHE_IP4ADDRESS,
@ -901,27 +905,31 @@ batadv_dat_cache_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
* a netlink socket * a netlink socket
* @msg: buffer for the message * @msg: buffer for the message
* @portid: netlink port * @portid: netlink port
* @seq: Sequence number of netlink message * @cb: Control block containing additional options
* @head: bucket to dump * @hash: hash to dump
* @bucket: bucket index to dump
* @idx_skip: How many entries to skip * @idx_skip: How many entries to skip
* *
* Return: 0 or error code. * Return: 0 or error code.
*/ */
static int static int
batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq, batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid,
struct hlist_head *head, int *idx_skip) struct netlink_callback *cb,
struct batadv_hashtable *hash, unsigned int bucket,
int *idx_skip)
{ {
struct batadv_dat_entry *dat_entry; struct batadv_dat_entry *dat_entry;
int idx = 0; int idx = 0;
rcu_read_lock(); spin_lock_bh(&hash->list_locks[bucket]);
hlist_for_each_entry_rcu(dat_entry, head, hash_entry) { cb->seq = atomic_read(&hash->generation) << 1 | 1;
hlist_for_each_entry(dat_entry, &hash->table[bucket], hash_entry) {
if (idx < *idx_skip) if (idx < *idx_skip)
goto skip; goto skip;
if (batadv_dat_cache_dump_entry(msg, portid, seq, if (batadv_dat_cache_dump_entry(msg, portid, cb, dat_entry)) {
dat_entry)) { spin_unlock_bh(&hash->list_locks[bucket]);
rcu_read_unlock();
*idx_skip = idx; *idx_skip = idx;
return -EMSGSIZE; return -EMSGSIZE;
@ -930,7 +938,7 @@ batadv_dat_cache_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
skip: skip:
idx++; idx++;
} }
rcu_read_unlock(); spin_unlock_bh(&hash->list_locks[bucket]);
return 0; return 0;
} }
@ -951,7 +959,6 @@ int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
struct batadv_hashtable *hash; struct batadv_hashtable *hash;
struct batadv_priv *bat_priv; struct batadv_priv *bat_priv;
int bucket = cb->args[0]; int bucket = cb->args[0];
struct hlist_head *head;
int idx = cb->args[1]; int idx = cb->args[1];
int ifindex; int ifindex;
int ret = 0; int ret = 0;
@ -977,10 +984,7 @@ int batadv_dat_cache_dump(struct sk_buff *msg, struct netlink_callback *cb)
} }
while (bucket < hash->size) { while (bucket < hash->size) {
head = &hash->table[bucket]; if (batadv_dat_cache_dump_bucket(msg, portid, cb, hash, bucket,
if (batadv_dat_cache_dump_bucket(msg, portid,
cb->nlh->nlmsg_seq, head,
&idx)) &idx))
break; break;