Staging: batman-adv: convert more files from /proc to /sys
[safe/jmp/linux-2.6] / 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 /* Returns the smallest signed integer in two's complement with the sizeof x */
31 #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
32
33 /* Checks if a sequence number x is a predecessor/successor of y.
34    they handle overflows/underflows and can correctly check for a
35    predecessor/successor unless the variable sequence number has grown by
36    more then 2**(bitwidth(x)-1)-1.
37    This means that for a uint8_t with the maximum value 255, it would think:
38     * when adding nothing - it is neither a predecessor nor a successor
39     * before adding more than 127 to the starting value - it is a predecessor,
40     * when adding 128 - it is neither a predecessor nor a successor,
41     * after adding more than 127 to the starting value - it is a successor */
42 #define seq_before(x, y) ({typeof(x) _dummy = (x - y); \
43                           _dummy > smallest_signed_int(_dummy); })
44 #define seq_after(x, y) seq_before(y, x)
45
46 struct hashtable_t *vis_hash;
47 DEFINE_SPINLOCK(vis_hash_lock);
48 static DEFINE_SPINLOCK(recv_list_lock);
49 static struct vis_info *my_vis_info;
50 static struct list_head send_list;      /* always locked with vis_hash_lock */
51
52 static void start_vis_timer(void);
53
54 /* free the info */
55 static void free_info(struct kref *ref)
56 {
57         struct vis_info *info = container_of(ref, struct vis_info, refcount);
58         struct recvlist_node *entry, *tmp;
59         unsigned long flags;
60
61         list_del_init(&info->send_list);
62         spin_lock_irqsave(&recv_list_lock, flags);
63         list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
64                 list_del(&entry->list);
65                 kfree(entry);
66         }
67         spin_unlock_irqrestore(&recv_list_lock, flags);
68         kfree(info);
69 }
70
71 /* Compare two vis packets, used by the hashing algorithm */
72 static int vis_info_cmp(void *data1, void *data2)
73 {
74         struct vis_info *d1, *d2;
75         d1 = data1;
76         d2 = data2;
77         return compare_orig(d1->packet.vis_orig, d2->packet.vis_orig);
78 }
79
80 /* hash function to choose an entry in a hash table of given size */
81 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
82 static int vis_info_choose(void *data, int size)
83 {
84         struct vis_info *vis_info = data;
85         unsigned char *key;
86         uint32_t hash = 0;
87         size_t i;
88
89         key = vis_info->packet.vis_orig;
90         for (i = 0; i < ETH_ALEN; i++) {
91                 hash += key[i];
92                 hash += (hash << 10);
93                 hash ^= (hash >> 6);
94         }
95
96         hash += (hash << 3);
97         hash ^= (hash >> 11);
98         hash += (hash << 15);
99
100         return hash % size;
101 }
102
103 /* insert interface to the list of interfaces of one originator, if it
104  * does not already exist in the list */
105 static void vis_data_insert_interface(const uint8_t *interface,
106                                       struct hlist_head *if_list,
107                                       bool primary)
108 {
109         struct if_list_entry *entry;
110         struct hlist_node *pos;
111
112         hlist_for_each_entry(entry, pos, if_list, list) {
113                 if (compare_orig(entry->addr, (void *)interface))
114                         return;
115         }
116
117         /* its a new address, add it to the list */
118         entry = kmalloc(sizeof(*entry), GFP_KERNEL);
119         if (!entry)
120                 return;
121         memcpy(entry->addr, interface, ETH_ALEN);
122         entry->primary = primary;
123         hlist_add_head(&entry->list, if_list);
124 }
125
126 static ssize_t vis_data_read_prim_sec(char *buff, struct hlist_head *if_list)
127 {
128         struct if_list_entry *entry;
129         struct hlist_node *pos;
130         char tmp_addr_str[ETH_STR_LEN];
131         size_t len = 0;
132
133         hlist_for_each_entry(entry, pos, if_list, list) {
134                 if (entry->primary)
135                         len += sprintf(buff + len, "PRIMARY, ");
136                 else {
137                         addr_to_string(tmp_addr_str, entry->addr);
138                         len += sprintf(buff + len,  "SEC %s, ", tmp_addr_str);
139                 }
140         }
141
142         return len;
143 }
144
145 /* read an entry  */
146 static ssize_t vis_data_read_entry(char *buff, struct vis_info_entry *entry,
147                                    uint8_t *src, bool primary)
148 {
149         char to[40];
150
151         addr_to_string(to, entry->dest);
152         if (primary && entry->quality == 0)
153                 return sprintf(buff, "HNA %s, ", to);
154         else if (compare_orig(entry->src, src))
155                 return sprintf(buff, "TQ %s %d, ", to, entry->quality);
156
157         return 0;
158 }
159
160 ssize_t vis_fill_buffer_text(struct net_device *net_dev, char *buff,
161                               size_t count, loff_t off)
162 {
163         HASHIT(hashit);
164         struct vis_info *info;
165         struct vis_info_entry *entries;
166         struct bat_priv *bat_priv = netdev_priv(net_dev);
167         HLIST_HEAD(vis_if_list);
168         struct if_list_entry *entry;
169         struct hlist_node *pos, *n;
170         size_t hdr_len, tmp_len;
171         int i, bytes_written = 0;
172         char tmp_addr_str[ETH_STR_LEN];
173         unsigned long flags;
174         int vis_server = atomic_read(&bat_priv->vis_mode);
175
176         rcu_read_lock();
177         if (list_empty(&if_list) || (vis_server == VIS_TYPE_CLIENT_UPDATE)) {
178                 rcu_read_unlock();
179                 return 0;
180         }
181
182         rcu_read_unlock();
183         hdr_len = 0;
184
185         spin_lock_irqsave(&vis_hash_lock, flags);
186         while (hash_iterate(vis_hash, &hashit)) {
187                 info = hashit.bucket->data;
188                 entries = (struct vis_info_entry *)
189                         ((char *)info + sizeof(struct vis_info));
190
191                 /* estimated line length */
192                 if (count < bytes_written + 200)
193                         break;
194
195                 for (i = 0; i < info->packet.entries; i++) {
196                         if (entries[i].quality == 0)
197                                 continue;
198                         vis_data_insert_interface(entries[i].src, &vis_if_list,
199                                 compare_orig(entries[i].src,
200                                                 info->packet.vis_orig));
201                 }
202
203                 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
204                         addr_to_string(tmp_addr_str, entry->addr);
205                         tmp_len = sprintf(buff + bytes_written,
206                                           "%s,", tmp_addr_str);
207
208                         for (i = 0; i < info->packet.entries; i++)
209                                 tmp_len += vis_data_read_entry(
210                                                 buff + bytes_written + tmp_len,
211                                                 &entries[i], entry->addr,
212                                                 entry->primary);
213
214                         /* add primary/secondary records */
215                         if (compare_orig(entry->addr, info->packet.vis_orig))
216                                 tmp_len += vis_data_read_prim_sec(
217                                                 buff + bytes_written + tmp_len,
218                                                 &vis_if_list);
219
220                         tmp_len += sprintf(buff + bytes_written + tmp_len,
221                                           "\n");
222
223                         hdr_len += tmp_len;
224
225                         if (off >= hdr_len)
226                                 continue;
227
228                         bytes_written += tmp_len;
229                 }
230
231                 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
232                         hlist_del(&entry->list);
233                         kfree(entry);
234                 }
235         }
236         spin_unlock_irqrestore(&vis_hash_lock, flags);
237
238         return bytes_written;
239 }
240
241 /* add the info packet to the send list, if it was not
242  * already linked in. */
243 static void send_list_add(struct vis_info *info)
244 {
245         if (list_empty(&info->send_list)) {
246                 kref_get(&info->refcount);
247                 list_add_tail(&info->send_list, &send_list);
248         }
249 }
250
251 /* delete the info packet from the send list, if it was
252  * linked in. */
253 static void send_list_del(struct vis_info *info)
254 {
255         if (!list_empty(&info->send_list)) {
256                 list_del_init(&info->send_list);
257                 kref_put(&info->refcount, free_info);
258         }
259 }
260
261 /* tries to add one entry to the receive list. */
262 static void recv_list_add(struct list_head *recv_list, char *mac)
263 {
264         struct recvlist_node *entry;
265         unsigned long flags;
266
267         entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC);
268         if (!entry)
269                 return;
270
271         memcpy(entry->mac, mac, ETH_ALEN);
272         spin_lock_irqsave(&recv_list_lock, flags);
273         list_add_tail(&entry->list, recv_list);
274         spin_unlock_irqrestore(&recv_list_lock, flags);
275 }
276
277 /* returns 1 if this mac is in the recv_list */
278 static int recv_list_is_in(struct list_head *recv_list, char *mac)
279 {
280         struct recvlist_node *entry;
281         unsigned long flags;
282
283         spin_lock_irqsave(&recv_list_lock, flags);
284         list_for_each_entry(entry, recv_list, list) {
285                 if (memcmp(entry->mac, mac, ETH_ALEN) == 0) {
286                         spin_unlock_irqrestore(&recv_list_lock, flags);
287                         return 1;
288                 }
289         }
290         spin_unlock_irqrestore(&recv_list_lock, flags);
291         return 0;
292 }
293
294 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
295  * broken.. ).  vis hash must be locked outside.  is_new is set when the packet
296  * is newer than old entries in the hash. */
297 static struct vis_info *add_packet(struct vis_packet *vis_packet,
298                                    int vis_info_len, int *is_new,
299                                    int make_broadcast)
300 {
301         struct vis_info *info, *old_info;
302         struct vis_info search_elem;
303
304         *is_new = 0;
305         /* sanity check */
306         if (vis_hash == NULL)
307                 return NULL;
308
309         /* see if the packet is already in vis_hash */
310         memcpy(search_elem.packet.vis_orig, vis_packet->vis_orig, ETH_ALEN);
311         old_info = hash_find(vis_hash, &search_elem);
312
313         if (old_info != NULL) {
314                 if (!seq_after(vis_packet->seqno, old_info->packet.seqno)) {
315                         if (old_info->packet.seqno == vis_packet->seqno) {
316                                 recv_list_add(&old_info->recv_list,
317                                               vis_packet->sender_orig);
318                                 return old_info;
319                         } else {
320                                 /* newer packet is already in hash. */
321                                 return NULL;
322                         }
323                 }
324                 /* remove old entry */
325                 hash_remove(vis_hash, old_info);
326                 send_list_del(old_info);
327                 kref_put(&old_info->refcount, free_info);
328         }
329
330         info = kmalloc(sizeof(struct vis_info) + vis_info_len, GFP_ATOMIC);
331         if (info == NULL)
332                 return NULL;
333
334         kref_init(&info->refcount);
335         INIT_LIST_HEAD(&info->send_list);
336         INIT_LIST_HEAD(&info->recv_list);
337         info->first_seen = jiffies;
338         memcpy(&info->packet, vis_packet,
339                sizeof(struct vis_packet) + vis_info_len);
340
341         /* initialize and add new packet. */
342         *is_new = 1;
343
344         /* Make it a broadcast packet, if required */
345         if (make_broadcast)
346                 memcpy(info->packet.target_orig, broadcastAddr, ETH_ALEN);
347
348         /* repair if entries is longer than packet. */
349         if (info->packet.entries * sizeof(struct vis_info_entry) > vis_info_len)
350                 info->packet.entries = vis_info_len /
351                         sizeof(struct vis_info_entry);
352
353         recv_list_add(&info->recv_list, info->packet.sender_orig);
354
355         /* try to add it */
356         if (hash_add(vis_hash, info) < 0) {
357                 /* did not work (for some reason) */
358                 kref_put(&old_info->refcount, free_info);
359                 info = NULL;
360         }
361
362         return info;
363 }
364
365 /* handle the server sync packet, forward if needed. */
366 void receive_server_sync_packet(struct bat_priv *bat_priv,
367                                 struct vis_packet *vis_packet,
368                                 int vis_info_len)
369 {
370         struct vis_info *info;
371         int is_new, make_broadcast;
372         unsigned long flags;
373         int vis_server = atomic_read(&bat_priv->vis_mode);
374
375         make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
376
377         spin_lock_irqsave(&vis_hash_lock, flags);
378         info = add_packet(vis_packet, vis_info_len, &is_new, make_broadcast);
379         if (info == NULL)
380                 goto end;
381
382         /* only if we are server ourselves and packet is newer than the one in
383          * hash.*/
384         if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
385                 send_list_add(info);
386 end:
387         spin_unlock_irqrestore(&vis_hash_lock, flags);
388 }
389
390 /* handle an incoming client update packet and schedule forward if needed. */
391 void receive_client_update_packet(struct bat_priv *bat_priv,
392                                   struct vis_packet *vis_packet,
393                                   int vis_info_len)
394 {
395         struct vis_info *info;
396         int is_new;
397         unsigned long flags;
398         int vis_server = atomic_read(&bat_priv->vis_mode);
399         int are_target = 0;
400
401         /* clients shall not broadcast. */
402         if (is_bcast(vis_packet->target_orig))
403                 return;
404
405         /* Are we the target for this VIS packet? */
406         if (vis_server == VIS_TYPE_SERVER_SYNC  &&
407             is_my_mac(vis_packet->target_orig))
408                 are_target = 1;
409
410         spin_lock_irqsave(&vis_hash_lock, flags);
411         info = add_packet(vis_packet, vis_info_len, &is_new, are_target);
412         if (info == NULL)
413                 goto end;
414         /* note that outdated packets will be dropped at this point. */
415
416
417         /* send only if we're the target server or ... */
418         if (are_target && is_new) {
419                 info->packet.vis_type = VIS_TYPE_SERVER_SYNC;   /* upgrade! */
420                 send_list_add(info);
421
422                 /* ... we're not the recipient (and thus need to forward). */
423         } else if (!is_my_mac(info->packet.target_orig)) {
424                 send_list_add(info);
425         }
426 end:
427         spin_unlock_irqrestore(&vis_hash_lock, flags);
428 }
429
430 /* Walk the originators and find the VIS server with the best tq. Set the packet
431  * address to its address and return the best_tq.
432  *
433  * Must be called with the originator hash locked */
434 static int find_best_vis_server(struct vis_info *info)
435 {
436         HASHIT(hashit);
437         struct orig_node *orig_node;
438         int best_tq = -1;
439
440         while (hash_iterate(orig_hash, &hashit)) {
441                 orig_node = hashit.bucket->data;
442                 if ((orig_node != NULL) &&
443                     (orig_node->router != NULL) &&
444                     (orig_node->flags & VIS_SERVER) &&
445                     (orig_node->router->tq_avg > best_tq)) {
446                         best_tq = orig_node->router->tq_avg;
447                         memcpy(info->packet.target_orig, orig_node->orig,
448                                ETH_ALEN);
449                 }
450         }
451         return best_tq;
452 }
453
454 /* Return true if the vis packet is full. */
455 static bool vis_packet_full(struct vis_info *info)
456 {
457         if (info->packet.entries + 1 >
458             (1000 - sizeof(struct vis_info)) / sizeof(struct vis_info_entry))
459                 return true;
460         return false;
461 }
462
463 /* generates a packet of own vis data,
464  * returns 0 on success, -1 if no packet could be generated */
465 static int generate_vis_packet(struct bat_priv *bat_priv)
466 {
467         HASHIT(hashit_local);
468         HASHIT(hashit_global);
469         struct orig_node *orig_node;
470         struct vis_info *info = (struct vis_info *)my_vis_info;
471         struct vis_info_entry *entry, *entry_array;
472         struct hna_local_entry *hna_local_entry;
473         int best_tq = -1;
474         unsigned long flags;
475
476         info->first_seen = jiffies;
477         info->packet.vis_type = atomic_read(&bat_priv->vis_mode);
478
479         spin_lock_irqsave(&orig_hash_lock, flags);
480         memcpy(info->packet.target_orig, broadcastAddr, ETH_ALEN);
481         info->packet.ttl = TTL;
482         info->packet.seqno++;
483         info->packet.entries = 0;
484
485         if (info->packet.vis_type == VIS_TYPE_CLIENT_UPDATE) {
486                 best_tq = find_best_vis_server(info);
487                 if (best_tq < 0) {
488                         spin_unlock_irqrestore(&orig_hash_lock, flags);
489                         return -1;
490                 }
491         }
492
493         entry_array = (struct vis_info_entry *)
494                 ((char *)info + sizeof(struct vis_info));
495
496         while (hash_iterate(orig_hash, &hashit_global)) {
497                 orig_node = hashit_global.bucket->data;
498                 if (orig_node->router != NULL
499                         && compare_orig(orig_node->router->addr,
500                                         orig_node->orig)
501                         && orig_node->batman_if
502                         && (orig_node->batman_if->if_active == IF_ACTIVE)
503                     && orig_node->router->tq_avg > 0) {
504
505                         /* fill one entry into buffer. */
506                         entry = &entry_array[info->packet.entries];
507                         memcpy(entry->src,
508                                orig_node->batman_if->net_dev->dev_addr,
509                                ETH_ALEN);
510                         memcpy(entry->dest, orig_node->orig, ETH_ALEN);
511                         entry->quality = orig_node->router->tq_avg;
512                         info->packet.entries++;
513
514                         if (vis_packet_full(info)) {
515                                 spin_unlock_irqrestore(&orig_hash_lock, flags);
516                                 return 0;
517                         }
518                 }
519         }
520
521         spin_unlock_irqrestore(&orig_hash_lock, flags);
522
523         spin_lock_irqsave(&hna_local_hash_lock, flags);
524         while (hash_iterate(hna_local_hash, &hashit_local)) {
525                 hna_local_entry = hashit_local.bucket->data;
526                 entry = &entry_array[info->packet.entries];
527                 memset(entry->src, 0, ETH_ALEN);
528                 memcpy(entry->dest, hna_local_entry->addr, ETH_ALEN);
529                 entry->quality = 0; /* 0 means HNA */
530                 info->packet.entries++;
531
532                 if (vis_packet_full(info)) {
533                         spin_unlock_irqrestore(&hna_local_hash_lock, flags);
534                         return 0;
535                 }
536         }
537         spin_unlock_irqrestore(&hna_local_hash_lock, flags);
538         return 0;
539 }
540
541 /* free old vis packets. Must be called with this vis_hash_lock
542  * held */
543 static void purge_vis_packets(void)
544 {
545         HASHIT(hashit);
546         struct vis_info *info;
547
548         while (hash_iterate(vis_hash, &hashit)) {
549                 info = hashit.bucket->data;
550                 if (info == my_vis_info)        /* never purge own data. */
551                         continue;
552                 if (time_after(jiffies,
553                                info->first_seen + (VIS_TIMEOUT*HZ)/1000)) {
554                         hash_remove_bucket(vis_hash, &hashit);
555                         send_list_del(info);
556                         kref_put(&info->refcount, free_info);
557                 }
558         }
559 }
560
561 static void broadcast_vis_packet(struct vis_info *info, int packet_length)
562 {
563         HASHIT(hashit);
564         struct orig_node *orig_node;
565         unsigned long flags;
566         struct batman_if *batman_if;
567         uint8_t dstaddr[ETH_ALEN];
568
569         spin_lock_irqsave(&orig_hash_lock, flags);
570
571         /* send to all routers in range. */
572         while (hash_iterate(orig_hash, &hashit)) {
573                 orig_node = hashit.bucket->data;
574
575                 /* if it's a vis server and reachable, send it. */
576                 if ((!orig_node) || (!orig_node->batman_if) ||
577                     (!orig_node->router))
578                         continue;
579                 if (!(orig_node->flags & VIS_SERVER))
580                         continue;
581                 /* don't send it if we already received the packet from
582                  * this node. */
583                 if (recv_list_is_in(&info->recv_list, orig_node->orig))
584                         continue;
585
586                 memcpy(info->packet.target_orig, orig_node->orig, ETH_ALEN);
587                 batman_if = orig_node->batman_if;
588                 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
589                 spin_unlock_irqrestore(&orig_hash_lock, flags);
590
591                 send_raw_packet((unsigned char *)&info->packet,
592                                 packet_length, batman_if, dstaddr);
593
594                 spin_lock_irqsave(&orig_hash_lock, flags);
595
596         }
597         spin_unlock_irqrestore(&orig_hash_lock, flags);
598         memcpy(info->packet.target_orig, broadcastAddr, ETH_ALEN);
599 }
600
601 static void unicast_vis_packet(struct vis_info *info, int packet_length)
602 {
603         struct orig_node *orig_node;
604         unsigned long flags;
605         struct batman_if *batman_if;
606         uint8_t dstaddr[ETH_ALEN];
607
608         spin_lock_irqsave(&orig_hash_lock, flags);
609         orig_node = ((struct orig_node *)
610                      hash_find(orig_hash, info->packet.target_orig));
611
612         if ((!orig_node) || (!orig_node->batman_if) || (!orig_node->router))
613                 goto out;
614
615         /* don't lock while sending the packets ... we therefore
616          * copy the required data before sending */
617         batman_if = orig_node->batman_if;
618         memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
619         spin_unlock_irqrestore(&orig_hash_lock, flags);
620
621         send_raw_packet((unsigned char *)&info->packet,
622                         packet_length, batman_if, dstaddr);
623         return;
624
625 out:
626         spin_unlock_irqrestore(&orig_hash_lock, flags);
627 }
628
629 /* only send one vis packet. called from send_vis_packets() */
630 static void send_vis_packet(struct vis_info *info)
631 {
632         int packet_length;
633
634         if (info->packet.ttl < 2) {
635                 printk(KERN_WARNING "batman-adv: Error - can't send vis packet: ttl exceeded\n");
636                 return;
637         }
638
639         memcpy(info->packet.sender_orig, mainIfAddr, ETH_ALEN);
640         info->packet.ttl--;
641
642         packet_length = sizeof(struct vis_packet) +
643                 info->packet.entries * sizeof(struct vis_info_entry);
644
645         if (is_bcast(info->packet.target_orig))
646                 broadcast_vis_packet(info, packet_length);
647         else
648                 unicast_vis_packet(info, packet_length);
649         info->packet.ttl++; /* restore TTL */
650 }
651
652 /* called from timer; send (and maybe generate) vis packet. */
653 static void send_vis_packets(struct work_struct *work)
654 {
655         struct vis_info *info, *temp;
656         unsigned long flags;
657         /* FIXME: each batman_if will be attached to a softif */
658         struct bat_priv *bat_priv = netdev_priv(soft_device);
659
660         spin_lock_irqsave(&vis_hash_lock, flags);
661
662         purge_vis_packets();
663
664         if (generate_vis_packet(bat_priv) == 0) {
665                 /* schedule if generation was successful */
666                 send_list_add(my_vis_info);
667         }
668
669         list_for_each_entry_safe(info, temp, &send_list, send_list) {
670
671                 kref_get(&info->refcount);
672                 spin_unlock_irqrestore(&vis_hash_lock, flags);
673
674                 send_vis_packet(info);
675
676                 spin_lock_irqsave(&vis_hash_lock, flags);
677                 send_list_del(info);
678                 kref_put(&info->refcount, free_info);
679         }
680         spin_unlock_irqrestore(&vis_hash_lock, flags);
681         start_vis_timer();
682 }
683 static DECLARE_DELAYED_WORK(vis_timer_wq, send_vis_packets);
684
685 /* init the vis server. this may only be called when if_list is already
686  * initialized (e.g. bat0 is initialized, interfaces have been added) */
687 int vis_init(void)
688 {
689         unsigned long flags;
690         if (vis_hash)
691                 return 1;
692
693         spin_lock_irqsave(&vis_hash_lock, flags);
694
695         vis_hash = hash_new(256, vis_info_cmp, vis_info_choose);
696         if (!vis_hash) {
697                 printk(KERN_ERR "batman-adv:Can't initialize vis_hash\n");
698                 goto err;
699         }
700
701         my_vis_info = kmalloc(1000, GFP_ATOMIC);
702         if (!my_vis_info) {
703                 printk(KERN_ERR "batman-adv:Can't initialize vis packet\n");
704                 goto err;
705         }
706
707         /* prefill the vis info */
708         my_vis_info->first_seen = jiffies - atomic_read(&vis_interval);
709         INIT_LIST_HEAD(&my_vis_info->recv_list);
710         INIT_LIST_HEAD(&my_vis_info->send_list);
711         kref_init(&my_vis_info->refcount);
712         my_vis_info->packet.version = COMPAT_VERSION;
713         my_vis_info->packet.packet_type = BAT_VIS;
714         my_vis_info->packet.ttl = TTL;
715         my_vis_info->packet.seqno = 0;
716         my_vis_info->packet.entries = 0;
717
718         INIT_LIST_HEAD(&send_list);
719
720         memcpy(my_vis_info->packet.vis_orig, mainIfAddr, ETH_ALEN);
721         memcpy(my_vis_info->packet.sender_orig, mainIfAddr, ETH_ALEN);
722
723         if (hash_add(vis_hash, my_vis_info) < 0) {
724                 printk(KERN_ERR
725                        "batman-adv:Can't add own vis packet into hash\n");
726                 /* not in hash, need to remove it manually. */
727                 kref_put(&my_vis_info->refcount, free_info);
728                 goto err;
729         }
730
731         spin_unlock_irqrestore(&vis_hash_lock, flags);
732         start_vis_timer();
733         return 1;
734
735 err:
736         spin_unlock_irqrestore(&vis_hash_lock, flags);
737         vis_quit();
738         return 0;
739 }
740
741 /* Decrease the reference count on a hash item info */
742 static void free_info_ref(void *data)
743 {
744         struct vis_info *info = data;
745
746         send_list_del(info);
747         kref_put(&info->refcount, free_info);
748 }
749
750 /* shutdown vis-server */
751 void vis_quit(void)
752 {
753         unsigned long flags;
754         if (!vis_hash)
755                 return;
756
757         cancel_delayed_work_sync(&vis_timer_wq);
758
759         spin_lock_irqsave(&vis_hash_lock, flags);
760         /* properly remove, kill timers ... */
761         hash_delete(vis_hash, free_info_ref);
762         vis_hash = NULL;
763         my_vis_info = NULL;
764         spin_unlock_irqrestore(&vis_hash_lock, flags);
765 }
766
767 /* schedule packets for (re)transmission */
768 static void start_vis_timer(void)
769 {
770         queue_delayed_work(bat_event_workqueue, &vis_timer_wq,
771                            (atomic_read(&vis_interval) * HZ) / 1000);
772 }