blob: 20eef04645bdb9ca656c7271fd7b191773469512 [file] [log] [blame]
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001/* Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000031static void start_vis_timer(struct bat_priv *bat_priv);
32
33/* free the info */
34static 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);
Sven Eckelmanndda9fc62011-01-28 18:34:06 +010049 kfree(info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000050}
51
52/* Compare two vis packets, used by the hashing algorithm */
Sven Eckelmann747e4222011-05-14 23:14:50 +020053static int vis_info_cmp(const struct hlist_node *node, const void *data2)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054{
Sven Eckelmann747e4222011-05-14 23:14:50 +020055 const struct vis_info *d1, *d2;
56 const struct vis_packet *p1, *p2;
Marek Lindner7aadf882011-02-18 12:28:09 +000057
58 d1 = container_of(node, struct vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059 d2 = data2;
60 p1 = (struct vis_packet *)d1->skb_packet->data;
61 p2 = (struct vis_packet *)d2->skb_packet->data;
Marek Lindner39901e72011-02-18 12:28:08 +000062 return compare_eth(p1->vis_orig, p2->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063}
64
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +020065/* 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 */
Antonio Quartullic90681b2011-10-05 17:05:25 +020068static uint32_t vis_info_choose(const void *data, uint32_t size)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069{
Sven Eckelmann747e4222011-05-14 23:14:50 +020070 const struct vis_info *vis_info = data;
71 const struct vis_packet *packet;
72 const unsigned char *key;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000073 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
Marek Lindner7aadf882011-02-18 12:28:09 +000091static struct vis_info *vis_hash_find(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +020092 const void *data)
Marek Lindner7aadf882011-02-18 12:28:09 +000093{
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;
Antonio Quartullic90681b2011-10-05 17:05:25 +020098 uint32_t index;
Marek Lindner7aadf882011-02-18 12:28:09 +000099
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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000119/* insert interface to the list of interfaces of one originator, if it
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200120 * does not already exist in the list
121 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000122static 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
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200191int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192{
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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210
Marek Lindner32ae9b22011-04-20 15:40:58 +0200211 primary_if = 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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000217
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
Marek Lindner7aadf882011-02-18 12:28:09 +0000224 rcu_read_lock();
225 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000226 packet = (struct vis_packet *)info->skb_packet->data;
227 entries = (struct vis_info_entry *)
Sven Eckelmann704509b2011-05-14 23:14:54 +0200228 ((char *)packet + sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200230 vis_data_insert_interface(packet->vis_orig,
231 &vis_if_list, true);
232
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233 for (j = 0; j < packet->entries; j++) {
234 if (entries[j].quality == 0)
235 continue;
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200236 if (compare_eth(entries[j].src,
237 packet->vis_orig))
238 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239 vis_data_insert_interface(entries[j].src,
240 &vis_if_list,
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200241 false);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000242 }
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 */
Marek Lindner39901e72011-02-18 12:28:08 +0000248 if (compare_eth(entry->addr, packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249 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 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000261 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262 }
263
264 buff = kmalloc(buf_size, GFP_ATOMIC);
265 if (!buff) {
266 spin_unlock_bh(&bat_priv->vis_hash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200267 ret = -ENOMEM;
268 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269 }
270 buff[0] = '\0';
271 buff_pos = 0;
272
273 for (i = 0; i < hash->size; i++) {
274 head = &hash->table[i];
275
Marek Lindner7aadf882011-02-18 12:28:09 +0000276 rcu_read_lock();
277 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000278 packet = (struct vis_packet *)info->skb_packet->data;
279 entries = (struct vis_info_entry *)
Sven Eckelmann704509b2011-05-14 23:14:54 +0200280 ((char *)packet + sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200282 vis_data_insert_interface(packet->vis_orig,
283 &vis_if_list, true);
284
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285 for (j = 0; j < packet->entries; j++) {
286 if (entries[j].quality == 0)
287 continue;
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200288 if (compare_eth(entries[j].src,
289 packet->vis_orig))
290 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000291 vis_data_insert_interface(entries[j].src,
292 &vis_if_list,
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200293 false);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000294 }
295
296 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
297 buff_pos += sprintf(buff + buff_pos, "%pM,",
298 entry->addr);
299
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000300 for (j = 0; j < packet->entries; j++)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301 buff_pos += vis_data_read_entry(
302 buff + buff_pos,
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000303 &entries[j],
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000304 entry->addr,
305 entry->primary);
306
307 /* add primary/secondary records */
Marek Lindner39901e72011-02-18 12:28:08 +0000308 if (compare_eth(entry->addr, packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000309 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 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000322 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 }
324
325 spin_unlock_bh(&bat_priv->vis_hash_lock);
326
327 seq_printf(seq, "%s", buff);
328 kfree(buff);
329
Marek Lindner32ae9b22011-04-20 15:40:58 +0200330out:
331 if (primary_if)
332 hardif_free_ref(primary_if);
333 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334}
335
336/* add the info packet to the send list, if it was not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200337 * already linked in.
338 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339static 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
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200348 * linked in.
349 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350static 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. */
359static void recv_list_add(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200360 struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361{
362 struct recvlist_node *entry;
363
Sven Eckelmann704509b2011-05-14 23:14:54 +0200364 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000365 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 */
375static int recv_list_is_in(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200376 const struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200378 const struct recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379
380 spin_lock_bh(&bat_priv->vis_list_lock);
381 list_for_each_entry(entry, recv_list, list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000382 if (compare_eth(entry->mac, mac)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383 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
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200393 * is newer than old entries in the hash.
394 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395static 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 */
Sven Eckelmann704509b2011-05-14 23:14:54 +0200412 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413 if (!search_elem.skb_packet)
414 return NULL;
415 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
Sven Eckelmann704509b2011-05-14 23:14:54 +0200416 sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417
418 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Marek Lindner7aadf882011-02-18 12:28:09 +0000419 old_info = vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000420 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 hash_remove(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
437 old_info);
438 send_list_del(old_info);
439 kref_put(&old_info->refcount, free_info);
440 }
441
Sven Eckelmann704509b2011-05-14 23:14:54 +0200442 info = kmalloc(sizeof(*info), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000443 if (!info)
444 return NULL;
445
Sven Eckelmann704509b2011-05-14 23:14:54 +0200446 info->skb_packet = dev_alloc_skb(sizeof(*packet) + vis_info_len +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100447 ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000448 if (!info->skb_packet) {
449 kfree(info);
450 return NULL;
451 }
Antonio Quartulli0d125072012-02-18 11:27:34 +0100452 skb_reserve(info->skb_packet, ETH_HLEN);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200453 packet = (struct vis_packet *)skb_put(info->skb_packet, sizeof(*packet)
454 + vis_info_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000455
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;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200461 memcpy(packet, vis_packet, sizeof(*packet) + vis_info_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000462
463 /* initialize and add new packet. */
464 *is_new = 1;
465
466 /* Make it a broadcast packet, if required */
467 if (make_broadcast)
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200468 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000469
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 = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
Marek Lindner7aadf882011-02-18 12:28:09 +0000478 info, &info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200479 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000480 /* did not work (for some reason) */
Sven Eckelmann2674c152011-01-28 18:34:05 +0100481 kref_put(&info->refcount, free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482 info = NULL;
483 }
484
485 return info;
486}
487
488/* handle the server sync packet, forward if needed. */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200489void batadv_receive_server_sync_packet(struct bat_priv *bat_priv,
490 struct vis_packet *vis_packet,
491 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000492{
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
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200506 * hash.
507 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000508 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
509 send_list_add(bat_priv, info);
510end:
511 spin_unlock_bh(&bat_priv->vis_hash_lock);
512}
513
514/* handle an incoming client update packet and schedule forward if needed. */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200515void batadv_receive_client_update_packet(struct bat_priv *bat_priv,
516 struct vis_packet *vis_packet,
517 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000518{
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 &&
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200531 batadv_is_my_mac(vis_packet->target_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000532 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). */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200550 } else if (!batadv_is_my_mac(packet->target_orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000551 send_list_add(bat_priv, info);
552 }
553
554end:
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 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200561 * Must be called with the originator hash locked
562 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563static 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;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000567 struct neigh_node *router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000568 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000569 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000570 struct orig_node *orig_node;
571 struct vis_packet *packet;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200572 int best_tq = -1;
573 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000574
575 packet = (struct vis_packet *)info->skb_packet->data;
576
577 for (i = 0; i < hash->size; i++) {
578 head = &hash->table[i];
579
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000580 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000581 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200582 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000583 if (!router)
584 continue;
585
586 if ((orig_node->flags & VIS_SERVER) &&
587 (router->tq_avg > best_tq)) {
588 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000589 memcpy(packet->target_orig, orig_node->orig,
590 ETH_ALEN);
591 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200592 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000593 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000594 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595 }
596
597 return best_tq;
598}
599
600/* Return true if the vis packet is full. */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200601static bool vis_packet_full(const struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000602{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200603 const struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604 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,
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200613 * returns 0 on success, -1 if no packet could be generated
614 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000615static int generate_vis_packet(struct bat_priv *bat_priv)
616{
617 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000618 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000619 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000620 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000621 struct neigh_node *router;
Sven Eckelmann958ca592011-05-14 23:14:53 +0200622 struct vis_info *info = bat_priv->my_vis_info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623 struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
624 struct vis_info_entry *entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100625 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200626 int best_tq = -1;
627 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000628
629 info->first_seen = jiffies;
630 packet->vis_type = atomic_read(&bat_priv->vis_mode);
631
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200632 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100633 packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634 packet->seqno = htonl(ntohl(packet->seqno) + 1);
635 packet->entries = 0;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200636 skb_trim(info->skb_packet, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637
638 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
639 best_tq = find_best_vis_server(bat_priv, info);
640
Marek Lindnerd0072602011-01-19 20:01:44 +0000641 if (best_tq < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200642 return best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643 }
644
645 for (i = 0; i < hash->size; i++) {
646 head = &hash->table[i];
647
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000648 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000649 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200650 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000651 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000652 continue;
653
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000654 if (!compare_eth(router->addr, orig_node->orig))
655 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000656
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000657 if (router->if_incoming->if_status != IF_ACTIVE)
658 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000660 if (router->tq_avg < 1)
661 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662
663 /* fill one entry into buffer. */
664 entry = (struct vis_info_entry *)
665 skb_put(info->skb_packet, sizeof(*entry));
666 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000667 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668 ETH_ALEN);
669 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000670 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000671 packet->entries++;
672
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000673next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200674 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000675
Marek Lindnerd0072602011-01-19 20:01:44 +0000676 if (vis_packet_full(info))
677 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000678 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000679 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680 }
681
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200682 hash = bat_priv->tt_local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000683
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000684 for (i = 0; i < hash->size; i++) {
685 head = &hash->table[i];
686
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200687 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100688 hlist_for_each_entry_rcu(tt_common_entry, node, head,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200689 hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000690 entry = (struct vis_info_entry *)
691 skb_put(info->skb_packet,
692 sizeof(*entry));
693 memset(entry->src, 0, ETH_ALEN);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100694 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200695 entry->quality = 0; /* 0 means TT */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000696 packet->entries++;
697
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200698 if (vis_packet_full(info))
699 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000700 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200701 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702 }
703
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000704 return 0;
Marek Lindnerd0072602011-01-19 20:01:44 +0000705
706unlock:
707 rcu_read_unlock();
708 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000709}
710
711/* free old vis packets. Must be called with this vis_hash_lock
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200712 * held
713 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000714static void purge_vis_packets(struct bat_priv *bat_priv)
715{
Antonio Quartullic90681b2011-10-05 17:05:25 +0200716 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000717 struct hashtable_t *hash = bat_priv->vis_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000718 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000720 struct vis_info *info;
721
722 for (i = 0; i < hash->size; i++) {
723 head = &hash->table[i];
724
Marek Lindner7aadf882011-02-18 12:28:09 +0000725 hlist_for_each_entry_safe(info, node, node_tmp,
726 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000727 /* never purge own data. */
728 if (info == bat_priv->my_vis_info)
729 continue;
730
Marek Lindner032b7962011-12-20 19:30:40 +0800731 if (has_timed_out(info->first_seen, VIS_TIMEOUT)) {
Marek Lindner7aadf882011-02-18 12:28:09 +0000732 hlist_del(node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000733 send_list_del(info);
734 kref_put(&info->refcount, free_info);
735 }
736 }
737 }
738}
739
740static void broadcast_vis_packet(struct bat_priv *bat_priv,
741 struct vis_info *info)
742{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000743 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000744 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000745 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000746 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747 struct orig_node *orig_node;
748 struct vis_packet *packet;
749 struct sk_buff *skb;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000750 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000751 uint8_t dstaddr[ETH_ALEN];
Antonio Quartullic90681b2011-10-05 17:05:25 +0200752 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000753
754
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755 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
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000761 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000762 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000763 /* if it's a vis server and reachable, send it. */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000764 if (!(orig_node->flags & VIS_SERVER))
765 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000766
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200767 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000768 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000769 continue;
770
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000771 /* don't send it if we already received the packet from
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200772 * this node.
773 */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000774 if (recv_list_is_in(bat_priv, &info->recv_list,
775 orig_node->orig)) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200776 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000777 continue;
778 }
779
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000781 hard_iface = router->if_incoming;
782 memcpy(dstaddr, router->addr, ETH_ALEN);
783
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200784 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000785
786 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
787 if (skb)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200788 batadv_send_skb_packet(skb, hard_iface,
789 dstaddr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000790
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000792 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794}
795
796static void unicast_vis_packet(struct bat_priv *bat_priv,
797 struct vis_info *info)
798{
799 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000800 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000801 struct sk_buff *skb;
802 struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000803
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000805
Marek Lindner7aadf882011-02-18 12:28:09 +0000806 orig_node = orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000807 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000808 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000809
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200810 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000811 if (!router)
812 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000813
814 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
815 if (skb)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200816 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000817
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000818out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000819 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200820 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000821 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200822 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000823}
824
825/* only send one vis packet. called from send_vis_packets() */
826static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
827{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200828 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000829 struct vis_packet *packet;
830
Marek Lindner32ae9b22011-04-20 15:40:58 +0200831 primary_if = primary_if_get_selected(bat_priv);
832 if (!primary_if)
833 goto out;
834
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000835 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100836 if (packet->header.ttl < 2) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837 pr_debug("Error - can't send vis packet: ttl exceeded\n");
Marek Lindner32ae9b22011-04-20 15:40:58 +0200838 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000839 }
840
Marek Lindner32ae9b22011-04-20 15:40:58 +0200841 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100842 packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000843
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);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100848 packet->header.ttl++; /* restore TTL */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200849
850out:
851 if (primary_if)
852 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853}
854
855/* called from timer; send (and maybe generate) vis packet. */
856static 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);
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100862 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863
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
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100872 while (!list_empty(&bat_priv->vis_send_list)) {
873 info = list_first_entry(&bat_priv->vis_send_list,
874 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875
876 kref_get(&info->refcount);
877 spin_unlock_bh(&bat_priv->vis_hash_lock);
878
Marek Lindner32ae9b22011-04-20 15:40:58 +0200879 send_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000880
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
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200890 * initialized (e.g. bat0 is initialized, interfaces have been added)
891 */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200892int batadv_vis_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000893{
894 struct vis_packet *packet;
895 int hash_added;
896
897 if (bat_priv->vis_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200898 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000899
900 spin_lock_bh(&bat_priv->vis_hash_lock);
901
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200902 bat_priv->vis_hash = batadv_hash_new(256);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000903 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);
Joe Perches320f4222011-08-29 14:17:24 -0700909 if (!bat_priv->my_vis_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000910 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000911
Sven Eckelmann704509b2011-05-14 23:14:54 +0200912 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(sizeof(*packet) +
913 MAX_VIS_PACKET_SIZE +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100914 ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915 if (!bat_priv->my_vis_info->skb_packet)
916 goto free_info;
917
Antonio Quartulli0d125072012-02-18 11:27:34 +0100918 skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200919 packet = (struct vis_packet *)skb_put(bat_priv->my_vis_info->skb_packet,
920 sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000921
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;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100929 packet->header.version = COMPAT_VERSION;
930 packet->header.packet_type = BAT_VIS;
931 packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000932 packet->seqno = 0;
933 packet->entries = 0;
934
935 INIT_LIST_HEAD(&bat_priv->vis_send_list);
936
937 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
Marek Lindner7aadf882011-02-18 12:28:09 +0000938 bat_priv->my_vis_info,
939 &bat_priv->my_vis_info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200940 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000941 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);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200949 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950
951free_info:
952 kfree(bat_priv->my_vis_info);
953 bat_priv->my_vis_info = NULL;
954err:
955 spin_unlock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200956 batadv_vis_quit(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200957 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000958}
959
960/* Decrease the reference count on a hash item info */
Marek Lindner7aadf882011-02-18 12:28:09 +0000961static void free_info_ref(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000962{
Marek Lindner7aadf882011-02-18 12:28:09 +0000963 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000964
Marek Lindner7aadf882011-02-18 12:28:09 +0000965 info = container_of(node, struct vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000966 send_list_del(info);
967 kref_put(&info->refcount, free_info);
968}
969
970/* shutdown vis-server */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200971void batadv_vis_quit(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000972{
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 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 */
987static void start_vis_timer(struct bat_priv *bat_priv)
988{
989 INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200990 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis_work,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000991 msecs_to_jiffies(VIS_INTERVAL));
992}