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