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/>.
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"
30 #include "multicast.h"
33 static struct lock_class_key batadv_orig_hash_lock_class_key;
35 static void batadv_purge_orig(struct work_struct *work);
37 /* returns 1 if they are the same originator */
38 int batadv_compare_orig(const struct hlist_node *node, const void *data2)
40 const void *data1 = container_of(node, struct batadv_orig_node,
43 return batadv_compare_eth(data1, data2);
47 * batadv_orig_node_vlan_get - get an orig_node_vlan object
48 * @orig_node: the originator serving the VLAN
49 * @vid: the VLAN identifier
51 * Returns the vlan object identified by vid and belonging to orig_node or NULL
52 * if it does not exist.
54 struct batadv_orig_node_vlan *
55 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
58 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
61 list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
65 if (!atomic_inc_not_zero(&tmp->refcount))
78 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
80 * @orig_node: the originator serving the VLAN
81 * @vid: the VLAN identifier
83 * Returns NULL in case of failure or the vlan object identified by vid and
84 * belonging to orig_node otherwise. The object is created and added to the list
85 * if it does not exist.
87 * The object is returned with refcounter increased by 1.
89 struct batadv_orig_node_vlan *
90 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
93 struct batadv_orig_node_vlan *vlan;
95 spin_lock_bh(&orig_node->vlan_list_lock);
97 /* first look if an object for this vid already exists */
98 vlan = batadv_orig_node_vlan_get(orig_node, vid);
102 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
106 atomic_set(&vlan->refcount, 2);
109 list_add_rcu(&vlan->list, &orig_node->vlan_list);
112 spin_unlock_bh(&orig_node->vlan_list_lock);
118 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
119 * the originator-vlan object
120 * @orig_vlan: the originator-vlan object to release
122 void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
124 if (atomic_dec_and_test(&orig_vlan->refcount))
125 kfree_rcu(orig_vlan, rcu);
128 int batadv_originator_init(struct batadv_priv *bat_priv)
130 if (bat_priv->orig_hash)
133 bat_priv->orig_hash = batadv_hash_new(1024);
135 if (!bat_priv->orig_hash)
138 batadv_hash_set_lock_class(bat_priv->orig_hash,
139 &batadv_orig_hash_lock_class_key);
141 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
142 queue_delayed_work(batadv_event_workqueue,
143 &bat_priv->orig_work,
144 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
153 * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
154 * @rcu: rcu pointer of the neigh_ifinfo object
156 static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu)
158 struct batadv_neigh_ifinfo *neigh_ifinfo;
160 neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu);
162 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
163 batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing);
169 * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free
170 * the neigh_ifinfo (without rcu callback)
171 * @neigh_ifinfo: the neigh_ifinfo object to release
174 batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo)
176 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
177 batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu);
181 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free
183 * @neigh_ifinfo: the neigh_ifinfo object to release
185 void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
187 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
188 call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu);
192 * batadv_neigh_node_free_rcu - free the neigh_node
193 * @rcu: rcu pointer of the neigh_node
195 static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
197 struct hlist_node *node_tmp;
198 struct batadv_neigh_node *neigh_node;
199 struct batadv_neigh_ifinfo *neigh_ifinfo;
200 struct batadv_algo_ops *bao;
202 neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
203 bao = neigh_node->orig_node->bat_priv->bat_algo_ops;
205 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
206 &neigh_node->ifinfo_list, list) {
207 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo);
210 if (bao->bat_neigh_free)
211 bao->bat_neigh_free(neigh_node);
213 batadv_hardif_free_ref_now(neigh_node->if_incoming);
219 * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter
220 * and possibly free it (without rcu callback)
221 * @neigh_node: neigh neighbor to free
224 batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node)
226 if (atomic_dec_and_test(&neigh_node->refcount))
227 batadv_neigh_node_free_rcu(&neigh_node->rcu);
231 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
232 * and possibly free it
233 * @neigh_node: neigh neighbor to free
235 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
237 if (atomic_dec_and_test(&neigh_node->refcount))
238 call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
242 * batadv_orig_node_get_router - router to the originator depending on iface
243 * @orig_node: the orig node for the router
244 * @if_outgoing: the interface where the payload packet has been received or
245 * the OGM should be sent to
247 * Returns the neighbor which should be router for this orig_node/iface.
249 * The object is returned with refcounter increased by 1.
251 struct batadv_neigh_node *
252 batadv_orig_router_get(struct batadv_orig_node *orig_node,
253 const struct batadv_hard_iface *if_outgoing)
255 struct batadv_orig_ifinfo *orig_ifinfo;
256 struct batadv_neigh_node *router = NULL;
259 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
260 if (orig_ifinfo->if_outgoing != if_outgoing)
263 router = rcu_dereference(orig_ifinfo->router);
267 if (router && !atomic_inc_not_zero(&router->refcount))
275 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
276 * @orig_node: the orig node to be queried
277 * @if_outgoing: the interface for which the ifinfo should be acquired
279 * Returns the requested orig_ifinfo or NULL if not found.
281 * The object is returned with refcounter increased by 1.
283 struct batadv_orig_ifinfo *
284 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
285 struct batadv_hard_iface *if_outgoing)
287 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
290 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
292 if (tmp->if_outgoing != if_outgoing)
295 if (!atomic_inc_not_zero(&tmp->refcount))
307 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
308 * @orig_node: the orig node to be queried
309 * @if_outgoing: the interface for which the ifinfo should be acquired
311 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
312 * interface otherwise. The object is created and added to the list
313 * if it does not exist.
315 * The object is returned with refcounter increased by 1.
317 struct batadv_orig_ifinfo *
318 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
319 struct batadv_hard_iface *if_outgoing)
321 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
322 unsigned long reset_time;
324 spin_lock_bh(&orig_node->neigh_list_lock);
326 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
330 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
334 if (if_outgoing != BATADV_IF_DEFAULT &&
335 !atomic_inc_not_zero(&if_outgoing->refcount)) {
341 reset_time = jiffies - 1;
342 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
343 orig_ifinfo->batman_seqno_reset = reset_time;
344 orig_ifinfo->if_outgoing = if_outgoing;
345 INIT_HLIST_NODE(&orig_ifinfo->list);
346 atomic_set(&orig_ifinfo->refcount, 2);
347 hlist_add_head_rcu(&orig_ifinfo->list,
348 &orig_node->ifinfo_list);
350 spin_unlock_bh(&orig_node->neigh_list_lock);
355 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
356 * @neigh_node: the neigh node to be queried
357 * @if_outgoing: the interface for which the ifinfo should be acquired
359 * The object is returned with refcounter increased by 1.
361 * Returns the requested neigh_ifinfo or NULL if not found
363 struct batadv_neigh_ifinfo *
364 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
365 struct batadv_hard_iface *if_outgoing)
367 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
371 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
373 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
376 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
379 neigh_ifinfo = tmp_neigh_ifinfo;
388 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
389 * @neigh_node: the neigh node to be queried
390 * @if_outgoing: the interface for which the ifinfo should be acquired
392 * Returns NULL in case of failure or the neigh_ifinfo object for the
393 * if_outgoing interface otherwise. The object is created and added to the list
394 * if it does not exist.
396 * The object is returned with refcounter increased by 1.
398 struct batadv_neigh_ifinfo *
399 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
400 struct batadv_hard_iface *if_outgoing)
402 struct batadv_neigh_ifinfo *neigh_ifinfo;
404 spin_lock_bh(&neigh->ifinfo_lock);
406 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
410 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
414 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
420 INIT_HLIST_NODE(&neigh_ifinfo->list);
421 atomic_set(&neigh_ifinfo->refcount, 2);
422 neigh_ifinfo->if_outgoing = if_outgoing;
424 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
427 spin_unlock_bh(&neigh->ifinfo_lock);
433 * batadv_neigh_node_new - create and init a new neigh_node object
434 * @hard_iface: the interface where the neighbour is connected to
435 * @neigh_addr: the mac address of the neighbour interface
436 * @orig_node: originator object representing the neighbour
438 * Allocates a new neigh_node object and initialises all the generic fields.
439 * Returns the new object or NULL on failure.
441 struct batadv_neigh_node *
442 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
443 const uint8_t *neigh_addr,
444 struct batadv_orig_node *orig_node)
446 struct batadv_neigh_node *neigh_node;
448 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
452 INIT_HLIST_NODE(&neigh_node->list);
453 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
454 spin_lock_init(&neigh_node->ifinfo_lock);
456 ether_addr_copy(neigh_node->addr, neigh_addr);
457 neigh_node->if_incoming = hard_iface;
458 neigh_node->orig_node = orig_node;
460 /* extra reference for return */
461 atomic_set(&neigh_node->refcount, 2);
468 * batadv_neigh_node_get - retrieve a neighbour from the list
469 * @orig_node: originator which the neighbour belongs to
470 * @hard_iface: the interface where this neighbour is connected to
471 * @addr: the address of the neighbour
473 * Looks for and possibly returns a neighbour belonging to this originator list
474 * which is connected through the provided hard interface.
475 * Returns NULL if the neighbour is not found.
477 struct batadv_neigh_node *
478 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
479 const struct batadv_hard_iface *hard_iface,
482 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
485 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
486 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
489 if (tmp_neigh_node->if_incoming != hard_iface)
492 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
495 res = tmp_neigh_node;
504 * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
505 * @rcu: rcu pointer of the orig_ifinfo object
507 static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu)
509 struct batadv_orig_ifinfo *orig_ifinfo;
510 struct batadv_neigh_node *router;
512 orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu);
514 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
515 batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing);
517 /* this is the last reference to this object */
518 router = rcu_dereference_protected(orig_ifinfo->router, true);
520 batadv_neigh_node_free_ref_now(router);
525 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
526 * the orig_ifinfo (without rcu callback)
527 * @orig_ifinfo: the orig_ifinfo object to release
530 batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo)
532 if (atomic_dec_and_test(&orig_ifinfo->refcount))
533 batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu);
537 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
539 * @orig_ifinfo: the orig_ifinfo object to release
541 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
543 if (atomic_dec_and_test(&orig_ifinfo->refcount))
544 call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu);
547 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
549 struct hlist_node *node_tmp;
550 struct batadv_neigh_node *neigh_node;
551 struct batadv_orig_node *orig_node;
552 struct batadv_orig_ifinfo *orig_ifinfo;
554 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
556 spin_lock_bh(&orig_node->neigh_list_lock);
558 /* for all neighbors towards this originator ... */
559 hlist_for_each_entry_safe(neigh_node, node_tmp,
560 &orig_node->neigh_list, list) {
561 hlist_del_rcu(&neigh_node->list);
562 batadv_neigh_node_free_ref_now(neigh_node);
565 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
566 &orig_node->ifinfo_list, list) {
567 hlist_del_rcu(&orig_ifinfo->list);
568 batadv_orig_ifinfo_free_ref_now(orig_ifinfo);
570 spin_unlock_bh(&orig_node->neigh_list_lock);
572 batadv_mcast_purge_orig(orig_node);
575 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
577 batadv_frag_purge_orig(orig_node, NULL);
579 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
580 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
582 kfree(orig_node->tt_buff);
587 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
588 * schedule an rcu callback for freeing it
589 * @orig_node: the orig node to free
591 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
593 if (atomic_dec_and_test(&orig_node->refcount))
594 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
598 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
599 * possibly free it (without rcu callback)
600 * @orig_node: the orig node to free
602 void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
604 if (atomic_dec_and_test(&orig_node->refcount))
605 batadv_orig_node_free_rcu(&orig_node->rcu);
608 void batadv_originator_free(struct batadv_priv *bat_priv)
610 struct batadv_hashtable *hash = bat_priv->orig_hash;
611 struct hlist_node *node_tmp;
612 struct hlist_head *head;
613 spinlock_t *list_lock; /* spinlock to protect write access */
614 struct batadv_orig_node *orig_node;
620 cancel_delayed_work_sync(&bat_priv->orig_work);
622 bat_priv->orig_hash = NULL;
624 for (i = 0; i < hash->size; i++) {
625 head = &hash->table[i];
626 list_lock = &hash->list_locks[i];
628 spin_lock_bh(list_lock);
629 hlist_for_each_entry_safe(orig_node, node_tmp,
631 hlist_del_rcu(&orig_node->hash_entry);
632 batadv_orig_node_free_ref(orig_node);
634 spin_unlock_bh(list_lock);
637 batadv_hash_destroy(hash);
641 * batadv_orig_node_new - creates a new orig_node
642 * @bat_priv: the bat priv with all the soft interface information
643 * @addr: the mac address of the originator
645 * Creates a new originator object and initialise all the generic fields.
646 * The new object is not added to the originator list.
647 * Returns the newly created object or NULL on failure.
649 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
652 struct batadv_orig_node *orig_node;
653 struct batadv_orig_node_vlan *vlan;
654 unsigned long reset_time;
657 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
658 "Creating new originator: %pM\n", addr);
660 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
664 INIT_HLIST_HEAD(&orig_node->neigh_list);
665 INIT_LIST_HEAD(&orig_node->vlan_list);
666 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
667 spin_lock_init(&orig_node->bcast_seqno_lock);
668 spin_lock_init(&orig_node->neigh_list_lock);
669 spin_lock_init(&orig_node->tt_buff_lock);
670 spin_lock_init(&orig_node->tt_lock);
671 spin_lock_init(&orig_node->vlan_list_lock);
673 batadv_nc_init_orig(orig_node);
675 /* extra reference for return */
676 atomic_set(&orig_node->refcount, 2);
678 orig_node->bat_priv = bat_priv;
679 ether_addr_copy(orig_node->orig, addr);
680 batadv_dat_init_orig_node_addr(orig_node);
681 atomic_set(&orig_node->last_ttvn, 0);
682 orig_node->tt_buff = NULL;
683 orig_node->tt_buff_len = 0;
684 orig_node->last_seen = jiffies;
685 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
686 orig_node->bcast_seqno_reset = reset_time;
687 #ifdef CONFIG_BATMAN_ADV_MCAST
688 orig_node->mcast_flags = BATADV_NO_FLAGS;
691 /* create a vlan object for the "untagged" LAN */
692 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
695 /* batadv_orig_node_vlan_new() increases the refcounter.
696 * Immediately release vlan since it is not needed anymore in this
699 batadv_orig_node_vlan_free_ref(vlan);
701 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
702 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
703 spin_lock_init(&orig_node->fragments[i].lock);
704 orig_node->fragments[i].size = 0;
714 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
715 * @bat_priv: the bat priv with all the soft interface information
716 * @neigh: orig node which is to be checked
719 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
720 struct batadv_neigh_node *neigh)
722 struct batadv_neigh_ifinfo *neigh_ifinfo;
723 struct batadv_hard_iface *if_outgoing;
724 struct hlist_node *node_tmp;
726 spin_lock_bh(&neigh->ifinfo_lock);
728 /* for all ifinfo objects for this neighinator */
729 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
730 &neigh->ifinfo_list, list) {
731 if_outgoing = neigh_ifinfo->if_outgoing;
733 /* always keep the default interface */
734 if (if_outgoing == BATADV_IF_DEFAULT)
737 /* don't purge if the interface is not (going) down */
738 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
739 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
740 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
743 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
744 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
745 neigh->addr, if_outgoing->net_dev->name);
747 hlist_del_rcu(&neigh_ifinfo->list);
748 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
751 spin_unlock_bh(&neigh->ifinfo_lock);
755 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
756 * @bat_priv: the bat priv with all the soft interface information
757 * @orig_node: orig node which is to be checked
759 * Returns true if any ifinfo entry was purged, false otherwise.
762 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
763 struct batadv_orig_node *orig_node)
765 struct batadv_orig_ifinfo *orig_ifinfo;
766 struct batadv_hard_iface *if_outgoing;
767 struct hlist_node *node_tmp;
768 bool ifinfo_purged = false;
770 spin_lock_bh(&orig_node->neigh_list_lock);
772 /* for all ifinfo objects for this originator */
773 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
774 &orig_node->ifinfo_list, list) {
775 if_outgoing = orig_ifinfo->if_outgoing;
777 /* always keep the default interface */
778 if (if_outgoing == BATADV_IF_DEFAULT)
781 /* don't purge if the interface is not (going) down */
782 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
783 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
784 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
787 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
788 "router/ifinfo purge: originator %pM, iface: %s\n",
789 orig_node->orig, if_outgoing->net_dev->name);
791 ifinfo_purged = true;
793 hlist_del_rcu(&orig_ifinfo->list);
794 batadv_orig_ifinfo_free_ref(orig_ifinfo);
795 if (orig_node->last_bonding_candidate == orig_ifinfo) {
796 orig_node->last_bonding_candidate = NULL;
797 batadv_orig_ifinfo_free_ref(orig_ifinfo);
801 spin_unlock_bh(&orig_node->neigh_list_lock);
803 return ifinfo_purged;
807 * batadv_purge_orig_neighbors - purges neighbors from originator
808 * @bat_priv: the bat priv with all the soft interface information
809 * @orig_node: orig node which is to be checked
811 * Returns true if any neighbor was purged, false otherwise
814 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
815 struct batadv_orig_node *orig_node)
817 struct hlist_node *node_tmp;
818 struct batadv_neigh_node *neigh_node;
819 bool neigh_purged = false;
820 unsigned long last_seen;
821 struct batadv_hard_iface *if_incoming;
823 spin_lock_bh(&orig_node->neigh_list_lock);
825 /* for all neighbors towards this originator ... */
826 hlist_for_each_entry_safe(neigh_node, node_tmp,
827 &orig_node->neigh_list, list) {
828 last_seen = neigh_node->last_seen;
829 if_incoming = neigh_node->if_incoming;
831 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
832 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
833 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
834 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
835 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
836 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
837 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
838 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
839 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
840 orig_node->orig, neigh_node->addr,
841 if_incoming->net_dev->name);
843 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
844 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
845 orig_node->orig, neigh_node->addr,
846 jiffies_to_msecs(last_seen));
850 hlist_del_rcu(&neigh_node->list);
851 batadv_neigh_node_free_ref(neigh_node);
853 /* only necessary if not the whole neighbor is to be
854 * deleted, but some interface has been removed.
856 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
860 spin_unlock_bh(&orig_node->neigh_list_lock);
865 * batadv_find_best_neighbor - finds the best neighbor after purging
866 * @bat_priv: the bat priv with all the soft interface information
867 * @orig_node: orig node which is to be checked
868 * @if_outgoing: the interface for which the metric should be compared
870 * Returns the current best neighbor, with refcount increased.
872 static struct batadv_neigh_node *
873 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
874 struct batadv_orig_node *orig_node,
875 struct batadv_hard_iface *if_outgoing)
877 struct batadv_neigh_node *best = NULL, *neigh;
878 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
881 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
882 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
883 best, if_outgoing) <= 0))
886 if (!atomic_inc_not_zero(&neigh->refcount))
890 batadv_neigh_node_free_ref(best);
900 * batadv_purge_orig_node - purges obsolete information from an orig_node
901 * @bat_priv: the bat priv with all the soft interface information
902 * @orig_node: orig node which is to be checked
904 * This function checks if the orig_node or substructures of it have become
905 * obsolete, and purges this information if that's the case.
907 * Returns true if the orig_node is to be removed, false otherwise.
909 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
910 struct batadv_orig_node *orig_node)
912 struct batadv_neigh_node *best_neigh_node;
913 struct batadv_hard_iface *hard_iface;
914 bool changed_ifinfo, changed_neigh;
916 if (batadv_has_timed_out(orig_node->last_seen,
917 2 * BATADV_PURGE_TIMEOUT)) {
918 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
919 "Originator timeout: originator %pM, last_seen %u\n",
921 jiffies_to_msecs(orig_node->last_seen));
924 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
925 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
927 if (!changed_ifinfo && !changed_neigh)
930 /* first for NULL ... */
931 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
933 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
936 batadv_neigh_node_free_ref(best_neigh_node);
938 /* ... then for all other interfaces. */
940 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
941 if (hard_iface->if_status != BATADV_IF_ACTIVE)
944 if (hard_iface->soft_iface != bat_priv->soft_iface)
947 best_neigh_node = batadv_find_best_neighbor(bat_priv,
950 batadv_update_route(bat_priv, orig_node, hard_iface,
953 batadv_neigh_node_free_ref(best_neigh_node);
960 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
962 struct batadv_hashtable *hash = bat_priv->orig_hash;
963 struct hlist_node *node_tmp;
964 struct hlist_head *head;
965 spinlock_t *list_lock; /* spinlock to protect write access */
966 struct batadv_orig_node *orig_node;
972 /* for all origins... */
973 for (i = 0; i < hash->size; i++) {
974 head = &hash->table[i];
975 list_lock = &hash->list_locks[i];
977 spin_lock_bh(list_lock);
978 hlist_for_each_entry_safe(orig_node, node_tmp,
980 if (batadv_purge_orig_node(bat_priv, orig_node)) {
981 batadv_gw_node_delete(bat_priv, orig_node);
982 hlist_del_rcu(&orig_node->hash_entry);
983 batadv_tt_global_del_orig(orig_node->bat_priv,
985 "originator timed out");
986 batadv_orig_node_free_ref(orig_node);
990 batadv_frag_purge_orig(orig_node,
991 batadv_frag_check_entry);
993 spin_unlock_bh(list_lock);
996 batadv_gw_node_purge(bat_priv);
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;