2 * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include "translation-table.h"
24 #include "soft-interface.h"
25 #include "hard-interface.h"
28 #include "originator.h"
31 #include <linux/crc16.h>
33 static void _tt_global_del(struct bat_priv
*bat_priv
,
34 struct tt_global_entry
*tt_global_entry
,
36 static void tt_purge(struct work_struct
*work
);
38 /* returns 1 if they are the same mac addr */
39 static int compare_ltt(const struct hlist_node
*node
, const void *data2
)
41 const void *data1
= container_of(node
, struct tt_local_entry
,
44 return (memcmp(data1
, data2
, ETH_ALEN
) == 0 ? 1 : 0);
47 /* returns 1 if they are the same mac addr */
48 static int compare_gtt(const struct hlist_node
*node
, const void *data2
)
50 const void *data1
= container_of(node
, struct tt_global_entry
,
53 return (memcmp(data1
, data2
, ETH_ALEN
) == 0 ? 1 : 0);
56 static void tt_start_timer(struct bat_priv
*bat_priv
)
58 INIT_DELAYED_WORK(&bat_priv
->tt_work
, tt_purge
);
59 queue_delayed_work(bat_event_workqueue
, &bat_priv
->tt_work
,
60 msecs_to_jiffies(5000));
63 static struct tt_local_entry
*tt_local_hash_find(struct bat_priv
*bat_priv
,
66 struct hashtable_t
*hash
= bat_priv
->tt_local_hash
;
67 struct hlist_head
*head
;
68 struct hlist_node
*node
;
69 struct tt_local_entry
*tt_local_entry
, *tt_local_entry_tmp
= NULL
;
75 index
= choose_orig(data
, hash
->size
);
76 head
= &hash
->table
[index
];
79 hlist_for_each_entry_rcu(tt_local_entry
, node
, head
, hash_entry
) {
80 if (!compare_eth(tt_local_entry
, data
))
83 if (!atomic_inc_not_zero(&tt_local_entry
->refcount
))
86 tt_local_entry_tmp
= tt_local_entry
;
91 return tt_local_entry_tmp
;
94 static struct tt_global_entry
*tt_global_hash_find(struct bat_priv
*bat_priv
,
97 struct hashtable_t
*hash
= bat_priv
->tt_global_hash
;
98 struct hlist_head
*head
;
99 struct hlist_node
*node
;
100 struct tt_global_entry
*tt_global_entry
;
101 struct tt_global_entry
*tt_global_entry_tmp
= NULL
;
107 index
= choose_orig(data
, hash
->size
);
108 head
= &hash
->table
[index
];
111 hlist_for_each_entry_rcu(tt_global_entry
, node
, head
, hash_entry
) {
112 if (!compare_eth(tt_global_entry
, data
))
115 if (!atomic_inc_not_zero(&tt_global_entry
->refcount
))
118 tt_global_entry_tmp
= tt_global_entry
;
123 return tt_global_entry_tmp
;
126 static bool is_out_of_time(unsigned long starting_time
, unsigned long timeout
)
128 unsigned long deadline
;
129 deadline
= starting_time
+ msecs_to_jiffies(timeout
);
131 return time_after(jiffies
, deadline
);
134 static void tt_local_entry_free_ref(struct tt_local_entry
*tt_local_entry
)
136 if (atomic_dec_and_test(&tt_local_entry
->refcount
))
137 kfree_rcu(tt_local_entry
, rcu
);
140 static void tt_global_entry_free_rcu(struct rcu_head
*rcu
)
142 struct tt_global_entry
*tt_global_entry
;
144 tt_global_entry
= container_of(rcu
, struct tt_global_entry
, rcu
);
146 if (tt_global_entry
->orig_node
)
147 orig_node_free_ref(tt_global_entry
->orig_node
);
149 kfree(tt_global_entry
);
152 static void tt_global_entry_free_ref(struct tt_global_entry
*tt_global_entry
)
154 if (atomic_dec_and_test(&tt_global_entry
->refcount
))
155 call_rcu(&tt_global_entry
->rcu
, tt_global_entry_free_rcu
);
158 static void tt_local_event(struct bat_priv
*bat_priv
, const uint8_t *addr
,
161 struct tt_change_node
*tt_change_node
;
163 tt_change_node
= kmalloc(sizeof(*tt_change_node
), GFP_ATOMIC
);
168 tt_change_node
->change
.flags
= flags
;
169 memcpy(tt_change_node
->change
.addr
, addr
, ETH_ALEN
);
171 spin_lock_bh(&bat_priv
->tt_changes_list_lock
);
172 /* track the change in the OGMinterval list */
173 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt_changes_list
);
174 atomic_inc(&bat_priv
->tt_local_changes
);
175 spin_unlock_bh(&bat_priv
->tt_changes_list_lock
);
177 atomic_set(&bat_priv
->tt_ogm_append_cnt
, 0);
180 int tt_len(int changes_num
)
182 return changes_num
* sizeof(struct tt_change
);
185 static int tt_local_init(struct bat_priv
*bat_priv
)
187 if (bat_priv
->tt_local_hash
)
190 bat_priv
->tt_local_hash
= hash_new(1024);
192 if (!bat_priv
->tt_local_hash
)
198 void tt_local_add(struct net_device
*soft_iface
, const uint8_t *addr
,
201 struct bat_priv
*bat_priv
= netdev_priv(soft_iface
);
202 struct tt_local_entry
*tt_local_entry
= NULL
;
203 struct tt_global_entry
*tt_global_entry
= NULL
;
205 tt_local_entry
= tt_local_hash_find(bat_priv
, addr
);
207 if (tt_local_entry
) {
208 tt_local_entry
->last_seen
= jiffies
;
212 tt_local_entry
= kmalloc(sizeof(*tt_local_entry
), GFP_ATOMIC
);
216 bat_dbg(DBG_TT
, bat_priv
,
217 "Creating new local tt entry: %pM (ttvn: %d)\n", addr
,
218 (uint8_t)atomic_read(&bat_priv
->ttvn
));
220 memcpy(tt_local_entry
->addr
, addr
, ETH_ALEN
);
221 tt_local_entry
->last_seen
= jiffies
;
222 tt_local_entry
->flags
= NO_FLAGS
;
223 if (is_wifi_iface(ifindex
))
224 tt_local_entry
->flags
|= TT_CLIENT_WIFI
;
225 atomic_set(&tt_local_entry
->refcount
, 2);
227 /* the batman interface mac address should never be purged */
228 if (compare_eth(addr
, soft_iface
->dev_addr
))
229 tt_local_entry
->flags
|= TT_CLIENT_NOPURGE
;
231 tt_local_event(bat_priv
, addr
, tt_local_entry
->flags
);
233 /* The local entry has to be marked as NEW to avoid to send it in
234 * a full table response going out before the next ttvn increment
235 * (consistency check) */
236 tt_local_entry
->flags
|= TT_CLIENT_NEW
;
238 hash_add(bat_priv
->tt_local_hash
, compare_ltt
, choose_orig
,
239 tt_local_entry
, &tt_local_entry
->hash_entry
);
241 /* remove address from global hash if present */
242 tt_global_entry
= tt_global_hash_find(bat_priv
, addr
);
244 /* Check whether it is a roaming! */
245 if (tt_global_entry
) {
246 /* This node is probably going to update its tt table */
247 tt_global_entry
->orig_node
->tt_poss_change
= true;
248 /* The global entry has to be marked as ROAMING and has to be
249 * kept for consistency purpose */
250 tt_global_entry
->flags
|= TT_CLIENT_ROAM
;
251 tt_global_entry
->roam_at
= jiffies
;
253 send_roam_adv(bat_priv
, tt_global_entry
->addr
,
254 tt_global_entry
->orig_node
);
258 tt_local_entry_free_ref(tt_local_entry
);
260 tt_global_entry_free_ref(tt_global_entry
);
263 int tt_changes_fill_buffer(struct bat_priv
*bat_priv
,
264 unsigned char *buff
, int buff_len
)
266 int count
= 0, tot_changes
= 0;
267 struct tt_change_node
*entry
, *safe
;
270 tot_changes
= buff_len
/ tt_len(1);
272 spin_lock_bh(&bat_priv
->tt_changes_list_lock
);
273 atomic_set(&bat_priv
->tt_local_changes
, 0);
275 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt_changes_list
,
277 if (count
< tot_changes
) {
278 memcpy(buff
+ tt_len(count
),
279 &entry
->change
, sizeof(struct tt_change
));
282 list_del(&entry
->list
);
285 spin_unlock_bh(&bat_priv
->tt_changes_list_lock
);
287 /* Keep the buffer for possible tt_request */
288 spin_lock_bh(&bat_priv
->tt_buff_lock
);
289 kfree(bat_priv
->tt_buff
);
290 bat_priv
->tt_buff_len
= 0;
291 bat_priv
->tt_buff
= NULL
;
292 /* We check whether this new OGM has no changes due to size
296 * if kmalloc() fails we will reply with the full table
297 * instead of providing the diff
299 bat_priv
->tt_buff
= kmalloc(buff_len
, GFP_ATOMIC
);
300 if (bat_priv
->tt_buff
) {
301 memcpy(bat_priv
->tt_buff
, buff
, buff_len
);
302 bat_priv
->tt_buff_len
= buff_len
;
305 spin_unlock_bh(&bat_priv
->tt_buff_lock
);
310 int tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
312 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
313 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
314 struct hashtable_t
*hash
= bat_priv
->tt_local_hash
;
315 struct tt_local_entry
*tt_local_entry
;
316 struct hard_iface
*primary_if
;
317 struct hlist_node
*node
;
318 struct hlist_head
*head
;
319 size_t buf_size
, pos
;
323 primary_if
= primary_if_get_selected(bat_priv
);
325 ret
= seq_printf(seq
, "BATMAN mesh %s disabled - "
326 "please specify interfaces to enable it\n",
331 if (primary_if
->if_status
!= IF_ACTIVE
) {
332 ret
= seq_printf(seq
, "BATMAN mesh %s disabled - "
333 "primary interface not active\n",
338 seq_printf(seq
, "Locally retrieved addresses (from %s) "
339 "announced via TT (TTVN: %u):\n",
340 net_dev
->name
, (uint8_t)atomic_read(&bat_priv
->ttvn
));
343 /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
344 for (i
= 0; i
< hash
->size
; i
++) {
345 head
= &hash
->table
[i
];
348 __hlist_for_each_rcu(node
, head
)
353 buff
= kmalloc(buf_size
, GFP_ATOMIC
);
362 for (i
= 0; i
< hash
->size
; i
++) {
363 head
= &hash
->table
[i
];
366 hlist_for_each_entry_rcu(tt_local_entry
, node
,
368 pos
+= snprintf(buff
+ pos
, 30, " * %pM "
370 tt_local_entry
->addr
,
371 (tt_local_entry
->flags
&
372 TT_CLIENT_ROAM
? 'R' : '.'),
373 (tt_local_entry
->flags
&
374 TT_CLIENT_NOPURGE
? 'P' : '.'),
375 (tt_local_entry
->flags
&
376 TT_CLIENT_NEW
? 'N' : '.'),
377 (tt_local_entry
->flags
&
378 TT_CLIENT_PENDING
? 'X' : '.'),
379 (tt_local_entry
->flags
&
380 TT_CLIENT_WIFI
? 'W' : '.'));
385 seq_printf(seq
, "%s", buff
);
389 hardif_free_ref(primary_if
);
393 static void tt_local_set_pending(struct bat_priv
*bat_priv
,
394 struct tt_local_entry
*tt_local_entry
,
397 tt_local_event(bat_priv
, tt_local_entry
->addr
,
398 tt_local_entry
->flags
| flags
);
400 /* The local client has to be marked as "pending to be removed" but has
401 * to be kept in the table in order to send it in a full table
402 * response issued before the net ttvn increment (consistency check) */
403 tt_local_entry
->flags
|= TT_CLIENT_PENDING
;
406 void tt_local_remove(struct bat_priv
*bat_priv
, const uint8_t *addr
,
407 const char *message
, bool roaming
)
409 struct tt_local_entry
*tt_local_entry
= NULL
;
411 tt_local_entry
= tt_local_hash_find(bat_priv
, addr
);
415 tt_local_set_pending(bat_priv
, tt_local_entry
, TT_CLIENT_DEL
|
416 (roaming
? TT_CLIENT_ROAM
: NO_FLAGS
));
418 bat_dbg(DBG_TT
, bat_priv
, "Local tt entry (%pM) pending to be removed: "
419 "%s\n", tt_local_entry
->addr
, message
);
422 tt_local_entry_free_ref(tt_local_entry
);
425 static void tt_local_purge(struct bat_priv
*bat_priv
)
427 struct hashtable_t
*hash
= bat_priv
->tt_local_hash
;
428 struct tt_local_entry
*tt_local_entry
;
429 struct hlist_node
*node
, *node_tmp
;
430 struct hlist_head
*head
;
431 spinlock_t
*list_lock
; /* protects write access to the hash lists */
434 for (i
= 0; i
< hash
->size
; i
++) {
435 head
= &hash
->table
[i
];
436 list_lock
= &hash
->list_locks
[i
];
438 spin_lock_bh(list_lock
);
439 hlist_for_each_entry_safe(tt_local_entry
, node
, node_tmp
,
441 if (tt_local_entry
->flags
& TT_CLIENT_NOPURGE
)
444 /* entry already marked for deletion */
445 if (tt_local_entry
->flags
& TT_CLIENT_PENDING
)
448 if (!is_out_of_time(tt_local_entry
->last_seen
,
449 TT_LOCAL_TIMEOUT
* 1000))
452 tt_local_set_pending(bat_priv
, tt_local_entry
,
454 bat_dbg(DBG_TT
, bat_priv
, "Local tt entry (%pM) "
455 "pending to be removed: timed out\n",
456 tt_local_entry
->addr
);
458 spin_unlock_bh(list_lock
);
463 static void tt_local_table_free(struct bat_priv
*bat_priv
)
465 struct hashtable_t
*hash
;
466 spinlock_t
*list_lock
; /* protects write access to the hash lists */
467 struct tt_local_entry
*tt_local_entry
;
468 struct hlist_node
*node
, *node_tmp
;
469 struct hlist_head
*head
;
472 if (!bat_priv
->tt_local_hash
)
475 hash
= bat_priv
->tt_local_hash
;
477 for (i
= 0; i
< hash
->size
; i
++) {
478 head
= &hash
->table
[i
];
479 list_lock
= &hash
->list_locks
[i
];
481 spin_lock_bh(list_lock
);
482 hlist_for_each_entry_safe(tt_local_entry
, node
, node_tmp
,
485 tt_local_entry_free_ref(tt_local_entry
);
487 spin_unlock_bh(list_lock
);
492 bat_priv
->tt_local_hash
= NULL
;
495 static int tt_global_init(struct bat_priv
*bat_priv
)
497 if (bat_priv
->tt_global_hash
)
500 bat_priv
->tt_global_hash
= hash_new(1024);
502 if (!bat_priv
->tt_global_hash
)
508 static void tt_changes_list_free(struct bat_priv
*bat_priv
)
510 struct tt_change_node
*entry
, *safe
;
512 spin_lock_bh(&bat_priv
->tt_changes_list_lock
);
514 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt_changes_list
,
516 list_del(&entry
->list
);
520 atomic_set(&bat_priv
->tt_local_changes
, 0);
521 spin_unlock_bh(&bat_priv
->tt_changes_list_lock
);
524 /* caller must hold orig_node refcount */
525 int tt_global_add(struct bat_priv
*bat_priv
, struct orig_node
*orig_node
,
526 const unsigned char *tt_addr
, uint8_t ttvn
, bool roaming
,
529 struct tt_global_entry
*tt_global_entry
;
530 struct orig_node
*orig_node_tmp
;
533 tt_global_entry
= tt_global_hash_find(bat_priv
, tt_addr
);
535 if (!tt_global_entry
) {
537 kmalloc(sizeof(*tt_global_entry
),
539 if (!tt_global_entry
)
542 memcpy(tt_global_entry
->addr
, tt_addr
, ETH_ALEN
);
543 /* Assign the new orig_node */
544 atomic_inc(&orig_node
->refcount
);
545 tt_global_entry
->orig_node
= orig_node
;
546 tt_global_entry
->ttvn
= ttvn
;
547 tt_global_entry
->flags
= NO_FLAGS
;
548 tt_global_entry
->roam_at
= 0;
549 atomic_set(&tt_global_entry
->refcount
, 2);
551 hash_add(bat_priv
->tt_global_hash
, compare_gtt
,
552 choose_orig
, tt_global_entry
,
553 &tt_global_entry
->hash_entry
);
554 atomic_inc(&orig_node
->tt_size
);
556 if (tt_global_entry
->orig_node
!= orig_node
) {
557 atomic_dec(&tt_global_entry
->orig_node
->tt_size
);
558 orig_node_tmp
= tt_global_entry
->orig_node
;
559 atomic_inc(&orig_node
->refcount
);
560 tt_global_entry
->orig_node
= orig_node
;
561 orig_node_free_ref(orig_node_tmp
);
562 atomic_inc(&orig_node
->tt_size
);
564 tt_global_entry
->ttvn
= ttvn
;
565 tt_global_entry
->flags
= NO_FLAGS
;
566 tt_global_entry
->roam_at
= 0;
570 tt_global_entry
->flags
|= TT_CLIENT_WIFI
;
572 bat_dbg(DBG_TT
, bat_priv
,
573 "Creating new global tt entry: %pM (via %pM)\n",
574 tt_global_entry
->addr
, orig_node
->orig
);
576 /* remove address from local hash if present */
577 tt_local_remove(bat_priv
, tt_global_entry
->addr
,
578 "global tt received", roaming
);
582 tt_global_entry_free_ref(tt_global_entry
);
586 int tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
588 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
589 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
590 struct hashtable_t
*hash
= bat_priv
->tt_global_hash
;
591 struct tt_global_entry
*tt_global_entry
;
592 struct hard_iface
*primary_if
;
593 struct hlist_node
*node
;
594 struct hlist_head
*head
;
595 size_t buf_size
, pos
;
599 primary_if
= primary_if_get_selected(bat_priv
);
601 ret
= seq_printf(seq
, "BATMAN mesh %s disabled - please "
602 "specify interfaces to enable it\n",
607 if (primary_if
->if_status
!= IF_ACTIVE
) {
608 ret
= seq_printf(seq
, "BATMAN mesh %s disabled - "
609 "primary interface not active\n",
615 "Globally announced TT entries received via the mesh %s\n",
617 seq_printf(seq
, " %-13s %s %-15s %s %s\n",
618 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
621 /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
622 * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
623 for (i
= 0; i
< hash
->size
; i
++) {
624 head
= &hash
->table
[i
];
627 __hlist_for_each_rcu(node
, head
)
632 buff
= kmalloc(buf_size
, GFP_ATOMIC
);
641 for (i
= 0; i
< hash
->size
; i
++) {
642 head
= &hash
->table
[i
];
645 hlist_for_each_entry_rcu(tt_global_entry
, node
,
647 pos
+= snprintf(buff
+ pos
, 69,
648 " * %pM (%3u) via %pM (%3u) "
649 "[%c%c%c]\n", tt_global_entry
->addr
,
650 tt_global_entry
->ttvn
,
651 tt_global_entry
->orig_node
->orig
,
652 (uint8_t) atomic_read(
653 &tt_global_entry
->orig_node
->
655 (tt_global_entry
->flags
&
656 TT_CLIENT_ROAM
? 'R' : '.'),
657 (tt_global_entry
->flags
&
658 TT_CLIENT_PENDING
? 'X' : '.'),
659 (tt_global_entry
->flags
&
660 TT_CLIENT_WIFI
? 'W' : '.'));
665 seq_printf(seq
, "%s", buff
);
669 hardif_free_ref(primary_if
);
673 static void _tt_global_del(struct bat_priv
*bat_priv
,
674 struct tt_global_entry
*tt_global_entry
,
677 if (!tt_global_entry
)
680 bat_dbg(DBG_TT
, bat_priv
,
681 "Deleting global tt entry %pM (via %pM): %s\n",
682 tt_global_entry
->addr
, tt_global_entry
->orig_node
->orig
,
685 atomic_dec(&tt_global_entry
->orig_node
->tt_size
);
687 hash_remove(bat_priv
->tt_global_hash
, compare_gtt
, choose_orig
,
688 tt_global_entry
->addr
);
691 tt_global_entry_free_ref(tt_global_entry
);
694 void tt_global_del(struct bat_priv
*bat_priv
,
695 struct orig_node
*orig_node
, const unsigned char *addr
,
696 const char *message
, bool roaming
)
698 struct tt_global_entry
*tt_global_entry
= NULL
;
699 struct tt_local_entry
*tt_local_entry
= NULL
;
701 tt_global_entry
= tt_global_hash_find(bat_priv
, addr
);
702 if (!tt_global_entry
)
705 if (tt_global_entry
->orig_node
== orig_node
) {
707 /* if we are deleting a global entry due to a roam
708 * event, there are two possibilities:
709 * 1) the client roamed from node A to node B => we mark
710 * it with TT_CLIENT_ROAM, we start a timer and we
711 * wait for node B to claim it. In case of timeout
712 * the entry is purged.
713 * 2) the client roamed to us => we can directly delete
714 * the global entry, since it is useless now. */
715 tt_local_entry
= tt_local_hash_find(bat_priv
,
716 tt_global_entry
->addr
);
717 if (!tt_local_entry
) {
718 tt_global_entry
->flags
|= TT_CLIENT_ROAM
;
719 tt_global_entry
->roam_at
= jiffies
;
723 _tt_global_del(bat_priv
, tt_global_entry
, message
);
727 tt_global_entry_free_ref(tt_global_entry
);
729 tt_local_entry_free_ref(tt_local_entry
);
732 void tt_global_del_orig(struct bat_priv
*bat_priv
,
733 struct orig_node
*orig_node
, const char *message
)
735 struct tt_global_entry
*tt_global_entry
;
737 struct hashtable_t
*hash
= bat_priv
->tt_global_hash
;
738 struct hlist_node
*node
, *safe
;
739 struct hlist_head
*head
;
740 spinlock_t
*list_lock
; /* protects write access to the hash lists */
745 for (i
= 0; i
< hash
->size
; i
++) {
746 head
= &hash
->table
[i
];
747 list_lock
= &hash
->list_locks
[i
];
749 spin_lock_bh(list_lock
);
750 hlist_for_each_entry_safe(tt_global_entry
, node
, safe
,
752 if (tt_global_entry
->orig_node
== orig_node
) {
753 bat_dbg(DBG_TT
, bat_priv
,
754 "Deleting global tt entry %pM "
755 "(via %pM): originator time out\n",
756 tt_global_entry
->addr
,
757 tt_global_entry
->orig_node
->orig
);
759 tt_global_entry_free_ref(tt_global_entry
);
762 spin_unlock_bh(list_lock
);
764 atomic_set(&orig_node
->tt_size
, 0);
767 static void tt_global_roam_purge(struct bat_priv
*bat_priv
)
769 struct hashtable_t
*hash
= bat_priv
->tt_global_hash
;
770 struct tt_global_entry
*tt_global_entry
;
771 struct hlist_node
*node
, *node_tmp
;
772 struct hlist_head
*head
;
773 spinlock_t
*list_lock
; /* protects write access to the hash lists */
776 for (i
= 0; i
< hash
->size
; i
++) {
777 head
= &hash
->table
[i
];
778 list_lock
= &hash
->list_locks
[i
];
780 spin_lock_bh(list_lock
);
781 hlist_for_each_entry_safe(tt_global_entry
, node
, node_tmp
,
783 if (!(tt_global_entry
->flags
& TT_CLIENT_ROAM
))
785 if (!is_out_of_time(tt_global_entry
->roam_at
,
786 TT_CLIENT_ROAM_TIMEOUT
* 1000))
789 bat_dbg(DBG_TT
, bat_priv
, "Deleting global "
790 "tt entry (%pM): Roaming timeout\n",
791 tt_global_entry
->addr
);
792 atomic_dec(&tt_global_entry
->orig_node
->tt_size
);
794 tt_global_entry_free_ref(tt_global_entry
);
796 spin_unlock_bh(list_lock
);
801 static void tt_global_table_free(struct bat_priv
*bat_priv
)
803 struct hashtable_t
*hash
;
804 spinlock_t
*list_lock
; /* protects write access to the hash lists */
805 struct tt_global_entry
*tt_global_entry
;
806 struct hlist_node
*node
, *node_tmp
;
807 struct hlist_head
*head
;
810 if (!bat_priv
->tt_global_hash
)
813 hash
= bat_priv
->tt_global_hash
;
815 for (i
= 0; i
< hash
->size
; i
++) {
816 head
= &hash
->table
[i
];
817 list_lock
= &hash
->list_locks
[i
];
819 spin_lock_bh(list_lock
);
820 hlist_for_each_entry_safe(tt_global_entry
, node
, node_tmp
,
823 tt_global_entry_free_ref(tt_global_entry
);
825 spin_unlock_bh(list_lock
);
830 bat_priv
->tt_global_hash
= NULL
;
833 static bool _is_ap_isolated(struct tt_local_entry
*tt_local_entry
,
834 struct tt_global_entry
*tt_global_entry
)
838 if (tt_local_entry
->flags
& TT_CLIENT_WIFI
&&
839 tt_global_entry
->flags
& TT_CLIENT_WIFI
)
845 struct orig_node
*transtable_search(struct bat_priv
*bat_priv
,
846 const uint8_t *src
, const uint8_t *addr
)
848 struct tt_local_entry
*tt_local_entry
= NULL
;
849 struct tt_global_entry
*tt_global_entry
= NULL
;
850 struct orig_node
*orig_node
= NULL
;
852 if (src
&& atomic_read(&bat_priv
->ap_isolation
)) {
853 tt_local_entry
= tt_local_hash_find(bat_priv
, src
);
858 tt_global_entry
= tt_global_hash_find(bat_priv
, addr
);
859 if (!tt_global_entry
)
862 /* check whether the clients should not communicate due to AP
864 if (tt_local_entry
&& _is_ap_isolated(tt_local_entry
, tt_global_entry
))
867 if (!atomic_inc_not_zero(&tt_global_entry
->orig_node
->refcount
))
870 /* A global client marked as PENDING has already moved from that
872 if (tt_global_entry
->flags
& TT_CLIENT_PENDING
)
875 orig_node
= tt_global_entry
->orig_node
;
879 tt_global_entry_free_ref(tt_global_entry
);
881 tt_local_entry_free_ref(tt_local_entry
);
886 /* Calculates the checksum of the local table of a given orig_node */
887 uint16_t tt_global_crc(struct bat_priv
*bat_priv
, struct orig_node
*orig_node
)
889 uint16_t total
= 0, total_one
;
890 struct hashtable_t
*hash
= bat_priv
->tt_global_hash
;
891 struct tt_global_entry
*tt_global_entry
;
892 struct hlist_node
*node
;
893 struct hlist_head
*head
;
896 for (i
= 0; i
< hash
->size
; i
++) {
897 head
= &hash
->table
[i
];
900 hlist_for_each_entry_rcu(tt_global_entry
, node
,
902 if (compare_eth(tt_global_entry
->orig_node
,
904 /* Roaming clients are in the global table for
905 * consistency only. They don't have to be
906 * taken into account while computing the
908 if (tt_global_entry
->flags
& TT_CLIENT_ROAM
)
911 for (j
= 0; j
< ETH_ALEN
; j
++)
912 total_one
= crc16_byte(total_one
,
913 tt_global_entry
->addr
[j
]);
923 /* Calculates the checksum of the local table */
924 uint16_t tt_local_crc(struct bat_priv
*bat_priv
)
926 uint16_t total
= 0, total_one
;
927 struct hashtable_t
*hash
= bat_priv
->tt_local_hash
;
928 struct tt_local_entry
*tt_local_entry
;
929 struct hlist_node
*node
;
930 struct hlist_head
*head
;
933 for (i
= 0; i
< hash
->size
; i
++) {
934 head
= &hash
->table
[i
];
937 hlist_for_each_entry_rcu(tt_local_entry
, node
,
939 /* not yet committed clients have not to be taken into
940 * account while computing the CRC */
941 if (tt_local_entry
->flags
& TT_CLIENT_NEW
)
944 for (j
= 0; j
< ETH_ALEN
; j
++)
945 total_one
= crc16_byte(total_one
,
946 tt_local_entry
->addr
[j
]);
955 static void tt_req_list_free(struct bat_priv
*bat_priv
)
957 struct tt_req_node
*node
, *safe
;
959 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
961 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_req_list
, list
) {
962 list_del(&node
->list
);
966 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
969 void tt_save_orig_buffer(struct bat_priv
*bat_priv
, struct orig_node
*orig_node
,
970 const unsigned char *tt_buff
, uint8_t tt_num_changes
)
972 uint16_t tt_buff_len
= tt_len(tt_num_changes
);
974 /* Replace the old buffer only if I received something in the
975 * last OGM (the OGM could carry no changes) */
976 spin_lock_bh(&orig_node
->tt_buff_lock
);
977 if (tt_buff_len
> 0) {
978 kfree(orig_node
->tt_buff
);
979 orig_node
->tt_buff_len
= 0;
980 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
981 if (orig_node
->tt_buff
) {
982 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
983 orig_node
->tt_buff_len
= tt_buff_len
;
986 spin_unlock_bh(&orig_node
->tt_buff_lock
);
989 static void tt_req_purge(struct bat_priv
*bat_priv
)
991 struct tt_req_node
*node
, *safe
;
993 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
994 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_req_list
, list
) {
995 if (is_out_of_time(node
->issued_at
,
996 TT_REQUEST_TIMEOUT
* 1000)) {
997 list_del(&node
->list
);
1001 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
1004 /* returns the pointer to the new tt_req_node struct if no request
1005 * has already been issued for this orig_node, NULL otherwise */
1006 static struct tt_req_node
*new_tt_req_node(struct bat_priv
*bat_priv
,
1007 struct orig_node
*orig_node
)
1009 struct tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
1011 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
1012 list_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt_req_list
, list
) {
1013 if (compare_eth(tt_req_node_tmp
, orig_node
) &&
1014 !is_out_of_time(tt_req_node_tmp
->issued_at
,
1015 TT_REQUEST_TIMEOUT
* 1000))
1019 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
1023 memcpy(tt_req_node
->addr
, orig_node
->orig
, ETH_ALEN
);
1024 tt_req_node
->issued_at
= jiffies
;
1026 list_add(&tt_req_node
->list
, &bat_priv
->tt_req_list
);
1028 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
1032 /* data_ptr is useless here, but has to be kept to respect the prototype */
1033 static int tt_local_valid_entry(const void *entry_ptr
, const void *data_ptr
)
1035 const struct tt_local_entry
*tt_local_entry
= entry_ptr
;
1037 if (tt_local_entry
->flags
& TT_CLIENT_NEW
)
1042 static int tt_global_valid_entry(const void *entry_ptr
, const void *data_ptr
)
1044 const struct tt_global_entry
*tt_global_entry
= entry_ptr
;
1045 const struct orig_node
*orig_node
= data_ptr
;
1047 if (tt_global_entry
->flags
& TT_CLIENT_ROAM
)
1050 return (tt_global_entry
->orig_node
== orig_node
);
1053 static struct sk_buff
*tt_response_fill_table(uint16_t tt_len
, uint8_t ttvn
,
1054 struct hashtable_t
*hash
,
1055 struct hard_iface
*primary_if
,
1056 int (*valid_cb
)(const void *,
1060 struct tt_local_entry
*tt_local_entry
;
1061 struct tt_query_packet
*tt_response
;
1062 struct tt_change
*tt_change
;
1063 struct hlist_node
*node
;
1064 struct hlist_head
*head
;
1065 struct sk_buff
*skb
= NULL
;
1066 uint16_t tt_tot
, tt_count
;
1067 ssize_t tt_query_size
= sizeof(struct tt_query_packet
);
1070 if (tt_query_size
+ tt_len
> primary_if
->soft_iface
->mtu
) {
1071 tt_len
= primary_if
->soft_iface
->mtu
- tt_query_size
;
1072 tt_len
-= tt_len
% sizeof(struct tt_change
);
1074 tt_tot
= tt_len
/ sizeof(struct tt_change
);
1076 skb
= dev_alloc_skb(tt_query_size
+ tt_len
+ ETH_HLEN
);
1080 skb_reserve(skb
, ETH_HLEN
);
1081 tt_response
= (struct tt_query_packet
*)skb_put(skb
,
1082 tt_query_size
+ tt_len
);
1083 tt_response
->ttvn
= ttvn
;
1085 tt_change
= (struct tt_change
*)(skb
->data
+ tt_query_size
);
1089 for (i
= 0; i
< hash
->size
; i
++) {
1090 head
= &hash
->table
[i
];
1092 hlist_for_each_entry_rcu(tt_local_entry
, node
,
1094 if (tt_count
== tt_tot
)
1097 if ((valid_cb
) && (!valid_cb(tt_local_entry
, cb_data
)))
1100 memcpy(tt_change
->addr
, tt_local_entry
->addr
, ETH_ALEN
);
1101 tt_change
->flags
= NO_FLAGS
;
1109 /* store in the message the number of entries we have successfully
1111 tt_response
->tt_data
= htons(tt_count
);
1117 static int send_tt_request(struct bat_priv
*bat_priv
,
1118 struct orig_node
*dst_orig_node
,
1119 uint8_t ttvn
, uint16_t tt_crc
, bool full_table
)
1121 struct sk_buff
*skb
= NULL
;
1122 struct tt_query_packet
*tt_request
;
1123 struct neigh_node
*neigh_node
= NULL
;
1124 struct hard_iface
*primary_if
;
1125 struct tt_req_node
*tt_req_node
= NULL
;
1128 primary_if
= primary_if_get_selected(bat_priv
);
1132 /* The new tt_req will be issued only if I'm not waiting for a
1133 * reply from the same orig_node yet */
1134 tt_req_node
= new_tt_req_node(bat_priv
, dst_orig_node
);
1138 skb
= dev_alloc_skb(sizeof(struct tt_query_packet
) + ETH_HLEN
);
1142 skb_reserve(skb
, ETH_HLEN
);
1144 tt_request
= (struct tt_query_packet
*)skb_put(skb
,
1145 sizeof(struct tt_query_packet
));
1147 tt_request
->packet_type
= BAT_TT_QUERY
;
1148 tt_request
->version
= COMPAT_VERSION
;
1149 memcpy(tt_request
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1150 memcpy(tt_request
->dst
, dst_orig_node
->orig
, ETH_ALEN
);
1151 tt_request
->ttl
= TTL
;
1152 tt_request
->ttvn
= ttvn
;
1153 tt_request
->tt_data
= tt_crc
;
1154 tt_request
->flags
= TT_REQUEST
;
1157 tt_request
->flags
|= TT_FULL_TABLE
;
1159 neigh_node
= orig_node_get_router(dst_orig_node
);
1163 bat_dbg(DBG_TT
, bat_priv
, "Sending TT_REQUEST to %pM via %pM "
1164 "[%c]\n", dst_orig_node
->orig
, neigh_node
->addr
,
1165 (full_table
? 'F' : '.'));
1167 send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1172 neigh_node_free_ref(neigh_node
);
1174 hardif_free_ref(primary_if
);
1177 if (ret
&& tt_req_node
) {
1178 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
1179 list_del(&tt_req_node
->list
);
1180 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
1186 static bool send_other_tt_response(struct bat_priv
*bat_priv
,
1187 struct tt_query_packet
*tt_request
)
1189 struct orig_node
*req_dst_orig_node
= NULL
, *res_dst_orig_node
= NULL
;
1190 struct neigh_node
*neigh_node
= NULL
;
1191 struct hard_iface
*primary_if
= NULL
;
1192 uint8_t orig_ttvn
, req_ttvn
, ttvn
;
1194 unsigned char *tt_buff
;
1196 uint16_t tt_len
, tt_tot
;
1197 struct sk_buff
*skb
= NULL
;
1198 struct tt_query_packet
*tt_response
;
1200 bat_dbg(DBG_TT
, bat_priv
,
1201 "Received TT_REQUEST from %pM for "
1202 "ttvn: %u (%pM) [%c]\n", tt_request
->src
,
1203 tt_request
->ttvn
, tt_request
->dst
,
1204 (tt_request
->flags
& TT_FULL_TABLE
? 'F' : '.'));
1206 /* Let's get the orig node of the REAL destination */
1207 req_dst_orig_node
= get_orig_node(bat_priv
, tt_request
->dst
);
1208 if (!req_dst_orig_node
)
1211 res_dst_orig_node
= get_orig_node(bat_priv
, tt_request
->src
);
1212 if (!res_dst_orig_node
)
1215 neigh_node
= orig_node_get_router(res_dst_orig_node
);
1219 primary_if
= primary_if_get_selected(bat_priv
);
1223 orig_ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1224 req_ttvn
= tt_request
->ttvn
;
1226 /* I don't have the requested data */
1227 if (orig_ttvn
!= req_ttvn
||
1228 tt_request
->tt_data
!= req_dst_orig_node
->tt_crc
)
1231 /* If the full table has been explicitly requested */
1232 if (tt_request
->flags
& TT_FULL_TABLE
||
1233 !req_dst_orig_node
->tt_buff
)
1238 /* In this version, fragmentation is not implemented, then
1239 * I'll send only one packet with as much TT entries as I can */
1241 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
1242 tt_len
= req_dst_orig_node
->tt_buff_len
;
1243 tt_tot
= tt_len
/ sizeof(struct tt_change
);
1245 skb
= dev_alloc_skb(sizeof(struct tt_query_packet
) +
1250 skb_reserve(skb
, ETH_HLEN
);
1251 tt_response
= (struct tt_query_packet
*)skb_put(skb
,
1252 sizeof(struct tt_query_packet
) + tt_len
);
1253 tt_response
->ttvn
= req_ttvn
;
1254 tt_response
->tt_data
= htons(tt_tot
);
1256 tt_buff
= skb
->data
+ sizeof(struct tt_query_packet
);
1257 /* Copy the last orig_node's OGM buffer */
1258 memcpy(tt_buff
, req_dst_orig_node
->tt_buff
,
1259 req_dst_orig_node
->tt_buff_len
);
1261 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1263 tt_len
= (uint16_t)atomic_read(&req_dst_orig_node
->tt_size
) *
1264 sizeof(struct tt_change
);
1265 ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1267 skb
= tt_response_fill_table(tt_len
, ttvn
,
1268 bat_priv
->tt_global_hash
,
1269 primary_if
, tt_global_valid_entry
,
1274 tt_response
= (struct tt_query_packet
*)skb
->data
;
1277 tt_response
->packet_type
= BAT_TT_QUERY
;
1278 tt_response
->version
= COMPAT_VERSION
;
1279 tt_response
->ttl
= TTL
;
1280 memcpy(tt_response
->src
, req_dst_orig_node
->orig
, ETH_ALEN
);
1281 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1282 tt_response
->flags
= TT_RESPONSE
;
1285 tt_response
->flags
|= TT_FULL_TABLE
;
1287 bat_dbg(DBG_TT
, bat_priv
,
1288 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1289 res_dst_orig_node
->orig
, neigh_node
->addr
,
1290 req_dst_orig_node
->orig
, req_ttvn
);
1292 send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1297 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1300 if (res_dst_orig_node
)
1301 orig_node_free_ref(res_dst_orig_node
);
1302 if (req_dst_orig_node
)
1303 orig_node_free_ref(req_dst_orig_node
);
1305 neigh_node_free_ref(neigh_node
);
1307 hardif_free_ref(primary_if
);
1313 static bool send_my_tt_response(struct bat_priv
*bat_priv
,
1314 struct tt_query_packet
*tt_request
)
1316 struct orig_node
*orig_node
= NULL
;
1317 struct neigh_node
*neigh_node
= NULL
;
1318 struct hard_iface
*primary_if
= NULL
;
1319 uint8_t my_ttvn
, req_ttvn
, ttvn
;
1321 unsigned char *tt_buff
;
1323 uint16_t tt_len
, tt_tot
;
1324 struct sk_buff
*skb
= NULL
;
1325 struct tt_query_packet
*tt_response
;
1327 bat_dbg(DBG_TT
, bat_priv
,
1328 "Received TT_REQUEST from %pM for "
1329 "ttvn: %u (me) [%c]\n", tt_request
->src
,
1331 (tt_request
->flags
& TT_FULL_TABLE
? 'F' : '.'));
1334 my_ttvn
= (uint8_t)atomic_read(&bat_priv
->ttvn
);
1335 req_ttvn
= tt_request
->ttvn
;
1337 orig_node
= get_orig_node(bat_priv
, tt_request
->src
);
1341 neigh_node
= orig_node_get_router(orig_node
);
1345 primary_if
= primary_if_get_selected(bat_priv
);
1349 /* If the full table has been explicitly requested or the gap
1350 * is too big send the whole local translation table */
1351 if (tt_request
->flags
& TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
1357 /* In this version, fragmentation is not implemented, then
1358 * I'll send only one packet with as much TT entries as I can */
1360 spin_lock_bh(&bat_priv
->tt_buff_lock
);
1361 tt_len
= bat_priv
->tt_buff_len
;
1362 tt_tot
= tt_len
/ sizeof(struct tt_change
);
1364 skb
= dev_alloc_skb(sizeof(struct tt_query_packet
) +
1369 skb_reserve(skb
, ETH_HLEN
);
1370 tt_response
= (struct tt_query_packet
*)skb_put(skb
,
1371 sizeof(struct tt_query_packet
) + tt_len
);
1372 tt_response
->ttvn
= req_ttvn
;
1373 tt_response
->tt_data
= htons(tt_tot
);
1375 tt_buff
= skb
->data
+ sizeof(struct tt_query_packet
);
1376 memcpy(tt_buff
, bat_priv
->tt_buff
,
1377 bat_priv
->tt_buff_len
);
1378 spin_unlock_bh(&bat_priv
->tt_buff_lock
);
1380 tt_len
= (uint16_t)atomic_read(&bat_priv
->num_local_tt
) *
1381 sizeof(struct tt_change
);
1382 ttvn
= (uint8_t)atomic_read(&bat_priv
->ttvn
);
1384 skb
= tt_response_fill_table(tt_len
, ttvn
,
1385 bat_priv
->tt_local_hash
,
1386 primary_if
, tt_local_valid_entry
,
1391 tt_response
= (struct tt_query_packet
*)skb
->data
;
1394 tt_response
->packet_type
= BAT_TT_QUERY
;
1395 tt_response
->version
= COMPAT_VERSION
;
1396 tt_response
->ttl
= TTL
;
1397 memcpy(tt_response
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1398 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1399 tt_response
->flags
= TT_RESPONSE
;
1402 tt_response
->flags
|= TT_FULL_TABLE
;
1404 bat_dbg(DBG_TT
, bat_priv
,
1405 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1406 orig_node
->orig
, neigh_node
->addr
,
1407 (tt_response
->flags
& TT_FULL_TABLE
? 'F' : '.'));
1409 send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1414 spin_unlock_bh(&bat_priv
->tt_buff_lock
);
1417 orig_node_free_ref(orig_node
);
1419 neigh_node_free_ref(neigh_node
);
1421 hardif_free_ref(primary_if
);
1424 /* This packet was for me, so it doesn't need to be re-routed */
1428 bool send_tt_response(struct bat_priv
*bat_priv
,
1429 struct tt_query_packet
*tt_request
)
1431 if (is_my_mac(tt_request
->dst
))
1432 return send_my_tt_response(bat_priv
, tt_request
);
1434 return send_other_tt_response(bat_priv
, tt_request
);
1437 static void _tt_update_changes(struct bat_priv
*bat_priv
,
1438 struct orig_node
*orig_node
,
1439 struct tt_change
*tt_change
,
1440 uint16_t tt_num_changes
, uint8_t ttvn
)
1444 for (i
= 0; i
< tt_num_changes
; i
++) {
1445 if ((tt_change
+ i
)->flags
& TT_CLIENT_DEL
)
1446 tt_global_del(bat_priv
, orig_node
,
1447 (tt_change
+ i
)->addr
,
1448 "tt removed by changes",
1449 (tt_change
+ i
)->flags
& TT_CLIENT_ROAM
);
1451 if (!tt_global_add(bat_priv
, orig_node
,
1452 (tt_change
+ i
)->addr
, ttvn
, false,
1453 (tt_change
+ i
)->flags
&
1455 /* In case of problem while storing a
1456 * global_entry, we stop the updating
1457 * procedure without committing the
1458 * ttvn change. This will avoid to send
1459 * corrupted data on tt_request
1465 static void tt_fill_gtable(struct bat_priv
*bat_priv
,
1466 struct tt_query_packet
*tt_response
)
1468 struct orig_node
*orig_node
= NULL
;
1470 orig_node
= orig_hash_find(bat_priv
, tt_response
->src
);
1474 /* Purge the old table first.. */
1475 tt_global_del_orig(bat_priv
, orig_node
, "Received full table");
1477 _tt_update_changes(bat_priv
, orig_node
,
1478 (struct tt_change
*)(tt_response
+ 1),
1479 tt_response
->tt_data
, tt_response
->ttvn
);
1481 spin_lock_bh(&orig_node
->tt_buff_lock
);
1482 kfree(orig_node
->tt_buff
);
1483 orig_node
->tt_buff_len
= 0;
1484 orig_node
->tt_buff
= NULL
;
1485 spin_unlock_bh(&orig_node
->tt_buff_lock
);
1487 atomic_set(&orig_node
->last_ttvn
, tt_response
->ttvn
);
1491 orig_node_free_ref(orig_node
);
1494 static void tt_update_changes(struct bat_priv
*bat_priv
,
1495 struct orig_node
*orig_node
,
1496 uint16_t tt_num_changes
, uint8_t ttvn
,
1497 struct tt_change
*tt_change
)
1499 _tt_update_changes(bat_priv
, orig_node
, tt_change
, tt_num_changes
,
1502 tt_save_orig_buffer(bat_priv
, orig_node
, (unsigned char *)tt_change
,
1504 atomic_set(&orig_node
->last_ttvn
, ttvn
);
1507 bool is_my_client(struct bat_priv
*bat_priv
, const uint8_t *addr
)
1509 struct tt_local_entry
*tt_local_entry
= NULL
;
1512 tt_local_entry
= tt_local_hash_find(bat_priv
, addr
);
1513 if (!tt_local_entry
)
1515 /* Check if the client has been logically deleted (but is kept for
1516 * consistency purpose) */
1517 if (tt_local_entry
->flags
& TT_CLIENT_PENDING
)
1522 tt_local_entry_free_ref(tt_local_entry
);
1526 void handle_tt_response(struct bat_priv
*bat_priv
,
1527 struct tt_query_packet
*tt_response
)
1529 struct tt_req_node
*node
, *safe
;
1530 struct orig_node
*orig_node
= NULL
;
1532 bat_dbg(DBG_TT
, bat_priv
, "Received TT_RESPONSE from %pM for "
1533 "ttvn %d t_size: %d [%c]\n",
1534 tt_response
->src
, tt_response
->ttvn
,
1535 tt_response
->tt_data
,
1536 (tt_response
->flags
& TT_FULL_TABLE
? 'F' : '.'));
1538 orig_node
= orig_hash_find(bat_priv
, tt_response
->src
);
1542 if (tt_response
->flags
& TT_FULL_TABLE
)
1543 tt_fill_gtable(bat_priv
, tt_response
);
1545 tt_update_changes(bat_priv
, orig_node
, tt_response
->tt_data
,
1547 (struct tt_change
*)(tt_response
+ 1));
1549 /* Delete the tt_req_node from pending tt_requests list */
1550 spin_lock_bh(&bat_priv
->tt_req_list_lock
);
1551 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_req_list
, list
) {
1552 if (!compare_eth(node
->addr
, tt_response
->src
))
1554 list_del(&node
->list
);
1557 spin_unlock_bh(&bat_priv
->tt_req_list_lock
);
1559 /* Recalculate the CRC for this orig_node and store it */
1560 orig_node
->tt_crc
= tt_global_crc(bat_priv
, orig_node
);
1561 /* Roaming phase is over: tables are in sync again. I can
1563 orig_node
->tt_poss_change
= false;
1566 orig_node_free_ref(orig_node
);
1569 int tt_init(struct bat_priv
*bat_priv
)
1571 if (!tt_local_init(bat_priv
))
1574 if (!tt_global_init(bat_priv
))
1577 tt_start_timer(bat_priv
);
1582 static void tt_roam_list_free(struct bat_priv
*bat_priv
)
1584 struct tt_roam_node
*node
, *safe
;
1586 spin_lock_bh(&bat_priv
->tt_roam_list_lock
);
1588 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_roam_list
, list
) {
1589 list_del(&node
->list
);
1593 spin_unlock_bh(&bat_priv
->tt_roam_list_lock
);
1596 static void tt_roam_purge(struct bat_priv
*bat_priv
)
1598 struct tt_roam_node
*node
, *safe
;
1600 spin_lock_bh(&bat_priv
->tt_roam_list_lock
);
1601 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt_roam_list
, list
) {
1602 if (!is_out_of_time(node
->first_time
,
1603 ROAMING_MAX_TIME
* 1000))
1606 list_del(&node
->list
);
1609 spin_unlock_bh(&bat_priv
->tt_roam_list_lock
);
1612 /* This function checks whether the client already reached the
1613 * maximum number of possible roaming phases. In this case the ROAMING_ADV
1616 * returns true if the ROAMING_ADV can be sent, false otherwise */
1617 static bool tt_check_roam_count(struct bat_priv
*bat_priv
,
1620 struct tt_roam_node
*tt_roam_node
;
1623 spin_lock_bh(&bat_priv
->tt_roam_list_lock
);
1624 /* The new tt_req will be issued only if I'm not waiting for a
1625 * reply from the same orig_node yet */
1626 list_for_each_entry(tt_roam_node
, &bat_priv
->tt_roam_list
, list
) {
1627 if (!compare_eth(tt_roam_node
->addr
, client
))
1630 if (is_out_of_time(tt_roam_node
->first_time
,
1631 ROAMING_MAX_TIME
* 1000))
1634 if (!atomic_dec_not_zero(&tt_roam_node
->counter
))
1635 /* Sorry, you roamed too many times! */
1642 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
1646 tt_roam_node
->first_time
= jiffies
;
1647 atomic_set(&tt_roam_node
->counter
, ROAMING_MAX_COUNT
- 1);
1648 memcpy(tt_roam_node
->addr
, client
, ETH_ALEN
);
1650 list_add(&tt_roam_node
->list
, &bat_priv
->tt_roam_list
);
1655 spin_unlock_bh(&bat_priv
->tt_roam_list_lock
);
1659 void send_roam_adv(struct bat_priv
*bat_priv
, uint8_t *client
,
1660 struct orig_node
*orig_node
)
1662 struct neigh_node
*neigh_node
= NULL
;
1663 struct sk_buff
*skb
= NULL
;
1664 struct roam_adv_packet
*roam_adv_packet
;
1666 struct hard_iface
*primary_if
;
1668 /* before going on we have to check whether the client has
1669 * already roamed to us too many times */
1670 if (!tt_check_roam_count(bat_priv
, client
))
1673 skb
= dev_alloc_skb(sizeof(struct roam_adv_packet
) + ETH_HLEN
);
1677 skb_reserve(skb
, ETH_HLEN
);
1679 roam_adv_packet
= (struct roam_adv_packet
*)skb_put(skb
,
1680 sizeof(struct roam_adv_packet
));
1682 roam_adv_packet
->packet_type
= BAT_ROAM_ADV
;
1683 roam_adv_packet
->version
= COMPAT_VERSION
;
1684 roam_adv_packet
->ttl
= TTL
;
1685 primary_if
= primary_if_get_selected(bat_priv
);
1688 memcpy(roam_adv_packet
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1689 hardif_free_ref(primary_if
);
1690 memcpy(roam_adv_packet
->dst
, orig_node
->orig
, ETH_ALEN
);
1691 memcpy(roam_adv_packet
->client
, client
, ETH_ALEN
);
1693 neigh_node
= orig_node_get_router(orig_node
);
1697 bat_dbg(DBG_TT
, bat_priv
,
1698 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1699 orig_node
->orig
, client
, neigh_node
->addr
);
1701 send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1706 neigh_node_free_ref(neigh_node
);
1712 static void tt_purge(struct work_struct
*work
)
1714 struct delayed_work
*delayed_work
=
1715 container_of(work
, struct delayed_work
, work
);
1716 struct bat_priv
*bat_priv
=
1717 container_of(delayed_work
, struct bat_priv
, tt_work
);
1719 tt_local_purge(bat_priv
);
1720 tt_global_roam_purge(bat_priv
);
1721 tt_req_purge(bat_priv
);
1722 tt_roam_purge(bat_priv
);
1724 tt_start_timer(bat_priv
);
1727 void tt_free(struct bat_priv
*bat_priv
)
1729 cancel_delayed_work_sync(&bat_priv
->tt_work
);
1731 tt_local_table_free(bat_priv
);
1732 tt_global_table_free(bat_priv
);
1733 tt_req_list_free(bat_priv
);
1734 tt_changes_list_free(bat_priv
);
1735 tt_roam_list_free(bat_priv
);
1737 kfree(bat_priv
->tt_buff
);
1740 /* This function will reset the specified flags from all the entries in
1741 * the given hash table and will increment num_local_tt for each involved
1743 static void tt_local_reset_flags(struct bat_priv
*bat_priv
, uint16_t flags
)
1746 struct hashtable_t
*hash
= bat_priv
->tt_local_hash
;
1747 struct hlist_head
*head
;
1748 struct hlist_node
*node
;
1749 struct tt_local_entry
*tt_local_entry
;
1754 for (i
= 0; i
< hash
->size
; i
++) {
1755 head
= &hash
->table
[i
];
1758 hlist_for_each_entry_rcu(tt_local_entry
, node
,
1760 if (!(tt_local_entry
->flags
& flags
))
1762 tt_local_entry
->flags
&= ~flags
;
1763 atomic_inc(&bat_priv
->num_local_tt
);
1770 /* Purge out all the tt local entries marked with TT_CLIENT_PENDING */
1771 static void tt_local_purge_pending_clients(struct bat_priv
*bat_priv
)
1773 struct hashtable_t
*hash
= bat_priv
->tt_local_hash
;
1774 struct tt_local_entry
*tt_local_entry
;
1775 struct hlist_node
*node
, *node_tmp
;
1776 struct hlist_head
*head
;
1777 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1783 for (i
= 0; i
< hash
->size
; i
++) {
1784 head
= &hash
->table
[i
];
1785 list_lock
= &hash
->list_locks
[i
];
1787 spin_lock_bh(list_lock
);
1788 hlist_for_each_entry_safe(tt_local_entry
, node
, node_tmp
,
1790 if (!(tt_local_entry
->flags
& TT_CLIENT_PENDING
))
1793 bat_dbg(DBG_TT
, bat_priv
, "Deleting local tt entry "
1794 "(%pM): pending\n", tt_local_entry
->addr
);
1796 atomic_dec(&bat_priv
->num_local_tt
);
1797 hlist_del_rcu(node
);
1798 tt_local_entry_free_ref(tt_local_entry
);
1800 spin_unlock_bh(list_lock
);
1805 void tt_commit_changes(struct bat_priv
*bat_priv
)
1807 tt_local_reset_flags(bat_priv
, TT_CLIENT_NEW
);
1808 tt_local_purge_pending_clients(bat_priv
);
1810 /* Increment the TTVN only once per OGM interval */
1811 atomic_inc(&bat_priv
->ttvn
);
1812 bat_priv
->tt_poss_change
= false;
1815 bool is_ap_isolated(struct bat_priv
*bat_priv
, uint8_t *src
, uint8_t *dst
)
1817 struct tt_local_entry
*tt_local_entry
= NULL
;
1818 struct tt_global_entry
*tt_global_entry
= NULL
;
1821 if (!atomic_read(&bat_priv
->ap_isolation
))
1824 tt_local_entry
= tt_local_hash_find(bat_priv
, dst
);
1825 if (!tt_local_entry
)
1828 tt_global_entry
= tt_global_hash_find(bat_priv
, src
);
1829 if (!tt_global_entry
)
1832 if (_is_ap_isolated(tt_local_entry
, tt_global_entry
))
1838 if (tt_global_entry
)
1839 tt_global_entry_free_ref(tt_global_entry
);
1841 tt_local_entry_free_ref(tt_local_entry
);
1845 void tt_update_orig(struct bat_priv
*bat_priv
, struct orig_node
*orig_node
,
1846 const unsigned char *tt_buff
, uint8_t tt_num_changes
,
1847 uint8_t ttvn
, uint16_t tt_crc
)
1849 uint8_t orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
1850 bool full_table
= true;
1852 /* the ttvn increased by one -> we can apply the attached changes */
1853 if (ttvn
- orig_ttvn
== 1) {
1854 /* the OGM could not contain the changes due to their size or
1855 * because they have already been sent TT_OGM_APPEND_MAX times.
1856 * In this case send a tt request */
1857 if (!tt_num_changes
) {
1862 tt_update_changes(bat_priv
, orig_node
, tt_num_changes
, ttvn
,
1863 (struct tt_change
*)tt_buff
);
1865 /* Even if we received the precomputed crc with the OGM, we
1866 * prefer to recompute it to spot any possible inconsistency
1867 * in the global table */
1868 orig_node
->tt_crc
= tt_global_crc(bat_priv
, orig_node
);
1870 /* The ttvn alone is not enough to guarantee consistency
1871 * because a single value could represent different states
1872 * (due to the wrap around). Thus a node has to check whether
1873 * the resulting table (after applying the changes) is still
1874 * consistent or not. E.g. a node could disconnect while its
1875 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
1876 * checking the CRC value is mandatory to detect the
1878 if (orig_node
->tt_crc
!= tt_crc
)
1881 /* Roaming phase is over: tables are in sync again. I can
1883 orig_node
->tt_poss_change
= false;
1885 /* if we missed more than one change or our tables are not
1886 * in sync anymore -> request fresh tt data */
1887 if (ttvn
!= orig_ttvn
|| orig_node
->tt_crc
!= tt_crc
) {
1889 bat_dbg(DBG_TT
, bat_priv
, "TT inconsistency for %pM. "
1890 "Need to retrieve the correct information "
1891 "(ttvn: %u last_ttvn: %u crc: %u last_crc: "
1892 "%u num_changes: %u)\n", orig_node
->orig
, ttvn
,
1893 orig_ttvn
, tt_crc
, orig_node
->tt_crc
,
1895 send_tt_request(bat_priv
, orig_node
, ttvn
, tt_crc
,