blob: 56a981af5c928fe7c160dad67c267d681395c3b5 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann6b1aea82018-01-01 00:00:00 +01002/* Copyright (C) 2009-2018 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000017 */
18
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000019#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#include "main.h"
21
Sven Eckelmann7c124392016-01-16 10:29:56 +010022#include <linux/atomic.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020023#include <linux/errno.h>
24#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010025#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020026#include <linux/jiffies.h>
27#include <linux/kernel.h>
Sven Eckelmann90f564d2016-01-16 10:29:40 +010028#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020029#include <linux/list.h>
30#include <linux/lockdep.h>
31#include <linux/netdevice.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020032#include <linux/netlink.h>
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080033#include <linux/rculist.h>
Denys Vlasenko3326afe2017-11-19 17:59:13 +010034#include <linux/rcupdate.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include <linux/seq_file.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020036#include <linux/skbuff.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020037#include <linux/slab.h>
38#include <linux/spinlock.h>
Denys Vlasenko3326afe2017-11-19 17:59:13 +010039#include <linux/stddef.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020040#include <linux/workqueue.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020041#include <net/sock.h>
42#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020043
Sven Eckelmann01d350d2016-05-15 11:07:44 +020044#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020045#include "distributed-arp-table.h"
46#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000047#include "gateway_client.h"
48#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020049#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020050#include "log.h"
Linus Lüssing60432d72014-02-15 17:47:51 +010051#include "multicast.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020052#include "netlink.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020053#include "network-coding.h"
54#include "routing.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020055#include "soft-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020056#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057
Antonio Quartullidec05072012-11-10 11:00:32 +010058/* hash class keys */
59static struct lock_class_key batadv_orig_hash_lock_class_key;
60
Sven Eckelmannff15c272017-12-02 19:51:53 +010061/**
62 * batadv_orig_hash_find() - Find and return originator from orig_hash
63 * @bat_priv: the bat priv with all the soft interface information
64 * @data: mac address of the originator
65 *
66 * Return: orig_node (with increased refcnt), NULL on errors
67 */
Denys Vlasenko3326afe2017-11-19 17:59:13 +010068struct batadv_orig_node *
69batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)
70{
71 struct batadv_hashtable *hash = bat_priv->orig_hash;
72 struct hlist_head *head;
73 struct batadv_orig_node *orig_node, *orig_node_tmp = NULL;
74 int index;
75
76 if (!hash)
77 return NULL;
78
79 index = batadv_choose_orig(data, hash->size);
80 head = &hash->table[index];
81
82 rcu_read_lock();
83 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
84 if (!batadv_compare_eth(orig_node, data))
85 continue;
86
87 if (!kref_get_unless_zero(&orig_node->refcount))
88 continue;
89
90 orig_node_tmp = orig_node;
91 break;
92 }
93 rcu_read_unlock();
94
95 return orig_node_tmp;
96}
97
Sven Eckelmann03fc7f82012-05-12 18:34:00 +020098static void batadv_purge_orig(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200100/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100101 * batadv_compare_orig() - comparing function used in the originator hash table
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +0100102 * @node: node in the local table
103 * @data2: second object to compare the node to
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200104 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100105 * Return: true if they are the same originator
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200106 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +0100107bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +0200108{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200109 const void *data1 = container_of(node, struct batadv_orig_node,
110 hash_entry);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +0200111
dingtianhong323813e2013-12-26 19:40:39 +0800112 return batadv_compare_eth(data1, data2);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +0200113}
114
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200115/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100116 * batadv_orig_node_vlan_get() - get an orig_node_vlan object
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200117 * @orig_node: the originator serving the VLAN
118 * @vid: the VLAN identifier
119 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200120 * Return: the vlan object identified by vid and belonging to orig_node or NULL
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200121 * if it does not exist.
122 */
123struct batadv_orig_node_vlan *
124batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
125 unsigned short vid)
126{
127 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
128
129 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800130 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200131 if (tmp->vid != vid)
132 continue;
133
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100134 if (!kref_get_unless_zero(&tmp->refcount))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200135 continue;
136
137 vlan = tmp;
138
139 break;
140 }
141 rcu_read_unlock();
142
143 return vlan;
144}
145
146/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100147 * batadv_orig_node_vlan_new() - search and possibly create an orig_node_vlan
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200148 * object
149 * @orig_node: the originator serving the VLAN
150 * @vid: the VLAN identifier
151 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200152 * Return: NULL in case of failure or the vlan object identified by vid and
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200153 * belonging to orig_node otherwise. The object is created and added to the list
154 * if it does not exist.
155 *
156 * The object is returned with refcounter increased by 1.
157 */
158struct batadv_orig_node_vlan *
159batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
160 unsigned short vid)
161{
162 struct batadv_orig_node_vlan *vlan;
163
164 spin_lock_bh(&orig_node->vlan_list_lock);
165
166 /* first look if an object for this vid already exists */
167 vlan = batadv_orig_node_vlan_get(orig_node, vid);
168 if (vlan)
169 goto out;
170
171 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
172 if (!vlan)
173 goto out;
174
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100175 kref_init(&vlan->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200176 vlan->vid = vid;
177
Sven Eckelmann09537d12016-07-15 17:39:16 +0200178 kref_get(&vlan->refcount);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800179 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200180
181out:
182 spin_unlock_bh(&orig_node->vlan_list_lock);
183
184 return vlan;
185}
186
187/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100188 * batadv_orig_node_vlan_release() - release originator-vlan object from lists
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100189 * and queue for free after rcu grace period
190 * @ref: kref pointer of the originator-vlan object
191 */
192static void batadv_orig_node_vlan_release(struct kref *ref)
193{
194 struct batadv_orig_node_vlan *orig_vlan;
195
196 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
197
198 kfree_rcu(orig_vlan, rcu);
199}
200
201/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100202 * batadv_orig_node_vlan_put() - decrement the refcounter and possibly release
Sven Eckelmann21754e22016-01-17 11:01:24 +0100203 * the originator-vlan object
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200204 * @orig_vlan: the originator-vlan object to release
205 */
Sven Eckelmann21754e22016-01-17 11:01:24 +0100206void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200207{
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100208 kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200209}
210
Sven Eckelmannff15c272017-12-02 19:51:53 +0100211/**
212 * batadv_originator_init() - Initialize all originator structures
213 * @bat_priv: the bat priv with all the soft interface information
214 *
215 * Return: 0 on success or negative error number in case of failure
216 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200217int batadv_originator_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218{
219 if (bat_priv->orig_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200220 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000221
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200222 bat_priv->orig_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000223
224 if (!bat_priv->orig_hash)
225 goto err;
226
Antonio Quartullidec05072012-11-10 11:00:32 +0100227 batadv_hash_set_lock_class(bat_priv->orig_hash,
228 &batadv_orig_hash_lock_class_key);
229
Antonio Quartulli72414442012-12-25 13:14:37 +0100230 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
231 queue_delayed_work(batadv_event_workqueue,
232 &bat_priv->orig_work,
233 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
234
Sven Eckelmann5346c352012-05-05 13:27:28 +0200235 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000236
237err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200238 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239}
240
Simon Wunderlich89652332013-11-13 19:14:46 +0100241/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100242 * batadv_neigh_ifinfo_release() - release neigh_ifinfo from lists and queue for
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100243 * free after rcu grace period
Sven Eckelmann962c6832016-01-16 10:29:51 +0100244 * @ref: kref pointer of the neigh_ifinfo
Simon Wunderlich89652332013-11-13 19:14:46 +0100245 */
Sven Eckelmann962c6832016-01-16 10:29:51 +0100246static void batadv_neigh_ifinfo_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100247{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100248 struct batadv_neigh_ifinfo *neigh_ifinfo;
249
250 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
251
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100252 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100253 batadv_hardif_put(neigh_ifinfo->if_outgoing);
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100254
255 kfree_rcu(neigh_ifinfo, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100256}
257
258/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100259 * batadv_neigh_ifinfo_put() - decrement the refcounter and possibly release
Simon Wunderlich89652332013-11-13 19:14:46 +0100260 * the neigh_ifinfo
261 * @neigh_ifinfo: the neigh_ifinfo object to release
262 */
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100263void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
Simon Wunderlich89652332013-11-13 19:14:46 +0100264{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100265 kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
Simon Wunderlich89652332013-11-13 19:14:46 +0100266}
267
268/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100269 * batadv_hardif_neigh_release() - release hardif neigh node from lists and
Sven Eckelmannf6389692016-01-05 12:06:23 +0100270 * queue for free after rcu grace period
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100271 * @ref: kref pointer of the neigh_node
Marek Lindnercef63412015-08-04 21:09:55 +0800272 */
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100273static void batadv_hardif_neigh_release(struct kref *ref)
Marek Lindnercef63412015-08-04 21:09:55 +0800274{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100275 struct batadv_hardif_neigh_node *hardif_neigh;
276
277 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
278 refcount);
279
Sven Eckelmannf6389692016-01-05 12:06:23 +0100280 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
281 hlist_del_init_rcu(&hardif_neigh->list);
282 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
Sven Eckelmannbab7c6c2016-01-05 12:06:17 +0100283
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100284 batadv_hardif_put(hardif_neigh->if_incoming);
Sven Eckelmannf6389692016-01-05 12:06:23 +0100285 kfree_rcu(hardif_neigh, rcu);
Marek Lindnercef63412015-08-04 21:09:55 +0800286}
287
288/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100289 * batadv_hardif_neigh_put() - decrement the hardif neighbors refcounter
Sven Eckelmannf6389692016-01-05 12:06:23 +0100290 * and possibly release it
Marek Lindnercef63412015-08-04 21:09:55 +0800291 * @hardif_neigh: hardif neigh neighbor to free
292 */
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100293void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800294{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100295 kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
Marek Lindnercef63412015-08-04 21:09:55 +0800296}
297
298/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100299 * batadv_neigh_node_release() - release neigh_node from lists and queue for
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100300 * free after rcu grace period
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100301 * @ref: kref pointer of the neigh_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100302 */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100303static void batadv_neigh_node_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100304{
305 struct hlist_node *node_tmp;
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100306 struct batadv_neigh_node *neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +0100307 struct batadv_neigh_ifinfo *neigh_ifinfo;
308
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100309 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100310
311 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
312 &neigh_node->ifinfo_list, list) {
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100313 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100314 }
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800315
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100316 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
Marek Lindnercef63412015-08-04 21:09:55 +0800317
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100318 batadv_hardif_put(neigh_node->if_incoming);
Simon Wunderlich89652332013-11-13 19:14:46 +0100319
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100320 kfree_rcu(neigh_node, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100321}
322
323/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100324 * batadv_neigh_node_put() - decrement the neighbors refcounter and possibly
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100325 * release it
Simon Wunderlich89652332013-11-13 19:14:46 +0100326 * @neigh_node: neigh neighbor to free
327 */
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100328void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000329{
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100330 kref_put(&neigh_node->refcount, batadv_neigh_node_release);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000331}
332
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100333/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100334 * batadv_orig_router_get() - router to the originator depending on iface
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100335 * @orig_node: the orig node for the router
336 * @if_outgoing: the interface where the payload packet has been received or
337 * the OGM should be sent to
338 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200339 * Return: the neighbor which should be router for this orig_node/iface.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100340 *
341 * The object is returned with refcounter increased by 1.
342 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200343struct batadv_neigh_node *
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100344batadv_orig_router_get(struct batadv_orig_node *orig_node,
345 const struct batadv_hard_iface *if_outgoing)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000346{
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100347 struct batadv_orig_ifinfo *orig_ifinfo;
348 struct batadv_neigh_node *router = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000349
350 rcu_read_lock();
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100351 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
352 if (orig_ifinfo->if_outgoing != if_outgoing)
353 continue;
354
355 router = rcu_dereference(orig_ifinfo->router);
356 break;
357 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000358
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100359 if (router && !kref_get_unless_zero(&router->refcount))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000360 router = NULL;
361
362 rcu_read_unlock();
363 return router;
364}
365
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200366/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100367 * batadv_orig_ifinfo_get() - find the ifinfo from an orig_node
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100368 * @orig_node: the orig node to be queried
369 * @if_outgoing: the interface for which the ifinfo should be acquired
370 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200371 * Return: the requested orig_ifinfo or NULL if not found.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100372 *
373 * The object is returned with refcounter increased by 1.
374 */
375struct batadv_orig_ifinfo *
376batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
377 struct batadv_hard_iface *if_outgoing)
378{
379 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
380
381 rcu_read_lock();
382 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
383 list) {
384 if (tmp->if_outgoing != if_outgoing)
385 continue;
386
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100387 if (!kref_get_unless_zero(&tmp->refcount))
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100388 continue;
389
390 orig_ifinfo = tmp;
391 break;
392 }
393 rcu_read_unlock();
394
395 return orig_ifinfo;
396}
397
398/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100399 * batadv_orig_ifinfo_new() - search and possibly create an orig_ifinfo object
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100400 * @orig_node: the orig node to be queried
401 * @if_outgoing: the interface for which the ifinfo should be acquired
402 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200403 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100404 * interface otherwise. The object is created and added to the list
405 * if it does not exist.
406 *
407 * The object is returned with refcounter increased by 1.
408 */
409struct batadv_orig_ifinfo *
410batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
411 struct batadv_hard_iface *if_outgoing)
412{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200413 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100414 unsigned long reset_time;
415
416 spin_lock_bh(&orig_node->neigh_list_lock);
417
418 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
419 if (orig_ifinfo)
420 goto out;
421
422 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
423 if (!orig_ifinfo)
424 goto out;
425
Sven Eckelmann17a86912016-04-11 13:06:40 +0200426 if (if_outgoing != BATADV_IF_DEFAULT)
427 kref_get(&if_outgoing->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100428
429 reset_time = jiffies - 1;
430 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
431 orig_ifinfo->batman_seqno_reset = reset_time;
432 orig_ifinfo->if_outgoing = if_outgoing;
433 INIT_HLIST_NODE(&orig_ifinfo->list);
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100434 kref_init(&orig_ifinfo->refcount);
Sven Eckelmannf257b992016-07-15 17:39:17 +0200435
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100436 kref_get(&orig_ifinfo->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100437 hlist_add_head_rcu(&orig_ifinfo->list,
438 &orig_node->ifinfo_list);
439out:
440 spin_unlock_bh(&orig_node->neigh_list_lock);
441 return orig_ifinfo;
442}
443
444/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100445 * batadv_neigh_ifinfo_get() - find the ifinfo from an neigh_node
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200446 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100447 * @if_outgoing: the interface for which the ifinfo should be acquired
448 *
449 * The object is returned with refcounter increased by 1.
450 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200451 * Return: the requested neigh_ifinfo or NULL if not found
Simon Wunderlich89652332013-11-13 19:14:46 +0100452 */
453struct batadv_neigh_ifinfo *
454batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
455 struct batadv_hard_iface *if_outgoing)
456{
457 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
458 *tmp_neigh_ifinfo;
459
460 rcu_read_lock();
461 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
462 list) {
463 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
464 continue;
465
Sven Eckelmann962c6832016-01-16 10:29:51 +0100466 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +0100467 continue;
468
469 neigh_ifinfo = tmp_neigh_ifinfo;
470 break;
471 }
472 rcu_read_unlock();
473
474 return neigh_ifinfo;
475}
476
477/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100478 * batadv_neigh_ifinfo_new() - search and possibly create an neigh_ifinfo object
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200479 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100480 * @if_outgoing: the interface for which the ifinfo should be acquired
481 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200482 * Return: NULL in case of failure or the neigh_ifinfo object for the
Simon Wunderlich89652332013-11-13 19:14:46 +0100483 * if_outgoing interface otherwise. The object is created and added to the list
484 * if it does not exist.
485 *
486 * The object is returned with refcounter increased by 1.
487 */
488struct batadv_neigh_ifinfo *
489batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
490 struct batadv_hard_iface *if_outgoing)
491{
492 struct batadv_neigh_ifinfo *neigh_ifinfo;
493
494 spin_lock_bh(&neigh->ifinfo_lock);
495
496 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
497 if (neigh_ifinfo)
498 goto out;
499
500 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
501 if (!neigh_ifinfo)
502 goto out;
503
Sven Eckelmann17a86912016-04-11 13:06:40 +0200504 if (if_outgoing)
505 kref_get(&if_outgoing->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100506
507 INIT_HLIST_NODE(&neigh_ifinfo->list);
Sven Eckelmann962c6832016-01-16 10:29:51 +0100508 kref_init(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100509 neigh_ifinfo->if_outgoing = if_outgoing;
510
Sven Eckelmann2e774ac2016-07-15 17:39:19 +0200511 kref_get(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100512 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
513
514out:
515 spin_unlock_bh(&neigh->ifinfo_lock);
516
517 return neigh_ifinfo;
518}
519
520/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100521 * batadv_neigh_node_get() - retrieve a neighbour from the list
Marek Lindnered292662015-08-04 23:31:44 +0800522 * @orig_node: originator which the neighbour belongs to
523 * @hard_iface: the interface where this neighbour is connected to
524 * @addr: the address of the neighbour
525 *
526 * Looks for and possibly returns a neighbour belonging to this originator list
527 * which is connected through the provided hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200528 *
529 * Return: neighbor when found. Othwerwise NULL
Marek Lindnered292662015-08-04 23:31:44 +0800530 */
531static struct batadv_neigh_node *
532batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
533 const struct batadv_hard_iface *hard_iface,
534 const u8 *addr)
535{
536 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
537
538 rcu_read_lock();
539 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
540 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
541 continue;
542
543 if (tmp_neigh_node->if_incoming != hard_iface)
544 continue;
545
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100546 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnered292662015-08-04 23:31:44 +0800547 continue;
548
549 res = tmp_neigh_node;
550 break;
551 }
552 rcu_read_unlock();
553
554 return res;
555}
556
557/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100558 * batadv_hardif_neigh_create() - create a hardif neighbour node
Marek Lindnercef63412015-08-04 21:09:55 +0800559 * @hard_iface: the interface this neighbour is connected to
560 * @neigh_addr: the interface address of the neighbour to retrieve
Linus Lüssing3111bee2016-08-07 12:34:19 +0200561 * @orig_node: originator object representing the neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800562 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200563 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800564 */
565static struct batadv_hardif_neigh_node *
566batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200567 const u8 *neigh_addr,
568 struct batadv_orig_node *orig_node)
Marek Lindnercef63412015-08-04 21:09:55 +0800569{
Marek Lindner8248a4c2015-08-04 21:09:56 +0800570 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200571 struct batadv_hardif_neigh_node *hardif_neigh;
Marek Lindnercef63412015-08-04 21:09:55 +0800572
573 spin_lock_bh(&hard_iface->neigh_list_lock);
574
575 /* check if neighbor hasn't been added in the meantime */
576 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
577 if (hardif_neigh)
578 goto out;
579
Marek Lindnercef63412015-08-04 21:09:55 +0800580 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
Sven Eckelmann17a86912016-04-11 13:06:40 +0200581 if (!hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800582 goto out;
Marek Lindnercef63412015-08-04 21:09:55 +0800583
Sven Eckelmann17a86912016-04-11 13:06:40 +0200584 kref_get(&hard_iface->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800585 INIT_HLIST_NODE(&hardif_neigh->list);
586 ether_addr_copy(hardif_neigh->addr, neigh_addr);
Linus Lüssing3111bee2016-08-07 12:34:19 +0200587 ether_addr_copy(hardif_neigh->orig, orig_node->orig);
Marek Lindnercef63412015-08-04 21:09:55 +0800588 hardif_neigh->if_incoming = hard_iface;
589 hardif_neigh->last_seen = jiffies;
590
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100591 kref_init(&hardif_neigh->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800592
Antonio Quartulli29824a52016-05-25 23:27:31 +0800593 if (bat_priv->algo_ops->neigh.hardif_init)
594 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
Marek Lindner8248a4c2015-08-04 21:09:56 +0800595
Sven Eckelmann9ca488d2016-09-29 17:22:58 +0200596 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
Marek Lindnercef63412015-08-04 21:09:55 +0800597
598out:
599 spin_unlock_bh(&hard_iface->neigh_list_lock);
600 return hardif_neigh;
601}
602
603/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100604 * batadv_hardif_neigh_get_or_create() - retrieve or create a hardif neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800605 * node
606 * @hard_iface: the interface this neighbour is connected to
607 * @neigh_addr: the interface address of the neighbour to retrieve
Linus Lüssing3111bee2016-08-07 12:34:19 +0200608 * @orig_node: originator object representing the neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800609 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200610 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800611 */
612static struct batadv_hardif_neigh_node *
613batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200614 const u8 *neigh_addr,
615 struct batadv_orig_node *orig_node)
Marek Lindnercef63412015-08-04 21:09:55 +0800616{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200617 struct batadv_hardif_neigh_node *hardif_neigh;
Marek Lindnercef63412015-08-04 21:09:55 +0800618
619 /* first check without locking to avoid the overhead */
620 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
621 if (hardif_neigh)
622 return hardif_neigh;
623
Linus Lüssing3111bee2016-08-07 12:34:19 +0200624 return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
Marek Lindnercef63412015-08-04 21:09:55 +0800625}
626
627/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100628 * batadv_hardif_neigh_get() - retrieve a hardif neighbour from the list
Marek Lindnercef63412015-08-04 21:09:55 +0800629 * @hard_iface: the interface where this neighbour is connected to
630 * @neigh_addr: the address of the neighbour
631 *
632 * Looks for and possibly returns a neighbour belonging to this hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200633 *
634 * Return: neighbor when found. Othwerwise NULL
Marek Lindnercef63412015-08-04 21:09:55 +0800635 */
636struct batadv_hardif_neigh_node *
637batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
638 const u8 *neigh_addr)
639{
640 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
641
642 rcu_read_lock();
643 hlist_for_each_entry_rcu(tmp_hardif_neigh,
644 &hard_iface->neigh_list, list) {
645 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
646 continue;
647
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100648 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
Marek Lindnercef63412015-08-04 21:09:55 +0800649 continue;
650
651 hardif_neigh = tmp_hardif_neigh;
652 break;
653 }
654 rcu_read_unlock();
655
656 return hardif_neigh;
657}
658
659/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100660 * batadv_neigh_node_create() - create a neigh node object
Marek Lindner3f32f8a2015-07-26 04:59:15 +0800661 * @orig_node: originator object representing the neighbour
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200662 * @hard_iface: the interface where the neighbour is connected to
663 * @neigh_addr: the mac address of the neighbour interface
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200664 *
665 * Allocates a new neigh_node object and initialises all the generic fields.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200666 *
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800667 * Return: the neighbour node if found or created or NULL otherwise.
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200668 */
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800669static struct batadv_neigh_node *
670batadv_neigh_node_create(struct batadv_orig_node *orig_node,
671 struct batadv_hard_iface *hard_iface,
672 const u8 *neigh_addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000673{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200674 struct batadv_neigh_node *neigh_node;
Marek Lindnercef63412015-08-04 21:09:55 +0800675 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000676
Linus Lüssinge1237052016-01-07 08:11:12 +0100677 spin_lock_bh(&orig_node->neigh_list_lock);
678
Marek Lindner741aa062015-07-26 04:57:43 +0800679 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
680 if (neigh_node)
681 goto out;
682
Marek Lindnercef63412015-08-04 21:09:55 +0800683 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200684 neigh_addr, orig_node);
Marek Lindnercef63412015-08-04 21:09:55 +0800685 if (!hardif_neigh)
686 goto out;
687
Sven Eckelmann704509b2011-05-14 23:14:54 +0200688 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000689 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800690 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000691
Marek Lindner9591a792010-12-12 21:57:11 +0000692 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlich89652332013-11-13 19:14:46 +0100693 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
694 spin_lock_init(&neigh_node->ifinfo_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000695
Sven Eckelmann17a86912016-04-11 13:06:40 +0200696 kref_get(&hard_iface->refcount);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100697 ether_addr_copy(neigh_node->addr, neigh_addr);
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200698 neigh_node->if_incoming = hard_iface;
699 neigh_node->orig_node = orig_node;
Marek Lindnere48474e2016-03-11 16:01:09 +0100700 neigh_node->last_seen = jiffies;
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200701
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100702 /* increment unique neighbor refcount */
703 kref_get(&hardif_neigh->refcount);
704 neigh_node->hardif_neigh = hardif_neigh;
705
Marek Lindner1605d0d2011-02-18 12:28:11 +0000706 /* extra reference for return */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100707 kref_init(&neigh_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000708
Sven Eckelmann84274452016-07-15 17:39:20 +0200709 kref_get(&neigh_node->refcount);
Marek Lindner741aa062015-07-26 04:57:43 +0800710 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
Marek Lindner741aa062015-07-26 04:57:43 +0800711
712 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
713 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
714 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
715
Marek Lindner7ae8b282012-03-01 15:35:21 +0800716out:
Linus Lüssinge1237052016-01-07 08:11:12 +0100717 spin_unlock_bh(&orig_node->neigh_list_lock);
718
Marek Lindnercef63412015-08-04 21:09:55 +0800719 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100720 batadv_hardif_neigh_put(hardif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000721 return neigh_node;
722}
723
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100724/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100725 * batadv_neigh_node_get_or_create() - retrieve or create a neigh node object
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800726 * @orig_node: originator object representing the neighbour
727 * @hard_iface: the interface where the neighbour is connected to
728 * @neigh_addr: the mac address of the neighbour interface
729 *
730 * Return: the neighbour node if found or created or NULL otherwise.
731 */
732struct batadv_neigh_node *
733batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
734 struct batadv_hard_iface *hard_iface,
735 const u8 *neigh_addr)
736{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200737 struct batadv_neigh_node *neigh_node;
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800738
739 /* first check without locking to avoid the overhead */
740 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
741 if (neigh_node)
742 return neigh_node;
743
744 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
745}
746
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200747#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800748/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100749 * batadv_hardif_neigh_seq_print_text() - print the single hop neighbour list
Marek Lindner75874052015-08-04 21:09:57 +0800750 * @seq: neighbour table seq_file struct
751 * @offset: not used
752 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200753 * Return: always 0
Marek Lindner75874052015-08-04 21:09:57 +0800754 */
755int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
756{
757 struct net_device *net_dev = (struct net_device *)seq->private;
758 struct batadv_priv *bat_priv = netdev_priv(net_dev);
759 struct batadv_hard_iface *primary_if;
760
761 primary_if = batadv_seq_print_text_primary_if_get(seq);
762 if (!primary_if)
763 return 0;
764
765 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
766 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
767 primary_if->net_dev->dev_addr, net_dev->name,
Antonio Quartulli29824a52016-05-25 23:27:31 +0800768 bat_priv->algo_ops->name);
Marek Lindner75874052015-08-04 21:09:57 +0800769
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100770 batadv_hardif_put(primary_if);
Marek Lindner75874052015-08-04 21:09:57 +0800771
Antonio Quartulli29824a52016-05-25 23:27:31 +0800772 if (!bat_priv->algo_ops->neigh.print) {
Marek Lindner75874052015-08-04 21:09:57 +0800773 seq_puts(seq,
774 "No printing function for this routing protocol\n");
775 return 0;
776 }
777
Antonio Quartulli29824a52016-05-25 23:27:31 +0800778 bat_priv->algo_ops->neigh.print(bat_priv, seq);
Marek Lindner75874052015-08-04 21:09:57 +0800779 return 0;
780}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200781#endif
Marek Lindner75874052015-08-04 21:09:57 +0800782
783/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100784 * batadv_hardif_neigh_dump() - Dump to netlink the neighbor infos for a
785 * specific outgoing interface
Matthias Schiffer85cf8c82016-07-03 13:31:39 +0200786 * @msg: message to dump into
787 * @cb: parameters for the dump
788 *
789 * Return: 0 or error value
790 */
791int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
792{
793 struct net *net = sock_net(cb->skb->sk);
794 struct net_device *soft_iface;
795 struct net_device *hard_iface = NULL;
796 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
797 struct batadv_priv *bat_priv;
798 struct batadv_hard_iface *primary_if = NULL;
799 int ret;
800 int ifindex, hard_ifindex;
801
802 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
803 if (!ifindex)
804 return -EINVAL;
805
806 soft_iface = dev_get_by_index(net, ifindex);
807 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
808 ret = -ENODEV;
809 goto out;
810 }
811
812 bat_priv = netdev_priv(soft_iface);
813
814 primary_if = batadv_primary_if_get_selected(bat_priv);
815 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
816 ret = -ENOENT;
817 goto out;
818 }
819
820 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
821 BATADV_ATTR_HARD_IFINDEX);
822 if (hard_ifindex) {
823 hard_iface = dev_get_by_index(net, hard_ifindex);
824 if (hard_iface)
825 hardif = batadv_hardif_get_by_netdev(hard_iface);
826
827 if (!hardif) {
828 ret = -ENODEV;
829 goto out;
830 }
831
832 if (hardif->soft_iface != soft_iface) {
833 ret = -ENOENT;
834 goto out;
835 }
836 }
837
838 if (!bat_priv->algo_ops->neigh.dump) {
839 ret = -EOPNOTSUPP;
840 goto out;
841 }
842
843 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
844
845 ret = msg->len;
846
847 out:
848 if (hardif)
849 batadv_hardif_put(hardif);
850 if (hard_iface)
851 dev_put(hard_iface);
852 if (primary_if)
853 batadv_hardif_put(primary_if);
854 if (soft_iface)
855 dev_put(soft_iface);
856
857 return ret;
858}
859
860/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100861 * batadv_orig_ifinfo_release() - release orig_ifinfo from lists and queue for
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100862 * free after rcu grace period
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100863 * @ref: kref pointer of the orig_ifinfo
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100864 */
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100865static void batadv_orig_ifinfo_release(struct kref *ref)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100866{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100867 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100868 struct batadv_neigh_node *router;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100869
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100870 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
871
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100872 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100873 batadv_hardif_put(orig_ifinfo->if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100874
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100875 /* this is the last reference to this object */
876 router = rcu_dereference_protected(orig_ifinfo->router, true);
877 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100878 batadv_neigh_node_put(router);
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100879
880 kfree_rcu(orig_ifinfo, rcu);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100881}
882
883/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100884 * batadv_orig_ifinfo_put() - decrement the refcounter and possibly release
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100885 * the orig_ifinfo
886 * @orig_ifinfo: the orig_ifinfo object to release
887 */
Sven Eckelmann35f94772016-01-17 11:01:13 +0100888void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100889{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100890 kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100891}
892
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100893/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100894 * batadv_orig_node_free_rcu() - free the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100895 * @rcu: rcu pointer of the orig_node
896 */
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200897static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000898{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200899 struct batadv_orig_node *orig_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000900
Sven Eckelmann56303d32012-06-05 22:31:31 +0200901 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000902
Linus Lüssing60432d72014-02-15 17:47:51 +0100903 batadv_mcast_purge_orig(orig_node);
904
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200905 batadv_frag_purge_orig(orig_node, NULL);
906
Antonio Quartullia73105b2011-04-27 14:27:44 +0200907 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000908 kfree(orig_node);
909}
910
Linus Lüssing72822222013-04-15 21:43:29 +0800911/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100912 * batadv_orig_node_release() - release orig_node from lists and queue for
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100913 * free after rcu grace period
Sven Eckelmann7c124392016-01-16 10:29:56 +0100914 * @ref: kref pointer of the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100915 */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100916static void batadv_orig_node_release(struct kref *ref)
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100917{
918 struct hlist_node *node_tmp;
919 struct batadv_neigh_node *neigh_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +0100920 struct batadv_orig_node *orig_node;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100921 struct batadv_orig_ifinfo *orig_ifinfo;
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200922 struct batadv_orig_node_vlan *vlan;
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200923 struct batadv_orig_ifinfo *last_candidate;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100924
Sven Eckelmann7c124392016-01-16 10:29:56 +0100925 orig_node = container_of(ref, struct batadv_orig_node, refcount);
926
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100927 spin_lock_bh(&orig_node->neigh_list_lock);
928
929 /* for all neighbors towards this originator ... */
930 hlist_for_each_entry_safe(neigh_node, node_tmp,
931 &orig_node->neigh_list, list) {
932 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100933 batadv_neigh_node_put(neigh_node);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100934 }
935
936 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
937 &orig_node->ifinfo_list, list) {
938 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +0100939 batadv_orig_ifinfo_put(orig_ifinfo);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100940 }
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200941
942 last_candidate = orig_node->last_bonding_candidate;
943 orig_node->last_bonding_candidate = NULL;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100944 spin_unlock_bh(&orig_node->neigh_list_lock);
945
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200946 if (last_candidate)
947 batadv_orig_ifinfo_put(last_candidate);
948
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200949 spin_lock_bh(&orig_node->vlan_list_lock);
950 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
951 hlist_del_rcu(&vlan->list);
952 batadv_orig_node_vlan_put(vlan);
953 }
954 spin_unlock_bh(&orig_node->vlan_list_lock);
955
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100956 /* Free nc_nodes */
957 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
958
959 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
960}
961
962/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100963 * batadv_orig_node_put() - decrement the orig node refcounter and possibly
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100964 * release it
Linus Lüssing72822222013-04-15 21:43:29 +0800965 * @orig_node: the orig node to free
966 */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100967void batadv_orig_node_put(struct batadv_orig_node *orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000968{
Sven Eckelmann7c124392016-01-16 10:29:56 +0100969 kref_put(&orig_node->refcount, batadv_orig_node_release);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000970}
971
Sven Eckelmannff15c272017-12-02 19:51:53 +0100972/**
973 * batadv_originator_free() - Free all originator structures
974 * @bat_priv: the bat priv with all the soft interface information
975 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200976void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000977{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200978 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800979 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000980 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000981 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200982 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200983 u32 i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000984
985 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000986 return;
987
988 cancel_delayed_work_sync(&bat_priv->orig_work);
989
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000990 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000991
992 for (i = 0; i < hash->size; i++) {
993 head = &hash->table[i];
994 list_lock = &hash->list_locks[i];
995
996 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800997 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000998 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800999 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann5d967312016-01-17 11:01:09 +01001000 batadv_orig_node_put(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +00001001 }
1002 spin_unlock_bh(list_lock);
1003 }
1004
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001005 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001006}
1007
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001008/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001009 * batadv_orig_node_new() - creates a new orig_node
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001010 * @bat_priv: the bat priv with all the soft interface information
1011 * @addr: the mac address of the originator
1012 *
1013 * Creates a new originator object and initialise all the generic fields.
1014 * The new object is not added to the originator list.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001015 *
1016 * Return: the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001017 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001018struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001019 const u8 *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001020{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001021 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001022 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001023 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001024 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001025
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001026 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1027 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028
Sven Eckelmann704509b2011-05-14 23:14:54 +02001029 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001030 if (!orig_node)
1031 return NULL;
1032
Marek Lindner9591a792010-12-12 21:57:11 +00001033 INIT_HLIST_HEAD(&orig_node->neigh_list);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +08001034 INIT_HLIST_HEAD(&orig_node->vlan_list);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001035 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001036 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +00001037 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001038 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02001039 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001040 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001041
Martin Hundebølld56b1702013-01-25 11:12:39 +01001042 batadv_nc_init_orig(orig_node);
1043
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001044 /* extra reference for return */
Sven Eckelmann7c124392016-01-16 10:29:56 +01001045 kref_init(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001046
Marek Lindner16b1aba2011-01-19 20:01:42 +00001047 orig_node->bat_priv = bat_priv;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001048 ether_addr_copy(orig_node->orig, addr);
Antonio Quartulli785ea112011-11-23 11:35:44 +01001049 batadv_dat_init_orig_node_addr(orig_node);
Antonio Quartullic8c991b2011-07-07 01:40:57 +02001050 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001051 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001052 orig_node->tt_buff_len = 0;
Linus Lüssing2c667a32014-10-30 06:23:40 +01001053 orig_node->last_seen = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001054 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
1055 orig_node->bcast_seqno_reset = reset_time;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001056
Linus Lüssing60432d72014-02-15 17:47:51 +01001057#ifdef CONFIG_BATMAN_ADV_MCAST
1058 orig_node->mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001059 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
1060 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
1061 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
1062 spin_lock_init(&orig_node->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001063#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001064
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001065 /* create a vlan object for the "untagged" LAN */
1066 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
1067 if (!vlan)
1068 goto free_orig_node;
1069 /* batadv_orig_node_vlan_new() increases the refcounter.
1070 * Immediately release vlan since it is not needed anymore in this
1071 * context
1072 */
Sven Eckelmann21754e22016-01-17 11:01:24 +01001073 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001074
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001075 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
Sven Eckelmann176e5b72016-07-27 12:31:07 +02001076 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001077 spin_lock_init(&orig_node->fragments[i].lock);
1078 orig_node->fragments[i].size = 0;
1079 }
1080
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001081 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001082free_orig_node:
1083 kfree(orig_node);
1084 return NULL;
1085}
1086
Simon Wunderlich89652332013-11-13 19:14:46 +01001087/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001088 * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor
Simon Wunderlich709de132014-03-26 15:46:24 +01001089 * @bat_priv: the bat priv with all the soft interface information
1090 * @neigh: orig node which is to be checked
1091 */
1092static void
1093batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1094 struct batadv_neigh_node *neigh)
1095{
1096 struct batadv_neigh_ifinfo *neigh_ifinfo;
1097 struct batadv_hard_iface *if_outgoing;
1098 struct hlist_node *node_tmp;
1099
1100 spin_lock_bh(&neigh->ifinfo_lock);
1101
1102 /* for all ifinfo objects for this neighinator */
1103 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1104 &neigh->ifinfo_list, list) {
1105 if_outgoing = neigh_ifinfo->if_outgoing;
1106
1107 /* always keep the default interface */
1108 if (if_outgoing == BATADV_IF_DEFAULT)
1109 continue;
1110
1111 /* don't purge if the interface is not (going) down */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001112 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1113 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1114 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
Simon Wunderlich709de132014-03-26 15:46:24 +01001115 continue;
1116
1117 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1118 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1119 neigh->addr, if_outgoing->net_dev->name);
1120
1121 hlist_del_rcu(&neigh_ifinfo->list);
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001122 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich709de132014-03-26 15:46:24 +01001123 }
1124
1125 spin_unlock_bh(&neigh->ifinfo_lock);
1126}
1127
1128/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001129 * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001130 * @bat_priv: the bat priv with all the soft interface information
1131 * @orig_node: orig node which is to be checked
1132 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001133 * Return: true if any ifinfo entry was purged, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001134 */
1135static bool
1136batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1137 struct batadv_orig_node *orig_node)
1138{
1139 struct batadv_orig_ifinfo *orig_ifinfo;
1140 struct batadv_hard_iface *if_outgoing;
1141 struct hlist_node *node_tmp;
1142 bool ifinfo_purged = false;
1143
1144 spin_lock_bh(&orig_node->neigh_list_lock);
1145
1146 /* for all ifinfo objects for this originator */
1147 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1148 &orig_node->ifinfo_list, list) {
1149 if_outgoing = orig_ifinfo->if_outgoing;
1150
1151 /* always keep the default interface */
1152 if (if_outgoing == BATADV_IF_DEFAULT)
1153 continue;
1154
1155 /* don't purge if the interface is not (going) down */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001156 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1157 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1158 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001159 continue;
1160
1161 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1162 "router/ifinfo purge: originator %pM, iface: %s\n",
1163 orig_node->orig, if_outgoing->net_dev->name);
1164
1165 ifinfo_purged = true;
1166
1167 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001168 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001169 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1170 orig_node->last_bonding_candidate = NULL;
Sven Eckelmann35f94772016-01-17 11:01:13 +01001171 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001172 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001173 }
1174
1175 spin_unlock_bh(&orig_node->neigh_list_lock);
1176
1177 return ifinfo_purged;
1178}
1179
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001180/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001181 * batadv_purge_orig_neighbors() - purges neighbors from originator
Simon Wunderlich89652332013-11-13 19:14:46 +01001182 * @bat_priv: the bat priv with all the soft interface information
1183 * @orig_node: orig node which is to be checked
1184 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001185 * Return: true if any neighbor was purged, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001186 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001187static bool
1188batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
Simon Wunderlich89652332013-11-13 19:14:46 +01001189 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001190{
Sasha Levinb67bfe02013-02-27 17:06:00 -08001191 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001192 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001193 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +08001194 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001195 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001196
Marek Lindnerf987ed62010-12-12 21:57:12 +00001197 spin_lock_bh(&orig_node->neigh_list_lock);
1198
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001199 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001200 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +00001201 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001202 last_seen = neigh_node->last_seen;
1203 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001204
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001205 if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) ||
1206 if_incoming->if_status == BATADV_IF_INACTIVE ||
1207 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1208 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) {
1209 if (if_incoming->if_status == BATADV_IF_INACTIVE ||
1210 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1211 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001212 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001213 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1214 orig_node->orig, neigh_node->addr,
1215 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001216 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001217 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001218 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1219 orig_node->orig, neigh_node->addr,
1220 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001221
1222 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +00001223
Marek Lindnerf987ed62010-12-12 21:57:12 +00001224 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001225 batadv_neigh_node_put(neigh_node);
Simon Wunderlich709de132014-03-26 15:46:24 +01001226 } else {
1227 /* only necessary if not the whole neighbor is to be
1228 * deleted, but some interface has been removed.
1229 */
1230 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001231 }
1232 }
Marek Lindnerf987ed62010-12-12 21:57:12 +00001233
1234 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001235 return neigh_purged;
1236}
1237
Simon Wunderlich89652332013-11-13 19:14:46 +01001238/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001239 * batadv_find_best_neighbor() - finds the best neighbor after purging
Simon Wunderlich89652332013-11-13 19:14:46 +01001240 * @bat_priv: the bat priv with all the soft interface information
1241 * @orig_node: orig node which is to be checked
1242 * @if_outgoing: the interface for which the metric should be compared
1243 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001244 * Return: the current best neighbor, with refcount increased.
Simon Wunderlich89652332013-11-13 19:14:46 +01001245 */
1246static struct batadv_neigh_node *
1247batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1248 struct batadv_orig_node *orig_node,
1249 struct batadv_hard_iface *if_outgoing)
1250{
1251 struct batadv_neigh_node *best = NULL, *neigh;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001252 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Simon Wunderlich89652332013-11-13 19:14:46 +01001253
1254 rcu_read_lock();
1255 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
Antonio Quartulli29824a52016-05-25 23:27:31 +08001256 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1257 if_outgoing) <= 0))
Simon Wunderlich89652332013-11-13 19:14:46 +01001258 continue;
1259
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001260 if (!kref_get_unless_zero(&neigh->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +01001261 continue;
1262
1263 if (best)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001264 batadv_neigh_node_put(best);
Simon Wunderlich89652332013-11-13 19:14:46 +01001265
1266 best = neigh;
1267 }
1268 rcu_read_unlock();
1269
1270 return best;
1271}
1272
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001273/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001274 * batadv_purge_orig_node() - purges obsolete information from an orig_node
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001275 * @bat_priv: the bat priv with all the soft interface information
1276 * @orig_node: orig node which is to be checked
1277 *
1278 * This function checks if the orig_node or substructures of it have become
1279 * obsolete, and purges this information if that's the case.
1280 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001281 * Return: true if the orig_node is to be removed, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001282 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001283static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1284 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001285{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001286 struct batadv_neigh_node *best_neigh_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001287 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001288 bool changed_ifinfo, changed_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001289
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001290 if (batadv_has_timed_out(orig_node->last_seen,
1291 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001292 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001293 "Originator timeout: originator %pM, last_seen %u\n",
1294 orig_node->orig,
1295 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001296 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001297 }
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001298 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1299 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001300
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001301 if (!changed_ifinfo && !changed_neigh)
Simon Wunderlich89652332013-11-13 19:14:46 +01001302 return false;
1303
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001304 /* first for NULL ... */
Simon Wunderlich89652332013-11-13 19:14:46 +01001305 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1306 BATADV_IF_DEFAULT);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001307 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1308 best_neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001309 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001310 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001311
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001312 /* ... then for all other interfaces. */
1313 rcu_read_lock();
1314 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1315 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1316 continue;
1317
1318 if (hard_iface->soft_iface != bat_priv->soft_iface)
1319 continue;
1320
Sven Eckelmann27353442016-03-05 16:09:16 +01001321 if (!kref_get_unless_zero(&hard_iface->refcount))
1322 continue;
1323
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001324 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1325 orig_node,
1326 hard_iface);
1327 batadv_update_route(bat_priv, orig_node, hard_iface,
1328 best_neigh_node);
1329 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001330 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmann27353442016-03-05 16:09:16 +01001331
1332 batadv_hardif_put(hard_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001333 }
1334 rcu_read_unlock();
1335
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001336 return false;
1337}
1338
Sven Eckelmann3b1709d2018-07-07 21:46:11 +02001339/**
1340 * batadv_purge_orig_ref() - Purge all outdated originators
1341 * @bat_priv: the bat priv with all the soft interface information
1342 */
1343void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001344{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001345 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001346 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001347 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001348 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001349 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001350 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001351
1352 if (!hash)
1353 return;
1354
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001355 /* for all origins... */
1356 for (i = 0; i < hash->size; i++) {
1357 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001358 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001359
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001360 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001361 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +00001362 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001363 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +08001364 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001365 hlist_del_rcu(&orig_node->hash_entry);
Linus Lüssing9d31b3c2014-12-13 23:32:15 +01001366 batadv_tt_global_del_orig(orig_node->bat_priv,
1367 orig_node, -1,
1368 "originator timed out");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001369 batadv_orig_node_put(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001370 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001371 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001372
1373 batadv_frag_purge_orig(orig_node,
1374 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001375 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001376 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001377 }
1378
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +02001379 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001380}
1381
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001382static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001383{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001384 struct delayed_work *delayed_work;
1385 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001386
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001387 delayed_work = to_delayed_work(work);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001388 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann3b1709d2018-07-07 21:46:11 +02001389 batadv_purge_orig_ref(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +01001390 queue_delayed_work(batadv_event_workqueue,
1391 &bat_priv->orig_work,
1392 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001393}
1394
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001395#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmannff15c272017-12-02 19:51:53 +01001396
1397/**
1398 * batadv_orig_seq_print_text() - Print the originator table in a seq file
1399 * @seq: seq file to print on
1400 * @offset: not used
1401 *
1402 * Return: always 0
1403 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001404int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001405{
1406 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001407 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001408 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001409
Marek Lindner30da63a2012-08-03 17:15:46 +02001410 primary_if = batadv_seq_print_text_primary_if_get(seq);
1411 if (!primary_if)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001412 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001413
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001414 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001415 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001416 primary_if->net_dev->dev_addr, net_dev->name,
Antonio Quartulli29824a52016-05-25 23:27:31 +08001417 bat_priv->algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001418
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001419 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001420
Antonio Quartulli29824a52016-05-25 23:27:31 +08001421 if (!bat_priv->algo_ops->orig.print) {
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001422 seq_puts(seq,
1423 "No printing function for this routing protocol\n");
1424 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001425 }
1426
Antonio Quartulli29824a52016-05-25 23:27:31 +08001427 bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001428
Marek Lindner30da63a2012-08-03 17:15:46 +02001429 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001430}
1431
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001432/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001433 * batadv_orig_hardif_seq_print_text() - writes originator infos for a specific
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001434 * outgoing interface
1435 * @seq: debugfs table seq_file struct
1436 * @offset: not used
1437 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001438 * Return: 0
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001439 */
1440int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1441{
1442 struct net_device *net_dev = (struct net_device *)seq->private;
1443 struct batadv_hard_iface *hard_iface;
1444 struct batadv_priv *bat_priv;
1445
1446 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1447
1448 if (!hard_iface || !hard_iface->soft_iface) {
1449 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1450 goto out;
1451 }
1452
1453 bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartulli29824a52016-05-25 23:27:31 +08001454 if (!bat_priv->algo_ops->orig.print) {
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001455 seq_puts(seq,
1456 "No printing function for this routing protocol\n");
1457 goto out;
1458 }
1459
1460 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1461 seq_puts(seq, "Interface not active\n");
1462 goto out;
1463 }
1464
1465 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1466 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1467 hard_iface->net_dev->dev_addr,
Antonio Quartulli29824a52016-05-25 23:27:31 +08001468 hard_iface->soft_iface->name, bat_priv->algo_ops->name);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001469
Antonio Quartulli29824a52016-05-25 23:27:31 +08001470 bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001471
1472out:
Marek Lindner16a41422014-04-24 03:44:25 +08001473 if (hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001474 batadv_hardif_put(hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001475 return 0;
1476}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001477#endif
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001478
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001479/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001480 * batadv_orig_dump() - Dump to netlink the originator infos for a specific
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001481 * outgoing interface
1482 * @msg: message to dump into
1483 * @cb: parameters for the dump
1484 *
1485 * Return: 0 or error value
1486 */
1487int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1488{
1489 struct net *net = sock_net(cb->skb->sk);
1490 struct net_device *soft_iface;
1491 struct net_device *hard_iface = NULL;
1492 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1493 struct batadv_priv *bat_priv;
1494 struct batadv_hard_iface *primary_if = NULL;
1495 int ret;
1496 int ifindex, hard_ifindex;
1497
1498 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1499 if (!ifindex)
1500 return -EINVAL;
1501
1502 soft_iface = dev_get_by_index(net, ifindex);
1503 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1504 ret = -ENODEV;
1505 goto out;
1506 }
1507
1508 bat_priv = netdev_priv(soft_iface);
1509
1510 primary_if = batadv_primary_if_get_selected(bat_priv);
1511 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1512 ret = -ENOENT;
1513 goto out;
1514 }
1515
1516 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1517 BATADV_ATTR_HARD_IFINDEX);
1518 if (hard_ifindex) {
1519 hard_iface = dev_get_by_index(net, hard_ifindex);
1520 if (hard_iface)
1521 hardif = batadv_hardif_get_by_netdev(hard_iface);
1522
1523 if (!hardif) {
1524 ret = -ENODEV;
1525 goto out;
1526 }
1527
1528 if (hardif->soft_iface != soft_iface) {
1529 ret = -ENOENT;
1530 goto out;
1531 }
1532 }
1533
1534 if (!bat_priv->algo_ops->orig.dump) {
1535 ret = -EOPNOTSUPP;
1536 goto out;
1537 }
1538
1539 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1540
1541 ret = msg->len;
1542
1543 out:
1544 if (hardif)
1545 batadv_hardif_put(hardif);
1546 if (hard_iface)
1547 dev_put(hard_iface);
1548 if (primary_if)
1549 batadv_hardif_put(primary_if);
1550 if (soft_iface)
1551 dev_put(soft_iface);
1552
1553 return ret;
1554}