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 spin_lock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
215 hlist_del_init_rcu(&hardif_neigh
->list
);
216 spin_unlock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
218 batadv_hardif_free_ref_now(hardif_neigh
->if_incoming
);
223 * batadv_hardif_neigh_free_now - decrement the hardif neighbors refcounter
224 * and possibly free it (without rcu callback)
225 * @hardif_neigh: hardif neigh neighbor to free
228 batadv_hardif_neigh_free_now(struct batadv_hardif_neigh_node
*hardif_neigh
)
230 if (atomic_dec_and_test(&hardif_neigh
->refcount
))
231 batadv_hardif_neigh_free_rcu(&hardif_neigh
->rcu
);
235 * batadv_hardif_neigh_free_ref - decrement the hardif neighbors refcounter
236 * and possibly free it
237 * @hardif_neigh: hardif neigh neighbor to free
239 void batadv_hardif_neigh_free_ref(struct batadv_hardif_neigh_node
*hardif_neigh
)
241 if (atomic_dec_and_test(&hardif_neigh
->refcount
))
242 call_rcu(&hardif_neigh
->rcu
, batadv_hardif_neigh_free_rcu
);
246 * batadv_neigh_node_free_rcu - free the neigh_node
247 * @rcu: rcu pointer of the neigh_node
249 static void batadv_neigh_node_free_rcu(struct rcu_head
*rcu
)
251 struct hlist_node
*node_tmp
;
252 struct batadv_neigh_node
*neigh_node
;
253 struct batadv_hardif_neigh_node
*hardif_neigh
;
254 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
255 struct batadv_algo_ops
*bao
;
257 neigh_node
= container_of(rcu
, struct batadv_neigh_node
, rcu
);
258 bao
= neigh_node
->orig_node
->bat_priv
->bat_algo_ops
;
260 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
261 &neigh_node
->ifinfo_list
, list
) {
262 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo
);
265 hardif_neigh
= batadv_hardif_neigh_get(neigh_node
->if_incoming
,
268 /* batadv_hardif_neigh_get() increases refcount too */
269 batadv_hardif_neigh_free_now(hardif_neigh
);
270 batadv_hardif_neigh_free_now(hardif_neigh
);
273 if (bao
->bat_neigh_free
)
274 bao
->bat_neigh_free(neigh_node
);
276 batadv_hardif_free_ref_now(neigh_node
->if_incoming
);
282 * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter
283 * and possibly free it (without rcu callback)
284 * @neigh_node: neigh neighbor to free
287 batadv_neigh_node_free_ref_now(struct batadv_neigh_node
*neigh_node
)
289 if (atomic_dec_and_test(&neigh_node
->refcount
))
290 batadv_neigh_node_free_rcu(&neigh_node
->rcu
);
294 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
295 * and possibly free it
296 * @neigh_node: neigh neighbor to free
298 void batadv_neigh_node_free_ref(struct batadv_neigh_node
*neigh_node
)
300 if (atomic_dec_and_test(&neigh_node
->refcount
))
301 call_rcu(&neigh_node
->rcu
, batadv_neigh_node_free_rcu
);
305 * batadv_orig_node_get_router - router to the originator depending on iface
306 * @orig_node: the orig node for the router
307 * @if_outgoing: the interface where the payload packet has been received or
308 * the OGM should be sent to
310 * Returns the neighbor which should be router for this orig_node/iface.
312 * The object is returned with refcounter increased by 1.
314 struct batadv_neigh_node
*
315 batadv_orig_router_get(struct batadv_orig_node
*orig_node
,
316 const struct batadv_hard_iface
*if_outgoing
)
318 struct batadv_orig_ifinfo
*orig_ifinfo
;
319 struct batadv_neigh_node
*router
= NULL
;
322 hlist_for_each_entry_rcu(orig_ifinfo
, &orig_node
->ifinfo_list
, list
) {
323 if (orig_ifinfo
->if_outgoing
!= if_outgoing
)
326 router
= rcu_dereference(orig_ifinfo
->router
);
330 if (router
&& !atomic_inc_not_zero(&router
->refcount
))
338 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
339 * @orig_node: the orig node to be queried
340 * @if_outgoing: the interface for which the ifinfo should be acquired
342 * Returns the requested orig_ifinfo or NULL if not found.
344 * The object is returned with refcounter increased by 1.
346 struct batadv_orig_ifinfo
*
347 batadv_orig_ifinfo_get(struct batadv_orig_node
*orig_node
,
348 struct batadv_hard_iface
*if_outgoing
)
350 struct batadv_orig_ifinfo
*tmp
, *orig_ifinfo
= NULL
;
353 hlist_for_each_entry_rcu(tmp
, &orig_node
->ifinfo_list
,
355 if (tmp
->if_outgoing
!= if_outgoing
)
358 if (!atomic_inc_not_zero(&tmp
->refcount
))
370 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
371 * @orig_node: the orig node to be queried
372 * @if_outgoing: the interface for which the ifinfo should be acquired
374 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
375 * interface otherwise. The object is created and added to the list
376 * if it does not exist.
378 * The object is returned with refcounter increased by 1.
380 struct batadv_orig_ifinfo
*
381 batadv_orig_ifinfo_new(struct batadv_orig_node
*orig_node
,
382 struct batadv_hard_iface
*if_outgoing
)
384 struct batadv_orig_ifinfo
*orig_ifinfo
= NULL
;
385 unsigned long reset_time
;
387 spin_lock_bh(&orig_node
->neigh_list_lock
);
389 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, if_outgoing
);
393 orig_ifinfo
= kzalloc(sizeof(*orig_ifinfo
), GFP_ATOMIC
);
397 if (if_outgoing
!= BATADV_IF_DEFAULT
&&
398 !atomic_inc_not_zero(&if_outgoing
->refcount
)) {
404 reset_time
= jiffies
- 1;
405 reset_time
-= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
406 orig_ifinfo
->batman_seqno_reset
= reset_time
;
407 orig_ifinfo
->if_outgoing
= if_outgoing
;
408 INIT_HLIST_NODE(&orig_ifinfo
->list
);
409 atomic_set(&orig_ifinfo
->refcount
, 2);
410 hlist_add_head_rcu(&orig_ifinfo
->list
,
411 &orig_node
->ifinfo_list
);
413 spin_unlock_bh(&orig_node
->neigh_list_lock
);
418 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
419 * @neigh_node: the neigh node to be queried
420 * @if_outgoing: the interface for which the ifinfo should be acquired
422 * The object is returned with refcounter increased by 1.
424 * Returns the requested neigh_ifinfo or NULL if not found
426 struct batadv_neigh_ifinfo
*
427 batadv_neigh_ifinfo_get(struct batadv_neigh_node
*neigh
,
428 struct batadv_hard_iface
*if_outgoing
)
430 struct batadv_neigh_ifinfo
*neigh_ifinfo
= NULL
,
434 hlist_for_each_entry_rcu(tmp_neigh_ifinfo
, &neigh
->ifinfo_list
,
436 if (tmp_neigh_ifinfo
->if_outgoing
!= if_outgoing
)
439 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo
->refcount
))
442 neigh_ifinfo
= tmp_neigh_ifinfo
;
451 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
452 * @neigh_node: the neigh node to be queried
453 * @if_outgoing: the interface for which the ifinfo should be acquired
455 * Returns NULL in case of failure or the neigh_ifinfo object for the
456 * if_outgoing interface otherwise. The object is created and added to the list
457 * if it does not exist.
459 * The object is returned with refcounter increased by 1.
461 struct batadv_neigh_ifinfo
*
462 batadv_neigh_ifinfo_new(struct batadv_neigh_node
*neigh
,
463 struct batadv_hard_iface
*if_outgoing
)
465 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
467 spin_lock_bh(&neigh
->ifinfo_lock
);
469 neigh_ifinfo
= batadv_neigh_ifinfo_get(neigh
, if_outgoing
);
473 neigh_ifinfo
= kzalloc(sizeof(*neigh_ifinfo
), GFP_ATOMIC
);
477 if (if_outgoing
&& !atomic_inc_not_zero(&if_outgoing
->refcount
)) {
483 INIT_HLIST_NODE(&neigh_ifinfo
->list
);
484 atomic_set(&neigh_ifinfo
->refcount
, 2);
485 neigh_ifinfo
->if_outgoing
= if_outgoing
;
487 hlist_add_head_rcu(&neigh_ifinfo
->list
, &neigh
->ifinfo_list
);
490 spin_unlock_bh(&neigh
->ifinfo_lock
);
496 * batadv_neigh_node_get - retrieve a neighbour from the list
497 * @orig_node: originator which the neighbour belongs to
498 * @hard_iface: the interface where this neighbour is connected to
499 * @addr: the address of the neighbour
501 * Looks for and possibly returns a neighbour belonging to this originator list
502 * which is connected through the provided hard interface.
503 * Returns NULL if the neighbour is not found.
505 static struct batadv_neigh_node
*
506 batadv_neigh_node_get(const struct batadv_orig_node
*orig_node
,
507 const struct batadv_hard_iface
*hard_iface
,
510 struct batadv_neigh_node
*tmp_neigh_node
, *res
= NULL
;
513 hlist_for_each_entry_rcu(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
514 if (!batadv_compare_eth(tmp_neigh_node
->addr
, addr
))
517 if (tmp_neigh_node
->if_incoming
!= hard_iface
)
520 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
523 res
= tmp_neigh_node
;
532 * batadv_hardif_neigh_create - create a hardif neighbour node
533 * @hard_iface: the interface this neighbour is connected to
534 * @neigh_addr: the interface address of the neighbour to retrieve
536 * Returns the hardif neighbour node if found or created or NULL otherwise.
538 static struct batadv_hardif_neigh_node
*
539 batadv_hardif_neigh_create(struct batadv_hard_iface
*hard_iface
,
540 const u8
*neigh_addr
)
542 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
543 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
545 spin_lock_bh(&hard_iface
->neigh_list_lock
);
547 /* check if neighbor hasn't been added in the meantime */
548 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
552 if (!atomic_inc_not_zero(&hard_iface
->refcount
))
555 hardif_neigh
= kzalloc(sizeof(*hardif_neigh
), GFP_ATOMIC
);
557 batadv_hardif_free_ref(hard_iface
);
561 INIT_HLIST_NODE(&hardif_neigh
->list
);
562 ether_addr_copy(hardif_neigh
->addr
, neigh_addr
);
563 hardif_neigh
->if_incoming
= hard_iface
;
564 hardif_neigh
->last_seen
= jiffies
;
566 atomic_set(&hardif_neigh
->refcount
, 1);
568 if (bat_priv
->bat_algo_ops
->bat_hardif_neigh_init
)
569 bat_priv
->bat_algo_ops
->bat_hardif_neigh_init(hardif_neigh
);
571 hlist_add_head(&hardif_neigh
->list
, &hard_iface
->neigh_list
);
574 spin_unlock_bh(&hard_iface
->neigh_list_lock
);
579 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
581 * @hard_iface: the interface this neighbour is connected to
582 * @neigh_addr: the interface address of the neighbour to retrieve
584 * Returns the hardif neighbour node if found or created or NULL otherwise.
586 static struct batadv_hardif_neigh_node
*
587 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface
*hard_iface
,
588 const u8
*neigh_addr
)
590 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
592 /* first check without locking to avoid the overhead */
593 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
597 return batadv_hardif_neigh_create(hard_iface
, neigh_addr
);
601 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
602 * @hard_iface: the interface where this neighbour is connected to
603 * @neigh_addr: the address of the neighbour
605 * Looks for and possibly returns a neighbour belonging to this hard interface.
606 * Returns NULL if the neighbour is not found.
608 struct batadv_hardif_neigh_node
*
609 batadv_hardif_neigh_get(const struct batadv_hard_iface
*hard_iface
,
610 const u8
*neigh_addr
)
612 struct batadv_hardif_neigh_node
*tmp_hardif_neigh
, *hardif_neigh
= NULL
;
615 hlist_for_each_entry_rcu(tmp_hardif_neigh
,
616 &hard_iface
->neigh_list
, list
) {
617 if (!batadv_compare_eth(tmp_hardif_neigh
->addr
, neigh_addr
))
620 if (!atomic_inc_not_zero(&tmp_hardif_neigh
->refcount
))
623 hardif_neigh
= tmp_hardif_neigh
;
632 * batadv_neigh_node_new - create and init a new neigh_node object
633 * @orig_node: originator object representing the neighbour
634 * @hard_iface: the interface where the neighbour is connected to
635 * @neigh_addr: the mac address of the neighbour interface
637 * Allocates a new neigh_node object and initialises all the generic fields.
638 * Returns the new object or NULL on failure.
640 struct batadv_neigh_node
*
641 batadv_neigh_node_new(struct batadv_orig_node
*orig_node
,
642 struct batadv_hard_iface
*hard_iface
,
643 const u8
*neigh_addr
)
645 struct batadv_neigh_node
*neigh_node
;
646 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
648 neigh_node
= batadv_neigh_node_get(orig_node
, hard_iface
, neigh_addr
);
652 hardif_neigh
= batadv_hardif_neigh_get_or_create(hard_iface
,
657 neigh_node
= kzalloc(sizeof(*neigh_node
), GFP_ATOMIC
);
661 if (!atomic_inc_not_zero(&hard_iface
->refcount
)) {
667 INIT_HLIST_NODE(&neigh_node
->list
);
668 INIT_HLIST_HEAD(&neigh_node
->ifinfo_list
);
669 spin_lock_init(&neigh_node
->ifinfo_lock
);
671 ether_addr_copy(neigh_node
->addr
, neigh_addr
);
672 neigh_node
->if_incoming
= hard_iface
;
673 neigh_node
->orig_node
= orig_node
;
675 /* extra reference for return */
676 atomic_set(&neigh_node
->refcount
, 2);
678 spin_lock_bh(&orig_node
->neigh_list_lock
);
679 hlist_add_head_rcu(&neigh_node
->list
, &orig_node
->neigh_list
);
680 spin_unlock_bh(&orig_node
->neigh_list_lock
);
682 /* increment unique neighbor refcount */
683 atomic_inc(&hardif_neigh
->refcount
);
685 batadv_dbg(BATADV_DBG_BATMAN
, orig_node
->bat_priv
,
686 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
687 neigh_addr
, orig_node
->orig
, hard_iface
->net_dev
->name
);
691 batadv_hardif_neigh_free_ref(hardif_neigh
);
696 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
697 * @seq: neighbour table seq_file struct
702 int batadv_hardif_neigh_seq_print_text(struct seq_file
*seq
, void *offset
)
704 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
705 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
706 struct batadv_hard_iface
*primary_if
;
708 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
712 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
713 BATADV_SOURCE_VERSION
, primary_if
->net_dev
->name
,
714 primary_if
->net_dev
->dev_addr
, net_dev
->name
,
715 bat_priv
->bat_algo_ops
->name
);
717 batadv_hardif_free_ref(primary_if
);
719 if (!bat_priv
->bat_algo_ops
->bat_neigh_print
) {
721 "No printing function for this routing protocol\n");
725 bat_priv
->bat_algo_ops
->bat_neigh_print(bat_priv
, seq
);
730 * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
731 * @rcu: rcu pointer of the orig_ifinfo object
733 static void batadv_orig_ifinfo_free_rcu(struct rcu_head
*rcu
)
735 struct batadv_orig_ifinfo
*orig_ifinfo
;
736 struct batadv_neigh_node
*router
;
738 orig_ifinfo
= container_of(rcu
, struct batadv_orig_ifinfo
, rcu
);
740 if (orig_ifinfo
->if_outgoing
!= BATADV_IF_DEFAULT
)
741 batadv_hardif_free_ref_now(orig_ifinfo
->if_outgoing
);
743 /* this is the last reference to this object */
744 router
= rcu_dereference_protected(orig_ifinfo
->router
, true);
746 batadv_neigh_node_free_ref_now(router
);
751 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
752 * the orig_ifinfo (without rcu callback)
753 * @orig_ifinfo: the orig_ifinfo object to release
756 batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo
*orig_ifinfo
)
758 if (atomic_dec_and_test(&orig_ifinfo
->refcount
))
759 batadv_orig_ifinfo_free_rcu(&orig_ifinfo
->rcu
);
763 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
765 * @orig_ifinfo: the orig_ifinfo object to release
767 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo
*orig_ifinfo
)
769 if (atomic_dec_and_test(&orig_ifinfo
->refcount
))
770 call_rcu(&orig_ifinfo
->rcu
, batadv_orig_ifinfo_free_rcu
);
773 static void batadv_orig_node_free_rcu(struct rcu_head
*rcu
)
775 struct hlist_node
*node_tmp
;
776 struct batadv_neigh_node
*neigh_node
;
777 struct batadv_orig_node
*orig_node
;
778 struct batadv_orig_ifinfo
*orig_ifinfo
;
780 orig_node
= container_of(rcu
, struct batadv_orig_node
, rcu
);
782 spin_lock_bh(&orig_node
->neigh_list_lock
);
784 /* for all neighbors towards this originator ... */
785 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
786 &orig_node
->neigh_list
, list
) {
787 hlist_del_rcu(&neigh_node
->list
);
788 batadv_neigh_node_free_ref_now(neigh_node
);
791 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
792 &orig_node
->ifinfo_list
, list
) {
793 hlist_del_rcu(&orig_ifinfo
->list
);
794 batadv_orig_ifinfo_free_ref_now(orig_ifinfo
);
796 spin_unlock_bh(&orig_node
->neigh_list_lock
);
798 batadv_mcast_purge_orig(orig_node
);
801 batadv_nc_purge_orig(orig_node
->bat_priv
, orig_node
, NULL
);
803 batadv_frag_purge_orig(orig_node
, NULL
);
805 if (orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free
)
806 orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free(orig_node
);
808 kfree(orig_node
->tt_buff
);
813 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
814 * schedule an rcu callback for freeing it
815 * @orig_node: the orig node to free
817 void batadv_orig_node_free_ref(struct batadv_orig_node
*orig_node
)
819 if (atomic_dec_and_test(&orig_node
->refcount
))
820 call_rcu(&orig_node
->rcu
, batadv_orig_node_free_rcu
);
824 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
825 * possibly free it (without rcu callback)
826 * @orig_node: the orig node to free
828 void batadv_orig_node_free_ref_now(struct batadv_orig_node
*orig_node
)
830 if (atomic_dec_and_test(&orig_node
->refcount
))
831 batadv_orig_node_free_rcu(&orig_node
->rcu
);
834 void batadv_originator_free(struct batadv_priv
*bat_priv
)
836 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
837 struct hlist_node
*node_tmp
;
838 struct hlist_head
*head
;
839 spinlock_t
*list_lock
; /* spinlock to protect write access */
840 struct batadv_orig_node
*orig_node
;
846 cancel_delayed_work_sync(&bat_priv
->orig_work
);
848 bat_priv
->orig_hash
= NULL
;
850 for (i
= 0; i
< hash
->size
; i
++) {
851 head
= &hash
->table
[i
];
852 list_lock
= &hash
->list_locks
[i
];
854 spin_lock_bh(list_lock
);
855 hlist_for_each_entry_safe(orig_node
, node_tmp
,
857 hlist_del_rcu(&orig_node
->hash_entry
);
858 batadv_orig_node_free_ref(orig_node
);
860 spin_unlock_bh(list_lock
);
863 batadv_hash_destroy(hash
);
867 * batadv_orig_node_new - creates a new orig_node
868 * @bat_priv: the bat priv with all the soft interface information
869 * @addr: the mac address of the originator
871 * Creates a new originator object and initialise all the generic fields.
872 * The new object is not added to the originator list.
873 * Returns the newly created object or NULL on failure.
875 struct batadv_orig_node
*batadv_orig_node_new(struct batadv_priv
*bat_priv
,
878 struct batadv_orig_node
*orig_node
;
879 struct batadv_orig_node_vlan
*vlan
;
880 unsigned long reset_time
;
883 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
884 "Creating new originator: %pM\n", addr
);
886 orig_node
= kzalloc(sizeof(*orig_node
), GFP_ATOMIC
);
890 INIT_HLIST_HEAD(&orig_node
->neigh_list
);
891 INIT_HLIST_HEAD(&orig_node
->vlan_list
);
892 INIT_HLIST_HEAD(&orig_node
->ifinfo_list
);
893 spin_lock_init(&orig_node
->bcast_seqno_lock
);
894 spin_lock_init(&orig_node
->neigh_list_lock
);
895 spin_lock_init(&orig_node
->tt_buff_lock
);
896 spin_lock_init(&orig_node
->tt_lock
);
897 spin_lock_init(&orig_node
->vlan_list_lock
);
899 batadv_nc_init_orig(orig_node
);
901 /* extra reference for return */
902 atomic_set(&orig_node
->refcount
, 2);
904 orig_node
->bat_priv
= bat_priv
;
905 ether_addr_copy(orig_node
->orig
, addr
);
906 batadv_dat_init_orig_node_addr(orig_node
);
907 atomic_set(&orig_node
->last_ttvn
, 0);
908 orig_node
->tt_buff
= NULL
;
909 orig_node
->tt_buff_len
= 0;
910 orig_node
->last_seen
= jiffies
;
911 reset_time
= jiffies
- 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
912 orig_node
->bcast_seqno_reset
= reset_time
;
914 #ifdef CONFIG_BATMAN_ADV_MCAST
915 orig_node
->mcast_flags
= BATADV_NO_FLAGS
;
916 INIT_HLIST_NODE(&orig_node
->mcast_want_all_unsnoopables_node
);
917 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv4_node
);
918 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv6_node
);
919 spin_lock_init(&orig_node
->mcast_handler_lock
);
922 /* create a vlan object for the "untagged" LAN */
923 vlan
= batadv_orig_node_vlan_new(orig_node
, BATADV_NO_FLAGS
);
926 /* batadv_orig_node_vlan_new() increases the refcounter.
927 * Immediately release vlan since it is not needed anymore in this
930 batadv_orig_node_vlan_free_ref(vlan
);
932 for (i
= 0; i
< BATADV_FRAG_BUFFER_COUNT
; i
++) {
933 INIT_HLIST_HEAD(&orig_node
->fragments
[i
].head
);
934 spin_lock_init(&orig_node
->fragments
[i
].lock
);
935 orig_node
->fragments
[i
].size
= 0;
945 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
946 * @bat_priv: the bat priv with all the soft interface information
947 * @neigh: orig node which is to be checked
950 batadv_purge_neigh_ifinfo(struct batadv_priv
*bat_priv
,
951 struct batadv_neigh_node
*neigh
)
953 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
954 struct batadv_hard_iface
*if_outgoing
;
955 struct hlist_node
*node_tmp
;
957 spin_lock_bh(&neigh
->ifinfo_lock
);
959 /* for all ifinfo objects for this neighinator */
960 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
961 &neigh
->ifinfo_list
, list
) {
962 if_outgoing
= neigh_ifinfo
->if_outgoing
;
964 /* always keep the default interface */
965 if (if_outgoing
== BATADV_IF_DEFAULT
)
968 /* don't purge if the interface is not (going) down */
969 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
970 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
971 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
974 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
975 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
976 neigh
->addr
, if_outgoing
->net_dev
->name
);
978 hlist_del_rcu(&neigh_ifinfo
->list
);
979 batadv_neigh_ifinfo_free_ref(neigh_ifinfo
);
982 spin_unlock_bh(&neigh
->ifinfo_lock
);
986 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
987 * @bat_priv: the bat priv with all the soft interface information
988 * @orig_node: orig node which is to be checked
990 * Returns true if any ifinfo entry was purged, false otherwise.
993 batadv_purge_orig_ifinfo(struct batadv_priv
*bat_priv
,
994 struct batadv_orig_node
*orig_node
)
996 struct batadv_orig_ifinfo
*orig_ifinfo
;
997 struct batadv_hard_iface
*if_outgoing
;
998 struct hlist_node
*node_tmp
;
999 bool ifinfo_purged
= false;
1001 spin_lock_bh(&orig_node
->neigh_list_lock
);
1003 /* for all ifinfo objects for this originator */
1004 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
1005 &orig_node
->ifinfo_list
, list
) {
1006 if_outgoing
= orig_ifinfo
->if_outgoing
;
1008 /* always keep the default interface */
1009 if (if_outgoing
== BATADV_IF_DEFAULT
)
1012 /* don't purge if the interface is not (going) down */
1013 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
1014 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
1015 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
1018 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1019 "router/ifinfo purge: originator %pM, iface: %s\n",
1020 orig_node
->orig
, if_outgoing
->net_dev
->name
);
1022 ifinfo_purged
= true;
1024 hlist_del_rcu(&orig_ifinfo
->list
);
1025 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
1026 if (orig_node
->last_bonding_candidate
== orig_ifinfo
) {
1027 orig_node
->last_bonding_candidate
= NULL
;
1028 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
1032 spin_unlock_bh(&orig_node
->neigh_list_lock
);
1034 return ifinfo_purged
;
1038 * batadv_purge_orig_neighbors - purges neighbors from originator
1039 * @bat_priv: the bat priv with all the soft interface information
1040 * @orig_node: orig node which is to be checked
1042 * Returns true if any neighbor was purged, false otherwise
1045 batadv_purge_orig_neighbors(struct batadv_priv
*bat_priv
,
1046 struct batadv_orig_node
*orig_node
)
1048 struct hlist_node
*node_tmp
;
1049 struct batadv_neigh_node
*neigh_node
;
1050 bool neigh_purged
= false;
1051 unsigned long last_seen
;
1052 struct batadv_hard_iface
*if_incoming
;
1054 spin_lock_bh(&orig_node
->neigh_list_lock
);
1056 /* for all neighbors towards this originator ... */
1057 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
1058 &orig_node
->neigh_list
, list
) {
1059 last_seen
= neigh_node
->last_seen
;
1060 if_incoming
= neigh_node
->if_incoming
;
1062 if ((batadv_has_timed_out(last_seen
, BATADV_PURGE_TIMEOUT
)) ||
1063 (if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
1064 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
1065 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
)) {
1066 if ((if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
1067 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
1068 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
))
1069 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1070 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1071 orig_node
->orig
, neigh_node
->addr
,
1072 if_incoming
->net_dev
->name
);
1074 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1075 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1076 orig_node
->orig
, neigh_node
->addr
,
1077 jiffies_to_msecs(last_seen
));
1079 neigh_purged
= true;
1081 hlist_del_rcu(&neigh_node
->list
);
1082 batadv_neigh_node_free_ref(neigh_node
);
1084 /* only necessary if not the whole neighbor is to be
1085 * deleted, but some interface has been removed.
1087 batadv_purge_neigh_ifinfo(bat_priv
, neigh_node
);
1091 spin_unlock_bh(&orig_node
->neigh_list_lock
);
1092 return neigh_purged
;
1096 * batadv_find_best_neighbor - finds the best neighbor after purging
1097 * @bat_priv: the bat priv with all the soft interface information
1098 * @orig_node: orig node which is to be checked
1099 * @if_outgoing: the interface for which the metric should be compared
1101 * Returns the current best neighbor, with refcount increased.
1103 static struct batadv_neigh_node
*
1104 batadv_find_best_neighbor(struct batadv_priv
*bat_priv
,
1105 struct batadv_orig_node
*orig_node
,
1106 struct batadv_hard_iface
*if_outgoing
)
1108 struct batadv_neigh_node
*best
= NULL
, *neigh
;
1109 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1112 hlist_for_each_entry_rcu(neigh
, &orig_node
->neigh_list
, list
) {
1113 if (best
&& (bao
->bat_neigh_cmp(neigh
, if_outgoing
,
1114 best
, if_outgoing
) <= 0))
1117 if (!atomic_inc_not_zero(&neigh
->refcount
))
1121 batadv_neigh_node_free_ref(best
);
1131 * batadv_purge_orig_node - purges obsolete information from an orig_node
1132 * @bat_priv: the bat priv with all the soft interface information
1133 * @orig_node: orig node which is to be checked
1135 * This function checks if the orig_node or substructures of it have become
1136 * obsolete, and purges this information if that's the case.
1138 * Returns true if the orig_node is to be removed, false otherwise.
1140 static bool batadv_purge_orig_node(struct batadv_priv
*bat_priv
,
1141 struct batadv_orig_node
*orig_node
)
1143 struct batadv_neigh_node
*best_neigh_node
;
1144 struct batadv_hard_iface
*hard_iface
;
1145 bool changed_ifinfo
, changed_neigh
;
1147 if (batadv_has_timed_out(orig_node
->last_seen
,
1148 2 * BATADV_PURGE_TIMEOUT
)) {
1149 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1150 "Originator timeout: originator %pM, last_seen %u\n",
1152 jiffies_to_msecs(orig_node
->last_seen
));
1155 changed_ifinfo
= batadv_purge_orig_ifinfo(bat_priv
, orig_node
);
1156 changed_neigh
= batadv_purge_orig_neighbors(bat_priv
, orig_node
);
1158 if (!changed_ifinfo
&& !changed_neigh
)
1161 /* first for NULL ... */
1162 best_neigh_node
= batadv_find_best_neighbor(bat_priv
, orig_node
,
1164 batadv_update_route(bat_priv
, orig_node
, BATADV_IF_DEFAULT
,
1166 if (best_neigh_node
)
1167 batadv_neigh_node_free_ref(best_neigh_node
);
1169 /* ... then for all other interfaces. */
1171 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1172 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1175 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
1178 best_neigh_node
= batadv_find_best_neighbor(bat_priv
,
1181 batadv_update_route(bat_priv
, orig_node
, hard_iface
,
1183 if (best_neigh_node
)
1184 batadv_neigh_node_free_ref(best_neigh_node
);
1191 static void _batadv_purge_orig(struct batadv_priv
*bat_priv
)
1193 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1194 struct hlist_node
*node_tmp
;
1195 struct hlist_head
*head
;
1196 spinlock_t
*list_lock
; /* spinlock to protect write access */
1197 struct batadv_orig_node
*orig_node
;
1203 /* for all origins... */
1204 for (i
= 0; i
< hash
->size
; i
++) {
1205 head
= &hash
->table
[i
];
1206 list_lock
= &hash
->list_locks
[i
];
1208 spin_lock_bh(list_lock
);
1209 hlist_for_each_entry_safe(orig_node
, node_tmp
,
1211 if (batadv_purge_orig_node(bat_priv
, orig_node
)) {
1212 batadv_gw_node_delete(bat_priv
, orig_node
);
1213 hlist_del_rcu(&orig_node
->hash_entry
);
1214 batadv_tt_global_del_orig(orig_node
->bat_priv
,
1216 "originator timed out");
1217 batadv_orig_node_free_ref(orig_node
);
1221 batadv_frag_purge_orig(orig_node
,
1222 batadv_frag_check_entry
);
1224 spin_unlock_bh(list_lock
);
1227 batadv_gw_election(bat_priv
);
1230 static void batadv_purge_orig(struct work_struct
*work
)
1232 struct delayed_work
*delayed_work
;
1233 struct batadv_priv
*bat_priv
;
1235 delayed_work
= container_of(work
, struct delayed_work
, work
);
1236 bat_priv
= container_of(delayed_work
, struct batadv_priv
, orig_work
);
1237 _batadv_purge_orig(bat_priv
);
1238 queue_delayed_work(batadv_event_workqueue
,
1239 &bat_priv
->orig_work
,
1240 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD
));
1243 void batadv_purge_orig_ref(struct batadv_priv
*bat_priv
)
1245 _batadv_purge_orig(bat_priv
);
1248 int batadv_orig_seq_print_text(struct seq_file
*seq
, void *offset
)
1250 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1251 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1252 struct batadv_hard_iface
*primary_if
;
1254 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1258 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1259 BATADV_SOURCE_VERSION
, primary_if
->net_dev
->name
,
1260 primary_if
->net_dev
->dev_addr
, net_dev
->name
,
1261 bat_priv
->bat_algo_ops
->name
);
1263 batadv_hardif_free_ref(primary_if
);
1265 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1267 "No printing function for this routing protocol\n");
1271 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
,
1278 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1279 * outgoing interface
1280 * @seq: debugfs table seq_file struct
1285 int batadv_orig_hardif_seq_print_text(struct seq_file
*seq
, void *offset
)
1287 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1288 struct batadv_hard_iface
*hard_iface
;
1289 struct batadv_priv
*bat_priv
;
1291 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
1293 if (!hard_iface
|| !hard_iface
->soft_iface
) {
1294 seq_puts(seq
, "Interface not known to B.A.T.M.A.N.\n");
1298 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1299 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1301 "No printing function for this routing protocol\n");
1305 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
) {
1306 seq_puts(seq
, "Interface not active\n");
1310 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1311 BATADV_SOURCE_VERSION
, hard_iface
->net_dev
->name
,
1312 hard_iface
->net_dev
->dev_addr
,
1313 hard_iface
->soft_iface
->name
, bat_priv
->bat_algo_ops
->name
);
1315 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
, hard_iface
);
1319 batadv_hardif_free_ref(hard_iface
);
1323 int batadv_orig_hash_add_if(struct batadv_hard_iface
*hard_iface
,
1326 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1327 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1328 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1329 struct hlist_head
*head
;
1330 struct batadv_orig_node
*orig_node
;
1334 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1337 for (i
= 0; i
< hash
->size
; i
++) {
1338 head
= &hash
->table
[i
];
1341 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1343 if (bao
->bat_orig_add_if
)
1344 ret
= bao
->bat_orig_add_if(orig_node
,
1359 int batadv_orig_hash_del_if(struct batadv_hard_iface
*hard_iface
,
1362 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1363 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1364 struct hlist_head
*head
;
1365 struct batadv_hard_iface
*hard_iface_tmp
;
1366 struct batadv_orig_node
*orig_node
;
1367 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1371 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1374 for (i
= 0; i
< hash
->size
; i
++) {
1375 head
= &hash
->table
[i
];
1378 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1380 if (bao
->bat_orig_del_if
)
1381 ret
= bao
->bat_orig_del_if(orig_node
,
1383 hard_iface
->if_num
);
1390 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1392 list_for_each_entry_rcu(hard_iface_tmp
, &batadv_hardif_list
, list
) {
1393 if (hard_iface_tmp
->if_status
== BATADV_IF_NOT_IN_USE
)
1396 if (hard_iface
== hard_iface_tmp
)
1399 if (hard_iface
->soft_iface
!= hard_iface_tmp
->soft_iface
)
1402 if (hard_iface_tmp
->if_num
> hard_iface
->if_num
)
1403 hard_iface_tmp
->if_num
--;
1407 hard_iface
->if_num
= -1;