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