1 /* Copyright (C) 2007-2014 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "translation-table.h"
20 #include "soft-interface.h"
21 #include "hard-interface.h"
24 #include "originator.h"
26 #include "bridge_loop_avoidance.h"
27 #include "multicast.h"
29 #include <linux/crc32c.h>
32 static struct lock_class_key batadv_tt_local_hash_lock_class_key
;
33 static struct lock_class_key batadv_tt_global_hash_lock_class_key
;
35 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
37 struct batadv_orig_node
*orig_node
);
38 static void batadv_tt_purge(struct work_struct
*work
);
40 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
);
41 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
42 struct batadv_orig_node
*orig_node
,
43 const unsigned char *addr
,
44 unsigned short vid
, const char *message
,
47 /* returns 1 if they are the same mac addr */
48 static int batadv_compare_tt(const struct hlist_node
*node
, const void *data2
)
50 const void *data1
= container_of(node
, struct batadv_tt_common_entry
,
53 return batadv_compare_eth(data1
, data2
);
57 * batadv_choose_tt - return the index of the tt entry in the hash table
58 * @data: pointer to the tt_common_entry object to map
59 * @size: the size of the hash table
61 * Returns the hash index where the object represented by 'data' should be
64 static inline uint32_t batadv_choose_tt(const void *data
, uint32_t size
)
66 struct batadv_tt_common_entry
*tt
;
69 tt
= (struct batadv_tt_common_entry
*)data
;
70 hash
= batadv_hash_bytes(hash
, &tt
->addr
, ETH_ALEN
);
71 hash
= batadv_hash_bytes(hash
, &tt
->vid
, sizeof(tt
->vid
));
81 * batadv_tt_hash_find - look for a client in the given hash table
82 * @hash: the hash table to search
83 * @addr: the mac address of the client to look for
84 * @vid: VLAN identifier
86 * Returns a pointer to the tt_common struct belonging to the searched client if
87 * found, NULL otherwise.
89 static struct batadv_tt_common_entry
*
90 batadv_tt_hash_find(struct batadv_hashtable
*hash
, const uint8_t *addr
,
93 struct hlist_head
*head
;
94 struct batadv_tt_common_entry to_search
, *tt
, *tt_tmp
= NULL
;
100 ether_addr_copy(to_search
.addr
, addr
);
103 index
= batadv_choose_tt(&to_search
, hash
->size
);
104 head
= &hash
->table
[index
];
107 hlist_for_each_entry_rcu(tt
, head
, hash_entry
) {
108 if (!batadv_compare_eth(tt
, addr
))
114 if (!atomic_inc_not_zero(&tt
->refcount
))
126 * batadv_tt_local_hash_find - search the local table for a given client
127 * @bat_priv: the bat priv with all the soft interface information
128 * @addr: the mac address of the client to look for
129 * @vid: VLAN identifier
131 * Returns a pointer to the corresponding tt_local_entry struct if the client is
132 * found, NULL otherwise.
134 static struct batadv_tt_local_entry
*
135 batadv_tt_local_hash_find(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
138 struct batadv_tt_common_entry
*tt_common_entry
;
139 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
141 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.local_hash
, addr
,
144 tt_local_entry
= container_of(tt_common_entry
,
145 struct batadv_tt_local_entry
,
147 return tt_local_entry
;
151 * batadv_tt_global_hash_find - search the global table for a given client
152 * @bat_priv: the bat priv with all the soft interface information
153 * @addr: the mac address of the client to look for
154 * @vid: VLAN identifier
156 * Returns a pointer to the corresponding tt_global_entry struct if the client
157 * is found, NULL otherwise.
159 static struct batadv_tt_global_entry
*
160 batadv_tt_global_hash_find(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
163 struct batadv_tt_common_entry
*tt_common_entry
;
164 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
166 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.global_hash
, addr
,
169 tt_global_entry
= container_of(tt_common_entry
,
170 struct batadv_tt_global_entry
,
172 return tt_global_entry
;
176 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry
*tt_local_entry
)
178 if (atomic_dec_and_test(&tt_local_entry
->common
.refcount
))
179 kfree_rcu(tt_local_entry
, common
.rcu
);
183 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
184 * tt_global_entry and possibly free it
185 * @tt_global_entry: the object to free
188 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry
*tt_global_entry
)
190 if (atomic_dec_and_test(&tt_global_entry
->common
.refcount
)) {
191 batadv_tt_global_del_orig_list(tt_global_entry
);
192 kfree_rcu(tt_global_entry
, common
.rcu
);
197 * batadv_tt_global_hash_count - count the number of orig entries
198 * @hash: hash table containing the tt entries
199 * @addr: the mac address of the client to count entries for
200 * @vid: VLAN identifier
202 * Return the number of originators advertising the given address/data
203 * (excluding ourself).
205 int batadv_tt_global_hash_count(struct batadv_priv
*bat_priv
,
206 const uint8_t *addr
, unsigned short vid
)
208 struct batadv_tt_global_entry
*tt_global_entry
;
211 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
212 if (!tt_global_entry
)
215 count
= atomic_read(&tt_global_entry
->orig_list_count
);
216 batadv_tt_global_entry_free_ref(tt_global_entry
);
221 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head
*rcu
)
223 struct batadv_tt_orig_list_entry
*orig_entry
;
225 orig_entry
= container_of(rcu
, struct batadv_tt_orig_list_entry
, rcu
);
227 /* We are in an rcu callback here, therefore we cannot use
228 * batadv_orig_node_free_ref() and its call_rcu():
229 * An rcu_barrier() wouldn't wait for that to finish
231 batadv_orig_node_free_ref_now(orig_entry
->orig_node
);
236 * batadv_tt_local_size_mod - change the size by v of the local table identified
238 * @bat_priv: the bat priv with all the soft interface information
239 * @vid: the VLAN identifier of the sub-table to change
240 * @v: the amount to sum to the local table size
242 static void batadv_tt_local_size_mod(struct batadv_priv
*bat_priv
,
243 unsigned short vid
, int v
)
245 struct batadv_softif_vlan
*vlan
;
247 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
251 atomic_add(v
, &vlan
->tt
.num_entries
);
253 batadv_softif_vlan_free_ref(vlan
);
257 * batadv_tt_local_size_inc - increase by one the local table size for the given
259 * @bat_priv: the bat priv with all the soft interface information
260 * @vid: the VLAN identifier
262 static void batadv_tt_local_size_inc(struct batadv_priv
*bat_priv
,
265 batadv_tt_local_size_mod(bat_priv
, vid
, 1);
269 * batadv_tt_local_size_dec - decrease by one the local table size for the given
271 * @bat_priv: the bat priv with all the soft interface information
272 * @vid: the VLAN identifier
274 static void batadv_tt_local_size_dec(struct batadv_priv
*bat_priv
,
277 batadv_tt_local_size_mod(bat_priv
, vid
, -1);
281 * batadv_tt_global_size_mod - change the size by v of the local table
283 * @bat_priv: the bat priv with all the soft interface information
284 * @vid: the VLAN identifier
285 * @v: the amount to sum to the global table size
287 static void batadv_tt_global_size_mod(struct batadv_orig_node
*orig_node
,
288 unsigned short vid
, int v
)
290 struct batadv_orig_node_vlan
*vlan
;
292 vlan
= batadv_orig_node_vlan_new(orig_node
, vid
);
296 if (atomic_add_return(v
, &vlan
->tt
.num_entries
) == 0) {
297 spin_lock_bh(&orig_node
->vlan_list_lock
);
298 list_del_rcu(&vlan
->list
);
299 spin_unlock_bh(&orig_node
->vlan_list_lock
);
300 batadv_orig_node_vlan_free_ref(vlan
);
303 batadv_orig_node_vlan_free_ref(vlan
);
307 * batadv_tt_global_size_inc - increase by one the global table size for the
309 * @orig_node: the originator which global table size has to be decreased
310 * @vid: the vlan identifier
312 static void batadv_tt_global_size_inc(struct batadv_orig_node
*orig_node
,
315 batadv_tt_global_size_mod(orig_node
, vid
, 1);
319 * batadv_tt_global_size_dec - decrease by one the global table size for the
321 * @orig_node: the originator which global table size has to be decreased
322 * @vid: the vlan identifier
324 static void batadv_tt_global_size_dec(struct batadv_orig_node
*orig_node
,
327 batadv_tt_global_size_mod(orig_node
, vid
, -1);
331 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry
*orig_entry
)
333 if (!atomic_dec_and_test(&orig_entry
->refcount
))
336 call_rcu(&orig_entry
->rcu
, batadv_tt_orig_list_entry_free_rcu
);
340 * batadv_tt_local_event - store a local TT event (ADD/DEL)
341 * @bat_priv: the bat priv with all the soft interface information
342 * @tt_local_entry: the TT entry involved in the event
343 * @event_flags: flags to store in the event structure
345 static void batadv_tt_local_event(struct batadv_priv
*bat_priv
,
346 struct batadv_tt_local_entry
*tt_local_entry
,
349 struct batadv_tt_change_node
*tt_change_node
, *entry
, *safe
;
350 struct batadv_tt_common_entry
*common
= &tt_local_entry
->common
;
351 uint8_t flags
= common
->flags
| event_flags
;
352 bool event_removed
= false;
353 bool del_op_requested
, del_op_entry
;
355 tt_change_node
= kmalloc(sizeof(*tt_change_node
), GFP_ATOMIC
);
359 tt_change_node
->change
.flags
= flags
;
360 memset(tt_change_node
->change
.reserved
, 0,
361 sizeof(tt_change_node
->change
.reserved
));
362 ether_addr_copy(tt_change_node
->change
.addr
, common
->addr
);
363 tt_change_node
->change
.vid
= htons(common
->vid
);
365 del_op_requested
= flags
& BATADV_TT_CLIENT_DEL
;
367 /* check for ADD+DEL or DEL+ADD events */
368 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
369 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
371 if (!batadv_compare_eth(entry
->change
.addr
, common
->addr
))
374 /* DEL+ADD in the same orig interval have no effect and can be
375 * removed to avoid silly behaviour on the receiver side. The
376 * other way around (ADD+DEL) can happen in case of roaming of
377 * a client still in the NEW state. Roaming of NEW clients is
378 * now possible due to automatically recognition of "temporary"
381 del_op_entry
= entry
->change
.flags
& BATADV_TT_CLIENT_DEL
;
382 if (!del_op_requested
&& del_op_entry
)
384 if (del_op_requested
&& !del_op_entry
)
387 /* this is a second add in the same originator interval. It
388 * means that flags have been changed: update them!
390 if (!del_op_requested
&& !del_op_entry
)
391 entry
->change
.flags
= flags
;
395 list_del(&entry
->list
);
397 kfree(tt_change_node
);
398 event_removed
= true;
402 /* track the change in the OGMinterval list */
403 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt
.changes_list
);
406 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
409 atomic_dec(&bat_priv
->tt
.local_changes
);
411 atomic_inc(&bat_priv
->tt
.local_changes
);
415 * batadv_tt_len - compute length in bytes of given number of tt changes
416 * @changes_num: number of tt changes
418 * Returns computed length in bytes.
420 static int batadv_tt_len(int changes_num
)
422 return changes_num
* sizeof(struct batadv_tvlv_tt_change
);
426 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
427 * @tt_len: available space
429 * Returns the number of entries.
431 static uint16_t batadv_tt_entries(uint16_t tt_len
)
433 return tt_len
/ batadv_tt_len(1);
437 * batadv_tt_local_table_transmit_size - calculates the local translation table
438 * size when transmitted over the air
439 * @bat_priv: the bat priv with all the soft interface information
441 * Returns local translation table size in bytes.
443 static int batadv_tt_local_table_transmit_size(struct batadv_priv
*bat_priv
)
445 uint16_t num_vlan
= 0, tt_local_entries
= 0;
446 struct batadv_softif_vlan
*vlan
;
450 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
452 tt_local_entries
+= atomic_read(&vlan
->tt
.num_entries
);
456 /* header size of tvlv encapsulated tt response payload */
457 hdr_size
= sizeof(struct batadv_unicast_tvlv_packet
);
458 hdr_size
+= sizeof(struct batadv_tvlv_hdr
);
459 hdr_size
+= sizeof(struct batadv_tvlv_tt_data
);
460 hdr_size
+= num_vlan
* sizeof(struct batadv_tvlv_tt_vlan_data
);
462 return hdr_size
+ batadv_tt_len(tt_local_entries
);
465 static int batadv_tt_local_init(struct batadv_priv
*bat_priv
)
467 if (bat_priv
->tt
.local_hash
)
470 bat_priv
->tt
.local_hash
= batadv_hash_new(1024);
472 if (!bat_priv
->tt
.local_hash
)
475 batadv_hash_set_lock_class(bat_priv
->tt
.local_hash
,
476 &batadv_tt_local_hash_lock_class_key
);
481 static void batadv_tt_global_free(struct batadv_priv
*bat_priv
,
482 struct batadv_tt_global_entry
*tt_global
,
485 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
486 "Deleting global tt entry %pM (vid: %d): %s\n",
487 tt_global
->common
.addr
,
488 BATADV_PRINT_VID(tt_global
->common
.vid
), message
);
490 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
491 batadv_choose_tt
, &tt_global
->common
);
492 batadv_tt_global_entry_free_ref(tt_global
);
496 * batadv_tt_local_add - add a new client to the local table or update an
498 * @soft_iface: netdev struct of the mesh interface
499 * @addr: the mac address of the client to add
500 * @vid: VLAN identifier
501 * @ifindex: index of the interface where the client is connected to (useful to
502 * identify wireless clients)
503 * @mark: the value contained in the skb->mark field of the received packet (if
506 * Returns true if the client was successfully added, false otherwise.
508 bool batadv_tt_local_add(struct net_device
*soft_iface
, const uint8_t *addr
,
509 unsigned short vid
, int ifindex
, uint32_t mark
)
511 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
512 struct batadv_tt_local_entry
*tt_local
;
513 struct batadv_tt_global_entry
*tt_global
= NULL
;
514 struct net_device
*in_dev
= NULL
;
515 struct hlist_head
*head
;
516 struct batadv_tt_orig_list_entry
*orig_entry
;
517 int hash_added
, table_size
, packet_size_max
;
518 bool ret
= false, roamed_back
= false;
519 uint8_t remote_flags
;
522 if (ifindex
!= BATADV_NULL_IFINDEX
)
523 in_dev
= dev_get_by_index(&init_net
, ifindex
);
525 tt_local
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
527 if (!is_multicast_ether_addr(addr
))
528 tt_global
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
531 tt_local
->last_seen
= jiffies
;
532 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_PENDING
) {
533 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
534 "Re-adding pending client %pM (vid: %d)\n",
535 addr
, BATADV_PRINT_VID(vid
));
536 /* whatever the reason why the PENDING flag was set,
537 * this is a client which was enqueued to be removed in
538 * this orig_interval. Since it popped up again, the
539 * flag can be reset like it was never enqueued
541 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_PENDING
;
545 if (tt_local
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
546 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
547 "Roaming client %pM (vid: %d) came back to its original location\n",
548 addr
, BATADV_PRINT_VID(vid
));
549 /* the ROAM flag is set because this client roamed away
550 * and the node got a roaming_advertisement message. Now
551 * that the client popped up again at its original
552 * location such flag can be unset
554 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
560 /* Ignore the client if we cannot send it in a full table response. */
561 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
562 table_size
+= batadv_tt_len(1);
563 packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
564 if (table_size
> packet_size_max
) {
565 net_ratelimited_function(batadv_info
, soft_iface
,
566 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
567 table_size
, packet_size_max
, addr
);
571 tt_local
= kmalloc(sizeof(*tt_local
), GFP_ATOMIC
);
575 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
576 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
577 addr
, BATADV_PRINT_VID(vid
),
578 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
580 ether_addr_copy(tt_local
->common
.addr
, addr
);
581 /* The local entry has to be marked as NEW to avoid to send it in
582 * a full table response going out before the next ttvn increment
583 * (consistency check)
585 tt_local
->common
.flags
= BATADV_TT_CLIENT_NEW
;
586 tt_local
->common
.vid
= vid
;
587 if (batadv_is_wifi_netdev(in_dev
))
588 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
589 atomic_set(&tt_local
->common
.refcount
, 2);
590 tt_local
->last_seen
= jiffies
;
591 tt_local
->common
.added_at
= tt_local
->last_seen
;
593 /* the batman interface mac and multicast addresses should never be
596 if (batadv_compare_eth(addr
, soft_iface
->dev_addr
) ||
597 is_multicast_ether_addr(addr
))
598 tt_local
->common
.flags
|= BATADV_TT_CLIENT_NOPURGE
;
600 hash_added
= batadv_hash_add(bat_priv
->tt
.local_hash
, batadv_compare_tt
,
601 batadv_choose_tt
, &tt_local
->common
,
602 &tt_local
->common
.hash_entry
);
604 if (unlikely(hash_added
!= 0)) {
605 /* remove the reference for the hash */
606 batadv_tt_local_entry_free_ref(tt_local
);
611 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
614 /* Check whether it is a roaming, but don't do anything if the roaming
615 * process has already been handled
617 if (tt_global
&& !(tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
)) {
618 /* These node are probably going to update their tt table */
619 head
= &tt_global
->orig_list
;
621 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
622 batadv_send_roam_adv(bat_priv
, tt_global
->common
.addr
,
623 tt_global
->common
.vid
,
624 orig_entry
->orig_node
);
628 batadv_tt_global_free(bat_priv
, tt_global
,
632 /* The global entry has to be marked as ROAMING and
633 * has to be kept for consistency purpose
635 tt_global
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
636 tt_global
->roam_at
= jiffies
;
640 /* store the current remote flags before altering them. This helps
641 * understanding is flags are changing or not
643 remote_flags
= tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
;
645 if (batadv_is_wifi_netdev(in_dev
))
646 tt_local
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
648 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_WIFI
;
650 /* check the mark in the skb: if it's equal to the configured
651 * isolation_mark, it means the packet is coming from an isolated
654 match_mark
= (mark
& bat_priv
->isolation_mark_mask
);
655 if (bat_priv
->isolation_mark_mask
&&
656 match_mark
== bat_priv
->isolation_mark
)
657 tt_local
->common
.flags
|= BATADV_TT_CLIENT_ISOLA
;
659 tt_local
->common
.flags
&= ~BATADV_TT_CLIENT_ISOLA
;
661 /* if any "dynamic" flag has been modified, resend an ADD event for this
662 * entry so that all the nodes can get the new flags
664 if (remote_flags
^ (tt_local
->common
.flags
& BATADV_TT_REMOTE_MASK
))
665 batadv_tt_local_event(bat_priv
, tt_local
, BATADV_NO_FLAGS
);
672 batadv_tt_local_entry_free_ref(tt_local
);
674 batadv_tt_global_entry_free_ref(tt_global
);
679 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
680 * within a TT Response directed to another node
681 * @orig_node: originator for which the TT data has to be prepared
682 * @tt_data: uninitialised pointer to the address of the TVLV buffer
683 * @tt_change: uninitialised pointer to the address of the area where the TT
684 * changed can be stored
685 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
686 * function reserves the amount of space needed to send the entire global TT
687 * table. In case of success the value is updated with the real amount of
690 * Allocate the needed amount of memory for the entire TT TVLV and write its
691 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
692 * objects, one per active VLAN served by the originator node.
694 * Return the size of the allocated buffer or 0 in case of failure.
697 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node
*orig_node
,
698 struct batadv_tvlv_tt_data
**tt_data
,
699 struct batadv_tvlv_tt_change
**tt_change
,
702 uint16_t num_vlan
= 0, num_entries
= 0, change_offset
, tvlv_len
;
703 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
704 struct batadv_orig_node_vlan
*vlan
;
705 uint8_t *tt_change_ptr
;
708 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
710 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
713 change_offset
= sizeof(**tt_data
);
714 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
716 /* if tt_len is negative, allocate the space needed by the full table */
718 *tt_len
= batadv_tt_len(num_entries
);
721 tvlv_len
+= change_offset
;
723 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
729 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
730 (*tt_data
)->ttvn
= atomic_read(&orig_node
->last_ttvn
);
731 (*tt_data
)->num_vlan
= htons(num_vlan
);
733 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
734 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
735 tt_vlan
->vid
= htons(vlan
->vid
);
736 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
741 tt_change_ptr
= (uint8_t *)*tt_data
+ change_offset
;
742 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
750 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
752 * @bat_priv: the bat priv with all the soft interface information
753 * @tt_data: uninitialised pointer to the address of the TVLV buffer
754 * @tt_change: uninitialised pointer to the address of the area where the TT
755 * changes can be stored
756 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
757 * function reserves the amount of space needed to send the entire local TT
758 * table. In case of success the value is updated with the real amount of
761 * Allocate the needed amount of memory for the entire TT TVLV and write its
762 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
763 * objects, one per active VLAN.
765 * Return the size of the allocated buffer or 0 in case of failure.
768 batadv_tt_prepare_tvlv_local_data(struct batadv_priv
*bat_priv
,
769 struct batadv_tvlv_tt_data
**tt_data
,
770 struct batadv_tvlv_tt_change
**tt_change
,
773 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
774 struct batadv_softif_vlan
*vlan
;
775 uint16_t num_vlan
= 0, num_entries
= 0, tvlv_len
;
776 uint8_t *tt_change_ptr
;
780 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
782 num_entries
+= atomic_read(&vlan
->tt
.num_entries
);
785 change_offset
= sizeof(**tt_data
);
786 change_offset
+= num_vlan
* sizeof(*tt_vlan
);
788 /* if tt_len is negative, allocate the space needed by the full table */
790 *tt_len
= batadv_tt_len(num_entries
);
793 tvlv_len
+= change_offset
;
795 *tt_data
= kmalloc(tvlv_len
, GFP_ATOMIC
);
801 (*tt_data
)->flags
= BATADV_NO_FLAGS
;
802 (*tt_data
)->ttvn
= atomic_read(&bat_priv
->tt
.vn
);
803 (*tt_data
)->num_vlan
= htons(num_vlan
);
805 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(*tt_data
+ 1);
806 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
807 tt_vlan
->vid
= htons(vlan
->vid
);
808 tt_vlan
->crc
= htonl(vlan
->tt
.crc
);
813 tt_change_ptr
= (uint8_t *)*tt_data
+ change_offset
;
814 *tt_change
= (struct batadv_tvlv_tt_change
*)tt_change_ptr
;
822 * batadv_tt_tvlv_container_update - update the translation table tvlv container
823 * after local tt changes have been committed
824 * @bat_priv: the bat priv with all the soft interface information
826 static void batadv_tt_tvlv_container_update(struct batadv_priv
*bat_priv
)
828 struct batadv_tt_change_node
*entry
, *safe
;
829 struct batadv_tvlv_tt_data
*tt_data
;
830 struct batadv_tvlv_tt_change
*tt_change
;
831 int tt_diff_len
, tt_change_len
= 0;
832 int tt_diff_entries_num
= 0, tt_diff_entries_count
= 0;
835 tt_diff_entries_num
= atomic_read(&bat_priv
->tt
.local_changes
);
836 tt_diff_len
= batadv_tt_len(tt_diff_entries_num
);
838 /* if we have too many changes for one packet don't send any
839 * and wait for the tt table request which will be fragmented
841 if (tt_diff_len
> bat_priv
->soft_iface
->mtu
)
844 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
, &tt_data
,
845 &tt_change
, &tt_diff_len
);
849 tt_data
->flags
= BATADV_TT_OGM_DIFF
;
851 if (tt_diff_len
== 0)
852 goto container_register
;
854 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
855 atomic_set(&bat_priv
->tt
.local_changes
, 0);
857 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
859 if (tt_diff_entries_count
< tt_diff_entries_num
) {
860 memcpy(tt_change
+ tt_diff_entries_count
,
862 sizeof(struct batadv_tvlv_tt_change
));
863 tt_diff_entries_count
++;
865 list_del(&entry
->list
);
868 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
870 /* Keep the buffer for possible tt_request */
871 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
872 kfree(bat_priv
->tt
.last_changeset
);
873 bat_priv
->tt
.last_changeset_len
= 0;
874 bat_priv
->tt
.last_changeset
= NULL
;
875 tt_change_len
= batadv_tt_len(tt_diff_entries_count
);
876 /* check whether this new OGM has no changes due to size problems */
877 if (tt_diff_entries_count
> 0) {
878 /* if kmalloc() fails we will reply with the full table
879 * instead of providing the diff
881 bat_priv
->tt
.last_changeset
= kzalloc(tt_diff_len
, GFP_ATOMIC
);
882 if (bat_priv
->tt
.last_changeset
) {
883 memcpy(bat_priv
->tt
.last_changeset
,
884 tt_change
, tt_change_len
);
885 bat_priv
->tt
.last_changeset_len
= tt_diff_len
;
888 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
891 batadv_tvlv_container_register(bat_priv
, BATADV_TVLV_TT
, 1, tt_data
,
896 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
898 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
899 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
900 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
901 struct batadv_tt_common_entry
*tt_common_entry
;
902 struct batadv_tt_local_entry
*tt_local
;
903 struct batadv_hard_iface
*primary_if
;
904 struct batadv_softif_vlan
*vlan
;
905 struct hlist_head
*head
;
910 unsigned long last_seen_jiffies
;
912 uint16_t np_flag
= BATADV_TT_CLIENT_NOPURGE
;
914 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
919 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
920 net_dev
->name
, (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
921 seq_printf(seq
, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
922 "Flags", "Last seen", "CRC");
924 for (i
= 0; i
< hash
->size
; i
++) {
925 head
= &hash
->table
[i
];
928 hlist_for_each_entry_rcu(tt_common_entry
,
930 tt_local
= container_of(tt_common_entry
,
931 struct batadv_tt_local_entry
,
933 vid
= tt_common_entry
->vid
;
934 last_seen_jiffies
= jiffies
- tt_local
->last_seen
;
935 last_seen_msecs
= jiffies_to_msecs(last_seen_jiffies
);
936 last_seen_secs
= last_seen_msecs
/ 1000;
937 last_seen_msecs
= last_seen_msecs
% 1000;
939 no_purge
= tt_common_entry
->flags
& np_flag
;
941 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
943 seq_printf(seq
, "Cannot retrieve VLAN %d\n",
944 BATADV_PRINT_VID(vid
));
949 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
950 tt_common_entry
->addr
,
951 BATADV_PRINT_VID(tt_common_entry
->vid
),
952 (tt_common_entry
->flags
&
953 BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
954 no_purge
? 'P' : '.',
955 (tt_common_entry
->flags
&
956 BATADV_TT_CLIENT_NEW
? 'N' : '.'),
957 (tt_common_entry
->flags
&
958 BATADV_TT_CLIENT_PENDING
? 'X' : '.'),
959 (tt_common_entry
->flags
&
960 BATADV_TT_CLIENT_WIFI
? 'W' : '.'),
961 (tt_common_entry
->flags
&
962 BATADV_TT_CLIENT_ISOLA
? 'I' : '.'),
963 no_purge
? 0 : last_seen_secs
,
964 no_purge
? 0 : last_seen_msecs
,
967 batadv_softif_vlan_free_ref(vlan
);
973 batadv_hardif_free_ref(primary_if
);
978 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
979 struct batadv_tt_local_entry
*tt_local_entry
,
980 uint16_t flags
, const char *message
)
982 batadv_tt_local_event(bat_priv
, tt_local_entry
, flags
);
984 /* The local client has to be marked as "pending to be removed" but has
985 * to be kept in the table in order to send it in a full table
986 * response issued before the net ttvn increment (consistency check)
988 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
990 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
991 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
992 tt_local_entry
->common
.addr
,
993 BATADV_PRINT_VID(tt_local_entry
->common
.vid
), message
);
997 * batadv_tt_local_remove - logically remove an entry from the local table
998 * @bat_priv: the bat priv with all the soft interface information
999 * @addr: the MAC address of the client to remove
1000 * @vid: VLAN identifier
1001 * @message: message to append to the log on deletion
1002 * @roaming: true if the deletion is due to a roaming event
1004 * Returns the flags assigned to the local entry before being deleted
1006 uint16_t batadv_tt_local_remove(struct batadv_priv
*bat_priv
,
1007 const uint8_t *addr
, unsigned short vid
,
1008 const char *message
, bool roaming
)
1010 struct batadv_tt_local_entry
*tt_local_entry
;
1011 uint16_t flags
, curr_flags
= BATADV_NO_FLAGS
;
1013 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
1014 if (!tt_local_entry
)
1017 curr_flags
= tt_local_entry
->common
.flags
;
1019 flags
= BATADV_TT_CLIENT_DEL
;
1020 /* if this global entry addition is due to a roaming, the node has to
1021 * mark the local entry as "roamed" in order to correctly reroute
1025 flags
|= BATADV_TT_CLIENT_ROAM
;
1026 /* mark the local client as ROAMed */
1027 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1030 if (!(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
)) {
1031 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
,
1035 /* if this client has been added right now, it is possible to
1036 * immediately purge it
1038 batadv_tt_local_event(bat_priv
, tt_local_entry
, BATADV_TT_CLIENT_DEL
);
1039 hlist_del_rcu(&tt_local_entry
->common
.hash_entry
);
1040 batadv_tt_local_entry_free_ref(tt_local_entry
);
1044 batadv_tt_local_entry_free_ref(tt_local_entry
);
1050 * batadv_tt_local_purge_list - purge inactive tt local entries
1051 * @bat_priv: the bat priv with all the soft interface information
1052 * @head: pointer to the list containing the local tt entries
1053 * @timeout: parameter deciding whether a given tt local entry is considered
1056 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
1057 struct hlist_head
*head
,
1060 struct batadv_tt_local_entry
*tt_local_entry
;
1061 struct batadv_tt_common_entry
*tt_common_entry
;
1062 struct hlist_node
*node_tmp
;
1064 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
, head
,
1066 tt_local_entry
= container_of(tt_common_entry
,
1067 struct batadv_tt_local_entry
,
1069 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
1072 /* entry already marked for deletion */
1073 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1076 if (!batadv_has_timed_out(tt_local_entry
->last_seen
, timeout
))
1079 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
1080 BATADV_TT_CLIENT_DEL
, "timed out");
1085 * batadv_tt_local_purge - purge inactive tt local entries
1086 * @bat_priv: the bat priv with all the soft interface information
1087 * @timeout: parameter deciding whether a given tt local entry is considered
1090 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
,
1093 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1094 struct hlist_head
*head
;
1095 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1098 for (i
= 0; i
< hash
->size
; i
++) {
1099 head
= &hash
->table
[i
];
1100 list_lock
= &hash
->list_locks
[i
];
1102 spin_lock_bh(list_lock
);
1103 batadv_tt_local_purge_list(bat_priv
, head
, timeout
);
1104 spin_unlock_bh(list_lock
);
1108 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
1110 struct batadv_hashtable
*hash
;
1111 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1112 struct batadv_tt_common_entry
*tt_common_entry
;
1113 struct batadv_tt_local_entry
*tt_local
;
1114 struct hlist_node
*node_tmp
;
1115 struct hlist_head
*head
;
1118 if (!bat_priv
->tt
.local_hash
)
1121 hash
= bat_priv
->tt
.local_hash
;
1123 for (i
= 0; i
< hash
->size
; i
++) {
1124 head
= &hash
->table
[i
];
1125 list_lock
= &hash
->list_locks
[i
];
1127 spin_lock_bh(list_lock
);
1128 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1130 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1131 tt_local
= container_of(tt_common_entry
,
1132 struct batadv_tt_local_entry
,
1134 batadv_tt_local_entry_free_ref(tt_local
);
1136 spin_unlock_bh(list_lock
);
1139 batadv_hash_destroy(hash
);
1141 bat_priv
->tt
.local_hash
= NULL
;
1144 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
1146 if (bat_priv
->tt
.global_hash
)
1149 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
1151 if (!bat_priv
->tt
.global_hash
)
1154 batadv_hash_set_lock_class(bat_priv
->tt
.global_hash
,
1155 &batadv_tt_global_hash_lock_class_key
);
1160 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
1162 struct batadv_tt_change_node
*entry
, *safe
;
1164 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
1166 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
1168 list_del(&entry
->list
);
1172 atomic_set(&bat_priv
->tt
.local_changes
, 0);
1173 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
1176 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1177 * batadv_tt_global_entry list
1179 * returns it with an increased refcounter, NULL if not found
1181 static struct batadv_tt_orig_list_entry
*
1182 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
1183 const struct batadv_orig_node
*orig_node
)
1185 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
1186 const struct hlist_head
*head
;
1189 head
= &entry
->orig_list
;
1190 hlist_for_each_entry_rcu(tmp_orig_entry
, head
, list
) {
1191 if (tmp_orig_entry
->orig_node
!= orig_node
)
1193 if (!atomic_inc_not_zero(&tmp_orig_entry
->refcount
))
1196 orig_entry
= tmp_orig_entry
;
1204 /* find out if an orig_node is already in the list of a tt_global_entry.
1205 * returns true if found, false otherwise
1208 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
1209 const struct batadv_orig_node
*orig_node
)
1211 struct batadv_tt_orig_list_entry
*orig_entry
;
1214 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
1217 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1224 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
1225 struct batadv_orig_node
*orig_node
, int ttvn
)
1227 struct batadv_tt_orig_list_entry
*orig_entry
;
1229 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
1231 /* refresh the ttvn: the current value could be a bogus one that
1232 * was added during a "temporary client detection"
1234 orig_entry
->ttvn
= ttvn
;
1238 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
1242 INIT_HLIST_NODE(&orig_entry
->list
);
1243 atomic_inc(&orig_node
->refcount
);
1244 batadv_tt_global_size_inc(orig_node
, tt_global
->common
.vid
);
1245 orig_entry
->orig_node
= orig_node
;
1246 orig_entry
->ttvn
= ttvn
;
1247 atomic_set(&orig_entry
->refcount
, 2);
1249 spin_lock_bh(&tt_global
->list_lock
);
1250 hlist_add_head_rcu(&orig_entry
->list
,
1251 &tt_global
->orig_list
);
1252 spin_unlock_bh(&tt_global
->list_lock
);
1253 atomic_inc(&tt_global
->orig_list_count
);
1257 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1261 * batadv_tt_global_add - add a new TT global entry or update an existing one
1262 * @bat_priv: the bat priv with all the soft interface information
1263 * @orig_node: the originator announcing the client
1264 * @tt_addr: the mac address of the non-mesh client
1265 * @vid: VLAN identifier
1266 * @flags: TT flags that have to be set for this non-mesh client
1267 * @ttvn: the tt version number ever announcing this non-mesh client
1269 * Add a new TT global entry for the given originator. If the entry already
1270 * exists add a new reference to the given originator (a global entry can have
1271 * references to multiple originators) and adjust the flags attribute to reflect
1272 * the function argument.
1273 * If a TT local entry exists for this non-mesh client remove it.
1275 * The caller must hold orig_node refcount.
1277 * Return true if the new entry has been added, false otherwise
1279 static bool batadv_tt_global_add(struct batadv_priv
*bat_priv
,
1280 struct batadv_orig_node
*orig_node
,
1281 const unsigned char *tt_addr
,
1282 unsigned short vid
, uint16_t flags
,
1285 struct batadv_tt_global_entry
*tt_global_entry
;
1286 struct batadv_tt_local_entry
*tt_local_entry
;
1289 struct batadv_tt_common_entry
*common
;
1290 uint16_t local_flags
;
1292 /* ignore global entries from backbone nodes */
1293 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
, vid
))
1296 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
, vid
);
1297 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, tt_addr
, vid
);
1299 /* if the node already has a local client for this entry, it has to wait
1300 * for a roaming advertisement instead of manually messing up the global
1303 if ((flags
& BATADV_TT_CLIENT_TEMP
) && tt_local_entry
&&
1304 !(tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NEW
))
1307 if (!tt_global_entry
) {
1308 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
1309 if (!tt_global_entry
)
1312 common
= &tt_global_entry
->common
;
1313 ether_addr_copy(common
->addr
, tt_addr
);
1316 common
->flags
= flags
;
1317 tt_global_entry
->roam_at
= 0;
1318 /* node must store current time in case of roaming. This is
1319 * needed to purge this entry out on timeout (if nobody claims
1322 if (flags
& BATADV_TT_CLIENT_ROAM
)
1323 tt_global_entry
->roam_at
= jiffies
;
1324 atomic_set(&common
->refcount
, 2);
1325 common
->added_at
= jiffies
;
1327 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
1328 atomic_set(&tt_global_entry
->orig_list_count
, 0);
1329 spin_lock_init(&tt_global_entry
->list_lock
);
1331 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
1333 batadv_choose_tt
, common
,
1334 &common
->hash_entry
);
1336 if (unlikely(hash_added
!= 0)) {
1337 /* remove the reference for the hash */
1338 batadv_tt_global_entry_free_ref(tt_global_entry
);
1342 common
= &tt_global_entry
->common
;
1343 /* If there is already a global entry, we can use this one for
1345 * But if we are trying to add a temporary client then here are
1346 * two options at this point:
1347 * 1) the global client is not a temporary client: the global
1348 * client has to be left as it is, temporary information
1349 * should never override any already known client state
1350 * 2) the global client is a temporary client: purge the
1351 * originator list and add the new one orig_entry
1353 if (flags
& BATADV_TT_CLIENT_TEMP
) {
1354 if (!(common
->flags
& BATADV_TT_CLIENT_TEMP
))
1356 if (batadv_tt_global_entry_has_orig(tt_global_entry
,
1359 batadv_tt_global_del_orig_list(tt_global_entry
);
1360 goto add_orig_entry
;
1363 /* if the client was temporary added before receiving the first
1364 * OGM announcing it, we have to clear the TEMP flag
1366 common
->flags
&= ~BATADV_TT_CLIENT_TEMP
;
1368 /* the change can carry possible "attribute" flags like the
1369 * TT_CLIENT_WIFI, therefore they have to be copied in the
1372 tt_global_entry
->common
.flags
|= flags
;
1374 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1375 * one originator left in the list and we previously received a
1376 * delete + roaming change for this originator.
1378 * We should first delete the old originator before adding the
1381 if (common
->flags
& BATADV_TT_CLIENT_ROAM
) {
1382 batadv_tt_global_del_orig_list(tt_global_entry
);
1383 common
->flags
&= ~BATADV_TT_CLIENT_ROAM
;
1384 tt_global_entry
->roam_at
= 0;
1388 /* add the new orig_entry (if needed) or update it */
1389 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
1391 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1392 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1393 common
->addr
, BATADV_PRINT_VID(common
->vid
),
1398 /* Do not remove multicast addresses from the local hash on
1401 if (is_multicast_ether_addr(tt_addr
))
1404 /* remove address from local hash if present */
1405 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
, vid
,
1406 "global tt received",
1407 flags
& BATADV_TT_CLIENT_ROAM
);
1408 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
1410 if (!(flags
& BATADV_TT_CLIENT_ROAM
))
1411 /* this is a normal global add. Therefore the client is not in a
1412 * roaming state anymore.
1414 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
1417 if (tt_global_entry
)
1418 batadv_tt_global_entry_free_ref(tt_global_entry
);
1420 batadv_tt_local_entry_free_ref(tt_local_entry
);
1425 * batadv_transtable_best_orig - Get best originator list entry from tt entry
1426 * @bat_priv: the bat priv with all the soft interface information
1427 * @tt_global_entry: global translation table entry to be analyzed
1429 * This functon assumes the caller holds rcu_read_lock().
1430 * Returns best originator list entry or NULL on errors.
1432 static struct batadv_tt_orig_list_entry
*
1433 batadv_transtable_best_orig(struct batadv_priv
*bat_priv
,
1434 struct batadv_tt_global_entry
*tt_global_entry
)
1436 struct batadv_neigh_node
*router
, *best_router
= NULL
;
1437 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1438 struct hlist_head
*head
;
1439 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
= NULL
;
1441 head
= &tt_global_entry
->orig_list
;
1442 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1443 router
= batadv_orig_router_get(orig_entry
->orig_node
,
1449 bao
->bat_neigh_cmp(router
, BATADV_IF_DEFAULT
,
1450 best_router
, BATADV_IF_DEFAULT
) <= 0) {
1451 batadv_neigh_node_free_ref(router
);
1455 /* release the refcount for the "old" best */
1457 batadv_neigh_node_free_ref(best_router
);
1459 best_entry
= orig_entry
;
1460 best_router
= router
;
1464 batadv_neigh_node_free_ref(best_router
);
1470 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1471 * for this global entry
1472 * @bat_priv: the bat priv with all the soft interface information
1473 * @tt_global_entry: global translation table entry to be printed
1474 * @seq: debugfs table seq_file struct
1476 * This functon assumes the caller holds rcu_read_lock().
1479 batadv_tt_global_print_entry(struct batadv_priv
*bat_priv
,
1480 struct batadv_tt_global_entry
*tt_global_entry
,
1481 struct seq_file
*seq
)
1483 struct batadv_tt_orig_list_entry
*orig_entry
, *best_entry
;
1484 struct batadv_tt_common_entry
*tt_common_entry
;
1485 struct batadv_orig_node_vlan
*vlan
;
1486 struct hlist_head
*head
;
1490 tt_common_entry
= &tt_global_entry
->common
;
1491 flags
= tt_common_entry
->flags
;
1493 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
1495 vlan
= batadv_orig_node_vlan_get(best_entry
->orig_node
,
1496 tt_common_entry
->vid
);
1499 " * Cannot retrieve VLAN %d for originator %pM\n",
1500 BATADV_PRINT_VID(tt_common_entry
->vid
),
1501 best_entry
->orig_node
->orig
);
1505 last_ttvn
= atomic_read(&best_entry
->orig_node
->last_ttvn
);
1507 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1508 '*', tt_global_entry
->common
.addr
,
1509 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1510 best_entry
->ttvn
, best_entry
->orig_node
->orig
,
1511 last_ttvn
, vlan
->tt
.crc
,
1512 (flags
& BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
1513 (flags
& BATADV_TT_CLIENT_WIFI
? 'W' : '.'),
1514 (flags
& BATADV_TT_CLIENT_ISOLA
? 'I' : '.'),
1515 (flags
& BATADV_TT_CLIENT_TEMP
? 'T' : '.'));
1517 batadv_orig_node_vlan_free_ref(vlan
);
1521 head
= &tt_global_entry
->orig_list
;
1523 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1524 if (best_entry
== orig_entry
)
1527 vlan
= batadv_orig_node_vlan_get(orig_entry
->orig_node
,
1528 tt_common_entry
->vid
);
1531 " + Cannot retrieve VLAN %d for originator %pM\n",
1532 BATADV_PRINT_VID(tt_common_entry
->vid
),
1533 orig_entry
->orig_node
->orig
);
1537 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
1539 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
1540 '+', tt_global_entry
->common
.addr
,
1541 BATADV_PRINT_VID(tt_global_entry
->common
.vid
),
1542 orig_entry
->ttvn
, orig_entry
->orig_node
->orig
,
1543 last_ttvn
, vlan
->tt
.crc
,
1544 (flags
& BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
1545 (flags
& BATADV_TT_CLIENT_WIFI
? 'W' : '.'),
1546 (flags
& BATADV_TT_CLIENT_ISOLA
? 'I' : '.'),
1547 (flags
& BATADV_TT_CLIENT_TEMP
? 'T' : '.'));
1549 batadv_orig_node_vlan_free_ref(vlan
);
1553 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
1555 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1556 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1557 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1558 struct batadv_tt_common_entry
*tt_common_entry
;
1559 struct batadv_tt_global_entry
*tt_global
;
1560 struct batadv_hard_iface
*primary_if
;
1561 struct hlist_head
*head
;
1564 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1569 "Globally announced TT entries received via the mesh %s\n",
1571 seq_printf(seq
, " %-13s %s %s %-15s %s (%-10s) %s\n",
1572 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1575 for (i
= 0; i
< hash
->size
; i
++) {
1576 head
= &hash
->table
[i
];
1579 hlist_for_each_entry_rcu(tt_common_entry
,
1581 tt_global
= container_of(tt_common_entry
,
1582 struct batadv_tt_global_entry
,
1584 batadv_tt_global_print_entry(bat_priv
, tt_global
, seq
);
1590 batadv_hardif_free_ref(primary_if
);
1595 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1596 * @tt_global_entry: the global entry to remove the orig_entry from
1597 * @orig_entry: the orig entry to remove and free
1599 * Remove an orig_entry from its list in the given tt_global_entry and
1600 * free this orig_entry afterwards.
1603 batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry
*tt_global_entry
,
1604 struct batadv_tt_orig_list_entry
*orig_entry
)
1606 batadv_tt_global_size_dec(orig_entry
->orig_node
,
1607 tt_global_entry
->common
.vid
);
1608 atomic_dec(&tt_global_entry
->orig_list_count
);
1609 hlist_del_rcu(&orig_entry
->list
);
1610 batadv_tt_orig_list_entry_free_ref(orig_entry
);
1613 /* deletes the orig list of a tt_global_entry */
1615 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
1617 struct hlist_head
*head
;
1618 struct hlist_node
*safe
;
1619 struct batadv_tt_orig_list_entry
*orig_entry
;
1621 spin_lock_bh(&tt_global_entry
->list_lock
);
1622 head
= &tt_global_entry
->orig_list
;
1623 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
)
1624 batadv_tt_global_del_orig_entry(tt_global_entry
, orig_entry
);
1625 spin_unlock_bh(&tt_global_entry
->list_lock
);
1629 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1630 * @bat_priv: the bat priv with all the soft interface information
1631 * @tt_global_entry: the global entry to remove the orig_node from
1632 * @orig_node: the originator announcing the client
1633 * @message: message to append to the log on deletion
1635 * Remove the given orig_node and its according orig_entry from the given
1639 batadv_tt_global_del_orig_node(struct batadv_priv
*bat_priv
,
1640 struct batadv_tt_global_entry
*tt_global_entry
,
1641 struct batadv_orig_node
*orig_node
,
1642 const char *message
)
1644 struct hlist_head
*head
;
1645 struct hlist_node
*safe
;
1646 struct batadv_tt_orig_list_entry
*orig_entry
;
1649 spin_lock_bh(&tt_global_entry
->list_lock
);
1650 head
= &tt_global_entry
->orig_list
;
1651 hlist_for_each_entry_safe(orig_entry
, safe
, head
, list
) {
1652 if (orig_entry
->orig_node
== orig_node
) {
1653 vid
= tt_global_entry
->common
.vid
;
1654 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1655 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1657 tt_global_entry
->common
.addr
,
1658 BATADV_PRINT_VID(vid
), message
);
1659 batadv_tt_global_del_orig_entry(tt_global_entry
,
1663 spin_unlock_bh(&tt_global_entry
->list_lock
);
1666 /* If the client is to be deleted, we check if it is the last origantor entry
1667 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1668 * timer, otherwise we simply remove the originator scheduled for deletion.
1671 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
1672 struct batadv_tt_global_entry
*tt_global_entry
,
1673 struct batadv_orig_node
*orig_node
,
1674 const char *message
)
1676 bool last_entry
= true;
1677 struct hlist_head
*head
;
1678 struct batadv_tt_orig_list_entry
*orig_entry
;
1680 /* no local entry exists, case 1:
1681 * Check if this is the last one or if other entries exist.
1685 head
= &tt_global_entry
->orig_list
;
1686 hlist_for_each_entry_rcu(orig_entry
, head
, list
) {
1687 if (orig_entry
->orig_node
!= orig_node
) {
1695 /* its the last one, mark for roaming. */
1696 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
1697 tt_global_entry
->roam_at
= jiffies
;
1699 /* there is another entry, we can simply delete this
1700 * one and can still use the other one.
1702 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1703 orig_node
, message
);
1707 * batadv_tt_global_del - remove a client from the global table
1708 * @bat_priv: the bat priv with all the soft interface information
1709 * @orig_node: an originator serving this client
1710 * @addr: the mac address of the client
1711 * @vid: VLAN identifier
1712 * @message: a message explaining the reason for deleting the client to print
1713 * for debugging purpose
1714 * @roaming: true if the deletion has been triggered by a roaming event
1716 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
1717 struct batadv_orig_node
*orig_node
,
1718 const unsigned char *addr
, unsigned short vid
,
1719 const char *message
, bool roaming
)
1721 struct batadv_tt_global_entry
*tt_global_entry
;
1722 struct batadv_tt_local_entry
*local_entry
= NULL
;
1724 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
1725 if (!tt_global_entry
)
1729 batadv_tt_global_del_orig_node(bat_priv
, tt_global_entry
,
1730 orig_node
, message
);
1732 if (hlist_empty(&tt_global_entry
->orig_list
))
1733 batadv_tt_global_free(bat_priv
, tt_global_entry
,
1739 /* if we are deleting a global entry due to a roam
1740 * event, there are two possibilities:
1741 * 1) the client roamed from node A to node B => if there
1742 * is only one originator left for this client, we mark
1743 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1744 * wait for node B to claim it. In case of timeout
1745 * the entry is purged.
1747 * If there are other originators left, we directly delete
1749 * 2) the client roamed to us => we can directly delete
1750 * the global entry, since it is useless now.
1752 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1753 tt_global_entry
->common
.addr
,
1756 /* local entry exists, case 2: client roamed to us. */
1757 batadv_tt_global_del_orig_list(tt_global_entry
);
1758 batadv_tt_global_free(bat_priv
, tt_global_entry
, message
);
1760 /* no local entry exists, case 1: check for roaming */
1761 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1762 orig_node
, message
);
1766 if (tt_global_entry
)
1767 batadv_tt_global_entry_free_ref(tt_global_entry
);
1769 batadv_tt_local_entry_free_ref(local_entry
);
1773 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1774 * given originator matching the provided vid
1775 * @bat_priv: the bat priv with all the soft interface information
1776 * @orig_node: the originator owning the entries to remove
1777 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1779 * @message: debug message to print as "reason"
1781 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1782 struct batadv_orig_node
*orig_node
,
1784 const char *message
)
1786 struct batadv_tt_global_entry
*tt_global
;
1787 struct batadv_tt_common_entry
*tt_common_entry
;
1789 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1790 struct hlist_node
*safe
;
1791 struct hlist_head
*head
;
1792 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1798 for (i
= 0; i
< hash
->size
; i
++) {
1799 head
= &hash
->table
[i
];
1800 list_lock
= &hash
->list_locks
[i
];
1802 spin_lock_bh(list_lock
);
1803 hlist_for_each_entry_safe(tt_common_entry
, safe
,
1805 /* remove only matching entries */
1806 if (match_vid
>= 0 && tt_common_entry
->vid
!= match_vid
)
1809 tt_global
= container_of(tt_common_entry
,
1810 struct batadv_tt_global_entry
,
1813 batadv_tt_global_del_orig_node(bat_priv
, tt_global
,
1814 orig_node
, message
);
1816 if (hlist_empty(&tt_global
->orig_list
)) {
1817 vid
= tt_global
->common
.vid
;
1818 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1819 "Deleting global tt entry %pM (vid: %d): %s\n",
1820 tt_global
->common
.addr
,
1821 BATADV_PRINT_VID(vid
), message
);
1822 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1823 batadv_tt_global_entry_free_ref(tt_global
);
1826 spin_unlock_bh(list_lock
);
1828 orig_node
->capa_initialized
&= ~BATADV_ORIG_CAPA_HAS_TT
;
1831 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1835 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1836 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1838 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1839 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1841 *msg
= "Roaming timeout\n";
1844 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1845 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1847 *msg
= "Temporary client timeout\n";
1853 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1855 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1856 struct hlist_head
*head
;
1857 struct hlist_node
*node_tmp
;
1858 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1861 struct batadv_tt_common_entry
*tt_common
;
1862 struct batadv_tt_global_entry
*tt_global
;
1864 for (i
= 0; i
< hash
->size
; i
++) {
1865 head
= &hash
->table
[i
];
1866 list_lock
= &hash
->list_locks
[i
];
1868 spin_lock_bh(list_lock
);
1869 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
1871 tt_global
= container_of(tt_common
,
1872 struct batadv_tt_global_entry
,
1875 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1878 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1879 "Deleting global tt entry %pM (vid: %d): %s\n",
1880 tt_global
->common
.addr
,
1881 BATADV_PRINT_VID(tt_global
->common
.vid
),
1884 hlist_del_rcu(&tt_common
->hash_entry
);
1886 batadv_tt_global_entry_free_ref(tt_global
);
1888 spin_unlock_bh(list_lock
);
1892 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
1894 struct batadv_hashtable
*hash
;
1895 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1896 struct batadv_tt_common_entry
*tt_common_entry
;
1897 struct batadv_tt_global_entry
*tt_global
;
1898 struct hlist_node
*node_tmp
;
1899 struct hlist_head
*head
;
1902 if (!bat_priv
->tt
.global_hash
)
1905 hash
= bat_priv
->tt
.global_hash
;
1907 for (i
= 0; i
< hash
->size
; i
++) {
1908 head
= &hash
->table
[i
];
1909 list_lock
= &hash
->list_locks
[i
];
1911 spin_lock_bh(list_lock
);
1912 hlist_for_each_entry_safe(tt_common_entry
, node_tmp
,
1914 hlist_del_rcu(&tt_common_entry
->hash_entry
);
1915 tt_global
= container_of(tt_common_entry
,
1916 struct batadv_tt_global_entry
,
1918 batadv_tt_global_entry_free_ref(tt_global
);
1920 spin_unlock_bh(list_lock
);
1923 batadv_hash_destroy(hash
);
1925 bat_priv
->tt
.global_hash
= NULL
;
1929 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
1930 struct batadv_tt_global_entry
*tt_global_entry
)
1934 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
1935 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
1938 /* check if the two clients are marked as isolated */
1939 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
&&
1940 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ISOLA
)
1947 * batadv_transtable_search - get the mesh destination for a given client
1948 * @bat_priv: the bat priv with all the soft interface information
1949 * @src: mac address of the source client
1950 * @addr: mac address of the destination client
1951 * @vid: VLAN identifier
1953 * Returns a pointer to the originator that was selected as destination in the
1954 * mesh for contacting the client 'addr', NULL otherwise.
1955 * In case of multiple originators serving the same client, the function returns
1956 * the best one (best in terms of metric towards the destination node).
1958 * If the two clients are AP isolated the function returns NULL.
1960 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
1962 const uint8_t *addr
,
1965 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
1966 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
1967 struct batadv_orig_node
*orig_node
= NULL
;
1968 struct batadv_tt_orig_list_entry
*best_entry
;
1970 if (src
&& batadv_vlan_ap_isola_get(bat_priv
, vid
)) {
1971 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
, vid
);
1972 if (!tt_local_entry
||
1973 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
))
1977 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
1978 if (!tt_global_entry
)
1981 /* check whether the clients should not communicate due to AP
1984 if (tt_local_entry
&&
1985 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
1989 best_entry
= batadv_transtable_best_orig(bat_priv
, tt_global_entry
);
1990 /* found anything? */
1992 orig_node
= best_entry
->orig_node
;
1993 if (orig_node
&& !atomic_inc_not_zero(&orig_node
->refcount
))
1998 if (tt_global_entry
)
1999 batadv_tt_global_entry_free_ref(tt_global_entry
);
2001 batadv_tt_local_entry_free_ref(tt_local_entry
);
2007 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2008 * to the given orig_node
2009 * @bat_priv: the bat priv with all the soft interface information
2010 * @orig_node: originator for which the CRC should be computed
2011 * @vid: VLAN identifier for which the CRC32 has to be computed
2013 * This function computes the checksum for the global table corresponding to a
2014 * specific originator. In particular, the checksum is computed as follows: For
2015 * each client connected to the originator the CRC32C of the MAC address and the
2016 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2019 * The idea behind is that CRC32C should be used as much as possible in order to
2020 * produce a unique hash of the table, but since the order which is used to feed
2021 * the CRC32C function affects the result and since every node in the network
2022 * probably sorts the clients differently, the hash function cannot be directly
2023 * computed over the entire table. Hence the CRC32C is used only on
2024 * the single client entry, while all the results are then xor'ed together
2025 * because the XOR operation can combine them all while trying to reduce the
2026 * noise as much as possible.
2028 * Returns the checksum of the global table of a given originator.
2030 static uint32_t batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
2031 struct batadv_orig_node
*orig_node
,
2034 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
2035 struct batadv_tt_common_entry
*tt_common
;
2036 struct batadv_tt_global_entry
*tt_global
;
2037 struct hlist_head
*head
;
2038 uint32_t i
, crc_tmp
, crc
= 0;
2042 for (i
= 0; i
< hash
->size
; i
++) {
2043 head
= &hash
->table
[i
];
2046 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2047 tt_global
= container_of(tt_common
,
2048 struct batadv_tt_global_entry
,
2050 /* compute the CRC only for entries belonging to the
2051 * VLAN identified by the vid passed as parameter
2053 if (tt_common
->vid
!= vid
)
2056 /* Roaming clients are in the global table for
2057 * consistency only. They don't have to be
2058 * taken into account while computing the
2061 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
2063 /* Temporary clients have not been announced yet, so
2064 * they have to be skipped while computing the global
2067 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
2070 /* find out if this global entry is announced by this
2073 if (!batadv_tt_global_entry_has_orig(tt_global
,
2077 /* use network order to read the VID: this ensures that
2078 * every node reads the bytes in the same order.
2080 tmp_vid
= htons(tt_common
->vid
);
2081 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2083 /* compute the CRC on flags that have to be kept in sync
2086 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2087 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2089 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2098 * batadv_tt_local_crc - calculates the checksum of the local table
2099 * @bat_priv: the bat priv with all the soft interface information
2100 * @vid: VLAN identifier for which the CRC32 has to be computed
2102 * For details about the computation, please refer to the documentation for
2103 * batadv_tt_global_crc().
2105 * Returns the checksum of the local table
2107 static uint32_t batadv_tt_local_crc(struct batadv_priv
*bat_priv
,
2110 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2111 struct batadv_tt_common_entry
*tt_common
;
2112 struct hlist_head
*head
;
2113 uint32_t i
, crc_tmp
, crc
= 0;
2117 for (i
= 0; i
< hash
->size
; i
++) {
2118 head
= &hash
->table
[i
];
2121 hlist_for_each_entry_rcu(tt_common
, head
, hash_entry
) {
2122 /* compute the CRC only for entries belonging to the
2123 * VLAN identified by vid
2125 if (tt_common
->vid
!= vid
)
2128 /* not yet committed clients have not to be taken into
2129 * account while computing the CRC
2131 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
2134 /* use network order to read the VID: this ensures that
2135 * every node reads the bytes in the same order.
2137 tmp_vid
= htons(tt_common
->vid
);
2138 crc_tmp
= crc32c(0, &tmp_vid
, sizeof(tmp_vid
));
2140 /* compute the CRC on flags that have to be kept in sync
2143 flags
= tt_common
->flags
& BATADV_TT_SYNC_MASK
;
2144 crc_tmp
= crc32c(crc_tmp
, &flags
, sizeof(flags
));
2146 crc
^= crc32c(crc_tmp
, tt_common
->addr
, ETH_ALEN
);
2154 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
2156 struct batadv_tt_req_node
*node
, *safe
;
2158 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2160 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2161 list_del(&node
->list
);
2165 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2168 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
2169 struct batadv_orig_node
*orig_node
,
2170 const void *tt_buff
,
2171 uint16_t tt_buff_len
)
2173 /* Replace the old buffer only if I received something in the
2174 * last OGM (the OGM could carry no changes)
2176 spin_lock_bh(&orig_node
->tt_buff_lock
);
2177 if (tt_buff_len
> 0) {
2178 kfree(orig_node
->tt_buff
);
2179 orig_node
->tt_buff_len
= 0;
2180 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
2181 if (orig_node
->tt_buff
) {
2182 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
2183 orig_node
->tt_buff_len
= tt_buff_len
;
2186 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2189 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
2191 struct batadv_tt_req_node
*node
, *safe
;
2193 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2194 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2195 if (batadv_has_timed_out(node
->issued_at
,
2196 BATADV_TT_REQUEST_TIMEOUT
)) {
2197 list_del(&node
->list
);
2201 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2204 /* returns the pointer to the new tt_req_node struct if no request
2205 * has already been issued for this orig_node, NULL otherwise
2207 static struct batadv_tt_req_node
*
2208 batadv_new_tt_req_node(struct batadv_priv
*bat_priv
,
2209 struct batadv_orig_node
*orig_node
)
2211 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
2213 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2214 list_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
2215 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
2216 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
2217 BATADV_TT_REQUEST_TIMEOUT
))
2221 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
2225 ether_addr_copy(tt_req_node
->addr
, orig_node
->orig
);
2226 tt_req_node
->issued_at
= jiffies
;
2228 list_add(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
2230 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2235 * batadv_tt_local_valid - verify that given tt entry is a valid one
2236 * @entry_ptr: to be checked local tt entry
2237 * @data_ptr: not used but definition required to satisfy the callback prototype
2239 * Returns 1 if the entry is a valid, 0 otherwise.
2241 static int batadv_tt_local_valid(const void *entry_ptr
, const void *data_ptr
)
2243 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2245 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
2250 static int batadv_tt_global_valid(const void *entry_ptr
,
2251 const void *data_ptr
)
2253 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
2254 const struct batadv_tt_global_entry
*tt_global_entry
;
2255 const struct batadv_orig_node
*orig_node
= data_ptr
;
2257 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
2258 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
2261 tt_global_entry
= container_of(tt_common_entry
,
2262 struct batadv_tt_global_entry
,
2265 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
2269 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2271 * @bat_priv: the bat priv with all the soft interface information
2272 * @hash: hash table containing the tt entries
2273 * @tt_len: expected tvlv tt data buffer length in number of bytes
2274 * @tvlv_buff: pointer to the buffer to fill with the TT data
2275 * @valid_cb: function to filter tt change entries
2276 * @cb_data: data passed to the filter function as argument
2278 static void batadv_tt_tvlv_generate(struct batadv_priv
*bat_priv
,
2279 struct batadv_hashtable
*hash
,
2280 void *tvlv_buff
, uint16_t tt_len
,
2281 int (*valid_cb
)(const void *, const void *),
2284 struct batadv_tt_common_entry
*tt_common_entry
;
2285 struct batadv_tvlv_tt_change
*tt_change
;
2286 struct hlist_head
*head
;
2287 uint16_t tt_tot
, tt_num_entries
= 0;
2290 tt_tot
= batadv_tt_entries(tt_len
);
2291 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_buff
;
2294 for (i
= 0; i
< hash
->size
; i
++) {
2295 head
= &hash
->table
[i
];
2297 hlist_for_each_entry_rcu(tt_common_entry
,
2299 if (tt_tot
== tt_num_entries
)
2302 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
2305 ether_addr_copy(tt_change
->addr
, tt_common_entry
->addr
);
2306 tt_change
->flags
= tt_common_entry
->flags
;
2307 tt_change
->vid
= htons(tt_common_entry
->vid
);
2308 memset(tt_change
->reserved
, 0,
2309 sizeof(tt_change
->reserved
));
2319 * batadv_tt_global_check_crc - check if all the CRCs are correct
2320 * @orig_node: originator for which the CRCs have to be checked
2321 * @tt_vlan: pointer to the first tvlv VLAN entry
2322 * @num_vlan: number of tvlv VLAN entries
2323 * @create: if true, create VLAN objects if not found
2325 * Return true if all the received CRCs match the locally stored ones, false
2328 static bool batadv_tt_global_check_crc(struct batadv_orig_node
*orig_node
,
2329 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2332 struct batadv_tvlv_tt_vlan_data
*tt_vlan_tmp
;
2333 struct batadv_orig_node_vlan
*vlan
;
2337 /* check if each received CRC matches the locally stored one */
2338 for (i
= 0; i
< num_vlan
; i
++) {
2339 tt_vlan_tmp
= tt_vlan
+ i
;
2341 /* if orig_node is a backbone node for this VLAN, don't check
2342 * the CRC as we ignore all the global entries over it
2344 if (batadv_bla_is_backbone_gw_orig(orig_node
->bat_priv
,
2346 ntohs(tt_vlan_tmp
->vid
)))
2349 vlan
= batadv_orig_node_vlan_get(orig_node
,
2350 ntohs(tt_vlan_tmp
->vid
));
2355 batadv_orig_node_vlan_free_ref(vlan
);
2357 if (crc
!= ntohl(tt_vlan_tmp
->crc
))
2365 * batadv_tt_local_update_crc - update all the local CRCs
2366 * @bat_priv: the bat priv with all the soft interface information
2368 static void batadv_tt_local_update_crc(struct batadv_priv
*bat_priv
)
2370 struct batadv_softif_vlan
*vlan
;
2372 /* recompute the global CRC for each VLAN */
2374 hlist_for_each_entry_rcu(vlan
, &bat_priv
->softif_vlan_list
, list
) {
2375 vlan
->tt
.crc
= batadv_tt_local_crc(bat_priv
, vlan
->vid
);
2381 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2382 * @bat_priv: the bat priv with all the soft interface information
2383 * @orig_node: the orig_node for which the CRCs have to be updated
2385 static void batadv_tt_global_update_crc(struct batadv_priv
*bat_priv
,
2386 struct batadv_orig_node
*orig_node
)
2388 struct batadv_orig_node_vlan
*vlan
;
2391 /* recompute the global CRC for each VLAN */
2393 list_for_each_entry_rcu(vlan
, &orig_node
->vlan_list
, list
) {
2394 /* if orig_node is a backbone node for this VLAN, don't compute
2395 * the CRC as we ignore all the global entries over it
2397 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
,
2401 crc
= batadv_tt_global_crc(bat_priv
, orig_node
, vlan
->vid
);
2408 * batadv_send_tt_request - send a TT Request message to a given node
2409 * @bat_priv: the bat priv with all the soft interface information
2410 * @dst_orig_node: the destination of the message
2411 * @ttvn: the version number that the source of the message is looking for
2412 * @tt_vlan: pointer to the first tvlv VLAN object to request
2413 * @num_vlan: number of tvlv VLAN entries
2414 * @full_table: ask for the entire translation table if true, while only for the
2415 * last TT diff otherwise
2417 static int batadv_send_tt_request(struct batadv_priv
*bat_priv
,
2418 struct batadv_orig_node
*dst_orig_node
,
2420 struct batadv_tvlv_tt_vlan_data
*tt_vlan
,
2421 uint16_t num_vlan
, bool full_table
)
2423 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2424 struct batadv_tt_req_node
*tt_req_node
= NULL
;
2425 struct batadv_tvlv_tt_vlan_data
*tt_vlan_req
;
2426 struct batadv_hard_iface
*primary_if
;
2430 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2434 /* The new tt_req will be issued only if I'm not waiting for a
2435 * reply from the same orig_node yet
2437 tt_req_node
= batadv_new_tt_req_node(bat_priv
, dst_orig_node
);
2441 size
= sizeof(*tvlv_tt_data
) + sizeof(*tt_vlan_req
) * num_vlan
;
2442 tvlv_tt_data
= kzalloc(size
, GFP_ATOMIC
);
2446 tvlv_tt_data
->flags
= BATADV_TT_REQUEST
;
2447 tvlv_tt_data
->ttvn
= ttvn
;
2448 tvlv_tt_data
->num_vlan
= htons(num_vlan
);
2450 /* send all the CRCs within the request. This is needed by intermediate
2451 * nodes to ensure they have the correct table before replying
2453 tt_vlan_req
= (struct batadv_tvlv_tt_vlan_data
*)(tvlv_tt_data
+ 1);
2454 for (i
= 0; i
< num_vlan
; i
++) {
2455 tt_vlan_req
->vid
= tt_vlan
->vid
;
2456 tt_vlan_req
->crc
= tt_vlan
->crc
;
2463 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2465 batadv_dbg(BATADV_DBG_TT
, bat_priv
, "Sending TT_REQUEST to %pM [%c]\n",
2466 dst_orig_node
->orig
, full_table
? 'F' : '.');
2468 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
2469 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2470 dst_orig_node
->orig
, BATADV_TVLV_TT
, 1,
2471 tvlv_tt_data
, size
);
2476 batadv_hardif_free_ref(primary_if
);
2477 if (ret
&& tt_req_node
) {
2478 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2479 list_del(&tt_req_node
->list
);
2480 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2483 kfree(tvlv_tt_data
);
2488 * batadv_send_other_tt_response - send reply to tt request concerning another
2489 * node's translation table
2490 * @bat_priv: the bat priv with all the soft interface information
2491 * @tt_data: tt data containing the tt request information
2492 * @req_src: mac address of tt request sender
2493 * @req_dst: mac address of tt request recipient
2495 * Returns true if tt request reply was sent, false otherwise.
2497 static bool batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
2498 struct batadv_tvlv_tt_data
*tt_data
,
2499 uint8_t *req_src
, uint8_t *req_dst
)
2501 struct batadv_orig_node
*req_dst_orig_node
;
2502 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
2503 struct batadv_tvlv_tt_change
*tt_change
;
2504 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2505 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
2506 bool ret
= false, full_table
;
2507 uint8_t orig_ttvn
, req_ttvn
;
2511 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2512 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2513 req_src
, tt_data
->ttvn
, req_dst
,
2514 (tt_data
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
2516 /* Let's get the orig node of the REAL destination */
2517 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_dst
);
2518 if (!req_dst_orig_node
)
2521 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2522 if (!res_dst_orig_node
)
2525 orig_ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
2526 req_ttvn
= tt_data
->ttvn
;
2528 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
2529 /* this node doesn't have the requested data */
2530 if (orig_ttvn
!= req_ttvn
||
2531 !batadv_tt_global_check_crc(req_dst_orig_node
, tt_vlan
,
2532 ntohs(tt_data
->num_vlan
)))
2535 /* If the full table has been explicitly requested */
2536 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
||
2537 !req_dst_orig_node
->tt_buff
)
2542 /* TT fragmentation hasn't been implemented yet, so send as many
2543 * TT entries fit a single packet as possible only
2546 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
2547 tt_len
= req_dst_orig_node
->tt_buff_len
;
2549 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2556 /* Copy the last orig_node's OGM buffer */
2557 memcpy(tt_change
, req_dst_orig_node
->tt_buff
,
2558 req_dst_orig_node
->tt_buff_len
);
2559 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2561 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2562 * in the initial part
2565 tvlv_len
= batadv_tt_prepare_tvlv_global_data(req_dst_orig_node
,
2572 /* fill the rest of the tvlv with the real TT entries */
2573 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.global_hash
,
2575 batadv_tt_global_valid
,
2579 /* Don't send the response, if larger than fragmented packet. */
2580 tt_len
= sizeof(struct batadv_unicast_tvlv_packet
) + tvlv_len
;
2581 if (tt_len
> atomic_read(&bat_priv
->packet_size_max
)) {
2582 net_ratelimited_function(batadv_info
, bat_priv
->soft_iface
,
2583 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2584 res_dst_orig_node
->orig
);
2588 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2589 tvlv_tt_data
->ttvn
= req_ttvn
;
2592 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2594 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2595 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2596 res_dst_orig_node
->orig
, req_dst_orig_node
->orig
,
2597 full_table
? 'F' : '.', req_ttvn
);
2599 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2601 batadv_tvlv_unicast_send(bat_priv
, req_dst_orig_node
->orig
,
2602 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2609 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
2612 if (res_dst_orig_node
)
2613 batadv_orig_node_free_ref(res_dst_orig_node
);
2614 if (req_dst_orig_node
)
2615 batadv_orig_node_free_ref(req_dst_orig_node
);
2616 kfree(tvlv_tt_data
);
2621 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2623 * @bat_priv: the bat priv with all the soft interface information
2624 * @tt_data: tt data containing the tt request information
2625 * @req_src: mac address of tt request sender
2627 * Returns true if tt request reply was sent, false otherwise.
2629 static bool batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
2630 struct batadv_tvlv_tt_data
*tt_data
,
2633 struct batadv_tvlv_tt_data
*tvlv_tt_data
= NULL
;
2634 struct batadv_hard_iface
*primary_if
= NULL
;
2635 struct batadv_tvlv_tt_change
*tt_change
;
2636 struct batadv_orig_node
*orig_node
;
2637 uint8_t my_ttvn
, req_ttvn
;
2642 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2643 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2644 req_src
, tt_data
->ttvn
,
2645 (tt_data
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
2647 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
2649 my_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
2650 req_ttvn
= tt_data
->ttvn
;
2652 orig_node
= batadv_orig_hash_find(bat_priv
, req_src
);
2656 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2660 /* If the full table has been explicitly requested or the gap
2661 * is too big send the whole local translation table
2663 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
2664 !bat_priv
->tt
.last_changeset
)
2669 /* TT fragmentation hasn't been implemented yet, so send as many
2670 * TT entries fit a single packet as possible only
2673 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
2675 tt_len
= bat_priv
->tt
.last_changeset_len
;
2676 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2683 /* Copy the last orig_node's OGM buffer */
2684 memcpy(tt_change
, bat_priv
->tt
.last_changeset
,
2685 bat_priv
->tt
.last_changeset_len
);
2686 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2688 req_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
2690 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2691 * in the initial part
2694 tvlv_len
= batadv_tt_prepare_tvlv_local_data(bat_priv
,
2701 /* fill the rest of the tvlv with the real TT entries */
2702 batadv_tt_tvlv_generate(bat_priv
, bat_priv
->tt
.local_hash
,
2704 batadv_tt_local_valid
, NULL
);
2707 tvlv_tt_data
->flags
= BATADV_TT_RESPONSE
;
2708 tvlv_tt_data
->ttvn
= req_ttvn
;
2711 tvlv_tt_data
->flags
|= BATADV_TT_FULL_TABLE
;
2713 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2714 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2715 orig_node
->orig
, full_table
? 'F' : '.', req_ttvn
);
2717 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
2719 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
2720 req_src
, BATADV_TVLV_TT
, 1, tvlv_tt_data
,
2726 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
2728 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
2730 batadv_orig_node_free_ref(orig_node
);
2732 batadv_hardif_free_ref(primary_if
);
2733 kfree(tvlv_tt_data
);
2734 /* The packet was for this host, so it doesn't need to be re-routed */
2739 * batadv_send_tt_response - send reply to tt request
2740 * @bat_priv: the bat priv with all the soft interface information
2741 * @tt_data: tt data containing the tt request information
2742 * @req_src: mac address of tt request sender
2743 * @req_dst: mac address of tt request recipient
2745 * Returns true if tt request reply was sent, false otherwise.
2747 static bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
2748 struct batadv_tvlv_tt_data
*tt_data
,
2749 uint8_t *req_src
, uint8_t *req_dst
)
2751 if (batadv_is_my_mac(bat_priv
, req_dst
))
2752 return batadv_send_my_tt_response(bat_priv
, tt_data
, req_src
);
2754 return batadv_send_other_tt_response(bat_priv
, tt_data
,
2758 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2759 struct batadv_orig_node
*orig_node
,
2760 struct batadv_tvlv_tt_change
*tt_change
,
2761 uint16_t tt_num_changes
, uint8_t ttvn
)
2766 for (i
= 0; i
< tt_num_changes
; i
++) {
2767 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
2768 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
2769 batadv_tt_global_del(bat_priv
, orig_node
,
2770 (tt_change
+ i
)->addr
,
2771 ntohs((tt_change
+ i
)->vid
),
2772 "tt removed by changes",
2775 if (!batadv_tt_global_add(bat_priv
, orig_node
,
2776 (tt_change
+ i
)->addr
,
2777 ntohs((tt_change
+ i
)->vid
),
2778 (tt_change
+ i
)->flags
, ttvn
))
2779 /* In case of problem while storing a
2780 * global_entry, we stop the updating
2781 * procedure without committing the
2782 * ttvn change. This will avoid to send
2783 * corrupted data on tt_request
2788 orig_node
->capa_initialized
|= BATADV_ORIG_CAPA_HAS_TT
;
2791 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
2792 struct batadv_tvlv_tt_change
*tt_change
,
2793 uint8_t ttvn
, uint8_t *resp_src
,
2794 uint16_t num_entries
)
2796 struct batadv_orig_node
*orig_node
;
2798 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2802 /* Purge the old table first.. */
2803 batadv_tt_global_del_orig(bat_priv
, orig_node
, -1,
2804 "Received full table");
2806 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
, num_entries
,
2809 spin_lock_bh(&orig_node
->tt_buff_lock
);
2810 kfree(orig_node
->tt_buff
);
2811 orig_node
->tt_buff_len
= 0;
2812 orig_node
->tt_buff
= NULL
;
2813 spin_unlock_bh(&orig_node
->tt_buff_lock
);
2815 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2819 batadv_orig_node_free_ref(orig_node
);
2822 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
2823 struct batadv_orig_node
*orig_node
,
2824 uint16_t tt_num_changes
, uint8_t ttvn
,
2825 struct batadv_tvlv_tt_change
*tt_change
)
2827 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
2828 tt_num_changes
, ttvn
);
2830 batadv_tt_save_orig_buffer(bat_priv
, orig_node
, tt_change
,
2831 batadv_tt_len(tt_num_changes
));
2832 atomic_set(&orig_node
->last_ttvn
, ttvn
);
2836 * batadv_is_my_client - check if a client is served by the local node
2837 * @bat_priv: the bat priv with all the soft interface information
2838 * @addr: the mac adress of the client to check
2839 * @vid: VLAN identifier
2841 * Returns true if the client is served by this node, false otherwise.
2843 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
2846 struct batadv_tt_local_entry
*tt_local_entry
;
2849 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
2850 if (!tt_local_entry
)
2852 /* Check if the client has been logically deleted (but is kept for
2853 * consistency purpose)
2855 if ((tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
) ||
2856 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
2861 batadv_tt_local_entry_free_ref(tt_local_entry
);
2866 * batadv_handle_tt_response - process incoming tt reply
2867 * @bat_priv: the bat priv with all the soft interface information
2868 * @tt_data: tt data containing the tt request information
2869 * @resp_src: mac address of tt reply sender
2870 * @num_entries: number of tt change entries appended to the tt data
2872 static void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
2873 struct batadv_tvlv_tt_data
*tt_data
,
2874 uint8_t *resp_src
, uint16_t num_entries
)
2876 struct batadv_tt_req_node
*node
, *safe
;
2877 struct batadv_orig_node
*orig_node
= NULL
;
2878 struct batadv_tvlv_tt_change
*tt_change
;
2879 uint8_t *tvlv_ptr
= (uint8_t *)tt_data
;
2880 uint16_t change_offset
;
2882 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2883 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2884 resp_src
, tt_data
->ttvn
, num_entries
,
2885 (tt_data
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
2887 orig_node
= batadv_orig_hash_find(bat_priv
, resp_src
);
2891 spin_lock_bh(&orig_node
->tt_lock
);
2893 change_offset
= sizeof(struct batadv_tvlv_tt_vlan_data
);
2894 change_offset
*= ntohs(tt_data
->num_vlan
);
2895 change_offset
+= sizeof(*tt_data
);
2896 tvlv_ptr
+= change_offset
;
2898 tt_change
= (struct batadv_tvlv_tt_change
*)tvlv_ptr
;
2899 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
) {
2900 batadv_tt_fill_gtable(bat_priv
, tt_change
, tt_data
->ttvn
,
2901 resp_src
, num_entries
);
2903 batadv_tt_update_changes(bat_priv
, orig_node
, num_entries
,
2904 tt_data
->ttvn
, tt_change
);
2907 /* Recalculate the CRC for this orig_node and store it */
2908 batadv_tt_global_update_crc(bat_priv
, orig_node
);
2910 spin_unlock_bh(&orig_node
->tt_lock
);
2912 /* Delete the tt_req_node from pending tt_requests list */
2913 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
2914 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
2915 if (!batadv_compare_eth(node
->addr
, resp_src
))
2917 list_del(&node
->list
);
2921 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
2924 batadv_orig_node_free_ref(orig_node
);
2927 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
2929 struct batadv_tt_roam_node
*node
, *safe
;
2931 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2933 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2934 list_del(&node
->list
);
2938 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2941 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
2943 struct batadv_tt_roam_node
*node
, *safe
;
2945 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2946 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2947 if (!batadv_has_timed_out(node
->first_time
,
2948 BATADV_ROAMING_MAX_TIME
))
2951 list_del(&node
->list
);
2954 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2957 /* This function checks whether the client already reached the
2958 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2961 * returns true if the ROAMING_ADV can be sent, false otherwise
2963 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
,
2966 struct batadv_tt_roam_node
*tt_roam_node
;
2969 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2970 /* The new tt_req will be issued only if I'm not waiting for a
2971 * reply from the same orig_node yet
2973 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
2974 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
2977 if (batadv_has_timed_out(tt_roam_node
->first_time
,
2978 BATADV_ROAMING_MAX_TIME
))
2981 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
2982 /* Sorry, you roamed too many times! */
2989 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
2993 tt_roam_node
->first_time
= jiffies
;
2994 atomic_set(&tt_roam_node
->counter
,
2995 BATADV_ROAMING_MAX_COUNT
- 1);
2996 ether_addr_copy(tt_roam_node
->addr
, client
);
2998 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
3003 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
3008 * batadv_send_roam_adv - send a roaming advertisement message
3009 * @bat_priv: the bat priv with all the soft interface information
3010 * @client: mac address of the roaming client
3011 * @vid: VLAN identifier
3012 * @orig_node: message destination
3014 * Send a ROAMING_ADV message to the node which was previously serving this
3015 * client. This is done to inform the node that from now on all traffic destined
3016 * for this particular roamed client has to be forwarded to the sender of the
3019 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
3021 struct batadv_orig_node
*orig_node
)
3023 struct batadv_hard_iface
*primary_if
;
3024 struct batadv_tvlv_roam_adv tvlv_roam
;
3026 primary_if
= batadv_primary_if_get_selected(bat_priv
);
3030 /* before going on we have to check whether the client has
3031 * already roamed to us too many times
3033 if (!batadv_tt_check_roam_count(bat_priv
, client
))
3036 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3037 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3038 orig_node
->orig
, client
, BATADV_PRINT_VID(vid
));
3040 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
3042 memcpy(tvlv_roam
.client
, client
, sizeof(tvlv_roam
.client
));
3043 tvlv_roam
.vid
= htons(vid
);
3045 batadv_tvlv_unicast_send(bat_priv
, primary_if
->net_dev
->dev_addr
,
3046 orig_node
->orig
, BATADV_TVLV_ROAM
, 1,
3047 &tvlv_roam
, sizeof(tvlv_roam
));
3051 batadv_hardif_free_ref(primary_if
);
3054 static void batadv_tt_purge(struct work_struct
*work
)
3056 struct delayed_work
*delayed_work
;
3057 struct batadv_priv_tt
*priv_tt
;
3058 struct batadv_priv
*bat_priv
;
3060 delayed_work
= container_of(work
, struct delayed_work
, work
);
3061 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
3062 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
3064 batadv_tt_local_purge(bat_priv
, BATADV_TT_LOCAL_TIMEOUT
);
3065 batadv_tt_global_purge(bat_priv
);
3066 batadv_tt_req_purge(bat_priv
);
3067 batadv_tt_roam_purge(bat_priv
);
3069 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3070 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3073 void batadv_tt_free(struct batadv_priv
*bat_priv
)
3075 batadv_tvlv_container_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3076 batadv_tvlv_handler_unregister(bat_priv
, BATADV_TVLV_TT
, 1);
3078 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
3080 batadv_tt_local_table_free(bat_priv
);
3081 batadv_tt_global_table_free(bat_priv
);
3082 batadv_tt_req_list_free(bat_priv
);
3083 batadv_tt_changes_list_free(bat_priv
);
3084 batadv_tt_roam_list_free(bat_priv
);
3086 kfree(bat_priv
->tt
.last_changeset
);
3090 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3091 * table and possibly count them in the TT size
3092 * @bat_priv: the bat priv with all the soft interface information
3093 * @flags: the flag to switch
3094 * @enable: whether to set or unset the flag
3095 * @count: whether to increase the TT size by the number of changed entries
3097 static void batadv_tt_local_set_flags(struct batadv_priv
*bat_priv
,
3098 uint16_t flags
, bool enable
, bool count
)
3100 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3101 struct batadv_tt_common_entry
*tt_common_entry
;
3102 uint16_t changed_num
= 0;
3103 struct hlist_head
*head
;
3109 for (i
= 0; i
< hash
->size
; i
++) {
3110 head
= &hash
->table
[i
];
3113 hlist_for_each_entry_rcu(tt_common_entry
,
3116 if ((tt_common_entry
->flags
& flags
) == flags
)
3118 tt_common_entry
->flags
|= flags
;
3120 if (!(tt_common_entry
->flags
& flags
))
3122 tt_common_entry
->flags
&= ~flags
;
3129 batadv_tt_local_size_inc(bat_priv
,
3130 tt_common_entry
->vid
);
3136 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3137 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
3139 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
3140 struct batadv_tt_common_entry
*tt_common
;
3141 struct batadv_tt_local_entry
*tt_local
;
3142 struct hlist_node
*node_tmp
;
3143 struct hlist_head
*head
;
3144 spinlock_t
*list_lock
; /* protects write access to the hash lists */
3150 for (i
= 0; i
< hash
->size
; i
++) {
3151 head
= &hash
->table
[i
];
3152 list_lock
= &hash
->list_locks
[i
];
3154 spin_lock_bh(list_lock
);
3155 hlist_for_each_entry_safe(tt_common
, node_tmp
, head
,
3157 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
3160 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3161 "Deleting local tt entry (%pM, vid: %d): pending\n",
3163 BATADV_PRINT_VID(tt_common
->vid
));
3165 batadv_tt_local_size_dec(bat_priv
, tt_common
->vid
);
3166 hlist_del_rcu(&tt_common
->hash_entry
);
3167 tt_local
= container_of(tt_common
,
3168 struct batadv_tt_local_entry
,
3170 batadv_tt_local_entry_free_ref(tt_local
);
3172 spin_unlock_bh(list_lock
);
3177 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3178 * which have been queued in the time since the last commit
3179 * @bat_priv: the bat priv with all the soft interface information
3181 * Caller must hold tt->commit_lock.
3183 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv
*bat_priv
)
3185 /* Update multicast addresses in local translation table */
3186 batadv_mcast_mla_update(bat_priv
);
3188 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1) {
3189 if (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))
3190 batadv_tt_tvlv_container_update(bat_priv
);
3194 batadv_tt_local_set_flags(bat_priv
, BATADV_TT_CLIENT_NEW
, false, true);
3196 batadv_tt_local_purge_pending_clients(bat_priv
);
3197 batadv_tt_local_update_crc(bat_priv
);
3199 /* Increment the TTVN only once per OGM interval */
3200 atomic_inc(&bat_priv
->tt
.vn
);
3201 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3202 "Local changes committed, updating to ttvn %u\n",
3203 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
3205 /* reset the sending counter */
3206 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
3207 batadv_tt_tvlv_container_update(bat_priv
);
3211 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3212 * have been queued in the time since the last commit
3213 * @bat_priv: the bat priv with all the soft interface information
3215 void batadv_tt_local_commit_changes(struct batadv_priv
*bat_priv
)
3217 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3218 batadv_tt_local_commit_changes_nolock(bat_priv
);
3219 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3222 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, uint8_t *src
,
3223 uint8_t *dst
, unsigned short vid
)
3225 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
3226 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
3227 struct batadv_softif_vlan
*vlan
;
3230 vlan
= batadv_softif_vlan_get(bat_priv
, vid
);
3231 if (!vlan
|| !atomic_read(&vlan
->ap_isolation
))
3234 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
, vid
);
3235 if (!tt_local_entry
)
3238 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
, vid
);
3239 if (!tt_global_entry
)
3242 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
3249 batadv_softif_vlan_free_ref(vlan
);
3250 if (tt_global_entry
)
3251 batadv_tt_global_entry_free_ref(tt_global_entry
);
3253 batadv_tt_local_entry_free_ref(tt_local_entry
);
3258 * batadv_tt_update_orig - update global translation table with new tt
3259 * information received via ogms
3260 * @bat_priv: the bat priv with all the soft interface information
3261 * @orig: the orig_node of the ogm
3262 * @tt_vlan: pointer to the first tvlv VLAN entry
3263 * @tt_num_vlan: number of tvlv VLAN entries
3264 * @tt_change: pointer to the first entry in the TT buffer
3265 * @tt_num_changes: number of tt changes inside the tt buffer
3266 * @ttvn: translation table version number of this changeset
3267 * @tt_crc: crc32 checksum of orig node's translation table
3269 static void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
3270 struct batadv_orig_node
*orig_node
,
3271 const void *tt_buff
, uint16_t tt_num_vlan
,
3272 struct batadv_tvlv_tt_change
*tt_change
,
3273 uint16_t tt_num_changes
, uint8_t ttvn
)
3275 uint8_t orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
3276 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3277 bool full_table
= true;
3280 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)tt_buff
;
3281 has_tt_init
= orig_node
->capa_initialized
& BATADV_ORIG_CAPA_HAS_TT
;
3283 /* orig table not initialised AND first diff is in the OGM OR the ttvn
3284 * increased by one -> we can apply the attached changes
3286 if ((!has_tt_init
&& ttvn
== 1) || ttvn
- orig_ttvn
== 1) {
3287 /* the OGM could not contain the changes due to their size or
3288 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3290 * In this case send a tt request
3292 if (!tt_num_changes
) {
3297 spin_lock_bh(&orig_node
->tt_lock
);
3299 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
3302 /* Even if we received the precomputed crc with the OGM, we
3303 * prefer to recompute it to spot any possible inconsistency
3304 * in the global table
3306 batadv_tt_global_update_crc(bat_priv
, orig_node
);
3308 spin_unlock_bh(&orig_node
->tt_lock
);
3310 /* The ttvn alone is not enough to guarantee consistency
3311 * because a single value could represent different states
3312 * (due to the wrap around). Thus a node has to check whether
3313 * the resulting table (after applying the changes) is still
3314 * consistent or not. E.g. a node could disconnect while its
3315 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3316 * checking the CRC value is mandatory to detect the
3319 if (!batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3323 /* if we missed more than one change or our tables are not
3324 * in sync anymore -> request fresh tt data
3326 if (!has_tt_init
|| ttvn
!= orig_ttvn
||
3327 !batadv_tt_global_check_crc(orig_node
, tt_vlan
,
3330 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3331 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3332 orig_node
->orig
, ttvn
, orig_ttvn
,
3334 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
3335 tt_vlan
, tt_num_vlan
,
3343 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3344 * @bat_priv: the bat priv with all the soft interface information
3345 * @addr: the mac address of the client to check
3346 * @vid: VLAN identifier
3348 * Returns true if we know that the client has moved from its old originator
3349 * to another one. This entry is still kept for consistency purposes and will be
3350 * deleted later by a DEL or because of timeout
3352 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
3353 uint8_t *addr
, unsigned short vid
)
3355 struct batadv_tt_global_entry
*tt_global_entry
;
3358 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3359 if (!tt_global_entry
)
3362 ret
= tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3363 batadv_tt_global_entry_free_ref(tt_global_entry
);
3369 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3370 * @bat_priv: the bat priv with all the soft interface information
3371 * @addr: the mac address of the local client to query
3372 * @vid: VLAN identifier
3374 * Returns true if the local client is known to be roaming (it is not served by
3375 * this node anymore) or not. If yes, the client is still present in the table
3376 * to keep the latter consistent with the node TTVN
3378 bool batadv_tt_local_client_is_roaming(struct batadv_priv
*bat_priv
,
3379 uint8_t *addr
, unsigned short vid
)
3381 struct batadv_tt_local_entry
*tt_local_entry
;
3384 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
, vid
);
3385 if (!tt_local_entry
)
3388 ret
= tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
3389 batadv_tt_local_entry_free_ref(tt_local_entry
);
3394 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
3395 struct batadv_orig_node
*orig_node
,
3396 const unsigned char *addr
,
3401 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
, vid
,
3402 BATADV_TT_CLIENT_TEMP
,
3403 atomic_read(&orig_node
->last_ttvn
)))
3406 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3407 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3408 addr
, BATADV_PRINT_VID(vid
), orig_node
->orig
);
3415 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3416 * maximum packet size that can be transported through the mesh
3417 * @soft_iface: netdev struct of the mesh interface
3419 * Remove entries older than 'timeout' and half timeout if more entries need
3422 void batadv_tt_local_resize_to_mtu(struct net_device
*soft_iface
)
3424 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
3425 int packet_size_max
= atomic_read(&bat_priv
->packet_size_max
);
3426 int table_size
, timeout
= BATADV_TT_LOCAL_TIMEOUT
/ 2;
3427 bool reduced
= false;
3429 spin_lock_bh(&bat_priv
->tt
.commit_lock
);
3432 table_size
= batadv_tt_local_table_transmit_size(bat_priv
);
3433 if (packet_size_max
>= table_size
)
3436 batadv_tt_local_purge(bat_priv
, timeout
);
3437 batadv_tt_local_purge_pending_clients(bat_priv
);
3441 net_ratelimited_function(batadv_info
, soft_iface
,
3442 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3446 /* commit these changes immediately, to avoid synchronization problem
3450 batadv_tt_local_commit_changes_nolock(bat_priv
);
3452 spin_unlock_bh(&bat_priv
->tt
.commit_lock
);
3456 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3457 * @bat_priv: the bat priv with all the soft interface information
3458 * @orig: the orig_node of the ogm
3459 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3460 * @tvlv_value: tvlv buffer containing the gateway data
3461 * @tvlv_value_len: tvlv buffer length
3463 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv
*bat_priv
,
3464 struct batadv_orig_node
*orig
,
3465 uint8_t flags
, void *tvlv_value
,
3466 uint16_t tvlv_value_len
)
3468 struct batadv_tvlv_tt_vlan_data
*tt_vlan
;
3469 struct batadv_tvlv_tt_change
*tt_change
;
3470 struct batadv_tvlv_tt_data
*tt_data
;
3471 uint16_t num_entries
, num_vlan
;
3473 if (tvlv_value_len
< sizeof(*tt_data
))
3476 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3477 tvlv_value_len
-= sizeof(*tt_data
);
3479 num_vlan
= ntohs(tt_data
->num_vlan
);
3481 if (tvlv_value_len
< sizeof(*tt_vlan
) * num_vlan
)
3484 tt_vlan
= (struct batadv_tvlv_tt_vlan_data
*)(tt_data
+ 1);
3485 tt_change
= (struct batadv_tvlv_tt_change
*)(tt_vlan
+ num_vlan
);
3486 tvlv_value_len
-= sizeof(*tt_vlan
) * num_vlan
;
3488 num_entries
= batadv_tt_entries(tvlv_value_len
);
3490 batadv_tt_update_orig(bat_priv
, orig
, tt_vlan
, num_vlan
, tt_change
,
3491 num_entries
, tt_data
->ttvn
);
3495 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3497 * @bat_priv: the bat priv with all the soft interface information
3498 * @src: mac address of tt tvlv sender
3499 * @dst: mac address of tt tvlv recipient
3500 * @tvlv_value: tvlv buffer containing the tt data
3501 * @tvlv_value_len: tvlv buffer length
3503 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3506 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3507 uint8_t *src
, uint8_t *dst
,
3509 uint16_t tvlv_value_len
)
3511 struct batadv_tvlv_tt_data
*tt_data
;
3512 uint16_t tt_vlan_len
, tt_num_entries
;
3516 if (tvlv_value_len
< sizeof(*tt_data
))
3517 return NET_RX_SUCCESS
;
3519 tt_data
= (struct batadv_tvlv_tt_data
*)tvlv_value
;
3520 tvlv_value_len
-= sizeof(*tt_data
);
3522 tt_vlan_len
= sizeof(struct batadv_tvlv_tt_vlan_data
);
3523 tt_vlan_len
*= ntohs(tt_data
->num_vlan
);
3525 if (tvlv_value_len
< tt_vlan_len
)
3526 return NET_RX_SUCCESS
;
3528 tvlv_value_len
-= tt_vlan_len
;
3529 tt_num_entries
= batadv_tt_entries(tvlv_value_len
);
3531 switch (tt_data
->flags
& BATADV_TT_DATA_TYPE_MASK
) {
3532 case BATADV_TT_REQUEST
:
3533 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_RX
);
3535 /* If this node cannot provide a TT response the tt_request is
3538 ret
= batadv_send_tt_response(bat_priv
, tt_data
, src
, dst
);
3540 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3545 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3546 "Routing TT_REQUEST to %pM [%c]\n",
3548 /* tvlv API will re-route the packet */
3552 case BATADV_TT_RESPONSE
:
3553 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_RX
);
3555 if (batadv_is_my_mac(bat_priv
, dst
)) {
3556 batadv_handle_tt_response(bat_priv
, tt_data
,
3557 src
, tt_num_entries
);
3558 return NET_RX_SUCCESS
;
3561 if (tt_data
->flags
& BATADV_TT_FULL_TABLE
)
3566 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3567 "Routing TT_RESPONSE to %pM [%c]\n", dst
, tt_flag
);
3569 /* tvlv API will re-route the packet */
3573 return NET_RX_SUCCESS
;
3577 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3578 * @bat_priv: the bat priv with all the soft interface information
3579 * @src: mac address of tt tvlv sender
3580 * @dst: mac address of tt tvlv recipient
3581 * @tvlv_value: tvlv buffer containing the tt data
3582 * @tvlv_value_len: tvlv buffer length
3584 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3587 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv
*bat_priv
,
3588 uint8_t *src
, uint8_t *dst
,
3590 uint16_t tvlv_value_len
)
3592 struct batadv_tvlv_roam_adv
*roaming_adv
;
3593 struct batadv_orig_node
*orig_node
= NULL
;
3595 /* If this node is not the intended recipient of the
3596 * roaming advertisement the packet is forwarded
3597 * (the tvlv API will re-route the packet).
3599 if (!batadv_is_my_mac(bat_priv
, dst
))
3602 if (tvlv_value_len
< sizeof(*roaming_adv
))
3605 orig_node
= batadv_orig_hash_find(bat_priv
, src
);
3609 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_RX
);
3610 roaming_adv
= (struct batadv_tvlv_roam_adv
*)tvlv_value
;
3612 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
3613 "Received ROAMING_ADV from %pM (client %pM)\n",
3614 src
, roaming_adv
->client
);
3616 batadv_tt_global_add(bat_priv
, orig_node
, roaming_adv
->client
,
3617 ntohs(roaming_adv
->vid
), BATADV_TT_CLIENT_ROAM
,
3618 atomic_read(&orig_node
->last_ttvn
) + 1);
3622 batadv_orig_node_free_ref(orig_node
);
3623 return NET_RX_SUCCESS
;
3627 * batadv_tt_init - initialise the translation table internals
3628 * @bat_priv: the bat priv with all the soft interface information
3630 * Return 0 on success or negative error number in case of failure.
3632 int batadv_tt_init(struct batadv_priv
*bat_priv
)
3636 /* synchronized flags must be remote */
3637 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK
& BATADV_TT_REMOTE_MASK
));
3639 ret
= batadv_tt_local_init(bat_priv
);
3643 ret
= batadv_tt_global_init(bat_priv
);
3647 batadv_tvlv_handler_register(bat_priv
, batadv_tt_tvlv_ogm_handler_v1
,
3648 batadv_tt_tvlv_unicast_handler_v1
,
3649 BATADV_TVLV_TT
, 1, BATADV_NO_FLAGS
);
3651 batadv_tvlv_handler_register(bat_priv
, NULL
,
3652 batadv_roam_tvlv_unicast_handler_v1
,
3653 BATADV_TVLV_ROAM
, 1, BATADV_NO_FLAGS
);
3655 INIT_DELAYED_WORK(&bat_priv
->tt
.work
, batadv_tt_purge
);
3656 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
3657 msecs_to_jiffies(BATADV_TT_WORK_PERIOD
));
3663 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3664 * @bat_priv: the bat priv with all the soft interface information
3665 * @addr: the mac address of the client
3666 * @vid: the identifier of the VLAN where this client is connected
3668 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3671 bool batadv_tt_global_is_isolated(struct batadv_priv
*bat_priv
,
3672 const uint8_t *addr
, unsigned short vid
)
3674 struct batadv_tt_global_entry
*tt
;
3677 tt
= batadv_tt_global_hash_find(bat_priv
, addr
, vid
);
3681 ret
= tt
->common
.flags
& BATADV_TT_CLIENT_ISOLA
;
3683 batadv_tt_global_entry_free_ref(tt
);