1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2009-2019 B.A.T.M.A.N. contributors:
4 * Marek Lindner, Simon Wunderlich
7 #include "originator.h"
10 #include <linux/atomic.h>
11 #include <linux/errno.h>
12 #include <linux/etherdevice.h>
13 #include <linux/gfp.h>
14 #include <linux/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/kref.h>
17 #include <linux/list.h>
18 #include <linux/lockdep.h>
19 #include <linux/netdevice.h>
20 #include <linux/netlink.h>
21 #include <linux/rculist.h>
22 #include <linux/rcupdate.h>
23 #include <linux/seq_file.h>
24 #include <linux/skbuff.h>
25 #include <linux/slab.h>
26 #include <linux/spinlock.h>
27 #include <linux/stddef.h>
28 #include <linux/workqueue.h>
30 #include <uapi/linux/batadv_packet.h>
31 #include <uapi/linux/batman_adv.h>
34 #include "distributed-arp-table.h"
35 #include "fragmentation.h"
36 #include "gateway_client.h"
37 #include "hard-interface.h"
40 #include "multicast.h"
42 #include "network-coding.h"
44 #include "soft-interface.h"
45 #include "translation-table.h"
48 static struct lock_class_key batadv_orig_hash_lock_class_key;
51 * batadv_orig_hash_find() - Find and return originator from orig_hash
52 * @bat_priv: the bat priv with all the soft interface information
53 * @data: mac address of the originator
55 * Return: orig_node (with increased refcnt), NULL on errors
57 struct batadv_orig_node *
58 batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)
60 struct batadv_hashtable *hash = bat_priv->orig_hash;
61 struct hlist_head *head;
62 struct batadv_orig_node *orig_node, *orig_node_tmp = NULL;
68 index = batadv_choose_orig(data, hash->size);
69 head = &hash->table[index];
72 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
73 if (!batadv_compare_eth(orig_node, data))
76 if (!kref_get_unless_zero(&orig_node->refcount))
79 orig_node_tmp = orig_node;
87 static void batadv_purge_orig(struct work_struct *work);
90 * batadv_compare_orig() - comparing function used in the originator hash table
91 * @node: node in the local table
92 * @data2: second object to compare the node to
94 * Return: true if they are the same originator
96 bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
98 const void *data1 = container_of(node, struct batadv_orig_node,
101 return batadv_compare_eth(data1, data2);
105 * batadv_orig_node_vlan_get() - get an orig_node_vlan object
106 * @orig_node: the originator serving the VLAN
107 * @vid: the VLAN identifier
109 * Return: the vlan object identified by vid and belonging to orig_node or NULL
110 * if it does not exist.
112 struct batadv_orig_node_vlan *
113 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
116 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
119 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
123 if (!kref_get_unless_zero(&tmp->refcount))
136 * batadv_orig_node_vlan_new() - search and possibly create an orig_node_vlan
138 * @orig_node: the originator serving the VLAN
139 * @vid: the VLAN identifier
141 * Return: NULL in case of failure or the vlan object identified by vid and
142 * belonging to orig_node otherwise. The object is created and added to the list
143 * if it does not exist.
145 * The object is returned with refcounter increased by 1.
147 struct batadv_orig_node_vlan *
148 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
151 struct batadv_orig_node_vlan *vlan;
153 spin_lock_bh(&orig_node->vlan_list_lock);
155 /* first look if an object for this vid already exists */
156 vlan = batadv_orig_node_vlan_get(orig_node, vid);
160 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
164 kref_init(&vlan->refcount);
167 kref_get(&vlan->refcount);
168 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
171 spin_unlock_bh(&orig_node->vlan_list_lock);
177 * batadv_orig_node_vlan_release() - release originator-vlan object from lists
178 * and queue for free after rcu grace period
179 * @ref: kref pointer of the originator-vlan object
181 static void batadv_orig_node_vlan_release(struct kref *ref)
183 struct batadv_orig_node_vlan *orig_vlan;
185 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
187 kfree_rcu(orig_vlan, rcu);
191 * batadv_orig_node_vlan_put() - decrement the refcounter and possibly release
192 * the originator-vlan object
193 * @orig_vlan: the originator-vlan object to release
195 void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
197 kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
201 * batadv_originator_init() - Initialize all originator structures
202 * @bat_priv: the bat priv with all the soft interface information
204 * Return: 0 on success or negative error number in case of failure
206 int batadv_originator_init(struct batadv_priv *bat_priv)
208 if (bat_priv->orig_hash)
211 bat_priv->orig_hash = batadv_hash_new(1024);
213 if (!bat_priv->orig_hash)
216 batadv_hash_set_lock_class(bat_priv->orig_hash,
217 &batadv_orig_hash_lock_class_key);
219 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
220 queue_delayed_work(batadv_event_workqueue,
221 &bat_priv->orig_work,
222 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
231 * batadv_neigh_ifinfo_release() - release neigh_ifinfo from lists and queue for
232 * free after rcu grace period
233 * @ref: kref pointer of the neigh_ifinfo
235 static void batadv_neigh_ifinfo_release(struct kref *ref)
237 struct batadv_neigh_ifinfo *neigh_ifinfo;
239 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
241 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
242 batadv_hardif_put(neigh_ifinfo->if_outgoing);
244 kfree_rcu(neigh_ifinfo, rcu);
248 * batadv_neigh_ifinfo_put() - decrement the refcounter and possibly release
250 * @neigh_ifinfo: the neigh_ifinfo object to release
252 void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
254 kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
258 * batadv_hardif_neigh_release() - release hardif neigh node from lists and
259 * queue for free after rcu grace period
260 * @ref: kref pointer of the neigh_node
262 static void batadv_hardif_neigh_release(struct kref *ref)
264 struct batadv_hardif_neigh_node *hardif_neigh;
266 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
269 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
270 hlist_del_init_rcu(&hardif_neigh->list);
271 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
273 batadv_hardif_put(hardif_neigh->if_incoming);
274 kfree_rcu(hardif_neigh, rcu);
278 * batadv_hardif_neigh_put() - decrement the hardif neighbors refcounter
279 * and possibly release it
280 * @hardif_neigh: hardif neigh neighbor to free
282 void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
284 kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
288 * batadv_neigh_node_release() - release neigh_node from lists and queue for
289 * free after rcu grace period
290 * @ref: kref pointer of the neigh_node
292 static void batadv_neigh_node_release(struct kref *ref)
294 struct hlist_node *node_tmp;
295 struct batadv_neigh_node *neigh_node;
296 struct batadv_neigh_ifinfo *neigh_ifinfo;
298 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
300 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
301 &neigh_node->ifinfo_list, list) {
302 batadv_neigh_ifinfo_put(neigh_ifinfo);
305 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
307 batadv_hardif_put(neigh_node->if_incoming);
309 kfree_rcu(neigh_node, rcu);
313 * batadv_neigh_node_put() - decrement the neighbors refcounter and possibly
315 * @neigh_node: neigh neighbor to free
317 void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
319 kref_put(&neigh_node->refcount, batadv_neigh_node_release);
323 * batadv_orig_router_get() - router to the originator depending on iface
324 * @orig_node: the orig node for the router
325 * @if_outgoing: the interface where the payload packet has been received or
326 * the OGM should be sent to
328 * Return: the neighbor which should be router for this orig_node/iface.
330 * The object is returned with refcounter increased by 1.
332 struct batadv_neigh_node *
333 batadv_orig_router_get(struct batadv_orig_node *orig_node,
334 const struct batadv_hard_iface *if_outgoing)
336 struct batadv_orig_ifinfo *orig_ifinfo;
337 struct batadv_neigh_node *router = NULL;
340 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
341 if (orig_ifinfo->if_outgoing != if_outgoing)
344 router = rcu_dereference(orig_ifinfo->router);
348 if (router && !kref_get_unless_zero(&router->refcount))
356 * batadv_orig_ifinfo_get() - find the ifinfo from an orig_node
357 * @orig_node: the orig node to be queried
358 * @if_outgoing: the interface for which the ifinfo should be acquired
360 * Return: the requested orig_ifinfo or NULL if not found.
362 * The object is returned with refcounter increased by 1.
364 struct batadv_orig_ifinfo *
365 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
366 struct batadv_hard_iface *if_outgoing)
368 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
371 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
373 if (tmp->if_outgoing != if_outgoing)
376 if (!kref_get_unless_zero(&tmp->refcount))
388 * batadv_orig_ifinfo_new() - search and possibly create an orig_ifinfo object
389 * @orig_node: the orig node to be queried
390 * @if_outgoing: the interface for which the ifinfo should be acquired
392 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
393 * interface otherwise. The object is created and added to the list
394 * if it does not exist.
396 * The object is returned with refcounter increased by 1.
398 struct batadv_orig_ifinfo *
399 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
400 struct batadv_hard_iface *if_outgoing)
402 struct batadv_orig_ifinfo *orig_ifinfo;
403 unsigned long reset_time;
405 spin_lock_bh(&orig_node->neigh_list_lock);
407 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
411 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
415 if (if_outgoing != BATADV_IF_DEFAULT)
416 kref_get(&if_outgoing->refcount);
418 reset_time = jiffies - 1;
419 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
420 orig_ifinfo->batman_seqno_reset = reset_time;
421 orig_ifinfo->if_outgoing = if_outgoing;
422 INIT_HLIST_NODE(&orig_ifinfo->list);
423 kref_init(&orig_ifinfo->refcount);
425 kref_get(&orig_ifinfo->refcount);
426 hlist_add_head_rcu(&orig_ifinfo->list,
427 &orig_node->ifinfo_list);
429 spin_unlock_bh(&orig_node->neigh_list_lock);
434 * batadv_neigh_ifinfo_get() - find the ifinfo from an neigh_node
435 * @neigh: the neigh node to be queried
436 * @if_outgoing: the interface for which the ifinfo should be acquired
438 * The object is returned with refcounter increased by 1.
440 * Return: the requested neigh_ifinfo or NULL if not found
442 struct batadv_neigh_ifinfo *
443 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
444 struct batadv_hard_iface *if_outgoing)
446 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
450 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
452 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
455 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
458 neigh_ifinfo = tmp_neigh_ifinfo;
467 * batadv_neigh_ifinfo_new() - search and possibly create an neigh_ifinfo object
468 * @neigh: the neigh node to be queried
469 * @if_outgoing: the interface for which the ifinfo should be acquired
471 * Return: NULL in case of failure or the neigh_ifinfo object for the
472 * if_outgoing interface otherwise. The object is created and added to the list
473 * if it does not exist.
475 * The object is returned with refcounter increased by 1.
477 struct batadv_neigh_ifinfo *
478 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
479 struct batadv_hard_iface *if_outgoing)
481 struct batadv_neigh_ifinfo *neigh_ifinfo;
483 spin_lock_bh(&neigh->ifinfo_lock);
485 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
489 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
494 kref_get(&if_outgoing->refcount);
496 INIT_HLIST_NODE(&neigh_ifinfo->list);
497 kref_init(&neigh_ifinfo->refcount);
498 neigh_ifinfo->if_outgoing = if_outgoing;
500 kref_get(&neigh_ifinfo->refcount);
501 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
504 spin_unlock_bh(&neigh->ifinfo_lock);
510 * batadv_neigh_node_get() - retrieve a neighbour from the list
511 * @orig_node: originator which the neighbour belongs to
512 * @hard_iface: the interface where this neighbour is connected to
513 * @addr: the address of the neighbour
515 * Looks for and possibly returns a neighbour belonging to this originator list
516 * which is connected through the provided hard interface.
518 * Return: neighbor when found. Othwerwise NULL
520 static struct batadv_neigh_node *
521 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
522 const struct batadv_hard_iface *hard_iface,
525 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
528 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
529 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
532 if (tmp_neigh_node->if_incoming != hard_iface)
535 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
538 res = tmp_neigh_node;
547 * batadv_hardif_neigh_create() - create a hardif neighbour node
548 * @hard_iface: the interface this neighbour is connected to
549 * @neigh_addr: the interface address of the neighbour to retrieve
550 * @orig_node: originator object representing the neighbour
552 * Return: the hardif neighbour node if found or created or NULL otherwise.
554 static struct batadv_hardif_neigh_node *
555 batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
556 const u8 *neigh_addr,
557 struct batadv_orig_node *orig_node)
559 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
560 struct batadv_hardif_neigh_node *hardif_neigh;
562 spin_lock_bh(&hard_iface->neigh_list_lock);
564 /* check if neighbor hasn't been added in the meantime */
565 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
569 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
573 kref_get(&hard_iface->refcount);
574 INIT_HLIST_NODE(&hardif_neigh->list);
575 ether_addr_copy(hardif_neigh->addr, neigh_addr);
576 ether_addr_copy(hardif_neigh->orig, orig_node->orig);
577 hardif_neigh->if_incoming = hard_iface;
578 hardif_neigh->last_seen = jiffies;
580 kref_init(&hardif_neigh->refcount);
582 if (bat_priv->algo_ops->neigh.hardif_init)
583 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
585 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
588 spin_unlock_bh(&hard_iface->neigh_list_lock);
593 * batadv_hardif_neigh_get_or_create() - retrieve or create a hardif neighbour
595 * @hard_iface: the interface this neighbour is connected to
596 * @neigh_addr: the interface address of the neighbour to retrieve
597 * @orig_node: originator object representing the neighbour
599 * Return: the hardif neighbour node if found or created or NULL otherwise.
601 static struct batadv_hardif_neigh_node *
602 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
603 const u8 *neigh_addr,
604 struct batadv_orig_node *orig_node)
606 struct batadv_hardif_neigh_node *hardif_neigh;
608 /* first check without locking to avoid the overhead */
609 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
613 return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
617 * batadv_hardif_neigh_get() - retrieve a hardif neighbour from the list
618 * @hard_iface: the interface where this neighbour is connected to
619 * @neigh_addr: the address of the neighbour
621 * Looks for and possibly returns a neighbour belonging to this hard interface.
623 * Return: neighbor when found. Othwerwise NULL
625 struct batadv_hardif_neigh_node *
626 batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
627 const u8 *neigh_addr)
629 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
632 hlist_for_each_entry_rcu(tmp_hardif_neigh,
633 &hard_iface->neigh_list, list) {
634 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
637 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
640 hardif_neigh = tmp_hardif_neigh;
649 * batadv_neigh_node_create() - create a neigh node object
650 * @orig_node: originator object representing the neighbour
651 * @hard_iface: the interface where the neighbour is connected to
652 * @neigh_addr: the mac address of the neighbour interface
654 * Allocates a new neigh_node object and initialises all the generic fields.
656 * Return: the neighbour node if found or created or NULL otherwise.
658 static struct batadv_neigh_node *
659 batadv_neigh_node_create(struct batadv_orig_node *orig_node,
660 struct batadv_hard_iface *hard_iface,
661 const u8 *neigh_addr)
663 struct batadv_neigh_node *neigh_node;
664 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
666 spin_lock_bh(&orig_node->neigh_list_lock);
668 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
672 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
673 neigh_addr, orig_node);
677 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
681 INIT_HLIST_NODE(&neigh_node->list);
682 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
683 spin_lock_init(&neigh_node->ifinfo_lock);
685 kref_get(&hard_iface->refcount);
686 ether_addr_copy(neigh_node->addr, neigh_addr);
687 neigh_node->if_incoming = hard_iface;
688 neigh_node->orig_node = orig_node;
689 neigh_node->last_seen = jiffies;
691 /* increment unique neighbor refcount */
692 kref_get(&hardif_neigh->refcount);
693 neigh_node->hardif_neigh = hardif_neigh;
695 /* extra reference for return */
696 kref_init(&neigh_node->refcount);
698 kref_get(&neigh_node->refcount);
699 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
701 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
702 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
703 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
706 spin_unlock_bh(&orig_node->neigh_list_lock);
709 batadv_hardif_neigh_put(hardif_neigh);
714 * batadv_neigh_node_get_or_create() - retrieve or create a neigh node object
715 * @orig_node: originator object representing the neighbour
716 * @hard_iface: the interface where the neighbour is connected to
717 * @neigh_addr: the mac address of the neighbour interface
719 * Return: the neighbour node if found or created or NULL otherwise.
721 struct batadv_neigh_node *
722 batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
723 struct batadv_hard_iface *hard_iface,
724 const u8 *neigh_addr)
726 struct batadv_neigh_node *neigh_node;
728 /* first check without locking to avoid the overhead */
729 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
733 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
736 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
738 * batadv_hardif_neigh_seq_print_text() - print the single hop neighbour list
739 * @seq: neighbour table seq_file struct
744 int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
746 struct net_device *net_dev = (struct net_device *)seq->private;
747 struct batadv_priv *bat_priv = netdev_priv(net_dev);
748 struct batadv_hard_iface *primary_if;
750 primary_if = batadv_seq_print_text_primary_if_get(seq);
754 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
755 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
756 primary_if->net_dev->dev_addr, net_dev->name,
757 bat_priv->algo_ops->name);
759 batadv_hardif_put(primary_if);
761 if (!bat_priv->algo_ops->neigh.print) {
763 "No printing function for this routing protocol\n");
767 bat_priv->algo_ops->neigh.print(bat_priv, seq);
773 * batadv_hardif_neigh_dump() - Dump to netlink the neighbor infos for a
774 * specific outgoing interface
775 * @msg: message to dump into
776 * @cb: parameters for the dump
778 * Return: 0 or error value
780 int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
782 struct net *net = sock_net(cb->skb->sk);
783 struct net_device *soft_iface;
784 struct net_device *hard_iface = NULL;
785 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
786 struct batadv_priv *bat_priv;
787 struct batadv_hard_iface *primary_if = NULL;
789 int ifindex, hard_ifindex;
791 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
795 soft_iface = dev_get_by_index(net, ifindex);
796 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
801 bat_priv = netdev_priv(soft_iface);
803 primary_if = batadv_primary_if_get_selected(bat_priv);
804 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
809 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
810 BATADV_ATTR_HARD_IFINDEX);
812 hard_iface = dev_get_by_index(net, hard_ifindex);
814 hardif = batadv_hardif_get_by_netdev(hard_iface);
821 if (hardif->soft_iface != soft_iface) {
827 if (!bat_priv->algo_ops->neigh.dump) {
832 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
838 batadv_hardif_put(hardif);
842 batadv_hardif_put(primary_if);
850 * batadv_orig_ifinfo_release() - release orig_ifinfo from lists and queue for
851 * free after rcu grace period
852 * @ref: kref pointer of the orig_ifinfo
854 static void batadv_orig_ifinfo_release(struct kref *ref)
856 struct batadv_orig_ifinfo *orig_ifinfo;
857 struct batadv_neigh_node *router;
859 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
861 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
862 batadv_hardif_put(orig_ifinfo->if_outgoing);
864 /* this is the last reference to this object */
865 router = rcu_dereference_protected(orig_ifinfo->router, true);
867 batadv_neigh_node_put(router);
869 kfree_rcu(orig_ifinfo, rcu);
873 * batadv_orig_ifinfo_put() - decrement the refcounter and possibly release
875 * @orig_ifinfo: the orig_ifinfo object to release
877 void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
879 kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
883 * batadv_orig_node_free_rcu() - free the orig_node
884 * @rcu: rcu pointer of the orig_node
886 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
888 struct batadv_orig_node *orig_node;
890 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
892 batadv_mcast_purge_orig(orig_node);
894 batadv_frag_purge_orig(orig_node, NULL);
896 kfree(orig_node->tt_buff);
901 * batadv_orig_node_release() - release orig_node from lists and queue for
902 * free after rcu grace period
903 * @ref: kref pointer of the orig_node
905 static void batadv_orig_node_release(struct kref *ref)
907 struct hlist_node *node_tmp;
908 struct batadv_neigh_node *neigh_node;
909 struct batadv_orig_node *orig_node;
910 struct batadv_orig_ifinfo *orig_ifinfo;
911 struct batadv_orig_node_vlan *vlan;
912 struct batadv_orig_ifinfo *last_candidate;
914 orig_node = container_of(ref, struct batadv_orig_node, refcount);
916 spin_lock_bh(&orig_node->neigh_list_lock);
918 /* for all neighbors towards this originator ... */
919 hlist_for_each_entry_safe(neigh_node, node_tmp,
920 &orig_node->neigh_list, list) {
921 hlist_del_rcu(&neigh_node->list);
922 batadv_neigh_node_put(neigh_node);
925 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
926 &orig_node->ifinfo_list, list) {
927 hlist_del_rcu(&orig_ifinfo->list);
928 batadv_orig_ifinfo_put(orig_ifinfo);
931 last_candidate = orig_node->last_bonding_candidate;
932 orig_node->last_bonding_candidate = NULL;
933 spin_unlock_bh(&orig_node->neigh_list_lock);
936 batadv_orig_ifinfo_put(last_candidate);
938 spin_lock_bh(&orig_node->vlan_list_lock);
939 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
940 hlist_del_rcu(&vlan->list);
941 batadv_orig_node_vlan_put(vlan);
943 spin_unlock_bh(&orig_node->vlan_list_lock);
946 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
948 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
952 * batadv_orig_node_put() - decrement the orig node refcounter and possibly
954 * @orig_node: the orig node to free
956 void batadv_orig_node_put(struct batadv_orig_node *orig_node)
958 kref_put(&orig_node->refcount, batadv_orig_node_release);
962 * batadv_originator_free() - Free all originator structures
963 * @bat_priv: the bat priv with all the soft interface information
965 void batadv_originator_free(struct batadv_priv *bat_priv)
967 struct batadv_hashtable *hash = bat_priv->orig_hash;
968 struct hlist_node *node_tmp;
969 struct hlist_head *head;
970 spinlock_t *list_lock; /* spinlock to protect write access */
971 struct batadv_orig_node *orig_node;
977 cancel_delayed_work_sync(&bat_priv->orig_work);
979 bat_priv->orig_hash = NULL;
981 for (i = 0; i < hash->size; i++) {
982 head = &hash->table[i];
983 list_lock = &hash->list_locks[i];
985 spin_lock_bh(list_lock);
986 hlist_for_each_entry_safe(orig_node, node_tmp,
988 hlist_del_rcu(&orig_node->hash_entry);
989 batadv_orig_node_put(orig_node);
991 spin_unlock_bh(list_lock);
994 batadv_hash_destroy(hash);
998 * batadv_orig_node_new() - creates a new orig_node
999 * @bat_priv: the bat priv with all the soft interface information
1000 * @addr: the mac address of the originator
1002 * Creates a new originator object and initialise all the generic fields.
1003 * The new object is not added to the originator list.
1005 * Return: the newly created object or NULL on failure.
1007 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
1010 struct batadv_orig_node *orig_node;
1011 struct batadv_orig_node_vlan *vlan;
1012 unsigned long reset_time;
1015 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1016 "Creating new originator: %pM\n", addr);
1018 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
1022 INIT_HLIST_HEAD(&orig_node->neigh_list);
1023 INIT_HLIST_HEAD(&orig_node->vlan_list);
1024 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
1025 spin_lock_init(&orig_node->bcast_seqno_lock);
1026 spin_lock_init(&orig_node->neigh_list_lock);
1027 spin_lock_init(&orig_node->tt_buff_lock);
1028 spin_lock_init(&orig_node->tt_lock);
1029 spin_lock_init(&orig_node->vlan_list_lock);
1031 batadv_nc_init_orig(orig_node);
1033 /* extra reference for return */
1034 kref_init(&orig_node->refcount);
1036 orig_node->bat_priv = bat_priv;
1037 ether_addr_copy(orig_node->orig, addr);
1038 batadv_dat_init_orig_node_addr(orig_node);
1039 atomic_set(&orig_node->last_ttvn, 0);
1040 orig_node->tt_buff = NULL;
1041 orig_node->tt_buff_len = 0;
1042 orig_node->last_seen = jiffies;
1043 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
1044 orig_node->bcast_seqno_reset = reset_time;
1046 #ifdef CONFIG_BATMAN_ADV_MCAST
1047 orig_node->mcast_flags = BATADV_MCAST_WANT_NO_RTR4;
1048 orig_node->mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
1049 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
1050 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
1051 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
1052 spin_lock_init(&orig_node->mcast_handler_lock);
1055 /* create a vlan object for the "untagged" LAN */
1056 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
1058 goto free_orig_node;
1059 /* batadv_orig_node_vlan_new() increases the refcounter.
1060 * Immediately release vlan since it is not needed anymore in this
1063 batadv_orig_node_vlan_put(vlan);
1065 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
1066 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
1067 spin_lock_init(&orig_node->fragments[i].lock);
1068 orig_node->fragments[i].size = 0;
1078 * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor
1079 * @bat_priv: the bat priv with all the soft interface information
1080 * @neigh: orig node which is to be checked
1083 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1084 struct batadv_neigh_node *neigh)
1086 struct batadv_neigh_ifinfo *neigh_ifinfo;
1087 struct batadv_hard_iface *if_outgoing;
1088 struct hlist_node *node_tmp;
1090 spin_lock_bh(&neigh->ifinfo_lock);
1092 /* for all ifinfo objects for this neighinator */
1093 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1094 &neigh->ifinfo_list, list) {
1095 if_outgoing = neigh_ifinfo->if_outgoing;
1097 /* always keep the default interface */
1098 if (if_outgoing == BATADV_IF_DEFAULT)
1101 /* don't purge if the interface is not (going) down */
1102 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1103 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1104 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
1107 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1108 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1109 neigh->addr, if_outgoing->net_dev->name);
1111 hlist_del_rcu(&neigh_ifinfo->list);
1112 batadv_neigh_ifinfo_put(neigh_ifinfo);
1115 spin_unlock_bh(&neigh->ifinfo_lock);
1119 * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator
1120 * @bat_priv: the bat priv with all the soft interface information
1121 * @orig_node: orig node which is to be checked
1123 * Return: true if any ifinfo entry was purged, false otherwise.
1126 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1127 struct batadv_orig_node *orig_node)
1129 struct batadv_orig_ifinfo *orig_ifinfo;
1130 struct batadv_hard_iface *if_outgoing;
1131 struct hlist_node *node_tmp;
1132 bool ifinfo_purged = false;
1134 spin_lock_bh(&orig_node->neigh_list_lock);
1136 /* for all ifinfo objects for this originator */
1137 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1138 &orig_node->ifinfo_list, list) {
1139 if_outgoing = orig_ifinfo->if_outgoing;
1141 /* always keep the default interface */
1142 if (if_outgoing == BATADV_IF_DEFAULT)
1145 /* don't purge if the interface is not (going) down */
1146 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1147 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1148 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
1151 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1152 "router/ifinfo purge: originator %pM, iface: %s\n",
1153 orig_node->orig, if_outgoing->net_dev->name);
1155 ifinfo_purged = true;
1157 hlist_del_rcu(&orig_ifinfo->list);
1158 batadv_orig_ifinfo_put(orig_ifinfo);
1159 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1160 orig_node->last_bonding_candidate = NULL;
1161 batadv_orig_ifinfo_put(orig_ifinfo);
1165 spin_unlock_bh(&orig_node->neigh_list_lock);
1167 return ifinfo_purged;
1171 * batadv_purge_orig_neighbors() - purges neighbors from originator
1172 * @bat_priv: the bat priv with all the soft interface information
1173 * @orig_node: orig node which is to be checked
1175 * Return: true if any neighbor was purged, false otherwise
1178 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
1179 struct batadv_orig_node *orig_node)
1181 struct hlist_node *node_tmp;
1182 struct batadv_neigh_node *neigh_node;
1183 bool neigh_purged = false;
1184 unsigned long last_seen;
1185 struct batadv_hard_iface *if_incoming;
1187 spin_lock_bh(&orig_node->neigh_list_lock);
1189 /* for all neighbors towards this originator ... */
1190 hlist_for_each_entry_safe(neigh_node, node_tmp,
1191 &orig_node->neigh_list, list) {
1192 last_seen = neigh_node->last_seen;
1193 if_incoming = neigh_node->if_incoming;
1195 if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) ||
1196 if_incoming->if_status == BATADV_IF_INACTIVE ||
1197 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1198 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) {
1199 if (if_incoming->if_status == BATADV_IF_INACTIVE ||
1200 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1201 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)
1202 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1203 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1204 orig_node->orig, neigh_node->addr,
1205 if_incoming->net_dev->name);
1207 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1208 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1209 orig_node->orig, neigh_node->addr,
1210 jiffies_to_msecs(last_seen));
1212 neigh_purged = true;
1214 hlist_del_rcu(&neigh_node->list);
1215 batadv_neigh_node_put(neigh_node);
1217 /* only necessary if not the whole neighbor is to be
1218 * deleted, but some interface has been removed.
1220 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
1224 spin_unlock_bh(&orig_node->neigh_list_lock);
1225 return neigh_purged;
1229 * batadv_find_best_neighbor() - finds the best neighbor after purging
1230 * @bat_priv: the bat priv with all the soft interface information
1231 * @orig_node: orig node which is to be checked
1232 * @if_outgoing: the interface for which the metric should be compared
1234 * Return: the current best neighbor, with refcount increased.
1236 static struct batadv_neigh_node *
1237 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1238 struct batadv_orig_node *orig_node,
1239 struct batadv_hard_iface *if_outgoing)
1241 struct batadv_neigh_node *best = NULL, *neigh;
1242 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1245 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1246 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1250 if (!kref_get_unless_zero(&neigh->refcount))
1254 batadv_neigh_node_put(best);
1264 * batadv_purge_orig_node() - purges obsolete information from an orig_node
1265 * @bat_priv: the bat priv with all the soft interface information
1266 * @orig_node: orig node which is to be checked
1268 * This function checks if the orig_node or substructures of it have become
1269 * obsolete, and purges this information if that's the case.
1271 * Return: true if the orig_node is to be removed, false otherwise.
1273 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1274 struct batadv_orig_node *orig_node)
1276 struct batadv_neigh_node *best_neigh_node;
1277 struct batadv_hard_iface *hard_iface;
1278 bool changed_ifinfo, changed_neigh;
1280 if (batadv_has_timed_out(orig_node->last_seen,
1281 2 * BATADV_PURGE_TIMEOUT)) {
1282 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1283 "Originator timeout: originator %pM, last_seen %u\n",
1285 jiffies_to_msecs(orig_node->last_seen));
1288 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1289 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
1291 if (!changed_ifinfo && !changed_neigh)
1294 /* first for NULL ... */
1295 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1297 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1299 if (best_neigh_node)
1300 batadv_neigh_node_put(best_neigh_node);
1302 /* ... then for all other interfaces. */
1304 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1305 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1308 if (hard_iface->soft_iface != bat_priv->soft_iface)
1311 if (!kref_get_unless_zero(&hard_iface->refcount))
1314 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1317 batadv_update_route(bat_priv, orig_node, hard_iface,
1319 if (best_neigh_node)
1320 batadv_neigh_node_put(best_neigh_node);
1322 batadv_hardif_put(hard_iface);
1330 * batadv_purge_orig_ref() - Purge all outdated originators
1331 * @bat_priv: the bat priv with all the soft interface information
1333 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1335 struct batadv_hashtable *hash = bat_priv->orig_hash;
1336 struct hlist_node *node_tmp;
1337 struct hlist_head *head;
1338 spinlock_t *list_lock; /* spinlock to protect write access */
1339 struct batadv_orig_node *orig_node;
1345 /* for all origins... */
1346 for (i = 0; i < hash->size; i++) {
1347 head = &hash->table[i];
1348 list_lock = &hash->list_locks[i];
1350 spin_lock_bh(list_lock);
1351 hlist_for_each_entry_safe(orig_node, node_tmp,
1353 if (batadv_purge_orig_node(bat_priv, orig_node)) {
1354 batadv_gw_node_delete(bat_priv, orig_node);
1355 hlist_del_rcu(&orig_node->hash_entry);
1356 batadv_tt_global_del_orig(orig_node->bat_priv,
1358 "originator timed out");
1359 batadv_orig_node_put(orig_node);
1363 batadv_frag_purge_orig(orig_node,
1364 batadv_frag_check_entry);
1366 spin_unlock_bh(list_lock);
1369 batadv_gw_election(bat_priv);
1372 static void batadv_purge_orig(struct work_struct *work)
1374 struct delayed_work *delayed_work;
1375 struct batadv_priv *bat_priv;
1377 delayed_work = to_delayed_work(work);
1378 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1379 batadv_purge_orig_ref(bat_priv);
1380 queue_delayed_work(batadv_event_workqueue,
1381 &bat_priv->orig_work,
1382 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1385 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1388 * batadv_orig_seq_print_text() - Print the originator table in a seq file
1389 * @seq: seq file to print on
1394 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1396 struct net_device *net_dev = (struct net_device *)seq->private;
1397 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1398 struct batadv_hard_iface *primary_if;
1400 primary_if = batadv_seq_print_text_primary_if_get(seq);
1404 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1405 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1406 primary_if->net_dev->dev_addr, net_dev->name,
1407 bat_priv->algo_ops->name);
1409 batadv_hardif_put(primary_if);
1411 if (!bat_priv->algo_ops->orig.print) {
1413 "No printing function for this routing protocol\n");
1417 bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
1423 * batadv_orig_hardif_seq_print_text() - writes originator infos for a specific
1424 * outgoing interface
1425 * @seq: debugfs table seq_file struct
1430 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1432 struct net_device *net_dev = (struct net_device *)seq->private;
1433 struct batadv_hard_iface *hard_iface;
1434 struct batadv_priv *bat_priv;
1436 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1438 if (!hard_iface || !hard_iface->soft_iface) {
1439 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1443 bat_priv = netdev_priv(hard_iface->soft_iface);
1444 if (!bat_priv->algo_ops->orig.print) {
1446 "No printing function for this routing protocol\n");
1450 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1451 seq_puts(seq, "Interface not active\n");
1455 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1456 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1457 hard_iface->net_dev->dev_addr,
1458 hard_iface->soft_iface->name, bat_priv->algo_ops->name);
1460 bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
1464 batadv_hardif_put(hard_iface);
1470 * batadv_orig_dump() - Dump to netlink the originator infos for a specific
1471 * outgoing interface
1472 * @msg: message to dump into
1473 * @cb: parameters for the dump
1475 * Return: 0 or error value
1477 int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1479 struct net *net = sock_net(cb->skb->sk);
1480 struct net_device *soft_iface;
1481 struct net_device *hard_iface = NULL;
1482 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1483 struct batadv_priv *bat_priv;
1484 struct batadv_hard_iface *primary_if = NULL;
1486 int ifindex, hard_ifindex;
1488 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1492 soft_iface = dev_get_by_index(net, ifindex);
1493 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1498 bat_priv = netdev_priv(soft_iface);
1500 primary_if = batadv_primary_if_get_selected(bat_priv);
1501 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1506 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1507 BATADV_ATTR_HARD_IFINDEX);
1509 hard_iface = dev_get_by_index(net, hard_ifindex);
1511 hardif = batadv_hardif_get_by_netdev(hard_iface);
1518 if (hardif->soft_iface != soft_iface) {
1524 if (!bat_priv->algo_ops->orig.dump) {
1529 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1535 batadv_hardif_put(hardif);
1537 dev_put(hard_iface);
1539 batadv_hardif_put(primary_if);
1541 dev_put(soft_iface);