1 /* Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "translation-table.h"
24 #include "soft-interface.h"
25 #include "hard-interface.h"
27 #include "originator.h"
29 #define BATADV_MAX_VIS_PACKET_SIZE 1000
32 static struct lock_class_key batadv_vis_hash_lock_class_key;
34 static void batadv_start_vis_timer(struct batadv_priv *bat_priv);
37 static void batadv_free_info(struct kref *ref)
39 struct batadv_vis_info *info;
40 struct batadv_priv *bat_priv;
41 struct batadv_recvlist_node *entry, *tmp;
43 info = container_of(ref, struct batadv_vis_info, refcount);
44 bat_priv = info->bat_priv;
46 list_del_init(&info->send_list);
47 spin_lock_bh(&bat_priv->vis.list_lock);
48 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
49 list_del(&entry->list);
53 spin_unlock_bh(&bat_priv->vis.list_lock);
54 kfree_skb(info->skb_packet);
58 /* Compare two vis packets, used by the hashing algorithm */
59 static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
61 const struct batadv_vis_info *d1, *d2;
62 const struct batadv_vis_packet *p1, *p2;
64 d1 = container_of(node, struct batadv_vis_info, hash_entry);
66 p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
67 p2 = (struct batadv_vis_packet *)d2->skb_packet->data;
68 return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
71 /* hash function to choose an entry in a hash table of given size
72 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
74 static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
76 const struct batadv_vis_info *vis_info = data;
77 const struct batadv_vis_packet *packet;
78 const unsigned char *key;
82 packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
83 key = packet->vis_orig;
84 for (i = 0; i < ETH_ALEN; i++) {
97 static struct batadv_vis_info *
98 batadv_vis_hash_find(struct batadv_priv *bat_priv, const void *data)
100 struct batadv_hashtable *hash = bat_priv->vis.hash;
101 struct hlist_head *head;
102 struct hlist_node *node;
103 struct batadv_vis_info *vis_info, *vis_info_tmp = NULL;
109 index = batadv_vis_info_choose(data, hash->size);
110 head = &hash->table[index];
113 hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
114 if (!batadv_vis_info_cmp(node, data))
117 vis_info_tmp = vis_info;
125 /* insert interface to the list of interfaces of one originator, if it
126 * does not already exist in the list
128 static void batadv_vis_data_insert_interface(const uint8_t *interface,
129 struct hlist_head *if_list,
132 struct batadv_if_list_entry *entry;
133 struct hlist_node *pos;
135 hlist_for_each_entry(entry, pos, if_list, list) {
136 if (batadv_compare_eth(entry->addr, interface))
140 /* it's a new address, add it to the list */
141 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
144 memcpy(entry->addr, interface, ETH_ALEN);
145 entry->primary = primary;
146 hlist_add_head(&entry->list, if_list);
149 static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
150 const struct hlist_head *if_list)
152 struct batadv_if_list_entry *entry;
153 struct hlist_node *pos;
155 hlist_for_each_entry(entry, pos, if_list, list) {
157 seq_printf(seq, "PRIMARY, ");
159 seq_printf(seq, "SEC %pM, ", entry->addr);
165 batadv_vis_data_read_entry(struct seq_file *seq,
166 const struct batadv_vis_info_entry *entry,
167 const uint8_t *src, bool primary)
169 if (primary && entry->quality == 0)
170 return seq_printf(seq, "TT %pM, ", entry->dest);
171 else if (batadv_compare_eth(entry->src, src))
172 return seq_printf(seq, "TQ %pM %d, ", entry->dest,
179 batadv_vis_data_insert_interfaces(struct hlist_head *list,
180 struct batadv_vis_packet *packet,
181 struct batadv_vis_info_entry *entries)
185 for (i = 0; i < packet->entries; i++) {
186 if (entries[i].quality == 0)
189 if (batadv_compare_eth(entries[i].src, packet->vis_orig))
192 batadv_vis_data_insert_interface(entries[i].src, list, false);
196 static void batadv_vis_data_read_entries(struct seq_file *seq,
197 struct hlist_head *list,
198 struct batadv_vis_packet *packet,
199 struct batadv_vis_info_entry *entries)
202 struct batadv_if_list_entry *entry;
203 struct hlist_node *pos;
205 hlist_for_each_entry(entry, pos, list, list) {
206 seq_printf(seq, "%pM,", entry->addr);
208 for (i = 0; i < packet->entries; i++)
209 batadv_vis_data_read_entry(seq, &entries[i],
210 entry->addr, entry->primary);
212 /* add primary/secondary records */
213 if (batadv_compare_eth(entry->addr, packet->vis_orig))
214 batadv_vis_data_read_prim_sec(seq, list);
216 seq_printf(seq, "\n");
220 static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
221 const struct hlist_head *head)
223 struct hlist_node *node;
224 struct batadv_vis_info *info;
225 struct batadv_vis_packet *packet;
226 uint8_t *entries_pos;
227 struct batadv_vis_info_entry *entries;
228 struct batadv_if_list_entry *entry;
229 struct hlist_node *pos, *n;
231 HLIST_HEAD(vis_if_list);
233 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
234 packet = (struct batadv_vis_packet *)info->skb_packet->data;
235 entries_pos = (uint8_t *)packet + sizeof(*packet);
236 entries = (struct batadv_vis_info_entry *)entries_pos;
238 batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
240 batadv_vis_data_insert_interfaces(&vis_if_list, packet,
242 batadv_vis_data_read_entries(seq, &vis_if_list, packet,
245 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
246 hlist_del(&entry->list);
252 int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
254 struct batadv_hard_iface *primary_if;
255 struct hlist_head *head;
256 struct net_device *net_dev = (struct net_device *)seq->private;
257 struct batadv_priv *bat_priv = netdev_priv(net_dev);
258 struct batadv_hashtable *hash = bat_priv->vis.hash;
261 int vis_server = atomic_read(&bat_priv->vis_mode);
263 primary_if = batadv_primary_if_get_selected(bat_priv);
267 if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
270 spin_lock_bh(&bat_priv->vis.hash_lock);
271 for (i = 0; i < hash->size; i++) {
272 head = &hash->table[i];
273 batadv_vis_seq_print_text_bucket(seq, head);
275 spin_unlock_bh(&bat_priv->vis.hash_lock);
279 batadv_hardif_free_ref(primary_if);
283 /* add the info packet to the send list, if it was not
286 static void batadv_send_list_add(struct batadv_priv *bat_priv,
287 struct batadv_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
298 static void batadv_send_list_del(struct batadv_vis_info *info)
300 if (!list_empty(&info->send_list)) {
301 list_del_init(&info->send_list);
302 kref_put(&info->refcount, batadv_free_info);
306 /* tries to add one entry to the receive list. */
307 static void batadv_recv_list_add(struct batadv_priv *bat_priv,
308 struct list_head *recv_list, const char *mac)
310 struct batadv_recvlist_node *entry;
312 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
316 memcpy(entry->mac, mac, ETH_ALEN);
317 spin_lock_bh(&bat_priv->vis.list_lock);
318 list_add_tail(&entry->list, recv_list);
319 spin_unlock_bh(&bat_priv->vis.list_lock);
322 /* returns 1 if this mac is in the recv_list */
323 static int batadv_recv_list_is_in(struct batadv_priv *bat_priv,
324 const struct list_head *recv_list,
327 const struct batadv_recvlist_node *entry;
329 spin_lock_bh(&bat_priv->vis.list_lock);
330 list_for_each_entry(entry, recv_list, list) {
331 if (batadv_compare_eth(entry->mac, mac)) {
332 spin_unlock_bh(&bat_priv->vis.list_lock);
336 spin_unlock_bh(&bat_priv->vis.list_lock);
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.
344 static struct batadv_vis_info *
345 batadv_add_packet(struct batadv_priv *bat_priv,
346 struct batadv_vis_packet *vis_packet, int vis_info_len,
347 int *is_new, int make_broadcast)
349 struct batadv_vis_info *info, *old_info;
350 struct batadv_vis_packet *search_packet, *old_packet;
351 struct batadv_vis_info search_elem;
352 struct batadv_vis_packet *packet;
353 struct sk_buff *tmp_skb;
360 if (!bat_priv->vis.hash)
363 /* see if the packet is already in vis_hash */
364 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
365 if (!search_elem.skb_packet)
367 len = sizeof(*search_packet);
368 tmp_skb = search_elem.skb_packet;
369 search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
371 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
372 old_info = batadv_vis_hash_find(bat_priv, &search_elem);
373 kfree_skb(search_elem.skb_packet);
376 tmp_skb = old_info->skb_packet;
377 old_packet = (struct batadv_vis_packet *)tmp_skb->data;
378 if (!batadv_seq_after(ntohl(vis_packet->seqno),
379 ntohl(old_packet->seqno))) {
380 if (old_packet->seqno == vis_packet->seqno) {
381 batadv_recv_list_add(bat_priv,
382 &old_info->recv_list,
383 vis_packet->sender_orig);
386 /* newer packet is already in hash. */
390 /* remove old entry */
391 batadv_hash_remove(bat_priv->vis.hash, batadv_vis_info_cmp,
392 batadv_vis_info_choose, old_info);
393 batadv_send_list_del(old_info);
394 kref_put(&old_info->refcount, batadv_free_info);
397 info = kmalloc(sizeof(*info), GFP_ATOMIC);
401 len = sizeof(*packet) + vis_info_len;
402 info->skb_packet = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
403 if (!info->skb_packet) {
407 skb_reserve(info->skb_packet, ETH_HLEN + NET_IP_ALIGN);
408 packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
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, len);
417 /* initialize and add new packet. */
420 /* Make it a broadcast packet, if required */
422 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
424 /* repair if entries is longer than packet. */
425 max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry);
426 if (packet->entries > max_entries)
427 packet->entries = max_entries;
429 batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
432 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
433 batadv_vis_info_choose, info,
435 if (hash_added != 0) {
436 /* did not work (for some reason) */
437 kref_put(&info->refcount, batadv_free_info);
444 /* handle the server sync packet, forward if needed. */
445 void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
446 struct batadv_vis_packet *vis_packet,
449 struct batadv_vis_info *info;
450 int is_new, make_broadcast;
451 int vis_server = atomic_read(&bat_priv->vis_mode);
453 make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
455 spin_lock_bh(&bat_priv->vis.hash_lock);
456 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
457 &is_new, make_broadcast);
461 /* only if we are server ourselves and packet is newer than the one in
464 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
465 batadv_send_list_add(bat_priv, info);
467 spin_unlock_bh(&bat_priv->vis.hash_lock);
470 /* handle an incoming client update packet and schedule forward if needed. */
471 void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
472 struct batadv_vis_packet *vis_packet,
475 struct batadv_vis_info *info;
476 struct batadv_vis_packet *packet;
478 int vis_server = atomic_read(&bat_priv->vis_mode);
481 /* clients shall not broadcast. */
482 if (is_broadcast_ether_addr(vis_packet->target_orig))
485 /* Are we the target for this VIS packet? */
486 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC &&
487 batadv_is_my_mac(vis_packet->target_orig))
490 spin_lock_bh(&bat_priv->vis.hash_lock);
491 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
492 &is_new, are_target);
496 /* note that outdated packets will be dropped at this point. */
498 packet = (struct batadv_vis_packet *)info->skb_packet->data;
500 /* send only if we're the target server or ... */
501 if (are_target && is_new) {
502 packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
503 batadv_send_list_add(bat_priv, info);
505 /* ... we're not the recipient (and thus need to forward). */
506 } else if (!batadv_is_my_mac(packet->target_orig)) {
507 batadv_send_list_add(bat_priv, info);
511 spin_unlock_bh(&bat_priv->vis.hash_lock);
514 /* Walk the originators and find the VIS server with the best tq. Set the packet
515 * address to its address and return the best_tq.
517 * Must be called with the originator hash locked
519 static int batadv_find_best_vis_server(struct batadv_priv *bat_priv,
520 struct batadv_vis_info *info)
522 struct batadv_hashtable *hash = bat_priv->orig_hash;
523 struct batadv_neigh_node *router;
524 struct hlist_node *node;
525 struct hlist_head *head;
526 struct batadv_orig_node *orig_node;
527 struct batadv_vis_packet *packet;
531 packet = (struct batadv_vis_packet *)info->skb_packet->data;
533 for (i = 0; i < hash->size; i++) {
534 head = &hash->table[i];
537 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
538 router = batadv_orig_node_get_router(orig_node);
542 if ((orig_node->flags & BATADV_VIS_SERVER) &&
543 (router->tq_avg > best_tq)) {
544 best_tq = router->tq_avg;
545 memcpy(packet->target_orig, orig_node->orig,
548 batadv_neigh_node_free_ref(router);
556 /* Return true if the vis packet is full. */
557 static bool batadv_vis_packet_full(const struct batadv_vis_info *info)
559 const struct batadv_vis_packet *packet;
562 packet = (struct batadv_vis_packet *)info->skb_packet->data;
563 num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry);
565 if (num < packet->entries + 1)
570 /* generates a packet of own vis data,
571 * returns 0 on success, -1 if no packet could be generated
573 static int batadv_generate_vis_packet(struct batadv_priv *bat_priv)
575 struct batadv_hashtable *hash = bat_priv->orig_hash;
576 struct hlist_node *node;
577 struct hlist_head *head;
578 struct batadv_orig_node *orig_node;
579 struct batadv_neigh_node *router;
580 struct batadv_vis_info *info = bat_priv->vis.my_info;
581 struct batadv_vis_packet *packet;
582 struct batadv_vis_info_entry *entry;
583 struct batadv_tt_common_entry *tt_common_entry;
588 info->first_seen = jiffies;
589 packet = (struct batadv_vis_packet *)info->skb_packet->data;
590 packet->vis_type = atomic_read(&bat_priv->vis_mode);
592 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
593 packet->header.ttl = BATADV_TTL;
594 packet->seqno = htonl(ntohl(packet->seqno) + 1);
596 packet->reserved = 0;
597 skb_trim(info->skb_packet, sizeof(*packet));
599 if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
600 best_tq = batadv_find_best_vis_server(bat_priv, info);
606 for (i = 0; i < hash->size; i++) {
607 head = &hash->table[i];
610 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
611 router = batadv_orig_node_get_router(orig_node);
615 if (!batadv_compare_eth(router->addr, orig_node->orig))
618 if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
621 if (router->tq_avg < 1)
624 /* fill one entry into buffer. */
625 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
626 entry = (struct batadv_vis_info_entry *)packet_pos;
628 router->if_incoming->net_dev->dev_addr,
630 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
631 entry->quality = router->tq_avg;
635 batadv_neigh_node_free_ref(router);
637 if (batadv_vis_packet_full(info))
643 hash = bat_priv->tt.local_hash;
645 for (i = 0; i < hash->size; i++) {
646 head = &hash->table[i];
649 hlist_for_each_entry_rcu(tt_common_entry, node, head,
651 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
652 entry = (struct batadv_vis_info_entry *)packet_pos;
653 memset(entry->src, 0, ETH_ALEN);
654 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
655 entry->quality = 0; /* 0 means TT */
658 if (batadv_vis_packet_full(info))
671 /* free old vis packets. Must be called with this vis_hash_lock
674 static void batadv_purge_vis_packets(struct batadv_priv *bat_priv)
677 struct batadv_hashtable *hash = bat_priv->vis.hash;
678 struct hlist_node *node, *node_tmp;
679 struct hlist_head *head;
680 struct batadv_vis_info *info;
682 for (i = 0; i < hash->size; i++) {
683 head = &hash->table[i];
685 hlist_for_each_entry_safe(info, node, node_tmp,
687 /* never purge own data. */
688 if (info == bat_priv->vis.my_info)
691 if (batadv_has_timed_out(info->first_seen,
692 BATADV_VIS_TIMEOUT)) {
694 batadv_send_list_del(info);
695 kref_put(&info->refcount, batadv_free_info);
701 static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv,
702 struct batadv_vis_info *info)
704 struct batadv_hashtable *hash = bat_priv->orig_hash;
705 struct hlist_node *node;
706 struct hlist_head *head;
707 struct batadv_orig_node *orig_node;
708 struct batadv_vis_packet *packet;
713 packet = (struct batadv_vis_packet *)info->skb_packet->data;
715 /* send to all routers in range. */
716 for (i = 0; i < hash->size; i++) {
717 head = &hash->table[i];
720 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
721 /* if it's a vis server and reachable, send it. */
722 if (!(orig_node->flags & BATADV_VIS_SERVER))
725 /* don't send it if we already received the packet from
728 if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
732 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
733 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
737 if (!batadv_send_skb_to_orig(skb, orig_node, NULL))
744 static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv,
745 struct batadv_vis_info *info)
747 struct batadv_orig_node *orig_node;
749 struct batadv_vis_packet *packet;
751 packet = (struct batadv_vis_packet *)info->skb_packet->data;
753 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
757 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
761 if (!batadv_send_skb_to_orig(skb, orig_node, NULL))
766 batadv_orig_node_free_ref(orig_node);
769 /* only send one vis packet. called from batadv_send_vis_packets() */
770 static void batadv_send_vis_packet(struct batadv_priv *bat_priv,
771 struct batadv_vis_info *info)
773 struct batadv_hard_iface *primary_if;
774 struct batadv_vis_packet *packet;
776 primary_if = batadv_primary_if_get_selected(bat_priv);
780 packet = (struct batadv_vis_packet *)info->skb_packet->data;
781 if (packet->header.ttl < 2) {
782 pr_debug("Error - can't send vis packet: ttl exceeded\n");
786 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
787 packet->header.ttl--;
789 if (is_broadcast_ether_addr(packet->target_orig))
790 batadv_broadcast_vis_packet(bat_priv, info);
792 batadv_unicast_vis_packet(bat_priv, info);
793 packet->header.ttl++; /* restore TTL */
797 batadv_hardif_free_ref(primary_if);
800 /* called from timer; send (and maybe generate) vis packet. */
801 static void batadv_send_vis_packets(struct work_struct *work)
803 struct delayed_work *delayed_work;
804 struct batadv_priv *bat_priv;
805 struct batadv_priv_vis *priv_vis;
806 struct batadv_vis_info *info;
808 delayed_work = container_of(work, struct delayed_work, work);
809 priv_vis = container_of(delayed_work, struct batadv_priv_vis, work);
810 bat_priv = container_of(priv_vis, struct batadv_priv, vis);
811 spin_lock_bh(&bat_priv->vis.hash_lock);
812 batadv_purge_vis_packets(bat_priv);
814 if (batadv_generate_vis_packet(bat_priv) == 0) {
815 /* schedule if generation was successful */
816 batadv_send_list_add(bat_priv, bat_priv->vis.my_info);
819 while (!list_empty(&bat_priv->vis.send_list)) {
820 info = list_first_entry(&bat_priv->vis.send_list,
821 typeof(*info), send_list);
823 kref_get(&info->refcount);
824 spin_unlock_bh(&bat_priv->vis.hash_lock);
826 batadv_send_vis_packet(bat_priv, info);
828 spin_lock_bh(&bat_priv->vis.hash_lock);
829 batadv_send_list_del(info);
830 kref_put(&info->refcount, batadv_free_info);
832 spin_unlock_bh(&bat_priv->vis.hash_lock);
833 batadv_start_vis_timer(bat_priv);
836 /* init the vis server. this may only be called when if_list is already
837 * initialized (e.g. bat0 is initialized, interfaces have been added)
839 int batadv_vis_init(struct batadv_priv *bat_priv)
841 struct batadv_vis_packet *packet;
844 unsigned long first_seen;
845 struct sk_buff *tmp_skb;
847 if (bat_priv->vis.hash)
850 spin_lock_bh(&bat_priv->vis.hash_lock);
852 bat_priv->vis.hash = batadv_hash_new(256);
853 if (!bat_priv->vis.hash) {
854 pr_err("Can't initialize vis_hash\n");
858 batadv_hash_set_lock_class(bat_priv->vis.hash,
859 &batadv_vis_hash_lock_class_key);
861 bat_priv->vis.my_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
862 if (!bat_priv->vis.my_info)
865 len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE;
866 len += ETH_HLEN + NET_IP_ALIGN;
867 bat_priv->vis.my_info->skb_packet = dev_alloc_skb(len);
868 if (!bat_priv->vis.my_info->skb_packet)
871 skb_reserve(bat_priv->vis.my_info->skb_packet, ETH_HLEN + NET_IP_ALIGN);
872 tmp_skb = bat_priv->vis.my_info->skb_packet;
873 packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
875 /* prefill the vis info */
876 first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
877 bat_priv->vis.my_info->first_seen = first_seen;
878 INIT_LIST_HEAD(&bat_priv->vis.my_info->recv_list);
879 INIT_LIST_HEAD(&bat_priv->vis.my_info->send_list);
880 kref_init(&bat_priv->vis.my_info->refcount);
881 bat_priv->vis.my_info->bat_priv = bat_priv;
882 packet->header.version = BATADV_COMPAT_VERSION;
883 packet->header.packet_type = BATADV_VIS;
884 packet->header.ttl = BATADV_TTL;
886 packet->reserved = 0;
889 INIT_LIST_HEAD(&bat_priv->vis.send_list);
891 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
892 batadv_vis_info_choose,
893 bat_priv->vis.my_info,
894 &bat_priv->vis.my_info->hash_entry);
895 if (hash_added != 0) {
896 pr_err("Can't add own vis packet into hash\n");
897 /* not in hash, need to remove it manually. */
898 kref_put(&bat_priv->vis.my_info->refcount, batadv_free_info);
902 spin_unlock_bh(&bat_priv->vis.hash_lock);
903 batadv_start_vis_timer(bat_priv);
907 kfree(bat_priv->vis.my_info);
908 bat_priv->vis.my_info = NULL;
910 spin_unlock_bh(&bat_priv->vis.hash_lock);
911 batadv_vis_quit(bat_priv);
915 /* Decrease the reference count on a hash item info */
916 static void batadv_free_info_ref(struct hlist_node *node, void *arg)
918 struct batadv_vis_info *info;
920 info = container_of(node, struct batadv_vis_info, hash_entry);
921 batadv_send_list_del(info);
922 kref_put(&info->refcount, batadv_free_info);
925 /* shutdown vis-server */
926 void batadv_vis_quit(struct batadv_priv *bat_priv)
928 if (!bat_priv->vis.hash)
931 cancel_delayed_work_sync(&bat_priv->vis.work);
933 spin_lock_bh(&bat_priv->vis.hash_lock);
934 /* properly remove, kill timers ... */
935 batadv_hash_delete(bat_priv->vis.hash, batadv_free_info_ref, NULL);
936 bat_priv->vis.hash = NULL;
937 bat_priv->vis.my_info = NULL;
938 spin_unlock_bh(&bat_priv->vis.hash_lock);
941 /* schedule packets for (re)transmission */
942 static void batadv_start_vis_timer(struct batadv_priv *bat_priv)
944 INIT_DELAYED_WORK(&bat_priv->vis.work, batadv_send_vis_packets);
945 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
946 msecs_to_jiffies(BATADV_VIS_INTERVAL));