Merge tag 'stable/for-linus-3.9-rc6-tag' of git://git.kernel.org/pub/scm/linux/kernel...
[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/crc16.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                                  struct batadv_orig_node *orig_node);
38 static void batadv_tt_purge(struct work_struct *work);
39 static void
40 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry);
41 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
42                                  struct batadv_orig_node *orig_node,
43                                  const unsigned char *addr,
44                                  const char *message, bool roaming);
45
46 /* returns 1 if they are the same mac addr */
47 static int batadv_compare_tt(const struct hlist_node *node, const void *data2)
48 {
49         const void *data1 = container_of(node, struct batadv_tt_common_entry,
50                                          hash_entry);
51
52         return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
53 }
54
55 static struct batadv_tt_common_entry *
56 batadv_tt_hash_find(struct batadv_hashtable *hash, const void *data)
57 {
58         struct hlist_head *head;
59         struct batadv_tt_common_entry *tt_common_entry;
60         struct batadv_tt_common_entry *tt_common_entry_tmp = NULL;
61         uint32_t index;
62
63         if (!hash)
64                 return NULL;
65
66         index = batadv_choose_orig(data, hash->size);
67         head = &hash->table[index];
68
69         rcu_read_lock();
70         hlist_for_each_entry_rcu(tt_common_entry, head, hash_entry) {
71                 if (!batadv_compare_eth(tt_common_entry, data))
72                         continue;
73
74                 if (!atomic_inc_not_zero(&tt_common_entry->refcount))
75                         continue;
76
77                 tt_common_entry_tmp = tt_common_entry;
78                 break;
79         }
80         rcu_read_unlock();
81
82         return tt_common_entry_tmp;
83 }
84
85 static struct batadv_tt_local_entry *
86 batadv_tt_local_hash_find(struct batadv_priv *bat_priv, const void *data)
87 {
88         struct batadv_tt_common_entry *tt_common_entry;
89         struct batadv_tt_local_entry *tt_local_entry = NULL;
90
91         tt_common_entry = batadv_tt_hash_find(bat_priv->tt.local_hash, data);
92         if (tt_common_entry)
93                 tt_local_entry = container_of(tt_common_entry,
94                                               struct batadv_tt_local_entry,
95                                               common);
96         return tt_local_entry;
97 }
98
99 static struct batadv_tt_global_entry *
100 batadv_tt_global_hash_find(struct batadv_priv *bat_priv, const void *data)
101 {
102         struct batadv_tt_common_entry *tt_common_entry;
103         struct batadv_tt_global_entry *tt_global_entry = NULL;
104
105         tt_common_entry = batadv_tt_hash_find(bat_priv->tt.global_hash, data);
106         if (tt_common_entry)
107                 tt_global_entry = container_of(tt_common_entry,
108                                                struct batadv_tt_global_entry,
109                                                common);
110         return tt_global_entry;
111 }
112
113 static void
114 batadv_tt_local_entry_free_ref(struct batadv_tt_local_entry *tt_local_entry)
115 {
116         if (atomic_dec_and_test(&tt_local_entry->common.refcount))
117                 kfree_rcu(tt_local_entry, common.rcu);
118 }
119
120 static void batadv_tt_global_entry_free_rcu(struct rcu_head *rcu)
121 {
122         struct batadv_tt_common_entry *tt_common_entry;
123         struct batadv_tt_global_entry *tt_global_entry;
124
125         tt_common_entry = container_of(rcu, struct batadv_tt_common_entry, rcu);
126         tt_global_entry = container_of(tt_common_entry,
127                                        struct batadv_tt_global_entry, common);
128
129         kfree(tt_global_entry);
130 }
131
132 static void
133 batadv_tt_global_entry_free_ref(struct batadv_tt_global_entry *tt_global_entry)
134 {
135         if (atomic_dec_and_test(&tt_global_entry->common.refcount)) {
136                 batadv_tt_global_del_orig_list(tt_global_entry);
137                 call_rcu(&tt_global_entry->common.rcu,
138                          batadv_tt_global_entry_free_rcu);
139         }
140 }
141
142 static void batadv_tt_orig_list_entry_free_rcu(struct rcu_head *rcu)
143 {
144         struct batadv_tt_orig_list_entry *orig_entry;
145
146         orig_entry = container_of(rcu, struct batadv_tt_orig_list_entry, rcu);
147         batadv_orig_node_free_ref(orig_entry->orig_node);
148         kfree(orig_entry);
149 }
150
151 static void
152 batadv_tt_orig_list_entry_free_ref(struct batadv_tt_orig_list_entry *orig_entry)
153 {
154         if (!atomic_dec_and_test(&orig_entry->refcount))
155                 return;
156         /* to avoid race conditions, immediately decrease the tt counter */
157         atomic_dec(&orig_entry->orig_node->tt_size);
158         call_rcu(&orig_entry->rcu, batadv_tt_orig_list_entry_free_rcu);
159 }
160
161 static void batadv_tt_local_event(struct batadv_priv *bat_priv,
162                                   const uint8_t *addr, uint8_t flags)
163 {
164         struct batadv_tt_change_node *tt_change_node, *entry, *safe;
165         bool event_removed = false;
166         bool del_op_requested, del_op_entry;
167
168         tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
169
170         if (!tt_change_node)
171                 return;
172
173         tt_change_node->change.flags = flags;
174         memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
175
176         del_op_requested = flags & BATADV_TT_CLIENT_DEL;
177
178         /* check for ADD+DEL or DEL+ADD events */
179         spin_lock_bh(&bat_priv->tt.changes_list_lock);
180         list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
181                                  list) {
182                 if (!batadv_compare_eth(entry->change.addr, addr))
183                         continue;
184
185                 /* DEL+ADD in the same orig interval have no effect and can be
186                  * removed to avoid silly behaviour on the receiver side. The
187                  * other way around (ADD+DEL) can happen in case of roaming of
188                  * a client still in the NEW state. Roaming of NEW clients is
189                  * now possible due to automatically recognition of "temporary"
190                  * clients
191                  */
192                 del_op_entry = entry->change.flags & BATADV_TT_CLIENT_DEL;
193                 if (!del_op_requested && del_op_entry)
194                         goto del;
195                 if (del_op_requested && !del_op_entry)
196                         goto del;
197                 continue;
198 del:
199                 list_del(&entry->list);
200                 kfree(entry);
201                 kfree(tt_change_node);
202                 event_removed = true;
203                 goto unlock;
204         }
205
206         /* track the change in the OGMinterval list */
207         list_add_tail(&tt_change_node->list, &bat_priv->tt.changes_list);
208
209 unlock:
210         spin_unlock_bh(&bat_priv->tt.changes_list_lock);
211
212         if (event_removed)
213                 atomic_dec(&bat_priv->tt.local_changes);
214         else
215                 atomic_inc(&bat_priv->tt.local_changes);
216 }
217
218 int batadv_tt_len(int changes_num)
219 {
220         return changes_num * sizeof(struct batadv_tt_change);
221 }
222
223 static int batadv_tt_local_init(struct batadv_priv *bat_priv)
224 {
225         if (bat_priv->tt.local_hash)
226                 return 0;
227
228         bat_priv->tt.local_hash = batadv_hash_new(1024);
229
230         if (!bat_priv->tt.local_hash)
231                 return -ENOMEM;
232
233         batadv_hash_set_lock_class(bat_priv->tt.local_hash,
234                                    &batadv_tt_local_hash_lock_class_key);
235
236         return 0;
237 }
238
239 static void batadv_tt_global_free(struct batadv_priv *bat_priv,
240                                   struct batadv_tt_global_entry *tt_global,
241                                   const char *message)
242 {
243         batadv_dbg(BATADV_DBG_TT, bat_priv,
244                    "Deleting global tt entry %pM: %s\n",
245                    tt_global->common.addr, message);
246
247         batadv_hash_remove(bat_priv->tt.global_hash, batadv_compare_tt,
248                            batadv_choose_orig, tt_global->common.addr);
249         batadv_tt_global_entry_free_ref(tt_global);
250 }
251
252 void batadv_tt_local_add(struct net_device *soft_iface, const uint8_t *addr,
253                          int ifindex)
254 {
255         struct batadv_priv *bat_priv = netdev_priv(soft_iface);
256         struct batadv_tt_local_entry *tt_local;
257         struct batadv_tt_global_entry *tt_global;
258         struct hlist_head *head;
259         struct batadv_tt_orig_list_entry *orig_entry;
260         int hash_added;
261         bool roamed_back = false;
262
263         tt_local = batadv_tt_local_hash_find(bat_priv, addr);
264         tt_global = batadv_tt_global_hash_find(bat_priv, addr);
265
266         if (tt_local) {
267                 tt_local->last_seen = jiffies;
268                 if (tt_local->common.flags & BATADV_TT_CLIENT_PENDING) {
269                         batadv_dbg(BATADV_DBG_TT, bat_priv,
270                                    "Re-adding pending client %pM\n", addr);
271                         /* whatever the reason why the PENDING flag was set,
272                          * this is a client which was enqueued to be removed in
273                          * this orig_interval. Since it popped up again, the
274                          * flag can be reset like it was never enqueued
275                          */
276                         tt_local->common.flags &= ~BATADV_TT_CLIENT_PENDING;
277                         goto add_event;
278                 }
279
280                 if (tt_local->common.flags & BATADV_TT_CLIENT_ROAM) {
281                         batadv_dbg(BATADV_DBG_TT, bat_priv,
282                                    "Roaming client %pM came back to its original location\n",
283                                    addr);
284                         /* the ROAM flag is set because this client roamed away
285                          * and the node got a roaming_advertisement message. Now
286                          * that the client popped up again at its original
287                          * location such flag can be unset
288                          */
289                         tt_local->common.flags &= ~BATADV_TT_CLIENT_ROAM;
290                         roamed_back = true;
291                 }
292                 goto check_roaming;
293         }
294
295         tt_local = kmalloc(sizeof(*tt_local), GFP_ATOMIC);
296         if (!tt_local)
297                 goto out;
298
299         batadv_dbg(BATADV_DBG_TT, bat_priv,
300                    "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
301                    (uint8_t)atomic_read(&bat_priv->tt.vn));
302
303         memcpy(tt_local->common.addr, addr, ETH_ALEN);
304         /* The local entry has to be marked as NEW to avoid to send it in
305          * a full table response going out before the next ttvn increment
306          * (consistency check)
307          */
308         tt_local->common.flags = BATADV_TT_CLIENT_NEW;
309         if (batadv_is_wifi_iface(ifindex))
310                 tt_local->common.flags |= BATADV_TT_CLIENT_WIFI;
311         atomic_set(&tt_local->common.refcount, 2);
312         tt_local->last_seen = jiffies;
313         tt_local->common.added_at = tt_local->last_seen;
314
315         /* the batman interface mac address should never be purged */
316         if (batadv_compare_eth(addr, soft_iface->dev_addr))
317                 tt_local->common.flags |= BATADV_TT_CLIENT_NOPURGE;
318
319         hash_added = batadv_hash_add(bat_priv->tt.local_hash, batadv_compare_tt,
320                                      batadv_choose_orig, &tt_local->common,
321                                      &tt_local->common.hash_entry);
322
323         if (unlikely(hash_added != 0)) {
324                 /* remove the reference for the hash */
325                 batadv_tt_local_entry_free_ref(tt_local);
326                 goto out;
327         }
328
329 add_event:
330         batadv_tt_local_event(bat_priv, addr, tt_local->common.flags);
331
332 check_roaming:
333         /* Check whether it is a roaming, but don't do anything if the roaming
334          * process has already been handled
335          */
336         if (tt_global && !(tt_global->common.flags & BATADV_TT_CLIENT_ROAM)) {
337                 /* These node are probably going to update their tt table */
338                 head = &tt_global->orig_list;
339                 rcu_read_lock();
340                 hlist_for_each_entry_rcu(orig_entry, head, list) {
341                         batadv_send_roam_adv(bat_priv, tt_global->common.addr,
342                                              orig_entry->orig_node);
343                 }
344                 rcu_read_unlock();
345                 if (roamed_back) {
346                         batadv_tt_global_free(bat_priv, tt_global,
347                                               "Roaming canceled");
348                         tt_global = NULL;
349                 } else {
350                         /* The global entry has to be marked as ROAMING and
351                          * has to be kept for consistency purpose
352                          */
353                         tt_global->common.flags |= BATADV_TT_CLIENT_ROAM;
354                         tt_global->roam_at = jiffies;
355                 }
356         }
357
358 out:
359         if (tt_local)
360                 batadv_tt_local_entry_free_ref(tt_local);
361         if (tt_global)
362                 batadv_tt_global_entry_free_ref(tt_global);
363 }
364
365 static void batadv_tt_realloc_packet_buff(unsigned char **packet_buff,
366                                           int *packet_buff_len,
367                                           int min_packet_len,
368                                           int new_packet_len)
369 {
370         unsigned char *new_buff;
371
372         new_buff = kmalloc(new_packet_len, GFP_ATOMIC);
373
374         /* keep old buffer if kmalloc should fail */
375         if (new_buff) {
376                 memcpy(new_buff, *packet_buff, min_packet_len);
377                 kfree(*packet_buff);
378                 *packet_buff = new_buff;
379                 *packet_buff_len = new_packet_len;
380         }
381 }
382
383 static void batadv_tt_prepare_packet_buff(struct batadv_priv *bat_priv,
384                                           unsigned char **packet_buff,
385                                           int *packet_buff_len,
386                                           int min_packet_len)
387 {
388         struct batadv_hard_iface *primary_if;
389         int req_len;
390
391         primary_if = batadv_primary_if_get_selected(bat_priv);
392
393         req_len = min_packet_len;
394         req_len += batadv_tt_len(atomic_read(&bat_priv->tt.local_changes));
395
396         /* if we have too many changes for one packet don't send any
397          * and wait for the tt table request which will be fragmented
398          */
399         if ((!primary_if) || (req_len > primary_if->soft_iface->mtu))
400                 req_len = min_packet_len;
401
402         batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
403                                       min_packet_len, req_len);
404
405         if (primary_if)
406                 batadv_hardif_free_ref(primary_if);
407 }
408
409 static int batadv_tt_changes_fill_buff(struct batadv_priv *bat_priv,
410                                        unsigned char **packet_buff,
411                                        int *packet_buff_len,
412                                        int min_packet_len)
413 {
414         struct batadv_tt_change_node *entry, *safe;
415         int count = 0, tot_changes = 0, new_len;
416         unsigned char *tt_buff;
417
418         batadv_tt_prepare_packet_buff(bat_priv, packet_buff,
419                                       packet_buff_len, min_packet_len);
420
421         new_len = *packet_buff_len - min_packet_len;
422         tt_buff = *packet_buff + min_packet_len;
423
424         if (new_len > 0)
425                 tot_changes = new_len / batadv_tt_len(1);
426
427         spin_lock_bh(&bat_priv->tt.changes_list_lock);
428         atomic_set(&bat_priv->tt.local_changes, 0);
429
430         list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
431                                  list) {
432                 if (count < tot_changes) {
433                         memcpy(tt_buff + batadv_tt_len(count),
434                                &entry->change, sizeof(struct batadv_tt_change));
435                         count++;
436                 }
437                 list_del(&entry->list);
438                 kfree(entry);
439         }
440         spin_unlock_bh(&bat_priv->tt.changes_list_lock);
441
442         /* Keep the buffer for possible tt_request */
443         spin_lock_bh(&bat_priv->tt.last_changeset_lock);
444         kfree(bat_priv->tt.last_changeset);
445         bat_priv->tt.last_changeset_len = 0;
446         bat_priv->tt.last_changeset = NULL;
447         /* check whether this new OGM has no changes due to size problems */
448         if (new_len > 0) {
449                 /* if kmalloc() fails we will reply with the full table
450                  * instead of providing the diff
451                  */
452                 bat_priv->tt.last_changeset = kmalloc(new_len, GFP_ATOMIC);
453                 if (bat_priv->tt.last_changeset) {
454                         memcpy(bat_priv->tt.last_changeset, tt_buff, new_len);
455                         bat_priv->tt.last_changeset_len = new_len;
456                 }
457         }
458         spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
459
460         return count;
461 }
462
463 int batadv_tt_local_seq_print_text(struct seq_file *seq, void *offset)
464 {
465         struct net_device *net_dev = (struct net_device *)seq->private;
466         struct batadv_priv *bat_priv = netdev_priv(net_dev);
467         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
468         struct batadv_tt_common_entry *tt_common_entry;
469         struct batadv_tt_local_entry *tt_local;
470         struct batadv_hard_iface *primary_if;
471         struct hlist_head *head;
472         uint32_t i;
473         int last_seen_secs;
474         int last_seen_msecs;
475         unsigned long last_seen_jiffies;
476         bool no_purge;
477         uint16_t np_flag = BATADV_TT_CLIENT_NOPURGE;
478
479         primary_if = batadv_seq_print_text_primary_if_get(seq);
480         if (!primary_if)
481                 goto out;
482
483         seq_printf(seq,
484                    "Locally retrieved addresses (from %s) announced via TT (TTVN: %u CRC: %#.4x):\n",
485                    net_dev->name, (uint8_t)atomic_read(&bat_priv->tt.vn),
486                    bat_priv->tt.local_crc);
487         seq_printf(seq, "       %-13s %-7s %-10s\n", "Client", "Flags",
488                    "Last seen");
489
490         for (i = 0; i < hash->size; i++) {
491                 head = &hash->table[i];
492
493                 rcu_read_lock();
494                 hlist_for_each_entry_rcu(tt_common_entry,
495                                          head, hash_entry) {
496                         tt_local = container_of(tt_common_entry,
497                                                 struct batadv_tt_local_entry,
498                                                 common);
499                         last_seen_jiffies = jiffies - tt_local->last_seen;
500                         last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
501                         last_seen_secs = last_seen_msecs / 1000;
502                         last_seen_msecs = last_seen_msecs % 1000;
503
504                         no_purge = tt_common_entry->flags & np_flag;
505
506                         seq_printf(seq, " * %pM [%c%c%c%c%c] %3u.%03u\n",
507                                    tt_common_entry->addr,
508                                    (tt_common_entry->flags &
509                                     BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
510                                    no_purge ? 'P' : '.',
511                                    (tt_common_entry->flags &
512                                     BATADV_TT_CLIENT_NEW ? 'N' : '.'),
513                                    (tt_common_entry->flags &
514                                     BATADV_TT_CLIENT_PENDING ? 'X' : '.'),
515                                    (tt_common_entry->flags &
516                                     BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
517                                    no_purge ? 0 : last_seen_secs,
518                                    no_purge ? 0 : last_seen_msecs);
519                 }
520                 rcu_read_unlock();
521         }
522 out:
523         if (primary_if)
524                 batadv_hardif_free_ref(primary_if);
525         return 0;
526 }
527
528 static void
529 batadv_tt_local_set_pending(struct batadv_priv *bat_priv,
530                             struct batadv_tt_local_entry *tt_local_entry,
531                             uint16_t flags, const char *message)
532 {
533         batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
534                               tt_local_entry->common.flags | flags);
535
536         /* The local client has to be marked as "pending to be removed" but has
537          * to be kept in the table in order to send it in a full table
538          * response issued before the net ttvn increment (consistency check)
539          */
540         tt_local_entry->common.flags |= BATADV_TT_CLIENT_PENDING;
541
542         batadv_dbg(BATADV_DBG_TT, bat_priv,
543                    "Local tt entry (%pM) pending to be removed: %s\n",
544                    tt_local_entry->common.addr, message);
545 }
546
547 /**
548  * batadv_tt_local_remove - logically remove an entry from the local table
549  * @bat_priv: the bat priv with all the soft interface information
550  * @addr: the MAC address of the client to remove
551  * @message: message to append to the log on deletion
552  * @roaming: true if the deletion is due to a roaming event
553  *
554  * Returns the flags assigned to the local entry before being deleted
555  */
556 uint16_t batadv_tt_local_remove(struct batadv_priv *bat_priv,
557                                 const uint8_t *addr, const char *message,
558                                 bool roaming)
559 {
560         struct batadv_tt_local_entry *tt_local_entry;
561         uint16_t flags, curr_flags = BATADV_NO_FLAGS;
562
563         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
564         if (!tt_local_entry)
565                 goto out;
566
567         curr_flags = tt_local_entry->common.flags;
568
569         flags = BATADV_TT_CLIENT_DEL;
570         /* if this global entry addition is due to a roaming, the node has to
571          * mark the local entry as "roamed" in order to correctly reroute
572          * packets later
573          */
574         if (roaming) {
575                 flags |= BATADV_TT_CLIENT_ROAM;
576                 /* mark the local client as ROAMed */
577                 tt_local_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
578         }
579
580         if (!(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW)) {
581                 batadv_tt_local_set_pending(bat_priv, tt_local_entry, flags,
582                                             message);
583                 goto out;
584         }
585         /* if this client has been added right now, it is possible to
586          * immediately purge it
587          */
588         batadv_tt_local_event(bat_priv, tt_local_entry->common.addr,
589                               curr_flags | BATADV_TT_CLIENT_DEL);
590         hlist_del_rcu(&tt_local_entry->common.hash_entry);
591         batadv_tt_local_entry_free_ref(tt_local_entry);
592
593 out:
594         if (tt_local_entry)
595                 batadv_tt_local_entry_free_ref(tt_local_entry);
596
597         return curr_flags;
598 }
599
600 static void batadv_tt_local_purge_list(struct batadv_priv *bat_priv,
601                                        struct hlist_head *head)
602 {
603         struct batadv_tt_local_entry *tt_local_entry;
604         struct batadv_tt_common_entry *tt_common_entry;
605         struct hlist_node *node_tmp;
606
607         hlist_for_each_entry_safe(tt_common_entry, node_tmp, head,
608                                   hash_entry) {
609                 tt_local_entry = container_of(tt_common_entry,
610                                               struct batadv_tt_local_entry,
611                                               common);
612                 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_NOPURGE)
613                         continue;
614
615                 /* entry already marked for deletion */
616                 if (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING)
617                         continue;
618
619                 if (!batadv_has_timed_out(tt_local_entry->last_seen,
620                                           BATADV_TT_LOCAL_TIMEOUT))
621                         continue;
622
623                 batadv_tt_local_set_pending(bat_priv, tt_local_entry,
624                                             BATADV_TT_CLIENT_DEL, "timed out");
625         }
626 }
627
628 static void batadv_tt_local_purge(struct batadv_priv *bat_priv)
629 {
630         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
631         struct hlist_head *head;
632         spinlock_t *list_lock; /* protects write access to the hash lists */
633         uint32_t i;
634
635         for (i = 0; i < hash->size; i++) {
636                 head = &hash->table[i];
637                 list_lock = &hash->list_locks[i];
638
639                 spin_lock_bh(list_lock);
640                 batadv_tt_local_purge_list(bat_priv, head);
641                 spin_unlock_bh(list_lock);
642         }
643 }
644
645 static void batadv_tt_local_table_free(struct batadv_priv *bat_priv)
646 {
647         struct batadv_hashtable *hash;
648         spinlock_t *list_lock; /* protects write access to the hash lists */
649         struct batadv_tt_common_entry *tt_common_entry;
650         struct batadv_tt_local_entry *tt_local;
651         struct hlist_node *node_tmp;
652         struct hlist_head *head;
653         uint32_t i;
654
655         if (!bat_priv->tt.local_hash)
656                 return;
657
658         hash = bat_priv->tt.local_hash;
659
660         for (i = 0; i < hash->size; i++) {
661                 head = &hash->table[i];
662                 list_lock = &hash->list_locks[i];
663
664                 spin_lock_bh(list_lock);
665                 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
666                                           head, hash_entry) {
667                         hlist_del_rcu(&tt_common_entry->hash_entry);
668                         tt_local = container_of(tt_common_entry,
669                                                 struct batadv_tt_local_entry,
670                                                 common);
671                         batadv_tt_local_entry_free_ref(tt_local);
672                 }
673                 spin_unlock_bh(list_lock);
674         }
675
676         batadv_hash_destroy(hash);
677
678         bat_priv->tt.local_hash = NULL;
679 }
680
681 static int batadv_tt_global_init(struct batadv_priv *bat_priv)
682 {
683         if (bat_priv->tt.global_hash)
684                 return 0;
685
686         bat_priv->tt.global_hash = batadv_hash_new(1024);
687
688         if (!bat_priv->tt.global_hash)
689                 return -ENOMEM;
690
691         batadv_hash_set_lock_class(bat_priv->tt.global_hash,
692                                    &batadv_tt_global_hash_lock_class_key);
693
694         return 0;
695 }
696
697 static void batadv_tt_changes_list_free(struct batadv_priv *bat_priv)
698 {
699         struct batadv_tt_change_node *entry, *safe;
700
701         spin_lock_bh(&bat_priv->tt.changes_list_lock);
702
703         list_for_each_entry_safe(entry, safe, &bat_priv->tt.changes_list,
704                                  list) {
705                 list_del(&entry->list);
706                 kfree(entry);
707         }
708
709         atomic_set(&bat_priv->tt.local_changes, 0);
710         spin_unlock_bh(&bat_priv->tt.changes_list_lock);
711 }
712
713 /* retrieves the orig_tt_list_entry belonging to orig_node from the
714  * batadv_tt_global_entry list
715  *
716  * returns it with an increased refcounter, NULL if not found
717  */
718 static struct batadv_tt_orig_list_entry *
719 batadv_tt_global_orig_entry_find(const struct batadv_tt_global_entry *entry,
720                                  const struct batadv_orig_node *orig_node)
721 {
722         struct batadv_tt_orig_list_entry *tmp_orig_entry, *orig_entry = NULL;
723         const struct hlist_head *head;
724
725         rcu_read_lock();
726         head = &entry->orig_list;
727         hlist_for_each_entry_rcu(tmp_orig_entry, head, list) {
728                 if (tmp_orig_entry->orig_node != orig_node)
729                         continue;
730                 if (!atomic_inc_not_zero(&tmp_orig_entry->refcount))
731                         continue;
732
733                 orig_entry = tmp_orig_entry;
734                 break;
735         }
736         rcu_read_unlock();
737
738         return orig_entry;
739 }
740
741 /* find out if an orig_node is already in the list of a tt_global_entry.
742  * returns true if found, false otherwise
743  */
744 static bool
745 batadv_tt_global_entry_has_orig(const struct batadv_tt_global_entry *entry,
746                                 const struct batadv_orig_node *orig_node)
747 {
748         struct batadv_tt_orig_list_entry *orig_entry;
749         bool found = false;
750
751         orig_entry = batadv_tt_global_orig_entry_find(entry, orig_node);
752         if (orig_entry) {
753                 found = true;
754                 batadv_tt_orig_list_entry_free_ref(orig_entry);
755         }
756
757         return found;
758 }
759
760 static void
761 batadv_tt_global_orig_entry_add(struct batadv_tt_global_entry *tt_global,
762                                 struct batadv_orig_node *orig_node, int ttvn)
763 {
764         struct batadv_tt_orig_list_entry *orig_entry;
765
766         orig_entry = batadv_tt_global_orig_entry_find(tt_global, orig_node);
767         if (orig_entry) {
768                 /* refresh the ttvn: the current value could be a bogus one that
769                  * was added during a "temporary client detection"
770                  */
771                 orig_entry->ttvn = ttvn;
772                 goto out;
773         }
774
775         orig_entry = kzalloc(sizeof(*orig_entry), GFP_ATOMIC);
776         if (!orig_entry)
777                 goto out;
778
779         INIT_HLIST_NODE(&orig_entry->list);
780         atomic_inc(&orig_node->refcount);
781         atomic_inc(&orig_node->tt_size);
782         orig_entry->orig_node = orig_node;
783         orig_entry->ttvn = ttvn;
784         atomic_set(&orig_entry->refcount, 2);
785
786         spin_lock_bh(&tt_global->list_lock);
787         hlist_add_head_rcu(&orig_entry->list,
788                            &tt_global->orig_list);
789         spin_unlock_bh(&tt_global->list_lock);
790 out:
791         if (orig_entry)
792                 batadv_tt_orig_list_entry_free_ref(orig_entry);
793 }
794
795 /* caller must hold orig_node refcount */
796 int batadv_tt_global_add(struct batadv_priv *bat_priv,
797                          struct batadv_orig_node *orig_node,
798                          const unsigned char *tt_addr, uint8_t flags,
799                          uint8_t ttvn)
800 {
801         struct batadv_tt_global_entry *tt_global_entry;
802         struct batadv_tt_local_entry *tt_local_entry;
803         int ret = 0;
804         int hash_added;
805         struct batadv_tt_common_entry *common;
806         uint16_t local_flags;
807
808         tt_global_entry = batadv_tt_global_hash_find(bat_priv, tt_addr);
809         tt_local_entry = batadv_tt_local_hash_find(bat_priv, tt_addr);
810
811         /* if the node already has a local client for this entry, it has to wait
812          * for a roaming advertisement instead of manually messing up the global
813          * table
814          */
815         if ((flags & BATADV_TT_CLIENT_TEMP) && tt_local_entry &&
816             !(tt_local_entry->common.flags & BATADV_TT_CLIENT_NEW))
817                 goto out;
818
819         if (!tt_global_entry) {
820                 tt_global_entry = kzalloc(sizeof(*tt_global_entry), GFP_ATOMIC);
821                 if (!tt_global_entry)
822                         goto out;
823
824                 common = &tt_global_entry->common;
825                 memcpy(common->addr, tt_addr, ETH_ALEN);
826
827                 common->flags = flags;
828                 tt_global_entry->roam_at = 0;
829                 /* node must store current time in case of roaming. This is
830                  * needed to purge this entry out on timeout (if nobody claims
831                  * it)
832                  */
833                 if (flags & BATADV_TT_CLIENT_ROAM)
834                         tt_global_entry->roam_at = jiffies;
835                 atomic_set(&common->refcount, 2);
836                 common->added_at = jiffies;
837
838                 INIT_HLIST_HEAD(&tt_global_entry->orig_list);
839                 spin_lock_init(&tt_global_entry->list_lock);
840
841                 hash_added = batadv_hash_add(bat_priv->tt.global_hash,
842                                              batadv_compare_tt,
843                                              batadv_choose_orig, common,
844                                              &common->hash_entry);
845
846                 if (unlikely(hash_added != 0)) {
847                         /* remove the reference for the hash */
848                         batadv_tt_global_entry_free_ref(tt_global_entry);
849                         goto out_remove;
850                 }
851         } else {
852                 common = &tt_global_entry->common;
853                 /* If there is already a global entry, we can use this one for
854                  * our processing.
855                  * But if we are trying to add a temporary client then here are
856                  * two options at this point:
857                  * 1) the global client is not a temporary client: the global
858                  *    client has to be left as it is, temporary information
859                  *    should never override any already known client state
860                  * 2) the global client is a temporary client: purge the
861                  *    originator list and add the new one orig_entry
862                  */
863                 if (flags & BATADV_TT_CLIENT_TEMP) {
864                         if (!(common->flags & BATADV_TT_CLIENT_TEMP))
865                                 goto out;
866                         if (batadv_tt_global_entry_has_orig(tt_global_entry,
867                                                             orig_node))
868                                 goto out_remove;
869                         batadv_tt_global_del_orig_list(tt_global_entry);
870                         goto add_orig_entry;
871                 }
872
873                 /* if the client was temporary added before receiving the first
874                  * OGM announcing it, we have to clear the TEMP flag
875                  */
876                 common->flags &= ~BATADV_TT_CLIENT_TEMP;
877
878                 /* the change can carry possible "attribute" flags like the
879                  * TT_CLIENT_WIFI, therefore they have to be copied in the
880                  * client entry
881                  */
882                 tt_global_entry->common.flags |= flags;
883
884                 /* If there is the BATADV_TT_CLIENT_ROAM flag set, there is only
885                  * one originator left in the list and we previously received a
886                  * delete + roaming change for this originator.
887                  *
888                  * We should first delete the old originator before adding the
889                  * new one.
890                  */
891                 if (common->flags & BATADV_TT_CLIENT_ROAM) {
892                         batadv_tt_global_del_orig_list(tt_global_entry);
893                         common->flags &= ~BATADV_TT_CLIENT_ROAM;
894                         tt_global_entry->roam_at = 0;
895                 }
896         }
897 add_orig_entry:
898         /* add the new orig_entry (if needed) or update it */
899         batadv_tt_global_orig_entry_add(tt_global_entry, orig_node, ttvn);
900
901         batadv_dbg(BATADV_DBG_TT, bat_priv,
902                    "Creating new global tt entry: %pM (via %pM)\n",
903                    common->addr, orig_node->orig);
904         ret = 1;
905
906 out_remove:
907
908         /* remove address from local hash if present */
909         local_flags = batadv_tt_local_remove(bat_priv, tt_addr,
910                                              "global tt received",
911                                              !!(flags & BATADV_TT_CLIENT_ROAM));
912         tt_global_entry->common.flags |= local_flags & BATADV_TT_CLIENT_WIFI;
913
914         if (!(flags & BATADV_TT_CLIENT_ROAM))
915                 /* this is a normal global add. Therefore the client is not in a
916                  * roaming state anymore.
917                  */
918                 tt_global_entry->common.flags &= ~BATADV_TT_CLIENT_ROAM;
919
920 out:
921         if (tt_global_entry)
922                 batadv_tt_global_entry_free_ref(tt_global_entry);
923         if (tt_local_entry)
924                 batadv_tt_local_entry_free_ref(tt_local_entry);
925         return ret;
926 }
927
928 /* batadv_transtable_best_orig - Get best originator list entry from tt entry
929  * @tt_global_entry: global translation table entry to be analyzed
930  *
931  * This functon assumes the caller holds rcu_read_lock().
932  * Returns best originator list entry or NULL on errors.
933  */
934 static struct batadv_tt_orig_list_entry *
935 batadv_transtable_best_orig(struct batadv_tt_global_entry *tt_global_entry)
936 {
937         struct batadv_neigh_node *router = NULL;
938         struct hlist_head *head;
939         struct batadv_tt_orig_list_entry *orig_entry, *best_entry = NULL;
940         int best_tq = 0;
941
942         head = &tt_global_entry->orig_list;
943         hlist_for_each_entry_rcu(orig_entry, head, list) {
944                 router = batadv_orig_node_get_router(orig_entry->orig_node);
945                 if (!router)
946                         continue;
947
948                 if (router->tq_avg > best_tq) {
949                         best_entry = orig_entry;
950                         best_tq = router->tq_avg;
951                 }
952
953                 batadv_neigh_node_free_ref(router);
954         }
955
956         return best_entry;
957 }
958
959 /* batadv_tt_global_print_entry - print all orig nodes who announce the address
960  * for this global entry
961  * @tt_global_entry: global translation table entry to be printed
962  * @seq: debugfs table seq_file struct
963  *
964  * This functon assumes the caller holds rcu_read_lock().
965  */
966 static void
967 batadv_tt_global_print_entry(struct batadv_tt_global_entry *tt_global_entry,
968                              struct seq_file *seq)
969 {
970         struct hlist_head *head;
971         struct batadv_tt_orig_list_entry *orig_entry, *best_entry;
972         struct batadv_tt_common_entry *tt_common_entry;
973         uint16_t flags;
974         uint8_t last_ttvn;
975
976         tt_common_entry = &tt_global_entry->common;
977         flags = tt_common_entry->flags;
978
979         best_entry = batadv_transtable_best_orig(tt_global_entry);
980         if (best_entry) {
981                 last_ttvn = atomic_read(&best_entry->orig_node->last_ttvn);
982                 seq_printf(seq,
983                            " %c %pM  (%3u) via %pM     (%3u)   (%#.4x) [%c%c%c]\n",
984                            '*', tt_global_entry->common.addr,
985                            best_entry->ttvn, best_entry->orig_node->orig,
986                            last_ttvn, best_entry->orig_node->tt_crc,
987                            (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
988                            (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
989                            (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
990         }
991
992         head = &tt_global_entry->orig_list;
993
994         hlist_for_each_entry_rcu(orig_entry, head, list) {
995                 if (best_entry == orig_entry)
996                         continue;
997
998                 last_ttvn = atomic_read(&orig_entry->orig_node->last_ttvn);
999                 seq_printf(seq, " %c %pM  (%3u) via %pM     (%3u)   [%c%c%c]\n",
1000                            '+', tt_global_entry->common.addr,
1001                            orig_entry->ttvn, orig_entry->orig_node->orig,
1002                            last_ttvn,
1003                            (flags & BATADV_TT_CLIENT_ROAM ? 'R' : '.'),
1004                            (flags & BATADV_TT_CLIENT_WIFI ? 'W' : '.'),
1005                            (flags & BATADV_TT_CLIENT_TEMP ? 'T' : '.'));
1006         }
1007 }
1008
1009 int batadv_tt_global_seq_print_text(struct seq_file *seq, void *offset)
1010 {
1011         struct net_device *net_dev = (struct net_device *)seq->private;
1012         struct batadv_priv *bat_priv = netdev_priv(net_dev);
1013         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1014         struct batadv_tt_common_entry *tt_common_entry;
1015         struct batadv_tt_global_entry *tt_global;
1016         struct batadv_hard_iface *primary_if;
1017         struct hlist_head *head;
1018         uint32_t i;
1019
1020         primary_if = batadv_seq_print_text_primary_if_get(seq);
1021         if (!primary_if)
1022                 goto out;
1023
1024         seq_printf(seq,
1025                    "Globally announced TT entries received via the mesh %s\n",
1026                    net_dev->name);
1027         seq_printf(seq, "       %-13s %s       %-15s %s (%-6s) %s\n",
1028                    "Client", "(TTVN)", "Originator", "(Curr TTVN)", "CRC",
1029                    "Flags");
1030
1031         for (i = 0; i < hash->size; i++) {
1032                 head = &hash->table[i];
1033
1034                 rcu_read_lock();
1035                 hlist_for_each_entry_rcu(tt_common_entry,
1036                                          head, hash_entry) {
1037                         tt_global = container_of(tt_common_entry,
1038                                                  struct batadv_tt_global_entry,
1039                                                  common);
1040                         batadv_tt_global_print_entry(tt_global, seq);
1041                 }
1042                 rcu_read_unlock();
1043         }
1044 out:
1045         if (primary_if)
1046                 batadv_hardif_free_ref(primary_if);
1047         return 0;
1048 }
1049
1050 /* deletes the orig list of a tt_global_entry */
1051 static void
1052 batadv_tt_global_del_orig_list(struct batadv_tt_global_entry *tt_global_entry)
1053 {
1054         struct hlist_head *head;
1055         struct hlist_node *safe;
1056         struct batadv_tt_orig_list_entry *orig_entry;
1057
1058         spin_lock_bh(&tt_global_entry->list_lock);
1059         head = &tt_global_entry->orig_list;
1060         hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1061                 hlist_del_rcu(&orig_entry->list);
1062                 batadv_tt_orig_list_entry_free_ref(orig_entry);
1063         }
1064         spin_unlock_bh(&tt_global_entry->list_lock);
1065 }
1066
1067 static void
1068 batadv_tt_global_del_orig_entry(struct batadv_priv *bat_priv,
1069                                 struct batadv_tt_global_entry *tt_global_entry,
1070                                 struct batadv_orig_node *orig_node,
1071                                 const char *message)
1072 {
1073         struct hlist_head *head;
1074         struct hlist_node *safe;
1075         struct batadv_tt_orig_list_entry *orig_entry;
1076
1077         spin_lock_bh(&tt_global_entry->list_lock);
1078         head = &tt_global_entry->orig_list;
1079         hlist_for_each_entry_safe(orig_entry, safe, head, list) {
1080                 if (orig_entry->orig_node == orig_node) {
1081                         batadv_dbg(BATADV_DBG_TT, bat_priv,
1082                                    "Deleting %pM from global tt entry %pM: %s\n",
1083                                    orig_node->orig,
1084                                    tt_global_entry->common.addr, message);
1085                         hlist_del_rcu(&orig_entry->list);
1086                         batadv_tt_orig_list_entry_free_ref(orig_entry);
1087                 }
1088         }
1089         spin_unlock_bh(&tt_global_entry->list_lock);
1090 }
1091
1092 /* If the client is to be deleted, we check if it is the last origantor entry
1093  * within tt_global entry. If yes, we set the BATADV_TT_CLIENT_ROAM flag and the
1094  * timer, otherwise we simply remove the originator scheduled for deletion.
1095  */
1096 static void
1097 batadv_tt_global_del_roaming(struct batadv_priv *bat_priv,
1098                              struct batadv_tt_global_entry *tt_global_entry,
1099                              struct batadv_orig_node *orig_node,
1100                              const char *message)
1101 {
1102         bool last_entry = true;
1103         struct hlist_head *head;
1104         struct batadv_tt_orig_list_entry *orig_entry;
1105
1106         /* no local entry exists, case 1:
1107          * Check if this is the last one or if other entries exist.
1108          */
1109
1110         rcu_read_lock();
1111         head = &tt_global_entry->orig_list;
1112         hlist_for_each_entry_rcu(orig_entry, head, list) {
1113                 if (orig_entry->orig_node != orig_node) {
1114                         last_entry = false;
1115                         break;
1116                 }
1117         }
1118         rcu_read_unlock();
1119
1120         if (last_entry) {
1121                 /* its the last one, mark for roaming. */
1122                 tt_global_entry->common.flags |= BATADV_TT_CLIENT_ROAM;
1123                 tt_global_entry->roam_at = jiffies;
1124         } else
1125                 /* there is another entry, we can simply delete this
1126                  * one and can still use the other one.
1127                  */
1128                 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1129                                                 orig_node, message);
1130 }
1131
1132
1133
1134 static void batadv_tt_global_del(struct batadv_priv *bat_priv,
1135                                  struct batadv_orig_node *orig_node,
1136                                  const unsigned char *addr,
1137                                  const char *message, bool roaming)
1138 {
1139         struct batadv_tt_global_entry *tt_global_entry;
1140         struct batadv_tt_local_entry *local_entry = NULL;
1141
1142         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1143         if (!tt_global_entry)
1144                 goto out;
1145
1146         if (!roaming) {
1147                 batadv_tt_global_del_orig_entry(bat_priv, tt_global_entry,
1148                                                 orig_node, message);
1149
1150                 if (hlist_empty(&tt_global_entry->orig_list))
1151                         batadv_tt_global_free(bat_priv, tt_global_entry,
1152                                               message);
1153
1154                 goto out;
1155         }
1156
1157         /* if we are deleting a global entry due to a roam
1158          * event, there are two possibilities:
1159          * 1) the client roamed from node A to node B => if there
1160          *    is only one originator left for this client, we mark
1161          *    it with BATADV_TT_CLIENT_ROAM, we start a timer and we
1162          *    wait for node B to claim it. In case of timeout
1163          *    the entry is purged.
1164          *
1165          *    If there are other originators left, we directly delete
1166          *    the originator.
1167          * 2) the client roamed to us => we can directly delete
1168          *    the global entry, since it is useless now.
1169          */
1170         local_entry = batadv_tt_local_hash_find(bat_priv,
1171                                                 tt_global_entry->common.addr);
1172         if (local_entry) {
1173                 /* local entry exists, case 2: client roamed to us. */
1174                 batadv_tt_global_del_orig_list(tt_global_entry);
1175                 batadv_tt_global_free(bat_priv, tt_global_entry, message);
1176         } else
1177                 /* no local entry exists, case 1: check for roaming */
1178                 batadv_tt_global_del_roaming(bat_priv, tt_global_entry,
1179                                              orig_node, message);
1180
1181
1182 out:
1183         if (tt_global_entry)
1184                 batadv_tt_global_entry_free_ref(tt_global_entry);
1185         if (local_entry)
1186                 batadv_tt_local_entry_free_ref(local_entry);
1187 }
1188
1189 void batadv_tt_global_del_orig(struct batadv_priv *bat_priv,
1190                                struct batadv_orig_node *orig_node,
1191                                const char *message)
1192 {
1193         struct batadv_tt_global_entry *tt_global;
1194         struct batadv_tt_common_entry *tt_common_entry;
1195         uint32_t i;
1196         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1197         struct hlist_node *safe;
1198         struct hlist_head *head;
1199         spinlock_t *list_lock; /* protects write access to the hash lists */
1200
1201         if (!hash)
1202                 return;
1203
1204         for (i = 0; i < hash->size; i++) {
1205                 head = &hash->table[i];
1206                 list_lock = &hash->list_locks[i];
1207
1208                 spin_lock_bh(list_lock);
1209                 hlist_for_each_entry_safe(tt_common_entry, safe,
1210                                           head, hash_entry) {
1211                         tt_global = container_of(tt_common_entry,
1212                                                  struct batadv_tt_global_entry,
1213                                                  common);
1214
1215                         batadv_tt_global_del_orig_entry(bat_priv, tt_global,
1216                                                         orig_node, message);
1217
1218                         if (hlist_empty(&tt_global->orig_list)) {
1219                                 batadv_dbg(BATADV_DBG_TT, bat_priv,
1220                                            "Deleting global tt entry %pM: %s\n",
1221                                            tt_global->common.addr, message);
1222                                 hlist_del_rcu(&tt_common_entry->hash_entry);
1223                                 batadv_tt_global_entry_free_ref(tt_global);
1224                         }
1225                 }
1226                 spin_unlock_bh(list_lock);
1227         }
1228         orig_node->tt_initialised = false;
1229 }
1230
1231 static bool batadv_tt_global_to_purge(struct batadv_tt_global_entry *tt_global,
1232                                       char **msg)
1233 {
1234         bool purge = false;
1235         unsigned long roam_timeout = BATADV_TT_CLIENT_ROAM_TIMEOUT;
1236         unsigned long temp_timeout = BATADV_TT_CLIENT_TEMP_TIMEOUT;
1237
1238         if ((tt_global->common.flags & BATADV_TT_CLIENT_ROAM) &&
1239             batadv_has_timed_out(tt_global->roam_at, roam_timeout)) {
1240                 purge = true;
1241                 *msg = "Roaming timeout\n";
1242         }
1243
1244         if ((tt_global->common.flags & BATADV_TT_CLIENT_TEMP) &&
1245             batadv_has_timed_out(tt_global->common.added_at, temp_timeout)) {
1246                 purge = true;
1247                 *msg = "Temporary client timeout\n";
1248         }
1249
1250         return purge;
1251 }
1252
1253 static void batadv_tt_global_purge(struct batadv_priv *bat_priv)
1254 {
1255         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1256         struct hlist_head *head;
1257         struct hlist_node *node_tmp;
1258         spinlock_t *list_lock; /* protects write access to the hash lists */
1259         uint32_t i;
1260         char *msg = NULL;
1261         struct batadv_tt_common_entry *tt_common;
1262         struct batadv_tt_global_entry *tt_global;
1263
1264         for (i = 0; i < hash->size; i++) {
1265                 head = &hash->table[i];
1266                 list_lock = &hash->list_locks[i];
1267
1268                 spin_lock_bh(list_lock);
1269                 hlist_for_each_entry_safe(tt_common, node_tmp, head,
1270                                           hash_entry) {
1271                         tt_global = container_of(tt_common,
1272                                                  struct batadv_tt_global_entry,
1273                                                  common);
1274
1275                         if (!batadv_tt_global_to_purge(tt_global, &msg))
1276                                 continue;
1277
1278                         batadv_dbg(BATADV_DBG_TT, bat_priv,
1279                                    "Deleting global tt entry (%pM): %s\n",
1280                                    tt_global->common.addr, msg);
1281
1282                         hlist_del_rcu(&tt_common->hash_entry);
1283
1284                         batadv_tt_global_entry_free_ref(tt_global);
1285                 }
1286                 spin_unlock_bh(list_lock);
1287         }
1288 }
1289
1290 static void batadv_tt_global_table_free(struct batadv_priv *bat_priv)
1291 {
1292         struct batadv_hashtable *hash;
1293         spinlock_t *list_lock; /* protects write access to the hash lists */
1294         struct batadv_tt_common_entry *tt_common_entry;
1295         struct batadv_tt_global_entry *tt_global;
1296         struct hlist_node *node_tmp;
1297         struct hlist_head *head;
1298         uint32_t i;
1299
1300         if (!bat_priv->tt.global_hash)
1301                 return;
1302
1303         hash = bat_priv->tt.global_hash;
1304
1305         for (i = 0; i < hash->size; i++) {
1306                 head = &hash->table[i];
1307                 list_lock = &hash->list_locks[i];
1308
1309                 spin_lock_bh(list_lock);
1310                 hlist_for_each_entry_safe(tt_common_entry, node_tmp,
1311                                           head, hash_entry) {
1312                         hlist_del_rcu(&tt_common_entry->hash_entry);
1313                         tt_global = container_of(tt_common_entry,
1314                                                  struct batadv_tt_global_entry,
1315                                                  common);
1316                         batadv_tt_global_entry_free_ref(tt_global);
1317                 }
1318                 spin_unlock_bh(list_lock);
1319         }
1320
1321         batadv_hash_destroy(hash);
1322
1323         bat_priv->tt.global_hash = NULL;
1324 }
1325
1326 static bool
1327 _batadv_is_ap_isolated(struct batadv_tt_local_entry *tt_local_entry,
1328                        struct batadv_tt_global_entry *tt_global_entry)
1329 {
1330         bool ret = false;
1331
1332         if (tt_local_entry->common.flags & BATADV_TT_CLIENT_WIFI &&
1333             tt_global_entry->common.flags & BATADV_TT_CLIENT_WIFI)
1334                 ret = true;
1335
1336         return ret;
1337 }
1338
1339 struct batadv_orig_node *batadv_transtable_search(struct batadv_priv *bat_priv,
1340                                                   const uint8_t *src,
1341                                                   const uint8_t *addr)
1342 {
1343         struct batadv_tt_local_entry *tt_local_entry = NULL;
1344         struct batadv_tt_global_entry *tt_global_entry = NULL;
1345         struct batadv_orig_node *orig_node = NULL;
1346         struct batadv_tt_orig_list_entry *best_entry;
1347
1348         if (src && atomic_read(&bat_priv->ap_isolation)) {
1349                 tt_local_entry = batadv_tt_local_hash_find(bat_priv, src);
1350                 if (!tt_local_entry ||
1351                     (tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING))
1352                         goto out;
1353         }
1354
1355         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
1356         if (!tt_global_entry)
1357                 goto out;
1358
1359         /* check whether the clients should not communicate due to AP
1360          * isolation
1361          */
1362         if (tt_local_entry &&
1363             _batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
1364                 goto out;
1365
1366         rcu_read_lock();
1367         best_entry = batadv_transtable_best_orig(tt_global_entry);
1368         /* found anything? */
1369         if (best_entry)
1370                 orig_node = best_entry->orig_node;
1371         if (orig_node && !atomic_inc_not_zero(&orig_node->refcount))
1372                 orig_node = NULL;
1373         rcu_read_unlock();
1374
1375 out:
1376         if (tt_global_entry)
1377                 batadv_tt_global_entry_free_ref(tt_global_entry);
1378         if (tt_local_entry)
1379                 batadv_tt_local_entry_free_ref(tt_local_entry);
1380
1381         return orig_node;
1382 }
1383
1384 /* Calculates the checksum of the local table of a given orig_node */
1385 static uint16_t batadv_tt_global_crc(struct batadv_priv *bat_priv,
1386                                      struct batadv_orig_node *orig_node)
1387 {
1388         uint16_t total = 0, total_one;
1389         struct batadv_hashtable *hash = bat_priv->tt.global_hash;
1390         struct batadv_tt_common_entry *tt_common;
1391         struct batadv_tt_global_entry *tt_global;
1392         struct hlist_head *head;
1393         uint32_t i;
1394         int j;
1395
1396         for (i = 0; i < hash->size; i++) {
1397                 head = &hash->table[i];
1398
1399                 rcu_read_lock();
1400                 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
1401                         tt_global = container_of(tt_common,
1402                                                  struct batadv_tt_global_entry,
1403                                                  common);
1404                         /* Roaming clients are in the global table for
1405                          * consistency only. They don't have to be
1406                          * taken into account while computing the
1407                          * global crc
1408                          */
1409                         if (tt_common->flags & BATADV_TT_CLIENT_ROAM)
1410                                 continue;
1411                         /* Temporary clients have not been announced yet, so
1412                          * they have to be skipped while computing the global
1413                          * crc
1414                          */
1415                         if (tt_common->flags & BATADV_TT_CLIENT_TEMP)
1416                                 continue;
1417
1418                         /* find out if this global entry is announced by this
1419                          * originator
1420                          */
1421                         if (!batadv_tt_global_entry_has_orig(tt_global,
1422                                                              orig_node))
1423                                 continue;
1424
1425                         total_one = 0;
1426                         for (j = 0; j < ETH_ALEN; j++)
1427                                 total_one = crc16_byte(total_one,
1428                                                        tt_common->addr[j]);
1429                         total ^= total_one;
1430                 }
1431                 rcu_read_unlock();
1432         }
1433
1434         return total;
1435 }
1436
1437 /* Calculates the checksum of the local table */
1438 static uint16_t batadv_tt_local_crc(struct batadv_priv *bat_priv)
1439 {
1440         uint16_t total = 0, total_one;
1441         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
1442         struct batadv_tt_common_entry *tt_common;
1443         struct hlist_head *head;
1444         uint32_t i;
1445         int j;
1446
1447         for (i = 0; i < hash->size; i++) {
1448                 head = &hash->table[i];
1449
1450                 rcu_read_lock();
1451                 hlist_for_each_entry_rcu(tt_common, head, hash_entry) {
1452                         /* not yet committed clients have not to be taken into
1453                          * account while computing the CRC
1454                          */
1455                         if (tt_common->flags & BATADV_TT_CLIENT_NEW)
1456                                 continue;
1457                         total_one = 0;
1458                         for (j = 0; j < ETH_ALEN; j++)
1459                                 total_one = crc16_byte(total_one,
1460                                                        tt_common->addr[j]);
1461                         total ^= total_one;
1462                 }
1463                 rcu_read_unlock();
1464         }
1465
1466         return total;
1467 }
1468
1469 static void batadv_tt_req_list_free(struct batadv_priv *bat_priv)
1470 {
1471         struct batadv_tt_req_node *node, *safe;
1472
1473         spin_lock_bh(&bat_priv->tt.req_list_lock);
1474
1475         list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1476                 list_del(&node->list);
1477                 kfree(node);
1478         }
1479
1480         spin_unlock_bh(&bat_priv->tt.req_list_lock);
1481 }
1482
1483 static void batadv_tt_save_orig_buffer(struct batadv_priv *bat_priv,
1484                                        struct batadv_orig_node *orig_node,
1485                                        const unsigned char *tt_buff,
1486                                        uint8_t tt_num_changes)
1487 {
1488         uint16_t tt_buff_len = batadv_tt_len(tt_num_changes);
1489
1490         /* Replace the old buffer only if I received something in the
1491          * last OGM (the OGM could carry no changes)
1492          */
1493         spin_lock_bh(&orig_node->tt_buff_lock);
1494         if (tt_buff_len > 0) {
1495                 kfree(orig_node->tt_buff);
1496                 orig_node->tt_buff_len = 0;
1497                 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
1498                 if (orig_node->tt_buff) {
1499                         memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
1500                         orig_node->tt_buff_len = tt_buff_len;
1501                 }
1502         }
1503         spin_unlock_bh(&orig_node->tt_buff_lock);
1504 }
1505
1506 static void batadv_tt_req_purge(struct batadv_priv *bat_priv)
1507 {
1508         struct batadv_tt_req_node *node, *safe;
1509
1510         spin_lock_bh(&bat_priv->tt.req_list_lock);
1511         list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
1512                 if (batadv_has_timed_out(node->issued_at,
1513                                          BATADV_TT_REQUEST_TIMEOUT)) {
1514                         list_del(&node->list);
1515                         kfree(node);
1516                 }
1517         }
1518         spin_unlock_bh(&bat_priv->tt.req_list_lock);
1519 }
1520
1521 /* returns the pointer to the new tt_req_node struct if no request
1522  * has already been issued for this orig_node, NULL otherwise
1523  */
1524 static struct batadv_tt_req_node *
1525 batadv_new_tt_req_node(struct batadv_priv *bat_priv,
1526                        struct batadv_orig_node *orig_node)
1527 {
1528         struct batadv_tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
1529
1530         spin_lock_bh(&bat_priv->tt.req_list_lock);
1531         list_for_each_entry(tt_req_node_tmp, &bat_priv->tt.req_list, list) {
1532                 if (batadv_compare_eth(tt_req_node_tmp, orig_node) &&
1533                     !batadv_has_timed_out(tt_req_node_tmp->issued_at,
1534                                           BATADV_TT_REQUEST_TIMEOUT))
1535                         goto unlock;
1536         }
1537
1538         tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
1539         if (!tt_req_node)
1540                 goto unlock;
1541
1542         memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
1543         tt_req_node->issued_at = jiffies;
1544
1545         list_add(&tt_req_node->list, &bat_priv->tt.req_list);
1546 unlock:
1547         spin_unlock_bh(&bat_priv->tt.req_list_lock);
1548         return tt_req_node;
1549 }
1550
1551 /* data_ptr is useless here, but has to be kept to respect the prototype */
1552 static int batadv_tt_local_valid_entry(const void *entry_ptr,
1553                                        const void *data_ptr)
1554 {
1555         const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1556
1557         if (tt_common_entry->flags & BATADV_TT_CLIENT_NEW)
1558                 return 0;
1559         return 1;
1560 }
1561
1562 static int batadv_tt_global_valid(const void *entry_ptr,
1563                                   const void *data_ptr)
1564 {
1565         const struct batadv_tt_common_entry *tt_common_entry = entry_ptr;
1566         const struct batadv_tt_global_entry *tt_global_entry;
1567         const struct batadv_orig_node *orig_node = data_ptr;
1568
1569         if (tt_common_entry->flags & BATADV_TT_CLIENT_ROAM ||
1570             tt_common_entry->flags & BATADV_TT_CLIENT_TEMP)
1571                 return 0;
1572
1573         tt_global_entry = container_of(tt_common_entry,
1574                                        struct batadv_tt_global_entry,
1575                                        common);
1576
1577         return batadv_tt_global_entry_has_orig(tt_global_entry, orig_node);
1578 }
1579
1580 static struct sk_buff *
1581 batadv_tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
1582                               struct batadv_hashtable *hash,
1583                               struct batadv_hard_iface *primary_if,
1584                               int (*valid_cb)(const void *, const void *),
1585                               void *cb_data)
1586 {
1587         struct batadv_tt_common_entry *tt_common_entry;
1588         struct batadv_tt_query_packet *tt_response;
1589         struct batadv_tt_change *tt_change;
1590         struct hlist_head *head;
1591         struct sk_buff *skb = NULL;
1592         uint16_t tt_tot, tt_count;
1593         ssize_t tt_query_size = sizeof(struct batadv_tt_query_packet);
1594         uint32_t i;
1595         size_t len;
1596
1597         if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
1598                 tt_len = primary_if->soft_iface->mtu - tt_query_size;
1599                 tt_len -= tt_len % sizeof(struct batadv_tt_change);
1600         }
1601         tt_tot = tt_len / sizeof(struct batadv_tt_change);
1602
1603         len = tt_query_size + tt_len;
1604         skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1605         if (!skb)
1606                 goto out;
1607
1608         skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1609         tt_response = (struct batadv_tt_query_packet *)skb_put(skb, len);
1610         tt_response->ttvn = ttvn;
1611
1612         tt_change = (struct batadv_tt_change *)(skb->data + tt_query_size);
1613         tt_count = 0;
1614
1615         rcu_read_lock();
1616         for (i = 0; i < hash->size; i++) {
1617                 head = &hash->table[i];
1618
1619                 hlist_for_each_entry_rcu(tt_common_entry,
1620                                          head, hash_entry) {
1621                         if (tt_count == tt_tot)
1622                                 break;
1623
1624                         if ((valid_cb) && (!valid_cb(tt_common_entry, cb_data)))
1625                                 continue;
1626
1627                         memcpy(tt_change->addr, tt_common_entry->addr,
1628                                ETH_ALEN);
1629                         tt_change->flags = tt_common_entry->flags;
1630
1631                         tt_count++;
1632                         tt_change++;
1633                 }
1634         }
1635         rcu_read_unlock();
1636
1637         /* store in the message the number of entries we have successfully
1638          * copied
1639          */
1640         tt_response->tt_data = htons(tt_count);
1641
1642 out:
1643         return skb;
1644 }
1645
1646 static int batadv_send_tt_request(struct batadv_priv *bat_priv,
1647                                   struct batadv_orig_node *dst_orig_node,
1648                                   uint8_t ttvn, uint16_t tt_crc,
1649                                   bool full_table)
1650 {
1651         struct sk_buff *skb = NULL;
1652         struct batadv_tt_query_packet *tt_request;
1653         struct batadv_hard_iface *primary_if;
1654         struct batadv_tt_req_node *tt_req_node = NULL;
1655         int ret = 1;
1656         size_t tt_req_len;
1657
1658         primary_if = batadv_primary_if_get_selected(bat_priv);
1659         if (!primary_if)
1660                 goto out;
1661
1662         /* The new tt_req will be issued only if I'm not waiting for a
1663          * reply from the same orig_node yet
1664          */
1665         tt_req_node = batadv_new_tt_req_node(bat_priv, dst_orig_node);
1666         if (!tt_req_node)
1667                 goto out;
1668
1669         skb = dev_alloc_skb(sizeof(*tt_request) + ETH_HLEN + NET_IP_ALIGN);
1670         if (!skb)
1671                 goto out;
1672
1673         skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1674
1675         tt_req_len = sizeof(*tt_request);
1676         tt_request = (struct batadv_tt_query_packet *)skb_put(skb, tt_req_len);
1677
1678         tt_request->header.packet_type = BATADV_TT_QUERY;
1679         tt_request->header.version = BATADV_COMPAT_VERSION;
1680         memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1681         memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1682         tt_request->header.ttl = BATADV_TTL;
1683         tt_request->ttvn = ttvn;
1684         tt_request->tt_data = htons(tt_crc);
1685         tt_request->flags = BATADV_TT_REQUEST;
1686
1687         if (full_table)
1688                 tt_request->flags |= BATADV_TT_FULL_TABLE;
1689
1690         batadv_dbg(BATADV_DBG_TT, bat_priv, "Sending TT_REQUEST to %pM [%c]\n",
1691                    dst_orig_node->orig, (full_table ? 'F' : '.'));
1692
1693         batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_TX);
1694
1695         if (batadv_send_skb_to_orig(skb, dst_orig_node, NULL))
1696                 ret = 0;
1697
1698 out:
1699         if (primary_if)
1700                 batadv_hardif_free_ref(primary_if);
1701         if (ret)
1702                 kfree_skb(skb);
1703         if (ret && tt_req_node) {
1704                 spin_lock_bh(&bat_priv->tt.req_list_lock);
1705                 list_del(&tt_req_node->list);
1706                 spin_unlock_bh(&bat_priv->tt.req_list_lock);
1707                 kfree(tt_req_node);
1708         }
1709         return ret;
1710 }
1711
1712 static bool
1713 batadv_send_other_tt_response(struct batadv_priv *bat_priv,
1714                               struct batadv_tt_query_packet *tt_request)
1715 {
1716         struct batadv_orig_node *req_dst_orig_node;
1717         struct batadv_orig_node *res_dst_orig_node = NULL;
1718         struct batadv_hard_iface *primary_if = NULL;
1719         uint8_t orig_ttvn, req_ttvn, ttvn;
1720         int ret = false;
1721         unsigned char *tt_buff;
1722         bool full_table;
1723         uint16_t tt_len, tt_tot;
1724         struct sk_buff *skb = NULL;
1725         struct batadv_tt_query_packet *tt_response;
1726         uint8_t *packet_pos;
1727         size_t len;
1728
1729         batadv_dbg(BATADV_DBG_TT, bat_priv,
1730                    "Received TT_REQUEST from %pM for ttvn: %u (%pM) [%c]\n",
1731                    tt_request->src, tt_request->ttvn, tt_request->dst,
1732                    (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1733
1734         /* Let's get the orig node of the REAL destination */
1735         req_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->dst);
1736         if (!req_dst_orig_node)
1737                 goto out;
1738
1739         res_dst_orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1740         if (!res_dst_orig_node)
1741                 goto out;
1742
1743         primary_if = batadv_primary_if_get_selected(bat_priv);
1744         if (!primary_if)
1745                 goto out;
1746
1747         orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1748         req_ttvn = tt_request->ttvn;
1749
1750         /* I don't have the requested data */
1751         if (orig_ttvn != req_ttvn ||
1752             tt_request->tt_data != htons(req_dst_orig_node->tt_crc))
1753                 goto out;
1754
1755         /* If the full table has been explicitly requested */
1756         if (tt_request->flags & BATADV_TT_FULL_TABLE ||
1757             !req_dst_orig_node->tt_buff)
1758                 full_table = true;
1759         else
1760                 full_table = false;
1761
1762         /* In this version, fragmentation is not implemented, then
1763          * I'll send only one packet with as much TT entries as I can
1764          */
1765         if (!full_table) {
1766                 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1767                 tt_len = req_dst_orig_node->tt_buff_len;
1768                 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1769
1770                 len = sizeof(*tt_response) + tt_len;
1771                 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1772                 if (!skb)
1773                         goto unlock;
1774
1775                 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1776                 packet_pos = skb_put(skb, len);
1777                 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1778                 tt_response->ttvn = req_ttvn;
1779                 tt_response->tt_data = htons(tt_tot);
1780
1781                 tt_buff = skb->data + sizeof(*tt_response);
1782                 /* Copy the last orig_node's OGM buffer */
1783                 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1784                        req_dst_orig_node->tt_buff_len);
1785
1786                 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1787         } else {
1788                 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size);
1789                 tt_len *= sizeof(struct batadv_tt_change);
1790                 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1791
1792                 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1793                                                     bat_priv->tt.global_hash,
1794                                                     primary_if,
1795                                                     batadv_tt_global_valid,
1796                                                     req_dst_orig_node);
1797                 if (!skb)
1798                         goto out;
1799
1800                 tt_response = (struct batadv_tt_query_packet *)skb->data;
1801         }
1802
1803         tt_response->header.packet_type = BATADV_TT_QUERY;
1804         tt_response->header.version = BATADV_COMPAT_VERSION;
1805         tt_response->header.ttl = BATADV_TTL;
1806         memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1807         memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1808         tt_response->flags = BATADV_TT_RESPONSE;
1809
1810         if (full_table)
1811                 tt_response->flags |= BATADV_TT_FULL_TABLE;
1812
1813         batadv_dbg(BATADV_DBG_TT, bat_priv,
1814                    "Sending TT_RESPONSE %pM for %pM (ttvn: %u)\n",
1815                    res_dst_orig_node->orig, req_dst_orig_node->orig, req_ttvn);
1816
1817         batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1818
1819         if (batadv_send_skb_to_orig(skb, res_dst_orig_node, NULL))
1820                 ret = true;
1821         goto out;
1822
1823 unlock:
1824         spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1825
1826 out:
1827         if (res_dst_orig_node)
1828                 batadv_orig_node_free_ref(res_dst_orig_node);
1829         if (req_dst_orig_node)
1830                 batadv_orig_node_free_ref(req_dst_orig_node);
1831         if (primary_if)
1832                 batadv_hardif_free_ref(primary_if);
1833         if (!ret)
1834                 kfree_skb(skb);
1835         return ret;
1836 }
1837
1838 static bool
1839 batadv_send_my_tt_response(struct batadv_priv *bat_priv,
1840                            struct batadv_tt_query_packet *tt_request)
1841 {
1842         struct batadv_orig_node *orig_node;
1843         struct batadv_hard_iface *primary_if = NULL;
1844         uint8_t my_ttvn, req_ttvn, ttvn;
1845         int ret = false;
1846         unsigned char *tt_buff;
1847         bool full_table;
1848         uint16_t tt_len, tt_tot;
1849         struct sk_buff *skb = NULL;
1850         struct batadv_tt_query_packet *tt_response;
1851         uint8_t *packet_pos;
1852         size_t len;
1853
1854         batadv_dbg(BATADV_DBG_TT, bat_priv,
1855                    "Received TT_REQUEST from %pM for ttvn: %u (me) [%c]\n",
1856                    tt_request->src, tt_request->ttvn,
1857                    (tt_request->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1858
1859
1860         my_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1861         req_ttvn = tt_request->ttvn;
1862
1863         orig_node = batadv_orig_hash_find(bat_priv, tt_request->src);
1864         if (!orig_node)
1865                 goto out;
1866
1867         primary_if = batadv_primary_if_get_selected(bat_priv);
1868         if (!primary_if)
1869                 goto out;
1870
1871         /* If the full table has been explicitly requested or the gap
1872          * is too big send the whole local translation table
1873          */
1874         if (tt_request->flags & BATADV_TT_FULL_TABLE || my_ttvn != req_ttvn ||
1875             !bat_priv->tt.last_changeset)
1876                 full_table = true;
1877         else
1878                 full_table = false;
1879
1880         /* In this version, fragmentation is not implemented, then
1881          * I'll send only one packet with as much TT entries as I can
1882          */
1883         if (!full_table) {
1884                 spin_lock_bh(&bat_priv->tt.last_changeset_lock);
1885                 tt_len = bat_priv->tt.last_changeset_len;
1886                 tt_tot = tt_len / sizeof(struct batadv_tt_change);
1887
1888                 len = sizeof(*tt_response) + tt_len;
1889                 skb = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
1890                 if (!skb)
1891                         goto unlock;
1892
1893                 skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
1894                 packet_pos = skb_put(skb, len);
1895                 tt_response = (struct batadv_tt_query_packet *)packet_pos;
1896                 tt_response->ttvn = req_ttvn;
1897                 tt_response->tt_data = htons(tt_tot);
1898
1899                 tt_buff = skb->data + sizeof(*tt_response);
1900                 memcpy(tt_buff, bat_priv->tt.last_changeset,
1901                        bat_priv->tt.last_changeset_len);
1902                 spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1903         } else {
1904                 tt_len = (uint16_t)atomic_read(&bat_priv->tt.local_entry_num);
1905                 tt_len *= sizeof(struct batadv_tt_change);
1906                 ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
1907
1908                 skb = batadv_tt_response_fill_table(tt_len, ttvn,
1909                                                     bat_priv->tt.local_hash,
1910                                                     primary_if,
1911                                                     batadv_tt_local_valid_entry,
1912                                                     NULL);
1913                 if (!skb)
1914                         goto out;
1915
1916                 tt_response = (struct batadv_tt_query_packet *)skb->data;
1917         }
1918
1919         tt_response->header.packet_type = BATADV_TT_QUERY;
1920         tt_response->header.version = BATADV_COMPAT_VERSION;
1921         tt_response->header.ttl = BATADV_TTL;
1922         memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1923         memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1924         tt_response->flags = BATADV_TT_RESPONSE;
1925
1926         if (full_table)
1927                 tt_response->flags |= BATADV_TT_FULL_TABLE;
1928
1929         batadv_dbg(BATADV_DBG_TT, bat_priv,
1930                    "Sending TT_RESPONSE to %pM [%c]\n",
1931                    orig_node->orig,
1932                    (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
1933
1934         batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_TX);
1935
1936         if (batadv_send_skb_to_orig(skb, orig_node, NULL))
1937                 ret = true;
1938         goto out;
1939
1940 unlock:
1941         spin_unlock_bh(&bat_priv->tt.last_changeset_lock);
1942 out:
1943         if (orig_node)
1944                 batadv_orig_node_free_ref(orig_node);
1945         if (primary_if)
1946                 batadv_hardif_free_ref(primary_if);
1947         if (!ret)
1948                 kfree_skb(skb);
1949         /* This packet was for me, so it doesn't need to be re-routed */
1950         return true;
1951 }
1952
1953 bool batadv_send_tt_response(struct batadv_priv *bat_priv,
1954                              struct batadv_tt_query_packet *tt_request)
1955 {
1956         if (batadv_is_my_mac(tt_request->dst)) {
1957                 /* don't answer backbone gws! */
1958                 if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_request->src))
1959                         return true;
1960
1961                 return batadv_send_my_tt_response(bat_priv, tt_request);
1962         } else {
1963                 return batadv_send_other_tt_response(bat_priv, tt_request);
1964         }
1965 }
1966
1967 static void _batadv_tt_update_changes(struct batadv_priv *bat_priv,
1968                                       struct batadv_orig_node *orig_node,
1969                                       struct batadv_tt_change *tt_change,
1970                                       uint16_t tt_num_changes, uint8_t ttvn)
1971 {
1972         int i;
1973         int roams;
1974
1975         for (i = 0; i < tt_num_changes; i++) {
1976                 if ((tt_change + i)->flags & BATADV_TT_CLIENT_DEL) {
1977                         roams = (tt_change + i)->flags & BATADV_TT_CLIENT_ROAM;
1978                         batadv_tt_global_del(bat_priv, orig_node,
1979                                              (tt_change + i)->addr,
1980                                              "tt removed by changes",
1981                                              roams);
1982                 } else {
1983                         if (!batadv_tt_global_add(bat_priv, orig_node,
1984                                                   (tt_change + i)->addr,
1985                                                   (tt_change + i)->flags, ttvn))
1986                                 /* In case of problem while storing a
1987                                  * global_entry, we stop the updating
1988                                  * procedure without committing the
1989                                  * ttvn change. This will avoid to send
1990                                  * corrupted data on tt_request
1991                                  */
1992                                 return;
1993                 }
1994         }
1995         orig_node->tt_initialised = true;
1996 }
1997
1998 static void batadv_tt_fill_gtable(struct batadv_priv *bat_priv,
1999                                   struct batadv_tt_query_packet *tt_response)
2000 {
2001         struct batadv_orig_node *orig_node;
2002
2003         orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2004         if (!orig_node)
2005                 goto out;
2006
2007         /* Purge the old table first.. */
2008         batadv_tt_global_del_orig(bat_priv, orig_node, "Received full table");
2009
2010         _batadv_tt_update_changes(bat_priv, orig_node,
2011                                   (struct batadv_tt_change *)(tt_response + 1),
2012                                   ntohs(tt_response->tt_data),
2013                                   tt_response->ttvn);
2014
2015         spin_lock_bh(&orig_node->tt_buff_lock);
2016         kfree(orig_node->tt_buff);
2017         orig_node->tt_buff_len = 0;
2018         orig_node->tt_buff = NULL;
2019         spin_unlock_bh(&orig_node->tt_buff_lock);
2020
2021         atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
2022
2023 out:
2024         if (orig_node)
2025                 batadv_orig_node_free_ref(orig_node);
2026 }
2027
2028 static void batadv_tt_update_changes(struct batadv_priv *bat_priv,
2029                                      struct batadv_orig_node *orig_node,
2030                                      uint16_t tt_num_changes, uint8_t ttvn,
2031                                      struct batadv_tt_change *tt_change)
2032 {
2033         _batadv_tt_update_changes(bat_priv, orig_node, tt_change,
2034                                   tt_num_changes, ttvn);
2035
2036         batadv_tt_save_orig_buffer(bat_priv, orig_node,
2037                                    (unsigned char *)tt_change, tt_num_changes);
2038         atomic_set(&orig_node->last_ttvn, ttvn);
2039 }
2040
2041 bool batadv_is_my_client(struct batadv_priv *bat_priv, const uint8_t *addr)
2042 {
2043         struct batadv_tt_local_entry *tt_local_entry;
2044         bool ret = false;
2045
2046         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2047         if (!tt_local_entry)
2048                 goto out;
2049         /* Check if the client has been logically deleted (but is kept for
2050          * consistency purpose)
2051          */
2052         if ((tt_local_entry->common.flags & BATADV_TT_CLIENT_PENDING) ||
2053             (tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM))
2054                 goto out;
2055         ret = true;
2056 out:
2057         if (tt_local_entry)
2058                 batadv_tt_local_entry_free_ref(tt_local_entry);
2059         return ret;
2060 }
2061
2062 void batadv_handle_tt_response(struct batadv_priv *bat_priv,
2063                                struct batadv_tt_query_packet *tt_response)
2064 {
2065         struct batadv_tt_req_node *node, *safe;
2066         struct batadv_orig_node *orig_node = NULL;
2067         struct batadv_tt_change *tt_change;
2068
2069         batadv_dbg(BATADV_DBG_TT, bat_priv,
2070                    "Received TT_RESPONSE from %pM for ttvn %d t_size: %d [%c]\n",
2071                    tt_response->src, tt_response->ttvn,
2072                    ntohs(tt_response->tt_data),
2073                    (tt_response->flags & BATADV_TT_FULL_TABLE ? 'F' : '.'));
2074
2075         /* we should have never asked a backbone gw */
2076         if (batadv_bla_is_backbone_gw_orig(bat_priv, tt_response->src))
2077                 goto out;
2078
2079         orig_node = batadv_orig_hash_find(bat_priv, tt_response->src);
2080         if (!orig_node)
2081                 goto out;
2082
2083         if (tt_response->flags & BATADV_TT_FULL_TABLE) {
2084                 batadv_tt_fill_gtable(bat_priv, tt_response);
2085         } else {
2086                 tt_change = (struct batadv_tt_change *)(tt_response + 1);
2087                 batadv_tt_update_changes(bat_priv, orig_node,
2088                                          ntohs(tt_response->tt_data),
2089                                          tt_response->ttvn, tt_change);
2090         }
2091
2092         /* Delete the tt_req_node from pending tt_requests list */
2093         spin_lock_bh(&bat_priv->tt.req_list_lock);
2094         list_for_each_entry_safe(node, safe, &bat_priv->tt.req_list, list) {
2095                 if (!batadv_compare_eth(node->addr, tt_response->src))
2096                         continue;
2097                 list_del(&node->list);
2098                 kfree(node);
2099         }
2100         spin_unlock_bh(&bat_priv->tt.req_list_lock);
2101
2102         /* Recalculate the CRC for this orig_node and store it */
2103         orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2104 out:
2105         if (orig_node)
2106                 batadv_orig_node_free_ref(orig_node);
2107 }
2108
2109 int batadv_tt_init(struct batadv_priv *bat_priv)
2110 {
2111         int ret;
2112
2113         ret = batadv_tt_local_init(bat_priv);
2114         if (ret < 0)
2115                 return ret;
2116
2117         ret = batadv_tt_global_init(bat_priv);
2118         if (ret < 0)
2119                 return ret;
2120
2121         INIT_DELAYED_WORK(&bat_priv->tt.work, batadv_tt_purge);
2122         queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2123                            msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2124
2125         return 1;
2126 }
2127
2128 static void batadv_tt_roam_list_free(struct batadv_priv *bat_priv)
2129 {
2130         struct batadv_tt_roam_node *node, *safe;
2131
2132         spin_lock_bh(&bat_priv->tt.roam_list_lock);
2133
2134         list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2135                 list_del(&node->list);
2136                 kfree(node);
2137         }
2138
2139         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2140 }
2141
2142 static void batadv_tt_roam_purge(struct batadv_priv *bat_priv)
2143 {
2144         struct batadv_tt_roam_node *node, *safe;
2145
2146         spin_lock_bh(&bat_priv->tt.roam_list_lock);
2147         list_for_each_entry_safe(node, safe, &bat_priv->tt.roam_list, list) {
2148                 if (!batadv_has_timed_out(node->first_time,
2149                                           BATADV_ROAMING_MAX_TIME))
2150                         continue;
2151
2152                 list_del(&node->list);
2153                 kfree(node);
2154         }
2155         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2156 }
2157
2158 /* This function checks whether the client already reached the
2159  * maximum number of possible roaming phases. In this case the ROAMING_ADV
2160  * will not be sent.
2161  *
2162  * returns true if the ROAMING_ADV can be sent, false otherwise
2163  */
2164 static bool batadv_tt_check_roam_count(struct batadv_priv *bat_priv,
2165                                        uint8_t *client)
2166 {
2167         struct batadv_tt_roam_node *tt_roam_node;
2168         bool ret = false;
2169
2170         spin_lock_bh(&bat_priv->tt.roam_list_lock);
2171         /* The new tt_req will be issued only if I'm not waiting for a
2172          * reply from the same orig_node yet
2173          */
2174         list_for_each_entry(tt_roam_node, &bat_priv->tt.roam_list, list) {
2175                 if (!batadv_compare_eth(tt_roam_node->addr, client))
2176                         continue;
2177
2178                 if (batadv_has_timed_out(tt_roam_node->first_time,
2179                                          BATADV_ROAMING_MAX_TIME))
2180                         continue;
2181
2182                 if (!batadv_atomic_dec_not_zero(&tt_roam_node->counter))
2183                         /* Sorry, you roamed too many times! */
2184                         goto unlock;
2185                 ret = true;
2186                 break;
2187         }
2188
2189         if (!ret) {
2190                 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
2191                 if (!tt_roam_node)
2192                         goto unlock;
2193
2194                 tt_roam_node->first_time = jiffies;
2195                 atomic_set(&tt_roam_node->counter,
2196                            BATADV_ROAMING_MAX_COUNT - 1);
2197                 memcpy(tt_roam_node->addr, client, ETH_ALEN);
2198
2199                 list_add(&tt_roam_node->list, &bat_priv->tt.roam_list);
2200                 ret = true;
2201         }
2202
2203 unlock:
2204         spin_unlock_bh(&bat_priv->tt.roam_list_lock);
2205         return ret;
2206 }
2207
2208 static void batadv_send_roam_adv(struct batadv_priv *bat_priv, uint8_t *client,
2209                                  struct batadv_orig_node *orig_node)
2210 {
2211         struct sk_buff *skb = NULL;
2212         struct batadv_roam_adv_packet *roam_adv_packet;
2213         int ret = 1;
2214         struct batadv_hard_iface *primary_if;
2215         size_t len = sizeof(*roam_adv_packet);
2216
2217         /* before going on we have to check whether the client has
2218          * already roamed to us too many times
2219          */
2220         if (!batadv_tt_check_roam_count(bat_priv, client))
2221                 goto out;
2222
2223         skb = dev_alloc_skb(sizeof(*roam_adv_packet) + ETH_HLEN + NET_IP_ALIGN);
2224         if (!skb)
2225                 goto out;
2226
2227         skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
2228
2229         roam_adv_packet = (struct batadv_roam_adv_packet *)skb_put(skb, len);
2230
2231         roam_adv_packet->header.packet_type = BATADV_ROAM_ADV;
2232         roam_adv_packet->header.version = BATADV_COMPAT_VERSION;
2233         roam_adv_packet->header.ttl = BATADV_TTL;
2234         roam_adv_packet->reserved = 0;
2235         primary_if = batadv_primary_if_get_selected(bat_priv);
2236         if (!primary_if)
2237                 goto out;
2238         memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
2239         batadv_hardif_free_ref(primary_if);
2240         memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
2241         memcpy(roam_adv_packet->client, client, ETH_ALEN);
2242
2243         batadv_dbg(BATADV_DBG_TT, bat_priv,
2244                    "Sending ROAMING_ADV to %pM (client %pM)\n",
2245                    orig_node->orig, client);
2246
2247         batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_TX);
2248
2249         if (batadv_send_skb_to_orig(skb, orig_node, NULL))
2250                 ret = 0;
2251
2252 out:
2253         if (ret && skb)
2254                 kfree_skb(skb);
2255         return;
2256 }
2257
2258 static void batadv_tt_purge(struct work_struct *work)
2259 {
2260         struct delayed_work *delayed_work;
2261         struct batadv_priv_tt *priv_tt;
2262         struct batadv_priv *bat_priv;
2263
2264         delayed_work = container_of(work, struct delayed_work, work);
2265         priv_tt = container_of(delayed_work, struct batadv_priv_tt, work);
2266         bat_priv = container_of(priv_tt, struct batadv_priv, tt);
2267
2268         batadv_tt_local_purge(bat_priv);
2269         batadv_tt_global_purge(bat_priv);
2270         batadv_tt_req_purge(bat_priv);
2271         batadv_tt_roam_purge(bat_priv);
2272
2273         queue_delayed_work(batadv_event_workqueue, &bat_priv->tt.work,
2274                            msecs_to_jiffies(BATADV_TT_WORK_PERIOD));
2275 }
2276
2277 void batadv_tt_free(struct batadv_priv *bat_priv)
2278 {
2279         cancel_delayed_work_sync(&bat_priv->tt.work);
2280
2281         batadv_tt_local_table_free(bat_priv);
2282         batadv_tt_global_table_free(bat_priv);
2283         batadv_tt_req_list_free(bat_priv);
2284         batadv_tt_changes_list_free(bat_priv);
2285         batadv_tt_roam_list_free(bat_priv);
2286
2287         kfree(bat_priv->tt.last_changeset);
2288 }
2289
2290 /* This function will enable or disable the specified flags for all the entries
2291  * in the given hash table and returns the number of modified entries
2292  */
2293 static uint16_t batadv_tt_set_flags(struct batadv_hashtable *hash,
2294                                     uint16_t flags, bool enable)
2295 {
2296         uint32_t i;
2297         uint16_t changed_num = 0;
2298         struct hlist_head *head;
2299         struct batadv_tt_common_entry *tt_common_entry;
2300
2301         if (!hash)
2302                 goto out;
2303
2304         for (i = 0; i < hash->size; i++) {
2305                 head = &hash->table[i];
2306
2307                 rcu_read_lock();
2308                 hlist_for_each_entry_rcu(tt_common_entry,
2309                                          head, hash_entry) {
2310                         if (enable) {
2311                                 if ((tt_common_entry->flags & flags) == flags)
2312                                         continue;
2313                                 tt_common_entry->flags |= flags;
2314                         } else {
2315                                 if (!(tt_common_entry->flags & flags))
2316                                         continue;
2317                                 tt_common_entry->flags &= ~flags;
2318                         }
2319                         changed_num++;
2320                 }
2321                 rcu_read_unlock();
2322         }
2323 out:
2324         return changed_num;
2325 }
2326
2327 /* Purge out all the tt local entries marked with BATADV_TT_CLIENT_PENDING */
2328 static void batadv_tt_local_purge_pending_clients(struct batadv_priv *bat_priv)
2329 {
2330         struct batadv_hashtable *hash = bat_priv->tt.local_hash;
2331         struct batadv_tt_common_entry *tt_common;
2332         struct batadv_tt_local_entry *tt_local;
2333         struct hlist_node *node_tmp;
2334         struct hlist_head *head;
2335         spinlock_t *list_lock; /* protects write access to the hash lists */
2336         uint32_t i;
2337
2338         if (!hash)
2339                 return;
2340
2341         for (i = 0; i < hash->size; i++) {
2342                 head = &hash->table[i];
2343                 list_lock = &hash->list_locks[i];
2344
2345                 spin_lock_bh(list_lock);
2346                 hlist_for_each_entry_safe(tt_common, node_tmp, head,
2347                                           hash_entry) {
2348                         if (!(tt_common->flags & BATADV_TT_CLIENT_PENDING))
2349                                 continue;
2350
2351                         batadv_dbg(BATADV_DBG_TT, bat_priv,
2352                                    "Deleting local tt entry (%pM): pending\n",
2353                                    tt_common->addr);
2354
2355                         atomic_dec(&bat_priv->tt.local_entry_num);
2356                         hlist_del_rcu(&tt_common->hash_entry);
2357                         tt_local = container_of(tt_common,
2358                                                 struct batadv_tt_local_entry,
2359                                                 common);
2360                         batadv_tt_local_entry_free_ref(tt_local);
2361                 }
2362                 spin_unlock_bh(list_lock);
2363         }
2364 }
2365
2366 static int batadv_tt_commit_changes(struct batadv_priv *bat_priv,
2367                                     unsigned char **packet_buff,
2368                                     int *packet_buff_len, int packet_min_len)
2369 {
2370         uint16_t changed_num = 0;
2371
2372         if (atomic_read(&bat_priv->tt.local_changes) < 1)
2373                 return -ENOENT;
2374
2375         changed_num = batadv_tt_set_flags(bat_priv->tt.local_hash,
2376                                           BATADV_TT_CLIENT_NEW, false);
2377
2378         /* all reset entries have to be counted as local entries */
2379         atomic_add(changed_num, &bat_priv->tt.local_entry_num);
2380         batadv_tt_local_purge_pending_clients(bat_priv);
2381         bat_priv->tt.local_crc = batadv_tt_local_crc(bat_priv);
2382
2383         /* Increment the TTVN only once per OGM interval */
2384         atomic_inc(&bat_priv->tt.vn);
2385         batadv_dbg(BATADV_DBG_TT, bat_priv,
2386                    "Local changes committed, updating to ttvn %u\n",
2387                    (uint8_t)atomic_read(&bat_priv->tt.vn));
2388
2389         /* reset the sending counter */
2390         atomic_set(&bat_priv->tt.ogm_append_cnt, BATADV_TT_OGM_APPEND_MAX);
2391
2392         return batadv_tt_changes_fill_buff(bat_priv, packet_buff,
2393                                            packet_buff_len, packet_min_len);
2394 }
2395
2396 /* when calling this function (hard_iface == primary_if) has to be true */
2397 int batadv_tt_append_diff(struct batadv_priv *bat_priv,
2398                           unsigned char **packet_buff, int *packet_buff_len,
2399                           int packet_min_len)
2400 {
2401         int tt_num_changes;
2402
2403         /* if at least one change happened */
2404         tt_num_changes = batadv_tt_commit_changes(bat_priv, packet_buff,
2405                                                   packet_buff_len,
2406                                                   packet_min_len);
2407
2408         /* if the changes have been sent often enough */
2409         if ((tt_num_changes < 0) &&
2410             (!batadv_atomic_dec_not_zero(&bat_priv->tt.ogm_append_cnt))) {
2411                 batadv_tt_realloc_packet_buff(packet_buff, packet_buff_len,
2412                                               packet_min_len, packet_min_len);
2413                 tt_num_changes = 0;
2414         }
2415
2416         return tt_num_changes;
2417 }
2418
2419 bool batadv_is_ap_isolated(struct batadv_priv *bat_priv, uint8_t *src,
2420                            uint8_t *dst)
2421 {
2422         struct batadv_tt_local_entry *tt_local_entry = NULL;
2423         struct batadv_tt_global_entry *tt_global_entry = NULL;
2424         bool ret = false;
2425
2426         if (!atomic_read(&bat_priv->ap_isolation))
2427                 goto out;
2428
2429         tt_local_entry = batadv_tt_local_hash_find(bat_priv, dst);
2430         if (!tt_local_entry)
2431                 goto out;
2432
2433         tt_global_entry = batadv_tt_global_hash_find(bat_priv, src);
2434         if (!tt_global_entry)
2435                 goto out;
2436
2437         if (!_batadv_is_ap_isolated(tt_local_entry, tt_global_entry))
2438                 goto out;
2439
2440         ret = true;
2441
2442 out:
2443         if (tt_global_entry)
2444                 batadv_tt_global_entry_free_ref(tt_global_entry);
2445         if (tt_local_entry)
2446                 batadv_tt_local_entry_free_ref(tt_local_entry);
2447         return ret;
2448 }
2449
2450 void batadv_tt_update_orig(struct batadv_priv *bat_priv,
2451                            struct batadv_orig_node *orig_node,
2452                            const unsigned char *tt_buff, uint8_t tt_num_changes,
2453                            uint8_t ttvn, uint16_t tt_crc)
2454 {
2455         uint8_t orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
2456         bool full_table = true;
2457         struct batadv_tt_change *tt_change;
2458
2459         /* don't care about a backbone gateways updates. */
2460         if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2461                 return;
2462
2463         /* orig table not initialised AND first diff is in the OGM OR the ttvn
2464          * increased by one -> we can apply the attached changes
2465          */
2466         if ((!orig_node->tt_initialised && ttvn == 1) ||
2467             ttvn - orig_ttvn == 1) {
2468                 /* the OGM could not contain the changes due to their size or
2469                  * because they have already been sent BATADV_TT_OGM_APPEND_MAX
2470                  * times.
2471                  * In this case send a tt request
2472                  */
2473                 if (!tt_num_changes) {
2474                         full_table = false;
2475                         goto request_table;
2476                 }
2477
2478                 tt_change = (struct batadv_tt_change *)tt_buff;
2479                 batadv_tt_update_changes(bat_priv, orig_node, tt_num_changes,
2480                                          ttvn, tt_change);
2481
2482                 /* Even if we received the precomputed crc with the OGM, we
2483                  * prefer to recompute it to spot any possible inconsistency
2484                  * in the global table
2485                  */
2486                 orig_node->tt_crc = batadv_tt_global_crc(bat_priv, orig_node);
2487
2488                 /* The ttvn alone is not enough to guarantee consistency
2489                  * because a single value could represent different states
2490                  * (due to the wrap around). Thus a node has to check whether
2491                  * the resulting table (after applying the changes) is still
2492                  * consistent or not. E.g. a node could disconnect while its
2493                  * ttvn is X and reconnect on ttvn = X + TTVN_MAX: in this case
2494                  * checking the CRC value is mandatory to detect the
2495                  * inconsistency
2496                  */
2497                 if (orig_node->tt_crc != tt_crc)
2498                         goto request_table;
2499         } else {
2500                 /* if we missed more than one change or our tables are not
2501                  * in sync anymore -> request fresh tt data
2502                  */
2503                 if (!orig_node->tt_initialised || ttvn != orig_ttvn ||
2504                     orig_node->tt_crc != tt_crc) {
2505 request_table:
2506                         batadv_dbg(BATADV_DBG_TT, bat_priv,
2507                                    "TT inconsistency for %pM. Need to retrieve the correct information (ttvn: %u last_ttvn: %u crc: %#.4x last_crc: %#.4x num_changes: %u)\n",
2508                                    orig_node->orig, ttvn, orig_ttvn, tt_crc,
2509                                    orig_node->tt_crc, tt_num_changes);
2510                         batadv_send_tt_request(bat_priv, orig_node, ttvn,
2511                                                tt_crc, full_table);
2512                         return;
2513                 }
2514         }
2515 }
2516
2517 /* returns true whether we know that the client has moved from its old
2518  * originator to another one. This entry is kept is still kept for consistency
2519  * purposes
2520  */
2521 bool batadv_tt_global_client_is_roaming(struct batadv_priv *bat_priv,
2522                                         uint8_t *addr)
2523 {
2524         struct batadv_tt_global_entry *tt_global_entry;
2525         bool ret = false;
2526
2527         tt_global_entry = batadv_tt_global_hash_find(bat_priv, addr);
2528         if (!tt_global_entry)
2529                 goto out;
2530
2531         ret = !!(tt_global_entry->common.flags & BATADV_TT_CLIENT_ROAM);
2532         batadv_tt_global_entry_free_ref(tt_global_entry);
2533 out:
2534         return ret;
2535 }
2536
2537 /**
2538  * batadv_tt_local_client_is_roaming - tells whether the client is roaming
2539  * @bat_priv: the bat priv with all the soft interface information
2540  * @addr: the MAC address of the local client to query
2541  *
2542  * Returns true if the local client is known to be roaming (it is not served by
2543  * this node anymore) or not. If yes, the client is still present in the table
2544  * to keep the latter consistent with the node TTVN
2545  */
2546 bool batadv_tt_local_client_is_roaming(struct batadv_priv *bat_priv,
2547                                        uint8_t *addr)
2548 {
2549         struct batadv_tt_local_entry *tt_local_entry;
2550         bool ret = false;
2551
2552         tt_local_entry = batadv_tt_local_hash_find(bat_priv, addr);
2553         if (!tt_local_entry)
2554                 goto out;
2555
2556         ret = tt_local_entry->common.flags & BATADV_TT_CLIENT_ROAM;
2557         batadv_tt_local_entry_free_ref(tt_local_entry);
2558 out:
2559         return ret;
2560 }
2561
2562 bool batadv_tt_add_temporary_global_entry(struct batadv_priv *bat_priv,
2563                                           struct batadv_orig_node *orig_node,
2564                                           const unsigned char *addr)
2565 {
2566         bool ret = false;
2567
2568         /* if the originator is a backbone node (meaning it belongs to the same
2569          * LAN of this node) the temporary client must not be added because to
2570          * reach such destination the node must use the LAN instead of the mesh
2571          */
2572         if (batadv_bla_is_backbone_gw_orig(bat_priv, orig_node->orig))
2573                 goto out;
2574
2575         if (!batadv_tt_global_add(bat_priv, orig_node, addr,
2576                                   BATADV_TT_CLIENT_TEMP,
2577                                   atomic_read(&orig_node->last_ttvn)))
2578                 goto out;
2579
2580         batadv_dbg(BATADV_DBG_TT, bat_priv,
2581                    "Added temporary global client (addr: %pM orig: %pM)\n",
2582                    addr, orig_node->orig);
2583         ret = true;
2584 out:
2585         return ret;
2586 }