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