2 * Copyright (C) 2008-2010 B.A.T.M.A.N. contributors:
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
24 #include "translation-table.h"
26 #include "soft-interface.h"
27 #include "hard-interface.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);
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;
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);
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;
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;
91 packet = (struct vis_packet *)vis_info->skb_packet->data;
92 key = packet->vis_orig;
93 for (i = 0; i < ETH_ALEN; i++) {
100 hash ^= (hash >> 11);
101 hash += (hash << 15);
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,
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))
120 /* its a new address, add it to the list */
121 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
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 char tmp_addr_str[ETH_STR_LEN];
136 hlist_for_each_entry(entry, pos, if_list, list) {
138 len += sprintf(buff + len, "PRIMARY, ");
140 addr_to_string(tmp_addr_str, entry->addr);
141 len += sprintf(buff + len, "SEC %s, ", tmp_addr_str);
148 static size_t vis_data_count_prim_sec(struct hlist_head *if_list)
150 struct if_list_entry *entry;
151 struct hlist_node *pos;
154 hlist_for_each_entry(entry, pos, if_list, list) {
165 static ssize_t vis_data_read_entry(char *buff, struct vis_info_entry *entry,
166 uint8_t *src, bool primary)
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);
180 int vis_seq_print_text(struct seq_file *seq, void *offset)
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;
193 char tmp_addr_str[ETH_STR_LEN];
195 int vis_server = atomic_read(&bat_priv->vis_mode);
196 size_t buff_pos, buf_size;
199 if ((!bat_priv->primary_if) ||
200 (vis_server == VIS_TYPE_CLIENT_UPDATE))
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));
212 for (i = 0; i < packet->entries; i++) {
213 if (entries[i].quality == 0)
215 vis_data_insert_interface(entries[i].src, &vis_if_list,
216 compare_orig(entries[i].src, packet->vis_orig));
219 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
220 buf_size += 18 + 26 * packet->entries;
222 /* add primary/secondary records */
223 if (compare_orig(entry->addr, packet->vis_orig))
225 vis_data_count_prim_sec(&vis_if_list);
230 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
231 hlist_del(&entry->list);
236 buff = kmalloc(buf_size, GFP_ATOMIC);
238 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
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));
250 for (i = 0; i < packet->entries; i++) {
251 if (entries[i].quality == 0)
253 vis_data_insert_interface(entries[i].src, &vis_if_list,
254 compare_orig(entries[i].src, packet->vis_orig));
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,",
262 for (i = 0; i < packet->entries; i++)
263 buff_pos += vis_data_read_entry(buff + buff_pos,
268 /* add primary/secondary records */
269 if (compare_orig(entry->addr, packet->vis_orig))
271 vis_data_read_prim_sec(buff + buff_pos,
274 buff_pos += sprintf(buff + buff_pos, "\n");
277 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
278 hlist_del(&entry->list);
283 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
285 seq_printf(seq, "%s", buff);
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)
295 if (list_empty(&info->send_list)) {
296 kref_get(&info->refcount);
297 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
301 /* delete the info packet from the send list, if it was
303 static void send_list_del(struct vis_info *info)
305 if (!list_empty(&info->send_list)) {
306 list_del_init(&info->send_list);
307 kref_put(&info->refcount, free_info);
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)
315 struct recvlist_node *entry;
318 entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC);
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);
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)
332 struct recvlist_node *entry;
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,
343 spin_unlock_irqrestore(&bat_priv->vis_list_lock, flags);
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,
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;
362 if (!bat_priv->vis_hash)
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)
369 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
370 sizeof(struct vis_packet));
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);
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);
385 /* newer packet is already in hash. */
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);
395 info = kmalloc(sizeof(struct vis_info), GFP_ATOMIC);
399 info->skb_packet = dev_alloc_skb(sizeof(struct vis_packet) +
400 vis_info_len + sizeof(struct ethhdr));
401 if (!info->skb_packet) {
405 skb_reserve(info->skb_packet, sizeof(struct ethhdr));
406 packet = (struct vis_packet *)skb_put(info->skb_packet,
407 sizeof(struct vis_packet) +
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);
417 /* initialize and add new packet. */
420 /* Make it a broadcast packet, if required */
422 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
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);
428 recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
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);
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,
445 struct vis_info *info;
446 int is_new, make_broadcast;
448 int vis_server = atomic_read(&bat_priv->vis_mode);
450 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
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);
458 /* only if we are server ourselves and packet is newer than the one in
460 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
461 send_list_add(bat_priv, info);
463 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
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,
471 struct vis_info *info;
472 struct vis_packet *packet;
475 int vis_server = atomic_read(&bat_priv->vis_mode);
478 /* clients shall not broadcast. */
479 if (is_bcast(vis_packet->target_orig))
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))
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);
493 /* note that outdated packets will be dropped at this point. */
495 packet = (struct vis_packet *)info->skb_packet->data;
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);
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);
508 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
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.
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)
519 struct orig_node *orig_node;
520 struct vis_packet *packet;
523 packet = (struct vis_packet *)info->skb_packet->data;
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);
537 /* Return true if the vis packet is full. */
538 static bool vis_packet_full(struct vis_info *info)
540 struct vis_packet *packet;
541 packet = (struct vis_packet *)info->skb_packet->data;
543 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
544 < packet->entries + 1)
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)
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;
563 info->first_seen = jiffies;
564 packet->vis_type = atomic_read(&bat_priv->vis_mode);
566 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
567 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
569 packet->seqno = htonl(ntohl(packet->seqno) + 1);
571 skb_trim(info->skb_packet, sizeof(struct vis_packet));
573 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
574 best_tq = find_best_vis_server(bat_priv, info);
577 spin_unlock_irqrestore(&bat_priv->orig_hash_lock,
583 while (hash_iterate(bat_priv->orig_hash, &hashit_global)) {
584 orig_node = hashit_global.bucket->data;
586 if (!orig_node->router)
589 if (!compare_orig(orig_node->router->addr, orig_node->orig))
592 if (orig_node->router->if_incoming->if_status != IF_ACTIVE)
595 if (orig_node->router->tq_avg < 1)
598 /* fill one entry into buffer. */
599 entry = (struct vis_info_entry *)
600 skb_put(info->skb_packet, sizeof(*entry));
602 orig_node->router->if_incoming->net_dev->dev_addr,
604 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
605 entry->quality = orig_node->router->tq_avg;
608 if (vis_packet_full(info)) {
609 spin_unlock_irqrestore(
610 &bat_priv->orig_hash_lock, flags);
615 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
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,
622 memset(entry->src, 0, ETH_ALEN);
623 memcpy(entry->dest, hna_local_entry->addr, ETH_ALEN);
624 entry->quality = 0; /* 0 means HNA */
627 if (vis_packet_full(info)) {
628 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock,
634 spin_unlock_irqrestore(&bat_priv->hna_lhash_lock, flags);
638 /* free old vis packets. Must be called with this vis_hash_lock
640 static void purge_vis_packets(struct bat_priv *bat_priv)
643 struct vis_info *info;
645 while (hash_iterate(bat_priv->vis_hash, &hashit)) {
646 info = hashit.bucket->data;
648 /* never purge own data. */
649 if (info == bat_priv->my_vis_info)
652 if (time_after(jiffies,
653 info->first_seen + VIS_TIMEOUT * HZ)) {
654 hash_remove_bucket(bat_priv->vis_hash, &hashit);
656 kref_put(&info->refcount, free_info);
661 static void broadcast_vis_packet(struct bat_priv *bat_priv,
662 struct vis_info *info)
665 struct orig_node *orig_node;
666 struct vis_packet *packet;
669 struct batman_if *batman_if;
670 uint8_t dstaddr[ETH_ALEN];
673 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
674 packet = (struct vis_packet *)info->skb_packet->data;
676 /* send to all routers in range. */
677 while (hash_iterate(bat_priv->orig_hash, &hashit)) {
678 orig_node = hashit.bucket->data;
680 /* if it's a vis server and reachable, send it. */
681 if ((!orig_node) || (!orig_node->router))
683 if (!(orig_node->flags & VIS_SERVER))
685 /* don't send it if we already received the packet from
687 if (recv_list_is_in(bat_priv, &info->recv_list,
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);
696 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
698 send_skb_packet(skb, batman_if, dstaddr);
700 spin_lock_irqsave(&bat_priv->orig_hash_lock, flags);
704 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
707 static void unicast_vis_packet(struct bat_priv *bat_priv,
708 struct vis_info *info)
710 struct orig_node *orig_node;
712 struct vis_packet *packet;
714 struct batman_if *batman_if;
715 uint8_t dstaddr[ETH_ALEN];
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));
722 if ((!orig_node) || (!orig_node->router))
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);
731 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
733 send_skb_packet(skb, batman_if, dstaddr);
738 spin_unlock_irqrestore(&bat_priv->orig_hash_lock, flags);
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)
744 struct vis_packet *packet;
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");
752 memcpy(packet->sender_orig, bat_priv->primary_if->net_dev->dev_addr,
756 if (is_bcast(packet->target_orig))
757 broadcast_vis_packet(bat_priv, info);
759 unicast_vis_packet(bat_priv, info);
760 packet->ttl++; /* restore TTL */
763 /* called from timer; send (and maybe generate) vis packet. */
764 static void send_vis_packets(struct work_struct *work)
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;
773 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
774 purge_vis_packets(bat_priv);
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);
781 list_for_each_entry_safe(info, temp, &bat_priv->vis_send_list,
784 kref_get(&info->refcount);
785 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
787 if (bat_priv->primary_if)
788 send_vis_packet(bat_priv, info);
790 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
792 kref_put(&info->refcount, free_info);
794 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
795 start_vis_timer(bat_priv);
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)
802 struct vis_packet *packet;
805 if (bat_priv->vis_hash)
808 spin_lock_irqsave(&bat_priv->vis_hash_lock, flags);
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");
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");
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)
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));
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;
847 INIT_LIST_HEAD(&bat_priv->vis_send_list);
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);
856 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
857 start_vis_timer(bat_priv);
861 kfree(bat_priv->my_vis_info);
862 bat_priv->my_vis_info = NULL;
864 spin_unlock_irqrestore(&bat_priv->vis_hash_lock, flags);
869 /* Decrease the reference count on a hash item info */
870 static void free_info_ref(void *data, void *arg)
872 struct vis_info *info = data;
875 kref_put(&info->refcount, free_info);
878 /* shutdown vis-server */
879 void vis_quit(struct bat_priv *bat_priv)
882 if (!bat_priv->vis_hash)
885 cancel_delayed_work_sync(&bat_priv->vis_work);
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);
895 /* schedule packets for (re)transmission */
896 static void start_vis_timer(struct bat_priv *bat_priv)
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));