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