]> bbs.cooldavid.org Git - net-next-2.6.git/blob - drivers/staging/batman-adv/originator.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[net-next-2.6.git] / drivers / staging / batman-adv / originator.c
1 /*
2  * Copyright (C) 2009-2010 B.A.T.M.A.N. contributors:
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 "hard-interface.h"
30
31 static DECLARE_DELAYED_WORK(purge_orig_wq, purge_orig);
32
33 static void start_purge_timer(void)
34 {
35         queue_delayed_work(bat_event_workqueue, &purge_orig_wq, 1 * HZ);
36 }
37
38 int originator_init(void)
39 {
40         unsigned long flags;
41         if (orig_hash)
42                 return 1;
43
44         spin_lock_irqsave(&orig_hash_lock, flags);
45         orig_hash = hash_new(128, compare_orig, choose_orig);
46
47         if (!orig_hash)
48                 goto err;
49
50         spin_unlock_irqrestore(&orig_hash_lock, flags);
51         start_purge_timer();
52         return 1;
53
54 err:
55         spin_unlock_irqrestore(&orig_hash_lock, flags);
56         return 0;
57 }
58
59 struct neigh_node *
60 create_neighbor(struct orig_node *orig_node, struct orig_node *orig_neigh_node,
61                 uint8_t *neigh, struct batman_if *if_incoming)
62 {
63         /* FIXME: each orig_node->batman_if will be attached to a softif */
64         struct bat_priv *bat_priv = netdev_priv(soft_device);
65         struct neigh_node *neigh_node;
66
67         bat_dbg(DBG_BATMAN, bat_priv,
68                 "Creating new last-hop neighbor of originator\n");
69
70         neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
71         if (!neigh_node)
72                 return NULL;
73
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
84 static void free_orig_node(void *data)
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
90         /* for all neighbors towards this originator ... */
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
105 void 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
120 /* this function finds or creates an originator entry for the given
121  * address if it does not exits */
122 struct orig_node *get_orig_node(uint8_t *addr)
123 {
124         /* FIXME: each batman_if will be attached to a softif */
125         struct bat_priv *bat_priv = netdev_priv(soft_device);
126         struct orig_node *orig_node;
127         struct hashtable_t *swaphash;
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
135         bat_dbg(DBG_BATMAN, bat_priv,
136                 "Creating new originator: %pM\n", addr);
137
138         orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
139         if (!orig_node)
140                 return NULL;
141
142         INIT_LIST_HEAD(&orig_node->neigh_list);
143
144         memcpy(orig_node->orig, addr, ETH_ALEN);
145         orig_node->router = NULL;
146         orig_node->hna_buff = NULL;
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);
151
152         size = bat_priv->num_ifaces * sizeof(TYPE_OF_WORD) * NUM_WORDS;
153
154         orig_node->bcast_own = kzalloc(size, GFP_ATOMIC);
155         if (!orig_node->bcast_own)
156                 goto free_orig_node;
157
158         size = bat_priv->num_ifaces * sizeof(uint8_t);
159         orig_node->bcast_own_sum = kzalloc(size, GFP_ATOMIC);
160         if (!orig_node->bcast_own_sum)
161                 goto free_bcast_own;
162
163         if (hash_add(orig_hash, orig_node) < 0)
164                 goto free_bcast_own_sum;
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)
170                         bat_err(soft_device,
171                                 "Couldn't resize orig hash table\n");
172                 else
173                         orig_hash = swaphash;
174         }
175
176         return orig_node;
177 free_bcast_own_sum:
178         kfree(orig_node->bcast_own_sum);
179 free_bcast_own:
180         kfree(orig_node->bcast_own);
181 free_orig_node:
182         kfree(orig_node);
183         return NULL;
184 }
185
186 static bool purge_orig_neighbors(struct orig_node *orig_node,
187                                  struct neigh_node **best_neigh_node)
188 {
189         /* FIXME: each orig_node->batman_if will be attached to a softif */
190         struct bat_priv *bat_priv = netdev_priv(soft_device);
191         struct list_head *list_pos, *list_pos_tmp;
192         struct neigh_node *neigh_node;
193         bool neigh_purged = false;
194
195         *best_neigh_node = NULL;
196
197         /* for all neighbors towards this originator ... */
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
201                 if ((time_after(jiffies,
202                         neigh_node->last_valid + PURGE_TIMEOUT * HZ)) ||
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)
208                                 bat_dbg(DBG_BATMAN, bat_priv,
209                                         "neighbor purge: originator %pM, "
210                                         "neighbor: %pM, iface: %s\n",
211                                         orig_node->orig, neigh_node->addr,
212                                         neigh_node->if_incoming->dev);
213                         else
214                                 bat_dbg(DBG_BATMAN, bat_priv,
215                                         "neighbor timeout: originator %pM, "
216                                         "neighbor: %pM, last_valid: %lu\n",
217                                         orig_node->orig, neigh_node->addr,
218                                         (neigh_node->last_valid / HZ));
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
232 static bool purge_orig_node(struct orig_node *orig_node)
233 {
234         /* FIXME: each batman_if will be attached to a softif */
235         struct bat_priv *bat_priv = netdev_priv(soft_device);
236         struct neigh_node *best_neigh_node;
237
238         if (time_after(jiffies,
239                 orig_node->last_valid + 2 * PURGE_TIMEOUT * HZ)) {
240
241                 bat_dbg(DBG_BATMAN, bat_priv,
242                         "Originator timeout: originator %pM, last_valid %lu\n",
243                         orig_node->orig, (orig_node->last_valid / HZ));
244                 return true;
245         } else {
246                 if (purge_orig_neighbors(orig_node, &best_neigh_node)) {
247                         update_routes(orig_node, best_neigh_node,
248                                       orig_node->hna_buff,
249                                       orig_node->hna_buff_len);
250                         /* update bonding candidates, we could have lost
251                          * some candidates. */
252                         update_bonding_candidates(bat_priv, orig_node);
253                 }
254         }
255
256         return false;
257 }
258
259 void purge_orig(struct work_struct *work)
260 {
261         HASHIT(hashit);
262         struct orig_node *orig_node;
263         unsigned long flags;
264
265         spin_lock_irqsave(&orig_hash_lock, flags);
266
267         /* for all origins... */
268         while (hash_iterate(orig_hash, &hashit)) {
269                 orig_node = hashit.bucket->data;
270                 if (purge_orig_node(orig_node)) {
271                         hash_remove_bucket(orig_hash, &hashit);
272                         free_orig_node(orig_node);
273                 }
274         }
275
276         spin_unlock_irqrestore(&orig_hash_lock, flags);
277
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();
282 }
283
284 int orig_seq_print_text(struct seq_file *seq, void *offset)
285 {
286         HASHIT(hashit);
287         struct net_device *net_dev = (struct net_device *)seq->private;
288         struct bat_priv *bat_priv = netdev_priv(net_dev);
289         struct orig_node *orig_node;
290         struct neigh_node *neigh_node;
291         int batman_count = 0;
292         int last_seen_secs;
293         int last_seen_msecs;
294         unsigned long flags;
295         char orig_str[ETH_STR_LEN], router_str[ETH_STR_LEN];
296
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 - "
301                                      "please specify interfaces to enable it\n",
302                                      net_dev->name);
303
304                 return seq_printf(seq, "BATMAN mesh %s "
305                                   "disabled - primary interface not active\n",
306                                   net_dev->name);
307         }
308
309         rcu_read_lock();
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,
312                    bat_priv->primary_if->dev, bat_priv->primary_if->addr_str,
313                    net_dev->name);
314         seq_printf(seq, "  %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
315                    "Originator", "last-seen", "#", TQ_MAX_VALUE, "Nexthop",
316                    "outgoingIF", "Potential nexthops");
317         rcu_read_unlock();
318
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
331                 addr_to_string(orig_str, orig_node->orig);
332                 addr_to_string(router_str, orig_node->router->addr);
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,
341                            orig_node->router->if_incoming->dev);
342
343                 list_for_each_entry(neigh_node, &orig_node->neigh_list, list) {
344                         addr_to_string(orig_str, neigh_node->addr);
345                         seq_printf(seq, " %17s (%3i)", orig_str,
346                                            neigh_node->tq_avg);
347                 }
348
349                 seq_printf(seq, "\n");
350                 batman_count++;
351         }
352
353         spin_unlock_irqrestore(&orig_hash_lock, flags);
354
355         if ((batman_count == 0))
356                 seq_printf(seq, "No batman nodes in range ...\n");
357
358         return 0;
359 }
360
361 static 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) {
368                 pr_err("Can't resize orig: out of memory\n");
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) {
379                 pr_err("Can't resize orig: out of memory\n");
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
391 int orig_hash_add_if(struct batman_if *batman_if, int max_if_num)
392 {
393         struct orig_node *orig_node;
394         unsigned long flags;
395         HASHIT(hashit);
396
397         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
398          * if_num */
399         spin_lock_irqsave(&orig_hash_lock, flags);
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
408         spin_unlock_irqrestore(&orig_hash_lock, flags);
409         return 0;
410
411 err:
412         spin_unlock_irqrestore(&orig_hash_lock, flags);
413         return -ENOMEM;
414 }
415
416 static 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) {
429                 pr_err("Can't resize orig: out of memory\n");
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
441 free_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) {
450                 pr_err("Can't resize orig: out of memory\n");
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
461 free_own_sum:
462         kfree(orig_node->bcast_own_sum);
463         orig_node->bcast_own_sum = data_ptr;
464
465         return 0;
466 }
467
468 int 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;
472         unsigned long flags;
473         HASHIT(hashit);
474         int ret;
475
476         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
477          * if_num */
478         spin_lock_irqsave(&orig_hash_lock, flags);
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;
505         spin_unlock_irqrestore(&orig_hash_lock, flags);
506         return 0;
507
508 err:
509         spin_unlock_irqrestore(&orig_hash_lock, flags);
510         return -ENOMEM;
511 }