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;
200 struct batadv_algo_ops *bao;
202 bao = neigh_node->orig_node->bat_priv->bat_algo_ops;
204 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
205 &neigh_node->ifinfo_list, list) {
206 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
209 if (bao->bat_neigh_free)
210 bao->bat_neigh_free(neigh_node);
212 batadv_hardif_free_ref(neigh_node->if_incoming);
214 kfree_rcu(neigh_node, rcu);
218 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
219 * and possibly release it
220 * @neigh_node: neigh neighbor to free
222 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
224 if (atomic_dec_and_test(&neigh_node->refcount))
225 batadv_neigh_node_release(neigh_node);
229 * batadv_orig_node_get_router - router to the originator depending on iface
230 * @orig_node: the orig node for the router
231 * @if_outgoing: the interface where the payload packet has been received or
232 * the OGM should be sent to
234 * Returns the neighbor which should be router for this orig_node/iface.
236 * The object is returned with refcounter increased by 1.
238 struct batadv_neigh_node *
239 batadv_orig_router_get(struct batadv_orig_node *orig_node,
240 const struct batadv_hard_iface *if_outgoing)
242 struct batadv_orig_ifinfo *orig_ifinfo;
243 struct batadv_neigh_node *router = NULL;
246 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
247 if (orig_ifinfo->if_outgoing != if_outgoing)
250 router = rcu_dereference(orig_ifinfo->router);
254 if (router && !atomic_inc_not_zero(&router->refcount))
262 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
263 * @orig_node: the orig node to be queried
264 * @if_outgoing: the interface for which the ifinfo should be acquired
266 * Returns the requested orig_ifinfo or NULL if not found.
268 * The object is returned with refcounter increased by 1.
270 struct batadv_orig_ifinfo *
271 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
272 struct batadv_hard_iface *if_outgoing)
274 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
277 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
279 if (tmp->if_outgoing != if_outgoing)
282 if (!atomic_inc_not_zero(&tmp->refcount))
294 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
295 * @orig_node: the orig node to be queried
296 * @if_outgoing: the interface for which the ifinfo should be acquired
298 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
299 * interface otherwise. The object is created and added to the list
300 * if it does not exist.
302 * The object is returned with refcounter increased by 1.
304 struct batadv_orig_ifinfo *
305 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
306 struct batadv_hard_iface *if_outgoing)
308 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
309 unsigned long reset_time;
311 spin_lock_bh(&orig_node->neigh_list_lock);
313 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
317 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
321 if (if_outgoing != BATADV_IF_DEFAULT &&
322 !atomic_inc_not_zero(&if_outgoing->refcount)) {
328 reset_time = jiffies - 1;
329 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
330 orig_ifinfo->batman_seqno_reset = reset_time;
331 orig_ifinfo->if_outgoing = if_outgoing;
332 INIT_HLIST_NODE(&orig_ifinfo->list);
333 atomic_set(&orig_ifinfo->refcount, 2);
334 hlist_add_head_rcu(&orig_ifinfo->list,
335 &orig_node->ifinfo_list);
337 spin_unlock_bh(&orig_node->neigh_list_lock);
342 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
343 * @neigh_node: the neigh node to be queried
344 * @if_outgoing: the interface for which the ifinfo should be acquired
346 * The object is returned with refcounter increased by 1.
348 * Returns the requested neigh_ifinfo or NULL if not found
350 struct batadv_neigh_ifinfo *
351 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
352 struct batadv_hard_iface *if_outgoing)
354 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
358 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
360 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
363 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
366 neigh_ifinfo = tmp_neigh_ifinfo;
375 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
376 * @neigh_node: the neigh node to be queried
377 * @if_outgoing: the interface for which the ifinfo should be acquired
379 * Returns NULL in case of failure or the neigh_ifinfo object for the
380 * if_outgoing interface otherwise. The object is created and added to the list
381 * if it does not exist.
383 * The object is returned with refcounter increased by 1.
385 struct batadv_neigh_ifinfo *
386 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
387 struct batadv_hard_iface *if_outgoing)
389 struct batadv_neigh_ifinfo *neigh_ifinfo;
391 spin_lock_bh(&neigh->ifinfo_lock);
393 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
397 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
401 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
407 INIT_HLIST_NODE(&neigh_ifinfo->list);
408 atomic_set(&neigh_ifinfo->refcount, 2);
409 neigh_ifinfo->if_outgoing = if_outgoing;
411 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
414 spin_unlock_bh(&neigh->ifinfo_lock);
420 * batadv_neigh_node_get - retrieve a neighbour from the list
421 * @orig_node: originator which the neighbour belongs to
422 * @hard_iface: the interface where this neighbour is connected to
423 * @addr: the address of the neighbour
425 * Looks for and possibly returns a neighbour belonging to this originator list
426 * which is connected through the provided hard interface.
427 * Returns NULL if the neighbour is not found.
429 static struct batadv_neigh_node *
430 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
431 const struct batadv_hard_iface *hard_iface,
434 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
437 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
438 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
441 if (tmp_neigh_node->if_incoming != hard_iface)
444 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
447 res = tmp_neigh_node;
456 * batadv_neigh_node_new - create and init a new neigh_node object
457 * @orig_node: originator object representing the neighbour
458 * @hard_iface: the interface where the neighbour is connected to
459 * @neigh_addr: the mac address of the neighbour interface
461 * Allocates a new neigh_node object and initialises all the generic fields.
462 * Returns the new object or NULL on failure.
464 struct batadv_neigh_node *
465 batadv_neigh_node_new(struct batadv_orig_node *orig_node,
466 struct batadv_hard_iface *hard_iface,
467 const u8 *neigh_addr)
469 struct batadv_neigh_node *neigh_node;
471 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
475 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
479 if (!atomic_inc_not_zero(&hard_iface->refcount)) {
485 INIT_HLIST_NODE(&neigh_node->list);
486 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
487 spin_lock_init(&neigh_node->ifinfo_lock);
489 ether_addr_copy(neigh_node->addr, neigh_addr);
490 neigh_node->if_incoming = hard_iface;
491 neigh_node->orig_node = orig_node;
493 /* extra reference for return */
494 atomic_set(&neigh_node->refcount, 2);
496 spin_lock_bh(&orig_node->neigh_list_lock);
497 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
498 spin_unlock_bh(&orig_node->neigh_list_lock);
500 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
501 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
502 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
509 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
510 * free after rcu grace period
511 * @orig_ifinfo: the orig_ifinfo object to release
513 static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo *orig_ifinfo)
515 struct batadv_neigh_node *router;
517 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
518 batadv_hardif_free_ref(orig_ifinfo->if_outgoing);
520 /* this is the last reference to this object */
521 router = rcu_dereference_protected(orig_ifinfo->router, true);
523 batadv_neigh_node_free_ref(router);
525 kfree_rcu(orig_ifinfo, rcu);
529 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
531 * @orig_ifinfo: the orig_ifinfo object to release
533 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
535 if (atomic_dec_and_test(&orig_ifinfo->refcount))
536 batadv_orig_ifinfo_release(orig_ifinfo);
540 * batadv_orig_node_free_rcu - free the orig_node
541 * @rcu: rcu pointer of the orig_node
543 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
545 struct batadv_orig_node *orig_node;
547 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
549 batadv_mcast_purge_orig(orig_node);
551 batadv_frag_purge_orig(orig_node, NULL);
553 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
554 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
556 kfree(orig_node->tt_buff);
561 * batadv_orig_node_release - release orig_node from lists and queue for
562 * free after rcu grace period
563 * @orig_node: the orig node to free
565 static void batadv_orig_node_release(struct batadv_orig_node *orig_node)
567 struct hlist_node *node_tmp;
568 struct batadv_neigh_node *neigh_node;
569 struct batadv_orig_ifinfo *orig_ifinfo;
571 spin_lock_bh(&orig_node->neigh_list_lock);
573 /* for all neighbors towards this originator ... */
574 hlist_for_each_entry_safe(neigh_node, node_tmp,
575 &orig_node->neigh_list, list) {
576 hlist_del_rcu(&neigh_node->list);
577 batadv_neigh_node_free_ref(neigh_node);
580 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
581 &orig_node->ifinfo_list, list) {
582 hlist_del_rcu(&orig_ifinfo->list);
583 batadv_orig_ifinfo_free_ref(orig_ifinfo);
585 spin_unlock_bh(&orig_node->neigh_list_lock);
588 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
590 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
594 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
596 * @orig_node: the orig node to free
598 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
600 if (atomic_dec_and_test(&orig_node->refcount))
601 batadv_orig_node_release(orig_node);
604 void batadv_originator_free(struct batadv_priv *bat_priv)
606 struct batadv_hashtable *hash = bat_priv->orig_hash;
607 struct hlist_node *node_tmp;
608 struct hlist_head *head;
609 spinlock_t *list_lock; /* spinlock to protect write access */
610 struct batadv_orig_node *orig_node;
616 cancel_delayed_work_sync(&bat_priv->orig_work);
618 bat_priv->orig_hash = NULL;
620 for (i = 0; i < hash->size; i++) {
621 head = &hash->table[i];
622 list_lock = &hash->list_locks[i];
624 spin_lock_bh(list_lock);
625 hlist_for_each_entry_safe(orig_node, node_tmp,
627 hlist_del_rcu(&orig_node->hash_entry);
628 batadv_orig_node_free_ref(orig_node);
630 spin_unlock_bh(list_lock);
633 batadv_hash_destroy(hash);
637 * batadv_orig_node_new - creates a new orig_node
638 * @bat_priv: the bat priv with all the soft interface information
639 * @addr: the mac address of the originator
641 * Creates a new originator object and initialise all the generic fields.
642 * The new object is not added to the originator list.
643 * Returns the newly created object or NULL on failure.
645 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
648 struct batadv_orig_node *orig_node;
649 struct batadv_orig_node_vlan *vlan;
650 unsigned long reset_time;
653 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
654 "Creating new originator: %pM\n", addr);
656 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
660 INIT_HLIST_HEAD(&orig_node->neigh_list);
661 INIT_HLIST_HEAD(&orig_node->vlan_list);
662 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
663 spin_lock_init(&orig_node->bcast_seqno_lock);
664 spin_lock_init(&orig_node->neigh_list_lock);
665 spin_lock_init(&orig_node->tt_buff_lock);
666 spin_lock_init(&orig_node->tt_lock);
667 spin_lock_init(&orig_node->vlan_list_lock);
669 batadv_nc_init_orig(orig_node);
671 /* extra reference for return */
672 atomic_set(&orig_node->refcount, 2);
674 orig_node->bat_priv = bat_priv;
675 ether_addr_copy(orig_node->orig, addr);
676 batadv_dat_init_orig_node_addr(orig_node);
677 atomic_set(&orig_node->last_ttvn, 0);
678 orig_node->tt_buff = NULL;
679 orig_node->tt_buff_len = 0;
680 orig_node->last_seen = jiffies;
681 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
682 orig_node->bcast_seqno_reset = reset_time;
684 #ifdef CONFIG_BATMAN_ADV_MCAST
685 orig_node->mcast_flags = BATADV_NO_FLAGS;
686 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
687 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
688 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
689 spin_lock_init(&orig_node->mcast_handler_lock);
692 /* create a vlan object for the "untagged" LAN */
693 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
696 /* batadv_orig_node_vlan_new() increases the refcounter.
697 * Immediately release vlan since it is not needed anymore in this
700 batadv_orig_node_vlan_free_ref(vlan);
702 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
703 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
704 spin_lock_init(&orig_node->fragments[i].lock);
705 orig_node->fragments[i].size = 0;
715 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
716 * @bat_priv: the bat priv with all the soft interface information
717 * @neigh: orig node which is to be checked
720 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
721 struct batadv_neigh_node *neigh)
723 struct batadv_neigh_ifinfo *neigh_ifinfo;
724 struct batadv_hard_iface *if_outgoing;
725 struct hlist_node *node_tmp;
727 spin_lock_bh(&neigh->ifinfo_lock);
729 /* for all ifinfo objects for this neighinator */
730 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
731 &neigh->ifinfo_list, list) {
732 if_outgoing = neigh_ifinfo->if_outgoing;
734 /* always keep the default interface */
735 if (if_outgoing == BATADV_IF_DEFAULT)
738 /* don't purge if the interface is not (going) down */
739 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
740 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
741 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
744 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
745 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
746 neigh->addr, if_outgoing->net_dev->name);
748 hlist_del_rcu(&neigh_ifinfo->list);
749 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
752 spin_unlock_bh(&neigh->ifinfo_lock);
756 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
757 * @bat_priv: the bat priv with all the soft interface information
758 * @orig_node: orig node which is to be checked
760 * Returns true if any ifinfo entry was purged, false otherwise.
763 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
764 struct batadv_orig_node *orig_node)
766 struct batadv_orig_ifinfo *orig_ifinfo;
767 struct batadv_hard_iface *if_outgoing;
768 struct hlist_node *node_tmp;
769 bool ifinfo_purged = false;
771 spin_lock_bh(&orig_node->neigh_list_lock);
773 /* for all ifinfo objects for this originator */
774 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
775 &orig_node->ifinfo_list, list) {
776 if_outgoing = orig_ifinfo->if_outgoing;
778 /* always keep the default interface */
779 if (if_outgoing == BATADV_IF_DEFAULT)
782 /* don't purge if the interface is not (going) down */
783 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
784 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
785 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
788 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
789 "router/ifinfo purge: originator %pM, iface: %s\n",
790 orig_node->orig, if_outgoing->net_dev->name);
792 ifinfo_purged = true;
794 hlist_del_rcu(&orig_ifinfo->list);
795 batadv_orig_ifinfo_free_ref(orig_ifinfo);
796 if (orig_node->last_bonding_candidate == orig_ifinfo) {
797 orig_node->last_bonding_candidate = NULL;
798 batadv_orig_ifinfo_free_ref(orig_ifinfo);
802 spin_unlock_bh(&orig_node->neigh_list_lock);
804 return ifinfo_purged;
808 * batadv_purge_orig_neighbors - purges neighbors from originator
809 * @bat_priv: the bat priv with all the soft interface information
810 * @orig_node: orig node which is to be checked
812 * Returns true if any neighbor was purged, false otherwise
815 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
816 struct batadv_orig_node *orig_node)
818 struct hlist_node *node_tmp;
819 struct batadv_neigh_node *neigh_node;
820 bool neigh_purged = false;
821 unsigned long last_seen;
822 struct batadv_hard_iface *if_incoming;
824 spin_lock_bh(&orig_node->neigh_list_lock);
826 /* for all neighbors towards this originator ... */
827 hlist_for_each_entry_safe(neigh_node, node_tmp,
828 &orig_node->neigh_list, list) {
829 last_seen = neigh_node->last_seen;
830 if_incoming = neigh_node->if_incoming;
832 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
833 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
834 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
835 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
836 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
837 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
838 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
839 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
840 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
841 orig_node->orig, neigh_node->addr,
842 if_incoming->net_dev->name);
844 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
845 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
846 orig_node->orig, neigh_node->addr,
847 jiffies_to_msecs(last_seen));
851 hlist_del_rcu(&neigh_node->list);
852 batadv_neigh_node_free_ref(neigh_node);
854 /* only necessary if not the whole neighbor is to be
855 * deleted, but some interface has been removed.
857 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
861 spin_unlock_bh(&orig_node->neigh_list_lock);
866 * batadv_find_best_neighbor - finds the best neighbor after purging
867 * @bat_priv: the bat priv with all the soft interface information
868 * @orig_node: orig node which is to be checked
869 * @if_outgoing: the interface for which the metric should be compared
871 * Returns the current best neighbor, with refcount increased.
873 static struct batadv_neigh_node *
874 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
875 struct batadv_orig_node *orig_node,
876 struct batadv_hard_iface *if_outgoing)
878 struct batadv_neigh_node *best = NULL, *neigh;
879 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
882 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
883 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
884 best, if_outgoing) <= 0))
887 if (!atomic_inc_not_zero(&neigh->refcount))
891 batadv_neigh_node_free_ref(best);
901 * batadv_purge_orig_node - purges obsolete information from an orig_node
902 * @bat_priv: the bat priv with all the soft interface information
903 * @orig_node: orig node which is to be checked
905 * This function checks if the orig_node or substructures of it have become
906 * obsolete, and purges this information if that's the case.
908 * Returns true if the orig_node is to be removed, false otherwise.
910 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
911 struct batadv_orig_node *orig_node)
913 struct batadv_neigh_node *best_neigh_node;
914 struct batadv_hard_iface *hard_iface;
915 bool changed_ifinfo, changed_neigh;
917 if (batadv_has_timed_out(orig_node->last_seen,
918 2 * BATADV_PURGE_TIMEOUT)) {
919 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
920 "Originator timeout: originator %pM, last_seen %u\n",
922 jiffies_to_msecs(orig_node->last_seen));
925 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
926 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
928 if (!changed_ifinfo && !changed_neigh)
931 /* first for NULL ... */
932 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
934 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
937 batadv_neigh_node_free_ref(best_neigh_node);
939 /* ... then for all other interfaces. */
941 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
942 if (hard_iface->if_status != BATADV_IF_ACTIVE)
945 if (hard_iface->soft_iface != bat_priv->soft_iface)
948 best_neigh_node = batadv_find_best_neighbor(bat_priv,
951 batadv_update_route(bat_priv, orig_node, hard_iface,
954 batadv_neigh_node_free_ref(best_neigh_node);
961 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
963 struct batadv_hashtable *hash = bat_priv->orig_hash;
964 struct hlist_node *node_tmp;
965 struct hlist_head *head;
966 spinlock_t *list_lock; /* spinlock to protect write access */
967 struct batadv_orig_node *orig_node;
973 /* for all origins... */
974 for (i = 0; i < hash->size; i++) {
975 head = &hash->table[i];
976 list_lock = &hash->list_locks[i];
978 spin_lock_bh(list_lock);
979 hlist_for_each_entry_safe(orig_node, node_tmp,
981 if (batadv_purge_orig_node(bat_priv, orig_node)) {
982 batadv_gw_node_delete(bat_priv, orig_node);
983 hlist_del_rcu(&orig_node->hash_entry);
984 batadv_tt_global_del_orig(orig_node->bat_priv,
986 "originator timed out");
987 batadv_orig_node_free_ref(orig_node);
991 batadv_frag_purge_orig(orig_node,
992 batadv_frag_check_entry);
994 spin_unlock_bh(list_lock);
997 batadv_gw_election(bat_priv);
1000 static void batadv_purge_orig(struct work_struct *work)
1002 struct delayed_work *delayed_work;
1003 struct batadv_priv *bat_priv;
1005 delayed_work = container_of(work, struct delayed_work, work);
1006 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1007 _batadv_purge_orig(bat_priv);
1008 queue_delayed_work(batadv_event_workqueue,
1009 &bat_priv->orig_work,
1010 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1013 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1015 _batadv_purge_orig(bat_priv);
1018 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1020 struct net_device *net_dev = (struct net_device *)seq->private;
1021 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1022 struct batadv_hard_iface *primary_if;
1024 primary_if = batadv_seq_print_text_primary_if_get(seq);
1028 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1029 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1030 primary_if->net_dev->dev_addr, net_dev->name,
1031 bat_priv->bat_algo_ops->name);
1033 batadv_hardif_free_ref(primary_if);
1035 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1037 "No printing function for this routing protocol\n");
1041 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1048 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1049 * outgoing interface
1050 * @seq: debugfs table seq_file struct
1055 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1057 struct net_device *net_dev = (struct net_device *)seq->private;
1058 struct batadv_hard_iface *hard_iface;
1059 struct batadv_priv *bat_priv;
1061 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1063 if (!hard_iface || !hard_iface->soft_iface) {
1064 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1068 bat_priv = netdev_priv(hard_iface->soft_iface);
1069 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1071 "No printing function for this routing protocol\n");
1075 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1076 seq_puts(seq, "Interface not active\n");
1080 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1081 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1082 hard_iface->net_dev->dev_addr,
1083 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1085 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1089 batadv_hardif_free_ref(hard_iface);
1093 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1096 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1097 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1098 struct batadv_hashtable *hash = bat_priv->orig_hash;
1099 struct hlist_head *head;
1100 struct batadv_orig_node *orig_node;
1104 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1107 for (i = 0; i < hash->size; i++) {
1108 head = &hash->table[i];
1111 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1113 if (bao->bat_orig_add_if)
1114 ret = bao->bat_orig_add_if(orig_node,
1129 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1132 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1133 struct batadv_hashtable *hash = bat_priv->orig_hash;
1134 struct hlist_head *head;
1135 struct batadv_hard_iface *hard_iface_tmp;
1136 struct batadv_orig_node *orig_node;
1137 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1141 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1144 for (i = 0; i < hash->size; i++) {
1145 head = &hash->table[i];
1148 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1150 if (bao->bat_orig_del_if)
1151 ret = bao->bat_orig_del_if(orig_node,
1153 hard_iface->if_num);
1160 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1162 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1163 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1166 if (hard_iface == hard_iface_tmp)
1169 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1172 if (hard_iface_tmp->if_num > hard_iface->if_num)
1173 hard_iface_tmp->if_num--;
1177 hard_iface->if_num = -1;