1 /* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "translation-table.h"
21 #include <linux/atomic.h>
22 #include <linux/bug.h>
23 #include <linux/byteorder/generic.h>
24 #include <linux/compiler.h>
25 #include <linux/crc32c.h>
26 #include <linux/errno.h>
27 #include <linux/etherdevice.h>
29 #include <linux/if_ether.h>
30 #include <linux/jhash.h>
31 #include <linux/jiffies.h>
32 #include <linux/kernel.h>
33 #include <linux/list.h>
34 #include <linux/lockdep.h>
35 #include <linux/netdevice.h>
36 #include <linux/rculist.h>
37 #include <linux/rcupdate.h>
38 #include <linux/seq_file.h>
39 #include <linux/slab.h>
40 #include <linux/spinlock.h>
41 #include <linux/stddef.h>
42 #include <linux/string.h>
43 #include <linux/workqueue.h>
44 #include <net/net_namespace.h>
46 #include "bridge_loop_avoidance.h"
47 #include "hard-interface.h"
49 #include "multicast.h"
50 #include "originator.h"
52 #include "soft-interface.h"
55 static struct lock_class_key batadv_tt_local_hash_lock_class_key
;
56 static struct lock_class_key batadv_tt_global_hash_lock_class_key
;
58 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
60 struct batadv_orig_node
*orig_node
);
61 static void batadv_tt_purge(struct work_struct
*work
);
63 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
);
64 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
65 struct batadv_orig_node
*orig_node
,
66 const unsigned char *addr
,
67 unsigned short vid
, const char *message
,
70 /* returns 1 if they are the same mac addr */
71 static int batadv_compare_tt(const struct hlist_node
*node
, const void *data2
)
73 const void *data1
= container_of(node
, struct batadv_tt_common_entry
,
76 return batadv_compare_eth(data1
, data2
);
80 * batadv_choose_tt - return the index of the tt entry in the hash table
81 * @data: pointer to the tt_common_entry object to map
82 * @size: the size of the hash table
84 * Returns the hash index where the object represented by 'data' should be
87 static inline uint32_t batadv_choose_tt(const void *data
, uint32_t size
)
89 struct batadv_tt_common_entry
*tt
;
92 tt
= (struct batadv_tt_common_entry
*)data
;
93 hash
= jhash(&tt
->addr
, ETH_ALEN
, hash
);
94 hash
= jhash(&tt
->vid
, sizeof(tt
->vid
), hash
);
100 * batadv_tt_hash_find - look for a client in the given hash table
101 * @hash: the hash table to search
102 * @addr: the mac address of the client to look for
103 * @vid: VLAN identifier
105 * Returns a pointer to the tt_common struct belonging to the searched client if
106 * found, NULL otherwise.
108 static struct batadv_tt_common_entry
*
109 batadv_tt_hash_find(struct batadv_hashtable
*hash
, const uint8_t *addr
,
112 struct hlist_head
*head
;
113 struct batadv_tt_common_entry to_search
, *tt
, *tt_tmp
= NULL
;
119 ether_addr_copy(to_search
.addr
, addr
);
122 index
= batadv_choose_tt(&to_search
, hash
->size
);
123 head
= &hash
->table
[index
];
126 hlist_for_each_entry_rcu(tt
, head
, hash_entry
) {
127 if (!batadv_compare_eth(tt
, addr
))
133 if (!atomic_inc_not_zero(&tt
->refcount
))
145 * batadv_tt_local_hash_find - search the local table for a given client
146 * @bat_priv: the bat priv with all the soft interface information
147 * @addr: the mac address of the client to look for
148 * @vid: VLAN identifier
150 * Returns a pointer to the corresponding tt_local_entry struct if the client is
151 * found, NULL otherwise.
153 static struct batadv_tt_local_entry
*
154 batadv_tt_local_hash_find(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
157 struct batadv_tt_common_entry
*tt_common_entry
;
158 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
160 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.local_hash
, addr
,
163 tt_local_entry
= container_of(tt_common_entry
,
164 struct batadv_tt_local_entry
,
166 return tt_local_entry
;
170 * batadv_tt_global_hash_find - search the global table for a given client
171 * @bat_priv: the bat priv with all the soft interface information
172 * @addr: the mac address of the client to look for
173 * @vid: VLAN identifier
175 * Returns a pointer to the corresponding tt_global_entry struct if the client
176 * is found, NULL otherwise.
178 static struct batadv_tt_global_entry
*
179 batadv_tt_global_hash_find(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
182 struct batadv_tt_common_entry
*tt_common_entry
;
183 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
185 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.global_hash
, addr
,
188 tt_global_entry
= container_of(tt_common_entry
,
189 struct batadv_tt_global_entry
,
191 return tt_global_entry
;
195 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry
*tt_local_entry
)
197 if (atomic_dec_and_test(&tt_local_entry
->common
.refcount
))
198 kfree_rcu(tt_local_entry
, common
.rcu
);
202 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
203 * tt_global_entry and possibly free it
204 * @tt_global_entry: the object to free
207 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry
*tt_global_entry
)
209 if (atomic_dec_and_test(&tt_global_entry
->common
.refcount
)) {
210 batadv_tt_global_del_orig_list(tt_global_entry
);
211 kfree_rcu(tt_global_entry
, common
.rcu
);
216 * batadv_tt_global_hash_count - count the number of orig entries
217 * @hash: hash table containing the tt entries
218 * @addr: the mac address of the client to count entries for
219 * @vid: VLAN identifier
221 * Return the number of originators advertising the given address/data
222 * (excluding ourself).
224 int batadv_tt_global_hash_count(struct batadv_priv
*bat_priv
,
225 const uint8_t *addr
, unsigned short vid
)
227 struct batadv_tt_global_entry
*tt_global_entry
;
230 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
231 if (!tt_global_entry
)
234 count
= atomic_read(&tt_global_entry
->orig_list_count
);
235 batadv_tt_global_entry_free_ref(tt_global_entry
);
240 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head
*rcu
)
242 struct batadv_tt_orig_list_entry
*orig_entry
;
244 orig_entry
= container_of(rcu
, struct batadv_tt_orig_list_entry
, rcu
);
246 /* We are in an rcu callback here, therefore we cannot use
247 * batadv_orig_node_free_ref() and its call_rcu():
248 * An rcu_barrier() wouldn't wait for that to finish
250 batadv_orig_node_free_ref_now(orig_entry
->orig_node
);
255 * batadv_tt_local_size_mod - change the size by v of the local table identified
257 * @bat_priv: the bat priv with all the soft interface information
258 * @vid: the VLAN identifier of the sub-table to change
259 * @v: the amount to sum to the local table size
261 static void batadv_tt_local_size_mod(struct batadv_priv
*bat_priv
,
262 unsigned short vid
, int v
)
264 struct batadv_softif_vlan
*vlan
;
266 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
270 atomic_add(v
, &vlan
->tt
.num_entries
);
272 batadv_softif_vlan_free_ref(vlan
);
276 * batadv_tt_local_size_inc - increase by one the local table size for the given
278 * @bat_priv: the bat priv with all the soft interface information
279 * @vid: the VLAN identifier
281 static void batadv_tt_local_size_inc(struct batadv_priv
*bat_priv
,
284 batadv_tt_local_size_mod(bat_priv
, vid
, 1);
288 * batadv_tt_local_size_dec - decrease by one the local table size for the given
290 * @bat_priv: the bat priv with all the soft interface information
291 * @vid: the VLAN identifier
293 static void batadv_tt_local_size_dec(struct batadv_priv
*bat_priv
,
296 batadv_tt_local_size_mod(bat_priv
, vid
, -1);
300 * batadv_tt_global_size_mod - change the size by v of the local table
302 * @bat_priv: the bat priv with all the soft interface information
303 * @vid: the VLAN identifier
304 * @v: the amount to sum to the global table size
306 static void batadv_tt_global_size_mod(struct batadv_orig_node
*orig_node
,
307 unsigned short vid
, int v
)
309 struct batadv_orig_node_vlan
*vlan
;
311 vlan
= batadv_orig_node_vlan_new(orig_node
, vid
);
315 if (atomic_add_return(v
, &vlan
->tt
.num_entries
) == 0) {
316 spin_lock_bh(&orig_node
->vlan_list_lock
);
317 list_del_rcu(&vlan
->list
);
318 spin_unlock_bh(&orig_node
->vlan_list_lock
);
319 batadv_orig_node_vlan_free_ref(vlan
);
322 batadv_orig_node_vlan_free_ref(vlan
);
326 * batadv_tt_global_size_inc - increase by one the global table size for the
328 * @orig_node: the originator which global table size has to be decreased
329 * @vid: the vlan identifier
331 static void batadv_tt_global_size_inc(struct batadv_orig_node
*orig_node
,
334 batadv_tt_global_size_mod(orig_node
, vid
, 1);
338 * batadv_tt_global_size_dec - decrease by one the global table size for the
340 * @orig_node: the originator which global table size has to be decreased
341 * @vid: the vlan identifier
343 static void batadv_tt_global_size_dec(struct batadv_orig_node
*orig_node
,
346 batadv_tt_global_size_mod(orig_node
, vid
, -1);
350 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry
*orig_entry
)
352 if (!atomic_dec_and_test(&orig_entry
->refcount
))
355 call_rcu(&orig_entry
->rcu
, batadv_tt_orig_list_entry_free_rcu
);
359 * batadv_tt_local_event - store a local TT event (ADD/DEL)
360 * @bat_priv: the bat priv with all the soft interface information
361 * @tt_local_entry: the TT entry involved in the event
362 * @event_flags: flags to store in the event structure
364 static void batadv_tt_local_event(struct batadv_priv
*bat_priv
,
365 struct batadv_tt_local_entry
*tt_local_entry
,
368 struct batadv_tt_change_node
*tt_change_node
, *entry
, *safe
;
369 struct batadv_tt_common_entry
*common
= &tt_local_entry
->common
;
370 uint8_t flags
= common
->flags
| event_flags
;
371 bool event_removed
= false;
372 bool del_op_requested
, del_op_entry
;
374 tt_change_node
= kmalloc(sizeof(*tt_change_node
), GFP_ATOMIC
);
378 tt_change_node
->change
.flags
= flags
;
379 memset(tt_change_node
->change
.reserved
, 0,
380 sizeof(tt_change_node
->change
.reserved
));
381 ether_addr_copy(tt_change_node
->change
.addr
, common
->addr
);
382 tt_change_node
->change
.vid
= htons(common
->vid
);
384 del_op_requested
= flags
& BATADV_TT_CLIENT_DEL
;
386 /* check for ADD+DEL or DEL+ADD events */
387 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
388 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
390 if (!batadv_compare_eth(entry
->change
.addr
, common
->addr
))
393 /* DEL+ADD in the same orig interval have no effect and can be
394 * removed to avoid silly behaviour on the receiver side. The
395 * other way around (ADD+DEL) can happen in case of roaming of
396 * a client still in the NEW state. Roaming of NEW clients is
397 * now possible due to automatically recognition of "temporary"
400 del_op_entry
= entry
->change
.flags
& BATADV_TT_CLIENT_DEL
;
401 if (!del_op_requested
&& del_op_entry
)
403 if (del_op_requested
&& !del_op_entry
)
406 /* this is a second add in the same originator interval. It
407 * means that flags have been changed: update them!
409 if (!del_op_requested
&& !del_op_entry
)
410 entry
->change
.flags
= flags
;
414 list_del(&entry
->list
);
416 kfree(tt_change_node
);
417 event_removed
= true;
421 /* track the change in the OGMinterval list */
422 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt
.changes_list
);
425 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
428 atomic_dec(&bat_priv
->tt
.local_changes
);
430 atomic_inc(&bat_priv
->tt
.local_changes
);
434 * batadv_tt_len - compute length in bytes of given number of tt changes
435 * @changes_num: number of tt changes
437 * Returns computed length in bytes.
439 static int batadv_tt_len(int changes_num
)
441 return changes_num
* sizeof(struct batadv_tvlv_tt_change
);
445 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
446 * @tt_len: available space
448 * Returns the number of entries.
450 static uint16_t batadv_tt_entries(uint16_t tt_len
)
452 return tt_len
/ batadv_tt_len(1);
456 * batadv_tt_local_table_transmit_size - calculates the local translation table
457 * size when transmitted over the air
458 * @bat_priv: the bat priv with all the soft interface information
460 * Returns local translation table size in bytes.
462 static int batadv_tt_local_table_transmit_size(struct batadv_priv
*bat_priv
)
464 uint16_t num_vlan
= 0, tt_local_entries
= 0;
465 struct batadv_softif_vlan
*vlan
;
469 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
471 tt_local_entries
+= atomic_read(&vlan
->tt
.num_entries
);
475 /* header size of tvlv encapsulated tt response payload */
476 hdr_size
= sizeof(struct batadv_unicast_tvlv_packet
);
477 hdr_size
+= sizeof(struct batadv_tvlv_hdr
);
478 hdr_size
+= sizeof(struct batadv_tvlv_tt_data
);
479 hdr_size
+= num_vlan
* sizeof(struct batadv_tvlv_tt_vlan_data
);
481 return hdr_size
+ batadv_tt_len(tt_local_entries
);
484 static int batadv_tt_local_init(struct batadv_priv
*bat_priv
)
486 if (bat_priv
->tt
.local_hash
)
489 bat_priv
->tt
.local_hash
= batadv_hash_new(1024);
491 if (!bat_priv
->tt
.local_hash
)
494 batadv_hash_set_lock_class(bat_priv
->tt
.local_hash
,
495 &batadv_tt_local_hash_lock_class_key
);
500 static void batadv_tt_global_free(struct batadv_priv
*bat_priv
,
501 struct batadv_tt_global_entry
*tt_global
,
504 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
505 "Deleting global tt entry %pM (vid: %d): %s\n",
506 tt_global
->common
.addr
,
507 BATADV_PRINT_VID(tt_global
->common
.vid
), message
);
509 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
510 batadv_choose_tt
, &tt_global
->common
);
511 batadv_tt_global_entry_free_ref(tt_global
);
515 * batadv_tt_local_add - add a new client to the local table or update an
517 * @soft_iface: netdev struct of the mesh interface
518 * @addr: the mac address of the client to add
519 * @vid: VLAN identifier
520 * @ifindex: index of the interface where the client is connected to (useful to
521 * identify wireless clients)
522 * @mark: the value contained in the skb->mark field of the received packet (if
525 * Returns true if the client was successfully added, false otherwise.
527 bool batadv_tt_local_add(struct net_device
*soft_iface
, const uint8_t *addr
,
528 unsigned short vid
, int ifindex
, uint32_t mark
)
530 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
531 struct batadv_tt_local_entry
*tt_local
;
532 struct batadv_tt_global_entry
*tt_global
= NULL
;
533 struct batadv_softif_vlan
*vlan
;
534 struct net_device
*in_dev
= NULL
;
535 struct hlist_head
*head
;
536 struct batadv_tt_orig_list_entry
*orig_entry
;
537 int hash_added
, table_size
, packet_size_max
;
538 bool ret
= false, roamed_back
= false;
539 uint8_t remote_flags
;
542 if (ifindex
!= BATADV_NULL_IFINDEX
)
543 in_dev
= dev_get_by_index(&init_net
, ifindex
);
545 tt_local
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
547 if (!is_multicast_ether_addr(addr
))
548 tt_global
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
551 tt_local
->last_seen
= jiffies
;
552 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_PENDING
) {
553 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
554 "Re-adding pending client %pM (vid: %d)\n",
555 addr
, BATADV_PRINT_VID(vid
));
556 /* whatever the reason why the PENDING flag was set,
557 * this is a client which was enqueued to be removed in
558 * this orig_interval. Since it popped up again, the
559 * flag can be reset like it was never enqueued
561 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_PENDING
;
565 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
566 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
567 "Roaming client %pM (vid: %d) came back to its original location\n",
568 addr
, BATADV_PRINT_VID(vid
));
569 /* the ROAM flag is set because this client roamed away
570 * and the node got a roaming_advertisement message. Now
571 * that the client popped up again at its original
572 * location such flag can be unset
574 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
580 /* Ignore the client if we cannot send it in a full table response. */
581 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
582 table_size
+= batadv_tt_len(1);
583 packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
584 if (table_size
> packet_size_max
) {
585 net_ratelimited_function(batadv_info
, soft_iface
,
586 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
587 table_size
, packet_size_max
, addr
);
591 tt_local
= kmalloc(sizeof(*tt_local
), GFP_ATOMIC
);
595 /* increase the refcounter of the related vlan */
596 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
597 if (WARN(!vlan
, "adding TT local entry %pM to non-existent VLAN %d",
598 addr
, BATADV_PRINT_VID(vid
)))
601 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
602 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
603 addr
, BATADV_PRINT_VID(vid
),
604 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
606 ether_addr_copy(tt_local
->common
.addr
, addr
);
607 /* The local entry has to be marked as NEW to avoid to send it in
608 * a full table response going out before the next ttvn increment
609 * (consistency check)
611 tt_local
->common
.flags
= BATADV_TT_CLIENT_NEW
;
612 tt_local
->common
.vid
= vid
;
613 if (batadv_is_wifi_netdev(in_dev
))
614 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
615 atomic_set(&tt_local
->common
.refcount
, 2);
616 tt_local
->last_seen
= jiffies
;
617 tt_local
->common
.added_at
= tt_local
->last_seen
;
619 /* the batman interface mac and multicast addresses should never be
622 if (batadv_compare_eth(addr
, soft_iface
->dev_addr
) ||
623 is_multicast_ether_addr(addr
))
624 tt_local
->common
.flags
|= BATADV_TT_CLIENT_NOPURGE
;
626 hash_added
= batadv_hash_add(bat_priv
->tt
.local_hash
, batadv_compare_tt
,
627 batadv_choose_tt
, &tt_local
->common
,
628 &tt_local
->common
.hash_entry
);
630 if (unlikely(hash_added
!= 0)) {
631 /* remove the reference for the hash */
632 batadv_tt_local_entry_free_ref(tt_local
);
633 batadv_softif_vlan_free_ref(vlan
);
638 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
641 /* Check whether it is a roaming, but don't do anything if the roaming
642 * process has already been handled
644 if (tt_global
&& !(tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
)) {
645 /* These node are probably going to update their tt table */
646 head
= &tt_global
->orig_list
;
648 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
649 batadv_send_roam_adv(bat_priv
, tt_global
->common
.addr
,
650 tt_global
->common
.vid
,
651 orig_entry
->orig_node
);
655 batadv_tt_global_free(bat_priv
, tt_global
,
659 /* The global entry has to be marked as ROAMING and
660 * has to be kept for consistency purpose
662 tt_global
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
663 tt_global
->roam_at
= jiffies
;
667 /* store the current remote flags before altering them. This helps
668 * understanding is flags are changing or not
670 remote_flags
= tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
;
672 if (batadv_is_wifi_netdev(in_dev
))
673 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
675 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_WIFI
;
677 /* check the mark in the skb: if it's equal to the configured
678 * isolation_mark, it means the packet is coming from an isolated
681 match_mark
= (mark
& bat_priv
->isolation_mark_mask
);
682 if (bat_priv
->isolation_mark_mask
&&
683 match_mark
== bat_priv
->isolation_mark
)
684 tt_local
->common
.flags
|= BATADV_TT_CLIENT_ISOLA
;
686 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ISOLA
;
688 /* if any "dynamic" flag has been modified, resend an ADD event for this
689 * entry so that all the nodes can get the new flags
691 if (remote_flags
^ (tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
))
692 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
699 batadv_tt_local_entry_free_ref(tt_local
);
701 batadv_tt_global_entry_free_ref(tt_global
);
706 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
707 * within a TT Response directed to another node
708 * @orig_node: originator for which the TT data has to be prepared
709 * @tt_data: uninitialised pointer to the address of the TVLV buffer
710 * @tt_change: uninitialised pointer to the address of the area where the TT
711 * changed can be stored
712 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
713 * function reserves the amount of space needed to send the entire global TT
714 * table. In case of success the value is updated with the real amount of
717 * Allocate the needed amount of memory for the entire TT TVLV and write its
718 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
719 * objects, one per active VLAN served by the originator node.
721 * Return the size of the allocated buffer or 0 in case of failure.
724 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node
*orig_node
,
725 struct batadv_tvlv_tt_data
**tt_data
,
726 struct batadv_tvlv_tt_change
**tt_change
,
729 uint16_t num_vlan
= 0, num_entries
= 0, change_offset
, tvlv_len
;
730 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
731 struct batadv_orig_node_vlan
*vlan
;
732 uint8_t *tt_change_ptr
;
735 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
737 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
740 change_offset
= sizeof(**tt_data
);
741 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
743 /* if tt_len is negative, allocate the space needed by the full table */
745 *tt_len
= batadv_tt_len(num_entries
);
748 tvlv_len
+= change_offset
;
750 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
756 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
757 (*tt_data
)->ttvn
= atomic_read(&orig_node
->last_ttvn
);
758 (*tt_data
)->num_vlan
= htons(num_vlan
);
760 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
761 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
762 tt_vlan
->vid
= htons(vlan
->vid
);
763 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
768 tt_change_ptr
= (uint8_t *)*tt_data
+ change_offset
;
769 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
777 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
779 * @bat_priv: the bat priv with all the soft interface information
780 * @tt_data: uninitialised pointer to the address of the TVLV buffer
781 * @tt_change: uninitialised pointer to the address of the area where the TT
782 * changes can be stored
783 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
784 * function reserves the amount of space needed to send the entire local TT
785 * table. In case of success the value is updated with the real amount of
788 * Allocate the needed amount of memory for the entire TT TVLV and write its
789 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
790 * objects, one per active VLAN.
792 * Return the size of the allocated buffer or 0 in case of failure.
795 batadv_tt_prepare_tvlv_local_data(struct batadv_priv
*bat_priv
,
796 struct batadv_tvlv_tt_data
**tt_data
,
797 struct batadv_tvlv_tt_change
**tt_change
,
800 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
801 struct batadv_softif_vlan
*vlan
;
802 uint16_t num_vlan
= 0, num_entries
= 0, tvlv_len
;
803 uint8_t *tt_change_ptr
;
807 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
809 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
812 change_offset
= sizeof(**tt_data
);
813 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
815 /* if tt_len is negative, allocate the space needed by the full table */
817 *tt_len
= batadv_tt_len(num_entries
);
820 tvlv_len
+= change_offset
;
822 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
828 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
829 (*tt_data
)->ttvn
= atomic_read(&bat_priv
->tt
.vn
);
830 (*tt_data
)->num_vlan
= htons(num_vlan
);
832 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
833 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
834 tt_vlan
->vid
= htons(vlan
->vid
);
835 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
840 tt_change_ptr
= (uint8_t *)*tt_data
+ change_offset
;
841 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
849 * batadv_tt_tvlv_container_update - update the translation table tvlv container
850 * after local tt changes have been committed
851 * @bat_priv: the bat priv with all the soft interface information
853 static void batadv_tt_tvlv_container_update(struct batadv_priv
*bat_priv
)
855 struct batadv_tt_change_node
*entry
, *safe
;
856 struct batadv_tvlv_tt_data
*tt_data
;
857 struct batadv_tvlv_tt_change
*tt_change
;
858 int tt_diff_len
, tt_change_len
= 0;
859 int tt_diff_entries_num
= 0, tt_diff_entries_count
= 0;
862 tt_diff_entries_num
= atomic_read(&bat_priv
->tt
.local_changes
);
863 tt_diff_len
= batadv_tt_len(tt_diff_entries_num
);
865 /* if we have too many changes for one packet don't send any
866 * and wait for the tt table request which will be fragmented
868 if (tt_diff_len
> bat_priv
->soft_iface
->mtu
)
871 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
, &tt_data
,
872 &tt_change
, &tt_diff_len
);
876 tt_data
->flags
= BATADV_TT_OGM_DIFF
;
878 if (tt_diff_len
== 0)
879 goto container_register
;
881 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
882 atomic_set(&bat_priv
->tt
.local_changes
, 0);
884 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
886 if (tt_diff_entries_count
< tt_diff_entries_num
) {
887 memcpy(tt_change
+ tt_diff_entries_count
,
889 sizeof(struct batadv_tvlv_tt_change
));
890 tt_diff_entries_count
++;
892 list_del(&entry
->list
);
895 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
897 /* Keep the buffer for possible tt_request */
898 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
899 kfree(bat_priv
->tt
.last_changeset
);
900 bat_priv
->tt
.last_changeset_len
= 0;
901 bat_priv
->tt
.last_changeset
= NULL
;
902 tt_change_len
= batadv_tt_len(tt_diff_entries_count
);
903 /* check whether this new OGM has no changes due to size problems */
904 if (tt_diff_entries_count
> 0) {
905 /* if kmalloc() fails we will reply with the full table
906 * instead of providing the diff
908 bat_priv
->tt
.last_changeset
= kzalloc(tt_diff_len
, GFP_ATOMIC
);
909 if (bat_priv
->tt
.last_changeset
) {
910 memcpy(bat_priv
->tt
.last_changeset
,
911 tt_change
, tt_change_len
);
912 bat_priv
->tt
.last_changeset_len
= tt_diff_len
;
915 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
918 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_TT
, 1, tt_data
,
923 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
925 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
926 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
927 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
928 struct batadv_tt_common_entry
*tt_common_entry
;
929 struct batadv_tt_local_entry
*tt_local
;
930 struct batadv_hard_iface
*primary_if
;
931 struct batadv_softif_vlan
*vlan
;
932 struct hlist_head
*head
;
937 unsigned long last_seen_jiffies
;
939 uint16_t np_flag
= BATADV_TT_CLIENT_NOPURGE
;
941 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
946 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
947 net_dev
->name
, (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
948 seq_printf(seq
, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
949 "Flags", "Last seen", "CRC");
951 for (i
= 0; i
< hash
->size
; i
++) {
952 head
= &hash
->table
[i
];
955 hlist_for_each_entry_rcu(tt_common_entry
,
957 tt_local
= container_of(tt_common_entry
,
958 struct batadv_tt_local_entry
,
960 vid
= tt_common_entry
->vid
;
961 last_seen_jiffies
= jiffies
- tt_local
->last_seen
;
962 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
963 last_seen_secs
= last_seen_msecs
/ 1000;
964 last_seen_msecs
= last_seen_msecs
% 1000;
966 no_purge
= tt_common_entry
->flags
& np_flag
;
968 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
970 seq_printf(seq
, "Cannot retrieve VLAN %d\n",
971 BATADV_PRINT_VID(vid
));
976 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
977 tt_common_entry
->addr
,
978 BATADV_PRINT_VID(tt_common_entry
->vid
),
979 ((tt_common_entry
->flags
&
980 BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
981 no_purge
? 'P' : '.',
982 ((tt_common_entry
->flags
&
983 BATADV_TT_CLIENT_NEW
) ? 'N' : '.'),
984 ((tt_common_entry
->flags
&
985 BATADV_TT_CLIENT_PENDING
) ? 'X' : '.'),
986 ((tt_common_entry
->flags
&
987 BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
988 ((tt_common_entry
->flags
&
989 BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
990 no_purge
? 0 : last_seen_secs
,
991 no_purge
? 0 : last_seen_msecs
,
994 batadv_softif_vlan_free_ref(vlan
);
1000 batadv_hardif_free_ref(primary_if
);
1005 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
1006 struct batadv_tt_local_entry
*tt_local_entry
,
1007 uint16_t flags
, const char *message
)
1009 batadv_tt_local_event(bat_priv
, tt_local_entry
, flags
);
1011 /* The local client has to be marked as "pending to be removed" but has
1012 * to be kept in the table in order to send it in a full table
1013 * response issued before the net ttvn increment (consistency check)
1015 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
1017 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1018 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1019 tt_local_entry
->common
.addr
,
1020 BATADV_PRINT_VID(tt_local_entry
->common
.vid
), message
);
1024 * batadv_tt_local_remove - logically remove an entry from the local table
1025 * @bat_priv: the bat priv with all the soft interface information
1026 * @addr: the MAC address of the client to remove
1027 * @vid: VLAN identifier
1028 * @message: message to append to the log on deletion
1029 * @roaming: true if the deletion is due to a roaming event
1031 * Returns the flags assigned to the local entry before being deleted
1033 uint16_t batadv_tt_local_remove(struct batadv_priv
*bat_priv
,
1034 const uint8_t *addr
, unsigned short vid
,
1035 const char *message
, bool roaming
)
1037 struct batadv_tt_local_entry
*tt_local_entry
;
1038 uint16_t flags
, curr_flags
= BATADV_NO_FLAGS
;
1039 struct batadv_softif_vlan
*vlan
;
1041 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
1042 if (!tt_local_entry
)
1045 curr_flags
= tt_local_entry
->common
.flags
;
1047 flags
= BATADV_TT_CLIENT_DEL
;
1048 /* if this global entry addition is due to a roaming, the node has to
1049 * mark the local entry as "roamed" in order to correctly reroute
1053 flags
|= BATADV_TT_CLIENT_ROAM
;
1054 /* mark the local client as ROAMed */
1055 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1058 if (!(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
)) {
1059 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
,
1063 /* if this client has been added right now, it is possible to
1064 * immediately purge it
1066 batadv_tt_local_event(bat_priv
, tt_local_entry
, BATADV_TT_CLIENT_DEL
);
1067 hlist_del_rcu(&tt_local_entry
->common
.hash_entry
);
1068 batadv_tt_local_entry_free_ref(tt_local_entry
);
1070 /* decrease the reference held for this vlan */
1071 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
1075 batadv_softif_vlan_free_ref(vlan
);
1076 batadv_softif_vlan_free_ref(vlan
);
1080 batadv_tt_local_entry_free_ref(tt_local_entry
);
1086 * batadv_tt_local_purge_list - purge inactive tt local entries
1087 * @bat_priv: the bat priv with all the soft interface information
1088 * @head: pointer to the list containing the local tt entries
1089 * @timeout: parameter deciding whether a given tt local entry is considered
1092 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
1093 struct hlist_head
*head
,
1096 struct batadv_tt_local_entry
*tt_local_entry
;
1097 struct batadv_tt_common_entry
*tt_common_entry
;
1098 struct hlist_node
*node_tmp
;
1100 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
, head
,
1102 tt_local_entry
= container_of(tt_common_entry
,
1103 struct batadv_tt_local_entry
,
1105 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
1108 /* entry already marked for deletion */
1109 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1112 if (!batadv_has_timed_out(tt_local_entry
->last_seen
, timeout
))
1115 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
1116 BATADV_TT_CLIENT_DEL
, "timed out");
1121 * batadv_tt_local_purge - purge inactive tt local entries
1122 * @bat_priv: the bat priv with all the soft interface information
1123 * @timeout: parameter deciding whether a given tt local entry is considered
1126 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
,
1129 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1130 struct hlist_head
*head
;
1131 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1134 for (i
= 0; i
< hash
->size
; i
++) {
1135 head
= &hash
->table
[i
];
1136 list_lock
= &hash
->list_locks
[i
];
1138 spin_lock_bh(list_lock
);
1139 batadv_tt_local_purge_list(bat_priv
, head
, timeout
);
1140 spin_unlock_bh(list_lock
);
1144 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
1146 struct batadv_hashtable
*hash
;
1147 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1148 struct batadv_tt_common_entry
*tt_common_entry
;
1149 struct batadv_tt_local_entry
*tt_local
;
1150 struct batadv_softif_vlan
*vlan
;
1151 struct hlist_node
*node_tmp
;
1152 struct hlist_head
*head
;
1155 if (!bat_priv
->tt
.local_hash
)
1158 hash
= bat_priv
->tt
.local_hash
;
1160 for (i
= 0; i
< hash
->size
; i
++) {
1161 head
= &hash
->table
[i
];
1162 list_lock
= &hash
->list_locks
[i
];
1164 spin_lock_bh(list_lock
);
1165 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1167 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1168 tt_local
= container_of(tt_common_entry
,
1169 struct batadv_tt_local_entry
,
1172 /* decrease the reference held for this vlan */
1173 vlan
= batadv_softif_vlan_get(bat_priv
,
1174 tt_common_entry
->vid
);
1176 batadv_softif_vlan_free_ref(vlan
);
1177 batadv_softif_vlan_free_ref(vlan
);
1180 batadv_tt_local_entry_free_ref(tt_local
);
1182 spin_unlock_bh(list_lock
);
1185 batadv_hash_destroy(hash
);
1187 bat_priv
->tt
.local_hash
= NULL
;
1190 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
1192 if (bat_priv
->tt
.global_hash
)
1195 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
1197 if (!bat_priv
->tt
.global_hash
)
1200 batadv_hash_set_lock_class(bat_priv
->tt
.global_hash
,
1201 &batadv_tt_global_hash_lock_class_key
);
1206 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
1208 struct batadv_tt_change_node
*entry
, *safe
;
1210 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
1212 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
1214 list_del(&entry
->list
);
1218 atomic_set(&bat_priv
->tt
.local_changes
, 0);
1219 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
1222 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1223 * batadv_tt_global_entry list
1225 * returns it with an increased refcounter, NULL if not found
1227 static struct batadv_tt_orig_list_entry
*
1228 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
1229 const struct batadv_orig_node
*orig_node
)
1231 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
1232 const struct hlist_head
*head
;
1235 head
= &entry
->orig_list
;
1236 hlist_for_each_entry_rcu(tmp_orig_entry
, head
, list
) {
1237 if (tmp_orig_entry
->orig_node
!= orig_node
)
1239 if (!atomic_inc_not_zero(&tmp_orig_entry
->refcount
))
1242 orig_entry
= tmp_orig_entry
;
1250 /* find out if an orig_node is already in the list of a tt_global_entry.
1251 * returns true if found, false otherwise
1254 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
1255 const struct batadv_orig_node
*orig_node
)
1257 struct batadv_tt_orig_list_entry
*orig_entry
;
1260 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
1263 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1270 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
1271 struct batadv_orig_node
*orig_node
, int ttvn
)
1273 struct batadv_tt_orig_list_entry
*orig_entry
;
1275 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
1277 /* refresh the ttvn: the current value could be a bogus one that
1278 * was added during a "temporary client detection"
1280 orig_entry
->ttvn
= ttvn
;
1284 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
1288 INIT_HLIST_NODE(&orig_entry
->list
);
1289 atomic_inc(&orig_node
->refcount
);
1290 batadv_tt_global_size_inc(orig_node
, tt_global
->common
.vid
);
1291 orig_entry
->orig_node
= orig_node
;
1292 orig_entry
->ttvn
= ttvn
;
1293 atomic_set(&orig_entry
->refcount
, 2);
1295 spin_lock_bh(&tt_global
->list_lock
);
1296 hlist_add_head_rcu(&orig_entry
->list
,
1297 &tt_global
->orig_list
);
1298 spin_unlock_bh(&tt_global
->list_lock
);
1299 atomic_inc(&tt_global
->orig_list_count
);
1303 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1307 * batadv_tt_global_add - add a new TT global entry or update an existing one
1308 * @bat_priv: the bat priv with all the soft interface information
1309 * @orig_node: the originator announcing the client
1310 * @tt_addr: the mac address of the non-mesh client
1311 * @vid: VLAN identifier
1312 * @flags: TT flags that have to be set for this non-mesh client
1313 * @ttvn: the tt version number ever announcing this non-mesh client
1315 * Add a new TT global entry for the given originator. If the entry already
1316 * exists add a new reference to the given originator (a global entry can have
1317 * references to multiple originators) and adjust the flags attribute to reflect
1318 * the function argument.
1319 * If a TT local entry exists for this non-mesh client remove it.
1321 * The caller must hold orig_node refcount.
1323 * Return true if the new entry has been added, false otherwise
1325 static bool batadv_tt_global_add(struct batadv_priv
*bat_priv
,
1326 struct batadv_orig_node
*orig_node
,
1327 const unsigned char *tt_addr
,
1328 unsigned short vid
, uint16_t flags
,
1331 struct batadv_tt_global_entry
*tt_global_entry
;
1332 struct batadv_tt_local_entry
*tt_local_entry
;
1335 struct batadv_tt_common_entry
*common
;
1336 uint16_t local_flags
;
1338 /* ignore global entries from backbone nodes */
1339 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
, vid
))
1342 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
, vid
);
1343 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, tt_addr
, vid
);
1345 /* if the node already has a local client for this entry, it has to wait
1346 * for a roaming advertisement instead of manually messing up the global
1349 if ((flags
& BATADV_TT_CLIENT_TEMP
) && tt_local_entry
&&
1350 !(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
))
1353 if (!tt_global_entry
) {
1354 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
1355 if (!tt_global_entry
)
1358 common
= &tt_global_entry
->common
;
1359 ether_addr_copy(common
->addr
, tt_addr
);
1362 common
->flags
= flags
;
1363 tt_global_entry
->roam_at
= 0;
1364 /* node must store current time in case of roaming. This is
1365 * needed to purge this entry out on timeout (if nobody claims
1368 if (flags
& BATADV_TT_CLIENT_ROAM
)
1369 tt_global_entry
->roam_at
= jiffies
;
1370 atomic_set(&common
->refcount
, 2);
1371 common
->added_at
= jiffies
;
1373 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
1374 atomic_set(&tt_global_entry
->orig_list_count
, 0);
1375 spin_lock_init(&tt_global_entry
->list_lock
);
1377 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
1379 batadv_choose_tt
, common
,
1380 &common
->hash_entry
);
1382 if (unlikely(hash_added
!= 0)) {
1383 /* remove the reference for the hash */
1384 batadv_tt_global_entry_free_ref(tt_global_entry
);
1388 common
= &tt_global_entry
->common
;
1389 /* If there is already a global entry, we can use this one for
1391 * But if we are trying to add a temporary client then here are
1392 * two options at this point:
1393 * 1) the global client is not a temporary client: the global
1394 * client has to be left as it is, temporary information
1395 * should never override any already known client state
1396 * 2) the global client is a temporary client: purge the
1397 * originator list and add the new one orig_entry
1399 if (flags
& BATADV_TT_CLIENT_TEMP
) {
1400 if (!(common
->flags
& BATADV_TT_CLIENT_TEMP
))
1402 if (batadv_tt_global_entry_has_orig(tt_global_entry
,
1405 batadv_tt_global_del_orig_list(tt_global_entry
);
1406 goto add_orig_entry
;
1409 /* if the client was temporary added before receiving the first
1410 * OGM announcing it, we have to clear the TEMP flag
1412 common
->flags
&= ~BATADV_TT_CLIENT_TEMP
;
1414 /* the change can carry possible "attribute" flags like the
1415 * TT_CLIENT_WIFI, therefore they have to be copied in the
1418 tt_global_entry
->common
.flags
|= flags
;
1420 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1421 * one originator left in the list and we previously received a
1422 * delete + roaming change for this originator.
1424 * We should first delete the old originator before adding the
1427 if (common
->flags
& BATADV_TT_CLIENT_ROAM
) {
1428 batadv_tt_global_del_orig_list(tt_global_entry
);
1429 common
->flags
&= ~BATADV_TT_CLIENT_ROAM
;
1430 tt_global_entry
->roam_at
= 0;
1434 /* add the new orig_entry (if needed) or update it */
1435 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
1437 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1438 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1439 common
->addr
, BATADV_PRINT_VID(common
->vid
),
1444 /* Do not remove multicast addresses from the local hash on
1447 if (is_multicast_ether_addr(tt_addr
))
1450 /* remove address from local hash if present */
1451 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
, vid
,
1452 "global tt received",
1453 flags
& BATADV_TT_CLIENT_ROAM
);
1454 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
1456 if (!(flags
& BATADV_TT_CLIENT_ROAM
))
1457 /* this is a normal global add. Therefore the client is not in a
1458 * roaming state anymore.
1460 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
1463 if (tt_global_entry
)
1464 batadv_tt_global_entry_free_ref(tt_global_entry
);
1466 batadv_tt_local_entry_free_ref(tt_local_entry
);
1471 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1472 * @bat_priv: the bat priv with all the soft interface information
1473 * @tt_global_entry: global translation table entry to be analyzed
1475 * This functon assumes the caller holds rcu_read_lock().
1476 * Returns best originator list entry or NULL on errors.
1478 static struct batadv_tt_orig_list_entry
*
1479 batadv_transtable_best_orig(struct batadv_priv
*bat_priv
,
1480 struct batadv_tt_global_entry
*tt_global_entry
)
1482 struct batadv_neigh_node
*router
, *best_router
= NULL
;
1483 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1484 struct hlist_head
*head
;
1485 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
= NULL
;
1487 head
= &tt_global_entry
->orig_list
;
1488 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1489 router
= batadv_orig_router_get(orig_entry
->orig_node
,
1495 bao
->bat_neigh_cmp(router
, BATADV_IF_DEFAULT
,
1496 best_router
, BATADV_IF_DEFAULT
) <= 0) {
1497 batadv_neigh_node_free_ref(router
);
1501 /* release the refcount for the "old" best */
1503 batadv_neigh_node_free_ref(best_router
);
1505 best_entry
= orig_entry
;
1506 best_router
= router
;
1510 batadv_neigh_node_free_ref(best_router
);
1516 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1517 * for this global entry
1518 * @bat_priv: the bat priv with all the soft interface information
1519 * @tt_global_entry: global translation table entry to be printed
1520 * @seq: debugfs table seq_file struct
1522 * This functon assumes the caller holds rcu_read_lock().
1525 batadv_tt_global_print_entry(struct batadv_priv
*bat_priv
,
1526 struct batadv_tt_global_entry
*tt_global_entry
,
1527 struct seq_file
*seq
)
1529 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
;
1530 struct batadv_tt_common_entry
*tt_common_entry
;
1531 struct batadv_orig_node_vlan
*vlan
;
1532 struct hlist_head
*head
;
1536 tt_common_entry
= &tt_global_entry
->common
;
1537 flags
= tt_common_entry
->flags
;
1539 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
1541 vlan
= batadv_orig_node_vlan_get(best_entry
->orig_node
,
1542 tt_common_entry
->vid
);
1545 " * Cannot retrieve VLAN %d for originator %pM\n",
1546 BATADV_PRINT_VID(tt_common_entry
->vid
),
1547 best_entry
->orig_node
->orig
);
1551 last_ttvn
= atomic_read(&best_entry
->orig_node
->last_ttvn
);
1553 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1554 '*', tt_global_entry
->common
.addr
,
1555 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1556 best_entry
->ttvn
, best_entry
->orig_node
->orig
,
1557 last_ttvn
, vlan
->tt
.crc
,
1558 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1559 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1560 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1561 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1563 batadv_orig_node_vlan_free_ref(vlan
);
1567 head
= &tt_global_entry
->orig_list
;
1569 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1570 if (best_entry
== orig_entry
)
1573 vlan
= batadv_orig_node_vlan_get(orig_entry
->orig_node
,
1574 tt_common_entry
->vid
);
1577 " + Cannot retrieve VLAN %d for originator %pM\n",
1578 BATADV_PRINT_VID(tt_common_entry
->vid
),
1579 orig_entry
->orig_node
->orig
);
1583 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
1585 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1586 '+', tt_global_entry
->common
.addr
,
1587 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1588 orig_entry
->ttvn
, orig_entry
->orig_node
->orig
,
1589 last_ttvn
, vlan
->tt
.crc
,
1590 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1591 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1592 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1593 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1595 batadv_orig_node_vlan_free_ref(vlan
);
1599 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
1601 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1602 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1603 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1604 struct batadv_tt_common_entry
*tt_common_entry
;
1605 struct batadv_tt_global_entry
*tt_global
;
1606 struct batadv_hard_iface
*primary_if
;
1607 struct hlist_head
*head
;
1610 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1615 "Globally announced TT entries received via the mesh %s\n",
1617 seq_printf(seq
, " %-13s %s %s %-15s %s (%-10s) %s\n",
1618 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1621 for (i
= 0; i
< hash
->size
; i
++) {
1622 head
= &hash
->table
[i
];
1625 hlist_for_each_entry_rcu(tt_common_entry
,
1627 tt_global
= container_of(tt_common_entry
,
1628 struct batadv_tt_global_entry
,
1630 batadv_tt_global_print_entry(bat_priv
, tt_global
, seq
);
1636 batadv_hardif_free_ref(primary_if
);
1641 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1642 * @tt_global_entry: the global entry to remove the orig_entry from
1643 * @orig_entry: the orig entry to remove and free
1645 * Remove an orig_entry from its list in the given tt_global_entry and
1646 * free this orig_entry afterwards.
1649 batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry
*tt_global_entry
,
1650 struct batadv_tt_orig_list_entry
*orig_entry
)
1652 batadv_tt_global_size_dec(orig_entry
->orig_node
,
1653 tt_global_entry
->common
.vid
);
1654 atomic_dec(&tt_global_entry
->orig_list_count
);
1655 hlist_del_rcu(&orig_entry
->list
);
1656 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1659 /* deletes the orig list of a tt_global_entry */
1661 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
1663 struct hlist_head
*head
;
1664 struct hlist_node
*safe
;
1665 struct batadv_tt_orig_list_entry
*orig_entry
;
1667 spin_lock_bh(&tt_global_entry
->list_lock
);
1668 head
= &tt_global_entry
->orig_list
;
1669 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
)
1670 batadv_tt_global_del_orig_entry(tt_global_entry
, orig_entry
);
1671 spin_unlock_bh(&tt_global_entry
->list_lock
);
1675 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1676 * @bat_priv: the bat priv with all the soft interface information
1677 * @tt_global_entry: the global entry to remove the orig_node from
1678 * @orig_node: the originator announcing the client
1679 * @message: message to append to the log on deletion
1681 * Remove the given orig_node and its according orig_entry from the given
1685 batadv_tt_global_del_orig_node(struct batadv_priv
*bat_priv
,
1686 struct batadv_tt_global_entry
*tt_global_entry
,
1687 struct batadv_orig_node
*orig_node
,
1688 const char *message
)
1690 struct hlist_head
*head
;
1691 struct hlist_node
*safe
;
1692 struct batadv_tt_orig_list_entry
*orig_entry
;
1695 spin_lock_bh(&tt_global_entry
->list_lock
);
1696 head
= &tt_global_entry
->orig_list
;
1697 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
) {
1698 if (orig_entry
->orig_node
== orig_node
) {
1699 vid
= tt_global_entry
->common
.vid
;
1700 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1701 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1703 tt_global_entry
->common
.addr
,
1704 BATADV_PRINT_VID(vid
), message
);
1705 batadv_tt_global_del_orig_entry(tt_global_entry
,
1709 spin_unlock_bh(&tt_global_entry
->list_lock
);
1712 /* If the client is to be deleted, we check if it is the last origantor entry
1713 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1714 * timer, otherwise we simply remove the originator scheduled for deletion.
1717 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
1718 struct batadv_tt_global_entry
*tt_global_entry
,
1719 struct batadv_orig_node
*orig_node
,
1720 const char *message
)
1722 bool last_entry
= true;
1723 struct hlist_head
*head
;
1724 struct batadv_tt_orig_list_entry
*orig_entry
;
1726 /* no local entry exists, case 1:
1727 * Check if this is the last one or if other entries exist.
1731 head
= &tt_global_entry
->orig_list
;
1732 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1733 if (orig_entry
->orig_node
!= orig_node
) {
1741 /* its the last one, mark for roaming. */
1742 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1743 tt_global_entry
->roam_at
= jiffies
;
1745 /* there is another entry, we can simply delete this
1746 * one and can still use the other one.
1748 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1749 orig_node
, message
);
1753 * batadv_tt_global_del - remove a client from the global table
1754 * @bat_priv: the bat priv with all the soft interface information
1755 * @orig_node: an originator serving this client
1756 * @addr: the mac address of the client
1757 * @vid: VLAN identifier
1758 * @message: a message explaining the reason for deleting the client to print
1759 * for debugging purpose
1760 * @roaming: true if the deletion has been triggered by a roaming event
1762 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
1763 struct batadv_orig_node
*orig_node
,
1764 const unsigned char *addr
, unsigned short vid
,
1765 const char *message
, bool roaming
)
1767 struct batadv_tt_global_entry
*tt_global_entry
;
1768 struct batadv_tt_local_entry
*local_entry
= NULL
;
1770 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
1771 if (!tt_global_entry
)
1775 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1776 orig_node
, message
);
1778 if (hlist_empty(&tt_global_entry
->orig_list
))
1779 batadv_tt_global_free(bat_priv
, tt_global_entry
,
1785 /* if we are deleting a global entry due to a roam
1786 * event, there are two possibilities:
1787 * 1) the client roamed from node A to node B => if there
1788 * is only one originator left for this client, we mark
1789 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1790 * wait for node B to claim it. In case of timeout
1791 * the entry is purged.
1793 * If there are other originators left, we directly delete
1795 * 2) the client roamed to us => we can directly delete
1796 * the global entry, since it is useless now.
1798 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1799 tt_global_entry
->common
.addr
,
1802 /* local entry exists, case 2: client roamed to us. */
1803 batadv_tt_global_del_orig_list(tt_global_entry
);
1804 batadv_tt_global_free(bat_priv
, tt_global_entry
, message
);
1806 /* no local entry exists, case 1: check for roaming */
1807 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1808 orig_node
, message
);
1811 if (tt_global_entry
)
1812 batadv_tt_global_entry_free_ref(tt_global_entry
);
1814 batadv_tt_local_entry_free_ref(local_entry
);
1818 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1819 * given originator matching the provided vid
1820 * @bat_priv: the bat priv with all the soft interface information
1821 * @orig_node: the originator owning the entries to remove
1822 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1824 * @message: debug message to print as "reason"
1826 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1827 struct batadv_orig_node
*orig_node
,
1829 const char *message
)
1831 struct batadv_tt_global_entry
*tt_global
;
1832 struct batadv_tt_common_entry
*tt_common_entry
;
1834 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1835 struct hlist_node
*safe
;
1836 struct hlist_head
*head
;
1837 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1843 for (i
= 0; i
< hash
->size
; i
++) {
1844 head
= &hash
->table
[i
];
1845 list_lock
= &hash
->list_locks
[i
];
1847 spin_lock_bh(list_lock
);
1848 hlist_for_each_entry_safe(tt_common_entry
, safe
,
1850 /* remove only matching entries */
1851 if (match_vid
>= 0 && tt_common_entry
->vid
!= match_vid
)
1854 tt_global
= container_of(tt_common_entry
,
1855 struct batadv_tt_global_entry
,
1858 batadv_tt_global_del_orig_node(bat_priv
, tt_global
,
1859 orig_node
, message
);
1861 if (hlist_empty(&tt_global
->orig_list
)) {
1862 vid
= tt_global
->common
.vid
;
1863 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1864 "Deleting global tt entry %pM (vid: %d): %s\n",
1865 tt_global
->common
.addr
,
1866 BATADV_PRINT_VID(vid
), message
);
1867 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1868 batadv_tt_global_entry_free_ref(tt_global
);
1871 spin_unlock_bh(list_lock
);
1873 orig_node
->capa_initialized
&= ~BATADV_ORIG_CAPA_HAS_TT
;
1876 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1880 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1881 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1883 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1884 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1886 *msg
= "Roaming timeout\n";
1889 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1890 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1892 *msg
= "Temporary client timeout\n";
1898 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1900 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1901 struct hlist_head
*head
;
1902 struct hlist_node
*node_tmp
;
1903 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1906 struct batadv_tt_common_entry
*tt_common
;
1907 struct batadv_tt_global_entry
*tt_global
;
1909 for (i
= 0; i
< hash
->size
; i
++) {
1910 head
= &hash
->table
[i
];
1911 list_lock
= &hash
->list_locks
[i
];
1913 spin_lock_bh(list_lock
);
1914 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
1916 tt_global
= container_of(tt_common
,
1917 struct batadv_tt_global_entry
,
1920 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1923 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1924 "Deleting global tt entry %pM (vid: %d): %s\n",
1925 tt_global
->common
.addr
,
1926 BATADV_PRINT_VID(tt_global
->common
.vid
),
1929 hlist_del_rcu(&tt_common
->hash_entry
);
1931 batadv_tt_global_entry_free_ref(tt_global
);
1933 spin_unlock_bh(list_lock
);
1937 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
1939 struct batadv_hashtable
*hash
;
1940 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1941 struct batadv_tt_common_entry
*tt_common_entry
;
1942 struct batadv_tt_global_entry
*tt_global
;
1943 struct hlist_node
*node_tmp
;
1944 struct hlist_head
*head
;
1947 if (!bat_priv
->tt
.global_hash
)
1950 hash
= bat_priv
->tt
.global_hash
;
1952 for (i
= 0; i
< hash
->size
; i
++) {
1953 head
= &hash
->table
[i
];
1954 list_lock
= &hash
->list_locks
[i
];
1956 spin_lock_bh(list_lock
);
1957 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1959 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1960 tt_global
= container_of(tt_common_entry
,
1961 struct batadv_tt_global_entry
,
1963 batadv_tt_global_entry_free_ref(tt_global
);
1965 spin_unlock_bh(list_lock
);
1968 batadv_hash_destroy(hash
);
1970 bat_priv
->tt
.global_hash
= NULL
;
1974 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
1975 struct batadv_tt_global_entry
*tt_global_entry
)
1979 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
1980 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
1983 /* check if the two clients are marked as isolated */
1984 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
&&
1985 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
)
1992 * batadv_transtable_search - get the mesh destination for a given client
1993 * @bat_priv: the bat priv with all the soft interface information
1994 * @src: mac address of the source client
1995 * @addr: mac address of the destination client
1996 * @vid: VLAN identifier
1998 * Returns a pointer to the originator that was selected as destination in the
1999 * mesh for contacting the client 'addr', NULL otherwise.
2000 * In case of multiple originators serving the same client, the function returns
2001 * the best one (best in terms of metric towards the destination node).
2003 * If the two clients are AP isolated the function returns NULL.
2005 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
2007 const uint8_t *addr
,
2010 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2011 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2012 struct batadv_orig_node
*orig_node
= NULL
;
2013 struct batadv_tt_orig_list_entry
*best_entry
;
2015 if (src
&& batadv_vlan_ap_isola_get(bat_priv
, vid
)) {
2016 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
, vid
);
2017 if (!tt_local_entry
||
2018 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
))
2022 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
2023 if (!tt_global_entry
)
2026 /* check whether the clients should not communicate due to AP
2029 if (tt_local_entry
&&
2030 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2034 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
2035 /* found anything? */
2037 orig_node
= best_entry
->orig_node
;
2038 if (orig_node
&& !atomic_inc_not_zero(&orig_node
->refcount
))
2043 if (tt_global_entry
)
2044 batadv_tt_global_entry_free_ref(tt_global_entry
);
2046 batadv_tt_local_entry_free_ref(tt_local_entry
);
2052 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2053 * to the given orig_node
2054 * @bat_priv: the bat priv with all the soft interface information
2055 * @orig_node: originator for which the CRC should be computed
2056 * @vid: VLAN identifier for which the CRC32 has to be computed
2058 * This function computes the checksum for the global table corresponding to a
2059 * specific originator. In particular, the checksum is computed as follows: For
2060 * each client connected to the originator the CRC32C of the MAC address and the
2061 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2064 * The idea behind is that CRC32C should be used as much as possible in order to
2065 * produce a unique hash of the table, but since the order which is used to feed
2066 * the CRC32C function affects the result and since every node in the network
2067 * probably sorts the clients differently, the hash function cannot be directly
2068 * computed over the entire table. Hence the CRC32C is used only on
2069 * the single client entry, while all the results are then xor'ed together
2070 * because the XOR operation can combine them all while trying to reduce the
2071 * noise as much as possible.
2073 * Returns the checksum of the global table of a given originator.
2075 static uint32_t batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
2076 struct batadv_orig_node
*orig_node
,
2079 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2080 struct batadv_tt_common_entry
*tt_common
;
2081 struct batadv_tt_global_entry
*tt_global
;
2082 struct hlist_head
*head
;
2083 uint32_t i
, crc_tmp
, crc
= 0;
2087 for (i
= 0; i
< hash
->size
; i
++) {
2088 head
= &hash
->table
[i
];
2091 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2092 tt_global
= container_of(tt_common
,
2093 struct batadv_tt_global_entry
,
2095 /* compute the CRC only for entries belonging to the
2096 * VLAN identified by the vid passed as parameter
2098 if (tt_common
->vid
!= vid
)
2101 /* Roaming clients are in the global table for
2102 * consistency only. They don't have to be
2103 * taken into account while computing the
2106 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
2108 /* Temporary clients have not been announced yet, so
2109 * they have to be skipped while computing the global
2112 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
2115 /* find out if this global entry is announced by this
2118 if (!batadv_tt_global_entry_has_orig(tt_global
,
2122 /* use network order to read the VID: this ensures that
2123 * every node reads the bytes in the same order.
2125 tmp_vid
= htons(tt_common
->vid
);
2126 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2128 /* compute the CRC on flags that have to be kept in sync
2131 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2132 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2134 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2143 * batadv_tt_local_crc - calculates the checksum of the local table
2144 * @bat_priv: the bat priv with all the soft interface information
2145 * @vid: VLAN identifier for which the CRC32 has to be computed
2147 * For details about the computation, please refer to the documentation for
2148 * batadv_tt_global_crc().
2150 * Returns the checksum of the local table
2152 static uint32_t batadv_tt_local_crc(struct batadv_priv
*bat_priv
,
2155 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2156 struct batadv_tt_common_entry
*tt_common
;
2157 struct hlist_head
*head
;
2158 uint32_t i
, crc_tmp
, crc
= 0;
2162 for (i
= 0; i
< hash
->size
; i
++) {
2163 head
= &hash
->table
[i
];
2166 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2167 /* compute the CRC only for entries belonging to the
2168 * VLAN identified by vid
2170 if (tt_common
->vid
!= vid
)
2173 /* not yet committed clients have not to be taken into
2174 * account while computing the CRC
2176 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
2179 /* use network order to read the VID: this ensures that
2180 * every node reads the bytes in the same order.
2182 tmp_vid
= htons(tt_common
->vid
);
2183 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2185 /* compute the CRC on flags that have to be kept in sync
2188 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2189 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2191 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2199 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
2201 struct batadv_tt_req_node
*node
, *safe
;
2203 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2205 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2206 list_del(&node
->list
);
2210 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2213 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
2214 struct batadv_orig_node
*orig_node
,
2215 const void *tt_buff
,
2216 uint16_t tt_buff_len
)
2218 /* Replace the old buffer only if I received something in the
2219 * last OGM (the OGM could carry no changes)
2221 spin_lock_bh(&orig_node
->tt_buff_lock
);
2222 if (tt_buff_len
> 0) {
2223 kfree(orig_node
->tt_buff
);
2224 orig_node
->tt_buff_len
= 0;
2225 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
2226 if (orig_node
->tt_buff
) {
2227 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
2228 orig_node
->tt_buff_len
= tt_buff_len
;
2231 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2234 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
2236 struct batadv_tt_req_node
*node
, *safe
;
2238 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2239 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2240 if (batadv_has_timed_out(node
->issued_at
,
2241 BATADV_TT_REQUEST_TIMEOUT
)) {
2242 list_del(&node
->list
);
2246 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2249 /* returns the pointer to the new tt_req_node struct if no request
2250 * has already been issued for this orig_node, NULL otherwise
2252 static struct batadv_tt_req_node
*
2253 batadv_new_tt_req_node(struct batadv_priv
*bat_priv
,
2254 struct batadv_orig_node
*orig_node
)
2256 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
2258 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2259 list_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
2260 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
2261 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
2262 BATADV_TT_REQUEST_TIMEOUT
))
2266 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
2270 ether_addr_copy(tt_req_node
->addr
, orig_node
->orig
);
2271 tt_req_node
->issued_at
= jiffies
;
2273 list_add(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
2275 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2280 * batadv_tt_local_valid - verify that given tt entry is a valid one
2281 * @entry_ptr: to be checked local tt entry
2282 * @data_ptr: not used but definition required to satisfy the callback prototype
2284 * Returns 1 if the entry is a valid, 0 otherwise.
2286 static int batadv_tt_local_valid(const void *entry_ptr
, const void *data_ptr
)
2288 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2290 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
2295 static int batadv_tt_global_valid(const void *entry_ptr
,
2296 const void *data_ptr
)
2298 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2299 const struct batadv_tt_global_entry
*tt_global_entry
;
2300 const struct batadv_orig_node
*orig_node
= data_ptr
;
2302 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
2303 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
2306 tt_global_entry
= container_of(tt_common_entry
,
2307 struct batadv_tt_global_entry
,
2310 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
2314 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2316 * @bat_priv: the bat priv with all the soft interface information
2317 * @hash: hash table containing the tt entries
2318 * @tt_len: expected tvlv tt data buffer length in number of bytes
2319 * @tvlv_buff: pointer to the buffer to fill with the TT data
2320 * @valid_cb: function to filter tt change entries
2321 * @cb_data: data passed to the filter function as argument
2323 static void batadv_tt_tvlv_generate(struct batadv_priv
*bat_priv
,
2324 struct batadv_hashtable
*hash
,
2325 void *tvlv_buff
, uint16_t tt_len
,
2326 int (*valid_cb
)(const void *, const void *),
2329 struct batadv_tt_common_entry
*tt_common_entry
;
2330 struct batadv_tvlv_tt_change
*tt_change
;
2331 struct hlist_head
*head
;
2332 uint16_t tt_tot
, tt_num_entries
= 0;
2335 tt_tot
= batadv_tt_entries(tt_len
);
2336 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_buff
;
2339 for (i
= 0; i
< hash
->size
; i
++) {
2340 head
= &hash
->table
[i
];
2342 hlist_for_each_entry_rcu(tt_common_entry
,
2344 if (tt_tot
== tt_num_entries
)
2347 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
2350 ether_addr_copy(tt_change
->addr
, tt_common_entry
->addr
);
2351 tt_change
->flags
= tt_common_entry
->flags
;
2352 tt_change
->vid
= htons(tt_common_entry
->vid
);
2353 memset(tt_change
->reserved
, 0,
2354 sizeof(tt_change
->reserved
));
2364 * batadv_tt_global_check_crc - check if all the CRCs are correct
2365 * @orig_node: originator for which the CRCs have to be checked
2366 * @tt_vlan: pointer to the first tvlv VLAN entry
2367 * @num_vlan: number of tvlv VLAN entries
2368 * @create: if true, create VLAN objects if not found
2370 * Return true if all the received CRCs match the locally stored ones, false
2373 static bool batadv_tt_global_check_crc(struct batadv_orig_node
*orig_node
,
2374 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2377 struct batadv_tvlv_tt_vlan_data
*tt_vlan_tmp
;
2378 struct batadv_orig_node_vlan
*vlan
;
2382 /* check if each received CRC matches the locally stored one */
2383 for (i
= 0; i
< num_vlan
; i
++) {
2384 tt_vlan_tmp
= tt_vlan
+ i
;
2386 /* if orig_node is a backbone node for this VLAN, don't check
2387 * the CRC as we ignore all the global entries over it
2389 if (batadv_bla_is_backbone_gw_orig(orig_node
->bat_priv
,
2391 ntohs(tt_vlan_tmp
->vid
)))
2394 vlan
= batadv_orig_node_vlan_get(orig_node
,
2395 ntohs(tt_vlan_tmp
->vid
));
2400 batadv_orig_node_vlan_free_ref(vlan
);
2402 if (crc
!= ntohl(tt_vlan_tmp
->crc
))
2410 * batadv_tt_local_update_crc - update all the local CRCs
2411 * @bat_priv: the bat priv with all the soft interface information
2413 static void batadv_tt_local_update_crc(struct batadv_priv
*bat_priv
)
2415 struct batadv_softif_vlan
*vlan
;
2417 /* recompute the global CRC for each VLAN */
2419 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
2420 vlan
->tt
.crc
= batadv_tt_local_crc(bat_priv
, vlan
->vid
);
2426 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2427 * @bat_priv: the bat priv with all the soft interface information
2428 * @orig_node: the orig_node for which the CRCs have to be updated
2430 static void batadv_tt_global_update_crc(struct batadv_priv
*bat_priv
,
2431 struct batadv_orig_node
*orig_node
)
2433 struct batadv_orig_node_vlan
*vlan
;
2436 /* recompute the global CRC for each VLAN */
2438 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
2439 /* if orig_node is a backbone node for this VLAN, don't compute
2440 * the CRC as we ignore all the global entries over it
2442 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
,
2446 crc
= batadv_tt_global_crc(bat_priv
, orig_node
, vlan
->vid
);
2453 * batadv_send_tt_request - send a TT Request message to a given node
2454 * @bat_priv: the bat priv with all the soft interface information
2455 * @dst_orig_node: the destination of the message
2456 * @ttvn: the version number that the source of the message is looking for
2457 * @tt_vlan: pointer to the first tvlv VLAN object to request
2458 * @num_vlan: number of tvlv VLAN entries
2459 * @full_table: ask for the entire translation table if true, while only for the
2460 * last TT diff otherwise
2462 static int batadv_send_tt_request(struct batadv_priv
*bat_priv
,
2463 struct batadv_orig_node
*dst_orig_node
,
2465 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2466 uint16_t num_vlan
, bool full_table
)
2468 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2469 struct batadv_tt_req_node
*tt_req_node
= NULL
;
2470 struct batadv_tvlv_tt_vlan_data
*tt_vlan_req
;
2471 struct batadv_hard_iface
*primary_if
;
2475 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2479 /* The new tt_req will be issued only if I'm not waiting for a
2480 * reply from the same orig_node yet
2482 tt_req_node
= batadv_new_tt_req_node(bat_priv
, dst_orig_node
);
2486 size
= sizeof(*tvlv_tt_data
) + sizeof(*tt_vlan_req
) * num_vlan
;
2487 tvlv_tt_data
= kzalloc(size
, GFP_ATOMIC
);
2491 tvlv_tt_data
->flags
= BATADV_TT_REQUEST
;
2492 tvlv_tt_data
->ttvn
= ttvn
;
2493 tvlv_tt_data
->num_vlan
= htons(num_vlan
);
2495 /* send all the CRCs within the request. This is needed by intermediate
2496 * nodes to ensure they have the correct table before replying
2498 tt_vlan_req
= (struct batadv_tvlv_tt_vlan_data
*)(tvlv_tt_data
+ 1);
2499 for (i
= 0; i
< num_vlan
; i
++) {
2500 tt_vlan_req
->vid
= tt_vlan
->vid
;
2501 tt_vlan_req
->crc
= tt_vlan
->crc
;
2508 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2510 batadv_dbg(BATADV_DBG_TT
, bat_priv
, "Sending TT_REQUEST to %pM [%c]\n",
2511 dst_orig_node
->orig
, full_table
? 'F' : '.');
2513 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
2514 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2515 dst_orig_node
->orig
, BATADV_TVLV_TT
, 1,
2516 tvlv_tt_data
, size
);
2521 batadv_hardif_free_ref(primary_if
);
2522 if (ret
&& tt_req_node
) {
2523 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2524 list_del(&tt_req_node
->list
);
2525 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2528 kfree(tvlv_tt_data
);
2533 * batadv_send_other_tt_response - send reply to tt request concerning another
2534 * node's translation table
2535 * @bat_priv: the bat priv with all the soft interface information
2536 * @tt_data: tt data containing the tt request information
2537 * @req_src: mac address of tt request sender
2538 * @req_dst: mac address of tt request recipient
2540 * Returns true if tt request reply was sent, false otherwise.
2542 static bool batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
2543 struct batadv_tvlv_tt_data
*tt_data
,
2544 uint8_t *req_src
, uint8_t *req_dst
)
2546 struct batadv_orig_node
*req_dst_orig_node
;
2547 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
2548 struct batadv_tvlv_tt_change
*tt_change
;
2549 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2550 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
2551 bool ret
= false, full_table
;
2552 uint8_t orig_ttvn
, req_ttvn
;
2556 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2557 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2558 req_src
, tt_data
->ttvn
, req_dst
,
2559 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2561 /* Let's get the orig node of the REAL destination */
2562 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_dst
);
2563 if (!req_dst_orig_node
)
2566 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2567 if (!res_dst_orig_node
)
2570 orig_ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
2571 req_ttvn
= tt_data
->ttvn
;
2573 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
2574 /* this node doesn't have the requested data */
2575 if (orig_ttvn
!= req_ttvn
||
2576 !batadv_tt_global_check_crc(req_dst_orig_node
, tt_vlan
,
2577 ntohs(tt_data
->num_vlan
)))
2580 /* If the full table has been explicitly requested */
2581 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
||
2582 !req_dst_orig_node
->tt_buff
)
2587 /* TT fragmentation hasn't been implemented yet, so send as many
2588 * TT entries fit a single packet as possible only
2591 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
2592 tt_len
= req_dst_orig_node
->tt_buff_len
;
2594 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2601 /* Copy the last orig_node's OGM buffer */
2602 memcpy(tt_change
, req_dst_orig_node
->tt_buff
,
2603 req_dst_orig_node
->tt_buff_len
);
2604 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2606 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2607 * in the initial part
2610 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2617 /* fill the rest of the tvlv with the real TT entries */
2618 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.global_hash
,
2620 batadv_tt_global_valid
,
2624 /* Don't send the response, if larger than fragmented packet. */
2625 tt_len
= sizeof(struct batadv_unicast_tvlv_packet
) + tvlv_len
;
2626 if (tt_len
> atomic_read(&bat_priv
->packet_size_max
)) {
2627 net_ratelimited_function(batadv_info
, bat_priv
->soft_iface
,
2628 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2629 res_dst_orig_node
->orig
);
2633 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2634 tvlv_tt_data
->ttvn
= req_ttvn
;
2637 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2639 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2640 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2641 res_dst_orig_node
->orig
, req_dst_orig_node
->orig
,
2642 full_table
? 'F' : '.', req_ttvn
);
2644 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2646 batadv_tvlv_unicast_send(bat_priv
, req_dst_orig_node
->orig
,
2647 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2654 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2657 if (res_dst_orig_node
)
2658 batadv_orig_node_free_ref(res_dst_orig_node
);
2659 if (req_dst_orig_node
)
2660 batadv_orig_node_free_ref(req_dst_orig_node
);
2661 kfree(tvlv_tt_data
);
2666 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2668 * @bat_priv: the bat priv with all the soft interface information
2669 * @tt_data: tt data containing the tt request information
2670 * @req_src: mac address of tt request sender
2672 * Returns true if tt request reply was sent, false otherwise.
2674 static bool batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
2675 struct batadv_tvlv_tt_data
*tt_data
,
2678 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2679 struct batadv_hard_iface
*primary_if
= NULL
;
2680 struct batadv_tvlv_tt_change
*tt_change
;
2681 struct batadv_orig_node
*orig_node
;
2682 uint8_t my_ttvn
, req_ttvn
;
2687 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2688 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2689 req_src
, tt_data
->ttvn
,
2690 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2692 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
2694 my_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
2695 req_ttvn
= tt_data
->ttvn
;
2697 orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2701 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2705 /* If the full table has been explicitly requested or the gap
2706 * is too big send the whole local translation table
2708 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
2709 !bat_priv
->tt
.last_changeset
)
2714 /* TT fragmentation hasn't been implemented yet, so send as many
2715 * TT entries fit a single packet as possible only
2718 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
2720 tt_len
= bat_priv
->tt
.last_changeset_len
;
2721 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2728 /* Copy the last orig_node's OGM buffer */
2729 memcpy(tt_change
, bat_priv
->tt
.last_changeset
,
2730 bat_priv
->tt
.last_changeset_len
);
2731 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2733 req_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
2735 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2736 * in the initial part
2739 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2746 /* fill the rest of the tvlv with the real TT entries */
2747 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.local_hash
,
2749 batadv_tt_local_valid
, NULL
);
2752 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2753 tvlv_tt_data
->ttvn
= req_ttvn
;
2756 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2758 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2759 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2760 orig_node
->orig
, full_table
? 'F' : '.', req_ttvn
);
2762 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2764 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2765 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2771 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2773 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
2775 batadv_orig_node_free_ref(orig_node
);
2777 batadv_hardif_free_ref(primary_if
);
2778 kfree(tvlv_tt_data
);
2779 /* The packet was for this host, so it doesn't need to be re-routed */
2784 * batadv_send_tt_response - send reply to tt request
2785 * @bat_priv: the bat priv with all the soft interface information
2786 * @tt_data: tt data containing the tt request information
2787 * @req_src: mac address of tt request sender
2788 * @req_dst: mac address of tt request recipient
2790 * Returns true if tt request reply was sent, false otherwise.
2792 static bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
2793 struct batadv_tvlv_tt_data
*tt_data
,
2794 uint8_t *req_src
, uint8_t *req_dst
)
2796 if (batadv_is_my_mac(bat_priv
, req_dst
))
2797 return batadv_send_my_tt_response(bat_priv
, tt_data
, req_src
);
2798 return batadv_send_other_tt_response(bat_priv
, tt_data
, req_src
,
2802 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2803 struct batadv_orig_node
*orig_node
,
2804 struct batadv_tvlv_tt_change
*tt_change
,
2805 uint16_t tt_num_changes
, uint8_t ttvn
)
2810 for (i
= 0; i
< tt_num_changes
; i
++) {
2811 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
2812 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
2813 batadv_tt_global_del(bat_priv
, orig_node
,
2814 (tt_change
+ i
)->addr
,
2815 ntohs((tt_change
+ i
)->vid
),
2816 "tt removed by changes",
2819 if (!batadv_tt_global_add(bat_priv
, orig_node
,
2820 (tt_change
+ i
)->addr
,
2821 ntohs((tt_change
+ i
)->vid
),
2822 (tt_change
+ i
)->flags
, ttvn
))
2823 /* In case of problem while storing a
2824 * global_entry, we stop the updating
2825 * procedure without committing the
2826 * ttvn change. This will avoid to send
2827 * corrupted data on tt_request
2832 orig_node
->capa_initialized
|= BATADV_ORIG_CAPA_HAS_TT
;
2835 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
2836 struct batadv_tvlv_tt_change
*tt_change
,
2837 uint8_t ttvn
, uint8_t *resp_src
,
2838 uint16_t num_entries
)
2840 struct batadv_orig_node
*orig_node
;
2842 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2846 /* Purge the old table first.. */
2847 batadv_tt_global_del_orig(bat_priv
, orig_node
, -1,
2848 "Received full table");
2850 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
, num_entries
,
2853 spin_lock_bh(&orig_node
->tt_buff_lock
);
2854 kfree(orig_node
->tt_buff
);
2855 orig_node
->tt_buff_len
= 0;
2856 orig_node
->tt_buff
= NULL
;
2857 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2859 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2863 batadv_orig_node_free_ref(orig_node
);
2866 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2867 struct batadv_orig_node
*orig_node
,
2868 uint16_t tt_num_changes
, uint8_t ttvn
,
2869 struct batadv_tvlv_tt_change
*tt_change
)
2871 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
2872 tt_num_changes
, ttvn
);
2874 batadv_tt_save_orig_buffer(bat_priv
, orig_node
, tt_change
,
2875 batadv_tt_len(tt_num_changes
));
2876 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2880 * batadv_is_my_client - check if a client is served by the local node
2881 * @bat_priv: the bat priv with all the soft interface information
2882 * @addr: the mac address of the client to check
2883 * @vid: VLAN identifier
2885 * Returns true if the client is served by this node, false otherwise.
2887 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
2890 struct batadv_tt_local_entry
*tt_local_entry
;
2893 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
2894 if (!tt_local_entry
)
2896 /* Check if the client has been logically deleted (but is kept for
2897 * consistency purpose)
2899 if ((tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
) ||
2900 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
2905 batadv_tt_local_entry_free_ref(tt_local_entry
);
2910 * batadv_handle_tt_response - process incoming tt reply
2911 * @bat_priv: the bat priv with all the soft interface information
2912 * @tt_data: tt data containing the tt request information
2913 * @resp_src: mac address of tt reply sender
2914 * @num_entries: number of tt change entries appended to the tt data
2916 static void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
2917 struct batadv_tvlv_tt_data
*tt_data
,
2918 uint8_t *resp_src
, uint16_t num_entries
)
2920 struct batadv_tt_req_node
*node
, *safe
;
2921 struct batadv_orig_node
*orig_node
= NULL
;
2922 struct batadv_tvlv_tt_change
*tt_change
;
2923 uint8_t *tvlv_ptr
= (uint8_t *)tt_data
;
2924 uint16_t change_offset
;
2926 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2927 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2928 resp_src
, tt_data
->ttvn
, num_entries
,
2929 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2931 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2935 spin_lock_bh(&orig_node
->tt_lock
);
2937 change_offset
= sizeof(struct batadv_tvlv_tt_vlan_data
);
2938 change_offset
*= ntohs(tt_data
->num_vlan
);
2939 change_offset
+= sizeof(*tt_data
);
2940 tvlv_ptr
+= change_offset
;
2942 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_ptr
;
2943 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
) {
2944 batadv_tt_fill_gtable(bat_priv
, tt_change
, tt_data
->ttvn
,
2945 resp_src
, num_entries
);
2947 batadv_tt_update_changes(bat_priv
, orig_node
, num_entries
,
2948 tt_data
->ttvn
, tt_change
);
2951 /* Recalculate the CRC for this orig_node and store it */
2952 batadv_tt_global_update_crc(bat_priv
, orig_node
);
2954 spin_unlock_bh(&orig_node
->tt_lock
);
2956 /* Delete the tt_req_node from pending tt_requests list */
2957 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2958 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2959 if (!batadv_compare_eth(node
->addr
, resp_src
))
2961 list_del(&node
->list
);
2965 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2968 batadv_orig_node_free_ref(orig_node
);
2971 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
2973 struct batadv_tt_roam_node
*node
, *safe
;
2975 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2977 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2978 list_del(&node
->list
);
2982 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2985 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
2987 struct batadv_tt_roam_node
*node
, *safe
;
2989 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2990 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2991 if (!batadv_has_timed_out(node
->first_time
,
2992 BATADV_ROAMING_MAX_TIME
))
2995 list_del(&node
->list
);
2998 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3001 /* This function checks whether the client already reached the
3002 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3005 * returns true if the ROAMING_ADV can be sent, false otherwise
3007 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
,
3010 struct batadv_tt_roam_node
*tt_roam_node
;
3013 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3014 /* The new tt_req will be issued only if I'm not waiting for a
3015 * reply from the same orig_node yet
3017 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
3018 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
3021 if (batadv_has_timed_out(tt_roam_node
->first_time
,
3022 BATADV_ROAMING_MAX_TIME
))
3025 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
3026 /* Sorry, you roamed too many times! */
3033 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
3037 tt_roam_node
->first_time
= jiffies
;
3038 atomic_set(&tt_roam_node
->counter
,
3039 BATADV_ROAMING_MAX_COUNT
- 1);
3040 ether_addr_copy(tt_roam_node
->addr
, client
);
3042 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
3047 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3052 * batadv_send_roam_adv - send a roaming advertisement message
3053 * @bat_priv: the bat priv with all the soft interface information
3054 * @client: mac address of the roaming client
3055 * @vid: VLAN identifier
3056 * @orig_node: message destination
3058 * Send a ROAMING_ADV message to the node which was previously serving this
3059 * client. This is done to inform the node that from now on all traffic destined
3060 * for this particular roamed client has to be forwarded to the sender of the
3063 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
3065 struct batadv_orig_node
*orig_node
)
3067 struct batadv_hard_iface
*primary_if
;
3068 struct batadv_tvlv_roam_adv tvlv_roam
;
3070 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3074 /* before going on we have to check whether the client has
3075 * already roamed to us too many times
3077 if (!batadv_tt_check_roam_count(bat_priv
, client
))
3080 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3081 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3082 orig_node
->orig
, client
, BATADV_PRINT_VID(vid
));
3084 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
3086 memcpy(tvlv_roam
.client
, client
, sizeof(tvlv_roam
.client
));
3087 tvlv_roam
.vid
= htons(vid
);
3089 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3090 orig_node
->orig
, BATADV_TVLV_ROAM
, 1,
3091 &tvlv_roam
, sizeof(tvlv_roam
));
3095 batadv_hardif_free_ref(primary_if
);
3098 static void batadv_tt_purge(struct work_struct
*work
)
3100 struct delayed_work
*delayed_work
;
3101 struct batadv_priv_tt
*priv_tt
;
3102 struct batadv_priv
*bat_priv
;
3104 delayed_work
= container_of(work
, struct delayed_work
, work
);
3105 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
3106 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
3108 batadv_tt_local_purge(bat_priv
, BATADV_TT_LOCAL_TIMEOUT
);
3109 batadv_tt_global_purge(bat_priv
);
3110 batadv_tt_req_purge(bat_priv
);
3111 batadv_tt_roam_purge(bat_priv
);
3113 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3114 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3117 void batadv_tt_free(struct batadv_priv
*bat_priv
)
3119 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3120 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3122 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
3124 batadv_tt_local_table_free(bat_priv
);
3125 batadv_tt_global_table_free(bat_priv
);
3126 batadv_tt_req_list_free(bat_priv
);
3127 batadv_tt_changes_list_free(bat_priv
);
3128 batadv_tt_roam_list_free(bat_priv
);
3130 kfree(bat_priv
->tt
.last_changeset
);
3134 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3135 * table and possibly count them in the TT size
3136 * @bat_priv: the bat priv with all the soft interface information
3137 * @flags: the flag to switch
3138 * @enable: whether to set or unset the flag
3139 * @count: whether to increase the TT size by the number of changed entries
3141 static void batadv_tt_local_set_flags(struct batadv_priv
*bat_priv
,
3142 uint16_t flags
, bool enable
, bool count
)
3144 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3145 struct batadv_tt_common_entry
*tt_common_entry
;
3146 uint16_t changed_num
= 0;
3147 struct hlist_head
*head
;
3153 for (i
= 0; i
< hash
->size
; i
++) {
3154 head
= &hash
->table
[i
];
3157 hlist_for_each_entry_rcu(tt_common_entry
,
3160 if ((tt_common_entry
->flags
& flags
) == flags
)
3162 tt_common_entry
->flags
|= flags
;
3164 if (!(tt_common_entry
->flags
& flags
))
3166 tt_common_entry
->flags
&= ~flags
;
3173 batadv_tt_local_size_inc(bat_priv
,
3174 tt_common_entry
->vid
);
3180 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3181 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
3183 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3184 struct batadv_tt_common_entry
*tt_common
;
3185 struct batadv_tt_local_entry
*tt_local
;
3186 struct batadv_softif_vlan
*vlan
;
3187 struct hlist_node
*node_tmp
;
3188 struct hlist_head
*head
;
3189 spinlock_t
*list_lock
; /* protects write access to the hash lists */
3195 for (i
= 0; i
< hash
->size
; i
++) {
3196 head
= &hash
->table
[i
];
3197 list_lock
= &hash
->list_locks
[i
];
3199 spin_lock_bh(list_lock
);
3200 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
3202 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
3205 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3206 "Deleting local tt entry (%pM, vid: %d): pending\n",
3208 BATADV_PRINT_VID(tt_common
->vid
));
3210 batadv_tt_local_size_dec(bat_priv
, tt_common
->vid
);
3211 hlist_del_rcu(&tt_common
->hash_entry
);
3212 tt_local
= container_of(tt_common
,
3213 struct batadv_tt_local_entry
,
3216 /* decrease the reference held for this vlan */
3217 vlan
= batadv_softif_vlan_get(bat_priv
, tt_common
->vid
);
3219 batadv_softif_vlan_free_ref(vlan
);
3220 batadv_softif_vlan_free_ref(vlan
);
3223 batadv_tt_local_entry_free_ref(tt_local
);
3225 spin_unlock_bh(list_lock
);
3230 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3231 * which have been queued in the time since the last commit
3232 * @bat_priv: the bat priv with all the soft interface information
3234 * Caller must hold tt->commit_lock.
3236 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv
*bat_priv
)
3238 /* Update multicast addresses in local translation table */
3239 batadv_mcast_mla_update(bat_priv
);
3241 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1) {
3242 if (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))
3243 batadv_tt_tvlv_container_update(bat_priv
);
3247 batadv_tt_local_set_flags(bat_priv
, BATADV_TT_CLIENT_NEW
, false, true);
3249 batadv_tt_local_purge_pending_clients(bat_priv
);
3250 batadv_tt_local_update_crc(bat_priv
);
3252 /* Increment the TTVN only once per OGM interval */
3253 atomic_inc(&bat_priv
->tt
.vn
);
3254 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3255 "Local changes committed, updating to ttvn %u\n",
3256 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
3258 /* reset the sending counter */
3259 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
3260 batadv_tt_tvlv_container_update(bat_priv
);
3264 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3265 * have been queued in the time since the last commit
3266 * @bat_priv: the bat priv with all the soft interface information
3268 void batadv_tt_local_commit_changes(struct batadv_priv
*bat_priv
)
3270 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3271 batadv_tt_local_commit_changes_nolock(bat_priv
);
3272 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3275 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, uint8_t *src
,
3276 uint8_t *dst
, unsigned short vid
)
3278 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
3279 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
3280 struct batadv_softif_vlan
*vlan
;
3283 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
3284 if (!vlan
|| !atomic_read(&vlan
->ap_isolation
))
3287 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
, vid
);
3288 if (!tt_local_entry
)
3291 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
, vid
);
3292 if (!tt_global_entry
)
3295 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
3302 batadv_softif_vlan_free_ref(vlan
);
3303 if (tt_global_entry
)
3304 batadv_tt_global_entry_free_ref(tt_global_entry
);
3306 batadv_tt_local_entry_free_ref(tt_local_entry
);
3311 * batadv_tt_update_orig - update global translation table with new tt
3312 * information received via ogms
3313 * @bat_priv: the bat priv with all the soft interface information
3314 * @orig: the orig_node of the ogm
3315 * @tt_vlan: pointer to the first tvlv VLAN entry
3316 * @tt_num_vlan: number of tvlv VLAN entries
3317 * @tt_change: pointer to the first entry in the TT buffer
3318 * @tt_num_changes: number of tt changes inside the tt buffer
3319 * @ttvn: translation table version number of this changeset
3320 * @tt_crc: crc32 checksum of orig node's translation table
3322 static void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
3323 struct batadv_orig_node
*orig_node
,
3324 const void *tt_buff
, uint16_t tt_num_vlan
,
3325 struct batadv_tvlv_tt_change
*tt_change
,
3326 uint16_t tt_num_changes
, uint8_t ttvn
)
3328 uint8_t orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
3329 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3330 bool full_table
= true;
3333 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)tt_buff
;
3334 has_tt_init
= orig_node
->capa_initialized
& BATADV_ORIG_CAPA_HAS_TT
;
3336 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3337 * increased by one -> we can apply the attached changes
3339 if ((!has_tt_init
&& ttvn
== 1) || ttvn
- orig_ttvn
== 1) {
3340 /* the OGM could not contain the changes due to their size or
3341 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3343 * In this case send a tt request
3345 if (!tt_num_changes
) {
3350 spin_lock_bh(&orig_node
->tt_lock
);
3352 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
3355 /* Even if we received the precomputed crc with the OGM, we
3356 * prefer to recompute it to spot any possible inconsistency
3357 * in the global table
3359 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3361 spin_unlock_bh(&orig_node
->tt_lock
);
3363 /* The ttvn alone is not enough to guarantee consistency
3364 * because a single value could represent different states
3365 * (due to the wrap around). Thus a node has to check whether
3366 * the resulting table (after applying the changes) is still
3367 * consistent or not. E.g. a node could disconnect while its
3368 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3369 * checking the CRC value is mandatory to detect the
3372 if (!batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3376 /* if we missed more than one change or our tables are not
3377 * in sync anymore -> request fresh tt data
3379 if (!has_tt_init
|| ttvn
!= orig_ttvn
||
3380 !batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3383 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3384 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3385 orig_node
->orig
, ttvn
, orig_ttvn
,
3387 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
3388 tt_vlan
, tt_num_vlan
,
3396 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3397 * @bat_priv: the bat priv with all the soft interface information
3398 * @addr: the mac address of the client to check
3399 * @vid: VLAN identifier
3401 * Returns true if we know that the client has moved from its old originator
3402 * to another one. This entry is still kept for consistency purposes and will be
3403 * deleted later by a DEL or because of timeout
3405 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
3406 uint8_t *addr
, unsigned short vid
)
3408 struct batadv_tt_global_entry
*tt_global_entry
;
3411 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3412 if (!tt_global_entry
)
3415 ret
= tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3416 batadv_tt_global_entry_free_ref(tt_global_entry
);
3422 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3423 * @bat_priv: the bat priv with all the soft interface information
3424 * @addr: the mac address of the local client to query
3425 * @vid: VLAN identifier
3427 * Returns true if the local client is known to be roaming (it is not served by
3428 * this node anymore) or not. If yes, the client is still present in the table
3429 * to keep the latter consistent with the node TTVN
3431 bool batadv_tt_local_client_is_roaming(struct batadv_priv
*bat_priv
,
3432 uint8_t *addr
, unsigned short vid
)
3434 struct batadv_tt_local_entry
*tt_local_entry
;
3437 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
3438 if (!tt_local_entry
)
3441 ret
= tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3442 batadv_tt_local_entry_free_ref(tt_local_entry
);
3447 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
3448 struct batadv_orig_node
*orig_node
,
3449 const unsigned char *addr
,
3454 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
, vid
,
3455 BATADV_TT_CLIENT_TEMP
,
3456 atomic_read(&orig_node
->last_ttvn
)))
3459 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3460 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3461 addr
, BATADV_PRINT_VID(vid
), orig_node
->orig
);
3468 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3469 * maximum packet size that can be transported through the mesh
3470 * @soft_iface: netdev struct of the mesh interface
3472 * Remove entries older than 'timeout' and half timeout if more entries need
3475 void batadv_tt_local_resize_to_mtu(struct net_device
*soft_iface
)
3477 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
3478 int packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
3479 int table_size
, timeout
= BATADV_TT_LOCAL_TIMEOUT
/ 2;
3480 bool reduced
= false;
3482 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3485 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
3486 if (packet_size_max
>= table_size
)
3489 batadv_tt_local_purge(bat_priv
, timeout
);
3490 batadv_tt_local_purge_pending_clients(bat_priv
);
3494 net_ratelimited_function(batadv_info
, soft_iface
,
3495 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3499 /* commit these changes immediately, to avoid synchronization problem
3503 batadv_tt_local_commit_changes_nolock(bat_priv
);
3505 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3509 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3510 * @bat_priv: the bat priv with all the soft interface information
3511 * @orig: the orig_node of the ogm
3512 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3513 * @tvlv_value: tvlv buffer containing the gateway data
3514 * @tvlv_value_len: tvlv buffer length
3516 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
3517 struct batadv_orig_node
*orig
,
3518 uint8_t flags
, void *tvlv_value
,
3519 uint16_t tvlv_value_len
)
3521 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3522 struct batadv_tvlv_tt_change
*tt_change
;
3523 struct batadv_tvlv_tt_data
*tt_data
;
3524 uint16_t num_entries
, num_vlan
;
3526 if (tvlv_value_len
< sizeof(*tt_data
))
3529 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3530 tvlv_value_len
-= sizeof(*tt_data
);
3532 num_vlan
= ntohs(tt_data
->num_vlan
);
3534 if (tvlv_value_len
< sizeof(*tt_vlan
) * num_vlan
)
3537 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
3538 tt_change
= (struct batadv_tvlv_tt_change
*)(tt_vlan
+ num_vlan
);
3539 tvlv_value_len
-= sizeof(*tt_vlan
) * num_vlan
;
3541 num_entries
= batadv_tt_entries(tvlv_value_len
);
3543 batadv_tt_update_orig(bat_priv
, orig
, tt_vlan
, num_vlan
, tt_change
,
3544 num_entries
, tt_data
->ttvn
);
3548 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3550 * @bat_priv: the bat priv with all the soft interface information
3551 * @src: mac address of tt tvlv sender
3552 * @dst: mac address of tt tvlv recipient
3553 * @tvlv_value: tvlv buffer containing the tt data
3554 * @tvlv_value_len: tvlv buffer length
3556 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3559 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3560 uint8_t *src
, uint8_t *dst
,
3562 uint16_t tvlv_value_len
)
3564 struct batadv_tvlv_tt_data
*tt_data
;
3565 uint16_t tt_vlan_len
, tt_num_entries
;
3569 if (tvlv_value_len
< sizeof(*tt_data
))
3570 return NET_RX_SUCCESS
;
3572 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3573 tvlv_value_len
-= sizeof(*tt_data
);
3575 tt_vlan_len
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3576 tt_vlan_len
*= ntohs(tt_data
->num_vlan
);
3578 if (tvlv_value_len
< tt_vlan_len
)
3579 return NET_RX_SUCCESS
;
3581 tvlv_value_len
-= tt_vlan_len
;
3582 tt_num_entries
= batadv_tt_entries(tvlv_value_len
);
3584 switch (tt_data
->flags
& BATADV_TT_DATA_TYPE_MASK
) {
3585 case BATADV_TT_REQUEST
:
3586 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
3588 /* If this node cannot provide a TT response the tt_request is
3591 ret
= batadv_send_tt_response(bat_priv
, tt_data
, src
, dst
);
3593 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3598 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3599 "Routing TT_REQUEST to %pM [%c]\n",
3601 /* tvlv API will re-route the packet */
3605 case BATADV_TT_RESPONSE
:
3606 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
3608 if (batadv_is_my_mac(bat_priv
, dst
)) {
3609 batadv_handle_tt_response(bat_priv
, tt_data
,
3610 src
, tt_num_entries
);
3611 return NET_RX_SUCCESS
;
3614 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3619 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3620 "Routing TT_RESPONSE to %pM [%c]\n", dst
, tt_flag
);
3622 /* tvlv API will re-route the packet */
3626 return NET_RX_SUCCESS
;
3630 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3631 * @bat_priv: the bat priv with all the soft interface information
3632 * @src: mac address of tt tvlv sender
3633 * @dst: mac address of tt tvlv recipient
3634 * @tvlv_value: tvlv buffer containing the tt data
3635 * @tvlv_value_len: tvlv buffer length
3637 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3640 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3641 uint8_t *src
, uint8_t *dst
,
3643 uint16_t tvlv_value_len
)
3645 struct batadv_tvlv_roam_adv
*roaming_adv
;
3646 struct batadv_orig_node
*orig_node
= NULL
;
3648 /* If this node is not the intended recipient of the
3649 * roaming advertisement the packet is forwarded
3650 * (the tvlv API will re-route the packet).
3652 if (!batadv_is_my_mac(bat_priv
, dst
))
3655 if (tvlv_value_len
< sizeof(*roaming_adv
))
3658 orig_node
= batadv_orig_hash_find(bat_priv
, src
);
3662 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
3663 roaming_adv
= (struct batadv_tvlv_roam_adv
*)tvlv_value
;
3665 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3666 "Received ROAMING_ADV from %pM (client %pM)\n",
3667 src
, roaming_adv
->client
);
3669 batadv_tt_global_add(bat_priv
, orig_node
, roaming_adv
->client
,
3670 ntohs(roaming_adv
->vid
), BATADV_TT_CLIENT_ROAM
,
3671 atomic_read(&orig_node
->last_ttvn
) + 1);
3675 batadv_orig_node_free_ref(orig_node
);
3676 return NET_RX_SUCCESS
;
3680 * batadv_tt_init - initialise the translation table internals
3681 * @bat_priv: the bat priv with all the soft interface information
3683 * Return 0 on success or negative error number in case of failure.
3685 int batadv_tt_init(struct batadv_priv
*bat_priv
)
3689 /* synchronized flags must be remote */
3690 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK
& BATADV_TT_REMOTE_MASK
));
3692 ret
= batadv_tt_local_init(bat_priv
);
3696 ret
= batadv_tt_global_init(bat_priv
);
3700 batadv_tvlv_handler_register(bat_priv
, batadv_tt_tvlv_ogm_handler_v1
,
3701 batadv_tt_tvlv_unicast_handler_v1
,
3702 BATADV_TVLV_TT
, 1, BATADV_NO_FLAGS
);
3704 batadv_tvlv_handler_register(bat_priv
, NULL
,
3705 batadv_roam_tvlv_unicast_handler_v1
,
3706 BATADV_TVLV_ROAM
, 1, BATADV_NO_FLAGS
);
3708 INIT_DELAYED_WORK(&bat_priv
->tt
.work
, batadv_tt_purge
);
3709 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3710 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3716 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3717 * @bat_priv: the bat priv with all the soft interface information
3718 * @addr: the mac address of the client
3719 * @vid: the identifier of the VLAN where this client is connected
3721 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3724 bool batadv_tt_global_is_isolated(struct batadv_priv
*bat_priv
,
3725 const uint8_t *addr
, unsigned short vid
)
3727 struct batadv_tt_global_entry
*tt
;
3730 tt
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3734 ret
= tt
->common
.flags
& BATADV_TT_CLIENT_ISOLA
;
3736 batadv_tt_global_entry_free_ref(tt
);