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