blob: c8f571d3b5d4c3c13a0ba12d26e921540a8af5a0 [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 Lindner32ae9b22011-04-20 15:40:58 +0200207 struct hard_iface *primary_if;
Marek Lindner7aadf882011-02-18 12:28:09 +0000208 struct hlist_node *node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000209 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210 struct vis_info *info;
211 struct vis_packet *packet;
212 struct vis_info_entry *entries;
213 struct net_device *net_dev = (struct net_device *)seq->private;
214 struct bat_priv *bat_priv = netdev_priv(net_dev);
215 struct hashtable_t *hash = bat_priv->vis_hash;
216 HLIST_HEAD(vis_if_list);
217 struct if_list_entry *entry;
218 struct hlist_node *pos, *n;
Marek Lindner32ae9b22011-04-20 15:40:58 +0200219 int i, j, ret = 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220 int vis_server = atomic_read(&bat_priv->vis_mode);
221 size_t buff_pos, buf_size;
222 char *buff;
223 int compare;
224
Marek Lindner32ae9b22011-04-20 15:40:58 +0200225 primary_if = primary_if_get_selected(bat_priv);
226 if (!primary_if)
227 goto out;
228
229 if (vis_server == VIS_TYPE_CLIENT_UPDATE)
230 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000231
232 buf_size = 1;
233 /* Estimate length */
234 spin_lock_bh(&bat_priv->vis_hash_lock);
235 for (i = 0; i < hash->size; i++) {
236 head = &hash->table[i];
237
Marek Lindner7aadf882011-02-18 12:28:09 +0000238 rcu_read_lock();
239 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240 packet = (struct vis_packet *)info->skb_packet->data;
241 entries = (struct vis_info_entry *)
242 ((char *)packet + sizeof(struct vis_packet));
243
244 for (j = 0; j < packet->entries; j++) {
245 if (entries[j].quality == 0)
246 continue;
247 compare =
Marek Lindner39901e72011-02-18 12:28:08 +0000248 compare_eth(entries[j].src, packet->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000249 vis_data_insert_interface(entries[j].src,
250 &vis_if_list,
251 compare);
252 }
253
254 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
255 buf_size += 18 + 26 * packet->entries;
256
257 /* add primary/secondary records */
Marek Lindner39901e72011-02-18 12:28:08 +0000258 if (compare_eth(entry->addr, packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259 buf_size +=
260 vis_data_count_prim_sec(&vis_if_list);
261
262 buf_size += 1;
263 }
264
265 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
266 list) {
267 hlist_del(&entry->list);
268 kfree(entry);
269 }
270 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000271 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000272 }
273
274 buff = kmalloc(buf_size, GFP_ATOMIC);
275 if (!buff) {
276 spin_unlock_bh(&bat_priv->vis_hash_lock);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200277 ret = -ENOMEM;
278 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279 }
280 buff[0] = '\0';
281 buff_pos = 0;
282
283 for (i = 0; i < hash->size; i++) {
284 head = &hash->table[i];
285
Marek Lindner7aadf882011-02-18 12:28:09 +0000286 rcu_read_lock();
287 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288 packet = (struct vis_packet *)info->skb_packet->data;
289 entries = (struct vis_info_entry *)
290 ((char *)packet + sizeof(struct vis_packet));
291
292 for (j = 0; j < packet->entries; j++) {
293 if (entries[j].quality == 0)
294 continue;
295 compare =
Marek Lindner39901e72011-02-18 12:28:08 +0000296 compare_eth(entries[j].src, packet->vis_orig);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 vis_data_insert_interface(entries[j].src,
298 &vis_if_list,
299 compare);
300 }
301
302 hlist_for_each_entry(entry, pos, &vis_if_list, list) {
303 buff_pos += sprintf(buff + buff_pos, "%pM,",
304 entry->addr);
305
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000306 for (j = 0; j < packet->entries; j++)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307 buff_pos += vis_data_read_entry(
308 buff + buff_pos,
Linus Lüssingdd58ddc2011-01-25 21:56:16 +0000309 &entries[j],
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000310 entry->addr,
311 entry->primary);
312
313 /* add primary/secondary records */
Marek Lindner39901e72011-02-18 12:28:08 +0000314 if (compare_eth(entry->addr, packet->vis_orig))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000315 buff_pos +=
316 vis_data_read_prim_sec(buff + buff_pos,
317 &vis_if_list);
318
319 buff_pos += sprintf(buff + buff_pos, "\n");
320 }
321
322 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list,
323 list) {
324 hlist_del(&entry->list);
325 kfree(entry);
326 }
327 }
Marek Lindner7aadf882011-02-18 12:28:09 +0000328 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329 }
330
331 spin_unlock_bh(&bat_priv->vis_hash_lock);
332
333 seq_printf(seq, "%s", buff);
334 kfree(buff);
335
Marek Lindner32ae9b22011-04-20 15:40:58 +0200336out:
337 if (primary_if)
338 hardif_free_ref(primary_if);
339 return ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340}
341
342/* add the info packet to the send list, if it was not
343 * already linked in. */
344static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info)
345{
346 if (list_empty(&info->send_list)) {
347 kref_get(&info->refcount);
348 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
349 }
350}
351
352/* delete the info packet from the send list, if it was
353 * linked in. */
354static void send_list_del(struct vis_info *info)
355{
356 if (!list_empty(&info->send_list)) {
357 list_del_init(&info->send_list);
358 kref_put(&info->refcount, free_info);
359 }
360}
361
362/* tries to add one entry to the receive list. */
363static void recv_list_add(struct bat_priv *bat_priv,
364 struct list_head *recv_list, char *mac)
365{
366 struct recvlist_node *entry;
367
368 entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC);
369 if (!entry)
370 return;
371
372 memcpy(entry->mac, mac, ETH_ALEN);
373 spin_lock_bh(&bat_priv->vis_list_lock);
374 list_add_tail(&entry->list, recv_list);
375 spin_unlock_bh(&bat_priv->vis_list_lock);
376}
377
378/* returns 1 if this mac is in the recv_list */
379static int recv_list_is_in(struct bat_priv *bat_priv,
380 struct list_head *recv_list, char *mac)
381{
382 struct recvlist_node *entry;
383
384 spin_lock_bh(&bat_priv->vis_list_lock);
385 list_for_each_entry(entry, recv_list, list) {
Marek Lindner39901e72011-02-18 12:28:08 +0000386 if (compare_eth(entry->mac, mac)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000387 spin_unlock_bh(&bat_priv->vis_list_lock);
388 return 1;
389 }
390 }
391 spin_unlock_bh(&bat_priv->vis_list_lock);
392 return 0;
393}
394
395/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
396 * broken.. ). vis hash must be locked outside. is_new is set when the packet
397 * is newer than old entries in the hash. */
398static struct vis_info *add_packet(struct bat_priv *bat_priv,
399 struct vis_packet *vis_packet,
400 int vis_info_len, int *is_new,
401 int make_broadcast)
402{
403 struct vis_info *info, *old_info;
404 struct vis_packet *search_packet, *old_packet;
405 struct vis_info search_elem;
406 struct vis_packet *packet;
407 int hash_added;
408
409 *is_new = 0;
410 /* sanity check */
411 if (!bat_priv->vis_hash)
412 return NULL;
413
414 /* see if the packet is already in vis_hash */
415 search_elem.skb_packet = dev_alloc_skb(sizeof(struct vis_packet));
416 if (!search_elem.skb_packet)
417 return NULL;
418 search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet,
419 sizeof(struct vis_packet));
420
421 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
Marek Lindner7aadf882011-02-18 12:28:09 +0000422 old_info = vis_hash_find(bat_priv, &search_elem);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000423 kfree_skb(search_elem.skb_packet);
424
425 if (old_info) {
426 old_packet = (struct vis_packet *)old_info->skb_packet->data;
427 if (!seq_after(ntohl(vis_packet->seqno),
428 ntohl(old_packet->seqno))) {
429 if (old_packet->seqno == vis_packet->seqno) {
430 recv_list_add(bat_priv, &old_info->recv_list,
431 vis_packet->sender_orig);
432 return old_info;
433 } else {
434 /* newer packet is already in hash. */
435 return NULL;
436 }
437 }
438 /* remove old entry */
439 hash_remove(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
440 old_info);
441 send_list_del(old_info);
442 kref_put(&old_info->refcount, free_info);
443 }
444
445 info = kmalloc(sizeof(struct vis_info), GFP_ATOMIC);
446 if (!info)
447 return NULL;
448
449 info->skb_packet = dev_alloc_skb(sizeof(struct vis_packet) +
450 vis_info_len + sizeof(struct ethhdr));
451 if (!info->skb_packet) {
452 kfree(info);
453 return NULL;
454 }
455 skb_reserve(info->skb_packet, sizeof(struct ethhdr));
456 packet = (struct vis_packet *)skb_put(info->skb_packet,
457 sizeof(struct vis_packet) +
458 vis_info_len);
459
460 kref_init(&info->refcount);
461 INIT_LIST_HEAD(&info->send_list);
462 INIT_LIST_HEAD(&info->recv_list);
463 info->first_seen = jiffies;
464 info->bat_priv = bat_priv;
465 memcpy(packet, vis_packet, sizeof(struct vis_packet) + vis_info_len);
466
467 /* initialize and add new packet. */
468 *is_new = 1;
469
470 /* Make it a broadcast packet, if required */
471 if (make_broadcast)
472 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
473
474 /* repair if entries is longer than packet. */
475 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
476 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
477
478 recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
479
480 /* try to add it */
481 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
Marek Lindner7aadf882011-02-18 12:28:09 +0000482 info, &info->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000483 if (hash_added < 0) {
484 /* did not work (for some reason) */
Sven Eckelmann2674c152011-01-28 18:34:05 +0100485 kref_put(&info->refcount, free_info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000486 info = NULL;
487 }
488
489 return info;
490}
491
492/* handle the server sync packet, forward if needed. */
493void receive_server_sync_packet(struct bat_priv *bat_priv,
494 struct vis_packet *vis_packet,
495 int vis_info_len)
496{
497 struct vis_info *info;
498 int is_new, make_broadcast;
499 int vis_server = atomic_read(&bat_priv->vis_mode);
500
501 make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC);
502
503 spin_lock_bh(&bat_priv->vis_hash_lock);
504 info = add_packet(bat_priv, vis_packet, vis_info_len,
505 &is_new, make_broadcast);
506 if (!info)
507 goto end;
508
509 /* only if we are server ourselves and packet is newer than the one in
510 * hash.*/
511 if (vis_server == VIS_TYPE_SERVER_SYNC && is_new)
512 send_list_add(bat_priv, info);
513end:
514 spin_unlock_bh(&bat_priv->vis_hash_lock);
515}
516
517/* handle an incoming client update packet and schedule forward if needed. */
518void receive_client_update_packet(struct bat_priv *bat_priv,
519 struct vis_packet *vis_packet,
520 int vis_info_len)
521{
522 struct vis_info *info;
523 struct vis_packet *packet;
524 int is_new;
525 int vis_server = atomic_read(&bat_priv->vis_mode);
526 int are_target = 0;
527
528 /* clients shall not broadcast. */
529 if (is_broadcast_ether_addr(vis_packet->target_orig))
530 return;
531
532 /* Are we the target for this VIS packet? */
533 if (vis_server == VIS_TYPE_SERVER_SYNC &&
534 is_my_mac(vis_packet->target_orig))
535 are_target = 1;
536
537 spin_lock_bh(&bat_priv->vis_hash_lock);
538 info = add_packet(bat_priv, vis_packet, vis_info_len,
539 &is_new, are_target);
540
541 if (!info)
542 goto end;
543 /* note that outdated packets will be dropped at this point. */
544
545 packet = (struct vis_packet *)info->skb_packet->data;
546
547 /* send only if we're the target server or ... */
548 if (are_target && is_new) {
549 packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */
550 send_list_add(bat_priv, info);
551
552 /* ... we're not the recipient (and thus need to forward). */
553 } else if (!is_my_mac(packet->target_orig)) {
554 send_list_add(bat_priv, info);
555 }
556
557end:
558 spin_unlock_bh(&bat_priv->vis_hash_lock);
559}
560
561/* Walk the originators and find the VIS server with the best tq. Set the packet
562 * address to its address and return the best_tq.
563 *
564 * Must be called with the originator hash locked */
565static 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;
574 int best_tq = -1, i;
575
576 packet = (struct vis_packet *)info->skb_packet->data;
577
578 for (i = 0; i < hash->size; i++) {
579 head = &hash->table[i];
580
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000581 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000582 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000583 router = orig_node_get_router(orig_node);
584 if (!router)
585 continue;
586
587 if ((orig_node->flags & VIS_SERVER) &&
588 (router->tq_avg > best_tq)) {
589 best_tq = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000590 memcpy(packet->target_orig, orig_node->orig,
591 ETH_ALEN);
592 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000593 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000594 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000595 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000596 }
597
598 return best_tq;
599}
600
601/* Return true if the vis packet is full. */
602static bool vis_packet_full(struct vis_info *info)
603{
604 struct vis_packet *packet;
605 packet = (struct vis_packet *)info->skb_packet->data;
606
607 if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry)
608 < packet->entries + 1)
609 return true;
610 return false;
611}
612
613/* generates a packet of own vis data,
614 * returns 0 on success, -1 if no packet could be generated */
615static 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 Eckelmannc6c8fea2010-12-13 11:19:28 +0000622 struct vis_info *info = (struct vis_info *)bat_priv->my_vis_info;
623 struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data;
624 struct vis_info_entry *entry;
625 struct hna_local_entry *hna_local_entry;
626 int best_tq = -1, i;
627
628 info->first_seen = jiffies;
629 packet->vis_type = atomic_read(&bat_priv->vis_mode);
630
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000631 memcpy(packet->target_orig, broadcast_addr, ETH_ALEN);
632 packet->ttl = TTL;
633 packet->seqno = htonl(ntohl(packet->seqno) + 1);
634 packet->entries = 0;
635 skb_trim(info->skb_packet, sizeof(struct vis_packet));
636
637 if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) {
638 best_tq = find_best_vis_server(bat_priv, info);
639
Marek Lindnerd0072602011-01-19 20:01:44 +0000640 if (best_tq < 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000641 return -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000642 }
643
644 for (i = 0; i < hash->size; i++) {
645 head = &hash->table[i];
646
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000647 rcu_read_lock();
Marek Lindner7aadf882011-02-18 12:28:09 +0000648 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000649 router = orig_node_get_router(orig_node);
650 if (!router)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651 continue;
652
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000653 if (!compare_eth(router->addr, orig_node->orig))
654 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000655
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000656 if (router->if_incoming->if_status != IF_ACTIVE)
657 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000658
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000659 if (router->tq_avg < 1)
660 goto next;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000661
662 /* fill one entry into buffer. */
663 entry = (struct vis_info_entry *)
664 skb_put(info->skb_packet, sizeof(*entry));
665 memcpy(entry->src,
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000666 router->if_incoming->net_dev->dev_addr,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000667 ETH_ALEN);
668 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000669 entry->quality = router->tq_avg;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000670 packet->entries++;
671
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000672next:
673 neigh_node_free_ref(router);
674
Marek Lindnerd0072602011-01-19 20:01:44 +0000675 if (vis_packet_full(info))
676 goto unlock;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000677 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000678 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000679 }
680
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000681 hash = bat_priv->hna_local_hash;
682
683 spin_lock_bh(&bat_priv->hna_lhash_lock);
684 for (i = 0; i < hash->size; i++) {
685 head = &hash->table[i];
686
Marek Lindner7aadf882011-02-18 12:28:09 +0000687 hlist_for_each_entry(hna_local_entry, node, head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000688 entry = (struct vis_info_entry *)
689 skb_put(info->skb_packet,
690 sizeof(*entry));
691 memset(entry->src, 0, ETH_ALEN);
692 memcpy(entry->dest, hna_local_entry->addr, ETH_ALEN);
693 entry->quality = 0; /* 0 means HNA */
694 packet->entries++;
695
696 if (vis_packet_full(info)) {
697 spin_unlock_bh(&bat_priv->hna_lhash_lock);
698 return 0;
699 }
700 }
701 }
702
703 spin_unlock_bh(&bat_priv->hna_lhash_lock);
704 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
712 * held */
713static void purge_vis_packets(struct bat_priv *bat_priv)
714{
715 int i;
716 struct hashtable_t *hash = bat_priv->vis_hash;
Marek Lindner7aadf882011-02-18 12:28:09 +0000717 struct hlist_node *node, *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000718 struct hlist_head *head;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000719 struct vis_info *info;
720
721 for (i = 0; i < hash->size; i++) {
722 head = &hash->table[i];
723
Marek Lindner7aadf882011-02-18 12:28:09 +0000724 hlist_for_each_entry_safe(info, node, node_tmp,
725 head, hash_entry) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000726 /* never purge own data. */
727 if (info == bat_priv->my_vis_info)
728 continue;
729
730 if (time_after(jiffies,
731 info->first_seen + VIS_TIMEOUT * HZ)) {
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];
752 int i;
753
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
767 router = orig_node_get_router(orig_node);
768 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
772 * this node. */
773 if (recv_list_is_in(bat_priv, &info->recv_list,
774 orig_node->orig)) {
775 neigh_node_free_ref(router);
776 continue;
777 }
778
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000779 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000780 hard_iface = router->if_incoming;
781 memcpy(dstaddr, router->addr, ETH_ALEN);
782
783 neigh_node_free_ref(router);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000784
785 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
786 if (skb)
Marek Lindnere6c10f42011-02-18 12:33:20 +0000787 send_skb_packet(skb, hard_iface, dstaddr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000788
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000789 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +0000790 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000791 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000792}
793
794static void unicast_vis_packet(struct bat_priv *bat_priv,
795 struct vis_info *info)
796{
797 struct orig_node *orig_node;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000798 struct neigh_node *router = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000799 struct sk_buff *skb;
800 struct vis_packet *packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000801
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000802 packet = (struct vis_packet *)info->skb_packet->data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000803
Marek Lindner7aadf882011-02-18 12:28:09 +0000804 orig_node = orig_hash_find(bat_priv, packet->target_orig);
Marek Lindner44524fc2011-02-10 14:33:53 +0000805 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000806 goto out;
Marek Lindner44524fc2011-02-10 14:33:53 +0000807
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000808 router = orig_node_get_router(orig_node);
809 if (!router)
810 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000811
812 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
813 if (skb)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000814 send_skb_packet(skb, router->if_incoming, router->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000815
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000816out:
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000817 if (router)
818 neigh_node_free_ref(router);
Marek Lindner44524fc2011-02-10 14:33:53 +0000819 if (orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000820 orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000821}
822
823/* only send one vis packet. called from send_vis_packets() */
824static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info)
825{
Marek Lindner32ae9b22011-04-20 15:40:58 +0200826 struct hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000827 struct vis_packet *packet;
828
Marek Lindner32ae9b22011-04-20 15:40:58 +0200829 primary_if = primary_if_get_selected(bat_priv);
830 if (!primary_if)
831 goto out;
832
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000833 packet = (struct vis_packet *)info->skb_packet->data;
834 if (packet->ttl < 2) {
835 pr_debug("Error - can't send vis packet: ttl exceeded\n");
Marek Lindner32ae9b22011-04-20 15:40:58 +0200836 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000837 }
838
Marek Lindner32ae9b22011-04-20 15:40:58 +0200839 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840 packet->ttl--;
841
842 if (is_broadcast_ether_addr(packet->target_orig))
843 broadcast_vis_packet(bat_priv, info);
844 else
845 unicast_vis_packet(bat_priv, info);
846 packet->ttl++; /* restore TTL */
Marek Lindner32ae9b22011-04-20 15:40:58 +0200847
848out:
849 if (primary_if)
850 hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000851}
852
853/* called from timer; send (and maybe generate) vis packet. */
854static void send_vis_packets(struct work_struct *work)
855{
856 struct delayed_work *delayed_work =
857 container_of(work, struct delayed_work, work);
858 struct bat_priv *bat_priv =
859 container_of(delayed_work, struct bat_priv, vis_work);
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100860 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861
862 spin_lock_bh(&bat_priv->vis_hash_lock);
863 purge_vis_packets(bat_priv);
864
865 if (generate_vis_packet(bat_priv) == 0) {
866 /* schedule if generation was successful */
867 send_list_add(bat_priv, bat_priv->my_vis_info);
868 }
869
Sven Eckelmann1181e1d2011-01-28 18:34:07 +0100870 while (!list_empty(&bat_priv->vis_send_list)) {
871 info = list_first_entry(&bat_priv->vis_send_list,
872 typeof(*info), send_list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000873
874 kref_get(&info->refcount);
875 spin_unlock_bh(&bat_priv->vis_hash_lock);
876
Marek Lindner32ae9b22011-04-20 15:40:58 +0200877 send_vis_packet(bat_priv, info);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000878
879 spin_lock_bh(&bat_priv->vis_hash_lock);
880 send_list_del(info);
881 kref_put(&info->refcount, free_info);
882 }
883 spin_unlock_bh(&bat_priv->vis_hash_lock);
884 start_vis_timer(bat_priv);
885}
886
887/* init the vis server. this may only be called when if_list is already
888 * initialized (e.g. bat0 is initialized, interfaces have been added) */
889int vis_init(struct bat_priv *bat_priv)
890{
891 struct vis_packet *packet;
892 int hash_added;
893
894 if (bat_priv->vis_hash)
895 return 1;
896
897 spin_lock_bh(&bat_priv->vis_hash_lock);
898
899 bat_priv->vis_hash = hash_new(256);
900 if (!bat_priv->vis_hash) {
901 pr_err("Can't initialize vis_hash\n");
902 goto err;
903 }
904
905 bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
906 if (!bat_priv->my_vis_info) {
907 pr_err("Can't initialize vis packet\n");
908 goto err;
909 }
910
911 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(
912 sizeof(struct vis_packet) +
913 MAX_VIS_PACKET_SIZE +
914 sizeof(struct ethhdr));
915 if (!bat_priv->my_vis_info->skb_packet)
916 goto free_info;
917
918 skb_reserve(bat_priv->my_vis_info->skb_packet, sizeof(struct ethhdr));
919 packet = (struct vis_packet *)skb_put(
920 bat_priv->my_vis_info->skb_packet,
921 sizeof(struct vis_packet));
922
923 /* prefill the vis info */
924 bat_priv->my_vis_info->first_seen = jiffies -
925 msecs_to_jiffies(VIS_INTERVAL);
926 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
927 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
928 kref_init(&bat_priv->my_vis_info->refcount);
929 bat_priv->my_vis_info->bat_priv = bat_priv;
930 packet->version = COMPAT_VERSION;
931 packet->packet_type = BAT_VIS;
932 packet->ttl = TTL;
933 packet->seqno = 0;
934 packet->entries = 0;
935
936 INIT_LIST_HEAD(&bat_priv->vis_send_list);
937
938 hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose,
Marek Lindner7aadf882011-02-18 12:28:09 +0000939 bat_priv->my_vis_info,
940 &bat_priv->my_vis_info->hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000941 if (hash_added < 0) {
942 pr_err("Can't add own vis packet into hash\n");
943 /* not in hash, need to remove it manually. */
944 kref_put(&bat_priv->my_vis_info->refcount, free_info);
945 goto err;
946 }
947
948 spin_unlock_bh(&bat_priv->vis_hash_lock);
949 start_vis_timer(bat_priv);
950 return 1;
951
952free_info:
953 kfree(bat_priv->my_vis_info);
954 bat_priv->my_vis_info = NULL;
955err:
956 spin_unlock_bh(&bat_priv->vis_hash_lock);
957 vis_quit(bat_priv);
958 return 0;
959}
960
961/* Decrease the reference count on a hash item info */
Marek Lindner7aadf882011-02-18 12:28:09 +0000962static void free_info_ref(struct hlist_node *node, void *arg)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000963{
Marek Lindner7aadf882011-02-18 12:28:09 +0000964 struct vis_info *info;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000965
Marek Lindner7aadf882011-02-18 12:28:09 +0000966 info = container_of(node, struct vis_info, hash_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000967 send_list_del(info);
968 kref_put(&info->refcount, free_info);
969}
970
971/* shutdown vis-server */
972void vis_quit(struct bat_priv *bat_priv)
973{
974 if (!bat_priv->vis_hash)
975 return;
976
977 cancel_delayed_work_sync(&bat_priv->vis_work);
978
979 spin_lock_bh(&bat_priv->vis_hash_lock);
980 /* properly remove, kill timers ... */
981 hash_delete(bat_priv->vis_hash, free_info_ref, NULL);
982 bat_priv->vis_hash = NULL;
983 bat_priv->my_vis_info = NULL;
984 spin_unlock_bh(&bat_priv->vis_hash_lock);
985}
986
987/* schedule packets for (re)transmission */
988static void start_vis_timer(struct bat_priv *bat_priv)
989{
990 INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets);
991 queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work,
992 msecs_to_jiffies(VIS_INTERVAL));
993}