blob: 0b7ab38e74fc1ed276366e1ccda4d16b5a7519c5 [file] [log] [blame]
Sven Eckelmann9f6446c2015-04-23 13:16:35 +02001/* Copyright (C) 2009-2015 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
21#include <linux/errno.h>
22#include <linux/etherdevice.h>
23#include <linux/fs.h>
24#include <linux/jiffies.h>
25#include <linux/kernel.h>
26#include <linux/list.h>
27#include <linux/lockdep.h>
28#include <linux/netdevice.h>
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080029#include <linux/rculist.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020030#include <linux/seq_file.h>
31#include <linux/slab.h>
32#include <linux/spinlock.h>
33#include <linux/workqueue.h>
34
35#include "distributed-arp-table.h"
36#include "fragmentation.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000037#include "gateway_client.h"
38#include "hard-interface.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020039#include "hash.h"
Linus Lüssing60432d72014-02-15 17:47:51 +010040#include "multicast.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020041#include "network-coding.h"
42#include "routing.h"
43#include "translation-table.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044
Antonio Quartullidec05072012-11-10 11:00:32 +010045/* hash class keys */
46static struct lock_class_key batadv_orig_hash_lock_class_key;
47
Sven Eckelmann03fc7f82012-05-12 18:34:00 +020048static void batadv_purge_orig(struct work_struct *work);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020050/* returns 1 if they are the same originator */
Antonio Quartullibbad0a52013-09-02 12:15:02 +020051int batadv_compare_orig(const struct hlist_node *node, const void *data2)
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020052{
Sven Eckelmann56303d32012-06-05 22:31:31 +020053 const void *data1 = container_of(node, struct batadv_orig_node,
54 hash_entry);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020055
dingtianhong323813e2013-12-26 19:40:39 +080056 return batadv_compare_eth(data1, data2);
Sven Eckelmannb8e2dd12011-06-15 15:08:59 +020057}
58
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020059/**
60 * batadv_orig_node_vlan_get - get an orig_node_vlan object
61 * @orig_node: the originator serving the VLAN
62 * @vid: the VLAN identifier
63 *
64 * Returns the vlan object identified by vid and belonging to orig_node or NULL
65 * if it does not exist.
66 */
67struct batadv_orig_node_vlan *
68batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
69 unsigned short vid)
70{
71 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
72
73 rcu_read_lock();
Marek Lindnerd0fa4f32015-06-22 00:30:22 +080074 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +020075 if (tmp->vid != vid)
76 continue;
77
78 if (!atomic_inc_not_zero(&tmp->refcount))
79 continue;
80
81 vlan = tmp;
82
83 break;
84 }
85 rcu_read_unlock();
86
87 return vlan;
88}
89
90/**
91 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
92 * object
93 * @orig_node: the originator serving the VLAN
94 * @vid: the VLAN identifier
95 *
96 * Returns NULL in case of failure or the vlan object identified by vid and
97 * belonging to orig_node otherwise. The object is created and added to the list
98 * if it does not exist.
99 *
100 * The object is returned with refcounter increased by 1.
101 */
102struct batadv_orig_node_vlan *
103batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
104 unsigned short vid)
105{
106 struct batadv_orig_node_vlan *vlan;
107
108 spin_lock_bh(&orig_node->vlan_list_lock);
109
110 /* first look if an object for this vid already exists */
111 vlan = batadv_orig_node_vlan_get(orig_node, vid);
112 if (vlan)
113 goto out;
114
115 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
116 if (!vlan)
117 goto out;
118
119 atomic_set(&vlan->refcount, 2);
120 vlan->vid = vid;
121
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800122 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200123
124out:
125 spin_unlock_bh(&orig_node->vlan_list_lock);
126
127 return vlan;
128}
129
130/**
131 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
132 * the originator-vlan object
133 * @orig_vlan: the originator-vlan object to release
134 */
135void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
136{
137 if (atomic_dec_and_test(&orig_vlan->refcount))
138 kfree_rcu(orig_vlan, rcu);
139}
140
Sven Eckelmann56303d32012-06-05 22:31:31 +0200141int batadv_originator_init(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000142{
143 if (bat_priv->orig_hash)
Sven Eckelmann5346c352012-05-05 13:27:28 +0200144 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000145
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200146 bat_priv->orig_hash = batadv_hash_new(1024);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000147
148 if (!bat_priv->orig_hash)
149 goto err;
150
Antonio Quartullidec05072012-11-10 11:00:32 +0100151 batadv_hash_set_lock_class(bat_priv->orig_hash,
152 &batadv_orig_hash_lock_class_key);
153
Antonio Quartulli72414442012-12-25 13:14:37 +0100154 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
155 queue_delayed_work(batadv_event_workqueue,
156 &bat_priv->orig_work,
157 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
158
Sven Eckelmann5346c352012-05-05 13:27:28 +0200159 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000160
161err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200162 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163}
164
Simon Wunderlich89652332013-11-13 19:14:46 +0100165/**
166 * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
167 * @rcu: rcu pointer of the neigh_ifinfo object
168 */
169static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu)
170{
171 struct batadv_neigh_ifinfo *neigh_ifinfo;
172
173 neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu);
174
175 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
176 batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing);
177
178 kfree(neigh_ifinfo);
179}
180
181/**
182 * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free
183 * the neigh_ifinfo (without rcu callback)
184 * @neigh_ifinfo: the neigh_ifinfo object to release
185 */
186static void
187batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo)
188{
189 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
190 batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu);
191}
192
193/**
194 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free
195 * the neigh_ifinfo
196 * @neigh_ifinfo: the neigh_ifinfo object to release
197 */
198void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
199{
200 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
201 call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu);
202}
203
204/**
Sven Eckelmannf6389692016-01-05 12:06:23 +0100205 * batadv_hardif_neigh_release - release hardif neigh node from lists and
206 * queue for free after rcu grace period
Marek Lindnercef63412015-08-04 21:09:55 +0800207 * @hardif_neigh: hardif neigh neighbor to free
208 */
209static void
Sven Eckelmannf6389692016-01-05 12:06:23 +0100210batadv_hardif_neigh_release(struct batadv_hardif_neigh_node *hardif_neigh)
Marek Lindnercef63412015-08-04 21:09:55 +0800211{
Sven Eckelmannf6389692016-01-05 12:06:23 +0100212 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
213 hlist_del_init_rcu(&hardif_neigh->list);
214 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
Sven Eckelmannbab7c6c2016-01-05 12:06:17 +0100215
Sven Eckelmannf6389692016-01-05 12:06:23 +0100216 batadv_hardif_free_ref(hardif_neigh->if_incoming);
217 kfree_rcu(hardif_neigh, rcu);
Marek Lindnercef63412015-08-04 21:09:55 +0800218}
219
220/**
221 * batadv_hardif_neigh_free_ref - decrement the hardif neighbors refcounter
Sven Eckelmannf6389692016-01-05 12:06:23 +0100222 * and possibly release it
Marek Lindnercef63412015-08-04 21:09:55 +0800223 * @hardif_neigh: hardif neigh neighbor to free
224 */
225void batadv_hardif_neigh_free_ref(struct batadv_hardif_neigh_node *hardif_neigh)
226{
Sven Eckelmannf6389692016-01-05 12:06:23 +0100227 if (atomic_dec_and_test(&hardif_neigh->refcount))
228 batadv_hardif_neigh_release(hardif_neigh);
Marek Lindnercef63412015-08-04 21:09:55 +0800229}
230
231/**
Simon Wunderlich89652332013-11-13 19:14:46 +0100232 * batadv_neigh_node_free_rcu - free the neigh_node
233 * @rcu: rcu pointer of the neigh_node
234 */
235static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
236{
237 struct hlist_node *node_tmp;
238 struct batadv_neigh_node *neigh_node;
Marek Lindnercef63412015-08-04 21:09:55 +0800239 struct batadv_hardif_neigh_node *hardif_neigh;
Simon Wunderlich89652332013-11-13 19:14:46 +0100240 struct batadv_neigh_ifinfo *neigh_ifinfo;
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800241 struct batadv_algo_ops *bao;
Simon Wunderlich89652332013-11-13 19:14:46 +0100242
243 neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800244 bao = neigh_node->orig_node->bat_priv->bat_algo_ops;
Simon Wunderlich89652332013-11-13 19:14:46 +0100245
246 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
247 &neigh_node->ifinfo_list, list) {
248 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo);
249 }
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800250
Marek Lindnercef63412015-08-04 21:09:55 +0800251 hardif_neigh = batadv_hardif_neigh_get(neigh_node->if_incoming,
252 neigh_node->addr);
253 if (hardif_neigh) {
254 /* batadv_hardif_neigh_get() increases refcount too */
Sven Eckelmannf6389692016-01-05 12:06:23 +0100255 batadv_hardif_neigh_free_ref(hardif_neigh);
256 batadv_hardif_neigh_free_ref(hardif_neigh);
Marek Lindnercef63412015-08-04 21:09:55 +0800257 }
258
Antonio Quartullibcef1f32015-03-01 00:50:17 +0800259 if (bao->bat_neigh_free)
260 bao->bat_neigh_free(neigh_node);
261
Simon Wunderlich89652332013-11-13 19:14:46 +0100262 batadv_hardif_free_ref_now(neigh_node->if_incoming);
263
264 kfree(neigh_node);
265}
266
267/**
Simon Wunderlich89652332013-11-13 19:14:46 +0100268 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100269 * and possibly release it
Simon Wunderlich89652332013-11-13 19:14:46 +0100270 * @neigh_node: neigh neighbor to free
271 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200272void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000273{
Marek Lindner44524fc2011-02-10 14:33:53 +0000274 if (atomic_dec_and_test(&neigh_node->refcount))
Simon Wunderlich89652332013-11-13 19:14:46 +0100275 call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
Simon Wunderlicha4c135c2011-01-19 20:01:43 +0000276}
277
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100278/**
279 * batadv_orig_node_get_router - router to the originator depending on iface
280 * @orig_node: the orig node for the router
281 * @if_outgoing: the interface where the payload packet has been received or
282 * the OGM should be sent to
283 *
284 * Returns the neighbor which should be router for this orig_node/iface.
285 *
286 * The object is returned with refcounter increased by 1.
287 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200288struct batadv_neigh_node *
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100289batadv_orig_router_get(struct batadv_orig_node *orig_node,
290 const struct batadv_hard_iface *if_outgoing)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000291{
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100292 struct batadv_orig_ifinfo *orig_ifinfo;
293 struct batadv_neigh_node *router = NULL;
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000294
295 rcu_read_lock();
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100296 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
297 if (orig_ifinfo->if_outgoing != if_outgoing)
298 continue;
299
300 router = rcu_dereference(orig_ifinfo->router);
301 break;
302 }
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000303
304 if (router && !atomic_inc_not_zero(&router->refcount))
305 router = NULL;
306
307 rcu_read_unlock();
308 return router;
309}
310
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200311/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100312 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
313 * @orig_node: the orig node to be queried
314 * @if_outgoing: the interface for which the ifinfo should be acquired
315 *
316 * Returns the requested orig_ifinfo or NULL if not found.
317 *
318 * The object is returned with refcounter increased by 1.
319 */
320struct batadv_orig_ifinfo *
321batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
322 struct batadv_hard_iface *if_outgoing)
323{
324 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
325
326 rcu_read_lock();
327 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
328 list) {
329 if (tmp->if_outgoing != if_outgoing)
330 continue;
331
332 if (!atomic_inc_not_zero(&tmp->refcount))
333 continue;
334
335 orig_ifinfo = tmp;
336 break;
337 }
338 rcu_read_unlock();
339
340 return orig_ifinfo;
341}
342
343/**
344 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
345 * @orig_node: the orig node to be queried
346 * @if_outgoing: the interface for which the ifinfo should be acquired
347 *
348 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
349 * interface otherwise. The object is created and added to the list
350 * if it does not exist.
351 *
352 * The object is returned with refcounter increased by 1.
353 */
354struct batadv_orig_ifinfo *
355batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
356 struct batadv_hard_iface *if_outgoing)
357{
358 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
359 unsigned long reset_time;
360
361 spin_lock_bh(&orig_node->neigh_list_lock);
362
363 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
364 if (orig_ifinfo)
365 goto out;
366
367 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
368 if (!orig_ifinfo)
369 goto out;
370
371 if (if_outgoing != BATADV_IF_DEFAULT &&
372 !atomic_inc_not_zero(&if_outgoing->refcount)) {
373 kfree(orig_ifinfo);
374 orig_ifinfo = NULL;
375 goto out;
376 }
377
378 reset_time = jiffies - 1;
379 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
380 orig_ifinfo->batman_seqno_reset = reset_time;
381 orig_ifinfo->if_outgoing = if_outgoing;
382 INIT_HLIST_NODE(&orig_ifinfo->list);
383 atomic_set(&orig_ifinfo->refcount, 2);
384 hlist_add_head_rcu(&orig_ifinfo->list,
385 &orig_node->ifinfo_list);
386out:
387 spin_unlock_bh(&orig_node->neigh_list_lock);
388 return orig_ifinfo;
389}
390
391/**
Simon Wunderlich89652332013-11-13 19:14:46 +0100392 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
393 * @neigh_node: the neigh node to be queried
394 * @if_outgoing: the interface for which the ifinfo should be acquired
395 *
396 * The object is returned with refcounter increased by 1.
397 *
398 * Returns the requested neigh_ifinfo or NULL if not found
399 */
400struct batadv_neigh_ifinfo *
401batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
402 struct batadv_hard_iface *if_outgoing)
403{
404 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
405 *tmp_neigh_ifinfo;
406
407 rcu_read_lock();
408 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
409 list) {
410 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
411 continue;
412
413 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
414 continue;
415
416 neigh_ifinfo = tmp_neigh_ifinfo;
417 break;
418 }
419 rcu_read_unlock();
420
421 return neigh_ifinfo;
422}
423
424/**
425 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
426 * @neigh_node: the neigh node to be queried
427 * @if_outgoing: the interface for which the ifinfo should be acquired
428 *
429 * Returns NULL in case of failure or the neigh_ifinfo object for the
430 * if_outgoing interface otherwise. The object is created and added to the list
431 * if it does not exist.
432 *
433 * The object is returned with refcounter increased by 1.
434 */
435struct batadv_neigh_ifinfo *
436batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
437 struct batadv_hard_iface *if_outgoing)
438{
439 struct batadv_neigh_ifinfo *neigh_ifinfo;
440
441 spin_lock_bh(&neigh->ifinfo_lock);
442
443 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
444 if (neigh_ifinfo)
445 goto out;
446
447 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
448 if (!neigh_ifinfo)
449 goto out;
450
451 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
452 kfree(neigh_ifinfo);
453 neigh_ifinfo = NULL;
454 goto out;
455 }
456
457 INIT_HLIST_NODE(&neigh_ifinfo->list);
458 atomic_set(&neigh_ifinfo->refcount, 2);
459 neigh_ifinfo->if_outgoing = if_outgoing;
460
461 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
462
463out:
464 spin_unlock_bh(&neigh->ifinfo_lock);
465
466 return neigh_ifinfo;
467}
468
469/**
Marek Lindnered292662015-08-04 23:31:44 +0800470 * batadv_neigh_node_get - retrieve a neighbour from the list
471 * @orig_node: originator which the neighbour belongs to
472 * @hard_iface: the interface where this neighbour is connected to
473 * @addr: the address of the neighbour
474 *
475 * Looks for and possibly returns a neighbour belonging to this originator list
476 * which is connected through the provided hard interface.
477 * Returns NULL if the neighbour is not found.
478 */
479static struct batadv_neigh_node *
480batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
481 const struct batadv_hard_iface *hard_iface,
482 const u8 *addr)
483{
484 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
485
486 rcu_read_lock();
487 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
488 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
489 continue;
490
491 if (tmp_neigh_node->if_incoming != hard_iface)
492 continue;
493
494 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
495 continue;
496
497 res = tmp_neigh_node;
498 break;
499 }
500 rcu_read_unlock();
501
502 return res;
503}
504
505/**
Marek Lindnercef63412015-08-04 21:09:55 +0800506 * batadv_hardif_neigh_create - create a hardif neighbour node
507 * @hard_iface: the interface this neighbour is connected to
508 * @neigh_addr: the interface address of the neighbour to retrieve
509 *
510 * Returns the hardif neighbour node if found or created or NULL otherwise.
511 */
512static struct batadv_hardif_neigh_node *
513batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
514 const u8 *neigh_addr)
515{
Marek Lindner8248a4c2015-08-04 21:09:56 +0800516 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Marek Lindnercef63412015-08-04 21:09:55 +0800517 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
518
519 spin_lock_bh(&hard_iface->neigh_list_lock);
520
521 /* check if neighbor hasn't been added in the meantime */
522 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
523 if (hardif_neigh)
524 goto out;
525
526 if (!atomic_inc_not_zero(&hard_iface->refcount))
527 goto out;
528
529 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
530 if (!hardif_neigh) {
531 batadv_hardif_free_ref(hard_iface);
532 goto out;
533 }
534
535 INIT_HLIST_NODE(&hardif_neigh->list);
536 ether_addr_copy(hardif_neigh->addr, neigh_addr);
537 hardif_neigh->if_incoming = hard_iface;
538 hardif_neigh->last_seen = jiffies;
539
540 atomic_set(&hardif_neigh->refcount, 1);
541
Marek Lindner8248a4c2015-08-04 21:09:56 +0800542 if (bat_priv->bat_algo_ops->bat_hardif_neigh_init)
543 bat_priv->bat_algo_ops->bat_hardif_neigh_init(hardif_neigh);
544
Marek Lindnercef63412015-08-04 21:09:55 +0800545 hlist_add_head(&hardif_neigh->list, &hard_iface->neigh_list);
546
547out:
548 spin_unlock_bh(&hard_iface->neigh_list_lock);
549 return hardif_neigh;
550}
551
552/**
553 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
554 * node
555 * @hard_iface: the interface this neighbour is connected to
556 * @neigh_addr: the interface address of the neighbour to retrieve
557 *
558 * Returns the hardif neighbour node if found or created or NULL otherwise.
559 */
560static struct batadv_hardif_neigh_node *
561batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
562 const u8 *neigh_addr)
563{
564 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
565
566 /* first check without locking to avoid the overhead */
567 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
568 if (hardif_neigh)
569 return hardif_neigh;
570
571 return batadv_hardif_neigh_create(hard_iface, neigh_addr);
572}
573
574/**
575 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
576 * @hard_iface: the interface where this neighbour is connected to
577 * @neigh_addr: the address of the neighbour
578 *
579 * Looks for and possibly returns a neighbour belonging to this hard interface.
580 * Returns NULL if the neighbour is not found.
581 */
582struct batadv_hardif_neigh_node *
583batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
584 const u8 *neigh_addr)
585{
586 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
587
588 rcu_read_lock();
589 hlist_for_each_entry_rcu(tmp_hardif_neigh,
590 &hard_iface->neigh_list, list) {
591 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
592 continue;
593
594 if (!atomic_inc_not_zero(&tmp_hardif_neigh->refcount))
595 continue;
596
597 hardif_neigh = tmp_hardif_neigh;
598 break;
599 }
600 rcu_read_unlock();
601
602 return hardif_neigh;
603}
604
605/**
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200606 * batadv_neigh_node_new - create and init a new neigh_node object
Marek Lindner3f32f8a2015-07-26 04:59:15 +0800607 * @orig_node: originator object representing the neighbour
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200608 * @hard_iface: the interface where the neighbour is connected to
609 * @neigh_addr: the mac address of the neighbour interface
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200610 *
611 * Allocates a new neigh_node object and initialises all the generic fields.
612 * Returns the new object or NULL on failure.
613 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200614struct batadv_neigh_node *
Marek Lindner3f32f8a2015-07-26 04:59:15 +0800615batadv_neigh_node_new(struct batadv_orig_node *orig_node,
616 struct batadv_hard_iface *hard_iface,
617 const u8 *neigh_addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000618{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200619 struct batadv_neigh_node *neigh_node;
Marek Lindnercef63412015-08-04 21:09:55 +0800620 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000621
Marek Lindner741aa062015-07-26 04:57:43 +0800622 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
623 if (neigh_node)
624 goto out;
625
Marek Lindnercef63412015-08-04 21:09:55 +0800626 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
627 neigh_addr);
628 if (!hardif_neigh)
629 goto out;
630
Sven Eckelmann704509b2011-05-14 23:14:54 +0200631 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000632 if (!neigh_node)
Marek Lindner7ae8b282012-03-01 15:35:21 +0800633 goto out;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000634
Marek Lindnerf729dc702015-07-26 04:37:15 +0800635 if (!atomic_inc_not_zero(&hard_iface->refcount)) {
636 kfree(neigh_node);
637 neigh_node = NULL;
638 goto out;
639 }
640
Marek Lindner9591a792010-12-12 21:57:11 +0000641 INIT_HLIST_NODE(&neigh_node->list);
Simon Wunderlich89652332013-11-13 19:14:46 +0100642 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
643 spin_lock_init(&neigh_node->ifinfo_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000644
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100645 ether_addr_copy(neigh_node->addr, neigh_addr);
Antonio Quartulli0538f7592013-09-02 12:15:01 +0200646 neigh_node->if_incoming = hard_iface;
647 neigh_node->orig_node = orig_node;
648
Marek Lindner1605d0d2011-02-18 12:28:11 +0000649 /* extra reference for return */
650 atomic_set(&neigh_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000651
Marek Lindner741aa062015-07-26 04:57:43 +0800652 spin_lock_bh(&orig_node->neigh_list_lock);
653 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
654 spin_unlock_bh(&orig_node->neigh_list_lock);
655
Marek Lindnercef63412015-08-04 21:09:55 +0800656 /* increment unique neighbor refcount */
657 atomic_inc(&hardif_neigh->refcount);
658
Marek Lindner741aa062015-07-26 04:57:43 +0800659 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
660 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
661 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
662
Marek Lindner7ae8b282012-03-01 15:35:21 +0800663out:
Marek Lindnercef63412015-08-04 21:09:55 +0800664 if (hardif_neigh)
665 batadv_hardif_neigh_free_ref(hardif_neigh);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000666 return neigh_node;
667}
668
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100669/**
Marek Lindner75874052015-08-04 21:09:57 +0800670 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
671 * @seq: neighbour table seq_file struct
672 * @offset: not used
673 *
674 * Always returns 0.
675 */
676int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
677{
678 struct net_device *net_dev = (struct net_device *)seq->private;
679 struct batadv_priv *bat_priv = netdev_priv(net_dev);
680 struct batadv_hard_iface *primary_if;
681
682 primary_if = batadv_seq_print_text_primary_if_get(seq);
683 if (!primary_if)
684 return 0;
685
686 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
687 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
688 primary_if->net_dev->dev_addr, net_dev->name,
689 bat_priv->bat_algo_ops->name);
690
691 batadv_hardif_free_ref(primary_if);
692
693 if (!bat_priv->bat_algo_ops->bat_neigh_print) {
694 seq_puts(seq,
695 "No printing function for this routing protocol\n");
696 return 0;
697 }
698
699 bat_priv->bat_algo_ops->bat_neigh_print(bat_priv, seq);
700 return 0;
701}
702
703/**
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100704 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
705 * free after rcu grace period
706 * @orig_ifinfo: the orig_ifinfo object to release
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100707 */
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100708static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo *orig_ifinfo)
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100709{
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100710 struct batadv_neigh_node *router;
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100711
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100712 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100713 batadv_hardif_free_ref(orig_ifinfo->if_outgoing);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100714
Simon Wunderlich000c8df2014-03-26 15:46:22 +0100715 /* this is the last reference to this object */
716 router = rcu_dereference_protected(orig_ifinfo->router, true);
717 if (router)
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100718 batadv_neigh_node_free_ref(router);
719
720 kfree_rcu(orig_ifinfo, rcu);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100721}
722
723/**
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100724 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100725 * the orig_ifinfo
726 * @orig_ifinfo: the orig_ifinfo object to release
727 */
728void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
729{
730 if (atomic_dec_and_test(&orig_ifinfo->refcount))
Sven Eckelmann2baa7532016-01-05 12:06:22 +0100731 batadv_orig_ifinfo_release(orig_ifinfo);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100732}
733
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100734/**
735 * batadv_orig_node_free_rcu - free the orig_node
736 * @rcu: rcu pointer of the orig_node
737 */
Sven Eckelmann03fc7f82012-05-12 18:34:00 +0200738static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000739{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200740 struct batadv_orig_node *orig_node;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000741
Sven Eckelmann56303d32012-06-05 22:31:31 +0200742 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000743
Linus Lüssing60432d72014-02-15 17:47:51 +0100744 batadv_mcast_purge_orig(orig_node);
745
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200746 batadv_frag_purge_orig(orig_node, NULL);
747
Antonio Quartullid0015fd2013-09-03 11:10:23 +0200748 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
749 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
750
Antonio Quartullia73105b2011-04-27 14:27:44 +0200751 kfree(orig_node->tt_buff);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000752 kfree(orig_node);
753}
754
Linus Lüssing72822222013-04-15 21:43:29 +0800755/**
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100756 * batadv_orig_node_release - release orig_node from lists and queue for
757 * free after rcu grace period
758 * @orig_node: the orig node to free
759 */
760static void batadv_orig_node_release(struct batadv_orig_node *orig_node)
761{
762 struct hlist_node *node_tmp;
763 struct batadv_neigh_node *neigh_node;
764 struct batadv_orig_ifinfo *orig_ifinfo;
765
766 spin_lock_bh(&orig_node->neigh_list_lock);
767
768 /* for all neighbors towards this originator ... */
769 hlist_for_each_entry_safe(neigh_node, node_tmp,
770 &orig_node->neigh_list, list) {
771 hlist_del_rcu(&neigh_node->list);
772 batadv_neigh_node_free_ref(neigh_node);
773 }
774
775 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
776 &orig_node->ifinfo_list, list) {
777 hlist_del_rcu(&orig_ifinfo->list);
778 batadv_orig_ifinfo_free_ref(orig_ifinfo);
779 }
780 spin_unlock_bh(&orig_node->neigh_list_lock);
781
782 /* Free nc_nodes */
783 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
784
785 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
786}
787
788/**
Linus Lüssing72822222013-04-15 21:43:29 +0800789 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100790 * release it
Linus Lüssing72822222013-04-15 21:43:29 +0800791 * @orig_node: the orig node to free
792 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200793void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000794{
795 if (atomic_dec_and_test(&orig_node->refcount))
Sven Eckelmanndeed9662016-01-05 12:06:21 +0100796 batadv_orig_node_release(orig_node);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000797}
798
Linus Lüssing72822222013-04-15 21:43:29 +0800799/**
800 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
801 * possibly free it (without rcu callback)
802 * @orig_node: the orig node to free
803 */
804void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
805{
806 if (atomic_dec_and_test(&orig_node->refcount))
807 batadv_orig_node_free_rcu(&orig_node->rcu);
808}
809
Sven Eckelmann56303d32012-06-05 22:31:31 +0200810void batadv_originator_free(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000811{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +0200812 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800813 struct hlist_node *node_tmp;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000814 struct hlist_head *head;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000815 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200816 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200817 u32 i;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000818
819 if (!hash)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000820 return;
821
822 cancel_delayed_work_sync(&bat_priv->orig_work);
823
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000824 bat_priv->orig_hash = NULL;
Marek Lindner16b1aba2011-01-19 20:01:42 +0000825
826 for (i = 0; i < hash->size; i++) {
827 head = &hash->table[i];
828 list_lock = &hash->list_locks[i];
829
830 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800831 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +0000832 head, hash_entry) {
Sasha Levinb67bfe02013-02-27 17:06:00 -0800833 hlist_del_rcu(&orig_node->hash_entry);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200834 batadv_orig_node_free_ref(orig_node);
Marek Lindner16b1aba2011-01-19 20:01:42 +0000835 }
836 spin_unlock_bh(list_lock);
837 }
838
Sven Eckelmann1a8eaf02012-05-12 02:09:32 +0200839 batadv_hash_destroy(hash);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000840}
841
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200842/**
843 * batadv_orig_node_new - creates a new orig_node
844 * @bat_priv: the bat priv with all the soft interface information
845 * @addr: the mac address of the originator
846 *
847 * Creates a new originator object and initialise all the generic fields.
848 * The new object is not added to the originator list.
849 * Returns the newly created object or NULL on failure.
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200850 */
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200851struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200852 const u8 *addr)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000853{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200854 struct batadv_orig_node *orig_node;
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200855 struct batadv_orig_node_vlan *vlan;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200856 unsigned long reset_time;
Antonio Quartullibbad0a52013-09-02 12:15:02 +0200857 int i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000858
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200859 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
860 "Creating new originator: %pM\n", addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000861
Sven Eckelmann704509b2011-05-14 23:14:54 +0200862 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000863 if (!orig_node)
864 return NULL;
865
Marek Lindner9591a792010-12-12 21:57:11 +0000866 INIT_HLIST_HEAD(&orig_node->neigh_list);
Marek Lindnerd0fa4f32015-06-22 00:30:22 +0800867 INIT_HLIST_HEAD(&orig_node->vlan_list);
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100868 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
Marek Lindnerf3e00082011-01-25 21:52:11 +0000869 spin_lock_init(&orig_node->bcast_seqno_lock);
Marek Lindnerf987ed62010-12-12 21:57:12 +0000870 spin_lock_init(&orig_node->neigh_list_lock);
Antonio Quartullia73105b2011-04-27 14:27:44 +0200871 spin_lock_init(&orig_node->tt_buff_lock);
Antonio Quartullia70a9aa2013-07-30 22:16:24 +0200872 spin_lock_init(&orig_node->tt_lock);
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200873 spin_lock_init(&orig_node->vlan_list_lock);
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000874
Martin Hundebølld56b1702013-01-25 11:12:39 +0100875 batadv_nc_init_orig(orig_node);
876
Marek Lindner7b36e8e2011-02-18 12:28:10 +0000877 /* extra reference for return */
878 atomic_set(&orig_node->refcount, 2);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000879
Marek Lindner16b1aba2011-01-19 20:01:42 +0000880 orig_node->bat_priv = bat_priv;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100881 ether_addr_copy(orig_node->orig, addr);
Antonio Quartulli785ea112011-11-23 11:35:44 +0100882 batadv_dat_init_orig_node_addr(orig_node);
Antonio Quartullic8c991b2011-07-07 01:40:57 +0200883 atomic_set(&orig_node->last_ttvn, 0);
Antonio Quartulli2dafb492011-05-05 08:42:45 +0200884 orig_node->tt_buff = NULL;
Antonio Quartullia73105b2011-04-27 14:27:44 +0200885 orig_node->tt_buff_len = 0;
Linus Lüssing2c667a32014-10-30 06:23:40 +0100886 orig_node->last_seen = jiffies;
Sven Eckelmann42d0b042012-06-03 22:19:17 +0200887 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
888 orig_node->bcast_seqno_reset = reset_time;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200889
Linus Lüssing60432d72014-02-15 17:47:51 +0100890#ifdef CONFIG_BATMAN_ADV_MCAST
891 orig_node->mcast_flags = BATADV_NO_FLAGS;
Linus Lüssing8a4023c2015-06-16 17:10:26 +0200892 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
893 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
894 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
895 spin_lock_init(&orig_node->mcast_handler_lock);
Linus Lüssing60432d72014-02-15 17:47:51 +0100896#endif
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000897
Antonio Quartulli7ea7b4a2013-07-30 22:16:25 +0200898 /* create a vlan object for the "untagged" LAN */
899 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
900 if (!vlan)
901 goto free_orig_node;
902 /* batadv_orig_node_vlan_new() increases the refcounter.
903 * Immediately release vlan since it is not needed anymore in this
904 * context
905 */
906 batadv_orig_node_vlan_free_ref(vlan);
907
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +0200908 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
909 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
910 spin_lock_init(&orig_node->fragments[i].lock);
911 orig_node->fragments[i].size = 0;
912 }
913
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000914 return orig_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000915free_orig_node:
916 kfree(orig_node);
917 return NULL;
918}
919
Simon Wunderlich89652332013-11-13 19:14:46 +0100920/**
Simon Wunderlich709de132014-03-26 15:46:24 +0100921 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
922 * @bat_priv: the bat priv with all the soft interface information
923 * @neigh: orig node which is to be checked
924 */
925static void
926batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
927 struct batadv_neigh_node *neigh)
928{
929 struct batadv_neigh_ifinfo *neigh_ifinfo;
930 struct batadv_hard_iface *if_outgoing;
931 struct hlist_node *node_tmp;
932
933 spin_lock_bh(&neigh->ifinfo_lock);
934
935 /* for all ifinfo objects for this neighinator */
936 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
937 &neigh->ifinfo_list, list) {
938 if_outgoing = neigh_ifinfo->if_outgoing;
939
940 /* always keep the default interface */
941 if (if_outgoing == BATADV_IF_DEFAULT)
942 continue;
943
944 /* don't purge if the interface is not (going) down */
945 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
946 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
947 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
948 continue;
949
950 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
951 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
952 neigh->addr, if_outgoing->net_dev->name);
953
954 hlist_del_rcu(&neigh_ifinfo->list);
955 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
956 }
957
958 spin_unlock_bh(&neigh->ifinfo_lock);
959}
960
961/**
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100962 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
963 * @bat_priv: the bat priv with all the soft interface information
964 * @orig_node: orig node which is to be checked
965 *
966 * Returns true if any ifinfo entry was purged, false otherwise.
967 */
968static bool
969batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
970 struct batadv_orig_node *orig_node)
971{
972 struct batadv_orig_ifinfo *orig_ifinfo;
973 struct batadv_hard_iface *if_outgoing;
974 struct hlist_node *node_tmp;
975 bool ifinfo_purged = false;
976
977 spin_lock_bh(&orig_node->neigh_list_lock);
978
979 /* for all ifinfo objects for this originator */
980 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
981 &orig_node->ifinfo_list, list) {
982 if_outgoing = orig_ifinfo->if_outgoing;
983
984 /* always keep the default interface */
985 if (if_outgoing == BATADV_IF_DEFAULT)
986 continue;
987
988 /* don't purge if the interface is not (going) down */
989 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
990 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
991 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
992 continue;
993
994 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
995 "router/ifinfo purge: originator %pM, iface: %s\n",
996 orig_node->orig, if_outgoing->net_dev->name);
997
998 ifinfo_purged = true;
999
1000 hlist_del_rcu(&orig_ifinfo->list);
1001 batadv_orig_ifinfo_free_ref(orig_ifinfo);
Simon Wunderlichf3b3d902013-11-13 19:14:50 +01001002 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1003 orig_node->last_bonding_candidate = NULL;
1004 batadv_orig_ifinfo_free_ref(orig_ifinfo);
1005 }
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001006 }
1007
1008 spin_unlock_bh(&orig_node->neigh_list_lock);
1009
1010 return ifinfo_purged;
1011}
1012
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001013/**
Simon Wunderlich89652332013-11-13 19:14:46 +01001014 * batadv_purge_orig_neighbors - purges neighbors from originator
1015 * @bat_priv: the bat priv with all the soft interface information
1016 * @orig_node: orig node which is to be checked
1017 *
1018 * Returns true if any neighbor was purged, false otherwise
1019 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001020static bool
1021batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
Simon Wunderlich89652332013-11-13 19:14:46 +01001022 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001023{
Sasha Levinb67bfe02013-02-27 17:06:00 -08001024 struct hlist_node *node_tmp;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001025 struct batadv_neigh_node *neigh_node;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001026 bool neigh_purged = false;
Marek Lindner0b0094e2012-03-01 15:35:20 +08001027 unsigned long last_seen;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001028 struct batadv_hard_iface *if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001029
Marek Lindnerf987ed62010-12-12 21:57:12 +00001030 spin_lock_bh(&orig_node->neigh_list_lock);
1031
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001032 /* for all neighbors towards this originator ... */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001033 hlist_for_each_entry_safe(neigh_node, node_tmp,
Marek Lindner9591a792010-12-12 21:57:11 +00001034 &orig_node->neigh_list, list) {
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001035 last_seen = neigh_node->last_seen;
1036 if_incoming = neigh_node->if_incoming;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001037
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001038 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001039 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
1040 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1041 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001042 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
1043 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1044 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001045 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001046 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1047 orig_node->orig, neigh_node->addr,
1048 if_incoming->net_dev->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001049 else
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001050 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001051 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1052 orig_node->orig, neigh_node->addr,
1053 jiffies_to_msecs(last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001054
1055 neigh_purged = true;
Marek Lindner9591a792010-12-12 21:57:11 +00001056
Marek Lindnerf987ed62010-12-12 21:57:12 +00001057 hlist_del_rcu(&neigh_node->list);
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001058 batadv_neigh_node_free_ref(neigh_node);
Simon Wunderlich709de132014-03-26 15:46:24 +01001059 } else {
1060 /* only necessary if not the whole neighbor is to be
1061 * deleted, but some interface has been removed.
1062 */
1063 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001064 }
1065 }
Marek Lindnerf987ed62010-12-12 21:57:12 +00001066
1067 spin_unlock_bh(&orig_node->neigh_list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001068 return neigh_purged;
1069}
1070
Simon Wunderlich89652332013-11-13 19:14:46 +01001071/**
1072 * batadv_find_best_neighbor - finds the best neighbor after purging
1073 * @bat_priv: the bat priv with all the soft interface information
1074 * @orig_node: orig node which is to be checked
1075 * @if_outgoing: the interface for which the metric should be compared
1076 *
1077 * Returns the current best neighbor, with refcount increased.
1078 */
1079static struct batadv_neigh_node *
1080batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1081 struct batadv_orig_node *orig_node,
1082 struct batadv_hard_iface *if_outgoing)
1083{
1084 struct batadv_neigh_node *best = NULL, *neigh;
1085 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1086
1087 rcu_read_lock();
1088 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1089 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
1090 best, if_outgoing) <= 0))
1091 continue;
1092
1093 if (!atomic_inc_not_zero(&neigh->refcount))
1094 continue;
1095
1096 if (best)
1097 batadv_neigh_node_free_ref(best);
1098
1099 best = neigh;
1100 }
1101 rcu_read_unlock();
1102
1103 return best;
1104}
1105
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001106/**
1107 * batadv_purge_orig_node - purges obsolete information from an orig_node
1108 * @bat_priv: the bat priv with all the soft interface information
1109 * @orig_node: orig node which is to be checked
1110 *
1111 * This function checks if the orig_node or substructures of it have become
1112 * obsolete, and purges this information if that's the case.
1113 *
1114 * Returns true if the orig_node is to be removed, false otherwise.
1115 */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001116static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1117 struct batadv_orig_node *orig_node)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001118{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001119 struct batadv_neigh_node *best_neigh_node;
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001120 struct batadv_hard_iface *hard_iface;
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001121 bool changed_ifinfo, changed_neigh;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001122
Sven Eckelmann42d0b042012-06-03 22:19:17 +02001123 if (batadv_has_timed_out(orig_node->last_seen,
1124 2 * BATADV_PURGE_TIMEOUT)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +02001125 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +02001126 "Originator timeout: originator %pM, last_seen %u\n",
1127 orig_node->orig,
1128 jiffies_to_msecs(orig_node->last_seen));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001129 return true;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001130 }
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001131 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1132 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001133
Simon Wunderlich7b955a92014-03-26 15:46:23 +01001134 if (!changed_ifinfo && !changed_neigh)
Simon Wunderlich89652332013-11-13 19:14:46 +01001135 return false;
1136
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001137 /* first for NULL ... */
Simon Wunderlich89652332013-11-13 19:14:46 +01001138 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1139 BATADV_IF_DEFAULT);
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001140 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1141 best_neigh_node);
Simon Wunderlich89652332013-11-13 19:14:46 +01001142 if (best_neigh_node)
1143 batadv_neigh_node_free_ref(best_neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001144
Simon Wunderlich7351a4822013-11-13 19:14:47 +01001145 /* ... then for all other interfaces. */
1146 rcu_read_lock();
1147 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1148 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1149 continue;
1150
1151 if (hard_iface->soft_iface != bat_priv->soft_iface)
1152 continue;
1153
1154 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1155 orig_node,
1156 hard_iface);
1157 batadv_update_route(bat_priv, orig_node, hard_iface,
1158 best_neigh_node);
1159 if (best_neigh_node)
1160 batadv_neigh_node_free_ref(best_neigh_node);
1161 }
1162 rcu_read_unlock();
1163
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001164 return false;
1165}
1166
Sven Eckelmann56303d32012-06-05 22:31:31 +02001167static void _batadv_purge_orig(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001168{
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001169 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001170 struct hlist_node *node_tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001171 struct hlist_head *head;
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001172 spinlock_t *list_lock; /* spinlock to protect write access */
Sven Eckelmann56303d32012-06-05 22:31:31 +02001173 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001174 u32 i;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001175
1176 if (!hash)
1177 return;
1178
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001179 /* for all origins... */
1180 for (i = 0; i < hash->size; i++) {
1181 head = &hash->table[i];
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001182 list_lock = &hash->list_locks[i];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001183
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001184 spin_lock_bh(list_lock);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001185 hlist_for_each_entry_safe(orig_node, node_tmp,
Marek Lindner7aadf882011-02-18 12:28:09 +00001186 head, hash_entry) {
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001187 if (batadv_purge_orig_node(bat_priv, orig_node)) {
Marek Lindner414254e2013-04-23 21:39:58 +08001188 batadv_gw_node_delete(bat_priv, orig_node);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001189 hlist_del_rcu(&orig_node->hash_entry);
Linus Lüssing9d31b3c2014-12-13 23:32:15 +01001190 batadv_tt_global_del_orig(orig_node->bat_priv,
1191 orig_node, -1,
1192 "originator timed out");
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001193 batadv_orig_node_free_ref(orig_node);
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001194 continue;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001195 }
Martin Hundebøll610bfc6bc2013-05-23 16:53:02 +02001196
1197 batadv_frag_purge_orig(orig_node,
1198 batadv_frag_check_entry);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001199 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001200 spin_unlock_bh(list_lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001201 }
1202
Sven Eckelmann7cf06bc2012-05-12 02:09:29 +02001203 batadv_gw_election(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001204}
1205
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001206static void batadv_purge_orig(struct work_struct *work)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001207{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001208 struct delayed_work *delayed_work;
1209 struct batadv_priv *bat_priv;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001210
Sven Eckelmann56303d32012-06-05 22:31:31 +02001211 delayed_work = container_of(work, struct delayed_work, work);
1212 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001213 _batadv_purge_orig(bat_priv);
Antonio Quartulli72414442012-12-25 13:14:37 +01001214 queue_delayed_work(batadv_event_workqueue,
1215 &bat_priv->orig_work,
1216 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001217}
1218
Sven Eckelmann56303d32012-06-05 22:31:31 +02001219void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001220{
Sven Eckelmann03fc7f82012-05-12 18:34:00 +02001221 _batadv_purge_orig(bat_priv);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001222}
1223
Sven Eckelmann7d211ef2012-05-12 02:09:34 +02001224int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001225{
1226 struct net_device *net_dev = (struct net_device *)seq->private;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001227 struct batadv_priv *bat_priv = netdev_priv(net_dev);
Sven Eckelmann56303d32012-06-05 22:31:31 +02001228 struct batadv_hard_iface *primary_if;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001229
Marek Lindner30da63a2012-08-03 17:15:46 +02001230 primary_if = batadv_seq_print_text_primary_if_get(seq);
1231 if (!primary_if)
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001232 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001233
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001234 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 +02001235 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001236 primary_if->net_dev->dev_addr, net_dev->name,
1237 bat_priv->bat_algo_ops->name);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001238
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001239 batadv_hardif_free_ref(primary_if);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001240
Antonio Quartulli737a2a222013-09-02 12:15:03 +02001241 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1242 seq_puts(seq,
1243 "No printing function for this routing protocol\n");
1244 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001245 }
1246
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001247 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1248 BATADV_IF_DEFAULT);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001249
Marek Lindner30da63a2012-08-03 17:15:46 +02001250 return 0;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001251}
1252
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001253/**
1254 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1255 * outgoing interface
1256 * @seq: debugfs table seq_file struct
1257 * @offset: not used
1258 *
1259 * Returns 0
1260 */
1261int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1262{
1263 struct net_device *net_dev = (struct net_device *)seq->private;
1264 struct batadv_hard_iface *hard_iface;
1265 struct batadv_priv *bat_priv;
1266
1267 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1268
1269 if (!hard_iface || !hard_iface->soft_iface) {
1270 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1271 goto out;
1272 }
1273
1274 bat_priv = netdev_priv(hard_iface->soft_iface);
1275 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1276 seq_puts(seq,
1277 "No printing function for this routing protocol\n");
1278 goto out;
1279 }
1280
1281 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1282 seq_puts(seq, "Interface not active\n");
1283 goto out;
1284 }
1285
1286 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1287 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1288 hard_iface->net_dev->dev_addr,
1289 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1290
1291 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1292
1293out:
Marek Lindner16a41422014-04-24 03:44:25 +08001294 if (hard_iface)
1295 batadv_hardif_free_ref(hard_iface);
Simon Wunderlichcb1c92e2013-11-21 11:52:16 +01001296 return 0;
1297}
1298
Sven Eckelmann56303d32012-06-05 22:31:31 +02001299int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1300 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001301{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001302 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001303 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001304 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001305 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001306 struct batadv_orig_node *orig_node;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001307 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001308 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001309
1310 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001311 * if_num
1312 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001313 for (i = 0; i < hash->size; i++) {
1314 head = &hash->table[i];
1315
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001316 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001317 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001318 ret = 0;
1319 if (bao->bat_orig_add_if)
1320 ret = bao->bat_orig_add_if(orig_node,
1321 max_if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001322 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001323 goto err;
1324 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001325 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001326 }
1327
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001328 return 0;
1329
1330err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001331 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001332 return -ENOMEM;
1333}
1334
Sven Eckelmann56303d32012-06-05 22:31:31 +02001335int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1336 int max_if_num)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001337{
Sven Eckelmann56303d32012-06-05 22:31:31 +02001338 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
Sven Eckelmann5bf74e92012-06-05 22:31:28 +02001339 struct batadv_hashtable *hash = bat_priv->orig_hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001340 struct hlist_head *head;
Sven Eckelmann56303d32012-06-05 22:31:31 +02001341 struct batadv_hard_iface *hard_iface_tmp;
1342 struct batadv_orig_node *orig_node;
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001343 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
Sven Eckelmann6b5e9712015-05-26 18:34:26 +02001344 u32 i;
Antonio Quartullic90681b2011-10-05 17:05:25 +02001345 int ret;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001346
1347 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +02001348 * if_num
1349 */
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001350 for (i = 0; i < hash->size; i++) {
1351 head = &hash->table[i];
1352
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001353 rcu_read_lock();
Sasha Levinb67bfe02013-02-27 17:06:00 -08001354 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
Antonio Quartullid0015fd2013-09-03 11:10:23 +02001355 ret = 0;
1356 if (bao->bat_orig_del_if)
1357 ret = bao->bat_orig_del_if(orig_node,
1358 max_if_num,
1359 hard_iface->if_num);
Sven Eckelmann5346c352012-05-05 13:27:28 +02001360 if (ret == -ENOMEM)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001361 goto err;
1362 }
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001363 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001364 }
1365
1366 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1367 rcu_read_lock();
Sven Eckelmann3193e8f2012-05-12 02:09:42 +02001368 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
Sven Eckelmanne9a4f292012-06-03 22:19:19 +02001369 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001370 continue;
1371
Marek Lindnere6c10f42011-02-18 12:33:20 +00001372 if (hard_iface == hard_iface_tmp)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001373 continue;
1374
Marek Lindnere6c10f42011-02-18 12:33:20 +00001375 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001376 continue;
1377
Marek Lindnere6c10f42011-02-18 12:33:20 +00001378 if (hard_iface_tmp->if_num > hard_iface->if_num)
1379 hard_iface_tmp->if_num--;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001380 }
1381 rcu_read_unlock();
1382
Marek Lindnere6c10f42011-02-18 12:33:20 +00001383 hard_iface->if_num = -1;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001384 return 0;
1385
1386err:
Marek Lindnerfb778ea2011-01-19 20:01:40 +00001387 rcu_read_unlock();
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00001388 return -ENOMEM;
1389}