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