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