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