1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2009-2020 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 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_originator_init() - Initialize all originator structures
192 * @bat_priv: the bat priv with all the soft interface information
194 * Return: 0 on success or negative error number in case of failure
196 int batadv_originator_init(struct batadv_priv *bat_priv)
198 if (bat_priv->orig_hash)
201 bat_priv->orig_hash = batadv_hash_new(1024);
203 if (!bat_priv->orig_hash)
206 batadv_hash_set_lock_class(bat_priv->orig_hash,
207 &batadv_orig_hash_lock_class_key);
209 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
210 queue_delayed_work(batadv_event_workqueue,
211 &bat_priv->orig_work,
212 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
221 * batadv_neigh_ifinfo_release() - release neigh_ifinfo from lists and queue for
222 * free after rcu grace period
223 * @ref: kref pointer of the neigh_ifinfo
225 void batadv_neigh_ifinfo_release(struct kref *ref)
227 struct batadv_neigh_ifinfo *neigh_ifinfo;
229 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
231 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
232 batadv_hardif_put(neigh_ifinfo->if_outgoing);
234 kfree_rcu(neigh_ifinfo, rcu);
238 * batadv_hardif_neigh_release() - release hardif neigh node from lists and
239 * queue for free after rcu grace period
240 * @ref: kref pointer of the neigh_node
242 void batadv_hardif_neigh_release(struct kref *ref)
244 struct batadv_hardif_neigh_node *hardif_neigh;
246 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
249 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
250 hlist_del_init_rcu(&hardif_neigh->list);
251 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
253 batadv_hardif_put(hardif_neigh->if_incoming);
254 kfree_rcu(hardif_neigh, rcu);
258 * batadv_neigh_node_release() - release neigh_node from lists and queue for
259 * free after rcu grace period
260 * @ref: kref pointer of the neigh_node
262 void batadv_neigh_node_release(struct kref *ref)
264 struct hlist_node *node_tmp;
265 struct batadv_neigh_node *neigh_node;
266 struct batadv_neigh_ifinfo *neigh_ifinfo;
268 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
270 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
271 &neigh_node->ifinfo_list, list) {
272 batadv_neigh_ifinfo_put(neigh_ifinfo);
275 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
277 batadv_hardif_put(neigh_node->if_incoming);
279 kfree_rcu(neigh_node, rcu);
283 * batadv_orig_router_get() - router to the originator depending on iface
284 * @orig_node: the orig node for the router
285 * @if_outgoing: the interface where the payload packet has been received or
286 * the OGM should be sent to
288 * Return: the neighbor which should be the router for this orig_node/iface.
290 * The object is returned with refcounter increased by 1.
292 struct batadv_neigh_node *
293 batadv_orig_router_get(struct batadv_orig_node *orig_node,
294 const struct batadv_hard_iface *if_outgoing)
296 struct batadv_orig_ifinfo *orig_ifinfo;
297 struct batadv_neigh_node *router = NULL;
300 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
301 if (orig_ifinfo->if_outgoing != if_outgoing)
304 router = rcu_dereference(orig_ifinfo->router);
308 if (router && !kref_get_unless_zero(&router->refcount))
316 * batadv_orig_ifinfo_get() - find the ifinfo from an orig_node
317 * @orig_node: the orig node to be queried
318 * @if_outgoing: the interface for which the ifinfo should be acquired
320 * Return: the requested orig_ifinfo or NULL if not found.
322 * The object is returned with refcounter increased by 1.
324 struct batadv_orig_ifinfo *
325 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
326 struct batadv_hard_iface *if_outgoing)
328 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
331 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
333 if (tmp->if_outgoing != if_outgoing)
336 if (!kref_get_unless_zero(&tmp->refcount))
348 * batadv_orig_ifinfo_new() - search and possibly create an orig_ifinfo object
349 * @orig_node: the orig node to be queried
350 * @if_outgoing: the interface for which the ifinfo should be acquired
352 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
353 * interface otherwise. The object is created and added to the list
354 * if it does not exist.
356 * The object is returned with refcounter increased by 1.
358 struct batadv_orig_ifinfo *
359 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
360 struct batadv_hard_iface *if_outgoing)
362 struct batadv_orig_ifinfo *orig_ifinfo;
363 unsigned long reset_time;
365 spin_lock_bh(&orig_node->neigh_list_lock);
367 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
371 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
375 if (if_outgoing != BATADV_IF_DEFAULT)
376 kref_get(&if_outgoing->refcount);
378 reset_time = jiffies - 1;
379 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
380 orig_ifinfo->batman_seqno_reset = reset_time;
381 orig_ifinfo->if_outgoing = if_outgoing;
382 INIT_HLIST_NODE(&orig_ifinfo->list);
383 kref_init(&orig_ifinfo->refcount);
385 kref_get(&orig_ifinfo->refcount);
386 hlist_add_head_rcu(&orig_ifinfo->list,
387 &orig_node->ifinfo_list);
389 spin_unlock_bh(&orig_node->neigh_list_lock);
394 * batadv_neigh_ifinfo_get() - find the ifinfo from an neigh_node
395 * @neigh: the neigh node to be queried
396 * @if_outgoing: the interface for which the ifinfo should be acquired
398 * The object is returned with refcounter increased by 1.
400 * Return: the requested neigh_ifinfo or NULL if not found
402 struct batadv_neigh_ifinfo *
403 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
404 struct batadv_hard_iface *if_outgoing)
406 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
410 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
412 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
415 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
418 neigh_ifinfo = tmp_neigh_ifinfo;
427 * batadv_neigh_ifinfo_new() - search and possibly create an neigh_ifinfo object
428 * @neigh: the neigh node to be queried
429 * @if_outgoing: the interface for which the ifinfo should be acquired
431 * Return: NULL in case of failure or the neigh_ifinfo object for the
432 * if_outgoing interface otherwise. The object is created and added to the list
433 * if it does not exist.
435 * The object is returned with refcounter increased by 1.
437 struct batadv_neigh_ifinfo *
438 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
439 struct batadv_hard_iface *if_outgoing)
441 struct batadv_neigh_ifinfo *neigh_ifinfo;
443 spin_lock_bh(&neigh->ifinfo_lock);
445 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
449 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
454 kref_get(&if_outgoing->refcount);
456 INIT_HLIST_NODE(&neigh_ifinfo->list);
457 kref_init(&neigh_ifinfo->refcount);
458 neigh_ifinfo->if_outgoing = if_outgoing;
460 kref_get(&neigh_ifinfo->refcount);
461 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
464 spin_unlock_bh(&neigh->ifinfo_lock);
470 * batadv_neigh_node_get() - retrieve a neighbour from the list
471 * @orig_node: originator which the neighbour belongs to
472 * @hard_iface: the interface where this neighbour is connected to
473 * @addr: the address of the neighbour
475 * Looks for and possibly returns a neighbour belonging to this originator list
476 * which is connected through the provided hard interface.
478 * Return: neighbor when found. Otherwise NULL
480 static struct batadv_neigh_node *
481 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
482 const struct batadv_hard_iface *hard_iface,
485 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
488 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
489 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
492 if (tmp_neigh_node->if_incoming != hard_iface)
495 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
498 res = tmp_neigh_node;
507 * batadv_hardif_neigh_create() - create a hardif neighbour node
508 * @hard_iface: the interface this neighbour is connected to
509 * @neigh_addr: the interface address of the neighbour to retrieve
510 * @orig_node: originator object representing the neighbour
512 * Return: the hardif neighbour node if found or created or NULL otherwise.
514 static struct batadv_hardif_neigh_node *
515 batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
516 const u8 *neigh_addr,
517 struct batadv_orig_node *orig_node)
519 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
520 struct batadv_hardif_neigh_node *hardif_neigh;
522 spin_lock_bh(&hard_iface->neigh_list_lock);
524 /* check if neighbor hasn't been added in the meantime */
525 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
529 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
533 kref_get(&hard_iface->refcount);
534 INIT_HLIST_NODE(&hardif_neigh->list);
535 ether_addr_copy(hardif_neigh->addr, neigh_addr);
536 ether_addr_copy(hardif_neigh->orig, orig_node->orig);
537 hardif_neigh->if_incoming = hard_iface;
538 hardif_neigh->last_seen = jiffies;
540 kref_init(&hardif_neigh->refcount);
542 if (bat_priv->algo_ops->neigh.hardif_init)
543 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
545 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
548 spin_unlock_bh(&hard_iface->neigh_list_lock);
553 * batadv_hardif_neigh_get_or_create() - retrieve or create a hardif neighbour
555 * @hard_iface: the interface this neighbour is connected to
556 * @neigh_addr: the interface address of the neighbour to retrieve
557 * @orig_node: originator object representing the neighbour
559 * Return: the hardif neighbour node if found or created or NULL otherwise.
561 static struct batadv_hardif_neigh_node *
562 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
563 const u8 *neigh_addr,
564 struct batadv_orig_node *orig_node)
566 struct batadv_hardif_neigh_node *hardif_neigh;
568 /* first check without locking to avoid the overhead */
569 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
573 return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
577 * batadv_hardif_neigh_get() - retrieve a hardif neighbour from the list
578 * @hard_iface: the interface where this neighbour is connected to
579 * @neigh_addr: the address of the neighbour
581 * Looks for and possibly returns a neighbour belonging to this hard interface.
583 * Return: neighbor when found. Otherwise NULL
585 struct batadv_hardif_neigh_node *
586 batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
587 const u8 *neigh_addr)
589 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
592 hlist_for_each_entry_rcu(tmp_hardif_neigh,
593 &hard_iface->neigh_list, list) {
594 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
597 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
600 hardif_neigh = tmp_hardif_neigh;
609 * batadv_neigh_node_create() - create a neigh node object
610 * @orig_node: originator object representing the neighbour
611 * @hard_iface: the interface where the neighbour is connected to
612 * @neigh_addr: the mac address of the neighbour interface
614 * Allocates a new neigh_node object and initialises all the generic fields.
616 * Return: the neighbour node if found or created or NULL otherwise.
618 static struct batadv_neigh_node *
619 batadv_neigh_node_create(struct batadv_orig_node *orig_node,
620 struct batadv_hard_iface *hard_iface,
621 const u8 *neigh_addr)
623 struct batadv_neigh_node *neigh_node;
624 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
626 spin_lock_bh(&orig_node->neigh_list_lock);
628 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
632 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
633 neigh_addr, orig_node);
637 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
641 INIT_HLIST_NODE(&neigh_node->list);
642 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
643 spin_lock_init(&neigh_node->ifinfo_lock);
645 kref_get(&hard_iface->refcount);
646 ether_addr_copy(neigh_node->addr, neigh_addr);
647 neigh_node->if_incoming = hard_iface;
648 neigh_node->orig_node = orig_node;
649 neigh_node->last_seen = jiffies;
651 /* increment unique neighbor refcount */
652 kref_get(&hardif_neigh->refcount);
653 neigh_node->hardif_neigh = hardif_neigh;
655 /* extra reference for return */
656 kref_init(&neigh_node->refcount);
658 kref_get(&neigh_node->refcount);
659 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
661 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
662 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
663 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
666 spin_unlock_bh(&orig_node->neigh_list_lock);
669 batadv_hardif_neigh_put(hardif_neigh);
674 * batadv_neigh_node_get_or_create() - retrieve or create a neigh node object
675 * @orig_node: originator object representing the neighbour
676 * @hard_iface: the interface where the neighbour is connected to
677 * @neigh_addr: the mac address of the neighbour interface
679 * Return: the neighbour node if found or created or NULL otherwise.
681 struct batadv_neigh_node *
682 batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
683 struct batadv_hard_iface *hard_iface,
684 const u8 *neigh_addr)
686 struct batadv_neigh_node *neigh_node;
688 /* first check without locking to avoid the overhead */
689 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
693 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
696 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
698 * batadv_hardif_neigh_seq_print_text() - print the single hop neighbour list
699 * @seq: neighbour table seq_file struct
704 int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
706 struct net_device *net_dev = (struct net_device *)seq->private;
707 struct batadv_priv *bat_priv = netdev_priv(net_dev);
708 struct batadv_hard_iface *primary_if;
710 primary_if = batadv_seq_print_text_primary_if_get(seq);
714 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
715 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
716 primary_if->net_dev->dev_addr, net_dev->name,
717 bat_priv->algo_ops->name);
719 batadv_hardif_put(primary_if);
721 if (!bat_priv->algo_ops->neigh.print) {
723 "No printing function for this routing protocol\n");
727 bat_priv->algo_ops->neigh.print(bat_priv, seq);
733 * batadv_hardif_neigh_dump() - Dump to netlink the neighbor infos for a
734 * specific outgoing interface
735 * @msg: message to dump into
736 * @cb: parameters for the dump
738 * Return: 0 or error value
740 int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
742 struct net *net = sock_net(cb->skb->sk);
743 struct net_device *soft_iface;
744 struct net_device *hard_iface = NULL;
745 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
746 struct batadv_priv *bat_priv;
747 struct batadv_hard_iface *primary_if = NULL;
749 int ifindex, hard_ifindex;
751 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
755 soft_iface = dev_get_by_index(net, ifindex);
756 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
761 bat_priv = netdev_priv(soft_iface);
763 primary_if = batadv_primary_if_get_selected(bat_priv);
764 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
769 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
770 BATADV_ATTR_HARD_IFINDEX);
772 hard_iface = dev_get_by_index(net, hard_ifindex);
774 hardif = batadv_hardif_get_by_netdev(hard_iface);
781 if (hardif->soft_iface != soft_iface) {
787 if (!bat_priv->algo_ops->neigh.dump) {
792 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
798 batadv_hardif_put(hardif);
802 batadv_hardif_put(primary_if);
810 * batadv_orig_ifinfo_release() - release orig_ifinfo from lists and queue for
811 * free after rcu grace period
812 * @ref: kref pointer of the orig_ifinfo
814 void batadv_orig_ifinfo_release(struct kref *ref)
816 struct batadv_orig_ifinfo *orig_ifinfo;
817 struct batadv_neigh_node *router;
819 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
821 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
822 batadv_hardif_put(orig_ifinfo->if_outgoing);
824 /* this is the last reference to this object */
825 router = rcu_dereference_protected(orig_ifinfo->router, true);
827 batadv_neigh_node_put(router);
829 kfree_rcu(orig_ifinfo, rcu);
833 * batadv_orig_node_free_rcu() - free the orig_node
834 * @rcu: rcu pointer of the orig_node
836 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
838 struct batadv_orig_node *orig_node;
840 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
842 batadv_mcast_purge_orig(orig_node);
844 batadv_frag_purge_orig(orig_node, NULL);
846 kfree(orig_node->tt_buff);
851 * batadv_orig_node_release() - release orig_node from lists and queue for
852 * free after rcu grace period
853 * @ref: kref pointer of the orig_node
855 void batadv_orig_node_release(struct kref *ref)
857 struct hlist_node *node_tmp;
858 struct batadv_neigh_node *neigh_node;
859 struct batadv_orig_node *orig_node;
860 struct batadv_orig_ifinfo *orig_ifinfo;
861 struct batadv_orig_node_vlan *vlan;
862 struct batadv_orig_ifinfo *last_candidate;
864 orig_node = container_of(ref, struct batadv_orig_node, refcount);
866 spin_lock_bh(&orig_node->neigh_list_lock);
868 /* for all neighbors towards this originator ... */
869 hlist_for_each_entry_safe(neigh_node, node_tmp,
870 &orig_node->neigh_list, list) {
871 hlist_del_rcu(&neigh_node->list);
872 batadv_neigh_node_put(neigh_node);
875 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
876 &orig_node->ifinfo_list, list) {
877 hlist_del_rcu(&orig_ifinfo->list);
878 batadv_orig_ifinfo_put(orig_ifinfo);
881 last_candidate = orig_node->last_bonding_candidate;
882 orig_node->last_bonding_candidate = NULL;
883 spin_unlock_bh(&orig_node->neigh_list_lock);
886 batadv_orig_ifinfo_put(last_candidate);
888 spin_lock_bh(&orig_node->vlan_list_lock);
889 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
890 hlist_del_rcu(&vlan->list);
891 batadv_orig_node_vlan_put(vlan);
893 spin_unlock_bh(&orig_node->vlan_list_lock);
896 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
898 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
902 * batadv_originator_free() - Free all originator structures
903 * @bat_priv: the bat priv with all the soft interface information
905 void batadv_originator_free(struct batadv_priv *bat_priv)
907 struct batadv_hashtable *hash = bat_priv->orig_hash;
908 struct hlist_node *node_tmp;
909 struct hlist_head *head;
910 spinlock_t *list_lock; /* spinlock to protect write access */
911 struct batadv_orig_node *orig_node;
917 cancel_delayed_work_sync(&bat_priv->orig_work);
919 bat_priv->orig_hash = NULL;
921 for (i = 0; i < hash->size; i++) {
922 head = &hash->table[i];
923 list_lock = &hash->list_locks[i];
925 spin_lock_bh(list_lock);
926 hlist_for_each_entry_safe(orig_node, node_tmp,
928 hlist_del_rcu(&orig_node->hash_entry);
929 batadv_orig_node_put(orig_node);
931 spin_unlock_bh(list_lock);
934 batadv_hash_destroy(hash);
938 * batadv_orig_node_new() - creates a new orig_node
939 * @bat_priv: the bat priv with all the soft interface information
940 * @addr: the mac address of the originator
942 * Creates a new originator object and initialises all the generic fields.
943 * The new object is not added to the originator list.
945 * Return: the newly created object or NULL on failure.
947 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
950 struct batadv_orig_node *orig_node;
951 struct batadv_orig_node_vlan *vlan;
952 unsigned long reset_time;
955 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
956 "Creating new originator: %pM\n", addr);
958 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
962 INIT_HLIST_HEAD(&orig_node->neigh_list);
963 INIT_HLIST_HEAD(&orig_node->vlan_list);
964 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
965 spin_lock_init(&orig_node->bcast_seqno_lock);
966 spin_lock_init(&orig_node->neigh_list_lock);
967 spin_lock_init(&orig_node->tt_buff_lock);
968 spin_lock_init(&orig_node->tt_lock);
969 spin_lock_init(&orig_node->vlan_list_lock);
971 batadv_nc_init_orig(orig_node);
973 /* extra reference for return */
974 kref_init(&orig_node->refcount);
976 orig_node->bat_priv = bat_priv;
977 ether_addr_copy(orig_node->orig, addr);
978 batadv_dat_init_orig_node_addr(orig_node);
979 atomic_set(&orig_node->last_ttvn, 0);
980 orig_node->tt_buff = NULL;
981 orig_node->tt_buff_len = 0;
982 orig_node->last_seen = jiffies;
983 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
984 orig_node->bcast_seqno_reset = reset_time;
986 #ifdef CONFIG_BATMAN_ADV_MCAST
987 orig_node->mcast_flags = BATADV_MCAST_WANT_NO_RTR4;
988 orig_node->mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
989 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
990 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
991 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
992 spin_lock_init(&orig_node->mcast_handler_lock);
995 /* create a vlan object for the "untagged" LAN */
996 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
999 /* batadv_orig_node_vlan_new() increases the refcounter.
1000 * Immediately release vlan since it is not needed anymore in this
1003 batadv_orig_node_vlan_put(vlan);
1005 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
1006 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
1007 spin_lock_init(&orig_node->fragments[i].lock);
1008 orig_node->fragments[i].size = 0;
1018 * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor
1019 * @bat_priv: the bat priv with all the soft interface information
1020 * @neigh: orig node which is to be checked
1023 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1024 struct batadv_neigh_node *neigh)
1026 struct batadv_neigh_ifinfo *neigh_ifinfo;
1027 struct batadv_hard_iface *if_outgoing;
1028 struct hlist_node *node_tmp;
1030 spin_lock_bh(&neigh->ifinfo_lock);
1032 /* for all ifinfo objects for this neighinator */
1033 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1034 &neigh->ifinfo_list, list) {
1035 if_outgoing = neigh_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 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1049 neigh->addr, if_outgoing->net_dev->name);
1051 hlist_del_rcu(&neigh_ifinfo->list);
1052 batadv_neigh_ifinfo_put(neigh_ifinfo);
1055 spin_unlock_bh(&neigh->ifinfo_lock);
1059 * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator
1060 * @bat_priv: the bat priv with all the soft interface information
1061 * @orig_node: orig node which is to be checked
1063 * Return: true if any ifinfo entry was purged, false otherwise.
1066 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1067 struct batadv_orig_node *orig_node)
1069 struct batadv_orig_ifinfo *orig_ifinfo;
1070 struct batadv_hard_iface *if_outgoing;
1071 struct hlist_node *node_tmp;
1072 bool ifinfo_purged = false;
1074 spin_lock_bh(&orig_node->neigh_list_lock);
1076 /* for all ifinfo objects for this originator */
1077 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1078 &orig_node->ifinfo_list, list) {
1079 if_outgoing = orig_ifinfo->if_outgoing;
1081 /* always keep the default interface */
1082 if (if_outgoing == BATADV_IF_DEFAULT)
1085 /* don't purge if the interface is not (going) down */
1086 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1087 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1088 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
1091 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1092 "router/ifinfo purge: originator %pM, iface: %s\n",
1093 orig_node->orig, if_outgoing->net_dev->name);
1095 ifinfo_purged = true;
1097 hlist_del_rcu(&orig_ifinfo->list);
1098 batadv_orig_ifinfo_put(orig_ifinfo);
1099 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1100 orig_node->last_bonding_candidate = NULL;
1101 batadv_orig_ifinfo_put(orig_ifinfo);
1105 spin_unlock_bh(&orig_node->neigh_list_lock);
1107 return ifinfo_purged;
1111 * batadv_purge_orig_neighbors() - purges neighbors from originator
1112 * @bat_priv: the bat priv with all the soft interface information
1113 * @orig_node: orig node which is to be checked
1115 * Return: true if any neighbor was purged, false otherwise
1118 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
1119 struct batadv_orig_node *orig_node)
1121 struct hlist_node *node_tmp;
1122 struct batadv_neigh_node *neigh_node;
1123 bool neigh_purged = false;
1124 unsigned long last_seen;
1125 struct batadv_hard_iface *if_incoming;
1127 spin_lock_bh(&orig_node->neigh_list_lock);
1129 /* for all neighbors towards this originator ... */
1130 hlist_for_each_entry_safe(neigh_node, node_tmp,
1131 &orig_node->neigh_list, list) {
1132 last_seen = neigh_node->last_seen;
1133 if_incoming = neigh_node->if_incoming;
1135 if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) ||
1136 if_incoming->if_status == BATADV_IF_INACTIVE ||
1137 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1138 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) {
1139 if (if_incoming->if_status == BATADV_IF_INACTIVE ||
1140 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1141 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)
1142 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1143 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1144 orig_node->orig, neigh_node->addr,
1145 if_incoming->net_dev->name);
1147 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1148 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1149 orig_node->orig, neigh_node->addr,
1150 jiffies_to_msecs(last_seen));
1152 neigh_purged = true;
1154 hlist_del_rcu(&neigh_node->list);
1155 batadv_neigh_node_put(neigh_node);
1157 /* only necessary if not the whole neighbor is to be
1158 * deleted, but some interface has been removed.
1160 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
1164 spin_unlock_bh(&orig_node->neigh_list_lock);
1165 return neigh_purged;
1169 * batadv_find_best_neighbor() - finds the best neighbor after purging
1170 * @bat_priv: the bat priv with all the soft interface information
1171 * @orig_node: orig node which is to be checked
1172 * @if_outgoing: the interface for which the metric should be compared
1174 * Return: the current best neighbor, with refcount increased.
1176 static struct batadv_neigh_node *
1177 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1178 struct batadv_orig_node *orig_node,
1179 struct batadv_hard_iface *if_outgoing)
1181 struct batadv_neigh_node *best = NULL, *neigh;
1182 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1185 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1186 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1190 if (!kref_get_unless_zero(&neigh->refcount))
1194 batadv_neigh_node_put(best);
1204 * batadv_purge_orig_node() - purges obsolete information from an orig_node
1205 * @bat_priv: the bat priv with all the soft interface information
1206 * @orig_node: orig node which is to be checked
1208 * This function checks if the orig_node or substructures of it have become
1209 * obsolete, and purges this information if that's the case.
1211 * Return: true if the orig_node is to be removed, false otherwise.
1213 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1214 struct batadv_orig_node *orig_node)
1216 struct batadv_neigh_node *best_neigh_node;
1217 struct batadv_hard_iface *hard_iface;
1218 bool changed_ifinfo, changed_neigh;
1220 if (batadv_has_timed_out(orig_node->last_seen,
1221 2 * BATADV_PURGE_TIMEOUT)) {
1222 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1223 "Originator timeout: originator %pM, last_seen %u\n",
1225 jiffies_to_msecs(orig_node->last_seen));
1228 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1229 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
1231 if (!changed_ifinfo && !changed_neigh)
1234 /* first for NULL ... */
1235 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1237 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1239 if (best_neigh_node)
1240 batadv_neigh_node_put(best_neigh_node);
1242 /* ... then for all other interfaces. */
1244 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1245 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1248 if (hard_iface->soft_iface != bat_priv->soft_iface)
1251 if (!kref_get_unless_zero(&hard_iface->refcount))
1254 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1257 batadv_update_route(bat_priv, orig_node, hard_iface,
1259 if (best_neigh_node)
1260 batadv_neigh_node_put(best_neigh_node);
1262 batadv_hardif_put(hard_iface);
1270 * batadv_purge_orig_ref() - Purge all outdated originators
1271 * @bat_priv: the bat priv with all the soft interface information
1273 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1275 struct batadv_hashtable *hash = bat_priv->orig_hash;
1276 struct hlist_node *node_tmp;
1277 struct hlist_head *head;
1278 spinlock_t *list_lock; /* spinlock to protect write access */
1279 struct batadv_orig_node *orig_node;
1285 /* for all origins... */
1286 for (i = 0; i < hash->size; i++) {
1287 head = &hash->table[i];
1288 list_lock = &hash->list_locks[i];
1290 spin_lock_bh(list_lock);
1291 hlist_for_each_entry_safe(orig_node, node_tmp,
1293 if (batadv_purge_orig_node(bat_priv, orig_node)) {
1294 batadv_gw_node_delete(bat_priv, orig_node);
1295 hlist_del_rcu(&orig_node->hash_entry);
1296 batadv_tt_global_del_orig(orig_node->bat_priv,
1298 "originator timed out");
1299 batadv_orig_node_put(orig_node);
1303 batadv_frag_purge_orig(orig_node,
1304 batadv_frag_check_entry);
1306 spin_unlock_bh(list_lock);
1309 batadv_gw_election(bat_priv);
1312 static void batadv_purge_orig(struct work_struct *work)
1314 struct delayed_work *delayed_work;
1315 struct batadv_priv *bat_priv;
1317 delayed_work = to_delayed_work(work);
1318 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1319 batadv_purge_orig_ref(bat_priv);
1320 queue_delayed_work(batadv_event_workqueue,
1321 &bat_priv->orig_work,
1322 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1325 #ifdef CONFIG_BATMAN_ADV_DEBUGFS
1328 * batadv_orig_seq_print_text() - Print the originator table in a seq file
1329 * @seq: seq file to print on
1334 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1336 struct net_device *net_dev = (struct net_device *)seq->private;
1337 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1338 struct batadv_hard_iface *primary_if;
1340 primary_if = batadv_seq_print_text_primary_if_get(seq);
1344 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1345 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1346 primary_if->net_dev->dev_addr, net_dev->name,
1347 bat_priv->algo_ops->name);
1349 batadv_hardif_put(primary_if);
1351 if (!bat_priv->algo_ops->orig.print) {
1353 "No printing function for this routing protocol\n");
1357 bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
1363 * batadv_orig_hardif_seq_print_text() - writes originator infos for a specific
1364 * outgoing interface
1365 * @seq: debugfs table seq_file struct
1370 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1372 struct net_device *net_dev = (struct net_device *)seq->private;
1373 struct batadv_hard_iface *hard_iface;
1374 struct batadv_priv *bat_priv;
1376 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1378 if (!hard_iface || !hard_iface->soft_iface) {
1379 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1383 bat_priv = netdev_priv(hard_iface->soft_iface);
1384 if (!bat_priv->algo_ops->orig.print) {
1386 "No printing function for this routing protocol\n");
1390 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1391 seq_puts(seq, "Interface not active\n");
1395 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1396 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1397 hard_iface->net_dev->dev_addr,
1398 hard_iface->soft_iface->name, bat_priv->algo_ops->name);
1400 bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
1404 batadv_hardif_put(hard_iface);
1410 * batadv_orig_dump() - Dump to netlink the originator infos for a specific
1411 * outgoing interface
1412 * @msg: message to dump into
1413 * @cb: parameters for the dump
1415 * Return: 0 or error value
1417 int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1419 struct net *net = sock_net(cb->skb->sk);
1420 struct net_device *soft_iface;
1421 struct net_device *hard_iface = NULL;
1422 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1423 struct batadv_priv *bat_priv;
1424 struct batadv_hard_iface *primary_if = NULL;
1426 int ifindex, hard_ifindex;
1428 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1432 soft_iface = dev_get_by_index(net, ifindex);
1433 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1438 bat_priv = netdev_priv(soft_iface);
1440 primary_if = batadv_primary_if_get_selected(bat_priv);
1441 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1446 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1447 BATADV_ATTR_HARD_IFINDEX);
1449 hard_iface = dev_get_by_index(net, hard_ifindex);
1451 hardif = batadv_hardif_get_by_netdev(hard_iface);
1458 if (hardif->soft_iface != soft_iface) {
1464 if (!bat_priv->algo_ops->orig.dump) {
1469 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1475 batadv_hardif_put(hardif);
1477 dev_put(hard_iface);
1479 batadv_hardif_put(primary_if);
1481 dev_put(soft_iface);