1 /* Copyright (C) 2009-2015 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "originator.h"
21 #include <linux/errno.h>
22 #include <linux/etherdevice.h>
24 #include <linux/jiffies.h>
25 #include <linux/kernel.h>
26 #include <linux/list.h>
27 #include <linux/lockdep.h>
28 #include <linux/netdevice.h>
29 #include <linux/rculist.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/workqueue.h>
35 #include "distributed-arp-table.h"
36 #include "fragmentation.h"
37 #include "gateway_client.h"
38 #include "hard-interface.h"
40 #include "multicast.h"
41 #include "network-coding.h"
43 #include "translation-table.h"
46 static struct lock_class_key batadv_orig_hash_lock_class_key
;
48 static void batadv_purge_orig(struct work_struct
*work
);
50 /* returns 1 if they are the same originator */
51 int batadv_compare_orig(const struct hlist_node
*node
, const void *data2
)
53 const void *data1
= container_of(node
, struct batadv_orig_node
,
56 return batadv_compare_eth(data1
, data2
);
60 * batadv_orig_node_vlan_get - get an orig_node_vlan object
61 * @orig_node: the originator serving the VLAN
62 * @vid: the VLAN identifier
64 * Returns the vlan object identified by vid and belonging to orig_node or NULL
65 * if it does not exist.
67 struct batadv_orig_node_vlan
*
68 batadv_orig_node_vlan_get(struct batadv_orig_node
*orig_node
,
71 struct batadv_orig_node_vlan
*vlan
= NULL
, *tmp
;
74 hlist_for_each_entry_rcu(tmp
, &orig_node
->vlan_list
, list
) {
78 if (!atomic_inc_not_zero(&tmp
->refcount
))
91 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
93 * @orig_node: the originator serving the VLAN
94 * @vid: the VLAN identifier
96 * Returns NULL in case of failure or the vlan object identified by vid and
97 * belonging to orig_node otherwise. The object is created and added to the list
98 * if it does not exist.
100 * The object is returned with refcounter increased by 1.
102 struct batadv_orig_node_vlan
*
103 batadv_orig_node_vlan_new(struct batadv_orig_node
*orig_node
,
106 struct batadv_orig_node_vlan
*vlan
;
108 spin_lock_bh(&orig_node
->vlan_list_lock
);
110 /* first look if an object for this vid already exists */
111 vlan
= batadv_orig_node_vlan_get(orig_node
, vid
);
115 vlan
= kzalloc(sizeof(*vlan
), GFP_ATOMIC
);
119 atomic_set(&vlan
->refcount
, 2);
122 hlist_add_head_rcu(&vlan
->list
, &orig_node
->vlan_list
);
125 spin_unlock_bh(&orig_node
->vlan_list_lock
);
131 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
132 * the originator-vlan object
133 * @orig_vlan: the originator-vlan object to release
135 void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan
*orig_vlan
)
137 if (atomic_dec_and_test(&orig_vlan
->refcount
))
138 kfree_rcu(orig_vlan
, rcu
);
141 int batadv_originator_init(struct batadv_priv
*bat_priv
)
143 if (bat_priv
->orig_hash
)
146 bat_priv
->orig_hash
= batadv_hash_new(1024);
148 if (!bat_priv
->orig_hash
)
151 batadv_hash_set_lock_class(bat_priv
->orig_hash
,
152 &batadv_orig_hash_lock_class_key
);
154 INIT_DELAYED_WORK(&bat_priv
->orig_work
, batadv_purge_orig
);
155 queue_delayed_work(batadv_event_workqueue
,
156 &bat_priv
->orig_work
,
157 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD
));
166 * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
167 * @rcu: rcu pointer of the neigh_ifinfo object
169 static void batadv_neigh_ifinfo_free_rcu(struct rcu_head
*rcu
)
171 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
173 neigh_ifinfo
= container_of(rcu
, struct batadv_neigh_ifinfo
, rcu
);
175 if (neigh_ifinfo
->if_outgoing
!= BATADV_IF_DEFAULT
)
176 batadv_hardif_free_ref_now(neigh_ifinfo
->if_outgoing
);
182 * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free
183 * the neigh_ifinfo (without rcu callback)
184 * @neigh_ifinfo: the neigh_ifinfo object to release
187 batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo
*neigh_ifinfo
)
189 if (atomic_dec_and_test(&neigh_ifinfo
->refcount
))
190 batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo
->rcu
);
194 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free
196 * @neigh_ifinfo: the neigh_ifinfo object to release
198 void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo
*neigh_ifinfo
)
200 if (atomic_dec_and_test(&neigh_ifinfo
->refcount
))
201 call_rcu(&neigh_ifinfo
->rcu
, batadv_neigh_ifinfo_free_rcu
);
205 * batadv_hardif_neigh_free_rcu - free the hardif neigh_node
206 * @rcu: rcu pointer of the neigh_node
208 static void batadv_hardif_neigh_free_rcu(struct rcu_head
*rcu
)
210 struct batadv_hardif_neigh_node
*hardif_neigh
;
212 hardif_neigh
= container_of(rcu
, struct batadv_hardif_neigh_node
, rcu
);
214 batadv_hardif_free_ref_now(hardif_neigh
->if_incoming
);
219 * batadv_hardif_neigh_free_now - decrement the hardif neighbors refcounter
220 * and possibly free it (without rcu callback)
221 * @hardif_neigh: hardif neigh neighbor to free
224 batadv_hardif_neigh_free_now(struct batadv_hardif_neigh_node
*hardif_neigh
)
226 if (atomic_dec_and_test(&hardif_neigh
->refcount
)) {
227 spin_lock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
228 hlist_del_init_rcu(&hardif_neigh
->list
);
229 spin_unlock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
231 batadv_hardif_neigh_free_rcu(&hardif_neigh
->rcu
);
236 * batadv_hardif_neigh_free_ref - decrement the hardif neighbors refcounter
237 * and possibly free it
238 * @hardif_neigh: hardif neigh neighbor to free
240 void batadv_hardif_neigh_free_ref(struct batadv_hardif_neigh_node
*hardif_neigh
)
242 if (atomic_dec_and_test(&hardif_neigh
->refcount
)) {
243 spin_lock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
244 hlist_del_init_rcu(&hardif_neigh
->list
);
245 spin_unlock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
247 call_rcu(&hardif_neigh
->rcu
, batadv_hardif_neigh_free_rcu
);
252 * batadv_neigh_node_free_rcu - free the neigh_node
253 * @rcu: rcu pointer of the neigh_node
255 static void batadv_neigh_node_free_rcu(struct rcu_head
*rcu
)
257 struct hlist_node
*node_tmp
;
258 struct batadv_neigh_node
*neigh_node
;
259 struct batadv_hardif_neigh_node
*hardif_neigh
;
260 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
261 struct batadv_algo_ops
*bao
;
263 neigh_node
= container_of(rcu
, struct batadv_neigh_node
, rcu
);
264 bao
= neigh_node
->orig_node
->bat_priv
->bat_algo_ops
;
266 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
267 &neigh_node
->ifinfo_list
, list
) {
268 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo
);
271 hardif_neigh
= batadv_hardif_neigh_get(neigh_node
->if_incoming
,
274 /* batadv_hardif_neigh_get() increases refcount too */
275 batadv_hardif_neigh_free_now(hardif_neigh
);
276 batadv_hardif_neigh_free_now(hardif_neigh
);
279 if (bao
->bat_neigh_free
)
280 bao
->bat_neigh_free(neigh_node
);
282 batadv_hardif_free_ref_now(neigh_node
->if_incoming
);
288 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
289 * and possibly release it
290 * @neigh_node: neigh neighbor to free
292 void batadv_neigh_node_free_ref(struct batadv_neigh_node
*neigh_node
)
294 if (atomic_dec_and_test(&neigh_node
->refcount
))
295 call_rcu(&neigh_node
->rcu
, batadv_neigh_node_free_rcu
);
299 * batadv_orig_node_get_router - router to the originator depending on iface
300 * @orig_node: the orig node for the router
301 * @if_outgoing: the interface where the payload packet has been received or
302 * the OGM should be sent to
304 * Returns the neighbor which should be router for this orig_node/iface.
306 * The object is returned with refcounter increased by 1.
308 struct batadv_neigh_node
*
309 batadv_orig_router_get(struct batadv_orig_node
*orig_node
,
310 const struct batadv_hard_iface
*if_outgoing
)
312 struct batadv_orig_ifinfo
*orig_ifinfo
;
313 struct batadv_neigh_node
*router
= NULL
;
316 hlist_for_each_entry_rcu(orig_ifinfo
, &orig_node
->ifinfo_list
, list
) {
317 if (orig_ifinfo
->if_outgoing
!= if_outgoing
)
320 router
= rcu_dereference(orig_ifinfo
->router
);
324 if (router
&& !atomic_inc_not_zero(&router
->refcount
))
332 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
333 * @orig_node: the orig node to be queried
334 * @if_outgoing: the interface for which the ifinfo should be acquired
336 * Returns the requested orig_ifinfo or NULL if not found.
338 * The object is returned with refcounter increased by 1.
340 struct batadv_orig_ifinfo
*
341 batadv_orig_ifinfo_get(struct batadv_orig_node
*orig_node
,
342 struct batadv_hard_iface
*if_outgoing
)
344 struct batadv_orig_ifinfo
*tmp
, *orig_ifinfo
= NULL
;
347 hlist_for_each_entry_rcu(tmp
, &orig_node
->ifinfo_list
,
349 if (tmp
->if_outgoing
!= if_outgoing
)
352 if (!atomic_inc_not_zero(&tmp
->refcount
))
364 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
365 * @orig_node: the orig node to be queried
366 * @if_outgoing: the interface for which the ifinfo should be acquired
368 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
369 * interface otherwise. The object is created and added to the list
370 * if it does not exist.
372 * The object is returned with refcounter increased by 1.
374 struct batadv_orig_ifinfo
*
375 batadv_orig_ifinfo_new(struct batadv_orig_node
*orig_node
,
376 struct batadv_hard_iface
*if_outgoing
)
378 struct batadv_orig_ifinfo
*orig_ifinfo
= NULL
;
379 unsigned long reset_time
;
381 spin_lock_bh(&orig_node
->neigh_list_lock
);
383 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, if_outgoing
);
387 orig_ifinfo
= kzalloc(sizeof(*orig_ifinfo
), GFP_ATOMIC
);
391 if (if_outgoing
!= BATADV_IF_DEFAULT
&&
392 !atomic_inc_not_zero(&if_outgoing
->refcount
)) {
398 reset_time
= jiffies
- 1;
399 reset_time
-= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
400 orig_ifinfo
->batman_seqno_reset
= reset_time
;
401 orig_ifinfo
->if_outgoing
= if_outgoing
;
402 INIT_HLIST_NODE(&orig_ifinfo
->list
);
403 atomic_set(&orig_ifinfo
->refcount
, 2);
404 hlist_add_head_rcu(&orig_ifinfo
->list
,
405 &orig_node
->ifinfo_list
);
407 spin_unlock_bh(&orig_node
->neigh_list_lock
);
412 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
413 * @neigh_node: the neigh node to be queried
414 * @if_outgoing: the interface for which the ifinfo should be acquired
416 * The object is returned with refcounter increased by 1.
418 * Returns the requested neigh_ifinfo or NULL if not found
420 struct batadv_neigh_ifinfo
*
421 batadv_neigh_ifinfo_get(struct batadv_neigh_node
*neigh
,
422 struct batadv_hard_iface
*if_outgoing
)
424 struct batadv_neigh_ifinfo
*neigh_ifinfo
= NULL
,
428 hlist_for_each_entry_rcu(tmp_neigh_ifinfo
, &neigh
->ifinfo_list
,
430 if (tmp_neigh_ifinfo
->if_outgoing
!= if_outgoing
)
433 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo
->refcount
))
436 neigh_ifinfo
= tmp_neigh_ifinfo
;
445 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
446 * @neigh_node: the neigh node to be queried
447 * @if_outgoing: the interface for which the ifinfo should be acquired
449 * Returns NULL in case of failure or the neigh_ifinfo object for the
450 * if_outgoing interface otherwise. The object is created and added to the list
451 * if it does not exist.
453 * The object is returned with refcounter increased by 1.
455 struct batadv_neigh_ifinfo
*
456 batadv_neigh_ifinfo_new(struct batadv_neigh_node
*neigh
,
457 struct batadv_hard_iface
*if_outgoing
)
459 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
461 spin_lock_bh(&neigh
->ifinfo_lock
);
463 neigh_ifinfo
= batadv_neigh_ifinfo_get(neigh
, if_outgoing
);
467 neigh_ifinfo
= kzalloc(sizeof(*neigh_ifinfo
), GFP_ATOMIC
);
471 if (if_outgoing
&& !atomic_inc_not_zero(&if_outgoing
->refcount
)) {
477 INIT_HLIST_NODE(&neigh_ifinfo
->list
);
478 atomic_set(&neigh_ifinfo
->refcount
, 2);
479 neigh_ifinfo
->if_outgoing
= if_outgoing
;
481 hlist_add_head_rcu(&neigh_ifinfo
->list
, &neigh
->ifinfo_list
);
484 spin_unlock_bh(&neigh
->ifinfo_lock
);
490 * batadv_neigh_node_get - retrieve a neighbour from the list
491 * @orig_node: originator which the neighbour belongs to
492 * @hard_iface: the interface where this neighbour is connected to
493 * @addr: the address of the neighbour
495 * Looks for and possibly returns a neighbour belonging to this originator list
496 * which is connected through the provided hard interface.
497 * Returns NULL if the neighbour is not found.
499 static struct batadv_neigh_node
*
500 batadv_neigh_node_get(const struct batadv_orig_node
*orig_node
,
501 const struct batadv_hard_iface
*hard_iface
,
504 struct batadv_neigh_node
*tmp_neigh_node
, *res
= NULL
;
507 hlist_for_each_entry_rcu(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
508 if (!batadv_compare_eth(tmp_neigh_node
->addr
, addr
))
511 if (tmp_neigh_node
->if_incoming
!= hard_iface
)
514 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
517 res
= tmp_neigh_node
;
526 * batadv_hardif_neigh_create - create a hardif neighbour node
527 * @hard_iface: the interface this neighbour is connected to
528 * @neigh_addr: the interface address of the neighbour to retrieve
530 * Returns the hardif neighbour node if found or created or NULL otherwise.
532 static struct batadv_hardif_neigh_node
*
533 batadv_hardif_neigh_create(struct batadv_hard_iface
*hard_iface
,
534 const u8
*neigh_addr
)
536 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
537 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
539 spin_lock_bh(&hard_iface
->neigh_list_lock
);
541 /* check if neighbor hasn't been added in the meantime */
542 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
546 if (!atomic_inc_not_zero(&hard_iface
->refcount
))
549 hardif_neigh
= kzalloc(sizeof(*hardif_neigh
), GFP_ATOMIC
);
551 batadv_hardif_free_ref(hard_iface
);
555 INIT_HLIST_NODE(&hardif_neigh
->list
);
556 ether_addr_copy(hardif_neigh
->addr
, neigh_addr
);
557 hardif_neigh
->if_incoming
= hard_iface
;
558 hardif_neigh
->last_seen
= jiffies
;
560 atomic_set(&hardif_neigh
->refcount
, 1);
562 if (bat_priv
->bat_algo_ops
->bat_hardif_neigh_init
)
563 bat_priv
->bat_algo_ops
->bat_hardif_neigh_init(hardif_neigh
);
565 hlist_add_head(&hardif_neigh
->list
, &hard_iface
->neigh_list
);
568 spin_unlock_bh(&hard_iface
->neigh_list_lock
);
573 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
575 * @hard_iface: the interface this neighbour is connected to
576 * @neigh_addr: the interface address of the neighbour to retrieve
578 * Returns the hardif neighbour node if found or created or NULL otherwise.
580 static struct batadv_hardif_neigh_node
*
581 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface
*hard_iface
,
582 const u8
*neigh_addr
)
584 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
586 /* first check without locking to avoid the overhead */
587 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
591 return batadv_hardif_neigh_create(hard_iface
, neigh_addr
);
595 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
596 * @hard_iface: the interface where this neighbour is connected to
597 * @neigh_addr: the address of the neighbour
599 * Looks for and possibly returns a neighbour belonging to this hard interface.
600 * Returns NULL if the neighbour is not found.
602 struct batadv_hardif_neigh_node
*
603 batadv_hardif_neigh_get(const struct batadv_hard_iface
*hard_iface
,
604 const u8
*neigh_addr
)
606 struct batadv_hardif_neigh_node
*tmp_hardif_neigh
, *hardif_neigh
= NULL
;
609 hlist_for_each_entry_rcu(tmp_hardif_neigh
,
610 &hard_iface
->neigh_list
, list
) {
611 if (!batadv_compare_eth(tmp_hardif_neigh
->addr
, neigh_addr
))
614 if (!atomic_inc_not_zero(&tmp_hardif_neigh
->refcount
))
617 hardif_neigh
= tmp_hardif_neigh
;
626 * batadv_neigh_node_new - create and init a new neigh_node object
627 * @orig_node: originator object representing the neighbour
628 * @hard_iface: the interface where the neighbour is connected to
629 * @neigh_addr: the mac address of the neighbour interface
631 * Allocates a new neigh_node object and initialises all the generic fields.
632 * Returns the new object or NULL on failure.
634 struct batadv_neigh_node
*
635 batadv_neigh_node_new(struct batadv_orig_node
*orig_node
,
636 struct batadv_hard_iface
*hard_iface
,
637 const u8
*neigh_addr
)
639 struct batadv_neigh_node
*neigh_node
;
640 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
642 neigh_node
= batadv_neigh_node_get(orig_node
, hard_iface
, neigh_addr
);
646 hardif_neigh
= batadv_hardif_neigh_get_or_create(hard_iface
,
651 neigh_node
= kzalloc(sizeof(*neigh_node
), GFP_ATOMIC
);
655 if (!atomic_inc_not_zero(&hard_iface
->refcount
)) {
661 INIT_HLIST_NODE(&neigh_node
->list
);
662 INIT_HLIST_HEAD(&neigh_node
->ifinfo_list
);
663 spin_lock_init(&neigh_node
->ifinfo_lock
);
665 ether_addr_copy(neigh_node
->addr
, neigh_addr
);
666 neigh_node
->if_incoming
= hard_iface
;
667 neigh_node
->orig_node
= orig_node
;
669 /* extra reference for return */
670 atomic_set(&neigh_node
->refcount
, 2);
672 spin_lock_bh(&orig_node
->neigh_list_lock
);
673 hlist_add_head_rcu(&neigh_node
->list
, &orig_node
->neigh_list
);
674 spin_unlock_bh(&orig_node
->neigh_list_lock
);
676 /* increment unique neighbor refcount */
677 atomic_inc(&hardif_neigh
->refcount
);
679 batadv_dbg(BATADV_DBG_BATMAN
, orig_node
->bat_priv
,
680 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
681 neigh_addr
, orig_node
->orig
, hard_iface
->net_dev
->name
);
685 batadv_hardif_neigh_free_ref(hardif_neigh
);
690 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
691 * @seq: neighbour table seq_file struct
696 int batadv_hardif_neigh_seq_print_text(struct seq_file
*seq
, void *offset
)
698 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
699 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
700 struct batadv_hard_iface
*primary_if
;
702 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
706 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
707 BATADV_SOURCE_VERSION
, primary_if
->net_dev
->name
,
708 primary_if
->net_dev
->dev_addr
, net_dev
->name
,
709 bat_priv
->bat_algo_ops
->name
);
711 batadv_hardif_free_ref(primary_if
);
713 if (!bat_priv
->bat_algo_ops
->bat_neigh_print
) {
715 "No printing function for this routing protocol\n");
719 bat_priv
->bat_algo_ops
->bat_neigh_print(bat_priv
, seq
);
724 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
725 * free after rcu grace period
726 * @orig_ifinfo: the orig_ifinfo object to release
728 static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo
*orig_ifinfo
)
730 struct batadv_neigh_node
*router
;
732 if (orig_ifinfo
->if_outgoing
!= BATADV_IF_DEFAULT
)
733 batadv_hardif_free_ref(orig_ifinfo
->if_outgoing
);
735 /* this is the last reference to this object */
736 router
= rcu_dereference_protected(orig_ifinfo
->router
, true);
738 batadv_neigh_node_free_ref(router
);
740 kfree_rcu(orig_ifinfo
, rcu
);
744 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
746 * @orig_ifinfo: the orig_ifinfo object to release
748 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo
*orig_ifinfo
)
750 if (atomic_dec_and_test(&orig_ifinfo
->refcount
))
751 batadv_orig_ifinfo_release(orig_ifinfo
);
755 * batadv_orig_node_free_rcu - free the orig_node
756 * @rcu: rcu pointer of the orig_node
758 static void batadv_orig_node_free_rcu(struct rcu_head
*rcu
)
760 struct batadv_orig_node
*orig_node
;
762 orig_node
= container_of(rcu
, struct batadv_orig_node
, rcu
);
764 batadv_mcast_purge_orig(orig_node
);
766 batadv_frag_purge_orig(orig_node
, NULL
);
768 if (orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free
)
769 orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free(orig_node
);
771 kfree(orig_node
->tt_buff
);
776 * batadv_orig_node_release - release orig_node from lists and queue for
777 * free after rcu grace period
778 * @orig_node: the orig node to free
780 static void batadv_orig_node_release(struct batadv_orig_node
*orig_node
)
782 struct hlist_node
*node_tmp
;
783 struct batadv_neigh_node
*neigh_node
;
784 struct batadv_orig_ifinfo
*orig_ifinfo
;
786 spin_lock_bh(&orig_node
->neigh_list_lock
);
788 /* for all neighbors towards this originator ... */
789 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
790 &orig_node
->neigh_list
, list
) {
791 hlist_del_rcu(&neigh_node
->list
);
792 batadv_neigh_node_free_ref(neigh_node
);
795 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
796 &orig_node
->ifinfo_list
, list
) {
797 hlist_del_rcu(&orig_ifinfo
->list
);
798 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
800 spin_unlock_bh(&orig_node
->neigh_list_lock
);
803 batadv_nc_purge_orig(orig_node
->bat_priv
, orig_node
, NULL
);
805 call_rcu(&orig_node
->rcu
, batadv_orig_node_free_rcu
);
809 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
811 * @orig_node: the orig node to free
813 void batadv_orig_node_free_ref(struct batadv_orig_node
*orig_node
)
815 if (atomic_dec_and_test(&orig_node
->refcount
))
816 batadv_orig_node_release(orig_node
);
820 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
821 * possibly free it (without rcu callback)
822 * @orig_node: the orig node to free
824 void batadv_orig_node_free_ref_now(struct batadv_orig_node
*orig_node
)
826 if (atomic_dec_and_test(&orig_node
->refcount
))
827 batadv_orig_node_free_rcu(&orig_node
->rcu
);
830 void batadv_originator_free(struct batadv_priv
*bat_priv
)
832 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
833 struct hlist_node
*node_tmp
;
834 struct hlist_head
*head
;
835 spinlock_t
*list_lock
; /* spinlock to protect write access */
836 struct batadv_orig_node
*orig_node
;
842 cancel_delayed_work_sync(&bat_priv
->orig_work
);
844 bat_priv
->orig_hash
= NULL
;
846 for (i
= 0; i
< hash
->size
; i
++) {
847 head
= &hash
->table
[i
];
848 list_lock
= &hash
->list_locks
[i
];
850 spin_lock_bh(list_lock
);
851 hlist_for_each_entry_safe(orig_node
, node_tmp
,
853 hlist_del_rcu(&orig_node
->hash_entry
);
854 batadv_orig_node_free_ref(orig_node
);
856 spin_unlock_bh(list_lock
);
859 batadv_hash_destroy(hash
);
863 * batadv_orig_node_new - creates a new orig_node
864 * @bat_priv: the bat priv with all the soft interface information
865 * @addr: the mac address of the originator
867 * Creates a new originator object and initialise all the generic fields.
868 * The new object is not added to the originator list.
869 * Returns the newly created object or NULL on failure.
871 struct batadv_orig_node
*batadv_orig_node_new(struct batadv_priv
*bat_priv
,
874 struct batadv_orig_node
*orig_node
;
875 struct batadv_orig_node_vlan
*vlan
;
876 unsigned long reset_time
;
879 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
880 "Creating new originator: %pM\n", addr
);
882 orig_node
= kzalloc(sizeof(*orig_node
), GFP_ATOMIC
);
886 INIT_HLIST_HEAD(&orig_node
->neigh_list
);
887 INIT_HLIST_HEAD(&orig_node
->vlan_list
);
888 INIT_HLIST_HEAD(&orig_node
->ifinfo_list
);
889 spin_lock_init(&orig_node
->bcast_seqno_lock
);
890 spin_lock_init(&orig_node
->neigh_list_lock
);
891 spin_lock_init(&orig_node
->tt_buff_lock
);
892 spin_lock_init(&orig_node
->tt_lock
);
893 spin_lock_init(&orig_node
->vlan_list_lock
);
895 batadv_nc_init_orig(orig_node
);
897 /* extra reference for return */
898 atomic_set(&orig_node
->refcount
, 2);
900 orig_node
->bat_priv
= bat_priv
;
901 ether_addr_copy(orig_node
->orig
, addr
);
902 batadv_dat_init_orig_node_addr(orig_node
);
903 atomic_set(&orig_node
->last_ttvn
, 0);
904 orig_node
->tt_buff
= NULL
;
905 orig_node
->tt_buff_len
= 0;
906 orig_node
->last_seen
= jiffies
;
907 reset_time
= jiffies
- 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
908 orig_node
->bcast_seqno_reset
= reset_time
;
910 #ifdef CONFIG_BATMAN_ADV_MCAST
911 orig_node
->mcast_flags
= BATADV_NO_FLAGS
;
912 INIT_HLIST_NODE(&orig_node
->mcast_want_all_unsnoopables_node
);
913 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv4_node
);
914 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv6_node
);
915 spin_lock_init(&orig_node
->mcast_handler_lock
);
918 /* create a vlan object for the "untagged" LAN */
919 vlan
= batadv_orig_node_vlan_new(orig_node
, BATADV_NO_FLAGS
);
922 /* batadv_orig_node_vlan_new() increases the refcounter.
923 * Immediately release vlan since it is not needed anymore in this
926 batadv_orig_node_vlan_free_ref(vlan
);
928 for (i
= 0; i
< BATADV_FRAG_BUFFER_COUNT
; i
++) {
929 INIT_HLIST_HEAD(&orig_node
->fragments
[i
].head
);
930 spin_lock_init(&orig_node
->fragments
[i
].lock
);
931 orig_node
->fragments
[i
].size
= 0;
941 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
942 * @bat_priv: the bat priv with all the soft interface information
943 * @neigh: orig node which is to be checked
946 batadv_purge_neigh_ifinfo(struct batadv_priv
*bat_priv
,
947 struct batadv_neigh_node
*neigh
)
949 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
950 struct batadv_hard_iface
*if_outgoing
;
951 struct hlist_node
*node_tmp
;
953 spin_lock_bh(&neigh
->ifinfo_lock
);
955 /* for all ifinfo objects for this neighinator */
956 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
957 &neigh
->ifinfo_list
, list
) {
958 if_outgoing
= neigh_ifinfo
->if_outgoing
;
960 /* always keep the default interface */
961 if (if_outgoing
== BATADV_IF_DEFAULT
)
964 /* don't purge if the interface is not (going) down */
965 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
966 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
967 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
970 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
971 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
972 neigh
->addr
, if_outgoing
->net_dev
->name
);
974 hlist_del_rcu(&neigh_ifinfo
->list
);
975 batadv_neigh_ifinfo_free_ref(neigh_ifinfo
);
978 spin_unlock_bh(&neigh
->ifinfo_lock
);
982 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
983 * @bat_priv: the bat priv with all the soft interface information
984 * @orig_node: orig node which is to be checked
986 * Returns true if any ifinfo entry was purged, false otherwise.
989 batadv_purge_orig_ifinfo(struct batadv_priv
*bat_priv
,
990 struct batadv_orig_node
*orig_node
)
992 struct batadv_orig_ifinfo
*orig_ifinfo
;
993 struct batadv_hard_iface
*if_outgoing
;
994 struct hlist_node
*node_tmp
;
995 bool ifinfo_purged
= false;
997 spin_lock_bh(&orig_node
->neigh_list_lock
);
999 /* for all ifinfo objects for this originator */
1000 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
1001 &orig_node
->ifinfo_list
, list
) {
1002 if_outgoing
= orig_ifinfo
->if_outgoing
;
1004 /* always keep the default interface */
1005 if (if_outgoing
== BATADV_IF_DEFAULT
)
1008 /* don't purge if the interface is not (going) down */
1009 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
1010 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
1011 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
1014 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1015 "router/ifinfo purge: originator %pM, iface: %s\n",
1016 orig_node
->orig
, if_outgoing
->net_dev
->name
);
1018 ifinfo_purged
= true;
1020 hlist_del_rcu(&orig_ifinfo
->list
);
1021 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
1022 if (orig_node
->last_bonding_candidate
== orig_ifinfo
) {
1023 orig_node
->last_bonding_candidate
= NULL
;
1024 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
1028 spin_unlock_bh(&orig_node
->neigh_list_lock
);
1030 return ifinfo_purged
;
1034 * batadv_purge_orig_neighbors - purges neighbors from originator
1035 * @bat_priv: the bat priv with all the soft interface information
1036 * @orig_node: orig node which is to be checked
1038 * Returns true if any neighbor was purged, false otherwise
1041 batadv_purge_orig_neighbors(struct batadv_priv
*bat_priv
,
1042 struct batadv_orig_node
*orig_node
)
1044 struct hlist_node
*node_tmp
;
1045 struct batadv_neigh_node
*neigh_node
;
1046 bool neigh_purged
= false;
1047 unsigned long last_seen
;
1048 struct batadv_hard_iface
*if_incoming
;
1050 spin_lock_bh(&orig_node
->neigh_list_lock
);
1052 /* for all neighbors towards this originator ... */
1053 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
1054 &orig_node
->neigh_list
, list
) {
1055 last_seen
= neigh_node
->last_seen
;
1056 if_incoming
= neigh_node
->if_incoming
;
1058 if ((batadv_has_timed_out(last_seen
, BATADV_PURGE_TIMEOUT
)) ||
1059 (if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
1060 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
1061 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
)) {
1062 if ((if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
1063 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
1064 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
))
1065 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1066 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1067 orig_node
->orig
, neigh_node
->addr
,
1068 if_incoming
->net_dev
->name
);
1070 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1071 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1072 orig_node
->orig
, neigh_node
->addr
,
1073 jiffies_to_msecs(last_seen
));
1075 neigh_purged
= true;
1077 hlist_del_rcu(&neigh_node
->list
);
1078 batadv_neigh_node_free_ref(neigh_node
);
1080 /* only necessary if not the whole neighbor is to be
1081 * deleted, but some interface has been removed.
1083 batadv_purge_neigh_ifinfo(bat_priv
, neigh_node
);
1087 spin_unlock_bh(&orig_node
->neigh_list_lock
);
1088 return neigh_purged
;
1092 * batadv_find_best_neighbor - finds the best neighbor after purging
1093 * @bat_priv: the bat priv with all the soft interface information
1094 * @orig_node: orig node which is to be checked
1095 * @if_outgoing: the interface for which the metric should be compared
1097 * Returns the current best neighbor, with refcount increased.
1099 static struct batadv_neigh_node
*
1100 batadv_find_best_neighbor(struct batadv_priv
*bat_priv
,
1101 struct batadv_orig_node
*orig_node
,
1102 struct batadv_hard_iface
*if_outgoing
)
1104 struct batadv_neigh_node
*best
= NULL
, *neigh
;
1105 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1108 hlist_for_each_entry_rcu(neigh
, &orig_node
->neigh_list
, list
) {
1109 if (best
&& (bao
->bat_neigh_cmp(neigh
, if_outgoing
,
1110 best
, if_outgoing
) <= 0))
1113 if (!atomic_inc_not_zero(&neigh
->refcount
))
1117 batadv_neigh_node_free_ref(best
);
1127 * batadv_purge_orig_node - purges obsolete information from an orig_node
1128 * @bat_priv: the bat priv with all the soft interface information
1129 * @orig_node: orig node which is to be checked
1131 * This function checks if the orig_node or substructures of it have become
1132 * obsolete, and purges this information if that's the case.
1134 * Returns true if the orig_node is to be removed, false otherwise.
1136 static bool batadv_purge_orig_node(struct batadv_priv
*bat_priv
,
1137 struct batadv_orig_node
*orig_node
)
1139 struct batadv_neigh_node
*best_neigh_node
;
1140 struct batadv_hard_iface
*hard_iface
;
1141 bool changed_ifinfo
, changed_neigh
;
1143 if (batadv_has_timed_out(orig_node
->last_seen
,
1144 2 * BATADV_PURGE_TIMEOUT
)) {
1145 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1146 "Originator timeout: originator %pM, last_seen %u\n",
1148 jiffies_to_msecs(orig_node
->last_seen
));
1151 changed_ifinfo
= batadv_purge_orig_ifinfo(bat_priv
, orig_node
);
1152 changed_neigh
= batadv_purge_orig_neighbors(bat_priv
, orig_node
);
1154 if (!changed_ifinfo
&& !changed_neigh
)
1157 /* first for NULL ... */
1158 best_neigh_node
= batadv_find_best_neighbor(bat_priv
, orig_node
,
1160 batadv_update_route(bat_priv
, orig_node
, BATADV_IF_DEFAULT
,
1162 if (best_neigh_node
)
1163 batadv_neigh_node_free_ref(best_neigh_node
);
1165 /* ... then for all other interfaces. */
1167 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1168 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1171 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
1174 best_neigh_node
= batadv_find_best_neighbor(bat_priv
,
1177 batadv_update_route(bat_priv
, orig_node
, hard_iface
,
1179 if (best_neigh_node
)
1180 batadv_neigh_node_free_ref(best_neigh_node
);
1187 static void _batadv_purge_orig(struct batadv_priv
*bat_priv
)
1189 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1190 struct hlist_node
*node_tmp
;
1191 struct hlist_head
*head
;
1192 spinlock_t
*list_lock
; /* spinlock to protect write access */
1193 struct batadv_orig_node
*orig_node
;
1199 /* for all origins... */
1200 for (i
= 0; i
< hash
->size
; i
++) {
1201 head
= &hash
->table
[i
];
1202 list_lock
= &hash
->list_locks
[i
];
1204 spin_lock_bh(list_lock
);
1205 hlist_for_each_entry_safe(orig_node
, node_tmp
,
1207 if (batadv_purge_orig_node(bat_priv
, orig_node
)) {
1208 batadv_gw_node_delete(bat_priv
, orig_node
);
1209 hlist_del_rcu(&orig_node
->hash_entry
);
1210 batadv_tt_global_del_orig(orig_node
->bat_priv
,
1212 "originator timed out");
1213 batadv_orig_node_free_ref(orig_node
);
1217 batadv_frag_purge_orig(orig_node
,
1218 batadv_frag_check_entry
);
1220 spin_unlock_bh(list_lock
);
1223 batadv_gw_election(bat_priv
);
1226 static void batadv_purge_orig(struct work_struct
*work
)
1228 struct delayed_work
*delayed_work
;
1229 struct batadv_priv
*bat_priv
;
1231 delayed_work
= container_of(work
, struct delayed_work
, work
);
1232 bat_priv
= container_of(delayed_work
, struct batadv_priv
, orig_work
);
1233 _batadv_purge_orig(bat_priv
);
1234 queue_delayed_work(batadv_event_workqueue
,
1235 &bat_priv
->orig_work
,
1236 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD
));
1239 void batadv_purge_orig_ref(struct batadv_priv
*bat_priv
)
1241 _batadv_purge_orig(bat_priv
);
1244 int batadv_orig_seq_print_text(struct seq_file
*seq
, void *offset
)
1246 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1247 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1248 struct batadv_hard_iface
*primary_if
;
1250 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1254 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1255 BATADV_SOURCE_VERSION
, primary_if
->net_dev
->name
,
1256 primary_if
->net_dev
->dev_addr
, net_dev
->name
,
1257 bat_priv
->bat_algo_ops
->name
);
1259 batadv_hardif_free_ref(primary_if
);
1261 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1263 "No printing function for this routing protocol\n");
1267 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
,
1274 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1275 * outgoing interface
1276 * @seq: debugfs table seq_file struct
1281 int batadv_orig_hardif_seq_print_text(struct seq_file
*seq
, void *offset
)
1283 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1284 struct batadv_hard_iface
*hard_iface
;
1285 struct batadv_priv
*bat_priv
;
1287 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
1289 if (!hard_iface
|| !hard_iface
->soft_iface
) {
1290 seq_puts(seq
, "Interface not known to B.A.T.M.A.N.\n");
1294 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1295 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1297 "No printing function for this routing protocol\n");
1301 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
) {
1302 seq_puts(seq
, "Interface not active\n");
1306 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1307 BATADV_SOURCE_VERSION
, hard_iface
->net_dev
->name
,
1308 hard_iface
->net_dev
->dev_addr
,
1309 hard_iface
->soft_iface
->name
, bat_priv
->bat_algo_ops
->name
);
1311 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
, hard_iface
);
1315 batadv_hardif_free_ref(hard_iface
);
1319 int batadv_orig_hash_add_if(struct batadv_hard_iface
*hard_iface
,
1322 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1323 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1324 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1325 struct hlist_head
*head
;
1326 struct batadv_orig_node
*orig_node
;
1330 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1333 for (i
= 0; i
< hash
->size
; i
++) {
1334 head
= &hash
->table
[i
];
1337 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1339 if (bao
->bat_orig_add_if
)
1340 ret
= bao
->bat_orig_add_if(orig_node
,
1355 int batadv_orig_hash_del_if(struct batadv_hard_iface
*hard_iface
,
1358 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1359 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1360 struct hlist_head
*head
;
1361 struct batadv_hard_iface
*hard_iface_tmp
;
1362 struct batadv_orig_node
*orig_node
;
1363 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1367 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1370 for (i
= 0; i
< hash
->size
; i
++) {
1371 head
= &hash
->table
[i
];
1374 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1376 if (bao
->bat_orig_del_if
)
1377 ret
= bao
->bat_orig_del_if(orig_node
,
1379 hard_iface
->if_num
);
1386 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1388 list_for_each_entry_rcu(hard_iface_tmp
, &batadv_hardif_list
, list
) {
1389 if (hard_iface_tmp
->if_status
== BATADV_IF_NOT_IN_USE
)
1392 if (hard_iface
== hard_iface_tmp
)
1395 if (hard_iface
->soft_iface
!= hard_iface_tmp
->soft_iface
)
1398 if (hard_iface_tmp
->if_num
> hard_iface
->if_num
)
1399 hard_iface_tmp
->if_num
--;
1403 hard_iface
->if_num
= -1;