blob: d45989e0bbd7e3424d0d7725ab3b3908ac273b9d [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;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +020062 return batadv_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 Eckelmann1eda58b2012-05-12 13:48:58 +0200130 if (batadv_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);
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200184 else if (batadv_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
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200211 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200212 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;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200236 if (batadv_compare_eth(entries[j].src,
237 packet->vis_orig))
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200238 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 */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200248 if (batadv_compare_eth(entry->addr,
249 packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250 buf_size +=
251 vis_data_count_prim_sec(&vis_if_list);
252
253 buf_size += 1;
254 }
255
256 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
257 list) {
258 hlist_del(&entry->list);
259 kfree(entry);
260 }
261 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000262 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263 }
264
265 buff = kmalloc(buf_size, GFP_ATOMIC);
266 if (!buff) {
267 spin_unlock_bh(&bat_priv->vis_hash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200268 ret = -ENOMEM;
269 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270 }
271 buff[0] = '\0';
272 buff_pos = 0;
273
274 for (i = 0; i < hash->size; i++) {
275 head = &hash->table[i];
276
Marek Lindner7aadf882011-02-18 12:28:09 +0000277 rcu_read_lock();
278 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279 packet = (struct vis_packet *)info->skb_packet->data;
280 entries = (struct vis_info_entry *)
Sven Eckelmann704509b2011-05-14 23:14:54 +0200281 ((char *)packet + sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000282
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200283 vis_data_insert_interface(packet->vis_orig,
284 &vis_if_list, true);
285
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286 for (j = 0; j < packet->entries; j++) {
287 if (entries[j].quality == 0)
288 continue;
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200289 if (batadv_compare_eth(entries[j].src,
290 packet->vis_orig))
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200291 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 vis_data_insert_interface(entries[j].src,
293 &vis_if_list,
Matthias Schifferbeeb96a2012-05-05 17:51:53 +0200294 false);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295 }
296
297 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
298 buff_pos += sprintf(buff + buff_pos, "%pM,",
299 entry->addr);
300
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000301 for (j = 0; j < packet->entries; j++)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000302 buff_pos += vis_data_read_entry(
303 buff + buff_pos,
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000304 &entries[j],
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305 entry->addr,
306 entry->primary);
307
308 /* add primary/secondary records */
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200309 if (batadv_compare_eth(entry->addr,
310 packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311 buff_pos +=
312 vis_data_read_prim_sec(buff + buff_pos,
313 &vis_if_list);
314
315 buff_pos += sprintf(buff + buff_pos, "\n");
316 }
317
318 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
319 list) {
320 hlist_del(&entry->list);
321 kfree(entry);
322 }
323 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000324 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000325 }
326
327 spin_unlock_bh(&bat_priv->vis_hash_lock);
328
329 seq_printf(seq, "%s", buff);
330 kfree(buff);
331
Marek Lindner32ae9b22011-04-20 15:40:58 +0200332out:
333 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200334 batadv_hardif_free_ref(primary_if);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200335 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000336}
337
338/* add the info packet to the send list, if it was not
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200339 * already linked in.
340 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info)
342{
343 if (list_empty(&info->send_list)) {
344 kref_get(&info->refcount);
345 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
346 }
347}
348
349/* delete the info packet from the send list, if it was
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200350 * linked in.
351 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352static void send_list_del(struct vis_info *info)
353{
354 if (!list_empty(&info->send_list)) {
355 list_del_init(&info->send_list);
356 kref_put(&info->refcount, free_info);
357 }
358}
359
360/* tries to add one entry to the receive list. */
361static void recv_list_add(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200362 struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363{
364 struct recvlist_node *entry;
365
Sven Eckelmann704509b2011-05-14 23:14:54 +0200366 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000367 if (!entry)
368 return;
369
370 memcpy(entry->mac, mac, ETH_ALEN);
371 spin_lock_bh(&bat_priv->vis_list_lock);
372 list_add_tail(&entry->list, recv_list);
373 spin_unlock_bh(&bat_priv->vis_list_lock);
374}
375
376/* returns 1 if this mac is in the recv_list */
377static int recv_list_is_in(struct bat_priv *bat_priv,
Sven Eckelmann747e4222011-05-14 23:14:50 +0200378 const struct list_head *recv_list, const char *mac)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200380 const struct recvlist_node *entry;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381
382 spin_lock_bh(&bat_priv->vis_list_lock);
383 list_for_each_entry(entry, recv_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200384 if (batadv_compare_eth(entry->mac, mac)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385 spin_unlock_bh(&bat_priv->vis_list_lock);
386 return 1;
387 }
388 }
389 spin_unlock_bh(&bat_priv->vis_list_lock);
390 return 0;
391}
392
393/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
394 * broken.. ). vis hash must be locked outside. is_new is set when the packet
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200395 * is newer than old entries in the hash.
396 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000397static struct vis_info *add_packet(struct bat_priv *bat_priv,
398 struct vis_packet *vis_packet,
399 int vis_info_len, int *is_new,
400 int make_broadcast)
401{
402 struct vis_info *info, *old_info;
403 struct vis_packet *search_packet, *old_packet;
404 struct vis_info search_elem;
405 struct vis_packet *packet;
406 int hash_added;
407
408 *is_new = 0;
409 /* sanity check */
410 if (!bat_priv->vis_hash)
411 return NULL;
412
413 /* see if the packet is already in vis_hash */
Sven Eckelmann704509b2011-05-14 23:14:54 +0200414 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415 if (!search_elem.skb_packet)
416 return NULL;
417 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
Sven Eckelmann704509b2011-05-14 23:14:54 +0200418 sizeof(*search_packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000419
420 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Marek Lindner7aadf882011-02-18 12:28:09 +0000421 old_info = vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000422 kfree_skb(search_elem.skb_packet);
423
424 if (old_info) {
425 old_packet = (struct vis_packet *)old_info->skb_packet->data;
426 if (!seq_after(ntohl(vis_packet->seqno),
427 ntohl(old_packet->seqno))) {
428 if (old_packet->seqno == vis_packet->seqno) {
429 recv_list_add(bat_priv, &old_info->recv_list,
430 vis_packet->sender_orig);
431 return old_info;
432 } else {
433 /* newer packet is already in hash. */
434 return NULL;
435 }
436 }
437 /* remove old entry */
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200438 batadv_hash_remove(bat_priv->vis_hash, vis_info_cmp,
439 vis_info_choose, old_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000440 send_list_del(old_info);
441 kref_put(&old_info->refcount, free_info);
442 }
443
Sven Eckelmann704509b2011-05-14 23:14:54 +0200444 info = kmalloc(sizeof(*info), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000445 if (!info)
446 return NULL;
447
Sven Eckelmann704509b2011-05-14 23:14:54 +0200448 info->skb_packet = dev_alloc_skb(sizeof(*packet) + vis_info_len +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100449 ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000450 if (!info->skb_packet) {
451 kfree(info);
452 return NULL;
453 }
Antonio Quartulli0d125072012-02-18 11:27:34 +0100454 skb_reserve(info->skb_packet, ETH_HLEN);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200455 packet = (struct vis_packet *)skb_put(info->skb_packet, sizeof(*packet)
456 + vis_info_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000457
458 kref_init(&info->refcount);
459 INIT_LIST_HEAD(&info->send_list);
460 INIT_LIST_HEAD(&info->recv_list);
461 info->first_seen = jiffies;
462 info->bat_priv = bat_priv;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200463 memcpy(packet, vis_packet, sizeof(*packet) + vis_info_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000464
465 /* initialize and add new packet. */
466 *is_new = 1;
467
468 /* Make it a broadcast packet, if required */
469 if (make_broadcast)
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200470 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000471
472 /* repair if entries is longer than packet. */
473 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
474 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
475
476 recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
477
478 /* try to add it */
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200479 hash_added = batadv_hash_add(bat_priv->vis_hash, vis_info_cmp,
480 vis_info_choose, info, &info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200481 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000482 /* did not work (for some reason) */
Sven Eckelmann2674c152011-01-28 18:34:05 +0100483 kref_put(&info->refcount, free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000484 info = NULL;
485 }
486
487 return info;
488}
489
490/* handle the server sync packet, forward if needed. */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200491void batadv_receive_server_sync_packet(struct bat_priv *bat_priv,
492 struct vis_packet *vis_packet,
493 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000494{
495 struct vis_info *info;
496 int is_new, make_broadcast;
497 int vis_server = atomic_read(&bat_priv->vis_mode);
498
499 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
500
501 spin_lock_bh(&bat_priv->vis_hash_lock);
502 info = add_packet(bat_priv, vis_packet, vis_info_len,
503 &is_new, make_broadcast);
504 if (!info)
505 goto end;
506
507 /* only if we are server ourselves and packet is newer than the one in
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200508 * hash.
509 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000510 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
511 send_list_add(bat_priv, info);
512end:
513 spin_unlock_bh(&bat_priv->vis_hash_lock);
514}
515
516/* handle an incoming client update packet and schedule forward if needed. */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200517void batadv_receive_client_update_packet(struct bat_priv *bat_priv,
518 struct vis_packet *vis_packet,
519 int vis_info_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000520{
521 struct vis_info *info;
522 struct vis_packet *packet;
523 int is_new;
524 int vis_server = atomic_read(&bat_priv->vis_mode);
525 int are_target = 0;
526
527 /* clients shall not broadcast. */
528 if (is_broadcast_ether_addr(vis_packet->target_orig))
529 return;
530
531 /* Are we the target for this VIS packet? */
532 if (vis_server == VIS_TYPE_SERVER_SYNC &&
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200533 batadv_is_my_mac(vis_packet->target_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000534 are_target = 1;
535
536 spin_lock_bh(&bat_priv->vis_hash_lock);
537 info = add_packet(bat_priv, vis_packet, vis_info_len,
538 &is_new, are_target);
539
540 if (!info)
541 goto end;
542 /* note that outdated packets will be dropped at this point. */
543
544 packet = (struct vis_packet *)info->skb_packet->data;
545
546 /* send only if we're the target server or ... */
547 if (are_target && is_new) {
548 packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
549 send_list_add(bat_priv, info);
550
551 /* ... we're not the recipient (and thus need to forward). */
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200552 } else if (!batadv_is_my_mac(packet->target_orig)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000553 send_list_add(bat_priv, info);
554 }
555
556end:
557 spin_unlock_bh(&bat_priv->vis_hash_lock);
558}
559
560/* Walk the originators and find the VIS server with the best tq. Set the packet
561 * address to its address and return the best_tq.
562 *
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200563 * Must be called with the originator hash locked
564 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000565static int find_best_vis_server(struct bat_priv *bat_priv,
566 struct vis_info *info)
567{
568 struct hashtable_t *hash = bat_priv->orig_hash;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000569 struct neigh_node *router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000570 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000571 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000572 struct orig_node *orig_node;
573 struct vis_packet *packet;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200574 int best_tq = -1;
575 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000576
577 packet = (struct vis_packet *)info->skb_packet->data;
578
579 for (i = 0; i < hash->size; i++) {
580 head = &hash->table[i];
581
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000582 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000583 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200584 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000585 if (!router)
586 continue;
587
588 if ((orig_node->flags & VIS_SERVER) &&
589 (router->tq_avg > best_tq)) {
590 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000591 memcpy(packet->target_orig, orig_node->orig,
592 ETH_ALEN);
593 }
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200594 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000595 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000596 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000597 }
598
599 return best_tq;
600}
601
602/* Return true if the vis packet is full. */
Sven Eckelmann747e4222011-05-14 23:14:50 +0200603static bool vis_packet_full(const struct vis_info *info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000604{
Sven Eckelmann747e4222011-05-14 23:14:50 +0200605 const struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000606 packet = (struct vis_packet *)info->skb_packet->data;
607
608 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
609 < packet->entries + 1)
610 return true;
611 return false;
612}
613
614/* generates a packet of own vis data,
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200615 * returns 0 on success, -1 if no packet could be generated
616 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000617static int generate_vis_packet(struct bat_priv *bat_priv)
618{
619 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000620 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000622 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000623 struct neigh_node *router;
Sven Eckelmann958ca592011-05-14 23:14:53 +0200624 struct vis_info *info = bat_priv->my_vis_info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000625 struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
626 struct vis_info_entry *entry;
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100627 struct tt_common_entry *tt_common_entry;
Antonio Quartullic90681b2011-10-05 17:05:25 +0200628 int best_tq = -1;
629 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630
631 info->first_seen = jiffies;
632 packet->vis_type = atomic_read(&bat_priv->vis_mode);
633
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200634 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100635 packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000636 packet->seqno = htonl(ntohl(packet->seqno) + 1);
637 packet->entries = 0;
Sven Eckelmann704509b2011-05-14 23:14:54 +0200638 skb_trim(info->skb_packet, sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639
640 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
641 best_tq = find_best_vis_server(bat_priv, info);
642
Marek Lindnerd0072602011-01-19 20:01:44 +0000643 if (best_tq < 0)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200644 return best_tq;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645 }
646
647 for (i = 0; i < hash->size; i++) {
648 head = &hash->table[i];
649
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000650 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000651 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200652 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000653 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000654 continue;
655
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200656 if (!batadv_compare_eth(router->addr, orig_node->orig))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000657 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000659 if (router->if_incoming->if_status != IF_ACTIVE)
660 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000662 if (router->tq_avg < 1)
663 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000664
665 /* fill one entry into buffer. */
666 entry = (struct vis_info_entry *)
667 skb_put(info->skb_packet, sizeof(*entry));
668 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000669 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000670 ETH_ALEN);
671 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000672 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673 packet->entries++;
674
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000675next:
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200676 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000677
Marek Lindnerd0072602011-01-19 20:01:44 +0000678 if (vis_packet_full(info))
679 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000681 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000682 }
683
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200684 hash = bat_priv->tt_local_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000685
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000686 for (i = 0; i < hash->size; i++) {
687 head = &hash->table[i];
688
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200689 rcu_read_lock();
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100690 hlist_for_each_entry_rcu(tt_common_entry, node, head,
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200691 hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000692 entry = (struct vis_info_entry *)
693 skb_put(info->skb_packet,
694 sizeof(*entry));
695 memset(entry->src, 0, ETH_ALEN);
Antonio Quartulli48100ba2011-10-30 12:17:33 +0100696 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200697 entry->quality = 0; /* 0 means TT */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000698 packet->entries++;
699
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200700 if (vis_packet_full(info))
701 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000702 }
Antonio Quartulli7683fdc2011-04-27 14:28:07 +0200703 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000704 }
705
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000706 return 0;
Marek Lindnerd0072602011-01-19 20:01:44 +0000707
708unlock:
709 rcu_read_unlock();
710 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711}
712
713/* free old vis packets. Must be called with this vis_hash_lock
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200714 * held
715 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000716static void purge_vis_packets(struct bat_priv *bat_priv)
717{
Antonio Quartullic90681b2011-10-05 17:05:25 +0200718 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719 struct hashtable_t *hash = bat_priv->vis_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000720 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000721 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000722 struct vis_info *info;
723
724 for (i = 0; i < hash->size; i++) {
725 head = &hash->table[i];
726
Marek Lindner7aadf882011-02-18 12:28:09 +0000727 hlist_for_each_entry_safe(info, node, node_tmp,
728 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000729 /* never purge own data. */
730 if (info == bat_priv->my_vis_info)
731 continue;
732
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200733 if (batadv_has_timed_out(info->first_seen,
734 VIS_TIMEOUT)) {
Marek Lindner7aadf882011-02-18 12:28:09 +0000735 hlist_del(node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000736 send_list_del(info);
737 kref_put(&info->refcount, free_info);
738 }
739 }
740 }
741}
742
743static void broadcast_vis_packet(struct bat_priv *bat_priv,
744 struct vis_info *info)
745{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000746 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000748 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000749 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000750 struct orig_node *orig_node;
751 struct vis_packet *packet;
752 struct sk_buff *skb;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000753 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000754 uint8_t dstaddr[ETH_ALEN];
Antonio Quartullic90681b2011-10-05 17:05:25 +0200755 uint32_t i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000756
757
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000758 packet = (struct vis_packet *)info->skb_packet->data;
759
760 /* send to all routers in range. */
761 for (i = 0; i < hash->size; i++) {
762 head = &hash->table[i];
763
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000764 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000765 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000766 /* if it's a vis server and reachable, send it. */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000767 if (!(orig_node->flags & VIS_SERVER))
768 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000769
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200770 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000771 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000772 continue;
773
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000774 /* don't send it if we already received the packet from
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200775 * this node.
776 */
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000777 if (recv_list_is_in(bat_priv, &info->recv_list,
778 orig_node->orig)) {
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200779 batadv_neigh_node_free_ref(router);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000780 continue;
781 }
782
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000784 hard_iface = router->if_incoming;
785 memcpy(dstaddr, router->addr, ETH_ALEN);
786
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200787 batadv_neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788
789 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
790 if (skb)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200791 batadv_send_skb_packet(skb, hard_iface,
792 dstaddr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000795 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000796 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000797}
798
799static void unicast_vis_packet(struct bat_priv *bat_priv,
800 struct vis_info *info)
801{
802 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000803 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804 struct sk_buff *skb;
805 struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000806
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808
Sven Eckelmannda641192012-05-12 13:48:56 +0200809 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000810 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000811 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000812
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200813 router = batadv_orig_node_get_router(orig_node);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000814 if (!router)
815 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816
817 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
818 if (skb)
Sven Eckelmann9455e342012-05-12 02:09:37 +0200819 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000821out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000822 if (router)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200823 batadv_neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000824 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200825 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000826}
827
828/* only send one vis packet. called from send_vis_packets() */
829static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
830{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200831 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000832 struct vis_packet *packet;
833
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200834 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200835 if (!primary_if)
836 goto out;
837
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000838 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100839 if (packet->header.ttl < 2) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840 pr_debug("Error - can't send vis packet: ttl exceeded\n");
Marek Lindner32ae9b22011-04-20 15:40:58 +0200841 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000842 }
843
Marek Lindner32ae9b22011-04-20 15:40:58 +0200844 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100845 packet->header.ttl--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000846
847 if (is_broadcast_ether_addr(packet->target_orig))
848 broadcast_vis_packet(bat_priv, info);
849 else
850 unicast_vis_packet(bat_priv, info);
Sven Eckelmann76543d12011-11-20 15:47:38 +0100851 packet->header.ttl++; /* restore TTL */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200852
853out:
854 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200855 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000856}
857
858/* called from timer; send (and maybe generate) vis packet. */
859static void send_vis_packets(struct work_struct *work)
860{
861 struct delayed_work *delayed_work =
862 container_of(work, struct delayed_work, work);
863 struct bat_priv *bat_priv =
864 container_of(delayed_work, struct bat_priv, vis_work);
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100865 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000866
867 spin_lock_bh(&bat_priv->vis_hash_lock);
868 purge_vis_packets(bat_priv);
869
870 if (generate_vis_packet(bat_priv) == 0) {
871 /* schedule if generation was successful */
872 send_list_add(bat_priv, bat_priv->my_vis_info);
873 }
874
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100875 while (!list_empty(&bat_priv->vis_send_list)) {
876 info = list_first_entry(&bat_priv->vis_send_list,
877 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000878
879 kref_get(&info->refcount);
880 spin_unlock_bh(&bat_priv->vis_hash_lock);
881
Marek Lindner32ae9b22011-04-20 15:40:58 +0200882 send_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000883
884 spin_lock_bh(&bat_priv->vis_hash_lock);
885 send_list_del(info);
886 kref_put(&info->refcount, free_info);
887 }
888 spin_unlock_bh(&bat_priv->vis_hash_lock);
889 start_vis_timer(bat_priv);
890}
891
892/* init the vis server. this may only be called when if_list is already
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200893 * initialized (e.g. bat0 is initialized, interfaces have been added)
894 */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200895int batadv_vis_init(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000896{
897 struct vis_packet *packet;
898 int hash_added;
899
900 if (bat_priv->vis_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200901 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000902
903 spin_lock_bh(&bat_priv->vis_hash_lock);
904
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200905 bat_priv->vis_hash = batadv_hash_new(256);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000906 if (!bat_priv->vis_hash) {
907 pr_err("Can't initialize vis_hash\n");
908 goto err;
909 }
910
911 bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
Joe Perches320f4222011-08-29 14:17:24 -0700912 if (!bat_priv->my_vis_info)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913 goto err;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000914
Sven Eckelmann704509b2011-05-14 23:14:54 +0200915 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(sizeof(*packet) +
916 MAX_VIS_PACKET_SIZE +
Antonio Quartulli0d125072012-02-18 11:27:34 +0100917 ETH_HLEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000918 if (!bat_priv->my_vis_info->skb_packet)
919 goto free_info;
920
Antonio Quartulli0d125072012-02-18 11:27:34 +0100921 skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
Sven Eckelmann704509b2011-05-14 23:14:54 +0200922 packet = (struct vis_packet *)skb_put(bat_priv->my_vis_info->skb_packet,
923 sizeof(*packet));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000924
925 /* prefill the vis info */
926 bat_priv->my_vis_info->first_seen = jiffies -
927 msecs_to_jiffies(VIS_INTERVAL);
928 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
929 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
930 kref_init(&bat_priv->my_vis_info->refcount);
931 bat_priv->my_vis_info->bat_priv = bat_priv;
Sven Eckelmann76543d12011-11-20 15:47:38 +0100932 packet->header.version = COMPAT_VERSION;
933 packet->header.packet_type = BAT_VIS;
934 packet->header.ttl = TTL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000935 packet->seqno = 0;
936 packet->entries = 0;
937
938 INIT_LIST_HEAD(&bat_priv->vis_send_list);
939
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200940 hash_added = batadv_hash_add(bat_priv->vis_hash, vis_info_cmp,
941 vis_info_choose, bat_priv->my_vis_info,
942 &bat_priv->my_vis_info->hash_entry);
Antonio Quartulli1a1f37d2011-07-10 00:36:36 +0200943 if (hash_added != 0) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000944 pr_err("Can't add own vis packet into hash\n");
945 /* not in hash, need to remove it manually. */
946 kref_put(&bat_priv->my_vis_info->refcount, free_info);
947 goto err;
948 }
949
950 spin_unlock_bh(&bat_priv->vis_hash_lock);
951 start_vis_timer(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200952 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000953
954free_info:
955 kfree(bat_priv->my_vis_info);
956 bat_priv->my_vis_info = NULL;
957err:
958 spin_unlock_bh(&bat_priv->vis_hash_lock);
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200959 batadv_vis_quit(bat_priv);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200960 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000961}
962
963/* Decrease the reference count on a hash item info */
Marek Lindner7aadf882011-02-18 12:28:09 +0000964static void free_info_ref(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000965{
Marek Lindner7aadf882011-02-18 12:28:09 +0000966 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000967
Marek Lindner7aadf882011-02-18 12:28:09 +0000968 info = container_of(node, struct vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000969 send_list_del(info);
970 kref_put(&info->refcount, free_info);
971}
972
973/* shutdown vis-server */
Sven Eckelmannd0f714f2012-05-12 02:09:41 +0200974void batadv_vis_quit(struct bat_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000975{
976 if (!bat_priv->vis_hash)
977 return;
978
979 cancel_delayed_work_sync(&bat_priv->vis_work);
980
981 spin_lock_bh(&bat_priv->vis_hash_lock);
982 /* properly remove, kill timers ... */
Sven Eckelmannc0a55922012-05-12 13:48:55 +0200983 batadv_hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000984 bat_priv->vis_hash = NULL;
985 bat_priv->my_vis_info = NULL;
986 spin_unlock_bh(&bat_priv->vis_hash_lock);
987}
988
989/* schedule packets for (re)transmission */
990static void start_vis_timer(struct bat_priv *bat_priv)
991{
992 INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
Sven Eckelmann3193e8f2012-05-12 02:09:42 +0200993 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis_work,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000994 msecs_to_jiffies(VIS_INTERVAL));
995}