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