]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/batman-adv/vis.c
batman-adv: Initialize lockdep class keys for hashes
[karo-tx-linux.git] / net / batman-adv / vis.c
1 /* Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
2  *
3  * Simon Wunderlich
4  *
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.
8  *
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.
13  *
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
18  */
19
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"
28
29 #define BATADV_MAX_VIS_PACKET_SIZE 1000
30
31 /* hash class keys */
32 static struct lock_class_key batadv_vis_hash_lock_class_key;
33
34 static void batadv_start_vis_timer(struct batadv_priv *bat_priv);
35
36 /* free the info */
37 static void batadv_free_info(struct kref *ref)
38 {
39         struct batadv_vis_info *info;
40         struct batadv_priv *bat_priv;
41         struct batadv_recvlist_node *entry, *tmp;
42
43         info = container_of(ref, struct batadv_vis_info, refcount);
44         bat_priv = info->bat_priv;
45
46         list_del_init(&info->send_list);
47         spin_lock_bh(&bat_priv->vis.list_lock);
48         list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
49                 list_del(&entry->list);
50                 kfree(entry);
51         }
52
53         spin_unlock_bh(&bat_priv->vis.list_lock);
54         kfree_skb(info->skb_packet);
55         kfree(info);
56 }
57
58 /* Compare two vis packets, used by the hashing algorithm */
59 static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
60 {
61         const struct batadv_vis_info *d1, *d2;
62         const struct batadv_vis_packet *p1, *p2;
63
64         d1 = container_of(node, struct batadv_vis_info, hash_entry);
65         d2 = data2;
66         p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
67         p2 = (struct batadv_vis_packet *)d2->skb_packet->data;
68         return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
69 }
70
71 /* hash function to choose an entry in a hash table of given size
72  * hash algorithm from http://en.wikipedia.org/wiki/Hash_table
73  */
74 static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
75 {
76         const struct batadv_vis_info *vis_info = data;
77         const struct batadv_vis_packet *packet;
78         const unsigned char *key;
79         uint32_t hash = 0;
80         size_t i;
81
82         packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
83         key = packet->vis_orig;
84         for (i = 0; i < ETH_ALEN; i++) {
85                 hash += key[i];
86                 hash += (hash << 10);
87                 hash ^= (hash >> 6);
88         }
89
90         hash += (hash << 3);
91         hash ^= (hash >> 11);
92         hash += (hash << 15);
93
94         return hash % size;
95 }
96
97 static struct batadv_vis_info *
98 batadv_vis_hash_find(struct batadv_priv *bat_priv, const void *data)
99 {
100         struct batadv_hashtable *hash = bat_priv->vis.hash;
101         struct hlist_head *head;
102         struct hlist_node *node;
103         struct batadv_vis_info *vis_info, *vis_info_tmp = NULL;
104         uint32_t index;
105
106         if (!hash)
107                 return NULL;
108
109         index = batadv_vis_info_choose(data, hash->size);
110         head = &hash->table[index];
111
112         rcu_read_lock();
113         hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
114                 if (!batadv_vis_info_cmp(node, data))
115                         continue;
116
117                 vis_info_tmp = vis_info;
118                 break;
119         }
120         rcu_read_unlock();
121
122         return vis_info_tmp;
123 }
124
125 /* insert interface to the list of interfaces of one originator, if it
126  * does not already exist in the list
127  */
128 static void batadv_vis_data_insert_interface(const uint8_t *interface,
129                                              struct hlist_head *if_list,
130                                              bool primary)
131 {
132         struct batadv_if_list_entry *entry;
133         struct hlist_node *pos;
134
135         hlist_for_each_entry(entry, pos, if_list, list) {
136                 if (batadv_compare_eth(entry->addr, interface))
137                         return;
138         }
139
140         /* it's a new address, add it to the list */
141         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
142         if (!entry)
143                 return;
144         memcpy(entry->addr, interface, ETH_ALEN);
145         entry->primary = primary;
146         hlist_add_head(&entry->list, if_list);
147 }
148
149 static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
150                                           const struct hlist_head *if_list)
151 {
152         struct batadv_if_list_entry *entry;
153         struct hlist_node *pos;
154
155         hlist_for_each_entry(entry, pos, if_list, list) {
156                 if (entry->primary)
157                         seq_printf(seq, "PRIMARY, ");
158                 else
159                         seq_printf(seq,  "SEC %pM, ", entry->addr);
160         }
161 }
162
163 /* read an entry  */
164 static ssize_t
165 batadv_vis_data_read_entry(struct seq_file *seq,
166                            const struct batadv_vis_info_entry *entry,
167                            const uint8_t *src, bool primary)
168 {
169         if (primary && entry->quality == 0)
170                 return seq_printf(seq, "TT %pM, ", entry->dest);
171         else if (batadv_compare_eth(entry->src, src))
172                 return seq_printf(seq, "TQ %pM %d, ", entry->dest,
173                                   entry->quality);
174
175         return 0;
176 }
177
178 static void
179 batadv_vis_data_insert_interfaces(struct hlist_head *list,
180                                   struct batadv_vis_packet *packet,
181                                   struct batadv_vis_info_entry *entries)
182 {
183         int i;
184
185         for (i = 0; i < packet->entries; i++) {
186                 if (entries[i].quality == 0)
187                         continue;
188
189                 if (batadv_compare_eth(entries[i].src, packet->vis_orig))
190                         continue;
191
192                 batadv_vis_data_insert_interface(entries[i].src, list, false);
193         }
194 }
195
196 static void batadv_vis_data_read_entries(struct seq_file *seq,
197                                          struct hlist_head *list,
198                                          struct batadv_vis_packet *packet,
199                                          struct batadv_vis_info_entry *entries)
200 {
201         int i;
202         struct batadv_if_list_entry *entry;
203         struct hlist_node *pos;
204
205         hlist_for_each_entry(entry, pos, list, list) {
206                 seq_printf(seq, "%pM,", entry->addr);
207
208                 for (i = 0; i < packet->entries; i++)
209                         batadv_vis_data_read_entry(seq, &entries[i],
210                                                    entry->addr, entry->primary);
211
212                 /* add primary/secondary records */
213                 if (batadv_compare_eth(entry->addr, packet->vis_orig))
214                         batadv_vis_data_read_prim_sec(seq, list);
215
216                 seq_printf(seq, "\n");
217         }
218 }
219
220 static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
221                                              const struct hlist_head *head)
222 {
223         struct hlist_node *node;
224         struct batadv_vis_info *info;
225         struct batadv_vis_packet *packet;
226         uint8_t *entries_pos;
227         struct batadv_vis_info_entry *entries;
228         struct batadv_if_list_entry *entry;
229         struct hlist_node *pos, *n;
230
231         HLIST_HEAD(vis_if_list);
232
233         hlist_for_each_entry_rcu(info, node, head, hash_entry) {
234                 packet = (struct batadv_vis_packet *)info->skb_packet->data;
235                 entries_pos = (uint8_t *)packet + sizeof(*packet);
236                 entries = (struct batadv_vis_info_entry *)entries_pos;
237
238                 batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
239                                                  true);
240                 batadv_vis_data_insert_interfaces(&vis_if_list, packet,
241                                                   entries);
242                 batadv_vis_data_read_entries(seq, &vis_if_list, packet,
243                                              entries);
244
245                 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
246                         hlist_del(&entry->list);
247                         kfree(entry);
248                 }
249         }
250 }
251
252 int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
253 {
254         struct batadv_hard_iface *primary_if;
255         struct hlist_head *head;
256         struct net_device *net_dev = (struct net_device *)seq->private;
257         struct batadv_priv *bat_priv = netdev_priv(net_dev);
258         struct batadv_hashtable *hash = bat_priv->vis.hash;
259         uint32_t i;
260         int ret = 0;
261         int vis_server = atomic_read(&bat_priv->vis_mode);
262
263         primary_if = batadv_primary_if_get_selected(bat_priv);
264         if (!primary_if)
265                 goto out;
266
267         if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
268                 goto out;
269
270         spin_lock_bh(&bat_priv->vis.hash_lock);
271         for (i = 0; i < hash->size; i++) {
272                 head = &hash->table[i];
273                 batadv_vis_seq_print_text_bucket(seq, head);
274         }
275         spin_unlock_bh(&bat_priv->vis.hash_lock);
276
277 out:
278         if (primary_if)
279                 batadv_hardif_free_ref(primary_if);
280         return ret;
281 }
282
283 /* add the info packet to the send list, if it was not
284  * already linked in.
285  */
286 static void batadv_send_list_add(struct batadv_priv *bat_priv,
287                                  struct batadv_vis_info *info)
288 {
289         if (list_empty(&info->send_list)) {
290                 kref_get(&info->refcount);
291                 list_add_tail(&info->send_list, &bat_priv->vis.send_list);
292         }
293 }
294
295 /* delete the info packet from the send list, if it was
296  * linked in.
297  */
298 static void batadv_send_list_del(struct batadv_vis_info *info)
299 {
300         if (!list_empty(&info->send_list)) {
301                 list_del_init(&info->send_list);
302                 kref_put(&info->refcount, batadv_free_info);
303         }
304 }
305
306 /* tries to add one entry to the receive list. */
307 static void batadv_recv_list_add(struct batadv_priv *bat_priv,
308                                  struct list_head *recv_list, const char *mac)
309 {
310         struct batadv_recvlist_node *entry;
311
312         entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
313         if (!entry)
314                 return;
315
316         memcpy(entry->mac, mac, ETH_ALEN);
317         spin_lock_bh(&bat_priv->vis.list_lock);
318         list_add_tail(&entry->list, recv_list);
319         spin_unlock_bh(&bat_priv->vis.list_lock);
320 }
321
322 /* returns 1 if this mac is in the recv_list */
323 static int batadv_recv_list_is_in(struct batadv_priv *bat_priv,
324                                   const struct list_head *recv_list,
325                                   const char *mac)
326 {
327         const struct batadv_recvlist_node *entry;
328
329         spin_lock_bh(&bat_priv->vis.list_lock);
330         list_for_each_entry(entry, recv_list, list) {
331                 if (batadv_compare_eth(entry->mac, mac)) {
332                         spin_unlock_bh(&bat_priv->vis.list_lock);
333                         return 1;
334                 }
335         }
336         spin_unlock_bh(&bat_priv->vis.list_lock);
337         return 0;
338 }
339
340 /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
341  * broken.. ).  vis hash must be locked outside.  is_new is set when the packet
342  * is newer than old entries in the hash.
343  */
344 static struct batadv_vis_info *
345 batadv_add_packet(struct batadv_priv *bat_priv,
346                   struct batadv_vis_packet *vis_packet, int vis_info_len,
347                   int *is_new, int make_broadcast)
348 {
349         struct batadv_vis_info *info, *old_info;
350         struct batadv_vis_packet *search_packet, *old_packet;
351         struct batadv_vis_info search_elem;
352         struct batadv_vis_packet *packet;
353         struct sk_buff *tmp_skb;
354         int hash_added;
355         size_t len;
356         size_t max_entries;
357
358         *is_new = 0;
359         /* sanity check */
360         if (!bat_priv->vis.hash)
361                 return NULL;
362
363         /* see if the packet is already in vis_hash */
364         search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
365         if (!search_elem.skb_packet)
366                 return NULL;
367         len = sizeof(*search_packet);
368         tmp_skb = search_elem.skb_packet;
369         search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
370
371         memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
372         old_info = batadv_vis_hash_find(bat_priv, &search_elem);
373         kfree_skb(search_elem.skb_packet);
374
375         if (old_info) {
376                 tmp_skb = old_info->skb_packet;
377                 old_packet = (struct batadv_vis_packet *)tmp_skb->data;
378                 if (!batadv_seq_after(ntohl(vis_packet->seqno),
379                                       ntohl(old_packet->seqno))) {
380                         if (old_packet->seqno == vis_packet->seqno) {
381                                 batadv_recv_list_add(bat_priv,
382                                                      &old_info->recv_list,
383                                                      vis_packet->sender_orig);
384                                 return old_info;
385                         } else {
386                                 /* newer packet is already in hash. */
387                                 return NULL;
388                         }
389                 }
390                 /* remove old entry */
391                 batadv_hash_remove(bat_priv->vis.hash, batadv_vis_info_cmp,
392                                    batadv_vis_info_choose, old_info);
393                 batadv_send_list_del(old_info);
394                 kref_put(&old_info->refcount, batadv_free_info);
395         }
396
397         info = kmalloc(sizeof(*info), GFP_ATOMIC);
398         if (!info)
399                 return NULL;
400
401         len = sizeof(*packet) + vis_info_len;
402         info->skb_packet = dev_alloc_skb(len + ETH_HLEN + NET_IP_ALIGN);
403         if (!info->skb_packet) {
404                 kfree(info);
405                 return NULL;
406         }
407         skb_reserve(info->skb_packet, ETH_HLEN + NET_IP_ALIGN);
408         packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
409
410         kref_init(&info->refcount);
411         INIT_LIST_HEAD(&info->send_list);
412         INIT_LIST_HEAD(&info->recv_list);
413         info->first_seen = jiffies;
414         info->bat_priv = bat_priv;
415         memcpy(packet, vis_packet, len);
416
417         /* initialize and add new packet. */
418         *is_new = 1;
419
420         /* Make it a broadcast packet, if required */
421         if (make_broadcast)
422                 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
423
424         /* repair if entries is longer than packet. */
425         max_entries = vis_info_len / sizeof(struct batadv_vis_info_entry);
426         if (packet->entries > max_entries)
427                 packet->entries = max_entries;
428
429         batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
430
431         /* try to add it */
432         hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
433                                      batadv_vis_info_choose, info,
434                                      &info->hash_entry);
435         if (hash_added != 0) {
436                 /* did not work (for some reason) */
437                 kref_put(&info->refcount, batadv_free_info);
438                 info = NULL;
439         }
440
441         return info;
442 }
443
444 /* handle the server sync packet, forward if needed. */
445 void batadv_receive_server_sync_packet(struct batadv_priv *bat_priv,
446                                        struct batadv_vis_packet *vis_packet,
447                                        int vis_info_len)
448 {
449         struct batadv_vis_info *info;
450         int is_new, make_broadcast;
451         int vis_server = atomic_read(&bat_priv->vis_mode);
452
453         make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
454
455         spin_lock_bh(&bat_priv->vis.hash_lock);
456         info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
457                                  &is_new, make_broadcast);
458         if (!info)
459                 goto end;
460
461         /* only if we are server ourselves and packet is newer than the one in
462          * hash.
463          */
464         if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
465                 batadv_send_list_add(bat_priv, info);
466 end:
467         spin_unlock_bh(&bat_priv->vis.hash_lock);
468 }
469
470 /* handle an incoming client update packet and schedule forward if needed. */
471 void batadv_receive_client_update_packet(struct batadv_priv *bat_priv,
472                                          struct batadv_vis_packet *vis_packet,
473                                          int vis_info_len)
474 {
475         struct batadv_vis_info *info;
476         struct batadv_vis_packet *packet;
477         int is_new;
478         int vis_server = atomic_read(&bat_priv->vis_mode);
479         int are_target = 0;
480
481         /* clients shall not broadcast. */
482         if (is_broadcast_ether_addr(vis_packet->target_orig))
483                 return;
484
485         /* Are we the target for this VIS packet? */
486         if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC   &&
487             batadv_is_my_mac(vis_packet->target_orig))
488                 are_target = 1;
489
490         spin_lock_bh(&bat_priv->vis.hash_lock);
491         info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
492                                  &is_new, are_target);
493
494         if (!info)
495                 goto end;
496         /* note that outdated packets will be dropped at this point. */
497
498         packet = (struct batadv_vis_packet *)info->skb_packet->data;
499
500         /* send only if we're the target server or ... */
501         if (are_target && is_new) {
502                 packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
503                 batadv_send_list_add(bat_priv, info);
504
505                 /* ... we're not the recipient (and thus need to forward). */
506         } else if (!batadv_is_my_mac(packet->target_orig)) {
507                 batadv_send_list_add(bat_priv, info);
508         }
509
510 end:
511         spin_unlock_bh(&bat_priv->vis.hash_lock);
512 }
513
514 /* Walk the originators and find the VIS server with the best tq. Set the packet
515  * address to its address and return the best_tq.
516  *
517  * Must be called with the originator hash locked
518  */
519 static int batadv_find_best_vis_server(struct batadv_priv *bat_priv,
520                                        struct batadv_vis_info *info)
521 {
522         struct batadv_hashtable *hash = bat_priv->orig_hash;
523         struct batadv_neigh_node *router;
524         struct hlist_node *node;
525         struct hlist_head *head;
526         struct batadv_orig_node *orig_node;
527         struct batadv_vis_packet *packet;
528         int best_tq = -1;
529         uint32_t i;
530
531         packet = (struct batadv_vis_packet *)info->skb_packet->data;
532
533         for (i = 0; i < hash->size; i++) {
534                 head = &hash->table[i];
535
536                 rcu_read_lock();
537                 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
538                         router = batadv_orig_node_get_router(orig_node);
539                         if (!router)
540                                 continue;
541
542                         if ((orig_node->flags & BATADV_VIS_SERVER) &&
543                             (router->tq_avg > best_tq)) {
544                                 best_tq = router->tq_avg;
545                                 memcpy(packet->target_orig, orig_node->orig,
546                                        ETH_ALEN);
547                         }
548                         batadv_neigh_node_free_ref(router);
549                 }
550                 rcu_read_unlock();
551         }
552
553         return best_tq;
554 }
555
556 /* Return true if the vis packet is full. */
557 static bool batadv_vis_packet_full(const struct batadv_vis_info *info)
558 {
559         const struct batadv_vis_packet *packet;
560         size_t num;
561
562         packet = (struct batadv_vis_packet *)info->skb_packet->data;
563         num = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct batadv_vis_info_entry);
564
565         if (num < packet->entries + 1)
566                 return true;
567         return false;
568 }
569
570 /* generates a packet of own vis data,
571  * returns 0 on success, -1 if no packet could be generated
572  */
573 static int batadv_generate_vis_packet(struct batadv_priv *bat_priv)
574 {
575         struct batadv_hashtable *hash = bat_priv->orig_hash;
576         struct hlist_node *node;
577         struct hlist_head *head;
578         struct batadv_orig_node *orig_node;
579         struct batadv_neigh_node *router;
580         struct batadv_vis_info *info = bat_priv->vis.my_info;
581         struct batadv_vis_packet *packet;
582         struct batadv_vis_info_entry *entry;
583         struct batadv_tt_common_entry *tt_common_entry;
584         uint8_t *packet_pos;
585         int best_tq = -1;
586         uint32_t i;
587
588         info->first_seen = jiffies;
589         packet = (struct batadv_vis_packet *)info->skb_packet->data;
590         packet->vis_type = atomic_read(&bat_priv->vis_mode);
591
592         memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
593         packet->header.ttl = BATADV_TTL;
594         packet->seqno = htonl(ntohl(packet->seqno) + 1);
595         packet->entries = 0;
596         packet->reserved = 0;
597         skb_trim(info->skb_packet, sizeof(*packet));
598
599         if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
600                 best_tq = batadv_find_best_vis_server(bat_priv, info);
601
602                 if (best_tq < 0)
603                         return best_tq;
604         }
605
606         for (i = 0; i < hash->size; i++) {
607                 head = &hash->table[i];
608
609                 rcu_read_lock();
610                 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
611                         router = batadv_orig_node_get_router(orig_node);
612                         if (!router)
613                                 continue;
614
615                         if (!batadv_compare_eth(router->addr, orig_node->orig))
616                                 goto next;
617
618                         if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
619                                 goto next;
620
621                         if (router->tq_avg < 1)
622                                 goto next;
623
624                         /* fill one entry into buffer. */
625                         packet_pos = skb_put(info->skb_packet, sizeof(*entry));
626                         entry = (struct batadv_vis_info_entry *)packet_pos;
627                         memcpy(entry->src,
628                                router->if_incoming->net_dev->dev_addr,
629                                ETH_ALEN);
630                         memcpy(entry->dest, orig_node->orig, ETH_ALEN);
631                         entry->quality = router->tq_avg;
632                         packet->entries++;
633
634 next:
635                         batadv_neigh_node_free_ref(router);
636
637                         if (batadv_vis_packet_full(info))
638                                 goto unlock;
639                 }
640                 rcu_read_unlock();
641         }
642
643         hash = bat_priv->tt.local_hash;
644
645         for (i = 0; i < hash->size; i++) {
646                 head = &hash->table[i];
647
648                 rcu_read_lock();
649                 hlist_for_each_entry_rcu(tt_common_entry, node, head,
650                                          hash_entry) {
651                         packet_pos = skb_put(info->skb_packet, sizeof(*entry));
652                         entry = (struct batadv_vis_info_entry *)packet_pos;
653                         memset(entry->src, 0, ETH_ALEN);
654                         memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
655                         entry->quality = 0; /* 0 means TT */
656                         packet->entries++;
657
658                         if (batadv_vis_packet_full(info))
659                                 goto unlock;
660                 }
661                 rcu_read_unlock();
662         }
663
664         return 0;
665
666 unlock:
667         rcu_read_unlock();
668         return 0;
669 }
670
671 /* free old vis packets. Must be called with this vis_hash_lock
672  * held
673  */
674 static void batadv_purge_vis_packets(struct batadv_priv *bat_priv)
675 {
676         uint32_t i;
677         struct batadv_hashtable *hash = bat_priv->vis.hash;
678         struct hlist_node *node, *node_tmp;
679         struct hlist_head *head;
680         struct batadv_vis_info *info;
681
682         for (i = 0; i < hash->size; i++) {
683                 head = &hash->table[i];
684
685                 hlist_for_each_entry_safe(info, node, node_tmp,
686                                           head, hash_entry) {
687                         /* never purge own data. */
688                         if (info == bat_priv->vis.my_info)
689                                 continue;
690
691                         if (batadv_has_timed_out(info->first_seen,
692                                                  BATADV_VIS_TIMEOUT)) {
693                                 hlist_del(node);
694                                 batadv_send_list_del(info);
695                                 kref_put(&info->refcount, batadv_free_info);
696                         }
697                 }
698         }
699 }
700
701 static void batadv_broadcast_vis_packet(struct batadv_priv *bat_priv,
702                                         struct batadv_vis_info *info)
703 {
704         struct batadv_hashtable *hash = bat_priv->orig_hash;
705         struct hlist_node *node;
706         struct hlist_head *head;
707         struct batadv_orig_node *orig_node;
708         struct batadv_vis_packet *packet;
709         struct sk_buff *skb;
710         uint32_t i;
711
712
713         packet = (struct batadv_vis_packet *)info->skb_packet->data;
714
715         /* send to all routers in range. */
716         for (i = 0; i < hash->size; i++) {
717                 head = &hash->table[i];
718
719                 rcu_read_lock();
720                 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
721                         /* if it's a vis server and reachable, send it. */
722                         if (!(orig_node->flags & BATADV_VIS_SERVER))
723                                 continue;
724
725                         /* don't send it if we already received the packet from
726                          * this node.
727                          */
728                         if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
729                                                    orig_node->orig))
730                                 continue;
731
732                         memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
733                         skb = skb_clone(info->skb_packet, GFP_ATOMIC);
734                         if (!skb)
735                                 continue;
736
737                         if (!batadv_send_skb_to_orig(skb, orig_node, NULL))
738                                 kfree_skb(skb);
739                 }
740                 rcu_read_unlock();
741         }
742 }
743
744 static void batadv_unicast_vis_packet(struct batadv_priv *bat_priv,
745                                       struct batadv_vis_info *info)
746 {
747         struct batadv_orig_node *orig_node;
748         struct sk_buff *skb;
749         struct batadv_vis_packet *packet;
750
751         packet = (struct batadv_vis_packet *)info->skb_packet->data;
752
753         orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
754         if (!orig_node)
755                 goto out;
756
757         skb = skb_clone(info->skb_packet, GFP_ATOMIC);
758         if (!skb)
759                 goto out;
760
761         if (!batadv_send_skb_to_orig(skb, orig_node, NULL))
762                 kfree_skb(skb);
763
764 out:
765         if (orig_node)
766                 batadv_orig_node_free_ref(orig_node);
767 }
768
769 /* only send one vis packet. called from batadv_send_vis_packets() */
770 static void batadv_send_vis_packet(struct batadv_priv *bat_priv,
771                                    struct batadv_vis_info *info)
772 {
773         struct batadv_hard_iface *primary_if;
774         struct batadv_vis_packet *packet;
775
776         primary_if = batadv_primary_if_get_selected(bat_priv);
777         if (!primary_if)
778                 goto out;
779
780         packet = (struct batadv_vis_packet *)info->skb_packet->data;
781         if (packet->header.ttl < 2) {
782                 pr_debug("Error - can't send vis packet: ttl exceeded\n");
783                 goto out;
784         }
785
786         memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
787         packet->header.ttl--;
788
789         if (is_broadcast_ether_addr(packet->target_orig))
790                 batadv_broadcast_vis_packet(bat_priv, info);
791         else
792                 batadv_unicast_vis_packet(bat_priv, info);
793         packet->header.ttl++; /* restore TTL */
794
795 out:
796         if (primary_if)
797                 batadv_hardif_free_ref(primary_if);
798 }
799
800 /* called from timer; send (and maybe generate) vis packet. */
801 static void batadv_send_vis_packets(struct work_struct *work)
802 {
803         struct delayed_work *delayed_work;
804         struct batadv_priv *bat_priv;
805         struct batadv_priv_vis *priv_vis;
806         struct batadv_vis_info *info;
807
808         delayed_work = container_of(work, struct delayed_work, work);
809         priv_vis = container_of(delayed_work, struct batadv_priv_vis, work);
810         bat_priv = container_of(priv_vis, struct batadv_priv, vis);
811         spin_lock_bh(&bat_priv->vis.hash_lock);
812         batadv_purge_vis_packets(bat_priv);
813
814         if (batadv_generate_vis_packet(bat_priv) == 0) {
815                 /* schedule if generation was successful */
816                 batadv_send_list_add(bat_priv, bat_priv->vis.my_info);
817         }
818
819         while (!list_empty(&bat_priv->vis.send_list)) {
820                 info = list_first_entry(&bat_priv->vis.send_list,
821                                         typeof(*info), send_list);
822
823                 kref_get(&info->refcount);
824                 spin_unlock_bh(&bat_priv->vis.hash_lock);
825
826                 batadv_send_vis_packet(bat_priv, info);
827
828                 spin_lock_bh(&bat_priv->vis.hash_lock);
829                 batadv_send_list_del(info);
830                 kref_put(&info->refcount, batadv_free_info);
831         }
832         spin_unlock_bh(&bat_priv->vis.hash_lock);
833         batadv_start_vis_timer(bat_priv);
834 }
835
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)
838  */
839 int batadv_vis_init(struct batadv_priv *bat_priv)
840 {
841         struct batadv_vis_packet *packet;
842         int hash_added;
843         unsigned int len;
844         unsigned long first_seen;
845         struct sk_buff *tmp_skb;
846
847         if (bat_priv->vis.hash)
848                 return 0;
849
850         spin_lock_bh(&bat_priv->vis.hash_lock);
851
852         bat_priv->vis.hash = batadv_hash_new(256);
853         if (!bat_priv->vis.hash) {
854                 pr_err("Can't initialize vis_hash\n");
855                 goto err;
856         }
857
858         batadv_hash_set_lock_class(bat_priv->vis.hash,
859                                    &batadv_vis_hash_lock_class_key);
860
861         bat_priv->vis.my_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
862         if (!bat_priv->vis.my_info)
863                 goto err;
864
865         len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE;
866         len += ETH_HLEN + NET_IP_ALIGN;
867         bat_priv->vis.my_info->skb_packet = dev_alloc_skb(len);
868         if (!bat_priv->vis.my_info->skb_packet)
869                 goto free_info;
870
871         skb_reserve(bat_priv->vis.my_info->skb_packet, ETH_HLEN + NET_IP_ALIGN);
872         tmp_skb = bat_priv->vis.my_info->skb_packet;
873         packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
874
875         /* prefill the vis info */
876         first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
877         bat_priv->vis.my_info->first_seen = first_seen;
878         INIT_LIST_HEAD(&bat_priv->vis.my_info->recv_list);
879         INIT_LIST_HEAD(&bat_priv->vis.my_info->send_list);
880         kref_init(&bat_priv->vis.my_info->refcount);
881         bat_priv->vis.my_info->bat_priv = bat_priv;
882         packet->header.version = BATADV_COMPAT_VERSION;
883         packet->header.packet_type = BATADV_VIS;
884         packet->header.ttl = BATADV_TTL;
885         packet->seqno = 0;
886         packet->reserved = 0;
887         packet->entries = 0;
888
889         INIT_LIST_HEAD(&bat_priv->vis.send_list);
890
891         hash_added = batadv_hash_add(bat_priv->vis.hash, batadv_vis_info_cmp,
892                                      batadv_vis_info_choose,
893                                      bat_priv->vis.my_info,
894                                      &bat_priv->vis.my_info->hash_entry);
895         if (hash_added != 0) {
896                 pr_err("Can't add own vis packet into hash\n");
897                 /* not in hash, need to remove it manually. */
898                 kref_put(&bat_priv->vis.my_info->refcount, batadv_free_info);
899                 goto err;
900         }
901
902         spin_unlock_bh(&bat_priv->vis.hash_lock);
903         batadv_start_vis_timer(bat_priv);
904         return 0;
905
906 free_info:
907         kfree(bat_priv->vis.my_info);
908         bat_priv->vis.my_info = NULL;
909 err:
910         spin_unlock_bh(&bat_priv->vis.hash_lock);
911         batadv_vis_quit(bat_priv);
912         return -ENOMEM;
913 }
914
915 /* Decrease the reference count on a hash item info */
916 static void batadv_free_info_ref(struct hlist_node *node, void *arg)
917 {
918         struct batadv_vis_info *info;
919
920         info = container_of(node, struct batadv_vis_info, hash_entry);
921         batadv_send_list_del(info);
922         kref_put(&info->refcount, batadv_free_info);
923 }
924
925 /* shutdown vis-server */
926 void batadv_vis_quit(struct batadv_priv *bat_priv)
927 {
928         if (!bat_priv->vis.hash)
929                 return;
930
931         cancel_delayed_work_sync(&bat_priv->vis.work);
932
933         spin_lock_bh(&bat_priv->vis.hash_lock);
934         /* properly remove, kill timers ... */
935         batadv_hash_delete(bat_priv->vis.hash, batadv_free_info_ref, NULL);
936         bat_priv->vis.hash = NULL;
937         bat_priv->vis.my_info = NULL;
938         spin_unlock_bh(&bat_priv->vis.hash_lock);
939 }
940
941 /* schedule packets for (re)transmission */
942 static void batadv_start_vis_timer(struct batadv_priv *bat_priv)
943 {
944         INIT_DELAYED_WORK(&bat_priv->vis.work, batadv_send_vis_packets);
945         queue_delayed_work(batadv_event_workqueue, &bat_priv->vis.work,
946                            msecs_to_jiffies(BATADV_VIS_INTERVAL));
947 }