1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2019 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
7 #include "translation-table.h"
10 #include <linux/atomic.h>
11 #include <linux/bitops.h>
12 #include <linux/build_bug.h>
13 #include <linux/byteorder/generic.h>
14 #include <linux/cache.h>
15 #include <linux/compiler.h>
16 #include <linux/crc32c.h>
17 #include <linux/errno.h>
18 #include <linux/etherdevice.h>
19 #include <linux/gfp.h>
20 #include <linux/if_ether.h>
21 #include <linux/init.h>
22 #include <linux/jhash.h>
23 #include <linux/jiffies.h>
24 #include <linux/kernel.h>
25 #include <linux/kref.h>
26 #include <linux/list.h>
27 #include <linux/lockdep.h>
28 #include <linux/net.h>
29 #include <linux/netdevice.h>
30 #include <linux/netlink.h>
31 #include <linux/rculist.h>
32 #include <linux/rcupdate.h>
33 #include <linux/seq_file.h>
34 #include <linux/skbuff.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/stddef.h>
38 #include <linux/string.h>
39 #include <linux/workqueue.h>
40 #include <net/genetlink.h>
41 #include <net/netlink.h>
43 #include <uapi/linux/batadv_packet.h>
44 #include <uapi/linux/batman_adv.h>
46 #include "bridge_loop_avoidance.h"
47 #include "hard-interface.h"
51 #include "originator.h"
52 #include "soft-interface.h"
55 static struct kmem_cache
*batadv_tl_cache __read_mostly
;
56 static struct kmem_cache
*batadv_tg_cache __read_mostly
;
57 static struct kmem_cache
*batadv_tt_orig_cache __read_mostly
;
58 static struct kmem_cache
*batadv_tt_change_cache __read_mostly
;
59 static struct kmem_cache
*batadv_tt_req_cache __read_mostly
;
60 static struct kmem_cache
*batadv_tt_roam_cache __read_mostly
;
63 static struct lock_class_key batadv_tt_local_hash_lock_class_key
;
64 static struct lock_class_key batadv_tt_global_hash_lock_class_key
;
66 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, u8
*client
,
68 struct batadv_orig_node
*orig_node
);
69 static void batadv_tt_purge(struct work_struct
*work
);
71 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
);
72 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
73 struct batadv_orig_node
*orig_node
,
74 const unsigned char *addr
,
75 unsigned short vid
, const char *message
,
79 * batadv_compare_tt() - check if two TT entries are the same
80 * @node: the list element pointer of the first TT entry
81 * @data2: pointer to the tt_common_entry of the second TT entry
83 * Compare the MAC address and the VLAN ID of the two TT entries and check if
84 * they are the same TT client.
85 * Return: true if the two TT clients are the same, false otherwise
87 static bool batadv_compare_tt(const struct hlist_node
*node
, const void *data2
)
89 const void *data1
= container_of(node
, struct batadv_tt_common_entry
,
91 const struct batadv_tt_common_entry
*tt1
= data1
;
92 const struct batadv_tt_common_entry
*tt2
= data2
;
94 return (tt1
->vid
== tt2
->vid
) && batadv_compare_eth(data1
, data2
);
98 * batadv_choose_tt() - return the index of the tt entry in the hash table
99 * @data: pointer to the tt_common_entry object to map
100 * @size: the size of the hash table
102 * Return: the hash index where the object represented by 'data' should be
105 static inline u32
batadv_choose_tt(const void *data
, u32 size
)
107 struct batadv_tt_common_entry
*tt
;
110 tt
= (struct batadv_tt_common_entry
*)data
;
111 hash
= jhash(&tt
->addr
, ETH_ALEN
, hash
);
112 hash
= jhash(&tt
->vid
, sizeof(tt
->vid
), hash
);
118 * batadv_tt_hash_find() - look for a client in the given hash table
119 * @hash: the hash table to search
120 * @addr: the mac address of the client to look for
121 * @vid: VLAN identifier
123 * Return: a pointer to the tt_common struct belonging to the searched client if
124 * found, NULL otherwise.
126 static struct batadv_tt_common_entry
*
127 batadv_tt_hash_find(struct batadv_hashtable
*hash
, const u8
*addr
,
130 struct hlist_head
*head
;
131 struct batadv_tt_common_entry to_search
, *tt
, *tt_tmp
= NULL
;
137 ether_addr_copy(to_search
.addr
, addr
);
140 index
= batadv_choose_tt(&to_search
, hash
->size
);
141 head
= &hash
->table
[index
];
144 hlist_for_each_entry_rcu(tt
, head
, hash_entry
) {
145 if (!batadv_compare_eth(tt
, addr
))
151 if (!kref_get_unless_zero(&tt
->refcount
))
163 * batadv_tt_local_hash_find() - search the local table for a given client
164 * @bat_priv: the bat priv with all the soft interface information
165 * @addr: the mac address of the client to look for
166 * @vid: VLAN identifier
168 * Return: a pointer to the corresponding tt_local_entry struct if the client is
169 * found, NULL otherwise.
171 static struct batadv_tt_local_entry
*
172 batadv_tt_local_hash_find(struct batadv_priv
*bat_priv
, const u8
*addr
,
175 struct batadv_tt_common_entry
*tt_common_entry
;
176 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
178 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.local_hash
, addr
,
181 tt_local_entry
= container_of(tt_common_entry
,
182 struct batadv_tt_local_entry
,
184 return tt_local_entry
;
188 * batadv_tt_global_hash_find() - search the global table for a given client
189 * @bat_priv: the bat priv with all the soft interface information
190 * @addr: the mac address of the client to look for
191 * @vid: VLAN identifier
193 * Return: a pointer to the corresponding tt_global_entry struct if the client
194 * is found, NULL otherwise.
196 struct batadv_tt_global_entry
*
197 batadv_tt_global_hash_find(struct batadv_priv
*bat_priv
, const u8
*addr
,
200 struct batadv_tt_common_entry
*tt_common_entry
;
201 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
203 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.global_hash
, addr
,
206 tt_global_entry
= container_of(tt_common_entry
,
207 struct batadv_tt_global_entry
,
209 return tt_global_entry
;
213 * batadv_tt_local_entry_free_rcu() - free the tt_local_entry
214 * @rcu: rcu pointer of the tt_local_entry
216 static void batadv_tt_local_entry_free_rcu(struct rcu_head
*rcu
)
218 struct batadv_tt_local_entry
*tt_local_entry
;
220 tt_local_entry
= container_of(rcu
, struct batadv_tt_local_entry
,
223 kmem_cache_free(batadv_tl_cache
, tt_local_entry
);
227 * batadv_tt_local_entry_release() - release tt_local_entry from lists and queue
228 * for free after rcu grace period
229 * @ref: kref pointer of the nc_node
231 static void batadv_tt_local_entry_release(struct kref
*ref
)
233 struct batadv_tt_local_entry
*tt_local_entry
;
235 tt_local_entry
= container_of(ref
, struct batadv_tt_local_entry
,
238 batadv_softif_vlan_put(tt_local_entry
->vlan
);
240 call_rcu(&tt_local_entry
->common
.rcu
, batadv_tt_local_entry_free_rcu
);
244 * batadv_tt_local_entry_put() - decrement the tt_local_entry refcounter and
245 * possibly release it
246 * @tt_local_entry: tt_local_entry to be free'd
249 batadv_tt_local_entry_put(struct batadv_tt_local_entry
*tt_local_entry
)
251 kref_put(&tt_local_entry
->common
.refcount
,
252 batadv_tt_local_entry_release
);
256 * batadv_tt_global_entry_free_rcu() - free the tt_global_entry
257 * @rcu: rcu pointer of the tt_global_entry
259 static void batadv_tt_global_entry_free_rcu(struct rcu_head
*rcu
)
261 struct batadv_tt_global_entry
*tt_global_entry
;
263 tt_global_entry
= container_of(rcu
, struct batadv_tt_global_entry
,
266 kmem_cache_free(batadv_tg_cache
, tt_global_entry
);
270 * batadv_tt_global_entry_release() - release tt_global_entry from lists and
271 * queue for free after rcu grace period
272 * @ref: kref pointer of the nc_node
274 static void batadv_tt_global_entry_release(struct kref
*ref
)
276 struct batadv_tt_global_entry
*tt_global_entry
;
278 tt_global_entry
= container_of(ref
, struct batadv_tt_global_entry
,
281 batadv_tt_global_del_orig_list(tt_global_entry
);
283 call_rcu(&tt_global_entry
->common
.rcu
, batadv_tt_global_entry_free_rcu
);
287 * batadv_tt_global_entry_put() - decrement the tt_global_entry refcounter and
288 * possibly release it
289 * @tt_global_entry: tt_global_entry to be free'd
291 void batadv_tt_global_entry_put(struct batadv_tt_global_entry
*tt_global_entry
)
293 kref_put(&tt_global_entry
->common
.refcount
,
294 batadv_tt_global_entry_release
);
298 * batadv_tt_global_hash_count() - count the number of orig entries
299 * @bat_priv: the bat priv with all the soft interface information
300 * @addr: the mac address of the client to count entries for
301 * @vid: VLAN identifier
303 * Return: the number of originators advertising the given address/data
304 * (excluding ourself).
306 int batadv_tt_global_hash_count(struct batadv_priv
*bat_priv
,
307 const u8
*addr
, unsigned short vid
)
309 struct batadv_tt_global_entry
*tt_global_entry
;
312 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
313 if (!tt_global_entry
)
316 count
= atomic_read(&tt_global_entry
->orig_list_count
);
317 batadv_tt_global_entry_put(tt_global_entry
);
323 * batadv_tt_local_size_mod() - change the size by v of the local table
325 * @bat_priv: the bat priv with all the soft interface information
326 * @vid: the VLAN identifier of the sub-table to change
327 * @v: the amount to sum to the local table size
329 static void batadv_tt_local_size_mod(struct batadv_priv
*bat_priv
,
330 unsigned short vid
, int v
)
332 struct batadv_softif_vlan
*vlan
;
334 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
338 atomic_add(v
, &vlan
->tt
.num_entries
);
340 batadv_softif_vlan_put(vlan
);
344 * batadv_tt_local_size_inc() - increase by one the local table size for the
346 * @bat_priv: the bat priv with all the soft interface information
347 * @vid: the VLAN identifier
349 static void batadv_tt_local_size_inc(struct batadv_priv
*bat_priv
,
352 batadv_tt_local_size_mod(bat_priv
, vid
, 1);
356 * batadv_tt_local_size_dec() - decrease by one the local table size for the
358 * @bat_priv: the bat priv with all the soft interface information
359 * @vid: the VLAN identifier
361 static void batadv_tt_local_size_dec(struct batadv_priv
*bat_priv
,
364 batadv_tt_local_size_mod(bat_priv
, vid
, -1);
368 * batadv_tt_global_size_mod() - change the size by v of the global table
369 * for orig_node identified by vid
370 * @orig_node: the originator for which the table has to be modified
371 * @vid: the VLAN identifier
372 * @v: the amount to sum to the global table size
374 static void batadv_tt_global_size_mod(struct batadv_orig_node
*orig_node
,
375 unsigned short vid
, int v
)
377 struct batadv_orig_node_vlan
*vlan
;
379 vlan
= batadv_orig_node_vlan_new(orig_node
, vid
);
383 if (atomic_add_return(v
, &vlan
->tt
.num_entries
) == 0) {
384 spin_lock_bh(&orig_node
->vlan_list_lock
);
385 if (!hlist_unhashed(&vlan
->list
)) {
386 hlist_del_init_rcu(&vlan
->list
);
387 batadv_orig_node_vlan_put(vlan
);
389 spin_unlock_bh(&orig_node
->vlan_list_lock
);
392 batadv_orig_node_vlan_put(vlan
);
396 * batadv_tt_global_size_inc() - increase by one the global table size for the
398 * @orig_node: the originator which global table size has to be decreased
399 * @vid: the vlan identifier
401 static void batadv_tt_global_size_inc(struct batadv_orig_node
*orig_node
,
404 batadv_tt_global_size_mod(orig_node
, vid
, 1);
408 * batadv_tt_global_size_dec() - decrease by one the global table size for the
410 * @orig_node: the originator which global table size has to be decreased
411 * @vid: the vlan identifier
413 static void batadv_tt_global_size_dec(struct batadv_orig_node
*orig_node
,
416 batadv_tt_global_size_mod(orig_node
, vid
, -1);
420 * batadv_tt_orig_list_entry_free_rcu() - free the orig_entry
421 * @rcu: rcu pointer of the orig_entry
423 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head
*rcu
)
425 struct batadv_tt_orig_list_entry
*orig_entry
;
427 orig_entry
= container_of(rcu
, struct batadv_tt_orig_list_entry
, rcu
);
429 kmem_cache_free(batadv_tt_orig_cache
, orig_entry
);
433 * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
434 * queue for free after rcu grace period
435 * @ref: kref pointer of the tt orig entry
437 static void batadv_tt_orig_list_entry_release(struct kref
*ref
)
439 struct batadv_tt_orig_list_entry
*orig_entry
;
441 orig_entry
= container_of(ref
, struct batadv_tt_orig_list_entry
,
444 batadv_orig_node_put(orig_entry
->orig_node
);
445 call_rcu(&orig_entry
->rcu
, batadv_tt_orig_list_entry_free_rcu
);
449 * batadv_tt_orig_list_entry_put() - decrement the tt orig entry refcounter and
450 * possibly release it
451 * @orig_entry: tt orig entry to be free'd
454 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry
*orig_entry
)
456 kref_put(&orig_entry
->refcount
, batadv_tt_orig_list_entry_release
);
460 * batadv_tt_local_event() - store a local TT event (ADD/DEL)
461 * @bat_priv: the bat priv with all the soft interface information
462 * @tt_local_entry: the TT entry involved in the event
463 * @event_flags: flags to store in the event structure
465 static void batadv_tt_local_event(struct batadv_priv
*bat_priv
,
466 struct batadv_tt_local_entry
*tt_local_entry
,
469 struct batadv_tt_change_node
*tt_change_node
, *entry
, *safe
;
470 struct batadv_tt_common_entry
*common
= &tt_local_entry
->common
;
471 u8 flags
= common
->flags
| event_flags
;
472 bool event_removed
= false;
473 bool del_op_requested
, del_op_entry
;
475 tt_change_node
= kmem_cache_alloc(batadv_tt_change_cache
, GFP_ATOMIC
);
479 tt_change_node
->change
.flags
= flags
;
480 memset(tt_change_node
->change
.reserved
, 0,
481 sizeof(tt_change_node
->change
.reserved
));
482 ether_addr_copy(tt_change_node
->change
.addr
, common
->addr
);
483 tt_change_node
->change
.vid
= htons(common
->vid
);
485 del_op_requested
= flags
& BATADV_TT_CLIENT_DEL
;
487 /* check for ADD+DEL or DEL+ADD events */
488 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
489 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
491 if (!batadv_compare_eth(entry
->change
.addr
, common
->addr
))
494 /* DEL+ADD in the same orig interval have no effect and can be
495 * removed to avoid silly behaviour on the receiver side. The
496 * other way around (ADD+DEL) can happen in case of roaming of
497 * a client still in the NEW state. Roaming of NEW clients is
498 * now possible due to automatically recognition of "temporary"
501 del_op_entry
= entry
->change
.flags
& BATADV_TT_CLIENT_DEL
;
502 if (!del_op_requested
&& del_op_entry
)
504 if (del_op_requested
&& !del_op_entry
)
507 /* this is a second add in the same originator interval. It
508 * means that flags have been changed: update them!
510 if (!del_op_requested
&& !del_op_entry
)
511 entry
->change
.flags
= flags
;
515 list_del(&entry
->list
);
516 kmem_cache_free(batadv_tt_change_cache
, entry
);
517 kmem_cache_free(batadv_tt_change_cache
, tt_change_node
);
518 event_removed
= true;
522 /* track the change in the OGMinterval list */
523 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt
.changes_list
);
526 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
529 atomic_dec(&bat_priv
->tt
.local_changes
);
531 atomic_inc(&bat_priv
->tt
.local_changes
);
535 * batadv_tt_len() - compute length in bytes of given number of tt changes
536 * @changes_num: number of tt changes
538 * Return: computed length in bytes.
540 static int batadv_tt_len(int changes_num
)
542 return changes_num
* sizeof(struct batadv_tvlv_tt_change
);
546 * batadv_tt_entries() - compute the number of entries fitting in tt_len bytes
547 * @tt_len: available space
549 * Return: the number of entries.
551 static u16
batadv_tt_entries(u16 tt_len
)
553 return tt_len
/ batadv_tt_len(1);
557 * batadv_tt_local_table_transmit_size() - calculates the local translation
558 * table size when transmitted over the air
559 * @bat_priv: the bat priv with all the soft interface information
561 * Return: local translation table size in bytes.
563 static int batadv_tt_local_table_transmit_size(struct batadv_priv
*bat_priv
)
566 u16 tt_local_entries
= 0;
567 struct batadv_softif_vlan
*vlan
;
571 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
573 tt_local_entries
+= atomic_read(&vlan
->tt
.num_entries
);
577 /* header size of tvlv encapsulated tt response payload */
578 hdr_size
= sizeof(struct batadv_unicast_tvlv_packet
);
579 hdr_size
+= sizeof(struct batadv_tvlv_hdr
);
580 hdr_size
+= sizeof(struct batadv_tvlv_tt_data
);
581 hdr_size
+= num_vlan
* sizeof(struct batadv_tvlv_tt_vlan_data
);
583 return hdr_size
+ batadv_tt_len(tt_local_entries
);
586 static int batadv_tt_local_init(struct batadv_priv
*bat_priv
)
588 if (bat_priv
->tt
.local_hash
)
591 bat_priv
->tt
.local_hash
= batadv_hash_new(1024);
593 if (!bat_priv
->tt
.local_hash
)
596 batadv_hash_set_lock_class(bat_priv
->tt
.local_hash
,
597 &batadv_tt_local_hash_lock_class_key
);
602 static void batadv_tt_global_free(struct batadv_priv
*bat_priv
,
603 struct batadv_tt_global_entry
*tt_global
,
606 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
607 "Deleting global tt entry %pM (vid: %d): %s\n",
608 tt_global
->common
.addr
,
609 batadv_print_vid(tt_global
->common
.vid
), message
);
611 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
612 batadv_choose_tt
, &tt_global
->common
);
613 batadv_tt_global_entry_put(tt_global
);
617 * batadv_tt_local_add() - add a new client to the local table or update an
619 * @soft_iface: netdev struct of the mesh interface
620 * @addr: the mac address of the client to add
621 * @vid: VLAN identifier
622 * @ifindex: index of the interface where the client is connected to (useful to
623 * identify wireless clients)
624 * @mark: the value contained in the skb->mark field of the received packet (if
627 * Return: true if the client was successfully added, false otherwise.
629 bool batadv_tt_local_add(struct net_device
*soft_iface
, const u8
*addr
,
630 unsigned short vid
, int ifindex
, u32 mark
)
632 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
633 struct batadv_tt_local_entry
*tt_local
;
634 struct batadv_tt_global_entry
*tt_global
= NULL
;
635 struct net
*net
= dev_net(soft_iface
);
636 struct batadv_softif_vlan
*vlan
;
637 struct net_device
*in_dev
= NULL
;
638 struct batadv_hard_iface
*in_hardif
= NULL
;
639 struct hlist_head
*head
;
640 struct batadv_tt_orig_list_entry
*orig_entry
;
641 int hash_added
, table_size
, packet_size_max
;
643 bool roamed_back
= false;
647 if (ifindex
!= BATADV_NULL_IFINDEX
)
648 in_dev
= dev_get_by_index(net
, ifindex
);
651 in_hardif
= batadv_hardif_get_by_netdev(in_dev
);
653 tt_local
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
655 if (!is_multicast_ether_addr(addr
))
656 tt_global
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
659 tt_local
->last_seen
= jiffies
;
660 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_PENDING
) {
661 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
662 "Re-adding pending client %pM (vid: %d)\n",
663 addr
, batadv_print_vid(vid
));
664 /* whatever the reason why the PENDING flag was set,
665 * this is a client which was enqueued to be removed in
666 * this orig_interval. Since it popped up again, the
667 * flag can be reset like it was never enqueued
669 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_PENDING
;
673 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
674 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
675 "Roaming client %pM (vid: %d) came back to its original location\n",
676 addr
, batadv_print_vid(vid
));
677 /* the ROAM flag is set because this client roamed away
678 * and the node got a roaming_advertisement message. Now
679 * that the client popped up again at its original
680 * location such flag can be unset
682 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
688 /* Ignore the client if we cannot send it in a full table response. */
689 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
690 table_size
+= batadv_tt_len(1);
691 packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
692 if (table_size
> packet_size_max
) {
693 net_ratelimited_function(batadv_info
, soft_iface
,
694 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
695 table_size
, packet_size_max
, addr
);
699 tt_local
= kmem_cache_alloc(batadv_tl_cache
, GFP_ATOMIC
);
703 /* increase the refcounter of the related vlan */
704 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
706 net_ratelimited_function(batadv_info
, soft_iface
,
707 "adding TT local entry %pM to non-existent VLAN %d\n",
708 addr
, batadv_print_vid(vid
));
709 kmem_cache_free(batadv_tl_cache
, tt_local
);
714 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
715 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
716 addr
, batadv_print_vid(vid
),
717 (u8
)atomic_read(&bat_priv
->tt
.vn
));
719 ether_addr_copy(tt_local
->common
.addr
, addr
);
720 /* The local entry has to be marked as NEW to avoid to send it in
721 * a full table response going out before the next ttvn increment
722 * (consistency check)
724 tt_local
->common
.flags
= BATADV_TT_CLIENT_NEW
;
725 tt_local
->common
.vid
= vid
;
726 if (batadv_is_wifi_hardif(in_hardif
))
727 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
728 kref_init(&tt_local
->common
.refcount
);
729 tt_local
->last_seen
= jiffies
;
730 tt_local
->common
.added_at
= tt_local
->last_seen
;
731 tt_local
->vlan
= vlan
;
733 /* the batman interface mac and multicast addresses should never be
736 if (batadv_compare_eth(addr
, soft_iface
->dev_addr
) ||
737 is_multicast_ether_addr(addr
))
738 tt_local
->common
.flags
|= BATADV_TT_CLIENT_NOPURGE
;
740 kref_get(&tt_local
->common
.refcount
);
741 hash_added
= batadv_hash_add(bat_priv
->tt
.local_hash
, batadv_compare_tt
,
742 batadv_choose_tt
, &tt_local
->common
,
743 &tt_local
->common
.hash_entry
);
745 if (unlikely(hash_added
!= 0)) {
746 /* remove the reference for the hash */
747 batadv_tt_local_entry_put(tt_local
);
752 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
755 /* Check whether it is a roaming, but don't do anything if the roaming
756 * process has already been handled
758 if (tt_global
&& !(tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
)) {
759 /* These node are probably going to update their tt table */
760 head
= &tt_global
->orig_list
;
762 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
763 batadv_send_roam_adv(bat_priv
, tt_global
->common
.addr
,
764 tt_global
->common
.vid
,
765 orig_entry
->orig_node
);
769 batadv_tt_global_free(bat_priv
, tt_global
,
773 /* The global entry has to be marked as ROAMING and
774 * has to be kept for consistency purpose
776 tt_global
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
777 tt_global
->roam_at
= jiffies
;
781 /* store the current remote flags before altering them. This helps
782 * understanding is flags are changing or not
784 remote_flags
= tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
;
786 if (batadv_is_wifi_hardif(in_hardif
))
787 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
789 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_WIFI
;
791 /* check the mark in the skb: if it's equal to the configured
792 * isolation_mark, it means the packet is coming from an isolated
795 match_mark
= (mark
& bat_priv
->isolation_mark_mask
);
796 if (bat_priv
->isolation_mark_mask
&&
797 match_mark
== bat_priv
->isolation_mark
)
798 tt_local
->common
.flags
|= BATADV_TT_CLIENT_ISOLA
;
800 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ISOLA
;
802 /* if any "dynamic" flag has been modified, resend an ADD event for this
803 * entry so that all the nodes can get the new flags
805 if (remote_flags
^ (tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
))
806 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
811 batadv_hardif_put(in_hardif
);
815 batadv_tt_local_entry_put(tt_local
);
817 batadv_tt_global_entry_put(tt_global
);
822 * batadv_tt_prepare_tvlv_global_data() - prepare the TVLV TT header to send
823 * within a TT Response directed to another node
824 * @orig_node: originator for which the TT data has to be prepared
825 * @tt_data: uninitialised pointer to the address of the TVLV buffer
826 * @tt_change: uninitialised pointer to the address of the area where the TT
827 * changed can be stored
828 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
829 * function reserves the amount of space needed to send the entire global TT
830 * table. In case of success the value is updated with the real amount of
832 * Allocate the needed amount of memory for the entire TT TVLV and write its
833 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
834 * objects, one per active VLAN served by the originator node.
836 * Return: the size of the allocated buffer or 0 in case of failure.
839 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node
*orig_node
,
840 struct batadv_tvlv_tt_data
**tt_data
,
841 struct batadv_tvlv_tt_change
**tt_change
,
848 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
849 struct batadv_orig_node_vlan
*vlan
;
852 spin_lock_bh(&orig_node
->vlan_list_lock
);
853 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
855 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
858 change_offset
= sizeof(**tt_data
);
859 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
861 /* if tt_len is negative, allocate the space needed by the full table */
863 *tt_len
= batadv_tt_len(num_entries
);
866 tvlv_len
+= change_offset
;
868 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
874 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
875 (*tt_data
)->ttvn
= atomic_read(&orig_node
->last_ttvn
);
876 (*tt_data
)->num_vlan
= htons(num_vlan
);
878 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
879 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
880 tt_vlan
->vid
= htons(vlan
->vid
);
881 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
886 tt_change_ptr
= (u8
*)*tt_data
+ change_offset
;
887 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
890 spin_unlock_bh(&orig_node
->vlan_list_lock
);
895 * batadv_tt_prepare_tvlv_local_data() - allocate and prepare the TT TVLV for
897 * @bat_priv: the bat priv with all the soft interface information
898 * @tt_data: uninitialised pointer to the address of the TVLV buffer
899 * @tt_change: uninitialised pointer to the address of the area where the TT
900 * changes can be stored
901 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
902 * function reserves the amount of space needed to send the entire local TT
903 * table. In case of success the value is updated with the real amount of
906 * Allocate the needed amount of memory for the entire TT TVLV and write its
907 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
908 * objects, one per active VLAN.
910 * Return: the size of the allocated buffer or 0 in case of failure.
913 batadv_tt_prepare_tvlv_local_data(struct batadv_priv
*bat_priv
,
914 struct batadv_tvlv_tt_data
**tt_data
,
915 struct batadv_tvlv_tt_change
**tt_change
,
918 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
919 struct batadv_softif_vlan
*vlan
;
921 u16 vlan_entries
= 0;
922 u16 total_entries
= 0;
927 spin_lock_bh(&bat_priv
->softif_vlan_list_lock
);
928 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
929 vlan_entries
= atomic_read(&vlan
->tt
.num_entries
);
930 if (vlan_entries
< 1)
934 total_entries
+= vlan_entries
;
937 change_offset
= sizeof(**tt_data
);
938 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
940 /* if tt_len is negative, allocate the space needed by the full table */
942 *tt_len
= batadv_tt_len(total_entries
);
945 tvlv_len
+= change_offset
;
947 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
953 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
954 (*tt_data
)->ttvn
= atomic_read(&bat_priv
->tt
.vn
);
955 (*tt_data
)->num_vlan
= htons(num_vlan
);
957 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
958 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
959 vlan_entries
= atomic_read(&vlan
->tt
.num_entries
);
960 if (vlan_entries
< 1)
963 tt_vlan
->vid
= htons(vlan
->vid
);
964 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
969 tt_change_ptr
= (u8
*)*tt_data
+ change_offset
;
970 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
973 spin_unlock_bh(&bat_priv
->softif_vlan_list_lock
);
978 * batadv_tt_tvlv_container_update() - update the translation table tvlv
979 * container after local tt changes have been committed
980 * @bat_priv: the bat priv with all the soft interface information
982 static void batadv_tt_tvlv_container_update(struct batadv_priv
*bat_priv
)
984 struct batadv_tt_change_node
*entry
, *safe
;
985 struct batadv_tvlv_tt_data
*tt_data
;
986 struct batadv_tvlv_tt_change
*tt_change
;
987 int tt_diff_len
, tt_change_len
= 0;
988 int tt_diff_entries_num
= 0;
989 int tt_diff_entries_count
= 0;
992 tt_diff_entries_num
= atomic_read(&bat_priv
->tt
.local_changes
);
993 tt_diff_len
= batadv_tt_len(tt_diff_entries_num
);
995 /* if we have too many changes for one packet don't send any
996 * and wait for the tt table request which will be fragmented
998 if (tt_diff_len
> bat_priv
->soft_iface
->mtu
)
1001 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
, &tt_data
,
1002 &tt_change
, &tt_diff_len
);
1006 tt_data
->flags
= BATADV_TT_OGM_DIFF
;
1008 if (tt_diff_len
== 0)
1009 goto container_register
;
1011 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
1012 atomic_set(&bat_priv
->tt
.local_changes
, 0);
1014 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
1016 if (tt_diff_entries_count
< tt_diff_entries_num
) {
1017 memcpy(tt_change
+ tt_diff_entries_count
,
1019 sizeof(struct batadv_tvlv_tt_change
));
1020 tt_diff_entries_count
++;
1022 list_del(&entry
->list
);
1023 kmem_cache_free(batadv_tt_change_cache
, entry
);
1025 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
1027 /* Keep the buffer for possible tt_request */
1028 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
1029 kfree(bat_priv
->tt
.last_changeset
);
1030 bat_priv
->tt
.last_changeset_len
= 0;
1031 bat_priv
->tt
.last_changeset
= NULL
;
1032 tt_change_len
= batadv_tt_len(tt_diff_entries_count
);
1033 /* check whether this new OGM has no changes due to size problems */
1034 if (tt_diff_entries_count
> 0) {
1035 /* if kmalloc() fails we will reply with the full table
1036 * instead of providing the diff
1038 bat_priv
->tt
.last_changeset
= kzalloc(tt_diff_len
, GFP_ATOMIC
);
1039 if (bat_priv
->tt
.last_changeset
) {
1040 memcpy(bat_priv
->tt
.last_changeset
,
1041 tt_change
, tt_change_len
);
1042 bat_priv
->tt
.last_changeset_len
= tt_diff_len
;
1045 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
1048 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_TT
, 1, tt_data
,
1053 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1056 * batadv_tt_local_seq_print_text() - Print the local tt table in a seq file
1057 * @seq: seq file to print on
1062 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
1064 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1065 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1066 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1067 struct batadv_tt_common_entry
*tt_common_entry
;
1068 struct batadv_tt_local_entry
*tt_local
;
1069 struct batadv_hard_iface
*primary_if
;
1070 struct hlist_head
*head
;
1073 int last_seen_msecs
;
1074 unsigned long last_seen_jiffies
;
1076 u16 np_flag
= BATADV_TT_CLIENT_NOPURGE
;
1078 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1083 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1084 net_dev
->name
, (u8
)atomic_read(&bat_priv
->tt
.vn
));
1086 " Client VID Flags Last seen (CRC )\n");
1088 for (i
= 0; i
< hash
->size
; i
++) {
1089 head
= &hash
->table
[i
];
1092 hlist_for_each_entry_rcu(tt_common_entry
,
1094 tt_local
= container_of(tt_common_entry
,
1095 struct batadv_tt_local_entry
,
1097 last_seen_jiffies
= jiffies
- tt_local
->last_seen
;
1098 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
1099 last_seen_secs
= last_seen_msecs
/ 1000;
1100 last_seen_msecs
= last_seen_msecs
% 1000;
1102 no_purge
= tt_common_entry
->flags
& np_flag
;
1104 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
1105 tt_common_entry
->addr
,
1106 batadv_print_vid(tt_common_entry
->vid
),
1107 ((tt_common_entry
->flags
&
1108 BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1109 no_purge
? 'P' : '.',
1110 ((tt_common_entry
->flags
&
1111 BATADV_TT_CLIENT_NEW
) ? 'N' : '.'),
1112 ((tt_common_entry
->flags
&
1113 BATADV_TT_CLIENT_PENDING
) ? 'X' : '.'),
1114 ((tt_common_entry
->flags
&
1115 BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1116 ((tt_common_entry
->flags
&
1117 BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1118 no_purge
? 0 : last_seen_secs
,
1119 no_purge
? 0 : last_seen_msecs
,
1120 tt_local
->vlan
->tt
.crc
);
1126 batadv_hardif_put(primary_if
);
1132 * batadv_tt_local_dump_entry() - Dump one TT local entry into a message
1133 * @msg :Netlink message to dump into
1134 * @portid: Port making netlink request
1135 * @cb: Control block containing additional options
1136 * @bat_priv: The bat priv with all the soft interface information
1137 * @common: tt local & tt global common data
1139 * Return: Error code, or 0 on success
1142 batadv_tt_local_dump_entry(struct sk_buff
*msg
, u32 portid
,
1143 struct netlink_callback
*cb
,
1144 struct batadv_priv
*bat_priv
,
1145 struct batadv_tt_common_entry
*common
)
1148 struct batadv_softif_vlan
*vlan
;
1149 struct batadv_tt_local_entry
*local
;
1150 unsigned int last_seen_msecs
;
1153 local
= container_of(common
, struct batadv_tt_local_entry
, common
);
1154 last_seen_msecs
= jiffies_to_msecs(jiffies
- local
->last_seen
);
1156 vlan
= batadv_softif_vlan_get(bat_priv
, common
->vid
);
1162 batadv_softif_vlan_put(vlan
);
1164 hdr
= genlmsg_put(msg
, portid
, cb
->nlh
->nlmsg_seq
,
1165 &batadv_netlink_family
, NLM_F_MULTI
,
1166 BATADV_CMD_GET_TRANSTABLE_LOCAL
);
1170 genl_dump_check_consistent(cb
, hdr
);
1172 if (nla_put(msg
, BATADV_ATTR_TT_ADDRESS
, ETH_ALEN
, common
->addr
) ||
1173 nla_put_u32(msg
, BATADV_ATTR_TT_CRC32
, crc
) ||
1174 nla_put_u16(msg
, BATADV_ATTR_TT_VID
, common
->vid
) ||
1175 nla_put_u32(msg
, BATADV_ATTR_TT_FLAGS
, common
->flags
))
1176 goto nla_put_failure
;
1178 if (!(common
->flags
& BATADV_TT_CLIENT_NOPURGE
) &&
1179 nla_put_u32(msg
, BATADV_ATTR_LAST_SEEN_MSECS
, last_seen_msecs
))
1180 goto nla_put_failure
;
1182 genlmsg_end(msg
, hdr
);
1186 genlmsg_cancel(msg
, hdr
);
1191 * batadv_tt_local_dump_bucket() - Dump one TT local bucket into a message
1192 * @msg: Netlink message to dump into
1193 * @portid: Port making netlink request
1194 * @cb: Control block containing additional options
1195 * @bat_priv: The bat priv with all the soft interface information
1196 * @hash: hash to dump
1197 * @bucket: bucket index to dump
1198 * @idx_s: Number of entries to skip
1200 * Return: Error code, or 0 on success
1203 batadv_tt_local_dump_bucket(struct sk_buff
*msg
, u32 portid
,
1204 struct netlink_callback
*cb
,
1205 struct batadv_priv
*bat_priv
,
1206 struct batadv_hashtable
*hash
, unsigned int bucket
,
1209 struct batadv_tt_common_entry
*common
;
1212 spin_lock_bh(&hash
->list_locks
[bucket
]);
1213 cb
->seq
= atomic_read(&hash
->generation
) << 1 | 1;
1215 hlist_for_each_entry(common
, &hash
->table
[bucket
], hash_entry
) {
1219 if (batadv_tt_local_dump_entry(msg
, portid
, cb
, bat_priv
,
1221 spin_unlock_bh(&hash
->list_locks
[bucket
]);
1226 spin_unlock_bh(&hash
->list_locks
[bucket
]);
1233 * batadv_tt_local_dump() - Dump TT local entries into a message
1234 * @msg: Netlink message to dump into
1235 * @cb: Parameters from query
1237 * Return: Error code, or 0 on success
1239 int batadv_tt_local_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
)
1241 struct net
*net
= sock_net(cb
->skb
->sk
);
1242 struct net_device
*soft_iface
;
1243 struct batadv_priv
*bat_priv
;
1244 struct batadv_hard_iface
*primary_if
= NULL
;
1245 struct batadv_hashtable
*hash
;
1248 int bucket
= cb
->args
[0];
1249 int idx
= cb
->args
[1];
1250 int portid
= NETLINK_CB(cb
->skb
).portid
;
1252 ifindex
= batadv_netlink_get_ifindex(cb
->nlh
, BATADV_ATTR_MESH_IFINDEX
);
1256 soft_iface
= dev_get_by_index(net
, ifindex
);
1257 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
1262 bat_priv
= netdev_priv(soft_iface
);
1264 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1265 if (!primary_if
|| primary_if
->if_status
!= BATADV_IF_ACTIVE
) {
1270 hash
= bat_priv
->tt
.local_hash
;
1272 while (bucket
< hash
->size
) {
1273 if (batadv_tt_local_dump_bucket(msg
, portid
, cb
, bat_priv
,
1274 hash
, bucket
, &idx
))
1284 batadv_hardif_put(primary_if
);
1286 dev_put(soft_iface
);
1288 cb
->args
[0] = bucket
;
1295 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
1296 struct batadv_tt_local_entry
*tt_local_entry
,
1297 u16 flags
, const char *message
)
1299 batadv_tt_local_event(bat_priv
, tt_local_entry
, flags
);
1301 /* The local client has to be marked as "pending to be removed" but has
1302 * to be kept in the table in order to send it in a full table
1303 * response issued before the net ttvn increment (consistency check)
1305 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
1307 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1308 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1309 tt_local_entry
->common
.addr
,
1310 batadv_print_vid(tt_local_entry
->common
.vid
), message
);
1314 * batadv_tt_local_remove() - logically remove an entry from the local table
1315 * @bat_priv: the bat priv with all the soft interface information
1316 * @addr: the MAC address of the client to remove
1317 * @vid: VLAN identifier
1318 * @message: message to append to the log on deletion
1319 * @roaming: true if the deletion is due to a roaming event
1321 * Return: the flags assigned to the local entry before being deleted
1323 u16
batadv_tt_local_remove(struct batadv_priv
*bat_priv
, const u8
*addr
,
1324 unsigned short vid
, const char *message
,
1327 struct batadv_tt_local_entry
*tt_local_entry
;
1328 u16 flags
, curr_flags
= BATADV_NO_FLAGS
;
1329 void *tt_entry_exists
;
1331 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
1332 if (!tt_local_entry
)
1335 curr_flags
= tt_local_entry
->common
.flags
;
1337 flags
= BATADV_TT_CLIENT_DEL
;
1338 /* if this global entry addition is due to a roaming, the node has to
1339 * mark the local entry as "roamed" in order to correctly reroute
1343 flags
|= BATADV_TT_CLIENT_ROAM
;
1344 /* mark the local client as ROAMed */
1345 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1348 if (!(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
)) {
1349 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
,
1353 /* if this client has been added right now, it is possible to
1354 * immediately purge it
1356 batadv_tt_local_event(bat_priv
, tt_local_entry
, BATADV_TT_CLIENT_DEL
);
1358 tt_entry_exists
= batadv_hash_remove(bat_priv
->tt
.local_hash
,
1361 &tt_local_entry
->common
);
1362 if (!tt_entry_exists
)
1365 /* extra call to free the local tt entry */
1366 batadv_tt_local_entry_put(tt_local_entry
);
1370 batadv_tt_local_entry_put(tt_local_entry
);
1376 * batadv_tt_local_purge_list() - purge inactive tt local entries
1377 * @bat_priv: the bat priv with all the soft interface information
1378 * @head: pointer to the list containing the local tt entries
1379 * @timeout: parameter deciding whether a given tt local entry is considered
1382 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
1383 struct hlist_head
*head
,
1386 struct batadv_tt_local_entry
*tt_local_entry
;
1387 struct batadv_tt_common_entry
*tt_common_entry
;
1388 struct hlist_node
*node_tmp
;
1390 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
, head
,
1392 tt_local_entry
= container_of(tt_common_entry
,
1393 struct batadv_tt_local_entry
,
1395 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
1398 /* entry already marked for deletion */
1399 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1402 if (!batadv_has_timed_out(tt_local_entry
->last_seen
, timeout
))
1405 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
1406 BATADV_TT_CLIENT_DEL
, "timed out");
1411 * batadv_tt_local_purge() - purge inactive tt local entries
1412 * @bat_priv: the bat priv with all the soft interface information
1413 * @timeout: parameter deciding whether a given tt local entry is considered
1416 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
,
1419 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1420 struct hlist_head
*head
;
1421 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1424 for (i
= 0; i
< hash
->size
; i
++) {
1425 head
= &hash
->table
[i
];
1426 list_lock
= &hash
->list_locks
[i
];
1428 spin_lock_bh(list_lock
);
1429 batadv_tt_local_purge_list(bat_priv
, head
, timeout
);
1430 spin_unlock_bh(list_lock
);
1434 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
1436 struct batadv_hashtable
*hash
;
1437 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1438 struct batadv_tt_common_entry
*tt_common_entry
;
1439 struct batadv_tt_local_entry
*tt_local
;
1440 struct hlist_node
*node_tmp
;
1441 struct hlist_head
*head
;
1444 if (!bat_priv
->tt
.local_hash
)
1447 hash
= bat_priv
->tt
.local_hash
;
1449 for (i
= 0; i
< hash
->size
; i
++) {
1450 head
= &hash
->table
[i
];
1451 list_lock
= &hash
->list_locks
[i
];
1453 spin_lock_bh(list_lock
);
1454 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1456 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1457 tt_local
= container_of(tt_common_entry
,
1458 struct batadv_tt_local_entry
,
1461 batadv_tt_local_entry_put(tt_local
);
1463 spin_unlock_bh(list_lock
);
1466 batadv_hash_destroy(hash
);
1468 bat_priv
->tt
.local_hash
= NULL
;
1471 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
1473 if (bat_priv
->tt
.global_hash
)
1476 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
1478 if (!bat_priv
->tt
.global_hash
)
1481 batadv_hash_set_lock_class(bat_priv
->tt
.global_hash
,
1482 &batadv_tt_global_hash_lock_class_key
);
1487 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
1489 struct batadv_tt_change_node
*entry
, *safe
;
1491 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
1493 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
1495 list_del(&entry
->list
);
1496 kmem_cache_free(batadv_tt_change_cache
, entry
);
1499 atomic_set(&bat_priv
->tt
.local_changes
, 0);
1500 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
1504 * batadv_tt_global_orig_entry_find() - find a TT orig_list_entry
1505 * @entry: the TT global entry where the orig_list_entry has to be
1507 * @orig_node: the originator for which the orig_list_entry has to be found
1509 * retrieve the orig_tt_list_entry belonging to orig_node from the
1510 * batadv_tt_global_entry list
1512 * Return: it with an increased refcounter, NULL if not found
1514 static struct batadv_tt_orig_list_entry
*
1515 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
1516 const struct batadv_orig_node
*orig_node
)
1518 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
1519 const struct hlist_head
*head
;
1522 head
= &entry
->orig_list
;
1523 hlist_for_each_entry_rcu(tmp_orig_entry
, head
, list
) {
1524 if (tmp_orig_entry
->orig_node
!= orig_node
)
1526 if (!kref_get_unless_zero(&tmp_orig_entry
->refcount
))
1529 orig_entry
= tmp_orig_entry
;
1538 * batadv_tt_global_entry_has_orig() - check if a TT global entry is also
1539 * handled by a given originator
1540 * @entry: the TT global entry to check
1541 * @orig_node: the originator to search in the list
1542 * @flags: a pointer to store TT flags for the given @entry received
1545 * find out if an orig_node is already in the list of a tt_global_entry.
1547 * Return: true if found, false otherwise
1550 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
1551 const struct batadv_orig_node
*orig_node
,
1554 struct batadv_tt_orig_list_entry
*orig_entry
;
1557 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
1562 *flags
= orig_entry
->flags
;
1564 batadv_tt_orig_list_entry_put(orig_entry
);
1571 * batadv_tt_global_sync_flags() - update TT sync flags
1572 * @tt_global: the TT global entry to update sync flags in
1574 * Updates the sync flag bits in the tt_global flag attribute with a logical
1575 * OR of all sync flags from any of its TT orig entries.
1578 batadv_tt_global_sync_flags(struct batadv_tt_global_entry
*tt_global
)
1580 struct batadv_tt_orig_list_entry
*orig_entry
;
1581 const struct hlist_head
*head
;
1582 u16 flags
= BATADV_NO_FLAGS
;
1585 head
= &tt_global
->orig_list
;
1586 hlist_for_each_entry_rcu(orig_entry
, head
, list
)
1587 flags
|= orig_entry
->flags
;
1590 flags
|= tt_global
->common
.flags
& (~BATADV_TT_SYNC_MASK
);
1591 tt_global
->common
.flags
= flags
;
1595 * batadv_tt_global_orig_entry_add() - add or update a TT orig entry
1596 * @tt_global: the TT global entry to add an orig entry in
1597 * @orig_node: the originator to add an orig entry for
1598 * @ttvn: translation table version number of this changeset
1599 * @flags: TT sync flags
1602 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
1603 struct batadv_orig_node
*orig_node
, int ttvn
,
1606 struct batadv_tt_orig_list_entry
*orig_entry
;
1608 spin_lock_bh(&tt_global
->list_lock
);
1610 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
1612 /* refresh the ttvn: the current value could be a bogus one that
1613 * was added during a "temporary client detection"
1615 orig_entry
->ttvn
= ttvn
;
1616 orig_entry
->flags
= flags
;
1620 orig_entry
= kmem_cache_zalloc(batadv_tt_orig_cache
, GFP_ATOMIC
);
1624 INIT_HLIST_NODE(&orig_entry
->list
);
1625 kref_get(&orig_node
->refcount
);
1626 batadv_tt_global_size_inc(orig_node
, tt_global
->common
.vid
);
1627 orig_entry
->orig_node
= orig_node
;
1628 orig_entry
->ttvn
= ttvn
;
1629 orig_entry
->flags
= flags
;
1630 kref_init(&orig_entry
->refcount
);
1632 kref_get(&orig_entry
->refcount
);
1633 hlist_add_head_rcu(&orig_entry
->list
,
1634 &tt_global
->orig_list
);
1635 atomic_inc(&tt_global
->orig_list_count
);
1638 batadv_tt_global_sync_flags(tt_global
);
1641 batadv_tt_orig_list_entry_put(orig_entry
);
1643 spin_unlock_bh(&tt_global
->list_lock
);
1647 * batadv_tt_global_add() - add a new TT global entry or update an existing one
1648 * @bat_priv: the bat priv with all the soft interface information
1649 * @orig_node: the originator announcing the client
1650 * @tt_addr: the mac address of the non-mesh client
1651 * @vid: VLAN identifier
1652 * @flags: TT flags that have to be set for this non-mesh client
1653 * @ttvn: the tt version number ever announcing this non-mesh client
1655 * Add a new TT global entry for the given originator. If the entry already
1656 * exists add a new reference to the given originator (a global entry can have
1657 * references to multiple originators) and adjust the flags attribute to reflect
1658 * the function argument.
1659 * If a TT local entry exists for this non-mesh client remove it.
1661 * The caller must hold orig_node refcount.
1663 * Return: true if the new entry has been added, false otherwise
1665 static bool batadv_tt_global_add(struct batadv_priv
*bat_priv
,
1666 struct batadv_orig_node
*orig_node
,
1667 const unsigned char *tt_addr
,
1668 unsigned short vid
, u16 flags
, u8 ttvn
)
1670 struct batadv_tt_global_entry
*tt_global_entry
;
1671 struct batadv_tt_local_entry
*tt_local_entry
;
1674 struct batadv_tt_common_entry
*common
;
1677 /* ignore global entries from backbone nodes */
1678 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
, vid
))
1681 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
, vid
);
1682 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, tt_addr
, vid
);
1684 /* if the node already has a local client for this entry, it has to wait
1685 * for a roaming advertisement instead of manually messing up the global
1688 if ((flags
& BATADV_TT_CLIENT_TEMP
) && tt_local_entry
&&
1689 !(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
))
1692 if (!tt_global_entry
) {
1693 tt_global_entry
= kmem_cache_zalloc(batadv_tg_cache
,
1695 if (!tt_global_entry
)
1698 common
= &tt_global_entry
->common
;
1699 ether_addr_copy(common
->addr
, tt_addr
);
1702 if (!is_multicast_ether_addr(common
->addr
))
1703 common
->flags
= flags
& (~BATADV_TT_SYNC_MASK
);
1705 tt_global_entry
->roam_at
= 0;
1706 /* node must store current time in case of roaming. This is
1707 * needed to purge this entry out on timeout (if nobody claims
1710 if (flags
& BATADV_TT_CLIENT_ROAM
)
1711 tt_global_entry
->roam_at
= jiffies
;
1712 kref_init(&common
->refcount
);
1713 common
->added_at
= jiffies
;
1715 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
1716 atomic_set(&tt_global_entry
->orig_list_count
, 0);
1717 spin_lock_init(&tt_global_entry
->list_lock
);
1719 kref_get(&common
->refcount
);
1720 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
1722 batadv_choose_tt
, common
,
1723 &common
->hash_entry
);
1725 if (unlikely(hash_added
!= 0)) {
1726 /* remove the reference for the hash */
1727 batadv_tt_global_entry_put(tt_global_entry
);
1731 common
= &tt_global_entry
->common
;
1732 /* If there is already a global entry, we can use this one for
1734 * But if we are trying to add a temporary client then here are
1735 * two options at this point:
1736 * 1) the global client is not a temporary client: the global
1737 * client has to be left as it is, temporary information
1738 * should never override any already known client state
1739 * 2) the global client is a temporary client: purge the
1740 * originator list and add the new one orig_entry
1742 if (flags
& BATADV_TT_CLIENT_TEMP
) {
1743 if (!(common
->flags
& BATADV_TT_CLIENT_TEMP
))
1745 if (batadv_tt_global_entry_has_orig(tt_global_entry
,
1748 batadv_tt_global_del_orig_list(tt_global_entry
);
1749 goto add_orig_entry
;
1752 /* if the client was temporary added before receiving the first
1753 * OGM announcing it, we have to clear the TEMP flag. Also,
1754 * remove the previous temporary orig node and re-add it
1755 * if required. If the orig entry changed, the new one which
1756 * is a non-temporary entry is preferred.
1758 if (common
->flags
& BATADV_TT_CLIENT_TEMP
) {
1759 batadv_tt_global_del_orig_list(tt_global_entry
);
1760 common
->flags
&= ~BATADV_TT_CLIENT_TEMP
;
1763 /* the change can carry possible "attribute" flags like the
1764 * TT_CLIENT_TEMP, therefore they have to be copied in the
1767 if (!is_multicast_ether_addr(common
->addr
))
1768 common
->flags
|= flags
& (~BATADV_TT_SYNC_MASK
);
1770 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1771 * one originator left in the list and we previously received a
1772 * delete + roaming change for this originator.
1774 * We should first delete the old originator before adding the
1777 if (common
->flags
& BATADV_TT_CLIENT_ROAM
) {
1778 batadv_tt_global_del_orig_list(tt_global_entry
);
1779 common
->flags
&= ~BATADV_TT_CLIENT_ROAM
;
1780 tt_global_entry
->roam_at
= 0;
1784 /* add the new orig_entry (if needed) or update it */
1785 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
,
1786 flags
& BATADV_TT_SYNC_MASK
);
1788 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1789 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1790 common
->addr
, batadv_print_vid(common
->vid
),
1795 /* Do not remove multicast addresses from the local hash on
1798 if (is_multicast_ether_addr(tt_addr
))
1801 /* remove address from local hash if present */
1802 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
, vid
,
1803 "global tt received",
1804 flags
& BATADV_TT_CLIENT_ROAM
);
1805 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
1807 if (!(flags
& BATADV_TT_CLIENT_ROAM
))
1808 /* this is a normal global add. Therefore the client is not in a
1809 * roaming state anymore.
1811 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
1814 if (tt_global_entry
)
1815 batadv_tt_global_entry_put(tt_global_entry
);
1817 batadv_tt_local_entry_put(tt_local_entry
);
1822 * batadv_transtable_best_orig() - Get best originator list entry from tt entry
1823 * @bat_priv: the bat priv with all the soft interface information
1824 * @tt_global_entry: global translation table entry to be analyzed
1826 * This functon assumes the caller holds rcu_read_lock().
1827 * Return: best originator list entry or NULL on errors.
1829 static struct batadv_tt_orig_list_entry
*
1830 batadv_transtable_best_orig(struct batadv_priv
*bat_priv
,
1831 struct batadv_tt_global_entry
*tt_global_entry
)
1833 struct batadv_neigh_node
*router
, *best_router
= NULL
;
1834 struct batadv_algo_ops
*bao
= bat_priv
->algo_ops
;
1835 struct hlist_head
*head
;
1836 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
= NULL
;
1838 head
= &tt_global_entry
->orig_list
;
1839 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1840 router
= batadv_orig_router_get(orig_entry
->orig_node
,
1846 bao
->neigh
.cmp(router
, BATADV_IF_DEFAULT
, best_router
,
1847 BATADV_IF_DEFAULT
) <= 0) {
1848 batadv_neigh_node_put(router
);
1852 /* release the refcount for the "old" best */
1854 batadv_neigh_node_put(best_router
);
1856 best_entry
= orig_entry
;
1857 best_router
= router
;
1861 batadv_neigh_node_put(best_router
);
1866 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1868 * batadv_tt_global_print_entry() - print all orig nodes who announce the
1869 * address for this global entry
1870 * @bat_priv: the bat priv with all the soft interface information
1871 * @tt_global_entry: global translation table entry to be printed
1872 * @seq: debugfs table seq_file struct
1874 * This functon assumes the caller holds rcu_read_lock().
1877 batadv_tt_global_print_entry(struct batadv_priv
*bat_priv
,
1878 struct batadv_tt_global_entry
*tt_global_entry
,
1879 struct seq_file
*seq
)
1881 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
;
1882 struct batadv_tt_common_entry
*tt_common_entry
;
1883 struct batadv_orig_node_vlan
*vlan
;
1884 struct hlist_head
*head
;
1888 tt_common_entry
= &tt_global_entry
->common
;
1889 flags
= tt_common_entry
->flags
;
1891 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
1893 vlan
= batadv_orig_node_vlan_get(best_entry
->orig_node
,
1894 tt_common_entry
->vid
);
1897 " * Cannot retrieve VLAN %d for originator %pM\n",
1898 batadv_print_vid(tt_common_entry
->vid
),
1899 best_entry
->orig_node
->orig
);
1903 last_ttvn
= atomic_read(&best_entry
->orig_node
->last_ttvn
);
1905 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1906 '*', tt_global_entry
->common
.addr
,
1907 batadv_print_vid(tt_global_entry
->common
.vid
),
1908 best_entry
->ttvn
, best_entry
->orig_node
->orig
,
1909 last_ttvn
, vlan
->tt
.crc
,
1910 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1911 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1912 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1913 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1915 batadv_orig_node_vlan_put(vlan
);
1919 head
= &tt_global_entry
->orig_list
;
1921 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1922 if (best_entry
== orig_entry
)
1925 vlan
= batadv_orig_node_vlan_get(orig_entry
->orig_node
,
1926 tt_common_entry
->vid
);
1929 " + Cannot retrieve VLAN %d for originator %pM\n",
1930 batadv_print_vid(tt_common_entry
->vid
),
1931 orig_entry
->orig_node
->orig
);
1935 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
1937 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1938 '+', tt_global_entry
->common
.addr
,
1939 batadv_print_vid(tt_global_entry
->common
.vid
),
1940 orig_entry
->ttvn
, orig_entry
->orig_node
->orig
,
1941 last_ttvn
, vlan
->tt
.crc
,
1942 ((flags
& BATADV_TT_CLIENT_ROAM
) ? 'R' : '.'),
1943 ((flags
& BATADV_TT_CLIENT_WIFI
) ? 'W' : '.'),
1944 ((flags
& BATADV_TT_CLIENT_ISOLA
) ? 'I' : '.'),
1945 ((flags
& BATADV_TT_CLIENT_TEMP
) ? 'T' : '.'));
1947 batadv_orig_node_vlan_put(vlan
);
1952 * batadv_tt_global_seq_print_text() - Print the global tt table in a seq file
1953 * @seq: seq file to print on
1958 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
1960 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1961 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1962 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1963 struct batadv_tt_common_entry
*tt_common_entry
;
1964 struct batadv_tt_global_entry
*tt_global
;
1965 struct batadv_hard_iface
*primary_if
;
1966 struct hlist_head
*head
;
1969 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1974 "Globally announced TT entries received via the mesh %s\n",
1977 " Client VID (TTVN) Originator (Curr TTVN) (CRC ) Flags\n");
1979 for (i
= 0; i
< hash
->size
; i
++) {
1980 head
= &hash
->table
[i
];
1983 hlist_for_each_entry_rcu(tt_common_entry
,
1985 tt_global
= container_of(tt_common_entry
,
1986 struct batadv_tt_global_entry
,
1988 batadv_tt_global_print_entry(bat_priv
, tt_global
, seq
);
1994 batadv_hardif_put(primary_if
);
2000 * batadv_tt_global_dump_subentry() - Dump all TT local entries into a message
2001 * @msg: Netlink message to dump into
2002 * @portid: Port making netlink request
2003 * @seq: Sequence number of netlink message
2004 * @common: tt local & tt global common data
2005 * @orig: Originator node announcing a non-mesh client
2006 * @best: Is the best originator for the TT entry
2008 * Return: Error code, or 0 on success
2011 batadv_tt_global_dump_subentry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
2012 struct batadv_tt_common_entry
*common
,
2013 struct batadv_tt_orig_list_entry
*orig
,
2016 u16 flags
= (common
->flags
& (~BATADV_TT_SYNC_MASK
)) | orig
->flags
;
2018 struct batadv_orig_node_vlan
*vlan
;
2022 vlan
= batadv_orig_node_vlan_get(orig
->orig_node
,
2029 batadv_orig_node_vlan_put(vlan
);
2031 hdr
= genlmsg_put(msg
, portid
, seq
, &batadv_netlink_family
,
2033 BATADV_CMD_GET_TRANSTABLE_GLOBAL
);
2037 last_ttvn
= atomic_read(&orig
->orig_node
->last_ttvn
);
2039 if (nla_put(msg
, BATADV_ATTR_TT_ADDRESS
, ETH_ALEN
, common
->addr
) ||
2040 nla_put(msg
, BATADV_ATTR_ORIG_ADDRESS
, ETH_ALEN
,
2041 orig
->orig_node
->orig
) ||
2042 nla_put_u8(msg
, BATADV_ATTR_TT_TTVN
, orig
->ttvn
) ||
2043 nla_put_u8(msg
, BATADV_ATTR_TT_LAST_TTVN
, last_ttvn
) ||
2044 nla_put_u32(msg
, BATADV_ATTR_TT_CRC32
, crc
) ||
2045 nla_put_u16(msg
, BATADV_ATTR_TT_VID
, common
->vid
) ||
2046 nla_put_u32(msg
, BATADV_ATTR_TT_FLAGS
, flags
))
2047 goto nla_put_failure
;
2049 if (best
&& nla_put_flag(msg
, BATADV_ATTR_FLAG_BEST
))
2050 goto nla_put_failure
;
2052 genlmsg_end(msg
, hdr
);
2056 genlmsg_cancel(msg
, hdr
);
2061 * batadv_tt_global_dump_entry() - Dump one TT global entry into a message
2062 * @msg: Netlink message to dump into
2063 * @portid: Port making netlink request
2064 * @seq: Sequence number of netlink message
2065 * @bat_priv: The bat priv with all the soft interface information
2066 * @common: tt local & tt global common data
2067 * @sub_s: Number of entries to skip
2069 * This function assumes the caller holds rcu_read_lock().
2071 * Return: Error code, or 0 on success
2074 batadv_tt_global_dump_entry(struct sk_buff
*msg
, u32 portid
, u32 seq
,
2075 struct batadv_priv
*bat_priv
,
2076 struct batadv_tt_common_entry
*common
, int *sub_s
)
2078 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
;
2079 struct batadv_tt_global_entry
*global
;
2080 struct hlist_head
*head
;
2084 global
= container_of(common
, struct batadv_tt_global_entry
, common
);
2085 best_entry
= batadv_transtable_best_orig(bat_priv
, global
);
2086 head
= &global
->orig_list
;
2088 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
2092 best
= (orig_entry
== best_entry
);
2094 if (batadv_tt_global_dump_subentry(msg
, portid
, seq
, common
,
2095 orig_entry
, best
)) {
2106 * batadv_tt_global_dump_bucket() - Dump one TT local bucket into a message
2107 * @msg: Netlink message to dump into
2108 * @portid: Port making netlink request
2109 * @seq: Sequence number of netlink message
2110 * @bat_priv: The bat priv with all the soft interface information
2111 * @head: Pointer to the list containing the global tt entries
2112 * @idx_s: Number of entries to skip
2113 * @sub: Number of entries to skip
2115 * Return: Error code, or 0 on success
2118 batadv_tt_global_dump_bucket(struct sk_buff
*msg
, u32 portid
, u32 seq
,
2119 struct batadv_priv
*bat_priv
,
2120 struct hlist_head
*head
, int *idx_s
, int *sub
)
2122 struct batadv_tt_common_entry
*common
;
2126 hlist_for_each_entry_rcu(common
, head
, hash_entry
) {
2130 if (batadv_tt_global_dump_entry(msg
, portid
, seq
, bat_priv
,
2145 * batadv_tt_global_dump() - Dump TT global entries into a message
2146 * @msg: Netlink message to dump into
2147 * @cb: Parameters from query
2149 * Return: Error code, or length of message on success
2151 int batadv_tt_global_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
)
2153 struct net
*net
= sock_net(cb
->skb
->sk
);
2154 struct net_device
*soft_iface
;
2155 struct batadv_priv
*bat_priv
;
2156 struct batadv_hard_iface
*primary_if
= NULL
;
2157 struct batadv_hashtable
*hash
;
2158 struct hlist_head
*head
;
2161 int bucket
= cb
->args
[0];
2162 int idx
= cb
->args
[1];
2163 int sub
= cb
->args
[2];
2164 int portid
= NETLINK_CB(cb
->skb
).portid
;
2166 ifindex
= batadv_netlink_get_ifindex(cb
->nlh
, BATADV_ATTR_MESH_IFINDEX
);
2170 soft_iface
= dev_get_by_index(net
, ifindex
);
2171 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
2176 bat_priv
= netdev_priv(soft_iface
);
2178 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2179 if (!primary_if
|| primary_if
->if_status
!= BATADV_IF_ACTIVE
) {
2184 hash
= bat_priv
->tt
.global_hash
;
2186 while (bucket
< hash
->size
) {
2187 head
= &hash
->table
[bucket
];
2189 if (batadv_tt_global_dump_bucket(msg
, portid
,
2190 cb
->nlh
->nlmsg_seq
, bat_priv
,
2201 batadv_hardif_put(primary_if
);
2203 dev_put(soft_iface
);
2205 cb
->args
[0] = bucket
;
2213 * _batadv_tt_global_del_orig_entry() - remove and free an orig_entry
2214 * @tt_global_entry: the global entry to remove the orig_entry from
2215 * @orig_entry: the orig entry to remove and free
2217 * Remove an orig_entry from its list in the given tt_global_entry and
2218 * free this orig_entry afterwards.
2220 * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2224 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry
*tt_global_entry
,
2225 struct batadv_tt_orig_list_entry
*orig_entry
)
2227 lockdep_assert_held(&tt_global_entry
->list_lock
);
2229 batadv_tt_global_size_dec(orig_entry
->orig_node
,
2230 tt_global_entry
->common
.vid
);
2231 atomic_dec(&tt_global_entry
->orig_list_count
);
2232 /* requires holding tt_global_entry->list_lock and orig_entry->list
2233 * being part of a list
2235 hlist_del_rcu(&orig_entry
->list
);
2236 batadv_tt_orig_list_entry_put(orig_entry
);
2239 /* deletes the orig list of a tt_global_entry */
2241 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
2243 struct hlist_head
*head
;
2244 struct hlist_node
*safe
;
2245 struct batadv_tt_orig_list_entry
*orig_entry
;
2247 spin_lock_bh(&tt_global_entry
->list_lock
);
2248 head
= &tt_global_entry
->orig_list
;
2249 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
)
2250 _batadv_tt_global_del_orig_entry(tt_global_entry
, orig_entry
);
2251 spin_unlock_bh(&tt_global_entry
->list_lock
);
2255 * batadv_tt_global_del_orig_node() - remove orig_node from a global tt entry
2256 * @bat_priv: the bat priv with all the soft interface information
2257 * @tt_global_entry: the global entry to remove the orig_node from
2258 * @orig_node: the originator announcing the client
2259 * @message: message to append to the log on deletion
2261 * Remove the given orig_node and its according orig_entry from the given
2265 batadv_tt_global_del_orig_node(struct batadv_priv
*bat_priv
,
2266 struct batadv_tt_global_entry
*tt_global_entry
,
2267 struct batadv_orig_node
*orig_node
,
2268 const char *message
)
2270 struct hlist_head
*head
;
2271 struct hlist_node
*safe
;
2272 struct batadv_tt_orig_list_entry
*orig_entry
;
2275 spin_lock_bh(&tt_global_entry
->list_lock
);
2276 head
= &tt_global_entry
->orig_list
;
2277 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
) {
2278 if (orig_entry
->orig_node
== orig_node
) {
2279 vid
= tt_global_entry
->common
.vid
;
2280 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2281 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2283 tt_global_entry
->common
.addr
,
2284 batadv_print_vid(vid
), message
);
2285 _batadv_tt_global_del_orig_entry(tt_global_entry
,
2289 spin_unlock_bh(&tt_global_entry
->list_lock
);
2292 /* If the client is to be deleted, we check if it is the last origantor entry
2293 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2294 * timer, otherwise we simply remove the originator scheduled for deletion.
2297 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
2298 struct batadv_tt_global_entry
*tt_global_entry
,
2299 struct batadv_orig_node
*orig_node
,
2300 const char *message
)
2302 bool last_entry
= true;
2303 struct hlist_head
*head
;
2304 struct batadv_tt_orig_list_entry
*orig_entry
;
2306 /* no local entry exists, case 1:
2307 * Check if this is the last one or if other entries exist.
2311 head
= &tt_global_entry
->orig_list
;
2312 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
2313 if (orig_entry
->orig_node
!= orig_node
) {
2321 /* its the last one, mark for roaming. */
2322 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
2323 tt_global_entry
->roam_at
= jiffies
;
2325 /* there is another entry, we can simply delete this
2326 * one and can still use the other one.
2328 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
2329 orig_node
, message
);
2334 * batadv_tt_global_del() - remove a client from the global table
2335 * @bat_priv: the bat priv with all the soft interface information
2336 * @orig_node: an originator serving this client
2337 * @addr: the mac address of the client
2338 * @vid: VLAN identifier
2339 * @message: a message explaining the reason for deleting the client to print
2340 * for debugging purpose
2341 * @roaming: true if the deletion has been triggered by a roaming event
2343 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
2344 struct batadv_orig_node
*orig_node
,
2345 const unsigned char *addr
, unsigned short vid
,
2346 const char *message
, bool roaming
)
2348 struct batadv_tt_global_entry
*tt_global_entry
;
2349 struct batadv_tt_local_entry
*local_entry
= NULL
;
2351 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
2352 if (!tt_global_entry
)
2356 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
2357 orig_node
, message
);
2359 if (hlist_empty(&tt_global_entry
->orig_list
))
2360 batadv_tt_global_free(bat_priv
, tt_global_entry
,
2366 /* if we are deleting a global entry due to a roam
2367 * event, there are two possibilities:
2368 * 1) the client roamed from node A to node B => if there
2369 * is only one originator left for this client, we mark
2370 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
2371 * wait for node B to claim it. In case of timeout
2372 * the entry is purged.
2374 * If there are other originators left, we directly delete
2376 * 2) the client roamed to us => we can directly delete
2377 * the global entry, since it is useless now.
2379 local_entry
= batadv_tt_local_hash_find(bat_priv
,
2380 tt_global_entry
->common
.addr
,
2383 /* local entry exists, case 2: client roamed to us. */
2384 batadv_tt_global_del_orig_list(tt_global_entry
);
2385 batadv_tt_global_free(bat_priv
, tt_global_entry
, message
);
2387 /* no local entry exists, case 1: check for roaming */
2388 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
2389 orig_node
, message
);
2393 if (tt_global_entry
)
2394 batadv_tt_global_entry_put(tt_global_entry
);
2396 batadv_tt_local_entry_put(local_entry
);
2400 * batadv_tt_global_del_orig() - remove all the TT global entries belonging to
2401 * the given originator matching the provided vid
2402 * @bat_priv: the bat priv with all the soft interface information
2403 * @orig_node: the originator owning the entries to remove
2404 * @match_vid: the VLAN identifier to match. If negative all the entries will be
2406 * @message: debug message to print as "reason"
2408 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
2409 struct batadv_orig_node
*orig_node
,
2411 const char *message
)
2413 struct batadv_tt_global_entry
*tt_global
;
2414 struct batadv_tt_common_entry
*tt_common_entry
;
2416 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2417 struct hlist_node
*safe
;
2418 struct hlist_head
*head
;
2419 spinlock_t
*list_lock
; /* protects write access to the hash lists */
2425 for (i
= 0; i
< hash
->size
; i
++) {
2426 head
= &hash
->table
[i
];
2427 list_lock
= &hash
->list_locks
[i
];
2429 spin_lock_bh(list_lock
);
2430 hlist_for_each_entry_safe(tt_common_entry
, safe
,
2432 /* remove only matching entries */
2433 if (match_vid
>= 0 && tt_common_entry
->vid
!= match_vid
)
2436 tt_global
= container_of(tt_common_entry
,
2437 struct batadv_tt_global_entry
,
2440 batadv_tt_global_del_orig_node(bat_priv
, tt_global
,
2441 orig_node
, message
);
2443 if (hlist_empty(&tt_global
->orig_list
)) {
2444 vid
= tt_global
->common
.vid
;
2445 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2446 "Deleting global tt entry %pM (vid: %d): %s\n",
2447 tt_global
->common
.addr
,
2448 batadv_print_vid(vid
), message
);
2449 hlist_del_rcu(&tt_common_entry
->hash_entry
);
2450 batadv_tt_global_entry_put(tt_global
);
2453 spin_unlock_bh(list_lock
);
2455 clear_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
2458 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
2462 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
2463 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
2465 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
2466 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
2468 *msg
= "Roaming timeout\n";
2471 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
2472 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
2474 *msg
= "Temporary client timeout\n";
2480 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
2482 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2483 struct hlist_head
*head
;
2484 struct hlist_node
*node_tmp
;
2485 spinlock_t
*list_lock
; /* protects write access to the hash lists */
2488 struct batadv_tt_common_entry
*tt_common
;
2489 struct batadv_tt_global_entry
*tt_global
;
2491 for (i
= 0; i
< hash
->size
; i
++) {
2492 head
= &hash
->table
[i
];
2493 list_lock
= &hash
->list_locks
[i
];
2495 spin_lock_bh(list_lock
);
2496 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
2498 tt_global
= container_of(tt_common
,
2499 struct batadv_tt_global_entry
,
2502 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
2505 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2506 "Deleting global tt entry %pM (vid: %d): %s\n",
2507 tt_global
->common
.addr
,
2508 batadv_print_vid(tt_global
->common
.vid
),
2511 hlist_del_rcu(&tt_common
->hash_entry
);
2513 batadv_tt_global_entry_put(tt_global
);
2515 spin_unlock_bh(list_lock
);
2519 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
2521 struct batadv_hashtable
*hash
;
2522 spinlock_t
*list_lock
; /* protects write access to the hash lists */
2523 struct batadv_tt_common_entry
*tt_common_entry
;
2524 struct batadv_tt_global_entry
*tt_global
;
2525 struct hlist_node
*node_tmp
;
2526 struct hlist_head
*head
;
2529 if (!bat_priv
->tt
.global_hash
)
2532 hash
= bat_priv
->tt
.global_hash
;
2534 for (i
= 0; i
< hash
->size
; i
++) {
2535 head
= &hash
->table
[i
];
2536 list_lock
= &hash
->list_locks
[i
];
2538 spin_lock_bh(list_lock
);
2539 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
2541 hlist_del_rcu(&tt_common_entry
->hash_entry
);
2542 tt_global
= container_of(tt_common_entry
,
2543 struct batadv_tt_global_entry
,
2545 batadv_tt_global_entry_put(tt_global
);
2547 spin_unlock_bh(list_lock
);
2550 batadv_hash_destroy(hash
);
2552 bat_priv
->tt
.global_hash
= NULL
;
2556 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
2557 struct batadv_tt_global_entry
*tt_global_entry
)
2559 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
2560 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
2563 /* check if the two clients are marked as isolated */
2564 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
&&
2565 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
)
2572 * batadv_transtable_search() - get the mesh destination for a given client
2573 * @bat_priv: the bat priv with all the soft interface information
2574 * @src: mac address of the source client
2575 * @addr: mac address of the destination client
2576 * @vid: VLAN identifier
2578 * Return: a pointer to the originator that was selected as destination in the
2579 * mesh for contacting the client 'addr', NULL otherwise.
2580 * In case of multiple originators serving the same client, the function returns
2581 * the best one (best in terms of metric towards the destination node).
2583 * If the two clients are AP isolated the function returns NULL.
2585 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
2590 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2591 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2592 struct batadv_orig_node
*orig_node
= NULL
;
2593 struct batadv_tt_orig_list_entry
*best_entry
;
2595 if (src
&& batadv_vlan_ap_isola_get(bat_priv
, vid
)) {
2596 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
, vid
);
2597 if (!tt_local_entry
||
2598 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
))
2602 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
2603 if (!tt_global_entry
)
2606 /* check whether the clients should not communicate due to AP
2609 if (tt_local_entry
&&
2610 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2614 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
2615 /* found anything? */
2617 orig_node
= best_entry
->orig_node
;
2618 if (orig_node
&& !kref_get_unless_zero(&orig_node
->refcount
))
2623 if (tt_global_entry
)
2624 batadv_tt_global_entry_put(tt_global_entry
);
2626 batadv_tt_local_entry_put(tt_local_entry
);
2632 * batadv_tt_global_crc() - calculates the checksum of the local table belonging
2633 * to the given orig_node
2634 * @bat_priv: the bat priv with all the soft interface information
2635 * @orig_node: originator for which the CRC should be computed
2636 * @vid: VLAN identifier for which the CRC32 has to be computed
2638 * This function computes the checksum for the global table corresponding to a
2639 * specific originator. In particular, the checksum is computed as follows: For
2640 * each client connected to the originator the CRC32C of the MAC address and the
2641 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2644 * The idea behind is that CRC32C should be used as much as possible in order to
2645 * produce a unique hash of the table, but since the order which is used to feed
2646 * the CRC32C function affects the result and since every node in the network
2647 * probably sorts the clients differently, the hash function cannot be directly
2648 * computed over the entire table. Hence the CRC32C is used only on
2649 * the single client entry, while all the results are then xor'ed together
2650 * because the XOR operation can combine them all while trying to reduce the
2651 * noise as much as possible.
2653 * Return: the checksum of the global table of a given originator.
2655 static u32
batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
2656 struct batadv_orig_node
*orig_node
,
2659 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2660 struct batadv_tt_orig_list_entry
*tt_orig
;
2661 struct batadv_tt_common_entry
*tt_common
;
2662 struct batadv_tt_global_entry
*tt_global
;
2663 struct hlist_head
*head
;
2664 u32 i
, crc_tmp
, crc
= 0;
2668 for (i
= 0; i
< hash
->size
; i
++) {
2669 head
= &hash
->table
[i
];
2672 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2673 tt_global
= container_of(tt_common
,
2674 struct batadv_tt_global_entry
,
2676 /* compute the CRC only for entries belonging to the
2677 * VLAN identified by the vid passed as parameter
2679 if (tt_common
->vid
!= vid
)
2682 /* Roaming clients are in the global table for
2683 * consistency only. They don't have to be
2684 * taken into account while computing the
2687 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
2689 /* Temporary clients have not been announced yet, so
2690 * they have to be skipped while computing the global
2693 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
2696 /* find out if this global entry is announced by this
2699 tt_orig
= batadv_tt_global_orig_entry_find(tt_global
,
2704 /* use network order to read the VID: this ensures that
2705 * every node reads the bytes in the same order.
2707 tmp_vid
= htons(tt_common
->vid
);
2708 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2710 /* compute the CRC on flags that have to be kept in sync
2713 flags
= tt_orig
->flags
;
2714 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2716 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2718 batadv_tt_orig_list_entry_put(tt_orig
);
2727 * batadv_tt_local_crc() - calculates the checksum of the local table
2728 * @bat_priv: the bat priv with all the soft interface information
2729 * @vid: VLAN identifier for which the CRC32 has to be computed
2731 * For details about the computation, please refer to the documentation for
2732 * batadv_tt_global_crc().
2734 * Return: the checksum of the local table
2736 static u32
batadv_tt_local_crc(struct batadv_priv
*bat_priv
,
2739 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2740 struct batadv_tt_common_entry
*tt_common
;
2741 struct hlist_head
*head
;
2742 u32 i
, crc_tmp
, crc
= 0;
2746 for (i
= 0; i
< hash
->size
; i
++) {
2747 head
= &hash
->table
[i
];
2750 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2751 /* compute the CRC only for entries belonging to the
2752 * VLAN identified by vid
2754 if (tt_common
->vid
!= vid
)
2757 /* not yet committed clients have not to be taken into
2758 * account while computing the CRC
2760 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
2763 /* use network order to read the VID: this ensures that
2764 * every node reads the bytes in the same order.
2766 tmp_vid
= htons(tt_common
->vid
);
2767 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2769 /* compute the CRC on flags that have to be kept in sync
2772 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2773 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2775 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2784 * batadv_tt_req_node_release() - free tt_req node entry
2785 * @ref: kref pointer of the tt req_node entry
2787 static void batadv_tt_req_node_release(struct kref
*ref
)
2789 struct batadv_tt_req_node
*tt_req_node
;
2791 tt_req_node
= container_of(ref
, struct batadv_tt_req_node
, refcount
);
2793 kmem_cache_free(batadv_tt_req_cache
, tt_req_node
);
2797 * batadv_tt_req_node_put() - decrement the tt_req_node refcounter and
2798 * possibly release it
2799 * @tt_req_node: tt_req_node to be free'd
2801 static void batadv_tt_req_node_put(struct batadv_tt_req_node
*tt_req_node
)
2803 kref_put(&tt_req_node
->refcount
, batadv_tt_req_node_release
);
2806 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
2808 struct batadv_tt_req_node
*node
;
2809 struct hlist_node
*safe
;
2811 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2813 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2814 hlist_del_init(&node
->list
);
2815 batadv_tt_req_node_put(node
);
2818 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2821 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
2822 struct batadv_orig_node
*orig_node
,
2823 const void *tt_buff
,
2826 /* Replace the old buffer only if I received something in the
2827 * last OGM (the OGM could carry no changes)
2829 spin_lock_bh(&orig_node
->tt_buff_lock
);
2830 if (tt_buff_len
> 0) {
2831 kfree(orig_node
->tt_buff
);
2832 orig_node
->tt_buff_len
= 0;
2833 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
2834 if (orig_node
->tt_buff
) {
2835 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
2836 orig_node
->tt_buff_len
= tt_buff_len
;
2839 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2842 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
2844 struct batadv_tt_req_node
*node
;
2845 struct hlist_node
*safe
;
2847 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2848 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2849 if (batadv_has_timed_out(node
->issued_at
,
2850 BATADV_TT_REQUEST_TIMEOUT
)) {
2851 hlist_del_init(&node
->list
);
2852 batadv_tt_req_node_put(node
);
2855 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2859 * batadv_tt_req_node_new() - search and possibly create a tt_req_node object
2860 * @bat_priv: the bat priv with all the soft interface information
2861 * @orig_node: orig node this request is being issued for
2863 * Return: the pointer to the new tt_req_node struct if no request
2864 * has already been issued for this orig_node, NULL otherwise.
2866 static struct batadv_tt_req_node
*
2867 batadv_tt_req_node_new(struct batadv_priv
*bat_priv
,
2868 struct batadv_orig_node
*orig_node
)
2870 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
2872 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2873 hlist_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
2874 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
2875 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
2876 BATADV_TT_REQUEST_TIMEOUT
))
2880 tt_req_node
= kmem_cache_alloc(batadv_tt_req_cache
, GFP_ATOMIC
);
2884 kref_init(&tt_req_node
->refcount
);
2885 ether_addr_copy(tt_req_node
->addr
, orig_node
->orig
);
2886 tt_req_node
->issued_at
= jiffies
;
2888 kref_get(&tt_req_node
->refcount
);
2889 hlist_add_head(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
2891 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2896 * batadv_tt_local_valid() - verify local tt entry and get flags
2897 * @entry_ptr: to be checked local tt entry
2898 * @data_ptr: not used but definition required to satisfy the callback prototype
2899 * @flags: a pointer to store TT flags for this client to
2901 * Checks the validity of the given local TT entry. If it is, then the provided
2902 * flags pointer is updated.
2904 * Return: true if the entry is a valid, false otherwise.
2906 static bool batadv_tt_local_valid(const void *entry_ptr
,
2907 const void *data_ptr
,
2910 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2912 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
2916 *flags
= tt_common_entry
->flags
;
2922 * batadv_tt_global_valid() - verify global tt entry and get flags
2923 * @entry_ptr: to be checked global tt entry
2924 * @data_ptr: an orig_node object (may be NULL)
2925 * @flags: a pointer to store TT flags for this client to
2927 * Checks the validity of the given global TT entry. If it is, then the provided
2928 * flags pointer is updated either with the common (summed) TT flags if data_ptr
2929 * is NULL or the specific, per originator TT flags otherwise.
2931 * Return: true if the entry is a valid, false otherwise.
2933 static bool batadv_tt_global_valid(const void *entry_ptr
,
2934 const void *data_ptr
,
2937 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2938 const struct batadv_tt_global_entry
*tt_global_entry
;
2939 const struct batadv_orig_node
*orig_node
= data_ptr
;
2941 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
2942 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
2945 tt_global_entry
= container_of(tt_common_entry
,
2946 struct batadv_tt_global_entry
,
2949 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
,
2954 * batadv_tt_tvlv_generate() - fill the tvlv buff with the tt entries from the
2956 * @bat_priv: the bat priv with all the soft interface information
2957 * @hash: hash table containing the tt entries
2958 * @tt_len: expected tvlv tt data buffer length in number of bytes
2959 * @tvlv_buff: pointer to the buffer to fill with the TT data
2960 * @valid_cb: function to filter tt change entries and to return TT flags
2961 * @cb_data: data passed to the filter function as argument
2963 * Fills the tvlv buff with the tt entries from the specified hash. If valid_cb
2964 * is not provided then this becomes a no-op.
2966 static void batadv_tt_tvlv_generate(struct batadv_priv
*bat_priv
,
2967 struct batadv_hashtable
*hash
,
2968 void *tvlv_buff
, u16 tt_len
,
2969 bool (*valid_cb
)(const void *,
2974 struct batadv_tt_common_entry
*tt_common_entry
;
2975 struct batadv_tvlv_tt_change
*tt_change
;
2976 struct hlist_head
*head
;
2977 u16 tt_tot
, tt_num_entries
= 0;
2982 tt_tot
= batadv_tt_entries(tt_len
);
2983 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_buff
;
2989 for (i
= 0; i
< hash
->size
; i
++) {
2990 head
= &hash
->table
[i
];
2992 hlist_for_each_entry_rcu(tt_common_entry
,
2994 if (tt_tot
== tt_num_entries
)
2997 ret
= valid_cb(tt_common_entry
, cb_data
, &flags
);
3001 ether_addr_copy(tt_change
->addr
, tt_common_entry
->addr
);
3002 tt_change
->flags
= flags
;
3003 tt_change
->vid
= htons(tt_common_entry
->vid
);
3004 memset(tt_change
->reserved
, 0,
3005 sizeof(tt_change
->reserved
));
3015 * batadv_tt_global_check_crc() - check if all the CRCs are correct
3016 * @orig_node: originator for which the CRCs have to be checked
3017 * @tt_vlan: pointer to the first tvlv VLAN entry
3018 * @num_vlan: number of tvlv VLAN entries
3020 * Return: true if all the received CRCs match the locally stored ones, false
3023 static bool batadv_tt_global_check_crc(struct batadv_orig_node
*orig_node
,
3024 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
3027 struct batadv_tvlv_tt_vlan_data
*tt_vlan_tmp
;
3028 struct batadv_orig_node_vlan
*vlan
;
3029 int i
, orig_num_vlan
;
3032 /* check if each received CRC matches the locally stored one */
3033 for (i
= 0; i
< num_vlan
; i
++) {
3034 tt_vlan_tmp
= tt_vlan
+ i
;
3036 /* if orig_node is a backbone node for this VLAN, don't check
3037 * the CRC as we ignore all the global entries over it
3039 if (batadv_bla_is_backbone_gw_orig(orig_node
->bat_priv
,
3041 ntohs(tt_vlan_tmp
->vid
)))
3044 vlan
= batadv_orig_node_vlan_get(orig_node
,
3045 ntohs(tt_vlan_tmp
->vid
));
3050 batadv_orig_node_vlan_put(vlan
);
3052 if (crc
!= ntohl(tt_vlan_tmp
->crc
))
3056 /* check if any excess VLANs exist locally for the originator
3057 * which are not mentioned in the TVLV from the originator.
3061 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
)
3065 if (orig_num_vlan
> num_vlan
)
3072 * batadv_tt_local_update_crc() - update all the local CRCs
3073 * @bat_priv: the bat priv with all the soft interface information
3075 static void batadv_tt_local_update_crc(struct batadv_priv
*bat_priv
)
3077 struct batadv_softif_vlan
*vlan
;
3079 /* recompute the global CRC for each VLAN */
3081 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
3082 vlan
->tt
.crc
= batadv_tt_local_crc(bat_priv
, vlan
->vid
);
3088 * batadv_tt_global_update_crc() - update all the global CRCs for this orig_node
3089 * @bat_priv: the bat priv with all the soft interface information
3090 * @orig_node: the orig_node for which the CRCs have to be updated
3092 static void batadv_tt_global_update_crc(struct batadv_priv
*bat_priv
,
3093 struct batadv_orig_node
*orig_node
)
3095 struct batadv_orig_node_vlan
*vlan
;
3098 /* recompute the global CRC for each VLAN */
3100 hlist_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
3101 /* if orig_node is a backbone node for this VLAN, don't compute
3102 * the CRC as we ignore all the global entries over it
3104 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
,
3108 crc
= batadv_tt_global_crc(bat_priv
, orig_node
, vlan
->vid
);
3115 * batadv_send_tt_request() - send a TT Request message to a given node
3116 * @bat_priv: the bat priv with all the soft interface information
3117 * @dst_orig_node: the destination of the message
3118 * @ttvn: the version number that the source of the message is looking for
3119 * @tt_vlan: pointer to the first tvlv VLAN object to request
3120 * @num_vlan: number of tvlv VLAN entries
3121 * @full_table: ask for the entire translation table if true, while only for the
3122 * last TT diff otherwise
3124 * Return: true if the TT Request was sent, false otherwise
3126 static bool batadv_send_tt_request(struct batadv_priv
*bat_priv
,
3127 struct batadv_orig_node
*dst_orig_node
,
3129 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
3130 u16 num_vlan
, bool full_table
)
3132 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
3133 struct batadv_tt_req_node
*tt_req_node
= NULL
;
3134 struct batadv_tvlv_tt_vlan_data
*tt_vlan_req
;
3135 struct batadv_hard_iface
*primary_if
;
3139 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3143 /* The new tt_req will be issued only if I'm not waiting for a
3144 * reply from the same orig_node yet
3146 tt_req_node
= batadv_tt_req_node_new(bat_priv
, dst_orig_node
);
3150 size
= sizeof(*tvlv_tt_data
) + sizeof(*tt_vlan_req
) * num_vlan
;
3151 tvlv_tt_data
= kzalloc(size
, GFP_ATOMIC
);
3155 tvlv_tt_data
->flags
= BATADV_TT_REQUEST
;
3156 tvlv_tt_data
->ttvn
= ttvn
;
3157 tvlv_tt_data
->num_vlan
= htons(num_vlan
);
3159 /* send all the CRCs within the request. This is needed by intermediate
3160 * nodes to ensure they have the correct table before replying
3162 tt_vlan_req
= (struct batadv_tvlv_tt_vlan_data
*)(tvlv_tt_data
+ 1);
3163 for (i
= 0; i
< num_vlan
; i
++) {
3164 tt_vlan_req
->vid
= tt_vlan
->vid
;
3165 tt_vlan_req
->crc
= tt_vlan
->crc
;
3172 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
3174 batadv_dbg(BATADV_DBG_TT
, bat_priv
, "Sending TT_REQUEST to %pM [%c]\n",
3175 dst_orig_node
->orig
, full_table
? 'F' : '.');
3177 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
3178 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3179 dst_orig_node
->orig
, BATADV_TVLV_TT
, 1,
3180 tvlv_tt_data
, size
);
3185 batadv_hardif_put(primary_if
);
3187 if (ret
&& tt_req_node
) {
3188 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
3189 if (!hlist_unhashed(&tt_req_node
->list
)) {
3190 hlist_del_init(&tt_req_node
->list
);
3191 batadv_tt_req_node_put(tt_req_node
);
3193 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
3197 batadv_tt_req_node_put(tt_req_node
);
3199 kfree(tvlv_tt_data
);
3204 * batadv_send_other_tt_response() - send reply to tt request concerning another
3205 * node's translation table
3206 * @bat_priv: the bat priv with all the soft interface information
3207 * @tt_data: tt data containing the tt request information
3208 * @req_src: mac address of tt request sender
3209 * @req_dst: mac address of tt request recipient
3211 * Return: true if tt request reply was sent, false otherwise.
3213 static bool batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
3214 struct batadv_tvlv_tt_data
*tt_data
,
3215 u8
*req_src
, u8
*req_dst
)
3217 struct batadv_orig_node
*req_dst_orig_node
;
3218 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
3219 struct batadv_tvlv_tt_change
*tt_change
;
3220 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
3221 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3222 bool ret
= false, full_table
;
3223 u8 orig_ttvn
, req_ttvn
;
3227 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3228 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
3229 req_src
, tt_data
->ttvn
, req_dst
,
3230 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
3232 /* Let's get the orig node of the REAL destination */
3233 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_dst
);
3234 if (!req_dst_orig_node
)
3237 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
3238 if (!res_dst_orig_node
)
3241 orig_ttvn
= (u8
)atomic_read(&req_dst_orig_node
->last_ttvn
);
3242 req_ttvn
= tt_data
->ttvn
;
3244 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
3245 /* this node doesn't have the requested data */
3246 if (orig_ttvn
!= req_ttvn
||
3247 !batadv_tt_global_check_crc(req_dst_orig_node
, tt_vlan
,
3248 ntohs(tt_data
->num_vlan
)))
3251 /* If the full table has been explicitly requested */
3252 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
||
3253 !req_dst_orig_node
->tt_buff
)
3258 /* TT fragmentation hasn't been implemented yet, so send as many
3259 * TT entries fit a single packet as possible only
3262 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
3263 tt_len
= req_dst_orig_node
->tt_buff_len
;
3265 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
3272 /* Copy the last orig_node's OGM buffer */
3273 memcpy(tt_change
, req_dst_orig_node
->tt_buff
,
3274 req_dst_orig_node
->tt_buff_len
);
3275 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
3277 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3278 * in the initial part
3281 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
3288 /* fill the rest of the tvlv with the real TT entries */
3289 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.global_hash
,
3291 batadv_tt_global_valid
,
3295 /* Don't send the response, if larger than fragmented packet. */
3296 tt_len
= sizeof(struct batadv_unicast_tvlv_packet
) + tvlv_len
;
3297 if (tt_len
> atomic_read(&bat_priv
->packet_size_max
)) {
3298 net_ratelimited_function(batadv_info
, bat_priv
->soft_iface
,
3299 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3300 res_dst_orig_node
->orig
);
3304 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
3305 tvlv_tt_data
->ttvn
= req_ttvn
;
3308 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
3310 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3311 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3312 res_dst_orig_node
->orig
, req_dst_orig_node
->orig
,
3313 full_table
? 'F' : '.', req_ttvn
);
3315 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
3317 batadv_tvlv_unicast_send(bat_priv
, req_dst_orig_node
->orig
,
3318 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
3325 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
3328 if (res_dst_orig_node
)
3329 batadv_orig_node_put(res_dst_orig_node
);
3330 if (req_dst_orig_node
)
3331 batadv_orig_node_put(req_dst_orig_node
);
3332 kfree(tvlv_tt_data
);
3337 * batadv_send_my_tt_response() - send reply to tt request concerning this
3338 * node's translation table
3339 * @bat_priv: the bat priv with all the soft interface information
3340 * @tt_data: tt data containing the tt request information
3341 * @req_src: mac address of tt request sender
3343 * Return: true if tt request reply was sent, false otherwise.
3345 static bool batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
3346 struct batadv_tvlv_tt_data
*tt_data
,
3349 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
3350 struct batadv_hard_iface
*primary_if
= NULL
;
3351 struct batadv_tvlv_tt_change
*tt_change
;
3352 struct batadv_orig_node
*orig_node
;
3353 u8 my_ttvn
, req_ttvn
;
3358 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3359 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3360 req_src
, tt_data
->ttvn
,
3361 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
3363 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3365 my_ttvn
= (u8
)atomic_read(&bat_priv
->tt
.vn
);
3366 req_ttvn
= tt_data
->ttvn
;
3368 orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
3372 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3376 /* If the full table has been explicitly requested or the gap
3377 * is too big send the whole local translation table
3379 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
3380 !bat_priv
->tt
.last_changeset
)
3385 /* TT fragmentation hasn't been implemented yet, so send as many
3386 * TT entries fit a single packet as possible only
3389 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
3391 tt_len
= bat_priv
->tt
.last_changeset_len
;
3392 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
3396 if (!tt_len
|| !tvlv_len
)
3399 /* Copy the last orig_node's OGM buffer */
3400 memcpy(tt_change
, bat_priv
->tt
.last_changeset
,
3401 bat_priv
->tt
.last_changeset_len
);
3402 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
3404 req_ttvn
= (u8
)atomic_read(&bat_priv
->tt
.vn
);
3406 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3407 * in the initial part
3410 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
3414 if (!tt_len
|| !tvlv_len
)
3417 /* fill the rest of the tvlv with the real TT entries */
3418 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.local_hash
,
3420 batadv_tt_local_valid
, NULL
);
3423 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
3424 tvlv_tt_data
->ttvn
= req_ttvn
;
3427 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
3429 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3430 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3431 orig_node
->orig
, full_table
? 'F' : '.', req_ttvn
);
3433 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
3435 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3436 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
3442 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
3444 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3446 batadv_orig_node_put(orig_node
);
3448 batadv_hardif_put(primary_if
);
3449 kfree(tvlv_tt_data
);
3450 /* The packet was for this host, so it doesn't need to be re-routed */
3455 * batadv_send_tt_response() - send reply to tt request
3456 * @bat_priv: the bat priv with all the soft interface information
3457 * @tt_data: tt data containing the tt request information
3458 * @req_src: mac address of tt request sender
3459 * @req_dst: mac address of tt request recipient
3461 * Return: true if tt request reply was sent, false otherwise.
3463 static bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
3464 struct batadv_tvlv_tt_data
*tt_data
,
3465 u8
*req_src
, u8
*req_dst
)
3467 if (batadv_is_my_mac(bat_priv
, req_dst
))
3468 return batadv_send_my_tt_response(bat_priv
, tt_data
, req_src
);
3469 return batadv_send_other_tt_response(bat_priv
, tt_data
, req_src
,
3473 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
3474 struct batadv_orig_node
*orig_node
,
3475 struct batadv_tvlv_tt_change
*tt_change
,
3476 u16 tt_num_changes
, u8 ttvn
)
3481 for (i
= 0; i
< tt_num_changes
; i
++) {
3482 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
3483 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
3484 batadv_tt_global_del(bat_priv
, orig_node
,
3485 (tt_change
+ i
)->addr
,
3486 ntohs((tt_change
+ i
)->vid
),
3487 "tt removed by changes",
3490 if (!batadv_tt_global_add(bat_priv
, orig_node
,
3491 (tt_change
+ i
)->addr
,
3492 ntohs((tt_change
+ i
)->vid
),
3493 (tt_change
+ i
)->flags
, ttvn
))
3494 /* In case of problem while storing a
3495 * global_entry, we stop the updating
3496 * procedure without committing the
3497 * ttvn change. This will avoid to send
3498 * corrupted data on tt_request
3503 set_bit(BATADV_ORIG_CAPA_HAS_TT
, &orig_node
->capa_initialized
);
3506 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
3507 struct batadv_tvlv_tt_change
*tt_change
,
3508 u8 ttvn
, u8
*resp_src
,
3511 struct batadv_orig_node
*orig_node
;
3513 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
3517 /* Purge the old table first.. */
3518 batadv_tt_global_del_orig(bat_priv
, orig_node
, -1,
3519 "Received full table");
3521 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
, num_entries
,
3524 spin_lock_bh(&orig_node
->tt_buff_lock
);
3525 kfree(orig_node
->tt_buff
);
3526 orig_node
->tt_buff_len
= 0;
3527 orig_node
->tt_buff
= NULL
;
3528 spin_unlock_bh(&orig_node
->tt_buff_lock
);
3530 atomic_set(&orig_node
->last_ttvn
, ttvn
);
3534 batadv_orig_node_put(orig_node
);
3537 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
3538 struct batadv_orig_node
*orig_node
,
3539 u16 tt_num_changes
, u8 ttvn
,
3540 struct batadv_tvlv_tt_change
*tt_change
)
3542 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
3543 tt_num_changes
, ttvn
);
3545 batadv_tt_save_orig_buffer(bat_priv
, orig_node
, tt_change
,
3546 batadv_tt_len(tt_num_changes
));
3547 atomic_set(&orig_node
->last_ttvn
, ttvn
);
3551 * batadv_is_my_client() - check if a client is served by the local node
3552 * @bat_priv: the bat priv with all the soft interface information
3553 * @addr: the mac address of the client to check
3554 * @vid: VLAN identifier
3556 * Return: true if the client is served by this node, false otherwise.
3558 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const u8
*addr
,
3561 struct batadv_tt_local_entry
*tt_local_entry
;
3564 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
3565 if (!tt_local_entry
)
3567 /* Check if the client has been logically deleted (but is kept for
3568 * consistency purpose)
3570 if ((tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
) ||
3571 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
3576 batadv_tt_local_entry_put(tt_local_entry
);
3581 * batadv_handle_tt_response() - process incoming tt reply
3582 * @bat_priv: the bat priv with all the soft interface information
3583 * @tt_data: tt data containing the tt request information
3584 * @resp_src: mac address of tt reply sender
3585 * @num_entries: number of tt change entries appended to the tt data
3587 static void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
3588 struct batadv_tvlv_tt_data
*tt_data
,
3589 u8
*resp_src
, u16 num_entries
)
3591 struct batadv_tt_req_node
*node
;
3592 struct hlist_node
*safe
;
3593 struct batadv_orig_node
*orig_node
= NULL
;
3594 struct batadv_tvlv_tt_change
*tt_change
;
3595 u8
*tvlv_ptr
= (u8
*)tt_data
;
3598 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3599 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3600 resp_src
, tt_data
->ttvn
, num_entries
,
3601 ((tt_data
->flags
& BATADV_TT_FULL_TABLE
) ? 'F' : '.'));
3603 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
3607 spin_lock_bh(&orig_node
->tt_lock
);
3609 change_offset
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3610 change_offset
*= ntohs(tt_data
->num_vlan
);
3611 change_offset
+= sizeof(*tt_data
);
3612 tvlv_ptr
+= change_offset
;
3614 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_ptr
;
3615 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
) {
3616 batadv_tt_fill_gtable(bat_priv
, tt_change
, tt_data
->ttvn
,
3617 resp_src
, num_entries
);
3619 batadv_tt_update_changes(bat_priv
, orig_node
, num_entries
,
3620 tt_data
->ttvn
, tt_change
);
3623 /* Recalculate the CRC for this orig_node and store it */
3624 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3626 spin_unlock_bh(&orig_node
->tt_lock
);
3628 /* Delete the tt_req_node from pending tt_requests list */
3629 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
3630 hlist_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
3631 if (!batadv_compare_eth(node
->addr
, resp_src
))
3633 hlist_del_init(&node
->list
);
3634 batadv_tt_req_node_put(node
);
3637 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
3640 batadv_orig_node_put(orig_node
);
3643 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
3645 struct batadv_tt_roam_node
*node
, *safe
;
3647 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3649 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
3650 list_del(&node
->list
);
3651 kmem_cache_free(batadv_tt_roam_cache
, node
);
3654 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3657 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
3659 struct batadv_tt_roam_node
*node
, *safe
;
3661 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3662 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
3663 if (!batadv_has_timed_out(node
->first_time
,
3664 BATADV_ROAMING_MAX_TIME
))
3667 list_del(&node
->list
);
3668 kmem_cache_free(batadv_tt_roam_cache
, node
);
3670 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3674 * batadv_tt_check_roam_count() - check if a client has roamed too frequently
3675 * @bat_priv: the bat priv with all the soft interface information
3676 * @client: mac address of the roaming client
3678 * This function checks whether the client already reached the
3679 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3682 * Return: true if the ROAMING_ADV can be sent, false otherwise
3684 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
, u8
*client
)
3686 struct batadv_tt_roam_node
*tt_roam_node
;
3689 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
3690 /* The new tt_req will be issued only if I'm not waiting for a
3691 * reply from the same orig_node yet
3693 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
3694 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
3697 if (batadv_has_timed_out(tt_roam_node
->first_time
,
3698 BATADV_ROAMING_MAX_TIME
))
3701 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
3702 /* Sorry, you roamed too many times! */
3709 tt_roam_node
= kmem_cache_alloc(batadv_tt_roam_cache
,
3714 tt_roam_node
->first_time
= jiffies
;
3715 atomic_set(&tt_roam_node
->counter
,
3716 BATADV_ROAMING_MAX_COUNT
- 1);
3717 ether_addr_copy(tt_roam_node
->addr
, client
);
3719 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
3724 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3729 * batadv_send_roam_adv() - send a roaming advertisement message
3730 * @bat_priv: the bat priv with all the soft interface information
3731 * @client: mac address of the roaming client
3732 * @vid: VLAN identifier
3733 * @orig_node: message destination
3735 * Send a ROAMING_ADV message to the node which was previously serving this
3736 * client. This is done to inform the node that from now on all traffic destined
3737 * for this particular roamed client has to be forwarded to the sender of the
3740 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, u8
*client
,
3742 struct batadv_orig_node
*orig_node
)
3744 struct batadv_hard_iface
*primary_if
;
3745 struct batadv_tvlv_roam_adv tvlv_roam
;
3747 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3751 /* before going on we have to check whether the client has
3752 * already roamed to us too many times
3754 if (!batadv_tt_check_roam_count(bat_priv
, client
))
3757 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3758 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3759 orig_node
->orig
, client
, batadv_print_vid(vid
));
3761 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
3763 memcpy(tvlv_roam
.client
, client
, sizeof(tvlv_roam
.client
));
3764 tvlv_roam
.vid
= htons(vid
);
3766 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3767 orig_node
->orig
, BATADV_TVLV_ROAM
, 1,
3768 &tvlv_roam
, sizeof(tvlv_roam
));
3772 batadv_hardif_put(primary_if
);
3775 static void batadv_tt_purge(struct work_struct
*work
)
3777 struct delayed_work
*delayed_work
;
3778 struct batadv_priv_tt
*priv_tt
;
3779 struct batadv_priv
*bat_priv
;
3781 delayed_work
= to_delayed_work(work
);
3782 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
3783 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
3785 batadv_tt_local_purge(bat_priv
, BATADV_TT_LOCAL_TIMEOUT
);
3786 batadv_tt_global_purge(bat_priv
);
3787 batadv_tt_req_purge(bat_priv
);
3788 batadv_tt_roam_purge(bat_priv
);
3790 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3791 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3795 * batadv_tt_free() - Free translation table of soft interface
3796 * @bat_priv: the bat priv with all the soft interface information
3798 void batadv_tt_free(struct batadv_priv
*bat_priv
)
3800 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3801 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3803 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
3805 batadv_tt_local_table_free(bat_priv
);
3806 batadv_tt_global_table_free(bat_priv
);
3807 batadv_tt_req_list_free(bat_priv
);
3808 batadv_tt_changes_list_free(bat_priv
);
3809 batadv_tt_roam_list_free(bat_priv
);
3811 kfree(bat_priv
->tt
.last_changeset
);
3815 * batadv_tt_local_set_flags() - set or unset the specified flags on the local
3816 * table and possibly count them in the TT size
3817 * @bat_priv: the bat priv with all the soft interface information
3818 * @flags: the flag to switch
3819 * @enable: whether to set or unset the flag
3820 * @count: whether to increase the TT size by the number of changed entries
3822 static void batadv_tt_local_set_flags(struct batadv_priv
*bat_priv
, u16 flags
,
3823 bool enable
, bool count
)
3825 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3826 struct batadv_tt_common_entry
*tt_common_entry
;
3827 struct hlist_head
*head
;
3833 for (i
= 0; i
< hash
->size
; i
++) {
3834 head
= &hash
->table
[i
];
3837 hlist_for_each_entry_rcu(tt_common_entry
,
3840 if ((tt_common_entry
->flags
& flags
) == flags
)
3842 tt_common_entry
->flags
|= flags
;
3844 if (!(tt_common_entry
->flags
& flags
))
3846 tt_common_entry
->flags
&= ~flags
;
3852 batadv_tt_local_size_inc(bat_priv
,
3853 tt_common_entry
->vid
);
3859 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3860 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
3862 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3863 struct batadv_tt_common_entry
*tt_common
;
3864 struct batadv_tt_local_entry
*tt_local
;
3865 struct hlist_node
*node_tmp
;
3866 struct hlist_head
*head
;
3867 spinlock_t
*list_lock
; /* protects write access to the hash lists */
3873 for (i
= 0; i
< hash
->size
; i
++) {
3874 head
= &hash
->table
[i
];
3875 list_lock
= &hash
->list_locks
[i
];
3877 spin_lock_bh(list_lock
);
3878 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
3880 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
3883 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3884 "Deleting local tt entry (%pM, vid: %d): pending\n",
3886 batadv_print_vid(tt_common
->vid
));
3888 batadv_tt_local_size_dec(bat_priv
, tt_common
->vid
);
3889 hlist_del_rcu(&tt_common
->hash_entry
);
3890 tt_local
= container_of(tt_common
,
3891 struct batadv_tt_local_entry
,
3894 batadv_tt_local_entry_put(tt_local
);
3896 spin_unlock_bh(list_lock
);
3901 * batadv_tt_local_commit_changes_nolock() - commit all pending local tt changes
3902 * which have been queued in the time since the last commit
3903 * @bat_priv: the bat priv with all the soft interface information
3905 * Caller must hold tt->commit_lock.
3907 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv
*bat_priv
)
3909 lockdep_assert_held(&bat_priv
->tt
.commit_lock
);
3911 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1) {
3912 if (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))
3913 batadv_tt_tvlv_container_update(bat_priv
);
3917 batadv_tt_local_set_flags(bat_priv
, BATADV_TT_CLIENT_NEW
, false, true);
3919 batadv_tt_local_purge_pending_clients(bat_priv
);
3920 batadv_tt_local_update_crc(bat_priv
);
3922 /* Increment the TTVN only once per OGM interval */
3923 atomic_inc(&bat_priv
->tt
.vn
);
3924 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3925 "Local changes committed, updating to ttvn %u\n",
3926 (u8
)atomic_read(&bat_priv
->tt
.vn
));
3928 /* reset the sending counter */
3929 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
3930 batadv_tt_tvlv_container_update(bat_priv
);
3934 * batadv_tt_local_commit_changes() - commit all pending local tt changes which
3935 * have been queued in the time since the last commit
3936 * @bat_priv: the bat priv with all the soft interface information
3938 void batadv_tt_local_commit_changes(struct batadv_priv
*bat_priv
)
3940 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3941 batadv_tt_local_commit_changes_nolock(bat_priv
);
3942 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3946 * batadv_is_ap_isolated() - Check if packet from upper layer should be dropped
3947 * @bat_priv: the bat priv with all the soft interface information
3948 * @src: source mac address of packet
3949 * @dst: destination mac address of packet
3950 * @vid: vlan id of packet
3952 * Return: true when src+dst(+vid) pair should be isolated, false otherwise
3954 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, u8
*src
, u8
*dst
,
3957 struct batadv_tt_local_entry
*tt_local_entry
;
3958 struct batadv_tt_global_entry
*tt_global_entry
;
3959 struct batadv_softif_vlan
*vlan
;
3962 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
3966 if (!atomic_read(&vlan
->ap_isolation
))
3969 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
, vid
);
3970 if (!tt_local_entry
)
3973 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
, vid
);
3974 if (!tt_global_entry
)
3975 goto local_entry_put
;
3977 if (_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
3980 batadv_tt_global_entry_put(tt_global_entry
);
3982 batadv_tt_local_entry_put(tt_local_entry
);
3984 batadv_softif_vlan_put(vlan
);
3989 * batadv_tt_update_orig() - update global translation table with new tt
3990 * information received via ogms
3991 * @bat_priv: the bat priv with all the soft interface information
3992 * @orig_node: the orig_node of the ogm
3993 * @tt_buff: pointer to the first tvlv VLAN entry
3994 * @tt_num_vlan: number of tvlv VLAN entries
3995 * @tt_change: pointer to the first entry in the TT buffer
3996 * @tt_num_changes: number of tt changes inside the tt buffer
3997 * @ttvn: translation table version number of this changeset
3999 static void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
4000 struct batadv_orig_node
*orig_node
,
4001 const void *tt_buff
, u16 tt_num_vlan
,
4002 struct batadv_tvlv_tt_change
*tt_change
,
4003 u16 tt_num_changes
, u8 ttvn
)
4005 u8 orig_ttvn
= (u8
)atomic_read(&orig_node
->last_ttvn
);
4006 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
4007 bool full_table
= true;
4010 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)tt_buff
;
4011 has_tt_init
= test_bit(BATADV_ORIG_CAPA_HAS_TT
,
4012 &orig_node
->capa_initialized
);
4014 /* orig table not initialised AND first diff is in the OGM OR the ttvn
4015 * increased by one -> we can apply the attached changes
4017 if ((!has_tt_init
&& ttvn
== 1) || ttvn
- orig_ttvn
== 1) {
4018 /* the OGM could not contain the changes due to their size or
4019 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
4021 * In this case send a tt request
4023 if (!tt_num_changes
) {
4028 spin_lock_bh(&orig_node
->tt_lock
);
4030 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
4033 /* Even if we received the precomputed crc with the OGM, we
4034 * prefer to recompute it to spot any possible inconsistency
4035 * in the global table
4037 batadv_tt_global_update_crc(bat_priv
, orig_node
);
4039 spin_unlock_bh(&orig_node
->tt_lock
);
4041 /* The ttvn alone is not enough to guarantee consistency
4042 * because a single value could represent different states
4043 * (due to the wrap around). Thus a node has to check whether
4044 * the resulting table (after applying the changes) is still
4045 * consistent or not. E.g. a node could disconnect while its
4046 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
4047 * checking the CRC value is mandatory to detect the
4050 if (!batadv_tt_global_check_crc(orig_node
, tt_vlan
,
4054 /* if we missed more than one change or our tables are not
4055 * in sync anymore -> request fresh tt data
4057 if (!has_tt_init
|| ttvn
!= orig_ttvn
||
4058 !batadv_tt_global_check_crc(orig_node
, tt_vlan
,
4061 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
4062 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
4063 orig_node
->orig
, ttvn
, orig_ttvn
,
4065 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
4066 tt_vlan
, tt_num_vlan
,
4074 * batadv_tt_global_client_is_roaming() - check if a client is marked as roaming
4075 * @bat_priv: the bat priv with all the soft interface information
4076 * @addr: the mac address of the client to check
4077 * @vid: VLAN identifier
4079 * Return: true if we know that the client has moved from its old originator
4080 * to another one. This entry is still kept for consistency purposes and will be
4081 * deleted later by a DEL or because of timeout
4083 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
4084 u8
*addr
, unsigned short vid
)
4086 struct batadv_tt_global_entry
*tt_global_entry
;
4089 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
4090 if (!tt_global_entry
)
4093 ret
= tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
4094 batadv_tt_global_entry_put(tt_global_entry
);
4100 * batadv_tt_local_client_is_roaming() - tells whether the client is roaming
4101 * @bat_priv: the bat priv with all the soft interface information
4102 * @addr: the mac address of the local client to query
4103 * @vid: VLAN identifier
4105 * Return: true if the local client is known to be roaming (it is not served by
4106 * this node anymore) or not. If yes, the client is still present in the table
4107 * to keep the latter consistent with the node TTVN
4109 bool batadv_tt_local_client_is_roaming(struct batadv_priv
*bat_priv
,
4110 u8
*addr
, unsigned short vid
)
4112 struct batadv_tt_local_entry
*tt_local_entry
;
4115 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
4116 if (!tt_local_entry
)
4119 ret
= tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
4120 batadv_tt_local_entry_put(tt_local_entry
);
4126 * batadv_tt_add_temporary_global_entry() - Add temporary entry to global TT
4127 * @bat_priv: the bat priv with all the soft interface information
4128 * @orig_node: orig node which the temporary entry should be associated with
4129 * @addr: mac address of the client
4130 * @vid: VLAN id of the new temporary global translation table
4132 * Return: true when temporary tt entry could be added, false otherwise
4134 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
4135 struct batadv_orig_node
*orig_node
,
4136 const unsigned char *addr
,
4139 /* ignore loop detect macs, they are not supposed to be in the tt local
4142 if (batadv_bla_is_loopdetect_mac(addr
))
4145 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
, vid
,
4146 BATADV_TT_CLIENT_TEMP
,
4147 atomic_read(&orig_node
->last_ttvn
)))
4150 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
4151 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
4152 addr
, batadv_print_vid(vid
), orig_node
->orig
);
4158 * batadv_tt_local_resize_to_mtu() - resize the local translation table fit the
4159 * maximum packet size that can be transported through the mesh
4160 * @soft_iface: netdev struct of the mesh interface
4162 * Remove entries older than 'timeout' and half timeout if more entries need
4165 void batadv_tt_local_resize_to_mtu(struct net_device
*soft_iface
)
4167 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
4168 int packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
4169 int table_size
, timeout
= BATADV_TT_LOCAL_TIMEOUT
/ 2;
4170 bool reduced
= false;
4172 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
4175 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
4176 if (packet_size_max
>= table_size
)
4179 batadv_tt_local_purge(bat_priv
, timeout
);
4180 batadv_tt_local_purge_pending_clients(bat_priv
);
4184 net_ratelimited_function(batadv_info
, soft_iface
,
4185 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
4189 /* commit these changes immediately, to avoid synchronization problem
4193 batadv_tt_local_commit_changes_nolock(bat_priv
);
4195 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
4199 * batadv_tt_tvlv_ogm_handler_v1() - process incoming tt tvlv container
4200 * @bat_priv: the bat priv with all the soft interface information
4201 * @orig: the orig_node of the ogm
4202 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4203 * @tvlv_value: tvlv buffer containing the gateway data
4204 * @tvlv_value_len: tvlv buffer length
4206 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
4207 struct batadv_orig_node
*orig
,
4208 u8 flags
, void *tvlv_value
,
4211 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
4212 struct batadv_tvlv_tt_change
*tt_change
;
4213 struct batadv_tvlv_tt_data
*tt_data
;
4214 u16 num_entries
, num_vlan
;
4216 if (tvlv_value_len
< sizeof(*tt_data
))
4219 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
4220 tvlv_value_len
-= sizeof(*tt_data
);
4222 num_vlan
= ntohs(tt_data
->num_vlan
);
4224 if (tvlv_value_len
< sizeof(*tt_vlan
) * num_vlan
)
4227 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
4228 tt_change
= (struct batadv_tvlv_tt_change
*)(tt_vlan
+ num_vlan
);
4229 tvlv_value_len
-= sizeof(*tt_vlan
) * num_vlan
;
4231 num_entries
= batadv_tt_entries(tvlv_value_len
);
4233 batadv_tt_update_orig(bat_priv
, orig
, tt_vlan
, num_vlan
, tt_change
,
4234 num_entries
, tt_data
->ttvn
);
4238 * batadv_tt_tvlv_unicast_handler_v1() - process incoming (unicast) tt tvlv
4240 * @bat_priv: the bat priv with all the soft interface information
4241 * @src: mac address of tt tvlv sender
4242 * @dst: mac address of tt tvlv recipient
4243 * @tvlv_value: tvlv buffer containing the tt data
4244 * @tvlv_value_len: tvlv buffer length
4246 * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
4249 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
4254 struct batadv_tvlv_tt_data
*tt_data
;
4255 u16 tt_vlan_len
, tt_num_entries
;
4259 if (tvlv_value_len
< sizeof(*tt_data
))
4260 return NET_RX_SUCCESS
;
4262 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
4263 tvlv_value_len
-= sizeof(*tt_data
);
4265 tt_vlan_len
= sizeof(struct batadv_tvlv_tt_vlan_data
);
4266 tt_vlan_len
*= ntohs(tt_data
->num_vlan
);
4268 if (tvlv_value_len
< tt_vlan_len
)
4269 return NET_RX_SUCCESS
;
4271 tvlv_value_len
-= tt_vlan_len
;
4272 tt_num_entries
= batadv_tt_entries(tvlv_value_len
);
4274 switch (tt_data
->flags
& BATADV_TT_DATA_TYPE_MASK
) {
4275 case BATADV_TT_REQUEST
:
4276 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
4278 /* If this node cannot provide a TT response the tt_request is
4281 ret
= batadv_send_tt_response(bat_priv
, tt_data
, src
, dst
);
4283 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
4288 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
4289 "Routing TT_REQUEST to %pM [%c]\n",
4291 /* tvlv API will re-route the packet */
4295 case BATADV_TT_RESPONSE
:
4296 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
4298 if (batadv_is_my_mac(bat_priv
, dst
)) {
4299 batadv_handle_tt_response(bat_priv
, tt_data
,
4300 src
, tt_num_entries
);
4301 return NET_RX_SUCCESS
;
4304 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
4309 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
4310 "Routing TT_RESPONSE to %pM [%c]\n", dst
, tt_flag
);
4312 /* tvlv API will re-route the packet */
4316 return NET_RX_SUCCESS
;
4320 * batadv_roam_tvlv_unicast_handler_v1() - process incoming tt roam tvlv
4322 * @bat_priv: the bat priv with all the soft interface information
4323 * @src: mac address of tt tvlv sender
4324 * @dst: mac address of tt tvlv recipient
4325 * @tvlv_value: tvlv buffer containing the tt data
4326 * @tvlv_value_len: tvlv buffer length
4328 * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
4331 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
4336 struct batadv_tvlv_roam_adv
*roaming_adv
;
4337 struct batadv_orig_node
*orig_node
= NULL
;
4339 /* If this node is not the intended recipient of the
4340 * roaming advertisement the packet is forwarded
4341 * (the tvlv API will re-route the packet).
4343 if (!batadv_is_my_mac(bat_priv
, dst
))
4346 if (tvlv_value_len
< sizeof(*roaming_adv
))
4349 orig_node
= batadv_orig_hash_find(bat_priv
, src
);
4353 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
4354 roaming_adv
= (struct batadv_tvlv_roam_adv
*)tvlv_value
;
4356 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
4357 "Received ROAMING_ADV from %pM (client %pM)\n",
4358 src
, roaming_adv
->client
);
4360 batadv_tt_global_add(bat_priv
, orig_node
, roaming_adv
->client
,
4361 ntohs(roaming_adv
->vid
), BATADV_TT_CLIENT_ROAM
,
4362 atomic_read(&orig_node
->last_ttvn
) + 1);
4366 batadv_orig_node_put(orig_node
);
4367 return NET_RX_SUCCESS
;
4371 * batadv_tt_init() - initialise the translation table internals
4372 * @bat_priv: the bat priv with all the soft interface information
4374 * Return: 0 on success or negative error number in case of failure.
4376 int batadv_tt_init(struct batadv_priv
*bat_priv
)
4380 /* synchronized flags must be remote */
4381 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK
& BATADV_TT_REMOTE_MASK
));
4383 ret
= batadv_tt_local_init(bat_priv
);
4387 ret
= batadv_tt_global_init(bat_priv
);
4391 batadv_tvlv_handler_register(bat_priv
, batadv_tt_tvlv_ogm_handler_v1
,
4392 batadv_tt_tvlv_unicast_handler_v1
,
4393 BATADV_TVLV_TT
, 1, BATADV_NO_FLAGS
);
4395 batadv_tvlv_handler_register(bat_priv
, NULL
,
4396 batadv_roam_tvlv_unicast_handler_v1
,
4397 BATADV_TVLV_ROAM
, 1, BATADV_NO_FLAGS
);
4399 INIT_DELAYED_WORK(&bat_priv
->tt
.work
, batadv_tt_purge
);
4400 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
4401 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
4407 * batadv_tt_global_is_isolated() - check if a client is marked as isolated
4408 * @bat_priv: the bat priv with all the soft interface information
4409 * @addr: the mac address of the client
4410 * @vid: the identifier of the VLAN where this client is connected
4412 * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
4415 bool batadv_tt_global_is_isolated(struct batadv_priv
*bat_priv
,
4416 const u8
*addr
, unsigned short vid
)
4418 struct batadv_tt_global_entry
*tt
;
4421 tt
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
4425 ret
= tt
->common
.flags
& BATADV_TT_CLIENT_ISOLA
;
4427 batadv_tt_global_entry_put(tt
);
4433 * batadv_tt_cache_init() - Initialize tt memory object cache
4435 * Return: 0 on success or negative error number in case of failure.
4437 int __init
batadv_tt_cache_init(void)
4439 size_t tl_size
= sizeof(struct batadv_tt_local_entry
);
4440 size_t tg_size
= sizeof(struct batadv_tt_global_entry
);
4441 size_t tt_orig_size
= sizeof(struct batadv_tt_orig_list_entry
);
4442 size_t tt_change_size
= sizeof(struct batadv_tt_change_node
);
4443 size_t tt_req_size
= sizeof(struct batadv_tt_req_node
);
4444 size_t tt_roam_size
= sizeof(struct batadv_tt_roam_node
);
4446 batadv_tl_cache
= kmem_cache_create("batadv_tl_cache", tl_size
, 0,
4447 SLAB_HWCACHE_ALIGN
, NULL
);
4448 if (!batadv_tl_cache
)
4451 batadv_tg_cache
= kmem_cache_create("batadv_tg_cache", tg_size
, 0,
4452 SLAB_HWCACHE_ALIGN
, NULL
);
4453 if (!batadv_tg_cache
)
4454 goto err_tt_tl_destroy
;
4456 batadv_tt_orig_cache
= kmem_cache_create("batadv_tt_orig_cache",
4458 SLAB_HWCACHE_ALIGN
, NULL
);
4459 if (!batadv_tt_orig_cache
)
4460 goto err_tt_tg_destroy
;
4462 batadv_tt_change_cache
= kmem_cache_create("batadv_tt_change_cache",
4464 SLAB_HWCACHE_ALIGN
, NULL
);
4465 if (!batadv_tt_change_cache
)
4466 goto err_tt_orig_destroy
;
4468 batadv_tt_req_cache
= kmem_cache_create("batadv_tt_req_cache",
4470 SLAB_HWCACHE_ALIGN
, NULL
);
4471 if (!batadv_tt_req_cache
)
4472 goto err_tt_change_destroy
;
4474 batadv_tt_roam_cache
= kmem_cache_create("batadv_tt_roam_cache",
4476 SLAB_HWCACHE_ALIGN
, NULL
);
4477 if (!batadv_tt_roam_cache
)
4478 goto err_tt_req_destroy
;
4483 kmem_cache_destroy(batadv_tt_req_cache
);
4484 batadv_tt_req_cache
= NULL
;
4485 err_tt_change_destroy
:
4486 kmem_cache_destroy(batadv_tt_change_cache
);
4487 batadv_tt_change_cache
= NULL
;
4488 err_tt_orig_destroy
:
4489 kmem_cache_destroy(batadv_tt_orig_cache
);
4490 batadv_tt_orig_cache
= NULL
;
4492 kmem_cache_destroy(batadv_tg_cache
);
4493 batadv_tg_cache
= NULL
;
4495 kmem_cache_destroy(batadv_tl_cache
);
4496 batadv_tl_cache
= NULL
;
4502 * batadv_tt_cache_destroy() - Destroy tt memory object cache
4504 void batadv_tt_cache_destroy(void)
4506 kmem_cache_destroy(batadv_tl_cache
);
4507 kmem_cache_destroy(batadv_tg_cache
);
4508 kmem_cache_destroy(batadv_tt_orig_cache
);
4509 kmem_cache_destroy(batadv_tt_change_cache
);
4510 kmem_cache_destroy(batadv_tt_req_cache
);
4511 kmem_cache_destroy(batadv_tt_roam_cache
);