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
);
37 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
38 struct batadv_orig_node
*orig_node
,
39 const unsigned char *addr
,
40 const char *message
, bool roaming
);
42 /* returns 1 if they are the same mac addr */
43 static int batadv_compare_tt(const struct hlist_node
*node
, const void *data2
)
45 const void *data1
= container_of(node
, struct batadv_tt_common_entry
,
48 return (memcmp(data1
, data2
, ETH_ALEN
) == 0 ? 1 : 0);
51 static void batadv_tt_start_timer(struct batadv_priv
*bat_priv
)
53 INIT_DELAYED_WORK(&bat_priv
->tt
.work
, batadv_tt_purge
);
54 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->tt
.work
,
55 msecs_to_jiffies(5000));
58 static struct batadv_tt_common_entry
*
59 batadv_tt_hash_find(struct batadv_hashtable
*hash
, const void *data
)
61 struct hlist_head
*head
;
62 struct hlist_node
*node
;
63 struct batadv_tt_common_entry
*tt_common_entry
;
64 struct batadv_tt_common_entry
*tt_common_entry_tmp
= NULL
;
70 index
= batadv_choose_orig(data
, hash
->size
);
71 head
= &hash
->table
[index
];
74 hlist_for_each_entry_rcu(tt_common_entry
, node
, head
, hash_entry
) {
75 if (!batadv_compare_eth(tt_common_entry
, data
))
78 if (!atomic_inc_not_zero(&tt_common_entry
->refcount
))
81 tt_common_entry_tmp
= tt_common_entry
;
86 return tt_common_entry_tmp
;
89 static struct batadv_tt_local_entry
*
90 batadv_tt_local_hash_find(struct batadv_priv
*bat_priv
, const void *data
)
92 struct batadv_tt_common_entry
*tt_common_entry
;
93 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
95 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.local_hash
, data
);
97 tt_local_entry
= container_of(tt_common_entry
,
98 struct batadv_tt_local_entry
,
100 return tt_local_entry
;
103 static struct batadv_tt_global_entry
*
104 batadv_tt_global_hash_find(struct batadv_priv
*bat_priv
, const void *data
)
106 struct batadv_tt_common_entry
*tt_common_entry
;
107 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
109 tt_common_entry
= batadv_tt_hash_find(bat_priv
->tt
.global_hash
, data
);
111 tt_global_entry
= container_of(tt_common_entry
,
112 struct batadv_tt_global_entry
,
114 return tt_global_entry
;
119 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry
*tt_local_entry
)
121 if (atomic_dec_and_test(&tt_local_entry
->common
.refcount
))
122 kfree_rcu(tt_local_entry
, common
.rcu
);
125 static void batadv_tt_global_entry_free_rcu(struct rcu_head
*rcu
)
127 struct batadv_tt_common_entry
*tt_common_entry
;
128 struct batadv_tt_global_entry
*tt_global_entry
;
130 tt_common_entry
= container_of(rcu
, struct batadv_tt_common_entry
, rcu
);
131 tt_global_entry
= container_of(tt_common_entry
,
132 struct batadv_tt_global_entry
, common
);
134 kfree(tt_global_entry
);
138 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry
*tt_global_entry
)
140 if (atomic_dec_and_test(&tt_global_entry
->common
.refcount
)) {
141 batadv_tt_global_del_orig_list(tt_global_entry
);
142 call_rcu(&tt_global_entry
->common
.rcu
,
143 batadv_tt_global_entry_free_rcu
);
147 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head
*rcu
)
149 struct batadv_tt_orig_list_entry
*orig_entry
;
151 orig_entry
= container_of(rcu
, struct batadv_tt_orig_list_entry
, rcu
);
152 batadv_orig_node_free_ref(orig_entry
->orig_node
);
157 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry
*orig_entry
)
159 if (!atomic_dec_and_test(&orig_entry
->refcount
))
161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry
->orig_node
->tt_size
);
163 call_rcu(&orig_entry
->rcu
, batadv_tt_orig_list_entry_free_rcu
);
166 static void batadv_tt_local_event(struct batadv_priv
*bat_priv
,
167 const uint8_t *addr
, uint8_t flags
)
169 struct batadv_tt_change_node
*tt_change_node
, *entry
, *safe
;
170 bool event_removed
= false;
171 bool del_op_requested
, del_op_entry
;
173 tt_change_node
= kmalloc(sizeof(*tt_change_node
), GFP_ATOMIC
);
178 tt_change_node
->change
.flags
= flags
;
179 memcpy(tt_change_node
->change
.addr
, addr
, ETH_ALEN
);
181 del_op_requested
= flags
& BATADV_TT_CLIENT_DEL
;
183 /* check for ADD+DEL or DEL+ADD events */
184 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
185 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
187 if (!batadv_compare_eth(entry
->change
.addr
, addr
))
190 /* DEL+ADD in the same orig interval have no effect and can be
191 * removed to avoid silly behaviour on the receiver side. The
192 * other way around (ADD+DEL) can happen in case of roaming of
193 * a client still in the NEW state. Roaming of NEW clients is
194 * now possible due to automatically recognition of "temporary"
197 del_op_entry
= entry
->change
.flags
& BATADV_TT_CLIENT_DEL
;
198 if (!del_op_requested
&& del_op_entry
)
200 if (del_op_requested
&& !del_op_entry
)
204 list_del(&entry
->list
);
206 kfree(tt_change_node
);
207 event_removed
= true;
211 /* track the change in the OGMinterval list */
212 list_add_tail(&tt_change_node
->list
, &bat_priv
->tt
.changes_list
);
215 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
218 atomic_dec(&bat_priv
->tt
.local_changes
);
220 atomic_inc(&bat_priv
->tt
.local_changes
);
223 int batadv_tt_len(int changes_num
)
225 return changes_num
* sizeof(struct batadv_tt_change
);
228 static int batadv_tt_local_init(struct batadv_priv
*bat_priv
)
230 if (bat_priv
->tt
.local_hash
)
233 bat_priv
->tt
.local_hash
= batadv_hash_new(1024);
235 if (!bat_priv
->tt
.local_hash
)
241 void batadv_tt_local_add(struct net_device
*soft_iface
, const uint8_t *addr
,
244 struct batadv_priv
*bat_priv
= netdev_priv(soft_iface
);
245 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
246 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
247 struct hlist_head
*head
;
248 struct hlist_node
*node
;
249 struct batadv_tt_orig_list_entry
*orig_entry
;
252 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
254 if (tt_local_entry
) {
255 tt_local_entry
->last_seen
= jiffies
;
256 /* possibly unset the BATADV_TT_CLIENT_PENDING flag */
257 tt_local_entry
->common
.flags
&= ~BATADV_TT_CLIENT_PENDING
;
261 tt_local_entry
= kmalloc(sizeof(*tt_local_entry
), GFP_ATOMIC
);
265 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
266 "Creating new local tt entry: %pM (ttvn: %d)\n", addr
,
267 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
269 memcpy(tt_local_entry
->common
.addr
, addr
, ETH_ALEN
);
270 tt_local_entry
->common
.flags
= BATADV_NO_FLAGS
;
271 if (batadv_is_wifi_iface(ifindex
))
272 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_WIFI
;
273 atomic_set(&tt_local_entry
->common
.refcount
, 2);
274 tt_local_entry
->last_seen
= jiffies
;
275 tt_local_entry
->common
.added_at
= tt_local_entry
->last_seen
;
277 /* the batman interface mac address should never be purged */
278 if (batadv_compare_eth(addr
, soft_iface
->dev_addr
))
279 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_NOPURGE
;
281 /* The local entry has to be marked as NEW to avoid to send it in
282 * a full table response going out before the next ttvn increment
283 * (consistency check)
285 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_NEW
;
287 hash_added
= batadv_hash_add(bat_priv
->tt
.local_hash
, batadv_compare_tt
,
289 &tt_local_entry
->common
,
290 &tt_local_entry
->common
.hash_entry
);
292 if (unlikely(hash_added
!= 0)) {
293 /* remove the reference for the hash */
294 batadv_tt_local_entry_free_ref(tt_local_entry
);
298 batadv_tt_local_event(bat_priv
, addr
, tt_local_entry
->common
.flags
);
300 /* remove address from global hash if present */
301 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
303 /* Check whether it is a roaming! */
304 if (tt_global_entry
) {
305 /* These node are probably going to update their tt table */
306 head
= &tt_global_entry
->orig_list
;
308 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
309 orig_entry
->orig_node
->tt_poss_change
= true;
311 batadv_send_roam_adv(bat_priv
,
312 tt_global_entry
->common
.addr
,
313 orig_entry
->orig_node
);
316 /* The global entry has to be marked as ROAMING and
317 * has to be kept for consistency purpose
319 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
320 tt_global_entry
->roam_at
= jiffies
;
324 batadv_tt_local_entry_free_ref(tt_local_entry
);
326 batadv_tt_global_entry_free_ref(tt_global_entry
);
329 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff
,
330 int *packet_buff_len
,
334 unsigned char *new_buff
;
336 new_buff
= kmalloc(new_packet_len
, GFP_ATOMIC
);
338 /* keep old buffer if kmalloc should fail */
340 memcpy(new_buff
, *packet_buff
, min_packet_len
);
342 *packet_buff
= new_buff
;
343 *packet_buff_len
= new_packet_len
;
347 static void batadv_tt_prepare_packet_buff(struct batadv_priv
*bat_priv
,
348 unsigned char **packet_buff
,
349 int *packet_buff_len
,
352 struct batadv_hard_iface
*primary_if
;
355 primary_if
= batadv_primary_if_get_selected(bat_priv
);
357 req_len
= min_packet_len
;
358 req_len
+= batadv_tt_len(atomic_read(&bat_priv
->tt
.local_changes
));
360 /* if we have too many changes for one packet don't send any
361 * and wait for the tt table request which will be fragmented
363 if ((!primary_if
) || (req_len
> primary_if
->soft_iface
->mtu
))
364 req_len
= min_packet_len
;
366 batadv_tt_realloc_packet_buff(packet_buff
, packet_buff_len
,
367 min_packet_len
, req_len
);
370 batadv_hardif_free_ref(primary_if
);
373 static int batadv_tt_changes_fill_buff(struct batadv_priv
*bat_priv
,
374 unsigned char **packet_buff
,
375 int *packet_buff_len
,
378 struct batadv_tt_change_node
*entry
, *safe
;
379 int count
= 0, tot_changes
= 0, new_len
;
380 unsigned char *tt_buff
;
382 batadv_tt_prepare_packet_buff(bat_priv
, packet_buff
,
383 packet_buff_len
, min_packet_len
);
385 new_len
= *packet_buff_len
- min_packet_len
;
386 tt_buff
= *packet_buff
+ min_packet_len
;
389 tot_changes
= new_len
/ batadv_tt_len(1);
391 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
392 atomic_set(&bat_priv
->tt
.local_changes
, 0);
394 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
396 if (count
< tot_changes
) {
397 memcpy(tt_buff
+ batadv_tt_len(count
),
398 &entry
->change
, sizeof(struct batadv_tt_change
));
401 list_del(&entry
->list
);
404 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
406 /* Keep the buffer for possible tt_request */
407 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
408 kfree(bat_priv
->tt
.last_changeset
);
409 bat_priv
->tt
.last_changeset_len
= 0;
410 bat_priv
->tt
.last_changeset
= NULL
;
411 /* check whether this new OGM has no changes due to size problems */
413 /* if kmalloc() fails we will reply with the full table
414 * instead of providing the diff
416 bat_priv
->tt
.last_changeset
= kmalloc(new_len
, GFP_ATOMIC
);
417 if (bat_priv
->tt
.last_changeset
) {
418 memcpy(bat_priv
->tt
.last_changeset
, tt_buff
, new_len
);
419 bat_priv
->tt
.last_changeset_len
= new_len
;
422 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
427 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
429 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
430 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
431 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
432 struct batadv_tt_common_entry
*tt_common_entry
;
433 struct batadv_hard_iface
*primary_if
;
434 struct hlist_node
*node
;
435 struct hlist_head
*head
;
438 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
443 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
444 net_dev
->name
, (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
446 for (i
= 0; i
< hash
->size
; i
++) {
447 head
= &hash
->table
[i
];
450 hlist_for_each_entry_rcu(tt_common_entry
, node
,
452 seq_printf(seq
, " * %pM [%c%c%c%c%c]\n",
453 tt_common_entry
->addr
,
454 (tt_common_entry
->flags
&
455 BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
456 (tt_common_entry
->flags
&
457 BATADV_TT_CLIENT_NOPURGE
? 'P' : '.'),
458 (tt_common_entry
->flags
&
459 BATADV_TT_CLIENT_NEW
? 'N' : '.'),
460 (tt_common_entry
->flags
&
461 BATADV_TT_CLIENT_PENDING
? 'X' : '.'),
462 (tt_common_entry
->flags
&
463 BATADV_TT_CLIENT_WIFI
? 'W' : '.'));
469 batadv_hardif_free_ref(primary_if
);
474 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
475 struct batadv_tt_local_entry
*tt_local_entry
,
476 uint16_t flags
, const char *message
)
478 batadv_tt_local_event(bat_priv
, tt_local_entry
->common
.addr
,
479 tt_local_entry
->common
.flags
| flags
);
481 /* The local client has to be marked as "pending to be removed" but has
482 * to be kept in the table in order to send it in a full table
483 * response issued before the net ttvn increment (consistency check)
485 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
487 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
488 "Local tt entry (%pM) pending to be removed: %s\n",
489 tt_local_entry
->common
.addr
, message
);
492 void batadv_tt_local_remove(struct batadv_priv
*bat_priv
, const uint8_t *addr
,
493 const char *message
, bool roaming
)
495 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
498 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
502 flags
= BATADV_TT_CLIENT_DEL
;
504 flags
|= BATADV_TT_CLIENT_ROAM
;
506 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
, message
);
509 batadv_tt_local_entry_free_ref(tt_local_entry
);
512 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
513 struct hlist_head
*head
)
515 struct batadv_tt_local_entry
*tt_local_entry
;
516 struct batadv_tt_common_entry
*tt_common_entry
;
517 struct hlist_node
*node
, *node_tmp
;
519 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
, head
,
521 tt_local_entry
= container_of(tt_common_entry
,
522 struct batadv_tt_local_entry
,
524 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
527 /* entry already marked for deletion */
528 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
531 if (!batadv_has_timed_out(tt_local_entry
->last_seen
,
532 BATADV_TT_LOCAL_TIMEOUT
))
535 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
536 BATADV_TT_CLIENT_DEL
, "timed out");
540 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
)
542 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
543 struct hlist_head
*head
;
544 spinlock_t
*list_lock
; /* protects write access to the hash lists */
547 for (i
= 0; i
< hash
->size
; i
++) {
548 head
= &hash
->table
[i
];
549 list_lock
= &hash
->list_locks
[i
];
551 spin_lock_bh(list_lock
);
552 batadv_tt_local_purge_list(bat_priv
, head
);
553 spin_unlock_bh(list_lock
);
558 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
560 struct batadv_hashtable
*hash
;
561 spinlock_t
*list_lock
; /* protects write access to the hash lists */
562 struct batadv_tt_common_entry
*tt_common_entry
;
563 struct batadv_tt_local_entry
*tt_local
;
564 struct hlist_node
*node
, *node_tmp
;
565 struct hlist_head
*head
;
568 if (!bat_priv
->tt
.local_hash
)
571 hash
= bat_priv
->tt
.local_hash
;
573 for (i
= 0; i
< hash
->size
; i
++) {
574 head
= &hash
->table
[i
];
575 list_lock
= &hash
->list_locks
[i
];
577 spin_lock_bh(list_lock
);
578 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
,
581 tt_local
= container_of(tt_common_entry
,
582 struct batadv_tt_local_entry
,
584 batadv_tt_local_entry_free_ref(tt_local
);
586 spin_unlock_bh(list_lock
);
589 batadv_hash_destroy(hash
);
591 bat_priv
->tt
.local_hash
= NULL
;
594 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
596 if (bat_priv
->tt
.global_hash
)
599 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
601 if (!bat_priv
->tt
.global_hash
)
607 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
609 struct batadv_tt_change_node
*entry
, *safe
;
611 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
613 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
615 list_del(&entry
->list
);
619 atomic_set(&bat_priv
->tt
.local_changes
, 0);
620 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
623 /* retrieves the orig_tt_list_entry belonging to orig_node from the
624 * batadv_tt_global_entry list
626 * returns it with an increased refcounter, NULL if not found
628 static struct batadv_tt_orig_list_entry
*
629 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
630 const struct batadv_orig_node
*orig_node
)
632 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
633 const struct hlist_head
*head
;
634 struct hlist_node
*node
;
637 head
= &entry
->orig_list
;
638 hlist_for_each_entry_rcu(tmp_orig_entry
, node
, head
, list
) {
639 if (tmp_orig_entry
->orig_node
!= orig_node
)
641 if (!atomic_inc_not_zero(&tmp_orig_entry
->refcount
))
644 orig_entry
= tmp_orig_entry
;
652 /* find out if an orig_node is already in the list of a tt_global_entry.
653 * returns true if found, false otherwise
656 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
657 const struct batadv_orig_node
*orig_node
)
659 struct batadv_tt_orig_list_entry
*orig_entry
;
662 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
665 batadv_tt_orig_list_entry_free_ref(orig_entry
);
672 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
673 struct batadv_orig_node
*orig_node
, int ttvn
)
675 struct batadv_tt_orig_list_entry
*orig_entry
;
677 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
679 /* refresh the ttvn: the current value could be a bogus one that
680 * was added during a "temporary client detection"
682 orig_entry
->ttvn
= ttvn
;
686 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
690 INIT_HLIST_NODE(&orig_entry
->list
);
691 atomic_inc(&orig_node
->refcount
);
692 atomic_inc(&orig_node
->tt_size
);
693 orig_entry
->orig_node
= orig_node
;
694 orig_entry
->ttvn
= ttvn
;
695 atomic_set(&orig_entry
->refcount
, 2);
697 spin_lock_bh(&tt_global
->list_lock
);
698 hlist_add_head_rcu(&orig_entry
->list
,
699 &tt_global
->orig_list
);
700 spin_unlock_bh(&tt_global
->list_lock
);
703 batadv_tt_orig_list_entry_free_ref(orig_entry
);
706 /* caller must hold orig_node refcount */
707 int batadv_tt_global_add(struct batadv_priv
*bat_priv
,
708 struct batadv_orig_node
*orig_node
,
709 const unsigned char *tt_addr
, uint8_t flags
,
712 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
715 struct batadv_tt_common_entry
*common
;
717 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
);
719 if (!tt_global_entry
) {
720 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
721 if (!tt_global_entry
)
724 common
= &tt_global_entry
->common
;
725 memcpy(common
->addr
, tt_addr
, ETH_ALEN
);
727 common
->flags
= flags
;
728 tt_global_entry
->roam_at
= 0;
729 /* node must store current time in case of roaming. This is
730 * needed to purge this entry out on timeout (if nobody claims
733 if (flags
& BATADV_TT_CLIENT_ROAM
)
734 tt_global_entry
->roam_at
= jiffies
;
735 atomic_set(&common
->refcount
, 2);
736 common
->added_at
= jiffies
;
738 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
739 spin_lock_init(&tt_global_entry
->list_lock
);
741 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
743 batadv_choose_orig
, common
,
744 &common
->hash_entry
);
746 if (unlikely(hash_added
!= 0)) {
747 /* remove the reference for the hash */
748 batadv_tt_global_entry_free_ref(tt_global_entry
);
752 /* If there is already a global entry, we can use this one for
754 * But if we are trying to add a temporary client we can exit
755 * directly because the temporary information should never
756 * override any already known client state (whatever it is)
758 if (flags
& BATADV_TT_CLIENT_TEMP
)
761 /* if the client was temporary added before receiving the first
762 * OGM announcing it, we have to clear the TEMP flag
764 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_TEMP
;
766 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
767 * one originator left in the list and we previously received a
768 * delete + roaming change for this originator.
770 * We should first delete the old originator before adding the
773 if (tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
774 batadv_tt_global_del_orig_list(tt_global_entry
);
775 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
776 tt_global_entry
->roam_at
= 0;
779 /* add the new orig_entry (if needed) or update it */
780 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
782 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
783 "Creating new global tt entry: %pM (via %pM)\n",
784 tt_global_entry
->common
.addr
, orig_node
->orig
);
788 /* remove address from local hash if present */
789 batadv_tt_local_remove(bat_priv
, tt_global_entry
->common
.addr
,
790 "global tt received",
791 flags
& BATADV_TT_CLIENT_ROAM
);
794 batadv_tt_global_entry_free_ref(tt_global_entry
);
798 /* print all orig nodes who announce the address for this global entry.
799 * it is assumed that the caller holds rcu_read_lock();
802 batadv_tt_global_print_entry(struct batadv_tt_global_entry
*tt_global_entry
,
803 struct seq_file
*seq
)
805 struct hlist_head
*head
;
806 struct hlist_node
*node
;
807 struct batadv_tt_orig_list_entry
*orig_entry
;
808 struct batadv_tt_common_entry
*tt_common_entry
;
812 tt_common_entry
= &tt_global_entry
->common
;
814 head
= &tt_global_entry
->orig_list
;
816 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
817 flags
= tt_common_entry
->flags
;
818 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
819 seq_printf(seq
, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
820 tt_global_entry
->common
.addr
, orig_entry
->ttvn
,
821 orig_entry
->orig_node
->orig
, last_ttvn
,
822 (flags
& BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
823 (flags
& BATADV_TT_CLIENT_WIFI
? 'W' : '.'),
824 (flags
& BATADV_TT_CLIENT_TEMP
? 'T' : '.'));
828 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
830 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
831 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
832 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
833 struct batadv_tt_common_entry
*tt_common_entry
;
834 struct batadv_tt_global_entry
*tt_global
;
835 struct batadv_hard_iface
*primary_if
;
836 struct hlist_node
*node
;
837 struct hlist_head
*head
;
840 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
845 "Globally announced TT entries received via the mesh %s\n",
847 seq_printf(seq
, " %-13s %s %-15s %s %s\n",
848 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
850 for (i
= 0; i
< hash
->size
; i
++) {
851 head
= &hash
->table
[i
];
854 hlist_for_each_entry_rcu(tt_common_entry
, node
,
856 tt_global
= container_of(tt_common_entry
,
857 struct batadv_tt_global_entry
,
859 batadv_tt_global_print_entry(tt_global
, seq
);
865 batadv_hardif_free_ref(primary_if
);
869 /* deletes the orig list of a tt_global_entry */
871 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
873 struct hlist_head
*head
;
874 struct hlist_node
*node
, *safe
;
875 struct batadv_tt_orig_list_entry
*orig_entry
;
877 spin_lock_bh(&tt_global_entry
->list_lock
);
878 head
= &tt_global_entry
->orig_list
;
879 hlist_for_each_entry_safe(orig_entry
, node
, safe
, head
, list
) {
881 batadv_tt_orig_list_entry_free_ref(orig_entry
);
883 spin_unlock_bh(&tt_global_entry
->list_lock
);
888 batadv_tt_global_del_orig_entry(struct batadv_priv
*bat_priv
,
889 struct batadv_tt_global_entry
*tt_global_entry
,
890 struct batadv_orig_node
*orig_node
,
893 struct hlist_head
*head
;
894 struct hlist_node
*node
, *safe
;
895 struct batadv_tt_orig_list_entry
*orig_entry
;
897 spin_lock_bh(&tt_global_entry
->list_lock
);
898 head
= &tt_global_entry
->orig_list
;
899 hlist_for_each_entry_safe(orig_entry
, node
, safe
, head
, list
) {
900 if (orig_entry
->orig_node
== orig_node
) {
901 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
902 "Deleting %pM from global tt entry %pM: %s\n",
904 tt_global_entry
->common
.addr
, message
);
906 batadv_tt_orig_list_entry_free_ref(orig_entry
);
909 spin_unlock_bh(&tt_global_entry
->list_lock
);
913 batadv_tt_global_del_struct(struct batadv_priv
*bat_priv
,
914 struct batadv_tt_global_entry
*tt_global_entry
,
917 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
918 "Deleting global tt entry %pM: %s\n",
919 tt_global_entry
->common
.addr
, message
);
921 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
922 batadv_choose_orig
, tt_global_entry
->common
.addr
);
923 batadv_tt_global_entry_free_ref(tt_global_entry
);
927 /* If the client is to be deleted, we check if it is the last origantor entry
928 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
929 * timer, otherwise we simply remove the originator scheduled for deletion.
932 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
933 struct batadv_tt_global_entry
*tt_global_entry
,
934 struct batadv_orig_node
*orig_node
,
937 bool last_entry
= true;
938 struct hlist_head
*head
;
939 struct hlist_node
*node
;
940 struct batadv_tt_orig_list_entry
*orig_entry
;
942 /* no local entry exists, case 1:
943 * Check if this is the last one or if other entries exist.
947 head
= &tt_global_entry
->orig_list
;
948 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
949 if (orig_entry
->orig_node
!= orig_node
) {
957 /* its the last one, mark for roaming. */
958 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
959 tt_global_entry
->roam_at
= jiffies
;
961 /* there is another entry, we can simply delete this
962 * one and can still use the other one.
964 batadv_tt_global_del_orig_entry(bat_priv
, tt_global_entry
,
970 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
971 struct batadv_orig_node
*orig_node
,
972 const unsigned char *addr
,
973 const char *message
, bool roaming
)
975 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
976 struct batadv_tt_local_entry
*local_entry
= NULL
;
978 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
979 if (!tt_global_entry
)
983 batadv_tt_global_del_orig_entry(bat_priv
, tt_global_entry
,
986 if (hlist_empty(&tt_global_entry
->orig_list
))
987 batadv_tt_global_del_struct(bat_priv
, tt_global_entry
,
993 /* if we are deleting a global entry due to a roam
994 * event, there are two possibilities:
995 * 1) the client roamed from node A to node B => if there
996 * is only one originator left for this client, we mark
997 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
998 * wait for node B to claim it. In case of timeout
999 * the entry is purged.
1001 * If there are other originators left, we directly delete
1003 * 2) the client roamed to us => we can directly delete
1004 * the global entry, since it is useless now.
1006 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1007 tt_global_entry
->common
.addr
);
1009 /* local entry exists, case 2: client roamed to us. */
1010 batadv_tt_global_del_orig_list(tt_global_entry
);
1011 batadv_tt_global_del_struct(bat_priv
, tt_global_entry
, message
);
1013 /* no local entry exists, case 1: check for roaming */
1014 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1015 orig_node
, message
);
1019 if (tt_global_entry
)
1020 batadv_tt_global_entry_free_ref(tt_global_entry
);
1022 batadv_tt_local_entry_free_ref(local_entry
);
1025 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1026 struct batadv_orig_node
*orig_node
,
1027 const char *message
)
1029 struct batadv_tt_global_entry
*tt_global
;
1030 struct batadv_tt_common_entry
*tt_common_entry
;
1032 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1033 struct hlist_node
*node
, *safe
;
1034 struct hlist_head
*head
;
1035 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1040 for (i
= 0; i
< hash
->size
; i
++) {
1041 head
= &hash
->table
[i
];
1042 list_lock
= &hash
->list_locks
[i
];
1044 spin_lock_bh(list_lock
);
1045 hlist_for_each_entry_safe(tt_common_entry
, node
, safe
,
1047 tt_global
= container_of(tt_common_entry
,
1048 struct batadv_tt_global_entry
,
1051 batadv_tt_global_del_orig_entry(bat_priv
, tt_global
,
1052 orig_node
, message
);
1054 if (hlist_empty(&tt_global
->orig_list
)) {
1055 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1056 "Deleting global tt entry %pM: %s\n",
1057 tt_global
->common
.addr
, message
);
1058 hlist_del_rcu(node
);
1059 batadv_tt_global_entry_free_ref(tt_global
);
1062 spin_unlock_bh(list_lock
);
1064 orig_node
->tt_initialised
= false;
1067 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1071 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1072 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1074 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1075 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1077 *msg
= "Roaming timeout\n";
1080 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1081 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1083 *msg
= "Temporary client timeout\n";
1089 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1091 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1092 struct hlist_head
*head
;
1093 struct hlist_node
*node
, *node_tmp
;
1094 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1097 struct batadv_tt_common_entry
*tt_common
;
1098 struct batadv_tt_global_entry
*tt_global
;
1100 for (i
= 0; i
< hash
->size
; i
++) {
1101 head
= &hash
->table
[i
];
1102 list_lock
= &hash
->list_locks
[i
];
1104 spin_lock_bh(list_lock
);
1105 hlist_for_each_entry_safe(tt_common
, node
, node_tmp
, head
,
1107 tt_global
= container_of(tt_common
,
1108 struct batadv_tt_global_entry
,
1111 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1114 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1115 "Deleting global tt entry (%pM): %s\n",
1116 tt_global
->common
.addr
, msg
);
1118 hlist_del_rcu(node
);
1120 batadv_tt_global_entry_free_ref(tt_global
);
1122 spin_unlock_bh(list_lock
);
1126 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
1128 struct batadv_hashtable
*hash
;
1129 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1130 struct batadv_tt_common_entry
*tt_common_entry
;
1131 struct batadv_tt_global_entry
*tt_global
;
1132 struct hlist_node
*node
, *node_tmp
;
1133 struct hlist_head
*head
;
1136 if (!bat_priv
->tt
.global_hash
)
1139 hash
= bat_priv
->tt
.global_hash
;
1141 for (i
= 0; i
< hash
->size
; i
++) {
1142 head
= &hash
->table
[i
];
1143 list_lock
= &hash
->list_locks
[i
];
1145 spin_lock_bh(list_lock
);
1146 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
,
1148 hlist_del_rcu(node
);
1149 tt_global
= container_of(tt_common_entry
,
1150 struct batadv_tt_global_entry
,
1152 batadv_tt_global_entry_free_ref(tt_global
);
1154 spin_unlock_bh(list_lock
);
1157 batadv_hash_destroy(hash
);
1159 bat_priv
->tt
.global_hash
= NULL
;
1163 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
1164 struct batadv_tt_global_entry
*tt_global_entry
)
1168 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
1169 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
1175 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
1177 const uint8_t *addr
)
1179 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
1180 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
1181 struct batadv_orig_node
*orig_node
= NULL
;
1182 struct batadv_neigh_node
*router
= NULL
;
1183 struct hlist_head
*head
;
1184 struct hlist_node
*node
;
1185 struct batadv_tt_orig_list_entry
*orig_entry
;
1188 if (src
&& atomic_read(&bat_priv
->ap_isolation
)) {
1189 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
);
1190 if (!tt_local_entry
)
1194 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
1195 if (!tt_global_entry
)
1198 /* check whether the clients should not communicate due to AP
1201 if (tt_local_entry
&&
1202 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
1208 head
= &tt_global_entry
->orig_list
;
1209 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
1210 router
= batadv_orig_node_get_router(orig_entry
->orig_node
);
1214 if (router
->tq_avg
> best_tq
) {
1215 orig_node
= orig_entry
->orig_node
;
1216 best_tq
= router
->tq_avg
;
1218 batadv_neigh_node_free_ref(router
);
1220 /* found anything? */
1221 if (orig_node
&& !atomic_inc_not_zero(&orig_node
->refcount
))
1225 if (tt_global_entry
)
1226 batadv_tt_global_entry_free_ref(tt_global_entry
);
1228 batadv_tt_local_entry_free_ref(tt_local_entry
);
1233 /* Calculates the checksum of the local table of a given orig_node */
1234 static uint16_t batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
1235 struct batadv_orig_node
*orig_node
)
1237 uint16_t total
= 0, total_one
;
1238 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1239 struct batadv_tt_common_entry
*tt_common
;
1240 struct batadv_tt_global_entry
*tt_global
;
1241 struct hlist_node
*node
;
1242 struct hlist_head
*head
;
1246 for (i
= 0; i
< hash
->size
; i
++) {
1247 head
= &hash
->table
[i
];
1250 hlist_for_each_entry_rcu(tt_common
, node
, head
, hash_entry
) {
1251 tt_global
= container_of(tt_common
,
1252 struct batadv_tt_global_entry
,
1254 /* Roaming clients are in the global table for
1255 * consistency only. They don't have to be
1256 * taken into account while computing the
1259 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
1261 /* Temporary clients have not been announced yet, so
1262 * they have to be skipped while computing the global
1265 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
1268 /* find out if this global entry is announced by this
1271 if (!batadv_tt_global_entry_has_orig(tt_global
,
1276 for (j
= 0; j
< ETH_ALEN
; j
++)
1277 total_one
= crc16_byte(total_one
,
1278 tt_common
->addr
[j
]);
1287 /* Calculates the checksum of the local table */
1288 static uint16_t batadv_tt_local_crc(struct batadv_priv
*bat_priv
)
1290 uint16_t total
= 0, total_one
;
1291 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1292 struct batadv_tt_common_entry
*tt_common
;
1293 struct hlist_node
*node
;
1294 struct hlist_head
*head
;
1298 for (i
= 0; i
< hash
->size
; i
++) {
1299 head
= &hash
->table
[i
];
1302 hlist_for_each_entry_rcu(tt_common
, node
, head
, hash_entry
) {
1303 /* not yet committed clients have not to be taken into
1304 * account while computing the CRC
1306 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
1309 for (j
= 0; j
< ETH_ALEN
; j
++)
1310 total_one
= crc16_byte(total_one
,
1311 tt_common
->addr
[j
]);
1320 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
1322 struct batadv_tt_req_node
*node
, *safe
;
1324 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1326 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
1327 list_del(&node
->list
);
1331 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1334 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
1335 struct batadv_orig_node
*orig_node
,
1336 const unsigned char *tt_buff
,
1337 uint8_t tt_num_changes
)
1339 uint16_t tt_buff_len
= batadv_tt_len(tt_num_changes
);
1341 /* Replace the old buffer only if I received something in the
1342 * last OGM (the OGM could carry no changes)
1344 spin_lock_bh(&orig_node
->tt_buff_lock
);
1345 if (tt_buff_len
> 0) {
1346 kfree(orig_node
->tt_buff
);
1347 orig_node
->tt_buff_len
= 0;
1348 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
1349 if (orig_node
->tt_buff
) {
1350 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
1351 orig_node
->tt_buff_len
= tt_buff_len
;
1354 spin_unlock_bh(&orig_node
->tt_buff_lock
);
1357 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
1359 struct batadv_tt_req_node
*node
, *safe
;
1361 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1362 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
1363 if (batadv_has_timed_out(node
->issued_at
,
1364 BATADV_TT_REQUEST_TIMEOUT
)) {
1365 list_del(&node
->list
);
1369 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1372 /* returns the pointer to the new tt_req_node struct if no request
1373 * has already been issued for this orig_node, NULL otherwise
1375 static struct batadv_tt_req_node
*
1376 batadv_new_tt_req_node(struct batadv_priv
*bat_priv
,
1377 struct batadv_orig_node
*orig_node
)
1379 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
1381 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1382 list_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
1383 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
1384 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
1385 BATADV_TT_REQUEST_TIMEOUT
))
1389 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
1393 memcpy(tt_req_node
->addr
, orig_node
->orig
, ETH_ALEN
);
1394 tt_req_node
->issued_at
= jiffies
;
1396 list_add(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
1398 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1402 /* data_ptr is useless here, but has to be kept to respect the prototype */
1403 static int batadv_tt_local_valid_entry(const void *entry_ptr
,
1404 const void *data_ptr
)
1406 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
1408 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
1413 static int batadv_tt_global_valid(const void *entry_ptr
,
1414 const void *data_ptr
)
1416 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
1417 const struct batadv_tt_global_entry
*tt_global_entry
;
1418 const struct batadv_orig_node
*orig_node
= data_ptr
;
1420 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
1421 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
1424 tt_global_entry
= container_of(tt_common_entry
,
1425 struct batadv_tt_global_entry
,
1428 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
1431 static struct sk_buff
*
1432 batadv_tt_response_fill_table(uint16_t tt_len
, uint8_t ttvn
,
1433 struct batadv_hashtable
*hash
,
1434 struct batadv_hard_iface
*primary_if
,
1435 int (*valid_cb
)(const void *, const void *),
1438 struct batadv_tt_common_entry
*tt_common_entry
;
1439 struct batadv_tt_query_packet
*tt_response
;
1440 struct batadv_tt_change
*tt_change
;
1441 struct hlist_node
*node
;
1442 struct hlist_head
*head
;
1443 struct sk_buff
*skb
= NULL
;
1444 uint16_t tt_tot
, tt_count
;
1445 ssize_t tt_query_size
= sizeof(struct batadv_tt_query_packet
);
1449 if (tt_query_size
+ tt_len
> primary_if
->soft_iface
->mtu
) {
1450 tt_len
= primary_if
->soft_iface
->mtu
- tt_query_size
;
1451 tt_len
-= tt_len
% sizeof(struct batadv_tt_change
);
1453 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1455 len
= tt_query_size
+ tt_len
;
1456 skb
= dev_alloc_skb(len
+ ETH_HLEN
);
1460 skb_reserve(skb
, ETH_HLEN
);
1461 tt_response
= (struct batadv_tt_query_packet
*)skb_put(skb
, len
);
1462 tt_response
->ttvn
= ttvn
;
1464 tt_change
= (struct batadv_tt_change
*)(skb
->data
+ tt_query_size
);
1468 for (i
= 0; i
< hash
->size
; i
++) {
1469 head
= &hash
->table
[i
];
1471 hlist_for_each_entry_rcu(tt_common_entry
, node
,
1473 if (tt_count
== tt_tot
)
1476 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
1479 memcpy(tt_change
->addr
, tt_common_entry
->addr
,
1481 tt_change
->flags
= BATADV_NO_FLAGS
;
1489 /* store in the message the number of entries we have successfully
1492 tt_response
->tt_data
= htons(tt_count
);
1498 static int batadv_send_tt_request(struct batadv_priv
*bat_priv
,
1499 struct batadv_orig_node
*dst_orig_node
,
1500 uint8_t ttvn
, uint16_t tt_crc
,
1503 struct sk_buff
*skb
= NULL
;
1504 struct batadv_tt_query_packet
*tt_request
;
1505 struct batadv_neigh_node
*neigh_node
= NULL
;
1506 struct batadv_hard_iface
*primary_if
;
1507 struct batadv_tt_req_node
*tt_req_node
= NULL
;
1511 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1515 /* The new tt_req will be issued only if I'm not waiting for a
1516 * reply from the same orig_node yet
1518 tt_req_node
= batadv_new_tt_req_node(bat_priv
, dst_orig_node
);
1522 skb
= dev_alloc_skb(sizeof(*tt_request
) + ETH_HLEN
);
1526 skb_reserve(skb
, ETH_HLEN
);
1528 tt_req_len
= sizeof(*tt_request
);
1529 tt_request
= (struct batadv_tt_query_packet
*)skb_put(skb
, tt_req_len
);
1531 tt_request
->header
.packet_type
= BATADV_TT_QUERY
;
1532 tt_request
->header
.version
= BATADV_COMPAT_VERSION
;
1533 memcpy(tt_request
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1534 memcpy(tt_request
->dst
, dst_orig_node
->orig
, ETH_ALEN
);
1535 tt_request
->header
.ttl
= BATADV_TTL
;
1536 tt_request
->ttvn
= ttvn
;
1537 tt_request
->tt_data
= htons(tt_crc
);
1538 tt_request
->flags
= BATADV_TT_REQUEST
;
1541 tt_request
->flags
|= BATADV_TT_FULL_TABLE
;
1543 neigh_node
= batadv_orig_node_get_router(dst_orig_node
);
1547 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1548 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1549 dst_orig_node
->orig
, neigh_node
->addr
,
1550 (full_table
? 'F' : '.'));
1552 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
1554 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1559 batadv_neigh_node_free_ref(neigh_node
);
1561 batadv_hardif_free_ref(primary_if
);
1564 if (ret
&& tt_req_node
) {
1565 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1566 list_del(&tt_req_node
->list
);
1567 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1574 batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
1575 struct batadv_tt_query_packet
*tt_request
)
1577 struct batadv_orig_node
*req_dst_orig_node
= NULL
;
1578 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
1579 struct batadv_neigh_node
*neigh_node
= NULL
;
1580 struct batadv_hard_iface
*primary_if
= NULL
;
1581 uint8_t orig_ttvn
, req_ttvn
, ttvn
;
1583 unsigned char *tt_buff
;
1585 uint16_t tt_len
, tt_tot
;
1586 struct sk_buff
*skb
= NULL
;
1587 struct batadv_tt_query_packet
*tt_response
;
1588 uint8_t *packet_pos
;
1591 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1592 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1593 tt_request
->src
, tt_request
->ttvn
, tt_request
->dst
,
1594 (tt_request
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1596 /* Let's get the orig node of the REAL destination */
1597 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->dst
);
1598 if (!req_dst_orig_node
)
1601 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->src
);
1602 if (!res_dst_orig_node
)
1605 neigh_node
= batadv_orig_node_get_router(res_dst_orig_node
);
1609 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1613 orig_ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1614 req_ttvn
= tt_request
->ttvn
;
1616 /* I don't have the requested data */
1617 if (orig_ttvn
!= req_ttvn
||
1618 tt_request
->tt_data
!= htons(req_dst_orig_node
->tt_crc
))
1621 /* If the full table has been explicitly requested */
1622 if (tt_request
->flags
& BATADV_TT_FULL_TABLE
||
1623 !req_dst_orig_node
->tt_buff
)
1628 /* In this version, fragmentation is not implemented, then
1629 * I'll send only one packet with as much TT entries as I can
1632 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
1633 tt_len
= req_dst_orig_node
->tt_buff_len
;
1634 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1636 len
= sizeof(*tt_response
) + tt_len
;
1637 skb
= dev_alloc_skb(len
+ ETH_HLEN
);
1641 skb_reserve(skb
, ETH_HLEN
);
1642 packet_pos
= skb_put(skb
, len
);
1643 tt_response
= (struct batadv_tt_query_packet
*)packet_pos
;
1644 tt_response
->ttvn
= req_ttvn
;
1645 tt_response
->tt_data
= htons(tt_tot
);
1647 tt_buff
= skb
->data
+ sizeof(*tt_response
);
1648 /* Copy the last orig_node's OGM buffer */
1649 memcpy(tt_buff
, req_dst_orig_node
->tt_buff
,
1650 req_dst_orig_node
->tt_buff_len
);
1652 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1654 tt_len
= (uint16_t)atomic_read(&req_dst_orig_node
->tt_size
);
1655 tt_len
*= sizeof(struct batadv_tt_change
);
1656 ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1658 skb
= batadv_tt_response_fill_table(tt_len
, ttvn
,
1659 bat_priv
->tt
.global_hash
,
1661 batadv_tt_global_valid
,
1666 tt_response
= (struct batadv_tt_query_packet
*)skb
->data
;
1669 tt_response
->header
.packet_type
= BATADV_TT_QUERY
;
1670 tt_response
->header
.version
= BATADV_COMPAT_VERSION
;
1671 tt_response
->header
.ttl
= BATADV_TTL
;
1672 memcpy(tt_response
->src
, req_dst_orig_node
->orig
, ETH_ALEN
);
1673 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1674 tt_response
->flags
= BATADV_TT_RESPONSE
;
1677 tt_response
->flags
|= BATADV_TT_FULL_TABLE
;
1679 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1680 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1681 res_dst_orig_node
->orig
, neigh_node
->addr
,
1682 req_dst_orig_node
->orig
, req_ttvn
);
1684 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
1686 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1691 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1694 if (res_dst_orig_node
)
1695 batadv_orig_node_free_ref(res_dst_orig_node
);
1696 if (req_dst_orig_node
)
1697 batadv_orig_node_free_ref(req_dst_orig_node
);
1699 batadv_neigh_node_free_ref(neigh_node
);
1701 batadv_hardif_free_ref(primary_if
);
1709 batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
1710 struct batadv_tt_query_packet
*tt_request
)
1712 struct batadv_orig_node
*orig_node
= NULL
;
1713 struct batadv_neigh_node
*neigh_node
= NULL
;
1714 struct batadv_hard_iface
*primary_if
= NULL
;
1715 uint8_t my_ttvn
, req_ttvn
, ttvn
;
1717 unsigned char *tt_buff
;
1719 uint16_t tt_len
, tt_tot
;
1720 struct sk_buff
*skb
= NULL
;
1721 struct batadv_tt_query_packet
*tt_response
;
1722 uint8_t *packet_pos
;
1725 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1726 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1727 tt_request
->src
, tt_request
->ttvn
,
1728 (tt_request
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1731 my_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
1732 req_ttvn
= tt_request
->ttvn
;
1734 orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->src
);
1738 neigh_node
= batadv_orig_node_get_router(orig_node
);
1742 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1746 /* If the full table has been explicitly requested or the gap
1747 * is too big send the whole local translation table
1749 if (tt_request
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
1750 !bat_priv
->tt
.last_changeset
)
1755 /* In this version, fragmentation is not implemented, then
1756 * I'll send only one packet with as much TT entries as I can
1759 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
1760 tt_len
= bat_priv
->tt
.last_changeset_len
;
1761 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1763 len
= sizeof(*tt_response
) + tt_len
;
1764 skb
= dev_alloc_skb(len
+ ETH_HLEN
);
1768 skb_reserve(skb
, ETH_HLEN
);
1769 packet_pos
= skb_put(skb
, len
);
1770 tt_response
= (struct batadv_tt_query_packet
*)packet_pos
;
1771 tt_response
->ttvn
= req_ttvn
;
1772 tt_response
->tt_data
= htons(tt_tot
);
1774 tt_buff
= skb
->data
+ sizeof(*tt_response
);
1775 memcpy(tt_buff
, bat_priv
->tt
.last_changeset
,
1776 bat_priv
->tt
.last_changeset_len
);
1777 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
1779 tt_len
= (uint16_t)atomic_read(&bat_priv
->tt
.local_entry_num
);
1780 tt_len
*= sizeof(struct batadv_tt_change
);
1781 ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
1783 skb
= batadv_tt_response_fill_table(tt_len
, ttvn
,
1784 bat_priv
->tt
.local_hash
,
1786 batadv_tt_local_valid_entry
,
1791 tt_response
= (struct batadv_tt_query_packet
*)skb
->data
;
1794 tt_response
->header
.packet_type
= BATADV_TT_QUERY
;
1795 tt_response
->header
.version
= BATADV_COMPAT_VERSION
;
1796 tt_response
->header
.ttl
= BATADV_TTL
;
1797 memcpy(tt_response
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1798 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1799 tt_response
->flags
= BATADV_TT_RESPONSE
;
1802 tt_response
->flags
|= BATADV_TT_FULL_TABLE
;
1804 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1805 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1806 orig_node
->orig
, neigh_node
->addr
,
1807 (tt_response
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1809 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
1811 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1816 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
1819 batadv_orig_node_free_ref(orig_node
);
1821 batadv_neigh_node_free_ref(neigh_node
);
1823 batadv_hardif_free_ref(primary_if
);
1826 /* This packet was for me, so it doesn't need to be re-routed */
1830 bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
1831 struct batadv_tt_query_packet
*tt_request
)
1833 if (batadv_is_my_mac(tt_request
->dst
)) {
1834 /* don't answer backbone gws! */
1835 if (batadv_bla_is_backbone_gw_orig(bat_priv
, tt_request
->src
))
1838 return batadv_send_my_tt_response(bat_priv
, tt_request
);
1840 return batadv_send_other_tt_response(bat_priv
, tt_request
);
1844 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
1845 struct batadv_orig_node
*orig_node
,
1846 struct batadv_tt_change
*tt_change
,
1847 uint16_t tt_num_changes
, uint8_t ttvn
)
1852 for (i
= 0; i
< tt_num_changes
; i
++) {
1853 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
1854 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
1855 batadv_tt_global_del(bat_priv
, orig_node
,
1856 (tt_change
+ i
)->addr
,
1857 "tt removed by changes",
1860 if (!batadv_tt_global_add(bat_priv
, orig_node
,
1861 (tt_change
+ i
)->addr
,
1862 (tt_change
+ i
)->flags
, ttvn
))
1863 /* In case of problem while storing a
1864 * global_entry, we stop the updating
1865 * procedure without committing the
1866 * ttvn change. This will avoid to send
1867 * corrupted data on tt_request
1872 orig_node
->tt_initialised
= true;
1875 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
1876 struct batadv_tt_query_packet
*tt_response
)
1878 struct batadv_orig_node
*orig_node
= NULL
;
1880 orig_node
= batadv_orig_hash_find(bat_priv
, tt_response
->src
);
1884 /* Purge the old table first.. */
1885 batadv_tt_global_del_orig(bat_priv
, orig_node
, "Received full table");
1887 _batadv_tt_update_changes(bat_priv
, orig_node
,
1888 (struct batadv_tt_change
*)(tt_response
+ 1),
1889 ntohs(tt_response
->tt_data
),
1892 spin_lock_bh(&orig_node
->tt_buff_lock
);
1893 kfree(orig_node
->tt_buff
);
1894 orig_node
->tt_buff_len
= 0;
1895 orig_node
->tt_buff
= NULL
;
1896 spin_unlock_bh(&orig_node
->tt_buff_lock
);
1898 atomic_set(&orig_node
->last_ttvn
, tt_response
->ttvn
);
1902 batadv_orig_node_free_ref(orig_node
);
1905 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
1906 struct batadv_orig_node
*orig_node
,
1907 uint16_t tt_num_changes
, uint8_t ttvn
,
1908 struct batadv_tt_change
*tt_change
)
1910 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
1911 tt_num_changes
, ttvn
);
1913 batadv_tt_save_orig_buffer(bat_priv
, orig_node
,
1914 (unsigned char *)tt_change
, tt_num_changes
);
1915 atomic_set(&orig_node
->last_ttvn
, ttvn
);
1918 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const uint8_t *addr
)
1920 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
1923 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
1924 if (!tt_local_entry
)
1926 /* Check if the client has been logically deleted (but is kept for
1927 * consistency purpose)
1929 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1934 batadv_tt_local_entry_free_ref(tt_local_entry
);
1938 void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
1939 struct batadv_tt_query_packet
*tt_response
)
1941 struct batadv_tt_req_node
*node
, *safe
;
1942 struct batadv_orig_node
*orig_node
= NULL
;
1943 struct batadv_tt_change
*tt_change
;
1945 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1946 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1947 tt_response
->src
, tt_response
->ttvn
,
1948 ntohs(tt_response
->tt_data
),
1949 (tt_response
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1951 /* we should have never asked a backbone gw */
1952 if (batadv_bla_is_backbone_gw_orig(bat_priv
, tt_response
->src
))
1955 orig_node
= batadv_orig_hash_find(bat_priv
, tt_response
->src
);
1959 if (tt_response
->flags
& BATADV_TT_FULL_TABLE
) {
1960 batadv_tt_fill_gtable(bat_priv
, tt_response
);
1962 tt_change
= (struct batadv_tt_change
*)(tt_response
+ 1);
1963 batadv_tt_update_changes(bat_priv
, orig_node
,
1964 ntohs(tt_response
->tt_data
),
1965 tt_response
->ttvn
, tt_change
);
1968 /* Delete the tt_req_node from pending tt_requests list */
1969 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1970 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
1971 if (!batadv_compare_eth(node
->addr
, tt_response
->src
))
1973 list_del(&node
->list
);
1976 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1978 /* Recalculate the CRC for this orig_node and store it */
1979 orig_node
->tt_crc
= batadv_tt_global_crc(bat_priv
, orig_node
);
1980 /* Roaming phase is over: tables are in sync again. I can
1983 orig_node
->tt_poss_change
= false;
1986 batadv_orig_node_free_ref(orig_node
);
1989 int batadv_tt_init(struct batadv_priv
*bat_priv
)
1993 ret
= batadv_tt_local_init(bat_priv
);
1997 ret
= batadv_tt_global_init(bat_priv
);
2001 batadv_tt_start_timer(bat_priv
);
2006 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
2008 struct batadv_tt_roam_node
*node
, *safe
;
2010 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2012 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2013 list_del(&node
->list
);
2017 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2020 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
2022 struct batadv_tt_roam_node
*node
, *safe
;
2024 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2025 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2026 if (!batadv_has_timed_out(node
->first_time
,
2027 BATADV_ROAMING_MAX_TIME
))
2030 list_del(&node
->list
);
2033 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2036 /* This function checks whether the client already reached the
2037 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2040 * returns true if the ROAMING_ADV can be sent, false otherwise
2042 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
,
2045 struct batadv_tt_roam_node
*tt_roam_node
;
2048 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2049 /* The new tt_req will be issued only if I'm not waiting for a
2050 * reply from the same orig_node yet
2052 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
2053 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
2056 if (batadv_has_timed_out(tt_roam_node
->first_time
,
2057 BATADV_ROAMING_MAX_TIME
))
2060 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
2061 /* Sorry, you roamed too many times! */
2068 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
2072 tt_roam_node
->first_time
= jiffies
;
2073 atomic_set(&tt_roam_node
->counter
,
2074 BATADV_ROAMING_MAX_COUNT
- 1);
2075 memcpy(tt_roam_node
->addr
, client
, ETH_ALEN
);
2077 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
2082 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2086 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
2087 struct batadv_orig_node
*orig_node
)
2089 struct batadv_neigh_node
*neigh_node
= NULL
;
2090 struct sk_buff
*skb
= NULL
;
2091 struct batadv_roam_adv_packet
*roam_adv_packet
;
2093 struct batadv_hard_iface
*primary_if
;
2094 size_t len
= sizeof(*roam_adv_packet
);
2096 /* before going on we have to check whether the client has
2097 * already roamed to us too many times
2099 if (!batadv_tt_check_roam_count(bat_priv
, client
))
2102 skb
= dev_alloc_skb(sizeof(*roam_adv_packet
) + ETH_HLEN
);
2106 skb_reserve(skb
, ETH_HLEN
);
2108 roam_adv_packet
= (struct batadv_roam_adv_packet
*)skb_put(skb
, len
);
2110 roam_adv_packet
->header
.packet_type
= BATADV_ROAM_ADV
;
2111 roam_adv_packet
->header
.version
= BATADV_COMPAT_VERSION
;
2112 roam_adv_packet
->header
.ttl
= BATADV_TTL
;
2113 roam_adv_packet
->reserved
= 0;
2114 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2117 memcpy(roam_adv_packet
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
2118 batadv_hardif_free_ref(primary_if
);
2119 memcpy(roam_adv_packet
->dst
, orig_node
->orig
, ETH_ALEN
);
2120 memcpy(roam_adv_packet
->client
, client
, ETH_ALEN
);
2122 neigh_node
= batadv_orig_node_get_router(orig_node
);
2126 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2127 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2128 orig_node
->orig
, client
, neigh_node
->addr
);
2130 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
2132 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
2137 batadv_neigh_node_free_ref(neigh_node
);
2143 static void batadv_tt_purge(struct work_struct
*work
)
2145 struct delayed_work
*delayed_work
;
2146 struct batadv_priv_tt
*priv_tt
;
2147 struct batadv_priv
*bat_priv
;
2149 delayed_work
= container_of(work
, struct delayed_work
, work
);
2150 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
2151 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
2153 batadv_tt_local_purge(bat_priv
);
2154 batadv_tt_global_purge(bat_priv
);
2155 batadv_tt_req_purge(bat_priv
);
2156 batadv_tt_roam_purge(bat_priv
);
2158 batadv_tt_start_timer(bat_priv
);
2161 void batadv_tt_free(struct batadv_priv
*bat_priv
)
2163 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
2165 batadv_tt_local_table_free(bat_priv
);
2166 batadv_tt_global_table_free(bat_priv
);
2167 batadv_tt_req_list_free(bat_priv
);
2168 batadv_tt_changes_list_free(bat_priv
);
2169 batadv_tt_roam_list_free(bat_priv
);
2171 kfree(bat_priv
->tt
.last_changeset
);
2174 /* This function will enable or disable the specified flags for all the entries
2175 * in the given hash table and returns the number of modified entries
2177 static uint16_t batadv_tt_set_flags(struct batadv_hashtable
*hash
,
2178 uint16_t flags
, bool enable
)
2181 uint16_t changed_num
= 0;
2182 struct hlist_head
*head
;
2183 struct hlist_node
*node
;
2184 struct batadv_tt_common_entry
*tt_common_entry
;
2189 for (i
= 0; i
< hash
->size
; i
++) {
2190 head
= &hash
->table
[i
];
2193 hlist_for_each_entry_rcu(tt_common_entry
, node
,
2196 if ((tt_common_entry
->flags
& flags
) == flags
)
2198 tt_common_entry
->flags
|= flags
;
2200 if (!(tt_common_entry
->flags
& flags
))
2202 tt_common_entry
->flags
&= ~flags
;
2212 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2213 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
2215 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2216 struct batadv_tt_common_entry
*tt_common
;
2217 struct batadv_tt_local_entry
*tt_local
;
2218 struct hlist_node
*node
, *node_tmp
;
2219 struct hlist_head
*head
;
2220 spinlock_t
*list_lock
; /* protects write access to the hash lists */
2226 for (i
= 0; i
< hash
->size
; i
++) {
2227 head
= &hash
->table
[i
];
2228 list_lock
= &hash
->list_locks
[i
];
2230 spin_lock_bh(list_lock
);
2231 hlist_for_each_entry_safe(tt_common
, node
, node_tmp
, head
,
2233 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
2236 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2237 "Deleting local tt entry (%pM): pending\n",
2240 atomic_dec(&bat_priv
->tt
.local_entry_num
);
2241 hlist_del_rcu(node
);
2242 tt_local
= container_of(tt_common
,
2243 struct batadv_tt_local_entry
,
2245 batadv_tt_local_entry_free_ref(tt_local
);
2247 spin_unlock_bh(list_lock
);
2252 static int batadv_tt_commit_changes(struct batadv_priv
*bat_priv
,
2253 unsigned char **packet_buff
,
2254 int *packet_buff_len
, int packet_min_len
)
2256 uint16_t changed_num
= 0;
2258 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1)
2261 changed_num
= batadv_tt_set_flags(bat_priv
->tt
.local_hash
,
2262 BATADV_TT_CLIENT_NEW
, false);
2264 /* all reset entries have to be counted as local entries */
2265 atomic_add(changed_num
, &bat_priv
->tt
.local_entry_num
);
2266 batadv_tt_local_purge_pending_clients(bat_priv
);
2267 bat_priv
->tt
.local_crc
= batadv_tt_local_crc(bat_priv
);
2269 /* Increment the TTVN only once per OGM interval */
2270 atomic_inc(&bat_priv
->tt
.vn
);
2271 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2272 "Local changes committed, updating to ttvn %u\n",
2273 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
2274 bat_priv
->tt
.poss_change
= false;
2276 /* reset the sending counter */
2277 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
2279 return batadv_tt_changes_fill_buff(bat_priv
, packet_buff
,
2280 packet_buff_len
, packet_min_len
);
2283 /* when calling this function (hard_iface == primary_if) has to be true */
2284 int batadv_tt_append_diff(struct batadv_priv
*bat_priv
,
2285 unsigned char **packet_buff
, int *packet_buff_len
,
2290 /* if at least one change happened */
2291 tt_num_changes
= batadv_tt_commit_changes(bat_priv
, packet_buff
,
2295 /* if the changes have been sent often enough */
2296 if ((tt_num_changes
< 0) &&
2297 (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))) {
2298 batadv_tt_realloc_packet_buff(packet_buff
, packet_buff_len
,
2299 packet_min_len
, packet_min_len
);
2303 return tt_num_changes
;
2306 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, uint8_t *src
,
2309 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2310 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2313 if (!atomic_read(&bat_priv
->ap_isolation
))
2316 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
);
2317 if (!tt_local_entry
)
2320 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
);
2321 if (!tt_global_entry
)
2324 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2330 if (tt_global_entry
)
2331 batadv_tt_global_entry_free_ref(tt_global_entry
);
2333 batadv_tt_local_entry_free_ref(tt_local_entry
);
2337 void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
2338 struct batadv_orig_node
*orig_node
,
2339 const unsigned char *tt_buff
, uint8_t tt_num_changes
,
2340 uint8_t ttvn
, uint16_t tt_crc
)
2342 uint8_t orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
2343 bool full_table
= true;
2344 struct batadv_tt_change
*tt_change
;
2346 /* don't care about a backbone gateways updates. */
2347 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
))
2350 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2351 * increased by one -> we can apply the attached changes
2353 if ((!orig_node
->tt_initialised
&& ttvn
== 1) ||
2354 ttvn
- orig_ttvn
== 1) {
2355 /* the OGM could not contain the changes due to their size or
2356 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2358 * In this case send a tt request
2360 if (!tt_num_changes
) {
2365 tt_change
= (struct batadv_tt_change
*)tt_buff
;
2366 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
2369 /* Even if we received the precomputed crc with the OGM, we
2370 * prefer to recompute it to spot any possible inconsistency
2371 * in the global table
2373 orig_node
->tt_crc
= batadv_tt_global_crc(bat_priv
, orig_node
);
2375 /* The ttvn alone is not enough to guarantee consistency
2376 * because a single value could represent different states
2377 * (due to the wrap around). Thus a node has to check whether
2378 * the resulting table (after applying the changes) is still
2379 * consistent or not. E.g. a node could disconnect while its
2380 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2381 * checking the CRC value is mandatory to detect the
2384 if (orig_node
->tt_crc
!= tt_crc
)
2387 /* Roaming phase is over: tables are in sync again. I can
2390 orig_node
->tt_poss_change
= false;
2392 /* if we missed more than one change or our tables are not
2393 * in sync anymore -> request fresh tt data
2395 if (!orig_node
->tt_initialised
|| ttvn
!= orig_ttvn
||
2396 orig_node
->tt_crc
!= tt_crc
) {
2398 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2399 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2400 orig_node
->orig
, ttvn
, orig_ttvn
, tt_crc
,
2401 orig_node
->tt_crc
, tt_num_changes
);
2402 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
2403 tt_crc
, full_table
);
2409 /* returns true whether we know that the client has moved from its old
2410 * originator to another one. This entry is kept is still kept for consistency
2413 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
2416 struct batadv_tt_global_entry
*tt_global_entry
;
2419 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
2420 if (!tt_global_entry
)
2423 ret
= !!(tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
);
2424 batadv_tt_global_entry_free_ref(tt_global_entry
);
2429 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
2430 struct batadv_orig_node
*orig_node
,
2431 const unsigned char *addr
)
2435 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
,
2436 BATADV_TT_CLIENT_TEMP
,
2437 atomic_read(&orig_node
->last_ttvn
)))
2440 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2441 "Added temporary global client (addr: %pM orig: %pM)\n",
2442 addr
, orig_node
->orig
);