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