blob: 8e2a4b205257929e9b64f157e8570972cf1383f9 [file] [log] [blame]
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01001/* Copyright (C) 2009-2017 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020019#include "main.h"
20
Sven Eckelmann7c124392016-01-16 10:29:56 +010021#include <linux/atomic.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020022#include <linux/errno.h>
23#include <linux/etherdevice.h>
24#include <linux/fs.h>
25#include <linux/jiffies.h>
26#include <linux/kernel.h>
Sven Eckelmann90f564d2016-01-16 10:29:40 +010027#include <linux/kref.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020028#include <linux/list.h>
29#include <linux/lockdep.h>
30#include <linux/netdevice.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020031#include <linux/netlink.h>
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080032#include <linux/rculist.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020033#include <linux/seq_file.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020034#include <linux/skbuff.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020035#include <linux/slab.h>
36#include <linux/spinlock.h>
37#include <linux/workqueue.h>
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020038#include <net/sock.h>
39#include <uapi/linux/batman_adv.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020040
Sven Eckelmann01d350d2016-05-15 11:07:44 +020041#include "bat_algo.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020042#include "distributed-arp-table.h"
43#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044#include "gateway_client.h"
45#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020046#include "hash.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020047#include "log.h"
Linus Lüssing60432d72014-02-15 17:47:51 +010048#include "multicast.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020049#include "netlink.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020050#include "network-coding.h"
51#include "routing.h"
Matthias Schiffer85cf8c82016-07-03 13:31:39 +020052#include "soft-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020053#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054
Antonio Quartullidec05072012-11-10 11:00:32 +010055/* hash class keys */
56static struct lock_class_key batadv_orig_hash_lock_class_key;
57
Sven Eckelmann03fc7f82012-05-12 18:34:00 +020058static void batadv_purge_orig(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059
Sven Eckelmann62fe7102015-09-15 19:00:48 +020060/**
Sven Eckelmann7afcbbe2015-10-31 12:29:29 +010061 * batadv_compare_orig - comparing function used in the originator hash table
62 * @node: node in the local table
63 * @data2: second object to compare the node to
Sven Eckelmann62fe7102015-09-15 19:00:48 +020064 *
Sven Eckelmann4b426b12016-02-22 21:02:39 +010065 * Return: true if they are the same originator
Sven Eckelmann62fe7102015-09-15 19:00:48 +020066 */
Sven Eckelmann4b426b12016-02-22 21:02:39 +010067bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020068{
Sven Eckelmann56303d32012-06-05 22:31:31 +020069 const void *data1 = container_of(node, struct batadv_orig_node,
70 hash_entry);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020071
dingtianhong323813e2013-12-26 19:40:39 +080072 return batadv_compare_eth(data1, data2);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020073}
74
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020075/**
76 * batadv_orig_node_vlan_get - get an orig_node_vlan object
77 * @orig_node: the originator serving the VLAN
78 * @vid: the VLAN identifier
79 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +020080 * Return: the vlan object identified by vid and belonging to orig_node or NULL
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020081 * if it does not exist.
82 */
83struct batadv_orig_node_vlan *
84batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
85 unsigned short vid)
86{
87 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
88
89 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080090 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020091 if (tmp->vid != vid)
92 continue;
93
Sven Eckelmann161a3be2016-01-16 10:29:55 +010094 if (!kref_get_unless_zero(&tmp->refcount))
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020095 continue;
96
97 vlan = tmp;
98
99 break;
100 }
101 rcu_read_unlock();
102
103 return vlan;
104}
105
106/**
107 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
108 * object
109 * @orig_node: the originator serving the VLAN
110 * @vid: the VLAN identifier
111 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200112 * Return: NULL in case of failure or the vlan object identified by vid and
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200113 * belonging to orig_node otherwise. The object is created and added to the list
114 * if it does not exist.
115 *
116 * The object is returned with refcounter increased by 1.
117 */
118struct batadv_orig_node_vlan *
119batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
120 unsigned short vid)
121{
122 struct batadv_orig_node_vlan *vlan;
123
124 spin_lock_bh(&orig_node->vlan_list_lock);
125
126 /* first look if an object for this vid already exists */
127 vlan = batadv_orig_node_vlan_get(orig_node, vid);
128 if (vlan)
129 goto out;
130
131 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
132 if (!vlan)
133 goto out;
134
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100135 kref_init(&vlan->refcount);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200136 vlan->vid = vid;
137
Sven Eckelmann09537d12016-07-15 17:39:16 +0200138 kref_get(&vlan->refcount);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800139 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200140
141out:
142 spin_unlock_bh(&orig_node->vlan_list_lock);
143
144 return vlan;
145}
146
147/**
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100148 * batadv_orig_node_vlan_release - release originator-vlan object from lists
149 * and queue for free after rcu grace period
150 * @ref: kref pointer of the originator-vlan object
151 */
152static void batadv_orig_node_vlan_release(struct kref *ref)
153{
154 struct batadv_orig_node_vlan *orig_vlan;
155
156 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
157
158 kfree_rcu(orig_vlan, rcu);
159}
160
161/**
Sven Eckelmann21754e22016-01-17 11:01:24 +0100162 * batadv_orig_node_vlan_put - decrement the refcounter and possibly release
163 * the originator-vlan object
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200164 * @orig_vlan: the originator-vlan object to release
165 */
Sven Eckelmann21754e22016-01-17 11:01:24 +0100166void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200167{
Sven Eckelmann161a3be2016-01-16 10:29:55 +0100168 kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200169}
170
Sven Eckelmann56303d32012-06-05 22:31:31 +0200171int batadv_originator_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000172{
173 if (bat_priv->orig_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200174 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000175
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200176 bat_priv->orig_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177
178 if (!bat_priv->orig_hash)
179 goto err;
180
Antonio Quartullidec05072012-11-10 11:00:32 +0100181 batadv_hash_set_lock_class(bat_priv->orig_hash,
182 &batadv_orig_hash_lock_class_key);
183
Antonio Quartulli72414442012-12-25 13:14:37 +0100184 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
185 queue_delayed_work(batadv_event_workqueue,
186 &bat_priv->orig_work,
187 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
188
Sven Eckelmann5346c352012-05-05 13:27:28 +0200189 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190
191err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200192 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193}
194
Simon Wunderlich89652332013-11-13 19:14:46 +0100195/**
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100196 * batadv_neigh_ifinfo_release - release neigh_ifinfo from lists and queue for
197 * free after rcu grace period
Sven Eckelmann962c6832016-01-16 10:29:51 +0100198 * @ref: kref pointer of the neigh_ifinfo
Simon Wunderlich89652332013-11-13 19:14:46 +0100199 */
Sven Eckelmann962c6832016-01-16 10:29:51 +0100200static void batadv_neigh_ifinfo_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100201{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100202 struct batadv_neigh_ifinfo *neigh_ifinfo;
203
204 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
205
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100206 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100207 batadv_hardif_put(neigh_ifinfo->if_outgoing);
Sven Eckelmannae3e1e32016-01-05 12:06:24 +0100208
209 kfree_rcu(neigh_ifinfo, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100210}
211
212/**
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100213 * batadv_neigh_ifinfo_put - decrement the refcounter and possibly release
Simon Wunderlich89652332013-11-13 19:14:46 +0100214 * the neigh_ifinfo
215 * @neigh_ifinfo: the neigh_ifinfo object to release
216 */
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100217void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
Simon Wunderlich89652332013-11-13 19:14:46 +0100218{
Sven Eckelmann962c6832016-01-16 10:29:51 +0100219 kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
Simon Wunderlich89652332013-11-13 19:14:46 +0100220}
221
222/**
Sven Eckelmannf6389692016-01-05 12:06:23 +0100223 * batadv_hardif_neigh_release - release hardif neigh node from lists and
224 * queue for free after rcu grace period
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100225 * @ref: kref pointer of the neigh_node
Marek Lindnercef63412015-08-04 21:09:55 +0800226 */
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100227static void batadv_hardif_neigh_release(struct kref *ref)
Marek Lindnercef63412015-08-04 21:09:55 +0800228{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100229 struct batadv_hardif_neigh_node *hardif_neigh;
230
231 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
232 refcount);
233
Sven Eckelmannf6389692016-01-05 12:06:23 +0100234 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
235 hlist_del_init_rcu(&hardif_neigh->list);
236 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
Sven Eckelmannbab7c6c2016-01-05 12:06:17 +0100237
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100238 batadv_hardif_put(hardif_neigh->if_incoming);
Sven Eckelmannf6389692016-01-05 12:06:23 +0100239 kfree_rcu(hardif_neigh, rcu);
Marek Lindnercef63412015-08-04 21:09:55 +0800240}
241
242/**
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100243 * batadv_hardif_neigh_put - decrement the hardif neighbors refcounter
Sven Eckelmannf6389692016-01-05 12:06:23 +0100244 * and possibly release it
Marek Lindnercef63412015-08-04 21:09:55 +0800245 * @hardif_neigh: hardif neigh neighbor to free
246 */
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100247void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800248{
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100249 kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
Marek Lindnercef63412015-08-04 21:09:55 +0800250}
251
252/**
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100253 * batadv_neigh_node_release - release neigh_node from lists and queue for
254 * free after rcu grace period
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100255 * @ref: kref pointer of the neigh_node
Simon Wunderlich89652332013-11-13 19:14:46 +0100256 */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100257static void batadv_neigh_node_release(struct kref *ref)
Simon Wunderlich89652332013-11-13 19:14:46 +0100258{
259 struct hlist_node *node_tmp;
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100260 struct batadv_neigh_node *neigh_node;
Simon Wunderlich89652332013-11-13 19:14:46 +0100261 struct batadv_neigh_ifinfo *neigh_ifinfo;
262
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100263 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100264
265 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
266 &neigh_node->ifinfo_list, list) {
Sven Eckelmann044fa3a2016-01-17 11:01:12 +0100267 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich89652332013-11-13 19:14:46 +0100268 }
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800269
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100270 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
Marek Lindnercef63412015-08-04 21:09:55 +0800271
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100272 batadv_hardif_put(neigh_node->if_incoming);
Simon Wunderlich89652332013-11-13 19:14:46 +0100273
Sven Eckelmannb4d922c2016-01-05 12:06:25 +0100274 kfree_rcu(neigh_node, rcu);
Simon Wunderlich89652332013-11-13 19:14:46 +0100275}
276
277/**
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100278 * batadv_neigh_node_put - decrement the neighbors refcounter and possibly
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100279 * release it
Simon Wunderlich89652332013-11-13 19:14:46 +0100280 * @neigh_node: neigh neighbor to free
281 */
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100282void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000283{
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100284 kref_put(&neigh_node->refcount, batadv_neigh_node_release);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000285}
286
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100287/**
Antonio Quartulli6d030de2016-03-11 16:36:19 +0100288 * batadv_orig_router_get - router to the originator depending on iface
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100289 * @orig_node: the orig node for the router
290 * @if_outgoing: the interface where the payload packet has been received or
291 * the OGM should be sent to
292 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200293 * Return: the neighbor which should be router for this orig_node/iface.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100294 *
295 * The object is returned with refcounter increased by 1.
296 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200297struct batadv_neigh_node *
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100298batadv_orig_router_get(struct batadv_orig_node *orig_node,
299 const struct batadv_hard_iface *if_outgoing)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000300{
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100301 struct batadv_orig_ifinfo *orig_ifinfo;
302 struct batadv_neigh_node *router = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000303
304 rcu_read_lock();
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100305 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
306 if (orig_ifinfo->if_outgoing != if_outgoing)
307 continue;
308
309 router = rcu_dereference(orig_ifinfo->router);
310 break;
311 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000312
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100313 if (router && !kref_get_unless_zero(&router->refcount))
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000314 router = NULL;
315
316 rcu_read_unlock();
317 return router;
318}
319
Antonio Quartulli0538f752013-09-02 12:15:01 +0200320/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100321 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
322 * @orig_node: the orig node to be queried
323 * @if_outgoing: the interface for which the ifinfo should be acquired
324 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200325 * Return: the requested orig_ifinfo or NULL if not found.
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100326 *
327 * The object is returned with refcounter increased by 1.
328 */
329struct batadv_orig_ifinfo *
330batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
331 struct batadv_hard_iface *if_outgoing)
332{
333 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
334
335 rcu_read_lock();
336 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
337 list) {
338 if (tmp->if_outgoing != if_outgoing)
339 continue;
340
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100341 if (!kref_get_unless_zero(&tmp->refcount))
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100342 continue;
343
344 orig_ifinfo = tmp;
345 break;
346 }
347 rcu_read_unlock();
348
349 return orig_ifinfo;
350}
351
352/**
353 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
354 * @orig_node: the orig node to be queried
355 * @if_outgoing: the interface for which the ifinfo should be acquired
356 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200357 * Return: NULL in case of failure or the orig_ifinfo object for the if_outgoing
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100358 * interface otherwise. The object is created and added to the list
359 * if it does not exist.
360 *
361 * The object is returned with refcounter increased by 1.
362 */
363struct batadv_orig_ifinfo *
364batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
365 struct batadv_hard_iface *if_outgoing)
366{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200367 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100368 unsigned long reset_time;
369
370 spin_lock_bh(&orig_node->neigh_list_lock);
371
372 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
373 if (orig_ifinfo)
374 goto out;
375
376 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
377 if (!orig_ifinfo)
378 goto out;
379
Sven Eckelmann17a86912016-04-11 13:06:40 +0200380 if (if_outgoing != BATADV_IF_DEFAULT)
381 kref_get(&if_outgoing->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100382
383 reset_time = jiffies - 1;
384 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
385 orig_ifinfo->batman_seqno_reset = reset_time;
386 orig_ifinfo->if_outgoing = if_outgoing;
387 INIT_HLIST_NODE(&orig_ifinfo->list);
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100388 kref_init(&orig_ifinfo->refcount);
Sven Eckelmannf257b992016-07-15 17:39:17 +0200389
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100390 kref_get(&orig_ifinfo->refcount);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100391 hlist_add_head_rcu(&orig_ifinfo->list,
392 &orig_node->ifinfo_list);
393out:
394 spin_unlock_bh(&orig_node->neigh_list_lock);
395 return orig_ifinfo;
396}
397
398/**
Simon Wunderlich89652332013-11-13 19:14:46 +0100399 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200400 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100401 * @if_outgoing: the interface for which the ifinfo should be acquired
402 *
403 * The object is returned with refcounter increased by 1.
404 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200405 * Return: the requested neigh_ifinfo or NULL if not found
Simon Wunderlich89652332013-11-13 19:14:46 +0100406 */
407struct batadv_neigh_ifinfo *
408batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
409 struct batadv_hard_iface *if_outgoing)
410{
411 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
412 *tmp_neigh_ifinfo;
413
414 rcu_read_lock();
415 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
416 list) {
417 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
418 continue;
419
Sven Eckelmann962c6832016-01-16 10:29:51 +0100420 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +0100421 continue;
422
423 neigh_ifinfo = tmp_neigh_ifinfo;
424 break;
425 }
426 rcu_read_unlock();
427
428 return neigh_ifinfo;
429}
430
431/**
432 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
Sven Eckelmanne51f0392015-09-06 21:38:51 +0200433 * @neigh: the neigh node to be queried
Simon Wunderlich89652332013-11-13 19:14:46 +0100434 * @if_outgoing: the interface for which the ifinfo should be acquired
435 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200436 * Return: NULL in case of failure or the neigh_ifinfo object for the
Simon Wunderlich89652332013-11-13 19:14:46 +0100437 * if_outgoing interface otherwise. The object is created and added to the list
438 * if it does not exist.
439 *
440 * The object is returned with refcounter increased by 1.
441 */
442struct batadv_neigh_ifinfo *
443batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
444 struct batadv_hard_iface *if_outgoing)
445{
446 struct batadv_neigh_ifinfo *neigh_ifinfo;
447
448 spin_lock_bh(&neigh->ifinfo_lock);
449
450 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
451 if (neigh_ifinfo)
452 goto out;
453
454 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
455 if (!neigh_ifinfo)
456 goto out;
457
Sven Eckelmann17a86912016-04-11 13:06:40 +0200458 if (if_outgoing)
459 kref_get(&if_outgoing->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100460
461 INIT_HLIST_NODE(&neigh_ifinfo->list);
Sven Eckelmann962c6832016-01-16 10:29:51 +0100462 kref_init(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100463 neigh_ifinfo->if_outgoing = if_outgoing;
464
Sven Eckelmann2e774ac2016-07-15 17:39:19 +0200465 kref_get(&neigh_ifinfo->refcount);
Simon Wunderlich89652332013-11-13 19:14:46 +0100466 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
467
468out:
469 spin_unlock_bh(&neigh->ifinfo_lock);
470
471 return neigh_ifinfo;
472}
473
474/**
Marek Lindnered292662015-08-04 23:31:44 +0800475 * batadv_neigh_node_get - retrieve a neighbour from the list
476 * @orig_node: originator which the neighbour belongs to
477 * @hard_iface: the interface where this neighbour is connected to
478 * @addr: the address of the neighbour
479 *
480 * Looks for and possibly returns a neighbour belonging to this originator list
481 * which is connected through the provided hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200482 *
483 * Return: neighbor when found. Othwerwise NULL
Marek Lindnered292662015-08-04 23:31:44 +0800484 */
485static struct batadv_neigh_node *
486batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
487 const struct batadv_hard_iface *hard_iface,
488 const u8 *addr)
489{
490 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
491
492 rcu_read_lock();
493 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
494 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
495 continue;
496
497 if (tmp_neigh_node->if_incoming != hard_iface)
498 continue;
499
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100500 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
Marek Lindnered292662015-08-04 23:31:44 +0800501 continue;
502
503 res = tmp_neigh_node;
504 break;
505 }
506 rcu_read_unlock();
507
508 return res;
509}
510
511/**
Marek Lindnercef63412015-08-04 21:09:55 +0800512 * batadv_hardif_neigh_create - create a hardif neighbour node
513 * @hard_iface: the interface this neighbour is connected to
514 * @neigh_addr: the interface address of the neighbour to retrieve
Linus Lüssing3111bee2016-08-07 12:34:19 +0200515 * @orig_node: originator object representing the neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800516 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200517 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800518 */
519static struct batadv_hardif_neigh_node *
520batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200521 const u8 *neigh_addr,
522 struct batadv_orig_node *orig_node)
Marek Lindnercef63412015-08-04 21:09:55 +0800523{
Marek Lindner8248a4c2015-08-04 21:09:56 +0800524 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200525 struct batadv_hardif_neigh_node *hardif_neigh;
Marek Lindnercef63412015-08-04 21:09:55 +0800526
527 spin_lock_bh(&hard_iface->neigh_list_lock);
528
529 /* check if neighbor hasn't been added in the meantime */
530 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
531 if (hardif_neigh)
532 goto out;
533
Marek Lindnercef63412015-08-04 21:09:55 +0800534 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
Sven Eckelmann17a86912016-04-11 13:06:40 +0200535 if (!hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800536 goto out;
Marek Lindnercef63412015-08-04 21:09:55 +0800537
Sven Eckelmann17a86912016-04-11 13:06:40 +0200538 kref_get(&hard_iface->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800539 INIT_HLIST_NODE(&hardif_neigh->list);
540 ether_addr_copy(hardif_neigh->addr, neigh_addr);
Linus Lüssing3111bee2016-08-07 12:34:19 +0200541 ether_addr_copy(hardif_neigh->orig, orig_node->orig);
Marek Lindnercef63412015-08-04 21:09:55 +0800542 hardif_neigh->if_incoming = hard_iface;
543 hardif_neigh->last_seen = jiffies;
544
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100545 kref_init(&hardif_neigh->refcount);
Marek Lindnercef63412015-08-04 21:09:55 +0800546
Antonio Quartulli29824a52016-05-25 23:27:31 +0800547 if (bat_priv->algo_ops->neigh.hardif_init)
548 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
Marek Lindner8248a4c2015-08-04 21:09:56 +0800549
Sven Eckelmann9ca488d2016-09-29 17:22:58 +0200550 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
Marek Lindnercef63412015-08-04 21:09:55 +0800551
552out:
553 spin_unlock_bh(&hard_iface->neigh_list_lock);
554 return hardif_neigh;
555}
556
557/**
558 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
559 * node
560 * @hard_iface: the interface this neighbour is connected to
561 * @neigh_addr: the interface address of the neighbour to retrieve
Linus Lüssing3111bee2016-08-07 12:34:19 +0200562 * @orig_node: originator object representing the neighbour
Marek Lindnercef63412015-08-04 21:09:55 +0800563 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200564 * Return: the hardif neighbour node if found or created or NULL otherwise.
Marek Lindnercef63412015-08-04 21:09:55 +0800565 */
566static struct batadv_hardif_neigh_node *
567batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200568 const u8 *neigh_addr,
569 struct batadv_orig_node *orig_node)
Marek Lindnercef63412015-08-04 21:09:55 +0800570{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200571 struct batadv_hardif_neigh_node *hardif_neigh;
Marek Lindnercef63412015-08-04 21:09:55 +0800572
573 /* first check without locking to avoid the overhead */
574 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
575 if (hardif_neigh)
576 return hardif_neigh;
577
Linus Lüssing3111bee2016-08-07 12:34:19 +0200578 return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
Marek Lindnercef63412015-08-04 21:09:55 +0800579}
580
581/**
582 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
583 * @hard_iface: the interface where this neighbour is connected to
584 * @neigh_addr: the address of the neighbour
585 *
586 * Looks for and possibly returns a neighbour belonging to this hard interface.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200587 *
588 * Return: neighbor when found. Othwerwise NULL
Marek Lindnercef63412015-08-04 21:09:55 +0800589 */
590struct batadv_hardif_neigh_node *
591batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
592 const u8 *neigh_addr)
593{
594 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
595
596 rcu_read_lock();
597 hlist_for_each_entry_rcu(tmp_hardif_neigh,
598 &hard_iface->neigh_list, list) {
599 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
600 continue;
601
Sven Eckelmann90f564d2016-01-16 10:29:40 +0100602 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
Marek Lindnercef63412015-08-04 21:09:55 +0800603 continue;
604
605 hardif_neigh = tmp_hardif_neigh;
606 break;
607 }
608 rcu_read_unlock();
609
610 return hardif_neigh;
611}
612
613/**
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800614 * batadv_neigh_node_create - create a neigh node object
Marek Lindner3f32f8a2015-07-26 04:59:15 +0800615 * @orig_node: originator object representing the neighbour
Antonio Quartulli0538f752013-09-02 12:15:01 +0200616 * @hard_iface: the interface where the neighbour is connected to
617 * @neigh_addr: the mac address of the neighbour interface
Antonio Quartulli0538f752013-09-02 12:15:01 +0200618 *
619 * Allocates a new neigh_node object and initialises all the generic fields.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200620 *
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800621 * Return: the neighbour node if found or created or NULL otherwise.
Antonio Quartulli0538f752013-09-02 12:15:01 +0200622 */
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800623static struct batadv_neigh_node *
624batadv_neigh_node_create(struct batadv_orig_node *orig_node,
625 struct batadv_hard_iface *hard_iface,
626 const u8 *neigh_addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000627{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200628 struct batadv_neigh_node *neigh_node;
Marek Lindnercef63412015-08-04 21:09:55 +0800629 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000630
Linus Lüssinge1237052016-01-07 08:11:12 +0100631 spin_lock_bh(&orig_node->neigh_list_lock);
632
Marek Lindner741aa062015-07-26 04:57:43 +0800633 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
634 if (neigh_node)
635 goto out;
636
Marek Lindnercef63412015-08-04 21:09:55 +0800637 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
Linus Lüssing3111bee2016-08-07 12:34:19 +0200638 neigh_addr, orig_node);
Marek Lindnercef63412015-08-04 21:09:55 +0800639 if (!hardif_neigh)
640 goto out;
641
Sven Eckelmann704509b2011-05-14 23:14:54 +0200642 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000643 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800644 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000645
Marek Lindner9591a792010-12-12 21:57:11 +0000646 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlich89652332013-11-13 19:14:46 +0100647 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
648 spin_lock_init(&neigh_node->ifinfo_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000649
Sven Eckelmann17a86912016-04-11 13:06:40 +0200650 kref_get(&hard_iface->refcount);
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100651 ether_addr_copy(neigh_node->addr, neigh_addr);
Antonio Quartulli0538f752013-09-02 12:15:01 +0200652 neigh_node->if_incoming = hard_iface;
653 neigh_node->orig_node = orig_node;
Marek Lindnere48474e2016-03-11 16:01:09 +0100654 neigh_node->last_seen = jiffies;
Antonio Quartulli0538f752013-09-02 12:15:01 +0200655
Sven Eckelmannabe59c62016-03-11 16:44:06 +0100656 /* increment unique neighbor refcount */
657 kref_get(&hardif_neigh->refcount);
658 neigh_node->hardif_neigh = hardif_neigh;
659
Marek Lindner1605d0d2011-02-18 12:28:11 +0000660 /* extra reference for return */
Sven Eckelmann77ae32e2016-01-16 10:29:53 +0100661 kref_init(&neigh_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000662
Sven Eckelmann84274452016-07-15 17:39:20 +0200663 kref_get(&neigh_node->refcount);
Marek Lindner741aa062015-07-26 04:57:43 +0800664 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
Marek Lindner741aa062015-07-26 04:57:43 +0800665
666 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
667 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
668 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
669
Marek Lindner7ae8b282012-03-01 15:35:21 +0800670out:
Linus Lüssinge1237052016-01-07 08:11:12 +0100671 spin_unlock_bh(&orig_node->neigh_list_lock);
672
Marek Lindnercef63412015-08-04 21:09:55 +0800673 if (hardif_neigh)
Sven Eckelmannaccadc32016-01-17 11:01:14 +0100674 batadv_hardif_neigh_put(hardif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000675 return neigh_node;
676}
677
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100678/**
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800679 * batadv_neigh_node_get_or_create - retrieve or create a neigh node object
680 * @orig_node: originator object representing the neighbour
681 * @hard_iface: the interface where the neighbour is connected to
682 * @neigh_addr: the mac address of the neighbour interface
683 *
684 * Return: the neighbour node if found or created or NULL otherwise.
685 */
686struct batadv_neigh_node *
687batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
688 struct batadv_hard_iface *hard_iface,
689 const u8 *neigh_addr)
690{
Sven Eckelmann422d2f72016-07-25 00:42:44 +0200691 struct batadv_neigh_node *neigh_node;
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800692
693 /* first check without locking to avoid the overhead */
694 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
695 if (neigh_node)
696 return neigh_node;
697
698 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
699}
700
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200701#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Marek Lindner6f0a6b52016-05-03 01:52:08 +0800702/**
Marek Lindner75874052015-08-04 21:09:57 +0800703 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
704 * @seq: neighbour table seq_file struct
705 * @offset: not used
706 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200707 * Return: always 0
Marek Lindner75874052015-08-04 21:09:57 +0800708 */
709int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
710{
711 struct net_device *net_dev = (struct net_device *)seq->private;
712 struct batadv_priv *bat_priv = netdev_priv(net_dev);
713 struct batadv_hard_iface *primary_if;
714
715 primary_if = batadv_seq_print_text_primary_if_get(seq);
716 if (!primary_if)
717 return 0;
718
719 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
720 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
721 primary_if->net_dev->dev_addr, net_dev->name,
Antonio Quartulli29824a52016-05-25 23:27:31 +0800722 bat_priv->algo_ops->name);
Marek Lindner75874052015-08-04 21:09:57 +0800723
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100724 batadv_hardif_put(primary_if);
Marek Lindner75874052015-08-04 21:09:57 +0800725
Antonio Quartulli29824a52016-05-25 23:27:31 +0800726 if (!bat_priv->algo_ops->neigh.print) {
Marek Lindner75874052015-08-04 21:09:57 +0800727 seq_puts(seq,
728 "No printing function for this routing protocol\n");
729 return 0;
730 }
731
Antonio Quartulli29824a52016-05-25 23:27:31 +0800732 bat_priv->algo_ops->neigh.print(bat_priv, seq);
Marek Lindner75874052015-08-04 21:09:57 +0800733 return 0;
734}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +0200735#endif
Marek Lindner75874052015-08-04 21:09:57 +0800736
737/**
Matthias Schiffer85cf8c82016-07-03 13:31:39 +0200738 * batadv_hardif_neigh_dump - Dump to netlink the neighbor infos for a specific
739 * outgoing interface
740 * @msg: message to dump into
741 * @cb: parameters for the dump
742 *
743 * Return: 0 or error value
744 */
745int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
746{
747 struct net *net = sock_net(cb->skb->sk);
748 struct net_device *soft_iface;
749 struct net_device *hard_iface = NULL;
750 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
751 struct batadv_priv *bat_priv;
752 struct batadv_hard_iface *primary_if = NULL;
753 int ret;
754 int ifindex, hard_ifindex;
755
756 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
757 if (!ifindex)
758 return -EINVAL;
759
760 soft_iface = dev_get_by_index(net, ifindex);
761 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
762 ret = -ENODEV;
763 goto out;
764 }
765
766 bat_priv = netdev_priv(soft_iface);
767
768 primary_if = batadv_primary_if_get_selected(bat_priv);
769 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
770 ret = -ENOENT;
771 goto out;
772 }
773
774 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
775 BATADV_ATTR_HARD_IFINDEX);
776 if (hard_ifindex) {
777 hard_iface = dev_get_by_index(net, hard_ifindex);
778 if (hard_iface)
779 hardif = batadv_hardif_get_by_netdev(hard_iface);
780
781 if (!hardif) {
782 ret = -ENODEV;
783 goto out;
784 }
785
786 if (hardif->soft_iface != soft_iface) {
787 ret = -ENOENT;
788 goto out;
789 }
790 }
791
792 if (!bat_priv->algo_ops->neigh.dump) {
793 ret = -EOPNOTSUPP;
794 goto out;
795 }
796
797 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
798
799 ret = msg->len;
800
801 out:
802 if (hardif)
803 batadv_hardif_put(hardif);
804 if (hard_iface)
805 dev_put(hard_iface);
806 if (primary_if)
807 batadv_hardif_put(primary_if);
808 if (soft_iface)
809 dev_put(soft_iface);
810
811 return ret;
812}
813
814/**
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100815 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
816 * free after rcu grace period
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100817 * @ref: kref pointer of the orig_ifinfo
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100818 */
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100819static void batadv_orig_ifinfo_release(struct kref *ref)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100820{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100821 struct batadv_orig_ifinfo *orig_ifinfo;
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100822 struct batadv_neigh_node *router;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100823
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100824 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
825
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100826 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100827 batadv_hardif_put(orig_ifinfo->if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100828
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100829 /* this is the last reference to this object */
830 router = rcu_dereference_protected(orig_ifinfo->router, true);
831 if (router)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100832 batadv_neigh_node_put(router);
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100833
834 kfree_rcu(orig_ifinfo, rcu);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100835}
836
837/**
Sven Eckelmann35f94772016-01-17 11:01:13 +0100838 * batadv_orig_ifinfo_put - decrement the refcounter and possibly release
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100839 * the orig_ifinfo
840 * @orig_ifinfo: the orig_ifinfo object to release
841 */
Sven Eckelmann35f94772016-01-17 11:01:13 +0100842void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100843{
Sven Eckelmanna6ba0d32016-01-16 10:29:52 +0100844 kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100845}
846
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100847/**
848 * batadv_orig_node_free_rcu - free the orig_node
849 * @rcu: rcu pointer of the orig_node
850 */
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200851static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000852{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200853 struct batadv_orig_node *orig_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000854
Sven Eckelmann56303d32012-06-05 22:31:31 +0200855 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000856
Linus Lüssing60432d72014-02-15 17:47:51 +0100857 batadv_mcast_purge_orig(orig_node);
858
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200859 batadv_frag_purge_orig(orig_node, NULL);
860
Antonio Quartulli29824a52016-05-25 23:27:31 +0800861 if (orig_node->bat_priv->algo_ops->orig.free)
862 orig_node->bat_priv->algo_ops->orig.free(orig_node);
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200863
Antonio Quartullia73105b2011-04-27 14:27:44 +0200864 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000865 kfree(orig_node);
866}
867
Linus Lüssing72822222013-04-15 21:43:29 +0800868/**
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100869 * batadv_orig_node_release - release orig_node from lists and queue for
870 * free after rcu grace period
Sven Eckelmann7c124392016-01-16 10:29:56 +0100871 * @ref: kref pointer of the orig_node
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100872 */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100873static void batadv_orig_node_release(struct kref *ref)
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100874{
875 struct hlist_node *node_tmp;
876 struct batadv_neigh_node *neigh_node;
Sven Eckelmann7c124392016-01-16 10:29:56 +0100877 struct batadv_orig_node *orig_node;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100878 struct batadv_orig_ifinfo *orig_ifinfo;
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200879 struct batadv_orig_node_vlan *vlan;
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200880 struct batadv_orig_ifinfo *last_candidate;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100881
Sven Eckelmann7c124392016-01-16 10:29:56 +0100882 orig_node = container_of(ref, struct batadv_orig_node, refcount);
883
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100884 spin_lock_bh(&orig_node->neigh_list_lock);
885
886 /* for all neighbors towards this originator ... */
887 hlist_for_each_entry_safe(neigh_node, node_tmp,
888 &orig_node->neigh_list, list) {
889 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100890 batadv_neigh_node_put(neigh_node);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100891 }
892
893 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
894 &orig_node->ifinfo_list, list) {
895 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +0100896 batadv_orig_ifinfo_put(orig_ifinfo);
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100897 }
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200898
899 last_candidate = orig_node->last_bonding_candidate;
900 orig_node->last_bonding_candidate = NULL;
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100901 spin_unlock_bh(&orig_node->neigh_list_lock);
902
Sven Eckelmanncbef1e12016-06-30 21:41:13 +0200903 if (last_candidate)
904 batadv_orig_ifinfo_put(last_candidate);
905
Sven Eckelmann33fbb1f2016-06-30 20:10:46 +0200906 spin_lock_bh(&orig_node->vlan_list_lock);
907 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
908 hlist_del_rcu(&vlan->list);
909 batadv_orig_node_vlan_put(vlan);
910 }
911 spin_unlock_bh(&orig_node->vlan_list_lock);
912
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100913 /* Free nc_nodes */
914 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
915
916 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
917}
918
919/**
Sven Eckelmann5d967312016-01-17 11:01:09 +0100920 * batadv_orig_node_put - decrement the orig node refcounter and possibly
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100921 * release it
Linus Lüssing72822222013-04-15 21:43:29 +0800922 * @orig_node: the orig node to free
923 */
Sven Eckelmann5d967312016-01-17 11:01:09 +0100924void batadv_orig_node_put(struct batadv_orig_node *orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000925{
Sven Eckelmann7c124392016-01-16 10:29:56 +0100926 kref_put(&orig_node->refcount, batadv_orig_node_release);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000927}
928
Sven Eckelmann56303d32012-06-05 22:31:31 +0200929void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000930{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200931 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800932 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000933 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000934 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200935 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200936 u32 i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000937
938 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000939 return;
940
941 cancel_delayed_work_sync(&bat_priv->orig_work);
942
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000943 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000944
945 for (i = 0; i < hash->size; i++) {
946 head = &hash->table[i];
947 list_lock = &hash->list_locks[i];
948
949 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800950 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000951 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800952 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann5d967312016-01-17 11:01:09 +0100953 batadv_orig_node_put(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000954 }
955 spin_unlock_bh(list_lock);
956 }
957
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200958 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000959}
960
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200961/**
962 * batadv_orig_node_new - creates a new orig_node
963 * @bat_priv: the bat priv with all the soft interface information
964 * @addr: the mac address of the originator
965 *
966 * Creates a new originator object and initialise all the generic fields.
967 * The new object is not added to the originator list.
Sven Eckelmann62fe7102015-09-15 19:00:48 +0200968 *
969 * Return: the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200970 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200971struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200972 const u8 *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000973{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200974 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200975 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200976 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200977 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000978
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200979 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
980 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000981
Sven Eckelmann704509b2011-05-14 23:14:54 +0200982 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000983 if (!orig_node)
984 return NULL;
985
Marek Lindner9591a792010-12-12 21:57:11 +0000986 INIT_HLIST_HEAD(&orig_node->neigh_list);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800987 INIT_HLIST_HEAD(&orig_node->vlan_list);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100988 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000989 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000990 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200991 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +0200992 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200993 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000994
Martin Hundebølld56b1702013-01-25 11:12:39 +0100995 batadv_nc_init_orig(orig_node);
996
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000997 /* extra reference for return */
Sven Eckelmann7c124392016-01-16 10:29:56 +0100998 kref_init(&orig_node->refcount);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000999
Marek Lindner16b1aba2011-01-19 20:01:42 +00001000 orig_node->bat_priv = bat_priv;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +01001001 ether_addr_copy(orig_node->orig, addr);
Antonio Quartulli785ea112011-11-23 11:35:44 +01001002 batadv_dat_init_orig_node_addr(orig_node);
Antonio Quartullic8c991b2011-07-07 01:40:57 +02001003 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +02001004 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +02001005 orig_node->tt_buff_len = 0;
Linus Lüssing2c667a32014-10-30 06:23:40 +01001006 orig_node->last_seen = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001007 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
1008 orig_node->bcast_seqno_reset = reset_time;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001009
Linus Lüssing60432d72014-02-15 17:47:51 +01001010#ifdef CONFIG_BATMAN_ADV_MCAST
1011 orig_node->mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing8a4023c2015-06-16 17:10:26 +02001012 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
1013 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
1014 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
1015 spin_lock_init(&orig_node->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +01001016#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001017
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001018 /* create a vlan object for the "untagged" LAN */
1019 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
1020 if (!vlan)
1021 goto free_orig_node;
1022 /* batadv_orig_node_vlan_new() increases the refcounter.
1023 * Immediately release vlan since it is not needed anymore in this
1024 * context
1025 */
Sven Eckelmann21754e22016-01-17 11:01:24 +01001026 batadv_orig_node_vlan_put(vlan);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +02001027
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001028 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
Sven Eckelmann176e5b72016-07-27 12:31:07 +02001029 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001030 spin_lock_init(&orig_node->fragments[i].lock);
1031 orig_node->fragments[i].size = 0;
1032 }
1033
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001034 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001035free_orig_node:
1036 kfree(orig_node);
1037 return NULL;
1038}
1039
Simon Wunderlich89652332013-11-13 19:14:46 +01001040/**
Simon Wunderlich709de132014-03-26 15:46:24 +01001041 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
1042 * @bat_priv: the bat priv with all the soft interface information
1043 * @neigh: orig node which is to be checked
1044 */
1045static void
1046batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1047 struct batadv_neigh_node *neigh)
1048{
1049 struct batadv_neigh_ifinfo *neigh_ifinfo;
1050 struct batadv_hard_iface *if_outgoing;
1051 struct hlist_node *node_tmp;
1052
1053 spin_lock_bh(&neigh->ifinfo_lock);
1054
1055 /* for all ifinfo objects for this neighinator */
1056 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1057 &neigh->ifinfo_list, list) {
1058 if_outgoing = neigh_ifinfo->if_outgoing;
1059
1060 /* always keep the default interface */
1061 if (if_outgoing == BATADV_IF_DEFAULT)
1062 continue;
1063
1064 /* don't purge if the interface is not (going) down */
1065 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
1066 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
1067 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
1068 continue;
1069
1070 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1071 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1072 neigh->addr, if_outgoing->net_dev->name);
1073
1074 hlist_del_rcu(&neigh_ifinfo->list);
Sven Eckelmann044fa3a2016-01-17 11:01:12 +01001075 batadv_neigh_ifinfo_put(neigh_ifinfo);
Simon Wunderlich709de132014-03-26 15:46:24 +01001076 }
1077
1078 spin_unlock_bh(&neigh->ifinfo_lock);
1079}
1080
1081/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001082 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
1083 * @bat_priv: the bat priv with all the soft interface information
1084 * @orig_node: orig node which is to be checked
1085 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001086 * Return: true if any ifinfo entry was purged, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001087 */
1088static bool
1089batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1090 struct batadv_orig_node *orig_node)
1091{
1092 struct batadv_orig_ifinfo *orig_ifinfo;
1093 struct batadv_hard_iface *if_outgoing;
1094 struct hlist_node *node_tmp;
1095 bool ifinfo_purged = false;
1096
1097 spin_lock_bh(&orig_node->neigh_list_lock);
1098
1099 /* for all ifinfo objects for this originator */
1100 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1101 &orig_node->ifinfo_list, list) {
1102 if_outgoing = orig_ifinfo->if_outgoing;
1103
1104 /* always keep the default interface */
1105 if (if_outgoing == BATADV_IF_DEFAULT)
1106 continue;
1107
1108 /* don't purge if the interface is not (going) down */
1109 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
1110 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
1111 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
1112 continue;
1113
1114 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1115 "router/ifinfo purge: originator %pM, iface: %s\n",
1116 orig_node->orig, if_outgoing->net_dev->name);
1117
1118 ifinfo_purged = true;
1119
1120 hlist_del_rcu(&orig_ifinfo->list);
Sven Eckelmann35f94772016-01-17 11:01:13 +01001121 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001122 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1123 orig_node->last_bonding_candidate = NULL;
Sven Eckelmann35f94772016-01-17 11:01:13 +01001124 batadv_orig_ifinfo_put(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001125 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001126 }
1127
1128 spin_unlock_bh(&orig_node->neigh_list_lock);
1129
1130 return ifinfo_purged;
1131}
1132
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001133/**
Simon Wunderlich89652332013-11-13 19:14:46 +01001134 * batadv_purge_orig_neighbors - purges neighbors from originator
1135 * @bat_priv: the bat priv with all the soft interface information
1136 * @orig_node: orig node which is to be checked
1137 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001138 * Return: true if any neighbor was purged, false otherwise
Simon Wunderlich89652332013-11-13 19:14:46 +01001139 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001140static bool
1141batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
Simon Wunderlich89652332013-11-13 19:14:46 +01001142 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001143{
Sasha Levinb67bfe02013-02-27 17:06:00 -08001144 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001145 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001146 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +08001147 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001148 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001149
Marek Lindnerf987ed62010-12-12 21:57:12 +00001150 spin_lock_bh(&orig_node->neigh_list_lock);
1151
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001152 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001153 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +00001154 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001155 last_seen = neigh_node->last_seen;
1156 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001157
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001158 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001159 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
1160 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1161 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001162 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
1163 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1164 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001165 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001166 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1167 orig_node->orig, neigh_node->addr,
1168 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001169 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001170 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001171 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1172 orig_node->orig, neigh_node->addr,
1173 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001174
1175 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +00001176
Marek Lindnerf987ed62010-12-12 21:57:12 +00001177 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001178 batadv_neigh_node_put(neigh_node);
Simon Wunderlich709de132014-03-26 15:46:24 +01001179 } else {
1180 /* only necessary if not the whole neighbor is to be
1181 * deleted, but some interface has been removed.
1182 */
1183 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001184 }
1185 }
Marek Lindnerf987ed62010-12-12 21:57:12 +00001186
1187 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001188 return neigh_purged;
1189}
1190
Simon Wunderlich89652332013-11-13 19:14:46 +01001191/**
1192 * batadv_find_best_neighbor - finds the best neighbor after purging
1193 * @bat_priv: the bat priv with all the soft interface information
1194 * @orig_node: orig node which is to be checked
1195 * @if_outgoing: the interface for which the metric should be compared
1196 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001197 * Return: the current best neighbor, with refcount increased.
Simon Wunderlich89652332013-11-13 19:14:46 +01001198 */
1199static struct batadv_neigh_node *
1200batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1201 struct batadv_orig_node *orig_node,
1202 struct batadv_hard_iface *if_outgoing)
1203{
1204 struct batadv_neigh_node *best = NULL, *neigh;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001205 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Simon Wunderlich89652332013-11-13 19:14:46 +01001206
1207 rcu_read_lock();
1208 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
Antonio Quartulli29824a52016-05-25 23:27:31 +08001209 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1210 if_outgoing) <= 0))
Simon Wunderlich89652332013-11-13 19:14:46 +01001211 continue;
1212
Sven Eckelmann77ae32e2016-01-16 10:29:53 +01001213 if (!kref_get_unless_zero(&neigh->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +01001214 continue;
1215
1216 if (best)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001217 batadv_neigh_node_put(best);
Simon Wunderlich89652332013-11-13 19:14:46 +01001218
1219 best = neigh;
1220 }
1221 rcu_read_unlock();
1222
1223 return best;
1224}
1225
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001226/**
1227 * batadv_purge_orig_node - purges obsolete information from an orig_node
1228 * @bat_priv: the bat priv with all the soft interface information
1229 * @orig_node: orig node which is to be checked
1230 *
1231 * This function checks if the orig_node or substructures of it have become
1232 * obsolete, and purges this information if that's the case.
1233 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001234 * Return: true if the orig_node is to be removed, false otherwise.
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001235 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001236static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1237 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001238{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001239 struct batadv_neigh_node *best_neigh_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001240 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001241 bool changed_ifinfo, changed_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001242
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001243 if (batadv_has_timed_out(orig_node->last_seen,
1244 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001245 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001246 "Originator timeout: originator %pM, last_seen %u\n",
1247 orig_node->orig,
1248 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001249 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001250 }
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001251 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1252 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001253
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001254 if (!changed_ifinfo && !changed_neigh)
Simon Wunderlich89652332013-11-13 19:14:46 +01001255 return false;
1256
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001257 /* first for NULL ... */
Simon Wunderlich89652332013-11-13 19:14:46 +01001258 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1259 BATADV_IF_DEFAULT);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001260 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1261 best_neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001262 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001263 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001264
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001265 /* ... then for all other interfaces. */
1266 rcu_read_lock();
1267 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1268 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1269 continue;
1270
1271 if (hard_iface->soft_iface != bat_priv->soft_iface)
1272 continue;
1273
Sven Eckelmann27353442016-03-05 16:09:16 +01001274 if (!kref_get_unless_zero(&hard_iface->refcount))
1275 continue;
1276
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001277 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1278 orig_node,
1279 hard_iface);
1280 batadv_update_route(bat_priv, orig_node, hard_iface,
1281 best_neigh_node);
1282 if (best_neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +01001283 batadv_neigh_node_put(best_neigh_node);
Sven Eckelmann27353442016-03-05 16:09:16 +01001284
1285 batadv_hardif_put(hard_iface);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001286 }
1287 rcu_read_unlock();
1288
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001289 return false;
1290}
1291
Sven Eckelmann56303d32012-06-05 22:31:31 +02001292static void _batadv_purge_orig(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001293{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001294 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001295 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001296 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001297 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001298 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001299 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001300
1301 if (!hash)
1302 return;
1303
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001304 /* for all origins... */
1305 for (i = 0; i < hash->size; i++) {
1306 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001307 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001308
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001309 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001310 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +00001311 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001312 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +08001313 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001314 hlist_del_rcu(&orig_node->hash_entry);
Linus Lüssing9d31b3c2014-12-13 23:32:15 +01001315 batadv_tt_global_del_orig(orig_node->bat_priv,
1316 orig_node, -1,
1317 "originator timed out");
Sven Eckelmann5d967312016-01-17 11:01:09 +01001318 batadv_orig_node_put(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001319 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001320 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001321
1322 batadv_frag_purge_orig(orig_node,
1323 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001324 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001325 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001326 }
1327
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +02001328 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001329}
1330
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001331static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001332{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001333 struct delayed_work *delayed_work;
1334 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001335
Geliang Tang4ba4bc02015-12-28 23:43:37 +08001336 delayed_work = to_delayed_work(work);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001337 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001338 _batadv_purge_orig(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +01001339 queue_delayed_work(batadv_event_workqueue,
1340 &bat_priv->orig_work,
1341 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001342}
1343
Sven Eckelmann56303d32012-06-05 22:31:31 +02001344void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001345{
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001346 _batadv_purge_orig(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001347}
1348
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001349#ifdef CONFIG_BATMAN_ADV_DEBUGFS
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001350int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001351{
1352 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001353 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001354 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001355
Marek Lindner30da63a2012-08-03 17:15:46 +02001356 primary_if = batadv_seq_print_text_primary_if_get(seq);
1357 if (!primary_if)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001358 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001359
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001360 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 +02001361 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001362 primary_if->net_dev->dev_addr, net_dev->name,
Antonio Quartulli29824a52016-05-25 23:27:31 +08001363 bat_priv->algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001364
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001365 batadv_hardif_put(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001366
Antonio Quartulli29824a52016-05-25 23:27:31 +08001367 if (!bat_priv->algo_ops->orig.print) {
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001368 seq_puts(seq,
1369 "No printing function for this routing protocol\n");
1370 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001371 }
1372
Antonio Quartulli29824a52016-05-25 23:27:31 +08001373 bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001374
Marek Lindner30da63a2012-08-03 17:15:46 +02001375 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001376}
1377
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001378/**
1379 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1380 * outgoing interface
1381 * @seq: debugfs table seq_file struct
1382 * @offset: not used
1383 *
Sven Eckelmann62fe7102015-09-15 19:00:48 +02001384 * Return: 0
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001385 */
1386int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1387{
1388 struct net_device *net_dev = (struct net_device *)seq->private;
1389 struct batadv_hard_iface *hard_iface;
1390 struct batadv_priv *bat_priv;
1391
1392 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1393
1394 if (!hard_iface || !hard_iface->soft_iface) {
1395 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1396 goto out;
1397 }
1398
1399 bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartulli29824a52016-05-25 23:27:31 +08001400 if (!bat_priv->algo_ops->orig.print) {
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001401 seq_puts(seq,
1402 "No printing function for this routing protocol\n");
1403 goto out;
1404 }
1405
1406 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1407 seq_puts(seq, "Interface not active\n");
1408 goto out;
1409 }
1410
1411 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1412 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1413 hard_iface->net_dev->dev_addr,
Antonio Quartulli29824a52016-05-25 23:27:31 +08001414 hard_iface->soft_iface->name, bat_priv->algo_ops->name);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001415
Antonio Quartulli29824a52016-05-25 23:27:31 +08001416 bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001417
1418out:
Marek Lindner16a41422014-04-24 03:44:25 +08001419 if (hard_iface)
Sven Eckelmann82047ad2016-01-17 11:01:10 +01001420 batadv_hardif_put(hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001421 return 0;
1422}
Sven Eckelmanndc1cbd12016-07-16 09:31:20 +02001423#endif
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001424
Matthias Schiffer85cf8c82016-07-03 13:31:39 +02001425/**
1426 * batadv_orig_dump - Dump to netlink the originator infos for a specific
1427 * outgoing interface
1428 * @msg: message to dump into
1429 * @cb: parameters for the dump
1430 *
1431 * Return: 0 or error value
1432 */
1433int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1434{
1435 struct net *net = sock_net(cb->skb->sk);
1436 struct net_device *soft_iface;
1437 struct net_device *hard_iface = NULL;
1438 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1439 struct batadv_priv *bat_priv;
1440 struct batadv_hard_iface *primary_if = NULL;
1441 int ret;
1442 int ifindex, hard_ifindex;
1443
1444 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1445 if (!ifindex)
1446 return -EINVAL;
1447
1448 soft_iface = dev_get_by_index(net, ifindex);
1449 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1450 ret = -ENODEV;
1451 goto out;
1452 }
1453
1454 bat_priv = netdev_priv(soft_iface);
1455
1456 primary_if = batadv_primary_if_get_selected(bat_priv);
1457 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1458 ret = -ENOENT;
1459 goto out;
1460 }
1461
1462 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1463 BATADV_ATTR_HARD_IFINDEX);
1464 if (hard_ifindex) {
1465 hard_iface = dev_get_by_index(net, hard_ifindex);
1466 if (hard_iface)
1467 hardif = batadv_hardif_get_by_netdev(hard_iface);
1468
1469 if (!hardif) {
1470 ret = -ENODEV;
1471 goto out;
1472 }
1473
1474 if (hardif->soft_iface != soft_iface) {
1475 ret = -ENOENT;
1476 goto out;
1477 }
1478 }
1479
1480 if (!bat_priv->algo_ops->orig.dump) {
1481 ret = -EOPNOTSUPP;
1482 goto out;
1483 }
1484
1485 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1486
1487 ret = msg->len;
1488
1489 out:
1490 if (hardif)
1491 batadv_hardif_put(hardif);
1492 if (hard_iface)
1493 dev_put(hard_iface);
1494 if (primary_if)
1495 batadv_hardif_put(primary_if);
1496 if (soft_iface)
1497 dev_put(soft_iface);
1498
1499 return ret;
1500}
1501
Sven Eckelmann56303d32012-06-05 22:31:31 +02001502int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1503 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001504{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001505 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartulli29824a52016-05-25 23:27:31 +08001506 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001507 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001508 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001509 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001510 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001511 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001512
1513 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001514 * if_num
1515 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001516 for (i = 0; i < hash->size; i++) {
1517 head = &hash->table[i];
1518
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001519 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001520 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001521 ret = 0;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001522 if (bao->orig.add_if)
1523 ret = bao->orig.add_if(orig_node, max_if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001524 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001525 goto err;
1526 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001527 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001528 }
1529
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001530 return 0;
1531
1532err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001533 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001534 return -ENOMEM;
1535}
1536
Sven Eckelmann56303d32012-06-05 22:31:31 +02001537int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1538 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001539{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001540 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001541 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001542 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001543 struct batadv_hard_iface *hard_iface_tmp;
1544 struct batadv_orig_node *orig_node;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001545 struct batadv_algo_ops *bao = bat_priv->algo_ops;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001546 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001547 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001548
1549 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001550 * if_num
1551 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001552 for (i = 0; i < hash->size; i++) {
1553 head = &hash->table[i];
1554
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001555 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001556 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001557 ret = 0;
Antonio Quartulli29824a52016-05-25 23:27:31 +08001558 if (bao->orig.del_if)
1559 ret = bao->orig.del_if(orig_node, max_if_num,
1560 hard_iface->if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001561 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001562 goto err;
1563 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001564 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001565 }
1566
1567 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1568 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001569 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001570 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001571 continue;
1572
Marek Lindnere6c10f42011-02-18 12:33:20 +00001573 if (hard_iface == hard_iface_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001574 continue;
1575
Marek Lindnere6c10f42011-02-18 12:33:20 +00001576 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001577 continue;
1578
Marek Lindnere6c10f42011-02-18 12:33:20 +00001579 if (hard_iface_tmp->if_num > hard_iface->if_num)
1580 hard_iface_tmp->if_num--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001581 }
1582 rcu_read_unlock();
1583
Marek Lindnere6c10f42011-02-18 12:33:20 +00001584 hard_iface->if_num = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001585 return 0;
1586
1587err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001588 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001589 return -ENOMEM;
1590}