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
31 static void batadv_start_vis_timer(struct bat_priv
*bat_priv
);
34 static void batadv_free_info(struct kref
*ref
)
36 struct vis_info
*info
= container_of(ref
, struct vis_info
, refcount
);
37 struct bat_priv
*bat_priv
= info
->bat_priv
;
38 struct recvlist_node
*entry
, *tmp
;
40 list_del_init(&info
->send_list
);
41 spin_lock_bh(&bat_priv
->vis_list_lock
);
42 list_for_each_entry_safe(entry
, tmp
, &info
->recv_list
, list
) {
43 list_del(&entry
->list
);
47 spin_unlock_bh(&bat_priv
->vis_list_lock
);
48 kfree_skb(info
->skb_packet
);
52 /* Compare two vis packets, used by the hashing algorithm */
53 static int batadv_vis_info_cmp(const struct hlist_node
*node
, const void *data2
)
55 const struct vis_info
*d1
, *d2
;
56 const struct vis_packet
*p1
, *p2
;
58 d1
= container_of(node
, struct vis_info
, hash_entry
);
60 p1
= (struct vis_packet
*)d1
->skb_packet
->data
;
61 p2
= (struct vis_packet
*)d2
->skb_packet
->data
;
62 return batadv_compare_eth(p1
->vis_orig
, p2
->vis_orig
);
65 /* hash function to choose an entry in a hash table of given size
66 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
68 static uint32_t batadv_vis_info_choose(const void *data
, uint32_t size
)
70 const struct vis_info
*vis_info
= data
;
71 const struct vis_packet
*packet
;
72 const unsigned char *key
;
76 packet
= (struct vis_packet
*)vis_info
->skb_packet
->data
;
77 key
= packet
->vis_orig
;
78 for (i
= 0; i
< ETH_ALEN
; i
++) {
91 static struct vis_info
*batadv_vis_hash_find(struct bat_priv
*bat_priv
,
94 struct hashtable_t
*hash
= bat_priv
->vis_hash
;
95 struct hlist_head
*head
;
96 struct hlist_node
*node
;
97 struct vis_info
*vis_info
, *vis_info_tmp
= NULL
;
103 index
= batadv_vis_info_choose(data
, hash
->size
);
104 head
= &hash
->table
[index
];
107 hlist_for_each_entry_rcu(vis_info
, node
, head
, hash_entry
) {
108 if (!batadv_vis_info_cmp(node
, data
))
111 vis_info_tmp
= vis_info
;
119 /* insert interface to the list of interfaces of one originator, if it
120 * does not already exist in the list
122 static void batadv_vis_data_insert_interface(const uint8_t *interface
,
123 struct hlist_head
*if_list
,
126 struct if_list_entry
*entry
;
127 struct hlist_node
*pos
;
129 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
130 if (batadv_compare_eth(entry
->addr
, interface
))
134 /* it's a new address, add it to the list */
135 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
138 memcpy(entry
->addr
, interface
, ETH_ALEN
);
139 entry
->primary
= primary
;
140 hlist_add_head(&entry
->list
, if_list
);
143 static void batadv_vis_data_read_prim_sec(struct seq_file
*seq
,
144 const struct hlist_head
*if_list
)
146 struct if_list_entry
*entry
;
147 struct hlist_node
*pos
;
149 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
151 seq_printf(seq
, "PRIMARY, ");
153 seq_printf(seq
, "SEC %pM, ", entry
->addr
);
158 static ssize_t
batadv_vis_data_read_entry(struct seq_file
*seq
,
159 const struct vis_info_entry
*entry
,
160 const uint8_t *src
, bool primary
)
162 if (primary
&& entry
->quality
== 0)
163 return seq_printf(seq
, "TT %pM, ", entry
->dest
);
164 else if (batadv_compare_eth(entry
->src
, src
))
165 return seq_printf(seq
, "TQ %pM %d, ", entry
->dest
,
171 static void batadv_vis_data_insert_interfaces(struct hlist_head
*list
,
172 struct vis_packet
*packet
,
173 struct vis_info_entry
*entries
)
177 for (i
= 0; i
< packet
->entries
; i
++) {
178 if (entries
[i
].quality
== 0)
181 if (batadv_compare_eth(entries
[i
].src
, packet
->vis_orig
))
184 batadv_vis_data_insert_interface(entries
[i
].src
, list
, false);
188 static void batadv_vis_data_read_entries(struct seq_file
*seq
,
189 struct hlist_head
*list
,
190 struct vis_packet
*packet
,
191 struct vis_info_entry
*entries
)
194 struct if_list_entry
*entry
;
195 struct hlist_node
*pos
;
197 hlist_for_each_entry(entry
, pos
, list
, list
) {
198 seq_printf(seq
, "%pM,", entry
->addr
);
200 for (i
= 0; i
< packet
->entries
; i
++)
201 batadv_vis_data_read_entry(seq
, &entries
[i
],
202 entry
->addr
, entry
->primary
);
204 /* add primary/secondary records */
205 if (batadv_compare_eth(entry
->addr
, packet
->vis_orig
))
206 batadv_vis_data_read_prim_sec(seq
, list
);
208 seq_printf(seq
, "\n");
212 static void batadv_vis_seq_print_text_bucket(struct seq_file
*seq
,
213 const struct hlist_head
*head
)
215 struct hlist_node
*node
;
216 struct vis_info
*info
;
217 struct vis_packet
*packet
;
218 uint8_t *entries_pos
;
219 struct vis_info_entry
*entries
;
220 struct if_list_entry
*entry
;
221 struct hlist_node
*pos
, *n
;
223 HLIST_HEAD(vis_if_list
);
225 hlist_for_each_entry_rcu(info
, node
, head
, hash_entry
) {
226 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
227 entries_pos
= (uint8_t *)packet
+ sizeof(*packet
);
228 entries
= (struct vis_info_entry
*)entries_pos
;
230 batadv_vis_data_insert_interface(packet
->vis_orig
, &vis_if_list
,
232 batadv_vis_data_insert_interfaces(&vis_if_list
, packet
,
234 batadv_vis_data_read_entries(seq
, &vis_if_list
, packet
,
237 hlist_for_each_entry_safe(entry
, pos
, n
, &vis_if_list
, list
) {
238 hlist_del(&entry
->list
);
244 int batadv_vis_seq_print_text(struct seq_file
*seq
, void *offset
)
246 struct hard_iface
*primary_if
;
247 struct hlist_head
*head
;
248 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
249 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
250 struct hashtable_t
*hash
= bat_priv
->vis_hash
;
253 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
255 primary_if
= batadv_primary_if_get_selected(bat_priv
);
259 if (vis_server
== VIS_TYPE_CLIENT_UPDATE
)
262 spin_lock_bh(&bat_priv
->vis_hash_lock
);
263 for (i
= 0; i
< hash
->size
; i
++) {
264 head
= &hash
->table
[i
];
265 batadv_vis_seq_print_text_bucket(seq
, head
);
267 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
271 batadv_hardif_free_ref(primary_if
);
275 /* add the info packet to the send list, if it was not
278 static void batadv_send_list_add(struct bat_priv
*bat_priv
,
279 struct vis_info
*info
)
281 if (list_empty(&info
->send_list
)) {
282 kref_get(&info
->refcount
);
283 list_add_tail(&info
->send_list
, &bat_priv
->vis_send_list
);
287 /* delete the info packet from the send list, if it was
290 static void batadv_send_list_del(struct vis_info
*info
)
292 if (!list_empty(&info
->send_list
)) {
293 list_del_init(&info
->send_list
);
294 kref_put(&info
->refcount
, batadv_free_info
);
298 /* tries to add one entry to the receive list. */
299 static void batadv_recv_list_add(struct bat_priv
*bat_priv
,
300 struct list_head
*recv_list
, const char *mac
)
302 struct recvlist_node
*entry
;
304 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
308 memcpy(entry
->mac
, mac
, ETH_ALEN
);
309 spin_lock_bh(&bat_priv
->vis_list_lock
);
310 list_add_tail(&entry
->list
, recv_list
);
311 spin_unlock_bh(&bat_priv
->vis_list_lock
);
314 /* returns 1 if this mac is in the recv_list */
315 static int batadv_recv_list_is_in(struct bat_priv
*bat_priv
,
316 const struct list_head
*recv_list
,
319 const struct recvlist_node
*entry
;
321 spin_lock_bh(&bat_priv
->vis_list_lock
);
322 list_for_each_entry(entry
, recv_list
, list
) {
323 if (batadv_compare_eth(entry
->mac
, mac
)) {
324 spin_unlock_bh(&bat_priv
->vis_list_lock
);
328 spin_unlock_bh(&bat_priv
->vis_list_lock
);
332 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
333 * broken.. ). vis hash must be locked outside. is_new is set when the packet
334 * is newer than old entries in the hash.
336 static struct vis_info
*batadv_add_packet(struct bat_priv
*bat_priv
,
337 struct vis_packet
*vis_packet
,
338 int vis_info_len
, int *is_new
,
341 struct vis_info
*info
, *old_info
;
342 struct vis_packet
*search_packet
, *old_packet
;
343 struct vis_info search_elem
;
344 struct vis_packet
*packet
;
349 if (!bat_priv
->vis_hash
)
352 /* see if the packet is already in vis_hash */
353 search_elem
.skb_packet
= dev_alloc_skb(sizeof(*search_packet
));
354 if (!search_elem
.skb_packet
)
356 search_packet
= (struct vis_packet
*)skb_put(search_elem
.skb_packet
,
357 sizeof(*search_packet
));
359 memcpy(search_packet
->vis_orig
, vis_packet
->vis_orig
, ETH_ALEN
);
360 old_info
= batadv_vis_hash_find(bat_priv
, &search_elem
);
361 kfree_skb(search_elem
.skb_packet
);
364 old_packet
= (struct vis_packet
*)old_info
->skb_packet
->data
;
365 if (!batadv_seq_after(ntohl(vis_packet
->seqno
),
366 ntohl(old_packet
->seqno
))) {
367 if (old_packet
->seqno
== vis_packet
->seqno
) {
368 batadv_recv_list_add(bat_priv
,
369 &old_info
->recv_list
,
370 vis_packet
->sender_orig
);
373 /* newer packet is already in hash. */
377 /* remove old entry */
378 batadv_hash_remove(bat_priv
->vis_hash
, batadv_vis_info_cmp
,
379 batadv_vis_info_choose
, old_info
);
380 batadv_send_list_del(old_info
);
381 kref_put(&old_info
->refcount
, batadv_free_info
);
384 info
= kmalloc(sizeof(*info
), GFP_ATOMIC
);
388 info
->skb_packet
= dev_alloc_skb(sizeof(*packet
) + vis_info_len
+
390 if (!info
->skb_packet
) {
394 skb_reserve(info
->skb_packet
, ETH_HLEN
);
395 packet
= (struct vis_packet
*)skb_put(info
->skb_packet
, sizeof(*packet
)
398 kref_init(&info
->refcount
);
399 INIT_LIST_HEAD(&info
->send_list
);
400 INIT_LIST_HEAD(&info
->recv_list
);
401 info
->first_seen
= jiffies
;
402 info
->bat_priv
= bat_priv
;
403 memcpy(packet
, vis_packet
, sizeof(*packet
) + vis_info_len
);
405 /* initialize and add new packet. */
408 /* Make it a broadcast packet, if required */
410 memcpy(packet
->target_orig
, batadv_broadcast_addr
, ETH_ALEN
);
412 /* repair if entries is longer than packet. */
413 if (packet
->entries
* sizeof(struct vis_info_entry
) > vis_info_len
)
414 packet
->entries
= vis_info_len
/ sizeof(struct vis_info_entry
);
416 batadv_recv_list_add(bat_priv
, &info
->recv_list
, packet
->sender_orig
);
419 hash_added
= batadv_hash_add(bat_priv
->vis_hash
, batadv_vis_info_cmp
,
420 batadv_vis_info_choose
, info
,
422 if (hash_added
!= 0) {
423 /* did not work (for some reason) */
424 kref_put(&info
->refcount
, batadv_free_info
);
431 /* handle the server sync packet, forward if needed. */
432 void batadv_receive_server_sync_packet(struct bat_priv
*bat_priv
,
433 struct vis_packet
*vis_packet
,
436 struct vis_info
*info
;
437 int is_new
, make_broadcast
;
438 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
440 make_broadcast
= (vis_server
== VIS_TYPE_SERVER_SYNC
);
442 spin_lock_bh(&bat_priv
->vis_hash_lock
);
443 info
= batadv_add_packet(bat_priv
, vis_packet
, vis_info_len
,
444 &is_new
, make_broadcast
);
448 /* only if we are server ourselves and packet is newer than the one in
451 if (vis_server
== VIS_TYPE_SERVER_SYNC
&& is_new
)
452 batadv_send_list_add(bat_priv
, info
);
454 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
457 /* handle an incoming client update packet and schedule forward if needed. */
458 void batadv_receive_client_update_packet(struct bat_priv
*bat_priv
,
459 struct vis_packet
*vis_packet
,
462 struct vis_info
*info
;
463 struct vis_packet
*packet
;
465 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
468 /* clients shall not broadcast. */
469 if (is_broadcast_ether_addr(vis_packet
->target_orig
))
472 /* Are we the target for this VIS packet? */
473 if (vis_server
== VIS_TYPE_SERVER_SYNC
&&
474 batadv_is_my_mac(vis_packet
->target_orig
))
477 spin_lock_bh(&bat_priv
->vis_hash_lock
);
478 info
= batadv_add_packet(bat_priv
, vis_packet
, vis_info_len
,
479 &is_new
, are_target
);
483 /* note that outdated packets will be dropped at this point. */
485 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
487 /* send only if we're the target server or ... */
488 if (are_target
&& is_new
) {
489 packet
->vis_type
= VIS_TYPE_SERVER_SYNC
; /* upgrade! */
490 batadv_send_list_add(bat_priv
, info
);
492 /* ... we're not the recipient (and thus need to forward). */
493 } else if (!batadv_is_my_mac(packet
->target_orig
)) {
494 batadv_send_list_add(bat_priv
, info
);
498 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
501 /* Walk the originators and find the VIS server with the best tq. Set the packet
502 * address to its address and return the best_tq.
504 * Must be called with the originator hash locked
506 static int batadv_find_best_vis_server(struct bat_priv
*bat_priv
,
507 struct vis_info
*info
)
509 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
510 struct neigh_node
*router
;
511 struct hlist_node
*node
;
512 struct hlist_head
*head
;
513 struct orig_node
*orig_node
;
514 struct vis_packet
*packet
;
518 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
520 for (i
= 0; i
< hash
->size
; i
++) {
521 head
= &hash
->table
[i
];
524 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
525 router
= batadv_orig_node_get_router(orig_node
);
529 if ((orig_node
->flags
& VIS_SERVER
) &&
530 (router
->tq_avg
> best_tq
)) {
531 best_tq
= router
->tq_avg
;
532 memcpy(packet
->target_orig
, orig_node
->orig
,
535 batadv_neigh_node_free_ref(router
);
543 /* Return true if the vis packet is full. */
544 static bool batadv_vis_packet_full(const struct vis_info
*info
)
546 const struct vis_packet
*packet
;
549 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
550 num_items
= BATADV_MAX_VIS_PACKET_SIZE
/ sizeof(struct vis_info_entry
);
552 if (num_items
< packet
->entries
+ 1)
557 /* generates a packet of own vis data,
558 * returns 0 on success, -1 if no packet could be generated
560 static int batadv_generate_vis_packet(struct bat_priv
*bat_priv
)
562 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
563 struct hlist_node
*node
;
564 struct hlist_head
*head
;
565 struct orig_node
*orig_node
;
566 struct neigh_node
*router
;
567 struct vis_info
*info
= bat_priv
->my_vis_info
;
568 struct vis_packet
*packet
= (struct vis_packet
*)info
->skb_packet
->data
;
569 struct vis_info_entry
*entry
;
570 struct tt_common_entry
*tt_common_entry
;
574 info
->first_seen
= jiffies
;
575 packet
->vis_type
= atomic_read(&bat_priv
->vis_mode
);
577 memcpy(packet
->target_orig
, batadv_broadcast_addr
, ETH_ALEN
);
578 packet
->header
.ttl
= TTL
;
579 packet
->seqno
= htonl(ntohl(packet
->seqno
) + 1);
581 skb_trim(info
->skb_packet
, sizeof(*packet
));
583 if (packet
->vis_type
== VIS_TYPE_CLIENT_UPDATE
) {
584 best_tq
= batadv_find_best_vis_server(bat_priv
, info
);
590 for (i
= 0; i
< hash
->size
; i
++) {
591 head
= &hash
->table
[i
];
594 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
595 router
= batadv_orig_node_get_router(orig_node
);
599 if (!batadv_compare_eth(router
->addr
, orig_node
->orig
))
602 if (router
->if_incoming
->if_status
!= IF_ACTIVE
)
605 if (router
->tq_avg
< 1)
608 /* fill one entry into buffer. */
609 entry
= (struct vis_info_entry
*)
610 skb_put(info
->skb_packet
, sizeof(*entry
));
612 router
->if_incoming
->net_dev
->dev_addr
,
614 memcpy(entry
->dest
, orig_node
->orig
, ETH_ALEN
);
615 entry
->quality
= router
->tq_avg
;
619 batadv_neigh_node_free_ref(router
);
621 if (batadv_vis_packet_full(info
))
627 hash
= bat_priv
->tt_local_hash
;
629 for (i
= 0; i
< hash
->size
; i
++) {
630 head
= &hash
->table
[i
];
633 hlist_for_each_entry_rcu(tt_common_entry
, node
, head
,
635 entry
= (struct vis_info_entry
*)
636 skb_put(info
->skb_packet
,
638 memset(entry
->src
, 0, ETH_ALEN
);
639 memcpy(entry
->dest
, tt_common_entry
->addr
, ETH_ALEN
);
640 entry
->quality
= 0; /* 0 means TT */
643 if (batadv_vis_packet_full(info
))
656 /* free old vis packets. Must be called with this vis_hash_lock
659 static void batadv_purge_vis_packets(struct bat_priv
*bat_priv
)
662 struct hashtable_t
*hash
= bat_priv
->vis_hash
;
663 struct hlist_node
*node
, *node_tmp
;
664 struct hlist_head
*head
;
665 struct vis_info
*info
;
667 for (i
= 0; i
< hash
->size
; i
++) {
668 head
= &hash
->table
[i
];
670 hlist_for_each_entry_safe(info
, node
, node_tmp
,
672 /* never purge own data. */
673 if (info
== bat_priv
->my_vis_info
)
676 if (batadv_has_timed_out(info
->first_seen
,
679 batadv_send_list_del(info
);
680 kref_put(&info
->refcount
, batadv_free_info
);
686 static void batadv_broadcast_vis_packet(struct bat_priv
*bat_priv
,
687 struct vis_info
*info
)
689 struct neigh_node
*router
;
690 struct hashtable_t
*hash
= bat_priv
->orig_hash
;
691 struct hlist_node
*node
;
692 struct hlist_head
*head
;
693 struct orig_node
*orig_node
;
694 struct vis_packet
*packet
;
696 struct hard_iface
*hard_iface
;
697 uint8_t dstaddr
[ETH_ALEN
];
701 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
703 /* send to all routers in range. */
704 for (i
= 0; i
< hash
->size
; i
++) {
705 head
= &hash
->table
[i
];
708 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
709 /* if it's a vis server and reachable, send it. */
710 if (!(orig_node
->flags
& VIS_SERVER
))
713 router
= batadv_orig_node_get_router(orig_node
);
717 /* don't send it if we already received the packet from
720 if (batadv_recv_list_is_in(bat_priv
, &info
->recv_list
,
722 batadv_neigh_node_free_ref(router
);
726 memcpy(packet
->target_orig
, orig_node
->orig
, ETH_ALEN
);
727 hard_iface
= router
->if_incoming
;
728 memcpy(dstaddr
, router
->addr
, ETH_ALEN
);
730 batadv_neigh_node_free_ref(router
);
732 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
734 batadv_send_skb_packet(skb
, hard_iface
,
742 static void batadv_unicast_vis_packet(struct bat_priv
*bat_priv
,
743 struct vis_info
*info
)
745 struct orig_node
*orig_node
;
746 struct neigh_node
*router
= NULL
;
748 struct vis_packet
*packet
;
750 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
752 orig_node
= batadv_orig_hash_find(bat_priv
, packet
->target_orig
);
756 router
= batadv_orig_node_get_router(orig_node
);
760 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
762 batadv_send_skb_packet(skb
, router
->if_incoming
, router
->addr
);
766 batadv_neigh_node_free_ref(router
);
768 batadv_orig_node_free_ref(orig_node
);
771 /* only send one vis packet. called from batadv_send_vis_packets() */
772 static void batadv_send_vis_packet(struct bat_priv
*bat_priv
,
773 struct vis_info
*info
)
775 struct hard_iface
*primary_if
;
776 struct vis_packet
*packet
;
778 primary_if
= batadv_primary_if_get_selected(bat_priv
);
782 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
783 if (packet
->header
.ttl
< 2) {
784 pr_debug("Error - can't send vis packet: ttl exceeded\n");
788 memcpy(packet
->sender_orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
789 packet
->header
.ttl
--;
791 if (is_broadcast_ether_addr(packet
->target_orig
))
792 batadv_broadcast_vis_packet(bat_priv
, info
);
794 batadv_unicast_vis_packet(bat_priv
, info
);
795 packet
->header
.ttl
++; /* restore TTL */
799 batadv_hardif_free_ref(primary_if
);
802 /* called from timer; send (and maybe generate) vis packet. */
803 static void batadv_send_vis_packets(struct work_struct
*work
)
805 struct delayed_work
*delayed_work
=
806 container_of(work
, struct delayed_work
, work
);
807 struct bat_priv
*bat_priv
=
808 container_of(delayed_work
, struct bat_priv
, vis_work
);
809 struct vis_info
*info
;
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
->my_vis_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 bat_priv
*bat_priv
)
841 struct vis_packet
*packet
;
845 if (bat_priv
->vis_hash
)
848 spin_lock_bh(&bat_priv
->vis_hash_lock
);
850 bat_priv
->vis_hash
= batadv_hash_new(256);
851 if (!bat_priv
->vis_hash
) {
852 pr_err("Can't initialize vis_hash\n");
856 bat_priv
->my_vis_info
= kmalloc(BATADV_MAX_VIS_PACKET_SIZE
, GFP_ATOMIC
);
857 if (!bat_priv
->my_vis_info
)
860 len
= sizeof(*packet
) + BATADV_MAX_VIS_PACKET_SIZE
+ ETH_HLEN
;
861 bat_priv
->my_vis_info
->skb_packet
= dev_alloc_skb(len
);
862 if (!bat_priv
->my_vis_info
->skb_packet
)
865 skb_reserve(bat_priv
->my_vis_info
->skb_packet
, ETH_HLEN
);
866 packet
= (struct vis_packet
*)skb_put(bat_priv
->my_vis_info
->skb_packet
,
869 /* prefill the vis info */
870 bat_priv
->my_vis_info
->first_seen
= jiffies
-
871 msecs_to_jiffies(VIS_INTERVAL
);
872 INIT_LIST_HEAD(&bat_priv
->my_vis_info
->recv_list
);
873 INIT_LIST_HEAD(&bat_priv
->my_vis_info
->send_list
);
874 kref_init(&bat_priv
->my_vis_info
->refcount
);
875 bat_priv
->my_vis_info
->bat_priv
= bat_priv
;
876 packet
->header
.version
= COMPAT_VERSION
;
877 packet
->header
.packet_type
= BAT_VIS
;
878 packet
->header
.ttl
= TTL
;
882 INIT_LIST_HEAD(&bat_priv
->vis_send_list
);
884 hash_added
= batadv_hash_add(bat_priv
->vis_hash
, batadv_vis_info_cmp
,
885 batadv_vis_info_choose
,
886 bat_priv
->my_vis_info
,
887 &bat_priv
->my_vis_info
->hash_entry
);
888 if (hash_added
!= 0) {
889 pr_err("Can't add own vis packet into hash\n");
890 /* not in hash, need to remove it manually. */
891 kref_put(&bat_priv
->my_vis_info
->refcount
, batadv_free_info
);
895 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
896 batadv_start_vis_timer(bat_priv
);
900 kfree(bat_priv
->my_vis_info
);
901 bat_priv
->my_vis_info
= NULL
;
903 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
904 batadv_vis_quit(bat_priv
);
908 /* Decrease the reference count on a hash item info */
909 static void batadv_free_info_ref(struct hlist_node
*node
, void *arg
)
911 struct vis_info
*info
;
913 info
= container_of(node
, struct vis_info
, hash_entry
);
914 batadv_send_list_del(info
);
915 kref_put(&info
->refcount
, batadv_free_info
);
918 /* shutdown vis-server */
919 void batadv_vis_quit(struct bat_priv
*bat_priv
)
921 if (!bat_priv
->vis_hash
)
924 cancel_delayed_work_sync(&bat_priv
->vis_work
);
926 spin_lock_bh(&bat_priv
->vis_hash_lock
);
927 /* properly remove, kill timers ... */
928 batadv_hash_delete(bat_priv
->vis_hash
, batadv_free_info_ref
, NULL
);
929 bat_priv
->vis_hash
= NULL
;
930 bat_priv
->my_vis_info
= NULL
;
931 spin_unlock_bh(&bat_priv
->vis_hash_lock
);
934 /* schedule packets for (re)transmission */
935 static void batadv_start_vis_timer(struct bat_priv
*bat_priv
)
937 INIT_DELAYED_WORK(&bat_priv
->vis_work
, batadv_send_vis_packets
);
938 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->vis_work
,
939 msecs_to_jiffies(VIS_INTERVAL
));