batman-adv: Remove batadv_ types forward declarations
[linux-2.6-block.git] / net / batman-adv / originator.c
CommitLineData
9f6446c7 1/* Copyright (C) 2009-2015 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
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
ebf38fb7 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
16 */
17
1e2c2a4f 18#include "originator.h"
c6c8fea2 19#include "main.h"
1e2c2a4f
SE
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>
29#include <linux/seq_file.h>
30#include <linux/slab.h>
31#include <linux/spinlock.h>
32#include <linux/workqueue.h>
33
785ea114 34#include "distributed-arp-table.h"
1e2c2a4f 35#include "fragmentation.h"
c6c8fea2
SE
36#include "gateway_client.h"
37#include "hard-interface.h"
1e2c2a4f 38#include "hash.h"
60432d75 39#include "multicast.h"
1e2c2a4f
SE
40#include "network-coding.h"
41#include "routing.h"
42#include "translation-table.h"
c6c8fea2 43
dec05074
AQ
44/* hash class keys */
45static struct lock_class_key batadv_orig_hash_lock_class_key;
46
03fc7f86 47static void batadv_purge_orig(struct work_struct *work);
c6c8fea2 48
b8e2dd13 49/* returns 1 if they are the same originator */
bbad0a5e 50int batadv_compare_orig(const struct hlist_node *node, const void *data2)
b8e2dd13 51{
56303d34
SE
52 const void *data1 = container_of(node, struct batadv_orig_node,
53 hash_entry);
b8e2dd13 54
323813ed 55 return batadv_compare_eth(data1, data2);
b8e2dd13
SE
56}
57
7ea7b4a1
AQ
58/**
59 * batadv_orig_node_vlan_get - get an orig_node_vlan object
60 * @orig_node: the originator serving the VLAN
61 * @vid: the VLAN identifier
62 *
63 * Returns the vlan object identified by vid and belonging to orig_node or NULL
64 * if it does not exist.
65 */
66struct batadv_orig_node_vlan *
67batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
68 unsigned short vid)
69{
70 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
71
72 rcu_read_lock();
73 list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
74 if (tmp->vid != vid)
75 continue;
76
77 if (!atomic_inc_not_zero(&tmp->refcount))
78 continue;
79
80 vlan = tmp;
81
82 break;
83 }
84 rcu_read_unlock();
85
86 return vlan;
87}
88
89/**
90 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
91 * object
92 * @orig_node: the originator serving the VLAN
93 * @vid: the VLAN identifier
94 *
95 * Returns NULL in case of failure or the vlan object identified by vid and
96 * belonging to orig_node otherwise. The object is created and added to the list
97 * if it does not exist.
98 *
99 * The object is returned with refcounter increased by 1.
100 */
101struct batadv_orig_node_vlan *
102batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
103 unsigned short vid)
104{
105 struct batadv_orig_node_vlan *vlan;
106
107 spin_lock_bh(&orig_node->vlan_list_lock);
108
109 /* first look if an object for this vid already exists */
110 vlan = batadv_orig_node_vlan_get(orig_node, vid);
111 if (vlan)
112 goto out;
113
114 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
115 if (!vlan)
116 goto out;
117
118 atomic_set(&vlan->refcount, 2);
119 vlan->vid = vid;
120
121 list_add_rcu(&vlan->list, &orig_node->vlan_list);
122
123out:
124 spin_unlock_bh(&orig_node->vlan_list_lock);
125
126 return vlan;
127}
128
129/**
130 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
131 * the originator-vlan object
132 * @orig_vlan: the originator-vlan object to release
133 */
134void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
135{
136 if (atomic_dec_and_test(&orig_vlan->refcount))
137 kfree_rcu(orig_vlan, rcu);
138}
139
56303d34 140int batadv_originator_init(struct batadv_priv *bat_priv)
c6c8fea2
SE
141{
142 if (bat_priv->orig_hash)
5346c35e 143 return 0;
c6c8fea2 144
1a8eaf07 145 bat_priv->orig_hash = batadv_hash_new(1024);
c6c8fea2
SE
146
147 if (!bat_priv->orig_hash)
148 goto err;
149
dec05074
AQ
150 batadv_hash_set_lock_class(bat_priv->orig_hash,
151 &batadv_orig_hash_lock_class_key);
152
72414442
AQ
153 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
154 queue_delayed_work(batadv_event_workqueue,
155 &bat_priv->orig_work,
156 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
157
5346c35e 158 return 0;
c6c8fea2
SE
159
160err:
5346c35e 161 return -ENOMEM;
c6c8fea2
SE
162}
163
89652331
SW
164/**
165 * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
166 * @rcu: rcu pointer of the neigh_ifinfo object
167 */
168static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu)
169{
170 struct batadv_neigh_ifinfo *neigh_ifinfo;
171
172 neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu);
173
174 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
175 batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing);
176
177 kfree(neigh_ifinfo);
178}
179
180/**
181 * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free
182 * the neigh_ifinfo (without rcu callback)
183 * @neigh_ifinfo: the neigh_ifinfo object to release
184 */
185static void
186batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo)
187{
188 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
189 batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu);
190}
191
192/**
193 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free
194 * the neigh_ifinfo
195 * @neigh_ifinfo: the neigh_ifinfo object to release
196 */
197void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
198{
199 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
200 call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu);
201}
202
203/**
204 * batadv_neigh_node_free_rcu - free the neigh_node
205 * @rcu: rcu pointer of the neigh_node
206 */
207static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
208{
209 struct hlist_node *node_tmp;
210 struct batadv_neigh_node *neigh_node;
211 struct batadv_neigh_ifinfo *neigh_ifinfo;
bcef1f3c 212 struct batadv_algo_ops *bao;
89652331
SW
213
214 neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
bcef1f3c 215 bao = neigh_node->orig_node->bat_priv->bat_algo_ops;
89652331
SW
216
217 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
218 &neigh_node->ifinfo_list, list) {
219 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo);
220 }
bcef1f3c
AQ
221
222 if (bao->bat_neigh_free)
223 bao->bat_neigh_free(neigh_node);
224
89652331
SW
225 batadv_hardif_free_ref_now(neigh_node->if_incoming);
226
227 kfree(neigh_node);
228}
229
230/**
231 * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter
232 * and possibly free it (without rcu callback)
233 * @neigh_node: neigh neighbor to free
234 */
235static void
236batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node)
237{
238 if (atomic_dec_and_test(&neigh_node->refcount))
239 batadv_neigh_node_free_rcu(&neigh_node->rcu);
240}
241
242/**
243 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
244 * and possibly free it
245 * @neigh_node: neigh neighbor to free
246 */
56303d34 247void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
a4c135c5 248{
44524fcd 249 if (atomic_dec_and_test(&neigh_node->refcount))
89652331 250 call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
a4c135c5
SW
251}
252
7351a482
SW
253/**
254 * batadv_orig_node_get_router - router to the originator depending on iface
255 * @orig_node: the orig node for the router
256 * @if_outgoing: the interface where the payload packet has been received or
257 * the OGM should be sent to
258 *
259 * Returns the neighbor which should be router for this orig_node/iface.
260 *
261 * The object is returned with refcounter increased by 1.
262 */
56303d34 263struct batadv_neigh_node *
7351a482
SW
264batadv_orig_router_get(struct batadv_orig_node *orig_node,
265 const struct batadv_hard_iface *if_outgoing)
e1a5382f 266{
7351a482
SW
267 struct batadv_orig_ifinfo *orig_ifinfo;
268 struct batadv_neigh_node *router = NULL;
e1a5382f
LL
269
270 rcu_read_lock();
7351a482
SW
271 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
272 if (orig_ifinfo->if_outgoing != if_outgoing)
273 continue;
274
275 router = rcu_dereference(orig_ifinfo->router);
276 break;
277 }
e1a5382f
LL
278
279 if (router && !atomic_inc_not_zero(&router->refcount))
280 router = NULL;
281
282 rcu_read_unlock();
283 return router;
284}
285
7351a482
SW
286/**
287 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
288 * @orig_node: the orig node to be queried
289 * @if_outgoing: the interface for which the ifinfo should be acquired
290 *
291 * Returns the requested orig_ifinfo or NULL if not found.
292 *
293 * The object is returned with refcounter increased by 1.
294 */
295struct batadv_orig_ifinfo *
296batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
297 struct batadv_hard_iface *if_outgoing)
298{
299 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
300
301 rcu_read_lock();
302 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
303 list) {
304 if (tmp->if_outgoing != if_outgoing)
305 continue;
306
307 if (!atomic_inc_not_zero(&tmp->refcount))
308 continue;
309
310 orig_ifinfo = tmp;
311 break;
312 }
313 rcu_read_unlock();
314
315 return orig_ifinfo;
316}
317
318/**
319 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
320 * @orig_node: the orig node to be queried
321 * @if_outgoing: the interface for which the ifinfo should be acquired
322 *
323 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
324 * interface otherwise. The object is created and added to the list
325 * if it does not exist.
326 *
327 * The object is returned with refcounter increased by 1.
328 */
329struct batadv_orig_ifinfo *
330batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
331 struct batadv_hard_iface *if_outgoing)
332{
333 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
334 unsigned long reset_time;
335
336 spin_lock_bh(&orig_node->neigh_list_lock);
337
338 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
339 if (orig_ifinfo)
340 goto out;
341
342 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
343 if (!orig_ifinfo)
344 goto out;
345
346 if (if_outgoing != BATADV_IF_DEFAULT &&
347 !atomic_inc_not_zero(&if_outgoing->refcount)) {
348 kfree(orig_ifinfo);
349 orig_ifinfo = NULL;
350 goto out;
351 }
352
353 reset_time = jiffies - 1;
354 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
355 orig_ifinfo->batman_seqno_reset = reset_time;
356 orig_ifinfo->if_outgoing = if_outgoing;
357 INIT_HLIST_NODE(&orig_ifinfo->list);
358 atomic_set(&orig_ifinfo->refcount, 2);
359 hlist_add_head_rcu(&orig_ifinfo->list,
360 &orig_node->ifinfo_list);
361out:
362 spin_unlock_bh(&orig_node->neigh_list_lock);
363 return orig_ifinfo;
364}
365
89652331
SW
366/**
367 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
368 * @neigh_node: the neigh node to be queried
369 * @if_outgoing: the interface for which the ifinfo should be acquired
370 *
371 * The object is returned with refcounter increased by 1.
372 *
373 * Returns the requested neigh_ifinfo or NULL if not found
374 */
375struct batadv_neigh_ifinfo *
376batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
377 struct batadv_hard_iface *if_outgoing)
378{
379 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
380 *tmp_neigh_ifinfo;
381
382 rcu_read_lock();
383 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
384 list) {
385 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
386 continue;
387
388 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
389 continue;
390
391 neigh_ifinfo = tmp_neigh_ifinfo;
392 break;
393 }
394 rcu_read_unlock();
395
396 return neigh_ifinfo;
397}
398
399/**
400 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
401 * @neigh_node: the neigh node to be queried
402 * @if_outgoing: the interface for which the ifinfo should be acquired
403 *
404 * Returns NULL in case of failure or the neigh_ifinfo object for the
405 * if_outgoing interface otherwise. The object is created and added to the list
406 * if it does not exist.
407 *
408 * The object is returned with refcounter increased by 1.
409 */
410struct batadv_neigh_ifinfo *
411batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
412 struct batadv_hard_iface *if_outgoing)
413{
414 struct batadv_neigh_ifinfo *neigh_ifinfo;
415
416 spin_lock_bh(&neigh->ifinfo_lock);
417
418 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
419 if (neigh_ifinfo)
420 goto out;
421
422 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
423 if (!neigh_ifinfo)
424 goto out;
425
426 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
427 kfree(neigh_ifinfo);
428 neigh_ifinfo = NULL;
429 goto out;
430 }
431
432 INIT_HLIST_NODE(&neigh_ifinfo->list);
433 atomic_set(&neigh_ifinfo->refcount, 2);
434 neigh_ifinfo->if_outgoing = if_outgoing;
435
436 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
437
438out:
439 spin_unlock_bh(&neigh->ifinfo_lock);
440
441 return neigh_ifinfo;
442}
443
0538f759
AQ
444/**
445 * batadv_neigh_node_new - create and init a new neigh_node object
446 * @hard_iface: the interface where the neighbour is connected to
447 * @neigh_addr: the mac address of the neighbour interface
448 * @orig_node: originator object representing the neighbour
449 *
450 * Allocates a new neigh_node object and initialises all the generic fields.
451 * Returns the new object or NULL on failure.
452 */
56303d34
SE
453struct batadv_neigh_node *
454batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
6b5e971a 455 const u8 *neigh_addr, struct batadv_orig_node *orig_node)
c6c8fea2 456{
56303d34 457 struct batadv_neigh_node *neigh_node;
c6c8fea2 458
704509b8 459 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
c6c8fea2 460 if (!neigh_node)
7ae8b285 461 goto out;
c6c8fea2 462
9591a79f 463 INIT_HLIST_NODE(&neigh_node->list);
89652331
SW
464 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
465 spin_lock_init(&neigh_node->ifinfo_lock);
c6c8fea2 466
8fdd0153 467 ether_addr_copy(neigh_node->addr, neigh_addr);
0538f759
AQ
468 neigh_node->if_incoming = hard_iface;
469 neigh_node->orig_node = orig_node;
470
1605d0d6
ML
471 /* extra reference for return */
472 atomic_set(&neigh_node->refcount, 2);
c6c8fea2 473
7ae8b285 474out:
c6c8fea2
SE
475 return neigh_node;
476}
477
08bf0ed2
AQ
478/**
479 * batadv_neigh_node_get - retrieve a neighbour from the list
480 * @orig_node: originator which the neighbour belongs to
481 * @hard_iface: the interface where this neighbour is connected to
482 * @addr: the address of the neighbour
483 *
484 * Looks for and possibly returns a neighbour belonging to this originator list
485 * which is connected through the provided hard interface.
486 * Returns NULL if the neighbour is not found.
487 */
488struct batadv_neigh_node *
489batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
490 const struct batadv_hard_iface *hard_iface,
6b5e971a 491 const u8 *addr)
08bf0ed2
AQ
492{
493 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
494
495 rcu_read_lock();
496 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
497 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
498 continue;
499
500 if (tmp_neigh_node->if_incoming != hard_iface)
501 continue;
502
503 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
504 continue;
505
506 res = tmp_neigh_node;
507 break;
508 }
509 rcu_read_unlock();
510
511 return res;
512}
513
7351a482
SW
514/**
515 * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
516 * @rcu: rcu pointer of the orig_ifinfo object
517 */
518static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu)
519{
520 struct batadv_orig_ifinfo *orig_ifinfo;
000c8dff 521 struct batadv_neigh_node *router;
7351a482
SW
522
523 orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu);
524
525 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
526 batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing);
527
000c8dff
SW
528 /* this is the last reference to this object */
529 router = rcu_dereference_protected(orig_ifinfo->router, true);
530 if (router)
531 batadv_neigh_node_free_ref_now(router);
7351a482
SW
532 kfree(orig_ifinfo);
533}
534
535/**
536 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
537 * the orig_ifinfo (without rcu callback)
538 * @orig_ifinfo: the orig_ifinfo object to release
539 */
540static void
541batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo)
542{
543 if (atomic_dec_and_test(&orig_ifinfo->refcount))
544 batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu);
545}
546
547/**
548 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
549 * the orig_ifinfo
550 * @orig_ifinfo: the orig_ifinfo object to release
551 */
552void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
553{
554 if (atomic_dec_and_test(&orig_ifinfo->refcount))
555 call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu);
556}
557
03fc7f86 558static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
c6c8fea2 559{
b67bfe0d 560 struct hlist_node *node_tmp;
f6c8b711 561 struct batadv_neigh_node *neigh_node;
56303d34 562 struct batadv_orig_node *orig_node;
7351a482 563 struct batadv_orig_ifinfo *orig_ifinfo;
16b1aba8 564
56303d34 565 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
c6c8fea2 566
f987ed6e
ML
567 spin_lock_bh(&orig_node->neigh_list_lock);
568
c6c8fea2 569 /* for all neighbors towards this originator ... */
b67bfe0d 570 hlist_for_each_entry_safe(neigh_node, node_tmp,
9591a79f 571 &orig_node->neigh_list, list) {
f987ed6e 572 hlist_del_rcu(&neigh_node->list);
89652331 573 batadv_neigh_node_free_ref_now(neigh_node);
c6c8fea2
SE
574 }
575
7351a482
SW
576 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
577 &orig_node->ifinfo_list, list) {
578 hlist_del_rcu(&orig_ifinfo->list);
579 batadv_orig_ifinfo_free_ref_now(orig_ifinfo);
580 }
f987ed6e
ML
581 spin_unlock_bh(&orig_node->neigh_list_lock);
582
60432d75
LL
583 batadv_mcast_purge_orig(orig_node);
584
d56b1705
MH
585 /* Free nc_nodes */
586 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
587
610bfc6b
MH
588 batadv_frag_purge_orig(orig_node, NULL);
589
d0015fdd
AQ
590 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
591 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
592
a73105b8 593 kfree(orig_node->tt_buff);
c6c8fea2
SE
594 kfree(orig_node);
595}
596
72822225
LL
597/**
598 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
599 * schedule an rcu callback for freeing it
600 * @orig_node: the orig node to free
601 */
56303d34 602void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
7b36e8ee
ML
603{
604 if (atomic_dec_and_test(&orig_node->refcount))
03fc7f86 605 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
7b36e8ee
ML
606}
607
72822225
LL
608/**
609 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
610 * possibly free it (without rcu callback)
611 * @orig_node: the orig node to free
612 */
613void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
614{
615 if (atomic_dec_and_test(&orig_node->refcount))
616 batadv_orig_node_free_rcu(&orig_node->rcu);
617}
618
56303d34 619void batadv_originator_free(struct batadv_priv *bat_priv)
c6c8fea2 620{
5bf74e9c 621 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 622 struct hlist_node *node_tmp;
16b1aba8 623 struct hlist_head *head;
16b1aba8 624 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 625 struct batadv_orig_node *orig_node;
6b5e971a 626 u32 i;
16b1aba8
ML
627
628 if (!hash)
c6c8fea2
SE
629 return;
630
631 cancel_delayed_work_sync(&bat_priv->orig_work);
632
c6c8fea2 633 bat_priv->orig_hash = NULL;
16b1aba8
ML
634
635 for (i = 0; i < hash->size; i++) {
636 head = &hash->table[i];
637 list_lock = &hash->list_locks[i];
638
639 spin_lock_bh(list_lock);
b67bfe0d 640 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 641 head, hash_entry) {
b67bfe0d 642 hlist_del_rcu(&orig_node->hash_entry);
7d211efc 643 batadv_orig_node_free_ref(orig_node);
16b1aba8
ML
644 }
645 spin_unlock_bh(list_lock);
646 }
647
1a8eaf07 648 batadv_hash_destroy(hash);
c6c8fea2
SE
649}
650
bbad0a5e
AQ
651/**
652 * batadv_orig_node_new - creates a new orig_node
653 * @bat_priv: the bat priv with all the soft interface information
654 * @addr: the mac address of the originator
655 *
656 * Creates a new originator object and initialise all the generic fields.
657 * The new object is not added to the originator list.
658 * Returns the newly created object or NULL on failure.
9cfc7bd6 659 */
bbad0a5e 660struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
6b5e971a 661 const u8 *addr)
c6c8fea2 662{
56303d34 663 struct batadv_orig_node *orig_node;
7ea7b4a1 664 struct batadv_orig_node_vlan *vlan;
42d0b044 665 unsigned long reset_time;
bbad0a5e 666 int i;
c6c8fea2 667
39c75a51
SE
668 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
669 "Creating new originator: %pM\n", addr);
c6c8fea2 670
704509b8 671 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
c6c8fea2
SE
672 if (!orig_node)
673 return NULL;
674
9591a79f 675 INIT_HLIST_HEAD(&orig_node->neigh_list);
7ea7b4a1 676 INIT_LIST_HEAD(&orig_node->vlan_list);
7351a482 677 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
f3e0008f 678 spin_lock_init(&orig_node->bcast_seqno_lock);
f987ed6e 679 spin_lock_init(&orig_node->neigh_list_lock);
a73105b8 680 spin_lock_init(&orig_node->tt_buff_lock);
a70a9aa9 681 spin_lock_init(&orig_node->tt_lock);
7ea7b4a1 682 spin_lock_init(&orig_node->vlan_list_lock);
7b36e8ee 683
d56b1705
MH
684 batadv_nc_init_orig(orig_node);
685
7b36e8ee
ML
686 /* extra reference for return */
687 atomic_set(&orig_node->refcount, 2);
c6c8fea2 688
16b1aba8 689 orig_node->bat_priv = bat_priv;
8fdd0153 690 ether_addr_copy(orig_node->orig, addr);
785ea114 691 batadv_dat_init_orig_node_addr(orig_node);
c8c991bf 692 atomic_set(&orig_node->last_ttvn, 0);
2dafb49d 693 orig_node->tt_buff = NULL;
a73105b8 694 orig_node->tt_buff_len = 0;
2c667a33 695 orig_node->last_seen = jiffies;
42d0b044
SE
696 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
697 orig_node->bcast_seqno_reset = reset_time;
8a4023c5 698
60432d75
LL
699#ifdef CONFIG_BATMAN_ADV_MCAST
700 orig_node->mcast_flags = BATADV_NO_FLAGS;
8a4023c5
LL
701 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
702 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
703 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
704 spin_lock_init(&orig_node->mcast_handler_lock);
60432d75 705#endif
c6c8fea2 706
7ea7b4a1
AQ
707 /* create a vlan object for the "untagged" LAN */
708 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
709 if (!vlan)
710 goto free_orig_node;
711 /* batadv_orig_node_vlan_new() increases the refcounter.
712 * Immediately release vlan since it is not needed anymore in this
713 * context
714 */
715 batadv_orig_node_vlan_free_ref(vlan);
716
610bfc6b
MH
717 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
718 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
719 spin_lock_init(&orig_node->fragments[i].lock);
720 orig_node->fragments[i].size = 0;
721 }
722
c6c8fea2 723 return orig_node;
c6c8fea2
SE
724free_orig_node:
725 kfree(orig_node);
726 return NULL;
727}
728
709de13f
SW
729/**
730 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
731 * @bat_priv: the bat priv with all the soft interface information
732 * @neigh: orig node which is to be checked
733 */
734static void
735batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
736 struct batadv_neigh_node *neigh)
737{
738 struct batadv_neigh_ifinfo *neigh_ifinfo;
739 struct batadv_hard_iface *if_outgoing;
740 struct hlist_node *node_tmp;
741
742 spin_lock_bh(&neigh->ifinfo_lock);
743
744 /* for all ifinfo objects for this neighinator */
745 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
746 &neigh->ifinfo_list, list) {
747 if_outgoing = neigh_ifinfo->if_outgoing;
748
749 /* always keep the default interface */
750 if (if_outgoing == BATADV_IF_DEFAULT)
751 continue;
752
753 /* don't purge if the interface is not (going) down */
754 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
755 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
756 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
757 continue;
758
759 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
760 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
761 neigh->addr, if_outgoing->net_dev->name);
762
763 hlist_del_rcu(&neigh_ifinfo->list);
764 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
765 }
766
767 spin_unlock_bh(&neigh->ifinfo_lock);
768}
769
7351a482
SW
770/**
771 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
772 * @bat_priv: the bat priv with all the soft interface information
773 * @orig_node: orig node which is to be checked
774 *
775 * Returns true if any ifinfo entry was purged, false otherwise.
776 */
777static bool
778batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
779 struct batadv_orig_node *orig_node)
780{
781 struct batadv_orig_ifinfo *orig_ifinfo;
782 struct batadv_hard_iface *if_outgoing;
783 struct hlist_node *node_tmp;
784 bool ifinfo_purged = false;
785
786 spin_lock_bh(&orig_node->neigh_list_lock);
787
788 /* for all ifinfo objects for this originator */
789 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
790 &orig_node->ifinfo_list, list) {
791 if_outgoing = orig_ifinfo->if_outgoing;
792
793 /* always keep the default interface */
794 if (if_outgoing == BATADV_IF_DEFAULT)
795 continue;
796
797 /* don't purge if the interface is not (going) down */
798 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
799 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
800 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
801 continue;
802
803 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
804 "router/ifinfo purge: originator %pM, iface: %s\n",
805 orig_node->orig, if_outgoing->net_dev->name);
806
807 ifinfo_purged = true;
808
809 hlist_del_rcu(&orig_ifinfo->list);
810 batadv_orig_ifinfo_free_ref(orig_ifinfo);
f3b3d901
SW
811 if (orig_node->last_bonding_candidate == orig_ifinfo) {
812 orig_node->last_bonding_candidate = NULL;
813 batadv_orig_ifinfo_free_ref(orig_ifinfo);
814 }
7351a482
SW
815 }
816
817 spin_unlock_bh(&orig_node->neigh_list_lock);
818
819 return ifinfo_purged;
820}
821
89652331
SW
822/**
823 * batadv_purge_orig_neighbors - purges neighbors from originator
824 * @bat_priv: the bat priv with all the soft interface information
825 * @orig_node: orig node which is to be checked
826 *
827 * Returns true if any neighbor was purged, false otherwise
828 */
56303d34
SE
829static bool
830batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
89652331 831 struct batadv_orig_node *orig_node)
c6c8fea2 832{
b67bfe0d 833 struct hlist_node *node_tmp;
56303d34 834 struct batadv_neigh_node *neigh_node;
c6c8fea2 835 bool neigh_purged = false;
0b0094e0 836 unsigned long last_seen;
56303d34 837 struct batadv_hard_iface *if_incoming;
c6c8fea2 838
f987ed6e
ML
839 spin_lock_bh(&orig_node->neigh_list_lock);
840
c6c8fea2 841 /* for all neighbors towards this originator ... */
b67bfe0d 842 hlist_for_each_entry_safe(neigh_node, node_tmp,
9591a79f 843 &orig_node->neigh_list, list) {
1eda58bf
SE
844 last_seen = neigh_node->last_seen;
845 if_incoming = neigh_node->if_incoming;
846
42d0b044 847 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
e9a4f295
SE
848 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
849 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
850 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
e9a4f295
SE
851 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
852 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
853 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
39c75a51 854 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
855 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
856 orig_node->orig, neigh_node->addr,
857 if_incoming->net_dev->name);
c6c8fea2 858 else
39c75a51 859 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
860 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
861 orig_node->orig, neigh_node->addr,
862 jiffies_to_msecs(last_seen));
c6c8fea2
SE
863
864 neigh_purged = true;
9591a79f 865
f987ed6e 866 hlist_del_rcu(&neigh_node->list);
7d211efc 867 batadv_neigh_node_free_ref(neigh_node);
709de13f
SW
868 } else {
869 /* only necessary if not the whole neighbor is to be
870 * deleted, but some interface has been removed.
871 */
872 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
c6c8fea2
SE
873 }
874 }
f987ed6e
ML
875
876 spin_unlock_bh(&orig_node->neigh_list_lock);
c6c8fea2
SE
877 return neigh_purged;
878}
879
89652331
SW
880/**
881 * batadv_find_best_neighbor - finds the best neighbor after purging
882 * @bat_priv: the bat priv with all the soft interface information
883 * @orig_node: orig node which is to be checked
884 * @if_outgoing: the interface for which the metric should be compared
885 *
886 * Returns the current best neighbor, with refcount increased.
887 */
888static struct batadv_neigh_node *
889batadv_find_best_neighbor(struct batadv_priv *bat_priv,
890 struct batadv_orig_node *orig_node,
891 struct batadv_hard_iface *if_outgoing)
892{
893 struct batadv_neigh_node *best = NULL, *neigh;
894 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
895
896 rcu_read_lock();
897 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
898 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
899 best, if_outgoing) <= 0))
900 continue;
901
902 if (!atomic_inc_not_zero(&neigh->refcount))
903 continue;
904
905 if (best)
906 batadv_neigh_node_free_ref(best);
907
908 best = neigh;
909 }
910 rcu_read_unlock();
911
912 return best;
913}
914
7351a482
SW
915/**
916 * batadv_purge_orig_node - purges obsolete information from an orig_node
917 * @bat_priv: the bat priv with all the soft interface information
918 * @orig_node: orig node which is to be checked
919 *
920 * This function checks if the orig_node or substructures of it have become
921 * obsolete, and purges this information if that's the case.
922 *
923 * Returns true if the orig_node is to be removed, false otherwise.
924 */
56303d34
SE
925static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
926 struct batadv_orig_node *orig_node)
c6c8fea2 927{
56303d34 928 struct batadv_neigh_node *best_neigh_node;
7351a482 929 struct batadv_hard_iface *hard_iface;
7b955a9f 930 bool changed_ifinfo, changed_neigh;
c6c8fea2 931
42d0b044
SE
932 if (batadv_has_timed_out(orig_node->last_seen,
933 2 * BATADV_PURGE_TIMEOUT)) {
39c75a51 934 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
935 "Originator timeout: originator %pM, last_seen %u\n",
936 orig_node->orig,
937 jiffies_to_msecs(orig_node->last_seen));
c6c8fea2 938 return true;
c6c8fea2 939 }
7b955a9f
SW
940 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
941 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
7351a482 942
7b955a9f 943 if (!changed_ifinfo && !changed_neigh)
89652331
SW
944 return false;
945
7351a482 946 /* first for NULL ... */
89652331
SW
947 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
948 BATADV_IF_DEFAULT);
7351a482
SW
949 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
950 best_neigh_node);
89652331
SW
951 if (best_neigh_node)
952 batadv_neigh_node_free_ref(best_neigh_node);
c6c8fea2 953
7351a482
SW
954 /* ... then for all other interfaces. */
955 rcu_read_lock();
956 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
957 if (hard_iface->if_status != BATADV_IF_ACTIVE)
958 continue;
959
960 if (hard_iface->soft_iface != bat_priv->soft_iface)
961 continue;
962
963 best_neigh_node = batadv_find_best_neighbor(bat_priv,
964 orig_node,
965 hard_iface);
966 batadv_update_route(bat_priv, orig_node, hard_iface,
967 best_neigh_node);
968 if (best_neigh_node)
969 batadv_neigh_node_free_ref(best_neigh_node);
970 }
971 rcu_read_unlock();
972
c6c8fea2
SE
973 return false;
974}
975
56303d34 976static void _batadv_purge_orig(struct batadv_priv *bat_priv)
c6c8fea2 977{
5bf74e9c 978 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 979 struct hlist_node *node_tmp;
c6c8fea2 980 struct hlist_head *head;
fb778ea1 981 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 982 struct batadv_orig_node *orig_node;
6b5e971a 983 u32 i;
c6c8fea2
SE
984
985 if (!hash)
986 return;
987
c6c8fea2
SE
988 /* for all origins... */
989 for (i = 0; i < hash->size; i++) {
990 head = &hash->table[i];
fb778ea1 991 list_lock = &hash->list_locks[i];
c6c8fea2 992
fb778ea1 993 spin_lock_bh(list_lock);
b67bfe0d 994 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 995 head, hash_entry) {
03fc7f86 996 if (batadv_purge_orig_node(bat_priv, orig_node)) {
414254e3 997 batadv_gw_node_delete(bat_priv, orig_node);
b67bfe0d 998 hlist_del_rcu(&orig_node->hash_entry);
9d31b3ce
LL
999 batadv_tt_global_del_orig(orig_node->bat_priv,
1000 orig_node, -1,
1001 "originator timed out");
7d211efc 1002 batadv_orig_node_free_ref(orig_node);
fb778ea1 1003 continue;
c6c8fea2 1004 }
610bfc6b
MH
1005
1006 batadv_frag_purge_orig(orig_node,
1007 batadv_frag_check_entry);
c6c8fea2 1008 }
fb778ea1 1009 spin_unlock_bh(list_lock);
c6c8fea2
SE
1010 }
1011
7cf06bc6
SE
1012 batadv_gw_node_purge(bat_priv);
1013 batadv_gw_election(bat_priv);
c6c8fea2
SE
1014}
1015
03fc7f86 1016static void batadv_purge_orig(struct work_struct *work)
c6c8fea2 1017{
56303d34
SE
1018 struct delayed_work *delayed_work;
1019 struct batadv_priv *bat_priv;
c6c8fea2 1020
56303d34
SE
1021 delayed_work = container_of(work, struct delayed_work, work);
1022 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
03fc7f86 1023 _batadv_purge_orig(bat_priv);
72414442
AQ
1024 queue_delayed_work(batadv_event_workqueue,
1025 &bat_priv->orig_work,
1026 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
c6c8fea2
SE
1027}
1028
56303d34 1029void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
c6c8fea2 1030{
03fc7f86 1031 _batadv_purge_orig(bat_priv);
c6c8fea2
SE
1032}
1033
7d211efc 1034int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
1035{
1036 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 1037 struct batadv_priv *bat_priv = netdev_priv(net_dev);
56303d34 1038 struct batadv_hard_iface *primary_if;
32ae9b22 1039
30da63a6
ML
1040 primary_if = batadv_seq_print_text_primary_if_get(seq);
1041 if (!primary_if)
737a2a22 1042 return 0;
c6c8fea2 1043
737a2a22 1044 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
42d0b044 1045 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
737a2a22
AQ
1046 primary_if->net_dev->dev_addr, net_dev->name,
1047 bat_priv->bat_algo_ops->name);
c6c8fea2 1048
737a2a22 1049 batadv_hardif_free_ref(primary_if);
c6c8fea2 1050
737a2a22
AQ
1051 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1052 seq_puts(seq,
1053 "No printing function for this routing protocol\n");
1054 return 0;
c6c8fea2
SE
1055 }
1056
cb1c92ec
SW
1057 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1058 BATADV_IF_DEFAULT);
c6c8fea2 1059
30da63a6 1060 return 0;
c6c8fea2
SE
1061}
1062
cb1c92ec
SW
1063/**
1064 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1065 * outgoing interface
1066 * @seq: debugfs table seq_file struct
1067 * @offset: not used
1068 *
1069 * Returns 0
1070 */
1071int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1072{
1073 struct net_device *net_dev = (struct net_device *)seq->private;
1074 struct batadv_hard_iface *hard_iface;
1075 struct batadv_priv *bat_priv;
1076
1077 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1078
1079 if (!hard_iface || !hard_iface->soft_iface) {
1080 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1081 goto out;
1082 }
1083
1084 bat_priv = netdev_priv(hard_iface->soft_iface);
1085 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1086 seq_puts(seq,
1087 "No printing function for this routing protocol\n");
1088 goto out;
1089 }
1090
1091 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1092 seq_puts(seq, "Interface not active\n");
1093 goto out;
1094 }
1095
1096 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1097 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1098 hard_iface->net_dev->dev_addr,
1099 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1100
1101 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1102
1103out:
16a41423
ML
1104 if (hard_iface)
1105 batadv_hardif_free_ref(hard_iface);
cb1c92ec
SW
1106 return 0;
1107}
1108
56303d34
SE
1109int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1110 int max_if_num)
c6c8fea2 1111{
56303d34 1112 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
d0015fdd 1113 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
5bf74e9c 1114 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 1115 struct hlist_head *head;
56303d34 1116 struct batadv_orig_node *orig_node;
6b5e971a 1117 u32 i;
c90681b8 1118 int ret;
c6c8fea2
SE
1119
1120 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
1121 * if_num
1122 */
c6c8fea2
SE
1123 for (i = 0; i < hash->size; i++) {
1124 head = &hash->table[i];
1125
fb778ea1 1126 rcu_read_lock();
b67bfe0d 1127 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
d0015fdd
AQ
1128 ret = 0;
1129 if (bao->bat_orig_add_if)
1130 ret = bao->bat_orig_add_if(orig_node,
1131 max_if_num);
5346c35e 1132 if (ret == -ENOMEM)
c6c8fea2
SE
1133 goto err;
1134 }
fb778ea1 1135 rcu_read_unlock();
c6c8fea2
SE
1136 }
1137
c6c8fea2
SE
1138 return 0;
1139
1140err:
fb778ea1 1141 rcu_read_unlock();
c6c8fea2
SE
1142 return -ENOMEM;
1143}
1144
56303d34
SE
1145int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1146 int max_if_num)
c6c8fea2 1147{
56303d34 1148 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
5bf74e9c 1149 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 1150 struct hlist_head *head;
56303d34
SE
1151 struct batadv_hard_iface *hard_iface_tmp;
1152 struct batadv_orig_node *orig_node;
d0015fdd 1153 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
6b5e971a 1154 u32 i;
c90681b8 1155 int ret;
c6c8fea2
SE
1156
1157 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
1158 * if_num
1159 */
c6c8fea2
SE
1160 for (i = 0; i < hash->size; i++) {
1161 head = &hash->table[i];
1162
fb778ea1 1163 rcu_read_lock();
b67bfe0d 1164 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
d0015fdd
AQ
1165 ret = 0;
1166 if (bao->bat_orig_del_if)
1167 ret = bao->bat_orig_del_if(orig_node,
1168 max_if_num,
1169 hard_iface->if_num);
5346c35e 1170 if (ret == -ENOMEM)
c6c8fea2
SE
1171 goto err;
1172 }
fb778ea1 1173 rcu_read_unlock();
c6c8fea2
SE
1174 }
1175
1176 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1177 rcu_read_lock();
3193e8fd 1178 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
e9a4f295 1179 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
c6c8fea2
SE
1180 continue;
1181
e6c10f43 1182 if (hard_iface == hard_iface_tmp)
c6c8fea2
SE
1183 continue;
1184
e6c10f43 1185 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
c6c8fea2
SE
1186 continue;
1187
e6c10f43
ML
1188 if (hard_iface_tmp->if_num > hard_iface->if_num)
1189 hard_iface_tmp->if_num--;
c6c8fea2
SE
1190 }
1191 rcu_read_unlock();
1192
e6c10f43 1193 hard_iface->if_num = -1;
c6c8fea2
SE
1194 return 0;
1195
1196err:
fb778ea1 1197 rcu_read_unlock();
c6c8fea2
SE
1198 return -ENOMEM;
1199}