]> Pileus Git - ~andy/linux/blob - net/batman-adv/translation-table.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[~andy/linux] / net / batman-adv / translation-table.c
1 /*
2  * Copyright (C) 2007-2011 B.A.T.M.A.N. contributors:
3  *
4  * Marek Lindner, Simon Wunderlich
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of version 2 of the GNU General Public
8  * License as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  *
20  */
21
22 #include "main.h"
23 #include "translation-table.h"
24 #include "soft-interface.h"
25 #include "hard-interface.h"
26 #include "send.h"
27 #include "hash.h"
28 #include "originator.h"
29 #include "routing.h"
30
31 #include <linux/crc16.h>
32
33 static void _tt_global_del(struct bat_priv *bat_priv,
34                            struct tt_global_entry *tt_global_entry,
35                            const char *message);
36 static void tt_purge(struct work_struct *work);
37
38 /* returns 1 if they are the same mac addr */
39 static int compare_ltt(const struct hlist_node *node, const void *data2)
40 {
41         const void *data1 = container_of(node, struct tt_local_entry,
42                                          hash_entry);
43
44         return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
45 }
46
47 /* returns 1 if they are the same mac addr */
48 static int compare_gtt(const struct hlist_node *node, const void *data2)
49 {
50         const void *data1 = container_of(node, struct tt_global_entry,
51                                          hash_entry);
52
53         return (memcmp(data1, data2, ETH_ALEN) == 0 ? 1 : 0);
54 }
55
56 static void tt_start_timer(struct bat_priv *bat_priv)
57 {
58         INIT_DELAYED_WORK(&bat_priv->tt_work, tt_purge);
59         queue_delayed_work(bat_event_workqueue, &bat_priv->tt_work,
60                            msecs_to_jiffies(5000));
61 }
62
63 static struct tt_local_entry *tt_local_hash_find(struct bat_priv *bat_priv,
64                                                  const void *data)
65 {
66         struct hashtable_t *hash = bat_priv->tt_local_hash;
67         struct hlist_head *head;
68         struct hlist_node *node;
69         struct tt_local_entry *tt_local_entry, *tt_local_entry_tmp = NULL;
70         int index;
71
72         if (!hash)
73                 return NULL;
74
75         index = choose_orig(data, hash->size);
76         head = &hash->table[index];
77
78         rcu_read_lock();
79         hlist_for_each_entry_rcu(tt_local_entry, node, head, hash_entry) {
80                 if (!compare_eth(tt_local_entry, data))
81                         continue;
82
83                 if (!atomic_inc_not_zero(&tt_local_entry->refcount))
84                         continue;
85
86                 tt_local_entry_tmp = tt_local_entry;
87                 break;
88         }
89         rcu_read_unlock();
90
91         return tt_local_entry_tmp;
92 }
93
94 static struct tt_global_entry *tt_global_hash_find(struct bat_priv *bat_priv,
95                                                    const void *data)
96 {
97         struct hashtable_t *hash = bat_priv->tt_global_hash;
98         struct hlist_head *head;
99         struct hlist_node *node;
100         struct tt_global_entry *tt_global_entry;
101         struct tt_global_entry *tt_global_entry_tmp = NULL;
102         int index;
103
104         if (!hash)
105                 return NULL;
106
107         index = choose_orig(data, hash->size);
108         head = &hash->table[index];
109
110         rcu_read_lock();
111         hlist_for_each_entry_rcu(tt_global_entry, node, head, hash_entry) {
112                 if (!compare_eth(tt_global_entry, data))
113                         continue;
114
115                 if (!atomic_inc_not_zero(&tt_global_entry->refcount))
116                         continue;
117
118                 tt_global_entry_tmp = tt_global_entry;
119                 break;
120         }
121         rcu_read_unlock();
122
123         return tt_global_entry_tmp;
124 }
125
126 static bool is_out_of_time(unsigned long starting_time, unsigned long timeout)
127 {
128         unsigned long deadline;
129         deadline = starting_time + msecs_to_jiffies(timeout);
130
131         return time_after(jiffies, deadline);
132 }
133
134 static void tt_local_entry_free_ref(struct tt_local_entry *tt_local_entry)
135 {
136         if (atomic_dec_and_test(&tt_local_entry->refcount))
137                 kfree_rcu(tt_local_entry, rcu);
138 }
139
140 static void tt_global_entry_free_ref(struct tt_global_entry *tt_global_entry)
141 {
142         if (atomic_dec_and_test(&tt_global_entry->refcount))
143                 kfree_rcu(tt_global_entry, rcu);
144 }
145
146 static void tt_local_event(struct bat_priv *bat_priv, const uint8_t *addr,
147                            uint8_t flags)
148 {
149         struct tt_change_node *tt_change_node;
150
151         tt_change_node = kmalloc(sizeof(*tt_change_node), GFP_ATOMIC);
152
153         if (!tt_change_node)
154                 return;
155
156         tt_change_node->change.flags = flags;
157         memcpy(tt_change_node->change.addr, addr, ETH_ALEN);
158
159         spin_lock_bh(&bat_priv->tt_changes_list_lock);
160         /* track the change in the OGMinterval list */
161         list_add_tail(&tt_change_node->list, &bat_priv->tt_changes_list);
162         atomic_inc(&bat_priv->tt_local_changes);
163         spin_unlock_bh(&bat_priv->tt_changes_list_lock);
164
165         atomic_set(&bat_priv->tt_ogm_append_cnt, 0);
166 }
167
168 int tt_len(int changes_num)
169 {
170         return changes_num * sizeof(struct tt_change);
171 }
172
173 static int tt_local_init(struct bat_priv *bat_priv)
174 {
175         if (bat_priv->tt_local_hash)
176                 return 1;
177
178         bat_priv->tt_local_hash = hash_new(1024);
179
180         if (!bat_priv->tt_local_hash)
181                 return 0;
182
183         return 1;
184 }
185
186 void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
187 {
188         struct bat_priv *bat_priv = netdev_priv(soft_iface);
189         struct tt_local_entry *tt_local_entry = NULL;
190         struct tt_global_entry *tt_global_entry = NULL;
191
192         tt_local_entry = tt_local_hash_find(bat_priv, addr);
193
194         if (tt_local_entry) {
195                 tt_local_entry->last_seen = jiffies;
196                 goto out;
197         }
198
199         tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
200         if (!tt_local_entry)
201                 goto out;
202
203         bat_dbg(DBG_TT, bat_priv,
204                 "Creating new local tt entry: %pM (ttvn: %d)\n", addr,
205                 (uint8_t)atomic_read(&bat_priv->ttvn));
206
207         memcpy(tt_local_entry->addr, addr, ETH_ALEN);
208         tt_local_entry->last_seen = jiffies;
209         tt_local_entry->flags = NO_FLAGS;
210         atomic_set(&tt_local_entry->refcount, 2);
211
212         /* the batman interface mac address should never be purged */
213         if (compare_eth(addr, soft_iface->dev_addr))
214                 tt_local_entry->flags |= TT_CLIENT_NOPURGE;
215
216         tt_local_event(bat_priv, addr, tt_local_entry->flags);
217
218         hash_add(bat_priv->tt_local_hash, compare_ltt, choose_orig,
219                  tt_local_entry, &tt_local_entry->hash_entry);
220
221         atomic_inc(&bat_priv->num_local_tt);
222
223         /* remove address from global hash if present */
224         tt_global_entry = tt_global_hash_find(bat_priv, addr);
225
226         /* Check whether it is a roaming! */
227         if (tt_global_entry) {
228                 /* This node is probably going to update its tt table */
229                 tt_global_entry->orig_node->tt_poss_change = true;
230                 _tt_global_del(bat_priv, tt_global_entry,
231                                "local tt received");
232                 send_roam_adv(bat_priv, tt_global_entry->addr,
233                               tt_global_entry->orig_node);
234         }
235 out:
236         if (tt_local_entry)
237                 tt_local_entry_free_ref(tt_local_entry);
238         if (tt_global_entry)
239                 tt_global_entry_free_ref(tt_global_entry);
240 }
241
242 int tt_changes_fill_buffer(struct bat_priv *bat_priv,
243                            unsigned char *buff, int buff_len)
244 {
245         int count = 0, tot_changes = 0;
246         struct tt_change_node *entry, *safe;
247
248         if (buff_len > 0)
249                 tot_changes = buff_len / tt_len(1);
250
251         spin_lock_bh(&bat_priv->tt_changes_list_lock);
252         atomic_set(&bat_priv->tt_local_changes, 0);
253
254         list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
255                         list) {
256                 if (count < tot_changes) {
257                         memcpy(buff + tt_len(count),
258                                &entry->change, sizeof(struct tt_change));
259                         count++;
260                 }
261                 list_del(&entry->list);
262                 kfree(entry);
263         }
264         spin_unlock_bh(&bat_priv->tt_changes_list_lock);
265
266         /* Keep the buffer for possible tt_request */
267         spin_lock_bh(&bat_priv->tt_buff_lock);
268         kfree(bat_priv->tt_buff);
269         bat_priv->tt_buff_len = 0;
270         bat_priv->tt_buff = NULL;
271         /* We check whether this new OGM has no changes due to size
272          * problems */
273         if (buff_len > 0) {
274                 /**
275                  * if kmalloc() fails we will reply with the full table
276                  * instead of providing the diff
277                  */
278                 bat_priv->tt_buff = kmalloc(buff_len, GFP_ATOMIC);
279                 if (bat_priv->tt_buff) {
280                         memcpy(bat_priv->tt_buff, buff, buff_len);
281                         bat_priv->tt_buff_len = buff_len;
282                 }
283         }
284         spin_unlock_bh(&bat_priv->tt_buff_lock);
285
286         return tot_changes;
287 }
288
289 int tt_local_seq_print_text(struct seq_file *seq, void *offset)
290 {
291         struct net_device *net_dev = (struct net_device *)seq->private;
292         struct bat_priv *bat_priv = netdev_priv(net_dev);
293         struct hashtable_t *hash = bat_priv->tt_local_hash;
294         struct tt_local_entry *tt_local_entry;
295         struct hard_iface *primary_if;
296         struct hlist_node *node;
297         struct hlist_head *head;
298         size_t buf_size, pos;
299         char *buff;
300         int i, ret = 0;
301
302         primary_if = primary_if_get_selected(bat_priv);
303         if (!primary_if) {
304                 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
305                                  "please specify interfaces to enable it\n",
306                                  net_dev->name);
307                 goto out;
308         }
309
310         if (primary_if->if_status != IF_ACTIVE) {
311                 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
312                                  "primary interface not active\n",
313                                  net_dev->name);
314                 goto out;
315         }
316
317         seq_printf(seq, "Locally retrieved addresses (from %s) "
318                    "announced via TT (TTVN: %u):\n",
319                    net_dev->name, (uint8_t)atomic_read(&bat_priv->ttvn));
320
321         buf_size = 1;
322         /* Estimate length for: " * xx:xx:xx:xx:xx:xx\n" */
323         for (i = 0; i < hash->size; i++) {
324                 head = &hash->table[i];
325
326                 rcu_read_lock();
327                 __hlist_for_each_rcu(node, head)
328                         buf_size += 21;
329                 rcu_read_unlock();
330         }
331
332         buff = kmalloc(buf_size, GFP_ATOMIC);
333         if (!buff) {
334                 ret = -ENOMEM;
335                 goto out;
336         }
337
338         buff[0] = '\0';
339         pos = 0;
340
341         for (i = 0; i < hash->size; i++) {
342                 head = &hash->table[i];
343
344                 rcu_read_lock();
345                 hlist_for_each_entry_rcu(tt_local_entry, node,
346                                          head, hash_entry) {
347                         pos += snprintf(buff + pos, 22, " * %pM\n",
348                                         tt_local_entry->addr);
349                 }
350                 rcu_read_unlock();
351         }
352
353         seq_printf(seq, "%s", buff);
354         kfree(buff);
355 out:
356         if (primary_if)
357                 hardif_free_ref(primary_if);
358         return ret;
359 }
360
361 static void tt_local_del(struct bat_priv *bat_priv,
362                          struct tt_local_entry *tt_local_entry,
363                          const char *message)
364 {
365         bat_dbg(DBG_TT, bat_priv, "Deleting local tt entry (%pM): %s\n",
366                 tt_local_entry->addr, message);
367
368         atomic_dec(&bat_priv->num_local_tt);
369
370         hash_remove(bat_priv->tt_local_hash, compare_ltt, choose_orig,
371                     tt_local_entry->addr);
372
373         tt_local_entry_free_ref(tt_local_entry);
374 }
375
376 void tt_local_remove(struct bat_priv *bat_priv, const uint8_t *addr,
377                      const char *message, bool roaming)
378 {
379         struct tt_local_entry *tt_local_entry = NULL;
380
381         tt_local_entry = tt_local_hash_find(bat_priv, addr);
382
383         if (!tt_local_entry)
384                 goto out;
385
386         tt_local_event(bat_priv, tt_local_entry->addr,
387                        tt_local_entry->flags | TT_CLIENT_DEL |
388                        (roaming ? TT_CLIENT_ROAM : NO_FLAGS));
389         tt_local_del(bat_priv, tt_local_entry, message);
390 out:
391         if (tt_local_entry)
392                 tt_local_entry_free_ref(tt_local_entry);
393 }
394
395 static void tt_local_purge(struct bat_priv *bat_priv)
396 {
397         struct hashtable_t *hash = bat_priv->tt_local_hash;
398         struct tt_local_entry *tt_local_entry;
399         struct hlist_node *node, *node_tmp;
400         struct hlist_head *head;
401         spinlock_t *list_lock; /* protects write access to the hash lists */
402         int i;
403
404         for (i = 0; i < hash->size; i++) {
405                 head = &hash->table[i];
406                 list_lock = &hash->list_locks[i];
407
408                 spin_lock_bh(list_lock);
409                 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
410                                           head, hash_entry) {
411                         if (tt_local_entry->flags & TT_CLIENT_NOPURGE)
412                                 continue;
413
414                         if (!is_out_of_time(tt_local_entry->last_seen,
415                                             TT_LOCAL_TIMEOUT * 1000))
416                                 continue;
417
418                         tt_local_event(bat_priv, tt_local_entry->addr,
419                                        tt_local_entry->flags | TT_CLIENT_DEL);
420                         atomic_dec(&bat_priv->num_local_tt);
421                         bat_dbg(DBG_TT, bat_priv, "Deleting local "
422                                 "tt entry (%pM): timed out\n",
423                                 tt_local_entry->addr);
424                         hlist_del_rcu(node);
425                         tt_local_entry_free_ref(tt_local_entry);
426                 }
427                 spin_unlock_bh(list_lock);
428         }
429
430 }
431
432 static void tt_local_table_free(struct bat_priv *bat_priv)
433 {
434         struct hashtable_t *hash;
435         spinlock_t *list_lock; /* protects write access to the hash lists */
436         struct tt_local_entry *tt_local_entry;
437         struct hlist_node *node, *node_tmp;
438         struct hlist_head *head;
439         int i;
440
441         if (!bat_priv->tt_local_hash)
442                 return;
443
444         hash = bat_priv->tt_local_hash;
445
446         for (i = 0; i < hash->size; i++) {
447                 head = &hash->table[i];
448                 list_lock = &hash->list_locks[i];
449
450                 spin_lock_bh(list_lock);
451                 hlist_for_each_entry_safe(tt_local_entry, node, node_tmp,
452                                           head, hash_entry) {
453                         hlist_del_rcu(node);
454                         tt_local_entry_free_ref(tt_local_entry);
455                 }
456                 spin_unlock_bh(list_lock);
457         }
458
459         hash_destroy(hash);
460
461         bat_priv->tt_local_hash = NULL;
462 }
463
464 static int tt_global_init(struct bat_priv *bat_priv)
465 {
466         if (bat_priv->tt_global_hash)
467                 return 1;
468
469         bat_priv->tt_global_hash = hash_new(1024);
470
471         if (!bat_priv->tt_global_hash)
472                 return 0;
473
474         return 1;
475 }
476
477 static void tt_changes_list_free(struct bat_priv *bat_priv)
478 {
479         struct tt_change_node *entry, *safe;
480
481         spin_lock_bh(&bat_priv->tt_changes_list_lock);
482
483         list_for_each_entry_safe(entry, safe, &bat_priv->tt_changes_list,
484                                  list) {
485                 list_del(&entry->list);
486                 kfree(entry);
487         }
488
489         atomic_set(&bat_priv->tt_local_changes, 0);
490         spin_unlock_bh(&bat_priv->tt_changes_list_lock);
491 }
492
493 /* caller must hold orig_node refcount */
494 int tt_global_add(struct bat_priv *bat_priv, struct orig_node *orig_node,
495                   const unsigned char *tt_addr, uint8_t ttvn, bool roaming)
496 {
497         struct tt_global_entry *tt_global_entry;
498         struct orig_node *orig_node_tmp;
499         int ret = 0;
500
501         tt_global_entry = tt_global_hash_find(bat_priv, tt_addr);
502
503         if (!tt_global_entry) {
504                 tt_global_entry =
505                         kmalloc(sizeof(*tt_global_entry),
506                                 GFP_ATOMIC);
507                 if (!tt_global_entry)
508                         goto out;
509
510                 memcpy(tt_global_entry->addr, tt_addr, ETH_ALEN);
511                 /* Assign the new orig_node */
512                 atomic_inc(&orig_node->refcount);
513                 tt_global_entry->orig_node = orig_node;
514                 tt_global_entry->ttvn = ttvn;
515                 tt_global_entry->flags = NO_FLAGS;
516                 tt_global_entry->roam_at = 0;
517                 atomic_set(&tt_global_entry->refcount, 2);
518
519                 hash_add(bat_priv->tt_global_hash, compare_gtt,
520                          choose_orig, tt_global_entry,
521                          &tt_global_entry->hash_entry);
522                 atomic_inc(&orig_node->tt_size);
523         } else {
524                 if (tt_global_entry->orig_node != orig_node) {
525                         atomic_dec(&tt_global_entry->orig_node->tt_size);
526                         orig_node_tmp = tt_global_entry->orig_node;
527                         atomic_inc(&orig_node->refcount);
528                         tt_global_entry->orig_node = orig_node;
529                         orig_node_free_ref(orig_node_tmp);
530                         atomic_inc(&orig_node->tt_size);
531                 }
532                 tt_global_entry->ttvn = ttvn;
533                 tt_global_entry->flags = NO_FLAGS;
534                 tt_global_entry->roam_at = 0;
535         }
536
537         bat_dbg(DBG_TT, bat_priv,
538                 "Creating new global tt entry: %pM (via %pM)\n",
539                 tt_global_entry->addr, orig_node->orig);
540
541         /* remove address from local hash if present */
542         tt_local_remove(bat_priv, tt_global_entry->addr,
543                         "global tt received", roaming);
544         ret = 1;
545 out:
546         if (tt_global_entry)
547                 tt_global_entry_free_ref(tt_global_entry);
548         return ret;
549 }
550
551 int tt_global_seq_print_text(struct seq_file *seq, void *offset)
552 {
553         struct net_device *net_dev = (struct net_device *)seq->private;
554         struct bat_priv *bat_priv = netdev_priv(net_dev);
555         struct hashtable_t *hash = bat_priv->tt_global_hash;
556         struct tt_global_entry *tt_global_entry;
557         struct hard_iface *primary_if;
558         struct hlist_node *node;
559         struct hlist_head *head;
560         size_t buf_size, pos;
561         char *buff;
562         int i, ret = 0;
563
564         primary_if = primary_if_get_selected(bat_priv);
565         if (!primary_if) {
566                 ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
567                                  "specify interfaces to enable it\n",
568                                  net_dev->name);
569                 goto out;
570         }
571
572         if (primary_if->if_status != IF_ACTIVE) {
573                 ret = seq_printf(seq, "BATMAN mesh %s disabled - "
574                                  "primary interface not active\n",
575                                  net_dev->name);
576                 goto out;
577         }
578
579         seq_printf(seq,
580                    "Globally announced TT entries received via the mesh %s\n",
581                    net_dev->name);
582         seq_printf(seq, "       %-13s %s       %-15s %s\n",
583                    "Client", "(TTVN)", "Originator", "(Curr TTVN)");
584
585         buf_size = 1;
586         /* Estimate length for: " * xx:xx:xx:xx:xx:xx (ttvn) via
587          * xx:xx:xx:xx:xx:xx (cur_ttvn)\n"*/
588         for (i = 0; i < hash->size; i++) {
589                 head = &hash->table[i];
590
591                 rcu_read_lock();
592                 __hlist_for_each_rcu(node, head)
593                         buf_size += 59;
594                 rcu_read_unlock();
595         }
596
597         buff = kmalloc(buf_size, GFP_ATOMIC);
598         if (!buff) {
599                 ret = -ENOMEM;
600                 goto out;
601         }
602
603         buff[0] = '\0';
604         pos = 0;
605
606         for (i = 0; i < hash->size; i++) {
607                 head = &hash->table[i];
608
609                 rcu_read_lock();
610                 hlist_for_each_entry_rcu(tt_global_entry, node,
611                                          head, hash_entry) {
612                         pos += snprintf(buff + pos, 61,
613                                         " * %pM  (%3u) via %pM     (%3u)\n",
614                                         tt_global_entry->addr,
615                                         tt_global_entry->ttvn,
616                                         tt_global_entry->orig_node->orig,
617                                         (uint8_t) atomic_read(
618                                                 &tt_global_entry->orig_node->
619                                                 last_ttvn));
620                 }
621                 rcu_read_unlock();
622         }
623
624         seq_printf(seq, "%s", buff);
625         kfree(buff);
626 out:
627         if (primary_if)
628                 hardif_free_ref(primary_if);
629         return ret;
630 }
631
632 static void _tt_global_del(struct bat_priv *bat_priv,
633                            struct tt_global_entry *tt_global_entry,
634                            const char *message)
635 {
636         if (!tt_global_entry)
637                 goto out;
638
639         bat_dbg(DBG_TT, bat_priv,
640                 "Deleting global tt entry %pM (via %pM): %s\n",
641                 tt_global_entry->addr, tt_global_entry->orig_node->orig,
642                 message);
643
644         atomic_dec(&tt_global_entry->orig_node->tt_size);
645
646         hash_remove(bat_priv->tt_global_hash, compare_gtt, choose_orig,
647                     tt_global_entry->addr);
648 out:
649         if (tt_global_entry)
650                 tt_global_entry_free_ref(tt_global_entry);
651 }
652
653 void tt_global_del(struct bat_priv *bat_priv,
654                    struct orig_node *orig_node, const unsigned char *addr,
655                    const char *message, bool roaming)
656 {
657         struct tt_global_entry *tt_global_entry = NULL;
658
659         tt_global_entry = tt_global_hash_find(bat_priv, addr);
660         if (!tt_global_entry)
661                 goto out;
662
663         if (tt_global_entry->orig_node == orig_node) {
664                 if (roaming) {
665                         tt_global_entry->flags |= TT_CLIENT_ROAM;
666                         tt_global_entry->roam_at = jiffies;
667                         goto out;
668                 }
669                 _tt_global_del(bat_priv, tt_global_entry, message);
670         }
671 out:
672         if (tt_global_entry)
673                 tt_global_entry_free_ref(tt_global_entry);
674 }
675
676 void tt_global_del_orig(struct bat_priv *bat_priv,
677                         struct orig_node *orig_node, const char *message)
678 {
679         struct tt_global_entry *tt_global_entry;
680         int i;
681         struct hashtable_t *hash = bat_priv->tt_global_hash;
682         struct hlist_node *node, *safe;
683         struct hlist_head *head;
684         spinlock_t *list_lock; /* protects write access to the hash lists */
685
686         for (i = 0; i < hash->size; i++) {
687                 head = &hash->table[i];
688                 list_lock = &hash->list_locks[i];
689
690                 spin_lock_bh(list_lock);
691                 hlist_for_each_entry_safe(tt_global_entry, node, safe,
692                                          head, hash_entry) {
693                         if (tt_global_entry->orig_node == orig_node) {
694                                 bat_dbg(DBG_TT, bat_priv,
695                                         "Deleting global tt entry %pM "
696                                         "(via %pM): originator time out\n",
697                                         tt_global_entry->addr,
698                                         tt_global_entry->orig_node->orig);
699                                 hlist_del_rcu(node);
700                                 tt_global_entry_free_ref(tt_global_entry);
701                         }
702                 }
703                 spin_unlock_bh(list_lock);
704         }
705         atomic_set(&orig_node->tt_size, 0);
706 }
707
708 static void tt_global_roam_purge(struct bat_priv *bat_priv)
709 {
710         struct hashtable_t *hash = bat_priv->tt_global_hash;
711         struct tt_global_entry *tt_global_entry;
712         struct hlist_node *node, *node_tmp;
713         struct hlist_head *head;
714         spinlock_t *list_lock; /* protects write access to the hash lists */
715         int i;
716
717         for (i = 0; i < hash->size; i++) {
718                 head = &hash->table[i];
719                 list_lock = &hash->list_locks[i];
720
721                 spin_lock_bh(list_lock);
722                 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
723                                           head, hash_entry) {
724                         if (!(tt_global_entry->flags & TT_CLIENT_ROAM))
725                                 continue;
726                         if (!is_out_of_time(tt_global_entry->roam_at,
727                                             TT_CLIENT_ROAM_TIMEOUT * 1000))
728                                 continue;
729
730                         bat_dbg(DBG_TT, bat_priv, "Deleting global "
731                                 "tt entry (%pM): Roaming timeout\n",
732                                 tt_global_entry->addr);
733                         atomic_dec(&tt_global_entry->orig_node->tt_size);
734                         hlist_del_rcu(node);
735                         tt_global_entry_free_ref(tt_global_entry);
736                 }
737                 spin_unlock_bh(list_lock);
738         }
739
740 }
741
742 static void tt_global_table_free(struct bat_priv *bat_priv)
743 {
744         struct hashtable_t *hash;
745         spinlock_t *list_lock; /* protects write access to the hash lists */
746         struct tt_global_entry *tt_global_entry;
747         struct hlist_node *node, *node_tmp;
748         struct hlist_head *head;
749         int i;
750
751         if (!bat_priv->tt_global_hash)
752                 return;
753
754         hash = bat_priv->tt_global_hash;
755
756         for (i = 0; i < hash->size; i++) {
757                 head = &hash->table[i];
758                 list_lock = &hash->list_locks[i];
759
760                 spin_lock_bh(list_lock);
761                 hlist_for_each_entry_safe(tt_global_entry, node, node_tmp,
762                                           head, hash_entry) {
763                         hlist_del_rcu(node);
764                         tt_global_entry_free_ref(tt_global_entry);
765                 }
766                 spin_unlock_bh(list_lock);
767         }
768
769         hash_destroy(hash);
770
771         bat_priv->tt_global_hash = NULL;
772 }
773
774 struct orig_node *transtable_search(struct bat_priv *bat_priv,
775                                     const uint8_t *addr)
776 {
777         struct tt_global_entry *tt_global_entry;
778         struct orig_node *orig_node = NULL;
779
780         tt_global_entry = tt_global_hash_find(bat_priv, addr);
781
782         if (!tt_global_entry)
783                 goto out;
784
785         if (!atomic_inc_not_zero(&tt_global_entry->orig_node->refcount))
786                 goto free_tt;
787
788         orig_node = tt_global_entry->orig_node;
789
790 free_tt:
791         tt_global_entry_free_ref(tt_global_entry);
792 out:
793         return orig_node;
794 }
795
796 /* Calculates the checksum of the local table of a given orig_node */
797 uint16_t tt_global_crc(struct bat_priv *bat_priv, struct orig_node *orig_node)
798 {
799         uint16_t total = 0, total_one;
800         struct hashtable_t *hash = bat_priv->tt_global_hash;
801         struct tt_global_entry *tt_global_entry;
802         struct hlist_node *node;
803         struct hlist_head *head;
804         int i, j;
805
806         for (i = 0; i < hash->size; i++) {
807                 head = &hash->table[i];
808
809                 rcu_read_lock();
810                 hlist_for_each_entry_rcu(tt_global_entry, node,
811                                          head, hash_entry) {
812                         if (compare_eth(tt_global_entry->orig_node,
813                                         orig_node)) {
814                                 /* Roaming clients are in the global table for
815                                  * consistency only. They don't have to be
816                                  * taken into account while computing the
817                                  * global crc */
818                                 if (tt_global_entry->flags & TT_CLIENT_ROAM)
819                                         continue;
820                                 total_one = 0;
821                                 for (j = 0; j < ETH_ALEN; j++)
822                                         total_one = crc16_byte(total_one,
823                                                 tt_global_entry->addr[j]);
824                                 total ^= total_one;
825                         }
826                 }
827                 rcu_read_unlock();
828         }
829
830         return total;
831 }
832
833 /* Calculates the checksum of the local table */
834 uint16_t tt_local_crc(struct bat_priv *bat_priv)
835 {
836         uint16_t total = 0, total_one;
837         struct hashtable_t *hash = bat_priv->tt_local_hash;
838         struct tt_local_entry *tt_local_entry;
839         struct hlist_node *node;
840         struct hlist_head *head;
841         int i, j;
842
843         for (i = 0; i < hash->size; i++) {
844                 head = &hash->table[i];
845
846                 rcu_read_lock();
847                 hlist_for_each_entry_rcu(tt_local_entry, node,
848                                          head, hash_entry) {
849                         total_one = 0;
850                         for (j = 0; j < ETH_ALEN; j++)
851                                 total_one = crc16_byte(total_one,
852                                                    tt_local_entry->addr[j]);
853                         total ^= total_one;
854                 }
855                 rcu_read_unlock();
856         }
857
858         return total;
859 }
860
861 static void tt_req_list_free(struct bat_priv *bat_priv)
862 {
863         struct tt_req_node *node, *safe;
864
865         spin_lock_bh(&bat_priv->tt_req_list_lock);
866
867         list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
868                 list_del(&node->list);
869                 kfree(node);
870         }
871
872         spin_unlock_bh(&bat_priv->tt_req_list_lock);
873 }
874
875 void tt_save_orig_buffer(struct bat_priv *bat_priv, struct orig_node *orig_node,
876                          const unsigned char *tt_buff, uint8_t tt_num_changes)
877 {
878         uint16_t tt_buff_len = tt_len(tt_num_changes);
879
880         /* Replace the old buffer only if I received something in the
881          * last OGM (the OGM could carry no changes) */
882         spin_lock_bh(&orig_node->tt_buff_lock);
883         if (tt_buff_len > 0) {
884                 kfree(orig_node->tt_buff);
885                 orig_node->tt_buff_len = 0;
886                 orig_node->tt_buff = kmalloc(tt_buff_len, GFP_ATOMIC);
887                 if (orig_node->tt_buff) {
888                         memcpy(orig_node->tt_buff, tt_buff, tt_buff_len);
889                         orig_node->tt_buff_len = tt_buff_len;
890                 }
891         }
892         spin_unlock_bh(&orig_node->tt_buff_lock);
893 }
894
895 static void tt_req_purge(struct bat_priv *bat_priv)
896 {
897         struct tt_req_node *node, *safe;
898
899         spin_lock_bh(&bat_priv->tt_req_list_lock);
900         list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
901                 if (is_out_of_time(node->issued_at,
902                     TT_REQUEST_TIMEOUT * 1000)) {
903                         list_del(&node->list);
904                         kfree(node);
905                 }
906         }
907         spin_unlock_bh(&bat_priv->tt_req_list_lock);
908 }
909
910 /* returns the pointer to the new tt_req_node struct if no request
911  * has already been issued for this orig_node, NULL otherwise */
912 static struct tt_req_node *new_tt_req_node(struct bat_priv *bat_priv,
913                                           struct orig_node *orig_node)
914 {
915         struct tt_req_node *tt_req_node_tmp, *tt_req_node = NULL;
916
917         spin_lock_bh(&bat_priv->tt_req_list_lock);
918         list_for_each_entry(tt_req_node_tmp, &bat_priv->tt_req_list, list) {
919                 if (compare_eth(tt_req_node_tmp, orig_node) &&
920                     !is_out_of_time(tt_req_node_tmp->issued_at,
921                                     TT_REQUEST_TIMEOUT * 1000))
922                         goto unlock;
923         }
924
925         tt_req_node = kmalloc(sizeof(*tt_req_node), GFP_ATOMIC);
926         if (!tt_req_node)
927                 goto unlock;
928
929         memcpy(tt_req_node->addr, orig_node->orig, ETH_ALEN);
930         tt_req_node->issued_at = jiffies;
931
932         list_add(&tt_req_node->list, &bat_priv->tt_req_list);
933 unlock:
934         spin_unlock_bh(&bat_priv->tt_req_list_lock);
935         return tt_req_node;
936 }
937
938 static int tt_global_valid_entry(const void *entry_ptr, const void *data_ptr)
939 {
940         const struct tt_global_entry *tt_global_entry = entry_ptr;
941         const struct orig_node *orig_node = data_ptr;
942
943         if (tt_global_entry->flags & TT_CLIENT_ROAM)
944                 return 0;
945
946         return (tt_global_entry->orig_node == orig_node);
947 }
948
949 static struct sk_buff *tt_response_fill_table(uint16_t tt_len, uint8_t ttvn,
950                                               struct hashtable_t *hash,
951                                               struct hard_iface *primary_if,
952                                               int (*valid_cb)(const void *,
953                                                               const void *),
954                                               void *cb_data)
955 {
956         struct tt_local_entry *tt_local_entry;
957         struct tt_query_packet *tt_response;
958         struct tt_change *tt_change;
959         struct hlist_node *node;
960         struct hlist_head *head;
961         struct sk_buff *skb = NULL;
962         uint16_t tt_tot, tt_count;
963         ssize_t tt_query_size = sizeof(struct tt_query_packet);
964         int i;
965
966         if (tt_query_size + tt_len > primary_if->soft_iface->mtu) {
967                 tt_len = primary_if->soft_iface->mtu - tt_query_size;
968                 tt_len -= tt_len % sizeof(struct tt_change);
969         }
970         tt_tot = tt_len / sizeof(struct tt_change);
971
972         skb = dev_alloc_skb(tt_query_size + tt_len + ETH_HLEN);
973         if (!skb)
974                 goto out;
975
976         skb_reserve(skb, ETH_HLEN);
977         tt_response = (struct tt_query_packet *)skb_put(skb,
978                                                      tt_query_size + tt_len);
979         tt_response->ttvn = ttvn;
980         tt_response->tt_data = htons(tt_tot);
981
982         tt_change = (struct tt_change *)(skb->data + tt_query_size);
983         tt_count = 0;
984
985         rcu_read_lock();
986         for (i = 0; i < hash->size; i++) {
987                 head = &hash->table[i];
988
989                 hlist_for_each_entry_rcu(tt_local_entry, node,
990                                          head, hash_entry) {
991                         if (tt_count == tt_tot)
992                                 break;
993
994                         if ((valid_cb) && (!valid_cb(tt_local_entry, cb_data)))
995                                 continue;
996
997                         memcpy(tt_change->addr, tt_local_entry->addr, ETH_ALEN);
998                         tt_change->flags = NO_FLAGS;
999
1000                         tt_count++;
1001                         tt_change++;
1002                 }
1003         }
1004         rcu_read_unlock();
1005
1006 out:
1007         return skb;
1008 }
1009
1010 int send_tt_request(struct bat_priv *bat_priv, struct orig_node *dst_orig_node,
1011                     uint8_t ttvn, uint16_t tt_crc, bool full_table)
1012 {
1013         struct sk_buff *skb = NULL;
1014         struct tt_query_packet *tt_request;
1015         struct neigh_node *neigh_node = NULL;
1016         struct hard_iface *primary_if;
1017         struct tt_req_node *tt_req_node = NULL;
1018         int ret = 1;
1019
1020         primary_if = primary_if_get_selected(bat_priv);
1021         if (!primary_if)
1022                 goto out;
1023
1024         /* The new tt_req will be issued only if I'm not waiting for a
1025          * reply from the same orig_node yet */
1026         tt_req_node = new_tt_req_node(bat_priv, dst_orig_node);
1027         if (!tt_req_node)
1028                 goto out;
1029
1030         skb = dev_alloc_skb(sizeof(struct tt_query_packet) + ETH_HLEN);
1031         if (!skb)
1032                 goto out;
1033
1034         skb_reserve(skb, ETH_HLEN);
1035
1036         tt_request = (struct tt_query_packet *)skb_put(skb,
1037                                 sizeof(struct tt_query_packet));
1038
1039         tt_request->packet_type = BAT_TT_QUERY;
1040         tt_request->version = COMPAT_VERSION;
1041         memcpy(tt_request->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1042         memcpy(tt_request->dst, dst_orig_node->orig, ETH_ALEN);
1043         tt_request->ttl = TTL;
1044         tt_request->ttvn = ttvn;
1045         tt_request->tt_data = tt_crc;
1046         tt_request->flags = TT_REQUEST;
1047
1048         if (full_table)
1049                 tt_request->flags |= TT_FULL_TABLE;
1050
1051         neigh_node = orig_node_get_router(dst_orig_node);
1052         if (!neigh_node)
1053                 goto out;
1054
1055         bat_dbg(DBG_TT, bat_priv, "Sending TT_REQUEST to %pM via %pM "
1056                 "[%c]\n", dst_orig_node->orig, neigh_node->addr,
1057                 (full_table ? 'F' : '.'));
1058
1059         send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1060         ret = 0;
1061
1062 out:
1063         if (neigh_node)
1064                 neigh_node_free_ref(neigh_node);
1065         if (primary_if)
1066                 hardif_free_ref(primary_if);
1067         if (ret)
1068                 kfree_skb(skb);
1069         if (ret && tt_req_node) {
1070                 spin_lock_bh(&bat_priv->tt_req_list_lock);
1071                 list_del(&tt_req_node->list);
1072                 spin_unlock_bh(&bat_priv->tt_req_list_lock);
1073                 kfree(tt_req_node);
1074         }
1075         return ret;
1076 }
1077
1078 static bool send_other_tt_response(struct bat_priv *bat_priv,
1079                                    struct tt_query_packet *tt_request)
1080 {
1081         struct orig_node *req_dst_orig_node = NULL, *res_dst_orig_node = NULL;
1082         struct neigh_node *neigh_node = NULL;
1083         struct hard_iface *primary_if = NULL;
1084         uint8_t orig_ttvn, req_ttvn, ttvn;
1085         int ret = false;
1086         unsigned char *tt_buff;
1087         bool full_table;
1088         uint16_t tt_len, tt_tot;
1089         struct sk_buff *skb = NULL;
1090         struct tt_query_packet *tt_response;
1091
1092         bat_dbg(DBG_TT, bat_priv,
1093                 "Received TT_REQUEST from %pM for "
1094                 "ttvn: %u (%pM) [%c]\n", tt_request->src,
1095                 tt_request->ttvn, tt_request->dst,
1096                 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1097
1098         /* Let's get the orig node of the REAL destination */
1099         req_dst_orig_node = get_orig_node(bat_priv, tt_request->dst);
1100         if (!req_dst_orig_node)
1101                 goto out;
1102
1103         res_dst_orig_node = get_orig_node(bat_priv, tt_request->src);
1104         if (!res_dst_orig_node)
1105                 goto out;
1106
1107         neigh_node = orig_node_get_router(res_dst_orig_node);
1108         if (!neigh_node)
1109                 goto out;
1110
1111         primary_if = primary_if_get_selected(bat_priv);
1112         if (!primary_if)
1113                 goto out;
1114
1115         orig_ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1116         req_ttvn = tt_request->ttvn;
1117
1118         /* I have not the requested data */
1119         if (orig_ttvn != req_ttvn ||
1120             tt_request->tt_data != req_dst_orig_node->tt_crc)
1121                 goto out;
1122
1123         /* If it has explicitly been requested the full table */
1124         if (tt_request->flags & TT_FULL_TABLE ||
1125             !req_dst_orig_node->tt_buff)
1126                 full_table = true;
1127         else
1128                 full_table = false;
1129
1130         /* In this version, fragmentation is not implemented, then
1131          * I'll send only one packet with as much TT entries as I can */
1132         if (!full_table) {
1133                 spin_lock_bh(&req_dst_orig_node->tt_buff_lock);
1134                 tt_len = req_dst_orig_node->tt_buff_len;
1135                 tt_tot = tt_len / sizeof(struct tt_change);
1136
1137                 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1138                                     tt_len + ETH_HLEN);
1139                 if (!skb)
1140                         goto unlock;
1141
1142                 skb_reserve(skb, ETH_HLEN);
1143                 tt_response = (struct tt_query_packet *)skb_put(skb,
1144                                 sizeof(struct tt_query_packet) + tt_len);
1145                 tt_response->ttvn = req_ttvn;
1146                 tt_response->tt_data = htons(tt_tot);
1147
1148                 tt_buff = skb->data + sizeof(struct tt_query_packet);
1149                 /* Copy the last orig_node's OGM buffer */
1150                 memcpy(tt_buff, req_dst_orig_node->tt_buff,
1151                        req_dst_orig_node->tt_buff_len);
1152
1153                 spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1154         } else {
1155                 tt_len = (uint16_t)atomic_read(&req_dst_orig_node->tt_size) *
1156                                                 sizeof(struct tt_change);
1157                 ttvn = (uint8_t)atomic_read(&req_dst_orig_node->last_ttvn);
1158
1159                 skb = tt_response_fill_table(tt_len, ttvn,
1160                                              bat_priv->tt_global_hash,
1161                                              primary_if, tt_global_valid_entry,
1162                                              req_dst_orig_node);
1163                 if (!skb)
1164                         goto out;
1165
1166                 tt_response = (struct tt_query_packet *)skb->data;
1167         }
1168
1169         tt_response->packet_type = BAT_TT_QUERY;
1170         tt_response->version = COMPAT_VERSION;
1171         tt_response->ttl = TTL;
1172         memcpy(tt_response->src, req_dst_orig_node->orig, ETH_ALEN);
1173         memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1174         tt_response->flags = TT_RESPONSE;
1175
1176         if (full_table)
1177                 tt_response->flags |= TT_FULL_TABLE;
1178
1179         bat_dbg(DBG_TT, bat_priv,
1180                 "Sending TT_RESPONSE %pM via %pM for %pM (ttvn: %u)\n",
1181                 res_dst_orig_node->orig, neigh_node->addr,
1182                 req_dst_orig_node->orig, req_ttvn);
1183
1184         send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1185         ret = true;
1186         goto out;
1187
1188 unlock:
1189         spin_unlock_bh(&req_dst_orig_node->tt_buff_lock);
1190
1191 out:
1192         if (res_dst_orig_node)
1193                 orig_node_free_ref(res_dst_orig_node);
1194         if (req_dst_orig_node)
1195                 orig_node_free_ref(req_dst_orig_node);
1196         if (neigh_node)
1197                 neigh_node_free_ref(neigh_node);
1198         if (primary_if)
1199                 hardif_free_ref(primary_if);
1200         if (!ret)
1201                 kfree_skb(skb);
1202         return ret;
1203
1204 }
1205 static bool send_my_tt_response(struct bat_priv *bat_priv,
1206                                 struct tt_query_packet *tt_request)
1207 {
1208         struct orig_node *orig_node = NULL;
1209         struct neigh_node *neigh_node = NULL;
1210         struct hard_iface *primary_if = NULL;
1211         uint8_t my_ttvn, req_ttvn, ttvn;
1212         int ret = false;
1213         unsigned char *tt_buff;
1214         bool full_table;
1215         uint16_t tt_len, tt_tot;
1216         struct sk_buff *skb = NULL;
1217         struct tt_query_packet *tt_response;
1218
1219         bat_dbg(DBG_TT, bat_priv,
1220                 "Received TT_REQUEST from %pM for "
1221                 "ttvn: %u (me) [%c]\n", tt_request->src,
1222                 tt_request->ttvn,
1223                 (tt_request->flags & TT_FULL_TABLE ? 'F' : '.'));
1224
1225
1226         my_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1227         req_ttvn = tt_request->ttvn;
1228
1229         orig_node = get_orig_node(bat_priv, tt_request->src);
1230         if (!orig_node)
1231                 goto out;
1232
1233         neigh_node = orig_node_get_router(orig_node);
1234         if (!neigh_node)
1235                 goto out;
1236
1237         primary_if = primary_if_get_selected(bat_priv);
1238         if (!primary_if)
1239                 goto out;
1240
1241         /* If the full table has been explicitly requested or the gap
1242          * is too big send the whole local translation table */
1243         if (tt_request->flags & TT_FULL_TABLE || my_ttvn != req_ttvn ||
1244             !bat_priv->tt_buff)
1245                 full_table = true;
1246         else
1247                 full_table = false;
1248
1249         /* In this version, fragmentation is not implemented, then
1250          * I'll send only one packet with as much TT entries as I can */
1251         if (!full_table) {
1252                 spin_lock_bh(&bat_priv->tt_buff_lock);
1253                 tt_len = bat_priv->tt_buff_len;
1254                 tt_tot = tt_len / sizeof(struct tt_change);
1255
1256                 skb = dev_alloc_skb(sizeof(struct tt_query_packet) +
1257                                     tt_len + ETH_HLEN);
1258                 if (!skb)
1259                         goto unlock;
1260
1261                 skb_reserve(skb, ETH_HLEN);
1262                 tt_response = (struct tt_query_packet *)skb_put(skb,
1263                                 sizeof(struct tt_query_packet) + tt_len);
1264                 tt_response->ttvn = req_ttvn;
1265                 tt_response->tt_data = htons(tt_tot);
1266
1267                 tt_buff = skb->data + sizeof(struct tt_query_packet);
1268                 memcpy(tt_buff, bat_priv->tt_buff,
1269                        bat_priv->tt_buff_len);
1270                 spin_unlock_bh(&bat_priv->tt_buff_lock);
1271         } else {
1272                 tt_len = (uint16_t)atomic_read(&bat_priv->num_local_tt) *
1273                                                 sizeof(struct tt_change);
1274                 ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
1275
1276                 skb = tt_response_fill_table(tt_len, ttvn,
1277                                              bat_priv->tt_local_hash,
1278                                              primary_if, NULL, NULL);
1279                 if (!skb)
1280                         goto out;
1281
1282                 tt_response = (struct tt_query_packet *)skb->data;
1283         }
1284
1285         tt_response->packet_type = BAT_TT_QUERY;
1286         tt_response->version = COMPAT_VERSION;
1287         tt_response->ttl = TTL;
1288         memcpy(tt_response->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1289         memcpy(tt_response->dst, tt_request->src, ETH_ALEN);
1290         tt_response->flags = TT_RESPONSE;
1291
1292         if (full_table)
1293                 tt_response->flags |= TT_FULL_TABLE;
1294
1295         bat_dbg(DBG_TT, bat_priv,
1296                 "Sending TT_RESPONSE to %pM via %pM [%c]\n",
1297                 orig_node->orig, neigh_node->addr,
1298                 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1299
1300         send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1301         ret = true;
1302         goto out;
1303
1304 unlock:
1305         spin_unlock_bh(&bat_priv->tt_buff_lock);
1306 out:
1307         if (orig_node)
1308                 orig_node_free_ref(orig_node);
1309         if (neigh_node)
1310                 neigh_node_free_ref(neigh_node);
1311         if (primary_if)
1312                 hardif_free_ref(primary_if);
1313         if (!ret)
1314                 kfree_skb(skb);
1315         /* This packet was for me, so it doesn't need to be re-routed */
1316         return true;
1317 }
1318
1319 bool send_tt_response(struct bat_priv *bat_priv,
1320                       struct tt_query_packet *tt_request)
1321 {
1322         if (is_my_mac(tt_request->dst))
1323                 return send_my_tt_response(bat_priv, tt_request);
1324         else
1325                 return send_other_tt_response(bat_priv, tt_request);
1326 }
1327
1328 static void _tt_update_changes(struct bat_priv *bat_priv,
1329                                struct orig_node *orig_node,
1330                                struct tt_change *tt_change,
1331                                uint16_t tt_num_changes, uint8_t ttvn)
1332 {
1333         int i;
1334
1335         for (i = 0; i < tt_num_changes; i++) {
1336                 if ((tt_change + i)->flags & TT_CLIENT_DEL)
1337                         tt_global_del(bat_priv, orig_node,
1338                                       (tt_change + i)->addr,
1339                                       "tt removed by changes",
1340                                       (tt_change + i)->flags & TT_CLIENT_ROAM);
1341                 else
1342                         if (!tt_global_add(bat_priv, orig_node,
1343                                            (tt_change + i)->addr, ttvn, false))
1344                                 /* In case of problem while storing a
1345                                  * global_entry, we stop the updating
1346                                  * procedure without committing the
1347                                  * ttvn change. This will avoid to send
1348                                  * corrupted data on tt_request
1349                                  */
1350                                 return;
1351         }
1352 }
1353
1354 static void tt_fill_gtable(struct bat_priv *bat_priv,
1355                            struct tt_query_packet *tt_response)
1356 {
1357         struct orig_node *orig_node = NULL;
1358
1359         orig_node = orig_hash_find(bat_priv, tt_response->src);
1360         if (!orig_node)
1361                 goto out;
1362
1363         /* Purge the old table first.. */
1364         tt_global_del_orig(bat_priv, orig_node, "Received full table");
1365
1366         _tt_update_changes(bat_priv, orig_node,
1367                            (struct tt_change *)(tt_response + 1),
1368                            tt_response->tt_data, tt_response->ttvn);
1369
1370         spin_lock_bh(&orig_node->tt_buff_lock);
1371         kfree(orig_node->tt_buff);
1372         orig_node->tt_buff_len = 0;
1373         orig_node->tt_buff = NULL;
1374         spin_unlock_bh(&orig_node->tt_buff_lock);
1375
1376         atomic_set(&orig_node->last_ttvn, tt_response->ttvn);
1377
1378 out:
1379         if (orig_node)
1380                 orig_node_free_ref(orig_node);
1381 }
1382
1383 void tt_update_changes(struct bat_priv *bat_priv, struct orig_node *orig_node,
1384                        uint16_t tt_num_changes, uint8_t ttvn,
1385                        struct tt_change *tt_change)
1386 {
1387         _tt_update_changes(bat_priv, orig_node, tt_change, tt_num_changes,
1388                            ttvn);
1389
1390         tt_save_orig_buffer(bat_priv, orig_node, (unsigned char *)tt_change,
1391                             tt_num_changes);
1392         atomic_set(&orig_node->last_ttvn, ttvn);
1393 }
1394
1395 bool is_my_client(struct bat_priv *bat_priv, const uint8_t *addr)
1396 {
1397         struct tt_local_entry *tt_local_entry = NULL;
1398         bool ret = false;
1399
1400         tt_local_entry = tt_local_hash_find(bat_priv, addr);
1401         if (!tt_local_entry)
1402                 goto out;
1403         ret = true;
1404 out:
1405         if (tt_local_entry)
1406                 tt_local_entry_free_ref(tt_local_entry);
1407         return ret;
1408 }
1409
1410 void handle_tt_response(struct bat_priv *bat_priv,
1411                         struct tt_query_packet *tt_response)
1412 {
1413         struct tt_req_node *node, *safe;
1414         struct orig_node *orig_node = NULL;
1415
1416         bat_dbg(DBG_TT, bat_priv, "Received TT_RESPONSE from %pM for "
1417                 "ttvn %d t_size: %d [%c]\n",
1418                 tt_response->src, tt_response->ttvn,
1419                 tt_response->tt_data,
1420                 (tt_response->flags & TT_FULL_TABLE ? 'F' : '.'));
1421
1422         orig_node = orig_hash_find(bat_priv, tt_response->src);
1423         if (!orig_node)
1424                 goto out;
1425
1426         if (tt_response->flags & TT_FULL_TABLE)
1427                 tt_fill_gtable(bat_priv, tt_response);
1428         else
1429                 tt_update_changes(bat_priv, orig_node, tt_response->tt_data,
1430                                   tt_response->ttvn,
1431                                   (struct tt_change *)(tt_response + 1));
1432
1433         /* Delete the tt_req_node from pending tt_requests list */
1434         spin_lock_bh(&bat_priv->tt_req_list_lock);
1435         list_for_each_entry_safe(node, safe, &bat_priv->tt_req_list, list) {
1436                 if (!compare_eth(node->addr, tt_response->src))
1437                         continue;
1438                 list_del(&node->list);
1439                 kfree(node);
1440         }
1441         spin_unlock_bh(&bat_priv->tt_req_list_lock);
1442
1443         /* Recalculate the CRC for this orig_node and store it */
1444         orig_node->tt_crc = tt_global_crc(bat_priv, orig_node);
1445         /* Roaming phase is over: tables are in sync again. I can
1446          * unset the flag */
1447         orig_node->tt_poss_change = false;
1448 out:
1449         if (orig_node)
1450                 orig_node_free_ref(orig_node);
1451 }
1452
1453 int tt_init(struct bat_priv *bat_priv)
1454 {
1455         if (!tt_local_init(bat_priv))
1456                 return 0;
1457
1458         if (!tt_global_init(bat_priv))
1459                 return 0;
1460
1461         tt_start_timer(bat_priv);
1462
1463         return 1;
1464 }
1465
1466 static void tt_roam_list_free(struct bat_priv *bat_priv)
1467 {
1468         struct tt_roam_node *node, *safe;
1469
1470         spin_lock_bh(&bat_priv->tt_roam_list_lock);
1471
1472         list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1473                 list_del(&node->list);
1474                 kfree(node);
1475         }
1476
1477         spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1478 }
1479
1480 static void tt_roam_purge(struct bat_priv *bat_priv)
1481 {
1482         struct tt_roam_node *node, *safe;
1483
1484         spin_lock_bh(&bat_priv->tt_roam_list_lock);
1485         list_for_each_entry_safe(node, safe, &bat_priv->tt_roam_list, list) {
1486                 if (!is_out_of_time(node->first_time,
1487                                     ROAMING_MAX_TIME * 1000))
1488                         continue;
1489
1490                 list_del(&node->list);
1491                 kfree(node);
1492         }
1493         spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1494 }
1495
1496 /* This function checks whether the client already reached the
1497  * maximum number of possible roaming phases. In this case the ROAMING_ADV
1498  * will not be sent.
1499  *
1500  * returns true if the ROAMING_ADV can be sent, false otherwise */
1501 static bool tt_check_roam_count(struct bat_priv *bat_priv,
1502                                 uint8_t *client)
1503 {
1504         struct tt_roam_node *tt_roam_node;
1505         bool ret = false;
1506
1507         spin_lock_bh(&bat_priv->tt_roam_list_lock);
1508         /* The new tt_req will be issued only if I'm not waiting for a
1509          * reply from the same orig_node yet */
1510         list_for_each_entry(tt_roam_node, &bat_priv->tt_roam_list, list) {
1511                 if (!compare_eth(tt_roam_node->addr, client))
1512                         continue;
1513
1514                 if (is_out_of_time(tt_roam_node->first_time,
1515                                    ROAMING_MAX_TIME * 1000))
1516                         continue;
1517
1518                 if (!atomic_dec_not_zero(&tt_roam_node->counter))
1519                         /* Sorry, you roamed too many times! */
1520                         goto unlock;
1521                 ret = true;
1522                 break;
1523         }
1524
1525         if (!ret) {
1526                 tt_roam_node = kmalloc(sizeof(*tt_roam_node), GFP_ATOMIC);
1527                 if (!tt_roam_node)
1528                         goto unlock;
1529
1530                 tt_roam_node->first_time = jiffies;
1531                 atomic_set(&tt_roam_node->counter, ROAMING_MAX_COUNT - 1);
1532                 memcpy(tt_roam_node->addr, client, ETH_ALEN);
1533
1534                 list_add(&tt_roam_node->list, &bat_priv->tt_roam_list);
1535                 ret = true;
1536         }
1537
1538 unlock:
1539         spin_unlock_bh(&bat_priv->tt_roam_list_lock);
1540         return ret;
1541 }
1542
1543 void send_roam_adv(struct bat_priv *bat_priv, uint8_t *client,
1544                    struct orig_node *orig_node)
1545 {
1546         struct neigh_node *neigh_node = NULL;
1547         struct sk_buff *skb = NULL;
1548         struct roam_adv_packet *roam_adv_packet;
1549         int ret = 1;
1550         struct hard_iface *primary_if;
1551
1552         /* before going on we have to check whether the client has
1553          * already roamed to us too many times */
1554         if (!tt_check_roam_count(bat_priv, client))
1555                 goto out;
1556
1557         skb = dev_alloc_skb(sizeof(struct roam_adv_packet) + ETH_HLEN);
1558         if (!skb)
1559                 goto out;
1560
1561         skb_reserve(skb, ETH_HLEN);
1562
1563         roam_adv_packet = (struct roam_adv_packet *)skb_put(skb,
1564                                         sizeof(struct roam_adv_packet));
1565
1566         roam_adv_packet->packet_type = BAT_ROAM_ADV;
1567         roam_adv_packet->version = COMPAT_VERSION;
1568         roam_adv_packet->ttl = TTL;
1569         primary_if = primary_if_get_selected(bat_priv);
1570         if (!primary_if)
1571                 goto out;
1572         memcpy(roam_adv_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
1573         hardif_free_ref(primary_if);
1574         memcpy(roam_adv_packet->dst, orig_node->orig, ETH_ALEN);
1575         memcpy(roam_adv_packet->client, client, ETH_ALEN);
1576
1577         neigh_node = orig_node_get_router(orig_node);
1578         if (!neigh_node)
1579                 goto out;
1580
1581         bat_dbg(DBG_TT, bat_priv,
1582                 "Sending ROAMING_ADV to %pM (client %pM) via %pM\n",
1583                 orig_node->orig, client, neigh_node->addr);
1584
1585         send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
1586         ret = 0;
1587
1588 out:
1589         if (neigh_node)
1590                 neigh_node_free_ref(neigh_node);
1591         if (ret)
1592                 kfree_skb(skb);
1593         return;
1594 }
1595
1596 static void tt_purge(struct work_struct *work)
1597 {
1598         struct delayed_work *delayed_work =
1599                 container_of(work, struct delayed_work, work);
1600         struct bat_priv *bat_priv =
1601                 container_of(delayed_work, struct bat_priv, tt_work);
1602
1603         tt_local_purge(bat_priv);
1604         tt_global_roam_purge(bat_priv);
1605         tt_req_purge(bat_priv);
1606         tt_roam_purge(bat_priv);
1607
1608         tt_start_timer(bat_priv);
1609 }
1610
1611 void tt_free(struct bat_priv *bat_priv)
1612 {
1613         cancel_delayed_work_sync(&bat_priv->tt_work);
1614
1615         tt_local_table_free(bat_priv);
1616         tt_global_table_free(bat_priv);
1617         tt_req_list_free(bat_priv);
1618         tt_changes_list_free(bat_priv);
1619         tt_roam_list_free(bat_priv);
1620
1621         kfree(bat_priv->tt_buff);
1622 }