blob: aadc653ca1d80a70698d44a6d63e306c1d8d64ba [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmanncfa55c62021-01-01 00:00:01 +01002/* Copyright (C) B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
4 * Marek Lindner, Simon Wunderlich
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00007#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
9
Sven Eckelmann7c124392016-01-16 10:29:56 +010010#include <linux/atomic.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020011#include <linux/errno.h>
12#include <linux/etherdevice.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010013#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020014#include <linux/jiffies.h>
15#include <linux/kernel.h>
Sven Eckelmann90f564d2016-01-16 10:29:40 +010016#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020017#include <linux/list.h>
18#include <linux/lockdep.h>
19#include <linux/netdevice.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020020#include <linux/netlink.h>
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080021#include <linux/rculist.h>
Denys Vlasenko3326afe2017-11-19 17:59:13 +010022#include <linux/rcupdate.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020023#include <linux/skbuff.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020024#include <linux/slab.h>
25#include <linux/spinlock.h>
Denys Vlasenko3326afe2017-11-19 17:59:13 +010026#include <linux/stddef.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020027#include <linux/workqueue.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020028#include <net/sock.h>
Linus Lüssing61caf3d2019-06-11 22:58:40 +020029#include <uapi/linux/batadv_packet.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020030#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031
Sven Eckelmann01d350d2016-05-15 11:07:44 +020032#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020033#include "distributed-arp-table.h"
34#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000035#include "gateway_client.h"
36#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020037#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020038#include "log.h"
Linus Lüssing60432d72014-02-15 17:47:51 +010039#include "multicast.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020040#include "netlink.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020041#include "network-coding.h"
42#include "routing.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020043#include "soft-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020044#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045
Antonio Quartullidec05072012-11-10 11:00:32 +010046/* hash class keys */
47static struct lock_class_key batadv_orig_hash_lock_class_key;
48
Sven Eckelmannff15c272017-12-02 19:51:53 +010049/**
50 * batadv_orig_hash_find() - Find and return originator from orig_hash
51 * @bat_priv: the bat priv with all the soft interface information
52 * @data: mac address of the originator
53 *
54 * Return: orig_node (with increased refcnt), NULL on errors
55 */
Denys Vlasenko3326afe2017-11-19 17:59:13 +010056struct batadv_orig_node *
57batadv_orig_hash_find(struct batadv_priv *bat_priv, const void *data)
58{
59 struct batadv_hashtable *hash = bat_priv->orig_hash;
60 struct hlist_head *head;
61 struct batadv_orig_node *orig_node, *orig_node_tmp = NULL;
62 int index;
63
64 if (!hash)
65 return NULL;
66
67 index = batadv_choose_orig(data, hash->size);
68 head = &hash->table[index];
69
70 rcu_read_lock();
71 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
72 if (!batadv_compare_eth(orig_node, data))
73 continue;
74
75 if (!kref_get_unless_zero(&orig_node->refcount))
76 continue;
77
78 orig_node_tmp = orig_node;
79 break;
80 }
81 rcu_read_unlock();
82
83 return orig_node_tmp;
84}
85
Sven Eckelmann03fc7f82012-05-12 18:34:00 +020086static void batadv_purge_orig(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087
Sven Eckelmann62fe7102015-09-15 19:00:48 +020088/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +010089 * batadv_compare_orig() - comparing function used in the originator hash table
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010090 * @node: node in the local table
91 * @data2: second object to compare the node to
Sven Eckelmann62fe7102015-09-15 19:00:48 +020092 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +010093 * Return: true if they are the same originator
Sven Eckelmann62fe7102015-09-15 19:00:48 +020094 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010095bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020096{
Sven Eckelmann56303d32012-06-05 22:31:31 +020097 const void *data1 = container_of(node, struct batadv_orig_node,
98 hash_entry);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020099
dingtianhong323813e2013-12-26 19:40:39 +0800100 return batadv_compare_eth(data1, data2);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +0200101}
102
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200103/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100104 * batadv_orig_node_vlan_get() - get an orig_node_vlan object
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200105 * @orig_node: the originator serving the VLAN
106 * @vid: the VLAN identifier
107 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200108 * Return: the vlan object identified by vid and belonging to orig_node or NULL
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200109 * if it does not exist.
110 */
111struct batadv_orig_node_vlan *
112batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
113 unsigned short vid)
114{
115 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
116
117 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800118 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200119 if (tmp->vid != vid)
120 continue;
121
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100122 if (!kref_get_unless_zero(&tmp->refcount))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200123 continue;
124
125 vlan = tmp;
126
127 break;
128 }
129 rcu_read_unlock();
130
131 return vlan;
132}
133
134/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100135 * batadv_orig_node_vlan_new() - search and possibly create an orig_node_vlan
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200136 * object
137 * @orig_node: the originator serving the VLAN
138 * @vid: the VLAN identifier
139 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200140 * Return: NULL in case of failure or the vlan object identified by vid and
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200141 * belonging to orig_node otherwise. The object is created and added to the list
142 * if it does not exist.
143 *
144 * The object is returned with refcounter increased by 1.
145 */
146struct batadv_orig_node_vlan *
147batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
148 unsigned short vid)
149{
150 struct batadv_orig_node_vlan *vlan;
151
152 spin_lock_bh(&orig_node->vlan_list_lock);
153
154 /* first look if an object for this vid already exists */
155 vlan = batadv_orig_node_vlan_get(orig_node, vid);
156 if (vlan)
157 goto out;
158
159 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
160 if (!vlan)
161 goto out;
162
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100163 kref_init(&vlan->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200164 vlan->vid = vid;
165
Sven Eckelmann09537d12016-07-15 17:39:16 +0200166 kref_get(&vlan->refcount);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800167 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200168
169out:
170 spin_unlock_bh(&orig_node->vlan_list_lock);
171
172 return vlan;
173}
174
175/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100176 * batadv_orig_node_vlan_release() - release originator-vlan object from lists
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100177 * and queue for free after rcu grace period
178 * @ref: kref pointer of the originator-vlan object
179 */
Sven Eckelmann6340dcb2021-08-08 19:56:17 +0200180void batadv_orig_node_vlan_release(struct kref *ref)
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100181{
182 struct batadv_orig_node_vlan *orig_vlan;
183
184 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
185
186 kfree_rcu(orig_vlan, rcu);
187}
188
189/**
Sven Eckelmannff15c272017-12-02 19:51:53 +0100190 * batadv_originator_init() - Initialize all originator structures
191 * @bat_priv: the bat priv with all the soft interface information
192 *
193 * Return: 0 on success or negative error number in case of failure
194 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200195int batadv_originator_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196{
197 if (bat_priv->orig_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200198 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000199
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200200 bat_priv->orig_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201
202 if (!bat_priv->orig_hash)
203 goto err;
204
Antonio Quartullidec05072012-11-10 11:00:32 +0100205 batadv_hash_set_lock_class(bat_priv->orig_hash,
206 &batadv_orig_hash_lock_class_key);
207
Antonio Quartulli72414442012-12-25 13:14:37 +0100208 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
209 queue_delayed_work(batadv_event_workqueue,
210 &bat_priv->orig_work,
211 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
212
Sven Eckelmann5346c352012-05-05 13:27:28 +0200213 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000214
215err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200216 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000217}
218
Simon Wunderlich89652332013-11-13 19:14:46 +0100219/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100220 * batadv_neigh_ifinfo_release() - release neigh_ifinfo from lists and queue for
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100221 * free after rcu grace period
Sven Eckelmann962c6832016-01-16 10:29:51 +0100222 * @ref: kref pointer of the neigh_ifinfo
Simon Wunderlich89652332013-11-13 19:14:46 +0100223 */
Sven Eckelmann6340dcb2021-08-08 19:56:17 +0200224void batadv_neigh_ifinfo_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100225{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100226 struct batadv_neigh_ifinfo *neigh_ifinfo;
227
228 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
229
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100230 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100231 batadv_hardif_put(neigh_ifinfo->if_outgoing);
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100232
233 kfree_rcu(neigh_ifinfo, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100234}
235
236/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100237 * batadv_hardif_neigh_release() - release hardif neigh node from lists and
Sven Eckelmannf6389692016-01-05 12:06:23 +0100238 * queue for free after rcu grace period
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100239 * @ref: kref pointer of the neigh_node
Marek Lindnercef63412015-08-04 21:09:55 +0800240 */
Sven Eckelmann6340dcb2021-08-08 19:56:17 +0200241void batadv_hardif_neigh_release(struct kref *ref)
Marek Lindnercef63412015-08-04 21:09:55 +0800242{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100243 struct batadv_hardif_neigh_node *hardif_neigh;
244
245 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
246 refcount);
247
Sven Eckelmannf6389692016-01-05 12:06:23 +0100248 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
249 hlist_del_init_rcu(&hardif_neigh->list);
250 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
Sven Eckelmannbab7c6c2016-01-05 12:06:17 +0100251
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100252 batadv_hardif_put(hardif_neigh->if_incoming);
Sven Eckelmannf6389692016-01-05 12:06:23 +0100253 kfree_rcu(hardif_neigh, rcu);
Marek Lindnercef63412015-08-04 21:09:55 +0800254}
255
256/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100257 * batadv_neigh_node_release() - release neigh_node from lists and queue for
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100258 * free after rcu grace period
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100259 * @ref: kref pointer of the neigh_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100260 */
Sven Eckelmann6340dcb2021-08-08 19:56:17 +0200261void batadv_neigh_node_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100262{
263 struct hlist_node *node_tmp;
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100264 struct batadv_neigh_node *neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +0100265 struct batadv_neigh_ifinfo *neigh_ifinfo;
266
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100267 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100268
269 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
270 &neigh_node->ifinfo_list, list) {
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100271 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100272 }
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800273
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100274 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
Marek Lindnercef63412015-08-04 21:09:55 +0800275
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100276 batadv_hardif_put(neigh_node->if_incoming);
Simon Wunderlich89652332013-11-13 19:14:46 +0100277
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100278 kfree_rcu(neigh_node, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100279}
280
281/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100282 * batadv_orig_router_get() - router to the originator depending on iface
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100283 * @orig_node: the orig node for the router
284 * @if_outgoing: the interface where the payload packet has been received or
285 * the OGM should be sent to
286 *
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200287 * Return: the neighbor which should be the router for this orig_node/iface.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100288 *
289 * The object is returned with refcounter increased by 1.
290 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200291struct batadv_neigh_node *
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100292batadv_orig_router_get(struct batadv_orig_node *orig_node,
293 const struct batadv_hard_iface *if_outgoing)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000294{
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100295 struct batadv_orig_ifinfo *orig_ifinfo;
296 struct batadv_neigh_node *router = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000297
298 rcu_read_lock();
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100299 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
300 if (orig_ifinfo->if_outgoing != if_outgoing)
301 continue;
302
303 router = rcu_dereference(orig_ifinfo->router);
304 break;
305 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000306
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100307 if (router && !kref_get_unless_zero(&router->refcount))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000308 router = NULL;
309
310 rcu_read_unlock();
311 return router;
312}
313
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200314/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100315 * batadv_orig_ifinfo_get() - find the ifinfo from an orig_node
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100316 * @orig_node: the orig node to be queried
317 * @if_outgoing: the interface for which the ifinfo should be acquired
318 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200319 * Return: the requested orig_ifinfo or NULL if not found.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100320 *
321 * The object is returned with refcounter increased by 1.
322 */
323struct batadv_orig_ifinfo *
324batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
325 struct batadv_hard_iface *if_outgoing)
326{
327 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
328
329 rcu_read_lock();
330 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
331 list) {
332 if (tmp->if_outgoing != if_outgoing)
333 continue;
334
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100335 if (!kref_get_unless_zero(&tmp->refcount))
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100336 continue;
337
338 orig_ifinfo = tmp;
339 break;
340 }
341 rcu_read_unlock();
342
343 return orig_ifinfo;
344}
345
346/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100347 * batadv_orig_ifinfo_new() - search and possibly create an orig_ifinfo object
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100348 * @orig_node: the orig node to be queried
349 * @if_outgoing: the interface for which the ifinfo should be acquired
350 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200351 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100352 * interface otherwise. The object is created and added to the list
353 * if it does not exist.
354 *
355 * The object is returned with refcounter increased by 1.
356 */
357struct batadv_orig_ifinfo *
358batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
359 struct batadv_hard_iface *if_outgoing)
360{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200361 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100362 unsigned long reset_time;
363
364 spin_lock_bh(&orig_node->neigh_list_lock);
365
366 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
367 if (orig_ifinfo)
368 goto out;
369
370 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
371 if (!orig_ifinfo)
372 goto out;
373
Sven Eckelmann17a86912016-04-11 13:06:40 +0200374 if (if_outgoing != BATADV_IF_DEFAULT)
375 kref_get(&if_outgoing->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100376
377 reset_time = jiffies - 1;
378 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
379 orig_ifinfo->batman_seqno_reset = reset_time;
380 orig_ifinfo->if_outgoing = if_outgoing;
381 INIT_HLIST_NODE(&orig_ifinfo->list);
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100382 kref_init(&orig_ifinfo->refcount);
Sven Eckelmannf257b992016-07-15 17:39:17 +0200383
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100384 kref_get(&orig_ifinfo->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100385 hlist_add_head_rcu(&orig_ifinfo->list,
386 &orig_node->ifinfo_list);
387out:
388 spin_unlock_bh(&orig_node->neigh_list_lock);
389 return orig_ifinfo;
390}
391
392/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100393 * batadv_neigh_ifinfo_get() - find the ifinfo from an neigh_node
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200394 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100395 * @if_outgoing: the interface for which the ifinfo should be acquired
396 *
397 * The object is returned with refcounter increased by 1.
398 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200399 * Return: the requested neigh_ifinfo or NULL if not found
Simon Wunderlich89652332013-11-13 19:14:46 +0100400 */
401struct batadv_neigh_ifinfo *
402batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
403 struct batadv_hard_iface *if_outgoing)
404{
405 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
406 *tmp_neigh_ifinfo;
407
408 rcu_read_lock();
409 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
410 list) {
411 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
412 continue;
413
Sven Eckelmann962c6832016-01-16 10:29:51 +0100414 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +0100415 continue;
416
417 neigh_ifinfo = tmp_neigh_ifinfo;
418 break;
419 }
420 rcu_read_unlock();
421
422 return neigh_ifinfo;
423}
424
425/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100426 * batadv_neigh_ifinfo_new() - search and possibly create an neigh_ifinfo object
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200427 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100428 * @if_outgoing: the interface for which the ifinfo should be acquired
429 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200430 * Return: NULL in case of failure or the neigh_ifinfo object for the
Simon Wunderlich89652332013-11-13 19:14:46 +0100431 * if_outgoing interface otherwise. The object is created and added to the list
432 * if it does not exist.
433 *
434 * The object is returned with refcounter increased by 1.
435 */
436struct batadv_neigh_ifinfo *
437batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
438 struct batadv_hard_iface *if_outgoing)
439{
440 struct batadv_neigh_ifinfo *neigh_ifinfo;
441
442 spin_lock_bh(&neigh->ifinfo_lock);
443
444 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
445 if (neigh_ifinfo)
446 goto out;
447
448 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
449 if (!neigh_ifinfo)
450 goto out;
451
Sven Eckelmann17a86912016-04-11 13:06:40 +0200452 if (if_outgoing)
453 kref_get(&if_outgoing->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100454
455 INIT_HLIST_NODE(&neigh_ifinfo->list);
Sven Eckelmann962c6832016-01-16 10:29:51 +0100456 kref_init(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100457 neigh_ifinfo->if_outgoing = if_outgoing;
458
Sven Eckelmann2e774ac2016-07-15 17:39:19 +0200459 kref_get(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100460 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
461
462out:
463 spin_unlock_bh(&neigh->ifinfo_lock);
464
465 return neigh_ifinfo;
466}
467
468/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100469 * batadv_neigh_node_get() - retrieve a neighbour from the list
Marek Lindnered292662015-08-04 23:31:44 +0800470 * @orig_node: originator which the neighbour belongs to
471 * @hard_iface: the interface where this neighbour is connected to
472 * @addr: the address of the neighbour
473 *
474 * Looks for and possibly returns a neighbour belonging to this originator list
475 * which is connected through the provided hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200476 *
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200477 * Return: neighbor when found. Otherwise NULL
Marek Lindnered292662015-08-04 23:31:44 +0800478 */
479static struct batadv_neigh_node *
480batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
481 const struct batadv_hard_iface *hard_iface,
482 const u8 *addr)
483{
484 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
485
486 rcu_read_lock();
487 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
488 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
489 continue;
490
491 if (tmp_neigh_node->if_incoming != hard_iface)
492 continue;
493
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100494 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnered292662015-08-04 23:31:44 +0800495 continue;
496
497 res = tmp_neigh_node;
498 break;
499 }
500 rcu_read_unlock();
501
502 return res;
503}
504
505/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100506 * batadv_hardif_neigh_create() - create a hardif neighbour node
Marek Lindnercef63412015-08-04 21:09:55 +0800507 * @hard_iface: the interface this neighbour is connected to
508 * @neigh_addr: the interface address of the neighbour to retrieve
Linus Lüssing3111bee2016-08-07 12:34:19 +0200509 * @orig_node: originator object representing the neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800510 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200511 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800512 */
513static struct batadv_hardif_neigh_node *
514batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200515 const u8 *neigh_addr,
516 struct batadv_orig_node *orig_node)
Marek Lindnercef63412015-08-04 21:09:55 +0800517{
Marek Lindner8248a4c2015-08-04 21:09:56 +0800518 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200519 struct batadv_hardif_neigh_node *hardif_neigh;
Marek Lindnercef63412015-08-04 21:09:55 +0800520
521 spin_lock_bh(&hard_iface->neigh_list_lock);
522
523 /* check if neighbor hasn't been added in the meantime */
524 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
525 if (hardif_neigh)
526 goto out;
527
Marek Lindnercef63412015-08-04 21:09:55 +0800528 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
Sven Eckelmann17a86912016-04-11 13:06:40 +0200529 if (!hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800530 goto out;
Marek Lindnercef63412015-08-04 21:09:55 +0800531
Sven Eckelmann17a86912016-04-11 13:06:40 +0200532 kref_get(&hard_iface->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800533 INIT_HLIST_NODE(&hardif_neigh->list);
534 ether_addr_copy(hardif_neigh->addr, neigh_addr);
Linus Lüssing3111bee2016-08-07 12:34:19 +0200535 ether_addr_copy(hardif_neigh->orig, orig_node->orig);
Marek Lindnercef63412015-08-04 21:09:55 +0800536 hardif_neigh->if_incoming = hard_iface;
537 hardif_neigh->last_seen = jiffies;
538
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100539 kref_init(&hardif_neigh->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800540
Antonio Quartulli29824a52016-05-25 23:27:31 +0800541 if (bat_priv->algo_ops->neigh.hardif_init)
542 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
Marek Lindner8248a4c2015-08-04 21:09:56 +0800543
Sven Eckelmann9ca488d2016-09-29 17:22:58 +0200544 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
Marek Lindnercef63412015-08-04 21:09:55 +0800545
546out:
547 spin_unlock_bh(&hard_iface->neigh_list_lock);
548 return hardif_neigh;
549}
550
551/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100552 * batadv_hardif_neigh_get_or_create() - retrieve or create a hardif neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800553 * node
554 * @hard_iface: the interface this neighbour is connected to
555 * @neigh_addr: the interface address of the neighbour to retrieve
Linus Lüssing3111bee2016-08-07 12:34:19 +0200556 * @orig_node: originator object representing the neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800557 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200558 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800559 */
560static struct batadv_hardif_neigh_node *
561batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200562 const u8 *neigh_addr,
563 struct batadv_orig_node *orig_node)
Marek Lindnercef63412015-08-04 21:09:55 +0800564{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200565 struct batadv_hardif_neigh_node *hardif_neigh;
Marek Lindnercef63412015-08-04 21:09:55 +0800566
567 /* first check without locking to avoid the overhead */
568 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
569 if (hardif_neigh)
570 return hardif_neigh;
571
Linus Lüssing3111bee2016-08-07 12:34:19 +0200572 return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
Marek Lindnercef63412015-08-04 21:09:55 +0800573}
574
575/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100576 * batadv_hardif_neigh_get() - retrieve a hardif neighbour from the list
Marek Lindnercef63412015-08-04 21:09:55 +0800577 * @hard_iface: the interface where this neighbour is connected to
578 * @neigh_addr: the address of the neighbour
579 *
580 * Looks for and possibly returns a neighbour belonging to this hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200581 *
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200582 * Return: neighbor when found. Otherwise NULL
Marek Lindnercef63412015-08-04 21:09:55 +0800583 */
584struct batadv_hardif_neigh_node *
585batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
586 const u8 *neigh_addr)
587{
588 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
589
590 rcu_read_lock();
591 hlist_for_each_entry_rcu(tmp_hardif_neigh,
592 &hard_iface->neigh_list, list) {
593 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
594 continue;
595
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100596 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
Marek Lindnercef63412015-08-04 21:09:55 +0800597 continue;
598
599 hardif_neigh = tmp_hardif_neigh;
600 break;
601 }
602 rcu_read_unlock();
603
604 return hardif_neigh;
605}
606
607/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100608 * batadv_neigh_node_create() - create a neigh node object
Marek Lindner3f32f8a2015-07-26 04:59:15 +0800609 * @orig_node: originator object representing the neighbour
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200610 * @hard_iface: the interface where the neighbour is connected to
611 * @neigh_addr: the mac address of the neighbour interface
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200612 *
613 * Allocates a new neigh_node object and initialises all the generic fields.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200614 *
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800615 * Return: the neighbour node if found or created or NULL otherwise.
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200616 */
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800617static struct batadv_neigh_node *
618batadv_neigh_node_create(struct batadv_orig_node *orig_node,
619 struct batadv_hard_iface *hard_iface,
620 const u8 *neigh_addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200622 struct batadv_neigh_node *neigh_node;
Marek Lindnercef63412015-08-04 21:09:55 +0800623 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000624
Linus Lüssinge1237052016-01-07 08:11:12 +0100625 spin_lock_bh(&orig_node->neigh_list_lock);
626
Marek Lindner741aa062015-07-26 04:57:43 +0800627 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
628 if (neigh_node)
629 goto out;
630
Marek Lindnercef63412015-08-04 21:09:55 +0800631 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200632 neigh_addr, orig_node);
Marek Lindnercef63412015-08-04 21:09:55 +0800633 if (!hardif_neigh)
634 goto out;
635
Sven Eckelmann704509b2011-05-14 23:14:54 +0200636 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000637 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800638 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000639
Marek Lindner9591a792010-12-12 21:57:11 +0000640 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlich89652332013-11-13 19:14:46 +0100641 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
642 spin_lock_init(&neigh_node->ifinfo_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643
Sven Eckelmann17a86912016-04-11 13:06:40 +0200644 kref_get(&hard_iface->refcount);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100645 ether_addr_copy(neigh_node->addr, neigh_addr);
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200646 neigh_node->if_incoming = hard_iface;
647 neigh_node->orig_node = orig_node;
Marek Lindnere48474e2016-03-11 16:01:09 +0100648 neigh_node->last_seen = jiffies;
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200649
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100650 /* increment unique neighbor refcount */
651 kref_get(&hardif_neigh->refcount);
652 neigh_node->hardif_neigh = hardif_neigh;
653
Marek Lindner1605d0d2011-02-18 12:28:11 +0000654 /* extra reference for return */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100655 kref_init(&neigh_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000656
Sven Eckelmann84274452016-07-15 17:39:20 +0200657 kref_get(&neigh_node->refcount);
Marek Lindner741aa062015-07-26 04:57:43 +0800658 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
Marek Lindner741aa062015-07-26 04:57:43 +0800659
660 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
661 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
662 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
663
Marek Lindner7ae8b282012-03-01 15:35:21 +0800664out:
Linus Lüssinge1237052016-01-07 08:11:12 +0100665 spin_unlock_bh(&orig_node->neigh_list_lock);
666
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200667 batadv_hardif_neigh_put(hardif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000668 return neigh_node;
669}
670
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100671/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100672 * batadv_neigh_node_get_or_create() - retrieve or create a neigh node object
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800673 * @orig_node: originator object representing the neighbour
674 * @hard_iface: the interface where the neighbour is connected to
675 * @neigh_addr: the mac address of the neighbour interface
676 *
677 * Return: the neighbour node if found or created or NULL otherwise.
678 */
679struct batadv_neigh_node *
680batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
681 struct batadv_hard_iface *hard_iface,
682 const u8 *neigh_addr)
683{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200684 struct batadv_neigh_node *neigh_node;
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800685
686 /* first check without locking to avoid the overhead */
687 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
688 if (neigh_node)
689 return neigh_node;
690
691 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
692}
693
Marek Lindner75874052015-08-04 21:09:57 +0800694/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100695 * batadv_hardif_neigh_dump() - Dump to netlink the neighbor infos for a
696 * specific outgoing interface
Matthias Schiffer85cf8c82016-07-03 13:31:39 +0200697 * @msg: message to dump into
698 * @cb: parameters for the dump
699 *
700 * Return: 0 or error value
701 */
702int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
703{
704 struct net *net = sock_net(cb->skb->sk);
705 struct net_device *soft_iface;
706 struct net_device *hard_iface = NULL;
707 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
708 struct batadv_priv *bat_priv;
709 struct batadv_hard_iface *primary_if = NULL;
710 int ret;
711 int ifindex, hard_ifindex;
712
713 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
714 if (!ifindex)
715 return -EINVAL;
716
717 soft_iface = dev_get_by_index(net, ifindex);
718 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
719 ret = -ENODEV;
720 goto out;
721 }
722
723 bat_priv = netdev_priv(soft_iface);
724
725 primary_if = batadv_primary_if_get_selected(bat_priv);
726 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
727 ret = -ENOENT;
728 goto out;
729 }
730
731 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
732 BATADV_ATTR_HARD_IFINDEX);
733 if (hard_ifindex) {
734 hard_iface = dev_get_by_index(net, hard_ifindex);
735 if (hard_iface)
736 hardif = batadv_hardif_get_by_netdev(hard_iface);
737
738 if (!hardif) {
739 ret = -ENODEV;
740 goto out;
741 }
742
743 if (hardif->soft_iface != soft_iface) {
744 ret = -ENOENT;
745 goto out;
746 }
747 }
748
749 if (!bat_priv->algo_ops->neigh.dump) {
750 ret = -EOPNOTSUPP;
751 goto out;
752 }
753
754 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
755
756 ret = msg->len;
757
758 out:
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200759 batadv_hardif_put(hardif);
Yajun Deng1160dfa2021-08-05 19:55:27 +0800760 dev_put(hard_iface);
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200761 batadv_hardif_put(primary_if);
Yajun Deng1160dfa2021-08-05 19:55:27 +0800762 dev_put(soft_iface);
Matthias Schiffer85cf8c82016-07-03 13:31:39 +0200763
764 return ret;
765}
766
767/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100768 * batadv_orig_ifinfo_release() - release orig_ifinfo from lists and queue for
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100769 * free after rcu grace period
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100770 * @ref: kref pointer of the orig_ifinfo
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100771 */
Sven Eckelmann6340dcb2021-08-08 19:56:17 +0200772void batadv_orig_ifinfo_release(struct kref *ref)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100773{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100774 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100775 struct batadv_neigh_node *router;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100776
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100777 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
778
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100779 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100780 batadv_hardif_put(orig_ifinfo->if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100781
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100782 /* this is the last reference to this object */
783 router = rcu_dereference_protected(orig_ifinfo->router, true);
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200784 batadv_neigh_node_put(router);
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100785
786 kfree_rcu(orig_ifinfo, rcu);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100787}
788
789/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100790 * batadv_orig_node_free_rcu() - free the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100791 * @rcu: rcu pointer of the orig_node
792 */
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200793static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000794{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200795 struct batadv_orig_node *orig_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000796
Sven Eckelmann56303d32012-06-05 22:31:31 +0200797 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000798
Linus Lüssing60432d72014-02-15 17:47:51 +0100799 batadv_mcast_purge_orig(orig_node);
800
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200801 batadv_frag_purge_orig(orig_node, NULL);
802
Antonio Quartullia73105b2011-04-27 14:27:44 +0200803 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000804 kfree(orig_node);
805}
806
Linus Lüssing72822222013-04-15 21:43:29 +0800807/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100808 * batadv_orig_node_release() - release orig_node from lists and queue for
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100809 * free after rcu grace period
Sven Eckelmann7c124392016-01-16 10:29:56 +0100810 * @ref: kref pointer of the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100811 */
Sven Eckelmann6340dcb2021-08-08 19:56:17 +0200812void batadv_orig_node_release(struct kref *ref)
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100813{
814 struct hlist_node *node_tmp;
815 struct batadv_neigh_node *neigh_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +0100816 struct batadv_orig_node *orig_node;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100817 struct batadv_orig_ifinfo *orig_ifinfo;
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200818 struct batadv_orig_node_vlan *vlan;
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200819 struct batadv_orig_ifinfo *last_candidate;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100820
Sven Eckelmann7c124392016-01-16 10:29:56 +0100821 orig_node = container_of(ref, struct batadv_orig_node, refcount);
822
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100823 spin_lock_bh(&orig_node->neigh_list_lock);
824
825 /* for all neighbors towards this originator ... */
826 hlist_for_each_entry_safe(neigh_node, node_tmp,
827 &orig_node->neigh_list, list) {
828 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100829 batadv_neigh_node_put(neigh_node);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100830 }
831
832 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
833 &orig_node->ifinfo_list, list) {
834 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +0100835 batadv_orig_ifinfo_put(orig_ifinfo);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100836 }
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200837
838 last_candidate = orig_node->last_bonding_candidate;
839 orig_node->last_bonding_candidate = NULL;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100840 spin_unlock_bh(&orig_node->neigh_list_lock);
841
Sven Eckelmann79a0bff2021-08-08 19:11:08 +0200842 batadv_orig_ifinfo_put(last_candidate);
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200843
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200844 spin_lock_bh(&orig_node->vlan_list_lock);
845 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
846 hlist_del_rcu(&vlan->list);
847 batadv_orig_node_vlan_put(vlan);
848 }
849 spin_unlock_bh(&orig_node->vlan_list_lock);
850
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100851 /* Free nc_nodes */
852 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
853
854 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
855}
856
857/**
Sven Eckelmannff15c272017-12-02 19:51:53 +0100858 * batadv_originator_free() - Free all originator structures
859 * @bat_priv: the bat priv with all the soft interface information
860 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200861void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000862{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200863 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800864 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000865 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000866 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200867 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200868 u32 i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000869
870 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000871 return;
872
873 cancel_delayed_work_sync(&bat_priv->orig_work);
874
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000875 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000876
877 for (i = 0; i < hash->size; i++) {
878 head = &hash->table[i];
879 list_lock = &hash->list_locks[i];
880
881 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800882 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000883 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800884 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann5d967312016-01-17 11:01:09 +0100885 batadv_orig_node_put(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000886 }
887 spin_unlock_bh(list_lock);
888 }
889
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200890 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000891}
892
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200893/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100894 * batadv_orig_node_new() - creates a new orig_node
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200895 * @bat_priv: the bat priv with all the soft interface information
896 * @addr: the mac address of the originator
897 *
Sven Eckelmannbccb48c2020-06-01 20:13:21 +0200898 * Creates a new originator object and initialises all the generic fields.
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200899 * The new object is not added to the originator list.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200900 *
901 * Return: the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200902 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200903struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200904 const u8 *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000905{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200906 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200907 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200908 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200909 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000910
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200911 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
912 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000913
Sven Eckelmann704509b2011-05-14 23:14:54 +0200914 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915 if (!orig_node)
916 return NULL;
917
Marek Lindner9591a792010-12-12 21:57:11 +0000918 INIT_HLIST_HEAD(&orig_node->neigh_list);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800919 INIT_HLIST_HEAD(&orig_node->vlan_list);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100920 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000921 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000922 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200923 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +0200924 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200925 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000926
Martin Hundebølld56b1702013-01-25 11:12:39 +0100927 batadv_nc_init_orig(orig_node);
928
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000929 /* extra reference for return */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100930 kref_init(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000931
Marek Lindner16b1aba2011-01-19 20:01:42 +0000932 orig_node->bat_priv = bat_priv;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100933 ether_addr_copy(orig_node->orig, addr);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100934 batadv_dat_init_orig_node_addr(orig_node);
Antonio Quartullic8c991b2011-07-07 01:40:57 +0200935 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200936 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200937 orig_node->tt_buff_len = 0;
Linus Lüssing2c667a32014-10-30 06:23:40 +0100938 orig_node->last_seen = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200939 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
940 orig_node->bcast_seqno_reset = reset_time;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200941
Linus Lüssing60432d72014-02-15 17:47:51 +0100942#ifdef CONFIG_BATMAN_ADV_MCAST
Linus Lüssing61caf3d2019-06-11 22:58:40 +0200943 orig_node->mcast_flags = BATADV_MCAST_WANT_NO_RTR4;
944 orig_node->mcast_flags |= BATADV_MCAST_WANT_NO_RTR6;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200945 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
946 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
947 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
948 spin_lock_init(&orig_node->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +0100949#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000950
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200951 /* create a vlan object for the "untagged" LAN */
952 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
953 if (!vlan)
954 goto free_orig_node;
955 /* batadv_orig_node_vlan_new() increases the refcounter.
956 * Immediately release vlan since it is not needed anymore in this
957 * context
958 */
Sven Eckelmann21754e22016-01-17 11:01:24 +0100959 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200960
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200961 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
Sven Eckelmann176e5b72016-07-27 12:31:07 +0200962 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200963 spin_lock_init(&orig_node->fragments[i].lock);
964 orig_node->fragments[i].size = 0;
965 }
966
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000967 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000968free_orig_node:
969 kfree(orig_node);
970 return NULL;
971}
972
Simon Wunderlich89652332013-11-13 19:14:46 +0100973/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100974 * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor
Simon Wunderlich709de132014-03-26 15:46:24 +0100975 * @bat_priv: the bat priv with all the soft interface information
976 * @neigh: orig node which is to be checked
977 */
978static void
979batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
980 struct batadv_neigh_node *neigh)
981{
982 struct batadv_neigh_ifinfo *neigh_ifinfo;
983 struct batadv_hard_iface *if_outgoing;
984 struct hlist_node *node_tmp;
985
986 spin_lock_bh(&neigh->ifinfo_lock);
987
988 /* for all ifinfo objects for this neighinator */
989 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
990 &neigh->ifinfo_list, list) {
991 if_outgoing = neigh_ifinfo->if_outgoing;
992
993 /* always keep the default interface */
994 if (if_outgoing == BATADV_IF_DEFAULT)
995 continue;
996
997 /* don't purge if the interface is not (going) down */
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200998 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
999 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1000 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
Simon Wunderlich709de132014-03-26 15:46:24 +01001001 continue;
1002
1003 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1004 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1005 neigh->addr, if_outgoing->net_dev->name);
1006
1007 hlist_del_rcu(&neigh_ifinfo->list);
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001008 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich709de132014-03-26 15:46:24 +01001009 }
1010
1011 spin_unlock_bh(&neigh->ifinfo_lock);
1012}
1013
1014/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001015 * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001016 * @bat_priv: the bat priv with all the soft interface information
1017 * @orig_node: orig node which is to be checked
1018 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001019 * Return: true if any ifinfo entry was purged, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001020 */
1021static bool
1022batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1023 struct batadv_orig_node *orig_node)
1024{
1025 struct batadv_orig_ifinfo *orig_ifinfo;
1026 struct batadv_hard_iface *if_outgoing;
1027 struct hlist_node *node_tmp;
1028 bool ifinfo_purged = false;
1029
1030 spin_lock_bh(&orig_node->neigh_list_lock);
1031
1032 /* for all ifinfo objects for this originator */
1033 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1034 &orig_node->ifinfo_list, list) {
1035 if_outgoing = orig_ifinfo->if_outgoing;
1036
1037 /* always keep the default interface */
1038 if (if_outgoing == BATADV_IF_DEFAULT)
1039 continue;
1040
1041 /* don't purge if the interface is not (going) down */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001042 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1043 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1044 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001045 continue;
1046
1047 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1048 "router/ifinfo purge: originator %pM, iface: %s\n",
1049 orig_node->orig, if_outgoing->net_dev->name);
1050
1051 ifinfo_purged = true;
1052
1053 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001054 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001055 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1056 orig_node->last_bonding_candidate = NULL;
Sven Eckelmann35f94772016-01-17 11:01:13 +01001057 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001058 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001059 }
1060
1061 spin_unlock_bh(&orig_node->neigh_list_lock);
1062
1063 return ifinfo_purged;
1064}
1065
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001066/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001067 * batadv_purge_orig_neighbors() - purges neighbors from originator
Simon Wunderlich89652332013-11-13 19:14:46 +01001068 * @bat_priv: the bat priv with all the soft interface information
1069 * @orig_node: orig node which is to be checked
1070 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001071 * Return: true if any neighbor was purged, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001072 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001073static bool
1074batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
Simon Wunderlich89652332013-11-13 19:14:46 +01001075 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001076{
Sasha Levinb67bfe02013-02-27 17:06:00 -08001077 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001078 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001079 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +08001080 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001081 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001082
Marek Lindnerf987ed62010-12-12 21:57:12 +00001083 spin_lock_bh(&orig_node->neigh_list_lock);
1084
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001085 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001086 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +00001087 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001088 last_seen = neigh_node->last_seen;
1089 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001090
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001091 if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) ||
1092 if_incoming->if_status == BATADV_IF_INACTIVE ||
1093 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1094 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) {
1095 if (if_incoming->if_status == BATADV_IF_INACTIVE ||
1096 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1097 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001098 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001099 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1100 orig_node->orig, neigh_node->addr,
1101 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001102 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001103 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001104 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1105 orig_node->orig, neigh_node->addr,
1106 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001107
1108 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +00001109
Marek Lindnerf987ed62010-12-12 21:57:12 +00001110 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001111 batadv_neigh_node_put(neigh_node);
Simon Wunderlich709de132014-03-26 15:46:24 +01001112 } else {
1113 /* only necessary if not the whole neighbor is to be
1114 * deleted, but some interface has been removed.
1115 */
1116 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001117 }
1118 }
Marek Lindnerf987ed62010-12-12 21:57:12 +00001119
1120 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001121 return neigh_purged;
1122}
1123
Simon Wunderlich89652332013-11-13 19:14:46 +01001124/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001125 * batadv_find_best_neighbor() - finds the best neighbor after purging
Simon Wunderlich89652332013-11-13 19:14:46 +01001126 * @bat_priv: the bat priv with all the soft interface information
1127 * @orig_node: orig node which is to be checked
1128 * @if_outgoing: the interface for which the metric should be compared
1129 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001130 * Return: the current best neighbor, with refcount increased.
Simon Wunderlich89652332013-11-13 19:14:46 +01001131 */
1132static struct batadv_neigh_node *
1133batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1134 struct batadv_orig_node *orig_node,
1135 struct batadv_hard_iface *if_outgoing)
1136{
1137 struct batadv_neigh_node *best = NULL, *neigh;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001138 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Simon Wunderlich89652332013-11-13 19:14:46 +01001139
1140 rcu_read_lock();
1141 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
Antonio Quartulli29824a52016-05-25 23:27:31 +08001142 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1143 if_outgoing) <= 0))
Simon Wunderlich89652332013-11-13 19:14:46 +01001144 continue;
1145
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001146 if (!kref_get_unless_zero(&neigh->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +01001147 continue;
1148
Sven Eckelmann79a0bff2021-08-08 19:11:08 +02001149 batadv_neigh_node_put(best);
Simon Wunderlich89652332013-11-13 19:14:46 +01001150
1151 best = neigh;
1152 }
1153 rcu_read_unlock();
1154
1155 return best;
1156}
1157
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001158/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001159 * batadv_purge_orig_node() - purges obsolete information from an orig_node
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001160 * @bat_priv: the bat priv with all the soft interface information
1161 * @orig_node: orig node which is to be checked
1162 *
1163 * This function checks if the orig_node or substructures of it have become
1164 * obsolete, and purges this information if that's the case.
1165 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001166 * Return: true if the orig_node is to be removed, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001167 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001168static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1169 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001170{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001171 struct batadv_neigh_node *best_neigh_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001172 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001173 bool changed_ifinfo, changed_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001174
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001175 if (batadv_has_timed_out(orig_node->last_seen,
1176 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001177 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001178 "Originator timeout: originator %pM, last_seen %u\n",
1179 orig_node->orig,
1180 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001181 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001182 }
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001183 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1184 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001185
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001186 if (!changed_ifinfo && !changed_neigh)
Simon Wunderlich89652332013-11-13 19:14:46 +01001187 return false;
1188
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001189 /* first for NULL ... */
Simon Wunderlich89652332013-11-13 19:14:46 +01001190 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1191 BATADV_IF_DEFAULT);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001192 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1193 best_neigh_node);
Sven Eckelmann79a0bff2021-08-08 19:11:08 +02001194 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001195
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001196 /* ... then for all other interfaces. */
1197 rcu_read_lock();
1198 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1199 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1200 continue;
1201
1202 if (hard_iface->soft_iface != bat_priv->soft_iface)
1203 continue;
1204
Sven Eckelmann27353442016-03-05 16:09:16 +01001205 if (!kref_get_unless_zero(&hard_iface->refcount))
1206 continue;
1207
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001208 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1209 orig_node,
1210 hard_iface);
1211 batadv_update_route(bat_priv, orig_node, hard_iface,
1212 best_neigh_node);
Sven Eckelmann79a0bff2021-08-08 19:11:08 +02001213 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmann27353442016-03-05 16:09:16 +01001214
1215 batadv_hardif_put(hard_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001216 }
1217 rcu_read_unlock();
1218
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001219 return false;
1220}
1221
Sven Eckelmann3b1709d2018-07-07 21:46:11 +02001222/**
1223 * batadv_purge_orig_ref() - Purge all outdated originators
1224 * @bat_priv: the bat priv with all the soft interface information
1225 */
1226void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001227{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001228 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001229 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001230 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001231 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001232 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001233 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001234
1235 if (!hash)
1236 return;
1237
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001238 /* for all origins... */
1239 for (i = 0; i < hash->size; i++) {
1240 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001241 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001242
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001243 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001244 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +00001245 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001246 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +08001247 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001248 hlist_del_rcu(&orig_node->hash_entry);
Linus Lüssing9d31b3c2014-12-13 23:32:15 +01001249 batadv_tt_global_del_orig(orig_node->bat_priv,
1250 orig_node, -1,
1251 "originator timed out");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001252 batadv_orig_node_put(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001253 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001254 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001255
1256 batadv_frag_purge_orig(orig_node,
1257 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001258 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001259 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001260 }
1261
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +02001262 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001263}
1264
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001265static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001266{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001267 struct delayed_work *delayed_work;
1268 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001269
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001270 delayed_work = to_delayed_work(work);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001271 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann3b1709d2018-07-07 21:46:11 +02001272 batadv_purge_orig_ref(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +01001273 queue_delayed_work(batadv_event_workqueue,
1274 &bat_priv->orig_work,
1275 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001276}
1277
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001278/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001279 * batadv_orig_dump() - Dump to netlink the originator infos for a specific
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001280 * outgoing interface
1281 * @msg: message to dump into
1282 * @cb: parameters for the dump
1283 *
1284 * Return: 0 or error value
1285 */
1286int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1287{
1288 struct net *net = sock_net(cb->skb->sk);
1289 struct net_device *soft_iface;
1290 struct net_device *hard_iface = NULL;
1291 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1292 struct batadv_priv *bat_priv;
1293 struct batadv_hard_iface *primary_if = NULL;
1294 int ret;
1295 int ifindex, hard_ifindex;
1296
1297 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1298 if (!ifindex)
1299 return -EINVAL;
1300
1301 soft_iface = dev_get_by_index(net, ifindex);
1302 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1303 ret = -ENODEV;
1304 goto out;
1305 }
1306
1307 bat_priv = netdev_priv(soft_iface);
1308
1309 primary_if = batadv_primary_if_get_selected(bat_priv);
1310 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1311 ret = -ENOENT;
1312 goto out;
1313 }
1314
1315 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1316 BATADV_ATTR_HARD_IFINDEX);
1317 if (hard_ifindex) {
1318 hard_iface = dev_get_by_index(net, hard_ifindex);
1319 if (hard_iface)
1320 hardif = batadv_hardif_get_by_netdev(hard_iface);
1321
1322 if (!hardif) {
1323 ret = -ENODEV;
1324 goto out;
1325 }
1326
1327 if (hardif->soft_iface != soft_iface) {
1328 ret = -ENOENT;
1329 goto out;
1330 }
1331 }
1332
1333 if (!bat_priv->algo_ops->orig.dump) {
1334 ret = -EOPNOTSUPP;
1335 goto out;
1336 }
1337
1338 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1339
1340 ret = msg->len;
1341
1342 out:
Sven Eckelmann79a0bff2021-08-08 19:11:08 +02001343 batadv_hardif_put(hardif);
Yajun Deng1160dfa2021-08-05 19:55:27 +08001344 dev_put(hard_iface);
Sven Eckelmann79a0bff2021-08-08 19:11:08 +02001345 batadv_hardif_put(primary_if);
Yajun Deng1160dfa2021-08-05 19:55:27 +08001346 dev_put(soft_iface);
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001347
1348 return ret;
1349}