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