batman-adv: separate ethernet comparing calls from hash functions
[firefly-linux-kernel-4.4.55.git] / net / batman-adv / vis.c
1 /*
2  * Copyright (C) 2008-2011 B.A.T.M.A.N. contributors:
3  *
4  * 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 "send.h"
24 #include "translation-table.h"
25 #include "vis.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "hash.h"
29 #include "originator.h"
30
31 #define MAX_VIS_PACKET_SIZE 1000
32
33 /* Returns the smallest signed integer in two's complement with the sizeof x */
34 #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
35
36 /* Checks if a sequence number x is a predecessor/successor of y.
37  * they handle overflows/underflows and can correctly check for a
38  * predecessor/successor unless the variable sequence number has grown by
39  * more then 2**(bitwidth(x)-1)-1.
40  * This means that for a uint8_t with the maximum value 255, it would think:
41  *  - when adding nothing - it is neither a predecessor nor a successor
42  *  - before adding more than 127 to the starting value - it is a predecessor,
43  *  - when adding 128 - it is neither a predecessor nor a successor,
44  *  - after adding more than 127 to the starting value - it is a successor */
45 #define seq_before(x, y) ({typeof(x) _dummy = (x - y); \
46                         _dummy > smallest_signed_int(_dummy); })
47 #define seq_after(x, y) seq_before(y, x)
48
49 static void start_vis_timer(struct bat_priv *bat_priv);
50
51 /* free the info */
52 static void free_info(struct kref *ref)
53 {
54         struct vis_info *info = container_of(ref, struct vis_info, refcount);
55         struct bat_priv *bat_priv = info->bat_priv;
56         struct recvlist_node *entry, *tmp;
57
58         list_del_init(&info->send_list);
59         spin_lock_bh(&bat_priv->vis_list_lock);
60         list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
61                 list_del(&entry->list);
62                 kfree(entry);
63         }
64
65         spin_unlock_bh(&bat_priv->vis_list_lock);
66         kfree_skb(info->skb_packet);
67 }
68
69 /* Compare two vis packets, used by the hashing algorithm */
70 static int vis_info_cmp(void *data1, void *data2)
71 {
72         struct vis_info *d1, *d2;
73         struct vis_packet *p1, *p2;
74         d1 = data1;
75         d2 = data2;
76         p1 = (struct vis_packet *)d1->skb_packet->data;
77         p2 = (struct vis_packet *)d2->skb_packet->data;
78         return compare_eth(p1->vis_orig, p2->vis_orig);
79 }
80
81 /* hash function to choose an entry in a hash table of given size */
82 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
83 static int vis_info_choose(void *data, int size)
84 {
85         struct vis_info *vis_info = data;
86         struct vis_packet *packet;
87         unsigned char *key;
88         uint32_t hash = 0;
89         size_t i;
90
91         packet = (struct vis_packet *)vis_info->skb_packet->data;
92         key = packet->vis_orig;
93         for (i = 0; i < ETH_ALEN; i++) {
94                 hash += key[i];
95                 hash += (hash << 10);
96                 hash ^= (hash >> 6);
97         }
98
99         hash += (hash << 3);
100         hash ^= (hash >> 11);
101         hash += (hash << 15);
102
103         return hash % size;
104 }
105
106 /* insert interface to the list of interfaces of one originator, if it
107  * does not already exist in the list */
108 static void vis_data_insert_interface(const uint8_t *interface,
109                                       struct hlist_head *if_list,
110                                       bool primary)
111 {
112         struct if_list_entry *entry;
113         struct hlist_node *pos;
114
115         hlist_for_each_entry(entry, pos, if_list, list) {
116                 if (compare_eth(entry->addr, (void *)interface))
117                         return;
118         }
119
120         /* its a new address, add it to the list */
121         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
122         if (!entry)
123                 return;
124         memcpy(entry->addr, interface, ETH_ALEN);
125         entry->primary = primary;
126         hlist_add_head(&entry->list, if_list);
127 }
128
129 static ssize_t vis_data_read_prim_sec(char *buff, struct hlist_head *if_list)
130 {
131         struct if_list_entry *entry;
132         struct hlist_node *pos;
133         size_t len = 0;
134
135         hlist_for_each_entry(entry, pos, if_list, list) {
136                 if (entry->primary)
137                         len += sprintf(buff + len, "PRIMARY, ");
138                 else
139                         len += sprintf(buff + len,  "SEC %pM, ", entry->addr);
140         }
141
142         return len;
143 }
144
145 static size_t vis_data_count_prim_sec(struct hlist_head *if_list)
146 {
147         struct if_list_entry *entry;
148         struct hlist_node *pos;
149         size_t count = 0;
150
151         hlist_for_each_entry(entry, pos, if_list, list) {
152                 if (entry->primary)
153                         count += 9;
154                 else
155                         count += 23;
156         }
157
158         return count;
159 }
160
161 /* read an entry  */
162 static ssize_t vis_data_read_entry(char *buff, struct vis_info_entry *entry,
163                                    uint8_t *src, bool primary)
164 {
165         /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
166         if (primary && entry->quality == 0)
167                 return sprintf(buff, "HNA %pM, ", entry->dest);
168         else if (compare_eth(entry->src, src))
169                 return sprintf(buff, "TQ %pM %d, ", entry->dest,
170                                entry->quality);
171
172         return 0;
173 }
174
175 int vis_seq_print_text(struct seq_file *seq, void *offset)
176 {
177         struct hlist_node *walk;
178         struct hlist_head *head;
179         struct element_t *bucket;
180         struct vis_info *info;
181         struct vis_packet *packet;
182         struct vis_info_entry *entries;
183         struct net_device *net_dev = (struct net_device *)seq->private;
184         struct bat_priv *bat_priv = netdev_priv(net_dev);
185         struct hashtable_t *hash = bat_priv->vis_hash;
186         HLIST_HEAD(vis_if_list);
187         struct if_list_entry *entry;
188         struct hlist_node *pos, *n;
189         int i, j;
190         int vis_server = atomic_read(&bat_priv->vis_mode);
191         size_t buff_pos, buf_size;
192         char *buff;
193         int compare;
194
195         if ((!bat_priv->primary_if) ||
196             (vis_server == VIS_TYPE_CLIENT_UPDATE))
197                 return 0;
198
199         buf_size = 1;
200         /* Estimate length */
201         spin_lock_bh(&bat_priv->vis_hash_lock);
202         for (i = 0; i < hash->size; i++) {
203                 head = &hash->table[i];
204
205                 hlist_for_each_entry(bucket, walk, head, hlist) {
206                         info = bucket->data;
207                         packet = (struct vis_packet *)info->skb_packet->data;
208                         entries = (struct vis_info_entry *)
209                                 ((char *)packet + sizeof(struct vis_packet));
210
211                         for (j = 0; j < packet->entries; j++) {
212                                 if (entries[j].quality == 0)
213                                         continue;
214                                 compare =
215                                  compare_eth(entries[j].src, packet->vis_orig);
216                                 vis_data_insert_interface(entries[j].src,
217                                                           &vis_if_list,
218                                                           compare);
219                         }
220
221                         hlist_for_each_entry(entry, pos, &vis_if_list, list) {
222                                 buf_size += 18 + 26 * packet->entries;
223
224                                 /* add primary/secondary records */
225                                 if (compare_eth(entry->addr, packet->vis_orig))
226                                         buf_size +=
227                                           vis_data_count_prim_sec(&vis_if_list);
228
229                                 buf_size += 1;
230                         }
231
232                         hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
233                                                   list) {
234                                 hlist_del(&entry->list);
235                                 kfree(entry);
236                         }
237                 }
238         }
239
240         buff = kmalloc(buf_size, GFP_ATOMIC);
241         if (!buff) {
242                 spin_unlock_bh(&bat_priv->vis_hash_lock);
243                 return -ENOMEM;
244         }
245         buff[0] = '\0';
246         buff_pos = 0;
247
248         for (i = 0; i < hash->size; i++) {
249                 head = &hash->table[i];
250
251                 hlist_for_each_entry(bucket, walk, head, hlist) {
252                         info = bucket->data;
253                         packet = (struct vis_packet *)info->skb_packet->data;
254                         entries = (struct vis_info_entry *)
255                                 ((char *)packet + sizeof(struct vis_packet));
256
257                         for (j = 0; j < packet->entries; j++) {
258                                 if (entries[j].quality == 0)
259                                         continue;
260                                 compare =
261                                  compare_eth(entries[j].src, packet->vis_orig);
262                                 vis_data_insert_interface(entries[j].src,
263                                                           &vis_if_list,
264                                                           compare);
265                         }
266
267                         hlist_for_each_entry(entry, pos, &vis_if_list, list) {
268                                 buff_pos += sprintf(buff + buff_pos, "%pM,",
269                                                 entry->addr);
270
271                                 for (i = 0; i < packet->entries; i++)
272                                         buff_pos += vis_data_read_entry(
273                                                         buff + buff_pos,
274                                                         &entries[i],
275                                                         entry->addr,
276                                                         entry->primary);
277
278                                 /* add primary/secondary records */
279                                 if (compare_eth(entry->addr, packet->vis_orig))
280                                         buff_pos +=
281                                          vis_data_read_prim_sec(buff + buff_pos,
282                                                                 &vis_if_list);
283
284                                 buff_pos += sprintf(buff + buff_pos, "\n");
285                         }
286
287                         hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
288                                                   list) {
289                                 hlist_del(&entry->list);
290                                 kfree(entry);
291                         }
292                 }
293         }
294
295         spin_unlock_bh(&bat_priv->vis_hash_lock);
296
297         seq_printf(seq, "%s", buff);
298         kfree(buff);
299
300         return 0;
301 }
302
303 /* add the info packet to the send list, if it was not
304  * already linked in. */
305 static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info)
306 {
307         if (list_empty(&info->send_list)) {
308                 kref_get(&info->refcount);
309                 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
310         }
311 }
312
313 /* delete the info packet from the send list, if it was
314  * linked in. */
315 static void send_list_del(struct vis_info *info)
316 {
317         if (!list_empty(&info->send_list)) {
318                 list_del_init(&info->send_list);
319                 kref_put(&info->refcount, free_info);
320         }
321 }
322
323 /* tries to add one entry to the receive list. */
324 static void recv_list_add(struct bat_priv *bat_priv,
325                           struct list_head *recv_list, char *mac)
326 {
327         struct recvlist_node *entry;
328
329         entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC);
330         if (!entry)
331                 return;
332
333         memcpy(entry->mac, mac, ETH_ALEN);
334         spin_lock_bh(&bat_priv->vis_list_lock);
335         list_add_tail(&entry->list, recv_list);
336         spin_unlock_bh(&bat_priv->vis_list_lock);
337 }
338
339 /* returns 1 if this mac is in the recv_list */
340 static int recv_list_is_in(struct bat_priv *bat_priv,
341                            struct list_head *recv_list, char *mac)
342 {
343         struct recvlist_node *entry;
344
345         spin_lock_bh(&bat_priv->vis_list_lock);
346         list_for_each_entry(entry, recv_list, list) {
347                 if (compare_eth(entry->mac, mac)) {
348                         spin_unlock_bh(&bat_priv->vis_list_lock);
349                         return 1;
350                 }
351         }
352         spin_unlock_bh(&bat_priv->vis_list_lock);
353         return 0;
354 }
355
356 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
357  * broken.. ).  vis hash must be locked outside.  is_new is set when the packet
358  * is newer than old entries in the hash. */
359 static struct vis_info *add_packet(struct bat_priv *bat_priv,
360                                    struct vis_packet *vis_packet,
361                                    int vis_info_len, int *is_new,
362                                    int make_broadcast)
363 {
364         struct vis_info *info, *old_info;
365         struct vis_packet *search_packet, *old_packet;
366         struct vis_info search_elem;
367         struct vis_packet *packet;
368         int hash_added;
369
370         *is_new = 0;
371         /* sanity check */
372         if (!bat_priv->vis_hash)
373                 return NULL;
374
375         /* see if the packet is already in vis_hash */
376         search_elem.skb_packet = dev_alloc_skb(sizeof(struct vis_packet));
377         if (!search_elem.skb_packet)
378                 return NULL;
379         search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
380                                                      sizeof(struct vis_packet));
381
382         memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
383         rcu_read_lock();
384         old_info = hash_find(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
385                              &search_elem);
386         rcu_read_unlock();
387         kfree_skb(search_elem.skb_packet);
388
389         if (old_info) {
390                 old_packet = (struct vis_packet *)old_info->skb_packet->data;
391                 if (!seq_after(ntohl(vis_packet->seqno),
392                                ntohl(old_packet->seqno))) {
393                         if (old_packet->seqno == vis_packet->seqno) {
394                                 recv_list_add(bat_priv, &old_info->recv_list,
395                                               vis_packet->sender_orig);
396                                 return old_info;
397                         } else {
398                                 /* newer packet is already in hash. */
399                                 return NULL;
400                         }
401                 }
402                 /* remove old entry */
403                 hash_remove(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
404                             old_info);
405                 send_list_del(old_info);
406                 kref_put(&old_info->refcount, free_info);
407         }
408
409         info = kmalloc(sizeof(struct vis_info), GFP_ATOMIC);
410         if (!info)
411                 return NULL;
412
413         info->skb_packet = dev_alloc_skb(sizeof(struct vis_packet) +
414                                          vis_info_len + sizeof(struct ethhdr));
415         if (!info->skb_packet) {
416                 kfree(info);
417                 return NULL;
418         }
419         skb_reserve(info->skb_packet, sizeof(struct ethhdr));
420         packet = (struct vis_packet *)skb_put(info->skb_packet,
421                                               sizeof(struct vis_packet) +
422                                               vis_info_len);
423
424         kref_init(&info->refcount);
425         INIT_LIST_HEAD(&info->send_list);
426         INIT_LIST_HEAD(&info->recv_list);
427         info->first_seen = jiffies;
428         info->bat_priv = bat_priv;
429         memcpy(packet, vis_packet, sizeof(struct vis_packet) + vis_info_len);
430
431         /* initialize and add new packet. */
432         *is_new = 1;
433
434         /* Make it a broadcast packet, if required */
435         if (make_broadcast)
436                 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
437
438         /* repair if entries is longer than packet. */
439         if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
440                 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
441
442         recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
443
444         /* try to add it */
445         hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
446                               info);
447         if (hash_added < 0) {
448                 /* did not work (for some reason) */
449                 kref_put(&old_info->refcount, free_info);
450                 info = NULL;
451         }
452
453         return info;
454 }
455
456 /* handle the server sync packet, forward if needed. */
457 void receive_server_sync_packet(struct bat_priv *bat_priv,
458                                 struct vis_packet *vis_packet,
459                                 int vis_info_len)
460 {
461         struct vis_info *info;
462         int is_new, make_broadcast;
463         int vis_server = atomic_read(&bat_priv->vis_mode);
464
465         make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
466
467         spin_lock_bh(&bat_priv->vis_hash_lock);
468         info = add_packet(bat_priv, vis_packet, vis_info_len,
469                           &is_new, make_broadcast);
470         if (!info)
471                 goto end;
472
473         /* only if we are server ourselves and packet is newer than the one in
474          * hash.*/
475         if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
476                 send_list_add(bat_priv, info);
477 end:
478         spin_unlock_bh(&bat_priv->vis_hash_lock);
479 }
480
481 /* handle an incoming client update packet and schedule forward if needed. */
482 void receive_client_update_packet(struct bat_priv *bat_priv,
483                                   struct vis_packet *vis_packet,
484                                   int vis_info_len)
485 {
486         struct vis_info *info;
487         struct vis_packet *packet;
488         int is_new;
489         int vis_server = atomic_read(&bat_priv->vis_mode);
490         int are_target = 0;
491
492         /* clients shall not broadcast. */
493         if (is_broadcast_ether_addr(vis_packet->target_orig))
494                 return;
495
496         /* Are we the target for this VIS packet? */
497         if (vis_server == VIS_TYPE_SERVER_SYNC  &&
498             is_my_mac(vis_packet->target_orig))
499                 are_target = 1;
500
501         spin_lock_bh(&bat_priv->vis_hash_lock);
502         info = add_packet(bat_priv, vis_packet, vis_info_len,
503                           &is_new, are_target);
504
505         if (!info)
506                 goto end;
507         /* note that outdated packets will be dropped at this point. */
508
509         packet = (struct vis_packet *)info->skb_packet->data;
510
511         /* send only if we're the target server or ... */
512         if (are_target && is_new) {
513                 packet->vis_type = VIS_TYPE_SERVER_SYNC;        /* upgrade! */
514                 send_list_add(bat_priv, info);
515
516                 /* ... we're not the recipient (and thus need to forward). */
517         } else if (!is_my_mac(packet->target_orig)) {
518                 send_list_add(bat_priv, info);
519         }
520
521 end:
522         spin_unlock_bh(&bat_priv->vis_hash_lock);
523 }
524
525 /* Walk the originators and find the VIS server with the best tq. Set the packet
526  * address to its address and return the best_tq.
527  *
528  * Must be called with the originator hash locked */
529 static int find_best_vis_server(struct bat_priv *bat_priv,
530                                 struct vis_info *info)
531 {
532         struct hashtable_t *hash = bat_priv->orig_hash;
533         struct hlist_node *walk;
534         struct hlist_head *head;
535         struct element_t *bucket;
536         struct orig_node *orig_node;
537         struct vis_packet *packet;
538         int best_tq = -1, i;
539
540         packet = (struct vis_packet *)info->skb_packet->data;
541
542         for (i = 0; i < hash->size; i++) {
543                 head = &hash->table[i];
544
545                 rcu_read_lock();
546                 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
547                         orig_node = bucket->data;
548                         if ((orig_node) && (orig_node->router) &&
549                         (orig_node->flags & VIS_SERVER) &&
550                         (orig_node->router->tq_avg > best_tq)) {
551                                 best_tq = orig_node->router->tq_avg;
552                                 memcpy(packet->target_orig, orig_node->orig,
553                                        ETH_ALEN);
554                         }
555                 }
556                 rcu_read_unlock();
557         }
558
559         return best_tq;
560 }
561
562 /* Return true if the vis packet is full. */
563 static bool vis_packet_full(struct vis_info *info)
564 {
565         struct vis_packet *packet;
566         packet = (struct vis_packet *)info->skb_packet->data;
567
568         if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
569                 < packet->entries + 1)
570                 return true;
571         return false;
572 }
573
574 /* generates a packet of own vis data,
575  * returns 0 on success, -1 if no packet could be generated */
576 static int generate_vis_packet(struct bat_priv *bat_priv)
577 {
578         struct hashtable_t *hash = bat_priv->orig_hash;
579         struct hlist_node *walk;
580         struct hlist_head *head;
581         struct element_t *bucket;
582         struct orig_node *orig_node;
583         struct neigh_node *neigh_node;
584         struct vis_info *info = (struct vis_info *)bat_priv->my_vis_info;
585         struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
586         struct vis_info_entry *entry;
587         struct hna_local_entry *hna_local_entry;
588         int best_tq = -1, i;
589
590         info->first_seen = jiffies;
591         packet->vis_type = atomic_read(&bat_priv->vis_mode);
592
593         spin_lock_bh(&bat_priv->orig_hash_lock);
594         memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
595         packet->ttl = TTL;
596         packet->seqno = htonl(ntohl(packet->seqno) + 1);
597         packet->entries = 0;
598         skb_trim(info->skb_packet, sizeof(struct vis_packet));
599
600         if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
601                 best_tq = find_best_vis_server(bat_priv, info);
602
603                 if (best_tq < 0) {
604                         spin_unlock_bh(&bat_priv->orig_hash_lock);
605                         return -1;
606                 }
607         }
608
609         for (i = 0; i < hash->size; i++) {
610                 head = &hash->table[i];
611
612                 rcu_read_lock();
613                 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
614                         orig_node = bucket->data;
615                         neigh_node = orig_node->router;
616
617                         if (!neigh_node)
618                                 continue;
619
620                         if (!compare_eth(neigh_node->addr, orig_node->orig))
621                                 continue;
622
623                         if (neigh_node->if_incoming->if_status != IF_ACTIVE)
624                                 continue;
625
626                         if (neigh_node->tq_avg < 1)
627                                 continue;
628
629                         /* fill one entry into buffer. */
630                         entry = (struct vis_info_entry *)
631                                       skb_put(info->skb_packet, sizeof(*entry));
632                         memcpy(entry->src,
633                                neigh_node->if_incoming->net_dev->dev_addr,
634                                ETH_ALEN);
635                         memcpy(entry->dest, orig_node->orig, ETH_ALEN);
636                         entry->quality = neigh_node->tq_avg;
637                         packet->entries++;
638
639                         if (vis_packet_full(info)) {
640                                 rcu_read_unlock();
641                                 spin_unlock_bh(&bat_priv->orig_hash_lock);
642                                 return 0;
643                         }
644                 }
645                 rcu_read_unlock();
646         }
647
648         spin_unlock_bh(&bat_priv->orig_hash_lock);
649
650         hash = bat_priv->hna_local_hash;
651
652         spin_lock_bh(&bat_priv->hna_lhash_lock);
653         for (i = 0; i < hash->size; i++) {
654                 head = &hash->table[i];
655
656                 hlist_for_each_entry(bucket, walk, head, hlist) {
657                         hna_local_entry = bucket->data;
658                         entry = (struct vis_info_entry *)
659                                         skb_put(info->skb_packet,
660                                                 sizeof(*entry));
661                         memset(entry->src, 0, ETH_ALEN);
662                         memcpy(entry->dest, hna_local_entry->addr, ETH_ALEN);
663                         entry->quality = 0; /* 0 means HNA */
664                         packet->entries++;
665
666                         if (vis_packet_full(info)) {
667                                 spin_unlock_bh(&bat_priv->hna_lhash_lock);
668                                 return 0;
669                         }
670                 }
671         }
672
673         spin_unlock_bh(&bat_priv->hna_lhash_lock);
674         return 0;
675 }
676
677 /* free old vis packets. Must be called with this vis_hash_lock
678  * held */
679 static void purge_vis_packets(struct bat_priv *bat_priv)
680 {
681         int i;
682         struct hashtable_t *hash = bat_priv->vis_hash;
683         struct hlist_node *walk, *safe;
684         struct hlist_head *head;
685         struct element_t *bucket;
686         struct vis_info *info;
687
688         for (i = 0; i < hash->size; i++) {
689                 head = &hash->table[i];
690
691                 hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
692                         info = bucket->data;
693
694                         /* never purge own data. */
695                         if (info == bat_priv->my_vis_info)
696                                 continue;
697
698                         if (time_after(jiffies,
699                                        info->first_seen + VIS_TIMEOUT * HZ)) {
700                                 hlist_del(walk);
701                                 kfree(bucket);
702                                 send_list_del(info);
703                                 kref_put(&info->refcount, free_info);
704                         }
705                 }
706         }
707 }
708
709 static void broadcast_vis_packet(struct bat_priv *bat_priv,
710                                  struct vis_info *info)
711 {
712         struct hashtable_t *hash = bat_priv->orig_hash;
713         struct hlist_node *walk;
714         struct hlist_head *head;
715         struct element_t *bucket;
716         struct orig_node *orig_node;
717         struct vis_packet *packet;
718         struct sk_buff *skb;
719         struct batman_if *batman_if;
720         uint8_t dstaddr[ETH_ALEN];
721         int i;
722
723
724         spin_lock_bh(&bat_priv->orig_hash_lock);
725         packet = (struct vis_packet *)info->skb_packet->data;
726
727         /* send to all routers in range. */
728         for (i = 0; i < hash->size; i++) {
729                 head = &hash->table[i];
730
731                 rcu_read_lock();
732                 hlist_for_each_entry_rcu(bucket, walk, head, hlist) {
733                         orig_node = bucket->data;
734
735                         /* if it's a vis server and reachable, send it. */
736                         if ((!orig_node) || (!orig_node->router))
737                                 continue;
738                         if (!(orig_node->flags & VIS_SERVER))
739                                 continue;
740                         /* don't send it if we already received the packet from
741                         * this node. */
742                         if (recv_list_is_in(bat_priv, &info->recv_list,
743                                             orig_node->orig))
744                                 continue;
745
746                         memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
747                         batman_if = orig_node->router->if_incoming;
748                         memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
749                         spin_unlock_bh(&bat_priv->orig_hash_lock);
750
751                         skb = skb_clone(info->skb_packet, GFP_ATOMIC);
752                         if (skb)
753                                 send_skb_packet(skb, batman_if, dstaddr);
754
755                         spin_lock_bh(&bat_priv->orig_hash_lock);
756                 }
757                 rcu_read_unlock();
758         }
759
760         spin_unlock_bh(&bat_priv->orig_hash_lock);
761 }
762
763 static void unicast_vis_packet(struct bat_priv *bat_priv,
764                                struct vis_info *info)
765 {
766         struct orig_node *orig_node;
767         struct neigh_node *neigh_node = NULL;
768         struct sk_buff *skb;
769         struct vis_packet *packet;
770         struct batman_if *batman_if;
771         uint8_t dstaddr[ETH_ALEN];
772
773         packet = (struct vis_packet *)info->skb_packet->data;
774
775         spin_lock_bh(&bat_priv->orig_hash_lock);
776         rcu_read_lock();
777         orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
778                                                    compare_orig, choose_orig,
779                                                    packet->target_orig));
780
781         if (!orig_node)
782                 goto unlock;
783
784         kref_get(&orig_node->refcount);
785         neigh_node = orig_node->router;
786
787         if (!neigh_node)
788                 goto unlock;
789
790         if (!atomic_inc_not_zero(&neigh_node->refcount)) {
791                 neigh_node = NULL;
792                 goto unlock;
793         }
794
795         rcu_read_unlock();
796
797         /* don't lock while sending the packets ... we therefore
798          * copy the required data before sending */
799         batman_if = orig_node->router->if_incoming;
800         memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
801         spin_unlock_bh(&bat_priv->orig_hash_lock);
802
803         skb = skb_clone(info->skb_packet, GFP_ATOMIC);
804         if (skb)
805                 send_skb_packet(skb, batman_if, dstaddr);
806
807         goto out;
808
809 unlock:
810         rcu_read_unlock();
811         spin_unlock_bh(&bat_priv->orig_hash_lock);
812 out:
813         if (neigh_node)
814                 neigh_node_free_ref(neigh_node);
815         if (orig_node)
816                 kref_put(&orig_node->refcount, orig_node_free_ref);
817         return;
818 }
819
820 /* only send one vis packet. called from send_vis_packets() */
821 static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
822 {
823         struct vis_packet *packet;
824
825         packet = (struct vis_packet *)info->skb_packet->data;
826         if (packet->ttl < 2) {
827                 pr_debug("Error - can't send vis packet: ttl exceeded\n");
828                 return;
829         }
830
831         memcpy(packet->sender_orig, bat_priv->primary_if->net_dev->dev_addr,
832                ETH_ALEN);
833         packet->ttl--;
834
835         if (is_broadcast_ether_addr(packet->target_orig))
836                 broadcast_vis_packet(bat_priv, info);
837         else
838                 unicast_vis_packet(bat_priv, info);
839         packet->ttl++; /* restore TTL */
840 }
841
842 /* called from timer; send (and maybe generate) vis packet. */
843 static void send_vis_packets(struct work_struct *work)
844 {
845         struct delayed_work *delayed_work =
846                 container_of(work, struct delayed_work, work);
847         struct bat_priv *bat_priv =
848                 container_of(delayed_work, struct bat_priv, vis_work);
849         struct vis_info *info, *temp;
850
851         spin_lock_bh(&bat_priv->vis_hash_lock);
852         purge_vis_packets(bat_priv);
853
854         if (generate_vis_packet(bat_priv) == 0) {
855                 /* schedule if generation was successful */
856                 send_list_add(bat_priv, bat_priv->my_vis_info);
857         }
858
859         list_for_each_entry_safe(info, temp, &bat_priv->vis_send_list,
860                                  send_list) {
861
862                 kref_get(&info->refcount);
863                 spin_unlock_bh(&bat_priv->vis_hash_lock);
864
865                 if (bat_priv->primary_if)
866                         send_vis_packet(bat_priv, info);
867
868                 spin_lock_bh(&bat_priv->vis_hash_lock);
869                 send_list_del(info);
870                 kref_put(&info->refcount, free_info);
871         }
872         spin_unlock_bh(&bat_priv->vis_hash_lock);
873         start_vis_timer(bat_priv);
874 }
875
876 /* init the vis server. this may only be called when if_list is already
877  * initialized (e.g. bat0 is initialized, interfaces have been added) */
878 int vis_init(struct bat_priv *bat_priv)
879 {
880         struct vis_packet *packet;
881         int hash_added;
882
883         if (bat_priv->vis_hash)
884                 return 1;
885
886         spin_lock_bh(&bat_priv->vis_hash_lock);
887
888         bat_priv->vis_hash = hash_new(256);
889         if (!bat_priv->vis_hash) {
890                 pr_err("Can't initialize vis_hash\n");
891                 goto err;
892         }
893
894         bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
895         if (!bat_priv->my_vis_info) {
896                 pr_err("Can't initialize vis packet\n");
897                 goto err;
898         }
899
900         bat_priv->my_vis_info->skb_packet = dev_alloc_skb(
901                                                 sizeof(struct vis_packet) +
902                                                 MAX_VIS_PACKET_SIZE +
903                                                 sizeof(struct ethhdr));
904         if (!bat_priv->my_vis_info->skb_packet)
905                 goto free_info;
906
907         skb_reserve(bat_priv->my_vis_info->skb_packet, sizeof(struct ethhdr));
908         packet = (struct vis_packet *)skb_put(
909                                         bat_priv->my_vis_info->skb_packet,
910                                         sizeof(struct vis_packet));
911
912         /* prefill the vis info */
913         bat_priv->my_vis_info->first_seen = jiffies -
914                                                 msecs_to_jiffies(VIS_INTERVAL);
915         INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
916         INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
917         kref_init(&bat_priv->my_vis_info->refcount);
918         bat_priv->my_vis_info->bat_priv = bat_priv;
919         packet->version = COMPAT_VERSION;
920         packet->packet_type = BAT_VIS;
921         packet->ttl = TTL;
922         packet->seqno = 0;
923         packet->entries = 0;
924
925         INIT_LIST_HEAD(&bat_priv->vis_send_list);
926
927         hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
928                               bat_priv->my_vis_info);
929         if (hash_added < 0) {
930                 pr_err("Can't add own vis packet into hash\n");
931                 /* not in hash, need to remove it manually. */
932                 kref_put(&bat_priv->my_vis_info->refcount, free_info);
933                 goto err;
934         }
935
936         spin_unlock_bh(&bat_priv->vis_hash_lock);
937         start_vis_timer(bat_priv);
938         return 1;
939
940 free_info:
941         kfree(bat_priv->my_vis_info);
942         bat_priv->my_vis_info = NULL;
943 err:
944         spin_unlock_bh(&bat_priv->vis_hash_lock);
945         vis_quit(bat_priv);
946         return 0;
947 }
948
949 /* Decrease the reference count on a hash item info */
950 static void free_info_ref(void *data, void *arg)
951 {
952         struct vis_info *info = data;
953
954         send_list_del(info);
955         kref_put(&info->refcount, free_info);
956 }
957
958 /* shutdown vis-server */
959 void vis_quit(struct bat_priv *bat_priv)
960 {
961         if (!bat_priv->vis_hash)
962                 return;
963
964         cancel_delayed_work_sync(&bat_priv->vis_work);
965
966         spin_lock_bh(&bat_priv->vis_hash_lock);
967         /* properly remove, kill timers ... */
968         hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
969         bat_priv->vis_hash = NULL;
970         bat_priv->my_vis_info = NULL;
971         spin_unlock_bh(&bat_priv->vis_hash_lock);
972 }
973
974 /* schedule packets for (re)transmission */
975 static void start_vis_timer(struct bat_priv *bat_priv)
976 {
977         INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
978         queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work,
979                            msecs_to_jiffies(VIS_INTERVAL));
980 }