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