1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
7 #include "originator.h"
10 #include <linux/atomic.h>
11 #include <linux/container_of.h>
12 #include <linux/errno.h>
13 #include <linux/etherdevice.h>
14 #include <linux/gfp.h>
15 #include <linux/jiffies.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>
29 #include <uapi/linux/batadv_packet.h>
30 #include <uapi/linux/batman_adv.h>
33 #include "distributed-arp-table.h"
34 #include "fragmentation.h"
35 #include "gateway_client.h"
36 #include "hard-interface.h"
39 #include "multicast.h"
41 #include "network-coding.h"
43 #include "soft-interface.h"
44 #include "translation-table.h"
47 static struct lock_class_key batadv_orig_hash_lock_class_key;
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
54 * Return: orig_node (with increased refcnt), NULL on errors
56 struct batadv_orig_node *
57 batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)
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;
67 index = batadv_choose_orig(data, hash->size);
68 head = &hash->table[index];
71 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
72 if (!batadv_compare_eth(orig_node, data))
75 if (!kref_get_unless_zero(&orig_node->refcount))
78 orig_node_tmp = orig_node;
86 static void batadv_purge_orig(struct work_struct *work);
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
93 * Return: true if they are the same originator
95 bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
97 const void *data1 = container_of(node, struct batadv_orig_node,
100 return batadv_compare_eth(data1, data2);
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
108 * Return: the vlan object identified by vid and belonging to orig_node or NULL
109 * if it does not exist.
111 struct batadv_orig_node_vlan *
112 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
115 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
118 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
122 if (!kref_get_unless_zero(&tmp->refcount))
135 * batadv_orig_node_vlan_new() - search and possibly create an orig_node_vlan
137 * @orig_node: the originator serving the VLAN
138 * @vid: the VLAN identifier
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.
144 * The object is returned with refcounter increased by 1.
146 struct batadv_orig_node_vlan *
147 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
150 struct batadv_orig_node_vlan *vlan;
152 spin_lock_bh(&orig_node->vlan_list_lock);
154 /* first look if an object for this vid already exists */
155 vlan = batadv_orig_node_vlan_get(orig_node, vid);
159 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
163 kref_init(&vlan->refcount);
166 kref_get(&vlan->refcount);
167 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
170 spin_unlock_bh(&orig_node->vlan_list_lock);
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
180 void batadv_orig_node_vlan_release(struct kref *ref)
182 struct batadv_orig_node_vlan *orig_vlan;
184 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
186 kfree_rcu(orig_vlan, rcu);
190 * batadv_originator_init() - Initialize all originator structures
191 * @bat_priv: the bat priv with all the soft interface information
193 * Return: 0 on success or negative error number in case of failure
195 int batadv_originator_init(struct batadv_priv *bat_priv)
197 if (bat_priv->orig_hash)
200 bat_priv->orig_hash = batadv_hash_new(1024);
202 if (!bat_priv->orig_hash)
205 batadv_hash_set_lock_class(bat_priv->orig_hash,
206 &batadv_orig_hash_lock_class_key);
208 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
209 queue_delayed_work(batadv_event_workqueue,
210 &bat_priv->orig_work,
211 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
220 * batadv_neigh_ifinfo_release() - release neigh_ifinfo from lists and queue for
221 * free after rcu grace period
222 * @ref: kref pointer of the neigh_ifinfo
224 void batadv_neigh_ifinfo_release(struct kref *ref)
226 struct batadv_neigh_ifinfo *neigh_ifinfo;
228 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
230 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
231 batadv_hardif_put(neigh_ifinfo->if_outgoing);
233 kfree_rcu(neigh_ifinfo, rcu);
237 * batadv_hardif_neigh_release() - release hardif neigh node from lists and
238 * queue for free after rcu grace period
239 * @ref: kref pointer of the neigh_node
241 void batadv_hardif_neigh_release(struct kref *ref)
243 struct batadv_hardif_neigh_node *hardif_neigh;
245 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
248 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
249 hlist_del_init_rcu(&hardif_neigh->list);
250 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
252 batadv_hardif_put(hardif_neigh->if_incoming);
253 kfree_rcu(hardif_neigh, rcu);
257 * batadv_neigh_node_release() - release neigh_node from lists and queue for
258 * free after rcu grace period
259 * @ref: kref pointer of the neigh_node
261 void batadv_neigh_node_release(struct kref *ref)
263 struct hlist_node *node_tmp;
264 struct batadv_neigh_node *neigh_node;
265 struct batadv_neigh_ifinfo *neigh_ifinfo;
267 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
269 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
270 &neigh_node->ifinfo_list, list) {
271 batadv_neigh_ifinfo_put(neigh_ifinfo);
274 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
276 batadv_hardif_put(neigh_node->if_incoming);
278 kfree_rcu(neigh_node, rcu);
282 * batadv_orig_router_get() - router to the originator depending on iface
283 * @orig_node: the orig node for the router
284 * @if_outgoing: the interface where the payload packet has been received or
285 * the OGM should be sent to
287 * Return: the neighbor which should be the router for this orig_node/iface.
289 * The object is returned with refcounter increased by 1.
291 struct batadv_neigh_node *
292 batadv_orig_router_get(struct batadv_orig_node *orig_node,
293 const struct batadv_hard_iface *if_outgoing)
295 struct batadv_orig_ifinfo *orig_ifinfo;
296 struct batadv_neigh_node *router = NULL;
299 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
300 if (orig_ifinfo->if_outgoing != if_outgoing)
303 router = rcu_dereference(orig_ifinfo->router);
307 if (router && !kref_get_unless_zero(&router->refcount))
315 * batadv_orig_ifinfo_get() - find the ifinfo from an orig_node
316 * @orig_node: the orig node to be queried
317 * @if_outgoing: the interface for which the ifinfo should be acquired
319 * Return: the requested orig_ifinfo or NULL if not found.
321 * The object is returned with refcounter increased by 1.
323 struct batadv_orig_ifinfo *
324 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
325 struct batadv_hard_iface *if_outgoing)
327 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
330 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
332 if (tmp->if_outgoing != if_outgoing)
335 if (!kref_get_unless_zero(&tmp->refcount))
347 * batadv_orig_ifinfo_new() - search and possibly create an orig_ifinfo object
348 * @orig_node: the orig node to be queried
349 * @if_outgoing: the interface for which the ifinfo should be acquired
351 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
352 * interface otherwise. The object is created and added to the list
353 * if it does not exist.
355 * The object is returned with refcounter increased by 1.
357 struct batadv_orig_ifinfo *
358 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
359 struct batadv_hard_iface *if_outgoing)
361 struct batadv_orig_ifinfo *orig_ifinfo;
362 unsigned long reset_time;
364 spin_lock_bh(&orig_node->neigh_list_lock);
366 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
370 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
374 if (if_outgoing != BATADV_IF_DEFAULT)
375 kref_get(&if_outgoing->refcount);
377 reset_time = jiffies - 1;
378 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
379 orig_ifinfo->batman_seqno_reset = reset_time;
380 orig_ifinfo->if_outgoing = if_outgoing;
381 INIT_HLIST_NODE(&orig_ifinfo->list);
382 kref_init(&orig_ifinfo->refcount);
384 kref_get(&orig_ifinfo->refcount);
385 hlist_add_head_rcu(&orig_ifinfo->list,
386 &orig_node->ifinfo_list);
388 spin_unlock_bh(&orig_node->neigh_list_lock);
393 * batadv_neigh_ifinfo_get() - find the ifinfo from an neigh_node
394 * @neigh: the neigh node to be queried
395 * @if_outgoing: the interface for which the ifinfo should be acquired
397 * The object is returned with refcounter increased by 1.
399 * Return: the requested neigh_ifinfo or NULL if not found
401 struct batadv_neigh_ifinfo *
402 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
403 struct batadv_hard_iface *if_outgoing)
405 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
409 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
411 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
414 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
417 neigh_ifinfo = tmp_neigh_ifinfo;
426 * batadv_neigh_ifinfo_new() - search and possibly create an neigh_ifinfo object
427 * @neigh: the neigh node to be queried
428 * @if_outgoing: the interface for which the ifinfo should be acquired
430 * Return: NULL in case of failure or the neigh_ifinfo object for the
431 * if_outgoing interface otherwise. The object is created and added to the list
432 * if it does not exist.
434 * The object is returned with refcounter increased by 1.
436 struct batadv_neigh_ifinfo *
437 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
438 struct batadv_hard_iface *if_outgoing)
440 struct batadv_neigh_ifinfo *neigh_ifinfo;
442 spin_lock_bh(&neigh->ifinfo_lock);
444 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
448 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
453 kref_get(&if_outgoing->refcount);
455 INIT_HLIST_NODE(&neigh_ifinfo->list);
456 kref_init(&neigh_ifinfo->refcount);
457 neigh_ifinfo->if_outgoing = if_outgoing;
459 kref_get(&neigh_ifinfo->refcount);
460 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
463 spin_unlock_bh(&neigh->ifinfo_lock);
469 * batadv_neigh_node_get() - retrieve a neighbour from the list
470 * @orig_node: originator which the neighbour belongs to
471 * @hard_iface: the interface where this neighbour is connected to
472 * @addr: the address of the neighbour
474 * Looks for and possibly returns a neighbour belonging to this originator list
475 * which is connected through the provided hard interface.
477 * Return: neighbor when found. Otherwise NULL
479 static struct batadv_neigh_node *
480 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
481 const struct batadv_hard_iface *hard_iface,
484 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
487 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
488 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
491 if (tmp_neigh_node->if_incoming != hard_iface)
494 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
497 res = tmp_neigh_node;
506 * batadv_hardif_neigh_create() - create a hardif neighbour node
507 * @hard_iface: the interface this neighbour is connected to
508 * @neigh_addr: the interface address of the neighbour to retrieve
509 * @orig_node: originator object representing the neighbour
511 * Return: the hardif neighbour node if found or created or NULL otherwise.
513 static struct batadv_hardif_neigh_node *
514 batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
515 const u8 *neigh_addr,
516 struct batadv_orig_node *orig_node)
518 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
519 struct batadv_hardif_neigh_node *hardif_neigh;
521 spin_lock_bh(&hard_iface->neigh_list_lock);
523 /* check if neighbor hasn't been added in the meantime */
524 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
528 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
532 kref_get(&hard_iface->refcount);
533 INIT_HLIST_NODE(&hardif_neigh->list);
534 ether_addr_copy(hardif_neigh->addr, neigh_addr);
535 ether_addr_copy(hardif_neigh->orig, orig_node->orig);
536 hardif_neigh->if_incoming = hard_iface;
537 hardif_neigh->last_seen = jiffies;
539 kref_init(&hardif_neigh->refcount);
541 if (bat_priv->algo_ops->neigh.hardif_init)
542 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
544 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
547 spin_unlock_bh(&hard_iface->neigh_list_lock);
552 * batadv_hardif_neigh_get_or_create() - retrieve or create a hardif neighbour
554 * @hard_iface: the interface this neighbour is connected to
555 * @neigh_addr: the interface address of the neighbour to retrieve
556 * @orig_node: originator object representing the neighbour
558 * Return: the hardif neighbour node if found or created or NULL otherwise.
560 static struct batadv_hardif_neigh_node *
561 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
562 const u8 *neigh_addr,
563 struct batadv_orig_node *orig_node)
565 struct batadv_hardif_neigh_node *hardif_neigh;
567 /* first check without locking to avoid the overhead */
568 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
572 return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
576 * batadv_hardif_neigh_get() - retrieve a hardif neighbour from the list
577 * @hard_iface: the interface where this neighbour is connected to
578 * @neigh_addr: the address of the neighbour
580 * Looks for and possibly returns a neighbour belonging to this hard interface.
582 * Return: neighbor when found. Otherwise NULL
584 struct batadv_hardif_neigh_node *
585 batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
586 const u8 *neigh_addr)
588 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
591 hlist_for_each_entry_rcu(tmp_hardif_neigh,
592 &hard_iface->neigh_list, list) {
593 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
596 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
599 hardif_neigh = tmp_hardif_neigh;
608 * batadv_neigh_node_create() - create a neigh node object
609 * @orig_node: originator object representing the neighbour
610 * @hard_iface: the interface where the neighbour is connected to
611 * @neigh_addr: the mac address of the neighbour interface
613 * Allocates a new neigh_node object and initialises all the generic fields.
615 * Return: the neighbour node if found or created or NULL otherwise.
617 static struct batadv_neigh_node *
618 batadv_neigh_node_create(struct batadv_orig_node *orig_node,
619 struct batadv_hard_iface *hard_iface,
620 const u8 *neigh_addr)
622 struct batadv_neigh_node *neigh_node;
623 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
625 spin_lock_bh(&orig_node->neigh_list_lock);
627 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
631 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
632 neigh_addr, orig_node);
636 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
640 INIT_HLIST_NODE(&neigh_node->list);
641 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
642 spin_lock_init(&neigh_node->ifinfo_lock);
644 kref_get(&hard_iface->refcount);
645 ether_addr_copy(neigh_node->addr, neigh_addr);
646 neigh_node->if_incoming = hard_iface;
647 neigh_node->orig_node = orig_node;
648 neigh_node->last_seen = jiffies;
650 /* increment unique neighbor refcount */
651 kref_get(&hardif_neigh->refcount);
652 neigh_node->hardif_neigh = hardif_neigh;
654 /* extra reference for return */
655 kref_init(&neigh_node->refcount);
657 kref_get(&neigh_node->refcount);
658 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
660 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
661 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
662 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
665 spin_unlock_bh(&orig_node->neigh_list_lock);
667 batadv_hardif_neigh_put(hardif_neigh);
672 * batadv_neigh_node_get_or_create() - retrieve or create a neigh node object
673 * @orig_node: originator object representing the neighbour
674 * @hard_iface: the interface where the neighbour is connected to
675 * @neigh_addr: the mac address of the neighbour interface
677 * Return: the neighbour node if found or created or NULL otherwise.
679 struct batadv_neigh_node *
680 batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
681 struct batadv_hard_iface *hard_iface,
682 const u8 *neigh_addr)
684 struct batadv_neigh_node *neigh_node;
686 /* first check without locking to avoid the overhead */
687 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
691 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
695 * batadv_hardif_neigh_dump() - Dump to netlink the neighbor infos for a
696 * specific outgoing interface
697 * @msg: message to dump into
698 * @cb: parameters for the dump
700 * Return: 0 or error value
702 int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
704 struct net *net = sock_net(cb->skb->sk);
705 struct net_device *soft_iface;
706 struct net_device *hard_iface = NULL;
707 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
708 struct batadv_priv *bat_priv;
709 struct batadv_hard_iface *primary_if = NULL;
711 int ifindex, hard_ifindex;
713 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
717 soft_iface = dev_get_by_index(net, ifindex);
718 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
723 bat_priv = netdev_priv(soft_iface);
725 primary_if = batadv_primary_if_get_selected(bat_priv);
726 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
731 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
732 BATADV_ATTR_HARD_IFINDEX);
734 hard_iface = dev_get_by_index(net, hard_ifindex);
736 hardif = batadv_hardif_get_by_netdev(hard_iface);
743 if (hardif->soft_iface != soft_iface) {
749 if (!bat_priv->algo_ops->neigh.dump) {
754 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
759 batadv_hardif_put(hardif);
761 batadv_hardif_put(primary_if);
768 * batadv_orig_ifinfo_release() - release orig_ifinfo from lists and queue for
769 * free after rcu grace period
770 * @ref: kref pointer of the orig_ifinfo
772 void batadv_orig_ifinfo_release(struct kref *ref)
774 struct batadv_orig_ifinfo *orig_ifinfo;
775 struct batadv_neigh_node *router;
777 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
779 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
780 batadv_hardif_put(orig_ifinfo->if_outgoing);
782 /* this is the last reference to this object */
783 router = rcu_dereference_protected(orig_ifinfo->router, true);
784 batadv_neigh_node_put(router);
786 kfree_rcu(orig_ifinfo, rcu);
790 * batadv_orig_node_free_rcu() - free the orig_node
791 * @rcu: rcu pointer of the orig_node
793 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
795 struct batadv_orig_node *orig_node;
797 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
799 batadv_mcast_purge_orig(orig_node);
801 batadv_frag_purge_orig(orig_node, NULL);
803 kfree(orig_node->tt_buff);
808 * batadv_orig_node_release() - release orig_node from lists and queue for
809 * free after rcu grace period
810 * @ref: kref pointer of the orig_node
812 void batadv_orig_node_release(struct kref *ref)
814 struct hlist_node *node_tmp;
815 struct batadv_neigh_node *neigh_node;
816 struct batadv_orig_node *orig_node;
817 struct batadv_orig_ifinfo *orig_ifinfo;
818 struct batadv_orig_node_vlan *vlan;
819 struct batadv_orig_ifinfo *last_candidate;
821 orig_node = container_of(ref, struct batadv_orig_node, refcount);
823 spin_lock_bh(&orig_node->neigh_list_lock);
825 /* for all neighbors towards this originator ... */
826 hlist_for_each_entry_safe(neigh_node, node_tmp,
827 &orig_node->neigh_list, list) {
828 hlist_del_rcu(&neigh_node->list);
829 batadv_neigh_node_put(neigh_node);
832 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
833 &orig_node->ifinfo_list, list) {
834 hlist_del_rcu(&orig_ifinfo->list);
835 batadv_orig_ifinfo_put(orig_ifinfo);
838 last_candidate = orig_node->last_bonding_candidate;
839 orig_node->last_bonding_candidate = NULL;
840 spin_unlock_bh(&orig_node->neigh_list_lock);
842 batadv_orig_ifinfo_put(last_candidate);
844 spin_lock_bh(&orig_node->vlan_list_lock);
845 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
846 hlist_del_rcu(&vlan->list);
847 batadv_orig_node_vlan_put(vlan);
849 spin_unlock_bh(&orig_node->vlan_list_lock);
852 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
854 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
858 * batadv_originator_free() - Free all originator structures
859 * @bat_priv: the bat priv with all the soft interface information
861 void batadv_originator_free(struct batadv_priv *bat_priv)
863 struct batadv_hashtable *hash = bat_priv->orig_hash;
864 struct hlist_node *node_tmp;
865 struct hlist_head *head;
866 spinlock_t *list_lock; /* spinlock to protect write access */
867 struct batadv_orig_node *orig_node;
873 cancel_delayed_work_sync(&bat_priv->orig_work);
875 bat_priv->orig_hash = NULL;
877 for (i = 0; i < hash->size; i++) {
878 head = &hash->table[i];
879 list_lock = &hash->list_locks[i];
881 spin_lock_bh(list_lock);
882 hlist_for_each_entry_safe(orig_node, node_tmp,
884 hlist_del_rcu(&orig_node->hash_entry);
885 batadv_orig_node_put(orig_node);
887 spin_unlock_bh(list_lock);
890 batadv_hash_destroy(hash);
894 * batadv_orig_node_new() - creates a new orig_node
895 * @bat_priv: the bat priv with all the soft interface information
896 * @addr: the mac address of the originator
898 * Creates a new originator object and initialises all the generic fields.
899 * The new object is not added to the originator list.
901 * Return: the newly created object or NULL on failure.
903 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
906 struct batadv_orig_node *orig_node;
907 struct batadv_orig_node_vlan *vlan;
908 unsigned long reset_time;
911 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
912 "Creating new originator: %pM\n", addr);
914 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
918 INIT_HLIST_HEAD(&orig_node->neigh_list);
919 INIT_HLIST_HEAD(&orig_node->vlan_list);
920 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
921 spin_lock_init(&orig_node->bcast_seqno_lock);
922 spin_lock_init(&orig_node->neigh_list_lock);
923 spin_lock_init(&orig_node->tt_buff_lock);
924 spin_lock_init(&orig_node->tt_lock);
925 spin_lock_init(&orig_node->vlan_list_lock);
927 batadv_nc_init_orig(orig_node);
929 /* extra reference for return */
930 kref_init(&orig_node->refcount);
932 orig_node->bat_priv = bat_priv;
933 ether_addr_copy(orig_node->orig, addr);
934 batadv_dat_init_orig_node_addr(orig_node);
935 atomic_set(&orig_node->last_ttvn, 0);
936 orig_node->tt_buff = NULL;
937 orig_node->tt_buff_len = 0;
938 orig_node->last_seen = jiffies;
939 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
940 orig_node->bcast_seqno_reset = reset_time;
942 #ifdef CONFIG_BATMAN_ADV_MCAST
943 orig_node->mcast_flags = BATADV_MCAST_WANT_NO_RTR4;
944 orig_node->mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
945 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
946 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
947 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
948 spin_lock_init(&orig_node->mcast_handler_lock);
951 /* create a vlan object for the "untagged" LAN */
952 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
955 /* batadv_orig_node_vlan_new() increases the refcounter.
956 * Immediately release vlan since it is not needed anymore in this
959 batadv_orig_node_vlan_put(vlan);
961 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
962 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
963 spin_lock_init(&orig_node->fragments[i].lock);
964 orig_node->fragments[i].size = 0;
974 * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor
975 * @bat_priv: the bat priv with all the soft interface information
976 * @neigh: orig node which is to be checked
979 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
980 struct batadv_neigh_node *neigh)
982 struct batadv_neigh_ifinfo *neigh_ifinfo;
983 struct batadv_hard_iface *if_outgoing;
984 struct hlist_node *node_tmp;
986 spin_lock_bh(&neigh->ifinfo_lock);
988 /* for all ifinfo objects for this neighinator */
989 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
990 &neigh->ifinfo_list, list) {
991 if_outgoing = neigh_ifinfo->if_outgoing;
993 /* always keep the default interface */
994 if (if_outgoing == BATADV_IF_DEFAULT)
997 /* don't purge if the interface is not (going) down */
998 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
999 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1000 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
1003 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1004 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1005 neigh->addr, if_outgoing->net_dev->name);
1007 hlist_del_rcu(&neigh_ifinfo->list);
1008 batadv_neigh_ifinfo_put(neigh_ifinfo);
1011 spin_unlock_bh(&neigh->ifinfo_lock);
1015 * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator
1016 * @bat_priv: the bat priv with all the soft interface information
1017 * @orig_node: orig node which is to be checked
1019 * Return: true if any ifinfo entry was purged, false otherwise.
1022 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1023 struct batadv_orig_node *orig_node)
1025 struct batadv_orig_ifinfo *orig_ifinfo;
1026 struct batadv_hard_iface *if_outgoing;
1027 struct hlist_node *node_tmp;
1028 bool ifinfo_purged = false;
1030 spin_lock_bh(&orig_node->neigh_list_lock);
1032 /* for all ifinfo objects for this originator */
1033 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1034 &orig_node->ifinfo_list, list) {
1035 if_outgoing = orig_ifinfo->if_outgoing;
1037 /* always keep the default interface */
1038 if (if_outgoing == BATADV_IF_DEFAULT)
1041 /* don't purge if the interface is not (going) down */
1042 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1043 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1044 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
1047 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1048 "router/ifinfo purge: originator %pM, iface: %s\n",
1049 orig_node->orig, if_outgoing->net_dev->name);
1051 ifinfo_purged = true;
1053 hlist_del_rcu(&orig_ifinfo->list);
1054 batadv_orig_ifinfo_put(orig_ifinfo);
1055 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1056 orig_node->last_bonding_candidate = NULL;
1057 batadv_orig_ifinfo_put(orig_ifinfo);
1061 spin_unlock_bh(&orig_node->neigh_list_lock);
1063 return ifinfo_purged;
1067 * batadv_purge_orig_neighbors() - purges neighbors from originator
1068 * @bat_priv: the bat priv with all the soft interface information
1069 * @orig_node: orig node which is to be checked
1071 * Return: true if any neighbor was purged, false otherwise
1074 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
1075 struct batadv_orig_node *orig_node)
1077 struct hlist_node *node_tmp;
1078 struct batadv_neigh_node *neigh_node;
1079 bool neigh_purged = false;
1080 unsigned long last_seen;
1081 struct batadv_hard_iface *if_incoming;
1083 spin_lock_bh(&orig_node->neigh_list_lock);
1085 /* for all neighbors towards this originator ... */
1086 hlist_for_each_entry_safe(neigh_node, node_tmp,
1087 &orig_node->neigh_list, list) {
1088 last_seen = neigh_node->last_seen;
1089 if_incoming = neigh_node->if_incoming;
1091 if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) ||
1092 if_incoming->if_status == BATADV_IF_INACTIVE ||
1093 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1094 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) {
1095 if (if_incoming->if_status == BATADV_IF_INACTIVE ||
1096 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1097 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)
1098 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1099 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1100 orig_node->orig, neigh_node->addr,
1101 if_incoming->net_dev->name);
1103 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1104 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1105 orig_node->orig, neigh_node->addr,
1106 jiffies_to_msecs(last_seen));
1108 neigh_purged = true;
1110 hlist_del_rcu(&neigh_node->list);
1111 batadv_neigh_node_put(neigh_node);
1113 /* only necessary if not the whole neighbor is to be
1114 * deleted, but some interface has been removed.
1116 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
1120 spin_unlock_bh(&orig_node->neigh_list_lock);
1121 return neigh_purged;
1125 * batadv_find_best_neighbor() - finds the best neighbor after purging
1126 * @bat_priv: the bat priv with all the soft interface information
1127 * @orig_node: orig node which is to be checked
1128 * @if_outgoing: the interface for which the metric should be compared
1130 * Return: the current best neighbor, with refcount increased.
1132 static struct batadv_neigh_node *
1133 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1134 struct batadv_orig_node *orig_node,
1135 struct batadv_hard_iface *if_outgoing)
1137 struct batadv_neigh_node *best = NULL, *neigh;
1138 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1141 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1142 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1146 if (!kref_get_unless_zero(&neigh->refcount))
1149 batadv_neigh_node_put(best);
1159 * batadv_purge_orig_node() - purges obsolete information from an orig_node
1160 * @bat_priv: the bat priv with all the soft interface information
1161 * @orig_node: orig node which is to be checked
1163 * This function checks if the orig_node or substructures of it have become
1164 * obsolete, and purges this information if that's the case.
1166 * Return: true if the orig_node is to be removed, false otherwise.
1168 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1169 struct batadv_orig_node *orig_node)
1171 struct batadv_neigh_node *best_neigh_node;
1172 struct batadv_hard_iface *hard_iface;
1173 bool changed_ifinfo, changed_neigh;
1175 if (batadv_has_timed_out(orig_node->last_seen,
1176 2 * BATADV_PURGE_TIMEOUT)) {
1177 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1178 "Originator timeout: originator %pM, last_seen %u\n",
1180 jiffies_to_msecs(orig_node->last_seen));
1183 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1184 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
1186 if (!changed_ifinfo && !changed_neigh)
1189 /* first for NULL ... */
1190 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1192 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1194 batadv_neigh_node_put(best_neigh_node);
1196 /* ... then for all other interfaces. */
1198 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1199 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1202 if (hard_iface->soft_iface != bat_priv->soft_iface)
1205 if (!kref_get_unless_zero(&hard_iface->refcount))
1208 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1211 batadv_update_route(bat_priv, orig_node, hard_iface,
1213 batadv_neigh_node_put(best_neigh_node);
1215 batadv_hardif_put(hard_iface);
1223 * batadv_purge_orig_ref() - Purge all outdated originators
1224 * @bat_priv: the bat priv with all the soft interface information
1226 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1228 struct batadv_hashtable *hash = bat_priv->orig_hash;
1229 struct hlist_node *node_tmp;
1230 struct hlist_head *head;
1231 spinlock_t *list_lock; /* spinlock to protect write access */
1232 struct batadv_orig_node *orig_node;
1238 /* for all origins... */
1239 for (i = 0; i < hash->size; i++) {
1240 head = &hash->table[i];
1241 list_lock = &hash->list_locks[i];
1243 spin_lock_bh(list_lock);
1244 hlist_for_each_entry_safe(orig_node, node_tmp,
1246 if (batadv_purge_orig_node(bat_priv, orig_node)) {
1247 batadv_gw_node_delete(bat_priv, orig_node);
1248 hlist_del_rcu(&orig_node->hash_entry);
1249 batadv_tt_global_del_orig(orig_node->bat_priv,
1251 "originator timed out");
1252 batadv_orig_node_put(orig_node);
1256 batadv_frag_purge_orig(orig_node,
1257 batadv_frag_check_entry);
1259 spin_unlock_bh(list_lock);
1262 batadv_gw_election(bat_priv);
1265 static void batadv_purge_orig(struct work_struct *work)
1267 struct delayed_work *delayed_work;
1268 struct batadv_priv *bat_priv;
1270 delayed_work = to_delayed_work(work);
1271 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1272 batadv_purge_orig_ref(bat_priv);
1273 queue_delayed_work(batadv_event_workqueue,
1274 &bat_priv->orig_work,
1275 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1279 * batadv_orig_dump() - Dump to netlink the originator infos for a specific
1280 * outgoing interface
1281 * @msg: message to dump into
1282 * @cb: parameters for the dump
1284 * Return: 0 or error value
1286 int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1288 struct net *net = sock_net(cb->skb->sk);
1289 struct net_device *soft_iface;
1290 struct net_device *hard_iface = NULL;
1291 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1292 struct batadv_priv *bat_priv;
1293 struct batadv_hard_iface *primary_if = NULL;
1295 int ifindex, hard_ifindex;
1297 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1301 soft_iface = dev_get_by_index(net, ifindex);
1302 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1307 bat_priv = netdev_priv(soft_iface);
1309 primary_if = batadv_primary_if_get_selected(bat_priv);
1310 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1315 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1316 BATADV_ATTR_HARD_IFINDEX);
1318 hard_iface = dev_get_by_index(net, hard_ifindex);
1320 hardif = batadv_hardif_get_by_netdev(hard_iface);
1327 if (hardif->soft_iface != soft_iface) {
1333 if (!bat_priv->algo_ops->orig.dump) {
1338 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1343 batadv_hardif_put(hardif);
1344 dev_put(hard_iface);
1345 batadv_hardif_put(primary_if);
1346 dev_put(soft_iface);