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
;
1040 void *tt_entry_exists
;
1042 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
1043 if (!tt_local_entry
)
1046 curr_flags
= tt_local_entry
->common
.flags
;
1048 flags
= BATADV_TT_CLIENT_DEL
;
1049 /* if this global entry addition is due to a roaming, the node has to
1050 * mark the local entry as "roamed" in order to correctly reroute
1054 flags
|= BATADV_TT_CLIENT_ROAM
;
1055 /* mark the local client as ROAMed */
1056 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1059 if (!(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
)) {
1060 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
,
1064 /* if this client has been added right now, it is possible to
1065 * immediately purge it
1067 batadv_tt_local_event(bat_priv
, tt_local_entry
, BATADV_TT_CLIENT_DEL
);
1069 tt_entry_exists
= batadv_hash_remove(bat_priv
->tt
.local_hash
,
1072 &tt_local_entry
->common
);
1073 if (!tt_entry_exists
)
1076 /* extra call to free the local tt entry */
1077 batadv_tt_local_entry_free_ref(tt_local_entry
);
1079 /* decrease the reference held for this vlan */
1080 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
1084 batadv_softif_vlan_free_ref(vlan
);
1085 batadv_softif_vlan_free_ref(vlan
);
1089 batadv_tt_local_entry_free_ref(tt_local_entry
);
1095 * batadv_tt_local_purge_list - purge inactive tt local entries
1096 * @bat_priv: the bat priv with all the soft interface information
1097 * @head: pointer to the list containing the local tt entries
1098 * @timeout: parameter deciding whether a given tt local entry is considered
1101 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
1102 struct hlist_head
*head
,
1105 struct batadv_tt_local_entry
*tt_local_entry
;
1106 struct batadv_tt_common_entry
*tt_common_entry
;
1107 struct hlist_node
*node_tmp
;
1109 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
, head
,
1111 tt_local_entry
= container_of(tt_common_entry
,
1112 struct batadv_tt_local_entry
,
1114 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
1117 /* entry already marked for deletion */
1118 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1121 if (!batadv_has_timed_out(tt_local_entry
->last_seen
, timeout
))
1124 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
1125 BATADV_TT_CLIENT_DEL
, "timed out");
1130 * batadv_tt_local_purge - purge inactive tt local entries
1131 * @bat_priv: the bat priv with all the soft interface information
1132 * @timeout: parameter deciding whether a given tt local entry is considered
1135 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
,
1138 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1139 struct hlist_head
*head
;
1140 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1143 for (i
= 0; i
< hash
->size
; i
++) {
1144 head
= &hash
->table
[i
];
1145 list_lock
= &hash
->list_locks
[i
];
1147 spin_lock_bh(list_lock
);
1148 batadv_tt_local_purge_list(bat_priv
, head
, timeout
);
1149 spin_unlock_bh(list_lock
);
1153 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
1155 struct batadv_hashtable
*hash
;
1156 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1157 struct batadv_tt_common_entry
*tt_common_entry
;
1158 struct batadv_tt_local_entry
*tt_local
;
1159 struct batadv_softif_vlan
*vlan
;
1160 struct hlist_node
*node_tmp
;
1161 struct hlist_head
*head
;
1164 if (!bat_priv
->tt
.local_hash
)
1167 hash
= bat_priv
->tt
.local_hash
;
1169 for (i
= 0; i
< hash
->size
; i
++) {
1170 head
= &hash
->table
[i
];
1171 list_lock
= &hash
->list_locks
[i
];
1173 spin_lock_bh(list_lock
);
1174 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1176 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1177 tt_local
= container_of(tt_common_entry
,
1178 struct batadv_tt_local_entry
,
1181 /* decrease the reference held for this vlan */
1182 vlan
= batadv_softif_vlan_get(bat_priv
,
1183 tt_common_entry
->vid
);
1185 batadv_softif_vlan_free_ref(vlan
);
1186 batadv_softif_vlan_free_ref(vlan
);
1189 batadv_tt_local_entry_free_ref(tt_local
);
1191 spin_unlock_bh(list_lock
);
1194 batadv_hash_destroy(hash
);
1196 bat_priv
->tt
.local_hash
= NULL
;
1199 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
1201 if (bat_priv
->tt
.global_hash
)
1204 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
1206 if (!bat_priv
->tt
.global_hash
)
1209 batadv_hash_set_lock_class(bat_priv
->tt
.global_hash
,
1210 &batadv_tt_global_hash_lock_class_key
);
1215 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
1217 struct batadv_tt_change_node
*entry
, *safe
;
1219 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
1221 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
1223 list_del(&entry
->list
);
1227 atomic_set(&bat_priv
->tt
.local_changes
, 0);
1228 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
1231 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1232 * batadv_tt_global_entry list
1234 * returns it with an increased refcounter, NULL if not found
1236 static struct batadv_tt_orig_list_entry
*
1237 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
1238 const struct batadv_orig_node
*orig_node
)
1240 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
1241 const struct hlist_head
*head
;
1244 head
= &entry
->orig_list
;
1245 hlist_for_each_entry_rcu(tmp_orig_entry
, head
, list
) {
1246 if (tmp_orig_entry
->orig_node
!= orig_node
)
1248 if (!atomic_inc_not_zero(&tmp_orig_entry
->refcount
))
1251 orig_entry
= tmp_orig_entry
;
1259 /* find out if an orig_node is already in the list of a tt_global_entry.
1260 * returns true if found, false otherwise
1263 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
1264 const struct batadv_orig_node
*orig_node
)
1266 struct batadv_tt_orig_list_entry
*orig_entry
;
1269 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
1272 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1279 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
1280 struct batadv_orig_node
*orig_node
, int ttvn
)
1282 struct batadv_tt_orig_list_entry
*orig_entry
;
1284 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
1286 /* refresh the ttvn: the current value could be a bogus one that
1287 * was added during a "temporary client detection"
1289 orig_entry
->ttvn
= ttvn
;
1293 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
1297 INIT_HLIST_NODE(&orig_entry
->list
);
1298 atomic_inc(&orig_node
->refcount
);
1299 batadv_tt_global_size_inc(orig_node
, tt_global
->common
.vid
);
1300 orig_entry
->orig_node
= orig_node
;
1301 orig_entry
->ttvn
= ttvn
;
1302 atomic_set(&orig_entry
->refcount
, 2);
1304 spin_lock_bh(&tt_global
->list_lock
);
1305 hlist_add_head_rcu(&orig_entry
->list
,
1306 &tt_global
->orig_list
);
1307 spin_unlock_bh(&tt_global
->list_lock
);
1308 atomic_inc(&tt_global
->orig_list_count
);
1312 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1316 * batadv_tt_global_add - add a new TT global entry or update an existing one
1317 * @bat_priv: the bat priv with all the soft interface information
1318 * @orig_node: the originator announcing the client
1319 * @tt_addr: the mac address of the non-mesh client
1320 * @vid: VLAN identifier
1321 * @flags: TT flags that have to be set for this non-mesh client
1322 * @ttvn: the tt version number ever announcing this non-mesh client
1324 * Add a new TT global entry for the given originator. If the entry already
1325 * exists add a new reference to the given originator (a global entry can have
1326 * references to multiple originators) and adjust the flags attribute to reflect
1327 * the function argument.
1328 * If a TT local entry exists for this non-mesh client remove it.
1330 * The caller must hold orig_node refcount.
1332 * Return true if the new entry has been added, false otherwise
1334 static bool batadv_tt_global_add(struct batadv_priv
*bat_priv
,
1335 struct batadv_orig_node
*orig_node
,
1336 const unsigned char *tt_addr
,
1337 unsigned short vid
, uint16_t flags
,
1340 struct batadv_tt_global_entry
*tt_global_entry
;
1341 struct batadv_tt_local_entry
*tt_local_entry
;
1344 struct batadv_tt_common_entry
*common
;
1345 uint16_t local_flags
;
1347 /* ignore global entries from backbone nodes */
1348 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
, vid
))
1351 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
, vid
);
1352 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, tt_addr
, vid
);
1354 /* if the node already has a local client for this entry, it has to wait
1355 * for a roaming advertisement instead of manually messing up the global
1358 if ((flags
& BATADV_TT_CLIENT_TEMP
) && tt_local_entry
&&
1359 !(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
))
1362 if (!tt_global_entry
) {
1363 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
1364 if (!tt_global_entry
)
1367 common
= &tt_global_entry
->common
;
1368 ether_addr_copy(common
->addr
, tt_addr
);
1371 common
->flags
= flags
;
1372 tt_global_entry
->roam_at
= 0;
1373 /* node must store current time in case of roaming. This is
1374 * needed to purge this entry out on timeout (if nobody claims
1377 if (flags
& BATADV_TT_CLIENT_ROAM
)
1378 tt_global_entry
->roam_at
= jiffies
;
1379 atomic_set(&common
->refcount
, 2);
1380 common
->added_at
= jiffies
;
1382 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
1383 atomic_set(&tt_global_entry
->orig_list_count
, 0);
1384 spin_lock_init(&tt_global_entry
->list_lock
);
1386 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
1388 batadv_choose_tt
, common
,
1389 &common
->hash_entry
);
1391 if (unlikely(hash_added
!= 0)) {
1392 /* remove the reference for the hash */
1393 batadv_tt_global_entry_free_ref(tt_global_entry
);
1397 common
= &tt_global_entry
->common
;
1398 /* If there is already a global entry, we can use this one for
1400 * But if we are trying to add a temporary client then here are
1401 * two options at this point:
1402 * 1) the global client is not a temporary client: the global
1403 * client has to be left as it is, temporary information
1404 * should never override any already known client state
1405 * 2) the global client is a temporary client: purge the
1406 * originator list and add the new one orig_entry
1408 if (flags
& BATADV_TT_CLIENT_TEMP
) {
1409 if (!(common
->flags
& BATADV_TT_CLIENT_TEMP
))
1411 if (batadv_tt_global_entry_has_orig(tt_global_entry
,
1414 batadv_tt_global_del_orig_list(tt_global_entry
);
1415 goto add_orig_entry
;
1418 /* if the client was temporary added before receiving the first
1419 * OGM announcing it, we have to clear the TEMP flag
1421 common
->flags
&= ~BATADV_TT_CLIENT_TEMP
;
1423 /* the change can carry possible "attribute" flags like the
1424 * TT_CLIENT_WIFI, therefore they have to be copied in the
1427 tt_global_entry
->common
.flags
|= flags
;
1429 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1430 * one originator left in the list and we previously received a
1431 * delete + roaming change for this originator.
1433 * We should first delete the old originator before adding the
1436 if (common
->flags
& BATADV_TT_CLIENT_ROAM
) {
1437 batadv_tt_global_del_orig_list(tt_global_entry
);
1438 common
->flags
&= ~BATADV_TT_CLIENT_ROAM
;
1439 tt_global_entry
->roam_at
= 0;
1443 /* add the new orig_entry (if needed) or update it */
1444 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
1446 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1447 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1448 common
->addr
, BATADV_PRINT_VID(common
->vid
),
1453 /* Do not remove multicast addresses from the local hash on
1456 if (is_multicast_ether_addr(tt_addr
))
1459 /* remove address from local hash if present */
1460 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
, vid
,
1461 "global tt received",
1462 flags
& BATADV_TT_CLIENT_ROAM
);
1463 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
1465 if (!(flags
& BATADV_TT_CLIENT_ROAM
))
1466 /* this is a normal global add. Therefore the client is not in a
1467 * roaming state anymore.
1469 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
1472 if (tt_global_entry
)
1473 batadv_tt_global_entry_free_ref(tt_global_entry
);
1475 batadv_tt_local_entry_free_ref(tt_local_entry
);
1480 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1481 * @bat_priv: the bat priv with all the soft interface information
1482 * @tt_global_entry: global translation table entry to be analyzed
1484 * This functon assumes the caller holds rcu_read_lock().
1485 * Returns best originator list entry or NULL on errors.
1487 static struct batadv_tt_orig_list_entry
*
1488 batadv_transtable_best_orig(struct batadv_priv
*bat_priv
,
1489 struct batadv_tt_global_entry
*tt_global_entry
)
1491 struct batadv_neigh_node
*router
, *best_router
= NULL
;
1492 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1493 struct hlist_head
*head
;
1494 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
= NULL
;
1496 head
= &tt_global_entry
->orig_list
;
1497 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1498 router
= batadv_orig_router_get(orig_entry
->orig_node
,
1504 bao
->bat_neigh_cmp(router
, BATADV_IF_DEFAULT
,
1505 best_router
, BATADV_IF_DEFAULT
) <= 0) {
1506 batadv_neigh_node_free_ref(router
);
1510 /* release the refcount for the "old" best */
1512 batadv_neigh_node_free_ref(best_router
);
1514 best_entry
= orig_entry
;
1515 best_router
= router
;
1519 batadv_neigh_node_free_ref(best_router
);
1525 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1526 * for this global entry
1527 * @bat_priv: the bat priv with all the soft interface information
1528 * @tt_global_entry: global translation table entry to be printed
1529 * @seq: debugfs table seq_file struct
1531 * This functon assumes the caller holds rcu_read_lock().
1534 batadv_tt_global_print_entry(struct batadv_priv
*bat_priv
,
1535 struct batadv_tt_global_entry
*tt_global_entry
,
1536 struct seq_file
*seq
)
1538 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
;
1539 struct batadv_tt_common_entry
*tt_common_entry
;
1540 struct batadv_orig_node_vlan
*vlan
;
1541 struct hlist_head
*head
;
1545 tt_common_entry
= &tt_global_entry
->common
;
1546 flags
= tt_common_entry
->flags
;
1548 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
1550 vlan
= batadv_orig_node_vlan_get(best_entry
->orig_node
,
1551 tt_common_entry
->vid
);
1554 " * Cannot retrieve VLAN %d for originator %pM\n",
1555 BATADV_PRINT_VID(tt_common_entry
->vid
),
1556 best_entry
->orig_node
->orig
);
1560 last_ttvn
= atomic_read(&best_entry
->orig_node
->last_ttvn
);
1562 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1563 '*', tt_global_entry
->common
.addr
,
1564 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1565 best_entry
->ttvn
, best_entry
->orig_node
->orig
,
1566 last_ttvn
, vlan
->tt
.crc
,
1567 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1568 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1569 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1570 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1572 batadv_orig_node_vlan_free_ref(vlan
);
1576 head
= &tt_global_entry
->orig_list
;
1578 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1579 if (best_entry
== orig_entry
)
1582 vlan
= batadv_orig_node_vlan_get(orig_entry
->orig_node
,
1583 tt_common_entry
->vid
);
1586 " + Cannot retrieve VLAN %d for originator %pM\n",
1587 BATADV_PRINT_VID(tt_common_entry
->vid
),
1588 orig_entry
->orig_node
->orig
);
1592 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
1594 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1595 '+', tt_global_entry
->common
.addr
,
1596 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1597 orig_entry
->ttvn
, orig_entry
->orig_node
->orig
,
1598 last_ttvn
, vlan
->tt
.crc
,
1599 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1600 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1601 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1602 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1604 batadv_orig_node_vlan_free_ref(vlan
);
1608 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
1610 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1611 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1612 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1613 struct batadv_tt_common_entry
*tt_common_entry
;
1614 struct batadv_tt_global_entry
*tt_global
;
1615 struct batadv_hard_iface
*primary_if
;
1616 struct hlist_head
*head
;
1619 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1624 "Globally announced TT entries received via the mesh %s\n",
1626 seq_printf(seq
, " %-13s %s %s %-15s %s (%-10s) %s\n",
1627 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1630 for (i
= 0; i
< hash
->size
; i
++) {
1631 head
= &hash
->table
[i
];
1634 hlist_for_each_entry_rcu(tt_common_entry
,
1636 tt_global
= container_of(tt_common_entry
,
1637 struct batadv_tt_global_entry
,
1639 batadv_tt_global_print_entry(bat_priv
, tt_global
, seq
);
1645 batadv_hardif_free_ref(primary_if
);
1650 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1651 * @tt_global_entry: the global entry to remove the orig_entry from
1652 * @orig_entry: the orig entry to remove and free
1654 * Remove an orig_entry from its list in the given tt_global_entry and
1655 * free this orig_entry afterwards.
1658 batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry
*tt_global_entry
,
1659 struct batadv_tt_orig_list_entry
*orig_entry
)
1661 batadv_tt_global_size_dec(orig_entry
->orig_node
,
1662 tt_global_entry
->common
.vid
);
1663 atomic_dec(&tt_global_entry
->orig_list_count
);
1664 hlist_del_rcu(&orig_entry
->list
);
1665 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1668 /* deletes the orig list of a tt_global_entry */
1670 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
1672 struct hlist_head
*head
;
1673 struct hlist_node
*safe
;
1674 struct batadv_tt_orig_list_entry
*orig_entry
;
1676 spin_lock_bh(&tt_global_entry
->list_lock
);
1677 head
= &tt_global_entry
->orig_list
;
1678 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
)
1679 batadv_tt_global_del_orig_entry(tt_global_entry
, orig_entry
);
1680 spin_unlock_bh(&tt_global_entry
->list_lock
);
1684 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1685 * @bat_priv: the bat priv with all the soft interface information
1686 * @tt_global_entry: the global entry to remove the orig_node from
1687 * @orig_node: the originator announcing the client
1688 * @message: message to append to the log on deletion
1690 * Remove the given orig_node and its according orig_entry from the given
1694 batadv_tt_global_del_orig_node(struct batadv_priv
*bat_priv
,
1695 struct batadv_tt_global_entry
*tt_global_entry
,
1696 struct batadv_orig_node
*orig_node
,
1697 const char *message
)
1699 struct hlist_head
*head
;
1700 struct hlist_node
*safe
;
1701 struct batadv_tt_orig_list_entry
*orig_entry
;
1704 spin_lock_bh(&tt_global_entry
->list_lock
);
1705 head
= &tt_global_entry
->orig_list
;
1706 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
) {
1707 if (orig_entry
->orig_node
== orig_node
) {
1708 vid
= tt_global_entry
->common
.vid
;
1709 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1710 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1712 tt_global_entry
->common
.addr
,
1713 BATADV_PRINT_VID(vid
), message
);
1714 batadv_tt_global_del_orig_entry(tt_global_entry
,
1718 spin_unlock_bh(&tt_global_entry
->list_lock
);
1721 /* If the client is to be deleted, we check if it is the last origantor entry
1722 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1723 * timer, otherwise we simply remove the originator scheduled for deletion.
1726 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
1727 struct batadv_tt_global_entry
*tt_global_entry
,
1728 struct batadv_orig_node
*orig_node
,
1729 const char *message
)
1731 bool last_entry
= true;
1732 struct hlist_head
*head
;
1733 struct batadv_tt_orig_list_entry
*orig_entry
;
1735 /* no local entry exists, case 1:
1736 * Check if this is the last one or if other entries exist.
1740 head
= &tt_global_entry
->orig_list
;
1741 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1742 if (orig_entry
->orig_node
!= orig_node
) {
1750 /* its the last one, mark for roaming. */
1751 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1752 tt_global_entry
->roam_at
= jiffies
;
1754 /* there is another entry, we can simply delete this
1755 * one and can still use the other one.
1757 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1758 orig_node
, message
);
1762 * batadv_tt_global_del - remove a client from the global table
1763 * @bat_priv: the bat priv with all the soft interface information
1764 * @orig_node: an originator serving this client
1765 * @addr: the mac address of the client
1766 * @vid: VLAN identifier
1767 * @message: a message explaining the reason for deleting the client to print
1768 * for debugging purpose
1769 * @roaming: true if the deletion has been triggered by a roaming event
1771 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
1772 struct batadv_orig_node
*orig_node
,
1773 const unsigned char *addr
, unsigned short vid
,
1774 const char *message
, bool roaming
)
1776 struct batadv_tt_global_entry
*tt_global_entry
;
1777 struct batadv_tt_local_entry
*local_entry
= NULL
;
1779 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
1780 if (!tt_global_entry
)
1784 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1785 orig_node
, message
);
1787 if (hlist_empty(&tt_global_entry
->orig_list
))
1788 batadv_tt_global_free(bat_priv
, tt_global_entry
,
1794 /* if we are deleting a global entry due to a roam
1795 * event, there are two possibilities:
1796 * 1) the client roamed from node A to node B => if there
1797 * is only one originator left for this client, we mark
1798 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1799 * wait for node B to claim it. In case of timeout
1800 * the entry is purged.
1802 * If there are other originators left, we directly delete
1804 * 2) the client roamed to us => we can directly delete
1805 * the global entry, since it is useless now.
1807 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1808 tt_global_entry
->common
.addr
,
1811 /* local entry exists, case 2: client roamed to us. */
1812 batadv_tt_global_del_orig_list(tt_global_entry
);
1813 batadv_tt_global_free(bat_priv
, tt_global_entry
, message
);
1815 /* no local entry exists, case 1: check for roaming */
1816 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1817 orig_node
, message
);
1820 if (tt_global_entry
)
1821 batadv_tt_global_entry_free_ref(tt_global_entry
);
1823 batadv_tt_local_entry_free_ref(local_entry
);
1827 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1828 * given originator matching the provided vid
1829 * @bat_priv: the bat priv with all the soft interface information
1830 * @orig_node: the originator owning the entries to remove
1831 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1833 * @message: debug message to print as "reason"
1835 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1836 struct batadv_orig_node
*orig_node
,
1838 const char *message
)
1840 struct batadv_tt_global_entry
*tt_global
;
1841 struct batadv_tt_common_entry
*tt_common_entry
;
1843 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1844 struct hlist_node
*safe
;
1845 struct hlist_head
*head
;
1846 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1852 for (i
= 0; i
< hash
->size
; i
++) {
1853 head
= &hash
->table
[i
];
1854 list_lock
= &hash
->list_locks
[i
];
1856 spin_lock_bh(list_lock
);
1857 hlist_for_each_entry_safe(tt_common_entry
, safe
,
1859 /* remove only matching entries */
1860 if (match_vid
>= 0 && tt_common_entry
->vid
!= match_vid
)
1863 tt_global
= container_of(tt_common_entry
,
1864 struct batadv_tt_global_entry
,
1867 batadv_tt_global_del_orig_node(bat_priv
, tt_global
,
1868 orig_node
, message
);
1870 if (hlist_empty(&tt_global
->orig_list
)) {
1871 vid
= tt_global
->common
.vid
;
1872 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1873 "Deleting global tt entry %pM (vid: %d): %s\n",
1874 tt_global
->common
.addr
,
1875 BATADV_PRINT_VID(vid
), message
);
1876 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1877 batadv_tt_global_entry_free_ref(tt_global
);
1880 spin_unlock_bh(list_lock
);
1882 orig_node
->capa_initialized
&= ~BATADV_ORIG_CAPA_HAS_TT
;
1885 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1889 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1890 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1892 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1893 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1895 *msg
= "Roaming timeout\n";
1898 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1899 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1901 *msg
= "Temporary client timeout\n";
1907 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1909 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1910 struct hlist_head
*head
;
1911 struct hlist_node
*node_tmp
;
1912 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1915 struct batadv_tt_common_entry
*tt_common
;
1916 struct batadv_tt_global_entry
*tt_global
;
1918 for (i
= 0; i
< hash
->size
; i
++) {
1919 head
= &hash
->table
[i
];
1920 list_lock
= &hash
->list_locks
[i
];
1922 spin_lock_bh(list_lock
);
1923 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
1925 tt_global
= container_of(tt_common
,
1926 struct batadv_tt_global_entry
,
1929 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1932 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1933 "Deleting global tt entry %pM (vid: %d): %s\n",
1934 tt_global
->common
.addr
,
1935 BATADV_PRINT_VID(tt_global
->common
.vid
),
1938 hlist_del_rcu(&tt_common
->hash_entry
);
1940 batadv_tt_global_entry_free_ref(tt_global
);
1942 spin_unlock_bh(list_lock
);
1946 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
1948 struct batadv_hashtable
*hash
;
1949 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1950 struct batadv_tt_common_entry
*tt_common_entry
;
1951 struct batadv_tt_global_entry
*tt_global
;
1952 struct hlist_node
*node_tmp
;
1953 struct hlist_head
*head
;
1956 if (!bat_priv
->tt
.global_hash
)
1959 hash
= bat_priv
->tt
.global_hash
;
1961 for (i
= 0; i
< hash
->size
; i
++) {
1962 head
= &hash
->table
[i
];
1963 list_lock
= &hash
->list_locks
[i
];
1965 spin_lock_bh(list_lock
);
1966 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1968 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1969 tt_global
= container_of(tt_common_entry
,
1970 struct batadv_tt_global_entry
,
1972 batadv_tt_global_entry_free_ref(tt_global
);
1974 spin_unlock_bh(list_lock
);
1977 batadv_hash_destroy(hash
);
1979 bat_priv
->tt
.global_hash
= NULL
;
1983 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
1984 struct batadv_tt_global_entry
*tt_global_entry
)
1988 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
1989 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
1992 /* check if the two clients are marked as isolated */
1993 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
&&
1994 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
)
2001 * batadv_transtable_search - get the mesh destination for a given client
2002 * @bat_priv: the bat priv with all the soft interface information
2003 * @src: mac address of the source client
2004 * @addr: mac address of the destination client
2005 * @vid: VLAN identifier
2007 * Returns a pointer to the originator that was selected as destination in the
2008 * mesh for contacting the client 'addr', NULL otherwise.
2009 * In case of multiple originators serving the same client, the function returns
2010 * the best one (best in terms of metric towards the destination node).
2012 * If the two clients are AP isolated the function returns NULL.
2014 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
2016 const uint8_t *addr
,
2019 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2020 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2021 struct batadv_orig_node
*orig_node
= NULL
;
2022 struct batadv_tt_orig_list_entry
*best_entry
;
2024 if (src
&& batadv_vlan_ap_isola_get(bat_priv
, vid
)) {
2025 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
, vid
);
2026 if (!tt_local_entry
||
2027 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
))
2031 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
2032 if (!tt_global_entry
)
2035 /* check whether the clients should not communicate due to AP
2038 if (tt_local_entry
&&
2039 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2043 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
2044 /* found anything? */
2046 orig_node
= best_entry
->orig_node
;
2047 if (orig_node
&& !atomic_inc_not_zero(&orig_node
->refcount
))
2052 if (tt_global_entry
)
2053 batadv_tt_global_entry_free_ref(tt_global_entry
);
2055 batadv_tt_local_entry_free_ref(tt_local_entry
);
2061 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2062 * to the given orig_node
2063 * @bat_priv: the bat priv with all the soft interface information
2064 * @orig_node: originator for which the CRC should be computed
2065 * @vid: VLAN identifier for which the CRC32 has to be computed
2067 * This function computes the checksum for the global table corresponding to a
2068 * specific originator. In particular, the checksum is computed as follows: For
2069 * each client connected to the originator the CRC32C of the MAC address and the
2070 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2073 * The idea behind is that CRC32C should be used as much as possible in order to
2074 * produce a unique hash of the table, but since the order which is used to feed
2075 * the CRC32C function affects the result and since every node in the network
2076 * probably sorts the clients differently, the hash function cannot be directly
2077 * computed over the entire table. Hence the CRC32C is used only on
2078 * the single client entry, while all the results are then xor'ed together
2079 * because the XOR operation can combine them all while trying to reduce the
2080 * noise as much as possible.
2082 * Returns the checksum of the global table of a given originator.
2084 static uint32_t batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
2085 struct batadv_orig_node
*orig_node
,
2088 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2089 struct batadv_tt_common_entry
*tt_common
;
2090 struct batadv_tt_global_entry
*tt_global
;
2091 struct hlist_head
*head
;
2092 uint32_t i
, crc_tmp
, crc
= 0;
2096 for (i
= 0; i
< hash
->size
; i
++) {
2097 head
= &hash
->table
[i
];
2100 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2101 tt_global
= container_of(tt_common
,
2102 struct batadv_tt_global_entry
,
2104 /* compute the CRC only for entries belonging to the
2105 * VLAN identified by the vid passed as parameter
2107 if (tt_common
->vid
!= vid
)
2110 /* Roaming clients are in the global table for
2111 * consistency only. They don't have to be
2112 * taken into account while computing the
2115 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
2117 /* Temporary clients have not been announced yet, so
2118 * they have to be skipped while computing the global
2121 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
2124 /* find out if this global entry is announced by this
2127 if (!batadv_tt_global_entry_has_orig(tt_global
,
2131 /* use network order to read the VID: this ensures that
2132 * every node reads the bytes in the same order.
2134 tmp_vid
= htons(tt_common
->vid
);
2135 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2137 /* compute the CRC on flags that have to be kept in sync
2140 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2141 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2143 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2152 * batadv_tt_local_crc - calculates the checksum of the local table
2153 * @bat_priv: the bat priv with all the soft interface information
2154 * @vid: VLAN identifier for which the CRC32 has to be computed
2156 * For details about the computation, please refer to the documentation for
2157 * batadv_tt_global_crc().
2159 * Returns the checksum of the local table
2161 static uint32_t batadv_tt_local_crc(struct batadv_priv
*bat_priv
,
2164 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2165 struct batadv_tt_common_entry
*tt_common
;
2166 struct hlist_head
*head
;
2167 uint32_t i
, crc_tmp
, crc
= 0;
2171 for (i
= 0; i
< hash
->size
; i
++) {
2172 head
= &hash
->table
[i
];
2175 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2176 /* compute the CRC only for entries belonging to the
2177 * VLAN identified by vid
2179 if (tt_common
->vid
!= vid
)
2182 /* not yet committed clients have not to be taken into
2183 * account while computing the CRC
2185 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
2188 /* use network order to read the VID: this ensures that
2189 * every node reads the bytes in the same order.
2191 tmp_vid
= htons(tt_common
->vid
);
2192 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2194 /* compute the CRC on flags that have to be kept in sync
2197 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2198 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2200 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2208 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
2210 struct batadv_tt_req_node
*node
, *safe
;
2212 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2214 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2215 list_del(&node
->list
);
2219 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2222 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
2223 struct batadv_orig_node
*orig_node
,
2224 const void *tt_buff
,
2225 uint16_t tt_buff_len
)
2227 /* Replace the old buffer only if I received something in the
2228 * last OGM (the OGM could carry no changes)
2230 spin_lock_bh(&orig_node
->tt_buff_lock
);
2231 if (tt_buff_len
> 0) {
2232 kfree(orig_node
->tt_buff
);
2233 orig_node
->tt_buff_len
= 0;
2234 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
2235 if (orig_node
->tt_buff
) {
2236 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
2237 orig_node
->tt_buff_len
= tt_buff_len
;
2240 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2243 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
2245 struct batadv_tt_req_node
*node
, *safe
;
2247 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2248 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2249 if (batadv_has_timed_out(node
->issued_at
,
2250 BATADV_TT_REQUEST_TIMEOUT
)) {
2251 list_del(&node
->list
);
2255 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2258 /* returns the pointer to the new tt_req_node struct if no request
2259 * has already been issued for this orig_node, NULL otherwise
2261 static struct batadv_tt_req_node
*
2262 batadv_new_tt_req_node(struct batadv_priv
*bat_priv
,
2263 struct batadv_orig_node
*orig_node
)
2265 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
2267 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2268 list_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
2269 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
2270 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
2271 BATADV_TT_REQUEST_TIMEOUT
))
2275 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
2279 ether_addr_copy(tt_req_node
->addr
, orig_node
->orig
);
2280 tt_req_node
->issued_at
= jiffies
;
2282 list_add(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
2284 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2289 * batadv_tt_local_valid - verify that given tt entry is a valid one
2290 * @entry_ptr: to be checked local tt entry
2291 * @data_ptr: not used but definition required to satisfy the callback prototype
2293 * Returns 1 if the entry is a valid, 0 otherwise.
2295 static int batadv_tt_local_valid(const void *entry_ptr
, const void *data_ptr
)
2297 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2299 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
2304 static int batadv_tt_global_valid(const void *entry_ptr
,
2305 const void *data_ptr
)
2307 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2308 const struct batadv_tt_global_entry
*tt_global_entry
;
2309 const struct batadv_orig_node
*orig_node
= data_ptr
;
2311 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
2312 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
2315 tt_global_entry
= container_of(tt_common_entry
,
2316 struct batadv_tt_global_entry
,
2319 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
2323 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2325 * @bat_priv: the bat priv with all the soft interface information
2326 * @hash: hash table containing the tt entries
2327 * @tt_len: expected tvlv tt data buffer length in number of bytes
2328 * @tvlv_buff: pointer to the buffer to fill with the TT data
2329 * @valid_cb: function to filter tt change entries
2330 * @cb_data: data passed to the filter function as argument
2332 static void batadv_tt_tvlv_generate(struct batadv_priv
*bat_priv
,
2333 struct batadv_hashtable
*hash
,
2334 void *tvlv_buff
, uint16_t tt_len
,
2335 int (*valid_cb
)(const void *, const void *),
2338 struct batadv_tt_common_entry
*tt_common_entry
;
2339 struct batadv_tvlv_tt_change
*tt_change
;
2340 struct hlist_head
*head
;
2341 uint16_t tt_tot
, tt_num_entries
= 0;
2344 tt_tot
= batadv_tt_entries(tt_len
);
2345 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_buff
;
2348 for (i
= 0; i
< hash
->size
; i
++) {
2349 head
= &hash
->table
[i
];
2351 hlist_for_each_entry_rcu(tt_common_entry
,
2353 if (tt_tot
== tt_num_entries
)
2356 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
2359 ether_addr_copy(tt_change
->addr
, tt_common_entry
->addr
);
2360 tt_change
->flags
= tt_common_entry
->flags
;
2361 tt_change
->vid
= htons(tt_common_entry
->vid
);
2362 memset(tt_change
->reserved
, 0,
2363 sizeof(tt_change
->reserved
));
2373 * batadv_tt_global_check_crc - check if all the CRCs are correct
2374 * @orig_node: originator for which the CRCs have to be checked
2375 * @tt_vlan: pointer to the first tvlv VLAN entry
2376 * @num_vlan: number of tvlv VLAN entries
2377 * @create: if true, create VLAN objects if not found
2379 * Return true if all the received CRCs match the locally stored ones, false
2382 static bool batadv_tt_global_check_crc(struct batadv_orig_node
*orig_node
,
2383 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2386 struct batadv_tvlv_tt_vlan_data
*tt_vlan_tmp
;
2387 struct batadv_orig_node_vlan
*vlan
;
2391 /* check if each received CRC matches the locally stored one */
2392 for (i
= 0; i
< num_vlan
; i
++) {
2393 tt_vlan_tmp
= tt_vlan
+ i
;
2395 /* if orig_node is a backbone node for this VLAN, don't check
2396 * the CRC as we ignore all the global entries over it
2398 if (batadv_bla_is_backbone_gw_orig(orig_node
->bat_priv
,
2400 ntohs(tt_vlan_tmp
->vid
)))
2403 vlan
= batadv_orig_node_vlan_get(orig_node
,
2404 ntohs(tt_vlan_tmp
->vid
));
2409 batadv_orig_node_vlan_free_ref(vlan
);
2411 if (crc
!= ntohl(tt_vlan_tmp
->crc
))
2419 * batadv_tt_local_update_crc - update all the local CRCs
2420 * @bat_priv: the bat priv with all the soft interface information
2422 static void batadv_tt_local_update_crc(struct batadv_priv
*bat_priv
)
2424 struct batadv_softif_vlan
*vlan
;
2426 /* recompute the global CRC for each VLAN */
2428 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
2429 vlan
->tt
.crc
= batadv_tt_local_crc(bat_priv
, vlan
->vid
);
2435 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2436 * @bat_priv: the bat priv with all the soft interface information
2437 * @orig_node: the orig_node for which the CRCs have to be updated
2439 static void batadv_tt_global_update_crc(struct batadv_priv
*bat_priv
,
2440 struct batadv_orig_node
*orig_node
)
2442 struct batadv_orig_node_vlan
*vlan
;
2445 /* recompute the global CRC for each VLAN */
2447 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
2448 /* if orig_node is a backbone node for this VLAN, don't compute
2449 * the CRC as we ignore all the global entries over it
2451 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
,
2455 crc
= batadv_tt_global_crc(bat_priv
, orig_node
, vlan
->vid
);
2462 * batadv_send_tt_request - send a TT Request message to a given node
2463 * @bat_priv: the bat priv with all the soft interface information
2464 * @dst_orig_node: the destination of the message
2465 * @ttvn: the version number that the source of the message is looking for
2466 * @tt_vlan: pointer to the first tvlv VLAN object to request
2467 * @num_vlan: number of tvlv VLAN entries
2468 * @full_table: ask for the entire translation table if true, while only for the
2469 * last TT diff otherwise
2471 static int batadv_send_tt_request(struct batadv_priv
*bat_priv
,
2472 struct batadv_orig_node
*dst_orig_node
,
2474 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2475 uint16_t num_vlan
, bool full_table
)
2477 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2478 struct batadv_tt_req_node
*tt_req_node
= NULL
;
2479 struct batadv_tvlv_tt_vlan_data
*tt_vlan_req
;
2480 struct batadv_hard_iface
*primary_if
;
2484 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2488 /* The new tt_req will be issued only if I'm not waiting for a
2489 * reply from the same orig_node yet
2491 tt_req_node
= batadv_new_tt_req_node(bat_priv
, dst_orig_node
);
2495 size
= sizeof(*tvlv_tt_data
) + sizeof(*tt_vlan_req
) * num_vlan
;
2496 tvlv_tt_data
= kzalloc(size
, GFP_ATOMIC
);
2500 tvlv_tt_data
->flags
= BATADV_TT_REQUEST
;
2501 tvlv_tt_data
->ttvn
= ttvn
;
2502 tvlv_tt_data
->num_vlan
= htons(num_vlan
);
2504 /* send all the CRCs within the request. This is needed by intermediate
2505 * nodes to ensure they have the correct table before replying
2507 tt_vlan_req
= (struct batadv_tvlv_tt_vlan_data
*)(tvlv_tt_data
+ 1);
2508 for (i
= 0; i
< num_vlan
; i
++) {
2509 tt_vlan_req
->vid
= tt_vlan
->vid
;
2510 tt_vlan_req
->crc
= tt_vlan
->crc
;
2517 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2519 batadv_dbg(BATADV_DBG_TT
, bat_priv
, "Sending TT_REQUEST to %pM [%c]\n",
2520 dst_orig_node
->orig
, full_table
? 'F' : '.');
2522 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
2523 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2524 dst_orig_node
->orig
, BATADV_TVLV_TT
, 1,
2525 tvlv_tt_data
, size
);
2530 batadv_hardif_free_ref(primary_if
);
2531 if (ret
&& tt_req_node
) {
2532 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2533 list_del(&tt_req_node
->list
);
2534 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2537 kfree(tvlv_tt_data
);
2542 * batadv_send_other_tt_response - send reply to tt request concerning another
2543 * node's translation table
2544 * @bat_priv: the bat priv with all the soft interface information
2545 * @tt_data: tt data containing the tt request information
2546 * @req_src: mac address of tt request sender
2547 * @req_dst: mac address of tt request recipient
2549 * Returns true if tt request reply was sent, false otherwise.
2551 static bool batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
2552 struct batadv_tvlv_tt_data
*tt_data
,
2553 uint8_t *req_src
, uint8_t *req_dst
)
2555 struct batadv_orig_node
*req_dst_orig_node
;
2556 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
2557 struct batadv_tvlv_tt_change
*tt_change
;
2558 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2559 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
2560 bool ret
= false, full_table
;
2561 uint8_t orig_ttvn
, req_ttvn
;
2565 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2566 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2567 req_src
, tt_data
->ttvn
, req_dst
,
2568 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2570 /* Let's get the orig node of the REAL destination */
2571 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_dst
);
2572 if (!req_dst_orig_node
)
2575 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2576 if (!res_dst_orig_node
)
2579 orig_ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
2580 req_ttvn
= tt_data
->ttvn
;
2582 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
2583 /* this node doesn't have the requested data */
2584 if (orig_ttvn
!= req_ttvn
||
2585 !batadv_tt_global_check_crc(req_dst_orig_node
, tt_vlan
,
2586 ntohs(tt_data
->num_vlan
)))
2589 /* If the full table has been explicitly requested */
2590 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
||
2591 !req_dst_orig_node
->tt_buff
)
2596 /* TT fragmentation hasn't been implemented yet, so send as many
2597 * TT entries fit a single packet as possible only
2600 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
2601 tt_len
= req_dst_orig_node
->tt_buff_len
;
2603 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2610 /* Copy the last orig_node's OGM buffer */
2611 memcpy(tt_change
, req_dst_orig_node
->tt_buff
,
2612 req_dst_orig_node
->tt_buff_len
);
2613 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2615 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2616 * in the initial part
2619 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2626 /* fill the rest of the tvlv with the real TT entries */
2627 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.global_hash
,
2629 batadv_tt_global_valid
,
2633 /* Don't send the response, if larger than fragmented packet. */
2634 tt_len
= sizeof(struct batadv_unicast_tvlv_packet
) + tvlv_len
;
2635 if (tt_len
> atomic_read(&bat_priv
->packet_size_max
)) {
2636 net_ratelimited_function(batadv_info
, bat_priv
->soft_iface
,
2637 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2638 res_dst_orig_node
->orig
);
2642 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2643 tvlv_tt_data
->ttvn
= req_ttvn
;
2646 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2648 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2649 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2650 res_dst_orig_node
->orig
, req_dst_orig_node
->orig
,
2651 full_table
? 'F' : '.', req_ttvn
);
2653 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2655 batadv_tvlv_unicast_send(bat_priv
, req_dst_orig_node
->orig
,
2656 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2663 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2666 if (res_dst_orig_node
)
2667 batadv_orig_node_free_ref(res_dst_orig_node
);
2668 if (req_dst_orig_node
)
2669 batadv_orig_node_free_ref(req_dst_orig_node
);
2670 kfree(tvlv_tt_data
);
2675 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2677 * @bat_priv: the bat priv with all the soft interface information
2678 * @tt_data: tt data containing the tt request information
2679 * @req_src: mac address of tt request sender
2681 * Returns true if tt request reply was sent, false otherwise.
2683 static bool batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
2684 struct batadv_tvlv_tt_data
*tt_data
,
2687 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2688 struct batadv_hard_iface
*primary_if
= NULL
;
2689 struct batadv_tvlv_tt_change
*tt_change
;
2690 struct batadv_orig_node
*orig_node
;
2691 uint8_t my_ttvn
, req_ttvn
;
2696 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2697 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2698 req_src
, tt_data
->ttvn
,
2699 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2701 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
2703 my_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
2704 req_ttvn
= tt_data
->ttvn
;
2706 orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2710 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2714 /* If the full table has been explicitly requested or the gap
2715 * is too big send the whole local translation table
2717 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
2718 !bat_priv
->tt
.last_changeset
)
2723 /* TT fragmentation hasn't been implemented yet, so send as many
2724 * TT entries fit a single packet as possible only
2727 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
2729 tt_len
= bat_priv
->tt
.last_changeset_len
;
2730 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2737 /* Copy the last orig_node's OGM buffer */
2738 memcpy(tt_change
, bat_priv
->tt
.last_changeset
,
2739 bat_priv
->tt
.last_changeset_len
);
2740 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2742 req_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
2744 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2745 * in the initial part
2748 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2755 /* fill the rest of the tvlv with the real TT entries */
2756 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.local_hash
,
2758 batadv_tt_local_valid
, NULL
);
2761 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2762 tvlv_tt_data
->ttvn
= req_ttvn
;
2765 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2767 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2768 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2769 orig_node
->orig
, full_table
? 'F' : '.', req_ttvn
);
2771 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2773 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2774 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2780 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2782 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
2784 batadv_orig_node_free_ref(orig_node
);
2786 batadv_hardif_free_ref(primary_if
);
2787 kfree(tvlv_tt_data
);
2788 /* The packet was for this host, so it doesn't need to be re-routed */
2793 * batadv_send_tt_response - send reply to tt request
2794 * @bat_priv: the bat priv with all the soft interface information
2795 * @tt_data: tt data containing the tt request information
2796 * @req_src: mac address of tt request sender
2797 * @req_dst: mac address of tt request recipient
2799 * Returns true if tt request reply was sent, false otherwise.
2801 static bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
2802 struct batadv_tvlv_tt_data
*tt_data
,
2803 uint8_t *req_src
, uint8_t *req_dst
)
2805 if (batadv_is_my_mac(bat_priv
, req_dst
))
2806 return batadv_send_my_tt_response(bat_priv
, tt_data
, req_src
);
2807 return batadv_send_other_tt_response(bat_priv
, tt_data
, req_src
,
2811 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2812 struct batadv_orig_node
*orig_node
,
2813 struct batadv_tvlv_tt_change
*tt_change
,
2814 uint16_t tt_num_changes
, uint8_t ttvn
)
2819 for (i
= 0; i
< tt_num_changes
; i
++) {
2820 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
2821 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
2822 batadv_tt_global_del(bat_priv
, orig_node
,
2823 (tt_change
+ i
)->addr
,
2824 ntohs((tt_change
+ i
)->vid
),
2825 "tt removed by changes",
2828 if (!batadv_tt_global_add(bat_priv
, orig_node
,
2829 (tt_change
+ i
)->addr
,
2830 ntohs((tt_change
+ i
)->vid
),
2831 (tt_change
+ i
)->flags
, ttvn
))
2832 /* In case of problem while storing a
2833 * global_entry, we stop the updating
2834 * procedure without committing the
2835 * ttvn change. This will avoid to send
2836 * corrupted data on tt_request
2841 orig_node
->capa_initialized
|= BATADV_ORIG_CAPA_HAS_TT
;
2844 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
2845 struct batadv_tvlv_tt_change
*tt_change
,
2846 uint8_t ttvn
, uint8_t *resp_src
,
2847 uint16_t num_entries
)
2849 struct batadv_orig_node
*orig_node
;
2851 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2855 /* Purge the old table first.. */
2856 batadv_tt_global_del_orig(bat_priv
, orig_node
, -1,
2857 "Received full table");
2859 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
, num_entries
,
2862 spin_lock_bh(&orig_node
->tt_buff_lock
);
2863 kfree(orig_node
->tt_buff
);
2864 orig_node
->tt_buff_len
= 0;
2865 orig_node
->tt_buff
= NULL
;
2866 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2868 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2872 batadv_orig_node_free_ref(orig_node
);
2875 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2876 struct batadv_orig_node
*orig_node
,
2877 uint16_t tt_num_changes
, uint8_t ttvn
,
2878 struct batadv_tvlv_tt_change
*tt_change
)
2880 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
2881 tt_num_changes
, ttvn
);
2883 batadv_tt_save_orig_buffer(bat_priv
, orig_node
, tt_change
,
2884 batadv_tt_len(tt_num_changes
));
2885 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2889 * batadv_is_my_client - check if a client is served by the local node
2890 * @bat_priv: the bat priv with all the soft interface information
2891 * @addr: the mac address of the client to check
2892 * @vid: VLAN identifier
2894 * Returns true if the client is served by this node, false otherwise.
2896 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
2899 struct batadv_tt_local_entry
*tt_local_entry
;
2902 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
2903 if (!tt_local_entry
)
2905 /* Check if the client has been logically deleted (but is kept for
2906 * consistency purpose)
2908 if ((tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
) ||
2909 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
2914 batadv_tt_local_entry_free_ref(tt_local_entry
);
2919 * batadv_handle_tt_response - process incoming tt reply
2920 * @bat_priv: the bat priv with all the soft interface information
2921 * @tt_data: tt data containing the tt request information
2922 * @resp_src: mac address of tt reply sender
2923 * @num_entries: number of tt change entries appended to the tt data
2925 static void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
2926 struct batadv_tvlv_tt_data
*tt_data
,
2927 uint8_t *resp_src
, uint16_t num_entries
)
2929 struct batadv_tt_req_node
*node
, *safe
;
2930 struct batadv_orig_node
*orig_node
= NULL
;
2931 struct batadv_tvlv_tt_change
*tt_change
;
2932 uint8_t *tvlv_ptr
= (uint8_t *)tt_data
;
2933 uint16_t change_offset
;
2935 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2936 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2937 resp_src
, tt_data
->ttvn
, num_entries
,
2938 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
2940 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2944 spin_lock_bh(&orig_node
->tt_lock
);
2946 change_offset
= sizeof(struct batadv_tvlv_tt_vlan_data
);
2947 change_offset
*= ntohs(tt_data
->num_vlan
);
2948 change_offset
+= sizeof(*tt_data
);
2949 tvlv_ptr
+= change_offset
;
2951 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_ptr
;
2952 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
) {
2953 batadv_tt_fill_gtable(bat_priv
, tt_change
, tt_data
->ttvn
,
2954 resp_src
, num_entries
);
2956 batadv_tt_update_changes(bat_priv
, orig_node
, num_entries
,
2957 tt_data
->ttvn
, tt_change
);
2960 /* Recalculate the CRC for this orig_node and store it */
2961 batadv_tt_global_update_crc(bat_priv
, orig_node
);
2963 spin_unlock_bh(&orig_node
->tt_lock
);
2965 /* Delete the tt_req_node from pending tt_requests list */
2966 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2967 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2968 if (!batadv_compare_eth(node
->addr
, resp_src
))
2970 list_del(&node
->list
);
2974 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2977 batadv_orig_node_free_ref(orig_node
);
2980 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
2982 struct batadv_tt_roam_node
*node
, *safe
;
2984 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2986 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2987 list_del(&node
->list
);
2991 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2994 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
2996 struct batadv_tt_roam_node
*node
, *safe
;
2998 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2999 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
3000 if (!batadv_has_timed_out(node
->first_time
,
3001 BATADV_ROAMING_MAX_TIME
))
3004 list_del(&node
->list
);
3007 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3010 /* This function checks whether the client already reached the
3011 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3014 * returns true if the ROAMING_ADV can be sent, false otherwise
3016 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
,
3019 struct batadv_tt_roam_node
*tt_roam_node
;
3022 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3023 /* The new tt_req will be issued only if I'm not waiting for a
3024 * reply from the same orig_node yet
3026 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
3027 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
3030 if (batadv_has_timed_out(tt_roam_node
->first_time
,
3031 BATADV_ROAMING_MAX_TIME
))
3034 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
3035 /* Sorry, you roamed too many times! */
3042 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
3046 tt_roam_node
->first_time
= jiffies
;
3047 atomic_set(&tt_roam_node
->counter
,
3048 BATADV_ROAMING_MAX_COUNT
- 1);
3049 ether_addr_copy(tt_roam_node
->addr
, client
);
3051 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
3056 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3061 * batadv_send_roam_adv - send a roaming advertisement message
3062 * @bat_priv: the bat priv with all the soft interface information
3063 * @client: mac address of the roaming client
3064 * @vid: VLAN identifier
3065 * @orig_node: message destination
3067 * Send a ROAMING_ADV message to the node which was previously serving this
3068 * client. This is done to inform the node that from now on all traffic destined
3069 * for this particular roamed client has to be forwarded to the sender of the
3072 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
3074 struct batadv_orig_node
*orig_node
)
3076 struct batadv_hard_iface
*primary_if
;
3077 struct batadv_tvlv_roam_adv tvlv_roam
;
3079 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3083 /* before going on we have to check whether the client has
3084 * already roamed to us too many times
3086 if (!batadv_tt_check_roam_count(bat_priv
, client
))
3089 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3090 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3091 orig_node
->orig
, client
, BATADV_PRINT_VID(vid
));
3093 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
3095 memcpy(tvlv_roam
.client
, client
, sizeof(tvlv_roam
.client
));
3096 tvlv_roam
.vid
= htons(vid
);
3098 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3099 orig_node
->orig
, BATADV_TVLV_ROAM
, 1,
3100 &tvlv_roam
, sizeof(tvlv_roam
));
3104 batadv_hardif_free_ref(primary_if
);
3107 static void batadv_tt_purge(struct work_struct
*work
)
3109 struct delayed_work
*delayed_work
;
3110 struct batadv_priv_tt
*priv_tt
;
3111 struct batadv_priv
*bat_priv
;
3113 delayed_work
= container_of(work
, struct delayed_work
, work
);
3114 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
3115 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
3117 batadv_tt_local_purge(bat_priv
, BATADV_TT_LOCAL_TIMEOUT
);
3118 batadv_tt_global_purge(bat_priv
);
3119 batadv_tt_req_purge(bat_priv
);
3120 batadv_tt_roam_purge(bat_priv
);
3122 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3123 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3126 void batadv_tt_free(struct batadv_priv
*bat_priv
)
3128 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3129 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3131 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
3133 batadv_tt_local_table_free(bat_priv
);
3134 batadv_tt_global_table_free(bat_priv
);
3135 batadv_tt_req_list_free(bat_priv
);
3136 batadv_tt_changes_list_free(bat_priv
);
3137 batadv_tt_roam_list_free(bat_priv
);
3139 kfree(bat_priv
->tt
.last_changeset
);
3143 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3144 * table and possibly count them in the TT size
3145 * @bat_priv: the bat priv with all the soft interface information
3146 * @flags: the flag to switch
3147 * @enable: whether to set or unset the flag
3148 * @count: whether to increase the TT size by the number of changed entries
3150 static void batadv_tt_local_set_flags(struct batadv_priv
*bat_priv
,
3151 uint16_t flags
, bool enable
, bool count
)
3153 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3154 struct batadv_tt_common_entry
*tt_common_entry
;
3155 uint16_t changed_num
= 0;
3156 struct hlist_head
*head
;
3162 for (i
= 0; i
< hash
->size
; i
++) {
3163 head
= &hash
->table
[i
];
3166 hlist_for_each_entry_rcu(tt_common_entry
,
3169 if ((tt_common_entry
->flags
& flags
) == flags
)
3171 tt_common_entry
->flags
|= flags
;
3173 if (!(tt_common_entry
->flags
& flags
))
3175 tt_common_entry
->flags
&= ~flags
;
3182 batadv_tt_local_size_inc(bat_priv
,
3183 tt_common_entry
->vid
);
3189 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3190 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
3192 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3193 struct batadv_tt_common_entry
*tt_common
;
3194 struct batadv_tt_local_entry
*tt_local
;
3195 struct batadv_softif_vlan
*vlan
;
3196 struct hlist_node
*node_tmp
;
3197 struct hlist_head
*head
;
3198 spinlock_t
*list_lock
; /* protects write access to the hash lists */
3204 for (i
= 0; i
< hash
->size
; i
++) {
3205 head
= &hash
->table
[i
];
3206 list_lock
= &hash
->list_locks
[i
];
3208 spin_lock_bh(list_lock
);
3209 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
3211 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
3214 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3215 "Deleting local tt entry (%pM, vid: %d): pending\n",
3217 BATADV_PRINT_VID(tt_common
->vid
));
3219 batadv_tt_local_size_dec(bat_priv
, tt_common
->vid
);
3220 hlist_del_rcu(&tt_common
->hash_entry
);
3221 tt_local
= container_of(tt_common
,
3222 struct batadv_tt_local_entry
,
3225 /* decrease the reference held for this vlan */
3226 vlan
= batadv_softif_vlan_get(bat_priv
, tt_common
->vid
);
3228 batadv_softif_vlan_free_ref(vlan
);
3229 batadv_softif_vlan_free_ref(vlan
);
3232 batadv_tt_local_entry_free_ref(tt_local
);
3234 spin_unlock_bh(list_lock
);
3239 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3240 * which have been queued in the time since the last commit
3241 * @bat_priv: the bat priv with all the soft interface information
3243 * Caller must hold tt->commit_lock.
3245 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv
*bat_priv
)
3247 /* Update multicast addresses in local translation table */
3248 batadv_mcast_mla_update(bat_priv
);
3250 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1) {
3251 if (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))
3252 batadv_tt_tvlv_container_update(bat_priv
);
3256 batadv_tt_local_set_flags(bat_priv
, BATADV_TT_CLIENT_NEW
, false, true);
3258 batadv_tt_local_purge_pending_clients(bat_priv
);
3259 batadv_tt_local_update_crc(bat_priv
);
3261 /* Increment the TTVN only once per OGM interval */
3262 atomic_inc(&bat_priv
->tt
.vn
);
3263 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3264 "Local changes committed, updating to ttvn %u\n",
3265 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
3267 /* reset the sending counter */
3268 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
3269 batadv_tt_tvlv_container_update(bat_priv
);
3273 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3274 * have been queued in the time since the last commit
3275 * @bat_priv: the bat priv with all the soft interface information
3277 void batadv_tt_local_commit_changes(struct batadv_priv
*bat_priv
)
3279 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3280 batadv_tt_local_commit_changes_nolock(bat_priv
);
3281 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3284 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, uint8_t *src
,
3285 uint8_t *dst
, unsigned short vid
)
3287 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
3288 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
3289 struct batadv_softif_vlan
*vlan
;
3292 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
3293 if (!vlan
|| !atomic_read(&vlan
->ap_isolation
))
3296 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
, vid
);
3297 if (!tt_local_entry
)
3300 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
, vid
);
3301 if (!tt_global_entry
)
3304 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
3311 batadv_softif_vlan_free_ref(vlan
);
3312 if (tt_global_entry
)
3313 batadv_tt_global_entry_free_ref(tt_global_entry
);
3315 batadv_tt_local_entry_free_ref(tt_local_entry
);
3320 * batadv_tt_update_orig - update global translation table with new tt
3321 * information received via ogms
3322 * @bat_priv: the bat priv with all the soft interface information
3323 * @orig: the orig_node of the ogm
3324 * @tt_vlan: pointer to the first tvlv VLAN entry
3325 * @tt_num_vlan: number of tvlv VLAN entries
3326 * @tt_change: pointer to the first entry in the TT buffer
3327 * @tt_num_changes: number of tt changes inside the tt buffer
3328 * @ttvn: translation table version number of this changeset
3329 * @tt_crc: crc32 checksum of orig node's translation table
3331 static void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
3332 struct batadv_orig_node
*orig_node
,
3333 const void *tt_buff
, uint16_t tt_num_vlan
,
3334 struct batadv_tvlv_tt_change
*tt_change
,
3335 uint16_t tt_num_changes
, uint8_t ttvn
)
3337 uint8_t orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
3338 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3339 bool full_table
= true;
3342 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)tt_buff
;
3343 has_tt_init
= orig_node
->capa_initialized
& BATADV_ORIG_CAPA_HAS_TT
;
3345 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3346 * increased by one -> we can apply the attached changes
3348 if ((!has_tt_init
&& ttvn
== 1) || ttvn
- orig_ttvn
== 1) {
3349 /* the OGM could not contain the changes due to their size or
3350 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3352 * In this case send a tt request
3354 if (!tt_num_changes
) {
3359 spin_lock_bh(&orig_node
->tt_lock
);
3361 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
3364 /* Even if we received the precomputed crc with the OGM, we
3365 * prefer to recompute it to spot any possible inconsistency
3366 * in the global table
3368 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3370 spin_unlock_bh(&orig_node
->tt_lock
);
3372 /* The ttvn alone is not enough to guarantee consistency
3373 * because a single value could represent different states
3374 * (due to the wrap around). Thus a node has to check whether
3375 * the resulting table (after applying the changes) is still
3376 * consistent or not. E.g. a node could disconnect while its
3377 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3378 * checking the CRC value is mandatory to detect the
3381 if (!batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3385 /* if we missed more than one change or our tables are not
3386 * in sync anymore -> request fresh tt data
3388 if (!has_tt_init
|| ttvn
!= orig_ttvn
||
3389 !batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3392 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3393 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3394 orig_node
->orig
, ttvn
, orig_ttvn
,
3396 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
3397 tt_vlan
, tt_num_vlan
,
3405 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3406 * @bat_priv: the bat priv with all the soft interface information
3407 * @addr: the mac address of the client to check
3408 * @vid: VLAN identifier
3410 * Returns true if we know that the client has moved from its old originator
3411 * to another one. This entry is still kept for consistency purposes and will be
3412 * deleted later by a DEL or because of timeout
3414 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
3415 uint8_t *addr
, unsigned short vid
)
3417 struct batadv_tt_global_entry
*tt_global_entry
;
3420 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3421 if (!tt_global_entry
)
3424 ret
= tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3425 batadv_tt_global_entry_free_ref(tt_global_entry
);
3431 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3432 * @bat_priv: the bat priv with all the soft interface information
3433 * @addr: the mac address of the local client to query
3434 * @vid: VLAN identifier
3436 * Returns true if the local client is known to be roaming (it is not served by
3437 * this node anymore) or not. If yes, the client is still present in the table
3438 * to keep the latter consistent with the node TTVN
3440 bool batadv_tt_local_client_is_roaming(struct batadv_priv
*bat_priv
,
3441 uint8_t *addr
, unsigned short vid
)
3443 struct batadv_tt_local_entry
*tt_local_entry
;
3446 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
3447 if (!tt_local_entry
)
3450 ret
= tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3451 batadv_tt_local_entry_free_ref(tt_local_entry
);
3456 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
3457 struct batadv_orig_node
*orig_node
,
3458 const unsigned char *addr
,
3463 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
, vid
,
3464 BATADV_TT_CLIENT_TEMP
,
3465 atomic_read(&orig_node
->last_ttvn
)))
3468 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3469 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3470 addr
, BATADV_PRINT_VID(vid
), orig_node
->orig
);
3477 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3478 * maximum packet size that can be transported through the mesh
3479 * @soft_iface: netdev struct of the mesh interface
3481 * Remove entries older than 'timeout' and half timeout if more entries need
3484 void batadv_tt_local_resize_to_mtu(struct net_device
*soft_iface
)
3486 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
3487 int packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
3488 int table_size
, timeout
= BATADV_TT_LOCAL_TIMEOUT
/ 2;
3489 bool reduced
= false;
3491 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3494 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
3495 if (packet_size_max
>= table_size
)
3498 batadv_tt_local_purge(bat_priv
, timeout
);
3499 batadv_tt_local_purge_pending_clients(bat_priv
);
3503 net_ratelimited_function(batadv_info
, soft_iface
,
3504 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3508 /* commit these changes immediately, to avoid synchronization problem
3512 batadv_tt_local_commit_changes_nolock(bat_priv
);
3514 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3518 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3519 * @bat_priv: the bat priv with all the soft interface information
3520 * @orig: the orig_node of the ogm
3521 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3522 * @tvlv_value: tvlv buffer containing the gateway data
3523 * @tvlv_value_len: tvlv buffer length
3525 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
3526 struct batadv_orig_node
*orig
,
3527 uint8_t flags
, void *tvlv_value
,
3528 uint16_t tvlv_value_len
)
3530 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3531 struct batadv_tvlv_tt_change
*tt_change
;
3532 struct batadv_tvlv_tt_data
*tt_data
;
3533 uint16_t num_entries
, num_vlan
;
3535 if (tvlv_value_len
< sizeof(*tt_data
))
3538 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3539 tvlv_value_len
-= sizeof(*tt_data
);
3541 num_vlan
= ntohs(tt_data
->num_vlan
);
3543 if (tvlv_value_len
< sizeof(*tt_vlan
) * num_vlan
)
3546 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
3547 tt_change
= (struct batadv_tvlv_tt_change
*)(tt_vlan
+ num_vlan
);
3548 tvlv_value_len
-= sizeof(*tt_vlan
) * num_vlan
;
3550 num_entries
= batadv_tt_entries(tvlv_value_len
);
3552 batadv_tt_update_orig(bat_priv
, orig
, tt_vlan
, num_vlan
, tt_change
,
3553 num_entries
, tt_data
->ttvn
);
3557 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3559 * @bat_priv: the bat priv with all the soft interface information
3560 * @src: mac address of tt tvlv sender
3561 * @dst: mac address of tt tvlv recipient
3562 * @tvlv_value: tvlv buffer containing the tt data
3563 * @tvlv_value_len: tvlv buffer length
3565 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3568 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3569 uint8_t *src
, uint8_t *dst
,
3571 uint16_t tvlv_value_len
)
3573 struct batadv_tvlv_tt_data
*tt_data
;
3574 uint16_t tt_vlan_len
, tt_num_entries
;
3578 if (tvlv_value_len
< sizeof(*tt_data
))
3579 return NET_RX_SUCCESS
;
3581 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3582 tvlv_value_len
-= sizeof(*tt_data
);
3584 tt_vlan_len
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3585 tt_vlan_len
*= ntohs(tt_data
->num_vlan
);
3587 if (tvlv_value_len
< tt_vlan_len
)
3588 return NET_RX_SUCCESS
;
3590 tvlv_value_len
-= tt_vlan_len
;
3591 tt_num_entries
= batadv_tt_entries(tvlv_value_len
);
3593 switch (tt_data
->flags
& BATADV_TT_DATA_TYPE_MASK
) {
3594 case BATADV_TT_REQUEST
:
3595 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
3597 /* If this node cannot provide a TT response the tt_request is
3600 ret
= batadv_send_tt_response(bat_priv
, tt_data
, src
, dst
);
3602 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3607 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3608 "Routing TT_REQUEST to %pM [%c]\n",
3610 /* tvlv API will re-route the packet */
3614 case BATADV_TT_RESPONSE
:
3615 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
3617 if (batadv_is_my_mac(bat_priv
, dst
)) {
3618 batadv_handle_tt_response(bat_priv
, tt_data
,
3619 src
, tt_num_entries
);
3620 return NET_RX_SUCCESS
;
3623 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3628 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3629 "Routing TT_RESPONSE to %pM [%c]\n", dst
, tt_flag
);
3631 /* tvlv API will re-route the packet */
3635 return NET_RX_SUCCESS
;
3639 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3640 * @bat_priv: the bat priv with all the soft interface information
3641 * @src: mac address of tt tvlv sender
3642 * @dst: mac address of tt tvlv recipient
3643 * @tvlv_value: tvlv buffer containing the tt data
3644 * @tvlv_value_len: tvlv buffer length
3646 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3649 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3650 uint8_t *src
, uint8_t *dst
,
3652 uint16_t tvlv_value_len
)
3654 struct batadv_tvlv_roam_adv
*roaming_adv
;
3655 struct batadv_orig_node
*orig_node
= NULL
;
3657 /* If this node is not the intended recipient of the
3658 * roaming advertisement the packet is forwarded
3659 * (the tvlv API will re-route the packet).
3661 if (!batadv_is_my_mac(bat_priv
, dst
))
3664 if (tvlv_value_len
< sizeof(*roaming_adv
))
3667 orig_node
= batadv_orig_hash_find(bat_priv
, src
);
3671 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
3672 roaming_adv
= (struct batadv_tvlv_roam_adv
*)tvlv_value
;
3674 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3675 "Received ROAMING_ADV from %pM (client %pM)\n",
3676 src
, roaming_adv
->client
);
3678 batadv_tt_global_add(bat_priv
, orig_node
, roaming_adv
->client
,
3679 ntohs(roaming_adv
->vid
), BATADV_TT_CLIENT_ROAM
,
3680 atomic_read(&orig_node
->last_ttvn
) + 1);
3684 batadv_orig_node_free_ref(orig_node
);
3685 return NET_RX_SUCCESS
;
3689 * batadv_tt_init - initialise the translation table internals
3690 * @bat_priv: the bat priv with all the soft interface information
3692 * Return 0 on success or negative error number in case of failure.
3694 int batadv_tt_init(struct batadv_priv
*bat_priv
)
3698 /* synchronized flags must be remote */
3699 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK
& BATADV_TT_REMOTE_MASK
));
3701 ret
= batadv_tt_local_init(bat_priv
);
3705 ret
= batadv_tt_global_init(bat_priv
);
3709 batadv_tvlv_handler_register(bat_priv
, batadv_tt_tvlv_ogm_handler_v1
,
3710 batadv_tt_tvlv_unicast_handler_v1
,
3711 BATADV_TVLV_TT
, 1, BATADV_NO_FLAGS
);
3713 batadv_tvlv_handler_register(bat_priv
, NULL
,
3714 batadv_roam_tvlv_unicast_handler_v1
,
3715 BATADV_TVLV_ROAM
, 1, BATADV_NO_FLAGS
);
3717 INIT_DELAYED_WORK(&bat_priv
->tt
.work
, batadv_tt_purge
);
3718 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3719 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3725 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3726 * @bat_priv: the bat priv with all the soft interface information
3727 * @addr: the mac address of the client
3728 * @vid: the identifier of the VLAN where this client is connected
3730 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3733 bool batadv_tt_global_is_isolated(struct batadv_priv
*bat_priv
,
3734 const uint8_t *addr
, unsigned short vid
)
3736 struct batadv_tt_global_entry
*tt
;
3739 tt
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3743 ret
= tt
->common
.flags
& BATADV_TT_CLIENT_ISOLA
;
3745 batadv_tt_global_entry_free_ref(tt
);