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