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