staging: batman-adv: Use linux/etherdevice.h address helper functions
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / batman-adv / vis.c
blob395f1109d606d7089091005ab168062a8b84eb93
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"
30 #define MAX_VIS_PACKET_SIZE 1000
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)))
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)
48 static void start_vis_timer(struct bat_priv *bat_priv);
50 /* free the info */
51 static void free_info(struct kref *ref)
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;
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);
65 spin_unlock_irqrestore(&bat_priv->vis_list_lock, flags);
66 kfree_skb(info->skb_packet);
69 /* Compare two vis packets, used by the hashing algorithm */
70 static int vis_info_cmp(void *data1, void *data2)
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);
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)
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;
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);
99 hash += (hash << 3);
100 hash ^= (hash >> 11);
101 hash += (hash << 15);
103 return hash % size;
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)
112 struct if_list_entry *entry;
113 struct hlist_node *pos;
115 hlist_for_each_entry(entry, pos, if_list, list) {
116 if (compare_orig(entry->addr, (void *)interface))
117 return;
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);
129 static ssize_t vis_data_read_prim_sec(char *buff, struct hlist_head *if_list)
131 struct if_list_entry *entry;
132 struct hlist_node *pos;
133 size_t len = 0;
135 hlist_for_each_entry(entry, pos, if_list, list) {
136 if (entry->primary)
137 len += sprintf(buff + len, "PRIMARY, ");
138 else
139 len += sprintf(buff + len, "SEC %pM, ", entry->addr);
142 return len;
145 static size_t vis_data_count_prim_sec(struct hlist_head *if_list)
147 struct if_list_entry *entry;
148 struct hlist_node *pos;
149 size_t count = 0;
151 hlist_for_each_entry(entry, pos, if_list, list) {
152 if (entry->primary)
153 count += 9;
154 else
155 count += 23;
158 return count;
161 /* read an entry */
162 static ssize_t vis_data_read_entry(char *buff, struct vis_info_entry *entry,
163 uint8_t *src, bool primary)
165 /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
166 if (primary && entry->quality == 0)
167 return sprintf(buff, "HNA %pM, ", entry->dest);
168 else if (compare_orig(entry->src, src))
169 return sprintf(buff, "TQ %pM %d, ", entry->dest,
170 entry->quality);
172 return 0;
175 int vis_seq_print_text(struct seq_file *seq, void *offset)
177 HASHIT(hashit);
178 HASHIT(hashit_count);
179 struct vis_info *info;
180 struct vis_packet *packet;
181 struct vis_info_entry *entries;
182 struct net_device *net_dev = (struct net_device *)seq->private;
183 struct bat_priv *bat_priv = netdev_priv(net_dev);
184 HLIST_HEAD(vis_if_list);
185 struct if_list_entry *entry;
186 struct hlist_node *pos, *n;
187 int i;
188 unsigned long flags;
189 int vis_server = atomic_read(&bat_priv->vis_mode);
190 size_t buff_pos, buf_size;
191 char *buff;
193 if ((!bat_priv->primary_if) ||
194 (vis_server == VIS_TYPE_CLIENT_UPDATE))
195 return 0;
197 buf_size = 1;
198 /* Estimate length */
199 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
200 while (hash_iterate(bat_priv->vis_hash, &hashit_count)) {
201 info = hashit_count.bucket->data;
202 packet = (struct vis_packet *)info->skb_packet->data;
203 entries = (struct vis_info_entry *)
204 ((char *)packet + sizeof(struct vis_packet));
206 for (i = 0; i < packet->entries; i++) {
207 if (entries[i].quality == 0)
208 continue;
209 vis_data_insert_interface(entries[i].src, &vis_if_list,
210 compare_orig(entries[i].src, packet->vis_orig));
213 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
214 buf_size += 18 + 26 * packet->entries;
216 /* add primary/secondary records */
217 if (compare_orig(entry->addr, packet->vis_orig))
218 buf_size +=
219 vis_data_count_prim_sec(&vis_if_list);
221 buf_size += 1;
224 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
225 hlist_del(&entry->list);
226 kfree(entry);
230 buff = kmalloc(buf_size, GFP_ATOMIC);
231 if (!buff) {
232 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
233 return -ENOMEM;
235 buff[0] = '\0';
236 buff_pos = 0;
238 while (hash_iterate(bat_priv->vis_hash, &hashit)) {
239 info = hashit.bucket->data;
240 packet = (struct vis_packet *)info->skb_packet->data;
241 entries = (struct vis_info_entry *)
242 ((char *)packet + sizeof(struct vis_packet));
244 for (i = 0; i < packet->entries; i++) {
245 if (entries[i].quality == 0)
246 continue;
247 vis_data_insert_interface(entries[i].src, &vis_if_list,
248 compare_orig(entries[i].src, packet->vis_orig));
251 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
252 buff_pos += sprintf(buff + buff_pos, "%pM,",
253 entry->addr);
255 for (i = 0; i < packet->entries; i++)
256 buff_pos += vis_data_read_entry(buff + buff_pos,
257 &entries[i],
258 entry->addr,
259 entry->primary);
261 /* add primary/secondary records */
262 if (compare_orig(entry->addr, packet->vis_orig))
263 buff_pos +=
264 vis_data_read_prim_sec(buff + buff_pos,
265 &vis_if_list);
267 buff_pos += sprintf(buff + buff_pos, "\n");
270 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
271 hlist_del(&entry->list);
272 kfree(entry);
276 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
278 seq_printf(seq, "%s", buff);
279 kfree(buff);
281 return 0;
284 /* add the info packet to the send list, if it was not
285 * already linked in. */
286 static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info)
288 if (list_empty(&info->send_list)) {
289 kref_get(&info->refcount);
290 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
294 /* delete the info packet from the send list, if it was
295 * linked in. */
296 static void send_list_del(struct vis_info *info)
298 if (!list_empty(&info->send_list)) {
299 list_del_init(&info->send_list);
300 kref_put(&info->refcount, free_info);
304 /* tries to add one entry to the receive list. */
305 static void recv_list_add(struct bat_priv *bat_priv,
306 struct list_head *recv_list, char *mac)
308 struct recvlist_node *entry;
309 unsigned long flags;
311 entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC);
312 if (!entry)
313 return;
315 memcpy(entry->mac, mac, ETH_ALEN);
316 spin_lock_irqsave(&bat_priv->vis_list_lock, flags);
317 list_add_tail(&entry->list, recv_list);
318 spin_unlock_irqrestore(&bat_priv->vis_list_lock, flags);
321 /* returns 1 if this mac is in the recv_list */
322 static int recv_list_is_in(struct bat_priv *bat_priv,
323 struct list_head *recv_list, char *mac)
325 struct recvlist_node *entry;
326 unsigned long flags;
328 spin_lock_irqsave(&bat_priv->vis_list_lock, flags);
329 list_for_each_entry(entry, recv_list, list) {
330 if (memcmp(entry->mac, mac, ETH_ALEN) == 0) {
331 spin_unlock_irqrestore(&bat_priv->vis_list_lock,
332 flags);
333 return 1;
336 spin_unlock_irqrestore(&bat_priv->vis_list_lock, flags);
337 return 0;
340 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
341 * broken.. ). vis hash must be locked outside. is_new is set when the packet
342 * is newer than old entries in the hash. */
343 static struct vis_info *add_packet(struct bat_priv *bat_priv,
344 struct vis_packet *vis_packet,
345 int vis_info_len, int *is_new,
346 int make_broadcast)
348 struct vis_info *info, *old_info;
349 struct vis_packet *search_packet, *old_packet;
350 struct vis_info search_elem;
351 struct vis_packet *packet;
353 *is_new = 0;
354 /* sanity check */
355 if (!bat_priv->vis_hash)
356 return NULL;
358 /* see if the packet is already in vis_hash */
359 search_elem.skb_packet = dev_alloc_skb(sizeof(struct vis_packet));
360 if (!search_elem.skb_packet)
361 return NULL;
362 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
363 sizeof(struct vis_packet));
365 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
366 old_info = hash_find(bat_priv->vis_hash, &search_elem);
367 kfree_skb(search_elem.skb_packet);
369 if (old_info != NULL) {
370 old_packet = (struct vis_packet *)old_info->skb_packet->data;
371 if (!seq_after(ntohl(vis_packet->seqno),
372 ntohl(old_packet->seqno))) {
373 if (old_packet->seqno == vis_packet->seqno) {
374 recv_list_add(bat_priv, &old_info->recv_list,
375 vis_packet->sender_orig);
376 return old_info;
377 } else {
378 /* newer packet is already in hash. */
379 return NULL;
382 /* remove old entry */
383 hash_remove(bat_priv->vis_hash, old_info);
384 send_list_del(old_info);
385 kref_put(&old_info->refcount, free_info);
388 info = kmalloc(sizeof(struct vis_info), GFP_ATOMIC);
389 if (!info)
390 return NULL;
392 info->skb_packet = dev_alloc_skb(sizeof(struct vis_packet) +
393 vis_info_len + sizeof(struct ethhdr));
394 if (!info->skb_packet) {
395 kfree(info);
396 return NULL;
398 skb_reserve(info->skb_packet, sizeof(struct ethhdr));
399 packet = (struct vis_packet *)skb_put(info->skb_packet,
400 sizeof(struct vis_packet) +
401 vis_info_len);
403 kref_init(&info->refcount);
404 INIT_LIST_HEAD(&info->send_list);
405 INIT_LIST_HEAD(&info->recv_list);
406 info->first_seen = jiffies;
407 info->bat_priv = bat_priv;
408 memcpy(packet, vis_packet, sizeof(struct vis_packet) + vis_info_len);
410 /* initialize and add new packet. */
411 *is_new = 1;
413 /* Make it a broadcast packet, if required */
414 if (make_broadcast)
415 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
417 /* repair if entries is longer than packet. */
418 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
419 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
421 recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
423 /* try to add it */
424 if (hash_add(bat_priv->vis_hash, info) < 0) {
425 /* did not work (for some reason) */
426 kref_put(&old_info->refcount, free_info);
427 info = NULL;
430 return info;
433 /* handle the server sync packet, forward if needed. */
434 void receive_server_sync_packet(struct bat_priv *bat_priv,
435 struct vis_packet *vis_packet,
436 int vis_info_len)
438 struct vis_info *info;
439 int is_new, make_broadcast;
440 unsigned long flags;
441 int vis_server = atomic_read(&bat_priv->vis_mode);
443 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
445 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
446 info = add_packet(bat_priv, vis_packet, vis_info_len,
447 &is_new, make_broadcast);
448 if (!info)
449 goto end;
451 /* only if we are server ourselves and packet is newer than the one in
452 * hash.*/
453 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
454 send_list_add(bat_priv, info);
455 end:
456 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
459 /* handle an incoming client update packet and schedule forward if needed. */
460 void receive_client_update_packet(struct bat_priv *bat_priv,
461 struct vis_packet *vis_packet,
462 int vis_info_len)
464 struct vis_info *info;
465 struct vis_packet *packet;
466 int is_new;
467 unsigned long flags;
468 int vis_server = atomic_read(&bat_priv->vis_mode);
469 int are_target = 0;
471 /* clients shall not broadcast. */
472 if (is_broadcast_ether_addr(vis_packet->target_orig))
473 return;
475 /* Are we the target for this VIS packet? */
476 if (vis_server == VIS_TYPE_SERVER_SYNC &&
477 is_my_mac(vis_packet->target_orig))
478 are_target = 1;
480 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
481 info = add_packet(bat_priv, vis_packet, vis_info_len,
482 &is_new, are_target);
484 if (!info)
485 goto end;
486 /* note that outdated packets will be dropped at this point. */
488 packet = (struct vis_packet *)info->skb_packet->data;
490 /* send only if we're the target server or ... */
491 if (are_target && is_new) {
492 packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
493 send_list_add(bat_priv, info);
495 /* ... we're not the recipient (and thus need to forward). */
496 } else if (!is_my_mac(packet->target_orig)) {
497 send_list_add(bat_priv, info);
500 end:
501 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
504 /* Walk the originators and find the VIS server with the best tq. Set the packet
505 * address to its address and return the best_tq.
507 * Must be called with the originator hash locked */
508 static int find_best_vis_server(struct bat_priv *bat_priv,
509 struct vis_info *info)
511 HASHIT(hashit);
512 struct orig_node *orig_node;
513 struct vis_packet *packet;
514 int best_tq = -1;
516 packet = (struct vis_packet *)info->skb_packet->data;
518 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
519 orig_node = hashit.bucket->data;
520 if ((orig_node) && (orig_node->router) &&
521 (orig_node->flags & VIS_SERVER) &&
522 (orig_node->router->tq_avg > best_tq)) {
523 best_tq = orig_node->router->tq_avg;
524 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
527 return best_tq;
530 /* Return true if the vis packet is full. */
531 static bool vis_packet_full(struct vis_info *info)
533 struct vis_packet *packet;
534 packet = (struct vis_packet *)info->skb_packet->data;
536 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
537 < packet->entries + 1)
538 return true;
539 return false;
542 /* generates a packet of own vis data,
543 * returns 0 on success, -1 if no packet could be generated */
544 static int generate_vis_packet(struct bat_priv *bat_priv)
546 HASHIT(hashit_local);
547 HASHIT(hashit_global);
548 struct orig_node *orig_node;
549 struct vis_info *info = (struct vis_info *)bat_priv->my_vis_info;
550 struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
551 struct vis_info_entry *entry;
552 struct hna_local_entry *hna_local_entry;
553 int best_tq = -1;
554 unsigned long flags;
556 info->first_seen = jiffies;
557 packet->vis_type = atomic_read(&bat_priv->vis_mode);
559 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
560 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
561 packet->ttl = TTL;
562 packet->seqno = htonl(ntohl(packet->seqno) + 1);
563 packet->entries = 0;
564 skb_trim(info->skb_packet, sizeof(struct vis_packet));
566 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
567 best_tq = find_best_vis_server(bat_priv, info);
569 if (best_tq < 0) {
570 spin_unlock_irqrestore(&bat_priv->orig_hash_lock,
571 flags);
572 return -1;
576 while (hash_iterate(bat_priv->orig_hash, &hashit_global)) {
577 orig_node = hashit_global.bucket->data;
579 if (!orig_node->router)
580 continue;
582 if (!compare_orig(orig_node->router->addr, orig_node->orig))
583 continue;
585 if (orig_node->router->if_incoming->if_status != IF_ACTIVE)
586 continue;
588 if (orig_node->router->tq_avg < 1)
589 continue;
591 /* fill one entry into buffer. */
592 entry = (struct vis_info_entry *)
593 skb_put(info->skb_packet, sizeof(*entry));
594 memcpy(entry->src,
595 orig_node->router->if_incoming->net_dev->dev_addr,
596 ETH_ALEN);
597 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
598 entry->quality = orig_node->router->tq_avg;
599 packet->entries++;
601 if (vis_packet_full(info)) {
602 spin_unlock_irqrestore(
603 &bat_priv->orig_hash_lock, flags);
604 return 0;
608 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
610 spin_lock_irqsave(&bat_priv->hna_lhash_lock, flags);
611 while (hash_iterate(bat_priv->hna_local_hash, &hashit_local)) {
612 hna_local_entry = hashit_local.bucket->data;
613 entry = (struct vis_info_entry *)skb_put(info->skb_packet,
614 sizeof(*entry));
615 memset(entry->src, 0, ETH_ALEN);
616 memcpy(entry->dest, hna_local_entry->addr, ETH_ALEN);
617 entry->quality = 0; /* 0 means HNA */
618 packet->entries++;
620 if (vis_packet_full(info)) {
621 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock,
622 flags);
623 return 0;
627 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
628 return 0;
631 /* free old vis packets. Must be called with this vis_hash_lock
632 * held */
633 static void purge_vis_packets(struct bat_priv *bat_priv)
635 HASHIT(hashit);
636 struct vis_info *info;
638 while (hash_iterate(bat_priv->vis_hash, &hashit)) {
639 info = hashit.bucket->data;
641 /* never purge own data. */
642 if (info == bat_priv->my_vis_info)
643 continue;
645 if (time_after(jiffies,
646 info->first_seen + VIS_TIMEOUT * HZ)) {
647 hash_remove_bucket(bat_priv->vis_hash, &hashit);
648 send_list_del(info);
649 kref_put(&info->refcount, free_info);
654 static void broadcast_vis_packet(struct bat_priv *bat_priv,
655 struct vis_info *info)
657 HASHIT(hashit);
658 struct orig_node *orig_node;
659 struct vis_packet *packet;
660 struct sk_buff *skb;
661 unsigned long flags;
662 struct batman_if *batman_if;
663 uint8_t dstaddr[ETH_ALEN];
666 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
667 packet = (struct vis_packet *)info->skb_packet->data;
669 /* send to all routers in range. */
670 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
671 orig_node = hashit.bucket->data;
673 /* if it's a vis server and reachable, send it. */
674 if ((!orig_node) || (!orig_node->router))
675 continue;
676 if (!(orig_node->flags & VIS_SERVER))
677 continue;
678 /* don't send it if we already received the packet from
679 * this node. */
680 if (recv_list_is_in(bat_priv, &info->recv_list,
681 orig_node->orig))
682 continue;
684 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
685 batman_if = orig_node->router->if_incoming;
686 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
687 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
689 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
690 if (skb)
691 send_skb_packet(skb, batman_if, dstaddr);
693 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
697 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
700 static void unicast_vis_packet(struct bat_priv *bat_priv,
701 struct vis_info *info)
703 struct orig_node *orig_node;
704 struct sk_buff *skb;
705 struct vis_packet *packet;
706 unsigned long flags;
707 struct batman_if *batman_if;
708 uint8_t dstaddr[ETH_ALEN];
710 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
711 packet = (struct vis_packet *)info->skb_packet->data;
712 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
713 packet->target_orig));
715 if ((!orig_node) || (!orig_node->router))
716 goto out;
718 /* don't lock while sending the packets ... we therefore
719 * copy the required data before sending */
720 batman_if = orig_node->router->if_incoming;
721 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
722 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
724 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
725 if (skb)
726 send_skb_packet(skb, batman_if, dstaddr);
728 return;
730 out:
731 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
734 /* only send one vis packet. called from send_vis_packets() */
735 static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
737 struct vis_packet *packet;
739 packet = (struct vis_packet *)info->skb_packet->data;
740 if (packet->ttl < 2) {
741 pr_debug("Error - can't send vis packet: ttl exceeded\n");
742 return;
745 memcpy(packet->sender_orig, bat_priv->primary_if->net_dev->dev_addr,
746 ETH_ALEN);
747 packet->ttl--;
749 if (is_broadcast_ether_addr(packet->target_orig))
750 broadcast_vis_packet(bat_priv, info);
751 else
752 unicast_vis_packet(bat_priv, info);
753 packet->ttl++; /* restore TTL */
756 /* called from timer; send (and maybe generate) vis packet. */
757 static void send_vis_packets(struct work_struct *work)
759 struct delayed_work *delayed_work =
760 container_of(work, struct delayed_work, work);
761 struct bat_priv *bat_priv =
762 container_of(delayed_work, struct bat_priv, vis_work);
763 struct vis_info *info, *temp;
764 unsigned long flags;
766 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
767 purge_vis_packets(bat_priv);
769 if (generate_vis_packet(bat_priv) == 0) {
770 /* schedule if generation was successful */
771 send_list_add(bat_priv, bat_priv->my_vis_info);
774 list_for_each_entry_safe(info, temp, &bat_priv->vis_send_list,
775 send_list) {
777 kref_get(&info->refcount);
778 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
780 if (bat_priv->primary_if)
781 send_vis_packet(bat_priv, info);
783 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
784 send_list_del(info);
785 kref_put(&info->refcount, free_info);
787 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
788 start_vis_timer(bat_priv);
791 /* init the vis server. this may only be called when if_list is already
792 * initialized (e.g. bat0 is initialized, interfaces have been added) */
793 int vis_init(struct bat_priv *bat_priv)
795 struct vis_packet *packet;
796 unsigned long flags;
798 if (bat_priv->vis_hash)
799 return 1;
801 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
803 bat_priv->vis_hash = hash_new(256, vis_info_cmp, vis_info_choose);
804 if (!bat_priv->vis_hash) {
805 pr_err("Can't initialize vis_hash\n");
806 goto err;
809 bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
810 if (!bat_priv->my_vis_info) {
811 pr_err("Can't initialize vis packet\n");
812 goto err;
815 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(
816 sizeof(struct vis_packet) +
817 MAX_VIS_PACKET_SIZE +
818 sizeof(struct ethhdr));
819 if (!bat_priv->my_vis_info->skb_packet)
820 goto free_info;
822 skb_reserve(bat_priv->my_vis_info->skb_packet, sizeof(struct ethhdr));
823 packet = (struct vis_packet *)skb_put(
824 bat_priv->my_vis_info->skb_packet,
825 sizeof(struct vis_packet));
827 /* prefill the vis info */
828 bat_priv->my_vis_info->first_seen = jiffies -
829 msecs_to_jiffies(VIS_INTERVAL);
830 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
831 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
832 kref_init(&bat_priv->my_vis_info->refcount);
833 bat_priv->my_vis_info->bat_priv = bat_priv;
834 packet->version = COMPAT_VERSION;
835 packet->packet_type = BAT_VIS;
836 packet->ttl = TTL;
837 packet->seqno = 0;
838 packet->entries = 0;
840 INIT_LIST_HEAD(&bat_priv->vis_send_list);
842 if (hash_add(bat_priv->vis_hash, bat_priv->my_vis_info) < 0) {
843 pr_err("Can't add own vis packet into hash\n");
844 /* not in hash, need to remove it manually. */
845 kref_put(&bat_priv->my_vis_info->refcount, free_info);
846 goto err;
849 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
850 start_vis_timer(bat_priv);
851 return 1;
853 free_info:
854 kfree(bat_priv->my_vis_info);
855 bat_priv->my_vis_info = NULL;
856 err:
857 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
858 vis_quit(bat_priv);
859 return 0;
862 /* Decrease the reference count on a hash item info */
863 static void free_info_ref(void *data, void *arg)
865 struct vis_info *info = data;
867 send_list_del(info);
868 kref_put(&info->refcount, free_info);
871 /* shutdown vis-server */
872 void vis_quit(struct bat_priv *bat_priv)
874 unsigned long flags;
875 if (!bat_priv->vis_hash)
876 return;
878 cancel_delayed_work_sync(&bat_priv->vis_work);
880 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
881 /* properly remove, kill timers ... */
882 hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
883 bat_priv->vis_hash = NULL;
884 bat_priv->my_vis_info = NULL;
885 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
888 /* schedule packets for (re)transmission */
889 static void start_vis_timer(struct bat_priv *bat_priv)
891 INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
892 queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work,
893 msecs_to_jiffies(VIS_INTERVAL));