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