batman-adv: convert neighbor list to hlist
[linux-2.6-block.git] / net / batman-adv / originator.c
CommitLineData
c6c8fea2 1/*
64afe353 2 * Copyright (C) 2009-2011 B.A.T.M.A.N. contributors:
c6c8fea2
SE
3 *
4 * Marek Lindner, 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/* increase the reference counter for this originator */
23
24#include "main.h"
25#include "originator.h"
26#include "hash.h"
27#include "translation-table.h"
28#include "routing.h"
29#include "gateway_client.h"
30#include "hard-interface.h"
31#include "unicast.h"
32#include "soft-interface.h"
33
34static void purge_orig(struct work_struct *work);
35
36static void start_purge_timer(struct bat_priv *bat_priv)
37{
38 INIT_DELAYED_WORK(&bat_priv->orig_work, purge_orig);
39 queue_delayed_work(bat_event_workqueue, &bat_priv->orig_work, 1 * HZ);
40}
41
42int originator_init(struct bat_priv *bat_priv)
43{
44 if (bat_priv->orig_hash)
45 return 1;
46
47 spin_lock_bh(&bat_priv->orig_hash_lock);
48 bat_priv->orig_hash = hash_new(1024);
49
50 if (!bat_priv->orig_hash)
51 goto err;
52
53 spin_unlock_bh(&bat_priv->orig_hash_lock);
54 start_purge_timer(bat_priv);
55 return 1;
56
57err:
58 spin_unlock_bh(&bat_priv->orig_hash_lock);
59 return 0;
60}
61
a8e7f4bc
ML
62void neigh_node_free_ref(struct kref *refcount)
63{
64 struct neigh_node *neigh_node;
65
66 neigh_node = container_of(refcount, struct neigh_node, refcount);
67 kfree(neigh_node);
68}
69
70struct neigh_node *create_neighbor(struct orig_node *orig_node,
71 struct orig_node *orig_neigh_node,
72 uint8_t *neigh,
73 struct batman_if *if_incoming)
c6c8fea2
SE
74{
75 struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
76 struct neigh_node *neigh_node;
77
78 bat_dbg(DBG_BATMAN, bat_priv,
79 "Creating new last-hop neighbor of originator\n");
80
81 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
82 if (!neigh_node)
83 return NULL;
84
9591a79f 85 INIT_HLIST_NODE(&neigh_node->list);
c6c8fea2
SE
86
87 memcpy(neigh_node->addr, neigh, ETH_ALEN);
88 neigh_node->orig_node = orig_neigh_node;
89 neigh_node->if_incoming = if_incoming;
a8e7f4bc 90 kref_init(&neigh_node->refcount);
c6c8fea2 91
9591a79f 92 hlist_add_head(&neigh_node->list, &orig_node->neigh_list);
c6c8fea2
SE
93 return neigh_node;
94}
95
96static void free_orig_node(void *data, void *arg)
97{
9591a79f 98 struct hlist_node *node, *node_tmp;
c6c8fea2
SE
99 struct neigh_node *neigh_node;
100 struct orig_node *orig_node = (struct orig_node *)data;
101 struct bat_priv *bat_priv = (struct bat_priv *)arg;
102
103 /* for all neighbors towards this originator ... */
9591a79f
ML
104 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
105 &orig_node->neigh_list, list) {
106 hlist_del(&neigh_node->list);
a8e7f4bc 107 kref_put(&neigh_node->refcount, neigh_node_free_ref);
c6c8fea2
SE
108 }
109
110 frag_list_free(&orig_node->frag_list);
111 hna_global_del_orig(bat_priv, orig_node, "originator timed out");
112
113 kfree(orig_node->bcast_own);
114 kfree(orig_node->bcast_own_sum);
115 kfree(orig_node);
116}
117
118void originator_free(struct bat_priv *bat_priv)
119{
120 if (!bat_priv->orig_hash)
121 return;
122
123 cancel_delayed_work_sync(&bat_priv->orig_work);
124
125 spin_lock_bh(&bat_priv->orig_hash_lock);
126 hash_delete(bat_priv->orig_hash, free_orig_node, bat_priv);
127 bat_priv->orig_hash = NULL;
128 spin_unlock_bh(&bat_priv->orig_hash_lock);
129}
130
131/* this function finds or creates an originator entry for the given
132 * address if it does not exits */
133struct orig_node *get_orig_node(struct bat_priv *bat_priv, uint8_t *addr)
134{
135 struct orig_node *orig_node;
136 int size;
137 int hash_added;
138
139 orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
140 compare_orig, choose_orig,
141 addr));
142
143 if (orig_node)
144 return orig_node;
145
146 bat_dbg(DBG_BATMAN, bat_priv,
147 "Creating new originator: %pM\n", addr);
148
149 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
150 if (!orig_node)
151 return NULL;
152
9591a79f 153 INIT_HLIST_HEAD(&orig_node->neigh_list);
c6c8fea2
SE
154
155 memcpy(orig_node->orig, addr, ETH_ALEN);
156 orig_node->router = NULL;
157 orig_node->hna_buff = NULL;
158 orig_node->bcast_seqno_reset = jiffies - 1
159 - msecs_to_jiffies(RESET_PROTECTION_MS);
160 orig_node->batman_seqno_reset = jiffies - 1
161 - msecs_to_jiffies(RESET_PROTECTION_MS);
162
163 size = bat_priv->num_ifaces * sizeof(unsigned long) * NUM_WORDS;
164
165 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
166 if (!orig_node->bcast_own)
167 goto free_orig_node;
168
169 size = bat_priv->num_ifaces * sizeof(uint8_t);
170 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
171
172 INIT_LIST_HEAD(&orig_node->frag_list);
173 orig_node->last_frag_packet = 0;
174
175 if (!orig_node->bcast_own_sum)
176 goto free_bcast_own;
177
178 hash_added = hash_add(bat_priv->orig_hash, compare_orig, choose_orig,
179 orig_node);
180 if (hash_added < 0)
181 goto free_bcast_own_sum;
182
183 return orig_node;
184free_bcast_own_sum:
185 kfree(orig_node->bcast_own_sum);
186free_bcast_own:
187 kfree(orig_node->bcast_own);
188free_orig_node:
189 kfree(orig_node);
190 return NULL;
191}
192
193static bool purge_orig_neighbors(struct bat_priv *bat_priv,
194 struct orig_node *orig_node,
195 struct neigh_node **best_neigh_node)
196{
9591a79f 197 struct hlist_node *node, *node_tmp;
c6c8fea2
SE
198 struct neigh_node *neigh_node;
199 bool neigh_purged = false;
200
201 *best_neigh_node = NULL;
202
203 /* for all neighbors towards this originator ... */
9591a79f
ML
204 hlist_for_each_entry_safe(neigh_node, node, node_tmp,
205 &orig_node->neigh_list, list) {
c6c8fea2
SE
206
207 if ((time_after(jiffies,
208 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
209 (neigh_node->if_incoming->if_status == IF_INACTIVE) ||
210 (neigh_node->if_incoming->if_status == IF_TO_BE_REMOVED)) {
211
212 if (neigh_node->if_incoming->if_status ==
213 IF_TO_BE_REMOVED)
214 bat_dbg(DBG_BATMAN, bat_priv,
215 "neighbor purge: originator %pM, "
216 "neighbor: %pM, iface: %s\n",
217 orig_node->orig, neigh_node->addr,
218 neigh_node->if_incoming->net_dev->name);
219 else
220 bat_dbg(DBG_BATMAN, bat_priv,
221 "neighbor timeout: originator %pM, "
222 "neighbor: %pM, last_valid: %lu\n",
223 orig_node->orig, neigh_node->addr,
224 (neigh_node->last_valid / HZ));
225
226 neigh_purged = true;
9591a79f
ML
227
228 hlist_del(&neigh_node->list);
a8e7f4bc 229 kref_put(&neigh_node->refcount, neigh_node_free_ref);
c6c8fea2
SE
230 } else {
231 if ((!*best_neigh_node) ||
232 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
233 *best_neigh_node = neigh_node;
234 }
235 }
236 return neigh_purged;
237}
238
239static bool purge_orig_node(struct bat_priv *bat_priv,
240 struct orig_node *orig_node)
241{
242 struct neigh_node *best_neigh_node;
243
244 if (time_after(jiffies,
245 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
246
247 bat_dbg(DBG_BATMAN, bat_priv,
248 "Originator timeout: originator %pM, last_valid %lu\n",
249 orig_node->orig, (orig_node->last_valid / HZ));
250 return true;
251 } else {
252 if (purge_orig_neighbors(bat_priv, orig_node,
253 &best_neigh_node)) {
254 update_routes(bat_priv, orig_node,
255 best_neigh_node,
256 orig_node->hna_buff,
257 orig_node->hna_buff_len);
258 /* update bonding candidates, we could have lost
259 * some candidates. */
74ef1153 260 update_bonding_candidates(orig_node);
c6c8fea2
SE
261 }
262 }
263
264 return false;
265}
266
267static void _purge_orig(struct bat_priv *bat_priv)
268{
269 struct hashtable_t *hash = bat_priv->orig_hash;
270 struct hlist_node *walk, *safe;
271 struct hlist_head *head;
272 struct element_t *bucket;
273 struct orig_node *orig_node;
274 int i;
275
276 if (!hash)
277 return;
278
279 spin_lock_bh(&bat_priv->orig_hash_lock);
280
281 /* for all origins... */
282 for (i = 0; i < hash->size; i++) {
283 head = &hash->table[i];
284
285 hlist_for_each_entry_safe(bucket, walk, safe, head, hlist) {
286 orig_node = bucket->data;
287
288 if (purge_orig_node(bat_priv, orig_node)) {
289 if (orig_node->gw_flags)
290 gw_node_delete(bat_priv, orig_node);
291 hlist_del(walk);
292 kfree(bucket);
293 free_orig_node(orig_node, bat_priv);
294 }
295
296 if (time_after(jiffies, orig_node->last_frag_packet +
297 msecs_to_jiffies(FRAG_TIMEOUT)))
298 frag_list_free(&orig_node->frag_list);
299 }
300 }
301
302 spin_unlock_bh(&bat_priv->orig_hash_lock);
303
304 gw_node_purge(bat_priv);
305 gw_election(bat_priv);
306
307 softif_neigh_purge(bat_priv);
308}
309
310static void purge_orig(struct work_struct *work)
311{
312 struct delayed_work *delayed_work =
313 container_of(work, struct delayed_work, work);
314 struct bat_priv *bat_priv =
315 container_of(delayed_work, struct bat_priv, orig_work);
316
317 _purge_orig(bat_priv);
318 start_purge_timer(bat_priv);
319}
320
321void purge_orig_ref(struct bat_priv *bat_priv)
322{
323 _purge_orig(bat_priv);
324}
325
326int orig_seq_print_text(struct seq_file *seq, void *offset)
327{
328 struct net_device *net_dev = (struct net_device *)seq->private;
329 struct bat_priv *bat_priv = netdev_priv(net_dev);
330 struct hashtable_t *hash = bat_priv->orig_hash;
9591a79f 331 struct hlist_node *walk, *node;
c6c8fea2
SE
332 struct hlist_head *head;
333 struct element_t *bucket;
334 struct orig_node *orig_node;
335 struct neigh_node *neigh_node;
336 int batman_count = 0;
337 int last_seen_secs;
338 int last_seen_msecs;
339 int i;
340
341 if ((!bat_priv->primary_if) ||
342 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
343 if (!bat_priv->primary_if)
344 return seq_printf(seq, "BATMAN mesh %s disabled - "
345 "please specify interfaces to enable it\n",
346 net_dev->name);
347
348 return seq_printf(seq, "BATMAN mesh %s "
349 "disabled - primary interface not active\n",
350 net_dev->name);
351 }
352
353 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
354 SOURCE_VERSION, REVISION_VERSION_STR,
355 bat_priv->primary_if->net_dev->name,
356 bat_priv->primary_if->net_dev->dev_addr, net_dev->name);
357 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
358 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
359 "outgoingIF", "Potential nexthops");
360
361 spin_lock_bh(&bat_priv->orig_hash_lock);
362
363 for (i = 0; i < hash->size; i++) {
364 head = &hash->table[i];
365
366 hlist_for_each_entry(bucket, walk, head, hlist) {
367 orig_node = bucket->data;
368
369 if (!orig_node->router)
370 continue;
371
372 if (orig_node->router->tq_avg == 0)
373 continue;
374
375 last_seen_secs = jiffies_to_msecs(jiffies -
376 orig_node->last_valid) / 1000;
377 last_seen_msecs = jiffies_to_msecs(jiffies -
378 orig_node->last_valid) % 1000;
379
380 neigh_node = orig_node->router;
381 seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
382 orig_node->orig, last_seen_secs,
383 last_seen_msecs, neigh_node->tq_avg,
384 neigh_node->addr,
385 neigh_node->if_incoming->net_dev->name);
386
9591a79f
ML
387 hlist_for_each_entry(neigh_node, node,
388 &orig_node->neigh_list, list) {
c6c8fea2
SE
389 seq_printf(seq, " %pM (%3i)", neigh_node->addr,
390 neigh_node->tq_avg);
391 }
392
393 seq_printf(seq, "\n");
394 batman_count++;
395 }
396 }
397
398 spin_unlock_bh(&bat_priv->orig_hash_lock);
399
400 if ((batman_count == 0))
401 seq_printf(seq, "No batman nodes in range ...\n");
402
403 return 0;
404}
405
406static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
407{
408 void *data_ptr;
409
410 data_ptr = kmalloc(max_if_num * sizeof(unsigned long) * NUM_WORDS,
411 GFP_ATOMIC);
412 if (!data_ptr) {
413 pr_err("Can't resize orig: out of memory\n");
414 return -1;
415 }
416
417 memcpy(data_ptr, orig_node->bcast_own,
418 (max_if_num - 1) * sizeof(unsigned long) * NUM_WORDS);
419 kfree(orig_node->bcast_own);
420 orig_node->bcast_own = data_ptr;
421
422 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
423 if (!data_ptr) {
424 pr_err("Can't resize orig: out of memory\n");
425 return -1;
426 }
427
428 memcpy(data_ptr, orig_node->bcast_own_sum,
429 (max_if_num - 1) * sizeof(uint8_t));
430 kfree(orig_node->bcast_own_sum);
431 orig_node->bcast_own_sum = data_ptr;
432
433 return 0;
434}
435
436int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
437{
438 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
439 struct hashtable_t *hash = bat_priv->orig_hash;
440 struct hlist_node *walk;
441 struct hlist_head *head;
442 struct element_t *bucket;
443 struct orig_node *orig_node;
444 int i;
445
446 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
447 * if_num */
448 spin_lock_bh(&bat_priv->orig_hash_lock);
449
450 for (i = 0; i < hash->size; i++) {
451 head = &hash->table[i];
452
453 hlist_for_each_entry(bucket, walk, head, hlist) {
454 orig_node = bucket->data;
455
456 if (orig_node_add_if(orig_node, max_if_num) == -1)
457 goto err;
458 }
459 }
460
461 spin_unlock_bh(&bat_priv->orig_hash_lock);
462 return 0;
463
464err:
465 spin_unlock_bh(&bat_priv->orig_hash_lock);
466 return -ENOMEM;
467}
468
469static int orig_node_del_if(struct orig_node *orig_node,
470 int max_if_num, int del_if_num)
471{
472 void *data_ptr = NULL;
473 int chunk_size;
474
475 /* last interface was removed */
476 if (max_if_num == 0)
477 goto free_bcast_own;
478
479 chunk_size = sizeof(unsigned long) * NUM_WORDS;
480 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
481 if (!data_ptr) {
482 pr_err("Can't resize orig: out of memory\n");
483 return -1;
484 }
485
486 /* copy first part */
487 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
488
489 /* copy second part */
490 memcpy(data_ptr + del_if_num * chunk_size,
491 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
492 (max_if_num - del_if_num) * chunk_size);
493
494free_bcast_own:
495 kfree(orig_node->bcast_own);
496 orig_node->bcast_own = data_ptr;
497
498 if (max_if_num == 0)
499 goto free_own_sum;
500
501 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
502 if (!data_ptr) {
503 pr_err("Can't resize orig: out of memory\n");
504 return -1;
505 }
506
507 memcpy(data_ptr, orig_node->bcast_own_sum,
508 del_if_num * sizeof(uint8_t));
509
510 memcpy(data_ptr + del_if_num * sizeof(uint8_t),
511 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
512 (max_if_num - del_if_num) * sizeof(uint8_t));
513
514free_own_sum:
515 kfree(orig_node->bcast_own_sum);
516 orig_node->bcast_own_sum = data_ptr;
517
518 return 0;
519}
520
521int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
522{
523 struct bat_priv *bat_priv = netdev_priv(batman_if->soft_iface);
524 struct hashtable_t *hash = bat_priv->orig_hash;
525 struct hlist_node *walk;
526 struct hlist_head *head;
527 struct element_t *bucket;
528 struct batman_if *batman_if_tmp;
529 struct orig_node *orig_node;
530 int i, ret;
531
532 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
533 * if_num */
534 spin_lock_bh(&bat_priv->orig_hash_lock);
535
536 for (i = 0; i < hash->size; i++) {
537 head = &hash->table[i];
538
539 hlist_for_each_entry(bucket, walk, head, hlist) {
540 orig_node = bucket->data;
541
542 ret = orig_node_del_if(orig_node, max_if_num,
543 batman_if->if_num);
544
545 if (ret == -1)
546 goto err;
547 }
548 }
549
550 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
551 rcu_read_lock();
552 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
553 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
554 continue;
555
556 if (batman_if == batman_if_tmp)
557 continue;
558
559 if (batman_if->soft_iface != batman_if_tmp->soft_iface)
560 continue;
561
562 if (batman_if_tmp->if_num > batman_if->if_num)
563 batman_if_tmp->if_num--;
564 }
565 rcu_read_unlock();
566
567 batman_if->if_num = -1;
568 spin_unlock_bh(&bat_priv->orig_hash_lock);
569 return 0;
570
571err:
572 spin_unlock_bh(&bat_priv->orig_hash_lock);
573 return -ENOMEM;
574}