1 /* Copyright (C) 2009-2014 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/>.
19 #include "distributed-arp-table.h"
20 #include "originator.h"
22 #include "translation-table.h"
24 #include "gateway_client.h"
25 #include "hard-interface.h"
26 #include "soft-interface.h"
27 #include "bridge_loop_avoidance.h"
28 #include "network-coding.h"
29 #include "fragmentation.h"
32 static struct lock_class_key batadv_orig_hash_lock_class_key;
34 static void batadv_purge_orig(struct work_struct *work);
36 /* returns 1 if they are the same originator */
37 int batadv_compare_orig(const struct hlist_node *node, const void *data2)
39 const void *data1 = container_of(node, struct batadv_orig_node,
42 return batadv_compare_eth(data1, data2);
46 * batadv_orig_node_vlan_get - get an orig_node_vlan object
47 * @orig_node: the originator serving the VLAN
48 * @vid: the VLAN identifier
50 * Returns the vlan object identified by vid and belonging to orig_node or NULL
51 * if it does not exist.
53 struct batadv_orig_node_vlan *
54 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
57 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
60 list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
64 if (!atomic_inc_not_zero(&tmp->refcount))
77 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
79 * @orig_node: the originator serving the VLAN
80 * @vid: the VLAN identifier
82 * Returns NULL in case of failure or the vlan object identified by vid and
83 * belonging to orig_node otherwise. The object is created and added to the list
84 * if it does not exist.
86 * The object is returned with refcounter increased by 1.
88 struct batadv_orig_node_vlan *
89 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
92 struct batadv_orig_node_vlan *vlan;
94 spin_lock_bh(&orig_node->vlan_list_lock);
96 /* first look if an object for this vid already exists */
97 vlan = batadv_orig_node_vlan_get(orig_node, vid);
101 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
105 atomic_set(&vlan->refcount, 2);
108 list_add_rcu(&vlan->list, &orig_node->vlan_list);
111 spin_unlock_bh(&orig_node->vlan_list_lock);
117 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
118 * the originator-vlan object
119 * @orig_vlan: the originator-vlan object to release
121 void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
123 if (atomic_dec_and_test(&orig_vlan->refcount))
124 kfree_rcu(orig_vlan, rcu);
127 int batadv_originator_init(struct batadv_priv *bat_priv)
129 if (bat_priv->orig_hash)
132 bat_priv->orig_hash = batadv_hash_new(1024);
134 if (!bat_priv->orig_hash)
137 batadv_hash_set_lock_class(bat_priv->orig_hash,
138 &batadv_orig_hash_lock_class_key);
140 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
141 queue_delayed_work(batadv_event_workqueue,
142 &bat_priv->orig_work,
143 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
152 * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
153 * @rcu: rcu pointer of the neigh_ifinfo object
155 static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu)
157 struct batadv_neigh_ifinfo *neigh_ifinfo;
159 neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu);
161 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
162 batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing);
168 * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free
169 * the neigh_ifinfo (without rcu callback)
170 * @neigh_ifinfo: the neigh_ifinfo object to release
173 batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo)
175 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
176 batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu);
180 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free
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 call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu);
191 * batadv_neigh_node_free_rcu - free the neigh_node
192 * @rcu: rcu pointer of the neigh_node
194 static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
196 struct hlist_node *node_tmp;
197 struct batadv_neigh_node *neigh_node;
198 struct batadv_neigh_ifinfo *neigh_ifinfo;
200 neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
202 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
203 &neigh_node->ifinfo_list, list) {
204 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo);
206 batadv_hardif_free_ref_now(neigh_node->if_incoming);
212 * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter
213 * and possibly free it (without rcu callback)
214 * @neigh_node: neigh neighbor to free
217 batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node)
219 if (atomic_dec_and_test(&neigh_node->refcount))
220 batadv_neigh_node_free_rcu(&neigh_node->rcu);
224 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
225 * and possibly free it
226 * @neigh_node: neigh neighbor to free
228 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
230 if (atomic_dec_and_test(&neigh_node->refcount))
231 call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
235 * batadv_orig_node_get_router - router to the originator depending on iface
236 * @orig_node: the orig node for the router
237 * @if_outgoing: the interface where the payload packet has been received or
238 * the OGM should be sent to
240 * Returns the neighbor which should be router for this orig_node/iface.
242 * The object is returned with refcounter increased by 1.
244 struct batadv_neigh_node *
245 batadv_orig_router_get(struct batadv_orig_node *orig_node,
246 const struct batadv_hard_iface *if_outgoing)
248 struct batadv_orig_ifinfo *orig_ifinfo;
249 struct batadv_neigh_node *router = NULL;
252 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
253 if (orig_ifinfo->if_outgoing != if_outgoing)
256 router = rcu_dereference(orig_ifinfo->router);
260 if (router && !atomic_inc_not_zero(&router->refcount))
268 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
269 * @orig_node: the orig node to be queried
270 * @if_outgoing: the interface for which the ifinfo should be acquired
272 * Returns the requested orig_ifinfo or NULL if not found.
274 * The object is returned with refcounter increased by 1.
276 struct batadv_orig_ifinfo *
277 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
278 struct batadv_hard_iface *if_outgoing)
280 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
283 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
285 if (tmp->if_outgoing != if_outgoing)
288 if (!atomic_inc_not_zero(&tmp->refcount))
300 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
301 * @orig_node: the orig node to be queried
302 * @if_outgoing: the interface for which the ifinfo should be acquired
304 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
305 * interface otherwise. The object is created and added to the list
306 * if it does not exist.
308 * The object is returned with refcounter increased by 1.
310 struct batadv_orig_ifinfo *
311 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
312 struct batadv_hard_iface *if_outgoing)
314 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
315 unsigned long reset_time;
317 spin_lock_bh(&orig_node->neigh_list_lock);
319 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
323 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
327 if (if_outgoing != BATADV_IF_DEFAULT &&
328 !atomic_inc_not_zero(&if_outgoing->refcount)) {
334 reset_time = jiffies - 1;
335 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
336 orig_ifinfo->batman_seqno_reset = reset_time;
337 orig_ifinfo->if_outgoing = if_outgoing;
338 INIT_HLIST_NODE(&orig_ifinfo->list);
339 atomic_set(&orig_ifinfo->refcount, 2);
340 hlist_add_head_rcu(&orig_ifinfo->list,
341 &orig_node->ifinfo_list);
343 spin_unlock_bh(&orig_node->neigh_list_lock);
348 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
349 * @neigh_node: the neigh node to be queried
350 * @if_outgoing: the interface for which the ifinfo should be acquired
352 * The object is returned with refcounter increased by 1.
354 * Returns the requested neigh_ifinfo or NULL if not found
356 struct batadv_neigh_ifinfo *
357 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
358 struct batadv_hard_iface *if_outgoing)
360 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
364 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
366 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
369 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
372 neigh_ifinfo = tmp_neigh_ifinfo;
381 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
382 * @neigh_node: the neigh node to be queried
383 * @if_outgoing: the interface for which the ifinfo should be acquired
385 * Returns NULL in case of failure or the neigh_ifinfo object for the
386 * if_outgoing interface otherwise. The object is created and added to the list
387 * if it does not exist.
389 * The object is returned with refcounter increased by 1.
391 struct batadv_neigh_ifinfo *
392 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
393 struct batadv_hard_iface *if_outgoing)
395 struct batadv_neigh_ifinfo *neigh_ifinfo;
397 spin_lock_bh(&neigh->ifinfo_lock);
399 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
403 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
407 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
413 INIT_HLIST_NODE(&neigh_ifinfo->list);
414 atomic_set(&neigh_ifinfo->refcount, 2);
415 neigh_ifinfo->if_outgoing = if_outgoing;
417 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
420 spin_unlock_bh(&neigh->ifinfo_lock);
426 * batadv_neigh_node_new - create and init a new neigh_node object
427 * @hard_iface: the interface where the neighbour is connected to
428 * @neigh_addr: the mac address of the neighbour interface
429 * @orig_node: originator object representing the neighbour
431 * Allocates a new neigh_node object and initialises all the generic fields.
432 * Returns the new object or NULL on failure.
434 struct batadv_neigh_node *
435 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
436 const uint8_t *neigh_addr,
437 struct batadv_orig_node *orig_node)
439 struct batadv_neigh_node *neigh_node;
441 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
445 INIT_HLIST_NODE(&neigh_node->list);
446 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
447 spin_lock_init(&neigh_node->ifinfo_lock);
449 memcpy(neigh_node->addr, neigh_addr, ETH_ALEN);
450 neigh_node->if_incoming = hard_iface;
451 neigh_node->orig_node = orig_node;
453 /* extra reference for return */
454 atomic_set(&neigh_node->refcount, 2);
461 * batadv_neigh_node_get - retrieve a neighbour from the list
462 * @orig_node: originator which the neighbour belongs to
463 * @hard_iface: the interface where this neighbour is connected to
464 * @addr: the address of the neighbour
466 * Looks for and possibly returns a neighbour belonging to this originator list
467 * which is connected through the provided hard interface.
468 * Returns NULL if the neighbour is not found.
470 struct batadv_neigh_node *
471 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
472 const struct batadv_hard_iface *hard_iface,
475 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
478 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
479 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
482 if (tmp_neigh_node->if_incoming != hard_iface)
485 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
488 res = tmp_neigh_node;
497 * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
498 * @rcu: rcu pointer of the orig_ifinfo object
500 static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu)
502 struct batadv_orig_ifinfo *orig_ifinfo;
503 struct batadv_neigh_node *router;
505 orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu);
507 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
508 batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing);
510 /* this is the last reference to this object */
511 router = rcu_dereference_protected(orig_ifinfo->router, true);
513 batadv_neigh_node_free_ref_now(router);
518 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
519 * the orig_ifinfo (without rcu callback)
520 * @orig_ifinfo: the orig_ifinfo object to release
523 batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo)
525 if (atomic_dec_and_test(&orig_ifinfo->refcount))
526 batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu);
530 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
532 * @orig_ifinfo: the orig_ifinfo object to release
534 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
536 if (atomic_dec_and_test(&orig_ifinfo->refcount))
537 call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu);
540 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
542 struct hlist_node *node_tmp;
543 struct batadv_neigh_node *neigh_node;
544 struct batadv_orig_node *orig_node;
545 struct batadv_orig_ifinfo *orig_ifinfo;
547 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
549 spin_lock_bh(&orig_node->neigh_list_lock);
551 /* for all neighbors towards this originator ... */
552 hlist_for_each_entry_safe(neigh_node, node_tmp,
553 &orig_node->neigh_list, list) {
554 hlist_del_rcu(&neigh_node->list);
555 batadv_neigh_node_free_ref_now(neigh_node);
558 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
559 &orig_node->ifinfo_list, list) {
560 hlist_del_rcu(&orig_ifinfo->list);
561 batadv_orig_ifinfo_free_ref_now(orig_ifinfo);
563 spin_unlock_bh(&orig_node->neigh_list_lock);
566 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
568 batadv_frag_purge_orig(orig_node, NULL);
570 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, -1,
571 "originator timed out");
573 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
574 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
576 kfree(orig_node->tt_buff);
581 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
582 * schedule an rcu callback for freeing it
583 * @orig_node: the orig node to free
585 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
587 if (atomic_dec_and_test(&orig_node->refcount))
588 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
592 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
593 * possibly free it (without rcu callback)
594 * @orig_node: the orig node to free
596 void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
598 if (atomic_dec_and_test(&orig_node->refcount))
599 batadv_orig_node_free_rcu(&orig_node->rcu);
602 void batadv_originator_free(struct batadv_priv *bat_priv)
604 struct batadv_hashtable *hash = bat_priv->orig_hash;
605 struct hlist_node *node_tmp;
606 struct hlist_head *head;
607 spinlock_t *list_lock; /* spinlock to protect write access */
608 struct batadv_orig_node *orig_node;
614 cancel_delayed_work_sync(&bat_priv->orig_work);
616 bat_priv->orig_hash = NULL;
618 for (i = 0; i < hash->size; i++) {
619 head = &hash->table[i];
620 list_lock = &hash->list_locks[i];
622 spin_lock_bh(list_lock);
623 hlist_for_each_entry_safe(orig_node, node_tmp,
625 hlist_del_rcu(&orig_node->hash_entry);
626 batadv_orig_node_free_ref(orig_node);
628 spin_unlock_bh(list_lock);
631 batadv_hash_destroy(hash);
635 * batadv_orig_node_new - creates a new orig_node
636 * @bat_priv: the bat priv with all the soft interface information
637 * @addr: the mac address of the originator
639 * Creates a new originator object and initialise all the generic fields.
640 * The new object is not added to the originator list.
641 * Returns the newly created object or NULL on failure.
643 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
646 struct batadv_orig_node *orig_node;
647 struct batadv_orig_node_vlan *vlan;
648 unsigned long reset_time;
651 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
652 "Creating new originator: %pM\n", addr);
654 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
658 INIT_HLIST_HEAD(&orig_node->neigh_list);
659 INIT_LIST_HEAD(&orig_node->vlan_list);
660 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
661 spin_lock_init(&orig_node->bcast_seqno_lock);
662 spin_lock_init(&orig_node->neigh_list_lock);
663 spin_lock_init(&orig_node->tt_buff_lock);
664 spin_lock_init(&orig_node->tt_lock);
665 spin_lock_init(&orig_node->vlan_list_lock);
667 batadv_nc_init_orig(orig_node);
669 /* extra reference for return */
670 atomic_set(&orig_node->refcount, 2);
672 orig_node->tt_initialised = false;
673 orig_node->bat_priv = bat_priv;
674 memcpy(orig_node->orig, addr, ETH_ALEN);
675 batadv_dat_init_orig_node_addr(orig_node);
676 atomic_set(&orig_node->last_ttvn, 0);
677 orig_node->tt_buff = NULL;
678 orig_node->tt_buff_len = 0;
679 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
680 orig_node->bcast_seqno_reset = reset_time;
682 /* create a vlan object for the "untagged" LAN */
683 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
686 /* batadv_orig_node_vlan_new() increases the refcounter.
687 * Immediately release vlan since it is not needed anymore in this
690 batadv_orig_node_vlan_free_ref(vlan);
692 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
693 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
694 spin_lock_init(&orig_node->fragments[i].lock);
695 orig_node->fragments[i].size = 0;
705 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
706 * @bat_priv: the bat priv with all the soft interface information
707 * @neigh: orig node which is to be checked
710 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
711 struct batadv_neigh_node *neigh)
713 struct batadv_neigh_ifinfo *neigh_ifinfo;
714 struct batadv_hard_iface *if_outgoing;
715 struct hlist_node *node_tmp;
717 spin_lock_bh(&neigh->ifinfo_lock);
719 /* for all ifinfo objects for this neighinator */
720 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
721 &neigh->ifinfo_list, list) {
722 if_outgoing = neigh_ifinfo->if_outgoing;
724 /* always keep the default interface */
725 if (if_outgoing == BATADV_IF_DEFAULT)
728 /* don't purge if the interface is not (going) down */
729 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
730 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
731 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
734 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
735 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
736 neigh->addr, if_outgoing->net_dev->name);
738 hlist_del_rcu(&neigh_ifinfo->list);
739 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
742 spin_unlock_bh(&neigh->ifinfo_lock);
746 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
747 * @bat_priv: the bat priv with all the soft interface information
748 * @orig_node: orig node which is to be checked
750 * Returns true if any ifinfo entry was purged, false otherwise.
753 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
754 struct batadv_orig_node *orig_node)
756 struct batadv_orig_ifinfo *orig_ifinfo;
757 struct batadv_hard_iface *if_outgoing;
758 struct hlist_node *node_tmp;
759 bool ifinfo_purged = false;
761 spin_lock_bh(&orig_node->neigh_list_lock);
763 /* for all ifinfo objects for this originator */
764 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
765 &orig_node->ifinfo_list, list) {
766 if_outgoing = orig_ifinfo->if_outgoing;
768 /* always keep the default interface */
769 if (if_outgoing == BATADV_IF_DEFAULT)
772 /* don't purge if the interface is not (going) down */
773 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
774 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
775 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
778 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
779 "router/ifinfo purge: originator %pM, iface: %s\n",
780 orig_node->orig, if_outgoing->net_dev->name);
782 ifinfo_purged = true;
784 hlist_del_rcu(&orig_ifinfo->list);
785 batadv_orig_ifinfo_free_ref(orig_ifinfo);
786 if (orig_node->last_bonding_candidate == orig_ifinfo) {
787 orig_node->last_bonding_candidate = NULL;
788 batadv_orig_ifinfo_free_ref(orig_ifinfo);
792 spin_unlock_bh(&orig_node->neigh_list_lock);
794 return ifinfo_purged;
799 * batadv_purge_orig_neighbors - purges neighbors from originator
800 * @bat_priv: the bat priv with all the soft interface information
801 * @orig_node: orig node which is to be checked
803 * Returns true if any neighbor was purged, false otherwise
806 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
807 struct batadv_orig_node *orig_node)
809 struct hlist_node *node_tmp;
810 struct batadv_neigh_node *neigh_node;
811 bool neigh_purged = false;
812 unsigned long last_seen;
813 struct batadv_hard_iface *if_incoming;
815 spin_lock_bh(&orig_node->neigh_list_lock);
817 /* for all neighbors towards this originator ... */
818 hlist_for_each_entry_safe(neigh_node, node_tmp,
819 &orig_node->neigh_list, list) {
820 last_seen = neigh_node->last_seen;
821 if_incoming = neigh_node->if_incoming;
823 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
824 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
825 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
826 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
827 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
828 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
829 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
830 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
831 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
832 orig_node->orig, neigh_node->addr,
833 if_incoming->net_dev->name);
835 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
836 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
837 orig_node->orig, neigh_node->addr,
838 jiffies_to_msecs(last_seen));
842 hlist_del_rcu(&neigh_node->list);
843 batadv_neigh_node_free_ref(neigh_node);
845 /* only necessary if not the whole neighbor is to be
846 * deleted, but some interface has been removed.
848 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
852 spin_unlock_bh(&orig_node->neigh_list_lock);
857 * batadv_find_best_neighbor - finds the best neighbor after purging
858 * @bat_priv: the bat priv with all the soft interface information
859 * @orig_node: orig node which is to be checked
860 * @if_outgoing: the interface for which the metric should be compared
862 * Returns the current best neighbor, with refcount increased.
864 static struct batadv_neigh_node *
865 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
866 struct batadv_orig_node *orig_node,
867 struct batadv_hard_iface *if_outgoing)
869 struct batadv_neigh_node *best = NULL, *neigh;
870 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
873 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
874 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
875 best, if_outgoing) <= 0))
878 if (!atomic_inc_not_zero(&neigh->refcount))
882 batadv_neigh_node_free_ref(best);
892 * batadv_purge_orig_node - purges obsolete information from an orig_node
893 * @bat_priv: the bat priv with all the soft interface information
894 * @orig_node: orig node which is to be checked
896 * This function checks if the orig_node or substructures of it have become
897 * obsolete, and purges this information if that's the case.
899 * Returns true if the orig_node is to be removed, false otherwise.
901 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
902 struct batadv_orig_node *orig_node)
904 struct batadv_neigh_node *best_neigh_node;
905 struct batadv_hard_iface *hard_iface;
906 bool changed_ifinfo, changed_neigh;
908 if (batadv_has_timed_out(orig_node->last_seen,
909 2 * BATADV_PURGE_TIMEOUT)) {
910 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
911 "Originator timeout: originator %pM, last_seen %u\n",
913 jiffies_to_msecs(orig_node->last_seen));
916 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
917 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
919 if (!changed_ifinfo && !changed_neigh)
922 /* first for NULL ... */
923 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
925 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
928 batadv_neigh_node_free_ref(best_neigh_node);
930 /* ... then for all other interfaces. */
932 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
933 if (hard_iface->if_status != BATADV_IF_ACTIVE)
936 if (hard_iface->soft_iface != bat_priv->soft_iface)
939 best_neigh_node = batadv_find_best_neighbor(bat_priv,
942 batadv_update_route(bat_priv, orig_node, hard_iface,
945 batadv_neigh_node_free_ref(best_neigh_node);
952 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
954 struct batadv_hashtable *hash = bat_priv->orig_hash;
955 struct hlist_node *node_tmp;
956 struct hlist_head *head;
957 spinlock_t *list_lock; /* spinlock to protect write access */
958 struct batadv_orig_node *orig_node;
964 /* for all origins... */
965 for (i = 0; i < hash->size; i++) {
966 head = &hash->table[i];
967 list_lock = &hash->list_locks[i];
969 spin_lock_bh(list_lock);
970 hlist_for_each_entry_safe(orig_node, node_tmp,
972 if (batadv_purge_orig_node(bat_priv, orig_node)) {
973 batadv_gw_node_delete(bat_priv, orig_node);
974 hlist_del_rcu(&orig_node->hash_entry);
975 batadv_orig_node_free_ref(orig_node);
979 batadv_frag_purge_orig(orig_node,
980 batadv_frag_check_entry);
982 spin_unlock_bh(list_lock);
985 batadv_gw_node_purge(bat_priv);
986 batadv_gw_election(bat_priv);
989 static void batadv_purge_orig(struct work_struct *work)
991 struct delayed_work *delayed_work;
992 struct batadv_priv *bat_priv;
994 delayed_work = container_of(work, struct delayed_work, work);
995 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
996 _batadv_purge_orig(bat_priv);
997 queue_delayed_work(batadv_event_workqueue,
998 &bat_priv->orig_work,
999 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1002 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1004 _batadv_purge_orig(bat_priv);
1007 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1009 struct net_device *net_dev = (struct net_device *)seq->private;
1010 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1011 struct batadv_hard_iface *primary_if;
1013 primary_if = batadv_seq_print_text_primary_if_get(seq);
1017 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1018 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1019 primary_if->net_dev->dev_addr, net_dev->name,
1020 bat_priv->bat_algo_ops->name);
1022 batadv_hardif_free_ref(primary_if);
1024 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1026 "No printing function for this routing protocol\n");
1030 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1037 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1038 * outgoing interface
1039 * @seq: debugfs table seq_file struct
1044 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1046 struct net_device *net_dev = (struct net_device *)seq->private;
1047 struct batadv_hard_iface *hard_iface;
1048 struct batadv_priv *bat_priv;
1050 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1052 if (!hard_iface || !hard_iface->soft_iface) {
1053 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1057 bat_priv = netdev_priv(hard_iface->soft_iface);
1058 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1060 "No printing function for this routing protocol\n");
1064 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1065 seq_puts(seq, "Interface not active\n");
1069 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1070 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1071 hard_iface->net_dev->dev_addr,
1072 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1074 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1078 batadv_hardif_free_ref(hard_iface);
1082 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1085 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1086 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1087 struct batadv_hashtable *hash = bat_priv->orig_hash;
1088 struct hlist_head *head;
1089 struct batadv_orig_node *orig_node;
1093 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1096 for (i = 0; i < hash->size; i++) {
1097 head = &hash->table[i];
1100 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1102 if (bao->bat_orig_add_if)
1103 ret = bao->bat_orig_add_if(orig_node,
1118 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1121 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1122 struct batadv_hashtable *hash = bat_priv->orig_hash;
1123 struct hlist_head *head;
1124 struct batadv_hard_iface *hard_iface_tmp;
1125 struct batadv_orig_node *orig_node;
1126 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1130 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1133 for (i = 0; i < hash->size; i++) {
1134 head = &hash->table[i];
1137 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1139 if (bao->bat_orig_del_if)
1140 ret = bao->bat_orig_del_if(orig_node,
1142 hard_iface->if_num);
1149 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1151 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1152 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1155 if (hard_iface == hard_iface_tmp)
1158 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1161 if (hard_iface_tmp->if_num > hard_iface->if_num)
1162 hard_iface_tmp->if_num--;
1166 hard_iface->if_num = -1;