1 /* Copyright (C) 2009-2015 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "originator.h"
21 #include <linux/errno.h>
22 #include <linux/etherdevice.h>
24 #include <linux/jiffies.h>
25 #include <linux/kernel.h>
26 #include <linux/list.h>
27 #include <linux/lockdep.h>
28 #include <linux/netdevice.h>
29 #include <linux/rculist.h>
30 #include <linux/seq_file.h>
31 #include <linux/slab.h>
32 #include <linux/spinlock.h>
33 #include <linux/workqueue.h>
35 #include "distributed-arp-table.h"
36 #include "fragmentation.h"
37 #include "gateway_client.h"
38 #include "hard-interface.h"
40 #include "multicast.h"
41 #include "network-coding.h"
43 #include "translation-table.h"
46 static struct lock_class_key batadv_orig_hash_lock_class_key;
48 static void batadv_purge_orig(struct work_struct *work);
50 /* returns 1 if they are the same originator */
51 int batadv_compare_orig(const struct hlist_node *node, const void *data2)
53 const void *data1 = container_of(node, struct batadv_orig_node,
56 return batadv_compare_eth(data1, data2);
60 * batadv_orig_node_vlan_get - get an orig_node_vlan object
61 * @orig_node: the originator serving the VLAN
62 * @vid: the VLAN identifier
64 * Returns the vlan object identified by vid and belonging to orig_node or NULL
65 * if it does not exist.
67 struct batadv_orig_node_vlan *
68 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
71 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
74 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
78 if (!atomic_inc_not_zero(&tmp->refcount))
91 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
93 * @orig_node: the originator serving the VLAN
94 * @vid: the VLAN identifier
96 * Returns NULL in case of failure or the vlan object identified by vid and
97 * belonging to orig_node otherwise. The object is created and added to the list
98 * if it does not exist.
100 * The object is returned with refcounter increased by 1.
102 struct batadv_orig_node_vlan *
103 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
106 struct batadv_orig_node_vlan *vlan;
108 spin_lock_bh(&orig_node->vlan_list_lock);
110 /* first look if an object for this vid already exists */
111 vlan = batadv_orig_node_vlan_get(orig_node, vid);
115 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
119 atomic_set(&vlan->refcount, 2);
122 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
125 spin_unlock_bh(&orig_node->vlan_list_lock);
131 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
132 * the originator-vlan object
133 * @orig_vlan: the originator-vlan object to release
135 void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
137 if (atomic_dec_and_test(&orig_vlan->refcount))
138 kfree_rcu(orig_vlan, rcu);
141 int batadv_originator_init(struct batadv_priv *bat_priv)
143 if (bat_priv->orig_hash)
146 bat_priv->orig_hash = batadv_hash_new(1024);
148 if (!bat_priv->orig_hash)
151 batadv_hash_set_lock_class(bat_priv->orig_hash,
152 &batadv_orig_hash_lock_class_key);
154 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
155 queue_delayed_work(batadv_event_workqueue,
156 &bat_priv->orig_work,
157 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
166 * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
167 * @rcu: rcu pointer of the neigh_ifinfo object
169 static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu)
171 struct batadv_neigh_ifinfo *neigh_ifinfo;
173 neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu);
175 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
176 batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing);
182 * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free
183 * the neigh_ifinfo (without rcu callback)
184 * @neigh_ifinfo: the neigh_ifinfo object to release
187 batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo)
189 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
190 batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu);
194 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free
196 * @neigh_ifinfo: the neigh_ifinfo object to release
198 void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
200 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
201 call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu);
205 * batadv_neigh_node_free_rcu - free the neigh_node
206 * @rcu: rcu pointer of the neigh_node
208 static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
210 struct hlist_node *node_tmp;
211 struct batadv_neigh_node *neigh_node;
212 struct batadv_neigh_ifinfo *neigh_ifinfo;
213 struct batadv_algo_ops *bao;
215 neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
216 bao = neigh_node->orig_node->bat_priv->bat_algo_ops;
218 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
219 &neigh_node->ifinfo_list, list) {
220 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo);
223 if (bao->bat_neigh_free)
224 bao->bat_neigh_free(neigh_node);
226 batadv_hardif_free_ref_now(neigh_node->if_incoming);
232 * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter
233 * and possibly free it (without rcu callback)
234 * @neigh_node: neigh neighbor to free
237 batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node)
239 if (atomic_dec_and_test(&neigh_node->refcount))
240 batadv_neigh_node_free_rcu(&neigh_node->rcu);
244 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
245 * and possibly free it
246 * @neigh_node: neigh neighbor to free
248 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
250 if (atomic_dec_and_test(&neigh_node->refcount))
251 call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
255 * batadv_orig_node_get_router - router to the originator depending on iface
256 * @orig_node: the orig node for the router
257 * @if_outgoing: the interface where the payload packet has been received or
258 * the OGM should be sent to
260 * Returns the neighbor which should be router for this orig_node/iface.
262 * The object is returned with refcounter increased by 1.
264 struct batadv_neigh_node *
265 batadv_orig_router_get(struct batadv_orig_node *orig_node,
266 const struct batadv_hard_iface *if_outgoing)
268 struct batadv_orig_ifinfo *orig_ifinfo;
269 struct batadv_neigh_node *router = NULL;
272 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
273 if (orig_ifinfo->if_outgoing != if_outgoing)
276 router = rcu_dereference(orig_ifinfo->router);
280 if (router && !atomic_inc_not_zero(&router->refcount))
288 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
289 * @orig_node: the orig node to be queried
290 * @if_outgoing: the interface for which the ifinfo should be acquired
292 * Returns the requested orig_ifinfo or NULL if not found.
294 * The object is returned with refcounter increased by 1.
296 struct batadv_orig_ifinfo *
297 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
298 struct batadv_hard_iface *if_outgoing)
300 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
303 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
305 if (tmp->if_outgoing != if_outgoing)
308 if (!atomic_inc_not_zero(&tmp->refcount))
320 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
321 * @orig_node: the orig node to be queried
322 * @if_outgoing: the interface for which the ifinfo should be acquired
324 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
325 * interface otherwise. The object is created and added to the list
326 * if it does not exist.
328 * The object is returned with refcounter increased by 1.
330 struct batadv_orig_ifinfo *
331 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
332 struct batadv_hard_iface *if_outgoing)
334 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
335 unsigned long reset_time;
337 spin_lock_bh(&orig_node->neigh_list_lock);
339 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
343 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
347 if (if_outgoing != BATADV_IF_DEFAULT &&
348 !atomic_inc_not_zero(&if_outgoing->refcount)) {
354 reset_time = jiffies - 1;
355 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
356 orig_ifinfo->batman_seqno_reset = reset_time;
357 orig_ifinfo->if_outgoing = if_outgoing;
358 INIT_HLIST_NODE(&orig_ifinfo->list);
359 atomic_set(&orig_ifinfo->refcount, 2);
360 hlist_add_head_rcu(&orig_ifinfo->list,
361 &orig_node->ifinfo_list);
363 spin_unlock_bh(&orig_node->neigh_list_lock);
368 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
369 * @neigh_node: the neigh node to be queried
370 * @if_outgoing: the interface for which the ifinfo should be acquired
372 * The object is returned with refcounter increased by 1.
374 * Returns the requested neigh_ifinfo or NULL if not found
376 struct batadv_neigh_ifinfo *
377 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
378 struct batadv_hard_iface *if_outgoing)
380 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
384 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
386 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
389 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
392 neigh_ifinfo = tmp_neigh_ifinfo;
401 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
402 * @neigh_node: the neigh node to be queried
403 * @if_outgoing: the interface for which the ifinfo should be acquired
405 * Returns NULL in case of failure or the neigh_ifinfo object for the
406 * if_outgoing interface otherwise. The object is created and added to the list
407 * if it does not exist.
409 * The object is returned with refcounter increased by 1.
411 struct batadv_neigh_ifinfo *
412 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
413 struct batadv_hard_iface *if_outgoing)
415 struct batadv_neigh_ifinfo *neigh_ifinfo;
417 spin_lock_bh(&neigh->ifinfo_lock);
419 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
423 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
427 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
433 INIT_HLIST_NODE(&neigh_ifinfo->list);
434 atomic_set(&neigh_ifinfo->refcount, 2);
435 neigh_ifinfo->if_outgoing = if_outgoing;
437 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
440 spin_unlock_bh(&neigh->ifinfo_lock);
446 * batadv_neigh_node_new - create and init a new neigh_node object
447 * @hard_iface: the interface where the neighbour is connected to
448 * @neigh_addr: the mac address of the neighbour interface
449 * @orig_node: originator object representing the neighbour
451 * Allocates a new neigh_node object and initialises all the generic fields.
452 * Returns the new object or NULL on failure.
454 struct batadv_neigh_node *
455 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
456 const u8 *neigh_addr, struct batadv_orig_node *orig_node)
458 struct batadv_neigh_node *neigh_node;
460 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
464 INIT_HLIST_NODE(&neigh_node->list);
465 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
466 spin_lock_init(&neigh_node->ifinfo_lock);
468 ether_addr_copy(neigh_node->addr, neigh_addr);
469 neigh_node->if_incoming = hard_iface;
470 neigh_node->orig_node = orig_node;
472 /* extra reference for return */
473 atomic_set(&neigh_node->refcount, 2);
480 * batadv_neigh_node_get - retrieve a neighbour from the list
481 * @orig_node: originator which the neighbour belongs to
482 * @hard_iface: the interface where this neighbour is connected to
483 * @addr: the address of the neighbour
485 * Looks for and possibly returns a neighbour belonging to this originator list
486 * which is connected through the provided hard interface.
487 * Returns NULL if the neighbour is not found.
489 struct batadv_neigh_node *
490 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
491 const struct batadv_hard_iface *hard_iface,
494 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
497 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
498 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
501 if (tmp_neigh_node->if_incoming != hard_iface)
504 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
507 res = tmp_neigh_node;
516 * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
517 * @rcu: rcu pointer of the orig_ifinfo object
519 static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu)
521 struct batadv_orig_ifinfo *orig_ifinfo;
522 struct batadv_neigh_node *router;
524 orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu);
526 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
527 batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing);
529 /* this is the last reference to this object */
530 router = rcu_dereference_protected(orig_ifinfo->router, true);
532 batadv_neigh_node_free_ref_now(router);
537 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
538 * the orig_ifinfo (without rcu callback)
539 * @orig_ifinfo: the orig_ifinfo object to release
542 batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo)
544 if (atomic_dec_and_test(&orig_ifinfo->refcount))
545 batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu);
549 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
551 * @orig_ifinfo: the orig_ifinfo object to release
553 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
555 if (atomic_dec_and_test(&orig_ifinfo->refcount))
556 call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu);
559 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
561 struct hlist_node *node_tmp;
562 struct batadv_neigh_node *neigh_node;
563 struct batadv_orig_node *orig_node;
564 struct batadv_orig_ifinfo *orig_ifinfo;
566 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
568 spin_lock_bh(&orig_node->neigh_list_lock);
570 /* for all neighbors towards this originator ... */
571 hlist_for_each_entry_safe(neigh_node, node_tmp,
572 &orig_node->neigh_list, list) {
573 hlist_del_rcu(&neigh_node->list);
574 batadv_neigh_node_free_ref_now(neigh_node);
577 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
578 &orig_node->ifinfo_list, list) {
579 hlist_del_rcu(&orig_ifinfo->list);
580 batadv_orig_ifinfo_free_ref_now(orig_ifinfo);
582 spin_unlock_bh(&orig_node->neigh_list_lock);
584 batadv_mcast_purge_orig(orig_node);
587 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
589 batadv_frag_purge_orig(orig_node, NULL);
591 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
592 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
594 kfree(orig_node->tt_buff);
599 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
600 * schedule an rcu callback for freeing it
601 * @orig_node: the orig node to free
603 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
605 if (atomic_dec_and_test(&orig_node->refcount))
606 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
610 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
611 * possibly free it (without rcu callback)
612 * @orig_node: the orig node to free
614 void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
616 if (atomic_dec_and_test(&orig_node->refcount))
617 batadv_orig_node_free_rcu(&orig_node->rcu);
620 void batadv_originator_free(struct batadv_priv *bat_priv)
622 struct batadv_hashtable *hash = bat_priv->orig_hash;
623 struct hlist_node *node_tmp;
624 struct hlist_head *head;
625 spinlock_t *list_lock; /* spinlock to protect write access */
626 struct batadv_orig_node *orig_node;
632 cancel_delayed_work_sync(&bat_priv->orig_work);
634 bat_priv->orig_hash = NULL;
636 for (i = 0; i < hash->size; i++) {
637 head = &hash->table[i];
638 list_lock = &hash->list_locks[i];
640 spin_lock_bh(list_lock);
641 hlist_for_each_entry_safe(orig_node, node_tmp,
643 hlist_del_rcu(&orig_node->hash_entry);
644 batadv_orig_node_free_ref(orig_node);
646 spin_unlock_bh(list_lock);
649 batadv_hash_destroy(hash);
653 * batadv_orig_node_new - creates a new orig_node
654 * @bat_priv: the bat priv with all the soft interface information
655 * @addr: the mac address of the originator
657 * Creates a new originator object and initialise all the generic fields.
658 * The new object is not added to the originator list.
659 * Returns the newly created object or NULL on failure.
661 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
664 struct batadv_orig_node *orig_node;
665 struct batadv_orig_node_vlan *vlan;
666 unsigned long reset_time;
669 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
670 "Creating new originator: %pM\n", addr);
672 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
676 INIT_HLIST_HEAD(&orig_node->neigh_list);
677 INIT_HLIST_HEAD(&orig_node->vlan_list);
678 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
679 spin_lock_init(&orig_node->bcast_seqno_lock);
680 spin_lock_init(&orig_node->neigh_list_lock);
681 spin_lock_init(&orig_node->tt_buff_lock);
682 spin_lock_init(&orig_node->tt_lock);
683 spin_lock_init(&orig_node->vlan_list_lock);
685 batadv_nc_init_orig(orig_node);
687 /* extra reference for return */
688 atomic_set(&orig_node->refcount, 2);
690 orig_node->bat_priv = bat_priv;
691 ether_addr_copy(orig_node->orig, addr);
692 batadv_dat_init_orig_node_addr(orig_node);
693 atomic_set(&orig_node->last_ttvn, 0);
694 orig_node->tt_buff = NULL;
695 orig_node->tt_buff_len = 0;
696 orig_node->last_seen = jiffies;
697 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
698 orig_node->bcast_seqno_reset = reset_time;
700 #ifdef CONFIG_BATMAN_ADV_MCAST
701 orig_node->mcast_flags = BATADV_NO_FLAGS;
702 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
703 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
704 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
705 spin_lock_init(&orig_node->mcast_handler_lock);
708 /* create a vlan object for the "untagged" LAN */
709 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
712 /* batadv_orig_node_vlan_new() increases the refcounter.
713 * Immediately release vlan since it is not needed anymore in this
716 batadv_orig_node_vlan_free_ref(vlan);
718 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
719 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
720 spin_lock_init(&orig_node->fragments[i].lock);
721 orig_node->fragments[i].size = 0;
731 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
732 * @bat_priv: the bat priv with all the soft interface information
733 * @neigh: orig node which is to be checked
736 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
737 struct batadv_neigh_node *neigh)
739 struct batadv_neigh_ifinfo *neigh_ifinfo;
740 struct batadv_hard_iface *if_outgoing;
741 struct hlist_node *node_tmp;
743 spin_lock_bh(&neigh->ifinfo_lock);
745 /* for all ifinfo objects for this neighinator */
746 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
747 &neigh->ifinfo_list, list) {
748 if_outgoing = neigh_ifinfo->if_outgoing;
750 /* always keep the default interface */
751 if (if_outgoing == BATADV_IF_DEFAULT)
754 /* don't purge if the interface is not (going) down */
755 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
756 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
757 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
760 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
761 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
762 neigh->addr, if_outgoing->net_dev->name);
764 hlist_del_rcu(&neigh_ifinfo->list);
765 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
768 spin_unlock_bh(&neigh->ifinfo_lock);
772 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
773 * @bat_priv: the bat priv with all the soft interface information
774 * @orig_node: orig node which is to be checked
776 * Returns true if any ifinfo entry was purged, false otherwise.
779 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
780 struct batadv_orig_node *orig_node)
782 struct batadv_orig_ifinfo *orig_ifinfo;
783 struct batadv_hard_iface *if_outgoing;
784 struct hlist_node *node_tmp;
785 bool ifinfo_purged = false;
787 spin_lock_bh(&orig_node->neigh_list_lock);
789 /* for all ifinfo objects for this originator */
790 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
791 &orig_node->ifinfo_list, list) {
792 if_outgoing = orig_ifinfo->if_outgoing;
794 /* always keep the default interface */
795 if (if_outgoing == BATADV_IF_DEFAULT)
798 /* don't purge if the interface is not (going) down */
799 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
800 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
801 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
804 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
805 "router/ifinfo purge: originator %pM, iface: %s\n",
806 orig_node->orig, if_outgoing->net_dev->name);
808 ifinfo_purged = true;
810 hlist_del_rcu(&orig_ifinfo->list);
811 batadv_orig_ifinfo_free_ref(orig_ifinfo);
812 if (orig_node->last_bonding_candidate == orig_ifinfo) {
813 orig_node->last_bonding_candidate = NULL;
814 batadv_orig_ifinfo_free_ref(orig_ifinfo);
818 spin_unlock_bh(&orig_node->neigh_list_lock);
820 return ifinfo_purged;
824 * batadv_purge_orig_neighbors - purges neighbors from originator
825 * @bat_priv: the bat priv with all the soft interface information
826 * @orig_node: orig node which is to be checked
828 * Returns true if any neighbor was purged, false otherwise
831 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
832 struct batadv_orig_node *orig_node)
834 struct hlist_node *node_tmp;
835 struct batadv_neigh_node *neigh_node;
836 bool neigh_purged = false;
837 unsigned long last_seen;
838 struct batadv_hard_iface *if_incoming;
840 spin_lock_bh(&orig_node->neigh_list_lock);
842 /* for all neighbors towards this originator ... */
843 hlist_for_each_entry_safe(neigh_node, node_tmp,
844 &orig_node->neigh_list, list) {
845 last_seen = neigh_node->last_seen;
846 if_incoming = neigh_node->if_incoming;
848 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
849 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
850 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
851 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
852 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
853 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
854 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
855 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
856 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
857 orig_node->orig, neigh_node->addr,
858 if_incoming->net_dev->name);
860 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
861 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
862 orig_node->orig, neigh_node->addr,
863 jiffies_to_msecs(last_seen));
867 hlist_del_rcu(&neigh_node->list);
868 batadv_neigh_node_free_ref(neigh_node);
870 /* only necessary if not the whole neighbor is to be
871 * deleted, but some interface has been removed.
873 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
877 spin_unlock_bh(&orig_node->neigh_list_lock);
882 * batadv_find_best_neighbor - finds the best neighbor after purging
883 * @bat_priv: the bat priv with all the soft interface information
884 * @orig_node: orig node which is to be checked
885 * @if_outgoing: the interface for which the metric should be compared
887 * Returns the current best neighbor, with refcount increased.
889 static struct batadv_neigh_node *
890 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
891 struct batadv_orig_node *orig_node,
892 struct batadv_hard_iface *if_outgoing)
894 struct batadv_neigh_node *best = NULL, *neigh;
895 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
898 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
899 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
900 best, if_outgoing) <= 0))
903 if (!atomic_inc_not_zero(&neigh->refcount))
907 batadv_neigh_node_free_ref(best);
917 * batadv_purge_orig_node - purges obsolete information from an orig_node
918 * @bat_priv: the bat priv with all the soft interface information
919 * @orig_node: orig node which is to be checked
921 * This function checks if the orig_node or substructures of it have become
922 * obsolete, and purges this information if that's the case.
924 * Returns true if the orig_node is to be removed, false otherwise.
926 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
927 struct batadv_orig_node *orig_node)
929 struct batadv_neigh_node *best_neigh_node;
930 struct batadv_hard_iface *hard_iface;
931 bool changed_ifinfo, changed_neigh;
933 if (batadv_has_timed_out(orig_node->last_seen,
934 2 * BATADV_PURGE_TIMEOUT)) {
935 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
936 "Originator timeout: originator %pM, last_seen %u\n",
938 jiffies_to_msecs(orig_node->last_seen));
941 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
942 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
944 if (!changed_ifinfo && !changed_neigh)
947 /* first for NULL ... */
948 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
950 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
953 batadv_neigh_node_free_ref(best_neigh_node);
955 /* ... then for all other interfaces. */
957 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
958 if (hard_iface->if_status != BATADV_IF_ACTIVE)
961 if (hard_iface->soft_iface != bat_priv->soft_iface)
964 best_neigh_node = batadv_find_best_neighbor(bat_priv,
967 batadv_update_route(bat_priv, orig_node, hard_iface,
970 batadv_neigh_node_free_ref(best_neigh_node);
977 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
979 struct batadv_hashtable *hash = bat_priv->orig_hash;
980 struct hlist_node *node_tmp;
981 struct hlist_head *head;
982 spinlock_t *list_lock; /* spinlock to protect write access */
983 struct batadv_orig_node *orig_node;
989 /* for all origins... */
990 for (i = 0; i < hash->size; i++) {
991 head = &hash->table[i];
992 list_lock = &hash->list_locks[i];
994 spin_lock_bh(list_lock);
995 hlist_for_each_entry_safe(orig_node, node_tmp,
997 if (batadv_purge_orig_node(bat_priv, orig_node)) {
998 batadv_gw_node_delete(bat_priv, orig_node);
999 hlist_del_rcu(&orig_node->hash_entry);
1000 batadv_tt_global_del_orig(orig_node->bat_priv,
1002 "originator timed out");
1003 batadv_orig_node_free_ref(orig_node);
1007 batadv_frag_purge_orig(orig_node,
1008 batadv_frag_check_entry);
1010 spin_unlock_bh(list_lock);
1013 batadv_gw_node_purge(bat_priv);
1014 batadv_gw_election(bat_priv);
1017 static void batadv_purge_orig(struct work_struct *work)
1019 struct delayed_work *delayed_work;
1020 struct batadv_priv *bat_priv;
1022 delayed_work = container_of(work, struct delayed_work, work);
1023 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1024 _batadv_purge_orig(bat_priv);
1025 queue_delayed_work(batadv_event_workqueue,
1026 &bat_priv->orig_work,
1027 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1030 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1032 _batadv_purge_orig(bat_priv);
1035 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1037 struct net_device *net_dev = (struct net_device *)seq->private;
1038 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1039 struct batadv_hard_iface *primary_if;
1041 primary_if = batadv_seq_print_text_primary_if_get(seq);
1045 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1046 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1047 primary_if->net_dev->dev_addr, net_dev->name,
1048 bat_priv->bat_algo_ops->name);
1050 batadv_hardif_free_ref(primary_if);
1052 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1054 "No printing function for this routing protocol\n");
1058 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1065 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1066 * outgoing interface
1067 * @seq: debugfs table seq_file struct
1072 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1074 struct net_device *net_dev = (struct net_device *)seq->private;
1075 struct batadv_hard_iface *hard_iface;
1076 struct batadv_priv *bat_priv;
1078 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1080 if (!hard_iface || !hard_iface->soft_iface) {
1081 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1085 bat_priv = netdev_priv(hard_iface->soft_iface);
1086 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1088 "No printing function for this routing protocol\n");
1092 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1093 seq_puts(seq, "Interface not active\n");
1097 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1098 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1099 hard_iface->net_dev->dev_addr,
1100 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1102 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1106 batadv_hardif_free_ref(hard_iface);
1110 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1113 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1114 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1115 struct batadv_hashtable *hash = bat_priv->orig_hash;
1116 struct hlist_head *head;
1117 struct batadv_orig_node *orig_node;
1121 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1124 for (i = 0; i < hash->size; i++) {
1125 head = &hash->table[i];
1128 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1130 if (bao->bat_orig_add_if)
1131 ret = bao->bat_orig_add_if(orig_node,
1146 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1149 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1150 struct batadv_hashtable *hash = bat_priv->orig_hash;
1151 struct hlist_head *head;
1152 struct batadv_hard_iface *hard_iface_tmp;
1153 struct batadv_orig_node *orig_node;
1154 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1158 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1161 for (i = 0; i < hash->size; i++) {
1162 head = &hash->table[i];
1165 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1167 if (bao->bat_orig_del_if)
1168 ret = bao->bat_orig_del_if(orig_node,
1170 hard_iface->if_num);
1177 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1179 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1180 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1183 if (hard_iface == hard_iface_tmp)
1186 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1189 if (hard_iface_tmp->if_num > hard_iface->if_num)
1190 hard_iface_tmp->if_num--;
1194 hard_iface->if_num = -1;