1 /* Copyright (C) 2007-2016 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/kref.h>
35 #include <linux/list.h>
36 #include <linux/lockdep.h>
37 #include <linux/netdevice.h>
38 #include <linux/rculist.h>
39 #include <linux/rcupdate.h>
40 #include <linux/seq_file.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/stddef.h>
44 #include <linux/string.h>
45 #include <linux/workqueue.h>
48 #include "bridge_loop_avoidance.h"
49 #include "hard-interface.h"
52 #include "multicast.h"
53 #include "originator.h"
55 #include "soft-interface.h"
59 static struct lock_class_key batadv_tt_local_hash_lock_class_key
;
60 static struct lock_class_key batadv_tt_global_hash_lock_class_key
;
62 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, u8
*client
,
64 struct batadv_orig_node
*orig_node
);
65 static void batadv_tt_purge(struct work_struct
*work
);
67 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
);
68 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
69 struct batadv_orig_node
*orig_node
,
70 const unsigned char *addr
,
71 unsigned short vid
, const char *message
,
75 * batadv_compare_tt - check if two TT entries are the same
76 * @node: the list element pointer of the first TT entry
77 * @data2: pointer to the tt_common_entry of the second TT entry
79 * Compare the MAC address and the VLAN ID of the two TT entries and check if
80 * they are the same TT client.
81 * Return: true if the two TT clients are the same, false otherwise
83 static bool batadv_compare_tt(const struct hlist_node
*node
, const void *data2
)
85 const void *data1
= container_of(node
, struct batadv_tt_common_entry
,
87 const struct batadv_tt_common_entry
*tt1
= data1
;
88 const struct batadv_tt_common_entry
*tt2
= data2
;
90 return (tt1
->vid
== tt2
->vid
) && batadv_compare_eth(data1
, data2
);
94 * batadv_choose_tt - return the index of the tt entry in the hash table
95 * @data: pointer to the tt_common_entry object to map
96 * @size: the size of the hash table
98 * Return: the hash index where the object represented by 'data' should be
101 static inline u32
batadv_choose_tt(const void *data
, u32 size
)
103 struct batadv_tt_common_entry
*tt
;
106 tt
= (struct batadv_tt_common_entry
*)data
;
107 hash
= jhash(&tt
->addr
, ETH_ALEN
, hash
);
108 hash
= jhash(&tt
->vid
, sizeof(tt
->vid
), hash
);
114 * batadv_tt_hash_find - look for a client in the given hash table
115 * @hash: the hash table to search
116 * @addr: the mac address of the client to look for
117 * @vid: VLAN identifier
119 * Return: a pointer to the tt_common struct belonging to the searched client if
120 * found, NULL otherwise.
122 static struct batadv_tt_common_entry
*
123 batadv_tt_hash_find(struct batadv_hashtable
*hash
, const u8
*addr
,
126 struct hlist_head
*head
;
127 struct batadv_tt_common_entry to_search
, *tt
, *tt_tmp
= NULL
;
133 ether_addr_copy(to_search
.addr
, addr
);
136 index
= batadv_choose_tt(&to_search
, hash
->size
);
137 head
= &hash
->table
[index
];
140 hlist_for_each_entry_rcu(tt
, head
, hash_entry
) {
141 if (!batadv_compare_eth(tt
, addr
))
147 if (!kref_get_unless_zero(&tt
->refcount
))
159 * batadv_tt_local_hash_find - search the local table for a given client
160 * @bat_priv: the bat priv with all the soft interface information
161 * @addr: the mac address of the client to look for
162 * @vid: VLAN identifier
164 * Return: a pointer to the corresponding tt_local_entry struct if the client is
165 * found, NULL otherwise.
167 static struct batadv_tt_local_entry
*
168 batadv_tt_local_hash_find(struct batadv_priv
*bat_priv
, const u8
*addr
,
171 struct batadv_tt_common_entry
*tt_common_entry
;
172 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
174 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.local_hash
, addr
,
177 tt_local_entry
= container_of(tt_common_entry
,
178 struct batadv_tt_local_entry
,
180 return tt_local_entry
;
184 * batadv_tt_global_hash_find - search the global table for a given client
185 * @bat_priv: the bat priv with all the soft interface information
186 * @addr: the mac address of the client to look for
187 * @vid: VLAN identifier
189 * Return: a pointer to the corresponding tt_global_entry struct if the client
190 * is found, NULL otherwise.
192 static struct batadv_tt_global_entry
*
193 batadv_tt_global_hash_find(struct batadv_priv
*bat_priv
, const u8
*addr
,
196 struct batadv_tt_common_entry
*tt_common_entry
;
197 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
199 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.global_hash
, addr
,
202 tt_global_entry
= container_of(tt_common_entry
,
203 struct batadv_tt_global_entry
,
205 return tt_global_entry
;
209 * batadv_tt_local_entry_release - release tt_local_entry from lists and queue
210 * for free after rcu grace period
211 * @ref: kref pointer of the nc_node
213 static void batadv_tt_local_entry_release(struct kref
*ref
)
215 struct batadv_tt_local_entry
*tt_local_entry
;
217 tt_local_entry
= container_of(ref
, struct batadv_tt_local_entry
,
220 batadv_softif_vlan_put(tt_local_entry
->vlan
);
222 kfree_rcu(tt_local_entry
, common
.rcu
);
226 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
227 * possibly release it
228 * @tt_local_entry: tt_local_entry to be free'd
231 batadv_tt_local_entry_put(struct batadv_tt_local_entry
*tt_local_entry
)
233 kref_put(&tt_local_entry
->common
.refcount
,
234 batadv_tt_local_entry_release
);
238 * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
239 * for free after rcu grace period
240 * @ref: kref pointer of the nc_node
242 static void batadv_tt_global_entry_release(struct kref
*ref
)
244 struct batadv_tt_global_entry
*tt_global_entry
;
246 tt_global_entry
= container_of(ref
, struct batadv_tt_global_entry
,
249 batadv_tt_global_del_orig_list(tt_global_entry
);
250 kfree_rcu(tt_global_entry
, common
.rcu
);
254 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
255 * possibly release it
256 * @tt_global_entry: tt_global_entry to be free'd
259 batadv_tt_global_entry_put(struct batadv_tt_global_entry
*tt_global_entry
)
261 kref_put(&tt_global_entry
->common
.refcount
,
262 batadv_tt_global_entry_release
);
266 * batadv_tt_global_hash_count - count the number of orig entries
267 * @bat_priv: the bat priv with all the soft interface information
268 * @addr: the mac address of the client to count entries for
269 * @vid: VLAN identifier
271 * Return: the number of originators advertising the given address/data
272 * (excluding ourself).
274 int batadv_tt_global_hash_count(struct batadv_priv
*bat_priv
,
275 const u8
*addr
, unsigned short vid
)
277 struct batadv_tt_global_entry
*tt_global_entry
;
280 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
281 if (!tt_global_entry
)
284 count
= atomic_read(&tt_global_entry
->orig_list_count
);
285 batadv_tt_global_entry_put(tt_global_entry
);
291 * batadv_tt_local_size_mod - change the size by v of the local table identified
293 * @bat_priv: the bat priv with all the soft interface information
294 * @vid: the VLAN identifier of the sub-table to change
295 * @v: the amount to sum to the local table size
297 static void batadv_tt_local_size_mod(struct batadv_priv
*bat_priv
,
298 unsigned short vid
, int v
)
300 struct batadv_softif_vlan
*vlan
;
302 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
306 atomic_add(v
, &vlan
->tt
.num_entries
);
308 batadv_softif_vlan_put(vlan
);
312 * batadv_tt_local_size_inc - increase by one the local table size for the given
314 * @bat_priv: the bat priv with all the soft interface information
315 * @vid: the VLAN identifier
317 static void batadv_tt_local_size_inc(struct batadv_priv
*bat_priv
,
320 batadv_tt_local_size_mod(bat_priv
, vid
, 1);
324 * batadv_tt_local_size_dec - decrease by one the local table size for the given
326 * @bat_priv: the bat priv with all the soft interface information
327 * @vid: the VLAN identifier
329 static void batadv_tt_local_size_dec(struct batadv_priv
*bat_priv
,
332 batadv_tt_local_size_mod(bat_priv
, vid
, -1);
336 * batadv_tt_global_size_mod - change the size by v of the global table
337 * for orig_node identified by vid
338 * @orig_node: the originator for which the table has to be modified
339 * @vid: the VLAN identifier
340 * @v: the amount to sum to the global table size
342 static void batadv_tt_global_size_mod(struct batadv_orig_node
*orig_node
,
343 unsigned short vid
, int v
)
345 struct batadv_orig_node_vlan
*vlan
;
347 vlan
= batadv_orig_node_vlan_new(orig_node
, vid
);
351 if (atomic_add_return(v
, &vlan
->tt
.num_entries
) == 0) {
352 spin_lock_bh(&orig_node
->vlan_list_lock
);
353 if (!hlist_unhashed(&vlan
->list
)) {
354 hlist_del_init_rcu(&vlan
->list
);
355 batadv_orig_node_vlan_put(vlan
);
357 spin_unlock_bh(&orig_node
->vlan_list_lock
);
360 batadv_orig_node_vlan_put(vlan
);
364 * batadv_tt_global_size_inc - increase by one the global table size for the
366 * @orig_node: the originator which global table size has to be decreased
367 * @vid: the vlan identifier
369 static void batadv_tt_global_size_inc(struct batadv_orig_node
*orig_node
,
372 batadv_tt_global_size_mod(orig_node
, vid
, 1);
376 * batadv_tt_global_size_dec - decrease by one the global table size for the
378 * @orig_node: the originator which global table size has to be decreased
379 * @vid: the vlan identifier
381 static void batadv_tt_global_size_dec(struct batadv_orig_node
*orig_node
,
384 batadv_tt_global_size_mod(orig_node
, vid
, -1);
388 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
389 * queue for free after rcu grace period
390 * @ref: kref pointer of the tt orig entry
392 static void batadv_tt_orig_list_entry_release(struct kref
*ref
)
394 struct batadv_tt_orig_list_entry
*orig_entry
;
396 orig_entry
= container_of(ref
, struct batadv_tt_orig_list_entry
,
399 batadv_orig_node_put(orig_entry
->orig_node
);
400 kfree_rcu(orig_entry
, rcu
);
404 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
405 * possibly release it
406 * @orig_entry: tt orig entry to be free'd
409 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry
*orig_entry
)
411 kref_put(&orig_entry
->refcount
, batadv_tt_orig_list_entry_release
);
415 * batadv_tt_local_event - store a local TT event (ADD/DEL)
416 * @bat_priv: the bat priv with all the soft interface information
417 * @tt_local_entry: the TT entry involved in the event
418 * @event_flags: flags to store in the event structure
420 static void batadv_tt_local_event(struct batadv_priv
*bat_priv
,
421 struct batadv_tt_local_entry
*tt_local_entry
,
424 struct batadv_tt_change_node
*tt_change_node
, *entry
, *safe
;
425 struct batadv_tt_common_entry
*common
= &tt_local_entry
->common
;
426 u8 flags
= common
->flags
| event_flags
;
427 bool event_removed
= false;
428 bool del_op_requested
, del_op_entry
;
430 tt_change_node
= kmalloc(sizeof(*tt_change_node
), GFP_ATOMIC
);
434 tt_change_node
->change
.flags
= flags
;
435 memset(tt_change_node
->change
.reserved
, 0,
436 sizeof(tt_change_node
->change
.reserved
));
437 ether_addr_copy(tt_change_node
->change
.addr
, common
->addr
);
438 tt_change_node
->change
.vid
= htons(common
->vid
);
440 del_op_requested
= flags
& BATADV_TT_CLIENT_DEL
;
442 /* check for ADD+DEL or DEL+ADD events */
443 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
444 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
446 if (!batadv_compare_eth(entry
->change
.addr
, common
->addr
))
449 /* DEL+ADD in the same orig interval have no effect and can be
450 * removed to avoid silly behaviour on the receiver side. The
451 * other way around (ADD+DEL) can happen in case of roaming of
452 * a client still in the NEW state. Roaming of NEW clients is
453 * now possible due to automatically recognition of "temporary"
456 del_op_entry
= entry
->change
.flags
& BATADV_TT_CLIENT_DEL
;
457 if (!del_op_requested
&& del_op_entry
)
459 if (del_op_requested
&& !del_op_entry
)
462 /* this is a second add in the same originator interval. It
463 * means that flags have been changed: update them!
465 if (!del_op_requested
&& !del_op_entry
)
466 entry
->change
.flags
= flags
;
470 list_del(&entry
->list
);
472 kfree(tt_change_node
);
473 event_removed
= true;
477 /* track the change in the OGMinterval list */
478 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt
.changes_list
);
481 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
484 atomic_dec(&bat_priv
->tt
.local_changes
);
486 atomic_inc(&bat_priv
->tt
.local_changes
);
490 * batadv_tt_len - compute length in bytes of given number of tt changes
491 * @changes_num: number of tt changes
493 * Return: computed length in bytes.
495 static int batadv_tt_len(int changes_num
)
497 return changes_num
* sizeof(struct batadv_tvlv_tt_change
);
501 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
502 * @tt_len: available space
504 * Return: the number of entries.
506 static u16
batadv_tt_entries(u16 tt_len
)
508 return tt_len
/ batadv_tt_len(1);
512 * batadv_tt_local_table_transmit_size - calculates the local translation table
513 * size when transmitted over the air
514 * @bat_priv: the bat priv with all the soft interface information
516 * Return: local translation table size in bytes.
518 static int batadv_tt_local_table_transmit_size(struct batadv_priv
*bat_priv
)
521 u16 tt_local_entries
= 0;
522 struct batadv_softif_vlan
*vlan
;
526 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
528 tt_local_entries
+= atomic_read(&vlan
->tt
.num_entries
);
532 /* header size of tvlv encapsulated tt response payload */
533 hdr_size
= sizeof(struct batadv_unicast_tvlv_packet
);
534 hdr_size
+= sizeof(struct batadv_tvlv_hdr
);
535 hdr_size
+= sizeof(struct batadv_tvlv_tt_data
);
536 hdr_size
+= num_vlan
* sizeof(struct batadv_tvlv_tt_vlan_data
);
538 return hdr_size
+ batadv_tt_len(tt_local_entries
);
541 static int batadv_tt_local_init(struct batadv_priv
*bat_priv
)
543 if (bat_priv
->tt
.local_hash
)
546 bat_priv
->tt
.local_hash
= batadv_hash_new(1024);
548 if (!bat_priv
->tt
.local_hash
)
551 batadv_hash_set_lock_class(bat_priv
->tt
.local_hash
,
552 &batadv_tt_local_hash_lock_class_key
);
557 static void batadv_tt_global_free(struct batadv_priv
*bat_priv
,
558 struct batadv_tt_global_entry
*tt_global
,
561 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
562 "Deleting global tt entry %pM (vid: %d): %s\n",
563 tt_global
->common
.addr
,
564 BATADV_PRINT_VID(tt_global
->common
.vid
), message
);
566 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
567 batadv_choose_tt
, &tt_global
->common
);
568 batadv_tt_global_entry_put(tt_global
);
572 * batadv_tt_local_add - add a new client to the local table or update an
574 * @soft_iface: netdev struct of the mesh interface
575 * @addr: the mac address of the client to add
576 * @vid: VLAN identifier
577 * @ifindex: index of the interface where the client is connected to (useful to
578 * identify wireless clients)
579 * @mark: the value contained in the skb->mark field of the received packet (if
582 * Return: true if the client was successfully added, false otherwise.
584 bool batadv_tt_local_add(struct net_device
*soft_iface
, const u8
*addr
,
585 unsigned short vid
, int ifindex
, u32 mark
)
587 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
588 struct batadv_tt_local_entry
*tt_local
;
589 struct batadv_tt_global_entry
*tt_global
= NULL
;
590 struct net
*net
= dev_net(soft_iface
);
591 struct batadv_softif_vlan
*vlan
;
592 struct net_device
*in_dev
= NULL
;
593 struct hlist_head
*head
;
594 struct batadv_tt_orig_list_entry
*orig_entry
;
595 int hash_added
, table_size
, packet_size_max
;
597 bool roamed_back
= false;
601 if (ifindex
!= BATADV_NULL_IFINDEX
)
602 in_dev
= dev_get_by_index(net
, ifindex
);
604 tt_local
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
606 if (!is_multicast_ether_addr(addr
))
607 tt_global
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
610 tt_local
->last_seen
= jiffies
;
611 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_PENDING
) {
612 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
613 "Re-adding pending client %pM (vid: %d)\n",
614 addr
, BATADV_PRINT_VID(vid
));
615 /* whatever the reason why the PENDING flag was set,
616 * this is a client which was enqueued to be removed in
617 * this orig_interval. Since it popped up again, the
618 * flag can be reset like it was never enqueued
620 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_PENDING
;
624 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
625 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
626 "Roaming client %pM (vid: %d) came back to its original location\n",
627 addr
, BATADV_PRINT_VID(vid
));
628 /* the ROAM flag is set because this client roamed away
629 * and the node got a roaming_advertisement message. Now
630 * that the client popped up again at its original
631 * location such flag can be unset
633 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
639 /* Ignore the client if we cannot send it in a full table response. */
640 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
641 table_size
+= batadv_tt_len(1);
642 packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
643 if (table_size
> packet_size_max
) {
644 net_ratelimited_function(batadv_info
, soft_iface
,
645 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
646 table_size
, packet_size_max
, addr
);
650 tt_local
= kmalloc(sizeof(*tt_local
), GFP_ATOMIC
);
654 /* increase the refcounter of the related vlan */
655 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
656 if (WARN(!vlan
, "adding TT local entry %pM to non-existent VLAN %d",
657 addr
, BATADV_PRINT_VID(vid
))) {
663 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
664 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
665 addr
, BATADV_PRINT_VID(vid
),
666 (u8
)atomic_read(&bat_priv
->tt
.vn
));
668 ether_addr_copy(tt_local
->common
.addr
, addr
);
669 /* The local entry has to be marked as NEW to avoid to send it in
670 * a full table response going out before the next ttvn increment
671 * (consistency check)
673 tt_local
->common
.flags
= BATADV_TT_CLIENT_NEW
;
674 tt_local
->common
.vid
= vid
;
675 if (batadv_is_wifi_netdev(in_dev
))
676 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
677 kref_init(&tt_local
->common
.refcount
);
678 kref_get(&tt_local
->common
.refcount
);
679 tt_local
->last_seen
= jiffies
;
680 tt_local
->common
.added_at
= tt_local
->last_seen
;
681 tt_local
->vlan
= vlan
;
683 /* the batman interface mac and multicast addresses should never be
686 if (batadv_compare_eth(addr
, soft_iface
->dev_addr
) ||
687 is_multicast_ether_addr(addr
))
688 tt_local
->common
.flags
|= BATADV_TT_CLIENT_NOPURGE
;
690 hash_added
= batadv_hash_add(bat_priv
->tt
.local_hash
, batadv_compare_tt
,
691 batadv_choose_tt
, &tt_local
->common
,
692 &tt_local
->common
.hash_entry
);
694 if (unlikely(hash_added
!= 0)) {
695 /* remove the reference for the hash */
696 batadv_tt_local_entry_put(tt_local
);
697 batadv_softif_vlan_put(vlan
);
702 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
705 /* Check whether it is a roaming, but don't do anything if the roaming
706 * process has already been handled
708 if (tt_global
&& !(tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
)) {
709 /* These node are probably going to update their tt table */
710 head
= &tt_global
->orig_list
;
712 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
713 batadv_send_roam_adv(bat_priv
, tt_global
->common
.addr
,
714 tt_global
->common
.vid
,
715 orig_entry
->orig_node
);
719 batadv_tt_global_free(bat_priv
, tt_global
,
723 /* The global entry has to be marked as ROAMING and
724 * has to be kept for consistency purpose
726 tt_global
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
727 tt_global
->roam_at
= jiffies
;
731 /* store the current remote flags before altering them. This helps
732 * understanding is flags are changing or not
734 remote_flags
= tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
;
736 if (batadv_is_wifi_netdev(in_dev
))
737 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
739 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_WIFI
;
741 /* check the mark in the skb: if it's equal to the configured
742 * isolation_mark, it means the packet is coming from an isolated
745 match_mark
= (mark
& bat_priv
->isolation_mark_mask
);
746 if (bat_priv
->isolation_mark_mask
&&
747 match_mark
== bat_priv
->isolation_mark
)
748 tt_local
->common
.flags
|= BATADV_TT_CLIENT_ISOLA
;
750 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ISOLA
;
752 /* if any "dynamic" flag has been modified, resend an ADD event for this
753 * entry so that all the nodes can get the new flags
755 if (remote_flags
^ (tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
))
756 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
763 batadv_tt_local_entry_put(tt_local
);
765 batadv_tt_global_entry_put(tt_global
);
770 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
771 * within a TT Response directed to another node
772 * @orig_node: originator for which the TT data has to be prepared
773 * @tt_data: uninitialised pointer to the address of the TVLV buffer
774 * @tt_change: uninitialised pointer to the address of the area where the TT
775 * changed can be stored
776 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
777 * function reserves the amount of space needed to send the entire global TT
778 * table. In case of success the value is updated with the real amount of
780 * Allocate the needed amount of memory for the entire TT TVLV and write its
781 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
782 * objects, one per active VLAN served by the originator node.
784 * Return: the size of the allocated buffer or 0 in case of failure.
787 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node
*orig_node
,
788 struct batadv_tvlv_tt_data
**tt_data
,
789 struct batadv_tvlv_tt_change
**tt_change
,
796 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
797 struct batadv_orig_node_vlan
*vlan
;
801 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
803 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
806 change_offset
= sizeof(**tt_data
);
807 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
809 /* if tt_len is negative, allocate the space needed by the full table */
811 *tt_len
= batadv_tt_len(num_entries
);
814 tvlv_len
+= change_offset
;
816 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
822 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
823 (*tt_data
)->ttvn
= atomic_read(&orig_node
->last_ttvn
);
824 (*tt_data
)->num_vlan
= htons(num_vlan
);
826 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
827 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
828 tt_vlan
->vid
= htons(vlan
->vid
);
829 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
834 tt_change_ptr
= (u8
*)*tt_data
+ change_offset
;
835 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
843 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
845 * @bat_priv: the bat priv with all the soft interface information
846 * @tt_data: uninitialised pointer to the address of the TVLV buffer
847 * @tt_change: uninitialised pointer to the address of the area where the TT
848 * changes can be stored
849 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
850 * function reserves the amount of space needed to send the entire local TT
851 * table. In case of success the value is updated with the real amount of
854 * Allocate the needed amount of memory for the entire TT TVLV and write its
855 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
856 * objects, one per active VLAN.
858 * Return: the size of the allocated buffer or 0 in case of failure.
861 batadv_tt_prepare_tvlv_local_data(struct batadv_priv
*bat_priv
,
862 struct batadv_tvlv_tt_data
**tt_data
,
863 struct batadv_tvlv_tt_change
**tt_change
,
866 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
867 struct batadv_softif_vlan
*vlan
;
875 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
877 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
880 change_offset
= sizeof(**tt_data
);
881 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
883 /* if tt_len is negative, allocate the space needed by the full table */
885 *tt_len
= batadv_tt_len(num_entries
);
888 tvlv_len
+= change_offset
;
890 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
896 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
897 (*tt_data
)->ttvn
= atomic_read(&bat_priv
->tt
.vn
);
898 (*tt_data
)->num_vlan
= htons(num_vlan
);
900 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
901 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
902 tt_vlan
->vid
= htons(vlan
->vid
);
903 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
908 tt_change_ptr
= (u8
*)*tt_data
+ change_offset
;
909 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
917 * batadv_tt_tvlv_container_update - update the translation table tvlv container
918 * after local tt changes have been committed
919 * @bat_priv: the bat priv with all the soft interface information
921 static void batadv_tt_tvlv_container_update(struct batadv_priv
*bat_priv
)
923 struct batadv_tt_change_node
*entry
, *safe
;
924 struct batadv_tvlv_tt_data
*tt_data
;
925 struct batadv_tvlv_tt_change
*tt_change
;
926 int tt_diff_len
, tt_change_len
= 0;
927 int tt_diff_entries_num
= 0;
928 int tt_diff_entries_count
= 0;
931 tt_diff_entries_num
= atomic_read(&bat_priv
->tt
.local_changes
);
932 tt_diff_len
= batadv_tt_len(tt_diff_entries_num
);
934 /* if we have too many changes for one packet don't send any
935 * and wait for the tt table request which will be fragmented
937 if (tt_diff_len
> bat_priv
->soft_iface
->mtu
)
940 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
, &tt_data
,
941 &tt_change
, &tt_diff_len
);
945 tt_data
->flags
= BATADV_TT_OGM_DIFF
;
947 if (tt_diff_len
== 0)
948 goto container_register
;
950 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
951 atomic_set(&bat_priv
->tt
.local_changes
, 0);
953 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
955 if (tt_diff_entries_count
< tt_diff_entries_num
) {
956 memcpy(tt_change
+ tt_diff_entries_count
,
958 sizeof(struct batadv_tvlv_tt_change
));
959 tt_diff_entries_count
++;
961 list_del(&entry
->list
);
964 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
966 /* Keep the buffer for possible tt_request */
967 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
968 kfree(bat_priv
->tt
.last_changeset
);
969 bat_priv
->tt
.last_changeset_len
= 0;
970 bat_priv
->tt
.last_changeset
= NULL
;
971 tt_change_len
= batadv_tt_len(tt_diff_entries_count
);
972 /* check whether this new OGM has no changes due to size problems */
973 if (tt_diff_entries_count
> 0) {
974 /* if kmalloc() fails we will reply with the full table
975 * instead of providing the diff
977 bat_priv
->tt
.last_changeset
= kzalloc(tt_diff_len
, GFP_ATOMIC
);
978 if (bat_priv
->tt
.last_changeset
) {
979 memcpy(bat_priv
->tt
.last_changeset
,
980 tt_change
, tt_change_len
);
981 bat_priv
->tt
.last_changeset_len
= tt_diff_len
;
984 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
987 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_TT
, 1, tt_data
,
992 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
994 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
995 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
996 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
997 struct batadv_tt_common_entry
*tt_common_entry
;
998 struct batadv_tt_local_entry
*tt_local
;
999 struct batadv_hard_iface
*primary_if
;
1000 struct hlist_head
*head
;
1003 int last_seen_msecs
;
1004 unsigned long last_seen_jiffies
;
1006 u16 np_flag
= BATADV_TT_CLIENT_NOPURGE
;
1008 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1013 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1014 net_dev
->name
, (u8
)atomic_read(&bat_priv
->tt
.vn
));
1016 " Client VID Flags Last seen (CRC )\n");
1018 for (i
= 0; i
< hash
->size
; i
++) {
1019 head
= &hash
->table
[i
];
1022 hlist_for_each_entry_rcu(tt_common_entry
,
1024 tt_local
= container_of(tt_common_entry
,
1025 struct batadv_tt_local_entry
,
1027 last_seen_jiffies
= jiffies
- tt_local
->last_seen
;
1028 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
1029 last_seen_secs
= last_seen_msecs
/ 1000;
1030 last_seen_msecs
= last_seen_msecs
% 1000;
1032 no_purge
= tt_common_entry
->flags
& np_flag
;
1034 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1035 tt_common_entry
->addr
,
1036 BATADV_PRINT_VID(tt_common_entry
->vid
),
1037 ((tt_common_entry
->flags
&
1038 BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1039 no_purge
? 'P' : '.',
1040 ((tt_common_entry
->flags
&
1041 BATADV_TT_CLIENT_NEW
) ? 'N' : '.'),
1042 ((tt_common_entry
->flags
&
1043 BATADV_TT_CLIENT_PENDING
) ? 'X' : '.'),
1044 ((tt_common_entry
->flags
&
1045 BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1046 ((tt_common_entry
->flags
&
1047 BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1048 no_purge
? 0 : last_seen_secs
,
1049 no_purge
? 0 : last_seen_msecs
,
1050 tt_local
->vlan
->tt
.crc
);
1056 batadv_hardif_put(primary_if
);
1061 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
1062 struct batadv_tt_local_entry
*tt_local_entry
,
1063 u16 flags
, const char *message
)
1065 batadv_tt_local_event(bat_priv
, tt_local_entry
, flags
);
1067 /* The local client has to be marked as "pending to be removed" but has
1068 * to be kept in the table in order to send it in a full table
1069 * response issued before the net ttvn increment (consistency check)
1071 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
1073 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1074 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1075 tt_local_entry
->common
.addr
,
1076 BATADV_PRINT_VID(tt_local_entry
->common
.vid
), message
);
1080 * batadv_tt_local_remove - logically remove an entry from the local table
1081 * @bat_priv: the bat priv with all the soft interface information
1082 * @addr: the MAC address of the client to remove
1083 * @vid: VLAN identifier
1084 * @message: message to append to the log on deletion
1085 * @roaming: true if the deletion is due to a roaming event
1087 * Return: the flags assigned to the local entry before being deleted
1089 u16
batadv_tt_local_remove(struct batadv_priv
*bat_priv
, const u8
*addr
,
1090 unsigned short vid
, const char *message
,
1093 struct batadv_tt_local_entry
*tt_local_entry
;
1094 u16 flags
, curr_flags
= BATADV_NO_FLAGS
;
1095 void *tt_entry_exists
;
1097 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
1098 if (!tt_local_entry
)
1101 curr_flags
= tt_local_entry
->common
.flags
;
1103 flags
= BATADV_TT_CLIENT_DEL
;
1104 /* if this global entry addition is due to a roaming, the node has to
1105 * mark the local entry as "roamed" in order to correctly reroute
1109 flags
|= BATADV_TT_CLIENT_ROAM
;
1110 /* mark the local client as ROAMed */
1111 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1114 if (!(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
)) {
1115 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
,
1119 /* if this client has been added right now, it is possible to
1120 * immediately purge it
1122 batadv_tt_local_event(bat_priv
, tt_local_entry
, BATADV_TT_CLIENT_DEL
);
1124 tt_entry_exists
= batadv_hash_remove(bat_priv
->tt
.local_hash
,
1127 &tt_local_entry
->common
);
1128 if (!tt_entry_exists
)
1131 /* extra call to free the local tt entry */
1132 batadv_tt_local_entry_put(tt_local_entry
);
1136 batadv_tt_local_entry_put(tt_local_entry
);
1142 * batadv_tt_local_purge_list - purge inactive tt local entries
1143 * @bat_priv: the bat priv with all the soft interface information
1144 * @head: pointer to the list containing the local tt entries
1145 * @timeout: parameter deciding whether a given tt local entry is considered
1148 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
1149 struct hlist_head
*head
,
1152 struct batadv_tt_local_entry
*tt_local_entry
;
1153 struct batadv_tt_common_entry
*tt_common_entry
;
1154 struct hlist_node
*node_tmp
;
1156 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
, head
,
1158 tt_local_entry
= container_of(tt_common_entry
,
1159 struct batadv_tt_local_entry
,
1161 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
1164 /* entry already marked for deletion */
1165 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1168 if (!batadv_has_timed_out(tt_local_entry
->last_seen
, timeout
))
1171 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
1172 BATADV_TT_CLIENT_DEL
, "timed out");
1177 * batadv_tt_local_purge - purge inactive tt local entries
1178 * @bat_priv: the bat priv with all the soft interface information
1179 * @timeout: parameter deciding whether a given tt local entry is considered
1182 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
,
1185 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1186 struct hlist_head
*head
;
1187 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1190 for (i
= 0; i
< hash
->size
; i
++) {
1191 head
= &hash
->table
[i
];
1192 list_lock
= &hash
->list_locks
[i
];
1194 spin_lock_bh(list_lock
);
1195 batadv_tt_local_purge_list(bat_priv
, head
, timeout
);
1196 spin_unlock_bh(list_lock
);
1200 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
1202 struct batadv_hashtable
*hash
;
1203 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1204 struct batadv_tt_common_entry
*tt_common_entry
;
1205 struct batadv_tt_local_entry
*tt_local
;
1206 struct hlist_node
*node_tmp
;
1207 struct hlist_head
*head
;
1210 if (!bat_priv
->tt
.local_hash
)
1213 hash
= bat_priv
->tt
.local_hash
;
1215 for (i
= 0; i
< hash
->size
; i
++) {
1216 head
= &hash
->table
[i
];
1217 list_lock
= &hash
->list_locks
[i
];
1219 spin_lock_bh(list_lock
);
1220 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1222 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1223 tt_local
= container_of(tt_common_entry
,
1224 struct batadv_tt_local_entry
,
1227 batadv_tt_local_entry_put(tt_local
);
1229 spin_unlock_bh(list_lock
);
1232 batadv_hash_destroy(hash
);
1234 bat_priv
->tt
.local_hash
= NULL
;
1237 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
1239 if (bat_priv
->tt
.global_hash
)
1242 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
1244 if (!bat_priv
->tt
.global_hash
)
1247 batadv_hash_set_lock_class(bat_priv
->tt
.global_hash
,
1248 &batadv_tt_global_hash_lock_class_key
);
1253 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
1255 struct batadv_tt_change_node
*entry
, *safe
;
1257 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
1259 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
1261 list_del(&entry
->list
);
1265 atomic_set(&bat_priv
->tt
.local_changes
, 0);
1266 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
1270 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1271 * @entry: the TT global entry where the orig_list_entry has to be
1273 * @orig_node: the originator for which the orig_list_entry has to be found
1275 * retrieve the orig_tt_list_entry belonging to orig_node from the
1276 * batadv_tt_global_entry list
1278 * Return: it with an increased refcounter, NULL if not found
1280 static struct batadv_tt_orig_list_entry
*
1281 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
1282 const struct batadv_orig_node
*orig_node
)
1284 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
1285 const struct hlist_head
*head
;
1288 head
= &entry
->orig_list
;
1289 hlist_for_each_entry_rcu(tmp_orig_entry
, head
, list
) {
1290 if (tmp_orig_entry
->orig_node
!= orig_node
)
1292 if (!kref_get_unless_zero(&tmp_orig_entry
->refcount
))
1295 orig_entry
= tmp_orig_entry
;
1304 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1305 * by a given originator
1306 * @entry: the TT global entry to check
1307 * @orig_node: the originator to search in the list
1309 * find out if an orig_node is already in the list of a tt_global_entry.
1311 * Return: true if found, false otherwise
1314 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
1315 const struct batadv_orig_node
*orig_node
)
1317 struct batadv_tt_orig_list_entry
*orig_entry
;
1320 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
1323 batadv_tt_orig_list_entry_put(orig_entry
);
1330 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
1331 struct batadv_orig_node
*orig_node
, int ttvn
)
1333 struct batadv_tt_orig_list_entry
*orig_entry
;
1335 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
1337 /* refresh the ttvn: the current value could be a bogus one that
1338 * was added during a "temporary client detection"
1340 orig_entry
->ttvn
= ttvn
;
1344 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
1348 INIT_HLIST_NODE(&orig_entry
->list
);
1349 kref_get(&orig_node
->refcount
);
1350 batadv_tt_global_size_inc(orig_node
, tt_global
->common
.vid
);
1351 orig_entry
->orig_node
= orig_node
;
1352 orig_entry
->ttvn
= ttvn
;
1353 kref_init(&orig_entry
->refcount
);
1354 kref_get(&orig_entry
->refcount
);
1356 spin_lock_bh(&tt_global
->list_lock
);
1357 hlist_add_head_rcu(&orig_entry
->list
,
1358 &tt_global
->orig_list
);
1359 spin_unlock_bh(&tt_global
->list_lock
);
1360 atomic_inc(&tt_global
->orig_list_count
);
1364 batadv_tt_orig_list_entry_put(orig_entry
);
1368 * batadv_tt_global_add - add a new TT global entry or update an existing one
1369 * @bat_priv: the bat priv with all the soft interface information
1370 * @orig_node: the originator announcing the client
1371 * @tt_addr: the mac address of the non-mesh client
1372 * @vid: VLAN identifier
1373 * @flags: TT flags that have to be set for this non-mesh client
1374 * @ttvn: the tt version number ever announcing this non-mesh client
1376 * Add a new TT global entry for the given originator. If the entry already
1377 * exists add a new reference to the given originator (a global entry can have
1378 * references to multiple originators) and adjust the flags attribute to reflect
1379 * the function argument.
1380 * If a TT local entry exists for this non-mesh client remove it.
1382 * The caller must hold orig_node refcount.
1384 * Return: true if the new entry has been added, false otherwise
1386 static bool batadv_tt_global_add(struct batadv_priv
*bat_priv
,
1387 struct batadv_orig_node
*orig_node
,
1388 const unsigned char *tt_addr
,
1389 unsigned short vid
, u16 flags
, u8 ttvn
)
1391 struct batadv_tt_global_entry
*tt_global_entry
;
1392 struct batadv_tt_local_entry
*tt_local_entry
;
1395 struct batadv_tt_common_entry
*common
;
1398 /* ignore global entries from backbone nodes */
1399 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
, vid
))
1402 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
, vid
);
1403 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, tt_addr
, vid
);
1405 /* if the node already has a local client for this entry, it has to wait
1406 * for a roaming advertisement instead of manually messing up the global
1409 if ((flags
& BATADV_TT_CLIENT_TEMP
) && tt_local_entry
&&
1410 !(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
))
1413 if (!tt_global_entry
) {
1414 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
1415 if (!tt_global_entry
)
1418 common
= &tt_global_entry
->common
;
1419 ether_addr_copy(common
->addr
, tt_addr
);
1422 common
->flags
= flags
;
1423 tt_global_entry
->roam_at
= 0;
1424 /* node must store current time in case of roaming. This is
1425 * needed to purge this entry out on timeout (if nobody claims
1428 if (flags
& BATADV_TT_CLIENT_ROAM
)
1429 tt_global_entry
->roam_at
= jiffies
;
1430 kref_init(&common
->refcount
);
1431 kref_get(&common
->refcount
);
1432 common
->added_at
= jiffies
;
1434 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
1435 atomic_set(&tt_global_entry
->orig_list_count
, 0);
1436 spin_lock_init(&tt_global_entry
->list_lock
);
1438 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
1440 batadv_choose_tt
, common
,
1441 &common
->hash_entry
);
1443 if (unlikely(hash_added
!= 0)) {
1444 /* remove the reference for the hash */
1445 batadv_tt_global_entry_put(tt_global_entry
);
1449 common
= &tt_global_entry
->common
;
1450 /* If there is already a global entry, we can use this one for
1452 * But if we are trying to add a temporary client then here are
1453 * two options at this point:
1454 * 1) the global client is not a temporary client: the global
1455 * client has to be left as it is, temporary information
1456 * should never override any already known client state
1457 * 2) the global client is a temporary client: purge the
1458 * originator list and add the new one orig_entry
1460 if (flags
& BATADV_TT_CLIENT_TEMP
) {
1461 if (!(common
->flags
& BATADV_TT_CLIENT_TEMP
))
1463 if (batadv_tt_global_entry_has_orig(tt_global_entry
,
1466 batadv_tt_global_del_orig_list(tt_global_entry
);
1467 goto add_orig_entry
;
1470 /* if the client was temporary added before receiving the first
1471 * OGM announcing it, we have to clear the TEMP flag. Also,
1472 * remove the previous temporary orig node and re-add it
1473 * if required. If the orig entry changed, the new one which
1474 * is a non-temporary entry is preferred.
1476 if (common
->flags
& BATADV_TT_CLIENT_TEMP
) {
1477 batadv_tt_global_del_orig_list(tt_global_entry
);
1478 common
->flags
&= ~BATADV_TT_CLIENT_TEMP
;
1481 /* the change can carry possible "attribute" flags like the
1482 * TT_CLIENT_WIFI, therefore they have to be copied in the
1485 common
->flags
|= flags
;
1487 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1488 * one originator left in the list and we previously received a
1489 * delete + roaming change for this originator.
1491 * We should first delete the old originator before adding the
1494 if (common
->flags
& BATADV_TT_CLIENT_ROAM
) {
1495 batadv_tt_global_del_orig_list(tt_global_entry
);
1496 common
->flags
&= ~BATADV_TT_CLIENT_ROAM
;
1497 tt_global_entry
->roam_at
= 0;
1501 /* add the new orig_entry (if needed) or update it */
1502 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
1504 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1505 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1506 common
->addr
, BATADV_PRINT_VID(common
->vid
),
1511 /* Do not remove multicast addresses from the local hash on
1514 if (is_multicast_ether_addr(tt_addr
))
1517 /* remove address from local hash if present */
1518 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
, vid
,
1519 "global tt received",
1520 flags
& BATADV_TT_CLIENT_ROAM
);
1521 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
1523 if (!(flags
& BATADV_TT_CLIENT_ROAM
))
1524 /* this is a normal global add. Therefore the client is not in a
1525 * roaming state anymore.
1527 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
1530 if (tt_global_entry
)
1531 batadv_tt_global_entry_put(tt_global_entry
);
1533 batadv_tt_local_entry_put(tt_local_entry
);
1538 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1539 * @bat_priv: the bat priv with all the soft interface information
1540 * @tt_global_entry: global translation table entry to be analyzed
1542 * This functon assumes the caller holds rcu_read_lock().
1543 * Return: best originator list entry or NULL on errors.
1545 static struct batadv_tt_orig_list_entry
*
1546 batadv_transtable_best_orig(struct batadv_priv
*bat_priv
,
1547 struct batadv_tt_global_entry
*tt_global_entry
)
1549 struct batadv_neigh_node
*router
, *best_router
= NULL
;
1550 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1551 struct hlist_head
*head
;
1552 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
= NULL
;
1554 head
= &tt_global_entry
->orig_list
;
1555 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1556 router
= batadv_orig_router_get(orig_entry
->orig_node
,
1562 bao
->bat_neigh_cmp(router
, BATADV_IF_DEFAULT
,
1563 best_router
, BATADV_IF_DEFAULT
) <= 0) {
1564 batadv_neigh_node_put(router
);
1568 /* release the refcount for the "old" best */
1570 batadv_neigh_node_put(best_router
);
1572 best_entry
= orig_entry
;
1573 best_router
= router
;
1577 batadv_neigh_node_put(best_router
);
1583 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1584 * for this global entry
1585 * @bat_priv: the bat priv with all the soft interface information
1586 * @tt_global_entry: global translation table entry to be printed
1587 * @seq: debugfs table seq_file struct
1589 * This functon assumes the caller holds rcu_read_lock().
1592 batadv_tt_global_print_entry(struct batadv_priv
*bat_priv
,
1593 struct batadv_tt_global_entry
*tt_global_entry
,
1594 struct seq_file
*seq
)
1596 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
;
1597 struct batadv_tt_common_entry
*tt_common_entry
;
1598 struct batadv_orig_node_vlan
*vlan
;
1599 struct hlist_head
*head
;
1603 tt_common_entry
= &tt_global_entry
->common
;
1604 flags
= tt_common_entry
->flags
;
1606 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
1608 vlan
= batadv_orig_node_vlan_get(best_entry
->orig_node
,
1609 tt_common_entry
->vid
);
1612 " * Cannot retrieve VLAN %d for originator %pM\n",
1613 BATADV_PRINT_VID(tt_common_entry
->vid
),
1614 best_entry
->orig_node
->orig
);
1618 last_ttvn
= atomic_read(&best_entry
->orig_node
->last_ttvn
);
1620 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1621 '*', tt_global_entry
->common
.addr
,
1622 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1623 best_entry
->ttvn
, best_entry
->orig_node
->orig
,
1624 last_ttvn
, vlan
->tt
.crc
,
1625 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1626 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1627 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1628 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1630 batadv_orig_node_vlan_put(vlan
);
1634 head
= &tt_global_entry
->orig_list
;
1636 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1637 if (best_entry
== orig_entry
)
1640 vlan
= batadv_orig_node_vlan_get(orig_entry
->orig_node
,
1641 tt_common_entry
->vid
);
1644 " + Cannot retrieve VLAN %d for originator %pM\n",
1645 BATADV_PRINT_VID(tt_common_entry
->vid
),
1646 orig_entry
->orig_node
->orig
);
1650 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
1652 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1653 '+', tt_global_entry
->common
.addr
,
1654 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1655 orig_entry
->ttvn
, orig_entry
->orig_node
->orig
,
1656 last_ttvn
, vlan
->tt
.crc
,
1657 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1658 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1659 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1660 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1662 batadv_orig_node_vlan_put(vlan
);
1666 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
1668 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1669 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1670 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1671 struct batadv_tt_common_entry
*tt_common_entry
;
1672 struct batadv_tt_global_entry
*tt_global
;
1673 struct batadv_hard_iface
*primary_if
;
1674 struct hlist_head
*head
;
1677 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1682 "Globally announced TT entries received via the mesh %s\n",
1685 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
1687 for (i
= 0; i
< hash
->size
; i
++) {
1688 head
= &hash
->table
[i
];
1691 hlist_for_each_entry_rcu(tt_common_entry
,
1693 tt_global
= container_of(tt_common_entry
,
1694 struct batadv_tt_global_entry
,
1696 batadv_tt_global_print_entry(bat_priv
, tt_global
, seq
);
1702 batadv_hardif_put(primary_if
);
1707 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1708 * @tt_global_entry: the global entry to remove the orig_entry from
1709 * @orig_entry: the orig entry to remove and free
1711 * Remove an orig_entry from its list in the given tt_global_entry and
1712 * free this orig_entry afterwards.
1714 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1718 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry
*tt_global_entry
,
1719 struct batadv_tt_orig_list_entry
*orig_entry
)
1721 lockdep_assert_held(&tt_global_entry
->list_lock
);
1723 batadv_tt_global_size_dec(orig_entry
->orig_node
,
1724 tt_global_entry
->common
.vid
);
1725 atomic_dec(&tt_global_entry
->orig_list_count
);
1726 /* requires holding tt_global_entry->list_lock and orig_entry->list
1727 * being part of a list
1729 hlist_del_rcu(&orig_entry
->list
);
1730 batadv_tt_orig_list_entry_put(orig_entry
);
1733 /* deletes the orig list of a tt_global_entry */
1735 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
1737 struct hlist_head
*head
;
1738 struct hlist_node
*safe
;
1739 struct batadv_tt_orig_list_entry
*orig_entry
;
1741 spin_lock_bh(&tt_global_entry
->list_lock
);
1742 head
= &tt_global_entry
->orig_list
;
1743 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
)
1744 _batadv_tt_global_del_orig_entry(tt_global_entry
, orig_entry
);
1745 spin_unlock_bh(&tt_global_entry
->list_lock
);
1749 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1750 * @bat_priv: the bat priv with all the soft interface information
1751 * @tt_global_entry: the global entry to remove the orig_node from
1752 * @orig_node: the originator announcing the client
1753 * @message: message to append to the log on deletion
1755 * Remove the given orig_node and its according orig_entry from the given
1759 batadv_tt_global_del_orig_node(struct batadv_priv
*bat_priv
,
1760 struct batadv_tt_global_entry
*tt_global_entry
,
1761 struct batadv_orig_node
*orig_node
,
1762 const char *message
)
1764 struct hlist_head
*head
;
1765 struct hlist_node
*safe
;
1766 struct batadv_tt_orig_list_entry
*orig_entry
;
1769 spin_lock_bh(&tt_global_entry
->list_lock
);
1770 head
= &tt_global_entry
->orig_list
;
1771 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
) {
1772 if (orig_entry
->orig_node
== orig_node
) {
1773 vid
= tt_global_entry
->common
.vid
;
1774 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1775 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1777 tt_global_entry
->common
.addr
,
1778 BATADV_PRINT_VID(vid
), message
);
1779 _batadv_tt_global_del_orig_entry(tt_global_entry
,
1783 spin_unlock_bh(&tt_global_entry
->list_lock
);
1786 /* If the client is to be deleted, we check if it is the last origantor entry
1787 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1788 * timer, otherwise we simply remove the originator scheduled for deletion.
1791 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
1792 struct batadv_tt_global_entry
*tt_global_entry
,
1793 struct batadv_orig_node
*orig_node
,
1794 const char *message
)
1796 bool last_entry
= true;
1797 struct hlist_head
*head
;
1798 struct batadv_tt_orig_list_entry
*orig_entry
;
1800 /* no local entry exists, case 1:
1801 * Check if this is the last one or if other entries exist.
1805 head
= &tt_global_entry
->orig_list
;
1806 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1807 if (orig_entry
->orig_node
!= orig_node
) {
1815 /* its the last one, mark for roaming. */
1816 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1817 tt_global_entry
->roam_at
= jiffies
;
1819 /* there is another entry, we can simply delete this
1820 * one and can still use the other one.
1822 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1823 orig_node
, message
);
1827 * batadv_tt_global_del - remove a client from the global table
1828 * @bat_priv: the bat priv with all the soft interface information
1829 * @orig_node: an originator serving this client
1830 * @addr: the mac address of the client
1831 * @vid: VLAN identifier
1832 * @message: a message explaining the reason for deleting the client to print
1833 * for debugging purpose
1834 * @roaming: true if the deletion has been triggered by a roaming event
1836 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
1837 struct batadv_orig_node
*orig_node
,
1838 const unsigned char *addr
, unsigned short vid
,
1839 const char *message
, bool roaming
)
1841 struct batadv_tt_global_entry
*tt_global_entry
;
1842 struct batadv_tt_local_entry
*local_entry
= NULL
;
1844 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
1845 if (!tt_global_entry
)
1849 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1850 orig_node
, message
);
1852 if (hlist_empty(&tt_global_entry
->orig_list
))
1853 batadv_tt_global_free(bat_priv
, tt_global_entry
,
1859 /* if we are deleting a global entry due to a roam
1860 * event, there are two possibilities:
1861 * 1) the client roamed from node A to node B => if there
1862 * is only one originator left for this client, we mark
1863 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1864 * wait for node B to claim it. In case of timeout
1865 * the entry is purged.
1867 * If there are other originators left, we directly delete
1869 * 2) the client roamed to us => we can directly delete
1870 * the global entry, since it is useless now.
1872 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1873 tt_global_entry
->common
.addr
,
1876 /* local entry exists, case 2: client roamed to us. */
1877 batadv_tt_global_del_orig_list(tt_global_entry
);
1878 batadv_tt_global_free(bat_priv
, tt_global_entry
, message
);
1880 /* no local entry exists, case 1: check for roaming */
1881 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1882 orig_node
, message
);
1885 if (tt_global_entry
)
1886 batadv_tt_global_entry_put(tt_global_entry
);
1888 batadv_tt_local_entry_put(local_entry
);
1892 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1893 * given originator matching the provided vid
1894 * @bat_priv: the bat priv with all the soft interface information
1895 * @orig_node: the originator owning the entries to remove
1896 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1898 * @message: debug message to print as "reason"
1900 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1901 struct batadv_orig_node
*orig_node
,
1903 const char *message
)
1905 struct batadv_tt_global_entry
*tt_global
;
1906 struct batadv_tt_common_entry
*tt_common_entry
;
1908 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1909 struct hlist_node
*safe
;
1910 struct hlist_head
*head
;
1911 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1917 for (i
= 0; i
< hash
->size
; i
++) {
1918 head
= &hash
->table
[i
];
1919 list_lock
= &hash
->list_locks
[i
];
1921 spin_lock_bh(list_lock
);
1922 hlist_for_each_entry_safe(tt_common_entry
, safe
,
1924 /* remove only matching entries */
1925 if (match_vid
>= 0 && tt_common_entry
->vid
!= match_vid
)
1928 tt_global
= container_of(tt_common_entry
,
1929 struct batadv_tt_global_entry
,
1932 batadv_tt_global_del_orig_node(bat_priv
, tt_global
,
1933 orig_node
, message
);
1935 if (hlist_empty(&tt_global
->orig_list
)) {
1936 vid
= tt_global
->common
.vid
;
1937 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1938 "Deleting global tt entry %pM (vid: %d): %s\n",
1939 tt_global
->common
.addr
,
1940 BATADV_PRINT_VID(vid
), message
);
1941 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1942 batadv_tt_global_entry_put(tt_global
);
1945 spin_unlock_bh(list_lock
);
1947 clear_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
1950 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1954 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1955 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1957 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1958 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1960 *msg
= "Roaming timeout\n";
1963 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1964 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1966 *msg
= "Temporary client timeout\n";
1972 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1974 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1975 struct hlist_head
*head
;
1976 struct hlist_node
*node_tmp
;
1977 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1980 struct batadv_tt_common_entry
*tt_common
;
1981 struct batadv_tt_global_entry
*tt_global
;
1983 for (i
= 0; i
< hash
->size
; i
++) {
1984 head
= &hash
->table
[i
];
1985 list_lock
= &hash
->list_locks
[i
];
1987 spin_lock_bh(list_lock
);
1988 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
1990 tt_global
= container_of(tt_common
,
1991 struct batadv_tt_global_entry
,
1994 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1997 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1998 "Deleting global tt entry %pM (vid: %d): %s\n",
1999 tt_global
->common
.addr
,
2000 BATADV_PRINT_VID(tt_global
->common
.vid
),
2003 hlist_del_rcu(&tt_common
->hash_entry
);
2005 batadv_tt_global_entry_put(tt_global
);
2007 spin_unlock_bh(list_lock
);
2011 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
2013 struct batadv_hashtable
*hash
;
2014 spinlock_t
*list_lock
; /* protects write access to the hash lists */
2015 struct batadv_tt_common_entry
*tt_common_entry
;
2016 struct batadv_tt_global_entry
*tt_global
;
2017 struct hlist_node
*node_tmp
;
2018 struct hlist_head
*head
;
2021 if (!bat_priv
->tt
.global_hash
)
2024 hash
= bat_priv
->tt
.global_hash
;
2026 for (i
= 0; i
< hash
->size
; i
++) {
2027 head
= &hash
->table
[i
];
2028 list_lock
= &hash
->list_locks
[i
];
2030 spin_lock_bh(list_lock
);
2031 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
2033 hlist_del_rcu(&tt_common_entry
->hash_entry
);
2034 tt_global
= container_of(tt_common_entry
,
2035 struct batadv_tt_global_entry
,
2037 batadv_tt_global_entry_put(tt_global
);
2039 spin_unlock_bh(list_lock
);
2042 batadv_hash_destroy(hash
);
2044 bat_priv
->tt
.global_hash
= NULL
;
2048 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
2049 struct batadv_tt_global_entry
*tt_global_entry
)
2053 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
2054 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
2057 /* check if the two clients are marked as isolated */
2058 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
&&
2059 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
)
2066 * batadv_transtable_search - get the mesh destination for a given client
2067 * @bat_priv: the bat priv with all the soft interface information
2068 * @src: mac address of the source client
2069 * @addr: mac address of the destination client
2070 * @vid: VLAN identifier
2072 * Return: a pointer to the originator that was selected as destination in the
2073 * mesh for contacting the client 'addr', NULL otherwise.
2074 * In case of multiple originators serving the same client, the function returns
2075 * the best one (best in terms of metric towards the destination node).
2077 * If the two clients are AP isolated the function returns NULL.
2079 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
2084 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2085 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2086 struct batadv_orig_node
*orig_node
= NULL
;
2087 struct batadv_tt_orig_list_entry
*best_entry
;
2089 if (src
&& batadv_vlan_ap_isola_get(bat_priv
, vid
)) {
2090 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
, vid
);
2091 if (!tt_local_entry
||
2092 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
))
2096 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
2097 if (!tt_global_entry
)
2100 /* check whether the clients should not communicate due to AP
2103 if (tt_local_entry
&&
2104 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2108 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
2109 /* found anything? */
2111 orig_node
= best_entry
->orig_node
;
2112 if (orig_node
&& !kref_get_unless_zero(&orig_node
->refcount
))
2117 if (tt_global_entry
)
2118 batadv_tt_global_entry_put(tt_global_entry
);
2120 batadv_tt_local_entry_put(tt_local_entry
);
2126 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2127 * to the given orig_node
2128 * @bat_priv: the bat priv with all the soft interface information
2129 * @orig_node: originator for which the CRC should be computed
2130 * @vid: VLAN identifier for which the CRC32 has to be computed
2132 * This function computes the checksum for the global table corresponding to a
2133 * specific originator. In particular, the checksum is computed as follows: For
2134 * each client connected to the originator the CRC32C of the MAC address and the
2135 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2138 * The idea behind is that CRC32C should be used as much as possible in order to
2139 * produce a unique hash of the table, but since the order which is used to feed
2140 * the CRC32C function affects the result and since every node in the network
2141 * probably sorts the clients differently, the hash function cannot be directly
2142 * computed over the entire table. Hence the CRC32C is used only on
2143 * the single client entry, while all the results are then xor'ed together
2144 * because the XOR operation can combine them all while trying to reduce the
2145 * noise as much as possible.
2147 * Return: the checksum of the global table of a given originator.
2149 static u32
batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
2150 struct batadv_orig_node
*orig_node
,
2153 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2154 struct batadv_tt_common_entry
*tt_common
;
2155 struct batadv_tt_global_entry
*tt_global
;
2156 struct hlist_head
*head
;
2157 u32 i
, crc_tmp
, crc
= 0;
2161 for (i
= 0; i
< hash
->size
; i
++) {
2162 head
= &hash
->table
[i
];
2165 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2166 tt_global
= container_of(tt_common
,
2167 struct batadv_tt_global_entry
,
2169 /* compute the CRC only for entries belonging to the
2170 * VLAN identified by the vid passed as parameter
2172 if (tt_common
->vid
!= vid
)
2175 /* Roaming clients are in the global table for
2176 * consistency only. They don't have to be
2177 * taken into account while computing the
2180 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
2182 /* Temporary clients have not been announced yet, so
2183 * they have to be skipped while computing the global
2186 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
2189 /* find out if this global entry is announced by this
2192 if (!batadv_tt_global_entry_has_orig(tt_global
,
2196 /* use network order to read the VID: this ensures that
2197 * every node reads the bytes in the same order.
2199 tmp_vid
= htons(tt_common
->vid
);
2200 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2202 /* compute the CRC on flags that have to be kept in sync
2205 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2206 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2208 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2217 * batadv_tt_local_crc - calculates the checksum of the local table
2218 * @bat_priv: the bat priv with all the soft interface information
2219 * @vid: VLAN identifier for which the CRC32 has to be computed
2221 * For details about the computation, please refer to the documentation for
2222 * batadv_tt_global_crc().
2224 * Return: the checksum of the local table
2226 static u32
batadv_tt_local_crc(struct batadv_priv
*bat_priv
,
2229 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2230 struct batadv_tt_common_entry
*tt_common
;
2231 struct hlist_head
*head
;
2232 u32 i
, crc_tmp
, crc
= 0;
2236 for (i
= 0; i
< hash
->size
; i
++) {
2237 head
= &hash
->table
[i
];
2240 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2241 /* compute the CRC only for entries belonging to the
2242 * VLAN identified by vid
2244 if (tt_common
->vid
!= vid
)
2247 /* not yet committed clients have not to be taken into
2248 * account while computing the CRC
2250 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
2253 /* use network order to read the VID: this ensures that
2254 * every node reads the bytes in the same order.
2256 tmp_vid
= htons(tt_common
->vid
);
2257 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2259 /* compute the CRC on flags that have to be kept in sync
2262 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2263 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2265 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2273 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
2275 struct batadv_tt_req_node
*node
;
2276 struct hlist_node
*safe
;
2278 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2280 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2281 hlist_del_init(&node
->list
);
2285 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2288 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
2289 struct batadv_orig_node
*orig_node
,
2290 const void *tt_buff
,
2293 /* Replace the old buffer only if I received something in the
2294 * last OGM (the OGM could carry no changes)
2296 spin_lock_bh(&orig_node
->tt_buff_lock
);
2297 if (tt_buff_len
> 0) {
2298 kfree(orig_node
->tt_buff
);
2299 orig_node
->tt_buff_len
= 0;
2300 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
2301 if (orig_node
->tt_buff
) {
2302 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
2303 orig_node
->tt_buff_len
= tt_buff_len
;
2306 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2309 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
2311 struct batadv_tt_req_node
*node
;
2312 struct hlist_node
*safe
;
2314 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2315 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2316 if (batadv_has_timed_out(node
->issued_at
,
2317 BATADV_TT_REQUEST_TIMEOUT
)) {
2318 hlist_del_init(&node
->list
);
2322 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2326 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2327 * @bat_priv: the bat priv with all the soft interface information
2328 * @orig_node: orig node this request is being issued for
2330 * Return: the pointer to the new tt_req_node struct if no request
2331 * has already been issued for this orig_node, NULL otherwise.
2333 static struct batadv_tt_req_node
*
2334 batadv_tt_req_node_new(struct batadv_priv
*bat_priv
,
2335 struct batadv_orig_node
*orig_node
)
2337 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
2339 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2340 hlist_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
2341 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
2342 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
2343 BATADV_TT_REQUEST_TIMEOUT
))
2347 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
2351 ether_addr_copy(tt_req_node
->addr
, orig_node
->orig
);
2352 tt_req_node
->issued_at
= jiffies
;
2354 hlist_add_head(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
2356 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2361 * batadv_tt_local_valid - verify that given tt entry is a valid one
2362 * @entry_ptr: to be checked local tt entry
2363 * @data_ptr: not used but definition required to satisfy the callback prototype
2365 * Return: true if the entry is a valid, false otherwise.
2367 static bool batadv_tt_local_valid(const void *entry_ptr
, const void *data_ptr
)
2369 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2371 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
2376 static bool batadv_tt_global_valid(const void *entry_ptr
,
2377 const void *data_ptr
)
2379 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2380 const struct batadv_tt_global_entry
*tt_global_entry
;
2381 const struct batadv_orig_node
*orig_node
= data_ptr
;
2383 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
2384 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
2387 tt_global_entry
= container_of(tt_common_entry
,
2388 struct batadv_tt_global_entry
,
2391 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
2395 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2397 * @bat_priv: the bat priv with all the soft interface information
2398 * @hash: hash table containing the tt entries
2399 * @tt_len: expected tvlv tt data buffer length in number of bytes
2400 * @tvlv_buff: pointer to the buffer to fill with the TT data
2401 * @valid_cb: function to filter tt change entries
2402 * @cb_data: data passed to the filter function as argument
2404 static void batadv_tt_tvlv_generate(struct batadv_priv
*bat_priv
,
2405 struct batadv_hashtable
*hash
,
2406 void *tvlv_buff
, u16 tt_len
,
2407 bool (*valid_cb
)(const void *,
2411 struct batadv_tt_common_entry
*tt_common_entry
;
2412 struct batadv_tvlv_tt_change
*tt_change
;
2413 struct hlist_head
*head
;
2414 u16 tt_tot
, tt_num_entries
= 0;
2417 tt_tot
= batadv_tt_entries(tt_len
);
2418 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_buff
;
2421 for (i
= 0; i
< hash
->size
; i
++) {
2422 head
= &hash
->table
[i
];
2424 hlist_for_each_entry_rcu(tt_common_entry
,
2426 if (tt_tot
== tt_num_entries
)
2429 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
2432 ether_addr_copy(tt_change
->addr
, tt_common_entry
->addr
);
2433 tt_change
->flags
= tt_common_entry
->flags
;
2434 tt_change
->vid
= htons(tt_common_entry
->vid
);
2435 memset(tt_change
->reserved
, 0,
2436 sizeof(tt_change
->reserved
));
2446 * batadv_tt_global_check_crc - check if all the CRCs are correct
2447 * @orig_node: originator for which the CRCs have to be checked
2448 * @tt_vlan: pointer to the first tvlv VLAN entry
2449 * @num_vlan: number of tvlv VLAN entries
2451 * Return: true if all the received CRCs match the locally stored ones, false
2454 static bool batadv_tt_global_check_crc(struct batadv_orig_node
*orig_node
,
2455 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2458 struct batadv_tvlv_tt_vlan_data
*tt_vlan_tmp
;
2459 struct batadv_orig_node_vlan
*vlan
;
2460 int i
, orig_num_vlan
;
2463 /* check if each received CRC matches the locally stored one */
2464 for (i
= 0; i
< num_vlan
; i
++) {
2465 tt_vlan_tmp
= tt_vlan
+ i
;
2467 /* if orig_node is a backbone node for this VLAN, don't check
2468 * the CRC as we ignore all the global entries over it
2470 if (batadv_bla_is_backbone_gw_orig(orig_node
->bat_priv
,
2472 ntohs(tt_vlan_tmp
->vid
)))
2475 vlan
= batadv_orig_node_vlan_get(orig_node
,
2476 ntohs(tt_vlan_tmp
->vid
));
2481 batadv_orig_node_vlan_put(vlan
);
2483 if (crc
!= ntohl(tt_vlan_tmp
->crc
))
2487 /* check if any excess VLANs exist locally for the originator
2488 * which are not mentioned in the TVLV from the originator.
2492 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
)
2496 if (orig_num_vlan
> num_vlan
)
2503 * batadv_tt_local_update_crc - update all the local CRCs
2504 * @bat_priv: the bat priv with all the soft interface information
2506 static void batadv_tt_local_update_crc(struct batadv_priv
*bat_priv
)
2508 struct batadv_softif_vlan
*vlan
;
2510 /* recompute the global CRC for each VLAN */
2512 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
2513 vlan
->tt
.crc
= batadv_tt_local_crc(bat_priv
, vlan
->vid
);
2519 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2520 * @bat_priv: the bat priv with all the soft interface information
2521 * @orig_node: the orig_node for which the CRCs have to be updated
2523 static void batadv_tt_global_update_crc(struct batadv_priv
*bat_priv
,
2524 struct batadv_orig_node
*orig_node
)
2526 struct batadv_orig_node_vlan
*vlan
;
2529 /* recompute the global CRC for each VLAN */
2531 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
2532 /* if orig_node is a backbone node for this VLAN, don't compute
2533 * the CRC as we ignore all the global entries over it
2535 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
,
2539 crc
= batadv_tt_global_crc(bat_priv
, orig_node
, vlan
->vid
);
2546 * batadv_send_tt_request - send a TT Request message to a given node
2547 * @bat_priv: the bat priv with all the soft interface information
2548 * @dst_orig_node: the destination of the message
2549 * @ttvn: the version number that the source of the message is looking for
2550 * @tt_vlan: pointer to the first tvlv VLAN object to request
2551 * @num_vlan: number of tvlv VLAN entries
2552 * @full_table: ask for the entire translation table if true, while only for the
2553 * last TT diff otherwise
2555 * Return: true if the TT Request was sent, false otherwise
2557 static bool batadv_send_tt_request(struct batadv_priv
*bat_priv
,
2558 struct batadv_orig_node
*dst_orig_node
,
2560 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2561 u16 num_vlan
, bool full_table
)
2563 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2564 struct batadv_tt_req_node
*tt_req_node
= NULL
;
2565 struct batadv_tvlv_tt_vlan_data
*tt_vlan_req
;
2566 struct batadv_hard_iface
*primary_if
;
2570 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2574 /* The new tt_req will be issued only if I'm not waiting for a
2575 * reply from the same orig_node yet
2577 tt_req_node
= batadv_tt_req_node_new(bat_priv
, dst_orig_node
);
2581 size
= sizeof(*tvlv_tt_data
) + sizeof(*tt_vlan_req
) * num_vlan
;
2582 tvlv_tt_data
= kzalloc(size
, GFP_ATOMIC
);
2586 tvlv_tt_data
->flags
= BATADV_TT_REQUEST
;
2587 tvlv_tt_data
->ttvn
= ttvn
;
2588 tvlv_tt_data
->num_vlan
= htons(num_vlan
);
2590 /* send all the CRCs within the request. This is needed by intermediate
2591 * nodes to ensure they have the correct table before replying
2593 tt_vlan_req
= (struct batadv_tvlv_tt_vlan_data
*)(tvlv_tt_data
+ 1);
2594 for (i
= 0; i
< num_vlan
; i
++) {
2595 tt_vlan_req
->vid
= tt_vlan
->vid
;
2596 tt_vlan_req
->crc
= tt_vlan
->crc
;
2603 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2605 batadv_dbg(BATADV_DBG_TT
, bat_priv
, "Sending TT_REQUEST to %pM [%c]\n",
2606 dst_orig_node
->orig
, full_table
? 'F' : '.');
2608 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
2609 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2610 dst_orig_node
->orig
, BATADV_TVLV_TT
, 1,
2611 tvlv_tt_data
, size
);
2616 batadv_hardif_put(primary_if
);
2617 if (ret
&& tt_req_node
) {
2618 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2619 /* hlist_del_init() verifies tt_req_node still is in the list */
2620 hlist_del_init(&tt_req_node
->list
);
2621 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2624 kfree(tvlv_tt_data
);
2629 * batadv_send_other_tt_response - send reply to tt request concerning another
2630 * node's translation table
2631 * @bat_priv: the bat priv with all the soft interface information
2632 * @tt_data: tt data containing the tt request information
2633 * @req_src: mac address of tt request sender
2634 * @req_dst: mac address of tt request recipient
2636 * Return: true if tt request reply was sent, false otherwise.
2638 static bool batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
2639 struct batadv_tvlv_tt_data
*tt_data
,
2640 u8
*req_src
, u8
*req_dst
)
2642 struct batadv_orig_node
*req_dst_orig_node
;
2643 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
2644 struct batadv_tvlv_tt_change
*tt_change
;
2645 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2646 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
2647 bool ret
= false, full_table
;
2648 u8 orig_ttvn
, req_ttvn
;
2652 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2653 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2654 req_src
, tt_data
->ttvn
, req_dst
,
2655 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2657 /* Let's get the orig node of the REAL destination */
2658 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_dst
);
2659 if (!req_dst_orig_node
)
2662 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2663 if (!res_dst_orig_node
)
2666 orig_ttvn
= (u8
)atomic_read(&req_dst_orig_node
->last_ttvn
);
2667 req_ttvn
= tt_data
->ttvn
;
2669 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
2670 /* this node doesn't have the requested data */
2671 if (orig_ttvn
!= req_ttvn
||
2672 !batadv_tt_global_check_crc(req_dst_orig_node
, tt_vlan
,
2673 ntohs(tt_data
->num_vlan
)))
2676 /* If the full table has been explicitly requested */
2677 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
||
2678 !req_dst_orig_node
->tt_buff
)
2683 /* TT fragmentation hasn't been implemented yet, so send as many
2684 * TT entries fit a single packet as possible only
2687 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
2688 tt_len
= req_dst_orig_node
->tt_buff_len
;
2690 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2697 /* Copy the last orig_node's OGM buffer */
2698 memcpy(tt_change
, req_dst_orig_node
->tt_buff
,
2699 req_dst_orig_node
->tt_buff_len
);
2700 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2702 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2703 * in the initial part
2706 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2713 /* fill the rest of the tvlv with the real TT entries */
2714 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.global_hash
,
2716 batadv_tt_global_valid
,
2720 /* Don't send the response, if larger than fragmented packet. */
2721 tt_len
= sizeof(struct batadv_unicast_tvlv_packet
) + tvlv_len
;
2722 if (tt_len
> atomic_read(&bat_priv
->packet_size_max
)) {
2723 net_ratelimited_function(batadv_info
, bat_priv
->soft_iface
,
2724 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2725 res_dst_orig_node
->orig
);
2729 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2730 tvlv_tt_data
->ttvn
= req_ttvn
;
2733 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2735 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2736 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2737 res_dst_orig_node
->orig
, req_dst_orig_node
->orig
,
2738 full_table
? 'F' : '.', req_ttvn
);
2740 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2742 batadv_tvlv_unicast_send(bat_priv
, req_dst_orig_node
->orig
,
2743 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2750 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2753 if (res_dst_orig_node
)
2754 batadv_orig_node_put(res_dst_orig_node
);
2755 if (req_dst_orig_node
)
2756 batadv_orig_node_put(req_dst_orig_node
);
2757 kfree(tvlv_tt_data
);
2762 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2764 * @bat_priv: the bat priv with all the soft interface information
2765 * @tt_data: tt data containing the tt request information
2766 * @req_src: mac address of tt request sender
2768 * Return: true if tt request reply was sent, false otherwise.
2770 static bool batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
2771 struct batadv_tvlv_tt_data
*tt_data
,
2774 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2775 struct batadv_hard_iface
*primary_if
= NULL
;
2776 struct batadv_tvlv_tt_change
*tt_change
;
2777 struct batadv_orig_node
*orig_node
;
2778 u8 my_ttvn
, req_ttvn
;
2783 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2784 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2785 req_src
, tt_data
->ttvn
,
2786 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2788 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
2790 my_ttvn
= (u8
)atomic_read(&bat_priv
->tt
.vn
);
2791 req_ttvn
= tt_data
->ttvn
;
2793 orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2797 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2801 /* If the full table has been explicitly requested or the gap
2802 * is too big send the whole local translation table
2804 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
2805 !bat_priv
->tt
.last_changeset
)
2810 /* TT fragmentation hasn't been implemented yet, so send as many
2811 * TT entries fit a single packet as possible only
2814 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
2816 tt_len
= bat_priv
->tt
.last_changeset_len
;
2817 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2824 /* Copy the last orig_node's OGM buffer */
2825 memcpy(tt_change
, bat_priv
->tt
.last_changeset
,
2826 bat_priv
->tt
.last_changeset_len
);
2827 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2829 req_ttvn
= (u8
)atomic_read(&bat_priv
->tt
.vn
);
2831 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2832 * in the initial part
2835 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2842 /* fill the rest of the tvlv with the real TT entries */
2843 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.local_hash
,
2845 batadv_tt_local_valid
, NULL
);
2848 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2849 tvlv_tt_data
->ttvn
= req_ttvn
;
2852 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2854 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2855 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2856 orig_node
->orig
, full_table
? 'F' : '.', req_ttvn
);
2858 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2860 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2861 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2867 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2869 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
2871 batadv_orig_node_put(orig_node
);
2873 batadv_hardif_put(primary_if
);
2874 kfree(tvlv_tt_data
);
2875 /* The packet was for this host, so it doesn't need to be re-routed */
2880 * batadv_send_tt_response - send reply to tt request
2881 * @bat_priv: the bat priv with all the soft interface information
2882 * @tt_data: tt data containing the tt request information
2883 * @req_src: mac address of tt request sender
2884 * @req_dst: mac address of tt request recipient
2886 * Return: true if tt request reply was sent, false otherwise.
2888 static bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
2889 struct batadv_tvlv_tt_data
*tt_data
,
2890 u8
*req_src
, u8
*req_dst
)
2892 if (batadv_is_my_mac(bat_priv
, req_dst
))
2893 return batadv_send_my_tt_response(bat_priv
, tt_data
, req_src
);
2894 return batadv_send_other_tt_response(bat_priv
, tt_data
, req_src
,
2898 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2899 struct batadv_orig_node
*orig_node
,
2900 struct batadv_tvlv_tt_change
*tt_change
,
2901 u16 tt_num_changes
, u8 ttvn
)
2906 for (i
= 0; i
< tt_num_changes
; i
++) {
2907 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
2908 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
2909 batadv_tt_global_del(bat_priv
, orig_node
,
2910 (tt_change
+ i
)->addr
,
2911 ntohs((tt_change
+ i
)->vid
),
2912 "tt removed by changes",
2915 if (!batadv_tt_global_add(bat_priv
, orig_node
,
2916 (tt_change
+ i
)->addr
,
2917 ntohs((tt_change
+ i
)->vid
),
2918 (tt_change
+ i
)->flags
, ttvn
))
2919 /* In case of problem while storing a
2920 * global_entry, we stop the updating
2921 * procedure without committing the
2922 * ttvn change. This will avoid to send
2923 * corrupted data on tt_request
2928 set_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
2931 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
2932 struct batadv_tvlv_tt_change
*tt_change
,
2933 u8 ttvn
, u8
*resp_src
,
2936 struct batadv_orig_node
*orig_node
;
2938 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2942 /* Purge the old table first.. */
2943 batadv_tt_global_del_orig(bat_priv
, orig_node
, -1,
2944 "Received full table");
2946 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
, num_entries
,
2949 spin_lock_bh(&orig_node
->tt_buff_lock
);
2950 kfree(orig_node
->tt_buff
);
2951 orig_node
->tt_buff_len
= 0;
2952 orig_node
->tt_buff
= NULL
;
2953 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2955 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2959 batadv_orig_node_put(orig_node
);
2962 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2963 struct batadv_orig_node
*orig_node
,
2964 u16 tt_num_changes
, u8 ttvn
,
2965 struct batadv_tvlv_tt_change
*tt_change
)
2967 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
2968 tt_num_changes
, ttvn
);
2970 batadv_tt_save_orig_buffer(bat_priv
, orig_node
, tt_change
,
2971 batadv_tt_len(tt_num_changes
));
2972 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2976 * batadv_is_my_client - check if a client is served by the local node
2977 * @bat_priv: the bat priv with all the soft interface information
2978 * @addr: the mac address of the client to check
2979 * @vid: VLAN identifier
2981 * Return: true if the client is served by this node, false otherwise.
2983 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const u8
*addr
,
2986 struct batadv_tt_local_entry
*tt_local_entry
;
2989 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
2990 if (!tt_local_entry
)
2992 /* Check if the client has been logically deleted (but is kept for
2993 * consistency purpose)
2995 if ((tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
) ||
2996 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
3001 batadv_tt_local_entry_put(tt_local_entry
);
3006 * batadv_handle_tt_response - process incoming tt reply
3007 * @bat_priv: the bat priv with all the soft interface information
3008 * @tt_data: tt data containing the tt request information
3009 * @resp_src: mac address of tt reply sender
3010 * @num_entries: number of tt change entries appended to the tt data
3012 static void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
3013 struct batadv_tvlv_tt_data
*tt_data
,
3014 u8
*resp_src
, u16 num_entries
)
3016 struct batadv_tt_req_node
*node
;
3017 struct hlist_node
*safe
;
3018 struct batadv_orig_node
*orig_node
= NULL
;
3019 struct batadv_tvlv_tt_change
*tt_change
;
3020 u8
*tvlv_ptr
= (u8
*)tt_data
;
3023 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3024 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3025 resp_src
, tt_data
->ttvn
, num_entries
,
3026 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
3028 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
3032 spin_lock_bh(&orig_node
->tt_lock
);
3034 change_offset
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3035 change_offset
*= ntohs(tt_data
->num_vlan
);
3036 change_offset
+= sizeof(*tt_data
);
3037 tvlv_ptr
+= change_offset
;
3039 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_ptr
;
3040 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
) {
3041 batadv_tt_fill_gtable(bat_priv
, tt_change
, tt_data
->ttvn
,
3042 resp_src
, num_entries
);
3044 batadv_tt_update_changes(bat_priv
, orig_node
, num_entries
,
3045 tt_data
->ttvn
, tt_change
);
3048 /* Recalculate the CRC for this orig_node and store it */
3049 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3051 spin_unlock_bh(&orig_node
->tt_lock
);
3053 /* Delete the tt_req_node from pending tt_requests list */
3054 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
3055 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
3056 if (!batadv_compare_eth(node
->addr
, resp_src
))
3058 hlist_del_init(&node
->list
);
3062 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
3065 batadv_orig_node_put(orig_node
);
3068 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
3070 struct batadv_tt_roam_node
*node
, *safe
;
3072 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3074 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
3075 list_del(&node
->list
);
3079 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3082 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
3084 struct batadv_tt_roam_node
*node
, *safe
;
3086 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3087 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
3088 if (!batadv_has_timed_out(node
->first_time
,
3089 BATADV_ROAMING_MAX_TIME
))
3092 list_del(&node
->list
);
3095 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3099 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3100 * @bat_priv: the bat priv with all the soft interface information
3101 * @client: mac address of the roaming client
3103 * This function checks whether the client already reached the
3104 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3107 * Return: true if the ROAMING_ADV can be sent, false otherwise
3109 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
, u8
*client
)
3111 struct batadv_tt_roam_node
*tt_roam_node
;
3114 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3115 /* The new tt_req will be issued only if I'm not waiting for a
3116 * reply from the same orig_node yet
3118 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
3119 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
3122 if (batadv_has_timed_out(tt_roam_node
->first_time
,
3123 BATADV_ROAMING_MAX_TIME
))
3126 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
3127 /* Sorry, you roamed too many times! */
3134 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
3138 tt_roam_node
->first_time
= jiffies
;
3139 atomic_set(&tt_roam_node
->counter
,
3140 BATADV_ROAMING_MAX_COUNT
- 1);
3141 ether_addr_copy(tt_roam_node
->addr
, client
);
3143 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
3148 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3153 * batadv_send_roam_adv - send a roaming advertisement message
3154 * @bat_priv: the bat priv with all the soft interface information
3155 * @client: mac address of the roaming client
3156 * @vid: VLAN identifier
3157 * @orig_node: message destination
3159 * Send a ROAMING_ADV message to the node which was previously serving this
3160 * client. This is done to inform the node that from now on all traffic destined
3161 * for this particular roamed client has to be forwarded to the sender of the
3164 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, u8
*client
,
3166 struct batadv_orig_node
*orig_node
)
3168 struct batadv_hard_iface
*primary_if
;
3169 struct batadv_tvlv_roam_adv tvlv_roam
;
3171 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3175 /* before going on we have to check whether the client has
3176 * already roamed to us too many times
3178 if (!batadv_tt_check_roam_count(bat_priv
, client
))
3181 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3182 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3183 orig_node
->orig
, client
, BATADV_PRINT_VID(vid
));
3185 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
3187 memcpy(tvlv_roam
.client
, client
, sizeof(tvlv_roam
.client
));
3188 tvlv_roam
.vid
= htons(vid
);
3190 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3191 orig_node
->orig
, BATADV_TVLV_ROAM
, 1,
3192 &tvlv_roam
, sizeof(tvlv_roam
));
3196 batadv_hardif_put(primary_if
);
3199 static void batadv_tt_purge(struct work_struct
*work
)
3201 struct delayed_work
*delayed_work
;
3202 struct batadv_priv_tt
*priv_tt
;
3203 struct batadv_priv
*bat_priv
;
3205 delayed_work
= to_delayed_work(work
);
3206 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
3207 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
3209 batadv_tt_local_purge(bat_priv
, BATADV_TT_LOCAL_TIMEOUT
);
3210 batadv_tt_global_purge(bat_priv
);
3211 batadv_tt_req_purge(bat_priv
);
3212 batadv_tt_roam_purge(bat_priv
);
3214 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3215 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3218 void batadv_tt_free(struct batadv_priv
*bat_priv
)
3220 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3221 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3223 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
3225 batadv_tt_local_table_free(bat_priv
);
3226 batadv_tt_global_table_free(bat_priv
);
3227 batadv_tt_req_list_free(bat_priv
);
3228 batadv_tt_changes_list_free(bat_priv
);
3229 batadv_tt_roam_list_free(bat_priv
);
3231 kfree(bat_priv
->tt
.last_changeset
);
3235 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3236 * table and possibly count them in the TT size
3237 * @bat_priv: the bat priv with all the soft interface information
3238 * @flags: the flag to switch
3239 * @enable: whether to set or unset the flag
3240 * @count: whether to increase the TT size by the number of changed entries
3242 static void batadv_tt_local_set_flags(struct batadv_priv
*bat_priv
, u16 flags
,
3243 bool enable
, bool count
)
3245 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3246 struct batadv_tt_common_entry
*tt_common_entry
;
3247 u16 changed_num
= 0;
3248 struct hlist_head
*head
;
3254 for (i
= 0; i
< hash
->size
; i
++) {
3255 head
= &hash
->table
[i
];
3258 hlist_for_each_entry_rcu(tt_common_entry
,
3261 if ((tt_common_entry
->flags
& flags
) == flags
)
3263 tt_common_entry
->flags
|= flags
;
3265 if (!(tt_common_entry
->flags
& flags
))
3267 tt_common_entry
->flags
&= ~flags
;
3274 batadv_tt_local_size_inc(bat_priv
,
3275 tt_common_entry
->vid
);
3281 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3282 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
3284 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3285 struct batadv_tt_common_entry
*tt_common
;
3286 struct batadv_tt_local_entry
*tt_local
;
3287 struct hlist_node
*node_tmp
;
3288 struct hlist_head
*head
;
3289 spinlock_t
*list_lock
; /* protects write access to the hash lists */
3295 for (i
= 0; i
< hash
->size
; i
++) {
3296 head
= &hash
->table
[i
];
3297 list_lock
= &hash
->list_locks
[i
];
3299 spin_lock_bh(list_lock
);
3300 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
3302 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
3305 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3306 "Deleting local tt entry (%pM, vid: %d): pending\n",
3308 BATADV_PRINT_VID(tt_common
->vid
));
3310 batadv_tt_local_size_dec(bat_priv
, tt_common
->vid
);
3311 hlist_del_rcu(&tt_common
->hash_entry
);
3312 tt_local
= container_of(tt_common
,
3313 struct batadv_tt_local_entry
,
3316 batadv_tt_local_entry_put(tt_local
);
3318 spin_unlock_bh(list_lock
);
3323 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3324 * which have been queued in the time since the last commit
3325 * @bat_priv: the bat priv with all the soft interface information
3327 * Caller must hold tt->commit_lock.
3329 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv
*bat_priv
)
3331 lockdep_assert_held(&bat_priv
->tt
.commit_lock
);
3333 /* Update multicast addresses in local translation table */
3334 batadv_mcast_mla_update(bat_priv
);
3336 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1) {
3337 if (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))
3338 batadv_tt_tvlv_container_update(bat_priv
);
3342 batadv_tt_local_set_flags(bat_priv
, BATADV_TT_CLIENT_NEW
, false, true);
3344 batadv_tt_local_purge_pending_clients(bat_priv
);
3345 batadv_tt_local_update_crc(bat_priv
);
3347 /* Increment the TTVN only once per OGM interval */
3348 atomic_inc(&bat_priv
->tt
.vn
);
3349 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3350 "Local changes committed, updating to ttvn %u\n",
3351 (u8
)atomic_read(&bat_priv
->tt
.vn
));
3353 /* reset the sending counter */
3354 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
3355 batadv_tt_tvlv_container_update(bat_priv
);
3359 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3360 * have been queued in the time since the last commit
3361 * @bat_priv: the bat priv with all the soft interface information
3363 void batadv_tt_local_commit_changes(struct batadv_priv
*bat_priv
)
3365 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3366 batadv_tt_local_commit_changes_nolock(bat_priv
);
3367 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3370 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, u8
*src
, u8
*dst
,
3373 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
3374 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
3375 struct batadv_softif_vlan
*vlan
;
3378 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
3382 if (!atomic_read(&vlan
->ap_isolation
))
3385 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
, vid
);
3386 if (!tt_local_entry
)
3389 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
, vid
);
3390 if (!tt_global_entry
)
3393 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
3399 batadv_softif_vlan_put(vlan
);
3400 if (tt_global_entry
)
3401 batadv_tt_global_entry_put(tt_global_entry
);
3403 batadv_tt_local_entry_put(tt_local_entry
);
3408 * batadv_tt_update_orig - update global translation table with new tt
3409 * information received via ogms
3410 * @bat_priv: the bat priv with all the soft interface information
3411 * @orig_node: the orig_node of the ogm
3412 * @tt_buff: pointer to the first tvlv VLAN entry
3413 * @tt_num_vlan: number of tvlv VLAN entries
3414 * @tt_change: pointer to the first entry in the TT buffer
3415 * @tt_num_changes: number of tt changes inside the tt buffer
3416 * @ttvn: translation table version number of this changeset
3418 static void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
3419 struct batadv_orig_node
*orig_node
,
3420 const void *tt_buff
, u16 tt_num_vlan
,
3421 struct batadv_tvlv_tt_change
*tt_change
,
3422 u16 tt_num_changes
, u8 ttvn
)
3424 u8 orig_ttvn
= (u8
)atomic_read(&orig_node
->last_ttvn
);
3425 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3426 bool full_table
= true;
3429 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)tt_buff
;
3430 has_tt_init
= test_bit(BATADV_ORIG_CAPA_HAS_TT
,
3431 &orig_node
->capa_initialized
);
3433 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3434 * increased by one -> we can apply the attached changes
3436 if ((!has_tt_init
&& ttvn
== 1) || ttvn
- orig_ttvn
== 1) {
3437 /* the OGM could not contain the changes due to their size or
3438 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3440 * In this case send a tt request
3442 if (!tt_num_changes
) {
3447 spin_lock_bh(&orig_node
->tt_lock
);
3449 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
3452 /* Even if we received the precomputed crc with the OGM, we
3453 * prefer to recompute it to spot any possible inconsistency
3454 * in the global table
3456 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3458 spin_unlock_bh(&orig_node
->tt_lock
);
3460 /* The ttvn alone is not enough to guarantee consistency
3461 * because a single value could represent different states
3462 * (due to the wrap around). Thus a node has to check whether
3463 * the resulting table (after applying the changes) is still
3464 * consistent or not. E.g. a node could disconnect while its
3465 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3466 * checking the CRC value is mandatory to detect the
3469 if (!batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3473 /* if we missed more than one change or our tables are not
3474 * in sync anymore -> request fresh tt data
3476 if (!has_tt_init
|| ttvn
!= orig_ttvn
||
3477 !batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3480 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3481 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3482 orig_node
->orig
, ttvn
, orig_ttvn
,
3484 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
3485 tt_vlan
, tt_num_vlan
,
3493 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3494 * @bat_priv: the bat priv with all the soft interface information
3495 * @addr: the mac address of the client to check
3496 * @vid: VLAN identifier
3498 * Return: true if we know that the client has moved from its old originator
3499 * to another one. This entry is still kept for consistency purposes and will be
3500 * deleted later by a DEL or because of timeout
3502 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
3503 u8
*addr
, unsigned short vid
)
3505 struct batadv_tt_global_entry
*tt_global_entry
;
3508 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3509 if (!tt_global_entry
)
3512 ret
= tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3513 batadv_tt_global_entry_put(tt_global_entry
);
3519 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3520 * @bat_priv: the bat priv with all the soft interface information
3521 * @addr: the mac address of the local client to query
3522 * @vid: VLAN identifier
3524 * Return: true if the local client is known to be roaming (it is not served by
3525 * this node anymore) or not. If yes, the client is still present in the table
3526 * to keep the latter consistent with the node TTVN
3528 bool batadv_tt_local_client_is_roaming(struct batadv_priv
*bat_priv
,
3529 u8
*addr
, unsigned short vid
)
3531 struct batadv_tt_local_entry
*tt_local_entry
;
3534 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
3535 if (!tt_local_entry
)
3538 ret
= tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3539 batadv_tt_local_entry_put(tt_local_entry
);
3544 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
3545 struct batadv_orig_node
*orig_node
,
3546 const unsigned char *addr
,
3551 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
, vid
,
3552 BATADV_TT_CLIENT_TEMP
,
3553 atomic_read(&orig_node
->last_ttvn
)))
3556 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3557 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3558 addr
, BATADV_PRINT_VID(vid
), orig_node
->orig
);
3565 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3566 * maximum packet size that can be transported through the mesh
3567 * @soft_iface: netdev struct of the mesh interface
3569 * Remove entries older than 'timeout' and half timeout if more entries need
3572 void batadv_tt_local_resize_to_mtu(struct net_device
*soft_iface
)
3574 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
3575 int packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
3576 int table_size
, timeout
= BATADV_TT_LOCAL_TIMEOUT
/ 2;
3577 bool reduced
= false;
3579 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3582 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
3583 if (packet_size_max
>= table_size
)
3586 batadv_tt_local_purge(bat_priv
, timeout
);
3587 batadv_tt_local_purge_pending_clients(bat_priv
);
3591 net_ratelimited_function(batadv_info
, soft_iface
,
3592 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3596 /* commit these changes immediately, to avoid synchronization problem
3600 batadv_tt_local_commit_changes_nolock(bat_priv
);
3602 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3606 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3607 * @bat_priv: the bat priv with all the soft interface information
3608 * @orig: the orig_node of the ogm
3609 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3610 * @tvlv_value: tvlv buffer containing the gateway data
3611 * @tvlv_value_len: tvlv buffer length
3613 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
3614 struct batadv_orig_node
*orig
,
3615 u8 flags
, void *tvlv_value
,
3618 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3619 struct batadv_tvlv_tt_change
*tt_change
;
3620 struct batadv_tvlv_tt_data
*tt_data
;
3621 u16 num_entries
, num_vlan
;
3623 if (tvlv_value_len
< sizeof(*tt_data
))
3626 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3627 tvlv_value_len
-= sizeof(*tt_data
);
3629 num_vlan
= ntohs(tt_data
->num_vlan
);
3631 if (tvlv_value_len
< sizeof(*tt_vlan
) * num_vlan
)
3634 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
3635 tt_change
= (struct batadv_tvlv_tt_change
*)(tt_vlan
+ num_vlan
);
3636 tvlv_value_len
-= sizeof(*tt_vlan
) * num_vlan
;
3638 num_entries
= batadv_tt_entries(tvlv_value_len
);
3640 batadv_tt_update_orig(bat_priv
, orig
, tt_vlan
, num_vlan
, tt_change
,
3641 num_entries
, tt_data
->ttvn
);
3645 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3647 * @bat_priv: the bat priv with all the soft interface information
3648 * @src: mac address of tt tvlv sender
3649 * @dst: mac address of tt tvlv recipient
3650 * @tvlv_value: tvlv buffer containing the tt data
3651 * @tvlv_value_len: tvlv buffer length
3653 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3656 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3661 struct batadv_tvlv_tt_data
*tt_data
;
3662 u16 tt_vlan_len
, tt_num_entries
;
3666 if (tvlv_value_len
< sizeof(*tt_data
))
3667 return NET_RX_SUCCESS
;
3669 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3670 tvlv_value_len
-= sizeof(*tt_data
);
3672 tt_vlan_len
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3673 tt_vlan_len
*= ntohs(tt_data
->num_vlan
);
3675 if (tvlv_value_len
< tt_vlan_len
)
3676 return NET_RX_SUCCESS
;
3678 tvlv_value_len
-= tt_vlan_len
;
3679 tt_num_entries
= batadv_tt_entries(tvlv_value_len
);
3681 switch (tt_data
->flags
& BATADV_TT_DATA_TYPE_MASK
) {
3682 case BATADV_TT_REQUEST
:
3683 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
3685 /* If this node cannot provide a TT response the tt_request is
3688 ret
= batadv_send_tt_response(bat_priv
, tt_data
, src
, dst
);
3690 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3695 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3696 "Routing TT_REQUEST to %pM [%c]\n",
3698 /* tvlv API will re-route the packet */
3702 case BATADV_TT_RESPONSE
:
3703 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
3705 if (batadv_is_my_mac(bat_priv
, dst
)) {
3706 batadv_handle_tt_response(bat_priv
, tt_data
,
3707 src
, tt_num_entries
);
3708 return NET_RX_SUCCESS
;
3711 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3716 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3717 "Routing TT_RESPONSE to %pM [%c]\n", dst
, tt_flag
);
3719 /* tvlv API will re-route the packet */
3723 return NET_RX_SUCCESS
;
3727 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3728 * @bat_priv: the bat priv with all the soft interface information
3729 * @src: mac address of tt tvlv sender
3730 * @dst: mac address of tt tvlv recipient
3731 * @tvlv_value: tvlv buffer containing the tt data
3732 * @tvlv_value_len: tvlv buffer length
3734 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3737 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3742 struct batadv_tvlv_roam_adv
*roaming_adv
;
3743 struct batadv_orig_node
*orig_node
= NULL
;
3745 /* If this node is not the intended recipient of the
3746 * roaming advertisement the packet is forwarded
3747 * (the tvlv API will re-route the packet).
3749 if (!batadv_is_my_mac(bat_priv
, dst
))
3752 if (tvlv_value_len
< sizeof(*roaming_adv
))
3755 orig_node
= batadv_orig_hash_find(bat_priv
, src
);
3759 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
3760 roaming_adv
= (struct batadv_tvlv_roam_adv
*)tvlv_value
;
3762 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3763 "Received ROAMING_ADV from %pM (client %pM)\n",
3764 src
, roaming_adv
->client
);
3766 batadv_tt_global_add(bat_priv
, orig_node
, roaming_adv
->client
,
3767 ntohs(roaming_adv
->vid
), BATADV_TT_CLIENT_ROAM
,
3768 atomic_read(&orig_node
->last_ttvn
) + 1);
3772 batadv_orig_node_put(orig_node
);
3773 return NET_RX_SUCCESS
;
3777 * batadv_tt_init - initialise the translation table internals
3778 * @bat_priv: the bat priv with all the soft interface information
3780 * Return: 0 on success or negative error number in case of failure.
3782 int batadv_tt_init(struct batadv_priv
*bat_priv
)
3786 /* synchronized flags must be remote */
3787 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK
& BATADV_TT_REMOTE_MASK
));
3789 ret
= batadv_tt_local_init(bat_priv
);
3793 ret
= batadv_tt_global_init(bat_priv
);
3797 batadv_tvlv_handler_register(bat_priv
, batadv_tt_tvlv_ogm_handler_v1
,
3798 batadv_tt_tvlv_unicast_handler_v1
,
3799 BATADV_TVLV_TT
, 1, BATADV_NO_FLAGS
);
3801 batadv_tvlv_handler_register(bat_priv
, NULL
,
3802 batadv_roam_tvlv_unicast_handler_v1
,
3803 BATADV_TVLV_ROAM
, 1, BATADV_NO_FLAGS
);
3805 INIT_DELAYED_WORK(&bat_priv
->tt
.work
, batadv_tt_purge
);
3806 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3807 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3813 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3814 * @bat_priv: the bat priv with all the soft interface information
3815 * @addr: the mac address of the client
3816 * @vid: the identifier of the VLAN where this client is connected
3818 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
3821 bool batadv_tt_global_is_isolated(struct batadv_priv
*bat_priv
,
3822 const u8
*addr
, unsigned short vid
)
3824 struct batadv_tt_global_entry
*tt
;
3827 tt
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3831 ret
= tt
->common
.flags
& BATADV_TT_CLIENT_ISOLA
;
3833 batadv_tt_global_entry_put(tt
);