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 spin_lock_bh(&orig_node->neigh_list_lock);
467 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
471 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
475 if (!atomic_inc_not_zero(&hard_iface->refcount)) {
481 INIT_HLIST_NODE(&neigh_node->list);
482 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
483 spin_lock_init(&neigh_node->ifinfo_lock);
485 ether_addr_copy(neigh_node->addr, neigh_addr);
486 neigh_node->if_incoming = hard_iface;
487 neigh_node->orig_node = orig_node;
488 neigh_node->last_seen = jiffies;
490 /* extra reference for return */
491 atomic_set(&neigh_node->refcount, 2);
493 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
495 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
496 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
497 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
500 spin_unlock_bh(&orig_node->neigh_list_lock);
506 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
507 * free after rcu grace period
508 * @orig_ifinfo: the orig_ifinfo object to release
510 static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo *orig_ifinfo)
512 struct batadv_neigh_node *router;
514 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
515 batadv_hardif_free_ref(orig_ifinfo->if_outgoing);
517 /* this is the last reference to this object */
518 router = rcu_dereference_protected(orig_ifinfo->router, true);
520 batadv_neigh_node_free_ref(router);
522 kfree_rcu(orig_ifinfo, rcu);
526 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
528 * @orig_ifinfo: the orig_ifinfo object to release
530 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
532 if (atomic_dec_and_test(&orig_ifinfo->refcount))
533 batadv_orig_ifinfo_release(orig_ifinfo);
537 * batadv_orig_node_free_rcu - free the orig_node
538 * @rcu: rcu pointer of the orig_node
540 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
542 struct batadv_orig_node *orig_node;
544 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
546 batadv_mcast_purge_orig(orig_node);
548 batadv_frag_purge_orig(orig_node, NULL);
550 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
551 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
553 kfree(orig_node->tt_buff);
558 * batadv_orig_node_release - release orig_node from lists and queue for
559 * free after rcu grace period
560 * @orig_node: the orig node to free
562 static void batadv_orig_node_release(struct batadv_orig_node *orig_node)
564 struct hlist_node *node_tmp;
565 struct batadv_neigh_node *neigh_node;
566 struct batadv_orig_ifinfo *orig_ifinfo;
567 struct batadv_orig_node_vlan *vlan;
568 struct batadv_orig_ifinfo *last_candidate;
570 spin_lock_bh(&orig_node->neigh_list_lock);
572 /* for all neighbors towards this originator ... */
573 hlist_for_each_entry_safe(neigh_node, node_tmp,
574 &orig_node->neigh_list, list) {
575 hlist_del_rcu(&neigh_node->list);
576 batadv_neigh_node_free_ref(neigh_node);
579 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
580 &orig_node->ifinfo_list, list) {
581 hlist_del_rcu(&orig_ifinfo->list);
582 batadv_orig_ifinfo_free_ref(orig_ifinfo);
585 last_candidate = orig_node->last_bonding_candidate;
586 orig_node->last_bonding_candidate = NULL;
587 spin_unlock_bh(&orig_node->neigh_list_lock);
590 batadv_orig_ifinfo_free_ref(last_candidate);
592 spin_lock_bh(&orig_node->vlan_list_lock);
593 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
594 hlist_del_rcu(&vlan->list);
595 batadv_orig_node_vlan_free_ref(vlan);
597 spin_unlock_bh(&orig_node->vlan_list_lock);
600 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
602 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
606 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
608 * @orig_node: the orig node to free
610 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
612 if (atomic_dec_and_test(&orig_node->refcount))
613 batadv_orig_node_release(orig_node);
616 void batadv_originator_free(struct batadv_priv *bat_priv)
618 struct batadv_hashtable *hash = bat_priv->orig_hash;
619 struct hlist_node *node_tmp;
620 struct hlist_head *head;
621 spinlock_t *list_lock; /* spinlock to protect write access */
622 struct batadv_orig_node *orig_node;
628 cancel_delayed_work_sync(&bat_priv->orig_work);
630 bat_priv->orig_hash = NULL;
632 for (i = 0; i < hash->size; i++) {
633 head = &hash->table[i];
634 list_lock = &hash->list_locks[i];
636 spin_lock_bh(list_lock);
637 hlist_for_each_entry_safe(orig_node, node_tmp,
639 hlist_del_rcu(&orig_node->hash_entry);
640 batadv_orig_node_free_ref(orig_node);
642 spin_unlock_bh(list_lock);
645 batadv_hash_destroy(hash);
649 * batadv_orig_node_new - creates a new orig_node
650 * @bat_priv: the bat priv with all the soft interface information
651 * @addr: the mac address of the originator
653 * Creates a new originator object and initialise all the generic fields.
654 * The new object is not added to the originator list.
655 * Returns the newly created object or NULL on failure.
657 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
660 struct batadv_orig_node *orig_node;
661 struct batadv_orig_node_vlan *vlan;
662 unsigned long reset_time;
665 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
666 "Creating new originator: %pM\n", addr);
668 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
672 INIT_HLIST_HEAD(&orig_node->neigh_list);
673 INIT_HLIST_HEAD(&orig_node->vlan_list);
674 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
675 spin_lock_init(&orig_node->bcast_seqno_lock);
676 spin_lock_init(&orig_node->neigh_list_lock);
677 spin_lock_init(&orig_node->tt_buff_lock);
678 spin_lock_init(&orig_node->tt_lock);
679 spin_lock_init(&orig_node->vlan_list_lock);
681 batadv_nc_init_orig(orig_node);
683 /* extra reference for return */
684 atomic_set(&orig_node->refcount, 2);
686 orig_node->bat_priv = bat_priv;
687 ether_addr_copy(orig_node->orig, addr);
688 batadv_dat_init_orig_node_addr(orig_node);
689 atomic_set(&orig_node->last_ttvn, 0);
690 orig_node->tt_buff = NULL;
691 orig_node->tt_buff_len = 0;
692 orig_node->last_seen = jiffies;
693 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
694 orig_node->bcast_seqno_reset = reset_time;
696 #ifdef CONFIG_BATMAN_ADV_MCAST
697 orig_node->mcast_flags = BATADV_NO_FLAGS;
698 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
699 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
700 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
701 spin_lock_init(&orig_node->mcast_handler_lock);
704 /* create a vlan object for the "untagged" LAN */
705 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
708 /* batadv_orig_node_vlan_new() increases the refcounter.
709 * Immediately release vlan since it is not needed anymore in this
712 batadv_orig_node_vlan_free_ref(vlan);
714 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
715 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
716 spin_lock_init(&orig_node->fragments[i].lock);
717 orig_node->fragments[i].size = 0;
727 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
728 * @bat_priv: the bat priv with all the soft interface information
729 * @neigh: orig node which is to be checked
732 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
733 struct batadv_neigh_node *neigh)
735 struct batadv_neigh_ifinfo *neigh_ifinfo;
736 struct batadv_hard_iface *if_outgoing;
737 struct hlist_node *node_tmp;
739 spin_lock_bh(&neigh->ifinfo_lock);
741 /* for all ifinfo objects for this neighinator */
742 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
743 &neigh->ifinfo_list, list) {
744 if_outgoing = neigh_ifinfo->if_outgoing;
746 /* always keep the default interface */
747 if (if_outgoing == BATADV_IF_DEFAULT)
750 /* don't purge if the interface is not (going) down */
751 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
752 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
753 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
756 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
757 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
758 neigh->addr, if_outgoing->net_dev->name);
760 hlist_del_rcu(&neigh_ifinfo->list);
761 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
764 spin_unlock_bh(&neigh->ifinfo_lock);
768 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
769 * @bat_priv: the bat priv with all the soft interface information
770 * @orig_node: orig node which is to be checked
772 * Returns true if any ifinfo entry was purged, false otherwise.
775 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
776 struct batadv_orig_node *orig_node)
778 struct batadv_orig_ifinfo *orig_ifinfo;
779 struct batadv_hard_iface *if_outgoing;
780 struct hlist_node *node_tmp;
781 bool ifinfo_purged = false;
783 spin_lock_bh(&orig_node->neigh_list_lock);
785 /* for all ifinfo objects for this originator */
786 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
787 &orig_node->ifinfo_list, list) {
788 if_outgoing = orig_ifinfo->if_outgoing;
790 /* always keep the default interface */
791 if (if_outgoing == BATADV_IF_DEFAULT)
794 /* don't purge if the interface is not (going) down */
795 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
796 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
797 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
800 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
801 "router/ifinfo purge: originator %pM, iface: %s\n",
802 orig_node->orig, if_outgoing->net_dev->name);
804 ifinfo_purged = true;
806 hlist_del_rcu(&orig_ifinfo->list);
807 batadv_orig_ifinfo_free_ref(orig_ifinfo);
808 if (orig_node->last_bonding_candidate == orig_ifinfo) {
809 orig_node->last_bonding_candidate = NULL;
810 batadv_orig_ifinfo_free_ref(orig_ifinfo);
814 spin_unlock_bh(&orig_node->neigh_list_lock);
816 return ifinfo_purged;
820 * batadv_purge_orig_neighbors - purges neighbors from originator
821 * @bat_priv: the bat priv with all the soft interface information
822 * @orig_node: orig node which is to be checked
824 * Returns true if any neighbor was purged, false otherwise
827 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
828 struct batadv_orig_node *orig_node)
830 struct hlist_node *node_tmp;
831 struct batadv_neigh_node *neigh_node;
832 bool neigh_purged = false;
833 unsigned long last_seen;
834 struct batadv_hard_iface *if_incoming;
836 spin_lock_bh(&orig_node->neigh_list_lock);
838 /* for all neighbors towards this originator ... */
839 hlist_for_each_entry_safe(neigh_node, node_tmp,
840 &orig_node->neigh_list, list) {
841 last_seen = neigh_node->last_seen;
842 if_incoming = neigh_node->if_incoming;
844 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
845 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
846 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
847 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
848 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
849 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
850 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
851 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
852 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
853 orig_node->orig, neigh_node->addr,
854 if_incoming->net_dev->name);
856 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
857 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
858 orig_node->orig, neigh_node->addr,
859 jiffies_to_msecs(last_seen));
863 hlist_del_rcu(&neigh_node->list);
864 batadv_neigh_node_free_ref(neigh_node);
866 /* only necessary if not the whole neighbor is to be
867 * deleted, but some interface has been removed.
869 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
873 spin_unlock_bh(&orig_node->neigh_list_lock);
878 * batadv_find_best_neighbor - finds the best neighbor after purging
879 * @bat_priv: the bat priv with all the soft interface information
880 * @orig_node: orig node which is to be checked
881 * @if_outgoing: the interface for which the metric should be compared
883 * Returns the current best neighbor, with refcount increased.
885 static struct batadv_neigh_node *
886 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
887 struct batadv_orig_node *orig_node,
888 struct batadv_hard_iface *if_outgoing)
890 struct batadv_neigh_node *best = NULL, *neigh;
891 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
894 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
895 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
896 best, if_outgoing) <= 0))
899 if (!atomic_inc_not_zero(&neigh->refcount))
903 batadv_neigh_node_free_ref(best);
913 * batadv_purge_orig_node - purges obsolete information from an orig_node
914 * @bat_priv: the bat priv with all the soft interface information
915 * @orig_node: orig node which is to be checked
917 * This function checks if the orig_node or substructures of it have become
918 * obsolete, and purges this information if that's the case.
920 * Returns true if the orig_node is to be removed, false otherwise.
922 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
923 struct batadv_orig_node *orig_node)
925 struct batadv_neigh_node *best_neigh_node;
926 struct batadv_hard_iface *hard_iface;
927 bool changed_ifinfo, changed_neigh;
929 if (batadv_has_timed_out(orig_node->last_seen,
930 2 * BATADV_PURGE_TIMEOUT)) {
931 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
932 "Originator timeout: originator %pM, last_seen %u\n",
934 jiffies_to_msecs(orig_node->last_seen));
937 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
938 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
940 if (!changed_ifinfo && !changed_neigh)
943 /* first for NULL ... */
944 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
946 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
949 batadv_neigh_node_free_ref(best_neigh_node);
951 /* ... then for all other interfaces. */
953 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
954 if (hard_iface->if_status != BATADV_IF_ACTIVE)
957 if (hard_iface->soft_iface != bat_priv->soft_iface)
960 best_neigh_node = batadv_find_best_neighbor(bat_priv,
963 batadv_update_route(bat_priv, orig_node, hard_iface,
966 batadv_neigh_node_free_ref(best_neigh_node);
973 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
975 struct batadv_hashtable *hash = bat_priv->orig_hash;
976 struct hlist_node *node_tmp;
977 struct hlist_head *head;
978 spinlock_t *list_lock; /* spinlock to protect write access */
979 struct batadv_orig_node *orig_node;
985 /* for all origins... */
986 for (i = 0; i < hash->size; i++) {
987 head = &hash->table[i];
988 list_lock = &hash->list_locks[i];
990 spin_lock_bh(list_lock);
991 hlist_for_each_entry_safe(orig_node, node_tmp,
993 if (batadv_purge_orig_node(bat_priv, orig_node)) {
994 batadv_gw_node_delete(bat_priv, orig_node);
995 hlist_del_rcu(&orig_node->hash_entry);
996 batadv_tt_global_del_orig(orig_node->bat_priv,
998 "originator timed out");
999 batadv_orig_node_free_ref(orig_node);
1003 batadv_frag_purge_orig(orig_node,
1004 batadv_frag_check_entry);
1006 spin_unlock_bh(list_lock);
1009 batadv_gw_election(bat_priv);
1012 static void batadv_purge_orig(struct work_struct *work)
1014 struct delayed_work *delayed_work;
1015 struct batadv_priv *bat_priv;
1017 delayed_work = container_of(work, struct delayed_work, work);
1018 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1019 _batadv_purge_orig(bat_priv);
1020 queue_delayed_work(batadv_event_workqueue,
1021 &bat_priv->orig_work,
1022 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1025 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1027 _batadv_purge_orig(bat_priv);
1030 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1032 struct net_device *net_dev = (struct net_device *)seq->private;
1033 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1034 struct batadv_hard_iface *primary_if;
1036 primary_if = batadv_seq_print_text_primary_if_get(seq);
1040 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1041 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1042 primary_if->net_dev->dev_addr, net_dev->name,
1043 bat_priv->bat_algo_ops->name);
1045 batadv_hardif_free_ref(primary_if);
1047 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1049 "No printing function for this routing protocol\n");
1053 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1060 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1061 * outgoing interface
1062 * @seq: debugfs table seq_file struct
1067 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1069 struct net_device *net_dev = (struct net_device *)seq->private;
1070 struct batadv_hard_iface *hard_iface;
1071 struct batadv_priv *bat_priv;
1073 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1075 if (!hard_iface || !hard_iface->soft_iface) {
1076 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1080 bat_priv = netdev_priv(hard_iface->soft_iface);
1081 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1083 "No printing function for this routing protocol\n");
1087 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1088 seq_puts(seq, "Interface not active\n");
1092 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1093 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1094 hard_iface->net_dev->dev_addr,
1095 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1097 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1101 batadv_hardif_free_ref(hard_iface);
1105 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1106 unsigned int max_if_num)
1108 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1109 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1110 struct batadv_hashtable *hash = bat_priv->orig_hash;
1111 struct hlist_head *head;
1112 struct batadv_orig_node *orig_node;
1116 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1119 for (i = 0; i < hash->size; i++) {
1120 head = &hash->table[i];
1123 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1125 if (bao->bat_orig_add_if)
1126 ret = bao->bat_orig_add_if(orig_node,
1141 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1142 unsigned int max_if_num)
1144 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1145 struct batadv_hashtable *hash = bat_priv->orig_hash;
1146 struct hlist_head *head;
1147 struct batadv_hard_iface *hard_iface_tmp;
1148 struct batadv_orig_node *orig_node;
1149 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1153 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1156 for (i = 0; i < hash->size; i++) {
1157 head = &hash->table[i];
1160 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1162 if (bao->bat_orig_del_if)
1163 ret = bao->bat_orig_del_if(orig_node,
1165 hard_iface->if_num);
1172 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1174 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1175 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1178 if (hard_iface == hard_iface_tmp)
1181 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1184 if (hard_iface_tmp->if_num > hard_iface->if_num)
1185 hard_iface_tmp->if_num--;
1189 hard_iface->if_num = -1;