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