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