Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6.git] / net / batman-adv / translation-table.c
blob4add57d4857f11e5ea9edfa13aae1f46dab3e4f9
1 /* Copyright (C) 2007-2013 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA
20 #include "main.h"
21 #include "translation-table.h"
22 #include "soft-interface.h"
23 #include "hard-interface.h"
24 #include "send.h"
25 #include "hash.h"
26 #include "originator.h"
27 #include "routing.h"
28 #include "bridge_loop_avoidance.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 (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
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 memcpy(to_search.addr, addr, ETH_ALEN);
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);
197 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
199 struct batadv_tt_orig_list_entry *orig_entry;
201 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
203 /* We are in an rcu callback here, therefore we cannot use
204 * batadv_orig_node_free_ref() and its call_rcu():
205 * An rcu_barrier() wouldn't wait for that to finish
207 batadv_orig_node_free_ref_now(orig_entry->orig_node);
208 kfree(orig_entry);
212 * batadv_tt_local_size_mod - change the size by v of the local table identified
213 * by vid
214 * @bat_priv: the bat priv with all the soft interface information
215 * @vid: the VLAN identifier of the sub-table to change
216 * @v: the amount to sum to the local table size
218 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
219 unsigned short vid, int v)
221 struct batadv_softif_vlan *vlan;
223 vlan = batadv_softif_vlan_get(bat_priv, vid);
224 if (!vlan)
225 return;
227 atomic_add(v, &vlan->tt.num_entries);
229 batadv_softif_vlan_free_ref(vlan);
233 * batadv_tt_local_size_inc - increase by one the local table size for the given
234 * vid
235 * @bat_priv: the bat priv with all the soft interface information
236 * @vid: the VLAN identifier
238 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
239 unsigned short vid)
241 batadv_tt_local_size_mod(bat_priv, vid, 1);
245 * batadv_tt_local_size_dec - decrease by one the local table size for the given
246 * vid
247 * @bat_priv: the bat priv with all the soft interface information
248 * @vid: the VLAN identifier
250 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
251 unsigned short vid)
253 batadv_tt_local_size_mod(bat_priv, vid, -1);
257 * batadv_tt_global_size_mod - change the size by v of the local table
258 * identified by vid
259 * @bat_priv: the bat priv with all the soft interface information
260 * @vid: the VLAN identifier
261 * @v: the amount to sum to the global table size
263 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
264 unsigned short vid, int v)
266 struct batadv_orig_node_vlan *vlan;
268 vlan = batadv_orig_node_vlan_new(orig_node, vid);
269 if (!vlan)
270 return;
272 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
273 spin_lock_bh(&orig_node->vlan_list_lock);
274 list_del_rcu(&vlan->list);
275 spin_unlock_bh(&orig_node->vlan_list_lock);
276 batadv_orig_node_vlan_free_ref(vlan);
279 batadv_orig_node_vlan_free_ref(vlan);
283 * batadv_tt_global_size_inc - increase by one the global table size for the
284 * given vid
285 * @orig_node: the originator which global table size has to be decreased
286 * @vid: the vlan identifier
288 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
289 unsigned short vid)
291 batadv_tt_global_size_mod(orig_node, vid, 1);
295 * batadv_tt_global_size_dec - decrease by one the global table size for the
296 * given vid
297 * @orig_node: the originator which global table size has to be decreased
298 * @vid: the vlan identifier
300 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
301 unsigned short vid)
303 batadv_tt_global_size_mod(orig_node, vid, -1);
306 static void
307 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
309 if (!atomic_dec_and_test(&orig_entry->refcount))
310 return;
312 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
316 * batadv_tt_local_event - store a local TT event (ADD/DEL)
317 * @bat_priv: the bat priv with all the soft interface information
318 * @tt_local_entry: the TT entry involved in the event
319 * @event_flags: flags to store in the event structure
321 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
322 struct batadv_tt_local_entry *tt_local_entry,
323 uint8_t event_flags)
325 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
326 struct batadv_tt_common_entry *common = &tt_local_entry->common;
327 uint8_t flags = common->flags | event_flags;
328 bool event_removed = false;
329 bool del_op_requested, del_op_entry;
331 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
332 if (!tt_change_node)
333 return;
335 tt_change_node->change.flags = flags;
336 tt_change_node->change.reserved = 0;
337 memcpy(tt_change_node->change.addr, common->addr, ETH_ALEN);
338 tt_change_node->change.vid = htons(common->vid);
340 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
342 /* check for ADD+DEL or DEL+ADD events */
343 spin_lock_bh(&bat_priv->tt.changes_list_lock);
344 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
345 list) {
346 if (!batadv_compare_eth(entry->change.addr, common->addr))
347 continue;
349 /* DEL+ADD in the same orig interval have no effect and can be
350 * removed to avoid silly behaviour on the receiver side. The
351 * other way around (ADD+DEL) can happen in case of roaming of
352 * a client still in the NEW state. Roaming of NEW clients is
353 * now possible due to automatically recognition of "temporary"
354 * clients
356 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
357 if (!del_op_requested && del_op_entry)
358 goto del;
359 if (del_op_requested && !del_op_entry)
360 goto del;
362 /* this is a second add in the same originator interval. It
363 * means that flags have been changed: update them!
365 if (!del_op_requested && !del_op_entry)
366 entry->change.flags = flags;
368 continue;
369 del:
370 list_del(&entry->list);
371 kfree(entry);
372 kfree(tt_change_node);
373 event_removed = true;
374 goto unlock;
377 /* track the change in the OGMinterval list */
378 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
380 unlock:
381 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
383 if (event_removed)
384 atomic_dec(&bat_priv->tt.local_changes);
385 else
386 atomic_inc(&bat_priv->tt.local_changes);
390 * batadv_tt_len - compute length in bytes of given number of tt changes
391 * @changes_num: number of tt changes
393 * Returns computed length in bytes.
395 static int batadv_tt_len(int changes_num)
397 return changes_num * sizeof(struct batadv_tvlv_tt_change);
401 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
402 * @tt_len: available space
404 * Returns the number of entries.
406 static uint16_t batadv_tt_entries(uint16_t tt_len)
408 return tt_len / batadv_tt_len(1);
412 * batadv_tt_local_table_transmit_size - calculates the local translation table
413 * size when transmitted over the air
414 * @bat_priv: the bat priv with all the soft interface information
416 * Returns local translation table size in bytes.
418 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
420 uint16_t num_vlan = 0, tt_local_entries = 0;
421 struct batadv_softif_vlan *vlan;
422 int hdr_size;
424 rcu_read_lock();
425 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
426 num_vlan++;
427 tt_local_entries += atomic_read(&vlan->tt.num_entries);
429 rcu_read_unlock();
431 /* header size of tvlv encapsulated tt response payload */
432 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
433 hdr_size += sizeof(struct batadv_tvlv_hdr);
434 hdr_size += sizeof(struct batadv_tvlv_tt_data);
435 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
437 return hdr_size + batadv_tt_len(tt_local_entries);
440 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
442 if (bat_priv->tt.local_hash)
443 return 0;
445 bat_priv->tt.local_hash = batadv_hash_new(1024);
447 if (!bat_priv->tt.local_hash)
448 return -ENOMEM;
450 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
451 &batadv_tt_local_hash_lock_class_key);
453 return 0;
456 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
457 struct batadv_tt_global_entry *tt_global,
458 const char *message)
460 batadv_dbg(BATADV_DBG_TT, bat_priv,
461 "Deleting global tt entry %pM (vid: %d): %s\n",
462 tt_global->common.addr,
463 BATADV_PRINT_VID(tt_global->common.vid), message);
465 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
466 batadv_choose_tt, &tt_global->common);
467 batadv_tt_global_entry_free_ref(tt_global);
471 * batadv_tt_local_add - add a new client to the local table or update an
472 * existing client
473 * @soft_iface: netdev struct of the mesh interface
474 * @addr: the mac address of the client to add
475 * @vid: VLAN identifier
476 * @ifindex: index of the interface where the client is connected to (useful to
477 * identify wireless clients)
479 * Returns true if the client was successfully added, false otherwise.
481 bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
482 unsigned short vid, int ifindex)
484 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
485 struct batadv_tt_local_entry *tt_local;
486 struct batadv_tt_global_entry *tt_global;
487 struct net_device *in_dev = NULL;
488 struct hlist_head *head;
489 struct batadv_tt_orig_list_entry *orig_entry;
490 int hash_added, table_size, packet_size_max;
491 bool ret = false, roamed_back = false;
492 uint8_t remote_flags;
494 if (ifindex != BATADV_NULL_IFINDEX)
495 in_dev = dev_get_by_index(&init_net, ifindex);
497 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
498 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
500 if (tt_local) {
501 tt_local->last_seen = jiffies;
502 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
503 batadv_dbg(BATADV_DBG_TT, bat_priv,
504 "Re-adding pending client %pM (vid: %d)\n",
505 addr, BATADV_PRINT_VID(vid));
506 /* whatever the reason why the PENDING flag was set,
507 * this is a client which was enqueued to be removed in
508 * this orig_interval. Since it popped up again, the
509 * flag can be reset like it was never enqueued
511 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
512 goto add_event;
515 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
516 batadv_dbg(BATADV_DBG_TT, bat_priv,
517 "Roaming client %pM (vid: %d) came back to its original location\n",
518 addr, BATADV_PRINT_VID(vid));
519 /* the ROAM flag is set because this client roamed away
520 * and the node got a roaming_advertisement message. Now
521 * that the client popped up again at its original
522 * location such flag can be unset
524 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
525 roamed_back = true;
527 goto check_roaming;
530 /* Ignore the client if we cannot send it in a full table response. */
531 table_size = batadv_tt_local_table_transmit_size(bat_priv);
532 table_size += batadv_tt_len(1);
533 packet_size_max = atomic_read(&bat_priv->packet_size_max);
534 if (table_size > packet_size_max) {
535 net_ratelimited_function(batadv_info, soft_iface,
536 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
537 table_size, packet_size_max, addr);
538 goto out;
541 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
542 if (!tt_local)
543 goto out;
545 batadv_dbg(BATADV_DBG_TT, bat_priv,
546 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
547 addr, BATADV_PRINT_VID(vid),
548 (uint8_t)atomic_read(&bat_priv->tt.vn));
550 memcpy(tt_local->common.addr, addr, ETH_ALEN);
551 /* The local entry has to be marked as NEW to avoid to send it in
552 * a full table response going out before the next ttvn increment
553 * (consistency check)
555 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
556 tt_local->common.vid = vid;
557 if (batadv_is_wifi_netdev(in_dev))
558 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
559 atomic_set(&tt_local->common.refcount, 2);
560 tt_local->last_seen = jiffies;
561 tt_local->common.added_at = tt_local->last_seen;
563 /* the batman interface mac address should never be purged */
564 if (batadv_compare_eth(addr, soft_iface->dev_addr))
565 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
567 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
568 batadv_choose_tt, &tt_local->common,
569 &tt_local->common.hash_entry);
571 if (unlikely(hash_added != 0)) {
572 /* remove the reference for the hash */
573 batadv_tt_local_entry_free_ref(tt_local);
574 goto out;
577 add_event:
578 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
580 check_roaming:
581 /* Check whether it is a roaming, but don't do anything if the roaming
582 * process has already been handled
584 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
585 /* These node are probably going to update their tt table */
586 head = &tt_global->orig_list;
587 rcu_read_lock();
588 hlist_for_each_entry_rcu(orig_entry, head, list) {
589 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
590 tt_global->common.vid,
591 orig_entry->orig_node);
593 rcu_read_unlock();
594 if (roamed_back) {
595 batadv_tt_global_free(bat_priv, tt_global,
596 "Roaming canceled");
597 tt_global = NULL;
598 } else {
599 /* The global entry has to be marked as ROAMING and
600 * has to be kept for consistency purpose
602 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
603 tt_global->roam_at = jiffies;
607 /* store the current remote flags before altering them. This helps
608 * understanding is flags are changing or not
610 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
612 if (batadv_is_wifi_netdev(in_dev))
613 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
614 else
615 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
617 /* if any "dynamic" flag has been modified, resend an ADD event for this
618 * entry so that all the nodes can get the new flags
620 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
621 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
623 ret = true;
624 out:
625 if (in_dev)
626 dev_put(in_dev);
627 if (tt_local)
628 batadv_tt_local_entry_free_ref(tt_local);
629 if (tt_global)
630 batadv_tt_global_entry_free_ref(tt_global);
631 return ret;
635 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
636 * within a TT Response directed to another node
637 * @orig_node: originator for which the TT data has to be prepared
638 * @tt_data: uninitialised pointer to the address of the TVLV buffer
639 * @tt_change: uninitialised pointer to the address of the area where the TT
640 * changed can be stored
641 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
642 * function reserves the amount of space needed to send the entire global TT
643 * table. In case of success the value is updated with the real amount of
644 * reserved bytes
646 * Allocate the needed amount of memory for the entire TT TVLV and write its
647 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
648 * objects, one per active VLAN served by the originator node.
650 * Return the size of the allocated buffer or 0 in case of failure.
652 static uint16_t
653 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
654 struct batadv_tvlv_tt_data **tt_data,
655 struct batadv_tvlv_tt_change **tt_change,
656 int32_t *tt_len)
658 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
659 struct batadv_tvlv_tt_vlan_data *tt_vlan;
660 struct batadv_orig_node_vlan *vlan;
661 uint8_t *tt_change_ptr;
663 rcu_read_lock();
664 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
665 num_vlan++;
666 num_entries += atomic_read(&vlan->tt.num_entries);
669 change_offset = sizeof(**tt_data);
670 change_offset += num_vlan * sizeof(*tt_vlan);
672 /* if tt_len is negative, allocate the space needed by the full table */
673 if (*tt_len < 0)
674 *tt_len = batadv_tt_len(num_entries);
676 tvlv_len = *tt_len;
677 tvlv_len += change_offset;
679 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
680 if (!*tt_data) {
681 *tt_len = 0;
682 goto out;
685 (*tt_data)->flags = BATADV_NO_FLAGS;
686 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
687 (*tt_data)->num_vlan = htons(num_vlan);
689 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
690 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
691 tt_vlan->vid = htons(vlan->vid);
692 tt_vlan->crc = htonl(vlan->tt.crc);
694 tt_vlan++;
697 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
698 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
700 out:
701 rcu_read_unlock();
702 return tvlv_len;
706 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
707 * node
708 * @bat_priv: the bat priv with all the soft interface information
709 * @tt_data: uninitialised pointer to the address of the TVLV buffer
710 * @tt_change: uninitialised pointer to the address of the area where the TT
711 * changes can be stored
712 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
713 * function reserves the amount of space needed to send the entire local TT
714 * table. In case of success the value is updated with the real amount of
715 * reserved bytes
717 * Allocate the needed amount of memory for the entire TT TVLV and write its
718 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
719 * objects, one per active VLAN.
721 * Return the size of the allocated buffer or 0 in case of failure.
723 static uint16_t
724 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
725 struct batadv_tvlv_tt_data **tt_data,
726 struct batadv_tvlv_tt_change **tt_change,
727 int32_t *tt_len)
729 struct batadv_tvlv_tt_vlan_data *tt_vlan;
730 struct batadv_softif_vlan *vlan;
731 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
732 uint8_t *tt_change_ptr;
733 int change_offset;
735 rcu_read_lock();
736 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
737 num_vlan++;
738 num_entries += atomic_read(&vlan->tt.num_entries);
741 change_offset = sizeof(**tt_data);
742 change_offset += num_vlan * sizeof(*tt_vlan);
744 /* if tt_len is negative, allocate the space needed by the full table */
745 if (*tt_len < 0)
746 *tt_len = batadv_tt_len(num_entries);
748 tvlv_len = *tt_len;
749 tvlv_len += change_offset;
751 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
752 if (!*tt_data) {
753 tvlv_len = 0;
754 goto out;
757 (*tt_data)->flags = BATADV_NO_FLAGS;
758 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
759 (*tt_data)->num_vlan = htons(num_vlan);
761 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
762 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
763 tt_vlan->vid = htons(vlan->vid);
764 tt_vlan->crc = htonl(vlan->tt.crc);
766 tt_vlan++;
769 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
770 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
772 out:
773 rcu_read_unlock();
774 return tvlv_len;
778 * batadv_tt_tvlv_container_update - update the translation table tvlv container
779 * after local tt changes have been committed
780 * @bat_priv: the bat priv with all the soft interface information
782 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
784 struct batadv_tt_change_node *entry, *safe;
785 struct batadv_tvlv_tt_data *tt_data;
786 struct batadv_tvlv_tt_change *tt_change;
787 int tt_diff_len, tt_change_len = 0;
788 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
789 uint16_t tvlv_len;
791 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
792 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
794 /* if we have too many changes for one packet don't send any
795 * and wait for the tt table request which will be fragmented
797 if (tt_diff_len > bat_priv->soft_iface->mtu)
798 tt_diff_len = 0;
800 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
801 &tt_change, &tt_diff_len);
802 if (!tvlv_len)
803 return;
805 tt_data->flags = BATADV_TT_OGM_DIFF;
807 if (tt_diff_len == 0)
808 goto container_register;
810 spin_lock_bh(&bat_priv->tt.changes_list_lock);
811 atomic_set(&bat_priv->tt.local_changes, 0);
813 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
814 list) {
815 if (tt_diff_entries_count < tt_diff_entries_num) {
816 memcpy(tt_change + tt_diff_entries_count,
817 &entry->change,
818 sizeof(struct batadv_tvlv_tt_change));
819 tt_diff_entries_count++;
821 list_del(&entry->list);
822 kfree(entry);
824 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
826 /* Keep the buffer for possible tt_request */
827 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
828 kfree(bat_priv->tt.last_changeset);
829 bat_priv->tt.last_changeset_len = 0;
830 bat_priv->tt.last_changeset = NULL;
831 tt_change_len = batadv_tt_len(tt_diff_entries_count);
832 /* check whether this new OGM has no changes due to size problems */
833 if (tt_diff_entries_count > 0) {
834 /* if kmalloc() fails we will reply with the full table
835 * instead of providing the diff
837 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
838 if (bat_priv->tt.last_changeset) {
839 memcpy(bat_priv->tt.last_changeset,
840 tt_change, tt_change_len);
841 bat_priv->tt.last_changeset_len = tt_diff_len;
844 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
846 container_register:
847 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
848 tvlv_len);
849 kfree(tt_data);
852 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
854 struct net_device *net_dev = (struct net_device *)seq->private;
855 struct batadv_priv *bat_priv = netdev_priv(net_dev);
856 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
857 struct batadv_tt_common_entry *tt_common_entry;
858 struct batadv_tt_local_entry *tt_local;
859 struct batadv_hard_iface *primary_if;
860 struct batadv_softif_vlan *vlan;
861 struct hlist_head *head;
862 unsigned short vid;
863 uint32_t i;
864 int last_seen_secs;
865 int last_seen_msecs;
866 unsigned long last_seen_jiffies;
867 bool no_purge;
868 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
870 primary_if = batadv_seq_print_text_primary_if_get(seq);
871 if (!primary_if)
872 goto out;
874 seq_printf(seq,
875 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
876 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
877 seq_printf(seq, " %-13s %s %-7s %-9s (%-10s)\n", "Client", "VID",
878 "Flags", "Last seen", "CRC");
880 for (i = 0; i < hash->size; i++) {
881 head = &hash->table[i];
883 rcu_read_lock();
884 hlist_for_each_entry_rcu(tt_common_entry,
885 head, hash_entry) {
886 tt_local = container_of(tt_common_entry,
887 struct batadv_tt_local_entry,
888 common);
889 vid = tt_common_entry->vid;
890 last_seen_jiffies = jiffies - tt_local->last_seen;
891 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
892 last_seen_secs = last_seen_msecs / 1000;
893 last_seen_msecs = last_seen_msecs % 1000;
895 no_purge = tt_common_entry->flags & np_flag;
897 vlan = batadv_softif_vlan_get(bat_priv, vid);
898 if (!vlan) {
899 seq_printf(seq, "Cannot retrieve VLAN %d\n",
900 BATADV_PRINT_VID(vid));
901 continue;
904 seq_printf(seq,
905 " * %pM %4i [%c%c%c%c%c] %3u.%03u (%#.8x)\n",
906 tt_common_entry->addr,
907 BATADV_PRINT_VID(tt_common_entry->vid),
908 (tt_common_entry->flags &
909 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
910 no_purge ? 'P' : '.',
911 (tt_common_entry->flags &
912 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
913 (tt_common_entry->flags &
914 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
915 (tt_common_entry->flags &
916 BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
917 no_purge ? 0 : last_seen_secs,
918 no_purge ? 0 : last_seen_msecs,
919 vlan->tt.crc);
921 batadv_softif_vlan_free_ref(vlan);
923 rcu_read_unlock();
925 out:
926 if (primary_if)
927 batadv_hardif_free_ref(primary_if);
928 return 0;
931 static void
932 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
933 struct batadv_tt_local_entry *tt_local_entry,
934 uint16_t flags, const char *message)
936 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
938 /* The local client has to be marked as "pending to be removed" but has
939 * to be kept in the table in order to send it in a full table
940 * response issued before the net ttvn increment (consistency check)
942 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
944 batadv_dbg(BATADV_DBG_TT, bat_priv,
945 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
946 tt_local_entry->common.addr,
947 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
951 * batadv_tt_local_remove - logically remove an entry from the local table
952 * @bat_priv: the bat priv with all the soft interface information
953 * @addr: the MAC address of the client to remove
954 * @vid: VLAN identifier
955 * @message: message to append to the log on deletion
956 * @roaming: true if the deletion is due to a roaming event
958 * Returns the flags assigned to the local entry before being deleted
960 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
961 const uint8_t *addr, unsigned short vid,
962 const char *message, bool roaming)
964 struct batadv_tt_local_entry *tt_local_entry;
965 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
967 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
968 if (!tt_local_entry)
969 goto out;
971 curr_flags = tt_local_entry->common.flags;
973 flags = BATADV_TT_CLIENT_DEL;
974 /* if this global entry addition is due to a roaming, the node has to
975 * mark the local entry as "roamed" in order to correctly reroute
976 * packets later
978 if (roaming) {
979 flags |= BATADV_TT_CLIENT_ROAM;
980 /* mark the local client as ROAMed */
981 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
984 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
985 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
986 message);
987 goto out;
989 /* if this client has been added right now, it is possible to
990 * immediately purge it
992 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
993 hlist_del_rcu(&tt_local_entry->common.hash_entry);
994 batadv_tt_local_entry_free_ref(tt_local_entry);
996 out:
997 if (tt_local_entry)
998 batadv_tt_local_entry_free_ref(tt_local_entry);
1000 return curr_flags;
1004 * batadv_tt_local_purge_list - purge inactive tt local entries
1005 * @bat_priv: the bat priv with all the soft interface information
1006 * @head: pointer to the list containing the local tt entries
1007 * @timeout: parameter deciding whether a given tt local entry is considered
1008 * inactive or not
1010 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1011 struct hlist_head *head,
1012 int timeout)
1014 struct batadv_tt_local_entry *tt_local_entry;
1015 struct batadv_tt_common_entry *tt_common_entry;
1016 struct hlist_node *node_tmp;
1018 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1019 hash_entry) {
1020 tt_local_entry = container_of(tt_common_entry,
1021 struct batadv_tt_local_entry,
1022 common);
1023 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1024 continue;
1026 /* entry already marked for deletion */
1027 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1028 continue;
1030 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1031 continue;
1033 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1034 BATADV_TT_CLIENT_DEL, "timed out");
1039 * batadv_tt_local_purge - purge inactive tt local entries
1040 * @bat_priv: the bat priv with all the soft interface information
1041 * @timeout: parameter deciding whether a given tt local entry is considered
1042 * inactive or not
1044 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1045 int timeout)
1047 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1048 struct hlist_head *head;
1049 spinlock_t *list_lock; /* protects write access to the hash lists */
1050 uint32_t i;
1052 for (i = 0; i < hash->size; i++) {
1053 head = &hash->table[i];
1054 list_lock = &hash->list_locks[i];
1056 spin_lock_bh(list_lock);
1057 batadv_tt_local_purge_list(bat_priv, head, timeout);
1058 spin_unlock_bh(list_lock);
1062 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1064 struct batadv_hashtable *hash;
1065 spinlock_t *list_lock; /* protects write access to the hash lists */
1066 struct batadv_tt_common_entry *tt_common_entry;
1067 struct batadv_tt_local_entry *tt_local;
1068 struct hlist_node *node_tmp;
1069 struct hlist_head *head;
1070 uint32_t i;
1072 if (!bat_priv->tt.local_hash)
1073 return;
1075 hash = bat_priv->tt.local_hash;
1077 for (i = 0; i < hash->size; i++) {
1078 head = &hash->table[i];
1079 list_lock = &hash->list_locks[i];
1081 spin_lock_bh(list_lock);
1082 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1083 head, hash_entry) {
1084 hlist_del_rcu(&tt_common_entry->hash_entry);
1085 tt_local = container_of(tt_common_entry,
1086 struct batadv_tt_local_entry,
1087 common);
1088 batadv_tt_local_entry_free_ref(tt_local);
1090 spin_unlock_bh(list_lock);
1093 batadv_hash_destroy(hash);
1095 bat_priv->tt.local_hash = NULL;
1098 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1100 if (bat_priv->tt.global_hash)
1101 return 0;
1103 bat_priv->tt.global_hash = batadv_hash_new(1024);
1105 if (!bat_priv->tt.global_hash)
1106 return -ENOMEM;
1108 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1109 &batadv_tt_global_hash_lock_class_key);
1111 return 0;
1114 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1116 struct batadv_tt_change_node *entry, *safe;
1118 spin_lock_bh(&bat_priv->tt.changes_list_lock);
1120 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1121 list) {
1122 list_del(&entry->list);
1123 kfree(entry);
1126 atomic_set(&bat_priv->tt.local_changes, 0);
1127 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1130 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1131 * batadv_tt_global_entry list
1133 * returns it with an increased refcounter, NULL if not found
1135 static struct batadv_tt_orig_list_entry *
1136 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1137 const struct batadv_orig_node *orig_node)
1139 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1140 const struct hlist_head *head;
1142 rcu_read_lock();
1143 head = &entry->orig_list;
1144 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1145 if (tmp_orig_entry->orig_node != orig_node)
1146 continue;
1147 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1148 continue;
1150 orig_entry = tmp_orig_entry;
1151 break;
1153 rcu_read_unlock();
1155 return orig_entry;
1158 /* find out if an orig_node is already in the list of a tt_global_entry.
1159 * returns true if found, false otherwise
1161 static bool
1162 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1163 const struct batadv_orig_node *orig_node)
1165 struct batadv_tt_orig_list_entry *orig_entry;
1166 bool found = false;
1168 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1169 if (orig_entry) {
1170 found = true;
1171 batadv_tt_orig_list_entry_free_ref(orig_entry);
1174 return found;
1177 static void
1178 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1179 struct batadv_orig_node *orig_node, int ttvn)
1181 struct batadv_tt_orig_list_entry *orig_entry;
1183 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1184 if (orig_entry) {
1185 /* refresh the ttvn: the current value could be a bogus one that
1186 * was added during a "temporary client detection"
1188 orig_entry->ttvn = ttvn;
1189 goto out;
1192 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1193 if (!orig_entry)
1194 goto out;
1196 INIT_HLIST_NODE(&orig_entry->list);
1197 atomic_inc(&orig_node->refcount);
1198 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1199 orig_entry->orig_node = orig_node;
1200 orig_entry->ttvn = ttvn;
1201 atomic_set(&orig_entry->refcount, 2);
1203 spin_lock_bh(&tt_global->list_lock);
1204 hlist_add_head_rcu(&orig_entry->list,
1205 &tt_global->orig_list);
1206 spin_unlock_bh(&tt_global->list_lock);
1207 out:
1208 if (orig_entry)
1209 batadv_tt_orig_list_entry_free_ref(orig_entry);
1213 * batadv_tt_global_add - add a new TT global entry or update an existing one
1214 * @bat_priv: the bat priv with all the soft interface information
1215 * @orig_node: the originator announcing the client
1216 * @tt_addr: the mac address of the non-mesh client
1217 * @vid: VLAN identifier
1218 * @flags: TT flags that have to be set for this non-mesh client
1219 * @ttvn: the tt version number ever announcing this non-mesh client
1221 * Add a new TT global entry for the given originator. If the entry already
1222 * exists add a new reference to the given originator (a global entry can have
1223 * references to multiple originators) and adjust the flags attribute to reflect
1224 * the function argument.
1225 * If a TT local entry exists for this non-mesh client remove it.
1227 * The caller must hold orig_node refcount.
1229 * Return true if the new entry has been added, false otherwise
1231 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1232 struct batadv_orig_node *orig_node,
1233 const unsigned char *tt_addr,
1234 unsigned short vid, uint16_t flags,
1235 uint8_t ttvn)
1237 struct batadv_tt_global_entry *tt_global_entry;
1238 struct batadv_tt_local_entry *tt_local_entry;
1239 bool ret = false;
1240 int hash_added;
1241 struct batadv_tt_common_entry *common;
1242 uint16_t local_flags;
1244 /* ignore global entries from backbone nodes */
1245 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1246 return true;
1248 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1249 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1251 /* if the node already has a local client for this entry, it has to wait
1252 * for a roaming advertisement instead of manually messing up the global
1253 * table
1255 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1256 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1257 goto out;
1259 if (!tt_global_entry) {
1260 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1261 if (!tt_global_entry)
1262 goto out;
1264 common = &tt_global_entry->common;
1265 memcpy(common->addr, tt_addr, ETH_ALEN);
1266 common->vid = vid;
1268 common->flags = flags;
1269 tt_global_entry->roam_at = 0;
1270 /* node must store current time in case of roaming. This is
1271 * needed to purge this entry out on timeout (if nobody claims
1272 * it)
1274 if (flags & BATADV_TT_CLIENT_ROAM)
1275 tt_global_entry->roam_at = jiffies;
1276 atomic_set(&common->refcount, 2);
1277 common->added_at = jiffies;
1279 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1280 spin_lock_init(&tt_global_entry->list_lock);
1282 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1283 batadv_compare_tt,
1284 batadv_choose_tt, common,
1285 &common->hash_entry);
1287 if (unlikely(hash_added != 0)) {
1288 /* remove the reference for the hash */
1289 batadv_tt_global_entry_free_ref(tt_global_entry);
1290 goto out_remove;
1292 } else {
1293 common = &tt_global_entry->common;
1294 /* If there is already a global entry, we can use this one for
1295 * our processing.
1296 * But if we are trying to add a temporary client then here are
1297 * two options at this point:
1298 * 1) the global client is not a temporary client: the global
1299 * client has to be left as it is, temporary information
1300 * should never override any already known client state
1301 * 2) the global client is a temporary client: purge the
1302 * originator list and add the new one orig_entry
1304 if (flags & BATADV_TT_CLIENT_TEMP) {
1305 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1306 goto out;
1307 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1308 orig_node))
1309 goto out_remove;
1310 batadv_tt_global_del_orig_list(tt_global_entry);
1311 goto add_orig_entry;
1314 /* if the client was temporary added before receiving the first
1315 * OGM announcing it, we have to clear the TEMP flag
1317 common->flags &= ~BATADV_TT_CLIENT_TEMP;
1319 /* the change can carry possible "attribute" flags like the
1320 * TT_CLIENT_WIFI, therefore they have to be copied in the
1321 * client entry
1323 tt_global_entry->common.flags |= flags;
1325 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1326 * one originator left in the list and we previously received a
1327 * delete + roaming change for this originator.
1329 * We should first delete the old originator before adding the
1330 * new one.
1332 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1333 batadv_tt_global_del_orig_list(tt_global_entry);
1334 common->flags &= ~BATADV_TT_CLIENT_ROAM;
1335 tt_global_entry->roam_at = 0;
1338 add_orig_entry:
1339 /* add the new orig_entry (if needed) or update it */
1340 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
1342 batadv_dbg(BATADV_DBG_TT, bat_priv,
1343 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1344 common->addr, BATADV_PRINT_VID(common->vid),
1345 orig_node->orig);
1346 ret = true;
1348 out_remove:
1350 /* remove address from local hash if present */
1351 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1352 "global tt received",
1353 flags & BATADV_TT_CLIENT_ROAM);
1354 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1356 if (!(flags & BATADV_TT_CLIENT_ROAM))
1357 /* this is a normal global add. Therefore the client is not in a
1358 * roaming state anymore.
1360 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1362 out:
1363 if (tt_global_entry)
1364 batadv_tt_global_entry_free_ref(tt_global_entry);
1365 if (tt_local_entry)
1366 batadv_tt_local_entry_free_ref(tt_local_entry);
1367 return ret;
1370 /* batadv_transtable_best_orig - Get best originator list entry from tt entry
1371 * @bat_priv: the bat priv with all the soft interface information
1372 * @tt_global_entry: global translation table entry to be analyzed
1374 * This functon assumes the caller holds rcu_read_lock().
1375 * Returns best originator list entry or NULL on errors.
1377 static struct batadv_tt_orig_list_entry *
1378 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1379 struct batadv_tt_global_entry *tt_global_entry)
1381 struct batadv_neigh_node *router, *best_router = NULL;
1382 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1383 struct hlist_head *head;
1384 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1386 head = &tt_global_entry->orig_list;
1387 hlist_for_each_entry_rcu(orig_entry, head, list) {
1388 router = batadv_orig_node_get_router(orig_entry->orig_node);
1389 if (!router)
1390 continue;
1392 if (best_router &&
1393 bao->bat_neigh_cmp(router, best_router) <= 0) {
1394 batadv_neigh_node_free_ref(router);
1395 continue;
1398 /* release the refcount for the "old" best */
1399 if (best_router)
1400 batadv_neigh_node_free_ref(best_router);
1402 best_entry = orig_entry;
1403 best_router = router;
1406 if (best_router)
1407 batadv_neigh_node_free_ref(best_router);
1409 return best_entry;
1412 /* batadv_tt_global_print_entry - print all orig nodes who announce the address
1413 * for this global entry
1414 * @bat_priv: the bat priv with all the soft interface information
1415 * @tt_global_entry: global translation table entry to be printed
1416 * @seq: debugfs table seq_file struct
1418 * This functon assumes the caller holds rcu_read_lock().
1420 static void
1421 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1422 struct batadv_tt_global_entry *tt_global_entry,
1423 struct seq_file *seq)
1425 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1426 struct batadv_tt_common_entry *tt_common_entry;
1427 struct batadv_orig_node_vlan *vlan;
1428 struct hlist_head *head;
1429 uint8_t last_ttvn;
1430 uint16_t flags;
1432 tt_common_entry = &tt_global_entry->common;
1433 flags = tt_common_entry->flags;
1435 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1436 if (best_entry) {
1437 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1438 tt_common_entry->vid);
1439 if (!vlan) {
1440 seq_printf(seq,
1441 " * Cannot retrieve VLAN %d for originator %pM\n",
1442 BATADV_PRINT_VID(tt_common_entry->vid),
1443 best_entry->orig_node->orig);
1444 goto print_list;
1447 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1448 seq_printf(seq,
1449 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
1450 '*', tt_global_entry->common.addr,
1451 BATADV_PRINT_VID(tt_global_entry->common.vid),
1452 best_entry->ttvn, best_entry->orig_node->orig,
1453 last_ttvn, vlan->tt.crc,
1454 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1455 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1456 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1458 batadv_orig_node_vlan_free_ref(vlan);
1461 print_list:
1462 head = &tt_global_entry->orig_list;
1464 hlist_for_each_entry_rcu(orig_entry, head, list) {
1465 if (best_entry == orig_entry)
1466 continue;
1468 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1469 tt_common_entry->vid);
1470 if (!vlan) {
1471 seq_printf(seq,
1472 " + Cannot retrieve VLAN %d for originator %pM\n",
1473 BATADV_PRINT_VID(tt_common_entry->vid),
1474 orig_entry->orig_node->orig);
1475 continue;
1478 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1479 seq_printf(seq,
1480 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c]\n",
1481 '+', tt_global_entry->common.addr,
1482 BATADV_PRINT_VID(tt_global_entry->common.vid),
1483 orig_entry->ttvn, orig_entry->orig_node->orig,
1484 last_ttvn, vlan->tt.crc,
1485 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1486 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1487 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1489 batadv_orig_node_vlan_free_ref(vlan);
1493 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1495 struct net_device *net_dev = (struct net_device *)seq->private;
1496 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1497 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1498 struct batadv_tt_common_entry *tt_common_entry;
1499 struct batadv_tt_global_entry *tt_global;
1500 struct batadv_hard_iface *primary_if;
1501 struct hlist_head *head;
1502 uint32_t i;
1504 primary_if = batadv_seq_print_text_primary_if_get(seq);
1505 if (!primary_if)
1506 goto out;
1508 seq_printf(seq,
1509 "Globally announced TT entries received via the mesh %s\n",
1510 net_dev->name);
1511 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1512 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1513 "CRC", "Flags");
1515 for (i = 0; i < hash->size; i++) {
1516 head = &hash->table[i];
1518 rcu_read_lock();
1519 hlist_for_each_entry_rcu(tt_common_entry,
1520 head, hash_entry) {
1521 tt_global = container_of(tt_common_entry,
1522 struct batadv_tt_global_entry,
1523 common);
1524 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1526 rcu_read_unlock();
1528 out:
1529 if (primary_if)
1530 batadv_hardif_free_ref(primary_if);
1531 return 0;
1534 /* deletes the orig list of a tt_global_entry */
1535 static void
1536 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1538 struct hlist_head *head;
1539 struct hlist_node *safe;
1540 struct batadv_tt_orig_list_entry *orig_entry;
1542 spin_lock_bh(&tt_global_entry->list_lock);
1543 head = &tt_global_entry->orig_list;
1544 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1545 hlist_del_rcu(&orig_entry->list);
1546 batadv_tt_global_size_dec(orig_entry->orig_node,
1547 tt_global_entry->common.vid);
1548 batadv_tt_orig_list_entry_free_ref(orig_entry);
1550 spin_unlock_bh(&tt_global_entry->list_lock);
1553 static void
1554 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1555 struct batadv_tt_global_entry *tt_global_entry,
1556 struct batadv_orig_node *orig_node,
1557 const char *message)
1559 struct hlist_head *head;
1560 struct hlist_node *safe;
1561 struct batadv_tt_orig_list_entry *orig_entry;
1562 unsigned short vid;
1564 spin_lock_bh(&tt_global_entry->list_lock);
1565 head = &tt_global_entry->orig_list;
1566 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1567 if (orig_entry->orig_node == orig_node) {
1568 vid = tt_global_entry->common.vid;
1569 batadv_dbg(BATADV_DBG_TT, bat_priv,
1570 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1571 orig_node->orig,
1572 tt_global_entry->common.addr,
1573 BATADV_PRINT_VID(vid), message);
1574 hlist_del_rcu(&orig_entry->list);
1575 batadv_tt_global_size_dec(orig_node,
1576 tt_global_entry->common.vid);
1577 batadv_tt_orig_list_entry_free_ref(orig_entry);
1580 spin_unlock_bh(&tt_global_entry->list_lock);
1583 /* If the client is to be deleted, we check if it is the last origantor entry
1584 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1585 * timer, otherwise we simply remove the originator scheduled for deletion.
1587 static void
1588 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1589 struct batadv_tt_global_entry *tt_global_entry,
1590 struct batadv_orig_node *orig_node,
1591 const char *message)
1593 bool last_entry = true;
1594 struct hlist_head *head;
1595 struct batadv_tt_orig_list_entry *orig_entry;
1597 /* no local entry exists, case 1:
1598 * Check if this is the last one or if other entries exist.
1601 rcu_read_lock();
1602 head = &tt_global_entry->orig_list;
1603 hlist_for_each_entry_rcu(orig_entry, head, list) {
1604 if (orig_entry->orig_node != orig_node) {
1605 last_entry = false;
1606 break;
1609 rcu_read_unlock();
1611 if (last_entry) {
1612 /* its the last one, mark for roaming. */
1613 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1614 tt_global_entry->roam_at = jiffies;
1615 } else
1616 /* there is another entry, we can simply delete this
1617 * one and can still use the other one.
1619 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1620 orig_node, message);
1624 * batadv_tt_global_del - remove a client from the global table
1625 * @bat_priv: the bat priv with all the soft interface information
1626 * @orig_node: an originator serving this client
1627 * @addr: the mac address of the client
1628 * @vid: VLAN identifier
1629 * @message: a message explaining the reason for deleting the client to print
1630 * for debugging purpose
1631 * @roaming: true if the deletion has been triggered by a roaming event
1633 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1634 struct batadv_orig_node *orig_node,
1635 const unsigned char *addr, unsigned short vid,
1636 const char *message, bool roaming)
1638 struct batadv_tt_global_entry *tt_global_entry;
1639 struct batadv_tt_local_entry *local_entry = NULL;
1641 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1642 if (!tt_global_entry)
1643 goto out;
1645 if (!roaming) {
1646 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1647 orig_node, message);
1649 if (hlist_empty(&tt_global_entry->orig_list))
1650 batadv_tt_global_free(bat_priv, tt_global_entry,
1651 message);
1653 goto out;
1656 /* if we are deleting a global entry due to a roam
1657 * event, there are two possibilities:
1658 * 1) the client roamed from node A to node B => if there
1659 * is only one originator left for this client, we mark
1660 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1661 * wait for node B to claim it. In case of timeout
1662 * the entry is purged.
1664 * If there are other originators left, we directly delete
1665 * the originator.
1666 * 2) the client roamed to us => we can directly delete
1667 * the global entry, since it is useless now.
1669 local_entry = batadv_tt_local_hash_find(bat_priv,
1670 tt_global_entry->common.addr,
1671 vid);
1672 if (local_entry) {
1673 /* local entry exists, case 2: client roamed to us. */
1674 batadv_tt_global_del_orig_list(tt_global_entry);
1675 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1676 } else
1677 /* no local entry exists, case 1: check for roaming */
1678 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1679 orig_node, message);
1682 out:
1683 if (tt_global_entry)
1684 batadv_tt_global_entry_free_ref(tt_global_entry);
1685 if (local_entry)
1686 batadv_tt_local_entry_free_ref(local_entry);
1690 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1691 * given originator matching the provided vid
1692 * @bat_priv: the bat priv with all the soft interface information
1693 * @orig_node: the originator owning the entries to remove
1694 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1695 * removed
1696 * @message: debug message to print as "reason"
1698 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1699 struct batadv_orig_node *orig_node,
1700 int32_t match_vid,
1701 const char *message)
1703 struct batadv_tt_global_entry *tt_global;
1704 struct batadv_tt_common_entry *tt_common_entry;
1705 uint32_t i;
1706 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1707 struct hlist_node *safe;
1708 struct hlist_head *head;
1709 spinlock_t *list_lock; /* protects write access to the hash lists */
1710 unsigned short vid;
1712 if (!hash)
1713 return;
1715 for (i = 0; i < hash->size; i++) {
1716 head = &hash->table[i];
1717 list_lock = &hash->list_locks[i];
1719 spin_lock_bh(list_lock);
1720 hlist_for_each_entry_safe(tt_common_entry, safe,
1721 head, hash_entry) {
1722 /* remove only matching entries */
1723 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1724 continue;
1726 tt_global = container_of(tt_common_entry,
1727 struct batadv_tt_global_entry,
1728 common);
1730 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1731 orig_node, message);
1733 if (hlist_empty(&tt_global->orig_list)) {
1734 vid = tt_global->common.vid;
1735 batadv_dbg(BATADV_DBG_TT, bat_priv,
1736 "Deleting global tt entry %pM (vid: %d): %s\n",
1737 tt_global->common.addr,
1738 BATADV_PRINT_VID(vid), message);
1739 hlist_del_rcu(&tt_common_entry->hash_entry);
1740 batadv_tt_global_entry_free_ref(tt_global);
1743 spin_unlock_bh(list_lock);
1745 orig_node->tt_initialised = false;
1748 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1749 char **msg)
1751 bool purge = false;
1752 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1753 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1755 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1756 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1757 purge = true;
1758 *msg = "Roaming timeout\n";
1761 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1762 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1763 purge = true;
1764 *msg = "Temporary client timeout\n";
1767 return purge;
1770 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1772 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1773 struct hlist_head *head;
1774 struct hlist_node *node_tmp;
1775 spinlock_t *list_lock; /* protects write access to the hash lists */
1776 uint32_t i;
1777 char *msg = NULL;
1778 struct batadv_tt_common_entry *tt_common;
1779 struct batadv_tt_global_entry *tt_global;
1781 for (i = 0; i < hash->size; i++) {
1782 head = &hash->table[i];
1783 list_lock = &hash->list_locks[i];
1785 spin_lock_bh(list_lock);
1786 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1787 hash_entry) {
1788 tt_global = container_of(tt_common,
1789 struct batadv_tt_global_entry,
1790 common);
1792 if (!batadv_tt_global_to_purge(tt_global, &msg))
1793 continue;
1795 batadv_dbg(BATADV_DBG_TT, bat_priv,
1796 "Deleting global tt entry %pM (vid: %d): %s\n",
1797 tt_global->common.addr,
1798 BATADV_PRINT_VID(tt_global->common.vid),
1799 msg);
1801 hlist_del_rcu(&tt_common->hash_entry);
1803 batadv_tt_global_entry_free_ref(tt_global);
1805 spin_unlock_bh(list_lock);
1809 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1811 struct batadv_hashtable *hash;
1812 spinlock_t *list_lock; /* protects write access to the hash lists */
1813 struct batadv_tt_common_entry *tt_common_entry;
1814 struct batadv_tt_global_entry *tt_global;
1815 struct hlist_node *node_tmp;
1816 struct hlist_head *head;
1817 uint32_t i;
1819 if (!bat_priv->tt.global_hash)
1820 return;
1822 hash = bat_priv->tt.global_hash;
1824 for (i = 0; i < hash->size; i++) {
1825 head = &hash->table[i];
1826 list_lock = &hash->list_locks[i];
1828 spin_lock_bh(list_lock);
1829 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1830 head, hash_entry) {
1831 hlist_del_rcu(&tt_common_entry->hash_entry);
1832 tt_global = container_of(tt_common_entry,
1833 struct batadv_tt_global_entry,
1834 common);
1835 batadv_tt_global_entry_free_ref(tt_global);
1837 spin_unlock_bh(list_lock);
1840 batadv_hash_destroy(hash);
1842 bat_priv->tt.global_hash = NULL;
1845 static bool
1846 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1847 struct batadv_tt_global_entry *tt_global_entry)
1849 bool ret = false;
1851 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1852 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1853 ret = true;
1855 return ret;
1859 * batadv_transtable_search - get the mesh destination for a given client
1860 * @bat_priv: the bat priv with all the soft interface information
1861 * @src: mac address of the source client
1862 * @addr: mac address of the destination client
1863 * @vid: VLAN identifier
1865 * Returns a pointer to the originator that was selected as destination in the
1866 * mesh for contacting the client 'addr', NULL otherwise.
1867 * In case of multiple originators serving the same client, the function returns
1868 * the best one (best in terms of metric towards the destination node).
1870 * If the two clients are AP isolated the function returns NULL.
1872 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1873 const uint8_t *src,
1874 const uint8_t *addr,
1875 unsigned short vid)
1877 struct batadv_tt_local_entry *tt_local_entry = NULL;
1878 struct batadv_tt_global_entry *tt_global_entry = NULL;
1879 struct batadv_orig_node *orig_node = NULL;
1880 struct batadv_tt_orig_list_entry *best_entry;
1881 bool ap_isolation_enabled = false;
1882 struct batadv_softif_vlan *vlan;
1884 /* if the AP isolation is requested on a VLAN, then check for its
1885 * setting in the proper VLAN private data structure
1887 vlan = batadv_softif_vlan_get(bat_priv, vid);
1888 if (vlan) {
1889 ap_isolation_enabled = atomic_read(&vlan->ap_isolation);
1890 batadv_softif_vlan_free_ref(vlan);
1893 if (src && ap_isolation_enabled) {
1894 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
1895 if (!tt_local_entry ||
1896 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1897 goto out;
1900 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1901 if (!tt_global_entry)
1902 goto out;
1904 /* check whether the clients should not communicate due to AP
1905 * isolation
1907 if (tt_local_entry &&
1908 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1909 goto out;
1911 rcu_read_lock();
1912 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1913 /* found anything? */
1914 if (best_entry)
1915 orig_node = best_entry->orig_node;
1916 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1917 orig_node = NULL;
1918 rcu_read_unlock();
1920 out:
1921 if (tt_global_entry)
1922 batadv_tt_global_entry_free_ref(tt_global_entry);
1923 if (tt_local_entry)
1924 batadv_tt_local_entry_free_ref(tt_local_entry);
1926 return orig_node;
1930 * batadv_tt_global_crc - calculates the checksum of the local table belonging
1931 * to the given orig_node
1932 * @bat_priv: the bat priv with all the soft interface information
1933 * @orig_node: originator for which the CRC should be computed
1934 * @vid: VLAN identifier for which the CRC32 has to be computed
1936 * This function computes the checksum for the global table corresponding to a
1937 * specific originator. In particular, the checksum is computed as follows: For
1938 * each client connected to the originator the CRC32C of the MAC address and the
1939 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
1940 * together.
1942 * The idea behind is that CRC32C should be used as much as possible in order to
1943 * produce a unique hash of the table, but since the order which is used to feed
1944 * the CRC32C function affects the result and since every node in the network
1945 * probably sorts the clients differently, the hash function cannot be directly
1946 * computed over the entire table. Hence the CRC32C is used only on
1947 * the single client entry, while all the results are then xor'ed together
1948 * because the XOR operation can combine them all while trying to reduce the
1949 * noise as much as possible.
1951 * Returns the checksum of the global table of a given originator.
1953 static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1954 struct batadv_orig_node *orig_node,
1955 unsigned short vid)
1957 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1958 struct batadv_tt_common_entry *tt_common;
1959 struct batadv_tt_global_entry *tt_global;
1960 struct hlist_head *head;
1961 uint32_t i, crc_tmp, crc = 0;
1962 uint8_t flags;
1964 for (i = 0; i < hash->size; i++) {
1965 head = &hash->table[i];
1967 rcu_read_lock();
1968 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
1969 tt_global = container_of(tt_common,
1970 struct batadv_tt_global_entry,
1971 common);
1972 /* compute the CRC only for entries belonging to the
1973 * VLAN identified by the vid passed as parameter
1975 if (tt_common->vid != vid)
1976 continue;
1978 /* Roaming clients are in the global table for
1979 * consistency only. They don't have to be
1980 * taken into account while computing the
1981 * global crc
1983 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1984 continue;
1985 /* Temporary clients have not been announced yet, so
1986 * they have to be skipped while computing the global
1987 * crc
1989 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1990 continue;
1992 /* find out if this global entry is announced by this
1993 * originator
1995 if (!batadv_tt_global_entry_has_orig(tt_global,
1996 orig_node))
1997 continue;
1999 crc_tmp = crc32c(0, &tt_common->vid,
2000 sizeof(tt_common->vid));
2002 /* compute the CRC on flags that have to be kept in sync
2003 * among nodes
2005 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2006 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2008 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2010 rcu_read_unlock();
2013 return crc;
2017 * batadv_tt_local_crc - calculates the checksum of the local table
2018 * @bat_priv: the bat priv with all the soft interface information
2019 * @vid: VLAN identifier for which the CRC32 has to be computed
2021 * For details about the computation, please refer to the documentation for
2022 * batadv_tt_global_crc().
2024 * Returns the checksum of the local table
2026 static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2027 unsigned short vid)
2029 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2030 struct batadv_tt_common_entry *tt_common;
2031 struct hlist_head *head;
2032 uint32_t i, crc_tmp, crc = 0;
2033 uint8_t flags;
2035 for (i = 0; i < hash->size; i++) {
2036 head = &hash->table[i];
2038 rcu_read_lock();
2039 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2040 /* compute the CRC only for entries belonging to the
2041 * VLAN identified by vid
2043 if (tt_common->vid != vid)
2044 continue;
2046 /* not yet committed clients have not to be taken into
2047 * account while computing the CRC
2049 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2050 continue;
2052 crc_tmp = crc32c(0, &tt_common->vid,
2053 sizeof(tt_common->vid));
2055 /* compute the CRC on flags that have to be kept in sync
2056 * among nodes
2058 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2059 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2061 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2063 rcu_read_unlock();
2066 return crc;
2069 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2071 struct batadv_tt_req_node *node, *safe;
2073 spin_lock_bh(&bat_priv->tt.req_list_lock);
2075 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2076 list_del(&node->list);
2077 kfree(node);
2080 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2083 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2084 struct batadv_orig_node *orig_node,
2085 const void *tt_buff,
2086 uint16_t tt_buff_len)
2088 /* Replace the old buffer only if I received something in the
2089 * last OGM (the OGM could carry no changes)
2091 spin_lock_bh(&orig_node->tt_buff_lock);
2092 if (tt_buff_len > 0) {
2093 kfree(orig_node->tt_buff);
2094 orig_node->tt_buff_len = 0;
2095 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2096 if (orig_node->tt_buff) {
2097 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2098 orig_node->tt_buff_len = tt_buff_len;
2101 spin_unlock_bh(&orig_node->tt_buff_lock);
2104 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2106 struct batadv_tt_req_node *node, *safe;
2108 spin_lock_bh(&bat_priv->tt.req_list_lock);
2109 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2110 if (batadv_has_timed_out(node->issued_at,
2111 BATADV_TT_REQUEST_TIMEOUT)) {
2112 list_del(&node->list);
2113 kfree(node);
2116 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2119 /* returns the pointer to the new tt_req_node struct if no request
2120 * has already been issued for this orig_node, NULL otherwise
2122 static struct batadv_tt_req_node *
2123 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2124 struct batadv_orig_node *orig_node)
2126 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2128 spin_lock_bh(&bat_priv->tt.req_list_lock);
2129 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2130 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2131 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2132 BATADV_TT_REQUEST_TIMEOUT))
2133 goto unlock;
2136 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2137 if (!tt_req_node)
2138 goto unlock;
2140 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
2141 tt_req_node->issued_at = jiffies;
2143 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
2144 unlock:
2145 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2146 return tt_req_node;
2150 * batadv_tt_local_valid - verify that given tt entry is a valid one
2151 * @entry_ptr: to be checked local tt entry
2152 * @data_ptr: not used but definition required to satisfy the callback prototype
2154 * Returns 1 if the entry is a valid, 0 otherwise.
2156 static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
2158 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2160 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2161 return 0;
2162 return 1;
2165 static int batadv_tt_global_valid(const void *entry_ptr,
2166 const void *data_ptr)
2168 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2169 const struct batadv_tt_global_entry *tt_global_entry;
2170 const struct batadv_orig_node *orig_node = data_ptr;
2172 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2173 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2174 return 0;
2176 tt_global_entry = container_of(tt_common_entry,
2177 struct batadv_tt_global_entry,
2178 common);
2180 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
2184 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2185 * specified tt hash
2186 * @bat_priv: the bat priv with all the soft interface information
2187 * @hash: hash table containing the tt entries
2188 * @tt_len: expected tvlv tt data buffer length in number of bytes
2189 * @tvlv_buff: pointer to the buffer to fill with the TT data
2190 * @valid_cb: function to filter tt change entries
2191 * @cb_data: data passed to the filter function as argument
2193 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2194 struct batadv_hashtable *hash,
2195 void *tvlv_buff, uint16_t tt_len,
2196 int (*valid_cb)(const void *, const void *),
2197 void *cb_data)
2199 struct batadv_tt_common_entry *tt_common_entry;
2200 struct batadv_tvlv_tt_change *tt_change;
2201 struct hlist_head *head;
2202 uint16_t tt_tot, tt_num_entries = 0;
2203 uint32_t i;
2205 tt_tot = batadv_tt_entries(tt_len);
2206 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2208 rcu_read_lock();
2209 for (i = 0; i < hash->size; i++) {
2210 head = &hash->table[i];
2212 hlist_for_each_entry_rcu(tt_common_entry,
2213 head, hash_entry) {
2214 if (tt_tot == tt_num_entries)
2215 break;
2217 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
2218 continue;
2220 memcpy(tt_change->addr, tt_common_entry->addr,
2221 ETH_ALEN);
2222 tt_change->flags = tt_common_entry->flags;
2223 tt_change->vid = htons(tt_common_entry->vid);
2224 tt_change->reserved = 0;
2226 tt_num_entries++;
2227 tt_change++;
2230 rcu_read_unlock();
2234 * batadv_tt_global_check_crc - check if all the CRCs are correct
2235 * @orig_node: originator for which the CRCs have to be checked
2236 * @tt_vlan: pointer to the first tvlv VLAN entry
2237 * @num_vlan: number of tvlv VLAN entries
2238 * @create: if true, create VLAN objects if not found
2240 * Return true if all the received CRCs match the locally stored ones, false
2241 * otherwise
2243 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2244 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2245 uint16_t num_vlan)
2247 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2248 struct batadv_orig_node_vlan *vlan;
2249 int i;
2251 /* check if each received CRC matches the locally stored one */
2252 for (i = 0; i < num_vlan; i++) {
2253 tt_vlan_tmp = tt_vlan + i;
2255 /* if orig_node is a backbone node for this VLAN, don't check
2256 * the CRC as we ignore all the global entries over it
2258 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2259 orig_node->orig,
2260 ntohs(tt_vlan_tmp->vid)))
2261 continue;
2263 vlan = batadv_orig_node_vlan_get(orig_node,
2264 ntohs(tt_vlan_tmp->vid));
2265 if (!vlan)
2266 return false;
2268 if (vlan->tt.crc != ntohl(tt_vlan_tmp->crc))
2269 return false;
2272 return true;
2276 * batadv_tt_local_update_crc - update all the local CRCs
2277 * @bat_priv: the bat priv with all the soft interface information
2279 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2281 struct batadv_softif_vlan *vlan;
2283 /* recompute the global CRC for each VLAN */
2284 rcu_read_lock();
2285 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2286 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2288 rcu_read_unlock();
2292 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2293 * @bat_priv: the bat priv with all the soft interface information
2294 * @orig_node: the orig_node for which the CRCs have to be updated
2296 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2297 struct batadv_orig_node *orig_node)
2299 struct batadv_orig_node_vlan *vlan;
2300 uint32_t crc;
2302 /* recompute the global CRC for each VLAN */
2303 rcu_read_lock();
2304 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2305 /* if orig_node is a backbone node for this VLAN, don't compute
2306 * the CRC as we ignore all the global entries over it
2308 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2309 vlan->vid))
2310 continue;
2312 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2313 vlan->tt.crc = crc;
2315 rcu_read_unlock();
2319 * batadv_send_tt_request - send a TT Request message to a given node
2320 * @bat_priv: the bat priv with all the soft interface information
2321 * @dst_orig_node: the destination of the message
2322 * @ttvn: the version number that the source of the message is looking for
2323 * @tt_vlan: pointer to the first tvlv VLAN object to request
2324 * @num_vlan: number of tvlv VLAN entries
2325 * @full_table: ask for the entire translation table if true, while only for the
2326 * last TT diff otherwise
2328 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2329 struct batadv_orig_node *dst_orig_node,
2330 uint8_t ttvn,
2331 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2332 uint16_t num_vlan, bool full_table)
2334 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2335 struct batadv_tt_req_node *tt_req_node = NULL;
2336 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2337 struct batadv_hard_iface *primary_if;
2338 bool ret = false;
2339 int i, size;
2341 primary_if = batadv_primary_if_get_selected(bat_priv);
2342 if (!primary_if)
2343 goto out;
2345 /* The new tt_req will be issued only if I'm not waiting for a
2346 * reply from the same orig_node yet
2348 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
2349 if (!tt_req_node)
2350 goto out;
2352 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2353 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2354 if (!tvlv_tt_data)
2355 goto out;
2357 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2358 tvlv_tt_data->ttvn = ttvn;
2359 tvlv_tt_data->num_vlan = htons(num_vlan);
2361 /* send all the CRCs within the request. This is needed by intermediate
2362 * nodes to ensure they have the correct table before replying
2364 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2365 for (i = 0; i < num_vlan; i++) {
2366 tt_vlan_req->vid = tt_vlan->vid;
2367 tt_vlan_req->crc = tt_vlan->crc;
2369 tt_vlan_req++;
2370 tt_vlan++;
2373 if (full_table)
2374 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2376 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2377 dst_orig_node->orig, full_table ? 'F' : '.');
2379 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2380 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2381 dst_orig_node->orig, BATADV_TVLV_TT, 1,
2382 tvlv_tt_data, size);
2383 ret = true;
2385 out:
2386 if (primary_if)
2387 batadv_hardif_free_ref(primary_if);
2388 if (ret && tt_req_node) {
2389 spin_lock_bh(&bat_priv->tt.req_list_lock);
2390 list_del(&tt_req_node->list);
2391 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2392 kfree(tt_req_node);
2394 kfree(tvlv_tt_data);
2395 return ret;
2399 * batadv_send_other_tt_response - send reply to tt request concerning another
2400 * node's translation table
2401 * @bat_priv: the bat priv with all the soft interface information
2402 * @tt_data: tt data containing the tt request information
2403 * @req_src: mac address of tt request sender
2404 * @req_dst: mac address of tt request recipient
2406 * Returns true if tt request reply was sent, false otherwise.
2408 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2409 struct batadv_tvlv_tt_data *tt_data,
2410 uint8_t *req_src, uint8_t *req_dst)
2412 struct batadv_orig_node *req_dst_orig_node;
2413 struct batadv_orig_node *res_dst_orig_node = NULL;
2414 struct batadv_tvlv_tt_change *tt_change;
2415 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2416 struct batadv_tvlv_tt_vlan_data *tt_vlan;
2417 bool ret = false, full_table;
2418 uint8_t orig_ttvn, req_ttvn;
2419 uint16_t tvlv_len;
2420 int32_t tt_len;
2422 batadv_dbg(BATADV_DBG_TT, bat_priv,
2423 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2424 req_src, tt_data->ttvn, req_dst,
2425 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2427 /* Let's get the orig node of the REAL destination */
2428 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2429 if (!req_dst_orig_node)
2430 goto out;
2432 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2433 if (!res_dst_orig_node)
2434 goto out;
2436 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
2437 req_ttvn = tt_data->ttvn;
2439 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2440 /* this node doesn't have the requested data */
2441 if (orig_ttvn != req_ttvn ||
2442 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2443 ntohs(tt_data->num_vlan)))
2444 goto out;
2446 /* If the full table has been explicitly requested */
2447 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2448 !req_dst_orig_node->tt_buff)
2449 full_table = true;
2450 else
2451 full_table = false;
2453 /* TT fragmentation hasn't been implemented yet, so send as many
2454 * TT entries fit a single packet as possible only
2456 if (!full_table) {
2457 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2458 tt_len = req_dst_orig_node->tt_buff_len;
2460 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2461 &tvlv_tt_data,
2462 &tt_change,
2463 &tt_len);
2464 if (!tt_len)
2465 goto unlock;
2467 /* Copy the last orig_node's OGM buffer */
2468 memcpy(tt_change, req_dst_orig_node->tt_buff,
2469 req_dst_orig_node->tt_buff_len);
2470 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2471 } else {
2472 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2473 * in the initial part
2475 tt_len = -1;
2476 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2477 &tvlv_tt_data,
2478 &tt_change,
2479 &tt_len);
2480 if (!tt_len)
2481 goto out;
2483 /* fill the rest of the tvlv with the real TT entries */
2484 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2485 tt_change, tt_len,
2486 batadv_tt_global_valid,
2487 req_dst_orig_node);
2490 /* Don't send the response, if larger than fragmented packet. */
2491 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2492 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2493 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2494 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2495 res_dst_orig_node->orig);
2496 goto out;
2499 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2500 tvlv_tt_data->ttvn = req_ttvn;
2502 if (full_table)
2503 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2505 batadv_dbg(BATADV_DBG_TT, bat_priv,
2506 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2507 res_dst_orig_node->orig, req_dst_orig_node->orig,
2508 full_table ? 'F' : '.', req_ttvn);
2510 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2512 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2513 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2514 tvlv_len);
2516 ret = true;
2517 goto out;
2519 unlock:
2520 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2522 out:
2523 if (res_dst_orig_node)
2524 batadv_orig_node_free_ref(res_dst_orig_node);
2525 if (req_dst_orig_node)
2526 batadv_orig_node_free_ref(req_dst_orig_node);
2527 kfree(tvlv_tt_data);
2528 return ret;
2532 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2533 * translation table
2534 * @bat_priv: the bat priv with all the soft interface information
2535 * @tt_data: tt data containing the tt request information
2536 * @req_src: mac address of tt request sender
2538 * Returns true if tt request reply was sent, false otherwise.
2540 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2541 struct batadv_tvlv_tt_data *tt_data,
2542 uint8_t *req_src)
2544 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2545 struct batadv_hard_iface *primary_if = NULL;
2546 struct batadv_tvlv_tt_change *tt_change;
2547 struct batadv_orig_node *orig_node;
2548 uint8_t my_ttvn, req_ttvn;
2549 uint16_t tvlv_len;
2550 bool full_table;
2551 int32_t tt_len;
2553 batadv_dbg(BATADV_DBG_TT, bat_priv,
2554 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2555 req_src, tt_data->ttvn,
2556 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2558 spin_lock_bh(&bat_priv->tt.commit_lock);
2560 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
2561 req_ttvn = tt_data->ttvn;
2563 orig_node = batadv_orig_hash_find(bat_priv, req_src);
2564 if (!orig_node)
2565 goto out;
2567 primary_if = batadv_primary_if_get_selected(bat_priv);
2568 if (!primary_if)
2569 goto out;
2571 /* If the full table has been explicitly requested or the gap
2572 * is too big send the whole local translation table
2574 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2575 !bat_priv->tt.last_changeset)
2576 full_table = true;
2577 else
2578 full_table = false;
2580 /* TT fragmentation hasn't been implemented yet, so send as many
2581 * TT entries fit a single packet as possible only
2583 if (!full_table) {
2584 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2586 tt_len = bat_priv->tt.last_changeset_len;
2587 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2588 &tvlv_tt_data,
2589 &tt_change,
2590 &tt_len);
2591 if (!tt_len)
2592 goto unlock;
2594 /* Copy the last orig_node's OGM buffer */
2595 memcpy(tt_change, bat_priv->tt.last_changeset,
2596 bat_priv->tt.last_changeset_len);
2597 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2598 } else {
2599 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
2601 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2602 * in the initial part
2604 tt_len = -1;
2605 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2606 &tvlv_tt_data,
2607 &tt_change,
2608 &tt_len);
2609 if (!tt_len)
2610 goto out;
2612 /* fill the rest of the tvlv with the real TT entries */
2613 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2614 tt_change, tt_len,
2615 batadv_tt_local_valid, NULL);
2618 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2619 tvlv_tt_data->ttvn = req_ttvn;
2621 if (full_table)
2622 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2624 batadv_dbg(BATADV_DBG_TT, bat_priv,
2625 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2626 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2628 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2630 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2631 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2632 tvlv_len);
2634 goto out;
2636 unlock:
2637 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2638 out:
2639 spin_unlock_bh(&bat_priv->tt.commit_lock);
2640 if (orig_node)
2641 batadv_orig_node_free_ref(orig_node);
2642 if (primary_if)
2643 batadv_hardif_free_ref(primary_if);
2644 kfree(tvlv_tt_data);
2645 /* The packet was for this host, so it doesn't need to be re-routed */
2646 return true;
2650 * batadv_send_tt_response - send reply to tt request
2651 * @bat_priv: the bat priv with all the soft interface information
2652 * @tt_data: tt data containing the tt request information
2653 * @req_src: mac address of tt request sender
2654 * @req_dst: mac address of tt request recipient
2656 * Returns true if tt request reply was sent, false otherwise.
2658 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2659 struct batadv_tvlv_tt_data *tt_data,
2660 uint8_t *req_src, uint8_t *req_dst)
2662 if (batadv_is_my_mac(bat_priv, req_dst))
2663 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2664 else
2665 return batadv_send_other_tt_response(bat_priv, tt_data,
2666 req_src, req_dst);
2669 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2670 struct batadv_orig_node *orig_node,
2671 struct batadv_tvlv_tt_change *tt_change,
2672 uint16_t tt_num_changes, uint8_t ttvn)
2674 int i;
2675 int roams;
2677 for (i = 0; i < tt_num_changes; i++) {
2678 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2679 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2680 batadv_tt_global_del(bat_priv, orig_node,
2681 (tt_change + i)->addr,
2682 ntohs((tt_change + i)->vid),
2683 "tt removed by changes",
2684 roams);
2685 } else {
2686 if (!batadv_tt_global_add(bat_priv, orig_node,
2687 (tt_change + i)->addr,
2688 ntohs((tt_change + i)->vid),
2689 (tt_change + i)->flags, ttvn))
2690 /* In case of problem while storing a
2691 * global_entry, we stop the updating
2692 * procedure without committing the
2693 * ttvn change. This will avoid to send
2694 * corrupted data on tt_request
2696 return;
2699 orig_node->tt_initialised = true;
2702 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2703 struct batadv_tvlv_tt_change *tt_change,
2704 uint8_t ttvn, uint8_t *resp_src,
2705 uint16_t num_entries)
2707 struct batadv_orig_node *orig_node;
2709 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2710 if (!orig_node)
2711 goto out;
2713 /* Purge the old table first.. */
2714 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2715 "Received full table");
2717 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2718 ttvn);
2720 spin_lock_bh(&orig_node->tt_buff_lock);
2721 kfree(orig_node->tt_buff);
2722 orig_node->tt_buff_len = 0;
2723 orig_node->tt_buff = NULL;
2724 spin_unlock_bh(&orig_node->tt_buff_lock);
2726 atomic_set(&orig_node->last_ttvn, ttvn);
2728 out:
2729 if (orig_node)
2730 batadv_orig_node_free_ref(orig_node);
2733 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2734 struct batadv_orig_node *orig_node,
2735 uint16_t tt_num_changes, uint8_t ttvn,
2736 struct batadv_tvlv_tt_change *tt_change)
2738 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2739 tt_num_changes, ttvn);
2741 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2742 batadv_tt_len(tt_num_changes));
2743 atomic_set(&orig_node->last_ttvn, ttvn);
2747 * batadv_is_my_client - check if a client is served by the local node
2748 * @bat_priv: the bat priv with all the soft interface information
2749 * @addr: the mac adress of the client to check
2750 * @vid: VLAN identifier
2752 * Returns true if the client is served by this node, false otherwise.
2754 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2755 unsigned short vid)
2757 struct batadv_tt_local_entry *tt_local_entry;
2758 bool ret = false;
2760 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
2761 if (!tt_local_entry)
2762 goto out;
2763 /* Check if the client has been logically deleted (but is kept for
2764 * consistency purpose)
2766 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2767 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2768 goto out;
2769 ret = true;
2770 out:
2771 if (tt_local_entry)
2772 batadv_tt_local_entry_free_ref(tt_local_entry);
2773 return ret;
2777 * batadv_handle_tt_response - process incoming tt reply
2778 * @bat_priv: the bat priv with all the soft interface information
2779 * @tt_data: tt data containing the tt request information
2780 * @resp_src: mac address of tt reply sender
2781 * @num_entries: number of tt change entries appended to the tt data
2783 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2784 struct batadv_tvlv_tt_data *tt_data,
2785 uint8_t *resp_src, uint16_t num_entries)
2787 struct batadv_tt_req_node *node, *safe;
2788 struct batadv_orig_node *orig_node = NULL;
2789 struct batadv_tvlv_tt_change *tt_change;
2790 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2791 uint16_t change_offset;
2793 batadv_dbg(BATADV_DBG_TT, bat_priv,
2794 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2795 resp_src, tt_data->ttvn, num_entries,
2796 (tt_data->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2798 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2799 if (!orig_node)
2800 goto out;
2802 spin_lock_bh(&orig_node->tt_lock);
2804 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2805 change_offset *= ntohs(tt_data->num_vlan);
2806 change_offset += sizeof(*tt_data);
2807 tvlv_ptr += change_offset;
2809 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
2810 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
2811 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2812 resp_src, num_entries);
2813 } else {
2814 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2815 tt_data->ttvn, tt_change);
2818 /* Recalculate the CRC for this orig_node and store it */
2819 batadv_tt_global_update_crc(bat_priv, orig_node);
2821 spin_unlock_bh(&orig_node->tt_lock);
2823 /* Delete the tt_req_node from pending tt_requests list */
2824 spin_lock_bh(&bat_priv->tt.req_list_lock);
2825 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2826 if (!batadv_compare_eth(node->addr, resp_src))
2827 continue;
2828 list_del(&node->list);
2829 kfree(node);
2832 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2833 out:
2834 if (orig_node)
2835 batadv_orig_node_free_ref(orig_node);
2838 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2840 struct batadv_tt_roam_node *node, *safe;
2842 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2844 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2845 list_del(&node->list);
2846 kfree(node);
2849 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2852 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2854 struct batadv_tt_roam_node *node, *safe;
2856 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2857 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2858 if (!batadv_has_timed_out(node->first_time,
2859 BATADV_ROAMING_MAX_TIME))
2860 continue;
2862 list_del(&node->list);
2863 kfree(node);
2865 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2868 /* This function checks whether the client already reached the
2869 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2870 * will not be sent.
2872 * returns true if the ROAMING_ADV can be sent, false otherwise
2874 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2875 uint8_t *client)
2877 struct batadv_tt_roam_node *tt_roam_node;
2878 bool ret = false;
2880 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2881 /* The new tt_req will be issued only if I'm not waiting for a
2882 * reply from the same orig_node yet
2884 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2885 if (!batadv_compare_eth(tt_roam_node->addr, client))
2886 continue;
2888 if (batadv_has_timed_out(tt_roam_node->first_time,
2889 BATADV_ROAMING_MAX_TIME))
2890 continue;
2892 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2893 /* Sorry, you roamed too many times! */
2894 goto unlock;
2895 ret = true;
2896 break;
2899 if (!ret) {
2900 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2901 if (!tt_roam_node)
2902 goto unlock;
2904 tt_roam_node->first_time = jiffies;
2905 atomic_set(&tt_roam_node->counter,
2906 BATADV_ROAMING_MAX_COUNT - 1);
2907 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2909 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2910 ret = true;
2913 unlock:
2914 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2915 return ret;
2919 * batadv_send_roam_adv - send a roaming advertisement message
2920 * @bat_priv: the bat priv with all the soft interface information
2921 * @client: mac address of the roaming client
2922 * @vid: VLAN identifier
2923 * @orig_node: message destination
2925 * Send a ROAMING_ADV message to the node which was previously serving this
2926 * client. This is done to inform the node that from now on all traffic destined
2927 * for this particular roamed client has to be forwarded to the sender of the
2928 * roaming message.
2930 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2931 unsigned short vid,
2932 struct batadv_orig_node *orig_node)
2934 struct batadv_hard_iface *primary_if;
2935 struct batadv_tvlv_roam_adv tvlv_roam;
2937 primary_if = batadv_primary_if_get_selected(bat_priv);
2938 if (!primary_if)
2939 goto out;
2941 /* before going on we have to check whether the client has
2942 * already roamed to us too many times
2944 if (!batadv_tt_check_roam_count(bat_priv, client))
2945 goto out;
2947 batadv_dbg(BATADV_DBG_TT, bat_priv,
2948 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
2949 orig_node->orig, client, BATADV_PRINT_VID(vid));
2951 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2953 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
2954 tvlv_roam.vid = htons(vid);
2956 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2957 orig_node->orig, BATADV_TVLV_ROAM, 1,
2958 &tvlv_roam, sizeof(tvlv_roam));
2960 out:
2961 if (primary_if)
2962 batadv_hardif_free_ref(primary_if);
2965 static void batadv_tt_purge(struct work_struct *work)
2967 struct delayed_work *delayed_work;
2968 struct batadv_priv_tt *priv_tt;
2969 struct batadv_priv *bat_priv;
2971 delayed_work = container_of(work, struct delayed_work, work);
2972 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2973 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2975 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
2976 batadv_tt_global_purge(bat_priv);
2977 batadv_tt_req_purge(bat_priv);
2978 batadv_tt_roam_purge(bat_priv);
2980 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2981 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2984 void batadv_tt_free(struct batadv_priv *bat_priv)
2986 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
2987 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
2989 cancel_delayed_work_sync(&bat_priv->tt.work);
2991 batadv_tt_local_table_free(bat_priv);
2992 batadv_tt_global_table_free(bat_priv);
2993 batadv_tt_req_list_free(bat_priv);
2994 batadv_tt_changes_list_free(bat_priv);
2995 batadv_tt_roam_list_free(bat_priv);
2997 kfree(bat_priv->tt.last_changeset);
3001 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3002 * table and possibly count them in the TT size
3003 * @bat_priv: the bat priv with all the soft interface information
3004 * @flags: the flag to switch
3005 * @enable: whether to set or unset the flag
3006 * @count: whether to increase the TT size by the number of changed entries
3008 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3009 uint16_t flags, bool enable, bool count)
3011 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3012 struct batadv_tt_common_entry *tt_common_entry;
3013 uint16_t changed_num = 0;
3014 struct hlist_head *head;
3015 uint32_t i;
3017 if (!hash)
3018 return;
3020 for (i = 0; i < hash->size; i++) {
3021 head = &hash->table[i];
3023 rcu_read_lock();
3024 hlist_for_each_entry_rcu(tt_common_entry,
3025 head, hash_entry) {
3026 if (enable) {
3027 if ((tt_common_entry->flags & flags) == flags)
3028 continue;
3029 tt_common_entry->flags |= flags;
3030 } else {
3031 if (!(tt_common_entry->flags & flags))
3032 continue;
3033 tt_common_entry->flags &= ~flags;
3035 changed_num++;
3037 if (!count)
3038 continue;
3040 batadv_tt_local_size_inc(bat_priv,
3041 tt_common_entry->vid);
3043 rcu_read_unlock();
3047 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3048 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3050 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3051 struct batadv_tt_common_entry *tt_common;
3052 struct batadv_tt_local_entry *tt_local;
3053 struct hlist_node *node_tmp;
3054 struct hlist_head *head;
3055 spinlock_t *list_lock; /* protects write access to the hash lists */
3056 uint32_t i;
3058 if (!hash)
3059 return;
3061 for (i = 0; i < hash->size; i++) {
3062 head = &hash->table[i];
3063 list_lock = &hash->list_locks[i];
3065 spin_lock_bh(list_lock);
3066 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3067 hash_entry) {
3068 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3069 continue;
3071 batadv_dbg(BATADV_DBG_TT, bat_priv,
3072 "Deleting local tt entry (%pM, vid: %d): pending\n",
3073 tt_common->addr,
3074 BATADV_PRINT_VID(tt_common->vid));
3076 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3077 hlist_del_rcu(&tt_common->hash_entry);
3078 tt_local = container_of(tt_common,
3079 struct batadv_tt_local_entry,
3080 common);
3081 batadv_tt_local_entry_free_ref(tt_local);
3083 spin_unlock_bh(list_lock);
3088 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3089 * which have been queued in the time since the last commit
3090 * @bat_priv: the bat priv with all the soft interface information
3092 * Caller must hold tt->commit_lock.
3094 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3096 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3097 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3098 batadv_tt_tvlv_container_update(bat_priv);
3099 return;
3102 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3104 batadv_tt_local_purge_pending_clients(bat_priv);
3105 batadv_tt_local_update_crc(bat_priv);
3107 /* Increment the TTVN only once per OGM interval */
3108 atomic_inc(&bat_priv->tt.vn);
3109 batadv_dbg(BATADV_DBG_TT, bat_priv,
3110 "Local changes committed, updating to ttvn %u\n",
3111 (uint8_t)atomic_read(&bat_priv->tt.vn));
3113 /* reset the sending counter */
3114 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3115 batadv_tt_tvlv_container_update(bat_priv);
3119 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3120 * have been queued in the time since the last commit
3121 * @bat_priv: the bat priv with all the soft interface information
3123 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3125 spin_lock_bh(&bat_priv->tt.commit_lock);
3126 batadv_tt_local_commit_changes_nolock(bat_priv);
3127 spin_unlock_bh(&bat_priv->tt.commit_lock);
3130 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
3131 uint8_t *dst, unsigned short vid)
3133 struct batadv_tt_local_entry *tt_local_entry = NULL;
3134 struct batadv_tt_global_entry *tt_global_entry = NULL;
3135 struct batadv_softif_vlan *vlan;
3136 bool ret = false;
3138 vlan = batadv_softif_vlan_get(bat_priv, vid);
3139 if (!vlan || !atomic_read(&vlan->ap_isolation))
3140 goto out;
3142 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3143 if (!tt_local_entry)
3144 goto out;
3146 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3147 if (!tt_global_entry)
3148 goto out;
3150 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3151 goto out;
3153 ret = true;
3155 out:
3156 if (vlan)
3157 batadv_softif_vlan_free_ref(vlan);
3158 if (tt_global_entry)
3159 batadv_tt_global_entry_free_ref(tt_global_entry);
3160 if (tt_local_entry)
3161 batadv_tt_local_entry_free_ref(tt_local_entry);
3162 return ret;
3166 * batadv_tt_update_orig - update global translation table with new tt
3167 * information received via ogms
3168 * @bat_priv: the bat priv with all the soft interface information
3169 * @orig: the orig_node of the ogm
3170 * @tt_vlan: pointer to the first tvlv VLAN entry
3171 * @tt_num_vlan: number of tvlv VLAN entries
3172 * @tt_change: pointer to the first entry in the TT buffer
3173 * @tt_num_changes: number of tt changes inside the tt buffer
3174 * @ttvn: translation table version number of this changeset
3175 * @tt_crc: crc32 checksum of orig node's translation table
3177 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3178 struct batadv_orig_node *orig_node,
3179 const void *tt_buff, uint16_t tt_num_vlan,
3180 struct batadv_tvlv_tt_change *tt_change,
3181 uint16_t tt_num_changes, uint8_t ttvn)
3183 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
3184 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3185 bool full_table = true;
3187 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3188 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3189 * increased by one -> we can apply the attached changes
3191 if ((!orig_node->tt_initialised && ttvn == 1) ||
3192 ttvn - orig_ttvn == 1) {
3193 /* the OGM could not contain the changes due to their size or
3194 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3195 * times.
3196 * In this case send a tt request
3198 if (!tt_num_changes) {
3199 full_table = false;
3200 goto request_table;
3203 spin_lock_bh(&orig_node->tt_lock);
3205 tt_change = (struct batadv_tvlv_tt_change *)tt_buff;
3206 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3207 ttvn, tt_change);
3209 /* Even if we received the precomputed crc with the OGM, we
3210 * prefer to recompute it to spot any possible inconsistency
3211 * in the global table
3213 batadv_tt_global_update_crc(bat_priv, orig_node);
3215 spin_unlock_bh(&orig_node->tt_lock);
3217 /* The ttvn alone is not enough to guarantee consistency
3218 * because a single value could represent different states
3219 * (due to the wrap around). Thus a node has to check whether
3220 * the resulting table (after applying the changes) is still
3221 * consistent or not. E.g. a node could disconnect while its
3222 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3223 * checking the CRC value is mandatory to detect the
3224 * inconsistency
3226 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3227 tt_num_vlan))
3228 goto request_table;
3229 } else {
3230 /* if we missed more than one change or our tables are not
3231 * in sync anymore -> request fresh tt data
3233 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
3234 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3235 tt_num_vlan)) {
3236 request_table:
3237 batadv_dbg(BATADV_DBG_TT, bat_priv,
3238 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3239 orig_node->orig, ttvn, orig_ttvn,
3240 tt_num_changes);
3241 batadv_send_tt_request(bat_priv, orig_node, ttvn,
3242 tt_vlan, tt_num_vlan,
3243 full_table);
3244 return;
3250 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3251 * @bat_priv: the bat priv with all the soft interface information
3252 * @addr: the mac address of the client to check
3253 * @vid: VLAN identifier
3255 * Returns true if we know that the client has moved from its old originator
3256 * to another one. This entry is still kept for consistency purposes and will be
3257 * deleted later by a DEL or because of timeout
3259 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3260 uint8_t *addr, unsigned short vid)
3262 struct batadv_tt_global_entry *tt_global_entry;
3263 bool ret = false;
3265 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3266 if (!tt_global_entry)
3267 goto out;
3269 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3270 batadv_tt_global_entry_free_ref(tt_global_entry);
3271 out:
3272 return ret;
3276 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3277 * @bat_priv: the bat priv with all the soft interface information
3278 * @addr: the mac address of the local client to query
3279 * @vid: VLAN identifier
3281 * Returns true if the local client is known to be roaming (it is not served by
3282 * this node anymore) or not. If yes, the client is still present in the table
3283 * to keep the latter consistent with the node TTVN
3285 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3286 uint8_t *addr, unsigned short vid)
3288 struct batadv_tt_local_entry *tt_local_entry;
3289 bool ret = false;
3291 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3292 if (!tt_local_entry)
3293 goto out;
3295 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3296 batadv_tt_local_entry_free_ref(tt_local_entry);
3297 out:
3298 return ret;
3301 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3302 struct batadv_orig_node *orig_node,
3303 const unsigned char *addr,
3304 unsigned short vid)
3306 bool ret = false;
3308 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3309 BATADV_TT_CLIENT_TEMP,
3310 atomic_read(&orig_node->last_ttvn)))
3311 goto out;
3313 batadv_dbg(BATADV_DBG_TT, bat_priv,
3314 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3315 addr, BATADV_PRINT_VID(vid), orig_node->orig);
3316 ret = true;
3317 out:
3318 return ret;
3322 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3323 * maximum packet size that can be transported through the mesh
3324 * @soft_iface: netdev struct of the mesh interface
3326 * Remove entries older than 'timeout' and half timeout if more entries need
3327 * to be removed.
3329 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3331 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3332 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3333 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3334 bool reduced = false;
3336 spin_lock_bh(&bat_priv->tt.commit_lock);
3338 while (true) {
3339 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3340 if (packet_size_max >= table_size)
3341 break;
3343 batadv_tt_local_purge(bat_priv, timeout);
3344 batadv_tt_local_purge_pending_clients(bat_priv);
3346 timeout /= 2;
3347 reduced = true;
3348 net_ratelimited_function(batadv_info, soft_iface,
3349 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3350 packet_size_max);
3353 /* commit these changes immediately, to avoid synchronization problem
3354 * with the TTVN
3356 if (reduced)
3357 batadv_tt_local_commit_changes_nolock(bat_priv);
3359 spin_unlock_bh(&bat_priv->tt.commit_lock);
3363 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3364 * @bat_priv: the bat priv with all the soft interface information
3365 * @orig: the orig_node of the ogm
3366 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3367 * @tvlv_value: tvlv buffer containing the gateway data
3368 * @tvlv_value_len: tvlv buffer length
3370 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3371 struct batadv_orig_node *orig,
3372 uint8_t flags, void *tvlv_value,
3373 uint16_t tvlv_value_len)
3375 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3376 struct batadv_tvlv_tt_change *tt_change;
3377 struct batadv_tvlv_tt_data *tt_data;
3378 uint16_t num_entries, num_vlan;
3380 if (tvlv_value_len < sizeof(*tt_data))
3381 return;
3383 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3384 tvlv_value_len -= sizeof(*tt_data);
3386 num_vlan = ntohs(tt_data->num_vlan);
3388 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3389 return;
3391 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3392 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3393 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3395 num_entries = batadv_tt_entries(tvlv_value_len);
3397 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3398 num_entries, tt_data->ttvn);
3402 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3403 * container
3404 * @bat_priv: the bat priv with all the soft interface information
3405 * @src: mac address of tt tvlv sender
3406 * @dst: mac address of tt tvlv recipient
3407 * @tvlv_value: tvlv buffer containing the tt data
3408 * @tvlv_value_len: tvlv buffer length
3410 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3411 * otherwise.
3413 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3414 uint8_t *src, uint8_t *dst,
3415 void *tvlv_value,
3416 uint16_t tvlv_value_len)
3418 struct batadv_tvlv_tt_data *tt_data;
3419 uint16_t tt_vlan_len, tt_num_entries;
3420 char tt_flag;
3421 bool ret;
3423 if (tvlv_value_len < sizeof(*tt_data))
3424 return NET_RX_SUCCESS;
3426 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3427 tvlv_value_len -= sizeof(*tt_data);
3429 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3430 tt_vlan_len *= ntohs(tt_data->num_vlan);
3432 if (tvlv_value_len < tt_vlan_len)
3433 return NET_RX_SUCCESS;
3435 tvlv_value_len -= tt_vlan_len;
3436 tt_num_entries = batadv_tt_entries(tvlv_value_len);
3438 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3439 case BATADV_TT_REQUEST:
3440 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3442 /* If this node cannot provide a TT response the tt_request is
3443 * forwarded
3445 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3446 if (!ret) {
3447 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3448 tt_flag = 'F';
3449 else
3450 tt_flag = '.';
3452 batadv_dbg(BATADV_DBG_TT, bat_priv,
3453 "Routing TT_REQUEST to %pM [%c]\n",
3454 dst, tt_flag);
3455 /* tvlv API will re-route the packet */
3456 return NET_RX_DROP;
3458 break;
3459 case BATADV_TT_RESPONSE:
3460 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3462 if (batadv_is_my_mac(bat_priv, dst)) {
3463 batadv_handle_tt_response(bat_priv, tt_data,
3464 src, tt_num_entries);
3465 return NET_RX_SUCCESS;
3468 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3469 tt_flag = 'F';
3470 else
3471 tt_flag = '.';
3473 batadv_dbg(BATADV_DBG_TT, bat_priv,
3474 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3476 /* tvlv API will re-route the packet */
3477 return NET_RX_DROP;
3480 return NET_RX_SUCCESS;
3484 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3485 * @bat_priv: the bat priv with all the soft interface information
3486 * @src: mac address of tt tvlv sender
3487 * @dst: mac address of tt tvlv recipient
3488 * @tvlv_value: tvlv buffer containing the tt data
3489 * @tvlv_value_len: tvlv buffer length
3491 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3492 * otherwise.
3494 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3495 uint8_t *src, uint8_t *dst,
3496 void *tvlv_value,
3497 uint16_t tvlv_value_len)
3499 struct batadv_tvlv_roam_adv *roaming_adv;
3500 struct batadv_orig_node *orig_node = NULL;
3502 /* If this node is not the intended recipient of the
3503 * roaming advertisement the packet is forwarded
3504 * (the tvlv API will re-route the packet).
3506 if (!batadv_is_my_mac(bat_priv, dst))
3507 return NET_RX_DROP;
3509 if (tvlv_value_len < sizeof(*roaming_adv))
3510 goto out;
3512 orig_node = batadv_orig_hash_find(bat_priv, src);
3513 if (!orig_node)
3514 goto out;
3516 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3517 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3519 batadv_dbg(BATADV_DBG_TT, bat_priv,
3520 "Received ROAMING_ADV from %pM (client %pM)\n",
3521 src, roaming_adv->client);
3523 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3524 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3525 atomic_read(&orig_node->last_ttvn) + 1);
3527 out:
3528 if (orig_node)
3529 batadv_orig_node_free_ref(orig_node);
3530 return NET_RX_SUCCESS;
3534 * batadv_tt_init - initialise the translation table internals
3535 * @bat_priv: the bat priv with all the soft interface information
3537 * Return 0 on success or negative error number in case of failure.
3539 int batadv_tt_init(struct batadv_priv *bat_priv)
3541 int ret;
3543 /* synchronized flags must be remote */
3544 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3546 ret = batadv_tt_local_init(bat_priv);
3547 if (ret < 0)
3548 return ret;
3550 ret = batadv_tt_global_init(bat_priv);
3551 if (ret < 0)
3552 return ret;
3554 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3555 batadv_tt_tvlv_unicast_handler_v1,
3556 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3558 batadv_tvlv_handler_register(bat_priv, NULL,
3559 batadv_roam_tvlv_unicast_handler_v1,
3560 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3562 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3563 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3564 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3566 return 1;