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_neigh_node_free_rcu - free the neigh_node
192 * batadv_neigh_node_release - release neigh_node from lists and queue for
193 * free after rcu grace period
194 * @neigh_node: neigh neighbor to free
196 static void batadv_neigh_node_release(struct batadv_neigh_node
*neigh_node
)
198 struct hlist_node
*node_tmp
;
199 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
201 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
202 &neigh_node
->ifinfo_list
, list
) {
203 batadv_neigh_ifinfo_free_ref(neigh_ifinfo
);
206 batadv_hardif_free_ref(neigh_node
->if_incoming
);
208 kfree_rcu(neigh_node
, rcu
);
212 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
213 * and possibly release it
214 * @neigh_node: neigh neighbor to free
216 void batadv_neigh_node_free_ref(struct batadv_neigh_node
*neigh_node
)
218 if (atomic_dec_and_test(&neigh_node
->refcount
))
219 batadv_neigh_node_release(neigh_node
);
223 * batadv_orig_node_get_router - router to the originator depending on iface
224 * @orig_node: the orig node for the router
225 * @if_outgoing: the interface where the payload packet has been received or
226 * the OGM should be sent to
228 * Returns the neighbor which should be router for this orig_node/iface.
230 * The object is returned with refcounter increased by 1.
232 struct batadv_neigh_node
*
233 batadv_orig_router_get(struct batadv_orig_node
*orig_node
,
234 const struct batadv_hard_iface
*if_outgoing
)
236 struct batadv_orig_ifinfo
*orig_ifinfo
;
237 struct batadv_neigh_node
*router
= NULL
;
240 hlist_for_each_entry_rcu(orig_ifinfo
, &orig_node
->ifinfo_list
, list
) {
241 if (orig_ifinfo
->if_outgoing
!= if_outgoing
)
244 router
= rcu_dereference(orig_ifinfo
->router
);
248 if (router
&& !atomic_inc_not_zero(&router
->refcount
))
256 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
257 * @orig_node: the orig node to be queried
258 * @if_outgoing: the interface for which the ifinfo should be acquired
260 * Returns the requested orig_ifinfo or NULL if not found.
262 * The object is returned with refcounter increased by 1.
264 struct batadv_orig_ifinfo
*
265 batadv_orig_ifinfo_get(struct batadv_orig_node
*orig_node
,
266 struct batadv_hard_iface
*if_outgoing
)
268 struct batadv_orig_ifinfo
*tmp
, *orig_ifinfo
= NULL
;
271 hlist_for_each_entry_rcu(tmp
, &orig_node
->ifinfo_list
,
273 if (tmp
->if_outgoing
!= if_outgoing
)
276 if (!atomic_inc_not_zero(&tmp
->refcount
))
288 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
289 * @orig_node: the orig node to be queried
290 * @if_outgoing: the interface for which the ifinfo should be acquired
292 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
293 * interface otherwise. The object is created and added to the list
294 * if it does not exist.
296 * The object is returned with refcounter increased by 1.
298 struct batadv_orig_ifinfo
*
299 batadv_orig_ifinfo_new(struct batadv_orig_node
*orig_node
,
300 struct batadv_hard_iface
*if_outgoing
)
302 struct batadv_orig_ifinfo
*orig_ifinfo
= NULL
;
303 unsigned long reset_time
;
305 spin_lock_bh(&orig_node
->neigh_list_lock
);
307 orig_ifinfo
= batadv_orig_ifinfo_get(orig_node
, if_outgoing
);
311 orig_ifinfo
= kzalloc(sizeof(*orig_ifinfo
), GFP_ATOMIC
);
315 if (if_outgoing
!= BATADV_IF_DEFAULT
&&
316 !atomic_inc_not_zero(&if_outgoing
->refcount
)) {
322 reset_time
= jiffies
- 1;
323 reset_time
-= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
324 orig_ifinfo
->batman_seqno_reset
= reset_time
;
325 orig_ifinfo
->if_outgoing
= if_outgoing
;
326 INIT_HLIST_NODE(&orig_ifinfo
->list
);
327 atomic_set(&orig_ifinfo
->refcount
, 2);
328 hlist_add_head_rcu(&orig_ifinfo
->list
,
329 &orig_node
->ifinfo_list
);
331 spin_unlock_bh(&orig_node
->neigh_list_lock
);
336 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
337 * @neigh_node: the neigh node to be queried
338 * @if_outgoing: the interface for which the ifinfo should be acquired
340 * The object is returned with refcounter increased by 1.
342 * Returns the requested neigh_ifinfo or NULL if not found
344 struct batadv_neigh_ifinfo
*
345 batadv_neigh_ifinfo_get(struct batadv_neigh_node
*neigh
,
346 struct batadv_hard_iface
*if_outgoing
)
348 struct batadv_neigh_ifinfo
*neigh_ifinfo
= NULL
,
352 hlist_for_each_entry_rcu(tmp_neigh_ifinfo
, &neigh
->ifinfo_list
,
354 if (tmp_neigh_ifinfo
->if_outgoing
!= if_outgoing
)
357 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo
->refcount
))
360 neigh_ifinfo
= tmp_neigh_ifinfo
;
369 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
370 * @neigh_node: the neigh node to be queried
371 * @if_outgoing: the interface for which the ifinfo should be acquired
373 * Returns NULL in case of failure or the neigh_ifinfo object for the
374 * if_outgoing interface otherwise. The object is created and added to the list
375 * if it does not exist.
377 * The object is returned with refcounter increased by 1.
379 struct batadv_neigh_ifinfo
*
380 batadv_neigh_ifinfo_new(struct batadv_neigh_node
*neigh
,
381 struct batadv_hard_iface
*if_outgoing
)
383 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
385 spin_lock_bh(&neigh
->ifinfo_lock
);
387 neigh_ifinfo
= batadv_neigh_ifinfo_get(neigh
, if_outgoing
);
391 neigh_ifinfo
= kzalloc(sizeof(*neigh_ifinfo
), GFP_ATOMIC
);
395 if (if_outgoing
&& !atomic_inc_not_zero(&if_outgoing
->refcount
)) {
401 INIT_HLIST_NODE(&neigh_ifinfo
->list
);
402 atomic_set(&neigh_ifinfo
->refcount
, 2);
403 neigh_ifinfo
->if_outgoing
= if_outgoing
;
405 hlist_add_head_rcu(&neigh_ifinfo
->list
, &neigh
->ifinfo_list
);
408 spin_unlock_bh(&neigh
->ifinfo_lock
);
414 * batadv_neigh_node_get - retrieve a neighbour from the list
415 * @orig_node: originator which the neighbour belongs to
416 * @hard_iface: the interface where this neighbour is connected to
417 * @addr: the address of the neighbour
419 * Looks for and possibly returns a neighbour belonging to this originator list
420 * which is connected through the provided hard interface.
421 * Returns NULL if the neighbour is not found.
423 static struct batadv_neigh_node
*
424 batadv_neigh_node_get(const struct batadv_orig_node
*orig_node
,
425 const struct batadv_hard_iface
*hard_iface
,
428 struct batadv_neigh_node
*tmp_neigh_node
, *res
= NULL
;
431 hlist_for_each_entry_rcu(tmp_neigh_node
, &orig_node
->neigh_list
, list
) {
432 if (!batadv_compare_eth(tmp_neigh_node
->addr
, addr
))
435 if (tmp_neigh_node
->if_incoming
!= hard_iface
)
438 if (!atomic_inc_not_zero(&tmp_neigh_node
->refcount
))
441 res
= tmp_neigh_node
;
450 * batadv_neigh_node_new - create and init a new neigh_node object
451 * @orig_node: originator object representing the neighbour
452 * @hard_iface: the interface where the neighbour is connected to
453 * @neigh_addr: the mac address of the neighbour interface
455 * Allocates a new neigh_node object and initialises all the generic fields.
456 * Returns the new object or NULL on failure.
458 struct batadv_neigh_node
*
459 batadv_neigh_node_new(struct batadv_orig_node
*orig_node
,
460 struct batadv_hard_iface
*hard_iface
,
461 const u8
*neigh_addr
)
463 struct batadv_neigh_node
*neigh_node
;
465 neigh_node
= batadv_neigh_node_get(orig_node
, hard_iface
, neigh_addr
);
469 neigh_node
= kzalloc(sizeof(*neigh_node
), GFP_ATOMIC
);
473 if (!atomic_inc_not_zero(&hard_iface
->refcount
)) {
479 INIT_HLIST_NODE(&neigh_node
->list
);
480 INIT_HLIST_HEAD(&neigh_node
->ifinfo_list
);
481 spin_lock_init(&neigh_node
->ifinfo_lock
);
483 ether_addr_copy(neigh_node
->addr
, neigh_addr
);
484 neigh_node
->if_incoming
= hard_iface
;
485 neigh_node
->orig_node
= orig_node
;
487 /* extra reference for return */
488 atomic_set(&neigh_node
->refcount
, 2);
490 spin_lock_bh(&orig_node
->neigh_list_lock
);
491 hlist_add_head_rcu(&neigh_node
->list
, &orig_node
->neigh_list
);
492 spin_unlock_bh(&orig_node
->neigh_list_lock
);
494 batadv_dbg(BATADV_DBG_BATMAN
, orig_node
->bat_priv
,
495 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
496 neigh_addr
, orig_node
->orig
, hard_iface
->net_dev
->name
);
503 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
504 * free after rcu grace period
505 * @orig_ifinfo: the orig_ifinfo object to release
507 static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo
*orig_ifinfo
)
509 struct batadv_neigh_node
*router
;
511 if (orig_ifinfo
->if_outgoing
!= BATADV_IF_DEFAULT
)
512 batadv_hardif_free_ref(orig_ifinfo
->if_outgoing
);
514 /* this is the last reference to this object */
515 router
= rcu_dereference_protected(orig_ifinfo
->router
, true);
517 batadv_neigh_node_free_ref(router
);
519 kfree_rcu(orig_ifinfo
, rcu
);
523 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
525 * @orig_ifinfo: the orig_ifinfo object to release
527 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo
*orig_ifinfo
)
529 if (atomic_dec_and_test(&orig_ifinfo
->refcount
))
530 batadv_orig_ifinfo_release(orig_ifinfo
);
534 * batadv_orig_node_free_rcu - free the orig_node
535 * @rcu: rcu pointer of the orig_node
537 static void batadv_orig_node_free_rcu(struct rcu_head
*rcu
)
539 struct batadv_orig_node
*orig_node
;
541 orig_node
= container_of(rcu
, struct batadv_orig_node
, rcu
);
543 batadv_mcast_purge_orig(orig_node
);
545 batadv_frag_purge_orig(orig_node
, NULL
);
547 if (orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free
)
548 orig_node
->bat_priv
->bat_algo_ops
->bat_orig_free(orig_node
);
550 kfree(orig_node
->tt_buff
);
555 * batadv_orig_node_release - release orig_node from lists and queue for
556 * free after rcu grace period
557 * @orig_node: the orig node to free
559 static void batadv_orig_node_release(struct batadv_orig_node
*orig_node
)
561 struct hlist_node
*node_tmp
;
562 struct batadv_neigh_node
*neigh_node
;
563 struct batadv_orig_ifinfo
*orig_ifinfo
;
565 spin_lock_bh(&orig_node
->neigh_list_lock
);
567 /* for all neighbors towards this originator ... */
568 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
569 &orig_node
->neigh_list
, list
) {
570 hlist_del_rcu(&neigh_node
->list
);
571 batadv_neigh_node_free_ref(neigh_node
);
574 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
575 &orig_node
->ifinfo_list
, list
) {
576 hlist_del_rcu(&orig_ifinfo
->list
);
577 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
579 spin_unlock_bh(&orig_node
->neigh_list_lock
);
582 batadv_nc_purge_orig(orig_node
->bat_priv
, orig_node
, NULL
);
584 call_rcu(&orig_node
->rcu
, batadv_orig_node_free_rcu
);
588 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
590 * @orig_node: the orig node to free
592 void batadv_orig_node_free_ref(struct batadv_orig_node
*orig_node
)
594 if (atomic_dec_and_test(&orig_node
->refcount
))
595 batadv_orig_node_release(orig_node
);
598 void batadv_originator_free(struct batadv_priv
*bat_priv
)
600 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
601 struct hlist_node
*node_tmp
;
602 struct hlist_head
*head
;
603 spinlock_t
*list_lock
; /* spinlock to protect write access */
604 struct batadv_orig_node
*orig_node
;
610 cancel_delayed_work_sync(&bat_priv
->orig_work
);
612 bat_priv
->orig_hash
= NULL
;
614 for (i
= 0; i
< hash
->size
; i
++) {
615 head
= &hash
->table
[i
];
616 list_lock
= &hash
->list_locks
[i
];
618 spin_lock_bh(list_lock
);
619 hlist_for_each_entry_safe(orig_node
, node_tmp
,
621 hlist_del_rcu(&orig_node
->hash_entry
);
622 batadv_orig_node_free_ref(orig_node
);
624 spin_unlock_bh(list_lock
);
627 batadv_hash_destroy(hash
);
631 * batadv_orig_node_new - creates a new orig_node
632 * @bat_priv: the bat priv with all the soft interface information
633 * @addr: the mac address of the originator
635 * Creates a new originator object and initialise all the generic fields.
636 * The new object is not added to the originator list.
637 * Returns the newly created object or NULL on failure.
639 struct batadv_orig_node
*batadv_orig_node_new(struct batadv_priv
*bat_priv
,
642 struct batadv_orig_node
*orig_node
;
643 struct batadv_orig_node_vlan
*vlan
;
644 unsigned long reset_time
;
647 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
648 "Creating new originator: %pM\n", addr
);
650 orig_node
= kzalloc(sizeof(*orig_node
), GFP_ATOMIC
);
654 INIT_HLIST_HEAD(&orig_node
->neigh_list
);
655 INIT_HLIST_HEAD(&orig_node
->vlan_list
);
656 INIT_HLIST_HEAD(&orig_node
->ifinfo_list
);
657 spin_lock_init(&orig_node
->bcast_seqno_lock
);
658 spin_lock_init(&orig_node
->neigh_list_lock
);
659 spin_lock_init(&orig_node
->tt_buff_lock
);
660 spin_lock_init(&orig_node
->tt_lock
);
661 spin_lock_init(&orig_node
->vlan_list_lock
);
663 batadv_nc_init_orig(orig_node
);
665 /* extra reference for return */
666 atomic_set(&orig_node
->refcount
, 2);
668 orig_node
->bat_priv
= bat_priv
;
669 ether_addr_copy(orig_node
->orig
, addr
);
670 batadv_dat_init_orig_node_addr(orig_node
);
671 atomic_set(&orig_node
->last_ttvn
, 0);
672 orig_node
->tt_buff
= NULL
;
673 orig_node
->tt_buff_len
= 0;
674 orig_node
->last_seen
= jiffies
;
675 reset_time
= jiffies
- 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS
);
676 orig_node
->bcast_seqno_reset
= reset_time
;
678 #ifdef CONFIG_BATMAN_ADV_MCAST
679 orig_node
->mcast_flags
= BATADV_NO_FLAGS
;
680 INIT_HLIST_NODE(&orig_node
->mcast_want_all_unsnoopables_node
);
681 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv4_node
);
682 INIT_HLIST_NODE(&orig_node
->mcast_want_all_ipv6_node
);
683 spin_lock_init(&orig_node
->mcast_handler_lock
);
686 /* create a vlan object for the "untagged" LAN */
687 vlan
= batadv_orig_node_vlan_new(orig_node
, BATADV_NO_FLAGS
);
690 /* batadv_orig_node_vlan_new() increases the refcounter.
691 * Immediately release vlan since it is not needed anymore in this
694 batadv_orig_node_vlan_free_ref(vlan
);
696 for (i
= 0; i
< BATADV_FRAG_BUFFER_COUNT
; i
++) {
697 INIT_HLIST_HEAD(&orig_node
->fragments
[i
].head
);
698 spin_lock_init(&orig_node
->fragments
[i
].lock
);
699 orig_node
->fragments
[i
].size
= 0;
709 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
710 * @bat_priv: the bat priv with all the soft interface information
711 * @neigh: orig node which is to be checked
714 batadv_purge_neigh_ifinfo(struct batadv_priv
*bat_priv
,
715 struct batadv_neigh_node
*neigh
)
717 struct batadv_neigh_ifinfo
*neigh_ifinfo
;
718 struct batadv_hard_iface
*if_outgoing
;
719 struct hlist_node
*node_tmp
;
721 spin_lock_bh(&neigh
->ifinfo_lock
);
723 /* for all ifinfo objects for this neighinator */
724 hlist_for_each_entry_safe(neigh_ifinfo
, node_tmp
,
725 &neigh
->ifinfo_list
, list
) {
726 if_outgoing
= neigh_ifinfo
->if_outgoing
;
728 /* always keep the default interface */
729 if (if_outgoing
== BATADV_IF_DEFAULT
)
732 /* don't purge if the interface is not (going) down */
733 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
734 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
735 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
738 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
739 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
740 neigh
->addr
, if_outgoing
->net_dev
->name
);
742 hlist_del_rcu(&neigh_ifinfo
->list
);
743 batadv_neigh_ifinfo_free_ref(neigh_ifinfo
);
746 spin_unlock_bh(&neigh
->ifinfo_lock
);
750 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
751 * @bat_priv: the bat priv with all the soft interface information
752 * @orig_node: orig node which is to be checked
754 * Returns true if any ifinfo entry was purged, false otherwise.
757 batadv_purge_orig_ifinfo(struct batadv_priv
*bat_priv
,
758 struct batadv_orig_node
*orig_node
)
760 struct batadv_orig_ifinfo
*orig_ifinfo
;
761 struct batadv_hard_iface
*if_outgoing
;
762 struct hlist_node
*node_tmp
;
763 bool ifinfo_purged
= false;
765 spin_lock_bh(&orig_node
->neigh_list_lock
);
767 /* for all ifinfo objects for this originator */
768 hlist_for_each_entry_safe(orig_ifinfo
, node_tmp
,
769 &orig_node
->ifinfo_list
, list
) {
770 if_outgoing
= orig_ifinfo
->if_outgoing
;
772 /* always keep the default interface */
773 if (if_outgoing
== BATADV_IF_DEFAULT
)
776 /* don't purge if the interface is not (going) down */
777 if ((if_outgoing
->if_status
!= BATADV_IF_INACTIVE
) &&
778 (if_outgoing
->if_status
!= BATADV_IF_NOT_IN_USE
) &&
779 (if_outgoing
->if_status
!= BATADV_IF_TO_BE_REMOVED
))
782 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
783 "router/ifinfo purge: originator %pM, iface: %s\n",
784 orig_node
->orig
, if_outgoing
->net_dev
->name
);
786 ifinfo_purged
= true;
788 hlist_del_rcu(&orig_ifinfo
->list
);
789 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
790 if (orig_node
->last_bonding_candidate
== orig_ifinfo
) {
791 orig_node
->last_bonding_candidate
= NULL
;
792 batadv_orig_ifinfo_free_ref(orig_ifinfo
);
796 spin_unlock_bh(&orig_node
->neigh_list_lock
);
798 return ifinfo_purged
;
802 * batadv_purge_orig_neighbors - purges neighbors from originator
803 * @bat_priv: the bat priv with all the soft interface information
804 * @orig_node: orig node which is to be checked
806 * Returns true if any neighbor was purged, false otherwise
809 batadv_purge_orig_neighbors(struct batadv_priv
*bat_priv
,
810 struct batadv_orig_node
*orig_node
)
812 struct hlist_node
*node_tmp
;
813 struct batadv_neigh_node
*neigh_node
;
814 bool neigh_purged
= false;
815 unsigned long last_seen
;
816 struct batadv_hard_iface
*if_incoming
;
818 spin_lock_bh(&orig_node
->neigh_list_lock
);
820 /* for all neighbors towards this originator ... */
821 hlist_for_each_entry_safe(neigh_node
, node_tmp
,
822 &orig_node
->neigh_list
, list
) {
823 last_seen
= neigh_node
->last_seen
;
824 if_incoming
= neigh_node
->if_incoming
;
826 if ((batadv_has_timed_out(last_seen
, BATADV_PURGE_TIMEOUT
)) ||
827 (if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
828 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
829 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
)) {
830 if ((if_incoming
->if_status
== BATADV_IF_INACTIVE
) ||
831 (if_incoming
->if_status
== BATADV_IF_NOT_IN_USE
) ||
832 (if_incoming
->if_status
== BATADV_IF_TO_BE_REMOVED
))
833 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
834 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
835 orig_node
->orig
, neigh_node
->addr
,
836 if_incoming
->net_dev
->name
);
838 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
839 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
840 orig_node
->orig
, neigh_node
->addr
,
841 jiffies_to_msecs(last_seen
));
845 hlist_del_rcu(&neigh_node
->list
);
846 batadv_neigh_node_free_ref(neigh_node
);
848 /* only necessary if not the whole neighbor is to be
849 * deleted, but some interface has been removed.
851 batadv_purge_neigh_ifinfo(bat_priv
, neigh_node
);
855 spin_unlock_bh(&orig_node
->neigh_list_lock
);
860 * batadv_find_best_neighbor - finds the best neighbor after purging
861 * @bat_priv: the bat priv with all the soft interface information
862 * @orig_node: orig node which is to be checked
863 * @if_outgoing: the interface for which the metric should be compared
865 * Returns the current best neighbor, with refcount increased.
867 static struct batadv_neigh_node
*
868 batadv_find_best_neighbor(struct batadv_priv
*bat_priv
,
869 struct batadv_orig_node
*orig_node
,
870 struct batadv_hard_iface
*if_outgoing
)
872 struct batadv_neigh_node
*best
= NULL
, *neigh
;
873 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
876 hlist_for_each_entry_rcu(neigh
, &orig_node
->neigh_list
, list
) {
877 if (best
&& (bao
->bat_neigh_cmp(neigh
, if_outgoing
,
878 best
, if_outgoing
) <= 0))
881 if (!atomic_inc_not_zero(&neigh
->refcount
))
885 batadv_neigh_node_free_ref(best
);
895 * batadv_purge_orig_node - purges obsolete information from an orig_node
896 * @bat_priv: the bat priv with all the soft interface information
897 * @orig_node: orig node which is to be checked
899 * This function checks if the orig_node or substructures of it have become
900 * obsolete, and purges this information if that's the case.
902 * Returns true if the orig_node is to be removed, false otherwise.
904 static bool batadv_purge_orig_node(struct batadv_priv
*bat_priv
,
905 struct batadv_orig_node
*orig_node
)
907 struct batadv_neigh_node
*best_neigh_node
;
908 struct batadv_hard_iface
*hard_iface
;
909 bool changed_ifinfo
, changed_neigh
;
911 if (batadv_has_timed_out(orig_node
->last_seen
,
912 2 * BATADV_PURGE_TIMEOUT
)) {
913 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
914 "Originator timeout: originator %pM, last_seen %u\n",
916 jiffies_to_msecs(orig_node
->last_seen
));
919 changed_ifinfo
= batadv_purge_orig_ifinfo(bat_priv
, orig_node
);
920 changed_neigh
= batadv_purge_orig_neighbors(bat_priv
, orig_node
);
922 if (!changed_ifinfo
&& !changed_neigh
)
925 /* first for NULL ... */
926 best_neigh_node
= batadv_find_best_neighbor(bat_priv
, orig_node
,
928 batadv_update_route(bat_priv
, orig_node
, BATADV_IF_DEFAULT
,
931 batadv_neigh_node_free_ref(best_neigh_node
);
933 /* ... then for all other interfaces. */
935 list_for_each_entry_rcu(hard_iface
, &batadv_hardif_list
, list
) {
936 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
)
939 if (hard_iface
->soft_iface
!= bat_priv
->soft_iface
)
942 best_neigh_node
= batadv_find_best_neighbor(bat_priv
,
945 batadv_update_route(bat_priv
, orig_node
, hard_iface
,
948 batadv_neigh_node_free_ref(best_neigh_node
);
955 static void _batadv_purge_orig(struct batadv_priv
*bat_priv
)
957 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
958 struct hlist_node
*node_tmp
;
959 struct hlist_head
*head
;
960 spinlock_t
*list_lock
; /* spinlock to protect write access */
961 struct batadv_orig_node
*orig_node
;
967 /* for all origins... */
968 for (i
= 0; i
< hash
->size
; i
++) {
969 head
= &hash
->table
[i
];
970 list_lock
= &hash
->list_locks
[i
];
972 spin_lock_bh(list_lock
);
973 hlist_for_each_entry_safe(orig_node
, node_tmp
,
975 if (batadv_purge_orig_node(bat_priv
, orig_node
)) {
976 batadv_gw_node_delete(bat_priv
, orig_node
);
977 hlist_del_rcu(&orig_node
->hash_entry
);
978 batadv_tt_global_del_orig(orig_node
->bat_priv
,
980 "originator timed out");
981 batadv_orig_node_free_ref(orig_node
);
985 batadv_frag_purge_orig(orig_node
,
986 batadv_frag_check_entry
);
988 spin_unlock_bh(list_lock
);
991 batadv_gw_election(bat_priv
);
994 static void batadv_purge_orig(struct work_struct
*work
)
996 struct delayed_work
*delayed_work
;
997 struct batadv_priv
*bat_priv
;
999 delayed_work
= container_of(work
, struct delayed_work
, work
);
1000 bat_priv
= container_of(delayed_work
, struct batadv_priv
, orig_work
);
1001 _batadv_purge_orig(bat_priv
);
1002 queue_delayed_work(batadv_event_workqueue
,
1003 &bat_priv
->orig_work
,
1004 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD
));
1007 void batadv_purge_orig_ref(struct batadv_priv
*bat_priv
)
1009 _batadv_purge_orig(bat_priv
);
1012 int batadv_orig_seq_print_text(struct seq_file
*seq
, void *offset
)
1014 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1015 struct batadv_priv
*bat_priv
= netdev_priv(net_dev
);
1016 struct batadv_hard_iface
*primary_if
;
1018 primary_if
= batadv_seq_print_text_primary_if_get(seq
);
1022 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1023 BATADV_SOURCE_VERSION
, primary_if
->net_dev
->name
,
1024 primary_if
->net_dev
->dev_addr
, net_dev
->name
,
1025 bat_priv
->bat_algo_ops
->name
);
1027 batadv_hardif_free_ref(primary_if
);
1029 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1031 "No printing function for this routing protocol\n");
1035 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
,
1042 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1043 * outgoing interface
1044 * @seq: debugfs table seq_file struct
1049 int batadv_orig_hardif_seq_print_text(struct seq_file
*seq
, void *offset
)
1051 struct net_device
*net_dev
= (struct net_device
*)seq
->private;
1052 struct batadv_hard_iface
*hard_iface
;
1053 struct batadv_priv
*bat_priv
;
1055 hard_iface
= batadv_hardif_get_by_netdev(net_dev
);
1057 if (!hard_iface
|| !hard_iface
->soft_iface
) {
1058 seq_puts(seq
, "Interface not known to B.A.T.M.A.N.\n");
1062 bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1063 if (!bat_priv
->bat_algo_ops
->bat_orig_print
) {
1065 "No printing function for this routing protocol\n");
1069 if (hard_iface
->if_status
!= BATADV_IF_ACTIVE
) {
1070 seq_puts(seq
, "Interface not active\n");
1074 seq_printf(seq
, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1075 BATADV_SOURCE_VERSION
, hard_iface
->net_dev
->name
,
1076 hard_iface
->net_dev
->dev_addr
,
1077 hard_iface
->soft_iface
->name
, bat_priv
->bat_algo_ops
->name
);
1079 bat_priv
->bat_algo_ops
->bat_orig_print(bat_priv
, seq
, hard_iface
);
1083 batadv_hardif_free_ref(hard_iface
);
1087 int batadv_orig_hash_add_if(struct batadv_hard_iface
*hard_iface
,
1090 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1091 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1092 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1093 struct hlist_head
*head
;
1094 struct batadv_orig_node
*orig_node
;
1098 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1101 for (i
= 0; i
< hash
->size
; i
++) {
1102 head
= &hash
->table
[i
];
1105 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1107 if (bao
->bat_orig_add_if
)
1108 ret
= bao
->bat_orig_add_if(orig_node
,
1123 int batadv_orig_hash_del_if(struct batadv_hard_iface
*hard_iface
,
1126 struct batadv_priv
*bat_priv
= netdev_priv(hard_iface
->soft_iface
);
1127 struct batadv_hashtable
*hash
= bat_priv
->orig_hash
;
1128 struct hlist_head
*head
;
1129 struct batadv_hard_iface
*hard_iface_tmp
;
1130 struct batadv_orig_node
*orig_node
;
1131 struct batadv_algo_ops
*bao
= bat_priv
->bat_algo_ops
;
1135 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1138 for (i
= 0; i
< hash
->size
; i
++) {
1139 head
= &hash
->table
[i
];
1142 hlist_for_each_entry_rcu(orig_node
, head
, hash_entry
) {
1144 if (bao
->bat_orig_del_if
)
1145 ret
= bao
->bat_orig_del_if(orig_node
,
1147 hard_iface
->if_num
);
1154 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1156 list_for_each_entry_rcu(hard_iface_tmp
, &batadv_hardif_list
, list
) {
1157 if (hard_iface_tmp
->if_status
== BATADV_IF_NOT_IN_USE
)
1160 if (hard_iface
== hard_iface_tmp
)
1163 if (hard_iface
->soft_iface
!= hard_iface_tmp
->soft_iface
)
1166 if (hard_iface_tmp
->if_num
> hard_iface
->if_num
)
1167 hard_iface_tmp
->if_num
--;
1171 hard_iface
->if_num
= -1;