batman-adv: tt: use protected flag modifications

The flags of translation table entries are modified in various places using
RMW operations like:

* read flags + add flag + store
* read flags + remove flag + store

These were done without making sure that no other context is doing a
similar operation at the same time. If another context does modify the
flags then it could happen that a store of the flag modifications is simply
lost. This problem can usually be fixed at a later point when the flags are
tried to be adjusted again.

To reduce the time the wrong flags are used, it is better to use a TT entry
specific spinlock when accessing the u16.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
This commit is contained in:
Sven Eckelmann 2026-08-30 17:21:42 +02:00
parent 52d8c713f4
commit 976b159b3c
No known key found for this signature in database
GPG Key ID: 4D0F772BD314F5CB
2 changed files with 255 additions and 107 deletions

View File

@ -13,6 +13,7 @@
#include <linux/build_bug.h> #include <linux/build_bug.h>
#include <linux/byteorder/generic.h> #include <linux/byteorder/generic.h>
#include <linux/cache.h> #include <linux/cache.h>
#include <linux/cleanup.h>
#include <linux/compiler.h> #include <linux/compiler.h>
#include <linux/container_of.h> #include <linux/container_of.h>
#include <linux/crc32.h> #include <linux/crc32.h>
@ -433,6 +434,22 @@ batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release); kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
} }
/**
* batadv_tt_flags_get() - get a snapshot of the flags of a TT entry
* @common: tt local & tt global common data
*
* Return: the flags of the TT entry as observed under its flags_lock.
*/
static u16 batadv_tt_flags_get(struct batadv_tt_common_entry *common)
{
u16 flags;
scoped_guard(spinlock_bh, &common->flags_lock)
flags = common->flags;
return flags;
}
/** /**
* batadv_tt_local_event() - store a local TT event (ADD/DEL) * batadv_tt_local_event() - store a local TT event (ADD/DEL)
* @bat_priv: the bat priv with all the mesh interface information * @bat_priv: the bat priv with all the mesh interface information
@ -445,23 +462,25 @@ static void batadv_tt_local_event(struct batadv_priv *bat_priv,
{ {
struct batadv_tt_common_entry *common = &tt_local_entry->common; struct batadv_tt_common_entry *common = &tt_local_entry->common;
struct batadv_tt_change_node *tt_change_node; struct batadv_tt_change_node *tt_change_node;
u8 flags = common->flags | event_flags;
struct batadv_tt_change_node *entry; struct batadv_tt_change_node *entry;
struct batadv_tt_change_node *safe; struct batadv_tt_change_node *safe;
bool del_op_requested; bool del_op_requested;
bool del_op_entry; bool del_op_entry;
size_t changes; size_t changes;
u8 flags;
tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC); tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
if (!tt_change_node) if (!tt_change_node)
return; return;
tt_change_node->change.flags = flags;
memset(tt_change_node->change.reserved, 0, memset(tt_change_node->change.reserved, 0,
sizeof(tt_change_node->change.reserved)); sizeof(tt_change_node->change.reserved));
ether_addr_copy(tt_change_node->change.addr, common->addr); ether_addr_copy(tt_change_node->change.addr, common->addr);
tt_change_node->change.vid = htons(common->vid); tt_change_node->change.vid = htons(common->vid);
flags = batadv_tt_flags_get(common) | event_flags;
tt_change_node->change.flags = flags;
del_op_requested = flags & BATADV_TT_CLIENT_DEL; del_op_requested = flags & BATADV_TT_CLIENT_DEL;
/* check for ADD+DEL, DEL+ADD, ADD+ADD or DEL+DEL events */ /* check for ADD+DEL, DEL+ADD, ADD+ADD or DEL+DEL events */
@ -633,8 +652,23 @@ static void batadv_tt_local_add_roam(struct batadv_priv *bat_priv,
/* Check whether it is a roaming, but don't do anything if the roaming /* Check whether it is a roaming, but don't do anything if the roaming
* process has already been handled * process has already been handled
*/ */
if (tt_global->common.flags & BATADV_TT_CLIENT_ROAM) scoped_guard(spinlock_bh, &tt_global->common.flags_lock) {
return; if (tt_global->common.flags & BATADV_TT_CLIENT_ROAM)
return;
if (!roamed_back) {
/* The global entry has to be marked as ROAMING and has to be
* kept for consistency purpose.
*
* batadv_tt_global_to_purge() evaluates roam_at as soon as it
* observes BATADV_TT_CLIENT_ROAM, so the timeout has to be
* stamped before the flag is published. Otherwise the entry can
* be deleted right away as "Roaming timeout".
*/
tt_global->roam_at = jiffies;
tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
}
}
/* These node are probably going to update their tt table */ /* These node are probably going to update their tt table */
head = &tt_global->orig_list; head = &tt_global->orig_list;
@ -646,16 +680,8 @@ static void batadv_tt_local_add_roam(struct batadv_priv *bat_priv,
} }
rcu_read_unlock(); rcu_read_unlock();
if (roamed_back) { if (roamed_back)
batadv_tt_global_free(bat_priv, tt_global, batadv_tt_global_free(bat_priv, tt_global, "Roaming canceled");
"Roaming canceled");
} else {
/* The global entry has to be marked as ROAMING and
* has to be kept for consistency purpose
*/
tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
tt_global->roam_at = jiffies;
}
} }
/** /**
@ -688,6 +714,7 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,
int hash_added; int hash_added;
int table_size; int table_size;
u32 match_mark; u32 match_mark;
bool modified;
if (ifindex != BATADV_NULL_IFINDEX) if (ifindex != BATADV_NULL_IFINDEX)
in_dev = dev_get_by_index(net, ifindex); in_dev = dev_get_by_index(net, ifindex);
@ -705,6 +732,8 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,
if (tt_local) { if (tt_local) {
tt_local->last_seen = jiffies; tt_local->last_seen = jiffies;
spin_lock_bh(&tt_local->common.flags_lock);
if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) { if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
batadv_dbg(BATADV_DBG_TT, bat_priv, batadv_dbg(BATADV_DBG_TT, bat_priv,
"Re-adding pending client %pM (vid: %d)\n", "Re-adding pending client %pM (vid: %d)\n",
@ -715,6 +744,8 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,
* flag can be reset like it was never enqueued * flag can be reset like it was never enqueued
*/ */
tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING; tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
spin_unlock_bh(&tt_local->common.flags_lock);
goto add_event; goto add_event;
} }
@ -730,6 +761,8 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,
tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM; tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
roamed_back = true; roamed_back = true;
} }
spin_unlock_bh(&tt_local->common.flags_lock);
goto check_roaming; goto check_roaming;
} }
@ -765,18 +798,21 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,
(u8)atomic_read(&bat_priv->tt.vn)); (u8)atomic_read(&bat_priv->tt.vn));
ether_addr_copy(tt_local->common.addr, addr); ether_addr_copy(tt_local->common.addr, addr);
tt_local->common.vid = vid;
kref_init(&tt_local->common.refcount);
tt_local->last_seen = jiffies;
tt_local->common.added_at = tt_local->last_seen;
tt_local->vlan = vlan;
spin_lock_init(&tt_local->common.flags_lock);
spin_lock_bh(&tt_local->common.flags_lock);
/* The local entry has to be marked as NEW to avoid to send it in /* The local entry has to be marked as NEW to avoid to send it in
* a full table response going out before the next ttvn increment * a full table response going out before the next ttvn increment
* (consistency check) * (consistency check)
*/ */
tt_local->common.flags = BATADV_TT_CLIENT_NEW; tt_local->common.flags = BATADV_TT_CLIENT_NEW;
tt_local->common.vid = vid;
if (iif_is_wifi) if (iif_is_wifi)
tt_local->common.flags |= BATADV_TT_CLIENT_WIFI; tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
kref_init(&tt_local->common.refcount);
tt_local->last_seen = jiffies;
tt_local->common.added_at = tt_local->last_seen;
tt_local->vlan = vlan;
/* the batman interface mac and multicast addresses should never be /* the batman interface mac and multicast addresses should never be
* purged * purged
@ -784,6 +820,7 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,
if (batadv_compare_eth(addr, mesh_iface->dev_addr) || if (batadv_compare_eth(addr, mesh_iface->dev_addr) ||
is_multicast_ether_addr(addr)) is_multicast_ether_addr(addr))
tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE; tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
spin_unlock_bh(&tt_local->common.flags_lock);
kref_get(&tt_local->common.refcount); kref_get(&tt_local->common.refcount);
hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt, hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
@ -802,6 +839,7 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,
check_roaming: check_roaming:
batadv_tt_local_add_roam(bat_priv, tt_global, roamed_back); batadv_tt_local_add_roam(bat_priv, tt_global, roamed_back);
spin_lock_bh(&tt_local->common.flags_lock);
/* store the current remote flags before altering them. This helps /* store the current remote flags before altering them. This helps
* understanding is flags are changing or not * understanding is flags are changing or not
*/ */
@ -823,10 +861,13 @@ bool batadv_tt_local_add(struct net_device *mesh_iface, const u8 *addr,
else else
tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA; tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
modified = remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK);
spin_unlock_bh(&tt_local->common.flags_lock);
/* if any "dynamic" flag has been modified, resend an ADD event for this /* if any "dynamic" flag has been modified, resend an ADD event for this
* entry so that all the nodes can get the new flags * entry so that all the nodes can get the new flags
*/ */
if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK)) if (modified)
batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS); batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
ret = true; ret = true;
@ -1151,6 +1192,7 @@ batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid,
struct batadv_meshif_vlan *vlan; struct batadv_meshif_vlan *vlan;
unsigned int last_seen_msecs; unsigned int last_seen_msecs;
void *hdr; void *hdr;
u16 flags;
u32 crc; u32 crc;
local = container_of(common, struct batadv_tt_local_entry, common); local = container_of(common, struct batadv_tt_local_entry, common);
@ -1172,13 +1214,15 @@ batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid,
genl_dump_check_consistent(cb, hdr); genl_dump_check_consistent(cb, hdr);
flags = batadv_tt_flags_get(common);
if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) || if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) || nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) || nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags)) nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
goto nla_put_failure; goto nla_put_failure;
if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) && if (!(flags & BATADV_TT_CLIENT_NOPURGE) &&
nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs)) nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
goto nla_put_failure; goto nla_put_failure;
@ -1285,29 +1329,23 @@ int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
} }
/** /**
* batadv_tt_local_set_pending() - mark a local TT entry as pending removal * batadv_tt_local_set_pending_event() - trigger events for TT pending removal
* @bat_priv: the bat priv with all the mesh interface information * @bat_priv: the bat priv with all the mesh interface information
* @tt_local_entry: local TT entry to mark * @tt_local_entry: local TT entry to mark
* @flags: TT change flags to announce together with the pending removal * @flags: TT change flags to announce together with the pending removal
* @message: debug message describing the reason for the change * @message: debug message describing the reason for the change
* *
* Schedule the TT change announcement and set BATADV_TT_CLIENT_PENDING on the * Schedule the TT change announcement for the entry. The entry is kept in the
* entry. The entry is kept in the local table until the next TTVN increment * local table until the next TTVN increment so that a consistency-check
* so that a consistency-check response can still be answered. * response can still be answered.
*/ */
static void static void
batadv_tt_local_set_pending(struct batadv_priv *bat_priv, batadv_tt_local_set_pending_event(struct batadv_priv *bat_priv,
struct batadv_tt_local_entry *tt_local_entry, struct batadv_tt_local_entry *tt_local_entry,
u16 flags, const char *message) u16 flags, const char *message)
{ {
batadv_tt_local_event(bat_priv, tt_local_entry, flags); batadv_tt_local_event(bat_priv, tt_local_entry, flags);
/* The local client has to be marked as "pending to be removed" but has
* to be kept in the table in order to send it in a full table
* response issued before the net ttvn increment (consistency check)
*/
tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
batadv_dbg(BATADV_DBG_TT, bat_priv, batadv_dbg(BATADV_DBG_TT, bat_priv,
"Local tt entry (%pM, vid: %d) pending to be removed: %s\n", "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
tt_local_entry->common.addr, tt_local_entry->common.addr,
@ -1331,12 +1369,14 @@ u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
struct batadv_tt_local_entry *tt_local_entry; struct batadv_tt_local_entry *tt_local_entry;
struct hlist_node *tt_removed_node; struct hlist_node *tt_removed_node;
u16 curr_flags = BATADV_NO_FLAGS; u16 curr_flags = BATADV_NO_FLAGS;
bool pending = false;
u16 flags; u16 flags;
tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid); tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
if (!tt_local_entry) if (!tt_local_entry)
goto out; goto out;
spin_lock_bh(&tt_local_entry->common.flags_lock);
curr_flags = tt_local_entry->common.flags; curr_flags = tt_local_entry->common.flags;
flags = BATADV_TT_CLIENT_DEL; flags = BATADV_TT_CLIENT_DEL;
@ -1351,10 +1391,17 @@ u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
} }
if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) { if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags, tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
message); pending = true;
}
spin_unlock_bh(&tt_local_entry->common.flags_lock);
if (pending) {
batadv_tt_local_set_pending_event(bat_priv, tt_local_entry, flags,
message);
goto out; goto out;
} }
/* if this client has been added right now, it is possible to /* if this client has been added right now, it is possible to
* immediately purge it * immediately purge it
*/ */
@ -1394,21 +1441,37 @@ static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
hlist_for_each_entry_safe(tt_common_entry, node_tmp, head, hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
hash_entry) { hash_entry) {
bool cont = false;
tt_local_entry = container_of(tt_common_entry, tt_local_entry = container_of(tt_common_entry,
struct batadv_tt_local_entry, struct batadv_tt_local_entry,
common); common);
if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
scoped_guard(spinlock_bh, &tt_local_entry->common.flags_lock) {
if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE) {
cont = true;
break;
}
/* entry already marked for deletion */
if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) {
cont = true;
break;
}
if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout)) {
cont = true;
break;
}
tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
}
if (cont)
continue; continue;
/* entry already marked for deletion */ batadv_tt_local_set_pending_event(bat_priv, tt_local_entry,
if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) BATADV_TT_CLIENT_DEL, "timed out");
continue;
if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
continue;
batadv_tt_local_set_pending(bat_priv, tt_local_entry,
BATADV_TT_CLIENT_DEL, "timed out");
} }
} }
@ -1614,8 +1677,10 @@ batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
flags |= orig_entry->flags; flags |= orig_entry->flags;
rcu_read_unlock(); rcu_read_unlock();
flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK); scoped_guard(spinlock_bh, &tt_global->common.flags_lock) {
tt_global->common.flags = flags; flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
tt_global->common.flags = flags;
}
} }
/** /**
@ -1697,8 +1762,10 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
struct batadv_tt_local_entry *tt_local_entry; struct batadv_tt_local_entry *tt_local_entry;
struct batadv_tt_common_entry *common; struct batadv_tt_common_entry *common;
bool ret = false; bool ret = false;
u16 global_flags;
u16 local_flags; u16 local_flags;
int hash_added; int hash_added;
bool delete;
/* ignore global entries from backbone nodes */ /* ignore global entries from backbone nodes */
if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid)) if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
@ -1711,9 +1778,11 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
* for a roaming advertisement instead of manually messing up the global * for a roaming advertisement instead of manually messing up the global
* table * table
*/ */
if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry && if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry) {
!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) local_flags = batadv_tt_flags_get(&tt_local_entry->common);
goto out; if (!(local_flags & BATADV_TT_CLIENT_NEW))
goto out;
}
if (!tt_global_entry) { if (!tt_global_entry) {
tt_global_entry = kmem_cache_zalloc(batadv_tg_cache, tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
@ -1724,9 +1793,13 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
common = &tt_global_entry->common; common = &tt_global_entry->common;
ether_addr_copy(common->addr, tt_addr); ether_addr_copy(common->addr, tt_addr);
common->vid = vid; common->vid = vid;
spin_lock_init(&common->flags_lock);
if (!is_multicast_ether_addr(common->addr)) if (!is_multicast_ether_addr(common->addr)) {
spin_lock_bh(&common->flags_lock);
common->flags = flags & (~BATADV_TT_SYNC_MASK); common->flags = flags & (~BATADV_TT_SYNC_MASK);
spin_unlock_bh(&common->flags_lock);
}
tt_global_entry->roam_at = 0; tt_global_entry->roam_at = 0;
/* node must store current time in case of roaming. This is /* node must store current time in case of roaming. This is
@ -1766,8 +1839,10 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
* originator list and add the new one orig_entry * originator list and add the new one orig_entry
*/ */
if (flags & BATADV_TT_CLIENT_TEMP) { if (flags & BATADV_TT_CLIENT_TEMP) {
if (!(common->flags & BATADV_TT_CLIENT_TEMP)) global_flags = batadv_tt_flags_get(common);
if (!(global_flags & BATADV_TT_CLIENT_TEMP))
goto out; goto out;
if (batadv_tt_global_entry_has_orig(tt_global_entry, if (batadv_tt_global_entry_has_orig(tt_global_entry,
orig_node, NULL)) orig_node, NULL))
goto out_remove; goto out_remove;
@ -1775,6 +1850,9 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
goto add_orig_entry; goto add_orig_entry;
} }
delete = false;
spin_lock_bh(&common->flags_lock);
/* if the client was temporary added before receiving the first /* if the client was temporary added before receiving the first
* OGM announcing it, we have to clear the TEMP flag. Also, * OGM announcing it, we have to clear the TEMP flag. Also,
* remove the previous temporary orig node and re-add it * remove the previous temporary orig node and re-add it
@ -1782,7 +1860,7 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
* is a non-temporary entry is preferred. * is a non-temporary entry is preferred.
*/ */
if (common->flags & BATADV_TT_CLIENT_TEMP) { if (common->flags & BATADV_TT_CLIENT_TEMP) {
batadv_tt_global_del_orig_list(tt_global_entry); delete = true;
common->flags &= ~BATADV_TT_CLIENT_TEMP; common->flags &= ~BATADV_TT_CLIENT_TEMP;
} }
@ -1801,10 +1879,14 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
* new one. * new one.
*/ */
if (common->flags & BATADV_TT_CLIENT_ROAM) { if (common->flags & BATADV_TT_CLIENT_ROAM) {
batadv_tt_global_del_orig_list(tt_global_entry); delete = true;
common->flags &= ~BATADV_TT_CLIENT_ROAM;
tt_global_entry->roam_at = 0; tt_global_entry->roam_at = 0;
common->flags &= ~BATADV_TT_CLIENT_ROAM;
} }
spin_unlock_bh(&common->flags_lock);
if (delete)
batadv_tt_global_del_orig_list(tt_global_entry);
} }
add_orig_entry: add_orig_entry:
/* add the new orig_entry (if needed) or update it */ /* add the new orig_entry (if needed) or update it */
@ -1828,6 +1910,8 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid, local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
"global tt received", "global tt received",
flags & BATADV_TT_CLIENT_ROAM); flags & BATADV_TT_CLIENT_ROAM);
spin_lock_bh(&tt_global_entry->common.flags_lock);
tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI; tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
if (!(flags & BATADV_TT_CLIENT_ROAM)) if (!(flags & BATADV_TT_CLIENT_ROAM))
@ -1835,6 +1919,7 @@ static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
* roaming state anymore. * roaming state anymore.
*/ */
tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM; tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
spin_unlock_bh(&tt_global_entry->common.flags_lock);
out: out:
batadv_tt_global_entry_put(tt_global_entry); batadv_tt_global_entry_put(tt_global_entry);
@ -1904,9 +1989,9 @@ batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
struct batadv_tt_orig_list_entry *orig, struct batadv_tt_orig_list_entry *orig,
bool best) bool best)
{ {
u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
struct batadv_orig_node_vlan *vlan; struct batadv_orig_node_vlan *vlan;
u8 last_ttvn; u8 last_ttvn;
u16 flags;
void *hdr; void *hdr;
u32 crc; u32 crc;
@ -1925,6 +2010,7 @@ batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
if (!hdr) if (!hdr)
return -ENOBUFS; return -ENOBUFS;
flags = (batadv_tt_flags_get(common) & (~BATADV_TT_SYNC_MASK)) | orig->flags;
last_ttvn = READ_ONCE(orig->orig_node->last_ttvn); last_ttvn = READ_ONCE(orig->orig_node->last_ttvn);
if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) || if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
@ -2212,9 +2298,12 @@ batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
rcu_read_unlock(); rcu_read_unlock();
if (last_entry) { if (last_entry) {
/* its the last one, mark for roaming. */ scoped_guard(spinlock_bh, &tt_global_entry->common.flags_lock) {
tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM; /* its the last one, mark for roaming. */
tt_global_entry->roam_at = jiffies; tt_global_entry->roam_at = jiffies;
tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
}
} else { } else {
/* there is another entry, we can simply delete this /* there is another entry, we can simply delete this
* one and can still use the other one. * one and can still use the other one.
@ -2363,16 +2452,18 @@ static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT; unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
bool purge = false; bool purge = false;
if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) && scoped_guard(spinlock_bh, &tt_global->common.flags_lock) {
batadv_has_timed_out(tt_global->roam_at, roam_timeout)) { if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
purge = true; batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
*msg = "Roaming timeout\n"; purge = true;
} *msg = "Roaming timeout\n";
}
if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) && if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) { batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
purge = true; purge = true;
*msg = "Temporary client timeout\n"; *msg = "Temporary client timeout\n";
}
} }
return purge; return purge;
@ -2481,13 +2572,19 @@ static bool
_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry, _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
struct batadv_tt_global_entry *tt_global_entry) struct batadv_tt_global_entry *tt_global_entry)
{ {
if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI && u16 global_flags;
tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI) u16 local_flags;
global_flags = batadv_tt_flags_get(&tt_global_entry->common);
local_flags = batadv_tt_flags_get(&tt_local_entry->common);
if (local_flags & BATADV_TT_CLIENT_WIFI &&
global_flags & BATADV_TT_CLIENT_WIFI)
return true; return true;
/* check if the two clients are marked as isolated */ /* check if the two clients are marked as isolated */
if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA && if (local_flags & BATADV_TT_CLIENT_ISOLA &&
tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA) global_flags & BATADV_TT_CLIENT_ISOLA)
return true; return true;
return false; return false;
@ -2516,11 +2613,15 @@ struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
struct batadv_tt_local_entry *tt_local_entry = NULL; struct batadv_tt_local_entry *tt_local_entry = NULL;
struct batadv_tt_orig_list_entry *best_entry; struct batadv_tt_orig_list_entry *best_entry;
struct batadv_orig_node *orig_node = NULL; struct batadv_orig_node *orig_node = NULL;
u16 flags;
if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) { if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid); tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
if (!tt_local_entry || if (!tt_local_entry)
(tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)) goto out;
flags = batadv_tt_flags_get(&tt_local_entry->common);
if (flags & BATADV_TT_CLIENT_PENDING)
goto out; goto out;
} }
@ -2595,6 +2696,8 @@ static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
rcu_read_lock(); rcu_read_lock();
hlist_for_each_entry_rcu(tt_common, head, hash_entry) { hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
u16 tt_flags;
tt_global = container_of(tt_common, tt_global = container_of(tt_common,
struct batadv_tt_global_entry, struct batadv_tt_global_entry,
common); common);
@ -2604,18 +2707,21 @@ static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
if (tt_common->vid != vid) if (tt_common->vid != vid)
continue; continue;
tt_flags = batadv_tt_flags_get(tt_common);
/* Roaming clients are in the global table for /* Roaming clients are in the global table for
* consistency only. They don't have to be * consistency only. They don't have to be
* taken into account while computing the * taken into account while computing the
* global crc * global crc
*/ */
if (tt_common->flags & BATADV_TT_CLIENT_ROAM) if (tt_flags & BATADV_TT_CLIENT_ROAM)
continue; continue;
/* Temporary clients have not been announced yet, so /* Temporary clients have not been announced yet, so
* they have to be skipped while computing the global * they have to be skipped while computing the global
* crc * crc
*/ */
if (tt_common->flags & BATADV_TT_CLIENT_TEMP) if (tt_flags & BATADV_TT_CLIENT_TEMP)
continue; continue;
/* find out if this global entry is announced by this /* find out if this global entry is announced by this
@ -2675,18 +2781,24 @@ static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
rcu_read_lock(); rcu_read_lock();
hlist_for_each_entry_rcu(tt_common, head, hash_entry) { hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
u16 tt_flags;
/* compute the CRC only for entries belonging to the /* compute the CRC only for entries belonging to the
* VLAN identified by vid * VLAN identified by vid
*/ */
if (tt_common->vid != vid) if (tt_common->vid != vid)
continue; continue;
tt_flags = batadv_tt_flags_get(tt_common);
/* not yet committed clients have not to be taken into /* not yet committed clients have not to be taken into
* account while computing the CRC * account while computing the CRC
*/ */
if (tt_common->flags & BATADV_TT_CLIENT_NEW) if (tt_flags & BATADV_TT_CLIENT_NEW)
continue; continue;
flags = tt_flags & BATADV_TT_SYNC_MASK;
/* use network order to read the VID: this ensures that /* use network order to read the VID: this ensures that
* every node reads the bytes in the same order. * every node reads the bytes in the same order.
*/ */
@ -2696,7 +2808,6 @@ static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
/* compute the CRC on flags that have to be kept in sync /* compute the CRC on flags that have to be kept in sync
* among nodes * among nodes
*/ */
flags = tt_common->flags & BATADV_TT_SYNC_MASK;
crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags)); crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN); crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
@ -2854,17 +2965,19 @@ batadv_tt_req_node_new(struct batadv_priv *bat_priv,
* *
* Return: true if the entry is a valid, false otherwise. * Return: true if the entry is a valid, false otherwise.
*/ */
static bool batadv_tt_local_valid(const void *entry_ptr, static bool batadv_tt_local_valid(void *entry_ptr,
const void *data_ptr, const void *data_ptr,
u8 *flags) u8 *flags)
{ {
const struct batadv_tt_common_entry *tt_common_entry = entry_ptr; struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
u16 tt_flags;
if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW) tt_flags = batadv_tt_flags_get(tt_common_entry);
if (tt_flags & BATADV_TT_CLIENT_NEW)
return false; return false;
if (flags) if (flags)
*flags = tt_common_entry->flags; *flags = tt_flags;
return true; return true;
} }
@ -2881,16 +2994,18 @@ static bool batadv_tt_local_valid(const void *entry_ptr,
* *
* Return: true if the entry is a valid, false otherwise. * Return: true if the entry is a valid, false otherwise.
*/ */
static bool batadv_tt_global_valid(const void *entry_ptr, static bool batadv_tt_global_valid(void *entry_ptr,
const void *data_ptr, const void *data_ptr,
u8 *flags) u8 *flags)
{ {
const struct batadv_tt_common_entry *tt_common_entry = entry_ptr; struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
const struct batadv_tt_global_entry *tt_global_entry;
const struct batadv_orig_node *orig_node = data_ptr; const struct batadv_orig_node *orig_node = data_ptr;
struct batadv_tt_global_entry *tt_global_entry;
u16 tt_flags;
if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM || tt_flags = batadv_tt_flags_get(tt_common_entry);
tt_common_entry->flags & BATADV_TT_CLIENT_TEMP) if (tt_flags & BATADV_TT_CLIENT_ROAM ||
tt_flags & BATADV_TT_CLIENT_TEMP)
return false; return false;
tt_global_entry = container_of(tt_common_entry, tt_global_entry = container_of(tt_common_entry,
@ -2919,7 +3034,7 @@ static bool batadv_tt_global_valid(const void *entry_ptr,
static u16 batadv_tt_tvlv_generate(struct batadv_priv *bat_priv, static u16 batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
struct batadv_hashtable *hash, struct batadv_hashtable *hash,
void *tvlv_buff, u16 tt_len, void *tvlv_buff, u16 tt_len,
bool (*valid_cb)(const void *, bool (*valid_cb)(void *,
const void *, const void *,
u8 *flags), u8 *flags),
void *cb_data) void *cb_data)
@ -3544,17 +3659,20 @@ bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
unsigned short vid) unsigned short vid)
{ {
struct batadv_tt_local_entry *tt_local_entry; struct batadv_tt_local_entry *tt_local_entry;
u16 tt_flags;
bool ret; bool ret;
tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid); tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
if (!tt_local_entry) if (!tt_local_entry)
return false; return false;
tt_flags = batadv_tt_flags_get(&tt_local_entry->common);
/* Check if the client has been logically deleted (but is kept for /* Check if the client has been logically deleted (but is kept for
* consistency purpose) * consistency purpose)
*/ */
ret = !((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) || ret = !((tt_flags & BATADV_TT_CLIENT_PENDING) ||
(tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM)); (tt_flags & BATADV_TT_CLIENT_ROAM));
batadv_tt_local_entry_put(tt_local_entry); batadv_tt_local_entry_put(tt_local_entry);
return ret; return ret;
@ -3834,10 +3952,19 @@ static void batadv_tt_local_transition_new(struct batadv_priv *bat_priv)
rcu_read_lock(); rcu_read_lock();
hlist_for_each_entry_rcu(tt_common_entry, hlist_for_each_entry_rcu(tt_common_entry,
head, hash_entry) { head, hash_entry) {
if (!(tt_common_entry->flags & BATADV_TT_CLIENT_NEW)) bool cont = false;
continue;
tt_common_entry->flags &= ~BATADV_TT_CLIENT_NEW; scoped_guard(spinlock_bh, &tt_common_entry->flags_lock) {
if (!(tt_common_entry->flags & BATADV_TT_CLIENT_NEW)) {
cont = true;
break;
}
tt_common_entry->flags &= ~BATADV_TT_CLIENT_NEW;
}
if (cont)
continue;
batadv_tt_local_size_inc(bat_priv, batadv_tt_local_size_inc(bat_priv,
tt_common_entry->vid); tt_common_entry->vid);
@ -3874,16 +4001,26 @@ static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
spin_lock_bh(list_lock); spin_lock_bh(list_lock);
hlist_for_each_entry_safe(tt_common, node_tmp, head, hlist_for_each_entry_safe(tt_common, node_tmp, head,
hash_entry) { hash_entry) {
if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING)) bool cont = false;
scoped_guard(spinlock_bh, &tt_common->flags_lock) {
if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING)) {
cont = true;
break;
}
batadv_dbg(BATADV_DBG_TT, bat_priv,
"Deleting local tt entry (%pM, vid: %d): pending\n",
tt_common->addr,
batadv_print_vid(tt_common->vid));
batadv_tt_local_size_dec(bat_priv, tt_common->vid);
hlist_del_rcu(&tt_common->hash_entry);
}
if (cont)
continue; continue;
batadv_dbg(BATADV_DBG_TT, bat_priv,
"Deleting local tt entry (%pM, vid: %d): pending\n",
tt_common->addr,
batadv_print_vid(tt_common->vid));
batadv_tt_local_size_dec(bat_priv, tt_common->vid);
hlist_del_rcu(&tt_common->hash_entry);
tt_local = container_of(tt_common, tt_local = container_of(tt_common,
struct batadv_tt_local_entry, struct batadv_tt_local_entry,
common); common);
@ -4087,7 +4224,8 @@ bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
if (!tt_global_entry) if (!tt_global_entry)
return false; return false;
ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM; ret = batadv_tt_flags_get(&tt_global_entry->common) & BATADV_TT_CLIENT_ROAM;
batadv_tt_global_entry_put(tt_global_entry); batadv_tt_global_entry_put(tt_global_entry);
return ret; return ret;
@ -4113,7 +4251,8 @@ bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
if (!tt_local_entry) if (!tt_local_entry)
return false; return false;
ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM; ret = batadv_tt_flags_get(&tt_local_entry->common) & BATADV_TT_CLIENT_ROAM;
batadv_tt_local_entry_put(tt_local_entry); batadv_tt_local_entry_put(tt_local_entry);
return ret; return ret;
@ -4424,7 +4563,7 @@ bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
if (!tt) if (!tt)
return false; return false;
ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA; ret = batadv_tt_flags_get(&tt->common) & BATADV_TT_CLIENT_ISOLA;
batadv_tt_global_entry_put(tt); batadv_tt_global_entry_put(tt);

View File

@ -1851,12 +1851,21 @@ struct batadv_tt_common_entry {
*/ */
struct hlist_node hash_entry; struct hlist_node hash_entry;
/** @flags: various state handling flags (see batadv_tt_client_flags) */ /**
* @flags: various state handling flags (see batadv_tt_client_flags),
* protected by @flags_lock
*/
u16 flags; u16 flags;
/** @added_at: timestamp used for purging stale tt common entries */ /** @added_at: timestamp used for purging stale tt common entries */
unsigned long added_at; unsigned long added_at;
/**
* @flags_lock: protect modifications of @flags and
* @batadv_tt_global_entry.roam_at
*/
spinlock_t flags_lock;
/** @refcount: number of contexts the object is used */ /** @refcount: number of contexts the object is used */
struct kref refcount; struct kref refcount;