Staging: batman-adv: Remove hashdata_compare_cb from hash
[linux-2.6/btrfs-unstable.git] / drivers / staging / batman-adv / vis.c
blobff0abe9f5607a9423e098a181d29025c11ab6d6d
1 /*
2 * Copyright (C) 2008-2010 B.A.T.M.A.N. contributors:
4 * Simon Wunderlich
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.
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.
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
22 #include "main.h"
23 #include "send.h"
24 #include "translation-table.h"
25 #include "vis.h"
26 #include "soft-interface.h"
27 #include "hard-interface.h"
28 #include "hash.h"
29 #include "originator.h"
31 #define MAX_VIS_PACKET_SIZE 1000
33 /* Returns the smallest signed integer in two's complement with the sizeof x */
34 #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
36 /* Checks if a sequence number x is a predecessor/successor of y.
37 * they handle overflows/underflows and can correctly check for a
38 * predecessor/successor unless the variable sequence number has grown by
39 * more then 2**(bitwidth(x)-1)-1.
40 * This means that for a uint8_t with the maximum value 255, it would think:
41 * - when adding nothing - it is neither a predecessor nor a successor
42 * - before adding more than 127 to the starting value - it is a predecessor,
43 * - when adding 128 - it is neither a predecessor nor a successor,
44 * - after adding more than 127 to the starting value - it is a successor */
45 #define seq_before(x, y) ({typeof(x) _dummy = (x - y); \
46 _dummy > smallest_signed_int(_dummy); })
47 #define seq_after(x, y) seq_before(y, x)
49 static void start_vis_timer(struct bat_priv *bat_priv);
51 /* free the info */
52 static void free_info(struct kref *ref)
54 struct vis_info *info = container_of(ref, struct vis_info, refcount);
55 struct bat_priv *bat_priv = info->bat_priv;
56 struct recvlist_node *entry, *tmp;
57 unsigned long flags;
59 list_del_init(&info->send_list);
60 spin_lock_irqsave(&bat_priv->vis_list_lock, flags);
61 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
62 list_del(&entry->list);
63 kfree(entry);
66 spin_unlock_irqrestore(&bat_priv->vis_list_lock, flags);
67 kfree_skb(info->skb_packet);
70 /* Compare two vis packets, used by the hashing algorithm */
71 static int vis_info_cmp(void *data1, void *data2)
73 struct vis_info *d1, *d2;
74 struct vis_packet *p1, *p2;
75 d1 = data1;
76 d2 = data2;
77 p1 = (struct vis_packet *)d1->skb_packet->data;
78 p2 = (struct vis_packet *)d2->skb_packet->data;
79 return compare_orig(p1->vis_orig, p2->vis_orig);
82 /* hash function to choose an entry in a hash table of given size */
83 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
84 static int vis_info_choose(void *data, int size)
86 struct vis_info *vis_info = data;
87 struct vis_packet *packet;
88 unsigned char *key;
89 uint32_t hash = 0;
90 size_t i;
92 packet = (struct vis_packet *)vis_info->skb_packet->data;
93 key = packet->vis_orig;
94 for (i = 0; i < ETH_ALEN; i++) {
95 hash += key[i];
96 hash += (hash << 10);
97 hash ^= (hash >> 6);
100 hash += (hash << 3);
101 hash ^= (hash >> 11);
102 hash += (hash << 15);
104 return hash % size;
107 /* insert interface to the list of interfaces of one originator, if it
108 * does not already exist in the list */
109 static void vis_data_insert_interface(const uint8_t *interface,
110 struct hlist_head *if_list,
111 bool primary)
113 struct if_list_entry *entry;
114 struct hlist_node *pos;
116 hlist_for_each_entry(entry, pos, if_list, list) {
117 if (compare_orig(entry->addr, (void *)interface))
118 return;
121 /* its a new address, add it to the list */
122 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
123 if (!entry)
124 return;
125 memcpy(entry->addr, interface, ETH_ALEN);
126 entry->primary = primary;
127 hlist_add_head(&entry->list, if_list);
130 static ssize_t vis_data_read_prim_sec(char *buff, struct hlist_head *if_list)
132 struct if_list_entry *entry;
133 struct hlist_node *pos;
134 size_t len = 0;
136 hlist_for_each_entry(entry, pos, if_list, list) {
137 if (entry->primary)
138 len += sprintf(buff + len, "PRIMARY, ");
139 else
140 len += sprintf(buff + len, "SEC %pM, ", entry->addr);
143 return len;
146 static size_t vis_data_count_prim_sec(struct hlist_head *if_list)
148 struct if_list_entry *entry;
149 struct hlist_node *pos;
150 size_t count = 0;
152 hlist_for_each_entry(entry, pos, if_list, list) {
153 if (entry->primary)
154 count += 9;
155 else
156 count += 23;
159 return count;
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)
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);
173 return 0;
176 int vis_seq_print_text(struct seq_file *seq, void *offset)
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;
194 if ((!bat_priv->primary_if) ||
195 (vis_server == VIS_TYPE_CLIENT_UPDATE))
196 return 0;
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));
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));
214 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
215 buf_size += 18 + 26 * packet->entries;
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);
222 buf_size += 1;
225 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
226 hlist_del(&entry->list);
227 kfree(entry);
231 buff = kmalloc(buf_size, GFP_ATOMIC);
232 if (!buff) {
233 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
234 return -ENOMEM;
236 buff[0] = '\0';
237 buff_pos = 0;
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));
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));
252 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
253 buff_pos += sprintf(buff + buff_pos, "%pM,",
254 entry->addr);
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);
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);
268 buff_pos += sprintf(buff + buff_pos, "\n");
271 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
272 hlist_del(&entry->list);
273 kfree(entry);
277 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
279 seq_printf(seq, "%s", buff);
280 kfree(buff);
282 return 0;
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)
289 if (list_empty(&info->send_list)) {
290 kref_get(&info->refcount);
291 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
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)
299 if (!list_empty(&info->send_list)) {
300 list_del_init(&info->send_list);
301 kref_put(&info->refcount, free_info);
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)
309 struct recvlist_node *entry;
310 unsigned long flags;
312 entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC);
313 if (!entry)
314 return;
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);
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)
326 struct recvlist_node *entry;
327 unsigned long flags;
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;
337 spin_unlock_irqrestore(&bat_priv->vis_list_lock, flags);
338 return 0;
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)
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;
354 *is_new = 0;
355 /* sanity check */
356 if (!bat_priv->vis_hash)
357 return NULL;
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));
366 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
367 old_info = hash_find(bat_priv->vis_hash, vis_info_cmp, &search_elem);
368 kfree_skb(search_elem.skb_packet);
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;
383 /* remove old entry */
384 hash_remove(bat_priv->vis_hash, vis_info_cmp, old_info);
385 send_list_del(old_info);
386 kref_put(&old_info->refcount, free_info);
389 info = kmalloc(sizeof(struct vis_info), GFP_ATOMIC);
390 if (!info)
391 return NULL;
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;
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);
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);
411 /* initialize and add new packet. */
412 *is_new = 1;
414 /* Make it a broadcast packet, if required */
415 if (make_broadcast)
416 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
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);
422 recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
424 /* try to add it */
425 if (hash_add(bat_priv->vis_hash, vis_info_cmp, info) < 0) {
426 /* did not work (for some reason) */
427 kref_put(&old_info->refcount, free_info);
428 info = NULL;
431 return info;
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)
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);
444 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
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;
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);
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)
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;
472 /* clients shall not broadcast. */
473 if (is_bcast(vis_packet->target_orig))
474 return;
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;
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);
485 if (!info)
486 goto end;
487 /* note that outdated packets will be dropped at this point. */
489 packet = (struct vis_packet *)info->skb_packet->data;
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);
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);
501 end:
502 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
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.
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)
512 HASHIT(hashit);
513 struct orig_node *orig_node;
514 struct vis_packet *packet;
515 int best_tq = -1;
517 packet = (struct vis_packet *)info->skb_packet->data;
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);
528 return best_tq;
531 /* Return true if the vis packet is full. */
532 static bool vis_packet_full(struct vis_info *info)
534 struct vis_packet *packet;
535 packet = (struct vis_packet *)info->skb_packet->data;
537 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
538 < packet->entries + 1)
539 return true;
540 return false;
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)
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;
557 info->first_seen = jiffies;
558 packet->vis_type = atomic_read(&bat_priv->vis_mode);
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));
567 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
568 best_tq = find_best_vis_server(bat_priv, info);
570 if (best_tq < 0) {
571 spin_unlock_irqrestore(&bat_priv->orig_hash_lock,
572 flags);
573 return -1;
577 while (hash_iterate(bat_priv->orig_hash, &hashit_global)) {
578 orig_node = hashit_global.bucket->data;
580 if (!orig_node->router)
581 continue;
583 if (!compare_orig(orig_node->router->addr, orig_node->orig))
584 continue;
586 if (orig_node->router->if_incoming->if_status != IF_ACTIVE)
587 continue;
589 if (orig_node->router->tq_avg < 1)
590 continue;
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++;
602 if (vis_packet_full(info)) {
603 spin_unlock_irqrestore(
604 &bat_priv->orig_hash_lock, flags);
605 return 0;
609 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
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++;
621 if (vis_packet_full(info)) {
622 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock,
623 flags);
624 return 0;
628 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
629 return 0;
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)
636 HASHIT(hashit);
637 struct vis_info *info;
639 while (hash_iterate(bat_priv->vis_hash, &hashit)) {
640 info = hashit.bucket->data;
642 /* never purge own data. */
643 if (info == bat_priv->my_vis_info)
644 continue;
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);
655 static void broadcast_vis_packet(struct bat_priv *bat_priv,
656 struct vis_info *info)
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];
667 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
668 packet = (struct vis_packet *)info->skb_packet->data;
670 /* send to all routers in range. */
671 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
672 orig_node = hashit.bucket->data;
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;
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);
690 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
691 if (skb)
692 send_skb_packet(skb, batman_if, dstaddr);
694 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
698 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
701 static void unicast_vis_packet(struct bat_priv *bat_priv,
702 struct vis_info *info)
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];
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 compare_orig,
715 packet->target_orig));
717 if ((!orig_node) || (!orig_node->router))
718 goto out;
720 /* don't lock while sending the packets ... we therefore
721 * copy the required data before sending */
722 batman_if = orig_node->router->if_incoming;
723 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
724 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
726 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
727 if (skb)
728 send_skb_packet(skb, batman_if, dstaddr);
730 return;
732 out:
733 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
736 /* only send one vis packet. called from send_vis_packets() */
737 static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
739 struct vis_packet *packet;
741 packet = (struct vis_packet *)info->skb_packet->data;
742 if (packet->ttl < 2) {
743 pr_debug("Error - can't send vis packet: ttl exceeded\n");
744 return;
747 memcpy(packet->sender_orig, bat_priv->primary_if->net_dev->dev_addr,
748 ETH_ALEN);
749 packet->ttl--;
751 if (is_bcast(packet->target_orig))
752 broadcast_vis_packet(bat_priv, info);
753 else
754 unicast_vis_packet(bat_priv, info);
755 packet->ttl++; /* restore TTL */
758 /* called from timer; send (and maybe generate) vis packet. */
759 static void send_vis_packets(struct work_struct *work)
761 struct delayed_work *delayed_work =
762 container_of(work, struct delayed_work, work);
763 struct bat_priv *bat_priv =
764 container_of(delayed_work, struct bat_priv, vis_work);
765 struct vis_info *info, *temp;
766 unsigned long flags;
768 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
769 purge_vis_packets(bat_priv);
771 if (generate_vis_packet(bat_priv) == 0) {
772 /* schedule if generation was successful */
773 send_list_add(bat_priv, bat_priv->my_vis_info);
776 list_for_each_entry_safe(info, temp, &bat_priv->vis_send_list,
777 send_list) {
779 kref_get(&info->refcount);
780 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
782 if (bat_priv->primary_if)
783 send_vis_packet(bat_priv, info);
785 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
786 send_list_del(info);
787 kref_put(&info->refcount, free_info);
789 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
790 start_vis_timer(bat_priv);
793 /* init the vis server. this may only be called when if_list is already
794 * initialized (e.g. bat0 is initialized, interfaces have been added) */
795 int vis_init(struct bat_priv *bat_priv)
797 struct vis_packet *packet;
798 unsigned long flags;
799 int hash_added;
801 if (bat_priv->vis_hash)
802 return 1;
804 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
806 bat_priv->vis_hash = hash_new(256, vis_info_choose);
807 if (!bat_priv->vis_hash) {
808 pr_err("Can't initialize vis_hash\n");
809 goto err;
812 bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
813 if (!bat_priv->my_vis_info) {
814 pr_err("Can't initialize vis packet\n");
815 goto err;
818 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(
819 sizeof(struct vis_packet) +
820 MAX_VIS_PACKET_SIZE +
821 sizeof(struct ethhdr));
822 if (!bat_priv->my_vis_info->skb_packet)
823 goto free_info;
825 skb_reserve(bat_priv->my_vis_info->skb_packet, sizeof(struct ethhdr));
826 packet = (struct vis_packet *)skb_put(
827 bat_priv->my_vis_info->skb_packet,
828 sizeof(struct vis_packet));
830 /* prefill the vis info */
831 bat_priv->my_vis_info->first_seen = jiffies -
832 msecs_to_jiffies(VIS_INTERVAL);
833 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
834 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
835 kref_init(&bat_priv->my_vis_info->refcount);
836 bat_priv->my_vis_info->bat_priv = bat_priv;
837 packet->version = COMPAT_VERSION;
838 packet->packet_type = BAT_VIS;
839 packet->ttl = TTL;
840 packet->seqno = 0;
841 packet->entries = 0;
843 INIT_LIST_HEAD(&bat_priv->vis_send_list);
845 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp,
846 bat_priv->my_vis_info);
847 if (hash_added < 0) {
848 pr_err("Can't add own vis packet into hash\n");
849 /* not in hash, need to remove it manually. */
850 kref_put(&bat_priv->my_vis_info->refcount, free_info);
851 goto err;
854 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
855 start_vis_timer(bat_priv);
856 return 1;
858 free_info:
859 kfree(bat_priv->my_vis_info);
860 bat_priv->my_vis_info = NULL;
861 err:
862 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
863 vis_quit(bat_priv);
864 return 0;
867 /* Decrease the reference count on a hash item info */
868 static void free_info_ref(void *data, void *arg)
870 struct vis_info *info = data;
872 send_list_del(info);
873 kref_put(&info->refcount, free_info);
876 /* shutdown vis-server */
877 void vis_quit(struct bat_priv *bat_priv)
879 unsigned long flags;
880 if (!bat_priv->vis_hash)
881 return;
883 cancel_delayed_work_sync(&bat_priv->vis_work);
885 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
886 /* properly remove, kill timers ... */
887 hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
888 bat_priv->vis_hash = NULL;
889 bat_priv->my_vis_info = NULL;
890 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
893 /* schedule packets for (re)transmission */
894 static void start_vis_timer(struct bat_priv *bat_priv)
896 INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
897 queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work,
898 msecs_to_jiffies(VIS_INTERVAL));