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_release - release neigh_ifinfo from lists and queue for
167 * free after rcu grace period
168 * @neigh_ifinfo: the neigh_ifinfo object to release
171 batadv_neigh_ifinfo_release(struct batadv_neigh_ifinfo
*neigh_ifinfo
)
173 if (neigh_ifinfo
->if_outgoing
!= BATADV_IF_DEFAULT
)
174 batadv_hardif_free_ref(neigh_ifinfo
->if_outgoing
);
176 kfree_rcu(neigh_ifinfo
, rcu
);
180 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly release
182 * @neigh_ifinfo: the neigh_ifinfo object to release
184 void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo
*neigh_ifinfo
)
186 if (atomic_dec_and_test(&neigh_ifinfo
->refcount
))
187 batadv_neigh_ifinfo_release(neigh_ifinfo
);
191 * batadv_hardif_neigh_release - release hardif neigh node from lists and
192 * queue for free after rcu grace period
193 * @hardif_neigh: hardif neigh neighbor to free
196 batadv_hardif_neigh_release(struct batadv_hardif_neigh_node
*hardif_neigh
)
198 spin_lock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
199 hlist_del_init_rcu(&hardif_neigh
->list
);
200 spin_unlock_bh(&hardif_neigh
->if_incoming
->neigh_list_lock
);
202 batadv_hardif_free_ref(hardif_neigh
->if_incoming
);
203 kfree_rcu(hardif_neigh
, rcu
);
207 * batadv_hardif_neigh_free_ref - decrement the hardif neighbors refcounter
208 * and possibly release it
209 * @hardif_neigh: hardif neigh neighbor to free
211 void batadv_hardif_neigh_free_ref(struct batadv_hardif_neigh_node
*hardif_neigh
)
213 if (atomic_dec_and_test(&hardif_neigh
->refcount
))
214 batadv_hardif_neigh_release(hardif_neigh
);
218 * batadv_neigh_node_free_rcu - free the neigh_node
219 * @rcu: rcu pointer of the neigh_node
221 static void batadv_neigh_node_free_rcu(struct rcu_head
*rcu
)
223 struct hlist_node
*node_tmp
;
224 struct batadv_neigh_node
*neigh_node
;
225 struct batadv_hardif_neigh_node
*hardif_neigh
;
226 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
227 struct batadv_algo_ops
*bao
;
229 neigh_node
= container_of(rcu
, struct batadv_neigh_node
, rcu
);
230 bao
= neigh_node
->orig_node
->bat_priv
->bat_algo_ops
;
232 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
233 &neigh_node
->ifinfo_list
, list
) {
234 batadv_neigh_ifinfo_free_ref(neigh_ifinfo
);
237 hardif_neigh
= batadv_hardif_neigh_get(neigh_node
->if_incoming
,
240 /* batadv_hardif_neigh_get() increases refcount too */
241 batadv_hardif_neigh_free_ref(hardif_neigh
);
242 batadv_hardif_neigh_free_ref(hardif_neigh
);
245 if (bao
->bat_neigh_free
)
246 bao
->bat_neigh_free(neigh_node
);
248 batadv_hardif_free_ref_now(neigh_node
->if_incoming
);
254 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
255 * and possibly release it
256 * @neigh_node: neigh neighbor to free
258 void batadv_neigh_node_free_ref(struct batadv_neigh_node
*neigh_node
)
260 if (atomic_dec_and_test(&neigh_node
->refcount
))
261 call_rcu(&neigh_node
->rcu
, batadv_neigh_node_free_rcu
);
265 * batadv_orig_node_get_router - router to the originator depending on iface
266 * @orig_node: the orig node for the router
267 * @if_outgoing: the interface where the payload packet has been received or
268 * the OGM should be sent to
270 * Returns the neighbor which should be router for this orig_node/iface.
272 * The object is returned with refcounter increased by 1.
274 struct batadv_neigh_node
*
275 batadv_orig_router_get(struct batadv_orig_node
*orig_node
,
276 const struct batadv_hard_iface
*if_outgoing
)
278 struct batadv_orig_ifinfo
*orig_ifinfo
;
279 struct batadv_neigh_node
*router
= NULL
;
282 hlist_for_each_entry_rcu(orig_ifinfo
, &orig_node
->ifinfo_list
, list
) {
283 if (orig_ifinfo
->if_outgoing
!= if_outgoing
)
286 router
= rcu_dereference(orig_ifinfo
->router
);
290 if (router
&& !atomic_inc_not_zero(&router
->refcount
))
298 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
299 * @orig_node: the orig node to be queried
300 * @if_outgoing: the interface for which the ifinfo should be acquired
302 * Returns the requested orig_ifinfo or NULL if not found.
304 * The object is returned with refcounter increased by 1.
306 struct batadv_orig_ifinfo
*
307 batadv_orig_ifinfo_get(struct batadv_orig_node
*orig_node
,
308 struct batadv_hard_iface
*if_outgoing
)
310 struct batadv_orig_ifinfo
*tmp
, *orig_ifinfo
= NULL
;
313 hlist_for_each_entry_rcu(tmp
, &orig_node
->ifinfo_list
,
315 if (tmp
->if_outgoing
!= if_outgoing
)
318 if (!atomic_inc_not_zero(&tmp
->refcount
))
330 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
331 * @orig_node: the orig node to be queried
332 * @if_outgoing: the interface for which the ifinfo should be acquired
334 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
335 * interface otherwise. The object is created and added to the list
336 * if it does not exist.
338 * The object is returned with refcounter increased by 1.
340 struct batadv_orig_ifinfo
*
341 batadv_orig_ifinfo_new(struct batadv_orig_node
*orig_node
,
342 struct batadv_hard_iface
*if_outgoing
)
344 struct batadv_orig_ifinfo
*orig_ifinfo
= NULL
;
345 unsigned long reset_time
;
347 spin_lock_bh(&orig_node
->neigh_list_lock
);
349 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, if_outgoing
);
353 orig_ifinfo
= kzalloc(sizeof(*orig_ifinfo
), GFP_ATOMIC
);
357 if (if_outgoing
!= BATADV_IF_DEFAULT
&&
358 !atomic_inc_not_zero(&if_outgoing
->refcount
)) {
364 reset_time
= jiffies
- 1;
365 reset_time
-= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
366 orig_ifinfo
->batman_seqno_reset
= reset_time
;
367 orig_ifinfo
->if_outgoing
= if_outgoing
;
368 INIT_HLIST_NODE(&orig_ifinfo
->list
);
369 atomic_set(&orig_ifinfo
->refcount
, 2);
370 hlist_add_head_rcu(&orig_ifinfo
->list
,
371 &orig_node
->ifinfo_list
);
373 spin_unlock_bh(&orig_node
->neigh_list_lock
);
378 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
379 * @neigh_node: the neigh node to be queried
380 * @if_outgoing: the interface for which the ifinfo should be acquired
382 * The object is returned with refcounter increased by 1.
384 * Returns the requested neigh_ifinfo or NULL if not found
386 struct batadv_neigh_ifinfo
*
387 batadv_neigh_ifinfo_get(struct batadv_neigh_node
*neigh
,
388 struct batadv_hard_iface
*if_outgoing
)
390 struct batadv_neigh_ifinfo
*neigh_ifinfo
= NULL
,
394 hlist_for_each_entry_rcu(tmp_neigh_ifinfo
, &neigh
->ifinfo_list
,
396 if (tmp_neigh_ifinfo
->if_outgoing
!= if_outgoing
)
399 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo
->refcount
))
402 neigh_ifinfo
= tmp_neigh_ifinfo
;
411 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
412 * @neigh_node: the neigh node to be queried
413 * @if_outgoing: the interface for which the ifinfo should be acquired
415 * Returns NULL in case of failure or the neigh_ifinfo object for the
416 * if_outgoing interface otherwise. The object is created and added to the list
417 * if it does not exist.
419 * The object is returned with refcounter increased by 1.
421 struct batadv_neigh_ifinfo
*
422 batadv_neigh_ifinfo_new(struct batadv_neigh_node
*neigh
,
423 struct batadv_hard_iface
*if_outgoing
)
425 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
427 spin_lock_bh(&neigh
->ifinfo_lock
);
429 neigh_ifinfo
= batadv_neigh_ifinfo_get(neigh
, if_outgoing
);
433 neigh_ifinfo
= kzalloc(sizeof(*neigh_ifinfo
), GFP_ATOMIC
);
437 if (if_outgoing
&& !atomic_inc_not_zero(&if_outgoing
->refcount
)) {
443 INIT_HLIST_NODE(&neigh_ifinfo
->list
);
444 atomic_set(&neigh_ifinfo
->refcount
, 2);
445 neigh_ifinfo
->if_outgoing
= if_outgoing
;
447 hlist_add_head_rcu(&neigh_ifinfo
->list
, &neigh
->ifinfo_list
);
450 spin_unlock_bh(&neigh
->ifinfo_lock
);
456 * batadv_neigh_node_get - retrieve a neighbour from the list
457 * @orig_node: originator which the neighbour belongs to
458 * @hard_iface: the interface where this neighbour is connected to
459 * @addr: the address of the neighbour
461 * Looks for and possibly returns a neighbour belonging to this originator list
462 * which is connected through the provided hard interface.
463 * Returns NULL if the neighbour is not found.
465 static struct batadv_neigh_node
*
466 batadv_neigh_node_get(const struct batadv_orig_node
*orig_node
,
467 const struct batadv_hard_iface
*hard_iface
,
470 struct batadv_neigh_node
*tmp_neigh_node
, *res
= NULL
;
473 hlist_for_each_entry_rcu(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
474 if (!batadv_compare_eth(tmp_neigh_node
->addr
, addr
))
477 if (tmp_neigh_node
->if_incoming
!= hard_iface
)
480 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
483 res
= tmp_neigh_node
;
492 * batadv_hardif_neigh_create - create a hardif neighbour node
493 * @hard_iface: the interface this neighbour is connected to
494 * @neigh_addr: the interface address of the neighbour to retrieve
496 * Returns the hardif neighbour node if found or created or NULL otherwise.
498 static struct batadv_hardif_neigh_node
*
499 batadv_hardif_neigh_create(struct batadv_hard_iface
*hard_iface
,
500 const u8
*neigh_addr
)
502 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
503 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
505 spin_lock_bh(&hard_iface
->neigh_list_lock
);
507 /* check if neighbor hasn't been added in the meantime */
508 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
512 if (!atomic_inc_not_zero(&hard_iface
->refcount
))
515 hardif_neigh
= kzalloc(sizeof(*hardif_neigh
), GFP_ATOMIC
);
517 batadv_hardif_free_ref(hard_iface
);
521 INIT_HLIST_NODE(&hardif_neigh
->list
);
522 ether_addr_copy(hardif_neigh
->addr
, neigh_addr
);
523 hardif_neigh
->if_incoming
= hard_iface
;
524 hardif_neigh
->last_seen
= jiffies
;
526 atomic_set(&hardif_neigh
->refcount
, 1);
528 if (bat_priv
->bat_algo_ops
->bat_hardif_neigh_init
)
529 bat_priv
->bat_algo_ops
->bat_hardif_neigh_init(hardif_neigh
);
531 hlist_add_head(&hardif_neigh
->list
, &hard_iface
->neigh_list
);
534 spin_unlock_bh(&hard_iface
->neigh_list_lock
);
539 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
541 * @hard_iface: the interface this neighbour is connected to
542 * @neigh_addr: the interface address of the neighbour to retrieve
544 * Returns the hardif neighbour node if found or created or NULL otherwise.
546 static struct batadv_hardif_neigh_node
*
547 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface
*hard_iface
,
548 const u8
*neigh_addr
)
550 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
552 /* first check without locking to avoid the overhead */
553 hardif_neigh
= batadv_hardif_neigh_get(hard_iface
, neigh_addr
);
557 return batadv_hardif_neigh_create(hard_iface
, neigh_addr
);
561 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
562 * @hard_iface: the interface where this neighbour is connected to
563 * @neigh_addr: the address of the neighbour
565 * Looks for and possibly returns a neighbour belonging to this hard interface.
566 * Returns NULL if the neighbour is not found.
568 struct batadv_hardif_neigh_node
*
569 batadv_hardif_neigh_get(const struct batadv_hard_iface
*hard_iface
,
570 const u8
*neigh_addr
)
572 struct batadv_hardif_neigh_node
*tmp_hardif_neigh
, *hardif_neigh
= NULL
;
575 hlist_for_each_entry_rcu(tmp_hardif_neigh
,
576 &hard_iface
->neigh_list
, list
) {
577 if (!batadv_compare_eth(tmp_hardif_neigh
->addr
, neigh_addr
))
580 if (!atomic_inc_not_zero(&tmp_hardif_neigh
->refcount
))
583 hardif_neigh
= tmp_hardif_neigh
;
592 * batadv_neigh_node_new - create and init a new neigh_node object
593 * @orig_node: originator object representing the neighbour
594 * @hard_iface: the interface where the neighbour is connected to
595 * @neigh_addr: the mac address of the neighbour interface
597 * Allocates a new neigh_node object and initialises all the generic fields.
598 * Returns the new object or NULL on failure.
600 struct batadv_neigh_node
*
601 batadv_neigh_node_new(struct batadv_orig_node
*orig_node
,
602 struct batadv_hard_iface
*hard_iface
,
603 const u8
*neigh_addr
)
605 struct batadv_neigh_node
*neigh_node
;
606 struct batadv_hardif_neigh_node
*hardif_neigh
= NULL
;
608 neigh_node
= batadv_neigh_node_get(orig_node
, hard_iface
, neigh_addr
);
612 hardif_neigh
= batadv_hardif_neigh_get_or_create(hard_iface
,
617 neigh_node
= kzalloc(sizeof(*neigh_node
), GFP_ATOMIC
);
621 if (!atomic_inc_not_zero(&hard_iface
->refcount
)) {
627 INIT_HLIST_NODE(&neigh_node
->list
);
628 INIT_HLIST_HEAD(&neigh_node
->ifinfo_list
);
629 spin_lock_init(&neigh_node
->ifinfo_lock
);
631 ether_addr_copy(neigh_node
->addr
, neigh_addr
);
632 neigh_node
->if_incoming
= hard_iface
;
633 neigh_node
->orig_node
= orig_node
;
635 /* extra reference for return */
636 atomic_set(&neigh_node
->refcount
, 2);
638 spin_lock_bh(&orig_node
->neigh_list_lock
);
639 hlist_add_head_rcu(&neigh_node
->list
, &orig_node
->neigh_list
);
640 spin_unlock_bh(&orig_node
->neigh_list_lock
);
642 /* increment unique neighbor refcount */
643 atomic_inc(&hardif_neigh
->refcount
);
645 batadv_dbg(BATADV_DBG_BATMAN
, orig_node
->bat_priv
,
646 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
647 neigh_addr
, orig_node
->orig
, hard_iface
->net_dev
->name
);
651 batadv_hardif_neigh_free_ref(hardif_neigh
);
656 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
657 * @seq: neighbour table seq_file struct
662 int batadv_hardif_neigh_seq_print_text(struct seq_file
*seq
, void *offset
)
664 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
665 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
666 struct batadv_hard_iface
*primary_if
;
668 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
672 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
673 BATADV_SOURCE_VERSION
, primary_if
->net_dev
->name
,
674 primary_if
->net_dev
->dev_addr
, net_dev
->name
,
675 bat_priv
->bat_algo_ops
->name
);
677 batadv_hardif_free_ref(primary_if
);
679 if (!bat_priv
->bat_algo_ops
->bat_neigh_print
) {
681 "No printing function for this routing protocol\n");
685 bat_priv
->bat_algo_ops
->bat_neigh_print(bat_priv
, seq
);
690 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
691 * free after rcu grace period
692 * @orig_ifinfo: the orig_ifinfo object to release
694 static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo
*orig_ifinfo
)
696 struct batadv_neigh_node
*router
;
698 if (orig_ifinfo
->if_outgoing
!= BATADV_IF_DEFAULT
)
699 batadv_hardif_free_ref(orig_ifinfo
->if_outgoing
);
701 /* this is the last reference to this object */
702 router
= rcu_dereference_protected(orig_ifinfo
->router
, true);
704 batadv_neigh_node_free_ref(router
);
706 kfree_rcu(orig_ifinfo
, rcu
);
710 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
712 * @orig_ifinfo: the orig_ifinfo object to release
714 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo
*orig_ifinfo
)
716 if (atomic_dec_and_test(&orig_ifinfo
->refcount
))
717 batadv_orig_ifinfo_release(orig_ifinfo
);
721 * batadv_orig_node_free_rcu - free the orig_node
722 * @rcu: rcu pointer of the orig_node
724 static void batadv_orig_node_free_rcu(struct rcu_head
*rcu
)
726 struct batadv_orig_node
*orig_node
;
728 orig_node
= container_of(rcu
, struct batadv_orig_node
, rcu
);
730 batadv_mcast_purge_orig(orig_node
);
732 batadv_frag_purge_orig(orig_node
, NULL
);
734 if (orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free
)
735 orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free(orig_node
);
737 kfree(orig_node
->tt_buff
);
742 * batadv_orig_node_release - release orig_node from lists and queue for
743 * free after rcu grace period
744 * @orig_node: the orig node to free
746 static void batadv_orig_node_release(struct batadv_orig_node
*orig_node
)
748 struct hlist_node
*node_tmp
;
749 struct batadv_neigh_node
*neigh_node
;
750 struct batadv_orig_ifinfo
*orig_ifinfo
;
752 spin_lock_bh(&orig_node
->neigh_list_lock
);
754 /* for all neighbors towards this originator ... */
755 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
756 &orig_node
->neigh_list
, list
) {
757 hlist_del_rcu(&neigh_node
->list
);
758 batadv_neigh_node_free_ref(neigh_node
);
761 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
762 &orig_node
->ifinfo_list
, list
) {
763 hlist_del_rcu(&orig_ifinfo
->list
);
764 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
766 spin_unlock_bh(&orig_node
->neigh_list_lock
);
769 batadv_nc_purge_orig(orig_node
->bat_priv
, orig_node
, NULL
);
771 call_rcu(&orig_node
->rcu
, batadv_orig_node_free_rcu
);
775 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
777 * @orig_node: the orig node to free
779 void batadv_orig_node_free_ref(struct batadv_orig_node
*orig_node
)
781 if (atomic_dec_and_test(&orig_node
->refcount
))
782 batadv_orig_node_release(orig_node
);
786 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
787 * possibly free it (without rcu callback)
788 * @orig_node: the orig node to free
790 void batadv_orig_node_free_ref_now(struct batadv_orig_node
*orig_node
)
792 if (atomic_dec_and_test(&orig_node
->refcount
))
793 batadv_orig_node_free_rcu(&orig_node
->rcu
);
796 void batadv_originator_free(struct batadv_priv
*bat_priv
)
798 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
799 struct hlist_node
*node_tmp
;
800 struct hlist_head
*head
;
801 spinlock_t
*list_lock
; /* spinlock to protect write access */
802 struct batadv_orig_node
*orig_node
;
808 cancel_delayed_work_sync(&bat_priv
->orig_work
);
810 bat_priv
->orig_hash
= NULL
;
812 for (i
= 0; i
< hash
->size
; i
++) {
813 head
= &hash
->table
[i
];
814 list_lock
= &hash
->list_locks
[i
];
816 spin_lock_bh(list_lock
);
817 hlist_for_each_entry_safe(orig_node
, node_tmp
,
819 hlist_del_rcu(&orig_node
->hash_entry
);
820 batadv_orig_node_free_ref(orig_node
);
822 spin_unlock_bh(list_lock
);
825 batadv_hash_destroy(hash
);
829 * batadv_orig_node_new - creates a new orig_node
830 * @bat_priv: the bat priv with all the soft interface information
831 * @addr: the mac address of the originator
833 * Creates a new originator object and initialise all the generic fields.
834 * The new object is not added to the originator list.
835 * Returns the newly created object or NULL on failure.
837 struct batadv_orig_node
*batadv_orig_node_new(struct batadv_priv
*bat_priv
,
840 struct batadv_orig_node
*orig_node
;
841 struct batadv_orig_node_vlan
*vlan
;
842 unsigned long reset_time
;
845 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
846 "Creating new originator: %pM\n", addr
);
848 orig_node
= kzalloc(sizeof(*orig_node
), GFP_ATOMIC
);
852 INIT_HLIST_HEAD(&orig_node
->neigh_list
);
853 INIT_HLIST_HEAD(&orig_node
->vlan_list
);
854 INIT_HLIST_HEAD(&orig_node
->ifinfo_list
);
855 spin_lock_init(&orig_node
->bcast_seqno_lock
);
856 spin_lock_init(&orig_node
->neigh_list_lock
);
857 spin_lock_init(&orig_node
->tt_buff_lock
);
858 spin_lock_init(&orig_node
->tt_lock
);
859 spin_lock_init(&orig_node
->vlan_list_lock
);
861 batadv_nc_init_orig(orig_node
);
863 /* extra reference for return */
864 atomic_set(&orig_node
->refcount
, 2);
866 orig_node
->bat_priv
= bat_priv
;
867 ether_addr_copy(orig_node
->orig
, addr
);
868 batadv_dat_init_orig_node_addr(orig_node
);
869 atomic_set(&orig_node
->last_ttvn
, 0);
870 orig_node
->tt_buff
= NULL
;
871 orig_node
->tt_buff_len
= 0;
872 orig_node
->last_seen
= jiffies
;
873 reset_time
= jiffies
- 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
874 orig_node
->bcast_seqno_reset
= reset_time
;
876 #ifdef CONFIG_BATMAN_ADV_MCAST
877 orig_node
->mcast_flags
= BATADV_NO_FLAGS
;
878 INIT_HLIST_NODE(&orig_node
->mcast_want_all_unsnoopables_node
);
879 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv4_node
);
880 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv6_node
);
881 spin_lock_init(&orig_node
->mcast_handler_lock
);
884 /* create a vlan object for the "untagged" LAN */
885 vlan
= batadv_orig_node_vlan_new(orig_node
, BATADV_NO_FLAGS
);
888 /* batadv_orig_node_vlan_new() increases the refcounter.
889 * Immediately release vlan since it is not needed anymore in this
892 batadv_orig_node_vlan_free_ref(vlan
);
894 for (i
= 0; i
< BATADV_FRAG_BUFFER_COUNT
; i
++) {
895 INIT_HLIST_HEAD(&orig_node
->fragments
[i
].head
);
896 spin_lock_init(&orig_node
->fragments
[i
].lock
);
897 orig_node
->fragments
[i
].size
= 0;
907 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
908 * @bat_priv: the bat priv with all the soft interface information
909 * @neigh: orig node which is to be checked
912 batadv_purge_neigh_ifinfo(struct batadv_priv
*bat_priv
,
913 struct batadv_neigh_node
*neigh
)
915 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
916 struct batadv_hard_iface
*if_outgoing
;
917 struct hlist_node
*node_tmp
;
919 spin_lock_bh(&neigh
->ifinfo_lock
);
921 /* for all ifinfo objects for this neighinator */
922 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
923 &neigh
->ifinfo_list
, list
) {
924 if_outgoing
= neigh_ifinfo
->if_outgoing
;
926 /* always keep the default interface */
927 if (if_outgoing
== BATADV_IF_DEFAULT
)
930 /* don't purge if the interface is not (going) down */
931 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
932 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
933 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
936 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
937 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
938 neigh
->addr
, if_outgoing
->net_dev
->name
);
940 hlist_del_rcu(&neigh_ifinfo
->list
);
941 batadv_neigh_ifinfo_free_ref(neigh_ifinfo
);
944 spin_unlock_bh(&neigh
->ifinfo_lock
);
948 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
949 * @bat_priv: the bat priv with all the soft interface information
950 * @orig_node: orig node which is to be checked
952 * Returns true if any ifinfo entry was purged, false otherwise.
955 batadv_purge_orig_ifinfo(struct batadv_priv
*bat_priv
,
956 struct batadv_orig_node
*orig_node
)
958 struct batadv_orig_ifinfo
*orig_ifinfo
;
959 struct batadv_hard_iface
*if_outgoing
;
960 struct hlist_node
*node_tmp
;
961 bool ifinfo_purged
= false;
963 spin_lock_bh(&orig_node
->neigh_list_lock
);
965 /* for all ifinfo objects for this originator */
966 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
967 &orig_node
->ifinfo_list
, list
) {
968 if_outgoing
= orig_ifinfo
->if_outgoing
;
970 /* always keep the default interface */
971 if (if_outgoing
== BATADV_IF_DEFAULT
)
974 /* don't purge if the interface is not (going) down */
975 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
976 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
977 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
980 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
981 "router/ifinfo purge: originator %pM, iface: %s\n",
982 orig_node
->orig
, if_outgoing
->net_dev
->name
);
984 ifinfo_purged
= true;
986 hlist_del_rcu(&orig_ifinfo
->list
);
987 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
988 if (orig_node
->last_bonding_candidate
== orig_ifinfo
) {
989 orig_node
->last_bonding_candidate
= NULL
;
990 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
994 spin_unlock_bh(&orig_node
->neigh_list_lock
);
996 return ifinfo_purged
;
1000 * batadv_purge_orig_neighbors - purges neighbors from originator
1001 * @bat_priv: the bat priv with all the soft interface information
1002 * @orig_node: orig node which is to be checked
1004 * Returns true if any neighbor was purged, false otherwise
1007 batadv_purge_orig_neighbors(struct batadv_priv
*bat_priv
,
1008 struct batadv_orig_node
*orig_node
)
1010 struct hlist_node
*node_tmp
;
1011 struct batadv_neigh_node
*neigh_node
;
1012 bool neigh_purged
= false;
1013 unsigned long last_seen
;
1014 struct batadv_hard_iface
*if_incoming
;
1016 spin_lock_bh(&orig_node
->neigh_list_lock
);
1018 /* for all neighbors towards this originator ... */
1019 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
1020 &orig_node
->neigh_list
, list
) {
1021 last_seen
= neigh_node
->last_seen
;
1022 if_incoming
= neigh_node
->if_incoming
;
1024 if ((batadv_has_timed_out(last_seen
, BATADV_PURGE_TIMEOUT
)) ||
1025 (if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
1026 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
1027 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
)) {
1028 if ((if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
1029 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
1030 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
))
1031 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1032 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1033 orig_node
->orig
, neigh_node
->addr
,
1034 if_incoming
->net_dev
->name
);
1036 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1037 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1038 orig_node
->orig
, neigh_node
->addr
,
1039 jiffies_to_msecs(last_seen
));
1041 neigh_purged
= true;
1043 hlist_del_rcu(&neigh_node
->list
);
1044 batadv_neigh_node_free_ref(neigh_node
);
1046 /* only necessary if not the whole neighbor is to be
1047 * deleted, but some interface has been removed.
1049 batadv_purge_neigh_ifinfo(bat_priv
, neigh_node
);
1053 spin_unlock_bh(&orig_node
->neigh_list_lock
);
1054 return neigh_purged
;
1058 * batadv_find_best_neighbor - finds the best neighbor after purging
1059 * @bat_priv: the bat priv with all the soft interface information
1060 * @orig_node: orig node which is to be checked
1061 * @if_outgoing: the interface for which the metric should be compared
1063 * Returns the current best neighbor, with refcount increased.
1065 static struct batadv_neigh_node
*
1066 batadv_find_best_neighbor(struct batadv_priv
*bat_priv
,
1067 struct batadv_orig_node
*orig_node
,
1068 struct batadv_hard_iface
*if_outgoing
)
1070 struct batadv_neigh_node
*best
= NULL
, *neigh
;
1071 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1074 hlist_for_each_entry_rcu(neigh
, &orig_node
->neigh_list
, list
) {
1075 if (best
&& (bao
->bat_neigh_cmp(neigh
, if_outgoing
,
1076 best
, if_outgoing
) <= 0))
1079 if (!atomic_inc_not_zero(&neigh
->refcount
))
1083 batadv_neigh_node_free_ref(best
);
1093 * batadv_purge_orig_node - purges obsolete information from an orig_node
1094 * @bat_priv: the bat priv with all the soft interface information
1095 * @orig_node: orig node which is to be checked
1097 * This function checks if the orig_node or substructures of it have become
1098 * obsolete, and purges this information if that's the case.
1100 * Returns true if the orig_node is to be removed, false otherwise.
1102 static bool batadv_purge_orig_node(struct batadv_priv
*bat_priv
,
1103 struct batadv_orig_node
*orig_node
)
1105 struct batadv_neigh_node
*best_neigh_node
;
1106 struct batadv_hard_iface
*hard_iface
;
1107 bool changed_ifinfo
, changed_neigh
;
1109 if (batadv_has_timed_out(orig_node
->last_seen
,
1110 2 * BATADV_PURGE_TIMEOUT
)) {
1111 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
1112 "Originator timeout: originator %pM, last_seen %u\n",
1114 jiffies_to_msecs(orig_node
->last_seen
));
1117 changed_ifinfo
= batadv_purge_orig_ifinfo(bat_priv
, orig_node
);
1118 changed_neigh
= batadv_purge_orig_neighbors(bat_priv
, orig_node
);
1120 if (!changed_ifinfo
&& !changed_neigh
)
1123 /* first for NULL ... */
1124 best_neigh_node
= batadv_find_best_neighbor(bat_priv
, orig_node
,
1126 batadv_update_route(bat_priv
, orig_node
, BATADV_IF_DEFAULT
,
1128 if (best_neigh_node
)
1129 batadv_neigh_node_free_ref(best_neigh_node
);
1131 /* ... then for all other interfaces. */
1133 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
1134 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
1137 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
1140 best_neigh_node
= batadv_find_best_neighbor(bat_priv
,
1143 batadv_update_route(bat_priv
, orig_node
, hard_iface
,
1145 if (best_neigh_node
)
1146 batadv_neigh_node_free_ref(best_neigh_node
);
1153 static void _batadv_purge_orig(struct batadv_priv
*bat_priv
)
1155 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1156 struct hlist_node
*node_tmp
;
1157 struct hlist_head
*head
;
1158 spinlock_t
*list_lock
; /* spinlock to protect write access */
1159 struct batadv_orig_node
*orig_node
;
1165 /* for all origins... */
1166 for (i
= 0; i
< hash
->size
; i
++) {
1167 head
= &hash
->table
[i
];
1168 list_lock
= &hash
->list_locks
[i
];
1170 spin_lock_bh(list_lock
);
1171 hlist_for_each_entry_safe(orig_node
, node_tmp
,
1173 if (batadv_purge_orig_node(bat_priv
, orig_node
)) {
1174 batadv_gw_node_delete(bat_priv
, orig_node
);
1175 hlist_del_rcu(&orig_node
->hash_entry
);
1176 batadv_tt_global_del_orig(orig_node
->bat_priv
,
1178 "originator timed out");
1179 batadv_orig_node_free_ref(orig_node
);
1183 batadv_frag_purge_orig(orig_node
,
1184 batadv_frag_check_entry
);
1186 spin_unlock_bh(list_lock
);
1189 batadv_gw_election(bat_priv
);
1192 static void batadv_purge_orig(struct work_struct
*work
)
1194 struct delayed_work
*delayed_work
;
1195 struct batadv_priv
*bat_priv
;
1197 delayed_work
= container_of(work
, struct delayed_work
, work
);
1198 bat_priv
= container_of(delayed_work
, struct batadv_priv
, orig_work
);
1199 _batadv_purge_orig(bat_priv
);
1200 queue_delayed_work(batadv_event_workqueue
,
1201 &bat_priv
->orig_work
,
1202 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD
));
1205 void batadv_purge_orig_ref(struct batadv_priv
*bat_priv
)
1207 _batadv_purge_orig(bat_priv
);
1210 int batadv_orig_seq_print_text(struct seq_file
*seq
, void *offset
)
1212 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1213 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1214 struct batadv_hard_iface
*primary_if
;
1216 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1220 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1221 BATADV_SOURCE_VERSION
, primary_if
->net_dev
->name
,
1222 primary_if
->net_dev
->dev_addr
, net_dev
->name
,
1223 bat_priv
->bat_algo_ops
->name
);
1225 batadv_hardif_free_ref(primary_if
);
1227 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1229 "No printing function for this routing protocol\n");
1233 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
,
1240 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1241 * outgoing interface
1242 * @seq: debugfs table seq_file struct
1247 int batadv_orig_hardif_seq_print_text(struct seq_file
*seq
, void *offset
)
1249 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1250 struct batadv_hard_iface
*hard_iface
;
1251 struct batadv_priv
*bat_priv
;
1253 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
1255 if (!hard_iface
|| !hard_iface
->soft_iface
) {
1256 seq_puts(seq
, "Interface not known to B.A.T.M.A.N.\n");
1260 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1261 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1263 "No printing function for this routing protocol\n");
1267 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
) {
1268 seq_puts(seq
, "Interface not active\n");
1272 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1273 BATADV_SOURCE_VERSION
, hard_iface
->net_dev
->name
,
1274 hard_iface
->net_dev
->dev_addr
,
1275 hard_iface
->soft_iface
->name
, bat_priv
->bat_algo_ops
->name
);
1277 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
, hard_iface
);
1281 batadv_hardif_free_ref(hard_iface
);
1285 int batadv_orig_hash_add_if(struct batadv_hard_iface
*hard_iface
,
1288 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1289 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1290 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1291 struct hlist_head
*head
;
1292 struct batadv_orig_node
*orig_node
;
1296 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1299 for (i
= 0; i
< hash
->size
; i
++) {
1300 head
= &hash
->table
[i
];
1303 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1305 if (bao
->bat_orig_add_if
)
1306 ret
= bao
->bat_orig_add_if(orig_node
,
1321 int batadv_orig_hash_del_if(struct batadv_hard_iface
*hard_iface
,
1324 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1325 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1326 struct hlist_head
*head
;
1327 struct batadv_hard_iface
*hard_iface_tmp
;
1328 struct batadv_orig_node
*orig_node
;
1329 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1333 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1336 for (i
= 0; i
< hash
->size
; i
++) {
1337 head
= &hash
->table
[i
];
1340 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1342 if (bao
->bat_orig_del_if
)
1343 ret
= bao
->bat_orig_del_if(orig_node
,
1345 hard_iface
->if_num
);
1352 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1354 list_for_each_entry_rcu(hard_iface_tmp
, &batadv_hardif_list
, list
) {
1355 if (hard_iface_tmp
->if_status
== BATADV_IF_NOT_IN_USE
)
1358 if (hard_iface
== hard_iface_tmp
)
1361 if (hard_iface
->soft_iface
!= hard_iface_tmp
->soft_iface
)
1364 if (hard_iface_tmp
->if_num
> hard_iface
->if_num
)
1365 hard_iface_tmp
->if_num
--;
1369 hard_iface
->if_num
= -1;