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