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