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