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