Merge branch 'x86-entry-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[sfrench/cifs-2.6.git] / net / batman-adv / translation-table.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2019  B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich, Antonio Quartulli
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include "translation-table.h"
20 #include "main.h"
21
22 #include <linux/atomic.h>
23 #include <linux/bitops.h>
24 #include <linux/build_bug.h>
25 #include <linux/byteorder/generic.h>
26 #include <linux/cache.h>
27 #include <linux/compiler.h>
28 #include <linux/crc32c.h>
29 #include <linux/errno.h>
30 #include <linux/etherdevice.h>
31 #include <linux/gfp.h>
32 #include <linux/if_ether.h>
33 #include <linux/init.h>
34 #include <linux/jhash.h>
35 #include <linux/jiffies.h>
36 #include <linux/kernel.h>
37 #include <linux/kref.h>
38 #include <linux/list.h>
39 #include <linux/lockdep.h>
40 #include <linux/net.h>
41 #include <linux/netdevice.h>
42 #include <linux/netlink.h>
43 #include <linux/rculist.h>
44 #include <linux/rcupdate.h>
45 #include <linux/seq_file.h>
46 #include <linux/skbuff.h>
47 #include <linux/slab.h>
48 #include <linux/spinlock.h>
49 #include <linux/stddef.h>
50 #include <linux/string.h>
51 #include <linux/workqueue.h>
52 #include <net/genetlink.h>
53 #include <net/netlink.h>
54 #include <net/sock.h>
55 #include <uapi/linux/batadv_packet.h>
56 #include <uapi/linux/batman_adv.h>
57
58 #include "bridge_loop_avoidance.h"
59 #include "hard-interface.h"
60 #include "hash.h"
61 #include "log.h"
62 #include "netlink.h"
63 #include "originator.h"
64 #include "soft-interface.h"
65 #include "tvlv.h"
66
67 static struct kmem_cache *batadv_tl_cache __read_mostly;
68 static struct kmem_cache *batadv_tg_cache __read_mostly;
69 static struct kmem_cache *batadv_tt_orig_cache __read_mostly;
70 static struct kmem_cache *batadv_tt_change_cache __read_mostly;
71 static struct kmem_cache *batadv_tt_req_cache __read_mostly;
72 static struct kmem_cache *batadv_tt_roam_cache __read_mostly;
73
74 /* hash class keys */
75 static struct lock_class_key batadv_tt_local_hash_lock_class_key;
76 static struct lock_class_key batadv_tt_global_hash_lock_class_key;
77
78 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
79                                  unsigned short vid,
80                                  struct batadv_orig_node *orig_node);
81 static void batadv_tt_purge(struct work_struct *work);
82 static void
83 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
84 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
85                                  struct batadv_orig_node *orig_node,
86                                  const unsigned char *addr,
87                                  unsigned short vid, const char *message,
88                                  bool roaming);
89
90 /**
91  * batadv_compare_tt() - check if two TT entries are the same
92  * @node: the list element pointer of the first TT entry
93  * @data2: pointer to the tt_common_entry of the second TT entry
94  *
95  * Compare the MAC address and the VLAN ID of the two TT entries and check if
96  * they are the same TT client.
97  * Return: true if the two TT clients are the same, false otherwise
98  */
99 static bool batadv_compare_tt(const struct hlist_node *node, const void *data2)
100 {
101         const void *data1 = container_of(node, struct batadv_tt_common_entry,
102                                          hash_entry);
103         const struct batadv_tt_common_entry *tt1 = data1;
104         const struct batadv_tt_common_entry *tt2 = data2;
105
106         return (tt1->vid == tt2->vid) && batadv_compare_eth(data1, data2);
107 }
108
109 /**
110  * batadv_choose_tt() - return the index of the tt entry in the hash table
111  * @data: pointer to the tt_common_entry object to map
112  * @size: the size of the hash table
113  *
114  * Return: the hash index where the object represented by 'data' should be
115  * stored at.
116  */
117 static inline u32 batadv_choose_tt(const void *data, u32 size)
118 {
119         struct batadv_tt_common_entry *tt;
120         u32 hash = 0;
121
122         tt = (struct batadv_tt_common_entry *)data;
123         hash = jhash(&tt->addr, ETH_ALEN, hash);
124         hash = jhash(&tt->vid, sizeof(tt->vid), hash);
125
126         return hash % size;
127 }
128
129 /**
130  * batadv_tt_hash_find() - look for a client in the given hash table
131  * @hash: the hash table to search
132  * @addr: the mac address of the client to look for
133  * @vid: VLAN identifier
134  *
135  * Return: a pointer to the tt_common struct belonging to the searched client if
136  * found, NULL otherwise.
137  */
138 static struct batadv_tt_common_entry *
139 batadv_tt_hash_find(struct batadv_hashtable *hash, const u8 *addr,
140                     unsigned short vid)
141 {
142         struct hlist_head *head;
143         struct batadv_tt_common_entry to_search, *tt, *tt_tmp = NULL;
144         u32 index;
145
146         if (!hash)
147                 return NULL;
148
149         ether_addr_copy(to_search.addr, addr);
150         to_search.vid = vid;
151
152         index = batadv_choose_tt(&to_search, hash->size);
153         head = &hash->table[index];
154
155         rcu_read_lock();
156         hlist_for_each_entry_rcu(tt, head, hash_entry) {
157                 if (!batadv_compare_eth(tt, addr))
158                         continue;
159
160                 if (tt->vid != vid)
161                         continue;
162
163                 if (!kref_get_unless_zero(&tt->refcount))
164                         continue;
165
166                 tt_tmp = tt;
167                 break;
168         }
169         rcu_read_unlock();
170
171         return tt_tmp;
172 }
173
174 /**
175  * batadv_tt_local_hash_find() - search the local table for a given client
176  * @bat_priv: the bat priv with all the soft interface information
177  * @addr: the mac address of the client to look for
178  * @vid: VLAN identifier
179  *
180  * Return: a pointer to the corresponding tt_local_entry struct if the client is
181  * found, NULL otherwise.
182  */
183 static struct batadv_tt_local_entry *
184 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
185                           unsigned short vid)
186 {
187         struct batadv_tt_common_entry *tt_common_entry;
188         struct batadv_tt_local_entry *tt_local_entry = NULL;
189
190         tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, addr,
191                                               vid);
192         if (tt_common_entry)
193                 tt_local_entry = container_of(tt_common_entry,
194                                               struct batadv_tt_local_entry,
195                                               common);
196         return tt_local_entry;
197 }
198
199 /**
200  * batadv_tt_global_hash_find() - search the global table for a given client
201  * @bat_priv: the bat priv with all the soft interface information
202  * @addr: the mac address of the client to look for
203  * @vid: VLAN identifier
204  *
205  * Return: a pointer to the corresponding tt_global_entry struct if the client
206  * is found, NULL otherwise.
207  */
208 static struct batadv_tt_global_entry *
209 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const u8 *addr,
210                            unsigned short vid)
211 {
212         struct batadv_tt_common_entry *tt_common_entry;
213         struct batadv_tt_global_entry *tt_global_entry = NULL;
214
215         tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, addr,
216                                               vid);
217         if (tt_common_entry)
218                 tt_global_entry = container_of(tt_common_entry,
219                                                struct batadv_tt_global_entry,
220                                                common);
221         return tt_global_entry;
222 }
223
224 /**
225  * batadv_tt_local_entry_free_rcu() - free the tt_local_entry
226  * @rcu: rcu pointer of the tt_local_entry
227  */
228 static void batadv_tt_local_entry_free_rcu(struct rcu_head *rcu)
229 {
230         struct batadv_tt_local_entry *tt_local_entry;
231
232         tt_local_entry = container_of(rcu, struct batadv_tt_local_entry,
233                                       common.rcu);
234
235         kmem_cache_free(batadv_tl_cache, tt_local_entry);
236 }
237
238 /**
239  * batadv_tt_local_entry_release() - release tt_local_entry from lists and queue
240  *  for free after rcu grace period
241  * @ref: kref pointer of the nc_node
242  */
243 static void batadv_tt_local_entry_release(struct kref *ref)
244 {
245         struct batadv_tt_local_entry *tt_local_entry;
246
247         tt_local_entry = container_of(ref, struct batadv_tt_local_entry,
248                                       common.refcount);
249
250         batadv_softif_vlan_put(tt_local_entry->vlan);
251
252         call_rcu(&tt_local_entry->common.rcu, batadv_tt_local_entry_free_rcu);
253 }
254
255 /**
256  * batadv_tt_local_entry_put() - decrement the tt_local_entry refcounter and
257  *  possibly release it
258  * @tt_local_entry: tt_local_entry to be free'd
259  */
260 static void
261 batadv_tt_local_entry_put(struct batadv_tt_local_entry *tt_local_entry)
262 {
263         kref_put(&tt_local_entry->common.refcount,
264                  batadv_tt_local_entry_release);
265 }
266
267 /**
268  * batadv_tt_global_entry_free_rcu() - free the tt_global_entry
269  * @rcu: rcu pointer of the tt_global_entry
270  */
271 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
272 {
273         struct batadv_tt_global_entry *tt_global_entry;
274
275         tt_global_entry = container_of(rcu, struct batadv_tt_global_entry,
276                                        common.rcu);
277
278         kmem_cache_free(batadv_tg_cache, tt_global_entry);
279 }
280
281 /**
282  * batadv_tt_global_entry_release() - release tt_global_entry from lists and
283  *  queue for free after rcu grace period
284  * @ref: kref pointer of the nc_node
285  */
286 static void batadv_tt_global_entry_release(struct kref *ref)
287 {
288         struct batadv_tt_global_entry *tt_global_entry;
289
290         tt_global_entry = container_of(ref, struct batadv_tt_global_entry,
291                                        common.refcount);
292
293         batadv_tt_global_del_orig_list(tt_global_entry);
294
295         call_rcu(&tt_global_entry->common.rcu, batadv_tt_global_entry_free_rcu);
296 }
297
298 /**
299  * batadv_tt_global_entry_put() - decrement the tt_global_entry refcounter and
300  *  possibly release it
301  * @tt_global_entry: tt_global_entry to be free'd
302  */
303 static void
304 batadv_tt_global_entry_put(struct batadv_tt_global_entry *tt_global_entry)
305 {
306         kref_put(&tt_global_entry->common.refcount,
307                  batadv_tt_global_entry_release);
308 }
309
310 /**
311  * batadv_tt_global_hash_count() - count the number of orig entries
312  * @bat_priv: the bat priv with all the soft interface information
313  * @addr: the mac address of the client to count entries for
314  * @vid: VLAN identifier
315  *
316  * Return: the number of originators advertising the given address/data
317  * (excluding ourself).
318  */
319 int batadv_tt_global_hash_count(struct batadv_priv *bat_priv,
320                                 const u8 *addr, unsigned short vid)
321 {
322         struct batadv_tt_global_entry *tt_global_entry;
323         int count;
324
325         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
326         if (!tt_global_entry)
327                 return 0;
328
329         count = atomic_read(&tt_global_entry->orig_list_count);
330         batadv_tt_global_entry_put(tt_global_entry);
331
332         return count;
333 }
334
335 /**
336  * batadv_tt_local_size_mod() - change the size by v of the local table
337  *  identified by vid
338  * @bat_priv: the bat priv with all the soft interface information
339  * @vid: the VLAN identifier of the sub-table to change
340  * @v: the amount to sum to the local table size
341  */
342 static void batadv_tt_local_size_mod(struct batadv_priv *bat_priv,
343                                      unsigned short vid, int v)
344 {
345         struct batadv_softif_vlan *vlan;
346
347         vlan = batadv_softif_vlan_get(bat_priv, vid);
348         if (!vlan)
349                 return;
350
351         atomic_add(v, &vlan->tt.num_entries);
352
353         batadv_softif_vlan_put(vlan);
354 }
355
356 /**
357  * batadv_tt_local_size_inc() - increase by one the local table size for the
358  *  given vid
359  * @bat_priv: the bat priv with all the soft interface information
360  * @vid: the VLAN identifier
361  */
362 static void batadv_tt_local_size_inc(struct batadv_priv *bat_priv,
363                                      unsigned short vid)
364 {
365         batadv_tt_local_size_mod(bat_priv, vid, 1);
366 }
367
368 /**
369  * batadv_tt_local_size_dec() - decrease by one the local table size for the
370  *  given vid
371  * @bat_priv: the bat priv with all the soft interface information
372  * @vid: the VLAN identifier
373  */
374 static void batadv_tt_local_size_dec(struct batadv_priv *bat_priv,
375                                      unsigned short vid)
376 {
377         batadv_tt_local_size_mod(bat_priv, vid, -1);
378 }
379
380 /**
381  * batadv_tt_global_size_mod() - change the size by v of the global table
382  *  for orig_node identified by vid
383  * @orig_node: the originator for which the table has to be modified
384  * @vid: the VLAN identifier
385  * @v: the amount to sum to the global table size
386  */
387 static void batadv_tt_global_size_mod(struct batadv_orig_node *orig_node,
388                                       unsigned short vid, int v)
389 {
390         struct batadv_orig_node_vlan *vlan;
391
392         vlan = batadv_orig_node_vlan_new(orig_node, vid);
393         if (!vlan)
394                 return;
395
396         if (atomic_add_return(v, &vlan->tt.num_entries) == 0) {
397                 spin_lock_bh(&orig_node->vlan_list_lock);
398                 if (!hlist_unhashed(&vlan->list)) {
399                         hlist_del_init_rcu(&vlan->list);
400                         batadv_orig_node_vlan_put(vlan);
401                 }
402                 spin_unlock_bh(&orig_node->vlan_list_lock);
403         }
404
405         batadv_orig_node_vlan_put(vlan);
406 }
407
408 /**
409  * batadv_tt_global_size_inc() - increase by one the global table size for the
410  *  given vid
411  * @orig_node: the originator which global table size has to be decreased
412  * @vid: the vlan identifier
413  */
414 static void batadv_tt_global_size_inc(struct batadv_orig_node *orig_node,
415                                       unsigned short vid)
416 {
417         batadv_tt_global_size_mod(orig_node, vid, 1);
418 }
419
420 /**
421  * batadv_tt_global_size_dec() - decrease by one the global table size for the
422  *  given vid
423  * @orig_node: the originator which global table size has to be decreased
424  * @vid: the vlan identifier
425  */
426 static void batadv_tt_global_size_dec(struct batadv_orig_node *orig_node,
427                                       unsigned short vid)
428 {
429         batadv_tt_global_size_mod(orig_node, vid, -1);
430 }
431
432 /**
433  * batadv_tt_orig_list_entry_free_rcu() - free the orig_entry
434  * @rcu: rcu pointer of the orig_entry
435  */
436 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
437 {
438         struct batadv_tt_orig_list_entry *orig_entry;
439
440         orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
441
442         kmem_cache_free(batadv_tt_orig_cache, orig_entry);
443 }
444
445 /**
446  * batadv_tt_orig_list_entry_release() - release tt orig entry from lists and
447  *  queue for free after rcu grace period
448  * @ref: kref pointer of the tt orig entry
449  */
450 static void batadv_tt_orig_list_entry_release(struct kref *ref)
451 {
452         struct batadv_tt_orig_list_entry *orig_entry;
453
454         orig_entry = container_of(ref, struct batadv_tt_orig_list_entry,
455                                   refcount);
456
457         batadv_orig_node_put(orig_entry->orig_node);
458         call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
459 }
460
461 /**
462  * batadv_tt_orig_list_entry_put() - decrement the tt orig entry refcounter and
463  *  possibly release it
464  * @orig_entry: tt orig entry to be free'd
465  */
466 static void
467 batadv_tt_orig_list_entry_put(struct batadv_tt_orig_list_entry *orig_entry)
468 {
469         kref_put(&orig_entry->refcount, batadv_tt_orig_list_entry_release);
470 }
471
472 /**
473  * batadv_tt_local_event() - store a local TT event (ADD/DEL)
474  * @bat_priv: the bat priv with all the soft interface information
475  * @tt_local_entry: the TT entry involved in the event
476  * @event_flags: flags to store in the event structure
477  */
478 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
479                                   struct batadv_tt_local_entry *tt_local_entry,
480                                   u8 event_flags)
481 {
482         struct batadv_tt_change_node *tt_change_node, *entry, *safe;
483         struct batadv_tt_common_entry *common = &tt_local_entry->common;
484         u8 flags = common->flags | event_flags;
485         bool event_removed = false;
486         bool del_op_requested, del_op_entry;
487
488         tt_change_node = kmem_cache_alloc(batadv_tt_change_cache, GFP_ATOMIC);
489         if (!tt_change_node)
490                 return;
491
492         tt_change_node->change.flags = flags;
493         memset(tt_change_node->change.reserved, 0,
494                sizeof(tt_change_node->change.reserved));
495         ether_addr_copy(tt_change_node->change.addr, common->addr);
496         tt_change_node->change.vid = htons(common->vid);
497
498         del_op_requested = flags & BATADV_TT_CLIENT_DEL;
499
500         /* check for ADD+DEL or DEL+ADD events */
501         spin_lock_bh(&bat_priv->tt.changes_list_lock);
502         list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
503                                  list) {
504                 if (!batadv_compare_eth(entry->change.addr, common->addr))
505                         continue;
506
507                 /* DEL+ADD in the same orig interval have no effect and can be
508                  * removed to avoid silly behaviour on the receiver side. The
509                  * other way around (ADD+DEL) can happen in case of roaming of
510                  * a client still in the NEW state. Roaming of NEW clients is
511                  * now possible due to automatically recognition of "temporary"
512                  * clients
513                  */
514                 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
515                 if (!del_op_requested && del_op_entry)
516                         goto del;
517                 if (del_op_requested && !del_op_entry)
518                         goto del;
519
520                 /* this is a second add in the same originator interval. It
521                  * means that flags have been changed: update them!
522                  */
523                 if (!del_op_requested && !del_op_entry)
524                         entry->change.flags = flags;
525
526                 continue;
527 del:
528                 list_del(&entry->list);
529                 kmem_cache_free(batadv_tt_change_cache, entry);
530                 kmem_cache_free(batadv_tt_change_cache, tt_change_node);
531                 event_removed = true;
532                 goto unlock;
533         }
534
535         /* track the change in the OGMinterval list */
536         list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
537
538 unlock:
539         spin_unlock_bh(&bat_priv->tt.changes_list_lock);
540
541         if (event_removed)
542                 atomic_dec(&bat_priv->tt.local_changes);
543         else
544                 atomic_inc(&bat_priv->tt.local_changes);
545 }
546
547 /**
548  * batadv_tt_len() - compute length in bytes of given number of tt changes
549  * @changes_num: number of tt changes
550  *
551  * Return: computed length in bytes.
552  */
553 static int batadv_tt_len(int changes_num)
554 {
555         return changes_num * sizeof(struct batadv_tvlv_tt_change);
556 }
557
558 /**
559  * batadv_tt_entries() - compute the number of entries fitting in tt_len bytes
560  * @tt_len: available space
561  *
562  * Return: the number of entries.
563  */
564 static u16 batadv_tt_entries(u16 tt_len)
565 {
566         return tt_len / batadv_tt_len(1);
567 }
568
569 /**
570  * batadv_tt_local_table_transmit_size() - calculates the local translation
571  *  table size when transmitted over the air
572  * @bat_priv: the bat priv with all the soft interface information
573  *
574  * Return: local translation table size in bytes.
575  */
576 static int batadv_tt_local_table_transmit_size(struct batadv_priv *bat_priv)
577 {
578         u16 num_vlan = 0;
579         u16 tt_local_entries = 0;
580         struct batadv_softif_vlan *vlan;
581         int hdr_size;
582
583         rcu_read_lock();
584         hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
585                 num_vlan++;
586                 tt_local_entries += atomic_read(&vlan->tt.num_entries);
587         }
588         rcu_read_unlock();
589
590         /* header size of tvlv encapsulated tt response payload */
591         hdr_size = sizeof(struct batadv_unicast_tvlv_packet);
592         hdr_size += sizeof(struct batadv_tvlv_hdr);
593         hdr_size += sizeof(struct batadv_tvlv_tt_data);
594         hdr_size += num_vlan * sizeof(struct batadv_tvlv_tt_vlan_data);
595
596         return hdr_size + batadv_tt_len(tt_local_entries);
597 }
598
599 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
600 {
601         if (bat_priv->tt.local_hash)
602                 return 0;
603
604         bat_priv->tt.local_hash = batadv_hash_new(1024);
605
606         if (!bat_priv->tt.local_hash)
607                 return -ENOMEM;
608
609         batadv_hash_set_lock_class(bat_priv->tt.local_hash,
610                                    &batadv_tt_local_hash_lock_class_key);
611
612         return 0;
613 }
614
615 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
616                                   struct batadv_tt_global_entry *tt_global,
617                                   const char *message)
618 {
619         struct batadv_tt_global_entry *tt_removed_entry;
620         struct hlist_node *tt_removed_node;
621
622         batadv_dbg(BATADV_DBG_TT, bat_priv,
623                    "Deleting global tt entry %pM (vid: %d): %s\n",
624                    tt_global->common.addr,
625                    batadv_print_vid(tt_global->common.vid), message);
626
627         tt_removed_node = batadv_hash_remove(bat_priv->tt.global_hash,
628                                              batadv_compare_tt,
629                                              batadv_choose_tt,
630                                              &tt_global->common);
631         if (!tt_removed_node)
632                 return;
633
634         /* drop reference of remove hash entry */
635         tt_removed_entry = hlist_entry(tt_removed_node,
636                                        struct batadv_tt_global_entry,
637                                        common.hash_entry);
638         batadv_tt_global_entry_put(tt_removed_entry);
639 }
640
641 /**
642  * batadv_tt_local_add() - add a new client to the local table or update an
643  *  existing client
644  * @soft_iface: netdev struct of the mesh interface
645  * @addr: the mac address of the client to add
646  * @vid: VLAN identifier
647  * @ifindex: index of the interface where the client is connected to (useful to
648  *  identify wireless clients)
649  * @mark: the value contained in the skb->mark field of the received packet (if
650  *  any)
651  *
652  * Return: true if the client was successfully added, false otherwise.
653  */
654 bool batadv_tt_local_add(struct net_device *soft_iface, const u8 *addr,
655                          unsigned short vid, int ifindex, u32 mark)
656 {
657         struct batadv_priv *bat_priv = netdev_priv(soft_iface);
658         struct batadv_tt_local_entry *tt_local;
659         struct batadv_tt_global_entry *tt_global = NULL;
660         struct net *net = dev_net(soft_iface);
661         struct batadv_softif_vlan *vlan;
662         struct net_device *in_dev = NULL;
663         struct batadv_hard_iface *in_hardif = NULL;
664         struct hlist_head *head;
665         struct batadv_tt_orig_list_entry *orig_entry;
666         int hash_added, table_size, packet_size_max;
667         bool ret = false;
668         bool roamed_back = false;
669         u8 remote_flags;
670         u32 match_mark;
671
672         if (ifindex != BATADV_NULL_IFINDEX)
673                 in_dev = dev_get_by_index(net, ifindex);
674
675         if (in_dev)
676                 in_hardif = batadv_hardif_get_by_netdev(in_dev);
677
678         tt_local = batadv_tt_local_hash_find(bat_priv, addr, vid);
679
680         if (!is_multicast_ether_addr(addr))
681                 tt_global = batadv_tt_global_hash_find(bat_priv, addr, vid);
682
683         if (tt_local) {
684                 tt_local->last_seen = jiffies;
685                 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
686                         batadv_dbg(BATADV_DBG_TT, bat_priv,
687                                    "Re-adding pending client %pM (vid: %d)\n",
688                                    addr, batadv_print_vid(vid));
689                         /* whatever the reason why the PENDING flag was set,
690                          * this is a client which was enqueued to be removed in
691                          * this orig_interval. Since it popped up again, the
692                          * flag can be reset like it was never enqueued
693                          */
694                         tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
695                         goto add_event;
696                 }
697
698                 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
699                         batadv_dbg(BATADV_DBG_TT, bat_priv,
700                                    "Roaming client %pM (vid: %d) came back to its original location\n",
701                                    addr, batadv_print_vid(vid));
702                         /* the ROAM flag is set because this client roamed away
703                          * and the node got a roaming_advertisement message. Now
704                          * that the client popped up again at its original
705                          * location such flag can be unset
706                          */
707                         tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
708                         roamed_back = true;
709                 }
710                 goto check_roaming;
711         }
712
713         /* Ignore the client if we cannot send it in a full table response. */
714         table_size = batadv_tt_local_table_transmit_size(bat_priv);
715         table_size += batadv_tt_len(1);
716         packet_size_max = atomic_read(&bat_priv->packet_size_max);
717         if (table_size > packet_size_max) {
718                 net_ratelimited_function(batadv_info, soft_iface,
719                                          "Local translation table size (%i) exceeds maximum packet size (%i); Ignoring new local tt entry: %pM\n",
720                                          table_size, packet_size_max, addr);
721                 goto out;
722         }
723
724         tt_local = kmem_cache_alloc(batadv_tl_cache, GFP_ATOMIC);
725         if (!tt_local)
726                 goto out;
727
728         /* increase the refcounter of the related vlan */
729         vlan = batadv_softif_vlan_get(bat_priv, vid);
730         if (!vlan) {
731                 net_ratelimited_function(batadv_info, soft_iface,
732                                          "adding TT local entry %pM to non-existent VLAN %d\n",
733                                          addr, batadv_print_vid(vid));
734                 kmem_cache_free(batadv_tl_cache, tt_local);
735                 tt_local = NULL;
736                 goto out;
737         }
738
739         batadv_dbg(BATADV_DBG_TT, bat_priv,
740                    "Creating new local tt entry: %pM (vid: %d, ttvn: %d)\n",
741                    addr, batadv_print_vid(vid),
742                    (u8)atomic_read(&bat_priv->tt.vn));
743
744         ether_addr_copy(tt_local->common.addr, addr);
745         /* The local entry has to be marked as NEW to avoid to send it in
746          * a full table response going out before the next ttvn increment
747          * (consistency check)
748          */
749         tt_local->common.flags = BATADV_TT_CLIENT_NEW;
750         tt_local->common.vid = vid;
751         if (batadv_is_wifi_hardif(in_hardif))
752                 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
753         kref_init(&tt_local->common.refcount);
754         tt_local->last_seen = jiffies;
755         tt_local->common.added_at = tt_local->last_seen;
756         tt_local->vlan = vlan;
757
758         /* the batman interface mac and multicast addresses should never be
759          * purged
760          */
761         if (batadv_compare_eth(addr, soft_iface->dev_addr) ||
762             is_multicast_ether_addr(addr))
763                 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
764
765         kref_get(&tt_local->common.refcount);
766         hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
767                                      batadv_choose_tt, &tt_local->common,
768                                      &tt_local->common.hash_entry);
769
770         if (unlikely(hash_added != 0)) {
771                 /* remove the reference for the hash */
772                 batadv_tt_local_entry_put(tt_local);
773                 goto out;
774         }
775
776 add_event:
777         batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
778
779 check_roaming:
780         /* Check whether it is a roaming, but don't do anything if the roaming
781          * process has already been handled
782          */
783         if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
784                 /* These node are probably going to update their tt table */
785                 head = &tt_global->orig_list;
786                 rcu_read_lock();
787                 hlist_for_each_entry_rcu(orig_entry, head, list) {
788                         batadv_send_roam_adv(bat_priv, tt_global->common.addr,
789                                              tt_global->common.vid,
790                                              orig_entry->orig_node);
791                 }
792                 rcu_read_unlock();
793                 if (roamed_back) {
794                         batadv_tt_global_free(bat_priv, tt_global,
795                                               "Roaming canceled");
796                         tt_global = NULL;
797                 } else {
798                         /* The global entry has to be marked as ROAMING and
799                          * has to be kept for consistency purpose
800                          */
801                         tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
802                         tt_global->roam_at = jiffies;
803                 }
804         }
805
806         /* store the current remote flags before altering them. This helps
807          * understanding is flags are changing or not
808          */
809         remote_flags = tt_local->common.flags & BATADV_TT_REMOTE_MASK;
810
811         if (batadv_is_wifi_hardif(in_hardif))
812                 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
813         else
814                 tt_local->common.flags &= ~BATADV_TT_CLIENT_WIFI;
815
816         /* check the mark in the skb: if it's equal to the configured
817          * isolation_mark, it means the packet is coming from an isolated
818          * non-mesh client
819          */
820         match_mark = (mark & bat_priv->isolation_mark_mask);
821         if (bat_priv->isolation_mark_mask &&
822             match_mark == bat_priv->isolation_mark)
823                 tt_local->common.flags |= BATADV_TT_CLIENT_ISOLA;
824         else
825                 tt_local->common.flags &= ~BATADV_TT_CLIENT_ISOLA;
826
827         /* if any "dynamic" flag has been modified, resend an ADD event for this
828          * entry so that all the nodes can get the new flags
829          */
830         if (remote_flags ^ (tt_local->common.flags & BATADV_TT_REMOTE_MASK))
831                 batadv_tt_local_event(bat_priv, tt_local, BATADV_NO_FLAGS);
832
833         ret = true;
834 out:
835         if (in_hardif)
836                 batadv_hardif_put(in_hardif);
837         if (in_dev)
838                 dev_put(in_dev);
839         if (tt_local)
840                 batadv_tt_local_entry_put(tt_local);
841         if (tt_global)
842                 batadv_tt_global_entry_put(tt_global);
843         return ret;
844 }
845
846 /**
847  * batadv_tt_prepare_tvlv_global_data() - prepare the TVLV TT header to send
848  *  within a TT Response directed to another node
849  * @orig_node: originator for which the TT data has to be prepared
850  * @tt_data: uninitialised pointer to the address of the TVLV buffer
851  * @tt_change: uninitialised pointer to the address of the area where the TT
852  *  changed can be stored
853  * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
854  *  function reserves the amount of space needed to send the entire global TT
855  *  table. In case of success the value is updated with the real amount of
856  *  reserved bytes
857  * Allocate the needed amount of memory for the entire TT TVLV and write its
858  * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
859  * objects, one per active VLAN served by the originator node.
860  *
861  * Return: the size of the allocated buffer or 0 in case of failure.
862  */
863 static u16
864 batadv_tt_prepare_tvlv_global_data(struct batadv_orig_node *orig_node,
865                                    struct batadv_tvlv_tt_data **tt_data,
866                                    struct batadv_tvlv_tt_change **tt_change,
867                                    s32 *tt_len)
868 {
869         u16 num_vlan = 0;
870         u16 num_entries = 0;
871         u16 change_offset;
872         u16 tvlv_len;
873         struct batadv_tvlv_tt_vlan_data *tt_vlan;
874         struct batadv_orig_node_vlan *vlan;
875         u8 *tt_change_ptr;
876
877         spin_lock_bh(&orig_node->vlan_list_lock);
878         hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
879                 num_vlan++;
880                 num_entries += atomic_read(&vlan->tt.num_entries);
881         }
882
883         change_offset = sizeof(**tt_data);
884         change_offset += num_vlan * sizeof(*tt_vlan);
885
886         /* if tt_len is negative, allocate the space needed by the full table */
887         if (*tt_len < 0)
888                 *tt_len = batadv_tt_len(num_entries);
889
890         tvlv_len = *tt_len;
891         tvlv_len += change_offset;
892
893         *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
894         if (!*tt_data) {
895                 *tt_len = 0;
896                 goto out;
897         }
898
899         (*tt_data)->flags = BATADV_NO_FLAGS;
900         (*tt_data)->ttvn = atomic_read(&orig_node->last_ttvn);
901         (*tt_data)->num_vlan = htons(num_vlan);
902
903         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
904         hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
905                 tt_vlan->vid = htons(vlan->vid);
906                 tt_vlan->crc = htonl(vlan->tt.crc);
907
908                 tt_vlan++;
909         }
910
911         tt_change_ptr = (u8 *)*tt_data + change_offset;
912         *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
913
914 out:
915         spin_unlock_bh(&orig_node->vlan_list_lock);
916         return tvlv_len;
917 }
918
919 /**
920  * batadv_tt_prepare_tvlv_local_data() - allocate and prepare the TT TVLV for
921  *  this node
922  * @bat_priv: the bat priv with all the soft interface information
923  * @tt_data: uninitialised pointer to the address of the TVLV buffer
924  * @tt_change: uninitialised pointer to the address of the area where the TT
925  *  changes can be stored
926  * @tt_len: pointer to the length to reserve to the tt_change. if -1 this
927  *  function reserves the amount of space needed to send the entire local TT
928  *  table. In case of success the value is updated with the real amount of
929  *  reserved bytes
930  *
931  * Allocate the needed amount of memory for the entire TT TVLV and write its
932  * header made up by one tvlv_tt_data object and a series of tvlv_tt_vlan_data
933  * objects, one per active VLAN.
934  *
935  * Return: the size of the allocated buffer or 0 in case of failure.
936  */
937 static u16
938 batadv_tt_prepare_tvlv_local_data(struct batadv_priv *bat_priv,
939                                   struct batadv_tvlv_tt_data **tt_data,
940                                   struct batadv_tvlv_tt_change **tt_change,
941                                   s32 *tt_len)
942 {
943         struct batadv_tvlv_tt_vlan_data *tt_vlan;
944         struct batadv_softif_vlan *vlan;
945         u16 num_vlan = 0;
946         u16 vlan_entries = 0;
947         u16 total_entries = 0;
948         u16 tvlv_len;
949         u8 *tt_change_ptr;
950         int change_offset;
951
952         spin_lock_bh(&bat_priv->softif_vlan_list_lock);
953         hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
954                 vlan_entries = atomic_read(&vlan->tt.num_entries);
955                 if (vlan_entries < 1)
956                         continue;
957
958                 num_vlan++;
959                 total_entries += vlan_entries;
960         }
961
962         change_offset = sizeof(**tt_data);
963         change_offset += num_vlan * sizeof(*tt_vlan);
964
965         /* if tt_len is negative, allocate the space needed by the full table */
966         if (*tt_len < 0)
967                 *tt_len = batadv_tt_len(total_entries);
968
969         tvlv_len = *tt_len;
970         tvlv_len += change_offset;
971
972         *tt_data = kmalloc(tvlv_len, GFP_ATOMIC);
973         if (!*tt_data) {
974                 tvlv_len = 0;
975                 goto out;
976         }
977
978         (*tt_data)->flags = BATADV_NO_FLAGS;
979         (*tt_data)->ttvn = atomic_read(&bat_priv->tt.vn);
980         (*tt_data)->num_vlan = htons(num_vlan);
981
982         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(*tt_data + 1);
983         hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
984                 vlan_entries = atomic_read(&vlan->tt.num_entries);
985                 if (vlan_entries < 1)
986                         continue;
987
988                 tt_vlan->vid = htons(vlan->vid);
989                 tt_vlan->crc = htonl(vlan->tt.crc);
990
991                 tt_vlan++;
992         }
993
994         tt_change_ptr = (u8 *)*tt_data + change_offset;
995         *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
996
997 out:
998         spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
999         return tvlv_len;
1000 }
1001
1002 /**
1003  * batadv_tt_tvlv_container_update() - update the translation table tvlv
1004  *  container after local tt changes have been committed
1005  * @bat_priv: the bat priv with all the soft interface information
1006  */
1007 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
1008 {
1009         struct batadv_tt_change_node *entry, *safe;
1010         struct batadv_tvlv_tt_data *tt_data;
1011         struct batadv_tvlv_tt_change *tt_change;
1012         int tt_diff_len, tt_change_len = 0;
1013         int tt_diff_entries_num = 0;
1014         int tt_diff_entries_count = 0;
1015         u16 tvlv_len;
1016
1017         tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
1018         tt_diff_len = batadv_tt_len(tt_diff_entries_num);
1019
1020         /* if we have too many changes for one packet don't send any
1021          * and wait for the tt table request which will be fragmented
1022          */
1023         if (tt_diff_len > bat_priv->soft_iface->mtu)
1024                 tt_diff_len = 0;
1025
1026         tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
1027                                                      &tt_change, &tt_diff_len);
1028         if (!tvlv_len)
1029                 return;
1030
1031         tt_data->flags = BATADV_TT_OGM_DIFF;
1032
1033         if (tt_diff_len == 0)
1034                 goto container_register;
1035
1036         spin_lock_bh(&bat_priv->tt.changes_list_lock);
1037         atomic_set(&bat_priv->tt.local_changes, 0);
1038
1039         list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1040                                  list) {
1041                 if (tt_diff_entries_count < tt_diff_entries_num) {
1042                         memcpy(tt_change + tt_diff_entries_count,
1043                                &entry->change,
1044                                sizeof(struct batadv_tvlv_tt_change));
1045                         tt_diff_entries_count++;
1046                 }
1047                 list_del(&entry->list);
1048                 kmem_cache_free(batadv_tt_change_cache, entry);
1049         }
1050         spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1051
1052         /* Keep the buffer for possible tt_request */
1053         spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1054         kfree(bat_priv->tt.last_changeset);
1055         bat_priv->tt.last_changeset_len = 0;
1056         bat_priv->tt.last_changeset = NULL;
1057         tt_change_len = batadv_tt_len(tt_diff_entries_count);
1058         /* check whether this new OGM has no changes due to size problems */
1059         if (tt_diff_entries_count > 0) {
1060                 /* if kmalloc() fails we will reply with the full table
1061                  * instead of providing the diff
1062                  */
1063                 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
1064                 if (bat_priv->tt.last_changeset) {
1065                         memcpy(bat_priv->tt.last_changeset,
1066                                tt_change, tt_change_len);
1067                         bat_priv->tt.last_changeset_len = tt_diff_len;
1068                 }
1069         }
1070         spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1071
1072 container_register:
1073         batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
1074                                        tvlv_len);
1075         kfree(tt_data);
1076 }
1077
1078 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1079
1080 /**
1081  * batadv_tt_local_seq_print_text() - Print the local tt table in a seq file
1082  * @seq: seq file to print on
1083  * @offset: not used
1084  *
1085  * Return: always 0
1086  */
1087 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
1088 {
1089         struct net_device *net_dev = (struct net_device *)seq->private;
1090         struct batadv_priv *bat_priv = netdev_priv(net_dev);
1091         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1092         struct batadv_tt_common_entry *tt_common_entry;
1093         struct batadv_tt_local_entry *tt_local;
1094         struct batadv_hard_iface *primary_if;
1095         struct hlist_head *head;
1096         u32 i;
1097         int last_seen_secs;
1098         int last_seen_msecs;
1099         unsigned long last_seen_jiffies;
1100         bool no_purge;
1101         u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
1102
1103         primary_if = batadv_seq_print_text_primary_if_get(seq);
1104         if (!primary_if)
1105                 goto out;
1106
1107         seq_printf(seq,
1108                    "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
1109                    net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
1110         seq_puts(seq,
1111                  "       Client         VID Flags    Last seen (CRC       )\n");
1112
1113         for (i = 0; i < hash->size; i++) {
1114                 head = &hash->table[i];
1115
1116                 rcu_read_lock();
1117                 hlist_for_each_entry_rcu(tt_common_entry,
1118                                          head, hash_entry) {
1119                         tt_local = container_of(tt_common_entry,
1120                                                 struct batadv_tt_local_entry,
1121                                                 common);
1122                         last_seen_jiffies = jiffies - tt_local->last_seen;
1123                         last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1124                         last_seen_secs = last_seen_msecs / 1000;
1125                         last_seen_msecs = last_seen_msecs % 1000;
1126
1127                         no_purge = tt_common_entry->flags & np_flag;
1128                         seq_printf(seq,
1129                                    " * %pM %4i [%c%c%c%c%c%c] %3u.%03u   (%#.8x)\n",
1130                                    tt_common_entry->addr,
1131                                    batadv_print_vid(tt_common_entry->vid),
1132                                    ((tt_common_entry->flags &
1133                                      BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1134                                    no_purge ? 'P' : '.',
1135                                    ((tt_common_entry->flags &
1136                                      BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1137                                    ((tt_common_entry->flags &
1138                                      BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1139                                    ((tt_common_entry->flags &
1140                                      BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1141                                    ((tt_common_entry->flags &
1142                                      BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1143                                    no_purge ? 0 : last_seen_secs,
1144                                    no_purge ? 0 : last_seen_msecs,
1145                                    tt_local->vlan->tt.crc);
1146                 }
1147                 rcu_read_unlock();
1148         }
1149 out:
1150         if (primary_if)
1151                 batadv_hardif_put(primary_if);
1152         return 0;
1153 }
1154 #endif
1155
1156 /**
1157  * batadv_tt_local_dump_entry() - Dump one TT local entry into a message
1158  * @msg :Netlink message to dump into
1159  * @portid: Port making netlink request
1160  * @cb: Control block containing additional options
1161  * @bat_priv: The bat priv with all the soft interface information
1162  * @common: tt local & tt global common data
1163  *
1164  * Return: Error code, or 0 on success
1165  */
1166 static int
1167 batadv_tt_local_dump_entry(struct sk_buff *msg, u32 portid,
1168                            struct netlink_callback *cb,
1169                            struct batadv_priv *bat_priv,
1170                            struct batadv_tt_common_entry *common)
1171 {
1172         void *hdr;
1173         struct batadv_softif_vlan *vlan;
1174         struct batadv_tt_local_entry *local;
1175         unsigned int last_seen_msecs;
1176         u32 crc;
1177
1178         local = container_of(common, struct batadv_tt_local_entry, common);
1179         last_seen_msecs = jiffies_to_msecs(jiffies - local->last_seen);
1180
1181         vlan = batadv_softif_vlan_get(bat_priv, common->vid);
1182         if (!vlan)
1183                 return 0;
1184
1185         crc = vlan->tt.crc;
1186
1187         batadv_softif_vlan_put(vlan);
1188
1189         hdr = genlmsg_put(msg, portid, cb->nlh->nlmsg_seq,
1190                           &batadv_netlink_family,  NLM_F_MULTI,
1191                           BATADV_CMD_GET_TRANSTABLE_LOCAL);
1192         if (!hdr)
1193                 return -ENOBUFS;
1194
1195         genl_dump_check_consistent(cb, hdr);
1196
1197         if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
1198             nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
1199             nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
1200             nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, common->flags))
1201                 goto nla_put_failure;
1202
1203         if (!(common->flags & BATADV_TT_CLIENT_NOPURGE) &&
1204             nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS, last_seen_msecs))
1205                 goto nla_put_failure;
1206
1207         genlmsg_end(msg, hdr);
1208         return 0;
1209
1210  nla_put_failure:
1211         genlmsg_cancel(msg, hdr);
1212         return -EMSGSIZE;
1213 }
1214
1215 /**
1216  * batadv_tt_local_dump_bucket() - Dump one TT local bucket into a message
1217  * @msg: Netlink message to dump into
1218  * @portid: Port making netlink request
1219  * @cb: Control block containing additional options
1220  * @bat_priv: The bat priv with all the soft interface information
1221  * @hash: hash to dump
1222  * @bucket: bucket index to dump
1223  * @idx_s: Number of entries to skip
1224  *
1225  * Return: Error code, or 0 on success
1226  */
1227 static int
1228 batadv_tt_local_dump_bucket(struct sk_buff *msg, u32 portid,
1229                             struct netlink_callback *cb,
1230                             struct batadv_priv *bat_priv,
1231                             struct batadv_hashtable *hash, unsigned int bucket,
1232                             int *idx_s)
1233 {
1234         struct batadv_tt_common_entry *common;
1235         int idx = 0;
1236
1237         spin_lock_bh(&hash->list_locks[bucket]);
1238         cb->seq = atomic_read(&hash->generation) << 1 | 1;
1239
1240         hlist_for_each_entry(common, &hash->table[bucket], hash_entry) {
1241                 if (idx++ < *idx_s)
1242                         continue;
1243
1244                 if (batadv_tt_local_dump_entry(msg, portid, cb, bat_priv,
1245                                                common)) {
1246                         spin_unlock_bh(&hash->list_locks[bucket]);
1247                         *idx_s = idx - 1;
1248                         return -EMSGSIZE;
1249                 }
1250         }
1251         spin_unlock_bh(&hash->list_locks[bucket]);
1252
1253         *idx_s = 0;
1254         return 0;
1255 }
1256
1257 /**
1258  * batadv_tt_local_dump() - Dump TT local entries into a message
1259  * @msg: Netlink message to dump into
1260  * @cb: Parameters from query
1261  *
1262  * Return: Error code, or 0 on success
1263  */
1264 int batadv_tt_local_dump(struct sk_buff *msg, struct netlink_callback *cb)
1265 {
1266         struct net *net = sock_net(cb->skb->sk);
1267         struct net_device *soft_iface;
1268         struct batadv_priv *bat_priv;
1269         struct batadv_hard_iface *primary_if = NULL;
1270         struct batadv_hashtable *hash;
1271         int ret;
1272         int ifindex;
1273         int bucket = cb->args[0];
1274         int idx = cb->args[1];
1275         int portid = NETLINK_CB(cb->skb).portid;
1276
1277         ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1278         if (!ifindex)
1279                 return -EINVAL;
1280
1281         soft_iface = dev_get_by_index(net, ifindex);
1282         if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1283                 ret = -ENODEV;
1284                 goto out;
1285         }
1286
1287         bat_priv = netdev_priv(soft_iface);
1288
1289         primary_if = batadv_primary_if_get_selected(bat_priv);
1290         if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1291                 ret = -ENOENT;
1292                 goto out;
1293         }
1294
1295         hash = bat_priv->tt.local_hash;
1296
1297         while (bucket < hash->size) {
1298                 if (batadv_tt_local_dump_bucket(msg, portid, cb, bat_priv,
1299                                                 hash, bucket, &idx))
1300                         break;
1301
1302                 bucket++;
1303         }
1304
1305         ret = msg->len;
1306
1307  out:
1308         if (primary_if)
1309                 batadv_hardif_put(primary_if);
1310         if (soft_iface)
1311                 dev_put(soft_iface);
1312
1313         cb->args[0] = bucket;
1314         cb->args[1] = idx;
1315
1316         return ret;
1317 }
1318
1319 static void
1320 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1321                             struct batadv_tt_local_entry *tt_local_entry,
1322                             u16 flags, const char *message)
1323 {
1324         batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1325
1326         /* The local client has to be marked as "pending to be removed" but has
1327          * to be kept in the table in order to send it in a full table
1328          * response issued before the net ttvn increment (consistency check)
1329          */
1330         tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1331
1332         batadv_dbg(BATADV_DBG_TT, bat_priv,
1333                    "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1334                    tt_local_entry->common.addr,
1335                    batadv_print_vid(tt_local_entry->common.vid), message);
1336 }
1337
1338 /**
1339  * batadv_tt_local_remove() - logically remove an entry from the local table
1340  * @bat_priv: the bat priv with all the soft interface information
1341  * @addr: the MAC address of the client to remove
1342  * @vid: VLAN identifier
1343  * @message: message to append to the log on deletion
1344  * @roaming: true if the deletion is due to a roaming event
1345  *
1346  * Return: the flags assigned to the local entry before being deleted
1347  */
1348 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1349                            unsigned short vid, const char *message,
1350                            bool roaming)
1351 {
1352         struct batadv_tt_local_entry *tt_removed_entry;
1353         struct batadv_tt_local_entry *tt_local_entry;
1354         u16 flags, curr_flags = BATADV_NO_FLAGS;
1355         struct hlist_node *tt_removed_node;
1356
1357         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1358         if (!tt_local_entry)
1359                 goto out;
1360
1361         curr_flags = tt_local_entry->common.flags;
1362
1363         flags = BATADV_TT_CLIENT_DEL;
1364         /* if this global entry addition is due to a roaming, the node has to
1365          * mark the local entry as "roamed" in order to correctly reroute
1366          * packets later
1367          */
1368         if (roaming) {
1369                 flags |= BATADV_TT_CLIENT_ROAM;
1370                 /* mark the local client as ROAMed */
1371                 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1372         }
1373
1374         if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1375                 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1376                                             message);
1377                 goto out;
1378         }
1379         /* if this client has been added right now, it is possible to
1380          * immediately purge it
1381          */
1382         batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1383
1384         tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1385                                              batadv_compare_tt,
1386                                              batadv_choose_tt,
1387                                              &tt_local_entry->common);
1388         if (!tt_removed_node)
1389                 goto out;
1390
1391         /* drop reference of remove hash entry */
1392         tt_removed_entry = hlist_entry(tt_removed_node,
1393                                        struct batadv_tt_local_entry,
1394                                        common.hash_entry);
1395         batadv_tt_local_entry_put(tt_removed_entry);
1396
1397 out:
1398         if (tt_local_entry)
1399                 batadv_tt_local_entry_put(tt_local_entry);
1400
1401         return curr_flags;
1402 }
1403
1404 /**
1405  * batadv_tt_local_purge_list() - purge inactive tt local entries
1406  * @bat_priv: the bat priv with all the soft interface information
1407  * @head: pointer to the list containing the local tt entries
1408  * @timeout: parameter deciding whether a given tt local entry is considered
1409  *  inactive or not
1410  */
1411 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1412                                        struct hlist_head *head,
1413                                        int timeout)
1414 {
1415         struct batadv_tt_local_entry *tt_local_entry;
1416         struct batadv_tt_common_entry *tt_common_entry;
1417         struct hlist_node *node_tmp;
1418
1419         hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1420                                   hash_entry) {
1421                 tt_local_entry = container_of(tt_common_entry,
1422                                               struct batadv_tt_local_entry,
1423                                               common);
1424                 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1425                         continue;
1426
1427                 /* entry already marked for deletion */
1428                 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1429                         continue;
1430
1431                 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1432                         continue;
1433
1434                 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1435                                             BATADV_TT_CLIENT_DEL, "timed out");
1436         }
1437 }
1438
1439 /**
1440  * batadv_tt_local_purge() - purge inactive tt local entries
1441  * @bat_priv: the bat priv with all the soft interface information
1442  * @timeout: parameter deciding whether a given tt local entry is considered
1443  *  inactive or not
1444  */
1445 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1446                                   int timeout)
1447 {
1448         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1449         struct hlist_head *head;
1450         spinlock_t *list_lock; /* protects write access to the hash lists */
1451         u32 i;
1452
1453         for (i = 0; i < hash->size; i++) {
1454                 head = &hash->table[i];
1455                 list_lock = &hash->list_locks[i];
1456
1457                 spin_lock_bh(list_lock);
1458                 batadv_tt_local_purge_list(bat_priv, head, timeout);
1459                 spin_unlock_bh(list_lock);
1460         }
1461 }
1462
1463 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1464 {
1465         struct batadv_hashtable *hash;
1466         spinlock_t *list_lock; /* protects write access to the hash lists */
1467         struct batadv_tt_common_entry *tt_common_entry;
1468         struct batadv_tt_local_entry *tt_local;
1469         struct hlist_node *node_tmp;
1470         struct hlist_head *head;
1471         u32 i;
1472
1473         if (!bat_priv->tt.local_hash)
1474                 return;
1475
1476         hash = bat_priv->tt.local_hash;
1477
1478         for (i = 0; i < hash->size; i++) {
1479                 head = &hash->table[i];
1480                 list_lock = &hash->list_locks[i];
1481
1482                 spin_lock_bh(list_lock);
1483                 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1484                                           head, hash_entry) {
1485                         hlist_del_rcu(&tt_common_entry->hash_entry);
1486                         tt_local = container_of(tt_common_entry,
1487                                                 struct batadv_tt_local_entry,
1488                                                 common);
1489
1490                         batadv_tt_local_entry_put(tt_local);
1491                 }
1492                 spin_unlock_bh(list_lock);
1493         }
1494
1495         batadv_hash_destroy(hash);
1496
1497         bat_priv->tt.local_hash = NULL;
1498 }
1499
1500 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1501 {
1502         if (bat_priv->tt.global_hash)
1503                 return 0;
1504
1505         bat_priv->tt.global_hash = batadv_hash_new(1024);
1506
1507         if (!bat_priv->tt.global_hash)
1508                 return -ENOMEM;
1509
1510         batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1511                                    &batadv_tt_global_hash_lock_class_key);
1512
1513         return 0;
1514 }
1515
1516 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1517 {
1518         struct batadv_tt_change_node *entry, *safe;
1519
1520         spin_lock_bh(&bat_priv->tt.changes_list_lock);
1521
1522         list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1523                                  list) {
1524                 list_del(&entry->list);
1525                 kmem_cache_free(batadv_tt_change_cache, entry);
1526         }
1527
1528         atomic_set(&bat_priv->tt.local_changes, 0);
1529         spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1530 }
1531
1532 /**
1533  * batadv_tt_global_orig_entry_find() - find a TT orig_list_entry
1534  * @entry: the TT global entry where the orig_list_entry has to be
1535  *  extracted from
1536  * @orig_node: the originator for which the orig_list_entry has to be found
1537  *
1538  * retrieve the orig_tt_list_entry belonging to orig_node from the
1539  * batadv_tt_global_entry list
1540  *
1541  * Return: it with an increased refcounter, NULL if not found
1542  */
1543 static struct batadv_tt_orig_list_entry *
1544 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1545                                  const struct batadv_orig_node *orig_node)
1546 {
1547         struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1548         const struct hlist_head *head;
1549
1550         rcu_read_lock();
1551         head = &entry->orig_list;
1552         hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1553                 if (tmp_orig_entry->orig_node != orig_node)
1554                         continue;
1555                 if (!kref_get_unless_zero(&tmp_orig_entry->refcount))
1556                         continue;
1557
1558                 orig_entry = tmp_orig_entry;
1559                 break;
1560         }
1561         rcu_read_unlock();
1562
1563         return orig_entry;
1564 }
1565
1566 /**
1567  * batadv_tt_global_entry_has_orig() - check if a TT global entry is also
1568  *  handled by a given originator
1569  * @entry: the TT global entry to check
1570  * @orig_node: the originator to search in the list
1571  * @flags: a pointer to store TT flags for the given @entry received
1572  *  from @orig_node
1573  *
1574  * find out if an orig_node is already in the list of a tt_global_entry.
1575  *
1576  * Return: true if found, false otherwise
1577  */
1578 static bool
1579 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1580                                 const struct batadv_orig_node *orig_node,
1581                                 u8 *flags)
1582 {
1583         struct batadv_tt_orig_list_entry *orig_entry;
1584         bool found = false;
1585
1586         orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1587         if (orig_entry) {
1588                 found = true;
1589
1590                 if (flags)
1591                         *flags = orig_entry->flags;
1592
1593                 batadv_tt_orig_list_entry_put(orig_entry);
1594         }
1595
1596         return found;
1597 }
1598
1599 /**
1600  * batadv_tt_global_sync_flags() - update TT sync flags
1601  * @tt_global: the TT global entry to update sync flags in
1602  *
1603  * Updates the sync flag bits in the tt_global flag attribute with a logical
1604  * OR of all sync flags from any of its TT orig entries.
1605  */
1606 static void
1607 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1608 {
1609         struct batadv_tt_orig_list_entry *orig_entry;
1610         const struct hlist_head *head;
1611         u16 flags = BATADV_NO_FLAGS;
1612
1613         rcu_read_lock();
1614         head = &tt_global->orig_list;
1615         hlist_for_each_entry_rcu(orig_entry, head, list)
1616                 flags |= orig_entry->flags;
1617         rcu_read_unlock();
1618
1619         flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1620         tt_global->common.flags = flags;
1621 }
1622
1623 /**
1624  * batadv_tt_global_orig_entry_add() - add or update a TT orig entry
1625  * @tt_global: the TT global entry to add an orig entry in
1626  * @orig_node: the originator to add an orig entry for
1627  * @ttvn: translation table version number of this changeset
1628  * @flags: TT sync flags
1629  */
1630 static void
1631 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1632                                 struct batadv_orig_node *orig_node, int ttvn,
1633                                 u8 flags)
1634 {
1635         struct batadv_tt_orig_list_entry *orig_entry;
1636
1637         spin_lock_bh(&tt_global->list_lock);
1638
1639         orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1640         if (orig_entry) {
1641                 /* refresh the ttvn: the current value could be a bogus one that
1642                  * was added during a "temporary client detection"
1643                  */
1644                 orig_entry->ttvn = ttvn;
1645                 orig_entry->flags = flags;
1646                 goto sync_flags;
1647         }
1648
1649         orig_entry = kmem_cache_zalloc(batadv_tt_orig_cache, GFP_ATOMIC);
1650         if (!orig_entry)
1651                 goto out;
1652
1653         INIT_HLIST_NODE(&orig_entry->list);
1654         kref_get(&orig_node->refcount);
1655         batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1656         orig_entry->orig_node = orig_node;
1657         orig_entry->ttvn = ttvn;
1658         orig_entry->flags = flags;
1659         kref_init(&orig_entry->refcount);
1660
1661         kref_get(&orig_entry->refcount);
1662         hlist_add_head_rcu(&orig_entry->list,
1663                            &tt_global->orig_list);
1664         atomic_inc(&tt_global->orig_list_count);
1665
1666 sync_flags:
1667         batadv_tt_global_sync_flags(tt_global);
1668 out:
1669         if (orig_entry)
1670                 batadv_tt_orig_list_entry_put(orig_entry);
1671
1672         spin_unlock_bh(&tt_global->list_lock);
1673 }
1674
1675 /**
1676  * batadv_tt_global_add() - add a new TT global entry or update an existing one
1677  * @bat_priv: the bat priv with all the soft interface information
1678  * @orig_node: the originator announcing the client
1679  * @tt_addr: the mac address of the non-mesh client
1680  * @vid: VLAN identifier
1681  * @flags: TT flags that have to be set for this non-mesh client
1682  * @ttvn: the tt version number ever announcing this non-mesh client
1683  *
1684  * Add a new TT global entry for the given originator. If the entry already
1685  * exists add a new reference to the given originator (a global entry can have
1686  * references to multiple originators) and adjust the flags attribute to reflect
1687  * the function argument.
1688  * If a TT local entry exists for this non-mesh client remove it.
1689  *
1690  * The caller must hold orig_node refcount.
1691  *
1692  * Return: true if the new entry has been added, false otherwise
1693  */
1694 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1695                                  struct batadv_orig_node *orig_node,
1696                                  const unsigned char *tt_addr,
1697                                  unsigned short vid, u16 flags, u8 ttvn)
1698 {
1699         struct batadv_tt_global_entry *tt_global_entry;
1700         struct batadv_tt_local_entry *tt_local_entry;
1701         bool ret = false;
1702         int hash_added;
1703         struct batadv_tt_common_entry *common;
1704         u16 local_flags;
1705
1706         /* ignore global entries from backbone nodes */
1707         if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1708                 return true;
1709
1710         tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1711         tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1712
1713         /* if the node already has a local client for this entry, it has to wait
1714          * for a roaming advertisement instead of manually messing up the global
1715          * table
1716          */
1717         if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1718             !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1719                 goto out;
1720
1721         if (!tt_global_entry) {
1722                 tt_global_entry = kmem_cache_zalloc(batadv_tg_cache,
1723                                                     GFP_ATOMIC);
1724                 if (!tt_global_entry)
1725                         goto out;
1726
1727                 common = &tt_global_entry->common;
1728                 ether_addr_copy(common->addr, tt_addr);
1729                 common->vid = vid;
1730
1731                 if (!is_multicast_ether_addr(common->addr))
1732                         common->flags = flags & (~BATADV_TT_SYNC_MASK);
1733
1734                 tt_global_entry->roam_at = 0;
1735                 /* node must store current time in case of roaming. This is
1736                  * needed to purge this entry out on timeout (if nobody claims
1737                  * it)
1738                  */
1739                 if (flags & BATADV_TT_CLIENT_ROAM)
1740                         tt_global_entry->roam_at = jiffies;
1741                 kref_init(&common->refcount);
1742                 common->added_at = jiffies;
1743
1744                 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1745                 atomic_set(&tt_global_entry->orig_list_count, 0);
1746                 spin_lock_init(&tt_global_entry->list_lock);
1747
1748                 kref_get(&common->refcount);
1749                 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1750                                              batadv_compare_tt,
1751                                              batadv_choose_tt, common,
1752                                              &common->hash_entry);
1753
1754                 if (unlikely(hash_added != 0)) {
1755                         /* remove the reference for the hash */
1756                         batadv_tt_global_entry_put(tt_global_entry);
1757                         goto out_remove;
1758                 }
1759         } else {
1760                 common = &tt_global_entry->common;
1761                 /* If there is already a global entry, we can use this one for
1762                  * our processing.
1763                  * But if we are trying to add a temporary client then here are
1764                  * two options at this point:
1765                  * 1) the global client is not a temporary client: the global
1766                  *    client has to be left as it is, temporary information
1767                  *    should never override any already known client state
1768                  * 2) the global client is a temporary client: purge the
1769                  *    originator list and add the new one orig_entry
1770                  */
1771                 if (flags & BATADV_TT_CLIENT_TEMP) {
1772                         if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1773                                 goto out;
1774                         if (batadv_tt_global_entry_has_orig(tt_global_entry,
1775                                                             orig_node, NULL))
1776                                 goto out_remove;
1777                         batadv_tt_global_del_orig_list(tt_global_entry);
1778                         goto add_orig_entry;
1779                 }
1780
1781                 /* if the client was temporary added before receiving the first
1782                  * OGM announcing it, we have to clear the TEMP flag. Also,
1783                  * remove the previous temporary orig node and re-add it
1784                  * if required. If the orig entry changed, the new one which
1785                  * is a non-temporary entry is preferred.
1786                  */
1787                 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1788                         batadv_tt_global_del_orig_list(tt_global_entry);
1789                         common->flags &= ~BATADV_TT_CLIENT_TEMP;
1790                 }
1791
1792                 /* the change can carry possible "attribute" flags like the
1793                  * TT_CLIENT_TEMP, therefore they have to be copied in the
1794                  * client entry
1795                  */
1796                 if (!is_multicast_ether_addr(common->addr))
1797                         common->flags |= flags & (~BATADV_TT_SYNC_MASK);
1798
1799                 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1800                  * one originator left in the list and we previously received a
1801                  * delete + roaming change for this originator.
1802                  *
1803                  * We should first delete the old originator before adding the
1804                  * new one.
1805                  */
1806                 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1807                         batadv_tt_global_del_orig_list(tt_global_entry);
1808                         common->flags &= ~BATADV_TT_CLIENT_ROAM;
1809                         tt_global_entry->roam_at = 0;
1810                 }
1811         }
1812 add_orig_entry:
1813         /* add the new orig_entry (if needed) or update it */
1814         batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1815                                         flags & BATADV_TT_SYNC_MASK);
1816
1817         batadv_dbg(BATADV_DBG_TT, bat_priv,
1818                    "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1819                    common->addr, batadv_print_vid(common->vid),
1820                    orig_node->orig);
1821         ret = true;
1822
1823 out_remove:
1824         /* Do not remove multicast addresses from the local hash on
1825          * global additions
1826          */
1827         if (is_multicast_ether_addr(tt_addr))
1828                 goto out;
1829
1830         /* remove address from local hash if present */
1831         local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1832                                              "global tt received",
1833                                              flags & BATADV_TT_CLIENT_ROAM);
1834         tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1835
1836         if (!(flags & BATADV_TT_CLIENT_ROAM))
1837                 /* this is a normal global add. Therefore the client is not in a
1838                  * roaming state anymore.
1839                  */
1840                 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1841
1842 out:
1843         if (tt_global_entry)
1844                 batadv_tt_global_entry_put(tt_global_entry);
1845         if (tt_local_entry)
1846                 batadv_tt_local_entry_put(tt_local_entry);
1847         return ret;
1848 }
1849
1850 /**
1851  * batadv_transtable_best_orig() - Get best originator list entry from tt entry
1852  * @bat_priv: the bat priv with all the soft interface information
1853  * @tt_global_entry: global translation table entry to be analyzed
1854  *
1855  * This functon assumes the caller holds rcu_read_lock().
1856  * Return: best originator list entry or NULL on errors.
1857  */
1858 static struct batadv_tt_orig_list_entry *
1859 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1860                             struct batadv_tt_global_entry *tt_global_entry)
1861 {
1862         struct batadv_neigh_node *router, *best_router = NULL;
1863         struct batadv_algo_ops *bao = bat_priv->algo_ops;
1864         struct hlist_head *head;
1865         struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1866
1867         head = &tt_global_entry->orig_list;
1868         hlist_for_each_entry_rcu(orig_entry, head, list) {
1869                 router = batadv_orig_router_get(orig_entry->orig_node,
1870                                                 BATADV_IF_DEFAULT);
1871                 if (!router)
1872                         continue;
1873
1874                 if (best_router &&
1875                     bao->neigh.cmp(router, BATADV_IF_DEFAULT, best_router,
1876                                    BATADV_IF_DEFAULT) <= 0) {
1877                         batadv_neigh_node_put(router);
1878                         continue;
1879                 }
1880
1881                 /* release the refcount for the "old" best */
1882                 if (best_router)
1883                         batadv_neigh_node_put(best_router);
1884
1885                 best_entry = orig_entry;
1886                 best_router = router;
1887         }
1888
1889         if (best_router)
1890                 batadv_neigh_node_put(best_router);
1891
1892         return best_entry;
1893 }
1894
1895 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1896 /**
1897  * batadv_tt_global_print_entry() - print all orig nodes who announce the
1898  *  address for this global entry
1899  * @bat_priv: the bat priv with all the soft interface information
1900  * @tt_global_entry: global translation table entry to be printed
1901  * @seq: debugfs table seq_file struct
1902  *
1903  * This functon assumes the caller holds rcu_read_lock().
1904  */
1905 static void
1906 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1907                              struct batadv_tt_global_entry *tt_global_entry,
1908                              struct seq_file *seq)
1909 {
1910         struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1911         struct batadv_tt_common_entry *tt_common_entry;
1912         struct batadv_orig_node_vlan *vlan;
1913         struct hlist_head *head;
1914         u8 last_ttvn;
1915         u16 flags;
1916
1917         tt_common_entry = &tt_global_entry->common;
1918         flags = tt_common_entry->flags;
1919
1920         best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1921         if (best_entry) {
1922                 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1923                                                  tt_common_entry->vid);
1924                 if (!vlan) {
1925                         seq_printf(seq,
1926                                    " * Cannot retrieve VLAN %d for originator %pM\n",
1927                                    batadv_print_vid(tt_common_entry->vid),
1928                                    best_entry->orig_node->orig);
1929                         goto print_list;
1930                 }
1931
1932                 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1933                 seq_printf(seq,
1934                            " %c %pM %4i   (%3u) via %pM     (%3u)   (%#.8x) [%c%c%c%c]\n",
1935                            '*', tt_global_entry->common.addr,
1936                            batadv_print_vid(tt_global_entry->common.vid),
1937                            best_entry->ttvn, best_entry->orig_node->orig,
1938                            last_ttvn, vlan->tt.crc,
1939                            ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1940                            ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1941                            ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1942                            ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1943
1944                 batadv_orig_node_vlan_put(vlan);
1945         }
1946
1947 print_list:
1948         head = &tt_global_entry->orig_list;
1949
1950         hlist_for_each_entry_rcu(orig_entry, head, list) {
1951                 if (best_entry == orig_entry)
1952                         continue;
1953
1954                 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1955                                                  tt_common_entry->vid);
1956                 if (!vlan) {
1957                         seq_printf(seq,
1958                                    " + Cannot retrieve VLAN %d for originator %pM\n",
1959                                    batadv_print_vid(tt_common_entry->vid),
1960                                    orig_entry->orig_node->orig);
1961                         continue;
1962                 }
1963
1964                 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1965                 seq_printf(seq,
1966                            " %c %pM %4d   (%3u) via %pM     (%3u)   (%#.8x) [%c%c%c%c]\n",
1967                            '+', tt_global_entry->common.addr,
1968                            batadv_print_vid(tt_global_entry->common.vid),
1969                            orig_entry->ttvn, orig_entry->orig_node->orig,
1970                            last_ttvn, vlan->tt.crc,
1971                            ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1972                            ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1973                            ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1974                            ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1975
1976                 batadv_orig_node_vlan_put(vlan);
1977         }
1978 }
1979
1980 /**
1981  * batadv_tt_global_seq_print_text() - Print the global tt table in a seq file
1982  * @seq: seq file to print on
1983  * @offset: not used
1984  *
1985  * Return: always 0
1986  */
1987 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1988 {
1989         struct net_device *net_dev = (struct net_device *)seq->private;
1990         struct batadv_priv *bat_priv = netdev_priv(net_dev);
1991         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1992         struct batadv_tt_common_entry *tt_common_entry;
1993         struct batadv_tt_global_entry *tt_global;
1994         struct batadv_hard_iface *primary_if;
1995         struct hlist_head *head;
1996         u32 i;
1997
1998         primary_if = batadv_seq_print_text_primary_if_get(seq);
1999         if (!primary_if)
2000                 goto out;
2001
2002         seq_printf(seq,
2003                    "Globally announced TT entries received via the mesh %s\n",
2004                    net_dev->name);
2005         seq_puts(seq,
2006                  "       Client         VID  (TTVN)       Originator      (Curr TTVN) (CRC       ) Flags\n");
2007
2008         for (i = 0; i < hash->size; i++) {
2009                 head = &hash->table[i];
2010
2011                 rcu_read_lock();
2012                 hlist_for_each_entry_rcu(tt_common_entry,
2013                                          head, hash_entry) {
2014                         tt_global = container_of(tt_common_entry,
2015                                                  struct batadv_tt_global_entry,
2016                                                  common);
2017                         batadv_tt_global_print_entry(bat_priv, tt_global, seq);
2018                 }
2019                 rcu_read_unlock();
2020         }
2021 out:
2022         if (primary_if)
2023                 batadv_hardif_put(primary_if);
2024         return 0;
2025 }
2026 #endif
2027
2028 /**
2029  * batadv_tt_global_dump_subentry() - Dump all TT local entries into a message
2030  * @msg: Netlink message to dump into
2031  * @portid: Port making netlink request
2032  * @seq: Sequence number of netlink message
2033  * @common: tt local & tt global common data
2034  * @orig: Originator node announcing a non-mesh client
2035  * @best: Is the best originator for the TT entry
2036  *
2037  * Return: Error code, or 0 on success
2038  */
2039 static int
2040 batadv_tt_global_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
2041                                struct batadv_tt_common_entry *common,
2042                                struct batadv_tt_orig_list_entry *orig,
2043                                bool best)
2044 {
2045         u16 flags = (common->flags & (~BATADV_TT_SYNC_MASK)) | orig->flags;
2046         void *hdr;
2047         struct batadv_orig_node_vlan *vlan;
2048         u8 last_ttvn;
2049         u32 crc;
2050
2051         vlan = batadv_orig_node_vlan_get(orig->orig_node,
2052                                          common->vid);
2053         if (!vlan)
2054                 return 0;
2055
2056         crc = vlan->tt.crc;
2057
2058         batadv_orig_node_vlan_put(vlan);
2059
2060         hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2061                           NLM_F_MULTI,
2062                           BATADV_CMD_GET_TRANSTABLE_GLOBAL);
2063         if (!hdr)
2064                 return -ENOBUFS;
2065
2066         last_ttvn = atomic_read(&orig->orig_node->last_ttvn);
2067
2068         if (nla_put(msg, BATADV_ATTR_TT_ADDRESS, ETH_ALEN, common->addr) ||
2069             nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2070                     orig->orig_node->orig) ||
2071             nla_put_u8(msg, BATADV_ATTR_TT_TTVN, orig->ttvn) ||
2072             nla_put_u8(msg, BATADV_ATTR_TT_LAST_TTVN, last_ttvn) ||
2073             nla_put_u32(msg, BATADV_ATTR_TT_CRC32, crc) ||
2074             nla_put_u16(msg, BATADV_ATTR_TT_VID, common->vid) ||
2075             nla_put_u32(msg, BATADV_ATTR_TT_FLAGS, flags))
2076                 goto nla_put_failure;
2077
2078         if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2079                 goto nla_put_failure;
2080
2081         genlmsg_end(msg, hdr);
2082         return 0;
2083
2084  nla_put_failure:
2085         genlmsg_cancel(msg, hdr);
2086         return -EMSGSIZE;
2087 }
2088
2089 /**
2090  * batadv_tt_global_dump_entry() - Dump one TT global entry into a message
2091  * @msg: Netlink message to dump into
2092  * @portid: Port making netlink request
2093  * @seq: Sequence number of netlink message
2094  * @bat_priv: The bat priv with all the soft interface information
2095  * @common: tt local & tt global common data
2096  * @sub_s: Number of entries to skip
2097  *
2098  * This function assumes the caller holds rcu_read_lock().
2099  *
2100  * Return: Error code, or 0 on success
2101  */
2102 static int
2103 batadv_tt_global_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2104                             struct batadv_priv *bat_priv,
2105                             struct batadv_tt_common_entry *common, int *sub_s)
2106 {
2107         struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
2108         struct batadv_tt_global_entry *global;
2109         struct hlist_head *head;
2110         int sub = 0;
2111         bool best;
2112
2113         global = container_of(common, struct batadv_tt_global_entry, common);
2114         best_entry = batadv_transtable_best_orig(bat_priv, global);
2115         head = &global->orig_list;
2116
2117         hlist_for_each_entry_rcu(orig_entry, head, list) {
2118                 if (sub++ < *sub_s)
2119                         continue;
2120
2121                 best = (orig_entry == best_entry);
2122
2123                 if (batadv_tt_global_dump_subentry(msg, portid, seq, common,
2124                                                    orig_entry, best)) {
2125                         *sub_s = sub - 1;
2126                         return -EMSGSIZE;
2127                 }
2128         }
2129
2130         *sub_s = 0;
2131         return 0;
2132 }
2133
2134 /**
2135  * batadv_tt_global_dump_bucket() - Dump one TT local bucket into a message
2136  * @msg: Netlink message to dump into
2137  * @portid: Port making netlink request
2138  * @seq: Sequence number of netlink message
2139  * @bat_priv: The bat priv with all the soft interface information
2140  * @head: Pointer to the list containing the global tt entries
2141  * @idx_s: Number of entries to skip
2142  * @sub: Number of entries to skip
2143  *
2144  * Return: Error code, or 0 on success
2145  */
2146 static int
2147 batadv_tt_global_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2148                              struct batadv_priv *bat_priv,
2149                              struct hlist_head *head, int *idx_s, int *sub)
2150 {
2151         struct batadv_tt_common_entry *common;
2152         int idx = 0;
2153
2154         rcu_read_lock();
2155         hlist_for_each_entry_rcu(common, head, hash_entry) {
2156                 if (idx++ < *idx_s)
2157                         continue;
2158
2159                 if (batadv_tt_global_dump_entry(msg, portid, seq, bat_priv,
2160                                                 common, sub)) {
2161                         rcu_read_unlock();
2162                         *idx_s = idx - 1;
2163                         return -EMSGSIZE;
2164                 }
2165         }
2166         rcu_read_unlock();
2167
2168         *idx_s = 0;
2169         *sub = 0;
2170         return 0;
2171 }
2172
2173 /**
2174  * batadv_tt_global_dump() -  Dump TT global entries into a message
2175  * @msg: Netlink message to dump into
2176  * @cb: Parameters from query
2177  *
2178  * Return: Error code, or length of message on success
2179  */
2180 int batadv_tt_global_dump(struct sk_buff *msg, struct netlink_callback *cb)
2181 {
2182         struct net *net = sock_net(cb->skb->sk);
2183         struct net_device *soft_iface;
2184         struct batadv_priv *bat_priv;
2185         struct batadv_hard_iface *primary_if = NULL;
2186         struct batadv_hashtable *hash;
2187         struct hlist_head *head;
2188         int ret;
2189         int ifindex;
2190         int bucket = cb->args[0];
2191         int idx = cb->args[1];
2192         int sub = cb->args[2];
2193         int portid = NETLINK_CB(cb->skb).portid;
2194
2195         ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
2196         if (!ifindex)
2197                 return -EINVAL;
2198
2199         soft_iface = dev_get_by_index(net, ifindex);
2200         if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
2201                 ret = -ENODEV;
2202                 goto out;
2203         }
2204
2205         bat_priv = netdev_priv(soft_iface);
2206
2207         primary_if = batadv_primary_if_get_selected(bat_priv);
2208         if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
2209                 ret = -ENOENT;
2210                 goto out;
2211         }
2212
2213         hash = bat_priv->tt.global_hash;
2214
2215         while (bucket < hash->size) {
2216                 head = &hash->table[bucket];
2217
2218                 if (batadv_tt_global_dump_bucket(msg, portid,
2219                                                  cb->nlh->nlmsg_seq, bat_priv,
2220                                                  head, &idx, &sub))
2221                         break;
2222
2223                 bucket++;
2224         }
2225
2226         ret = msg->len;
2227
2228  out:
2229         if (primary_if)
2230                 batadv_hardif_put(primary_if);
2231         if (soft_iface)
2232                 dev_put(soft_iface);
2233
2234         cb->args[0] = bucket;
2235         cb->args[1] = idx;
2236         cb->args[2] = sub;
2237
2238         return ret;
2239 }
2240
2241 /**
2242  * _batadv_tt_global_del_orig_entry() - remove and free an orig_entry
2243  * @tt_global_entry: the global entry to remove the orig_entry from
2244  * @orig_entry: the orig entry to remove and free
2245  *
2246  * Remove an orig_entry from its list in the given tt_global_entry and
2247  * free this orig_entry afterwards.
2248  *
2249  * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
2250  * part of a list.
2251  */
2252 static void
2253 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
2254                                  struct batadv_tt_orig_list_entry *orig_entry)
2255 {
2256         lockdep_assert_held(&tt_global_entry->list_lock);
2257
2258         batadv_tt_global_size_dec(orig_entry->orig_node,
2259                                   tt_global_entry->common.vid);
2260         atomic_dec(&tt_global_entry->orig_list_count);
2261         /* requires holding tt_global_entry->list_lock and orig_entry->list
2262          * being part of a list
2263          */
2264         hlist_del_rcu(&orig_entry->list);
2265         batadv_tt_orig_list_entry_put(orig_entry);
2266 }
2267
2268 /* deletes the orig list of a tt_global_entry */
2269 static void
2270 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
2271 {
2272         struct hlist_head *head;
2273         struct hlist_node *safe;
2274         struct batadv_tt_orig_list_entry *orig_entry;
2275
2276         spin_lock_bh(&tt_global_entry->list_lock);
2277         head = &tt_global_entry->orig_list;
2278         hlist_for_each_entry_safe(orig_entry, safe, head, list)
2279                 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
2280         spin_unlock_bh(&tt_global_entry->list_lock);
2281 }
2282
2283 /**
2284  * batadv_tt_global_del_orig_node() - remove orig_node from a global tt entry
2285  * @bat_priv: the bat priv with all the soft interface information
2286  * @tt_global_entry: the global entry to remove the orig_node from
2287  * @orig_node: the originator announcing the client
2288  * @message: message to append to the log on deletion
2289  *
2290  * Remove the given orig_node and its according orig_entry from the given
2291  * global tt entry.
2292  */
2293 static void
2294 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
2295                                struct batadv_tt_global_entry *tt_global_entry,
2296                                struct batadv_orig_node *orig_node,
2297                                const char *message)
2298 {
2299         struct hlist_head *head;
2300         struct hlist_node *safe;
2301         struct batadv_tt_orig_list_entry *orig_entry;
2302         unsigned short vid;
2303
2304         spin_lock_bh(&tt_global_entry->list_lock);
2305         head = &tt_global_entry->orig_list;
2306         hlist_for_each_entry_safe(orig_entry, safe, head, list) {
2307                 if (orig_entry->orig_node == orig_node) {
2308                         vid = tt_global_entry->common.vid;
2309                         batadv_dbg(BATADV_DBG_TT, bat_priv,
2310                                    "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
2311                                    orig_node->orig,
2312                                    tt_global_entry->common.addr,
2313                                    batadv_print_vid(vid), message);
2314                         _batadv_tt_global_del_orig_entry(tt_global_entry,
2315                                                          orig_entry);
2316                 }
2317         }
2318         spin_unlock_bh(&tt_global_entry->list_lock);
2319 }
2320
2321 /* If the client is to be deleted, we check if it is the last origantor entry
2322  * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
2323  * timer, otherwise we simply remove the originator scheduled for deletion.
2324  */
2325 static void
2326 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
2327                              struct batadv_tt_global_entry *tt_global_entry,
2328                              struct batadv_orig_node *orig_node,
2329                              const char *message)
2330 {
2331         bool last_entry = true;
2332         struct hlist_head *head;
2333         struct batadv_tt_orig_list_entry *orig_entry;
2334
2335         /* no local entry exists, case 1:
2336          * Check if this is the last one or if other entries exist.
2337          */
2338
2339         rcu_read_lock();
2340         head = &tt_global_entry->orig_list;
2341         hlist_for_each_entry_rcu(orig_entry, head, list) {
2342                 if (orig_entry->orig_node != orig_node) {
2343                         last_entry = false;
2344                         break;
2345                 }
2346         }
2347         rcu_read_unlock();
2348
2349         if (last_entry) {
2350                 /* its the last one, mark for roaming. */
2351                 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
2352                 tt_global_entry->roam_at = jiffies;
2353         } else {
2354                 /* there is another entry, we can simply delete this
2355                  * one and can still use the other one.
2356                  */
2357                 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2358                                                orig_node, message);
2359         }
2360 }
2361
2362 /**
2363  * batadv_tt_global_del() - remove a client from the global table
2364  * @bat_priv: the bat priv with all the soft interface information
2365  * @orig_node: an originator serving this client
2366  * @addr: the mac address of the client
2367  * @vid: VLAN identifier
2368  * @message: a message explaining the reason for deleting the client to print
2369  *  for debugging purpose
2370  * @roaming: true if the deletion has been triggered by a roaming event
2371  */
2372 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
2373                                  struct batadv_orig_node *orig_node,
2374                                  const unsigned char *addr, unsigned short vid,
2375                                  const char *message, bool roaming)
2376 {
2377         struct batadv_tt_global_entry *tt_global_entry;
2378         struct batadv_tt_local_entry *local_entry = NULL;
2379
2380         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2381         if (!tt_global_entry)
2382                 goto out;
2383
2384         if (!roaming) {
2385                 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
2386                                                orig_node, message);
2387
2388                 if (hlist_empty(&tt_global_entry->orig_list))
2389                         batadv_tt_global_free(bat_priv, tt_global_entry,
2390                                               message);
2391
2392                 goto out;
2393         }
2394
2395         /* if we are deleting a global entry due to a roam
2396          * event, there are two possibilities:
2397          * 1) the client roamed from node A to node B => if there
2398          *    is only one originator left for this client, we mark
2399          *    it with BATADV_TT_CLIENT_ROAM, we start a timer and we
2400          *    wait for node B to claim it. In case of timeout
2401          *    the entry is purged.
2402          *
2403          *    If there are other originators left, we directly delete
2404          *    the originator.
2405          * 2) the client roamed to us => we can directly delete
2406          *    the global entry, since it is useless now.
2407          */
2408         local_entry = batadv_tt_local_hash_find(bat_priv,
2409                                                 tt_global_entry->common.addr,
2410                                                 vid);
2411         if (local_entry) {
2412                 /* local entry exists, case 2: client roamed to us. */
2413                 batadv_tt_global_del_orig_list(tt_global_entry);
2414                 batadv_tt_global_free(bat_priv, tt_global_entry, message);
2415         } else {
2416                 /* no local entry exists, case 1: check for roaming */
2417                 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
2418                                              orig_node, message);
2419         }
2420
2421 out:
2422         if (tt_global_entry)
2423                 batadv_tt_global_entry_put(tt_global_entry);
2424         if (local_entry)
2425                 batadv_tt_local_entry_put(local_entry);
2426 }
2427
2428 /**
2429  * batadv_tt_global_del_orig() - remove all the TT global entries belonging to
2430  *  the given originator matching the provided vid
2431  * @bat_priv: the bat priv with all the soft interface information
2432  * @orig_node: the originator owning the entries to remove
2433  * @match_vid: the VLAN identifier to match. If negative all the entries will be
2434  *  removed
2435  * @message: debug message to print as "reason"
2436  */
2437 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
2438                                struct batadv_orig_node *orig_node,
2439                                s32 match_vid,
2440                                const char *message)
2441 {
2442         struct batadv_tt_global_entry *tt_global;
2443         struct batadv_tt_common_entry *tt_common_entry;
2444         u32 i;
2445         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2446         struct hlist_node *safe;
2447         struct hlist_head *head;
2448         spinlock_t *list_lock; /* protects write access to the hash lists */
2449         unsigned short vid;
2450
2451         if (!hash)
2452                 return;
2453
2454         for (i = 0; i < hash->size; i++) {
2455                 head = &hash->table[i];
2456                 list_lock = &hash->list_locks[i];
2457
2458                 spin_lock_bh(list_lock);
2459                 hlist_for_each_entry_safe(tt_common_entry, safe,
2460                                           head, hash_entry) {
2461                         /* remove only matching entries */
2462                         if (match_vid >= 0 && tt_common_entry->vid != match_vid)
2463                                 continue;
2464
2465                         tt_global = container_of(tt_common_entry,
2466                                                  struct batadv_tt_global_entry,
2467                                                  common);
2468
2469                         batadv_tt_global_del_orig_node(bat_priv, tt_global,
2470                                                        orig_node, message);
2471
2472                         if (hlist_empty(&tt_global->orig_list)) {
2473                                 vid = tt_global->common.vid;
2474                                 batadv_dbg(BATADV_DBG_TT, bat_priv,
2475                                            "Deleting global tt entry %pM (vid: %d): %s\n",
2476                                            tt_global->common.addr,
2477                                            batadv_print_vid(vid), message);
2478                                 hlist_del_rcu(&tt_common_entry->hash_entry);
2479                                 batadv_tt_global_entry_put(tt_global);
2480                         }
2481                 }
2482                 spin_unlock_bh(list_lock);
2483         }
2484         clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2485 }
2486
2487 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
2488                                       char **msg)
2489 {
2490         bool purge = false;
2491         unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
2492         unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
2493
2494         if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
2495             batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
2496                 purge = true;
2497                 *msg = "Roaming timeout\n";
2498         }
2499
2500         if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
2501             batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
2502                 purge = true;
2503                 *msg = "Temporary client timeout\n";
2504         }
2505
2506         return purge;
2507 }
2508
2509 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
2510 {
2511         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2512         struct hlist_head *head;
2513         struct hlist_node *node_tmp;
2514         spinlock_t *list_lock; /* protects write access to the hash lists */
2515         u32 i;
2516         char *msg = NULL;
2517         struct batadv_tt_common_entry *tt_common;
2518         struct batadv_tt_global_entry *tt_global;
2519
2520         for (i = 0; i < hash->size; i++) {
2521                 head = &hash->table[i];
2522                 list_lock = &hash->list_locks[i];
2523
2524                 spin_lock_bh(list_lock);
2525                 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2526                                           hash_entry) {
2527                         tt_global = container_of(tt_common,
2528                                                  struct batadv_tt_global_entry,
2529                                                  common);
2530
2531                         if (!batadv_tt_global_to_purge(tt_global, &msg))
2532                                 continue;
2533
2534                         batadv_dbg(BATADV_DBG_TT, bat_priv,
2535                                    "Deleting global tt entry %pM (vid: %d): %s\n",
2536                                    tt_global->common.addr,
2537                                    batadv_print_vid(tt_global->common.vid),
2538                                    msg);
2539
2540                         hlist_del_rcu(&tt_common->hash_entry);
2541
2542                         batadv_tt_global_entry_put(tt_global);
2543                 }
2544                 spin_unlock_bh(list_lock);
2545         }
2546 }
2547
2548 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2549 {
2550         struct batadv_hashtable *hash;
2551         spinlock_t *list_lock; /* protects write access to the hash lists */
2552         struct batadv_tt_common_entry *tt_common_entry;
2553         struct batadv_tt_global_entry *tt_global;
2554         struct hlist_node *node_tmp;
2555         struct hlist_head *head;
2556         u32 i;
2557
2558         if (!bat_priv->tt.global_hash)
2559                 return;
2560
2561         hash = bat_priv->tt.global_hash;
2562
2563         for (i = 0; i < hash->size; i++) {
2564                 head = &hash->table[i];
2565                 list_lock = &hash->list_locks[i];
2566
2567                 spin_lock_bh(list_lock);
2568                 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2569                                           head, hash_entry) {
2570                         hlist_del_rcu(&tt_common_entry->hash_entry);
2571                         tt_global = container_of(tt_common_entry,
2572                                                  struct batadv_tt_global_entry,
2573                                                  common);
2574                         batadv_tt_global_entry_put(tt_global);
2575                 }
2576                 spin_unlock_bh(list_lock);
2577         }
2578
2579         batadv_hash_destroy(hash);
2580
2581         bat_priv->tt.global_hash = NULL;
2582 }
2583
2584 static bool
2585 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2586                        struct batadv_tt_global_entry *tt_global_entry)
2587 {
2588         if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2589             tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2590                 return true;
2591
2592         /* check if the two clients are marked as isolated */
2593         if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2594             tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2595                 return true;
2596
2597         return false;
2598 }
2599
2600 /**
2601  * batadv_transtable_search() - get the mesh destination for a given client
2602  * @bat_priv: the bat priv with all the soft interface information
2603  * @src: mac address of the source client
2604  * @addr: mac address of the destination client
2605  * @vid: VLAN identifier
2606  *
2607  * Return: a pointer to the originator that was selected as destination in the
2608  * mesh for contacting the client 'addr', NULL otherwise.
2609  * In case of multiple originators serving the same client, the function returns
2610  * the best one (best in terms of metric towards the destination node).
2611  *
2612  * If the two clients are AP isolated the function returns NULL.
2613  */
2614 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2615                                                   const u8 *src,
2616                                                   const u8 *addr,
2617                                                   unsigned short vid)
2618 {
2619         struct batadv_tt_local_entry *tt_local_entry = NULL;
2620         struct batadv_tt_global_entry *tt_global_entry = NULL;
2621         struct batadv_orig_node *orig_node = NULL;
2622         struct batadv_tt_orig_list_entry *best_entry;
2623
2624         if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2625                 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2626                 if (!tt_local_entry ||
2627                     (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2628                         goto out;
2629         }
2630
2631         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2632         if (!tt_global_entry)
2633                 goto out;
2634
2635         /* check whether the clients should not communicate due to AP
2636          * isolation
2637          */
2638         if (tt_local_entry &&
2639             _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2640                 goto out;
2641
2642         rcu_read_lock();
2643         best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2644         /* found anything? */
2645         if (best_entry)
2646                 orig_node = best_entry->orig_node;
2647         if (orig_node && !kref_get_unless_zero(&orig_node->refcount))
2648                 orig_node = NULL;
2649         rcu_read_unlock();
2650
2651 out:
2652         if (tt_global_entry)
2653                 batadv_tt_global_entry_put(tt_global_entry);
2654         if (tt_local_entry)
2655                 batadv_tt_local_entry_put(tt_local_entry);
2656
2657         return orig_node;
2658 }
2659
2660 /**
2661  * batadv_tt_global_crc() - calculates the checksum of the local table belonging
2662  *  to the given orig_node
2663  * @bat_priv: the bat priv with all the soft interface information
2664  * @orig_node: originator for which the CRC should be computed
2665  * @vid: VLAN identifier for which the CRC32 has to be computed
2666  *
2667  * This function computes the checksum for the global table corresponding to a
2668  * specific originator. In particular, the checksum is computed as follows: For
2669  * each client connected to the originator the CRC32C of the MAC address and the
2670  * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2671  * together.
2672  *
2673  * The idea behind is that CRC32C should be used as much as possible in order to
2674  * produce a unique hash of the table, but since the order which is used to feed
2675  * the CRC32C function affects the result and since every node in the network
2676  * probably sorts the clients differently, the hash function cannot be directly
2677  * computed over the entire table. Hence the CRC32C is used only on
2678  * the single client entry, while all the results are then xor'ed together
2679  * because the XOR operation can combine them all while trying to reduce the
2680  * noise as much as possible.
2681  *
2682  * Return: the checksum of the global table of a given originator.
2683  */
2684 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2685                                 struct batadv_orig_node *orig_node,
2686                                 unsigned short vid)
2687 {
2688         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2689         struct batadv_tt_orig_list_entry *tt_orig;
2690         struct batadv_tt_common_entry *tt_common;
2691         struct batadv_tt_global_entry *tt_global;
2692         struct hlist_head *head;
2693         u32 i, crc_tmp, crc = 0;
2694         u8 flags;
2695         __be16 tmp_vid;
2696
2697         for (i = 0; i < hash->size; i++) {
2698                 head = &hash->table[i];
2699
2700                 rcu_read_lock();
2701                 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2702                         tt_global = container_of(tt_common,
2703                                                  struct batadv_tt_global_entry,
2704                                                  common);
2705                         /* compute the CRC only for entries belonging to the
2706                          * VLAN identified by the vid passed as parameter
2707                          */
2708                         if (tt_common->vid != vid)
2709                                 continue;
2710
2711                         /* Roaming clients are in the global table for
2712                          * consistency only. They don't have to be
2713                          * taken into account while computing the
2714                          * global crc
2715                          */
2716                         if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2717                                 continue;
2718                         /* Temporary clients have not been announced yet, so
2719                          * they have to be skipped while computing the global
2720                          * crc
2721                          */
2722                         if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2723                                 continue;
2724
2725                         /* find out if this global entry is announced by this
2726                          * originator
2727                          */
2728                         tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2729                                                                    orig_node);
2730                         if (!tt_orig)
2731                                 continue;
2732
2733                         /* use network order to read the VID: this ensures that
2734                          * every node reads the bytes in the same order.
2735                          */
2736                         tmp_vid = htons(tt_common->vid);
2737                         crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2738
2739                         /* compute the CRC on flags that have to be kept in sync
2740                          * among nodes
2741                          */
2742                         flags = tt_orig->flags;
2743                         crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2744
2745                         crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2746
2747                         batadv_tt_orig_list_entry_put(tt_orig);
2748                 }
2749                 rcu_read_unlock();
2750         }
2751
2752         return crc;
2753 }
2754
2755 /**
2756  * batadv_tt_local_crc() - calculates the checksum of the local table
2757  * @bat_priv: the bat priv with all the soft interface information
2758  * @vid: VLAN identifier for which the CRC32 has to be computed
2759  *
2760  * For details about the computation, please refer to the documentation for
2761  * batadv_tt_global_crc().
2762  *
2763  * Return: the checksum of the local table
2764  */
2765 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2766                                unsigned short vid)
2767 {
2768         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2769         struct batadv_tt_common_entry *tt_common;
2770         struct hlist_head *head;
2771         u32 i, crc_tmp, crc = 0;
2772         u8 flags;
2773         __be16 tmp_vid;
2774
2775         for (i = 0; i < hash->size; i++) {
2776                 head = &hash->table[i];
2777
2778                 rcu_read_lock();
2779                 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2780                         /* compute the CRC only for entries belonging to the
2781                          * VLAN identified by vid
2782                          */
2783                         if (tt_common->vid != vid)
2784                                 continue;
2785
2786                         /* not yet committed clients have not to be taken into
2787                          * account while computing the CRC
2788                          */
2789                         if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2790                                 continue;
2791
2792                         /* use network order to read the VID: this ensures that
2793                          * every node reads the bytes in the same order.
2794                          */
2795                         tmp_vid = htons(tt_common->vid);
2796                         crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2797
2798                         /* compute the CRC on flags that have to be kept in sync
2799                          * among nodes
2800                          */
2801                         flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2802                         crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2803
2804                         crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2805                 }
2806                 rcu_read_unlock();
2807         }
2808
2809         return crc;
2810 }
2811
2812 /**
2813  * batadv_tt_req_node_release() - free tt_req node entry
2814  * @ref: kref pointer of the tt req_node entry
2815  */
2816 static void batadv_tt_req_node_release(struct kref *ref)
2817 {
2818         struct batadv_tt_req_node *tt_req_node;
2819
2820         tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2821
2822         kmem_cache_free(batadv_tt_req_cache, tt_req_node);
2823 }
2824
2825 /**
2826  * batadv_tt_req_node_put() - decrement the tt_req_node refcounter and
2827  *  possibly release it
2828  * @tt_req_node: tt_req_node to be free'd
2829  */
2830 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2831 {
2832         kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2833 }
2834
2835 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2836 {
2837         struct batadv_tt_req_node *node;
2838         struct hlist_node *safe;
2839
2840         spin_lock_bh(&bat_priv->tt.req_list_lock);
2841
2842         hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2843                 hlist_del_init(&node->list);
2844                 batadv_tt_req_node_put(node);
2845         }
2846
2847         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2848 }
2849
2850 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2851                                        struct batadv_orig_node *orig_node,
2852                                        const void *tt_buff,
2853                                        u16 tt_buff_len)
2854 {
2855         /* Replace the old buffer only if I received something in the
2856          * last OGM (the OGM could carry no changes)
2857          */
2858         spin_lock_bh(&orig_node->tt_buff_lock);
2859         if (tt_buff_len > 0) {
2860                 kfree(orig_node->tt_buff);
2861                 orig_node->tt_buff_len = 0;
2862                 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2863                 if (orig_node->tt_buff) {
2864                         memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2865                         orig_node->tt_buff_len = tt_buff_len;
2866                 }
2867         }
2868         spin_unlock_bh(&orig_node->tt_buff_lock);
2869 }
2870
2871 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2872 {
2873         struct batadv_tt_req_node *node;
2874         struct hlist_node *safe;
2875
2876         spin_lock_bh(&bat_priv->tt.req_list_lock);
2877         hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2878                 if (batadv_has_timed_out(node->issued_at,
2879                                          BATADV_TT_REQUEST_TIMEOUT)) {
2880                         hlist_del_init(&node->list);
2881                         batadv_tt_req_node_put(node);
2882                 }
2883         }
2884         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2885 }
2886
2887 /**
2888  * batadv_tt_req_node_new() - search and possibly create a tt_req_node object
2889  * @bat_priv: the bat priv with all the soft interface information
2890  * @orig_node: orig node this request is being issued for
2891  *
2892  * Return: the pointer to the new tt_req_node struct if no request
2893  * has already been issued for this orig_node, NULL otherwise.
2894  */
2895 static struct batadv_tt_req_node *
2896 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2897                        struct batadv_orig_node *orig_node)
2898 {
2899         struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2900
2901         spin_lock_bh(&bat_priv->tt.req_list_lock);
2902         hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2903                 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2904                     !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2905                                           BATADV_TT_REQUEST_TIMEOUT))
2906                         goto unlock;
2907         }
2908
2909         tt_req_node = kmem_cache_alloc(batadv_tt_req_cache, GFP_ATOMIC);
2910         if (!tt_req_node)
2911                 goto unlock;
2912
2913         kref_init(&tt_req_node->refcount);
2914         ether_addr_copy(tt_req_node->addr, orig_node->orig);
2915         tt_req_node->issued_at = jiffies;
2916
2917         kref_get(&tt_req_node->refcount);
2918         hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2919 unlock:
2920         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2921         return tt_req_node;
2922 }
2923
2924 /**
2925  * batadv_tt_local_valid() - verify local tt entry and get flags
2926  * @entry_ptr: to be checked local tt entry
2927  * @data_ptr: not used but definition required to satisfy the callback prototype
2928  * @flags: a pointer to store TT flags for this client to
2929  *
2930  * Checks the validity of the given local TT entry. If it is, then the provided
2931  * flags pointer is updated.
2932  *
2933  * Return: true if the entry is a valid, false otherwise.
2934  */
2935 static bool batadv_tt_local_valid(const void *entry_ptr,
2936                                   const void *data_ptr,
2937                                   u8 *flags)
2938 {
2939         const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2940
2941         if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2942                 return false;
2943
2944         if (flags)
2945                 *flags = tt_common_entry->flags;
2946
2947         return true;
2948 }
2949
2950 /**
2951  * batadv_tt_global_valid() - verify global tt entry and get flags
2952  * @entry_ptr: to be checked global tt entry
2953  * @data_ptr: an orig_node object (may be NULL)
2954  * @flags: a pointer to store TT flags for this client to
2955  *
2956  * Checks the validity of the given global TT entry. If it is, then the provided
2957  * flags pointer is updated either with the common (summed) TT flags if data_ptr
2958  * is NULL or the specific, per originator TT flags otherwise.
2959  *
2960  * Return: true if the entry is a valid, false otherwise.
2961  */
2962 static bool batadv_tt_global_valid(const void *entry_ptr,
2963                                    const void *data_ptr,
2964                                    u8 *flags)
2965 {
2966         const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2967         const struct batadv_tt_global_entry *tt_global_entry;
2968         const struct batadv_orig_node *orig_node = data_ptr;
2969
2970         if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2971             tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2972                 return false;
2973
2974         tt_global_entry = container_of(tt_common_entry,
2975                                        struct batadv_tt_global_entry,
2976                                        common);
2977
2978         return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2979                                                flags);
2980 }
2981
2982 /**
2983  * batadv_tt_tvlv_generate() - fill the tvlv buff with the tt entries from the
2984  *  specified tt hash
2985  * @bat_priv: the bat priv with all the soft interface information
2986  * @hash: hash table containing the tt entries
2987  * @tt_len: expected tvlv tt data buffer length in number of bytes
2988  * @tvlv_buff: pointer to the buffer to fill with the TT data
2989  * @valid_cb: function to filter tt change entries and to return TT flags
2990  * @cb_data: data passed to the filter function as argument
2991  *
2992  * Fills the tvlv buff with the tt entries from the specified hash. If valid_cb
2993  * is not provided then this becomes a no-op.
2994  */
2995 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2996                                     struct batadv_hashtable *hash,
2997                                     void *tvlv_buff, u16 tt_len,
2998                                     bool (*valid_cb)(const void *,
2999                                                      const void *,
3000                                                      u8 *flags),
3001                                     void *cb_data)
3002 {
3003         struct batadv_tt_common_entry *tt_common_entry;
3004         struct batadv_tvlv_tt_change *tt_change;
3005         struct hlist_head *head;
3006         u16 tt_tot, tt_num_entries = 0;
3007         u8 flags;
3008         bool ret;
3009         u32 i;
3010
3011         tt_tot = batadv_tt_entries(tt_len);
3012         tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
3013
3014         if (!valid_cb)
3015                 return;
3016
3017         rcu_read_lock();
3018         for (i = 0; i < hash->size; i++) {
3019                 head = &hash->table[i];
3020
3021                 hlist_for_each_entry_rcu(tt_common_entry,
3022                                          head, hash_entry) {
3023                         if (tt_tot == tt_num_entries)
3024                                 break;
3025
3026                         ret = valid_cb(tt_common_entry, cb_data, &flags);
3027                         if (!ret)
3028                                 continue;
3029
3030                         ether_addr_copy(tt_change->addr, tt_common_entry->addr);
3031                         tt_change->flags = flags;
3032                         tt_change->vid = htons(tt_common_entry->vid);
3033                         memset(tt_change->reserved, 0,
3034                                sizeof(tt_change->reserved));
3035
3036                         tt_num_entries++;
3037                         tt_change++;
3038                 }
3039         }
3040         rcu_read_unlock();
3041 }
3042
3043 /**
3044  * batadv_tt_global_check_crc() - check if all the CRCs are correct
3045  * @orig_node: originator for which the CRCs have to be checked
3046  * @tt_vlan: pointer to the first tvlv VLAN entry
3047  * @num_vlan: number of tvlv VLAN entries
3048  *
3049  * Return: true if all the received CRCs match the locally stored ones, false
3050  * otherwise
3051  */
3052 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
3053                                        struct batadv_tvlv_tt_vlan_data *tt_vlan,
3054                                        u16 num_vlan)
3055 {
3056         struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
3057         struct batadv_orig_node_vlan *vlan;
3058         int i, orig_num_vlan;
3059         u32 crc;
3060
3061         /* check if each received CRC matches the locally stored one */
3062         for (i = 0; i < num_vlan; i++) {
3063                 tt_vlan_tmp = tt_vlan + i;
3064
3065                 /* if orig_node is a backbone node for this VLAN, don't check
3066                  * the CRC as we ignore all the global entries over it
3067                  */
3068                 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
3069                                                    orig_node->orig,
3070                                                    ntohs(tt_vlan_tmp->vid)))
3071                         continue;
3072
3073                 vlan = batadv_orig_node_vlan_get(orig_node,
3074                                                  ntohs(tt_vlan_tmp->vid));
3075                 if (!vlan)
3076                         return false;
3077
3078                 crc = vlan->tt.crc;
3079                 batadv_orig_node_vlan_put(vlan);
3080
3081                 if (crc != ntohl(tt_vlan_tmp->crc))
3082                         return false;
3083         }
3084
3085         /* check if any excess VLANs exist locally for the originator
3086          * which are not mentioned in the TVLV from the originator.
3087          */
3088         rcu_read_lock();
3089         orig_num_vlan = 0;
3090         hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list)
3091                 orig_num_vlan++;
3092         rcu_read_unlock();
3093
3094         if (orig_num_vlan > num_vlan)
3095                 return false;
3096
3097         return true;
3098 }
3099
3100 /**
3101  * batadv_tt_local_update_crc() - update all the local CRCs
3102  * @bat_priv: the bat priv with all the soft interface information
3103  */
3104 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
3105 {
3106         struct batadv_softif_vlan *vlan;
3107
3108         /* recompute the global CRC for each VLAN */
3109         rcu_read_lock();
3110         hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
3111                 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
3112         }
3113         rcu_read_unlock();
3114 }
3115
3116 /**
3117  * batadv_tt_global_update_crc() - update all the global CRCs for this orig_node
3118  * @bat_priv: the bat priv with all the soft interface information
3119  * @orig_node: the orig_node for which the CRCs have to be updated
3120  */
3121 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
3122                                         struct batadv_orig_node *orig_node)
3123 {
3124         struct batadv_orig_node_vlan *vlan;
3125         u32 crc;
3126
3127         /* recompute the global CRC for each VLAN */
3128         rcu_read_lock();
3129         hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
3130                 /* if orig_node is a backbone node for this VLAN, don't compute
3131                  * the CRC as we ignore all the global entries over it
3132                  */
3133                 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
3134                                                    vlan->vid))
3135                         continue;
3136
3137                 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
3138                 vlan->tt.crc = crc;
3139         }
3140         rcu_read_unlock();
3141 }
3142
3143 /**
3144  * batadv_send_tt_request() - send a TT Request message to a given node
3145  * @bat_priv: the bat priv with all the soft interface information
3146  * @dst_orig_node: the destination of the message
3147  * @ttvn: the version number that the source of the message is looking for
3148  * @tt_vlan: pointer to the first tvlv VLAN object to request
3149  * @num_vlan: number of tvlv VLAN entries
3150  * @full_table: ask for the entire translation table if true, while only for the
3151  *  last TT diff otherwise
3152  *
3153  * Return: true if the TT Request was sent, false otherwise
3154  */
3155 static bool batadv_send_tt_request(struct batadv_priv *bat_priv,
3156                                    struct batadv_orig_node *dst_orig_node,
3157                                    u8 ttvn,
3158                                    struct batadv_tvlv_tt_vlan_data *tt_vlan,
3159                                    u16 num_vlan, bool full_table)
3160 {
3161         struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3162         struct batadv_tt_req_node *tt_req_node = NULL;
3163         struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
3164         struct batadv_hard_iface *primary_if;
3165         bool ret = false;
3166         int i, size;
3167
3168         primary_if = batadv_primary_if_get_selected(bat_priv);
3169         if (!primary_if)
3170                 goto out;
3171
3172         /* The new tt_req will be issued only if I'm not waiting for a
3173          * reply from the same orig_node yet
3174          */
3175         tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
3176         if (!tt_req_node)
3177                 goto out;
3178
3179         size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
3180         tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
3181         if (!tvlv_tt_data)
3182                 goto out;
3183
3184         tvlv_tt_data->flags = BATADV_TT_REQUEST;
3185         tvlv_tt_data->ttvn = ttvn;
3186         tvlv_tt_data->num_vlan = htons(num_vlan);
3187
3188         /* send all the CRCs within the request. This is needed by intermediate
3189          * nodes to ensure they have the correct table before replying
3190          */
3191         tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
3192         for (i = 0; i < num_vlan; i++) {
3193                 tt_vlan_req->vid = tt_vlan->vid;
3194                 tt_vlan_req->crc = tt_vlan->crc;
3195
3196                 tt_vlan_req++;
3197                 tt_vlan++;
3198         }
3199
3200         if (full_table)
3201                 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3202
3203         batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
3204                    dst_orig_node->orig, full_table ? 'F' : '.');
3205
3206         batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
3207         batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3208                                  dst_orig_node->orig, BATADV_TVLV_TT, 1,
3209                                  tvlv_tt_data, size);
3210         ret = true;
3211
3212 out:
3213         if (primary_if)
3214                 batadv_hardif_put(primary_if);
3215
3216         if (ret && tt_req_node) {
3217                 spin_lock_bh(&bat_priv->tt.req_list_lock);
3218                 if (!hlist_unhashed(&tt_req_node->list)) {
3219                         hlist_del_init(&tt_req_node->list);
3220                         batadv_tt_req_node_put(tt_req_node);
3221                 }
3222                 spin_unlock_bh(&bat_priv->tt.req_list_lock);
3223         }
3224
3225         if (tt_req_node)
3226                 batadv_tt_req_node_put(tt_req_node);
3227
3228         kfree(tvlv_tt_data);
3229         return ret;
3230 }
3231
3232 /**
3233  * batadv_send_other_tt_response() - send reply to tt request concerning another
3234  *  node's translation table
3235  * @bat_priv: the bat priv with all the soft interface information
3236  * @tt_data: tt data containing the tt request information
3237  * @req_src: mac address of tt request sender
3238  * @req_dst: mac address of tt request recipient
3239  *
3240  * Return: true if tt request reply was sent, false otherwise.
3241  */
3242 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
3243                                           struct batadv_tvlv_tt_data *tt_data,
3244                                           u8 *req_src, u8 *req_dst)
3245 {
3246         struct batadv_orig_node *req_dst_orig_node;
3247         struct batadv_orig_node *res_dst_orig_node = NULL;
3248         struct batadv_tvlv_tt_change *tt_change;
3249         struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3250         struct batadv_tvlv_tt_vlan_data *tt_vlan;
3251         bool ret = false, full_table;
3252         u8 orig_ttvn, req_ttvn;
3253         u16 tvlv_len;
3254         s32 tt_len;
3255
3256         batadv_dbg(BATADV_DBG_TT, bat_priv,
3257                    "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
3258                    req_src, tt_data->ttvn, req_dst,
3259                    ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3260
3261         /* Let's get the orig node of the REAL destination */
3262         req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
3263         if (!req_dst_orig_node)
3264                 goto out;
3265
3266         res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
3267         if (!res_dst_orig_node)
3268                 goto out;
3269
3270         orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
3271         req_ttvn = tt_data->ttvn;
3272
3273         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3274         /* this node doesn't have the requested data */
3275         if (orig_ttvn != req_ttvn ||
3276             !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
3277                                         ntohs(tt_data->num_vlan)))
3278                 goto out;
3279
3280         /* If the full table has been explicitly requested */
3281         if (tt_data->flags & BATADV_TT_FULL_TABLE ||
3282             !req_dst_orig_node->tt_buff)
3283                 full_table = true;
3284         else
3285                 full_table = false;
3286
3287         /* TT fragmentation hasn't been implemented yet, so send as many
3288          * TT entries fit a single packet as possible only
3289          */
3290         if (!full_table) {
3291                 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
3292                 tt_len = req_dst_orig_node->tt_buff_len;
3293
3294                 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3295                                                               &tvlv_tt_data,
3296                                                               &tt_change,
3297                                                               &tt_len);
3298                 if (!tt_len)
3299                         goto unlock;
3300
3301                 /* Copy the last orig_node's OGM buffer */
3302                 memcpy(tt_change, req_dst_orig_node->tt_buff,
3303                        req_dst_orig_node->tt_buff_len);
3304                 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3305         } else {
3306                 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3307                  * in the initial part
3308                  */
3309                 tt_len = -1;
3310                 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
3311                                                               &tvlv_tt_data,
3312                                                               &tt_change,
3313                                                               &tt_len);
3314                 if (!tt_len)
3315                         goto out;
3316
3317                 /* fill the rest of the tvlv with the real TT entries */
3318                 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
3319                                         tt_change, tt_len,
3320                                         batadv_tt_global_valid,
3321                                         req_dst_orig_node);
3322         }
3323
3324         /* Don't send the response, if larger than fragmented packet. */
3325         tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
3326         if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
3327                 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
3328                                          "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
3329                                          res_dst_orig_node->orig);
3330                 goto out;
3331         }
3332
3333         tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3334         tvlv_tt_data->ttvn = req_ttvn;
3335
3336         if (full_table)
3337                 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3338
3339         batadv_dbg(BATADV_DBG_TT, bat_priv,
3340                    "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
3341                    res_dst_orig_node->orig, req_dst_orig_node->orig,
3342                    full_table ? 'F' : '.', req_ttvn);
3343
3344         batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3345
3346         batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
3347                                  req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3348                                  tvlv_len);
3349
3350         ret = true;
3351         goto out;
3352
3353 unlock:
3354         spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
3355
3356 out:
3357         if (res_dst_orig_node)
3358                 batadv_orig_node_put(res_dst_orig_node);
3359         if (req_dst_orig_node)
3360                 batadv_orig_node_put(req_dst_orig_node);
3361         kfree(tvlv_tt_data);
3362         return ret;
3363 }
3364
3365 /**
3366  * batadv_send_my_tt_response() - send reply to tt request concerning this
3367  *  node's translation table
3368  * @bat_priv: the bat priv with all the soft interface information
3369  * @tt_data: tt data containing the tt request information
3370  * @req_src: mac address of tt request sender
3371  *
3372  * Return: true if tt request reply was sent, false otherwise.
3373  */
3374 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
3375                                        struct batadv_tvlv_tt_data *tt_data,
3376                                        u8 *req_src)
3377 {
3378         struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
3379         struct batadv_hard_iface *primary_if = NULL;
3380         struct batadv_tvlv_tt_change *tt_change;
3381         struct batadv_orig_node *orig_node;
3382         u8 my_ttvn, req_ttvn;
3383         u16 tvlv_len;
3384         bool full_table;
3385         s32 tt_len;
3386
3387         batadv_dbg(BATADV_DBG_TT, bat_priv,
3388                    "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
3389                    req_src, tt_data->ttvn,
3390                    ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3391
3392         spin_lock_bh(&bat_priv->tt.commit_lock);
3393
3394         my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3395         req_ttvn = tt_data->ttvn;
3396
3397         orig_node = batadv_orig_hash_find(bat_priv, req_src);
3398         if (!orig_node)
3399                 goto out;
3400
3401         primary_if = batadv_primary_if_get_selected(bat_priv);
3402         if (!primary_if)
3403                 goto out;
3404
3405         /* If the full table has been explicitly requested or the gap
3406          * is too big send the whole local translation table
3407          */
3408         if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
3409             !bat_priv->tt.last_changeset)
3410                 full_table = true;
3411         else
3412                 full_table = false;
3413
3414         /* TT fragmentation hasn't been implemented yet, so send as many
3415          * TT entries fit a single packet as possible only
3416          */
3417         if (!full_table) {
3418                 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
3419
3420                 tt_len = bat_priv->tt.last_changeset_len;
3421                 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3422                                                              &tvlv_tt_data,
3423                                                              &tt_change,
3424                                                              &tt_len);
3425                 if (!tt_len || !tvlv_len)
3426                         goto unlock;
3427
3428                 /* Copy the last orig_node's OGM buffer */
3429                 memcpy(tt_change, bat_priv->tt.last_changeset,
3430                        bat_priv->tt.last_changeset_len);
3431                 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3432         } else {
3433                 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
3434
3435                 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
3436                  * in the initial part
3437                  */
3438                 tt_len = -1;
3439                 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
3440                                                              &tvlv_tt_data,
3441                                                              &tt_change,
3442                                                              &tt_len);
3443                 if (!tt_len || !tvlv_len)
3444                         goto out;
3445
3446                 /* fill the rest of the tvlv with the real TT entries */
3447                 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
3448                                         tt_change, tt_len,
3449                                         batadv_tt_local_valid, NULL);
3450         }
3451
3452         tvlv_tt_data->flags = BATADV_TT_RESPONSE;
3453         tvlv_tt_data->ttvn = req_ttvn;
3454
3455         if (full_table)
3456                 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
3457
3458         batadv_dbg(BATADV_DBG_TT, bat_priv,
3459                    "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
3460                    orig_node->orig, full_table ? 'F' : '.', req_ttvn);
3461
3462         batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
3463
3464         batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3465                                  req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
3466                                  tvlv_len);
3467
3468         goto out;
3469
3470 unlock:
3471         spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
3472 out:
3473         spin_unlock_bh(&bat_priv->tt.commit_lock);
3474         if (orig_node)
3475                 batadv_orig_node_put(orig_node);
3476         if (primary_if)
3477                 batadv_hardif_put(primary_if);
3478         kfree(tvlv_tt_data);
3479         /* The packet was for this host, so it doesn't need to be re-routed */
3480         return true;
3481 }
3482
3483 /**
3484  * batadv_send_tt_response() - send reply to tt request
3485  * @bat_priv: the bat priv with all the soft interface information
3486  * @tt_data: tt data containing the tt request information
3487  * @req_src: mac address of tt request sender
3488  * @req_dst: mac address of tt request recipient
3489  *
3490  * Return: true if tt request reply was sent, false otherwise.
3491  */
3492 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
3493                                     struct batadv_tvlv_tt_data *tt_data,
3494                                     u8 *req_src, u8 *req_dst)
3495 {
3496         if (batadv_is_my_mac(bat_priv, req_dst))
3497                 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
3498         return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
3499                                              req_dst);
3500 }
3501
3502 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
3503                                       struct batadv_orig_node *orig_node,
3504                                       struct batadv_tvlv_tt_change *tt_change,
3505                                       u16 tt_num_changes, u8 ttvn)
3506 {
3507         int i;
3508         int roams;
3509
3510         for (i = 0; i < tt_num_changes; i++) {
3511                 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
3512                         roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
3513                         batadv_tt_global_del(bat_priv, orig_node,
3514                                              (tt_change + i)->addr,
3515                                              ntohs((tt_change + i)->vid),
3516                                              "tt removed by changes",
3517                                              roams);
3518                 } else {
3519                         if (!batadv_tt_global_add(bat_priv, orig_node,
3520                                                   (tt_change + i)->addr,
3521                                                   ntohs((tt_change + i)->vid),
3522                                                   (tt_change + i)->flags, ttvn))
3523                                 /* In case of problem while storing a
3524                                  * global_entry, we stop the updating
3525                                  * procedure without committing the
3526                                  * ttvn change. This will avoid to send
3527                                  * corrupted data on tt_request
3528                                  */
3529                                 return;
3530                 }
3531         }
3532         set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
3533 }
3534
3535 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
3536                                   struct batadv_tvlv_tt_change *tt_change,
3537                                   u8 ttvn, u8 *resp_src,
3538                                   u16 num_entries)
3539 {
3540         struct batadv_orig_node *orig_node;
3541
3542         orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3543         if (!orig_node)
3544                 goto out;
3545
3546         /* Purge the old table first.. */
3547         batadv_tt_global_del_orig(bat_priv, orig_node, -1,
3548                                   "Received full table");
3549
3550         _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
3551                                   ttvn);
3552
3553         spin_lock_bh(&orig_node->tt_buff_lock);
3554         kfree(orig_node->tt_buff);
3555         orig_node->tt_buff_len = 0;
3556         orig_node->tt_buff = NULL;
3557         spin_unlock_bh(&orig_node->tt_buff_lock);
3558
3559         atomic_set(&orig_node->last_ttvn, ttvn);
3560
3561 out:
3562         if (orig_node)
3563                 batadv_orig_node_put(orig_node);
3564 }
3565
3566 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3567                                      struct batadv_orig_node *orig_node,
3568                                      u16 tt_num_changes, u8 ttvn,
3569                                      struct batadv_tvlv_tt_change *tt_change)
3570 {
3571         _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3572                                   tt_num_changes, ttvn);
3573
3574         batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3575                                    batadv_tt_len(tt_num_changes));
3576         atomic_set(&orig_node->last_ttvn, ttvn);
3577 }
3578
3579 /**
3580  * batadv_is_my_client() - check if a client is served by the local node
3581  * @bat_priv: the bat priv with all the soft interface information
3582  * @addr: the mac address of the client to check
3583  * @vid: VLAN identifier
3584  *
3585  * Return: true if the client is served by this node, false otherwise.
3586  */
3587 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3588                          unsigned short vid)
3589 {
3590         struct batadv_tt_local_entry *tt_local_entry;
3591         bool ret = false;
3592
3593         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3594         if (!tt_local_entry)
3595                 goto out;
3596         /* Check if the client has been logically deleted (but is kept for
3597          * consistency purpose)
3598          */
3599         if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3600             (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3601                 goto out;
3602         ret = true;
3603 out:
3604         if (tt_local_entry)
3605                 batadv_tt_local_entry_put(tt_local_entry);
3606         return ret;
3607 }
3608
3609 /**
3610  * batadv_handle_tt_response() - process incoming tt reply
3611  * @bat_priv: the bat priv with all the soft interface information
3612  * @tt_data: tt data containing the tt request information
3613  * @resp_src: mac address of tt reply sender
3614  * @num_entries: number of tt change entries appended to the tt data
3615  */
3616 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3617                                       struct batadv_tvlv_tt_data *tt_data,
3618                                       u8 *resp_src, u16 num_entries)
3619 {
3620         struct batadv_tt_req_node *node;
3621         struct hlist_node *safe;
3622         struct batadv_orig_node *orig_node = NULL;
3623         struct batadv_tvlv_tt_change *tt_change;
3624         u8 *tvlv_ptr = (u8 *)tt_data;
3625         u16 change_offset;
3626
3627         batadv_dbg(BATADV_DBG_TT, bat_priv,
3628                    "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3629                    resp_src, tt_data->ttvn, num_entries,
3630                    ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3631
3632         orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3633         if (!orig_node)
3634                 goto out;
3635
3636         spin_lock_bh(&orig_node->tt_lock);
3637
3638         change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3639         change_offset *= ntohs(tt_data->num_vlan);
3640         change_offset += sizeof(*tt_data);
3641         tvlv_ptr += change_offset;
3642
3643         tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3644         if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3645                 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3646                                       resp_src, num_entries);
3647         } else {
3648                 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3649                                          tt_data->ttvn, tt_change);
3650         }
3651
3652         /* Recalculate the CRC for this orig_node and store it */
3653         batadv_tt_global_update_crc(bat_priv, orig_node);
3654
3655         spin_unlock_bh(&orig_node->tt_lock);
3656
3657         /* Delete the tt_req_node from pending tt_requests list */
3658         spin_lock_bh(&bat_priv->tt.req_list_lock);
3659         hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3660                 if (!batadv_compare_eth(node->addr, resp_src))
3661                         continue;
3662                 hlist_del_init(&node->list);
3663                 batadv_tt_req_node_put(node);
3664         }
3665
3666         spin_unlock_bh(&bat_priv->tt.req_list_lock);
3667 out:
3668         if (orig_node)
3669                 batadv_orig_node_put(orig_node);
3670 }
3671
3672 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3673 {
3674         struct batadv_tt_roam_node *node, *safe;
3675
3676         spin_lock_bh(&bat_priv->tt.roam_list_lock);
3677
3678         list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3679                 list_del(&node->list);
3680                 kmem_cache_free(batadv_tt_roam_cache, node);
3681         }
3682
3683         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3684 }
3685
3686 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3687 {
3688         struct batadv_tt_roam_node *node, *safe;
3689
3690         spin_lock_bh(&bat_priv->tt.roam_list_lock);
3691         list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3692                 if (!batadv_has_timed_out(node->first_time,
3693                                           BATADV_ROAMING_MAX_TIME))
3694                         continue;
3695
3696                 list_del(&node->list);
3697                 kmem_cache_free(batadv_tt_roam_cache, node);
3698         }
3699         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3700 }
3701
3702 /**
3703  * batadv_tt_check_roam_count() - check if a client has roamed too frequently
3704  * @bat_priv: the bat priv with all the soft interface information
3705  * @client: mac address of the roaming client
3706  *
3707  * This function checks whether the client already reached the
3708  * maximum number of possible roaming phases. In this case the ROAMING_ADV
3709  * will not be sent.
3710  *
3711  * Return: true if the ROAMING_ADV can be sent, false otherwise
3712  */
3713 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3714 {
3715         struct batadv_tt_roam_node *tt_roam_node;
3716         bool ret = false;
3717
3718         spin_lock_bh(&bat_priv->tt.roam_list_lock);
3719         /* The new tt_req will be issued only if I'm not waiting for a
3720          * reply from the same orig_node yet
3721          */
3722         list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3723                 if (!batadv_compare_eth(tt_roam_node->addr, client))
3724                         continue;
3725
3726                 if (batadv_has_timed_out(tt_roam_node->first_time,
3727                                          BATADV_ROAMING_MAX_TIME))
3728                         continue;
3729
3730                 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3731                         /* Sorry, you roamed too many times! */
3732                         goto unlock;
3733                 ret = true;
3734                 break;
3735         }
3736
3737         if (!ret) {
3738                 tt_roam_node = kmem_cache_alloc(batadv_tt_roam_cache,
3739                                                 GFP_ATOMIC);
3740                 if (!tt_roam_node)
3741                         goto unlock;
3742
3743                 tt_roam_node->first_time = jiffies;
3744                 atomic_set(&tt_roam_node->counter,
3745                            BATADV_ROAMING_MAX_COUNT - 1);
3746                 ether_addr_copy(tt_roam_node->addr, client);
3747
3748                 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3749                 ret = true;
3750         }
3751
3752 unlock:
3753         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3754         return ret;
3755 }
3756
3757 /**
3758  * batadv_send_roam_adv() - send a roaming advertisement message
3759  * @bat_priv: the bat priv with all the soft interface information
3760  * @client: mac address of the roaming client
3761  * @vid: VLAN identifier
3762  * @orig_node: message destination
3763  *
3764  * Send a ROAMING_ADV message to the node which was previously serving this
3765  * client. This is done to inform the node that from now on all traffic destined
3766  * for this particular roamed client has to be forwarded to the sender of the
3767  * roaming message.
3768  */
3769 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3770                                  unsigned short vid,
3771                                  struct batadv_orig_node *orig_node)
3772 {
3773         struct batadv_hard_iface *primary_if;
3774         struct batadv_tvlv_roam_adv tvlv_roam;
3775
3776         primary_if = batadv_primary_if_get_selected(bat_priv);
3777         if (!primary_if)
3778                 goto out;
3779
3780         /* before going on we have to check whether the client has
3781          * already roamed to us too many times
3782          */
3783         if (!batadv_tt_check_roam_count(bat_priv, client))
3784                 goto out;
3785
3786         batadv_dbg(BATADV_DBG_TT, bat_priv,
3787                    "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3788                    orig_node->orig, client, batadv_print_vid(vid));
3789
3790         batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3791
3792         memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3793         tvlv_roam.vid = htons(vid);
3794
3795         batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3796                                  orig_node->orig, BATADV_TVLV_ROAM, 1,
3797                                  &tvlv_roam, sizeof(tvlv_roam));
3798
3799 out:
3800         if (primary_if)
3801                 batadv_hardif_put(primary_if);
3802 }
3803
3804 static void batadv_tt_purge(struct work_struct *work)
3805 {
3806         struct delayed_work *delayed_work;
3807         struct batadv_priv_tt *priv_tt;
3808         struct batadv_priv *bat_priv;
3809
3810         delayed_work = to_delayed_work(work);
3811         priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3812         bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3813
3814         batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3815         batadv_tt_global_purge(bat_priv);
3816         batadv_tt_req_purge(bat_priv);
3817         batadv_tt_roam_purge(bat_priv);
3818
3819         queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3820                            msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3821 }
3822
3823 /**
3824  * batadv_tt_free() - Free translation table of soft interface
3825  * @bat_priv: the bat priv with all the soft interface information
3826  */
3827 void batadv_tt_free(struct batadv_priv *bat_priv)
3828 {
3829         batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3830         batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3831
3832         cancel_delayed_work_sync(&bat_priv->tt.work);
3833
3834         batadv_tt_local_table_free(bat_priv);
3835         batadv_tt_global_table_free(bat_priv);
3836         batadv_tt_req_list_free(bat_priv);
3837         batadv_tt_changes_list_free(bat_priv);
3838         batadv_tt_roam_list_free(bat_priv);
3839
3840         kfree(bat_priv->tt.last_changeset);
3841 }
3842
3843 /**
3844  * batadv_tt_local_set_flags() - set or unset the specified flags on the local
3845  *  table and possibly count them in the TT size
3846  * @bat_priv: the bat priv with all the soft interface information
3847  * @flags: the flag to switch
3848  * @enable: whether to set or unset the flag
3849  * @count: whether to increase the TT size by the number of changed entries
3850  */
3851 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3852                                       bool enable, bool count)
3853 {
3854         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3855         struct batadv_tt_common_entry *tt_common_entry;
3856         struct hlist_head *head;
3857         u32 i;
3858
3859         if (!hash)
3860                 return;
3861
3862         for (i = 0; i < hash->size; i++) {
3863                 head = &hash->table[i];
3864
3865                 rcu_read_lock();
3866                 hlist_for_each_entry_rcu(tt_common_entry,
3867                                          head, hash_entry) {
3868                         if (enable) {
3869                                 if ((tt_common_entry->flags & flags) == flags)
3870                                         continue;
3871                                 tt_common_entry->flags |= flags;
3872                         } else {
3873                                 if (!(tt_common_entry->flags & flags))
3874                                         continue;
3875                                 tt_common_entry->flags &= ~flags;
3876                         }
3877
3878                         if (!count)
3879                                 continue;
3880
3881                         batadv_tt_local_size_inc(bat_priv,
3882                                                  tt_common_entry->vid);
3883                 }
3884                 rcu_read_unlock();
3885         }
3886 }
3887
3888 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3889 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3890 {
3891         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3892         struct batadv_tt_common_entry *tt_common;
3893         struct batadv_tt_local_entry *tt_local;
3894         struct hlist_node *node_tmp;
3895         struct hlist_head *head;
3896         spinlock_t *list_lock; /* protects write access to the hash lists */
3897         u32 i;
3898
3899         if (!hash)
3900                 return;
3901
3902         for (i = 0; i < hash->size; i++) {
3903                 head = &hash->table[i];
3904                 list_lock = &hash->list_locks[i];
3905
3906                 spin_lock_bh(list_lock);
3907                 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3908                                           hash_entry) {
3909                         if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3910                                 continue;
3911
3912                         batadv_dbg(BATADV_DBG_TT, bat_priv,
3913                                    "Deleting local tt entry (%pM, vid: %d): pending\n",
3914                                    tt_common->addr,
3915                                    batadv_print_vid(tt_common->vid));
3916
3917                         batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3918                         hlist_del_rcu(&tt_common->hash_entry);
3919                         tt_local = container_of(tt_common,
3920                                                 struct batadv_tt_local_entry,
3921                                                 common);
3922
3923                         batadv_tt_local_entry_put(tt_local);
3924                 }
3925                 spin_unlock_bh(list_lock);
3926         }
3927 }
3928
3929 /**
3930  * batadv_tt_local_commit_changes_nolock() - commit all pending local tt changes
3931  *  which have been queued in the time since the last commit
3932  * @bat_priv: the bat priv with all the soft interface information
3933  *
3934  * Caller must hold tt->commit_lock.
3935  */
3936 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3937 {
3938         lockdep_assert_held(&bat_priv->tt.commit_lock);
3939
3940         if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3941                 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3942                         batadv_tt_tvlv_container_update(bat_priv);
3943                 return;
3944         }
3945
3946         batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3947
3948         batadv_tt_local_purge_pending_clients(bat_priv);
3949         batadv_tt_local_update_crc(bat_priv);
3950
3951         /* Increment the TTVN only once per OGM interval */
3952         atomic_inc(&bat_priv->tt.vn);
3953         batadv_dbg(BATADV_DBG_TT, bat_priv,
3954                    "Local changes committed, updating to ttvn %u\n",
3955                    (u8)atomic_read(&bat_priv->tt.vn));
3956
3957         /* reset the sending counter */
3958         atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3959         batadv_tt_tvlv_container_update(bat_priv);
3960 }
3961
3962 /**
3963  * batadv_tt_local_commit_changes() - commit all pending local tt changes which
3964  *  have been queued in the time since the last commit
3965  * @bat_priv: the bat priv with all the soft interface information
3966  */
3967 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3968 {
3969         spin_lock_bh(&bat_priv->tt.commit_lock);
3970         batadv_tt_local_commit_changes_nolock(bat_priv);
3971         spin_unlock_bh(&bat_priv->tt.commit_lock);
3972 }
3973
3974 /**
3975  * batadv_is_ap_isolated() - Check if packet from upper layer should be dropped
3976  * @bat_priv: the bat priv with all the soft interface information
3977  * @src: source mac address of packet
3978  * @dst: destination mac address of packet
3979  * @vid: vlan id of packet
3980  *
3981  * Return: true when src+dst(+vid) pair should be isolated, false otherwise
3982  */
3983 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3984                            unsigned short vid)
3985 {
3986         struct batadv_tt_local_entry *tt_local_entry;
3987         struct batadv_tt_global_entry *tt_global_entry;
3988         struct batadv_softif_vlan *vlan;
3989         bool ret = false;
3990
3991         vlan = batadv_softif_vlan_get(bat_priv, vid);
3992         if (!vlan)
3993                 return false;
3994
3995         if (!atomic_read(&vlan->ap_isolation))
3996                 goto vlan_put;
3997
3998         tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3999         if (!tt_local_entry)
4000                 goto vlan_put;
4001
4002         tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
4003         if (!tt_global_entry)
4004                 goto local_entry_put;
4005
4006         if (_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
4007                 ret = true;
4008
4009         batadv_tt_global_entry_put(tt_global_entry);
4010 local_entry_put:
4011         batadv_tt_local_entry_put(tt_local_entry);
4012 vlan_put:
4013         batadv_softif_vlan_put(vlan);
4014         return ret;
4015 }
4016
4017 /**
4018  * batadv_tt_update_orig() - update global translation table with new tt
4019  *  information received via ogms
4020  * @bat_priv: the bat priv with all the soft interface information
4021  * @orig_node: the orig_node of the ogm
4022  * @tt_buff: pointer to the first tvlv VLAN entry
4023  * @tt_num_vlan: number of tvlv VLAN entries
4024  * @tt_change: pointer to the first entry in the TT buffer
4025  * @tt_num_changes: number of tt changes inside the tt buffer
4026  * @ttvn: translation table version number of this changeset
4027  */
4028 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
4029                                   struct batadv_orig_node *orig_node,
4030                                   const void *tt_buff, u16 tt_num_vlan,
4031                                   struct batadv_tvlv_tt_change *tt_change,
4032                                   u16 tt_num_changes, u8 ttvn)
4033 {
4034         u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
4035         struct batadv_tvlv_tt_vlan_data *tt_vlan;
4036         bool full_table = true;
4037         bool has_tt_init;
4038
4039         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
4040         has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
4041                                &orig_node->capa_initialized);
4042
4043         /* orig table not initialised AND first diff is in the OGM OR the ttvn
4044          * increased by one -> we can apply the attached changes
4045          */
4046         if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
4047                 /* the OGM could not contain the changes due to their size or
4048                  * because they have already been sent BATADV_TT_OGM_APPEND_MAX
4049                  * times.
4050                  * In this case send a tt request
4051                  */
4052                 if (!tt_num_changes) {
4053                         full_table = false;
4054                         goto request_table;
4055                 }
4056
4057                 spin_lock_bh(&orig_node->tt_lock);
4058
4059                 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
4060                                          ttvn, tt_change);
4061
4062                 /* Even if we received the precomputed crc with the OGM, we
4063                  * prefer to recompute it to spot any possible inconsistency
4064                  * in the global table
4065                  */
4066                 batadv_tt_global_update_crc(bat_priv, orig_node);
4067
4068                 spin_unlock_bh(&orig_node->tt_lock);
4069
4070                 /* The ttvn alone is not enough to guarantee consistency
4071                  * because a single value could represent different states
4072                  * (due to the wrap around). Thus a node has to check whether
4073                  * the resulting table (after applying the changes) is still
4074                  * consistent or not. E.g. a node could disconnect while its
4075                  * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
4076                  * checking the CRC value is mandatory to detect the
4077                  * inconsistency
4078                  */
4079                 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
4080                                                 tt_num_vlan))
4081                         goto request_table;
4082         } else {
4083                 /* if we missed more than one change or our tables are not
4084                  * in sync anymore -> request fresh tt data
4085                  */
4086                 if (!has_tt_init || ttvn != orig_ttvn ||
4087                     !batadv_tt_global_check_crc(orig_node, tt_vlan,
4088                                                 tt_num_vlan)) {
4089 request_table:
4090                         batadv_dbg(BATADV_DBG_TT, bat_priv,
4091                                    "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
4092                                    orig_node->orig, ttvn, orig_ttvn,
4093                                    tt_num_changes);
4094                         batadv_send_tt_request(bat_priv, orig_node, ttvn,
4095                                                tt_vlan, tt_num_vlan,
4096                                                full_table);
4097                         return;
4098                 }
4099         }
4100 }
4101
4102 /**
4103  * batadv_tt_global_client_is_roaming() - check if a client is marked as roaming
4104  * @bat_priv: the bat priv with all the soft interface information
4105  * @addr: the mac address of the client to check
4106  * @vid: VLAN identifier
4107  *
4108  * Return: true if we know that the client has moved from its old originator
4109  * to another one. This entry is still kept for consistency purposes and will be
4110  * deleted later by a DEL or because of timeout
4111  */
4112 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
4113                                         u8 *addr, unsigned short vid)
4114 {
4115         struct batadv_tt_global_entry *tt_global_entry;
4116         bool ret = false;
4117
4118         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
4119         if (!tt_global_entry)
4120                 goto out;
4121
4122         ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
4123         batadv_tt_global_entry_put(tt_global_entry);
4124 out:
4125         return ret;
4126 }
4127
4128 /**
4129  * batadv_tt_local_client_is_roaming() - tells whether the client is roaming
4130  * @bat_priv: the bat priv with all the soft interface information
4131  * @addr: the mac address of the local client to query
4132  * @vid: VLAN identifier
4133  *
4134  * Return: true if the local client is known to be roaming (it is not served by
4135  * this node anymore) or not. If yes, the client is still present in the table
4136  * to keep the latter consistent with the node TTVN
4137  */
4138 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
4139                                        u8 *addr, unsigned short vid)
4140 {
4141         struct batadv_tt_local_entry *tt_local_entry;
4142         bool ret = false;
4143
4144         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
4145         if (!tt_local_entry)
4146                 goto out;
4147
4148         ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
4149         batadv_tt_local_entry_put(tt_local_entry);
4150 out:
4151         return ret;
4152 }
4153
4154 /**
4155  * batadv_tt_add_temporary_global_entry() - Add temporary entry to global TT
4156  * @bat_priv: the bat priv with all the soft interface information
4157  * @orig_node: orig node which the temporary entry should be associated with
4158  * @addr: mac address of the client
4159  * @vid: VLAN id of the new temporary global translation table
4160  *
4161  * Return: true when temporary tt entry could be added, false otherwise
4162  */
4163 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
4164                                           struct batadv_orig_node *orig_node,
4165                                           const unsigned char *addr,
4166                                           unsigned short vid)
4167 {
4168         /* ignore loop detect macs, they are not supposed to be in the tt local
4169          * data as well.
4170          */
4171         if (batadv_bla_is_loopdetect_mac(addr))
4172                 return false;
4173
4174         if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
4175                                   BATADV_TT_CLIENT_TEMP,
4176                                   atomic_read(&orig_node->last_ttvn)))
4177                 return false;
4178
4179         batadv_dbg(BATADV_DBG_TT, bat_priv,
4180                    "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
4181                    addr, batadv_print_vid(vid), orig_node->orig);
4182
4183         return true;
4184 }
4185
4186 /**
4187  * batadv_tt_local_resize_to_mtu() - resize the local translation table fit the
4188  *  maximum packet size that can be transported through the mesh
4189  * @soft_iface: netdev struct of the mesh interface
4190  *
4191  * Remove entries older than 'timeout' and half timeout if more entries need
4192  * to be removed.
4193  */
4194 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
4195 {
4196         struct batadv_priv *bat_priv = netdev_priv(soft_iface);
4197         int packet_size_max = atomic_read(&bat_priv->packet_size_max);
4198         int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
4199         bool reduced = false;
4200
4201         spin_lock_bh(&bat_priv->tt.commit_lock);
4202
4203         while (true) {
4204                 table_size = batadv_tt_local_table_transmit_size(bat_priv);
4205                 if (packet_size_max >= table_size)
4206                         break;
4207
4208                 batadv_tt_local_purge(bat_priv, timeout);
4209                 batadv_tt_local_purge_pending_clients(bat_priv);
4210
4211                 timeout /= 2;
4212                 reduced = true;
4213                 net_ratelimited_function(batadv_info, soft_iface,
4214                                          "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
4215                                          packet_size_max);
4216         }
4217
4218         /* commit these changes immediately, to avoid synchronization problem
4219          * with the TTVN
4220          */
4221         if (reduced)
4222                 batadv_tt_local_commit_changes_nolock(bat_priv);
4223
4224         spin_unlock_bh(&bat_priv->tt.commit_lock);
4225 }
4226
4227 /**
4228  * batadv_tt_tvlv_ogm_handler_v1() - process incoming tt tvlv container
4229  * @bat_priv: the bat priv with all the soft interface information
4230  * @orig: the orig_node of the ogm
4231  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
4232  * @tvlv_value: tvlv buffer containing the gateway data
4233  * @tvlv_value_len: tvlv buffer length
4234  */
4235 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
4236                                           struct batadv_orig_node *orig,
4237                                           u8 flags, void *tvlv_value,
4238                                           u16 tvlv_value_len)
4239 {
4240         struct batadv_tvlv_tt_vlan_data *tt_vlan;
4241         struct batadv_tvlv_tt_change *tt_change;
4242         struct batadv_tvlv_tt_data *tt_data;
4243         u16 num_entries, num_vlan;
4244
4245         if (tvlv_value_len < sizeof(*tt_data))
4246                 return;
4247
4248         tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4249         tvlv_value_len -= sizeof(*tt_data);
4250
4251         num_vlan = ntohs(tt_data->num_vlan);
4252
4253         if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
4254                 return;
4255
4256         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
4257         tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
4258         tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
4259
4260         num_entries = batadv_tt_entries(tvlv_value_len);
4261
4262         batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
4263                               num_entries, tt_data->ttvn);
4264 }
4265
4266 /**
4267  * batadv_tt_tvlv_unicast_handler_v1() - process incoming (unicast) tt tvlv
4268  *  container
4269  * @bat_priv: the bat priv with all the soft interface information
4270  * @src: mac address of tt tvlv sender
4271  * @dst: mac address of tt tvlv recipient
4272  * @tvlv_value: tvlv buffer containing the tt data
4273  * @tvlv_value_len: tvlv buffer length
4274  *
4275  * Return: NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
4276  * otherwise.
4277  */
4278 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4279                                              u8 *src, u8 *dst,
4280                                              void *tvlv_value,
4281                                              u16 tvlv_value_len)
4282 {
4283         struct batadv_tvlv_tt_data *tt_data;
4284         u16 tt_vlan_len, tt_num_entries;
4285         char tt_flag;
4286         bool ret;
4287
4288         if (tvlv_value_len < sizeof(*tt_data))
4289                 return NET_RX_SUCCESS;
4290
4291         tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
4292         tvlv_value_len -= sizeof(*tt_data);
4293
4294         tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
4295         tt_vlan_len *= ntohs(tt_data->num_vlan);
4296
4297         if (tvlv_value_len < tt_vlan_len)
4298                 return NET_RX_SUCCESS;
4299
4300         tvlv_value_len -= tt_vlan_len;
4301         tt_num_entries = batadv_tt_entries(tvlv_value_len);
4302
4303         switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
4304         case BATADV_TT_REQUEST:
4305                 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
4306
4307                 /* If this node cannot provide a TT response the tt_request is
4308                  * forwarded
4309                  */
4310                 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
4311                 if (!ret) {
4312                         if (tt_data->flags & BATADV_TT_FULL_TABLE)
4313                                 tt_flag = 'F';
4314                         else
4315                                 tt_flag = '.';
4316
4317                         batadv_dbg(BATADV_DBG_TT, bat_priv,
4318                                    "Routing TT_REQUEST to %pM [%c]\n",
4319                                    dst, tt_flag);
4320                         /* tvlv API will re-route the packet */
4321                         return NET_RX_DROP;
4322                 }
4323                 break;
4324         case BATADV_TT_RESPONSE:
4325                 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
4326
4327                 if (batadv_is_my_mac(bat_priv, dst)) {
4328                         batadv_handle_tt_response(bat_priv, tt_data,
4329                                                   src, tt_num_entries);
4330                         return NET_RX_SUCCESS;
4331                 }
4332
4333                 if (tt_data->flags & BATADV_TT_FULL_TABLE)
4334                         tt_flag =  'F';
4335                 else
4336                         tt_flag = '.';
4337
4338                 batadv_dbg(BATADV_DBG_TT, bat_priv,
4339                            "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
4340
4341                 /* tvlv API will re-route the packet */
4342                 return NET_RX_DROP;
4343         }
4344
4345         return NET_RX_SUCCESS;
4346 }
4347
4348 /**
4349  * batadv_roam_tvlv_unicast_handler_v1() - process incoming tt roam tvlv
4350  *  container
4351  * @bat_priv: the bat priv with all the soft interface information
4352  * @src: mac address of tt tvlv sender
4353  * @dst: mac address of tt tvlv recipient
4354  * @tvlv_value: tvlv buffer containing the tt data
4355  * @tvlv_value_len: tvlv buffer length
4356  *
4357  * Return: NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
4358  * otherwise.
4359  */
4360 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
4361                                                u8 *src, u8 *dst,
4362                                                void *tvlv_value,
4363                                                u16 tvlv_value_len)
4364 {
4365         struct batadv_tvlv_roam_adv *roaming_adv;
4366         struct batadv_orig_node *orig_node = NULL;
4367
4368         /* If this node is not the intended recipient of the
4369          * roaming advertisement the packet is forwarded
4370          * (the tvlv API will re-route the packet).
4371          */
4372         if (!batadv_is_my_mac(bat_priv, dst))
4373                 return NET_RX_DROP;
4374
4375         if (tvlv_value_len < sizeof(*roaming_adv))
4376                 goto out;
4377
4378         orig_node = batadv_orig_hash_find(bat_priv, src);
4379         if (!orig_node)
4380                 goto out;
4381
4382         batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
4383         roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
4384
4385         batadv_dbg(BATADV_DBG_TT, bat_priv,
4386                    "Received ROAMING_ADV from %pM (client %pM)\n",
4387                    src, roaming_adv->client);
4388
4389         batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
4390                              ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
4391                              atomic_read(&orig_node->last_ttvn) + 1);
4392
4393 out:
4394         if (orig_node)
4395                 batadv_orig_node_put(orig_node);
4396         return NET_RX_SUCCESS;
4397 }
4398
4399 /**
4400  * batadv_tt_init() - initialise the translation table internals
4401  * @bat_priv: the bat priv with all the soft interface information
4402  *
4403  * Return: 0 on success or negative error number in case of failure.
4404  */
4405 int batadv_tt_init(struct batadv_priv *bat_priv)
4406 {
4407         int ret;
4408
4409         /* synchronized flags must be remote */
4410         BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
4411
4412         ret = batadv_tt_local_init(bat_priv);
4413         if (ret < 0)
4414                 return ret;
4415
4416         ret = batadv_tt_global_init(bat_priv);
4417         if (ret < 0)
4418                 return ret;
4419
4420         batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
4421                                      batadv_tt_tvlv_unicast_handler_v1,
4422                                      BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
4423
4424         batadv_tvlv_handler_register(bat_priv, NULL,
4425                                      batadv_roam_tvlv_unicast_handler_v1,
4426                                      BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
4427
4428         INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
4429         queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
4430                            msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
4431
4432         return 1;
4433 }
4434
4435 /**
4436  * batadv_tt_global_is_isolated() - check if a client is marked as isolated
4437  * @bat_priv: the bat priv with all the soft interface information
4438  * @addr: the mac address of the client
4439  * @vid: the identifier of the VLAN where this client is connected
4440  *
4441  * Return: true if the client is marked with the TT_CLIENT_ISOLA flag, false
4442  * otherwise
4443  */
4444 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
4445                                   const u8 *addr, unsigned short vid)
4446 {
4447         struct batadv_tt_global_entry *tt;
4448         bool ret;
4449
4450         tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
4451         if (!tt)
4452                 return false;
4453
4454         ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
4455
4456         batadv_tt_global_entry_put(tt);
4457
4458         return ret;
4459 }
4460
4461 /**
4462  * batadv_tt_cache_init() - Initialize tt memory object cache
4463  *
4464  * Return: 0 on success or negative error number in case of failure.
4465  */
4466 int __init batadv_tt_cache_init(void)
4467 {
4468         size_t tl_size = sizeof(struct batadv_tt_local_entry);
4469         size_t tg_size = sizeof(struct batadv_tt_global_entry);
4470         size_t tt_orig_size = sizeof(struct batadv_tt_orig_list_entry);
4471         size_t tt_change_size = sizeof(struct batadv_tt_change_node);
4472         size_t tt_req_size = sizeof(struct batadv_tt_req_node);
4473         size_t tt_roam_size = sizeof(struct batadv_tt_roam_node);
4474
4475         batadv_tl_cache = kmem_cache_create("batadv_tl_cache", tl_size, 0,
4476                                             SLAB_HWCACHE_ALIGN, NULL);
4477         if (!batadv_tl_cache)
4478                 return -ENOMEM;
4479
4480         batadv_tg_cache = kmem_cache_create("batadv_tg_cache", tg_size, 0,
4481                                             SLAB_HWCACHE_ALIGN, NULL);
4482         if (!batadv_tg_cache)
4483                 goto err_tt_tl_destroy;
4484
4485         batadv_tt_orig_cache = kmem_cache_create("batadv_tt_orig_cache",
4486                                                  tt_orig_size, 0,
4487                                                  SLAB_HWCACHE_ALIGN, NULL);
4488         if (!batadv_tt_orig_cache)
4489                 goto err_tt_tg_destroy;
4490
4491         batadv_tt_change_cache = kmem_cache_create("batadv_tt_change_cache",
4492                                                    tt_change_size, 0,
4493                                                    SLAB_HWCACHE_ALIGN, NULL);
4494         if (!batadv_tt_change_cache)
4495                 goto err_tt_orig_destroy;
4496
4497         batadv_tt_req_cache = kmem_cache_create("batadv_tt_req_cache",
4498                                                 tt_req_size, 0,
4499                                                 SLAB_HWCACHE_ALIGN, NULL);
4500         if (!batadv_tt_req_cache)
4501                 goto err_tt_change_destroy;
4502
4503         batadv_tt_roam_cache = kmem_cache_create("batadv_tt_roam_cache",
4504                                                  tt_roam_size, 0,
4505                                                  SLAB_HWCACHE_ALIGN, NULL);
4506         if (!batadv_tt_roam_cache)
4507                 goto err_tt_req_destroy;
4508
4509         return 0;
4510
4511 err_tt_req_destroy:
4512         kmem_cache_destroy(batadv_tt_req_cache);
4513         batadv_tt_req_cache = NULL;
4514 err_tt_change_destroy:
4515         kmem_cache_destroy(batadv_tt_change_cache);
4516         batadv_tt_change_cache = NULL;
4517 err_tt_orig_destroy:
4518         kmem_cache_destroy(batadv_tt_orig_cache);
4519         batadv_tt_orig_cache = NULL;
4520 err_tt_tg_destroy:
4521         kmem_cache_destroy(batadv_tg_cache);
4522         batadv_tg_cache = NULL;
4523 err_tt_tl_destroy:
4524         kmem_cache_destroy(batadv_tl_cache);
4525         batadv_tl_cache = NULL;
4526
4527         return -ENOMEM;
4528 }
4529
4530 /**
4531  * batadv_tt_cache_destroy() - Destroy tt memory object cache
4532  */
4533 void batadv_tt_cache_destroy(void)
4534 {
4535         kmem_cache_destroy(batadv_tl_cache);
4536         kmem_cache_destroy(batadv_tg_cache);
4537         kmem_cache_destroy(batadv_tt_orig_cache);
4538         kmem_cache_destroy(batadv_tt_change_cache);
4539         kmem_cache_destroy(batadv_tt_req_cache);
4540         kmem_cache_destroy(batadv_tt_roam_cache);
4541 }