1 /* Copyright (C) 2007-2012 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include "translation-table.h"
22 #include "soft-interface.h"
23 #include "hard-interface.h"
26 #include "originator.h"
28 #include "bridge_loop_avoidance.h"
30 #include <linux/crc16.h>
32 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
33 struct batadv_orig_node
*orig_node
);
34 static void batadv_tt_purge(struct work_struct
*work
);
36 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
);
38 /* returns 1 if they are the same mac addr */
39 static int batadv_compare_tt(const struct hlist_node
*node
, const void *data2
)
41 const void *data1
= container_of(node
, struct batadv_tt_common_entry
,
44 return (memcmp(data1
, data2
, ETH_ALEN
) == 0 ? 1 : 0);
47 static void batadv_tt_start_timer(struct batadv_priv
*bat_priv
)
49 INIT_DELAYED_WORK(&bat_priv
->tt_work
, batadv_tt_purge
);
50 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt_work
,
51 msecs_to_jiffies(5000));
54 static struct batadv_tt_common_entry
*
55 batadv_tt_hash_find(struct batadv_hashtable
*hash
, const void *data
)
57 struct hlist_head
*head
;
58 struct hlist_node
*node
;
59 struct batadv_tt_common_entry
*tt_common_entry
;
60 struct batadv_tt_common_entry
*tt_common_entry_tmp
= NULL
;
66 index
= batadv_choose_orig(data
, hash
->size
);
67 head
= &hash
->table
[index
];
70 hlist_for_each_entry_rcu(tt_common_entry
, node
, head
, hash_entry
) {
71 if (!batadv_compare_eth(tt_common_entry
, data
))
74 if (!atomic_inc_not_zero(&tt_common_entry
->refcount
))
77 tt_common_entry_tmp
= tt_common_entry
;
82 return tt_common_entry_tmp
;
85 static struct batadv_tt_local_entry
*
86 batadv_tt_local_hash_find(struct batadv_priv
*bat_priv
, const void *data
)
88 struct batadv_tt_common_entry
*tt_common_entry
;
89 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
91 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt_local_hash
, data
);
93 tt_local_entry
= container_of(tt_common_entry
,
94 struct batadv_tt_local_entry
,
96 return tt_local_entry
;
99 static struct batadv_tt_global_entry
*
100 batadv_tt_global_hash_find(struct batadv_priv
*bat_priv
, const void *data
)
102 struct batadv_tt_common_entry
*tt_common_entry
;
103 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
105 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt_global_hash
, data
);
107 tt_global_entry
= container_of(tt_common_entry
,
108 struct batadv_tt_global_entry
,
110 return tt_global_entry
;
115 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry
*tt_local_entry
)
117 if (atomic_dec_and_test(&tt_local_entry
->common
.refcount
))
118 kfree_rcu(tt_local_entry
, common
.rcu
);
121 static void batadv_tt_global_entry_free_rcu(struct rcu_head
*rcu
)
123 struct batadv_tt_common_entry
*tt_common_entry
;
124 struct batadv_tt_global_entry
*tt_global_entry
;
126 tt_common_entry
= container_of(rcu
, struct batadv_tt_common_entry
, rcu
);
127 tt_global_entry
= container_of(tt_common_entry
,
128 struct batadv_tt_global_entry
, common
);
130 kfree(tt_global_entry
);
134 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry
*tt_global_entry
)
136 if (atomic_dec_and_test(&tt_global_entry
->common
.refcount
)) {
137 batadv_tt_global_del_orig_list(tt_global_entry
);
138 call_rcu(&tt_global_entry
->common
.rcu
,
139 batadv_tt_global_entry_free_rcu
);
143 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head
*rcu
)
145 struct batadv_tt_orig_list_entry
*orig_entry
;
147 orig_entry
= container_of(rcu
, struct batadv_tt_orig_list_entry
, rcu
);
148 batadv_orig_node_free_ref(orig_entry
->orig_node
);
153 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry
*orig_entry
)
155 /* to avoid race conditions, immediately decrease the tt counter */
156 atomic_dec(&orig_entry
->orig_node
->tt_size
);
157 call_rcu(&orig_entry
->rcu
, batadv_tt_orig_list_entry_free_rcu
);
160 static void batadv_tt_local_event(struct batadv_priv
*bat_priv
,
161 const uint8_t *addr
, uint8_t flags
)
163 struct batadv_tt_change_node
*tt_change_node
, *entry
, *safe
;
164 bool event_removed
= false;
165 bool del_op_requested
, del_op_entry
;
167 tt_change_node
= kmalloc(sizeof(*tt_change_node
), GFP_ATOMIC
);
172 tt_change_node
->change
.flags
= flags
;
173 memcpy(tt_change_node
->change
.addr
, addr
, ETH_ALEN
);
175 del_op_requested
= flags
& BATADV_TT_CLIENT_DEL
;
177 /* check for ADD+DEL or DEL+ADD events */
178 spin_lock_bh(&bat_priv
->tt_changes_list_lock
);
179 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt_changes_list
,
181 if (!batadv_compare_eth(entry
->change
.addr
, addr
))
184 /* DEL+ADD in the same orig interval have no effect and can be
185 * removed to avoid silly behaviour on the receiver side. The
186 * other way around (ADD+DEL) can happen in case of roaming of
187 * a client still in the NEW state. Roaming of NEW clients is
188 * now possible due to automatically recognition of "temporary"
191 del_op_entry
= entry
->change
.flags
& BATADV_TT_CLIENT_DEL
;
192 if (!del_op_requested
&& del_op_entry
)
194 if (del_op_requested
&& !del_op_entry
)
198 list_del(&entry
->list
);
200 event_removed
= true;
204 /* track the change in the OGMinterval list */
205 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt_changes_list
);
208 spin_unlock_bh(&bat_priv
->tt_changes_list_lock
);
211 atomic_dec(&bat_priv
->tt_local_changes
);
213 atomic_inc(&bat_priv
->tt_local_changes
);
216 int batadv_tt_len(int changes_num
)
218 return changes_num
* sizeof(struct batadv_tt_change
);
221 static int batadv_tt_local_init(struct batadv_priv
*bat_priv
)
223 if (bat_priv
->tt_local_hash
)
226 bat_priv
->tt_local_hash
= batadv_hash_new(1024);
228 if (!bat_priv
->tt_local_hash
)
234 void batadv_tt_local_add(struct net_device
*soft_iface
, const uint8_t *addr
,
237 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
238 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
239 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
240 struct hlist_head
*head
;
241 struct hlist_node
*node
;
242 struct batadv_tt_orig_list_entry
*orig_entry
;
245 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
247 if (tt_local_entry
) {
248 tt_local_entry
->last_seen
= jiffies
;
249 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
250 tt_local_entry
->common
.flags
&= ~BATADV_TT_CLIENT_PENDING
;
254 tt_local_entry
= kmalloc(sizeof(*tt_local_entry
), GFP_ATOMIC
);
258 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
259 "Creating new local tt entry: %pM (ttvn: %d)\n", addr
,
260 (uint8_t)atomic_read(&bat_priv
->ttvn
));
262 memcpy(tt_local_entry
->common
.addr
, addr
, ETH_ALEN
);
263 tt_local_entry
->common
.flags
= BATADV_NO_FLAGS
;
264 if (batadv_is_wifi_iface(ifindex
))
265 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
266 atomic_set(&tt_local_entry
->common
.refcount
, 2);
267 tt_local_entry
->last_seen
= jiffies
;
269 /* the batman interface mac address should never be purged */
270 if (batadv_compare_eth(addr
, soft_iface
->dev_addr
))
271 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_NOPURGE
;
273 /* The local entry has to be marked as NEW to avoid to send it in
274 * a full table response going out before the next ttvn increment
275 * (consistency check)
277 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_NEW
;
279 hash_added
= batadv_hash_add(bat_priv
->tt_local_hash
, batadv_compare_tt
,
281 &tt_local_entry
->common
,
282 &tt_local_entry
->common
.hash_entry
);
284 if (unlikely(hash_added
!= 0)) {
285 /* remove the reference for the hash */
286 batadv_tt_local_entry_free_ref(tt_local_entry
);
290 batadv_tt_local_event(bat_priv
, addr
, tt_local_entry
->common
.flags
);
292 /* remove address from global hash if present */
293 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
295 /* Check whether it is a roaming! */
296 if (tt_global_entry
) {
297 /* These node are probably going to update their tt table */
298 head
= &tt_global_entry
->orig_list
;
300 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
301 orig_entry
->orig_node
->tt_poss_change
= true;
303 batadv_send_roam_adv(bat_priv
,
304 tt_global_entry
->common
.addr
,
305 orig_entry
->orig_node
);
308 /* The global entry has to be marked as ROAMING and
309 * has to be kept for consistency purpose
311 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
312 tt_global_entry
->roam_at
= jiffies
;
316 batadv_tt_local_entry_free_ref(tt_local_entry
);
318 batadv_tt_global_entry_free_ref(tt_global_entry
);
321 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff
,
322 int *packet_buff_len
,
326 unsigned char *new_buff
;
328 new_buff
= kmalloc(new_packet_len
, GFP_ATOMIC
);
330 /* keep old buffer if kmalloc should fail */
332 memcpy(new_buff
, *packet_buff
, min_packet_len
);
334 *packet_buff
= new_buff
;
335 *packet_buff_len
= new_packet_len
;
339 static void batadv_tt_prepare_packet_buff(struct batadv_priv
*bat_priv
,
340 unsigned char **packet_buff
,
341 int *packet_buff_len
,
344 struct batadv_hard_iface
*primary_if
;
347 primary_if
= batadv_primary_if_get_selected(bat_priv
);
349 req_len
= min_packet_len
;
350 req_len
+= batadv_tt_len(atomic_read(&bat_priv
->tt_local_changes
));
352 /* if we have too many changes for one packet don't send any
353 * and wait for the tt table request which will be fragmented
355 if ((!primary_if
) || (req_len
> primary_if
->soft_iface
->mtu
))
356 req_len
= min_packet_len
;
358 batadv_tt_realloc_packet_buff(packet_buff
, packet_buff_len
,
359 min_packet_len
, req_len
);
362 batadv_hardif_free_ref(primary_if
);
365 static int batadv_tt_changes_fill_buff(struct batadv_priv
*bat_priv
,
366 unsigned char **packet_buff
,
367 int *packet_buff_len
,
370 struct batadv_tt_change_node
*entry
, *safe
;
371 int count
= 0, tot_changes
= 0, new_len
;
372 unsigned char *tt_buff
;
374 batadv_tt_prepare_packet_buff(bat_priv
, packet_buff
,
375 packet_buff_len
, min_packet_len
);
377 new_len
= *packet_buff_len
- min_packet_len
;
378 tt_buff
= *packet_buff
+ min_packet_len
;
381 tot_changes
= new_len
/ batadv_tt_len(1);
383 spin_lock_bh(&bat_priv
->tt_changes_list_lock
);
384 atomic_set(&bat_priv
->tt_local_changes
, 0);
386 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt_changes_list
,
388 if (count
< tot_changes
) {
389 memcpy(tt_buff
+ batadv_tt_len(count
),
390 &entry
->change
, sizeof(struct batadv_tt_change
));
393 list_del(&entry
->list
);
396 spin_unlock_bh(&bat_priv
->tt_changes_list_lock
);
398 /* Keep the buffer for possible tt_request */
399 spin_lock_bh(&bat_priv
->tt_buff_lock
);
400 kfree(bat_priv
->tt_buff
);
401 bat_priv
->tt_buff_len
= 0;
402 bat_priv
->tt_buff
= NULL
;
403 /* check whether this new OGM has no changes due to size problems */
405 /* if kmalloc() fails we will reply with the full table
406 * instead of providing the diff
408 bat_priv
->tt_buff
= kmalloc(new_len
, GFP_ATOMIC
);
409 if (bat_priv
->tt_buff
) {
410 memcpy(bat_priv
->tt_buff
, tt_buff
, new_len
);
411 bat_priv
->tt_buff_len
= new_len
;
414 spin_unlock_bh(&bat_priv
->tt_buff_lock
);
419 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
421 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
422 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
423 struct batadv_hashtable
*hash
= bat_priv
->tt_local_hash
;
424 struct batadv_tt_common_entry
*tt_common_entry
;
425 struct batadv_hard_iface
*primary_if
;
426 struct hlist_node
*node
;
427 struct hlist_head
*head
;
431 primary_if
= batadv_primary_if_get_selected(bat_priv
);
433 ret
= seq_printf(seq
,
434 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
439 if (primary_if
->if_status
!= BATADV_IF_ACTIVE
) {
440 ret
= seq_printf(seq
,
441 "BATMAN mesh %s disabled - primary interface not active\n",
447 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
448 net_dev
->name
, (uint8_t)atomic_read(&bat_priv
->ttvn
));
450 for (i
= 0; i
< hash
->size
; i
++) {
451 head
= &hash
->table
[i
];
454 hlist_for_each_entry_rcu(tt_common_entry
, node
,
456 seq_printf(seq
, " * %pM [%c%c%c%c%c]\n",
457 tt_common_entry
->addr
,
458 (tt_common_entry
->flags
&
459 BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
460 (tt_common_entry
->flags
&
461 BATADV_TT_CLIENT_NOPURGE
? 'P' : '.'),
462 (tt_common_entry
->flags
&
463 BATADV_TT_CLIENT_NEW
? 'N' : '.'),
464 (tt_common_entry
->flags
&
465 BATADV_TT_CLIENT_PENDING
? 'X' : '.'),
466 (tt_common_entry
->flags
&
467 BATADV_TT_CLIENT_WIFI
? 'W' : '.'));
473 batadv_hardif_free_ref(primary_if
);
478 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
479 struct batadv_tt_local_entry
*tt_local_entry
,
480 uint16_t flags
, const char *message
)
482 batadv_tt_local_event(bat_priv
, tt_local_entry
->common
.addr
,
483 tt_local_entry
->common
.flags
| flags
);
485 /* The local client has to be marked as "pending to be removed" but has
486 * to be kept in the table in order to send it in a full table
487 * response issued before the net ttvn increment (consistency check)
489 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
491 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
492 "Local tt entry (%pM) pending to be removed: %s\n",
493 tt_local_entry
->common
.addr
, message
);
496 void batadv_tt_local_remove(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
497 const char *message
, bool roaming
)
499 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
502 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
506 flags
= BATADV_TT_CLIENT_DEL
;
508 flags
|= BATADV_TT_CLIENT_ROAM
;
510 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
, message
);
513 batadv_tt_local_entry_free_ref(tt_local_entry
);
516 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
517 struct hlist_head
*head
)
519 struct batadv_tt_local_entry
*tt_local_entry
;
520 struct batadv_tt_common_entry
*tt_common_entry
;
521 struct hlist_node
*node
, *node_tmp
;
523 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
, head
,
525 tt_local_entry
= container_of(tt_common_entry
,
526 struct batadv_tt_local_entry
,
528 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
531 /* entry already marked for deletion */
532 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
535 if (!batadv_has_timed_out(tt_local_entry
->last_seen
,
536 BATADV_TT_LOCAL_TIMEOUT
))
539 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
540 BATADV_TT_CLIENT_DEL
, "timed out");
544 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
)
546 struct batadv_hashtable
*hash
= bat_priv
->tt_local_hash
;
547 struct hlist_head
*head
;
548 spinlock_t
*list_lock
; /* protects write access to the hash lists */
551 for (i
= 0; i
< hash
->size
; i
++) {
552 head
= &hash
->table
[i
];
553 list_lock
= &hash
->list_locks
[i
];
555 spin_lock_bh(list_lock
);
556 batadv_tt_local_purge_list(bat_priv
, head
);
557 spin_unlock_bh(list_lock
);
562 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
564 struct batadv_hashtable
*hash
;
565 spinlock_t
*list_lock
; /* protects write access to the hash lists */
566 struct batadv_tt_common_entry
*tt_common_entry
;
567 struct batadv_tt_local_entry
*tt_local
;
568 struct hlist_node
*node
, *node_tmp
;
569 struct hlist_head
*head
;
572 if (!bat_priv
->tt_local_hash
)
575 hash
= bat_priv
->tt_local_hash
;
577 for (i
= 0; i
< hash
->size
; i
++) {
578 head
= &hash
->table
[i
];
579 list_lock
= &hash
->list_locks
[i
];
581 spin_lock_bh(list_lock
);
582 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
,
585 tt_local
= container_of(tt_common_entry
,
586 struct batadv_tt_local_entry
,
588 batadv_tt_local_entry_free_ref(tt_local
);
590 spin_unlock_bh(list_lock
);
593 batadv_hash_destroy(hash
);
595 bat_priv
->tt_local_hash
= NULL
;
598 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
600 if (bat_priv
->tt_global_hash
)
603 bat_priv
->tt_global_hash
= batadv_hash_new(1024);
605 if (!bat_priv
->tt_global_hash
)
611 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
613 struct batadv_tt_change_node
*entry
, *safe
;
615 spin_lock_bh(&bat_priv
->tt_changes_list_lock
);
617 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt_changes_list
,
619 list_del(&entry
->list
);
623 atomic_set(&bat_priv
->tt_local_changes
, 0);
624 spin_unlock_bh(&bat_priv
->tt_changes_list_lock
);
627 /* find out if an orig_node is already in the list of a tt_global_entry.
628 * returns 1 if found, 0 otherwise
631 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
632 const struct batadv_orig_node
*orig_node
)
634 struct batadv_tt_orig_list_entry
*tmp_orig_entry
;
635 const struct hlist_head
*head
;
636 struct hlist_node
*node
;
640 head
= &entry
->orig_list
;
641 hlist_for_each_entry_rcu(tmp_orig_entry
, node
, head
, list
) {
642 if (tmp_orig_entry
->orig_node
== orig_node
) {
652 batadv_tt_global_add_orig_entry(struct batadv_tt_global_entry
*tt_global_entry
,
653 struct batadv_orig_node
*orig_node
, int ttvn
)
655 struct batadv_tt_orig_list_entry
*orig_entry
;
657 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
661 INIT_HLIST_NODE(&orig_entry
->list
);
662 atomic_inc(&orig_node
->refcount
);
663 atomic_inc(&orig_node
->tt_size
);
664 orig_entry
->orig_node
= orig_node
;
665 orig_entry
->ttvn
= ttvn
;
667 spin_lock_bh(&tt_global_entry
->list_lock
);
668 hlist_add_head_rcu(&orig_entry
->list
,
669 &tt_global_entry
->orig_list
);
670 spin_unlock_bh(&tt_global_entry
->list_lock
);
673 /* caller must hold orig_node refcount */
674 int batadv_tt_global_add(struct batadv_priv
*bat_priv
,
675 struct batadv_orig_node
*orig_node
,
676 const unsigned char *tt_addr
, uint8_t flags
,
679 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
682 struct batadv_tt_common_entry
*common
;
684 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
);
686 if (!tt_global_entry
) {
687 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
688 if (!tt_global_entry
)
691 common
= &tt_global_entry
->common
;
692 memcpy(common
->addr
, tt_addr
, ETH_ALEN
);
694 common
->flags
= flags
;
695 tt_global_entry
->roam_at
= 0;
696 atomic_set(&common
->refcount
, 2);
698 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
699 spin_lock_init(&tt_global_entry
->list_lock
);
701 hash_added
= batadv_hash_add(bat_priv
->tt_global_hash
,
703 batadv_choose_orig
, common
,
704 &common
->hash_entry
);
706 if (unlikely(hash_added
!= 0)) {
707 /* remove the reference for the hash */
708 batadv_tt_global_entry_free_ref(tt_global_entry
);
712 batadv_tt_global_add_orig_entry(tt_global_entry
, orig_node
,
715 /* there is already a global entry, use this one. */
717 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
718 * one originator left in the list and we previously received a
719 * delete + roaming change for this originator.
721 * We should first delete the old originator before adding the
724 if (tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
725 batadv_tt_global_del_orig_list(tt_global_entry
);
726 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
727 tt_global_entry
->roam_at
= 0;
730 if (!batadv_tt_global_entry_has_orig(tt_global_entry
,
732 batadv_tt_global_add_orig_entry(tt_global_entry
,
736 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
737 "Creating new global tt entry: %pM (via %pM)\n",
738 tt_global_entry
->common
.addr
, orig_node
->orig
);
741 /* remove address from local hash if present */
742 batadv_tt_local_remove(bat_priv
, tt_global_entry
->common
.addr
,
743 "global tt received",
744 flags
& BATADV_TT_CLIENT_ROAM
);
748 batadv_tt_global_entry_free_ref(tt_global_entry
);
752 /* print all orig nodes who announce the address for this global entry.
753 * it is assumed that the caller holds rcu_read_lock();
756 batadv_tt_global_print_entry(struct batadv_tt_global_entry
*tt_global_entry
,
757 struct seq_file
*seq
)
759 struct hlist_head
*head
;
760 struct hlist_node
*node
;
761 struct batadv_tt_orig_list_entry
*orig_entry
;
762 struct batadv_tt_common_entry
*tt_common_entry
;
766 tt_common_entry
= &tt_global_entry
->common
;
768 head
= &tt_global_entry
->orig_list
;
770 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
771 flags
= tt_common_entry
->flags
;
772 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
773 seq_printf(seq
, " * %pM (%3u) via %pM (%3u) [%c%c]\n",
774 tt_global_entry
->common
.addr
, orig_entry
->ttvn
,
775 orig_entry
->orig_node
->orig
, last_ttvn
,
776 (flags
& BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
777 (flags
& BATADV_TT_CLIENT_WIFI
? 'W' : '.'));
781 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
783 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
784 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
785 struct batadv_hashtable
*hash
= bat_priv
->tt_global_hash
;
786 struct batadv_tt_common_entry
*tt_common_entry
;
787 struct batadv_tt_global_entry
*tt_global
;
788 struct batadv_hard_iface
*primary_if
;
789 struct hlist_node
*node
;
790 struct hlist_head
*head
;
794 primary_if
= batadv_primary_if_get_selected(bat_priv
);
796 ret
= seq_printf(seq
,
797 "BATMAN mesh %s disabled - please specify interfaces to enable it\n",
802 if (primary_if
->if_status
!= BATADV_IF_ACTIVE
) {
803 ret
= seq_printf(seq
,
804 "BATMAN mesh %s disabled - primary interface not active\n",
810 "Globally announced TT entries received via the mesh %s\n",
812 seq_printf(seq
, " %-13s %s %-15s %s %s\n",
813 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
815 for (i
= 0; i
< hash
->size
; i
++) {
816 head
= &hash
->table
[i
];
819 hlist_for_each_entry_rcu(tt_common_entry
, node
,
821 tt_global
= container_of(tt_common_entry
,
822 struct batadv_tt_global_entry
,
824 batadv_tt_global_print_entry(tt_global
, seq
);
830 batadv_hardif_free_ref(primary_if
);
834 /* deletes the orig list of a tt_global_entry */
836 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
838 struct hlist_head
*head
;
839 struct hlist_node
*node
, *safe
;
840 struct batadv_tt_orig_list_entry
*orig_entry
;
842 spin_lock_bh(&tt_global_entry
->list_lock
);
843 head
= &tt_global_entry
->orig_list
;
844 hlist_for_each_entry_safe(orig_entry
, node
, safe
, head
, list
) {
846 batadv_tt_orig_list_entry_free_ref(orig_entry
);
848 spin_unlock_bh(&tt_global_entry
->list_lock
);
853 batadv_tt_global_del_orig_entry(struct batadv_priv
*bat_priv
,
854 struct batadv_tt_global_entry
*tt_global_entry
,
855 struct batadv_orig_node
*orig_node
,
858 struct hlist_head
*head
;
859 struct hlist_node
*node
, *safe
;
860 struct batadv_tt_orig_list_entry
*orig_entry
;
862 spin_lock_bh(&tt_global_entry
->list_lock
);
863 head
= &tt_global_entry
->orig_list
;
864 hlist_for_each_entry_safe(orig_entry
, node
, safe
, head
, list
) {
865 if (orig_entry
->orig_node
== orig_node
) {
866 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
867 "Deleting %pM from global tt entry %pM: %s\n",
869 tt_global_entry
->common
.addr
, message
);
871 batadv_tt_orig_list_entry_free_ref(orig_entry
);
874 spin_unlock_bh(&tt_global_entry
->list_lock
);
878 batadv_tt_global_del_struct(struct batadv_priv
*bat_priv
,
879 struct batadv_tt_global_entry
*tt_global_entry
,
882 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
883 "Deleting global tt entry %pM: %s\n",
884 tt_global_entry
->common
.addr
, message
);
886 batadv_hash_remove(bat_priv
->tt_global_hash
, batadv_compare_tt
,
887 batadv_choose_orig
, tt_global_entry
->common
.addr
);
888 batadv_tt_global_entry_free_ref(tt_global_entry
);
892 /* If the client is to be deleted, we check if it is the last origantor entry
893 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
894 * timer, otherwise we simply remove the originator scheduled for deletion.
897 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
898 struct batadv_tt_global_entry
*tt_global_entry
,
899 struct batadv_orig_node
*orig_node
,
902 bool last_entry
= true;
903 struct hlist_head
*head
;
904 struct hlist_node
*node
;
905 struct batadv_tt_orig_list_entry
*orig_entry
;
907 /* no local entry exists, case 1:
908 * Check if this is the last one or if other entries exist.
912 head
= &tt_global_entry
->orig_list
;
913 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
914 if (orig_entry
->orig_node
!= orig_node
) {
922 /* its the last one, mark for roaming. */
923 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
924 tt_global_entry
->roam_at
= jiffies
;
926 /* there is another entry, we can simply delete this
927 * one and can still use the other one.
929 batadv_tt_global_del_orig_entry(bat_priv
, tt_global_entry
,
935 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
936 struct batadv_orig_node
*orig_node
,
937 const unsigned char *addr
,
938 const char *message
, bool roaming
)
940 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
941 struct batadv_tt_local_entry
*local_entry
= NULL
;
943 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
944 if (!tt_global_entry
)
948 batadv_tt_global_del_orig_entry(bat_priv
, tt_global_entry
,
951 if (hlist_empty(&tt_global_entry
->orig_list
))
952 batadv_tt_global_del_struct(bat_priv
, tt_global_entry
,
958 /* if we are deleting a global entry due to a roam
959 * event, there are two possibilities:
960 * 1) the client roamed from node A to node B => if there
961 * is only one originator left for this client, we mark
962 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
963 * wait for node B to claim it. In case of timeout
964 * the entry is purged.
966 * If there are other originators left, we directly delete
968 * 2) the client roamed to us => we can directly delete
969 * the global entry, since it is useless now.
971 local_entry
= batadv_tt_local_hash_find(bat_priv
,
972 tt_global_entry
->common
.addr
);
974 /* local entry exists, case 2: client roamed to us. */
975 batadv_tt_global_del_orig_list(tt_global_entry
);
976 batadv_tt_global_del_struct(bat_priv
, tt_global_entry
, message
);
978 /* no local entry exists, case 1: check for roaming */
979 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
985 batadv_tt_global_entry_free_ref(tt_global_entry
);
987 batadv_tt_local_entry_free_ref(local_entry
);
990 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
991 struct batadv_orig_node
*orig_node
,
994 struct batadv_tt_global_entry
*tt_global
;
995 struct batadv_tt_common_entry
*tt_common_entry
;
997 struct batadv_hashtable
*hash
= bat_priv
->tt_global_hash
;
998 struct hlist_node
*node
, *safe
;
999 struct hlist_head
*head
;
1000 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1005 for (i
= 0; i
< hash
->size
; i
++) {
1006 head
= &hash
->table
[i
];
1007 list_lock
= &hash
->list_locks
[i
];
1009 spin_lock_bh(list_lock
);
1010 hlist_for_each_entry_safe(tt_common_entry
, node
, safe
,
1012 tt_global
= container_of(tt_common_entry
,
1013 struct batadv_tt_global_entry
,
1016 batadv_tt_global_del_orig_entry(bat_priv
, tt_global
,
1017 orig_node
, message
);
1019 if (hlist_empty(&tt_global
->orig_list
)) {
1020 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1021 "Deleting global tt entry %pM: %s\n",
1022 tt_global
->common
.addr
, message
);
1023 hlist_del_rcu(node
);
1024 batadv_tt_global_entry_free_ref(tt_global
);
1027 spin_unlock_bh(list_lock
);
1029 orig_node
->tt_initialised
= false;
1032 static void batadv_tt_global_roam_purge_list(struct batadv_priv
*bat_priv
,
1033 struct hlist_head
*head
)
1035 struct batadv_tt_common_entry
*tt_common_entry
;
1036 struct batadv_tt_global_entry
*tt_global_entry
;
1037 struct hlist_node
*node
, *node_tmp
;
1039 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
, head
,
1041 tt_global_entry
= container_of(tt_common_entry
,
1042 struct batadv_tt_global_entry
,
1044 if (!(tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
1046 if (!batadv_has_timed_out(tt_global_entry
->roam_at
,
1047 BATADV_TT_CLIENT_ROAM_TIMEOUT
))
1050 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1051 "Deleting global tt entry (%pM): Roaming timeout\n",
1052 tt_global_entry
->common
.addr
);
1054 hlist_del_rcu(node
);
1055 batadv_tt_global_entry_free_ref(tt_global_entry
);
1059 static void batadv_tt_global_roam_purge(struct batadv_priv
*bat_priv
)
1061 struct batadv_hashtable
*hash
= bat_priv
->tt_global_hash
;
1062 struct hlist_head
*head
;
1063 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1066 for (i
= 0; i
< hash
->size
; i
++) {
1067 head
= &hash
->table
[i
];
1068 list_lock
= &hash
->list_locks
[i
];
1070 spin_lock_bh(list_lock
);
1071 batadv_tt_global_roam_purge_list(bat_priv
, head
);
1072 spin_unlock_bh(list_lock
);
1077 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
1079 struct batadv_hashtable
*hash
;
1080 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1081 struct batadv_tt_common_entry
*tt_common_entry
;
1082 struct batadv_tt_global_entry
*tt_global
;
1083 struct hlist_node
*node
, *node_tmp
;
1084 struct hlist_head
*head
;
1087 if (!bat_priv
->tt_global_hash
)
1090 hash
= bat_priv
->tt_global_hash
;
1092 for (i
= 0; i
< hash
->size
; i
++) {
1093 head
= &hash
->table
[i
];
1094 list_lock
= &hash
->list_locks
[i
];
1096 spin_lock_bh(list_lock
);
1097 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
,
1099 hlist_del_rcu(node
);
1100 tt_global
= container_of(tt_common_entry
,
1101 struct batadv_tt_global_entry
,
1103 batadv_tt_global_entry_free_ref(tt_global
);
1105 spin_unlock_bh(list_lock
);
1108 batadv_hash_destroy(hash
);
1110 bat_priv
->tt_global_hash
= NULL
;
1114 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
1115 struct batadv_tt_global_entry
*tt_global_entry
)
1119 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
1120 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
1126 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
1128 const uint8_t *addr
)
1130 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
1131 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
1132 struct batadv_orig_node
*orig_node
= NULL
;
1133 struct batadv_neigh_node
*router
= NULL
;
1134 struct hlist_head
*head
;
1135 struct hlist_node
*node
;
1136 struct batadv_tt_orig_list_entry
*orig_entry
;
1139 if (src
&& atomic_read(&bat_priv
->ap_isolation
)) {
1140 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
);
1141 if (!tt_local_entry
)
1145 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
1146 if (!tt_global_entry
)
1149 /* check whether the clients should not communicate due to AP
1152 if (tt_local_entry
&&
1153 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
1159 head
= &tt_global_entry
->orig_list
;
1160 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
1161 router
= batadv_orig_node_get_router(orig_entry
->orig_node
);
1165 if (router
->tq_avg
> best_tq
) {
1166 orig_node
= orig_entry
->orig_node
;
1167 best_tq
= router
->tq_avg
;
1169 batadv_neigh_node_free_ref(router
);
1171 /* found anything? */
1172 if (orig_node
&& !atomic_inc_not_zero(&orig_node
->refcount
))
1176 if (tt_global_entry
)
1177 batadv_tt_global_entry_free_ref(tt_global_entry
);
1179 batadv_tt_local_entry_free_ref(tt_local_entry
);
1184 /* Calculates the checksum of the local table of a given orig_node */
1185 static uint16_t batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
1186 struct batadv_orig_node
*orig_node
)
1188 uint16_t total
= 0, total_one
;
1189 struct batadv_hashtable
*hash
= bat_priv
->tt_global_hash
;
1190 struct batadv_tt_common_entry
*tt_common
;
1191 struct batadv_tt_global_entry
*tt_global
;
1192 struct hlist_node
*node
;
1193 struct hlist_head
*head
;
1197 for (i
= 0; i
< hash
->size
; i
++) {
1198 head
= &hash
->table
[i
];
1201 hlist_for_each_entry_rcu(tt_common
, node
, head
, hash_entry
) {
1202 tt_global
= container_of(tt_common
,
1203 struct batadv_tt_global_entry
,
1205 /* Roaming clients are in the global table for
1206 * consistency only. They don't have to be
1207 * taken into account while computing the
1210 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
1213 /* find out if this global entry is announced by this
1216 if (!batadv_tt_global_entry_has_orig(tt_global
,
1221 for (j
= 0; j
< ETH_ALEN
; j
++)
1222 total_one
= crc16_byte(total_one
,
1223 tt_common
->addr
[j
]);
1232 /* Calculates the checksum of the local table */
1233 static uint16_t batadv_tt_local_crc(struct batadv_priv
*bat_priv
)
1235 uint16_t total
= 0, total_one
;
1236 struct batadv_hashtable
*hash
= bat_priv
->tt_local_hash
;
1237 struct batadv_tt_common_entry
*tt_common
;
1238 struct hlist_node
*node
;
1239 struct hlist_head
*head
;
1243 for (i
= 0; i
< hash
->size
; i
++) {
1244 head
= &hash
->table
[i
];
1247 hlist_for_each_entry_rcu(tt_common
, node
, head
, hash_entry
) {
1248 /* not yet committed clients have not to be taken into
1249 * account while computing the CRC
1251 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
1254 for (j
= 0; j
< ETH_ALEN
; j
++)
1255 total_one
= crc16_byte(total_one
,
1256 tt_common
->addr
[j
]);
1265 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
1267 struct batadv_tt_req_node
*node
, *safe
;
1269 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
1271 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_req_list
, list
) {
1272 list_del(&node
->list
);
1276 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
1279 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
1280 struct batadv_orig_node
*orig_node
,
1281 const unsigned char *tt_buff
,
1282 uint8_t tt_num_changes
)
1284 uint16_t tt_buff_len
= batadv_tt_len(tt_num_changes
);
1286 /* Replace the old buffer only if I received something in the
1287 * last OGM (the OGM could carry no changes)
1289 spin_lock_bh(&orig_node
->tt_buff_lock
);
1290 if (tt_buff_len
> 0) {
1291 kfree(orig_node
->tt_buff
);
1292 orig_node
->tt_buff_len
= 0;
1293 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
1294 if (orig_node
->tt_buff
) {
1295 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
1296 orig_node
->tt_buff_len
= tt_buff_len
;
1299 spin_unlock_bh(&orig_node
->tt_buff_lock
);
1302 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
1304 struct batadv_tt_req_node
*node
, *safe
;
1306 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
1307 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_req_list
, list
) {
1308 if (batadv_has_timed_out(node
->issued_at
,
1309 BATADV_TT_REQUEST_TIMEOUT
)) {
1310 list_del(&node
->list
);
1314 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
1317 /* returns the pointer to the new tt_req_node struct if no request
1318 * has already been issued for this orig_node, NULL otherwise
1320 static struct batadv_tt_req_node
*
1321 batadv_new_tt_req_node(struct batadv_priv
*bat_priv
,
1322 struct batadv_orig_node
*orig_node
)
1324 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
1326 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
1327 list_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt_req_list
, list
) {
1328 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
1329 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
1330 BATADV_TT_REQUEST_TIMEOUT
))
1334 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
1338 memcpy(tt_req_node
->addr
, orig_node
->orig
, ETH_ALEN
);
1339 tt_req_node
->issued_at
= jiffies
;
1341 list_add(&tt_req_node
->list
, &bat_priv
->tt_req_list
);
1343 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
1347 /* data_ptr is useless here, but has to be kept to respect the prototype */
1348 static int batadv_tt_local_valid_entry(const void *entry_ptr
,
1349 const void *data_ptr
)
1351 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
1353 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
1358 static int batadv_tt_global_valid(const void *entry_ptr
,
1359 const void *data_ptr
)
1361 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
1362 const struct batadv_tt_global_entry
*tt_global_entry
;
1363 const struct batadv_orig_node
*orig_node
= data_ptr
;
1365 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
)
1368 tt_global_entry
= container_of(tt_common_entry
,
1369 struct batadv_tt_global_entry
,
1372 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
1375 static struct sk_buff
*
1376 batadv_tt_response_fill_table(uint16_t tt_len
, uint8_t ttvn
,
1377 struct batadv_hashtable
*hash
,
1378 struct batadv_hard_iface
*primary_if
,
1379 int (*valid_cb
)(const void *, const void *),
1382 struct batadv_tt_common_entry
*tt_common_entry
;
1383 struct batadv_tt_query_packet
*tt_response
;
1384 struct batadv_tt_change
*tt_change
;
1385 struct hlist_node
*node
;
1386 struct hlist_head
*head
;
1387 struct sk_buff
*skb
= NULL
;
1388 uint16_t tt_tot
, tt_count
;
1389 ssize_t tt_query_size
= sizeof(struct batadv_tt_query_packet
);
1393 if (tt_query_size
+ tt_len
> primary_if
->soft_iface
->mtu
) {
1394 tt_len
= primary_if
->soft_iface
->mtu
- tt_query_size
;
1395 tt_len
-= tt_len
% sizeof(struct batadv_tt_change
);
1397 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1399 len
= tt_query_size
+ tt_len
;
1400 skb
= dev_alloc_skb(len
+ ETH_HLEN
);
1404 skb_reserve(skb
, ETH_HLEN
);
1405 tt_response
= (struct batadv_tt_query_packet
*)skb_put(skb
, len
);
1406 tt_response
->ttvn
= ttvn
;
1408 tt_change
= (struct batadv_tt_change
*)(skb
->data
+ tt_query_size
);
1412 for (i
= 0; i
< hash
->size
; i
++) {
1413 head
= &hash
->table
[i
];
1415 hlist_for_each_entry_rcu(tt_common_entry
, node
,
1417 if (tt_count
== tt_tot
)
1420 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
1423 memcpy(tt_change
->addr
, tt_common_entry
->addr
,
1425 tt_change
->flags
= BATADV_NO_FLAGS
;
1433 /* store in the message the number of entries we have successfully
1436 tt_response
->tt_data
= htons(tt_count
);
1442 static int batadv_send_tt_request(struct batadv_priv
*bat_priv
,
1443 struct batadv_orig_node
*dst_orig_node
,
1444 uint8_t ttvn
, uint16_t tt_crc
,
1447 struct sk_buff
*skb
= NULL
;
1448 struct batadv_tt_query_packet
*tt_request
;
1449 struct batadv_neigh_node
*neigh_node
= NULL
;
1450 struct batadv_hard_iface
*primary_if
;
1451 struct batadv_tt_req_node
*tt_req_node
= NULL
;
1455 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1459 /* The new tt_req will be issued only if I'm not waiting for a
1460 * reply from the same orig_node yet
1462 tt_req_node
= batadv_new_tt_req_node(bat_priv
, dst_orig_node
);
1466 skb
= dev_alloc_skb(sizeof(*tt_request
) + ETH_HLEN
);
1470 skb_reserve(skb
, ETH_HLEN
);
1472 tt_req_len
= sizeof(*tt_request
);
1473 tt_request
= (struct batadv_tt_query_packet
*)skb_put(skb
, tt_req_len
);
1475 tt_request
->header
.packet_type
= BATADV_TT_QUERY
;
1476 tt_request
->header
.version
= BATADV_COMPAT_VERSION
;
1477 memcpy(tt_request
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1478 memcpy(tt_request
->dst
, dst_orig_node
->orig
, ETH_ALEN
);
1479 tt_request
->header
.ttl
= BATADV_TTL
;
1480 tt_request
->ttvn
= ttvn
;
1481 tt_request
->tt_data
= htons(tt_crc
);
1482 tt_request
->flags
= BATADV_TT_REQUEST
;
1485 tt_request
->flags
|= BATADV_TT_FULL_TABLE
;
1487 neigh_node
= batadv_orig_node_get_router(dst_orig_node
);
1491 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1492 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1493 dst_orig_node
->orig
, neigh_node
->addr
,
1494 (full_table
? 'F' : '.'));
1496 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
1498 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1503 batadv_neigh_node_free_ref(neigh_node
);
1505 batadv_hardif_free_ref(primary_if
);
1508 if (ret
&& tt_req_node
) {
1509 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
1510 list_del(&tt_req_node
->list
);
1511 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
1518 batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
1519 struct batadv_tt_query_packet
*tt_request
)
1521 struct batadv_orig_node
*req_dst_orig_node
= NULL
;
1522 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
1523 struct batadv_neigh_node
*neigh_node
= NULL
;
1524 struct batadv_hard_iface
*primary_if
= NULL
;
1525 uint8_t orig_ttvn
, req_ttvn
, ttvn
;
1527 unsigned char *tt_buff
;
1529 uint16_t tt_len
, tt_tot
;
1530 struct sk_buff
*skb
= NULL
;
1531 struct batadv_tt_query_packet
*tt_response
;
1534 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1535 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1536 tt_request
->src
, tt_request
->ttvn
, tt_request
->dst
,
1537 (tt_request
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1539 /* Let's get the orig node of the REAL destination */
1540 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->dst
);
1541 if (!req_dst_orig_node
)
1544 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->src
);
1545 if (!res_dst_orig_node
)
1548 neigh_node
= batadv_orig_node_get_router(res_dst_orig_node
);
1552 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1556 orig_ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1557 req_ttvn
= tt_request
->ttvn
;
1559 /* I don't have the requested data */
1560 if (orig_ttvn
!= req_ttvn
||
1561 tt_request
->tt_data
!= htons(req_dst_orig_node
->tt_crc
))
1564 /* If the full table has been explicitly requested */
1565 if (tt_request
->flags
& BATADV_TT_FULL_TABLE
||
1566 !req_dst_orig_node
->tt_buff
)
1571 /* In this version, fragmentation is not implemented, then
1572 * I'll send only one packet with as much TT entries as I can
1575 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
1576 tt_len
= req_dst_orig_node
->tt_buff_len
;
1577 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1579 len
= sizeof(*tt_response
) + tt_len
;
1580 skb
= dev_alloc_skb(len
+ ETH_HLEN
);
1584 skb_reserve(skb
, ETH_HLEN
);
1585 tt_response
= (struct batadv_tt_query_packet
*)skb_put(skb
,
1587 tt_response
->ttvn
= req_ttvn
;
1588 tt_response
->tt_data
= htons(tt_tot
);
1590 tt_buff
= skb
->data
+ sizeof(*tt_response
);
1591 /* Copy the last orig_node's OGM buffer */
1592 memcpy(tt_buff
, req_dst_orig_node
->tt_buff
,
1593 req_dst_orig_node
->tt_buff_len
);
1595 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1597 tt_len
= (uint16_t)atomic_read(&req_dst_orig_node
->tt_size
);
1598 tt_len
*= sizeof(struct batadv_tt_change
);
1599 ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1601 skb
= batadv_tt_response_fill_table(tt_len
, ttvn
,
1602 bat_priv
->tt_global_hash
,
1604 batadv_tt_global_valid
,
1609 tt_response
= (struct batadv_tt_query_packet
*)skb
->data
;
1612 tt_response
->header
.packet_type
= BATADV_TT_QUERY
;
1613 tt_response
->header
.version
= BATADV_COMPAT_VERSION
;
1614 tt_response
->header
.ttl
= BATADV_TTL
;
1615 memcpy(tt_response
->src
, req_dst_orig_node
->orig
, ETH_ALEN
);
1616 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1617 tt_response
->flags
= BATADV_TT_RESPONSE
;
1620 tt_response
->flags
|= BATADV_TT_FULL_TABLE
;
1622 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1623 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1624 res_dst_orig_node
->orig
, neigh_node
->addr
,
1625 req_dst_orig_node
->orig
, req_ttvn
);
1627 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
1629 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1634 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1637 if (res_dst_orig_node
)
1638 batadv_orig_node_free_ref(res_dst_orig_node
);
1639 if (req_dst_orig_node
)
1640 batadv_orig_node_free_ref(req_dst_orig_node
);
1642 batadv_neigh_node_free_ref(neigh_node
);
1644 batadv_hardif_free_ref(primary_if
);
1652 batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
1653 struct batadv_tt_query_packet
*tt_request
)
1655 struct batadv_orig_node
*orig_node
= NULL
;
1656 struct batadv_neigh_node
*neigh_node
= NULL
;
1657 struct batadv_hard_iface
*primary_if
= NULL
;
1658 uint8_t my_ttvn
, req_ttvn
, ttvn
;
1660 unsigned char *tt_buff
;
1662 uint16_t tt_len
, tt_tot
;
1663 struct sk_buff
*skb
= NULL
;
1664 struct batadv_tt_query_packet
*tt_response
;
1667 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1668 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1669 tt_request
->src
, tt_request
->ttvn
,
1670 (tt_request
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1673 my_ttvn
= (uint8_t)atomic_read(&bat_priv
->ttvn
);
1674 req_ttvn
= tt_request
->ttvn
;
1676 orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->src
);
1680 neigh_node
= batadv_orig_node_get_router(orig_node
);
1684 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1688 /* If the full table has been explicitly requested or the gap
1689 * is too big send the whole local translation table
1691 if (tt_request
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
1697 /* In this version, fragmentation is not implemented, then
1698 * I'll send only one packet with as much TT entries as I can
1701 spin_lock_bh(&bat_priv
->tt_buff_lock
);
1702 tt_len
= bat_priv
->tt_buff_len
;
1703 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1705 len
= sizeof(*tt_response
) + tt_len
;
1706 skb
= dev_alloc_skb(len
+ ETH_HLEN
);
1710 skb_reserve(skb
, ETH_HLEN
);
1711 tt_response
= (struct batadv_tt_query_packet
*)skb_put(skb
,
1713 tt_response
->ttvn
= req_ttvn
;
1714 tt_response
->tt_data
= htons(tt_tot
);
1716 tt_buff
= skb
->data
+ sizeof(*tt_response
);
1717 memcpy(tt_buff
, bat_priv
->tt_buff
,
1718 bat_priv
->tt_buff_len
);
1719 spin_unlock_bh(&bat_priv
->tt_buff_lock
);
1721 tt_len
= (uint16_t)atomic_read(&bat_priv
->num_local_tt
);
1722 tt_len
*= sizeof(struct batadv_tt_change
);
1723 ttvn
= (uint8_t)atomic_read(&bat_priv
->ttvn
);
1725 skb
= batadv_tt_response_fill_table(tt_len
, ttvn
,
1726 bat_priv
->tt_local_hash
,
1728 batadv_tt_local_valid_entry
,
1733 tt_response
= (struct batadv_tt_query_packet
*)skb
->data
;
1736 tt_response
->header
.packet_type
= BATADV_TT_QUERY
;
1737 tt_response
->header
.version
= BATADV_COMPAT_VERSION
;
1738 tt_response
->header
.ttl
= BATADV_TTL
;
1739 memcpy(tt_response
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1740 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1741 tt_response
->flags
= BATADV_TT_RESPONSE
;
1744 tt_response
->flags
|= BATADV_TT_FULL_TABLE
;
1746 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1747 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1748 orig_node
->orig
, neigh_node
->addr
,
1749 (tt_response
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1751 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
1753 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1758 spin_unlock_bh(&bat_priv
->tt_buff_lock
);
1761 batadv_orig_node_free_ref(orig_node
);
1763 batadv_neigh_node_free_ref(neigh_node
);
1765 batadv_hardif_free_ref(primary_if
);
1768 /* This packet was for me, so it doesn't need to be re-routed */
1772 bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
1773 struct batadv_tt_query_packet
*tt_request
)
1775 if (batadv_is_my_mac(tt_request
->dst
)) {
1776 /* don't answer backbone gws! */
1777 if (batadv_bla_is_backbone_gw_orig(bat_priv
, tt_request
->src
))
1780 return batadv_send_my_tt_response(bat_priv
, tt_request
);
1782 return batadv_send_other_tt_response(bat_priv
, tt_request
);
1786 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
1787 struct batadv_orig_node
*orig_node
,
1788 struct batadv_tt_change
*tt_change
,
1789 uint16_t tt_num_changes
, uint8_t ttvn
)
1794 for (i
= 0; i
< tt_num_changes
; i
++) {
1795 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
1796 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
1797 batadv_tt_global_del(bat_priv
, orig_node
,
1798 (tt_change
+ i
)->addr
,
1799 "tt removed by changes",
1802 if (!batadv_tt_global_add(bat_priv
, orig_node
,
1803 (tt_change
+ i
)->addr
,
1804 (tt_change
+ i
)->flags
, ttvn
))
1805 /* In case of problem while storing a
1806 * global_entry, we stop the updating
1807 * procedure without committing the
1808 * ttvn change. This will avoid to send
1809 * corrupted data on tt_request
1814 orig_node
->tt_initialised
= true;
1817 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
1818 struct batadv_tt_query_packet
*tt_response
)
1820 struct batadv_orig_node
*orig_node
= NULL
;
1822 orig_node
= batadv_orig_hash_find(bat_priv
, tt_response
->src
);
1826 /* Purge the old table first.. */
1827 batadv_tt_global_del_orig(bat_priv
, orig_node
, "Received full table");
1829 _batadv_tt_update_changes(bat_priv
, orig_node
,
1830 (struct batadv_tt_change
*)(tt_response
+ 1),
1831 ntohs(tt_response
->tt_data
),
1834 spin_lock_bh(&orig_node
->tt_buff_lock
);
1835 kfree(orig_node
->tt_buff
);
1836 orig_node
->tt_buff_len
= 0;
1837 orig_node
->tt_buff
= NULL
;
1838 spin_unlock_bh(&orig_node
->tt_buff_lock
);
1840 atomic_set(&orig_node
->last_ttvn
, tt_response
->ttvn
);
1844 batadv_orig_node_free_ref(orig_node
);
1847 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
1848 struct batadv_orig_node
*orig_node
,
1849 uint16_t tt_num_changes
, uint8_t ttvn
,
1850 struct batadv_tt_change
*tt_change
)
1852 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
1853 tt_num_changes
, ttvn
);
1855 batadv_tt_save_orig_buffer(bat_priv
, orig_node
,
1856 (unsigned char *)tt_change
, tt_num_changes
);
1857 atomic_set(&orig_node
->last_ttvn
, ttvn
);
1860 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const uint8_t *addr
)
1862 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
1865 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
1866 if (!tt_local_entry
)
1868 /* Check if the client has been logically deleted (but is kept for
1869 * consistency purpose)
1871 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1876 batadv_tt_local_entry_free_ref(tt_local_entry
);
1880 void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
1881 struct batadv_tt_query_packet
*tt_response
)
1883 struct batadv_tt_req_node
*node
, *safe
;
1884 struct batadv_orig_node
*orig_node
= NULL
;
1885 struct batadv_tt_change
*tt_change
;
1887 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1888 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1889 tt_response
->src
, tt_response
->ttvn
,
1890 ntohs(tt_response
->tt_data
),
1891 (tt_response
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1893 /* we should have never asked a backbone gw */
1894 if (batadv_bla_is_backbone_gw_orig(bat_priv
, tt_response
->src
))
1897 orig_node
= batadv_orig_hash_find(bat_priv
, tt_response
->src
);
1901 if (tt_response
->flags
& BATADV_TT_FULL_TABLE
) {
1902 batadv_tt_fill_gtable(bat_priv
, tt_response
);
1904 tt_change
= (struct batadv_tt_change
*)(tt_response
+ 1);
1905 batadv_tt_update_changes(bat_priv
, orig_node
,
1906 ntohs(tt_response
->tt_data
),
1907 tt_response
->ttvn
, tt_change
);
1910 /* Delete the tt_req_node from pending tt_requests list */
1911 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
1912 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_req_list
, list
) {
1913 if (!batadv_compare_eth(node
->addr
, tt_response
->src
))
1915 list_del(&node
->list
);
1918 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
1920 /* Recalculate the CRC for this orig_node and store it */
1921 orig_node
->tt_crc
= batadv_tt_global_crc(bat_priv
, orig_node
);
1922 /* Roaming phase is over: tables are in sync again. I can
1925 orig_node
->tt_poss_change
= false;
1928 batadv_orig_node_free_ref(orig_node
);
1931 int batadv_tt_init(struct batadv_priv
*bat_priv
)
1935 ret
= batadv_tt_local_init(bat_priv
);
1939 ret
= batadv_tt_global_init(bat_priv
);
1943 batadv_tt_start_timer(bat_priv
);
1948 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
1950 struct batadv_tt_roam_node
*node
, *safe
;
1952 spin_lock_bh(&bat_priv
->tt_roam_list_lock
);
1954 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_roam_list
, list
) {
1955 list_del(&node
->list
);
1959 spin_unlock_bh(&bat_priv
->tt_roam_list_lock
);
1962 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
1964 struct batadv_tt_roam_node
*node
, *safe
;
1966 spin_lock_bh(&bat_priv
->tt_roam_list_lock
);
1967 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_roam_list
, list
) {
1968 if (!batadv_has_timed_out(node
->first_time
,
1969 BATADV_ROAMING_MAX_TIME
))
1972 list_del(&node
->list
);
1975 spin_unlock_bh(&bat_priv
->tt_roam_list_lock
);
1978 /* This function checks whether the client already reached the
1979 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1982 * returns true if the ROAMING_ADV can be sent, false otherwise
1984 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
,
1987 struct batadv_tt_roam_node
*tt_roam_node
;
1990 spin_lock_bh(&bat_priv
->tt_roam_list_lock
);
1991 /* The new tt_req will be issued only if I'm not waiting for a
1992 * reply from the same orig_node yet
1994 list_for_each_entry(tt_roam_node
, &bat_priv
->tt_roam_list
, list
) {
1995 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
1998 if (batadv_has_timed_out(tt_roam_node
->first_time
,
1999 BATADV_ROAMING_MAX_TIME
))
2002 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
2003 /* Sorry, you roamed too many times! */
2010 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
2014 tt_roam_node
->first_time
= jiffies
;
2015 atomic_set(&tt_roam_node
->counter
,
2016 BATADV_ROAMING_MAX_COUNT
- 1);
2017 memcpy(tt_roam_node
->addr
, client
, ETH_ALEN
);
2019 list_add(&tt_roam_node
->list
, &bat_priv
->tt_roam_list
);
2024 spin_unlock_bh(&bat_priv
->tt_roam_list_lock
);
2028 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
2029 struct batadv_orig_node
*orig_node
)
2031 struct batadv_neigh_node
*neigh_node
= NULL
;
2032 struct sk_buff
*skb
= NULL
;
2033 struct batadv_roam_adv_packet
*roam_adv_packet
;
2035 struct batadv_hard_iface
*primary_if
;
2036 size_t len
= sizeof(*roam_adv_packet
);
2038 /* before going on we have to check whether the client has
2039 * already roamed to us too many times
2041 if (!batadv_tt_check_roam_count(bat_priv
, client
))
2044 skb
= dev_alloc_skb(sizeof(*roam_adv_packet
) + ETH_HLEN
);
2048 skb_reserve(skb
, ETH_HLEN
);
2050 roam_adv_packet
= (struct batadv_roam_adv_packet
*)skb_put(skb
, len
);
2052 roam_adv_packet
->header
.packet_type
= BATADV_ROAM_ADV
;
2053 roam_adv_packet
->header
.version
= BATADV_COMPAT_VERSION
;
2054 roam_adv_packet
->header
.ttl
= BATADV_TTL
;
2055 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2058 memcpy(roam_adv_packet
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
2059 batadv_hardif_free_ref(primary_if
);
2060 memcpy(roam_adv_packet
->dst
, orig_node
->orig
, ETH_ALEN
);
2061 memcpy(roam_adv_packet
->client
, client
, ETH_ALEN
);
2063 neigh_node
= batadv_orig_node_get_router(orig_node
);
2067 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2068 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2069 orig_node
->orig
, client
, neigh_node
->addr
);
2071 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
2073 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
2078 batadv_neigh_node_free_ref(neigh_node
);
2084 static void batadv_tt_purge(struct work_struct
*work
)
2086 struct delayed_work
*delayed_work
;
2087 struct batadv_priv
*bat_priv
;
2089 delayed_work
= container_of(work
, struct delayed_work
, work
);
2090 bat_priv
= container_of(delayed_work
, struct batadv_priv
, tt_work
);
2092 batadv_tt_local_purge(bat_priv
);
2093 batadv_tt_global_roam_purge(bat_priv
);
2094 batadv_tt_req_purge(bat_priv
);
2095 batadv_tt_roam_purge(bat_priv
);
2097 batadv_tt_start_timer(bat_priv
);
2100 void batadv_tt_free(struct batadv_priv
*bat_priv
)
2102 cancel_delayed_work_sync(&bat_priv
->tt_work
);
2104 batadv_tt_local_table_free(bat_priv
);
2105 batadv_tt_global_table_free(bat_priv
);
2106 batadv_tt_req_list_free(bat_priv
);
2107 batadv_tt_changes_list_free(bat_priv
);
2108 batadv_tt_roam_list_free(bat_priv
);
2110 kfree(bat_priv
->tt_buff
);
2113 /* This function will enable or disable the specified flags for all the entries
2114 * in the given hash table and returns the number of modified entries
2116 static uint16_t batadv_tt_set_flags(struct batadv_hashtable
*hash
,
2117 uint16_t flags
, bool enable
)
2120 uint16_t changed_num
= 0;
2121 struct hlist_head
*head
;
2122 struct hlist_node
*node
;
2123 struct batadv_tt_common_entry
*tt_common_entry
;
2128 for (i
= 0; i
< hash
->size
; i
++) {
2129 head
= &hash
->table
[i
];
2132 hlist_for_each_entry_rcu(tt_common_entry
, node
,
2135 if ((tt_common_entry
->flags
& flags
) == flags
)
2137 tt_common_entry
->flags
|= flags
;
2139 if (!(tt_common_entry
->flags
& flags
))
2141 tt_common_entry
->flags
&= ~flags
;
2151 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2152 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
2154 struct batadv_hashtable
*hash
= bat_priv
->tt_local_hash
;
2155 struct batadv_tt_common_entry
*tt_common
;
2156 struct batadv_tt_local_entry
*tt_local
;
2157 struct hlist_node
*node
, *node_tmp
;
2158 struct hlist_head
*head
;
2159 spinlock_t
*list_lock
; /* protects write access to the hash lists */
2165 for (i
= 0; i
< hash
->size
; i
++) {
2166 head
= &hash
->table
[i
];
2167 list_lock
= &hash
->list_locks
[i
];
2169 spin_lock_bh(list_lock
);
2170 hlist_for_each_entry_safe(tt_common
, node
, node_tmp
, head
,
2172 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
2175 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2176 "Deleting local tt entry (%pM): pending\n",
2179 atomic_dec(&bat_priv
->num_local_tt
);
2180 hlist_del_rcu(node
);
2181 tt_local
= container_of(tt_common
,
2182 struct batadv_tt_local_entry
,
2184 batadv_tt_local_entry_free_ref(tt_local
);
2186 spin_unlock_bh(list_lock
);
2191 static int batadv_tt_commit_changes(struct batadv_priv
*bat_priv
,
2192 unsigned char **packet_buff
,
2193 int *packet_buff_len
, int packet_min_len
)
2195 uint16_t changed_num
= 0;
2197 if (atomic_read(&bat_priv
->tt_local_changes
) < 1)
2200 changed_num
= batadv_tt_set_flags(bat_priv
->tt_local_hash
,
2201 BATADV_TT_CLIENT_NEW
, false);
2203 /* all reset entries have to be counted as local entries */
2204 atomic_add(changed_num
, &bat_priv
->num_local_tt
);
2205 batadv_tt_local_purge_pending_clients(bat_priv
);
2206 bat_priv
->tt_crc
= batadv_tt_local_crc(bat_priv
);
2208 /* Increment the TTVN only once per OGM interval */
2209 atomic_inc(&bat_priv
->ttvn
);
2210 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2211 "Local changes committed, updating to ttvn %u\n",
2212 (uint8_t)atomic_read(&bat_priv
->ttvn
));
2213 bat_priv
->tt_poss_change
= false;
2215 /* reset the sending counter */
2216 atomic_set(&bat_priv
->tt_ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
2218 return batadv_tt_changes_fill_buff(bat_priv
, packet_buff
,
2219 packet_buff_len
, packet_min_len
);
2222 /* when calling this function (hard_iface == primary_if) has to be true */
2223 int batadv_tt_append_diff(struct batadv_priv
*bat_priv
,
2224 unsigned char **packet_buff
, int *packet_buff_len
,
2229 /* if at least one change happened */
2230 tt_num_changes
= batadv_tt_commit_changes(bat_priv
, packet_buff
,
2234 /* if the changes have been sent often enough */
2235 if ((tt_num_changes
< 0) &&
2236 (!batadv_atomic_dec_not_zero(&bat_priv
->tt_ogm_append_cnt
))) {
2237 batadv_tt_realloc_packet_buff(packet_buff
, packet_buff_len
,
2238 packet_min_len
, packet_min_len
);
2242 return tt_num_changes
;
2245 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, uint8_t *src
,
2248 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2249 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2252 if (!atomic_read(&bat_priv
->ap_isolation
))
2255 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
);
2256 if (!tt_local_entry
)
2259 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
);
2260 if (!tt_global_entry
)
2263 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2269 if (tt_global_entry
)
2270 batadv_tt_global_entry_free_ref(tt_global_entry
);
2272 batadv_tt_local_entry_free_ref(tt_local_entry
);
2276 void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
2277 struct batadv_orig_node
*orig_node
,
2278 const unsigned char *tt_buff
, uint8_t tt_num_changes
,
2279 uint8_t ttvn
, uint16_t tt_crc
)
2281 uint8_t orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
2282 bool full_table
= true;
2283 struct batadv_tt_change
*tt_change
;
2285 /* don't care about a backbone gateways updates. */
2286 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
))
2289 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2290 * increased by one -> we can apply the attached changes
2292 if ((!orig_node
->tt_initialised
&& ttvn
== 1) ||
2293 ttvn
- orig_ttvn
== 1) {
2294 /* the OGM could not contain the changes due to their size or
2295 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2297 * In this case send a tt request
2299 if (!tt_num_changes
) {
2304 tt_change
= (struct batadv_tt_change
*)tt_buff
;
2305 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
2308 /* Even if we received the precomputed crc with the OGM, we
2309 * prefer to recompute it to spot any possible inconsistency
2310 * in the global table
2312 orig_node
->tt_crc
= batadv_tt_global_crc(bat_priv
, orig_node
);
2314 /* The ttvn alone is not enough to guarantee consistency
2315 * because a single value could represent different states
2316 * (due to the wrap around). Thus a node has to check whether
2317 * the resulting table (after applying the changes) is still
2318 * consistent or not. E.g. a node could disconnect while its
2319 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2320 * checking the CRC value is mandatory to detect the
2323 if (orig_node
->tt_crc
!= tt_crc
)
2326 /* Roaming phase is over: tables are in sync again. I can
2329 orig_node
->tt_poss_change
= false;
2331 /* if we missed more than one change or our tables are not
2332 * in sync anymore -> request fresh tt data
2334 if (!orig_node
->tt_initialised
|| ttvn
!= orig_ttvn
||
2335 orig_node
->tt_crc
!= tt_crc
) {
2337 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2338 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2339 orig_node
->orig
, ttvn
, orig_ttvn
, tt_crc
,
2340 orig_node
->tt_crc
, tt_num_changes
);
2341 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
2342 tt_crc
, full_table
);
2348 /* returns true whether we know that the client has moved from its old
2349 * originator to another one. This entry is kept is still kept for consistency
2352 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
2355 struct batadv_tt_global_entry
*tt_global_entry
;
2358 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
2359 if (!tt_global_entry
)
2362 ret
= tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
2363 batadv_tt_global_entry_free_ref(tt_global_entry
);