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