Linux 4.14.67
[linux/fpc-iii.git] / net / batman-adv / translation-table.c
blob0f4d4eece3e4deca20f3e6531fba13d4dbe005bf
1 /* Copyright (C) 2007-2017 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "translation-table.h"
19 #include "main.h"
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/cache.h>
26 #include <linux/compiler.h>
27 #include <linux/crc32c.h>
28 #include <linux/errno.h>
29 #include <linux/etherdevice.h>
30 #include <linux/fs.h>
31 #include <linux/if_ether.h>
32 #include <linux/init.h>
33 #include <linux/jhash.h>
34 #include <linux/jiffies.h>
35 #include <linux/kernel.h>
36 #include <linux/kref.h>
37 #include <linux/list.h>
38 #include <linux/lockdep.h>
39 #include <linux/netdevice.h>
40 #include <linux/netlink.h>
41 #include <linux/rculist.h>
42 #include <linux/rcupdate.h>
43 #include <linux/seq_file.h>
44 #include <linux/skbuff.h>
45 #include <linux/slab.h>
46 #include <linux/spinlock.h>
47 #include <linux/stddef.h>
48 #include <linux/string.h>
49 #include <linux/workqueue.h>
50 #include <net/genetlink.h>
51 #include <net/netlink.h>
52 #include <net/sock.h>
53 #include <uapi/linux/batman_adv.h>
55 #include "bridge_loop_avoidance.h"
56 #include "hard-interface.h"
57 #include "hash.h"
58 #include "log.h"
59 #include "netlink.h"
60 #include "originator.h"
61 #include "packet.h"
62 #include "soft-interface.h"
63 #include "tvlv.h"
65 static struct kmem_cache *batadv_tl_cache __read_mostly;
66 static struct kmem_cache *batadv_tg_cache __read_mostly;
67 static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
68 static struct kmem_cache *batadv_tt_change_cache __read_mostly;
69 static struct kmem_cache *batadv_tt_req_cache __read_mostly;
70 static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
72 /* hash class keys */
73 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
74 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
76 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
77 unsigned short vid,
78 struct batadv_orig_node *orig_node);
79 static void batadv_tt_purge(struct work_struct *work);
80 static void
81 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
82 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
83 struct batadv_orig_node *orig_node,
84 const unsigned char *addr,
85 unsigned short vid, const char *message,
86 bool roaming);
88 /**
89 * batadv_compare_tt - check if two TT entries are the same
90 * @node: the list element pointer of the first TT entry
91 * @data2: pointer to the tt_common_entry of the second TT entry
93 * Compare the MAC address and the VLAN ID of the two TT entries and check if
94 * they are the same TT client.
95 * Return: true if the two TT clients are the same, false otherwise
97 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
99 const void *data1 = container_of(node, struct batadv_tt_common_entry,
100 hash_entry);
101 const struct batadv_tt_common_entry *tt1 = data1;
102 const struct batadv_tt_common_entry *tt2 = data2;
104 return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
108 * batadv_choose_tt - return the index of the tt entry in the hash table
109 * @data: pointer to the tt_common_entry object to map
110 * @size: the size of the hash table
112 * Return: the hash index where the object represented by 'data' should be
113 * stored at.
115 static inline u32 batadv_choose_tt(const void *data, u32 size)
117 struct batadv_tt_common_entry *tt;
118 u32 hash = 0;
120 tt = (struct batadv_tt_common_entry *)data;
121 hash = jhash(&tt->addr, ETH_ALEN, hash);
122 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
124 return hash % size;
128 * batadv_tt_hash_find - look for a client in the given hash table
129 * @hash: the hash table to search
130 * @addr: the mac address of the client to look for
131 * @vid: VLAN identifier
133 * Return: a pointer to the tt_common struct belonging to the searched client if
134 * found, NULL otherwise.
136 static struct batadv_tt_common_entry *
137 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
138 unsigned short vid)
140 struct hlist_head *head;
141 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
142 u32 index;
144 if (!hash)
145 return NULL;
147 ether_addr_copy(to_search.addr, addr);
148 to_search.vid = vid;
150 index = batadv_choose_tt(&to_search, hash->size);
151 head = &hash->table[index];
153 rcu_read_lock();
154 hlist_for_each_entry_rcu(tt, head, hash_entry) {
155 if (!batadv_compare_eth(tt, addr))
156 continue;
158 if (tt->vid != vid)
159 continue;
161 if (!kref_get_unless_zero(&tt->refcount))
162 continue;
164 tt_tmp = tt;
165 break;
167 rcu_read_unlock();
169 return tt_tmp;
173 * batadv_tt_local_hash_find - search the local table for a given client
174 * @bat_priv: the bat priv with all the soft interface information
175 * @addr: the mac address of the client to look for
176 * @vid: VLAN identifier
178 * Return: a pointer to the corresponding tt_local_entry struct if the client is
179 * found, NULL otherwise.
181 static struct batadv_tt_local_entry *
182 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
183 unsigned short vid)
185 struct batadv_tt_common_entry *tt_common_entry;
186 struct batadv_tt_local_entry *tt_local_entry = NULL;
188 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
189 vid);
190 if (tt_common_entry)
191 tt_local_entry = container_of(tt_common_entry,
192 struct batadv_tt_local_entry,
193 common);
194 return tt_local_entry;
198 * batadv_tt_global_hash_find - search the global table for a given client
199 * @bat_priv: the bat priv with all the soft interface information
200 * @addr: the mac address of the client to look for
201 * @vid: VLAN identifier
203 * Return: a pointer to the corresponding tt_global_entry struct if the client
204 * is found, NULL otherwise.
206 static struct batadv_tt_global_entry *
207 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
208 unsigned short vid)
210 struct batadv_tt_common_entry *tt_common_entry;
211 struct batadv_tt_global_entry *tt_global_entry = NULL;
213 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
214 vid);
215 if (tt_common_entry)
216 tt_global_entry = container_of(tt_common_entry,
217 struct batadv_tt_global_entry,
218 common);
219 return tt_global_entry;
223 * batadv_tt_local_entry_free_rcu - free the tt_local_entry
224 * @rcu: rcu pointer of the tt_local_entry
226 static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
228 struct batadv_tt_local_entry *tt_local_entry;
230 tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
231 common.rcu);
233 kmem_cache_free(batadv_tl_cache, tt_local_entry);
237 * batadv_tt_local_entry_release - release tt_local_entry from lists and queue
238 * for free after rcu grace period
239 * @ref: kref pointer of the nc_node
241 static void batadv_tt_local_entry_release(struct kref *ref)
243 struct batadv_tt_local_entry *tt_local_entry;
245 tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
246 common.refcount);
248 batadv_softif_vlan_put(tt_local_entry->vlan);
250 call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
254 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
255 * possibly release it
256 * @tt_local_entry: tt_local_entry to be free'd
258 static void
259 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
261 kref_put(&tt_local_entry->common.refcount,
262 batadv_tt_local_entry_release);
266 * batadv_tt_global_entry_free_rcu - free the tt_global_entry
267 * @rcu: rcu pointer of the tt_global_entry
269 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
271 struct batadv_tt_global_entry *tt_global_entry;
273 tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
274 common.rcu);
276 kmem_cache_free(batadv_tg_cache, tt_global_entry);
280 * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
281 * for free after rcu grace period
282 * @ref: kref pointer of the nc_node
284 static void batadv_tt_global_entry_release(struct kref *ref)
286 struct batadv_tt_global_entry *tt_global_entry;
288 tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
289 common.refcount);
291 batadv_tt_global_del_orig_list(tt_global_entry);
293 call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
297 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
298 * possibly release it
299 * @tt_global_entry: tt_global_entry to be free'd
301 static void
302 batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
304 kref_put(&tt_global_entry->common.refcount,
305 batadv_tt_global_entry_release);
309 * batadv_tt_global_hash_count - count the number of orig entries
310 * @bat_priv: the bat priv with all the soft interface information
311 * @addr: the mac address of the client to count entries for
312 * @vid: VLAN identifier
314 * Return: the number of originators advertising the given address/data
315 * (excluding ourself).
317 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
318 const u8 *addr, unsigned short vid)
320 struct batadv_tt_global_entry *tt_global_entry;
321 int count;
323 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
324 if (!tt_global_entry)
325 return 0;
327 count = atomic_read(&tt_global_entry->orig_list_count);
328 batadv_tt_global_entry_put(tt_global_entry);
330 return count;
334 * batadv_tt_local_size_mod - change the size by v of the local table identified
335 * by vid
336 * @bat_priv: the bat priv with all the soft interface information
337 * @vid: the VLAN identifier of the sub-table to change
338 * @v: the amount to sum to the local table size
340 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
341 unsigned short vid, int v)
343 struct batadv_softif_vlan *vlan;
345 vlan = batadv_softif_vlan_get(bat_priv, vid);
346 if (!vlan)
347 return;
349 atomic_add(v, &vlan->tt.num_entries);
351 batadv_softif_vlan_put(vlan);
355 * batadv_tt_local_size_inc - increase by one the local table size for the given
356 * vid
357 * @bat_priv: the bat priv with all the soft interface information
358 * @vid: the VLAN identifier
360 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
361 unsigned short vid)
363 batadv_tt_local_size_mod(bat_priv, vid, 1);
367 * batadv_tt_local_size_dec - decrease by one the local table size for the given
368 * vid
369 * @bat_priv: the bat priv with all the soft interface information
370 * @vid: the VLAN identifier
372 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
373 unsigned short vid)
375 batadv_tt_local_size_mod(bat_priv, vid, -1);
379 * batadv_tt_global_size_mod - change the size by v of the global table
380 * for orig_node identified by vid
381 * @orig_node: the originator for which the table has to be modified
382 * @vid: the VLAN identifier
383 * @v: the amount to sum to the global table size
385 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
386 unsigned short vid, int v)
388 struct batadv_orig_node_vlan *vlan;
390 vlan = batadv_orig_node_vlan_new(orig_node, vid);
391 if (!vlan)
392 return;
394 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
395 spin_lock_bh(&orig_node->vlan_list_lock);
396 if (!hlist_unhashed(&vlan->list)) {
397 hlist_del_init_rcu(&vlan->list);
398 batadv_orig_node_vlan_put(vlan);
400 spin_unlock_bh(&orig_node->vlan_list_lock);
403 batadv_orig_node_vlan_put(vlan);
407 * batadv_tt_global_size_inc - increase by one the global table size for the
408 * given vid
409 * @orig_node: the originator which global table size has to be decreased
410 * @vid: the vlan identifier
412 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
413 unsigned short vid)
415 batadv_tt_global_size_mod(orig_node, vid, 1);
419 * batadv_tt_global_size_dec - decrease by one the global table size for the
420 * given vid
421 * @orig_node: the originator which global table size has to be decreased
422 * @vid: the vlan identifier
424 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
425 unsigned short vid)
427 batadv_tt_global_size_mod(orig_node, vid, -1);
431 * batadv_tt_orig_list_entry_free_rcu - free the orig_entry
432 * @rcu: rcu pointer of the orig_entry
434 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
436 struct batadv_tt_orig_list_entry *orig_entry;
438 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
440 kmem_cache_free(batadv_tt_orig_cache, orig_entry);
444 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
445 * queue for free after rcu grace period
446 * @ref: kref pointer of the tt orig entry
448 static void batadv_tt_orig_list_entry_release(struct kref *ref)
450 struct batadv_tt_orig_list_entry *orig_entry;
452 orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
453 refcount);
455 batadv_orig_node_put(orig_entry->orig_node);
456 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
460 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
461 * possibly release it
462 * @orig_entry: tt orig entry to be free'd
464 static void
465 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
467 kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
471 * batadv_tt_local_event - store a local TT event (ADD/DEL)
472 * @bat_priv: the bat priv with all the soft interface information
473 * @tt_local_entry: the TT entry involved in the event
474 * @event_flags: flags to store in the event structure
476 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
477 struct batadv_tt_local_entry *tt_local_entry,
478 u8 event_flags)
480 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
481 struct batadv_tt_common_entry *common = &tt_local_entry->common;
482 u8 flags = common->flags | event_flags;
483 bool event_removed = false;
484 bool del_op_requested, del_op_entry;
486 tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
487 if (!tt_change_node)
488 return;
490 tt_change_node->change.flags = flags;
491 memset(tt_change_node->change.reserved, 0,
492 sizeof(tt_change_node->change.reserved));
493 ether_addr_copy(tt_change_node->change.addr, common->addr);
494 tt_change_node->change.vid = htons(common->vid);
496 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
498 /* check for ADD+DEL or DEL+ADD events */
499 spin_lock_bh(&bat_priv->tt.changes_list_lock);
500 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
501 list) {
502 if (!batadv_compare_eth(entry->change.addr, common->addr))
503 continue;
505 /* DEL+ADD in the same orig interval have no effect and can be
506 * removed to avoid silly behaviour on the receiver side. The
507 * other way around (ADD+DEL) can happen in case of roaming of
508 * a client still in the NEW state. Roaming of NEW clients is
509 * now possible due to automatically recognition of "temporary"
510 * clients
512 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
513 if (!del_op_requested && del_op_entry)
514 goto del;
515 if (del_op_requested && !del_op_entry)
516 goto del;
518 /* this is a second add in the same originator interval. It
519 * means that flags have been changed: update them!
521 if (!del_op_requested && !del_op_entry)
522 entry->change.flags = flags;
524 continue;
525 del:
526 list_del(&entry->list);
527 kmem_cache_free(batadv_tt_change_cache, entry);
528 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
529 event_removed = true;
530 goto unlock;
533 /* track the change in the OGMinterval list */
534 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
536 unlock:
537 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
539 if (event_removed)
540 atomic_dec(&bat_priv->tt.local_changes);
541 else
542 atomic_inc(&bat_priv->tt.local_changes);
546 * batadv_tt_len - compute length in bytes of given number of tt changes
547 * @changes_num: number of tt changes
549 * Return: computed length in bytes.
551 static int batadv_tt_len(int changes_num)
553 return changes_num * sizeof(struct batadv_tvlv_tt_change);
557 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
558 * @tt_len: available space
560 * Return: the number of entries.
562 static u16 batadv_tt_entries(u16 tt_len)
564 return tt_len / batadv_tt_len(1);
568 * batadv_tt_local_table_transmit_size - calculates the local translation table
569 * size when transmitted over the air
570 * @bat_priv: the bat priv with all the soft interface information
572 * Return: local translation table size in bytes.
574 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
576 u16 num_vlan = 0;
577 u16 tt_local_entries = 0;
578 struct batadv_softif_vlan *vlan;
579 int hdr_size;
581 rcu_read_lock();
582 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
583 num_vlan++;
584 tt_local_entries += atomic_read(&vlan->tt.num_entries);
586 rcu_read_unlock();
588 /* header size of tvlv encapsulated tt response payload */
589 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
590 hdr_size += sizeof(struct batadv_tvlv_hdr);
591 hdr_size += sizeof(struct batadv_tvlv_tt_data);
592 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
594 return hdr_size + batadv_tt_len(tt_local_entries);
597 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
599 if (bat_priv->tt.local_hash)
600 return 0;
602 bat_priv->tt.local_hash = batadv_hash_new(1024);
604 if (!bat_priv->tt.local_hash)
605 return -ENOMEM;
607 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
608 &batadv_tt_local_hash_lock_class_key);
610 return 0;
613 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
614 struct batadv_tt_global_entry *tt_global,
615 const char *message)
617 batadv_dbg(BATADV_DBG_TT, bat_priv,
618 "Deleting global tt entry %pM (vid: %d): %s\n",
619 tt_global->common.addr,
620 batadv_print_vid(tt_global->common.vid), message);
622 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
623 batadv_choose_tt, &tt_global->common);
624 batadv_tt_global_entry_put(tt_global);
628 * batadv_tt_local_add - add a new client to the local table or update an
629 * existing client
630 * @soft_iface: netdev struct of the mesh interface
631 * @addr: the mac address of the client to add
632 * @vid: VLAN identifier
633 * @ifindex: index of the interface where the client is connected to (useful to
634 * identify wireless clients)
635 * @mark: the value contained in the skb->mark field of the received packet (if
636 * any)
638 * Return: true if the client was successfully added, false otherwise.
640 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
641 unsigned short vid, int ifindex, u32 mark)
643 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
644 struct batadv_tt_local_entry *tt_local;
645 struct batadv_tt_global_entry *tt_global = NULL;
646 struct net *net = dev_net(soft_iface);
647 struct batadv_softif_vlan *vlan;
648 struct net_device *in_dev = NULL;
649 struct batadv_hard_iface *in_hardif = NULL;
650 struct hlist_head *head;
651 struct batadv_tt_orig_list_entry *orig_entry;
652 int hash_added, table_size, packet_size_max;
653 bool ret = false;
654 bool roamed_back = false;
655 u8 remote_flags;
656 u32 match_mark;
658 if (ifindex != BATADV_NULL_IFINDEX)
659 in_dev = dev_get_by_index(net, ifindex);
661 if (in_dev)
662 in_hardif = batadv_hardif_get_by_netdev(in_dev);
664 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
666 if (!is_multicast_ether_addr(addr))
667 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
669 if (tt_local) {
670 tt_local->last_seen = jiffies;
671 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
672 batadv_dbg(BATADV_DBG_TT, bat_priv,
673 "Re-adding pending client %pM (vid: %d)\n",
674 addr, batadv_print_vid(vid));
675 /* whatever the reason why the PENDING flag was set,
676 * this is a client which was enqueued to be removed in
677 * this orig_interval. Since it popped up again, the
678 * flag can be reset like it was never enqueued
680 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
681 goto add_event;
684 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
685 batadv_dbg(BATADV_DBG_TT, bat_priv,
686 "Roaming client %pM (vid: %d) came back to its original location\n",
687 addr, batadv_print_vid(vid));
688 /* the ROAM flag is set because this client roamed away
689 * and the node got a roaming_advertisement message. Now
690 * that the client popped up again at its original
691 * location such flag can be unset
693 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
694 roamed_back = true;
696 goto check_roaming;
699 /* Ignore the client if we cannot send it in a full table response. */
700 table_size = batadv_tt_local_table_transmit_size(bat_priv);
701 table_size += batadv_tt_len(1);
702 packet_size_max = atomic_read(&bat_priv->packet_size_max);
703 if (table_size > packet_size_max) {
704 net_ratelimited_function(batadv_info, soft_iface,
705 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
706 table_size, packet_size_max, addr);
707 goto out;
710 tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
711 if (!tt_local)
712 goto out;
714 /* increase the refcounter of the related vlan */
715 vlan = batadv_softif_vlan_get(bat_priv, vid);
716 if (!vlan) {
717 net_ratelimited_function(batadv_info, soft_iface,
718 "adding TT local entry %pM to non-existent VLAN %d\n",
719 addr, batadv_print_vid(vid));
720 kmem_cache_free(batadv_tl_cache, tt_local);
721 tt_local = NULL;
722 goto out;
725 batadv_dbg(BATADV_DBG_TT, bat_priv,
726 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
727 addr, batadv_print_vid(vid),
728 (u8)atomic_read(&bat_priv->tt.vn));
730 ether_addr_copy(tt_local->common.addr, addr);
731 /* The local entry has to be marked as NEW to avoid to send it in
732 * a full table response going out before the next ttvn increment
733 * (consistency check)
735 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
736 tt_local->common.vid = vid;
737 if (batadv_is_wifi_hardif(in_hardif))
738 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
739 kref_init(&tt_local->common.refcount);
740 tt_local->last_seen = jiffies;
741 tt_local->common.added_at = tt_local->last_seen;
742 tt_local->vlan = vlan;
744 /* the batman interface mac and multicast addresses should never be
745 * purged
747 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
748 is_multicast_ether_addr(addr))
749 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
751 kref_get(&tt_local->common.refcount);
752 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
753 batadv_choose_tt, &tt_local->common,
754 &tt_local->common.hash_entry);
756 if (unlikely(hash_added != 0)) {
757 /* remove the reference for the hash */
758 batadv_tt_local_entry_put(tt_local);
759 goto out;
762 add_event:
763 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
765 check_roaming:
766 /* Check whether it is a roaming, but don't do anything if the roaming
767 * process has already been handled
769 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
770 /* These node are probably going to update their tt table */
771 head = &tt_global->orig_list;
772 rcu_read_lock();
773 hlist_for_each_entry_rcu(orig_entry, head, list) {
774 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
775 tt_global->common.vid,
776 orig_entry->orig_node);
778 rcu_read_unlock();
779 if (roamed_back) {
780 batadv_tt_global_free(bat_priv, tt_global,
781 "Roaming canceled");
782 tt_global = NULL;
783 } else {
784 /* The global entry has to be marked as ROAMING and
785 * has to be kept for consistency purpose
787 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
788 tt_global->roam_at = jiffies;
792 /* store the current remote flags before altering them. This helps
793 * understanding is flags are changing or not
795 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
797 if (batadv_is_wifi_hardif(in_hardif))
798 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
799 else
800 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
802 /* check the mark in the skb: if it's equal to the configured
803 * isolation_mark, it means the packet is coming from an isolated
804 * non-mesh client
806 match_mark = (mark & bat_priv->isolation_mark_mask);
807 if (bat_priv->isolation_mark_mask &&
808 match_mark == bat_priv->isolation_mark)
809 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
810 else
811 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
813 /* if any "dynamic" flag has been modified, resend an ADD event for this
814 * entry so that all the nodes can get the new flags
816 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
817 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
819 ret = true;
820 out:
821 if (in_hardif)
822 batadv_hardif_put(in_hardif);
823 if (in_dev)
824 dev_put(in_dev);
825 if (tt_local)
826 batadv_tt_local_entry_put(tt_local);
827 if (tt_global)
828 batadv_tt_global_entry_put(tt_global);
829 return ret;
833 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
834 * within a TT Response directed to another node
835 * @orig_node: originator for which the TT data has to be prepared
836 * @tt_data: uninitialised pointer to the address of the TVLV buffer
837 * @tt_change: uninitialised pointer to the address of the area where the TT
838 * changed can be stored
839 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
840 * function reserves the amount of space needed to send the entire global TT
841 * table. In case of success the value is updated with the real amount of
842 * reserved bytes
843 * Allocate the needed amount of memory for the entire TT TVLV and write its
844 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
845 * objects, one per active VLAN served by the originator node.
847 * Return: the size of the allocated buffer or 0 in case of failure.
849 static u16
850 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
851 struct batadv_tvlv_tt_data **tt_data,
852 struct batadv_tvlv_tt_change **tt_change,
853 s32 *tt_len)
855 u16 num_vlan = 0;
856 u16 num_entries = 0;
857 u16 change_offset;
858 u16 tvlv_len;
859 struct batadv_tvlv_tt_vlan_data *tt_vlan;
860 struct batadv_orig_node_vlan *vlan;
861 u8 *tt_change_ptr;
863 rcu_read_lock();
864 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
865 num_vlan++;
866 num_entries += atomic_read(&vlan->tt.num_entries);
869 change_offset = sizeof(**tt_data);
870 change_offset += num_vlan * sizeof(*tt_vlan);
872 /* if tt_len is negative, allocate the space needed by the full table */
873 if (*tt_len < 0)
874 *tt_len = batadv_tt_len(num_entries);
876 tvlv_len = *tt_len;
877 tvlv_len += change_offset;
879 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
880 if (!*tt_data) {
881 *tt_len = 0;
882 goto out;
885 (*tt_data)->flags = BATADV_NO_FLAGS;
886 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
887 (*tt_data)->num_vlan = htons(num_vlan);
889 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
890 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
891 tt_vlan->vid = htons(vlan->vid);
892 tt_vlan->crc = htonl(vlan->tt.crc);
894 tt_vlan++;
897 tt_change_ptr = (u8 *)*tt_data + change_offset;
898 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
900 out:
901 rcu_read_unlock();
902 return tvlv_len;
906 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
907 * node
908 * @bat_priv: the bat priv with all the soft interface information
909 * @tt_data: uninitialised pointer to the address of the TVLV buffer
910 * @tt_change: uninitialised pointer to the address of the area where the TT
911 * changes can be stored
912 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
913 * function reserves the amount of space needed to send the entire local TT
914 * table. In case of success the value is updated with the real amount of
915 * reserved bytes
917 * Allocate the needed amount of memory for the entire TT TVLV and write its
918 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
919 * objects, one per active VLAN.
921 * Return: the size of the allocated buffer or 0 in case of failure.
923 static u16
924 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
925 struct batadv_tvlv_tt_data **tt_data,
926 struct batadv_tvlv_tt_change **tt_change,
927 s32 *tt_len)
929 struct batadv_tvlv_tt_vlan_data *tt_vlan;
930 struct batadv_softif_vlan *vlan;
931 u16 num_vlan = 0;
932 u16 num_entries = 0;
933 u16 tvlv_len;
934 u8 *tt_change_ptr;
935 int change_offset;
937 rcu_read_lock();
938 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
939 num_vlan++;
940 num_entries += atomic_read(&vlan->tt.num_entries);
943 change_offset = sizeof(**tt_data);
944 change_offset += num_vlan * sizeof(*tt_vlan);
946 /* if tt_len is negative, allocate the space needed by the full table */
947 if (*tt_len < 0)
948 *tt_len = batadv_tt_len(num_entries);
950 tvlv_len = *tt_len;
951 tvlv_len += change_offset;
953 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
954 if (!*tt_data) {
955 tvlv_len = 0;
956 goto out;
959 (*tt_data)->flags = BATADV_NO_FLAGS;
960 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
961 (*tt_data)->num_vlan = htons(num_vlan);
963 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
964 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
965 tt_vlan->vid = htons(vlan->vid);
966 tt_vlan->crc = htonl(vlan->tt.crc);
968 tt_vlan++;
971 tt_change_ptr = (u8 *)*tt_data + change_offset;
972 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
974 out:
975 rcu_read_unlock();
976 return tvlv_len;
980 * batadv_tt_tvlv_container_update - update the translation table tvlv container
981 * after local tt changes have been committed
982 * @bat_priv: the bat priv with all the soft interface information
984 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
986 struct batadv_tt_change_node *entry, *safe;
987 struct batadv_tvlv_tt_data *tt_data;
988 struct batadv_tvlv_tt_change *tt_change;
989 int tt_diff_len, tt_change_len = 0;
990 int tt_diff_entries_num = 0;
991 int tt_diff_entries_count = 0;
992 u16 tvlv_len;
994 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
995 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
997 /* if we have too many changes for one packet don't send any
998 * and wait for the tt table request which will be fragmented
1000 if (tt_diff_len > bat_priv->soft_iface->mtu)
1001 tt_diff_len = 0;
1003 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1004 &tt_change, &tt_diff_len);
1005 if (!tvlv_len)
1006 return;
1008 tt_data->flags = BATADV_TT_OGM_DIFF;
1010 if (tt_diff_len == 0)
1011 goto container_register;
1013 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1014 atomic_set(&bat_priv->tt.local_changes, 0);
1016 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1017 list) {
1018 if (tt_diff_entries_count < tt_diff_entries_num) {
1019 memcpy(tt_change + tt_diff_entries_count,
1020 &entry->change,
1021 sizeof(struct batadv_tvlv_tt_change));
1022 tt_diff_entries_count++;
1024 list_del(&entry->list);
1025 kmem_cache_free(batadv_tt_change_cache, entry);
1027 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1029 /* Keep the buffer for possible tt_request */
1030 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1031 kfree(bat_priv->tt.last_changeset);
1032 bat_priv->tt.last_changeset_len = 0;
1033 bat_priv->tt.last_changeset = NULL;
1034 tt_change_len = batadv_tt_len(tt_diff_entries_count);
1035 /* check whether this new OGM has no changes due to size problems */
1036 if (tt_diff_entries_count > 0) {
1037 /* if kmalloc() fails we will reply with the full table
1038 * instead of providing the diff
1040 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1041 if (bat_priv->tt.last_changeset) {
1042 memcpy(bat_priv->tt.last_changeset,
1043 tt_change, tt_change_len);
1044 bat_priv->tt.last_changeset_len = tt_diff_len;
1047 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1049 container_register:
1050 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1051 tvlv_len);
1052 kfree(tt_data);
1055 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1056 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
1058 struct net_device *net_dev = (struct net_device *)seq->private;
1059 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1060 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1061 struct batadv_tt_common_entry *tt_common_entry;
1062 struct batadv_tt_local_entry *tt_local;
1063 struct batadv_hard_iface *primary_if;
1064 struct hlist_head *head;
1065 u32 i;
1066 int last_seen_secs;
1067 int last_seen_msecs;
1068 unsigned long last_seen_jiffies;
1069 bool no_purge;
1070 u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
1072 primary_if = batadv_seq_print_text_primary_if_get(seq);
1073 if (!primary_if)
1074 goto out;
1076 seq_printf(seq,
1077 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1078 net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
1079 seq_puts(seq,
1080 " Client VID Flags Last seen (CRC )\n");
1082 for (i = 0; i < hash->size; i++) {
1083 head = &hash->table[i];
1085 rcu_read_lock();
1086 hlist_for_each_entry_rcu(tt_common_entry,
1087 head, hash_entry) {
1088 tt_local = container_of(tt_common_entry,
1089 struct batadv_tt_local_entry,
1090 common);
1091 last_seen_jiffies = jiffies - tt_local->last_seen;
1092 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1093 last_seen_secs = last_seen_msecs / 1000;
1094 last_seen_msecs = last_seen_msecs % 1000;
1096 no_purge = tt_common_entry->flags & np_flag;
1097 seq_printf(seq,
1098 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1099 tt_common_entry->addr,
1100 batadv_print_vid(tt_common_entry->vid),
1101 ((tt_common_entry->flags &
1102 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1103 no_purge ? 'P' : '.',
1104 ((tt_common_entry->flags &
1105 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1106 ((tt_common_entry->flags &
1107 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1108 ((tt_common_entry->flags &
1109 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1110 ((tt_common_entry->flags &
1111 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1112 no_purge ? 0 : last_seen_secs,
1113 no_purge ? 0 : last_seen_msecs,
1114 tt_local->vlan->tt.crc);
1116 rcu_read_unlock();
1118 out:
1119 if (primary_if)
1120 batadv_hardif_put(primary_if);
1121 return 0;
1123 #endif
1126 * batadv_tt_local_dump_entry - Dump one TT local entry into a message
1127 * @msg :Netlink message to dump into
1128 * @portid: Port making netlink request
1129 * @seq: Sequence number of netlink message
1130 * @bat_priv: The bat priv with all the soft interface information
1131 * @common: tt local & tt global common data
1133 * Return: Error code, or 0 on success
1135 static int
1136 batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
1137 struct batadv_priv *bat_priv,
1138 struct batadv_tt_common_entry *common)
1140 void *hdr;
1141 struct batadv_softif_vlan *vlan;
1142 struct batadv_tt_local_entry *local;
1143 unsigned int last_seen_msecs;
1144 u32 crc;
1146 local = container_of(common, struct batadv_tt_local_entry, common);
1147 last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1149 vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1150 if (!vlan)
1151 return 0;
1153 crc = vlan->tt.crc;
1155 batadv_softif_vlan_put(vlan);
1157 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
1158 NLM_F_MULTI,
1159 BATADV_CMD_GET_TRANSTABLE_LOCAL);
1160 if (!hdr)
1161 return -ENOBUFS;
1163 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1164 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1165 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1166 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1167 goto nla_put_failure;
1169 if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1170 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1171 goto nla_put_failure;
1173 genlmsg_end(msg, hdr);
1174 return 0;
1176 nla_put_failure:
1177 genlmsg_cancel(msg, hdr);
1178 return -EMSGSIZE;
1182 * batadv_tt_local_dump_bucket - Dump one TT local bucket into a message
1183 * @msg: Netlink message to dump into
1184 * @portid: Port making netlink request
1185 * @seq: Sequence number of netlink message
1186 * @bat_priv: The bat priv with all the soft interface information
1187 * @head: Pointer to the list containing the local tt entries
1188 * @idx_s: Number of entries to skip
1190 * Return: Error code, or 0 on success
1192 static int
1193 batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
1194 struct batadv_priv *bat_priv,
1195 struct hlist_head *head, int *idx_s)
1197 struct batadv_tt_common_entry *common;
1198 int idx = 0;
1200 rcu_read_lock();
1201 hlist_for_each_entry_rcu(common, head, hash_entry) {
1202 if (idx++ < *idx_s)
1203 continue;
1205 if (batadv_tt_local_dump_entry(msg, portid, seq, bat_priv,
1206 common)) {
1207 rcu_read_unlock();
1208 *idx_s = idx - 1;
1209 return -EMSGSIZE;
1212 rcu_read_unlock();
1214 *idx_s = 0;
1215 return 0;
1219 * batadv_tt_local_dump - Dump TT local entries into a message
1220 * @msg: Netlink message to dump into
1221 * @cb: Parameters from query
1223 * Return: Error code, or 0 on success
1225 int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1227 struct net *net = sock_net(cb->skb->sk);
1228 struct net_device *soft_iface;
1229 struct batadv_priv *bat_priv;
1230 struct batadv_hard_iface *primary_if = NULL;
1231 struct batadv_hashtable *hash;
1232 struct hlist_head *head;
1233 int ret;
1234 int ifindex;
1235 int bucket = cb->args[0];
1236 int idx = cb->args[1];
1237 int portid = NETLINK_CB(cb->skb).portid;
1239 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1240 if (!ifindex)
1241 return -EINVAL;
1243 soft_iface = dev_get_by_index(net, ifindex);
1244 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1245 ret = -ENODEV;
1246 goto out;
1249 bat_priv = netdev_priv(soft_iface);
1251 primary_if = batadv_primary_if_get_selected(bat_priv);
1252 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1253 ret = -ENOENT;
1254 goto out;
1257 hash = bat_priv->tt.local_hash;
1259 while (bucket < hash->size) {
1260 head = &hash->table[bucket];
1262 if (batadv_tt_local_dump_bucket(msg, portid, cb->nlh->nlmsg_seq,
1263 bat_priv, head, &idx))
1264 break;
1266 bucket++;
1269 ret = msg->len;
1271 out:
1272 if (primary_if)
1273 batadv_hardif_put(primary_if);
1274 if (soft_iface)
1275 dev_put(soft_iface);
1277 cb->args[0] = bucket;
1278 cb->args[1] = idx;
1280 return ret;
1283 static void
1284 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1285 struct batadv_tt_local_entry *tt_local_entry,
1286 u16 flags, const char *message)
1288 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1290 /* The local client has to be marked as "pending to be removed" but has
1291 * to be kept in the table in order to send it in a full table
1292 * response issued before the net ttvn increment (consistency check)
1294 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1296 batadv_dbg(BATADV_DBG_TT, bat_priv,
1297 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1298 tt_local_entry->common.addr,
1299 batadv_print_vid(tt_local_entry->common.vid), message);
1303 * batadv_tt_local_remove - logically remove an entry from the local table
1304 * @bat_priv: the bat priv with all the soft interface information
1305 * @addr: the MAC address of the client to remove
1306 * @vid: VLAN identifier
1307 * @message: message to append to the log on deletion
1308 * @roaming: true if the deletion is due to a roaming event
1310 * Return: the flags assigned to the local entry before being deleted
1312 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1313 unsigned short vid, const char *message,
1314 bool roaming)
1316 struct batadv_tt_local_entry *tt_local_entry;
1317 u16 flags, curr_flags = BATADV_NO_FLAGS;
1318 void *tt_entry_exists;
1320 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1321 if (!tt_local_entry)
1322 goto out;
1324 curr_flags = tt_local_entry->common.flags;
1326 flags = BATADV_TT_CLIENT_DEL;
1327 /* if this global entry addition is due to a roaming, the node has to
1328 * mark the local entry as "roamed" in order to correctly reroute
1329 * packets later
1331 if (roaming) {
1332 flags |= BATADV_TT_CLIENT_ROAM;
1333 /* mark the local client as ROAMed */
1334 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1337 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1338 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1339 message);
1340 goto out;
1342 /* if this client has been added right now, it is possible to
1343 * immediately purge it
1345 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1347 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1348 batadv_compare_tt,
1349 batadv_choose_tt,
1350 &tt_local_entry->common);
1351 if (!tt_entry_exists)
1352 goto out;
1354 /* extra call to free the local tt entry */
1355 batadv_tt_local_entry_put(tt_local_entry);
1357 out:
1358 if (tt_local_entry)
1359 batadv_tt_local_entry_put(tt_local_entry);
1361 return curr_flags;
1365 * batadv_tt_local_purge_list - purge inactive tt local entries
1366 * @bat_priv: the bat priv with all the soft interface information
1367 * @head: pointer to the list containing the local tt entries
1368 * @timeout: parameter deciding whether a given tt local entry is considered
1369 * inactive or not
1371 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1372 struct hlist_head *head,
1373 int timeout)
1375 struct batadv_tt_local_entry *tt_local_entry;
1376 struct batadv_tt_common_entry *tt_common_entry;
1377 struct hlist_node *node_tmp;
1379 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1380 hash_entry) {
1381 tt_local_entry = container_of(tt_common_entry,
1382 struct batadv_tt_local_entry,
1383 common);
1384 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1385 continue;
1387 /* entry already marked for deletion */
1388 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1389 continue;
1391 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1392 continue;
1394 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1395 BATADV_TT_CLIENT_DEL, "timed out");
1400 * batadv_tt_local_purge - purge inactive tt local entries
1401 * @bat_priv: the bat priv with all the soft interface information
1402 * @timeout: parameter deciding whether a given tt local entry is considered
1403 * inactive or not
1405 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1406 int timeout)
1408 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1409 struct hlist_head *head;
1410 spinlock_t *list_lock; /* protects write access to the hash lists */
1411 u32 i;
1413 for (i = 0; i < hash->size; i++) {
1414 head = &hash->table[i];
1415 list_lock = &hash->list_locks[i];
1417 spin_lock_bh(list_lock);
1418 batadv_tt_local_purge_list(bat_priv, head, timeout);
1419 spin_unlock_bh(list_lock);
1423 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1425 struct batadv_hashtable *hash;
1426 spinlock_t *list_lock; /* protects write access to the hash lists */
1427 struct batadv_tt_common_entry *tt_common_entry;
1428 struct batadv_tt_local_entry *tt_local;
1429 struct hlist_node *node_tmp;
1430 struct hlist_head *head;
1431 u32 i;
1433 if (!bat_priv->tt.local_hash)
1434 return;
1436 hash = bat_priv->tt.local_hash;
1438 for (i = 0; i < hash->size; i++) {
1439 head = &hash->table[i];
1440 list_lock = &hash->list_locks[i];
1442 spin_lock_bh(list_lock);
1443 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1444 head, hash_entry) {
1445 hlist_del_rcu(&tt_common_entry->hash_entry);
1446 tt_local = container_of(tt_common_entry,
1447 struct batadv_tt_local_entry,
1448 common);
1450 batadv_tt_local_entry_put(tt_local);
1452 spin_unlock_bh(list_lock);
1455 batadv_hash_destroy(hash);
1457 bat_priv->tt.local_hash = NULL;
1460 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1462 if (bat_priv->tt.global_hash)
1463 return 0;
1465 bat_priv->tt.global_hash = batadv_hash_new(1024);
1467 if (!bat_priv->tt.global_hash)
1468 return -ENOMEM;
1470 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1471 &batadv_tt_global_hash_lock_class_key);
1473 return 0;
1476 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1478 struct batadv_tt_change_node *entry, *safe;
1480 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1482 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1483 list) {
1484 list_del(&entry->list);
1485 kmem_cache_free(batadv_tt_change_cache, entry);
1488 atomic_set(&bat_priv->tt.local_changes, 0);
1489 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1493 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1494 * @entry: the TT global entry where the orig_list_entry has to be
1495 * extracted from
1496 * @orig_node: the originator for which the orig_list_entry has to be found
1498 * retrieve the orig_tt_list_entry belonging to orig_node from the
1499 * batadv_tt_global_entry list
1501 * Return: it with an increased refcounter, NULL if not found
1503 static struct batadv_tt_orig_list_entry *
1504 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1505 const struct batadv_orig_node *orig_node)
1507 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1508 const struct hlist_head *head;
1510 rcu_read_lock();
1511 head = &entry->orig_list;
1512 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1513 if (tmp_orig_entry->orig_node != orig_node)
1514 continue;
1515 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1516 continue;
1518 orig_entry = tmp_orig_entry;
1519 break;
1521 rcu_read_unlock();
1523 return orig_entry;
1527 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1528 * by a given originator
1529 * @entry: the TT global entry to check
1530 * @orig_node: the originator to search in the list
1532 * find out if an orig_node is already in the list of a tt_global_entry.
1534 * Return: true if found, false otherwise
1536 static bool
1537 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1538 const struct batadv_orig_node *orig_node)
1540 struct batadv_tt_orig_list_entry *orig_entry;
1541 bool found = false;
1543 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1544 if (orig_entry) {
1545 found = true;
1546 batadv_tt_orig_list_entry_put(orig_entry);
1549 return found;
1553 * batadv_tt_global_sync_flags - update TT sync flags
1554 * @tt_global: the TT global entry to update sync flags in
1556 * Updates the sync flag bits in the tt_global flag attribute with a logical
1557 * OR of all sync flags from any of its TT orig entries.
1559 static void
1560 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1562 struct batadv_tt_orig_list_entry *orig_entry;
1563 const struct hlist_head *head;
1564 u16 flags = BATADV_NO_FLAGS;
1566 rcu_read_lock();
1567 head = &tt_global->orig_list;
1568 hlist_for_each_entry_rcu(orig_entry, head, list)
1569 flags |= orig_entry->flags;
1570 rcu_read_unlock();
1572 flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1573 tt_global->common.flags = flags;
1577 * batadv_tt_global_orig_entry_add - add or update a TT orig entry
1578 * @tt_global: the TT global entry to add an orig entry in
1579 * @orig_node: the originator to add an orig entry for
1580 * @ttvn: translation table version number of this changeset
1581 * @flags: TT sync flags
1583 static void
1584 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1585 struct batadv_orig_node *orig_node, int ttvn,
1586 u8 flags)
1588 struct batadv_tt_orig_list_entry *orig_entry;
1590 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1591 if (orig_entry) {
1592 /* refresh the ttvn: the current value could be a bogus one that
1593 * was added during a "temporary client detection"
1595 orig_entry->ttvn = ttvn;
1596 orig_entry->flags = flags;
1597 goto sync_flags;
1600 orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1601 if (!orig_entry)
1602 goto out;
1604 INIT_HLIST_NODE(&orig_entry->list);
1605 kref_get(&orig_node->refcount);
1606 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1607 orig_entry->orig_node = orig_node;
1608 orig_entry->ttvn = ttvn;
1609 orig_entry->flags = flags;
1610 kref_init(&orig_entry->refcount);
1612 spin_lock_bh(&tt_global->list_lock);
1613 kref_get(&orig_entry->refcount);
1614 hlist_add_head_rcu(&orig_entry->list,
1615 &tt_global->orig_list);
1616 spin_unlock_bh(&tt_global->list_lock);
1617 atomic_inc(&tt_global->orig_list_count);
1619 sync_flags:
1620 batadv_tt_global_sync_flags(tt_global);
1621 out:
1622 if (orig_entry)
1623 batadv_tt_orig_list_entry_put(orig_entry);
1627 * batadv_tt_global_add - add a new TT global entry or update an existing one
1628 * @bat_priv: the bat priv with all the soft interface information
1629 * @orig_node: the originator announcing the client
1630 * @tt_addr: the mac address of the non-mesh client
1631 * @vid: VLAN identifier
1632 * @flags: TT flags that have to be set for this non-mesh client
1633 * @ttvn: the tt version number ever announcing this non-mesh client
1635 * Add a new TT global entry for the given originator. If the entry already
1636 * exists add a new reference to the given originator (a global entry can have
1637 * references to multiple originators) and adjust the flags attribute to reflect
1638 * the function argument.
1639 * If a TT local entry exists for this non-mesh client remove it.
1641 * The caller must hold orig_node refcount.
1643 * Return: true if the new entry has been added, false otherwise
1645 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1646 struct batadv_orig_node *orig_node,
1647 const unsigned char *tt_addr,
1648 unsigned short vid, u16 flags, u8 ttvn)
1650 struct batadv_tt_global_entry *tt_global_entry;
1651 struct batadv_tt_local_entry *tt_local_entry;
1652 bool ret = false;
1653 int hash_added;
1654 struct batadv_tt_common_entry *common;
1655 u16 local_flags;
1657 /* ignore global entries from backbone nodes */
1658 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1659 return true;
1661 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1662 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1664 /* if the node already has a local client for this entry, it has to wait
1665 * for a roaming advertisement instead of manually messing up the global
1666 * table
1668 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1669 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1670 goto out;
1672 if (!tt_global_entry) {
1673 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1674 GFP_ATOMIC);
1675 if (!tt_global_entry)
1676 goto out;
1678 common = &tt_global_entry->common;
1679 ether_addr_copy(common->addr, tt_addr);
1680 common->vid = vid;
1682 if (!is_multicast_ether_addr(common->addr))
1683 common->flags = flags & (~BATADV_TT_SYNC_MASK);
1685 tt_global_entry->roam_at = 0;
1686 /* node must store current time in case of roaming. This is
1687 * needed to purge this entry out on timeout (if nobody claims
1688 * it)
1690 if (flags & BATADV_TT_CLIENT_ROAM)
1691 tt_global_entry->roam_at = jiffies;
1692 kref_init(&common->refcount);
1693 common->added_at = jiffies;
1695 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1696 atomic_set(&tt_global_entry->orig_list_count, 0);
1697 spin_lock_init(&tt_global_entry->list_lock);
1699 kref_get(&common->refcount);
1700 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1701 batadv_compare_tt,
1702 batadv_choose_tt, common,
1703 &common->hash_entry);
1705 if (unlikely(hash_added != 0)) {
1706 /* remove the reference for the hash */
1707 batadv_tt_global_entry_put(tt_global_entry);
1708 goto out_remove;
1710 } else {
1711 common = &tt_global_entry->common;
1712 /* If there is already a global entry, we can use this one for
1713 * our processing.
1714 * But if we are trying to add a temporary client then here are
1715 * two options at this point:
1716 * 1) the global client is not a temporary client: the global
1717 * client has to be left as it is, temporary information
1718 * should never override any already known client state
1719 * 2) the global client is a temporary client: purge the
1720 * originator list and add the new one orig_entry
1722 if (flags & BATADV_TT_CLIENT_TEMP) {
1723 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1724 goto out;
1725 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1726 orig_node))
1727 goto out_remove;
1728 batadv_tt_global_del_orig_list(tt_global_entry);
1729 goto add_orig_entry;
1732 /* if the client was temporary added before receiving the first
1733 * OGM announcing it, we have to clear the TEMP flag. Also,
1734 * remove the previous temporary orig node and re-add it
1735 * if required. If the orig entry changed, the new one which
1736 * is a non-temporary entry is preferred.
1738 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1739 batadv_tt_global_del_orig_list(tt_global_entry);
1740 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1743 /* the change can carry possible "attribute" flags like the
1744 * TT_CLIENT_TEMP, therefore they have to be copied in the
1745 * client entry
1747 if (!is_multicast_ether_addr(common->addr))
1748 common->flags |= flags & (~BATADV_TT_SYNC_MASK);
1750 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1751 * one originator left in the list and we previously received a
1752 * delete + roaming change for this originator.
1754 * We should first delete the old originator before adding the
1755 * new one.
1757 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1758 batadv_tt_global_del_orig_list(tt_global_entry);
1759 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1760 tt_global_entry->roam_at = 0;
1763 add_orig_entry:
1764 /* add the new orig_entry (if needed) or update it */
1765 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1766 flags & BATADV_TT_SYNC_MASK);
1768 batadv_dbg(BATADV_DBG_TT, bat_priv,
1769 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1770 common->addr, batadv_print_vid(common->vid),
1771 orig_node->orig);
1772 ret = true;
1774 out_remove:
1775 /* Do not remove multicast addresses from the local hash on
1776 * global additions
1778 if (is_multicast_ether_addr(tt_addr))
1779 goto out;
1781 /* remove address from local hash if present */
1782 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1783 "global tt received",
1784 flags & BATADV_TT_CLIENT_ROAM);
1785 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1787 if (!(flags & BATADV_TT_CLIENT_ROAM))
1788 /* this is a normal global add. Therefore the client is not in a
1789 * roaming state anymore.
1791 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1793 out:
1794 if (tt_global_entry)
1795 batadv_tt_global_entry_put(tt_global_entry);
1796 if (tt_local_entry)
1797 batadv_tt_local_entry_put(tt_local_entry);
1798 return ret;
1802 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1803 * @bat_priv: the bat priv with all the soft interface information
1804 * @tt_global_entry: global translation table entry to be analyzed
1806 * This functon assumes the caller holds rcu_read_lock().
1807 * Return: best originator list entry or NULL on errors.
1809 static struct batadv_tt_orig_list_entry *
1810 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1811 struct batadv_tt_global_entry *tt_global_entry)
1813 struct batadv_neigh_node *router, *best_router = NULL;
1814 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1815 struct hlist_head *head;
1816 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1818 head = &tt_global_entry->orig_list;
1819 hlist_for_each_entry_rcu(orig_entry, head, list) {
1820 router = batadv_orig_router_get(orig_entry->orig_node,
1821 BATADV_IF_DEFAULT);
1822 if (!router)
1823 continue;
1825 if (best_router &&
1826 bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1827 BATADV_IF_DEFAULT) <= 0) {
1828 batadv_neigh_node_put(router);
1829 continue;
1832 /* release the refcount for the "old" best */
1833 if (best_router)
1834 batadv_neigh_node_put(best_router);
1836 best_entry = orig_entry;
1837 best_router = router;
1840 if (best_router)
1841 batadv_neigh_node_put(best_router);
1843 return best_entry;
1846 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1848 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1849 * for this global entry
1850 * @bat_priv: the bat priv with all the soft interface information
1851 * @tt_global_entry: global translation table entry to be printed
1852 * @seq: debugfs table seq_file struct
1854 * This functon assumes the caller holds rcu_read_lock().
1856 static void
1857 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1858 struct batadv_tt_global_entry *tt_global_entry,
1859 struct seq_file *seq)
1861 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1862 struct batadv_tt_common_entry *tt_common_entry;
1863 struct batadv_orig_node_vlan *vlan;
1864 struct hlist_head *head;
1865 u8 last_ttvn;
1866 u16 flags;
1868 tt_common_entry = &tt_global_entry->common;
1869 flags = tt_common_entry->flags;
1871 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1872 if (best_entry) {
1873 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1874 tt_common_entry->vid);
1875 if (!vlan) {
1876 seq_printf(seq,
1877 " * Cannot retrieve VLAN %d for originator %pM\n",
1878 batadv_print_vid(tt_common_entry->vid),
1879 best_entry->orig_node->orig);
1880 goto print_list;
1883 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1884 seq_printf(seq,
1885 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1886 '*', tt_global_entry->common.addr,
1887 batadv_print_vid(tt_global_entry->common.vid),
1888 best_entry->ttvn, best_entry->orig_node->orig,
1889 last_ttvn, vlan->tt.crc,
1890 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1891 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1892 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1893 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1895 batadv_orig_node_vlan_put(vlan);
1898 print_list:
1899 head = &tt_global_entry->orig_list;
1901 hlist_for_each_entry_rcu(orig_entry, head, list) {
1902 if (best_entry == orig_entry)
1903 continue;
1905 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1906 tt_common_entry->vid);
1907 if (!vlan) {
1908 seq_printf(seq,
1909 " + Cannot retrieve VLAN %d for originator %pM\n",
1910 batadv_print_vid(tt_common_entry->vid),
1911 orig_entry->orig_node->orig);
1912 continue;
1915 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1916 seq_printf(seq,
1917 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1918 '+', tt_global_entry->common.addr,
1919 batadv_print_vid(tt_global_entry->common.vid),
1920 orig_entry->ttvn, orig_entry->orig_node->orig,
1921 last_ttvn, vlan->tt.crc,
1922 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1923 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1924 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1925 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1927 batadv_orig_node_vlan_put(vlan);
1931 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1933 struct net_device *net_dev = (struct net_device *)seq->private;
1934 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1935 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1936 struct batadv_tt_common_entry *tt_common_entry;
1937 struct batadv_tt_global_entry *tt_global;
1938 struct batadv_hard_iface *primary_if;
1939 struct hlist_head *head;
1940 u32 i;
1942 primary_if = batadv_seq_print_text_primary_if_get(seq);
1943 if (!primary_if)
1944 goto out;
1946 seq_printf(seq,
1947 "Globally announced TT entries received via the mesh %s\n",
1948 net_dev->name);
1949 seq_puts(seq,
1950 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
1952 for (i = 0; i < hash->size; i++) {
1953 head = &hash->table[i];
1955 rcu_read_lock();
1956 hlist_for_each_entry_rcu(tt_common_entry,
1957 head, hash_entry) {
1958 tt_global = container_of(tt_common_entry,
1959 struct batadv_tt_global_entry,
1960 common);
1961 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1963 rcu_read_unlock();
1965 out:
1966 if (primary_if)
1967 batadv_hardif_put(primary_if);
1968 return 0;
1970 #endif
1973 * batadv_tt_global_dump_subentry - Dump all TT local entries into a message
1974 * @msg: Netlink message to dump into
1975 * @portid: Port making netlink request
1976 * @seq: Sequence number of netlink message
1977 * @common: tt local & tt global common data
1978 * @orig: Originator node announcing a non-mesh client
1979 * @best: Is the best originator for the TT entry
1981 * Return: Error code, or 0 on success
1983 static int
1984 batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1985 struct batadv_tt_common_entry *common,
1986 struct batadv_tt_orig_list_entry *orig,
1987 bool best)
1989 u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
1990 void *hdr;
1991 struct batadv_orig_node_vlan *vlan;
1992 u8 last_ttvn;
1993 u32 crc;
1995 vlan = batadv_orig_node_vlan_get(orig->orig_node,
1996 common->vid);
1997 if (!vlan)
1998 return 0;
2000 crc = vlan->tt.crc;
2002 batadv_orig_node_vlan_put(vlan);
2004 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2005 NLM_F_MULTI,
2006 BATADV_CMD_GET_TRANSTABLE_GLOBAL);
2007 if (!hdr)
2008 return -ENOBUFS;
2010 last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
2012 if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
2013 nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2014 orig->orig_node->orig) ||
2015 nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
2016 nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
2017 nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
2018 nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
2019 nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
2020 goto nla_put_failure;
2022 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2023 goto nla_put_failure;
2025 genlmsg_end(msg, hdr);
2026 return 0;
2028 nla_put_failure:
2029 genlmsg_cancel(msg, hdr);
2030 return -EMSGSIZE;
2034 * batadv_tt_global_dump_entry - Dump one TT global entry into a message
2035 * @msg: Netlink message to dump into
2036 * @portid: Port making netlink request
2037 * @seq: Sequence number of netlink message
2038 * @bat_priv: The bat priv with all the soft interface information
2039 * @common: tt local & tt global common data
2040 * @sub_s: Number of entries to skip
2042 * This function assumes the caller holds rcu_read_lock().
2044 * Return: Error code, or 0 on success
2046 static int
2047 batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2048 struct batadv_priv *bat_priv,
2049 struct batadv_tt_common_entry *common, int *sub_s)
2051 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
2052 struct batadv_tt_global_entry *global;
2053 struct hlist_head *head;
2054 int sub = 0;
2055 bool best;
2057 global = container_of(common, struct batadv_tt_global_entry, common);
2058 best_entry = batadv_transtable_best_orig(bat_priv, global);
2059 head = &global->orig_list;
2061 hlist_for_each_entry_rcu(orig_entry, head, list) {
2062 if (sub++ < *sub_s)
2063 continue;
2065 best = (orig_entry == best_entry);
2067 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
2068 orig_entry, best)) {
2069 *sub_s = sub - 1;
2070 return -EMSGSIZE;
2074 *sub_s = 0;
2075 return 0;
2079 * batadv_tt_global_dump_bucket - Dump one TT local bucket into a message
2080 * @msg: Netlink message to dump into
2081 * @portid: Port making netlink request
2082 * @seq: Sequence number of netlink message
2083 * @bat_priv: The bat priv with all the soft interface information
2084 * @head: Pointer to the list containing the global tt entries
2085 * @idx_s: Number of entries to skip
2086 * @sub: Number of entries to skip
2088 * Return: Error code, or 0 on success
2090 static int
2091 batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2092 struct batadv_priv *bat_priv,
2093 struct hlist_head *head, int *idx_s, int *sub)
2095 struct batadv_tt_common_entry *common;
2096 int idx = 0;
2098 rcu_read_lock();
2099 hlist_for_each_entry_rcu(common, head, hash_entry) {
2100 if (idx++ < *idx_s)
2101 continue;
2103 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
2104 common, sub)) {
2105 rcu_read_unlock();
2106 *idx_s = idx - 1;
2107 return -EMSGSIZE;
2110 rcu_read_unlock();
2112 *idx_s = 0;
2113 *sub = 0;
2114 return 0;
2118 * batadv_tt_global_dump - Dump TT global entries into a message
2119 * @msg: Netlink message to dump into
2120 * @cb: Parameters from query
2122 * Return: Error code, or length of message on success
2124 int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
2126 struct net *net = sock_net(cb->skb->sk);
2127 struct net_device *soft_iface;
2128 struct batadv_priv *bat_priv;
2129 struct batadv_hard_iface *primary_if = NULL;
2130 struct batadv_hashtable *hash;
2131 struct hlist_head *head;
2132 int ret;
2133 int ifindex;
2134 int bucket = cb->args[0];
2135 int idx = cb->args[1];
2136 int sub = cb->args[2];
2137 int portid = NETLINK_CB(cb->skb).portid;
2139 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2140 if (!ifindex)
2141 return -EINVAL;
2143 soft_iface = dev_get_by_index(net, ifindex);
2144 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2145 ret = -ENODEV;
2146 goto out;
2149 bat_priv = netdev_priv(soft_iface);
2151 primary_if = batadv_primary_if_get_selected(bat_priv);
2152 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2153 ret = -ENOENT;
2154 goto out;
2157 hash = bat_priv->tt.global_hash;
2159 while (bucket < hash->size) {
2160 head = &hash->table[bucket];
2162 if (batadv_tt_global_dump_bucket(msg, portid,
2163 cb->nlh->nlmsg_seq, bat_priv,
2164 head, &idx, &sub))
2165 break;
2167 bucket++;
2170 ret = msg->len;
2172 out:
2173 if (primary_if)
2174 batadv_hardif_put(primary_if);
2175 if (soft_iface)
2176 dev_put(soft_iface);
2178 cb->args[0] = bucket;
2179 cb->args[1] = idx;
2180 cb->args[2] = sub;
2182 return ret;
2186 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
2187 * @tt_global_entry: the global entry to remove the orig_entry from
2188 * @orig_entry: the orig entry to remove and free
2190 * Remove an orig_entry from its list in the given tt_global_entry and
2191 * free this orig_entry afterwards.
2193 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2194 * part of a list.
2196 static void
2197 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2198 struct batadv_tt_orig_list_entry *orig_entry)
2200 lockdep_assert_held(&tt_global_entry->list_lock);
2202 batadv_tt_global_size_dec(orig_entry->orig_node,
2203 tt_global_entry->common.vid);
2204 atomic_dec(&tt_global_entry->orig_list_count);
2205 /* requires holding tt_global_entry->list_lock and orig_entry->list
2206 * being part of a list
2208 hlist_del_rcu(&orig_entry->list);
2209 batadv_tt_orig_list_entry_put(orig_entry);
2212 /* deletes the orig list of a tt_global_entry */
2213 static void
2214 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
2216 struct hlist_head *head;
2217 struct hlist_node *safe;
2218 struct batadv_tt_orig_list_entry *orig_entry;
2220 spin_lock_bh(&tt_global_entry->list_lock);
2221 head = &tt_global_entry->orig_list;
2222 hlist_for_each_entry_safe(orig_entry, safe, head, list)
2223 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
2224 spin_unlock_bh(&tt_global_entry->list_lock);
2228 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
2229 * @bat_priv: the bat priv with all the soft interface information
2230 * @tt_global_entry: the global entry to remove the orig_node from
2231 * @orig_node: the originator announcing the client
2232 * @message: message to append to the log on deletion
2234 * Remove the given orig_node and its according orig_entry from the given
2235 * global tt entry.
2237 static void
2238 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2239 struct batadv_tt_global_entry *tt_global_entry,
2240 struct batadv_orig_node *orig_node,
2241 const char *message)
2243 struct hlist_head *head;
2244 struct hlist_node *safe;
2245 struct batadv_tt_orig_list_entry *orig_entry;
2246 unsigned short vid;
2248 spin_lock_bh(&tt_global_entry->list_lock);
2249 head = &tt_global_entry->orig_list;
2250 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
2251 if (orig_entry->orig_node == orig_node) {
2252 vid = tt_global_entry->common.vid;
2253 batadv_dbg(BATADV_DBG_TT, bat_priv,
2254 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2255 orig_node->orig,
2256 tt_global_entry->common.addr,
2257 batadv_print_vid(vid), message);
2258 _batadv_tt_global_del_orig_entry(tt_global_entry,
2259 orig_entry);
2262 spin_unlock_bh(&tt_global_entry->list_lock);
2265 /* If the client is to be deleted, we check if it is the last origantor entry
2266 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2267 * timer, otherwise we simply remove the originator scheduled for deletion.
2269 static void
2270 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2271 struct batadv_tt_global_entry *tt_global_entry,
2272 struct batadv_orig_node *orig_node,
2273 const char *message)
2275 bool last_entry = true;
2276 struct hlist_head *head;
2277 struct batadv_tt_orig_list_entry *orig_entry;
2279 /* no local entry exists, case 1:
2280 * Check if this is the last one or if other entries exist.
2283 rcu_read_lock();
2284 head = &tt_global_entry->orig_list;
2285 hlist_for_each_entry_rcu(orig_entry, head, list) {
2286 if (orig_entry->orig_node != orig_node) {
2287 last_entry = false;
2288 break;
2291 rcu_read_unlock();
2293 if (last_entry) {
2294 /* its the last one, mark for roaming. */
2295 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
2296 tt_global_entry->roam_at = jiffies;
2297 } else {
2298 /* there is another entry, we can simply delete this
2299 * one and can still use the other one.
2301 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2302 orig_node, message);
2307 * batadv_tt_global_del - remove a client from the global table
2308 * @bat_priv: the bat priv with all the soft interface information
2309 * @orig_node: an originator serving this client
2310 * @addr: the mac address of the client
2311 * @vid: VLAN identifier
2312 * @message: a message explaining the reason for deleting the client to print
2313 * for debugging purpose
2314 * @roaming: true if the deletion has been triggered by a roaming event
2316 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2317 struct batadv_orig_node *orig_node,
2318 const unsigned char *addr, unsigned short vid,
2319 const char *message, bool roaming)
2321 struct batadv_tt_global_entry *tt_global_entry;
2322 struct batadv_tt_local_entry *local_entry = NULL;
2324 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2325 if (!tt_global_entry)
2326 goto out;
2328 if (!roaming) {
2329 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2330 orig_node, message);
2332 if (hlist_empty(&tt_global_entry->orig_list))
2333 batadv_tt_global_free(bat_priv, tt_global_entry,
2334 message);
2336 goto out;
2339 /* if we are deleting a global entry due to a roam
2340 * event, there are two possibilities:
2341 * 1) the client roamed from node A to node B => if there
2342 * is only one originator left for this client, we mark
2343 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
2344 * wait for node B to claim it. In case of timeout
2345 * the entry is purged.
2347 * If there are other originators left, we directly delete
2348 * the originator.
2349 * 2) the client roamed to us => we can directly delete
2350 * the global entry, since it is useless now.
2352 local_entry = batadv_tt_local_hash_find(bat_priv,
2353 tt_global_entry->common.addr,
2354 vid);
2355 if (local_entry) {
2356 /* local entry exists, case 2: client roamed to us. */
2357 batadv_tt_global_del_orig_list(tt_global_entry);
2358 batadv_tt_global_free(bat_priv, tt_global_entry, message);
2359 } else {
2360 /* no local entry exists, case 1: check for roaming */
2361 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2362 orig_node, message);
2365 out:
2366 if (tt_global_entry)
2367 batadv_tt_global_entry_put(tt_global_entry);
2368 if (local_entry)
2369 batadv_tt_local_entry_put(local_entry);
2373 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
2374 * given originator matching the provided vid
2375 * @bat_priv: the bat priv with all the soft interface information
2376 * @orig_node: the originator owning the entries to remove
2377 * @match_vid: the VLAN identifier to match. If negative all the entries will be
2378 * removed
2379 * @message: debug message to print as "reason"
2381 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2382 struct batadv_orig_node *orig_node,
2383 s32 match_vid,
2384 const char *message)
2386 struct batadv_tt_global_entry *tt_global;
2387 struct batadv_tt_common_entry *tt_common_entry;
2388 u32 i;
2389 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2390 struct hlist_node *safe;
2391 struct hlist_head *head;
2392 spinlock_t *list_lock; /* protects write access to the hash lists */
2393 unsigned short vid;
2395 if (!hash)
2396 return;
2398 for (i = 0; i < hash->size; i++) {
2399 head = &hash->table[i];
2400 list_lock = &hash->list_locks[i];
2402 spin_lock_bh(list_lock);
2403 hlist_for_each_entry_safe(tt_common_entry, safe,
2404 head, hash_entry) {
2405 /* remove only matching entries */
2406 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2407 continue;
2409 tt_global = container_of(tt_common_entry,
2410 struct batadv_tt_global_entry,
2411 common);
2413 batadv_tt_global_del_orig_node(bat_priv, tt_global,
2414 orig_node, message);
2416 if (hlist_empty(&tt_global->orig_list)) {
2417 vid = tt_global->common.vid;
2418 batadv_dbg(BATADV_DBG_TT, bat_priv,
2419 "Deleting global tt entry %pM (vid: %d): %s\n",
2420 tt_global->common.addr,
2421 batadv_print_vid(vid), message);
2422 hlist_del_rcu(&tt_common_entry->hash_entry);
2423 batadv_tt_global_entry_put(tt_global);
2426 spin_unlock_bh(list_lock);
2428 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2431 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2432 char **msg)
2434 bool purge = false;
2435 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2436 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2438 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2439 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2440 purge = true;
2441 *msg = "Roaming timeout\n";
2444 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2445 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2446 purge = true;
2447 *msg = "Temporary client timeout\n";
2450 return purge;
2453 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2455 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2456 struct hlist_head *head;
2457 struct hlist_node *node_tmp;
2458 spinlock_t *list_lock; /* protects write access to the hash lists */
2459 u32 i;
2460 char *msg = NULL;
2461 struct batadv_tt_common_entry *tt_common;
2462 struct batadv_tt_global_entry *tt_global;
2464 for (i = 0; i < hash->size; i++) {
2465 head = &hash->table[i];
2466 list_lock = &hash->list_locks[i];
2468 spin_lock_bh(list_lock);
2469 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2470 hash_entry) {
2471 tt_global = container_of(tt_common,
2472 struct batadv_tt_global_entry,
2473 common);
2475 if (!batadv_tt_global_to_purge(tt_global, &msg))
2476 continue;
2478 batadv_dbg(BATADV_DBG_TT, bat_priv,
2479 "Deleting global tt entry %pM (vid: %d): %s\n",
2480 tt_global->common.addr,
2481 batadv_print_vid(tt_global->common.vid),
2482 msg);
2484 hlist_del_rcu(&tt_common->hash_entry);
2486 batadv_tt_global_entry_put(tt_global);
2488 spin_unlock_bh(list_lock);
2492 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2494 struct batadv_hashtable *hash;
2495 spinlock_t *list_lock; /* protects write access to the hash lists */
2496 struct batadv_tt_common_entry *tt_common_entry;
2497 struct batadv_tt_global_entry *tt_global;
2498 struct hlist_node *node_tmp;
2499 struct hlist_head *head;
2500 u32 i;
2502 if (!bat_priv->tt.global_hash)
2503 return;
2505 hash = bat_priv->tt.global_hash;
2507 for (i = 0; i < hash->size; i++) {
2508 head = &hash->table[i];
2509 list_lock = &hash->list_locks[i];
2511 spin_lock_bh(list_lock);
2512 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2513 head, hash_entry) {
2514 hlist_del_rcu(&tt_common_entry->hash_entry);
2515 tt_global = container_of(tt_common_entry,
2516 struct batadv_tt_global_entry,
2517 common);
2518 batadv_tt_global_entry_put(tt_global);
2520 spin_unlock_bh(list_lock);
2523 batadv_hash_destroy(hash);
2525 bat_priv->tt.global_hash = NULL;
2528 static bool
2529 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2530 struct batadv_tt_global_entry *tt_global_entry)
2532 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2533 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2534 return true;
2536 /* check if the two clients are marked as isolated */
2537 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2538 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2539 return true;
2541 return false;
2545 * batadv_transtable_search - get the mesh destination for a given client
2546 * @bat_priv: the bat priv with all the soft interface information
2547 * @src: mac address of the source client
2548 * @addr: mac address of the destination client
2549 * @vid: VLAN identifier
2551 * Return: a pointer to the originator that was selected as destination in the
2552 * mesh for contacting the client 'addr', NULL otherwise.
2553 * In case of multiple originators serving the same client, the function returns
2554 * the best one (best in terms of metric towards the destination node).
2556 * If the two clients are AP isolated the function returns NULL.
2558 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2559 const u8 *src,
2560 const u8 *addr,
2561 unsigned short vid)
2563 struct batadv_tt_local_entry *tt_local_entry = NULL;
2564 struct batadv_tt_global_entry *tt_global_entry = NULL;
2565 struct batadv_orig_node *orig_node = NULL;
2566 struct batadv_tt_orig_list_entry *best_entry;
2568 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2569 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2570 if (!tt_local_entry ||
2571 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2572 goto out;
2575 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2576 if (!tt_global_entry)
2577 goto out;
2579 /* check whether the clients should not communicate due to AP
2580 * isolation
2582 if (tt_local_entry &&
2583 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2584 goto out;
2586 rcu_read_lock();
2587 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2588 /* found anything? */
2589 if (best_entry)
2590 orig_node = best_entry->orig_node;
2591 if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2592 orig_node = NULL;
2593 rcu_read_unlock();
2595 out:
2596 if (tt_global_entry)
2597 batadv_tt_global_entry_put(tt_global_entry);
2598 if (tt_local_entry)
2599 batadv_tt_local_entry_put(tt_local_entry);
2601 return orig_node;
2605 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2606 * to the given orig_node
2607 * @bat_priv: the bat priv with all the soft interface information
2608 * @orig_node: originator for which the CRC should be computed
2609 * @vid: VLAN identifier for which the CRC32 has to be computed
2611 * This function computes the checksum for the global table corresponding to a
2612 * specific originator. In particular, the checksum is computed as follows: For
2613 * each client connected to the originator the CRC32C of the MAC address and the
2614 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2615 * together.
2617 * The idea behind is that CRC32C should be used as much as possible in order to
2618 * produce a unique hash of the table, but since the order which is used to feed
2619 * the CRC32C function affects the result and since every node in the network
2620 * probably sorts the clients differently, the hash function cannot be directly
2621 * computed over the entire table. Hence the CRC32C is used only on
2622 * the single client entry, while all the results are then xor'ed together
2623 * because the XOR operation can combine them all while trying to reduce the
2624 * noise as much as possible.
2626 * Return: the checksum of the global table of a given originator.
2628 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2629 struct batadv_orig_node *orig_node,
2630 unsigned short vid)
2632 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2633 struct batadv_tt_orig_list_entry *tt_orig;
2634 struct batadv_tt_common_entry *tt_common;
2635 struct batadv_tt_global_entry *tt_global;
2636 struct hlist_head *head;
2637 u32 i, crc_tmp, crc = 0;
2638 u8 flags;
2639 __be16 tmp_vid;
2641 for (i = 0; i < hash->size; i++) {
2642 head = &hash->table[i];
2644 rcu_read_lock();
2645 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2646 tt_global = container_of(tt_common,
2647 struct batadv_tt_global_entry,
2648 common);
2649 /* compute the CRC only for entries belonging to the
2650 * VLAN identified by the vid passed as parameter
2652 if (tt_common->vid != vid)
2653 continue;
2655 /* Roaming clients are in the global table for
2656 * consistency only. They don't have to be
2657 * taken into account while computing the
2658 * global crc
2660 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2661 continue;
2662 /* Temporary clients have not been announced yet, so
2663 * they have to be skipped while computing the global
2664 * crc
2666 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2667 continue;
2669 /* find out if this global entry is announced by this
2670 * originator
2672 tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2673 orig_node);
2674 if (!tt_orig)
2675 continue;
2677 /* use network order to read the VID: this ensures that
2678 * every node reads the bytes in the same order.
2680 tmp_vid = htons(tt_common->vid);
2681 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2683 /* compute the CRC on flags that have to be kept in sync
2684 * among nodes
2686 flags = tt_orig->flags;
2687 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2689 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2691 batadv_tt_orig_list_entry_put(tt_orig);
2693 rcu_read_unlock();
2696 return crc;
2700 * batadv_tt_local_crc - calculates the checksum of the local table
2701 * @bat_priv: the bat priv with all the soft interface information
2702 * @vid: VLAN identifier for which the CRC32 has to be computed
2704 * For details about the computation, please refer to the documentation for
2705 * batadv_tt_global_crc().
2707 * Return: the checksum of the local table
2709 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2710 unsigned short vid)
2712 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2713 struct batadv_tt_common_entry *tt_common;
2714 struct hlist_head *head;
2715 u32 i, crc_tmp, crc = 0;
2716 u8 flags;
2717 __be16 tmp_vid;
2719 for (i = 0; i < hash->size; i++) {
2720 head = &hash->table[i];
2722 rcu_read_lock();
2723 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2724 /* compute the CRC only for entries belonging to the
2725 * VLAN identified by vid
2727 if (tt_common->vid != vid)
2728 continue;
2730 /* not yet committed clients have not to be taken into
2731 * account while computing the CRC
2733 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2734 continue;
2736 /* use network order to read the VID: this ensures that
2737 * every node reads the bytes in the same order.
2739 tmp_vid = htons(tt_common->vid);
2740 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2742 /* compute the CRC on flags that have to be kept in sync
2743 * among nodes
2745 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2746 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2748 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2750 rcu_read_unlock();
2753 return crc;
2757 * batadv_tt_req_node_release - free tt_req node entry
2758 * @ref: kref pointer of the tt req_node entry
2760 static void batadv_tt_req_node_release(struct kref *ref)
2762 struct batadv_tt_req_node *tt_req_node;
2764 tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2766 kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2770 * batadv_tt_req_node_put - decrement the tt_req_node refcounter and
2771 * possibly release it
2772 * @tt_req_node: tt_req_node to be free'd
2774 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2776 kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2779 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2781 struct batadv_tt_req_node *node;
2782 struct hlist_node *safe;
2784 spin_lock_bh(&bat_priv->tt.req_list_lock);
2786 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2787 hlist_del_init(&node->list);
2788 batadv_tt_req_node_put(node);
2791 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2794 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2795 struct batadv_orig_node *orig_node,
2796 const void *tt_buff,
2797 u16 tt_buff_len)
2799 /* Replace the old buffer only if I received something in the
2800 * last OGM (the OGM could carry no changes)
2802 spin_lock_bh(&orig_node->tt_buff_lock);
2803 if (tt_buff_len > 0) {
2804 kfree(orig_node->tt_buff);
2805 orig_node->tt_buff_len = 0;
2806 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2807 if (orig_node->tt_buff) {
2808 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2809 orig_node->tt_buff_len = tt_buff_len;
2812 spin_unlock_bh(&orig_node->tt_buff_lock);
2815 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2817 struct batadv_tt_req_node *node;
2818 struct hlist_node *safe;
2820 spin_lock_bh(&bat_priv->tt.req_list_lock);
2821 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2822 if (batadv_has_timed_out(node->issued_at,
2823 BATADV_TT_REQUEST_TIMEOUT)) {
2824 hlist_del_init(&node->list);
2825 batadv_tt_req_node_put(node);
2828 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2832 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2833 * @bat_priv: the bat priv with all the soft interface information
2834 * @orig_node: orig node this request is being issued for
2836 * Return: the pointer to the new tt_req_node struct if no request
2837 * has already been issued for this orig_node, NULL otherwise.
2839 static struct batadv_tt_req_node *
2840 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2841 struct batadv_orig_node *orig_node)
2843 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2845 spin_lock_bh(&bat_priv->tt.req_list_lock);
2846 hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2847 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2848 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2849 BATADV_TT_REQUEST_TIMEOUT))
2850 goto unlock;
2853 tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2854 if (!tt_req_node)
2855 goto unlock;
2857 kref_init(&tt_req_node->refcount);
2858 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2859 tt_req_node->issued_at = jiffies;
2861 kref_get(&tt_req_node->refcount);
2862 hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2863 unlock:
2864 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2865 return tt_req_node;
2869 * batadv_tt_local_valid - verify that given tt entry is a valid one
2870 * @entry_ptr: to be checked local tt entry
2871 * @data_ptr: not used but definition required to satisfy the callback prototype
2873 * Return: true if the entry is a valid, false otherwise.
2875 static bool batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2877 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2879 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2880 return false;
2881 return true;
2884 static bool batadv_tt_global_valid(const void *entry_ptr,
2885 const void *data_ptr)
2887 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2888 const struct batadv_tt_global_entry *tt_global_entry;
2889 const struct batadv_orig_node *orig_node = data_ptr;
2891 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2892 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2893 return false;
2895 tt_global_entry = container_of(tt_common_entry,
2896 struct batadv_tt_global_entry,
2897 common);
2899 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2903 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2904 * specified tt hash
2905 * @bat_priv: the bat priv with all the soft interface information
2906 * @hash: hash table containing the tt entries
2907 * @tt_len: expected tvlv tt data buffer length in number of bytes
2908 * @tvlv_buff: pointer to the buffer to fill with the TT data
2909 * @valid_cb: function to filter tt change entries
2910 * @cb_data: data passed to the filter function as argument
2912 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2913 struct batadv_hashtable *hash,
2914 void *tvlv_buff, u16 tt_len,
2915 bool (*valid_cb)(const void *,
2916 const void *),
2917 void *cb_data)
2919 struct batadv_tt_common_entry *tt_common_entry;
2920 struct batadv_tvlv_tt_change *tt_change;
2921 struct hlist_head *head;
2922 u16 tt_tot, tt_num_entries = 0;
2923 u32 i;
2925 tt_tot = batadv_tt_entries(tt_len);
2926 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2928 rcu_read_lock();
2929 for (i = 0; i < hash->size; i++) {
2930 head = &hash->table[i];
2932 hlist_for_each_entry_rcu(tt_common_entry,
2933 head, hash_entry) {
2934 if (tt_tot == tt_num_entries)
2935 break;
2937 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2938 continue;
2940 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2941 tt_change->flags = tt_common_entry->flags;
2942 tt_change->vid = htons(tt_common_entry->vid);
2943 memset(tt_change->reserved, 0,
2944 sizeof(tt_change->reserved));
2946 tt_num_entries++;
2947 tt_change++;
2950 rcu_read_unlock();
2954 * batadv_tt_global_check_crc - check if all the CRCs are correct
2955 * @orig_node: originator for which the CRCs have to be checked
2956 * @tt_vlan: pointer to the first tvlv VLAN entry
2957 * @num_vlan: number of tvlv VLAN entries
2959 * Return: true if all the received CRCs match the locally stored ones, false
2960 * otherwise
2962 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2963 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2964 u16 num_vlan)
2966 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2967 struct batadv_orig_node_vlan *vlan;
2968 int i, orig_num_vlan;
2969 u32 crc;
2971 /* check if each received CRC matches the locally stored one */
2972 for (i = 0; i < num_vlan; i++) {
2973 tt_vlan_tmp = tt_vlan + i;
2975 /* if orig_node is a backbone node for this VLAN, don't check
2976 * the CRC as we ignore all the global entries over it
2978 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2979 orig_node->orig,
2980 ntohs(tt_vlan_tmp->vid)))
2981 continue;
2983 vlan = batadv_orig_node_vlan_get(orig_node,
2984 ntohs(tt_vlan_tmp->vid));
2985 if (!vlan)
2986 return false;
2988 crc = vlan->tt.crc;
2989 batadv_orig_node_vlan_put(vlan);
2991 if (crc != ntohl(tt_vlan_tmp->crc))
2992 return false;
2995 /* check if any excess VLANs exist locally for the originator
2996 * which are not mentioned in the TVLV from the originator.
2998 rcu_read_lock();
2999 orig_num_vlan = 0;
3000 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
3001 orig_num_vlan++;
3002 rcu_read_unlock();
3004 if (orig_num_vlan > num_vlan)
3005 return false;
3007 return true;
3011 * batadv_tt_local_update_crc - update all the local CRCs
3012 * @bat_priv: the bat priv with all the soft interface information
3014 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
3016 struct batadv_softif_vlan *vlan;
3018 /* recompute the global CRC for each VLAN */
3019 rcu_read_lock();
3020 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
3021 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
3023 rcu_read_unlock();
3027 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
3028 * @bat_priv: the bat priv with all the soft interface information
3029 * @orig_node: the orig_node for which the CRCs have to be updated
3031 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
3032 struct batadv_orig_node *orig_node)
3034 struct batadv_orig_node_vlan *vlan;
3035 u32 crc;
3037 /* recompute the global CRC for each VLAN */
3038 rcu_read_lock();
3039 hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
3040 /* if orig_node is a backbone node for this VLAN, don't compute
3041 * the CRC as we ignore all the global entries over it
3043 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
3044 vlan->vid))
3045 continue;
3047 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
3048 vlan->tt.crc = crc;
3050 rcu_read_unlock();
3054 * batadv_send_tt_request - send a TT Request message to a given node
3055 * @bat_priv: the bat priv with all the soft interface information
3056 * @dst_orig_node: the destination of the message
3057 * @ttvn: the version number that the source of the message is looking for
3058 * @tt_vlan: pointer to the first tvlv VLAN object to request
3059 * @num_vlan: number of tvlv VLAN entries
3060 * @full_table: ask for the entire translation table if true, while only for the
3061 * last TT diff otherwise
3063 * Return: true if the TT Request was sent, false otherwise
3065 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
3066 struct batadv_orig_node *dst_orig_node,
3067 u8 ttvn,
3068 struct batadv_tvlv_tt_vlan_data *tt_vlan,
3069 u16 num_vlan, bool full_table)
3071 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3072 struct batadv_tt_req_node *tt_req_node = NULL;
3073 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
3074 struct batadv_hard_iface *primary_if;
3075 bool ret = false;
3076 int i, size;
3078 primary_if = batadv_primary_if_get_selected(bat_priv);
3079 if (!primary_if)
3080 goto out;
3082 /* The new tt_req will be issued only if I'm not waiting for a
3083 * reply from the same orig_node yet
3085 tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
3086 if (!tt_req_node)
3087 goto out;
3089 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
3090 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
3091 if (!tvlv_tt_data)
3092 goto out;
3094 tvlv_tt_data->flags = BATADV_TT_REQUEST;
3095 tvlv_tt_data->ttvn = ttvn;
3096 tvlv_tt_data->num_vlan = htons(num_vlan);
3098 /* send all the CRCs within the request. This is needed by intermediate
3099 * nodes to ensure they have the correct table before replying
3101 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
3102 for (i = 0; i < num_vlan; i++) {
3103 tt_vlan_req->vid = tt_vlan->vid;
3104 tt_vlan_req->crc = tt_vlan->crc;
3106 tt_vlan_req++;
3107 tt_vlan++;
3110 if (full_table)
3111 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3113 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
3114 dst_orig_node->orig, full_table ? 'F' : '.');
3116 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
3117 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3118 dst_orig_node->orig, BATADV_TVLV_TT, 1,
3119 tvlv_tt_data, size);
3120 ret = true;
3122 out:
3123 if (primary_if)
3124 batadv_hardif_put(primary_if);
3126 if (ret && tt_req_node) {
3127 spin_lock_bh(&bat_priv->tt.req_list_lock);
3128 if (!hlist_unhashed(&tt_req_node->list)) {
3129 hlist_del_init(&tt_req_node->list);
3130 batadv_tt_req_node_put(tt_req_node);
3132 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3135 if (tt_req_node)
3136 batadv_tt_req_node_put(tt_req_node);
3138 kfree(tvlv_tt_data);
3139 return ret;
3143 * batadv_send_other_tt_response - send reply to tt request concerning another
3144 * node's translation table
3145 * @bat_priv: the bat priv with all the soft interface information
3146 * @tt_data: tt data containing the tt request information
3147 * @req_src: mac address of tt request sender
3148 * @req_dst: mac address of tt request recipient
3150 * Return: true if tt request reply was sent, false otherwise.
3152 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
3153 struct batadv_tvlv_tt_data *tt_data,
3154 u8 *req_src, u8 *req_dst)
3156 struct batadv_orig_node *req_dst_orig_node;
3157 struct batadv_orig_node *res_dst_orig_node = NULL;
3158 struct batadv_tvlv_tt_change *tt_change;
3159 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3160 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3161 bool ret = false, full_table;
3162 u8 orig_ttvn, req_ttvn;
3163 u16 tvlv_len;
3164 s32 tt_len;
3166 batadv_dbg(BATADV_DBG_TT, bat_priv,
3167 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
3168 req_src, tt_data->ttvn, req_dst,
3169 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3171 /* Let's get the orig node of the REAL destination */
3172 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
3173 if (!req_dst_orig_node)
3174 goto out;
3176 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
3177 if (!res_dst_orig_node)
3178 goto out;
3180 orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
3181 req_ttvn = tt_data->ttvn;
3183 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3184 /* this node doesn't have the requested data */
3185 if (orig_ttvn != req_ttvn ||
3186 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3187 ntohs(tt_data->num_vlan)))
3188 goto out;
3190 /* If the full table has been explicitly requested */
3191 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
3192 !req_dst_orig_node->tt_buff)
3193 full_table = true;
3194 else
3195 full_table = false;
3197 /* TT fragmentation hasn't been implemented yet, so send as many
3198 * TT entries fit a single packet as possible only
3200 if (!full_table) {
3201 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3202 tt_len = req_dst_orig_node->tt_buff_len;
3204 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3205 &tvlv_tt_data,
3206 &tt_change,
3207 &tt_len);
3208 if (!tt_len)
3209 goto unlock;
3211 /* Copy the last orig_node's OGM buffer */
3212 memcpy(tt_change, req_dst_orig_node->tt_buff,
3213 req_dst_orig_node->tt_buff_len);
3214 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3215 } else {
3216 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3217 * in the initial part
3219 tt_len = -1;
3220 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3221 &tvlv_tt_data,
3222 &tt_change,
3223 &tt_len);
3224 if (!tt_len)
3225 goto out;
3227 /* fill the rest of the tvlv with the real TT entries */
3228 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3229 tt_change, tt_len,
3230 batadv_tt_global_valid,
3231 req_dst_orig_node);
3234 /* Don't send the response, if larger than fragmented packet. */
3235 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3236 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3237 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3238 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3239 res_dst_orig_node->orig);
3240 goto out;
3243 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3244 tvlv_tt_data->ttvn = req_ttvn;
3246 if (full_table)
3247 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3249 batadv_dbg(BATADV_DBG_TT, bat_priv,
3250 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3251 res_dst_orig_node->orig, req_dst_orig_node->orig,
3252 full_table ? 'F' : '.', req_ttvn);
3254 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3256 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
3257 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3258 tvlv_len);
3260 ret = true;
3261 goto out;
3263 unlock:
3264 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3266 out:
3267 if (res_dst_orig_node)
3268 batadv_orig_node_put(res_dst_orig_node);
3269 if (req_dst_orig_node)
3270 batadv_orig_node_put(req_dst_orig_node);
3271 kfree(tvlv_tt_data);
3272 return ret;
3276 * batadv_send_my_tt_response - send reply to tt request concerning this node's
3277 * translation table
3278 * @bat_priv: the bat priv with all the soft interface information
3279 * @tt_data: tt data containing the tt request information
3280 * @req_src: mac address of tt request sender
3282 * Return: true if tt request reply was sent, false otherwise.
3284 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3285 struct batadv_tvlv_tt_data *tt_data,
3286 u8 *req_src)
3288 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3289 struct batadv_hard_iface *primary_if = NULL;
3290 struct batadv_tvlv_tt_change *tt_change;
3291 struct batadv_orig_node *orig_node;
3292 u8 my_ttvn, req_ttvn;
3293 u16 tvlv_len;
3294 bool full_table;
3295 s32 tt_len;
3297 batadv_dbg(BATADV_DBG_TT, bat_priv,
3298 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3299 req_src, tt_data->ttvn,
3300 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3302 spin_lock_bh(&bat_priv->tt.commit_lock);
3304 my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3305 req_ttvn = tt_data->ttvn;
3307 orig_node = batadv_orig_hash_find(bat_priv, req_src);
3308 if (!orig_node)
3309 goto out;
3311 primary_if = batadv_primary_if_get_selected(bat_priv);
3312 if (!primary_if)
3313 goto out;
3315 /* If the full table has been explicitly requested or the gap
3316 * is too big send the whole local translation table
3318 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
3319 !bat_priv->tt.last_changeset)
3320 full_table = true;
3321 else
3322 full_table = false;
3324 /* TT fragmentation hasn't been implemented yet, so send as many
3325 * TT entries fit a single packet as possible only
3327 if (!full_table) {
3328 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
3330 tt_len = bat_priv->tt.last_changeset_len;
3331 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3332 &tvlv_tt_data,
3333 &tt_change,
3334 &tt_len);
3335 if (!tt_len || !tvlv_len)
3336 goto unlock;
3338 /* Copy the last orig_node's OGM buffer */
3339 memcpy(tt_change, bat_priv->tt.last_changeset,
3340 bat_priv->tt.last_changeset_len);
3341 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3342 } else {
3343 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3345 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3346 * in the initial part
3348 tt_len = -1;
3349 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3350 &tvlv_tt_data,
3351 &tt_change,
3352 &tt_len);
3353 if (!tt_len || !tvlv_len)
3354 goto out;
3356 /* fill the rest of the tvlv with the real TT entries */
3357 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3358 tt_change, tt_len,
3359 batadv_tt_local_valid, NULL);
3362 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3363 tvlv_tt_data->ttvn = req_ttvn;
3365 if (full_table)
3366 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3368 batadv_dbg(BATADV_DBG_TT, bat_priv,
3369 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3370 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
3372 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3374 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3375 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3376 tvlv_len);
3378 goto out;
3380 unlock:
3381 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3382 out:
3383 spin_unlock_bh(&bat_priv->tt.commit_lock);
3384 if (orig_node)
3385 batadv_orig_node_put(orig_node);
3386 if (primary_if)
3387 batadv_hardif_put(primary_if);
3388 kfree(tvlv_tt_data);
3389 /* The packet was for this host, so it doesn't need to be re-routed */
3390 return true;
3394 * batadv_send_tt_response - send reply to tt request
3395 * @bat_priv: the bat priv with all the soft interface information
3396 * @tt_data: tt data containing the tt request information
3397 * @req_src: mac address of tt request sender
3398 * @req_dst: mac address of tt request recipient
3400 * Return: true if tt request reply was sent, false otherwise.
3402 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3403 struct batadv_tvlv_tt_data *tt_data,
3404 u8 *req_src, u8 *req_dst)
3406 if (batadv_is_my_mac(bat_priv, req_dst))
3407 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
3408 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3409 req_dst);
3412 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3413 struct batadv_orig_node *orig_node,
3414 struct batadv_tvlv_tt_change *tt_change,
3415 u16 tt_num_changes, u8 ttvn)
3417 int i;
3418 int roams;
3420 for (i = 0; i < tt_num_changes; i++) {
3421 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3422 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
3423 batadv_tt_global_del(bat_priv, orig_node,
3424 (tt_change + i)->addr,
3425 ntohs((tt_change + i)->vid),
3426 "tt removed by changes",
3427 roams);
3428 } else {
3429 if (!batadv_tt_global_add(bat_priv, orig_node,
3430 (tt_change + i)->addr,
3431 ntohs((tt_change + i)->vid),
3432 (tt_change + i)->flags, ttvn))
3433 /* In case of problem while storing a
3434 * global_entry, we stop the updating
3435 * procedure without committing the
3436 * ttvn change. This will avoid to send
3437 * corrupted data on tt_request
3439 return;
3442 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3445 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3446 struct batadv_tvlv_tt_change *tt_change,
3447 u8 ttvn, u8 *resp_src,
3448 u16 num_entries)
3450 struct batadv_orig_node *orig_node;
3452 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3453 if (!orig_node)
3454 goto out;
3456 /* Purge the old table first.. */
3457 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3458 "Received full table");
3460 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3461 ttvn);
3463 spin_lock_bh(&orig_node->tt_buff_lock);
3464 kfree(orig_node->tt_buff);
3465 orig_node->tt_buff_len = 0;
3466 orig_node->tt_buff = NULL;
3467 spin_unlock_bh(&orig_node->tt_buff_lock);
3469 atomic_set(&orig_node->last_ttvn, ttvn);
3471 out:
3472 if (orig_node)
3473 batadv_orig_node_put(orig_node);
3476 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3477 struct batadv_orig_node *orig_node,
3478 u16 tt_num_changes, u8 ttvn,
3479 struct batadv_tvlv_tt_change *tt_change)
3481 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3482 tt_num_changes, ttvn);
3484 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3485 batadv_tt_len(tt_num_changes));
3486 atomic_set(&orig_node->last_ttvn, ttvn);
3490 * batadv_is_my_client - check if a client is served by the local node
3491 * @bat_priv: the bat priv with all the soft interface information
3492 * @addr: the mac address of the client to check
3493 * @vid: VLAN identifier
3495 * Return: true if the client is served by this node, false otherwise.
3497 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3498 unsigned short vid)
3500 struct batadv_tt_local_entry *tt_local_entry;
3501 bool ret = false;
3503 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3504 if (!tt_local_entry)
3505 goto out;
3506 /* Check if the client has been logically deleted (but is kept for
3507 * consistency purpose)
3509 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3510 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3511 goto out;
3512 ret = true;
3513 out:
3514 if (tt_local_entry)
3515 batadv_tt_local_entry_put(tt_local_entry);
3516 return ret;
3520 * batadv_handle_tt_response - process incoming tt reply
3521 * @bat_priv: the bat priv with all the soft interface information
3522 * @tt_data: tt data containing the tt request information
3523 * @resp_src: mac address of tt reply sender
3524 * @num_entries: number of tt change entries appended to the tt data
3526 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3527 struct batadv_tvlv_tt_data *tt_data,
3528 u8 *resp_src, u16 num_entries)
3530 struct batadv_tt_req_node *node;
3531 struct hlist_node *safe;
3532 struct batadv_orig_node *orig_node = NULL;
3533 struct batadv_tvlv_tt_change *tt_change;
3534 u8 *tvlv_ptr = (u8 *)tt_data;
3535 u16 change_offset;
3537 batadv_dbg(BATADV_DBG_TT, bat_priv,
3538 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3539 resp_src, tt_data->ttvn, num_entries,
3540 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3542 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3543 if (!orig_node)
3544 goto out;
3546 spin_lock_bh(&orig_node->tt_lock);
3548 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3549 change_offset *= ntohs(tt_data->num_vlan);
3550 change_offset += sizeof(*tt_data);
3551 tvlv_ptr += change_offset;
3553 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3554 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3555 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3556 resp_src, num_entries);
3557 } else {
3558 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3559 tt_data->ttvn, tt_change);
3562 /* Recalculate the CRC for this orig_node and store it */
3563 batadv_tt_global_update_crc(bat_priv, orig_node);
3565 spin_unlock_bh(&orig_node->tt_lock);
3567 /* Delete the tt_req_node from pending tt_requests list */
3568 spin_lock_bh(&bat_priv->tt.req_list_lock);
3569 hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3570 if (!batadv_compare_eth(node->addr, resp_src))
3571 continue;
3572 hlist_del_init(&node->list);
3573 batadv_tt_req_node_put(node);
3576 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3577 out:
3578 if (orig_node)
3579 batadv_orig_node_put(orig_node);
3582 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3584 struct batadv_tt_roam_node *node, *safe;
3586 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3588 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3589 list_del(&node->list);
3590 kmem_cache_free(batadv_tt_roam_cache, node);
3593 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3596 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3598 struct batadv_tt_roam_node *node, *safe;
3600 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3601 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3602 if (!batadv_has_timed_out(node->first_time,
3603 BATADV_ROAMING_MAX_TIME))
3604 continue;
3606 list_del(&node->list);
3607 kmem_cache_free(batadv_tt_roam_cache, node);
3609 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3613 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3614 * @bat_priv: the bat priv with all the soft interface information
3615 * @client: mac address of the roaming client
3617 * This function checks whether the client already reached the
3618 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3619 * will not be sent.
3621 * Return: true if the ROAMING_ADV can be sent, false otherwise
3623 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3625 struct batadv_tt_roam_node *tt_roam_node;
3626 bool ret = false;
3628 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3629 /* The new tt_req will be issued only if I'm not waiting for a
3630 * reply from the same orig_node yet
3632 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3633 if (!batadv_compare_eth(tt_roam_node->addr, client))
3634 continue;
3636 if (batadv_has_timed_out(tt_roam_node->first_time,
3637 BATADV_ROAMING_MAX_TIME))
3638 continue;
3640 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3641 /* Sorry, you roamed too many times! */
3642 goto unlock;
3643 ret = true;
3644 break;
3647 if (!ret) {
3648 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3649 GFP_ATOMIC);
3650 if (!tt_roam_node)
3651 goto unlock;
3653 tt_roam_node->first_time = jiffies;
3654 atomic_set(&tt_roam_node->counter,
3655 BATADV_ROAMING_MAX_COUNT - 1);
3656 ether_addr_copy(tt_roam_node->addr, client);
3658 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3659 ret = true;
3662 unlock:
3663 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3664 return ret;
3668 * batadv_send_roam_adv - send a roaming advertisement message
3669 * @bat_priv: the bat priv with all the soft interface information
3670 * @client: mac address of the roaming client
3671 * @vid: VLAN identifier
3672 * @orig_node: message destination
3674 * Send a ROAMING_ADV message to the node which was previously serving this
3675 * client. This is done to inform the node that from now on all traffic destined
3676 * for this particular roamed client has to be forwarded to the sender of the
3677 * roaming message.
3679 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3680 unsigned short vid,
3681 struct batadv_orig_node *orig_node)
3683 struct batadv_hard_iface *primary_if;
3684 struct batadv_tvlv_roam_adv tvlv_roam;
3686 primary_if = batadv_primary_if_get_selected(bat_priv);
3687 if (!primary_if)
3688 goto out;
3690 /* before going on we have to check whether the client has
3691 * already roamed to us too many times
3693 if (!batadv_tt_check_roam_count(bat_priv, client))
3694 goto out;
3696 batadv_dbg(BATADV_DBG_TT, bat_priv,
3697 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3698 orig_node->orig, client, batadv_print_vid(vid));
3700 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3702 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3703 tvlv_roam.vid = htons(vid);
3705 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3706 orig_node->orig, BATADV_TVLV_ROAM, 1,
3707 &tvlv_roam, sizeof(tvlv_roam));
3709 out:
3710 if (primary_if)
3711 batadv_hardif_put(primary_if);
3714 static void batadv_tt_purge(struct work_struct *work)
3716 struct delayed_work *delayed_work;
3717 struct batadv_priv_tt *priv_tt;
3718 struct batadv_priv *bat_priv;
3720 delayed_work = to_delayed_work(work);
3721 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3722 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3724 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3725 batadv_tt_global_purge(bat_priv);
3726 batadv_tt_req_purge(bat_priv);
3727 batadv_tt_roam_purge(bat_priv);
3729 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3730 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3733 void batadv_tt_free(struct batadv_priv *bat_priv)
3735 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3736 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3738 cancel_delayed_work_sync(&bat_priv->tt.work);
3740 batadv_tt_local_table_free(bat_priv);
3741 batadv_tt_global_table_free(bat_priv);
3742 batadv_tt_req_list_free(bat_priv);
3743 batadv_tt_changes_list_free(bat_priv);
3744 batadv_tt_roam_list_free(bat_priv);
3746 kfree(bat_priv->tt.last_changeset);
3750 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3751 * table and possibly count them in the TT size
3752 * @bat_priv: the bat priv with all the soft interface information
3753 * @flags: the flag to switch
3754 * @enable: whether to set or unset the flag
3755 * @count: whether to increase the TT size by the number of changed entries
3757 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3758 bool enable, bool count)
3760 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3761 struct batadv_tt_common_entry *tt_common_entry;
3762 struct hlist_head *head;
3763 u32 i;
3765 if (!hash)
3766 return;
3768 for (i = 0; i < hash->size; i++) {
3769 head = &hash->table[i];
3771 rcu_read_lock();
3772 hlist_for_each_entry_rcu(tt_common_entry,
3773 head, hash_entry) {
3774 if (enable) {
3775 if ((tt_common_entry->flags & flags) == flags)
3776 continue;
3777 tt_common_entry->flags |= flags;
3778 } else {
3779 if (!(tt_common_entry->flags & flags))
3780 continue;
3781 tt_common_entry->flags &= ~flags;
3784 if (!count)
3785 continue;
3787 batadv_tt_local_size_inc(bat_priv,
3788 tt_common_entry->vid);
3790 rcu_read_unlock();
3794 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3795 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3797 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3798 struct batadv_tt_common_entry *tt_common;
3799 struct batadv_tt_local_entry *tt_local;
3800 struct hlist_node *node_tmp;
3801 struct hlist_head *head;
3802 spinlock_t *list_lock; /* protects write access to the hash lists */
3803 u32 i;
3805 if (!hash)
3806 return;
3808 for (i = 0; i < hash->size; i++) {
3809 head = &hash->table[i];
3810 list_lock = &hash->list_locks[i];
3812 spin_lock_bh(list_lock);
3813 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3814 hash_entry) {
3815 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3816 continue;
3818 batadv_dbg(BATADV_DBG_TT, bat_priv,
3819 "Deleting local tt entry (%pM, vid: %d): pending\n",
3820 tt_common->addr,
3821 batadv_print_vid(tt_common->vid));
3823 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3824 hlist_del_rcu(&tt_common->hash_entry);
3825 tt_local = container_of(tt_common,
3826 struct batadv_tt_local_entry,
3827 common);
3829 batadv_tt_local_entry_put(tt_local);
3831 spin_unlock_bh(list_lock);
3836 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3837 * which have been queued in the time since the last commit
3838 * @bat_priv: the bat priv with all the soft interface information
3840 * Caller must hold tt->commit_lock.
3842 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3844 lockdep_assert_held(&bat_priv->tt.commit_lock);
3846 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3847 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3848 batadv_tt_tvlv_container_update(bat_priv);
3849 return;
3852 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3854 batadv_tt_local_purge_pending_clients(bat_priv);
3855 batadv_tt_local_update_crc(bat_priv);
3857 /* Increment the TTVN only once per OGM interval */
3858 atomic_inc(&bat_priv->tt.vn);
3859 batadv_dbg(BATADV_DBG_TT, bat_priv,
3860 "Local changes committed, updating to ttvn %u\n",
3861 (u8)atomic_read(&bat_priv->tt.vn));
3863 /* reset the sending counter */
3864 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3865 batadv_tt_tvlv_container_update(bat_priv);
3869 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3870 * have been queued in the time since the last commit
3871 * @bat_priv: the bat priv with all the soft interface information
3873 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3875 spin_lock_bh(&bat_priv->tt.commit_lock);
3876 batadv_tt_local_commit_changes_nolock(bat_priv);
3877 spin_unlock_bh(&bat_priv->tt.commit_lock);
3880 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3881 unsigned short vid)
3883 struct batadv_tt_local_entry *tt_local_entry;
3884 struct batadv_tt_global_entry *tt_global_entry;
3885 struct batadv_softif_vlan *vlan;
3886 bool ret = false;
3888 vlan = batadv_softif_vlan_get(bat_priv, vid);
3889 if (!vlan)
3890 return false;
3892 if (!atomic_read(&vlan->ap_isolation))
3893 goto vlan_put;
3895 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3896 if (!tt_local_entry)
3897 goto vlan_put;
3899 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3900 if (!tt_global_entry)
3901 goto local_entry_put;
3903 if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3904 ret = true;
3906 batadv_tt_global_entry_put(tt_global_entry);
3907 local_entry_put:
3908 batadv_tt_local_entry_put(tt_local_entry);
3909 vlan_put:
3910 batadv_softif_vlan_put(vlan);
3911 return ret;
3915 * batadv_tt_update_orig - update global translation table with new tt
3916 * information received via ogms
3917 * @bat_priv: the bat priv with all the soft interface information
3918 * @orig_node: the orig_node of the ogm
3919 * @tt_buff: pointer to the first tvlv VLAN entry
3920 * @tt_num_vlan: number of tvlv VLAN entries
3921 * @tt_change: pointer to the first entry in the TT buffer
3922 * @tt_num_changes: number of tt changes inside the tt buffer
3923 * @ttvn: translation table version number of this changeset
3925 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3926 struct batadv_orig_node *orig_node,
3927 const void *tt_buff, u16 tt_num_vlan,
3928 struct batadv_tvlv_tt_change *tt_change,
3929 u16 tt_num_changes, u8 ttvn)
3931 u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3932 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3933 bool full_table = true;
3934 bool has_tt_init;
3936 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3937 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3938 &orig_node->capa_initialized);
3940 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3941 * increased by one -> we can apply the attached changes
3943 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3944 /* the OGM could not contain the changes due to their size or
3945 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3946 * times.
3947 * In this case send a tt request
3949 if (!tt_num_changes) {
3950 full_table = false;
3951 goto request_table;
3954 spin_lock_bh(&orig_node->tt_lock);
3956 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3957 ttvn, tt_change);
3959 /* Even if we received the precomputed crc with the OGM, we
3960 * prefer to recompute it to spot any possible inconsistency
3961 * in the global table
3963 batadv_tt_global_update_crc(bat_priv, orig_node);
3965 spin_unlock_bh(&orig_node->tt_lock);
3967 /* The ttvn alone is not enough to guarantee consistency
3968 * because a single value could represent different states
3969 * (due to the wrap around). Thus a node has to check whether
3970 * the resulting table (after applying the changes) is still
3971 * consistent or not. E.g. a node could disconnect while its
3972 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3973 * checking the CRC value is mandatory to detect the
3974 * inconsistency
3976 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3977 tt_num_vlan))
3978 goto request_table;
3979 } else {
3980 /* if we missed more than one change or our tables are not
3981 * in sync anymore -> request fresh tt data
3983 if (!has_tt_init || ttvn != orig_ttvn ||
3984 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3985 tt_num_vlan)) {
3986 request_table:
3987 batadv_dbg(BATADV_DBG_TT, bat_priv,
3988 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3989 orig_node->orig, ttvn, orig_ttvn,
3990 tt_num_changes);
3991 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3992 tt_vlan, tt_num_vlan,
3993 full_table);
3994 return;
4000 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
4001 * @bat_priv: the bat priv with all the soft interface information
4002 * @addr: the mac address of the client to check
4003 * @vid: VLAN identifier
4005 * Return: true if we know that the client has moved from its old originator
4006 * to another one. This entry is still kept for consistency purposes and will be
4007 * deleted later by a DEL or because of timeout
4009 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
4010 u8 *addr, unsigned short vid)
4012 struct batadv_tt_global_entry *tt_global_entry;
4013 bool ret = false;
4015 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
4016 if (!tt_global_entry)
4017 goto out;
4019 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
4020 batadv_tt_global_entry_put(tt_global_entry);
4021 out:
4022 return ret;
4026 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
4027 * @bat_priv: the bat priv with all the soft interface information
4028 * @addr: the mac address of the local client to query
4029 * @vid: VLAN identifier
4031 * Return: true if the local client is known to be roaming (it is not served by
4032 * this node anymore) or not. If yes, the client is still present in the table
4033 * to keep the latter consistent with the node TTVN
4035 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
4036 u8 *addr, unsigned short vid)
4038 struct batadv_tt_local_entry *tt_local_entry;
4039 bool ret = false;
4041 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
4042 if (!tt_local_entry)
4043 goto out;
4045 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
4046 batadv_tt_local_entry_put(tt_local_entry);
4047 out:
4048 return ret;
4051 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
4052 struct batadv_orig_node *orig_node,
4053 const unsigned char *addr,
4054 unsigned short vid)
4056 /* ignore loop detect macs, they are not supposed to be in the tt local
4057 * data as well.
4059 if (batadv_bla_is_loopdetect_mac(addr))
4060 return false;
4062 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
4063 BATADV_TT_CLIENT_TEMP,
4064 atomic_read(&orig_node->last_ttvn)))
4065 return false;
4067 batadv_dbg(BATADV_DBG_TT, bat_priv,
4068 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
4069 addr, batadv_print_vid(vid), orig_node->orig);
4071 return true;
4075 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
4076 * maximum packet size that can be transported through the mesh
4077 * @soft_iface: netdev struct of the mesh interface
4079 * Remove entries older than 'timeout' and half timeout if more entries need
4080 * to be removed.
4082 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
4084 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
4085 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
4086 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
4087 bool reduced = false;
4089 spin_lock_bh(&bat_priv->tt.commit_lock);
4091 while (true) {
4092 table_size = batadv_tt_local_table_transmit_size(bat_priv);
4093 if (packet_size_max >= table_size)
4094 break;
4096 batadv_tt_local_purge(bat_priv, timeout);
4097 batadv_tt_local_purge_pending_clients(bat_priv);
4099 timeout /= 2;
4100 reduced = true;
4101 net_ratelimited_function(batadv_info, soft_iface,
4102 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
4103 packet_size_max);
4106 /* commit these changes immediately, to avoid synchronization problem
4107 * with the TTVN
4109 if (reduced)
4110 batadv_tt_local_commit_changes_nolock(bat_priv);
4112 spin_unlock_bh(&bat_priv->tt.commit_lock);
4116 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
4117 * @bat_priv: the bat priv with all the soft interface information
4118 * @orig: the orig_node of the ogm
4119 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4120 * @tvlv_value: tvlv buffer containing the gateway data
4121 * @tvlv_value_len: tvlv buffer length
4123 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
4124 struct batadv_orig_node *orig,
4125 u8 flags, void *tvlv_value,
4126 u16 tvlv_value_len)
4128 struct batadv_tvlv_tt_vlan_data *tt_vlan;
4129 struct batadv_tvlv_tt_change *tt_change;
4130 struct batadv_tvlv_tt_data *tt_data;
4131 u16 num_entries, num_vlan;
4133 if (tvlv_value_len < sizeof(*tt_data))
4134 return;
4136 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4137 tvlv_value_len -= sizeof(*tt_data);
4139 num_vlan = ntohs(tt_data->num_vlan);
4141 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4142 return;
4144 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4145 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4146 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4148 num_entries = batadv_tt_entries(tvlv_value_len);
4150 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4151 num_entries, tt_data->ttvn);
4155 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
4156 * container
4157 * @bat_priv: the bat priv with all the soft interface information
4158 * @src: mac address of tt tvlv sender
4159 * @dst: mac address of tt tvlv recipient
4160 * @tvlv_value: tvlv buffer containing the tt data
4161 * @tvlv_value_len: tvlv buffer length
4163 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
4164 * otherwise.
4166 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4167 u8 *src, u8 *dst,
4168 void *tvlv_value,
4169 u16 tvlv_value_len)
4171 struct batadv_tvlv_tt_data *tt_data;
4172 u16 tt_vlan_len, tt_num_entries;
4173 char tt_flag;
4174 bool ret;
4176 if (tvlv_value_len < sizeof(*tt_data))
4177 return NET_RX_SUCCESS;
4179 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4180 tvlv_value_len -= sizeof(*tt_data);
4182 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4183 tt_vlan_len *= ntohs(tt_data->num_vlan);
4185 if (tvlv_value_len < tt_vlan_len)
4186 return NET_RX_SUCCESS;
4188 tvlv_value_len -= tt_vlan_len;
4189 tt_num_entries = batadv_tt_entries(tvlv_value_len);
4191 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4192 case BATADV_TT_REQUEST:
4193 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4195 /* If this node cannot provide a TT response the tt_request is
4196 * forwarded
4198 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4199 if (!ret) {
4200 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4201 tt_flag = 'F';
4202 else
4203 tt_flag = '.';
4205 batadv_dbg(BATADV_DBG_TT, bat_priv,
4206 "Routing TT_REQUEST to %pM [%c]\n",
4207 dst, tt_flag);
4208 /* tvlv API will re-route the packet */
4209 return NET_RX_DROP;
4211 break;
4212 case BATADV_TT_RESPONSE:
4213 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4215 if (batadv_is_my_mac(bat_priv, dst)) {
4216 batadv_handle_tt_response(bat_priv, tt_data,
4217 src, tt_num_entries);
4218 return NET_RX_SUCCESS;
4221 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4222 tt_flag = 'F';
4223 else
4224 tt_flag = '.';
4226 batadv_dbg(BATADV_DBG_TT, bat_priv,
4227 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4229 /* tvlv API will re-route the packet */
4230 return NET_RX_DROP;
4233 return NET_RX_SUCCESS;
4237 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
4238 * @bat_priv: the bat priv with all the soft interface information
4239 * @src: mac address of tt tvlv sender
4240 * @dst: mac address of tt tvlv recipient
4241 * @tvlv_value: tvlv buffer containing the tt data
4242 * @tvlv_value_len: tvlv buffer length
4244 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
4245 * otherwise.
4247 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4248 u8 *src, u8 *dst,
4249 void *tvlv_value,
4250 u16 tvlv_value_len)
4252 struct batadv_tvlv_roam_adv *roaming_adv;
4253 struct batadv_orig_node *orig_node = NULL;
4255 /* If this node is not the intended recipient of the
4256 * roaming advertisement the packet is forwarded
4257 * (the tvlv API will re-route the packet).
4259 if (!batadv_is_my_mac(bat_priv, dst))
4260 return NET_RX_DROP;
4262 if (tvlv_value_len < sizeof(*roaming_adv))
4263 goto out;
4265 orig_node = batadv_orig_hash_find(bat_priv, src);
4266 if (!orig_node)
4267 goto out;
4269 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4270 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
4272 batadv_dbg(BATADV_DBG_TT, bat_priv,
4273 "Received ROAMING_ADV from %pM (client %pM)\n",
4274 src, roaming_adv->client);
4276 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
4277 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
4278 atomic_read(&orig_node->last_ttvn) + 1);
4280 out:
4281 if (orig_node)
4282 batadv_orig_node_put(orig_node);
4283 return NET_RX_SUCCESS;
4287 * batadv_tt_init - initialise the translation table internals
4288 * @bat_priv: the bat priv with all the soft interface information
4290 * Return: 0 on success or negative error number in case of failure.
4292 int batadv_tt_init(struct batadv_priv *bat_priv)
4294 int ret;
4296 /* synchronized flags must be remote */
4297 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4299 ret = batadv_tt_local_init(bat_priv);
4300 if (ret < 0)
4301 return ret;
4303 ret = batadv_tt_global_init(bat_priv);
4304 if (ret < 0)
4305 return ret;
4307 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
4308 batadv_tt_tvlv_unicast_handler_v1,
4309 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
4311 batadv_tvlv_handler_register(bat_priv, NULL,
4312 batadv_roam_tvlv_unicast_handler_v1,
4313 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4315 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4316 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4317 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4319 return 1;
4323 * batadv_tt_global_is_isolated - check if a client is marked as isolated
4324 * @bat_priv: the bat priv with all the soft interface information
4325 * @addr: the mac address of the client
4326 * @vid: the identifier of the VLAN where this client is connected
4328 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
4329 * otherwise
4331 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
4332 const u8 *addr, unsigned short vid)
4334 struct batadv_tt_global_entry *tt;
4335 bool ret;
4337 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4338 if (!tt)
4339 return false;
4341 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4343 batadv_tt_global_entry_put(tt);
4345 return ret;
4349 * batadv_tt_cache_init - Initialize tt memory object cache
4351 * Return: 0 on success or negative error number in case of failure.
4353 int __init batadv_tt_cache_init(void)
4355 size_t tl_size = sizeof(struct batadv_tt_local_entry);
4356 size_t tg_size = sizeof(struct batadv_tt_global_entry);
4357 size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4358 size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4359 size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4360 size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4362 batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4363 SLAB_HWCACHE_ALIGN, NULL);
4364 if (!batadv_tl_cache)
4365 return -ENOMEM;
4367 batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4368 SLAB_HWCACHE_ALIGN, NULL);
4369 if (!batadv_tg_cache)
4370 goto err_tt_tl_destroy;
4372 batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4373 tt_orig_size, 0,
4374 SLAB_HWCACHE_ALIGN, NULL);
4375 if (!batadv_tt_orig_cache)
4376 goto err_tt_tg_destroy;
4378 batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4379 tt_change_size, 0,
4380 SLAB_HWCACHE_ALIGN, NULL);
4381 if (!batadv_tt_change_cache)
4382 goto err_tt_orig_destroy;
4384 batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4385 tt_req_size, 0,
4386 SLAB_HWCACHE_ALIGN, NULL);
4387 if (!batadv_tt_req_cache)
4388 goto err_tt_change_destroy;
4390 batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4391 tt_roam_size, 0,
4392 SLAB_HWCACHE_ALIGN, NULL);
4393 if (!batadv_tt_roam_cache)
4394 goto err_tt_req_destroy;
4396 return 0;
4398 err_tt_req_destroy:
4399 kmem_cache_destroy(batadv_tt_req_cache);
4400 batadv_tt_req_cache = NULL;
4401 err_tt_change_destroy:
4402 kmem_cache_destroy(batadv_tt_change_cache);
4403 batadv_tt_change_cache = NULL;
4404 err_tt_orig_destroy:
4405 kmem_cache_destroy(batadv_tt_orig_cache);
4406 batadv_tt_orig_cache = NULL;
4407 err_tt_tg_destroy:
4408 kmem_cache_destroy(batadv_tg_cache);
4409 batadv_tg_cache = NULL;
4410 err_tt_tl_destroy:
4411 kmem_cache_destroy(batadv_tl_cache);
4412 batadv_tl_cache = NULL;
4414 return -ENOMEM;
4418 * batadv_tt_cache_destroy - Destroy tt memory object cache
4420 void batadv_tt_cache_destroy(void)
4422 kmem_cache_destroy(batadv_tl_cache);
4423 kmem_cache_destroy(batadv_tg_cache);
4424 kmem_cache_destroy(batadv_tt_orig_cache);
4425 kmem_cache_destroy(batadv_tt_change_cache);
4426 kmem_cache_destroy(batadv_tt_req_cache);
4427 kmem_cache_destroy(batadv_tt_roam_cache);