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_to_router() - get next hop neighbor to an orig address
316 * @bat_priv: the bat priv with all the soft interface information
317 * @orig_addr: the originator MAC address to search the best next hop router for
318 * @if_outgoing: the interface where the payload packet has been received or
319 * the OGM should be sent to
321 * Return: A neighbor node which is the best router towards the given originator
324 struct batadv_neigh_node *
325 batadv_orig_to_router(struct batadv_priv *bat_priv, u8 *orig_addr,
326 struct batadv_hard_iface *if_outgoing)
328 struct batadv_neigh_node *neigh_node;
329 struct batadv_orig_node *orig_node;
331 orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
335 neigh_node = batadv_find_router(bat_priv, orig_node, if_outgoing);
336 batadv_orig_node_put(orig_node);
342 * batadv_orig_ifinfo_get() - find the ifinfo from an orig_node
343 * @orig_node: the orig node to be queried
344 * @if_outgoing: the interface for which the ifinfo should be acquired
346 * Return: the requested orig_ifinfo or NULL if not found.
348 * The object is returned with refcounter increased by 1.
350 struct batadv_orig_ifinfo *
351 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
352 struct batadv_hard_iface *if_outgoing)
354 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
357 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
359 if (tmp->if_outgoing != if_outgoing)
362 if (!kref_get_unless_zero(&tmp->refcount))
374 * batadv_orig_ifinfo_new() - search and possibly create an orig_ifinfo object
375 * @orig_node: the orig node to be queried
376 * @if_outgoing: the interface for which the ifinfo should be acquired
378 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
379 * interface otherwise. The object is created and added to the list
380 * if it does not exist.
382 * The object is returned with refcounter increased by 1.
384 struct batadv_orig_ifinfo *
385 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
386 struct batadv_hard_iface *if_outgoing)
388 struct batadv_orig_ifinfo *orig_ifinfo;
389 unsigned long reset_time;
391 spin_lock_bh(&orig_node->neigh_list_lock);
393 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
397 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
401 if (if_outgoing != BATADV_IF_DEFAULT)
402 kref_get(&if_outgoing->refcount);
404 reset_time = jiffies - 1;
405 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
406 orig_ifinfo->batman_seqno_reset = reset_time;
407 orig_ifinfo->if_outgoing = if_outgoing;
408 INIT_HLIST_NODE(&orig_ifinfo->list);
409 kref_init(&orig_ifinfo->refcount);
411 kref_get(&orig_ifinfo->refcount);
412 hlist_add_head_rcu(&orig_ifinfo->list,
413 &orig_node->ifinfo_list);
415 spin_unlock_bh(&orig_node->neigh_list_lock);
420 * batadv_neigh_ifinfo_get() - find the ifinfo from an neigh_node
421 * @neigh: the neigh node to be queried
422 * @if_outgoing: the interface for which the ifinfo should be acquired
424 * The object is returned with refcounter increased by 1.
426 * Return: the requested neigh_ifinfo or NULL if not found
428 struct batadv_neigh_ifinfo *
429 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
430 struct batadv_hard_iface *if_outgoing)
432 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
436 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
438 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
441 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
444 neigh_ifinfo = tmp_neigh_ifinfo;
453 * batadv_neigh_ifinfo_new() - search and possibly create an neigh_ifinfo object
454 * @neigh: the neigh node to be queried
455 * @if_outgoing: the interface for which the ifinfo should be acquired
457 * Return: NULL in case of failure or the neigh_ifinfo object for the
458 * if_outgoing interface otherwise. The object is created and added to the list
459 * if it does not exist.
461 * The object is returned with refcounter increased by 1.
463 struct batadv_neigh_ifinfo *
464 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
465 struct batadv_hard_iface *if_outgoing)
467 struct batadv_neigh_ifinfo *neigh_ifinfo;
469 spin_lock_bh(&neigh->ifinfo_lock);
471 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
475 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
480 kref_get(&if_outgoing->refcount);
482 INIT_HLIST_NODE(&neigh_ifinfo->list);
483 kref_init(&neigh_ifinfo->refcount);
484 neigh_ifinfo->if_outgoing = if_outgoing;
486 kref_get(&neigh_ifinfo->refcount);
487 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
490 spin_unlock_bh(&neigh->ifinfo_lock);
496 * batadv_neigh_node_get() - retrieve a neighbour from the list
497 * @orig_node: originator which the neighbour belongs to
498 * @hard_iface: the interface where this neighbour is connected to
499 * @addr: the address of the neighbour
501 * Looks for and possibly returns a neighbour belonging to this originator list
502 * which is connected through the provided hard interface.
504 * Return: neighbor when found. Otherwise NULL
506 static struct batadv_neigh_node *
507 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
508 const struct batadv_hard_iface *hard_iface,
511 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
514 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
515 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
518 if (tmp_neigh_node->if_incoming != hard_iface)
521 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
524 res = tmp_neigh_node;
533 * batadv_hardif_neigh_create() - create a hardif neighbour node
534 * @hard_iface: the interface this neighbour is connected to
535 * @neigh_addr: the interface address of the neighbour to retrieve
536 * @orig_node: originator object representing the neighbour
538 * Return: the hardif neighbour node if found or created or NULL otherwise.
540 static struct batadv_hardif_neigh_node *
541 batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
542 const u8 *neigh_addr,
543 struct batadv_orig_node *orig_node)
545 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
546 struct batadv_hardif_neigh_node *hardif_neigh;
548 spin_lock_bh(&hard_iface->neigh_list_lock);
550 /* check if neighbor hasn't been added in the meantime */
551 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
555 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
559 kref_get(&hard_iface->refcount);
560 INIT_HLIST_NODE(&hardif_neigh->list);
561 ether_addr_copy(hardif_neigh->addr, neigh_addr);
562 ether_addr_copy(hardif_neigh->orig, orig_node->orig);
563 hardif_neigh->if_incoming = hard_iface;
564 hardif_neigh->last_seen = jiffies;
566 kref_init(&hardif_neigh->refcount);
568 if (bat_priv->algo_ops->neigh.hardif_init)
569 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
571 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
574 spin_unlock_bh(&hard_iface->neigh_list_lock);
579 * batadv_hardif_neigh_get_or_create() - retrieve or create a hardif neighbour
581 * @hard_iface: the interface this neighbour is connected to
582 * @neigh_addr: the interface address of the neighbour to retrieve
583 * @orig_node: originator object representing the neighbour
585 * Return: the hardif neighbour node if found or created or NULL otherwise.
587 static struct batadv_hardif_neigh_node *
588 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
589 const u8 *neigh_addr,
590 struct batadv_orig_node *orig_node)
592 struct batadv_hardif_neigh_node *hardif_neigh;
594 /* first check without locking to avoid the overhead */
595 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
599 return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
603 * batadv_hardif_neigh_get() - retrieve a hardif neighbour from the list
604 * @hard_iface: the interface where this neighbour is connected to
605 * @neigh_addr: the address of the neighbour
607 * Looks for and possibly returns a neighbour belonging to this hard interface.
609 * Return: neighbor when found. Otherwise NULL
611 struct batadv_hardif_neigh_node *
612 batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
613 const u8 *neigh_addr)
615 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
618 hlist_for_each_entry_rcu(tmp_hardif_neigh,
619 &hard_iface->neigh_list, list) {
620 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
623 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
626 hardif_neigh = tmp_hardif_neigh;
635 * batadv_neigh_node_create() - create a neigh node object
636 * @orig_node: originator object representing the neighbour
637 * @hard_iface: the interface where the neighbour is connected to
638 * @neigh_addr: the mac address of the neighbour interface
640 * Allocates a new neigh_node object and initialises all the generic fields.
642 * Return: the neighbour node if found or created or NULL otherwise.
644 static struct batadv_neigh_node *
645 batadv_neigh_node_create(struct batadv_orig_node *orig_node,
646 struct batadv_hard_iface *hard_iface,
647 const u8 *neigh_addr)
649 struct batadv_neigh_node *neigh_node;
650 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
652 spin_lock_bh(&orig_node->neigh_list_lock);
654 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
658 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
659 neigh_addr, orig_node);
663 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
667 INIT_HLIST_NODE(&neigh_node->list);
668 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
669 spin_lock_init(&neigh_node->ifinfo_lock);
671 kref_get(&hard_iface->refcount);
672 ether_addr_copy(neigh_node->addr, neigh_addr);
673 neigh_node->if_incoming = hard_iface;
674 neigh_node->orig_node = orig_node;
675 neigh_node->last_seen = jiffies;
677 /* increment unique neighbor refcount */
678 kref_get(&hardif_neigh->refcount);
679 neigh_node->hardif_neigh = hardif_neigh;
681 /* extra reference for return */
682 kref_init(&neigh_node->refcount);
684 kref_get(&neigh_node->refcount);
685 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
687 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
688 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
689 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
692 spin_unlock_bh(&orig_node->neigh_list_lock);
694 batadv_hardif_neigh_put(hardif_neigh);
699 * batadv_neigh_node_get_or_create() - retrieve or create a neigh node object
700 * @orig_node: originator object representing the neighbour
701 * @hard_iface: the interface where the neighbour is connected to
702 * @neigh_addr: the mac address of the neighbour interface
704 * Return: the neighbour node if found or created or NULL otherwise.
706 struct batadv_neigh_node *
707 batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
708 struct batadv_hard_iface *hard_iface,
709 const u8 *neigh_addr)
711 struct batadv_neigh_node *neigh_node;
713 /* first check without locking to avoid the overhead */
714 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
718 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
722 * batadv_hardif_neigh_dump() - Dump to netlink the neighbor infos for a
723 * specific outgoing interface
724 * @msg: message to dump into
725 * @cb: parameters for the dump
727 * Return: 0 or error value
729 int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
731 struct net *net = sock_net(cb->skb->sk);
732 struct net_device *soft_iface;
733 struct net_device *hard_iface = NULL;
734 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
735 struct batadv_priv *bat_priv;
736 struct batadv_hard_iface *primary_if = NULL;
738 int ifindex, hard_ifindex;
740 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
744 soft_iface = dev_get_by_index(net, ifindex);
745 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
750 bat_priv = netdev_priv(soft_iface);
752 primary_if = batadv_primary_if_get_selected(bat_priv);
753 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
758 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
759 BATADV_ATTR_HARD_IFINDEX);
761 hard_iface = dev_get_by_index(net, hard_ifindex);
763 hardif = batadv_hardif_get_by_netdev(hard_iface);
770 if (hardif->soft_iface != soft_iface) {
776 if (!bat_priv->algo_ops->neigh.dump) {
781 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
786 batadv_hardif_put(hardif);
788 batadv_hardif_put(primary_if);
795 * batadv_orig_ifinfo_release() - release orig_ifinfo from lists and queue for
796 * free after rcu grace period
797 * @ref: kref pointer of the orig_ifinfo
799 void batadv_orig_ifinfo_release(struct kref *ref)
801 struct batadv_orig_ifinfo *orig_ifinfo;
802 struct batadv_neigh_node *router;
804 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
806 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
807 batadv_hardif_put(orig_ifinfo->if_outgoing);
809 /* this is the last reference to this object */
810 router = rcu_dereference_protected(orig_ifinfo->router, true);
811 batadv_neigh_node_put(router);
813 kfree_rcu(orig_ifinfo, rcu);
817 * batadv_orig_node_free_rcu() - free the orig_node
818 * @rcu: rcu pointer of the orig_node
820 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
822 struct batadv_orig_node *orig_node;
824 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
826 batadv_mcast_purge_orig(orig_node);
828 batadv_frag_purge_orig(orig_node, NULL);
830 kfree(orig_node->tt_buff);
835 * batadv_orig_node_release() - release orig_node from lists and queue for
836 * free after rcu grace period
837 * @ref: kref pointer of the orig_node
839 void batadv_orig_node_release(struct kref *ref)
841 struct hlist_node *node_tmp;
842 struct batadv_neigh_node *neigh_node;
843 struct batadv_orig_node *orig_node;
844 struct batadv_orig_ifinfo *orig_ifinfo;
845 struct batadv_orig_node_vlan *vlan;
846 struct batadv_orig_ifinfo *last_candidate;
848 orig_node = container_of(ref, struct batadv_orig_node, refcount);
850 spin_lock_bh(&orig_node->neigh_list_lock);
852 /* for all neighbors towards this originator ... */
853 hlist_for_each_entry_safe(neigh_node, node_tmp,
854 &orig_node->neigh_list, list) {
855 hlist_del_rcu(&neigh_node->list);
856 batadv_neigh_node_put(neigh_node);
859 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
860 &orig_node->ifinfo_list, list) {
861 hlist_del_rcu(&orig_ifinfo->list);
862 batadv_orig_ifinfo_put(orig_ifinfo);
865 last_candidate = orig_node->last_bonding_candidate;
866 orig_node->last_bonding_candidate = NULL;
867 spin_unlock_bh(&orig_node->neigh_list_lock);
869 batadv_orig_ifinfo_put(last_candidate);
871 spin_lock_bh(&orig_node->vlan_list_lock);
872 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
873 hlist_del_rcu(&vlan->list);
874 batadv_orig_node_vlan_put(vlan);
876 spin_unlock_bh(&orig_node->vlan_list_lock);
879 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
881 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
885 * batadv_originator_free() - Free all originator structures
886 * @bat_priv: the bat priv with all the soft interface information
888 void batadv_originator_free(struct batadv_priv *bat_priv)
890 struct batadv_hashtable *hash = bat_priv->orig_hash;
891 struct hlist_node *node_tmp;
892 struct hlist_head *head;
893 spinlock_t *list_lock; /* spinlock to protect write access */
894 struct batadv_orig_node *orig_node;
900 cancel_delayed_work_sync(&bat_priv->orig_work);
902 bat_priv->orig_hash = NULL;
904 for (i = 0; i < hash->size; i++) {
905 head = &hash->table[i];
906 list_lock = &hash->list_locks[i];
908 spin_lock_bh(list_lock);
909 hlist_for_each_entry_safe(orig_node, node_tmp,
911 hlist_del_rcu(&orig_node->hash_entry);
912 batadv_orig_node_put(orig_node);
914 spin_unlock_bh(list_lock);
917 batadv_hash_destroy(hash);
921 * batadv_orig_node_new() - creates a new orig_node
922 * @bat_priv: the bat priv with all the soft interface information
923 * @addr: the mac address of the originator
925 * Creates a new originator object and initialises all the generic fields.
926 * The new object is not added to the originator list.
928 * Return: the newly created object or NULL on failure.
930 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
933 struct batadv_orig_node *orig_node;
934 struct batadv_orig_node_vlan *vlan;
935 unsigned long reset_time;
938 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
939 "Creating new originator: %pM\n", addr);
941 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
945 INIT_HLIST_HEAD(&orig_node->neigh_list);
946 INIT_HLIST_HEAD(&orig_node->vlan_list);
947 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
948 spin_lock_init(&orig_node->bcast_seqno_lock);
949 spin_lock_init(&orig_node->neigh_list_lock);
950 spin_lock_init(&orig_node->tt_buff_lock);
951 spin_lock_init(&orig_node->tt_lock);
952 spin_lock_init(&orig_node->vlan_list_lock);
954 batadv_nc_init_orig(orig_node);
956 /* extra reference for return */
957 kref_init(&orig_node->refcount);
959 orig_node->bat_priv = bat_priv;
960 ether_addr_copy(orig_node->orig, addr);
961 batadv_dat_init_orig_node_addr(orig_node);
962 atomic_set(&orig_node->last_ttvn, 0);
963 orig_node->tt_buff = NULL;
964 orig_node->tt_buff_len = 0;
965 orig_node->last_seen = jiffies;
966 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
967 orig_node->bcast_seqno_reset = reset_time;
969 #ifdef CONFIG_BATMAN_ADV_MCAST
970 orig_node->mcast_flags = BATADV_MCAST_WANT_NO_RTR4;
971 orig_node->mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
972 orig_node->mcast_flags |= BATADV_MCAST_HAVE_MC_PTYPE_CAPA;
973 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
974 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
975 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
976 spin_lock_init(&orig_node->mcast_handler_lock);
979 /* create a vlan object for the "untagged" LAN */
980 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
983 /* batadv_orig_node_vlan_new() increases the refcounter.
984 * Immediately release vlan since it is not needed anymore in this
987 batadv_orig_node_vlan_put(vlan);
989 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
990 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
991 spin_lock_init(&orig_node->fragments[i].lock);
992 orig_node->fragments[i].size = 0;
1002 * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor
1003 * @bat_priv: the bat priv with all the soft interface information
1004 * @neigh: orig node which is to be checked
1007 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1008 struct batadv_neigh_node *neigh)
1010 struct batadv_neigh_ifinfo *neigh_ifinfo;
1011 struct batadv_hard_iface *if_outgoing;
1012 struct hlist_node *node_tmp;
1014 spin_lock_bh(&neigh->ifinfo_lock);
1016 /* for all ifinfo objects for this neighinator */
1017 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1018 &neigh->ifinfo_list, list) {
1019 if_outgoing = neigh_ifinfo->if_outgoing;
1021 /* always keep the default interface */
1022 if (if_outgoing == BATADV_IF_DEFAULT)
1025 /* don't purge if the interface is not (going) down */
1026 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1027 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1028 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
1031 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1032 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1033 neigh->addr, if_outgoing->net_dev->name);
1035 hlist_del_rcu(&neigh_ifinfo->list);
1036 batadv_neigh_ifinfo_put(neigh_ifinfo);
1039 spin_unlock_bh(&neigh->ifinfo_lock);
1043 * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator
1044 * @bat_priv: the bat priv with all the soft interface information
1045 * @orig_node: orig node which is to be checked
1047 * Return: true if any ifinfo entry was purged, false otherwise.
1050 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1051 struct batadv_orig_node *orig_node)
1053 struct batadv_orig_ifinfo *orig_ifinfo;
1054 struct batadv_hard_iface *if_outgoing;
1055 struct hlist_node *node_tmp;
1056 bool ifinfo_purged = false;
1058 spin_lock_bh(&orig_node->neigh_list_lock);
1060 /* for all ifinfo objects for this originator */
1061 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1062 &orig_node->ifinfo_list, list) {
1063 if_outgoing = orig_ifinfo->if_outgoing;
1065 /* always keep the default interface */
1066 if (if_outgoing == BATADV_IF_DEFAULT)
1069 /* don't purge if the interface is not (going) down */
1070 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1071 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1072 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
1075 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1076 "router/ifinfo purge: originator %pM, iface: %s\n",
1077 orig_node->orig, if_outgoing->net_dev->name);
1079 ifinfo_purged = true;
1081 hlist_del_rcu(&orig_ifinfo->list);
1082 batadv_orig_ifinfo_put(orig_ifinfo);
1083 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1084 orig_node->last_bonding_candidate = NULL;
1085 batadv_orig_ifinfo_put(orig_ifinfo);
1089 spin_unlock_bh(&orig_node->neigh_list_lock);
1091 return ifinfo_purged;
1095 * batadv_purge_orig_neighbors() - purges neighbors from originator
1096 * @bat_priv: the bat priv with all the soft interface information
1097 * @orig_node: orig node which is to be checked
1099 * Return: true if any neighbor was purged, false otherwise
1102 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
1103 struct batadv_orig_node *orig_node)
1105 struct hlist_node *node_tmp;
1106 struct batadv_neigh_node *neigh_node;
1107 bool neigh_purged = false;
1108 unsigned long last_seen;
1109 struct batadv_hard_iface *if_incoming;
1111 spin_lock_bh(&orig_node->neigh_list_lock);
1113 /* for all neighbors towards this originator ... */
1114 hlist_for_each_entry_safe(neigh_node, node_tmp,
1115 &orig_node->neigh_list, list) {
1116 last_seen = neigh_node->last_seen;
1117 if_incoming = neigh_node->if_incoming;
1119 if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) ||
1120 if_incoming->if_status == BATADV_IF_INACTIVE ||
1121 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1122 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) {
1123 if (if_incoming->if_status == BATADV_IF_INACTIVE ||
1124 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1125 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)
1126 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1127 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1128 orig_node->orig, neigh_node->addr,
1129 if_incoming->net_dev->name);
1131 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1132 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1133 orig_node->orig, neigh_node->addr,
1134 jiffies_to_msecs(last_seen));
1136 neigh_purged = true;
1138 hlist_del_rcu(&neigh_node->list);
1139 batadv_neigh_node_put(neigh_node);
1141 /* only necessary if not the whole neighbor is to be
1142 * deleted, but some interface has been removed.
1144 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
1148 spin_unlock_bh(&orig_node->neigh_list_lock);
1149 return neigh_purged;
1153 * batadv_find_best_neighbor() - finds the best neighbor after purging
1154 * @bat_priv: the bat priv with all the soft interface information
1155 * @orig_node: orig node which is to be checked
1156 * @if_outgoing: the interface for which the metric should be compared
1158 * Return: the current best neighbor, with refcount increased.
1160 static struct batadv_neigh_node *
1161 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1162 struct batadv_orig_node *orig_node,
1163 struct batadv_hard_iface *if_outgoing)
1165 struct batadv_neigh_node *best = NULL, *neigh;
1166 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1169 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1170 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1174 if (!kref_get_unless_zero(&neigh->refcount))
1177 batadv_neigh_node_put(best);
1187 * batadv_purge_orig_node() - purges obsolete information from an orig_node
1188 * @bat_priv: the bat priv with all the soft interface information
1189 * @orig_node: orig node which is to be checked
1191 * This function checks if the orig_node or substructures of it have become
1192 * obsolete, and purges this information if that's the case.
1194 * Return: true if the orig_node is to be removed, false otherwise.
1196 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1197 struct batadv_orig_node *orig_node)
1199 struct batadv_neigh_node *best_neigh_node;
1200 struct batadv_hard_iface *hard_iface;
1201 bool changed_ifinfo, changed_neigh;
1203 if (batadv_has_timed_out(orig_node->last_seen,
1204 2 * BATADV_PURGE_TIMEOUT)) {
1205 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1206 "Originator timeout: originator %pM, last_seen %u\n",
1208 jiffies_to_msecs(orig_node->last_seen));
1211 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1212 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
1214 if (!changed_ifinfo && !changed_neigh)
1217 /* first for NULL ... */
1218 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1220 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1222 batadv_neigh_node_put(best_neigh_node);
1224 /* ... then for all other interfaces. */
1226 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1227 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1230 if (hard_iface->soft_iface != bat_priv->soft_iface)
1233 if (!kref_get_unless_zero(&hard_iface->refcount))
1236 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1239 batadv_update_route(bat_priv, orig_node, hard_iface,
1241 batadv_neigh_node_put(best_neigh_node);
1243 batadv_hardif_put(hard_iface);
1251 * batadv_purge_orig_ref() - Purge all outdated originators
1252 * @bat_priv: the bat priv with all the soft interface information
1254 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
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;
1266 /* for all origins... */
1267 for (i = 0; i < hash->size; i++) {
1268 head = &hash->table[i];
1269 list_lock = &hash->list_locks[i];
1271 spin_lock_bh(list_lock);
1272 hlist_for_each_entry_safe(orig_node, node_tmp,
1274 if (batadv_purge_orig_node(bat_priv, orig_node)) {
1275 batadv_gw_node_delete(bat_priv, orig_node);
1276 hlist_del_rcu(&orig_node->hash_entry);
1277 batadv_tt_global_del_orig(orig_node->bat_priv,
1279 "originator timed out");
1280 batadv_orig_node_put(orig_node);
1284 batadv_frag_purge_orig(orig_node,
1285 batadv_frag_check_entry);
1287 spin_unlock_bh(list_lock);
1290 batadv_gw_election(bat_priv);
1293 static void batadv_purge_orig(struct work_struct *work)
1295 struct delayed_work *delayed_work;
1296 struct batadv_priv *bat_priv;
1298 delayed_work = to_delayed_work(work);
1299 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1300 batadv_purge_orig_ref(bat_priv);
1301 queue_delayed_work(batadv_event_workqueue,
1302 &bat_priv->orig_work,
1303 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1307 * batadv_orig_dump() - Dump to netlink the originator infos for a specific
1308 * outgoing interface
1309 * @msg: message to dump into
1310 * @cb: parameters for the dump
1312 * Return: 0 or error value
1314 int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1316 struct net *net = sock_net(cb->skb->sk);
1317 struct net_device *soft_iface;
1318 struct net_device *hard_iface = NULL;
1319 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1320 struct batadv_priv *bat_priv;
1321 struct batadv_hard_iface *primary_if = NULL;
1323 int ifindex, hard_ifindex;
1325 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1329 soft_iface = dev_get_by_index(net, ifindex);
1330 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1335 bat_priv = netdev_priv(soft_iface);
1337 primary_if = batadv_primary_if_get_selected(bat_priv);
1338 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1343 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1344 BATADV_ATTR_HARD_IFINDEX);
1346 hard_iface = dev_get_by_index(net, hard_ifindex);
1348 hardif = batadv_hardif_get_by_netdev(hard_iface);
1355 if (hardif->soft_iface != soft_iface) {
1361 if (!bat_priv->algo_ops->orig.dump) {
1366 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1371 batadv_hardif_put(hardif);
1372 dev_put(hard_iface);
1373 batadv_hardif_put(primary_if);
1374 dev_put(soft_iface);