blob: d4cc4f5399f464f360e5ab3233f2d1190cf063b2 [file] [log] [blame]
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001/*
Sven Eckelmann64afe352011-01-27 10:38:15 +01002 * Copyright (C) 2008-2011 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
33/* Returns the smallest signed integer in two's complement with the sizeof x */
34#define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u)))
35
36/* Checks if a sequence number x is a predecessor/successor of y.
37 * they handle overflows/underflows and can correctly check for a
38 * predecessor/successor unless the variable sequence number has grown by
39 * more then 2**(bitwidth(x)-1)-1.
40 * This means that for a uint8_t with the maximum value 255, it would think:
41 * - when adding nothing - it is neither a predecessor nor a successor
42 * - before adding more than 127 to the starting value - it is a predecessor,
43 * - when adding 128 - it is neither a predecessor nor a successor,
44 * - after adding more than 127 to the starting value - it is a successor */
45#define seq_before(x, y) ({typeof(x) _dummy = (x - y); \
46 _dummy > smallest_signed_int(_dummy); })
47#define seq_after(x, y) seq_before(y, x)
48
49static void start_vis_timer(struct bat_priv *bat_priv);
50
51/* free the info */
52static void free_info(struct kref *ref)
53{
54 struct vis_info *info = container_of(ref, struct vis_info, refcount);
55 struct bat_priv *bat_priv = info->bat_priv;
56 struct recvlist_node *entry, *tmp;
57
58 list_del_init(&info->send_list);
59 spin_lock_bh(&bat_priv->vis_list_lock);
60 list_for_each_entry_safe(entry, tmp, &info->recv_list, list) {
61 list_del(&entry->list);
62 kfree(entry);
63 }
64
65 spin_unlock_bh(&bat_priv->vis_list_lock);
66 kfree_skb(info->skb_packet);
Sven Eckelmanndda9fc62011-01-28 18:34:06 +010067 kfree(info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000068}
69
70/* Compare two vis packets, used by the hashing algorithm */
Marek Lindner7aadf882011-02-18 12:28:09 +000071static int vis_info_cmp(struct hlist_node *node, void *data2)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072{
73 struct vis_info *d1, *d2;
74 struct vis_packet *p1, *p2;
Marek Lindner7aadf882011-02-18 12:28:09 +000075
76 d1 = container_of(node, struct vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000077 d2 = data2;
78 p1 = (struct vis_packet *)d1->skb_packet->data;
79 p2 = (struct vis_packet *)d2->skb_packet->data;
Marek Lindner39901e72011-02-18 12:28:08 +000080 return compare_eth(p1->vis_orig, p2->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081}
82
83/* hash function to choose an entry in a hash table of given size */
84/* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */
85static int vis_info_choose(void *data, int size)
86{
87 struct vis_info *vis_info = data;
88 struct vis_packet *packet;
89 unsigned char *key;
90 uint32_t hash = 0;
91 size_t i;
92
93 packet = (struct vis_packet *)vis_info->skb_packet->data;
94 key = packet->vis_orig;
95 for (i = 0; i < ETH_ALEN; i++) {
96 hash += key[i];
97 hash += (hash << 10);
98 hash ^= (hash >> 6);
99 }
100
101 hash += (hash << 3);
102 hash ^= (hash >> 11);
103 hash += (hash << 15);
104
105 return hash % size;
106}
107
Marek Lindner7aadf882011-02-18 12:28:09 +0000108static struct vis_info *vis_hash_find(struct bat_priv *bat_priv,
109 void *data)
110{
111 struct hashtable_t *hash = bat_priv->vis_hash;
112 struct hlist_head *head;
113 struct hlist_node *node;
114 struct vis_info *vis_info, *vis_info_tmp = NULL;
115 int index;
116
117 if (!hash)
118 return NULL;
119
120 index = vis_info_choose(data, hash->size);
121 head = &hash->table[index];
122
123 rcu_read_lock();
124 hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
125 if (!vis_info_cmp(node, data))
126 continue;
127
128 vis_info_tmp = vis_info;
129 break;
130 }
131 rcu_read_unlock();
132
133 return vis_info_tmp;
134}
135
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136/* insert interface to the list of interfaces of one originator, if it
137 * does not already exist in the list */
138static void vis_data_insert_interface(const uint8_t *interface,
139 struct hlist_head *if_list,
140 bool primary)
141{
142 struct if_list_entry *entry;
143 struct hlist_node *pos;
144
145 hlist_for_each_entry(entry, pos, if_list, list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000146 if (compare_eth(entry->addr, (void *)interface))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000147 return;
148 }
149
150 /* its a new address, add it to the list */
151 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
152 if (!entry)
153 return;
154 memcpy(entry->addr, interface, ETH_ALEN);
155 entry->primary = primary;
156 hlist_add_head(&entry->list, if_list);
157}
158
159static ssize_t vis_data_read_prim_sec(char *buff, struct hlist_head *if_list)
160{
161 struct if_list_entry *entry;
162 struct hlist_node *pos;
163 size_t len = 0;
164
165 hlist_for_each_entry(entry, pos, if_list, list) {
166 if (entry->primary)
167 len += sprintf(buff + len, "PRIMARY, ");
168 else
169 len += sprintf(buff + len, "SEC %pM, ", entry->addr);
170 }
171
172 return len;
173}
174
175static size_t vis_data_count_prim_sec(struct hlist_head *if_list)
176{
177 struct if_list_entry *entry;
178 struct hlist_node *pos;
179 size_t count = 0;
180
181 hlist_for_each_entry(entry, pos, if_list, list) {
182 if (entry->primary)
183 count += 9;
184 else
185 count += 23;
186 }
187
188 return count;
189}
190
191/* read an entry */
192static ssize_t vis_data_read_entry(char *buff, struct vis_info_entry *entry,
193 uint8_t *src, bool primary)
194{
195 /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */
196 if (primary && entry->quality == 0)
197 return sprintf(buff, "HNA %pM, ", entry->dest);
Marek Lindner39901e72011-02-18 12:28:08 +0000198 else if (compare_eth(entry->src, src))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199 return sprintf(buff, "TQ %pM %d, ", entry->dest,
200 entry->quality);
201
202 return 0;
203}
204
205int vis_seq_print_text(struct seq_file *seq, void *offset)
206{
Marek Lindner7aadf882011-02-18 12:28:09 +0000207 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209 struct vis_info *info;
210 struct vis_packet *packet;
211 struct vis_info_entry *entries;
212 struct net_device *net_dev = (struct net_device *)seq->private;
213 struct bat_priv *bat_priv = netdev_priv(net_dev);
214 struct hashtable_t *hash = bat_priv->vis_hash;
215 HLIST_HEAD(vis_if_list);
216 struct if_list_entry *entry;
217 struct hlist_node *pos, *n;
218 int i, j;
219 int vis_server = atomic_read(&bat_priv->vis_mode);
220 size_t buff_pos, buf_size;
221 char *buff;
222 int compare;
223
224 if ((!bat_priv->primary_if) ||
225 (vis_server == VIS_TYPE_CLIENT_UPDATE))
226 return 0;
227
228 buf_size = 1;
229 /* Estimate length */
230 spin_lock_bh(&bat_priv->vis_hash_lock);
231 for (i = 0; i < hash->size; i++) {
232 head = &hash->table[i];
233
Marek Lindner7aadf882011-02-18 12:28:09 +0000234 rcu_read_lock();
235 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236 packet = (struct vis_packet *)info->skb_packet->data;
237 entries = (struct vis_info_entry *)
238 ((char *)packet + sizeof(struct vis_packet));
239
240 for (j = 0; j < packet->entries; j++) {
241 if (entries[j].quality == 0)
242 continue;
243 compare =
Marek Lindner39901e72011-02-18 12:28:08 +0000244 compare_eth(entries[j].src, packet->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245 vis_data_insert_interface(entries[j].src,
246 &vis_if_list,
247 compare);
248 }
249
250 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
251 buf_size += 18 + 26 * packet->entries;
252
253 /* add primary/secondary records */
Marek Lindner39901e72011-02-18 12:28:08 +0000254 if (compare_eth(entry->addr, packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255 buf_size +=
256 vis_data_count_prim_sec(&vis_if_list);
257
258 buf_size += 1;
259 }
260
261 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
262 list) {
263 hlist_del(&entry->list);
264 kfree(entry);
265 }
266 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000267 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000268 }
269
270 buff = kmalloc(buf_size, GFP_ATOMIC);
271 if (!buff) {
272 spin_unlock_bh(&bat_priv->vis_hash_lock);
273 return -ENOMEM;
274 }
275 buff[0] = '\0';
276 buff_pos = 0;
277
278 for (i = 0; i < hash->size; i++) {
279 head = &hash->table[i];
280
Marek Lindner7aadf882011-02-18 12:28:09 +0000281 rcu_read_lock();
282 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283 packet = (struct vis_packet *)info->skb_packet->data;
284 entries = (struct vis_info_entry *)
285 ((char *)packet + sizeof(struct vis_packet));
286
287 for (j = 0; j < packet->entries; j++) {
288 if (entries[j].quality == 0)
289 continue;
290 compare =
Marek Lindner39901e72011-02-18 12:28:08 +0000291 compare_eth(entries[j].src, packet->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 vis_data_insert_interface(entries[j].src,
293 &vis_if_list,
294 compare);
295 }
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 */
Marek Lindner39901e72011-02-18 12:28:08 +0000309 if (compare_eth(entry->addr, packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310 buff_pos +=
311 vis_data_read_prim_sec(buff + buff_pos,
312 &vis_if_list);
313
314 buff_pos += sprintf(buff + buff_pos, "\n");
315 }
316
317 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
318 list) {
319 hlist_del(&entry->list);
320 kfree(entry);
321 }
322 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000323 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324 }
325
326 spin_unlock_bh(&bat_priv->vis_hash_lock);
327
328 seq_printf(seq, "%s", buff);
329 kfree(buff);
330
331 return 0;
332}
333
334/* add the info packet to the send list, if it was not
335 * already linked in. */
336static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info)
337{
338 if (list_empty(&info->send_list)) {
339 kref_get(&info->refcount);
340 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
341 }
342}
343
344/* delete the info packet from the send list, if it was
345 * linked in. */
346static void send_list_del(struct vis_info *info)
347{
348 if (!list_empty(&info->send_list)) {
349 list_del_init(&info->send_list);
350 kref_put(&info->refcount, free_info);
351 }
352}
353
354/* tries to add one entry to the receive list. */
355static void recv_list_add(struct bat_priv *bat_priv,
356 struct list_head *recv_list, char *mac)
357{
358 struct recvlist_node *entry;
359
360 entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC);
361 if (!entry)
362 return;
363
364 memcpy(entry->mac, mac, ETH_ALEN);
365 spin_lock_bh(&bat_priv->vis_list_lock);
366 list_add_tail(&entry->list, recv_list);
367 spin_unlock_bh(&bat_priv->vis_list_lock);
368}
369
370/* returns 1 if this mac is in the recv_list */
371static int recv_list_is_in(struct bat_priv *bat_priv,
372 struct list_head *recv_list, char *mac)
373{
374 struct recvlist_node *entry;
375
376 spin_lock_bh(&bat_priv->vis_list_lock);
377 list_for_each_entry(entry, recv_list, list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000378 if (compare_eth(entry->mac, mac)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379 spin_unlock_bh(&bat_priv->vis_list_lock);
380 return 1;
381 }
382 }
383 spin_unlock_bh(&bat_priv->vis_list_lock);
384 return 0;
385}
386
387/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
388 * broken.. ). vis hash must be locked outside. is_new is set when the packet
389 * is newer than old entries in the hash. */
390static struct vis_info *add_packet(struct bat_priv *bat_priv,
391 struct vis_packet *vis_packet,
392 int vis_info_len, int *is_new,
393 int make_broadcast)
394{
395 struct vis_info *info, *old_info;
396 struct vis_packet *search_packet, *old_packet;
397 struct vis_info search_elem;
398 struct vis_packet *packet;
399 int hash_added;
400
401 *is_new = 0;
402 /* sanity check */
403 if (!bat_priv->vis_hash)
404 return NULL;
405
406 /* see if the packet is already in vis_hash */
407 search_elem.skb_packet = dev_alloc_skb(sizeof(struct vis_packet));
408 if (!search_elem.skb_packet)
409 return NULL;
410 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
411 sizeof(struct vis_packet));
412
413 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Marek Lindner7aadf882011-02-18 12:28:09 +0000414 old_info = vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415 kfree_skb(search_elem.skb_packet);
416
417 if (old_info) {
418 old_packet = (struct vis_packet *)old_info->skb_packet->data;
419 if (!seq_after(ntohl(vis_packet->seqno),
420 ntohl(old_packet->seqno))) {
421 if (old_packet->seqno == vis_packet->seqno) {
422 recv_list_add(bat_priv, &old_info->recv_list,
423 vis_packet->sender_orig);
424 return old_info;
425 } else {
426 /* newer packet is already in hash. */
427 return NULL;
428 }
429 }
430 /* remove old entry */
431 hash_remove(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
432 old_info);
433 send_list_del(old_info);
434 kref_put(&old_info->refcount, free_info);
435 }
436
437 info = kmalloc(sizeof(struct vis_info), GFP_ATOMIC);
438 if (!info)
439 return NULL;
440
441 info->skb_packet = dev_alloc_skb(sizeof(struct vis_packet) +
442 vis_info_len + sizeof(struct ethhdr));
443 if (!info->skb_packet) {
444 kfree(info);
445 return NULL;
446 }
447 skb_reserve(info->skb_packet, sizeof(struct ethhdr));
448 packet = (struct vis_packet *)skb_put(info->skb_packet,
449 sizeof(struct vis_packet) +
450 vis_info_len);
451
452 kref_init(&info->refcount);
453 INIT_LIST_HEAD(&info->send_list);
454 INIT_LIST_HEAD(&info->recv_list);
455 info->first_seen = jiffies;
456 info->bat_priv = bat_priv;
457 memcpy(packet, vis_packet, sizeof(struct vis_packet) + vis_info_len);
458
459 /* initialize and add new packet. */
460 *is_new = 1;
461
462 /* Make it a broadcast packet, if required */
463 if (make_broadcast)
464 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
465
466 /* repair if entries is longer than packet. */
467 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
468 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
469
470 recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
471
472 /* try to add it */
473 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
Marek Lindner7aadf882011-02-18 12:28:09 +0000474 info, &info->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000475 if (hash_added < 0) {
476 /* did not work (for some reason) */
Sven Eckelmann2674c152011-01-28 18:34:05 +0100477 kref_put(&info->refcount, free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000478 info = NULL;
479 }
480
481 return info;
482}
483
484/* handle the server sync packet, forward if needed. */
485void receive_server_sync_packet(struct bat_priv *bat_priv,
486 struct vis_packet *vis_packet,
487 int vis_info_len)
488{
489 struct vis_info *info;
490 int is_new, make_broadcast;
491 int vis_server = atomic_read(&bat_priv->vis_mode);
492
493 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
494
495 spin_lock_bh(&bat_priv->vis_hash_lock);
496 info = add_packet(bat_priv, vis_packet, vis_info_len,
497 &is_new, make_broadcast);
498 if (!info)
499 goto end;
500
501 /* only if we are server ourselves and packet is newer than the one in
502 * hash.*/
503 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
504 send_list_add(bat_priv, info);
505end:
506 spin_unlock_bh(&bat_priv->vis_hash_lock);
507}
508
509/* handle an incoming client update packet and schedule forward if needed. */
510void receive_client_update_packet(struct bat_priv *bat_priv,
511 struct vis_packet *vis_packet,
512 int vis_info_len)
513{
514 struct vis_info *info;
515 struct vis_packet *packet;
516 int is_new;
517 int vis_server = atomic_read(&bat_priv->vis_mode);
518 int are_target = 0;
519
520 /* clients shall not broadcast. */
521 if (is_broadcast_ether_addr(vis_packet->target_orig))
522 return;
523
524 /* Are we the target for this VIS packet? */
525 if (vis_server == VIS_TYPE_SERVER_SYNC &&
526 is_my_mac(vis_packet->target_orig))
527 are_target = 1;
528
529 spin_lock_bh(&bat_priv->vis_hash_lock);
530 info = add_packet(bat_priv, vis_packet, vis_info_len,
531 &is_new, are_target);
532
533 if (!info)
534 goto end;
535 /* note that outdated packets will be dropped at this point. */
536
537 packet = (struct vis_packet *)info->skb_packet->data;
538
539 /* send only if we're the target server or ... */
540 if (are_target && is_new) {
541 packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
542 send_list_add(bat_priv, info);
543
544 /* ... we're not the recipient (and thus need to forward). */
545 } else if (!is_my_mac(packet->target_orig)) {
546 send_list_add(bat_priv, info);
547 }
548
549end:
550 spin_unlock_bh(&bat_priv->vis_hash_lock);
551}
552
553/* Walk the originators and find the VIS server with the best tq. Set the packet
554 * address to its address and return the best_tq.
555 *
556 * Must be called with the originator hash locked */
557static int find_best_vis_server(struct bat_priv *bat_priv,
558 struct vis_info *info)
559{
560 struct hashtable_t *hash = bat_priv->orig_hash;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000561 struct neigh_node *router;
Marek Lindner7aadf882011-02-18 12:28:09 +0000562 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000563 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000564 struct orig_node *orig_node;
565 struct vis_packet *packet;
566 int best_tq = -1, i;
567
568 packet = (struct vis_packet *)info->skb_packet->data;
569
570 for (i = 0; i < hash->size; i++) {
571 head = &hash->table[i];
572
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000573 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000574 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000575 router = orig_node_get_router(orig_node);
576 if (!router)
577 continue;
578
579 if ((orig_node->flags & VIS_SERVER) &&
580 (router->tq_avg > best_tq)) {
581 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000582 memcpy(packet->target_orig, orig_node->orig,
583 ETH_ALEN);
584 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000585 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000586 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000587 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000588 }
589
590 return best_tq;
591}
592
593/* Return true if the vis packet is full. */
594static bool vis_packet_full(struct vis_info *info)
595{
596 struct vis_packet *packet;
597 packet = (struct vis_packet *)info->skb_packet->data;
598
599 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
600 < packet->entries + 1)
601 return true;
602 return false;
603}
604
605/* generates a packet of own vis data,
606 * returns 0 on success, -1 if no packet could be generated */
607static int generate_vis_packet(struct bat_priv *bat_priv)
608{
609 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000610 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000611 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000612 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000613 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000614 struct vis_info *info = (struct vis_info *)bat_priv->my_vis_info;
615 struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
616 struct vis_info_entry *entry;
617 struct hna_local_entry *hna_local_entry;
618 int best_tq = -1, i;
619
620 info->first_seen = jiffies;
621 packet->vis_type = atomic_read(&bat_priv->vis_mode);
622
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000623 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
624 packet->ttl = TTL;
625 packet->seqno = htonl(ntohl(packet->seqno) + 1);
626 packet->entries = 0;
627 skb_trim(info->skb_packet, sizeof(struct vis_packet));
628
629 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
630 best_tq = find_best_vis_server(bat_priv, info);
631
Marek Lindnerd0072602011-01-19 20:01:44 +0000632 if (best_tq < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000633 return -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634 }
635
636 for (i = 0; i < hash->size; i++) {
637 head = &hash->table[i];
638
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000639 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000640 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000641 router = orig_node_get_router(orig_node);
642 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643 continue;
644
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000645 if (!compare_eth(router->addr, orig_node->orig))
646 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000647
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000648 if (router->if_incoming->if_status != IF_ACTIVE)
649 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000650
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000651 if (router->tq_avg < 1)
652 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000653
654 /* fill one entry into buffer. */
655 entry = (struct vis_info_entry *)
656 skb_put(info->skb_packet, sizeof(*entry));
657 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000658 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000659 ETH_ALEN);
660 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000661 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662 packet->entries++;
663
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000664next:
665 neigh_node_free_ref(router);
666
Marek Lindnerd0072602011-01-19 20:01:44 +0000667 if (vis_packet_full(info))
668 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000669 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000670 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000671 }
672
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673 hash = bat_priv->hna_local_hash;
674
675 spin_lock_bh(&bat_priv->hna_lhash_lock);
676 for (i = 0; i < hash->size; i++) {
677 head = &hash->table[i];
678
Marek Lindner7aadf882011-02-18 12:28:09 +0000679 hlist_for_each_entry(hna_local_entry, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000680 entry = (struct vis_info_entry *)
681 skb_put(info->skb_packet,
682 sizeof(*entry));
683 memset(entry->src, 0, ETH_ALEN);
684 memcpy(entry->dest, hna_local_entry->addr, ETH_ALEN);
685 entry->quality = 0; /* 0 means HNA */
686 packet->entries++;
687
688 if (vis_packet_full(info)) {
689 spin_unlock_bh(&bat_priv->hna_lhash_lock);
690 return 0;
691 }
692 }
693 }
694
695 spin_unlock_bh(&bat_priv->hna_lhash_lock);
696 return 0;
Marek Lindnerd0072602011-01-19 20:01:44 +0000697
698unlock:
699 rcu_read_unlock();
700 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000701}
702
703/* free old vis packets. Must be called with this vis_hash_lock
704 * held */
705static void purge_vis_packets(struct bat_priv *bat_priv)
706{
707 int i;
708 struct hashtable_t *hash = bat_priv->vis_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000709 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000710 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000711 struct vis_info *info;
712
713 for (i = 0; i < hash->size; i++) {
714 head = &hash->table[i];
715
Marek Lindner7aadf882011-02-18 12:28:09 +0000716 hlist_for_each_entry_safe(info, node, node_tmp,
717 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718 /* never purge own data. */
719 if (info == bat_priv->my_vis_info)
720 continue;
721
722 if (time_after(jiffies,
723 info->first_seen + VIS_TIMEOUT * HZ)) {
Marek Lindner7aadf882011-02-18 12:28:09 +0000724 hlist_del(node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000725 send_list_del(info);
726 kref_put(&info->refcount, free_info);
727 }
728 }
729 }
730}
731
732static void broadcast_vis_packet(struct bat_priv *bat_priv,
733 struct vis_info *info)
734{
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000735 struct neigh_node *router;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000736 struct hashtable_t *hash = bat_priv->orig_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000737 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000738 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000739 struct orig_node *orig_node;
740 struct vis_packet *packet;
741 struct sk_buff *skb;
Marek Lindnere6c10f42011-02-18 12:33:20 +0000742 struct hard_iface *hard_iface;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000743 uint8_t dstaddr[ETH_ALEN];
744 int i;
745
746
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000747 packet = (struct vis_packet *)info->skb_packet->data;
748
749 /* send to all routers in range. */
750 for (i = 0; i < hash->size; i++) {
751 head = &hash->table[i];
752
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000753 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000754 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000755 /* if it's a vis server and reachable, send it. */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000756 if (!(orig_node->flags & VIS_SERVER))
757 continue;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000758
759 router = orig_node_get_router(orig_node);
760 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000761 continue;
762
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000763 /* don't send it if we already received the packet from
764 * this node. */
765 if (recv_list_is_in(bat_priv, &info->recv_list,
766 orig_node->orig)) {
767 neigh_node_free_ref(router);
768 continue;
769 }
770
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000771 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000772 hard_iface = router->if_incoming;
773 memcpy(dstaddr, router->addr, ETH_ALEN);
774
775 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000776
777 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
778 if (skb)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000779 send_skb_packet(skb, hard_iface, dstaddr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000780
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000781 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000782 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000783 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000784}
785
786static void unicast_vis_packet(struct bat_priv *bat_priv,
787 struct vis_info *info)
788{
789 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000790 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791 struct sk_buff *skb;
792 struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000793
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000795
Marek Lindner7aadf882011-02-18 12:28:09 +0000796 orig_node = orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000797 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000798 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000799
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000800 router = orig_node_get_router(orig_node);
801 if (!router)
802 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000803
804 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
805 if (skb)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000806 send_skb_packet(skb, router->if_incoming, router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000807
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000808out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000809 if (router)
810 neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000811 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000812 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000813}
814
815/* only send one vis packet. called from send_vis_packets() */
816static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
817{
818 struct vis_packet *packet;
819
820 packet = (struct vis_packet *)info->skb_packet->data;
821 if (packet->ttl < 2) {
822 pr_debug("Error - can't send vis packet: ttl exceeded\n");
823 return;
824 }
825
826 memcpy(packet->sender_orig, bat_priv->primary_if->net_dev->dev_addr,
827 ETH_ALEN);
828 packet->ttl--;
829
830 if (is_broadcast_ether_addr(packet->target_orig))
831 broadcast_vis_packet(bat_priv, info);
832 else
833 unicast_vis_packet(bat_priv, info);
834 packet->ttl++; /* restore TTL */
835}
836
837/* called from timer; send (and maybe generate) vis packet. */
838static void send_vis_packets(struct work_struct *work)
839{
840 struct delayed_work *delayed_work =
841 container_of(work, struct delayed_work, work);
842 struct bat_priv *bat_priv =
843 container_of(delayed_work, struct bat_priv, vis_work);
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100844 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000845
846 spin_lock_bh(&bat_priv->vis_hash_lock);
847 purge_vis_packets(bat_priv);
848
849 if (generate_vis_packet(bat_priv) == 0) {
850 /* schedule if generation was successful */
851 send_list_add(bat_priv, bat_priv->my_vis_info);
852 }
853
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100854 while (!list_empty(&bat_priv->vis_send_list)) {
855 info = list_first_entry(&bat_priv->vis_send_list,
856 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000857
858 kref_get(&info->refcount);
859 spin_unlock_bh(&bat_priv->vis_hash_lock);
860
861 if (bat_priv->primary_if)
862 send_vis_packet(bat_priv, info);
863
864 spin_lock_bh(&bat_priv->vis_hash_lock);
865 send_list_del(info);
866 kref_put(&info->refcount, free_info);
867 }
868 spin_unlock_bh(&bat_priv->vis_hash_lock);
869 start_vis_timer(bat_priv);
870}
871
872/* init the vis server. this may only be called when if_list is already
873 * initialized (e.g. bat0 is initialized, interfaces have been added) */
874int vis_init(struct bat_priv *bat_priv)
875{
876 struct vis_packet *packet;
877 int hash_added;
878
879 if (bat_priv->vis_hash)
880 return 1;
881
882 spin_lock_bh(&bat_priv->vis_hash_lock);
883
884 bat_priv->vis_hash = hash_new(256);
885 if (!bat_priv->vis_hash) {
886 pr_err("Can't initialize vis_hash\n");
887 goto err;
888 }
889
890 bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
891 if (!bat_priv->my_vis_info) {
892 pr_err("Can't initialize vis packet\n");
893 goto err;
894 }
895
896 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(
897 sizeof(struct vis_packet) +
898 MAX_VIS_PACKET_SIZE +
899 sizeof(struct ethhdr));
900 if (!bat_priv->my_vis_info->skb_packet)
901 goto free_info;
902
903 skb_reserve(bat_priv->my_vis_info->skb_packet, sizeof(struct ethhdr));
904 packet = (struct vis_packet *)skb_put(
905 bat_priv->my_vis_info->skb_packet,
906 sizeof(struct vis_packet));
907
908 /* prefill the vis info */
909 bat_priv->my_vis_info->first_seen = jiffies -
910 msecs_to_jiffies(VIS_INTERVAL);
911 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
912 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
913 kref_init(&bat_priv->my_vis_info->refcount);
914 bat_priv->my_vis_info->bat_priv = bat_priv;
915 packet->version = COMPAT_VERSION;
916 packet->packet_type = BAT_VIS;
917 packet->ttl = TTL;
918 packet->seqno = 0;
919 packet->entries = 0;
920
921 INIT_LIST_HEAD(&bat_priv->vis_send_list);
922
923 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
Marek Lindner7aadf882011-02-18 12:28:09 +0000924 bat_priv->my_vis_info,
925 &bat_priv->my_vis_info->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000926 if (hash_added < 0) {
927 pr_err("Can't add own vis packet into hash\n");
928 /* not in hash, need to remove it manually. */
929 kref_put(&bat_priv->my_vis_info->refcount, free_info);
930 goto err;
931 }
932
933 spin_unlock_bh(&bat_priv->vis_hash_lock);
934 start_vis_timer(bat_priv);
935 return 1;
936
937free_info:
938 kfree(bat_priv->my_vis_info);
939 bat_priv->my_vis_info = NULL;
940err:
941 spin_unlock_bh(&bat_priv->vis_hash_lock);
942 vis_quit(bat_priv);
943 return 0;
944}
945
946/* Decrease the reference count on a hash item info */
Marek Lindner7aadf882011-02-18 12:28:09 +0000947static void free_info_ref(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000948{
Marek Lindner7aadf882011-02-18 12:28:09 +0000949 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950
Marek Lindner7aadf882011-02-18 12:28:09 +0000951 info = container_of(node, struct vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000952 send_list_del(info);
953 kref_put(&info->refcount, free_info);
954}
955
956/* shutdown vis-server */
957void vis_quit(struct bat_priv *bat_priv)
958{
959 if (!bat_priv->vis_hash)
960 return;
961
962 cancel_delayed_work_sync(&bat_priv->vis_work);
963
964 spin_lock_bh(&bat_priv->vis_hash_lock);
965 /* properly remove, kill timers ... */
966 hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
967 bat_priv->vis_hash = NULL;
968 bat_priv->my_vis_info = NULL;
969 spin_unlock_bh(&bat_priv->vis_hash_lock);
970}
971
972/* schedule packets for (re)transmission */
973static void start_vis_timer(struct bat_priv *bat_priv)
974{
975 INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
976 queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work,
977 msecs_to_jiffies(VIS_INTERVAL));
978}