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
, u8
*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 and vid */
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
,
76 const struct batadv_tt_common_entry
*tt1
= data1
;
77 const struct batadv_tt_common_entry
*tt2
= data2
;
79 return (tt1
->vid
== tt2
->vid
) && batadv_compare_eth(data1
, data2
);
83 * batadv_choose_tt - return the index of the tt entry in the hash table
84 * @data: pointer to the tt_common_entry object to map
85 * @size: the size of the hash table
87 * Returns the hash index where the object represented by 'data' should be
90 static inline u32
batadv_choose_tt(const void *data
, u32 size
)
92 struct batadv_tt_common_entry
*tt
;
95 tt
= (struct batadv_tt_common_entry
*)data
;
96 hash
= jhash(&tt
->addr
, ETH_ALEN
, hash
);
97 hash
= jhash(&tt
->vid
, sizeof(tt
->vid
), hash
);
103 * batadv_tt_hash_find - look for a client in the given hash table
104 * @hash: the hash table to search
105 * @addr: the mac address of the client to look for
106 * @vid: VLAN identifier
108 * Returns a pointer to the tt_common struct belonging to the searched client if
109 * found, NULL otherwise.
111 static struct batadv_tt_common_entry
*
112 batadv_tt_hash_find(struct batadv_hashtable
*hash
, const u8
*addr
,
115 struct hlist_head
*head
;
116 struct batadv_tt_common_entry to_search
, *tt
, *tt_tmp
= NULL
;
122 ether_addr_copy(to_search
.addr
, addr
);
125 index
= batadv_choose_tt(&to_search
, hash
->size
);
126 head
= &hash
->table
[index
];
129 hlist_for_each_entry_rcu(tt
, head
, hash_entry
) {
130 if (!batadv_compare_eth(tt
, addr
))
136 if (!atomic_inc_not_zero(&tt
->refcount
))
148 * batadv_tt_local_hash_find - search the local table for a given client
149 * @bat_priv: the bat priv with all the soft interface information
150 * @addr: the mac address of the client to look for
151 * @vid: VLAN identifier
153 * Returns a pointer to the corresponding tt_local_entry struct if the client is
154 * found, NULL otherwise.
156 static struct batadv_tt_local_entry
*
157 batadv_tt_local_hash_find(struct batadv_priv
*bat_priv
, const u8
*addr
,
160 struct batadv_tt_common_entry
*tt_common_entry
;
161 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
163 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.local_hash
, addr
,
166 tt_local_entry
= container_of(tt_common_entry
,
167 struct batadv_tt_local_entry
,
169 return tt_local_entry
;
173 * batadv_tt_global_hash_find - search the global table for a given client
174 * @bat_priv: the bat priv with all the soft interface information
175 * @addr: the mac address of the client to look for
176 * @vid: VLAN identifier
178 * Returns a pointer to the corresponding tt_global_entry struct if the client
179 * is found, NULL otherwise.
181 static struct batadv_tt_global_entry
*
182 batadv_tt_global_hash_find(struct batadv_priv
*bat_priv
, const u8
*addr
,
185 struct batadv_tt_common_entry
*tt_common_entry
;
186 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
188 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.global_hash
, addr
,
191 tt_global_entry
= container_of(tt_common_entry
,
192 struct batadv_tt_global_entry
,
194 return tt_global_entry
;
198 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry
*tt_local_entry
)
200 if (atomic_dec_and_test(&tt_local_entry
->common
.refcount
))
201 kfree_rcu(tt_local_entry
, common
.rcu
);
205 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
206 * tt_global_entry and possibly free it
207 * @tt_global_entry: the object to free
210 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry
*tt_global_entry
)
212 if (atomic_dec_and_test(&tt_global_entry
->common
.refcount
)) {
213 batadv_tt_global_del_orig_list(tt_global_entry
);
214 kfree_rcu(tt_global_entry
, common
.rcu
);
219 * batadv_tt_global_hash_count - count the number of orig entries
220 * @addr: the mac address of the client to count entries for
221 * @vid: VLAN identifier
223 * Return the number of originators advertising the given address/data
224 * (excluding ourself).
226 int batadv_tt_global_hash_count(struct batadv_priv
*bat_priv
,
227 const u8
*addr
, unsigned short vid
)
229 struct batadv_tt_global_entry
*tt_global_entry
;
232 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
233 if (!tt_global_entry
)
236 count
= atomic_read(&tt_global_entry
->orig_list_count
);
237 batadv_tt_global_entry_free_ref(tt_global_entry
);
243 * batadv_tt_local_size_mod - change the size by v of the local table identified
245 * @bat_priv: the bat priv with all the soft interface information
246 * @vid: the VLAN identifier of the sub-table to change
247 * @v: the amount to sum to the local table size
249 static void batadv_tt_local_size_mod(struct batadv_priv
*bat_priv
,
250 unsigned short vid
, int v
)
252 struct batadv_softif_vlan
*vlan
;
254 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
258 atomic_add(v
, &vlan
->tt
.num_entries
);
260 batadv_softif_vlan_free_ref(vlan
);
264 * batadv_tt_local_size_inc - increase by one the local table size for the given
266 * @bat_priv: the bat priv with all the soft interface information
267 * @vid: the VLAN identifier
269 static void batadv_tt_local_size_inc(struct batadv_priv
*bat_priv
,
272 batadv_tt_local_size_mod(bat_priv
, vid
, 1);
276 * batadv_tt_local_size_dec - decrease by one the local table size for the given
278 * @bat_priv: the bat priv with all the soft interface information
279 * @vid: the VLAN identifier
281 static void batadv_tt_local_size_dec(struct batadv_priv
*bat_priv
,
284 batadv_tt_local_size_mod(bat_priv
, vid
, -1);
288 * batadv_tt_global_size_mod - change the size by v of the local table
290 * @vid: the VLAN identifier
291 * @v: the amount to sum to the global table size
293 static void batadv_tt_global_size_mod(struct batadv_orig_node
*orig_node
,
294 unsigned short vid
, int v
)
296 struct batadv_orig_node_vlan
*vlan
;
298 vlan
= batadv_orig_node_vlan_new(orig_node
, vid
);
302 if (atomic_add_return(v
, &vlan
->tt
.num_entries
) == 0) {
303 spin_lock_bh(&orig_node
->vlan_list_lock
);
304 hlist_del_init_rcu(&vlan
->list
);
305 spin_unlock_bh(&orig_node
->vlan_list_lock
);
306 batadv_orig_node_vlan_free_ref(vlan
);
309 batadv_orig_node_vlan_free_ref(vlan
);
313 * batadv_tt_global_size_inc - increase by one the global table size for the
315 * @orig_node: the originator which global table size has to be decreased
316 * @vid: the vlan identifier
318 static void batadv_tt_global_size_inc(struct batadv_orig_node
*orig_node
,
321 batadv_tt_global_size_mod(orig_node
, vid
, 1);
325 * batadv_tt_global_size_dec - decrease by one the global table size for the
327 * @orig_node: the originator which global table size has to be decreased
328 * @vid: the vlan identifier
330 static void batadv_tt_global_size_dec(struct batadv_orig_node
*orig_node
,
333 batadv_tt_global_size_mod(orig_node
, vid
, -1);
337 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
338 * queue for free after rcu grace period
339 * @orig_entry: tt orig entry to be free'd
342 batadv_tt_orig_list_entry_release(struct batadv_tt_orig_list_entry
*orig_entry
)
344 batadv_orig_node_free_ref(orig_entry
->orig_node
);
345 kfree_rcu(orig_entry
, rcu
);
349 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry
*orig_entry
)
351 if (!atomic_dec_and_test(&orig_entry
->refcount
))
354 batadv_tt_orig_list_entry_release(orig_entry
);
358 * batadv_tt_local_event - store a local TT event (ADD/DEL)
359 * @bat_priv: the bat priv with all the soft interface information
360 * @tt_local_entry: the TT entry involved in the event
361 * @event_flags: flags to store in the event structure
363 static void batadv_tt_local_event(struct batadv_priv
*bat_priv
,
364 struct batadv_tt_local_entry
*tt_local_entry
,
367 struct batadv_tt_change_node
*tt_change_node
, *entry
, *safe
;
368 struct batadv_tt_common_entry
*common
= &tt_local_entry
->common
;
369 u8 flags
= common
->flags
| event_flags
;
370 bool event_removed
= false;
371 bool del_op_requested
, del_op_entry
;
373 tt_change_node
= kmalloc(sizeof(*tt_change_node
), GFP_ATOMIC
);
377 tt_change_node
->change
.flags
= flags
;
378 memset(tt_change_node
->change
.reserved
, 0,
379 sizeof(tt_change_node
->change
.reserved
));
380 ether_addr_copy(tt_change_node
->change
.addr
, common
->addr
);
381 tt_change_node
->change
.vid
= htons(common
->vid
);
383 del_op_requested
= flags
& BATADV_TT_CLIENT_DEL
;
385 /* check for ADD+DEL or DEL+ADD events */
386 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
387 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
389 if (!batadv_compare_eth(entry
->change
.addr
, common
->addr
))
392 /* DEL+ADD in the same orig interval have no effect and can be
393 * removed to avoid silly behaviour on the receiver side. The
394 * other way around (ADD+DEL) can happen in case of roaming of
395 * a client still in the NEW state. Roaming of NEW clients is
396 * now possible due to automatically recognition of "temporary"
399 del_op_entry
= entry
->change
.flags
& BATADV_TT_CLIENT_DEL
;
400 if (!del_op_requested
&& del_op_entry
)
402 if (del_op_requested
&& !del_op_entry
)
405 /* this is a second add in the same originator interval. It
406 * means that flags have been changed: update them!
408 if (!del_op_requested
&& !del_op_entry
)
409 entry
->change
.flags
= flags
;
413 list_del(&entry
->list
);
415 kfree(tt_change_node
);
416 event_removed
= true;
420 /* track the change in the OGMinterval list */
421 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt
.changes_list
);
424 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
427 atomic_dec(&bat_priv
->tt
.local_changes
);
429 atomic_inc(&bat_priv
->tt
.local_changes
);
433 * batadv_tt_len - compute length in bytes of given number of tt changes
434 * @changes_num: number of tt changes
436 * Returns computed length in bytes.
438 static int batadv_tt_len(int changes_num
)
440 return changes_num
* sizeof(struct batadv_tvlv_tt_change
);
444 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
445 * @tt_len: available space
447 * Returns the number of entries.
449 static u16
batadv_tt_entries(u16 tt_len
)
451 return tt_len
/ batadv_tt_len(1);
455 * batadv_tt_local_table_transmit_size - calculates the local translation table
456 * size when transmitted over the air
457 * @bat_priv: the bat priv with all the soft interface information
459 * Returns local translation table size in bytes.
461 static int batadv_tt_local_table_transmit_size(struct batadv_priv
*bat_priv
)
464 u16 tt_local_entries
= 0;
465 struct batadv_softif_vlan
*vlan
;
469 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
471 tt_local_entries
+= atomic_read(&vlan
->tt
.num_entries
);
475 /* header size of tvlv encapsulated tt response payload */
476 hdr_size
= sizeof(struct batadv_unicast_tvlv_packet
);
477 hdr_size
+= sizeof(struct batadv_tvlv_hdr
);
478 hdr_size
+= sizeof(struct batadv_tvlv_tt_data
);
479 hdr_size
+= num_vlan
* sizeof(struct batadv_tvlv_tt_vlan_data
);
481 return hdr_size
+ batadv_tt_len(tt_local_entries
);
484 static int batadv_tt_local_init(struct batadv_priv
*bat_priv
)
486 if (bat_priv
->tt
.local_hash
)
489 bat_priv
->tt
.local_hash
= batadv_hash_new(1024);
491 if (!bat_priv
->tt
.local_hash
)
494 batadv_hash_set_lock_class(bat_priv
->tt
.local_hash
,
495 &batadv_tt_local_hash_lock_class_key
);
500 static void batadv_tt_global_free(struct batadv_priv
*bat_priv
,
501 struct batadv_tt_global_entry
*tt_global
,
504 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
505 "Deleting global tt entry %pM (vid: %d): %s\n",
506 tt_global
->common
.addr
,
507 BATADV_PRINT_VID(tt_global
->common
.vid
), message
);
509 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
510 batadv_choose_tt
, &tt_global
->common
);
511 batadv_tt_global_entry_free_ref(tt_global
);
515 * batadv_tt_local_add - add a new client to the local table or update an
517 * @soft_iface: netdev struct of the mesh interface
518 * @addr: the mac address of the client to add
519 * @vid: VLAN identifier
520 * @ifindex: index of the interface where the client is connected to (useful to
521 * identify wireless clients)
522 * @mark: the value contained in the skb->mark field of the received packet (if
525 * Returns true if the client was successfully added, false otherwise.
527 bool batadv_tt_local_add(struct net_device
*soft_iface
, const u8
*addr
,
528 unsigned short vid
, int ifindex
, u32 mark
)
530 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
531 struct batadv_tt_local_entry
*tt_local
;
532 struct batadv_tt_global_entry
*tt_global
= NULL
;
533 struct batadv_softif_vlan
*vlan
;
534 struct net_device
*in_dev
= NULL
;
535 struct hlist_head
*head
;
536 struct batadv_tt_orig_list_entry
*orig_entry
;
537 int hash_added
, table_size
, packet_size_max
;
539 bool roamed_back
= false;
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
);
598 if (WARN(!vlan
, "adding TT local entry %pM to non-existent VLAN %d",
599 addr
, BATADV_PRINT_VID(vid
))) {
605 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
606 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
607 addr
, BATADV_PRINT_VID(vid
),
608 (u8
)atomic_read(&bat_priv
->tt
.vn
));
610 ether_addr_copy(tt_local
->common
.addr
, addr
);
611 /* The local entry has to be marked as NEW to avoid to send it in
612 * a full table response going out before the next ttvn increment
613 * (consistency check)
615 tt_local
->common
.flags
= BATADV_TT_CLIENT_NEW
;
616 tt_local
->common
.vid
= vid
;
617 if (batadv_is_wifi_netdev(in_dev
))
618 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
619 atomic_set(&tt_local
->common
.refcount
, 2);
620 tt_local
->last_seen
= jiffies
;
621 tt_local
->common
.added_at
= tt_local
->last_seen
;
623 /* the batman interface mac and multicast addresses should never be
626 if (batadv_compare_eth(addr
, soft_iface
->dev_addr
) ||
627 is_multicast_ether_addr(addr
))
628 tt_local
->common
.flags
|= BATADV_TT_CLIENT_NOPURGE
;
630 hash_added
= batadv_hash_add(bat_priv
->tt
.local_hash
, batadv_compare_tt
,
631 batadv_choose_tt
, &tt_local
->common
,
632 &tt_local
->common
.hash_entry
);
634 if (unlikely(hash_added
!= 0)) {
635 /* remove the reference for the hash */
636 batadv_tt_local_entry_free_ref(tt_local
);
637 batadv_softif_vlan_free_ref(vlan
);
642 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
645 /* Check whether it is a roaming, but don't do anything if the roaming
646 * process has already been handled
648 if (tt_global
&& !(tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
)) {
649 /* These node are probably going to update their tt table */
650 head
= &tt_global
->orig_list
;
652 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
653 batadv_send_roam_adv(bat_priv
, tt_global
->common
.addr
,
654 tt_global
->common
.vid
,
655 orig_entry
->orig_node
);
659 batadv_tt_global_free(bat_priv
, tt_global
,
663 /* The global entry has to be marked as ROAMING and
664 * has to be kept for consistency purpose
666 tt_global
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
667 tt_global
->roam_at
= jiffies
;
671 /* store the current remote flags before altering them. This helps
672 * understanding is flags are changing or not
674 remote_flags
= tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
;
676 if (batadv_is_wifi_netdev(in_dev
))
677 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
679 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_WIFI
;
681 /* check the mark in the skb: if it's equal to the configured
682 * isolation_mark, it means the packet is coming from an isolated
685 match_mark
= (mark
& bat_priv
->isolation_mark_mask
);
686 if (bat_priv
->isolation_mark_mask
&&
687 match_mark
== bat_priv
->isolation_mark
)
688 tt_local
->common
.flags
|= BATADV_TT_CLIENT_ISOLA
;
690 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ISOLA
;
692 /* if any "dynamic" flag has been modified, resend an ADD event for this
693 * entry so that all the nodes can get the new flags
695 if (remote_flags
^ (tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
))
696 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
703 batadv_tt_local_entry_free_ref(tt_local
);
705 batadv_tt_global_entry_free_ref(tt_global
);
710 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
711 * within a TT Response directed to another node
712 * @orig_node: originator for which the TT data has to be prepared
713 * @tt_data: uninitialised pointer to the address of the TVLV buffer
714 * @tt_change: uninitialised pointer to the address of the area where the TT
715 * changed can be stored
716 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
717 * function reserves the amount of space needed to send the entire global TT
718 * table. In case of success the value is updated with the real amount of
721 * Allocate the needed amount of memory for the entire TT TVLV and write its
722 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
723 * objects, one per active VLAN served by the originator node.
725 * Return the size of the allocated buffer or 0 in case of failure.
728 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node
*orig_node
,
729 struct batadv_tvlv_tt_data
**tt_data
,
730 struct batadv_tvlv_tt_change
**tt_change
,
737 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
738 struct batadv_orig_node_vlan
*vlan
;
742 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
744 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
747 change_offset
= sizeof(**tt_data
);
748 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
750 /* if tt_len is negative, allocate the space needed by the full table */
752 *tt_len
= batadv_tt_len(num_entries
);
755 tvlv_len
+= change_offset
;
757 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
763 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
764 (*tt_data
)->ttvn
= atomic_read(&orig_node
->last_ttvn
);
765 (*tt_data
)->num_vlan
= htons(num_vlan
);
767 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
768 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
769 tt_vlan
->vid
= htons(vlan
->vid
);
770 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
775 tt_change_ptr
= (u8
*)*tt_data
+ change_offset
;
776 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
784 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
786 * @bat_priv: the bat priv with all the soft interface information
787 * @tt_data: uninitialised pointer to the address of the TVLV buffer
788 * @tt_change: uninitialised pointer to the address of the area where the TT
789 * changes can be stored
790 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
791 * function reserves the amount of space needed to send the entire local TT
792 * table. In case of success the value is updated with the real amount of
795 * Allocate the needed amount of memory for the entire TT TVLV and write its
796 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
797 * objects, one per active VLAN.
799 * Return the size of the allocated buffer or 0 in case of failure.
802 batadv_tt_prepare_tvlv_local_data(struct batadv_priv
*bat_priv
,
803 struct batadv_tvlv_tt_data
**tt_data
,
804 struct batadv_tvlv_tt_change
**tt_change
,
807 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
808 struct batadv_softif_vlan
*vlan
;
816 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
818 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
821 change_offset
= sizeof(**tt_data
);
822 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
824 /* if tt_len is negative, allocate the space needed by the full table */
826 *tt_len
= batadv_tt_len(num_entries
);
829 tvlv_len
+= change_offset
;
831 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
837 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
838 (*tt_data
)->ttvn
= atomic_read(&bat_priv
->tt
.vn
);
839 (*tt_data
)->num_vlan
= htons(num_vlan
);
841 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
842 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
843 tt_vlan
->vid
= htons(vlan
->vid
);
844 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
849 tt_change_ptr
= (u8
*)*tt_data
+ change_offset
;
850 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
858 * batadv_tt_tvlv_container_update - update the translation table tvlv container
859 * after local tt changes have been committed
860 * @bat_priv: the bat priv with all the soft interface information
862 static void batadv_tt_tvlv_container_update(struct batadv_priv
*bat_priv
)
864 struct batadv_tt_change_node
*entry
, *safe
;
865 struct batadv_tvlv_tt_data
*tt_data
;
866 struct batadv_tvlv_tt_change
*tt_change
;
867 int tt_diff_len
, tt_change_len
= 0;
868 int tt_diff_entries_num
= 0;
869 int tt_diff_entries_count
= 0;
872 tt_diff_entries_num
= atomic_read(&bat_priv
->tt
.local_changes
);
873 tt_diff_len
= batadv_tt_len(tt_diff_entries_num
);
875 /* if we have too many changes for one packet don't send any
876 * and wait for the tt table request which will be fragmented
878 if (tt_diff_len
> bat_priv
->soft_iface
->mtu
)
881 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
, &tt_data
,
882 &tt_change
, &tt_diff_len
);
886 tt_data
->flags
= BATADV_TT_OGM_DIFF
;
888 if (tt_diff_len
== 0)
889 goto container_register
;
891 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
892 atomic_set(&bat_priv
->tt
.local_changes
, 0);
894 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
896 if (tt_diff_entries_count
< tt_diff_entries_num
) {
897 memcpy(tt_change
+ tt_diff_entries_count
,
899 sizeof(struct batadv_tvlv_tt_change
));
900 tt_diff_entries_count
++;
902 list_del(&entry
->list
);
905 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
907 /* Keep the buffer for possible tt_request */
908 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
909 kfree(bat_priv
->tt
.last_changeset
);
910 bat_priv
->tt
.last_changeset_len
= 0;
911 bat_priv
->tt
.last_changeset
= NULL
;
912 tt_change_len
= batadv_tt_len(tt_diff_entries_count
);
913 /* check whether this new OGM has no changes due to size problems */
914 if (tt_diff_entries_count
> 0) {
915 /* if kmalloc() fails we will reply with the full table
916 * instead of providing the diff
918 bat_priv
->tt
.last_changeset
= kzalloc(tt_diff_len
, GFP_ATOMIC
);
919 if (bat_priv
->tt
.last_changeset
) {
920 memcpy(bat_priv
->tt
.last_changeset
,
921 tt_change
, tt_change_len
);
922 bat_priv
->tt
.last_changeset_len
= tt_diff_len
;
925 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
928 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_TT
, 1, tt_data
,
933 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
935 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
936 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
937 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
938 struct batadv_tt_common_entry
*tt_common_entry
;
939 struct batadv_tt_local_entry
*tt_local
;
940 struct batadv_hard_iface
*primary_if
;
941 struct batadv_softif_vlan
*vlan
;
942 struct hlist_head
*head
;
947 unsigned long last_seen_jiffies
;
949 u16 np_flag
= BATADV_TT_CLIENT_NOPURGE
;
951 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
956 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
957 net_dev
->name
, (u8
)atomic_read(&bat_priv
->tt
.vn
));
958 seq_printf(seq
, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
959 "Flags", "Last seen", "CRC");
961 for (i
= 0; i
< hash
->size
; i
++) {
962 head
= &hash
->table
[i
];
965 hlist_for_each_entry_rcu(tt_common_entry
,
967 tt_local
= container_of(tt_common_entry
,
968 struct batadv_tt_local_entry
,
970 vid
= tt_common_entry
->vid
;
971 last_seen_jiffies
= jiffies
- tt_local
->last_seen
;
972 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
973 last_seen_secs
= last_seen_msecs
/ 1000;
974 last_seen_msecs
= last_seen_msecs
% 1000;
976 no_purge
= tt_common_entry
->flags
& np_flag
;
978 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
980 seq_printf(seq
, "Cannot retrieve VLAN %d\n",
981 BATADV_PRINT_VID(vid
));
986 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
987 tt_common_entry
->addr
,
988 BATADV_PRINT_VID(tt_common_entry
->vid
),
989 ((tt_common_entry
->flags
&
990 BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
991 no_purge
? 'P' : '.',
992 ((tt_common_entry
->flags
&
993 BATADV_TT_CLIENT_NEW
) ? 'N' : '.'),
994 ((tt_common_entry
->flags
&
995 BATADV_TT_CLIENT_PENDING
) ? 'X' : '.'),
996 ((tt_common_entry
->flags
&
997 BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
998 ((tt_common_entry
->flags
&
999 BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1000 no_purge
? 0 : last_seen_secs
,
1001 no_purge
? 0 : last_seen_msecs
,
1004 batadv_softif_vlan_free_ref(vlan
);
1010 batadv_hardif_free_ref(primary_if
);
1015 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
1016 struct batadv_tt_local_entry
*tt_local_entry
,
1017 u16 flags
, const char *message
)
1019 batadv_tt_local_event(bat_priv
, tt_local_entry
, flags
);
1021 /* The local client has to be marked as "pending to be removed" but has
1022 * to be kept in the table in order to send it in a full table
1023 * response issued before the net ttvn increment (consistency check)
1025 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
1027 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1028 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1029 tt_local_entry
->common
.addr
,
1030 BATADV_PRINT_VID(tt_local_entry
->common
.vid
), message
);
1034 * batadv_tt_local_remove - logically remove an entry from the local table
1035 * @bat_priv: the bat priv with all the soft interface information
1036 * @addr: the MAC address of the client to remove
1037 * @vid: VLAN identifier
1038 * @message: message to append to the log on deletion
1039 * @roaming: true if the deletion is due to a roaming event
1041 * Returns the flags assigned to the local entry before being deleted
1043 u16
batadv_tt_local_remove(struct batadv_priv
*bat_priv
, const u8
*addr
,
1044 unsigned short vid
, const char *message
,
1047 struct batadv_tt_local_entry
*tt_local_entry
;
1048 u16 flags
, curr_flags
= BATADV_NO_FLAGS
;
1049 struct batadv_softif_vlan
*vlan
;
1050 void *tt_entry_exists
;
1052 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
1053 if (!tt_local_entry
)
1056 curr_flags
= tt_local_entry
->common
.flags
;
1058 flags
= BATADV_TT_CLIENT_DEL
;
1059 /* if this global entry addition is due to a roaming, the node has to
1060 * mark the local entry as "roamed" in order to correctly reroute
1064 flags
|= BATADV_TT_CLIENT_ROAM
;
1065 /* mark the local client as ROAMed */
1066 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1069 if (!(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
)) {
1070 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
,
1074 /* if this client has been added right now, it is possible to
1075 * immediately purge it
1077 batadv_tt_local_event(bat_priv
, tt_local_entry
, BATADV_TT_CLIENT_DEL
);
1079 tt_entry_exists
= batadv_hash_remove(bat_priv
->tt
.local_hash
,
1082 &tt_local_entry
->common
);
1083 if (!tt_entry_exists
)
1086 /* extra call to free the local tt entry */
1087 batadv_tt_local_entry_free_ref(tt_local_entry
);
1089 /* decrease the reference held for this vlan */
1090 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
1094 batadv_softif_vlan_free_ref(vlan
);
1095 batadv_softif_vlan_free_ref(vlan
);
1099 batadv_tt_local_entry_free_ref(tt_local_entry
);
1105 * batadv_tt_local_purge_list - purge inactive tt local entries
1106 * @bat_priv: the bat priv with all the soft interface information
1107 * @head: pointer to the list containing the local tt entries
1108 * @timeout: parameter deciding whether a given tt local entry is considered
1111 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
1112 struct hlist_head
*head
,
1115 struct batadv_tt_local_entry
*tt_local_entry
;
1116 struct batadv_tt_common_entry
*tt_common_entry
;
1117 struct hlist_node
*node_tmp
;
1119 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
, head
,
1121 tt_local_entry
= container_of(tt_common_entry
,
1122 struct batadv_tt_local_entry
,
1124 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
1127 /* entry already marked for deletion */
1128 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1131 if (!batadv_has_timed_out(tt_local_entry
->last_seen
, timeout
))
1134 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
1135 BATADV_TT_CLIENT_DEL
, "timed out");
1140 * batadv_tt_local_purge - purge inactive tt local entries
1141 * @bat_priv: the bat priv with all the soft interface information
1142 * @timeout: parameter deciding whether a given tt local entry is considered
1145 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
,
1148 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1149 struct hlist_head
*head
;
1150 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1153 for (i
= 0; i
< hash
->size
; i
++) {
1154 head
= &hash
->table
[i
];
1155 list_lock
= &hash
->list_locks
[i
];
1157 spin_lock_bh(list_lock
);
1158 batadv_tt_local_purge_list(bat_priv
, head
, timeout
);
1159 spin_unlock_bh(list_lock
);
1163 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
1165 struct batadv_hashtable
*hash
;
1166 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1167 struct batadv_tt_common_entry
*tt_common_entry
;
1168 struct batadv_tt_local_entry
*tt_local
;
1169 struct batadv_softif_vlan
*vlan
;
1170 struct hlist_node
*node_tmp
;
1171 struct hlist_head
*head
;
1174 if (!bat_priv
->tt
.local_hash
)
1177 hash
= bat_priv
->tt
.local_hash
;
1179 for (i
= 0; i
< hash
->size
; i
++) {
1180 head
= &hash
->table
[i
];
1181 list_lock
= &hash
->list_locks
[i
];
1183 spin_lock_bh(list_lock
);
1184 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1186 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1187 tt_local
= container_of(tt_common_entry
,
1188 struct batadv_tt_local_entry
,
1191 /* decrease the reference held for this vlan */
1192 vlan
= batadv_softif_vlan_get(bat_priv
,
1193 tt_common_entry
->vid
);
1195 batadv_softif_vlan_free_ref(vlan
);
1196 batadv_softif_vlan_free_ref(vlan
);
1199 batadv_tt_local_entry_free_ref(tt_local
);
1201 spin_unlock_bh(list_lock
);
1204 batadv_hash_destroy(hash
);
1206 bat_priv
->tt
.local_hash
= NULL
;
1209 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
1211 if (bat_priv
->tt
.global_hash
)
1214 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
1216 if (!bat_priv
->tt
.global_hash
)
1219 batadv_hash_set_lock_class(bat_priv
->tt
.global_hash
,
1220 &batadv_tt_global_hash_lock_class_key
);
1225 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
1227 struct batadv_tt_change_node
*entry
, *safe
;
1229 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
1231 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
1233 list_del(&entry
->list
);
1237 atomic_set(&bat_priv
->tt
.local_changes
, 0);
1238 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
1241 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1242 * batadv_tt_global_entry list
1244 * returns it with an increased refcounter, NULL if not found
1246 static struct batadv_tt_orig_list_entry
*
1247 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
1248 const struct batadv_orig_node
*orig_node
)
1250 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
1251 const struct hlist_head
*head
;
1254 head
= &entry
->orig_list
;
1255 hlist_for_each_entry_rcu(tmp_orig_entry
, head
, list
) {
1256 if (tmp_orig_entry
->orig_node
!= orig_node
)
1258 if (!atomic_inc_not_zero(&tmp_orig_entry
->refcount
))
1261 orig_entry
= tmp_orig_entry
;
1269 /* find out if an orig_node is already in the list of a tt_global_entry.
1270 * returns true if found, false otherwise
1273 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
1274 const struct batadv_orig_node
*orig_node
)
1276 struct batadv_tt_orig_list_entry
*orig_entry
;
1279 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
1282 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1289 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
1290 struct batadv_orig_node
*orig_node
, int ttvn
)
1292 struct batadv_tt_orig_list_entry
*orig_entry
;
1294 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
1296 /* refresh the ttvn: the current value could be a bogus one that
1297 * was added during a "temporary client detection"
1299 orig_entry
->ttvn
= ttvn
;
1303 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
1307 INIT_HLIST_NODE(&orig_entry
->list
);
1308 atomic_inc(&orig_node
->refcount
);
1309 batadv_tt_global_size_inc(orig_node
, tt_global
->common
.vid
);
1310 orig_entry
->orig_node
= orig_node
;
1311 orig_entry
->ttvn
= ttvn
;
1312 atomic_set(&orig_entry
->refcount
, 2);
1314 spin_lock_bh(&tt_global
->list_lock
);
1315 hlist_add_head_rcu(&orig_entry
->list
,
1316 &tt_global
->orig_list
);
1317 spin_unlock_bh(&tt_global
->list_lock
);
1318 atomic_inc(&tt_global
->orig_list_count
);
1322 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1326 * batadv_tt_global_add - add a new TT global entry or update an existing one
1327 * @bat_priv: the bat priv with all the soft interface information
1328 * @orig_node: the originator announcing the client
1329 * @tt_addr: the mac address of the non-mesh client
1330 * @vid: VLAN identifier
1331 * @flags: TT flags that have to be set for this non-mesh client
1332 * @ttvn: the tt version number ever announcing this non-mesh client
1334 * Add a new TT global entry for the given originator. If the entry already
1335 * exists add a new reference to the given originator (a global entry can have
1336 * references to multiple originators) and adjust the flags attribute to reflect
1337 * the function argument.
1338 * If a TT local entry exists for this non-mesh client remove it.
1340 * The caller must hold orig_node refcount.
1342 * Return true if the new entry has been added, false otherwise
1344 static bool batadv_tt_global_add(struct batadv_priv
*bat_priv
,
1345 struct batadv_orig_node
*orig_node
,
1346 const unsigned char *tt_addr
,
1347 unsigned short vid
, u16 flags
, u8 ttvn
)
1349 struct batadv_tt_global_entry
*tt_global_entry
;
1350 struct batadv_tt_local_entry
*tt_local_entry
;
1353 struct batadv_tt_common_entry
*common
;
1356 /* ignore global entries from backbone nodes */
1357 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
, vid
))
1360 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
, vid
);
1361 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, tt_addr
, vid
);
1363 /* if the node already has a local client for this entry, it has to wait
1364 * for a roaming advertisement instead of manually messing up the global
1367 if ((flags
& BATADV_TT_CLIENT_TEMP
) && tt_local_entry
&&
1368 !(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
))
1371 if (!tt_global_entry
) {
1372 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
1373 if (!tt_global_entry
)
1376 common
= &tt_global_entry
->common
;
1377 ether_addr_copy(common
->addr
, tt_addr
);
1380 common
->flags
= flags
;
1381 tt_global_entry
->roam_at
= 0;
1382 /* node must store current time in case of roaming. This is
1383 * needed to purge this entry out on timeout (if nobody claims
1386 if (flags
& BATADV_TT_CLIENT_ROAM
)
1387 tt_global_entry
->roam_at
= jiffies
;
1388 atomic_set(&common
->refcount
, 2);
1389 common
->added_at
= jiffies
;
1391 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
1392 atomic_set(&tt_global_entry
->orig_list_count
, 0);
1393 spin_lock_init(&tt_global_entry
->list_lock
);
1395 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
1397 batadv_choose_tt
, common
,
1398 &common
->hash_entry
);
1400 if (unlikely(hash_added
!= 0)) {
1401 /* remove the reference for the hash */
1402 batadv_tt_global_entry_free_ref(tt_global_entry
);
1406 common
= &tt_global_entry
->common
;
1407 /* If there is already a global entry, we can use this one for
1409 * But if we are trying to add a temporary client then here are
1410 * two options at this point:
1411 * 1) the global client is not a temporary client: the global
1412 * client has to be left as it is, temporary information
1413 * should never override any already known client state
1414 * 2) the global client is a temporary client: purge the
1415 * originator list and add the new one orig_entry
1417 if (flags
& BATADV_TT_CLIENT_TEMP
) {
1418 if (!(common
->flags
& BATADV_TT_CLIENT_TEMP
))
1420 if (batadv_tt_global_entry_has_orig(tt_global_entry
,
1423 batadv_tt_global_del_orig_list(tt_global_entry
);
1424 goto add_orig_entry
;
1427 /* if the client was temporary added before receiving the first
1428 * OGM announcing it, we have to clear the TEMP flag. Also,
1429 * remove the previous temporary orig node and re-add it
1430 * if required. If the orig entry changed, the new one which
1431 * is a non-temporary entry is preferred.
1433 if (common
->flags
& BATADV_TT_CLIENT_TEMP
) {
1434 batadv_tt_global_del_orig_list(tt_global_entry
);
1435 common
->flags
&= ~BATADV_TT_CLIENT_TEMP
;
1438 /* the change can carry possible "attribute" flags like the
1439 * TT_CLIENT_WIFI, therefore they have to be copied in the
1442 common
->flags
|= flags
;
1444 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1445 * one originator left in the list and we previously received a
1446 * delete + roaming change for this originator.
1448 * We should first delete the old originator before adding the
1451 if (common
->flags
& BATADV_TT_CLIENT_ROAM
) {
1452 batadv_tt_global_del_orig_list(tt_global_entry
);
1453 common
->flags
&= ~BATADV_TT_CLIENT_ROAM
;
1454 tt_global_entry
->roam_at
= 0;
1458 /* add the new orig_entry (if needed) or update it */
1459 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
1461 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1462 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1463 common
->addr
, BATADV_PRINT_VID(common
->vid
),
1468 /* Do not remove multicast addresses from the local hash on
1471 if (is_multicast_ether_addr(tt_addr
))
1474 /* remove address from local hash if present */
1475 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
, vid
,
1476 "global tt received",
1477 flags
& BATADV_TT_CLIENT_ROAM
);
1478 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
1480 if (!(flags
& BATADV_TT_CLIENT_ROAM
))
1481 /* this is a normal global add. Therefore the client is not in a
1482 * roaming state anymore.
1484 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
1487 if (tt_global_entry
)
1488 batadv_tt_global_entry_free_ref(tt_global_entry
);
1490 batadv_tt_local_entry_free_ref(tt_local_entry
);
1495 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1496 * @bat_priv: the bat priv with all the soft interface information
1497 * @tt_global_entry: global translation table entry to be analyzed
1499 * This functon assumes the caller holds rcu_read_lock().
1500 * Returns best originator list entry or NULL on errors.
1502 static struct batadv_tt_orig_list_entry
*
1503 batadv_transtable_best_orig(struct batadv_priv
*bat_priv
,
1504 struct batadv_tt_global_entry
*tt_global_entry
)
1506 struct batadv_neigh_node
*router
, *best_router
= NULL
;
1507 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1508 struct hlist_head
*head
;
1509 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
= NULL
;
1511 head
= &tt_global_entry
->orig_list
;
1512 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1513 router
= batadv_orig_router_get(orig_entry
->orig_node
,
1519 bao
->bat_neigh_cmp(router
, BATADV_IF_DEFAULT
,
1520 best_router
, BATADV_IF_DEFAULT
) <= 0) {
1521 batadv_neigh_node_free_ref(router
);
1525 /* release the refcount for the "old" best */
1527 batadv_neigh_node_free_ref(best_router
);
1529 best_entry
= orig_entry
;
1530 best_router
= router
;
1534 batadv_neigh_node_free_ref(best_router
);
1540 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1541 * for this global entry
1542 * @bat_priv: the bat priv with all the soft interface information
1543 * @tt_global_entry: global translation table entry to be printed
1544 * @seq: debugfs table seq_file struct
1546 * This functon assumes the caller holds rcu_read_lock().
1549 batadv_tt_global_print_entry(struct batadv_priv
*bat_priv
,
1550 struct batadv_tt_global_entry
*tt_global_entry
,
1551 struct seq_file
*seq
)
1553 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
;
1554 struct batadv_tt_common_entry
*tt_common_entry
;
1555 struct batadv_orig_node_vlan
*vlan
;
1556 struct hlist_head
*head
;
1560 tt_common_entry
= &tt_global_entry
->common
;
1561 flags
= tt_common_entry
->flags
;
1563 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
1565 vlan
= batadv_orig_node_vlan_get(best_entry
->orig_node
,
1566 tt_common_entry
->vid
);
1569 " * Cannot retrieve VLAN %d for originator %pM\n",
1570 BATADV_PRINT_VID(tt_common_entry
->vid
),
1571 best_entry
->orig_node
->orig
);
1575 last_ttvn
= atomic_read(&best_entry
->orig_node
->last_ttvn
);
1577 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1578 '*', tt_global_entry
->common
.addr
,
1579 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1580 best_entry
->ttvn
, best_entry
->orig_node
->orig
,
1581 last_ttvn
, vlan
->tt
.crc
,
1582 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1583 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1584 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1585 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1587 batadv_orig_node_vlan_free_ref(vlan
);
1591 head
= &tt_global_entry
->orig_list
;
1593 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1594 if (best_entry
== orig_entry
)
1597 vlan
= batadv_orig_node_vlan_get(orig_entry
->orig_node
,
1598 tt_common_entry
->vid
);
1601 " + Cannot retrieve VLAN %d for originator %pM\n",
1602 BATADV_PRINT_VID(tt_common_entry
->vid
),
1603 orig_entry
->orig_node
->orig
);
1607 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
1609 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1610 '+', tt_global_entry
->common
.addr
,
1611 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1612 orig_entry
->ttvn
, orig_entry
->orig_node
->orig
,
1613 last_ttvn
, vlan
->tt
.crc
,
1614 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1615 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1616 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1617 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1619 batadv_orig_node_vlan_free_ref(vlan
);
1623 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
1625 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1626 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1627 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1628 struct batadv_tt_common_entry
*tt_common_entry
;
1629 struct batadv_tt_global_entry
*tt_global
;
1630 struct batadv_hard_iface
*primary_if
;
1631 struct hlist_head
*head
;
1634 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1639 "Globally announced TT entries received via the mesh %s\n",
1641 seq_printf(seq
, " %-13s %s %s %-15s %s (%-10s) %s\n",
1642 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1645 for (i
= 0; i
< hash
->size
; i
++) {
1646 head
= &hash
->table
[i
];
1649 hlist_for_each_entry_rcu(tt_common_entry
,
1651 tt_global
= container_of(tt_common_entry
,
1652 struct batadv_tt_global_entry
,
1654 batadv_tt_global_print_entry(bat_priv
, tt_global
, seq
);
1660 batadv_hardif_free_ref(primary_if
);
1665 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1666 * @tt_global_entry: the global entry to remove the orig_entry from
1667 * @orig_entry: the orig entry to remove and free
1669 * Remove an orig_entry from its list in the given tt_global_entry and
1670 * free this orig_entry afterwards.
1672 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1676 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry
*tt_global_entry
,
1677 struct batadv_tt_orig_list_entry
*orig_entry
)
1679 lockdep_assert_held(&tt_global_entry
->list_lock
);
1681 batadv_tt_global_size_dec(orig_entry
->orig_node
,
1682 tt_global_entry
->common
.vid
);
1683 atomic_dec(&tt_global_entry
->orig_list_count
);
1684 /* requires holding tt_global_entry->list_lock and orig_entry->list
1685 * being part of a list
1687 hlist_del_rcu(&orig_entry
->list
);
1688 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1691 /* deletes the orig list of a tt_global_entry */
1693 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
1695 struct hlist_head
*head
;
1696 struct hlist_node
*safe
;
1697 struct batadv_tt_orig_list_entry
*orig_entry
;
1699 spin_lock_bh(&tt_global_entry
->list_lock
);
1700 head
= &tt_global_entry
->orig_list
;
1701 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
)
1702 _batadv_tt_global_del_orig_entry(tt_global_entry
, orig_entry
);
1703 spin_unlock_bh(&tt_global_entry
->list_lock
);
1707 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1708 * @bat_priv: the bat priv with all the soft interface information
1709 * @tt_global_entry: the global entry to remove the orig_node from
1710 * @orig_node: the originator announcing the client
1711 * @message: message to append to the log on deletion
1713 * Remove the given orig_node and its according orig_entry from the given
1717 batadv_tt_global_del_orig_node(struct batadv_priv
*bat_priv
,
1718 struct batadv_tt_global_entry
*tt_global_entry
,
1719 struct batadv_orig_node
*orig_node
,
1720 const char *message
)
1722 struct hlist_head
*head
;
1723 struct hlist_node
*safe
;
1724 struct batadv_tt_orig_list_entry
*orig_entry
;
1727 spin_lock_bh(&tt_global_entry
->list_lock
);
1728 head
= &tt_global_entry
->orig_list
;
1729 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
) {
1730 if (orig_entry
->orig_node
== orig_node
) {
1731 vid
= tt_global_entry
->common
.vid
;
1732 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1733 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1735 tt_global_entry
->common
.addr
,
1736 BATADV_PRINT_VID(vid
), message
);
1737 _batadv_tt_global_del_orig_entry(tt_global_entry
,
1741 spin_unlock_bh(&tt_global_entry
->list_lock
);
1744 /* If the client is to be deleted, we check if it is the last origantor entry
1745 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1746 * timer, otherwise we simply remove the originator scheduled for deletion.
1749 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
1750 struct batadv_tt_global_entry
*tt_global_entry
,
1751 struct batadv_orig_node
*orig_node
,
1752 const char *message
)
1754 bool last_entry
= true;
1755 struct hlist_head
*head
;
1756 struct batadv_tt_orig_list_entry
*orig_entry
;
1758 /* no local entry exists, case 1:
1759 * Check if this is the last one or if other entries exist.
1763 head
= &tt_global_entry
->orig_list
;
1764 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1765 if (orig_entry
->orig_node
!= orig_node
) {
1773 /* its the last one, mark for roaming. */
1774 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1775 tt_global_entry
->roam_at
= jiffies
;
1777 /* there is another entry, we can simply delete this
1778 * one and can still use the other one.
1780 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1781 orig_node
, message
);
1785 * batadv_tt_global_del - remove a client from the global table
1786 * @bat_priv: the bat priv with all the soft interface information
1787 * @orig_node: an originator serving this client
1788 * @addr: the mac address of the client
1789 * @vid: VLAN identifier
1790 * @message: a message explaining the reason for deleting the client to print
1791 * for debugging purpose
1792 * @roaming: true if the deletion has been triggered by a roaming event
1794 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
1795 struct batadv_orig_node
*orig_node
,
1796 const unsigned char *addr
, unsigned short vid
,
1797 const char *message
, bool roaming
)
1799 struct batadv_tt_global_entry
*tt_global_entry
;
1800 struct batadv_tt_local_entry
*local_entry
= NULL
;
1802 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
1803 if (!tt_global_entry
)
1807 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1808 orig_node
, message
);
1810 if (hlist_empty(&tt_global_entry
->orig_list
))
1811 batadv_tt_global_free(bat_priv
, tt_global_entry
,
1817 /* if we are deleting a global entry due to a roam
1818 * event, there are two possibilities:
1819 * 1) the client roamed from node A to node B => if there
1820 * is only one originator left for this client, we mark
1821 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1822 * wait for node B to claim it. In case of timeout
1823 * the entry is purged.
1825 * If there are other originators left, we directly delete
1827 * 2) the client roamed to us => we can directly delete
1828 * the global entry, since it is useless now.
1830 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1831 tt_global_entry
->common
.addr
,
1834 /* local entry exists, case 2: client roamed to us. */
1835 batadv_tt_global_del_orig_list(tt_global_entry
);
1836 batadv_tt_global_free(bat_priv
, tt_global_entry
, message
);
1838 /* no local entry exists, case 1: check for roaming */
1839 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1840 orig_node
, message
);
1843 if (tt_global_entry
)
1844 batadv_tt_global_entry_free_ref(tt_global_entry
);
1846 batadv_tt_local_entry_free_ref(local_entry
);
1850 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1851 * given originator matching the provided vid
1852 * @bat_priv: the bat priv with all the soft interface information
1853 * @orig_node: the originator owning the entries to remove
1854 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1856 * @message: debug message to print as "reason"
1858 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1859 struct batadv_orig_node
*orig_node
,
1861 const char *message
)
1863 struct batadv_tt_global_entry
*tt_global
;
1864 struct batadv_tt_common_entry
*tt_common_entry
;
1866 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1867 struct hlist_node
*safe
;
1868 struct hlist_head
*head
;
1869 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1875 for (i
= 0; i
< hash
->size
; i
++) {
1876 head
= &hash
->table
[i
];
1877 list_lock
= &hash
->list_locks
[i
];
1879 spin_lock_bh(list_lock
);
1880 hlist_for_each_entry_safe(tt_common_entry
, safe
,
1882 /* remove only matching entries */
1883 if (match_vid
>= 0 && tt_common_entry
->vid
!= match_vid
)
1886 tt_global
= container_of(tt_common_entry
,
1887 struct batadv_tt_global_entry
,
1890 batadv_tt_global_del_orig_node(bat_priv
, tt_global
,
1891 orig_node
, message
);
1893 if (hlist_empty(&tt_global
->orig_list
)) {
1894 vid
= tt_global
->common
.vid
;
1895 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1896 "Deleting global tt entry %pM (vid: %d): %s\n",
1897 tt_global
->common
.addr
,
1898 BATADV_PRINT_VID(vid
), message
);
1899 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1900 batadv_tt_global_entry_free_ref(tt_global
);
1903 spin_unlock_bh(list_lock
);
1905 clear_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
1908 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1912 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1913 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1915 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1916 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1918 *msg
= "Roaming timeout\n";
1921 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1922 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1924 *msg
= "Temporary client timeout\n";
1930 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1932 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1933 struct hlist_head
*head
;
1934 struct hlist_node
*node_tmp
;
1935 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1938 struct batadv_tt_common_entry
*tt_common
;
1939 struct batadv_tt_global_entry
*tt_global
;
1941 for (i
= 0; i
< hash
->size
; i
++) {
1942 head
= &hash
->table
[i
];
1943 list_lock
= &hash
->list_locks
[i
];
1945 spin_lock_bh(list_lock
);
1946 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
1948 tt_global
= container_of(tt_common
,
1949 struct batadv_tt_global_entry
,
1952 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1955 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1956 "Deleting global tt entry %pM (vid: %d): %s\n",
1957 tt_global
->common
.addr
,
1958 BATADV_PRINT_VID(tt_global
->common
.vid
),
1961 hlist_del_rcu(&tt_common
->hash_entry
);
1963 batadv_tt_global_entry_free_ref(tt_global
);
1965 spin_unlock_bh(list_lock
);
1969 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
1971 struct batadv_hashtable
*hash
;
1972 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1973 struct batadv_tt_common_entry
*tt_common_entry
;
1974 struct batadv_tt_global_entry
*tt_global
;
1975 struct hlist_node
*node_tmp
;
1976 struct hlist_head
*head
;
1979 if (!bat_priv
->tt
.global_hash
)
1982 hash
= bat_priv
->tt
.global_hash
;
1984 for (i
= 0; i
< hash
->size
; i
++) {
1985 head
= &hash
->table
[i
];
1986 list_lock
= &hash
->list_locks
[i
];
1988 spin_lock_bh(list_lock
);
1989 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1991 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1992 tt_global
= container_of(tt_common_entry
,
1993 struct batadv_tt_global_entry
,
1995 batadv_tt_global_entry_free_ref(tt_global
);
1997 spin_unlock_bh(list_lock
);
2000 batadv_hash_destroy(hash
);
2002 bat_priv
->tt
.global_hash
= NULL
;
2006 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
2007 struct batadv_tt_global_entry
*tt_global_entry
)
2011 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
2012 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
2015 /* check if the two clients are marked as isolated */
2016 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
&&
2017 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
)
2024 * batadv_transtable_search - get the mesh destination for a given client
2025 * @bat_priv: the bat priv with all the soft interface information
2026 * @src: mac address of the source client
2027 * @addr: mac address of the destination client
2028 * @vid: VLAN identifier
2030 * Returns a pointer to the originator that was selected as destination in the
2031 * mesh for contacting the client 'addr', NULL otherwise.
2032 * In case of multiple originators serving the same client, the function returns
2033 * the best one (best in terms of metric towards the destination node).
2035 * If the two clients are AP isolated the function returns NULL.
2037 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
2042 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2043 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2044 struct batadv_orig_node
*orig_node
= NULL
;
2045 struct batadv_tt_orig_list_entry
*best_entry
;
2047 if (src
&& batadv_vlan_ap_isola_get(bat_priv
, vid
)) {
2048 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
, vid
);
2049 if (!tt_local_entry
||
2050 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
))
2054 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
2055 if (!tt_global_entry
)
2058 /* check whether the clients should not communicate due to AP
2061 if (tt_local_entry
&&
2062 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2066 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
2067 /* found anything? */
2069 orig_node
= best_entry
->orig_node
;
2070 if (orig_node
&& !atomic_inc_not_zero(&orig_node
->refcount
))
2075 if (tt_global_entry
)
2076 batadv_tt_global_entry_free_ref(tt_global_entry
);
2078 batadv_tt_local_entry_free_ref(tt_local_entry
);
2084 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2085 * to the given orig_node
2086 * @bat_priv: the bat priv with all the soft interface information
2087 * @orig_node: originator for which the CRC should be computed
2088 * @vid: VLAN identifier for which the CRC32 has to be computed
2090 * This function computes the checksum for the global table corresponding to a
2091 * specific originator. In particular, the checksum is computed as follows: For
2092 * each client connected to the originator the CRC32C of the MAC address and the
2093 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2096 * The idea behind is that CRC32C should be used as much as possible in order to
2097 * produce a unique hash of the table, but since the order which is used to feed
2098 * the CRC32C function affects the result and since every node in the network
2099 * probably sorts the clients differently, the hash function cannot be directly
2100 * computed over the entire table. Hence the CRC32C is used only on
2101 * the single client entry, while all the results are then xor'ed together
2102 * because the XOR operation can combine them all while trying to reduce the
2103 * noise as much as possible.
2105 * Returns the checksum of the global table of a given originator.
2107 static u32
batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
2108 struct batadv_orig_node
*orig_node
,
2111 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2112 struct batadv_tt_common_entry
*tt_common
;
2113 struct batadv_tt_global_entry
*tt_global
;
2114 struct hlist_head
*head
;
2115 u32 i
, crc_tmp
, crc
= 0;
2119 for (i
= 0; i
< hash
->size
; i
++) {
2120 head
= &hash
->table
[i
];
2123 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2124 tt_global
= container_of(tt_common
,
2125 struct batadv_tt_global_entry
,
2127 /* compute the CRC only for entries belonging to the
2128 * VLAN identified by the vid passed as parameter
2130 if (tt_common
->vid
!= vid
)
2133 /* Roaming clients are in the global table for
2134 * consistency only. They don't have to be
2135 * taken into account while computing the
2138 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
2140 /* Temporary clients have not been announced yet, so
2141 * they have to be skipped while computing the global
2144 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
2147 /* find out if this global entry is announced by this
2150 if (!batadv_tt_global_entry_has_orig(tt_global
,
2154 /* use network order to read the VID: this ensures that
2155 * every node reads the bytes in the same order.
2157 tmp_vid
= htons(tt_common
->vid
);
2158 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2160 /* compute the CRC on flags that have to be kept in sync
2163 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2164 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2166 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2175 * batadv_tt_local_crc - calculates the checksum of the local table
2176 * @bat_priv: the bat priv with all the soft interface information
2177 * @vid: VLAN identifier for which the CRC32 has to be computed
2179 * For details about the computation, please refer to the documentation for
2180 * batadv_tt_global_crc().
2182 * Returns the checksum of the local table
2184 static u32
batadv_tt_local_crc(struct batadv_priv
*bat_priv
,
2187 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2188 struct batadv_tt_common_entry
*tt_common
;
2189 struct hlist_head
*head
;
2190 u32 i
, crc_tmp
, crc
= 0;
2194 for (i
= 0; i
< hash
->size
; i
++) {
2195 head
= &hash
->table
[i
];
2198 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2199 /* compute the CRC only for entries belonging to the
2200 * VLAN identified by vid
2202 if (tt_common
->vid
!= vid
)
2205 /* not yet committed clients have not to be taken into
2206 * account while computing the CRC
2208 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
2211 /* use network order to read the VID: this ensures that
2212 * every node reads the bytes in the same order.
2214 tmp_vid
= htons(tt_common
->vid
);
2215 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2217 /* compute the CRC on flags that have to be kept in sync
2220 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2221 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2223 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2231 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
2233 struct batadv_tt_req_node
*node
;
2234 struct hlist_node
*safe
;
2236 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2238 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2239 hlist_del_init(&node
->list
);
2243 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2246 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
2247 struct batadv_orig_node
*orig_node
,
2248 const void *tt_buff
,
2251 /* Replace the old buffer only if I received something in the
2252 * last OGM (the OGM could carry no changes)
2254 spin_lock_bh(&orig_node
->tt_buff_lock
);
2255 if (tt_buff_len
> 0) {
2256 kfree(orig_node
->tt_buff
);
2257 orig_node
->tt_buff_len
= 0;
2258 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
2259 if (orig_node
->tt_buff
) {
2260 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
2261 orig_node
->tt_buff_len
= tt_buff_len
;
2264 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2267 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
2269 struct batadv_tt_req_node
*node
;
2270 struct hlist_node
*safe
;
2272 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2273 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2274 if (batadv_has_timed_out(node
->issued_at
,
2275 BATADV_TT_REQUEST_TIMEOUT
)) {
2276 hlist_del_init(&node
->list
);
2280 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2284 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2285 * @bat_priv: the bat priv with all the soft interface information
2286 * @orig_node: orig node this request is being issued for
2288 * Returns the pointer to the new tt_req_node struct if no request
2289 * has already been issued for this orig_node, NULL otherwise.
2291 static struct batadv_tt_req_node
*
2292 batadv_tt_req_node_new(struct batadv_priv
*bat_priv
,
2293 struct batadv_orig_node
*orig_node
)
2295 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
2297 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2298 hlist_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
2299 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
2300 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
2301 BATADV_TT_REQUEST_TIMEOUT
))
2305 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
2309 ether_addr_copy(tt_req_node
->addr
, orig_node
->orig
);
2310 tt_req_node
->issued_at
= jiffies
;
2312 hlist_add_head(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
2314 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2319 * batadv_tt_local_valid - verify that given tt entry is a valid one
2320 * @entry_ptr: to be checked local tt entry
2321 * @data_ptr: not used but definition required to satisfy the callback prototype
2323 * Returns 1 if the entry is a valid, 0 otherwise.
2325 static int batadv_tt_local_valid(const void *entry_ptr
, const void *data_ptr
)
2327 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2329 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
2334 static int batadv_tt_global_valid(const void *entry_ptr
,
2335 const void *data_ptr
)
2337 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2338 const struct batadv_tt_global_entry
*tt_global_entry
;
2339 const struct batadv_orig_node
*orig_node
= data_ptr
;
2341 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
2342 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
2345 tt_global_entry
= container_of(tt_common_entry
,
2346 struct batadv_tt_global_entry
,
2349 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
2353 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2355 * @bat_priv: the bat priv with all the soft interface information
2356 * @hash: hash table containing the tt entries
2357 * @tt_len: expected tvlv tt data buffer length in number of bytes
2358 * @tvlv_buff: pointer to the buffer to fill with the TT data
2359 * @valid_cb: function to filter tt change entries
2360 * @cb_data: data passed to the filter function as argument
2362 static void batadv_tt_tvlv_generate(struct batadv_priv
*bat_priv
,
2363 struct batadv_hashtable
*hash
,
2364 void *tvlv_buff
, u16 tt_len
,
2365 int (*valid_cb
)(const void *, const void *),
2368 struct batadv_tt_common_entry
*tt_common_entry
;
2369 struct batadv_tvlv_tt_change
*tt_change
;
2370 struct hlist_head
*head
;
2371 u16 tt_tot
, tt_num_entries
= 0;
2374 tt_tot
= batadv_tt_entries(tt_len
);
2375 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_buff
;
2378 for (i
= 0; i
< hash
->size
; i
++) {
2379 head
= &hash
->table
[i
];
2381 hlist_for_each_entry_rcu(tt_common_entry
,
2383 if (tt_tot
== tt_num_entries
)
2386 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
2389 ether_addr_copy(tt_change
->addr
, tt_common_entry
->addr
);
2390 tt_change
->flags
= tt_common_entry
->flags
;
2391 tt_change
->vid
= htons(tt_common_entry
->vid
);
2392 memset(tt_change
->reserved
, 0,
2393 sizeof(tt_change
->reserved
));
2403 * batadv_tt_global_check_crc - check if all the CRCs are correct
2404 * @orig_node: originator for which the CRCs have to be checked
2405 * @tt_vlan: pointer to the first tvlv VLAN entry
2406 * @num_vlan: number of tvlv VLAN entries
2408 * Return true if all the received CRCs match the locally stored ones, false
2411 static bool batadv_tt_global_check_crc(struct batadv_orig_node
*orig_node
,
2412 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2415 struct batadv_tvlv_tt_vlan_data
*tt_vlan_tmp
;
2416 struct batadv_orig_node_vlan
*vlan
;
2417 int i
, orig_num_vlan
;
2420 /* check if each received CRC matches the locally stored one */
2421 for (i
= 0; i
< num_vlan
; i
++) {
2422 tt_vlan_tmp
= tt_vlan
+ i
;
2424 /* if orig_node is a backbone node for this VLAN, don't check
2425 * the CRC as we ignore all the global entries over it
2427 if (batadv_bla_is_backbone_gw_orig(orig_node
->bat_priv
,
2429 ntohs(tt_vlan_tmp
->vid
)))
2432 vlan
= batadv_orig_node_vlan_get(orig_node
,
2433 ntohs(tt_vlan_tmp
->vid
));
2438 batadv_orig_node_vlan_free_ref(vlan
);
2440 if (crc
!= ntohl(tt_vlan_tmp
->crc
))
2444 /* check if any excess VLANs exist locally for the originator
2445 * which are not mentioned in the TVLV from the originator.
2449 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
)
2453 if (orig_num_vlan
> num_vlan
)
2460 * batadv_tt_local_update_crc - update all the local CRCs
2461 * @bat_priv: the bat priv with all the soft interface information
2463 static void batadv_tt_local_update_crc(struct batadv_priv
*bat_priv
)
2465 struct batadv_softif_vlan
*vlan
;
2467 /* recompute the global CRC for each VLAN */
2469 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
2470 vlan
->tt
.crc
= batadv_tt_local_crc(bat_priv
, vlan
->vid
);
2476 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2477 * @bat_priv: the bat priv with all the soft interface information
2478 * @orig_node: the orig_node for which the CRCs have to be updated
2480 static void batadv_tt_global_update_crc(struct batadv_priv
*bat_priv
,
2481 struct batadv_orig_node
*orig_node
)
2483 struct batadv_orig_node_vlan
*vlan
;
2486 /* recompute the global CRC for each VLAN */
2488 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
2489 /* if orig_node is a backbone node for this VLAN, don't compute
2490 * the CRC as we ignore all the global entries over it
2492 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
,
2496 crc
= batadv_tt_global_crc(bat_priv
, orig_node
, vlan
->vid
);
2503 * batadv_send_tt_request - send a TT Request message to a given node
2504 * @bat_priv: the bat priv with all the soft interface information
2505 * @dst_orig_node: the destination of the message
2506 * @ttvn: the version number that the source of the message is looking for
2507 * @tt_vlan: pointer to the first tvlv VLAN object to request
2508 * @num_vlan: number of tvlv VLAN entries
2509 * @full_table: ask for the entire translation table if true, while only for the
2510 * last TT diff otherwise
2512 static int batadv_send_tt_request(struct batadv_priv
*bat_priv
,
2513 struct batadv_orig_node
*dst_orig_node
,
2515 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2516 u16 num_vlan
, bool full_table
)
2518 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2519 struct batadv_tt_req_node
*tt_req_node
= NULL
;
2520 struct batadv_tvlv_tt_vlan_data
*tt_vlan_req
;
2521 struct batadv_hard_iface
*primary_if
;
2525 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2529 /* The new tt_req will be issued only if I'm not waiting for a
2530 * reply from the same orig_node yet
2532 tt_req_node
= batadv_tt_req_node_new(bat_priv
, dst_orig_node
);
2536 size
= sizeof(*tvlv_tt_data
) + sizeof(*tt_vlan_req
) * num_vlan
;
2537 tvlv_tt_data
= kzalloc(size
, GFP_ATOMIC
);
2541 tvlv_tt_data
->flags
= BATADV_TT_REQUEST
;
2542 tvlv_tt_data
->ttvn
= ttvn
;
2543 tvlv_tt_data
->num_vlan
= htons(num_vlan
);
2545 /* send all the CRCs within the request. This is needed by intermediate
2546 * nodes to ensure they have the correct table before replying
2548 tt_vlan_req
= (struct batadv_tvlv_tt_vlan_data
*)(tvlv_tt_data
+ 1);
2549 for (i
= 0; i
< num_vlan
; i
++) {
2550 tt_vlan_req
->vid
= tt_vlan
->vid
;
2551 tt_vlan_req
->crc
= tt_vlan
->crc
;
2558 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2560 batadv_dbg(BATADV_DBG_TT
, bat_priv
, "Sending TT_REQUEST to %pM [%c]\n",
2561 dst_orig_node
->orig
, full_table
? 'F' : '.');
2563 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
2564 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2565 dst_orig_node
->orig
, BATADV_TVLV_TT
, 1,
2566 tvlv_tt_data
, size
);
2571 batadv_hardif_free_ref(primary_if
);
2572 if (ret
&& tt_req_node
) {
2573 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2574 /* hlist_del_init() verifies tt_req_node still is in the list */
2575 hlist_del_init(&tt_req_node
->list
);
2576 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2579 kfree(tvlv_tt_data
);
2584 * batadv_send_other_tt_response - send reply to tt request concerning another
2585 * node's translation table
2586 * @bat_priv: the bat priv with all the soft interface information
2587 * @tt_data: tt data containing the tt request information
2588 * @req_src: mac address of tt request sender
2589 * @req_dst: mac address of tt request recipient
2591 * Returns true if tt request reply was sent, false otherwise.
2593 static bool batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
2594 struct batadv_tvlv_tt_data
*tt_data
,
2595 u8
*req_src
, u8
*req_dst
)
2597 struct batadv_orig_node
*req_dst_orig_node
;
2598 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
2599 struct batadv_tvlv_tt_change
*tt_change
;
2600 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2601 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
2602 bool ret
= false, full_table
;
2603 u8 orig_ttvn
, req_ttvn
;
2607 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2608 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2609 req_src
, tt_data
->ttvn
, req_dst
,
2610 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2612 /* Let's get the orig node of the REAL destination */
2613 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_dst
);
2614 if (!req_dst_orig_node
)
2617 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2618 if (!res_dst_orig_node
)
2621 orig_ttvn
= (u8
)atomic_read(&req_dst_orig_node
->last_ttvn
);
2622 req_ttvn
= tt_data
->ttvn
;
2624 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
2625 /* this node doesn't have the requested data */
2626 if (orig_ttvn
!= req_ttvn
||
2627 !batadv_tt_global_check_crc(req_dst_orig_node
, tt_vlan
,
2628 ntohs(tt_data
->num_vlan
)))
2631 /* If the full table has been explicitly requested */
2632 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
||
2633 !req_dst_orig_node
->tt_buff
)
2638 /* TT fragmentation hasn't been implemented yet, so send as many
2639 * TT entries fit a single packet as possible only
2642 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
2643 tt_len
= req_dst_orig_node
->tt_buff_len
;
2645 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2652 /* Copy the last orig_node's OGM buffer */
2653 memcpy(tt_change
, req_dst_orig_node
->tt_buff
,
2654 req_dst_orig_node
->tt_buff_len
);
2655 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2657 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2658 * in the initial part
2661 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2668 /* fill the rest of the tvlv with the real TT entries */
2669 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.global_hash
,
2671 batadv_tt_global_valid
,
2675 /* Don't send the response, if larger than fragmented packet. */
2676 tt_len
= sizeof(struct batadv_unicast_tvlv_packet
) + tvlv_len
;
2677 if (tt_len
> atomic_read(&bat_priv
->packet_size_max
)) {
2678 net_ratelimited_function(batadv_info
, bat_priv
->soft_iface
,
2679 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2680 res_dst_orig_node
->orig
);
2684 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2685 tvlv_tt_data
->ttvn
= req_ttvn
;
2688 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2690 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2691 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2692 res_dst_orig_node
->orig
, req_dst_orig_node
->orig
,
2693 full_table
? 'F' : '.', req_ttvn
);
2695 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2697 batadv_tvlv_unicast_send(bat_priv
, req_dst_orig_node
->orig
,
2698 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2705 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2708 if (res_dst_orig_node
)
2709 batadv_orig_node_free_ref(res_dst_orig_node
);
2710 if (req_dst_orig_node
)
2711 batadv_orig_node_free_ref(req_dst_orig_node
);
2712 kfree(tvlv_tt_data
);
2717 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2719 * @bat_priv: the bat priv with all the soft interface information
2720 * @tt_data: tt data containing the tt request information
2721 * @req_src: mac address of tt request sender
2723 * Returns true if tt request reply was sent, false otherwise.
2725 static bool batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
2726 struct batadv_tvlv_tt_data
*tt_data
,
2729 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2730 struct batadv_hard_iface
*primary_if
= NULL
;
2731 struct batadv_tvlv_tt_change
*tt_change
;
2732 struct batadv_orig_node
*orig_node
;
2733 u8 my_ttvn
, req_ttvn
;
2738 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2739 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2740 req_src
, tt_data
->ttvn
,
2741 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2743 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
2745 my_ttvn
= (u8
)atomic_read(&bat_priv
->tt
.vn
);
2746 req_ttvn
= tt_data
->ttvn
;
2748 orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2752 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2756 /* If the full table has been explicitly requested or the gap
2757 * is too big send the whole local translation table
2759 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
2760 !bat_priv
->tt
.last_changeset
)
2765 /* TT fragmentation hasn't been implemented yet, so send as many
2766 * TT entries fit a single packet as possible only
2769 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
2771 tt_len
= bat_priv
->tt
.last_changeset_len
;
2772 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2779 /* Copy the last orig_node's OGM buffer */
2780 memcpy(tt_change
, bat_priv
->tt
.last_changeset
,
2781 bat_priv
->tt
.last_changeset_len
);
2782 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2784 req_ttvn
= (u8
)atomic_read(&bat_priv
->tt
.vn
);
2786 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2787 * in the initial part
2790 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2797 /* fill the rest of the tvlv with the real TT entries */
2798 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.local_hash
,
2800 batadv_tt_local_valid
, NULL
);
2803 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2804 tvlv_tt_data
->ttvn
= req_ttvn
;
2807 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2809 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2810 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2811 orig_node
->orig
, full_table
? 'F' : '.', req_ttvn
);
2813 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2815 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2816 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2822 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2824 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
2826 batadv_orig_node_free_ref(orig_node
);
2828 batadv_hardif_free_ref(primary_if
);
2829 kfree(tvlv_tt_data
);
2830 /* The packet was for this host, so it doesn't need to be re-routed */
2835 * batadv_send_tt_response - send reply to tt request
2836 * @bat_priv: the bat priv with all the soft interface information
2837 * @tt_data: tt data containing the tt request information
2838 * @req_src: mac address of tt request sender
2839 * @req_dst: mac address of tt request recipient
2841 * Returns true if tt request reply was sent, false otherwise.
2843 static bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
2844 struct batadv_tvlv_tt_data
*tt_data
,
2845 u8
*req_src
, u8
*req_dst
)
2847 if (batadv_is_my_mac(bat_priv
, req_dst
))
2848 return batadv_send_my_tt_response(bat_priv
, tt_data
, req_src
);
2849 return batadv_send_other_tt_response(bat_priv
, tt_data
, req_src
,
2853 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2854 struct batadv_orig_node
*orig_node
,
2855 struct batadv_tvlv_tt_change
*tt_change
,
2856 u16 tt_num_changes
, u8 ttvn
)
2861 for (i
= 0; i
< tt_num_changes
; i
++) {
2862 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
2863 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
2864 batadv_tt_global_del(bat_priv
, orig_node
,
2865 (tt_change
+ i
)->addr
,
2866 ntohs((tt_change
+ i
)->vid
),
2867 "tt removed by changes",
2870 if (!batadv_tt_global_add(bat_priv
, orig_node
,
2871 (tt_change
+ i
)->addr
,
2872 ntohs((tt_change
+ i
)->vid
),
2873 (tt_change
+ i
)->flags
, ttvn
))
2874 /* In case of problem while storing a
2875 * global_entry, we stop the updating
2876 * procedure without committing the
2877 * ttvn change. This will avoid to send
2878 * corrupted data on tt_request
2883 set_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
2886 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
2887 struct batadv_tvlv_tt_change
*tt_change
,
2888 u8 ttvn
, u8
*resp_src
,
2891 struct batadv_orig_node
*orig_node
;
2893 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2897 /* Purge the old table first.. */
2898 batadv_tt_global_del_orig(bat_priv
, orig_node
, -1,
2899 "Received full table");
2901 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
, num_entries
,
2904 spin_lock_bh(&orig_node
->tt_buff_lock
);
2905 kfree(orig_node
->tt_buff
);
2906 orig_node
->tt_buff_len
= 0;
2907 orig_node
->tt_buff
= NULL
;
2908 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2910 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2914 batadv_orig_node_free_ref(orig_node
);
2917 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2918 struct batadv_orig_node
*orig_node
,
2919 u16 tt_num_changes
, u8 ttvn
,
2920 struct batadv_tvlv_tt_change
*tt_change
)
2922 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
2923 tt_num_changes
, ttvn
);
2925 batadv_tt_save_orig_buffer(bat_priv
, orig_node
, tt_change
,
2926 batadv_tt_len(tt_num_changes
));
2927 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2931 * batadv_is_my_client - check if a client is served by the local node
2932 * @bat_priv: the bat priv with all the soft interface information
2933 * @addr: the mac address of the client to check
2934 * @vid: VLAN identifier
2936 * Returns true if the client is served by this node, false otherwise.
2938 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const u8
*addr
,
2941 struct batadv_tt_local_entry
*tt_local_entry
;
2944 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
2945 if (!tt_local_entry
)
2947 /* Check if the client has been logically deleted (but is kept for
2948 * consistency purpose)
2950 if ((tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
) ||
2951 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
2956 batadv_tt_local_entry_free_ref(tt_local_entry
);
2961 * batadv_handle_tt_response - process incoming tt reply
2962 * @bat_priv: the bat priv with all the soft interface information
2963 * @tt_data: tt data containing the tt request information
2964 * @resp_src: mac address of tt reply sender
2965 * @num_entries: number of tt change entries appended to the tt data
2967 static void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
2968 struct batadv_tvlv_tt_data
*tt_data
,
2969 u8
*resp_src
, u16 num_entries
)
2971 struct batadv_tt_req_node
*node
;
2972 struct hlist_node
*safe
;
2973 struct batadv_orig_node
*orig_node
= NULL
;
2974 struct batadv_tvlv_tt_change
*tt_change
;
2975 u8
*tvlv_ptr
= (u8
*)tt_data
;
2978 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2979 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2980 resp_src
, tt_data
->ttvn
, num_entries
,
2981 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2983 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2987 spin_lock_bh(&orig_node
->tt_lock
);
2989 change_offset
= sizeof(struct batadv_tvlv_tt_vlan_data
);
2990 change_offset
*= ntohs(tt_data
->num_vlan
);
2991 change_offset
+= sizeof(*tt_data
);
2992 tvlv_ptr
+= change_offset
;
2994 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_ptr
;
2995 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
) {
2996 batadv_tt_fill_gtable(bat_priv
, tt_change
, tt_data
->ttvn
,
2997 resp_src
, num_entries
);
2999 batadv_tt_update_changes(bat_priv
, orig_node
, num_entries
,
3000 tt_data
->ttvn
, tt_change
);
3003 /* Recalculate the CRC for this orig_node and store it */
3004 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3006 spin_unlock_bh(&orig_node
->tt_lock
);
3008 /* Delete the tt_req_node from pending tt_requests list */
3009 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
3010 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
3011 if (!batadv_compare_eth(node
->addr
, resp_src
))
3013 hlist_del_init(&node
->list
);
3017 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
3020 batadv_orig_node_free_ref(orig_node
);
3023 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
3025 struct batadv_tt_roam_node
*node
, *safe
;
3027 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3029 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
3030 list_del(&node
->list
);
3034 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3037 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
3039 struct batadv_tt_roam_node
*node
, *safe
;
3041 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3042 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
3043 if (!batadv_has_timed_out(node
->first_time
,
3044 BATADV_ROAMING_MAX_TIME
))
3047 list_del(&node
->list
);
3050 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3053 /* This function checks whether the client already reached the
3054 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3057 * returns true if the ROAMING_ADV can be sent, false otherwise
3059 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
, u8
*client
)
3061 struct batadv_tt_roam_node
*tt_roam_node
;
3064 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3065 /* The new tt_req will be issued only if I'm not waiting for a
3066 * reply from the same orig_node yet
3068 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
3069 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
3072 if (batadv_has_timed_out(tt_roam_node
->first_time
,
3073 BATADV_ROAMING_MAX_TIME
))
3076 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
3077 /* Sorry, you roamed too many times! */
3084 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
3088 tt_roam_node
->first_time
= jiffies
;
3089 atomic_set(&tt_roam_node
->counter
,
3090 BATADV_ROAMING_MAX_COUNT
- 1);
3091 ether_addr_copy(tt_roam_node
->addr
, client
);
3093 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
3098 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3103 * batadv_send_roam_adv - send a roaming advertisement message
3104 * @bat_priv: the bat priv with all the soft interface information
3105 * @client: mac address of the roaming client
3106 * @vid: VLAN identifier
3107 * @orig_node: message destination
3109 * Send a ROAMING_ADV message to the node which was previously serving this
3110 * client. This is done to inform the node that from now on all traffic destined
3111 * for this particular roamed client has to be forwarded to the sender of the
3114 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, u8
*client
,
3116 struct batadv_orig_node
*orig_node
)
3118 struct batadv_hard_iface
*primary_if
;
3119 struct batadv_tvlv_roam_adv tvlv_roam
;
3121 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3125 /* before going on we have to check whether the client has
3126 * already roamed to us too many times
3128 if (!batadv_tt_check_roam_count(bat_priv
, client
))
3131 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3132 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3133 orig_node
->orig
, client
, BATADV_PRINT_VID(vid
));
3135 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
3137 memcpy(tvlv_roam
.client
, client
, sizeof(tvlv_roam
.client
));
3138 tvlv_roam
.vid
= htons(vid
);
3140 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3141 orig_node
->orig
, BATADV_TVLV_ROAM
, 1,
3142 &tvlv_roam
, sizeof(tvlv_roam
));
3146 batadv_hardif_free_ref(primary_if
);
3149 static void batadv_tt_purge(struct work_struct
*work
)
3151 struct delayed_work
*delayed_work
;
3152 struct batadv_priv_tt
*priv_tt
;
3153 struct batadv_priv
*bat_priv
;
3155 delayed_work
= container_of(work
, struct delayed_work
, work
);
3156 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
3157 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
3159 batadv_tt_local_purge(bat_priv
, BATADV_TT_LOCAL_TIMEOUT
);
3160 batadv_tt_global_purge(bat_priv
);
3161 batadv_tt_req_purge(bat_priv
);
3162 batadv_tt_roam_purge(bat_priv
);
3164 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3165 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3168 void batadv_tt_free(struct batadv_priv
*bat_priv
)
3170 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3171 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3173 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
3175 batadv_tt_local_table_free(bat_priv
);
3176 batadv_tt_global_table_free(bat_priv
);
3177 batadv_tt_req_list_free(bat_priv
);
3178 batadv_tt_changes_list_free(bat_priv
);
3179 batadv_tt_roam_list_free(bat_priv
);
3181 kfree(bat_priv
->tt
.last_changeset
);
3185 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3186 * table and possibly count them in the TT size
3187 * @bat_priv: the bat priv with all the soft interface information
3188 * @flags: the flag to switch
3189 * @enable: whether to set or unset the flag
3190 * @count: whether to increase the TT size by the number of changed entries
3192 static void batadv_tt_local_set_flags(struct batadv_priv
*bat_priv
, u16 flags
,
3193 bool enable
, bool count
)
3195 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3196 struct batadv_tt_common_entry
*tt_common_entry
;
3197 u16 changed_num
= 0;
3198 struct hlist_head
*head
;
3204 for (i
= 0; i
< hash
->size
; i
++) {
3205 head
= &hash
->table
[i
];
3208 hlist_for_each_entry_rcu(tt_common_entry
,
3211 if ((tt_common_entry
->flags
& flags
) == flags
)
3213 tt_common_entry
->flags
|= flags
;
3215 if (!(tt_common_entry
->flags
& flags
))
3217 tt_common_entry
->flags
&= ~flags
;
3224 batadv_tt_local_size_inc(bat_priv
,
3225 tt_common_entry
->vid
);
3231 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3232 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
3234 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3235 struct batadv_tt_common_entry
*tt_common
;
3236 struct batadv_tt_local_entry
*tt_local
;
3237 struct batadv_softif_vlan
*vlan
;
3238 struct hlist_node
*node_tmp
;
3239 struct hlist_head
*head
;
3240 spinlock_t
*list_lock
; /* protects write access to the hash lists */
3246 for (i
= 0; i
< hash
->size
; i
++) {
3247 head
= &hash
->table
[i
];
3248 list_lock
= &hash
->list_locks
[i
];
3250 spin_lock_bh(list_lock
);
3251 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
3253 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
3256 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3257 "Deleting local tt entry (%pM, vid: %d): pending\n",
3259 BATADV_PRINT_VID(tt_common
->vid
));
3261 batadv_tt_local_size_dec(bat_priv
, tt_common
->vid
);
3262 hlist_del_rcu(&tt_common
->hash_entry
);
3263 tt_local
= container_of(tt_common
,
3264 struct batadv_tt_local_entry
,
3267 /* decrease the reference held for this vlan */
3268 vlan
= batadv_softif_vlan_get(bat_priv
, tt_common
->vid
);
3270 batadv_softif_vlan_free_ref(vlan
);
3271 batadv_softif_vlan_free_ref(vlan
);
3274 batadv_tt_local_entry_free_ref(tt_local
);
3276 spin_unlock_bh(list_lock
);
3281 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3282 * which have been queued in the time since the last commit
3283 * @bat_priv: the bat priv with all the soft interface information
3285 * Caller must hold tt->commit_lock.
3287 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv
*bat_priv
)
3289 lockdep_assert_held(&bat_priv
->tt
.commit_lock
);
3291 /* Update multicast addresses in local translation table */
3292 batadv_mcast_mla_update(bat_priv
);
3294 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1) {
3295 if (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))
3296 batadv_tt_tvlv_container_update(bat_priv
);
3300 batadv_tt_local_set_flags(bat_priv
, BATADV_TT_CLIENT_NEW
, false, true);
3302 batadv_tt_local_purge_pending_clients(bat_priv
);
3303 batadv_tt_local_update_crc(bat_priv
);
3305 /* Increment the TTVN only once per OGM interval */
3306 atomic_inc(&bat_priv
->tt
.vn
);
3307 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3308 "Local changes committed, updating to ttvn %u\n",
3309 (u8
)atomic_read(&bat_priv
->tt
.vn
));
3311 /* reset the sending counter */
3312 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
3313 batadv_tt_tvlv_container_update(bat_priv
);
3317 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3318 * have been queued in the time since the last commit
3319 * @bat_priv: the bat priv with all the soft interface information
3321 void batadv_tt_local_commit_changes(struct batadv_priv
*bat_priv
)
3323 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3324 batadv_tt_local_commit_changes_nolock(bat_priv
);
3325 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3328 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, u8
*src
, u8
*dst
,
3331 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
3332 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
3333 struct batadv_softif_vlan
*vlan
;
3336 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
3340 if (!atomic_read(&vlan
->ap_isolation
))
3343 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
, vid
);
3344 if (!tt_local_entry
)
3347 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
, vid
);
3348 if (!tt_global_entry
)
3351 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
3357 batadv_softif_vlan_free_ref(vlan
);
3358 if (tt_global_entry
)
3359 batadv_tt_global_entry_free_ref(tt_global_entry
);
3361 batadv_tt_local_entry_free_ref(tt_local_entry
);
3366 * batadv_tt_update_orig - update global translation table with new tt
3367 * information received via ogms
3368 * @bat_priv: the bat priv with all the soft interface information
3369 * @orig: the orig_node of the ogm
3370 * @tt_vlan: pointer to the first tvlv VLAN entry
3371 * @tt_num_vlan: number of tvlv VLAN entries
3372 * @tt_change: pointer to the first entry in the TT buffer
3373 * @tt_num_changes: number of tt changes inside the tt buffer
3374 * @ttvn: translation table version number of this changeset
3376 static void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
3377 struct batadv_orig_node
*orig_node
,
3378 const void *tt_buff
, u16 tt_num_vlan
,
3379 struct batadv_tvlv_tt_change
*tt_change
,
3380 u16 tt_num_changes
, u8 ttvn
)
3382 u8 orig_ttvn
= (u8
)atomic_read(&orig_node
->last_ttvn
);
3383 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3384 bool full_table
= true;
3387 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)tt_buff
;
3388 has_tt_init
= test_bit(BATADV_ORIG_CAPA_HAS_TT
,
3389 &orig_node
->capa_initialized
);
3391 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3392 * increased by one -> we can apply the attached changes
3394 if ((!has_tt_init
&& ttvn
== 1) || ttvn
- orig_ttvn
== 1) {
3395 /* the OGM could not contain the changes due to their size or
3396 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3398 * In this case send a tt request
3400 if (!tt_num_changes
) {
3405 spin_lock_bh(&orig_node
->tt_lock
);
3407 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
3410 /* Even if we received the precomputed crc with the OGM, we
3411 * prefer to recompute it to spot any possible inconsistency
3412 * in the global table
3414 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3416 spin_unlock_bh(&orig_node
->tt_lock
);
3418 /* The ttvn alone is not enough to guarantee consistency
3419 * because a single value could represent different states
3420 * (due to the wrap around). Thus a node has to check whether
3421 * the resulting table (after applying the changes) is still
3422 * consistent or not. E.g. a node could disconnect while its
3423 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3424 * checking the CRC value is mandatory to detect the
3427 if (!batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3431 /* if we missed more than one change or our tables are not
3432 * in sync anymore -> request fresh tt data
3434 if (!has_tt_init
|| ttvn
!= orig_ttvn
||
3435 !batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3438 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3439 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3440 orig_node
->orig
, ttvn
, orig_ttvn
,
3442 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
3443 tt_vlan
, tt_num_vlan
,
3451 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3452 * @bat_priv: the bat priv with all the soft interface information
3453 * @addr: the mac address of the client to check
3454 * @vid: VLAN identifier
3456 * Returns true if we know that the client has moved from its old originator
3457 * to another one. This entry is still kept for consistency purposes and will be
3458 * deleted later by a DEL or because of timeout
3460 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
3461 u8
*addr
, unsigned short vid
)
3463 struct batadv_tt_global_entry
*tt_global_entry
;
3466 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3467 if (!tt_global_entry
)
3470 ret
= tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3471 batadv_tt_global_entry_free_ref(tt_global_entry
);
3477 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3478 * @bat_priv: the bat priv with all the soft interface information
3479 * @addr: the mac address of the local client to query
3480 * @vid: VLAN identifier
3482 * Returns true if the local client is known to be roaming (it is not served by
3483 * this node anymore) or not. If yes, the client is still present in the table
3484 * to keep the latter consistent with the node TTVN
3486 bool batadv_tt_local_client_is_roaming(struct batadv_priv
*bat_priv
,
3487 u8
*addr
, unsigned short vid
)
3489 struct batadv_tt_local_entry
*tt_local_entry
;
3492 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
3493 if (!tt_local_entry
)
3496 ret
= tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3497 batadv_tt_local_entry_free_ref(tt_local_entry
);
3502 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
3503 struct batadv_orig_node
*orig_node
,
3504 const unsigned char *addr
,
3509 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
, vid
,
3510 BATADV_TT_CLIENT_TEMP
,
3511 atomic_read(&orig_node
->last_ttvn
)))
3514 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3515 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3516 addr
, BATADV_PRINT_VID(vid
), orig_node
->orig
);
3523 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3524 * maximum packet size that can be transported through the mesh
3525 * @soft_iface: netdev struct of the mesh interface
3527 * Remove entries older than 'timeout' and half timeout if more entries need
3530 void batadv_tt_local_resize_to_mtu(struct net_device
*soft_iface
)
3532 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
3533 int packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
3534 int table_size
, timeout
= BATADV_TT_LOCAL_TIMEOUT
/ 2;
3535 bool reduced
= false;
3537 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3540 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
3541 if (packet_size_max
>= table_size
)
3544 batadv_tt_local_purge(bat_priv
, timeout
);
3545 batadv_tt_local_purge_pending_clients(bat_priv
);
3549 net_ratelimited_function(batadv_info
, soft_iface
,
3550 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3554 /* commit these changes immediately, to avoid synchronization problem
3558 batadv_tt_local_commit_changes_nolock(bat_priv
);
3560 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3564 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3565 * @bat_priv: the bat priv with all the soft interface information
3566 * @orig: the orig_node of the ogm
3567 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3568 * @tvlv_value: tvlv buffer containing the gateway data
3569 * @tvlv_value_len: tvlv buffer length
3571 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
3572 struct batadv_orig_node
*orig
,
3573 u8 flags
, void *tvlv_value
,
3576 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3577 struct batadv_tvlv_tt_change
*tt_change
;
3578 struct batadv_tvlv_tt_data
*tt_data
;
3579 u16 num_entries
, num_vlan
;
3581 if (tvlv_value_len
< sizeof(*tt_data
))
3584 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3585 tvlv_value_len
-= sizeof(*tt_data
);
3587 num_vlan
= ntohs(tt_data
->num_vlan
);
3589 if (tvlv_value_len
< sizeof(*tt_vlan
) * num_vlan
)
3592 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
3593 tt_change
= (struct batadv_tvlv_tt_change
*)(tt_vlan
+ num_vlan
);
3594 tvlv_value_len
-= sizeof(*tt_vlan
) * num_vlan
;
3596 num_entries
= batadv_tt_entries(tvlv_value_len
);
3598 batadv_tt_update_orig(bat_priv
, orig
, tt_vlan
, num_vlan
, tt_change
,
3599 num_entries
, tt_data
->ttvn
);
3603 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3605 * @bat_priv: the bat priv with all the soft interface information
3606 * @src: mac address of tt tvlv sender
3607 * @dst: mac address of tt tvlv recipient
3608 * @tvlv_value: tvlv buffer containing the tt data
3609 * @tvlv_value_len: tvlv buffer length
3611 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3614 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3619 struct batadv_tvlv_tt_data
*tt_data
;
3620 u16 tt_vlan_len
, tt_num_entries
;
3624 if (tvlv_value_len
< sizeof(*tt_data
))
3625 return NET_RX_SUCCESS
;
3627 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3628 tvlv_value_len
-= sizeof(*tt_data
);
3630 tt_vlan_len
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3631 tt_vlan_len
*= ntohs(tt_data
->num_vlan
);
3633 if (tvlv_value_len
< tt_vlan_len
)
3634 return NET_RX_SUCCESS
;
3636 tvlv_value_len
-= tt_vlan_len
;
3637 tt_num_entries
= batadv_tt_entries(tvlv_value_len
);
3639 switch (tt_data
->flags
& BATADV_TT_DATA_TYPE_MASK
) {
3640 case BATADV_TT_REQUEST
:
3641 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
3643 /* If this node cannot provide a TT response the tt_request is
3646 ret
= batadv_send_tt_response(bat_priv
, tt_data
, src
, dst
);
3648 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3653 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3654 "Routing TT_REQUEST to %pM [%c]\n",
3656 /* tvlv API will re-route the packet */
3660 case BATADV_TT_RESPONSE
:
3661 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
3663 if (batadv_is_my_mac(bat_priv
, dst
)) {
3664 batadv_handle_tt_response(bat_priv
, tt_data
,
3665 src
, tt_num_entries
);
3666 return NET_RX_SUCCESS
;
3669 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3674 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3675 "Routing TT_RESPONSE to %pM [%c]\n", dst
, tt_flag
);
3677 /* tvlv API will re-route the packet */
3681 return NET_RX_SUCCESS
;
3685 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3686 * @bat_priv: the bat priv with all the soft interface information
3687 * @src: mac address of tt tvlv sender
3688 * @dst: mac address of tt tvlv recipient
3689 * @tvlv_value: tvlv buffer containing the tt data
3690 * @tvlv_value_len: tvlv buffer length
3692 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3695 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3700 struct batadv_tvlv_roam_adv
*roaming_adv
;
3701 struct batadv_orig_node
*orig_node
= NULL
;
3703 /* If this node is not the intended recipient of the
3704 * roaming advertisement the packet is forwarded
3705 * (the tvlv API will re-route the packet).
3707 if (!batadv_is_my_mac(bat_priv
, dst
))
3710 if (tvlv_value_len
< sizeof(*roaming_adv
))
3713 orig_node
= batadv_orig_hash_find(bat_priv
, src
);
3717 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
3718 roaming_adv
= (struct batadv_tvlv_roam_adv
*)tvlv_value
;
3720 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3721 "Received ROAMING_ADV from %pM (client %pM)\n",
3722 src
, roaming_adv
->client
);
3724 batadv_tt_global_add(bat_priv
, orig_node
, roaming_adv
->client
,
3725 ntohs(roaming_adv
->vid
), BATADV_TT_CLIENT_ROAM
,
3726 atomic_read(&orig_node
->last_ttvn
) + 1);
3730 batadv_orig_node_free_ref(orig_node
);
3731 return NET_RX_SUCCESS
;
3735 * batadv_tt_init - initialise the translation table internals
3736 * @bat_priv: the bat priv with all the soft interface information
3738 * Return 0 on success or negative error number in case of failure.
3740 int batadv_tt_init(struct batadv_priv
*bat_priv
)
3744 /* synchronized flags must be remote */
3745 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK
& BATADV_TT_REMOTE_MASK
));
3747 ret
= batadv_tt_local_init(bat_priv
);
3751 ret
= batadv_tt_global_init(bat_priv
);
3755 batadv_tvlv_handler_register(bat_priv
, batadv_tt_tvlv_ogm_handler_v1
,
3756 batadv_tt_tvlv_unicast_handler_v1
,
3757 BATADV_TVLV_TT
, 1, BATADV_NO_FLAGS
);
3759 batadv_tvlv_handler_register(bat_priv
, NULL
,
3760 batadv_roam_tvlv_unicast_handler_v1
,
3761 BATADV_TVLV_ROAM
, 1, BATADV_NO_FLAGS
);
3763 INIT_DELAYED_WORK(&bat_priv
->tt
.work
, batadv_tt_purge
);
3764 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3765 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3771 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3772 * @bat_priv: the bat priv with all the soft interface information
3773 * @addr: the mac address of the client
3774 * @vid: the identifier of the VLAN where this client is connected
3776 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3779 bool batadv_tt_global_is_isolated(struct batadv_priv
*bat_priv
,
3780 const u8
*addr
, unsigned short vid
)
3782 struct batadv_tt_global_entry
*tt
;
3785 tt
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3789 ret
= tt
->common
.flags
& BATADV_TT_CLIENT_ISOLA
;
3791 batadv_tt_global_entry_free_ref(tt
);