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