blob: 1d295da3e342b6552abfd1fbced1347db5e9a2fe [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 Quartulli0538f752013-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 Quartulli0538f752013-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 Quartulli0538f752013-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 Quartulli0538f752013-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 Quartulli0538f752013-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 Quartulli0538f752013-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 Quartulli29824a52016-05-25 23:27:31 +0800907 if (orig_node->bat_priv->algo_ops->orig.free)
908 orig_node->bat_priv->algo_ops->orig.free(orig_node);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200909
Antonio Quartullia73105b2011-04-27 14:27:44 +0200910 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000911 kfree(orig_node);
912}
913
Linus Lüssing72822222013-04-15 21:43:29 +0800914/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100915 * batadv_orig_node_release() - release orig_node from lists and queue for
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100916 * free after rcu grace period
Sven Eckelmann7c124392016-01-16 10:29:56 +0100917 * @ref: kref pointer of the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100918 */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100919static void batadv_orig_node_release(struct kref *ref)
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100920{
921 struct hlist_node *node_tmp;
922 struct batadv_neigh_node *neigh_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +0100923 struct batadv_orig_node *orig_node;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100924 struct batadv_orig_ifinfo *orig_ifinfo;
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200925 struct batadv_orig_node_vlan *vlan;
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200926 struct batadv_orig_ifinfo *last_candidate;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100927
Sven Eckelmann7c124392016-01-16 10:29:56 +0100928 orig_node = container_of(ref, struct batadv_orig_node, refcount);
929
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100930 spin_lock_bh(&orig_node->neigh_list_lock);
931
932 /* for all neighbors towards this originator ... */
933 hlist_for_each_entry_safe(neigh_node, node_tmp,
934 &orig_node->neigh_list, list) {
935 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100936 batadv_neigh_node_put(neigh_node);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100937 }
938
939 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
940 &orig_node->ifinfo_list, list) {
941 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +0100942 batadv_orig_ifinfo_put(orig_ifinfo);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100943 }
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200944
945 last_candidate = orig_node->last_bonding_candidate;
946 orig_node->last_bonding_candidate = NULL;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100947 spin_unlock_bh(&orig_node->neigh_list_lock);
948
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200949 if (last_candidate)
950 batadv_orig_ifinfo_put(last_candidate);
951
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200952 spin_lock_bh(&orig_node->vlan_list_lock);
953 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
954 hlist_del_rcu(&vlan->list);
955 batadv_orig_node_vlan_put(vlan);
956 }
957 spin_unlock_bh(&orig_node->vlan_list_lock);
958
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100959 /* Free nc_nodes */
960 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
961
962 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
963}
964
965/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100966 * batadv_orig_node_put() - decrement the orig node refcounter and possibly
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100967 * release it
Linus Lüssing72822222013-04-15 21:43:29 +0800968 * @orig_node: the orig node to free
969 */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100970void batadv_orig_node_put(struct batadv_orig_node *orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000971{
Sven Eckelmann7c124392016-01-16 10:29:56 +0100972 kref_put(&orig_node->refcount, batadv_orig_node_release);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000973}
974
Sven Eckelmannff15c272017-12-02 19:51:53 +0100975/**
976 * batadv_originator_free() - Free all originator structures
977 * @bat_priv: the bat priv with all the soft interface information
978 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200979void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000980{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200981 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800982 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000983 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000984 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200985 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200986 u32 i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000987
988 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000989 return;
990
991 cancel_delayed_work_sync(&bat_priv->orig_work);
992
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000993 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000994
995 for (i = 0; i < hash->size; i++) {
996 head = &hash->table[i];
997 list_lock = &hash->list_locks[i];
998
999 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001000 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +00001001 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08001002 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann5d967312016-01-17 11:01:09 +01001003 batadv_orig_node_put(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +00001004 }
1005 spin_unlock_bh(list_lock);
1006 }
1007
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +02001008 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001009}
1010
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001011/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001012 * batadv_orig_node_new() - creates a new orig_node
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001013 * @bat_priv: the bat priv with all the soft interface information
1014 * @addr: the mac address of the originator
1015 *
1016 * Creates a new originator object and initialise all the generic fields.
1017 * The new object is not added to the originator list.
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001018 *
1019 * Return: the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001020 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001021struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001022 const u8 *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001024 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001025 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001026 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +02001027 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001028
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001029 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1030 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001031
Sven Eckelmann704509b2011-05-14 23:14:54 +02001032 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001033 if (!orig_node)
1034 return NULL;
1035
Marek Lindner9591a792010-12-12 21:57:11 +00001036 INIT_HLIST_HEAD(&orig_node->neigh_list);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +08001037 INIT_HLIST_HEAD(&orig_node->vlan_list);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001038 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +00001039 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +00001040 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +02001041 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +02001042 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001043 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001044
Martin Hundebølld56b1702013-01-25 11:12:39 +01001045 batadv_nc_init_orig(orig_node);
1046
Marek Lindner7b36e8e2011-02-18 12:28:10 +00001047 /* extra reference for return */
Sven Eckelmann7c124392016-01-16 10:29:56 +01001048 kref_init(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001049
Marek Lindner16b1aba2011-01-19 20:01:42 +00001050 orig_node->bat_priv = bat_priv;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001051 ether_addr_copy(orig_node->orig, addr);
Antonio Quartulli785ea112011-11-23 11:35:44 +01001052 batadv_dat_init_orig_node_addr(orig_node);
Antonio Quartullic8c991b2011-07-07 01:40:57 +02001053 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001054 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001055 orig_node->tt_buff_len = 0;
Linus Lüssing2c667a32014-10-30 06:23:40 +01001056 orig_node->last_seen = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001057 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
1058 orig_node->bcast_seqno_reset = reset_time;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001059
Linus Lüssing60432d72014-02-15 17:47:51 +01001060#ifdef CONFIG_BATMAN_ADV_MCAST
1061 orig_node->mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001062 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
1063 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
1064 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
1065 spin_lock_init(&orig_node->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001066#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001067
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001068 /* create a vlan object for the "untagged" LAN */
1069 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
1070 if (!vlan)
1071 goto free_orig_node;
1072 /* batadv_orig_node_vlan_new() increases the refcounter.
1073 * Immediately release vlan since it is not needed anymore in this
1074 * context
1075 */
Sven Eckelmann21754e22016-01-17 11:01:24 +01001076 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001077
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001078 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
Sven Eckelmann176e5b72016-07-27 12:31:07 +02001079 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001080 spin_lock_init(&orig_node->fragments[i].lock);
1081 orig_node->fragments[i].size = 0;
1082 }
1083
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001084 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001085free_orig_node:
1086 kfree(orig_node);
1087 return NULL;
1088}
1089
Simon Wunderlich89652332013-11-13 19:14:46 +01001090/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001091 * batadv_purge_neigh_ifinfo() - purge obsolete ifinfo entries from neighbor
Simon Wunderlich709de132014-03-26 15:46:24 +01001092 * @bat_priv: the bat priv with all the soft interface information
1093 * @neigh: orig node which is to be checked
1094 */
1095static void
1096batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1097 struct batadv_neigh_node *neigh)
1098{
1099 struct batadv_neigh_ifinfo *neigh_ifinfo;
1100 struct batadv_hard_iface *if_outgoing;
1101 struct hlist_node *node_tmp;
1102
1103 spin_lock_bh(&neigh->ifinfo_lock);
1104
1105 /* for all ifinfo objects for this neighinator */
1106 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1107 &neigh->ifinfo_list, list) {
1108 if_outgoing = neigh_ifinfo->if_outgoing;
1109
1110 /* always keep the default interface */
1111 if (if_outgoing == BATADV_IF_DEFAULT)
1112 continue;
1113
1114 /* don't purge if the interface is not (going) down */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001115 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1116 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1117 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
Simon Wunderlich709de132014-03-26 15:46:24 +01001118 continue;
1119
1120 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1121 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1122 neigh->addr, if_outgoing->net_dev->name);
1123
1124 hlist_del_rcu(&neigh_ifinfo->list);
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001125 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich709de132014-03-26 15:46:24 +01001126 }
1127
1128 spin_unlock_bh(&neigh->ifinfo_lock);
1129}
1130
1131/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001132 * batadv_purge_orig_ifinfo() - purge obsolete ifinfo entries from originator
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001133 * @bat_priv: the bat priv with all the soft interface information
1134 * @orig_node: orig node which is to be checked
1135 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001136 * Return: true if any ifinfo entry was purged, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001137 */
1138static bool
1139batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1140 struct batadv_orig_node *orig_node)
1141{
1142 struct batadv_orig_ifinfo *orig_ifinfo;
1143 struct batadv_hard_iface *if_outgoing;
1144 struct hlist_node *node_tmp;
1145 bool ifinfo_purged = false;
1146
1147 spin_lock_bh(&orig_node->neigh_list_lock);
1148
1149 /* for all ifinfo objects for this originator */
1150 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1151 &orig_node->ifinfo_list, list) {
1152 if_outgoing = orig_ifinfo->if_outgoing;
1153
1154 /* always keep the default interface */
1155 if (if_outgoing == BATADV_IF_DEFAULT)
1156 continue;
1157
1158 /* don't purge if the interface is not (going) down */
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001159 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1160 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1161 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001162 continue;
1163
1164 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1165 "router/ifinfo purge: originator %pM, iface: %s\n",
1166 orig_node->orig, if_outgoing->net_dev->name);
1167
1168 ifinfo_purged = true;
1169
1170 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001171 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001172 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1173 orig_node->last_bonding_candidate = NULL;
Sven Eckelmann35f94772016-01-17 11:01:13 +01001174 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001175 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001176 }
1177
1178 spin_unlock_bh(&orig_node->neigh_list_lock);
1179
1180 return ifinfo_purged;
1181}
1182
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001183/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001184 * batadv_purge_orig_neighbors() - purges neighbors from originator
Simon Wunderlich89652332013-11-13 19:14:46 +01001185 * @bat_priv: the bat priv with all the soft interface information
1186 * @orig_node: orig node which is to be checked
1187 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001188 * Return: true if any neighbor was purged, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001189 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001190static bool
1191batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
Simon Wunderlich89652332013-11-13 19:14:46 +01001192 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001193{
Sasha Levinb67bfe02013-02-27 17:06:00 -08001194 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001195 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001196 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +08001197 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001198 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001199
Marek Lindnerf987ed62010-12-12 21:57:12 +00001200 spin_lock_bh(&orig_node->neigh_list_lock);
1201
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001202 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001203 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +00001204 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001205 last_seen = neigh_node->last_seen;
1206 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001207
Sven Eckelmann825ffe12017-08-23 21:52:13 +02001208 if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) ||
1209 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) {
1212 if (if_incoming->if_status == BATADV_IF_INACTIVE ||
1213 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1214 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001215 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001216 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1217 orig_node->orig, neigh_node->addr,
1218 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001219 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001220 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001221 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1222 orig_node->orig, neigh_node->addr,
1223 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001224
1225 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +00001226
Marek Lindnerf987ed62010-12-12 21:57:12 +00001227 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001228 batadv_neigh_node_put(neigh_node);
Simon Wunderlich709de132014-03-26 15:46:24 +01001229 } else {
1230 /* only necessary if not the whole neighbor is to be
1231 * deleted, but some interface has been removed.
1232 */
1233 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001234 }
1235 }
Marek Lindnerf987ed62010-12-12 21:57:12 +00001236
1237 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001238 return neigh_purged;
1239}
1240
Simon Wunderlich89652332013-11-13 19:14:46 +01001241/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001242 * batadv_find_best_neighbor() - finds the best neighbor after purging
Simon Wunderlich89652332013-11-13 19:14:46 +01001243 * @bat_priv: the bat priv with all the soft interface information
1244 * @orig_node: orig node which is to be checked
1245 * @if_outgoing: the interface for which the metric should be compared
1246 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001247 * Return: the current best neighbor, with refcount increased.
Simon Wunderlich89652332013-11-13 19:14:46 +01001248 */
1249static struct batadv_neigh_node *
1250batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1251 struct batadv_orig_node *orig_node,
1252 struct batadv_hard_iface *if_outgoing)
1253{
1254 struct batadv_neigh_node *best = NULL, *neigh;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001255 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Simon Wunderlich89652332013-11-13 19:14:46 +01001256
1257 rcu_read_lock();
1258 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
Antonio Quartulli29824a52016-05-25 23:27:31 +08001259 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1260 if_outgoing) <= 0))
Simon Wunderlich89652332013-11-13 19:14:46 +01001261 continue;
1262
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001263 if (!kref_get_unless_zero(&neigh->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +01001264 continue;
1265
1266 if (best)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001267 batadv_neigh_node_put(best);
Simon Wunderlich89652332013-11-13 19:14:46 +01001268
1269 best = neigh;
1270 }
1271 rcu_read_unlock();
1272
1273 return best;
1274}
1275
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001276/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001277 * batadv_purge_orig_node() - purges obsolete information from an orig_node
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001278 * @bat_priv: the bat priv with all the soft interface information
1279 * @orig_node: orig node which is to be checked
1280 *
1281 * This function checks if the orig_node or substructures of it have become
1282 * obsolete, and purges this information if that's the case.
1283 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001284 * Return: true if the orig_node is to be removed, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001285 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001286static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1287 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001288{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001289 struct batadv_neigh_node *best_neigh_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001290 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001291 bool changed_ifinfo, changed_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001292
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001293 if (batadv_has_timed_out(orig_node->last_seen,
1294 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001295 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001296 "Originator timeout: originator %pM, last_seen %u\n",
1297 orig_node->orig,
1298 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001299 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001300 }
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001301 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1302 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001303
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001304 if (!changed_ifinfo && !changed_neigh)
Simon Wunderlich89652332013-11-13 19:14:46 +01001305 return false;
1306
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001307 /* first for NULL ... */
Simon Wunderlich89652332013-11-13 19:14:46 +01001308 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1309 BATADV_IF_DEFAULT);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001310 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1311 best_neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001312 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001313 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001314
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001315 /* ... then for all other interfaces. */
1316 rcu_read_lock();
1317 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1318 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1319 continue;
1320
1321 if (hard_iface->soft_iface != bat_priv->soft_iface)
1322 continue;
1323
Sven Eckelmann27353442016-03-05 16:09:16 +01001324 if (!kref_get_unless_zero(&hard_iface->refcount))
1325 continue;
1326
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001327 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1328 orig_node,
1329 hard_iface);
1330 batadv_update_route(bat_priv, orig_node, hard_iface,
1331 best_neigh_node);
1332 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001333 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmann27353442016-03-05 16:09:16 +01001334
1335 batadv_hardif_put(hard_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001336 }
1337 rcu_read_unlock();
1338
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001339 return false;
1340}
1341
Sven Eckelmann3b1709d2018-07-07 21:46:11 +02001342/**
1343 * batadv_purge_orig_ref() - Purge all outdated originators
1344 * @bat_priv: the bat priv with all the soft interface information
1345 */
1346void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001347{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001348 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001349 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001350 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001351 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001352 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001353 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001354
1355 if (!hash)
1356 return;
1357
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001358 /* for all origins... */
1359 for (i = 0; i < hash->size; i++) {
1360 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001361 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001362
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001363 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001364 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +00001365 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001366 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +08001367 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001368 hlist_del_rcu(&orig_node->hash_entry);
Linus Lüssing9d31b3c2014-12-13 23:32:15 +01001369 batadv_tt_global_del_orig(orig_node->bat_priv,
1370 orig_node, -1,
1371 "originator timed out");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001372 batadv_orig_node_put(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001373 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001374 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001375
1376 batadv_frag_purge_orig(orig_node,
1377 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001378 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001379 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001380 }
1381
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +02001382 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001383}
1384
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001385static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001386{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001387 struct delayed_work *delayed_work;
1388 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001389
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001390 delayed_work = to_delayed_work(work);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001391 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann3b1709d2018-07-07 21:46:11 +02001392 batadv_purge_orig_ref(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +01001393 queue_delayed_work(batadv_event_workqueue,
1394 &bat_priv->orig_work,
1395 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001396}
1397
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001398#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmannff15c272017-12-02 19:51:53 +01001399
1400/**
1401 * batadv_orig_seq_print_text() - Print the originator table in a seq file
1402 * @seq: seq file to print on
1403 * @offset: not used
1404 *
1405 * Return: always 0
1406 */
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001407int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001408{
1409 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001410 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001411 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001412
Marek Lindner30da63a2012-08-03 17:15:46 +02001413 primary_if = batadv_seq_print_text_primary_if_get(seq);
1414 if (!primary_if)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001415 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001416
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001417 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 +02001418 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001419 primary_if->net_dev->dev_addr, net_dev->name,
Antonio Quartulli29824a52016-05-25 23:27:31 +08001420 bat_priv->algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001421
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001422 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001423
Antonio Quartulli29824a52016-05-25 23:27:31 +08001424 if (!bat_priv->algo_ops->orig.print) {
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001425 seq_puts(seq,
1426 "No printing function for this routing protocol\n");
1427 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001428 }
1429
Antonio Quartulli29824a52016-05-25 23:27:31 +08001430 bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001431
Marek Lindner30da63a2012-08-03 17:15:46 +02001432 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001433}
1434
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001435/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001436 * batadv_orig_hardif_seq_print_text() - writes originator infos for a specific
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001437 * outgoing interface
1438 * @seq: debugfs table seq_file struct
1439 * @offset: not used
1440 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001441 * Return: 0
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001442 */
1443int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1444{
1445 struct net_device *net_dev = (struct net_device *)seq->private;
1446 struct batadv_hard_iface *hard_iface;
1447 struct batadv_priv *bat_priv;
1448
1449 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1450
1451 if (!hard_iface || !hard_iface->soft_iface) {
1452 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1453 goto out;
1454 }
1455
1456 bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartulli29824a52016-05-25 23:27:31 +08001457 if (!bat_priv->algo_ops->orig.print) {
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001458 seq_puts(seq,
1459 "No printing function for this routing protocol\n");
1460 goto out;
1461 }
1462
1463 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1464 seq_puts(seq, "Interface not active\n");
1465 goto out;
1466 }
1467
1468 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1469 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1470 hard_iface->net_dev->dev_addr,
Antonio Quartulli29824a52016-05-25 23:27:31 +08001471 hard_iface->soft_iface->name, bat_priv->algo_ops->name);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001472
Antonio Quartulli29824a52016-05-25 23:27:31 +08001473 bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001474
1475out:
Marek Lindner16a41422014-04-24 03:44:25 +08001476 if (hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001477 batadv_hardif_put(hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001478 return 0;
1479}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001480#endif
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001481
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001482/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +01001483 * batadv_orig_dump() - Dump to netlink the originator infos for a specific
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001484 * outgoing interface
1485 * @msg: message to dump into
1486 * @cb: parameters for the dump
1487 *
1488 * Return: 0 or error value
1489 */
1490int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1491{
1492 struct net *net = sock_net(cb->skb->sk);
1493 struct net_device *soft_iface;
1494 struct net_device *hard_iface = NULL;
1495 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1496 struct batadv_priv *bat_priv;
1497 struct batadv_hard_iface *primary_if = NULL;
1498 int ret;
1499 int ifindex, hard_ifindex;
1500
1501 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1502 if (!ifindex)
1503 return -EINVAL;
1504
1505 soft_iface = dev_get_by_index(net, ifindex);
1506 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1507 ret = -ENODEV;
1508 goto out;
1509 }
1510
1511 bat_priv = netdev_priv(soft_iface);
1512
1513 primary_if = batadv_primary_if_get_selected(bat_priv);
1514 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1515 ret = -ENOENT;
1516 goto out;
1517 }
1518
1519 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1520 BATADV_ATTR_HARD_IFINDEX);
1521 if (hard_ifindex) {
1522 hard_iface = dev_get_by_index(net, hard_ifindex);
1523 if (hard_iface)
1524 hardif = batadv_hardif_get_by_netdev(hard_iface);
1525
1526 if (!hardif) {
1527 ret = -ENODEV;
1528 goto out;
1529 }
1530
1531 if (hardif->soft_iface != soft_iface) {
1532 ret = -ENOENT;
1533 goto out;
1534 }
1535 }
1536
1537 if (!bat_priv->algo_ops->orig.dump) {
1538 ret = -EOPNOTSUPP;
1539 goto out;
1540 }
1541
1542 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1543
1544 ret = msg->len;
1545
1546 out:
1547 if (hardif)
1548 batadv_hardif_put(hardif);
1549 if (hard_iface)
1550 dev_put(hard_iface);
1551 if (primary_if)
1552 batadv_hardif_put(primary_if);
1553 if (soft_iface)
1554 dev_put(soft_iface);
1555
1556 return ret;
1557}
1558
Sven Eckelmannff15c272017-12-02 19:51:53 +01001559/**
1560 * batadv_orig_hash_add_if() - Add interface to originators in orig_hash
1561 * @hard_iface: hard interface to add (already slave of the soft interface)
1562 * @max_if_num: new number of interfaces
1563 *
1564 * Return: 0 on success or negative error number in case of failure
1565 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001566int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
Sven Eckelmannf22e0892017-12-26 15:14:01 +01001567 unsigned int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001568{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001569 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartulli29824a52016-05-25 23:27:31 +08001570 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001571 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001572 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001573 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001574 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001575 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001576
1577 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001578 * if_num
1579 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001580 for (i = 0; i < hash->size; i++) {
1581 head = &hash->table[i];
1582
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001583 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001584 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001585 ret = 0;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001586 if (bao->orig.add_if)
1587 ret = bao->orig.add_if(orig_node, max_if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001588 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001589 goto err;
1590 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001591 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001592 }
1593
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001594 return 0;
1595
1596err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001597 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001598 return -ENOMEM;
1599}
1600
Sven Eckelmannff15c272017-12-02 19:51:53 +01001601/**
1602 * batadv_orig_hash_del_if() - Remove interface from originators in orig_hash
1603 * @hard_iface: hard interface to remove (still slave of the soft interface)
1604 * @max_if_num: new number of interfaces
1605 *
1606 * Return: 0 on success or negative error number in case of failure
1607 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001608int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
Sven Eckelmannf22e0892017-12-26 15:14:01 +01001609 unsigned int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001610{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001611 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001612 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001613 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001614 struct batadv_hard_iface *hard_iface_tmp;
1615 struct batadv_orig_node *orig_node;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001616 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001617 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001618 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001619
1620 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001621 * if_num
1622 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001623 for (i = 0; i < hash->size; i++) {
1624 head = &hash->table[i];
1625
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001626 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001627 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001628 ret = 0;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001629 if (bao->orig.del_if)
1630 ret = bao->orig.del_if(orig_node, max_if_num,
1631 hard_iface->if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001632 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001633 goto err;
1634 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001635 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001636 }
1637
1638 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1639 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001640 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001641 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001642 continue;
1643
Marek Lindnere6c10f42011-02-18 12:33:20 +00001644 if (hard_iface == hard_iface_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001645 continue;
1646
Marek Lindnere6c10f42011-02-18 12:33:20 +00001647 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001648 continue;
1649
Marek Lindnere6c10f42011-02-18 12:33:20 +00001650 if (hard_iface_tmp->if_num > hard_iface->if_num)
1651 hard_iface_tmp->if_num--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001652 }
1653 rcu_read_unlock();
1654
Marek Lindnere6c10f42011-02-18 12:33:20 +00001655 hard_iface->if_num = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001656 return 0;
1657
1658err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001659 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001660 return -ENOMEM;
1661}