1 /* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "translation-table.h"
21 #include <linux/atomic.h>
22 #include <linux/bitops.h>
23 #include <linux/bug.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/compiler.h>
26 #include <linux/crc32c.h>
27 #include <linux/errno.h>
28 #include <linux/etherdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/jhash.h>
32 #include <linux/jiffies.h>
33 #include <linux/kernel.h>
34 #include <linux/list.h>
35 #include <linux/lockdep.h>
36 #include <linux/netdevice.h>
37 #include <linux/rculist.h>
38 #include <linux/rcupdate.h>
39 #include <linux/seq_file.h>
40 #include <linux/slab.h>
41 #include <linux/spinlock.h>
42 #include <linux/stddef.h>
43 #include <linux/string.h>
44 #include <linux/workqueue.h>
45 #include <net/net_namespace.h>
47 #include "bridge_loop_avoidance.h"
48 #include "hard-interface.h"
50 #include "multicast.h"
51 #include "originator.h"
53 #include "soft-interface.h"
56 static struct lock_class_key batadv_tt_local_hash_lock_class_key
;
57 static struct lock_class_key batadv_tt_global_hash_lock_class_key
;
59 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
61 struct batadv_orig_node
*orig_node
);
62 static void batadv_tt_purge(struct work_struct
*work
);
64 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
);
65 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
66 struct batadv_orig_node
*orig_node
,
67 const unsigned char *addr
,
68 unsigned short vid
, const char *message
,
71 /* returns 1 if they are the same mac addr */
72 static int batadv_compare_tt(const struct hlist_node
*node
, const void *data2
)
74 const void *data1
= container_of(node
, struct batadv_tt_common_entry
,
77 return batadv_compare_eth(data1
, data2
);
81 * batadv_choose_tt - return the index of the tt entry in the hash table
82 * @data: pointer to the tt_common_entry object to map
83 * @size: the size of the hash table
85 * Returns the hash index where the object represented by 'data' should be
88 static inline uint32_t batadv_choose_tt(const void *data
, uint32_t size
)
90 struct batadv_tt_common_entry
*tt
;
93 tt
= (struct batadv_tt_common_entry
*)data
;
94 hash
= jhash(&tt
->addr
, ETH_ALEN
, hash
);
95 hash
= jhash(&tt
->vid
, sizeof(tt
->vid
), hash
);
101 * batadv_tt_hash_find - look for a client in the given hash table
102 * @hash: the hash table to search
103 * @addr: the mac address of the client to look for
104 * @vid: VLAN identifier
106 * Returns a pointer to the tt_common struct belonging to the searched client if
107 * found, NULL otherwise.
109 static struct batadv_tt_common_entry
*
110 batadv_tt_hash_find(struct batadv_hashtable
*hash
, const uint8_t *addr
,
113 struct hlist_head
*head
;
114 struct batadv_tt_common_entry to_search
, *tt
, *tt_tmp
= NULL
;
120 ether_addr_copy(to_search
.addr
, addr
);
123 index
= batadv_choose_tt(&to_search
, hash
->size
);
124 head
= &hash
->table
[index
];
127 hlist_for_each_entry_rcu(tt
, head
, hash_entry
) {
128 if (!batadv_compare_eth(tt
, addr
))
134 if (!atomic_inc_not_zero(&tt
->refcount
))
146 * batadv_tt_local_hash_find - search the local table for a given client
147 * @bat_priv: the bat priv with all the soft interface information
148 * @addr: the mac address of the client to look for
149 * @vid: VLAN identifier
151 * Returns a pointer to the corresponding tt_local_entry struct if the client is
152 * found, NULL otherwise.
154 static struct batadv_tt_local_entry
*
155 batadv_tt_local_hash_find(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
158 struct batadv_tt_common_entry
*tt_common_entry
;
159 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
161 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.local_hash
, addr
,
164 tt_local_entry
= container_of(tt_common_entry
,
165 struct batadv_tt_local_entry
,
167 return tt_local_entry
;
171 * batadv_tt_global_hash_find - search the global table for a given client
172 * @bat_priv: the bat priv with all the soft interface information
173 * @addr: the mac address of the client to look for
174 * @vid: VLAN identifier
176 * Returns a pointer to the corresponding tt_global_entry struct if the client
177 * is found, NULL otherwise.
179 static struct batadv_tt_global_entry
*
180 batadv_tt_global_hash_find(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
183 struct batadv_tt_common_entry
*tt_common_entry
;
184 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
186 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.global_hash
, addr
,
189 tt_global_entry
= container_of(tt_common_entry
,
190 struct batadv_tt_global_entry
,
192 return tt_global_entry
;
196 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry
*tt_local_entry
)
198 if (atomic_dec_and_test(&tt_local_entry
->common
.refcount
))
199 kfree_rcu(tt_local_entry
, common
.rcu
);
203 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
204 * tt_global_entry and possibly free it
205 * @tt_global_entry: the object to free
208 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry
*tt_global_entry
)
210 if (atomic_dec_and_test(&tt_global_entry
->common
.refcount
)) {
211 batadv_tt_global_del_orig_list(tt_global_entry
);
212 kfree_rcu(tt_global_entry
, common
.rcu
);
217 * batadv_tt_global_hash_count - count the number of orig entries
218 * @hash: hash table containing the tt entries
219 * @addr: the mac address of the client to count entries for
220 * @vid: VLAN identifier
222 * Return the number of originators advertising the given address/data
223 * (excluding ourself).
225 int batadv_tt_global_hash_count(struct batadv_priv
*bat_priv
,
226 const uint8_t *addr
, unsigned short vid
)
228 struct batadv_tt_global_entry
*tt_global_entry
;
231 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
232 if (!tt_global_entry
)
235 count
= atomic_read(&tt_global_entry
->orig_list_count
);
236 batadv_tt_global_entry_free_ref(tt_global_entry
);
241 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head
*rcu
)
243 struct batadv_tt_orig_list_entry
*orig_entry
;
245 orig_entry
= container_of(rcu
, struct batadv_tt_orig_list_entry
, rcu
);
247 /* We are in an rcu callback here, therefore we cannot use
248 * batadv_orig_node_free_ref() and its call_rcu():
249 * An rcu_barrier() wouldn't wait for that to finish
251 batadv_orig_node_free_ref_now(orig_entry
->orig_node
);
256 * batadv_tt_local_size_mod - change the size by v of the local table identified
258 * @bat_priv: the bat priv with all the soft interface information
259 * @vid: the VLAN identifier of the sub-table to change
260 * @v: the amount to sum to the local table size
262 static void batadv_tt_local_size_mod(struct batadv_priv
*bat_priv
,
263 unsigned short vid
, int v
)
265 struct batadv_softif_vlan
*vlan
;
267 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
271 atomic_add(v
, &vlan
->tt
.num_entries
);
273 batadv_softif_vlan_free_ref(vlan
);
277 * batadv_tt_local_size_inc - increase by one the local table size for the given
279 * @bat_priv: the bat priv with all the soft interface information
280 * @vid: the VLAN identifier
282 static void batadv_tt_local_size_inc(struct batadv_priv
*bat_priv
,
285 batadv_tt_local_size_mod(bat_priv
, vid
, 1);
289 * batadv_tt_local_size_dec - decrease by one the local table size for the given
291 * @bat_priv: the bat priv with all the soft interface information
292 * @vid: the VLAN identifier
294 static void batadv_tt_local_size_dec(struct batadv_priv
*bat_priv
,
297 batadv_tt_local_size_mod(bat_priv
, vid
, -1);
301 * batadv_tt_global_size_mod - change the size by v of the local table
303 * @bat_priv: the bat priv with all the soft interface information
304 * @vid: the VLAN identifier
305 * @v: the amount to sum to the global table size
307 static void batadv_tt_global_size_mod(struct batadv_orig_node
*orig_node
,
308 unsigned short vid
, int v
)
310 struct batadv_orig_node_vlan
*vlan
;
312 vlan
= batadv_orig_node_vlan_new(orig_node
, vid
);
316 if (atomic_add_return(v
, &vlan
->tt
.num_entries
) == 0) {
317 spin_lock_bh(&orig_node
->vlan_list_lock
);
318 list_del_rcu(&vlan
->list
);
319 spin_unlock_bh(&orig_node
->vlan_list_lock
);
320 batadv_orig_node_vlan_free_ref(vlan
);
323 batadv_orig_node_vlan_free_ref(vlan
);
327 * batadv_tt_global_size_inc - increase by one the global table size for the
329 * @orig_node: the originator which global table size has to be decreased
330 * @vid: the vlan identifier
332 static void batadv_tt_global_size_inc(struct batadv_orig_node
*orig_node
,
335 batadv_tt_global_size_mod(orig_node
, vid
, 1);
339 * batadv_tt_global_size_dec - decrease by one the global table size for the
341 * @orig_node: the originator which global table size has to be decreased
342 * @vid: the vlan identifier
344 static void batadv_tt_global_size_dec(struct batadv_orig_node
*orig_node
,
347 batadv_tt_global_size_mod(orig_node
, vid
, -1);
351 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry
*orig_entry
)
353 if (!atomic_dec_and_test(&orig_entry
->refcount
))
356 call_rcu(&orig_entry
->rcu
, batadv_tt_orig_list_entry_free_rcu
);
360 * batadv_tt_local_event - store a local TT event (ADD/DEL)
361 * @bat_priv: the bat priv with all the soft interface information
362 * @tt_local_entry: the TT entry involved in the event
363 * @event_flags: flags to store in the event structure
365 static void batadv_tt_local_event(struct batadv_priv
*bat_priv
,
366 struct batadv_tt_local_entry
*tt_local_entry
,
369 struct batadv_tt_change_node
*tt_change_node
, *entry
, *safe
;
370 struct batadv_tt_common_entry
*common
= &tt_local_entry
->common
;
371 uint8_t flags
= common
->flags
| event_flags
;
372 bool event_removed
= false;
373 bool del_op_requested
, del_op_entry
;
375 tt_change_node
= kmalloc(sizeof(*tt_change_node
), GFP_ATOMIC
);
379 tt_change_node
->change
.flags
= flags
;
380 memset(tt_change_node
->change
.reserved
, 0,
381 sizeof(tt_change_node
->change
.reserved
));
382 ether_addr_copy(tt_change_node
->change
.addr
, common
->addr
);
383 tt_change_node
->change
.vid
= htons(common
->vid
);
385 del_op_requested
= flags
& BATADV_TT_CLIENT_DEL
;
387 /* check for ADD+DEL or DEL+ADD events */
388 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
389 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
391 if (!batadv_compare_eth(entry
->change
.addr
, common
->addr
))
394 /* DEL+ADD in the same orig interval have no effect and can be
395 * removed to avoid silly behaviour on the receiver side. The
396 * other way around (ADD+DEL) can happen in case of roaming of
397 * a client still in the NEW state. Roaming of NEW clients is
398 * now possible due to automatically recognition of "temporary"
401 del_op_entry
= entry
->change
.flags
& BATADV_TT_CLIENT_DEL
;
402 if (!del_op_requested
&& del_op_entry
)
404 if (del_op_requested
&& !del_op_entry
)
407 /* this is a second add in the same originator interval. It
408 * means that flags have been changed: update them!
410 if (!del_op_requested
&& !del_op_entry
)
411 entry
->change
.flags
= flags
;
415 list_del(&entry
->list
);
417 kfree(tt_change_node
);
418 event_removed
= true;
422 /* track the change in the OGMinterval list */
423 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt
.changes_list
);
426 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
429 atomic_dec(&bat_priv
->tt
.local_changes
);
431 atomic_inc(&bat_priv
->tt
.local_changes
);
435 * batadv_tt_len - compute length in bytes of given number of tt changes
436 * @changes_num: number of tt changes
438 * Returns computed length in bytes.
440 static int batadv_tt_len(int changes_num
)
442 return changes_num
* sizeof(struct batadv_tvlv_tt_change
);
446 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
447 * @tt_len: available space
449 * Returns the number of entries.
451 static uint16_t batadv_tt_entries(uint16_t tt_len
)
453 return tt_len
/ batadv_tt_len(1);
457 * batadv_tt_local_table_transmit_size - calculates the local translation table
458 * size when transmitted over the air
459 * @bat_priv: the bat priv with all the soft interface information
461 * Returns local translation table size in bytes.
463 static int batadv_tt_local_table_transmit_size(struct batadv_priv
*bat_priv
)
465 uint16_t num_vlan
= 0, tt_local_entries
= 0;
466 struct batadv_softif_vlan
*vlan
;
470 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
472 tt_local_entries
+= atomic_read(&vlan
->tt
.num_entries
);
476 /* header size of tvlv encapsulated tt response payload */
477 hdr_size
= sizeof(struct batadv_unicast_tvlv_packet
);
478 hdr_size
+= sizeof(struct batadv_tvlv_hdr
);
479 hdr_size
+= sizeof(struct batadv_tvlv_tt_data
);
480 hdr_size
+= num_vlan
* sizeof(struct batadv_tvlv_tt_vlan_data
);
482 return hdr_size
+ batadv_tt_len(tt_local_entries
);
485 static int batadv_tt_local_init(struct batadv_priv
*bat_priv
)
487 if (bat_priv
->tt
.local_hash
)
490 bat_priv
->tt
.local_hash
= batadv_hash_new(1024);
492 if (!bat_priv
->tt
.local_hash
)
495 batadv_hash_set_lock_class(bat_priv
->tt
.local_hash
,
496 &batadv_tt_local_hash_lock_class_key
);
501 static void batadv_tt_global_free(struct batadv_priv
*bat_priv
,
502 struct batadv_tt_global_entry
*tt_global
,
505 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
506 "Deleting global tt entry %pM (vid: %d): %s\n",
507 tt_global
->common
.addr
,
508 BATADV_PRINT_VID(tt_global
->common
.vid
), message
);
510 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
511 batadv_choose_tt
, &tt_global
->common
);
512 batadv_tt_global_entry_free_ref(tt_global
);
516 * batadv_tt_local_add - add a new client to the local table or update an
518 * @soft_iface: netdev struct of the mesh interface
519 * @addr: the mac address of the client to add
520 * @vid: VLAN identifier
521 * @ifindex: index of the interface where the client is connected to (useful to
522 * identify wireless clients)
523 * @mark: the value contained in the skb->mark field of the received packet (if
526 * Returns true if the client was successfully added, false otherwise.
528 bool batadv_tt_local_add(struct net_device
*soft_iface
, const uint8_t *addr
,
529 unsigned short vid
, int ifindex
, uint32_t mark
)
531 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
532 struct batadv_tt_local_entry
*tt_local
;
533 struct batadv_tt_global_entry
*tt_global
= NULL
;
534 struct batadv_softif_vlan
*vlan
;
535 struct net_device
*in_dev
= NULL
;
536 struct hlist_head
*head
;
537 struct batadv_tt_orig_list_entry
*orig_entry
;
538 int hash_added
, table_size
, packet_size_max
;
539 bool ret
= false, roamed_back
= false;
540 uint8_t remote_flags
;
543 if (ifindex
!= BATADV_NULL_IFINDEX
)
544 in_dev
= dev_get_by_index(&init_net
, ifindex
);
546 tt_local
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
548 if (!is_multicast_ether_addr(addr
))
549 tt_global
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
552 tt_local
->last_seen
= jiffies
;
553 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_PENDING
) {
554 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
555 "Re-adding pending client %pM (vid: %d)\n",
556 addr
, BATADV_PRINT_VID(vid
));
557 /* whatever the reason why the PENDING flag was set,
558 * this is a client which was enqueued to be removed in
559 * this orig_interval. Since it popped up again, the
560 * flag can be reset like it was never enqueued
562 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_PENDING
;
566 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
567 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
568 "Roaming client %pM (vid: %d) came back to its original location\n",
569 addr
, BATADV_PRINT_VID(vid
));
570 /* the ROAM flag is set because this client roamed away
571 * and the node got a roaming_advertisement message. Now
572 * that the client popped up again at its original
573 * location such flag can be unset
575 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
581 /* Ignore the client if we cannot send it in a full table response. */
582 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
583 table_size
+= batadv_tt_len(1);
584 packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
585 if (table_size
> packet_size_max
) {
586 net_ratelimited_function(batadv_info
, soft_iface
,
587 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
588 table_size
, packet_size_max
, addr
);
592 tt_local
= kmalloc(sizeof(*tt_local
), GFP_ATOMIC
);
596 /* increase the refcounter of the related vlan */
597 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
599 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
600 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
601 addr
, BATADV_PRINT_VID(vid
),
602 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
604 ether_addr_copy(tt_local
->common
.addr
, addr
);
605 /* The local entry has to be marked as NEW to avoid to send it in
606 * a full table response going out before the next ttvn increment
607 * (consistency check)
609 tt_local
->common
.flags
= BATADV_TT_CLIENT_NEW
;
610 tt_local
->common
.vid
= vid
;
611 if (batadv_is_wifi_netdev(in_dev
))
612 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
613 atomic_set(&tt_local
->common
.refcount
, 2);
614 tt_local
->last_seen
= jiffies
;
615 tt_local
->common
.added_at
= tt_local
->last_seen
;
617 /* the batman interface mac and multicast addresses should never be
620 if (batadv_compare_eth(addr
, soft_iface
->dev_addr
) ||
621 is_multicast_ether_addr(addr
))
622 tt_local
->common
.flags
|= BATADV_TT_CLIENT_NOPURGE
;
624 hash_added
= batadv_hash_add(bat_priv
->tt
.local_hash
, batadv_compare_tt
,
625 batadv_choose_tt
, &tt_local
->common
,
626 &tt_local
->common
.hash_entry
);
628 if (unlikely(hash_added
!= 0)) {
629 /* remove the reference for the hash */
630 batadv_tt_local_entry_free_ref(tt_local
);
631 batadv_softif_vlan_free_ref(vlan
);
636 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
639 /* Check whether it is a roaming, but don't do anything if the roaming
640 * process has already been handled
642 if (tt_global
&& !(tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
)) {
643 /* These node are probably going to update their tt table */
644 head
= &tt_global
->orig_list
;
646 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
647 batadv_send_roam_adv(bat_priv
, tt_global
->common
.addr
,
648 tt_global
->common
.vid
,
649 orig_entry
->orig_node
);
653 batadv_tt_global_free(bat_priv
, tt_global
,
657 /* The global entry has to be marked as ROAMING and
658 * has to be kept for consistency purpose
660 tt_global
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
661 tt_global
->roam_at
= jiffies
;
665 /* store the current remote flags before altering them. This helps
666 * understanding is flags are changing or not
668 remote_flags
= tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
;
670 if (batadv_is_wifi_netdev(in_dev
))
671 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
673 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_WIFI
;
675 /* check the mark in the skb: if it's equal to the configured
676 * isolation_mark, it means the packet is coming from an isolated
679 match_mark
= (mark
& bat_priv
->isolation_mark_mask
);
680 if (bat_priv
->isolation_mark_mask
&&
681 match_mark
== bat_priv
->isolation_mark
)
682 tt_local
->common
.flags
|= BATADV_TT_CLIENT_ISOLA
;
684 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ISOLA
;
686 /* if any "dynamic" flag has been modified, resend an ADD event for this
687 * entry so that all the nodes can get the new flags
689 if (remote_flags
^ (tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
))
690 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
697 batadv_tt_local_entry_free_ref(tt_local
);
699 batadv_tt_global_entry_free_ref(tt_global
);
704 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
705 * within a TT Response directed to another node
706 * @orig_node: originator for which the TT data has to be prepared
707 * @tt_data: uninitialised pointer to the address of the TVLV buffer
708 * @tt_change: uninitialised pointer to the address of the area where the TT
709 * changed can be stored
710 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
711 * function reserves the amount of space needed to send the entire global TT
712 * table. In case of success the value is updated with the real amount of
715 * Allocate the needed amount of memory for the entire TT TVLV and write its
716 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
717 * objects, one per active VLAN served by the originator node.
719 * Return the size of the allocated buffer or 0 in case of failure.
722 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node
*orig_node
,
723 struct batadv_tvlv_tt_data
**tt_data
,
724 struct batadv_tvlv_tt_change
**tt_change
,
727 uint16_t num_vlan
= 0, num_entries
= 0, change_offset
, tvlv_len
;
728 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
729 struct batadv_orig_node_vlan
*vlan
;
730 uint8_t *tt_change_ptr
;
733 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
735 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
738 change_offset
= sizeof(**tt_data
);
739 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
741 /* if tt_len is negative, allocate the space needed by the full table */
743 *tt_len
= batadv_tt_len(num_entries
);
746 tvlv_len
+= change_offset
;
748 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
754 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
755 (*tt_data
)->ttvn
= atomic_read(&orig_node
->last_ttvn
);
756 (*tt_data
)->num_vlan
= htons(num_vlan
);
758 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
759 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
760 tt_vlan
->vid
= htons(vlan
->vid
);
761 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
766 tt_change_ptr
= (uint8_t *)*tt_data
+ change_offset
;
767 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
775 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
777 * @bat_priv: the bat priv with all the soft interface information
778 * @tt_data: uninitialised pointer to the address of the TVLV buffer
779 * @tt_change: uninitialised pointer to the address of the area where the TT
780 * changes can be stored
781 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
782 * function reserves the amount of space needed to send the entire local TT
783 * table. In case of success the value is updated with the real amount of
786 * Allocate the needed amount of memory for the entire TT TVLV and write its
787 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
788 * objects, one per active VLAN.
790 * Return the size of the allocated buffer or 0 in case of failure.
793 batadv_tt_prepare_tvlv_local_data(struct batadv_priv
*bat_priv
,
794 struct batadv_tvlv_tt_data
**tt_data
,
795 struct batadv_tvlv_tt_change
**tt_change
,
798 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
799 struct batadv_softif_vlan
*vlan
;
800 uint16_t num_vlan
= 0, num_entries
= 0, tvlv_len
;
801 uint8_t *tt_change_ptr
;
805 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
807 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
810 change_offset
= sizeof(**tt_data
);
811 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
813 /* if tt_len is negative, allocate the space needed by the full table */
815 *tt_len
= batadv_tt_len(num_entries
);
818 tvlv_len
+= change_offset
;
820 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
826 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
827 (*tt_data
)->ttvn
= atomic_read(&bat_priv
->tt
.vn
);
828 (*tt_data
)->num_vlan
= htons(num_vlan
);
830 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
831 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
832 tt_vlan
->vid
= htons(vlan
->vid
);
833 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
838 tt_change_ptr
= (uint8_t *)*tt_data
+ change_offset
;
839 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
847 * batadv_tt_tvlv_container_update - update the translation table tvlv container
848 * after local tt changes have been committed
849 * @bat_priv: the bat priv with all the soft interface information
851 static void batadv_tt_tvlv_container_update(struct batadv_priv
*bat_priv
)
853 struct batadv_tt_change_node
*entry
, *safe
;
854 struct batadv_tvlv_tt_data
*tt_data
;
855 struct batadv_tvlv_tt_change
*tt_change
;
856 int tt_diff_len
, tt_change_len
= 0;
857 int tt_diff_entries_num
= 0, tt_diff_entries_count
= 0;
860 tt_diff_entries_num
= atomic_read(&bat_priv
->tt
.local_changes
);
861 tt_diff_len
= batadv_tt_len(tt_diff_entries_num
);
863 /* if we have too many changes for one packet don't send any
864 * and wait for the tt table request which will be fragmented
866 if (tt_diff_len
> bat_priv
->soft_iface
->mtu
)
869 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
, &tt_data
,
870 &tt_change
, &tt_diff_len
);
874 tt_data
->flags
= BATADV_TT_OGM_DIFF
;
876 if (tt_diff_len
== 0)
877 goto container_register
;
879 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
880 atomic_set(&bat_priv
->tt
.local_changes
, 0);
882 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
884 if (tt_diff_entries_count
< tt_diff_entries_num
) {
885 memcpy(tt_change
+ tt_diff_entries_count
,
887 sizeof(struct batadv_tvlv_tt_change
));
888 tt_diff_entries_count
++;
890 list_del(&entry
->list
);
893 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
895 /* Keep the buffer for possible tt_request */
896 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
897 kfree(bat_priv
->tt
.last_changeset
);
898 bat_priv
->tt
.last_changeset_len
= 0;
899 bat_priv
->tt
.last_changeset
= NULL
;
900 tt_change_len
= batadv_tt_len(tt_diff_entries_count
);
901 /* check whether this new OGM has no changes due to size problems */
902 if (tt_diff_entries_count
> 0) {
903 /* if kmalloc() fails we will reply with the full table
904 * instead of providing the diff
906 bat_priv
->tt
.last_changeset
= kzalloc(tt_diff_len
, GFP_ATOMIC
);
907 if (bat_priv
->tt
.last_changeset
) {
908 memcpy(bat_priv
->tt
.last_changeset
,
909 tt_change
, tt_change_len
);
910 bat_priv
->tt
.last_changeset_len
= tt_diff_len
;
913 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
916 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_TT
, 1, tt_data
,
921 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
923 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
924 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
925 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
926 struct batadv_tt_common_entry
*tt_common_entry
;
927 struct batadv_tt_local_entry
*tt_local
;
928 struct batadv_hard_iface
*primary_if
;
929 struct batadv_softif_vlan
*vlan
;
930 struct hlist_head
*head
;
935 unsigned long last_seen_jiffies
;
937 uint16_t np_flag
= BATADV_TT_CLIENT_NOPURGE
;
939 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
944 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
945 net_dev
->name
, (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
946 seq_printf(seq
, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
947 "Flags", "Last seen", "CRC");
949 for (i
= 0; i
< hash
->size
; i
++) {
950 head
= &hash
->table
[i
];
953 hlist_for_each_entry_rcu(tt_common_entry
,
955 tt_local
= container_of(tt_common_entry
,
956 struct batadv_tt_local_entry
,
958 vid
= tt_common_entry
->vid
;
959 last_seen_jiffies
= jiffies
- tt_local
->last_seen
;
960 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
961 last_seen_secs
= last_seen_msecs
/ 1000;
962 last_seen_msecs
= last_seen_msecs
% 1000;
964 no_purge
= tt_common_entry
->flags
& np_flag
;
966 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
968 seq_printf(seq
, "Cannot retrieve VLAN %d\n",
969 BATADV_PRINT_VID(vid
));
974 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
975 tt_common_entry
->addr
,
976 BATADV_PRINT_VID(tt_common_entry
->vid
),
977 ((tt_common_entry
->flags
&
978 BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
979 no_purge
? 'P' : '.',
980 ((tt_common_entry
->flags
&
981 BATADV_TT_CLIENT_NEW
) ? 'N' : '.'),
982 ((tt_common_entry
->flags
&
983 BATADV_TT_CLIENT_PENDING
) ? 'X' : '.'),
984 ((tt_common_entry
->flags
&
985 BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
986 ((tt_common_entry
->flags
&
987 BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
988 no_purge
? 0 : last_seen_secs
,
989 no_purge
? 0 : last_seen_msecs
,
992 batadv_softif_vlan_free_ref(vlan
);
998 batadv_hardif_free_ref(primary_if
);
1003 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
1004 struct batadv_tt_local_entry
*tt_local_entry
,
1005 uint16_t flags
, const char *message
)
1007 batadv_tt_local_event(bat_priv
, tt_local_entry
, flags
);
1009 /* The local client has to be marked as "pending to be removed" but has
1010 * to be kept in the table in order to send it in a full table
1011 * response issued before the net ttvn increment (consistency check)
1013 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
1015 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1016 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1017 tt_local_entry
->common
.addr
,
1018 BATADV_PRINT_VID(tt_local_entry
->common
.vid
), message
);
1022 * batadv_tt_local_remove - logically remove an entry from the local table
1023 * @bat_priv: the bat priv with all the soft interface information
1024 * @addr: the MAC address of the client to remove
1025 * @vid: VLAN identifier
1026 * @message: message to append to the log on deletion
1027 * @roaming: true if the deletion is due to a roaming event
1029 * Returns the flags assigned to the local entry before being deleted
1031 uint16_t batadv_tt_local_remove(struct batadv_priv
*bat_priv
,
1032 const uint8_t *addr
, unsigned short vid
,
1033 const char *message
, bool roaming
)
1035 struct batadv_tt_local_entry
*tt_local_entry
;
1036 uint16_t flags
, curr_flags
= BATADV_NO_FLAGS
;
1037 struct batadv_softif_vlan
*vlan
;
1039 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
1040 if (!tt_local_entry
)
1043 curr_flags
= tt_local_entry
->common
.flags
;
1045 flags
= BATADV_TT_CLIENT_DEL
;
1046 /* if this global entry addition is due to a roaming, the node has to
1047 * mark the local entry as "roamed" in order to correctly reroute
1051 flags
|= BATADV_TT_CLIENT_ROAM
;
1052 /* mark the local client as ROAMed */
1053 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1056 if (!(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
)) {
1057 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
,
1061 /* if this client has been added right now, it is possible to
1062 * immediately purge it
1064 batadv_tt_local_event(bat_priv
, tt_local_entry
, BATADV_TT_CLIENT_DEL
);
1065 hlist_del_rcu(&tt_local_entry
->common
.hash_entry
);
1066 batadv_tt_local_entry_free_ref(tt_local_entry
);
1068 /* decrease the reference held for this vlan */
1069 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
1070 batadv_softif_vlan_free_ref(vlan
);
1071 batadv_softif_vlan_free_ref(vlan
);
1075 batadv_tt_local_entry_free_ref(tt_local_entry
);
1081 * batadv_tt_local_purge_list - purge inactive tt local entries
1082 * @bat_priv: the bat priv with all the soft interface information
1083 * @head: pointer to the list containing the local tt entries
1084 * @timeout: parameter deciding whether a given tt local entry is considered
1087 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
1088 struct hlist_head
*head
,
1091 struct batadv_tt_local_entry
*tt_local_entry
;
1092 struct batadv_tt_common_entry
*tt_common_entry
;
1093 struct hlist_node
*node_tmp
;
1095 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
, head
,
1097 tt_local_entry
= container_of(tt_common_entry
,
1098 struct batadv_tt_local_entry
,
1100 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
1103 /* entry already marked for deletion */
1104 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1107 if (!batadv_has_timed_out(tt_local_entry
->last_seen
, timeout
))
1110 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
1111 BATADV_TT_CLIENT_DEL
, "timed out");
1116 * batadv_tt_local_purge - purge inactive tt local entries
1117 * @bat_priv: the bat priv with all the soft interface information
1118 * @timeout: parameter deciding whether a given tt local entry is considered
1121 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
,
1124 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1125 struct hlist_head
*head
;
1126 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1129 for (i
= 0; i
< hash
->size
; i
++) {
1130 head
= &hash
->table
[i
];
1131 list_lock
= &hash
->list_locks
[i
];
1133 spin_lock_bh(list_lock
);
1134 batadv_tt_local_purge_list(bat_priv
, head
, timeout
);
1135 spin_unlock_bh(list_lock
);
1139 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
1141 struct batadv_hashtable
*hash
;
1142 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1143 struct batadv_tt_common_entry
*tt_common_entry
;
1144 struct batadv_tt_local_entry
*tt_local
;
1145 struct batadv_softif_vlan
*vlan
;
1146 struct hlist_node
*node_tmp
;
1147 struct hlist_head
*head
;
1150 if (!bat_priv
->tt
.local_hash
)
1153 hash
= bat_priv
->tt
.local_hash
;
1155 for (i
= 0; i
< hash
->size
; i
++) {
1156 head
= &hash
->table
[i
];
1157 list_lock
= &hash
->list_locks
[i
];
1159 spin_lock_bh(list_lock
);
1160 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1162 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1163 tt_local
= container_of(tt_common_entry
,
1164 struct batadv_tt_local_entry
,
1167 /* decrease the reference held for this vlan */
1168 vlan
= batadv_softif_vlan_get(bat_priv
,
1169 tt_common_entry
->vid
);
1170 batadv_softif_vlan_free_ref(vlan
);
1171 batadv_softif_vlan_free_ref(vlan
);
1173 batadv_tt_local_entry_free_ref(tt_local
);
1175 spin_unlock_bh(list_lock
);
1178 batadv_hash_destroy(hash
);
1180 bat_priv
->tt
.local_hash
= NULL
;
1183 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
1185 if (bat_priv
->tt
.global_hash
)
1188 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
1190 if (!bat_priv
->tt
.global_hash
)
1193 batadv_hash_set_lock_class(bat_priv
->tt
.global_hash
,
1194 &batadv_tt_global_hash_lock_class_key
);
1199 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
1201 struct batadv_tt_change_node
*entry
, *safe
;
1203 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
1205 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
1207 list_del(&entry
->list
);
1211 atomic_set(&bat_priv
->tt
.local_changes
, 0);
1212 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
1215 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1216 * batadv_tt_global_entry list
1218 * returns it with an increased refcounter, NULL if not found
1220 static struct batadv_tt_orig_list_entry
*
1221 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
1222 const struct batadv_orig_node
*orig_node
)
1224 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
1225 const struct hlist_head
*head
;
1228 head
= &entry
->orig_list
;
1229 hlist_for_each_entry_rcu(tmp_orig_entry
, head
, list
) {
1230 if (tmp_orig_entry
->orig_node
!= orig_node
)
1232 if (!atomic_inc_not_zero(&tmp_orig_entry
->refcount
))
1235 orig_entry
= tmp_orig_entry
;
1243 /* find out if an orig_node is already in the list of a tt_global_entry.
1244 * returns true if found, false otherwise
1247 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
1248 const struct batadv_orig_node
*orig_node
)
1250 struct batadv_tt_orig_list_entry
*orig_entry
;
1253 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
1256 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1263 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
1264 struct batadv_orig_node
*orig_node
, int ttvn
)
1266 struct batadv_tt_orig_list_entry
*orig_entry
;
1268 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
1270 /* refresh the ttvn: the current value could be a bogus one that
1271 * was added during a "temporary client detection"
1273 orig_entry
->ttvn
= ttvn
;
1277 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
1281 INIT_HLIST_NODE(&orig_entry
->list
);
1282 atomic_inc(&orig_node
->refcount
);
1283 batadv_tt_global_size_inc(orig_node
, tt_global
->common
.vid
);
1284 orig_entry
->orig_node
= orig_node
;
1285 orig_entry
->ttvn
= ttvn
;
1286 atomic_set(&orig_entry
->refcount
, 2);
1288 spin_lock_bh(&tt_global
->list_lock
);
1289 hlist_add_head_rcu(&orig_entry
->list
,
1290 &tt_global
->orig_list
);
1291 spin_unlock_bh(&tt_global
->list_lock
);
1292 atomic_inc(&tt_global
->orig_list_count
);
1296 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1300 * batadv_tt_global_add - add a new TT global entry or update an existing one
1301 * @bat_priv: the bat priv with all the soft interface information
1302 * @orig_node: the originator announcing the client
1303 * @tt_addr: the mac address of the non-mesh client
1304 * @vid: VLAN identifier
1305 * @flags: TT flags that have to be set for this non-mesh client
1306 * @ttvn: the tt version number ever announcing this non-mesh client
1308 * Add a new TT global entry for the given originator. If the entry already
1309 * exists add a new reference to the given originator (a global entry can have
1310 * references to multiple originators) and adjust the flags attribute to reflect
1311 * the function argument.
1312 * If a TT local entry exists for this non-mesh client remove it.
1314 * The caller must hold orig_node refcount.
1316 * Return true if the new entry has been added, false otherwise
1318 static bool batadv_tt_global_add(struct batadv_priv
*bat_priv
,
1319 struct batadv_orig_node
*orig_node
,
1320 const unsigned char *tt_addr
,
1321 unsigned short vid
, uint16_t flags
,
1324 struct batadv_tt_global_entry
*tt_global_entry
;
1325 struct batadv_tt_local_entry
*tt_local_entry
;
1328 struct batadv_tt_common_entry
*common
;
1329 uint16_t local_flags
;
1331 /* ignore global entries from backbone nodes */
1332 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
, vid
))
1335 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
, vid
);
1336 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, tt_addr
, vid
);
1338 /* if the node already has a local client for this entry, it has to wait
1339 * for a roaming advertisement instead of manually messing up the global
1342 if ((flags
& BATADV_TT_CLIENT_TEMP
) && tt_local_entry
&&
1343 !(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
))
1346 if (!tt_global_entry
) {
1347 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
1348 if (!tt_global_entry
)
1351 common
= &tt_global_entry
->common
;
1352 ether_addr_copy(common
->addr
, tt_addr
);
1355 common
->flags
= flags
;
1356 tt_global_entry
->roam_at
= 0;
1357 /* node must store current time in case of roaming. This is
1358 * needed to purge this entry out on timeout (if nobody claims
1361 if (flags
& BATADV_TT_CLIENT_ROAM
)
1362 tt_global_entry
->roam_at
= jiffies
;
1363 atomic_set(&common
->refcount
, 2);
1364 common
->added_at
= jiffies
;
1366 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
1367 atomic_set(&tt_global_entry
->orig_list_count
, 0);
1368 spin_lock_init(&tt_global_entry
->list_lock
);
1370 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
1372 batadv_choose_tt
, common
,
1373 &common
->hash_entry
);
1375 if (unlikely(hash_added
!= 0)) {
1376 /* remove the reference for the hash */
1377 batadv_tt_global_entry_free_ref(tt_global_entry
);
1381 common
= &tt_global_entry
->common
;
1382 /* If there is already a global entry, we can use this one for
1384 * But if we are trying to add a temporary client then here are
1385 * two options at this point:
1386 * 1) the global client is not a temporary client: the global
1387 * client has to be left as it is, temporary information
1388 * should never override any already known client state
1389 * 2) the global client is a temporary client: purge the
1390 * originator list and add the new one orig_entry
1392 if (flags
& BATADV_TT_CLIENT_TEMP
) {
1393 if (!(common
->flags
& BATADV_TT_CLIENT_TEMP
))
1395 if (batadv_tt_global_entry_has_orig(tt_global_entry
,
1398 batadv_tt_global_del_orig_list(tt_global_entry
);
1399 goto add_orig_entry
;
1402 /* if the client was temporary added before receiving the first
1403 * OGM announcing it, we have to clear the TEMP flag
1405 common
->flags
&= ~BATADV_TT_CLIENT_TEMP
;
1407 /* the change can carry possible "attribute" flags like the
1408 * TT_CLIENT_WIFI, therefore they have to be copied in the
1411 tt_global_entry
->common
.flags
|= flags
;
1413 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1414 * one originator left in the list and we previously received a
1415 * delete + roaming change for this originator.
1417 * We should first delete the old originator before adding the
1420 if (common
->flags
& BATADV_TT_CLIENT_ROAM
) {
1421 batadv_tt_global_del_orig_list(tt_global_entry
);
1422 common
->flags
&= ~BATADV_TT_CLIENT_ROAM
;
1423 tt_global_entry
->roam_at
= 0;
1427 /* add the new orig_entry (if needed) or update it */
1428 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
1430 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1431 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1432 common
->addr
, BATADV_PRINT_VID(common
->vid
),
1437 /* Do not remove multicast addresses from the local hash on
1440 if (is_multicast_ether_addr(tt_addr
))
1443 /* remove address from local hash if present */
1444 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
, vid
,
1445 "global tt received",
1446 flags
& BATADV_TT_CLIENT_ROAM
);
1447 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
1449 if (!(flags
& BATADV_TT_CLIENT_ROAM
))
1450 /* this is a normal global add. Therefore the client is not in a
1451 * roaming state anymore.
1453 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
1456 if (tt_global_entry
)
1457 batadv_tt_global_entry_free_ref(tt_global_entry
);
1459 batadv_tt_local_entry_free_ref(tt_local_entry
);
1464 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1465 * @bat_priv: the bat priv with all the soft interface information
1466 * @tt_global_entry: global translation table entry to be analyzed
1468 * This functon assumes the caller holds rcu_read_lock().
1469 * Returns best originator list entry or NULL on errors.
1471 static struct batadv_tt_orig_list_entry
*
1472 batadv_transtable_best_orig(struct batadv_priv
*bat_priv
,
1473 struct batadv_tt_global_entry
*tt_global_entry
)
1475 struct batadv_neigh_node
*router
, *best_router
= NULL
;
1476 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1477 struct hlist_head
*head
;
1478 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
= NULL
;
1480 head
= &tt_global_entry
->orig_list
;
1481 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1482 router
= batadv_orig_router_get(orig_entry
->orig_node
,
1488 bao
->bat_neigh_cmp(router
, BATADV_IF_DEFAULT
,
1489 best_router
, BATADV_IF_DEFAULT
) <= 0) {
1490 batadv_neigh_node_free_ref(router
);
1494 /* release the refcount for the "old" best */
1496 batadv_neigh_node_free_ref(best_router
);
1498 best_entry
= orig_entry
;
1499 best_router
= router
;
1503 batadv_neigh_node_free_ref(best_router
);
1509 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1510 * for this global entry
1511 * @bat_priv: the bat priv with all the soft interface information
1512 * @tt_global_entry: global translation table entry to be printed
1513 * @seq: debugfs table seq_file struct
1515 * This functon assumes the caller holds rcu_read_lock().
1518 batadv_tt_global_print_entry(struct batadv_priv
*bat_priv
,
1519 struct batadv_tt_global_entry
*tt_global_entry
,
1520 struct seq_file
*seq
)
1522 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
;
1523 struct batadv_tt_common_entry
*tt_common_entry
;
1524 struct batadv_orig_node_vlan
*vlan
;
1525 struct hlist_head
*head
;
1529 tt_common_entry
= &tt_global_entry
->common
;
1530 flags
= tt_common_entry
->flags
;
1532 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
1534 vlan
= batadv_orig_node_vlan_get(best_entry
->orig_node
,
1535 tt_common_entry
->vid
);
1538 " * Cannot retrieve VLAN %d for originator %pM\n",
1539 BATADV_PRINT_VID(tt_common_entry
->vid
),
1540 best_entry
->orig_node
->orig
);
1544 last_ttvn
= atomic_read(&best_entry
->orig_node
->last_ttvn
);
1546 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1547 '*', tt_global_entry
->common
.addr
,
1548 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1549 best_entry
->ttvn
, best_entry
->orig_node
->orig
,
1550 last_ttvn
, vlan
->tt
.crc
,
1551 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1552 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1553 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1554 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1556 batadv_orig_node_vlan_free_ref(vlan
);
1560 head
= &tt_global_entry
->orig_list
;
1562 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1563 if (best_entry
== orig_entry
)
1566 vlan
= batadv_orig_node_vlan_get(orig_entry
->orig_node
,
1567 tt_common_entry
->vid
);
1570 " + Cannot retrieve VLAN %d for originator %pM\n",
1571 BATADV_PRINT_VID(tt_common_entry
->vid
),
1572 orig_entry
->orig_node
->orig
);
1576 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
1578 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1579 '+', tt_global_entry
->common
.addr
,
1580 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1581 orig_entry
->ttvn
, orig_entry
->orig_node
->orig
,
1582 last_ttvn
, vlan
->tt
.crc
,
1583 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1584 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1585 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1586 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1588 batadv_orig_node_vlan_free_ref(vlan
);
1592 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
1594 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1595 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1596 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1597 struct batadv_tt_common_entry
*tt_common_entry
;
1598 struct batadv_tt_global_entry
*tt_global
;
1599 struct batadv_hard_iface
*primary_if
;
1600 struct hlist_head
*head
;
1603 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1608 "Globally announced TT entries received via the mesh %s\n",
1610 seq_printf(seq
, " %-13s %s %s %-15s %s (%-10s) %s\n",
1611 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1614 for (i
= 0; i
< hash
->size
; i
++) {
1615 head
= &hash
->table
[i
];
1618 hlist_for_each_entry_rcu(tt_common_entry
,
1620 tt_global
= container_of(tt_common_entry
,
1621 struct batadv_tt_global_entry
,
1623 batadv_tt_global_print_entry(bat_priv
, tt_global
, seq
);
1629 batadv_hardif_free_ref(primary_if
);
1634 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1635 * @tt_global_entry: the global entry to remove the orig_entry from
1636 * @orig_entry: the orig entry to remove and free
1638 * Remove an orig_entry from its list in the given tt_global_entry and
1639 * free this orig_entry afterwards.
1642 batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry
*tt_global_entry
,
1643 struct batadv_tt_orig_list_entry
*orig_entry
)
1645 batadv_tt_global_size_dec(orig_entry
->orig_node
,
1646 tt_global_entry
->common
.vid
);
1647 atomic_dec(&tt_global_entry
->orig_list_count
);
1648 hlist_del_rcu(&orig_entry
->list
);
1649 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1652 /* deletes the orig list of a tt_global_entry */
1654 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
1656 struct hlist_head
*head
;
1657 struct hlist_node
*safe
;
1658 struct batadv_tt_orig_list_entry
*orig_entry
;
1660 spin_lock_bh(&tt_global_entry
->list_lock
);
1661 head
= &tt_global_entry
->orig_list
;
1662 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
)
1663 batadv_tt_global_del_orig_entry(tt_global_entry
, orig_entry
);
1664 spin_unlock_bh(&tt_global_entry
->list_lock
);
1668 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1669 * @bat_priv: the bat priv with all the soft interface information
1670 * @tt_global_entry: the global entry to remove the orig_node from
1671 * @orig_node: the originator announcing the client
1672 * @message: message to append to the log on deletion
1674 * Remove the given orig_node and its according orig_entry from the given
1678 batadv_tt_global_del_orig_node(struct batadv_priv
*bat_priv
,
1679 struct batadv_tt_global_entry
*tt_global_entry
,
1680 struct batadv_orig_node
*orig_node
,
1681 const char *message
)
1683 struct hlist_head
*head
;
1684 struct hlist_node
*safe
;
1685 struct batadv_tt_orig_list_entry
*orig_entry
;
1688 spin_lock_bh(&tt_global_entry
->list_lock
);
1689 head
= &tt_global_entry
->orig_list
;
1690 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
) {
1691 if (orig_entry
->orig_node
== orig_node
) {
1692 vid
= tt_global_entry
->common
.vid
;
1693 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1694 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1696 tt_global_entry
->common
.addr
,
1697 BATADV_PRINT_VID(vid
), message
);
1698 batadv_tt_global_del_orig_entry(tt_global_entry
,
1702 spin_unlock_bh(&tt_global_entry
->list_lock
);
1705 /* If the client is to be deleted, we check if it is the last origantor entry
1706 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1707 * timer, otherwise we simply remove the originator scheduled for deletion.
1710 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
1711 struct batadv_tt_global_entry
*tt_global_entry
,
1712 struct batadv_orig_node
*orig_node
,
1713 const char *message
)
1715 bool last_entry
= true;
1716 struct hlist_head
*head
;
1717 struct batadv_tt_orig_list_entry
*orig_entry
;
1719 /* no local entry exists, case 1:
1720 * Check if this is the last one or if other entries exist.
1724 head
= &tt_global_entry
->orig_list
;
1725 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1726 if (orig_entry
->orig_node
!= orig_node
) {
1734 /* its the last one, mark for roaming. */
1735 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1736 tt_global_entry
->roam_at
= jiffies
;
1738 /* there is another entry, we can simply delete this
1739 * one and can still use the other one.
1741 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1742 orig_node
, message
);
1746 * batadv_tt_global_del - remove a client from the global table
1747 * @bat_priv: the bat priv with all the soft interface information
1748 * @orig_node: an originator serving this client
1749 * @addr: the mac address of the client
1750 * @vid: VLAN identifier
1751 * @message: a message explaining the reason for deleting the client to print
1752 * for debugging purpose
1753 * @roaming: true if the deletion has been triggered by a roaming event
1755 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
1756 struct batadv_orig_node
*orig_node
,
1757 const unsigned char *addr
, unsigned short vid
,
1758 const char *message
, bool roaming
)
1760 struct batadv_tt_global_entry
*tt_global_entry
;
1761 struct batadv_tt_local_entry
*local_entry
= NULL
;
1763 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
1764 if (!tt_global_entry
)
1768 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1769 orig_node
, message
);
1771 if (hlist_empty(&tt_global_entry
->orig_list
))
1772 batadv_tt_global_free(bat_priv
, tt_global_entry
,
1778 /* if we are deleting a global entry due to a roam
1779 * event, there are two possibilities:
1780 * 1) the client roamed from node A to node B => if there
1781 * is only one originator left for this client, we mark
1782 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1783 * wait for node B to claim it. In case of timeout
1784 * the entry is purged.
1786 * If there are other originators left, we directly delete
1788 * 2) the client roamed to us => we can directly delete
1789 * the global entry, since it is useless now.
1791 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1792 tt_global_entry
->common
.addr
,
1795 /* local entry exists, case 2: client roamed to us. */
1796 batadv_tt_global_del_orig_list(tt_global_entry
);
1797 batadv_tt_global_free(bat_priv
, tt_global_entry
, message
);
1799 /* no local entry exists, case 1: check for roaming */
1800 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1801 orig_node
, message
);
1804 if (tt_global_entry
)
1805 batadv_tt_global_entry_free_ref(tt_global_entry
);
1807 batadv_tt_local_entry_free_ref(local_entry
);
1811 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1812 * given originator matching the provided vid
1813 * @bat_priv: the bat priv with all the soft interface information
1814 * @orig_node: the originator owning the entries to remove
1815 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1817 * @message: debug message to print as "reason"
1819 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1820 struct batadv_orig_node
*orig_node
,
1822 const char *message
)
1824 struct batadv_tt_global_entry
*tt_global
;
1825 struct batadv_tt_common_entry
*tt_common_entry
;
1827 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1828 struct hlist_node
*safe
;
1829 struct hlist_head
*head
;
1830 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1836 for (i
= 0; i
< hash
->size
; i
++) {
1837 head
= &hash
->table
[i
];
1838 list_lock
= &hash
->list_locks
[i
];
1840 spin_lock_bh(list_lock
);
1841 hlist_for_each_entry_safe(tt_common_entry
, safe
,
1843 /* remove only matching entries */
1844 if (match_vid
>= 0 && tt_common_entry
->vid
!= match_vid
)
1847 tt_global
= container_of(tt_common_entry
,
1848 struct batadv_tt_global_entry
,
1851 batadv_tt_global_del_orig_node(bat_priv
, tt_global
,
1852 orig_node
, message
);
1854 if (hlist_empty(&tt_global
->orig_list
)) {
1855 vid
= tt_global
->common
.vid
;
1856 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1857 "Deleting global tt entry %pM (vid: %d): %s\n",
1858 tt_global
->common
.addr
,
1859 BATADV_PRINT_VID(vid
), message
);
1860 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1861 batadv_tt_global_entry_free_ref(tt_global
);
1864 spin_unlock_bh(list_lock
);
1866 clear_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
1869 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1873 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1874 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1876 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1877 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1879 *msg
= "Roaming timeout\n";
1882 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1883 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1885 *msg
= "Temporary client timeout\n";
1891 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1893 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1894 struct hlist_head
*head
;
1895 struct hlist_node
*node_tmp
;
1896 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1899 struct batadv_tt_common_entry
*tt_common
;
1900 struct batadv_tt_global_entry
*tt_global
;
1902 for (i
= 0; i
< hash
->size
; i
++) {
1903 head
= &hash
->table
[i
];
1904 list_lock
= &hash
->list_locks
[i
];
1906 spin_lock_bh(list_lock
);
1907 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
1909 tt_global
= container_of(tt_common
,
1910 struct batadv_tt_global_entry
,
1913 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1916 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1917 "Deleting global tt entry %pM (vid: %d): %s\n",
1918 tt_global
->common
.addr
,
1919 BATADV_PRINT_VID(tt_global
->common
.vid
),
1922 hlist_del_rcu(&tt_common
->hash_entry
);
1924 batadv_tt_global_entry_free_ref(tt_global
);
1926 spin_unlock_bh(list_lock
);
1930 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
1932 struct batadv_hashtable
*hash
;
1933 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1934 struct batadv_tt_common_entry
*tt_common_entry
;
1935 struct batadv_tt_global_entry
*tt_global
;
1936 struct hlist_node
*node_tmp
;
1937 struct hlist_head
*head
;
1940 if (!bat_priv
->tt
.global_hash
)
1943 hash
= bat_priv
->tt
.global_hash
;
1945 for (i
= 0; i
< hash
->size
; i
++) {
1946 head
= &hash
->table
[i
];
1947 list_lock
= &hash
->list_locks
[i
];
1949 spin_lock_bh(list_lock
);
1950 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1952 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1953 tt_global
= container_of(tt_common_entry
,
1954 struct batadv_tt_global_entry
,
1956 batadv_tt_global_entry_free_ref(tt_global
);
1958 spin_unlock_bh(list_lock
);
1961 batadv_hash_destroy(hash
);
1963 bat_priv
->tt
.global_hash
= NULL
;
1967 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
1968 struct batadv_tt_global_entry
*tt_global_entry
)
1972 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
1973 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
1976 /* check if the two clients are marked as isolated */
1977 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
&&
1978 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
)
1985 * batadv_transtable_search - get the mesh destination for a given client
1986 * @bat_priv: the bat priv with all the soft interface information
1987 * @src: mac address of the source client
1988 * @addr: mac address of the destination client
1989 * @vid: VLAN identifier
1991 * Returns a pointer to the originator that was selected as destination in the
1992 * mesh for contacting the client 'addr', NULL otherwise.
1993 * In case of multiple originators serving the same client, the function returns
1994 * the best one (best in terms of metric towards the destination node).
1996 * If the two clients are AP isolated the function returns NULL.
1998 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
2000 const uint8_t *addr
,
2003 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2004 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2005 struct batadv_orig_node
*orig_node
= NULL
;
2006 struct batadv_tt_orig_list_entry
*best_entry
;
2008 if (src
&& batadv_vlan_ap_isola_get(bat_priv
, vid
)) {
2009 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
, vid
);
2010 if (!tt_local_entry
||
2011 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
))
2015 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
2016 if (!tt_global_entry
)
2019 /* check whether the clients should not communicate due to AP
2022 if (tt_local_entry
&&
2023 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2027 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
2028 /* found anything? */
2030 orig_node
= best_entry
->orig_node
;
2031 if (orig_node
&& !atomic_inc_not_zero(&orig_node
->refcount
))
2036 if (tt_global_entry
)
2037 batadv_tt_global_entry_free_ref(tt_global_entry
);
2039 batadv_tt_local_entry_free_ref(tt_local_entry
);
2045 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2046 * to the given orig_node
2047 * @bat_priv: the bat priv with all the soft interface information
2048 * @orig_node: originator for which the CRC should be computed
2049 * @vid: VLAN identifier for which the CRC32 has to be computed
2051 * This function computes the checksum for the global table corresponding to a
2052 * specific originator. In particular, the checksum is computed as follows: For
2053 * each client connected to the originator the CRC32C of the MAC address and the
2054 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2057 * The idea behind is that CRC32C should be used as much as possible in order to
2058 * produce a unique hash of the table, but since the order which is used to feed
2059 * the CRC32C function affects the result and since every node in the network
2060 * probably sorts the clients differently, the hash function cannot be directly
2061 * computed over the entire table. Hence the CRC32C is used only on
2062 * the single client entry, while all the results are then xor'ed together
2063 * because the XOR operation can combine them all while trying to reduce the
2064 * noise as much as possible.
2066 * Returns the checksum of the global table of a given originator.
2068 static uint32_t batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
2069 struct batadv_orig_node
*orig_node
,
2072 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2073 struct batadv_tt_common_entry
*tt_common
;
2074 struct batadv_tt_global_entry
*tt_global
;
2075 struct hlist_head
*head
;
2076 uint32_t i
, crc_tmp
, crc
= 0;
2080 for (i
= 0; i
< hash
->size
; i
++) {
2081 head
= &hash
->table
[i
];
2084 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2085 tt_global
= container_of(tt_common
,
2086 struct batadv_tt_global_entry
,
2088 /* compute the CRC only for entries belonging to the
2089 * VLAN identified by the vid passed as parameter
2091 if (tt_common
->vid
!= vid
)
2094 /* Roaming clients are in the global table for
2095 * consistency only. They don't have to be
2096 * taken into account while computing the
2099 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
2101 /* Temporary clients have not been announced yet, so
2102 * they have to be skipped while computing the global
2105 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
2108 /* find out if this global entry is announced by this
2111 if (!batadv_tt_global_entry_has_orig(tt_global
,
2115 /* use network order to read the VID: this ensures that
2116 * every node reads the bytes in the same order.
2118 tmp_vid
= htons(tt_common
->vid
);
2119 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2121 /* compute the CRC on flags that have to be kept in sync
2124 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2125 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2127 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2136 * batadv_tt_local_crc - calculates the checksum of the local table
2137 * @bat_priv: the bat priv with all the soft interface information
2138 * @vid: VLAN identifier for which the CRC32 has to be computed
2140 * For details about the computation, please refer to the documentation for
2141 * batadv_tt_global_crc().
2143 * Returns the checksum of the local table
2145 static uint32_t batadv_tt_local_crc(struct batadv_priv
*bat_priv
,
2148 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2149 struct batadv_tt_common_entry
*tt_common
;
2150 struct hlist_head
*head
;
2151 uint32_t i
, crc_tmp
, crc
= 0;
2155 for (i
= 0; i
< hash
->size
; i
++) {
2156 head
= &hash
->table
[i
];
2159 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2160 /* compute the CRC only for entries belonging to the
2161 * VLAN identified by vid
2163 if (tt_common
->vid
!= vid
)
2166 /* not yet committed clients have not to be taken into
2167 * account while computing the CRC
2169 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
2172 /* use network order to read the VID: this ensures that
2173 * every node reads the bytes in the same order.
2175 tmp_vid
= htons(tt_common
->vid
);
2176 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2178 /* compute the CRC on flags that have to be kept in sync
2181 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2182 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2184 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2192 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
2194 struct batadv_tt_req_node
*node
, *safe
;
2196 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2198 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2199 list_del_init(&node
->list
);
2203 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2206 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
2207 struct batadv_orig_node
*orig_node
,
2208 const void *tt_buff
,
2209 uint16_t tt_buff_len
)
2211 /* Replace the old buffer only if I received something in the
2212 * last OGM (the OGM could carry no changes)
2214 spin_lock_bh(&orig_node
->tt_buff_lock
);
2215 if (tt_buff_len
> 0) {
2216 kfree(orig_node
->tt_buff
);
2217 orig_node
->tt_buff_len
= 0;
2218 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
2219 if (orig_node
->tt_buff
) {
2220 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
2221 orig_node
->tt_buff_len
= tt_buff_len
;
2224 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2227 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
2229 struct batadv_tt_req_node
*node
, *safe
;
2231 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2232 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2233 if (batadv_has_timed_out(node
->issued_at
,
2234 BATADV_TT_REQUEST_TIMEOUT
)) {
2235 list_del_init(&node
->list
);
2239 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2242 /* returns the pointer to the new tt_req_node struct if no request
2243 * has already been issued for this orig_node, NULL otherwise
2245 static struct batadv_tt_req_node
*
2246 batadv_new_tt_req_node(struct batadv_priv
*bat_priv
,
2247 struct batadv_orig_node
*orig_node
)
2249 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
2251 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2252 list_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
2253 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
2254 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
2255 BATADV_TT_REQUEST_TIMEOUT
))
2259 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
2263 ether_addr_copy(tt_req_node
->addr
, orig_node
->orig
);
2264 tt_req_node
->issued_at
= jiffies
;
2266 list_add(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
2268 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2273 * batadv_tt_local_valid - verify that given tt entry is a valid one
2274 * @entry_ptr: to be checked local tt entry
2275 * @data_ptr: not used but definition required to satisfy the callback prototype
2277 * Returns 1 if the entry is a valid, 0 otherwise.
2279 static int batadv_tt_local_valid(const void *entry_ptr
, const void *data_ptr
)
2281 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2283 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
2288 static int batadv_tt_global_valid(const void *entry_ptr
,
2289 const void *data_ptr
)
2291 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2292 const struct batadv_tt_global_entry
*tt_global_entry
;
2293 const struct batadv_orig_node
*orig_node
= data_ptr
;
2295 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
2296 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
2299 tt_global_entry
= container_of(tt_common_entry
,
2300 struct batadv_tt_global_entry
,
2303 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
2307 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2309 * @bat_priv: the bat priv with all the soft interface information
2310 * @hash: hash table containing the tt entries
2311 * @tt_len: expected tvlv tt data buffer length in number of bytes
2312 * @tvlv_buff: pointer to the buffer to fill with the TT data
2313 * @valid_cb: function to filter tt change entries
2314 * @cb_data: data passed to the filter function as argument
2316 static void batadv_tt_tvlv_generate(struct batadv_priv
*bat_priv
,
2317 struct batadv_hashtable
*hash
,
2318 void *tvlv_buff
, uint16_t tt_len
,
2319 int (*valid_cb
)(const void *, const void *),
2322 struct batadv_tt_common_entry
*tt_common_entry
;
2323 struct batadv_tvlv_tt_change
*tt_change
;
2324 struct hlist_head
*head
;
2325 uint16_t tt_tot
, tt_num_entries
= 0;
2328 tt_tot
= batadv_tt_entries(tt_len
);
2329 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_buff
;
2332 for (i
= 0; i
< hash
->size
; i
++) {
2333 head
= &hash
->table
[i
];
2335 hlist_for_each_entry_rcu(tt_common_entry
,
2337 if (tt_tot
== tt_num_entries
)
2340 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
2343 ether_addr_copy(tt_change
->addr
, tt_common_entry
->addr
);
2344 tt_change
->flags
= tt_common_entry
->flags
;
2345 tt_change
->vid
= htons(tt_common_entry
->vid
);
2346 memset(tt_change
->reserved
, 0,
2347 sizeof(tt_change
->reserved
));
2357 * batadv_tt_global_check_crc - check if all the CRCs are correct
2358 * @orig_node: originator for which the CRCs have to be checked
2359 * @tt_vlan: pointer to the first tvlv VLAN entry
2360 * @num_vlan: number of tvlv VLAN entries
2361 * @create: if true, create VLAN objects if not found
2363 * Return true if all the received CRCs match the locally stored ones, false
2366 static bool batadv_tt_global_check_crc(struct batadv_orig_node
*orig_node
,
2367 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2370 struct batadv_tvlv_tt_vlan_data
*tt_vlan_tmp
;
2371 struct batadv_orig_node_vlan
*vlan
;
2375 /* check if each received CRC matches the locally stored one */
2376 for (i
= 0; i
< num_vlan
; i
++) {
2377 tt_vlan_tmp
= tt_vlan
+ i
;
2379 /* if orig_node is a backbone node for this VLAN, don't check
2380 * the CRC as we ignore all the global entries over it
2382 if (batadv_bla_is_backbone_gw_orig(orig_node
->bat_priv
,
2384 ntohs(tt_vlan_tmp
->vid
)))
2387 vlan
= batadv_orig_node_vlan_get(orig_node
,
2388 ntohs(tt_vlan_tmp
->vid
));
2393 batadv_orig_node_vlan_free_ref(vlan
);
2395 if (crc
!= ntohl(tt_vlan_tmp
->crc
))
2403 * batadv_tt_local_update_crc - update all the local CRCs
2404 * @bat_priv: the bat priv with all the soft interface information
2406 static void batadv_tt_local_update_crc(struct batadv_priv
*bat_priv
)
2408 struct batadv_softif_vlan
*vlan
;
2410 /* recompute the global CRC for each VLAN */
2412 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
2413 vlan
->tt
.crc
= batadv_tt_local_crc(bat_priv
, vlan
->vid
);
2419 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2420 * @bat_priv: the bat priv with all the soft interface information
2421 * @orig_node: the orig_node for which the CRCs have to be updated
2423 static void batadv_tt_global_update_crc(struct batadv_priv
*bat_priv
,
2424 struct batadv_orig_node
*orig_node
)
2426 struct batadv_orig_node_vlan
*vlan
;
2429 /* recompute the global CRC for each VLAN */
2431 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
2432 /* if orig_node is a backbone node for this VLAN, don't compute
2433 * the CRC as we ignore all the global entries over it
2435 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
,
2439 crc
= batadv_tt_global_crc(bat_priv
, orig_node
, vlan
->vid
);
2446 * batadv_send_tt_request - send a TT Request message to a given node
2447 * @bat_priv: the bat priv with all the soft interface information
2448 * @dst_orig_node: the destination of the message
2449 * @ttvn: the version number that the source of the message is looking for
2450 * @tt_vlan: pointer to the first tvlv VLAN object to request
2451 * @num_vlan: number of tvlv VLAN entries
2452 * @full_table: ask for the entire translation table if true, while only for the
2453 * last TT diff otherwise
2455 static int batadv_send_tt_request(struct batadv_priv
*bat_priv
,
2456 struct batadv_orig_node
*dst_orig_node
,
2458 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2459 uint16_t num_vlan
, bool full_table
)
2461 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2462 struct batadv_tt_req_node
*tt_req_node
= NULL
;
2463 struct batadv_tvlv_tt_vlan_data
*tt_vlan_req
;
2464 struct batadv_hard_iface
*primary_if
;
2468 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2472 /* The new tt_req will be issued only if I'm not waiting for a
2473 * reply from the same orig_node yet
2475 tt_req_node
= batadv_new_tt_req_node(bat_priv
, dst_orig_node
);
2479 size
= sizeof(*tvlv_tt_data
) + sizeof(*tt_vlan_req
) * num_vlan
;
2480 tvlv_tt_data
= kzalloc(size
, GFP_ATOMIC
);
2484 tvlv_tt_data
->flags
= BATADV_TT_REQUEST
;
2485 tvlv_tt_data
->ttvn
= ttvn
;
2486 tvlv_tt_data
->num_vlan
= htons(num_vlan
);
2488 /* send all the CRCs within the request. This is needed by intermediate
2489 * nodes to ensure they have the correct table before replying
2491 tt_vlan_req
= (struct batadv_tvlv_tt_vlan_data
*)(tvlv_tt_data
+ 1);
2492 for (i
= 0; i
< num_vlan
; i
++) {
2493 tt_vlan_req
->vid
= tt_vlan
->vid
;
2494 tt_vlan_req
->crc
= tt_vlan
->crc
;
2501 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2503 batadv_dbg(BATADV_DBG_TT
, bat_priv
, "Sending TT_REQUEST to %pM [%c]\n",
2504 dst_orig_node
->orig
, full_table
? 'F' : '.');
2506 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
2507 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2508 dst_orig_node
->orig
, BATADV_TVLV_TT
, 1,
2509 tvlv_tt_data
, size
);
2514 batadv_hardif_free_ref(primary_if
);
2515 if (ret
&& tt_req_node
) {
2516 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2517 /* list_del_init() verifies tt_req_node still is in the list */
2518 list_del_init(&tt_req_node
->list
);
2519 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2522 kfree(tvlv_tt_data
);
2527 * batadv_send_other_tt_response - send reply to tt request concerning another
2528 * node's translation table
2529 * @bat_priv: the bat priv with all the soft interface information
2530 * @tt_data: tt data containing the tt request information
2531 * @req_src: mac address of tt request sender
2532 * @req_dst: mac address of tt request recipient
2534 * Returns true if tt request reply was sent, false otherwise.
2536 static bool batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
2537 struct batadv_tvlv_tt_data
*tt_data
,
2538 uint8_t *req_src
, uint8_t *req_dst
)
2540 struct batadv_orig_node
*req_dst_orig_node
;
2541 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
2542 struct batadv_tvlv_tt_change
*tt_change
;
2543 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2544 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
2545 bool ret
= false, full_table
;
2546 uint8_t orig_ttvn
, req_ttvn
;
2550 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2551 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2552 req_src
, tt_data
->ttvn
, req_dst
,
2553 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2555 /* Let's get the orig node of the REAL destination */
2556 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_dst
);
2557 if (!req_dst_orig_node
)
2560 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2561 if (!res_dst_orig_node
)
2564 orig_ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
2565 req_ttvn
= tt_data
->ttvn
;
2567 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
2568 /* this node doesn't have the requested data */
2569 if (orig_ttvn
!= req_ttvn
||
2570 !batadv_tt_global_check_crc(req_dst_orig_node
, tt_vlan
,
2571 ntohs(tt_data
->num_vlan
)))
2574 /* If the full table has been explicitly requested */
2575 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
||
2576 !req_dst_orig_node
->tt_buff
)
2581 /* TT fragmentation hasn't been implemented yet, so send as many
2582 * TT entries fit a single packet as possible only
2585 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
2586 tt_len
= req_dst_orig_node
->tt_buff_len
;
2588 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2595 /* Copy the last orig_node's OGM buffer */
2596 memcpy(tt_change
, req_dst_orig_node
->tt_buff
,
2597 req_dst_orig_node
->tt_buff_len
);
2598 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2600 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2601 * in the initial part
2604 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2611 /* fill the rest of the tvlv with the real TT entries */
2612 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.global_hash
,
2614 batadv_tt_global_valid
,
2618 /* Don't send the response, if larger than fragmented packet. */
2619 tt_len
= sizeof(struct batadv_unicast_tvlv_packet
) + tvlv_len
;
2620 if (tt_len
> atomic_read(&bat_priv
->packet_size_max
)) {
2621 net_ratelimited_function(batadv_info
, bat_priv
->soft_iface
,
2622 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2623 res_dst_orig_node
->orig
);
2627 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2628 tvlv_tt_data
->ttvn
= req_ttvn
;
2631 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2633 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2634 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2635 res_dst_orig_node
->orig
, req_dst_orig_node
->orig
,
2636 full_table
? 'F' : '.', req_ttvn
);
2638 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2640 batadv_tvlv_unicast_send(bat_priv
, req_dst_orig_node
->orig
,
2641 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2648 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2651 if (res_dst_orig_node
)
2652 batadv_orig_node_free_ref(res_dst_orig_node
);
2653 if (req_dst_orig_node
)
2654 batadv_orig_node_free_ref(req_dst_orig_node
);
2655 kfree(tvlv_tt_data
);
2660 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2662 * @bat_priv: the bat priv with all the soft interface information
2663 * @tt_data: tt data containing the tt request information
2664 * @req_src: mac address of tt request sender
2666 * Returns true if tt request reply was sent, false otherwise.
2668 static bool batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
2669 struct batadv_tvlv_tt_data
*tt_data
,
2672 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2673 struct batadv_hard_iface
*primary_if
= NULL
;
2674 struct batadv_tvlv_tt_change
*tt_change
;
2675 struct batadv_orig_node
*orig_node
;
2676 uint8_t my_ttvn
, req_ttvn
;
2681 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2682 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2683 req_src
, tt_data
->ttvn
,
2684 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2686 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
2688 my_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
2689 req_ttvn
= tt_data
->ttvn
;
2691 orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2695 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2699 /* If the full table has been explicitly requested or the gap
2700 * is too big send the whole local translation table
2702 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
2703 !bat_priv
->tt
.last_changeset
)
2708 /* TT fragmentation hasn't been implemented yet, so send as many
2709 * TT entries fit a single packet as possible only
2712 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
2714 tt_len
= bat_priv
->tt
.last_changeset_len
;
2715 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2722 /* Copy the last orig_node's OGM buffer */
2723 memcpy(tt_change
, bat_priv
->tt
.last_changeset
,
2724 bat_priv
->tt
.last_changeset_len
);
2725 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2727 req_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
2729 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2730 * in the initial part
2733 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2740 /* fill the rest of the tvlv with the real TT entries */
2741 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.local_hash
,
2743 batadv_tt_local_valid
, NULL
);
2746 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2747 tvlv_tt_data
->ttvn
= req_ttvn
;
2750 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2752 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2753 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2754 orig_node
->orig
, full_table
? 'F' : '.', req_ttvn
);
2756 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2758 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2759 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2765 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2767 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
2769 batadv_orig_node_free_ref(orig_node
);
2771 batadv_hardif_free_ref(primary_if
);
2772 kfree(tvlv_tt_data
);
2773 /* The packet was for this host, so it doesn't need to be re-routed */
2778 * batadv_send_tt_response - send reply to tt request
2779 * @bat_priv: the bat priv with all the soft interface information
2780 * @tt_data: tt data containing the tt request information
2781 * @req_src: mac address of tt request sender
2782 * @req_dst: mac address of tt request recipient
2784 * Returns true if tt request reply was sent, false otherwise.
2786 static bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
2787 struct batadv_tvlv_tt_data
*tt_data
,
2788 uint8_t *req_src
, uint8_t *req_dst
)
2790 if (batadv_is_my_mac(bat_priv
, req_dst
))
2791 return batadv_send_my_tt_response(bat_priv
, tt_data
, req_src
);
2792 return batadv_send_other_tt_response(bat_priv
, tt_data
, req_src
,
2796 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2797 struct batadv_orig_node
*orig_node
,
2798 struct batadv_tvlv_tt_change
*tt_change
,
2799 uint16_t tt_num_changes
, uint8_t ttvn
)
2804 for (i
= 0; i
< tt_num_changes
; i
++) {
2805 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
2806 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
2807 batadv_tt_global_del(bat_priv
, orig_node
,
2808 (tt_change
+ i
)->addr
,
2809 ntohs((tt_change
+ i
)->vid
),
2810 "tt removed by changes",
2813 if (!batadv_tt_global_add(bat_priv
, orig_node
,
2814 (tt_change
+ i
)->addr
,
2815 ntohs((tt_change
+ i
)->vid
),
2816 (tt_change
+ i
)->flags
, ttvn
))
2817 /* In case of problem while storing a
2818 * global_entry, we stop the updating
2819 * procedure without committing the
2820 * ttvn change. This will avoid to send
2821 * corrupted data on tt_request
2826 set_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
2829 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
2830 struct batadv_tvlv_tt_change
*tt_change
,
2831 uint8_t ttvn
, uint8_t *resp_src
,
2832 uint16_t num_entries
)
2834 struct batadv_orig_node
*orig_node
;
2836 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2840 /* Purge the old table first.. */
2841 batadv_tt_global_del_orig(bat_priv
, orig_node
, -1,
2842 "Received full table");
2844 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
, num_entries
,
2847 spin_lock_bh(&orig_node
->tt_buff_lock
);
2848 kfree(orig_node
->tt_buff
);
2849 orig_node
->tt_buff_len
= 0;
2850 orig_node
->tt_buff
= NULL
;
2851 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2853 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2857 batadv_orig_node_free_ref(orig_node
);
2860 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2861 struct batadv_orig_node
*orig_node
,
2862 uint16_t tt_num_changes
, uint8_t ttvn
,
2863 struct batadv_tvlv_tt_change
*tt_change
)
2865 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
2866 tt_num_changes
, ttvn
);
2868 batadv_tt_save_orig_buffer(bat_priv
, orig_node
, tt_change
,
2869 batadv_tt_len(tt_num_changes
));
2870 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2874 * batadv_is_my_client - check if a client is served by the local node
2875 * @bat_priv: the bat priv with all the soft interface information
2876 * @addr: the mac address of the client to check
2877 * @vid: VLAN identifier
2879 * Returns true if the client is served by this node, false otherwise.
2881 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
2884 struct batadv_tt_local_entry
*tt_local_entry
;
2887 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
2888 if (!tt_local_entry
)
2890 /* Check if the client has been logically deleted (but is kept for
2891 * consistency purpose)
2893 if ((tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
) ||
2894 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
2899 batadv_tt_local_entry_free_ref(tt_local_entry
);
2904 * batadv_handle_tt_response - process incoming tt reply
2905 * @bat_priv: the bat priv with all the soft interface information
2906 * @tt_data: tt data containing the tt request information
2907 * @resp_src: mac address of tt reply sender
2908 * @num_entries: number of tt change entries appended to the tt data
2910 static void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
2911 struct batadv_tvlv_tt_data
*tt_data
,
2912 uint8_t *resp_src
, uint16_t num_entries
)
2914 struct batadv_tt_req_node
*node
, *safe
;
2915 struct batadv_orig_node
*orig_node
= NULL
;
2916 struct batadv_tvlv_tt_change
*tt_change
;
2917 uint8_t *tvlv_ptr
= (uint8_t *)tt_data
;
2918 uint16_t change_offset
;
2920 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2921 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2922 resp_src
, tt_data
->ttvn
, num_entries
,
2923 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2925 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2929 spin_lock_bh(&orig_node
->tt_lock
);
2931 change_offset
= sizeof(struct batadv_tvlv_tt_vlan_data
);
2932 change_offset
*= ntohs(tt_data
->num_vlan
);
2933 change_offset
+= sizeof(*tt_data
);
2934 tvlv_ptr
+= change_offset
;
2936 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_ptr
;
2937 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
) {
2938 batadv_tt_fill_gtable(bat_priv
, tt_change
, tt_data
->ttvn
,
2939 resp_src
, num_entries
);
2941 batadv_tt_update_changes(bat_priv
, orig_node
, num_entries
,
2942 tt_data
->ttvn
, tt_change
);
2945 /* Recalculate the CRC for this orig_node and store it */
2946 batadv_tt_global_update_crc(bat_priv
, orig_node
);
2948 spin_unlock_bh(&orig_node
->tt_lock
);
2950 /* Delete the tt_req_node from pending tt_requests list */
2951 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2952 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2953 if (!batadv_compare_eth(node
->addr
, resp_src
))
2955 list_del_init(&node
->list
);
2959 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2962 batadv_orig_node_free_ref(orig_node
);
2965 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
2967 struct batadv_tt_roam_node
*node
, *safe
;
2969 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2971 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2972 list_del(&node
->list
);
2976 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2979 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
2981 struct batadv_tt_roam_node
*node
, *safe
;
2983 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2984 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2985 if (!batadv_has_timed_out(node
->first_time
,
2986 BATADV_ROAMING_MAX_TIME
))
2989 list_del(&node
->list
);
2992 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2995 /* This function checks whether the client already reached the
2996 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2999 * returns true if the ROAMING_ADV can be sent, false otherwise
3001 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
,
3004 struct batadv_tt_roam_node
*tt_roam_node
;
3007 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3008 /* The new tt_req will be issued only if I'm not waiting for a
3009 * reply from the same orig_node yet
3011 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
3012 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
3015 if (batadv_has_timed_out(tt_roam_node
->first_time
,
3016 BATADV_ROAMING_MAX_TIME
))
3019 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
3020 /* Sorry, you roamed too many times! */
3027 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
3031 tt_roam_node
->first_time
= jiffies
;
3032 atomic_set(&tt_roam_node
->counter
,
3033 BATADV_ROAMING_MAX_COUNT
- 1);
3034 ether_addr_copy(tt_roam_node
->addr
, client
);
3036 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
3041 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3046 * batadv_send_roam_adv - send a roaming advertisement message
3047 * @bat_priv: the bat priv with all the soft interface information
3048 * @client: mac address of the roaming client
3049 * @vid: VLAN identifier
3050 * @orig_node: message destination
3052 * Send a ROAMING_ADV message to the node which was previously serving this
3053 * client. This is done to inform the node that from now on all traffic destined
3054 * for this particular roamed client has to be forwarded to the sender of the
3057 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
3059 struct batadv_orig_node
*orig_node
)
3061 struct batadv_hard_iface
*primary_if
;
3062 struct batadv_tvlv_roam_adv tvlv_roam
;
3064 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3068 /* before going on we have to check whether the client has
3069 * already roamed to us too many times
3071 if (!batadv_tt_check_roam_count(bat_priv
, client
))
3074 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3075 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3076 orig_node
->orig
, client
, BATADV_PRINT_VID(vid
));
3078 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
3080 memcpy(tvlv_roam
.client
, client
, sizeof(tvlv_roam
.client
));
3081 tvlv_roam
.vid
= htons(vid
);
3083 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3084 orig_node
->orig
, BATADV_TVLV_ROAM
, 1,
3085 &tvlv_roam
, sizeof(tvlv_roam
));
3089 batadv_hardif_free_ref(primary_if
);
3092 static void batadv_tt_purge(struct work_struct
*work
)
3094 struct delayed_work
*delayed_work
;
3095 struct batadv_priv_tt
*priv_tt
;
3096 struct batadv_priv
*bat_priv
;
3098 delayed_work
= container_of(work
, struct delayed_work
, work
);
3099 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
3100 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
3102 batadv_tt_local_purge(bat_priv
, BATADV_TT_LOCAL_TIMEOUT
);
3103 batadv_tt_global_purge(bat_priv
);
3104 batadv_tt_req_purge(bat_priv
);
3105 batadv_tt_roam_purge(bat_priv
);
3107 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3108 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3111 void batadv_tt_free(struct batadv_priv
*bat_priv
)
3113 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3114 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3116 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
3118 batadv_tt_local_table_free(bat_priv
);
3119 batadv_tt_global_table_free(bat_priv
);
3120 batadv_tt_req_list_free(bat_priv
);
3121 batadv_tt_changes_list_free(bat_priv
);
3122 batadv_tt_roam_list_free(bat_priv
);
3124 kfree(bat_priv
->tt
.last_changeset
);
3128 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3129 * table and possibly count them in the TT size
3130 * @bat_priv: the bat priv with all the soft interface information
3131 * @flags: the flag to switch
3132 * @enable: whether to set or unset the flag
3133 * @count: whether to increase the TT size by the number of changed entries
3135 static void batadv_tt_local_set_flags(struct batadv_priv
*bat_priv
,
3136 uint16_t flags
, bool enable
, bool count
)
3138 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3139 struct batadv_tt_common_entry
*tt_common_entry
;
3140 uint16_t changed_num
= 0;
3141 struct hlist_head
*head
;
3147 for (i
= 0; i
< hash
->size
; i
++) {
3148 head
= &hash
->table
[i
];
3151 hlist_for_each_entry_rcu(tt_common_entry
,
3154 if ((tt_common_entry
->flags
& flags
) == flags
)
3156 tt_common_entry
->flags
|= flags
;
3158 if (!(tt_common_entry
->flags
& flags
))
3160 tt_common_entry
->flags
&= ~flags
;
3167 batadv_tt_local_size_inc(bat_priv
,
3168 tt_common_entry
->vid
);
3174 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3175 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
3177 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3178 struct batadv_tt_common_entry
*tt_common
;
3179 struct batadv_tt_local_entry
*tt_local
;
3180 struct batadv_softif_vlan
*vlan
;
3181 struct hlist_node
*node_tmp
;
3182 struct hlist_head
*head
;
3183 spinlock_t
*list_lock
; /* protects write access to the hash lists */
3189 for (i
= 0; i
< hash
->size
; i
++) {
3190 head
= &hash
->table
[i
];
3191 list_lock
= &hash
->list_locks
[i
];
3193 spin_lock_bh(list_lock
);
3194 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
3196 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
3199 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3200 "Deleting local tt entry (%pM, vid: %d): pending\n",
3202 BATADV_PRINT_VID(tt_common
->vid
));
3204 batadv_tt_local_size_dec(bat_priv
, tt_common
->vid
);
3205 hlist_del_rcu(&tt_common
->hash_entry
);
3206 tt_local
= container_of(tt_common
,
3207 struct batadv_tt_local_entry
,
3210 /* decrease the reference held for this vlan */
3211 vlan
= batadv_softif_vlan_get(bat_priv
, tt_common
->vid
);
3212 batadv_softif_vlan_free_ref(vlan
);
3213 batadv_softif_vlan_free_ref(vlan
);
3215 batadv_tt_local_entry_free_ref(tt_local
);
3217 spin_unlock_bh(list_lock
);
3222 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3223 * which have been queued in the time since the last commit
3224 * @bat_priv: the bat priv with all the soft interface information
3226 * Caller must hold tt->commit_lock.
3228 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv
*bat_priv
)
3230 /* Update multicast addresses in local translation table */
3231 batadv_mcast_mla_update(bat_priv
);
3233 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1) {
3234 if (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))
3235 batadv_tt_tvlv_container_update(bat_priv
);
3239 batadv_tt_local_set_flags(bat_priv
, BATADV_TT_CLIENT_NEW
, false, true);
3241 batadv_tt_local_purge_pending_clients(bat_priv
);
3242 batadv_tt_local_update_crc(bat_priv
);
3244 /* Increment the TTVN only once per OGM interval */
3245 atomic_inc(&bat_priv
->tt
.vn
);
3246 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3247 "Local changes committed, updating to ttvn %u\n",
3248 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
3250 /* reset the sending counter */
3251 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
3252 batadv_tt_tvlv_container_update(bat_priv
);
3256 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3257 * have been queued in the time since the last commit
3258 * @bat_priv: the bat priv with all the soft interface information
3260 void batadv_tt_local_commit_changes(struct batadv_priv
*bat_priv
)
3262 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3263 batadv_tt_local_commit_changes_nolock(bat_priv
);
3264 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3267 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, uint8_t *src
,
3268 uint8_t *dst
, unsigned short vid
)
3270 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
3271 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
3272 struct batadv_softif_vlan
*vlan
;
3275 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
3276 if (!vlan
|| !atomic_read(&vlan
->ap_isolation
))
3279 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
, vid
);
3280 if (!tt_local_entry
)
3283 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
, vid
);
3284 if (!tt_global_entry
)
3287 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
3294 batadv_softif_vlan_free_ref(vlan
);
3295 if (tt_global_entry
)
3296 batadv_tt_global_entry_free_ref(tt_global_entry
);
3298 batadv_tt_local_entry_free_ref(tt_local_entry
);
3303 * batadv_tt_update_orig - update global translation table with new tt
3304 * information received via ogms
3305 * @bat_priv: the bat priv with all the soft interface information
3306 * @orig: the orig_node of the ogm
3307 * @tt_vlan: pointer to the first tvlv VLAN entry
3308 * @tt_num_vlan: number of tvlv VLAN entries
3309 * @tt_change: pointer to the first entry in the TT buffer
3310 * @tt_num_changes: number of tt changes inside the tt buffer
3311 * @ttvn: translation table version number of this changeset
3312 * @tt_crc: crc32 checksum of orig node's translation table
3314 static void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
3315 struct batadv_orig_node
*orig_node
,
3316 const void *tt_buff
, uint16_t tt_num_vlan
,
3317 struct batadv_tvlv_tt_change
*tt_change
,
3318 uint16_t tt_num_changes
, uint8_t ttvn
)
3320 uint8_t orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
3321 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3322 bool full_table
= true;
3325 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)tt_buff
;
3326 has_tt_init
= test_bit(BATADV_ORIG_CAPA_HAS_TT
,
3327 &orig_node
->capa_initialized
);
3329 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3330 * increased by one -> we can apply the attached changes
3332 if ((!has_tt_init
&& ttvn
== 1) || ttvn
- orig_ttvn
== 1) {
3333 /* the OGM could not contain the changes due to their size or
3334 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3336 * In this case send a tt request
3338 if (!tt_num_changes
) {
3343 spin_lock_bh(&orig_node
->tt_lock
);
3345 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
3348 /* Even if we received the precomputed crc with the OGM, we
3349 * prefer to recompute it to spot any possible inconsistency
3350 * in the global table
3352 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3354 spin_unlock_bh(&orig_node
->tt_lock
);
3356 /* The ttvn alone is not enough to guarantee consistency
3357 * because a single value could represent different states
3358 * (due to the wrap around). Thus a node has to check whether
3359 * the resulting table (after applying the changes) is still
3360 * consistent or not. E.g. a node could disconnect while its
3361 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3362 * checking the CRC value is mandatory to detect the
3365 if (!batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3369 /* if we missed more than one change or our tables are not
3370 * in sync anymore -> request fresh tt data
3372 if (!has_tt_init
|| ttvn
!= orig_ttvn
||
3373 !batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3376 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3377 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3378 orig_node
->orig
, ttvn
, orig_ttvn
,
3380 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
3381 tt_vlan
, tt_num_vlan
,
3389 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3390 * @bat_priv: the bat priv with all the soft interface information
3391 * @addr: the mac address of the client to check
3392 * @vid: VLAN identifier
3394 * Returns true if we know that the client has moved from its old originator
3395 * to another one. This entry is still kept for consistency purposes and will be
3396 * deleted later by a DEL or because of timeout
3398 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
3399 uint8_t *addr
, unsigned short vid
)
3401 struct batadv_tt_global_entry
*tt_global_entry
;
3404 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3405 if (!tt_global_entry
)
3408 ret
= tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3409 batadv_tt_global_entry_free_ref(tt_global_entry
);
3415 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3416 * @bat_priv: the bat priv with all the soft interface information
3417 * @addr: the mac address of the local client to query
3418 * @vid: VLAN identifier
3420 * Returns true if the local client is known to be roaming (it is not served by
3421 * this node anymore) or not. If yes, the client is still present in the table
3422 * to keep the latter consistent with the node TTVN
3424 bool batadv_tt_local_client_is_roaming(struct batadv_priv
*bat_priv
,
3425 uint8_t *addr
, unsigned short vid
)
3427 struct batadv_tt_local_entry
*tt_local_entry
;
3430 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
3431 if (!tt_local_entry
)
3434 ret
= tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3435 batadv_tt_local_entry_free_ref(tt_local_entry
);
3440 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
3441 struct batadv_orig_node
*orig_node
,
3442 const unsigned char *addr
,
3447 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
, vid
,
3448 BATADV_TT_CLIENT_TEMP
,
3449 atomic_read(&orig_node
->last_ttvn
)))
3452 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3453 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3454 addr
, BATADV_PRINT_VID(vid
), orig_node
->orig
);
3461 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3462 * maximum packet size that can be transported through the mesh
3463 * @soft_iface: netdev struct of the mesh interface
3465 * Remove entries older than 'timeout' and half timeout if more entries need
3468 void batadv_tt_local_resize_to_mtu(struct net_device
*soft_iface
)
3470 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
3471 int packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
3472 int table_size
, timeout
= BATADV_TT_LOCAL_TIMEOUT
/ 2;
3473 bool reduced
= false;
3475 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3478 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
3479 if (packet_size_max
>= table_size
)
3482 batadv_tt_local_purge(bat_priv
, timeout
);
3483 batadv_tt_local_purge_pending_clients(bat_priv
);
3487 net_ratelimited_function(batadv_info
, soft_iface
,
3488 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3492 /* commit these changes immediately, to avoid synchronization problem
3496 batadv_tt_local_commit_changes_nolock(bat_priv
);
3498 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3502 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3503 * @bat_priv: the bat priv with all the soft interface information
3504 * @orig: the orig_node of the ogm
3505 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3506 * @tvlv_value: tvlv buffer containing the gateway data
3507 * @tvlv_value_len: tvlv buffer length
3509 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
3510 struct batadv_orig_node
*orig
,
3511 uint8_t flags
, void *tvlv_value
,
3512 uint16_t tvlv_value_len
)
3514 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3515 struct batadv_tvlv_tt_change
*tt_change
;
3516 struct batadv_tvlv_tt_data
*tt_data
;
3517 uint16_t num_entries
, num_vlan
;
3519 if (tvlv_value_len
< sizeof(*tt_data
))
3522 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3523 tvlv_value_len
-= sizeof(*tt_data
);
3525 num_vlan
= ntohs(tt_data
->num_vlan
);
3527 if (tvlv_value_len
< sizeof(*tt_vlan
) * num_vlan
)
3530 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
3531 tt_change
= (struct batadv_tvlv_tt_change
*)(tt_vlan
+ num_vlan
);
3532 tvlv_value_len
-= sizeof(*tt_vlan
) * num_vlan
;
3534 num_entries
= batadv_tt_entries(tvlv_value_len
);
3536 batadv_tt_update_orig(bat_priv
, orig
, tt_vlan
, num_vlan
, tt_change
,
3537 num_entries
, tt_data
->ttvn
);
3541 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3543 * @bat_priv: the bat priv with all the soft interface information
3544 * @src: mac address of tt tvlv sender
3545 * @dst: mac address of tt tvlv recipient
3546 * @tvlv_value: tvlv buffer containing the tt data
3547 * @tvlv_value_len: tvlv buffer length
3549 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3552 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3553 uint8_t *src
, uint8_t *dst
,
3555 uint16_t tvlv_value_len
)
3557 struct batadv_tvlv_tt_data
*tt_data
;
3558 uint16_t tt_vlan_len
, tt_num_entries
;
3562 if (tvlv_value_len
< sizeof(*tt_data
))
3563 return NET_RX_SUCCESS
;
3565 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3566 tvlv_value_len
-= sizeof(*tt_data
);
3568 tt_vlan_len
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3569 tt_vlan_len
*= ntohs(tt_data
->num_vlan
);
3571 if (tvlv_value_len
< tt_vlan_len
)
3572 return NET_RX_SUCCESS
;
3574 tvlv_value_len
-= tt_vlan_len
;
3575 tt_num_entries
= batadv_tt_entries(tvlv_value_len
);
3577 switch (tt_data
->flags
& BATADV_TT_DATA_TYPE_MASK
) {
3578 case BATADV_TT_REQUEST
:
3579 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
3581 /* If this node cannot provide a TT response the tt_request is
3584 ret
= batadv_send_tt_response(bat_priv
, tt_data
, src
, dst
);
3586 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3591 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3592 "Routing TT_REQUEST to %pM [%c]\n",
3594 /* tvlv API will re-route the packet */
3598 case BATADV_TT_RESPONSE
:
3599 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
3601 if (batadv_is_my_mac(bat_priv
, dst
)) {
3602 batadv_handle_tt_response(bat_priv
, tt_data
,
3603 src
, tt_num_entries
);
3604 return NET_RX_SUCCESS
;
3607 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3612 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3613 "Routing TT_RESPONSE to %pM [%c]\n", dst
, tt_flag
);
3615 /* tvlv API will re-route the packet */
3619 return NET_RX_SUCCESS
;
3623 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3624 * @bat_priv: the bat priv with all the soft interface information
3625 * @src: mac address of tt tvlv sender
3626 * @dst: mac address of tt tvlv recipient
3627 * @tvlv_value: tvlv buffer containing the tt data
3628 * @tvlv_value_len: tvlv buffer length
3630 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3633 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3634 uint8_t *src
, uint8_t *dst
,
3636 uint16_t tvlv_value_len
)
3638 struct batadv_tvlv_roam_adv
*roaming_adv
;
3639 struct batadv_orig_node
*orig_node
= NULL
;
3641 /* If this node is not the intended recipient of the
3642 * roaming advertisement the packet is forwarded
3643 * (the tvlv API will re-route the packet).
3645 if (!batadv_is_my_mac(bat_priv
, dst
))
3648 if (tvlv_value_len
< sizeof(*roaming_adv
))
3651 orig_node
= batadv_orig_hash_find(bat_priv
, src
);
3655 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
3656 roaming_adv
= (struct batadv_tvlv_roam_adv
*)tvlv_value
;
3658 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3659 "Received ROAMING_ADV from %pM (client %pM)\n",
3660 src
, roaming_adv
->client
);
3662 batadv_tt_global_add(bat_priv
, orig_node
, roaming_adv
->client
,
3663 ntohs(roaming_adv
->vid
), BATADV_TT_CLIENT_ROAM
,
3664 atomic_read(&orig_node
->last_ttvn
) + 1);
3668 batadv_orig_node_free_ref(orig_node
);
3669 return NET_RX_SUCCESS
;
3673 * batadv_tt_init - initialise the translation table internals
3674 * @bat_priv: the bat priv with all the soft interface information
3676 * Return 0 on success or negative error number in case of failure.
3678 int batadv_tt_init(struct batadv_priv
*bat_priv
)
3682 /* synchronized flags must be remote */
3683 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK
& BATADV_TT_REMOTE_MASK
));
3685 ret
= batadv_tt_local_init(bat_priv
);
3689 ret
= batadv_tt_global_init(bat_priv
);
3693 batadv_tvlv_handler_register(bat_priv
, batadv_tt_tvlv_ogm_handler_v1
,
3694 batadv_tt_tvlv_unicast_handler_v1
,
3695 BATADV_TVLV_TT
, 1, BATADV_NO_FLAGS
);
3697 batadv_tvlv_handler_register(bat_priv
, NULL
,
3698 batadv_roam_tvlv_unicast_handler_v1
,
3699 BATADV_TVLV_ROAM
, 1, BATADV_NO_FLAGS
);
3701 INIT_DELAYED_WORK(&bat_priv
->tt
.work
, batadv_tt_purge
);
3702 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3703 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3709 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3710 * @bat_priv: the bat priv with all the soft interface information
3711 * @addr: the mac address of the client
3712 * @vid: the identifier of the VLAN where this client is connected
3714 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3717 bool batadv_tt_global_is_isolated(struct batadv_priv
*bat_priv
,
3718 const uint8_t *addr
, unsigned short vid
)
3720 struct batadv_tt_global_entry
*tt
;
3723 tt
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3727 ret
= tt
->common
.flags
& BATADV_TT_CLIENT_ISOLA
;
3729 batadv_tt_global_entry_free_ref(tt
);