batman-adv: support array of debugfs general attributes
[linux-2.6-block.git] / net / batman-adv / translation-table.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
c6c8fea2 2 *
35c133a0 3 * Marek Lindner, Simon Wunderlich, Antonio Quartulli
c6c8fea2
SE
4 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA
c6c8fea2
SE
18 */
19
20#include "main.h"
21#include "translation-table.h"
22#include "soft-interface.h"
32ae9b22 23#include "hard-interface.h"
a73105b8 24#include "send.h"
c6c8fea2
SE
25#include "hash.h"
26#include "originator.h"
a73105b8 27#include "routing.h"
20ff9d59 28#include "bridge_loop_avoidance.h"
c6c8fea2 29
a73105b8
AQ
30#include <linux/crc16.h>
31
56303d34
SE
32static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
33 struct batadv_orig_node *orig_node);
a513088d
SE
34static void batadv_tt_purge(struct work_struct *work);
35static void
56303d34 36batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
30cfd02b
AQ
37static 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);
c6c8fea2 41
7aadf889 42/* returns 1 if they are the same mac addr */
a513088d 43static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
7aadf889 44{
56303d34 45 const void *data1 = container_of(node, struct batadv_tt_common_entry,
747e4221 46 hash_entry);
7aadf889
ML
47
48 return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
49}
50
56303d34 51static void batadv_tt_start_timer(struct batadv_priv *bat_priv)
c6c8fea2 52{
807736f6
SE
53 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
54 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
a73105b8 55 msecs_to_jiffies(5000));
c6c8fea2
SE
56}
57
56303d34 58static struct batadv_tt_common_entry *
5bf74e9c 59batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
7aadf889 60{
7aadf889
ML
61 struct hlist_head *head;
62 struct hlist_node *node;
56303d34
SE
63 struct batadv_tt_common_entry *tt_common_entry;
64 struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
c90681b8 65 uint32_t index;
7aadf889
ML
66
67 if (!hash)
68 return NULL;
69
da641193 70 index = batadv_choose_orig(data, hash->size);
7aadf889
ML
71 head = &hash->table[index];
72
73 rcu_read_lock();
48100bac 74 hlist_for_each_entry_rcu(tt_common_entry, node, head, hash_entry) {
1eda58bf 75 if (!batadv_compare_eth(tt_common_entry, data))
7aadf889
ML
76 continue;
77
48100bac 78 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
7683fdc1
AQ
79 continue;
80
48100bac 81 tt_common_entry_tmp = tt_common_entry;
7aadf889
ML
82 break;
83 }
84 rcu_read_unlock();
85
48100bac 86 return tt_common_entry_tmp;
7aadf889
ML
87}
88
56303d34
SE
89static struct batadv_tt_local_entry *
90batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
7aadf889 91{
56303d34
SE
92 struct batadv_tt_common_entry *tt_common_entry;
93 struct batadv_tt_local_entry *tt_local_entry = NULL;
7aadf889 94
807736f6 95 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
48100bac
AQ
96 if (tt_common_entry)
97 tt_local_entry = container_of(tt_common_entry,
56303d34
SE
98 struct batadv_tt_local_entry,
99 common);
48100bac
AQ
100 return tt_local_entry;
101}
7aadf889 102
56303d34
SE
103static struct batadv_tt_global_entry *
104batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
48100bac 105{
56303d34
SE
106 struct batadv_tt_common_entry *tt_common_entry;
107 struct batadv_tt_global_entry *tt_global_entry = NULL;
7683fdc1 108
807736f6 109 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
48100bac
AQ
110 if (tt_common_entry)
111 tt_global_entry = container_of(tt_common_entry,
56303d34
SE
112 struct batadv_tt_global_entry,
113 common);
48100bac 114 return tt_global_entry;
7aadf889 115
7aadf889
ML
116}
117
a513088d 118static void
56303d34 119batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
7683fdc1 120{
48100bac
AQ
121 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
122 kfree_rcu(tt_local_entry, common.rcu);
7683fdc1
AQ
123}
124
a513088d 125static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
531027fc 126{
56303d34
SE
127 struct batadv_tt_common_entry *tt_common_entry;
128 struct batadv_tt_global_entry *tt_global_entry;
531027fc 129
56303d34
SE
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);
531027fc 133
531027fc
SW
134 kfree(tt_global_entry);
135}
136
a513088d 137static void
56303d34 138batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
7683fdc1 139{
db08e6e5 140 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
a513088d 141 batadv_tt_global_del_orig_list(tt_global_entry);
48100bac 142 call_rcu(&tt_global_entry->common.rcu,
a513088d 143 batadv_tt_global_entry_free_rcu);
db08e6e5
SW
144 }
145}
146
a513088d 147static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
db08e6e5 148{
56303d34 149 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5 150
56303d34 151 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
7d211efc 152 batadv_orig_node_free_ref(orig_entry->orig_node);
db08e6e5
SW
153 kfree(orig_entry);
154}
155
a513088d 156static void
56303d34 157batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
db08e6e5 158{
d657e621
AQ
159 if (!atomic_dec_and_test(&orig_entry->refcount))
160 return;
29cb99de
AQ
161 /* to avoid race conditions, immediately decrease the tt counter */
162 atomic_dec(&orig_entry->orig_node->tt_size);
a513088d 163 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
7683fdc1
AQ
164}
165
56303d34 166static void batadv_tt_local_event(struct batadv_priv *bat_priv,
a513088d 167 const uint8_t *addr, uint8_t flags)
a73105b8 168{
56303d34 169 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
3b643de5
AQ
170 bool event_removed = false;
171 bool del_op_requested, del_op_entry;
a73105b8
AQ
172
173 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
174
175 if (!tt_change_node)
176 return;
177
ff66c975 178 tt_change_node->change.flags = flags;
a73105b8
AQ
179 memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
180
acd34afa 181 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
3b643de5
AQ
182
183 /* check for ADD+DEL or DEL+ADD events */
807736f6
SE
184 spin_lock_bh(&bat_priv->tt.changes_list_lock);
185 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
3b643de5
AQ
186 list) {
187 if (!batadv_compare_eth(entry->change.addr, addr))
188 continue;
189
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"
195 * clients
196 */
acd34afa 197 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
3b643de5
AQ
198 if (!del_op_requested && del_op_entry)
199 goto del;
200 if (del_op_requested && !del_op_entry)
201 goto del;
202 continue;
203del:
204 list_del(&entry->list);
205 kfree(entry);
155e4e12 206 kfree(tt_change_node);
3b643de5
AQ
207 event_removed = true;
208 goto unlock;
209 }
210
a73105b8 211 /* track the change in the OGMinterval list */
807736f6 212 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
3b643de5
AQ
213
214unlock:
807736f6 215 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8 216
3b643de5 217 if (event_removed)
807736f6 218 atomic_dec(&bat_priv->tt.local_changes);
3b643de5 219 else
807736f6 220 atomic_inc(&bat_priv->tt.local_changes);
a73105b8
AQ
221}
222
08c36d3e 223int batadv_tt_len(int changes_num)
a73105b8 224{
96412690 225 return changes_num * sizeof(struct batadv_tt_change);
a73105b8
AQ
226}
227
56303d34 228static int batadv_tt_local_init(struct batadv_priv *bat_priv)
c6c8fea2 229{
807736f6 230 if (bat_priv->tt.local_hash)
5346c35e 231 return 0;
c6c8fea2 232
807736f6 233 bat_priv->tt.local_hash = batadv_hash_new(1024);
c6c8fea2 234
807736f6 235 if (!bat_priv->tt.local_hash)
5346c35e 236 return -ENOMEM;
c6c8fea2 237
5346c35e 238 return 0;
c6c8fea2
SE
239}
240
068ee6e2
AQ
241static void batadv_tt_global_free(struct batadv_priv *bat_priv,
242 struct batadv_tt_global_entry *tt_global,
243 const char *message)
244{
245 batadv_dbg(BATADV_DBG_TT, bat_priv,
246 "Deleting global tt entry %pM: %s\n",
247 tt_global->common.addr, message);
248
249 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
250 batadv_choose_orig, tt_global->common.addr);
251 batadv_tt_global_entry_free_ref(tt_global);
252
253}
254
08c36d3e
SE
255void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
256 int ifindex)
c6c8fea2 257{
56303d34 258 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
170173bf
SE
259 struct batadv_tt_local_entry *tt_local;
260 struct batadv_tt_global_entry *tt_global;
db08e6e5
SW
261 struct hlist_head *head;
262 struct hlist_node *node;
56303d34 263 struct batadv_tt_orig_list_entry *orig_entry;
80b3f58c 264 int hash_added;
068ee6e2 265 bool roamed_back = false;
c6c8fea2 266
47c94655 267 tt_local = batadv_tt_local_hash_find(bat_priv, addr);
068ee6e2 268 tt_global = batadv_tt_global_hash_find(bat_priv, addr);
c6c8fea2 269
47c94655
AQ
270 if (tt_local) {
271 tt_local->last_seen = jiffies;
068ee6e2
AQ
272 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
273 batadv_dbg(BATADV_DBG_TT, bat_priv,
274 "Re-adding pending client %pM\n", addr);
275 /* whatever the reason why the PENDING flag was set,
276 * this is a client which was enqueued to be removed in
277 * this orig_interval. Since it popped up again, the
278 * flag can be reset like it was never enqueued
279 */
280 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
281 goto add_event;
282 }
283
284 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
285 batadv_dbg(BATADV_DBG_TT, bat_priv,
286 "Roaming client %pM came back to its original location\n",
287 addr);
288 /* the ROAM flag is set because this client roamed away
289 * and the node got a roaming_advertisement message. Now
290 * that the client popped up again at its original
291 * location such flag can be unset
292 */
293 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
294 roamed_back = true;
295 }
296 goto check_roaming;
c6c8fea2
SE
297 }
298
47c94655
AQ
299 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
300 if (!tt_local)
7683fdc1 301 goto out;
a73105b8 302
39c75a51 303 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 304 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
807736f6 305 (uint8_t)atomic_read(&bat_priv->tt.vn));
c6c8fea2 306
47c94655
AQ
307 memcpy(tt_local->common.addr, addr, ETH_ALEN);
308 tt_local->common.flags = BATADV_NO_FLAGS;
9563877e 309 if (batadv_is_wifi_iface(ifindex))
47c94655
AQ
310 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
311 atomic_set(&tt_local->common.refcount, 2);
312 tt_local->last_seen = jiffies;
313 tt_local->common.added_at = tt_local->last_seen;
c6c8fea2
SE
314
315 /* the batman interface mac address should never be purged */
1eda58bf 316 if (batadv_compare_eth(addr, soft_iface->dev_addr))
47c94655 317 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
c6c8fea2 318
c40ed2bf
AQ
319 /* The local entry has to be marked as NEW to avoid to send it in
320 * a full table response going out before the next ttvn increment
9cfc7bd6
SE
321 * (consistency check)
322 */
47c94655 323 tt_local->common.flags |= BATADV_TT_CLIENT_NEW;
c40ed2bf 324
807736f6 325 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
47c94655
AQ
326 batadv_choose_orig, &tt_local->common,
327 &tt_local->common.hash_entry);
80b3f58c
SW
328
329 if (unlikely(hash_added != 0)) {
330 /* remove the reference for the hash */
47c94655 331 batadv_tt_local_entry_free_ref(tt_local);
80b3f58c
SW
332 goto out;
333 }
334
068ee6e2 335add_event:
47c94655 336 batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
ff66c975 337
068ee6e2
AQ
338check_roaming:
339 /* Check whether it is a roaming, but don't do anything if the roaming
340 * process has already been handled
341 */
342 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
db08e6e5 343 /* These node are probably going to update their tt table */
47c94655 344 head = &tt_global->orig_list;
db08e6e5
SW
345 rcu_read_lock();
346 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
47c94655 347 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
a513088d 348 orig_entry->orig_node);
db08e6e5
SW
349 }
350 rcu_read_unlock();
068ee6e2
AQ
351 if (roamed_back) {
352 batadv_tt_global_free(bat_priv, tt_global,
353 "Roaming canceled");
354 tt_global = NULL;
355 } else {
356 /* The global entry has to be marked as ROAMING and
357 * has to be kept for consistency purpose
358 */
359 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
360 tt_global->roam_at = jiffies;
361 }
7683fdc1 362 }
068ee6e2 363
7683fdc1 364out:
47c94655
AQ
365 if (tt_local)
366 batadv_tt_local_entry_free_ref(tt_local);
367 if (tt_global)
368 batadv_tt_global_entry_free_ref(tt_global);
c6c8fea2
SE
369}
370
a513088d
SE
371static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
372 int *packet_buff_len,
373 int min_packet_len,
374 int new_packet_len)
be9aa4c1
ML
375{
376 unsigned char *new_buff;
377
378 new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
379
380 /* keep old buffer if kmalloc should fail */
381 if (new_buff) {
382 memcpy(new_buff, *packet_buff, min_packet_len);
383 kfree(*packet_buff);
384 *packet_buff = new_buff;
385 *packet_buff_len = new_packet_len;
386 }
387}
388
56303d34 389static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
a513088d
SE
390 unsigned char **packet_buff,
391 int *packet_buff_len,
392 int min_packet_len)
be9aa4c1 393{
56303d34 394 struct batadv_hard_iface *primary_if;
be9aa4c1
ML
395 int req_len;
396
e5d89254 397 primary_if = batadv_primary_if_get_selected(bat_priv);
be9aa4c1
ML
398
399 req_len = min_packet_len;
807736f6 400 req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
be9aa4c1
ML
401
402 /* if we have too many changes for one packet don't send any
403 * and wait for the tt table request which will be fragmented
404 */
405 if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
406 req_len = min_packet_len;
407
a513088d
SE
408 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
409 min_packet_len, req_len);
be9aa4c1
ML
410
411 if (primary_if)
e5d89254 412 batadv_hardif_free_ref(primary_if);
be9aa4c1
ML
413}
414
56303d34 415static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
a513088d
SE
416 unsigned char **packet_buff,
417 int *packet_buff_len,
418 int min_packet_len)
c6c8fea2 419{
56303d34 420 struct batadv_tt_change_node *entry, *safe;
be9aa4c1
ML
421 int count = 0, tot_changes = 0, new_len;
422 unsigned char *tt_buff;
423
a513088d
SE
424 batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
425 packet_buff_len, min_packet_len);
c6c8fea2 426
be9aa4c1
ML
427 new_len = *packet_buff_len - min_packet_len;
428 tt_buff = *packet_buff + min_packet_len;
429
430 if (new_len > 0)
08c36d3e 431 tot_changes = new_len / batadv_tt_len(1);
c6c8fea2 432
807736f6
SE
433 spin_lock_bh(&bat_priv->tt.changes_list_lock);
434 atomic_set(&bat_priv->tt.local_changes, 0);
c6c8fea2 435
807736f6 436 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
7c64fd98 437 list) {
a73105b8 438 if (count < tot_changes) {
08c36d3e 439 memcpy(tt_buff + batadv_tt_len(count),
96412690 440 &entry->change, sizeof(struct batadv_tt_change));
c6c8fea2
SE
441 count++;
442 }
a73105b8
AQ
443 list_del(&entry->list);
444 kfree(entry);
c6c8fea2 445 }
807736f6 446 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8
AQ
447
448 /* Keep the buffer for possible tt_request */
807736f6
SE
449 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
450 kfree(bat_priv->tt.last_changeset);
451 bat_priv->tt.last_changeset_len = 0;
452 bat_priv->tt.last_changeset = NULL;
be9aa4c1
ML
453 /* check whether this new OGM has no changes due to size problems */
454 if (new_len > 0) {
455 /* if kmalloc() fails we will reply with the full table
a73105b8
AQ
456 * instead of providing the diff
457 */
807736f6
SE
458 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
459 if (bat_priv->tt.last_changeset) {
460 memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
461 bat_priv->tt.last_changeset_len = new_len;
a73105b8
AQ
462 }
463 }
807736f6 464 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
c6c8fea2 465
08ad76ec 466 return count;
c6c8fea2
SE
467}
468
08c36d3e 469int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
470{
471 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 472 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 473 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34
SE
474 struct batadv_tt_common_entry *tt_common_entry;
475 struct batadv_hard_iface *primary_if;
7aadf889 476 struct hlist_node *node;
c6c8fea2 477 struct hlist_head *head;
c90681b8 478 uint32_t i;
c6c8fea2 479
30da63a6
ML
480 primary_if = batadv_seq_print_text_primary_if_get(seq);
481 if (!primary_if)
32ae9b22 482 goto out;
c6c8fea2 483
86ceb360
SE
484 seq_printf(seq,
485 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
807736f6 486 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
c6c8fea2 487
c6c8fea2
SE
488 for (i = 0; i < hash->size; i++) {
489 head = &hash->table[i];
490
7aadf889 491 rcu_read_lock();
48100bac 492 hlist_for_each_entry_rcu(tt_common_entry, node,
7aadf889 493 head, hash_entry) {
d099c2c5 494 seq_printf(seq, " * %pM [%c%c%c%c%c]\n",
7c64fd98
SE
495 tt_common_entry->addr,
496 (tt_common_entry->flags &
acd34afa 497 BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
7c64fd98 498 (tt_common_entry->flags &
acd34afa 499 BATADV_TT_CLIENT_NOPURGE ? 'P' : '.'),
7c64fd98 500 (tt_common_entry->flags &
acd34afa 501 BATADV_TT_CLIENT_NEW ? 'N' : '.'),
7c64fd98 502 (tt_common_entry->flags &
acd34afa 503 BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
7c64fd98 504 (tt_common_entry->flags &
acd34afa 505 BATADV_TT_CLIENT_WIFI ? 'W' : '.'));
c6c8fea2 506 }
7aadf889 507 rcu_read_unlock();
c6c8fea2 508 }
32ae9b22
ML
509out:
510 if (primary_if)
e5d89254 511 batadv_hardif_free_ref(primary_if);
30da63a6 512 return 0;
c6c8fea2
SE
513}
514
56303d34
SE
515static void
516batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
517 struct batadv_tt_local_entry *tt_local_entry,
518 uint16_t flags, const char *message)
c6c8fea2 519{
a513088d
SE
520 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
521 tt_local_entry->common.flags | flags);
a73105b8 522
015758d0
AQ
523 /* The local client has to be marked as "pending to be removed" but has
524 * to be kept in the table in order to send it in a full table
9cfc7bd6
SE
525 * response issued before the net ttvn increment (consistency check)
526 */
acd34afa 527 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
c566dbbe 528
39c75a51 529 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
530 "Local tt entry (%pM) pending to be removed: %s\n",
531 tt_local_entry->common.addr, message);
c6c8fea2
SE
532}
533
7f91d06c
AQ
534/**
535 * batadv_tt_local_remove - logically remove an entry from the local table
536 * @bat_priv: the bat priv with all the soft interface information
537 * @addr: the MAC address of the client to remove
538 * @message: message to append to the log on deletion
539 * @roaming: true if the deletion is due to a roaming event
540 *
541 * Returns the flags assigned to the local entry before being deleted
542 */
543uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
544 const uint8_t *addr, const char *message,
545 bool roaming)
c6c8fea2 546{
170173bf 547 struct batadv_tt_local_entry *tt_local_entry;
7f91d06c 548 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
c6c8fea2 549
a513088d 550 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
7683fdc1
AQ
551 if (!tt_local_entry)
552 goto out;
553
7f91d06c
AQ
554 curr_flags = tt_local_entry->common.flags;
555
acd34afa 556 flags = BATADV_TT_CLIENT_DEL;
068ee6e2
AQ
557 /* if this global entry addition is due to a roaming, the node has to
558 * mark the local entry as "roamed" in order to correctly reroute
559 * packets later
560 */
7c1fd91d 561 if (roaming) {
acd34afa 562 flags |= BATADV_TT_CLIENT_ROAM;
7c1fd91d
AQ
563 /* mark the local client as ROAMed */
564 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
565 }
42d0b044 566
068ee6e2
AQ
567 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
568 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
569 message);
570 goto out;
571 }
572 /* if this client has been added right now, it is possible to
573 * immediately purge it
574 */
575 batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
576 curr_flags | BATADV_TT_CLIENT_DEL);
577 hlist_del_rcu(&tt_local_entry->common.hash_entry);
578 batadv_tt_local_entry_free_ref(tt_local_entry);
7f91d06c 579
7683fdc1
AQ
580out:
581 if (tt_local_entry)
a513088d 582 batadv_tt_local_entry_free_ref(tt_local_entry);
7f91d06c
AQ
583
584 return curr_flags;
c6c8fea2
SE
585}
586
56303d34 587static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
acd34afa 588 struct hlist_head *head)
c6c8fea2 589{
56303d34
SE
590 struct batadv_tt_local_entry *tt_local_entry;
591 struct batadv_tt_common_entry *tt_common_entry;
7aadf889 592 struct hlist_node *node, *node_tmp;
acd34afa
SE
593
594 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp, head,
595 hash_entry) {
596 tt_local_entry = container_of(tt_common_entry,
56303d34
SE
597 struct batadv_tt_local_entry,
598 common);
acd34afa
SE
599 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
600 continue;
601
602 /* entry already marked for deletion */
603 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
604 continue;
605
606 if (!batadv_has_timed_out(tt_local_entry->last_seen,
607 BATADV_TT_LOCAL_TIMEOUT))
608 continue;
609
610 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
611 BATADV_TT_CLIENT_DEL, "timed out");
612 }
613}
614
56303d34 615static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
acd34afa 616{
807736f6 617 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
c6c8fea2 618 struct hlist_head *head;
7683fdc1 619 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 620 uint32_t i;
c6c8fea2 621
c6c8fea2
SE
622 for (i = 0; i < hash->size; i++) {
623 head = &hash->table[i];
7683fdc1 624 list_lock = &hash->list_locks[i];
c6c8fea2 625
7683fdc1 626 spin_lock_bh(list_lock);
acd34afa 627 batadv_tt_local_purge_list(bat_priv, head);
7683fdc1 628 spin_unlock_bh(list_lock);
c6c8fea2
SE
629 }
630
c6c8fea2
SE
631}
632
56303d34 633static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
c6c8fea2 634{
5bf74e9c 635 struct batadv_hashtable *hash;
a73105b8 636 spinlock_t *list_lock; /* protects write access to the hash lists */
56303d34
SE
637 struct batadv_tt_common_entry *tt_common_entry;
638 struct batadv_tt_local_entry *tt_local;
7683fdc1
AQ
639 struct hlist_node *node, *node_tmp;
640 struct hlist_head *head;
c90681b8 641 uint32_t i;
a73105b8 642
807736f6 643 if (!bat_priv->tt.local_hash)
c6c8fea2
SE
644 return;
645
807736f6 646 hash = bat_priv->tt.local_hash;
a73105b8
AQ
647
648 for (i = 0; i < hash->size; i++) {
649 head = &hash->table[i];
650 list_lock = &hash->list_locks[i];
651
652 spin_lock_bh(list_lock);
48100bac 653 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
a73105b8
AQ
654 head, hash_entry) {
655 hlist_del_rcu(node);
56303d34
SE
656 tt_local = container_of(tt_common_entry,
657 struct batadv_tt_local_entry,
658 common);
659 batadv_tt_local_entry_free_ref(tt_local);
a73105b8
AQ
660 }
661 spin_unlock_bh(list_lock);
662 }
663
1a8eaf07 664 batadv_hash_destroy(hash);
a73105b8 665
807736f6 666 bat_priv->tt.local_hash = NULL;
c6c8fea2
SE
667}
668
56303d34 669static int batadv_tt_global_init(struct batadv_priv *bat_priv)
c6c8fea2 670{
807736f6 671 if (bat_priv->tt.global_hash)
5346c35e 672 return 0;
c6c8fea2 673
807736f6 674 bat_priv->tt.global_hash = batadv_hash_new(1024);
c6c8fea2 675
807736f6 676 if (!bat_priv->tt.global_hash)
5346c35e 677 return -ENOMEM;
c6c8fea2 678
5346c35e 679 return 0;
c6c8fea2
SE
680}
681
56303d34 682static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
c6c8fea2 683{
56303d34 684 struct batadv_tt_change_node *entry, *safe;
c6c8fea2 685
807736f6 686 spin_lock_bh(&bat_priv->tt.changes_list_lock);
c6c8fea2 687
807736f6 688 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
a73105b8
AQ
689 list) {
690 list_del(&entry->list);
691 kfree(entry);
692 }
c6c8fea2 693
807736f6
SE
694 atomic_set(&bat_priv->tt.local_changes, 0);
695 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8 696}
c6c8fea2 697
d657e621
AQ
698/* retrieves the orig_tt_list_entry belonging to orig_node from the
699 * batadv_tt_global_entry list
700 *
701 * returns it with an increased refcounter, NULL if not found
db08e6e5 702 */
d657e621
AQ
703static struct batadv_tt_orig_list_entry *
704batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
705 const struct batadv_orig_node *orig_node)
db08e6e5 706{
d657e621 707 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
db08e6e5
SW
708 const struct hlist_head *head;
709 struct hlist_node *node;
db08e6e5
SW
710
711 rcu_read_lock();
712 head = &entry->orig_list;
713 hlist_for_each_entry_rcu(tmp_orig_entry, node, head, list) {
d657e621
AQ
714 if (tmp_orig_entry->orig_node != orig_node)
715 continue;
716 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
717 continue;
718
719 orig_entry = tmp_orig_entry;
720 break;
db08e6e5
SW
721 }
722 rcu_read_unlock();
d657e621
AQ
723
724 return orig_entry;
725}
726
727/* find out if an orig_node is already in the list of a tt_global_entry.
728 * returns true if found, false otherwise
729 */
730static bool
731batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
732 const struct batadv_orig_node *orig_node)
733{
734 struct batadv_tt_orig_list_entry *orig_entry;
735 bool found = false;
736
737 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
738 if (orig_entry) {
739 found = true;
740 batadv_tt_orig_list_entry_free_ref(orig_entry);
741 }
742
db08e6e5
SW
743 return found;
744}
745
a513088d 746static void
d657e621 747batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
56303d34 748 struct batadv_orig_node *orig_node, int ttvn)
db08e6e5 749{
56303d34 750 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5 751
d657e621 752 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
30cfd02b
AQ
753 if (orig_entry) {
754 /* refresh the ttvn: the current value could be a bogus one that
755 * was added during a "temporary client detection"
756 */
757 orig_entry->ttvn = ttvn;
d657e621 758 goto out;
30cfd02b 759 }
d657e621 760
db08e6e5
SW
761 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
762 if (!orig_entry)
d657e621 763 goto out;
db08e6e5
SW
764
765 INIT_HLIST_NODE(&orig_entry->list);
766 atomic_inc(&orig_node->refcount);
767 atomic_inc(&orig_node->tt_size);
768 orig_entry->orig_node = orig_node;
769 orig_entry->ttvn = ttvn;
d657e621 770 atomic_set(&orig_entry->refcount, 2);
db08e6e5 771
d657e621 772 spin_lock_bh(&tt_global->list_lock);
db08e6e5 773 hlist_add_head_rcu(&orig_entry->list,
d657e621
AQ
774 &tt_global->orig_list);
775 spin_unlock_bh(&tt_global->list_lock);
776out:
777 if (orig_entry)
778 batadv_tt_orig_list_entry_free_ref(orig_entry);
db08e6e5
SW
779}
780
a73105b8 781/* caller must hold orig_node refcount */
56303d34
SE
782int batadv_tt_global_add(struct batadv_priv *bat_priv,
783 struct batadv_orig_node *orig_node,
d4f44692
AQ
784 const unsigned char *tt_addr, uint8_t flags,
785 uint8_t ttvn)
a73105b8 786{
170173bf
SE
787 struct batadv_tt_global_entry *tt_global_entry;
788 struct batadv_tt_local_entry *tt_local_entry;
7683fdc1 789 int ret = 0;
80b3f58c 790 int hash_added;
56303d34 791 struct batadv_tt_common_entry *common;
7f91d06c 792 uint16_t local_flags;
c6c8fea2 793
a513088d 794 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
068ee6e2
AQ
795 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
796
797 /* if the node already has a local client for this entry, it has to wait
798 * for a roaming advertisement instead of manually messing up the global
799 * table
800 */
801 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
802 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
803 goto out;
a73105b8
AQ
804
805 if (!tt_global_entry) {
d4f44692 806 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
a73105b8 807 if (!tt_global_entry)
7683fdc1
AQ
808 goto out;
809
c0a55929
SE
810 common = &tt_global_entry->common;
811 memcpy(common->addr, tt_addr, ETH_ALEN);
db08e6e5 812
d4f44692 813 common->flags = flags;
cc47f66e 814 tt_global_entry->roam_at = 0;
fdf79320
AQ
815 /* node must store current time in case of roaming. This is
816 * needed to purge this entry out on timeout (if nobody claims
817 * it)
818 */
819 if (flags & BATADV_TT_CLIENT_ROAM)
820 tt_global_entry->roam_at = jiffies;
c0a55929 821 atomic_set(&common->refcount, 2);
30cfd02b 822 common->added_at = jiffies;
db08e6e5
SW
823
824 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
825 spin_lock_init(&tt_global_entry->list_lock);
7683fdc1 826
807736f6 827 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
a513088d
SE
828 batadv_compare_tt,
829 batadv_choose_orig, common,
830 &common->hash_entry);
80b3f58c
SW
831
832 if (unlikely(hash_added != 0)) {
833 /* remove the reference for the hash */
a513088d 834 batadv_tt_global_entry_free_ref(tt_global_entry);
80b3f58c
SW
835 goto out_remove;
836 }
a73105b8 837 } else {
068ee6e2 838 common = &tt_global_entry->common;
30cfd02b
AQ
839 /* If there is already a global entry, we can use this one for
840 * our processing.
068ee6e2
AQ
841 * But if we are trying to add a temporary client then here are
842 * two options at this point:
843 * 1) the global client is not a temporary client: the global
844 * client has to be left as it is, temporary information
845 * should never override any already known client state
846 * 2) the global client is a temporary client: purge the
847 * originator list and add the new one orig_entry
30cfd02b 848 */
068ee6e2
AQ
849 if (flags & BATADV_TT_CLIENT_TEMP) {
850 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
851 goto out;
852 if (batadv_tt_global_entry_has_orig(tt_global_entry,
853 orig_node))
854 goto out_remove;
855 batadv_tt_global_del_orig_list(tt_global_entry);
856 goto add_orig_entry;
857 }
30cfd02b
AQ
858
859 /* if the client was temporary added before receiving the first
860 * OGM announcing it, we have to clear the TEMP flag
861 */
068ee6e2 862 common->flags &= ~BATADV_TT_CLIENT_TEMP;
db08e6e5 863
e9c00136
AQ
864 /* the change can carry possible "attribute" flags like the
865 * TT_CLIENT_WIFI, therefore they have to be copied in the
866 * client entry
867 */
868 tt_global_entry->common.flags |= flags;
869
acd34afa
SE
870 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
871 * one originator left in the list and we previously received a
db08e6e5
SW
872 * delete + roaming change for this originator.
873 *
874 * We should first delete the old originator before adding the
875 * new one.
876 */
068ee6e2 877 if (common->flags & BATADV_TT_CLIENT_ROAM) {
a513088d 878 batadv_tt_global_del_orig_list(tt_global_entry);
068ee6e2 879 common->flags &= ~BATADV_TT_CLIENT_ROAM;
db08e6e5 880 tt_global_entry->roam_at = 0;
a73105b8
AQ
881 }
882 }
068ee6e2 883add_orig_entry:
30cfd02b 884 /* add the new orig_entry (if needed) or update it */
d657e621 885 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
c6c8fea2 886
39c75a51 887 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 888 "Creating new global tt entry: %pM (via %pM)\n",
068ee6e2 889 common->addr, orig_node->orig);
c10dba05 890 ret = 1;
c6c8fea2 891
80b3f58c 892out_remove:
7f91d06c 893
a73105b8 894 /* remove address from local hash if present */
7f91d06c
AQ
895 local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
896 "global tt received",
068ee6e2 897 !!(flags & BATADV_TT_CLIENT_ROAM));
7f91d06c
AQ
898 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
899
068ee6e2
AQ
900 if (!(flags & BATADV_TT_CLIENT_ROAM))
901 /* this is a normal global add. Therefore the client is not in a
902 * roaming state anymore.
903 */
904 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
905
7683fdc1
AQ
906out:
907 if (tt_global_entry)
a513088d 908 batadv_tt_global_entry_free_ref(tt_global_entry);
068ee6e2
AQ
909 if (tt_local_entry)
910 batadv_tt_local_entry_free_ref(tt_local_entry);
7683fdc1 911 return ret;
c6c8fea2
SE
912}
913
981d8900
SE
914/* batadv_transtable_best_orig - Get best originator list entry from tt entry
915 * @tt_global_entry: global translation table entry to be analyzed
916 *
917 * This functon assumes the caller holds rcu_read_lock().
918 * Returns best originator list entry or NULL on errors.
919 */
920static struct batadv_tt_orig_list_entry *
921batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
922{
923 struct batadv_neigh_node *router = NULL;
924 struct hlist_head *head;
925 struct hlist_node *node;
926 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
927 int best_tq = 0;
928
929 head = &tt_global_entry->orig_list;
930 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
931 router = batadv_orig_node_get_router(orig_entry->orig_node);
932 if (!router)
933 continue;
934
935 if (router->tq_avg > best_tq) {
936 best_entry = orig_entry;
937 best_tq = router->tq_avg;
938 }
939
940 batadv_neigh_node_free_ref(router);
941 }
942
943 return best_entry;
944}
945
946/* batadv_tt_global_print_entry - print all orig nodes who announce the address
947 * for this global entry
948 * @tt_global_entry: global translation table entry to be printed
949 * @seq: debugfs table seq_file struct
950 *
951 * This functon assumes the caller holds rcu_read_lock().
db08e6e5 952 */
a513088d 953static void
56303d34 954batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
a513088d 955 struct seq_file *seq)
db08e6e5
SW
956{
957 struct hlist_head *head;
958 struct hlist_node *node;
981d8900 959 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
56303d34 960 struct batadv_tt_common_entry *tt_common_entry;
db08e6e5
SW
961 uint16_t flags;
962 uint8_t last_ttvn;
963
964 tt_common_entry = &tt_global_entry->common;
981d8900
SE
965 flags = tt_common_entry->flags;
966
967 best_entry = batadv_transtable_best_orig(tt_global_entry);
968 if (best_entry) {
969 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
970 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
971 '*', tt_global_entry->common.addr,
972 best_entry->ttvn, best_entry->orig_node->orig,
973 last_ttvn,
974 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
975 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
976 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
977 }
db08e6e5
SW
978
979 head = &tt_global_entry->orig_list;
980
981 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
981d8900
SE
982 if (best_entry == orig_entry)
983 continue;
984
db08e6e5 985 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
981d8900
SE
986 seq_printf(seq, " %c %pM (%3u) via %pM (%3u) [%c%c%c]\n",
987 '+', tt_global_entry->common.addr,
988 orig_entry->ttvn, orig_entry->orig_node->orig,
989 last_ttvn,
acd34afa 990 (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
30cfd02b
AQ
991 (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
992 (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
db08e6e5
SW
993 }
994}
995
08c36d3e 996int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
997{
998 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 999 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 1000 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
56303d34
SE
1001 struct batadv_tt_common_entry *tt_common_entry;
1002 struct batadv_tt_global_entry *tt_global;
1003 struct batadv_hard_iface *primary_if;
7aadf889 1004 struct hlist_node *node;
c6c8fea2 1005 struct hlist_head *head;
c90681b8 1006 uint32_t i;
c6c8fea2 1007
30da63a6
ML
1008 primary_if = batadv_seq_print_text_primary_if_get(seq);
1009 if (!primary_if)
32ae9b22 1010 goto out;
c6c8fea2 1011
2dafb49d
AQ
1012 seq_printf(seq,
1013 "Globally announced TT entries received via the mesh %s\n",
c6c8fea2 1014 net_dev->name);
df6edb9e
AQ
1015 seq_printf(seq, " %-13s %s %-15s %s %s\n",
1016 "Client", "(TTVN)", "Originator", "(Curr TTVN)", "Flags");
c6c8fea2 1017
c6c8fea2
SE
1018 for (i = 0; i < hash->size; i++) {
1019 head = &hash->table[i];
1020
7aadf889 1021 rcu_read_lock();
48100bac 1022 hlist_for_each_entry_rcu(tt_common_entry, node,
7aadf889 1023 head, hash_entry) {
56303d34
SE
1024 tt_global = container_of(tt_common_entry,
1025 struct batadv_tt_global_entry,
1026 common);
1027 batadv_tt_global_print_entry(tt_global, seq);
c6c8fea2 1028 }
7aadf889 1029 rcu_read_unlock();
c6c8fea2 1030 }
32ae9b22
ML
1031out:
1032 if (primary_if)
e5d89254 1033 batadv_hardif_free_ref(primary_if);
30da63a6 1034 return 0;
c6c8fea2
SE
1035}
1036
db08e6e5 1037/* deletes the orig list of a tt_global_entry */
a513088d 1038static void
56303d34 1039batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
c6c8fea2 1040{
db08e6e5
SW
1041 struct hlist_head *head;
1042 struct hlist_node *node, *safe;
56303d34 1043 struct batadv_tt_orig_list_entry *orig_entry;
a73105b8 1044
db08e6e5
SW
1045 spin_lock_bh(&tt_global_entry->list_lock);
1046 head = &tt_global_entry->orig_list;
1047 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1048 hlist_del_rcu(node);
a513088d 1049 batadv_tt_orig_list_entry_free_ref(orig_entry);
db08e6e5
SW
1050 }
1051 spin_unlock_bh(&tt_global_entry->list_lock);
c6c8fea2 1052
db08e6e5
SW
1053}
1054
a513088d 1055static void
56303d34
SE
1056batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1057 struct batadv_tt_global_entry *tt_global_entry,
1058 struct batadv_orig_node *orig_node,
a513088d 1059 const char *message)
db08e6e5
SW
1060{
1061 struct hlist_head *head;
1062 struct hlist_node *node, *safe;
56303d34 1063 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5
SW
1064
1065 spin_lock_bh(&tt_global_entry->list_lock);
1066 head = &tt_global_entry->orig_list;
1067 hlist_for_each_entry_safe(orig_entry, node, safe, head, list) {
1068 if (orig_entry->orig_node == orig_node) {
39c75a51 1069 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
1070 "Deleting %pM from global tt entry %pM: %s\n",
1071 orig_node->orig,
1072 tt_global_entry->common.addr, message);
db08e6e5 1073 hlist_del_rcu(node);
a513088d 1074 batadv_tt_orig_list_entry_free_ref(orig_entry);
db08e6e5
SW
1075 }
1076 }
1077 spin_unlock_bh(&tt_global_entry->list_lock);
1078}
1079
db08e6e5 1080/* If the client is to be deleted, we check if it is the last origantor entry
acd34afa
SE
1081 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1082 * timer, otherwise we simply remove the originator scheduled for deletion.
db08e6e5 1083 */
a513088d 1084static void
56303d34
SE
1085batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1086 struct batadv_tt_global_entry *tt_global_entry,
1087 struct batadv_orig_node *orig_node,
1088 const char *message)
db08e6e5
SW
1089{
1090 bool last_entry = true;
1091 struct hlist_head *head;
1092 struct hlist_node *node;
56303d34 1093 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5
SW
1094
1095 /* no local entry exists, case 1:
1096 * Check if this is the last one or if other entries exist.
1097 */
1098
1099 rcu_read_lock();
1100 head = &tt_global_entry->orig_list;
1101 hlist_for_each_entry_rcu(orig_entry, node, head, list) {
1102 if (orig_entry->orig_node != orig_node) {
1103 last_entry = false;
1104 break;
1105 }
1106 }
1107 rcu_read_unlock();
1108
1109 if (last_entry) {
1110 /* its the last one, mark for roaming. */
acd34afa 1111 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
db08e6e5
SW
1112 tt_global_entry->roam_at = jiffies;
1113 } else
1114 /* there is another entry, we can simply delete this
1115 * one and can still use the other one.
1116 */
a513088d
SE
1117 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1118 orig_node, message);
db08e6e5
SW
1119}
1120
1121
1122
56303d34
SE
1123static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1124 struct batadv_orig_node *orig_node,
a513088d
SE
1125 const unsigned char *addr,
1126 const char *message, bool roaming)
a73105b8 1127{
170173bf 1128 struct batadv_tt_global_entry *tt_global_entry;
56303d34 1129 struct batadv_tt_local_entry *local_entry = NULL;
a73105b8 1130
a513088d 1131 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
db08e6e5 1132 if (!tt_global_entry)
7683fdc1 1133 goto out;
a73105b8 1134
db08e6e5 1135 if (!roaming) {
a513088d
SE
1136 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1137 orig_node, message);
db08e6e5
SW
1138
1139 if (hlist_empty(&tt_global_entry->orig_list))
be73b488
AQ
1140 batadv_tt_global_free(bat_priv, tt_global_entry,
1141 message);
db08e6e5
SW
1142
1143 goto out;
1144 }
92f90f56
SE
1145
1146 /* if we are deleting a global entry due to a roam
1147 * event, there are two possibilities:
db08e6e5
SW
1148 * 1) the client roamed from node A to node B => if there
1149 * is only one originator left for this client, we mark
acd34afa 1150 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
92f90f56
SE
1151 * wait for node B to claim it. In case of timeout
1152 * the entry is purged.
db08e6e5
SW
1153 *
1154 * If there are other originators left, we directly delete
1155 * the originator.
92f90f56 1156 * 2) the client roamed to us => we can directly delete
9cfc7bd6
SE
1157 * the global entry, since it is useless now.
1158 */
a513088d
SE
1159 local_entry = batadv_tt_local_hash_find(bat_priv,
1160 tt_global_entry->common.addr);
1161 if (local_entry) {
db08e6e5 1162 /* local entry exists, case 2: client roamed to us. */
a513088d 1163 batadv_tt_global_del_orig_list(tt_global_entry);
be73b488 1164 batadv_tt_global_free(bat_priv, tt_global_entry, message);
db08e6e5
SW
1165 } else
1166 /* no local entry exists, case 1: check for roaming */
a513088d
SE
1167 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1168 orig_node, message);
92f90f56 1169
92f90f56 1170
cc47f66e 1171out:
7683fdc1 1172 if (tt_global_entry)
a513088d
SE
1173 batadv_tt_global_entry_free_ref(tt_global_entry);
1174 if (local_entry)
1175 batadv_tt_local_entry_free_ref(local_entry);
a73105b8
AQ
1176}
1177
56303d34
SE
1178void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1179 struct batadv_orig_node *orig_node,
1180 const char *message)
c6c8fea2 1181{
56303d34
SE
1182 struct batadv_tt_global_entry *tt_global;
1183 struct batadv_tt_common_entry *tt_common_entry;
c90681b8 1184 uint32_t i;
807736f6 1185 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
a73105b8
AQ
1186 struct hlist_node *node, *safe;
1187 struct hlist_head *head;
7683fdc1 1188 spinlock_t *list_lock; /* protects write access to the hash lists */
c6c8fea2 1189
6e801494
SW
1190 if (!hash)
1191 return;
1192
a73105b8
AQ
1193 for (i = 0; i < hash->size; i++) {
1194 head = &hash->table[i];
7683fdc1 1195 list_lock = &hash->list_locks[i];
c6c8fea2 1196
7683fdc1 1197 spin_lock_bh(list_lock);
48100bac 1198 hlist_for_each_entry_safe(tt_common_entry, node, safe,
7c64fd98 1199 head, hash_entry) {
56303d34
SE
1200 tt_global = container_of(tt_common_entry,
1201 struct batadv_tt_global_entry,
1202 common);
db08e6e5 1203
56303d34 1204 batadv_tt_global_del_orig_entry(bat_priv, tt_global,
a513088d 1205 orig_node, message);
db08e6e5 1206
56303d34 1207 if (hlist_empty(&tt_global->orig_list)) {
39c75a51 1208 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 1209 "Deleting global tt entry %pM: %s\n",
56303d34 1210 tt_global->common.addr, message);
7683fdc1 1211 hlist_del_rcu(node);
56303d34 1212 batadv_tt_global_entry_free_ref(tt_global);
7683fdc1 1213 }
a73105b8 1214 }
7683fdc1 1215 spin_unlock_bh(list_lock);
c6c8fea2 1216 }
17071578 1217 orig_node->tt_initialised = false;
c6c8fea2
SE
1218}
1219
30cfd02b
AQ
1220static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1221 char **msg)
cc47f66e 1222{
30cfd02b
AQ
1223 bool purge = false;
1224 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1225 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
42d0b044 1226
30cfd02b
AQ
1227 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1228 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1229 purge = true;
1230 *msg = "Roaming timeout\n";
1231 }
42d0b044 1232
30cfd02b
AQ
1233 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1234 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1235 purge = true;
1236 *msg = "Temporary client timeout\n";
42d0b044 1237 }
30cfd02b
AQ
1238
1239 return purge;
42d0b044
SE
1240}
1241
30cfd02b 1242static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
42d0b044 1243{
807736f6 1244 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
cc47f66e 1245 struct hlist_head *head;
30cfd02b 1246 struct hlist_node *node, *node_tmp;
7683fdc1 1247 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 1248 uint32_t i;
30cfd02b
AQ
1249 char *msg = NULL;
1250 struct batadv_tt_common_entry *tt_common;
1251 struct batadv_tt_global_entry *tt_global;
cc47f66e 1252
cc47f66e
AQ
1253 for (i = 0; i < hash->size; i++) {
1254 head = &hash->table[i];
7683fdc1 1255 list_lock = &hash->list_locks[i];
cc47f66e 1256
7683fdc1 1257 spin_lock_bh(list_lock);
30cfd02b
AQ
1258 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
1259 hash_entry) {
1260 tt_global = container_of(tt_common,
1261 struct batadv_tt_global_entry,
1262 common);
1263
1264 if (!batadv_tt_global_to_purge(tt_global, &msg))
1265 continue;
1266
1267 batadv_dbg(BATADV_DBG_TT, bat_priv,
1268 "Deleting global tt entry (%pM): %s\n",
1269 tt_global->common.addr, msg);
1270
1271 hlist_del_rcu(node);
1272
1273 batadv_tt_global_entry_free_ref(tt_global);
1274 }
7683fdc1 1275 spin_unlock_bh(list_lock);
cc47f66e 1276 }
cc47f66e
AQ
1277}
1278
56303d34 1279static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
c6c8fea2 1280{
5bf74e9c 1281 struct batadv_hashtable *hash;
7683fdc1 1282 spinlock_t *list_lock; /* protects write access to the hash lists */
56303d34
SE
1283 struct batadv_tt_common_entry *tt_common_entry;
1284 struct batadv_tt_global_entry *tt_global;
7683fdc1
AQ
1285 struct hlist_node *node, *node_tmp;
1286 struct hlist_head *head;
c90681b8 1287 uint32_t i;
7683fdc1 1288
807736f6 1289 if (!bat_priv->tt.global_hash)
c6c8fea2
SE
1290 return;
1291
807736f6 1292 hash = bat_priv->tt.global_hash;
7683fdc1
AQ
1293
1294 for (i = 0; i < hash->size; i++) {
1295 head = &hash->table[i];
1296 list_lock = &hash->list_locks[i];
1297
1298 spin_lock_bh(list_lock);
48100bac 1299 hlist_for_each_entry_safe(tt_common_entry, node, node_tmp,
7683fdc1
AQ
1300 head, hash_entry) {
1301 hlist_del_rcu(node);
56303d34
SE
1302 tt_global = container_of(tt_common_entry,
1303 struct batadv_tt_global_entry,
1304 common);
1305 batadv_tt_global_entry_free_ref(tt_global);
7683fdc1
AQ
1306 }
1307 spin_unlock_bh(list_lock);
1308 }
1309
1a8eaf07 1310 batadv_hash_destroy(hash);
7683fdc1 1311
807736f6 1312 bat_priv->tt.global_hash = NULL;
c6c8fea2
SE
1313}
1314
56303d34
SE
1315static bool
1316_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1317 struct batadv_tt_global_entry *tt_global_entry)
59b699cd
AQ
1318{
1319 bool ret = false;
1320
acd34afa
SE
1321 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1322 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
59b699cd
AQ
1323 ret = true;
1324
1325 return ret;
1326}
1327
56303d34
SE
1328struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1329 const uint8_t *src,
1330 const uint8_t *addr)
c6c8fea2 1331{
56303d34
SE
1332 struct batadv_tt_local_entry *tt_local_entry = NULL;
1333 struct batadv_tt_global_entry *tt_global_entry = NULL;
1334 struct batadv_orig_node *orig_node = NULL;
981d8900 1335 struct batadv_tt_orig_list_entry *best_entry;
c6c8fea2 1336
3d393e47 1337 if (src && atomic_read(&bat_priv->ap_isolation)) {
a513088d 1338 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
068ee6e2
AQ
1339 if (!tt_local_entry ||
1340 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
3d393e47
AQ
1341 goto out;
1342 }
7aadf889 1343
a513088d 1344 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2dafb49d 1345 if (!tt_global_entry)
7b36e8ee 1346 goto out;
7aadf889 1347
3d393e47 1348 /* check whether the clients should not communicate due to AP
9cfc7bd6
SE
1349 * isolation
1350 */
a513088d
SE
1351 if (tt_local_entry &&
1352 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3d393e47
AQ
1353 goto out;
1354
db08e6e5 1355 rcu_read_lock();
981d8900 1356 best_entry = batadv_transtable_best_orig(tt_global_entry);
db08e6e5 1357 /* found anything? */
981d8900
SE
1358 if (best_entry)
1359 orig_node = best_entry->orig_node;
db08e6e5
SW
1360 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1361 orig_node = NULL;
1362 rcu_read_unlock();
981d8900 1363
7b36e8ee 1364out:
3d393e47 1365 if (tt_global_entry)
a513088d 1366 batadv_tt_global_entry_free_ref(tt_global_entry);
3d393e47 1367 if (tt_local_entry)
a513088d 1368 batadv_tt_local_entry_free_ref(tt_local_entry);
3d393e47 1369
7b36e8ee 1370 return orig_node;
c6c8fea2 1371}
a73105b8
AQ
1372
1373/* Calculates the checksum of the local table of a given orig_node */
56303d34
SE
1374static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1375 struct batadv_orig_node *orig_node)
a73105b8
AQ
1376{
1377 uint16_t total = 0, total_one;
807736f6 1378 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
56303d34
SE
1379 struct batadv_tt_common_entry *tt_common;
1380 struct batadv_tt_global_entry *tt_global;
a73105b8
AQ
1381 struct hlist_node *node;
1382 struct hlist_head *head;
c90681b8
AQ
1383 uint32_t i;
1384 int j;
a73105b8
AQ
1385
1386 for (i = 0; i < hash->size; i++) {
1387 head = &hash->table[i];
1388
1389 rcu_read_lock();
acd34afa 1390 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
56303d34
SE
1391 tt_global = container_of(tt_common,
1392 struct batadv_tt_global_entry,
1393 common);
db08e6e5
SW
1394 /* Roaming clients are in the global table for
1395 * consistency only. They don't have to be
1396 * taken into account while computing the
1397 * global crc
1398 */
acd34afa 1399 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
db08e6e5 1400 continue;
30cfd02b
AQ
1401 /* Temporary clients have not been announced yet, so
1402 * they have to be skipped while computing the global
1403 * crc
1404 */
1405 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1406 continue;
db08e6e5
SW
1407
1408 /* find out if this global entry is announced by this
1409 * originator
1410 */
56303d34 1411 if (!batadv_tt_global_entry_has_orig(tt_global,
a513088d 1412 orig_node))
db08e6e5
SW
1413 continue;
1414
1415 total_one = 0;
1416 for (j = 0; j < ETH_ALEN; j++)
1417 total_one = crc16_byte(total_one,
acd34afa 1418 tt_common->addr[j]);
db08e6e5 1419 total ^= total_one;
a73105b8
AQ
1420 }
1421 rcu_read_unlock();
1422 }
1423
1424 return total;
1425}
1426
1427/* Calculates the checksum of the local table */
56303d34 1428static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
a73105b8
AQ
1429{
1430 uint16_t total = 0, total_one;
807736f6 1431 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34 1432 struct batadv_tt_common_entry *tt_common;
a73105b8
AQ
1433 struct hlist_node *node;
1434 struct hlist_head *head;
c90681b8
AQ
1435 uint32_t i;
1436 int j;
a73105b8
AQ
1437
1438 for (i = 0; i < hash->size; i++) {
1439 head = &hash->table[i];
1440
1441 rcu_read_lock();
acd34afa 1442 hlist_for_each_entry_rcu(tt_common, node, head, hash_entry) {
058d0e26 1443 /* not yet committed clients have not to be taken into
9cfc7bd6
SE
1444 * account while computing the CRC
1445 */
acd34afa 1446 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
058d0e26 1447 continue;
a73105b8
AQ
1448 total_one = 0;
1449 for (j = 0; j < ETH_ALEN; j++)
1450 total_one = crc16_byte(total_one,
acd34afa 1451 tt_common->addr[j]);
a73105b8
AQ
1452 total ^= total_one;
1453 }
a73105b8
AQ
1454 rcu_read_unlock();
1455 }
1456
1457 return total;
1458}
1459
56303d34 1460static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
a73105b8 1461{
56303d34 1462 struct batadv_tt_req_node *node, *safe;
a73105b8 1463
807736f6 1464 spin_lock_bh(&bat_priv->tt.req_list_lock);
a73105b8 1465
807736f6 1466 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
a73105b8
AQ
1467 list_del(&node->list);
1468 kfree(node);
1469 }
1470
807736f6 1471 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
1472}
1473
56303d34
SE
1474static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1475 struct batadv_orig_node *orig_node,
a513088d
SE
1476 const unsigned char *tt_buff,
1477 uint8_t tt_num_changes)
a73105b8 1478{
08c36d3e 1479 uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
a73105b8
AQ
1480
1481 /* Replace the old buffer only if I received something in the
9cfc7bd6
SE
1482 * last OGM (the OGM could carry no changes)
1483 */
a73105b8
AQ
1484 spin_lock_bh(&orig_node->tt_buff_lock);
1485 if (tt_buff_len > 0) {
1486 kfree(orig_node->tt_buff);
1487 orig_node->tt_buff_len = 0;
1488 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1489 if (orig_node->tt_buff) {
1490 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1491 orig_node->tt_buff_len = tt_buff_len;
1492 }
1493 }
1494 spin_unlock_bh(&orig_node->tt_buff_lock);
1495}
1496
56303d34 1497static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
a73105b8 1498{
56303d34 1499 struct batadv_tt_req_node *node, *safe;
a73105b8 1500
807736f6
SE
1501 spin_lock_bh(&bat_priv->tt.req_list_lock);
1502 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
42d0b044
SE
1503 if (batadv_has_timed_out(node->issued_at,
1504 BATADV_TT_REQUEST_TIMEOUT)) {
a73105b8
AQ
1505 list_del(&node->list);
1506 kfree(node);
1507 }
1508 }
807736f6 1509 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
1510}
1511
1512/* returns the pointer to the new tt_req_node struct if no request
9cfc7bd6
SE
1513 * has already been issued for this orig_node, NULL otherwise
1514 */
56303d34
SE
1515static struct batadv_tt_req_node *
1516batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1517 struct batadv_orig_node *orig_node)
a73105b8 1518{
56303d34 1519 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
a73105b8 1520
807736f6
SE
1521 spin_lock_bh(&bat_priv->tt.req_list_lock);
1522 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1eda58bf
SE
1523 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1524 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
42d0b044 1525 BATADV_TT_REQUEST_TIMEOUT))
a73105b8
AQ
1526 goto unlock;
1527 }
1528
1529 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1530 if (!tt_req_node)
1531 goto unlock;
1532
1533 memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1534 tt_req_node->issued_at = jiffies;
1535
807736f6 1536 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
a73105b8 1537unlock:
807736f6 1538 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
1539 return tt_req_node;
1540}
1541
058d0e26 1542/* data_ptr is useless here, but has to be kept to respect the prototype */
a513088d
SE
1543static int batadv_tt_local_valid_entry(const void *entry_ptr,
1544 const void *data_ptr)
058d0e26 1545{
56303d34 1546 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
058d0e26 1547
acd34afa 1548 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
058d0e26
AQ
1549 return 0;
1550 return 1;
1551}
1552
a513088d
SE
1553static int batadv_tt_global_valid(const void *entry_ptr,
1554 const void *data_ptr)
a73105b8 1555{
56303d34
SE
1556 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1557 const struct batadv_tt_global_entry *tt_global_entry;
1558 const struct batadv_orig_node *orig_node = data_ptr;
a73105b8 1559
30cfd02b
AQ
1560 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1561 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
cc47f66e
AQ
1562 return 0;
1563
56303d34
SE
1564 tt_global_entry = container_of(tt_common_entry,
1565 struct batadv_tt_global_entry,
48100bac
AQ
1566 common);
1567
a513088d 1568 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
a73105b8
AQ
1569}
1570
a513088d
SE
1571static struct sk_buff *
1572batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
5bf74e9c 1573 struct batadv_hashtable *hash,
56303d34 1574 struct batadv_hard_iface *primary_if,
a513088d
SE
1575 int (*valid_cb)(const void *, const void *),
1576 void *cb_data)
a73105b8 1577{
56303d34 1578 struct batadv_tt_common_entry *tt_common_entry;
96412690
SE
1579 struct batadv_tt_query_packet *tt_response;
1580 struct batadv_tt_change *tt_change;
a73105b8
AQ
1581 struct hlist_node *node;
1582 struct hlist_head *head;
1583 struct sk_buff *skb = NULL;
1584 uint16_t tt_tot, tt_count;
96412690 1585 ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
c90681b8 1586 uint32_t i;
96412690 1587 size_t len;
a73105b8
AQ
1588
1589 if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1590 tt_len = primary_if->soft_iface->mtu - tt_query_size;
96412690 1591 tt_len -= tt_len % sizeof(struct batadv_tt_change);
a73105b8 1592 }
96412690 1593 tt_tot = tt_len / sizeof(struct batadv_tt_change);
a73105b8 1594
96412690 1595 len = tt_query_size + tt_len;
5b246574 1596 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
a73105b8
AQ
1597 if (!skb)
1598 goto out;
1599
5b246574 1600 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
96412690 1601 tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
a73105b8 1602 tt_response->ttvn = ttvn;
a73105b8 1603
96412690 1604 tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
a73105b8
AQ
1605 tt_count = 0;
1606
1607 rcu_read_lock();
1608 for (i = 0; i < hash->size; i++) {
1609 head = &hash->table[i];
1610
48100bac 1611 hlist_for_each_entry_rcu(tt_common_entry, node,
a73105b8
AQ
1612 head, hash_entry) {
1613 if (tt_count == tt_tot)
1614 break;
1615
48100bac 1616 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
a73105b8
AQ
1617 continue;
1618
48100bac
AQ
1619 memcpy(tt_change->addr, tt_common_entry->addr,
1620 ETH_ALEN);
27b37ebf 1621 tt_change->flags = tt_common_entry->flags;
a73105b8
AQ
1622
1623 tt_count++;
1624 tt_change++;
1625 }
1626 }
1627 rcu_read_unlock();
1628
9d852393 1629 /* store in the message the number of entries we have successfully
9cfc7bd6
SE
1630 * copied
1631 */
9d852393
AQ
1632 tt_response->tt_data = htons(tt_count);
1633
a73105b8
AQ
1634out:
1635 return skb;
1636}
1637
56303d34
SE
1638static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1639 struct batadv_orig_node *dst_orig_node,
a513088d
SE
1640 uint8_t ttvn, uint16_t tt_crc,
1641 bool full_table)
a73105b8
AQ
1642{
1643 struct sk_buff *skb = NULL;
96412690 1644 struct batadv_tt_query_packet *tt_request;
56303d34
SE
1645 struct batadv_neigh_node *neigh_node = NULL;
1646 struct batadv_hard_iface *primary_if;
1647 struct batadv_tt_req_node *tt_req_node = NULL;
a73105b8 1648 int ret = 1;
96412690 1649 size_t tt_req_len;
a73105b8 1650
e5d89254 1651 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
1652 if (!primary_if)
1653 goto out;
1654
1655 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
1656 * reply from the same orig_node yet
1657 */
a513088d 1658 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
a73105b8
AQ
1659 if (!tt_req_node)
1660 goto out;
1661
5b246574 1662 skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
a73105b8
AQ
1663 if (!skb)
1664 goto out;
1665
5b246574 1666 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
a73105b8 1667
96412690
SE
1668 tt_req_len = sizeof(*tt_request);
1669 tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
a73105b8 1670
acd34afa 1671 tt_request->header.packet_type = BATADV_TT_QUERY;
7e071c79 1672 tt_request->header.version = BATADV_COMPAT_VERSION;
a73105b8
AQ
1673 memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1674 memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
42d0b044 1675 tt_request->header.ttl = BATADV_TTL;
a73105b8 1676 tt_request->ttvn = ttvn;
6d2003fc 1677 tt_request->tt_data = htons(tt_crc);
acd34afa 1678 tt_request->flags = BATADV_TT_REQUEST;
a73105b8
AQ
1679
1680 if (full_table)
acd34afa 1681 tt_request->flags |= BATADV_TT_FULL_TABLE;
a73105b8 1682
7d211efc 1683 neigh_node = batadv_orig_node_get_router(dst_orig_node);
a73105b8
AQ
1684 if (!neigh_node)
1685 goto out;
1686
39c75a51 1687 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
1688 "Sending TT_REQUEST to %pM via %pM [%c]\n",
1689 dst_orig_node->orig, neigh_node->addr,
1690 (full_table ? 'F' : '.'));
a73105b8 1691
d69909d2 1692 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
f8214865 1693
9455e34c 1694 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
a73105b8
AQ
1695 ret = 0;
1696
1697out:
1698 if (neigh_node)
7d211efc 1699 batadv_neigh_node_free_ref(neigh_node);
a73105b8 1700 if (primary_if)
e5d89254 1701 batadv_hardif_free_ref(primary_if);
a73105b8
AQ
1702 if (ret)
1703 kfree_skb(skb);
1704 if (ret && tt_req_node) {
807736f6 1705 spin_lock_bh(&bat_priv->tt.req_list_lock);
a73105b8 1706 list_del(&tt_req_node->list);
807736f6 1707 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
1708 kfree(tt_req_node);
1709 }
1710 return ret;
1711}
1712
96412690 1713static bool
56303d34 1714batadv_send_other_tt_response(struct batadv_priv *bat_priv,
96412690 1715 struct batadv_tt_query_packet *tt_request)
a73105b8 1716{
170173bf 1717 struct batadv_orig_node *req_dst_orig_node;
56303d34
SE
1718 struct batadv_orig_node *res_dst_orig_node = NULL;
1719 struct batadv_neigh_node *neigh_node = NULL;
1720 struct batadv_hard_iface *primary_if = NULL;
a73105b8
AQ
1721 uint8_t orig_ttvn, req_ttvn, ttvn;
1722 int ret = false;
1723 unsigned char *tt_buff;
1724 bool full_table;
1725 uint16_t tt_len, tt_tot;
1726 struct sk_buff *skb = NULL;
96412690 1727 struct batadv_tt_query_packet *tt_response;
c67893d1 1728 uint8_t *packet_pos;
96412690 1729 size_t len;
a73105b8 1730
39c75a51 1731 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
1732 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1733 tt_request->src, tt_request->ttvn, tt_request->dst,
acd34afa 1734 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
a73105b8
AQ
1735
1736 /* Let's get the orig node of the REAL destination */
da641193 1737 req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
a73105b8
AQ
1738 if (!req_dst_orig_node)
1739 goto out;
1740
da641193 1741 res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
a73105b8
AQ
1742 if (!res_dst_orig_node)
1743 goto out;
1744
7d211efc 1745 neigh_node = batadv_orig_node_get_router(res_dst_orig_node);
a73105b8
AQ
1746 if (!neigh_node)
1747 goto out;
1748
e5d89254 1749 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
1750 if (!primary_if)
1751 goto out;
1752
1753 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1754 req_ttvn = tt_request->ttvn;
1755
015758d0 1756 /* I don't have the requested data */
a73105b8 1757 if (orig_ttvn != req_ttvn ||
f25bd58a 1758 tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
a73105b8
AQ
1759 goto out;
1760
015758d0 1761 /* If the full table has been explicitly requested */
acd34afa 1762 if (tt_request->flags & BATADV_TT_FULL_TABLE ||
a73105b8
AQ
1763 !req_dst_orig_node->tt_buff)
1764 full_table = true;
1765 else
1766 full_table = false;
1767
1768 /* In this version, fragmentation is not implemented, then
9cfc7bd6
SE
1769 * I'll send only one packet with as much TT entries as I can
1770 */
a73105b8
AQ
1771 if (!full_table) {
1772 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1773 tt_len = req_dst_orig_node->tt_buff_len;
96412690 1774 tt_tot = tt_len / sizeof(struct batadv_tt_change);
a73105b8 1775
96412690 1776 len = sizeof(*tt_response) + tt_len;
5b246574 1777 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
a73105b8
AQ
1778 if (!skb)
1779 goto unlock;
1780
5b246574 1781 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
c67893d1
SE
1782 packet_pos = skb_put(skb, len);
1783 tt_response = (struct batadv_tt_query_packet *)packet_pos;
a73105b8
AQ
1784 tt_response->ttvn = req_ttvn;
1785 tt_response->tt_data = htons(tt_tot);
1786
96412690 1787 tt_buff = skb->data + sizeof(*tt_response);
a73105b8
AQ
1788 /* Copy the last orig_node's OGM buffer */
1789 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1790 req_dst_orig_node->tt_buff_len);
1791
1792 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1793 } else {
96412690
SE
1794 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1795 tt_len *= sizeof(struct batadv_tt_change);
a73105b8
AQ
1796 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1797
a513088d 1798 skb = batadv_tt_response_fill_table(tt_len, ttvn,
807736f6 1799 bat_priv->tt.global_hash,
a513088d
SE
1800 primary_if,
1801 batadv_tt_global_valid,
1802 req_dst_orig_node);
a73105b8
AQ
1803 if (!skb)
1804 goto out;
1805
96412690 1806 tt_response = (struct batadv_tt_query_packet *)skb->data;
a73105b8
AQ
1807 }
1808
acd34afa 1809 tt_response->header.packet_type = BATADV_TT_QUERY;
7e071c79 1810 tt_response->header.version = BATADV_COMPAT_VERSION;
42d0b044 1811 tt_response->header.ttl = BATADV_TTL;
a73105b8
AQ
1812 memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1813 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
acd34afa 1814 tt_response->flags = BATADV_TT_RESPONSE;
a73105b8
AQ
1815
1816 if (full_table)
acd34afa 1817 tt_response->flags |= BATADV_TT_FULL_TABLE;
a73105b8 1818
39c75a51 1819 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
1820 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1821 res_dst_orig_node->orig, neigh_node->addr,
1822 req_dst_orig_node->orig, req_ttvn);
a73105b8 1823
d69909d2 1824 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
f8214865 1825
9455e34c 1826 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
a73105b8
AQ
1827 ret = true;
1828 goto out;
1829
1830unlock:
1831 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1832
1833out:
1834 if (res_dst_orig_node)
7d211efc 1835 batadv_orig_node_free_ref(res_dst_orig_node);
a73105b8 1836 if (req_dst_orig_node)
7d211efc 1837 batadv_orig_node_free_ref(req_dst_orig_node);
a73105b8 1838 if (neigh_node)
7d211efc 1839 batadv_neigh_node_free_ref(neigh_node);
a73105b8 1840 if (primary_if)
e5d89254 1841 batadv_hardif_free_ref(primary_if);
a73105b8
AQ
1842 if (!ret)
1843 kfree_skb(skb);
1844 return ret;
1845
1846}
96412690
SE
1847
1848static bool
56303d34 1849batadv_send_my_tt_response(struct batadv_priv *bat_priv,
96412690 1850 struct batadv_tt_query_packet *tt_request)
a73105b8 1851{
170173bf 1852 struct batadv_orig_node *orig_node;
56303d34
SE
1853 struct batadv_neigh_node *neigh_node = NULL;
1854 struct batadv_hard_iface *primary_if = NULL;
a73105b8
AQ
1855 uint8_t my_ttvn, req_ttvn, ttvn;
1856 int ret = false;
1857 unsigned char *tt_buff;
1858 bool full_table;
1859 uint16_t tt_len, tt_tot;
1860 struct sk_buff *skb = NULL;
96412690 1861 struct batadv_tt_query_packet *tt_response;
c67893d1 1862 uint8_t *packet_pos;
96412690 1863 size_t len;
a73105b8 1864
39c75a51 1865 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
1866 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1867 tt_request->src, tt_request->ttvn,
acd34afa 1868 (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
a73105b8
AQ
1869
1870
807736f6 1871 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
a73105b8
AQ
1872 req_ttvn = tt_request->ttvn;
1873
da641193 1874 orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
a73105b8
AQ
1875 if (!orig_node)
1876 goto out;
1877
7d211efc 1878 neigh_node = batadv_orig_node_get_router(orig_node);
a73105b8
AQ
1879 if (!neigh_node)
1880 goto out;
1881
e5d89254 1882 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
1883 if (!primary_if)
1884 goto out;
1885
1886 /* If the full table has been explicitly requested or the gap
9cfc7bd6
SE
1887 * is too big send the whole local translation table
1888 */
acd34afa 1889 if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
807736f6 1890 !bat_priv->tt.last_changeset)
a73105b8
AQ
1891 full_table = true;
1892 else
1893 full_table = false;
1894
1895 /* In this version, fragmentation is not implemented, then
9cfc7bd6
SE
1896 * I'll send only one packet with as much TT entries as I can
1897 */
a73105b8 1898 if (!full_table) {
807736f6
SE
1899 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1900 tt_len = bat_priv->tt.last_changeset_len;
96412690 1901 tt_tot = tt_len / sizeof(struct batadv_tt_change);
a73105b8 1902
96412690 1903 len = sizeof(*tt_response) + tt_len;
5b246574 1904 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
a73105b8
AQ
1905 if (!skb)
1906 goto unlock;
1907
5b246574 1908 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
c67893d1
SE
1909 packet_pos = skb_put(skb, len);
1910 tt_response = (struct batadv_tt_query_packet *)packet_pos;
a73105b8
AQ
1911 tt_response->ttvn = req_ttvn;
1912 tt_response->tt_data = htons(tt_tot);
1913
96412690 1914 tt_buff = skb->data + sizeof(*tt_response);
807736f6
SE
1915 memcpy(tt_buff, bat_priv->tt.last_changeset,
1916 bat_priv->tt.last_changeset_len);
1917 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 1918 } else {
807736f6 1919 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
96412690 1920 tt_len *= sizeof(struct batadv_tt_change);
807736f6 1921 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
a73105b8 1922
a513088d 1923 skb = batadv_tt_response_fill_table(tt_len, ttvn,
807736f6 1924 bat_priv->tt.local_hash,
a513088d
SE
1925 primary_if,
1926 batadv_tt_local_valid_entry,
1927 NULL);
a73105b8
AQ
1928 if (!skb)
1929 goto out;
1930
96412690 1931 tt_response = (struct batadv_tt_query_packet *)skb->data;
a73105b8
AQ
1932 }
1933
acd34afa 1934 tt_response->header.packet_type = BATADV_TT_QUERY;
7e071c79 1935 tt_response->header.version = BATADV_COMPAT_VERSION;
42d0b044 1936 tt_response->header.ttl = BATADV_TTL;
a73105b8
AQ
1937 memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1938 memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
acd34afa 1939 tt_response->flags = BATADV_TT_RESPONSE;
a73105b8
AQ
1940
1941 if (full_table)
acd34afa 1942 tt_response->flags |= BATADV_TT_FULL_TABLE;
a73105b8 1943
39c75a51 1944 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
1945 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1946 orig_node->orig, neigh_node->addr,
acd34afa 1947 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
a73105b8 1948
d69909d2 1949 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
f8214865 1950
9455e34c 1951 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
a73105b8
AQ
1952 ret = true;
1953 goto out;
1954
1955unlock:
807736f6 1956 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8
AQ
1957out:
1958 if (orig_node)
7d211efc 1959 batadv_orig_node_free_ref(orig_node);
a73105b8 1960 if (neigh_node)
7d211efc 1961 batadv_neigh_node_free_ref(neigh_node);
a73105b8 1962 if (primary_if)
e5d89254 1963 batadv_hardif_free_ref(primary_if);
a73105b8
AQ
1964 if (!ret)
1965 kfree_skb(skb);
1966 /* This packet was for me, so it doesn't need to be re-routed */
1967 return true;
1968}
1969
56303d34 1970bool batadv_send_tt_response(struct batadv_priv *bat_priv,
96412690 1971 struct batadv_tt_query_packet *tt_request)
a73105b8 1972{
3193e8fd 1973 if (batadv_is_my_mac(tt_request->dst)) {
20ff9d59 1974 /* don't answer backbone gws! */
08adf151 1975 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
20ff9d59
SW
1976 return true;
1977
a513088d 1978 return batadv_send_my_tt_response(bat_priv, tt_request);
20ff9d59 1979 } else {
a513088d 1980 return batadv_send_other_tt_response(bat_priv, tt_request);
20ff9d59 1981 }
a73105b8
AQ
1982}
1983
56303d34
SE
1984static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1985 struct batadv_orig_node *orig_node,
96412690 1986 struct batadv_tt_change *tt_change,
a513088d 1987 uint16_t tt_num_changes, uint8_t ttvn)
a73105b8
AQ
1988{
1989 int i;
a513088d 1990 int roams;
a73105b8
AQ
1991
1992 for (i = 0; i < tt_num_changes; i++) {
acd34afa
SE
1993 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1994 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
a513088d
SE
1995 batadv_tt_global_del(bat_priv, orig_node,
1996 (tt_change + i)->addr,
d4f44692
AQ
1997 "tt removed by changes",
1998 roams);
08c36d3e 1999 } else {
08c36d3e 2000 if (!batadv_tt_global_add(bat_priv, orig_node,
d4f44692
AQ
2001 (tt_change + i)->addr,
2002 (tt_change + i)->flags, ttvn))
a73105b8
AQ
2003 /* In case of problem while storing a
2004 * global_entry, we stop the updating
2005 * procedure without committing the
2006 * ttvn change. This will avoid to send
2007 * corrupted data on tt_request
2008 */
2009 return;
08c36d3e 2010 }
a73105b8 2011 }
17071578 2012 orig_node->tt_initialised = true;
a73105b8
AQ
2013}
2014
56303d34 2015static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
96412690 2016 struct batadv_tt_query_packet *tt_response)
a73105b8 2017{
170173bf 2018 struct batadv_orig_node *orig_node;
a73105b8 2019
da641193 2020 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
a73105b8
AQ
2021 if (!orig_node)
2022 goto out;
2023
2024 /* Purge the old table first.. */
08c36d3e 2025 batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
a73105b8 2026
a513088d 2027 _batadv_tt_update_changes(bat_priv, orig_node,
96412690 2028 (struct batadv_tt_change *)(tt_response + 1),
a513088d
SE
2029 ntohs(tt_response->tt_data),
2030 tt_response->ttvn);
a73105b8
AQ
2031
2032 spin_lock_bh(&orig_node->tt_buff_lock);
2033 kfree(orig_node->tt_buff);
2034 orig_node->tt_buff_len = 0;
2035 orig_node->tt_buff = NULL;
2036 spin_unlock_bh(&orig_node->tt_buff_lock);
2037
2038 atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2039
2040out:
2041 if (orig_node)
7d211efc 2042 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
2043}
2044
56303d34
SE
2045static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2046 struct batadv_orig_node *orig_node,
a513088d 2047 uint16_t tt_num_changes, uint8_t ttvn,
96412690 2048 struct batadv_tt_change *tt_change)
a73105b8 2049{
a513088d
SE
2050 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2051 tt_num_changes, ttvn);
a73105b8 2052
a513088d
SE
2053 batadv_tt_save_orig_buffer(bat_priv, orig_node,
2054 (unsigned char *)tt_change, tt_num_changes);
a73105b8
AQ
2055 atomic_set(&orig_node->last_ttvn, ttvn);
2056}
2057
56303d34 2058bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
a73105b8 2059{
170173bf 2060 struct batadv_tt_local_entry *tt_local_entry;
7683fdc1 2061 bool ret = false;
a73105b8 2062
a513088d 2063 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
7683fdc1
AQ
2064 if (!tt_local_entry)
2065 goto out;
058d0e26 2066 /* Check if the client has been logically deleted (but is kept for
9cfc7bd6
SE
2067 * consistency purpose)
2068 */
7c1fd91d
AQ
2069 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2070 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
058d0e26 2071 goto out;
7683fdc1
AQ
2072 ret = true;
2073out:
a73105b8 2074 if (tt_local_entry)
a513088d 2075 batadv_tt_local_entry_free_ref(tt_local_entry);
7683fdc1 2076 return ret;
a73105b8
AQ
2077}
2078
56303d34 2079void batadv_handle_tt_response(struct batadv_priv *bat_priv,
96412690 2080 struct batadv_tt_query_packet *tt_response)
a73105b8 2081{
56303d34
SE
2082 struct batadv_tt_req_node *node, *safe;
2083 struct batadv_orig_node *orig_node = NULL;
96412690 2084 struct batadv_tt_change *tt_change;
a73105b8 2085
39c75a51 2086 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
2087 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2088 tt_response->src, tt_response->ttvn,
2089 ntohs(tt_response->tt_data),
acd34afa 2090 (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
a73105b8 2091
20ff9d59 2092 /* we should have never asked a backbone gw */
08adf151 2093 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
20ff9d59
SW
2094 goto out;
2095
da641193 2096 orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
a73105b8
AQ
2097 if (!orig_node)
2098 goto out;
2099
96412690 2100 if (tt_response->flags & BATADV_TT_FULL_TABLE) {
a513088d 2101 batadv_tt_fill_gtable(bat_priv, tt_response);
96412690
SE
2102 } else {
2103 tt_change = (struct batadv_tt_change *)(tt_response + 1);
a513088d
SE
2104 batadv_tt_update_changes(bat_priv, orig_node,
2105 ntohs(tt_response->tt_data),
96412690
SE
2106 tt_response->ttvn, tt_change);
2107 }
a73105b8
AQ
2108
2109 /* Delete the tt_req_node from pending tt_requests list */
807736f6
SE
2110 spin_lock_bh(&bat_priv->tt.req_list_lock);
2111 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1eda58bf 2112 if (!batadv_compare_eth(node->addr, tt_response->src))
a73105b8
AQ
2113 continue;
2114 list_del(&node->list);
2115 kfree(node);
2116 }
807736f6 2117 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2118
2119 /* Recalculate the CRC for this orig_node and store it */
a513088d 2120 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
a73105b8
AQ
2121out:
2122 if (orig_node)
7d211efc 2123 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
2124}
2125
56303d34 2126int batadv_tt_init(struct batadv_priv *bat_priv)
a73105b8 2127{
5346c35e 2128 int ret;
a73105b8 2129
a513088d 2130 ret = batadv_tt_local_init(bat_priv);
5346c35e
SE
2131 if (ret < 0)
2132 return ret;
2133
a513088d 2134 ret = batadv_tt_global_init(bat_priv);
5346c35e
SE
2135 if (ret < 0)
2136 return ret;
a73105b8 2137
a513088d 2138 batadv_tt_start_timer(bat_priv);
a73105b8
AQ
2139
2140 return 1;
2141}
2142
56303d34 2143static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
a73105b8 2144{
56303d34 2145 struct batadv_tt_roam_node *node, *safe;
a73105b8 2146
807736f6 2147 spin_lock_bh(&bat_priv->tt.roam_list_lock);
a73105b8 2148
807736f6 2149 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
cc47f66e
AQ
2150 list_del(&node->list);
2151 kfree(node);
2152 }
2153
807736f6 2154 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
2155}
2156
56303d34 2157static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
cc47f66e 2158{
56303d34 2159 struct batadv_tt_roam_node *node, *safe;
cc47f66e 2160
807736f6
SE
2161 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2162 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
42d0b044
SE
2163 if (!batadv_has_timed_out(node->first_time,
2164 BATADV_ROAMING_MAX_TIME))
cc47f66e
AQ
2165 continue;
2166
2167 list_del(&node->list);
2168 kfree(node);
2169 }
807736f6 2170 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
2171}
2172
2173/* This function checks whether the client already reached the
2174 * maximum number of possible roaming phases. In this case the ROAMING_ADV
2175 * will not be sent.
2176 *
9cfc7bd6
SE
2177 * returns true if the ROAMING_ADV can be sent, false otherwise
2178 */
56303d34 2179static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
a513088d 2180 uint8_t *client)
cc47f66e 2181{
56303d34 2182 struct batadv_tt_roam_node *tt_roam_node;
cc47f66e
AQ
2183 bool ret = false;
2184
807736f6 2185 spin_lock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e 2186 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
2187 * reply from the same orig_node yet
2188 */
807736f6 2189 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
1eda58bf 2190 if (!batadv_compare_eth(tt_roam_node->addr, client))
cc47f66e
AQ
2191 continue;
2192
1eda58bf 2193 if (batadv_has_timed_out(tt_roam_node->first_time,
42d0b044 2194 BATADV_ROAMING_MAX_TIME))
cc47f66e
AQ
2195 continue;
2196
3e34819e 2197 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
cc47f66e
AQ
2198 /* Sorry, you roamed too many times! */
2199 goto unlock;
2200 ret = true;
2201 break;
2202 }
2203
2204 if (!ret) {
2205 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2206 if (!tt_roam_node)
2207 goto unlock;
2208
2209 tt_roam_node->first_time = jiffies;
42d0b044
SE
2210 atomic_set(&tt_roam_node->counter,
2211 BATADV_ROAMING_MAX_COUNT - 1);
cc47f66e
AQ
2212 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2213
807736f6 2214 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
cc47f66e
AQ
2215 ret = true;
2216 }
2217
2218unlock:
807736f6 2219 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
2220 return ret;
2221}
2222
56303d34
SE
2223static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2224 struct batadv_orig_node *orig_node)
cc47f66e 2225{
56303d34 2226 struct batadv_neigh_node *neigh_node = NULL;
cc47f66e 2227 struct sk_buff *skb = NULL;
96412690 2228 struct batadv_roam_adv_packet *roam_adv_packet;
cc47f66e 2229 int ret = 1;
56303d34 2230 struct batadv_hard_iface *primary_if;
96412690 2231 size_t len = sizeof(*roam_adv_packet);
cc47f66e
AQ
2232
2233 /* before going on we have to check whether the client has
9cfc7bd6
SE
2234 * already roamed to us too many times
2235 */
a513088d 2236 if (!batadv_tt_check_roam_count(bat_priv, client))
cc47f66e
AQ
2237 goto out;
2238
5b246574 2239 skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
cc47f66e
AQ
2240 if (!skb)
2241 goto out;
2242
5b246574 2243 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
cc47f66e 2244
96412690 2245 roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
cc47f66e 2246
acd34afa 2247 roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
7e071c79 2248 roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
42d0b044 2249 roam_adv_packet->header.ttl = BATADV_TTL;
162d549c 2250 roam_adv_packet->reserved = 0;
e5d89254 2251 primary_if = batadv_primary_if_get_selected(bat_priv);
cc47f66e
AQ
2252 if (!primary_if)
2253 goto out;
2254 memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
e5d89254 2255 batadv_hardif_free_ref(primary_if);
cc47f66e
AQ
2256 memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2257 memcpy(roam_adv_packet->client, client, ETH_ALEN);
2258
7d211efc 2259 neigh_node = batadv_orig_node_get_router(orig_node);
cc47f66e
AQ
2260 if (!neigh_node)
2261 goto out;
2262
39c75a51 2263 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
2264 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
2265 orig_node->orig, client, neigh_node->addr);
cc47f66e 2266
d69909d2 2267 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
f8214865 2268
9455e34c 2269 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
cc47f66e
AQ
2270 ret = 0;
2271
2272out:
2273 if (neigh_node)
7d211efc 2274 batadv_neigh_node_free_ref(neigh_node);
cc47f66e
AQ
2275 if (ret)
2276 kfree_skb(skb);
2277 return;
a73105b8
AQ
2278}
2279
a513088d 2280static void batadv_tt_purge(struct work_struct *work)
a73105b8 2281{
56303d34 2282 struct delayed_work *delayed_work;
807736f6 2283 struct batadv_priv_tt *priv_tt;
56303d34
SE
2284 struct batadv_priv *bat_priv;
2285
2286 delayed_work = container_of(work, struct delayed_work, work);
807736f6
SE
2287 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2288 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
a73105b8 2289
a513088d 2290 batadv_tt_local_purge(bat_priv);
30cfd02b 2291 batadv_tt_global_purge(bat_priv);
a513088d
SE
2292 batadv_tt_req_purge(bat_priv);
2293 batadv_tt_roam_purge(bat_priv);
a73105b8 2294
a513088d 2295 batadv_tt_start_timer(bat_priv);
a73105b8 2296}
cc47f66e 2297
56303d34 2298void batadv_tt_free(struct batadv_priv *bat_priv)
cc47f66e 2299{
807736f6 2300 cancel_delayed_work_sync(&bat_priv->tt.work);
cc47f66e 2301
a513088d
SE
2302 batadv_tt_local_table_free(bat_priv);
2303 batadv_tt_global_table_free(bat_priv);
2304 batadv_tt_req_list_free(bat_priv);
2305 batadv_tt_changes_list_free(bat_priv);
2306 batadv_tt_roam_list_free(bat_priv);
cc47f66e 2307
807736f6 2308 kfree(bat_priv->tt.last_changeset);
cc47f66e 2309}
058d0e26 2310
697f2531 2311/* This function will enable or disable the specified flags for all the entries
9cfc7bd6
SE
2312 * in the given hash table and returns the number of modified entries
2313 */
5bf74e9c
SE
2314static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2315 uint16_t flags, bool enable)
058d0e26 2316{
c90681b8 2317 uint32_t i;
697f2531 2318 uint16_t changed_num = 0;
058d0e26
AQ
2319 struct hlist_head *head;
2320 struct hlist_node *node;
56303d34 2321 struct batadv_tt_common_entry *tt_common_entry;
058d0e26
AQ
2322
2323 if (!hash)
697f2531 2324 goto out;
058d0e26
AQ
2325
2326 for (i = 0; i < hash->size; i++) {
2327 head = &hash->table[i];
2328
2329 rcu_read_lock();
48100bac 2330 hlist_for_each_entry_rcu(tt_common_entry, node,
058d0e26 2331 head, hash_entry) {
697f2531
AQ
2332 if (enable) {
2333 if ((tt_common_entry->flags & flags) == flags)
2334 continue;
2335 tt_common_entry->flags |= flags;
2336 } else {
2337 if (!(tt_common_entry->flags & flags))
2338 continue;
2339 tt_common_entry->flags &= ~flags;
2340 }
2341 changed_num++;
058d0e26
AQ
2342 }
2343 rcu_read_unlock();
2344 }
697f2531
AQ
2345out:
2346 return changed_num;
058d0e26
AQ
2347}
2348
acd34afa 2349/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
56303d34 2350static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
058d0e26 2351{
807736f6 2352 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34
SE
2353 struct batadv_tt_common_entry *tt_common;
2354 struct batadv_tt_local_entry *tt_local;
058d0e26
AQ
2355 struct hlist_node *node, *node_tmp;
2356 struct hlist_head *head;
2357 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 2358 uint32_t i;
058d0e26
AQ
2359
2360 if (!hash)
2361 return;
2362
2363 for (i = 0; i < hash->size; i++) {
2364 head = &hash->table[i];
2365 list_lock = &hash->list_locks[i];
2366
2367 spin_lock_bh(list_lock);
acd34afa
SE
2368 hlist_for_each_entry_safe(tt_common, node, node_tmp, head,
2369 hash_entry) {
2370 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
058d0e26
AQ
2371 continue;
2372
39c75a51 2373 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 2374 "Deleting local tt entry (%pM): pending\n",
acd34afa 2375 tt_common->addr);
058d0e26 2376
807736f6 2377 atomic_dec(&bat_priv->tt.local_entry_num);
058d0e26 2378 hlist_del_rcu(node);
56303d34
SE
2379 tt_local = container_of(tt_common,
2380 struct batadv_tt_local_entry,
2381 common);
2382 batadv_tt_local_entry_free_ref(tt_local);
058d0e26
AQ
2383 }
2384 spin_unlock_bh(list_lock);
2385 }
2386
2387}
2388
56303d34 2389static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
a513088d
SE
2390 unsigned char **packet_buff,
2391 int *packet_buff_len, int packet_min_len)
058d0e26 2392{
be9aa4c1
ML
2393 uint16_t changed_num = 0;
2394
807736f6 2395 if (atomic_read(&bat_priv->tt.local_changes) < 1)
be9aa4c1
ML
2396 return -ENOENT;
2397
807736f6 2398 changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
acd34afa 2399 BATADV_TT_CLIENT_NEW, false);
be9aa4c1
ML
2400
2401 /* all reset entries have to be counted as local entries */
807736f6 2402 atomic_add(changed_num, &bat_priv->tt.local_entry_num);
a513088d 2403 batadv_tt_local_purge_pending_clients(bat_priv);
807736f6 2404 bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
058d0e26
AQ
2405
2406 /* Increment the TTVN only once per OGM interval */
807736f6 2407 atomic_inc(&bat_priv->tt.vn);
39c75a51 2408 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 2409 "Local changes committed, updating to ttvn %u\n",
807736f6 2410 (uint8_t)atomic_read(&bat_priv->tt.vn));
be9aa4c1
ML
2411
2412 /* reset the sending counter */
807736f6 2413 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
be9aa4c1 2414
a513088d
SE
2415 return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2416 packet_buff_len, packet_min_len);
be9aa4c1
ML
2417}
2418
2419/* when calling this function (hard_iface == primary_if) has to be true */
56303d34 2420int batadv_tt_append_diff(struct batadv_priv *bat_priv,
be9aa4c1
ML
2421 unsigned char **packet_buff, int *packet_buff_len,
2422 int packet_min_len)
2423{
2424 int tt_num_changes;
2425
2426 /* if at least one change happened */
a513088d
SE
2427 tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2428 packet_buff_len,
2429 packet_min_len);
be9aa4c1
ML
2430
2431 /* if the changes have been sent often enough */
2432 if ((tt_num_changes < 0) &&
807736f6 2433 (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
a513088d
SE
2434 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2435 packet_min_len, packet_min_len);
be9aa4c1
ML
2436 tt_num_changes = 0;
2437 }
2438
2439 return tt_num_changes;
058d0e26 2440}
59b699cd 2441
56303d34 2442bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
08c36d3e 2443 uint8_t *dst)
59b699cd 2444{
56303d34
SE
2445 struct batadv_tt_local_entry *tt_local_entry = NULL;
2446 struct batadv_tt_global_entry *tt_global_entry = NULL;
5870adc6 2447 bool ret = false;
59b699cd
AQ
2448
2449 if (!atomic_read(&bat_priv->ap_isolation))
5870adc6 2450 goto out;
59b699cd 2451
a513088d 2452 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
59b699cd
AQ
2453 if (!tt_local_entry)
2454 goto out;
2455
a513088d 2456 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
59b699cd
AQ
2457 if (!tt_global_entry)
2458 goto out;
2459
1f129fef 2460 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
59b699cd
AQ
2461 goto out;
2462
5870adc6 2463 ret = true;
59b699cd
AQ
2464
2465out:
2466 if (tt_global_entry)
a513088d 2467 batadv_tt_global_entry_free_ref(tt_global_entry);
59b699cd 2468 if (tt_local_entry)
a513088d 2469 batadv_tt_local_entry_free_ref(tt_local_entry);
59b699cd
AQ
2470 return ret;
2471}
a943cac1 2472
56303d34
SE
2473void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2474 struct batadv_orig_node *orig_node,
08c36d3e
SE
2475 const unsigned char *tt_buff, uint8_t tt_num_changes,
2476 uint8_t ttvn, uint16_t tt_crc)
a943cac1
ML
2477{
2478 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2479 bool full_table = true;
96412690 2480 struct batadv_tt_change *tt_change;
a943cac1 2481
20ff9d59 2482 /* don't care about a backbone gateways updates. */
08adf151 2483 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
20ff9d59
SW
2484 return;
2485
17071578 2486 /* orig table not initialised AND first diff is in the OGM OR the ttvn
9cfc7bd6
SE
2487 * increased by one -> we can apply the attached changes
2488 */
17071578
AQ
2489 if ((!orig_node->tt_initialised && ttvn == 1) ||
2490 ttvn - orig_ttvn == 1) {
a943cac1 2491 /* the OGM could not contain the changes due to their size or
42d0b044
SE
2492 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2493 * times.
9cfc7bd6
SE
2494 * In this case send a tt request
2495 */
a943cac1
ML
2496 if (!tt_num_changes) {
2497 full_table = false;
2498 goto request_table;
2499 }
2500
96412690 2501 tt_change = (struct batadv_tt_change *)tt_buff;
a513088d 2502 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
96412690 2503 ttvn, tt_change);
a943cac1
ML
2504
2505 /* Even if we received the precomputed crc with the OGM, we
2506 * prefer to recompute it to spot any possible inconsistency
9cfc7bd6
SE
2507 * in the global table
2508 */
a513088d 2509 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
a943cac1
ML
2510
2511 /* The ttvn alone is not enough to guarantee consistency
2512 * because a single value could represent different states
2513 * (due to the wrap around). Thus a node has to check whether
2514 * the resulting table (after applying the changes) is still
2515 * consistent or not. E.g. a node could disconnect while its
2516 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2517 * checking the CRC value is mandatory to detect the
9cfc7bd6
SE
2518 * inconsistency
2519 */
a943cac1
ML
2520 if (orig_node->tt_crc != tt_crc)
2521 goto request_table;
a943cac1
ML
2522 } else {
2523 /* if we missed more than one change or our tables are not
9cfc7bd6
SE
2524 * in sync anymore -> request fresh tt data
2525 */
17071578
AQ
2526 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2527 orig_node->tt_crc != tt_crc) {
a943cac1 2528request_table:
39c75a51 2529 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf
SE
2530 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %u last_crc: %u num_changes: %u)\n",
2531 orig_node->orig, ttvn, orig_ttvn, tt_crc,
2532 orig_node->tt_crc, tt_num_changes);
a513088d
SE
2533 batadv_send_tt_request(bat_priv, orig_node, ttvn,
2534 tt_crc, full_table);
a943cac1
ML
2535 return;
2536 }
2537 }
2538}
3275e7cc
AQ
2539
2540/* returns true whether we know that the client has moved from its old
2541 * originator to another one. This entry is kept is still kept for consistency
2542 * purposes
2543 */
56303d34 2544bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
08c36d3e 2545 uint8_t *addr)
3275e7cc 2546{
56303d34 2547 struct batadv_tt_global_entry *tt_global_entry;
3275e7cc
AQ
2548 bool ret = false;
2549
a513088d 2550 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
3275e7cc
AQ
2551 if (!tt_global_entry)
2552 goto out;
2553
9f9ff08d 2554 ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
a513088d 2555 batadv_tt_global_entry_free_ref(tt_global_entry);
3275e7cc
AQ
2556out:
2557 return ret;
2558}
30cfd02b 2559
7c1fd91d
AQ
2560/**
2561 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2562 * @bat_priv: the bat priv with all the soft interface information
2563 * @addr: the MAC address of the local client to query
2564 *
2565 * Returns true if the local client is known to be roaming (it is not served by
2566 * this node anymore) or not. If yes, the client is still present in the table
2567 * to keep the latter consistent with the node TTVN
2568 */
2569bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2570 uint8_t *addr)
2571{
2572 struct batadv_tt_local_entry *tt_local_entry;
2573 bool ret = false;
2574
2575 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2576 if (!tt_local_entry)
2577 goto out;
2578
2579 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2580 batadv_tt_local_entry_free_ref(tt_local_entry);
2581out:
2582 return ret;
2583
2584}
2585
30cfd02b
AQ
2586bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2587 struct batadv_orig_node *orig_node,
2588 const unsigned char *addr)
2589{
2590 bool ret = false;
2591
1f36aebc
AQ
2592 /* if the originator is a backbone node (meaning it belongs to the same
2593 * LAN of this node) the temporary client must not be added because to
2594 * reach such destination the node must use the LAN instead of the mesh
2595 */
2596 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2597 goto out;
2598
30cfd02b
AQ
2599 if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2600 BATADV_TT_CLIENT_TEMP,
2601 atomic_read(&orig_node->last_ttvn)))
2602 goto out;
2603
2604 batadv_dbg(BATADV_DBG_TT, bat_priv,
2605 "Added temporary global client (addr: %pM orig: %pM)\n",
2606 addr, orig_node->orig);
2607 ret = true;
2608out:
2609 return ret;
2610}