net: phy: fix semicolon.cocci warnings
[deliverable/linux.git] / net / batman-adv / translation-table.c
CommitLineData
9f6446c7 1/* Copyright (C) 2007-2015 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
ebf38fb7 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
16 */
17
c6c8fea2 18#include "translation-table.h"
1e2c2a4f
SE
19#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/bug.h>
23#include <linux/byteorder/generic.h>
24#include <linux/compiler.h>
25#include <linux/crc32c.h>
26#include <linux/errno.h>
27#include <linux/etherdevice.h>
28#include <linux/fs.h>
29#include <linux/if_ether.h>
30#include <linux/jhash.h>
31#include <linux/jiffies.h>
32#include <linux/kernel.h>
33#include <linux/list.h>
34#include <linux/lockdep.h>
35#include <linux/netdevice.h>
36#include <linux/rculist.h>
37#include <linux/rcupdate.h>
38#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/spinlock.h>
41#include <linux/stddef.h>
42#include <linux/string.h>
43#include <linux/workqueue.h>
44#include <net/net_namespace.h>
45
46#include "bridge_loop_avoidance.h"
32ae9b22 47#include "hard-interface.h"
c6c8fea2 48#include "hash.h"
c5caf4ef 49#include "multicast.h"
1e2c2a4f
SE
50#include "originator.h"
51#include "packet.h"
52#include "soft-interface.h"
a73105b8 53
dec05074
AQ
54/* hash class keys */
55static struct lock_class_key batadv_tt_local_hash_lock_class_key;
56static struct lock_class_key batadv_tt_global_hash_lock_class_key;
57
56303d34 58static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
c018ad3d 59 unsigned short vid,
56303d34 60 struct batadv_orig_node *orig_node);
a513088d
SE
61static void batadv_tt_purge(struct work_struct *work);
62static void
56303d34 63batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
30cfd02b
AQ
64static void batadv_tt_global_del(struct batadv_priv *bat_priv,
65 struct batadv_orig_node *orig_node,
66 const unsigned char *addr,
c018ad3d
AQ
67 unsigned short vid, const char *message,
68 bool roaming);
c6c8fea2 69
7aadf889 70/* returns 1 if they are the same mac addr */
a513088d 71static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
7aadf889 72{
56303d34 73 const void *data1 = container_of(node, struct batadv_tt_common_entry,
747e4221 74 hash_entry);
7aadf889 75
323813ed 76 return batadv_compare_eth(data1, data2);
7aadf889
ML
77}
78
c018ad3d
AQ
79/**
80 * batadv_choose_tt - return the index of the tt entry in the hash table
81 * @data: pointer to the tt_common_entry object to map
82 * @size: the size of the hash table
83 *
84 * Returns the hash index where the object represented by 'data' should be
85 * stored at.
86 */
87static inline uint32_t batadv_choose_tt(const void *data, uint32_t size)
88{
89 struct batadv_tt_common_entry *tt;
90 uint32_t hash = 0;
91
92 tt = (struct batadv_tt_common_entry *)data;
36fd61cb
SE
93 hash = jhash(&tt->addr, ETH_ALEN, hash);
94 hash = jhash(&tt->vid, sizeof(tt->vid), hash);
c018ad3d
AQ
95
96 return hash % size;
97}
98
99/**
100 * batadv_tt_hash_find - look for a client in the given hash table
101 * @hash: the hash table to search
102 * @addr: the mac address of the client to look for
103 * @vid: VLAN identifier
104 *
105 * Returns a pointer to the tt_common struct belonging to the searched client if
106 * found, NULL otherwise.
107 */
56303d34 108static struct batadv_tt_common_entry *
c018ad3d
AQ
109batadv_tt_hash_find(struct batadv_hashtable *hash, const uint8_t *addr,
110 unsigned short vid)
7aadf889 111{
7aadf889 112 struct hlist_head *head;
c018ad3d 113 struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
c90681b8 114 uint32_t index;
7aadf889
ML
115
116 if (!hash)
117 return NULL;
118
8fdd0153 119 ether_addr_copy(to_search.addr, addr);
c018ad3d
AQ
120 to_search.vid = vid;
121
122 index = batadv_choose_tt(&to_search, hash->size);
7aadf889
ML
123 head = &hash->table[index];
124
125 rcu_read_lock();
c018ad3d
AQ
126 hlist_for_each_entry_rcu(tt, head, hash_entry) {
127 if (!batadv_compare_eth(tt, addr))
128 continue;
129
130 if (tt->vid != vid)
7aadf889
ML
131 continue;
132
c018ad3d 133 if (!atomic_inc_not_zero(&tt->refcount))
7683fdc1
AQ
134 continue;
135
c018ad3d 136 tt_tmp = tt;
7aadf889
ML
137 break;
138 }
139 rcu_read_unlock();
140
c018ad3d 141 return tt_tmp;
7aadf889
ML
142}
143
c018ad3d
AQ
144/**
145 * batadv_tt_local_hash_find - search the local table for a given client
146 * @bat_priv: the bat priv with all the soft interface information
147 * @addr: the mac address of the client to look for
148 * @vid: VLAN identifier
149 *
150 * Returns a pointer to the corresponding tt_local_entry struct if the client is
151 * found, NULL otherwise.
152 */
56303d34 153static struct batadv_tt_local_entry *
c018ad3d
AQ
154batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
155 unsigned short vid)
7aadf889 156{
56303d34
SE
157 struct batadv_tt_common_entry *tt_common_entry;
158 struct batadv_tt_local_entry *tt_local_entry = NULL;
7aadf889 159
c018ad3d
AQ
160 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
161 vid);
48100bac
AQ
162 if (tt_common_entry)
163 tt_local_entry = container_of(tt_common_entry,
56303d34
SE
164 struct batadv_tt_local_entry,
165 common);
48100bac
AQ
166 return tt_local_entry;
167}
7aadf889 168
c018ad3d
AQ
169/**
170 * batadv_tt_global_hash_find - search the global table for a given client
171 * @bat_priv: the bat priv with all the soft interface information
172 * @addr: the mac address of the client to look for
173 * @vid: VLAN identifier
174 *
175 * Returns a pointer to the corresponding tt_global_entry struct if the client
176 * is found, NULL otherwise.
177 */
56303d34 178static struct batadv_tt_global_entry *
c018ad3d
AQ
179batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const uint8_t *addr,
180 unsigned short vid)
48100bac 181{
56303d34
SE
182 struct batadv_tt_common_entry *tt_common_entry;
183 struct batadv_tt_global_entry *tt_global_entry = NULL;
7683fdc1 184
c018ad3d
AQ
185 tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
186 vid);
48100bac
AQ
187 if (tt_common_entry)
188 tt_global_entry = container_of(tt_common_entry,
56303d34
SE
189 struct batadv_tt_global_entry,
190 common);
48100bac 191 return tt_global_entry;
7aadf889
ML
192}
193
a513088d 194static void
56303d34 195batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
7683fdc1 196{
48100bac
AQ
197 if (atomic_dec_and_test(&tt_local_entry->common.refcount))
198 kfree_rcu(tt_local_entry, common.rcu);
7683fdc1
AQ
199}
200
21026059
AQ
201/**
202 * batadv_tt_global_entry_free_ref - decrement the refcounter for a
203 * tt_global_entry and possibly free it
204 * @tt_global_entry: the object to free
205 */
a513088d 206static void
56303d34 207batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
7683fdc1 208{
db08e6e5 209 if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
a513088d 210 batadv_tt_global_del_orig_list(tt_global_entry);
21026059 211 kfree_rcu(tt_global_entry, common.rcu);
db08e6e5
SW
212 }
213}
214
1d8ab8d3
LL
215/**
216 * batadv_tt_global_hash_count - count the number of orig entries
217 * @hash: hash table containing the tt entries
218 * @addr: the mac address of the client to count entries for
219 * @vid: VLAN identifier
220 *
221 * Return the number of originators advertising the given address/data
222 * (excluding ourself).
223 */
224int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
225 const uint8_t *addr, unsigned short vid)
226{
227 struct batadv_tt_global_entry *tt_global_entry;
228 int count;
229
230 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
231 if (!tt_global_entry)
232 return 0;
233
234 count = atomic_read(&tt_global_entry->orig_list_count);
235 batadv_tt_global_entry_free_ref(tt_global_entry);
236
237 return count;
238}
239
a513088d 240static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
db08e6e5 241{
56303d34 242 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5 243
56303d34 244 orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
72822225
LL
245
246 /* We are in an rcu callback here, therefore we cannot use
247 * batadv_orig_node_free_ref() and its call_rcu():
248 * An rcu_barrier() wouldn't wait for that to finish
249 */
250 batadv_orig_node_free_ref_now(orig_entry->orig_node);
db08e6e5
SW
251 kfree(orig_entry);
252}
253
7ea7b4a1
AQ
254/**
255 * batadv_tt_local_size_mod - change the size by v of the local table identified
256 * by vid
257 * @bat_priv: the bat priv with all the soft interface information
258 * @vid: the VLAN identifier of the sub-table to change
259 * @v: the amount to sum to the local table size
260 */
261static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
262 unsigned short vid, int v)
263{
264 struct batadv_softif_vlan *vlan;
265
266 vlan = batadv_softif_vlan_get(bat_priv, vid);
267 if (!vlan)
268 return;
269
270 atomic_add(v, &vlan->tt.num_entries);
271
272 batadv_softif_vlan_free_ref(vlan);
273}
274
275/**
276 * batadv_tt_local_size_inc - increase by one the local table size for the given
277 * vid
278 * @bat_priv: the bat priv with all the soft interface information
279 * @vid: the VLAN identifier
280 */
281static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
282 unsigned short vid)
283{
284 batadv_tt_local_size_mod(bat_priv, vid, 1);
285}
286
287/**
288 * batadv_tt_local_size_dec - decrease by one the local table size for the given
289 * vid
290 * @bat_priv: the bat priv with all the soft interface information
291 * @vid: the VLAN identifier
292 */
293static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
294 unsigned short vid)
295{
296 batadv_tt_local_size_mod(bat_priv, vid, -1);
297}
298
299/**
300 * batadv_tt_global_size_mod - change the size by v of the local table
301 * identified by vid
302 * @bat_priv: the bat priv with all the soft interface information
303 * @vid: the VLAN identifier
304 * @v: the amount to sum to the global table size
305 */
306static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
307 unsigned short vid, int v)
308{
309 struct batadv_orig_node_vlan *vlan;
310
311 vlan = batadv_orig_node_vlan_new(orig_node, vid);
312 if (!vlan)
313 return;
314
315 if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
316 spin_lock_bh(&orig_node->vlan_list_lock);
317 list_del_rcu(&vlan->list);
318 spin_unlock_bh(&orig_node->vlan_list_lock);
319 batadv_orig_node_vlan_free_ref(vlan);
320 }
321
322 batadv_orig_node_vlan_free_ref(vlan);
323}
324
325/**
326 * batadv_tt_global_size_inc - increase by one the global table size for the
327 * given vid
328 * @orig_node: the originator which global table size has to be decreased
329 * @vid: the vlan identifier
330 */
331static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
332 unsigned short vid)
333{
334 batadv_tt_global_size_mod(orig_node, vid, 1);
335}
336
337/**
338 * batadv_tt_global_size_dec - decrease by one the global table size for the
339 * given vid
340 * @orig_node: the originator which global table size has to be decreased
341 * @vid: the vlan identifier
342 */
343static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
344 unsigned short vid)
345{
346 batadv_tt_global_size_mod(orig_node, vid, -1);
347}
348
a513088d 349static void
56303d34 350batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
db08e6e5 351{
d657e621
AQ
352 if (!atomic_dec_and_test(&orig_entry->refcount))
353 return;
7ea7b4a1 354
a513088d 355 call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
7683fdc1
AQ
356}
357
3abe4adb
AQ
358/**
359 * batadv_tt_local_event - store a local TT event (ADD/DEL)
360 * @bat_priv: the bat priv with all the soft interface information
361 * @tt_local_entry: the TT entry involved in the event
362 * @event_flags: flags to store in the event structure
363 */
56303d34 364static void batadv_tt_local_event(struct batadv_priv *bat_priv,
3abe4adb
AQ
365 struct batadv_tt_local_entry *tt_local_entry,
366 uint8_t event_flags)
a73105b8 367{
56303d34 368 struct batadv_tt_change_node *tt_change_node, *entry, *safe;
3abe4adb
AQ
369 struct batadv_tt_common_entry *common = &tt_local_entry->common;
370 uint8_t flags = common->flags | event_flags;
3b643de5
AQ
371 bool event_removed = false;
372 bool del_op_requested, del_op_entry;
a73105b8
AQ
373
374 tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
a73105b8
AQ
375 if (!tt_change_node)
376 return;
377
ff66c975 378 tt_change_node->change.flags = flags;
ca663046
AQ
379 memset(tt_change_node->change.reserved, 0,
380 sizeof(tt_change_node->change.reserved));
8fdd0153 381 ether_addr_copy(tt_change_node->change.addr, common->addr);
c018ad3d 382 tt_change_node->change.vid = htons(common->vid);
a73105b8 383
acd34afa 384 del_op_requested = flags & BATADV_TT_CLIENT_DEL;
3b643de5
AQ
385
386 /* check for ADD+DEL or DEL+ADD events */
807736f6
SE
387 spin_lock_bh(&bat_priv->tt.changes_list_lock);
388 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
3b643de5 389 list) {
3abe4adb 390 if (!batadv_compare_eth(entry->change.addr, common->addr))
3b643de5
AQ
391 continue;
392
393 /* DEL+ADD in the same orig interval have no effect and can be
394 * removed to avoid silly behaviour on the receiver side. The
395 * other way around (ADD+DEL) can happen in case of roaming of
396 * a client still in the NEW state. Roaming of NEW clients is
397 * now possible due to automatically recognition of "temporary"
398 * clients
399 */
acd34afa 400 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
3b643de5
AQ
401 if (!del_op_requested && del_op_entry)
402 goto del;
403 if (del_op_requested && !del_op_entry)
404 goto del;
3c4f7ab6
AQ
405
406 /* this is a second add in the same originator interval. It
407 * means that flags have been changed: update them!
408 */
409 if (!del_op_requested && !del_op_entry)
410 entry->change.flags = flags;
411
3b643de5
AQ
412 continue;
413del:
414 list_del(&entry->list);
415 kfree(entry);
155e4e12 416 kfree(tt_change_node);
3b643de5
AQ
417 event_removed = true;
418 goto unlock;
419 }
420
a73105b8 421 /* track the change in the OGMinterval list */
807736f6 422 list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
3b643de5
AQ
423
424unlock:
807736f6 425 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8 426
3b643de5 427 if (event_removed)
807736f6 428 atomic_dec(&bat_priv->tt.local_changes);
3b643de5 429 else
807736f6 430 atomic_inc(&bat_priv->tt.local_changes);
a73105b8
AQ
431}
432
335fbe0f
ML
433/**
434 * batadv_tt_len - compute length in bytes of given number of tt changes
435 * @changes_num: number of tt changes
436 *
437 * Returns computed length in bytes.
438 */
439static int batadv_tt_len(int changes_num)
a73105b8 440{
335fbe0f 441 return changes_num * sizeof(struct batadv_tvlv_tt_change);
a73105b8
AQ
442}
443
298e6e68
AQ
444/**
445 * batadv_tt_entries - compute the number of entries fitting in tt_len bytes
446 * @tt_len: available space
447 *
448 * Returns the number of entries.
449 */
450static uint16_t batadv_tt_entries(uint16_t tt_len)
451{
452 return tt_len / batadv_tt_len(1);
453}
454
a19d3d85
ML
455/**
456 * batadv_tt_local_table_transmit_size - calculates the local translation table
457 * size when transmitted over the air
458 * @bat_priv: the bat priv with all the soft interface information
459 *
460 * Returns local translation table size in bytes.
461 */
462static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
463{
464 uint16_t num_vlan = 0, tt_local_entries = 0;
465 struct batadv_softif_vlan *vlan;
466 int hdr_size;
467
468 rcu_read_lock();
469 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
470 num_vlan++;
471 tt_local_entries += atomic_read(&vlan->tt.num_entries);
472 }
473 rcu_read_unlock();
474
475 /* header size of tvlv encapsulated tt response payload */
476 hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
477 hdr_size += sizeof(struct batadv_tvlv_hdr);
478 hdr_size += sizeof(struct batadv_tvlv_tt_data);
479 hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
480
481 return hdr_size + batadv_tt_len(tt_local_entries);
482}
483
56303d34 484static int batadv_tt_local_init(struct batadv_priv *bat_priv)
c6c8fea2 485{
807736f6 486 if (bat_priv->tt.local_hash)
5346c35e 487 return 0;
c6c8fea2 488
807736f6 489 bat_priv->tt.local_hash = batadv_hash_new(1024);
c6c8fea2 490
807736f6 491 if (!bat_priv->tt.local_hash)
5346c35e 492 return -ENOMEM;
c6c8fea2 493
dec05074
AQ
494 batadv_hash_set_lock_class(bat_priv->tt.local_hash,
495 &batadv_tt_local_hash_lock_class_key);
496
5346c35e 497 return 0;
c6c8fea2
SE
498}
499
068ee6e2
AQ
500static void batadv_tt_global_free(struct batadv_priv *bat_priv,
501 struct batadv_tt_global_entry *tt_global,
502 const char *message)
503{
504 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
505 "Deleting global tt entry %pM (vid: %d): %s\n",
506 tt_global->common.addr,
507 BATADV_PRINT_VID(tt_global->common.vid), message);
068ee6e2
AQ
508
509 batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
c018ad3d 510 batadv_choose_tt, &tt_global->common);
068ee6e2 511 batadv_tt_global_entry_free_ref(tt_global);
068ee6e2
AQ
512}
513
c018ad3d
AQ
514/**
515 * batadv_tt_local_add - add a new client to the local table or update an
516 * existing client
517 * @soft_iface: netdev struct of the mesh interface
518 * @addr: the mac address of the client to add
519 * @vid: VLAN identifier
520 * @ifindex: index of the interface where the client is connected to (useful to
521 * identify wireless clients)
9464d071
AQ
522 * @mark: the value contained in the skb->mark field of the received packet (if
523 * any)
a19d3d85
ML
524 *
525 * Returns true if the client was successfully added, false otherwise.
c018ad3d 526 */
a19d3d85 527bool batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
9464d071 528 unsigned short vid, int ifindex, uint32_t mark)
c6c8fea2 529{
56303d34 530 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
170173bf 531 struct batadv_tt_local_entry *tt_local;
c5caf4ef 532 struct batadv_tt_global_entry *tt_global = NULL;
35df3b29 533 struct batadv_softif_vlan *vlan;
0c69aecc 534 struct net_device *in_dev = NULL;
db08e6e5 535 struct hlist_head *head;
56303d34 536 struct batadv_tt_orig_list_entry *orig_entry;
a19d3d85
ML
537 int hash_added, table_size, packet_size_max;
538 bool ret = false, roamed_back = false;
3c4f7ab6 539 uint8_t remote_flags;
9464d071 540 uint32_t match_mark;
c6c8fea2 541
0c69aecc
AQ
542 if (ifindex != BATADV_NULL_IFINDEX)
543 in_dev = dev_get_by_index(&init_net, ifindex);
544
c018ad3d 545 tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
c5caf4ef
LL
546
547 if (!is_multicast_ether_addr(addr))
548 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
c6c8fea2 549
47c94655
AQ
550 if (tt_local) {
551 tt_local->last_seen = jiffies;
068ee6e2
AQ
552 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
553 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
554 "Re-adding pending client %pM (vid: %d)\n",
555 addr, BATADV_PRINT_VID(vid));
068ee6e2
AQ
556 /* whatever the reason why the PENDING flag was set,
557 * this is a client which was enqueued to be removed in
558 * this orig_interval. Since it popped up again, the
559 * flag can be reset like it was never enqueued
560 */
561 tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
562 goto add_event;
563 }
564
565 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
566 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
567 "Roaming client %pM (vid: %d) came back to its original location\n",
568 addr, BATADV_PRINT_VID(vid));
068ee6e2
AQ
569 /* the ROAM flag is set because this client roamed away
570 * and the node got a roaming_advertisement message. Now
571 * that the client popped up again at its original
572 * location such flag can be unset
573 */
574 tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
575 roamed_back = true;
576 }
577 goto check_roaming;
c6c8fea2
SE
578 }
579
a19d3d85
ML
580 /* Ignore the client if we cannot send it in a full table response. */
581 table_size = batadv_tt_local_table_transmit_size(bat_priv);
582 table_size += batadv_tt_len(1);
583 packet_size_max = atomic_read(&bat_priv->packet_size_max);
584 if (table_size > packet_size_max) {
585 net_ratelimited_function(batadv_info, soft_iface,
586 "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
587 table_size, packet_size_max, addr);
588 goto out;
589 }
590
47c94655
AQ
591 tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
592 if (!tt_local)
7683fdc1 593 goto out;
a73105b8 594
35df3b29
AQ
595 /* increase the refcounter of the related vlan */
596 vlan = batadv_softif_vlan_get(bat_priv, vid);
354136bc
ML
597 if (WARN(!vlan, "adding TT local entry %pM to non-existent VLAN %d",
598 addr, BATADV_PRINT_VID(vid)))
599 goto out;
35df3b29 600
39c75a51 601 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
602 "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
603 addr, BATADV_PRINT_VID(vid),
807736f6 604 (uint8_t)atomic_read(&bat_priv->tt.vn));
c6c8fea2 605
8fdd0153 606 ether_addr_copy(tt_local->common.addr, addr);
8425ec6a
AQ
607 /* The local entry has to be marked as NEW to avoid to send it in
608 * a full table response going out before the next ttvn increment
609 * (consistency check)
610 */
611 tt_local->common.flags = BATADV_TT_CLIENT_NEW;
c018ad3d 612 tt_local->common.vid = vid;
0c69aecc 613 if (batadv_is_wifi_netdev(in_dev))
47c94655
AQ
614 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
615 atomic_set(&tt_local->common.refcount, 2);
616 tt_local->last_seen = jiffies;
617 tt_local->common.added_at = tt_local->last_seen;
c6c8fea2 618
c5caf4ef
LL
619 /* the batman interface mac and multicast addresses should never be
620 * purged
621 */
622 if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
623 is_multicast_ether_addr(addr))
47c94655 624 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
c6c8fea2 625
807736f6 626 hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
c018ad3d 627 batadv_choose_tt, &tt_local->common,
47c94655 628 &tt_local->common.hash_entry);
80b3f58c
SW
629
630 if (unlikely(hash_added != 0)) {
631 /* remove the reference for the hash */
47c94655 632 batadv_tt_local_entry_free_ref(tt_local);
35df3b29 633 batadv_softif_vlan_free_ref(vlan);
80b3f58c
SW
634 goto out;
635 }
636
068ee6e2 637add_event:
3abe4adb 638 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
ff66c975 639
068ee6e2
AQ
640check_roaming:
641 /* Check whether it is a roaming, but don't do anything if the roaming
642 * process has already been handled
643 */
644 if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
db08e6e5 645 /* These node are probably going to update their tt table */
47c94655 646 head = &tt_global->orig_list;
db08e6e5 647 rcu_read_lock();
b67bfe0d 648 hlist_for_each_entry_rcu(orig_entry, head, list) {
47c94655 649 batadv_send_roam_adv(bat_priv, tt_global->common.addr,
c018ad3d 650 tt_global->common.vid,
a513088d 651 orig_entry->orig_node);
db08e6e5
SW
652 }
653 rcu_read_unlock();
068ee6e2
AQ
654 if (roamed_back) {
655 batadv_tt_global_free(bat_priv, tt_global,
656 "Roaming canceled");
657 tt_global = NULL;
658 } else {
659 /* The global entry has to be marked as ROAMING and
660 * has to be kept for consistency purpose
661 */
662 tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
663 tt_global->roam_at = jiffies;
664 }
7683fdc1 665 }
068ee6e2 666
3c4f7ab6
AQ
667 /* store the current remote flags before altering them. This helps
668 * understanding is flags are changing or not
669 */
670 remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
671
672 if (batadv_is_wifi_netdev(in_dev))
673 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
674 else
675 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
a19d3d85 676
9464d071
AQ
677 /* check the mark in the skb: if it's equal to the configured
678 * isolation_mark, it means the packet is coming from an isolated
679 * non-mesh client
680 */
681 match_mark = (mark & bat_priv->isolation_mark_mask);
682 if (bat_priv->isolation_mark_mask &&
683 match_mark == bat_priv->isolation_mark)
684 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
685 else
686 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
687
3c4f7ab6
AQ
688 /* if any "dynamic" flag has been modified, resend an ADD event for this
689 * entry so that all the nodes can get the new flags
690 */
691 if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
692 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
693
694 ret = true;
7683fdc1 695out:
0c69aecc
AQ
696 if (in_dev)
697 dev_put(in_dev);
47c94655
AQ
698 if (tt_local)
699 batadv_tt_local_entry_free_ref(tt_local);
700 if (tt_global)
701 batadv_tt_global_entry_free_ref(tt_global);
a19d3d85 702 return ret;
c6c8fea2
SE
703}
704
7ea7b4a1
AQ
705/**
706 * batadv_tt_prepare_tvlv_global_data - prepare the TVLV TT header to send
707 * within a TT Response directed to another node
708 * @orig_node: originator for which the TT data has to be prepared
709 * @tt_data: uninitialised pointer to the address of the TVLV buffer
710 * @tt_change: uninitialised pointer to the address of the area where the TT
711 * changed can be stored
712 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
713 * function reserves the amount of space needed to send the entire global TT
714 * table. In case of success the value is updated with the real amount of
715 * reserved bytes
716
717 * Allocate the needed amount of memory for the entire TT TVLV and write its
718 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
719 * objects, one per active VLAN served by the originator node.
720 *
721 * Return the size of the allocated buffer or 0 in case of failure.
722 */
723static uint16_t
724batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
725 struct batadv_tvlv_tt_data **tt_data,
726 struct batadv_tvlv_tt_change **tt_change,
727 int32_t *tt_len)
728{
729 uint16_t num_vlan = 0, num_entries = 0, change_offset, tvlv_len;
730 struct batadv_tvlv_tt_vlan_data *tt_vlan;
731 struct batadv_orig_node_vlan *vlan;
732 uint8_t *tt_change_ptr;
733
734 rcu_read_lock();
735 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
736 num_vlan++;
737 num_entries += atomic_read(&vlan->tt.num_entries);
738 }
739
740 change_offset = sizeof(**tt_data);
741 change_offset += num_vlan * sizeof(*tt_vlan);
742
743 /* if tt_len is negative, allocate the space needed by the full table */
744 if (*tt_len < 0)
745 *tt_len = batadv_tt_len(num_entries);
746
747 tvlv_len = *tt_len;
748 tvlv_len += change_offset;
749
750 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
751 if (!*tt_data) {
752 *tt_len = 0;
753 goto out;
754 }
755
756 (*tt_data)->flags = BATADV_NO_FLAGS;
757 (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
758 (*tt_data)->num_vlan = htons(num_vlan);
759
760 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
761 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
762 tt_vlan->vid = htons(vlan->vid);
763 tt_vlan->crc = htonl(vlan->tt.crc);
764
765 tt_vlan++;
766 }
767
768 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
769 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
770
771out:
772 rcu_read_unlock();
773 return tvlv_len;
774}
775
776/**
777 * batadv_tt_prepare_tvlv_local_data - allocate and prepare the TT TVLV for this
778 * node
779 * @bat_priv: the bat priv with all the soft interface information
780 * @tt_data: uninitialised pointer to the address of the TVLV buffer
781 * @tt_change: uninitialised pointer to the address of the area where the TT
782 * changes can be stored
783 * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
784 * function reserves the amount of space needed to send the entire local TT
785 * table. In case of success the value is updated with the real amount of
786 * reserved bytes
787 *
788 * Allocate the needed amount of memory for the entire TT TVLV and write its
789 * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
790 * objects, one per active VLAN.
791 *
792 * Return the size of the allocated buffer or 0 in case of failure.
793 */
794static uint16_t
795batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
796 struct batadv_tvlv_tt_data **tt_data,
797 struct batadv_tvlv_tt_change **tt_change,
798 int32_t *tt_len)
799{
800 struct batadv_tvlv_tt_vlan_data *tt_vlan;
801 struct batadv_softif_vlan *vlan;
802 uint16_t num_vlan = 0, num_entries = 0, tvlv_len;
803 uint8_t *tt_change_ptr;
804 int change_offset;
805
806 rcu_read_lock();
807 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
808 num_vlan++;
809 num_entries += atomic_read(&vlan->tt.num_entries);
810 }
811
812 change_offset = sizeof(**tt_data);
813 change_offset += num_vlan * sizeof(*tt_vlan);
814
815 /* if tt_len is negative, allocate the space needed by the full table */
816 if (*tt_len < 0)
817 *tt_len = batadv_tt_len(num_entries);
818
819 tvlv_len = *tt_len;
820 tvlv_len += change_offset;
821
822 *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
823 if (!*tt_data) {
824 tvlv_len = 0;
825 goto out;
826 }
827
828 (*tt_data)->flags = BATADV_NO_FLAGS;
829 (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
830 (*tt_data)->num_vlan = htons(num_vlan);
831
832 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
833 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
834 tt_vlan->vid = htons(vlan->vid);
835 tt_vlan->crc = htonl(vlan->tt.crc);
836
837 tt_vlan++;
838 }
839
840 tt_change_ptr = (uint8_t *)*tt_data + change_offset;
841 *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
842
843out:
844 rcu_read_unlock();
845 return tvlv_len;
846}
847
e1bf0c14
ML
848/**
849 * batadv_tt_tvlv_container_update - update the translation table tvlv container
850 * after local tt changes have been committed
851 * @bat_priv: the bat priv with all the soft interface information
852 */
853static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
be9aa4c1 854{
e1bf0c14
ML
855 struct batadv_tt_change_node *entry, *safe;
856 struct batadv_tvlv_tt_data *tt_data;
857 struct batadv_tvlv_tt_change *tt_change;
7ea7b4a1 858 int tt_diff_len, tt_change_len = 0;
e1bf0c14 859 int tt_diff_entries_num = 0, tt_diff_entries_count = 0;
7ea7b4a1 860 uint16_t tvlv_len;
be9aa4c1 861
7ea7b4a1
AQ
862 tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
863 tt_diff_len = batadv_tt_len(tt_diff_entries_num);
be9aa4c1
ML
864
865 /* if we have too many changes for one packet don't send any
866 * and wait for the tt table request which will be fragmented
867 */
e1bf0c14
ML
868 if (tt_diff_len > bat_priv->soft_iface->mtu)
869 tt_diff_len = 0;
be9aa4c1 870
7ea7b4a1
AQ
871 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
872 &tt_change, &tt_diff_len);
873 if (!tvlv_len)
e1bf0c14 874 return;
be9aa4c1 875
e1bf0c14 876 tt_data->flags = BATADV_TT_OGM_DIFF;
c6c8fea2 877
e1bf0c14
ML
878 if (tt_diff_len == 0)
879 goto container_register;
be9aa4c1 880
807736f6
SE
881 spin_lock_bh(&bat_priv->tt.changes_list_lock);
882 atomic_set(&bat_priv->tt.local_changes, 0);
c6c8fea2 883
807736f6 884 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
7c64fd98 885 list) {
e1bf0c14
ML
886 if (tt_diff_entries_count < tt_diff_entries_num) {
887 memcpy(tt_change + tt_diff_entries_count,
888 &entry->change,
889 sizeof(struct batadv_tvlv_tt_change));
890 tt_diff_entries_count++;
c6c8fea2 891 }
a73105b8
AQ
892 list_del(&entry->list);
893 kfree(entry);
c6c8fea2 894 }
807736f6 895 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8
AQ
896
897 /* Keep the buffer for possible tt_request */
807736f6
SE
898 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
899 kfree(bat_priv->tt.last_changeset);
900 bat_priv->tt.last_changeset_len = 0;
901 bat_priv->tt.last_changeset = NULL;
e1bf0c14 902 tt_change_len = batadv_tt_len(tt_diff_entries_count);
be9aa4c1 903 /* check whether this new OGM has no changes due to size problems */
e1bf0c14 904 if (tt_diff_entries_count > 0) {
be9aa4c1 905 /* if kmalloc() fails we will reply with the full table
a73105b8
AQ
906 * instead of providing the diff
907 */
e1bf0c14 908 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
807736f6 909 if (bat_priv->tt.last_changeset) {
e1bf0c14
ML
910 memcpy(bat_priv->tt.last_changeset,
911 tt_change, tt_change_len);
912 bat_priv->tt.last_changeset_len = tt_diff_len;
a73105b8
AQ
913 }
914 }
807736f6 915 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
c6c8fea2 916
e1bf0c14
ML
917container_register:
918 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
7ea7b4a1 919 tvlv_len);
e1bf0c14 920 kfree(tt_data);
c6c8fea2
SE
921}
922
08c36d3e 923int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
924{
925 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 926 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 927 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34 928 struct batadv_tt_common_entry *tt_common_entry;
85766a82 929 struct batadv_tt_local_entry *tt_local;
56303d34 930 struct batadv_hard_iface *primary_if;
7ea7b4a1 931 struct batadv_softif_vlan *vlan;
c6c8fea2 932 struct hlist_head *head;
7ea7b4a1 933 unsigned short vid;
c90681b8 934 uint32_t i;
85766a82
AQ
935 int last_seen_secs;
936 int last_seen_msecs;
937 unsigned long last_seen_jiffies;
938 bool no_purge;
939 uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
c6c8fea2 940
30da63a6
ML
941 primary_if = batadv_seq_print_text_primary_if_get(seq);
942 if (!primary_if)
32ae9b22 943 goto out;
c6c8fea2 944
86ceb360 945 seq_printf(seq,
7ea7b4a1
AQ
946 "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
947 net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn));
dd24ddb2 948 seq_printf(seq, " %-13s %s %-8s %-9s (%-10s)\n", "Client", "VID",
7ea7b4a1 949 "Flags", "Last seen", "CRC");
c6c8fea2 950
c6c8fea2
SE
951 for (i = 0; i < hash->size; i++) {
952 head = &hash->table[i];
953
7aadf889 954 rcu_read_lock();
b67bfe0d 955 hlist_for_each_entry_rcu(tt_common_entry,
7aadf889 956 head, hash_entry) {
85766a82
AQ
957 tt_local = container_of(tt_common_entry,
958 struct batadv_tt_local_entry,
959 common);
7ea7b4a1 960 vid = tt_common_entry->vid;
85766a82
AQ
961 last_seen_jiffies = jiffies - tt_local->last_seen;
962 last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
963 last_seen_secs = last_seen_msecs / 1000;
964 last_seen_msecs = last_seen_msecs % 1000;
965
966 no_purge = tt_common_entry->flags & np_flag;
967
7ea7b4a1
AQ
968 vlan = batadv_softif_vlan_get(bat_priv, vid);
969 if (!vlan) {
970 seq_printf(seq, "Cannot retrieve VLAN %d\n",
971 BATADV_PRINT_VID(vid));
972 continue;
973 }
974
975 seq_printf(seq,
dd24ddb2 976 " * %pM %4i [%c%c%c%c%c%c] %3u.%03u (%#.8x)\n",
7c64fd98 977 tt_common_entry->addr,
16052789 978 BATADV_PRINT_VID(tt_common_entry->vid),
a2f2b6cd
SE
979 ((tt_common_entry->flags &
980 BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
85766a82 981 no_purge ? 'P' : '.',
a2f2b6cd
SE
982 ((tt_common_entry->flags &
983 BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
984 ((tt_common_entry->flags &
985 BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
986 ((tt_common_entry->flags &
987 BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
988 ((tt_common_entry->flags &
989 BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
a7966d90 990 no_purge ? 0 : last_seen_secs,
7ea7b4a1
AQ
991 no_purge ? 0 : last_seen_msecs,
992 vlan->tt.crc);
993
994 batadv_softif_vlan_free_ref(vlan);
c6c8fea2 995 }
7aadf889 996 rcu_read_unlock();
c6c8fea2 997 }
32ae9b22
ML
998out:
999 if (primary_if)
e5d89254 1000 batadv_hardif_free_ref(primary_if);
30da63a6 1001 return 0;
c6c8fea2
SE
1002}
1003
56303d34
SE
1004static void
1005batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1006 struct batadv_tt_local_entry *tt_local_entry,
1007 uint16_t flags, const char *message)
c6c8fea2 1008{
3abe4adb 1009 batadv_tt_local_event(bat_priv, tt_local_entry, flags);
a73105b8 1010
015758d0
AQ
1011 /* The local client has to be marked as "pending to be removed" but has
1012 * to be kept in the table in order to send it in a full table
9cfc7bd6
SE
1013 * response issued before the net ttvn increment (consistency check)
1014 */
acd34afa 1015 tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
c566dbbe 1016
39c75a51 1017 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
1018 "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1019 tt_local_entry->common.addr,
1020 BATADV_PRINT_VID(tt_local_entry->common.vid), message);
c6c8fea2
SE
1021}
1022
7f91d06c
AQ
1023/**
1024 * batadv_tt_local_remove - logically remove an entry from the local table
1025 * @bat_priv: the bat priv with all the soft interface information
1026 * @addr: the MAC address of the client to remove
c018ad3d 1027 * @vid: VLAN identifier
7f91d06c
AQ
1028 * @message: message to append to the log on deletion
1029 * @roaming: true if the deletion is due to a roaming event
1030 *
1031 * Returns the flags assigned to the local entry before being deleted
1032 */
1033uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
c018ad3d
AQ
1034 const uint8_t *addr, unsigned short vid,
1035 const char *message, bool roaming)
c6c8fea2 1036{
170173bf 1037 struct batadv_tt_local_entry *tt_local_entry;
7f91d06c 1038 uint16_t flags, curr_flags = BATADV_NO_FLAGS;
35df3b29 1039 struct batadv_softif_vlan *vlan;
ef72706a 1040 void *tt_entry_exists;
c6c8fea2 1041
c018ad3d 1042 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
7683fdc1
AQ
1043 if (!tt_local_entry)
1044 goto out;
1045
7f91d06c
AQ
1046 curr_flags = tt_local_entry->common.flags;
1047
acd34afa 1048 flags = BATADV_TT_CLIENT_DEL;
068ee6e2
AQ
1049 /* if this global entry addition is due to a roaming, the node has to
1050 * mark the local entry as "roamed" in order to correctly reroute
1051 * packets later
1052 */
7c1fd91d 1053 if (roaming) {
acd34afa 1054 flags |= BATADV_TT_CLIENT_ROAM;
7c1fd91d
AQ
1055 /* mark the local client as ROAMed */
1056 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1057 }
42d0b044 1058
068ee6e2
AQ
1059 if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1060 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1061 message);
1062 goto out;
1063 }
1064 /* if this client has been added right now, it is possible to
1065 * immediately purge it
1066 */
3abe4adb 1067 batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
ef72706a
ML
1068
1069 tt_entry_exists = batadv_hash_remove(bat_priv->tt.local_hash,
1070 batadv_compare_tt,
1071 batadv_choose_tt,
1072 &tt_local_entry->common);
1073 if (!tt_entry_exists)
1074 goto out;
1075
1076 /* extra call to free the local tt entry */
068ee6e2 1077 batadv_tt_local_entry_free_ref(tt_local_entry);
7f91d06c 1078
35df3b29
AQ
1079 /* decrease the reference held for this vlan */
1080 vlan = batadv_softif_vlan_get(bat_priv, vid);
354136bc
ML
1081 if (!vlan)
1082 goto out;
1083
35df3b29
AQ
1084 batadv_softif_vlan_free_ref(vlan);
1085 batadv_softif_vlan_free_ref(vlan);
1086
7683fdc1
AQ
1087out:
1088 if (tt_local_entry)
a513088d 1089 batadv_tt_local_entry_free_ref(tt_local_entry);
7f91d06c
AQ
1090
1091 return curr_flags;
c6c8fea2
SE
1092}
1093
a19d3d85
ML
1094/**
1095 * batadv_tt_local_purge_list - purge inactive tt local entries
1096 * @bat_priv: the bat priv with all the soft interface information
1097 * @head: pointer to the list containing the local tt entries
1098 * @timeout: parameter deciding whether a given tt local entry is considered
1099 * inactive or not
1100 */
56303d34 1101static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
a19d3d85
ML
1102 struct hlist_head *head,
1103 int timeout)
c6c8fea2 1104{
56303d34
SE
1105 struct batadv_tt_local_entry *tt_local_entry;
1106 struct batadv_tt_common_entry *tt_common_entry;
b67bfe0d 1107 struct hlist_node *node_tmp;
acd34afa 1108
b67bfe0d 1109 hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
acd34afa
SE
1110 hash_entry) {
1111 tt_local_entry = container_of(tt_common_entry,
56303d34
SE
1112 struct batadv_tt_local_entry,
1113 common);
acd34afa
SE
1114 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1115 continue;
1116
1117 /* entry already marked for deletion */
1118 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1119 continue;
1120
a19d3d85 1121 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
acd34afa
SE
1122 continue;
1123
1124 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1125 BATADV_TT_CLIENT_DEL, "timed out");
1126 }
1127}
1128
a19d3d85
ML
1129/**
1130 * batadv_tt_local_purge - purge inactive tt local entries
1131 * @bat_priv: the bat priv with all the soft interface information
1132 * @timeout: parameter deciding whether a given tt local entry is considered
1133 * inactive or not
1134 */
1135static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1136 int timeout)
acd34afa 1137{
807736f6 1138 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
c6c8fea2 1139 struct hlist_head *head;
7683fdc1 1140 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 1141 uint32_t i;
c6c8fea2 1142
c6c8fea2
SE
1143 for (i = 0; i < hash->size; i++) {
1144 head = &hash->table[i];
7683fdc1 1145 list_lock = &hash->list_locks[i];
c6c8fea2 1146
7683fdc1 1147 spin_lock_bh(list_lock);
a19d3d85 1148 batadv_tt_local_purge_list(bat_priv, head, timeout);
7683fdc1 1149 spin_unlock_bh(list_lock);
c6c8fea2 1150 }
c6c8fea2
SE
1151}
1152
56303d34 1153static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
c6c8fea2 1154{
5bf74e9c 1155 struct batadv_hashtable *hash;
a73105b8 1156 spinlock_t *list_lock; /* protects write access to the hash lists */
56303d34
SE
1157 struct batadv_tt_common_entry *tt_common_entry;
1158 struct batadv_tt_local_entry *tt_local;
35df3b29 1159 struct batadv_softif_vlan *vlan;
b67bfe0d 1160 struct hlist_node *node_tmp;
7683fdc1 1161 struct hlist_head *head;
c90681b8 1162 uint32_t i;
a73105b8 1163
807736f6 1164 if (!bat_priv->tt.local_hash)
c6c8fea2
SE
1165 return;
1166
807736f6 1167 hash = bat_priv->tt.local_hash;
a73105b8
AQ
1168
1169 for (i = 0; i < hash->size; i++) {
1170 head = &hash->table[i];
1171 list_lock = &hash->list_locks[i];
1172
1173 spin_lock_bh(list_lock);
b67bfe0d 1174 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
a73105b8 1175 head, hash_entry) {
b67bfe0d 1176 hlist_del_rcu(&tt_common_entry->hash_entry);
56303d34
SE
1177 tt_local = container_of(tt_common_entry,
1178 struct batadv_tt_local_entry,
1179 common);
35df3b29
AQ
1180
1181 /* decrease the reference held for this vlan */
1182 vlan = batadv_softif_vlan_get(bat_priv,
1183 tt_common_entry->vid);
354136bc
ML
1184 if (vlan) {
1185 batadv_softif_vlan_free_ref(vlan);
1186 batadv_softif_vlan_free_ref(vlan);
1187 }
35df3b29 1188
56303d34 1189 batadv_tt_local_entry_free_ref(tt_local);
a73105b8
AQ
1190 }
1191 spin_unlock_bh(list_lock);
1192 }
1193
1a8eaf07 1194 batadv_hash_destroy(hash);
a73105b8 1195
807736f6 1196 bat_priv->tt.local_hash = NULL;
c6c8fea2
SE
1197}
1198
56303d34 1199static int batadv_tt_global_init(struct batadv_priv *bat_priv)
c6c8fea2 1200{
807736f6 1201 if (bat_priv->tt.global_hash)
5346c35e 1202 return 0;
c6c8fea2 1203
807736f6 1204 bat_priv->tt.global_hash = batadv_hash_new(1024);
c6c8fea2 1205
807736f6 1206 if (!bat_priv->tt.global_hash)
5346c35e 1207 return -ENOMEM;
c6c8fea2 1208
dec05074
AQ
1209 batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1210 &batadv_tt_global_hash_lock_class_key);
1211
5346c35e 1212 return 0;
c6c8fea2
SE
1213}
1214
56303d34 1215static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
c6c8fea2 1216{
56303d34 1217 struct batadv_tt_change_node *entry, *safe;
c6c8fea2 1218
807736f6 1219 spin_lock_bh(&bat_priv->tt.changes_list_lock);
c6c8fea2 1220
807736f6 1221 list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
a73105b8
AQ
1222 list) {
1223 list_del(&entry->list);
1224 kfree(entry);
1225 }
c6c8fea2 1226
807736f6
SE
1227 atomic_set(&bat_priv->tt.local_changes, 0);
1228 spin_unlock_bh(&bat_priv->tt.changes_list_lock);
a73105b8 1229}
c6c8fea2 1230
d657e621
AQ
1231/* retrieves the orig_tt_list_entry belonging to orig_node from the
1232 * batadv_tt_global_entry list
1233 *
1234 * returns it with an increased refcounter, NULL if not found
db08e6e5 1235 */
d657e621
AQ
1236static struct batadv_tt_orig_list_entry *
1237batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1238 const struct batadv_orig_node *orig_node)
db08e6e5 1239{
d657e621 1240 struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
db08e6e5 1241 const struct hlist_head *head;
db08e6e5
SW
1242
1243 rcu_read_lock();
1244 head = &entry->orig_list;
b67bfe0d 1245 hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
d657e621
AQ
1246 if (tmp_orig_entry->orig_node != orig_node)
1247 continue;
1248 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1249 continue;
1250
1251 orig_entry = tmp_orig_entry;
1252 break;
db08e6e5
SW
1253 }
1254 rcu_read_unlock();
d657e621
AQ
1255
1256 return orig_entry;
1257}
1258
1259/* find out if an orig_node is already in the list of a tt_global_entry.
1260 * returns true if found, false otherwise
1261 */
1262static bool
1263batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1264 const struct batadv_orig_node *orig_node)
1265{
1266 struct batadv_tt_orig_list_entry *orig_entry;
1267 bool found = false;
1268
1269 orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1270 if (orig_entry) {
1271 found = true;
1272 batadv_tt_orig_list_entry_free_ref(orig_entry);
1273 }
1274
db08e6e5
SW
1275 return found;
1276}
1277
a513088d 1278static void
d657e621 1279batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
56303d34 1280 struct batadv_orig_node *orig_node, int ttvn)
db08e6e5 1281{
56303d34 1282 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5 1283
d657e621 1284 orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
30cfd02b
AQ
1285 if (orig_entry) {
1286 /* refresh the ttvn: the current value could be a bogus one that
1287 * was added during a "temporary client detection"
1288 */
1289 orig_entry->ttvn = ttvn;
d657e621 1290 goto out;
30cfd02b 1291 }
d657e621 1292
db08e6e5
SW
1293 orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1294 if (!orig_entry)
d657e621 1295 goto out;
db08e6e5
SW
1296
1297 INIT_HLIST_NODE(&orig_entry->list);
1298 atomic_inc(&orig_node->refcount);
7ea7b4a1 1299 batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
db08e6e5
SW
1300 orig_entry->orig_node = orig_node;
1301 orig_entry->ttvn = ttvn;
d657e621 1302 atomic_set(&orig_entry->refcount, 2);
db08e6e5 1303
d657e621 1304 spin_lock_bh(&tt_global->list_lock);
db08e6e5 1305 hlist_add_head_rcu(&orig_entry->list,
d657e621
AQ
1306 &tt_global->orig_list);
1307 spin_unlock_bh(&tt_global->list_lock);
1d8ab8d3
LL
1308 atomic_inc(&tt_global->orig_list_count);
1309
d657e621
AQ
1310out:
1311 if (orig_entry)
1312 batadv_tt_orig_list_entry_free_ref(orig_entry);
db08e6e5
SW
1313}
1314
d4ff40f6
AQ
1315/**
1316 * batadv_tt_global_add - add a new TT global entry or update an existing one
1317 * @bat_priv: the bat priv with all the soft interface information
1318 * @orig_node: the originator announcing the client
1319 * @tt_addr: the mac address of the non-mesh client
c018ad3d 1320 * @vid: VLAN identifier
d4ff40f6
AQ
1321 * @flags: TT flags that have to be set for this non-mesh client
1322 * @ttvn: the tt version number ever announcing this non-mesh client
1323 *
1324 * Add a new TT global entry for the given originator. If the entry already
1325 * exists add a new reference to the given originator (a global entry can have
1326 * references to multiple originators) and adjust the flags attribute to reflect
1327 * the function argument.
1328 * If a TT local entry exists for this non-mesh client remove it.
1329 *
1330 * The caller must hold orig_node refcount.
1e5d49fc
AQ
1331 *
1332 * Return true if the new entry has been added, false otherwise
d4ff40f6 1333 */
1e5d49fc
AQ
1334static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1335 struct batadv_orig_node *orig_node,
c018ad3d
AQ
1336 const unsigned char *tt_addr,
1337 unsigned short vid, uint16_t flags,
1e5d49fc 1338 uint8_t ttvn)
a73105b8 1339{
170173bf
SE
1340 struct batadv_tt_global_entry *tt_global_entry;
1341 struct batadv_tt_local_entry *tt_local_entry;
1e5d49fc 1342 bool ret = false;
80b3f58c 1343 int hash_added;
56303d34 1344 struct batadv_tt_common_entry *common;
7f91d06c 1345 uint16_t local_flags;
c6c8fea2 1346
cfd4f757
AQ
1347 /* ignore global entries from backbone nodes */
1348 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1349 return true;
1350
c018ad3d
AQ
1351 tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1352 tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
068ee6e2
AQ
1353
1354 /* if the node already has a local client for this entry, it has to wait
1355 * for a roaming advertisement instead of manually messing up the global
1356 * table
1357 */
1358 if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1359 !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1360 goto out;
a73105b8
AQ
1361
1362 if (!tt_global_entry) {
d4f44692 1363 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
a73105b8 1364 if (!tt_global_entry)
7683fdc1
AQ
1365 goto out;
1366
c0a55929 1367 common = &tt_global_entry->common;
8fdd0153 1368 ether_addr_copy(common->addr, tt_addr);
c018ad3d 1369 common->vid = vid;
db08e6e5 1370
d4f44692 1371 common->flags = flags;
cc47f66e 1372 tt_global_entry->roam_at = 0;
fdf79320
AQ
1373 /* node must store current time in case of roaming. This is
1374 * needed to purge this entry out on timeout (if nobody claims
1375 * it)
1376 */
1377 if (flags & BATADV_TT_CLIENT_ROAM)
1378 tt_global_entry->roam_at = jiffies;
c0a55929 1379 atomic_set(&common->refcount, 2);
30cfd02b 1380 common->added_at = jiffies;
db08e6e5
SW
1381
1382 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1d8ab8d3 1383 atomic_set(&tt_global_entry->orig_list_count, 0);
db08e6e5 1384 spin_lock_init(&tt_global_entry->list_lock);
7683fdc1 1385
807736f6 1386 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
a513088d 1387 batadv_compare_tt,
c018ad3d 1388 batadv_choose_tt, common,
a513088d 1389 &common->hash_entry);
80b3f58c
SW
1390
1391 if (unlikely(hash_added != 0)) {
1392 /* remove the reference for the hash */
a513088d 1393 batadv_tt_global_entry_free_ref(tt_global_entry);
80b3f58c
SW
1394 goto out_remove;
1395 }
a73105b8 1396 } else {
068ee6e2 1397 common = &tt_global_entry->common;
30cfd02b
AQ
1398 /* If there is already a global entry, we can use this one for
1399 * our processing.
068ee6e2
AQ
1400 * But if we are trying to add a temporary client then here are
1401 * two options at this point:
1402 * 1) the global client is not a temporary client: the global
1403 * client has to be left as it is, temporary information
1404 * should never override any already known client state
1405 * 2) the global client is a temporary client: purge the
1406 * originator list and add the new one orig_entry
30cfd02b 1407 */
068ee6e2
AQ
1408 if (flags & BATADV_TT_CLIENT_TEMP) {
1409 if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1410 goto out;
1411 if (batadv_tt_global_entry_has_orig(tt_global_entry,
1412 orig_node))
1413 goto out_remove;
1414 batadv_tt_global_del_orig_list(tt_global_entry);
1415 goto add_orig_entry;
1416 }
30cfd02b
AQ
1417
1418 /* if the client was temporary added before receiving the first
1419 * OGM announcing it, we have to clear the TEMP flag
1420 */
068ee6e2 1421 common->flags &= ~BATADV_TT_CLIENT_TEMP;
db08e6e5 1422
e9c00136
AQ
1423 /* the change can carry possible "attribute" flags like the
1424 * TT_CLIENT_WIFI, therefore they have to be copied in the
1425 * client entry
1426 */
1427 tt_global_entry->common.flags |= flags;
1428
acd34afa
SE
1429 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1430 * one originator left in the list and we previously received a
db08e6e5
SW
1431 * delete + roaming change for this originator.
1432 *
1433 * We should first delete the old originator before adding the
1434 * new one.
1435 */
068ee6e2 1436 if (common->flags & BATADV_TT_CLIENT_ROAM) {
a513088d 1437 batadv_tt_global_del_orig_list(tt_global_entry);
068ee6e2 1438 common->flags &= ~BATADV_TT_CLIENT_ROAM;
db08e6e5 1439 tt_global_entry->roam_at = 0;
a73105b8
AQ
1440 }
1441 }
068ee6e2 1442add_orig_entry:
30cfd02b 1443 /* add the new orig_entry (if needed) or update it */
d657e621 1444 batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
c6c8fea2 1445
39c75a51 1446 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
1447 "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1448 common->addr, BATADV_PRINT_VID(common->vid),
1449 orig_node->orig);
1e5d49fc 1450 ret = true;
c6c8fea2 1451
80b3f58c 1452out_remove:
c5caf4ef
LL
1453 /* Do not remove multicast addresses from the local hash on
1454 * global additions
1455 */
1456 if (is_multicast_ether_addr(tt_addr))
1457 goto out;
7f91d06c 1458
a73105b8 1459 /* remove address from local hash if present */
c018ad3d 1460 local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
7f91d06c 1461 "global tt received",
c1d07431 1462 flags & BATADV_TT_CLIENT_ROAM);
7f91d06c
AQ
1463 tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1464
068ee6e2
AQ
1465 if (!(flags & BATADV_TT_CLIENT_ROAM))
1466 /* this is a normal global add. Therefore the client is not in a
1467 * roaming state anymore.
1468 */
1469 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1470
7683fdc1
AQ
1471out:
1472 if (tt_global_entry)
a513088d 1473 batadv_tt_global_entry_free_ref(tt_global_entry);
068ee6e2
AQ
1474 if (tt_local_entry)
1475 batadv_tt_local_entry_free_ref(tt_local_entry);
7683fdc1 1476 return ret;
c6c8fea2
SE
1477}
1478
1b371d13
SW
1479/**
1480 * batadv_transtable_best_orig - Get best originator list entry from tt entry
4627456a 1481 * @bat_priv: the bat priv with all the soft interface information
981d8900
SE
1482 * @tt_global_entry: global translation table entry to be analyzed
1483 *
1484 * This functon assumes the caller holds rcu_read_lock().
1485 * Returns best originator list entry or NULL on errors.
1486 */
1487static struct batadv_tt_orig_list_entry *
4627456a
AQ
1488batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1489 struct batadv_tt_global_entry *tt_global_entry)
981d8900 1490{
4627456a
AQ
1491 struct batadv_neigh_node *router, *best_router = NULL;
1492 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
981d8900 1493 struct hlist_head *head;
981d8900 1494 struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
981d8900
SE
1495
1496 head = &tt_global_entry->orig_list;
b67bfe0d 1497 hlist_for_each_entry_rcu(orig_entry, head, list) {
7351a482
SW
1498 router = batadv_orig_router_get(orig_entry->orig_node,
1499 BATADV_IF_DEFAULT);
981d8900
SE
1500 if (!router)
1501 continue;
1502
4627456a 1503 if (best_router &&
89652331
SW
1504 bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1505 best_router, BATADV_IF_DEFAULT) <= 0) {
4627456a
AQ
1506 batadv_neigh_node_free_ref(router);
1507 continue;
981d8900
SE
1508 }
1509
4627456a
AQ
1510 /* release the refcount for the "old" best */
1511 if (best_router)
1512 batadv_neigh_node_free_ref(best_router);
1513
1514 best_entry = orig_entry;
1515 best_router = router;
981d8900
SE
1516 }
1517
4627456a
AQ
1518 if (best_router)
1519 batadv_neigh_node_free_ref(best_router);
1520
981d8900
SE
1521 return best_entry;
1522}
1523
1b371d13
SW
1524/**
1525 * batadv_tt_global_print_entry - print all orig nodes who announce the address
1526 * for this global entry
4627456a 1527 * @bat_priv: the bat priv with all the soft interface information
981d8900
SE
1528 * @tt_global_entry: global translation table entry to be printed
1529 * @seq: debugfs table seq_file struct
1530 *
1531 * This functon assumes the caller holds rcu_read_lock().
db08e6e5 1532 */
a513088d 1533static void
4627456a
AQ
1534batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1535 struct batadv_tt_global_entry *tt_global_entry,
a513088d 1536 struct seq_file *seq)
db08e6e5 1537{
981d8900 1538 struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
56303d34 1539 struct batadv_tt_common_entry *tt_common_entry;
7ea7b4a1
AQ
1540 struct batadv_orig_node_vlan *vlan;
1541 struct hlist_head *head;
db08e6e5 1542 uint8_t last_ttvn;
7ea7b4a1 1543 uint16_t flags;
db08e6e5
SW
1544
1545 tt_common_entry = &tt_global_entry->common;
981d8900
SE
1546 flags = tt_common_entry->flags;
1547
4627456a 1548 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
981d8900 1549 if (best_entry) {
7ea7b4a1
AQ
1550 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1551 tt_common_entry->vid);
1552 if (!vlan) {
1553 seq_printf(seq,
1554 " * Cannot retrieve VLAN %d for originator %pM\n",
1555 BATADV_PRINT_VID(tt_common_entry->vid),
1556 best_entry->orig_node->orig);
1557 goto print_list;
1558 }
1559
981d8900 1560 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
f9d8a537 1561 seq_printf(seq,
dd24ddb2 1562 " %c %pM %4i (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
981d8900 1563 '*', tt_global_entry->common.addr,
16052789 1564 BATADV_PRINT_VID(tt_global_entry->common.vid),
981d8900 1565 best_entry->ttvn, best_entry->orig_node->orig,
7ea7b4a1 1566 last_ttvn, vlan->tt.crc,
a2f2b6cd
SE
1567 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1568 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1569 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1570 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
7ea7b4a1
AQ
1571
1572 batadv_orig_node_vlan_free_ref(vlan);
981d8900 1573 }
db08e6e5 1574
7ea7b4a1 1575print_list:
db08e6e5
SW
1576 head = &tt_global_entry->orig_list;
1577
b67bfe0d 1578 hlist_for_each_entry_rcu(orig_entry, head, list) {
981d8900
SE
1579 if (best_entry == orig_entry)
1580 continue;
1581
7ea7b4a1
AQ
1582 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1583 tt_common_entry->vid);
1584 if (!vlan) {
1585 seq_printf(seq,
1586 " + Cannot retrieve VLAN %d for originator %pM\n",
1587 BATADV_PRINT_VID(tt_common_entry->vid),
1588 orig_entry->orig_node->orig);
1589 continue;
1590 }
1591
db08e6e5 1592 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
16052789 1593 seq_printf(seq,
dd24ddb2 1594 " %c %pM %4d (%3u) via %pM (%3u) (%#.8x) [%c%c%c%c]\n",
981d8900 1595 '+', tt_global_entry->common.addr,
16052789 1596 BATADV_PRINT_VID(tt_global_entry->common.vid),
981d8900 1597 orig_entry->ttvn, orig_entry->orig_node->orig,
7ea7b4a1 1598 last_ttvn, vlan->tt.crc,
a2f2b6cd
SE
1599 ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1600 ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1601 ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1602 ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
7ea7b4a1
AQ
1603
1604 batadv_orig_node_vlan_free_ref(vlan);
db08e6e5
SW
1605 }
1606}
1607
08c36d3e 1608int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
1609{
1610 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 1611 struct batadv_priv *bat_priv = netdev_priv(net_dev);
807736f6 1612 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
56303d34
SE
1613 struct batadv_tt_common_entry *tt_common_entry;
1614 struct batadv_tt_global_entry *tt_global;
1615 struct batadv_hard_iface *primary_if;
c6c8fea2 1616 struct hlist_head *head;
c90681b8 1617 uint32_t i;
c6c8fea2 1618
30da63a6
ML
1619 primary_if = batadv_seq_print_text_primary_if_get(seq);
1620 if (!primary_if)
32ae9b22 1621 goto out;
c6c8fea2 1622
2dafb49d
AQ
1623 seq_printf(seq,
1624 "Globally announced TT entries received via the mesh %s\n",
c6c8fea2 1625 net_dev->name);
16052789
AQ
1626 seq_printf(seq, " %-13s %s %s %-15s %s (%-10s) %s\n",
1627 "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1628 "CRC", "Flags");
c6c8fea2 1629
c6c8fea2
SE
1630 for (i = 0; i < hash->size; i++) {
1631 head = &hash->table[i];
1632
7aadf889 1633 rcu_read_lock();
b67bfe0d 1634 hlist_for_each_entry_rcu(tt_common_entry,
7aadf889 1635 head, hash_entry) {
56303d34
SE
1636 tt_global = container_of(tt_common_entry,
1637 struct batadv_tt_global_entry,
1638 common);
4627456a 1639 batadv_tt_global_print_entry(bat_priv, tt_global, seq);
c6c8fea2 1640 }
7aadf889 1641 rcu_read_unlock();
c6c8fea2 1642 }
32ae9b22
ML
1643out:
1644 if (primary_if)
e5d89254 1645 batadv_hardif_free_ref(primary_if);
30da63a6 1646 return 0;
c6c8fea2
SE
1647}
1648
1d8ab8d3
LL
1649/**
1650 * batadv_tt_global_del_orig_entry - remove and free an orig_entry
1651 * @tt_global_entry: the global entry to remove the orig_entry from
1652 * @orig_entry: the orig entry to remove and free
1653 *
1654 * Remove an orig_entry from its list in the given tt_global_entry and
1655 * free this orig_entry afterwards.
1656 */
1657static void
1658batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1659 struct batadv_tt_orig_list_entry *orig_entry)
1660{
1661 batadv_tt_global_size_dec(orig_entry->orig_node,
1662 tt_global_entry->common.vid);
1663 atomic_dec(&tt_global_entry->orig_list_count);
1664 hlist_del_rcu(&orig_entry->list);
1665 batadv_tt_orig_list_entry_free_ref(orig_entry);
1666}
1667
db08e6e5 1668/* deletes the orig list of a tt_global_entry */
a513088d 1669static void
56303d34 1670batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
c6c8fea2 1671{
db08e6e5 1672 struct hlist_head *head;
b67bfe0d 1673 struct hlist_node *safe;
56303d34 1674 struct batadv_tt_orig_list_entry *orig_entry;
a73105b8 1675
db08e6e5
SW
1676 spin_lock_bh(&tt_global_entry->list_lock);
1677 head = &tt_global_entry->orig_list;
1d8ab8d3
LL
1678 hlist_for_each_entry_safe(orig_entry, safe, head, list)
1679 batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
db08e6e5 1680 spin_unlock_bh(&tt_global_entry->list_lock);
db08e6e5
SW
1681}
1682
1d8ab8d3
LL
1683/**
1684 * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1685 * @bat_priv: the bat priv with all the soft interface information
1686 * @tt_global_entry: the global entry to remove the orig_node from
1687 * @orig_node: the originator announcing the client
1688 * @message: message to append to the log on deletion
1689 *
1690 * Remove the given orig_node and its according orig_entry from the given
1691 * global tt entry.
1692 */
a513088d 1693static void
1d8ab8d3
LL
1694batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1695 struct batadv_tt_global_entry *tt_global_entry,
1696 struct batadv_orig_node *orig_node,
1697 const char *message)
db08e6e5
SW
1698{
1699 struct hlist_head *head;
b67bfe0d 1700 struct hlist_node *safe;
56303d34 1701 struct batadv_tt_orig_list_entry *orig_entry;
16052789 1702 unsigned short vid;
db08e6e5
SW
1703
1704 spin_lock_bh(&tt_global_entry->list_lock);
1705 head = &tt_global_entry->orig_list;
b67bfe0d 1706 hlist_for_each_entry_safe(orig_entry, safe, head, list) {
db08e6e5 1707 if (orig_entry->orig_node == orig_node) {
16052789 1708 vid = tt_global_entry->common.vid;
39c75a51 1709 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789 1710 "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1eda58bf 1711 orig_node->orig,
16052789
AQ
1712 tt_global_entry->common.addr,
1713 BATADV_PRINT_VID(vid), message);
1d8ab8d3
LL
1714 batadv_tt_global_del_orig_entry(tt_global_entry,
1715 orig_entry);
db08e6e5
SW
1716 }
1717 }
1718 spin_unlock_bh(&tt_global_entry->list_lock);
1719}
1720
db08e6e5 1721/* If the client is to be deleted, we check if it is the last origantor entry
acd34afa
SE
1722 * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1723 * timer, otherwise we simply remove the originator scheduled for deletion.
db08e6e5 1724 */
a513088d 1725static void
56303d34
SE
1726batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1727 struct batadv_tt_global_entry *tt_global_entry,
1728 struct batadv_orig_node *orig_node,
1729 const char *message)
db08e6e5
SW
1730{
1731 bool last_entry = true;
1732 struct hlist_head *head;
56303d34 1733 struct batadv_tt_orig_list_entry *orig_entry;
db08e6e5
SW
1734
1735 /* no local entry exists, case 1:
1736 * Check if this is the last one or if other entries exist.
1737 */
1738
1739 rcu_read_lock();
1740 head = &tt_global_entry->orig_list;
b67bfe0d 1741 hlist_for_each_entry_rcu(orig_entry, head, list) {
db08e6e5
SW
1742 if (orig_entry->orig_node != orig_node) {
1743 last_entry = false;
1744 break;
1745 }
1746 }
1747 rcu_read_unlock();
1748
1749 if (last_entry) {
1750 /* its the last one, mark for roaming. */
acd34afa 1751 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
db08e6e5
SW
1752 tt_global_entry->roam_at = jiffies;
1753 } else
1754 /* there is another entry, we can simply delete this
1755 * one and can still use the other one.
1756 */
1d8ab8d3
LL
1757 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1758 orig_node, message);
db08e6e5
SW
1759}
1760
c018ad3d
AQ
1761/**
1762 * batadv_tt_global_del - remove a client from the global table
1763 * @bat_priv: the bat priv with all the soft interface information
1764 * @orig_node: an originator serving this client
1765 * @addr: the mac address of the client
1766 * @vid: VLAN identifier
1767 * @message: a message explaining the reason for deleting the client to print
1768 * for debugging purpose
1769 * @roaming: true if the deletion has been triggered by a roaming event
1770 */
56303d34
SE
1771static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1772 struct batadv_orig_node *orig_node,
c018ad3d 1773 const unsigned char *addr, unsigned short vid,
a513088d 1774 const char *message, bool roaming)
a73105b8 1775{
170173bf 1776 struct batadv_tt_global_entry *tt_global_entry;
56303d34 1777 struct batadv_tt_local_entry *local_entry = NULL;
a73105b8 1778
c018ad3d 1779 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
db08e6e5 1780 if (!tt_global_entry)
7683fdc1 1781 goto out;
a73105b8 1782
db08e6e5 1783 if (!roaming) {
1d8ab8d3
LL
1784 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1785 orig_node, message);
db08e6e5
SW
1786
1787 if (hlist_empty(&tt_global_entry->orig_list))
be73b488
AQ
1788 batadv_tt_global_free(bat_priv, tt_global_entry,
1789 message);
db08e6e5
SW
1790
1791 goto out;
1792 }
92f90f56
SE
1793
1794 /* if we are deleting a global entry due to a roam
1795 * event, there are two possibilities:
db08e6e5
SW
1796 * 1) the client roamed from node A to node B => if there
1797 * is only one originator left for this client, we mark
acd34afa 1798 * it with BATADV_TT_CLIENT_ROAM, we start a timer and we
92f90f56
SE
1799 * wait for node B to claim it. In case of timeout
1800 * the entry is purged.
db08e6e5
SW
1801 *
1802 * If there are other originators left, we directly delete
1803 * the originator.
92f90f56 1804 * 2) the client roamed to us => we can directly delete
9cfc7bd6
SE
1805 * the global entry, since it is useless now.
1806 */
a513088d 1807 local_entry = batadv_tt_local_hash_find(bat_priv,
c018ad3d
AQ
1808 tt_global_entry->common.addr,
1809 vid);
a513088d 1810 if (local_entry) {
db08e6e5 1811 /* local entry exists, case 2: client roamed to us. */
a513088d 1812 batadv_tt_global_del_orig_list(tt_global_entry);
be73b488 1813 batadv_tt_global_free(bat_priv, tt_global_entry, message);
db08e6e5
SW
1814 } else
1815 /* no local entry exists, case 1: check for roaming */
a513088d
SE
1816 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1817 orig_node, message);
92f90f56 1818
cc47f66e 1819out:
7683fdc1 1820 if (tt_global_entry)
a513088d
SE
1821 batadv_tt_global_entry_free_ref(tt_global_entry);
1822 if (local_entry)
1823 batadv_tt_local_entry_free_ref(local_entry);
a73105b8
AQ
1824}
1825
95fb130d
AQ
1826/**
1827 * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1828 * given originator matching the provided vid
1829 * @bat_priv: the bat priv with all the soft interface information
1830 * @orig_node: the originator owning the entries to remove
1831 * @match_vid: the VLAN identifier to match. If negative all the entries will be
1832 * removed
1833 * @message: debug message to print as "reason"
1834 */
56303d34
SE
1835void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1836 struct batadv_orig_node *orig_node,
95fb130d 1837 int32_t match_vid,
56303d34 1838 const char *message)
c6c8fea2 1839{
56303d34
SE
1840 struct batadv_tt_global_entry *tt_global;
1841 struct batadv_tt_common_entry *tt_common_entry;
c90681b8 1842 uint32_t i;
807736f6 1843 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
b67bfe0d 1844 struct hlist_node *safe;
a73105b8 1845 struct hlist_head *head;
7683fdc1 1846 spinlock_t *list_lock; /* protects write access to the hash lists */
16052789 1847 unsigned short vid;
c6c8fea2 1848
6e801494
SW
1849 if (!hash)
1850 return;
1851
a73105b8
AQ
1852 for (i = 0; i < hash->size; i++) {
1853 head = &hash->table[i];
7683fdc1 1854 list_lock = &hash->list_locks[i];
c6c8fea2 1855
7683fdc1 1856 spin_lock_bh(list_lock);
b67bfe0d 1857 hlist_for_each_entry_safe(tt_common_entry, safe,
7c64fd98 1858 head, hash_entry) {
95fb130d
AQ
1859 /* remove only matching entries */
1860 if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1861 continue;
1862
56303d34
SE
1863 tt_global = container_of(tt_common_entry,
1864 struct batadv_tt_global_entry,
1865 common);
db08e6e5 1866
1d8ab8d3
LL
1867 batadv_tt_global_del_orig_node(bat_priv, tt_global,
1868 orig_node, message);
db08e6e5 1869
56303d34 1870 if (hlist_empty(&tt_global->orig_list)) {
16052789 1871 vid = tt_global->common.vid;
39c75a51 1872 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
1873 "Deleting global tt entry %pM (vid: %d): %s\n",
1874 tt_global->common.addr,
1875 BATADV_PRINT_VID(vid), message);
b67bfe0d 1876 hlist_del_rcu(&tt_common_entry->hash_entry);
56303d34 1877 batadv_tt_global_entry_free_ref(tt_global);
7683fdc1 1878 }
a73105b8 1879 }
7683fdc1 1880 spin_unlock_bh(list_lock);
c6c8fea2 1881 }
e17931d1 1882 orig_node->capa_initialized &= ~BATADV_ORIG_CAPA_HAS_TT;
c6c8fea2
SE
1883}
1884
30cfd02b
AQ
1885static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1886 char **msg)
cc47f66e 1887{
30cfd02b
AQ
1888 bool purge = false;
1889 unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1890 unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
42d0b044 1891
30cfd02b
AQ
1892 if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1893 batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1894 purge = true;
1895 *msg = "Roaming timeout\n";
1896 }
42d0b044 1897
30cfd02b
AQ
1898 if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1899 batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1900 purge = true;
1901 *msg = "Temporary client timeout\n";
42d0b044 1902 }
30cfd02b
AQ
1903
1904 return purge;
42d0b044
SE
1905}
1906
30cfd02b 1907static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
42d0b044 1908{
807736f6 1909 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
cc47f66e 1910 struct hlist_head *head;
b67bfe0d 1911 struct hlist_node *node_tmp;
7683fdc1 1912 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 1913 uint32_t i;
30cfd02b
AQ
1914 char *msg = NULL;
1915 struct batadv_tt_common_entry *tt_common;
1916 struct batadv_tt_global_entry *tt_global;
cc47f66e 1917
cc47f66e
AQ
1918 for (i = 0; i < hash->size; i++) {
1919 head = &hash->table[i];
7683fdc1 1920 list_lock = &hash->list_locks[i];
cc47f66e 1921
7683fdc1 1922 spin_lock_bh(list_lock);
b67bfe0d 1923 hlist_for_each_entry_safe(tt_common, node_tmp, head,
30cfd02b
AQ
1924 hash_entry) {
1925 tt_global = container_of(tt_common,
1926 struct batadv_tt_global_entry,
1927 common);
1928
1929 if (!batadv_tt_global_to_purge(tt_global, &msg))
1930 continue;
1931
1932 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
1933 "Deleting global tt entry %pM (vid: %d): %s\n",
1934 tt_global->common.addr,
1935 BATADV_PRINT_VID(tt_global->common.vid),
1936 msg);
30cfd02b 1937
b67bfe0d 1938 hlist_del_rcu(&tt_common->hash_entry);
30cfd02b
AQ
1939
1940 batadv_tt_global_entry_free_ref(tt_global);
1941 }
7683fdc1 1942 spin_unlock_bh(list_lock);
cc47f66e 1943 }
cc47f66e
AQ
1944}
1945
56303d34 1946static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
c6c8fea2 1947{
5bf74e9c 1948 struct batadv_hashtable *hash;
7683fdc1 1949 spinlock_t *list_lock; /* protects write access to the hash lists */
56303d34
SE
1950 struct batadv_tt_common_entry *tt_common_entry;
1951 struct batadv_tt_global_entry *tt_global;
b67bfe0d 1952 struct hlist_node *node_tmp;
7683fdc1 1953 struct hlist_head *head;
c90681b8 1954 uint32_t i;
7683fdc1 1955
807736f6 1956 if (!bat_priv->tt.global_hash)
c6c8fea2
SE
1957 return;
1958
807736f6 1959 hash = bat_priv->tt.global_hash;
7683fdc1
AQ
1960
1961 for (i = 0; i < hash->size; i++) {
1962 head = &hash->table[i];
1963 list_lock = &hash->list_locks[i];
1964
1965 spin_lock_bh(list_lock);
b67bfe0d 1966 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
7683fdc1 1967 head, hash_entry) {
b67bfe0d 1968 hlist_del_rcu(&tt_common_entry->hash_entry);
56303d34
SE
1969 tt_global = container_of(tt_common_entry,
1970 struct batadv_tt_global_entry,
1971 common);
1972 batadv_tt_global_entry_free_ref(tt_global);
7683fdc1
AQ
1973 }
1974 spin_unlock_bh(list_lock);
1975 }
1976
1a8eaf07 1977 batadv_hash_destroy(hash);
7683fdc1 1978
807736f6 1979 bat_priv->tt.global_hash = NULL;
c6c8fea2
SE
1980}
1981
56303d34
SE
1982static bool
1983_batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1984 struct batadv_tt_global_entry *tt_global_entry)
59b699cd
AQ
1985{
1986 bool ret = false;
1987
acd34afa
SE
1988 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1989 tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
59b699cd
AQ
1990 ret = true;
1991
2d2fcc2a
AQ
1992 /* check if the two clients are marked as isolated */
1993 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
1994 tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
1995 ret = true;
1996
59b699cd
AQ
1997 return ret;
1998}
1999
c018ad3d
AQ
2000/**
2001 * batadv_transtable_search - get the mesh destination for a given client
2002 * @bat_priv: the bat priv with all the soft interface information
2003 * @src: mac address of the source client
2004 * @addr: mac address of the destination client
2005 * @vid: VLAN identifier
2006 *
2007 * Returns a pointer to the originator that was selected as destination in the
2008 * mesh for contacting the client 'addr', NULL otherwise.
2009 * In case of multiple originators serving the same client, the function returns
2010 * the best one (best in terms of metric towards the destination node).
2011 *
2012 * If the two clients are AP isolated the function returns NULL.
2013 */
56303d34
SE
2014struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2015 const uint8_t *src,
c018ad3d
AQ
2016 const uint8_t *addr,
2017 unsigned short vid)
c6c8fea2 2018{
56303d34
SE
2019 struct batadv_tt_local_entry *tt_local_entry = NULL;
2020 struct batadv_tt_global_entry *tt_global_entry = NULL;
2021 struct batadv_orig_node *orig_node = NULL;
981d8900 2022 struct batadv_tt_orig_list_entry *best_entry;
b8cbd81d 2023
eceb22ae 2024 if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
c018ad3d 2025 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
068ee6e2
AQ
2026 if (!tt_local_entry ||
2027 (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
3d393e47
AQ
2028 goto out;
2029 }
7aadf889 2030
c018ad3d 2031 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2dafb49d 2032 if (!tt_global_entry)
7b36e8ee 2033 goto out;
7aadf889 2034
3d393e47 2035 /* check whether the clients should not communicate due to AP
9cfc7bd6
SE
2036 * isolation
2037 */
a513088d
SE
2038 if (tt_local_entry &&
2039 _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3d393e47
AQ
2040 goto out;
2041
db08e6e5 2042 rcu_read_lock();
4627456a 2043 best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
db08e6e5 2044 /* found anything? */
981d8900
SE
2045 if (best_entry)
2046 orig_node = best_entry->orig_node;
db08e6e5
SW
2047 if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2048 orig_node = NULL;
2049 rcu_read_unlock();
981d8900 2050
7b36e8ee 2051out:
3d393e47 2052 if (tt_global_entry)
a513088d 2053 batadv_tt_global_entry_free_ref(tt_global_entry);
3d393e47 2054 if (tt_local_entry)
a513088d 2055 batadv_tt_local_entry_free_ref(tt_local_entry);
3d393e47 2056
7b36e8ee 2057 return orig_node;
c6c8fea2 2058}
a73105b8 2059
ced72933
AQ
2060/**
2061 * batadv_tt_global_crc - calculates the checksum of the local table belonging
2062 * to the given orig_node
2063 * @bat_priv: the bat priv with all the soft interface information
0ffa9e8d 2064 * @orig_node: originator for which the CRC should be computed
7ea7b4a1 2065 * @vid: VLAN identifier for which the CRC32 has to be computed
0ffa9e8d
AQ
2066 *
2067 * This function computes the checksum for the global table corresponding to a
2068 * specific originator. In particular, the checksum is computed as follows: For
2069 * each client connected to the originator the CRC32C of the MAC address and the
2070 * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2071 * together.
2072 *
2073 * The idea behind is that CRC32C should be used as much as possible in order to
2074 * produce a unique hash of the table, but since the order which is used to feed
2075 * the CRC32C function affects the result and since every node in the network
2076 * probably sorts the clients differently, the hash function cannot be directly
2077 * computed over the entire table. Hence the CRC32C is used only on
2078 * the single client entry, while all the results are then xor'ed together
2079 * because the XOR operation can combine them all while trying to reduce the
2080 * noise as much as possible.
2081 *
2082 * Returns the checksum of the global table of a given originator.
ced72933
AQ
2083 */
2084static uint32_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
7ea7b4a1
AQ
2085 struct batadv_orig_node *orig_node,
2086 unsigned short vid)
a73105b8 2087{
807736f6 2088 struct batadv_hashtable *hash = bat_priv->tt.global_hash;
56303d34
SE
2089 struct batadv_tt_common_entry *tt_common;
2090 struct batadv_tt_global_entry *tt_global;
a73105b8 2091 struct hlist_head *head;
0ffa9e8d 2092 uint32_t i, crc_tmp, crc = 0;
0eb01568 2093 uint8_t flags;
a30e22ca 2094 __be16 tmp_vid;
a73105b8
AQ
2095
2096 for (i = 0; i < hash->size; i++) {
2097 head = &hash->table[i];
2098
2099 rcu_read_lock();
b67bfe0d 2100 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
56303d34
SE
2101 tt_global = container_of(tt_common,
2102 struct batadv_tt_global_entry,
2103 common);
7ea7b4a1
AQ
2104 /* compute the CRC only for entries belonging to the
2105 * VLAN identified by the vid passed as parameter
2106 */
2107 if (tt_common->vid != vid)
2108 continue;
2109
db08e6e5
SW
2110 /* Roaming clients are in the global table for
2111 * consistency only. They don't have to be
2112 * taken into account while computing the
2113 * global crc
2114 */
acd34afa 2115 if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
db08e6e5 2116 continue;
30cfd02b
AQ
2117 /* Temporary clients have not been announced yet, so
2118 * they have to be skipped while computing the global
2119 * crc
2120 */
2121 if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2122 continue;
db08e6e5
SW
2123
2124 /* find out if this global entry is announced by this
2125 * originator
2126 */
56303d34 2127 if (!batadv_tt_global_entry_has_orig(tt_global,
a513088d 2128 orig_node))
db08e6e5
SW
2129 continue;
2130
a30e22ca
AQ
2131 /* use network order to read the VID: this ensures that
2132 * every node reads the bytes in the same order.
2133 */
2134 tmp_vid = htons(tt_common->vid);
2135 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
0eb01568
AQ
2136
2137 /* compute the CRC on flags that have to be kept in sync
2138 * among nodes
2139 */
2140 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2141 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2142
0ffa9e8d 2143 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
a73105b8
AQ
2144 }
2145 rcu_read_unlock();
2146 }
2147
ced72933 2148 return crc;
a73105b8
AQ
2149}
2150
ced72933
AQ
2151/**
2152 * batadv_tt_local_crc - calculates the checksum of the local table
2153 * @bat_priv: the bat priv with all the soft interface information
7ea7b4a1 2154 * @vid: VLAN identifier for which the CRC32 has to be computed
0ffa9e8d
AQ
2155 *
2156 * For details about the computation, please refer to the documentation for
2157 * batadv_tt_global_crc().
2158 *
2159 * Returns the checksum of the local table
ced72933 2160 */
7ea7b4a1
AQ
2161static uint32_t batadv_tt_local_crc(struct batadv_priv *bat_priv,
2162 unsigned short vid)
a73105b8 2163{
807736f6 2164 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34 2165 struct batadv_tt_common_entry *tt_common;
a73105b8 2166 struct hlist_head *head;
0ffa9e8d 2167 uint32_t i, crc_tmp, crc = 0;
0eb01568 2168 uint8_t flags;
a30e22ca 2169 __be16 tmp_vid;
a73105b8
AQ
2170
2171 for (i = 0; i < hash->size; i++) {
2172 head = &hash->table[i];
2173
2174 rcu_read_lock();
b67bfe0d 2175 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
7ea7b4a1
AQ
2176 /* compute the CRC only for entries belonging to the
2177 * VLAN identified by vid
2178 */
2179 if (tt_common->vid != vid)
2180 continue;
2181
058d0e26 2182 /* not yet committed clients have not to be taken into
9cfc7bd6
SE
2183 * account while computing the CRC
2184 */
acd34afa 2185 if (tt_common->flags & BATADV_TT_CLIENT_NEW)
058d0e26 2186 continue;
ced72933 2187
a30e22ca
AQ
2188 /* use network order to read the VID: this ensures that
2189 * every node reads the bytes in the same order.
2190 */
2191 tmp_vid = htons(tt_common->vid);
2192 crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
0eb01568
AQ
2193
2194 /* compute the CRC on flags that have to be kept in sync
2195 * among nodes
2196 */
2197 flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2198 crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2199
0ffa9e8d 2200 crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
a73105b8 2201 }
a73105b8
AQ
2202 rcu_read_unlock();
2203 }
2204
ced72933 2205 return crc;
a73105b8
AQ
2206}
2207
56303d34 2208static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
a73105b8 2209{
56303d34 2210 struct batadv_tt_req_node *node, *safe;
a73105b8 2211
807736f6 2212 spin_lock_bh(&bat_priv->tt.req_list_lock);
a73105b8 2213
807736f6 2214 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
a73105b8
AQ
2215 list_del(&node->list);
2216 kfree(node);
2217 }
2218
807736f6 2219 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2220}
2221
56303d34
SE
2222static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2223 struct batadv_orig_node *orig_node,
e8cf234a
AQ
2224 const void *tt_buff,
2225 uint16_t tt_buff_len)
a73105b8 2226{
a73105b8 2227 /* Replace the old buffer only if I received something in the
9cfc7bd6
SE
2228 * last OGM (the OGM could carry no changes)
2229 */
a73105b8
AQ
2230 spin_lock_bh(&orig_node->tt_buff_lock);
2231 if (tt_buff_len > 0) {
2232 kfree(orig_node->tt_buff);
2233 orig_node->tt_buff_len = 0;
2234 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2235 if (orig_node->tt_buff) {
2236 memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2237 orig_node->tt_buff_len = tt_buff_len;
2238 }
2239 }
2240 spin_unlock_bh(&orig_node->tt_buff_lock);
2241}
2242
56303d34 2243static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
a73105b8 2244{
56303d34 2245 struct batadv_tt_req_node *node, *safe;
a73105b8 2246
807736f6
SE
2247 spin_lock_bh(&bat_priv->tt.req_list_lock);
2248 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
42d0b044
SE
2249 if (batadv_has_timed_out(node->issued_at,
2250 BATADV_TT_REQUEST_TIMEOUT)) {
a73105b8
AQ
2251 list_del(&node->list);
2252 kfree(node);
2253 }
2254 }
807736f6 2255 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2256}
2257
2258/* returns the pointer to the new tt_req_node struct if no request
9cfc7bd6
SE
2259 * has already been issued for this orig_node, NULL otherwise
2260 */
56303d34
SE
2261static struct batadv_tt_req_node *
2262batadv_new_tt_req_node(struct batadv_priv *bat_priv,
2263 struct batadv_orig_node *orig_node)
a73105b8 2264{
56303d34 2265 struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
a73105b8 2266
807736f6
SE
2267 spin_lock_bh(&bat_priv->tt.req_list_lock);
2268 list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1eda58bf
SE
2269 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2270 !batadv_has_timed_out(tt_req_node_tmp->issued_at,
42d0b044 2271 BATADV_TT_REQUEST_TIMEOUT))
a73105b8
AQ
2272 goto unlock;
2273 }
2274
2275 tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2276 if (!tt_req_node)
2277 goto unlock;
2278
8fdd0153 2279 ether_addr_copy(tt_req_node->addr, orig_node->orig);
a73105b8
AQ
2280 tt_req_node->issued_at = jiffies;
2281
807736f6 2282 list_add(&tt_req_node->list, &bat_priv->tt.req_list);
a73105b8 2283unlock:
807736f6 2284 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2285 return tt_req_node;
2286}
2287
335fbe0f
ML
2288/**
2289 * batadv_tt_local_valid - verify that given tt entry is a valid one
2290 * @entry_ptr: to be checked local tt entry
2291 * @data_ptr: not used but definition required to satisfy the callback prototype
2292 *
2293 * Returns 1 if the entry is a valid, 0 otherwise.
2294 */
2295static int batadv_tt_local_valid(const void *entry_ptr, const void *data_ptr)
058d0e26 2296{
56303d34 2297 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
058d0e26 2298
acd34afa 2299 if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
058d0e26
AQ
2300 return 0;
2301 return 1;
2302}
2303
a513088d
SE
2304static int batadv_tt_global_valid(const void *entry_ptr,
2305 const void *data_ptr)
a73105b8 2306{
56303d34
SE
2307 const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2308 const struct batadv_tt_global_entry *tt_global_entry;
2309 const struct batadv_orig_node *orig_node = data_ptr;
a73105b8 2310
30cfd02b
AQ
2311 if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2312 tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
cc47f66e
AQ
2313 return 0;
2314
56303d34
SE
2315 tt_global_entry = container_of(tt_common_entry,
2316 struct batadv_tt_global_entry,
48100bac
AQ
2317 common);
2318
a513088d 2319 return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
a73105b8
AQ
2320}
2321
335fbe0f 2322/**
7ea7b4a1
AQ
2323 * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2324 * specified tt hash
335fbe0f
ML
2325 * @bat_priv: the bat priv with all the soft interface information
2326 * @hash: hash table containing the tt entries
2327 * @tt_len: expected tvlv tt data buffer length in number of bytes
7ea7b4a1 2328 * @tvlv_buff: pointer to the buffer to fill with the TT data
335fbe0f
ML
2329 * @valid_cb: function to filter tt change entries
2330 * @cb_data: data passed to the filter function as argument
335fbe0f 2331 */
7ea7b4a1
AQ
2332static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2333 struct batadv_hashtable *hash,
2334 void *tvlv_buff, uint16_t tt_len,
2335 int (*valid_cb)(const void *, const void *),
2336 void *cb_data)
a73105b8 2337{
56303d34 2338 struct batadv_tt_common_entry *tt_common_entry;
335fbe0f 2339 struct batadv_tvlv_tt_change *tt_change;
a73105b8 2340 struct hlist_head *head;
335fbe0f 2341 uint16_t tt_tot, tt_num_entries = 0;
c90681b8 2342 uint32_t i;
a73105b8 2343
298e6e68 2344 tt_tot = batadv_tt_entries(tt_len);
7ea7b4a1 2345 tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
a73105b8
AQ
2346
2347 rcu_read_lock();
2348 for (i = 0; i < hash->size; i++) {
2349 head = &hash->table[i];
2350
b67bfe0d 2351 hlist_for_each_entry_rcu(tt_common_entry,
a73105b8 2352 head, hash_entry) {
335fbe0f 2353 if (tt_tot == tt_num_entries)
a73105b8
AQ
2354 break;
2355
48100bac 2356 if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
a73105b8
AQ
2357 continue;
2358
8fdd0153 2359 ether_addr_copy(tt_change->addr, tt_common_entry->addr);
27b37ebf 2360 tt_change->flags = tt_common_entry->flags;
c018ad3d 2361 tt_change->vid = htons(tt_common_entry->vid);
ca663046
AQ
2362 memset(tt_change->reserved, 0,
2363 sizeof(tt_change->reserved));
a73105b8 2364
335fbe0f 2365 tt_num_entries++;
a73105b8
AQ
2366 tt_change++;
2367 }
2368 }
2369 rcu_read_unlock();
7ea7b4a1 2370}
a73105b8 2371
7ea7b4a1
AQ
2372/**
2373 * batadv_tt_global_check_crc - check if all the CRCs are correct
2374 * @orig_node: originator for which the CRCs have to be checked
2375 * @tt_vlan: pointer to the first tvlv VLAN entry
2376 * @num_vlan: number of tvlv VLAN entries
2377 * @create: if true, create VLAN objects if not found
2378 *
2379 * Return true if all the received CRCs match the locally stored ones, false
2380 * otherwise
2381 */
2382static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2383 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2384 uint16_t num_vlan)
2385{
2386 struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2387 struct batadv_orig_node_vlan *vlan;
91c2b1a9 2388 uint32_t crc;
7ea7b4a1
AQ
2389 int i;
2390
2391 /* check if each received CRC matches the locally stored one */
2392 for (i = 0; i < num_vlan; i++) {
2393 tt_vlan_tmp = tt_vlan + i;
2394
2395 /* if orig_node is a backbone node for this VLAN, don't check
2396 * the CRC as we ignore all the global entries over it
2397 */
2398 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
cfd4f757
AQ
2399 orig_node->orig,
2400 ntohs(tt_vlan_tmp->vid)))
7ea7b4a1
AQ
2401 continue;
2402
2403 vlan = batadv_orig_node_vlan_get(orig_node,
2404 ntohs(tt_vlan_tmp->vid));
2405 if (!vlan)
2406 return false;
2407
91c2b1a9
AQ
2408 crc = vlan->tt.crc;
2409 batadv_orig_node_vlan_free_ref(vlan);
2410
2411 if (crc != ntohl(tt_vlan_tmp->crc))
7ea7b4a1
AQ
2412 return false;
2413 }
2414
2415 return true;
2416}
2417
2418/**
2419 * batadv_tt_local_update_crc - update all the local CRCs
2420 * @bat_priv: the bat priv with all the soft interface information
2421 */
2422static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2423{
2424 struct batadv_softif_vlan *vlan;
2425
2426 /* recompute the global CRC for each VLAN */
2427 rcu_read_lock();
2428 hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2429 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2430 }
2431 rcu_read_unlock();
2432}
2433
2434/**
2435 * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2436 * @bat_priv: the bat priv with all the soft interface information
2437 * @orig_node: the orig_node for which the CRCs have to be updated
2438 */
2439static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2440 struct batadv_orig_node *orig_node)
2441{
2442 struct batadv_orig_node_vlan *vlan;
2443 uint32_t crc;
2444
2445 /* recompute the global CRC for each VLAN */
2446 rcu_read_lock();
2447 list_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2448 /* if orig_node is a backbone node for this VLAN, don't compute
2449 * the CRC as we ignore all the global entries over it
2450 */
cfd4f757
AQ
2451 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2452 vlan->vid))
7ea7b4a1
AQ
2453 continue;
2454
2455 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2456 vlan->tt.crc = crc;
2457 }
2458 rcu_read_unlock();
a73105b8
AQ
2459}
2460
ced72933
AQ
2461/**
2462 * batadv_send_tt_request - send a TT Request message to a given node
2463 * @bat_priv: the bat priv with all the soft interface information
2464 * @dst_orig_node: the destination of the message
2465 * @ttvn: the version number that the source of the message is looking for
7ea7b4a1
AQ
2466 * @tt_vlan: pointer to the first tvlv VLAN object to request
2467 * @num_vlan: number of tvlv VLAN entries
ced72933
AQ
2468 * @full_table: ask for the entire translation table if true, while only for the
2469 * last TT diff otherwise
2470 */
56303d34
SE
2471static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2472 struct batadv_orig_node *dst_orig_node,
7ea7b4a1
AQ
2473 uint8_t ttvn,
2474 struct batadv_tvlv_tt_vlan_data *tt_vlan,
2475 uint16_t num_vlan, bool full_table)
a73105b8 2476{
335fbe0f 2477 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
56303d34 2478 struct batadv_tt_req_node *tt_req_node = NULL;
7ea7b4a1
AQ
2479 struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2480 struct batadv_hard_iface *primary_if;
335fbe0f 2481 bool ret = false;
7ea7b4a1 2482 int i, size;
a73105b8 2483
e5d89254 2484 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
2485 if (!primary_if)
2486 goto out;
2487
2488 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
2489 * reply from the same orig_node yet
2490 */
a513088d 2491 tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
a73105b8
AQ
2492 if (!tt_req_node)
2493 goto out;
2494
7ea7b4a1
AQ
2495 size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2496 tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
335fbe0f 2497 if (!tvlv_tt_data)
a73105b8
AQ
2498 goto out;
2499
335fbe0f
ML
2500 tvlv_tt_data->flags = BATADV_TT_REQUEST;
2501 tvlv_tt_data->ttvn = ttvn;
7ea7b4a1
AQ
2502 tvlv_tt_data->num_vlan = htons(num_vlan);
2503
2504 /* send all the CRCs within the request. This is needed by intermediate
2505 * nodes to ensure they have the correct table before replying
2506 */
2507 tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2508 for (i = 0; i < num_vlan; i++) {
2509 tt_vlan_req->vid = tt_vlan->vid;
2510 tt_vlan_req->crc = tt_vlan->crc;
2511
2512 tt_vlan_req++;
2513 tt_vlan++;
2514 }
a73105b8
AQ
2515
2516 if (full_table)
335fbe0f 2517 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
a73105b8 2518
bb351ba0 2519 batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
335fbe0f 2520 dst_orig_node->orig, full_table ? 'F' : '.');
a73105b8 2521
d69909d2 2522 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
335fbe0f
ML
2523 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2524 dst_orig_node->orig, BATADV_TVLV_TT, 1,
7ea7b4a1 2525 tvlv_tt_data, size);
335fbe0f 2526 ret = true;
a73105b8
AQ
2527
2528out:
a73105b8 2529 if (primary_if)
e5d89254 2530 batadv_hardif_free_ref(primary_if);
a73105b8 2531 if (ret && tt_req_node) {
807736f6 2532 spin_lock_bh(&bat_priv->tt.req_list_lock);
a73105b8 2533 list_del(&tt_req_node->list);
807736f6 2534 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2535 kfree(tt_req_node);
2536 }
335fbe0f 2537 kfree(tvlv_tt_data);
a73105b8
AQ
2538 return ret;
2539}
2540
335fbe0f
ML
2541/**
2542 * batadv_send_other_tt_response - send reply to tt request concerning another
2543 * node's translation table
2544 * @bat_priv: the bat priv with all the soft interface information
2545 * @tt_data: tt data containing the tt request information
2546 * @req_src: mac address of tt request sender
2547 * @req_dst: mac address of tt request recipient
2548 *
2549 * Returns true if tt request reply was sent, false otherwise.
2550 */
2551static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2552 struct batadv_tvlv_tt_data *tt_data,
2553 uint8_t *req_src, uint8_t *req_dst)
a73105b8 2554{
170173bf 2555 struct batadv_orig_node *req_dst_orig_node;
56303d34 2556 struct batadv_orig_node *res_dst_orig_node = NULL;
7ea7b4a1 2557 struct batadv_tvlv_tt_change *tt_change;
335fbe0f 2558 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
7ea7b4a1 2559 struct batadv_tvlv_tt_vlan_data *tt_vlan;
335fbe0f 2560 bool ret = false, full_table;
7ea7b4a1
AQ
2561 uint8_t orig_ttvn, req_ttvn;
2562 uint16_t tvlv_len;
2563 int32_t tt_len;
a73105b8 2564
39c75a51 2565 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 2566 "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
335fbe0f 2567 req_src, tt_data->ttvn, req_dst,
a2f2b6cd 2568 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
a73105b8
AQ
2569
2570 /* Let's get the orig node of the REAL destination */
335fbe0f 2571 req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
a73105b8
AQ
2572 if (!req_dst_orig_node)
2573 goto out;
2574
335fbe0f 2575 res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
a73105b8
AQ
2576 if (!res_dst_orig_node)
2577 goto out;
2578
a73105b8 2579 orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
335fbe0f 2580 req_ttvn = tt_data->ttvn;
a73105b8 2581
7ea7b4a1 2582 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
335fbe0f 2583 /* this node doesn't have the requested data */
a73105b8 2584 if (orig_ttvn != req_ttvn ||
7ea7b4a1
AQ
2585 !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2586 ntohs(tt_data->num_vlan)))
a73105b8
AQ
2587 goto out;
2588
015758d0 2589 /* If the full table has been explicitly requested */
335fbe0f 2590 if (tt_data->flags & BATADV_TT_FULL_TABLE ||
a73105b8
AQ
2591 !req_dst_orig_node->tt_buff)
2592 full_table = true;
2593 else
2594 full_table = false;
2595
335fbe0f
ML
2596 /* TT fragmentation hasn't been implemented yet, so send as many
2597 * TT entries fit a single packet as possible only
9cfc7bd6 2598 */
a73105b8
AQ
2599 if (!full_table) {
2600 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2601 tt_len = req_dst_orig_node->tt_buff_len;
a73105b8 2602
7ea7b4a1
AQ
2603 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2604 &tvlv_tt_data,
2605 &tt_change,
2606 &tt_len);
2607 if (!tt_len)
a73105b8
AQ
2608 goto unlock;
2609
a73105b8 2610 /* Copy the last orig_node's OGM buffer */
7ea7b4a1 2611 memcpy(tt_change, req_dst_orig_node->tt_buff,
a73105b8 2612 req_dst_orig_node->tt_buff_len);
a73105b8
AQ
2613 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2614 } else {
7ea7b4a1
AQ
2615 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2616 * in the initial part
2617 */
2618 tt_len = -1;
2619 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2620 &tvlv_tt_data,
2621 &tt_change,
2622 &tt_len);
2623 if (!tt_len)
a73105b8 2624 goto out;
7ea7b4a1
AQ
2625
2626 /* fill the rest of the tvlv with the real TT entries */
2627 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2628 tt_change, tt_len,
2629 batadv_tt_global_valid,
2630 req_dst_orig_node);
a73105b8
AQ
2631 }
2632
a19d3d85
ML
2633 /* Don't send the response, if larger than fragmented packet. */
2634 tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2635 if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2636 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2637 "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2638 res_dst_orig_node->orig);
2639 goto out;
2640 }
2641
335fbe0f
ML
2642 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2643 tvlv_tt_data->ttvn = req_ttvn;
a73105b8
AQ
2644
2645 if (full_table)
335fbe0f 2646 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
a73105b8 2647
39c75a51 2648 batadv_dbg(BATADV_DBG_TT, bat_priv,
335fbe0f
ML
2649 "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2650 res_dst_orig_node->orig, req_dst_orig_node->orig,
2651 full_table ? 'F' : '.', req_ttvn);
a73105b8 2652
d69909d2 2653 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
f8214865 2654
335fbe0f 2655 batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
7ea7b4a1
AQ
2656 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2657 tvlv_len);
e91ecfc6 2658
335fbe0f 2659 ret = true;
a73105b8
AQ
2660 goto out;
2661
2662unlock:
2663 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2664
2665out:
2666 if (res_dst_orig_node)
7d211efc 2667 batadv_orig_node_free_ref(res_dst_orig_node);
a73105b8 2668 if (req_dst_orig_node)
7d211efc 2669 batadv_orig_node_free_ref(req_dst_orig_node);
335fbe0f 2670 kfree(tvlv_tt_data);
a73105b8 2671 return ret;
a73105b8 2672}
96412690 2673
335fbe0f
ML
2674/**
2675 * batadv_send_my_tt_response - send reply to tt request concerning this node's
2676 * translation table
2677 * @bat_priv: the bat priv with all the soft interface information
2678 * @tt_data: tt data containing the tt request information
2679 * @req_src: mac address of tt request sender
2680 *
2681 * Returns true if tt request reply was sent, false otherwise.
2682 */
2683static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2684 struct batadv_tvlv_tt_data *tt_data,
2685 uint8_t *req_src)
a73105b8 2686{
335fbe0f 2687 struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
56303d34 2688 struct batadv_hard_iface *primary_if = NULL;
7ea7b4a1
AQ
2689 struct batadv_tvlv_tt_change *tt_change;
2690 struct batadv_orig_node *orig_node;
335fbe0f 2691 uint8_t my_ttvn, req_ttvn;
7ea7b4a1 2692 uint16_t tvlv_len;
a73105b8 2693 bool full_table;
7ea7b4a1 2694 int32_t tt_len;
a73105b8 2695
39c75a51 2696 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 2697 "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
335fbe0f 2698 req_src, tt_data->ttvn,
a2f2b6cd 2699 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
a73105b8 2700
a70a9aa9 2701 spin_lock_bh(&bat_priv->tt.commit_lock);
a73105b8 2702
807736f6 2703 my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
335fbe0f 2704 req_ttvn = tt_data->ttvn;
a73105b8 2705
335fbe0f 2706 orig_node = batadv_orig_hash_find(bat_priv, req_src);
a73105b8
AQ
2707 if (!orig_node)
2708 goto out;
2709
e5d89254 2710 primary_if = batadv_primary_if_get_selected(bat_priv);
a73105b8
AQ
2711 if (!primary_if)
2712 goto out;
2713
2714 /* If the full table has been explicitly requested or the gap
9cfc7bd6
SE
2715 * is too big send the whole local translation table
2716 */
335fbe0f 2717 if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
807736f6 2718 !bat_priv->tt.last_changeset)
a73105b8
AQ
2719 full_table = true;
2720 else
2721 full_table = false;
2722
335fbe0f
ML
2723 /* TT fragmentation hasn't been implemented yet, so send as many
2724 * TT entries fit a single packet as possible only
9cfc7bd6 2725 */
a73105b8 2726 if (!full_table) {
807736f6 2727 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 2728
7ea7b4a1
AQ
2729 tt_len = bat_priv->tt.last_changeset_len;
2730 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2731 &tvlv_tt_data,
2732 &tt_change,
2733 &tt_len);
2734 if (!tt_len)
a73105b8
AQ
2735 goto unlock;
2736
335fbe0f 2737 /* Copy the last orig_node's OGM buffer */
7ea7b4a1 2738 memcpy(tt_change, bat_priv->tt.last_changeset,
807736f6
SE
2739 bat_priv->tt.last_changeset_len);
2740 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 2741 } else {
335fbe0f
ML
2742 req_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
2743
7ea7b4a1
AQ
2744 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2745 * in the initial part
2746 */
2747 tt_len = -1;
2748 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2749 &tvlv_tt_data,
2750 &tt_change,
2751 &tt_len);
2752 if (!tt_len)
a73105b8 2753 goto out;
7ea7b4a1
AQ
2754
2755 /* fill the rest of the tvlv with the real TT entries */
2756 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2757 tt_change, tt_len,
2758 batadv_tt_local_valid, NULL);
a73105b8
AQ
2759 }
2760
335fbe0f
ML
2761 tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2762 tvlv_tt_data->ttvn = req_ttvn;
a73105b8
AQ
2763
2764 if (full_table)
335fbe0f 2765 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
a73105b8 2766
39c75a51 2767 batadv_dbg(BATADV_DBG_TT, bat_priv,
335fbe0f
ML
2768 "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2769 orig_node->orig, full_table ? 'F' : '.', req_ttvn);
a73105b8 2770
d69909d2 2771 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
f8214865 2772
335fbe0f 2773 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
7ea7b4a1
AQ
2774 req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2775 tvlv_len);
335fbe0f 2776
a73105b8
AQ
2777 goto out;
2778
2779unlock:
807736f6 2780 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
a73105b8 2781out:
a70a9aa9 2782 spin_unlock_bh(&bat_priv->tt.commit_lock);
a73105b8 2783 if (orig_node)
7d211efc 2784 batadv_orig_node_free_ref(orig_node);
a73105b8 2785 if (primary_if)
e5d89254 2786 batadv_hardif_free_ref(primary_if);
335fbe0f
ML
2787 kfree(tvlv_tt_data);
2788 /* The packet was for this host, so it doesn't need to be re-routed */
a73105b8
AQ
2789 return true;
2790}
2791
335fbe0f
ML
2792/**
2793 * batadv_send_tt_response - send reply to tt request
2794 * @bat_priv: the bat priv with all the soft interface information
2795 * @tt_data: tt data containing the tt request information
2796 * @req_src: mac address of tt request sender
2797 * @req_dst: mac address of tt request recipient
2798 *
2799 * Returns true if tt request reply was sent, false otherwise.
2800 */
2801static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2802 struct batadv_tvlv_tt_data *tt_data,
2803 uint8_t *req_src, uint8_t *req_dst)
a73105b8 2804{
cfd4f757 2805 if (batadv_is_my_mac(bat_priv, req_dst))
335fbe0f 2806 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
24820df1
AQ
2807 return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2808 req_dst);
a73105b8
AQ
2809}
2810
56303d34
SE
2811static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2812 struct batadv_orig_node *orig_node,
335fbe0f 2813 struct batadv_tvlv_tt_change *tt_change,
a513088d 2814 uint16_t tt_num_changes, uint8_t ttvn)
a73105b8
AQ
2815{
2816 int i;
a513088d 2817 int roams;
a73105b8
AQ
2818
2819 for (i = 0; i < tt_num_changes; i++) {
acd34afa
SE
2820 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2821 roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
a513088d
SE
2822 batadv_tt_global_del(bat_priv, orig_node,
2823 (tt_change + i)->addr,
c018ad3d 2824 ntohs((tt_change + i)->vid),
d4f44692
AQ
2825 "tt removed by changes",
2826 roams);
08c36d3e 2827 } else {
08c36d3e 2828 if (!batadv_tt_global_add(bat_priv, orig_node,
d4f44692 2829 (tt_change + i)->addr,
c018ad3d 2830 ntohs((tt_change + i)->vid),
d4f44692 2831 (tt_change + i)->flags, ttvn))
a73105b8
AQ
2832 /* In case of problem while storing a
2833 * global_entry, we stop the updating
2834 * procedure without committing the
2835 * ttvn change. This will avoid to send
2836 * corrupted data on tt_request
2837 */
2838 return;
08c36d3e 2839 }
a73105b8 2840 }
e17931d1 2841 orig_node->capa_initialized |= BATADV_ORIG_CAPA_HAS_TT;
a73105b8
AQ
2842}
2843
56303d34 2844static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
7ea7b4a1
AQ
2845 struct batadv_tvlv_tt_change *tt_change,
2846 uint8_t ttvn, uint8_t *resp_src,
2847 uint16_t num_entries)
a73105b8 2848{
170173bf 2849 struct batadv_orig_node *orig_node;
a73105b8 2850
335fbe0f 2851 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
a73105b8
AQ
2852 if (!orig_node)
2853 goto out;
2854
2855 /* Purge the old table first.. */
95fb130d
AQ
2856 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2857 "Received full table");
a73105b8 2858
7ea7b4a1
AQ
2859 _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2860 ttvn);
a73105b8
AQ
2861
2862 spin_lock_bh(&orig_node->tt_buff_lock);
2863 kfree(orig_node->tt_buff);
2864 orig_node->tt_buff_len = 0;
2865 orig_node->tt_buff = NULL;
2866 spin_unlock_bh(&orig_node->tt_buff_lock);
2867
7ea7b4a1 2868 atomic_set(&orig_node->last_ttvn, ttvn);
a73105b8
AQ
2869
2870out:
2871 if (orig_node)
7d211efc 2872 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
2873}
2874
56303d34
SE
2875static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2876 struct batadv_orig_node *orig_node,
a513088d 2877 uint16_t tt_num_changes, uint8_t ttvn,
335fbe0f 2878 struct batadv_tvlv_tt_change *tt_change)
a73105b8 2879{
a513088d
SE
2880 _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2881 tt_num_changes, ttvn);
a73105b8 2882
e8cf234a
AQ
2883 batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
2884 batadv_tt_len(tt_num_changes));
a73105b8
AQ
2885 atomic_set(&orig_node->last_ttvn, ttvn);
2886}
2887
c018ad3d
AQ
2888/**
2889 * batadv_is_my_client - check if a client is served by the local node
2890 * @bat_priv: the bat priv with all the soft interface information
3f68785e 2891 * @addr: the mac address of the client to check
c018ad3d
AQ
2892 * @vid: VLAN identifier
2893 *
2894 * Returns true if the client is served by this node, false otherwise.
2895 */
2896bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr,
2897 unsigned short vid)
a73105b8 2898{
170173bf 2899 struct batadv_tt_local_entry *tt_local_entry;
7683fdc1 2900 bool ret = false;
a73105b8 2901
c018ad3d 2902 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
7683fdc1
AQ
2903 if (!tt_local_entry)
2904 goto out;
058d0e26 2905 /* Check if the client has been logically deleted (but is kept for
9cfc7bd6
SE
2906 * consistency purpose)
2907 */
7c1fd91d
AQ
2908 if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2909 (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
058d0e26 2910 goto out;
7683fdc1
AQ
2911 ret = true;
2912out:
a73105b8 2913 if (tt_local_entry)
a513088d 2914 batadv_tt_local_entry_free_ref(tt_local_entry);
7683fdc1 2915 return ret;
a73105b8
AQ
2916}
2917
335fbe0f
ML
2918/**
2919 * batadv_handle_tt_response - process incoming tt reply
2920 * @bat_priv: the bat priv with all the soft interface information
2921 * @tt_data: tt data containing the tt request information
2922 * @resp_src: mac address of tt reply sender
2923 * @num_entries: number of tt change entries appended to the tt data
2924 */
2925static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2926 struct batadv_tvlv_tt_data *tt_data,
2927 uint8_t *resp_src, uint16_t num_entries)
a73105b8 2928{
56303d34
SE
2929 struct batadv_tt_req_node *node, *safe;
2930 struct batadv_orig_node *orig_node = NULL;
335fbe0f 2931 struct batadv_tvlv_tt_change *tt_change;
7ea7b4a1
AQ
2932 uint8_t *tvlv_ptr = (uint8_t *)tt_data;
2933 uint16_t change_offset;
a73105b8 2934
39c75a51 2935 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 2936 "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
335fbe0f 2937 resp_src, tt_data->ttvn, num_entries,
a2f2b6cd 2938 ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
a73105b8 2939
335fbe0f 2940 orig_node = batadv_orig_hash_find(bat_priv, resp_src);
a73105b8
AQ
2941 if (!orig_node)
2942 goto out;
2943
a70a9aa9
AQ
2944 spin_lock_bh(&orig_node->tt_lock);
2945
7ea7b4a1
AQ
2946 change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
2947 change_offset *= ntohs(tt_data->num_vlan);
2948 change_offset += sizeof(*tt_data);
2949 tvlv_ptr += change_offset;
2950
2951 tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
335fbe0f 2952 if (tt_data->flags & BATADV_TT_FULL_TABLE) {
7ea7b4a1
AQ
2953 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
2954 resp_src, num_entries);
96412690 2955 } else {
335fbe0f
ML
2956 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
2957 tt_data->ttvn, tt_change);
96412690 2958 }
a73105b8 2959
a70a9aa9 2960 /* Recalculate the CRC for this orig_node and store it */
7ea7b4a1 2961 batadv_tt_global_update_crc(bat_priv, orig_node);
a70a9aa9
AQ
2962
2963 spin_unlock_bh(&orig_node->tt_lock);
2964
a73105b8 2965 /* Delete the tt_req_node from pending tt_requests list */
807736f6
SE
2966 spin_lock_bh(&bat_priv->tt.req_list_lock);
2967 list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
335fbe0f 2968 if (!batadv_compare_eth(node->addr, resp_src))
a73105b8
AQ
2969 continue;
2970 list_del(&node->list);
2971 kfree(node);
2972 }
7ea7b4a1 2973
807736f6 2974 spin_unlock_bh(&bat_priv->tt.req_list_lock);
a73105b8
AQ
2975out:
2976 if (orig_node)
7d211efc 2977 batadv_orig_node_free_ref(orig_node);
a73105b8
AQ
2978}
2979
56303d34 2980static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
a73105b8 2981{
56303d34 2982 struct batadv_tt_roam_node *node, *safe;
a73105b8 2983
807736f6 2984 spin_lock_bh(&bat_priv->tt.roam_list_lock);
a73105b8 2985
807736f6 2986 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
cc47f66e
AQ
2987 list_del(&node->list);
2988 kfree(node);
2989 }
2990
807736f6 2991 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
2992}
2993
56303d34 2994static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
cc47f66e 2995{
56303d34 2996 struct batadv_tt_roam_node *node, *safe;
cc47f66e 2997
807736f6
SE
2998 spin_lock_bh(&bat_priv->tt.roam_list_lock);
2999 list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
42d0b044
SE
3000 if (!batadv_has_timed_out(node->first_time,
3001 BATADV_ROAMING_MAX_TIME))
cc47f66e
AQ
3002 continue;
3003
3004 list_del(&node->list);
3005 kfree(node);
3006 }
807736f6 3007 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
3008}
3009
3010/* This function checks whether the client already reached the
3011 * maximum number of possible roaming phases. In this case the ROAMING_ADV
3012 * will not be sent.
3013 *
9cfc7bd6
SE
3014 * returns true if the ROAMING_ADV can be sent, false otherwise
3015 */
56303d34 3016static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
a513088d 3017 uint8_t *client)
cc47f66e 3018{
56303d34 3019 struct batadv_tt_roam_node *tt_roam_node;
cc47f66e
AQ
3020 bool ret = false;
3021
807736f6 3022 spin_lock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e 3023 /* The new tt_req will be issued only if I'm not waiting for a
9cfc7bd6
SE
3024 * reply from the same orig_node yet
3025 */
807736f6 3026 list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
1eda58bf 3027 if (!batadv_compare_eth(tt_roam_node->addr, client))
cc47f66e
AQ
3028 continue;
3029
1eda58bf 3030 if (batadv_has_timed_out(tt_roam_node->first_time,
42d0b044 3031 BATADV_ROAMING_MAX_TIME))
cc47f66e
AQ
3032 continue;
3033
3e34819e 3034 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
cc47f66e
AQ
3035 /* Sorry, you roamed too many times! */
3036 goto unlock;
3037 ret = true;
3038 break;
3039 }
3040
3041 if (!ret) {
3042 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3043 if (!tt_roam_node)
3044 goto unlock;
3045
3046 tt_roam_node->first_time = jiffies;
42d0b044
SE
3047 atomic_set(&tt_roam_node->counter,
3048 BATADV_ROAMING_MAX_COUNT - 1);
8fdd0153 3049 ether_addr_copy(tt_roam_node->addr, client);
cc47f66e 3050
807736f6 3051 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
cc47f66e
AQ
3052 ret = true;
3053 }
3054
3055unlock:
807736f6 3056 spin_unlock_bh(&bat_priv->tt.roam_list_lock);
cc47f66e
AQ
3057 return ret;
3058}
3059
c018ad3d
AQ
3060/**
3061 * batadv_send_roam_adv - send a roaming advertisement message
3062 * @bat_priv: the bat priv with all the soft interface information
3063 * @client: mac address of the roaming client
3064 * @vid: VLAN identifier
3065 * @orig_node: message destination
3066 *
3067 * Send a ROAMING_ADV message to the node which was previously serving this
3068 * client. This is done to inform the node that from now on all traffic destined
3069 * for this particular roamed client has to be forwarded to the sender of the
3070 * roaming message.
3071 */
56303d34 3072static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
c018ad3d 3073 unsigned short vid,
56303d34 3074 struct batadv_orig_node *orig_node)
cc47f66e 3075{
56303d34 3076 struct batadv_hard_iface *primary_if;
122edaa0
ML
3077 struct batadv_tvlv_roam_adv tvlv_roam;
3078
3079 primary_if = batadv_primary_if_get_selected(bat_priv);
3080 if (!primary_if)
3081 goto out;
cc47f66e
AQ
3082
3083 /* before going on we have to check whether the client has
9cfc7bd6
SE
3084 * already roamed to us too many times
3085 */
a513088d 3086 if (!batadv_tt_check_roam_count(bat_priv, client))
cc47f66e
AQ
3087 goto out;
3088
39c75a51 3089 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
3090 "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3091 orig_node->orig, client, BATADV_PRINT_VID(vid));
cc47f66e 3092
d69909d2 3093 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
f8214865 3094
122edaa0 3095 memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
c018ad3d 3096 tvlv_roam.vid = htons(vid);
122edaa0
ML
3097
3098 batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3099 orig_node->orig, BATADV_TVLV_ROAM, 1,
3100 &tvlv_roam, sizeof(tvlv_roam));
cc47f66e
AQ
3101
3102out:
122edaa0
ML
3103 if (primary_if)
3104 batadv_hardif_free_ref(primary_if);
a73105b8
AQ
3105}
3106
a513088d 3107static void batadv_tt_purge(struct work_struct *work)
a73105b8 3108{
56303d34 3109 struct delayed_work *delayed_work;
807736f6 3110 struct batadv_priv_tt *priv_tt;
56303d34
SE
3111 struct batadv_priv *bat_priv;
3112
3113 delayed_work = container_of(work, struct delayed_work, work);
807736f6
SE
3114 priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3115 bat_priv = container_of(priv_tt, struct batadv_priv, tt);
a73105b8 3116
a19d3d85 3117 batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
30cfd02b 3118 batadv_tt_global_purge(bat_priv);
a513088d
SE
3119 batadv_tt_req_purge(bat_priv);
3120 batadv_tt_roam_purge(bat_priv);
a73105b8 3121
72414442
AQ
3122 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3123 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
a73105b8 3124}
cc47f66e 3125
56303d34 3126void batadv_tt_free(struct batadv_priv *bat_priv)
cc47f66e 3127{
e1bf0c14
ML
3128 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3129 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3130
807736f6 3131 cancel_delayed_work_sync(&bat_priv->tt.work);
cc47f66e 3132
a513088d
SE
3133 batadv_tt_local_table_free(bat_priv);
3134 batadv_tt_global_table_free(bat_priv);
3135 batadv_tt_req_list_free(bat_priv);
3136 batadv_tt_changes_list_free(bat_priv);
3137 batadv_tt_roam_list_free(bat_priv);
cc47f66e 3138
807736f6 3139 kfree(bat_priv->tt.last_changeset);
cc47f66e 3140}
058d0e26 3141
7ea7b4a1
AQ
3142/**
3143 * batadv_tt_local_set_flags - set or unset the specified flags on the local
3144 * table and possibly count them in the TT size
3145 * @bat_priv: the bat priv with all the soft interface information
3146 * @flags: the flag to switch
3147 * @enable: whether to set or unset the flag
3148 * @count: whether to increase the TT size by the number of changed entries
9cfc7bd6 3149 */
7ea7b4a1
AQ
3150static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv,
3151 uint16_t flags, bool enable, bool count)
058d0e26 3152{
7ea7b4a1
AQ
3153 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3154 struct batadv_tt_common_entry *tt_common_entry;
697f2531 3155 uint16_t changed_num = 0;
058d0e26 3156 struct hlist_head *head;
7ea7b4a1 3157 uint32_t i;
058d0e26
AQ
3158
3159 if (!hash)
7ea7b4a1 3160 return;
058d0e26
AQ
3161
3162 for (i = 0; i < hash->size; i++) {
3163 head = &hash->table[i];
3164
3165 rcu_read_lock();
b67bfe0d 3166 hlist_for_each_entry_rcu(tt_common_entry,
058d0e26 3167 head, hash_entry) {
697f2531
AQ
3168 if (enable) {
3169 if ((tt_common_entry->flags & flags) == flags)
3170 continue;
3171 tt_common_entry->flags |= flags;
3172 } else {
3173 if (!(tt_common_entry->flags & flags))
3174 continue;
3175 tt_common_entry->flags &= ~flags;
3176 }
3177 changed_num++;
7ea7b4a1
AQ
3178
3179 if (!count)
3180 continue;
3181
3182 batadv_tt_local_size_inc(bat_priv,
3183 tt_common_entry->vid);
058d0e26
AQ
3184 }
3185 rcu_read_unlock();
3186 }
058d0e26
AQ
3187}
3188
acd34afa 3189/* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
56303d34 3190static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
058d0e26 3191{
807736f6 3192 struct batadv_hashtable *hash = bat_priv->tt.local_hash;
56303d34
SE
3193 struct batadv_tt_common_entry *tt_common;
3194 struct batadv_tt_local_entry *tt_local;
35df3b29 3195 struct batadv_softif_vlan *vlan;
b67bfe0d 3196 struct hlist_node *node_tmp;
058d0e26
AQ
3197 struct hlist_head *head;
3198 spinlock_t *list_lock; /* protects write access to the hash lists */
c90681b8 3199 uint32_t i;
058d0e26
AQ
3200
3201 if (!hash)
3202 return;
3203
3204 for (i = 0; i < hash->size; i++) {
3205 head = &hash->table[i];
3206 list_lock = &hash->list_locks[i];
3207
3208 spin_lock_bh(list_lock);
b67bfe0d 3209 hlist_for_each_entry_safe(tt_common, node_tmp, head,
acd34afa
SE
3210 hash_entry) {
3211 if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
058d0e26
AQ
3212 continue;
3213
39c75a51 3214 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
3215 "Deleting local tt entry (%pM, vid: %d): pending\n",
3216 tt_common->addr,
3217 BATADV_PRINT_VID(tt_common->vid));
058d0e26 3218
7ea7b4a1 3219 batadv_tt_local_size_dec(bat_priv, tt_common->vid);
b67bfe0d 3220 hlist_del_rcu(&tt_common->hash_entry);
56303d34
SE
3221 tt_local = container_of(tt_common,
3222 struct batadv_tt_local_entry,
3223 common);
35df3b29
AQ
3224
3225 /* decrease the reference held for this vlan */
3226 vlan = batadv_softif_vlan_get(bat_priv, tt_common->vid);
354136bc
ML
3227 if (vlan) {
3228 batadv_softif_vlan_free_ref(vlan);
3229 batadv_softif_vlan_free_ref(vlan);
3230 }
35df3b29 3231
56303d34 3232 batadv_tt_local_entry_free_ref(tt_local);
058d0e26
AQ
3233 }
3234 spin_unlock_bh(list_lock);
3235 }
058d0e26
AQ
3236}
3237
e1bf0c14 3238/**
a19d3d85
ML
3239 * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3240 * which have been queued in the time since the last commit
e1bf0c14 3241 * @bat_priv: the bat priv with all the soft interface information
a19d3d85
ML
3242 *
3243 * Caller must hold tt->commit_lock.
e1bf0c14 3244 */
a19d3d85 3245static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
058d0e26 3246{
c5caf4ef
LL
3247 /* Update multicast addresses in local translation table */
3248 batadv_mcast_mla_update(bat_priv);
3249
e1bf0c14
ML
3250 if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3251 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3252 batadv_tt_tvlv_container_update(bat_priv);
a19d3d85 3253 return;
e1bf0c14 3254 }
be9aa4c1 3255
7ea7b4a1 3256 batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
be9aa4c1 3257
a513088d 3258 batadv_tt_local_purge_pending_clients(bat_priv);
7ea7b4a1 3259 batadv_tt_local_update_crc(bat_priv);
058d0e26
AQ
3260
3261 /* Increment the TTVN only once per OGM interval */
807736f6 3262 atomic_inc(&bat_priv->tt.vn);
39c75a51 3263 batadv_dbg(BATADV_DBG_TT, bat_priv,
1eda58bf 3264 "Local changes committed, updating to ttvn %u\n",
807736f6 3265 (uint8_t)atomic_read(&bat_priv->tt.vn));
be9aa4c1
ML
3266
3267 /* reset the sending counter */
807736f6 3268 atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
e1bf0c14 3269 batadv_tt_tvlv_container_update(bat_priv);
a19d3d85 3270}
a70a9aa9 3271
a19d3d85
ML
3272/**
3273 * batadv_tt_local_commit_changes - commit all pending local tt changes which
3274 * have been queued in the time since the last commit
3275 * @bat_priv: the bat priv with all the soft interface information
3276 */
3277void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3278{
3279 spin_lock_bh(&bat_priv->tt.commit_lock);
3280 batadv_tt_local_commit_changes_nolock(bat_priv);
a70a9aa9 3281 spin_unlock_bh(&bat_priv->tt.commit_lock);
058d0e26 3282}
59b699cd 3283
56303d34 3284bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
b8cbd81d 3285 uint8_t *dst, unsigned short vid)
59b699cd 3286{
56303d34
SE
3287 struct batadv_tt_local_entry *tt_local_entry = NULL;
3288 struct batadv_tt_global_entry *tt_global_entry = NULL;
b8cbd81d 3289 struct batadv_softif_vlan *vlan;
5870adc6 3290 bool ret = false;
59b699cd 3291
b8cbd81d
AQ
3292 vlan = batadv_softif_vlan_get(bat_priv, vid);
3293 if (!vlan || !atomic_read(&vlan->ap_isolation))
5870adc6 3294 goto out;
59b699cd 3295
b8cbd81d 3296 tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
59b699cd
AQ
3297 if (!tt_local_entry)
3298 goto out;
3299
b8cbd81d 3300 tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
59b699cd
AQ
3301 if (!tt_global_entry)
3302 goto out;
3303
1f129fef 3304 if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
59b699cd
AQ
3305 goto out;
3306
5870adc6 3307 ret = true;
59b699cd
AQ
3308
3309out:
b8cbd81d
AQ
3310 if (vlan)
3311 batadv_softif_vlan_free_ref(vlan);
59b699cd 3312 if (tt_global_entry)
a513088d 3313 batadv_tt_global_entry_free_ref(tt_global_entry);
59b699cd 3314 if (tt_local_entry)
a513088d 3315 batadv_tt_local_entry_free_ref(tt_local_entry);
59b699cd
AQ
3316 return ret;
3317}
a943cac1 3318
e1bf0c14
ML
3319/**
3320 * batadv_tt_update_orig - update global translation table with new tt
3321 * information received via ogms
3322 * @bat_priv: the bat priv with all the soft interface information
3323 * @orig: the orig_node of the ogm
7ea7b4a1
AQ
3324 * @tt_vlan: pointer to the first tvlv VLAN entry
3325 * @tt_num_vlan: number of tvlv VLAN entries
3326 * @tt_change: pointer to the first entry in the TT buffer
e1bf0c14
ML
3327 * @tt_num_changes: number of tt changes inside the tt buffer
3328 * @ttvn: translation table version number of this changeset
ced72933 3329 * @tt_crc: crc32 checksum of orig node's translation table
e1bf0c14
ML
3330 */
3331static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3332 struct batadv_orig_node *orig_node,
7ea7b4a1
AQ
3333 const void *tt_buff, uint16_t tt_num_vlan,
3334 struct batadv_tvlv_tt_change *tt_change,
3335 uint16_t tt_num_changes, uint8_t ttvn)
a943cac1
ML
3336{
3337 uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
7ea7b4a1 3338 struct batadv_tvlv_tt_vlan_data *tt_vlan;
a943cac1 3339 bool full_table = true;
e17931d1 3340 bool has_tt_init;
a943cac1 3341
7ea7b4a1 3342 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
e17931d1
LL
3343 has_tt_init = orig_node->capa_initialized & BATADV_ORIG_CAPA_HAS_TT;
3344
17071578 3345 /* orig table not initialised AND first diff is in the OGM OR the ttvn
9cfc7bd6
SE
3346 * increased by one -> we can apply the attached changes
3347 */
e17931d1 3348 if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
a943cac1 3349 /* the OGM could not contain the changes due to their size or
42d0b044
SE
3350 * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3351 * times.
9cfc7bd6
SE
3352 * In this case send a tt request
3353 */
a943cac1
ML
3354 if (!tt_num_changes) {
3355 full_table = false;
3356 goto request_table;
3357 }
3358
a70a9aa9
AQ
3359 spin_lock_bh(&orig_node->tt_lock);
3360
a513088d 3361 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
96412690 3362 ttvn, tt_change);
a943cac1
ML
3363
3364 /* Even if we received the precomputed crc with the OGM, we
3365 * prefer to recompute it to spot any possible inconsistency
9cfc7bd6
SE
3366 * in the global table
3367 */
7ea7b4a1 3368 batadv_tt_global_update_crc(bat_priv, orig_node);
a943cac1 3369
a70a9aa9
AQ
3370 spin_unlock_bh(&orig_node->tt_lock);
3371
a943cac1
ML
3372 /* The ttvn alone is not enough to guarantee consistency
3373 * because a single value could represent different states
3374 * (due to the wrap around). Thus a node has to check whether
3375 * the resulting table (after applying the changes) is still
3376 * consistent or not. E.g. a node could disconnect while its
3377 * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3378 * checking the CRC value is mandatory to detect the
9cfc7bd6
SE
3379 * inconsistency
3380 */
7ea7b4a1
AQ
3381 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3382 tt_num_vlan))
a943cac1 3383 goto request_table;
a943cac1
ML
3384 } else {
3385 /* if we missed more than one change or our tables are not
9cfc7bd6
SE
3386 * in sync anymore -> request fresh tt data
3387 */
e17931d1 3388 if (!has_tt_init || ttvn != orig_ttvn ||
7ea7b4a1
AQ
3389 !batadv_tt_global_check_crc(orig_node, tt_vlan,
3390 tt_num_vlan)) {
a943cac1 3391request_table:
39c75a51 3392 batadv_dbg(BATADV_DBG_TT, bat_priv,
7ea7b4a1
AQ
3393 "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3394 orig_node->orig, ttvn, orig_ttvn,
3395 tt_num_changes);
a513088d 3396 batadv_send_tt_request(bat_priv, orig_node, ttvn,
7ea7b4a1
AQ
3397 tt_vlan, tt_num_vlan,
3398 full_table);
a943cac1
ML
3399 return;
3400 }
3401 }
3402}
3275e7cc 3403
c018ad3d
AQ
3404/**
3405 * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3406 * @bat_priv: the bat priv with all the soft interface information
3407 * @addr: the mac address of the client to check
3408 * @vid: VLAN identifier
3409 *
3410 * Returns true if we know that the client has moved from its old originator
3411 * to another one. This entry is still kept for consistency purposes and will be
3412 * deleted later by a DEL or because of timeout
3275e7cc 3413 */
56303d34 3414bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
c018ad3d 3415 uint8_t *addr, unsigned short vid)
3275e7cc 3416{
56303d34 3417 struct batadv_tt_global_entry *tt_global_entry;
3275e7cc
AQ
3418 bool ret = false;
3419
c018ad3d 3420 tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3275e7cc
AQ
3421 if (!tt_global_entry)
3422 goto out;
3423
c1d07431 3424 ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
a513088d 3425 batadv_tt_global_entry_free_ref(tt_global_entry);
3275e7cc
AQ
3426out:
3427 return ret;
3428}
30cfd02b 3429
7c1fd91d
AQ
3430/**
3431 * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3432 * @bat_priv: the bat priv with all the soft interface information
c018ad3d
AQ
3433 * @addr: the mac address of the local client to query
3434 * @vid: VLAN identifier
7c1fd91d
AQ
3435 *
3436 * Returns true if the local client is known to be roaming (it is not served by
3437 * this node anymore) or not. If yes, the client is still present in the table
3438 * to keep the latter consistent with the node TTVN
3439 */
3440bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
c018ad3d 3441 uint8_t *addr, unsigned short vid)
7c1fd91d
AQ
3442{
3443 struct batadv_tt_local_entry *tt_local_entry;
3444 bool ret = false;
3445
c018ad3d 3446 tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
7c1fd91d
AQ
3447 if (!tt_local_entry)
3448 goto out;
3449
3450 ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3451 batadv_tt_local_entry_free_ref(tt_local_entry);
3452out:
3453 return ret;
7c1fd91d
AQ
3454}
3455
30cfd02b
AQ
3456bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3457 struct batadv_orig_node *orig_node,
c018ad3d 3458 const unsigned char *addr,
16052789 3459 unsigned short vid)
30cfd02b
AQ
3460{
3461 bool ret = false;
3462
16052789 3463 if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
30cfd02b
AQ
3464 BATADV_TT_CLIENT_TEMP,
3465 atomic_read(&orig_node->last_ttvn)))
3466 goto out;
3467
3468 batadv_dbg(BATADV_DBG_TT, bat_priv,
16052789
AQ
3469 "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3470 addr, BATADV_PRINT_VID(vid), orig_node->orig);
30cfd02b
AQ
3471 ret = true;
3472out:
3473 return ret;
3474}
e1bf0c14 3475
a19d3d85
ML
3476/**
3477 * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3478 * maximum packet size that can be transported through the mesh
3479 * @soft_iface: netdev struct of the mesh interface
3480 *
3481 * Remove entries older than 'timeout' and half timeout if more entries need
3482 * to be removed.
3483 */
3484void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3485{
3486 struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3487 int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3488 int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3489 bool reduced = false;
3490
3491 spin_lock_bh(&bat_priv->tt.commit_lock);
3492
3493 while (true) {
3494 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3495 if (packet_size_max >= table_size)
3496 break;
3497
3498 batadv_tt_local_purge(bat_priv, timeout);
3499 batadv_tt_local_purge_pending_clients(bat_priv);
3500
3501 timeout /= 2;
3502 reduced = true;
3503 net_ratelimited_function(batadv_info, soft_iface,
3504 "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3505 packet_size_max);
3506 }
3507
3508 /* commit these changes immediately, to avoid synchronization problem
3509 * with the TTVN
3510 */
3511 if (reduced)
3512 batadv_tt_local_commit_changes_nolock(bat_priv);
3513
3514 spin_unlock_bh(&bat_priv->tt.commit_lock);
3515}
3516
e1bf0c14
ML
3517/**
3518 * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3519 * @bat_priv: the bat priv with all the soft interface information
3520 * @orig: the orig_node of the ogm
3521 * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3522 * @tvlv_value: tvlv buffer containing the gateway data
3523 * @tvlv_value_len: tvlv buffer length
3524 */
3525static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3526 struct batadv_orig_node *orig,
7ea7b4a1 3527 uint8_t flags, void *tvlv_value,
e1bf0c14
ML
3528 uint16_t tvlv_value_len)
3529{
7ea7b4a1
AQ
3530 struct batadv_tvlv_tt_vlan_data *tt_vlan;
3531 struct batadv_tvlv_tt_change *tt_change;
e1bf0c14 3532 struct batadv_tvlv_tt_data *tt_data;
7ea7b4a1 3533 uint16_t num_entries, num_vlan;
e1bf0c14
ML
3534
3535 if (tvlv_value_len < sizeof(*tt_data))
3536 return;
3537
3538 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3539 tvlv_value_len -= sizeof(*tt_data);
3540
7ea7b4a1
AQ
3541 num_vlan = ntohs(tt_data->num_vlan);
3542
3543 if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3544 return;
3545
3546 tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3547 tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3548 tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3549
298e6e68 3550 num_entries = batadv_tt_entries(tvlv_value_len);
e1bf0c14 3551
7ea7b4a1
AQ
3552 batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3553 num_entries, tt_data->ttvn);
e1bf0c14
ML
3554}
3555
335fbe0f
ML
3556/**
3557 * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3558 * container
3559 * @bat_priv: the bat priv with all the soft interface information
3560 * @src: mac address of tt tvlv sender
3561 * @dst: mac address of tt tvlv recipient
3562 * @tvlv_value: tvlv buffer containing the tt data
3563 * @tvlv_value_len: tvlv buffer length
3564 *
3565 * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3566 * otherwise.
3567 */
3568static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3569 uint8_t *src, uint8_t *dst,
3570 void *tvlv_value,
3571 uint16_t tvlv_value_len)
3572{
3573 struct batadv_tvlv_tt_data *tt_data;
7ea7b4a1 3574 uint16_t tt_vlan_len, tt_num_entries;
335fbe0f
ML
3575 char tt_flag;
3576 bool ret;
3577
3578 if (tvlv_value_len < sizeof(*tt_data))
3579 return NET_RX_SUCCESS;
3580
3581 tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3582 tvlv_value_len -= sizeof(*tt_data);
3583
7ea7b4a1
AQ
3584 tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3585 tt_vlan_len *= ntohs(tt_data->num_vlan);
3586
3587 if (tvlv_value_len < tt_vlan_len)
3588 return NET_RX_SUCCESS;
3589
3590 tvlv_value_len -= tt_vlan_len;
3591 tt_num_entries = batadv_tt_entries(tvlv_value_len);
335fbe0f
ML
3592
3593 switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3594 case BATADV_TT_REQUEST:
3595 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3596
3597 /* If this node cannot provide a TT response the tt_request is
3598 * forwarded
3599 */
3600 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3601 if (!ret) {
3602 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3603 tt_flag = 'F';
3604 else
3605 tt_flag = '.';
3606
3607 batadv_dbg(BATADV_DBG_TT, bat_priv,
3608 "Routing TT_REQUEST to %pM [%c]\n",
3609 dst, tt_flag);
3610 /* tvlv API will re-route the packet */
3611 return NET_RX_DROP;
3612 }
3613 break;
3614 case BATADV_TT_RESPONSE:
3615 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3616
3617 if (batadv_is_my_mac(bat_priv, dst)) {
3618 batadv_handle_tt_response(bat_priv, tt_data,
7ea7b4a1 3619 src, tt_num_entries);
335fbe0f
ML
3620 return NET_RX_SUCCESS;
3621 }
3622
3623 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3624 tt_flag = 'F';
3625 else
3626 tt_flag = '.';
3627
3628 batadv_dbg(BATADV_DBG_TT, bat_priv,
3629 "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3630
3631 /* tvlv API will re-route the packet */
3632 return NET_RX_DROP;
3633 }
3634
3635 return NET_RX_SUCCESS;
3636}
3637
122edaa0
ML
3638/**
3639 * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3640 * @bat_priv: the bat priv with all the soft interface information
3641 * @src: mac address of tt tvlv sender
3642 * @dst: mac address of tt tvlv recipient
3643 * @tvlv_value: tvlv buffer containing the tt data
3644 * @tvlv_value_len: tvlv buffer length
3645 *
3646 * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3647 * otherwise.
3648 */
3649static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3650 uint8_t *src, uint8_t *dst,
3651 void *tvlv_value,
3652 uint16_t tvlv_value_len)
3653{
3654 struct batadv_tvlv_roam_adv *roaming_adv;
3655 struct batadv_orig_node *orig_node = NULL;
3656
3657 /* If this node is not the intended recipient of the
3658 * roaming advertisement the packet is forwarded
3659 * (the tvlv API will re-route the packet).
3660 */
3661 if (!batadv_is_my_mac(bat_priv, dst))
3662 return NET_RX_DROP;
3663
122edaa0
ML
3664 if (tvlv_value_len < sizeof(*roaming_adv))
3665 goto out;
3666
3667 orig_node = batadv_orig_hash_find(bat_priv, src);
3668 if (!orig_node)
3669 goto out;
3670
3671 batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3672 roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3673
3674 batadv_dbg(BATADV_DBG_TT, bat_priv,
3675 "Received ROAMING_ADV from %pM (client %pM)\n",
3676 src, roaming_adv->client);
3677
3678 batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
c018ad3d 3679 ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
122edaa0
ML
3680 atomic_read(&orig_node->last_ttvn) + 1);
3681
3682out:
3683 if (orig_node)
3684 batadv_orig_node_free_ref(orig_node);
3685 return NET_RX_SUCCESS;
3686}
3687
e1bf0c14
ML
3688/**
3689 * batadv_tt_init - initialise the translation table internals
3690 * @bat_priv: the bat priv with all the soft interface information
3691 *
3692 * Return 0 on success or negative error number in case of failure.
3693 */
3694int batadv_tt_init(struct batadv_priv *bat_priv)
3695{
3696 int ret;
3697
0eb01568
AQ
3698 /* synchronized flags must be remote */
3699 BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3700
e1bf0c14
ML
3701 ret = batadv_tt_local_init(bat_priv);
3702 if (ret < 0)
3703 return ret;
3704
3705 ret = batadv_tt_global_init(bat_priv);
3706 if (ret < 0)
3707 return ret;
3708
3709 batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
335fbe0f
ML
3710 batadv_tt_tvlv_unicast_handler_v1,
3711 BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
e1bf0c14 3712
122edaa0
ML
3713 batadv_tvlv_handler_register(bat_priv, NULL,
3714 batadv_roam_tvlv_unicast_handler_v1,
3715 BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3716
e1bf0c14
ML
3717 INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3718 queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3719 msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3720
3721 return 1;
3722}
42cb0bef
AQ
3723
3724/**
3725 * batadv_tt_global_is_isolated - check if a client is marked as isolated
3726 * @bat_priv: the bat priv with all the soft interface information
3727 * @addr: the mac address of the client
3728 * @vid: the identifier of the VLAN where this client is connected
3729 *
3730 * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3731 * otherwise
3732 */
3733bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3734 const uint8_t *addr, unsigned short vid)
3735{
3736 struct batadv_tt_global_entry *tt;
3737 bool ret;
3738
3739 tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3740 if (!tt)
3741 return false;
3742
3743 ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3744
3745 batadv_tt_global_entry_free_ref(tt);
3746
3747 return ret;
3748}
This page took 0.508093 seconds and 5 git commands to generate.