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