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