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