batman-adv: Place kref_get for neigh_ifinfo near use
[linux/fpc-iii.git] / net / batman-adv / originator.c
blob5e99a6e296e6267b2e24729585bb9c72ea304d0e
1 /* Copyright (C) 2009-2016 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"
19 #include "main.h"
21 #include <linux/atomic.h>
22 #include <linux/errno.h>
23 #include <linux/etherdevice.h>
24 #include <linux/fs.h>
25 #include <linux/jiffies.h>
26 #include <linux/kernel.h>
27 #include <linux/kref.h>
28 #include <linux/list.h>
29 #include <linux/lockdep.h>
30 #include <linux/netdevice.h>
31 #include <linux/netlink.h>
32 #include <linux/rculist.h>
33 #include <linux/seq_file.h>
34 #include <linux/skbuff.h>
35 #include <linux/slab.h>
36 #include <linux/spinlock.h>
37 #include <linux/workqueue.h>
38 #include <net/sock.h>
39 #include <uapi/linux/batman_adv.h>
41 #include "bat_algo.h"
42 #include "distributed-arp-table.h"
43 #include "fragmentation.h"
44 #include "gateway_client.h"
45 #include "hard-interface.h"
46 #include "hash.h"
47 #include "log.h"
48 #include "multicast.h"
49 #include "netlink.h"
50 #include "network-coding.h"
51 #include "routing.h"
52 #include "soft-interface.h"
53 #include "translation-table.h"
55 /* hash class keys */
56 static struct lock_class_key batadv_orig_hash_lock_class_key;
58 static void batadv_purge_orig(struct work_struct *work);
60 /**
61 * batadv_compare_orig - comparing function used in the originator hash table
62 * @node: node in the local table
63 * @data2: second object to compare the node to
65 * Return: true if they are the same originator
67 bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
69 const void *data1 = container_of(node, struct batadv_orig_node,
70 hash_entry);
72 return batadv_compare_eth(data1, data2);
75 /**
76 * batadv_orig_node_vlan_get - get an orig_node_vlan object
77 * @orig_node: the originator serving the VLAN
78 * @vid: the VLAN identifier
80 * Return: the vlan object identified by vid and belonging to orig_node or NULL
81 * if it does not exist.
83 struct batadv_orig_node_vlan *
84 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
85 unsigned short vid)
87 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
89 rcu_read_lock();
90 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
91 if (tmp->vid != vid)
92 continue;
94 if (!kref_get_unless_zero(&tmp->refcount))
95 continue;
97 vlan = tmp;
99 break;
101 rcu_read_unlock();
103 return vlan;
107 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
108 * object
109 * @orig_node: the originator serving the VLAN
110 * @vid: the VLAN identifier
112 * Return: NULL in case of failure or the vlan object identified by vid and
113 * belonging to orig_node otherwise. The object is created and added to the list
114 * if it does not exist.
116 * The object is returned with refcounter increased by 1.
118 struct batadv_orig_node_vlan *
119 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
120 unsigned short vid)
122 struct batadv_orig_node_vlan *vlan;
124 spin_lock_bh(&orig_node->vlan_list_lock);
126 /* first look if an object for this vid already exists */
127 vlan = batadv_orig_node_vlan_get(orig_node, vid);
128 if (vlan)
129 goto out;
131 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
132 if (!vlan)
133 goto out;
135 kref_init(&vlan->refcount);
136 vlan->vid = vid;
138 kref_get(&vlan->refcount);
139 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
141 out:
142 spin_unlock_bh(&orig_node->vlan_list_lock);
144 return vlan;
148 * batadv_orig_node_vlan_release - release originator-vlan object from lists
149 * and queue for free after rcu grace period
150 * @ref: kref pointer of the originator-vlan object
152 static void batadv_orig_node_vlan_release(struct kref *ref)
154 struct batadv_orig_node_vlan *orig_vlan;
156 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
158 kfree_rcu(orig_vlan, rcu);
162 * batadv_orig_node_vlan_put - decrement the refcounter and possibly release
163 * the originator-vlan object
164 * @orig_vlan: the originator-vlan object to release
166 void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
168 kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
171 int batadv_originator_init(struct batadv_priv *bat_priv)
173 if (bat_priv->orig_hash)
174 return 0;
176 bat_priv->orig_hash = batadv_hash_new(1024);
178 if (!bat_priv->orig_hash)
179 goto err;
181 batadv_hash_set_lock_class(bat_priv->orig_hash,
182 &batadv_orig_hash_lock_class_key);
184 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
185 queue_delayed_work(batadv_event_workqueue,
186 &bat_priv->orig_work,
187 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
189 return 0;
191 err:
192 return -ENOMEM;
196 * batadv_neigh_ifinfo_release - release neigh_ifinfo from lists and queue for
197 * free after rcu grace period
198 * @ref: kref pointer of the neigh_ifinfo
200 static void batadv_neigh_ifinfo_release(struct kref *ref)
202 struct batadv_neigh_ifinfo *neigh_ifinfo;
204 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
206 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
207 batadv_hardif_put(neigh_ifinfo->if_outgoing);
209 kfree_rcu(neigh_ifinfo, rcu);
213 * batadv_neigh_ifinfo_put - decrement the refcounter and possibly release
214 * the neigh_ifinfo
215 * @neigh_ifinfo: the neigh_ifinfo object to release
217 void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
219 kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
223 * batadv_hardif_neigh_release - release hardif neigh node from lists and
224 * queue for free after rcu grace period
225 * @ref: kref pointer of the neigh_node
227 static void batadv_hardif_neigh_release(struct kref *ref)
229 struct batadv_hardif_neigh_node *hardif_neigh;
231 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
232 refcount);
234 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
235 hlist_del_init_rcu(&hardif_neigh->list);
236 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
238 batadv_hardif_put(hardif_neigh->if_incoming);
239 kfree_rcu(hardif_neigh, rcu);
243 * batadv_hardif_neigh_put - decrement the hardif neighbors refcounter
244 * and possibly release it
245 * @hardif_neigh: hardif neigh neighbor to free
247 void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
249 kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
253 * batadv_neigh_node_release - release neigh_node from lists and queue for
254 * free after rcu grace period
255 * @ref: kref pointer of the neigh_node
257 static void batadv_neigh_node_release(struct kref *ref)
259 struct hlist_node *node_tmp;
260 struct batadv_neigh_node *neigh_node;
261 struct batadv_neigh_ifinfo *neigh_ifinfo;
263 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
265 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
266 &neigh_node->ifinfo_list, list) {
267 batadv_neigh_ifinfo_put(neigh_ifinfo);
270 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
272 batadv_hardif_put(neigh_node->if_incoming);
274 kfree_rcu(neigh_node, rcu);
278 * batadv_neigh_node_put - decrement the neighbors refcounter and possibly
279 * release it
280 * @neigh_node: neigh neighbor to free
282 void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
284 kref_put(&neigh_node->refcount, batadv_neigh_node_release);
288 * batadv_orig_router_get - router to the originator depending on iface
289 * @orig_node: the orig node for the router
290 * @if_outgoing: the interface where the payload packet has been received or
291 * the OGM should be sent to
293 * Return: the neighbor which should be router for this orig_node/iface.
295 * The object is returned with refcounter increased by 1.
297 struct batadv_neigh_node *
298 batadv_orig_router_get(struct batadv_orig_node *orig_node,
299 const struct batadv_hard_iface *if_outgoing)
301 struct batadv_orig_ifinfo *orig_ifinfo;
302 struct batadv_neigh_node *router = NULL;
304 rcu_read_lock();
305 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
306 if (orig_ifinfo->if_outgoing != if_outgoing)
307 continue;
309 router = rcu_dereference(orig_ifinfo->router);
310 break;
313 if (router && !kref_get_unless_zero(&router->refcount))
314 router = NULL;
316 rcu_read_unlock();
317 return router;
321 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
322 * @orig_node: the orig node to be queried
323 * @if_outgoing: the interface for which the ifinfo should be acquired
325 * Return: the requested orig_ifinfo or NULL if not found.
327 * The object is returned with refcounter increased by 1.
329 struct batadv_orig_ifinfo *
330 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
331 struct batadv_hard_iface *if_outgoing)
333 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
335 rcu_read_lock();
336 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
337 list) {
338 if (tmp->if_outgoing != if_outgoing)
339 continue;
341 if (!kref_get_unless_zero(&tmp->refcount))
342 continue;
344 orig_ifinfo = tmp;
345 break;
347 rcu_read_unlock();
349 return orig_ifinfo;
353 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
354 * @orig_node: the orig node to be queried
355 * @if_outgoing: the interface for which the ifinfo should be acquired
357 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
358 * interface otherwise. The object is created and added to the list
359 * if it does not exist.
361 * The object is returned with refcounter increased by 1.
363 struct batadv_orig_ifinfo *
364 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
365 struct batadv_hard_iface *if_outgoing)
367 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
368 unsigned long reset_time;
370 spin_lock_bh(&orig_node->neigh_list_lock);
372 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
373 if (orig_ifinfo)
374 goto out;
376 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
377 if (!orig_ifinfo)
378 goto out;
380 if (if_outgoing != BATADV_IF_DEFAULT)
381 kref_get(&if_outgoing->refcount);
383 reset_time = jiffies - 1;
384 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
385 orig_ifinfo->batman_seqno_reset = reset_time;
386 orig_ifinfo->if_outgoing = if_outgoing;
387 INIT_HLIST_NODE(&orig_ifinfo->list);
388 kref_init(&orig_ifinfo->refcount);
390 kref_get(&orig_ifinfo->refcount);
391 hlist_add_head_rcu(&orig_ifinfo->list,
392 &orig_node->ifinfo_list);
393 out:
394 spin_unlock_bh(&orig_node->neigh_list_lock);
395 return orig_ifinfo;
399 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
400 * @neigh: the neigh node to be queried
401 * @if_outgoing: the interface for which the ifinfo should be acquired
403 * The object is returned with refcounter increased by 1.
405 * Return: the requested neigh_ifinfo or NULL if not found
407 struct batadv_neigh_ifinfo *
408 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
409 struct batadv_hard_iface *if_outgoing)
411 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
412 *tmp_neigh_ifinfo;
414 rcu_read_lock();
415 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
416 list) {
417 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
418 continue;
420 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
421 continue;
423 neigh_ifinfo = tmp_neigh_ifinfo;
424 break;
426 rcu_read_unlock();
428 return neigh_ifinfo;
432 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
433 * @neigh: the neigh node to be queried
434 * @if_outgoing: the interface for which the ifinfo should be acquired
436 * Return: NULL in case of failure or the neigh_ifinfo object for the
437 * if_outgoing interface otherwise. The object is created and added to the list
438 * if it does not exist.
440 * The object is returned with refcounter increased by 1.
442 struct batadv_neigh_ifinfo *
443 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
444 struct batadv_hard_iface *if_outgoing)
446 struct batadv_neigh_ifinfo *neigh_ifinfo;
448 spin_lock_bh(&neigh->ifinfo_lock);
450 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
451 if (neigh_ifinfo)
452 goto out;
454 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
455 if (!neigh_ifinfo)
456 goto out;
458 if (if_outgoing)
459 kref_get(&if_outgoing->refcount);
461 INIT_HLIST_NODE(&neigh_ifinfo->list);
462 kref_init(&neigh_ifinfo->refcount);
463 neigh_ifinfo->if_outgoing = if_outgoing;
465 kref_get(&neigh_ifinfo->refcount);
466 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
468 out:
469 spin_unlock_bh(&neigh->ifinfo_lock);
471 return neigh_ifinfo;
475 * batadv_neigh_node_get - retrieve a neighbour from the list
476 * @orig_node: originator which the neighbour belongs to
477 * @hard_iface: the interface where this neighbour is connected to
478 * @addr: the address of the neighbour
480 * Looks for and possibly returns a neighbour belonging to this originator list
481 * which is connected through the provided hard interface.
483 * Return: neighbor when found. Othwerwise NULL
485 static struct batadv_neigh_node *
486 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
487 const struct batadv_hard_iface *hard_iface,
488 const u8 *addr)
490 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
492 rcu_read_lock();
493 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
494 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
495 continue;
497 if (tmp_neigh_node->if_incoming != hard_iface)
498 continue;
500 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
501 continue;
503 res = tmp_neigh_node;
504 break;
506 rcu_read_unlock();
508 return res;
512 * batadv_hardif_neigh_create - create a hardif neighbour node
513 * @hard_iface: the interface this neighbour is connected to
514 * @neigh_addr: the interface address of the neighbour to retrieve
516 * Return: the hardif neighbour node if found or created or NULL otherwise.
518 static struct batadv_hardif_neigh_node *
519 batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
520 const u8 *neigh_addr)
522 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
523 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
525 spin_lock_bh(&hard_iface->neigh_list_lock);
527 /* check if neighbor hasn't been added in the meantime */
528 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
529 if (hardif_neigh)
530 goto out;
532 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
533 if (!hardif_neigh)
534 goto out;
536 kref_get(&hard_iface->refcount);
537 INIT_HLIST_NODE(&hardif_neigh->list);
538 ether_addr_copy(hardif_neigh->addr, neigh_addr);
539 hardif_neigh->if_incoming = hard_iface;
540 hardif_neigh->last_seen = jiffies;
542 kref_init(&hardif_neigh->refcount);
544 if (bat_priv->algo_ops->neigh.hardif_init)
545 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
547 hlist_add_head(&hardif_neigh->list, &hard_iface->neigh_list);
549 out:
550 spin_unlock_bh(&hard_iface->neigh_list_lock);
551 return hardif_neigh;
555 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
556 * node
557 * @hard_iface: the interface this neighbour is connected to
558 * @neigh_addr: the interface address of the neighbour to retrieve
560 * Return: the hardif neighbour node if found or created or NULL otherwise.
562 static struct batadv_hardif_neigh_node *
563 batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
564 const u8 *neigh_addr)
566 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
568 /* first check without locking to avoid the overhead */
569 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
570 if (hardif_neigh)
571 return hardif_neigh;
573 return batadv_hardif_neigh_create(hard_iface, neigh_addr);
577 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
578 * @hard_iface: the interface where this neighbour is connected to
579 * @neigh_addr: the address of the neighbour
581 * Looks for and possibly returns a neighbour belonging to this hard interface.
583 * Return: neighbor when found. Othwerwise NULL
585 struct batadv_hardif_neigh_node *
586 batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
587 const u8 *neigh_addr)
589 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
591 rcu_read_lock();
592 hlist_for_each_entry_rcu(tmp_hardif_neigh,
593 &hard_iface->neigh_list, list) {
594 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
595 continue;
597 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
598 continue;
600 hardif_neigh = tmp_hardif_neigh;
601 break;
603 rcu_read_unlock();
605 return hardif_neigh;
609 * batadv_neigh_node_create - create a neigh node object
610 * @orig_node: originator object representing the neighbour
611 * @hard_iface: the interface where the neighbour is connected to
612 * @neigh_addr: the mac address of the neighbour interface
614 * Allocates a new neigh_node object and initialises all the generic fields.
616 * Return: the neighbour node if found or created or NULL otherwise.
618 static struct batadv_neigh_node *
619 batadv_neigh_node_create(struct batadv_orig_node *orig_node,
620 struct batadv_hard_iface *hard_iface,
621 const u8 *neigh_addr)
623 struct batadv_neigh_node *neigh_node;
624 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
626 spin_lock_bh(&orig_node->neigh_list_lock);
628 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
629 if (neigh_node)
630 goto out;
632 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
633 neigh_addr);
634 if (!hardif_neigh)
635 goto out;
637 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
638 if (!neigh_node)
639 goto out;
641 INIT_HLIST_NODE(&neigh_node->list);
642 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
643 spin_lock_init(&neigh_node->ifinfo_lock);
645 kref_get(&hard_iface->refcount);
646 ether_addr_copy(neigh_node->addr, neigh_addr);
647 neigh_node->if_incoming = hard_iface;
648 neigh_node->orig_node = orig_node;
649 neigh_node->last_seen = jiffies;
651 /* increment unique neighbor refcount */
652 kref_get(&hardif_neigh->refcount);
653 neigh_node->hardif_neigh = hardif_neigh;
655 /* extra reference for return */
656 kref_init(&neigh_node->refcount);
657 kref_get(&neigh_node->refcount);
659 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
661 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
662 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
663 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
665 out:
666 spin_unlock_bh(&orig_node->neigh_list_lock);
668 if (hardif_neigh)
669 batadv_hardif_neigh_put(hardif_neigh);
670 return neigh_node;
674 * batadv_neigh_node_get_or_create - retrieve or create a neigh node object
675 * @orig_node: originator object representing the neighbour
676 * @hard_iface: the interface where the neighbour is connected to
677 * @neigh_addr: the mac address of the neighbour interface
679 * Return: the neighbour node if found or created or NULL otherwise.
681 struct batadv_neigh_node *
682 batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
683 struct batadv_hard_iface *hard_iface,
684 const u8 *neigh_addr)
686 struct batadv_neigh_node *neigh_node = NULL;
688 /* first check without locking to avoid the overhead */
689 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
690 if (neigh_node)
691 return neigh_node;
693 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
697 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
698 * @seq: neighbour table seq_file struct
699 * @offset: not used
701 * Return: always 0
703 int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
705 struct net_device *net_dev = (struct net_device *)seq->private;
706 struct batadv_priv *bat_priv = netdev_priv(net_dev);
707 struct batadv_hard_iface *primary_if;
709 primary_if = batadv_seq_print_text_primary_if_get(seq);
710 if (!primary_if)
711 return 0;
713 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
714 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
715 primary_if->net_dev->dev_addr, net_dev->name,
716 bat_priv->algo_ops->name);
718 batadv_hardif_put(primary_if);
720 if (!bat_priv->algo_ops->neigh.print) {
721 seq_puts(seq,
722 "No printing function for this routing protocol\n");
723 return 0;
726 bat_priv->algo_ops->neigh.print(bat_priv, seq);
727 return 0;
731 * batadv_hardif_neigh_dump - Dump to netlink the neighbor infos for a specific
732 * outgoing interface
733 * @msg: message to dump into
734 * @cb: parameters for the dump
736 * Return: 0 or error value
738 int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
740 struct net *net = sock_net(cb->skb->sk);
741 struct net_device *soft_iface;
742 struct net_device *hard_iface = NULL;
743 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
744 struct batadv_priv *bat_priv;
745 struct batadv_hard_iface *primary_if = NULL;
746 int ret;
747 int ifindex, hard_ifindex;
749 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
750 if (!ifindex)
751 return -EINVAL;
753 soft_iface = dev_get_by_index(net, ifindex);
754 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
755 ret = -ENODEV;
756 goto out;
759 bat_priv = netdev_priv(soft_iface);
761 primary_if = batadv_primary_if_get_selected(bat_priv);
762 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
763 ret = -ENOENT;
764 goto out;
767 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
768 BATADV_ATTR_HARD_IFINDEX);
769 if (hard_ifindex) {
770 hard_iface = dev_get_by_index(net, hard_ifindex);
771 if (hard_iface)
772 hardif = batadv_hardif_get_by_netdev(hard_iface);
774 if (!hardif) {
775 ret = -ENODEV;
776 goto out;
779 if (hardif->soft_iface != soft_iface) {
780 ret = -ENOENT;
781 goto out;
785 if (!bat_priv->algo_ops->neigh.dump) {
786 ret = -EOPNOTSUPP;
787 goto out;
790 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
792 ret = msg->len;
794 out:
795 if (hardif)
796 batadv_hardif_put(hardif);
797 if (hard_iface)
798 dev_put(hard_iface);
799 if (primary_if)
800 batadv_hardif_put(primary_if);
801 if (soft_iface)
802 dev_put(soft_iface);
804 return ret;
808 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
809 * free after rcu grace period
810 * @ref: kref pointer of the orig_ifinfo
812 static void batadv_orig_ifinfo_release(struct kref *ref)
814 struct batadv_orig_ifinfo *orig_ifinfo;
815 struct batadv_neigh_node *router;
817 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
819 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
820 batadv_hardif_put(orig_ifinfo->if_outgoing);
822 /* this is the last reference to this object */
823 router = rcu_dereference_protected(orig_ifinfo->router, true);
824 if (router)
825 batadv_neigh_node_put(router);
827 kfree_rcu(orig_ifinfo, rcu);
831 * batadv_orig_ifinfo_put - decrement the refcounter and possibly release
832 * the orig_ifinfo
833 * @orig_ifinfo: the orig_ifinfo object to release
835 void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
837 kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
841 * batadv_orig_node_free_rcu - free the orig_node
842 * @rcu: rcu pointer of the orig_node
844 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
846 struct batadv_orig_node *orig_node;
848 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
850 batadv_mcast_purge_orig(orig_node);
852 batadv_frag_purge_orig(orig_node, NULL);
854 if (orig_node->bat_priv->algo_ops->orig.free)
855 orig_node->bat_priv->algo_ops->orig.free(orig_node);
857 kfree(orig_node->tt_buff);
858 kfree(orig_node);
862 * batadv_orig_node_release - release orig_node from lists and queue for
863 * free after rcu grace period
864 * @ref: kref pointer of the orig_node
866 static void batadv_orig_node_release(struct kref *ref)
868 struct hlist_node *node_tmp;
869 struct batadv_neigh_node *neigh_node;
870 struct batadv_orig_node *orig_node;
871 struct batadv_orig_ifinfo *orig_ifinfo;
872 struct batadv_orig_node_vlan *vlan;
873 struct batadv_orig_ifinfo *last_candidate;
875 orig_node = container_of(ref, struct batadv_orig_node, refcount);
877 spin_lock_bh(&orig_node->neigh_list_lock);
879 /* for all neighbors towards this originator ... */
880 hlist_for_each_entry_safe(neigh_node, node_tmp,
881 &orig_node->neigh_list, list) {
882 hlist_del_rcu(&neigh_node->list);
883 batadv_neigh_node_put(neigh_node);
886 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
887 &orig_node->ifinfo_list, list) {
888 hlist_del_rcu(&orig_ifinfo->list);
889 batadv_orig_ifinfo_put(orig_ifinfo);
892 last_candidate = orig_node->last_bonding_candidate;
893 orig_node->last_bonding_candidate = NULL;
894 spin_unlock_bh(&orig_node->neigh_list_lock);
896 if (last_candidate)
897 batadv_orig_ifinfo_put(last_candidate);
899 spin_lock_bh(&orig_node->vlan_list_lock);
900 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
901 hlist_del_rcu(&vlan->list);
902 batadv_orig_node_vlan_put(vlan);
904 spin_unlock_bh(&orig_node->vlan_list_lock);
906 /* Free nc_nodes */
907 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
909 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
913 * batadv_orig_node_put - decrement the orig node refcounter and possibly
914 * release it
915 * @orig_node: the orig node to free
917 void batadv_orig_node_put(struct batadv_orig_node *orig_node)
919 kref_put(&orig_node->refcount, batadv_orig_node_release);
922 void batadv_originator_free(struct batadv_priv *bat_priv)
924 struct batadv_hashtable *hash = bat_priv->orig_hash;
925 struct hlist_node *node_tmp;
926 struct hlist_head *head;
927 spinlock_t *list_lock; /* spinlock to protect write access */
928 struct batadv_orig_node *orig_node;
929 u32 i;
931 if (!hash)
932 return;
934 cancel_delayed_work_sync(&bat_priv->orig_work);
936 bat_priv->orig_hash = NULL;
938 for (i = 0; i < hash->size; i++) {
939 head = &hash->table[i];
940 list_lock = &hash->list_locks[i];
942 spin_lock_bh(list_lock);
943 hlist_for_each_entry_safe(orig_node, node_tmp,
944 head, hash_entry) {
945 hlist_del_rcu(&orig_node->hash_entry);
946 batadv_orig_node_put(orig_node);
948 spin_unlock_bh(list_lock);
951 batadv_hash_destroy(hash);
955 * batadv_orig_node_new - creates a new orig_node
956 * @bat_priv: the bat priv with all the soft interface information
957 * @addr: the mac address of the originator
959 * Creates a new originator object and initialise all the generic fields.
960 * The new object is not added to the originator list.
962 * Return: the newly created object or NULL on failure.
964 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
965 const u8 *addr)
967 struct batadv_orig_node *orig_node;
968 struct batadv_orig_node_vlan *vlan;
969 unsigned long reset_time;
970 int i;
972 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
973 "Creating new originator: %pM\n", addr);
975 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
976 if (!orig_node)
977 return NULL;
979 INIT_HLIST_HEAD(&orig_node->neigh_list);
980 INIT_HLIST_HEAD(&orig_node->vlan_list);
981 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
982 spin_lock_init(&orig_node->bcast_seqno_lock);
983 spin_lock_init(&orig_node->neigh_list_lock);
984 spin_lock_init(&orig_node->tt_buff_lock);
985 spin_lock_init(&orig_node->tt_lock);
986 spin_lock_init(&orig_node->vlan_list_lock);
988 batadv_nc_init_orig(orig_node);
990 /* extra reference for return */
991 kref_init(&orig_node->refcount);
992 kref_get(&orig_node->refcount);
994 orig_node->bat_priv = bat_priv;
995 ether_addr_copy(orig_node->orig, addr);
996 batadv_dat_init_orig_node_addr(orig_node);
997 atomic_set(&orig_node->last_ttvn, 0);
998 orig_node->tt_buff = NULL;
999 orig_node->tt_buff_len = 0;
1000 orig_node->last_seen = jiffies;
1001 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
1002 orig_node->bcast_seqno_reset = reset_time;
1004 #ifdef CONFIG_BATMAN_ADV_MCAST
1005 orig_node->mcast_flags = BATADV_NO_FLAGS;
1006 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
1007 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
1008 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
1009 spin_lock_init(&orig_node->mcast_handler_lock);
1010 #endif
1012 /* create a vlan object for the "untagged" LAN */
1013 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
1014 if (!vlan)
1015 goto free_orig_node;
1016 /* batadv_orig_node_vlan_new() increases the refcounter.
1017 * Immediately release vlan since it is not needed anymore in this
1018 * context
1020 batadv_orig_node_vlan_put(vlan);
1022 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
1023 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
1024 spin_lock_init(&orig_node->fragments[i].lock);
1025 orig_node->fragments[i].size = 0;
1028 return orig_node;
1029 free_orig_node:
1030 kfree(orig_node);
1031 return NULL;
1035 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
1036 * @bat_priv: the bat priv with all the soft interface information
1037 * @neigh: orig node which is to be checked
1039 static void
1040 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1041 struct batadv_neigh_node *neigh)
1043 struct batadv_neigh_ifinfo *neigh_ifinfo;
1044 struct batadv_hard_iface *if_outgoing;
1045 struct hlist_node *node_tmp;
1047 spin_lock_bh(&neigh->ifinfo_lock);
1049 /* for all ifinfo objects for this neighinator */
1050 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1051 &neigh->ifinfo_list, list) {
1052 if_outgoing = neigh_ifinfo->if_outgoing;
1054 /* always keep the default interface */
1055 if (if_outgoing == BATADV_IF_DEFAULT)
1056 continue;
1058 /* don't purge if the interface is not (going) down */
1059 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
1060 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
1061 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
1062 continue;
1064 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1065 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1066 neigh->addr, if_outgoing->net_dev->name);
1068 hlist_del_rcu(&neigh_ifinfo->list);
1069 batadv_neigh_ifinfo_put(neigh_ifinfo);
1072 spin_unlock_bh(&neigh->ifinfo_lock);
1076 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
1077 * @bat_priv: the bat priv with all the soft interface information
1078 * @orig_node: orig node which is to be checked
1080 * Return: true if any ifinfo entry was purged, false otherwise.
1082 static bool
1083 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1084 struct batadv_orig_node *orig_node)
1086 struct batadv_orig_ifinfo *orig_ifinfo;
1087 struct batadv_hard_iface *if_outgoing;
1088 struct hlist_node *node_tmp;
1089 bool ifinfo_purged = false;
1091 spin_lock_bh(&orig_node->neigh_list_lock);
1093 /* for all ifinfo objects for this originator */
1094 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1095 &orig_node->ifinfo_list, list) {
1096 if_outgoing = orig_ifinfo->if_outgoing;
1098 /* always keep the default interface */
1099 if (if_outgoing == BATADV_IF_DEFAULT)
1100 continue;
1102 /* don't purge if the interface is not (going) down */
1103 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
1104 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
1105 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
1106 continue;
1108 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1109 "router/ifinfo purge: originator %pM, iface: %s\n",
1110 orig_node->orig, if_outgoing->net_dev->name);
1112 ifinfo_purged = true;
1114 hlist_del_rcu(&orig_ifinfo->list);
1115 batadv_orig_ifinfo_put(orig_ifinfo);
1116 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1117 orig_node->last_bonding_candidate = NULL;
1118 batadv_orig_ifinfo_put(orig_ifinfo);
1122 spin_unlock_bh(&orig_node->neigh_list_lock);
1124 return ifinfo_purged;
1128 * batadv_purge_orig_neighbors - purges neighbors from originator
1129 * @bat_priv: the bat priv with all the soft interface information
1130 * @orig_node: orig node which is to be checked
1132 * Return: true if any neighbor was purged, false otherwise
1134 static bool
1135 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
1136 struct batadv_orig_node *orig_node)
1138 struct hlist_node *node_tmp;
1139 struct batadv_neigh_node *neigh_node;
1140 bool neigh_purged = false;
1141 unsigned long last_seen;
1142 struct batadv_hard_iface *if_incoming;
1144 spin_lock_bh(&orig_node->neigh_list_lock);
1146 /* for all neighbors towards this originator ... */
1147 hlist_for_each_entry_safe(neigh_node, node_tmp,
1148 &orig_node->neigh_list, list) {
1149 last_seen = neigh_node->last_seen;
1150 if_incoming = neigh_node->if_incoming;
1152 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
1153 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
1154 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1155 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
1156 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
1157 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1158 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
1159 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1160 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1161 orig_node->orig, neigh_node->addr,
1162 if_incoming->net_dev->name);
1163 else
1164 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1165 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1166 orig_node->orig, neigh_node->addr,
1167 jiffies_to_msecs(last_seen));
1169 neigh_purged = true;
1171 hlist_del_rcu(&neigh_node->list);
1172 batadv_neigh_node_put(neigh_node);
1173 } else {
1174 /* only necessary if not the whole neighbor is to be
1175 * deleted, but some interface has been removed.
1177 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
1181 spin_unlock_bh(&orig_node->neigh_list_lock);
1182 return neigh_purged;
1186 * batadv_find_best_neighbor - finds the best neighbor after purging
1187 * @bat_priv: the bat priv with all the soft interface information
1188 * @orig_node: orig node which is to be checked
1189 * @if_outgoing: the interface for which the metric should be compared
1191 * Return: the current best neighbor, with refcount increased.
1193 static struct batadv_neigh_node *
1194 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1195 struct batadv_orig_node *orig_node,
1196 struct batadv_hard_iface *if_outgoing)
1198 struct batadv_neigh_node *best = NULL, *neigh;
1199 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1201 rcu_read_lock();
1202 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1203 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1204 if_outgoing) <= 0))
1205 continue;
1207 if (!kref_get_unless_zero(&neigh->refcount))
1208 continue;
1210 if (best)
1211 batadv_neigh_node_put(best);
1213 best = neigh;
1215 rcu_read_unlock();
1217 return best;
1221 * batadv_purge_orig_node - purges obsolete information from an orig_node
1222 * @bat_priv: the bat priv with all the soft interface information
1223 * @orig_node: orig node which is to be checked
1225 * This function checks if the orig_node or substructures of it have become
1226 * obsolete, and purges this information if that's the case.
1228 * Return: true if the orig_node is to be removed, false otherwise.
1230 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1231 struct batadv_orig_node *orig_node)
1233 struct batadv_neigh_node *best_neigh_node;
1234 struct batadv_hard_iface *hard_iface;
1235 bool changed_ifinfo, changed_neigh;
1237 if (batadv_has_timed_out(orig_node->last_seen,
1238 2 * BATADV_PURGE_TIMEOUT)) {
1239 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1240 "Originator timeout: originator %pM, last_seen %u\n",
1241 orig_node->orig,
1242 jiffies_to_msecs(orig_node->last_seen));
1243 return true;
1245 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1246 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
1248 if (!changed_ifinfo && !changed_neigh)
1249 return false;
1251 /* first for NULL ... */
1252 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1253 BATADV_IF_DEFAULT);
1254 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1255 best_neigh_node);
1256 if (best_neigh_node)
1257 batadv_neigh_node_put(best_neigh_node);
1259 /* ... then for all other interfaces. */
1260 rcu_read_lock();
1261 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1262 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1263 continue;
1265 if (hard_iface->soft_iface != bat_priv->soft_iface)
1266 continue;
1268 if (!kref_get_unless_zero(&hard_iface->refcount))
1269 continue;
1271 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1272 orig_node,
1273 hard_iface);
1274 batadv_update_route(bat_priv, orig_node, hard_iface,
1275 best_neigh_node);
1276 if (best_neigh_node)
1277 batadv_neigh_node_put(best_neigh_node);
1279 batadv_hardif_put(hard_iface);
1281 rcu_read_unlock();
1283 return false;
1286 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
1288 struct batadv_hashtable *hash = bat_priv->orig_hash;
1289 struct hlist_node *node_tmp;
1290 struct hlist_head *head;
1291 spinlock_t *list_lock; /* spinlock to protect write access */
1292 struct batadv_orig_node *orig_node;
1293 u32 i;
1295 if (!hash)
1296 return;
1298 /* for all origins... */
1299 for (i = 0; i < hash->size; i++) {
1300 head = &hash->table[i];
1301 list_lock = &hash->list_locks[i];
1303 spin_lock_bh(list_lock);
1304 hlist_for_each_entry_safe(orig_node, node_tmp,
1305 head, hash_entry) {
1306 if (batadv_purge_orig_node(bat_priv, orig_node)) {
1307 batadv_gw_node_delete(bat_priv, orig_node);
1308 hlist_del_rcu(&orig_node->hash_entry);
1309 batadv_tt_global_del_orig(orig_node->bat_priv,
1310 orig_node, -1,
1311 "originator timed out");
1312 batadv_orig_node_put(orig_node);
1313 continue;
1316 batadv_frag_purge_orig(orig_node,
1317 batadv_frag_check_entry);
1319 spin_unlock_bh(list_lock);
1322 batadv_gw_election(bat_priv);
1325 static void batadv_purge_orig(struct work_struct *work)
1327 struct delayed_work *delayed_work;
1328 struct batadv_priv *bat_priv;
1330 delayed_work = to_delayed_work(work);
1331 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1332 _batadv_purge_orig(bat_priv);
1333 queue_delayed_work(batadv_event_workqueue,
1334 &bat_priv->orig_work,
1335 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1338 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1340 _batadv_purge_orig(bat_priv);
1343 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1345 struct net_device *net_dev = (struct net_device *)seq->private;
1346 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1347 struct batadv_hard_iface *primary_if;
1349 primary_if = batadv_seq_print_text_primary_if_get(seq);
1350 if (!primary_if)
1351 return 0;
1353 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1354 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1355 primary_if->net_dev->dev_addr, net_dev->name,
1356 bat_priv->algo_ops->name);
1358 batadv_hardif_put(primary_if);
1360 if (!bat_priv->algo_ops->orig.print) {
1361 seq_puts(seq,
1362 "No printing function for this routing protocol\n");
1363 return 0;
1366 bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
1368 return 0;
1372 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1373 * outgoing interface
1374 * @seq: debugfs table seq_file struct
1375 * @offset: not used
1377 * Return: 0
1379 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1381 struct net_device *net_dev = (struct net_device *)seq->private;
1382 struct batadv_hard_iface *hard_iface;
1383 struct batadv_priv *bat_priv;
1385 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1387 if (!hard_iface || !hard_iface->soft_iface) {
1388 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1389 goto out;
1392 bat_priv = netdev_priv(hard_iface->soft_iface);
1393 if (!bat_priv->algo_ops->orig.print) {
1394 seq_puts(seq,
1395 "No printing function for this routing protocol\n");
1396 goto out;
1399 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1400 seq_puts(seq, "Interface not active\n");
1401 goto out;
1404 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1405 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1406 hard_iface->net_dev->dev_addr,
1407 hard_iface->soft_iface->name, bat_priv->algo_ops->name);
1409 bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
1411 out:
1412 if (hard_iface)
1413 batadv_hardif_put(hard_iface);
1414 return 0;
1418 * batadv_orig_dump - Dump to netlink the originator infos for a specific
1419 * outgoing interface
1420 * @msg: message to dump into
1421 * @cb: parameters for the dump
1423 * Return: 0 or error value
1425 int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1427 struct net *net = sock_net(cb->skb->sk);
1428 struct net_device *soft_iface;
1429 struct net_device *hard_iface = NULL;
1430 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1431 struct batadv_priv *bat_priv;
1432 struct batadv_hard_iface *primary_if = NULL;
1433 int ret;
1434 int ifindex, hard_ifindex;
1436 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1437 if (!ifindex)
1438 return -EINVAL;
1440 soft_iface = dev_get_by_index(net, ifindex);
1441 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1442 ret = -ENODEV;
1443 goto out;
1446 bat_priv = netdev_priv(soft_iface);
1448 primary_if = batadv_primary_if_get_selected(bat_priv);
1449 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1450 ret = -ENOENT;
1451 goto out;
1454 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1455 BATADV_ATTR_HARD_IFINDEX);
1456 if (hard_ifindex) {
1457 hard_iface = dev_get_by_index(net, hard_ifindex);
1458 if (hard_iface)
1459 hardif = batadv_hardif_get_by_netdev(hard_iface);
1461 if (!hardif) {
1462 ret = -ENODEV;
1463 goto out;
1466 if (hardif->soft_iface != soft_iface) {
1467 ret = -ENOENT;
1468 goto out;
1472 if (!bat_priv->algo_ops->orig.dump) {
1473 ret = -EOPNOTSUPP;
1474 goto out;
1477 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1479 ret = msg->len;
1481 out:
1482 if (hardif)
1483 batadv_hardif_put(hardif);
1484 if (hard_iface)
1485 dev_put(hard_iface);
1486 if (primary_if)
1487 batadv_hardif_put(primary_if);
1488 if (soft_iface)
1489 dev_put(soft_iface);
1491 return ret;
1494 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1495 int max_if_num)
1497 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1498 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1499 struct batadv_hashtable *hash = bat_priv->orig_hash;
1500 struct hlist_head *head;
1501 struct batadv_orig_node *orig_node;
1502 u32 i;
1503 int ret;
1505 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1506 * if_num
1508 for (i = 0; i < hash->size; i++) {
1509 head = &hash->table[i];
1511 rcu_read_lock();
1512 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1513 ret = 0;
1514 if (bao->orig.add_if)
1515 ret = bao->orig.add_if(orig_node, max_if_num);
1516 if (ret == -ENOMEM)
1517 goto err;
1519 rcu_read_unlock();
1522 return 0;
1524 err:
1525 rcu_read_unlock();
1526 return -ENOMEM;
1529 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1530 int max_if_num)
1532 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1533 struct batadv_hashtable *hash = bat_priv->orig_hash;
1534 struct hlist_head *head;
1535 struct batadv_hard_iface *hard_iface_tmp;
1536 struct batadv_orig_node *orig_node;
1537 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1538 u32 i;
1539 int ret;
1541 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1542 * if_num
1544 for (i = 0; i < hash->size; i++) {
1545 head = &hash->table[i];
1547 rcu_read_lock();
1548 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1549 ret = 0;
1550 if (bao->orig.del_if)
1551 ret = bao->orig.del_if(orig_node, max_if_num,
1552 hard_iface->if_num);
1553 if (ret == -ENOMEM)
1554 goto err;
1556 rcu_read_unlock();
1559 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1560 rcu_read_lock();
1561 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1562 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1563 continue;
1565 if (hard_iface == hard_iface_tmp)
1566 continue;
1568 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1569 continue;
1571 if (hard_iface_tmp->if_num > hard_iface->if_num)
1572 hard_iface_tmp->if_num--;
1574 rcu_read_unlock();
1576 hard_iface->if_num = -1;
1577 return 0;
1579 err:
1580 rcu_read_unlock();
1581 return -ENOMEM;