Linux 4.1.18
[linux/fpc-iii.git] / net / batman-adv / translation-table.c
blobddd62c9af5b4ce61c453c529116a25ef11bf6f31
1 /* Copyright (C) 2007-2014 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 <linux/bitops.h>
19 #include "main.h"
20 #include "translation-table.h"
21 #include "soft-interface.h"
22 #include "hard-interface.h"
23 #include "send.h"
24 #include "hash.h"
25 #include "originator.h"
26 #include "routing.h"
27 #include "bridge_loop_avoidance.h"
28 #include "multicast.h"
30 #include <linux/crc32c.h>
32 /* hash class keys */
33 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
34 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
36 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
37 unsigned short vid,
38 struct batadv_orig_node *orig_node);
39 static void batadv_tt_purge(struct work_struct *work);
40 static void
41 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
42 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
43 struct batadv_orig_node *orig_node,
44 const unsigned char *addr,
45 unsigned short vid, const char *message,
46 bool roaming);
48 /* returns 1 if they are the same mac addr */
49 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
51 const void *data1 = container_of(node, struct batadv_tt_common_entry,
52 hash_entry);
54 return batadv_compare_eth(data1, data2);
57 /**
58 * batadv_choose_tt - return the index of the tt entry in the hash table
59 * @data: pointer to the tt_common_entry object to map
60 * @size: the size of the hash table
62 * Returns the hash index where the object represented by 'data' should be
63 * stored at.
65 static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
67 struct batadv_tt_common_entry *tt;
68 uint32_t hash = 0;
70 tt = (struct batadv_tt_common_entry *)data;
71 hash = batadv_hash_bytes(hash, &tt->addr, ETH_ALEN);
72 hash = batadv_hash_bytes(hash, &tt->vid, sizeof(tt->vid));
74 hash += (hash << 3);
75 hash ^= (hash >> 11);
76 hash += (hash << 15);
78 return hash % size;
81 /**
82 * batadv_tt_hash_find - look for a client in the given hash table
83 * @hash: the hash table to search
84 * @addr: the mac address of the client to look for
85 * @vid: VLAN identifier
87 * Returns a pointer to the tt_common struct belonging to the searched client if
88 * found, NULL otherwise.
90 static struct batadv_tt_common_entry *
91 batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
92 unsigned short vid)
94 struct hlist_head *head;
95 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
96 uint32_t index;
98 if (!hash)
99 return NULL;
101 ether_addr_copy(to_search.addr, addr);
102 to_search.vid = vid;
104 index = batadv_choose_tt(&to_search, hash->size);
105 head = &hash->table[index];
107 rcu_read_lock();
108 hlist_for_each_entry_rcu(tt, head, hash_entry) {
109 if (!batadv_compare_eth(tt, addr))
110 continue;
112 if (tt->vid != vid)
113 continue;
115 if (!atomic_inc_not_zero(&tt->refcount))
116 continue;
118 tt_tmp = tt;
119 break;
121 rcu_read_unlock();
123 return tt_tmp;
127 * batadv_tt_local_hash_find - search the local table for a given client
128 * @bat_priv: the bat priv with all the soft interface information
129 * @addr: the mac address of the client to look for
130 * @vid: VLAN identifier
132 * Returns a pointer to the corresponding tt_local_entry struct if the client is
133 * found, NULL otherwise.
135 static struct batadv_tt_local_entry *
136 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
137 unsigned short vid)
139 struct batadv_tt_common_entry *tt_common_entry;
140 struct batadv_tt_local_entry *tt_local_entry = NULL;
142 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
143 vid);
144 if (tt_common_entry)
145 tt_local_entry = container_of(tt_common_entry,
146 struct batadv_tt_local_entry,
147 common);
148 return tt_local_entry;
152 * batadv_tt_global_hash_find - search the global table for a given client
153 * @bat_priv: the bat priv with all the soft interface information
154 * @addr: the mac address of the client to look for
155 * @vid: VLAN identifier
157 * Returns a pointer to the corresponding tt_global_entry struct if the client
158 * is found, NULL otherwise.
160 static struct batadv_tt_global_entry *
161 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
162 unsigned short vid)
164 struct batadv_tt_common_entry *tt_common_entry;
165 struct batadv_tt_global_entry *tt_global_entry = NULL;
167 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
168 vid);
169 if (tt_common_entry)
170 tt_global_entry = container_of(tt_common_entry,
171 struct batadv_tt_global_entry,
172 common);
173 return tt_global_entry;
176 static void
177 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
179 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
180 kfree_rcu(tt_local_entry, common.rcu);
184 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
185 * tt_global_entry and possibly free it
186 * @tt_global_entry: the object to free
188 static void
189 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
191 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
192 batadv_tt_global_del_orig_list(tt_global_entry);
193 kfree_rcu(tt_global_entry, common.rcu);
198 * batadv_tt_global_hash_count - count the number of orig entries
199 * @hash: hash table containing the tt entries
200 * @addr: the mac address of the client to count entries for
201 * @vid: VLAN identifier
203 * Return the number of originators advertising the given address/data
204 * (excluding ourself).
206 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
207 const uint8_t *addr, unsigned short vid)
209 struct batadv_tt_global_entry *tt_global_entry;
210 int count;
212 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
213 if (!tt_global_entry)
214 return 0;
216 count = atomic_read(&tt_global_entry->orig_list_count);
217 batadv_tt_global_entry_free_ref(tt_global_entry);
219 return count;
223 * batadv_tt_local_size_mod - change the size by v of the local table identified
224 * by vid
225 * @bat_priv: the bat priv with all the soft interface information
226 * @vid: the VLAN identifier of the sub-table to change
227 * @v: the amount to sum to the local table size
229 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
230 unsigned short vid, int v)
232 struct batadv_softif_vlan *vlan;
234 vlan = batadv_softif_vlan_get(bat_priv, vid);
235 if (!vlan)
236 return;
238 atomic_add(v, &vlan->tt.num_entries);
240 batadv_softif_vlan_free_ref(vlan);
244 * batadv_tt_local_size_inc - increase by one the local table size for the given
245 * vid
246 * @bat_priv: the bat priv with all the soft interface information
247 * @vid: the VLAN identifier
249 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
250 unsigned short vid)
252 batadv_tt_local_size_mod(bat_priv, vid, 1);
256 * batadv_tt_local_size_dec - decrease by one the local table size for the given
257 * vid
258 * @bat_priv: the bat priv with all the soft interface information
259 * @vid: the VLAN identifier
261 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
262 unsigned short vid)
264 batadv_tt_local_size_mod(bat_priv, vid, -1);
268 * batadv_tt_global_size_mod - change the size by v of the local table
269 * identified by vid
270 * @bat_priv: the bat priv with all the soft interface information
271 * @vid: the VLAN identifier
272 * @v: the amount to sum to the global table size
274 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
275 unsigned short vid, int v)
277 struct batadv_orig_node_vlan *vlan;
279 vlan = batadv_orig_node_vlan_new(orig_node, vid);
280 if (!vlan)
281 return;
283 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
284 spin_lock_bh(&orig_node->vlan_list_lock);
285 list_del_rcu(&vlan->list);
286 spin_unlock_bh(&orig_node->vlan_list_lock);
287 batadv_orig_node_vlan_free_ref(vlan);
290 batadv_orig_node_vlan_free_ref(vlan);
294 * batadv_tt_global_size_inc - increase by one the global table size for the
295 * given vid
296 * @orig_node: the originator which global table size has to be decreased
297 * @vid: the vlan identifier
299 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
300 unsigned short vid)
302 batadv_tt_global_size_mod(orig_node, vid, 1);
306 * batadv_tt_global_size_dec - decrease by one the global table size for the
307 * given vid
308 * @orig_node: the originator which global table size has to be decreased
309 * @vid: the vlan identifier
311 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
312 unsigned short vid)
314 batadv_tt_global_size_mod(orig_node, vid, -1);
318 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
319 * queue for free after rcu grace period
320 * @orig_entry: tt orig entry to be free'd
322 static void
323 batadv_tt_orig_list_entry_release(struct batadv_tt_orig_list_entry *orig_entry)
325 batadv_orig_node_free_ref(orig_entry->orig_node);
326 kfree_rcu(orig_entry, rcu);
329 static void
330 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
332 if (!atomic_dec_and_test(&orig_entry->refcount))
333 return;
335 batadv_tt_orig_list_entry_release(orig_entry);
339 * batadv_tt_local_event - store a local TT event (ADD/DEL)
340 * @bat_priv: the bat priv with all the soft interface information
341 * @tt_local_entry: the TT entry involved in the event
342 * @event_flags: flags to store in the event structure
344 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
345 struct batadv_tt_local_entry *tt_local_entry,
346 uint8_t event_flags)
348 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
349 struct batadv_tt_common_entry *common = &tt_local_entry->common;
350 uint8_t flags = common->flags | event_flags;
351 bool event_removed = false;
352 bool del_op_requested, del_op_entry;
354 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
355 if (!tt_change_node)
356 return;
358 tt_change_node->change.flags = flags;
359 memset(tt_change_node->change.reserved, 0,
360 sizeof(tt_change_node->change.reserved));
361 ether_addr_copy(tt_change_node->change.addr, common->addr);
362 tt_change_node->change.vid = htons(common->vid);
364 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
366 /* check for ADD+DEL or DEL+ADD events */
367 spin_lock_bh(&bat_priv->tt.changes_list_lock);
368 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
369 list) {
370 if (!batadv_compare_eth(entry->change.addr, common->addr))
371 continue;
373 /* DEL+ADD in the same orig interval have no effect and can be
374 * removed to avoid silly behaviour on the receiver side. The
375 * other way around (ADD+DEL) can happen in case of roaming of
376 * a client still in the NEW state. Roaming of NEW clients is
377 * now possible due to automatically recognition of "temporary"
378 * clients
380 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
381 if (!del_op_requested && del_op_entry)
382 goto del;
383 if (del_op_requested && !del_op_entry)
384 goto del;
386 /* this is a second add in the same originator interval. It
387 * means that flags have been changed: update them!
389 if (!del_op_requested && !del_op_entry)
390 entry->change.flags = flags;
392 continue;
393 del:
394 list_del(&entry->list);
395 kfree(entry);
396 kfree(tt_change_node);
397 event_removed = true;
398 goto unlock;
401 /* track the change in the OGMinterval list */
402 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
404 unlock:
405 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
407 if (event_removed)
408 atomic_dec(&bat_priv->tt.local_changes);
409 else
410 atomic_inc(&bat_priv->tt.local_changes);
414 * batadv_tt_len - compute length in bytes of given number of tt changes
415 * @changes_num: number of tt changes
417 * Returns computed length in bytes.
419 static int batadv_tt_len(int changes_num)
421 return changes_num * sizeof(struct batadv_tvlv_tt_change);
425 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
426 * @tt_len: available space
428 * Returns the number of entries.
430 static uint16_t batadv_tt_entries(uint16_t tt_len)
432 return tt_len / batadv_tt_len(1);
436 * batadv_tt_local_table_transmit_size - calculates the local translation table
437 * size when transmitted over the air
438 * @bat_priv: the bat priv with all the soft interface information
440 * Returns local translation table size in bytes.
442 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
444 uint16_t num_vlan = 0, tt_local_entries = 0;
445 struct batadv_softif_vlan *vlan;
446 int hdr_size;
448 rcu_read_lock();
449 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
450 num_vlan++;
451 tt_local_entries += atomic_read(&vlan->tt.num_entries);
453 rcu_read_unlock();
455 /* header size of tvlv encapsulated tt response payload */
456 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
457 hdr_size += sizeof(struct batadv_tvlv_hdr);
458 hdr_size += sizeof(struct batadv_tvlv_tt_data);
459 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
461 return hdr_size + batadv_tt_len(tt_local_entries);
464 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
466 if (bat_priv->tt.local_hash)
467 return 0;
469 bat_priv->tt.local_hash = batadv_hash_new(1024);
471 if (!bat_priv->tt.local_hash)
472 return -ENOMEM;
474 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
475 &batadv_tt_local_hash_lock_class_key);
477 return 0;
480 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
481 struct batadv_tt_global_entry *tt_global,
482 const char *message)
484 batadv_dbg(BATADV_DBG_TT, bat_priv,
485 "Deleting global tt entry %pM (vid: %d): %s\n",
486 tt_global->common.addr,
487 BATADV_PRINT_VID(tt_global->common.vid), message);
489 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
490 batadv_choose_tt, &tt_global->common);
491 batadv_tt_global_entry_free_ref(tt_global);
495 * batadv_tt_local_add - add a new client to the local table or update an
496 * existing client
497 * @soft_iface: netdev struct of the mesh interface
498 * @addr: the mac address of the client to add
499 * @vid: VLAN identifier
500 * @ifindex: index of the interface where the client is connected to (useful to
501 * identify wireless clients)
502 * @mark: the value contained in the skb->mark field of the received packet (if
503 * any)
505 * Returns true if the client was successfully added, false otherwise.
507 bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
508 unsigned short vid, int ifindex, uint32_t mark)
510 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
511 struct batadv_tt_local_entry *tt_local;
512 struct batadv_tt_global_entry *tt_global = NULL;
513 struct batadv_softif_vlan *vlan;
514 struct net_device *in_dev = NULL;
515 struct hlist_head *head;
516 struct batadv_tt_orig_list_entry *orig_entry;
517 int hash_added, table_size, packet_size_max;
518 bool ret = false, roamed_back = false;
519 uint8_t remote_flags;
520 uint32_t match_mark;
522 if (ifindex != BATADV_NULL_IFINDEX)
523 in_dev = dev_get_by_index(&init_net, ifindex);
525 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
527 if (!is_multicast_ether_addr(addr))
528 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
530 if (tt_local) {
531 tt_local->last_seen = jiffies;
532 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
533 batadv_dbg(BATADV_DBG_TT, bat_priv,
534 "Re-adding pending client %pM (vid: %d)\n",
535 addr, BATADV_PRINT_VID(vid));
536 /* whatever the reason why the PENDING flag was set,
537 * this is a client which was enqueued to be removed in
538 * this orig_interval. Since it popped up again, the
539 * flag can be reset like it was never enqueued
541 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
542 goto add_event;
545 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
546 batadv_dbg(BATADV_DBG_TT, bat_priv,
547 "Roaming client %pM (vid: %d) came back to its original location\n",
548 addr, BATADV_PRINT_VID(vid));
549 /* the ROAM flag is set because this client roamed away
550 * and the node got a roaming_advertisement message. Now
551 * that the client popped up again at its original
552 * location such flag can be unset
554 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
555 roamed_back = true;
557 goto check_roaming;
560 /* Ignore the client if we cannot send it in a full table response. */
561 table_size = batadv_tt_local_table_transmit_size(bat_priv);
562 table_size += batadv_tt_len(1);
563 packet_size_max = atomic_read(&bat_priv->packet_size_max);
564 if (table_size > packet_size_max) {
565 net_ratelimited_function(batadv_info, soft_iface,
566 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
567 table_size, packet_size_max, addr);
568 goto out;
571 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
572 if (!tt_local)
573 goto out;
575 /* increase the refcounter of the related vlan */
576 vlan = batadv_softif_vlan_get(bat_priv, vid);
577 if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
578 addr, BATADV_PRINT_VID(vid)))
579 goto out;
581 batadv_dbg(BATADV_DBG_TT, bat_priv,
582 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
583 addr, BATADV_PRINT_VID(vid),
584 (uint8_t)atomic_read(&bat_priv->tt.vn));
586 ether_addr_copy(tt_local->common.addr, addr);
587 /* The local entry has to be marked as NEW to avoid to send it in
588 * a full table response going out before the next ttvn increment
589 * (consistency check)
591 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
592 tt_local->common.vid = vid;
593 if (batadv_is_wifi_netdev(in_dev))
594 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
595 atomic_set(&tt_local->common.refcount, 2);
596 tt_local->last_seen = jiffies;
597 tt_local->common.added_at = tt_local->last_seen;
599 /* the batman interface mac and multicast addresses should never be
600 * purged
602 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
603 is_multicast_ether_addr(addr))
604 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
606 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
607 batadv_choose_tt, &tt_local->common,
608 &tt_local->common.hash_entry);
610 if (unlikely(hash_added != 0)) {
611 /* remove the reference for the hash */
612 batadv_tt_local_entry_free_ref(tt_local);
613 batadv_softif_vlan_free_ref(vlan);
614 goto out;
617 add_event:
618 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
620 check_roaming:
621 /* Check whether it is a roaming, but don't do anything if the roaming
622 * process has already been handled
624 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
625 /* These node are probably going to update their tt table */
626 head = &tt_global->orig_list;
627 rcu_read_lock();
628 hlist_for_each_entry_rcu(orig_entry, head, list) {
629 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
630 tt_global->common.vid,
631 orig_entry->orig_node);
633 rcu_read_unlock();
634 if (roamed_back) {
635 batadv_tt_global_free(bat_priv, tt_global,
636 "Roaming canceled");
637 tt_global = NULL;
638 } else {
639 /* The global entry has to be marked as ROAMING and
640 * has to be kept for consistency purpose
642 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
643 tt_global->roam_at = jiffies;
647 /* store the current remote flags before altering them. This helps
648 * understanding is flags are changing or not
650 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
652 if (batadv_is_wifi_netdev(in_dev))
653 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
654 else
655 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
657 /* check the mark in the skb: if it's equal to the configured
658 * isolation_mark, it means the packet is coming from an isolated
659 * non-mesh client
661 match_mark = (mark & bat_priv->isolation_mark_mask);
662 if (bat_priv->isolation_mark_mask &&
663 match_mark == bat_priv->isolation_mark)
664 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
665 else
666 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
668 /* if any "dynamic" flag has been modified, resend an ADD event for this
669 * entry so that all the nodes can get the new flags
671 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
672 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
674 ret = true;
675 out:
676 if (in_dev)
677 dev_put(in_dev);
678 if (tt_local)
679 batadv_tt_local_entry_free_ref(tt_local);
680 if (tt_global)
681 batadv_tt_global_entry_free_ref(tt_global);
682 return ret;
686 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
687 * within a TT Response directed to another node
688 * @orig_node: originator for which the TT data has to be prepared
689 * @tt_data: uninitialised pointer to the address of the TVLV buffer
690 * @tt_change: uninitialised pointer to the address of the area where the TT
691 * changed can be stored
692 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
693 * function reserves the amount of space needed to send the entire global TT
694 * table. In case of success the value is updated with the real amount of
695 * reserved bytes
697 * Allocate the needed amount of memory for the entire TT TVLV and write its
698 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
699 * objects, one per active VLAN served by the originator node.
701 * Return the size of the allocated buffer or 0 in case of failure.
703 static uint16_t
704 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
705 struct batadv_tvlv_tt_data **tt_data,
706 struct batadv_tvlv_tt_change **tt_change,
707 int32_t *tt_len)
709 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
710 struct batadv_tvlv_tt_vlan_data *tt_vlan;
711 struct batadv_orig_node_vlan *vlan;
712 uint8_t *tt_change_ptr;
714 rcu_read_lock();
715 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
716 num_vlan++;
717 num_entries += atomic_read(&vlan->tt.num_entries);
720 change_offset = sizeof(**tt_data);
721 change_offset += num_vlan * sizeof(*tt_vlan);
723 /* if tt_len is negative, allocate the space needed by the full table */
724 if (*tt_len < 0)
725 *tt_len = batadv_tt_len(num_entries);
727 tvlv_len = *tt_len;
728 tvlv_len += change_offset;
730 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
731 if (!*tt_data) {
732 *tt_len = 0;
733 goto out;
736 (*tt_data)->flags = BATADV_NO_FLAGS;
737 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
738 (*tt_data)->num_vlan = htons(num_vlan);
740 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
741 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
742 tt_vlan->vid = htons(vlan->vid);
743 tt_vlan->crc = htonl(vlan->tt.crc);
745 tt_vlan++;
748 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
749 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
751 out:
752 rcu_read_unlock();
753 return tvlv_len;
757 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
758 * node
759 * @bat_priv: the bat priv with all the soft interface information
760 * @tt_data: uninitialised pointer to the address of the TVLV buffer
761 * @tt_change: uninitialised pointer to the address of the area where the TT
762 * changes can be stored
763 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
764 * function reserves the amount of space needed to send the entire local TT
765 * table. In case of success the value is updated with the real amount of
766 * reserved bytes
768 * Allocate the needed amount of memory for the entire TT TVLV and write its
769 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
770 * objects, one per active VLAN.
772 * Return the size of the allocated buffer or 0 in case of failure.
774 static uint16_t
775 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
776 struct batadv_tvlv_tt_data **tt_data,
777 struct batadv_tvlv_tt_change **tt_change,
778 int32_t *tt_len)
780 struct batadv_tvlv_tt_vlan_data *tt_vlan;
781 struct batadv_softif_vlan *vlan;
782 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
783 uint8_t *tt_change_ptr;
784 int change_offset;
786 rcu_read_lock();
787 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
788 num_vlan++;
789 num_entries += atomic_read(&vlan->tt.num_entries);
792 change_offset = sizeof(**tt_data);
793 change_offset += num_vlan * sizeof(*tt_vlan);
795 /* if tt_len is negative, allocate the space needed by the full table */
796 if (*tt_len < 0)
797 *tt_len = batadv_tt_len(num_entries);
799 tvlv_len = *tt_len;
800 tvlv_len += change_offset;
802 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
803 if (!*tt_data) {
804 tvlv_len = 0;
805 goto out;
808 (*tt_data)->flags = BATADV_NO_FLAGS;
809 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
810 (*tt_data)->num_vlan = htons(num_vlan);
812 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
813 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
814 tt_vlan->vid = htons(vlan->vid);
815 tt_vlan->crc = htonl(vlan->tt.crc);
817 tt_vlan++;
820 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
821 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
823 out:
824 rcu_read_unlock();
825 return tvlv_len;
829 * batadv_tt_tvlv_container_update - update the translation table tvlv container
830 * after local tt changes have been committed
831 * @bat_priv: the bat priv with all the soft interface information
833 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
835 struct batadv_tt_change_node *entry, *safe;
836 struct batadv_tvlv_tt_data *tt_data;
837 struct batadv_tvlv_tt_change *tt_change;
838 int tt_diff_len, tt_change_len = 0;
839 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
840 uint16_t tvlv_len;
842 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
843 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
845 /* if we have too many changes for one packet don't send any
846 * and wait for the tt table request which will be fragmented
848 if (tt_diff_len > bat_priv->soft_iface->mtu)
849 tt_diff_len = 0;
851 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
852 &tt_change, &tt_diff_len);
853 if (!tvlv_len)
854 return;
856 tt_data->flags = BATADV_TT_OGM_DIFF;
858 if (tt_diff_len == 0)
859 goto container_register;
861 spin_lock_bh(&bat_priv->tt.changes_list_lock);
862 atomic_set(&bat_priv->tt.local_changes, 0);
864 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
865 list) {
866 if (tt_diff_entries_count < tt_diff_entries_num) {
867 memcpy(tt_change + tt_diff_entries_count,
868 &entry->change,
869 sizeof(struct batadv_tvlv_tt_change));
870 tt_diff_entries_count++;
872 list_del(&entry->list);
873 kfree(entry);
875 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
877 /* Keep the buffer for possible tt_request */
878 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
879 kfree(bat_priv->tt.last_changeset);
880 bat_priv->tt.last_changeset_len = 0;
881 bat_priv->tt.last_changeset = NULL;
882 tt_change_len = batadv_tt_len(tt_diff_entries_count);
883 /* check whether this new OGM has no changes due to size problems */
884 if (tt_diff_entries_count > 0) {
885 /* if kmalloc() fails we will reply with the full table
886 * instead of providing the diff
888 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
889 if (bat_priv->tt.last_changeset) {
890 memcpy(bat_priv->tt.last_changeset,
891 tt_change, tt_change_len);
892 bat_priv->tt.last_changeset_len = tt_diff_len;
895 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
897 container_register:
898 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
899 tvlv_len);
900 kfree(tt_data);
903 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
905 struct net_device *net_dev = (struct net_device *)seq->private;
906 struct batadv_priv *bat_priv = netdev_priv(net_dev);
907 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
908 struct batadv_tt_common_entry *tt_common_entry;
909 struct batadv_tt_local_entry *tt_local;
910 struct batadv_hard_iface *primary_if;
911 struct batadv_softif_vlan *vlan;
912 struct hlist_head *head;
913 unsigned short vid;
914 uint32_t i;
915 int last_seen_secs;
916 int last_seen_msecs;
917 unsigned long last_seen_jiffies;
918 bool no_purge;
919 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
921 primary_if = batadv_seq_print_text_primary_if_get(seq);
922 if (!primary_if)
923 goto out;
925 seq_printf(seq,
926 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
927 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
928 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
929 "Flags", "Last seen", "CRC");
931 for (i = 0; i < hash->size; i++) {
932 head = &hash->table[i];
934 rcu_read_lock();
935 hlist_for_each_entry_rcu(tt_common_entry,
936 head, hash_entry) {
937 tt_local = container_of(tt_common_entry,
938 struct batadv_tt_local_entry,
939 common);
940 vid = tt_common_entry->vid;
941 last_seen_jiffies = jiffies - tt_local->last_seen;
942 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
943 last_seen_secs = last_seen_msecs / 1000;
944 last_seen_msecs = last_seen_msecs % 1000;
946 no_purge = tt_common_entry->flags & np_flag;
948 vlan = batadv_softif_vlan_get(bat_priv, vid);
949 if (!vlan) {
950 seq_printf(seq, "Cannot retrieve VLAN %d\n",
951 BATADV_PRINT_VID(vid));
952 continue;
955 seq_printf(seq,
956 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
957 tt_common_entry->addr,
958 BATADV_PRINT_VID(tt_common_entry->vid),
959 (tt_common_entry->flags &
960 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
961 no_purge ? 'P' : '.',
962 (tt_common_entry->flags &
963 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
964 (tt_common_entry->flags &
965 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
966 (tt_common_entry->flags &
967 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
968 (tt_common_entry->flags &
969 BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
970 no_purge ? 0 : last_seen_secs,
971 no_purge ? 0 : last_seen_msecs,
972 vlan->tt.crc);
974 batadv_softif_vlan_free_ref(vlan);
976 rcu_read_unlock();
978 out:
979 if (primary_if)
980 batadv_hardif_free_ref(primary_if);
981 return 0;
984 static void
985 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
986 struct batadv_tt_local_entry *tt_local_entry,
987 uint16_t flags, const char *message)
989 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
991 /* The local client has to be marked as "pending to be removed" but has
992 * to be kept in the table in order to send it in a full table
993 * response issued before the net ttvn increment (consistency check)
995 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
997 batadv_dbg(BATADV_DBG_TT, bat_priv,
998 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
999 tt_local_entry->common.addr,
1000 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1004 * batadv_tt_local_remove - logically remove an entry from the local table
1005 * @bat_priv: the bat priv with all the soft interface information
1006 * @addr: the MAC address of the client to remove
1007 * @vid: VLAN identifier
1008 * @message: message to append to the log on deletion
1009 * @roaming: true if the deletion is due to a roaming event
1011 * Returns the flags assigned to the local entry before being deleted
1013 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
1014 const uint8_t *addr, unsigned short vid,
1015 const char *message, bool roaming)
1017 struct batadv_tt_local_entry *tt_local_entry;
1018 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
1019 struct batadv_softif_vlan *vlan;
1020 void *tt_entry_exists;
1022 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1023 if (!tt_local_entry)
1024 goto out;
1026 curr_flags = tt_local_entry->common.flags;
1028 flags = BATADV_TT_CLIENT_DEL;
1029 /* if this global entry addition is due to a roaming, the node has to
1030 * mark the local entry as "roamed" in order to correctly reroute
1031 * packets later
1033 if (roaming) {
1034 flags |= BATADV_TT_CLIENT_ROAM;
1035 /* mark the local client as ROAMed */
1036 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1039 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1040 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1041 message);
1042 goto out;
1044 /* if this client has been added right now, it is possible to
1045 * immediately purge it
1047 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1049 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1050 batadv_compare_tt,
1051 batadv_choose_tt,
1052 &tt_local_entry->common);
1053 if (!tt_entry_exists)
1054 goto out;
1056 /* extra call to free the local tt entry */
1057 batadv_tt_local_entry_free_ref(tt_local_entry);
1059 /* decrease the reference held for this vlan */
1060 vlan = batadv_softif_vlan_get(bat_priv, vid);
1061 if (!vlan)
1062 goto out;
1064 batadv_softif_vlan_free_ref(vlan);
1065 batadv_softif_vlan_free_ref(vlan);
1067 out:
1068 if (tt_local_entry)
1069 batadv_tt_local_entry_free_ref(tt_local_entry);
1071 return curr_flags;
1075 * batadv_tt_local_purge_list - purge inactive tt local entries
1076 * @bat_priv: the bat priv with all the soft interface information
1077 * @head: pointer to the list containing the local tt entries
1078 * @timeout: parameter deciding whether a given tt local entry is considered
1079 * inactive or not
1081 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1082 struct hlist_head *head,
1083 int timeout)
1085 struct batadv_tt_local_entry *tt_local_entry;
1086 struct batadv_tt_common_entry *tt_common_entry;
1087 struct hlist_node *node_tmp;
1089 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1090 hash_entry) {
1091 tt_local_entry = container_of(tt_common_entry,
1092 struct batadv_tt_local_entry,
1093 common);
1094 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1095 continue;
1097 /* entry already marked for deletion */
1098 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1099 continue;
1101 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1102 continue;
1104 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1105 BATADV_TT_CLIENT_DEL, "timed out");
1110 * batadv_tt_local_purge - purge inactive tt local entries
1111 * @bat_priv: the bat priv with all the soft interface information
1112 * @timeout: parameter deciding whether a given tt local entry is considered
1113 * inactive or not
1115 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1116 int timeout)
1118 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1119 struct hlist_head *head;
1120 spinlock_t *list_lock; /* protects write access to the hash lists */
1121 uint32_t i;
1123 for (i = 0; i < hash->size; i++) {
1124 head = &hash->table[i];
1125 list_lock = &hash->list_locks[i];
1127 spin_lock_bh(list_lock);
1128 batadv_tt_local_purge_list(bat_priv, head, timeout);
1129 spin_unlock_bh(list_lock);
1133 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1135 struct batadv_hashtable *hash;
1136 spinlock_t *list_lock; /* protects write access to the hash lists */
1137 struct batadv_tt_common_entry *tt_common_entry;
1138 struct batadv_tt_local_entry *tt_local;
1139 struct batadv_softif_vlan *vlan;
1140 struct hlist_node *node_tmp;
1141 struct hlist_head *head;
1142 uint32_t i;
1144 if (!bat_priv->tt.local_hash)
1145 return;
1147 hash = bat_priv->tt.local_hash;
1149 for (i = 0; i < hash->size; i++) {
1150 head = &hash->table[i];
1151 list_lock = &hash->list_locks[i];
1153 spin_lock_bh(list_lock);
1154 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1155 head, hash_entry) {
1156 hlist_del_rcu(&tt_common_entry->hash_entry);
1157 tt_local = container_of(tt_common_entry,
1158 struct batadv_tt_local_entry,
1159 common);
1161 /* decrease the reference held for this vlan */
1162 vlan = batadv_softif_vlan_get(bat_priv,
1163 tt_common_entry->vid);
1164 if (vlan) {
1165 batadv_softif_vlan_free_ref(vlan);
1166 batadv_softif_vlan_free_ref(vlan);
1169 batadv_tt_local_entry_free_ref(tt_local);
1171 spin_unlock_bh(list_lock);
1174 batadv_hash_destroy(hash);
1176 bat_priv->tt.local_hash = NULL;
1179 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1181 if (bat_priv->tt.global_hash)
1182 return 0;
1184 bat_priv->tt.global_hash = batadv_hash_new(1024);
1186 if (!bat_priv->tt.global_hash)
1187 return -ENOMEM;
1189 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1190 &batadv_tt_global_hash_lock_class_key);
1192 return 0;
1195 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1197 struct batadv_tt_change_node *entry, *safe;
1199 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1201 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1202 list) {
1203 list_del(&entry->list);
1204 kfree(entry);
1207 atomic_set(&bat_priv->tt.local_changes, 0);
1208 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1211 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1212 * batadv_tt_global_entry list
1214 * returns it with an increased refcounter, NULL if not found
1216 static struct batadv_tt_orig_list_entry *
1217 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1218 const struct batadv_orig_node *orig_node)
1220 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1221 const struct hlist_head *head;
1223 rcu_read_lock();
1224 head = &entry->orig_list;
1225 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1226 if (tmp_orig_entry->orig_node != orig_node)
1227 continue;
1228 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1229 continue;
1231 orig_entry = tmp_orig_entry;
1232 break;
1234 rcu_read_unlock();
1236 return orig_entry;
1239 /* find out if an orig_node is already in the list of a tt_global_entry.
1240 * returns true if found, false otherwise
1242 static bool
1243 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1244 const struct batadv_orig_node *orig_node)
1246 struct batadv_tt_orig_list_entry *orig_entry;
1247 bool found = false;
1249 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1250 if (orig_entry) {
1251 found = true;
1252 batadv_tt_orig_list_entry_free_ref(orig_entry);
1255 return found;
1258 static void
1259 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1260 struct batadv_orig_node *orig_node, int ttvn)
1262 struct batadv_tt_orig_list_entry *orig_entry;
1264 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1265 if (orig_entry) {
1266 /* refresh the ttvn: the current value could be a bogus one that
1267 * was added during a "temporary client detection"
1269 orig_entry->ttvn = ttvn;
1270 goto out;
1273 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1274 if (!orig_entry)
1275 goto out;
1277 INIT_HLIST_NODE(&orig_entry->list);
1278 atomic_inc(&orig_node->refcount);
1279 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1280 orig_entry->orig_node = orig_node;
1281 orig_entry->ttvn = ttvn;
1282 atomic_set(&orig_entry->refcount, 2);
1284 spin_lock_bh(&tt_global->list_lock);
1285 hlist_add_head_rcu(&orig_entry->list,
1286 &tt_global->orig_list);
1287 spin_unlock_bh(&tt_global->list_lock);
1288 atomic_inc(&tt_global->orig_list_count);
1290 out:
1291 if (orig_entry)
1292 batadv_tt_orig_list_entry_free_ref(orig_entry);
1296 * batadv_tt_global_add - add a new TT global entry or update an existing one
1297 * @bat_priv: the bat priv with all the soft interface information
1298 * @orig_node: the originator announcing the client
1299 * @tt_addr: the mac address of the non-mesh client
1300 * @vid: VLAN identifier
1301 * @flags: TT flags that have to be set for this non-mesh client
1302 * @ttvn: the tt version number ever announcing this non-mesh client
1304 * Add a new TT global entry for the given originator. If the entry already
1305 * exists add a new reference to the given originator (a global entry can have
1306 * references to multiple originators) and adjust the flags attribute to reflect
1307 * the function argument.
1308 * If a TT local entry exists for this non-mesh client remove it.
1310 * The caller must hold orig_node refcount.
1312 * Return true if the new entry has been added, false otherwise
1314 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1315 struct batadv_orig_node *orig_node,
1316 const unsigned char *tt_addr,
1317 unsigned short vid, uint16_t flags,
1318 uint8_t ttvn)
1320 struct batadv_tt_global_entry *tt_global_entry;
1321 struct batadv_tt_local_entry *tt_local_entry;
1322 bool ret = false;
1323 int hash_added;
1324 struct batadv_tt_common_entry *common;
1325 uint16_t local_flags;
1327 /* ignore global entries from backbone nodes */
1328 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1329 return true;
1331 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1332 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1334 /* if the node already has a local client for this entry, it has to wait
1335 * for a roaming advertisement instead of manually messing up the global
1336 * table
1338 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1339 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1340 goto out;
1342 if (!tt_global_entry) {
1343 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1344 if (!tt_global_entry)
1345 goto out;
1347 common = &tt_global_entry->common;
1348 ether_addr_copy(common->addr, tt_addr);
1349 common->vid = vid;
1351 common->flags = flags;
1352 tt_global_entry->roam_at = 0;
1353 /* node must store current time in case of roaming. This is
1354 * needed to purge this entry out on timeout (if nobody claims
1355 * it)
1357 if (flags & BATADV_TT_CLIENT_ROAM)
1358 tt_global_entry->roam_at = jiffies;
1359 atomic_set(&common->refcount, 2);
1360 common->added_at = jiffies;
1362 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1363 atomic_set(&tt_global_entry->orig_list_count, 0);
1364 spin_lock_init(&tt_global_entry->list_lock);
1366 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1367 batadv_compare_tt,
1368 batadv_choose_tt, common,
1369 &common->hash_entry);
1371 if (unlikely(hash_added != 0)) {
1372 /* remove the reference for the hash */
1373 batadv_tt_global_entry_free_ref(tt_global_entry);
1374 goto out_remove;
1376 } else {
1377 common = &tt_global_entry->common;
1378 /* If there is already a global entry, we can use this one for
1379 * our processing.
1380 * But if we are trying to add a temporary client then here are
1381 * two options at this point:
1382 * 1) the global client is not a temporary client: the global
1383 * client has to be left as it is, temporary information
1384 * should never override any already known client state
1385 * 2) the global client is a temporary client: purge the
1386 * originator list and add the new one orig_entry
1388 if (flags & BATADV_TT_CLIENT_TEMP) {
1389 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1390 goto out;
1391 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1392 orig_node))
1393 goto out_remove;
1394 batadv_tt_global_del_orig_list(tt_global_entry);
1395 goto add_orig_entry;
1398 /* if the client was temporary added before receiving the first
1399 * OGM announcing it, we have to clear the TEMP flag
1401 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1403 /* the change can carry possible "attribute" flags like the
1404 * TT_CLIENT_WIFI, therefore they have to be copied in the
1405 * client entry
1407 tt_global_entry->common.flags |= flags;
1409 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1410 * one originator left in the list and we previously received a
1411 * delete + roaming change for this originator.
1413 * We should first delete the old originator before adding the
1414 * new one.
1416 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1417 batadv_tt_global_del_orig_list(tt_global_entry);
1418 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1419 tt_global_entry->roam_at = 0;
1422 add_orig_entry:
1423 /* add the new orig_entry (if needed) or update it */
1424 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1426 batadv_dbg(BATADV_DBG_TT, bat_priv,
1427 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1428 common->addr, BATADV_PRINT_VID(common->vid),
1429 orig_node->orig);
1430 ret = true;
1432 out_remove:
1433 /* Do not remove multicast addresses from the local hash on
1434 * global additions
1436 if (is_multicast_ether_addr(tt_addr))
1437 goto out;
1439 /* remove address from local hash if present */
1440 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1441 "global tt received",
1442 flags & BATADV_TT_CLIENT_ROAM);
1443 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1445 if (!(flags & BATADV_TT_CLIENT_ROAM))
1446 /* this is a normal global add. Therefore the client is not in a
1447 * roaming state anymore.
1449 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1451 out:
1452 if (tt_global_entry)
1453 batadv_tt_global_entry_free_ref(tt_global_entry);
1454 if (tt_local_entry)
1455 batadv_tt_local_entry_free_ref(tt_local_entry);
1456 return ret;
1460 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1461 * @bat_priv: the bat priv with all the soft interface information
1462 * @tt_global_entry: global translation table entry to be analyzed
1464 * This functon assumes the caller holds rcu_read_lock().
1465 * Returns best originator list entry or NULL on errors.
1467 static struct batadv_tt_orig_list_entry *
1468 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1469 struct batadv_tt_global_entry *tt_global_entry)
1471 struct batadv_neigh_node *router, *best_router = NULL;
1472 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1473 struct hlist_head *head;
1474 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1476 head = &tt_global_entry->orig_list;
1477 hlist_for_each_entry_rcu(orig_entry, head, list) {
1478 router = batadv_orig_router_get(orig_entry->orig_node,
1479 BATADV_IF_DEFAULT);
1480 if (!router)
1481 continue;
1483 if (best_router &&
1484 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1485 best_router, BATADV_IF_DEFAULT) <= 0) {
1486 batadv_neigh_node_free_ref(router);
1487 continue;
1490 /* release the refcount for the "old" best */
1491 if (best_router)
1492 batadv_neigh_node_free_ref(best_router);
1494 best_entry = orig_entry;
1495 best_router = router;
1498 if (best_router)
1499 batadv_neigh_node_free_ref(best_router);
1501 return best_entry;
1505 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1506 * for this global entry
1507 * @bat_priv: the bat priv with all the soft interface information
1508 * @tt_global_entry: global translation table entry to be printed
1509 * @seq: debugfs table seq_file struct
1511 * This functon assumes the caller holds rcu_read_lock().
1513 static void
1514 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1515 struct batadv_tt_global_entry *tt_global_entry,
1516 struct seq_file *seq)
1518 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1519 struct batadv_tt_common_entry *tt_common_entry;
1520 struct batadv_orig_node_vlan *vlan;
1521 struct hlist_head *head;
1522 uint8_t last_ttvn;
1523 uint16_t flags;
1525 tt_common_entry = &tt_global_entry->common;
1526 flags = tt_common_entry->flags;
1528 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1529 if (best_entry) {
1530 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1531 tt_common_entry->vid);
1532 if (!vlan) {
1533 seq_printf(seq,
1534 " * Cannot retrieve VLAN %d for originator %pM\n",
1535 BATADV_PRINT_VID(tt_common_entry->vid),
1536 best_entry->orig_node->orig);
1537 goto print_list;
1540 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1541 seq_printf(seq,
1542 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1543 '*', tt_global_entry->common.addr,
1544 BATADV_PRINT_VID(tt_global_entry->common.vid),
1545 best_entry->ttvn, best_entry->orig_node->orig,
1546 last_ttvn, vlan->tt.crc,
1547 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1548 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1549 (flags & BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
1550 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1552 batadv_orig_node_vlan_free_ref(vlan);
1555 print_list:
1556 head = &tt_global_entry->orig_list;
1558 hlist_for_each_entry_rcu(orig_entry, head, list) {
1559 if (best_entry == orig_entry)
1560 continue;
1562 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1563 tt_common_entry->vid);
1564 if (!vlan) {
1565 seq_printf(seq,
1566 " + Cannot retrieve VLAN %d for originator %pM\n",
1567 BATADV_PRINT_VID(tt_common_entry->vid),
1568 orig_entry->orig_node->orig);
1569 continue;
1572 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1573 seq_printf(seq,
1574 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1575 '+', tt_global_entry->common.addr,
1576 BATADV_PRINT_VID(tt_global_entry->common.vid),
1577 orig_entry->ttvn, orig_entry->orig_node->orig,
1578 last_ttvn, vlan->tt.crc,
1579 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1580 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1581 (flags & BATADV_TT_CLIENT_ISOLA ? 'I' : '.'),
1582 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1584 batadv_orig_node_vlan_free_ref(vlan);
1588 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1590 struct net_device *net_dev = (struct net_device *)seq->private;
1591 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1592 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1593 struct batadv_tt_common_entry *tt_common_entry;
1594 struct batadv_tt_global_entry *tt_global;
1595 struct batadv_hard_iface *primary_if;
1596 struct hlist_head *head;
1597 uint32_t i;
1599 primary_if = batadv_seq_print_text_primary_if_get(seq);
1600 if (!primary_if)
1601 goto out;
1603 seq_printf(seq,
1604 "Globally announced TT entries received via the mesh %s\n",
1605 net_dev->name);
1606 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1607 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1608 "CRC", "Flags");
1610 for (i = 0; i < hash->size; i++) {
1611 head = &hash->table[i];
1613 rcu_read_lock();
1614 hlist_for_each_entry_rcu(tt_common_entry,
1615 head, hash_entry) {
1616 tt_global = container_of(tt_common_entry,
1617 struct batadv_tt_global_entry,
1618 common);
1619 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1621 rcu_read_unlock();
1623 out:
1624 if (primary_if)
1625 batadv_hardif_free_ref(primary_if);
1626 return 0;
1630 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1631 * @tt_global_entry: the global entry to remove the orig_entry from
1632 * @orig_entry: the orig entry to remove and free
1634 * Remove an orig_entry from its list in the given tt_global_entry and
1635 * free this orig_entry afterwards.
1637 static void
1638 batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1639 struct batadv_tt_orig_list_entry *orig_entry)
1641 batadv_tt_global_size_dec(orig_entry->orig_node,
1642 tt_global_entry->common.vid);
1643 atomic_dec(&tt_global_entry->orig_list_count);
1644 hlist_del_rcu(&orig_entry->list);
1645 batadv_tt_orig_list_entry_free_ref(orig_entry);
1648 /* deletes the orig list of a tt_global_entry */
1649 static void
1650 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1652 struct hlist_head *head;
1653 struct hlist_node *safe;
1654 struct batadv_tt_orig_list_entry *orig_entry;
1656 spin_lock_bh(&tt_global_entry->list_lock);
1657 head = &tt_global_entry->orig_list;
1658 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1659 batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1660 spin_unlock_bh(&tt_global_entry->list_lock);
1664 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1665 * @bat_priv: the bat priv with all the soft interface information
1666 * @tt_global_entry: the global entry to remove the orig_node from
1667 * @orig_node: the originator announcing the client
1668 * @message: message to append to the log on deletion
1670 * Remove the given orig_node and its according orig_entry from the given
1671 * global tt entry.
1673 static void
1674 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1675 struct batadv_tt_global_entry *tt_global_entry,
1676 struct batadv_orig_node *orig_node,
1677 const char *message)
1679 struct hlist_head *head;
1680 struct hlist_node *safe;
1681 struct batadv_tt_orig_list_entry *orig_entry;
1682 unsigned short vid;
1684 spin_lock_bh(&tt_global_entry->list_lock);
1685 head = &tt_global_entry->orig_list;
1686 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1687 if (orig_entry->orig_node == orig_node) {
1688 vid = tt_global_entry->common.vid;
1689 batadv_dbg(BATADV_DBG_TT, bat_priv,
1690 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1691 orig_node->orig,
1692 tt_global_entry->common.addr,
1693 BATADV_PRINT_VID(vid), message);
1694 batadv_tt_global_del_orig_entry(tt_global_entry,
1695 orig_entry);
1698 spin_unlock_bh(&tt_global_entry->list_lock);
1701 /* If the client is to be deleted, we check if it is the last origantor entry
1702 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1703 * timer, otherwise we simply remove the originator scheduled for deletion.
1705 static void
1706 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1707 struct batadv_tt_global_entry *tt_global_entry,
1708 struct batadv_orig_node *orig_node,
1709 const char *message)
1711 bool last_entry = true;
1712 struct hlist_head *head;
1713 struct batadv_tt_orig_list_entry *orig_entry;
1715 /* no local entry exists, case 1:
1716 * Check if this is the last one or if other entries exist.
1719 rcu_read_lock();
1720 head = &tt_global_entry->orig_list;
1721 hlist_for_each_entry_rcu(orig_entry, head, list) {
1722 if (orig_entry->orig_node != orig_node) {
1723 last_entry = false;
1724 break;
1727 rcu_read_unlock();
1729 if (last_entry) {
1730 /* its the last one, mark for roaming. */
1731 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1732 tt_global_entry->roam_at = jiffies;
1733 } else
1734 /* there is another entry, we can simply delete this
1735 * one and can still use the other one.
1737 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1738 orig_node, message);
1742 * batadv_tt_global_del - remove a client from the global table
1743 * @bat_priv: the bat priv with all the soft interface information
1744 * @orig_node: an originator serving this client
1745 * @addr: the mac address of the client
1746 * @vid: VLAN identifier
1747 * @message: a message explaining the reason for deleting the client to print
1748 * for debugging purpose
1749 * @roaming: true if the deletion has been triggered by a roaming event
1751 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1752 struct batadv_orig_node *orig_node,
1753 const unsigned char *addr, unsigned short vid,
1754 const char *message, bool roaming)
1756 struct batadv_tt_global_entry *tt_global_entry;
1757 struct batadv_tt_local_entry *local_entry = NULL;
1759 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1760 if (!tt_global_entry)
1761 goto out;
1763 if (!roaming) {
1764 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1765 orig_node, message);
1767 if (hlist_empty(&tt_global_entry->orig_list))
1768 batadv_tt_global_free(bat_priv, tt_global_entry,
1769 message);
1771 goto out;
1774 /* if we are deleting a global entry due to a roam
1775 * event, there are two possibilities:
1776 * 1) the client roamed from node A to node B => if there
1777 * is only one originator left for this client, we mark
1778 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1779 * wait for node B to claim it. In case of timeout
1780 * the entry is purged.
1782 * If there are other originators left, we directly delete
1783 * the originator.
1784 * 2) the client roamed to us => we can directly delete
1785 * the global entry, since it is useless now.
1787 local_entry = batadv_tt_local_hash_find(bat_priv,
1788 tt_global_entry->common.addr,
1789 vid);
1790 if (local_entry) {
1791 /* local entry exists, case 2: client roamed to us. */
1792 batadv_tt_global_del_orig_list(tt_global_entry);
1793 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1794 } else
1795 /* no local entry exists, case 1: check for roaming */
1796 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1797 orig_node, message);
1799 out:
1800 if (tt_global_entry)
1801 batadv_tt_global_entry_free_ref(tt_global_entry);
1802 if (local_entry)
1803 batadv_tt_local_entry_free_ref(local_entry);
1807 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1808 * given originator matching the provided vid
1809 * @bat_priv: the bat priv with all the soft interface information
1810 * @orig_node: the originator owning the entries to remove
1811 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1812 * removed
1813 * @message: debug message to print as "reason"
1815 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1816 struct batadv_orig_node *orig_node,
1817 int32_t match_vid,
1818 const char *message)
1820 struct batadv_tt_global_entry *tt_global;
1821 struct batadv_tt_common_entry *tt_common_entry;
1822 uint32_t i;
1823 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1824 struct hlist_node *safe;
1825 struct hlist_head *head;
1826 spinlock_t *list_lock; /* protects write access to the hash lists */
1827 unsigned short vid;
1829 if (!hash)
1830 return;
1832 for (i = 0; i < hash->size; i++) {
1833 head = &hash->table[i];
1834 list_lock = &hash->list_locks[i];
1836 spin_lock_bh(list_lock);
1837 hlist_for_each_entry_safe(tt_common_entry, safe,
1838 head, hash_entry) {
1839 /* remove only matching entries */
1840 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1841 continue;
1843 tt_global = container_of(tt_common_entry,
1844 struct batadv_tt_global_entry,
1845 common);
1847 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1848 orig_node, message);
1850 if (hlist_empty(&tt_global->orig_list)) {
1851 vid = tt_global->common.vid;
1852 batadv_dbg(BATADV_DBG_TT, bat_priv,
1853 "Deleting global tt entry %pM (vid: %d): %s\n",
1854 tt_global->common.addr,
1855 BATADV_PRINT_VID(vid), message);
1856 hlist_del_rcu(&tt_common_entry->hash_entry);
1857 batadv_tt_global_entry_free_ref(tt_global);
1860 spin_unlock_bh(list_lock);
1862 clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
1865 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1866 char **msg)
1868 bool purge = false;
1869 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1870 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1872 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1873 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1874 purge = true;
1875 *msg = "Roaming timeout\n";
1878 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1879 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1880 purge = true;
1881 *msg = "Temporary client timeout\n";
1884 return purge;
1887 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1889 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1890 struct hlist_head *head;
1891 struct hlist_node *node_tmp;
1892 spinlock_t *list_lock; /* protects write access to the hash lists */
1893 uint32_t i;
1894 char *msg = NULL;
1895 struct batadv_tt_common_entry *tt_common;
1896 struct batadv_tt_global_entry *tt_global;
1898 for (i = 0; i < hash->size; i++) {
1899 head = &hash->table[i];
1900 list_lock = &hash->list_locks[i];
1902 spin_lock_bh(list_lock);
1903 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1904 hash_entry) {
1905 tt_global = container_of(tt_common,
1906 struct batadv_tt_global_entry,
1907 common);
1909 if (!batadv_tt_global_to_purge(tt_global, &msg))
1910 continue;
1912 batadv_dbg(BATADV_DBG_TT, bat_priv,
1913 "Deleting global tt entry %pM (vid: %d): %s\n",
1914 tt_global->common.addr,
1915 BATADV_PRINT_VID(tt_global->common.vid),
1916 msg);
1918 hlist_del_rcu(&tt_common->hash_entry);
1920 batadv_tt_global_entry_free_ref(tt_global);
1922 spin_unlock_bh(list_lock);
1926 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1928 struct batadv_hashtable *hash;
1929 spinlock_t *list_lock; /* protects write access to the hash lists */
1930 struct batadv_tt_common_entry *tt_common_entry;
1931 struct batadv_tt_global_entry *tt_global;
1932 struct hlist_node *node_tmp;
1933 struct hlist_head *head;
1934 uint32_t i;
1936 if (!bat_priv->tt.global_hash)
1937 return;
1939 hash = bat_priv->tt.global_hash;
1941 for (i = 0; i < hash->size; i++) {
1942 head = &hash->table[i];
1943 list_lock = &hash->list_locks[i];
1945 spin_lock_bh(list_lock);
1946 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1947 head, hash_entry) {
1948 hlist_del_rcu(&tt_common_entry->hash_entry);
1949 tt_global = container_of(tt_common_entry,
1950 struct batadv_tt_global_entry,
1951 common);
1952 batadv_tt_global_entry_free_ref(tt_global);
1954 spin_unlock_bh(list_lock);
1957 batadv_hash_destroy(hash);
1959 bat_priv->tt.global_hash = NULL;
1962 static bool
1963 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1964 struct batadv_tt_global_entry *tt_global_entry)
1966 bool ret = false;
1968 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1969 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1970 ret = true;
1972 /* check if the two clients are marked as isolated */
1973 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
1974 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
1975 ret = true;
1977 return ret;
1981 * batadv_transtable_search - get the mesh destination for a given client
1982 * @bat_priv: the bat priv with all the soft interface information
1983 * @src: mac address of the source client
1984 * @addr: mac address of the destination client
1985 * @vid: VLAN identifier
1987 * Returns a pointer to the originator that was selected as destination in the
1988 * mesh for contacting the client 'addr', NULL otherwise.
1989 * In case of multiple originators serving the same client, the function returns
1990 * the best one (best in terms of metric towards the destination node).
1992 * If the two clients are AP isolated the function returns NULL.
1994 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1995 const uint8_t *src,
1996 const uint8_t *addr,
1997 unsigned short vid)
1999 struct batadv_tt_local_entry *tt_local_entry = NULL;
2000 struct batadv_tt_global_entry *tt_global_entry = NULL;
2001 struct batadv_orig_node *orig_node = NULL;
2002 struct batadv_tt_orig_list_entry *best_entry;
2004 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2005 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2006 if (!tt_local_entry ||
2007 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2008 goto out;
2011 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2012 if (!tt_global_entry)
2013 goto out;
2015 /* check whether the clients should not communicate due to AP
2016 * isolation
2018 if (tt_local_entry &&
2019 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2020 goto out;
2022 rcu_read_lock();
2023 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2024 /* found anything? */
2025 if (best_entry)
2026 orig_node = best_entry->orig_node;
2027 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2028 orig_node = NULL;
2029 rcu_read_unlock();
2031 out:
2032 if (tt_global_entry)
2033 batadv_tt_global_entry_free_ref(tt_global_entry);
2034 if (tt_local_entry)
2035 batadv_tt_local_entry_free_ref(tt_local_entry);
2037 return orig_node;
2041 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2042 * to the given orig_node
2043 * @bat_priv: the bat priv with all the soft interface information
2044 * @orig_node: originator for which the CRC should be computed
2045 * @vid: VLAN identifier for which the CRC32 has to be computed
2047 * This function computes the checksum for the global table corresponding to a
2048 * specific originator. In particular, the checksum is computed as follows: For
2049 * each client connected to the originator the CRC32C of the MAC address and the
2050 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2051 * together.
2053 * The idea behind is that CRC32C should be used as much as possible in order to
2054 * produce a unique hash of the table, but since the order which is used to feed
2055 * the CRC32C function affects the result and since every node in the network
2056 * probably sorts the clients differently, the hash function cannot be directly
2057 * computed over the entire table. Hence the CRC32C is used only on
2058 * the single client entry, while all the results are then xor'ed together
2059 * because the XOR operation can combine them all while trying to reduce the
2060 * noise as much as possible.
2062 * Returns the checksum of the global table of a given originator.
2064 static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
2065 struct batadv_orig_node *orig_node,
2066 unsigned short vid)
2068 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2069 struct batadv_tt_common_entry *tt_common;
2070 struct batadv_tt_global_entry *tt_global;
2071 struct hlist_head *head;
2072 uint32_t i, crc_tmp, crc = 0;
2073 uint8_t flags;
2074 __be16 tmp_vid;
2076 for (i = 0; i < hash->size; i++) {
2077 head = &hash->table[i];
2079 rcu_read_lock();
2080 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2081 tt_global = container_of(tt_common,
2082 struct batadv_tt_global_entry,
2083 common);
2084 /* compute the CRC only for entries belonging to the
2085 * VLAN identified by the vid passed as parameter
2087 if (tt_common->vid != vid)
2088 continue;
2090 /* Roaming clients are in the global table for
2091 * consistency only. They don't have to be
2092 * taken into account while computing the
2093 * global crc
2095 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2096 continue;
2097 /* Temporary clients have not been announced yet, so
2098 * they have to be skipped while computing the global
2099 * crc
2101 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2102 continue;
2104 /* find out if this global entry is announced by this
2105 * originator
2107 if (!batadv_tt_global_entry_has_orig(tt_global,
2108 orig_node))
2109 continue;
2111 /* use network order to read the VID: this ensures that
2112 * every node reads the bytes in the same order.
2114 tmp_vid = htons(tt_common->vid);
2115 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2117 /* compute the CRC on flags that have to be kept in sync
2118 * among nodes
2120 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2121 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2123 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2125 rcu_read_unlock();
2128 return crc;
2132 * batadv_tt_local_crc - calculates the checksum of the local table
2133 * @bat_priv: the bat priv with all the soft interface information
2134 * @vid: VLAN identifier for which the CRC32 has to be computed
2136 * For details about the computation, please refer to the documentation for
2137 * batadv_tt_global_crc().
2139 * Returns the checksum of the local table
2141 static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2142 unsigned short vid)
2144 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2145 struct batadv_tt_common_entry *tt_common;
2146 struct hlist_head *head;
2147 uint32_t i, crc_tmp, crc = 0;
2148 uint8_t flags;
2149 __be16 tmp_vid;
2151 for (i = 0; i < hash->size; i++) {
2152 head = &hash->table[i];
2154 rcu_read_lock();
2155 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2156 /* compute the CRC only for entries belonging to the
2157 * VLAN identified by vid
2159 if (tt_common->vid != vid)
2160 continue;
2162 /* not yet committed clients have not to be taken into
2163 * account while computing the CRC
2165 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2166 continue;
2168 /* use network order to read the VID: this ensures that
2169 * every node reads the bytes in the same order.
2171 tmp_vid = htons(tt_common->vid);
2172 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2174 /* compute the CRC on flags that have to be kept in sync
2175 * among nodes
2177 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2178 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2180 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2182 rcu_read_unlock();
2185 return crc;
2188 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2190 struct batadv_tt_req_node *node, *safe;
2192 spin_lock_bh(&bat_priv->tt.req_list_lock);
2194 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2195 list_del(&node->list);
2196 kfree(node);
2199 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2202 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2203 struct batadv_orig_node *orig_node,
2204 const void *tt_buff,
2205 uint16_t tt_buff_len)
2207 /* Replace the old buffer only if I received something in the
2208 * last OGM (the OGM could carry no changes)
2210 spin_lock_bh(&orig_node->tt_buff_lock);
2211 if (tt_buff_len > 0) {
2212 kfree(orig_node->tt_buff);
2213 orig_node->tt_buff_len = 0;
2214 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2215 if (orig_node->tt_buff) {
2216 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2217 orig_node->tt_buff_len = tt_buff_len;
2220 spin_unlock_bh(&orig_node->tt_buff_lock);
2223 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2225 struct batadv_tt_req_node *node, *safe;
2227 spin_lock_bh(&bat_priv->tt.req_list_lock);
2228 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2229 if (batadv_has_timed_out(node->issued_at,
2230 BATADV_TT_REQUEST_TIMEOUT)) {
2231 list_del(&node->list);
2232 kfree(node);
2235 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2238 /* returns the pointer to the new tt_req_node struct if no request
2239 * has already been issued for this orig_node, NULL otherwise
2241 static struct batadv_tt_req_node *
2242 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2243 struct batadv_orig_node *orig_node)
2245 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2247 spin_lock_bh(&bat_priv->tt.req_list_lock);
2248 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2249 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2250 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2251 BATADV_TT_REQUEST_TIMEOUT))
2252 goto unlock;
2255 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2256 if (!tt_req_node)
2257 goto unlock;
2259 ether_addr_copy(tt_req_node->addr, orig_node->orig);
2260 tt_req_node->issued_at = jiffies;
2262 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
2263 unlock:
2264 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2265 return tt_req_node;
2269 * batadv_tt_local_valid - verify that given tt entry is a valid one
2270 * @entry_ptr: to be checked local tt entry
2271 * @data_ptr: not used but definition required to satisfy the callback prototype
2273 * Returns 1 if the entry is a valid, 0 otherwise.
2275 static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2277 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2279 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2280 return 0;
2281 return 1;
2284 static int batadv_tt_global_valid(const void *entry_ptr,
2285 const void *data_ptr)
2287 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2288 const struct batadv_tt_global_entry *tt_global_entry;
2289 const struct batadv_orig_node *orig_node = data_ptr;
2291 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2292 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2293 return 0;
2295 tt_global_entry = container_of(tt_common_entry,
2296 struct batadv_tt_global_entry,
2297 common);
2299 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2303 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2304 * specified tt hash
2305 * @bat_priv: the bat priv with all the soft interface information
2306 * @hash: hash table containing the tt entries
2307 * @tt_len: expected tvlv tt data buffer length in number of bytes
2308 * @tvlv_buff: pointer to the buffer to fill with the TT data
2309 * @valid_cb: function to filter tt change entries
2310 * @cb_data: data passed to the filter function as argument
2312 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2313 struct batadv_hashtable *hash,
2314 void *tvlv_buff, uint16_t tt_len,
2315 int (*valid_cb)(const void *, const void *),
2316 void *cb_data)
2318 struct batadv_tt_common_entry *tt_common_entry;
2319 struct batadv_tvlv_tt_change *tt_change;
2320 struct hlist_head *head;
2321 uint16_t tt_tot, tt_num_entries = 0;
2322 uint32_t i;
2324 tt_tot = batadv_tt_entries(tt_len);
2325 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2327 rcu_read_lock();
2328 for (i = 0; i < hash->size; i++) {
2329 head = &hash->table[i];
2331 hlist_for_each_entry_rcu(tt_common_entry,
2332 head, hash_entry) {
2333 if (tt_tot == tt_num_entries)
2334 break;
2336 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2337 continue;
2339 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2340 tt_change->flags = tt_common_entry->flags;
2341 tt_change->vid = htons(tt_common_entry->vid);
2342 memset(tt_change->reserved, 0,
2343 sizeof(tt_change->reserved));
2345 tt_num_entries++;
2346 tt_change++;
2349 rcu_read_unlock();
2353 * batadv_tt_global_check_crc - check if all the CRCs are correct
2354 * @orig_node: originator for which the CRCs have to be checked
2355 * @tt_vlan: pointer to the first tvlv VLAN entry
2356 * @num_vlan: number of tvlv VLAN entries
2357 * @create: if true, create VLAN objects if not found
2359 * Return true if all the received CRCs match the locally stored ones, false
2360 * otherwise
2362 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2363 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2364 uint16_t num_vlan)
2366 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2367 struct batadv_orig_node_vlan *vlan;
2368 uint32_t crc;
2369 int i;
2371 /* check if each received CRC matches the locally stored one */
2372 for (i = 0; i < num_vlan; i++) {
2373 tt_vlan_tmp = tt_vlan + i;
2375 /* if orig_node is a backbone node for this VLAN, don't check
2376 * the CRC as we ignore all the global entries over it
2378 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2379 orig_node->orig,
2380 ntohs(tt_vlan_tmp->vid)))
2381 continue;
2383 vlan = batadv_orig_node_vlan_get(orig_node,
2384 ntohs(tt_vlan_tmp->vid));
2385 if (!vlan)
2386 return false;
2388 crc = vlan->tt.crc;
2389 batadv_orig_node_vlan_free_ref(vlan);
2391 if (crc != ntohl(tt_vlan_tmp->crc))
2392 return false;
2395 return true;
2399 * batadv_tt_local_update_crc - update all the local CRCs
2400 * @bat_priv: the bat priv with all the soft interface information
2402 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2404 struct batadv_softif_vlan *vlan;
2406 /* recompute the global CRC for each VLAN */
2407 rcu_read_lock();
2408 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2409 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2411 rcu_read_unlock();
2415 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2416 * @bat_priv: the bat priv with all the soft interface information
2417 * @orig_node: the orig_node for which the CRCs have to be updated
2419 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2420 struct batadv_orig_node *orig_node)
2422 struct batadv_orig_node_vlan *vlan;
2423 uint32_t crc;
2425 /* recompute the global CRC for each VLAN */
2426 rcu_read_lock();
2427 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2428 /* if orig_node is a backbone node for this VLAN, don't compute
2429 * the CRC as we ignore all the global entries over it
2431 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2432 vlan->vid))
2433 continue;
2435 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2436 vlan->tt.crc = crc;
2438 rcu_read_unlock();
2442 * batadv_send_tt_request - send a TT Request message to a given node
2443 * @bat_priv: the bat priv with all the soft interface information
2444 * @dst_orig_node: the destination of the message
2445 * @ttvn: the version number that the source of the message is looking for
2446 * @tt_vlan: pointer to the first tvlv VLAN object to request
2447 * @num_vlan: number of tvlv VLAN entries
2448 * @full_table: ask for the entire translation table if true, while only for the
2449 * last TT diff otherwise
2451 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2452 struct batadv_orig_node *dst_orig_node,
2453 uint8_t ttvn,
2454 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2455 uint16_t num_vlan, bool full_table)
2457 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2458 struct batadv_tt_req_node *tt_req_node = NULL;
2459 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2460 struct batadv_hard_iface *primary_if;
2461 bool ret = false;
2462 int i, size;
2464 primary_if = batadv_primary_if_get_selected(bat_priv);
2465 if (!primary_if)
2466 goto out;
2468 /* The new tt_req will be issued only if I'm not waiting for a
2469 * reply from the same orig_node yet
2471 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
2472 if (!tt_req_node)
2473 goto out;
2475 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2476 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2477 if (!tvlv_tt_data)
2478 goto out;
2480 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2481 tvlv_tt_data->ttvn = ttvn;
2482 tvlv_tt_data->num_vlan = htons(num_vlan);
2484 /* send all the CRCs within the request. This is needed by intermediate
2485 * nodes to ensure they have the correct table before replying
2487 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2488 for (i = 0; i < num_vlan; i++) {
2489 tt_vlan_req->vid = tt_vlan->vid;
2490 tt_vlan_req->crc = tt_vlan->crc;
2492 tt_vlan_req++;
2493 tt_vlan++;
2496 if (full_table)
2497 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2499 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2500 dst_orig_node->orig, full_table ? 'F' : '.');
2502 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2503 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2504 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2505 tvlv_tt_data, size);
2506 ret = true;
2508 out:
2509 if (primary_if)
2510 batadv_hardif_free_ref(primary_if);
2511 if (ret && tt_req_node) {
2512 spin_lock_bh(&bat_priv->tt.req_list_lock);
2513 list_del(&tt_req_node->list);
2514 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2515 kfree(tt_req_node);
2517 kfree(tvlv_tt_data);
2518 return ret;
2522 * batadv_send_other_tt_response - send reply to tt request concerning another
2523 * node's translation table
2524 * @bat_priv: the bat priv with all the soft interface information
2525 * @tt_data: tt data containing the tt request information
2526 * @req_src: mac address of tt request sender
2527 * @req_dst: mac address of tt request recipient
2529 * Returns true if tt request reply was sent, false otherwise.
2531 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2532 struct batadv_tvlv_tt_data *tt_data,
2533 uint8_t *req_src, uint8_t *req_dst)
2535 struct batadv_orig_node *req_dst_orig_node;
2536 struct batadv_orig_node *res_dst_orig_node = NULL;
2537 struct batadv_tvlv_tt_change *tt_change;
2538 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2539 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2540 bool ret = false, full_table;
2541 uint8_t orig_ttvn, req_ttvn;
2542 uint16_t tvlv_len;
2543 int32_t tt_len;
2545 batadv_dbg(BATADV_DBG_TT, bat_priv,
2546 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2547 req_src, tt_data->ttvn, req_dst,
2548 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2550 /* Let's get the orig node of the REAL destination */
2551 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2552 if (!req_dst_orig_node)
2553 goto out;
2555 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2556 if (!res_dst_orig_node)
2557 goto out;
2559 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
2560 req_ttvn = tt_data->ttvn;
2562 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2563 /* this node doesn't have the requested data */
2564 if (orig_ttvn != req_ttvn ||
2565 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2566 ntohs(tt_data->num_vlan)))
2567 goto out;
2569 /* If the full table has been explicitly requested */
2570 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2571 !req_dst_orig_node->tt_buff)
2572 full_table = true;
2573 else
2574 full_table = false;
2576 /* TT fragmentation hasn't been implemented yet, so send as many
2577 * TT entries fit a single packet as possible only
2579 if (!full_table) {
2580 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2581 tt_len = req_dst_orig_node->tt_buff_len;
2583 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2584 &tvlv_tt_data,
2585 &tt_change,
2586 &tt_len);
2587 if (!tt_len)
2588 goto unlock;
2590 /* Copy the last orig_node's OGM buffer */
2591 memcpy(tt_change, req_dst_orig_node->tt_buff,
2592 req_dst_orig_node->tt_buff_len);
2593 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2594 } else {
2595 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2596 * in the initial part
2598 tt_len = -1;
2599 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2600 &tvlv_tt_data,
2601 &tt_change,
2602 &tt_len);
2603 if (!tt_len)
2604 goto out;
2606 /* fill the rest of the tvlv with the real TT entries */
2607 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2608 tt_change, tt_len,
2609 batadv_tt_global_valid,
2610 req_dst_orig_node);
2613 /* Don't send the response, if larger than fragmented packet. */
2614 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2615 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2616 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2617 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2618 res_dst_orig_node->orig);
2619 goto out;
2622 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2623 tvlv_tt_data->ttvn = req_ttvn;
2625 if (full_table)
2626 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2628 batadv_dbg(BATADV_DBG_TT, bat_priv,
2629 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2630 res_dst_orig_node->orig, req_dst_orig_node->orig,
2631 full_table ? 'F' : '.', req_ttvn);
2633 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2635 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2636 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2637 tvlv_len);
2639 ret = true;
2640 goto out;
2642 unlock:
2643 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2645 out:
2646 if (res_dst_orig_node)
2647 batadv_orig_node_free_ref(res_dst_orig_node);
2648 if (req_dst_orig_node)
2649 batadv_orig_node_free_ref(req_dst_orig_node);
2650 kfree(tvlv_tt_data);
2651 return ret;
2655 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2656 * translation table
2657 * @bat_priv: the bat priv with all the soft interface information
2658 * @tt_data: tt data containing the tt request information
2659 * @req_src: mac address of tt request sender
2661 * Returns true if tt request reply was sent, false otherwise.
2663 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2664 struct batadv_tvlv_tt_data *tt_data,
2665 uint8_t *req_src)
2667 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2668 struct batadv_hard_iface *primary_if = NULL;
2669 struct batadv_tvlv_tt_change *tt_change;
2670 struct batadv_orig_node *orig_node;
2671 uint8_t my_ttvn, req_ttvn;
2672 uint16_t tvlv_len;
2673 bool full_table;
2674 int32_t tt_len;
2676 batadv_dbg(BATADV_DBG_TT, bat_priv,
2677 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2678 req_src, tt_data->ttvn,
2679 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2681 spin_lock_bh(&bat_priv->tt.commit_lock);
2683 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
2684 req_ttvn = tt_data->ttvn;
2686 orig_node = batadv_orig_hash_find(bat_priv, req_src);
2687 if (!orig_node)
2688 goto out;
2690 primary_if = batadv_primary_if_get_selected(bat_priv);
2691 if (!primary_if)
2692 goto out;
2694 /* If the full table has been explicitly requested or the gap
2695 * is too big send the whole local translation table
2697 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2698 !bat_priv->tt.last_changeset)
2699 full_table = true;
2700 else
2701 full_table = false;
2703 /* TT fragmentation hasn't been implemented yet, so send as many
2704 * TT entries fit a single packet as possible only
2706 if (!full_table) {
2707 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2709 tt_len = bat_priv->tt.last_changeset_len;
2710 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2711 &tvlv_tt_data,
2712 &tt_change,
2713 &tt_len);
2714 if (!tt_len)
2715 goto unlock;
2717 /* Copy the last orig_node's OGM buffer */
2718 memcpy(tt_change, bat_priv->tt.last_changeset,
2719 bat_priv->tt.last_changeset_len);
2720 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2721 } else {
2722 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
2724 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2725 * in the initial part
2727 tt_len = -1;
2728 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2729 &tvlv_tt_data,
2730 &tt_change,
2731 &tt_len);
2732 if (!tt_len)
2733 goto out;
2735 /* fill the rest of the tvlv with the real TT entries */
2736 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2737 tt_change, tt_len,
2738 batadv_tt_local_valid, NULL);
2741 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2742 tvlv_tt_data->ttvn = req_ttvn;
2744 if (full_table)
2745 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2747 batadv_dbg(BATADV_DBG_TT, bat_priv,
2748 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2749 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2751 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2753 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2754 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2755 tvlv_len);
2757 goto out;
2759 unlock:
2760 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2761 out:
2762 spin_unlock_bh(&bat_priv->tt.commit_lock);
2763 if (orig_node)
2764 batadv_orig_node_free_ref(orig_node);
2765 if (primary_if)
2766 batadv_hardif_free_ref(primary_if);
2767 kfree(tvlv_tt_data);
2768 /* The packet was for this host, so it doesn't need to be re-routed */
2769 return true;
2773 * batadv_send_tt_response - send reply to tt request
2774 * @bat_priv: the bat priv with all the soft interface information
2775 * @tt_data: tt data containing the tt request information
2776 * @req_src: mac address of tt request sender
2777 * @req_dst: mac address of tt request recipient
2779 * Returns true if tt request reply was sent, false otherwise.
2781 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2782 struct batadv_tvlv_tt_data *tt_data,
2783 uint8_t *req_src, uint8_t *req_dst)
2785 if (batadv_is_my_mac(bat_priv, req_dst))
2786 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2787 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2788 req_dst);
2791 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2792 struct batadv_orig_node *orig_node,
2793 struct batadv_tvlv_tt_change *tt_change,
2794 uint16_t tt_num_changes, uint8_t ttvn)
2796 int i;
2797 int roams;
2799 for (i = 0; i < tt_num_changes; i++) {
2800 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2801 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2802 batadv_tt_global_del(bat_priv, orig_node,
2803 (tt_change + i)->addr,
2804 ntohs((tt_change + i)->vid),
2805 "tt removed by changes",
2806 roams);
2807 } else {
2808 if (!batadv_tt_global_add(bat_priv, orig_node,
2809 (tt_change + i)->addr,
2810 ntohs((tt_change + i)->vid),
2811 (tt_change + i)->flags, ttvn))
2812 /* In case of problem while storing a
2813 * global_entry, we stop the updating
2814 * procedure without committing the
2815 * ttvn change. This will avoid to send
2816 * corrupted data on tt_request
2818 return;
2821 set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2824 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2825 struct batadv_tvlv_tt_change *tt_change,
2826 uint8_t ttvn, uint8_t *resp_src,
2827 uint16_t num_entries)
2829 struct batadv_orig_node *orig_node;
2831 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2832 if (!orig_node)
2833 goto out;
2835 /* Purge the old table first.. */
2836 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2837 "Received full table");
2839 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2840 ttvn);
2842 spin_lock_bh(&orig_node->tt_buff_lock);
2843 kfree(orig_node->tt_buff);
2844 orig_node->tt_buff_len = 0;
2845 orig_node->tt_buff = NULL;
2846 spin_unlock_bh(&orig_node->tt_buff_lock);
2848 atomic_set(&orig_node->last_ttvn, ttvn);
2850 out:
2851 if (orig_node)
2852 batadv_orig_node_free_ref(orig_node);
2855 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2856 struct batadv_orig_node *orig_node,
2857 uint16_t tt_num_changes, uint8_t ttvn,
2858 struct batadv_tvlv_tt_change *tt_change)
2860 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2861 tt_num_changes, ttvn);
2863 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2864 batadv_tt_len(tt_num_changes));
2865 atomic_set(&orig_node->last_ttvn, ttvn);
2869 * batadv_is_my_client - check if a client is served by the local node
2870 * @bat_priv: the bat priv with all the soft interface information
2871 * @addr: the mac address of the client to check
2872 * @vid: VLAN identifier
2874 * Returns true if the client is served by this node, false otherwise.
2876 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2877 unsigned short vid)
2879 struct batadv_tt_local_entry *tt_local_entry;
2880 bool ret = false;
2882 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2883 if (!tt_local_entry)
2884 goto out;
2885 /* Check if the client has been logically deleted (but is kept for
2886 * consistency purpose)
2888 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2889 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2890 goto out;
2891 ret = true;
2892 out:
2893 if (tt_local_entry)
2894 batadv_tt_local_entry_free_ref(tt_local_entry);
2895 return ret;
2899 * batadv_handle_tt_response - process incoming tt reply
2900 * @bat_priv: the bat priv with all the soft interface information
2901 * @tt_data: tt data containing the tt request information
2902 * @resp_src: mac address of tt reply sender
2903 * @num_entries: number of tt change entries appended to the tt data
2905 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2906 struct batadv_tvlv_tt_data *tt_data,
2907 uint8_t *resp_src, uint16_t num_entries)
2909 struct batadv_tt_req_node *node, *safe;
2910 struct batadv_orig_node *orig_node = NULL;
2911 struct batadv_tvlv_tt_change *tt_change;
2912 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2913 uint16_t change_offset;
2915 batadv_dbg(BATADV_DBG_TT, bat_priv,
2916 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2917 resp_src, tt_data->ttvn, num_entries,
2918 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2920 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2921 if (!orig_node)
2922 goto out;
2924 spin_lock_bh(&orig_node->tt_lock);
2926 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2927 change_offset *= ntohs(tt_data->num_vlan);
2928 change_offset += sizeof(*tt_data);
2929 tvlv_ptr += change_offset;
2931 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
2932 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2933 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2934 resp_src, num_entries);
2935 } else {
2936 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2937 tt_data->ttvn, tt_change);
2940 /* Recalculate the CRC for this orig_node and store it */
2941 batadv_tt_global_update_crc(bat_priv, orig_node);
2943 spin_unlock_bh(&orig_node->tt_lock);
2945 /* Delete the tt_req_node from pending tt_requests list */
2946 spin_lock_bh(&bat_priv->tt.req_list_lock);
2947 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2948 if (!batadv_compare_eth(node->addr, resp_src))
2949 continue;
2950 list_del(&node->list);
2951 kfree(node);
2954 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2955 out:
2956 if (orig_node)
2957 batadv_orig_node_free_ref(orig_node);
2960 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2962 struct batadv_tt_roam_node *node, *safe;
2964 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2966 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2967 list_del(&node->list);
2968 kfree(node);
2971 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2974 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2976 struct batadv_tt_roam_node *node, *safe;
2978 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2979 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2980 if (!batadv_has_timed_out(node->first_time,
2981 BATADV_ROAMING_MAX_TIME))
2982 continue;
2984 list_del(&node->list);
2985 kfree(node);
2987 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2990 /* This function checks whether the client already reached the
2991 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2992 * will not be sent.
2994 * returns true if the ROAMING_ADV can be sent, false otherwise
2996 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2997 uint8_t *client)
2999 struct batadv_tt_roam_node *tt_roam_node;
3000 bool ret = false;
3002 spin_lock_bh(&bat_priv->tt.roam_list_lock);
3003 /* The new tt_req will be issued only if I'm not waiting for a
3004 * reply from the same orig_node yet
3006 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3007 if (!batadv_compare_eth(tt_roam_node->addr, client))
3008 continue;
3010 if (batadv_has_timed_out(tt_roam_node->first_time,
3011 BATADV_ROAMING_MAX_TIME))
3012 continue;
3014 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3015 /* Sorry, you roamed too many times! */
3016 goto unlock;
3017 ret = true;
3018 break;
3021 if (!ret) {
3022 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3023 if (!tt_roam_node)
3024 goto unlock;
3026 tt_roam_node->first_time = jiffies;
3027 atomic_set(&tt_roam_node->counter,
3028 BATADV_ROAMING_MAX_COUNT - 1);
3029 ether_addr_copy(tt_roam_node->addr, client);
3031 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3032 ret = true;
3035 unlock:
3036 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3037 return ret;
3041 * batadv_send_roam_adv - send a roaming advertisement message
3042 * @bat_priv: the bat priv with all the soft interface information
3043 * @client: mac address of the roaming client
3044 * @vid: VLAN identifier
3045 * @orig_node: message destination
3047 * Send a ROAMING_ADV message to the node which was previously serving this
3048 * client. This is done to inform the node that from now on all traffic destined
3049 * for this particular roamed client has to be forwarded to the sender of the
3050 * roaming message.
3052 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
3053 unsigned short vid,
3054 struct batadv_orig_node *orig_node)
3056 struct batadv_hard_iface *primary_if;
3057 struct batadv_tvlv_roam_adv tvlv_roam;
3059 primary_if = batadv_primary_if_get_selected(bat_priv);
3060 if (!primary_if)
3061 goto out;
3063 /* before going on we have to check whether the client has
3064 * already roamed to us too many times
3066 if (!batadv_tt_check_roam_count(bat_priv, client))
3067 goto out;
3069 batadv_dbg(BATADV_DBG_TT, bat_priv,
3070 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3071 orig_node->orig, client, BATADV_PRINT_VID(vid));
3073 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3075 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3076 tvlv_roam.vid = htons(vid);
3078 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3079 orig_node->orig, BATADV_TVLV_ROAM, 1,
3080 &tvlv_roam, sizeof(tvlv_roam));
3082 out:
3083 if (primary_if)
3084 batadv_hardif_free_ref(primary_if);
3087 static void batadv_tt_purge(struct work_struct *work)
3089 struct delayed_work *delayed_work;
3090 struct batadv_priv_tt *priv_tt;
3091 struct batadv_priv *bat_priv;
3093 delayed_work = container_of(work, struct delayed_work, work);
3094 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3095 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3097 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3098 batadv_tt_global_purge(bat_priv);
3099 batadv_tt_req_purge(bat_priv);
3100 batadv_tt_roam_purge(bat_priv);
3102 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3103 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3106 void batadv_tt_free(struct batadv_priv *bat_priv)
3108 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3109 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3111 cancel_delayed_work_sync(&bat_priv->tt.work);
3113 batadv_tt_local_table_free(bat_priv);
3114 batadv_tt_global_table_free(bat_priv);
3115 batadv_tt_req_list_free(bat_priv);
3116 batadv_tt_changes_list_free(bat_priv);
3117 batadv_tt_roam_list_free(bat_priv);
3119 kfree(bat_priv->tt.last_changeset);
3123 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3124 * table and possibly count them in the TT size
3125 * @bat_priv: the bat priv with all the soft interface information
3126 * @flags: the flag to switch
3127 * @enable: whether to set or unset the flag
3128 * @count: whether to increase the TT size by the number of changed entries
3130 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3131 uint16_t flags, bool enable, bool count)
3133 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3134 struct batadv_tt_common_entry *tt_common_entry;
3135 uint16_t changed_num = 0;
3136 struct hlist_head *head;
3137 uint32_t i;
3139 if (!hash)
3140 return;
3142 for (i = 0; i < hash->size; i++) {
3143 head = &hash->table[i];
3145 rcu_read_lock();
3146 hlist_for_each_entry_rcu(tt_common_entry,
3147 head, hash_entry) {
3148 if (enable) {
3149 if ((tt_common_entry->flags & flags) == flags)
3150 continue;
3151 tt_common_entry->flags |= flags;
3152 } else {
3153 if (!(tt_common_entry->flags & flags))
3154 continue;
3155 tt_common_entry->flags &= ~flags;
3157 changed_num++;
3159 if (!count)
3160 continue;
3162 batadv_tt_local_size_inc(bat_priv,
3163 tt_common_entry->vid);
3165 rcu_read_unlock();
3169 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3170 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3172 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3173 struct batadv_tt_common_entry *tt_common;
3174 struct batadv_tt_local_entry *tt_local;
3175 struct batadv_softif_vlan *vlan;
3176 struct hlist_node *node_tmp;
3177 struct hlist_head *head;
3178 spinlock_t *list_lock; /* protects write access to the hash lists */
3179 uint32_t i;
3181 if (!hash)
3182 return;
3184 for (i = 0; i < hash->size; i++) {
3185 head = &hash->table[i];
3186 list_lock = &hash->list_locks[i];
3188 spin_lock_bh(list_lock);
3189 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3190 hash_entry) {
3191 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3192 continue;
3194 batadv_dbg(BATADV_DBG_TT, bat_priv,
3195 "Deleting local tt entry (%pM, vid: %d): pending\n",
3196 tt_common->addr,
3197 BATADV_PRINT_VID(tt_common->vid));
3199 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3200 hlist_del_rcu(&tt_common->hash_entry);
3201 tt_local = container_of(tt_common,
3202 struct batadv_tt_local_entry,
3203 common);
3205 /* decrease the reference held for this vlan */
3206 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
3207 if (vlan) {
3208 batadv_softif_vlan_free_ref(vlan);
3209 batadv_softif_vlan_free_ref(vlan);
3212 batadv_tt_local_entry_free_ref(tt_local);
3214 spin_unlock_bh(list_lock);
3219 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3220 * which have been queued in the time since the last commit
3221 * @bat_priv: the bat priv with all the soft interface information
3223 * Caller must hold tt->commit_lock.
3225 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3227 /* Update multicast addresses in local translation table */
3228 batadv_mcast_mla_update(bat_priv);
3230 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3231 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3232 batadv_tt_tvlv_container_update(bat_priv);
3233 return;
3236 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3238 batadv_tt_local_purge_pending_clients(bat_priv);
3239 batadv_tt_local_update_crc(bat_priv);
3241 /* Increment the TTVN only once per OGM interval */
3242 atomic_inc(&bat_priv->tt.vn);
3243 batadv_dbg(BATADV_DBG_TT, bat_priv,
3244 "Local changes committed, updating to ttvn %u\n",
3245 (uint8_t)atomic_read(&bat_priv->tt.vn));
3247 /* reset the sending counter */
3248 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3249 batadv_tt_tvlv_container_update(bat_priv);
3253 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3254 * have been queued in the time since the last commit
3255 * @bat_priv: the bat priv with all the soft interface information
3257 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3259 spin_lock_bh(&bat_priv->tt.commit_lock);
3260 batadv_tt_local_commit_changes_nolock(bat_priv);
3261 spin_unlock_bh(&bat_priv->tt.commit_lock);
3264 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
3265 uint8_t *dst, unsigned short vid)
3267 struct batadv_tt_local_entry *tt_local_entry = NULL;
3268 struct batadv_tt_global_entry *tt_global_entry = NULL;
3269 struct batadv_softif_vlan *vlan;
3270 bool ret = false;
3272 vlan = batadv_softif_vlan_get(bat_priv, vid);
3273 if (!vlan || !atomic_read(&vlan->ap_isolation))
3274 goto out;
3276 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3277 if (!tt_local_entry)
3278 goto out;
3280 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3281 if (!tt_global_entry)
3282 goto out;
3284 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3285 goto out;
3287 ret = true;
3289 out:
3290 if (vlan)
3291 batadv_softif_vlan_free_ref(vlan);
3292 if (tt_global_entry)
3293 batadv_tt_global_entry_free_ref(tt_global_entry);
3294 if (tt_local_entry)
3295 batadv_tt_local_entry_free_ref(tt_local_entry);
3296 return ret;
3300 * batadv_tt_update_orig - update global translation table with new tt
3301 * information received via ogms
3302 * @bat_priv: the bat priv with all the soft interface information
3303 * @orig: the orig_node of the ogm
3304 * @tt_vlan: pointer to the first tvlv VLAN entry
3305 * @tt_num_vlan: number of tvlv VLAN entries
3306 * @tt_change: pointer to the first entry in the TT buffer
3307 * @tt_num_changes: number of tt changes inside the tt buffer
3308 * @ttvn: translation table version number of this changeset
3309 * @tt_crc: crc32 checksum of orig node's translation table
3311 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3312 struct batadv_orig_node *orig_node,
3313 const void *tt_buff, uint16_t tt_num_vlan,
3314 struct batadv_tvlv_tt_change *tt_change,
3315 uint16_t tt_num_changes, uint8_t ttvn)
3317 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
3318 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3319 bool full_table = true;
3320 bool has_tt_init;
3322 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3323 has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3324 &orig_node->capa_initialized);
3326 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3327 * increased by one -> we can apply the attached changes
3329 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3330 /* the OGM could not contain the changes due to their size or
3331 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3332 * times.
3333 * In this case send a tt request
3335 if (!tt_num_changes) {
3336 full_table = false;
3337 goto request_table;
3340 spin_lock_bh(&orig_node->tt_lock);
3342 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3343 ttvn, tt_change);
3345 /* Even if we received the precomputed crc with the OGM, we
3346 * prefer to recompute it to spot any possible inconsistency
3347 * in the global table
3349 batadv_tt_global_update_crc(bat_priv, orig_node);
3351 spin_unlock_bh(&orig_node->tt_lock);
3353 /* The ttvn alone is not enough to guarantee consistency
3354 * because a single value could represent different states
3355 * (due to the wrap around). Thus a node has to check whether
3356 * the resulting table (after applying the changes) is still
3357 * consistent or not. E.g. a node could disconnect while its
3358 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3359 * checking the CRC value is mandatory to detect the
3360 * inconsistency
3362 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3363 tt_num_vlan))
3364 goto request_table;
3365 } else {
3366 /* if we missed more than one change or our tables are not
3367 * in sync anymore -> request fresh tt data
3369 if (!has_tt_init || ttvn != orig_ttvn ||
3370 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3371 tt_num_vlan)) {
3372 request_table:
3373 batadv_dbg(BATADV_DBG_TT, bat_priv,
3374 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3375 orig_node->orig, ttvn, orig_ttvn,
3376 tt_num_changes);
3377 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3378 tt_vlan, tt_num_vlan,
3379 full_table);
3380 return;
3386 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3387 * @bat_priv: the bat priv with all the soft interface information
3388 * @addr: the mac address of the client to check
3389 * @vid: VLAN identifier
3391 * Returns true if we know that the client has moved from its old originator
3392 * to another one. This entry is still kept for consistency purposes and will be
3393 * deleted later by a DEL or because of timeout
3395 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3396 uint8_t *addr, unsigned short vid)
3398 struct batadv_tt_global_entry *tt_global_entry;
3399 bool ret = false;
3401 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3402 if (!tt_global_entry)
3403 goto out;
3405 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3406 batadv_tt_global_entry_free_ref(tt_global_entry);
3407 out:
3408 return ret;
3412 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3413 * @bat_priv: the bat priv with all the soft interface information
3414 * @addr: the mac address of the local client to query
3415 * @vid: VLAN identifier
3417 * Returns true if the local client is known to be roaming (it is not served by
3418 * this node anymore) or not. If yes, the client is still present in the table
3419 * to keep the latter consistent with the node TTVN
3421 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3422 uint8_t *addr, unsigned short vid)
3424 struct batadv_tt_local_entry *tt_local_entry;
3425 bool ret = false;
3427 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3428 if (!tt_local_entry)
3429 goto out;
3431 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3432 batadv_tt_local_entry_free_ref(tt_local_entry);
3433 out:
3434 return ret;
3437 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3438 struct batadv_orig_node *orig_node,
3439 const unsigned char *addr,
3440 unsigned short vid)
3442 bool ret = false;
3444 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3445 BATADV_TT_CLIENT_TEMP,
3446 atomic_read(&orig_node->last_ttvn)))
3447 goto out;
3449 batadv_dbg(BATADV_DBG_TT, bat_priv,
3450 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3451 addr, BATADV_PRINT_VID(vid), orig_node->orig);
3452 ret = true;
3453 out:
3454 return ret;
3458 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3459 * maximum packet size that can be transported through the mesh
3460 * @soft_iface: netdev struct of the mesh interface
3462 * Remove entries older than 'timeout' and half timeout if more entries need
3463 * to be removed.
3465 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3467 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3468 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3469 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3470 bool reduced = false;
3472 spin_lock_bh(&bat_priv->tt.commit_lock);
3474 while (true) {
3475 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3476 if (packet_size_max >= table_size)
3477 break;
3479 batadv_tt_local_purge(bat_priv, timeout);
3480 batadv_tt_local_purge_pending_clients(bat_priv);
3482 timeout /= 2;
3483 reduced = true;
3484 net_ratelimited_function(batadv_info, soft_iface,
3485 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3486 packet_size_max);
3489 /* commit these changes immediately, to avoid synchronization problem
3490 * with the TTVN
3492 if (reduced)
3493 batadv_tt_local_commit_changes_nolock(bat_priv);
3495 spin_unlock_bh(&bat_priv->tt.commit_lock);
3499 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3500 * @bat_priv: the bat priv with all the soft interface information
3501 * @orig: the orig_node of the ogm
3502 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3503 * @tvlv_value: tvlv buffer containing the gateway data
3504 * @tvlv_value_len: tvlv buffer length
3506 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3507 struct batadv_orig_node *orig,
3508 uint8_t flags, void *tvlv_value,
3509 uint16_t tvlv_value_len)
3511 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3512 struct batadv_tvlv_tt_change *tt_change;
3513 struct batadv_tvlv_tt_data *tt_data;
3514 uint16_t num_entries, num_vlan;
3516 if (tvlv_value_len < sizeof(*tt_data))
3517 return;
3519 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3520 tvlv_value_len -= sizeof(*tt_data);
3522 num_vlan = ntohs(tt_data->num_vlan);
3524 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3525 return;
3527 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3528 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3529 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3531 num_entries = batadv_tt_entries(tvlv_value_len);
3533 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3534 num_entries, tt_data->ttvn);
3538 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3539 * container
3540 * @bat_priv: the bat priv with all the soft interface information
3541 * @src: mac address of tt tvlv sender
3542 * @dst: mac address of tt tvlv recipient
3543 * @tvlv_value: tvlv buffer containing the tt data
3544 * @tvlv_value_len: tvlv buffer length
3546 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3547 * otherwise.
3549 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3550 uint8_t *src, uint8_t *dst,
3551 void *tvlv_value,
3552 uint16_t tvlv_value_len)
3554 struct batadv_tvlv_tt_data *tt_data;
3555 uint16_t tt_vlan_len, tt_num_entries;
3556 char tt_flag;
3557 bool ret;
3559 if (tvlv_value_len < sizeof(*tt_data))
3560 return NET_RX_SUCCESS;
3562 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3563 tvlv_value_len -= sizeof(*tt_data);
3565 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3566 tt_vlan_len *= ntohs(tt_data->num_vlan);
3568 if (tvlv_value_len < tt_vlan_len)
3569 return NET_RX_SUCCESS;
3571 tvlv_value_len -= tt_vlan_len;
3572 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3574 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3575 case BATADV_TT_REQUEST:
3576 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3578 /* If this node cannot provide a TT response the tt_request is
3579 * forwarded
3581 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3582 if (!ret) {
3583 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3584 tt_flag = 'F';
3585 else
3586 tt_flag = '.';
3588 batadv_dbg(BATADV_DBG_TT, bat_priv,
3589 "Routing TT_REQUEST to %pM [%c]\n",
3590 dst, tt_flag);
3591 /* tvlv API will re-route the packet */
3592 return NET_RX_DROP;
3594 break;
3595 case BATADV_TT_RESPONSE:
3596 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3598 if (batadv_is_my_mac(bat_priv, dst)) {
3599 batadv_handle_tt_response(bat_priv, tt_data,
3600 src, tt_num_entries);
3601 return NET_RX_SUCCESS;
3604 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3605 tt_flag = 'F';
3606 else
3607 tt_flag = '.';
3609 batadv_dbg(BATADV_DBG_TT, bat_priv,
3610 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3612 /* tvlv API will re-route the packet */
3613 return NET_RX_DROP;
3616 return NET_RX_SUCCESS;
3620 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3621 * @bat_priv: the bat priv with all the soft interface information
3622 * @src: mac address of tt tvlv sender
3623 * @dst: mac address of tt tvlv recipient
3624 * @tvlv_value: tvlv buffer containing the tt data
3625 * @tvlv_value_len: tvlv buffer length
3627 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3628 * otherwise.
3630 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3631 uint8_t *src, uint8_t *dst,
3632 void *tvlv_value,
3633 uint16_t tvlv_value_len)
3635 struct batadv_tvlv_roam_adv *roaming_adv;
3636 struct batadv_orig_node *orig_node = NULL;
3638 /* If this node is not the intended recipient of the
3639 * roaming advertisement the packet is forwarded
3640 * (the tvlv API will re-route the packet).
3642 if (!batadv_is_my_mac(bat_priv, dst))
3643 return NET_RX_DROP;
3645 if (tvlv_value_len < sizeof(*roaming_adv))
3646 goto out;
3648 orig_node = batadv_orig_hash_find(bat_priv, src);
3649 if (!orig_node)
3650 goto out;
3652 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3653 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3655 batadv_dbg(BATADV_DBG_TT, bat_priv,
3656 "Received ROAMING_ADV from %pM (client %pM)\n",
3657 src, roaming_adv->client);
3659 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3660 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3661 atomic_read(&orig_node->last_ttvn) + 1);
3663 out:
3664 if (orig_node)
3665 batadv_orig_node_free_ref(orig_node);
3666 return NET_RX_SUCCESS;
3670 * batadv_tt_init - initialise the translation table internals
3671 * @bat_priv: the bat priv with all the soft interface information
3673 * Return 0 on success or negative error number in case of failure.
3675 int batadv_tt_init(struct batadv_priv *bat_priv)
3677 int ret;
3679 /* synchronized flags must be remote */
3680 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3682 ret = batadv_tt_local_init(bat_priv);
3683 if (ret < 0)
3684 return ret;
3686 ret = batadv_tt_global_init(bat_priv);
3687 if (ret < 0)
3688 return ret;
3690 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3691 batadv_tt_tvlv_unicast_handler_v1,
3692 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3694 batadv_tvlv_handler_register(bat_priv, NULL,
3695 batadv_roam_tvlv_unicast_handler_v1,
3696 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3698 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3699 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3700 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3702 return 1;
3706 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3707 * @bat_priv: the bat priv with all the soft interface information
3708 * @addr: the mac address of the client
3709 * @vid: the identifier of the VLAN where this client is connected
3711 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3712 * otherwise
3714 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3715 const uint8_t *addr, unsigned short vid)
3717 struct batadv_tt_global_entry *tt;
3718 bool ret;
3720 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3721 if (!tt)
3722 return false;
3724 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3726 batadv_tt_global_entry_free_ref(tt);
3728 return ret;