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/if_vlan.h>
16 #include <linux/jiffies.h>
17 #include <linux/kref.h>
18 #include <linux/list.h>
19 #include <linux/lockdep.h>
20 #include <linux/netdevice.h>
21 #include <linux/netlink.h>
22 #include <linux/rculist.h>
23 #include <linux/rcupdate.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_vlan_id_valid() - check if vlan id is in valid batman-adv encoding
137 * @vid: the VLAN identifier
139 * Return: true when either no vlan is set or if VLAN is in correct range,
142 static bool batadv_vlan_id_valid(unsigned short vid
)
144 unsigned short non_vlan
= vid
& ~(BATADV_VLAN_HAS_TAG
| VLAN_VID_MASK
);
149 if (!(vid
& BATADV_VLAN_HAS_TAG
))
159 * batadv_orig_node_vlan_new() - search and possibly create an orig_node_vlan
161 * @orig_node: the originator serving the VLAN
162 * @vid: the VLAN identifier
164 * Return: NULL in case of failure or the vlan object identified by vid and
165 * belonging to orig_node otherwise. The object is created and added to the list
166 * if it does not exist.
168 * The object is returned with refcounter increased by 1.
170 struct batadv_orig_node_vlan
*
171 batadv_orig_node_vlan_new(struct batadv_orig_node
*orig_node
,
174 struct batadv_orig_node_vlan
*vlan
;
176 if (!batadv_vlan_id_valid(vid
))
179 spin_lock_bh(&orig_node
->vlan_list_lock
);
181 /* first look if an object for this vid already exists */
182 vlan
= batadv_orig_node_vlan_get(orig_node
, vid
);
186 vlan
= kzalloc(sizeof(*vlan
), GFP_ATOMIC
);
190 kref_init(&vlan
->refcount
);
193 kref_get(&vlan
->refcount
);
194 hlist_add_head_rcu(&vlan
->list
, &orig_node
->vlan_list
);
197 spin_unlock_bh(&orig_node
->vlan_list_lock
);
203 * batadv_orig_node_vlan_release() - release originator-vlan object from lists
204 * and queue for free after rcu grace period
205 * @ref: kref pointer of the originator-vlan object
207 void batadv_orig_node_vlan_release(struct kref
*ref
)
209 struct batadv_orig_node_vlan
*orig_vlan
;
211 orig_vlan
= container_of(ref
, struct batadv_orig_node_vlan
, refcount
);
213 kfree_rcu(orig_vlan
, rcu
);
217 * batadv_originator_init() - Initialize all originator structures
218 * @bat_priv: the bat priv with all the soft interface information
220 * Return: 0 on success or negative error number in case of failure
222 int batadv_originator_init(struct batadv_priv
*bat_priv
)
224 if (bat_priv
->orig_hash
)
227 bat_priv
->orig_hash
= batadv_hash_new(1024);
229 if (!bat_priv
->orig_hash
)
232 batadv_hash_set_lock_class(bat_priv
->orig_hash
,
233 &batadv_orig_hash_lock_class_key
);
235 INIT_DELAYED_WORK(&bat_priv
->orig_work
, batadv_purge_orig
);
236 queue_delayed_work(batadv_event_workqueue
,
237 &bat_priv
->orig_work
,
238 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD
));
247 * batadv_neigh_ifinfo_release() - release neigh_ifinfo from lists and queue for
248 * free after rcu grace period
249 * @ref: kref pointer of the neigh_ifinfo
251 void batadv_neigh_ifinfo_release(struct kref
*ref
)
253 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
255 neigh_ifinfo
= container_of(ref
, struct batadv_neigh_ifinfo
, refcount
);
257 if (neigh_ifinfo
->if_outgoing
!= BATADV_IF_DEFAULT
)
258 batadv_hardif_put(neigh_ifinfo
->if_outgoing
);
260 kfree_rcu(neigh_ifinfo
, rcu
);
264 * batadv_hardif_neigh_release() - release hardif neigh node from lists and
265 * queue for free after rcu grace period
266 * @ref: kref pointer of the neigh_node
268 void batadv_hardif_neigh_release(struct kref
*ref
)
270 struct batadv_hardif_neigh_node
*hardif_neigh
;
272 hardif_neigh
= container_of(ref
, struct batadv_hardif_neigh_node
,
275 spin_lock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
276 hlist_del_init_rcu(&hardif_neigh
->list
);
277 spin_unlock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
279 batadv_hardif_put(hardif_neigh
->if_incoming
);
280 kfree_rcu(hardif_neigh
, rcu
);
284 * batadv_neigh_node_release() - release neigh_node from lists and queue for
285 * free after rcu grace period
286 * @ref: kref pointer of the neigh_node
288 void batadv_neigh_node_release(struct kref
*ref
)
290 struct hlist_node
*node_tmp
;
291 struct batadv_neigh_node
*neigh_node
;
292 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
294 neigh_node
= container_of(ref
, struct batadv_neigh_node
, refcount
);
296 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
297 &neigh_node
->ifinfo_list
, list
) {
298 batadv_neigh_ifinfo_put(neigh_ifinfo
);
301 batadv_hardif_neigh_put(neigh_node
->hardif_neigh
);
303 batadv_hardif_put(neigh_node
->if_incoming
);
305 kfree_rcu(neigh_node
, rcu
);
309 * batadv_orig_router_get() - router to the originator depending on iface
310 * @orig_node: the orig node for the router
311 * @if_outgoing: the interface where the payload packet has been received or
312 * the OGM should be sent to
314 * Return: the neighbor which should be the router for this orig_node/iface.
316 * The object is returned with refcounter increased by 1.
318 struct batadv_neigh_node
*
319 batadv_orig_router_get(struct batadv_orig_node
*orig_node
,
320 const struct batadv_hard_iface
*if_outgoing
)
322 struct batadv_orig_ifinfo
*orig_ifinfo
;
323 struct batadv_neigh_node
*router
= NULL
;
326 hlist_for_each_entry_rcu(orig_ifinfo
, &orig_node
->ifinfo_list
, list
) {
327 if (orig_ifinfo
->if_outgoing
!= if_outgoing
)
330 router
= rcu_dereference(orig_ifinfo
->router
);
334 if (router
&& !kref_get_unless_zero(&router
->refcount
))
342 * batadv_orig_to_router() - get next hop neighbor to an orig address
343 * @bat_priv: the bat priv with all the soft interface information
344 * @orig_addr: the originator MAC address to search the best next hop router for
345 * @if_outgoing: the interface where the payload packet has been received or
346 * the OGM should be sent to
348 * Return: A neighbor node which is the best router towards the given originator
351 struct batadv_neigh_node
*
352 batadv_orig_to_router(struct batadv_priv
*bat_priv
, u8
*orig_addr
,
353 struct batadv_hard_iface
*if_outgoing
)
355 struct batadv_neigh_node
*neigh_node
;
356 struct batadv_orig_node
*orig_node
;
358 orig_node
= batadv_orig_hash_find(bat_priv
, orig_addr
);
362 neigh_node
= batadv_find_router(bat_priv
, orig_node
, if_outgoing
);
363 batadv_orig_node_put(orig_node
);
369 * batadv_orig_ifinfo_get() - find the ifinfo from an orig_node
370 * @orig_node: the orig node to be queried
371 * @if_outgoing: the interface for which the ifinfo should be acquired
373 * Return: the requested orig_ifinfo or NULL if not found.
375 * The object is returned with refcounter increased by 1.
377 struct batadv_orig_ifinfo
*
378 batadv_orig_ifinfo_get(struct batadv_orig_node
*orig_node
,
379 struct batadv_hard_iface
*if_outgoing
)
381 struct batadv_orig_ifinfo
*tmp
, *orig_ifinfo
= NULL
;
384 hlist_for_each_entry_rcu(tmp
, &orig_node
->ifinfo_list
,
386 if (tmp
->if_outgoing
!= if_outgoing
)
389 if (!kref_get_unless_zero(&tmp
->refcount
))
401 * batadv_orig_ifinfo_new() - search and possibly create an orig_ifinfo object
402 * @orig_node: the orig node to be queried
403 * @if_outgoing: the interface for which the ifinfo should be acquired
405 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
406 * interface otherwise. The object is created and added to the list
407 * if it does not exist.
409 * The object is returned with refcounter increased by 1.
411 struct batadv_orig_ifinfo
*
412 batadv_orig_ifinfo_new(struct batadv_orig_node
*orig_node
,
413 struct batadv_hard_iface
*if_outgoing
)
415 struct batadv_orig_ifinfo
*orig_ifinfo
;
416 unsigned long reset_time
;
418 spin_lock_bh(&orig_node
->neigh_list_lock
);
420 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, if_outgoing
);
424 orig_ifinfo
= kzalloc(sizeof(*orig_ifinfo
), GFP_ATOMIC
);
428 if (if_outgoing
!= BATADV_IF_DEFAULT
)
429 kref_get(&if_outgoing
->refcount
);
431 reset_time
= jiffies
- 1;
432 reset_time
-= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
433 orig_ifinfo
->batman_seqno_reset
= reset_time
;
434 orig_ifinfo
->if_outgoing
= if_outgoing
;
435 INIT_HLIST_NODE(&orig_ifinfo
->list
);
436 kref_init(&orig_ifinfo
->refcount
);
438 kref_get(&orig_ifinfo
->refcount
);
439 hlist_add_head_rcu(&orig_ifinfo
->list
,
440 &orig_node
->ifinfo_list
);
442 spin_unlock_bh(&orig_node
->neigh_list_lock
);
447 * batadv_neigh_ifinfo_get() - find the ifinfo from an neigh_node
448 * @neigh: the neigh node to be queried
449 * @if_outgoing: the interface for which the ifinfo should be acquired
451 * The object is returned with refcounter increased by 1.
453 * Return: the requested neigh_ifinfo or NULL if not found
455 struct batadv_neigh_ifinfo
*
456 batadv_neigh_ifinfo_get(struct batadv_neigh_node
*neigh
,
457 struct batadv_hard_iface
*if_outgoing
)
459 struct batadv_neigh_ifinfo
*neigh_ifinfo
= NULL
,
463 hlist_for_each_entry_rcu(tmp_neigh_ifinfo
, &neigh
->ifinfo_list
,
465 if (tmp_neigh_ifinfo
->if_outgoing
!= if_outgoing
)
468 if (!kref_get_unless_zero(&tmp_neigh_ifinfo
->refcount
))
471 neigh_ifinfo
= tmp_neigh_ifinfo
;
480 * batadv_neigh_ifinfo_new() - search and possibly create an neigh_ifinfo object
481 * @neigh: the neigh node to be queried
482 * @if_outgoing: the interface for which the ifinfo should be acquired
484 * Return: NULL in case of failure or the neigh_ifinfo object for the
485 * if_outgoing interface otherwise. The object is created and added to the list
486 * if it does not exist.
488 * The object is returned with refcounter increased by 1.
490 struct batadv_neigh_ifinfo
*
491 batadv_neigh_ifinfo_new(struct batadv_neigh_node
*neigh
,
492 struct batadv_hard_iface
*if_outgoing
)
494 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
496 spin_lock_bh(&neigh
->ifinfo_lock
);
498 neigh_ifinfo
= batadv_neigh_ifinfo_get(neigh
, if_outgoing
);
502 neigh_ifinfo
= kzalloc(sizeof(*neigh_ifinfo
), GFP_ATOMIC
);
507 kref_get(&if_outgoing
->refcount
);
509 INIT_HLIST_NODE(&neigh_ifinfo
->list
);
510 kref_init(&neigh_ifinfo
->refcount
);
511 neigh_ifinfo
->if_outgoing
= if_outgoing
;
513 kref_get(&neigh_ifinfo
->refcount
);
514 hlist_add_head_rcu(&neigh_ifinfo
->list
, &neigh
->ifinfo_list
);
517 spin_unlock_bh(&neigh
->ifinfo_lock
);
523 * batadv_neigh_node_get() - retrieve a neighbour from the list
524 * @orig_node: originator which the neighbour belongs to
525 * @hard_iface: the interface where this neighbour is connected to
526 * @addr: the address of the neighbour
528 * Looks for and possibly returns a neighbour belonging to this originator list
529 * which is connected through the provided hard interface.
531 * Return: neighbor when found. Otherwise NULL
533 static struct batadv_neigh_node
*
534 batadv_neigh_node_get(const struct batadv_orig_node
*orig_node
,
535 const struct batadv_hard_iface
*hard_iface
,
538 struct batadv_neigh_node
*tmp_neigh_node
, *res
= NULL
;
541 hlist_for_each_entry_rcu(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
542 if (!batadv_compare_eth(tmp_neigh_node
->addr
, addr
))
545 if (tmp_neigh_node
->if_incoming
!= hard_iface
)
548 if (!kref_get_unless_zero(&tmp_neigh_node
->refcount
))
551 res
= tmp_neigh_node
;
560 * batadv_hardif_neigh_create() - create a hardif neighbour node
561 * @hard_iface: the interface this neighbour is connected to
562 * @neigh_addr: the interface address of the neighbour to retrieve
563 * @orig_node: originator object representing the neighbour
565 * Return: the hardif neighbour node if found or created or NULL otherwise.
567 static struct batadv_hardif_neigh_node
*
568 batadv_hardif_neigh_create(struct batadv_hard_iface
*hard_iface
,
569 const u8
*neigh_addr
,
570 struct batadv_orig_node
*orig_node
)
572 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
573 struct batadv_hardif_neigh_node
*hardif_neigh
;
575 spin_lock_bh(&hard_iface
->neigh_list_lock
);
577 /* check if neighbor hasn't been added in the meantime */
578 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
582 hardif_neigh
= kzalloc(sizeof(*hardif_neigh
), GFP_ATOMIC
);
586 kref_get(&hard_iface
->refcount
);
587 INIT_HLIST_NODE(&hardif_neigh
->list
);
588 ether_addr_copy(hardif_neigh
->addr
, neigh_addr
);
589 ether_addr_copy(hardif_neigh
->orig
, orig_node
->orig
);
590 hardif_neigh
->if_incoming
= hard_iface
;
591 hardif_neigh
->last_seen
= jiffies
;
593 kref_init(&hardif_neigh
->refcount
);
595 if (bat_priv
->algo_ops
->neigh
.hardif_init
)
596 bat_priv
->algo_ops
->neigh
.hardif_init(hardif_neigh
);
598 hlist_add_head_rcu(&hardif_neigh
->list
, &hard_iface
->neigh_list
);
601 spin_unlock_bh(&hard_iface
->neigh_list_lock
);
606 * batadv_hardif_neigh_get_or_create() - retrieve or create a hardif neighbour
608 * @hard_iface: the interface this neighbour is connected to
609 * @neigh_addr: the interface address of the neighbour to retrieve
610 * @orig_node: originator object representing the neighbour
612 * Return: the hardif neighbour node if found or created or NULL otherwise.
614 static struct batadv_hardif_neigh_node
*
615 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface
*hard_iface
,
616 const u8
*neigh_addr
,
617 struct batadv_orig_node
*orig_node
)
619 struct batadv_hardif_neigh_node
*hardif_neigh
;
621 /* first check without locking to avoid the overhead */
622 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
626 return batadv_hardif_neigh_create(hard_iface
, neigh_addr
, orig_node
);
630 * batadv_hardif_neigh_get() - retrieve a hardif neighbour from the list
631 * @hard_iface: the interface where this neighbour is connected to
632 * @neigh_addr: the address of the neighbour
634 * Looks for and possibly returns a neighbour belonging to this hard interface.
636 * Return: neighbor when found. Otherwise NULL
638 struct batadv_hardif_neigh_node
*
639 batadv_hardif_neigh_get(const struct batadv_hard_iface
*hard_iface
,
640 const u8
*neigh_addr
)
642 struct batadv_hardif_neigh_node
*tmp_hardif_neigh
, *hardif_neigh
= NULL
;
645 hlist_for_each_entry_rcu(tmp_hardif_neigh
,
646 &hard_iface
->neigh_list
, list
) {
647 if (!batadv_compare_eth(tmp_hardif_neigh
->addr
, neigh_addr
))
650 if (!kref_get_unless_zero(&tmp_hardif_neigh
->refcount
))
653 hardif_neigh
= tmp_hardif_neigh
;
662 * batadv_neigh_node_create() - create a neigh node object
663 * @orig_node: originator object representing the neighbour
664 * @hard_iface: the interface where the neighbour is connected to
665 * @neigh_addr: the mac address of the neighbour interface
667 * Allocates a new neigh_node object and initialises all the generic fields.
669 * Return: the neighbour node if found or created or NULL otherwise.
671 static struct batadv_neigh_node
*
672 batadv_neigh_node_create(struct batadv_orig_node
*orig_node
,
673 struct batadv_hard_iface
*hard_iface
,
674 const u8
*neigh_addr
)
676 struct batadv_neigh_node
*neigh_node
;
677 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
679 spin_lock_bh(&orig_node
->neigh_list_lock
);
681 neigh_node
= batadv_neigh_node_get(orig_node
, hard_iface
, neigh_addr
);
685 hardif_neigh
= batadv_hardif_neigh_get_or_create(hard_iface
,
686 neigh_addr
, orig_node
);
690 neigh_node
= kzalloc(sizeof(*neigh_node
), GFP_ATOMIC
);
694 INIT_HLIST_NODE(&neigh_node
->list
);
695 INIT_HLIST_HEAD(&neigh_node
->ifinfo_list
);
696 spin_lock_init(&neigh_node
->ifinfo_lock
);
698 kref_get(&hard_iface
->refcount
);
699 ether_addr_copy(neigh_node
->addr
, neigh_addr
);
700 neigh_node
->if_incoming
= hard_iface
;
701 neigh_node
->orig_node
= orig_node
;
702 neigh_node
->last_seen
= jiffies
;
704 /* increment unique neighbor refcount */
705 kref_get(&hardif_neigh
->refcount
);
706 neigh_node
->hardif_neigh
= hardif_neigh
;
708 /* extra reference for return */
709 kref_init(&neigh_node
->refcount
);
711 kref_get(&neigh_node
->refcount
);
712 hlist_add_head_rcu(&neigh_node
->list
, &orig_node
->neigh_list
);
714 batadv_dbg(BATADV_DBG_BATMAN
, orig_node
->bat_priv
,
715 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
716 neigh_addr
, orig_node
->orig
, hard_iface
->net_dev
->name
);
719 spin_unlock_bh(&orig_node
->neigh_list_lock
);
721 batadv_hardif_neigh_put(hardif_neigh
);
726 * batadv_neigh_node_get_or_create() - retrieve or create a neigh node object
727 * @orig_node: originator object representing the neighbour
728 * @hard_iface: the interface where the neighbour is connected to
729 * @neigh_addr: the mac address of the neighbour interface
731 * Return: the neighbour node if found or created or NULL otherwise.
733 struct batadv_neigh_node
*
734 batadv_neigh_node_get_or_create(struct batadv_orig_node
*orig_node
,
735 struct batadv_hard_iface
*hard_iface
,
736 const u8
*neigh_addr
)
738 struct batadv_neigh_node
*neigh_node
;
740 /* first check without locking to avoid the overhead */
741 neigh_node
= batadv_neigh_node_get(orig_node
, hard_iface
, neigh_addr
);
745 return batadv_neigh_node_create(orig_node
, hard_iface
, neigh_addr
);
749 * batadv_hardif_neigh_dump() - Dump to netlink the neighbor infos for a
750 * specific outgoing interface
751 * @msg: message to dump into
752 * @cb: parameters for the dump
754 * Return: 0 or error value
756 int batadv_hardif_neigh_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
)
758 struct net
*net
= sock_net(cb
->skb
->sk
);
759 struct net_device
*soft_iface
;
760 struct net_device
*hard_iface
= NULL
;
761 struct batadv_hard_iface
*hardif
= BATADV_IF_DEFAULT
;
762 struct batadv_priv
*bat_priv
;
763 struct batadv_hard_iface
*primary_if
= NULL
;
765 int ifindex
, hard_ifindex
;
767 ifindex
= batadv_netlink_get_ifindex(cb
->nlh
, BATADV_ATTR_MESH_IFINDEX
);
771 soft_iface
= dev_get_by_index(net
, ifindex
);
772 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
777 bat_priv
= netdev_priv(soft_iface
);
779 primary_if
= batadv_primary_if_get_selected(bat_priv
);
780 if (!primary_if
|| primary_if
->if_status
!= BATADV_IF_ACTIVE
) {
785 hard_ifindex
= batadv_netlink_get_ifindex(cb
->nlh
,
786 BATADV_ATTR_HARD_IFINDEX
);
788 hard_iface
= dev_get_by_index(net
, hard_ifindex
);
790 hardif
= batadv_hardif_get_by_netdev(hard_iface
);
797 if (hardif
->soft_iface
!= soft_iface
) {
803 if (!bat_priv
->algo_ops
->neigh
.dump
) {
808 bat_priv
->algo_ops
->neigh
.dump(msg
, cb
, bat_priv
, hardif
);
813 batadv_hardif_put(hardif
);
815 batadv_hardif_put(primary_if
);
822 * batadv_orig_ifinfo_release() - release orig_ifinfo from lists and queue for
823 * free after rcu grace period
824 * @ref: kref pointer of the orig_ifinfo
826 void batadv_orig_ifinfo_release(struct kref
*ref
)
828 struct batadv_orig_ifinfo
*orig_ifinfo
;
829 struct batadv_neigh_node
*router
;
831 orig_ifinfo
= container_of(ref
, struct batadv_orig_ifinfo
, refcount
);
833 if (orig_ifinfo
->if_outgoing
!= BATADV_IF_DEFAULT
)
834 batadv_hardif_put(orig_ifinfo
->if_outgoing
);
836 /* this is the last reference to this object */
837 router
= rcu_dereference_protected(orig_ifinfo
->router
, true);
838 batadv_neigh_node_put(router
);
840 kfree_rcu(orig_ifinfo
, rcu
);
844 * batadv_orig_node_free_rcu() - free the orig_node
845 * @rcu: rcu pointer of the orig_node
847 static void batadv_orig_node_free_rcu(struct rcu_head
*rcu
)
849 struct batadv_orig_node
*orig_node
;
851 orig_node
= container_of(rcu
, struct batadv_orig_node
, rcu
);
853 batadv_mcast_purge_orig(orig_node
);
855 batadv_frag_purge_orig(orig_node
, NULL
);
857 kfree(orig_node
->tt_buff
);
862 * batadv_orig_node_release() - release orig_node from lists and queue for
863 * free after rcu grace period
864 * @ref: kref pointer of the orig_node
866 void batadv_orig_node_release(struct kref
*ref
)
868 struct hlist_node
*node_tmp
;
869 struct batadv_neigh_node
*neigh_node
;
870 struct batadv_orig_node
*orig_node
;
871 struct batadv_orig_ifinfo
*orig_ifinfo
;
872 struct batadv_orig_node_vlan
*vlan
;
873 struct batadv_orig_ifinfo
*last_candidate
;
875 orig_node
= container_of(ref
, struct batadv_orig_node
, refcount
);
877 spin_lock_bh(&orig_node
->neigh_list_lock
);
879 /* for all neighbors towards this originator ... */
880 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
881 &orig_node
->neigh_list
, list
) {
882 hlist_del_rcu(&neigh_node
->list
);
883 batadv_neigh_node_put(neigh_node
);
886 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
887 &orig_node
->ifinfo_list
, list
) {
888 hlist_del_rcu(&orig_ifinfo
->list
);
889 batadv_orig_ifinfo_put(orig_ifinfo
);
892 last_candidate
= orig_node
->last_bonding_candidate
;
893 orig_node
->last_bonding_candidate
= NULL
;
894 spin_unlock_bh(&orig_node
->neigh_list_lock
);
896 batadv_orig_ifinfo_put(last_candidate
);
898 spin_lock_bh(&orig_node
->vlan_list_lock
);
899 hlist_for_each_entry_safe(vlan
, node_tmp
, &orig_node
->vlan_list
, list
) {
900 hlist_del_rcu(&vlan
->list
);
901 batadv_orig_node_vlan_put(vlan
);
903 spin_unlock_bh(&orig_node
->vlan_list_lock
);
906 batadv_nc_purge_orig(orig_node
->bat_priv
, orig_node
, NULL
);
908 call_rcu(&orig_node
->rcu
, batadv_orig_node_free_rcu
);
912 * batadv_originator_free() - Free all originator structures
913 * @bat_priv: the bat priv with all the soft interface information
915 void batadv_originator_free(struct batadv_priv
*bat_priv
)
917 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
918 struct hlist_node
*node_tmp
;
919 struct hlist_head
*head
;
920 spinlock_t
*list_lock
; /* spinlock to protect write access */
921 struct batadv_orig_node
*orig_node
;
927 cancel_delayed_work_sync(&bat_priv
->orig_work
);
929 bat_priv
->orig_hash
= NULL
;
931 for (i
= 0; i
< hash
->size
; i
++) {
932 head
= &hash
->table
[i
];
933 list_lock
= &hash
->list_locks
[i
];
935 spin_lock_bh(list_lock
);
936 hlist_for_each_entry_safe(orig_node
, node_tmp
,
938 hlist_del_rcu(&orig_node
->hash_entry
);
939 batadv_orig_node_put(orig_node
);
941 spin_unlock_bh(list_lock
);
944 batadv_hash_destroy(hash
);
948 * batadv_orig_node_new() - creates a new orig_node
949 * @bat_priv: the bat priv with all the soft interface information
950 * @addr: the mac address of the originator
952 * Creates a new originator object and initialises all the generic fields.
953 * The new object is not added to the originator list.
955 * Return: the newly created object or NULL on failure.
957 struct batadv_orig_node
*batadv_orig_node_new(struct batadv_priv
*bat_priv
,
960 struct batadv_orig_node
*orig_node
;
961 struct batadv_orig_node_vlan
*vlan
;
962 unsigned long reset_time
;
965 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
966 "Creating new originator: %pM\n", addr
);
968 orig_node
= kzalloc(sizeof(*orig_node
), GFP_ATOMIC
);
972 INIT_HLIST_HEAD(&orig_node
->neigh_list
);
973 INIT_HLIST_HEAD(&orig_node
->vlan_list
);
974 INIT_HLIST_HEAD(&orig_node
->ifinfo_list
);
975 spin_lock_init(&orig_node
->bcast_seqno_lock
);
976 spin_lock_init(&orig_node
->neigh_list_lock
);
977 spin_lock_init(&orig_node
->tt_buff_lock
);
978 spin_lock_init(&orig_node
->tt_lock
);
979 spin_lock_init(&orig_node
->vlan_list_lock
);
981 batadv_nc_init_orig(orig_node
);
983 /* extra reference for return */
984 kref_init(&orig_node
->refcount
);
986 orig_node
->bat_priv
= bat_priv
;
987 ether_addr_copy(orig_node
->orig
, addr
);
988 batadv_dat_init_orig_node_addr(orig_node
);
989 atomic_set(&orig_node
->last_ttvn
, 0);
990 orig_node
->tt_buff
= NULL
;
991 orig_node
->tt_buff_len
= 0;
992 orig_node
->last_seen
= jiffies
;
993 reset_time
= jiffies
- 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
994 orig_node
->bcast_seqno_reset
= reset_time
;
996 #ifdef CONFIG_BATMAN_ADV_MCAST
997 orig_node
->mcast_flags
= BATADV_MCAST_WANT_NO_RTR4
;
998 orig_node
->mcast_flags
|= BATADV_MCAST_WANT_NO_RTR6
;
999 orig_node
->mcast_flags
|= BATADV_MCAST_HAVE_MC_PTYPE_CAPA
;
1000 INIT_HLIST_NODE(&orig_node
->mcast_want_all_unsnoopables_node
);
1001 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv4_node
);
1002 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv6_node
);
1003 spin_lock_init(&orig_node
->mcast_handler_lock
);
1006 /* create a vlan object for the "untagged" LAN */
1007 vlan
= batadv_orig_node_vlan_new(orig_node
, BATADV_NO_FLAGS
);
1009 goto free_orig_node
;
1010 /* batadv_orig_node_vlan_new() increases the refcounter.
1011 * Immediately release vlan since it is not needed anymore in this
1014 batadv_orig_node_vlan_put(vlan
);
1016 for (i
= 0; i
< BATADV_FRAG_BUFFER_COUNT
; i
++) {
1017 INIT_HLIST_HEAD(&orig_node
->fragments
[i
].fragment_list
);
1018 spin_lock_init(&orig_node
->fragments
[i
].lock
);
1019 orig_node
->fragments
[i
].size
= 0;
1029 * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor
1030 * @bat_priv: the bat priv with all the soft interface information
1031 * @neigh: orig node which is to be checked
1034 batadv_purge_neigh_ifinfo(struct batadv_priv
*bat_priv
,
1035 struct batadv_neigh_node
*neigh
)
1037 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
1038 struct batadv_hard_iface
*if_outgoing
;
1039 struct hlist_node
*node_tmp
;
1041 spin_lock_bh(&neigh
->ifinfo_lock
);
1043 /* for all ifinfo objects for this neighinator */
1044 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
1045 &neigh
->ifinfo_list
, list
) {
1046 if_outgoing
= neigh_ifinfo
->if_outgoing
;
1048 /* always keep the default interface */
1049 if (if_outgoing
== BATADV_IF_DEFAULT
)
1052 /* don't purge if the interface is not (going) down */
1053 if (if_outgoing
->if_status
!= BATADV_IF_INACTIVE
&&
1054 if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
&&
1055 if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
)
1058 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1059 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1060 neigh
->addr
, if_outgoing
->net_dev
->name
);
1062 hlist_del_rcu(&neigh_ifinfo
->list
);
1063 batadv_neigh_ifinfo_put(neigh_ifinfo
);
1066 spin_unlock_bh(&neigh
->ifinfo_lock
);
1070 * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator
1071 * @bat_priv: the bat priv with all the soft interface information
1072 * @orig_node: orig node which is to be checked
1074 * Return: true if any ifinfo entry was purged, false otherwise.
1077 batadv_purge_orig_ifinfo(struct batadv_priv
*bat_priv
,
1078 struct batadv_orig_node
*orig_node
)
1080 struct batadv_orig_ifinfo
*orig_ifinfo
;
1081 struct batadv_hard_iface
*if_outgoing
;
1082 struct hlist_node
*node_tmp
;
1083 bool ifinfo_purged
= false;
1085 spin_lock_bh(&orig_node
->neigh_list_lock
);
1087 /* for all ifinfo objects for this originator */
1088 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
1089 &orig_node
->ifinfo_list
, list
) {
1090 if_outgoing
= orig_ifinfo
->if_outgoing
;
1092 /* always keep the default interface */
1093 if (if_outgoing
== BATADV_IF_DEFAULT
)
1096 /* don't purge if the interface is not (going) down */
1097 if (if_outgoing
->if_status
!= BATADV_IF_INACTIVE
&&
1098 if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
&&
1099 if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
)
1102 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1103 "router/ifinfo purge: originator %pM, iface: %s\n",
1104 orig_node
->orig
, if_outgoing
->net_dev
->name
);
1106 ifinfo_purged
= true;
1108 hlist_del_rcu(&orig_ifinfo
->list
);
1109 batadv_orig_ifinfo_put(orig_ifinfo
);
1110 if (orig_node
->last_bonding_candidate
== orig_ifinfo
) {
1111 orig_node
->last_bonding_candidate
= NULL
;
1112 batadv_orig_ifinfo_put(orig_ifinfo
);
1116 spin_unlock_bh(&orig_node
->neigh_list_lock
);
1118 return ifinfo_purged
;
1122 * batadv_purge_orig_neighbors() - purges neighbors from originator
1123 * @bat_priv: the bat priv with all the soft interface information
1124 * @orig_node: orig node which is to be checked
1126 * Return: true if any neighbor was purged, false otherwise
1129 batadv_purge_orig_neighbors(struct batadv_priv
*bat_priv
,
1130 struct batadv_orig_node
*orig_node
)
1132 struct hlist_node
*node_tmp
;
1133 struct batadv_neigh_node
*neigh_node
;
1134 bool neigh_purged
= false;
1135 unsigned long last_seen
;
1136 struct batadv_hard_iface
*if_incoming
;
1138 spin_lock_bh(&orig_node
->neigh_list_lock
);
1140 /* for all neighbors towards this originator ... */
1141 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
1142 &orig_node
->neigh_list
, list
) {
1143 last_seen
= neigh_node
->last_seen
;
1144 if_incoming
= neigh_node
->if_incoming
;
1146 if (batadv_has_timed_out(last_seen
, BATADV_PURGE_TIMEOUT
) ||
1147 if_incoming
->if_status
== BATADV_IF_INACTIVE
||
1148 if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
||
1149 if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
) {
1150 if (if_incoming
->if_status
== BATADV_IF_INACTIVE
||
1151 if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
||
1152 if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
)
1153 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1154 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1155 orig_node
->orig
, neigh_node
->addr
,
1156 if_incoming
->net_dev
->name
);
1158 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1159 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1160 orig_node
->orig
, neigh_node
->addr
,
1161 jiffies_to_msecs(last_seen
));
1163 neigh_purged
= true;
1165 hlist_del_rcu(&neigh_node
->list
);
1166 batadv_neigh_node_put(neigh_node
);
1168 /* only necessary if not the whole neighbor is to be
1169 * deleted, but some interface has been removed.
1171 batadv_purge_neigh_ifinfo(bat_priv
, neigh_node
);
1175 spin_unlock_bh(&orig_node
->neigh_list_lock
);
1176 return neigh_purged
;
1180 * batadv_find_best_neighbor() - finds the best neighbor after purging
1181 * @bat_priv: the bat priv with all the soft interface information
1182 * @orig_node: orig node which is to be checked
1183 * @if_outgoing: the interface for which the metric should be compared
1185 * Return: the current best neighbor, with refcount increased.
1187 static struct batadv_neigh_node
*
1188 batadv_find_best_neighbor(struct batadv_priv
*bat_priv
,
1189 struct batadv_orig_node
*orig_node
,
1190 struct batadv_hard_iface
*if_outgoing
)
1192 struct batadv_neigh_node
*best
= NULL
, *neigh
;
1193 struct batadv_algo_ops
*bao
= bat_priv
->algo_ops
;
1196 hlist_for_each_entry_rcu(neigh
, &orig_node
->neigh_list
, list
) {
1197 if (best
&& (bao
->neigh
.cmp(neigh
, if_outgoing
, best
,
1201 if (!kref_get_unless_zero(&neigh
->refcount
))
1204 batadv_neigh_node_put(best
);
1214 * batadv_purge_orig_node() - purges obsolete information from an orig_node
1215 * @bat_priv: the bat priv with all the soft interface information
1216 * @orig_node: orig node which is to be checked
1218 * This function checks if the orig_node or substructures of it have become
1219 * obsolete, and purges this information if that's the case.
1221 * Return: true if the orig_node is to be removed, false otherwise.
1223 static bool batadv_purge_orig_node(struct batadv_priv
*bat_priv
,
1224 struct batadv_orig_node
*orig_node
)
1226 struct batadv_neigh_node
*best_neigh_node
;
1227 struct batadv_hard_iface
*hard_iface
;
1228 bool changed_ifinfo
, changed_neigh
;
1230 if (batadv_has_timed_out(orig_node
->last_seen
,
1231 2 * BATADV_PURGE_TIMEOUT
)) {
1232 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1233 "Originator timeout: originator %pM, last_seen %u\n",
1235 jiffies_to_msecs(orig_node
->last_seen
));
1238 changed_ifinfo
= batadv_purge_orig_ifinfo(bat_priv
, orig_node
);
1239 changed_neigh
= batadv_purge_orig_neighbors(bat_priv
, orig_node
);
1241 if (!changed_ifinfo
&& !changed_neigh
)
1244 /* first for NULL ... */
1245 best_neigh_node
= batadv_find_best_neighbor(bat_priv
, orig_node
,
1247 batadv_update_route(bat_priv
, orig_node
, BATADV_IF_DEFAULT
,
1249 batadv_neigh_node_put(best_neigh_node
);
1251 /* ... then for all other interfaces. */
1253 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1254 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1257 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
1260 if (!kref_get_unless_zero(&hard_iface
->refcount
))
1263 best_neigh_node
= batadv_find_best_neighbor(bat_priv
,
1266 batadv_update_route(bat_priv
, orig_node
, hard_iface
,
1268 batadv_neigh_node_put(best_neigh_node
);
1270 batadv_hardif_put(hard_iface
);
1278 * batadv_purge_orig_ref() - Purge all outdated originators
1279 * @bat_priv: the bat priv with all the soft interface information
1281 void batadv_purge_orig_ref(struct batadv_priv
*bat_priv
)
1283 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1284 struct hlist_node
*node_tmp
;
1285 struct hlist_head
*head
;
1286 spinlock_t
*list_lock
; /* spinlock to protect write access */
1287 struct batadv_orig_node
*orig_node
;
1293 /* for all origins... */
1294 for (i
= 0; i
< hash
->size
; i
++) {
1295 head
= &hash
->table
[i
];
1296 if (hlist_empty(head
))
1298 list_lock
= &hash
->list_locks
[i
];
1300 spin_lock_bh(list_lock
);
1301 hlist_for_each_entry_safe(orig_node
, node_tmp
,
1303 if (batadv_purge_orig_node(bat_priv
, orig_node
)) {
1304 batadv_gw_node_delete(bat_priv
, orig_node
);
1305 hlist_del_rcu(&orig_node
->hash_entry
);
1306 batadv_tt_global_del_orig(orig_node
->bat_priv
,
1308 "originator timed out");
1309 batadv_orig_node_put(orig_node
);
1313 batadv_frag_purge_orig(orig_node
,
1314 batadv_frag_check_entry
);
1316 spin_unlock_bh(list_lock
);
1319 batadv_gw_election(bat_priv
);
1322 static void batadv_purge_orig(struct work_struct
*work
)
1324 struct delayed_work
*delayed_work
;
1325 struct batadv_priv
*bat_priv
;
1327 delayed_work
= to_delayed_work(work
);
1328 bat_priv
= container_of(delayed_work
, struct batadv_priv
, orig_work
);
1329 batadv_purge_orig_ref(bat_priv
);
1330 queue_delayed_work(batadv_event_workqueue
,
1331 &bat_priv
->orig_work
,
1332 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD
));
1336 * batadv_orig_dump() - Dump to netlink the originator infos for a specific
1337 * outgoing interface
1338 * @msg: message to dump into
1339 * @cb: parameters for the dump
1341 * Return: 0 or error value
1343 int batadv_orig_dump(struct sk_buff
*msg
, struct netlink_callback
*cb
)
1345 struct net
*net
= sock_net(cb
->skb
->sk
);
1346 struct net_device
*soft_iface
;
1347 struct net_device
*hard_iface
= NULL
;
1348 struct batadv_hard_iface
*hardif
= BATADV_IF_DEFAULT
;
1349 struct batadv_priv
*bat_priv
;
1350 struct batadv_hard_iface
*primary_if
= NULL
;
1352 int ifindex
, hard_ifindex
;
1354 ifindex
= batadv_netlink_get_ifindex(cb
->nlh
, BATADV_ATTR_MESH_IFINDEX
);
1358 soft_iface
= dev_get_by_index(net
, ifindex
);
1359 if (!soft_iface
|| !batadv_softif_is_valid(soft_iface
)) {
1364 bat_priv
= netdev_priv(soft_iface
);
1366 primary_if
= batadv_primary_if_get_selected(bat_priv
);
1367 if (!primary_if
|| primary_if
->if_status
!= BATADV_IF_ACTIVE
) {
1372 hard_ifindex
= batadv_netlink_get_ifindex(cb
->nlh
,
1373 BATADV_ATTR_HARD_IFINDEX
);
1375 hard_iface
= dev_get_by_index(net
, hard_ifindex
);
1377 hardif
= batadv_hardif_get_by_netdev(hard_iface
);
1384 if (hardif
->soft_iface
!= soft_iface
) {
1390 if (!bat_priv
->algo_ops
->orig
.dump
) {
1395 bat_priv
->algo_ops
->orig
.dump(msg
, cb
, bat_priv
, hardif
);
1400 batadv_hardif_put(hardif
);
1401 dev_put(hard_iface
);
1402 batadv_hardif_put(primary_if
);
1403 dev_put(soft_iface
);