blob: 411c0e16f911c7773e26afd16832d31b3b837809 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann567db7b2012-01-01 00:41:38 +01002 * Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA
19 *
20 */
21
22#include "main.h"
23#include "send.h"
24#include "translation-table.h"
25#include "vis.h"
26#include "soft-interface.h"
27#include "hard-interface.h"
28#include "hash.h"
29#include "originator.h"
30
31#define MAX_VIS_PACKET_SIZE 1000
32
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000033static void start_vis_timer(struct bat_priv *bat_priv);
34
35/* free the info */
36static void free_info(struct kref *ref)
37{
38 struct vis_info *info = container_of(ref, struct vis_info, refcount);
39 struct bat_priv *bat_priv = info->bat_priv;
40 struct recvlist_node *entry, *tmp;
41
42 list_del_init(&info->send_list);
43 spin_lock_bh(&bat_priv->vis_list_lock);
44 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
45 list_del(&entry->list);
46 kfree(entry);
47 }
48
49 spin_unlock_bh(&bat_priv->vis_list_lock);
50 kfree_skb(info->skb_packet);
Sven Eckelmanndda9fc62011-01-28 18:34:06 +010051 kfree(info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000052}
53
54/* Compare two vis packets, used by the hashing algorithm */
Sven Eckelmann747e4222011-05-14 23:14:50 +020055static int vis_info_cmp(const struct hlist_node *node, const void *data2)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056{
Sven Eckelmann747e4222011-05-14 23:14:50 +020057 const struct vis_info *d1, *d2;
58 const struct vis_packet *p1, *p2;
Marek Lindner7aadf882011-02-18 12:28:09 +000059
60 d1 = container_of(node, struct vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061 d2 = data2;
62 p1 = (struct vis_packet *)d1->skb_packet->data;
63 p2 = (struct vis_packet *)d2->skb_packet->data;
Marek Lindner39901e72011-02-18 12:28:08 +000064 return compare_eth(p1->vis_orig, p2->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065}
66
67/* hash function to choose an entry in a hash table of given size */
68/* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
Antonio Quartullic90681b2011-10-05 17:05:25 +020069static uint32_t vis_info_choose(const void *data, uint32_t size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000070{
Sven Eckelmann747e4222011-05-14 23:14:50 +020071 const struct vis_info *vis_info = data;
72 const struct vis_packet *packet;
73 const unsigned char *key;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000074 uint32_t hash = 0;
75 size_t i;
76
77 packet = (struct vis_packet *)vis_info->skb_packet->data;
78 key = packet->vis_orig;
79 for (i = 0; i < ETH_ALEN; i++) {
80 hash += key[i];
81 hash += (hash << 10);
82 hash ^= (hash >> 6);
83 }
84
85 hash += (hash << 3);
86 hash ^= (hash >> 11);
87 hash += (hash << 15);
88
89 return hash % size;
90}
91
Marek Lindner7aadf882011-02-18 12:28:09 +000092static struct vis_info *vis_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020093 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000094{
95 struct hashtable_t *hash = bat_priv->vis_hash;
96 struct hlist_head *head;
97 struct hlist_node *node;
98 struct vis_info *vis_info, *vis_info_tmp = NULL;
Antonio Quartullic90681b2011-10-05 17:05:25 +020099 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +0000100
101 if (!hash)
102 return NULL;
103
104 index = vis_info_choose(data, hash->size);
105 head = &hash->table[index];
106
107 rcu_read_lock();
108 hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
109 if (!vis_info_cmp(node, data))
110 continue;
111
112 vis_info_tmp = vis_info;
113 break;
114 }
115 rcu_read_unlock();
116
117 return vis_info_tmp;
118}
119
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120/* insert interface to the list of interfaces of one originator, if it
121 * does not already exist in the list */
122static 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) {
Sven Eckelmann747e4222011-05-14 23:14:50 +0200130 if (compare_eth(entry->addr, interface))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000131 return;
132 }
133
Antonio Quartulli015758d2011-07-09 17:52:13 +0200134 /* it's a new address, add it to the list */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000135 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
Sven Eckelmann747e4222011-05-14 23:14:50 +0200143static ssize_t vis_data_read_prim_sec(char *buff,
144 const struct hlist_head *if_list)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000145{
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
160static 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 */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200177static ssize_t vis_data_read_entry(char *buff,
178 const struct vis_info_entry *entry,
179 const uint8_t *src, bool primary)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180{
181 /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
182 if (primary && entry->quality == 0)
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200183 return sprintf(buff, "TT %pM, ", entry->dest);
Marek Lindner39901e72011-02-18 12:28:08 +0000184 else if (compare_eth(entry->src, src))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185 return sprintf(buff, "TQ %pM %d, ", entry->dest,
186 entry->quality);
187
188 return 0;
189}
190
191int vis_seq_print_text(struct seq_file *seq, void *offset)
192{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200193 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000194 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000195 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196 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;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200205 uint32_t i;
206 int j, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000207 int vis_server = atomic_read(&bat_priv->vis_mode);
208 size_t buff_pos, buf_size;
209 char *buff;
210 int compare;
211
Marek Lindner32ae9b22011-04-20 15:40:58 +0200212 primary_if = primary_if_get_selected(bat_priv);
213 if (!primary_if)
214 goto out;
215
216 if (vis_server == VIS_TYPE_CLIENT_UPDATE)
217 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218
219 buf_size = 1;
220 /* Estimate length */
221 spin_lock_bh(&bat_priv->vis_hash_lock);
222 for (i = 0; i < hash->size; i++) {
223 head = &hash->table[i];
224
Marek Lindner7aadf882011-02-18 12:28:09 +0000225 rcu_read_lock();
226 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000227 packet = (struct vis_packet *)info->skb_packet->data;
228 entries = (struct vis_info_entry *)
Sven Eckelmann704509b2011-05-14 23:14:54 +0200229 ((char *)packet + sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230
231 for (j = 0; j < packet->entries; j++) {
232 if (entries[j].quality == 0)
233 continue;
234 compare =
Marek Lindner39901e72011-02-18 12:28:08 +0000235 compare_eth(entries[j].src, packet->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236 vis_data_insert_interface(entries[j].src,
237 &vis_if_list,
238 compare);
239 }
240
241 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
242 buf_size += 18 + 26 * packet->entries;
243
244 /* add primary/secondary records */
Marek Lindner39901e72011-02-18 12:28:08 +0000245 if (compare_eth(entry->addr, packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000246 buf_size +=
247 vis_data_count_prim_sec(&vis_if_list);
248
249 buf_size += 1;
250 }
251
252 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
253 list) {
254 hlist_del(&entry->list);
255 kfree(entry);
256 }
257 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000258 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259 }
260
261 buff = kmalloc(buf_size, GFP_ATOMIC);
262 if (!buff) {
263 spin_unlock_bh(&bat_priv->vis_hash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200264 ret = -ENOMEM;
265 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266 }
267 buff[0] = '\0';
268 buff_pos = 0;
269
270 for (i = 0; i < hash->size; i++) {
271 head = &hash->table[i];
272
Marek Lindner7aadf882011-02-18 12:28:09 +0000273 rcu_read_lock();
274 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275 packet = (struct vis_packet *)info->skb_packet->data;
276 entries = (struct vis_info_entry *)
Sven Eckelmann704509b2011-05-14 23:14:54 +0200277 ((char *)packet + sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278
279 for (j = 0; j < packet->entries; j++) {
280 if (entries[j].quality == 0)
281 continue;
282 compare =
Marek Lindner39901e72011-02-18 12:28:08 +0000283 compare_eth(entries[j].src, packet->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000284 vis_data_insert_interface(entries[j].src,
285 &vis_if_list,
286 compare);
287 }
288
289 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
290 buff_pos += sprintf(buff + buff_pos, "%pM,",
291 entry->addr);
292
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000293 for (j = 0; j < packet->entries; j++)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294 buff_pos += vis_data_read_entry(
295 buff + buff_pos,
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000296 &entries[j],
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 entry->addr,
298 entry->primary);
299
300 /* add primary/secondary records */
Marek Lindner39901e72011-02-18 12:28:08 +0000301 if (compare_eth(entry->addr, packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302 buff_pos +=
303 vis_data_read_prim_sec(buff + buff_pos,
304 &vis_if_list);
305
306 buff_pos += sprintf(buff + buff_pos, "\n");
307 }
308
309 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
310 list) {
311 hlist_del(&entry->list);
312 kfree(entry);
313 }
314 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000315 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000316 }
317
318 spin_unlock_bh(&bat_priv->vis_hash_lock);
319
320 seq_printf(seq, "%s", buff);
321 kfree(buff);
322
Marek Lindner32ae9b22011-04-20 15:40:58 +0200323out:
324 if (primary_if)
325 hardif_free_ref(primary_if);
326 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327}
328
329/* add the info packet to the send list, if it was not
330 * already linked in. */
331static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info)
332{
333 if (list_empty(&info->send_list)) {
334 kref_get(&info->refcount);
335 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
336 }
337}
338
339/* delete the info packet from the send list, if it was
340 * linked in. */
341static void send_list_del(struct vis_info *info)
342{
343 if (!list_empty(&info->send_list)) {
344 list_del_init(&info->send_list);
345 kref_put(&info->refcount, free_info);
346 }
347}
348
349/* tries to add one entry to the receive list. */
350static void recv_list_add(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200351 struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352{
353 struct recvlist_node *entry;
354
Sven Eckelmann704509b2011-05-14 23:14:54 +0200355 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356 if (!entry)
357 return;
358
359 memcpy(entry->mac, mac, ETH_ALEN);
360 spin_lock_bh(&bat_priv->vis_list_lock);
361 list_add_tail(&entry->list, recv_list);
362 spin_unlock_bh(&bat_priv->vis_list_lock);
363}
364
365/* returns 1 if this mac is in the recv_list */
366static int recv_list_is_in(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200367 const struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000368{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200369 const struct recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
371 spin_lock_bh(&bat_priv->vis_list_lock);
372 list_for_each_entry(entry, recv_list, list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000373 if (compare_eth(entry->mac, mac)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374 spin_unlock_bh(&bat_priv->vis_list_lock);
375 return 1;
376 }
377 }
378 spin_unlock_bh(&bat_priv->vis_list_lock);
379 return 0;
380}
381
382/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
383 * broken.. ). vis hash must be locked outside. is_new is set when the packet
384 * is newer than old entries in the hash. */
385static struct vis_info *add_packet(struct bat_priv *bat_priv,
386 struct vis_packet *vis_packet,
387 int vis_info_len, int *is_new,
388 int make_broadcast)
389{
390 struct vis_info *info, *old_info;
391 struct vis_packet *search_packet, *old_packet;
392 struct vis_info search_elem;
393 struct vis_packet *packet;
394 int hash_added;
395
396 *is_new = 0;
397 /* sanity check */
398 if (!bat_priv->vis_hash)
399 return NULL;
400
401 /* see if the packet is already in vis_hash */
Sven Eckelmann704509b2011-05-14 23:14:54 +0200402 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000403 if (!search_elem.skb_packet)
404 return NULL;
405 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
Sven Eckelmann704509b2011-05-14 23:14:54 +0200406 sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000407
408 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Marek Lindner7aadf882011-02-18 12:28:09 +0000409 old_info = vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000410 kfree_skb(search_elem.skb_packet);
411
412 if (old_info) {
413 old_packet = (struct vis_packet *)old_info->skb_packet->data;
414 if (!seq_after(ntohl(vis_packet->seqno),
415 ntohl(old_packet->seqno))) {
416 if (old_packet->seqno == vis_packet->seqno) {
417 recv_list_add(bat_priv, &old_info->recv_list,
418 vis_packet->sender_orig);
419 return old_info;
420 } else {
421 /* newer packet is already in hash. */
422 return NULL;
423 }
424 }
425 /* remove old entry */
426 hash_remove(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
427 old_info);
428 send_list_del(old_info);
429 kref_put(&old_info->refcount, free_info);
430 }
431
Sven Eckelmann704509b2011-05-14 23:14:54 +0200432 info = kmalloc(sizeof(*info), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000433 if (!info)
434 return NULL;
435
Sven Eckelmann704509b2011-05-14 23:14:54 +0200436 info->skb_packet = dev_alloc_skb(sizeof(*packet) + vis_info_len +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100437 ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000438 if (!info->skb_packet) {
439 kfree(info);
440 return NULL;
441 }
Antonio Quartulli0d125072012-02-18 11:27:34 +0100442 skb_reserve(info->skb_packet, ETH_HLEN);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200443 packet = (struct vis_packet *)skb_put(info->skb_packet, sizeof(*packet)
444 + vis_info_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445
446 kref_init(&info->refcount);
447 INIT_LIST_HEAD(&info->send_list);
448 INIT_LIST_HEAD(&info->recv_list);
449 info->first_seen = jiffies;
450 info->bat_priv = bat_priv;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200451 memcpy(packet, vis_packet, sizeof(*packet) + vis_info_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000452
453 /* initialize and add new packet. */
454 *is_new = 1;
455
456 /* Make it a broadcast packet, if required */
457 if (make_broadcast)
458 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
459
460 /* repair if entries is longer than packet. */
461 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
462 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
463
464 recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
465
466 /* try to add it */
467 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
Marek Lindner7aadf882011-02-18 12:28:09 +0000468 info, &info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200469 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000470 /* did not work (for some reason) */
Sven Eckelmann2674c152011-01-28 18:34:05 +0100471 kref_put(&info->refcount, free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000472 info = NULL;
473 }
474
475 return info;
476}
477
478/* handle the server sync packet, forward if needed. */
479void receive_server_sync_packet(struct bat_priv *bat_priv,
480 struct vis_packet *vis_packet,
481 int vis_info_len)
482{
483 struct vis_info *info;
484 int is_new, make_broadcast;
485 int vis_server = atomic_read(&bat_priv->vis_mode);
486
487 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
488
489 spin_lock_bh(&bat_priv->vis_hash_lock);
490 info = add_packet(bat_priv, vis_packet, vis_info_len,
491 &is_new, make_broadcast);
492 if (!info)
493 goto end;
494
495 /* only if we are server ourselves and packet is newer than the one in
496 * hash.*/
497 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
498 send_list_add(bat_priv, info);
499end:
500 spin_unlock_bh(&bat_priv->vis_hash_lock);
501}
502
503/* handle an incoming client update packet and schedule forward if needed. */
504void receive_client_update_packet(struct bat_priv *bat_priv,
505 struct vis_packet *vis_packet,
506 int vis_info_len)
507{
508 struct vis_info *info;
509 struct vis_packet *packet;
510 int is_new;
511 int vis_server = atomic_read(&bat_priv->vis_mode);
512 int are_target = 0;
513
514 /* clients shall not broadcast. */
515 if (is_broadcast_ether_addr(vis_packet->target_orig))
516 return;
517
518 /* Are we the target for this VIS packet? */
519 if (vis_server == VIS_TYPE_SERVER_SYNC &&
520 is_my_mac(vis_packet->target_orig))
521 are_target = 1;
522
523 spin_lock_bh(&bat_priv->vis_hash_lock);
524 info = add_packet(bat_priv, vis_packet, vis_info_len,
525 &is_new, are_target);
526
527 if (!info)
528 goto end;
529 /* note that outdated packets will be dropped at this point. */
530
531 packet = (struct vis_packet *)info->skb_packet->data;
532
533 /* send only if we're the target server or ... */
534 if (are_target && is_new) {
535 packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
536 send_list_add(bat_priv, info);
537
538 /* ... we're not the recipient (and thus need to forward). */
539 } else if (!is_my_mac(packet->target_orig)) {
540 send_list_add(bat_priv, info);
541 }
542
543end:
544 spin_unlock_bh(&bat_priv->vis_hash_lock);
545}
546
547/* Walk the originators and find the VIS server with the best tq. Set the packet
548 * address to its address and return the best_tq.
549 *
550 * Must be called with the originator hash locked */
551static int find_best_vis_server(struct bat_priv *bat_priv,
552 struct vis_info *info)
553{
554 struct hashtable_t *hash = bat_priv->orig_hash;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000555 struct neigh_node *router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000556 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000557 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000558 struct orig_node *orig_node;
559 struct vis_packet *packet;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200560 int best_tq = -1;
561 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000562
563 packet = (struct vis_packet *)info->skb_packet->data;
564
565 for (i = 0; i < hash->size; i++) {
566 head = &hash->table[i];
567
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000568 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000569 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000570 router = orig_node_get_router(orig_node);
571 if (!router)
572 continue;
573
574 if ((orig_node->flags & VIS_SERVER) &&
575 (router->tq_avg > best_tq)) {
576 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000577 memcpy(packet->target_orig, orig_node->orig,
578 ETH_ALEN);
579 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000580 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000581 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000582 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000583 }
584
585 return best_tq;
586}
587
588/* Return true if the vis packet is full. */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200589static bool vis_packet_full(const struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200591 const struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000592 packet = (struct vis_packet *)info->skb_packet->data;
593
594 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
595 < packet->entries + 1)
596 return true;
597 return false;
598}
599
600/* generates a packet of own vis data,
601 * returns 0 on success, -1 if no packet could be generated */
602static int generate_vis_packet(struct bat_priv *bat_priv)
603{
604 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000605 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000606 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000607 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000608 struct neigh_node *router;
Sven Eckelmann958ca592011-05-14 23:14:53 +0200609 struct vis_info *info = bat_priv->my_vis_info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000610 struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
611 struct vis_info_entry *entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100612 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200613 int best_tq = -1;
614 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615
616 info->first_seen = jiffies;
617 packet->vis_type = atomic_read(&bat_priv->vis_mode);
618
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100620 packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621 packet->seqno = htonl(ntohl(packet->seqno) + 1);
622 packet->entries = 0;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200623 skb_trim(info->skb_packet, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624
625 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
626 best_tq = find_best_vis_server(bat_priv, info);
627
Marek Lindnerd0072602011-01-19 20:01:44 +0000628 if (best_tq < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200629 return best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630 }
631
632 for (i = 0; i < hash->size; i++) {
633 head = &hash->table[i];
634
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000635 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000636 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000637 router = orig_node_get_router(orig_node);
638 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639 continue;
640
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000641 if (!compare_eth(router->addr, orig_node->orig))
642 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000644 if (router->if_incoming->if_status != IF_ACTIVE)
645 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000646
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000647 if (router->tq_avg < 1)
648 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000649
650 /* fill one entry into buffer. */
651 entry = (struct vis_info_entry *)
652 skb_put(info->skb_packet, sizeof(*entry));
653 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000654 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000655 ETH_ALEN);
656 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000657 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658 packet->entries++;
659
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000660next:
661 neigh_node_free_ref(router);
662
Marek Lindnerd0072602011-01-19 20:01:44 +0000663 if (vis_packet_full(info))
664 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000665 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000666 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667 }
668
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200669 hash = bat_priv->tt_local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000670
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000671 for (i = 0; i < hash->size; i++) {
672 head = &hash->table[i];
673
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200674 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100675 hlist_for_each_entry_rcu(tt_common_entry, node, head,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200676 hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000677 entry = (struct vis_info_entry *)
678 skb_put(info->skb_packet,
679 sizeof(*entry));
680 memset(entry->src, 0, ETH_ALEN);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100681 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200682 entry->quality = 0; /* 0 means TT */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683 packet->entries++;
684
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200685 if (vis_packet_full(info))
686 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000687 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200688 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689 }
690
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000691 return 0;
Marek Lindnerd0072602011-01-19 20:01:44 +0000692
693unlock:
694 rcu_read_unlock();
695 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696}
697
698/* free old vis packets. Must be called with this vis_hash_lock
699 * held */
700static void purge_vis_packets(struct bat_priv *bat_priv)
701{
Antonio Quartullic90681b2011-10-05 17:05:25 +0200702 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000703 struct hashtable_t *hash = bat_priv->vis_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000704 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000705 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706 struct vis_info *info;
707
708 for (i = 0; i < hash->size; i++) {
709 head = &hash->table[i];
710
Marek Lindner7aadf882011-02-18 12:28:09 +0000711 hlist_for_each_entry_safe(info, node, node_tmp,
712 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000713 /* never purge own data. */
714 if (info == bat_priv->my_vis_info)
715 continue;
716
Marek Lindner032b7962011-12-20 19:30:40 +0800717 if (has_timed_out(info->first_seen, VIS_TIMEOUT)) {
Marek Lindner7aadf882011-02-18 12:28:09 +0000718 hlist_del(node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719 send_list_del(info);
720 kref_put(&info->refcount, free_info);
721 }
722 }
723 }
724}
725
726static void broadcast_vis_packet(struct bat_priv *bat_priv,
727 struct vis_info *info)
728{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000729 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000730 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000731 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000732 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733 struct orig_node *orig_node;
734 struct vis_packet *packet;
735 struct sk_buff *skb;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000736 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000737 uint8_t dstaddr[ETH_ALEN];
Antonio Quartullic90681b2011-10-05 17:05:25 +0200738 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000739
740
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000741 packet = (struct vis_packet *)info->skb_packet->data;
742
743 /* send to all routers in range. */
744 for (i = 0; i < hash->size; i++) {
745 head = &hash->table[i];
746
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000747 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000748 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749 /* if it's a vis server and reachable, send it. */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750 if (!(orig_node->flags & VIS_SERVER))
751 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000752
753 router = orig_node_get_router(orig_node);
754 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755 continue;
756
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000757 /* don't send it if we already received the packet from
758 * this node. */
759 if (recv_list_is_in(bat_priv, &info->recv_list,
760 orig_node->orig)) {
761 neigh_node_free_ref(router);
762 continue;
763 }
764
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000765 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000766 hard_iface = router->if_incoming;
767 memcpy(dstaddr, router->addr, ETH_ALEN);
768
769 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000770
771 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
772 if (skb)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000773 send_skb_packet(skb, hard_iface, dstaddr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000774
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000775 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000776 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000777 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000778}
779
780static void unicast_vis_packet(struct bat_priv *bat_priv,
781 struct vis_info *info)
782{
783 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000784 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785 struct sk_buff *skb;
786 struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000787
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000789
Marek Lindner7aadf882011-02-18 12:28:09 +0000790 orig_node = orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000791 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000792 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000793
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000794 router = orig_node_get_router(orig_node);
795 if (!router)
796 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000797
798 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
799 if (skb)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000800 send_skb_packet(skb, router->if_incoming, router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000801
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000803 if (router)
804 neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000805 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000806 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807}
808
809/* only send one vis packet. called from send_vis_packets() */
810static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
811{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200812 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000813 struct vis_packet *packet;
814
Marek Lindner32ae9b22011-04-20 15:40:58 +0200815 primary_if = primary_if_get_selected(bat_priv);
816 if (!primary_if)
817 goto out;
818
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000819 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100820 if (packet->header.ttl < 2) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000821 pr_debug("Error - can't send vis packet: ttl exceeded\n");
Marek Lindner32ae9b22011-04-20 15:40:58 +0200822 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823 }
824
Marek Lindner32ae9b22011-04-20 15:40:58 +0200825 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100826 packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827
828 if (is_broadcast_ether_addr(packet->target_orig))
829 broadcast_vis_packet(bat_priv, info);
830 else
831 unicast_vis_packet(bat_priv, info);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100832 packet->header.ttl++; /* restore TTL */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200833
834out:
835 if (primary_if)
836 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837}
838
839/* called from timer; send (and maybe generate) vis packet. */
840static void send_vis_packets(struct work_struct *work)
841{
842 struct delayed_work *delayed_work =
843 container_of(work, struct delayed_work, work);
844 struct bat_priv *bat_priv =
845 container_of(delayed_work, struct bat_priv, vis_work);
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100846 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000847
848 spin_lock_bh(&bat_priv->vis_hash_lock);
849 purge_vis_packets(bat_priv);
850
851 if (generate_vis_packet(bat_priv) == 0) {
852 /* schedule if generation was successful */
853 send_list_add(bat_priv, bat_priv->my_vis_info);
854 }
855
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100856 while (!list_empty(&bat_priv->vis_send_list)) {
857 info = list_first_entry(&bat_priv->vis_send_list,
858 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000859
860 kref_get(&info->refcount);
861 spin_unlock_bh(&bat_priv->vis_hash_lock);
862
Marek Lindner32ae9b22011-04-20 15:40:58 +0200863 send_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000864
865 spin_lock_bh(&bat_priv->vis_hash_lock);
866 send_list_del(info);
867 kref_put(&info->refcount, free_info);
868 }
869 spin_unlock_bh(&bat_priv->vis_hash_lock);
870 start_vis_timer(bat_priv);
871}
872
873/* init the vis server. this may only be called when if_list is already
874 * initialized (e.g. bat0 is initialized, interfaces have been added) */
875int vis_init(struct bat_priv *bat_priv)
876{
877 struct vis_packet *packet;
878 int hash_added;
879
880 if (bat_priv->vis_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200881 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000882
883 spin_lock_bh(&bat_priv->vis_hash_lock);
884
885 bat_priv->vis_hash = hash_new(256);
886 if (!bat_priv->vis_hash) {
887 pr_err("Can't initialize vis_hash\n");
888 goto err;
889 }
890
891 bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700892 if (!bat_priv->my_vis_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000893 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000894
Sven Eckelmann704509b2011-05-14 23:14:54 +0200895 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(sizeof(*packet) +
896 MAX_VIS_PACKET_SIZE +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100897 ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000898 if (!bat_priv->my_vis_info->skb_packet)
899 goto free_info;
900
Antonio Quartulli0d125072012-02-18 11:27:34 +0100901 skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200902 packet = (struct vis_packet *)skb_put(bat_priv->my_vis_info->skb_packet,
903 sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000904
905 /* prefill the vis info */
906 bat_priv->my_vis_info->first_seen = jiffies -
907 msecs_to_jiffies(VIS_INTERVAL);
908 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
909 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
910 kref_init(&bat_priv->my_vis_info->refcount);
911 bat_priv->my_vis_info->bat_priv = bat_priv;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100912 packet->header.version = COMPAT_VERSION;
913 packet->header.packet_type = BAT_VIS;
914 packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915 packet->seqno = 0;
916 packet->entries = 0;
917
918 INIT_LIST_HEAD(&bat_priv->vis_send_list);
919
920 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
Marek Lindner7aadf882011-02-18 12:28:09 +0000921 bat_priv->my_vis_info,
922 &bat_priv->my_vis_info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200923 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000924 pr_err("Can't add own vis packet into hash\n");
925 /* not in hash, need to remove it manually. */
926 kref_put(&bat_priv->my_vis_info->refcount, free_info);
927 goto err;
928 }
929
930 spin_unlock_bh(&bat_priv->vis_hash_lock);
931 start_vis_timer(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200932 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000933
934free_info:
935 kfree(bat_priv->my_vis_info);
936 bat_priv->my_vis_info = NULL;
937err:
938 spin_unlock_bh(&bat_priv->vis_hash_lock);
939 vis_quit(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200940 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000941}
942
943/* Decrease the reference count on a hash item info */
Marek Lindner7aadf882011-02-18 12:28:09 +0000944static void free_info_ref(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000945{
Marek Lindner7aadf882011-02-18 12:28:09 +0000946 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000947
Marek Lindner7aadf882011-02-18 12:28:09 +0000948 info = container_of(node, struct vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000949 send_list_del(info);
950 kref_put(&info->refcount, free_info);
951}
952
953/* shutdown vis-server */
954void vis_quit(struct bat_priv *bat_priv)
955{
956 if (!bat_priv->vis_hash)
957 return;
958
959 cancel_delayed_work_sync(&bat_priv->vis_work);
960
961 spin_lock_bh(&bat_priv->vis_hash_lock);
962 /* properly remove, kill timers ... */
963 hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
964 bat_priv->vis_hash = NULL;
965 bat_priv->my_vis_info = NULL;
966 spin_unlock_bh(&bat_priv->vis_hash_lock);
967}
968
969/* schedule packets for (re)transmission */
970static void start_vis_timer(struct bat_priv *bat_priv)
971{
972 INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
973 queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work,
974 msecs_to_jiffies(VIS_INTERVAL));
975}