]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/staging/batman-adv/originator.c
Merge branch 'for-linus' of git://git.infradead.org/users/eparis/notify
[net-next-2.6.git] / drivers / staging / batman-adv / originator.c
CommitLineData
8a2e042c 1/*
9b6d10b7 2 * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors:
8a2e042c
AL
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"
14741240 29#include "hard-interface.h"
8a2e042c
AL
30
31static DECLARE_DELAYED_WORK(purge_orig_wq, purge_orig);
32
33static void start_purge_timer(void)
34{
35 queue_delayed_work(bat_event_workqueue, &purge_orig_wq, 1 * HZ);
36}
37
38int originator_init(void)
39{
e7017195 40 unsigned long flags;
8a2e042c
AL
41 if (orig_hash)
42 return 1;
43
e7017195 44 spin_lock_irqsave(&orig_hash_lock, flags);
8a2e042c
AL
45 orig_hash = hash_new(128, compare_orig, choose_orig);
46
47 if (!orig_hash)
48 goto err;
49
e7017195 50 spin_unlock_irqrestore(&orig_hash_lock, flags);
8a2e042c
AL
51 start_purge_timer();
52 return 1;
53
54err:
e7017195 55 spin_unlock_irqrestore(&orig_hash_lock, flags);
8a2e042c
AL
56 return 0;
57}
58
8a2e042c
AL
59struct neigh_node *
60create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
61 uint8_t *neigh, struct batman_if *if_incoming)
62{
84ec0864
ML
63 /* FIXME: each orig_node->batman_if will be attached to a softif */
64 struct bat_priv *bat_priv = netdev_priv(soft_device);
8a2e042c
AL
65 struct neigh_node *neigh_node;
66
84ec0864
ML
67 bat_dbg(DBG_BATMAN, bat_priv,
68 "Creating new last-hop neighbor of originator\n");
8a2e042c 69
abad5446 70 neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
c4bf05d3
SW
71 if (!neigh_node)
72 return NULL;
73
8a2e042c
AL
74 INIT_LIST_HEAD(&neigh_node->list);
75
76 memcpy(neigh_node->addr, neigh, ETH_ALEN);
77 neigh_node->orig_node = orig_neigh_node;
78 neigh_node->if_incoming = if_incoming;
79
80 list_add_tail(&neigh_node->list, &orig_node->neigh_list);
81 return neigh_node;
82}
83
42fa1b92 84static void free_orig_node(void *data)
8a2e042c
AL
85{
86 struct list_head *list_pos, *list_pos_tmp;
87 struct neigh_node *neigh_node;
88 struct orig_node *orig_node = (struct orig_node *)data;
89
5ea84fa3 90 /* for all neighbors towards this originator ... */
8a2e042c
AL
91 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
92 neigh_node = list_entry(list_pos, struct neigh_node, list);
93
94 list_del(list_pos);
95 kfree(neigh_node);
96 }
97
98 hna_global_del_orig(orig_node, "originator timed out");
99
100 kfree(orig_node->bcast_own);
101 kfree(orig_node->bcast_own_sum);
102 kfree(orig_node);
103}
104
42fa1b92
SE
105void originator_free(void)
106{
107 unsigned long flags;
108
109 if (!orig_hash)
110 return;
111
112 cancel_delayed_work_sync(&purge_orig_wq);
113
114 spin_lock_irqsave(&orig_hash_lock, flags);
115 hash_delete(orig_hash, free_orig_node);
116 orig_hash = NULL;
117 spin_unlock_irqrestore(&orig_hash_lock, flags);
118}
119
8a2e042c
AL
120/* this function finds or creates an originator entry for the given
121 * address if it does not exits */
122struct orig_node *get_orig_node(uint8_t *addr)
123{
208e13e4
ML
124 /* FIXME: each batman_if will be attached to a softif */
125 struct bat_priv *bat_priv = netdev_priv(soft_device);
8a2e042c
AL
126 struct orig_node *orig_node;
127 struct hashtable_t *swaphash;
8a2e042c
AL
128 int size;
129
130 orig_node = ((struct orig_node *)hash_find(orig_hash, addr));
131
132 if (orig_node != NULL)
133 return orig_node;
134
84ec0864
ML
135 bat_dbg(DBG_BATMAN, bat_priv,
136 "Creating new originator: %pM\n", addr);
8a2e042c 137
abad5446 138 orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
c4bf05d3
SW
139 if (!orig_node)
140 return NULL;
141
8a2e042c
AL
142 INIT_LIST_HEAD(&orig_node->neigh_list);
143
144 memcpy(orig_node->orig, addr, ETH_ALEN);
145 orig_node->router = NULL;
8a2e042c 146 orig_node->hna_buff = NULL;
6d45d8df
SE
147 orig_node->bcast_seqno_reset = jiffies - 1
148 - msecs_to_jiffies(RESET_PROTECTION_MS);
149 orig_node->batman_seqno_reset = jiffies - 1
150 - msecs_to_jiffies(RESET_PROTECTION_MS);
8a2e042c 151
208e13e4 152 size = bat_priv->num_ifaces * sizeof(TYPE_OF_WORD) * NUM_WORDS;
8a2e042c 153
abad5446 154 orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
c4bf05d3
SW
155 if (!orig_node->bcast_own)
156 goto free_orig_node;
157
208e13e4 158 size = bat_priv->num_ifaces * sizeof(uint8_t);
abad5446 159 orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
c4bf05d3
SW
160 if (!orig_node->bcast_own_sum)
161 goto free_bcast_own;
162
c4bf05d3
SW
163 if (hash_add(orig_hash, orig_node) < 0)
164 goto free_bcast_own_sum;
8a2e042c
AL
165
166 if (orig_hash->elements * 4 > orig_hash->size) {
167 swaphash = hash_resize(orig_hash, orig_hash->size * 2);
168
169 if (swaphash == NULL)
c1641862
SE
170 bat_err(soft_device,
171 "Couldn't resize orig hash table\n");
8a2e042c
AL
172 else
173 orig_hash = swaphash;
174 }
175
176 return orig_node;
c4bf05d3
SW
177free_bcast_own_sum:
178 kfree(orig_node->bcast_own_sum);
179free_bcast_own:
180 kfree(orig_node->bcast_own);
181free_orig_node:
182 kfree(orig_node);
183 return NULL;
8a2e042c
AL
184}
185
5ea84fa3 186static bool purge_orig_neighbors(struct orig_node *orig_node,
8a2e042c
AL
187 struct neigh_node **best_neigh_node)
188{
84ec0864
ML
189 /* FIXME: each orig_node->batman_if will be attached to a softif */
190 struct bat_priv *bat_priv = netdev_priv(soft_device);
8a2e042c 191 struct list_head *list_pos, *list_pos_tmp;
8a2e042c
AL
192 struct neigh_node *neigh_node;
193 bool neigh_purged = false;
194
195 *best_neigh_node = NULL;
196
5ea84fa3 197 /* for all neighbors towards this originator ... */
8a2e042c
AL
198 list_for_each_safe(list_pos, list_pos_tmp, &orig_node->neigh_list) {
199 neigh_node = list_entry(list_pos, struct neigh_node, list);
200
208e13e4 201 if ((time_after(jiffies,
b6be4535 202 neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
208e13e4
ML
203 (neigh_node->if_incoming->if_status ==
204 IF_TO_BE_REMOVED)) {
205
206 if (neigh_node->if_incoming->if_status ==
207 IF_TO_BE_REMOVED)
84ec0864 208 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df
SE
209 "neighbor purge: originator %pM, "
210 "neighbor: %pM, iface: %s\n",
208e13e4
ML
211 orig_node->orig, neigh_node->addr,
212 neigh_node->if_incoming->dev);
213 else
84ec0864 214 bat_dbg(DBG_BATMAN, bat_priv,
6d45d8df
SE
215 "neighbor timeout: originator %pM, "
216 "neighbor: %pM, last_valid: %lu\n",
208e13e4
ML
217 orig_node->orig, neigh_node->addr,
218 (neigh_node->last_valid / HZ));
8a2e042c
AL
219
220 neigh_purged = true;
221 list_del(list_pos);
222 kfree(neigh_node);
223 } else {
224 if ((*best_neigh_node == NULL) ||
225 (neigh_node->tq_avg > (*best_neigh_node)->tq_avg))
226 *best_neigh_node = neigh_node;
227 }
228 }
229 return neigh_purged;
230}
231
8a2e042c
AL
232static bool purge_orig_node(struct orig_node *orig_node)
233{
e35fd5ec
SW
234 /* FIXME: each batman_if will be attached to a softif */
235 struct bat_priv *bat_priv = netdev_priv(soft_device);
8a2e042c 236 struct neigh_node *best_neigh_node;
8a2e042c
AL
237
238 if (time_after(jiffies,
b6be4535 239 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
8a2e042c 240
84ec0864 241 bat_dbg(DBG_BATMAN, bat_priv,
b9b27e4e
AL
242 "Originator timeout: originator %pM, last_valid %lu\n",
243 orig_node->orig, (orig_node->last_valid / HZ));
8a2e042c
AL
244 return true;
245 } else {
e35fd5ec 246 if (purge_orig_neighbors(orig_node, &best_neigh_node)) {
8a2e042c
AL
247 update_routes(orig_node, best_neigh_node,
248 orig_node->hna_buff,
249 orig_node->hna_buff_len);
e35fd5ec
SW
250 /* update bonding candidates, we could have lost
251 * some candidates. */
252 update_bonding_candidates(bat_priv, orig_node);
253 }
8a2e042c 254 }
14741240 255
8a2e042c
AL
256 return false;
257}
258
259void purge_orig(struct work_struct *work)
260{
b6c35976 261 HASHIT(hashit);
8a2e042c 262 struct orig_node *orig_node;
e7017195 263 unsigned long flags;
8a2e042c 264
e7017195 265 spin_lock_irqsave(&orig_hash_lock, flags);
8a2e042c
AL
266
267 /* for all origins... */
b6c35976
SW
268 while (hash_iterate(orig_hash, &hashit)) {
269 orig_node = hashit.bucket->data;
8a2e042c 270 if (purge_orig_node(orig_node)) {
b6c35976 271 hash_remove_bucket(orig_hash, &hashit);
8a2e042c
AL
272 free_orig_node(orig_node);
273 }
274 }
275
e7017195 276 spin_unlock_irqrestore(&orig_hash_lock, flags);
8a2e042c 277
208e13e4
ML
278 /* if work == NULL we were not called by the timer
279 * and thus do not need to re-arm the timer */
280 if (work)
281 start_purge_timer();
8a2e042c
AL
282}
283
4caecbc0 284int orig_seq_print_text(struct seq_file *seq, void *offset)
47fdf097
ML
285{
286 HASHIT(hashit);
4caecbc0 287 struct net_device *net_dev = (struct net_device *)seq->private;
208e13e4 288 struct bat_priv *bat_priv = netdev_priv(net_dev);
47fdf097
ML
289 struct orig_node *orig_node;
290 struct neigh_node *neigh_node;
4caecbc0 291 int batman_count = 0;
80f6c34b
LL
292 int last_seen_secs;
293 int last_seen_msecs;
47fdf097
ML
294 unsigned long flags;
295 char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
296
4caecbc0
SE
297 if ((!bat_priv->primary_if) ||
298 (bat_priv->primary_if->if_status != IF_ACTIVE)) {
299 if (!bat_priv->primary_if)
300 return seq_printf(seq, "BATMAN mesh %s disabled - "
6d45d8df
SE
301 "please specify interfaces to enable it\n",
302 net_dev->name);
14741240 303
4caecbc0
SE
304 return seq_printf(seq, "BATMAN mesh %s "
305 "disabled - primary interface not active\n",
306 net_dev->name);
14741240
ML
307 }
308
208e13e4 309 rcu_read_lock();
80f6c34b
LL
310 seq_printf(seq, "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%s (%s)]\n",
311 SOURCE_VERSION, REVISION_VERSION_STR,
208e13e4 312 bat_priv->primary_if->dev, bat_priv->primary_if->addr_str,
14741240 313 net_dev->name);
80f6c34b
LL
314 seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
315 "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
316 "outgoingIF", "Potential nexthops");
47fdf097
ML
317 rcu_read_unlock();
318
47fdf097
ML
319 spin_lock_irqsave(&orig_hash_lock, flags);
320
321 while (hash_iterate(orig_hash, &hashit)) {
322
323 orig_node = hashit.bucket->data;
324
325 if (!orig_node->router)
326 continue;
327
328 if (orig_node->router->tq_avg == 0)
329 continue;
330
47fdf097
ML
331 addr_to_string(orig_str, orig_node->orig);
332 addr_to_string(router_str, orig_node->router->addr);
80f6c34b
LL
333 last_seen_secs = jiffies_to_msecs(jiffies -
334 orig_node->last_valid) / 1000;
335 last_seen_msecs = jiffies_to_msecs(jiffies -
336 orig_node->last_valid) % 1000;
337
338 seq_printf(seq, "%-17s %4i.%03is (%3i) %17s [%10s]:",
339 orig_str, last_seen_secs, last_seen_msecs,
340 orig_node->router->tq_avg, router_str,
4caecbc0 341 orig_node->router->if_incoming->dev);
47fdf097
ML
342
343 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
344 addr_to_string(orig_str, neigh_node->addr);
4caecbc0 345 seq_printf(seq, " %17s (%3i)", orig_str,
47fdf097
ML
346 neigh_node->tq_avg);
347 }
348
4caecbc0 349 seq_printf(seq, "\n");
47fdf097 350 batman_count++;
47fdf097
ML
351 }
352
353 spin_unlock_irqrestore(&orig_hash_lock, flags);
354
4caecbc0
SE
355 if ((batman_count == 0))
356 seq_printf(seq, "No batman nodes in range ...\n");
47fdf097 357
4caecbc0 358 return 0;
47fdf097 359}
8a2e042c 360
208e13e4
ML
361static int orig_node_add_if(struct orig_node *orig_node, int max_if_num)
362{
363 void *data_ptr;
364
365 data_ptr = kmalloc(max_if_num * sizeof(TYPE_OF_WORD) * NUM_WORDS,
366 GFP_ATOMIC);
367 if (!data_ptr) {
c1641862 368 pr_err("Can't resize orig: out of memory\n");
208e13e4
ML
369 return -1;
370 }
371
372 memcpy(data_ptr, orig_node->bcast_own,
373 (max_if_num - 1) * sizeof(TYPE_OF_WORD) * NUM_WORDS);
374 kfree(orig_node->bcast_own);
375 orig_node->bcast_own = data_ptr;
376
377 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
378 if (!data_ptr) {
c1641862 379 pr_err("Can't resize orig: out of memory\n");
208e13e4
ML
380 return -1;
381 }
382
383 memcpy(data_ptr, orig_node->bcast_own_sum,
384 (max_if_num - 1) * sizeof(uint8_t));
385 kfree(orig_node->bcast_own_sum);
386 orig_node->bcast_own_sum = data_ptr;
387
388 return 0;
389}
390
391int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
392{
393 struct orig_node *orig_node;
9abc1023 394 unsigned long flags;
208e13e4
ML
395 HASHIT(hashit);
396
397 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
398 * if_num */
9abc1023 399 spin_lock_irqsave(&orig_hash_lock, flags);
208e13e4
ML
400
401 while (hash_iterate(orig_hash, &hashit)) {
402 orig_node = hashit.bucket->data;
403
404 if (orig_node_add_if(orig_node, max_if_num) == -1)
405 goto err;
406 }
407
9abc1023 408 spin_unlock_irqrestore(&orig_hash_lock, flags);
208e13e4
ML
409 return 0;
410
411err:
9abc1023 412 spin_unlock_irqrestore(&orig_hash_lock, flags);
208e13e4
ML
413 return -ENOMEM;
414}
415
416static int orig_node_del_if(struct orig_node *orig_node,
417 int max_if_num, int del_if_num)
418{
419 void *data_ptr = NULL;
420 int chunk_size;
421
422 /* last interface was removed */
423 if (max_if_num == 0)
424 goto free_bcast_own;
425
426 chunk_size = sizeof(TYPE_OF_WORD) * NUM_WORDS;
427 data_ptr = kmalloc(max_if_num * chunk_size, GFP_ATOMIC);
428 if (!data_ptr) {
c1641862 429 pr_err("Can't resize orig: out of memory\n");
208e13e4
ML
430 return -1;
431 }
432
433 /* copy first part */
434 memcpy(data_ptr, orig_node->bcast_own, del_if_num * chunk_size);
435
436 /* copy second part */
437 memcpy(data_ptr,
438 orig_node->bcast_own + ((del_if_num + 1) * chunk_size),
439 (max_if_num - del_if_num) * chunk_size);
440
441free_bcast_own:
442 kfree(orig_node->bcast_own);
443 orig_node->bcast_own = data_ptr;
444
445 if (max_if_num == 0)
446 goto free_own_sum;
447
448 data_ptr = kmalloc(max_if_num * sizeof(uint8_t), GFP_ATOMIC);
449 if (!data_ptr) {
c1641862 450 pr_err("Can't resize orig: out of memory\n");
208e13e4
ML
451 return -1;
452 }
453
454 memcpy(data_ptr, orig_node->bcast_own_sum,
455 del_if_num * sizeof(uint8_t));
456
457 memcpy(data_ptr,
458 orig_node->bcast_own_sum + ((del_if_num + 1) * sizeof(uint8_t)),
459 (max_if_num - del_if_num) * sizeof(uint8_t));
460
461free_own_sum:
462 kfree(orig_node->bcast_own_sum);
463 orig_node->bcast_own_sum = data_ptr;
464
465 return 0;
466}
467
468int orig_hash_del_if(struct batman_if *batman_if, int max_if_num)
469{
470 struct batman_if *batman_if_tmp;
471 struct orig_node *orig_node;
9abc1023 472 unsigned long flags;
208e13e4
ML
473 HASHIT(hashit);
474 int ret;
475
476 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
477 * if_num */
9abc1023 478 spin_lock_irqsave(&orig_hash_lock, flags);
208e13e4
ML
479
480 while (hash_iterate(orig_hash, &hashit)) {
481 orig_node = hashit.bucket->data;
482
483 ret = orig_node_del_if(orig_node, max_if_num,
484 batman_if->if_num);
485
486 if (ret == -1)
487 goto err;
488 }
489
490 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
491 rcu_read_lock();
492 list_for_each_entry_rcu(batman_if_tmp, &if_list, list) {
493 if (batman_if_tmp->if_status == IF_NOT_IN_USE)
494 continue;
495
496 if (batman_if == batman_if_tmp)
497 continue;
498
499 if (batman_if_tmp->if_num > batman_if->if_num)
500 batman_if_tmp->if_num--;
501 }
502 rcu_read_unlock();
503
504 batman_if->if_num = -1;
9abc1023 505 spin_unlock_irqrestore(&orig_hash_lock, flags);
208e13e4
ML
506 return 0;
507
508err:
9abc1023 509 spin_unlock_irqrestore(&orig_hash_lock, flags);
208e13e4
ML
510 return -ENOMEM;
511}