OSDN Git Service

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