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
);
493 * batadv_tt_local_remove - logically remove an entry from the local table
494 * @bat_priv: the bat priv with all the soft interface information
495 * @addr: the MAC address of the client to remove
496 * @message: message to append to the log on deletion
497 * @roaming: true if the deletion is due to a roaming event
499 * Returns the flags assigned to the local entry before being deleted
501 uint16_t batadv_tt_local_remove(struct batadv_priv
*bat_priv
,
502 const uint8_t *addr
, const char *message
,
505 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
506 uint16_t flags
, curr_flags
= BATADV_NO_FLAGS
;
508 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
512 curr_flags
= tt_local_entry
->common
.flags
;
514 flags
= BATADV_TT_CLIENT_DEL
;
516 flags
|= BATADV_TT_CLIENT_ROAM
;
518 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
, message
);
522 batadv_tt_local_entry_free_ref(tt_local_entry
);
527 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
528 struct hlist_head
*head
)
530 struct batadv_tt_local_entry
*tt_local_entry
;
531 struct batadv_tt_common_entry
*tt_common_entry
;
532 struct hlist_node
*node
, *node_tmp
;
534 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
, head
,
536 tt_local_entry
= container_of(tt_common_entry
,
537 struct batadv_tt_local_entry
,
539 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
542 /* entry already marked for deletion */
543 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
546 if (!batadv_has_timed_out(tt_local_entry
->last_seen
,
547 BATADV_TT_LOCAL_TIMEOUT
))
550 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
551 BATADV_TT_CLIENT_DEL
, "timed out");
555 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
)
557 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
558 struct hlist_head
*head
;
559 spinlock_t
*list_lock
; /* protects write access to the hash lists */
562 for (i
= 0; i
< hash
->size
; i
++) {
563 head
= &hash
->table
[i
];
564 list_lock
= &hash
->list_locks
[i
];
566 spin_lock_bh(list_lock
);
567 batadv_tt_local_purge_list(bat_priv
, head
);
568 spin_unlock_bh(list_lock
);
573 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
575 struct batadv_hashtable
*hash
;
576 spinlock_t
*list_lock
; /* protects write access to the hash lists */
577 struct batadv_tt_common_entry
*tt_common_entry
;
578 struct batadv_tt_local_entry
*tt_local
;
579 struct hlist_node
*node
, *node_tmp
;
580 struct hlist_head
*head
;
583 if (!bat_priv
->tt
.local_hash
)
586 hash
= bat_priv
->tt
.local_hash
;
588 for (i
= 0; i
< hash
->size
; i
++) {
589 head
= &hash
->table
[i
];
590 list_lock
= &hash
->list_locks
[i
];
592 spin_lock_bh(list_lock
);
593 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
,
596 tt_local
= container_of(tt_common_entry
,
597 struct batadv_tt_local_entry
,
599 batadv_tt_local_entry_free_ref(tt_local
);
601 spin_unlock_bh(list_lock
);
604 batadv_hash_destroy(hash
);
606 bat_priv
->tt
.local_hash
= NULL
;
609 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
611 if (bat_priv
->tt
.global_hash
)
614 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
616 if (!bat_priv
->tt
.global_hash
)
622 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
624 struct batadv_tt_change_node
*entry
, *safe
;
626 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
628 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
630 list_del(&entry
->list
);
634 atomic_set(&bat_priv
->tt
.local_changes
, 0);
635 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
638 /* retrieves the orig_tt_list_entry belonging to orig_node from the
639 * batadv_tt_global_entry list
641 * returns it with an increased refcounter, NULL if not found
643 static struct batadv_tt_orig_list_entry
*
644 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
645 const struct batadv_orig_node
*orig_node
)
647 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
648 const struct hlist_head
*head
;
649 struct hlist_node
*node
;
652 head
= &entry
->orig_list
;
653 hlist_for_each_entry_rcu(tmp_orig_entry
, node
, head
, list
) {
654 if (tmp_orig_entry
->orig_node
!= orig_node
)
656 if (!atomic_inc_not_zero(&tmp_orig_entry
->refcount
))
659 orig_entry
= tmp_orig_entry
;
667 /* find out if an orig_node is already in the list of a tt_global_entry.
668 * returns true if found, false otherwise
671 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
672 const struct batadv_orig_node
*orig_node
)
674 struct batadv_tt_orig_list_entry
*orig_entry
;
677 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
680 batadv_tt_orig_list_entry_free_ref(orig_entry
);
687 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
688 struct batadv_orig_node
*orig_node
, int ttvn
)
690 struct batadv_tt_orig_list_entry
*orig_entry
;
692 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
694 /* refresh the ttvn: the current value could be a bogus one that
695 * was added during a "temporary client detection"
697 orig_entry
->ttvn
= ttvn
;
701 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
705 INIT_HLIST_NODE(&orig_entry
->list
);
706 atomic_inc(&orig_node
->refcount
);
707 atomic_inc(&orig_node
->tt_size
);
708 orig_entry
->orig_node
= orig_node
;
709 orig_entry
->ttvn
= ttvn
;
710 atomic_set(&orig_entry
->refcount
, 2);
712 spin_lock_bh(&tt_global
->list_lock
);
713 hlist_add_head_rcu(&orig_entry
->list
,
714 &tt_global
->orig_list
);
715 spin_unlock_bh(&tt_global
->list_lock
);
718 batadv_tt_orig_list_entry_free_ref(orig_entry
);
721 /* caller must hold orig_node refcount */
722 int batadv_tt_global_add(struct batadv_priv
*bat_priv
,
723 struct batadv_orig_node
*orig_node
,
724 const unsigned char *tt_addr
, uint8_t flags
,
727 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
730 struct batadv_tt_common_entry
*common
;
731 uint16_t local_flags
;
733 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
);
735 if (!tt_global_entry
) {
736 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
737 if (!tt_global_entry
)
740 common
= &tt_global_entry
->common
;
741 memcpy(common
->addr
, tt_addr
, ETH_ALEN
);
743 common
->flags
= flags
;
744 tt_global_entry
->roam_at
= 0;
745 /* node must store current time in case of roaming. This is
746 * needed to purge this entry out on timeout (if nobody claims
749 if (flags
& BATADV_TT_CLIENT_ROAM
)
750 tt_global_entry
->roam_at
= jiffies
;
751 atomic_set(&common
->refcount
, 2);
752 common
->added_at
= jiffies
;
754 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
755 spin_lock_init(&tt_global_entry
->list_lock
);
757 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
759 batadv_choose_orig
, common
,
760 &common
->hash_entry
);
762 if (unlikely(hash_added
!= 0)) {
763 /* remove the reference for the hash */
764 batadv_tt_global_entry_free_ref(tt_global_entry
);
768 /* If there is already a global entry, we can use this one for
770 * But if we are trying to add a temporary client we can exit
771 * directly because the temporary information should never
772 * override any already known client state (whatever it is)
774 if (flags
& BATADV_TT_CLIENT_TEMP
)
777 /* if the client was temporary added before receiving the first
778 * OGM announcing it, we have to clear the TEMP flag
780 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_TEMP
;
782 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
783 * one originator left in the list and we previously received a
784 * delete + roaming change for this originator.
786 * We should first delete the old originator before adding the
789 if (tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
790 batadv_tt_global_del_orig_list(tt_global_entry
);
791 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
792 tt_global_entry
->roam_at
= 0;
795 /* add the new orig_entry (if needed) or update it */
796 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
798 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
799 "Creating new global tt entry: %pM (via %pM)\n",
800 tt_global_entry
->common
.addr
, orig_node
->orig
);
805 /* remove address from local hash if present */
806 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
,
807 "global tt received",
808 flags
& BATADV_TT_CLIENT_ROAM
);
809 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
813 batadv_tt_global_entry_free_ref(tt_global_entry
);
817 /* print all orig nodes who announce the address for this global entry.
818 * it is assumed that the caller holds rcu_read_lock();
821 batadv_tt_global_print_entry(struct batadv_tt_global_entry
*tt_global_entry
,
822 struct seq_file
*seq
)
824 struct hlist_head
*head
;
825 struct hlist_node
*node
;
826 struct batadv_tt_orig_list_entry
*orig_entry
;
827 struct batadv_tt_common_entry
*tt_common_entry
;
831 tt_common_entry
= &tt_global_entry
->common
;
833 head
= &tt_global_entry
->orig_list
;
835 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
836 flags
= tt_common_entry
->flags
;
837 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
838 seq_printf(seq
, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
839 tt_global_entry
->common
.addr
, orig_entry
->ttvn
,
840 orig_entry
->orig_node
->orig
, last_ttvn
,
841 (flags
& BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
842 (flags
& BATADV_TT_CLIENT_WIFI
? 'W' : '.'),
843 (flags
& BATADV_TT_CLIENT_TEMP
? 'T' : '.'));
847 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
849 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
850 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
851 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
852 struct batadv_tt_common_entry
*tt_common_entry
;
853 struct batadv_tt_global_entry
*tt_global
;
854 struct batadv_hard_iface
*primary_if
;
855 struct hlist_node
*node
;
856 struct hlist_head
*head
;
859 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
864 "Globally announced TT entries received via the mesh %s\n",
866 seq_printf(seq
, " %-13s %s %-15s %s %s\n",
867 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
869 for (i
= 0; i
< hash
->size
; i
++) {
870 head
= &hash
->table
[i
];
873 hlist_for_each_entry_rcu(tt_common_entry
, node
,
875 tt_global
= container_of(tt_common_entry
,
876 struct batadv_tt_global_entry
,
878 batadv_tt_global_print_entry(tt_global
, seq
);
884 batadv_hardif_free_ref(primary_if
);
888 /* deletes the orig list of a tt_global_entry */
890 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
892 struct hlist_head
*head
;
893 struct hlist_node
*node
, *safe
;
894 struct batadv_tt_orig_list_entry
*orig_entry
;
896 spin_lock_bh(&tt_global_entry
->list_lock
);
897 head
= &tt_global_entry
->orig_list
;
898 hlist_for_each_entry_safe(orig_entry
, node
, safe
, head
, list
) {
900 batadv_tt_orig_list_entry_free_ref(orig_entry
);
902 spin_unlock_bh(&tt_global_entry
->list_lock
);
907 batadv_tt_global_del_orig_entry(struct batadv_priv
*bat_priv
,
908 struct batadv_tt_global_entry
*tt_global_entry
,
909 struct batadv_orig_node
*orig_node
,
912 struct hlist_head
*head
;
913 struct hlist_node
*node
, *safe
;
914 struct batadv_tt_orig_list_entry
*orig_entry
;
916 spin_lock_bh(&tt_global_entry
->list_lock
);
917 head
= &tt_global_entry
->orig_list
;
918 hlist_for_each_entry_safe(orig_entry
, node
, safe
, head
, list
) {
919 if (orig_entry
->orig_node
== orig_node
) {
920 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
921 "Deleting %pM from global tt entry %pM: %s\n",
923 tt_global_entry
->common
.addr
, message
);
925 batadv_tt_orig_list_entry_free_ref(orig_entry
);
928 spin_unlock_bh(&tt_global_entry
->list_lock
);
932 batadv_tt_global_del_struct(struct batadv_priv
*bat_priv
,
933 struct batadv_tt_global_entry
*tt_global_entry
,
936 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
937 "Deleting global tt entry %pM: %s\n",
938 tt_global_entry
->common
.addr
, message
);
940 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
941 batadv_choose_orig
, tt_global_entry
->common
.addr
);
942 batadv_tt_global_entry_free_ref(tt_global_entry
);
946 /* If the client is to be deleted, we check if it is the last origantor entry
947 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
948 * timer, otherwise we simply remove the originator scheduled for deletion.
951 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
952 struct batadv_tt_global_entry
*tt_global_entry
,
953 struct batadv_orig_node
*orig_node
,
956 bool last_entry
= true;
957 struct hlist_head
*head
;
958 struct hlist_node
*node
;
959 struct batadv_tt_orig_list_entry
*orig_entry
;
961 /* no local entry exists, case 1:
962 * Check if this is the last one or if other entries exist.
966 head
= &tt_global_entry
->orig_list
;
967 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
968 if (orig_entry
->orig_node
!= orig_node
) {
976 /* its the last one, mark for roaming. */
977 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
978 tt_global_entry
->roam_at
= jiffies
;
980 /* there is another entry, we can simply delete this
981 * one and can still use the other one.
983 batadv_tt_global_del_orig_entry(bat_priv
, tt_global_entry
,
989 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
990 struct batadv_orig_node
*orig_node
,
991 const unsigned char *addr
,
992 const char *message
, bool roaming
)
994 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
995 struct batadv_tt_local_entry
*local_entry
= NULL
;
997 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
998 if (!tt_global_entry
)
1002 batadv_tt_global_del_orig_entry(bat_priv
, tt_global_entry
,
1003 orig_node
, message
);
1005 if (hlist_empty(&tt_global_entry
->orig_list
))
1006 batadv_tt_global_del_struct(bat_priv
, tt_global_entry
,
1012 /* if we are deleting a global entry due to a roam
1013 * event, there are two possibilities:
1014 * 1) the client roamed from node A to node B => if there
1015 * is only one originator left for this client, we mark
1016 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1017 * wait for node B to claim it. In case of timeout
1018 * the entry is purged.
1020 * If there are other originators left, we directly delete
1022 * 2) the client roamed to us => we can directly delete
1023 * the global entry, since it is useless now.
1025 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1026 tt_global_entry
->common
.addr
);
1028 /* local entry exists, case 2: client roamed to us. */
1029 batadv_tt_global_del_orig_list(tt_global_entry
);
1030 batadv_tt_global_del_struct(bat_priv
, tt_global_entry
, message
);
1032 /* no local entry exists, case 1: check for roaming */
1033 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1034 orig_node
, message
);
1038 if (tt_global_entry
)
1039 batadv_tt_global_entry_free_ref(tt_global_entry
);
1041 batadv_tt_local_entry_free_ref(local_entry
);
1044 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1045 struct batadv_orig_node
*orig_node
,
1046 const char *message
)
1048 struct batadv_tt_global_entry
*tt_global
;
1049 struct batadv_tt_common_entry
*tt_common_entry
;
1051 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1052 struct hlist_node
*node
, *safe
;
1053 struct hlist_head
*head
;
1054 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1059 for (i
= 0; i
< hash
->size
; i
++) {
1060 head
= &hash
->table
[i
];
1061 list_lock
= &hash
->list_locks
[i
];
1063 spin_lock_bh(list_lock
);
1064 hlist_for_each_entry_safe(tt_common_entry
, node
, safe
,
1066 tt_global
= container_of(tt_common_entry
,
1067 struct batadv_tt_global_entry
,
1070 batadv_tt_global_del_orig_entry(bat_priv
, tt_global
,
1071 orig_node
, message
);
1073 if (hlist_empty(&tt_global
->orig_list
)) {
1074 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1075 "Deleting global tt entry %pM: %s\n",
1076 tt_global
->common
.addr
, message
);
1077 hlist_del_rcu(node
);
1078 batadv_tt_global_entry_free_ref(tt_global
);
1081 spin_unlock_bh(list_lock
);
1083 orig_node
->tt_initialised
= false;
1086 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1090 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1091 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1093 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1094 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1096 *msg
= "Roaming timeout\n";
1099 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1100 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1102 *msg
= "Temporary client timeout\n";
1108 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1110 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1111 struct hlist_head
*head
;
1112 struct hlist_node
*node
, *node_tmp
;
1113 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1116 struct batadv_tt_common_entry
*tt_common
;
1117 struct batadv_tt_global_entry
*tt_global
;
1119 for (i
= 0; i
< hash
->size
; i
++) {
1120 head
= &hash
->table
[i
];
1121 list_lock
= &hash
->list_locks
[i
];
1123 spin_lock_bh(list_lock
);
1124 hlist_for_each_entry_safe(tt_common
, node
, node_tmp
, head
,
1126 tt_global
= container_of(tt_common
,
1127 struct batadv_tt_global_entry
,
1130 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1133 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1134 "Deleting global tt entry (%pM): %s\n",
1135 tt_global
->common
.addr
, msg
);
1137 hlist_del_rcu(node
);
1139 batadv_tt_global_entry_free_ref(tt_global
);
1141 spin_unlock_bh(list_lock
);
1145 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
1147 struct batadv_hashtable
*hash
;
1148 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1149 struct batadv_tt_common_entry
*tt_common_entry
;
1150 struct batadv_tt_global_entry
*tt_global
;
1151 struct hlist_node
*node
, *node_tmp
;
1152 struct hlist_head
*head
;
1155 if (!bat_priv
->tt
.global_hash
)
1158 hash
= bat_priv
->tt
.global_hash
;
1160 for (i
= 0; i
< hash
->size
; i
++) {
1161 head
= &hash
->table
[i
];
1162 list_lock
= &hash
->list_locks
[i
];
1164 spin_lock_bh(list_lock
);
1165 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
,
1167 hlist_del_rcu(node
);
1168 tt_global
= container_of(tt_common_entry
,
1169 struct batadv_tt_global_entry
,
1171 batadv_tt_global_entry_free_ref(tt_global
);
1173 spin_unlock_bh(list_lock
);
1176 batadv_hash_destroy(hash
);
1178 bat_priv
->tt
.global_hash
= NULL
;
1182 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
1183 struct batadv_tt_global_entry
*tt_global_entry
)
1187 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
1188 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
1194 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
1196 const uint8_t *addr
)
1198 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
1199 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
1200 struct batadv_orig_node
*orig_node
= NULL
;
1201 struct batadv_neigh_node
*router
= NULL
;
1202 struct hlist_head
*head
;
1203 struct hlist_node
*node
;
1204 struct batadv_tt_orig_list_entry
*orig_entry
;
1207 if (src
&& atomic_read(&bat_priv
->ap_isolation
)) {
1208 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
);
1209 if (!tt_local_entry
)
1213 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
1214 if (!tt_global_entry
)
1217 /* check whether the clients should not communicate due to AP
1220 if (tt_local_entry
&&
1221 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
1227 head
= &tt_global_entry
->orig_list
;
1228 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
1229 router
= batadv_orig_node_get_router(orig_entry
->orig_node
);
1233 if (router
->tq_avg
> best_tq
) {
1234 orig_node
= orig_entry
->orig_node
;
1235 best_tq
= router
->tq_avg
;
1237 batadv_neigh_node_free_ref(router
);
1239 /* found anything? */
1240 if (orig_node
&& !atomic_inc_not_zero(&orig_node
->refcount
))
1244 if (tt_global_entry
)
1245 batadv_tt_global_entry_free_ref(tt_global_entry
);
1247 batadv_tt_local_entry_free_ref(tt_local_entry
);
1252 /* Calculates the checksum of the local table of a given orig_node */
1253 static uint16_t batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
1254 struct batadv_orig_node
*orig_node
)
1256 uint16_t total
= 0, total_one
;
1257 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1258 struct batadv_tt_common_entry
*tt_common
;
1259 struct batadv_tt_global_entry
*tt_global
;
1260 struct hlist_node
*node
;
1261 struct hlist_head
*head
;
1265 for (i
= 0; i
< hash
->size
; i
++) {
1266 head
= &hash
->table
[i
];
1269 hlist_for_each_entry_rcu(tt_common
, node
, head
, hash_entry
) {
1270 tt_global
= container_of(tt_common
,
1271 struct batadv_tt_global_entry
,
1273 /* Roaming clients are in the global table for
1274 * consistency only. They don't have to be
1275 * taken into account while computing the
1278 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
1280 /* Temporary clients have not been announced yet, so
1281 * they have to be skipped while computing the global
1284 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
1287 /* find out if this global entry is announced by this
1290 if (!batadv_tt_global_entry_has_orig(tt_global
,
1295 for (j
= 0; j
< ETH_ALEN
; j
++)
1296 total_one
= crc16_byte(total_one
,
1297 tt_common
->addr
[j
]);
1306 /* Calculates the checksum of the local table */
1307 static uint16_t batadv_tt_local_crc(struct batadv_priv
*bat_priv
)
1309 uint16_t total
= 0, total_one
;
1310 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1311 struct batadv_tt_common_entry
*tt_common
;
1312 struct hlist_node
*node
;
1313 struct hlist_head
*head
;
1317 for (i
= 0; i
< hash
->size
; i
++) {
1318 head
= &hash
->table
[i
];
1321 hlist_for_each_entry_rcu(tt_common
, node
, head
, hash_entry
) {
1322 /* not yet committed clients have not to be taken into
1323 * account while computing the CRC
1325 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
1328 for (j
= 0; j
< ETH_ALEN
; j
++)
1329 total_one
= crc16_byte(total_one
,
1330 tt_common
->addr
[j
]);
1339 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
1341 struct batadv_tt_req_node
*node
, *safe
;
1343 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1345 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
1346 list_del(&node
->list
);
1350 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1353 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
1354 struct batadv_orig_node
*orig_node
,
1355 const unsigned char *tt_buff
,
1356 uint8_t tt_num_changes
)
1358 uint16_t tt_buff_len
= batadv_tt_len(tt_num_changes
);
1360 /* Replace the old buffer only if I received something in the
1361 * last OGM (the OGM could carry no changes)
1363 spin_lock_bh(&orig_node
->tt_buff_lock
);
1364 if (tt_buff_len
> 0) {
1365 kfree(orig_node
->tt_buff
);
1366 orig_node
->tt_buff_len
= 0;
1367 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
1368 if (orig_node
->tt_buff
) {
1369 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
1370 orig_node
->tt_buff_len
= tt_buff_len
;
1373 spin_unlock_bh(&orig_node
->tt_buff_lock
);
1376 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
1378 struct batadv_tt_req_node
*node
, *safe
;
1380 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1381 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
1382 if (batadv_has_timed_out(node
->issued_at
,
1383 BATADV_TT_REQUEST_TIMEOUT
)) {
1384 list_del(&node
->list
);
1388 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1391 /* returns the pointer to the new tt_req_node struct if no request
1392 * has already been issued for this orig_node, NULL otherwise
1394 static struct batadv_tt_req_node
*
1395 batadv_new_tt_req_node(struct batadv_priv
*bat_priv
,
1396 struct batadv_orig_node
*orig_node
)
1398 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
1400 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1401 list_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
1402 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
1403 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
1404 BATADV_TT_REQUEST_TIMEOUT
))
1408 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
1412 memcpy(tt_req_node
->addr
, orig_node
->orig
, ETH_ALEN
);
1413 tt_req_node
->issued_at
= jiffies
;
1415 list_add(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
1417 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1421 /* data_ptr is useless here, but has to be kept to respect the prototype */
1422 static int batadv_tt_local_valid_entry(const void *entry_ptr
,
1423 const void *data_ptr
)
1425 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
1427 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
1432 static int batadv_tt_global_valid(const void *entry_ptr
,
1433 const void *data_ptr
)
1435 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
1436 const struct batadv_tt_global_entry
*tt_global_entry
;
1437 const struct batadv_orig_node
*orig_node
= data_ptr
;
1439 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
1440 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
1443 tt_global_entry
= container_of(tt_common_entry
,
1444 struct batadv_tt_global_entry
,
1447 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
1450 static struct sk_buff
*
1451 batadv_tt_response_fill_table(uint16_t tt_len
, uint8_t ttvn
,
1452 struct batadv_hashtable
*hash
,
1453 struct batadv_hard_iface
*primary_if
,
1454 int (*valid_cb
)(const void *, const void *),
1457 struct batadv_tt_common_entry
*tt_common_entry
;
1458 struct batadv_tt_query_packet
*tt_response
;
1459 struct batadv_tt_change
*tt_change
;
1460 struct hlist_node
*node
;
1461 struct hlist_head
*head
;
1462 struct sk_buff
*skb
= NULL
;
1463 uint16_t tt_tot
, tt_count
;
1464 ssize_t tt_query_size
= sizeof(struct batadv_tt_query_packet
);
1468 if (tt_query_size
+ tt_len
> primary_if
->soft_iface
->mtu
) {
1469 tt_len
= primary_if
->soft_iface
->mtu
- tt_query_size
;
1470 tt_len
-= tt_len
% sizeof(struct batadv_tt_change
);
1472 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1474 len
= tt_query_size
+ tt_len
;
1475 skb
= dev_alloc_skb(len
+ ETH_HLEN
);
1479 skb_reserve(skb
, ETH_HLEN
);
1480 tt_response
= (struct batadv_tt_query_packet
*)skb_put(skb
, len
);
1481 tt_response
->ttvn
= ttvn
;
1483 tt_change
= (struct batadv_tt_change
*)(skb
->data
+ tt_query_size
);
1487 for (i
= 0; i
< hash
->size
; i
++) {
1488 head
= &hash
->table
[i
];
1490 hlist_for_each_entry_rcu(tt_common_entry
, node
,
1492 if (tt_count
== tt_tot
)
1495 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
1498 memcpy(tt_change
->addr
, tt_common_entry
->addr
,
1500 tt_change
->flags
= BATADV_NO_FLAGS
;
1508 /* store in the message the number of entries we have successfully
1511 tt_response
->tt_data
= htons(tt_count
);
1517 static int batadv_send_tt_request(struct batadv_priv
*bat_priv
,
1518 struct batadv_orig_node
*dst_orig_node
,
1519 uint8_t ttvn
, uint16_t tt_crc
,
1522 struct sk_buff
*skb
= NULL
;
1523 struct batadv_tt_query_packet
*tt_request
;
1524 struct batadv_neigh_node
*neigh_node
= NULL
;
1525 struct batadv_hard_iface
*primary_if
;
1526 struct batadv_tt_req_node
*tt_req_node
= NULL
;
1530 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1534 /* The new tt_req will be issued only if I'm not waiting for a
1535 * reply from the same orig_node yet
1537 tt_req_node
= batadv_new_tt_req_node(bat_priv
, dst_orig_node
);
1541 skb
= dev_alloc_skb(sizeof(*tt_request
) + ETH_HLEN
);
1545 skb_reserve(skb
, ETH_HLEN
);
1547 tt_req_len
= sizeof(*tt_request
);
1548 tt_request
= (struct batadv_tt_query_packet
*)skb_put(skb
, tt_req_len
);
1550 tt_request
->header
.packet_type
= BATADV_TT_QUERY
;
1551 tt_request
->header
.version
= BATADV_COMPAT_VERSION
;
1552 memcpy(tt_request
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1553 memcpy(tt_request
->dst
, dst_orig_node
->orig
, ETH_ALEN
);
1554 tt_request
->header
.ttl
= BATADV_TTL
;
1555 tt_request
->ttvn
= ttvn
;
1556 tt_request
->tt_data
= htons(tt_crc
);
1557 tt_request
->flags
= BATADV_TT_REQUEST
;
1560 tt_request
->flags
|= BATADV_TT_FULL_TABLE
;
1562 neigh_node
= batadv_orig_node_get_router(dst_orig_node
);
1566 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1567 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1568 dst_orig_node
->orig
, neigh_node
->addr
,
1569 (full_table
? 'F' : '.'));
1571 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
1573 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1578 batadv_neigh_node_free_ref(neigh_node
);
1580 batadv_hardif_free_ref(primary_if
);
1583 if (ret
&& tt_req_node
) {
1584 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1585 list_del(&tt_req_node
->list
);
1586 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1593 batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
1594 struct batadv_tt_query_packet
*tt_request
)
1596 struct batadv_orig_node
*req_dst_orig_node
= NULL
;
1597 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
1598 struct batadv_neigh_node
*neigh_node
= NULL
;
1599 struct batadv_hard_iface
*primary_if
= NULL
;
1600 uint8_t orig_ttvn
, req_ttvn
, ttvn
;
1602 unsigned char *tt_buff
;
1604 uint16_t tt_len
, tt_tot
;
1605 struct sk_buff
*skb
= NULL
;
1606 struct batadv_tt_query_packet
*tt_response
;
1607 uint8_t *packet_pos
;
1610 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1611 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1612 tt_request
->src
, tt_request
->ttvn
, tt_request
->dst
,
1613 (tt_request
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1615 /* Let's get the orig node of the REAL destination */
1616 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->dst
);
1617 if (!req_dst_orig_node
)
1620 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->src
);
1621 if (!res_dst_orig_node
)
1624 neigh_node
= batadv_orig_node_get_router(res_dst_orig_node
);
1628 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1632 orig_ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1633 req_ttvn
= tt_request
->ttvn
;
1635 /* I don't have the requested data */
1636 if (orig_ttvn
!= req_ttvn
||
1637 tt_request
->tt_data
!= htons(req_dst_orig_node
->tt_crc
))
1640 /* If the full table has been explicitly requested */
1641 if (tt_request
->flags
& BATADV_TT_FULL_TABLE
||
1642 !req_dst_orig_node
->tt_buff
)
1647 /* In this version, fragmentation is not implemented, then
1648 * I'll send only one packet with as much TT entries as I can
1651 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
1652 tt_len
= req_dst_orig_node
->tt_buff_len
;
1653 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1655 len
= sizeof(*tt_response
) + tt_len
;
1656 skb
= dev_alloc_skb(len
+ ETH_HLEN
);
1660 skb_reserve(skb
, ETH_HLEN
);
1661 packet_pos
= skb_put(skb
, len
);
1662 tt_response
= (struct batadv_tt_query_packet
*)packet_pos
;
1663 tt_response
->ttvn
= req_ttvn
;
1664 tt_response
->tt_data
= htons(tt_tot
);
1666 tt_buff
= skb
->data
+ sizeof(*tt_response
);
1667 /* Copy the last orig_node's OGM buffer */
1668 memcpy(tt_buff
, req_dst_orig_node
->tt_buff
,
1669 req_dst_orig_node
->tt_buff_len
);
1671 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1673 tt_len
= (uint16_t)atomic_read(&req_dst_orig_node
->tt_size
);
1674 tt_len
*= sizeof(struct batadv_tt_change
);
1675 ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1677 skb
= batadv_tt_response_fill_table(tt_len
, ttvn
,
1678 bat_priv
->tt
.global_hash
,
1680 batadv_tt_global_valid
,
1685 tt_response
= (struct batadv_tt_query_packet
*)skb
->data
;
1688 tt_response
->header
.packet_type
= BATADV_TT_QUERY
;
1689 tt_response
->header
.version
= BATADV_COMPAT_VERSION
;
1690 tt_response
->header
.ttl
= BATADV_TTL
;
1691 memcpy(tt_response
->src
, req_dst_orig_node
->orig
, ETH_ALEN
);
1692 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1693 tt_response
->flags
= BATADV_TT_RESPONSE
;
1696 tt_response
->flags
|= BATADV_TT_FULL_TABLE
;
1698 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1699 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1700 res_dst_orig_node
->orig
, neigh_node
->addr
,
1701 req_dst_orig_node
->orig
, req_ttvn
);
1703 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
1705 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1710 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1713 if (res_dst_orig_node
)
1714 batadv_orig_node_free_ref(res_dst_orig_node
);
1715 if (req_dst_orig_node
)
1716 batadv_orig_node_free_ref(req_dst_orig_node
);
1718 batadv_neigh_node_free_ref(neigh_node
);
1720 batadv_hardif_free_ref(primary_if
);
1728 batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
1729 struct batadv_tt_query_packet
*tt_request
)
1731 struct batadv_orig_node
*orig_node
= NULL
;
1732 struct batadv_neigh_node
*neigh_node
= NULL
;
1733 struct batadv_hard_iface
*primary_if
= NULL
;
1734 uint8_t my_ttvn
, req_ttvn
, ttvn
;
1736 unsigned char *tt_buff
;
1738 uint16_t tt_len
, tt_tot
;
1739 struct sk_buff
*skb
= NULL
;
1740 struct batadv_tt_query_packet
*tt_response
;
1741 uint8_t *packet_pos
;
1744 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1745 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1746 tt_request
->src
, tt_request
->ttvn
,
1747 (tt_request
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1750 my_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
1751 req_ttvn
= tt_request
->ttvn
;
1753 orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->src
);
1757 neigh_node
= batadv_orig_node_get_router(orig_node
);
1761 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1765 /* If the full table has been explicitly requested or the gap
1766 * is too big send the whole local translation table
1768 if (tt_request
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
1769 !bat_priv
->tt
.last_changeset
)
1774 /* In this version, fragmentation is not implemented, then
1775 * I'll send only one packet with as much TT entries as I can
1778 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
1779 tt_len
= bat_priv
->tt
.last_changeset_len
;
1780 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1782 len
= sizeof(*tt_response
) + tt_len
;
1783 skb
= dev_alloc_skb(len
+ ETH_HLEN
);
1787 skb_reserve(skb
, ETH_HLEN
);
1788 packet_pos
= skb_put(skb
, len
);
1789 tt_response
= (struct batadv_tt_query_packet
*)packet_pos
;
1790 tt_response
->ttvn
= req_ttvn
;
1791 tt_response
->tt_data
= htons(tt_tot
);
1793 tt_buff
= skb
->data
+ sizeof(*tt_response
);
1794 memcpy(tt_buff
, bat_priv
->tt
.last_changeset
,
1795 bat_priv
->tt
.last_changeset_len
);
1796 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
1798 tt_len
= (uint16_t)atomic_read(&bat_priv
->tt
.local_entry_num
);
1799 tt_len
*= sizeof(struct batadv_tt_change
);
1800 ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
1802 skb
= batadv_tt_response_fill_table(tt_len
, ttvn
,
1803 bat_priv
->tt
.local_hash
,
1805 batadv_tt_local_valid_entry
,
1810 tt_response
= (struct batadv_tt_query_packet
*)skb
->data
;
1813 tt_response
->header
.packet_type
= BATADV_TT_QUERY
;
1814 tt_response
->header
.version
= BATADV_COMPAT_VERSION
;
1815 tt_response
->header
.ttl
= BATADV_TTL
;
1816 memcpy(tt_response
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1817 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1818 tt_response
->flags
= BATADV_TT_RESPONSE
;
1821 tt_response
->flags
|= BATADV_TT_FULL_TABLE
;
1823 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1824 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1825 orig_node
->orig
, neigh_node
->addr
,
1826 (tt_response
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1828 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
1830 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1835 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
1838 batadv_orig_node_free_ref(orig_node
);
1840 batadv_neigh_node_free_ref(neigh_node
);
1842 batadv_hardif_free_ref(primary_if
);
1845 /* This packet was for me, so it doesn't need to be re-routed */
1849 bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
1850 struct batadv_tt_query_packet
*tt_request
)
1852 if (batadv_is_my_mac(tt_request
->dst
)) {
1853 /* don't answer backbone gws! */
1854 if (batadv_bla_is_backbone_gw_orig(bat_priv
, tt_request
->src
))
1857 return batadv_send_my_tt_response(bat_priv
, tt_request
);
1859 return batadv_send_other_tt_response(bat_priv
, tt_request
);
1863 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
1864 struct batadv_orig_node
*orig_node
,
1865 struct batadv_tt_change
*tt_change
,
1866 uint16_t tt_num_changes
, uint8_t ttvn
)
1871 for (i
= 0; i
< tt_num_changes
; i
++) {
1872 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
1873 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
1874 batadv_tt_global_del(bat_priv
, orig_node
,
1875 (tt_change
+ i
)->addr
,
1876 "tt removed by changes",
1879 if (!batadv_tt_global_add(bat_priv
, orig_node
,
1880 (tt_change
+ i
)->addr
,
1881 (tt_change
+ i
)->flags
, ttvn
))
1882 /* In case of problem while storing a
1883 * global_entry, we stop the updating
1884 * procedure without committing the
1885 * ttvn change. This will avoid to send
1886 * corrupted data on tt_request
1891 orig_node
->tt_initialised
= true;
1894 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
1895 struct batadv_tt_query_packet
*tt_response
)
1897 struct batadv_orig_node
*orig_node
= NULL
;
1899 orig_node
= batadv_orig_hash_find(bat_priv
, tt_response
->src
);
1903 /* Purge the old table first.. */
1904 batadv_tt_global_del_orig(bat_priv
, orig_node
, "Received full table");
1906 _batadv_tt_update_changes(bat_priv
, orig_node
,
1907 (struct batadv_tt_change
*)(tt_response
+ 1),
1908 ntohs(tt_response
->tt_data
),
1911 spin_lock_bh(&orig_node
->tt_buff_lock
);
1912 kfree(orig_node
->tt_buff
);
1913 orig_node
->tt_buff_len
= 0;
1914 orig_node
->tt_buff
= NULL
;
1915 spin_unlock_bh(&orig_node
->tt_buff_lock
);
1917 atomic_set(&orig_node
->last_ttvn
, tt_response
->ttvn
);
1921 batadv_orig_node_free_ref(orig_node
);
1924 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
1925 struct batadv_orig_node
*orig_node
,
1926 uint16_t tt_num_changes
, uint8_t ttvn
,
1927 struct batadv_tt_change
*tt_change
)
1929 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
1930 tt_num_changes
, ttvn
);
1932 batadv_tt_save_orig_buffer(bat_priv
, orig_node
,
1933 (unsigned char *)tt_change
, tt_num_changes
);
1934 atomic_set(&orig_node
->last_ttvn
, ttvn
);
1937 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const uint8_t *addr
)
1939 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
1942 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
1943 if (!tt_local_entry
)
1945 /* Check if the client has been logically deleted (but is kept for
1946 * consistency purpose)
1948 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
1953 batadv_tt_local_entry_free_ref(tt_local_entry
);
1957 void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
1958 struct batadv_tt_query_packet
*tt_response
)
1960 struct batadv_tt_req_node
*node
, *safe
;
1961 struct batadv_orig_node
*orig_node
= NULL
;
1962 struct batadv_tt_change
*tt_change
;
1964 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1965 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1966 tt_response
->src
, tt_response
->ttvn
,
1967 ntohs(tt_response
->tt_data
),
1968 (tt_response
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1970 /* we should have never asked a backbone gw */
1971 if (batadv_bla_is_backbone_gw_orig(bat_priv
, tt_response
->src
))
1974 orig_node
= batadv_orig_hash_find(bat_priv
, tt_response
->src
);
1978 if (tt_response
->flags
& BATADV_TT_FULL_TABLE
) {
1979 batadv_tt_fill_gtable(bat_priv
, tt_response
);
1981 tt_change
= (struct batadv_tt_change
*)(tt_response
+ 1);
1982 batadv_tt_update_changes(bat_priv
, orig_node
,
1983 ntohs(tt_response
->tt_data
),
1984 tt_response
->ttvn
, tt_change
);
1987 /* Delete the tt_req_node from pending tt_requests list */
1988 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1989 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
1990 if (!batadv_compare_eth(node
->addr
, tt_response
->src
))
1992 list_del(&node
->list
);
1995 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1997 /* Recalculate the CRC for this orig_node and store it */
1998 orig_node
->tt_crc
= batadv_tt_global_crc(bat_priv
, orig_node
);
1999 /* Roaming phase is over: tables are in sync again. I can
2002 orig_node
->tt_poss_change
= false;
2005 batadv_orig_node_free_ref(orig_node
);
2008 int batadv_tt_init(struct batadv_priv
*bat_priv
)
2012 ret
= batadv_tt_local_init(bat_priv
);
2016 ret
= batadv_tt_global_init(bat_priv
);
2020 batadv_tt_start_timer(bat_priv
);
2025 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
2027 struct batadv_tt_roam_node
*node
, *safe
;
2029 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2031 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2032 list_del(&node
->list
);
2036 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2039 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
2041 struct batadv_tt_roam_node
*node
, *safe
;
2043 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2044 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2045 if (!batadv_has_timed_out(node
->first_time
,
2046 BATADV_ROAMING_MAX_TIME
))
2049 list_del(&node
->list
);
2052 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2055 /* This function checks whether the client already reached the
2056 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2059 * returns true if the ROAMING_ADV can be sent, false otherwise
2061 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
,
2064 struct batadv_tt_roam_node
*tt_roam_node
;
2067 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2068 /* The new tt_req will be issued only if I'm not waiting for a
2069 * reply from the same orig_node yet
2071 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
2072 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
2075 if (batadv_has_timed_out(tt_roam_node
->first_time
,
2076 BATADV_ROAMING_MAX_TIME
))
2079 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
2080 /* Sorry, you roamed too many times! */
2087 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
2091 tt_roam_node
->first_time
= jiffies
;
2092 atomic_set(&tt_roam_node
->counter
,
2093 BATADV_ROAMING_MAX_COUNT
- 1);
2094 memcpy(tt_roam_node
->addr
, client
, ETH_ALEN
);
2096 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
2101 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2105 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
2106 struct batadv_orig_node
*orig_node
)
2108 struct batadv_neigh_node
*neigh_node
= NULL
;
2109 struct sk_buff
*skb
= NULL
;
2110 struct batadv_roam_adv_packet
*roam_adv_packet
;
2112 struct batadv_hard_iface
*primary_if
;
2113 size_t len
= sizeof(*roam_adv_packet
);
2115 /* before going on we have to check whether the client has
2116 * already roamed to us too many times
2118 if (!batadv_tt_check_roam_count(bat_priv
, client
))
2121 skb
= dev_alloc_skb(sizeof(*roam_adv_packet
) + ETH_HLEN
);
2125 skb_reserve(skb
, ETH_HLEN
);
2127 roam_adv_packet
= (struct batadv_roam_adv_packet
*)skb_put(skb
, len
);
2129 roam_adv_packet
->header
.packet_type
= BATADV_ROAM_ADV
;
2130 roam_adv_packet
->header
.version
= BATADV_COMPAT_VERSION
;
2131 roam_adv_packet
->header
.ttl
= BATADV_TTL
;
2132 roam_adv_packet
->reserved
= 0;
2133 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2136 memcpy(roam_adv_packet
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
2137 batadv_hardif_free_ref(primary_if
);
2138 memcpy(roam_adv_packet
->dst
, orig_node
->orig
, ETH_ALEN
);
2139 memcpy(roam_adv_packet
->client
, client
, ETH_ALEN
);
2141 neigh_node
= batadv_orig_node_get_router(orig_node
);
2145 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2146 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2147 orig_node
->orig
, client
, neigh_node
->addr
);
2149 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
2151 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
2156 batadv_neigh_node_free_ref(neigh_node
);
2162 static void batadv_tt_purge(struct work_struct
*work
)
2164 struct delayed_work
*delayed_work
;
2165 struct batadv_priv_tt
*priv_tt
;
2166 struct batadv_priv
*bat_priv
;
2168 delayed_work
= container_of(work
, struct delayed_work
, work
);
2169 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
2170 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
2172 batadv_tt_local_purge(bat_priv
);
2173 batadv_tt_global_purge(bat_priv
);
2174 batadv_tt_req_purge(bat_priv
);
2175 batadv_tt_roam_purge(bat_priv
);
2177 batadv_tt_start_timer(bat_priv
);
2180 void batadv_tt_free(struct batadv_priv
*bat_priv
)
2182 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
2184 batadv_tt_local_table_free(bat_priv
);
2185 batadv_tt_global_table_free(bat_priv
);
2186 batadv_tt_req_list_free(bat_priv
);
2187 batadv_tt_changes_list_free(bat_priv
);
2188 batadv_tt_roam_list_free(bat_priv
);
2190 kfree(bat_priv
->tt
.last_changeset
);
2193 /* This function will enable or disable the specified flags for all the entries
2194 * in the given hash table and returns the number of modified entries
2196 static uint16_t batadv_tt_set_flags(struct batadv_hashtable
*hash
,
2197 uint16_t flags
, bool enable
)
2200 uint16_t changed_num
= 0;
2201 struct hlist_head
*head
;
2202 struct hlist_node
*node
;
2203 struct batadv_tt_common_entry
*tt_common_entry
;
2208 for (i
= 0; i
< hash
->size
; i
++) {
2209 head
= &hash
->table
[i
];
2212 hlist_for_each_entry_rcu(tt_common_entry
, node
,
2215 if ((tt_common_entry
->flags
& flags
) == flags
)
2217 tt_common_entry
->flags
|= flags
;
2219 if (!(tt_common_entry
->flags
& flags
))
2221 tt_common_entry
->flags
&= ~flags
;
2231 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2232 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
2234 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2235 struct batadv_tt_common_entry
*tt_common
;
2236 struct batadv_tt_local_entry
*tt_local
;
2237 struct hlist_node
*node
, *node_tmp
;
2238 struct hlist_head
*head
;
2239 spinlock_t
*list_lock
; /* protects write access to the hash lists */
2245 for (i
= 0; i
< hash
->size
; i
++) {
2246 head
= &hash
->table
[i
];
2247 list_lock
= &hash
->list_locks
[i
];
2249 spin_lock_bh(list_lock
);
2250 hlist_for_each_entry_safe(tt_common
, node
, node_tmp
, head
,
2252 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
2255 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2256 "Deleting local tt entry (%pM): pending\n",
2259 atomic_dec(&bat_priv
->tt
.local_entry_num
);
2260 hlist_del_rcu(node
);
2261 tt_local
= container_of(tt_common
,
2262 struct batadv_tt_local_entry
,
2264 batadv_tt_local_entry_free_ref(tt_local
);
2266 spin_unlock_bh(list_lock
);
2271 static int batadv_tt_commit_changes(struct batadv_priv
*bat_priv
,
2272 unsigned char **packet_buff
,
2273 int *packet_buff_len
, int packet_min_len
)
2275 uint16_t changed_num
= 0;
2277 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1)
2280 changed_num
= batadv_tt_set_flags(bat_priv
->tt
.local_hash
,
2281 BATADV_TT_CLIENT_NEW
, false);
2283 /* all reset entries have to be counted as local entries */
2284 atomic_add(changed_num
, &bat_priv
->tt
.local_entry_num
);
2285 batadv_tt_local_purge_pending_clients(bat_priv
);
2286 bat_priv
->tt
.local_crc
= batadv_tt_local_crc(bat_priv
);
2288 /* Increment the TTVN only once per OGM interval */
2289 atomic_inc(&bat_priv
->tt
.vn
);
2290 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2291 "Local changes committed, updating to ttvn %u\n",
2292 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
2293 bat_priv
->tt
.poss_change
= false;
2295 /* reset the sending counter */
2296 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
2298 return batadv_tt_changes_fill_buff(bat_priv
, packet_buff
,
2299 packet_buff_len
, packet_min_len
);
2302 /* when calling this function (hard_iface == primary_if) has to be true */
2303 int batadv_tt_append_diff(struct batadv_priv
*bat_priv
,
2304 unsigned char **packet_buff
, int *packet_buff_len
,
2309 /* if at least one change happened */
2310 tt_num_changes
= batadv_tt_commit_changes(bat_priv
, packet_buff
,
2314 /* if the changes have been sent often enough */
2315 if ((tt_num_changes
< 0) &&
2316 (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))) {
2317 batadv_tt_realloc_packet_buff(packet_buff
, packet_buff_len
,
2318 packet_min_len
, packet_min_len
);
2322 return tt_num_changes
;
2325 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, uint8_t *src
,
2328 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2329 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2332 if (!atomic_read(&bat_priv
->ap_isolation
))
2335 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
);
2336 if (!tt_local_entry
)
2339 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
);
2340 if (!tt_global_entry
)
2343 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2349 if (tt_global_entry
)
2350 batadv_tt_global_entry_free_ref(tt_global_entry
);
2352 batadv_tt_local_entry_free_ref(tt_local_entry
);
2356 void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
2357 struct batadv_orig_node
*orig_node
,
2358 const unsigned char *tt_buff
, uint8_t tt_num_changes
,
2359 uint8_t ttvn
, uint16_t tt_crc
)
2361 uint8_t orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
2362 bool full_table
= true;
2363 struct batadv_tt_change
*tt_change
;
2365 /* don't care about a backbone gateways updates. */
2366 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
))
2369 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2370 * increased by one -> we can apply the attached changes
2372 if ((!orig_node
->tt_initialised
&& ttvn
== 1) ||
2373 ttvn
- orig_ttvn
== 1) {
2374 /* the OGM could not contain the changes due to their size or
2375 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2377 * In this case send a tt request
2379 if (!tt_num_changes
) {
2384 tt_change
= (struct batadv_tt_change
*)tt_buff
;
2385 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
2388 /* Even if we received the precomputed crc with the OGM, we
2389 * prefer to recompute it to spot any possible inconsistency
2390 * in the global table
2392 orig_node
->tt_crc
= batadv_tt_global_crc(bat_priv
, orig_node
);
2394 /* The ttvn alone is not enough to guarantee consistency
2395 * because a single value could represent different states
2396 * (due to the wrap around). Thus a node has to check whether
2397 * the resulting table (after applying the changes) is still
2398 * consistent or not. E.g. a node could disconnect while its
2399 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2400 * checking the CRC value is mandatory to detect the
2403 if (orig_node
->tt_crc
!= tt_crc
)
2406 /* Roaming phase is over: tables are in sync again. I can
2409 orig_node
->tt_poss_change
= false;
2411 /* if we missed more than one change or our tables are not
2412 * in sync anymore -> request fresh tt data
2414 if (!orig_node
->tt_initialised
|| ttvn
!= orig_ttvn
||
2415 orig_node
->tt_crc
!= tt_crc
) {
2417 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2418 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2419 orig_node
->orig
, ttvn
, orig_ttvn
, tt_crc
,
2420 orig_node
->tt_crc
, tt_num_changes
);
2421 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
2422 tt_crc
, full_table
);
2428 /* returns true whether we know that the client has moved from its old
2429 * originator to another one. This entry is kept is still kept for consistency
2432 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
2435 struct batadv_tt_global_entry
*tt_global_entry
;
2438 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
2439 if (!tt_global_entry
)
2442 ret
= !!(tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
);
2443 batadv_tt_global_entry_free_ref(tt_global_entry
);
2448 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
2449 struct batadv_orig_node
*orig_node
,
2450 const unsigned char *addr
)
2454 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
,
2455 BATADV_TT_CLIENT_TEMP
,
2456 atomic_read(&orig_node
->last_ttvn
)))
2459 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2460 "Added temporary global client (addr: %pM orig: %pM)\n",
2461 addr
, orig_node
->orig
);