]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/batman-adv/vis.c
batman-adv: Prefix packet structs with batadv_
[mirror_ubuntu-artful-kernel.git] / net / batman-adv / vis.c
CommitLineData
9cfc7bd6 1/* Copyright (C) 2008-2012 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
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
c6c8fea2
SE
18 */
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
347c80f0 29#define BATADV_MAX_VIS_PACKET_SIZE 1000
c6c8fea2 30
eaad8ad9 31static void batadv_start_vis_timer(struct bat_priv *bat_priv);
c6c8fea2
SE
32
33/* free the info */
eaad8ad9 34static void batadv_free_info(struct kref *ref)
c6c8fea2
SE
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);
dda9fc6b 49 kfree(info);
c6c8fea2
SE
50}
51
52/* Compare two vis packets, used by the hashing algorithm */
eaad8ad9 53static int batadv_vis_info_cmp(const struct hlist_node *node, const void *data2)
c6c8fea2 54{
747e4221 55 const struct vis_info *d1, *d2;
96412690 56 const struct batadv_vis_packet *p1, *p2;
7aadf889
ML
57
58 d1 = container_of(node, struct vis_info, hash_entry);
c6c8fea2 59 d2 = data2;
96412690
SE
60 p1 = (struct batadv_vis_packet *)d1->skb_packet->data;
61 p2 = (struct batadv_vis_packet *)d2->skb_packet->data;
1eda58bf 62 return batadv_compare_eth(p1->vis_orig, p2->vis_orig);
c6c8fea2
SE
63}
64
9cfc7bd6
SE
65/* 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 */
eaad8ad9 68static uint32_t batadv_vis_info_choose(const void *data, uint32_t size)
c6c8fea2 69{
747e4221 70 const struct vis_info *vis_info = data;
96412690 71 const struct batadv_vis_packet *packet;
747e4221 72 const unsigned char *key;
c6c8fea2
SE
73 uint32_t hash = 0;
74 size_t i;
75
96412690 76 packet = (struct batadv_vis_packet *)vis_info->skb_packet->data;
c6c8fea2
SE
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
eaad8ad9
SE
91static struct vis_info *batadv_vis_hash_find(struct bat_priv *bat_priv,
92 const void *data)
7aadf889 93{
5bf74e9c 94 struct batadv_hashtable *hash = bat_priv->vis_hash;
7aadf889
ML
95 struct hlist_head *head;
96 struct hlist_node *node;
97 struct vis_info *vis_info, *vis_info_tmp = NULL;
c90681b8 98 uint32_t index;
7aadf889
ML
99
100 if (!hash)
101 return NULL;
102
eaad8ad9 103 index = batadv_vis_info_choose(data, hash->size);
7aadf889
ML
104 head = &hash->table[index];
105
106 rcu_read_lock();
107 hlist_for_each_entry_rcu(vis_info, node, head, hash_entry) {
eaad8ad9 108 if (!batadv_vis_info_cmp(node, data))
7aadf889
ML
109 continue;
110
111 vis_info_tmp = vis_info;
112 break;
113 }
114 rcu_read_unlock();
115
116 return vis_info_tmp;
117}
118
c6c8fea2 119/* insert interface to the list of interfaces of one originator, if it
9cfc7bd6
SE
120 * does not already exist in the list
121 */
eaad8ad9
SE
122static void batadv_vis_data_insert_interface(const uint8_t *interface,
123 struct hlist_head *if_list,
124 bool primary)
c6c8fea2
SE
125{
126 struct if_list_entry *entry;
127 struct hlist_node *pos;
128
129 hlist_for_each_entry(entry, pos, if_list, list) {
1eda58bf 130 if (batadv_compare_eth(entry->addr, interface))
c6c8fea2
SE
131 return;
132 }
133
015758d0 134 /* it's a new address, add it to the list */
c6c8fea2
SE
135 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
28afd3c0
SE
143static void batadv_vis_data_read_prim_sec(struct seq_file *seq,
144 const struct hlist_head *if_list)
c6c8fea2
SE
145{
146 struct if_list_entry *entry;
147 struct hlist_node *pos;
c6c8fea2
SE
148
149 hlist_for_each_entry(entry, pos, if_list, list) {
150 if (entry->primary)
28afd3c0 151 seq_printf(seq, "PRIMARY, ");
c6c8fea2 152 else
28afd3c0 153 seq_printf(seq, "SEC %pM, ", entry->addr);
c6c8fea2 154 }
28afd3c0
SE
155}
156
157/* read an entry */
158static ssize_t batadv_vis_data_read_entry(struct seq_file *seq,
159 const struct vis_info_entry *entry,
160 const uint8_t *src, bool primary)
161{
162 if (primary && entry->quality == 0)
163 return seq_printf(seq, "TT %pM, ", entry->dest);
164 else if (batadv_compare_eth(entry->src, src))
165 return seq_printf(seq, "TQ %pM %d, ", entry->dest,
166 entry->quality);
c6c8fea2 167
28afd3c0 168 return 0;
c6c8fea2
SE
169}
170
28afd3c0 171static void batadv_vis_data_insert_interfaces(struct hlist_head *list,
96412690 172 struct batadv_vis_packet *packet,
28afd3c0 173 struct vis_info_entry *entries)
c6c8fea2 174{
28afd3c0
SE
175 int i;
176
177 for (i = 0; i < packet->entries; i++) {
178 if (entries[i].quality == 0)
179 continue;
180
181 if (batadv_compare_eth(entries[i].src, packet->vis_orig))
182 continue;
183
184 batadv_vis_data_insert_interface(entries[i].src, list, false);
185 }
186}
187
188static void batadv_vis_data_read_entries(struct seq_file *seq,
189 struct hlist_head *list,
96412690 190 struct batadv_vis_packet *packet,
28afd3c0
SE
191 struct vis_info_entry *entries)
192{
193 int i;
c6c8fea2
SE
194 struct if_list_entry *entry;
195 struct hlist_node *pos;
c6c8fea2 196
28afd3c0
SE
197 hlist_for_each_entry(entry, pos, list, list) {
198 seq_printf(seq, "%pM,", entry->addr);
199
200 for (i = 0; i < packet->entries; i++)
201 batadv_vis_data_read_entry(seq, &entries[i],
202 entry->addr, entry->primary);
203
204 /* add primary/secondary records */
205 if (batadv_compare_eth(entry->addr, packet->vis_orig))
206 batadv_vis_data_read_prim_sec(seq, list);
c6c8fea2 207
28afd3c0
SE
208 seq_printf(seq, "\n");
209 }
c6c8fea2
SE
210}
211
28afd3c0
SE
212static void batadv_vis_seq_print_text_bucket(struct seq_file *seq,
213 const struct hlist_head *head)
c6c8fea2 214{
28afd3c0
SE
215 struct hlist_node *node;
216 struct vis_info *info;
96412690 217 struct batadv_vis_packet *packet;
28afd3c0
SE
218 uint8_t *entries_pos;
219 struct vis_info_entry *entries;
220 struct if_list_entry *entry;
221 struct hlist_node *pos, *n;
c6c8fea2 222
28afd3c0
SE
223 HLIST_HEAD(vis_if_list);
224
225 hlist_for_each_entry_rcu(info, node, head, hash_entry) {
96412690 226 packet = (struct batadv_vis_packet *)info->skb_packet->data;
28afd3c0
SE
227 entries_pos = (uint8_t *)packet + sizeof(*packet);
228 entries = (struct vis_info_entry *)entries_pos;
229
230 batadv_vis_data_insert_interface(packet->vis_orig, &vis_if_list,
231 true);
232 batadv_vis_data_insert_interfaces(&vis_if_list, packet,
233 entries);
234 batadv_vis_data_read_entries(seq, &vis_if_list, packet,
235 entries);
236
237 hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, list) {
238 hlist_del(&entry->list);
239 kfree(entry);
240 }
241 }
c6c8fea2
SE
242}
243
d0f714f4 244int batadv_vis_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2 245{
32ae9b22 246 struct hard_iface *primary_if;
c6c8fea2 247 struct hlist_head *head;
c6c8fea2
SE
248 struct net_device *net_dev = (struct net_device *)seq->private;
249 struct bat_priv *bat_priv = netdev_priv(net_dev);
5bf74e9c 250 struct batadv_hashtable *hash = bat_priv->vis_hash;
c90681b8 251 uint32_t i;
28afd3c0 252 int ret = 0;
c6c8fea2 253 int vis_server = atomic_read(&bat_priv->vis_mode);
c6c8fea2 254
e5d89254 255 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22
ML
256 if (!primary_if)
257 goto out;
258
acd34afa 259 if (vis_server == BATADV_VIS_TYPE_CLIENT_UPDATE)
32ae9b22 260 goto out;
c6c8fea2 261
c6c8fea2
SE
262 spin_lock_bh(&bat_priv->vis_hash_lock);
263 for (i = 0; i < hash->size; i++) {
264 head = &hash->table[i];
28afd3c0 265 batadv_vis_seq_print_text_bucket(seq, head);
c6c8fea2 266 }
c6c8fea2
SE
267 spin_unlock_bh(&bat_priv->vis_hash_lock);
268
32ae9b22
ML
269out:
270 if (primary_if)
e5d89254 271 batadv_hardif_free_ref(primary_if);
32ae9b22 272 return ret;
c6c8fea2
SE
273}
274
275/* add the info packet to the send list, if it was not
9cfc7bd6
SE
276 * already linked in.
277 */
eaad8ad9
SE
278static void batadv_send_list_add(struct bat_priv *bat_priv,
279 struct vis_info *info)
c6c8fea2
SE
280{
281 if (list_empty(&info->send_list)) {
282 kref_get(&info->refcount);
283 list_add_tail(&info->send_list, &bat_priv->vis_send_list);
284 }
285}
286
287/* delete the info packet from the send list, if it was
9cfc7bd6
SE
288 * linked in.
289 */
eaad8ad9 290static void batadv_send_list_del(struct vis_info *info)
c6c8fea2
SE
291{
292 if (!list_empty(&info->send_list)) {
293 list_del_init(&info->send_list);
eaad8ad9 294 kref_put(&info->refcount, batadv_free_info);
c6c8fea2
SE
295 }
296}
297
298/* tries to add one entry to the receive list. */
eaad8ad9
SE
299static void batadv_recv_list_add(struct bat_priv *bat_priv,
300 struct list_head *recv_list, const char *mac)
c6c8fea2
SE
301{
302 struct recvlist_node *entry;
303
704509b8 304 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
c6c8fea2
SE
305 if (!entry)
306 return;
307
308 memcpy(entry->mac, mac, ETH_ALEN);
309 spin_lock_bh(&bat_priv->vis_list_lock);
310 list_add_tail(&entry->list, recv_list);
311 spin_unlock_bh(&bat_priv->vis_list_lock);
312}
313
314/* returns 1 if this mac is in the recv_list */
eaad8ad9
SE
315static int batadv_recv_list_is_in(struct bat_priv *bat_priv,
316 const struct list_head *recv_list,
317 const char *mac)
c6c8fea2 318{
747e4221 319 const struct recvlist_node *entry;
c6c8fea2
SE
320
321 spin_lock_bh(&bat_priv->vis_list_lock);
322 list_for_each_entry(entry, recv_list, list) {
1eda58bf 323 if (batadv_compare_eth(entry->mac, mac)) {
c6c8fea2
SE
324 spin_unlock_bh(&bat_priv->vis_list_lock);
325 return 1;
326 }
327 }
328 spin_unlock_bh(&bat_priv->vis_list_lock);
329 return 0;
330}
331
332/* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old,
333 * broken.. ). vis hash must be locked outside. is_new is set when the packet
9cfc7bd6
SE
334 * is newer than old entries in the hash.
335 */
eaad8ad9 336static struct vis_info *batadv_add_packet(struct bat_priv *bat_priv,
96412690 337 struct batadv_vis_packet *vis_packet,
eaad8ad9
SE
338 int vis_info_len, int *is_new,
339 int make_broadcast)
c6c8fea2
SE
340{
341 struct vis_info *info, *old_info;
96412690 342 struct batadv_vis_packet *search_packet, *old_packet;
c6c8fea2 343 struct vis_info search_elem;
96412690
SE
344 struct batadv_vis_packet *packet;
345 struct sk_buff *tmp_skb;
c6c8fea2 346 int hash_added;
96412690 347 size_t len;
c6c8fea2
SE
348
349 *is_new = 0;
350 /* sanity check */
351 if (!bat_priv->vis_hash)
352 return NULL;
353
354 /* see if the packet is already in vis_hash */
704509b8 355 search_elem.skb_packet = dev_alloc_skb(sizeof(*search_packet));
c6c8fea2
SE
356 if (!search_elem.skb_packet)
357 return NULL;
96412690
SE
358 len = sizeof(*search_packet);
359 tmp_skb = search_elem.skb_packet;
360 search_packet = (struct batadv_vis_packet *)skb_put(tmp_skb, len);
c6c8fea2
SE
361
362 memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN);
eaad8ad9 363 old_info = batadv_vis_hash_find(bat_priv, &search_elem);
c6c8fea2
SE
364 kfree_skb(search_elem.skb_packet);
365
366 if (old_info) {
96412690
SE
367 tmp_skb = old_info->skb_packet;
368 old_packet = (struct batadv_vis_packet *)tmp_skb->data;
3e34819e
SE
369 if (!batadv_seq_after(ntohl(vis_packet->seqno),
370 ntohl(old_packet->seqno))) {
c6c8fea2 371 if (old_packet->seqno == vis_packet->seqno) {
eaad8ad9
SE
372 batadv_recv_list_add(bat_priv,
373 &old_info->recv_list,
374 vis_packet->sender_orig);
c6c8fea2
SE
375 return old_info;
376 } else {
377 /* newer packet is already in hash. */
378 return NULL;
379 }
380 }
381 /* remove old entry */
eaad8ad9
SE
382 batadv_hash_remove(bat_priv->vis_hash, batadv_vis_info_cmp,
383 batadv_vis_info_choose, old_info);
384 batadv_send_list_del(old_info);
385 kref_put(&old_info->refcount, batadv_free_info);
c6c8fea2
SE
386 }
387
704509b8 388 info = kmalloc(sizeof(*info), GFP_ATOMIC);
c6c8fea2
SE
389 if (!info)
390 return NULL;
391
96412690
SE
392 len = sizeof(*packet) + vis_info_len;
393 info->skb_packet = dev_alloc_skb(len + ETH_HLEN);
c6c8fea2
SE
394 if (!info->skb_packet) {
395 kfree(info);
396 return NULL;
397 }
0d125074 398 skb_reserve(info->skb_packet, ETH_HLEN);
96412690 399 packet = (struct batadv_vis_packet *)skb_put(info->skb_packet, len);
c6c8fea2
SE
400
401 kref_init(&info->refcount);
402 INIT_LIST_HEAD(&info->send_list);
403 INIT_LIST_HEAD(&info->recv_list);
404 info->first_seen = jiffies;
405 info->bat_priv = bat_priv;
96412690 406 memcpy(packet, vis_packet, len);
c6c8fea2
SE
407
408 /* initialize and add new packet. */
409 *is_new = 1;
410
411 /* Make it a broadcast packet, if required */
412 if (make_broadcast)
3193e8fd 413 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
c6c8fea2
SE
414
415 /* repair if entries is longer than packet. */
416 if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len)
417 packet->entries = vis_info_len / sizeof(struct vis_info_entry);
418
eaad8ad9 419 batadv_recv_list_add(bat_priv, &info->recv_list, packet->sender_orig);
c6c8fea2
SE
420
421 /* try to add it */
eaad8ad9
SE
422 hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
423 batadv_vis_info_choose, info,
424 &info->hash_entry);
1a1f37d9 425 if (hash_added != 0) {
c6c8fea2 426 /* did not work (for some reason) */
eaad8ad9 427 kref_put(&info->refcount, batadv_free_info);
c6c8fea2
SE
428 info = NULL;
429 }
430
431 return info;
432}
433
434/* handle the server sync packet, forward if needed. */
d0f714f4 435void batadv_receive_server_sync_packet(struct bat_priv *bat_priv,
96412690 436 struct batadv_vis_packet *vis_packet,
d0f714f4 437 int vis_info_len)
c6c8fea2
SE
438{
439 struct vis_info *info;
440 int is_new, make_broadcast;
441 int vis_server = atomic_read(&bat_priv->vis_mode);
442
acd34afa 443 make_broadcast = (vis_server == BATADV_VIS_TYPE_SERVER_SYNC);
c6c8fea2
SE
444
445 spin_lock_bh(&bat_priv->vis_hash_lock);
eaad8ad9
SE
446 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
447 &is_new, make_broadcast);
c6c8fea2
SE
448 if (!info)
449 goto end;
450
451 /* only if we are server ourselves and packet is newer than the one in
9cfc7bd6
SE
452 * hash.
453 */
acd34afa 454 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC && is_new)
eaad8ad9 455 batadv_send_list_add(bat_priv, info);
c6c8fea2
SE
456end:
457 spin_unlock_bh(&bat_priv->vis_hash_lock);
458}
459
460/* handle an incoming client update packet and schedule forward if needed. */
d0f714f4 461void batadv_receive_client_update_packet(struct bat_priv *bat_priv,
96412690 462 struct batadv_vis_packet *vis_packet,
d0f714f4 463 int vis_info_len)
c6c8fea2
SE
464{
465 struct vis_info *info;
96412690 466 struct batadv_vis_packet *packet;
c6c8fea2
SE
467 int is_new;
468 int vis_server = atomic_read(&bat_priv->vis_mode);
469 int are_target = 0;
470
471 /* clients shall not broadcast. */
472 if (is_broadcast_ether_addr(vis_packet->target_orig))
473 return;
474
475 /* Are we the target for this VIS packet? */
acd34afa 476 if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC &&
3193e8fd 477 batadv_is_my_mac(vis_packet->target_orig))
c6c8fea2
SE
478 are_target = 1;
479
480 spin_lock_bh(&bat_priv->vis_hash_lock);
eaad8ad9
SE
481 info = batadv_add_packet(bat_priv, vis_packet, vis_info_len,
482 &is_new, are_target);
c6c8fea2
SE
483
484 if (!info)
485 goto end;
486 /* note that outdated packets will be dropped at this point. */
487
96412690 488 packet = (struct batadv_vis_packet *)info->skb_packet->data;
c6c8fea2
SE
489
490 /* send only if we're the target server or ... */
491 if (are_target && is_new) {
acd34afa 492 packet->vis_type = BATADV_VIS_TYPE_SERVER_SYNC; /* upgrade! */
eaad8ad9 493 batadv_send_list_add(bat_priv, info);
c6c8fea2
SE
494
495 /* ... we're not the recipient (and thus need to forward). */
3193e8fd 496 } else if (!batadv_is_my_mac(packet->target_orig)) {
eaad8ad9 497 batadv_send_list_add(bat_priv, info);
c6c8fea2
SE
498 }
499
500end:
501 spin_unlock_bh(&bat_priv->vis_hash_lock);
502}
503
504/* Walk the originators and find the VIS server with the best tq. Set the packet
505 * address to its address and return the best_tq.
506 *
9cfc7bd6
SE
507 * Must be called with the originator hash locked
508 */
eaad8ad9
SE
509static int batadv_find_best_vis_server(struct bat_priv *bat_priv,
510 struct vis_info *info)
c6c8fea2 511{
5bf74e9c 512 struct batadv_hashtable *hash = bat_priv->orig_hash;
e1a5382f 513 struct neigh_node *router;
7aadf889 514 struct hlist_node *node;
c6c8fea2 515 struct hlist_head *head;
c6c8fea2 516 struct orig_node *orig_node;
96412690 517 struct batadv_vis_packet *packet;
c90681b8
AQ
518 int best_tq = -1;
519 uint32_t i;
c6c8fea2 520
96412690 521 packet = (struct batadv_vis_packet *)info->skb_packet->data;
c6c8fea2
SE
522
523 for (i = 0; i < hash->size; i++) {
524 head = &hash->table[i];
525
fb778ea1 526 rcu_read_lock();
7aadf889 527 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
7d211efc 528 router = batadv_orig_node_get_router(orig_node);
e1a5382f
LL
529 if (!router)
530 continue;
531
acd34afa 532 if ((orig_node->flags & BATADV_VIS_SERVER) &&
e1a5382f
LL
533 (router->tq_avg > best_tq)) {
534 best_tq = router->tq_avg;
c6c8fea2
SE
535 memcpy(packet->target_orig, orig_node->orig,
536 ETH_ALEN);
537 }
7d211efc 538 batadv_neigh_node_free_ref(router);
c6c8fea2 539 }
fb778ea1 540 rcu_read_unlock();
c6c8fea2
SE
541 }
542
543 return best_tq;
544}
545
546/* Return true if the vis packet is full. */
eaad8ad9 547static bool batadv_vis_packet_full(const struct vis_info *info)
c6c8fea2 548{
96412690 549 const struct batadv_vis_packet *packet;
347c80f0
SE
550 size_t num_items;
551
96412690 552 packet = (struct batadv_vis_packet *)info->skb_packet->data;
347c80f0 553 num_items = BATADV_MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry);
c6c8fea2 554
347c80f0 555 if (num_items < packet->entries + 1)
c6c8fea2
SE
556 return true;
557 return false;
558}
559
560/* generates a packet of own vis data,
9cfc7bd6
SE
561 * returns 0 on success, -1 if no packet could be generated
562 */
eaad8ad9 563static int batadv_generate_vis_packet(struct bat_priv *bat_priv)
c6c8fea2 564{
5bf74e9c 565 struct batadv_hashtable *hash = bat_priv->orig_hash;
7aadf889 566 struct hlist_node *node;
c6c8fea2 567 struct hlist_head *head;
c6c8fea2 568 struct orig_node *orig_node;
e1a5382f 569 struct neigh_node *router;
958ca598 570 struct vis_info *info = bat_priv->my_vis_info;
96412690 571 struct batadv_vis_packet *packet;
c6c8fea2 572 struct vis_info_entry *entry;
48100bac 573 struct tt_common_entry *tt_common_entry;
c90681b8
AQ
574 int best_tq = -1;
575 uint32_t i;
c6c8fea2
SE
576
577 info->first_seen = jiffies;
96412690 578 packet = (struct batadv_vis_packet *)info->skb_packet->data;
c6c8fea2
SE
579 packet->vis_type = atomic_read(&bat_priv->vis_mode);
580
3193e8fd 581 memcpy(packet->target_orig, batadv_broadcast_addr, ETH_ALEN);
42d0b044 582 packet->header.ttl = BATADV_TTL;
c6c8fea2
SE
583 packet->seqno = htonl(ntohl(packet->seqno) + 1);
584 packet->entries = 0;
704509b8 585 skb_trim(info->skb_packet, sizeof(*packet));
c6c8fea2 586
acd34afa 587 if (packet->vis_type == BATADV_VIS_TYPE_CLIENT_UPDATE) {
eaad8ad9 588 best_tq = batadv_find_best_vis_server(bat_priv, info);
c6c8fea2 589
d0072609 590 if (best_tq < 0)
5346c35e 591 return best_tq;
c6c8fea2
SE
592 }
593
594 for (i = 0; i < hash->size; i++) {
595 head = &hash->table[i];
596
fb778ea1 597 rcu_read_lock();
7aadf889 598 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
7d211efc 599 router = batadv_orig_node_get_router(orig_node);
e1a5382f 600 if (!router)
c6c8fea2
SE
601 continue;
602
1eda58bf 603 if (!batadv_compare_eth(router->addr, orig_node->orig))
e1a5382f 604 goto next;
c6c8fea2 605
e9a4f295 606 if (router->if_incoming->if_status != BATADV_IF_ACTIVE)
e1a5382f 607 goto next;
c6c8fea2 608
e1a5382f
LL
609 if (router->tq_avg < 1)
610 goto next;
c6c8fea2
SE
611
612 /* fill one entry into buffer. */
613 entry = (struct vis_info_entry *)
614 skb_put(info->skb_packet, sizeof(*entry));
615 memcpy(entry->src,
e1a5382f 616 router->if_incoming->net_dev->dev_addr,
c6c8fea2
SE
617 ETH_ALEN);
618 memcpy(entry->dest, orig_node->orig, ETH_ALEN);
e1a5382f 619 entry->quality = router->tq_avg;
c6c8fea2
SE
620 packet->entries++;
621
e1a5382f 622next:
7d211efc 623 batadv_neigh_node_free_ref(router);
e1a5382f 624
eaad8ad9 625 if (batadv_vis_packet_full(info))
d0072609 626 goto unlock;
c6c8fea2 627 }
fb778ea1 628 rcu_read_unlock();
c6c8fea2
SE
629 }
630
2dafb49d 631 hash = bat_priv->tt_local_hash;
c6c8fea2 632
c6c8fea2
SE
633 for (i = 0; i < hash->size; i++) {
634 head = &hash->table[i];
635
7683fdc1 636 rcu_read_lock();
48100bac 637 hlist_for_each_entry_rcu(tt_common_entry, node, head,
7683fdc1 638 hash_entry) {
c6c8fea2
SE
639 entry = (struct vis_info_entry *)
640 skb_put(info->skb_packet,
641 sizeof(*entry));
642 memset(entry->src, 0, ETH_ALEN);
48100bac 643 memcpy(entry->dest, tt_common_entry->addr, ETH_ALEN);
2dafb49d 644 entry->quality = 0; /* 0 means TT */
c6c8fea2
SE
645 packet->entries++;
646
eaad8ad9 647 if (batadv_vis_packet_full(info))
7683fdc1 648 goto unlock;
c6c8fea2 649 }
7683fdc1 650 rcu_read_unlock();
c6c8fea2
SE
651 }
652
c6c8fea2 653 return 0;
d0072609
ML
654
655unlock:
656 rcu_read_unlock();
657 return 0;
c6c8fea2
SE
658}
659
660/* free old vis packets. Must be called with this vis_hash_lock
9cfc7bd6
SE
661 * held
662 */
eaad8ad9 663static void batadv_purge_vis_packets(struct bat_priv *bat_priv)
c6c8fea2 664{
c90681b8 665 uint32_t i;
5bf74e9c 666 struct batadv_hashtable *hash = bat_priv->vis_hash;
7aadf889 667 struct hlist_node *node, *node_tmp;
c6c8fea2 668 struct hlist_head *head;
c6c8fea2
SE
669 struct vis_info *info;
670
671 for (i = 0; i < hash->size; i++) {
672 head = &hash->table[i];
673
7aadf889
ML
674 hlist_for_each_entry_safe(info, node, node_tmp,
675 head, hash_entry) {
c6c8fea2
SE
676 /* never purge own data. */
677 if (info == bat_priv->my_vis_info)
678 continue;
679
1eda58bf 680 if (batadv_has_timed_out(info->first_seen,
edbf7ff7 681 BATADV_VIS_TIMEOUT)) {
7aadf889 682 hlist_del(node);
eaad8ad9
SE
683 batadv_send_list_del(info);
684 kref_put(&info->refcount, batadv_free_info);
c6c8fea2
SE
685 }
686 }
687 }
688}
689
eaad8ad9
SE
690static void batadv_broadcast_vis_packet(struct bat_priv *bat_priv,
691 struct vis_info *info)
c6c8fea2 692{
e1a5382f 693 struct neigh_node *router;
5bf74e9c 694 struct batadv_hashtable *hash = bat_priv->orig_hash;
7aadf889 695 struct hlist_node *node;
c6c8fea2 696 struct hlist_head *head;
c6c8fea2 697 struct orig_node *orig_node;
96412690 698 struct batadv_vis_packet *packet;
c6c8fea2 699 struct sk_buff *skb;
e6c10f43 700 struct hard_iface *hard_iface;
c6c8fea2 701 uint8_t dstaddr[ETH_ALEN];
c90681b8 702 uint32_t i;
c6c8fea2
SE
703
704
96412690 705 packet = (struct batadv_vis_packet *)info->skb_packet->data;
c6c8fea2
SE
706
707 /* send to all routers in range. */
708 for (i = 0; i < hash->size; i++) {
709 head = &hash->table[i];
710
fb778ea1 711 rcu_read_lock();
7aadf889 712 hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
c6c8fea2 713 /* if it's a vis server and reachable, send it. */
acd34afa 714 if (!(orig_node->flags & BATADV_VIS_SERVER))
c6c8fea2 715 continue;
e1a5382f 716
7d211efc 717 router = batadv_orig_node_get_router(orig_node);
e1a5382f
LL
718 if (!router)
719 continue;
720
c6c8fea2 721 /* don't send it if we already received the packet from
9cfc7bd6
SE
722 * this node.
723 */
eaad8ad9
SE
724 if (batadv_recv_list_is_in(bat_priv, &info->recv_list,
725 orig_node->orig)) {
7d211efc 726 batadv_neigh_node_free_ref(router);
c6c8fea2 727 continue;
e1a5382f 728 }
c6c8fea2
SE
729
730 memcpy(packet->target_orig, orig_node->orig, ETH_ALEN);
e1a5382f
LL
731 hard_iface = router->if_incoming;
732 memcpy(dstaddr, router->addr, ETH_ALEN);
733
7d211efc 734 batadv_neigh_node_free_ref(router);
c6c8fea2
SE
735
736 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
737 if (skb)
9455e34c
SE
738 batadv_send_skb_packet(skb, hard_iface,
739 dstaddr);
c6c8fea2 740
c6c8fea2 741 }
fb778ea1 742 rcu_read_unlock();
c6c8fea2 743 }
c6c8fea2
SE
744}
745
eaad8ad9
SE
746static void batadv_unicast_vis_packet(struct bat_priv *bat_priv,
747 struct vis_info *info)
c6c8fea2
SE
748{
749 struct orig_node *orig_node;
e1a5382f 750 struct neigh_node *router = NULL;
c6c8fea2 751 struct sk_buff *skb;
96412690 752 struct batadv_vis_packet *packet;
c6c8fea2 753
96412690 754 packet = (struct batadv_vis_packet *)info->skb_packet->data;
c6c8fea2 755
da641193 756 orig_node = batadv_orig_hash_find(bat_priv, packet->target_orig);
44524fcd 757 if (!orig_node)
e1a5382f 758 goto out;
44524fcd 759
7d211efc 760 router = batadv_orig_node_get_router(orig_node);
e1a5382f
LL
761 if (!router)
762 goto out;
c6c8fea2
SE
763
764 skb = skb_clone(info->skb_packet, GFP_ATOMIC);
765 if (skb)
9455e34c 766 batadv_send_skb_packet(skb, router->if_incoming, router->addr);
c6c8fea2
SE
767
768out:
e1a5382f 769 if (router)
7d211efc 770 batadv_neigh_node_free_ref(router);
44524fcd 771 if (orig_node)
7d211efc 772 batadv_orig_node_free_ref(orig_node);
c6c8fea2
SE
773}
774
eaad8ad9
SE
775/* only send one vis packet. called from batadv_send_vis_packets() */
776static void batadv_send_vis_packet(struct bat_priv *bat_priv,
777 struct vis_info *info)
c6c8fea2 778{
32ae9b22 779 struct hard_iface *primary_if;
96412690 780 struct batadv_vis_packet *packet;
c6c8fea2 781
e5d89254 782 primary_if = batadv_primary_if_get_selected(bat_priv);
32ae9b22
ML
783 if (!primary_if)
784 goto out;
785
96412690 786 packet = (struct batadv_vis_packet *)info->skb_packet->data;
76543d14 787 if (packet->header.ttl < 2) {
c6c8fea2 788 pr_debug("Error - can't send vis packet: ttl exceeded\n");
32ae9b22 789 goto out;
c6c8fea2
SE
790 }
791
32ae9b22 792 memcpy(packet->sender_orig, primary_if->net_dev->dev_addr, ETH_ALEN);
76543d14 793 packet->header.ttl--;
c6c8fea2
SE
794
795 if (is_broadcast_ether_addr(packet->target_orig))
eaad8ad9 796 batadv_broadcast_vis_packet(bat_priv, info);
c6c8fea2 797 else
eaad8ad9 798 batadv_unicast_vis_packet(bat_priv, info);
76543d14 799 packet->header.ttl++; /* restore TTL */
32ae9b22
ML
800
801out:
802 if (primary_if)
e5d89254 803 batadv_hardif_free_ref(primary_if);
c6c8fea2
SE
804}
805
806/* called from timer; send (and maybe generate) vis packet. */
eaad8ad9 807static void batadv_send_vis_packets(struct work_struct *work)
c6c8fea2
SE
808{
809 struct delayed_work *delayed_work =
810 container_of(work, struct delayed_work, work);
811 struct bat_priv *bat_priv =
812 container_of(delayed_work, struct bat_priv, vis_work);
1181e1da 813 struct vis_info *info;
c6c8fea2
SE
814
815 spin_lock_bh(&bat_priv->vis_hash_lock);
eaad8ad9 816 batadv_purge_vis_packets(bat_priv);
c6c8fea2 817
eaad8ad9 818 if (batadv_generate_vis_packet(bat_priv) == 0) {
c6c8fea2 819 /* schedule if generation was successful */
eaad8ad9 820 batadv_send_list_add(bat_priv, bat_priv->my_vis_info);
c6c8fea2
SE
821 }
822
1181e1da
SE
823 while (!list_empty(&bat_priv->vis_send_list)) {
824 info = list_first_entry(&bat_priv->vis_send_list,
825 typeof(*info), send_list);
c6c8fea2
SE
826
827 kref_get(&info->refcount);
828 spin_unlock_bh(&bat_priv->vis_hash_lock);
829
eaad8ad9 830 batadv_send_vis_packet(bat_priv, info);
c6c8fea2
SE
831
832 spin_lock_bh(&bat_priv->vis_hash_lock);
eaad8ad9
SE
833 batadv_send_list_del(info);
834 kref_put(&info->refcount, batadv_free_info);
c6c8fea2
SE
835 }
836 spin_unlock_bh(&bat_priv->vis_hash_lock);
eaad8ad9 837 batadv_start_vis_timer(bat_priv);
c6c8fea2
SE
838}
839
840/* init the vis server. this may only be called when if_list is already
9cfc7bd6
SE
841 * initialized (e.g. bat0 is initialized, interfaces have been added)
842 */
d0f714f4 843int batadv_vis_init(struct bat_priv *bat_priv)
c6c8fea2 844{
96412690 845 struct batadv_vis_packet *packet;
c6c8fea2 846 int hash_added;
347c80f0 847 unsigned int len;
42d0b044 848 unsigned long first_seen;
96412690 849 struct sk_buff *tmp_skb;
c6c8fea2
SE
850
851 if (bat_priv->vis_hash)
5346c35e 852 return 0;
c6c8fea2
SE
853
854 spin_lock_bh(&bat_priv->vis_hash_lock);
855
1a8eaf07 856 bat_priv->vis_hash = batadv_hash_new(256);
c6c8fea2
SE
857 if (!bat_priv->vis_hash) {
858 pr_err("Can't initialize vis_hash\n");
859 goto err;
860 }
861
347c80f0 862 bat_priv->my_vis_info = kmalloc(BATADV_MAX_VIS_PACKET_SIZE, GFP_ATOMIC);
320f422f 863 if (!bat_priv->my_vis_info)
c6c8fea2 864 goto err;
c6c8fea2 865
347c80f0
SE
866 len = sizeof(*packet) + BATADV_MAX_VIS_PACKET_SIZE + ETH_HLEN;
867 bat_priv->my_vis_info->skb_packet = dev_alloc_skb(len);
c6c8fea2
SE
868 if (!bat_priv->my_vis_info->skb_packet)
869 goto free_info;
870
0d125074 871 skb_reserve(bat_priv->my_vis_info->skb_packet, ETH_HLEN);
96412690
SE
872 tmp_skb = bat_priv->my_vis_info->skb_packet;
873 packet = (struct batadv_vis_packet *)skb_put(tmp_skb, sizeof(*packet));
c6c8fea2
SE
874
875 /* prefill the vis info */
42d0b044
SE
876 first_seen = jiffies - msecs_to_jiffies(BATADV_VIS_INTERVAL);
877 bat_priv->my_vis_info->first_seen = first_seen;
c6c8fea2
SE
878 INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list);
879 INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list);
880 kref_init(&bat_priv->my_vis_info->refcount);
881 bat_priv->my_vis_info->bat_priv = bat_priv;
7e071c79 882 packet->header.version = BATADV_COMPAT_VERSION;
acd34afa 883 packet->header.packet_type = BATADV_VIS;
42d0b044 884 packet->header.ttl = BATADV_TTL;
c6c8fea2
SE
885 packet->seqno = 0;
886 packet->entries = 0;
887
888 INIT_LIST_HEAD(&bat_priv->vis_send_list);
889
eaad8ad9
SE
890 hash_added = batadv_hash_add(bat_priv->vis_hash, batadv_vis_info_cmp,
891 batadv_vis_info_choose,
892 bat_priv->my_vis_info,
c0a55929 893 &bat_priv->my_vis_info->hash_entry);
1a1f37d9 894 if (hash_added != 0) {
c6c8fea2
SE
895 pr_err("Can't add own vis packet into hash\n");
896 /* not in hash, need to remove it manually. */
eaad8ad9 897 kref_put(&bat_priv->my_vis_info->refcount, batadv_free_info);
c6c8fea2
SE
898 goto err;
899 }
900
901 spin_unlock_bh(&bat_priv->vis_hash_lock);
eaad8ad9 902 batadv_start_vis_timer(bat_priv);
5346c35e 903 return 0;
c6c8fea2
SE
904
905free_info:
906 kfree(bat_priv->my_vis_info);
907 bat_priv->my_vis_info = NULL;
908err:
909 spin_unlock_bh(&bat_priv->vis_hash_lock);
d0f714f4 910 batadv_vis_quit(bat_priv);
5346c35e 911 return -ENOMEM;
c6c8fea2
SE
912}
913
914/* Decrease the reference count on a hash item info */
eaad8ad9 915static void batadv_free_info_ref(struct hlist_node *node, void *arg)
c6c8fea2 916{
7aadf889 917 struct vis_info *info;
c6c8fea2 918
7aadf889 919 info = container_of(node, struct vis_info, hash_entry);
eaad8ad9
SE
920 batadv_send_list_del(info);
921 kref_put(&info->refcount, batadv_free_info);
c6c8fea2
SE
922}
923
924/* shutdown vis-server */
d0f714f4 925void batadv_vis_quit(struct bat_priv *bat_priv)
c6c8fea2
SE
926{
927 if (!bat_priv->vis_hash)
928 return;
929
930 cancel_delayed_work_sync(&bat_priv->vis_work);
931
932 spin_lock_bh(&bat_priv->vis_hash_lock);
933 /* properly remove, kill timers ... */
eaad8ad9 934 batadv_hash_delete(bat_priv->vis_hash, batadv_free_info_ref, NULL);
c6c8fea2
SE
935 bat_priv->vis_hash = NULL;
936 bat_priv->my_vis_info = NULL;
937 spin_unlock_bh(&bat_priv->vis_hash_lock);
938}
939
940/* schedule packets for (re)transmission */
eaad8ad9 941static void batadv_start_vis_timer(struct bat_priv *bat_priv)
c6c8fea2 942{
eaad8ad9 943 INIT_DELAYED_WORK(&bat_priv->vis_work, batadv_send_vis_packets);
3193e8fd 944 queue_delayed_work(batadv_event_workqueue, &bat_priv->vis_work,
42d0b044 945 msecs_to_jiffies(BATADV_VIS_INTERVAL));
c6c8fea2 946}