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