Linux-libre 4.4.228-gnu
[librecmc/linux-libre.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
875                 tt_vlan++;
876         }
877
878         tt_change_ptr = (u8 *)*tt_data + change_offset;
879         *tt_change = (struct batadv_tvlv_tt_change *)tt_change_ptr;
880
881 out:
882         spin_unlock_bh(&bat_priv->softif_vlan_list_lock);
883         return tvlv_len;
884 }
885
886 /**
887  * batadv_tt_tvlv_container_update - update the translation table tvlv container
888  *  after local tt changes have been committed
889  * @bat_priv: the bat priv with all the soft interface information
890  */
891 static void batadv_tt_tvlv_container_update(struct batadv_priv *bat_priv)
892 {
893         struct batadv_tt_change_node *entry, *safe;
894         struct batadv_tvlv_tt_data *tt_data;
895         struct batadv_tvlv_tt_change *tt_change;
896         int tt_diff_len, tt_change_len = 0;
897         int tt_diff_entries_num = 0;
898         int tt_diff_entries_count = 0;
899         u16 tvlv_len;
900
901         tt_diff_entries_num = atomic_read(&bat_priv->tt.local_changes);
902         tt_diff_len = batadv_tt_len(tt_diff_entries_num);
903
904         /* if we have too many changes for one packet don't send any
905          * and wait for the tt table request which will be fragmented
906          */
907         if (tt_diff_len > bat_priv->soft_iface->mtu)
908                 tt_diff_len = 0;
909
910         tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv, &tt_data,
911                                                      &tt_change, &tt_diff_len);
912         if (!tvlv_len)
913                 return;
914
915         tt_data->flags = BATADV_TT_OGM_DIFF;
916
917         if (tt_diff_len == 0)
918                 goto container_register;
919
920         spin_lock_bh(&bat_priv->tt.changes_list_lock);
921         atomic_set(&bat_priv->tt.local_changes, 0);
922
923         list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
924                                  list) {
925                 if (tt_diff_entries_count < tt_diff_entries_num) {
926                         memcpy(tt_change + tt_diff_entries_count,
927                                &entry->change,
928                                sizeof(struct batadv_tvlv_tt_change));
929                         tt_diff_entries_count++;
930                 }
931                 list_del(&entry->list);
932                 kfree(entry);
933         }
934         spin_unlock_bh(&bat_priv->tt.changes_list_lock);
935
936         /* Keep the buffer for possible tt_request */
937         spin_lock_bh(&bat_priv->tt.last_changeset_lock);
938         kfree(bat_priv->tt.last_changeset);
939         bat_priv->tt.last_changeset_len = 0;
940         bat_priv->tt.last_changeset = NULL;
941         tt_change_len = batadv_tt_len(tt_diff_entries_count);
942         /* check whether this new OGM has no changes due to size problems */
943         if (tt_diff_entries_count > 0) {
944                 /* if kmalloc() fails we will reply with the full table
945                  * instead of providing the diff
946                  */
947                 bat_priv->tt.last_changeset = kzalloc(tt_diff_len, GFP_ATOMIC);
948                 if (bat_priv->tt.last_changeset) {
949                         memcpy(bat_priv->tt.last_changeset,
950                                tt_change, tt_change_len);
951                         bat_priv->tt.last_changeset_len = tt_diff_len;
952                 }
953         }
954         spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
955
956 container_register:
957         batadv_tvlv_container_register(bat_priv, BATADV_TVLV_TT, 1, tt_data,
958                                        tvlv_len);
959         kfree(tt_data);
960 }
961
962 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
963 {
964         struct net_device *net_dev = (struct net_device *)seq->private;
965         struct batadv_priv *bat_priv = netdev_priv(net_dev);
966         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
967         struct batadv_tt_common_entry *tt_common_entry;
968         struct batadv_tt_local_entry *tt_local;
969         struct batadv_hard_iface *primary_if;
970         struct hlist_head *head;
971         unsigned short vid;
972         u32 i;
973         int last_seen_secs;
974         int last_seen_msecs;
975         unsigned long last_seen_jiffies;
976         bool no_purge;
977         u16 np_flag = BATADV_TT_CLIENT_NOPURGE;
978
979         primary_if = batadv_seq_print_text_primary_if_get(seq);
980         if (!primary_if)
981                 goto out;
982
983         seq_printf(seq,
984                    "Locally retrieved addresses (from %s) announced via TT (TTVN: %u):\n",
985                    net_dev->name, (u8)atomic_read(&bat_priv->tt.vn));
986         seq_printf(seq, "       %-13s  %s %-8s %-9s (%-10s)\n", "Client", "VID",
987                    "Flags", "Last seen", "CRC");
988
989         for (i = 0; i < hash->size; i++) {
990                 head = &hash->table[i];
991
992                 rcu_read_lock();
993                 hlist_for_each_entry_rcu(tt_common_entry,
994                                          head, hash_entry) {
995                         tt_local = container_of(tt_common_entry,
996                                                 struct batadv_tt_local_entry,
997                                                 common);
998                         vid = tt_common_entry->vid;
999                         last_seen_jiffies = jiffies - tt_local->last_seen;
1000                         last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
1001                         last_seen_secs = last_seen_msecs / 1000;
1002                         last_seen_msecs = last_seen_msecs % 1000;
1003
1004                         no_purge = tt_common_entry->flags & np_flag;
1005
1006                         seq_printf(seq,
1007                                    " * %pM %4i [%c%c%c%c%c%c] %3u.%03u   (%#.8x)\n",
1008                                    tt_common_entry->addr,
1009                                    BATADV_PRINT_VID(tt_common_entry->vid),
1010                                    ((tt_common_entry->flags &
1011                                      BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1012                                    no_purge ? 'P' : '.',
1013                                    ((tt_common_entry->flags &
1014                                      BATADV_TT_CLIENT_NEW) ? 'N' : '.'),
1015                                    ((tt_common_entry->flags &
1016                                      BATADV_TT_CLIENT_PENDING) ? 'X' : '.'),
1017                                    ((tt_common_entry->flags &
1018                                      BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1019                                    ((tt_common_entry->flags &
1020                                      BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1021                                    no_purge ? 0 : last_seen_secs,
1022                                    no_purge ? 0 : last_seen_msecs,
1023                                    tt_local->vlan->tt.crc);
1024                 }
1025                 rcu_read_unlock();
1026         }
1027 out:
1028         if (primary_if)
1029                 batadv_hardif_free_ref(primary_if);
1030         return 0;
1031 }
1032
1033 static void
1034 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
1035                             struct batadv_tt_local_entry *tt_local_entry,
1036                             u16 flags, const char *message)
1037 {
1038         batadv_tt_local_event(bat_priv, tt_local_entry, flags);
1039
1040         /* The local client has to be marked as "pending to be removed" but has
1041          * to be kept in the table in order to send it in a full table
1042          * response issued before the net ttvn increment (consistency check)
1043          */
1044         tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
1045
1046         batadv_dbg(BATADV_DBG_TT, bat_priv,
1047                    "Local tt entry (%pM, vid: %d) pending to be removed: %s\n",
1048                    tt_local_entry->common.addr,
1049                    BATADV_PRINT_VID(tt_local_entry->common.vid), message);
1050 }
1051
1052 /**
1053  * batadv_tt_local_remove - logically remove an entry from the local table
1054  * @bat_priv: the bat priv with all the soft interface information
1055  * @addr: the MAC address of the client to remove
1056  * @vid: VLAN identifier
1057  * @message: message to append to the log on deletion
1058  * @roaming: true if the deletion is due to a roaming event
1059  *
1060  * Returns the flags assigned to the local entry before being deleted
1061  */
1062 u16 batadv_tt_local_remove(struct batadv_priv *bat_priv, const u8 *addr,
1063                            unsigned short vid, const char *message,
1064                            bool roaming)
1065 {
1066         struct batadv_tt_local_entry *tt_removed_entry;
1067         struct batadv_tt_local_entry *tt_local_entry;
1068         u16 flags, curr_flags = BATADV_NO_FLAGS;
1069         struct hlist_node *tt_removed_node;
1070
1071         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
1072         if (!tt_local_entry)
1073                 goto out;
1074
1075         curr_flags = tt_local_entry->common.flags;
1076
1077         flags = BATADV_TT_CLIENT_DEL;
1078         /* if this global entry addition is due to a roaming, the node has to
1079          * mark the local entry as "roamed" in order to correctly reroute
1080          * packets later
1081          */
1082         if (roaming) {
1083                 flags |= BATADV_TT_CLIENT_ROAM;
1084                 /* mark the local client as ROAMed */
1085                 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1086         }
1087
1088         if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
1089                 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
1090                                             message);
1091                 goto out;
1092         }
1093         /* if this client has been added right now, it is possible to
1094          * immediately purge it
1095          */
1096         batadv_tt_local_event(bat_priv, tt_local_entry, BATADV_TT_CLIENT_DEL);
1097
1098         tt_removed_node = batadv_hash_remove(bat_priv->tt.local_hash,
1099                                              batadv_compare_tt,
1100                                              batadv_choose_tt,
1101                                              &tt_local_entry->common);
1102         if (!tt_removed_node)
1103                 goto out;
1104
1105         /* drop reference of remove hash entry */
1106         tt_removed_entry = hlist_entry(tt_removed_node,
1107                                        struct batadv_tt_local_entry,
1108                                        common.hash_entry);
1109         batadv_tt_local_entry_free_ref(tt_removed_entry);
1110
1111 out:
1112         if (tt_local_entry)
1113                 batadv_tt_local_entry_free_ref(tt_local_entry);
1114
1115         return curr_flags;
1116 }
1117
1118 /**
1119  * batadv_tt_local_purge_list - purge inactive tt local entries
1120  * @bat_priv: the bat priv with all the soft interface information
1121  * @head: pointer to the list containing the local tt entries
1122  * @timeout: parameter deciding whether a given tt local entry is considered
1123  *  inactive or not
1124  */
1125 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
1126                                        struct hlist_head *head,
1127                                        int timeout)
1128 {
1129         struct batadv_tt_local_entry *tt_local_entry;
1130         struct batadv_tt_common_entry *tt_common_entry;
1131         struct hlist_node *node_tmp;
1132
1133         hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
1134                                   hash_entry) {
1135                 tt_local_entry = container_of(tt_common_entry,
1136                                               struct batadv_tt_local_entry,
1137                                               common);
1138                 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
1139                         continue;
1140
1141                 /* entry already marked for deletion */
1142                 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
1143                         continue;
1144
1145                 if (!batadv_has_timed_out(tt_local_entry->last_seen, timeout))
1146                         continue;
1147
1148                 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
1149                                             BATADV_TT_CLIENT_DEL, "timed out");
1150         }
1151 }
1152
1153 /**
1154  * batadv_tt_local_purge - purge inactive tt local entries
1155  * @bat_priv: the bat priv with all the soft interface information
1156  * @timeout: parameter deciding whether a given tt local entry is considered
1157  *  inactive or not
1158  */
1159 static void batadv_tt_local_purge(struct batadv_priv *bat_priv,
1160                                   int timeout)
1161 {
1162         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1163         struct hlist_head *head;
1164         spinlock_t *list_lock; /* protects write access to the hash lists */
1165         u32 i;
1166
1167         for (i = 0; i < hash->size; i++) {
1168                 head = &hash->table[i];
1169                 list_lock = &hash->list_locks[i];
1170
1171                 spin_lock_bh(list_lock);
1172                 batadv_tt_local_purge_list(bat_priv, head, timeout);
1173                 spin_unlock_bh(list_lock);
1174         }
1175 }
1176
1177 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
1178 {
1179         struct batadv_hashtable *hash;
1180         spinlock_t *list_lock; /* protects write access to the hash lists */
1181         struct batadv_tt_common_entry *tt_common_entry;
1182         struct batadv_tt_local_entry *tt_local;
1183         struct hlist_node *node_tmp;
1184         struct hlist_head *head;
1185         u32 i;
1186
1187         if (!bat_priv->tt.local_hash)
1188                 return;
1189
1190         hash = bat_priv->tt.local_hash;
1191
1192         for (i = 0; i < hash->size; i++) {
1193                 head = &hash->table[i];
1194                 list_lock = &hash->list_locks[i];
1195
1196                 spin_lock_bh(list_lock);
1197                 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1198                                           head, hash_entry) {
1199                         hlist_del_rcu(&tt_common_entry->hash_entry);
1200                         tt_local = container_of(tt_common_entry,
1201                                                 struct batadv_tt_local_entry,
1202                                                 common);
1203
1204                         batadv_tt_local_entry_free_ref(tt_local);
1205                 }
1206                 spin_unlock_bh(list_lock);
1207         }
1208
1209         batadv_hash_destroy(hash);
1210
1211         bat_priv->tt.local_hash = NULL;
1212 }
1213
1214 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
1215 {
1216         if (bat_priv->tt.global_hash)
1217                 return 0;
1218
1219         bat_priv->tt.global_hash = batadv_hash_new(1024);
1220
1221         if (!bat_priv->tt.global_hash)
1222                 return -ENOMEM;
1223
1224         batadv_hash_set_lock_class(bat_priv->tt.global_hash,
1225                                    &batadv_tt_global_hash_lock_class_key);
1226
1227         return 0;
1228 }
1229
1230 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
1231 {
1232         struct batadv_tt_change_node *entry, *safe;
1233
1234         spin_lock_bh(&bat_priv->tt.changes_list_lock);
1235
1236         list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
1237                                  list) {
1238                 list_del(&entry->list);
1239                 kfree(entry);
1240         }
1241
1242         atomic_set(&bat_priv->tt.local_changes, 0);
1243         spin_unlock_bh(&bat_priv->tt.changes_list_lock);
1244 }
1245
1246 /* retrieves the orig_tt_list_entry belonging to orig_node from the
1247  * batadv_tt_global_entry list
1248  *
1249  * returns it with an increased refcounter, NULL if not found
1250  */
1251 static struct batadv_tt_orig_list_entry *
1252 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
1253                                  const struct batadv_orig_node *orig_node)
1254 {
1255         struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
1256         const struct hlist_head *head;
1257
1258         rcu_read_lock();
1259         head = &entry->orig_list;
1260         hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
1261                 if (tmp_orig_entry->orig_node != orig_node)
1262                         continue;
1263                 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
1264                         continue;
1265
1266                 orig_entry = tmp_orig_entry;
1267                 break;
1268         }
1269         rcu_read_unlock();
1270
1271         return orig_entry;
1272 }
1273
1274 /* find out if an orig_node is already in the list of a tt_global_entry.
1275  * returns true if found, false otherwise
1276  */
1277 static bool
1278 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
1279                                 const struct batadv_orig_node *orig_node,
1280                                 u8 *flags)
1281 {
1282         struct batadv_tt_orig_list_entry *orig_entry;
1283         bool found = false;
1284
1285         orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
1286         if (orig_entry) {
1287                 found = true;
1288
1289                 if (flags)
1290                         *flags = orig_entry->flags;
1291
1292                 batadv_tt_orig_list_entry_free_ref(orig_entry);
1293         }
1294
1295         return found;
1296 }
1297
1298 /**
1299  * batadv_tt_global_sync_flags - update TT sync flags
1300  * @tt_global: the TT global entry to update sync flags in
1301  *
1302  * Updates the sync flag bits in the tt_global flag attribute with a logical
1303  * OR of all sync flags from any of its TT orig entries.
1304  */
1305 static void
1306 batadv_tt_global_sync_flags(struct batadv_tt_global_entry *tt_global)
1307 {
1308         struct batadv_tt_orig_list_entry *orig_entry;
1309         const struct hlist_head *head;
1310         u16 flags = BATADV_NO_FLAGS;
1311
1312         rcu_read_lock();
1313         head = &tt_global->orig_list;
1314         hlist_for_each_entry_rcu(orig_entry, head, list)
1315                 flags |= orig_entry->flags;
1316         rcu_read_unlock();
1317
1318         flags |= tt_global->common.flags & (~BATADV_TT_SYNC_MASK);
1319         tt_global->common.flags = flags;
1320 }
1321
1322 /**
1323  * batadv_tt_global_orig_entry_add - add or update a TT orig entry
1324  * @tt_global: the TT global entry to add an orig entry in
1325  * @orig_node: the originator to add an orig entry for
1326  * @ttvn: translation table version number of this changeset
1327  * @flags: TT sync flags
1328  */
1329 static void
1330 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
1331                                 struct batadv_orig_node *orig_node, int ttvn,
1332                                 u8 flags)
1333 {
1334         struct batadv_tt_orig_list_entry *orig_entry;
1335
1336         spin_lock_bh(&tt_global->list_lock);
1337
1338         orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
1339         if (orig_entry) {
1340                 /* refresh the ttvn: the current value could be a bogus one that
1341                  * was added during a "temporary client detection"
1342                  */
1343                 orig_entry->ttvn = ttvn;
1344                 orig_entry->flags = flags;
1345                 goto sync_flags;
1346         }
1347
1348         orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
1349         if (!orig_entry)
1350                 goto out;
1351
1352         INIT_HLIST_NODE(&orig_entry->list);
1353         atomic_inc(&orig_node->refcount);
1354         batadv_tt_global_size_inc(orig_node, tt_global->common.vid);
1355         orig_entry->orig_node = orig_node;
1356         orig_entry->ttvn = ttvn;
1357         orig_entry->flags = flags;
1358         atomic_set(&orig_entry->refcount, 2);
1359
1360         hlist_add_head_rcu(&orig_entry->list,
1361                            &tt_global->orig_list);
1362         atomic_inc(&tt_global->orig_list_count);
1363
1364 sync_flags:
1365         batadv_tt_global_sync_flags(tt_global);
1366 out:
1367         if (orig_entry)
1368                 batadv_tt_orig_list_entry_free_ref(orig_entry);
1369
1370         spin_unlock_bh(&tt_global->list_lock);
1371 }
1372
1373 /**
1374  * batadv_tt_global_add - add a new TT global entry or update an existing one
1375  * @bat_priv: the bat priv with all the soft interface information
1376  * @orig_node: the originator announcing the client
1377  * @tt_addr: the mac address of the non-mesh client
1378  * @vid: VLAN identifier
1379  * @flags: TT flags that have to be set for this non-mesh client
1380  * @ttvn: the tt version number ever announcing this non-mesh client
1381  *
1382  * Add a new TT global entry for the given originator. If the entry already
1383  * exists add a new reference to the given originator (a global entry can have
1384  * references to multiple originators) and adjust the flags attribute to reflect
1385  * the function argument.
1386  * If a TT local entry exists for this non-mesh client remove it.
1387  *
1388  * The caller must hold orig_node refcount.
1389  *
1390  * Return true if the new entry has been added, false otherwise
1391  */
1392 static bool batadv_tt_global_add(struct batadv_priv *bat_priv,
1393                                  struct batadv_orig_node *orig_node,
1394                                  const unsigned char *tt_addr,
1395                                  unsigned short vid, u16 flags, u8 ttvn)
1396 {
1397         struct batadv_tt_global_entry *tt_global_entry;
1398         struct batadv_tt_local_entry *tt_local_entry;
1399         bool ret = false;
1400         int hash_added;
1401         struct batadv_tt_common_entry *common;
1402         u16 local_flags;
1403
1404         /* ignore global entries from backbone nodes */
1405         if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig, vid))
1406                 return true;
1407
1408         tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr, vid);
1409         tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr, vid);
1410
1411         /* if the node already has a local client for this entry, it has to wait
1412          * for a roaming advertisement instead of manually messing up the global
1413          * table
1414          */
1415         if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
1416             !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
1417                 goto out;
1418
1419         if (!tt_global_entry) {
1420                 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
1421                 if (!tt_global_entry)
1422                         goto out;
1423
1424                 common = &tt_global_entry->common;
1425                 ether_addr_copy(common->addr, tt_addr);
1426                 common->vid = vid;
1427
1428                 common->flags = flags & (~BATADV_TT_SYNC_MASK);
1429
1430                 tt_global_entry->roam_at = 0;
1431                 /* node must store current time in case of roaming. This is
1432                  * needed to purge this entry out on timeout (if nobody claims
1433                  * it)
1434                  */
1435                 if (flags & BATADV_TT_CLIENT_ROAM)
1436                         tt_global_entry->roam_at = jiffies;
1437                 atomic_set(&common->refcount, 2);
1438                 common->added_at = jiffies;
1439
1440                 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
1441                 atomic_set(&tt_global_entry->orig_list_count, 0);
1442                 spin_lock_init(&tt_global_entry->list_lock);
1443
1444                 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
1445                                              batadv_compare_tt,
1446                                              batadv_choose_tt, common,
1447                                              &common->hash_entry);
1448
1449                 if (unlikely(hash_added != 0)) {
1450                         /* remove the reference for the hash */
1451                         batadv_tt_global_entry_free_ref(tt_global_entry);
1452                         goto out_remove;
1453                 }
1454         } else {
1455                 common = &tt_global_entry->common;
1456                 /* If there is already a global entry, we can use this one for
1457                  * our processing.
1458                  * But if we are trying to add a temporary client then here are
1459                  * two options at this point:
1460                  * 1) the global client is not a temporary client: the global
1461                  *    client has to be left as it is, temporary information
1462                  *    should never override any already known client state
1463                  * 2) the global client is a temporary client: purge the
1464                  *    originator list and add the new one orig_entry
1465                  */
1466                 if (flags & BATADV_TT_CLIENT_TEMP) {
1467                         if (!(common->flags & BATADV_TT_CLIENT_TEMP))
1468                                 goto out;
1469                         if (batadv_tt_global_entry_has_orig(tt_global_entry,
1470                                                             orig_node, NULL))
1471                                 goto out_remove;
1472                         batadv_tt_global_del_orig_list(tt_global_entry);
1473                         goto add_orig_entry;
1474                 }
1475
1476                 /* if the client was temporary added before receiving the first
1477                  * OGM announcing it, we have to clear the TEMP flag. Also,
1478                  * remove the previous temporary orig node and re-add it
1479                  * if required. If the orig entry changed, the new one which
1480                  * is a non-temporary entry is preferred.
1481                  */
1482                 if (common->flags & BATADV_TT_CLIENT_TEMP) {
1483                         batadv_tt_global_del_orig_list(tt_global_entry);
1484                         common->flags &= ~BATADV_TT_CLIENT_TEMP;
1485                 }
1486
1487                 /* the change can carry possible "attribute" flags like the
1488                  * TT_CLIENT_WIFI, therefore they have to be copied in the
1489                  * client entry
1490                  */
1491                 tt_global_entry->common.flags |= flags & (~BATADV_TT_SYNC_MASK);
1492
1493                 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
1494                  * one originator left in the list and we previously received a
1495                  * delete + roaming change for this originator.
1496                  *
1497                  * We should first delete the old originator before adding the
1498                  * new one.
1499                  */
1500                 if (common->flags & BATADV_TT_CLIENT_ROAM) {
1501                         batadv_tt_global_del_orig_list(tt_global_entry);
1502                         common->flags &= ~BATADV_TT_CLIENT_ROAM;
1503                         tt_global_entry->roam_at = 0;
1504                 }
1505         }
1506 add_orig_entry:
1507         /* add the new orig_entry (if needed) or update it */
1508         batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn,
1509                                         flags & BATADV_TT_SYNC_MASK);
1510
1511         batadv_dbg(BATADV_DBG_TT, bat_priv,
1512                    "Creating new global tt entry: %pM (vid: %d, via %pM)\n",
1513                    common->addr, BATADV_PRINT_VID(common->vid),
1514                    orig_node->orig);
1515         ret = true;
1516
1517 out_remove:
1518         /* Do not remove multicast addresses from the local hash on
1519          * global additions
1520          */
1521         if (is_multicast_ether_addr(tt_addr))
1522                 goto out;
1523
1524         /* remove address from local hash if present */
1525         local_flags = batadv_tt_local_remove(bat_priv, tt_addr, vid,
1526                                              "global tt received",
1527                                              flags & BATADV_TT_CLIENT_ROAM);
1528         tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
1529
1530         if (!(flags & BATADV_TT_CLIENT_ROAM))
1531                 /* this is a normal global add. Therefore the client is not in a
1532                  * roaming state anymore.
1533                  */
1534                 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
1535
1536 out:
1537         if (tt_global_entry)
1538                 batadv_tt_global_entry_free_ref(tt_global_entry);
1539         if (tt_local_entry)
1540                 batadv_tt_local_entry_free_ref(tt_local_entry);
1541         return ret;
1542 }
1543
1544 /**
1545  * batadv_transtable_best_orig - Get best originator list entry from tt entry
1546  * @bat_priv: the bat priv with all the soft interface information
1547  * @tt_global_entry: global translation table entry to be analyzed
1548  *
1549  * This functon assumes the caller holds rcu_read_lock().
1550  * Returns best originator list entry or NULL on errors.
1551  */
1552 static struct batadv_tt_orig_list_entry *
1553 batadv_transtable_best_orig(struct batadv_priv *bat_priv,
1554                             struct batadv_tt_global_entry *tt_global_entry)
1555 {
1556         struct batadv_neigh_node *router, *best_router = NULL;
1557         struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1558         struct hlist_head *head;
1559         struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
1560
1561         head = &tt_global_entry->orig_list;
1562         hlist_for_each_entry_rcu(orig_entry, head, list) {
1563                 router = batadv_orig_router_get(orig_entry->orig_node,
1564                                                 BATADV_IF_DEFAULT);
1565                 if (!router)
1566                         continue;
1567
1568                 if (best_router &&
1569                     bao->bat_neigh_cmp(router, BATADV_IF_DEFAULT,
1570                                        best_router, BATADV_IF_DEFAULT) <= 0) {
1571                         batadv_neigh_node_free_ref(router);
1572                         continue;
1573                 }
1574
1575                 /* release the refcount for the "old" best */
1576                 if (best_router)
1577                         batadv_neigh_node_free_ref(best_router);
1578
1579                 best_entry = orig_entry;
1580                 best_router = router;
1581         }
1582
1583         if (best_router)
1584                 batadv_neigh_node_free_ref(best_router);
1585
1586         return best_entry;
1587 }
1588
1589 /**
1590  * batadv_tt_global_print_entry - print all orig nodes who announce the address
1591  *  for this global entry
1592  * @bat_priv: the bat priv with all the soft interface information
1593  * @tt_global_entry: global translation table entry to be printed
1594  * @seq: debugfs table seq_file struct
1595  *
1596  * This functon assumes the caller holds rcu_read_lock().
1597  */
1598 static void
1599 batadv_tt_global_print_entry(struct batadv_priv *bat_priv,
1600                              struct batadv_tt_global_entry *tt_global_entry,
1601                              struct seq_file *seq)
1602 {
1603         struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
1604         struct batadv_tt_common_entry *tt_common_entry;
1605         struct batadv_orig_node_vlan *vlan;
1606         struct hlist_head *head;
1607         u8 last_ttvn;
1608         u16 flags;
1609
1610         tt_common_entry = &tt_global_entry->common;
1611         flags = tt_common_entry->flags;
1612
1613         best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
1614         if (best_entry) {
1615                 vlan = batadv_orig_node_vlan_get(best_entry->orig_node,
1616                                                  tt_common_entry->vid);
1617                 if (!vlan) {
1618                         seq_printf(seq,
1619                                    " * Cannot retrieve VLAN %d for originator %pM\n",
1620                                    BATADV_PRINT_VID(tt_common_entry->vid),
1621                                    best_entry->orig_node->orig);
1622                         goto print_list;
1623                 }
1624
1625                 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
1626                 seq_printf(seq,
1627                            " %c %pM %4i   (%3u) via %pM     (%3u)   (%#.8x) [%c%c%c%c]\n",
1628                            '*', tt_global_entry->common.addr,
1629                            BATADV_PRINT_VID(tt_global_entry->common.vid),
1630                            best_entry->ttvn, best_entry->orig_node->orig,
1631                            last_ttvn, vlan->tt.crc,
1632                            ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1633                            ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1634                            ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1635                            ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1636
1637                 batadv_orig_node_vlan_free_ref(vlan);
1638         }
1639
1640 print_list:
1641         head = &tt_global_entry->orig_list;
1642
1643         hlist_for_each_entry_rcu(orig_entry, head, list) {
1644                 if (best_entry == orig_entry)
1645                         continue;
1646
1647                 vlan = batadv_orig_node_vlan_get(orig_entry->orig_node,
1648                                                  tt_common_entry->vid);
1649                 if (!vlan) {
1650                         seq_printf(seq,
1651                                    " + Cannot retrieve VLAN %d for originator %pM\n",
1652                                    BATADV_PRINT_VID(tt_common_entry->vid),
1653                                    orig_entry->orig_node->orig);
1654                         continue;
1655                 }
1656
1657                 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
1658                 seq_printf(seq,
1659                            " %c %pM %4d   (%3u) via %pM     (%3u)   (%#.8x) [%c%c%c%c]\n",
1660                            '+', tt_global_entry->common.addr,
1661                            BATADV_PRINT_VID(tt_global_entry->common.vid),
1662                            orig_entry->ttvn, orig_entry->orig_node->orig,
1663                            last_ttvn, vlan->tt.crc,
1664                            ((flags & BATADV_TT_CLIENT_ROAM) ? 'R' : '.'),
1665                            ((flags & BATADV_TT_CLIENT_WIFI) ? 'W' : '.'),
1666                            ((flags & BATADV_TT_CLIENT_ISOLA) ? 'I' : '.'),
1667                            ((flags & BATADV_TT_CLIENT_TEMP) ? 'T' : '.'));
1668
1669                 batadv_orig_node_vlan_free_ref(vlan);
1670         }
1671 }
1672
1673 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1674 {
1675         struct net_device *net_dev = (struct net_device *)seq->private;
1676         struct batadv_priv *bat_priv = netdev_priv(net_dev);
1677         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1678         struct batadv_tt_common_entry *tt_common_entry;
1679         struct batadv_tt_global_entry *tt_global;
1680         struct batadv_hard_iface *primary_if;
1681         struct hlist_head *head;
1682         u32 i;
1683
1684         primary_if = batadv_seq_print_text_primary_if_get(seq);
1685         if (!primary_if)
1686                 goto out;
1687
1688         seq_printf(seq,
1689                    "Globally announced TT entries received via the mesh %s\n",
1690                    net_dev->name);
1691         seq_printf(seq, "       %-13s  %s  %s       %-15s %s (%-10s) %s\n",
1692                    "Client", "VID", "(TTVN)", "Originator", "(Curr TTVN)",
1693                    "CRC", "Flags");
1694
1695         for (i = 0; i < hash->size; i++) {
1696                 head = &hash->table[i];
1697
1698                 rcu_read_lock();
1699                 hlist_for_each_entry_rcu(tt_common_entry,
1700                                          head, hash_entry) {
1701                         tt_global = container_of(tt_common_entry,
1702                                                  struct batadv_tt_global_entry,
1703                                                  common);
1704                         batadv_tt_global_print_entry(bat_priv, tt_global, seq);
1705                 }
1706                 rcu_read_unlock();
1707         }
1708 out:
1709         if (primary_if)
1710                 batadv_hardif_free_ref(primary_if);
1711         return 0;
1712 }
1713
1714 /**
1715  * _batadv_tt_global_del_orig_entry - remove and free an orig_entry
1716  * @tt_global_entry: the global entry to remove the orig_entry from
1717  * @orig_entry: the orig entry to remove and free
1718  *
1719  * Remove an orig_entry from its list in the given tt_global_entry and
1720  * free this orig_entry afterwards.
1721  *
1722  * Caller must hold tt_global_entry->list_lock and ensure orig_entry->list is
1723  * part of a list.
1724  */
1725 static void
1726 _batadv_tt_global_del_orig_entry(struct batadv_tt_global_entry *tt_global_entry,
1727                                  struct batadv_tt_orig_list_entry *orig_entry)
1728 {
1729         lockdep_assert_held(&tt_global_entry->list_lock);
1730
1731         batadv_tt_global_size_dec(orig_entry->orig_node,
1732                                   tt_global_entry->common.vid);
1733         atomic_dec(&tt_global_entry->orig_list_count);
1734         /* requires holding tt_global_entry->list_lock and orig_entry->list
1735          * being part of a list
1736          */
1737         hlist_del_rcu(&orig_entry->list);
1738         batadv_tt_orig_list_entry_free_ref(orig_entry);
1739 }
1740
1741 /* deletes the orig list of a tt_global_entry */
1742 static void
1743 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1744 {
1745         struct hlist_head *head;
1746         struct hlist_node *safe;
1747         struct batadv_tt_orig_list_entry *orig_entry;
1748
1749         spin_lock_bh(&tt_global_entry->list_lock);
1750         head = &tt_global_entry->orig_list;
1751         hlist_for_each_entry_safe(orig_entry, safe, head, list)
1752                 _batadv_tt_global_del_orig_entry(tt_global_entry, orig_entry);
1753         spin_unlock_bh(&tt_global_entry->list_lock);
1754 }
1755
1756 /**
1757  * batadv_tt_global_del_orig_node - remove orig_node from a global tt entry
1758  * @bat_priv: the bat priv with all the soft interface information
1759  * @tt_global_entry: the global entry to remove the orig_node from
1760  * @orig_node: the originator announcing the client
1761  * @message: message to append to the log on deletion
1762  *
1763  * Remove the given orig_node and its according orig_entry from the given
1764  * global tt entry.
1765  */
1766 static void
1767 batadv_tt_global_del_orig_node(struct batadv_priv *bat_priv,
1768                                struct batadv_tt_global_entry *tt_global_entry,
1769                                struct batadv_orig_node *orig_node,
1770                                const char *message)
1771 {
1772         struct hlist_head *head;
1773         struct hlist_node *safe;
1774         struct batadv_tt_orig_list_entry *orig_entry;
1775         unsigned short vid;
1776
1777         spin_lock_bh(&tt_global_entry->list_lock);
1778         head = &tt_global_entry->orig_list;
1779         hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1780                 if (orig_entry->orig_node == orig_node) {
1781                         vid = tt_global_entry->common.vid;
1782                         batadv_dbg(BATADV_DBG_TT, bat_priv,
1783                                    "Deleting %pM from global tt entry %pM (vid: %d): %s\n",
1784                                    orig_node->orig,
1785                                    tt_global_entry->common.addr,
1786                                    BATADV_PRINT_VID(vid), message);
1787                         _batadv_tt_global_del_orig_entry(tt_global_entry,
1788                                                          orig_entry);
1789                 }
1790         }
1791         spin_unlock_bh(&tt_global_entry->list_lock);
1792 }
1793
1794 /* If the client is to be deleted, we check if it is the last origantor entry
1795  * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1796  * timer, otherwise we simply remove the originator scheduled for deletion.
1797  */
1798 static void
1799 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1800                              struct batadv_tt_global_entry *tt_global_entry,
1801                              struct batadv_orig_node *orig_node,
1802                              const char *message)
1803 {
1804         bool last_entry = true;
1805         struct hlist_head *head;
1806         struct batadv_tt_orig_list_entry *orig_entry;
1807
1808         /* no local entry exists, case 1:
1809          * Check if this is the last one or if other entries exist.
1810          */
1811
1812         rcu_read_lock();
1813         head = &tt_global_entry->orig_list;
1814         hlist_for_each_entry_rcu(orig_entry, head, list) {
1815                 if (orig_entry->orig_node != orig_node) {
1816                         last_entry = false;
1817                         break;
1818                 }
1819         }
1820         rcu_read_unlock();
1821
1822         if (last_entry) {
1823                 /* its the last one, mark for roaming. */
1824                 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1825                 tt_global_entry->roam_at = jiffies;
1826         } else
1827                 /* there is another entry, we can simply delete this
1828                  * one and can still use the other one.
1829                  */
1830                 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1831                                                orig_node, message);
1832 }
1833
1834 /**
1835  * batadv_tt_global_del - remove a client from the global table
1836  * @bat_priv: the bat priv with all the soft interface information
1837  * @orig_node: an originator serving this client
1838  * @addr: the mac address of the client
1839  * @vid: VLAN identifier
1840  * @message: a message explaining the reason for deleting the client to print
1841  *  for debugging purpose
1842  * @roaming: true if the deletion has been triggered by a roaming event
1843  */
1844 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1845                                  struct batadv_orig_node *orig_node,
1846                                  const unsigned char *addr, unsigned short vid,
1847                                  const char *message, bool roaming)
1848 {
1849         struct batadv_tt_global_entry *tt_global_entry;
1850         struct batadv_tt_local_entry *local_entry = NULL;
1851
1852         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
1853         if (!tt_global_entry)
1854                 goto out;
1855
1856         if (!roaming) {
1857                 batadv_tt_global_del_orig_node(bat_priv, tt_global_entry,
1858                                                orig_node, message);
1859
1860                 if (hlist_empty(&tt_global_entry->orig_list))
1861                         batadv_tt_global_free(bat_priv, tt_global_entry,
1862                                               message);
1863
1864                 goto out;
1865         }
1866
1867         /* if we are deleting a global entry due to a roam
1868          * event, there are two possibilities:
1869          * 1) the client roamed from node A to node B => if there
1870          *    is only one originator left for this client, we mark
1871          *    it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1872          *    wait for node B to claim it. In case of timeout
1873          *    the entry is purged.
1874          *
1875          *    If there are other originators left, we directly delete
1876          *    the originator.
1877          * 2) the client roamed to us => we can directly delete
1878          *    the global entry, since it is useless now.
1879          */
1880         local_entry = batadv_tt_local_hash_find(bat_priv,
1881                                                 tt_global_entry->common.addr,
1882                                                 vid);
1883         if (local_entry) {
1884                 /* local entry exists, case 2: client roamed to us. */
1885                 batadv_tt_global_del_orig_list(tt_global_entry);
1886                 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1887         } else
1888                 /* no local entry exists, case 1: check for roaming */
1889                 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1890                                              orig_node, message);
1891
1892 out:
1893         if (tt_global_entry)
1894                 batadv_tt_global_entry_free_ref(tt_global_entry);
1895         if (local_entry)
1896                 batadv_tt_local_entry_free_ref(local_entry);
1897 }
1898
1899 /**
1900  * batadv_tt_global_del_orig - remove all the TT global entries belonging to the
1901  *  given originator matching the provided vid
1902  * @bat_priv: the bat priv with all the soft interface information
1903  * @orig_node: the originator owning the entries to remove
1904  * @match_vid: the VLAN identifier to match. If negative all the entries will be
1905  *  removed
1906  * @message: debug message to print as "reason"
1907  */
1908 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1909                                struct batadv_orig_node *orig_node,
1910                                s32 match_vid,
1911                                const char *message)
1912 {
1913         struct batadv_tt_global_entry *tt_global;
1914         struct batadv_tt_common_entry *tt_common_entry;
1915         u32 i;
1916         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1917         struct hlist_node *safe;
1918         struct hlist_head *head;
1919         spinlock_t *list_lock; /* protects write access to the hash lists */
1920         unsigned short vid;
1921
1922         if (!hash)
1923                 return;
1924
1925         for (i = 0; i < hash->size; i++) {
1926                 head = &hash->table[i];
1927                 list_lock = &hash->list_locks[i];
1928
1929                 spin_lock_bh(list_lock);
1930                 hlist_for_each_entry_safe(tt_common_entry, safe,
1931                                           head, hash_entry) {
1932                         /* remove only matching entries */
1933                         if (match_vid >= 0 && tt_common_entry->vid != match_vid)
1934                                 continue;
1935
1936                         tt_global = container_of(tt_common_entry,
1937                                                  struct batadv_tt_global_entry,
1938                                                  common);
1939
1940                         batadv_tt_global_del_orig_node(bat_priv, tt_global,
1941                                                        orig_node, message);
1942
1943                         if (hlist_empty(&tt_global->orig_list)) {
1944                                 vid = tt_global->common.vid;
1945                                 batadv_dbg(BATADV_DBG_TT, bat_priv,
1946                                            "Deleting global tt entry %pM (vid: %d): %s\n",
1947                                            tt_global->common.addr,
1948                                            BATADV_PRINT_VID(vid), message);
1949                                 hlist_del_rcu(&tt_common_entry->hash_entry);
1950                                 batadv_tt_global_entry_free_ref(tt_global);
1951                         }
1952                 }
1953                 spin_unlock_bh(list_lock);
1954         }
1955         clear_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
1956 }
1957
1958 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1959                                       char **msg)
1960 {
1961         bool purge = false;
1962         unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1963         unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1964
1965         if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1966             batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1967                 purge = true;
1968                 *msg = "Roaming timeout\n";
1969         }
1970
1971         if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1972             batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1973                 purge = true;
1974                 *msg = "Temporary client timeout\n";
1975         }
1976
1977         return purge;
1978 }
1979
1980 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1981 {
1982         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1983         struct hlist_head *head;
1984         struct hlist_node *node_tmp;
1985         spinlock_t *list_lock; /* protects write access to the hash lists */
1986         u32 i;
1987         char *msg = NULL;
1988         struct batadv_tt_common_entry *tt_common;
1989         struct batadv_tt_global_entry *tt_global;
1990
1991         for (i = 0; i < hash->size; i++) {
1992                 head = &hash->table[i];
1993                 list_lock = &hash->list_locks[i];
1994
1995                 spin_lock_bh(list_lock);
1996                 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1997                                           hash_entry) {
1998                         tt_global = container_of(tt_common,
1999                                                  struct batadv_tt_global_entry,
2000                                                  common);
2001
2002                         if (!batadv_tt_global_to_purge(tt_global, &msg))
2003                                 continue;
2004
2005                         batadv_dbg(BATADV_DBG_TT, bat_priv,
2006                                    "Deleting global tt entry %pM (vid: %d): %s\n",
2007                                    tt_global->common.addr,
2008                                    BATADV_PRINT_VID(tt_global->common.vid),
2009                                    msg);
2010
2011                         hlist_del_rcu(&tt_common->hash_entry);
2012
2013                         batadv_tt_global_entry_free_ref(tt_global);
2014                 }
2015                 spin_unlock_bh(list_lock);
2016         }
2017 }
2018
2019 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
2020 {
2021         struct batadv_hashtable *hash;
2022         spinlock_t *list_lock; /* protects write access to the hash lists */
2023         struct batadv_tt_common_entry *tt_common_entry;
2024         struct batadv_tt_global_entry *tt_global;
2025         struct hlist_node *node_tmp;
2026         struct hlist_head *head;
2027         u32 i;
2028
2029         if (!bat_priv->tt.global_hash)
2030                 return;
2031
2032         hash = bat_priv->tt.global_hash;
2033
2034         for (i = 0; i < hash->size; i++) {
2035                 head = &hash->table[i];
2036                 list_lock = &hash->list_locks[i];
2037
2038                 spin_lock_bh(list_lock);
2039                 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
2040                                           head, hash_entry) {
2041                         hlist_del_rcu(&tt_common_entry->hash_entry);
2042                         tt_global = container_of(tt_common_entry,
2043                                                  struct batadv_tt_global_entry,
2044                                                  common);
2045                         batadv_tt_global_entry_free_ref(tt_global);
2046                 }
2047                 spin_unlock_bh(list_lock);
2048         }
2049
2050         batadv_hash_destroy(hash);
2051
2052         bat_priv->tt.global_hash = NULL;
2053 }
2054
2055 static bool
2056 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
2057                        struct batadv_tt_global_entry *tt_global_entry)
2058 {
2059         bool ret = false;
2060
2061         if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
2062             tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
2063                 ret = true;
2064
2065         /* check if the two clients are marked as isolated */
2066         if (tt_local_entry->common.flags & BATADV_TT_CLIENT_ISOLA &&
2067             tt_global_entry->common.flags & BATADV_TT_CLIENT_ISOLA)
2068                 ret = true;
2069
2070         return ret;
2071 }
2072
2073 /**
2074  * batadv_transtable_search - get the mesh destination for a given client
2075  * @bat_priv: the bat priv with all the soft interface information
2076  * @src: mac address of the source client
2077  * @addr: mac address of the destination client
2078  * @vid: VLAN identifier
2079  *
2080  * Returns a pointer to the originator that was selected as destination in the
2081  * mesh for contacting the client 'addr', NULL otherwise.
2082  * In case of multiple originators serving the same client, the function returns
2083  * the best one (best in terms of metric towards the destination node).
2084  *
2085  * If the two clients are AP isolated the function returns NULL.
2086  */
2087 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
2088                                                   const u8 *src,
2089                                                   const u8 *addr,
2090                                                   unsigned short vid)
2091 {
2092         struct batadv_tt_local_entry *tt_local_entry = NULL;
2093         struct batadv_tt_global_entry *tt_global_entry = NULL;
2094         struct batadv_orig_node *orig_node = NULL;
2095         struct batadv_tt_orig_list_entry *best_entry;
2096
2097         if (src && batadv_vlan_ap_isola_get(bat_priv, vid)) {
2098                 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src, vid);
2099                 if (!tt_local_entry ||
2100                     (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
2101                         goto out;
2102         }
2103
2104         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
2105         if (!tt_global_entry)
2106                 goto out;
2107
2108         /* check whether the clients should not communicate due to AP
2109          * isolation
2110          */
2111         if (tt_local_entry &&
2112             _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2113                 goto out;
2114
2115         rcu_read_lock();
2116         best_entry = batadv_transtable_best_orig(bat_priv, tt_global_entry);
2117         /* found anything? */
2118         if (best_entry)
2119                 orig_node = best_entry->orig_node;
2120         if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
2121                 orig_node = NULL;
2122         rcu_read_unlock();
2123
2124 out:
2125         if (tt_global_entry)
2126                 batadv_tt_global_entry_free_ref(tt_global_entry);
2127         if (tt_local_entry)
2128                 batadv_tt_local_entry_free_ref(tt_local_entry);
2129
2130         return orig_node;
2131 }
2132
2133 /**
2134  * batadv_tt_global_crc - calculates the checksum of the local table belonging
2135  *  to the given orig_node
2136  * @bat_priv: the bat priv with all the soft interface information
2137  * @orig_node: originator for which the CRC should be computed
2138  * @vid: VLAN identifier for which the CRC32 has to be computed
2139  *
2140  * This function computes the checksum for the global table corresponding to a
2141  * specific originator. In particular, the checksum is computed as follows: For
2142  * each client connected to the originator the CRC32C of the MAC address and the
2143  * VID is computed and then all the CRC32Cs of the various clients are xor'ed
2144  * together.
2145  *
2146  * The idea behind is that CRC32C should be used as much as possible in order to
2147  * produce a unique hash of the table, but since the order which is used to feed
2148  * the CRC32C function affects the result and since every node in the network
2149  * probably sorts the clients differently, the hash function cannot be directly
2150  * computed over the entire table. Hence the CRC32C is used only on
2151  * the single client entry, while all the results are then xor'ed together
2152  * because the XOR operation can combine them all while trying to reduce the
2153  * noise as much as possible.
2154  *
2155  * Returns the checksum of the global table of a given originator.
2156  */
2157 static u32 batadv_tt_global_crc(struct batadv_priv *bat_priv,
2158                                 struct batadv_orig_node *orig_node,
2159                                 unsigned short vid)
2160 {
2161         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
2162         struct batadv_tt_orig_list_entry *tt_orig;
2163         struct batadv_tt_common_entry *tt_common;
2164         struct batadv_tt_global_entry *tt_global;
2165         struct hlist_head *head;
2166         u32 i, crc_tmp, crc = 0;
2167         u8 flags;
2168         __be16 tmp_vid;
2169
2170         for (i = 0; i < hash->size; i++) {
2171                 head = &hash->table[i];
2172
2173                 rcu_read_lock();
2174                 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2175                         tt_global = container_of(tt_common,
2176                                                  struct batadv_tt_global_entry,
2177                                                  common);
2178                         /* compute the CRC only for entries belonging to the
2179                          * VLAN identified by the vid passed as parameter
2180                          */
2181                         if (tt_common->vid != vid)
2182                                 continue;
2183
2184                         /* Roaming clients are in the global table for
2185                          * consistency only. They don't have to be
2186                          * taken into account while computing the
2187                          * global crc
2188                          */
2189                         if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
2190                                 continue;
2191                         /* Temporary clients have not been announced yet, so
2192                          * they have to be skipped while computing the global
2193                          * crc
2194                          */
2195                         if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
2196                                 continue;
2197
2198                         /* find out if this global entry is announced by this
2199                          * originator
2200                          */
2201                         tt_orig = batadv_tt_global_orig_entry_find(tt_global,
2202                                                                    orig_node);
2203                         if (!tt_orig)
2204                                 continue;
2205
2206                         /* use network order to read the VID: this ensures that
2207                          * every node reads the bytes in the same order.
2208                          */
2209                         tmp_vid = htons(tt_common->vid);
2210                         crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2211
2212                         /* compute the CRC on flags that have to be kept in sync
2213                          * among nodes
2214                          */
2215                         flags = tt_orig->flags;
2216                         crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2217
2218                         crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2219
2220                         batadv_tt_orig_list_entry_free_ref(tt_orig);
2221                 }
2222                 rcu_read_unlock();
2223         }
2224
2225         return crc;
2226 }
2227
2228 /**
2229  * batadv_tt_local_crc - calculates the checksum of the local table
2230  * @bat_priv: the bat priv with all the soft interface information
2231  * @vid: VLAN identifier for which the CRC32 has to be computed
2232  *
2233  * For details about the computation, please refer to the documentation for
2234  * batadv_tt_global_crc().
2235  *
2236  * Returns the checksum of the local table
2237  */
2238 static u32 batadv_tt_local_crc(struct batadv_priv *bat_priv,
2239                                unsigned short vid)
2240 {
2241         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2242         struct batadv_tt_common_entry *tt_common;
2243         struct hlist_head *head;
2244         u32 i, crc_tmp, crc = 0;
2245         u8 flags;
2246         __be16 tmp_vid;
2247
2248         for (i = 0; i < hash->size; i++) {
2249                 head = &hash->table[i];
2250
2251                 rcu_read_lock();
2252                 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
2253                         /* compute the CRC only for entries belonging to the
2254                          * VLAN identified by vid
2255                          */
2256                         if (tt_common->vid != vid)
2257                                 continue;
2258
2259                         /* not yet committed clients have not to be taken into
2260                          * account while computing the CRC
2261                          */
2262                         if (tt_common->flags & BATADV_TT_CLIENT_NEW)
2263                                 continue;
2264
2265                         /* use network order to read the VID: this ensures that
2266                          * every node reads the bytes in the same order.
2267                          */
2268                         tmp_vid = htons(tt_common->vid);
2269                         crc_tmp = crc32c(0, &tmp_vid, sizeof(tmp_vid));
2270
2271                         /* compute the CRC on flags that have to be kept in sync
2272                          * among nodes
2273                          */
2274                         flags = tt_common->flags & BATADV_TT_SYNC_MASK;
2275                         crc_tmp = crc32c(crc_tmp, &flags, sizeof(flags));
2276
2277                         crc ^= crc32c(crc_tmp, tt_common->addr, ETH_ALEN);
2278                 }
2279                 rcu_read_unlock();
2280         }
2281
2282         return crc;
2283 }
2284
2285 /**
2286  * batadv_tt_req_node_release - free tt_req node entry
2287  * @ref: kref pointer of the tt req_node entry
2288  */
2289 static void batadv_tt_req_node_release(struct kref *ref)
2290 {
2291         struct batadv_tt_req_node *tt_req_node;
2292
2293         tt_req_node = container_of(ref, struct batadv_tt_req_node, refcount);
2294
2295         kfree(tt_req_node);
2296 }
2297
2298 /**
2299  * batadv_tt_req_node_put - decrement the tt_req_node refcounter and
2300  *  possibly release it
2301  * @tt_req_node: tt_req_node to be free'd
2302  */
2303 static void batadv_tt_req_node_put(struct batadv_tt_req_node *tt_req_node)
2304 {
2305         kref_put(&tt_req_node->refcount, batadv_tt_req_node_release);
2306 }
2307
2308 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
2309 {
2310         struct batadv_tt_req_node *node;
2311         struct hlist_node *safe;
2312
2313         spin_lock_bh(&bat_priv->tt.req_list_lock);
2314
2315         hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2316                 hlist_del_init(&node->list);
2317                 batadv_tt_req_node_put(node);
2318         }
2319
2320         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2321 }
2322
2323 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
2324                                        struct batadv_orig_node *orig_node,
2325                                        const void *tt_buff,
2326                                        u16 tt_buff_len)
2327 {
2328         /* Replace the old buffer only if I received something in the
2329          * last OGM (the OGM could carry no changes)
2330          */
2331         spin_lock_bh(&orig_node->tt_buff_lock);
2332         if (tt_buff_len > 0) {
2333                 kfree(orig_node->tt_buff);
2334                 orig_node->tt_buff_len = 0;
2335                 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
2336                 if (orig_node->tt_buff) {
2337                         memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
2338                         orig_node->tt_buff_len = tt_buff_len;
2339                 }
2340         }
2341         spin_unlock_bh(&orig_node->tt_buff_lock);
2342 }
2343
2344 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
2345 {
2346         struct batadv_tt_req_node *node;
2347         struct hlist_node *safe;
2348
2349         spin_lock_bh(&bat_priv->tt.req_list_lock);
2350         hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2351                 if (batadv_has_timed_out(node->issued_at,
2352                                          BATADV_TT_REQUEST_TIMEOUT)) {
2353                         hlist_del_init(&node->list);
2354                         batadv_tt_req_node_put(node);
2355                 }
2356         }
2357         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2358 }
2359
2360 /**
2361  * batadv_tt_req_node_new - search and possibly create a tt_req_node object
2362  * @bat_priv: the bat priv with all the soft interface information
2363  * @orig_node: orig node this request is being issued for
2364  *
2365  * Returns the pointer to the new tt_req_node struct if no request
2366  * has already been issued for this orig_node, NULL otherwise.
2367  */
2368 static struct batadv_tt_req_node *
2369 batadv_tt_req_node_new(struct batadv_priv *bat_priv,
2370                        struct batadv_orig_node *orig_node)
2371 {
2372         struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
2373
2374         spin_lock_bh(&bat_priv->tt.req_list_lock);
2375         hlist_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
2376                 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
2377                     !batadv_has_timed_out(tt_req_node_tmp->issued_at,
2378                                           BATADV_TT_REQUEST_TIMEOUT))
2379                         goto unlock;
2380         }
2381
2382         tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
2383         if (!tt_req_node)
2384                 goto unlock;
2385
2386         kref_init(&tt_req_node->refcount);
2387         ether_addr_copy(tt_req_node->addr, orig_node->orig);
2388         tt_req_node->issued_at = jiffies;
2389
2390         kref_get(&tt_req_node->refcount);
2391         hlist_add_head(&tt_req_node->list, &bat_priv->tt.req_list);
2392 unlock:
2393         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2394         return tt_req_node;
2395 }
2396
2397 /**
2398  * batadv_tt_local_valid - verify that given tt entry is a valid one
2399  * @entry_ptr: to be checked local tt entry
2400  * @data_ptr: not used but definition required to satisfy the callback prototype
2401  *
2402  * Returns 1 if the entry is a valid, 0 otherwise.
2403  */
2404 static int batadv_tt_local_valid(const void *entry_ptr,
2405                                  const void *data_ptr,
2406                                  u8 *flags)
2407 {
2408         const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2409
2410         if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
2411                 return 0;
2412
2413         if (flags)
2414                 *flags = tt_common_entry->flags;
2415
2416         return 1;
2417 }
2418
2419 static int batadv_tt_global_valid(const void *entry_ptr,
2420                                   const void *data_ptr,
2421                                   u8 *flags)
2422 {
2423         const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
2424         const struct batadv_tt_global_entry *tt_global_entry;
2425         const struct batadv_orig_node *orig_node = data_ptr;
2426
2427         if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
2428             tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
2429                 return 0;
2430
2431         tt_global_entry = container_of(tt_common_entry,
2432                                        struct batadv_tt_global_entry,
2433                                        common);
2434
2435         return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node,
2436                                                flags);
2437 }
2438
2439 /**
2440  * batadv_tt_tvlv_generate - fill the tvlv buff with the tt entries from the
2441  *  specified tt hash
2442  * @bat_priv: the bat priv with all the soft interface information
2443  * @hash: hash table containing the tt entries
2444  * @tt_len: expected tvlv tt data buffer length in number of bytes
2445  * @tvlv_buff: pointer to the buffer to fill with the TT data
2446  * @valid_cb: function to filter tt change entries
2447  * @cb_data: data passed to the filter function as argument
2448  */
2449 static void batadv_tt_tvlv_generate(struct batadv_priv *bat_priv,
2450                                     struct batadv_hashtable *hash,
2451                                     void *tvlv_buff, u16 tt_len,
2452                                     int (*valid_cb)(const void *,
2453                                                     const void *,
2454                                                     u8 *flags),
2455                                     void *cb_data)
2456 {
2457         struct batadv_tt_common_entry *tt_common_entry;
2458         struct batadv_tvlv_tt_change *tt_change;
2459         struct hlist_head *head;
2460         u16 tt_tot, tt_num_entries = 0;
2461         u8 flags;
2462         bool ret;
2463         u32 i;
2464
2465         tt_tot = batadv_tt_entries(tt_len);
2466         tt_change = (struct batadv_tvlv_tt_change *)tvlv_buff;
2467
2468         if (!valid_cb)
2469                 return;
2470
2471         rcu_read_lock();
2472         for (i = 0; i < hash->size; i++) {
2473                 head = &hash->table[i];
2474
2475                 hlist_for_each_entry_rcu(tt_common_entry,
2476                                          head, hash_entry) {
2477                         if (tt_tot == tt_num_entries)
2478                                 break;
2479
2480                         ret = valid_cb(tt_common_entry, cb_data, &flags);
2481                         if (!ret)
2482                                 continue;
2483
2484                         ether_addr_copy(tt_change->addr, tt_common_entry->addr);
2485                         tt_change->flags = flags;
2486                         tt_change->vid = htons(tt_common_entry->vid);
2487                         memset(tt_change->reserved, 0,
2488                                sizeof(tt_change->reserved));
2489
2490                         tt_num_entries++;
2491                         tt_change++;
2492                 }
2493         }
2494         rcu_read_unlock();
2495 }
2496
2497 /**
2498  * batadv_tt_global_check_crc - check if all the CRCs are correct
2499  * @orig_node: originator for which the CRCs have to be checked
2500  * @tt_vlan: pointer to the first tvlv VLAN entry
2501  * @num_vlan: number of tvlv VLAN entries
2502  * @create: if true, create VLAN objects if not found
2503  *
2504  * Return true if all the received CRCs match the locally stored ones, false
2505  * otherwise
2506  */
2507 static bool batadv_tt_global_check_crc(struct batadv_orig_node *orig_node,
2508                                        struct batadv_tvlv_tt_vlan_data *tt_vlan,
2509                                        u16 num_vlan)
2510 {
2511         struct batadv_tvlv_tt_vlan_data *tt_vlan_tmp;
2512         struct batadv_orig_node_vlan *vlan;
2513         u32 crc;
2514         int i;
2515
2516         /* check if each received CRC matches the locally stored one */
2517         for (i = 0; i < num_vlan; i++) {
2518                 tt_vlan_tmp = tt_vlan + i;
2519
2520                 /* if orig_node is a backbone node for this VLAN, don't check
2521                  * the CRC as we ignore all the global entries over it
2522                  */
2523                 if (batadv_bla_is_backbone_gw_orig(orig_node->bat_priv,
2524                                                    orig_node->orig,
2525                                                    ntohs(tt_vlan_tmp->vid)))
2526                         continue;
2527
2528                 vlan = batadv_orig_node_vlan_get(orig_node,
2529                                                  ntohs(tt_vlan_tmp->vid));
2530                 if (!vlan)
2531                         return false;
2532
2533                 crc = vlan->tt.crc;
2534                 batadv_orig_node_vlan_free_ref(vlan);
2535
2536                 if (crc != ntohl(tt_vlan_tmp->crc))
2537                         return false;
2538         }
2539
2540         return true;
2541 }
2542
2543 /**
2544  * batadv_tt_local_update_crc - update all the local CRCs
2545  * @bat_priv: the bat priv with all the soft interface information
2546  */
2547 static void batadv_tt_local_update_crc(struct batadv_priv *bat_priv)
2548 {
2549         struct batadv_softif_vlan *vlan;
2550
2551         /* recompute the global CRC for each VLAN */
2552         rcu_read_lock();
2553         hlist_for_each_entry_rcu(vlan, &bat_priv->softif_vlan_list, list) {
2554                 vlan->tt.crc = batadv_tt_local_crc(bat_priv, vlan->vid);
2555         }
2556         rcu_read_unlock();
2557 }
2558
2559 /**
2560  * batadv_tt_global_update_crc - update all the global CRCs for this orig_node
2561  * @bat_priv: the bat priv with all the soft interface information
2562  * @orig_node: the orig_node for which the CRCs have to be updated
2563  */
2564 static void batadv_tt_global_update_crc(struct batadv_priv *bat_priv,
2565                                         struct batadv_orig_node *orig_node)
2566 {
2567         struct batadv_orig_node_vlan *vlan;
2568         u32 crc;
2569
2570         /* recompute the global CRC for each VLAN */
2571         rcu_read_lock();
2572         hlist_for_each_entry_rcu(vlan, &orig_node->vlan_list, list) {
2573                 /* if orig_node is a backbone node for this VLAN, don't compute
2574                  * the CRC as we ignore all the global entries over it
2575                  */
2576                 if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig,
2577                                                    vlan->vid))
2578                         continue;
2579
2580                 crc = batadv_tt_global_crc(bat_priv, orig_node, vlan->vid);
2581                 vlan->tt.crc = crc;
2582         }
2583         rcu_read_unlock();
2584 }
2585
2586 /**
2587  * batadv_send_tt_request - send a TT Request message to a given node
2588  * @bat_priv: the bat priv with all the soft interface information
2589  * @dst_orig_node: the destination of the message
2590  * @ttvn: the version number that the source of the message is looking for
2591  * @tt_vlan: pointer to the first tvlv VLAN object to request
2592  * @num_vlan: number of tvlv VLAN entries
2593  * @full_table: ask for the entire translation table if true, while only for the
2594  *  last TT diff otherwise
2595  */
2596 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
2597                                   struct batadv_orig_node *dst_orig_node,
2598                                   u8 ttvn,
2599                                   struct batadv_tvlv_tt_vlan_data *tt_vlan,
2600                                   u16 num_vlan, bool full_table)
2601 {
2602         struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2603         struct batadv_tt_req_node *tt_req_node = NULL;
2604         struct batadv_tvlv_tt_vlan_data *tt_vlan_req;
2605         struct batadv_hard_iface *primary_if;
2606         bool ret = false;
2607         int i, size;
2608
2609         primary_if = batadv_primary_if_get_selected(bat_priv);
2610         if (!primary_if)
2611                 goto out;
2612
2613         /* The new tt_req will be issued only if I'm not waiting for a
2614          * reply from the same orig_node yet
2615          */
2616         tt_req_node = batadv_tt_req_node_new(bat_priv, dst_orig_node);
2617         if (!tt_req_node)
2618                 goto out;
2619
2620         size = sizeof(*tvlv_tt_data) + sizeof(*tt_vlan_req) * num_vlan;
2621         tvlv_tt_data = kzalloc(size, GFP_ATOMIC);
2622         if (!tvlv_tt_data)
2623                 goto out;
2624
2625         tvlv_tt_data->flags = BATADV_TT_REQUEST;
2626         tvlv_tt_data->ttvn = ttvn;
2627         tvlv_tt_data->num_vlan = htons(num_vlan);
2628
2629         /* send all the CRCs within the request. This is needed by intermediate
2630          * nodes to ensure they have the correct table before replying
2631          */
2632         tt_vlan_req = (struct batadv_tvlv_tt_vlan_data *)(tvlv_tt_data + 1);
2633         for (i = 0; i < num_vlan; i++) {
2634                 tt_vlan_req->vid = tt_vlan->vid;
2635                 tt_vlan_req->crc = tt_vlan->crc;
2636
2637                 tt_vlan_req++;
2638                 tt_vlan++;
2639         }
2640
2641         if (full_table)
2642                 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2643
2644         batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
2645                    dst_orig_node->orig, full_table ? 'F' : '.');
2646
2647         batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
2648         batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2649                                  dst_orig_node->orig, BATADV_TVLV_TT, 1,
2650                                  tvlv_tt_data, size);
2651         ret = true;
2652
2653 out:
2654         if (primary_if)
2655                 batadv_hardif_free_ref(primary_if);
2656
2657         if (ret && tt_req_node) {
2658                 spin_lock_bh(&bat_priv->tt.req_list_lock);
2659                 if (!hlist_unhashed(&tt_req_node->list)) {
2660                         hlist_del_init(&tt_req_node->list);
2661                         batadv_tt_req_node_put(tt_req_node);
2662                 }
2663                 spin_unlock_bh(&bat_priv->tt.req_list_lock);
2664         }
2665
2666         if (tt_req_node)
2667                 batadv_tt_req_node_put(tt_req_node);
2668
2669         kfree(tvlv_tt_data);
2670         return ret;
2671 }
2672
2673 /**
2674  * batadv_send_other_tt_response - send reply to tt request concerning another
2675  *  node's translation table
2676  * @bat_priv: the bat priv with all the soft interface information
2677  * @tt_data: tt data containing the tt request information
2678  * @req_src: mac address of tt request sender
2679  * @req_dst: mac address of tt request recipient
2680  *
2681  * Returns true if tt request reply was sent, false otherwise.
2682  */
2683 static bool batadv_send_other_tt_response(struct batadv_priv *bat_priv,
2684                                           struct batadv_tvlv_tt_data *tt_data,
2685                                           u8 *req_src, u8 *req_dst)
2686 {
2687         struct batadv_orig_node *req_dst_orig_node;
2688         struct batadv_orig_node *res_dst_orig_node = NULL;
2689         struct batadv_tvlv_tt_change *tt_change;
2690         struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2691         struct batadv_tvlv_tt_vlan_data *tt_vlan;
2692         bool ret = false, full_table;
2693         u8 orig_ttvn, req_ttvn;
2694         u16 tvlv_len;
2695         s32 tt_len;
2696
2697         batadv_dbg(BATADV_DBG_TT, bat_priv,
2698                    "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
2699                    req_src, tt_data->ttvn, req_dst,
2700                    ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2701
2702         /* Let's get the orig node of the REAL destination */
2703         req_dst_orig_node = batadv_orig_hash_find(bat_priv, req_dst);
2704         if (!req_dst_orig_node)
2705                 goto out;
2706
2707         res_dst_orig_node = batadv_orig_hash_find(bat_priv, req_src);
2708         if (!res_dst_orig_node)
2709                 goto out;
2710
2711         orig_ttvn = (u8)atomic_read(&req_dst_orig_node->last_ttvn);
2712         req_ttvn = tt_data->ttvn;
2713
2714         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
2715         /* this node doesn't have the requested data */
2716         if (orig_ttvn != req_ttvn ||
2717             !batadv_tt_global_check_crc(req_dst_orig_node, tt_vlan,
2718                                         ntohs(tt_data->num_vlan)))
2719                 goto out;
2720
2721         /* If the full table has been explicitly requested */
2722         if (tt_data->flags & BATADV_TT_FULL_TABLE ||
2723             !req_dst_orig_node->tt_buff)
2724                 full_table = true;
2725         else
2726                 full_table = false;
2727
2728         /* TT fragmentation hasn't been implemented yet, so send as many
2729          * TT entries fit a single packet as possible only
2730          */
2731         if (!full_table) {
2732                 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
2733                 tt_len = req_dst_orig_node->tt_buff_len;
2734
2735                 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2736                                                               &tvlv_tt_data,
2737                                                               &tt_change,
2738                                                               &tt_len);
2739                 if (!tt_len)
2740                         goto unlock;
2741
2742                 /* Copy the last orig_node's OGM buffer */
2743                 memcpy(tt_change, req_dst_orig_node->tt_buff,
2744                        req_dst_orig_node->tt_buff_len);
2745                 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2746         } else {
2747                 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2748                  * in the initial part
2749                  */
2750                 tt_len = -1;
2751                 tvlv_len = batadv_tt_prepare_tvlv_global_data(req_dst_orig_node,
2752                                                               &tvlv_tt_data,
2753                                                               &tt_change,
2754                                                               &tt_len);
2755                 if (!tt_len)
2756                         goto out;
2757
2758                 /* fill the rest of the tvlv with the real TT entries */
2759                 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.global_hash,
2760                                         tt_change, tt_len,
2761                                         batadv_tt_global_valid,
2762                                         req_dst_orig_node);
2763         }
2764
2765         /* Don't send the response, if larger than fragmented packet. */
2766         tt_len = sizeof(struct batadv_unicast_tvlv_packet) + tvlv_len;
2767         if (tt_len > atomic_read(&bat_priv->packet_size_max)) {
2768                 net_ratelimited_function(batadv_info, bat_priv->soft_iface,
2769                                          "Ignoring TT_REQUEST from %pM; Response size exceeds max packet size.\n",
2770                                          res_dst_orig_node->orig);
2771                 goto out;
2772         }
2773
2774         tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2775         tvlv_tt_data->ttvn = req_ttvn;
2776
2777         if (full_table)
2778                 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2779
2780         batadv_dbg(BATADV_DBG_TT, bat_priv,
2781                    "Sending TT_RESPONSE %pM for %pM [%c] (ttvn: %u)\n",
2782                    res_dst_orig_node->orig, req_dst_orig_node->orig,
2783                    full_table ? 'F' : '.', req_ttvn);
2784
2785         batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2786
2787         batadv_tvlv_unicast_send(bat_priv, req_dst_orig_node->orig,
2788                                  req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2789                                  tvlv_len);
2790
2791         ret = true;
2792         goto out;
2793
2794 unlock:
2795         spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
2796
2797 out:
2798         if (res_dst_orig_node)
2799                 batadv_orig_node_free_ref(res_dst_orig_node);
2800         if (req_dst_orig_node)
2801                 batadv_orig_node_free_ref(req_dst_orig_node);
2802         kfree(tvlv_tt_data);
2803         return ret;
2804 }
2805
2806 /**
2807  * batadv_send_my_tt_response - send reply to tt request concerning this node's
2808  *  translation table
2809  * @bat_priv: the bat priv with all the soft interface information
2810  * @tt_data: tt data containing the tt request information
2811  * @req_src: mac address of tt request sender
2812  *
2813  * Returns true if tt request reply was sent, false otherwise.
2814  */
2815 static bool batadv_send_my_tt_response(struct batadv_priv *bat_priv,
2816                                        struct batadv_tvlv_tt_data *tt_data,
2817                                        u8 *req_src)
2818 {
2819         struct batadv_tvlv_tt_data *tvlv_tt_data = NULL;
2820         struct batadv_hard_iface *primary_if = NULL;
2821         struct batadv_tvlv_tt_change *tt_change;
2822         struct batadv_orig_node *orig_node;
2823         u8 my_ttvn, req_ttvn;
2824         u16 tvlv_len;
2825         bool full_table;
2826         s32 tt_len;
2827
2828         batadv_dbg(BATADV_DBG_TT, bat_priv,
2829                    "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
2830                    req_src, tt_data->ttvn,
2831                    ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
2832
2833         spin_lock_bh(&bat_priv->tt.commit_lock);
2834
2835         my_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2836         req_ttvn = tt_data->ttvn;
2837
2838         orig_node = batadv_orig_hash_find(bat_priv, req_src);
2839         if (!orig_node)
2840                 goto out;
2841
2842         primary_if = batadv_primary_if_get_selected(bat_priv);
2843         if (!primary_if)
2844                 goto out;
2845
2846         /* If the full table has been explicitly requested or the gap
2847          * is too big send the whole local translation table
2848          */
2849         if (tt_data->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
2850             !bat_priv->tt.last_changeset)
2851                 full_table = true;
2852         else
2853                 full_table = false;
2854
2855         /* TT fragmentation hasn't been implemented yet, so send as many
2856          * TT entries fit a single packet as possible only
2857          */
2858         if (!full_table) {
2859                 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
2860
2861                 tt_len = bat_priv->tt.last_changeset_len;
2862                 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2863                                                              &tvlv_tt_data,
2864                                                              &tt_change,
2865                                                              &tt_len);
2866                 if (!tt_len || !tvlv_len)
2867                         goto unlock;
2868
2869                 /* Copy the last orig_node's OGM buffer */
2870                 memcpy(tt_change, bat_priv->tt.last_changeset,
2871                        bat_priv->tt.last_changeset_len);
2872                 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2873         } else {
2874                 req_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
2875
2876                 /* allocate the tvlv, put the tt_data and all the tt_vlan_data
2877                  * in the initial part
2878                  */
2879                 tt_len = -1;
2880                 tvlv_len = batadv_tt_prepare_tvlv_local_data(bat_priv,
2881                                                              &tvlv_tt_data,
2882                                                              &tt_change,
2883                                                              &tt_len);
2884                 if (!tt_len || !tvlv_len)
2885                         goto out;
2886
2887                 /* fill the rest of the tvlv with the real TT entries */
2888                 batadv_tt_tvlv_generate(bat_priv, bat_priv->tt.local_hash,
2889                                         tt_change, tt_len,
2890                                         batadv_tt_local_valid, NULL);
2891         }
2892
2893         tvlv_tt_data->flags = BATADV_TT_RESPONSE;
2894         tvlv_tt_data->ttvn = req_ttvn;
2895
2896         if (full_table)
2897                 tvlv_tt_data->flags |= BATADV_TT_FULL_TABLE;
2898
2899         batadv_dbg(BATADV_DBG_TT, bat_priv,
2900                    "Sending TT_RESPONSE to %pM [%c] (ttvn: %u)\n",
2901                    orig_node->orig, full_table ? 'F' : '.', req_ttvn);
2902
2903         batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
2904
2905         batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
2906                                  req_src, BATADV_TVLV_TT, 1, tvlv_tt_data,
2907                                  tvlv_len);
2908
2909         goto out;
2910
2911 unlock:
2912         spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
2913 out:
2914         spin_unlock_bh(&bat_priv->tt.commit_lock);
2915         if (orig_node)
2916                 batadv_orig_node_free_ref(orig_node);
2917         if (primary_if)
2918                 batadv_hardif_free_ref(primary_if);
2919         kfree(tvlv_tt_data);
2920         /* The packet was for this host, so it doesn't need to be re-routed */
2921         return true;
2922 }
2923
2924 /**
2925  * batadv_send_tt_response - send reply to tt request
2926  * @bat_priv: the bat priv with all the soft interface information
2927  * @tt_data: tt data containing the tt request information
2928  * @req_src: mac address of tt request sender
2929  * @req_dst: mac address of tt request recipient
2930  *
2931  * Returns true if tt request reply was sent, false otherwise.
2932  */
2933 static bool batadv_send_tt_response(struct batadv_priv *bat_priv,
2934                                     struct batadv_tvlv_tt_data *tt_data,
2935                                     u8 *req_src, u8 *req_dst)
2936 {
2937         if (batadv_is_my_mac(bat_priv, req_dst))
2938                 return batadv_send_my_tt_response(bat_priv, tt_data, req_src);
2939         return batadv_send_other_tt_response(bat_priv, tt_data, req_src,
2940                                              req_dst);
2941 }
2942
2943 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
2944                                       struct batadv_orig_node *orig_node,
2945                                       struct batadv_tvlv_tt_change *tt_change,
2946                                       u16 tt_num_changes, u8 ttvn)
2947 {
2948         int i;
2949         int roams;
2950
2951         for (i = 0; i < tt_num_changes; i++) {
2952                 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
2953                         roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
2954                         batadv_tt_global_del(bat_priv, orig_node,
2955                                              (tt_change + i)->addr,
2956                                              ntohs((tt_change + i)->vid),
2957                                              "tt removed by changes",
2958                                              roams);
2959                 } else {
2960                         if (!batadv_tt_global_add(bat_priv, orig_node,
2961                                                   (tt_change + i)->addr,
2962                                                   ntohs((tt_change + i)->vid),
2963                                                   (tt_change + i)->flags, ttvn))
2964                                 /* In case of problem while storing a
2965                                  * global_entry, we stop the updating
2966                                  * procedure without committing the
2967                                  * ttvn change. This will avoid to send
2968                                  * corrupted data on tt_request
2969                                  */
2970                                 return;
2971                 }
2972         }
2973         set_bit(BATADV_ORIG_CAPA_HAS_TT, &orig_node->capa_initialized);
2974 }
2975
2976 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
2977                                   struct batadv_tvlv_tt_change *tt_change,
2978                                   u8 ttvn, u8 *resp_src,
2979                                   u16 num_entries)
2980 {
2981         struct batadv_orig_node *orig_node;
2982
2983         orig_node = batadv_orig_hash_find(bat_priv, resp_src);
2984         if (!orig_node)
2985                 goto out;
2986
2987         /* Purge the old table first.. */
2988         batadv_tt_global_del_orig(bat_priv, orig_node, -1,
2989                                   "Received full table");
2990
2991         _batadv_tt_update_changes(bat_priv, orig_node, tt_change, num_entries,
2992                                   ttvn);
2993
2994         spin_lock_bh(&orig_node->tt_buff_lock);
2995         kfree(orig_node->tt_buff);
2996         orig_node->tt_buff_len = 0;
2997         orig_node->tt_buff = NULL;
2998         spin_unlock_bh(&orig_node->tt_buff_lock);
2999
3000         atomic_set(&orig_node->last_ttvn, ttvn);
3001
3002 out:
3003         if (orig_node)
3004                 batadv_orig_node_free_ref(orig_node);
3005 }
3006
3007 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
3008                                      struct batadv_orig_node *orig_node,
3009                                      u16 tt_num_changes, u8 ttvn,
3010                                      struct batadv_tvlv_tt_change *tt_change)
3011 {
3012         _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
3013                                   tt_num_changes, ttvn);
3014
3015         batadv_tt_save_orig_buffer(bat_priv, orig_node, tt_change,
3016                                    batadv_tt_len(tt_num_changes));
3017         atomic_set(&orig_node->last_ttvn, ttvn);
3018 }
3019
3020 /**
3021  * batadv_is_my_client - check if a client is served by the local node
3022  * @bat_priv: the bat priv with all the soft interface information
3023  * @addr: the mac address of the client to check
3024  * @vid: VLAN identifier
3025  *
3026  * Returns true if the client is served by this node, false otherwise.
3027  */
3028 bool batadv_is_my_client(struct batadv_priv *bat_priv, const u8 *addr,
3029                          unsigned short vid)
3030 {
3031         struct batadv_tt_local_entry *tt_local_entry;
3032         bool ret = false;
3033
3034         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3035         if (!tt_local_entry)
3036                 goto out;
3037         /* Check if the client has been logically deleted (but is kept for
3038          * consistency purpose)
3039          */
3040         if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
3041             (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
3042                 goto out;
3043         ret = true;
3044 out:
3045         if (tt_local_entry)
3046                 batadv_tt_local_entry_free_ref(tt_local_entry);
3047         return ret;
3048 }
3049
3050 /**
3051  * batadv_handle_tt_response - process incoming tt reply
3052  * @bat_priv: the bat priv with all the soft interface information
3053  * @tt_data: tt data containing the tt request information
3054  * @resp_src: mac address of tt reply sender
3055  * @num_entries: number of tt change entries appended to the tt data
3056  */
3057 static void batadv_handle_tt_response(struct batadv_priv *bat_priv,
3058                                       struct batadv_tvlv_tt_data *tt_data,
3059                                       u8 *resp_src, u16 num_entries)
3060 {
3061         struct batadv_tt_req_node *node;
3062         struct hlist_node *safe;
3063         struct batadv_orig_node *orig_node = NULL;
3064         struct batadv_tvlv_tt_change *tt_change;
3065         u8 *tvlv_ptr = (u8 *)tt_data;
3066         u16 change_offset;
3067
3068         batadv_dbg(BATADV_DBG_TT, bat_priv,
3069                    "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
3070                    resp_src, tt_data->ttvn, num_entries,
3071                    ((tt_data->flags & BATADV_TT_FULL_TABLE) ? 'F' : '.'));
3072
3073         orig_node = batadv_orig_hash_find(bat_priv, resp_src);
3074         if (!orig_node)
3075                 goto out;
3076
3077         spin_lock_bh(&orig_node->tt_lock);
3078
3079         change_offset = sizeof(struct batadv_tvlv_tt_vlan_data);
3080         change_offset *= ntohs(tt_data->num_vlan);
3081         change_offset += sizeof(*tt_data);
3082         tvlv_ptr += change_offset;
3083
3084         tt_change = (struct batadv_tvlv_tt_change *)tvlv_ptr;
3085         if (tt_data->flags & BATADV_TT_FULL_TABLE) {
3086                 batadv_tt_fill_gtable(bat_priv, tt_change, tt_data->ttvn,
3087                                       resp_src, num_entries);
3088         } else {
3089                 batadv_tt_update_changes(bat_priv, orig_node, num_entries,
3090                                          tt_data->ttvn, tt_change);
3091         }
3092
3093         /* Recalculate the CRC for this orig_node and store it */
3094         batadv_tt_global_update_crc(bat_priv, orig_node);
3095
3096         spin_unlock_bh(&orig_node->tt_lock);
3097
3098         /* Delete the tt_req_node from pending tt_requests list */
3099         spin_lock_bh(&bat_priv->tt.req_list_lock);
3100         hlist_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
3101                 if (!batadv_compare_eth(node->addr, resp_src))
3102                         continue;
3103                 hlist_del_init(&node->list);
3104                 batadv_tt_req_node_put(node);
3105         }
3106
3107         spin_unlock_bh(&bat_priv->tt.req_list_lock);
3108 out:
3109         if (orig_node)
3110                 batadv_orig_node_free_ref(orig_node);
3111 }
3112
3113 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
3114 {
3115         struct batadv_tt_roam_node *node, *safe;
3116
3117         spin_lock_bh(&bat_priv->tt.roam_list_lock);
3118
3119         list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3120                 list_del(&node->list);
3121                 kfree(node);
3122         }
3123
3124         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3125 }
3126
3127 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
3128 {
3129         struct batadv_tt_roam_node *node, *safe;
3130
3131         spin_lock_bh(&bat_priv->tt.roam_list_lock);
3132         list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
3133                 if (!batadv_has_timed_out(node->first_time,
3134                                           BATADV_ROAMING_MAX_TIME))
3135                         continue;
3136
3137                 list_del(&node->list);
3138                 kfree(node);
3139         }
3140         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3141 }
3142
3143 /* This function checks whether the client already reached the
3144  * maximum number of possible roaming phases. In this case the ROAMING_ADV
3145  * will not be sent.
3146  *
3147  * returns true if the ROAMING_ADV can be sent, false otherwise
3148  */
3149 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv, u8 *client)
3150 {
3151         struct batadv_tt_roam_node *tt_roam_node;
3152         bool ret = false;
3153
3154         spin_lock_bh(&bat_priv->tt.roam_list_lock);
3155         /* The new tt_req will be issued only if I'm not waiting for a
3156          * reply from the same orig_node yet
3157          */
3158         list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
3159                 if (!batadv_compare_eth(tt_roam_node->addr, client))
3160                         continue;
3161
3162                 if (batadv_has_timed_out(tt_roam_node->first_time,
3163                                          BATADV_ROAMING_MAX_TIME))
3164                         continue;
3165
3166                 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
3167                         /* Sorry, you roamed too many times! */
3168                         goto unlock;
3169                 ret = true;
3170                 break;
3171         }
3172
3173         if (!ret) {
3174                 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
3175                 if (!tt_roam_node)
3176                         goto unlock;
3177
3178                 tt_roam_node->first_time = jiffies;
3179                 atomic_set(&tt_roam_node->counter,
3180                            BATADV_ROAMING_MAX_COUNT - 1);
3181                 ether_addr_copy(tt_roam_node->addr, client);
3182
3183                 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
3184                 ret = true;
3185         }
3186
3187 unlock:
3188         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
3189         return ret;
3190 }
3191
3192 /**
3193  * batadv_send_roam_adv - send a roaming advertisement message
3194  * @bat_priv: the bat priv with all the soft interface information
3195  * @client: mac address of the roaming client
3196  * @vid: VLAN identifier
3197  * @orig_node: message destination
3198  *
3199  * Send a ROAMING_ADV message to the node which was previously serving this
3200  * client. This is done to inform the node that from now on all traffic destined
3201  * for this particular roamed client has to be forwarded to the sender of the
3202  * roaming message.
3203  */
3204 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, u8 *client,
3205                                  unsigned short vid,
3206                                  struct batadv_orig_node *orig_node)
3207 {
3208         struct batadv_hard_iface *primary_if;
3209         struct batadv_tvlv_roam_adv tvlv_roam;
3210
3211         primary_if = batadv_primary_if_get_selected(bat_priv);
3212         if (!primary_if)
3213                 goto out;
3214
3215         /* before going on we have to check whether the client has
3216          * already roamed to us too many times
3217          */
3218         if (!batadv_tt_check_roam_count(bat_priv, client))
3219                 goto out;
3220
3221         batadv_dbg(BATADV_DBG_TT, bat_priv,
3222                    "Sending ROAMING_ADV to %pM (client %pM, vid: %d)\n",
3223                    orig_node->orig, client, BATADV_PRINT_VID(vid));
3224
3225         batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
3226
3227         memcpy(tvlv_roam.client, client, sizeof(tvlv_roam.client));
3228         tvlv_roam.vid = htons(vid);
3229
3230         batadv_tvlv_unicast_send(bat_priv, primary_if->net_dev->dev_addr,
3231                                  orig_node->orig, BATADV_TVLV_ROAM, 1,
3232                                  &tvlv_roam, sizeof(tvlv_roam));
3233
3234 out:
3235         if (primary_if)
3236                 batadv_hardif_free_ref(primary_if);
3237 }
3238
3239 static void batadv_tt_purge(struct work_struct *work)
3240 {
3241         struct delayed_work *delayed_work;
3242         struct batadv_priv_tt *priv_tt;
3243         struct batadv_priv *bat_priv;
3244
3245         delayed_work = container_of(work, struct delayed_work, work);
3246         priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
3247         bat_priv = container_of(priv_tt, struct batadv_priv, tt);
3248
3249         batadv_tt_local_purge(bat_priv, BATADV_TT_LOCAL_TIMEOUT);
3250         batadv_tt_global_purge(bat_priv);
3251         batadv_tt_req_purge(bat_priv);
3252         batadv_tt_roam_purge(bat_priv);
3253
3254         queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3255                            msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3256 }
3257
3258 void batadv_tt_free(struct batadv_priv *bat_priv)
3259 {
3260         batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_ROAM, 1);
3261
3262         batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_TT, 1);
3263         batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_TT, 1);
3264
3265         cancel_delayed_work_sync(&bat_priv->tt.work);
3266
3267         batadv_tt_local_table_free(bat_priv);
3268         batadv_tt_global_table_free(bat_priv);
3269         batadv_tt_req_list_free(bat_priv);
3270         batadv_tt_changes_list_free(bat_priv);
3271         batadv_tt_roam_list_free(bat_priv);
3272
3273         kfree(bat_priv->tt.last_changeset);
3274 }
3275
3276 /**
3277  * batadv_tt_local_set_flags - set or unset the specified flags on the local
3278  *  table and possibly count them in the TT size
3279  * @bat_priv: the bat priv with all the soft interface information
3280  * @flags: the flag to switch
3281  * @enable: whether to set or unset the flag
3282  * @count: whether to increase the TT size by the number of changed entries
3283  */
3284 static void batadv_tt_local_set_flags(struct batadv_priv *bat_priv, u16 flags,
3285                                       bool enable, bool count)
3286 {
3287         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3288         struct batadv_tt_common_entry *tt_common_entry;
3289         u16 changed_num = 0;
3290         struct hlist_head *head;
3291         u32 i;
3292
3293         if (!hash)
3294                 return;
3295
3296         for (i = 0; i < hash->size; i++) {
3297                 head = &hash->table[i];
3298
3299                 rcu_read_lock();
3300                 hlist_for_each_entry_rcu(tt_common_entry,
3301                                          head, hash_entry) {
3302                         if (enable) {
3303                                 if ((tt_common_entry->flags & flags) == flags)
3304                                         continue;
3305                                 tt_common_entry->flags |= flags;
3306                         } else {
3307                                 if (!(tt_common_entry->flags & flags))
3308                                         continue;
3309                                 tt_common_entry->flags &= ~flags;
3310                         }
3311                         changed_num++;
3312
3313                         if (!count)
3314                                 continue;
3315
3316                         batadv_tt_local_size_inc(bat_priv,
3317                                                  tt_common_entry->vid);
3318                 }
3319                 rcu_read_unlock();
3320         }
3321 }
3322
3323 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
3324 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
3325 {
3326         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
3327         struct batadv_tt_common_entry *tt_common;
3328         struct batadv_tt_local_entry *tt_local;
3329         struct hlist_node *node_tmp;
3330         struct hlist_head *head;
3331         spinlock_t *list_lock; /* protects write access to the hash lists */
3332         u32 i;
3333
3334         if (!hash)
3335                 return;
3336
3337         for (i = 0; i < hash->size; i++) {
3338                 head = &hash->table[i];
3339                 list_lock = &hash->list_locks[i];
3340
3341                 spin_lock_bh(list_lock);
3342                 hlist_for_each_entry_safe(tt_common, node_tmp, head,
3343                                           hash_entry) {
3344                         if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
3345                                 continue;
3346
3347                         batadv_dbg(BATADV_DBG_TT, bat_priv,
3348                                    "Deleting local tt entry (%pM, vid: %d): pending\n",
3349                                    tt_common->addr,
3350                                    BATADV_PRINT_VID(tt_common->vid));
3351
3352                         batadv_tt_local_size_dec(bat_priv, tt_common->vid);
3353                         hlist_del_rcu(&tt_common->hash_entry);
3354                         tt_local = container_of(tt_common,
3355                                                 struct batadv_tt_local_entry,
3356                                                 common);
3357
3358                         batadv_tt_local_entry_free_ref(tt_local);
3359                 }
3360                 spin_unlock_bh(list_lock);
3361         }
3362 }
3363
3364 /**
3365  * batadv_tt_local_commit_changes_nolock - commit all pending local tt changes
3366  *  which have been queued in the time since the last commit
3367  * @bat_priv: the bat priv with all the soft interface information
3368  *
3369  * Caller must hold tt->commit_lock.
3370  */
3371 static void batadv_tt_local_commit_changes_nolock(struct batadv_priv *bat_priv)
3372 {
3373         lockdep_assert_held(&bat_priv->tt.commit_lock);
3374
3375         /* Update multicast addresses in local translation table */
3376         batadv_mcast_mla_update(bat_priv);
3377
3378         if (atomic_read(&bat_priv->tt.local_changes) < 1) {
3379                 if (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))
3380                         batadv_tt_tvlv_container_update(bat_priv);
3381                 return;
3382         }
3383
3384         batadv_tt_local_set_flags(bat_priv, BATADV_TT_CLIENT_NEW, false, true);
3385
3386         batadv_tt_local_purge_pending_clients(bat_priv);
3387         batadv_tt_local_update_crc(bat_priv);
3388
3389         /* Increment the TTVN only once per OGM interval */
3390         atomic_inc(&bat_priv->tt.vn);
3391         batadv_dbg(BATADV_DBG_TT, bat_priv,
3392                    "Local changes committed, updating to ttvn %u\n",
3393                    (u8)atomic_read(&bat_priv->tt.vn));
3394
3395         /* reset the sending counter */
3396         atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
3397         batadv_tt_tvlv_container_update(bat_priv);
3398 }
3399
3400 /**
3401  * batadv_tt_local_commit_changes - commit all pending local tt changes which
3402  *  have been queued in the time since the last commit
3403  * @bat_priv: the bat priv with all the soft interface information
3404  */
3405 void batadv_tt_local_commit_changes(struct batadv_priv *bat_priv)
3406 {
3407         spin_lock_bh(&bat_priv->tt.commit_lock);
3408         batadv_tt_local_commit_changes_nolock(bat_priv);
3409         spin_unlock_bh(&bat_priv->tt.commit_lock);
3410 }
3411
3412 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, u8 *src, u8 *dst,
3413                            unsigned short vid)
3414 {
3415         struct batadv_tt_local_entry *tt_local_entry = NULL;
3416         struct batadv_tt_global_entry *tt_global_entry = NULL;
3417         struct batadv_softif_vlan *vlan;
3418         bool ret = false;
3419
3420         vlan = batadv_softif_vlan_get(bat_priv, vid);
3421         if (!vlan || !atomic_read(&vlan->ap_isolation))
3422                 goto out;
3423
3424         tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst, vid);
3425         if (!tt_local_entry)
3426                 goto out;
3427
3428         tt_global_entry = batadv_tt_global_hash_find(bat_priv, src, vid);
3429         if (!tt_global_entry)
3430                 goto out;
3431
3432         if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
3433                 goto out;
3434
3435         ret = true;
3436
3437 out:
3438         if (vlan)
3439                 batadv_softif_vlan_free_ref(vlan);
3440         if (tt_global_entry)
3441                 batadv_tt_global_entry_free_ref(tt_global_entry);
3442         if (tt_local_entry)
3443                 batadv_tt_local_entry_free_ref(tt_local_entry);
3444         return ret;
3445 }
3446
3447 /**
3448  * batadv_tt_update_orig - update global translation table with new tt
3449  *  information received via ogms
3450  * @bat_priv: the bat priv with all the soft interface information
3451  * @orig: the orig_node of the ogm
3452  * @tt_vlan: pointer to the first tvlv VLAN entry
3453  * @tt_num_vlan: number of tvlv VLAN entries
3454  * @tt_change: pointer to the first entry in the TT buffer
3455  * @tt_num_changes: number of tt changes inside the tt buffer
3456  * @ttvn: translation table version number of this changeset
3457  * @tt_crc: crc32 checksum of orig node's translation table
3458  */
3459 static void batadv_tt_update_orig(struct batadv_priv *bat_priv,
3460                                   struct batadv_orig_node *orig_node,
3461                                   const void *tt_buff, u16 tt_num_vlan,
3462                                   struct batadv_tvlv_tt_change *tt_change,
3463                                   u16 tt_num_changes, u8 ttvn)
3464 {
3465         u8 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
3466         struct batadv_tvlv_tt_vlan_data *tt_vlan;
3467         bool full_table = true;
3468         bool has_tt_init;
3469
3470         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)tt_buff;
3471         has_tt_init = test_bit(BATADV_ORIG_CAPA_HAS_TT,
3472                                &orig_node->capa_initialized);
3473
3474         /* orig table not initialised AND first diff is in the OGM OR the ttvn
3475          * increased by one -> we can apply the attached changes
3476          */
3477         if ((!has_tt_init && ttvn == 1) || ttvn - orig_ttvn == 1) {
3478                 /* the OGM could not contain the changes due to their size or
3479                  * because they have already been sent BATADV_TT_OGM_APPEND_MAX
3480                  * times.
3481                  * In this case send a tt request
3482                  */
3483                 if (!tt_num_changes) {
3484                         full_table = false;
3485                         goto request_table;
3486                 }
3487
3488                 spin_lock_bh(&orig_node->tt_lock);
3489
3490                 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
3491                                          ttvn, tt_change);
3492
3493                 /* Even if we received the precomputed crc with the OGM, we
3494                  * prefer to recompute it to spot any possible inconsistency
3495                  * in the global table
3496                  */
3497                 batadv_tt_global_update_crc(bat_priv, orig_node);
3498
3499                 spin_unlock_bh(&orig_node->tt_lock);
3500
3501                 /* The ttvn alone is not enough to guarantee consistency
3502                  * because a single value could represent different states
3503                  * (due to the wrap around). Thus a node has to check whether
3504                  * the resulting table (after applying the changes) is still
3505                  * consistent or not. E.g. a node could disconnect while its
3506                  * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
3507                  * checking the CRC value is mandatory to detect the
3508                  * inconsistency
3509                  */
3510                 if (!batadv_tt_global_check_crc(orig_node, tt_vlan,
3511                                                 tt_num_vlan))
3512                         goto request_table;
3513         } else {
3514                 /* if we missed more than one change or our tables are not
3515                  * in sync anymore -> request fresh tt data
3516                  */
3517                 if (!has_tt_init || ttvn != orig_ttvn ||
3518                     !batadv_tt_global_check_crc(orig_node, tt_vlan,
3519                                                 tt_num_vlan)) {
3520 request_table:
3521                         batadv_dbg(BATADV_DBG_TT, bat_priv,
3522                                    "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u num_changes: %u)\n",
3523                                    orig_node->orig, ttvn, orig_ttvn,
3524                                    tt_num_changes);
3525                         batadv_send_tt_request(bat_priv, orig_node, ttvn,
3526                                                tt_vlan, tt_num_vlan,
3527                                                full_table);
3528                         return;
3529                 }
3530         }
3531 }
3532
3533 /**
3534  * batadv_tt_global_client_is_roaming - check if a client is marked as roaming
3535  * @bat_priv: the bat priv with all the soft interface information
3536  * @addr: the mac address of the client to check
3537  * @vid: VLAN identifier
3538  *
3539  * Returns true if we know that the client has moved from its old originator
3540  * to another one. This entry is still kept for consistency purposes and will be
3541  * deleted later by a DEL or because of timeout
3542  */
3543 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
3544                                         u8 *addr, unsigned short vid)
3545 {
3546         struct batadv_tt_global_entry *tt_global_entry;
3547         bool ret = false;
3548
3549         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr, vid);
3550         if (!tt_global_entry)
3551                 goto out;
3552
3553         ret = tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3554         batadv_tt_global_entry_free_ref(tt_global_entry);
3555 out:
3556         return ret;
3557 }
3558
3559 /**
3560  * batadv_tt_local_client_is_roaming - tells whether the client is roaming
3561  * @bat_priv: the bat priv with all the soft interface information
3562  * @addr: the mac address of the local client to query
3563  * @vid: VLAN identifier
3564  *
3565  * Returns true if the local client is known to be roaming (it is not served by
3566  * this node anymore) or not. If yes, the client is still present in the table
3567  * to keep the latter consistent with the node TTVN
3568  */
3569 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
3570                                        u8 *addr, unsigned short vid)
3571 {
3572         struct batadv_tt_local_entry *tt_local_entry;
3573         bool ret = false;
3574
3575         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr, vid);
3576         if (!tt_local_entry)
3577                 goto out;
3578
3579         ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
3580         batadv_tt_local_entry_free_ref(tt_local_entry);
3581 out:
3582         return ret;
3583 }
3584
3585 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
3586                                           struct batadv_orig_node *orig_node,
3587                                           const unsigned char *addr,
3588                                           unsigned short vid)
3589 {
3590         bool ret = false;
3591
3592         if (!batadv_tt_global_add(bat_priv, orig_node, addr, vid,
3593                                   BATADV_TT_CLIENT_TEMP,
3594                                   atomic_read(&orig_node->last_ttvn)))
3595                 goto out;
3596
3597         batadv_dbg(BATADV_DBG_TT, bat_priv,
3598                    "Added temporary global client (addr: %pM, vid: %d, orig: %pM)\n",
3599                    addr, BATADV_PRINT_VID(vid), orig_node->orig);
3600         ret = true;
3601 out:
3602         return ret;
3603 }
3604
3605 /**
3606  * batadv_tt_local_resize_to_mtu - resize the local translation table fit the
3607  *  maximum packet size that can be transported through the mesh
3608  * @soft_iface: netdev struct of the mesh interface
3609  *
3610  * Remove entries older than 'timeout' and half timeout if more entries need
3611  * to be removed.
3612  */
3613 void batadv_tt_local_resize_to_mtu(struct net_device *soft_iface)
3614 {
3615         struct batadv_priv *bat_priv = netdev_priv(soft_iface);
3616         int packet_size_max = atomic_read(&bat_priv->packet_size_max);
3617         int table_size, timeout = BATADV_TT_LOCAL_TIMEOUT / 2;
3618         bool reduced = false;
3619
3620         spin_lock_bh(&bat_priv->tt.commit_lock);
3621
3622         while (true) {
3623                 table_size = batadv_tt_local_table_transmit_size(bat_priv);
3624                 if (packet_size_max >= table_size)
3625                         break;
3626
3627                 batadv_tt_local_purge(bat_priv, timeout);
3628                 batadv_tt_local_purge_pending_clients(bat_priv);
3629
3630                 timeout /= 2;
3631                 reduced = true;
3632                 net_ratelimited_function(batadv_info, soft_iface,
3633                                          "Forced to purge local tt entries to fit new maximum fragment MTU (%i)\n",
3634                                          packet_size_max);
3635         }
3636
3637         /* commit these changes immediately, to avoid synchronization problem
3638          * with the TTVN
3639          */
3640         if (reduced)
3641                 batadv_tt_local_commit_changes_nolock(bat_priv);
3642
3643         spin_unlock_bh(&bat_priv->tt.commit_lock);
3644 }
3645
3646 /**
3647  * batadv_tt_tvlv_ogm_handler_v1 - process incoming tt tvlv container
3648  * @bat_priv: the bat priv with all the soft interface information
3649  * @orig: the orig_node of the ogm
3650  * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
3651  * @tvlv_value: tvlv buffer containing the gateway data
3652  * @tvlv_value_len: tvlv buffer length
3653  */
3654 static void batadv_tt_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
3655                                           struct batadv_orig_node *orig,
3656                                           u8 flags, void *tvlv_value,
3657                                           u16 tvlv_value_len)
3658 {
3659         struct batadv_tvlv_tt_vlan_data *tt_vlan;
3660         struct batadv_tvlv_tt_change *tt_change;
3661         struct batadv_tvlv_tt_data *tt_data;
3662         u16 num_entries, num_vlan;
3663
3664         if (tvlv_value_len < sizeof(*tt_data))
3665                 return;
3666
3667         tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3668         tvlv_value_len -= sizeof(*tt_data);
3669
3670         num_vlan = ntohs(tt_data->num_vlan);
3671
3672         if (tvlv_value_len < sizeof(*tt_vlan) * num_vlan)
3673                 return;
3674
3675         tt_vlan = (struct batadv_tvlv_tt_vlan_data *)(tt_data + 1);
3676         tt_change = (struct batadv_tvlv_tt_change *)(tt_vlan + num_vlan);
3677         tvlv_value_len -= sizeof(*tt_vlan) * num_vlan;
3678
3679         num_entries = batadv_tt_entries(tvlv_value_len);
3680
3681         batadv_tt_update_orig(bat_priv, orig, tt_vlan, num_vlan, tt_change,
3682                               num_entries, tt_data->ttvn);
3683 }
3684
3685 /**
3686  * batadv_tt_tvlv_unicast_handler_v1 - process incoming (unicast) tt tvlv
3687  *  container
3688  * @bat_priv: the bat priv with all the soft interface information
3689  * @src: mac address of tt tvlv sender
3690  * @dst: mac address of tt tvlv recipient
3691  * @tvlv_value: tvlv buffer containing the tt data
3692  * @tvlv_value_len: tvlv buffer length
3693  *
3694  * Returns NET_RX_DROP if the tt tvlv is to be re-routed, NET_RX_SUCCESS
3695  * otherwise.
3696  */
3697 static int batadv_tt_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3698                                              u8 *src, u8 *dst,
3699                                              void *tvlv_value,
3700                                              u16 tvlv_value_len)
3701 {
3702         struct batadv_tvlv_tt_data *tt_data;
3703         u16 tt_vlan_len, tt_num_entries;
3704         char tt_flag;
3705         bool ret;
3706
3707         if (tvlv_value_len < sizeof(*tt_data))
3708                 return NET_RX_SUCCESS;
3709
3710         tt_data = (struct batadv_tvlv_tt_data *)tvlv_value;
3711         tvlv_value_len -= sizeof(*tt_data);
3712
3713         tt_vlan_len = sizeof(struct batadv_tvlv_tt_vlan_data);
3714         tt_vlan_len *= ntohs(tt_data->num_vlan);
3715
3716         if (tvlv_value_len < tt_vlan_len)
3717                 return NET_RX_SUCCESS;
3718
3719         tvlv_value_len -= tt_vlan_len;
3720         tt_num_entries = batadv_tt_entries(tvlv_value_len);
3721
3722         switch (tt_data->flags & BATADV_TT_DATA_TYPE_MASK) {
3723         case BATADV_TT_REQUEST:
3724                 batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
3725
3726                 /* If this node cannot provide a TT response the tt_request is
3727                  * forwarded
3728                  */
3729                 ret = batadv_send_tt_response(bat_priv, tt_data, src, dst);
3730                 if (!ret) {
3731                         if (tt_data->flags & BATADV_TT_FULL_TABLE)
3732                                 tt_flag = 'F';
3733                         else
3734                                 tt_flag = '.';
3735
3736                         batadv_dbg(BATADV_DBG_TT, bat_priv,
3737                                    "Routing TT_REQUEST to %pM [%c]\n",
3738                                    dst, tt_flag);
3739                         /* tvlv API will re-route the packet */
3740                         return NET_RX_DROP;
3741                 }
3742                 break;
3743         case BATADV_TT_RESPONSE:
3744                 batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
3745
3746                 if (batadv_is_my_mac(bat_priv, dst)) {
3747                         batadv_handle_tt_response(bat_priv, tt_data,
3748                                                   src, tt_num_entries);
3749                         return NET_RX_SUCCESS;
3750                 }
3751
3752                 if (tt_data->flags & BATADV_TT_FULL_TABLE)
3753                         tt_flag =  'F';
3754                 else
3755                         tt_flag = '.';
3756
3757                 batadv_dbg(BATADV_DBG_TT, bat_priv,
3758                            "Routing TT_RESPONSE to %pM [%c]\n", dst, tt_flag);
3759
3760                 /* tvlv API will re-route the packet */
3761                 return NET_RX_DROP;
3762         }
3763
3764         return NET_RX_SUCCESS;
3765 }
3766
3767 /**
3768  * batadv_roam_tvlv_unicast_handler_v1 - process incoming tt roam tvlv container
3769  * @bat_priv: the bat priv with all the soft interface information
3770  * @src: mac address of tt tvlv sender
3771  * @dst: mac address of tt tvlv recipient
3772  * @tvlv_value: tvlv buffer containing the tt data
3773  * @tvlv_value_len: tvlv buffer length
3774  *
3775  * Returns NET_RX_DROP if the tt roam tvlv is to be re-routed, NET_RX_SUCCESS
3776  * otherwise.
3777  */
3778 static int batadv_roam_tvlv_unicast_handler_v1(struct batadv_priv *bat_priv,
3779                                                u8 *src, u8 *dst,
3780                                                void *tvlv_value,
3781                                                u16 tvlv_value_len)
3782 {
3783         struct batadv_tvlv_roam_adv *roaming_adv;
3784         struct batadv_orig_node *orig_node = NULL;
3785
3786         /* If this node is not the intended recipient of the
3787          * roaming advertisement the packet is forwarded
3788          * (the tvlv API will re-route the packet).
3789          */
3790         if (!batadv_is_my_mac(bat_priv, dst))
3791                 return NET_RX_DROP;
3792
3793         if (tvlv_value_len < sizeof(*roaming_adv))
3794                 goto out;
3795
3796         orig_node = batadv_orig_hash_find(bat_priv, src);
3797         if (!orig_node)
3798                 goto out;
3799
3800         batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
3801         roaming_adv = (struct batadv_tvlv_roam_adv *)tvlv_value;
3802
3803         batadv_dbg(BATADV_DBG_TT, bat_priv,
3804                    "Received ROAMING_ADV from %pM (client %pM)\n",
3805                    src, roaming_adv->client);
3806
3807         batadv_tt_global_add(bat_priv, orig_node, roaming_adv->client,
3808                              ntohs(roaming_adv->vid), BATADV_TT_CLIENT_ROAM,
3809                              atomic_read(&orig_node->last_ttvn) + 1);
3810
3811 out:
3812         if (orig_node)
3813                 batadv_orig_node_free_ref(orig_node);
3814         return NET_RX_SUCCESS;
3815 }
3816
3817 /**
3818  * batadv_tt_init - initialise the translation table internals
3819  * @bat_priv: the bat priv with all the soft interface information
3820  *
3821  * Return 0 on success or negative error number in case of failure.
3822  */
3823 int batadv_tt_init(struct batadv_priv *bat_priv)
3824 {
3825         int ret;
3826
3827         /* synchronized flags must be remote */
3828         BUILD_BUG_ON(!(BATADV_TT_SYNC_MASK & BATADV_TT_REMOTE_MASK));
3829
3830         ret = batadv_tt_local_init(bat_priv);
3831         if (ret < 0)
3832                 return ret;
3833
3834         ret = batadv_tt_global_init(bat_priv);
3835         if (ret < 0)
3836                 return ret;
3837
3838         batadv_tvlv_handler_register(bat_priv, batadv_tt_tvlv_ogm_handler_v1,
3839                                      batadv_tt_tvlv_unicast_handler_v1,
3840                                      BATADV_TVLV_TT, 1, BATADV_NO_FLAGS);
3841
3842         batadv_tvlv_handler_register(bat_priv, NULL,
3843                                      batadv_roam_tvlv_unicast_handler_v1,
3844                                      BATADV_TVLV_ROAM, 1, BATADV_NO_FLAGS);
3845
3846         INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
3847         queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
3848                            msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
3849
3850         return 1;
3851 }
3852
3853 /**
3854  * batadv_tt_global_is_isolated - check if a client is marked as isolated
3855  * @bat_priv: the bat priv with all the soft interface information
3856  * @addr: the mac address of the client
3857  * @vid: the identifier of the VLAN where this client is connected
3858  *
3859  * Returns true if the client is marked with the TT_CLIENT_ISOLA flag, false
3860  * otherwise
3861  */
3862 bool batadv_tt_global_is_isolated(struct batadv_priv *bat_priv,
3863                                   const u8 *addr, unsigned short vid)
3864 {
3865         struct batadv_tt_global_entry *tt;
3866         bool ret;
3867
3868         tt = batadv_tt_global_hash_find(bat_priv, addr, vid);
3869         if (!tt)
3870                 return false;
3871
3872         ret = tt->common.flags & BATADV_TT_CLIENT_ISOLA;
3873
3874         batadv_tt_global_entry_free_ref(tt);
3875
3876         return ret;
3877 }