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