Commit | Line | Data |
---|---|---|
c6c8fea2 | 1 | /* |
64afe353 | 2 | * Copyright (C) 2008-2011 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
3 | * |
4 | * Simon Wunderlich | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of version 2 of the GNU General Public | |
8 | * License as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 | * 02110-1301, USA | |
19 | * | |
20 | */ | |
21 | ||
22 | #include "main.h" | |
23 | #include "send.h" | |
24 | #include "translation-table.h" | |
25 | #include "vis.h" | |
26 | #include "soft-interface.h" | |
27 | #include "hard-interface.h" | |
28 | #include "hash.h" | |
29 | #include "originator.h" | |
30 | ||
31 | #define MAX_VIS_PACKET_SIZE 1000 | |
32 | ||
33 | /* Returns the smallest signed integer in two's complement with the sizeof x */ | |
34 | #define smallest_signed_int(x) (1u << (7u + 8u * (sizeof(x) - 1u))) | |
35 | ||
36 | /* Checks if a sequence number x is a predecessor/successor of y. | |
37 | * they handle overflows/underflows and can correctly check for a | |
38 | * predecessor/successor unless the variable sequence number has grown by | |
39 | * more then 2**(bitwidth(x)-1)-1. | |
40 | * This means that for a uint8_t with the maximum value 255, it would think: | |
41 | * - when adding nothing - it is neither a predecessor nor a successor | |
42 | * - before adding more than 127 to the starting value - it is a predecessor, | |
43 | * - when adding 128 - it is neither a predecessor nor a successor, | |
44 | * - after adding more than 127 to the starting value - it is a successor */ | |
45 | #define seq_before(x, y) ({typeof(x) _dummy = (x - y); \ | |
46 | _dummy > smallest_signed_int(_dummy); }) | |
47 | #define seq_after(x, y) seq_before(y, x) | |
48 | ||
49 | static void start_vis_timer(struct bat_priv *bat_priv); | |
50 | ||
51 | /* free the info */ | |
52 | static void free_info(struct kref *ref) | |
53 | { | |
54 | struct vis_info *info = container_of(ref, struct vis_info, refcount); | |
55 | struct bat_priv *bat_priv = info->bat_priv; | |
56 | struct recvlist_node *entry, *tmp; | |
57 | ||
58 | list_del_init(&info->send_list); | |
59 | spin_lock_bh(&bat_priv->vis_list_lock); | |
60 | list_for_each_entry_safe(entry, tmp, &info->recv_list, list) { | |
61 | list_del(&entry->list); | |
62 | kfree(entry); | |
63 | } | |
64 | ||
65 | spin_unlock_bh(&bat_priv->vis_list_lock); | |
66 | kfree_skb(info->skb_packet); | |
67 | } | |
68 | ||
69 | /* Compare two vis packets, used by the hashing algorithm */ | |
70 | static int vis_info_cmp(void *data1, void *data2) | |
71 | { | |
72 | struct vis_info *d1, *d2; | |
73 | struct vis_packet *p1, *p2; | |
74 | d1 = data1; | |
75 | d2 = data2; | |
76 | p1 = (struct vis_packet *)d1->skb_packet->data; | |
77 | p2 = (struct vis_packet *)d2->skb_packet->data; | |
78 | return compare_orig(p1->vis_orig, p2->vis_orig); | |
79 | } | |
80 | ||
81 | /* hash function to choose an entry in a hash table of given size */ | |
82 | /* hash algorithm from http://en.wikipedia.org/wiki/Hash_table */ | |
83 | static int vis_info_choose(void *data, int size) | |
84 | { | |
85 | struct vis_info *vis_info = data; | |
86 | struct vis_packet *packet; | |
87 | unsigned char *key; | |
88 | uint32_t hash = 0; | |
89 | size_t i; | |
90 | ||
91 | packet = (struct vis_packet *)vis_info->skb_packet->data; | |
92 | key = packet->vis_orig; | |
93 | for (i = 0; i < ETH_ALEN; i++) { | |
94 | hash += key[i]; | |
95 | hash += (hash << 10); | |
96 | hash ^= (hash >> 6); | |
97 | } | |
98 | ||
99 | hash += (hash << 3); | |
100 | hash ^= (hash >> 11); | |
101 | hash += (hash << 15); | |
102 | ||
103 | return hash % size; | |
104 | } | |
105 | ||
106 | /* insert interface to the list of interfaces of one originator, if it | |
107 | * does not already exist in the list */ | |
108 | static void vis_data_insert_interface(const uint8_t *interface, | |
109 | struct hlist_head *if_list, | |
110 | bool primary) | |
111 | { | |
112 | struct if_list_entry *entry; | |
113 | struct hlist_node *pos; | |
114 | ||
115 | hlist_for_each_entry(entry, pos, if_list, list) { | |
116 | if (compare_orig(entry->addr, (void *)interface)) | |
117 | return; | |
118 | } | |
119 | ||
120 | /* its a new address, add it to the list */ | |
121 | entry = kmalloc(sizeof(*entry), GFP_ATOMIC); | |
122 | if (!entry) | |
123 | return; | |
124 | memcpy(entry->addr, interface, ETH_ALEN); | |
125 | entry->primary = primary; | |
126 | hlist_add_head(&entry->list, if_list); | |
127 | } | |
128 | ||
129 | static ssize_t vis_data_read_prim_sec(char *buff, struct hlist_head *if_list) | |
130 | { | |
131 | struct if_list_entry *entry; | |
132 | struct hlist_node *pos; | |
133 | size_t len = 0; | |
134 | ||
135 | hlist_for_each_entry(entry, pos, if_list, list) { | |
136 | if (entry->primary) | |
137 | len += sprintf(buff + len, "PRIMARY, "); | |
138 | else | |
139 | len += sprintf(buff + len, "SEC %pM, ", entry->addr); | |
140 | } | |
141 | ||
142 | return len; | |
143 | } | |
144 | ||
145 | static size_t vis_data_count_prim_sec(struct hlist_head *if_list) | |
146 | { | |
147 | struct if_list_entry *entry; | |
148 | struct hlist_node *pos; | |
149 | size_t count = 0; | |
150 | ||
151 | hlist_for_each_entry(entry, pos, if_list, list) { | |
152 | if (entry->primary) | |
153 | count += 9; | |
154 | else | |
155 | count += 23; | |
156 | } | |
157 | ||
158 | return count; | |
159 | } | |
160 | ||
161 | /* read an entry */ | |
162 | static ssize_t vis_data_read_entry(char *buff, struct vis_info_entry *entry, | |
163 | uint8_t *src, bool primary) | |
164 | { | |
165 | /* maximal length: max(4+17+2, 3+17+1+3+2) == 26 */ | |
166 | if (primary && entry->quality == 0) | |
167 | return sprintf(buff, "HNA %pM, ", entry->dest); | |
168 | else if (compare_orig(entry->src, src)) | |
169 | return sprintf(buff, "TQ %pM %d, ", entry->dest, | |
170 | entry->quality); | |
171 | ||
172 | return 0; | |
173 | } | |
174 | ||
175 | int vis_seq_print_text(struct seq_file *seq, void *offset) | |
176 | { | |
177 | struct hlist_node *walk; | |
178 | struct hlist_head *head; | |
179 | struct element_t *bucket; | |
180 | struct vis_info *info; | |
181 | struct vis_packet *packet; | |
182 | struct vis_info_entry *entries; | |
183 | struct net_device *net_dev = (struct net_device *)seq->private; | |
184 | struct bat_priv *bat_priv = netdev_priv(net_dev); | |
185 | struct hashtable_t *hash = bat_priv->vis_hash; | |
186 | HLIST_HEAD(vis_if_list); | |
187 | struct if_list_entry *entry; | |
188 | struct hlist_node *pos, *n; | |
189 | int i, j; | |
190 | int vis_server = atomic_read(&bat_priv->vis_mode); | |
191 | size_t buff_pos, buf_size; | |
192 | char *buff; | |
193 | int compare; | |
194 | ||
195 | if ((!bat_priv->primary_if) || | |
196 | (vis_server == VIS_TYPE_CLIENT_UPDATE)) | |
197 | return 0; | |
198 | ||
199 | buf_size = 1; | |
200 | /* Estimate length */ | |
201 | spin_lock_bh(&bat_priv->vis_hash_lock); | |
202 | for (i = 0; i < hash->size; i++) { | |
203 | head = &hash->table[i]; | |
204 | ||
205 | hlist_for_each_entry(bucket, walk, head, hlist) { | |
206 | info = bucket->data; | |
207 | packet = (struct vis_packet *)info->skb_packet->data; | |
208 | entries = (struct vis_info_entry *) | |
209 | ((char *)packet + sizeof(struct vis_packet)); | |
210 | ||
211 | for (j = 0; j < packet->entries; j++) { | |
212 | if (entries[j].quality == 0) | |
213 | continue; | |
214 | compare = | |
215 | compare_orig(entries[j].src, packet->vis_orig); | |
216 | vis_data_insert_interface(entries[j].src, | |
217 | &vis_if_list, | |
218 | compare); | |
219 | } | |
220 | ||
221 | hlist_for_each_entry(entry, pos, &vis_if_list, list) { | |
222 | buf_size += 18 + 26 * packet->entries; | |
223 | ||
224 | /* add primary/secondary records */ | |
225 | if (compare_orig(entry->addr, packet->vis_orig)) | |
226 | buf_size += | |
227 | vis_data_count_prim_sec(&vis_if_list); | |
228 | ||
229 | buf_size += 1; | |
230 | } | |
231 | ||
232 | hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, | |
233 | list) { | |
234 | hlist_del(&entry->list); | |
235 | kfree(entry); | |
236 | } | |
237 | } | |
238 | } | |
239 | ||
240 | buff = kmalloc(buf_size, GFP_ATOMIC); | |
241 | if (!buff) { | |
242 | spin_unlock_bh(&bat_priv->vis_hash_lock); | |
243 | return -ENOMEM; | |
244 | } | |
245 | buff[0] = '\0'; | |
246 | buff_pos = 0; | |
247 | ||
248 | for (i = 0; i < hash->size; i++) { | |
249 | head = &hash->table[i]; | |
250 | ||
251 | hlist_for_each_entry(bucket, walk, head, hlist) { | |
252 | info = bucket->data; | |
253 | packet = (struct vis_packet *)info->skb_packet->data; | |
254 | entries = (struct vis_info_entry *) | |
255 | ((char *)packet + sizeof(struct vis_packet)); | |
256 | ||
257 | for (j = 0; j < packet->entries; j++) { | |
258 | if (entries[j].quality == 0) | |
259 | continue; | |
260 | compare = | |
261 | compare_orig(entries[j].src, packet->vis_orig); | |
262 | vis_data_insert_interface(entries[j].src, | |
263 | &vis_if_list, | |
264 | compare); | |
265 | } | |
266 | ||
267 | hlist_for_each_entry(entry, pos, &vis_if_list, list) { | |
268 | buff_pos += sprintf(buff + buff_pos, "%pM,", | |
269 | entry->addr); | |
270 | ||
271 | for (i = 0; i < packet->entries; i++) | |
272 | buff_pos += vis_data_read_entry( | |
273 | buff + buff_pos, | |
274 | &entries[i], | |
275 | entry->addr, | |
276 | entry->primary); | |
277 | ||
278 | /* add primary/secondary records */ | |
279 | if (compare_orig(entry->addr, packet->vis_orig)) | |
280 | buff_pos += | |
281 | vis_data_read_prim_sec(buff + buff_pos, | |
282 | &vis_if_list); | |
283 | ||
284 | buff_pos += sprintf(buff + buff_pos, "\n"); | |
285 | } | |
286 | ||
287 | hlist_for_each_entry_safe(entry, pos, n, &vis_if_list, | |
288 | list) { | |
289 | hlist_del(&entry->list); | |
290 | kfree(entry); | |
291 | } | |
292 | } | |
293 | } | |
294 | ||
295 | spin_unlock_bh(&bat_priv->vis_hash_lock); | |
296 | ||
297 | seq_printf(seq, "%s", buff); | |
298 | kfree(buff); | |
299 | ||
300 | return 0; | |
301 | } | |
302 | ||
303 | /* add the info packet to the send list, if it was not | |
304 | * already linked in. */ | |
305 | static void send_list_add(struct bat_priv *bat_priv, struct vis_info *info) | |
306 | { | |
307 | if (list_empty(&info->send_list)) { | |
308 | kref_get(&info->refcount); | |
309 | list_add_tail(&info->send_list, &bat_priv->vis_send_list); | |
310 | } | |
311 | } | |
312 | ||
313 | /* delete the info packet from the send list, if it was | |
314 | * linked in. */ | |
315 | static void send_list_del(struct vis_info *info) | |
316 | { | |
317 | if (!list_empty(&info->send_list)) { | |
318 | list_del_init(&info->send_list); | |
319 | kref_put(&info->refcount, free_info); | |
320 | } | |
321 | } | |
322 | ||
323 | /* tries to add one entry to the receive list. */ | |
324 | static void recv_list_add(struct bat_priv *bat_priv, | |
325 | struct list_head *recv_list, char *mac) | |
326 | { | |
327 | struct recvlist_node *entry; | |
328 | ||
329 | entry = kmalloc(sizeof(struct recvlist_node), GFP_ATOMIC); | |
330 | if (!entry) | |
331 | return; | |
332 | ||
333 | memcpy(entry->mac, mac, ETH_ALEN); | |
334 | spin_lock_bh(&bat_priv->vis_list_lock); | |
335 | list_add_tail(&entry->list, recv_list); | |
336 | spin_unlock_bh(&bat_priv->vis_list_lock); | |
337 | } | |
338 | ||
339 | /* returns 1 if this mac is in the recv_list */ | |
340 | static int recv_list_is_in(struct bat_priv *bat_priv, | |
341 | struct list_head *recv_list, char *mac) | |
342 | { | |
343 | struct recvlist_node *entry; | |
344 | ||
345 | spin_lock_bh(&bat_priv->vis_list_lock); | |
346 | list_for_each_entry(entry, recv_list, list) { | |
347 | if (memcmp(entry->mac, mac, ETH_ALEN) == 0) { | |
348 | spin_unlock_bh(&bat_priv->vis_list_lock); | |
349 | return 1; | |
350 | } | |
351 | } | |
352 | spin_unlock_bh(&bat_priv->vis_list_lock); | |
353 | return 0; | |
354 | } | |
355 | ||
356 | /* try to add the packet to the vis_hash. return NULL if invalid (e.g. too old, | |
357 | * broken.. ). vis hash must be locked outside. is_new is set when the packet | |
358 | * is newer than old entries in the hash. */ | |
359 | static struct vis_info *add_packet(struct bat_priv *bat_priv, | |
360 | struct vis_packet *vis_packet, | |
361 | int vis_info_len, int *is_new, | |
362 | int make_broadcast) | |
363 | { | |
364 | struct vis_info *info, *old_info; | |
365 | struct vis_packet *search_packet, *old_packet; | |
366 | struct vis_info search_elem; | |
367 | struct vis_packet *packet; | |
368 | int hash_added; | |
369 | ||
370 | *is_new = 0; | |
371 | /* sanity check */ | |
372 | if (!bat_priv->vis_hash) | |
373 | return NULL; | |
374 | ||
375 | /* see if the packet is already in vis_hash */ | |
376 | search_elem.skb_packet = dev_alloc_skb(sizeof(struct vis_packet)); | |
377 | if (!search_elem.skb_packet) | |
378 | return NULL; | |
379 | search_packet = (struct vis_packet *)skb_put(search_elem.skb_packet, | |
380 | sizeof(struct vis_packet)); | |
381 | ||
382 | memcpy(search_packet->vis_orig, vis_packet->vis_orig, ETH_ALEN); | |
fb778ea1 | 383 | rcu_read_lock(); |
c6c8fea2 SE |
384 | old_info = hash_find(bat_priv->vis_hash, vis_info_cmp, vis_info_choose, |
385 | &search_elem); | |
fb778ea1 | 386 | rcu_read_unlock(); |
c6c8fea2 SE |
387 | kfree_skb(search_elem.skb_packet); |
388 | ||
389 | if (old_info) { | |
390 | old_packet = (struct vis_packet *)old_info->skb_packet->data; | |
391 | if (!seq_after(ntohl(vis_packet->seqno), | |
392 | ntohl(old_packet->seqno))) { | |
393 | if (old_packet->seqno == vis_packet->seqno) { | |
394 | recv_list_add(bat_priv, &old_info->recv_list, | |
395 | vis_packet->sender_orig); | |
396 | return old_info; | |
397 | } else { | |
398 | /* newer packet is already in hash. */ | |
399 | return NULL; | |
400 | } | |
401 | } | |
402 | /* remove old entry */ | |
403 | hash_remove(bat_priv->vis_hash, vis_info_cmp, vis_info_choose, | |
404 | old_info); | |
405 | send_list_del(old_info); | |
406 | kref_put(&old_info->refcount, free_info); | |
407 | } | |
408 | ||
409 | info = kmalloc(sizeof(struct vis_info), GFP_ATOMIC); | |
410 | if (!info) | |
411 | return NULL; | |
412 | ||
413 | info->skb_packet = dev_alloc_skb(sizeof(struct vis_packet) + | |
414 | vis_info_len + sizeof(struct ethhdr)); | |
415 | if (!info->skb_packet) { | |
416 | kfree(info); | |
417 | return NULL; | |
418 | } | |
419 | skb_reserve(info->skb_packet, sizeof(struct ethhdr)); | |
420 | packet = (struct vis_packet *)skb_put(info->skb_packet, | |
421 | sizeof(struct vis_packet) + | |
422 | vis_info_len); | |
423 | ||
424 | kref_init(&info->refcount); | |
425 | INIT_LIST_HEAD(&info->send_list); | |
426 | INIT_LIST_HEAD(&info->recv_list); | |
427 | info->first_seen = jiffies; | |
428 | info->bat_priv = bat_priv; | |
429 | memcpy(packet, vis_packet, sizeof(struct vis_packet) + vis_info_len); | |
430 | ||
431 | /* initialize and add new packet. */ | |
432 | *is_new = 1; | |
433 | ||
434 | /* Make it a broadcast packet, if required */ | |
435 | if (make_broadcast) | |
436 | memcpy(packet->target_orig, broadcast_addr, ETH_ALEN); | |
437 | ||
438 | /* repair if entries is longer than packet. */ | |
439 | if (packet->entries * sizeof(struct vis_info_entry) > vis_info_len) | |
440 | packet->entries = vis_info_len / sizeof(struct vis_info_entry); | |
441 | ||
442 | recv_list_add(bat_priv, &info->recv_list, packet->sender_orig); | |
443 | ||
444 | /* try to add it */ | |
445 | hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose, | |
446 | info); | |
447 | if (hash_added < 0) { | |
448 | /* did not work (for some reason) */ | |
449 | kref_put(&old_info->refcount, free_info); | |
450 | info = NULL; | |
451 | } | |
452 | ||
453 | return info; | |
454 | } | |
455 | ||
456 | /* handle the server sync packet, forward if needed. */ | |
457 | void receive_server_sync_packet(struct bat_priv *bat_priv, | |
458 | struct vis_packet *vis_packet, | |
459 | int vis_info_len) | |
460 | { | |
461 | struct vis_info *info; | |
462 | int is_new, make_broadcast; | |
463 | int vis_server = atomic_read(&bat_priv->vis_mode); | |
464 | ||
465 | make_broadcast = (vis_server == VIS_TYPE_SERVER_SYNC); | |
466 | ||
467 | spin_lock_bh(&bat_priv->vis_hash_lock); | |
468 | info = add_packet(bat_priv, vis_packet, vis_info_len, | |
469 | &is_new, make_broadcast); | |
470 | if (!info) | |
471 | goto end; | |
472 | ||
473 | /* only if we are server ourselves and packet is newer than the one in | |
474 | * hash.*/ | |
475 | if (vis_server == VIS_TYPE_SERVER_SYNC && is_new) | |
476 | send_list_add(bat_priv, info); | |
477 | end: | |
478 | spin_unlock_bh(&bat_priv->vis_hash_lock); | |
479 | } | |
480 | ||
481 | /* handle an incoming client update packet and schedule forward if needed. */ | |
482 | void receive_client_update_packet(struct bat_priv *bat_priv, | |
483 | struct vis_packet *vis_packet, | |
484 | int vis_info_len) | |
485 | { | |
486 | struct vis_info *info; | |
487 | struct vis_packet *packet; | |
488 | int is_new; | |
489 | int vis_server = atomic_read(&bat_priv->vis_mode); | |
490 | int are_target = 0; | |
491 | ||
492 | /* clients shall not broadcast. */ | |
493 | if (is_broadcast_ether_addr(vis_packet->target_orig)) | |
494 | return; | |
495 | ||
496 | /* Are we the target for this VIS packet? */ | |
497 | if (vis_server == VIS_TYPE_SERVER_SYNC && | |
498 | is_my_mac(vis_packet->target_orig)) | |
499 | are_target = 1; | |
500 | ||
501 | spin_lock_bh(&bat_priv->vis_hash_lock); | |
502 | info = add_packet(bat_priv, vis_packet, vis_info_len, | |
503 | &is_new, are_target); | |
504 | ||
505 | if (!info) | |
506 | goto end; | |
507 | /* note that outdated packets will be dropped at this point. */ | |
508 | ||
509 | packet = (struct vis_packet *)info->skb_packet->data; | |
510 | ||
511 | /* send only if we're the target server or ... */ | |
512 | if (are_target && is_new) { | |
513 | packet->vis_type = VIS_TYPE_SERVER_SYNC; /* upgrade! */ | |
514 | send_list_add(bat_priv, info); | |
515 | ||
516 | /* ... we're not the recipient (and thus need to forward). */ | |
517 | } else if (!is_my_mac(packet->target_orig)) { | |
518 | send_list_add(bat_priv, info); | |
519 | } | |
520 | ||
521 | end: | |
522 | spin_unlock_bh(&bat_priv->vis_hash_lock); | |
523 | } | |
524 | ||
525 | /* Walk the originators and find the VIS server with the best tq. Set the packet | |
526 | * address to its address and return the best_tq. | |
527 | * | |
528 | * Must be called with the originator hash locked */ | |
529 | static int find_best_vis_server(struct bat_priv *bat_priv, | |
530 | struct vis_info *info) | |
531 | { | |
532 | struct hashtable_t *hash = bat_priv->orig_hash; | |
533 | struct hlist_node *walk; | |
534 | struct hlist_head *head; | |
535 | struct element_t *bucket; | |
536 | struct orig_node *orig_node; | |
537 | struct vis_packet *packet; | |
538 | int best_tq = -1, i; | |
539 | ||
540 | packet = (struct vis_packet *)info->skb_packet->data; | |
541 | ||
542 | for (i = 0; i < hash->size; i++) { | |
543 | head = &hash->table[i]; | |
544 | ||
fb778ea1 ML |
545 | rcu_read_lock(); |
546 | hlist_for_each_entry_rcu(bucket, walk, head, hlist) { | |
c6c8fea2 SE |
547 | orig_node = bucket->data; |
548 | if ((orig_node) && (orig_node->router) && | |
549 | (orig_node->flags & VIS_SERVER) && | |
550 | (orig_node->router->tq_avg > best_tq)) { | |
551 | best_tq = orig_node->router->tq_avg; | |
552 | memcpy(packet->target_orig, orig_node->orig, | |
553 | ETH_ALEN); | |
554 | } | |
555 | } | |
fb778ea1 | 556 | rcu_read_unlock(); |
c6c8fea2 SE |
557 | } |
558 | ||
559 | return best_tq; | |
560 | } | |
561 | ||
562 | /* Return true if the vis packet is full. */ | |
563 | static bool vis_packet_full(struct vis_info *info) | |
564 | { | |
565 | struct vis_packet *packet; | |
566 | packet = (struct vis_packet *)info->skb_packet->data; | |
567 | ||
568 | if (MAX_VIS_PACKET_SIZE / sizeof(struct vis_info_entry) | |
569 | < packet->entries + 1) | |
570 | return true; | |
571 | return false; | |
572 | } | |
573 | ||
574 | /* generates a packet of own vis data, | |
575 | * returns 0 on success, -1 if no packet could be generated */ | |
576 | static int generate_vis_packet(struct bat_priv *bat_priv) | |
577 | { | |
578 | struct hashtable_t *hash = bat_priv->orig_hash; | |
579 | struct hlist_node *walk; | |
580 | struct hlist_head *head; | |
581 | struct element_t *bucket; | |
582 | struct orig_node *orig_node; | |
583 | struct neigh_node *neigh_node; | |
584 | struct vis_info *info = (struct vis_info *)bat_priv->my_vis_info; | |
585 | struct vis_packet *packet = (struct vis_packet *)info->skb_packet->data; | |
586 | struct vis_info_entry *entry; | |
587 | struct hna_local_entry *hna_local_entry; | |
588 | int best_tq = -1, i; | |
589 | ||
590 | info->first_seen = jiffies; | |
591 | packet->vis_type = atomic_read(&bat_priv->vis_mode); | |
592 | ||
593 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
594 | memcpy(packet->target_orig, broadcast_addr, ETH_ALEN); | |
595 | packet->ttl = TTL; | |
596 | packet->seqno = htonl(ntohl(packet->seqno) + 1); | |
597 | packet->entries = 0; | |
598 | skb_trim(info->skb_packet, sizeof(struct vis_packet)); | |
599 | ||
600 | if (packet->vis_type == VIS_TYPE_CLIENT_UPDATE) { | |
601 | best_tq = find_best_vis_server(bat_priv, info); | |
602 | ||
603 | if (best_tq < 0) { | |
604 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
605 | return -1; | |
606 | } | |
607 | } | |
608 | ||
609 | for (i = 0; i < hash->size; i++) { | |
610 | head = &hash->table[i]; | |
611 | ||
fb778ea1 ML |
612 | rcu_read_lock(); |
613 | hlist_for_each_entry_rcu(bucket, walk, head, hlist) { | |
c6c8fea2 SE |
614 | orig_node = bucket->data; |
615 | neigh_node = orig_node->router; | |
616 | ||
617 | if (!neigh_node) | |
618 | continue; | |
619 | ||
620 | if (!compare_orig(neigh_node->addr, orig_node->orig)) | |
621 | continue; | |
622 | ||
623 | if (neigh_node->if_incoming->if_status != IF_ACTIVE) | |
624 | continue; | |
625 | ||
626 | if (neigh_node->tq_avg < 1) | |
627 | continue; | |
628 | ||
629 | /* fill one entry into buffer. */ | |
630 | entry = (struct vis_info_entry *) | |
631 | skb_put(info->skb_packet, sizeof(*entry)); | |
632 | memcpy(entry->src, | |
633 | neigh_node->if_incoming->net_dev->dev_addr, | |
634 | ETH_ALEN); | |
635 | memcpy(entry->dest, orig_node->orig, ETH_ALEN); | |
636 | entry->quality = neigh_node->tq_avg; | |
637 | packet->entries++; | |
638 | ||
639 | if (vis_packet_full(info)) { | |
fb778ea1 | 640 | rcu_read_unlock(); |
c6c8fea2 SE |
641 | spin_unlock_bh(&bat_priv->orig_hash_lock); |
642 | return 0; | |
643 | } | |
644 | } | |
fb778ea1 | 645 | rcu_read_unlock(); |
c6c8fea2 SE |
646 | } |
647 | ||
648 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
649 | ||
650 | hash = bat_priv->hna_local_hash; | |
651 | ||
652 | spin_lock_bh(&bat_priv->hna_lhash_lock); | |
653 | for (i = 0; i < hash->size; i++) { | |
654 | head = &hash->table[i]; | |
655 | ||
656 | hlist_for_each_entry(bucket, walk, head, hlist) { | |
657 | hna_local_entry = bucket->data; | |
658 | entry = (struct vis_info_entry *) | |
659 | skb_put(info->skb_packet, | |
660 | sizeof(*entry)); | |
661 | memset(entry->src, 0, ETH_ALEN); | |
662 | memcpy(entry->dest, hna_local_entry->addr, ETH_ALEN); | |
663 | entry->quality = 0; /* 0 means HNA */ | |
664 | packet->entries++; | |
665 | ||
666 | if (vis_packet_full(info)) { | |
667 | spin_unlock_bh(&bat_priv->hna_lhash_lock); | |
668 | return 0; | |
669 | } | |
670 | } | |
671 | } | |
672 | ||
673 | spin_unlock_bh(&bat_priv->hna_lhash_lock); | |
674 | return 0; | |
675 | } | |
676 | ||
677 | /* free old vis packets. Must be called with this vis_hash_lock | |
678 | * held */ | |
679 | static void purge_vis_packets(struct bat_priv *bat_priv) | |
680 | { | |
681 | int i; | |
682 | struct hashtable_t *hash = bat_priv->vis_hash; | |
683 | struct hlist_node *walk, *safe; | |
684 | struct hlist_head *head; | |
685 | struct element_t *bucket; | |
686 | struct vis_info *info; | |
687 | ||
688 | for (i = 0; i < hash->size; i++) { | |
689 | head = &hash->table[i]; | |
690 | ||
691 | hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) { | |
692 | info = bucket->data; | |
693 | ||
694 | /* never purge own data. */ | |
695 | if (info == bat_priv->my_vis_info) | |
696 | continue; | |
697 | ||
698 | if (time_after(jiffies, | |
699 | info->first_seen + VIS_TIMEOUT * HZ)) { | |
700 | hlist_del(walk); | |
701 | kfree(bucket); | |
702 | send_list_del(info); | |
703 | kref_put(&info->refcount, free_info); | |
704 | } | |
705 | } | |
706 | } | |
707 | } | |
708 | ||
709 | static void broadcast_vis_packet(struct bat_priv *bat_priv, | |
710 | struct vis_info *info) | |
711 | { | |
712 | struct hashtable_t *hash = bat_priv->orig_hash; | |
713 | struct hlist_node *walk; | |
714 | struct hlist_head *head; | |
715 | struct element_t *bucket; | |
716 | struct orig_node *orig_node; | |
717 | struct vis_packet *packet; | |
718 | struct sk_buff *skb; | |
719 | struct batman_if *batman_if; | |
720 | uint8_t dstaddr[ETH_ALEN]; | |
721 | int i; | |
722 | ||
723 | ||
724 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
725 | packet = (struct vis_packet *)info->skb_packet->data; | |
726 | ||
727 | /* send to all routers in range. */ | |
728 | for (i = 0; i < hash->size; i++) { | |
729 | head = &hash->table[i]; | |
730 | ||
fb778ea1 ML |
731 | rcu_read_lock(); |
732 | hlist_for_each_entry_rcu(bucket, walk, head, hlist) { | |
c6c8fea2 SE |
733 | orig_node = bucket->data; |
734 | ||
735 | /* if it's a vis server and reachable, send it. */ | |
736 | if ((!orig_node) || (!orig_node->router)) | |
737 | continue; | |
738 | if (!(orig_node->flags & VIS_SERVER)) | |
739 | continue; | |
740 | /* don't send it if we already received the packet from | |
741 | * this node. */ | |
742 | if (recv_list_is_in(bat_priv, &info->recv_list, | |
743 | orig_node->orig)) | |
744 | continue; | |
745 | ||
746 | memcpy(packet->target_orig, orig_node->orig, ETH_ALEN); | |
747 | batman_if = orig_node->router->if_incoming; | |
748 | memcpy(dstaddr, orig_node->router->addr, ETH_ALEN); | |
749 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
750 | ||
751 | skb = skb_clone(info->skb_packet, GFP_ATOMIC); | |
752 | if (skb) | |
753 | send_skb_packet(skb, batman_if, dstaddr); | |
754 | ||
755 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
756 | } | |
fb778ea1 | 757 | rcu_read_unlock(); |
c6c8fea2 SE |
758 | } |
759 | ||
760 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
761 | } | |
762 | ||
763 | static void unicast_vis_packet(struct bat_priv *bat_priv, | |
764 | struct vis_info *info) | |
765 | { | |
766 | struct orig_node *orig_node; | |
767 | struct sk_buff *skb; | |
768 | struct vis_packet *packet; | |
769 | struct batman_if *batman_if; | |
770 | uint8_t dstaddr[ETH_ALEN]; | |
771 | ||
772 | spin_lock_bh(&bat_priv->orig_hash_lock); | |
773 | packet = (struct vis_packet *)info->skb_packet->data; | |
fb778ea1 | 774 | rcu_read_lock(); |
c6c8fea2 SE |
775 | orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash, |
776 | compare_orig, choose_orig, | |
777 | packet->target_orig)); | |
fb778ea1 | 778 | rcu_read_unlock(); |
c6c8fea2 SE |
779 | |
780 | if ((!orig_node) || (!orig_node->router)) | |
781 | goto out; | |
782 | ||
783 | /* don't lock while sending the packets ... we therefore | |
784 | * copy the required data before sending */ | |
785 | batman_if = orig_node->router->if_incoming; | |
786 | memcpy(dstaddr, orig_node->router->addr, ETH_ALEN); | |
787 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
788 | ||
789 | skb = skb_clone(info->skb_packet, GFP_ATOMIC); | |
790 | if (skb) | |
791 | send_skb_packet(skb, batman_if, dstaddr); | |
792 | ||
793 | return; | |
794 | ||
795 | out: | |
796 | spin_unlock_bh(&bat_priv->orig_hash_lock); | |
797 | } | |
798 | ||
799 | /* only send one vis packet. called from send_vis_packets() */ | |
800 | static void send_vis_packet(struct bat_priv *bat_priv, struct vis_info *info) | |
801 | { | |
802 | struct vis_packet *packet; | |
803 | ||
804 | packet = (struct vis_packet *)info->skb_packet->data; | |
805 | if (packet->ttl < 2) { | |
806 | pr_debug("Error - can't send vis packet: ttl exceeded\n"); | |
807 | return; | |
808 | } | |
809 | ||
810 | memcpy(packet->sender_orig, bat_priv->primary_if->net_dev->dev_addr, | |
811 | ETH_ALEN); | |
812 | packet->ttl--; | |
813 | ||
814 | if (is_broadcast_ether_addr(packet->target_orig)) | |
815 | broadcast_vis_packet(bat_priv, info); | |
816 | else | |
817 | unicast_vis_packet(bat_priv, info); | |
818 | packet->ttl++; /* restore TTL */ | |
819 | } | |
820 | ||
821 | /* called from timer; send (and maybe generate) vis packet. */ | |
822 | static void send_vis_packets(struct work_struct *work) | |
823 | { | |
824 | struct delayed_work *delayed_work = | |
825 | container_of(work, struct delayed_work, work); | |
826 | struct bat_priv *bat_priv = | |
827 | container_of(delayed_work, struct bat_priv, vis_work); | |
828 | struct vis_info *info, *temp; | |
829 | ||
830 | spin_lock_bh(&bat_priv->vis_hash_lock); | |
831 | purge_vis_packets(bat_priv); | |
832 | ||
833 | if (generate_vis_packet(bat_priv) == 0) { | |
834 | /* schedule if generation was successful */ | |
835 | send_list_add(bat_priv, bat_priv->my_vis_info); | |
836 | } | |
837 | ||
838 | list_for_each_entry_safe(info, temp, &bat_priv->vis_send_list, | |
839 | send_list) { | |
840 | ||
841 | kref_get(&info->refcount); | |
842 | spin_unlock_bh(&bat_priv->vis_hash_lock); | |
843 | ||
844 | if (bat_priv->primary_if) | |
845 | send_vis_packet(bat_priv, info); | |
846 | ||
847 | spin_lock_bh(&bat_priv->vis_hash_lock); | |
848 | send_list_del(info); | |
849 | kref_put(&info->refcount, free_info); | |
850 | } | |
851 | spin_unlock_bh(&bat_priv->vis_hash_lock); | |
852 | start_vis_timer(bat_priv); | |
853 | } | |
854 | ||
855 | /* init the vis server. this may only be called when if_list is already | |
856 | * initialized (e.g. bat0 is initialized, interfaces have been added) */ | |
857 | int vis_init(struct bat_priv *bat_priv) | |
858 | { | |
859 | struct vis_packet *packet; | |
860 | int hash_added; | |
861 | ||
862 | if (bat_priv->vis_hash) | |
863 | return 1; | |
864 | ||
865 | spin_lock_bh(&bat_priv->vis_hash_lock); | |
866 | ||
867 | bat_priv->vis_hash = hash_new(256); | |
868 | if (!bat_priv->vis_hash) { | |
869 | pr_err("Can't initialize vis_hash\n"); | |
870 | goto err; | |
871 | } | |
872 | ||
873 | bat_priv->my_vis_info = kmalloc(MAX_VIS_PACKET_SIZE, GFP_ATOMIC); | |
874 | if (!bat_priv->my_vis_info) { | |
875 | pr_err("Can't initialize vis packet\n"); | |
876 | goto err; | |
877 | } | |
878 | ||
879 | bat_priv->my_vis_info->skb_packet = dev_alloc_skb( | |
880 | sizeof(struct vis_packet) + | |
881 | MAX_VIS_PACKET_SIZE + | |
882 | sizeof(struct ethhdr)); | |
883 | if (!bat_priv->my_vis_info->skb_packet) | |
884 | goto free_info; | |
885 | ||
886 | skb_reserve(bat_priv->my_vis_info->skb_packet, sizeof(struct ethhdr)); | |
887 | packet = (struct vis_packet *)skb_put( | |
888 | bat_priv->my_vis_info->skb_packet, | |
889 | sizeof(struct vis_packet)); | |
890 | ||
891 | /* prefill the vis info */ | |
892 | bat_priv->my_vis_info->first_seen = jiffies - | |
893 | msecs_to_jiffies(VIS_INTERVAL); | |
894 | INIT_LIST_HEAD(&bat_priv->my_vis_info->recv_list); | |
895 | INIT_LIST_HEAD(&bat_priv->my_vis_info->send_list); | |
896 | kref_init(&bat_priv->my_vis_info->refcount); | |
897 | bat_priv->my_vis_info->bat_priv = bat_priv; | |
898 | packet->version = COMPAT_VERSION; | |
899 | packet->packet_type = BAT_VIS; | |
900 | packet->ttl = TTL; | |
901 | packet->seqno = 0; | |
902 | packet->entries = 0; | |
903 | ||
904 | INIT_LIST_HEAD(&bat_priv->vis_send_list); | |
905 | ||
906 | hash_added = hash_add(bat_priv->vis_hash, vis_info_cmp, vis_info_choose, | |
907 | bat_priv->my_vis_info); | |
908 | if (hash_added < 0) { | |
909 | pr_err("Can't add own vis packet into hash\n"); | |
910 | /* not in hash, need to remove it manually. */ | |
911 | kref_put(&bat_priv->my_vis_info->refcount, free_info); | |
912 | goto err; | |
913 | } | |
914 | ||
915 | spin_unlock_bh(&bat_priv->vis_hash_lock); | |
916 | start_vis_timer(bat_priv); | |
917 | return 1; | |
918 | ||
919 | free_info: | |
920 | kfree(bat_priv->my_vis_info); | |
921 | bat_priv->my_vis_info = NULL; | |
922 | err: | |
923 | spin_unlock_bh(&bat_priv->vis_hash_lock); | |
924 | vis_quit(bat_priv); | |
925 | return 0; | |
926 | } | |
927 | ||
928 | /* Decrease the reference count on a hash item info */ | |
929 | static void free_info_ref(void *data, void *arg) | |
930 | { | |
931 | struct vis_info *info = data; | |
932 | ||
933 | send_list_del(info); | |
934 | kref_put(&info->refcount, free_info); | |
935 | } | |
936 | ||
937 | /* shutdown vis-server */ | |
938 | void vis_quit(struct bat_priv *bat_priv) | |
939 | { | |
940 | if (!bat_priv->vis_hash) | |
941 | return; | |
942 | ||
943 | cancel_delayed_work_sync(&bat_priv->vis_work); | |
944 | ||
945 | spin_lock_bh(&bat_priv->vis_hash_lock); | |
946 | /* properly remove, kill timers ... */ | |
947 | hash_delete(bat_priv->vis_hash, free_info_ref, NULL); | |
948 | bat_priv->vis_hash = NULL; | |
949 | bat_priv->my_vis_info = NULL; | |
950 | spin_unlock_bh(&bat_priv->vis_hash_lock); | |
951 | } | |
952 | ||
953 | /* schedule packets for (re)transmission */ | |
954 | static void start_vis_timer(struct bat_priv *bat_priv) | |
955 | { | |
956 | INIT_DELAYED_WORK(&bat_priv->vis_work, send_vis_packets); | |
957 | queue_delayed_work(bat_event_workqueue, &bat_priv->vis_work, | |
958 | msecs_to_jiffies(VIS_INTERVAL)); | |
959 | } |