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
;
135 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
137 len
+= sprintf(buff
+ len
, "PRIMARY, ");
139 len
+= sprintf(buff
+ len
, "SEC %pM, ", entry
->addr
);
146 static size_t vis_data_count_prim_sec(struct hlist_head
*if_list
)
148 struct if_list_entry
*entry
;
149 struct hlist_node
*pos
;
152 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
163 static ssize_t
vis_data_read_entry(char *buff
, struct vis_info_entry
*entry
,
164 uint8_t *src
, bool primary
)
166 /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
167 if (primary
&& entry
->quality
== 0)
168 return sprintf(buff
, "HNA %pM, ", entry
->dest
);
169 else if (compare_orig(entry
->src
, src
))
170 return sprintf(buff
, "TQ %pM %d, ", entry
->dest
,
176 int vis_seq_print_text(struct seq_file
*seq
, void *offset
)
179 HASHIT(hashit_count
);
180 struct vis_info
*info
;
181 struct vis_packet
*packet
;
182 struct vis_info_entry
*entries
;
183 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
184 struct bat_priv
*bat_priv
= netdev_priv(net_dev
);
185 HLIST_HEAD(vis_if_list
);
186 struct if_list_entry
*entry
;
187 struct hlist_node
*pos
, *n
;
190 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
191 size_t buff_pos
, buf_size
;
194 if ((!bat_priv
->primary_if
) ||
195 (vis_server
== VIS_TYPE_CLIENT_UPDATE
))
199 /* Estimate length */
200 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
201 while (hash_iterate(bat_priv
->vis_hash
, &hashit_count
)) {
202 info
= hashit_count
.bucket
->data
;
203 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
204 entries
= (struct vis_info_entry
*)
205 ((char *)packet
+ sizeof(struct vis_packet
));
207 for (i
= 0; i
< packet
->entries
; i
++) {
208 if (entries
[i
].quality
== 0)
210 vis_data_insert_interface(entries
[i
].src
, &vis_if_list
,
211 compare_orig(entries
[i
].src
, packet
->vis_orig
));
214 hlist_for_each_entry(entry
, pos
, &vis_if_list
, list
) {
215 buf_size
+= 18 + 26 * packet
->entries
;
217 /* add primary/secondary records */
218 if (compare_orig(entry
->addr
, packet
->vis_orig
))
220 vis_data_count_prim_sec(&vis_if_list
);
225 hlist_for_each_entry_safe(entry
, pos
, n
, &vis_if_list
, list
) {
226 hlist_del(&entry
->list
);
231 buff
= kmalloc(buf_size
, GFP_ATOMIC
);
233 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
239 while (hash_iterate(bat_priv
->vis_hash
, &hashit
)) {
240 info
= hashit
.bucket
->data
;
241 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
242 entries
= (struct vis_info_entry
*)
243 ((char *)packet
+ sizeof(struct vis_packet
));
245 for (i
= 0; i
< packet
->entries
; i
++) {
246 if (entries
[i
].quality
== 0)
248 vis_data_insert_interface(entries
[i
].src
, &vis_if_list
,
249 compare_orig(entries
[i
].src
, packet
->vis_orig
));
252 hlist_for_each_entry(entry
, pos
, &vis_if_list
, list
) {
253 buff_pos
+= sprintf(buff
+ buff_pos
, "%pM,",
256 for (i
= 0; i
< packet
->entries
; i
++)
257 buff_pos
+= vis_data_read_entry(buff
+ buff_pos
,
262 /* add primary/secondary records */
263 if (compare_orig(entry
->addr
, packet
->vis_orig
))
265 vis_data_read_prim_sec(buff
+ buff_pos
,
268 buff_pos
+= sprintf(buff
+ buff_pos
, "\n");
271 hlist_for_each_entry_safe(entry
, pos
, n
, &vis_if_list
, list
) {
272 hlist_del(&entry
->list
);
277 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
279 seq_printf(seq
, "%s", buff
);
285 /* add the info packet to the send list, if it was not
286 * already linked in. */
287 static void send_list_add(struct bat_priv
*bat_priv
, struct 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
297 static void send_list_del(struct vis_info
*info
)
299 if (!list_empty(&info
->send_list
)) {
300 list_del_init(&info
->send_list
);
301 kref_put(&info
->refcount
, free_info
);
305 /* tries to add one entry to the receive list. */
306 static void recv_list_add(struct bat_priv
*bat_priv
,
307 struct list_head
*recv_list
, char *mac
)
309 struct recvlist_node
*entry
;
312 entry
= kmalloc(sizeof(struct recvlist_node
), GFP_ATOMIC
);
316 memcpy(entry
->mac
, mac
, ETH_ALEN
);
317 spin_lock_irqsave(&bat_priv
->vis_list_lock
, flags
);
318 list_add_tail(&entry
->list
, recv_list
);
319 spin_unlock_irqrestore(&bat_priv
->vis_list_lock
, flags
);
322 /* returns 1 if this mac is in the recv_list */
323 static int recv_list_is_in(struct bat_priv
*bat_priv
,
324 struct list_head
*recv_list
, char *mac
)
326 struct recvlist_node
*entry
;
329 spin_lock_irqsave(&bat_priv
->vis_list_lock
, flags
);
330 list_for_each_entry(entry
, recv_list
, list
) {
331 if (memcmp(entry
->mac
, mac
, ETH_ALEN
) == 0) {
332 spin_unlock_irqrestore(&bat_priv
->vis_list_lock
,
337 spin_unlock_irqrestore(&bat_priv
->vis_list_lock
, flags
);
341 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
342 * broken.. ). vis hash must be locked outside. is_new is set when the packet
343 * is newer than old entries in the hash. */
344 static struct vis_info
*add_packet(struct bat_priv
*bat_priv
,
345 struct vis_packet
*vis_packet
,
346 int vis_info_len
, int *is_new
,
349 struct vis_info
*info
, *old_info
;
350 struct vis_packet
*search_packet
, *old_packet
;
351 struct vis_info search_elem
;
352 struct vis_packet
*packet
;
356 if (!bat_priv
->vis_hash
)
359 /* see if the packet is already in vis_hash */
360 search_elem
.skb_packet
= dev_alloc_skb(sizeof(struct vis_packet
));
361 if (!search_elem
.skb_packet
)
363 search_packet
= (struct vis_packet
*)skb_put(search_elem
.skb_packet
,
364 sizeof(struct vis_packet
));
366 memcpy(search_packet
->vis_orig
, vis_packet
->vis_orig
, ETH_ALEN
);
367 old_info
= hash_find(bat_priv
->vis_hash
, &search_elem
);
368 kfree_skb(search_elem
.skb_packet
);
370 if (old_info
!= NULL
) {
371 old_packet
= (struct vis_packet
*)old_info
->skb_packet
->data
;
372 if (!seq_after(ntohl(vis_packet
->seqno
),
373 ntohl(old_packet
->seqno
))) {
374 if (old_packet
->seqno
== vis_packet
->seqno
) {
375 recv_list_add(bat_priv
, &old_info
->recv_list
,
376 vis_packet
->sender_orig
);
379 /* newer packet is already in hash. */
383 /* remove old entry */
384 hash_remove(bat_priv
->vis_hash
, old_info
);
385 send_list_del(old_info
);
386 kref_put(&old_info
->refcount
, free_info
);
389 info
= kmalloc(sizeof(struct vis_info
), GFP_ATOMIC
);
393 info
->skb_packet
= dev_alloc_skb(sizeof(struct vis_packet
) +
394 vis_info_len
+ sizeof(struct ethhdr
));
395 if (!info
->skb_packet
) {
399 skb_reserve(info
->skb_packet
, sizeof(struct ethhdr
));
400 packet
= (struct vis_packet
*)skb_put(info
->skb_packet
,
401 sizeof(struct vis_packet
) +
404 kref_init(&info
->refcount
);
405 INIT_LIST_HEAD(&info
->send_list
);
406 INIT_LIST_HEAD(&info
->recv_list
);
407 info
->first_seen
= jiffies
;
408 info
->bat_priv
= bat_priv
;
409 memcpy(packet
, vis_packet
, sizeof(struct vis_packet
) + vis_info_len
);
411 /* initialize and add new packet. */
414 /* Make it a broadcast packet, if required */
416 memcpy(packet
->target_orig
, broadcast_addr
, ETH_ALEN
);
418 /* repair if entries is longer than packet. */
419 if (packet
->entries
* sizeof(struct vis_info_entry
) > vis_info_len
)
420 packet
->entries
= vis_info_len
/ sizeof(struct vis_info_entry
);
422 recv_list_add(bat_priv
, &info
->recv_list
, packet
->sender_orig
);
425 if (hash_add(bat_priv
->vis_hash
, info
) < 0) {
426 /* did not work (for some reason) */
427 kref_put(&old_info
->refcount
, free_info
);
434 /* handle the server sync packet, forward if needed. */
435 void receive_server_sync_packet(struct bat_priv
*bat_priv
,
436 struct vis_packet
*vis_packet
,
439 struct vis_info
*info
;
440 int is_new
, make_broadcast
;
442 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
444 make_broadcast
= (vis_server
== VIS_TYPE_SERVER_SYNC
);
446 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
447 info
= add_packet(bat_priv
, vis_packet
, vis_info_len
,
448 &is_new
, make_broadcast
);
452 /* only if we are server ourselves and packet is newer than the one in
454 if (vis_server
== VIS_TYPE_SERVER_SYNC
&& is_new
)
455 send_list_add(bat_priv
, info
);
457 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
460 /* handle an incoming client update packet and schedule forward if needed. */
461 void receive_client_update_packet(struct bat_priv
*bat_priv
,
462 struct vis_packet
*vis_packet
,
465 struct vis_info
*info
;
466 struct vis_packet
*packet
;
469 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
472 /* clients shall not broadcast. */
473 if (is_bcast(vis_packet
->target_orig
))
476 /* Are we the target for this VIS packet? */
477 if (vis_server
== VIS_TYPE_SERVER_SYNC
&&
478 is_my_mac(vis_packet
->target_orig
))
481 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
482 info
= add_packet(bat_priv
, vis_packet
, vis_info_len
,
483 &is_new
, are_target
);
487 /* note that outdated packets will be dropped at this point. */
489 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
491 /* send only if we're the target server or ... */
492 if (are_target
&& is_new
) {
493 packet
->vis_type
= VIS_TYPE_SERVER_SYNC
; /* upgrade! */
494 send_list_add(bat_priv
, info
);
496 /* ... we're not the recipient (and thus need to forward). */
497 } else if (!is_my_mac(packet
->target_orig
)) {
498 send_list_add(bat_priv
, info
);
502 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
505 /* Walk the originators and find the VIS server with the best tq. Set the packet
506 * address to its address and return the best_tq.
508 * Must be called with the originator hash locked */
509 static int find_best_vis_server(struct bat_priv
*bat_priv
,
510 struct vis_info
*info
)
513 struct orig_node
*orig_node
;
514 struct vis_packet
*packet
;
517 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
519 while (hash_iterate(bat_priv
->orig_hash
, &hashit
)) {
520 orig_node
= hashit
.bucket
->data
;
521 if ((orig_node
) && (orig_node
->router
) &&
522 (orig_node
->flags
& VIS_SERVER
) &&
523 (orig_node
->router
->tq_avg
> best_tq
)) {
524 best_tq
= orig_node
->router
->tq_avg
;
525 memcpy(packet
->target_orig
, orig_node
->orig
, ETH_ALEN
);
531 /* Return true if the vis packet is full. */
532 static bool vis_packet_full(struct vis_info
*info
)
534 struct vis_packet
*packet
;
535 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
537 if (MAX_VIS_PACKET_SIZE
/ sizeof(struct vis_info_entry
)
538 < packet
->entries
+ 1)
543 /* generates a packet of own vis data,
544 * returns 0 on success, -1 if no packet could be generated */
545 static int generate_vis_packet(struct bat_priv
*bat_priv
)
547 HASHIT(hashit_local
);
548 HASHIT(hashit_global
);
549 struct orig_node
*orig_node
;
550 struct vis_info
*info
= (struct vis_info
*)bat_priv
->my_vis_info
;
551 struct vis_packet
*packet
= (struct vis_packet
*)info
->skb_packet
->data
;
552 struct vis_info_entry
*entry
;
553 struct hna_local_entry
*hna_local_entry
;
557 info
->first_seen
= jiffies
;
558 packet
->vis_type
= atomic_read(&bat_priv
->vis_mode
);
560 spin_lock_irqsave(&bat_priv
->orig_hash_lock
, flags
);
561 memcpy(packet
->target_orig
, broadcast_addr
, ETH_ALEN
);
563 packet
->seqno
= htonl(ntohl(packet
->seqno
) + 1);
565 skb_trim(info
->skb_packet
, sizeof(struct vis_packet
));
567 if (packet
->vis_type
== VIS_TYPE_CLIENT_UPDATE
) {
568 best_tq
= find_best_vis_server(bat_priv
, info
);
571 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
,
577 while (hash_iterate(bat_priv
->orig_hash
, &hashit_global
)) {
578 orig_node
= hashit_global
.bucket
->data
;
580 if (!orig_node
->router
)
583 if (!compare_orig(orig_node
->router
->addr
, orig_node
->orig
))
586 if (orig_node
->router
->if_incoming
->if_status
!= IF_ACTIVE
)
589 if (orig_node
->router
->tq_avg
< 1)
592 /* fill one entry into buffer. */
593 entry
= (struct vis_info_entry
*)
594 skb_put(info
->skb_packet
, sizeof(*entry
));
596 orig_node
->router
->if_incoming
->net_dev
->dev_addr
,
598 memcpy(entry
->dest
, orig_node
->orig
, ETH_ALEN
);
599 entry
->quality
= orig_node
->router
->tq_avg
;
602 if (vis_packet_full(info
)) {
603 spin_unlock_irqrestore(
604 &bat_priv
->orig_hash_lock
, flags
);
609 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
611 spin_lock_irqsave(&bat_priv
->hna_lhash_lock
, flags
);
612 while (hash_iterate(bat_priv
->hna_local_hash
, &hashit_local
)) {
613 hna_local_entry
= hashit_local
.bucket
->data
;
614 entry
= (struct vis_info_entry
*)skb_put(info
->skb_packet
,
616 memset(entry
->src
, 0, ETH_ALEN
);
617 memcpy(entry
->dest
, hna_local_entry
->addr
, ETH_ALEN
);
618 entry
->quality
= 0; /* 0 means HNA */
621 if (vis_packet_full(info
)) {
622 spin_unlock_irqrestore(&bat_priv
->hna_lhash_lock
,
628 spin_unlock_irqrestore(&bat_priv
->hna_lhash_lock
, flags
);
632 /* free old vis packets. Must be called with this vis_hash_lock
634 static void purge_vis_packets(struct bat_priv
*bat_priv
)
637 struct vis_info
*info
;
639 while (hash_iterate(bat_priv
->vis_hash
, &hashit
)) {
640 info
= hashit
.bucket
->data
;
642 /* never purge own data. */
643 if (info
== bat_priv
->my_vis_info
)
646 if (time_after(jiffies
,
647 info
->first_seen
+ VIS_TIMEOUT
* HZ
)) {
648 hash_remove_bucket(bat_priv
->vis_hash
, &hashit
);
650 kref_put(&info
->refcount
, free_info
);
655 static void broadcast_vis_packet(struct bat_priv
*bat_priv
,
656 struct vis_info
*info
)
659 struct orig_node
*orig_node
;
660 struct vis_packet
*packet
;
663 struct batman_if
*batman_if
;
664 uint8_t dstaddr
[ETH_ALEN
];
667 spin_lock_irqsave(&bat_priv
->orig_hash_lock
, flags
);
668 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
670 /* send to all routers in range. */
671 while (hash_iterate(bat_priv
->orig_hash
, &hashit
)) {
672 orig_node
= hashit
.bucket
->data
;
674 /* if it's a vis server and reachable, send it. */
675 if ((!orig_node
) || (!orig_node
->router
))
677 if (!(orig_node
->flags
& VIS_SERVER
))
679 /* don't send it if we already received the packet from
681 if (recv_list_is_in(bat_priv
, &info
->recv_list
,
685 memcpy(packet
->target_orig
, orig_node
->orig
, ETH_ALEN
);
686 batman_if
= orig_node
->router
->if_incoming
;
687 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
688 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
690 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
692 send_skb_packet(skb
, batman_if
, dstaddr
);
694 spin_lock_irqsave(&bat_priv
->orig_hash_lock
, flags
);
698 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
701 static void unicast_vis_packet(struct bat_priv
*bat_priv
,
702 struct vis_info
*info
)
704 struct orig_node
*orig_node
;
706 struct vis_packet
*packet
;
708 struct batman_if
*batman_if
;
709 uint8_t dstaddr
[ETH_ALEN
];
711 spin_lock_irqsave(&bat_priv
->orig_hash_lock
, flags
);
712 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
713 orig_node
= ((struct orig_node
*)hash_find(bat_priv
->orig_hash
,
714 packet
->target_orig
));
716 if ((!orig_node
) || (!orig_node
->router
))
719 /* don't lock while sending the packets ... we therefore
720 * copy the required data before sending */
721 batman_if
= orig_node
->router
->if_incoming
;
722 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
723 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
725 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
727 send_skb_packet(skb
, batman_if
, dstaddr
);
732 spin_unlock_irqrestore(&bat_priv
->orig_hash_lock
, flags
);
735 /* only send one vis packet. called from send_vis_packets() */
736 static void send_vis_packet(struct bat_priv
*bat_priv
, struct vis_info
*info
)
738 struct vis_packet
*packet
;
740 packet
= (struct vis_packet
*)info
->skb_packet
->data
;
741 if (packet
->ttl
< 2) {
742 pr_debug("Error - can't send vis packet: ttl exceeded\n");
746 memcpy(packet
->sender_orig
, bat_priv
->primary_if
->net_dev
->dev_addr
,
750 if (is_bcast(packet
->target_orig
))
751 broadcast_vis_packet(bat_priv
, info
);
753 unicast_vis_packet(bat_priv
, info
);
754 packet
->ttl
++; /* restore TTL */
757 /* called from timer; send (and maybe generate) vis packet. */
758 static void send_vis_packets(struct work_struct
*work
)
760 struct delayed_work
*delayed_work
=
761 container_of(work
, struct delayed_work
, work
);
762 struct bat_priv
*bat_priv
=
763 container_of(delayed_work
, struct bat_priv
, vis_work
);
764 struct vis_info
*info
, *temp
;
767 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
768 purge_vis_packets(bat_priv
);
770 if (generate_vis_packet(bat_priv
) == 0) {
771 /* schedule if generation was successful */
772 send_list_add(bat_priv
, bat_priv
->my_vis_info
);
775 list_for_each_entry_safe(info
, temp
, &bat_priv
->vis_send_list
,
778 kref_get(&info
->refcount
);
779 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
781 if (bat_priv
->primary_if
)
782 send_vis_packet(bat_priv
, info
);
784 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
786 kref_put(&info
->refcount
, free_info
);
788 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
789 start_vis_timer(bat_priv
);
792 /* init the vis server. this may only be called when if_list is already
793 * initialized (e.g. bat0 is initialized, interfaces have been added) */
794 int vis_init(struct bat_priv
*bat_priv
)
796 struct vis_packet
*packet
;
799 if (bat_priv
->vis_hash
)
802 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
804 bat_priv
->vis_hash
= hash_new(256, vis_info_cmp
, vis_info_choose
);
805 if (!bat_priv
->vis_hash
) {
806 pr_err("Can't initialize vis_hash\n");
810 bat_priv
->my_vis_info
= kmalloc(MAX_VIS_PACKET_SIZE
, GFP_ATOMIC
);
811 if (!bat_priv
->my_vis_info
) {
812 pr_err("Can't initialize vis packet\n");
816 bat_priv
->my_vis_info
->skb_packet
= dev_alloc_skb(
817 sizeof(struct vis_packet
) +
818 MAX_VIS_PACKET_SIZE
+
819 sizeof(struct ethhdr
));
820 if (!bat_priv
->my_vis_info
->skb_packet
)
823 skb_reserve(bat_priv
->my_vis_info
->skb_packet
, sizeof(struct ethhdr
));
824 packet
= (struct vis_packet
*)skb_put(
825 bat_priv
->my_vis_info
->skb_packet
,
826 sizeof(struct vis_packet
));
828 /* prefill the vis info */
829 bat_priv
->my_vis_info
->first_seen
= jiffies
-
830 msecs_to_jiffies(VIS_INTERVAL
);
831 INIT_LIST_HEAD(&bat_priv
->my_vis_info
->recv_list
);
832 INIT_LIST_HEAD(&bat_priv
->my_vis_info
->send_list
);
833 kref_init(&bat_priv
->my_vis_info
->refcount
);
834 bat_priv
->my_vis_info
->bat_priv
= bat_priv
;
835 packet
->version
= COMPAT_VERSION
;
836 packet
->packet_type
= BAT_VIS
;
841 INIT_LIST_HEAD(&bat_priv
->vis_send_list
);
843 if (hash_add(bat_priv
->vis_hash
, bat_priv
->my_vis_info
) < 0) {
844 pr_err("Can't add own vis packet into hash\n");
845 /* not in hash, need to remove it manually. */
846 kref_put(&bat_priv
->my_vis_info
->refcount
, free_info
);
850 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
851 start_vis_timer(bat_priv
);
855 kfree(bat_priv
->my_vis_info
);
856 bat_priv
->my_vis_info
= NULL
;
858 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
863 /* Decrease the reference count on a hash item info */
864 static void free_info_ref(void *data
, void *arg
)
866 struct vis_info
*info
= data
;
869 kref_put(&info
->refcount
, free_info
);
872 /* shutdown vis-server */
873 void vis_quit(struct bat_priv
*bat_priv
)
876 if (!bat_priv
->vis_hash
)
879 cancel_delayed_work_sync(&bat_priv
->vis_work
);
881 spin_lock_irqsave(&bat_priv
->vis_hash_lock
, flags
);
882 /* properly remove, kill timers ... */
883 hash_delete(bat_priv
->vis_hash
, free_info_ref
, NULL
);
884 bat_priv
->vis_hash
= NULL
;
885 bat_priv
->my_vis_info
= NULL
;
886 spin_unlock_irqrestore(&bat_priv
->vis_hash_lock
, flags
);
889 /* schedule packets for (re)transmission */
890 static void start_vis_timer(struct bat_priv
*bat_priv
)
892 INIT_DELAYED_WORK(&bat_priv
->vis_work
, send_vis_packets
);
893 queue_delayed_work(bat_event_workqueue
, &bat_priv
->vis_work
,
894 msecs_to_jiffies(VIS_INTERVAL
));