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_release - release hardif neigh node from lists and
206 * queue for free after rcu grace period
207 * @hardif_neigh: hardif neigh neighbor to free
210 batadv_hardif_neigh_release(struct batadv_hardif_neigh_node
*hardif_neigh
)
212 spin_lock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
213 hlist_del_init_rcu(&hardif_neigh
->list
);
214 spin_unlock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
216 batadv_hardif_free_ref(hardif_neigh
->if_incoming
);
217 kfree_rcu(hardif_neigh
, rcu
);
221 * batadv_hardif_neigh_free_ref - decrement the hardif neighbors refcounter
222 * and possibly release it
223 * @hardif_neigh: hardif neigh neighbor to free
225 void batadv_hardif_neigh_free_ref(struct batadv_hardif_neigh_node
*hardif_neigh
)
227 if (atomic_dec_and_test(&hardif_neigh
->refcount
))
228 batadv_hardif_neigh_release(hardif_neigh
);
232 * batadv_neigh_node_free_rcu - free the neigh_node
233 * @rcu: rcu pointer of the neigh_node
235 static void batadv_neigh_node_free_rcu(struct rcu_head
*rcu
)
237 struct hlist_node
*node_tmp
;
238 struct batadv_neigh_node
*neigh_node
;
239 struct batadv_hardif_neigh_node
*hardif_neigh
;
240 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
241 struct batadv_algo_ops
*bao
;
243 neigh_node
= container_of(rcu
, struct batadv_neigh_node
, rcu
);
244 bao
= neigh_node
->orig_node
->bat_priv
->bat_algo_ops
;
246 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
247 &neigh_node
->ifinfo_list
, list
) {
248 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo
);
251 hardif_neigh
= batadv_hardif_neigh_get(neigh_node
->if_incoming
,
254 /* batadv_hardif_neigh_get() increases refcount too */
255 batadv_hardif_neigh_free_ref(hardif_neigh
);
256 batadv_hardif_neigh_free_ref(hardif_neigh
);
259 if (bao
->bat_neigh_free
)
260 bao
->bat_neigh_free(neigh_node
);
262 batadv_hardif_free_ref_now(neigh_node
->if_incoming
);
268 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
269 * and possibly release it
270 * @neigh_node: neigh neighbor to free
272 void batadv_neigh_node_free_ref(struct batadv_neigh_node
*neigh_node
)
274 if (atomic_dec_and_test(&neigh_node
->refcount
))
275 call_rcu(&neigh_node
->rcu
, batadv_neigh_node_free_rcu
);
279 * batadv_orig_node_get_router - router to the originator depending on iface
280 * @orig_node: the orig node for the router
281 * @if_outgoing: the interface where the payload packet has been received or
282 * the OGM should be sent to
284 * Returns the neighbor which should be router for this orig_node/iface.
286 * The object is returned with refcounter increased by 1.
288 struct batadv_neigh_node
*
289 batadv_orig_router_get(struct batadv_orig_node
*orig_node
,
290 const struct batadv_hard_iface
*if_outgoing
)
292 struct batadv_orig_ifinfo
*orig_ifinfo
;
293 struct batadv_neigh_node
*router
= NULL
;
296 hlist_for_each_entry_rcu(orig_ifinfo
, &orig_node
->ifinfo_list
, list
) {
297 if (orig_ifinfo
->if_outgoing
!= if_outgoing
)
300 router
= rcu_dereference(orig_ifinfo
->router
);
304 if (router
&& !atomic_inc_not_zero(&router
->refcount
))
312 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
313 * @orig_node: the orig node to be queried
314 * @if_outgoing: the interface for which the ifinfo should be acquired
316 * Returns the requested orig_ifinfo or NULL if not found.
318 * The object is returned with refcounter increased by 1.
320 struct batadv_orig_ifinfo
*
321 batadv_orig_ifinfo_get(struct batadv_orig_node
*orig_node
,
322 struct batadv_hard_iface
*if_outgoing
)
324 struct batadv_orig_ifinfo
*tmp
, *orig_ifinfo
= NULL
;
327 hlist_for_each_entry_rcu(tmp
, &orig_node
->ifinfo_list
,
329 if (tmp
->if_outgoing
!= if_outgoing
)
332 if (!atomic_inc_not_zero(&tmp
->refcount
))
344 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
345 * @orig_node: the orig node to be queried
346 * @if_outgoing: the interface for which the ifinfo should be acquired
348 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
349 * interface otherwise. The object is created and added to the list
350 * if it does not exist.
352 * The object is returned with refcounter increased by 1.
354 struct batadv_orig_ifinfo
*
355 batadv_orig_ifinfo_new(struct batadv_orig_node
*orig_node
,
356 struct batadv_hard_iface
*if_outgoing
)
358 struct batadv_orig_ifinfo
*orig_ifinfo
= NULL
;
359 unsigned long reset_time
;
361 spin_lock_bh(&orig_node
->neigh_list_lock
);
363 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, if_outgoing
);
367 orig_ifinfo
= kzalloc(sizeof(*orig_ifinfo
), GFP_ATOMIC
);
371 if (if_outgoing
!= BATADV_IF_DEFAULT
&&
372 !atomic_inc_not_zero(&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 atomic_set(&orig_ifinfo
->refcount
, 2);
384 hlist_add_head_rcu(&orig_ifinfo
->list
,
385 &orig_node
->ifinfo_list
);
387 spin_unlock_bh(&orig_node
->neigh_list_lock
);
392 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
393 * @neigh_node: the neigh node to be queried
394 * @if_outgoing: the interface for which the ifinfo should be acquired
396 * The object is returned with refcounter increased by 1.
398 * Returns the requested neigh_ifinfo or NULL if not found
400 struct batadv_neigh_ifinfo
*
401 batadv_neigh_ifinfo_get(struct batadv_neigh_node
*neigh
,
402 struct batadv_hard_iface
*if_outgoing
)
404 struct batadv_neigh_ifinfo
*neigh_ifinfo
= NULL
,
408 hlist_for_each_entry_rcu(tmp_neigh_ifinfo
, &neigh
->ifinfo_list
,
410 if (tmp_neigh_ifinfo
->if_outgoing
!= if_outgoing
)
413 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo
->refcount
))
416 neigh_ifinfo
= tmp_neigh_ifinfo
;
425 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
426 * @neigh_node: the neigh node to be queried
427 * @if_outgoing: the interface for which the ifinfo should be acquired
429 * Returns NULL in case of failure or the neigh_ifinfo object for the
430 * if_outgoing interface otherwise. The object is created and added to the list
431 * if it does not exist.
433 * The object is returned with refcounter increased by 1.
435 struct batadv_neigh_ifinfo
*
436 batadv_neigh_ifinfo_new(struct batadv_neigh_node
*neigh
,
437 struct batadv_hard_iface
*if_outgoing
)
439 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
441 spin_lock_bh(&neigh
->ifinfo_lock
);
443 neigh_ifinfo
= batadv_neigh_ifinfo_get(neigh
, if_outgoing
);
447 neigh_ifinfo
= kzalloc(sizeof(*neigh_ifinfo
), GFP_ATOMIC
);
451 if (if_outgoing
&& !atomic_inc_not_zero(&if_outgoing
->refcount
)) {
457 INIT_HLIST_NODE(&neigh_ifinfo
->list
);
458 atomic_set(&neigh_ifinfo
->refcount
, 2);
459 neigh_ifinfo
->if_outgoing
= if_outgoing
;
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.
477 * Returns NULL if the neighbour is not found.
479 static struct batadv_neigh_node
*
480 batadv_neigh_node_get(const struct batadv_orig_node
*orig_node
,
481 const struct batadv_hard_iface
*hard_iface
,
484 struct batadv_neigh_node
*tmp_neigh_node
, *res
= NULL
;
487 hlist_for_each_entry_rcu(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
488 if (!batadv_compare_eth(tmp_neigh_node
->addr
, addr
))
491 if (tmp_neigh_node
->if_incoming
!= hard_iface
)
494 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
497 res
= tmp_neigh_node
;
506 * batadv_hardif_neigh_create - create a hardif neighbour node
507 * @hard_iface: the interface this neighbour is connected to
508 * @neigh_addr: the interface address of the neighbour to retrieve
510 * Returns the hardif neighbour node if found or created or NULL otherwise.
512 static struct batadv_hardif_neigh_node
*
513 batadv_hardif_neigh_create(struct batadv_hard_iface
*hard_iface
,
514 const u8
*neigh_addr
)
516 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
517 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
519 spin_lock_bh(&hard_iface
->neigh_list_lock
);
521 /* check if neighbor hasn't been added in the meantime */
522 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
526 if (!atomic_inc_not_zero(&hard_iface
->refcount
))
529 hardif_neigh
= kzalloc(sizeof(*hardif_neigh
), GFP_ATOMIC
);
531 batadv_hardif_free_ref(hard_iface
);
535 INIT_HLIST_NODE(&hardif_neigh
->list
);
536 ether_addr_copy(hardif_neigh
->addr
, neigh_addr
);
537 hardif_neigh
->if_incoming
= hard_iface
;
538 hardif_neigh
->last_seen
= jiffies
;
540 atomic_set(&hardif_neigh
->refcount
, 1);
542 if (bat_priv
->bat_algo_ops
->bat_hardif_neigh_init
)
543 bat_priv
->bat_algo_ops
->bat_hardif_neigh_init(hardif_neigh
);
545 hlist_add_head(&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
558 * Returns the hardif neighbour node if found or created or NULL otherwise.
560 static struct batadv_hardif_neigh_node
*
561 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface
*hard_iface
,
562 const u8
*neigh_addr
)
564 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
566 /* first check without locking to avoid the overhead */
567 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
571 return batadv_hardif_neigh_create(hard_iface
, neigh_addr
);
575 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
576 * @hard_iface: the interface where this neighbour is connected to
577 * @neigh_addr: the address of the neighbour
579 * Looks for and possibly returns a neighbour belonging to this hard interface.
580 * Returns NULL if the neighbour is not found.
582 struct batadv_hardif_neigh_node
*
583 batadv_hardif_neigh_get(const struct batadv_hard_iface
*hard_iface
,
584 const u8
*neigh_addr
)
586 struct batadv_hardif_neigh_node
*tmp_hardif_neigh
, *hardif_neigh
= NULL
;
589 hlist_for_each_entry_rcu(tmp_hardif_neigh
,
590 &hard_iface
->neigh_list
, list
) {
591 if (!batadv_compare_eth(tmp_hardif_neigh
->addr
, neigh_addr
))
594 if (!atomic_inc_not_zero(&tmp_hardif_neigh
->refcount
))
597 hardif_neigh
= tmp_hardif_neigh
;
606 * batadv_neigh_node_new - create and init a new neigh_node object
607 * @orig_node: originator object representing the neighbour
608 * @hard_iface: the interface where the neighbour is connected to
609 * @neigh_addr: the mac address of the neighbour interface
611 * Allocates a new neigh_node object and initialises all the generic fields.
612 * Returns the new object or NULL on failure.
614 struct batadv_neigh_node
*
615 batadv_neigh_node_new(struct batadv_orig_node
*orig_node
,
616 struct batadv_hard_iface
*hard_iface
,
617 const u8
*neigh_addr
)
619 struct batadv_neigh_node
*neigh_node
;
620 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
622 neigh_node
= batadv_neigh_node_get(orig_node
, hard_iface
, neigh_addr
);
626 hardif_neigh
= batadv_hardif_neigh_get_or_create(hard_iface
,
631 neigh_node
= kzalloc(sizeof(*neigh_node
), GFP_ATOMIC
);
635 if (!atomic_inc_not_zero(&hard_iface
->refcount
)) {
641 INIT_HLIST_NODE(&neigh_node
->list
);
642 INIT_HLIST_HEAD(&neigh_node
->ifinfo_list
);
643 spin_lock_init(&neigh_node
->ifinfo_lock
);
645 ether_addr_copy(neigh_node
->addr
, neigh_addr
);
646 neigh_node
->if_incoming
= hard_iface
;
647 neigh_node
->orig_node
= orig_node
;
649 /* extra reference for return */
650 atomic_set(&neigh_node
->refcount
, 2);
652 spin_lock_bh(&orig_node
->neigh_list_lock
);
653 hlist_add_head_rcu(&neigh_node
->list
, &orig_node
->neigh_list
);
654 spin_unlock_bh(&orig_node
->neigh_list_lock
);
656 /* increment unique neighbor refcount */
657 atomic_inc(&hardif_neigh
->refcount
);
659 batadv_dbg(BATADV_DBG_BATMAN
, orig_node
->bat_priv
,
660 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
661 neigh_addr
, orig_node
->orig
, hard_iface
->net_dev
->name
);
665 batadv_hardif_neigh_free_ref(hardif_neigh
);
670 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
671 * @seq: neighbour table seq_file struct
676 int batadv_hardif_neigh_seq_print_text(struct seq_file
*seq
, void *offset
)
678 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
679 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
680 struct batadv_hard_iface
*primary_if
;
682 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
686 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
687 BATADV_SOURCE_VERSION
, primary_if
->net_dev
->name
,
688 primary_if
->net_dev
->dev_addr
, net_dev
->name
,
689 bat_priv
->bat_algo_ops
->name
);
691 batadv_hardif_free_ref(primary_if
);
693 if (!bat_priv
->bat_algo_ops
->bat_neigh_print
) {
695 "No printing function for this routing protocol\n");
699 bat_priv
->bat_algo_ops
->bat_neigh_print(bat_priv
, seq
);
704 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
705 * free after rcu grace period
706 * @orig_ifinfo: the orig_ifinfo object to release
708 static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo
*orig_ifinfo
)
710 struct batadv_neigh_node
*router
;
712 if (orig_ifinfo
->if_outgoing
!= BATADV_IF_DEFAULT
)
713 batadv_hardif_free_ref(orig_ifinfo
->if_outgoing
);
715 /* this is the last reference to this object */
716 router
= rcu_dereference_protected(orig_ifinfo
->router
, true);
718 batadv_neigh_node_free_ref(router
);
720 kfree_rcu(orig_ifinfo
, rcu
);
724 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
726 * @orig_ifinfo: the orig_ifinfo object to release
728 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo
*orig_ifinfo
)
730 if (atomic_dec_and_test(&orig_ifinfo
->refcount
))
731 batadv_orig_ifinfo_release(orig_ifinfo
);
735 * batadv_orig_node_free_rcu - free the orig_node
736 * @rcu: rcu pointer of the orig_node
738 static void batadv_orig_node_free_rcu(struct rcu_head
*rcu
)
740 struct batadv_orig_node
*orig_node
;
742 orig_node
= container_of(rcu
, struct batadv_orig_node
, rcu
);
744 batadv_mcast_purge_orig(orig_node
);
746 batadv_frag_purge_orig(orig_node
, NULL
);
748 if (orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free
)
749 orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free(orig_node
);
751 kfree(orig_node
->tt_buff
);
756 * batadv_orig_node_release - release orig_node from lists and queue for
757 * free after rcu grace period
758 * @orig_node: the orig node to free
760 static void batadv_orig_node_release(struct batadv_orig_node
*orig_node
)
762 struct hlist_node
*node_tmp
;
763 struct batadv_neigh_node
*neigh_node
;
764 struct batadv_orig_ifinfo
*orig_ifinfo
;
766 spin_lock_bh(&orig_node
->neigh_list_lock
);
768 /* for all neighbors towards this originator ... */
769 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
770 &orig_node
->neigh_list
, list
) {
771 hlist_del_rcu(&neigh_node
->list
);
772 batadv_neigh_node_free_ref(neigh_node
);
775 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
776 &orig_node
->ifinfo_list
, list
) {
777 hlist_del_rcu(&orig_ifinfo
->list
);
778 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
780 spin_unlock_bh(&orig_node
->neigh_list_lock
);
783 batadv_nc_purge_orig(orig_node
->bat_priv
, orig_node
, NULL
);
785 call_rcu(&orig_node
->rcu
, batadv_orig_node_free_rcu
);
789 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
791 * @orig_node: the orig node to free
793 void batadv_orig_node_free_ref(struct batadv_orig_node
*orig_node
)
795 if (atomic_dec_and_test(&orig_node
->refcount
))
796 batadv_orig_node_release(orig_node
);
800 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
801 * possibly free it (without rcu callback)
802 * @orig_node: the orig node to free
804 void batadv_orig_node_free_ref_now(struct batadv_orig_node
*orig_node
)
806 if (atomic_dec_and_test(&orig_node
->refcount
))
807 batadv_orig_node_free_rcu(&orig_node
->rcu
);
810 void batadv_originator_free(struct batadv_priv
*bat_priv
)
812 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
813 struct hlist_node
*node_tmp
;
814 struct hlist_head
*head
;
815 spinlock_t
*list_lock
; /* spinlock to protect write access */
816 struct batadv_orig_node
*orig_node
;
822 cancel_delayed_work_sync(&bat_priv
->orig_work
);
824 bat_priv
->orig_hash
= NULL
;
826 for (i
= 0; i
< hash
->size
; i
++) {
827 head
= &hash
->table
[i
];
828 list_lock
= &hash
->list_locks
[i
];
830 spin_lock_bh(list_lock
);
831 hlist_for_each_entry_safe(orig_node
, node_tmp
,
833 hlist_del_rcu(&orig_node
->hash_entry
);
834 batadv_orig_node_free_ref(orig_node
);
836 spin_unlock_bh(list_lock
);
839 batadv_hash_destroy(hash
);
843 * batadv_orig_node_new - creates a new orig_node
844 * @bat_priv: the bat priv with all the soft interface information
845 * @addr: the mac address of the originator
847 * Creates a new originator object and initialise all the generic fields.
848 * The new object is not added to the originator list.
849 * Returns the newly created object or NULL on failure.
851 struct batadv_orig_node
*batadv_orig_node_new(struct batadv_priv
*bat_priv
,
854 struct batadv_orig_node
*orig_node
;
855 struct batadv_orig_node_vlan
*vlan
;
856 unsigned long reset_time
;
859 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
860 "Creating new originator: %pM\n", addr
);
862 orig_node
= kzalloc(sizeof(*orig_node
), GFP_ATOMIC
);
866 INIT_HLIST_HEAD(&orig_node
->neigh_list
);
867 INIT_HLIST_HEAD(&orig_node
->vlan_list
);
868 INIT_HLIST_HEAD(&orig_node
->ifinfo_list
);
869 spin_lock_init(&orig_node
->bcast_seqno_lock
);
870 spin_lock_init(&orig_node
->neigh_list_lock
);
871 spin_lock_init(&orig_node
->tt_buff_lock
);
872 spin_lock_init(&orig_node
->tt_lock
);
873 spin_lock_init(&orig_node
->vlan_list_lock
);
875 batadv_nc_init_orig(orig_node
);
877 /* extra reference for return */
878 atomic_set(&orig_node
->refcount
, 2);
880 orig_node
->bat_priv
= bat_priv
;
881 ether_addr_copy(orig_node
->orig
, addr
);
882 batadv_dat_init_orig_node_addr(orig_node
);
883 atomic_set(&orig_node
->last_ttvn
, 0);
884 orig_node
->tt_buff
= NULL
;
885 orig_node
->tt_buff_len
= 0;
886 orig_node
->last_seen
= jiffies
;
887 reset_time
= jiffies
- 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
888 orig_node
->bcast_seqno_reset
= reset_time
;
890 #ifdef CONFIG_BATMAN_ADV_MCAST
891 orig_node
->mcast_flags
= BATADV_NO_FLAGS
;
892 INIT_HLIST_NODE(&orig_node
->mcast_want_all_unsnoopables_node
);
893 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv4_node
);
894 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv6_node
);
895 spin_lock_init(&orig_node
->mcast_handler_lock
);
898 /* create a vlan object for the "untagged" LAN */
899 vlan
= batadv_orig_node_vlan_new(orig_node
, BATADV_NO_FLAGS
);
902 /* batadv_orig_node_vlan_new() increases the refcounter.
903 * Immediately release vlan since it is not needed anymore in this
906 batadv_orig_node_vlan_free_ref(vlan
);
908 for (i
= 0; i
< BATADV_FRAG_BUFFER_COUNT
; i
++) {
909 INIT_HLIST_HEAD(&orig_node
->fragments
[i
].head
);
910 spin_lock_init(&orig_node
->fragments
[i
].lock
);
911 orig_node
->fragments
[i
].size
= 0;
921 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
922 * @bat_priv: the bat priv with all the soft interface information
923 * @neigh: orig node which is to be checked
926 batadv_purge_neigh_ifinfo(struct batadv_priv
*bat_priv
,
927 struct batadv_neigh_node
*neigh
)
929 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
930 struct batadv_hard_iface
*if_outgoing
;
931 struct hlist_node
*node_tmp
;
933 spin_lock_bh(&neigh
->ifinfo_lock
);
935 /* for all ifinfo objects for this neighinator */
936 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
937 &neigh
->ifinfo_list
, list
) {
938 if_outgoing
= neigh_ifinfo
->if_outgoing
;
940 /* always keep the default interface */
941 if (if_outgoing
== BATADV_IF_DEFAULT
)
944 /* don't purge if the interface is not (going) down */
945 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
946 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
947 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
950 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
951 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
952 neigh
->addr
, if_outgoing
->net_dev
->name
);
954 hlist_del_rcu(&neigh_ifinfo
->list
);
955 batadv_neigh_ifinfo_free_ref(neigh_ifinfo
);
958 spin_unlock_bh(&neigh
->ifinfo_lock
);
962 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
963 * @bat_priv: the bat priv with all the soft interface information
964 * @orig_node: orig node which is to be checked
966 * Returns true if any ifinfo entry was purged, false otherwise.
969 batadv_purge_orig_ifinfo(struct batadv_priv
*bat_priv
,
970 struct batadv_orig_node
*orig_node
)
972 struct batadv_orig_ifinfo
*orig_ifinfo
;
973 struct batadv_hard_iface
*if_outgoing
;
974 struct hlist_node
*node_tmp
;
975 bool ifinfo_purged
= false;
977 spin_lock_bh(&orig_node
->neigh_list_lock
);
979 /* for all ifinfo objects for this originator */
980 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
981 &orig_node
->ifinfo_list
, list
) {
982 if_outgoing
= orig_ifinfo
->if_outgoing
;
984 /* always keep the default interface */
985 if (if_outgoing
== BATADV_IF_DEFAULT
)
988 /* don't purge if the interface is not (going) down */
989 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
990 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
991 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
994 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
995 "router/ifinfo purge: originator %pM, iface: %s\n",
996 orig_node
->orig
, if_outgoing
->net_dev
->name
);
998 ifinfo_purged
= true;
1000 hlist_del_rcu(&orig_ifinfo
->list
);
1001 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
1002 if (orig_node
->last_bonding_candidate
== orig_ifinfo
) {
1003 orig_node
->last_bonding_candidate
= NULL
;
1004 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
1008 spin_unlock_bh(&orig_node
->neigh_list_lock
);
1010 return ifinfo_purged
;
1014 * batadv_purge_orig_neighbors - purges neighbors from originator
1015 * @bat_priv: the bat priv with all the soft interface information
1016 * @orig_node: orig node which is to be checked
1018 * Returns true if any neighbor was purged, false otherwise
1021 batadv_purge_orig_neighbors(struct batadv_priv
*bat_priv
,
1022 struct batadv_orig_node
*orig_node
)
1024 struct hlist_node
*node_tmp
;
1025 struct batadv_neigh_node
*neigh_node
;
1026 bool neigh_purged
= false;
1027 unsigned long last_seen
;
1028 struct batadv_hard_iface
*if_incoming
;
1030 spin_lock_bh(&orig_node
->neigh_list_lock
);
1032 /* for all neighbors towards this originator ... */
1033 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
1034 &orig_node
->neigh_list
, list
) {
1035 last_seen
= neigh_node
->last_seen
;
1036 if_incoming
= neigh_node
->if_incoming
;
1038 if ((batadv_has_timed_out(last_seen
, BATADV_PURGE_TIMEOUT
)) ||
1039 (if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
1040 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
1041 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
)) {
1042 if ((if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
1043 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
1044 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
))
1045 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1046 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1047 orig_node
->orig
, neigh_node
->addr
,
1048 if_incoming
->net_dev
->name
);
1050 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1051 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1052 orig_node
->orig
, neigh_node
->addr
,
1053 jiffies_to_msecs(last_seen
));
1055 neigh_purged
= true;
1057 hlist_del_rcu(&neigh_node
->list
);
1058 batadv_neigh_node_free_ref(neigh_node
);
1060 /* only necessary if not the whole neighbor is to be
1061 * deleted, but some interface has been removed.
1063 batadv_purge_neigh_ifinfo(bat_priv
, neigh_node
);
1067 spin_unlock_bh(&orig_node
->neigh_list_lock
);
1068 return neigh_purged
;
1072 * batadv_find_best_neighbor - finds the best neighbor after purging
1073 * @bat_priv: the bat priv with all the soft interface information
1074 * @orig_node: orig node which is to be checked
1075 * @if_outgoing: the interface for which the metric should be compared
1077 * Returns the current best neighbor, with refcount increased.
1079 static struct batadv_neigh_node
*
1080 batadv_find_best_neighbor(struct batadv_priv
*bat_priv
,
1081 struct batadv_orig_node
*orig_node
,
1082 struct batadv_hard_iface
*if_outgoing
)
1084 struct batadv_neigh_node
*best
= NULL
, *neigh
;
1085 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1088 hlist_for_each_entry_rcu(neigh
, &orig_node
->neigh_list
, list
) {
1089 if (best
&& (bao
->bat_neigh_cmp(neigh
, if_outgoing
,
1090 best
, if_outgoing
) <= 0))
1093 if (!atomic_inc_not_zero(&neigh
->refcount
))
1097 batadv_neigh_node_free_ref(best
);
1107 * batadv_purge_orig_node - purges obsolete information from an orig_node
1108 * @bat_priv: the bat priv with all the soft interface information
1109 * @orig_node: orig node which is to be checked
1111 * This function checks if the orig_node or substructures of it have become
1112 * obsolete, and purges this information if that's the case.
1114 * Returns true if the orig_node is to be removed, false otherwise.
1116 static bool batadv_purge_orig_node(struct batadv_priv
*bat_priv
,
1117 struct batadv_orig_node
*orig_node
)
1119 struct batadv_neigh_node
*best_neigh_node
;
1120 struct batadv_hard_iface
*hard_iface
;
1121 bool changed_ifinfo
, changed_neigh
;
1123 if (batadv_has_timed_out(orig_node
->last_seen
,
1124 2 * BATADV_PURGE_TIMEOUT
)) {
1125 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1126 "Originator timeout: originator %pM, last_seen %u\n",
1128 jiffies_to_msecs(orig_node
->last_seen
));
1131 changed_ifinfo
= batadv_purge_orig_ifinfo(bat_priv
, orig_node
);
1132 changed_neigh
= batadv_purge_orig_neighbors(bat_priv
, orig_node
);
1134 if (!changed_ifinfo
&& !changed_neigh
)
1137 /* first for NULL ... */
1138 best_neigh_node
= batadv_find_best_neighbor(bat_priv
, orig_node
,
1140 batadv_update_route(bat_priv
, orig_node
, BATADV_IF_DEFAULT
,
1142 if (best_neigh_node
)
1143 batadv_neigh_node_free_ref(best_neigh_node
);
1145 /* ... then for all other interfaces. */
1147 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1148 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1151 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
1154 best_neigh_node
= batadv_find_best_neighbor(bat_priv
,
1157 batadv_update_route(bat_priv
, orig_node
, hard_iface
,
1159 if (best_neigh_node
)
1160 batadv_neigh_node_free_ref(best_neigh_node
);
1167 static void _batadv_purge_orig(struct batadv_priv
*bat_priv
)
1169 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1170 struct hlist_node
*node_tmp
;
1171 struct hlist_head
*head
;
1172 spinlock_t
*list_lock
; /* spinlock to protect write access */
1173 struct batadv_orig_node
*orig_node
;
1179 /* for all origins... */
1180 for (i
= 0; i
< hash
->size
; i
++) {
1181 head
= &hash
->table
[i
];
1182 list_lock
= &hash
->list_locks
[i
];
1184 spin_lock_bh(list_lock
);
1185 hlist_for_each_entry_safe(orig_node
, node_tmp
,
1187 if (batadv_purge_orig_node(bat_priv
, orig_node
)) {
1188 batadv_gw_node_delete(bat_priv
, orig_node
);
1189 hlist_del_rcu(&orig_node
->hash_entry
);
1190 batadv_tt_global_del_orig(orig_node
->bat_priv
,
1192 "originator timed out");
1193 batadv_orig_node_free_ref(orig_node
);
1197 batadv_frag_purge_orig(orig_node
,
1198 batadv_frag_check_entry
);
1200 spin_unlock_bh(list_lock
);
1203 batadv_gw_election(bat_priv
);
1206 static void batadv_purge_orig(struct work_struct
*work
)
1208 struct delayed_work
*delayed_work
;
1209 struct batadv_priv
*bat_priv
;
1211 delayed_work
= container_of(work
, struct delayed_work
, work
);
1212 bat_priv
= container_of(delayed_work
, struct batadv_priv
, orig_work
);
1213 _batadv_purge_orig(bat_priv
);
1214 queue_delayed_work(batadv_event_workqueue
,
1215 &bat_priv
->orig_work
,
1216 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD
));
1219 void batadv_purge_orig_ref(struct batadv_priv
*bat_priv
)
1221 _batadv_purge_orig(bat_priv
);
1224 int batadv_orig_seq_print_text(struct seq_file
*seq
, void *offset
)
1226 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1227 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1228 struct batadv_hard_iface
*primary_if
;
1230 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1234 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1235 BATADV_SOURCE_VERSION
, primary_if
->net_dev
->name
,
1236 primary_if
->net_dev
->dev_addr
, net_dev
->name
,
1237 bat_priv
->bat_algo_ops
->name
);
1239 batadv_hardif_free_ref(primary_if
);
1241 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1243 "No printing function for this routing protocol\n");
1247 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
,
1254 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1255 * outgoing interface
1256 * @seq: debugfs table seq_file struct
1261 int batadv_orig_hardif_seq_print_text(struct seq_file
*seq
, void *offset
)
1263 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1264 struct batadv_hard_iface
*hard_iface
;
1265 struct batadv_priv
*bat_priv
;
1267 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
1269 if (!hard_iface
|| !hard_iface
->soft_iface
) {
1270 seq_puts(seq
, "Interface not known to B.A.T.M.A.N.\n");
1274 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1275 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1277 "No printing function for this routing protocol\n");
1281 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
) {
1282 seq_puts(seq
, "Interface not active\n");
1286 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1287 BATADV_SOURCE_VERSION
, hard_iface
->net_dev
->name
,
1288 hard_iface
->net_dev
->dev_addr
,
1289 hard_iface
->soft_iface
->name
, bat_priv
->bat_algo_ops
->name
);
1291 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
, hard_iface
);
1295 batadv_hardif_free_ref(hard_iface
);
1299 int batadv_orig_hash_add_if(struct batadv_hard_iface
*hard_iface
,
1302 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1303 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1304 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1305 struct hlist_head
*head
;
1306 struct batadv_orig_node
*orig_node
;
1310 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1313 for (i
= 0; i
< hash
->size
; i
++) {
1314 head
= &hash
->table
[i
];
1317 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1319 if (bao
->bat_orig_add_if
)
1320 ret
= bao
->bat_orig_add_if(orig_node
,
1335 int batadv_orig_hash_del_if(struct batadv_hard_iface
*hard_iface
,
1338 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1339 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1340 struct hlist_head
*head
;
1341 struct batadv_hard_iface
*hard_iface_tmp
;
1342 struct batadv_orig_node
*orig_node
;
1343 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1347 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1350 for (i
= 0; i
< hash
->size
; i
++) {
1351 head
= &hash
->table
[i
];
1354 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1356 if (bao
->bat_orig_del_if
)
1357 ret
= bao
->bat_orig_del_if(orig_node
,
1359 hard_iface
->if_num
);
1366 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1368 list_for_each_entry_rcu(hard_iface_tmp
, &batadv_hardif_list
, list
) {
1369 if (hard_iface_tmp
->if_status
== BATADV_IF_NOT_IN_USE
)
1372 if (hard_iface
== hard_iface_tmp
)
1375 if (hard_iface
->soft_iface
!= hard_iface_tmp
->soft_iface
)
1378 if (hard_iface_tmp
->if_num
> hard_iface
->if_num
)
1379 hard_iface_tmp
->if_num
--;
1383 hard_iface
->if_num
= -1;