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