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 batadv_priv
*bat_priv
);
34 static void batadv_free_info(struct kref
*ref
)
36 struct batadv_vis_info
*info
;
37 struct batadv_priv
*bat_priv
;
38 struct batadv_recvlist_node
*entry
, *tmp
;
40 info
= container_of(ref
, struct batadv_vis_info
, refcount
);
41 bat_priv
= info
->bat_priv
;
43 list_del_init(&info
->send_list
);
44 spin_lock_bh(&bat_priv
->vis
.list_lock
);
45 list_for_each_entry_safe(entry
, tmp
, &info
->recv_list
, list
) {
46 list_del(&entry
->list
);
50 spin_unlock_bh(&bat_priv
->vis
.list_lock
);
51 kfree_skb(info
->skb_packet
);
55 /* Compare two vis packets, used by the hashing algorithm */
56 static int batadv_vis_info_cmp(const struct hlist_node
*node
, const void *data2
)
58 const struct batadv_vis_info
*d1
, *d2
;
59 const struct batadv_vis_packet
*p1
, *p2
;
61 d1
= container_of(node
, struct batadv_vis_info
, hash_entry
);
63 p1
= (struct batadv_vis_packet
*)d1
->skb_packet
->data
;
64 p2
= (struct batadv_vis_packet
*)d2
->skb_packet
->data
;
65 return batadv_compare_eth(p1
->vis_orig
, p2
->vis_orig
);
68 /* hash function to choose an entry in a hash table of given size
69 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
71 static uint32_t batadv_vis_info_choose(const void *data
, uint32_t size
)
73 const struct batadv_vis_info
*vis_info
= data
;
74 const struct batadv_vis_packet
*packet
;
75 const unsigned char *key
;
79 packet
= (struct batadv_vis_packet
*)vis_info
->skb_packet
->data
;
80 key
= packet
->vis_orig
;
81 for (i
= 0; i
< ETH_ALEN
; i
++) {
94 static struct batadv_vis_info
*
95 batadv_vis_hash_find(struct batadv_priv
*bat_priv
, const void *data
)
97 struct batadv_hashtable
*hash
= bat_priv
->vis
.hash
;
98 struct hlist_head
*head
;
99 struct hlist_node
*node
;
100 struct batadv_vis_info
*vis_info
, *vis_info_tmp
= NULL
;
106 index
= batadv_vis_info_choose(data
, hash
->size
);
107 head
= &hash
->table
[index
];
110 hlist_for_each_entry_rcu(vis_info
, node
, head
, hash_entry
) {
111 if (!batadv_vis_info_cmp(node
, data
))
114 vis_info_tmp
= vis_info
;
122 /* insert interface to the list of interfaces of one originator, if it
123 * does not already exist in the list
125 static void batadv_vis_data_insert_interface(const uint8_t *interface
,
126 struct hlist_head
*if_list
,
129 struct batadv_if_list_entry
*entry
;
130 struct hlist_node
*pos
;
132 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
133 if (batadv_compare_eth(entry
->addr
, interface
))
137 /* it's a new address, add it to the list */
138 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
141 memcpy(entry
->addr
, interface
, ETH_ALEN
);
142 entry
->primary
= primary
;
143 hlist_add_head(&entry
->list
, if_list
);
146 static void batadv_vis_data_read_prim_sec(struct seq_file
*seq
,
147 const struct hlist_head
*if_list
)
149 struct batadv_if_list_entry
*entry
;
150 struct hlist_node
*pos
;
152 hlist_for_each_entry(entry
, pos
, if_list
, list
) {
154 seq_printf(seq
, "PRIMARY, ");
156 seq_printf(seq
, "SEC %pM, ", entry
->addr
);
162 batadv_vis_data_read_entry(struct seq_file
*seq
,
163 const struct batadv_vis_info_entry
*entry
,
164 const uint8_t *src
, bool primary
)
166 if (primary
&& entry
->quality
== 0)
167 return seq_printf(seq
, "TT %pM, ", entry
->dest
);
168 else if (batadv_compare_eth(entry
->src
, src
))
169 return seq_printf(seq
, "TQ %pM %d, ", entry
->dest
,
176 batadv_vis_data_insert_interfaces(struct hlist_head
*list
,
177 struct batadv_vis_packet
*packet
,
178 struct batadv_vis_info_entry
*entries
)
182 for (i
= 0; i
< packet
->entries
; i
++) {
183 if (entries
[i
].quality
== 0)
186 if (batadv_compare_eth(entries
[i
].src
, packet
->vis_orig
))
189 batadv_vis_data_insert_interface(entries
[i
].src
, list
, false);
193 static void batadv_vis_data_read_entries(struct seq_file
*seq
,
194 struct hlist_head
*list
,
195 struct batadv_vis_packet
*packet
,
196 struct batadv_vis_info_entry
*entries
)
199 struct batadv_if_list_entry
*entry
;
200 struct hlist_node
*pos
;
202 hlist_for_each_entry(entry
, pos
, list
, list
) {
203 seq_printf(seq
, "%pM,", entry
->addr
);
205 for (i
= 0; i
< packet
->entries
; i
++)
206 batadv_vis_data_read_entry(seq
, &entries
[i
],
207 entry
->addr
, entry
->primary
);
209 /* add primary/secondary records */
210 if (batadv_compare_eth(entry
->addr
, packet
->vis_orig
))
211 batadv_vis_data_read_prim_sec(seq
, list
);
213 seq_printf(seq
, "\n");
217 static void batadv_vis_seq_print_text_bucket(struct seq_file
*seq
,
218 const struct hlist_head
*head
)
220 struct hlist_node
*node
;
221 struct batadv_vis_info
*info
;
222 struct batadv_vis_packet
*packet
;
223 uint8_t *entries_pos
;
224 struct batadv_vis_info_entry
*entries
;
225 struct batadv_if_list_entry
*entry
;
226 struct hlist_node
*pos
, *n
;
228 HLIST_HEAD(vis_if_list
);
230 hlist_for_each_entry_rcu(info
, node
, head
, hash_entry
) {
231 packet
= (struct batadv_vis_packet
*)info
->skb_packet
->data
;
232 entries_pos
= (uint8_t *)packet
+ sizeof(*packet
);
233 entries
= (struct batadv_vis_info_entry
*)entries_pos
;
235 batadv_vis_data_insert_interface(packet
->vis_orig
, &vis_if_list
,
237 batadv_vis_data_insert_interfaces(&vis_if_list
, packet
,
239 batadv_vis_data_read_entries(seq
, &vis_if_list
, packet
,
242 hlist_for_each_entry_safe(entry
, pos
, n
, &vis_if_list
, list
) {
243 hlist_del(&entry
->list
);
249 int batadv_vis_seq_print_text(struct seq_file
*seq
, void *offset
)
251 struct batadv_hard_iface
*primary_if
;
252 struct hlist_head
*head
;
253 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
254 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
255 struct batadv_hashtable
*hash
= bat_priv
->vis
.hash
;
258 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
260 primary_if
= batadv_primary_if_get_selected(bat_priv
);
264 if (vis_server
== BATADV_VIS_TYPE_CLIENT_UPDATE
)
267 spin_lock_bh(&bat_priv
->vis
.hash_lock
);
268 for (i
= 0; i
< hash
->size
; i
++) {
269 head
= &hash
->table
[i
];
270 batadv_vis_seq_print_text_bucket(seq
, head
);
272 spin_unlock_bh(&bat_priv
->vis
.hash_lock
);
276 batadv_hardif_free_ref(primary_if
);
280 /* add the info packet to the send list, if it was not
283 static void batadv_send_list_add(struct batadv_priv
*bat_priv
,
284 struct batadv_vis_info
*info
)
286 if (list_empty(&info
->send_list
)) {
287 kref_get(&info
->refcount
);
288 list_add_tail(&info
->send_list
, &bat_priv
->vis
.send_list
);
292 /* delete the info packet from the send list, if it was
295 static void batadv_send_list_del(struct batadv_vis_info
*info
)
297 if (!list_empty(&info
->send_list
)) {
298 list_del_init(&info
->send_list
);
299 kref_put(&info
->refcount
, batadv_free_info
);
303 /* tries to add one entry to the receive list. */
304 static void batadv_recv_list_add(struct batadv_priv
*bat_priv
,
305 struct list_head
*recv_list
, const char *mac
)
307 struct batadv_recvlist_node
*entry
;
309 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
313 memcpy(entry
->mac
, mac
, ETH_ALEN
);
314 spin_lock_bh(&bat_priv
->vis
.list_lock
);
315 list_add_tail(&entry
->list
, recv_list
);
316 spin_unlock_bh(&bat_priv
->vis
.list_lock
);
319 /* returns 1 if this mac is in the recv_list */
320 static int batadv_recv_list_is_in(struct batadv_priv
*bat_priv
,
321 const struct list_head
*recv_list
,
324 const struct batadv_recvlist_node
*entry
;
326 spin_lock_bh(&bat_priv
->vis
.list_lock
);
327 list_for_each_entry(entry
, recv_list
, list
) {
328 if (batadv_compare_eth(entry
->mac
, mac
)) {
329 spin_unlock_bh(&bat_priv
->vis
.list_lock
);
333 spin_unlock_bh(&bat_priv
->vis
.list_lock
);
337 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
338 * broken.. ). vis hash must be locked outside. is_new is set when the packet
339 * is newer than old entries in the hash.
341 static struct batadv_vis_info
*
342 batadv_add_packet(struct batadv_priv
*bat_priv
,
343 struct batadv_vis_packet
*vis_packet
, int vis_info_len
,
344 int *is_new
, int make_broadcast
)
346 struct batadv_vis_info
*info
, *old_info
;
347 struct batadv_vis_packet
*search_packet
, *old_packet
;
348 struct batadv_vis_info search_elem
;
349 struct batadv_vis_packet
*packet
;
350 struct sk_buff
*tmp_skb
;
357 if (!bat_priv
->vis
.hash
)
360 /* see if the packet is already in vis_hash */
361 search_elem
.skb_packet
= dev_alloc_skb(sizeof(*search_packet
));
362 if (!search_elem
.skb_packet
)
364 len
= sizeof(*search_packet
);
365 tmp_skb
= search_elem
.skb_packet
;
366 search_packet
= (struct batadv_vis_packet
*)skb_put(tmp_skb
, len
);
368 memcpy(search_packet
->vis_orig
, vis_packet
->vis_orig
, ETH_ALEN
);
369 old_info
= batadv_vis_hash_find(bat_priv
, &search_elem
);
370 kfree_skb(search_elem
.skb_packet
);
373 tmp_skb
= old_info
->skb_packet
;
374 old_packet
= (struct batadv_vis_packet
*)tmp_skb
->data
;
375 if (!batadv_seq_after(ntohl(vis_packet
->seqno
),
376 ntohl(old_packet
->seqno
))) {
377 if (old_packet
->seqno
== vis_packet
->seqno
) {
378 batadv_recv_list_add(bat_priv
,
379 &old_info
->recv_list
,
380 vis_packet
->sender_orig
);
383 /* newer packet is already in hash. */
387 /* remove old entry */
388 batadv_hash_remove(bat_priv
->vis
.hash
, batadv_vis_info_cmp
,
389 batadv_vis_info_choose
, old_info
);
390 batadv_send_list_del(old_info
);
391 kref_put(&old_info
->refcount
, batadv_free_info
);
394 info
= kmalloc(sizeof(*info
), GFP_ATOMIC
);
398 len
= sizeof(*packet
) + vis_info_len
;
399 info
->skb_packet
= dev_alloc_skb(len
+ ETH_HLEN
+ NET_IP_ALIGN
);
400 if (!info
->skb_packet
) {
404 skb_reserve(info
->skb_packet
, ETH_HLEN
+ NET_IP_ALIGN
);
405 packet
= (struct batadv_vis_packet
*)skb_put(info
->skb_packet
, len
);
407 kref_init(&info
->refcount
);
408 INIT_LIST_HEAD(&info
->send_list
);
409 INIT_LIST_HEAD(&info
->recv_list
);
410 info
->first_seen
= jiffies
;
411 info
->bat_priv
= bat_priv
;
412 memcpy(packet
, vis_packet
, len
);
414 /* initialize and add new packet. */
417 /* Make it a broadcast packet, if required */
419 memcpy(packet
->target_orig
, batadv_broadcast_addr
, ETH_ALEN
);
421 /* repair if entries is longer than packet. */
422 max_entries
= vis_info_len
/ sizeof(struct batadv_vis_info_entry
);
423 if (packet
->entries
> max_entries
)
424 packet
->entries
= max_entries
;
426 batadv_recv_list_add(bat_priv
, &info
->recv_list
, packet
->sender_orig
);
429 hash_added
= batadv_hash_add(bat_priv
->vis
.hash
, batadv_vis_info_cmp
,
430 batadv_vis_info_choose
, info
,
432 if (hash_added
!= 0) {
433 /* did not work (for some reason) */
434 kref_put(&info
->refcount
, batadv_free_info
);
441 /* handle the server sync packet, forward if needed. */
442 void batadv_receive_server_sync_packet(struct batadv_priv
*bat_priv
,
443 struct batadv_vis_packet
*vis_packet
,
446 struct batadv_vis_info
*info
;
447 int is_new
, make_broadcast
;
448 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
450 make_broadcast
= (vis_server
== BATADV_VIS_TYPE_SERVER_SYNC
);
452 spin_lock_bh(&bat_priv
->vis
.hash_lock
);
453 info
= batadv_add_packet(bat_priv
, vis_packet
, vis_info_len
,
454 &is_new
, make_broadcast
);
458 /* only if we are server ourselves and packet is newer than the one in
461 if (vis_server
== BATADV_VIS_TYPE_SERVER_SYNC
&& is_new
)
462 batadv_send_list_add(bat_priv
, info
);
464 spin_unlock_bh(&bat_priv
->vis
.hash_lock
);
467 /* handle an incoming client update packet and schedule forward if needed. */
468 void batadv_receive_client_update_packet(struct batadv_priv
*bat_priv
,
469 struct batadv_vis_packet
*vis_packet
,
472 struct batadv_vis_info
*info
;
473 struct batadv_vis_packet
*packet
;
475 int vis_server
= atomic_read(&bat_priv
->vis_mode
);
478 /* clients shall not broadcast. */
479 if (is_broadcast_ether_addr(vis_packet
->target_orig
))
482 /* Are we the target for this VIS packet? */
483 if (vis_server
== BATADV_VIS_TYPE_SERVER_SYNC
&&
484 batadv_is_my_mac(vis_packet
->target_orig
))
487 spin_lock_bh(&bat_priv
->vis
.hash_lock
);
488 info
= batadv_add_packet(bat_priv
, vis_packet
, vis_info_len
,
489 &is_new
, are_target
);
493 /* note that outdated packets will be dropped at this point. */
495 packet
= (struct batadv_vis_packet
*)info
->skb_packet
->data
;
497 /* send only if we're the target server or ... */
498 if (are_target
&& is_new
) {
499 packet
->vis_type
= BATADV_VIS_TYPE_SERVER_SYNC
; /* upgrade! */
500 batadv_send_list_add(bat_priv
, info
);
502 /* ... we're not the recipient (and thus need to forward). */
503 } else if (!batadv_is_my_mac(packet
->target_orig
)) {
504 batadv_send_list_add(bat_priv
, info
);
508 spin_unlock_bh(&bat_priv
->vis
.hash_lock
);
511 /* Walk the originators and find the VIS server with the best tq. Set the packet
512 * address to its address and return the best_tq.
514 * Must be called with the originator hash locked
516 static int batadv_find_best_vis_server(struct batadv_priv
*bat_priv
,
517 struct batadv_vis_info
*info
)
519 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
520 struct batadv_neigh_node
*router
;
521 struct hlist_node
*node
;
522 struct hlist_head
*head
;
523 struct batadv_orig_node
*orig_node
;
524 struct batadv_vis_packet
*packet
;
528 packet
= (struct batadv_vis_packet
*)info
->skb_packet
->data
;
530 for (i
= 0; i
< hash
->size
; i
++) {
531 head
= &hash
->table
[i
];
534 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
535 router
= batadv_orig_node_get_router(orig_node
);
539 if ((orig_node
->flags
& BATADV_VIS_SERVER
) &&
540 (router
->tq_avg
> best_tq
)) {
541 best_tq
= router
->tq_avg
;
542 memcpy(packet
->target_orig
, orig_node
->orig
,
545 batadv_neigh_node_free_ref(router
);
553 /* Return true if the vis packet is full. */
554 static bool batadv_vis_packet_full(const struct batadv_vis_info
*info
)
556 const struct batadv_vis_packet
*packet
;
559 packet
= (struct batadv_vis_packet
*)info
->skb_packet
->data
;
560 num
= BATADV_MAX_VIS_PACKET_SIZE
/ sizeof(struct batadv_vis_info_entry
);
562 if (num
< packet
->entries
+ 1)
567 /* generates a packet of own vis data,
568 * returns 0 on success, -1 if no packet could be generated
570 static int batadv_generate_vis_packet(struct batadv_priv
*bat_priv
)
572 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
573 struct hlist_node
*node
;
574 struct hlist_head
*head
;
575 struct batadv_orig_node
*orig_node
;
576 struct batadv_neigh_node
*router
;
577 struct batadv_vis_info
*info
= bat_priv
->vis
.my_info
;
578 struct batadv_vis_packet
*packet
;
579 struct batadv_vis_info_entry
*entry
;
580 struct batadv_tt_common_entry
*tt_common_entry
;
585 info
->first_seen
= jiffies
;
586 packet
= (struct batadv_vis_packet
*)info
->skb_packet
->data
;
587 packet
->vis_type
= atomic_read(&bat_priv
->vis_mode
);
589 memcpy(packet
->target_orig
, batadv_broadcast_addr
, ETH_ALEN
);
590 packet
->header
.ttl
= BATADV_TTL
;
591 packet
->seqno
= htonl(ntohl(packet
->seqno
) + 1);
593 packet
->reserved
= 0;
594 skb_trim(info
->skb_packet
, sizeof(*packet
));
596 if (packet
->vis_type
== BATADV_VIS_TYPE_CLIENT_UPDATE
) {
597 best_tq
= batadv_find_best_vis_server(bat_priv
, info
);
603 for (i
= 0; i
< hash
->size
; i
++) {
604 head
= &hash
->table
[i
];
607 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
608 router
= batadv_orig_node_get_router(orig_node
);
612 if (!batadv_compare_eth(router
->addr
, orig_node
->orig
))
615 if (router
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
618 if (router
->tq_avg
< 1)
621 /* fill one entry into buffer. */
622 packet_pos
= skb_put(info
->skb_packet
, sizeof(*entry
));
623 entry
= (struct batadv_vis_info_entry
*)packet_pos
;
625 router
->if_incoming
->net_dev
->dev_addr
,
627 memcpy(entry
->dest
, orig_node
->orig
, ETH_ALEN
);
628 entry
->quality
= router
->tq_avg
;
632 batadv_neigh_node_free_ref(router
);
634 if (batadv_vis_packet_full(info
))
640 hash
= bat_priv
->tt
.local_hash
;
642 for (i
= 0; i
< hash
->size
; i
++) {
643 head
= &hash
->table
[i
];
646 hlist_for_each_entry_rcu(tt_common_entry
, node
, head
,
648 packet_pos
= skb_put(info
->skb_packet
, sizeof(*entry
));
649 entry
= (struct batadv_vis_info_entry
*)packet_pos
;
650 memset(entry
->src
, 0, ETH_ALEN
);
651 memcpy(entry
->dest
, tt_common_entry
->addr
, ETH_ALEN
);
652 entry
->quality
= 0; /* 0 means TT */
655 if (batadv_vis_packet_full(info
))
668 /* free old vis packets. Must be called with this vis_hash_lock
671 static void batadv_purge_vis_packets(struct batadv_priv
*bat_priv
)
674 struct batadv_hashtable
*hash
= bat_priv
->vis
.hash
;
675 struct hlist_node
*node
, *node_tmp
;
676 struct hlist_head
*head
;
677 struct batadv_vis_info
*info
;
679 for (i
= 0; i
< hash
->size
; i
++) {
680 head
= &hash
->table
[i
];
682 hlist_for_each_entry_safe(info
, node
, node_tmp
,
684 /* never purge own data. */
685 if (info
== bat_priv
->vis
.my_info
)
688 if (batadv_has_timed_out(info
->first_seen
,
689 BATADV_VIS_TIMEOUT
)) {
691 batadv_send_list_del(info
);
692 kref_put(&info
->refcount
, batadv_free_info
);
698 static void batadv_broadcast_vis_packet(struct batadv_priv
*bat_priv
,
699 struct batadv_vis_info
*info
)
701 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
702 struct hlist_node
*node
;
703 struct hlist_head
*head
;
704 struct batadv_orig_node
*orig_node
;
705 struct batadv_vis_packet
*packet
;
710 packet
= (struct batadv_vis_packet
*)info
->skb_packet
->data
;
712 /* send to all routers in range. */
713 for (i
= 0; i
< hash
->size
; i
++) {
714 head
= &hash
->table
[i
];
717 hlist_for_each_entry_rcu(orig_node
, node
, head
, hash_entry
) {
718 /* if it's a vis server and reachable, send it. */
719 if (!(orig_node
->flags
& BATADV_VIS_SERVER
))
722 /* don't send it if we already received the packet from
725 if (batadv_recv_list_is_in(bat_priv
, &info
->recv_list
,
729 memcpy(packet
->target_orig
, orig_node
->orig
, ETH_ALEN
);
730 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
734 if (!batadv_send_skb_to_orig(skb
, orig_node
, NULL
))
741 static void batadv_unicast_vis_packet(struct batadv_priv
*bat_priv
,
742 struct batadv_vis_info
*info
)
744 struct batadv_orig_node
*orig_node
;
746 struct batadv_vis_packet
*packet
;
748 packet
= (struct batadv_vis_packet
*)info
->skb_packet
->data
;
750 orig_node
= batadv_orig_hash_find(bat_priv
, packet
->target_orig
);
754 skb
= skb_clone(info
->skb_packet
, GFP_ATOMIC
);
758 if (!batadv_send_skb_to_orig(skb
, orig_node
, NULL
))
763 batadv_orig_node_free_ref(orig_node
);
766 /* only send one vis packet. called from batadv_send_vis_packets() */
767 static void batadv_send_vis_packet(struct batadv_priv
*bat_priv
,
768 struct batadv_vis_info
*info
)
770 struct batadv_hard_iface
*primary_if
;
771 struct batadv_vis_packet
*packet
;
773 primary_if
= batadv_primary_if_get_selected(bat_priv
);
777 packet
= (struct batadv_vis_packet
*)info
->skb_packet
->data
;
778 if (packet
->header
.ttl
< 2) {
779 pr_debug("Error - can't send vis packet: ttl exceeded\n");
783 memcpy(packet
->sender_orig
, primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
784 packet
->header
.ttl
--;
786 if (is_broadcast_ether_addr(packet
->target_orig
))
787 batadv_broadcast_vis_packet(bat_priv
, info
);
789 batadv_unicast_vis_packet(bat_priv
, info
);
790 packet
->header
.ttl
++; /* restore TTL */
794 batadv_hardif_free_ref(primary_if
);
797 /* called from timer; send (and maybe generate) vis packet. */
798 static void batadv_send_vis_packets(struct work_struct
*work
)
800 struct delayed_work
*delayed_work
;
801 struct batadv_priv
*bat_priv
;
802 struct batadv_priv_vis
*priv_vis
;
803 struct batadv_vis_info
*info
;
805 delayed_work
= container_of(work
, struct delayed_work
, work
);
806 priv_vis
= container_of(delayed_work
, struct batadv_priv_vis
, work
);
807 bat_priv
= container_of(priv_vis
, struct batadv_priv
, vis
);
808 spin_lock_bh(&bat_priv
->vis
.hash_lock
);
809 batadv_purge_vis_packets(bat_priv
);
811 if (batadv_generate_vis_packet(bat_priv
) == 0) {
812 /* schedule if generation was successful */
813 batadv_send_list_add(bat_priv
, bat_priv
->vis
.my_info
);
816 while (!list_empty(&bat_priv
->vis
.send_list
)) {
817 info
= list_first_entry(&bat_priv
->vis
.send_list
,
818 typeof(*info
), send_list
);
820 kref_get(&info
->refcount
);
821 spin_unlock_bh(&bat_priv
->vis
.hash_lock
);
823 batadv_send_vis_packet(bat_priv
, info
);
825 spin_lock_bh(&bat_priv
->vis
.hash_lock
);
826 batadv_send_list_del(info
);
827 kref_put(&info
->refcount
, batadv_free_info
);
829 spin_unlock_bh(&bat_priv
->vis
.hash_lock
);
830 batadv_start_vis_timer(bat_priv
);
833 /* init the vis server. this may only be called when if_list is already
834 * initialized (e.g. bat0 is initialized, interfaces have been added)
836 int batadv_vis_init(struct batadv_priv
*bat_priv
)
838 struct batadv_vis_packet
*packet
;
841 unsigned long first_seen
;
842 struct sk_buff
*tmp_skb
;
844 if (bat_priv
->vis
.hash
)
847 spin_lock_bh(&bat_priv
->vis
.hash_lock
);
849 bat_priv
->vis
.hash
= batadv_hash_new(256);
850 if (!bat_priv
->vis
.hash
) {
851 pr_err("Can't initialize vis_hash\n");
855 bat_priv
->vis
.my_info
= kmalloc(BATADV_MAX_VIS_PACKET_SIZE
, GFP_ATOMIC
);
856 if (!bat_priv
->vis
.my_info
)
859 len
= sizeof(*packet
) + BATADV_MAX_VIS_PACKET_SIZE
;
860 len
+= ETH_HLEN
+ NET_IP_ALIGN
;
861 bat_priv
->vis
.my_info
->skb_packet
= dev_alloc_skb(len
);
862 if (!bat_priv
->vis
.my_info
->skb_packet
)
865 skb_reserve(bat_priv
->vis
.my_info
->skb_packet
, ETH_HLEN
+ NET_IP_ALIGN
);
866 tmp_skb
= bat_priv
->vis
.my_info
->skb_packet
;
867 packet
= (struct batadv_vis_packet
*)skb_put(tmp_skb
, sizeof(*packet
));
869 /* prefill the vis info */
870 first_seen
= jiffies
- msecs_to_jiffies(BATADV_VIS_INTERVAL
);
871 bat_priv
->vis
.my_info
->first_seen
= first_seen
;
872 INIT_LIST_HEAD(&bat_priv
->vis
.my_info
->recv_list
);
873 INIT_LIST_HEAD(&bat_priv
->vis
.my_info
->send_list
);
874 kref_init(&bat_priv
->vis
.my_info
->refcount
);
875 bat_priv
->vis
.my_info
->bat_priv
= bat_priv
;
876 packet
->header
.version
= BATADV_COMPAT_VERSION
;
877 packet
->header
.packet_type
= BATADV_VIS
;
878 packet
->header
.ttl
= BATADV_TTL
;
880 packet
->reserved
= 0;
883 INIT_LIST_HEAD(&bat_priv
->vis
.send_list
);
885 hash_added
= batadv_hash_add(bat_priv
->vis
.hash
, batadv_vis_info_cmp
,
886 batadv_vis_info_choose
,
887 bat_priv
->vis
.my_info
,
888 &bat_priv
->vis
.my_info
->hash_entry
);
889 if (hash_added
!= 0) {
890 pr_err("Can't add own vis packet into hash\n");
891 /* not in hash, need to remove it manually. */
892 kref_put(&bat_priv
->vis
.my_info
->refcount
, batadv_free_info
);
896 spin_unlock_bh(&bat_priv
->vis
.hash_lock
);
897 batadv_start_vis_timer(bat_priv
);
901 kfree(bat_priv
->vis
.my_info
);
902 bat_priv
->vis
.my_info
= NULL
;
904 spin_unlock_bh(&bat_priv
->vis
.hash_lock
);
905 batadv_vis_quit(bat_priv
);
909 /* Decrease the reference count on a hash item info */
910 static void batadv_free_info_ref(struct hlist_node
*node
, void *arg
)
912 struct batadv_vis_info
*info
;
914 info
= container_of(node
, struct batadv_vis_info
, hash_entry
);
915 batadv_send_list_del(info
);
916 kref_put(&info
->refcount
, batadv_free_info
);
919 /* shutdown vis-server */
920 void batadv_vis_quit(struct batadv_priv
*bat_priv
)
922 if (!bat_priv
->vis
.hash
)
925 cancel_delayed_work_sync(&bat_priv
->vis
.work
);
927 spin_lock_bh(&bat_priv
->vis
.hash_lock
);
928 /* properly remove, kill timers ... */
929 batadv_hash_delete(bat_priv
->vis
.hash
, batadv_free_info_ref
, NULL
);
930 bat_priv
->vis
.hash
= NULL
;
931 bat_priv
->vis
.my_info
= NULL
;
932 spin_unlock_bh(&bat_priv
->vis
.hash_lock
);
935 /* schedule packets for (re)transmission */
936 static void batadv_start_vis_timer(struct batadv_priv
*bat_priv
)
938 INIT_DELAYED_WORK(&bat_priv
->vis
.work
, batadv_send_vis_packets
);
939 queue_delayed_work(batadv_event_workqueue
, &bat_priv
->vis
.work
,
940 msecs_to_jiffies(BATADV_VIS_INTERVAL
));