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