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>
47 #include "bridge_loop_avoidance.h"
48 #include "hard-interface.h"
50 #include "multicast.h"
51 #include "originator.h"
53 #include "soft-interface.h"
56 static struct lock_class_key batadv_tt_local_hash_lock_class_key
;
57 static struct lock_class_key batadv_tt_global_hash_lock_class_key
;
59 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, u8
*client
,
61 struct batadv_orig_node
*orig_node
);
62 static void batadv_tt_purge(struct work_struct
*work
);
64 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
);
65 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
66 struct batadv_orig_node
*orig_node
,
67 const unsigned char *addr
,
68 unsigned short vid
, const char *message
,
72 * batadv_compare_tt - check if two TT entries are the same
73 * @node: the list element pointer of the first TT entry
74 * @data2: pointer to the tt_common_entry of the second TT entry
76 * Compare the MAC address and the VLAN ID of the two TT entries and check if
77 * they are the same TT client.
78 * Return: true if the two TT clients are the same, false otherwise
80 static bool batadv_compare_tt(const struct hlist_node
*node
, const void *data2
)
82 const void *data1
= container_of(node
, struct batadv_tt_common_entry
,
84 const struct batadv_tt_common_entry
*tt1
= data1
;
85 const struct batadv_tt_common_entry
*tt2
= data2
;
87 return (tt1
->vid
== tt2
->vid
) && batadv_compare_eth(data1
, data2
);
91 * batadv_choose_tt - return the index of the tt entry in the hash table
92 * @data: pointer to the tt_common_entry object to map
93 * @size: the size of the hash table
95 * Return: the hash index where the object represented by 'data' should be
98 static inline u32
batadv_choose_tt(const void *data
, u32 size
)
100 struct batadv_tt_common_entry
*tt
;
103 tt
= (struct batadv_tt_common_entry
*)data
;
104 hash
= jhash(&tt
->addr
, ETH_ALEN
, hash
);
105 hash
= jhash(&tt
->vid
, sizeof(tt
->vid
), hash
);
111 * batadv_tt_hash_find - look for a client in the given hash table
112 * @hash: the hash table to search
113 * @addr: the mac address of the client to look for
114 * @vid: VLAN identifier
116 * Return: a pointer to the tt_common struct belonging to the searched client if
117 * found, NULL otherwise.
119 static struct batadv_tt_common_entry
*
120 batadv_tt_hash_find(struct batadv_hashtable
*hash
, const u8
*addr
,
123 struct hlist_head
*head
;
124 struct batadv_tt_common_entry to_search
, *tt
, *tt_tmp
= NULL
;
130 ether_addr_copy(to_search
.addr
, addr
);
133 index
= batadv_choose_tt(&to_search
, hash
->size
);
134 head
= &hash
->table
[index
];
137 hlist_for_each_entry_rcu(tt
, head
, hash_entry
) {
138 if (!batadv_compare_eth(tt
, addr
))
144 if (!kref_get_unless_zero(&tt
->refcount
))
156 * batadv_tt_local_hash_find - search the local table for a given client
157 * @bat_priv: the bat priv with all the soft interface information
158 * @addr: the mac address of the client to look for
159 * @vid: VLAN identifier
161 * Return: a pointer to the corresponding tt_local_entry struct if the client is
162 * found, NULL otherwise.
164 static struct batadv_tt_local_entry
*
165 batadv_tt_local_hash_find(struct batadv_priv
*bat_priv
, const u8
*addr
,
168 struct batadv_tt_common_entry
*tt_common_entry
;
169 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
171 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.local_hash
, addr
,
174 tt_local_entry
= container_of(tt_common_entry
,
175 struct batadv_tt_local_entry
,
177 return tt_local_entry
;
181 * batadv_tt_global_hash_find - search the global table for a given client
182 * @bat_priv: the bat priv with all the soft interface information
183 * @addr: the mac address of the client to look for
184 * @vid: VLAN identifier
186 * Return: a pointer to the corresponding tt_global_entry struct if the client
187 * is found, NULL otherwise.
189 static struct batadv_tt_global_entry
*
190 batadv_tt_global_hash_find(struct batadv_priv
*bat_priv
, const u8
*addr
,
193 struct batadv_tt_common_entry
*tt_common_entry
;
194 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
196 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.global_hash
, addr
,
199 tt_global_entry
= container_of(tt_common_entry
,
200 struct batadv_tt_global_entry
,
202 return tt_global_entry
;
206 * batadv_tt_local_entry_release - release tt_local_entry from lists and queue
207 * for free after rcu grace period
208 * @ref: kref pointer of the nc_node
210 static void batadv_tt_local_entry_release(struct kref
*ref
)
212 struct batadv_tt_local_entry
*tt_local_entry
;
214 tt_local_entry
= container_of(ref
, struct batadv_tt_local_entry
,
217 batadv_softif_vlan_put(tt_local_entry
->vlan
);
219 kfree_rcu(tt_local_entry
, common
.rcu
);
223 * batadv_tt_local_entry_put - decrement the tt_local_entry refcounter and
224 * possibly release it
225 * @tt_local_entry: tt_local_entry to be free'd
228 batadv_tt_local_entry_put(struct batadv_tt_local_entry
*tt_local_entry
)
230 kref_put(&tt_local_entry
->common
.refcount
,
231 batadv_tt_local_entry_release
);
235 * batadv_tt_global_entry_release - release tt_global_entry from lists and queue
236 * for free after rcu grace period
237 * @ref: kref pointer of the nc_node
239 static void batadv_tt_global_entry_release(struct kref
*ref
)
241 struct batadv_tt_global_entry
*tt_global_entry
;
243 tt_global_entry
= container_of(ref
, struct batadv_tt_global_entry
,
246 batadv_tt_global_del_orig_list(tt_global_entry
);
247 kfree_rcu(tt_global_entry
, common
.rcu
);
251 * batadv_tt_global_entry_put - decrement the tt_global_entry refcounter and
252 * possibly release it
253 * @tt_global_entry: tt_global_entry to be free'd
256 batadv_tt_global_entry_put(struct batadv_tt_global_entry
*tt_global_entry
)
258 kref_put(&tt_global_entry
->common
.refcount
,
259 batadv_tt_global_entry_release
);
263 * batadv_tt_global_hash_count - count the number of orig entries
264 * @bat_priv: the bat priv with all the soft interface information
265 * @addr: the mac address of the client to count entries for
266 * @vid: VLAN identifier
268 * Return: the number of originators advertising the given address/data
269 * (excluding ourself).
271 int batadv_tt_global_hash_count(struct batadv_priv
*bat_priv
,
272 const u8
*addr
, unsigned short vid
)
274 struct batadv_tt_global_entry
*tt_global_entry
;
277 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
278 if (!tt_global_entry
)
281 count
= atomic_read(&tt_global_entry
->orig_list_count
);
282 batadv_tt_global_entry_put(tt_global_entry
);
288 * batadv_tt_local_size_mod - change the size by v of the local table identified
290 * @bat_priv: the bat priv with all the soft interface information
291 * @vid: the VLAN identifier of the sub-table to change
292 * @v: the amount to sum to the local table size
294 static void batadv_tt_local_size_mod(struct batadv_priv
*bat_priv
,
295 unsigned short vid
, int v
)
297 struct batadv_softif_vlan
*vlan
;
299 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
303 atomic_add(v
, &vlan
->tt
.num_entries
);
305 batadv_softif_vlan_put(vlan
);
309 * batadv_tt_local_size_inc - increase by one the local table size for the given
311 * @bat_priv: the bat priv with all the soft interface information
312 * @vid: the VLAN identifier
314 static void batadv_tt_local_size_inc(struct batadv_priv
*bat_priv
,
317 batadv_tt_local_size_mod(bat_priv
, vid
, 1);
321 * batadv_tt_local_size_dec - decrease by one the local table size for the given
323 * @bat_priv: the bat priv with all the soft interface information
324 * @vid: the VLAN identifier
326 static void batadv_tt_local_size_dec(struct batadv_priv
*bat_priv
,
329 batadv_tt_local_size_mod(bat_priv
, vid
, -1);
333 * batadv_tt_global_size_mod - change the size by v of the global table
334 * for orig_node identified by vid
335 * @orig_node: the originator for which the table has to be modified
336 * @vid: the VLAN identifier
337 * @v: the amount to sum to the global table size
339 static void batadv_tt_global_size_mod(struct batadv_orig_node
*orig_node
,
340 unsigned short vid
, int v
)
342 struct batadv_orig_node_vlan
*vlan
;
344 vlan
= batadv_orig_node_vlan_new(orig_node
, vid
);
348 if (atomic_add_return(v
, &vlan
->tt
.num_entries
) == 0) {
349 spin_lock_bh(&orig_node
->vlan_list_lock
);
350 if (!hlist_unhashed(&vlan
->list
)) {
351 hlist_del_init_rcu(&vlan
->list
);
352 batadv_orig_node_vlan_put(vlan
);
354 spin_unlock_bh(&orig_node
->vlan_list_lock
);
357 batadv_orig_node_vlan_put(vlan
);
361 * batadv_tt_global_size_inc - increase by one the global table size for the
363 * @orig_node: the originator which global table size has to be decreased
364 * @vid: the vlan identifier
366 static void batadv_tt_global_size_inc(struct batadv_orig_node
*orig_node
,
369 batadv_tt_global_size_mod(orig_node
, vid
, 1);
373 * batadv_tt_global_size_dec - decrease by one the global table size for the
375 * @orig_node: the originator which global table size has to be decreased
376 * @vid: the vlan identifier
378 static void batadv_tt_global_size_dec(struct batadv_orig_node
*orig_node
,
381 batadv_tt_global_size_mod(orig_node
, vid
, -1);
385 * batadv_tt_orig_list_entry_release - release tt orig entry from lists and
386 * queue for free after rcu grace period
387 * @ref: kref pointer of the tt orig entry
389 static void batadv_tt_orig_list_entry_release(struct kref
*ref
)
391 struct batadv_tt_orig_list_entry
*orig_entry
;
393 orig_entry
= container_of(ref
, struct batadv_tt_orig_list_entry
,
396 batadv_orig_node_put(orig_entry
->orig_node
);
397 kfree_rcu(orig_entry
, rcu
);
401 * batadv_tt_orig_list_entry_put - decrement the tt orig entry refcounter and
402 * possibly release it
403 * @orig_entry: tt orig entry to be free'd
406 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry
*orig_entry
)
408 kref_put(&orig_entry
->refcount
, batadv_tt_orig_list_entry_release
);
412 * batadv_tt_local_event - store a local TT event (ADD/DEL)
413 * @bat_priv: the bat priv with all the soft interface information
414 * @tt_local_entry: the TT entry involved in the event
415 * @event_flags: flags to store in the event structure
417 static void batadv_tt_local_event(struct batadv_priv
*bat_priv
,
418 struct batadv_tt_local_entry
*tt_local_entry
,
421 struct batadv_tt_change_node
*tt_change_node
, *entry
, *safe
;
422 struct batadv_tt_common_entry
*common
= &tt_local_entry
->common
;
423 u8 flags
= common
->flags
| event_flags
;
424 bool event_removed
= false;
425 bool del_op_requested
, del_op_entry
;
427 tt_change_node
= kmalloc(sizeof(*tt_change_node
), GFP_ATOMIC
);
431 tt_change_node
->change
.flags
= flags
;
432 memset(tt_change_node
->change
.reserved
, 0,
433 sizeof(tt_change_node
->change
.reserved
));
434 ether_addr_copy(tt_change_node
->change
.addr
, common
->addr
);
435 tt_change_node
->change
.vid
= htons(common
->vid
);
437 del_op_requested
= flags
& BATADV_TT_CLIENT_DEL
;
439 /* check for ADD+DEL or DEL+ADD events */
440 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
441 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
443 if (!batadv_compare_eth(entry
->change
.addr
, common
->addr
))
446 /* DEL+ADD in the same orig interval have no effect and can be
447 * removed to avoid silly behaviour on the receiver side. The
448 * other way around (ADD+DEL) can happen in case of roaming of
449 * a client still in the NEW state. Roaming of NEW clients is
450 * now possible due to automatically recognition of "temporary"
453 del_op_entry
= entry
->change
.flags
& BATADV_TT_CLIENT_DEL
;
454 if (!del_op_requested
&& del_op_entry
)
456 if (del_op_requested
&& !del_op_entry
)
459 /* this is a second add in the same originator interval. It
460 * means that flags have been changed: update them!
462 if (!del_op_requested
&& !del_op_entry
)
463 entry
->change
.flags
= flags
;
467 list_del(&entry
->list
);
469 kfree(tt_change_node
);
470 event_removed
= true;
474 /* track the change in the OGMinterval list */
475 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt
.changes_list
);
478 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
481 atomic_dec(&bat_priv
->tt
.local_changes
);
483 atomic_inc(&bat_priv
->tt
.local_changes
);
487 * batadv_tt_len - compute length in bytes of given number of tt changes
488 * @changes_num: number of tt changes
490 * Return: computed length in bytes.
492 static int batadv_tt_len(int changes_num
)
494 return changes_num
* sizeof(struct batadv_tvlv_tt_change
);
498 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
499 * @tt_len: available space
501 * Return: the number of entries.
503 static u16
batadv_tt_entries(u16 tt_len
)
505 return tt_len
/ batadv_tt_len(1);
509 * batadv_tt_local_table_transmit_size - calculates the local translation table
510 * size when transmitted over the air
511 * @bat_priv: the bat priv with all the soft interface information
513 * Return: local translation table size in bytes.
515 static int batadv_tt_local_table_transmit_size(struct batadv_priv
*bat_priv
)
518 u16 tt_local_entries
= 0;
519 struct batadv_softif_vlan
*vlan
;
523 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
525 tt_local_entries
+= atomic_read(&vlan
->tt
.num_entries
);
529 /* header size of tvlv encapsulated tt response payload */
530 hdr_size
= sizeof(struct batadv_unicast_tvlv_packet
);
531 hdr_size
+= sizeof(struct batadv_tvlv_hdr
);
532 hdr_size
+= sizeof(struct batadv_tvlv_tt_data
);
533 hdr_size
+= num_vlan
* sizeof(struct batadv_tvlv_tt_vlan_data
);
535 return hdr_size
+ batadv_tt_len(tt_local_entries
);
538 static int batadv_tt_local_init(struct batadv_priv
*bat_priv
)
540 if (bat_priv
->tt
.local_hash
)
543 bat_priv
->tt
.local_hash
= batadv_hash_new(1024);
545 if (!bat_priv
->tt
.local_hash
)
548 batadv_hash_set_lock_class(bat_priv
->tt
.local_hash
,
549 &batadv_tt_local_hash_lock_class_key
);
554 static void batadv_tt_global_free(struct batadv_priv
*bat_priv
,
555 struct batadv_tt_global_entry
*tt_global
,
558 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
559 "Deleting global tt entry %pM (vid: %d): %s\n",
560 tt_global
->common
.addr
,
561 BATADV_PRINT_VID(tt_global
->common
.vid
), message
);
563 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
564 batadv_choose_tt
, &tt_global
->common
);
565 batadv_tt_global_entry_put(tt_global
);
569 * batadv_tt_local_add - add a new client to the local table or update an
571 * @soft_iface: netdev struct of the mesh interface
572 * @addr: the mac address of the client to add
573 * @vid: VLAN identifier
574 * @ifindex: index of the interface where the client is connected to (useful to
575 * identify wireless clients)
576 * @mark: the value contained in the skb->mark field of the received packet (if
579 * Return: true if the client was successfully added, false otherwise.
581 bool batadv_tt_local_add(struct net_device
*soft_iface
, const u8
*addr
,
582 unsigned short vid
, int ifindex
, u32 mark
)
584 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
585 struct batadv_tt_local_entry
*tt_local
;
586 struct batadv_tt_global_entry
*tt_global
= NULL
;
587 struct net
*net
= dev_net(soft_iface
);
588 struct batadv_softif_vlan
*vlan
;
589 struct net_device
*in_dev
= NULL
;
590 struct hlist_head
*head
;
591 struct batadv_tt_orig_list_entry
*orig_entry
;
592 int hash_added
, table_size
, packet_size_max
;
594 bool roamed_back
= false;
598 if (ifindex
!= BATADV_NULL_IFINDEX
)
599 in_dev
= dev_get_by_index(net
, ifindex
);
601 tt_local
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
603 if (!is_multicast_ether_addr(addr
))
604 tt_global
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
607 tt_local
->last_seen
= jiffies
;
608 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_PENDING
) {
609 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
610 "Re-adding pending client %pM (vid: %d)\n",
611 addr
, BATADV_PRINT_VID(vid
));
612 /* whatever the reason why the PENDING flag was set,
613 * this is a client which was enqueued to be removed in
614 * this orig_interval. Since it popped up again, the
615 * flag can be reset like it was never enqueued
617 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_PENDING
;
621 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
622 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
623 "Roaming client %pM (vid: %d) came back to its original location\n",
624 addr
, BATADV_PRINT_VID(vid
));
625 /* the ROAM flag is set because this client roamed away
626 * and the node got a roaming_advertisement message. Now
627 * that the client popped up again at its original
628 * location such flag can be unset
630 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
636 /* Ignore the client if we cannot send it in a full table response. */
637 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
638 table_size
+= batadv_tt_len(1);
639 packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
640 if (table_size
> packet_size_max
) {
641 net_ratelimited_function(batadv_info
, soft_iface
,
642 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
643 table_size
, packet_size_max
, addr
);
647 tt_local
= kmalloc(sizeof(*tt_local
), GFP_ATOMIC
);
651 /* increase the refcounter of the related vlan */
652 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
653 if (WARN(!vlan
, "adding TT local entry %pM to non-existent VLAN %d",
654 addr
, BATADV_PRINT_VID(vid
))) {
660 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
661 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
662 addr
, BATADV_PRINT_VID(vid
),
663 (u8
)atomic_read(&bat_priv
->tt
.vn
));
665 ether_addr_copy(tt_local
->common
.addr
, addr
);
666 /* The local entry has to be marked as NEW to avoid to send it in
667 * a full table response going out before the next ttvn increment
668 * (consistency check)
670 tt_local
->common
.flags
= BATADV_TT_CLIENT_NEW
;
671 tt_local
->common
.vid
= vid
;
672 if (batadv_is_wifi_netdev(in_dev
))
673 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
674 kref_init(&tt_local
->common
.refcount
);
675 kref_get(&tt_local
->common
.refcount
);
676 tt_local
->last_seen
= jiffies
;
677 tt_local
->common
.added_at
= tt_local
->last_seen
;
678 tt_local
->vlan
= vlan
;
680 /* the batman interface mac and multicast addresses should never be
683 if (batadv_compare_eth(addr
, soft_iface
->dev_addr
) ||
684 is_multicast_ether_addr(addr
))
685 tt_local
->common
.flags
|= BATADV_TT_CLIENT_NOPURGE
;
687 hash_added
= batadv_hash_add(bat_priv
->tt
.local_hash
, batadv_compare_tt
,
688 batadv_choose_tt
, &tt_local
->common
,
689 &tt_local
->common
.hash_entry
);
691 if (unlikely(hash_added
!= 0)) {
692 /* remove the reference for the hash */
693 batadv_tt_local_entry_put(tt_local
);
694 batadv_softif_vlan_put(vlan
);
699 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
702 /* Check whether it is a roaming, but don't do anything if the roaming
703 * process has already been handled
705 if (tt_global
&& !(tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
)) {
706 /* These node are probably going to update their tt table */
707 head
= &tt_global
->orig_list
;
709 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
710 batadv_send_roam_adv(bat_priv
, tt_global
->common
.addr
,
711 tt_global
->common
.vid
,
712 orig_entry
->orig_node
);
716 batadv_tt_global_free(bat_priv
, tt_global
,
720 /* The global entry has to be marked as ROAMING and
721 * has to be kept for consistency purpose
723 tt_global
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
724 tt_global
->roam_at
= jiffies
;
728 /* store the current remote flags before altering them. This helps
729 * understanding is flags are changing or not
731 remote_flags
= tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
;
733 if (batadv_is_wifi_netdev(in_dev
))
734 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
736 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_WIFI
;
738 /* check the mark in the skb: if it's equal to the configured
739 * isolation_mark, it means the packet is coming from an isolated
742 match_mark
= (mark
& bat_priv
->isolation_mark_mask
);
743 if (bat_priv
->isolation_mark_mask
&&
744 match_mark
== bat_priv
->isolation_mark
)
745 tt_local
->common
.flags
|= BATADV_TT_CLIENT_ISOLA
;
747 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ISOLA
;
749 /* if any "dynamic" flag has been modified, resend an ADD event for this
750 * entry so that all the nodes can get the new flags
752 if (remote_flags
^ (tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
))
753 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
760 batadv_tt_local_entry_put(tt_local
);
762 batadv_tt_global_entry_put(tt_global
);
767 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
768 * within a TT Response directed to another node
769 * @orig_node: originator for which the TT data has to be prepared
770 * @tt_data: uninitialised pointer to the address of the TVLV buffer
771 * @tt_change: uninitialised pointer to the address of the area where the TT
772 * changed can be stored
773 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
774 * function reserves the amount of space needed to send the entire global TT
775 * table. In case of success the value is updated with the real amount of
777 * Allocate the needed amount of memory for the entire TT TVLV and write its
778 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
779 * objects, one per active VLAN served by the originator node.
781 * Return: the size of the allocated buffer or 0 in case of failure.
784 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node
*orig_node
,
785 struct batadv_tvlv_tt_data
**tt_data
,
786 struct batadv_tvlv_tt_change
**tt_change
,
793 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
794 struct batadv_orig_node_vlan
*vlan
;
798 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
800 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
803 change_offset
= sizeof(**tt_data
);
804 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
806 /* if tt_len is negative, allocate the space needed by the full table */
808 *tt_len
= batadv_tt_len(num_entries
);
811 tvlv_len
+= change_offset
;
813 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
819 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
820 (*tt_data
)->ttvn
= atomic_read(&orig_node
->last_ttvn
);
821 (*tt_data
)->num_vlan
= htons(num_vlan
);
823 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
824 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
825 tt_vlan
->vid
= htons(vlan
->vid
);
826 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
831 tt_change_ptr
= (u8
*)*tt_data
+ change_offset
;
832 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
840 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
842 * @bat_priv: the bat priv with all the soft interface information
843 * @tt_data: uninitialised pointer to the address of the TVLV buffer
844 * @tt_change: uninitialised pointer to the address of the area where the TT
845 * changes can be stored
846 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
847 * function reserves the amount of space needed to send the entire local TT
848 * table. In case of success the value is updated with the real amount of
851 * Allocate the needed amount of memory for the entire TT TVLV and write its
852 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
853 * objects, one per active VLAN.
855 * Return: the size of the allocated buffer or 0 in case of failure.
858 batadv_tt_prepare_tvlv_local_data(struct batadv_priv
*bat_priv
,
859 struct batadv_tvlv_tt_data
**tt_data
,
860 struct batadv_tvlv_tt_change
**tt_change
,
863 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
864 struct batadv_softif_vlan
*vlan
;
872 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
874 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
877 change_offset
= sizeof(**tt_data
);
878 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
880 /* if tt_len is negative, allocate the space needed by the full table */
882 *tt_len
= batadv_tt_len(num_entries
);
885 tvlv_len
+= change_offset
;
887 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
893 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
894 (*tt_data
)->ttvn
= atomic_read(&bat_priv
->tt
.vn
);
895 (*tt_data
)->num_vlan
= htons(num_vlan
);
897 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
898 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
899 tt_vlan
->vid
= htons(vlan
->vid
);
900 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
905 tt_change_ptr
= (u8
*)*tt_data
+ change_offset
;
906 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
914 * batadv_tt_tvlv_container_update - update the translation table tvlv container
915 * after local tt changes have been committed
916 * @bat_priv: the bat priv with all the soft interface information
918 static void batadv_tt_tvlv_container_update(struct batadv_priv
*bat_priv
)
920 struct batadv_tt_change_node
*entry
, *safe
;
921 struct batadv_tvlv_tt_data
*tt_data
;
922 struct batadv_tvlv_tt_change
*tt_change
;
923 int tt_diff_len
, tt_change_len
= 0;
924 int tt_diff_entries_num
= 0;
925 int tt_diff_entries_count
= 0;
928 tt_diff_entries_num
= atomic_read(&bat_priv
->tt
.local_changes
);
929 tt_diff_len
= batadv_tt_len(tt_diff_entries_num
);
931 /* if we have too many changes for one packet don't send any
932 * and wait for the tt table request which will be fragmented
934 if (tt_diff_len
> bat_priv
->soft_iface
->mtu
)
937 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
, &tt_data
,
938 &tt_change
, &tt_diff_len
);
942 tt_data
->flags
= BATADV_TT_OGM_DIFF
;
944 if (tt_diff_len
== 0)
945 goto container_register
;
947 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
948 atomic_set(&bat_priv
->tt
.local_changes
, 0);
950 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
952 if (tt_diff_entries_count
< tt_diff_entries_num
) {
953 memcpy(tt_change
+ tt_diff_entries_count
,
955 sizeof(struct batadv_tvlv_tt_change
));
956 tt_diff_entries_count
++;
958 list_del(&entry
->list
);
961 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
963 /* Keep the buffer for possible tt_request */
964 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
965 kfree(bat_priv
->tt
.last_changeset
);
966 bat_priv
->tt
.last_changeset_len
= 0;
967 bat_priv
->tt
.last_changeset
= NULL
;
968 tt_change_len
= batadv_tt_len(tt_diff_entries_count
);
969 /* check whether this new OGM has no changes due to size problems */
970 if (tt_diff_entries_count
> 0) {
971 /* if kmalloc() fails we will reply with the full table
972 * instead of providing the diff
974 bat_priv
->tt
.last_changeset
= kzalloc(tt_diff_len
, GFP_ATOMIC
);
975 if (bat_priv
->tt
.last_changeset
) {
976 memcpy(bat_priv
->tt
.last_changeset
,
977 tt_change
, tt_change_len
);
978 bat_priv
->tt
.last_changeset_len
= tt_diff_len
;
981 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
984 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_TT
, 1, tt_data
,
989 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
991 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
992 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
993 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
994 struct batadv_tt_common_entry
*tt_common_entry
;
995 struct batadv_tt_local_entry
*tt_local
;
996 struct batadv_hard_iface
*primary_if
;
997 struct hlist_head
*head
;
1000 int last_seen_msecs
;
1001 unsigned long last_seen_jiffies
;
1003 u16 np_flag
= BATADV_TT_CLIENT_NOPURGE
;
1005 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1010 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1011 net_dev
->name
, (u8
)atomic_read(&bat_priv
->tt
.vn
));
1013 " Client VID Flags Last seen (CRC )\n");
1015 for (i
= 0; i
< hash
->size
; i
++) {
1016 head
= &hash
->table
[i
];
1019 hlist_for_each_entry_rcu(tt_common_entry
,
1021 tt_local
= container_of(tt_common_entry
,
1022 struct batadv_tt_local_entry
,
1024 last_seen_jiffies
= jiffies
- tt_local
->last_seen
;
1025 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
1026 last_seen_secs
= last_seen_msecs
/ 1000;
1027 last_seen_msecs
= last_seen_msecs
% 1000;
1029 no_purge
= tt_common_entry
->flags
& np_flag
;
1031 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1032 tt_common_entry
->addr
,
1033 BATADV_PRINT_VID(tt_common_entry
->vid
),
1034 ((tt_common_entry
->flags
&
1035 BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1036 no_purge
? 'P' : '.',
1037 ((tt_common_entry
->flags
&
1038 BATADV_TT_CLIENT_NEW
) ? 'N' : '.'),
1039 ((tt_common_entry
->flags
&
1040 BATADV_TT_CLIENT_PENDING
) ? 'X' : '.'),
1041 ((tt_common_entry
->flags
&
1042 BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1043 ((tt_common_entry
->flags
&
1044 BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1045 no_purge
? 0 : last_seen_secs
,
1046 no_purge
? 0 : last_seen_msecs
,
1047 tt_local
->vlan
->tt
.crc
);
1053 batadv_hardif_put(primary_if
);
1058 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
1059 struct batadv_tt_local_entry
*tt_local_entry
,
1060 u16 flags
, const char *message
)
1062 batadv_tt_local_event(bat_priv
, tt_local_entry
, flags
);
1064 /* The local client has to be marked as "pending to be removed" but has
1065 * to be kept in the table in order to send it in a full table
1066 * response issued before the net ttvn increment (consistency check)
1068 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
1070 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1071 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1072 tt_local_entry
->common
.addr
,
1073 BATADV_PRINT_VID(tt_local_entry
->common
.vid
), message
);
1077 * batadv_tt_local_remove - logically remove an entry from the local table
1078 * @bat_priv: the bat priv with all the soft interface information
1079 * @addr: the MAC address of the client to remove
1080 * @vid: VLAN identifier
1081 * @message: message to append to the log on deletion
1082 * @roaming: true if the deletion is due to a roaming event
1084 * Return: the flags assigned to the local entry before being deleted
1086 u16
batadv_tt_local_remove(struct batadv_priv
*bat_priv
, const u8
*addr
,
1087 unsigned short vid
, const char *message
,
1090 struct batadv_tt_local_entry
*tt_local_entry
;
1091 u16 flags
, curr_flags
= BATADV_NO_FLAGS
;
1092 void *tt_entry_exists
;
1094 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
1095 if (!tt_local_entry
)
1098 curr_flags
= tt_local_entry
->common
.flags
;
1100 flags
= BATADV_TT_CLIENT_DEL
;
1101 /* if this global entry addition is due to a roaming, the node has to
1102 * mark the local entry as "roamed" in order to correctly reroute
1106 flags
|= BATADV_TT_CLIENT_ROAM
;
1107 /* mark the local client as ROAMed */
1108 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1111 if (!(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
)) {
1112 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
,
1116 /* if this client has been added right now, it is possible to
1117 * immediately purge it
1119 batadv_tt_local_event(bat_priv
, tt_local_entry
, BATADV_TT_CLIENT_DEL
);
1121 tt_entry_exists
= batadv_hash_remove(bat_priv
->tt
.local_hash
,
1124 &tt_local_entry
->common
);
1125 if (!tt_entry_exists
)
1128 /* extra call to free the local tt entry */
1129 batadv_tt_local_entry_put(tt_local_entry
);
1133 batadv_tt_local_entry_put(tt_local_entry
);
1139 * batadv_tt_local_purge_list - purge inactive tt local entries
1140 * @bat_priv: the bat priv with all the soft interface information
1141 * @head: pointer to the list containing the local tt entries
1142 * @timeout: parameter deciding whether a given tt local entry is considered
1145 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
1146 struct hlist_head
*head
,
1149 struct batadv_tt_local_entry
*tt_local_entry
;
1150 struct batadv_tt_common_entry
*tt_common_entry
;
1151 struct hlist_node
*node_tmp
;
1153 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
, head
,
1155 tt_local_entry
= container_of(tt_common_entry
,
1156 struct batadv_tt_local_entry
,
1158 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
1161 /* entry already marked for deletion */
1162 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1165 if (!batadv_has_timed_out(tt_local_entry
->last_seen
, timeout
))
1168 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
1169 BATADV_TT_CLIENT_DEL
, "timed out");
1174 * batadv_tt_local_purge - purge inactive tt local entries
1175 * @bat_priv: the bat priv with all the soft interface information
1176 * @timeout: parameter deciding whether a given tt local entry is considered
1179 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
,
1182 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1183 struct hlist_head
*head
;
1184 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1187 for (i
= 0; i
< hash
->size
; i
++) {
1188 head
= &hash
->table
[i
];
1189 list_lock
= &hash
->list_locks
[i
];
1191 spin_lock_bh(list_lock
);
1192 batadv_tt_local_purge_list(bat_priv
, head
, timeout
);
1193 spin_unlock_bh(list_lock
);
1197 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
1199 struct batadv_hashtable
*hash
;
1200 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1201 struct batadv_tt_common_entry
*tt_common_entry
;
1202 struct batadv_tt_local_entry
*tt_local
;
1203 struct hlist_node
*node_tmp
;
1204 struct hlist_head
*head
;
1207 if (!bat_priv
->tt
.local_hash
)
1210 hash
= bat_priv
->tt
.local_hash
;
1212 for (i
= 0; i
< hash
->size
; i
++) {
1213 head
= &hash
->table
[i
];
1214 list_lock
= &hash
->list_locks
[i
];
1216 spin_lock_bh(list_lock
);
1217 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1219 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1220 tt_local
= container_of(tt_common_entry
,
1221 struct batadv_tt_local_entry
,
1224 batadv_tt_local_entry_put(tt_local
);
1226 spin_unlock_bh(list_lock
);
1229 batadv_hash_destroy(hash
);
1231 bat_priv
->tt
.local_hash
= NULL
;
1234 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
1236 if (bat_priv
->tt
.global_hash
)
1239 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
1241 if (!bat_priv
->tt
.global_hash
)
1244 batadv_hash_set_lock_class(bat_priv
->tt
.global_hash
,
1245 &batadv_tt_global_hash_lock_class_key
);
1250 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
1252 struct batadv_tt_change_node
*entry
, *safe
;
1254 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
1256 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
1258 list_del(&entry
->list
);
1262 atomic_set(&bat_priv
->tt
.local_changes
, 0);
1263 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
1267 * batadv_tt_global_orig_entry_find - find a TT orig_list_entry
1268 * @entry: the TT global entry where the orig_list_entry has to be
1270 * @orig_node: the originator for which the orig_list_entry has to be found
1272 * retrieve the orig_tt_list_entry belonging to orig_node from the
1273 * batadv_tt_global_entry list
1275 * Return: it with an increased refcounter, NULL if not found
1277 static struct batadv_tt_orig_list_entry
*
1278 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
1279 const struct batadv_orig_node
*orig_node
)
1281 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
1282 const struct hlist_head
*head
;
1285 head
= &entry
->orig_list
;
1286 hlist_for_each_entry_rcu(tmp_orig_entry
, head
, list
) {
1287 if (tmp_orig_entry
->orig_node
!= orig_node
)
1289 if (!kref_get_unless_zero(&tmp_orig_entry
->refcount
))
1292 orig_entry
= tmp_orig_entry
;
1301 * batadv_tt_global_entry_has_orig - check if a TT global entry is also handled
1302 * by a given originator
1303 * @entry: the TT global entry to check
1304 * @orig_node: the originator to search in the list
1306 * find out if an orig_node is already in the list of a tt_global_entry.
1308 * Return: true if found, false otherwise
1311 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
1312 const struct batadv_orig_node
*orig_node
)
1314 struct batadv_tt_orig_list_entry
*orig_entry
;
1317 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
1320 batadv_tt_orig_list_entry_put(orig_entry
);
1327 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
1328 struct batadv_orig_node
*orig_node
, int ttvn
)
1330 struct batadv_tt_orig_list_entry
*orig_entry
;
1332 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
1334 /* refresh the ttvn: the current value could be a bogus one that
1335 * was added during a "temporary client detection"
1337 orig_entry
->ttvn
= ttvn
;
1341 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
1345 INIT_HLIST_NODE(&orig_entry
->list
);
1346 kref_get(&orig_node
->refcount
);
1347 batadv_tt_global_size_inc(orig_node
, tt_global
->common
.vid
);
1348 orig_entry
->orig_node
= orig_node
;
1349 orig_entry
->ttvn
= ttvn
;
1350 kref_init(&orig_entry
->refcount
);
1351 kref_get(&orig_entry
->refcount
);
1353 spin_lock_bh(&tt_global
->list_lock
);
1354 hlist_add_head_rcu(&orig_entry
->list
,
1355 &tt_global
->orig_list
);
1356 spin_unlock_bh(&tt_global
->list_lock
);
1357 atomic_inc(&tt_global
->orig_list_count
);
1361 batadv_tt_orig_list_entry_put(orig_entry
);
1365 * batadv_tt_global_add - add a new TT global entry or update an existing one
1366 * @bat_priv: the bat priv with all the soft interface information
1367 * @orig_node: the originator announcing the client
1368 * @tt_addr: the mac address of the non-mesh client
1369 * @vid: VLAN identifier
1370 * @flags: TT flags that have to be set for this non-mesh client
1371 * @ttvn: the tt version number ever announcing this non-mesh client
1373 * Add a new TT global entry for the given originator. If the entry already
1374 * exists add a new reference to the given originator (a global entry can have
1375 * references to multiple originators) and adjust the flags attribute to reflect
1376 * the function argument.
1377 * If a TT local entry exists for this non-mesh client remove it.
1379 * The caller must hold orig_node refcount.
1381 * Return: true if the new entry has been added, false otherwise
1383 static bool batadv_tt_global_add(struct batadv_priv
*bat_priv
,
1384 struct batadv_orig_node
*orig_node
,
1385 const unsigned char *tt_addr
,
1386 unsigned short vid
, u16 flags
, u8 ttvn
)
1388 struct batadv_tt_global_entry
*tt_global_entry
;
1389 struct batadv_tt_local_entry
*tt_local_entry
;
1392 struct batadv_tt_common_entry
*common
;
1395 /* ignore global entries from backbone nodes */
1396 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
, vid
))
1399 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
, vid
);
1400 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, tt_addr
, vid
);
1402 /* if the node already has a local client for this entry, it has to wait
1403 * for a roaming advertisement instead of manually messing up the global
1406 if ((flags
& BATADV_TT_CLIENT_TEMP
) && tt_local_entry
&&
1407 !(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
))
1410 if (!tt_global_entry
) {
1411 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
1412 if (!tt_global_entry
)
1415 common
= &tt_global_entry
->common
;
1416 ether_addr_copy(common
->addr
, tt_addr
);
1419 common
->flags
= flags
;
1420 tt_global_entry
->roam_at
= 0;
1421 /* node must store current time in case of roaming. This is
1422 * needed to purge this entry out on timeout (if nobody claims
1425 if (flags
& BATADV_TT_CLIENT_ROAM
)
1426 tt_global_entry
->roam_at
= jiffies
;
1427 kref_init(&common
->refcount
);
1428 kref_get(&common
->refcount
);
1429 common
->added_at
= jiffies
;
1431 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
1432 atomic_set(&tt_global_entry
->orig_list_count
, 0);
1433 spin_lock_init(&tt_global_entry
->list_lock
);
1435 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
1437 batadv_choose_tt
, common
,
1438 &common
->hash_entry
);
1440 if (unlikely(hash_added
!= 0)) {
1441 /* remove the reference for the hash */
1442 batadv_tt_global_entry_put(tt_global_entry
);
1446 common
= &tt_global_entry
->common
;
1447 /* If there is already a global entry, we can use this one for
1449 * But if we are trying to add a temporary client then here are
1450 * two options at this point:
1451 * 1) the global client is not a temporary client: the global
1452 * client has to be left as it is, temporary information
1453 * should never override any already known client state
1454 * 2) the global client is a temporary client: purge the
1455 * originator list and add the new one orig_entry
1457 if (flags
& BATADV_TT_CLIENT_TEMP
) {
1458 if (!(common
->flags
& BATADV_TT_CLIENT_TEMP
))
1460 if (batadv_tt_global_entry_has_orig(tt_global_entry
,
1463 batadv_tt_global_del_orig_list(tt_global_entry
);
1464 goto add_orig_entry
;
1467 /* if the client was temporary added before receiving the first
1468 * OGM announcing it, we have to clear the TEMP flag. Also,
1469 * remove the previous temporary orig node and re-add it
1470 * if required. If the orig entry changed, the new one which
1471 * is a non-temporary entry is preferred.
1473 if (common
->flags
& BATADV_TT_CLIENT_TEMP
) {
1474 batadv_tt_global_del_orig_list(tt_global_entry
);
1475 common
->flags
&= ~BATADV_TT_CLIENT_TEMP
;
1478 /* the change can carry possible "attribute" flags like the
1479 * TT_CLIENT_WIFI, therefore they have to be copied in the
1482 common
->flags
|= flags
;
1484 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1485 * one originator left in the list and we previously received a
1486 * delete + roaming change for this originator.
1488 * We should first delete the old originator before adding the
1491 if (common
->flags
& BATADV_TT_CLIENT_ROAM
) {
1492 batadv_tt_global_del_orig_list(tt_global_entry
);
1493 common
->flags
&= ~BATADV_TT_CLIENT_ROAM
;
1494 tt_global_entry
->roam_at
= 0;
1498 /* add the new orig_entry (if needed) or update it */
1499 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
1501 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1502 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1503 common
->addr
, BATADV_PRINT_VID(common
->vid
),
1508 /* Do not remove multicast addresses from the local hash on
1511 if (is_multicast_ether_addr(tt_addr
))
1514 /* remove address from local hash if present */
1515 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
, vid
,
1516 "global tt received",
1517 flags
& BATADV_TT_CLIENT_ROAM
);
1518 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
1520 if (!(flags
& BATADV_TT_CLIENT_ROAM
))
1521 /* this is a normal global add. Therefore the client is not in a
1522 * roaming state anymore.
1524 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
1527 if (tt_global_entry
)
1528 batadv_tt_global_entry_put(tt_global_entry
);
1530 batadv_tt_local_entry_put(tt_local_entry
);
1535 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1536 * @bat_priv: the bat priv with all the soft interface information
1537 * @tt_global_entry: global translation table entry to be analyzed
1539 * This functon assumes the caller holds rcu_read_lock().
1540 * Return: best originator list entry or NULL on errors.
1542 static struct batadv_tt_orig_list_entry
*
1543 batadv_transtable_best_orig(struct batadv_priv
*bat_priv
,
1544 struct batadv_tt_global_entry
*tt_global_entry
)
1546 struct batadv_neigh_node
*router
, *best_router
= NULL
;
1547 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1548 struct hlist_head
*head
;
1549 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
= NULL
;
1551 head
= &tt_global_entry
->orig_list
;
1552 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1553 router
= batadv_orig_router_get(orig_entry
->orig_node
,
1559 bao
->bat_neigh_cmp(router
, BATADV_IF_DEFAULT
,
1560 best_router
, BATADV_IF_DEFAULT
) <= 0) {
1561 batadv_neigh_node_put(router
);
1565 /* release the refcount for the "old" best */
1567 batadv_neigh_node_put(best_router
);
1569 best_entry
= orig_entry
;
1570 best_router
= router
;
1574 batadv_neigh_node_put(best_router
);
1580 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1581 * for this global entry
1582 * @bat_priv: the bat priv with all the soft interface information
1583 * @tt_global_entry: global translation table entry to be printed
1584 * @seq: debugfs table seq_file struct
1586 * This functon assumes the caller holds rcu_read_lock().
1589 batadv_tt_global_print_entry(struct batadv_priv
*bat_priv
,
1590 struct batadv_tt_global_entry
*tt_global_entry
,
1591 struct seq_file
*seq
)
1593 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
;
1594 struct batadv_tt_common_entry
*tt_common_entry
;
1595 struct batadv_orig_node_vlan
*vlan
;
1596 struct hlist_head
*head
;
1600 tt_common_entry
= &tt_global_entry
->common
;
1601 flags
= tt_common_entry
->flags
;
1603 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
1605 vlan
= batadv_orig_node_vlan_get(best_entry
->orig_node
,
1606 tt_common_entry
->vid
);
1609 " * Cannot retrieve VLAN %d for originator %pM\n",
1610 BATADV_PRINT_VID(tt_common_entry
->vid
),
1611 best_entry
->orig_node
->orig
);
1615 last_ttvn
= atomic_read(&best_entry
->orig_node
->last_ttvn
);
1617 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1618 '*', tt_global_entry
->common
.addr
,
1619 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1620 best_entry
->ttvn
, best_entry
->orig_node
->orig
,
1621 last_ttvn
, vlan
->tt
.crc
,
1622 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1623 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1624 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1625 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1627 batadv_orig_node_vlan_put(vlan
);
1631 head
= &tt_global_entry
->orig_list
;
1633 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1634 if (best_entry
== orig_entry
)
1637 vlan
= batadv_orig_node_vlan_get(orig_entry
->orig_node
,
1638 tt_common_entry
->vid
);
1641 " + Cannot retrieve VLAN %d for originator %pM\n",
1642 BATADV_PRINT_VID(tt_common_entry
->vid
),
1643 orig_entry
->orig_node
->orig
);
1647 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
1649 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1650 '+', tt_global_entry
->common
.addr
,
1651 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1652 orig_entry
->ttvn
, orig_entry
->orig_node
->orig
,
1653 last_ttvn
, vlan
->tt
.crc
,
1654 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1655 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1656 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1657 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1659 batadv_orig_node_vlan_put(vlan
);
1663 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
1665 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1666 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1667 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1668 struct batadv_tt_common_entry
*tt_common_entry
;
1669 struct batadv_tt_global_entry
*tt_global
;
1670 struct batadv_hard_iface
*primary_if
;
1671 struct hlist_head
*head
;
1674 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1679 "Globally announced TT entries received via the mesh %s\n",
1682 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
1684 for (i
= 0; i
< hash
->size
; i
++) {
1685 head
= &hash
->table
[i
];
1688 hlist_for_each_entry_rcu(tt_common_entry
,
1690 tt_global
= container_of(tt_common_entry
,
1691 struct batadv_tt_global_entry
,
1693 batadv_tt_global_print_entry(bat_priv
, tt_global
, seq
);
1699 batadv_hardif_put(primary_if
);
1704 * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1705 * @tt_global_entry: the global entry to remove the orig_entry from
1706 * @orig_entry: the orig entry to remove and free
1708 * Remove an orig_entry from its list in the given tt_global_entry and
1709 * free this orig_entry afterwards.
1711 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1715 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry
*tt_global_entry
,
1716 struct batadv_tt_orig_list_entry
*orig_entry
)
1718 lockdep_assert_held(&tt_global_entry
->list_lock
);
1720 batadv_tt_global_size_dec(orig_entry
->orig_node
,
1721 tt_global_entry
->common
.vid
);
1722 atomic_dec(&tt_global_entry
->orig_list_count
);
1723 /* requires holding tt_global_entry->list_lock and orig_entry->list
1724 * being part of a list
1726 hlist_del_rcu(&orig_entry
->list
);
1727 batadv_tt_orig_list_entry_put(orig_entry
);
1730 /* deletes the orig list of a tt_global_entry */
1732 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
1734 struct hlist_head
*head
;
1735 struct hlist_node
*safe
;
1736 struct batadv_tt_orig_list_entry
*orig_entry
;
1738 spin_lock_bh(&tt_global_entry
->list_lock
);
1739 head
= &tt_global_entry
->orig_list
;
1740 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
)
1741 _batadv_tt_global_del_orig_entry(tt_global_entry
, orig_entry
);
1742 spin_unlock_bh(&tt_global_entry
->list_lock
);
1746 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1747 * @bat_priv: the bat priv with all the soft interface information
1748 * @tt_global_entry: the global entry to remove the orig_node from
1749 * @orig_node: the originator announcing the client
1750 * @message: message to append to the log on deletion
1752 * Remove the given orig_node and its according orig_entry from the given
1756 batadv_tt_global_del_orig_node(struct batadv_priv
*bat_priv
,
1757 struct batadv_tt_global_entry
*tt_global_entry
,
1758 struct batadv_orig_node
*orig_node
,
1759 const char *message
)
1761 struct hlist_head
*head
;
1762 struct hlist_node
*safe
;
1763 struct batadv_tt_orig_list_entry
*orig_entry
;
1766 spin_lock_bh(&tt_global_entry
->list_lock
);
1767 head
= &tt_global_entry
->orig_list
;
1768 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
) {
1769 if (orig_entry
->orig_node
== orig_node
) {
1770 vid
= tt_global_entry
->common
.vid
;
1771 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1772 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1774 tt_global_entry
->common
.addr
,
1775 BATADV_PRINT_VID(vid
), message
);
1776 _batadv_tt_global_del_orig_entry(tt_global_entry
,
1780 spin_unlock_bh(&tt_global_entry
->list_lock
);
1783 /* If the client is to be deleted, we check if it is the last origantor entry
1784 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1785 * timer, otherwise we simply remove the originator scheduled for deletion.
1788 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
1789 struct batadv_tt_global_entry
*tt_global_entry
,
1790 struct batadv_orig_node
*orig_node
,
1791 const char *message
)
1793 bool last_entry
= true;
1794 struct hlist_head
*head
;
1795 struct batadv_tt_orig_list_entry
*orig_entry
;
1797 /* no local entry exists, case 1:
1798 * Check if this is the last one or if other entries exist.
1802 head
= &tt_global_entry
->orig_list
;
1803 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1804 if (orig_entry
->orig_node
!= orig_node
) {
1812 /* its the last one, mark for roaming. */
1813 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1814 tt_global_entry
->roam_at
= jiffies
;
1816 /* there is another entry, we can simply delete this
1817 * one and can still use the other one.
1819 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1820 orig_node
, message
);
1824 * batadv_tt_global_del - remove a client from the global table
1825 * @bat_priv: the bat priv with all the soft interface information
1826 * @orig_node: an originator serving this client
1827 * @addr: the mac address of the client
1828 * @vid: VLAN identifier
1829 * @message: a message explaining the reason for deleting the client to print
1830 * for debugging purpose
1831 * @roaming: true if the deletion has been triggered by a roaming event
1833 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
1834 struct batadv_orig_node
*orig_node
,
1835 const unsigned char *addr
, unsigned short vid
,
1836 const char *message
, bool roaming
)
1838 struct batadv_tt_global_entry
*tt_global_entry
;
1839 struct batadv_tt_local_entry
*local_entry
= NULL
;
1841 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
1842 if (!tt_global_entry
)
1846 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1847 orig_node
, message
);
1849 if (hlist_empty(&tt_global_entry
->orig_list
))
1850 batadv_tt_global_free(bat_priv
, tt_global_entry
,
1856 /* if we are deleting a global entry due to a roam
1857 * event, there are two possibilities:
1858 * 1) the client roamed from node A to node B => if there
1859 * is only one originator left for this client, we mark
1860 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1861 * wait for node B to claim it. In case of timeout
1862 * the entry is purged.
1864 * If there are other originators left, we directly delete
1866 * 2) the client roamed to us => we can directly delete
1867 * the global entry, since it is useless now.
1869 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1870 tt_global_entry
->common
.addr
,
1873 /* local entry exists, case 2: client roamed to us. */
1874 batadv_tt_global_del_orig_list(tt_global_entry
);
1875 batadv_tt_global_free(bat_priv
, tt_global_entry
, message
);
1877 /* no local entry exists, case 1: check for roaming */
1878 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1879 orig_node
, message
);
1882 if (tt_global_entry
)
1883 batadv_tt_global_entry_put(tt_global_entry
);
1885 batadv_tt_local_entry_put(local_entry
);
1889 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1890 * given originator matching the provided vid
1891 * @bat_priv: the bat priv with all the soft interface information
1892 * @orig_node: the originator owning the entries to remove
1893 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1895 * @message: debug message to print as "reason"
1897 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1898 struct batadv_orig_node
*orig_node
,
1900 const char *message
)
1902 struct batadv_tt_global_entry
*tt_global
;
1903 struct batadv_tt_common_entry
*tt_common_entry
;
1905 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1906 struct hlist_node
*safe
;
1907 struct hlist_head
*head
;
1908 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1914 for (i
= 0; i
< hash
->size
; i
++) {
1915 head
= &hash
->table
[i
];
1916 list_lock
= &hash
->list_locks
[i
];
1918 spin_lock_bh(list_lock
);
1919 hlist_for_each_entry_safe(tt_common_entry
, safe
,
1921 /* remove only matching entries */
1922 if (match_vid
>= 0 && tt_common_entry
->vid
!= match_vid
)
1925 tt_global
= container_of(tt_common_entry
,
1926 struct batadv_tt_global_entry
,
1929 batadv_tt_global_del_orig_node(bat_priv
, tt_global
,
1930 orig_node
, message
);
1932 if (hlist_empty(&tt_global
->orig_list
)) {
1933 vid
= tt_global
->common
.vid
;
1934 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1935 "Deleting global tt entry %pM (vid: %d): %s\n",
1936 tt_global
->common
.addr
,
1937 BATADV_PRINT_VID(vid
), message
);
1938 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1939 batadv_tt_global_entry_put(tt_global
);
1942 spin_unlock_bh(list_lock
);
1944 clear_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
1947 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1951 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1952 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1954 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1955 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1957 *msg
= "Roaming timeout\n";
1960 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1961 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1963 *msg
= "Temporary client timeout\n";
1969 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1971 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1972 struct hlist_head
*head
;
1973 struct hlist_node
*node_tmp
;
1974 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1977 struct batadv_tt_common_entry
*tt_common
;
1978 struct batadv_tt_global_entry
*tt_global
;
1980 for (i
= 0; i
< hash
->size
; i
++) {
1981 head
= &hash
->table
[i
];
1982 list_lock
= &hash
->list_locks
[i
];
1984 spin_lock_bh(list_lock
);
1985 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
1987 tt_global
= container_of(tt_common
,
1988 struct batadv_tt_global_entry
,
1991 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1994 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1995 "Deleting global tt entry %pM (vid: %d): %s\n",
1996 tt_global
->common
.addr
,
1997 BATADV_PRINT_VID(tt_global
->common
.vid
),
2000 hlist_del_rcu(&tt_common
->hash_entry
);
2002 batadv_tt_global_entry_put(tt_global
);
2004 spin_unlock_bh(list_lock
);
2008 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
2010 struct batadv_hashtable
*hash
;
2011 spinlock_t
*list_lock
; /* protects write access to the hash lists */
2012 struct batadv_tt_common_entry
*tt_common_entry
;
2013 struct batadv_tt_global_entry
*tt_global
;
2014 struct hlist_node
*node_tmp
;
2015 struct hlist_head
*head
;
2018 if (!bat_priv
->tt
.global_hash
)
2021 hash
= bat_priv
->tt
.global_hash
;
2023 for (i
= 0; i
< hash
->size
; i
++) {
2024 head
= &hash
->table
[i
];
2025 list_lock
= &hash
->list_locks
[i
];
2027 spin_lock_bh(list_lock
);
2028 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
2030 hlist_del_rcu(&tt_common_entry
->hash_entry
);
2031 tt_global
= container_of(tt_common_entry
,
2032 struct batadv_tt_global_entry
,
2034 batadv_tt_global_entry_put(tt_global
);
2036 spin_unlock_bh(list_lock
);
2039 batadv_hash_destroy(hash
);
2041 bat_priv
->tt
.global_hash
= NULL
;
2045 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
2046 struct batadv_tt_global_entry
*tt_global_entry
)
2050 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
2051 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
2054 /* check if the two clients are marked as isolated */
2055 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
&&
2056 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
)
2063 * batadv_transtable_search - get the mesh destination for a given client
2064 * @bat_priv: the bat priv with all the soft interface information
2065 * @src: mac address of the source client
2066 * @addr: mac address of the destination client
2067 * @vid: VLAN identifier
2069 * Return: a pointer to the originator that was selected as destination in the
2070 * mesh for contacting the client 'addr', NULL otherwise.
2071 * In case of multiple originators serving the same client, the function returns
2072 * the best one (best in terms of metric towards the destination node).
2074 * If the two clients are AP isolated the function returns NULL.
2076 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
2081 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2082 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2083 struct batadv_orig_node
*orig_node
= NULL
;
2084 struct batadv_tt_orig_list_entry
*best_entry
;
2086 if (src
&& batadv_vlan_ap_isola_get(bat_priv
, vid
)) {
2087 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
, vid
);
2088 if (!tt_local_entry
||
2089 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
))
2093 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
2094 if (!tt_global_entry
)
2097 /* check whether the clients should not communicate due to AP
2100 if (tt_local_entry
&&
2101 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2105 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
2106 /* found anything? */
2108 orig_node
= best_entry
->orig_node
;
2109 if (orig_node
&& !kref_get_unless_zero(&orig_node
->refcount
))
2114 if (tt_global_entry
)
2115 batadv_tt_global_entry_put(tt_global_entry
);
2117 batadv_tt_local_entry_put(tt_local_entry
);
2123 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2124 * to the given orig_node
2125 * @bat_priv: the bat priv with all the soft interface information
2126 * @orig_node: originator for which the CRC should be computed
2127 * @vid: VLAN identifier for which the CRC32 has to be computed
2129 * This function computes the checksum for the global table corresponding to a
2130 * specific originator. In particular, the checksum is computed as follows: For
2131 * each client connected to the originator the CRC32C of the MAC address and the
2132 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2135 * The idea behind is that CRC32C should be used as much as possible in order to
2136 * produce a unique hash of the table, but since the order which is used to feed
2137 * the CRC32C function affects the result and since every node in the network
2138 * probably sorts the clients differently, the hash function cannot be directly
2139 * computed over the entire table. Hence the CRC32C is used only on
2140 * the single client entry, while all the results are then xor'ed together
2141 * because the XOR operation can combine them all while trying to reduce the
2142 * noise as much as possible.
2144 * Return: the checksum of the global table of a given originator.
2146 static u32
batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
2147 struct batadv_orig_node
*orig_node
,
2150 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2151 struct batadv_tt_common_entry
*tt_common
;
2152 struct batadv_tt_global_entry
*tt_global
;
2153 struct hlist_head
*head
;
2154 u32 i
, crc_tmp
, crc
= 0;
2158 for (i
= 0; i
< hash
->size
; i
++) {
2159 head
= &hash
->table
[i
];
2162 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2163 tt_global
= container_of(tt_common
,
2164 struct batadv_tt_global_entry
,
2166 /* compute the CRC only for entries belonging to the
2167 * VLAN identified by the vid passed as parameter
2169 if (tt_common
->vid
!= vid
)
2172 /* Roaming clients are in the global table for
2173 * consistency only. They don't have to be
2174 * taken into account while computing the
2177 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
2179 /* Temporary clients have not been announced yet, so
2180 * they have to be skipped while computing the global
2183 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
2186 /* find out if this global entry is announced by this
2189 if (!batadv_tt_global_entry_has_orig(tt_global
,
2193 /* use network order to read the VID: this ensures that
2194 * every node reads the bytes in the same order.
2196 tmp_vid
= htons(tt_common
->vid
);
2197 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2199 /* compute the CRC on flags that have to be kept in sync
2202 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2203 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2205 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2214 * batadv_tt_local_crc - calculates the checksum of the local table
2215 * @bat_priv: the bat priv with all the soft interface information
2216 * @vid: VLAN identifier for which the CRC32 has to be computed
2218 * For details about the computation, please refer to the documentation for
2219 * batadv_tt_global_crc().
2221 * Return: the checksum of the local table
2223 static u32
batadv_tt_local_crc(struct batadv_priv
*bat_priv
,
2226 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2227 struct batadv_tt_common_entry
*tt_common
;
2228 struct hlist_head
*head
;
2229 u32 i
, crc_tmp
, crc
= 0;
2233 for (i
= 0; i
< hash
->size
; i
++) {
2234 head
= &hash
->table
[i
];
2237 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2238 /* compute the CRC only for entries belonging to the
2239 * VLAN identified by vid
2241 if (tt_common
->vid
!= vid
)
2244 /* not yet committed clients have not to be taken into
2245 * account while computing the CRC
2247 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
2250 /* use network order to read the VID: this ensures that
2251 * every node reads the bytes in the same order.
2253 tmp_vid
= htons(tt_common
->vid
);
2254 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2256 /* compute the CRC on flags that have to be kept in sync
2259 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2260 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2262 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2270 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
2272 struct batadv_tt_req_node
*node
;
2273 struct hlist_node
*safe
;
2275 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2277 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2278 hlist_del_init(&node
->list
);
2282 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2285 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
2286 struct batadv_orig_node
*orig_node
,
2287 const void *tt_buff
,
2290 /* Replace the old buffer only if I received something in the
2291 * last OGM (the OGM could carry no changes)
2293 spin_lock_bh(&orig_node
->tt_buff_lock
);
2294 if (tt_buff_len
> 0) {
2295 kfree(orig_node
->tt_buff
);
2296 orig_node
->tt_buff_len
= 0;
2297 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
2298 if (orig_node
->tt_buff
) {
2299 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
2300 orig_node
->tt_buff_len
= tt_buff_len
;
2303 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2306 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
2308 struct batadv_tt_req_node
*node
;
2309 struct hlist_node
*safe
;
2311 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2312 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2313 if (batadv_has_timed_out(node
->issued_at
,
2314 BATADV_TT_REQUEST_TIMEOUT
)) {
2315 hlist_del_init(&node
->list
);
2319 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2323 * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2324 * @bat_priv: the bat priv with all the soft interface information
2325 * @orig_node: orig node this request is being issued for
2327 * Return: the pointer to the new tt_req_node struct if no request
2328 * has already been issued for this orig_node, NULL otherwise.
2330 static struct batadv_tt_req_node
*
2331 batadv_tt_req_node_new(struct batadv_priv
*bat_priv
,
2332 struct batadv_orig_node
*orig_node
)
2334 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
2336 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2337 hlist_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
2338 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
2339 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
2340 BATADV_TT_REQUEST_TIMEOUT
))
2344 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
2348 ether_addr_copy(tt_req_node
->addr
, orig_node
->orig
);
2349 tt_req_node
->issued_at
= jiffies
;
2351 hlist_add_head(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
2353 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2358 * batadv_tt_local_valid - verify that given tt entry is a valid one
2359 * @entry_ptr: to be checked local tt entry
2360 * @data_ptr: not used but definition required to satisfy the callback prototype
2362 * Return: true if the entry is a valid, false otherwise.
2364 static bool batadv_tt_local_valid(const void *entry_ptr
, const void *data_ptr
)
2366 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2368 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
2373 static bool batadv_tt_global_valid(const void *entry_ptr
,
2374 const void *data_ptr
)
2376 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2377 const struct batadv_tt_global_entry
*tt_global_entry
;
2378 const struct batadv_orig_node
*orig_node
= data_ptr
;
2380 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
2381 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
2384 tt_global_entry
= container_of(tt_common_entry
,
2385 struct batadv_tt_global_entry
,
2388 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
2392 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2394 * @bat_priv: the bat priv with all the soft interface information
2395 * @hash: hash table containing the tt entries
2396 * @tt_len: expected tvlv tt data buffer length in number of bytes
2397 * @tvlv_buff: pointer to the buffer to fill with the TT data
2398 * @valid_cb: function to filter tt change entries
2399 * @cb_data: data passed to the filter function as argument
2401 static void batadv_tt_tvlv_generate(struct batadv_priv
*bat_priv
,
2402 struct batadv_hashtable
*hash
,
2403 void *tvlv_buff
, u16 tt_len
,
2404 bool (*valid_cb
)(const void *,
2408 struct batadv_tt_common_entry
*tt_common_entry
;
2409 struct batadv_tvlv_tt_change
*tt_change
;
2410 struct hlist_head
*head
;
2411 u16 tt_tot
, tt_num_entries
= 0;
2414 tt_tot
= batadv_tt_entries(tt_len
);
2415 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_buff
;
2418 for (i
= 0; i
< hash
->size
; i
++) {
2419 head
= &hash
->table
[i
];
2421 hlist_for_each_entry_rcu(tt_common_entry
,
2423 if (tt_tot
== tt_num_entries
)
2426 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
2429 ether_addr_copy(tt_change
->addr
, tt_common_entry
->addr
);
2430 tt_change
->flags
= tt_common_entry
->flags
;
2431 tt_change
->vid
= htons(tt_common_entry
->vid
);
2432 memset(tt_change
->reserved
, 0,
2433 sizeof(tt_change
->reserved
));
2443 * batadv_tt_global_check_crc - check if all the CRCs are correct
2444 * @orig_node: originator for which the CRCs have to be checked
2445 * @tt_vlan: pointer to the first tvlv VLAN entry
2446 * @num_vlan: number of tvlv VLAN entries
2448 * Return: true if all the received CRCs match the locally stored ones, false
2451 static bool batadv_tt_global_check_crc(struct batadv_orig_node
*orig_node
,
2452 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2455 struct batadv_tvlv_tt_vlan_data
*tt_vlan_tmp
;
2456 struct batadv_orig_node_vlan
*vlan
;
2457 int i
, orig_num_vlan
;
2460 /* check if each received CRC matches the locally stored one */
2461 for (i
= 0; i
< num_vlan
; i
++) {
2462 tt_vlan_tmp
= tt_vlan
+ i
;
2464 /* if orig_node is a backbone node for this VLAN, don't check
2465 * the CRC as we ignore all the global entries over it
2467 if (batadv_bla_is_backbone_gw_orig(orig_node
->bat_priv
,
2469 ntohs(tt_vlan_tmp
->vid
)))
2472 vlan
= batadv_orig_node_vlan_get(orig_node
,
2473 ntohs(tt_vlan_tmp
->vid
));
2478 batadv_orig_node_vlan_put(vlan
);
2480 if (crc
!= ntohl(tt_vlan_tmp
->crc
))
2484 /* check if any excess VLANs exist locally for the originator
2485 * which are not mentioned in the TVLV from the originator.
2489 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
)
2493 if (orig_num_vlan
> num_vlan
)
2500 * batadv_tt_local_update_crc - update all the local CRCs
2501 * @bat_priv: the bat priv with all the soft interface information
2503 static void batadv_tt_local_update_crc(struct batadv_priv
*bat_priv
)
2505 struct batadv_softif_vlan
*vlan
;
2507 /* recompute the global CRC for each VLAN */
2509 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
2510 vlan
->tt
.crc
= batadv_tt_local_crc(bat_priv
, vlan
->vid
);
2516 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2517 * @bat_priv: the bat priv with all the soft interface information
2518 * @orig_node: the orig_node for which the CRCs have to be updated
2520 static void batadv_tt_global_update_crc(struct batadv_priv
*bat_priv
,
2521 struct batadv_orig_node
*orig_node
)
2523 struct batadv_orig_node_vlan
*vlan
;
2526 /* recompute the global CRC for each VLAN */
2528 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
2529 /* if orig_node is a backbone node for this VLAN, don't compute
2530 * the CRC as we ignore all the global entries over it
2532 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
,
2536 crc
= batadv_tt_global_crc(bat_priv
, orig_node
, vlan
->vid
);
2543 * batadv_send_tt_request - send a TT Request message to a given node
2544 * @bat_priv: the bat priv with all the soft interface information
2545 * @dst_orig_node: the destination of the message
2546 * @ttvn: the version number that the source of the message is looking for
2547 * @tt_vlan: pointer to the first tvlv VLAN object to request
2548 * @num_vlan: number of tvlv VLAN entries
2549 * @full_table: ask for the entire translation table if true, while only for the
2550 * last TT diff otherwise
2552 * Return: true if the TT Request was sent, false otherwise
2554 static bool batadv_send_tt_request(struct batadv_priv
*bat_priv
,
2555 struct batadv_orig_node
*dst_orig_node
,
2557 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2558 u16 num_vlan
, bool full_table
)
2560 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2561 struct batadv_tt_req_node
*tt_req_node
= NULL
;
2562 struct batadv_tvlv_tt_vlan_data
*tt_vlan_req
;
2563 struct batadv_hard_iface
*primary_if
;
2567 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2571 /* The new tt_req will be issued only if I'm not waiting for a
2572 * reply from the same orig_node yet
2574 tt_req_node
= batadv_tt_req_node_new(bat_priv
, dst_orig_node
);
2578 size
= sizeof(*tvlv_tt_data
) + sizeof(*tt_vlan_req
) * num_vlan
;
2579 tvlv_tt_data
= kzalloc(size
, GFP_ATOMIC
);
2583 tvlv_tt_data
->flags
= BATADV_TT_REQUEST
;
2584 tvlv_tt_data
->ttvn
= ttvn
;
2585 tvlv_tt_data
->num_vlan
= htons(num_vlan
);
2587 /* send all the CRCs within the request. This is needed by intermediate
2588 * nodes to ensure they have the correct table before replying
2590 tt_vlan_req
= (struct batadv_tvlv_tt_vlan_data
*)(tvlv_tt_data
+ 1);
2591 for (i
= 0; i
< num_vlan
; i
++) {
2592 tt_vlan_req
->vid
= tt_vlan
->vid
;
2593 tt_vlan_req
->crc
= tt_vlan
->crc
;
2600 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2602 batadv_dbg(BATADV_DBG_TT
, bat_priv
, "Sending TT_REQUEST to %pM [%c]\n",
2603 dst_orig_node
->orig
, full_table
? 'F' : '.');
2605 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
2606 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2607 dst_orig_node
->orig
, BATADV_TVLV_TT
, 1,
2608 tvlv_tt_data
, size
);
2613 batadv_hardif_put(primary_if
);
2614 if (ret
&& tt_req_node
) {
2615 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2616 /* hlist_del_init() verifies tt_req_node still is in the list */
2617 hlist_del_init(&tt_req_node
->list
);
2618 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2621 kfree(tvlv_tt_data
);
2626 * batadv_send_other_tt_response - send reply to tt request concerning another
2627 * node's translation table
2628 * @bat_priv: the bat priv with all the soft interface information
2629 * @tt_data: tt data containing the tt request information
2630 * @req_src: mac address of tt request sender
2631 * @req_dst: mac address of tt request recipient
2633 * Return: true if tt request reply was sent, false otherwise.
2635 static bool batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
2636 struct batadv_tvlv_tt_data
*tt_data
,
2637 u8
*req_src
, u8
*req_dst
)
2639 struct batadv_orig_node
*req_dst_orig_node
;
2640 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
2641 struct batadv_tvlv_tt_change
*tt_change
;
2642 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2643 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
2644 bool ret
= false, full_table
;
2645 u8 orig_ttvn
, req_ttvn
;
2649 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2650 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2651 req_src
, tt_data
->ttvn
, req_dst
,
2652 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2654 /* Let's get the orig node of the REAL destination */
2655 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_dst
);
2656 if (!req_dst_orig_node
)
2659 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2660 if (!res_dst_orig_node
)
2663 orig_ttvn
= (u8
)atomic_read(&req_dst_orig_node
->last_ttvn
);
2664 req_ttvn
= tt_data
->ttvn
;
2666 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
2667 /* this node doesn't have the requested data */
2668 if (orig_ttvn
!= req_ttvn
||
2669 !batadv_tt_global_check_crc(req_dst_orig_node
, tt_vlan
,
2670 ntohs(tt_data
->num_vlan
)))
2673 /* If the full table has been explicitly requested */
2674 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
||
2675 !req_dst_orig_node
->tt_buff
)
2680 /* TT fragmentation hasn't been implemented yet, so send as many
2681 * TT entries fit a single packet as possible only
2684 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
2685 tt_len
= req_dst_orig_node
->tt_buff_len
;
2687 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2694 /* Copy the last orig_node's OGM buffer */
2695 memcpy(tt_change
, req_dst_orig_node
->tt_buff
,
2696 req_dst_orig_node
->tt_buff_len
);
2697 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2699 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2700 * in the initial part
2703 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2710 /* fill the rest of the tvlv with the real TT entries */
2711 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.global_hash
,
2713 batadv_tt_global_valid
,
2717 /* Don't send the response, if larger than fragmented packet. */
2718 tt_len
= sizeof(struct batadv_unicast_tvlv_packet
) + tvlv_len
;
2719 if (tt_len
> atomic_read(&bat_priv
->packet_size_max
)) {
2720 net_ratelimited_function(batadv_info
, bat_priv
->soft_iface
,
2721 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2722 res_dst_orig_node
->orig
);
2726 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2727 tvlv_tt_data
->ttvn
= req_ttvn
;
2730 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2732 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2733 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2734 res_dst_orig_node
->orig
, req_dst_orig_node
->orig
,
2735 full_table
? 'F' : '.', req_ttvn
);
2737 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2739 batadv_tvlv_unicast_send(bat_priv
, req_dst_orig_node
->orig
,
2740 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2747 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2750 if (res_dst_orig_node
)
2751 batadv_orig_node_put(res_dst_orig_node
);
2752 if (req_dst_orig_node
)
2753 batadv_orig_node_put(req_dst_orig_node
);
2754 kfree(tvlv_tt_data
);
2759 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2761 * @bat_priv: the bat priv with all the soft interface information
2762 * @tt_data: tt data containing the tt request information
2763 * @req_src: mac address of tt request sender
2765 * Return: true if tt request reply was sent, false otherwise.
2767 static bool batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
2768 struct batadv_tvlv_tt_data
*tt_data
,
2771 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2772 struct batadv_hard_iface
*primary_if
= NULL
;
2773 struct batadv_tvlv_tt_change
*tt_change
;
2774 struct batadv_orig_node
*orig_node
;
2775 u8 my_ttvn
, req_ttvn
;
2780 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2781 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2782 req_src
, tt_data
->ttvn
,
2783 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2785 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
2787 my_ttvn
= (u8
)atomic_read(&bat_priv
->tt
.vn
);
2788 req_ttvn
= tt_data
->ttvn
;
2790 orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2794 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2798 /* If the full table has been explicitly requested or the gap
2799 * is too big send the whole local translation table
2801 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
2802 !bat_priv
->tt
.last_changeset
)
2807 /* TT fragmentation hasn't been implemented yet, so send as many
2808 * TT entries fit a single packet as possible only
2811 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
2813 tt_len
= bat_priv
->tt
.last_changeset_len
;
2814 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2821 /* Copy the last orig_node's OGM buffer */
2822 memcpy(tt_change
, bat_priv
->tt
.last_changeset
,
2823 bat_priv
->tt
.last_changeset_len
);
2824 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2826 req_ttvn
= (u8
)atomic_read(&bat_priv
->tt
.vn
);
2828 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2829 * in the initial part
2832 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2839 /* fill the rest of the tvlv with the real TT entries */
2840 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.local_hash
,
2842 batadv_tt_local_valid
, NULL
);
2845 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2846 tvlv_tt_data
->ttvn
= req_ttvn
;
2849 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2851 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2852 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2853 orig_node
->orig
, full_table
? 'F' : '.', req_ttvn
);
2855 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2857 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2858 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2864 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2866 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
2868 batadv_orig_node_put(orig_node
);
2870 batadv_hardif_put(primary_if
);
2871 kfree(tvlv_tt_data
);
2872 /* The packet was for this host, so it doesn't need to be re-routed */
2877 * batadv_send_tt_response - send reply to tt request
2878 * @bat_priv: the bat priv with all the soft interface information
2879 * @tt_data: tt data containing the tt request information
2880 * @req_src: mac address of tt request sender
2881 * @req_dst: mac address of tt request recipient
2883 * Return: true if tt request reply was sent, false otherwise.
2885 static bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
2886 struct batadv_tvlv_tt_data
*tt_data
,
2887 u8
*req_src
, u8
*req_dst
)
2889 if (batadv_is_my_mac(bat_priv
, req_dst
))
2890 return batadv_send_my_tt_response(bat_priv
, tt_data
, req_src
);
2891 return batadv_send_other_tt_response(bat_priv
, tt_data
, req_src
,
2895 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2896 struct batadv_orig_node
*orig_node
,
2897 struct batadv_tvlv_tt_change
*tt_change
,
2898 u16 tt_num_changes
, u8 ttvn
)
2903 for (i
= 0; i
< tt_num_changes
; i
++) {
2904 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
2905 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
2906 batadv_tt_global_del(bat_priv
, orig_node
,
2907 (tt_change
+ i
)->addr
,
2908 ntohs((tt_change
+ i
)->vid
),
2909 "tt removed by changes",
2912 if (!batadv_tt_global_add(bat_priv
, orig_node
,
2913 (tt_change
+ i
)->addr
,
2914 ntohs((tt_change
+ i
)->vid
),
2915 (tt_change
+ i
)->flags
, ttvn
))
2916 /* In case of problem while storing a
2917 * global_entry, we stop the updating
2918 * procedure without committing the
2919 * ttvn change. This will avoid to send
2920 * corrupted data on tt_request
2925 set_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
2928 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
2929 struct batadv_tvlv_tt_change
*tt_change
,
2930 u8 ttvn
, u8
*resp_src
,
2933 struct batadv_orig_node
*orig_node
;
2935 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2939 /* Purge the old table first.. */
2940 batadv_tt_global_del_orig(bat_priv
, orig_node
, -1,
2941 "Received full table");
2943 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
, num_entries
,
2946 spin_lock_bh(&orig_node
->tt_buff_lock
);
2947 kfree(orig_node
->tt_buff
);
2948 orig_node
->tt_buff_len
= 0;
2949 orig_node
->tt_buff
= NULL
;
2950 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2952 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2956 batadv_orig_node_put(orig_node
);
2959 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2960 struct batadv_orig_node
*orig_node
,
2961 u16 tt_num_changes
, u8 ttvn
,
2962 struct batadv_tvlv_tt_change
*tt_change
)
2964 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
2965 tt_num_changes
, ttvn
);
2967 batadv_tt_save_orig_buffer(bat_priv
, orig_node
, tt_change
,
2968 batadv_tt_len(tt_num_changes
));
2969 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2973 * batadv_is_my_client - check if a client is served by the local node
2974 * @bat_priv: the bat priv with all the soft interface information
2975 * @addr: the mac address of the client to check
2976 * @vid: VLAN identifier
2978 * Return: true if the client is served by this node, false otherwise.
2980 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const u8
*addr
,
2983 struct batadv_tt_local_entry
*tt_local_entry
;
2986 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
2987 if (!tt_local_entry
)
2989 /* Check if the client has been logically deleted (but is kept for
2990 * consistency purpose)
2992 if ((tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
) ||
2993 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
2998 batadv_tt_local_entry_put(tt_local_entry
);
3003 * batadv_handle_tt_response - process incoming tt reply
3004 * @bat_priv: the bat priv with all the soft interface information
3005 * @tt_data: tt data containing the tt request information
3006 * @resp_src: mac address of tt reply sender
3007 * @num_entries: number of tt change entries appended to the tt data
3009 static void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
3010 struct batadv_tvlv_tt_data
*tt_data
,
3011 u8
*resp_src
, u16 num_entries
)
3013 struct batadv_tt_req_node
*node
;
3014 struct hlist_node
*safe
;
3015 struct batadv_orig_node
*orig_node
= NULL
;
3016 struct batadv_tvlv_tt_change
*tt_change
;
3017 u8
*tvlv_ptr
= (u8
*)tt_data
;
3020 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3021 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3022 resp_src
, tt_data
->ttvn
, num_entries
,
3023 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
3025 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
3029 spin_lock_bh(&orig_node
->tt_lock
);
3031 change_offset
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3032 change_offset
*= ntohs(tt_data
->num_vlan
);
3033 change_offset
+= sizeof(*tt_data
);
3034 tvlv_ptr
+= change_offset
;
3036 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_ptr
;
3037 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
) {
3038 batadv_tt_fill_gtable(bat_priv
, tt_change
, tt_data
->ttvn
,
3039 resp_src
, num_entries
);
3041 batadv_tt_update_changes(bat_priv
, orig_node
, num_entries
,
3042 tt_data
->ttvn
, tt_change
);
3045 /* Recalculate the CRC for this orig_node and store it */
3046 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3048 spin_unlock_bh(&orig_node
->tt_lock
);
3050 /* Delete the tt_req_node from pending tt_requests list */
3051 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
3052 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
3053 if (!batadv_compare_eth(node
->addr
, resp_src
))
3055 hlist_del_init(&node
->list
);
3059 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
3062 batadv_orig_node_put(orig_node
);
3065 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
3067 struct batadv_tt_roam_node
*node
, *safe
;
3069 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3071 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
3072 list_del(&node
->list
);
3076 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3079 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
3081 struct batadv_tt_roam_node
*node
, *safe
;
3083 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3084 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
3085 if (!batadv_has_timed_out(node
->first_time
,
3086 BATADV_ROAMING_MAX_TIME
))
3089 list_del(&node
->list
);
3092 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3096 * batadv_tt_check_roam_count - check if a client has roamed too frequently
3097 * @bat_priv: the bat priv with all the soft interface information
3098 * @client: mac address of the roaming client
3100 * This function checks whether the client already reached the
3101 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3104 * Return: true if the ROAMING_ADV can be sent, false otherwise
3106 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
, u8
*client
)
3108 struct batadv_tt_roam_node
*tt_roam_node
;
3111 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3112 /* The new tt_req will be issued only if I'm not waiting for a
3113 * reply from the same orig_node yet
3115 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
3116 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
3119 if (batadv_has_timed_out(tt_roam_node
->first_time
,
3120 BATADV_ROAMING_MAX_TIME
))
3123 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
3124 /* Sorry, you roamed too many times! */
3131 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
3135 tt_roam_node
->first_time
= jiffies
;
3136 atomic_set(&tt_roam_node
->counter
,
3137 BATADV_ROAMING_MAX_COUNT
- 1);
3138 ether_addr_copy(tt_roam_node
->addr
, client
);
3140 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
3145 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3150 * batadv_send_roam_adv - send a roaming advertisement message
3151 * @bat_priv: the bat priv with all the soft interface information
3152 * @client: mac address of the roaming client
3153 * @vid: VLAN identifier
3154 * @orig_node: message destination
3156 * Send a ROAMING_ADV message to the node which was previously serving this
3157 * client. This is done to inform the node that from now on all traffic destined
3158 * for this particular roamed client has to be forwarded to the sender of the
3161 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, u8
*client
,
3163 struct batadv_orig_node
*orig_node
)
3165 struct batadv_hard_iface
*primary_if
;
3166 struct batadv_tvlv_roam_adv tvlv_roam
;
3168 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3172 /* before going on we have to check whether the client has
3173 * already roamed to us too many times
3175 if (!batadv_tt_check_roam_count(bat_priv
, client
))
3178 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3179 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3180 orig_node
->orig
, client
, BATADV_PRINT_VID(vid
));
3182 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
3184 memcpy(tvlv_roam
.client
, client
, sizeof(tvlv_roam
.client
));
3185 tvlv_roam
.vid
= htons(vid
);
3187 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3188 orig_node
->orig
, BATADV_TVLV_ROAM
, 1,
3189 &tvlv_roam
, sizeof(tvlv_roam
));
3193 batadv_hardif_put(primary_if
);
3196 static void batadv_tt_purge(struct work_struct
*work
)
3198 struct delayed_work
*delayed_work
;
3199 struct batadv_priv_tt
*priv_tt
;
3200 struct batadv_priv
*bat_priv
;
3202 delayed_work
= to_delayed_work(work
);
3203 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
3204 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
3206 batadv_tt_local_purge(bat_priv
, BATADV_TT_LOCAL_TIMEOUT
);
3207 batadv_tt_global_purge(bat_priv
);
3208 batadv_tt_req_purge(bat_priv
);
3209 batadv_tt_roam_purge(bat_priv
);
3211 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3212 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3215 void batadv_tt_free(struct batadv_priv
*bat_priv
)
3217 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3218 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3220 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
3222 batadv_tt_local_table_free(bat_priv
);
3223 batadv_tt_global_table_free(bat_priv
);
3224 batadv_tt_req_list_free(bat_priv
);
3225 batadv_tt_changes_list_free(bat_priv
);
3226 batadv_tt_roam_list_free(bat_priv
);
3228 kfree(bat_priv
->tt
.last_changeset
);
3232 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3233 * table and possibly count them in the TT size
3234 * @bat_priv: the bat priv with all the soft interface information
3235 * @flags: the flag to switch
3236 * @enable: whether to set or unset the flag
3237 * @count: whether to increase the TT size by the number of changed entries
3239 static void batadv_tt_local_set_flags(struct batadv_priv
*bat_priv
, u16 flags
,
3240 bool enable
, bool count
)
3242 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3243 struct batadv_tt_common_entry
*tt_common_entry
;
3244 u16 changed_num
= 0;
3245 struct hlist_head
*head
;
3251 for (i
= 0; i
< hash
->size
; i
++) {
3252 head
= &hash
->table
[i
];
3255 hlist_for_each_entry_rcu(tt_common_entry
,
3258 if ((tt_common_entry
->flags
& flags
) == flags
)
3260 tt_common_entry
->flags
|= flags
;
3262 if (!(tt_common_entry
->flags
& flags
))
3264 tt_common_entry
->flags
&= ~flags
;
3271 batadv_tt_local_size_inc(bat_priv
,
3272 tt_common_entry
->vid
);
3278 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3279 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
3281 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3282 struct batadv_tt_common_entry
*tt_common
;
3283 struct batadv_tt_local_entry
*tt_local
;
3284 struct hlist_node
*node_tmp
;
3285 struct hlist_head
*head
;
3286 spinlock_t
*list_lock
; /* protects write access to the hash lists */
3292 for (i
= 0; i
< hash
->size
; i
++) {
3293 head
= &hash
->table
[i
];
3294 list_lock
= &hash
->list_locks
[i
];
3296 spin_lock_bh(list_lock
);
3297 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
3299 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
3302 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3303 "Deleting local tt entry (%pM, vid: %d): pending\n",
3305 BATADV_PRINT_VID(tt_common
->vid
));
3307 batadv_tt_local_size_dec(bat_priv
, tt_common
->vid
);
3308 hlist_del_rcu(&tt_common
->hash_entry
);
3309 tt_local
= container_of(tt_common
,
3310 struct batadv_tt_local_entry
,
3313 batadv_tt_local_entry_put(tt_local
);
3315 spin_unlock_bh(list_lock
);
3320 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3321 * which have been queued in the time since the last commit
3322 * @bat_priv: the bat priv with all the soft interface information
3324 * Caller must hold tt->commit_lock.
3326 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv
*bat_priv
)
3328 lockdep_assert_held(&bat_priv
->tt
.commit_lock
);
3330 /* Update multicast addresses in local translation table */
3331 batadv_mcast_mla_update(bat_priv
);
3333 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1) {
3334 if (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))
3335 batadv_tt_tvlv_container_update(bat_priv
);
3339 batadv_tt_local_set_flags(bat_priv
, BATADV_TT_CLIENT_NEW
, false, true);
3341 batadv_tt_local_purge_pending_clients(bat_priv
);
3342 batadv_tt_local_update_crc(bat_priv
);
3344 /* Increment the TTVN only once per OGM interval */
3345 atomic_inc(&bat_priv
->tt
.vn
);
3346 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3347 "Local changes committed, updating to ttvn %u\n",
3348 (u8
)atomic_read(&bat_priv
->tt
.vn
));
3350 /* reset the sending counter */
3351 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
3352 batadv_tt_tvlv_container_update(bat_priv
);
3356 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3357 * have been queued in the time since the last commit
3358 * @bat_priv: the bat priv with all the soft interface information
3360 void batadv_tt_local_commit_changes(struct batadv_priv
*bat_priv
)
3362 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3363 batadv_tt_local_commit_changes_nolock(bat_priv
);
3364 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3367 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, u8
*src
, u8
*dst
,
3370 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
3371 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
3372 struct batadv_softif_vlan
*vlan
;
3375 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
3379 if (!atomic_read(&vlan
->ap_isolation
))
3382 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
, vid
);
3383 if (!tt_local_entry
)
3386 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
, vid
);
3387 if (!tt_global_entry
)
3390 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
3396 batadv_softif_vlan_put(vlan
);
3397 if (tt_global_entry
)
3398 batadv_tt_global_entry_put(tt_global_entry
);
3400 batadv_tt_local_entry_put(tt_local_entry
);
3405 * batadv_tt_update_orig - update global translation table with new tt
3406 * information received via ogms
3407 * @bat_priv: the bat priv with all the soft interface information
3408 * @orig_node: the orig_node of the ogm
3409 * @tt_buff: pointer to the first tvlv VLAN entry
3410 * @tt_num_vlan: number of tvlv VLAN entries
3411 * @tt_change: pointer to the first entry in the TT buffer
3412 * @tt_num_changes: number of tt changes inside the tt buffer
3413 * @ttvn: translation table version number of this changeset
3415 static void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
3416 struct batadv_orig_node
*orig_node
,
3417 const void *tt_buff
, u16 tt_num_vlan
,
3418 struct batadv_tvlv_tt_change
*tt_change
,
3419 u16 tt_num_changes
, u8 ttvn
)
3421 u8 orig_ttvn
= (u8
)atomic_read(&orig_node
->last_ttvn
);
3422 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3423 bool full_table
= true;
3426 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)tt_buff
;
3427 has_tt_init
= test_bit(BATADV_ORIG_CAPA_HAS_TT
,
3428 &orig_node
->capa_initialized
);
3430 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3431 * increased by one -> we can apply the attached changes
3433 if ((!has_tt_init
&& ttvn
== 1) || ttvn
- orig_ttvn
== 1) {
3434 /* the OGM could not contain the changes due to their size or
3435 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3437 * In this case send a tt request
3439 if (!tt_num_changes
) {
3444 spin_lock_bh(&orig_node
->tt_lock
);
3446 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
3449 /* Even if we received the precomputed crc with the OGM, we
3450 * prefer to recompute it to spot any possible inconsistency
3451 * in the global table
3453 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3455 spin_unlock_bh(&orig_node
->tt_lock
);
3457 /* The ttvn alone is not enough to guarantee consistency
3458 * because a single value could represent different states
3459 * (due to the wrap around). Thus a node has to check whether
3460 * the resulting table (after applying the changes) is still
3461 * consistent or not. E.g. a node could disconnect while its
3462 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3463 * checking the CRC value is mandatory to detect the
3466 if (!batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3470 /* if we missed more than one change or our tables are not
3471 * in sync anymore -> request fresh tt data
3473 if (!has_tt_init
|| ttvn
!= orig_ttvn
||
3474 !batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3477 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3478 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3479 orig_node
->orig
, ttvn
, orig_ttvn
,
3481 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
3482 tt_vlan
, tt_num_vlan
,
3490 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3491 * @bat_priv: the bat priv with all the soft interface information
3492 * @addr: the mac address of the client to check
3493 * @vid: VLAN identifier
3495 * Return: true if we know that the client has moved from its old originator
3496 * to another one. This entry is still kept for consistency purposes and will be
3497 * deleted later by a DEL or because of timeout
3499 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
3500 u8
*addr
, unsigned short vid
)
3502 struct batadv_tt_global_entry
*tt_global_entry
;
3505 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3506 if (!tt_global_entry
)
3509 ret
= tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3510 batadv_tt_global_entry_put(tt_global_entry
);
3516 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3517 * @bat_priv: the bat priv with all the soft interface information
3518 * @addr: the mac address of the local client to query
3519 * @vid: VLAN identifier
3521 * Return: true if the local client is known to be roaming (it is not served by
3522 * this node anymore) or not. If yes, the client is still present in the table
3523 * to keep the latter consistent with the node TTVN
3525 bool batadv_tt_local_client_is_roaming(struct batadv_priv
*bat_priv
,
3526 u8
*addr
, unsigned short vid
)
3528 struct batadv_tt_local_entry
*tt_local_entry
;
3531 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
3532 if (!tt_local_entry
)
3535 ret
= tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3536 batadv_tt_local_entry_put(tt_local_entry
);
3541 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
3542 struct batadv_orig_node
*orig_node
,
3543 const unsigned char *addr
,
3548 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
, vid
,
3549 BATADV_TT_CLIENT_TEMP
,
3550 atomic_read(&orig_node
->last_ttvn
)))
3553 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3554 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3555 addr
, BATADV_PRINT_VID(vid
), orig_node
->orig
);
3562 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3563 * maximum packet size that can be transported through the mesh
3564 * @soft_iface: netdev struct of the mesh interface
3566 * Remove entries older than 'timeout' and half timeout if more entries need
3569 void batadv_tt_local_resize_to_mtu(struct net_device
*soft_iface
)
3571 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
3572 int packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
3573 int table_size
, timeout
= BATADV_TT_LOCAL_TIMEOUT
/ 2;
3574 bool reduced
= false;
3576 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3579 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
3580 if (packet_size_max
>= table_size
)
3583 batadv_tt_local_purge(bat_priv
, timeout
);
3584 batadv_tt_local_purge_pending_clients(bat_priv
);
3588 net_ratelimited_function(batadv_info
, soft_iface
,
3589 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3593 /* commit these changes immediately, to avoid synchronization problem
3597 batadv_tt_local_commit_changes_nolock(bat_priv
);
3599 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3603 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3604 * @bat_priv: the bat priv with all the soft interface information
3605 * @orig: the orig_node of the ogm
3606 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3607 * @tvlv_value: tvlv buffer containing the gateway data
3608 * @tvlv_value_len: tvlv buffer length
3610 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
3611 struct batadv_orig_node
*orig
,
3612 u8 flags
, void *tvlv_value
,
3615 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3616 struct batadv_tvlv_tt_change
*tt_change
;
3617 struct batadv_tvlv_tt_data
*tt_data
;
3618 u16 num_entries
, num_vlan
;
3620 if (tvlv_value_len
< sizeof(*tt_data
))
3623 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3624 tvlv_value_len
-= sizeof(*tt_data
);
3626 num_vlan
= ntohs(tt_data
->num_vlan
);
3628 if (tvlv_value_len
< sizeof(*tt_vlan
) * num_vlan
)
3631 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
3632 tt_change
= (struct batadv_tvlv_tt_change
*)(tt_vlan
+ num_vlan
);
3633 tvlv_value_len
-= sizeof(*tt_vlan
) * num_vlan
;
3635 num_entries
= batadv_tt_entries(tvlv_value_len
);
3637 batadv_tt_update_orig(bat_priv
, orig
, tt_vlan
, num_vlan
, tt_change
,
3638 num_entries
, tt_data
->ttvn
);
3642 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3644 * @bat_priv: the bat priv with all the soft interface information
3645 * @src: mac address of tt tvlv sender
3646 * @dst: mac address of tt tvlv recipient
3647 * @tvlv_value: tvlv buffer containing the tt data
3648 * @tvlv_value_len: tvlv buffer length
3650 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3653 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3658 struct batadv_tvlv_tt_data
*tt_data
;
3659 u16 tt_vlan_len
, tt_num_entries
;
3663 if (tvlv_value_len
< sizeof(*tt_data
))
3664 return NET_RX_SUCCESS
;
3666 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3667 tvlv_value_len
-= sizeof(*tt_data
);
3669 tt_vlan_len
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3670 tt_vlan_len
*= ntohs(tt_data
->num_vlan
);
3672 if (tvlv_value_len
< tt_vlan_len
)
3673 return NET_RX_SUCCESS
;
3675 tvlv_value_len
-= tt_vlan_len
;
3676 tt_num_entries
= batadv_tt_entries(tvlv_value_len
);
3678 switch (tt_data
->flags
& BATADV_TT_DATA_TYPE_MASK
) {
3679 case BATADV_TT_REQUEST
:
3680 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
3682 /* If this node cannot provide a TT response the tt_request is
3685 ret
= batadv_send_tt_response(bat_priv
, tt_data
, src
, dst
);
3687 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3692 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3693 "Routing TT_REQUEST to %pM [%c]\n",
3695 /* tvlv API will re-route the packet */
3699 case BATADV_TT_RESPONSE
:
3700 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
3702 if (batadv_is_my_mac(bat_priv
, dst
)) {
3703 batadv_handle_tt_response(bat_priv
, tt_data
,
3704 src
, tt_num_entries
);
3705 return NET_RX_SUCCESS
;
3708 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3713 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3714 "Routing TT_RESPONSE to %pM [%c]\n", dst
, tt_flag
);
3716 /* tvlv API will re-route the packet */
3720 return NET_RX_SUCCESS
;
3724 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3725 * @bat_priv: the bat priv with all the soft interface information
3726 * @src: mac address of tt tvlv sender
3727 * @dst: mac address of tt tvlv recipient
3728 * @tvlv_value: tvlv buffer containing the tt data
3729 * @tvlv_value_len: tvlv buffer length
3731 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3734 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3739 struct batadv_tvlv_roam_adv
*roaming_adv
;
3740 struct batadv_orig_node
*orig_node
= NULL
;
3742 /* If this node is not the intended recipient of the
3743 * roaming advertisement the packet is forwarded
3744 * (the tvlv API will re-route the packet).
3746 if (!batadv_is_my_mac(bat_priv
, dst
))
3749 if (tvlv_value_len
< sizeof(*roaming_adv
))
3752 orig_node
= batadv_orig_hash_find(bat_priv
, src
);
3756 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
3757 roaming_adv
= (struct batadv_tvlv_roam_adv
*)tvlv_value
;
3759 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3760 "Received ROAMING_ADV from %pM (client %pM)\n",
3761 src
, roaming_adv
->client
);
3763 batadv_tt_global_add(bat_priv
, orig_node
, roaming_adv
->client
,
3764 ntohs(roaming_adv
->vid
), BATADV_TT_CLIENT_ROAM
,
3765 atomic_read(&orig_node
->last_ttvn
) + 1);
3769 batadv_orig_node_put(orig_node
);
3770 return NET_RX_SUCCESS
;
3774 * batadv_tt_init - initialise the translation table internals
3775 * @bat_priv: the bat priv with all the soft interface information
3777 * Return: 0 on success or negative error number in case of failure.
3779 int batadv_tt_init(struct batadv_priv
*bat_priv
)
3783 /* synchronized flags must be remote */
3784 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK
& BATADV_TT_REMOTE_MASK
));
3786 ret
= batadv_tt_local_init(bat_priv
);
3790 ret
= batadv_tt_global_init(bat_priv
);
3794 batadv_tvlv_handler_register(bat_priv
, batadv_tt_tvlv_ogm_handler_v1
,
3795 batadv_tt_tvlv_unicast_handler_v1
,
3796 BATADV_TVLV_TT
, 1, BATADV_NO_FLAGS
);
3798 batadv_tvlv_handler_register(bat_priv
, NULL
,
3799 batadv_roam_tvlv_unicast_handler_v1
,
3800 BATADV_TVLV_ROAM
, 1, BATADV_NO_FLAGS
);
3802 INIT_DELAYED_WORK(&bat_priv
->tt
.work
, batadv_tt_purge
);
3803 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3804 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3810 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3811 * @bat_priv: the bat priv with all the soft interface information
3812 * @addr: the mac address of the client
3813 * @vid: the identifier of the VLAN where this client is connected
3815 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
3818 bool batadv_tt_global_is_isolated(struct batadv_priv
*bat_priv
,
3819 const u8
*addr
, unsigned short vid
)
3821 struct batadv_tt_global_entry
*tt
;
3824 tt
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3828 ret
= tt
->common
.flags
& BATADV_TT_CLIENT_ISOLA
;
3830 batadv_tt_global_entry_put(tt
);