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