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