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);
51 * batadv_compare_orig - comparing function used in the originator hash table
52 * @node: node in the local table
53 * @data2: second object to compare the node to
55 * Return: 1 if they are the same originator
57 int batadv_compare_orig(const struct hlist_node *node, const void *data2)
59 const void *data1 = container_of(node, struct batadv_orig_node,
62 return batadv_compare_eth(data1, data2);
66 * batadv_orig_node_vlan_get - get an orig_node_vlan object
67 * @orig_node: the originator serving the VLAN
68 * @vid: the VLAN identifier
70 * Return: the vlan object identified by vid and belonging to orig_node or NULL
71 * if it does not exist.
73 struct batadv_orig_node_vlan *
74 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
77 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
80 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
84 if (!atomic_inc_not_zero(&tmp->refcount))
97 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
99 * @orig_node: the originator serving the VLAN
100 * @vid: the VLAN identifier
102 * Return: NULL in case of failure or the vlan object identified by vid and
103 * belonging to orig_node otherwise. The object is created and added to the list
104 * if it does not exist.
106 * The object is returned with refcounter increased by 1.
108 struct batadv_orig_node_vlan *
109 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
112 struct batadv_orig_node_vlan *vlan;
114 spin_lock_bh(&orig_node->vlan_list_lock);
116 /* first look if an object for this vid already exists */
117 vlan = batadv_orig_node_vlan_get(orig_node, vid);
121 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
125 atomic_set(&vlan->refcount, 2);
128 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
131 spin_unlock_bh(&orig_node->vlan_list_lock);
137 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
138 * the originator-vlan object
139 * @orig_vlan: the originator-vlan object to release
141 void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
143 if (atomic_dec_and_test(&orig_vlan->refcount))
144 kfree_rcu(orig_vlan, rcu);
147 int batadv_originator_init(struct batadv_priv *bat_priv)
149 if (bat_priv->orig_hash)
152 bat_priv->orig_hash = batadv_hash_new(1024);
154 if (!bat_priv->orig_hash)
157 batadv_hash_set_lock_class(bat_priv->orig_hash,
158 &batadv_orig_hash_lock_class_key);
160 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
161 queue_delayed_work(batadv_event_workqueue,
162 &bat_priv->orig_work,
163 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
172 * batadv_neigh_ifinfo_release - release neigh_ifinfo from lists and queue for
173 * free after rcu grace period
174 * @neigh_ifinfo: the neigh_ifinfo object to release
177 batadv_neigh_ifinfo_release(struct batadv_neigh_ifinfo *neigh_ifinfo)
179 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
180 batadv_hardif_free_ref(neigh_ifinfo->if_outgoing);
182 kfree_rcu(neigh_ifinfo, rcu);
186 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly release
188 * @neigh_ifinfo: the neigh_ifinfo object to release
190 void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
192 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
193 batadv_neigh_ifinfo_release(neigh_ifinfo);
197 * batadv_hardif_neigh_release - release hardif neigh node from lists and
198 * queue for free after rcu grace period
199 * @hardif_neigh: hardif neigh neighbor to free
202 batadv_hardif_neigh_release(struct batadv_hardif_neigh_node *hardif_neigh)
204 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
205 hlist_del_init_rcu(&hardif_neigh->list);
206 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
208 batadv_hardif_free_ref(hardif_neigh->if_incoming);
209 kfree_rcu(hardif_neigh, rcu);
213 * batadv_hardif_neigh_free_ref - decrement the hardif neighbors refcounter
214 * and possibly release it
215 * @hardif_neigh: hardif neigh neighbor to free
217 void batadv_hardif_neigh_free_ref(struct batadv_hardif_neigh_node *hardif_neigh)
219 if (atomic_dec_and_test(&hardif_neigh->refcount))
220 batadv_hardif_neigh_release(hardif_neigh);
224 * batadv_neigh_node_release - release neigh_node from lists and queue for
225 * free after rcu grace period
226 * @neigh_node: neigh neighbor to free
228 static void batadv_neigh_node_release(struct batadv_neigh_node *neigh_node)
230 struct hlist_node *node_tmp;
231 struct batadv_hardif_neigh_node *hardif_neigh;
232 struct batadv_neigh_ifinfo *neigh_ifinfo;
233 struct batadv_algo_ops *bao;
235 bao = neigh_node->orig_node->bat_priv->bat_algo_ops;
237 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
238 &neigh_node->ifinfo_list, list) {
239 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
242 hardif_neigh = batadv_hardif_neigh_get(neigh_node->if_incoming,
245 /* batadv_hardif_neigh_get() increases refcount too */
246 batadv_hardif_neigh_free_ref(hardif_neigh);
247 batadv_hardif_neigh_free_ref(hardif_neigh);
250 if (bao->bat_neigh_free)
251 bao->bat_neigh_free(neigh_node);
253 batadv_hardif_free_ref(neigh_node->if_incoming);
255 kfree_rcu(neigh_node, rcu);
259 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
260 * and possibly release it
261 * @neigh_node: neigh neighbor to free
263 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
265 if (atomic_dec_and_test(&neigh_node->refcount))
266 batadv_neigh_node_release(neigh_node);
270 * batadv_orig_node_get_router - router to the originator depending on iface
271 * @orig_node: the orig node for the router
272 * @if_outgoing: the interface where the payload packet has been received or
273 * the OGM should be sent to
275 * Return: the neighbor which should be router for this orig_node/iface.
277 * The object is returned with refcounter increased by 1.
279 struct batadv_neigh_node *
280 batadv_orig_router_get(struct batadv_orig_node *orig_node,
281 const struct batadv_hard_iface *if_outgoing)
283 struct batadv_orig_ifinfo *orig_ifinfo;
284 struct batadv_neigh_node *router = NULL;
287 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
288 if (orig_ifinfo->if_outgoing != if_outgoing)
291 router = rcu_dereference(orig_ifinfo->router);
295 if (router && !atomic_inc_not_zero(&router->refcount))
303 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
304 * @orig_node: the orig node to be queried
305 * @if_outgoing: the interface for which the ifinfo should be acquired
307 * Return: the requested orig_ifinfo or NULL if not found.
309 * The object is returned with refcounter increased by 1.
311 struct batadv_orig_ifinfo *
312 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
313 struct batadv_hard_iface *if_outgoing)
315 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
318 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
320 if (tmp->if_outgoing != if_outgoing)
323 if (!atomic_inc_not_zero(&tmp->refcount))
335 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
336 * @orig_node: the orig node to be queried
337 * @if_outgoing: the interface for which the ifinfo should be acquired
339 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
340 * interface otherwise. The object is created and added to the list
341 * if it does not exist.
343 * The object is returned with refcounter increased by 1.
345 struct batadv_orig_ifinfo *
346 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
347 struct batadv_hard_iface *if_outgoing)
349 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
350 unsigned long reset_time;
352 spin_lock_bh(&orig_node->neigh_list_lock);
354 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
358 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
362 if (if_outgoing != BATADV_IF_DEFAULT &&
363 !atomic_inc_not_zero(&if_outgoing->refcount)) {
369 reset_time = jiffies - 1;
370 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
371 orig_ifinfo->batman_seqno_reset = reset_time;
372 orig_ifinfo->if_outgoing = if_outgoing;
373 INIT_HLIST_NODE(&orig_ifinfo->list);
374 atomic_set(&orig_ifinfo->refcount, 2);
375 hlist_add_head_rcu(&orig_ifinfo->list,
376 &orig_node->ifinfo_list);
378 spin_unlock_bh(&orig_node->neigh_list_lock);
383 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
384 * @neigh: the neigh node to be queried
385 * @if_outgoing: the interface for which the ifinfo should be acquired
387 * The object is returned with refcounter increased by 1.
389 * Return: the requested neigh_ifinfo or NULL if not found
391 struct batadv_neigh_ifinfo *
392 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
393 struct batadv_hard_iface *if_outgoing)
395 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
399 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
401 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
404 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
407 neigh_ifinfo = tmp_neigh_ifinfo;
416 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
417 * @neigh: the neigh node to be queried
418 * @if_outgoing: the interface for which the ifinfo should be acquired
420 * Return: NULL in case of failure or the neigh_ifinfo object for the
421 * if_outgoing interface otherwise. The object is created and added to the list
422 * if it does not exist.
424 * The object is returned with refcounter increased by 1.
426 struct batadv_neigh_ifinfo *
427 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
428 struct batadv_hard_iface *if_outgoing)
430 struct batadv_neigh_ifinfo *neigh_ifinfo;
432 spin_lock_bh(&neigh->ifinfo_lock);
434 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
438 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
442 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
448 INIT_HLIST_NODE(&neigh_ifinfo->list);
449 atomic_set(&neigh_ifinfo->refcount, 2);
450 neigh_ifinfo->if_outgoing = if_outgoing;
452 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
455 spin_unlock_bh(&neigh->ifinfo_lock);
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.
469 * Return: neighbor when found. Othwerwise NULL
471 static struct batadv_neigh_node *
472 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
473 const struct batadv_hard_iface *hard_iface,
476 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
479 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
480 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
483 if (tmp_neigh_node->if_incoming != hard_iface)
486 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
489 res = tmp_neigh_node;
498 * batadv_hardif_neigh_create - create a hardif neighbour node
499 * @hard_iface: the interface this neighbour is connected to
500 * @neigh_addr: the interface address of the neighbour to retrieve
502 * Return: the hardif neighbour node if found or created or NULL otherwise.
504 static struct batadv_hardif_neigh_node *
505 batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
506 const u8 *neigh_addr)
508 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
509 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
511 spin_lock_bh(&hard_iface->neigh_list_lock);
513 /* check if neighbor hasn't been added in the meantime */
514 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
518 if (!atomic_inc_not_zero(&hard_iface->refcount))
521 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
523 batadv_hardif_free_ref(hard_iface);
527 INIT_HLIST_NODE(&hardif_neigh->list);
528 ether_addr_copy(hardif_neigh->addr, neigh_addr);
529 hardif_neigh->if_incoming = hard_iface;
530 hardif_neigh->last_seen = jiffies;
532 atomic_set(&hardif_neigh->refcount, 1);
534 if (bat_priv->bat_algo_ops->bat_hardif_neigh_init)
535 bat_priv->bat_algo_ops->bat_hardif_neigh_init(hardif_neigh);
537 hlist_add_head(&hardif_neigh->list, &hard_iface->neigh_list);
540 spin_unlock_bh(&hard_iface->neigh_list_lock);
545 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
547 * @hard_iface: the interface this neighbour is connected to
548 * @neigh_addr: the interface address of the neighbour to retrieve
550 * Return: the hardif neighbour node if found or created or NULL otherwise.
552 static struct batadv_hardif_neigh_node *
553 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
554 const u8 *neigh_addr)
556 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
558 /* first check without locking to avoid the overhead */
559 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
563 return batadv_hardif_neigh_create(hard_iface, neigh_addr);
567 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
568 * @hard_iface: the interface where this neighbour is connected to
569 * @neigh_addr: the address of the neighbour
571 * Looks for and possibly returns a neighbour belonging to this hard interface.
573 * Return: neighbor when found. Othwerwise NULL
575 struct batadv_hardif_neigh_node *
576 batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
577 const u8 *neigh_addr)
579 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
582 hlist_for_each_entry_rcu(tmp_hardif_neigh,
583 &hard_iface->neigh_list, list) {
584 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
587 if (!atomic_inc_not_zero(&tmp_hardif_neigh->refcount))
590 hardif_neigh = tmp_hardif_neigh;
599 * batadv_neigh_node_new - create and init a new neigh_node object
600 * @orig_node: originator object representing the neighbour
601 * @hard_iface: the interface where the neighbour is connected to
602 * @neigh_addr: the mac address of the neighbour interface
604 * Allocates a new neigh_node object and initialises all the generic fields.
606 * Return: neighbor when found. Othwerwise NULL
608 struct batadv_neigh_node *
609 batadv_neigh_node_new(struct batadv_orig_node *orig_node,
610 struct batadv_hard_iface *hard_iface,
611 const u8 *neigh_addr)
613 struct batadv_neigh_node *neigh_node;
614 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
616 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
620 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
625 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
629 if (!atomic_inc_not_zero(&hard_iface->refcount)) {
635 INIT_HLIST_NODE(&neigh_node->list);
636 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
637 spin_lock_init(&neigh_node->ifinfo_lock);
639 ether_addr_copy(neigh_node->addr, neigh_addr);
640 neigh_node->if_incoming = hard_iface;
641 neigh_node->orig_node = orig_node;
643 /* extra reference for return */
644 atomic_set(&neigh_node->refcount, 2);
646 spin_lock_bh(&orig_node->neigh_list_lock);
647 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
648 spin_unlock_bh(&orig_node->neigh_list_lock);
650 /* increment unique neighbor refcount */
651 atomic_inc(&hardif_neigh->refcount);
653 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
654 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
655 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
659 batadv_hardif_neigh_free_ref(hardif_neigh);
664 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
665 * @seq: neighbour table seq_file struct
670 int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
672 struct net_device *net_dev = (struct net_device *)seq->private;
673 struct batadv_priv *bat_priv = netdev_priv(net_dev);
674 struct batadv_hard_iface *primary_if;
676 primary_if = batadv_seq_print_text_primary_if_get(seq);
680 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
681 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
682 primary_if->net_dev->dev_addr, net_dev->name,
683 bat_priv->bat_algo_ops->name);
685 batadv_hardif_free_ref(primary_if);
687 if (!bat_priv->bat_algo_ops->bat_neigh_print) {
689 "No printing function for this routing protocol\n");
693 bat_priv->bat_algo_ops->bat_neigh_print(bat_priv, seq);
698 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
699 * free after rcu grace period
700 * @orig_ifinfo: the orig_ifinfo object to release
702 static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo *orig_ifinfo)
704 struct batadv_neigh_node *router;
706 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
707 batadv_hardif_free_ref(orig_ifinfo->if_outgoing);
709 /* this is the last reference to this object */
710 router = rcu_dereference_protected(orig_ifinfo->router, true);
712 batadv_neigh_node_free_ref(router);
714 kfree_rcu(orig_ifinfo, rcu);
718 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
720 * @orig_ifinfo: the orig_ifinfo object to release
722 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
724 if (atomic_dec_and_test(&orig_ifinfo->refcount))
725 batadv_orig_ifinfo_release(orig_ifinfo);
729 * batadv_orig_node_free_rcu - free the orig_node
730 * @rcu: rcu pointer of the orig_node
732 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
734 struct batadv_orig_node *orig_node;
736 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
738 batadv_mcast_purge_orig(orig_node);
740 batadv_frag_purge_orig(orig_node, NULL);
742 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
743 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
745 kfree(orig_node->tt_buff);
750 * batadv_orig_node_release - release orig_node from lists and queue for
751 * free after rcu grace period
752 * @orig_node: the orig node to free
754 static void batadv_orig_node_release(struct batadv_orig_node *orig_node)
756 struct hlist_node *node_tmp;
757 struct batadv_neigh_node *neigh_node;
758 struct batadv_orig_ifinfo *orig_ifinfo;
760 spin_lock_bh(&orig_node->neigh_list_lock);
762 /* for all neighbors towards this originator ... */
763 hlist_for_each_entry_safe(neigh_node, node_tmp,
764 &orig_node->neigh_list, list) {
765 hlist_del_rcu(&neigh_node->list);
766 batadv_neigh_node_free_ref(neigh_node);
769 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
770 &orig_node->ifinfo_list, list) {
771 hlist_del_rcu(&orig_ifinfo->list);
772 batadv_orig_ifinfo_free_ref(orig_ifinfo);
774 spin_unlock_bh(&orig_node->neigh_list_lock);
777 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
779 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
783 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
785 * @orig_node: the orig node to free
787 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
789 if (atomic_dec_and_test(&orig_node->refcount))
790 batadv_orig_node_release(orig_node);
793 void batadv_originator_free(struct batadv_priv *bat_priv)
795 struct batadv_hashtable *hash = bat_priv->orig_hash;
796 struct hlist_node *node_tmp;
797 struct hlist_head *head;
798 spinlock_t *list_lock; /* spinlock to protect write access */
799 struct batadv_orig_node *orig_node;
805 cancel_delayed_work_sync(&bat_priv->orig_work);
807 bat_priv->orig_hash = NULL;
809 for (i = 0; i < hash->size; i++) {
810 head = &hash->table[i];
811 list_lock = &hash->list_locks[i];
813 spin_lock_bh(list_lock);
814 hlist_for_each_entry_safe(orig_node, node_tmp,
816 hlist_del_rcu(&orig_node->hash_entry);
817 batadv_orig_node_free_ref(orig_node);
819 spin_unlock_bh(list_lock);
822 batadv_hash_destroy(hash);
826 * batadv_orig_node_new - creates a new orig_node
827 * @bat_priv: the bat priv with all the soft interface information
828 * @addr: the mac address of the originator
830 * Creates a new originator object and initialise all the generic fields.
831 * The new object is not added to the originator list.
833 * Return: the newly created object or NULL on failure.
835 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
838 struct batadv_orig_node *orig_node;
839 struct batadv_orig_node_vlan *vlan;
840 unsigned long reset_time;
843 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
844 "Creating new originator: %pM\n", addr);
846 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
850 INIT_HLIST_HEAD(&orig_node->neigh_list);
851 INIT_HLIST_HEAD(&orig_node->vlan_list);
852 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
853 spin_lock_init(&orig_node->bcast_seqno_lock);
854 spin_lock_init(&orig_node->neigh_list_lock);
855 spin_lock_init(&orig_node->tt_buff_lock);
856 spin_lock_init(&orig_node->tt_lock);
857 spin_lock_init(&orig_node->vlan_list_lock);
859 batadv_nc_init_orig(orig_node);
861 /* extra reference for return */
862 atomic_set(&orig_node->refcount, 2);
864 orig_node->bat_priv = bat_priv;
865 ether_addr_copy(orig_node->orig, addr);
866 batadv_dat_init_orig_node_addr(orig_node);
867 atomic_set(&orig_node->last_ttvn, 0);
868 orig_node->tt_buff = NULL;
869 orig_node->tt_buff_len = 0;
870 orig_node->last_seen = jiffies;
871 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
872 orig_node->bcast_seqno_reset = reset_time;
874 #ifdef CONFIG_BATMAN_ADV_MCAST
875 orig_node->mcast_flags = BATADV_NO_FLAGS;
876 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
877 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
878 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
879 spin_lock_init(&orig_node->mcast_handler_lock);
882 /* create a vlan object for the "untagged" LAN */
883 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
886 /* batadv_orig_node_vlan_new() increases the refcounter.
887 * Immediately release vlan since it is not needed anymore in this
890 batadv_orig_node_vlan_free_ref(vlan);
892 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
893 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
894 spin_lock_init(&orig_node->fragments[i].lock);
895 orig_node->fragments[i].size = 0;
905 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
906 * @bat_priv: the bat priv with all the soft interface information
907 * @neigh: orig node which is to be checked
910 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
911 struct batadv_neigh_node *neigh)
913 struct batadv_neigh_ifinfo *neigh_ifinfo;
914 struct batadv_hard_iface *if_outgoing;
915 struct hlist_node *node_tmp;
917 spin_lock_bh(&neigh->ifinfo_lock);
919 /* for all ifinfo objects for this neighinator */
920 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
921 &neigh->ifinfo_list, list) {
922 if_outgoing = neigh_ifinfo->if_outgoing;
924 /* always keep the default interface */
925 if (if_outgoing == BATADV_IF_DEFAULT)
928 /* don't purge if the interface is not (going) down */
929 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
930 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
931 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
934 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
935 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
936 neigh->addr, if_outgoing->net_dev->name);
938 hlist_del_rcu(&neigh_ifinfo->list);
939 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
942 spin_unlock_bh(&neigh->ifinfo_lock);
946 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
947 * @bat_priv: the bat priv with all the soft interface information
948 * @orig_node: orig node which is to be checked
950 * Return: true if any ifinfo entry was purged, false otherwise.
953 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
954 struct batadv_orig_node *orig_node)
956 struct batadv_orig_ifinfo *orig_ifinfo;
957 struct batadv_hard_iface *if_outgoing;
958 struct hlist_node *node_tmp;
959 bool ifinfo_purged = false;
961 spin_lock_bh(&orig_node->neigh_list_lock);
963 /* for all ifinfo objects for this originator */
964 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
965 &orig_node->ifinfo_list, list) {
966 if_outgoing = orig_ifinfo->if_outgoing;
968 /* always keep the default interface */
969 if (if_outgoing == BATADV_IF_DEFAULT)
972 /* don't purge if the interface is not (going) down */
973 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
974 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
975 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
978 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
979 "router/ifinfo purge: originator %pM, iface: %s\n",
980 orig_node->orig, if_outgoing->net_dev->name);
982 ifinfo_purged = true;
984 hlist_del_rcu(&orig_ifinfo->list);
985 batadv_orig_ifinfo_free_ref(orig_ifinfo);
986 if (orig_node->last_bonding_candidate == orig_ifinfo) {
987 orig_node->last_bonding_candidate = NULL;
988 batadv_orig_ifinfo_free_ref(orig_ifinfo);
992 spin_unlock_bh(&orig_node->neigh_list_lock);
994 return ifinfo_purged;
998 * batadv_purge_orig_neighbors - purges neighbors from originator
999 * @bat_priv: the bat priv with all the soft interface information
1000 * @orig_node: orig node which is to be checked
1002 * Return: true if any neighbor was purged, false otherwise
1005 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
1006 struct batadv_orig_node *orig_node)
1008 struct hlist_node *node_tmp;
1009 struct batadv_neigh_node *neigh_node;
1010 bool neigh_purged = false;
1011 unsigned long last_seen;
1012 struct batadv_hard_iface *if_incoming;
1014 spin_lock_bh(&orig_node->neigh_list_lock);
1016 /* for all neighbors towards this originator ... */
1017 hlist_for_each_entry_safe(neigh_node, node_tmp,
1018 &orig_node->neigh_list, list) {
1019 last_seen = neigh_node->last_seen;
1020 if_incoming = neigh_node->if_incoming;
1022 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
1023 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
1024 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1025 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
1026 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
1027 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1028 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
1029 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1030 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1031 orig_node->orig, neigh_node->addr,
1032 if_incoming->net_dev->name);
1034 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1035 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1036 orig_node->orig, neigh_node->addr,
1037 jiffies_to_msecs(last_seen));
1039 neigh_purged = true;
1041 hlist_del_rcu(&neigh_node->list);
1042 batadv_neigh_node_free_ref(neigh_node);
1044 /* only necessary if not the whole neighbor is to be
1045 * deleted, but some interface has been removed.
1047 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
1051 spin_unlock_bh(&orig_node->neigh_list_lock);
1052 return neigh_purged;
1056 * batadv_find_best_neighbor - finds the best neighbor after purging
1057 * @bat_priv: the bat priv with all the soft interface information
1058 * @orig_node: orig node which is to be checked
1059 * @if_outgoing: the interface for which the metric should be compared
1061 * Return: the current best neighbor, with refcount increased.
1063 static struct batadv_neigh_node *
1064 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1065 struct batadv_orig_node *orig_node,
1066 struct batadv_hard_iface *if_outgoing)
1068 struct batadv_neigh_node *best = NULL, *neigh;
1069 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1072 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1073 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
1074 best, if_outgoing) <= 0))
1077 if (!atomic_inc_not_zero(&neigh->refcount))
1081 batadv_neigh_node_free_ref(best);
1091 * batadv_purge_orig_node - purges obsolete information from an orig_node
1092 * @bat_priv: the bat priv with all the soft interface information
1093 * @orig_node: orig node which is to be checked
1095 * This function checks if the orig_node or substructures of it have become
1096 * obsolete, and purges this information if that's the case.
1098 * Return: true if the orig_node is to be removed, false otherwise.
1100 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1101 struct batadv_orig_node *orig_node)
1103 struct batadv_neigh_node *best_neigh_node;
1104 struct batadv_hard_iface *hard_iface;
1105 bool changed_ifinfo, changed_neigh;
1107 if (batadv_has_timed_out(orig_node->last_seen,
1108 2 * BATADV_PURGE_TIMEOUT)) {
1109 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1110 "Originator timeout: originator %pM, last_seen %u\n",
1112 jiffies_to_msecs(orig_node->last_seen));
1115 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1116 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
1118 if (!changed_ifinfo && !changed_neigh)
1121 /* first for NULL ... */
1122 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1124 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1126 if (best_neigh_node)
1127 batadv_neigh_node_free_ref(best_neigh_node);
1129 /* ... then for all other interfaces. */
1131 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1132 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1135 if (hard_iface->soft_iface != bat_priv->soft_iface)
1138 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1141 batadv_update_route(bat_priv, orig_node, hard_iface,
1143 if (best_neigh_node)
1144 batadv_neigh_node_free_ref(best_neigh_node);
1151 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
1153 struct batadv_hashtable *hash = bat_priv->orig_hash;
1154 struct hlist_node *node_tmp;
1155 struct hlist_head *head;
1156 spinlock_t *list_lock; /* spinlock to protect write access */
1157 struct batadv_orig_node *orig_node;
1163 /* for all origins... */
1164 for (i = 0; i < hash->size; i++) {
1165 head = &hash->table[i];
1166 list_lock = &hash->list_locks[i];
1168 spin_lock_bh(list_lock);
1169 hlist_for_each_entry_safe(orig_node, node_tmp,
1171 if (batadv_purge_orig_node(bat_priv, orig_node)) {
1172 batadv_gw_node_delete(bat_priv, orig_node);
1173 hlist_del_rcu(&orig_node->hash_entry);
1174 batadv_tt_global_del_orig(orig_node->bat_priv,
1176 "originator timed out");
1177 batadv_orig_node_free_ref(orig_node);
1181 batadv_frag_purge_orig(orig_node,
1182 batadv_frag_check_entry);
1184 spin_unlock_bh(list_lock);
1187 batadv_gw_election(bat_priv);
1190 static void batadv_purge_orig(struct work_struct *work)
1192 struct delayed_work *delayed_work;
1193 struct batadv_priv *bat_priv;
1195 delayed_work = container_of(work, struct delayed_work, work);
1196 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1197 _batadv_purge_orig(bat_priv);
1198 queue_delayed_work(batadv_event_workqueue,
1199 &bat_priv->orig_work,
1200 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1203 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1205 _batadv_purge_orig(bat_priv);
1208 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1210 struct net_device *net_dev = (struct net_device *)seq->private;
1211 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1212 struct batadv_hard_iface *primary_if;
1214 primary_if = batadv_seq_print_text_primary_if_get(seq);
1218 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1219 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1220 primary_if->net_dev->dev_addr, net_dev->name,
1221 bat_priv->bat_algo_ops->name);
1223 batadv_hardif_free_ref(primary_if);
1225 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1227 "No printing function for this routing protocol\n");
1231 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1238 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1239 * outgoing interface
1240 * @seq: debugfs table seq_file struct
1245 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1247 struct net_device *net_dev = (struct net_device *)seq->private;
1248 struct batadv_hard_iface *hard_iface;
1249 struct batadv_priv *bat_priv;
1251 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1253 if (!hard_iface || !hard_iface->soft_iface) {
1254 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1258 bat_priv = netdev_priv(hard_iface->soft_iface);
1259 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1261 "No printing function for this routing protocol\n");
1265 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1266 seq_puts(seq, "Interface not active\n");
1270 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1271 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1272 hard_iface->net_dev->dev_addr,
1273 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1275 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1279 batadv_hardif_free_ref(hard_iface);
1283 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1286 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1287 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1288 struct batadv_hashtable *hash = bat_priv->orig_hash;
1289 struct hlist_head *head;
1290 struct batadv_orig_node *orig_node;
1294 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1297 for (i = 0; i < hash->size; i++) {
1298 head = &hash->table[i];
1301 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1303 if (bao->bat_orig_add_if)
1304 ret = bao->bat_orig_add_if(orig_node,
1319 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1322 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1323 struct batadv_hashtable *hash = bat_priv->orig_hash;
1324 struct hlist_head *head;
1325 struct batadv_hard_iface *hard_iface_tmp;
1326 struct batadv_orig_node *orig_node;
1327 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1331 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1334 for (i = 0; i < hash->size; i++) {
1335 head = &hash->table[i];
1338 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1340 if (bao->bat_orig_del_if)
1341 ret = bao->bat_orig_del_if(orig_node,
1343 hard_iface->if_num);
1350 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1352 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1353 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1356 if (hard_iface == hard_iface_tmp)
1359 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1362 if (hard_iface_tmp->if_num > hard_iface->if_num)
1363 hard_iface_tmp->if_num--;
1367 hard_iface->if_num = -1;