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 batadv_send_roam_adv(bat_priv
,
310 tt_global_entry
->common
.addr
,
311 orig_entry
->orig_node
);
314 /* The global entry has to be marked as ROAMING and
315 * has to be kept for consistency purpose
317 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
318 tt_global_entry
->roam_at
= jiffies
;
322 batadv_tt_local_entry_free_ref(tt_local_entry
);
324 batadv_tt_global_entry_free_ref(tt_global_entry
);
327 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff
,
328 int *packet_buff_len
,
332 unsigned char *new_buff
;
334 new_buff
= kmalloc(new_packet_len
, GFP_ATOMIC
);
336 /* keep old buffer if kmalloc should fail */
338 memcpy(new_buff
, *packet_buff
, min_packet_len
);
340 *packet_buff
= new_buff
;
341 *packet_buff_len
= new_packet_len
;
345 static void batadv_tt_prepare_packet_buff(struct batadv_priv
*bat_priv
,
346 unsigned char **packet_buff
,
347 int *packet_buff_len
,
350 struct batadv_hard_iface
*primary_if
;
353 primary_if
= batadv_primary_if_get_selected(bat_priv
);
355 req_len
= min_packet_len
;
356 req_len
+= batadv_tt_len(atomic_read(&bat_priv
->tt
.local_changes
));
358 /* if we have too many changes for one packet don't send any
359 * and wait for the tt table request which will be fragmented
361 if ((!primary_if
) || (req_len
> primary_if
->soft_iface
->mtu
))
362 req_len
= min_packet_len
;
364 batadv_tt_realloc_packet_buff(packet_buff
, packet_buff_len
,
365 min_packet_len
, req_len
);
368 batadv_hardif_free_ref(primary_if
);
371 static int batadv_tt_changes_fill_buff(struct batadv_priv
*bat_priv
,
372 unsigned char **packet_buff
,
373 int *packet_buff_len
,
376 struct batadv_tt_change_node
*entry
, *safe
;
377 int count
= 0, tot_changes
= 0, new_len
;
378 unsigned char *tt_buff
;
380 batadv_tt_prepare_packet_buff(bat_priv
, packet_buff
,
381 packet_buff_len
, min_packet_len
);
383 new_len
= *packet_buff_len
- min_packet_len
;
384 tt_buff
= *packet_buff
+ min_packet_len
;
387 tot_changes
= new_len
/ batadv_tt_len(1);
389 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
390 atomic_set(&bat_priv
->tt
.local_changes
, 0);
392 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
394 if (count
< tot_changes
) {
395 memcpy(tt_buff
+ batadv_tt_len(count
),
396 &entry
->change
, sizeof(struct batadv_tt_change
));
399 list_del(&entry
->list
);
402 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
404 /* Keep the buffer for possible tt_request */
405 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
406 kfree(bat_priv
->tt
.last_changeset
);
407 bat_priv
->tt
.last_changeset_len
= 0;
408 bat_priv
->tt
.last_changeset
= NULL
;
409 /* check whether this new OGM has no changes due to size problems */
411 /* if kmalloc() fails we will reply with the full table
412 * instead of providing the diff
414 bat_priv
->tt
.last_changeset
= kmalloc(new_len
, GFP_ATOMIC
);
415 if (bat_priv
->tt
.last_changeset
) {
416 memcpy(bat_priv
->tt
.last_changeset
, tt_buff
, new_len
);
417 bat_priv
->tt
.last_changeset_len
= new_len
;
420 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
425 int batadv_tt_local_seq_print_text(struct seq_file
*seq
, void *offset
)
427 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
428 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
429 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
430 struct batadv_tt_common_entry
*tt_common_entry
;
431 struct batadv_hard_iface
*primary_if
;
432 struct hlist_node
*node
;
433 struct hlist_head
*head
;
436 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
441 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
442 net_dev
->name
, (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
444 for (i
= 0; i
< hash
->size
; i
++) {
445 head
= &hash
->table
[i
];
448 hlist_for_each_entry_rcu(tt_common_entry
, node
,
450 seq_printf(seq
, " * %pM [%c%c%c%c%c]\n",
451 tt_common_entry
->addr
,
452 (tt_common_entry
->flags
&
453 BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
454 (tt_common_entry
->flags
&
455 BATADV_TT_CLIENT_NOPURGE
? 'P' : '.'),
456 (tt_common_entry
->flags
&
457 BATADV_TT_CLIENT_NEW
? 'N' : '.'),
458 (tt_common_entry
->flags
&
459 BATADV_TT_CLIENT_PENDING
? 'X' : '.'),
460 (tt_common_entry
->flags
&
461 BATADV_TT_CLIENT_WIFI
? 'W' : '.'));
467 batadv_hardif_free_ref(primary_if
);
472 batadv_tt_local_set_pending(struct batadv_priv
*bat_priv
,
473 struct batadv_tt_local_entry
*tt_local_entry
,
474 uint16_t flags
, const char *message
)
476 batadv_tt_local_event(bat_priv
, tt_local_entry
->common
.addr
,
477 tt_local_entry
->common
.flags
| flags
);
479 /* The local client has to be marked as "pending to be removed" but has
480 * to be kept in the table in order to send it in a full table
481 * response issued before the net ttvn increment (consistency check)
483 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_PENDING
;
485 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
486 "Local tt entry (%pM) pending to be removed: %s\n",
487 tt_local_entry
->common
.addr
, message
);
491 * batadv_tt_local_remove - logically remove an entry from the local table
492 * @bat_priv: the bat priv with all the soft interface information
493 * @addr: the MAC address of the client to remove
494 * @message: message to append to the log on deletion
495 * @roaming: true if the deletion is due to a roaming event
497 * Returns the flags assigned to the local entry before being deleted
499 uint16_t batadv_tt_local_remove(struct batadv_priv
*bat_priv
,
500 const uint8_t *addr
, const char *message
,
503 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
504 uint16_t flags
, curr_flags
= BATADV_NO_FLAGS
;
506 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
510 curr_flags
= tt_local_entry
->common
.flags
;
512 flags
= BATADV_TT_CLIENT_DEL
;
514 flags
|= BATADV_TT_CLIENT_ROAM
;
515 /* mark the local client as ROAMed */
516 tt_local_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
519 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
, flags
, message
);
523 batadv_tt_local_entry_free_ref(tt_local_entry
);
528 static void batadv_tt_local_purge_list(struct batadv_priv
*bat_priv
,
529 struct hlist_head
*head
)
531 struct batadv_tt_local_entry
*tt_local_entry
;
532 struct batadv_tt_common_entry
*tt_common_entry
;
533 struct hlist_node
*node
, *node_tmp
;
535 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
, head
,
537 tt_local_entry
= container_of(tt_common_entry
,
538 struct batadv_tt_local_entry
,
540 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_NOPURGE
)
543 /* entry already marked for deletion */
544 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
)
547 if (!batadv_has_timed_out(tt_local_entry
->last_seen
,
548 BATADV_TT_LOCAL_TIMEOUT
))
551 batadv_tt_local_set_pending(bat_priv
, tt_local_entry
,
552 BATADV_TT_CLIENT_DEL
, "timed out");
556 static void batadv_tt_local_purge(struct batadv_priv
*bat_priv
)
558 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
559 struct hlist_head
*head
;
560 spinlock_t
*list_lock
; /* protects write access to the hash lists */
563 for (i
= 0; i
< hash
->size
; i
++) {
564 head
= &hash
->table
[i
];
565 list_lock
= &hash
->list_locks
[i
];
567 spin_lock_bh(list_lock
);
568 batadv_tt_local_purge_list(bat_priv
, head
);
569 spin_unlock_bh(list_lock
);
574 static void batadv_tt_local_table_free(struct batadv_priv
*bat_priv
)
576 struct batadv_hashtable
*hash
;
577 spinlock_t
*list_lock
; /* protects write access to the hash lists */
578 struct batadv_tt_common_entry
*tt_common_entry
;
579 struct batadv_tt_local_entry
*tt_local
;
580 struct hlist_node
*node
, *node_tmp
;
581 struct hlist_head
*head
;
584 if (!bat_priv
->tt
.local_hash
)
587 hash
= bat_priv
->tt
.local_hash
;
589 for (i
= 0; i
< hash
->size
; i
++) {
590 head
= &hash
->table
[i
];
591 list_lock
= &hash
->list_locks
[i
];
593 spin_lock_bh(list_lock
);
594 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
,
597 tt_local
= container_of(tt_common_entry
,
598 struct batadv_tt_local_entry
,
600 batadv_tt_local_entry_free_ref(tt_local
);
602 spin_unlock_bh(list_lock
);
605 batadv_hash_destroy(hash
);
607 bat_priv
->tt
.local_hash
= NULL
;
610 static int batadv_tt_global_init(struct batadv_priv
*bat_priv
)
612 if (bat_priv
->tt
.global_hash
)
615 bat_priv
->tt
.global_hash
= batadv_hash_new(1024);
617 if (!bat_priv
->tt
.global_hash
)
623 static void batadv_tt_changes_list_free(struct batadv_priv
*bat_priv
)
625 struct batadv_tt_change_node
*entry
, *safe
;
627 spin_lock_bh(&bat_priv
->tt
.changes_list_lock
);
629 list_for_each_entry_safe(entry
, safe
, &bat_priv
->tt
.changes_list
,
631 list_del(&entry
->list
);
635 atomic_set(&bat_priv
->tt
.local_changes
, 0);
636 spin_unlock_bh(&bat_priv
->tt
.changes_list_lock
);
639 /* retrieves the orig_tt_list_entry belonging to orig_node from the
640 * batadv_tt_global_entry list
642 * returns it with an increased refcounter, NULL if not found
644 static struct batadv_tt_orig_list_entry
*
645 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry
*entry
,
646 const struct batadv_orig_node
*orig_node
)
648 struct batadv_tt_orig_list_entry
*tmp_orig_entry
, *orig_entry
= NULL
;
649 const struct hlist_head
*head
;
650 struct hlist_node
*node
;
653 head
= &entry
->orig_list
;
654 hlist_for_each_entry_rcu(tmp_orig_entry
, node
, head
, list
) {
655 if (tmp_orig_entry
->orig_node
!= orig_node
)
657 if (!atomic_inc_not_zero(&tmp_orig_entry
->refcount
))
660 orig_entry
= tmp_orig_entry
;
668 /* find out if an orig_node is already in the list of a tt_global_entry.
669 * returns true if found, false otherwise
672 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry
*entry
,
673 const struct batadv_orig_node
*orig_node
)
675 struct batadv_tt_orig_list_entry
*orig_entry
;
678 orig_entry
= batadv_tt_global_orig_entry_find(entry
, orig_node
);
681 batadv_tt_orig_list_entry_free_ref(orig_entry
);
688 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry
*tt_global
,
689 struct batadv_orig_node
*orig_node
, int ttvn
)
691 struct batadv_tt_orig_list_entry
*orig_entry
;
693 orig_entry
= batadv_tt_global_orig_entry_find(tt_global
, orig_node
);
695 /* refresh the ttvn: the current value could be a bogus one that
696 * was added during a "temporary client detection"
698 orig_entry
->ttvn
= ttvn
;
702 orig_entry
= kzalloc(sizeof(*orig_entry
), GFP_ATOMIC
);
706 INIT_HLIST_NODE(&orig_entry
->list
);
707 atomic_inc(&orig_node
->refcount
);
708 atomic_inc(&orig_node
->tt_size
);
709 orig_entry
->orig_node
= orig_node
;
710 orig_entry
->ttvn
= ttvn
;
711 atomic_set(&orig_entry
->refcount
, 2);
713 spin_lock_bh(&tt_global
->list_lock
);
714 hlist_add_head_rcu(&orig_entry
->list
,
715 &tt_global
->orig_list
);
716 spin_unlock_bh(&tt_global
->list_lock
);
719 batadv_tt_orig_list_entry_free_ref(orig_entry
);
722 /* caller must hold orig_node refcount */
723 int batadv_tt_global_add(struct batadv_priv
*bat_priv
,
724 struct batadv_orig_node
*orig_node
,
725 const unsigned char *tt_addr
, uint8_t flags
,
728 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
731 struct batadv_tt_common_entry
*common
;
732 uint16_t local_flags
;
734 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, tt_addr
);
736 if (!tt_global_entry
) {
737 tt_global_entry
= kzalloc(sizeof(*tt_global_entry
), GFP_ATOMIC
);
738 if (!tt_global_entry
)
741 common
= &tt_global_entry
->common
;
742 memcpy(common
->addr
, tt_addr
, ETH_ALEN
);
744 common
->flags
= flags
;
745 tt_global_entry
->roam_at
= 0;
746 /* node must store current time in case of roaming. This is
747 * needed to purge this entry out on timeout (if nobody claims
750 if (flags
& BATADV_TT_CLIENT_ROAM
)
751 tt_global_entry
->roam_at
= jiffies
;
752 atomic_set(&common
->refcount
, 2);
753 common
->added_at
= jiffies
;
755 INIT_HLIST_HEAD(&tt_global_entry
->orig_list
);
756 spin_lock_init(&tt_global_entry
->list_lock
);
758 hash_added
= batadv_hash_add(bat_priv
->tt
.global_hash
,
760 batadv_choose_orig
, common
,
761 &common
->hash_entry
);
763 if (unlikely(hash_added
!= 0)) {
764 /* remove the reference for the hash */
765 batadv_tt_global_entry_free_ref(tt_global_entry
);
769 /* If there is already a global entry, we can use this one for
771 * But if we are trying to add a temporary client we can exit
772 * directly because the temporary information should never
773 * override any already known client state (whatever it is)
775 if (flags
& BATADV_TT_CLIENT_TEMP
)
778 /* if the client was temporary added before receiving the first
779 * OGM announcing it, we have to clear the TEMP flag
781 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_TEMP
;
783 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
784 * one originator left in the list and we previously received a
785 * delete + roaming change for this originator.
787 * We should first delete the old originator before adding the
790 if (tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
) {
791 batadv_tt_global_del_orig_list(tt_global_entry
);
792 tt_global_entry
->common
.flags
&= ~BATADV_TT_CLIENT_ROAM
;
793 tt_global_entry
->roam_at
= 0;
796 /* add the new orig_entry (if needed) or update it */
797 batadv_tt_global_orig_entry_add(tt_global_entry
, orig_node
, ttvn
);
799 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
800 "Creating new global tt entry: %pM (via %pM)\n",
801 tt_global_entry
->common
.addr
, orig_node
->orig
);
806 /* remove address from local hash if present */
807 local_flags
= batadv_tt_local_remove(bat_priv
, tt_addr
,
808 "global tt received",
809 flags
& BATADV_TT_CLIENT_ROAM
);
810 tt_global_entry
->common
.flags
|= local_flags
& BATADV_TT_CLIENT_WIFI
;
814 batadv_tt_global_entry_free_ref(tt_global_entry
);
818 /* print all orig nodes who announce the address for this global entry.
819 * it is assumed that the caller holds rcu_read_lock();
822 batadv_tt_global_print_entry(struct batadv_tt_global_entry
*tt_global_entry
,
823 struct seq_file
*seq
)
825 struct hlist_head
*head
;
826 struct hlist_node
*node
;
827 struct batadv_tt_orig_list_entry
*orig_entry
;
828 struct batadv_tt_common_entry
*tt_common_entry
;
832 tt_common_entry
= &tt_global_entry
->common
;
834 head
= &tt_global_entry
->orig_list
;
836 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
837 flags
= tt_common_entry
->flags
;
838 last_ttvn
= atomic_read(&orig_entry
->orig_node
->last_ttvn
);
839 seq_printf(seq
, " * %pM (%3u) via %pM (%3u) [%c%c%c]\n",
840 tt_global_entry
->common
.addr
, orig_entry
->ttvn
,
841 orig_entry
->orig_node
->orig
, last_ttvn
,
842 (flags
& BATADV_TT_CLIENT_ROAM
? 'R' : '.'),
843 (flags
& BATADV_TT_CLIENT_WIFI
? 'W' : '.'),
844 (flags
& BATADV_TT_CLIENT_TEMP
? 'T' : '.'));
848 int batadv_tt_global_seq_print_text(struct seq_file
*seq
, void *offset
)
850 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
851 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
852 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
853 struct batadv_tt_common_entry
*tt_common_entry
;
854 struct batadv_tt_global_entry
*tt_global
;
855 struct batadv_hard_iface
*primary_if
;
856 struct hlist_node
*node
;
857 struct hlist_head
*head
;
860 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
865 "Globally announced TT entries received via the mesh %s\n",
867 seq_printf(seq
, " %-13s %s %-15s %s %s\n",
868 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
870 for (i
= 0; i
< hash
->size
; i
++) {
871 head
= &hash
->table
[i
];
874 hlist_for_each_entry_rcu(tt_common_entry
, node
,
876 tt_global
= container_of(tt_common_entry
,
877 struct batadv_tt_global_entry
,
879 batadv_tt_global_print_entry(tt_global
, seq
);
885 batadv_hardif_free_ref(primary_if
);
889 /* deletes the orig list of a tt_global_entry */
891 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry
*tt_global_entry
)
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
) {
901 batadv_tt_orig_list_entry_free_ref(orig_entry
);
903 spin_unlock_bh(&tt_global_entry
->list_lock
);
908 batadv_tt_global_del_orig_entry(struct batadv_priv
*bat_priv
,
909 struct batadv_tt_global_entry
*tt_global_entry
,
910 struct batadv_orig_node
*orig_node
,
913 struct hlist_head
*head
;
914 struct hlist_node
*node
, *safe
;
915 struct batadv_tt_orig_list_entry
*orig_entry
;
917 spin_lock_bh(&tt_global_entry
->list_lock
);
918 head
= &tt_global_entry
->orig_list
;
919 hlist_for_each_entry_safe(orig_entry
, node
, safe
, head
, list
) {
920 if (orig_entry
->orig_node
== orig_node
) {
921 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
922 "Deleting %pM from global tt entry %pM: %s\n",
924 tt_global_entry
->common
.addr
, message
);
926 batadv_tt_orig_list_entry_free_ref(orig_entry
);
929 spin_unlock_bh(&tt_global_entry
->list_lock
);
933 batadv_tt_global_del_struct(struct batadv_priv
*bat_priv
,
934 struct batadv_tt_global_entry
*tt_global_entry
,
937 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
938 "Deleting global tt entry %pM: %s\n",
939 tt_global_entry
->common
.addr
, message
);
941 batadv_hash_remove(bat_priv
->tt
.global_hash
, batadv_compare_tt
,
942 batadv_choose_orig
, tt_global_entry
->common
.addr
);
943 batadv_tt_global_entry_free_ref(tt_global_entry
);
947 /* If the client is to be deleted, we check if it is the last origantor entry
948 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
949 * timer, otherwise we simply remove the originator scheduled for deletion.
952 batadv_tt_global_del_roaming(struct batadv_priv
*bat_priv
,
953 struct batadv_tt_global_entry
*tt_global_entry
,
954 struct batadv_orig_node
*orig_node
,
957 bool last_entry
= true;
958 struct hlist_head
*head
;
959 struct hlist_node
*node
;
960 struct batadv_tt_orig_list_entry
*orig_entry
;
962 /* no local entry exists, case 1:
963 * Check if this is the last one or if other entries exist.
967 head
= &tt_global_entry
->orig_list
;
968 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
969 if (orig_entry
->orig_node
!= orig_node
) {
977 /* its the last one, mark for roaming. */
978 tt_global_entry
->common
.flags
|= BATADV_TT_CLIENT_ROAM
;
979 tt_global_entry
->roam_at
= jiffies
;
981 /* there is another entry, we can simply delete this
982 * one and can still use the other one.
984 batadv_tt_global_del_orig_entry(bat_priv
, tt_global_entry
,
990 static void batadv_tt_global_del(struct batadv_priv
*bat_priv
,
991 struct batadv_orig_node
*orig_node
,
992 const unsigned char *addr
,
993 const char *message
, bool roaming
)
995 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
996 struct batadv_tt_local_entry
*local_entry
= NULL
;
998 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
999 if (!tt_global_entry
)
1003 batadv_tt_global_del_orig_entry(bat_priv
, tt_global_entry
,
1004 orig_node
, message
);
1006 if (hlist_empty(&tt_global_entry
->orig_list
))
1007 batadv_tt_global_del_struct(bat_priv
, tt_global_entry
,
1013 /* if we are deleting a global entry due to a roam
1014 * event, there are two possibilities:
1015 * 1) the client roamed from node A to node B => if there
1016 * is only one originator left for this client, we mark
1017 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1018 * wait for node B to claim it. In case of timeout
1019 * the entry is purged.
1021 * If there are other originators left, we directly delete
1023 * 2) the client roamed to us => we can directly delete
1024 * the global entry, since it is useless now.
1026 local_entry
= batadv_tt_local_hash_find(bat_priv
,
1027 tt_global_entry
->common
.addr
);
1029 /* local entry exists, case 2: client roamed to us. */
1030 batadv_tt_global_del_orig_list(tt_global_entry
);
1031 batadv_tt_global_del_struct(bat_priv
, tt_global_entry
, message
);
1033 /* no local entry exists, case 1: check for roaming */
1034 batadv_tt_global_del_roaming(bat_priv
, tt_global_entry
,
1035 orig_node
, message
);
1039 if (tt_global_entry
)
1040 batadv_tt_global_entry_free_ref(tt_global_entry
);
1042 batadv_tt_local_entry_free_ref(local_entry
);
1045 void batadv_tt_global_del_orig(struct batadv_priv
*bat_priv
,
1046 struct batadv_orig_node
*orig_node
,
1047 const char *message
)
1049 struct batadv_tt_global_entry
*tt_global
;
1050 struct batadv_tt_common_entry
*tt_common_entry
;
1052 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1053 struct hlist_node
*node
, *safe
;
1054 struct hlist_head
*head
;
1055 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1060 for (i
= 0; i
< hash
->size
; i
++) {
1061 head
= &hash
->table
[i
];
1062 list_lock
= &hash
->list_locks
[i
];
1064 spin_lock_bh(list_lock
);
1065 hlist_for_each_entry_safe(tt_common_entry
, node
, safe
,
1067 tt_global
= container_of(tt_common_entry
,
1068 struct batadv_tt_global_entry
,
1071 batadv_tt_global_del_orig_entry(bat_priv
, tt_global
,
1072 orig_node
, message
);
1074 if (hlist_empty(&tt_global
->orig_list
)) {
1075 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1076 "Deleting global tt entry %pM: %s\n",
1077 tt_global
->common
.addr
, message
);
1078 hlist_del_rcu(node
);
1079 batadv_tt_global_entry_free_ref(tt_global
);
1082 spin_unlock_bh(list_lock
);
1084 orig_node
->tt_initialised
= false;
1087 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry
*tt_global
,
1091 unsigned long roam_timeout
= BATADV_TT_CLIENT_ROAM_TIMEOUT
;
1092 unsigned long temp_timeout
= BATADV_TT_CLIENT_TEMP_TIMEOUT
;
1094 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_ROAM
) &&
1095 batadv_has_timed_out(tt_global
->roam_at
, roam_timeout
)) {
1097 *msg
= "Roaming timeout\n";
1100 if ((tt_global
->common
.flags
& BATADV_TT_CLIENT_TEMP
) &&
1101 batadv_has_timed_out(tt_global
->common
.added_at
, temp_timeout
)) {
1103 *msg
= "Temporary client timeout\n";
1109 static void batadv_tt_global_purge(struct batadv_priv
*bat_priv
)
1111 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1112 struct hlist_head
*head
;
1113 struct hlist_node
*node
, *node_tmp
;
1114 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1117 struct batadv_tt_common_entry
*tt_common
;
1118 struct batadv_tt_global_entry
*tt_global
;
1120 for (i
= 0; i
< hash
->size
; i
++) {
1121 head
= &hash
->table
[i
];
1122 list_lock
= &hash
->list_locks
[i
];
1124 spin_lock_bh(list_lock
);
1125 hlist_for_each_entry_safe(tt_common
, node
, node_tmp
, head
,
1127 tt_global
= container_of(tt_common
,
1128 struct batadv_tt_global_entry
,
1131 if (!batadv_tt_global_to_purge(tt_global
, &msg
))
1134 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1135 "Deleting global tt entry (%pM): %s\n",
1136 tt_global
->common
.addr
, msg
);
1138 hlist_del_rcu(node
);
1140 batadv_tt_global_entry_free_ref(tt_global
);
1142 spin_unlock_bh(list_lock
);
1146 static void batadv_tt_global_table_free(struct batadv_priv
*bat_priv
)
1148 struct batadv_hashtable
*hash
;
1149 spinlock_t
*list_lock
; /* protects write access to the hash lists */
1150 struct batadv_tt_common_entry
*tt_common_entry
;
1151 struct batadv_tt_global_entry
*tt_global
;
1152 struct hlist_node
*node
, *node_tmp
;
1153 struct hlist_head
*head
;
1156 if (!bat_priv
->tt
.global_hash
)
1159 hash
= bat_priv
->tt
.global_hash
;
1161 for (i
= 0; i
< hash
->size
; i
++) {
1162 head
= &hash
->table
[i
];
1163 list_lock
= &hash
->list_locks
[i
];
1165 spin_lock_bh(list_lock
);
1166 hlist_for_each_entry_safe(tt_common_entry
, node
, node_tmp
,
1168 hlist_del_rcu(node
);
1169 tt_global
= container_of(tt_common_entry
,
1170 struct batadv_tt_global_entry
,
1172 batadv_tt_global_entry_free_ref(tt_global
);
1174 spin_unlock_bh(list_lock
);
1177 batadv_hash_destroy(hash
);
1179 bat_priv
->tt
.global_hash
= NULL
;
1183 _batadv_is_ap_isolated(struct batadv_tt_local_entry
*tt_local_entry
,
1184 struct batadv_tt_global_entry
*tt_global_entry
)
1188 if (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
&&
1189 tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_WIFI
)
1195 struct batadv_orig_node
*batadv_transtable_search(struct batadv_priv
*bat_priv
,
1197 const uint8_t *addr
)
1199 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
1200 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
1201 struct batadv_orig_node
*orig_node
= NULL
;
1202 struct batadv_neigh_node
*router
= NULL
;
1203 struct hlist_head
*head
;
1204 struct hlist_node
*node
;
1205 struct batadv_tt_orig_list_entry
*orig_entry
;
1208 if (src
&& atomic_read(&bat_priv
->ap_isolation
)) {
1209 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, src
);
1210 if (!tt_local_entry
)
1214 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
1215 if (!tt_global_entry
)
1218 /* check whether the clients should not communicate due to AP
1221 if (tt_local_entry
&&
1222 _batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
1228 head
= &tt_global_entry
->orig_list
;
1229 hlist_for_each_entry_rcu(orig_entry
, node
, head
, list
) {
1230 router
= batadv_orig_node_get_router(orig_entry
->orig_node
);
1234 if (router
->tq_avg
> best_tq
) {
1235 orig_node
= orig_entry
->orig_node
;
1236 best_tq
= router
->tq_avg
;
1238 batadv_neigh_node_free_ref(router
);
1240 /* found anything? */
1241 if (orig_node
&& !atomic_inc_not_zero(&orig_node
->refcount
))
1245 if (tt_global_entry
)
1246 batadv_tt_global_entry_free_ref(tt_global_entry
);
1248 batadv_tt_local_entry_free_ref(tt_local_entry
);
1253 /* Calculates the checksum of the local table of a given orig_node */
1254 static uint16_t batadv_tt_global_crc(struct batadv_priv
*bat_priv
,
1255 struct batadv_orig_node
*orig_node
)
1257 uint16_t total
= 0, total_one
;
1258 struct batadv_hashtable
*hash
= bat_priv
->tt
.global_hash
;
1259 struct batadv_tt_common_entry
*tt_common
;
1260 struct batadv_tt_global_entry
*tt_global
;
1261 struct hlist_node
*node
;
1262 struct hlist_head
*head
;
1266 for (i
= 0; i
< hash
->size
; i
++) {
1267 head
= &hash
->table
[i
];
1270 hlist_for_each_entry_rcu(tt_common
, node
, head
, hash_entry
) {
1271 tt_global
= container_of(tt_common
,
1272 struct batadv_tt_global_entry
,
1274 /* Roaming clients are in the global table for
1275 * consistency only. They don't have to be
1276 * taken into account while computing the
1279 if (tt_common
->flags
& BATADV_TT_CLIENT_ROAM
)
1281 /* Temporary clients have not been announced yet, so
1282 * they have to be skipped while computing the global
1285 if (tt_common
->flags
& BATADV_TT_CLIENT_TEMP
)
1288 /* find out if this global entry is announced by this
1291 if (!batadv_tt_global_entry_has_orig(tt_global
,
1296 for (j
= 0; j
< ETH_ALEN
; j
++)
1297 total_one
= crc16_byte(total_one
,
1298 tt_common
->addr
[j
]);
1307 /* Calculates the checksum of the local table */
1308 static uint16_t batadv_tt_local_crc(struct batadv_priv
*bat_priv
)
1310 uint16_t total
= 0, total_one
;
1311 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
1312 struct batadv_tt_common_entry
*tt_common
;
1313 struct hlist_node
*node
;
1314 struct hlist_head
*head
;
1318 for (i
= 0; i
< hash
->size
; i
++) {
1319 head
= &hash
->table
[i
];
1322 hlist_for_each_entry_rcu(tt_common
, node
, head
, hash_entry
) {
1323 /* not yet committed clients have not to be taken into
1324 * account while computing the CRC
1326 if (tt_common
->flags
& BATADV_TT_CLIENT_NEW
)
1329 for (j
= 0; j
< ETH_ALEN
; j
++)
1330 total_one
= crc16_byte(total_one
,
1331 tt_common
->addr
[j
]);
1340 static void batadv_tt_req_list_free(struct batadv_priv
*bat_priv
)
1342 struct batadv_tt_req_node
*node
, *safe
;
1344 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1346 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
1347 list_del(&node
->list
);
1351 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1354 static void batadv_tt_save_orig_buffer(struct batadv_priv
*bat_priv
,
1355 struct batadv_orig_node
*orig_node
,
1356 const unsigned char *tt_buff
,
1357 uint8_t tt_num_changes
)
1359 uint16_t tt_buff_len
= batadv_tt_len(tt_num_changes
);
1361 /* Replace the old buffer only if I received something in the
1362 * last OGM (the OGM could carry no changes)
1364 spin_lock_bh(&orig_node
->tt_buff_lock
);
1365 if (tt_buff_len
> 0) {
1366 kfree(orig_node
->tt_buff
);
1367 orig_node
->tt_buff_len
= 0;
1368 orig_node
->tt_buff
= kmalloc(tt_buff_len
, GFP_ATOMIC
);
1369 if (orig_node
->tt_buff
) {
1370 memcpy(orig_node
->tt_buff
, tt_buff
, tt_buff_len
);
1371 orig_node
->tt_buff_len
= tt_buff_len
;
1374 spin_unlock_bh(&orig_node
->tt_buff_lock
);
1377 static void batadv_tt_req_purge(struct batadv_priv
*bat_priv
)
1379 struct batadv_tt_req_node
*node
, *safe
;
1381 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1382 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
1383 if (batadv_has_timed_out(node
->issued_at
,
1384 BATADV_TT_REQUEST_TIMEOUT
)) {
1385 list_del(&node
->list
);
1389 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1392 /* returns the pointer to the new tt_req_node struct if no request
1393 * has already been issued for this orig_node, NULL otherwise
1395 static struct batadv_tt_req_node
*
1396 batadv_new_tt_req_node(struct batadv_priv
*bat_priv
,
1397 struct batadv_orig_node
*orig_node
)
1399 struct batadv_tt_req_node
*tt_req_node_tmp
, *tt_req_node
= NULL
;
1401 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1402 list_for_each_entry(tt_req_node_tmp
, &bat_priv
->tt
.req_list
, list
) {
1403 if (batadv_compare_eth(tt_req_node_tmp
, orig_node
) &&
1404 !batadv_has_timed_out(tt_req_node_tmp
->issued_at
,
1405 BATADV_TT_REQUEST_TIMEOUT
))
1409 tt_req_node
= kmalloc(sizeof(*tt_req_node
), GFP_ATOMIC
);
1413 memcpy(tt_req_node
->addr
, orig_node
->orig
, ETH_ALEN
);
1414 tt_req_node
->issued_at
= jiffies
;
1416 list_add(&tt_req_node
->list
, &bat_priv
->tt
.req_list
);
1418 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1422 /* data_ptr is useless here, but has to be kept to respect the prototype */
1423 static int batadv_tt_local_valid_entry(const void *entry_ptr
,
1424 const void *data_ptr
)
1426 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
1428 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_NEW
)
1433 static int batadv_tt_global_valid(const void *entry_ptr
,
1434 const void *data_ptr
)
1436 const struct batadv_tt_common_entry
*tt_common_entry
= entry_ptr
;
1437 const struct batadv_tt_global_entry
*tt_global_entry
;
1438 const struct batadv_orig_node
*orig_node
= data_ptr
;
1440 if (tt_common_entry
->flags
& BATADV_TT_CLIENT_ROAM
||
1441 tt_common_entry
->flags
& BATADV_TT_CLIENT_TEMP
)
1444 tt_global_entry
= container_of(tt_common_entry
,
1445 struct batadv_tt_global_entry
,
1448 return batadv_tt_global_entry_has_orig(tt_global_entry
, orig_node
);
1451 static struct sk_buff
*
1452 batadv_tt_response_fill_table(uint16_t tt_len
, uint8_t ttvn
,
1453 struct batadv_hashtable
*hash
,
1454 struct batadv_hard_iface
*primary_if
,
1455 int (*valid_cb
)(const void *, const void *),
1458 struct batadv_tt_common_entry
*tt_common_entry
;
1459 struct batadv_tt_query_packet
*tt_response
;
1460 struct batadv_tt_change
*tt_change
;
1461 struct hlist_node
*node
;
1462 struct hlist_head
*head
;
1463 struct sk_buff
*skb
= NULL
;
1464 uint16_t tt_tot
, tt_count
;
1465 ssize_t tt_query_size
= sizeof(struct batadv_tt_query_packet
);
1469 if (tt_query_size
+ tt_len
> primary_if
->soft_iface
->mtu
) {
1470 tt_len
= primary_if
->soft_iface
->mtu
- tt_query_size
;
1471 tt_len
-= tt_len
% sizeof(struct batadv_tt_change
);
1473 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1475 len
= tt_query_size
+ tt_len
;
1476 skb
= dev_alloc_skb(len
+ ETH_HLEN
+ NET_IP_ALIGN
);
1480 skb_reserve(skb
, ETH_HLEN
+ NET_IP_ALIGN
);
1481 tt_response
= (struct batadv_tt_query_packet
*)skb_put(skb
, len
);
1482 tt_response
->ttvn
= ttvn
;
1484 tt_change
= (struct batadv_tt_change
*)(skb
->data
+ tt_query_size
);
1488 for (i
= 0; i
< hash
->size
; i
++) {
1489 head
= &hash
->table
[i
];
1491 hlist_for_each_entry_rcu(tt_common_entry
, node
,
1493 if (tt_count
== tt_tot
)
1496 if ((valid_cb
) && (!valid_cb(tt_common_entry
, cb_data
)))
1499 memcpy(tt_change
->addr
, tt_common_entry
->addr
,
1501 tt_change
->flags
= BATADV_NO_FLAGS
;
1509 /* store in the message the number of entries we have successfully
1512 tt_response
->tt_data
= htons(tt_count
);
1518 static int batadv_send_tt_request(struct batadv_priv
*bat_priv
,
1519 struct batadv_orig_node
*dst_orig_node
,
1520 uint8_t ttvn
, uint16_t tt_crc
,
1523 struct sk_buff
*skb
= NULL
;
1524 struct batadv_tt_query_packet
*tt_request
;
1525 struct batadv_neigh_node
*neigh_node
= NULL
;
1526 struct batadv_hard_iface
*primary_if
;
1527 struct batadv_tt_req_node
*tt_req_node
= NULL
;
1531 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1535 /* The new tt_req will be issued only if I'm not waiting for a
1536 * reply from the same orig_node yet
1538 tt_req_node
= batadv_new_tt_req_node(bat_priv
, dst_orig_node
);
1542 skb
= dev_alloc_skb(sizeof(*tt_request
) + ETH_HLEN
+ NET_IP_ALIGN
);
1546 skb_reserve(skb
, ETH_HLEN
+ NET_IP_ALIGN
);
1548 tt_req_len
= sizeof(*tt_request
);
1549 tt_request
= (struct batadv_tt_query_packet
*)skb_put(skb
, tt_req_len
);
1551 tt_request
->header
.packet_type
= BATADV_TT_QUERY
;
1552 tt_request
->header
.version
= BATADV_COMPAT_VERSION
;
1553 memcpy(tt_request
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1554 memcpy(tt_request
->dst
, dst_orig_node
->orig
, ETH_ALEN
);
1555 tt_request
->header
.ttl
= BATADV_TTL
;
1556 tt_request
->ttvn
= ttvn
;
1557 tt_request
->tt_data
= htons(tt_crc
);
1558 tt_request
->flags
= BATADV_TT_REQUEST
;
1561 tt_request
->flags
|= BATADV_TT_FULL_TABLE
;
1563 neigh_node
= batadv_orig_node_get_router(dst_orig_node
);
1567 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1568 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1569 dst_orig_node
->orig
, neigh_node
->addr
,
1570 (full_table
? 'F' : '.'));
1572 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_REQUEST_TX
);
1574 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1579 batadv_neigh_node_free_ref(neigh_node
);
1581 batadv_hardif_free_ref(primary_if
);
1584 if (ret
&& tt_req_node
) {
1585 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1586 list_del(&tt_req_node
->list
);
1587 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1594 batadv_send_other_tt_response(struct batadv_priv
*bat_priv
,
1595 struct batadv_tt_query_packet
*tt_request
)
1597 struct batadv_orig_node
*req_dst_orig_node
= NULL
;
1598 struct batadv_orig_node
*res_dst_orig_node
= NULL
;
1599 struct batadv_neigh_node
*neigh_node
= NULL
;
1600 struct batadv_hard_iface
*primary_if
= NULL
;
1601 uint8_t orig_ttvn
, req_ttvn
, ttvn
;
1603 unsigned char *tt_buff
;
1605 uint16_t tt_len
, tt_tot
;
1606 struct sk_buff
*skb
= NULL
;
1607 struct batadv_tt_query_packet
*tt_response
;
1608 uint8_t *packet_pos
;
1611 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1612 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1613 tt_request
->src
, tt_request
->ttvn
, tt_request
->dst
,
1614 (tt_request
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1616 /* Let's get the orig node of the REAL destination */
1617 req_dst_orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->dst
);
1618 if (!req_dst_orig_node
)
1621 res_dst_orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->src
);
1622 if (!res_dst_orig_node
)
1625 neigh_node
= batadv_orig_node_get_router(res_dst_orig_node
);
1629 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1633 orig_ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1634 req_ttvn
= tt_request
->ttvn
;
1636 /* I don't have the requested data */
1637 if (orig_ttvn
!= req_ttvn
||
1638 tt_request
->tt_data
!= htons(req_dst_orig_node
->tt_crc
))
1641 /* If the full table has been explicitly requested */
1642 if (tt_request
->flags
& BATADV_TT_FULL_TABLE
||
1643 !req_dst_orig_node
->tt_buff
)
1648 /* In this version, fragmentation is not implemented, then
1649 * I'll send only one packet with as much TT entries as I can
1652 spin_lock_bh(&req_dst_orig_node
->tt_buff_lock
);
1653 tt_len
= req_dst_orig_node
->tt_buff_len
;
1654 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1656 len
= sizeof(*tt_response
) + tt_len
;
1657 skb
= dev_alloc_skb(len
+ ETH_HLEN
+ NET_IP_ALIGN
);
1661 skb_reserve(skb
, ETH_HLEN
+ NET_IP_ALIGN
);
1662 packet_pos
= skb_put(skb
, len
);
1663 tt_response
= (struct batadv_tt_query_packet
*)packet_pos
;
1664 tt_response
->ttvn
= req_ttvn
;
1665 tt_response
->tt_data
= htons(tt_tot
);
1667 tt_buff
= skb
->data
+ sizeof(*tt_response
);
1668 /* Copy the last orig_node's OGM buffer */
1669 memcpy(tt_buff
, req_dst_orig_node
->tt_buff
,
1670 req_dst_orig_node
->tt_buff_len
);
1672 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1674 tt_len
= (uint16_t)atomic_read(&req_dst_orig_node
->tt_size
);
1675 tt_len
*= sizeof(struct batadv_tt_change
);
1676 ttvn
= (uint8_t)atomic_read(&req_dst_orig_node
->last_ttvn
);
1678 skb
= batadv_tt_response_fill_table(tt_len
, ttvn
,
1679 bat_priv
->tt
.global_hash
,
1681 batadv_tt_global_valid
,
1686 tt_response
= (struct batadv_tt_query_packet
*)skb
->data
;
1689 tt_response
->header
.packet_type
= BATADV_TT_QUERY
;
1690 tt_response
->header
.version
= BATADV_COMPAT_VERSION
;
1691 tt_response
->header
.ttl
= BATADV_TTL
;
1692 memcpy(tt_response
->src
, req_dst_orig_node
->orig
, ETH_ALEN
);
1693 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1694 tt_response
->flags
= BATADV_TT_RESPONSE
;
1697 tt_response
->flags
|= BATADV_TT_FULL_TABLE
;
1699 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1700 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1701 res_dst_orig_node
->orig
, neigh_node
->addr
,
1702 req_dst_orig_node
->orig
, req_ttvn
);
1704 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
1706 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1711 spin_unlock_bh(&req_dst_orig_node
->tt_buff_lock
);
1714 if (res_dst_orig_node
)
1715 batadv_orig_node_free_ref(res_dst_orig_node
);
1716 if (req_dst_orig_node
)
1717 batadv_orig_node_free_ref(req_dst_orig_node
);
1719 batadv_neigh_node_free_ref(neigh_node
);
1721 batadv_hardif_free_ref(primary_if
);
1729 batadv_send_my_tt_response(struct batadv_priv
*bat_priv
,
1730 struct batadv_tt_query_packet
*tt_request
)
1732 struct batadv_orig_node
*orig_node
= NULL
;
1733 struct batadv_neigh_node
*neigh_node
= NULL
;
1734 struct batadv_hard_iface
*primary_if
= NULL
;
1735 uint8_t my_ttvn
, req_ttvn
, ttvn
;
1737 unsigned char *tt_buff
;
1739 uint16_t tt_len
, tt_tot
;
1740 struct sk_buff
*skb
= NULL
;
1741 struct batadv_tt_query_packet
*tt_response
;
1742 uint8_t *packet_pos
;
1745 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1746 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1747 tt_request
->src
, tt_request
->ttvn
,
1748 (tt_request
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1751 my_ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
1752 req_ttvn
= tt_request
->ttvn
;
1754 orig_node
= batadv_orig_hash_find(bat_priv
, tt_request
->src
);
1758 neigh_node
= batadv_orig_node_get_router(orig_node
);
1762 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1766 /* If the full table has been explicitly requested or the gap
1767 * is too big send the whole local translation table
1769 if (tt_request
->flags
& BATADV_TT_FULL_TABLE
|| my_ttvn
!= req_ttvn
||
1770 !bat_priv
->tt
.last_changeset
)
1775 /* In this version, fragmentation is not implemented, then
1776 * I'll send only one packet with as much TT entries as I can
1779 spin_lock_bh(&bat_priv
->tt
.last_changeset_lock
);
1780 tt_len
= bat_priv
->tt
.last_changeset_len
;
1781 tt_tot
= tt_len
/ sizeof(struct batadv_tt_change
);
1783 len
= sizeof(*tt_response
) + tt_len
;
1784 skb
= dev_alloc_skb(len
+ ETH_HLEN
+ NET_IP_ALIGN
);
1788 skb_reserve(skb
, ETH_HLEN
+ NET_IP_ALIGN
);
1789 packet_pos
= skb_put(skb
, len
);
1790 tt_response
= (struct batadv_tt_query_packet
*)packet_pos
;
1791 tt_response
->ttvn
= req_ttvn
;
1792 tt_response
->tt_data
= htons(tt_tot
);
1794 tt_buff
= skb
->data
+ sizeof(*tt_response
);
1795 memcpy(tt_buff
, bat_priv
->tt
.last_changeset
,
1796 bat_priv
->tt
.last_changeset_len
);
1797 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
1799 tt_len
= (uint16_t)atomic_read(&bat_priv
->tt
.local_entry_num
);
1800 tt_len
*= sizeof(struct batadv_tt_change
);
1801 ttvn
= (uint8_t)atomic_read(&bat_priv
->tt
.vn
);
1803 skb
= batadv_tt_response_fill_table(tt_len
, ttvn
,
1804 bat_priv
->tt
.local_hash
,
1806 batadv_tt_local_valid_entry
,
1811 tt_response
= (struct batadv_tt_query_packet
*)skb
->data
;
1814 tt_response
->header
.packet_type
= BATADV_TT_QUERY
;
1815 tt_response
->header
.version
= BATADV_COMPAT_VERSION
;
1816 tt_response
->header
.ttl
= BATADV_TTL
;
1817 memcpy(tt_response
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
1818 memcpy(tt_response
->dst
, tt_request
->src
, ETH_ALEN
);
1819 tt_response
->flags
= BATADV_TT_RESPONSE
;
1822 tt_response
->flags
|= BATADV_TT_FULL_TABLE
;
1824 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1825 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1826 orig_node
->orig
, neigh_node
->addr
,
1827 (tt_response
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1829 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_RESPONSE_TX
);
1831 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
1836 spin_unlock_bh(&bat_priv
->tt
.last_changeset_lock
);
1839 batadv_orig_node_free_ref(orig_node
);
1841 batadv_neigh_node_free_ref(neigh_node
);
1843 batadv_hardif_free_ref(primary_if
);
1846 /* This packet was for me, so it doesn't need to be re-routed */
1850 bool batadv_send_tt_response(struct batadv_priv
*bat_priv
,
1851 struct batadv_tt_query_packet
*tt_request
)
1853 if (batadv_is_my_mac(tt_request
->dst
)) {
1854 /* don't answer backbone gws! */
1855 if (batadv_bla_is_backbone_gw_orig(bat_priv
, tt_request
->src
))
1858 return batadv_send_my_tt_response(bat_priv
, tt_request
);
1860 return batadv_send_other_tt_response(bat_priv
, tt_request
);
1864 static void _batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
1865 struct batadv_orig_node
*orig_node
,
1866 struct batadv_tt_change
*tt_change
,
1867 uint16_t tt_num_changes
, uint8_t ttvn
)
1872 for (i
= 0; i
< tt_num_changes
; i
++) {
1873 if ((tt_change
+ i
)->flags
& BATADV_TT_CLIENT_DEL
) {
1874 roams
= (tt_change
+ i
)->flags
& BATADV_TT_CLIENT_ROAM
;
1875 batadv_tt_global_del(bat_priv
, orig_node
,
1876 (tt_change
+ i
)->addr
,
1877 "tt removed by changes",
1880 if (!batadv_tt_global_add(bat_priv
, orig_node
,
1881 (tt_change
+ i
)->addr
,
1882 (tt_change
+ i
)->flags
, ttvn
))
1883 /* In case of problem while storing a
1884 * global_entry, we stop the updating
1885 * procedure without committing the
1886 * ttvn change. This will avoid to send
1887 * corrupted data on tt_request
1892 orig_node
->tt_initialised
= true;
1895 static void batadv_tt_fill_gtable(struct batadv_priv
*bat_priv
,
1896 struct batadv_tt_query_packet
*tt_response
)
1898 struct batadv_orig_node
*orig_node
= NULL
;
1900 orig_node
= batadv_orig_hash_find(bat_priv
, tt_response
->src
);
1904 /* Purge the old table first.. */
1905 batadv_tt_global_del_orig(bat_priv
, orig_node
, "Received full table");
1907 _batadv_tt_update_changes(bat_priv
, orig_node
,
1908 (struct batadv_tt_change
*)(tt_response
+ 1),
1909 ntohs(tt_response
->tt_data
),
1912 spin_lock_bh(&orig_node
->tt_buff_lock
);
1913 kfree(orig_node
->tt_buff
);
1914 orig_node
->tt_buff_len
= 0;
1915 orig_node
->tt_buff
= NULL
;
1916 spin_unlock_bh(&orig_node
->tt_buff_lock
);
1918 atomic_set(&orig_node
->last_ttvn
, tt_response
->ttvn
);
1922 batadv_orig_node_free_ref(orig_node
);
1925 static void batadv_tt_update_changes(struct batadv_priv
*bat_priv
,
1926 struct batadv_orig_node
*orig_node
,
1927 uint16_t tt_num_changes
, uint8_t ttvn
,
1928 struct batadv_tt_change
*tt_change
)
1930 _batadv_tt_update_changes(bat_priv
, orig_node
, tt_change
,
1931 tt_num_changes
, ttvn
);
1933 batadv_tt_save_orig_buffer(bat_priv
, orig_node
,
1934 (unsigned char *)tt_change
, tt_num_changes
);
1935 atomic_set(&orig_node
->last_ttvn
, ttvn
);
1938 bool batadv_is_my_client(struct batadv_priv
*bat_priv
, const uint8_t *addr
)
1940 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
1943 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
1944 if (!tt_local_entry
)
1946 /* Check if the client has been logically deleted (but is kept for
1947 * consistency purpose)
1949 if ((tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_PENDING
) ||
1950 (tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
))
1955 batadv_tt_local_entry_free_ref(tt_local_entry
);
1959 void batadv_handle_tt_response(struct batadv_priv
*bat_priv
,
1960 struct batadv_tt_query_packet
*tt_response
)
1962 struct batadv_tt_req_node
*node
, *safe
;
1963 struct batadv_orig_node
*orig_node
= NULL
;
1964 struct batadv_tt_change
*tt_change
;
1966 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
1967 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
1968 tt_response
->src
, tt_response
->ttvn
,
1969 ntohs(tt_response
->tt_data
),
1970 (tt_response
->flags
& BATADV_TT_FULL_TABLE
? 'F' : '.'));
1972 /* we should have never asked a backbone gw */
1973 if (batadv_bla_is_backbone_gw_orig(bat_priv
, tt_response
->src
))
1976 orig_node
= batadv_orig_hash_find(bat_priv
, tt_response
->src
);
1980 if (tt_response
->flags
& BATADV_TT_FULL_TABLE
) {
1981 batadv_tt_fill_gtable(bat_priv
, tt_response
);
1983 tt_change
= (struct batadv_tt_change
*)(tt_response
+ 1);
1984 batadv_tt_update_changes(bat_priv
, orig_node
,
1985 ntohs(tt_response
->tt_data
),
1986 tt_response
->ttvn
, tt_change
);
1989 /* Delete the tt_req_node from pending tt_requests list */
1990 spin_lock_bh(&bat_priv
->tt
.req_list_lock
);
1991 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.req_list
, list
) {
1992 if (!batadv_compare_eth(node
->addr
, tt_response
->src
))
1994 list_del(&node
->list
);
1997 spin_unlock_bh(&bat_priv
->tt
.req_list_lock
);
1999 /* Recalculate the CRC for this orig_node and store it */
2000 orig_node
->tt_crc
= batadv_tt_global_crc(bat_priv
, orig_node
);
2003 batadv_orig_node_free_ref(orig_node
);
2006 int batadv_tt_init(struct batadv_priv
*bat_priv
)
2010 ret
= batadv_tt_local_init(bat_priv
);
2014 ret
= batadv_tt_global_init(bat_priv
);
2018 batadv_tt_start_timer(bat_priv
);
2023 static void batadv_tt_roam_list_free(struct batadv_priv
*bat_priv
)
2025 struct batadv_tt_roam_node
*node
, *safe
;
2027 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2029 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2030 list_del(&node
->list
);
2034 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2037 static void batadv_tt_roam_purge(struct batadv_priv
*bat_priv
)
2039 struct batadv_tt_roam_node
*node
, *safe
;
2041 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2042 list_for_each_entry_safe(node
, safe
, &bat_priv
->tt
.roam_list
, list
) {
2043 if (!batadv_has_timed_out(node
->first_time
,
2044 BATADV_ROAMING_MAX_TIME
))
2047 list_del(&node
->list
);
2050 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2053 /* This function checks whether the client already reached the
2054 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2057 * returns true if the ROAMING_ADV can be sent, false otherwise
2059 static bool batadv_tt_check_roam_count(struct batadv_priv
*bat_priv
,
2062 struct batadv_tt_roam_node
*tt_roam_node
;
2065 spin_lock_bh(&bat_priv
->tt
.roam_list_lock
);
2066 /* The new tt_req will be issued only if I'm not waiting for a
2067 * reply from the same orig_node yet
2069 list_for_each_entry(tt_roam_node
, &bat_priv
->tt
.roam_list
, list
) {
2070 if (!batadv_compare_eth(tt_roam_node
->addr
, client
))
2073 if (batadv_has_timed_out(tt_roam_node
->first_time
,
2074 BATADV_ROAMING_MAX_TIME
))
2077 if (!batadv_atomic_dec_not_zero(&tt_roam_node
->counter
))
2078 /* Sorry, you roamed too many times! */
2085 tt_roam_node
= kmalloc(sizeof(*tt_roam_node
), GFP_ATOMIC
);
2089 tt_roam_node
->first_time
= jiffies
;
2090 atomic_set(&tt_roam_node
->counter
,
2091 BATADV_ROAMING_MAX_COUNT
- 1);
2092 memcpy(tt_roam_node
->addr
, client
, ETH_ALEN
);
2094 list_add(&tt_roam_node
->list
, &bat_priv
->tt
.roam_list
);
2099 spin_unlock_bh(&bat_priv
->tt
.roam_list_lock
);
2103 static void batadv_send_roam_adv(struct batadv_priv
*bat_priv
, uint8_t *client
,
2104 struct batadv_orig_node
*orig_node
)
2106 struct batadv_neigh_node
*neigh_node
= NULL
;
2107 struct sk_buff
*skb
= NULL
;
2108 struct batadv_roam_adv_packet
*roam_adv_packet
;
2110 struct batadv_hard_iface
*primary_if
;
2111 size_t len
= sizeof(*roam_adv_packet
);
2113 /* before going on we have to check whether the client has
2114 * already roamed to us too many times
2116 if (!batadv_tt_check_roam_count(bat_priv
, client
))
2119 skb
= dev_alloc_skb(sizeof(*roam_adv_packet
) + ETH_HLEN
+ NET_IP_ALIGN
);
2123 skb_reserve(skb
, ETH_HLEN
+ NET_IP_ALIGN
);
2125 roam_adv_packet
= (struct batadv_roam_adv_packet
*)skb_put(skb
, len
);
2127 roam_adv_packet
->header
.packet_type
= BATADV_ROAM_ADV
;
2128 roam_adv_packet
->header
.version
= BATADV_COMPAT_VERSION
;
2129 roam_adv_packet
->header
.ttl
= BATADV_TTL
;
2130 roam_adv_packet
->reserved
= 0;
2131 primary_if
= batadv_primary_if_get_selected(bat_priv
);
2134 memcpy(roam_adv_packet
->src
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
2135 batadv_hardif_free_ref(primary_if
);
2136 memcpy(roam_adv_packet
->dst
, orig_node
->orig
, ETH_ALEN
);
2137 memcpy(roam_adv_packet
->client
, client
, ETH_ALEN
);
2139 neigh_node
= batadv_orig_node_get_router(orig_node
);
2143 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2144 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2145 orig_node
->orig
, client
, neigh_node
->addr
);
2147 batadv_inc_counter(bat_priv
, BATADV_CNT_TT_ROAM_ADV_TX
);
2149 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
2154 batadv_neigh_node_free_ref(neigh_node
);
2160 static void batadv_tt_purge(struct work_struct
*work
)
2162 struct delayed_work
*delayed_work
;
2163 struct batadv_priv_tt
*priv_tt
;
2164 struct batadv_priv
*bat_priv
;
2166 delayed_work
= container_of(work
, struct delayed_work
, work
);
2167 priv_tt
= container_of(delayed_work
, struct batadv_priv_tt
, work
);
2168 bat_priv
= container_of(priv_tt
, struct batadv_priv
, tt
);
2170 batadv_tt_local_purge(bat_priv
);
2171 batadv_tt_global_purge(bat_priv
);
2172 batadv_tt_req_purge(bat_priv
);
2173 batadv_tt_roam_purge(bat_priv
);
2175 batadv_tt_start_timer(bat_priv
);
2178 void batadv_tt_free(struct batadv_priv
*bat_priv
)
2180 cancel_delayed_work_sync(&bat_priv
->tt
.work
);
2182 batadv_tt_local_table_free(bat_priv
);
2183 batadv_tt_global_table_free(bat_priv
);
2184 batadv_tt_req_list_free(bat_priv
);
2185 batadv_tt_changes_list_free(bat_priv
);
2186 batadv_tt_roam_list_free(bat_priv
);
2188 kfree(bat_priv
->tt
.last_changeset
);
2191 /* This function will enable or disable the specified flags for all the entries
2192 * in the given hash table and returns the number of modified entries
2194 static uint16_t batadv_tt_set_flags(struct batadv_hashtable
*hash
,
2195 uint16_t flags
, bool enable
)
2198 uint16_t changed_num
= 0;
2199 struct hlist_head
*head
;
2200 struct hlist_node
*node
;
2201 struct batadv_tt_common_entry
*tt_common_entry
;
2206 for (i
= 0; i
< hash
->size
; i
++) {
2207 head
= &hash
->table
[i
];
2210 hlist_for_each_entry_rcu(tt_common_entry
, node
,
2213 if ((tt_common_entry
->flags
& flags
) == flags
)
2215 tt_common_entry
->flags
|= flags
;
2217 if (!(tt_common_entry
->flags
& flags
))
2219 tt_common_entry
->flags
&= ~flags
;
2229 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2230 static void batadv_tt_local_purge_pending_clients(struct batadv_priv
*bat_priv
)
2232 struct batadv_hashtable
*hash
= bat_priv
->tt
.local_hash
;
2233 struct batadv_tt_common_entry
*tt_common
;
2234 struct batadv_tt_local_entry
*tt_local
;
2235 struct hlist_node
*node
, *node_tmp
;
2236 struct hlist_head
*head
;
2237 spinlock_t
*list_lock
; /* protects write access to the hash lists */
2243 for (i
= 0; i
< hash
->size
; i
++) {
2244 head
= &hash
->table
[i
];
2245 list_lock
= &hash
->list_locks
[i
];
2247 spin_lock_bh(list_lock
);
2248 hlist_for_each_entry_safe(tt_common
, node
, node_tmp
, head
,
2250 if (!(tt_common
->flags
& BATADV_TT_CLIENT_PENDING
))
2253 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2254 "Deleting local tt entry (%pM): pending\n",
2257 atomic_dec(&bat_priv
->tt
.local_entry_num
);
2258 hlist_del_rcu(node
);
2259 tt_local
= container_of(tt_common
,
2260 struct batadv_tt_local_entry
,
2262 batadv_tt_local_entry_free_ref(tt_local
);
2264 spin_unlock_bh(list_lock
);
2269 static int batadv_tt_commit_changes(struct batadv_priv
*bat_priv
,
2270 unsigned char **packet_buff
,
2271 int *packet_buff_len
, int packet_min_len
)
2273 uint16_t changed_num
= 0;
2275 if (atomic_read(&bat_priv
->tt
.local_changes
) < 1)
2278 changed_num
= batadv_tt_set_flags(bat_priv
->tt
.local_hash
,
2279 BATADV_TT_CLIENT_NEW
, false);
2281 /* all reset entries have to be counted as local entries */
2282 atomic_add(changed_num
, &bat_priv
->tt
.local_entry_num
);
2283 batadv_tt_local_purge_pending_clients(bat_priv
);
2284 bat_priv
->tt
.local_crc
= batadv_tt_local_crc(bat_priv
);
2286 /* Increment the TTVN only once per OGM interval */
2287 atomic_inc(&bat_priv
->tt
.vn
);
2288 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2289 "Local changes committed, updating to ttvn %u\n",
2290 (uint8_t)atomic_read(&bat_priv
->tt
.vn
));
2292 /* reset the sending counter */
2293 atomic_set(&bat_priv
->tt
.ogm_append_cnt
, BATADV_TT_OGM_APPEND_MAX
);
2295 return batadv_tt_changes_fill_buff(bat_priv
, packet_buff
,
2296 packet_buff_len
, packet_min_len
);
2299 /* when calling this function (hard_iface == primary_if) has to be true */
2300 int batadv_tt_append_diff(struct batadv_priv
*bat_priv
,
2301 unsigned char **packet_buff
, int *packet_buff_len
,
2306 /* if at least one change happened */
2307 tt_num_changes
= batadv_tt_commit_changes(bat_priv
, packet_buff
,
2311 /* if the changes have been sent often enough */
2312 if ((tt_num_changes
< 0) &&
2313 (!batadv_atomic_dec_not_zero(&bat_priv
->tt
.ogm_append_cnt
))) {
2314 batadv_tt_realloc_packet_buff(packet_buff
, packet_buff_len
,
2315 packet_min_len
, packet_min_len
);
2319 return tt_num_changes
;
2322 bool batadv_is_ap_isolated(struct batadv_priv
*bat_priv
, uint8_t *src
,
2325 struct batadv_tt_local_entry
*tt_local_entry
= NULL
;
2326 struct batadv_tt_global_entry
*tt_global_entry
= NULL
;
2329 if (!atomic_read(&bat_priv
->ap_isolation
))
2332 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, dst
);
2333 if (!tt_local_entry
)
2336 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, src
);
2337 if (!tt_global_entry
)
2340 if (!_batadv_is_ap_isolated(tt_local_entry
, tt_global_entry
))
2346 if (tt_global_entry
)
2347 batadv_tt_global_entry_free_ref(tt_global_entry
);
2349 batadv_tt_local_entry_free_ref(tt_local_entry
);
2353 void batadv_tt_update_orig(struct batadv_priv
*bat_priv
,
2354 struct batadv_orig_node
*orig_node
,
2355 const unsigned char *tt_buff
, uint8_t tt_num_changes
,
2356 uint8_t ttvn
, uint16_t tt_crc
)
2358 uint8_t orig_ttvn
= (uint8_t)atomic_read(&orig_node
->last_ttvn
);
2359 bool full_table
= true;
2360 struct batadv_tt_change
*tt_change
;
2362 /* don't care about a backbone gateways updates. */
2363 if (batadv_bla_is_backbone_gw_orig(bat_priv
, orig_node
->orig
))
2366 /* orig table not initialised AND first diff is in the OGM OR the ttvn
2367 * increased by one -> we can apply the attached changes
2369 if ((!orig_node
->tt_initialised
&& ttvn
== 1) ||
2370 ttvn
- orig_ttvn
== 1) {
2371 /* the OGM could not contain the changes due to their size or
2372 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2374 * In this case send a tt request
2376 if (!tt_num_changes
) {
2381 tt_change
= (struct batadv_tt_change
*)tt_buff
;
2382 batadv_tt_update_changes(bat_priv
, orig_node
, tt_num_changes
,
2385 /* Even if we received the precomputed crc with the OGM, we
2386 * prefer to recompute it to spot any possible inconsistency
2387 * in the global table
2389 orig_node
->tt_crc
= batadv_tt_global_crc(bat_priv
, orig_node
);
2391 /* The ttvn alone is not enough to guarantee consistency
2392 * because a single value could represent different states
2393 * (due to the wrap around). Thus a node has to check whether
2394 * the resulting table (after applying the changes) is still
2395 * consistent or not. E.g. a node could disconnect while its
2396 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2397 * checking the CRC value is mandatory to detect the
2400 if (orig_node
->tt_crc
!= tt_crc
)
2403 /* if we missed more than one change or our tables are not
2404 * in sync anymore -> request fresh tt data
2406 if (!orig_node
->tt_initialised
|| ttvn
!= orig_ttvn
||
2407 orig_node
->tt_crc
!= tt_crc
) {
2409 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2410 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2411 orig_node
->orig
, ttvn
, orig_ttvn
, tt_crc
,
2412 orig_node
->tt_crc
, tt_num_changes
);
2413 batadv_send_tt_request(bat_priv
, orig_node
, ttvn
,
2414 tt_crc
, full_table
);
2420 /* returns true whether we know that the client has moved from its old
2421 * originator to another one. This entry is kept is still kept for consistency
2424 bool batadv_tt_global_client_is_roaming(struct batadv_priv
*bat_priv
,
2427 struct batadv_tt_global_entry
*tt_global_entry
;
2430 tt_global_entry
= batadv_tt_global_hash_find(bat_priv
, addr
);
2431 if (!tt_global_entry
)
2434 ret
= !!(tt_global_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
);
2435 batadv_tt_global_entry_free_ref(tt_global_entry
);
2441 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2442 * @bat_priv: the bat priv with all the soft interface information
2443 * @addr: the MAC address of the local client to query
2445 * Returns true if the local client is known to be roaming (it is not served by
2446 * this node anymore) or not. If yes, the client is still present in the table
2447 * to keep the latter consistent with the node TTVN
2449 bool batadv_tt_local_client_is_roaming(struct batadv_priv
*bat_priv
,
2452 struct batadv_tt_local_entry
*tt_local_entry
;
2455 tt_local_entry
= batadv_tt_local_hash_find(bat_priv
, addr
);
2456 if (!tt_local_entry
)
2459 ret
= tt_local_entry
->common
.flags
& BATADV_TT_CLIENT_ROAM
;
2460 batadv_tt_local_entry_free_ref(tt_local_entry
);
2466 bool batadv_tt_add_temporary_global_entry(struct batadv_priv
*bat_priv
,
2467 struct batadv_orig_node
*orig_node
,
2468 const unsigned char *addr
)
2472 if (!batadv_tt_global_add(bat_priv
, orig_node
, addr
,
2473 BATADV_TT_CLIENT_TEMP
,
2474 atomic_read(&orig_node
->last_ttvn
)))
2477 batadv_dbg(BATADV_DBG_TT
, bat_priv
,
2478 "Added temporary global client (addr: %pM orig: %pM)\n",
2479 addr
, orig_node
->orig
);