2 * Copyright (C) 2008-2011 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"
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);
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;
58 list_del_init(&info->send_list);
59 spin_lock_bh(&bat_priv->vis_list_lock);
60 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
61 list_del(&entry->list);
65 spin_unlock_bh(&bat_priv->vis_list_lock);
66 kfree_skb(info->skb_packet);
70 /* Compare two vis packets, used by the hashing algorithm */
71 static int vis_info_cmp(const struct hlist_node *node, const void *data2)
73 const struct vis_info *d1, *d2;
74 const struct vis_packet *p1, *p2;
76 d1 = container_of(node, struct vis_info, hash_entry);
78 p1 = (struct vis_packet *)d1->skb_packet->data;
79 p2 = (struct vis_packet *)d2->skb_packet->data;
80 return compare_eth(p1->vis_orig, p2->vis_orig);
83 /* hash function to choose an entry in a hash table of given size */
84 /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
85 static int vis_info_choose(const void *data, int size)
87 const struct vis_info *vis_info = data;
88 const struct vis_packet *packet;
89 const unsigned char *key;
93 packet = (struct vis_packet *)vis_info->skb_packet->data;
94 key = packet->vis_orig;
95 for (i = 0; i < ETH_ALEN; i++) {
102 hash ^= (hash >> 11);
103 hash += (hash << 15);
108 static struct vis_info *vis_hash_find(struct bat_priv *bat_priv,
111 struct hashtable_t *hash = bat_priv->vis_hash;
112 struct hlist_head *head;
113 struct hlist_node *node;
114 struct vis_info *vis_info, *vis_info_tmp = NULL;
120 index = vis_info_choose(data, hash->size);
121 head = &hash->table[index];
124 hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
125 if (!vis_info_cmp(node, data))
128 vis_info_tmp = vis_info;
136 /* insert interface to the list of interfaces of one originator, if it
137 * does not already exist in the list */
138 static void vis_data_insert_interface(const uint8_t *interface,
139 struct hlist_head *if_list,
142 struct if_list_entry *entry;
143 struct hlist_node *pos;
145 hlist_for_each_entry(entry, pos, if_list, list) {
146 if (compare_eth(entry->addr, interface))
150 /* its a new address, add it to the list */
151 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
154 memcpy(entry->addr, interface, ETH_ALEN);
155 entry->primary = primary;
156 hlist_add_head(&entry->list, if_list);
159 static ssize_t vis_data_read_prim_sec(char *buff,
160 const struct hlist_head *if_list)
162 struct if_list_entry *entry;
163 struct hlist_node *pos;
166 hlist_for_each_entry(entry, pos, if_list, list) {
168 len += sprintf(buff + len, "PRIMARY, ");
170 len += sprintf(buff + len, "SEC %pM, ", entry->addr);
176 static size_t vis_data_count_prim_sec(struct hlist_head *if_list)
178 struct if_list_entry *entry;
179 struct hlist_node *pos;
182 hlist_for_each_entry(entry, pos, if_list, list) {
193 static ssize_t vis_data_read_entry(char *buff,
194 const struct vis_info_entry *entry,
195 const uint8_t *src, bool primary)
197 /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
198 if (primary && entry->quality == 0)
199 return sprintf(buff, "TT %pM, ", entry->dest);
200 else if (compare_eth(entry->src, src))
201 return sprintf(buff, "TQ %pM %d, ", entry->dest,
207 int vis_seq_print_text(struct seq_file *seq, void *offset)
209 struct hard_iface *primary_if;
210 struct hlist_node *node;
211 struct hlist_head *head;
212 struct vis_info *info;
213 struct vis_packet *packet;
214 struct vis_info_entry *entries;
215 struct net_device *net_dev = (struct net_device *)seq->private;
216 struct bat_priv *bat_priv = netdev_priv(net_dev);
217 struct hashtable_t *hash = bat_priv->vis_hash;
218 HLIST_HEAD(vis_if_list);
219 struct if_list_entry *entry;
220 struct hlist_node *pos, *n;
222 int vis_server = atomic_read(&bat_priv->vis_mode);
223 size_t buff_pos, buf_size;
227 primary_if = primary_if_get_selected(bat_priv);
231 if (vis_server == VIS_TYPE_CLIENT_UPDATE)
235 /* Estimate length */
236 spin_lock_bh(&bat_priv->vis_hash_lock);
237 for (i = 0; i < hash->size; i++) {
238 head = &hash->table[i];
241 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
242 packet = (struct vis_packet *)info->skb_packet->data;
243 entries = (struct vis_info_entry *)
244 ((char *)packet + sizeof(*packet));
246 for (j = 0; j < packet->entries; j++) {
247 if (entries[j].quality == 0)
250 compare_eth(entries[j].src, packet->vis_orig);
251 vis_data_insert_interface(entries[j].src,
256 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
257 buf_size += 18 + 26 * packet->entries;
259 /* add primary/secondary records */
260 if (compare_eth(entry->addr, packet->vis_orig))
262 vis_data_count_prim_sec(&vis_if_list);
267 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
269 hlist_del(&entry->list);
276 buff = kmalloc(buf_size, GFP_ATOMIC);
278 spin_unlock_bh(&bat_priv->vis_hash_lock);
285 for (i = 0; i < hash->size; i++) {
286 head = &hash->table[i];
289 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
290 packet = (struct vis_packet *)info->skb_packet->data;
291 entries = (struct vis_info_entry *)
292 ((char *)packet + sizeof(*packet));
294 for (j = 0; j < packet->entries; j++) {
295 if (entries[j].quality == 0)
298 compare_eth(entries[j].src, packet->vis_orig);
299 vis_data_insert_interface(entries[j].src,
304 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
305 buff_pos += sprintf(buff + buff_pos, "%pM,",
308 for (j = 0; j < packet->entries; j++)
309 buff_pos += vis_data_read_entry(
315 /* add primary/secondary records */
316 if (compare_eth(entry->addr, packet->vis_orig))
318 vis_data_read_prim_sec(buff + buff_pos,
321 buff_pos += sprintf(buff + buff_pos, "\n");
324 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
326 hlist_del(&entry->list);
333 spin_unlock_bh(&bat_priv->vis_hash_lock);
335 seq_printf(seq, "%s", buff);
340 hardif_free_ref(primary_if);
344 /* add the info packet to the send list, if it was not
345 * already linked in. */
346 static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info)
348 if (list_empty(&info->send_list)) {
349 kref_get(&info->refcount);
350 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
354 /* delete the info packet from the send list, if it was
356 static void send_list_del(struct vis_info *info)
358 if (!list_empty(&info->send_list)) {
359 list_del_init(&info->send_list);
360 kref_put(&info->refcount, free_info);
364 /* tries to add one entry to the receive list. */
365 static void recv_list_add(struct bat_priv *bat_priv,
366 struct list_head *recv_list, const char *mac)
368 struct recvlist_node *entry;
370 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
374 memcpy(entry->mac, mac, ETH_ALEN);
375 spin_lock_bh(&bat_priv->vis_list_lock);
376 list_add_tail(&entry->list, recv_list);
377 spin_unlock_bh(&bat_priv->vis_list_lock);
380 /* returns 1 if this mac is in the recv_list */
381 static int recv_list_is_in(struct bat_priv *bat_priv,
382 const struct list_head *recv_list, const char *mac)
384 const struct recvlist_node *entry;
386 spin_lock_bh(&bat_priv->vis_list_lock);
387 list_for_each_entry(entry, recv_list, list) {
388 if (compare_eth(entry->mac, mac)) {
389 spin_unlock_bh(&bat_priv->vis_list_lock);
393 spin_unlock_bh(&bat_priv->vis_list_lock);
397 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
398 * broken.. ). vis hash must be locked outside. is_new is set when the packet
399 * is newer than old entries in the hash. */
400 static struct vis_info *add_packet(struct bat_priv *bat_priv,
401 struct vis_packet *vis_packet,
402 int vis_info_len, int *is_new,
405 struct vis_info *info, *old_info;
406 struct vis_packet *search_packet, *old_packet;
407 struct vis_info search_elem;
408 struct vis_packet *packet;
413 if (!bat_priv->vis_hash)
416 /* see if the packet is already in vis_hash */
417 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
418 if (!search_elem.skb_packet)
420 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
421 sizeof(*search_packet));
423 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
424 old_info = vis_hash_find(bat_priv, &search_elem);
425 kfree_skb(search_elem.skb_packet);
428 old_packet = (struct vis_packet *)old_info->skb_packet->data;
429 if (!seq_after(ntohl(vis_packet->seqno),
430 ntohl(old_packet->seqno))) {
431 if (old_packet->seqno == vis_packet->seqno) {
432 recv_list_add(bat_priv, &old_info->recv_list,
433 vis_packet->sender_orig);
436 /* newer packet is already in hash. */
440 /* remove old entry */
441 hash_remove(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
443 send_list_del(old_info);
444 kref_put(&old_info->refcount, free_info);
447 info = kmalloc(sizeof(*info), GFP_ATOMIC);
451 info->skb_packet = dev_alloc_skb(sizeof(*packet) + vis_info_len +
452 sizeof(struct ethhdr));
453 if (!info->skb_packet) {
457 skb_reserve(info->skb_packet, sizeof(struct ethhdr));
458 packet = (struct vis_packet *)skb_put(info->skb_packet, sizeof(*packet)
461 kref_init(&info->refcount);
462 INIT_LIST_HEAD(&info->send_list);
463 INIT_LIST_HEAD(&info->recv_list);
464 info->first_seen = jiffies;
465 info->bat_priv = bat_priv;
466 memcpy(packet, vis_packet, sizeof(*packet) + vis_info_len);
468 /* initialize and add new packet. */
471 /* Make it a broadcast packet, if required */
473 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
475 /* repair if entries is longer than packet. */
476 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
477 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
479 recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
482 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
483 info, &info->hash_entry);
484 if (hash_added < 0) {
485 /* did not work (for some reason) */
486 kref_put(&info->refcount, free_info);
493 /* handle the server sync packet, forward if needed. */
494 void receive_server_sync_packet(struct bat_priv *bat_priv,
495 struct vis_packet *vis_packet,
498 struct vis_info *info;
499 int is_new, make_broadcast;
500 int vis_server = atomic_read(&bat_priv->vis_mode);
502 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
504 spin_lock_bh(&bat_priv->vis_hash_lock);
505 info = add_packet(bat_priv, vis_packet, vis_info_len,
506 &is_new, make_broadcast);
510 /* only if we are server ourselves and packet is newer than the one in
512 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
513 send_list_add(bat_priv, info);
515 spin_unlock_bh(&bat_priv->vis_hash_lock);
518 /* handle an incoming client update packet and schedule forward if needed. */
519 void receive_client_update_packet(struct bat_priv *bat_priv,
520 struct vis_packet *vis_packet,
523 struct vis_info *info;
524 struct vis_packet *packet;
526 int vis_server = atomic_read(&bat_priv->vis_mode);
529 /* clients shall not broadcast. */
530 if (is_broadcast_ether_addr(vis_packet->target_orig))
533 /* Are we the target for this VIS packet? */
534 if (vis_server == VIS_TYPE_SERVER_SYNC &&
535 is_my_mac(vis_packet->target_orig))
538 spin_lock_bh(&bat_priv->vis_hash_lock);
539 info = add_packet(bat_priv, vis_packet, vis_info_len,
540 &is_new, are_target);
544 /* note that outdated packets will be dropped at this point. */
546 packet = (struct vis_packet *)info->skb_packet->data;
548 /* send only if we're the target server or ... */
549 if (are_target && is_new) {
550 packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
551 send_list_add(bat_priv, info);
553 /* ... we're not the recipient (and thus need to forward). */
554 } else if (!is_my_mac(packet->target_orig)) {
555 send_list_add(bat_priv, info);
559 spin_unlock_bh(&bat_priv->vis_hash_lock);
562 /* Walk the originators and find the VIS server with the best tq. Set the packet
563 * address to its address and return the best_tq.
565 * Must be called with the originator hash locked */
566 static int find_best_vis_server(struct bat_priv *bat_priv,
567 struct vis_info *info)
569 struct hashtable_t *hash = bat_priv->orig_hash;
570 struct neigh_node *router;
571 struct hlist_node *node;
572 struct hlist_head *head;
573 struct orig_node *orig_node;
574 struct vis_packet *packet;
577 packet = (struct vis_packet *)info->skb_packet->data;
579 for (i = 0; i < hash->size; i++) {
580 head = &hash->table[i];
583 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
584 router = orig_node_get_router(orig_node);
588 if ((orig_node->flags & VIS_SERVER) &&
589 (router->tq_avg > best_tq)) {
590 best_tq = router->tq_avg;
591 memcpy(packet->target_orig, orig_node->orig,
594 neigh_node_free_ref(router);
602 /* Return true if the vis packet is full. */
603 static bool vis_packet_full(const struct vis_info *info)
605 const struct vis_packet *packet;
606 packet = (struct vis_packet *)info->skb_packet->data;
608 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
609 < packet->entries + 1)
614 /* generates a packet of own vis data,
615 * returns 0 on success, -1 if no packet could be generated */
616 static int generate_vis_packet(struct bat_priv *bat_priv)
618 struct hashtable_t *hash = bat_priv->orig_hash;
619 struct hlist_node *node;
620 struct hlist_head *head;
621 struct orig_node *orig_node;
622 struct neigh_node *router;
623 struct vis_info *info = bat_priv->my_vis_info;
624 struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
625 struct vis_info_entry *entry;
626 struct tt_local_entry *tt_local_entry;
629 info->first_seen = jiffies;
630 packet->vis_type = atomic_read(&bat_priv->vis_mode);
632 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
634 packet->seqno = htonl(ntohl(packet->seqno) + 1);
636 skb_trim(info->skb_packet, sizeof(*packet));
638 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
639 best_tq = find_best_vis_server(bat_priv, info);
645 for (i = 0; i < hash->size; i++) {
646 head = &hash->table[i];
649 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
650 router = orig_node_get_router(orig_node);
654 if (!compare_eth(router->addr, orig_node->orig))
657 if (router->if_incoming->if_status != IF_ACTIVE)
660 if (router->tq_avg < 1)
663 /* fill one entry into buffer. */
664 entry = (struct vis_info_entry *)
665 skb_put(info->skb_packet, sizeof(*entry));
667 router->if_incoming->net_dev->dev_addr,
669 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
670 entry->quality = router->tq_avg;
674 neigh_node_free_ref(router);
676 if (vis_packet_full(info))
682 hash = bat_priv->tt_local_hash;
684 spin_lock_bh(&bat_priv->tt_lhash_lock);
685 for (i = 0; i < hash->size; i++) {
686 head = &hash->table[i];
688 hlist_for_each_entry(tt_local_entry, node, head, hash_entry) {
689 entry = (struct vis_info_entry *)
690 skb_put(info->skb_packet,
692 memset(entry->src, 0, ETH_ALEN);
693 memcpy(entry->dest, tt_local_entry->addr, ETH_ALEN);
694 entry->quality = 0; /* 0 means TT */
697 if (vis_packet_full(info)) {
698 spin_unlock_bh(&bat_priv->tt_lhash_lock);
704 spin_unlock_bh(&bat_priv->tt_lhash_lock);
712 /* free old vis packets. Must be called with this vis_hash_lock
714 static void purge_vis_packets(struct bat_priv *bat_priv)
717 struct hashtable_t *hash = bat_priv->vis_hash;
718 struct hlist_node *node, *node_tmp;
719 struct hlist_head *head;
720 struct vis_info *info;
722 for (i = 0; i < hash->size; i++) {
723 head = &hash->table[i];
725 hlist_for_each_entry_safe(info, node, node_tmp,
727 /* never purge own data. */
728 if (info == bat_priv->my_vis_info)
731 if (time_after(jiffies,
732 info->first_seen + VIS_TIMEOUT * HZ)) {
735 kref_put(&info->refcount, free_info);
741 static void broadcast_vis_packet(struct bat_priv *bat_priv,
742 struct vis_info *info)
744 struct neigh_node *router;
745 struct hashtable_t *hash = bat_priv->orig_hash;
746 struct hlist_node *node;
747 struct hlist_head *head;
748 struct orig_node *orig_node;
749 struct vis_packet *packet;
751 struct hard_iface *hard_iface;
752 uint8_t dstaddr[ETH_ALEN];
756 packet = (struct vis_packet *)info->skb_packet->data;
758 /* send to all routers in range. */
759 for (i = 0; i < hash->size; i++) {
760 head = &hash->table[i];
763 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
764 /* if it's a vis server and reachable, send it. */
765 if (!(orig_node->flags & VIS_SERVER))
768 router = orig_node_get_router(orig_node);
772 /* don't send it if we already received the packet from
774 if (recv_list_is_in(bat_priv, &info->recv_list,
776 neigh_node_free_ref(router);
780 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
781 hard_iface = router->if_incoming;
782 memcpy(dstaddr, router->addr, ETH_ALEN);
784 neigh_node_free_ref(router);
786 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
788 send_skb_packet(skb, hard_iface, dstaddr);
795 static void unicast_vis_packet(struct bat_priv *bat_priv,
796 struct vis_info *info)
798 struct orig_node *orig_node;
799 struct neigh_node *router = NULL;
801 struct vis_packet *packet;
803 packet = (struct vis_packet *)info->skb_packet->data;
805 orig_node = orig_hash_find(bat_priv, packet->target_orig);
809 router = orig_node_get_router(orig_node);
813 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
815 send_skb_packet(skb, router->if_incoming, router->addr);
819 neigh_node_free_ref(router);
821 orig_node_free_ref(orig_node);
824 /* only send one vis packet. called from send_vis_packets() */
825 static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
827 struct hard_iface *primary_if;
828 struct vis_packet *packet;
830 primary_if = primary_if_get_selected(bat_priv);
834 packet = (struct vis_packet *)info->skb_packet->data;
835 if (packet->ttl < 2) {
836 pr_debug("Error - can't send vis packet: ttl exceeded\n");
840 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
843 if (is_broadcast_ether_addr(packet->target_orig))
844 broadcast_vis_packet(bat_priv, info);
846 unicast_vis_packet(bat_priv, info);
847 packet->ttl++; /* restore TTL */
851 hardif_free_ref(primary_if);
854 /* called from timer; send (and maybe generate) vis packet. */
855 static void send_vis_packets(struct work_struct *work)
857 struct delayed_work *delayed_work =
858 container_of(work, struct delayed_work, work);
859 struct bat_priv *bat_priv =
860 container_of(delayed_work, struct bat_priv, vis_work);
861 struct vis_info *info;
863 spin_lock_bh(&bat_priv->vis_hash_lock);
864 purge_vis_packets(bat_priv);
866 if (generate_vis_packet(bat_priv) == 0) {
867 /* schedule if generation was successful */
868 send_list_add(bat_priv, bat_priv->my_vis_info);
871 while (!list_empty(&bat_priv->vis_send_list)) {
872 info = list_first_entry(&bat_priv->vis_send_list,
873 typeof(*info), send_list);
875 kref_get(&info->refcount);
876 spin_unlock_bh(&bat_priv->vis_hash_lock);
878 send_vis_packet(bat_priv, info);
880 spin_lock_bh(&bat_priv->vis_hash_lock);
882 kref_put(&info->refcount, free_info);
884 spin_unlock_bh(&bat_priv->vis_hash_lock);
885 start_vis_timer(bat_priv);
888 /* init the vis server. this may only be called when if_list is already
889 * initialized (e.g. bat0 is initialized, interfaces have been added) */
890 int vis_init(struct bat_priv *bat_priv)
892 struct vis_packet *packet;
895 if (bat_priv->vis_hash)
898 spin_lock_bh(&bat_priv->vis_hash_lock);
900 bat_priv->vis_hash = hash_new(256);
901 if (!bat_priv->vis_hash) {
902 pr_err("Can't initialize vis_hash\n");
906 bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
907 if (!bat_priv->my_vis_info) {
908 pr_err("Can't initialize vis packet\n");
912 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(sizeof(*packet) +
913 MAX_VIS_PACKET_SIZE +
914 sizeof(struct ethhdr));
915 if (!bat_priv->my_vis_info->skb_packet)
918 skb_reserve(bat_priv->my_vis_info->skb_packet, sizeof(struct ethhdr));
919 packet = (struct vis_packet *)skb_put(bat_priv->my_vis_info->skb_packet,
922 /* prefill the vis info */
923 bat_priv->my_vis_info->first_seen = jiffies -
924 msecs_to_jiffies(VIS_INTERVAL);
925 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
926 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
927 kref_init(&bat_priv->my_vis_info->refcount);
928 bat_priv->my_vis_info->bat_priv = bat_priv;
929 packet->version = COMPAT_VERSION;
930 packet->packet_type = BAT_VIS;
935 INIT_LIST_HEAD(&bat_priv->vis_send_list);
937 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
938 bat_priv->my_vis_info,
939 &bat_priv->my_vis_info->hash_entry);
940 if (hash_added < 0) {
941 pr_err("Can't add own vis packet into hash\n");
942 /* not in hash, need to remove it manually. */
943 kref_put(&bat_priv->my_vis_info->refcount, free_info);
947 spin_unlock_bh(&bat_priv->vis_hash_lock);
948 start_vis_timer(bat_priv);
952 kfree(bat_priv->my_vis_info);
953 bat_priv->my_vis_info = NULL;
955 spin_unlock_bh(&bat_priv->vis_hash_lock);
960 /* Decrease the reference count on a hash item info */
961 static void free_info_ref(struct hlist_node *node, void *arg)
963 struct vis_info *info;
965 info = container_of(node, struct vis_info, hash_entry);
967 kref_put(&info->refcount, free_info);
970 /* shutdown vis-server */
971 void vis_quit(struct bat_priv *bat_priv)
973 if (!bat_priv->vis_hash)
976 cancel_delayed_work_sync(&bat_priv->vis_work);
978 spin_lock_bh(&bat_priv->vis_hash_lock);
979 /* properly remove, kill timers ... */
980 hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
981 bat_priv->vis_hash = NULL;
982 bat_priv->my_vis_info = NULL;
983 spin_unlock_bh(&bat_priv->vis_hash_lock);
986 /* schedule packets for (re)transmission */
987 static void start_vis_timer(struct bat_priv *bat_priv)
989 INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
990 queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work,
991 msecs_to_jiffies(VIS_INTERVAL));