]> git.karo-electronics.de Git - karo-tx-linux.git/blob - net/batman-adv/originator.c
Merge remote-tracking branch 'regulator/for-next'
[karo-tx-linux.git] / net / batman-adv / originator.c
1 /* Copyright (C) 2009-2016  B.A.T.M.A.N. contributors:
2  *
3  * Marek Lindner, Simon Wunderlich
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 2 of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include "originator.h"
19 #include "main.h"
20
21 #include <linux/errno.h>
22 #include <linux/etherdevice.h>
23 #include <linux/fs.h>
24 #include <linux/jiffies.h>
25 #include <linux/kernel.h>
26 #include <linux/list.h>
27 #include <linux/lockdep.h>
28 #include <linux/netdevice.h>
29 #include <linux/rculist.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/workqueue.h>
34
35 #include "distributed-arp-table.h"
36 #include "fragmentation.h"
37 #include "gateway_client.h"
38 #include "hard-interface.h"
39 #include "hash.h"
40 #include "multicast.h"
41 #include "network-coding.h"
42 #include "routing.h"
43 #include "translation-table.h"
44
45 /* hash class keys */
46 static struct lock_class_key batadv_orig_hash_lock_class_key;
47
48 static void batadv_purge_orig(struct work_struct *work);
49
50 /**
51  * batadv_compare_orig - comparing function used in the originator hash table
52  * @node: node in the local table
53  * @data2: second object to compare the node to
54  *
55  * Return: 1 if they are the same originator
56  */
57 int batadv_compare_orig(const struct hlist_node *node, const void *data2)
58 {
59         const void *data1 = container_of(node, struct batadv_orig_node,
60                                          hash_entry);
61
62         return batadv_compare_eth(data1, data2);
63 }
64
65 /**
66  * batadv_orig_node_vlan_get - get an orig_node_vlan object
67  * @orig_node: the originator serving the VLAN
68  * @vid: the VLAN identifier
69  *
70  * Return: the vlan object identified by vid and belonging to orig_node or NULL
71  * if it does not exist.
72  */
73 struct batadv_orig_node_vlan *
74 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
75                           unsigned short vid)
76 {
77         struct batadv_orig_node_vlan *vlan = NULL, *tmp;
78
79         rcu_read_lock();
80         hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
81                 if (tmp->vid != vid)
82                         continue;
83
84                 if (!atomic_inc_not_zero(&tmp->refcount))
85                         continue;
86
87                 vlan = tmp;
88
89                 break;
90         }
91         rcu_read_unlock();
92
93         return vlan;
94 }
95
96 /**
97  * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
98  *  object
99  * @orig_node: the originator serving the VLAN
100  * @vid: the VLAN identifier
101  *
102  * Return: NULL in case of failure or the vlan object identified by vid and
103  * belonging to orig_node otherwise. The object is created and added to the list
104  * if it does not exist.
105  *
106  * The object is returned with refcounter increased by 1.
107  */
108 struct batadv_orig_node_vlan *
109 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
110                           unsigned short vid)
111 {
112         struct batadv_orig_node_vlan *vlan;
113
114         spin_lock_bh(&orig_node->vlan_list_lock);
115
116         /* first look if an object for this vid already exists */
117         vlan = batadv_orig_node_vlan_get(orig_node, vid);
118         if (vlan)
119                 goto out;
120
121         vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
122         if (!vlan)
123                 goto out;
124
125         atomic_set(&vlan->refcount, 2);
126         vlan->vid = vid;
127
128         hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
129
130 out:
131         spin_unlock_bh(&orig_node->vlan_list_lock);
132
133         return vlan;
134 }
135
136 /**
137  * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
138  *  the originator-vlan object
139  * @orig_vlan: the originator-vlan object to release
140  */
141 void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
142 {
143         if (atomic_dec_and_test(&orig_vlan->refcount))
144                 kfree_rcu(orig_vlan, rcu);
145 }
146
147 int batadv_originator_init(struct batadv_priv *bat_priv)
148 {
149         if (bat_priv->orig_hash)
150                 return 0;
151
152         bat_priv->orig_hash = batadv_hash_new(1024);
153
154         if (!bat_priv->orig_hash)
155                 goto err;
156
157         batadv_hash_set_lock_class(bat_priv->orig_hash,
158                                    &batadv_orig_hash_lock_class_key);
159
160         INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
161         queue_delayed_work(batadv_event_workqueue,
162                            &bat_priv->orig_work,
163                            msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
164
165         return 0;
166
167 err:
168         return -ENOMEM;
169 }
170
171 /**
172  * batadv_neigh_ifinfo_release - release neigh_ifinfo from lists and queue for
173  *  free after rcu grace period
174  * @neigh_ifinfo: the neigh_ifinfo object to release
175  */
176 static void
177 batadv_neigh_ifinfo_release(struct batadv_neigh_ifinfo *neigh_ifinfo)
178 {
179         if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
180                 batadv_hardif_free_ref(neigh_ifinfo->if_outgoing);
181
182         kfree_rcu(neigh_ifinfo, rcu);
183 }
184
185 /**
186  * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly release
187  *  the neigh_ifinfo
188  * @neigh_ifinfo: the neigh_ifinfo object to release
189  */
190 void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
191 {
192         if (atomic_dec_and_test(&neigh_ifinfo->refcount))
193                 batadv_neigh_ifinfo_release(neigh_ifinfo);
194 }
195
196 /**
197  * batadv_hardif_neigh_release - release hardif neigh node from lists and
198  *  queue for free after rcu grace period
199  * @hardif_neigh: hardif neigh neighbor to free
200  */
201 static void
202 batadv_hardif_neigh_release(struct batadv_hardif_neigh_node *hardif_neigh)
203 {
204         spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
205         hlist_del_init_rcu(&hardif_neigh->list);
206         spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
207
208         batadv_hardif_free_ref(hardif_neigh->if_incoming);
209         kfree_rcu(hardif_neigh, rcu);
210 }
211
212 /**
213  * batadv_hardif_neigh_free_ref - decrement the hardif neighbors refcounter
214  *  and possibly release it
215  * @hardif_neigh: hardif neigh neighbor to free
216  */
217 void batadv_hardif_neigh_free_ref(struct batadv_hardif_neigh_node *hardif_neigh)
218 {
219         if (atomic_dec_and_test(&hardif_neigh->refcount))
220                 batadv_hardif_neigh_release(hardif_neigh);
221 }
222
223 /**
224  * batadv_neigh_node_release - release neigh_node from lists and queue for
225  *  free after rcu grace period
226  * @neigh_node: neigh neighbor to free
227  */
228 static void batadv_neigh_node_release(struct batadv_neigh_node *neigh_node)
229 {
230         struct hlist_node *node_tmp;
231         struct batadv_hardif_neigh_node *hardif_neigh;
232         struct batadv_neigh_ifinfo *neigh_ifinfo;
233         struct batadv_algo_ops *bao;
234
235         bao = neigh_node->orig_node->bat_priv->bat_algo_ops;
236
237         hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
238                                   &neigh_node->ifinfo_list, list) {
239                 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
240         }
241
242         hardif_neigh = batadv_hardif_neigh_get(neigh_node->if_incoming,
243                                                neigh_node->addr);
244         if (hardif_neigh) {
245                 /* batadv_hardif_neigh_get() increases refcount too */
246                 batadv_hardif_neigh_free_ref(hardif_neigh);
247                 batadv_hardif_neigh_free_ref(hardif_neigh);
248         }
249
250         if (bao->bat_neigh_free)
251                 bao->bat_neigh_free(neigh_node);
252
253         batadv_hardif_free_ref(neigh_node->if_incoming);
254
255         kfree_rcu(neigh_node, rcu);
256 }
257
258 /**
259  * batadv_neigh_node_free_ref - decrement the neighbors refcounter
260  *  and possibly release it
261  * @neigh_node: neigh neighbor to free
262  */
263 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
264 {
265         if (atomic_dec_and_test(&neigh_node->refcount))
266                 batadv_neigh_node_release(neigh_node);
267 }
268
269 /**
270  * batadv_orig_node_get_router - router to the originator depending on iface
271  * @orig_node: the orig node for the router
272  * @if_outgoing: the interface where the payload packet has been received or
273  *  the OGM should be sent to
274  *
275  * Return: the neighbor which should be router for this orig_node/iface.
276  *
277  * The object is returned with refcounter increased by 1.
278  */
279 struct batadv_neigh_node *
280 batadv_orig_router_get(struct batadv_orig_node *orig_node,
281                        const struct batadv_hard_iface *if_outgoing)
282 {
283         struct batadv_orig_ifinfo *orig_ifinfo;
284         struct batadv_neigh_node *router = NULL;
285
286         rcu_read_lock();
287         hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
288                 if (orig_ifinfo->if_outgoing != if_outgoing)
289                         continue;
290
291                 router = rcu_dereference(orig_ifinfo->router);
292                 break;
293         }
294
295         if (router && !atomic_inc_not_zero(&router->refcount))
296                 router = NULL;
297
298         rcu_read_unlock();
299         return router;
300 }
301
302 /**
303  * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
304  * @orig_node: the orig node to be queried
305  * @if_outgoing: the interface for which the ifinfo should be acquired
306  *
307  * Return: the requested orig_ifinfo or NULL if not found.
308  *
309  * The object is returned with refcounter increased by 1.
310  */
311 struct batadv_orig_ifinfo *
312 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
313                        struct batadv_hard_iface *if_outgoing)
314 {
315         struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
316
317         rcu_read_lock();
318         hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
319                                  list) {
320                 if (tmp->if_outgoing != if_outgoing)
321                         continue;
322
323                 if (!atomic_inc_not_zero(&tmp->refcount))
324                         continue;
325
326                 orig_ifinfo = tmp;
327                 break;
328         }
329         rcu_read_unlock();
330
331         return orig_ifinfo;
332 }
333
334 /**
335  * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
336  * @orig_node: the orig node to be queried
337  * @if_outgoing: the interface for which the ifinfo should be acquired
338  *
339  * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
340  * interface otherwise. The object is created and added to the list
341  * if it does not exist.
342  *
343  * The object is returned with refcounter increased by 1.
344  */
345 struct batadv_orig_ifinfo *
346 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
347                        struct batadv_hard_iface *if_outgoing)
348 {
349         struct batadv_orig_ifinfo *orig_ifinfo = NULL;
350         unsigned long reset_time;
351
352         spin_lock_bh(&orig_node->neigh_list_lock);
353
354         orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
355         if (orig_ifinfo)
356                 goto out;
357
358         orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
359         if (!orig_ifinfo)
360                 goto out;
361
362         if (if_outgoing != BATADV_IF_DEFAULT &&
363             !atomic_inc_not_zero(&if_outgoing->refcount)) {
364                 kfree(orig_ifinfo);
365                 orig_ifinfo = NULL;
366                 goto out;
367         }
368
369         reset_time = jiffies - 1;
370         reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
371         orig_ifinfo->batman_seqno_reset = reset_time;
372         orig_ifinfo->if_outgoing = if_outgoing;
373         INIT_HLIST_NODE(&orig_ifinfo->list);
374         atomic_set(&orig_ifinfo->refcount, 2);
375         hlist_add_head_rcu(&orig_ifinfo->list,
376                            &orig_node->ifinfo_list);
377 out:
378         spin_unlock_bh(&orig_node->neigh_list_lock);
379         return orig_ifinfo;
380 }
381
382 /**
383  * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
384  * @neigh: the neigh node to be queried
385  * @if_outgoing: the interface for which the ifinfo should be acquired
386  *
387  * The object is returned with refcounter increased by 1.
388  *
389  * Return: the requested neigh_ifinfo or NULL if not found
390  */
391 struct batadv_neigh_ifinfo *
392 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
393                         struct batadv_hard_iface *if_outgoing)
394 {
395         struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
396                                    *tmp_neigh_ifinfo;
397
398         rcu_read_lock();
399         hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
400                                  list) {
401                 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
402                         continue;
403
404                 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
405                         continue;
406
407                 neigh_ifinfo = tmp_neigh_ifinfo;
408                 break;
409         }
410         rcu_read_unlock();
411
412         return neigh_ifinfo;
413 }
414
415 /**
416  * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
417  * @neigh: the neigh node to be queried
418  * @if_outgoing: the interface for which the ifinfo should be acquired
419  *
420  * Return: NULL in case of failure or the neigh_ifinfo object for the
421  * if_outgoing interface otherwise. The object is created and added to the list
422  * if it does not exist.
423  *
424  * The object is returned with refcounter increased by 1.
425  */
426 struct batadv_neigh_ifinfo *
427 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
428                         struct batadv_hard_iface *if_outgoing)
429 {
430         struct batadv_neigh_ifinfo *neigh_ifinfo;
431
432         spin_lock_bh(&neigh->ifinfo_lock);
433
434         neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
435         if (neigh_ifinfo)
436                 goto out;
437
438         neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
439         if (!neigh_ifinfo)
440                 goto out;
441
442         if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
443                 kfree(neigh_ifinfo);
444                 neigh_ifinfo = NULL;
445                 goto out;
446         }
447
448         INIT_HLIST_NODE(&neigh_ifinfo->list);
449         atomic_set(&neigh_ifinfo->refcount, 2);
450         neigh_ifinfo->if_outgoing = if_outgoing;
451
452         hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
453
454 out:
455         spin_unlock_bh(&neigh->ifinfo_lock);
456
457         return neigh_ifinfo;
458 }
459
460 /**
461  * batadv_neigh_node_get - retrieve a neighbour from the list
462  * @orig_node: originator which the neighbour belongs to
463  * @hard_iface: the interface where this neighbour is connected to
464  * @addr: the address of the neighbour
465  *
466  * Looks for and possibly returns a neighbour belonging to this originator list
467  * which is connected through the provided hard interface.
468  *
469  * Return: neighbor when found. Othwerwise NULL
470  */
471 static struct batadv_neigh_node *
472 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
473                       const struct batadv_hard_iface *hard_iface,
474                       const u8 *addr)
475 {
476         struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
477
478         rcu_read_lock();
479         hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
480                 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
481                         continue;
482
483                 if (tmp_neigh_node->if_incoming != hard_iface)
484                         continue;
485
486                 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
487                         continue;
488
489                 res = tmp_neigh_node;
490                 break;
491         }
492         rcu_read_unlock();
493
494         return res;
495 }
496
497 /**
498  * batadv_hardif_neigh_create - create a hardif neighbour node
499  * @hard_iface: the interface this neighbour is connected to
500  * @neigh_addr: the interface address of the neighbour to retrieve
501  *
502  * Return: the hardif neighbour node if found or created or NULL otherwise.
503  */
504 static struct batadv_hardif_neigh_node *
505 batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
506                            const u8 *neigh_addr)
507 {
508         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
509         struct batadv_hardif_neigh_node *hardif_neigh = NULL;
510
511         spin_lock_bh(&hard_iface->neigh_list_lock);
512
513         /* check if neighbor hasn't been added in the meantime */
514         hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
515         if (hardif_neigh)
516                 goto out;
517
518         if (!atomic_inc_not_zero(&hard_iface->refcount))
519                 goto out;
520
521         hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
522         if (!hardif_neigh) {
523                 batadv_hardif_free_ref(hard_iface);
524                 goto out;
525         }
526
527         INIT_HLIST_NODE(&hardif_neigh->list);
528         ether_addr_copy(hardif_neigh->addr, neigh_addr);
529         hardif_neigh->if_incoming = hard_iface;
530         hardif_neigh->last_seen = jiffies;
531
532         atomic_set(&hardif_neigh->refcount, 1);
533
534         if (bat_priv->bat_algo_ops->bat_hardif_neigh_init)
535                 bat_priv->bat_algo_ops->bat_hardif_neigh_init(hardif_neigh);
536
537         hlist_add_head(&hardif_neigh->list, &hard_iface->neigh_list);
538
539 out:
540         spin_unlock_bh(&hard_iface->neigh_list_lock);
541         return hardif_neigh;
542 }
543
544 /**
545  * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
546  *  node
547  * @hard_iface: the interface this neighbour is connected to
548  * @neigh_addr: the interface address of the neighbour to retrieve
549  *
550  * Return: the hardif neighbour node if found or created or NULL otherwise.
551  */
552 static struct batadv_hardif_neigh_node *
553 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
554                                   const u8 *neigh_addr)
555 {
556         struct batadv_hardif_neigh_node *hardif_neigh = NULL;
557
558         /* first check without locking to avoid the overhead */
559         hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
560         if (hardif_neigh)
561                 return hardif_neigh;
562
563         return batadv_hardif_neigh_create(hard_iface, neigh_addr);
564 }
565
566 /**
567  * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
568  * @hard_iface: the interface where this neighbour is connected to
569  * @neigh_addr: the address of the neighbour
570  *
571  * Looks for and possibly returns a neighbour belonging to this hard interface.
572  *
573  * Return: neighbor when found. Othwerwise NULL
574  */
575 struct batadv_hardif_neigh_node *
576 batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
577                         const u8 *neigh_addr)
578 {
579         struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
580
581         rcu_read_lock();
582         hlist_for_each_entry_rcu(tmp_hardif_neigh,
583                                  &hard_iface->neigh_list, list) {
584                 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
585                         continue;
586
587                 if (!atomic_inc_not_zero(&tmp_hardif_neigh->refcount))
588                         continue;
589
590                 hardif_neigh = tmp_hardif_neigh;
591                 break;
592         }
593         rcu_read_unlock();
594
595         return hardif_neigh;
596 }
597
598 /**
599  * batadv_neigh_node_new - create and init a new neigh_node object
600  * @orig_node: originator object representing the neighbour
601  * @hard_iface: the interface where the neighbour is connected to
602  * @neigh_addr: the mac address of the neighbour interface
603  *
604  * Allocates a new neigh_node object and initialises all the generic fields.
605  *
606  * Return: neighbor when found. Othwerwise NULL
607  */
608 struct batadv_neigh_node *
609 batadv_neigh_node_new(struct batadv_orig_node *orig_node,
610                       struct batadv_hard_iface *hard_iface,
611                       const u8 *neigh_addr)
612 {
613         struct batadv_neigh_node *neigh_node;
614         struct batadv_hardif_neigh_node *hardif_neigh = NULL;
615
616         neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
617         if (neigh_node)
618                 goto out;
619
620         hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
621                                                          neigh_addr);
622         if (!hardif_neigh)
623                 goto out;
624
625         neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
626         if (!neigh_node)
627                 goto out;
628
629         if (!atomic_inc_not_zero(&hard_iface->refcount)) {
630                 kfree(neigh_node);
631                 neigh_node = NULL;
632                 goto out;
633         }
634
635         INIT_HLIST_NODE(&neigh_node->list);
636         INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
637         spin_lock_init(&neigh_node->ifinfo_lock);
638
639         ether_addr_copy(neigh_node->addr, neigh_addr);
640         neigh_node->if_incoming = hard_iface;
641         neigh_node->orig_node = orig_node;
642
643         /* extra reference for return */
644         atomic_set(&neigh_node->refcount, 2);
645
646         spin_lock_bh(&orig_node->neigh_list_lock);
647         hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
648         spin_unlock_bh(&orig_node->neigh_list_lock);
649
650         /* increment unique neighbor refcount */
651         atomic_inc(&hardif_neigh->refcount);
652
653         batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
654                    "Creating new neighbor %pM for orig_node %pM on interface %s\n",
655                    neigh_addr, orig_node->orig, hard_iface->net_dev->name);
656
657 out:
658         if (hardif_neigh)
659                 batadv_hardif_neigh_free_ref(hardif_neigh);
660         return neigh_node;
661 }
662
663 /**
664  * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
665  * @seq: neighbour table seq_file struct
666  * @offset: not used
667  *
668  * Return: always 0
669  */
670 int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
671 {
672         struct net_device *net_dev = (struct net_device *)seq->private;
673         struct batadv_priv *bat_priv = netdev_priv(net_dev);
674         struct batadv_hard_iface *primary_if;
675
676         primary_if = batadv_seq_print_text_primary_if_get(seq);
677         if (!primary_if)
678                 return 0;
679
680         seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
681                    BATADV_SOURCE_VERSION, primary_if->net_dev->name,
682                    primary_if->net_dev->dev_addr, net_dev->name,
683                    bat_priv->bat_algo_ops->name);
684
685         batadv_hardif_free_ref(primary_if);
686
687         if (!bat_priv->bat_algo_ops->bat_neigh_print) {
688                 seq_puts(seq,
689                          "No printing function for this routing protocol\n");
690                 return 0;
691         }
692
693         bat_priv->bat_algo_ops->bat_neigh_print(bat_priv, seq);
694         return 0;
695 }
696
697 /**
698  * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
699  *  free after rcu grace period
700  * @orig_ifinfo: the orig_ifinfo object to release
701  */
702 static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo *orig_ifinfo)
703 {
704         struct batadv_neigh_node *router;
705
706         if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
707                 batadv_hardif_free_ref(orig_ifinfo->if_outgoing);
708
709         /* this is the last reference to this object */
710         router = rcu_dereference_protected(orig_ifinfo->router, true);
711         if (router)
712                 batadv_neigh_node_free_ref(router);
713
714         kfree_rcu(orig_ifinfo, rcu);
715 }
716
717 /**
718  * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
719  *  the orig_ifinfo
720  * @orig_ifinfo: the orig_ifinfo object to release
721  */
722 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
723 {
724         if (atomic_dec_and_test(&orig_ifinfo->refcount))
725                 batadv_orig_ifinfo_release(orig_ifinfo);
726 }
727
728 /**
729  * batadv_orig_node_free_rcu - free the orig_node
730  * @rcu: rcu pointer of the orig_node
731  */
732 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
733 {
734         struct batadv_orig_node *orig_node;
735
736         orig_node = container_of(rcu, struct batadv_orig_node, rcu);
737
738         batadv_mcast_purge_orig(orig_node);
739
740         batadv_frag_purge_orig(orig_node, NULL);
741
742         if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
743                 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
744
745         kfree(orig_node->tt_buff);
746         kfree(orig_node);
747 }
748
749 /**
750  * batadv_orig_node_release - release orig_node from lists and queue for
751  *  free after rcu grace period
752  * @orig_node: the orig node to free
753  */
754 static void batadv_orig_node_release(struct batadv_orig_node *orig_node)
755 {
756         struct hlist_node *node_tmp;
757         struct batadv_neigh_node *neigh_node;
758         struct batadv_orig_ifinfo *orig_ifinfo;
759
760         spin_lock_bh(&orig_node->neigh_list_lock);
761
762         /* for all neighbors towards this originator ... */
763         hlist_for_each_entry_safe(neigh_node, node_tmp,
764                                   &orig_node->neigh_list, list) {
765                 hlist_del_rcu(&neigh_node->list);
766                 batadv_neigh_node_free_ref(neigh_node);
767         }
768
769         hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
770                                   &orig_node->ifinfo_list, list) {
771                 hlist_del_rcu(&orig_ifinfo->list);
772                 batadv_orig_ifinfo_free_ref(orig_ifinfo);
773         }
774         spin_unlock_bh(&orig_node->neigh_list_lock);
775
776         /* Free nc_nodes */
777         batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
778
779         call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
780 }
781
782 /**
783  * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
784  *  release it
785  * @orig_node: the orig node to free
786  */
787 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
788 {
789         if (atomic_dec_and_test(&orig_node->refcount))
790                 batadv_orig_node_release(orig_node);
791 }
792
793 void batadv_originator_free(struct batadv_priv *bat_priv)
794 {
795         struct batadv_hashtable *hash = bat_priv->orig_hash;
796         struct hlist_node *node_tmp;
797         struct hlist_head *head;
798         spinlock_t *list_lock; /* spinlock to protect write access */
799         struct batadv_orig_node *orig_node;
800         u32 i;
801
802         if (!hash)
803                 return;
804
805         cancel_delayed_work_sync(&bat_priv->orig_work);
806
807         bat_priv->orig_hash = NULL;
808
809         for (i = 0; i < hash->size; i++) {
810                 head = &hash->table[i];
811                 list_lock = &hash->list_locks[i];
812
813                 spin_lock_bh(list_lock);
814                 hlist_for_each_entry_safe(orig_node, node_tmp,
815                                           head, hash_entry) {
816                         hlist_del_rcu(&orig_node->hash_entry);
817                         batadv_orig_node_free_ref(orig_node);
818                 }
819                 spin_unlock_bh(list_lock);
820         }
821
822         batadv_hash_destroy(hash);
823 }
824
825 /**
826  * batadv_orig_node_new - creates a new orig_node
827  * @bat_priv: the bat priv with all the soft interface information
828  * @addr: the mac address of the originator
829  *
830  * Creates a new originator object and initialise all the generic fields.
831  * The new object is not added to the originator list.
832  *
833  * Return: the newly created object or NULL on failure.
834  */
835 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
836                                               const u8 *addr)
837 {
838         struct batadv_orig_node *orig_node;
839         struct batadv_orig_node_vlan *vlan;
840         unsigned long reset_time;
841         int i;
842
843         batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
844                    "Creating new originator: %pM\n", addr);
845
846         orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
847         if (!orig_node)
848                 return NULL;
849
850         INIT_HLIST_HEAD(&orig_node->neigh_list);
851         INIT_HLIST_HEAD(&orig_node->vlan_list);
852         INIT_HLIST_HEAD(&orig_node->ifinfo_list);
853         spin_lock_init(&orig_node->bcast_seqno_lock);
854         spin_lock_init(&orig_node->neigh_list_lock);
855         spin_lock_init(&orig_node->tt_buff_lock);
856         spin_lock_init(&orig_node->tt_lock);
857         spin_lock_init(&orig_node->vlan_list_lock);
858
859         batadv_nc_init_orig(orig_node);
860
861         /* extra reference for return */
862         atomic_set(&orig_node->refcount, 2);
863
864         orig_node->bat_priv = bat_priv;
865         ether_addr_copy(orig_node->orig, addr);
866         batadv_dat_init_orig_node_addr(orig_node);
867         atomic_set(&orig_node->last_ttvn, 0);
868         orig_node->tt_buff = NULL;
869         orig_node->tt_buff_len = 0;
870         orig_node->last_seen = jiffies;
871         reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
872         orig_node->bcast_seqno_reset = reset_time;
873
874 #ifdef CONFIG_BATMAN_ADV_MCAST
875         orig_node->mcast_flags = BATADV_NO_FLAGS;
876         INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
877         INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
878         INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
879         spin_lock_init(&orig_node->mcast_handler_lock);
880 #endif
881
882         /* create a vlan object for the "untagged" LAN */
883         vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
884         if (!vlan)
885                 goto free_orig_node;
886         /* batadv_orig_node_vlan_new() increases the refcounter.
887          * Immediately release vlan since it is not needed anymore in this
888          * context
889          */
890         batadv_orig_node_vlan_free_ref(vlan);
891
892         for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
893                 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
894                 spin_lock_init(&orig_node->fragments[i].lock);
895                 orig_node->fragments[i].size = 0;
896         }
897
898         return orig_node;
899 free_orig_node:
900         kfree(orig_node);
901         return NULL;
902 }
903
904 /**
905  * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
906  * @bat_priv: the bat priv with all the soft interface information
907  * @neigh: orig node which is to be checked
908  */
909 static void
910 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
911                           struct batadv_neigh_node *neigh)
912 {
913         struct batadv_neigh_ifinfo *neigh_ifinfo;
914         struct batadv_hard_iface *if_outgoing;
915         struct hlist_node *node_tmp;
916
917         spin_lock_bh(&neigh->ifinfo_lock);
918
919         /* for all ifinfo objects for this neighinator */
920         hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
921                                   &neigh->ifinfo_list, list) {
922                 if_outgoing = neigh_ifinfo->if_outgoing;
923
924                 /* always keep the default interface */
925                 if (if_outgoing == BATADV_IF_DEFAULT)
926                         continue;
927
928                 /* don't purge if the interface is not (going) down */
929                 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
930                     (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
931                     (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
932                         continue;
933
934                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
935                            "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
936                            neigh->addr, if_outgoing->net_dev->name);
937
938                 hlist_del_rcu(&neigh_ifinfo->list);
939                 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
940         }
941
942         spin_unlock_bh(&neigh->ifinfo_lock);
943 }
944
945 /**
946  * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
947  * @bat_priv: the bat priv with all the soft interface information
948  * @orig_node: orig node which is to be checked
949  *
950  * Return: true if any ifinfo entry was purged, false otherwise.
951  */
952 static bool
953 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
954                          struct batadv_orig_node *orig_node)
955 {
956         struct batadv_orig_ifinfo *orig_ifinfo;
957         struct batadv_hard_iface *if_outgoing;
958         struct hlist_node *node_tmp;
959         bool ifinfo_purged = false;
960
961         spin_lock_bh(&orig_node->neigh_list_lock);
962
963         /* for all ifinfo objects for this originator */
964         hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
965                                   &orig_node->ifinfo_list, list) {
966                 if_outgoing = orig_ifinfo->if_outgoing;
967
968                 /* always keep the default interface */
969                 if (if_outgoing == BATADV_IF_DEFAULT)
970                         continue;
971
972                 /* don't purge if the interface is not (going) down */
973                 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
974                     (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
975                     (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
976                         continue;
977
978                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
979                            "router/ifinfo purge: originator %pM, iface: %s\n",
980                            orig_node->orig, if_outgoing->net_dev->name);
981
982                 ifinfo_purged = true;
983
984                 hlist_del_rcu(&orig_ifinfo->list);
985                 batadv_orig_ifinfo_free_ref(orig_ifinfo);
986                 if (orig_node->last_bonding_candidate == orig_ifinfo) {
987                         orig_node->last_bonding_candidate = NULL;
988                         batadv_orig_ifinfo_free_ref(orig_ifinfo);
989                 }
990         }
991
992         spin_unlock_bh(&orig_node->neigh_list_lock);
993
994         return ifinfo_purged;
995 }
996
997 /**
998  * batadv_purge_orig_neighbors - purges neighbors from originator
999  * @bat_priv: the bat priv with all the soft interface information
1000  * @orig_node: orig node which is to be checked
1001  *
1002  * Return: true if any neighbor was purged, false otherwise
1003  */
1004 static bool
1005 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
1006                             struct batadv_orig_node *orig_node)
1007 {
1008         struct hlist_node *node_tmp;
1009         struct batadv_neigh_node *neigh_node;
1010         bool neigh_purged = false;
1011         unsigned long last_seen;
1012         struct batadv_hard_iface *if_incoming;
1013
1014         spin_lock_bh(&orig_node->neigh_list_lock);
1015
1016         /* for all neighbors towards this originator ... */
1017         hlist_for_each_entry_safe(neigh_node, node_tmp,
1018                                   &orig_node->neigh_list, list) {
1019                 last_seen = neigh_node->last_seen;
1020                 if_incoming = neigh_node->if_incoming;
1021
1022                 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
1023                     (if_incoming->if_status == BATADV_IF_INACTIVE) ||
1024                     (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1025                     (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
1026                         if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
1027                             (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1028                             (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
1029                                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1030                                            "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1031                                            orig_node->orig, neigh_node->addr,
1032                                            if_incoming->net_dev->name);
1033                         else
1034                                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1035                                            "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1036                                            orig_node->orig, neigh_node->addr,
1037                                            jiffies_to_msecs(last_seen));
1038
1039                         neigh_purged = true;
1040
1041                         hlist_del_rcu(&neigh_node->list);
1042                         batadv_neigh_node_free_ref(neigh_node);
1043                 } else {
1044                         /* only necessary if not the whole neighbor is to be
1045                          * deleted, but some interface has been removed.
1046                          */
1047                         batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
1048                 }
1049         }
1050
1051         spin_unlock_bh(&orig_node->neigh_list_lock);
1052         return neigh_purged;
1053 }
1054
1055 /**
1056  * batadv_find_best_neighbor - finds the best neighbor after purging
1057  * @bat_priv: the bat priv with all the soft interface information
1058  * @orig_node: orig node which is to be checked
1059  * @if_outgoing: the interface for which the metric should be compared
1060  *
1061  * Return: the current best neighbor, with refcount increased.
1062  */
1063 static struct batadv_neigh_node *
1064 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1065                           struct batadv_orig_node *orig_node,
1066                           struct batadv_hard_iface *if_outgoing)
1067 {
1068         struct batadv_neigh_node *best = NULL, *neigh;
1069         struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1070
1071         rcu_read_lock();
1072         hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1073                 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
1074                                                 best, if_outgoing) <= 0))
1075                         continue;
1076
1077                 if (!atomic_inc_not_zero(&neigh->refcount))
1078                         continue;
1079
1080                 if (best)
1081                         batadv_neigh_node_free_ref(best);
1082
1083                 best = neigh;
1084         }
1085         rcu_read_unlock();
1086
1087         return best;
1088 }
1089
1090 /**
1091  * batadv_purge_orig_node - purges obsolete information from an orig_node
1092  * @bat_priv: the bat priv with all the soft interface information
1093  * @orig_node: orig node which is to be checked
1094  *
1095  * This function checks if the orig_node or substructures of it have become
1096  * obsolete, and purges this information if that's the case.
1097  *
1098  * Return: true if the orig_node is to be removed, false otherwise.
1099  */
1100 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1101                                    struct batadv_orig_node *orig_node)
1102 {
1103         struct batadv_neigh_node *best_neigh_node;
1104         struct batadv_hard_iface *hard_iface;
1105         bool changed_ifinfo, changed_neigh;
1106
1107         if (batadv_has_timed_out(orig_node->last_seen,
1108                                  2 * BATADV_PURGE_TIMEOUT)) {
1109                 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1110                            "Originator timeout: originator %pM, last_seen %u\n",
1111                            orig_node->orig,
1112                            jiffies_to_msecs(orig_node->last_seen));
1113                 return true;
1114         }
1115         changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1116         changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
1117
1118         if (!changed_ifinfo && !changed_neigh)
1119                 return false;
1120
1121         /* first for NULL ... */
1122         best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1123                                                     BATADV_IF_DEFAULT);
1124         batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1125                             best_neigh_node);
1126         if (best_neigh_node)
1127                 batadv_neigh_node_free_ref(best_neigh_node);
1128
1129         /* ... then for all other interfaces. */
1130         rcu_read_lock();
1131         list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1132                 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1133                         continue;
1134
1135                 if (hard_iface->soft_iface != bat_priv->soft_iface)
1136                         continue;
1137
1138                 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1139                                                             orig_node,
1140                                                             hard_iface);
1141                 batadv_update_route(bat_priv, orig_node, hard_iface,
1142                                     best_neigh_node);
1143                 if (best_neigh_node)
1144                         batadv_neigh_node_free_ref(best_neigh_node);
1145         }
1146         rcu_read_unlock();
1147
1148         return false;
1149 }
1150
1151 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
1152 {
1153         struct batadv_hashtable *hash = bat_priv->orig_hash;
1154         struct hlist_node *node_tmp;
1155         struct hlist_head *head;
1156         spinlock_t *list_lock; /* spinlock to protect write access */
1157         struct batadv_orig_node *orig_node;
1158         u32 i;
1159
1160         if (!hash)
1161                 return;
1162
1163         /* for all origins... */
1164         for (i = 0; i < hash->size; i++) {
1165                 head = &hash->table[i];
1166                 list_lock = &hash->list_locks[i];
1167
1168                 spin_lock_bh(list_lock);
1169                 hlist_for_each_entry_safe(orig_node, node_tmp,
1170                                           head, hash_entry) {
1171                         if (batadv_purge_orig_node(bat_priv, orig_node)) {
1172                                 batadv_gw_node_delete(bat_priv, orig_node);
1173                                 hlist_del_rcu(&orig_node->hash_entry);
1174                                 batadv_tt_global_del_orig(orig_node->bat_priv,
1175                                                           orig_node, -1,
1176                                                           "originator timed out");
1177                                 batadv_orig_node_free_ref(orig_node);
1178                                 continue;
1179                         }
1180
1181                         batadv_frag_purge_orig(orig_node,
1182                                                batadv_frag_check_entry);
1183                 }
1184                 spin_unlock_bh(list_lock);
1185         }
1186
1187         batadv_gw_election(bat_priv);
1188 }
1189
1190 static void batadv_purge_orig(struct work_struct *work)
1191 {
1192         struct delayed_work *delayed_work;
1193         struct batadv_priv *bat_priv;
1194
1195         delayed_work = container_of(work, struct delayed_work, work);
1196         bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1197         _batadv_purge_orig(bat_priv);
1198         queue_delayed_work(batadv_event_workqueue,
1199                            &bat_priv->orig_work,
1200                            msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1201 }
1202
1203 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1204 {
1205         _batadv_purge_orig(bat_priv);
1206 }
1207
1208 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1209 {
1210         struct net_device *net_dev = (struct net_device *)seq->private;
1211         struct batadv_priv *bat_priv = netdev_priv(net_dev);
1212         struct batadv_hard_iface *primary_if;
1213
1214         primary_if = batadv_seq_print_text_primary_if_get(seq);
1215         if (!primary_if)
1216                 return 0;
1217
1218         seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1219                    BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1220                    primary_if->net_dev->dev_addr, net_dev->name,
1221                    bat_priv->bat_algo_ops->name);
1222
1223         batadv_hardif_free_ref(primary_if);
1224
1225         if (!bat_priv->bat_algo_ops->bat_orig_print) {
1226                 seq_puts(seq,
1227                          "No printing function for this routing protocol\n");
1228                 return 0;
1229         }
1230
1231         bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1232                                                BATADV_IF_DEFAULT);
1233
1234         return 0;
1235 }
1236
1237 /**
1238  * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1239  *  outgoing interface
1240  * @seq: debugfs table seq_file struct
1241  * @offset: not used
1242  *
1243  * Return: 0
1244  */
1245 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1246 {
1247         struct net_device *net_dev = (struct net_device *)seq->private;
1248         struct batadv_hard_iface *hard_iface;
1249         struct batadv_priv *bat_priv;
1250
1251         hard_iface = batadv_hardif_get_by_netdev(net_dev);
1252
1253         if (!hard_iface || !hard_iface->soft_iface) {
1254                 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1255                 goto out;
1256         }
1257
1258         bat_priv = netdev_priv(hard_iface->soft_iface);
1259         if (!bat_priv->bat_algo_ops->bat_orig_print) {
1260                 seq_puts(seq,
1261                          "No printing function for this routing protocol\n");
1262                 goto out;
1263         }
1264
1265         if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1266                 seq_puts(seq, "Interface not active\n");
1267                 goto out;
1268         }
1269
1270         seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1271                    BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1272                    hard_iface->net_dev->dev_addr,
1273                    hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1274
1275         bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1276
1277 out:
1278         if (hard_iface)
1279                 batadv_hardif_free_ref(hard_iface);
1280         return 0;
1281 }
1282
1283 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1284                             int max_if_num)
1285 {
1286         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1287         struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1288         struct batadv_hashtable *hash = bat_priv->orig_hash;
1289         struct hlist_head *head;
1290         struct batadv_orig_node *orig_node;
1291         u32 i;
1292         int ret;
1293
1294         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1295          * if_num
1296          */
1297         for (i = 0; i < hash->size; i++) {
1298                 head = &hash->table[i];
1299
1300                 rcu_read_lock();
1301                 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1302                         ret = 0;
1303                         if (bao->bat_orig_add_if)
1304                                 ret = bao->bat_orig_add_if(orig_node,
1305                                                            max_if_num);
1306                         if (ret == -ENOMEM)
1307                                 goto err;
1308                 }
1309                 rcu_read_unlock();
1310         }
1311
1312         return 0;
1313
1314 err:
1315         rcu_read_unlock();
1316         return -ENOMEM;
1317 }
1318
1319 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1320                             int max_if_num)
1321 {
1322         struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1323         struct batadv_hashtable *hash = bat_priv->orig_hash;
1324         struct hlist_head *head;
1325         struct batadv_hard_iface *hard_iface_tmp;
1326         struct batadv_orig_node *orig_node;
1327         struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1328         u32 i;
1329         int ret;
1330
1331         /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1332          * if_num
1333          */
1334         for (i = 0; i < hash->size; i++) {
1335                 head = &hash->table[i];
1336
1337                 rcu_read_lock();
1338                 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1339                         ret = 0;
1340                         if (bao->bat_orig_del_if)
1341                                 ret = bao->bat_orig_del_if(orig_node,
1342                                                            max_if_num,
1343                                                            hard_iface->if_num);
1344                         if (ret == -ENOMEM)
1345                                 goto err;
1346                 }
1347                 rcu_read_unlock();
1348         }
1349
1350         /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1351         rcu_read_lock();
1352         list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1353                 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1354                         continue;
1355
1356                 if (hard_iface == hard_iface_tmp)
1357                         continue;
1358
1359                 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1360                         continue;
1361
1362                 if (hard_iface_tmp->if_num > hard_iface->if_num)
1363                         hard_iface_tmp->if_num--;
1364         }
1365         rcu_read_unlock();
1366
1367         hard_iface->if_num = -1;
1368         return 0;
1369
1370 err:
1371         rcu_read_unlock();
1372         return -ENOMEM;
1373 }