Merge tag 'staging-3.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6.git] / net / batman-adv / vis.c
blob4983340f1943c5a0cc491785bf835981e7c44a1a
1 /* Copyright (C) 2008-2013 B.A.T.M.A.N. contributors:
3 * Simon Wunderlich
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
17 * 02110-1301, USA
20 #include "main.h"
21 #include "send.h"
22 #include "translation-table.h"
23 #include "vis.h"
24 #include "soft-interface.h"
25 #include "hard-interface.h"
26 #include "hash.h"
27 #include "originator.h"
29 #define BATADV_MAX_VIS_PACKET_SIZE 1000
31 /* hash class keys */
32 static struct lock_class_key batadv_vis_hash_lock_class_key;
34 /* free the info */
35 static void batadv_free_info(struct kref *ref)
37 struct batadv_vis_info *info;
38 struct batadv_priv *bat_priv;
39 struct batadv_vis_recvlist_node *entry, *tmp;
41 info = container_of(ref, struct batadv_vis_info, refcount);
42 bat_priv = info->bat_priv;
44 list_del_init(&info->send_list);
45 spin_lock_bh(&bat_priv->vis.list_lock);
46 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
47 list_del(&entry->list);
48 kfree(entry);
51 spin_unlock_bh(&bat_priv->vis.list_lock);
52 kfree_skb(info->skb_packet);
53 kfree(info);
56 /* Compare two vis packets, used by the hashing algorithm */
57 static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
59 const struct batadv_vis_info *d1, *d2;
60 const struct batadv_vis_packet *p1, *p2;
62 d1 = container_of(node, struct batadv_vis_info, hash_entry);
63 d2 = data2;
64 p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
65 p2 = (struct batadv_vis_packet *)d2->skb_packet->data;
66 return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
69 /* hash function to choose an entry in a hash table of given size
70 * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
72 static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
74 const struct batadv_vis_info *vis_info = data;
75 const struct batadv_vis_packet *packet;
76 const unsigned char *key;
77 uint32_t hash = 0;
78 size_t i;
80 packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
81 key = packet->vis_orig;
82 for (i = 0; i < ETH_ALEN; i++) {
83 hash += key[i];
84 hash += (hash << 10);
85 hash ^= (hash >> 6);
88 hash += (hash << 3);
89 hash ^= (hash >> 11);
90 hash += (hash << 15);
92 return hash % size;
95 static struct batadv_vis_info *
96 batadv_vis_hash_find(struct batadv_priv *bat_priv, const void *data)
98 struct batadv_hashtable *hash = bat_priv->vis.hash;
99 struct hlist_head *head;
100 struct batadv_vis_info *vis_info, *vis_info_tmp = NULL;
101 uint32_t index;
103 if (!hash)
104 return NULL;
106 index = batadv_vis_info_choose(data, hash->size);
107 head = &hash->table[index];
109 rcu_read_lock();
110 hlist_for_each_entry_rcu(vis_info, head, hash_entry) {
111 if (!batadv_vis_info_cmp(&vis_info->hash_entry, data))
112 continue;
114 vis_info_tmp = vis_info;
115 break;
117 rcu_read_unlock();
119 return vis_info_tmp;
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,
127 bool primary)
129 struct batadv_vis_if_list_entry *entry;
131 hlist_for_each_entry(entry, if_list, list) {
132 if (batadv_compare_eth(entry->addr, interface))
133 return;
136 /* it's a new address, add it to the list */
137 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
138 if (!entry)
139 return;
140 memcpy(entry->addr, interface, ETH_ALEN);
141 entry->primary = primary;
142 hlist_add_head(&entry->list, if_list);
145 static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
146 const struct hlist_head *if_list)
148 struct batadv_vis_if_list_entry *entry;
150 hlist_for_each_entry(entry, if_list, list) {
151 if (entry->primary)
152 seq_puts(seq, "PRIMARY, ");
153 else
154 seq_printf(seq, "SEC %pM, ", entry->addr);
158 /* read an entry */
159 static ssize_t
160 batadv_vis_data_read_entry(struct seq_file *seq,
161 const struct batadv_vis_info_entry *entry,
162 const uint8_t *src, bool primary)
164 if (primary && entry->quality == 0)
165 return seq_printf(seq, "TT %pM, ", entry->dest);
166 else if (batadv_compare_eth(entry->src, src))
167 return seq_printf(seq, "TQ %pM %d, ", entry->dest,
168 entry->quality);
170 return 0;
173 static void
174 batadv_vis_data_insert_interfaces(struct hlist_head *list,
175 struct batadv_vis_packet *packet,
176 struct batadv_vis_info_entry *entries)
178 int i;
180 for (i = 0; i < packet->entries; i++) {
181 if (entries[i].quality == 0)
182 continue;
184 if (batadv_compare_eth(entries[i].src, packet->vis_orig))
185 continue;
187 batadv_vis_data_insert_interface(entries[i].src, list, false);
191 static void batadv_vis_data_read_entries(struct seq_file *seq,
192 struct hlist_head *list,
193 struct batadv_vis_packet *packet,
194 struct batadv_vis_info_entry *entries)
196 int i;
197 struct batadv_vis_if_list_entry *entry;
199 hlist_for_each_entry(entry, list, list) {
200 seq_printf(seq, "%pM,", entry->addr);
202 for (i = 0; i < packet->entries; i++)
203 batadv_vis_data_read_entry(seq, &entries[i],
204 entry->addr, entry->primary);
206 /* add primary/secondary records */
207 if (batadv_compare_eth(entry->addr, packet->vis_orig))
208 batadv_vis_data_read_prim_sec(seq, list);
210 seq_puts(seq, "\n");
214 static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
215 const struct hlist_head *head)
217 struct batadv_vis_info *info;
218 struct batadv_vis_packet *packet;
219 uint8_t *entries_pos;
220 struct batadv_vis_info_entry *entries;
221 struct batadv_vis_if_list_entry *entry;
222 struct hlist_node *n;
224 HLIST_HEAD(vis_if_list);
226 hlist_for_each_entry_rcu(info, head, hash_entry) {
227 packet = (struct batadv_vis_packet *)info->skb_packet->data;
228 entries_pos = (uint8_t *)packet + sizeof(*packet);
229 entries = (struct batadv_vis_info_entry *)entries_pos;
231 batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
232 true);
233 batadv_vis_data_insert_interfaces(&vis_if_list, packet,
234 entries);
235 batadv_vis_data_read_entries(seq, &vis_if_list, packet,
236 entries);
238 hlist_for_each_entry_safe(entry, n, &vis_if_list, list) {
239 hlist_del(&entry->list);
240 kfree(entry);
245 int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
247 struct batadv_hard_iface *primary_if;
248 struct hlist_head *head;
249 struct net_device *net_dev = (struct net_device *)seq->private;
250 struct batadv_priv *bat_priv = netdev_priv(net_dev);
251 struct batadv_hashtable *hash = bat_priv->vis.hash;
252 uint32_t i;
253 int ret = 0;
254 int vis_server = atomic_read(&bat_priv->vis_mode);
256 primary_if = batadv_primary_if_get_selected(bat_priv);
257 if (!primary_if)
258 goto out;
260 if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
261 goto out;
263 spin_lock_bh(&bat_priv->vis.hash_lock);
264 for (i = 0; i < hash->size; i++) {
265 head = &hash->table[i];
266 batadv_vis_seq_print_text_bucket(seq, head);
268 spin_unlock_bh(&bat_priv->vis.hash_lock);
270 out:
271 if (primary_if)
272 batadv_hardif_free_ref(primary_if);
273 return ret;
276 /* add the info packet to the send list, if it was not
277 * already linked in.
279 static void batadv_send_list_add(struct batadv_priv *bat_priv,
280 struct batadv_vis_info *info)
282 if (list_empty(&info->send_list)) {
283 kref_get(&info->refcount);
284 list_add_tail(&info->send_list, &bat_priv->vis.send_list);
288 /* delete the info packet from the send list, if it was
289 * linked in.
291 static void batadv_send_list_del(struct batadv_vis_info *info)
293 if (!list_empty(&info->send_list)) {
294 list_del_init(&info->send_list);
295 kref_put(&info->refcount, batadv_free_info);
299 /* tries to add one entry to the receive list. */
300 static void batadv_recv_list_add(struct batadv_priv *bat_priv,
301 struct list_head *recv_list, const char *mac)
303 struct batadv_vis_recvlist_node *entry;
305 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
306 if (!entry)
307 return;
309 memcpy(entry->mac, mac, ETH_ALEN);
310 spin_lock_bh(&bat_priv->vis.list_lock);
311 list_add_tail(&entry->list, recv_list);
312 spin_unlock_bh(&bat_priv->vis.list_lock);
315 /* returns 1 if this mac is in the recv_list */
316 static int batadv_recv_list_is_in(struct batadv_priv *bat_priv,
317 const struct list_head *recv_list,
318 const char *mac)
320 const struct batadv_vis_recvlist_node *entry;
322 spin_lock_bh(&bat_priv->vis.list_lock);
323 list_for_each_entry(entry, recv_list, list) {
324 if (batadv_compare_eth(entry->mac, mac)) {
325 spin_unlock_bh(&bat_priv->vis.list_lock);
326 return 1;
329 spin_unlock_bh(&bat_priv->vis.list_lock);
330 return 0;
333 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
334 * broken.. ). vis hash must be locked outside. is_new is set when the packet
335 * is newer than old entries in the hash.
337 static struct batadv_vis_info *
338 batadv_add_packet(struct batadv_priv *bat_priv,
339 struct batadv_vis_packet *vis_packet, int vis_info_len,
340 int *is_new, int make_broadcast)
342 struct batadv_vis_info *info, *old_info;
343 struct batadv_vis_packet *search_packet, *old_packet;
344 struct batadv_vis_info search_elem;
345 struct batadv_vis_packet *packet;
346 struct sk_buff *tmp_skb;
347 int hash_added;
348 size_t len;
349 size_t max_entries;
351 *is_new = 0;
352 /* sanity check */
353 if (!bat_priv->vis.hash)
354 return NULL;
356 /* see if the packet is already in vis_hash */
357 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
358 if (!search_elem.skb_packet)
359 return NULL;
360 len = sizeof(*search_packet);
361 tmp_skb = search_elem.skb_packet;
362 search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
364 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
365 old_info = batadv_vis_hash_find(bat_priv, &search_elem);
366 kfree_skb(search_elem.skb_packet);
368 if (old_info) {
369 tmp_skb = old_info->skb_packet;
370 old_packet = (struct batadv_vis_packet *)tmp_skb->data;
371 if (!batadv_seq_after(ntohl(vis_packet->seqno),
372 ntohl(old_packet->seqno))) {
373 if (old_packet->seqno == vis_packet->seqno) {
374 batadv_recv_list_add(bat_priv,
375 &old_info->recv_list,
376 vis_packet->sender_orig);
377 return old_info;
378 } else {
379 /* newer packet is already in hash. */
380 return NULL;
383 /* remove old entry */
384 batadv_hash_remove(bat_priv->vis.hash, batadv_vis_info_cmp,
385 batadv_vis_info_choose, old_info);
386 batadv_send_list_del(old_info);
387 kref_put(&old_info->refcount, batadv_free_info);
390 info = kmalloc(sizeof(*info), GFP_ATOMIC);
391 if (!info)
392 return NULL;
394 len = sizeof(*packet) + vis_info_len;
395 info->skb_packet = netdev_alloc_skb_ip_align(NULL, len + ETH_HLEN);
396 if (!info->skb_packet) {
397 kfree(info);
398 return NULL;
400 skb_reserve(info->skb_packet, ETH_HLEN);
401 packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
403 kref_init(&info->refcount);
404 INIT_LIST_HEAD(&info->send_list);
405 INIT_LIST_HEAD(&info->recv_list);
406 info->first_seen = jiffies;
407 info->bat_priv = bat_priv;
408 memcpy(packet, vis_packet, len);
410 /* initialize and add new packet. */
411 *is_new = 1;
413 /* Make it a broadcast packet, if required */
414 if (make_broadcast)
415 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
417 /* repair if entries is longer than packet. */
418 max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry);
419 if (packet->entries > max_entries)
420 packet->entries = max_entries;
422 batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
424 /* try to add it */
425 hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
426 batadv_vis_info_choose, info,
427 &info->hash_entry);
428 if (hash_added != 0) {
429 /* did not work (for some reason) */
430 kref_put(&info->refcount, batadv_free_info);
431 info = NULL;
434 return info;
437 /* handle the server sync packet, forward if needed. */
438 void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
439 struct batadv_vis_packet *vis_packet,
440 int vis_info_len)
442 struct batadv_vis_info *info;
443 int is_new, make_broadcast;
444 int vis_server = atomic_read(&bat_priv->vis_mode);
446 make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
448 spin_lock_bh(&bat_priv->vis.hash_lock);
449 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
450 &is_new, make_broadcast);
451 if (!info)
452 goto end;
454 /* only if we are server ourselves and packet is newer than the one in
455 * hash.
457 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
458 batadv_send_list_add(bat_priv, info);
459 end:
460 spin_unlock_bh(&bat_priv->vis.hash_lock);
463 /* handle an incoming client update packet and schedule forward if needed. */
464 void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
465 struct batadv_vis_packet *vis_packet,
466 int vis_info_len)
468 struct batadv_vis_info *info;
469 struct batadv_vis_packet *packet;
470 int is_new;
471 int vis_server = atomic_read(&bat_priv->vis_mode);
472 int are_target = 0;
474 /* clients shall not broadcast. */
475 if (is_broadcast_ether_addr(vis_packet->target_orig))
476 return;
478 /* Are we the target for this VIS packet? */
479 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC &&
480 batadv_is_my_mac(bat_priv, vis_packet->target_orig))
481 are_target = 1;
483 spin_lock_bh(&bat_priv->vis.hash_lock);
484 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
485 &is_new, are_target);
487 if (!info)
488 goto end;
489 /* note that outdated packets will be dropped at this point. */
491 packet = (struct batadv_vis_packet *)info->skb_packet->data;
493 /* send only if we're the target server or ... */
494 if (are_target && is_new) {
495 packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
496 batadv_send_list_add(bat_priv, info);
498 /* ... we're not the recipient (and thus need to forward). */
499 } else if (!batadv_is_my_mac(bat_priv, packet->target_orig)) {
500 batadv_send_list_add(bat_priv, info);
503 end:
504 spin_unlock_bh(&bat_priv->vis.hash_lock);
507 /* Walk the originators and find the VIS server with the best tq. Set the packet
508 * address to its address and return the best_tq.
510 * Must be called with the originator hash locked
512 static int batadv_find_best_vis_server(struct batadv_priv *bat_priv,
513 struct batadv_vis_info *info)
515 struct batadv_hashtable *hash = bat_priv->orig_hash;
516 struct batadv_neigh_node *router;
517 struct hlist_head *head;
518 struct batadv_orig_node *orig_node;
519 struct batadv_vis_packet *packet;
520 int best_tq = -1;
521 uint32_t i;
523 packet = (struct batadv_vis_packet *)info->skb_packet->data;
525 for (i = 0; i < hash->size; i++) {
526 head = &hash->table[i];
528 rcu_read_lock();
529 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
530 router = batadv_orig_node_get_router(orig_node);
531 if (!router)
532 continue;
534 if ((orig_node->flags & BATADV_VIS_SERVER) &&
535 (router->tq_avg > best_tq)) {
536 best_tq = router->tq_avg;
537 memcpy(packet->target_orig, orig_node->orig,
538 ETH_ALEN);
540 batadv_neigh_node_free_ref(router);
542 rcu_read_unlock();
545 return best_tq;
548 /* Return true if the vis packet is full. */
549 static bool batadv_vis_packet_full(const struct batadv_vis_info *info)
551 const struct batadv_vis_packet *packet;
552 size_t num;
554 packet = (struct batadv_vis_packet *)info->skb_packet->data;
555 num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry);
557 if (num < packet->entries + 1)
558 return true;
559 return false;
562 /* generates a packet of own vis data,
563 * returns 0 on success, -1 if no packet could be generated
565 static int batadv_generate_vis_packet(struct batadv_priv *bat_priv)
567 struct batadv_hashtable *hash = bat_priv->orig_hash;
568 struct hlist_head *head;
569 struct batadv_orig_node *orig_node;
570 struct batadv_neigh_node *router;
571 struct batadv_vis_info *info = bat_priv->vis.my_info;
572 struct batadv_vis_packet *packet;
573 struct batadv_vis_info_entry *entry;
574 struct batadv_tt_common_entry *tt_common_entry;
575 uint8_t *packet_pos;
576 int best_tq = -1;
577 uint32_t i;
579 info->first_seen = jiffies;
580 packet = (struct batadv_vis_packet *)info->skb_packet->data;
581 packet->vis_type = atomic_read(&bat_priv->vis_mode);
583 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
584 packet->header.ttl = BATADV_TTL;
585 packet->seqno = htonl(ntohl(packet->seqno) + 1);
586 packet->entries = 0;
587 packet->reserved = 0;
588 skb_trim(info->skb_packet, sizeof(*packet));
590 if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
591 best_tq = batadv_find_best_vis_server(bat_priv, info);
593 if (best_tq < 0)
594 return best_tq;
597 for (i = 0; i < hash->size; i++) {
598 head = &hash->table[i];
600 rcu_read_lock();
601 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
602 router = batadv_orig_node_get_router(orig_node);
603 if (!router)
604 continue;
606 if (!batadv_compare_eth(router->addr, orig_node->orig))
607 goto next;
609 if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
610 goto next;
612 if (router->tq_avg < 1)
613 goto next;
615 /* fill one entry into buffer. */
616 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
617 entry = (struct batadv_vis_info_entry *)packet_pos;
618 memcpy(entry->src,
619 router->if_incoming->net_dev->dev_addr,
620 ETH_ALEN);
621 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
622 entry->quality = router->tq_avg;
623 packet->entries++;
625 next:
626 batadv_neigh_node_free_ref(router);
628 if (batadv_vis_packet_full(info))
629 goto unlock;
631 rcu_read_unlock();
634 hash = bat_priv->tt.local_hash;
636 for (i = 0; i < hash->size; i++) {
637 head = &hash->table[i];
639 rcu_read_lock();
640 hlist_for_each_entry_rcu(tt_common_entry, head,
641 hash_entry) {
642 packet_pos = skb_put(info->skb_packet, sizeof(*entry));
643 entry = (struct batadv_vis_info_entry *)packet_pos;
644 memset(entry->src, 0, ETH_ALEN);
645 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
646 entry->quality = 0; /* 0 means TT */
647 packet->entries++;
649 if (batadv_vis_packet_full(info))
650 goto unlock;
652 rcu_read_unlock();
655 return 0;
657 unlock:
658 rcu_read_unlock();
659 return 0;
662 /* free old vis packets. Must be called with this vis_hash_lock
663 * held
665 static void batadv_purge_vis_packets(struct batadv_priv *bat_priv)
667 uint32_t i;
668 struct batadv_hashtable *hash = bat_priv->vis.hash;
669 struct hlist_node *node_tmp;
670 struct hlist_head *head;
671 struct batadv_vis_info *info;
673 for (i = 0; i < hash->size; i++) {
674 head = &hash->table[i];
676 hlist_for_each_entry_safe(info, node_tmp,
677 head, hash_entry) {
678 /* never purge own data. */
679 if (info == bat_priv->vis.my_info)
680 continue;
682 if (batadv_has_timed_out(info->first_seen,
683 BATADV_VIS_TIMEOUT)) {
684 hlist_del(&info->hash_entry);
685 batadv_send_list_del(info);
686 kref_put(&info->refcount, batadv_free_info);
692 static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv,
693 struct batadv_vis_info *info)
695 struct batadv_hashtable *hash = bat_priv->orig_hash;
696 struct hlist_head *head;
697 struct batadv_orig_node *orig_node;
698 struct batadv_vis_packet *packet;
699 struct sk_buff *skb;
700 uint32_t i, res;
703 packet = (struct batadv_vis_packet *)info->skb_packet->data;
705 /* send to all routers in range. */
706 for (i = 0; i < hash->size; i++) {
707 head = &hash->table[i];
709 rcu_read_lock();
710 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
711 /* if it's a vis server and reachable, send it. */
712 if (!(orig_node->flags & BATADV_VIS_SERVER))
713 continue;
715 /* don't send it if we already received the packet from
716 * this node.
718 if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
719 orig_node->orig))
720 continue;
722 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
723 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
724 if (!skb)
725 continue;
727 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
728 if (res == NET_XMIT_DROP)
729 kfree_skb(skb);
731 rcu_read_unlock();
735 static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv,
736 struct batadv_vis_info *info)
738 struct batadv_orig_node *orig_node;
739 struct sk_buff *skb;
740 struct batadv_vis_packet *packet;
742 packet = (struct batadv_vis_packet *)info->skb_packet->data;
744 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
745 if (!orig_node)
746 goto out;
748 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
749 if (!skb)
750 goto out;
752 if (batadv_send_skb_to_orig(skb, orig_node, NULL) == NET_XMIT_DROP)
753 kfree_skb(skb);
755 out:
756 if (orig_node)
757 batadv_orig_node_free_ref(orig_node);
760 /* only send one vis packet. called from batadv_send_vis_packets() */
761 static void batadv_send_vis_packet(struct batadv_priv *bat_priv,
762 struct batadv_vis_info *info)
764 struct batadv_hard_iface *primary_if;
765 struct batadv_vis_packet *packet;
767 primary_if = batadv_primary_if_get_selected(bat_priv);
768 if (!primary_if)
769 goto out;
771 packet = (struct batadv_vis_packet *)info->skb_packet->data;
772 if (packet->header.ttl < 2) {
773 pr_debug("Error - can't send vis packet: ttl exceeded\n");
774 goto out;
777 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
778 packet->header.ttl--;
780 if (is_broadcast_ether_addr(packet->target_orig))
781 batadv_broadcast_vis_packet(bat_priv, info);
782 else
783 batadv_unicast_vis_packet(bat_priv, info);
784 packet->header.ttl++; /* restore TTL */
786 out:
787 if (primary_if)
788 batadv_hardif_free_ref(primary_if);
791 /* called from timer; send (and maybe generate) vis packet. */
792 static void batadv_send_vis_packets(struct work_struct *work)
794 struct delayed_work *delayed_work;
795 struct batadv_priv *bat_priv;
796 struct batadv_priv_vis *priv_vis;
797 struct batadv_vis_info *info;
799 delayed_work = container_of(work, struct delayed_work, work);
800 priv_vis = container_of(delayed_work, struct batadv_priv_vis, work);
801 bat_priv = container_of(priv_vis, struct batadv_priv, vis);
802 spin_lock_bh(&bat_priv->vis.hash_lock);
803 batadv_purge_vis_packets(bat_priv);
805 if (batadv_generate_vis_packet(bat_priv) == 0) {
806 /* schedule if generation was successful */
807 batadv_send_list_add(bat_priv, bat_priv->vis.my_info);
810 while (!list_empty(&bat_priv->vis.send_list)) {
811 info = list_first_entry(&bat_priv->vis.send_list,
812 typeof(*info), send_list);
814 kref_get(&info->refcount);
815 spin_unlock_bh(&bat_priv->vis.hash_lock);
817 batadv_send_vis_packet(bat_priv, info);
819 spin_lock_bh(&bat_priv->vis.hash_lock);
820 batadv_send_list_del(info);
821 kref_put(&info->refcount, batadv_free_info);
823 spin_unlock_bh(&bat_priv->vis.hash_lock);
825 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
826 msecs_to_jiffies(BATADV_VIS_INTERVAL));
829 /* init the vis server. this may only be called when if_list is already
830 * initialized (e.g. bat0 is initialized, interfaces have been added)
832 int batadv_vis_init(struct batadv_priv *bat_priv)
834 struct batadv_vis_packet *packet;
835 int hash_added;
836 unsigned int len;
837 unsigned long first_seen;
838 struct sk_buff *tmp_skb;
840 if (bat_priv->vis.hash)
841 return 0;
843 spin_lock_bh(&bat_priv->vis.hash_lock);
845 bat_priv->vis.hash = batadv_hash_new(256);
846 if (!bat_priv->vis.hash) {
847 pr_err("Can't initialize vis_hash\n");
848 goto err;
851 batadv_hash_set_lock_class(bat_priv->vis.hash,
852 &batadv_vis_hash_lock_class_key);
854 bat_priv->vis.my_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
855 if (!bat_priv->vis.my_info)
856 goto err;
858 len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE + ETH_HLEN;
859 bat_priv->vis.my_info->skb_packet = netdev_alloc_skb_ip_align(NULL,
860 len);
861 if (!bat_priv->vis.my_info->skb_packet)
862 goto free_info;
864 skb_reserve(bat_priv->vis.my_info->skb_packet, ETH_HLEN);
865 tmp_skb = bat_priv->vis.my_info->skb_packet;
866 packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
868 /* prefill the vis info */
869 first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
870 bat_priv->vis.my_info->first_seen = first_seen;
871 INIT_LIST_HEAD(&bat_priv->vis.my_info->recv_list);
872 INIT_LIST_HEAD(&bat_priv->vis.my_info->send_list);
873 kref_init(&bat_priv->vis.my_info->refcount);
874 bat_priv->vis.my_info->bat_priv = bat_priv;
875 packet->header.version = BATADV_COMPAT_VERSION;
876 packet->header.packet_type = BATADV_VIS;
877 packet->header.ttl = BATADV_TTL;
878 packet->seqno = 0;
879 packet->reserved = 0;
880 packet->entries = 0;
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->vis.my_info,
887 &bat_priv->vis.my_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->vis.my_info->refcount, batadv_free_info);
892 goto err;
895 spin_unlock_bh(&bat_priv->vis.hash_lock);
897 INIT_DELAYED_WORK(&bat_priv->vis.work, batadv_send_vis_packets);
898 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
899 msecs_to_jiffies(BATADV_VIS_INTERVAL));
901 return 0;
903 free_info:
904 kfree(bat_priv->vis.my_info);
905 bat_priv->vis.my_info = NULL;
906 err:
907 spin_unlock_bh(&bat_priv->vis.hash_lock);
908 batadv_vis_quit(bat_priv);
909 return -ENOMEM;
912 /* Decrease the reference count on a hash item info */
913 static void batadv_free_info_ref(struct hlist_node *node, void *arg)
915 struct batadv_vis_info *info;
917 info = container_of(node, struct batadv_vis_info, hash_entry);
918 batadv_send_list_del(info);
919 kref_put(&info->refcount, batadv_free_info);
922 /* shutdown vis-server */
923 void batadv_vis_quit(struct batadv_priv *bat_priv)
925 if (!bat_priv->vis.hash)
926 return;
928 cancel_delayed_work_sync(&bat_priv->vis.work);
930 spin_lock_bh(&bat_priv->vis.hash_lock);
931 /* properly remove, kill timers ... */
932 batadv_hash_delete(bat_priv->vis.hash, batadv_free_info_ref, NULL);
933 bat_priv->vis.hash = NULL;
934 bat_priv->vis.my_info = NULL;
935 spin_unlock_bh(&bat_priv->vis.hash_lock);