batman-adv: Avoid recursive call_rcu for batadv_nc_node
[deliverable/linux.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>
d0fa4f3f 29#include <linux/rculist.h>
1e2c2a4f
SE
30#include <linux/seq_file.h>
31#include <linux/slab.h>
32#include <linux/spinlock.h>
33#include <linux/workqueue.h>
34
785ea114 35#include "distributed-arp-table.h"
1e2c2a4f 36#include "fragmentation.h"
c6c8fea2
SE
37#include "gateway_client.h"
38#include "hard-interface.h"
1e2c2a4f 39#include "hash.h"
60432d75 40#include "multicast.h"
1e2c2a4f
SE
41#include "network-coding.h"
42#include "routing.h"
43#include "translation-table.h"
c6c8fea2 44
dec05074
AQ
45/* hash class keys */
46static struct lock_class_key batadv_orig_hash_lock_class_key;
47
03fc7f86 48static void batadv_purge_orig(struct work_struct *work);
c6c8fea2 49
b8e2dd13 50/* returns 1 if they are the same originator */
bbad0a5e 51int batadv_compare_orig(const struct hlist_node *node, const void *data2)
b8e2dd13 52{
56303d34
SE
53 const void *data1 = container_of(node, struct batadv_orig_node,
54 hash_entry);
b8e2dd13 55
323813ed 56 return batadv_compare_eth(data1, data2);
b8e2dd13
SE
57}
58
7ea7b4a1
AQ
59/**
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();
d0fa4f3f 74 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
7ea7b4a1
AQ
75 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
d0fa4f3f 122 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
7ea7b4a1
AQ
123
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
56303d34 141int batadv_originator_init(struct batadv_priv *bat_priv)
c6c8fea2
SE
142{
143 if (bat_priv->orig_hash)
5346c35e 144 return 0;
c6c8fea2 145
1a8eaf07 146 bat_priv->orig_hash = batadv_hash_new(1024);
c6c8fea2
SE
147
148 if (!bat_priv->orig_hash)
149 goto err;
150
dec05074
AQ
151 batadv_hash_set_lock_class(bat_priv->orig_hash,
152 &batadv_orig_hash_lock_class_key);
153
72414442
AQ
154 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
5346c35e 159 return 0;
c6c8fea2
SE
160
161err:
5346c35e 162 return -ENOMEM;
c6c8fea2
SE
163}
164
89652331
SW
165/**
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
cef63419
ML
204/**
205 * batadv_hardif_neigh_free_rcu - free the hardif neigh_node
206 * @rcu: rcu pointer of the neigh_node
207 */
208static void batadv_hardif_neigh_free_rcu(struct rcu_head *rcu)
209{
210 struct batadv_hardif_neigh_node *hardif_neigh;
211
212 hardif_neigh = container_of(rcu, struct batadv_hardif_neigh_node, rcu);
213
cef63419
ML
214 batadv_hardif_free_ref_now(hardif_neigh->if_incoming);
215 kfree(hardif_neigh);
216}
217
218/**
219 * batadv_hardif_neigh_free_now - decrement the hardif neighbors refcounter
220 * and possibly free it (without rcu callback)
221 * @hardif_neigh: hardif neigh neighbor to free
222 */
223static void
224batadv_hardif_neigh_free_now(struct batadv_hardif_neigh_node *hardif_neigh)
225{
bab7c6c3
SE
226 if (atomic_dec_and_test(&hardif_neigh->refcount)) {
227 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
228 hlist_del_init_rcu(&hardif_neigh->list);
229 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
230
cef63419 231 batadv_hardif_neigh_free_rcu(&hardif_neigh->rcu);
bab7c6c3 232 }
cef63419
ML
233}
234
235/**
236 * batadv_hardif_neigh_free_ref - decrement the hardif neighbors refcounter
237 * and possibly free it
238 * @hardif_neigh: hardif neigh neighbor to free
239 */
240void batadv_hardif_neigh_free_ref(struct batadv_hardif_neigh_node *hardif_neigh)
241{
bab7c6c3
SE
242 if (atomic_dec_and_test(&hardif_neigh->refcount)) {
243 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
244 hlist_del_init_rcu(&hardif_neigh->list);
245 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
246
cef63419 247 call_rcu(&hardif_neigh->rcu, batadv_hardif_neigh_free_rcu);
bab7c6c3 248 }
cef63419
ML
249}
250
89652331
SW
251/**
252 * batadv_neigh_node_free_rcu - free the neigh_node
253 * @rcu: rcu pointer of the neigh_node
254 */
255static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
256{
257 struct hlist_node *node_tmp;
258 struct batadv_neigh_node *neigh_node;
cef63419 259 struct batadv_hardif_neigh_node *hardif_neigh;
89652331 260 struct batadv_neigh_ifinfo *neigh_ifinfo;
bcef1f3c 261 struct batadv_algo_ops *bao;
89652331
SW
262
263 neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
bcef1f3c 264 bao = neigh_node->orig_node->bat_priv->bat_algo_ops;
89652331
SW
265
266 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
267 &neigh_node->ifinfo_list, list) {
268 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo);
269 }
bcef1f3c 270
cef63419
ML
271 hardif_neigh = batadv_hardif_neigh_get(neigh_node->if_incoming,
272 neigh_node->addr);
273 if (hardif_neigh) {
274 /* batadv_hardif_neigh_get() increases refcount too */
275 batadv_hardif_neigh_free_now(hardif_neigh);
276 batadv_hardif_neigh_free_now(hardif_neigh);
277 }
278
bcef1f3c
AQ
279 if (bao->bat_neigh_free)
280 bao->bat_neigh_free(neigh_node);
281
89652331
SW
282 batadv_hardif_free_ref_now(neigh_node->if_incoming);
283
284 kfree(neigh_node);
285}
286
287/**
288 * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter
289 * and possibly free it (without rcu callback)
290 * @neigh_node: neigh neighbor to free
291 */
292static void
293batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node)
294{
295 if (atomic_dec_and_test(&neigh_node->refcount))
296 batadv_neigh_node_free_rcu(&neigh_node->rcu);
297}
298
299/**
300 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
301 * and possibly free it
302 * @neigh_node: neigh neighbor to free
303 */
56303d34 304void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
a4c135c5 305{
44524fcd 306 if (atomic_dec_and_test(&neigh_node->refcount))
89652331 307 call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
a4c135c5
SW
308}
309
7351a482
SW
310/**
311 * batadv_orig_node_get_router - router to the originator depending on iface
312 * @orig_node: the orig node for the router
313 * @if_outgoing: the interface where the payload packet has been received or
314 * the OGM should be sent to
315 *
316 * Returns the neighbor which should be router for this orig_node/iface.
317 *
318 * The object is returned with refcounter increased by 1.
319 */
56303d34 320struct batadv_neigh_node *
7351a482
SW
321batadv_orig_router_get(struct batadv_orig_node *orig_node,
322 const struct batadv_hard_iface *if_outgoing)
e1a5382f 323{
7351a482
SW
324 struct batadv_orig_ifinfo *orig_ifinfo;
325 struct batadv_neigh_node *router = NULL;
e1a5382f
LL
326
327 rcu_read_lock();
7351a482
SW
328 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
329 if (orig_ifinfo->if_outgoing != if_outgoing)
330 continue;
331
332 router = rcu_dereference(orig_ifinfo->router);
333 break;
334 }
e1a5382f
LL
335
336 if (router && !atomic_inc_not_zero(&router->refcount))
337 router = NULL;
338
339 rcu_read_unlock();
340 return router;
341}
342
7351a482
SW
343/**
344 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
345 * @orig_node: the orig node to be queried
346 * @if_outgoing: the interface for which the ifinfo should be acquired
347 *
348 * Returns the requested orig_ifinfo or NULL if not found.
349 *
350 * The object is returned with refcounter increased by 1.
351 */
352struct batadv_orig_ifinfo *
353batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
354 struct batadv_hard_iface *if_outgoing)
355{
356 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
357
358 rcu_read_lock();
359 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
360 list) {
361 if (tmp->if_outgoing != if_outgoing)
362 continue;
363
364 if (!atomic_inc_not_zero(&tmp->refcount))
365 continue;
366
367 orig_ifinfo = tmp;
368 break;
369 }
370 rcu_read_unlock();
371
372 return orig_ifinfo;
373}
374
375/**
376 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
377 * @orig_node: the orig node to be queried
378 * @if_outgoing: the interface for which the ifinfo should be acquired
379 *
380 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
381 * interface otherwise. The object is created and added to the list
382 * if it does not exist.
383 *
384 * The object is returned with refcounter increased by 1.
385 */
386struct batadv_orig_ifinfo *
387batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
388 struct batadv_hard_iface *if_outgoing)
389{
390 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
391 unsigned long reset_time;
392
393 spin_lock_bh(&orig_node->neigh_list_lock);
394
395 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
396 if (orig_ifinfo)
397 goto out;
398
399 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
400 if (!orig_ifinfo)
401 goto out;
402
403 if (if_outgoing != BATADV_IF_DEFAULT &&
404 !atomic_inc_not_zero(&if_outgoing->refcount)) {
405 kfree(orig_ifinfo);
406 orig_ifinfo = NULL;
407 goto out;
408 }
409
410 reset_time = jiffies - 1;
411 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
412 orig_ifinfo->batman_seqno_reset = reset_time;
413 orig_ifinfo->if_outgoing = if_outgoing;
414 INIT_HLIST_NODE(&orig_ifinfo->list);
415 atomic_set(&orig_ifinfo->refcount, 2);
416 hlist_add_head_rcu(&orig_ifinfo->list,
417 &orig_node->ifinfo_list);
418out:
419 spin_unlock_bh(&orig_node->neigh_list_lock);
420 return orig_ifinfo;
421}
422
89652331
SW
423/**
424 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
425 * @neigh_node: the neigh node to be queried
426 * @if_outgoing: the interface for which the ifinfo should be acquired
427 *
428 * The object is returned with refcounter increased by 1.
429 *
430 * Returns the requested neigh_ifinfo or NULL if not found
431 */
432struct batadv_neigh_ifinfo *
433batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
434 struct batadv_hard_iface *if_outgoing)
435{
436 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
437 *tmp_neigh_ifinfo;
438
439 rcu_read_lock();
440 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
441 list) {
442 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
443 continue;
444
445 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
446 continue;
447
448 neigh_ifinfo = tmp_neigh_ifinfo;
449 break;
450 }
451 rcu_read_unlock();
452
453 return neigh_ifinfo;
454}
455
456/**
457 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
458 * @neigh_node: the neigh node to be queried
459 * @if_outgoing: the interface for which the ifinfo should be acquired
460 *
461 * Returns NULL in case of failure or the neigh_ifinfo object for the
462 * if_outgoing interface otherwise. The object is created and added to the list
463 * if it does not exist.
464 *
465 * The object is returned with refcounter increased by 1.
466 */
467struct batadv_neigh_ifinfo *
468batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
469 struct batadv_hard_iface *if_outgoing)
470{
471 struct batadv_neigh_ifinfo *neigh_ifinfo;
472
473 spin_lock_bh(&neigh->ifinfo_lock);
474
475 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
476 if (neigh_ifinfo)
477 goto out;
478
479 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
480 if (!neigh_ifinfo)
481 goto out;
482
483 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
484 kfree(neigh_ifinfo);
485 neigh_ifinfo = NULL;
486 goto out;
487 }
488
489 INIT_HLIST_NODE(&neigh_ifinfo->list);
490 atomic_set(&neigh_ifinfo->refcount, 2);
491 neigh_ifinfo->if_outgoing = if_outgoing;
492
493 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
494
495out:
496 spin_unlock_bh(&neigh->ifinfo_lock);
497
498 return neigh_ifinfo;
499}
500
ed292663
ML
501/**
502 * batadv_neigh_node_get - retrieve a neighbour from the list
503 * @orig_node: originator which the neighbour belongs to
504 * @hard_iface: the interface where this neighbour is connected to
505 * @addr: the address of the neighbour
506 *
507 * Looks for and possibly returns a neighbour belonging to this originator list
508 * which is connected through the provided hard interface.
509 * Returns NULL if the neighbour is not found.
510 */
511static struct batadv_neigh_node *
512batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
513 const struct batadv_hard_iface *hard_iface,
514 const u8 *addr)
515{
516 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
517
518 rcu_read_lock();
519 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
520 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
521 continue;
522
523 if (tmp_neigh_node->if_incoming != hard_iface)
524 continue;
525
526 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
527 continue;
528
529 res = tmp_neigh_node;
530 break;
531 }
532 rcu_read_unlock();
533
534 return res;
535}
536
cef63419
ML
537/**
538 * batadv_hardif_neigh_create - create a hardif neighbour node
539 * @hard_iface: the interface this neighbour is connected to
540 * @neigh_addr: the interface address of the neighbour to retrieve
541 *
542 * Returns the hardif neighbour node if found or created or NULL otherwise.
543 */
544static struct batadv_hardif_neigh_node *
545batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
546 const u8 *neigh_addr)
547{
8248a4c7 548 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
cef63419
ML
549 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
550
551 spin_lock_bh(&hard_iface->neigh_list_lock);
552
553 /* check if neighbor hasn't been added in the meantime */
554 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
555 if (hardif_neigh)
556 goto out;
557
558 if (!atomic_inc_not_zero(&hard_iface->refcount))
559 goto out;
560
561 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
562 if (!hardif_neigh) {
563 batadv_hardif_free_ref(hard_iface);
564 goto out;
565 }
566
567 INIT_HLIST_NODE(&hardif_neigh->list);
568 ether_addr_copy(hardif_neigh->addr, neigh_addr);
569 hardif_neigh->if_incoming = hard_iface;
570 hardif_neigh->last_seen = jiffies;
571
572 atomic_set(&hardif_neigh->refcount, 1);
573
8248a4c7
ML
574 if (bat_priv->bat_algo_ops->bat_hardif_neigh_init)
575 bat_priv->bat_algo_ops->bat_hardif_neigh_init(hardif_neigh);
576
cef63419
ML
577 hlist_add_head(&hardif_neigh->list, &hard_iface->neigh_list);
578
579out:
580 spin_unlock_bh(&hard_iface->neigh_list_lock);
581 return hardif_neigh;
582}
583
584/**
585 * batadv_hardif_neigh_get_or_create - retrieve or create a hardif neighbour
586 * node
587 * @hard_iface: the interface this neighbour is connected to
588 * @neigh_addr: the interface address of the neighbour to retrieve
589 *
590 * Returns the hardif neighbour node if found or created or NULL otherwise.
591 */
592static struct batadv_hardif_neigh_node *
593batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
594 const u8 *neigh_addr)
595{
596 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
597
598 /* first check without locking to avoid the overhead */
599 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
600 if (hardif_neigh)
601 return hardif_neigh;
602
603 return batadv_hardif_neigh_create(hard_iface, neigh_addr);
604}
605
606/**
607 * batadv_hardif_neigh_get - retrieve a hardif neighbour from the list
608 * @hard_iface: the interface where this neighbour is connected to
609 * @neigh_addr: the address of the neighbour
610 *
611 * Looks for and possibly returns a neighbour belonging to this hard interface.
612 * Returns NULL if the neighbour is not found.
613 */
614struct batadv_hardif_neigh_node *
615batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
616 const u8 *neigh_addr)
617{
618 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
619
620 rcu_read_lock();
621 hlist_for_each_entry_rcu(tmp_hardif_neigh,
622 &hard_iface->neigh_list, list) {
623 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
624 continue;
625
626 if (!atomic_inc_not_zero(&tmp_hardif_neigh->refcount))
627 continue;
628
629 hardif_neigh = tmp_hardif_neigh;
630 break;
631 }
632 rcu_read_unlock();
633
634 return hardif_neigh;
635}
636
0538f759
AQ
637/**
638 * batadv_neigh_node_new - create and init a new neigh_node object
3f32f8a6 639 * @orig_node: originator object representing the neighbour
0538f759
AQ
640 * @hard_iface: the interface where the neighbour is connected to
641 * @neigh_addr: the mac address of the neighbour interface
0538f759
AQ
642 *
643 * Allocates a new neigh_node object and initialises all the generic fields.
644 * Returns the new object or NULL on failure.
645 */
56303d34 646struct batadv_neigh_node *
3f32f8a6
ML
647batadv_neigh_node_new(struct batadv_orig_node *orig_node,
648 struct batadv_hard_iface *hard_iface,
649 const u8 *neigh_addr)
c6c8fea2 650{
56303d34 651 struct batadv_neigh_node *neigh_node;
cef63419 652 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
c6c8fea2 653
741aa06b
ML
654 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
655 if (neigh_node)
656 goto out;
657
cef63419
ML
658 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
659 neigh_addr);
660 if (!hardif_neigh)
661 goto out;
662
704509b8 663 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
c6c8fea2 664 if (!neigh_node)
7ae8b285 665 goto out;
c6c8fea2 666
f729dc70
ML
667 if (!atomic_inc_not_zero(&hard_iface->refcount)) {
668 kfree(neigh_node);
669 neigh_node = NULL;
670 goto out;
671 }
672
9591a79f 673 INIT_HLIST_NODE(&neigh_node->list);
89652331
SW
674 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
675 spin_lock_init(&neigh_node->ifinfo_lock);
c6c8fea2 676
8fdd0153 677 ether_addr_copy(neigh_node->addr, neigh_addr);
0538f759
AQ
678 neigh_node->if_incoming = hard_iface;
679 neigh_node->orig_node = orig_node;
680
1605d0d6
ML
681 /* extra reference for return */
682 atomic_set(&neigh_node->refcount, 2);
c6c8fea2 683
741aa06b
ML
684 spin_lock_bh(&orig_node->neigh_list_lock);
685 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
686 spin_unlock_bh(&orig_node->neigh_list_lock);
687
cef63419
ML
688 /* increment unique neighbor refcount */
689 atomic_inc(&hardif_neigh->refcount);
690
741aa06b
ML
691 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
692 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
693 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
694
7ae8b285 695out:
cef63419
ML
696 if (hardif_neigh)
697 batadv_hardif_neigh_free_ref(hardif_neigh);
c6c8fea2
SE
698 return neigh_node;
699}
700
7587405a
ML
701/**
702 * batadv_hardif_neigh_seq_print_text - print the single hop neighbour list
703 * @seq: neighbour table seq_file struct
704 * @offset: not used
705 *
706 * Always returns 0.
707 */
708int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
709{
710 struct net_device *net_dev = (struct net_device *)seq->private;
711 struct batadv_priv *bat_priv = netdev_priv(net_dev);
712 struct batadv_hard_iface *primary_if;
713
714 primary_if = batadv_seq_print_text_primary_if_get(seq);
715 if (!primary_if)
716 return 0;
717
718 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
719 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
720 primary_if->net_dev->dev_addr, net_dev->name,
721 bat_priv->bat_algo_ops->name);
722
723 batadv_hardif_free_ref(primary_if);
724
725 if (!bat_priv->bat_algo_ops->bat_neigh_print) {
726 seq_puts(seq,
727 "No printing function for this routing protocol\n");
728 return 0;
729 }
730
731 bat_priv->bat_algo_ops->bat_neigh_print(bat_priv, seq);
732 return 0;
733}
734
7351a482
SW
735/**
736 * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
737 * @rcu: rcu pointer of the orig_ifinfo object
738 */
739static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu)
740{
741 struct batadv_orig_ifinfo *orig_ifinfo;
000c8dff 742 struct batadv_neigh_node *router;
7351a482
SW
743
744 orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu);
745
746 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
747 batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing);
748
000c8dff
SW
749 /* this is the last reference to this object */
750 router = rcu_dereference_protected(orig_ifinfo->router, true);
751 if (router)
752 batadv_neigh_node_free_ref_now(router);
7351a482
SW
753 kfree(orig_ifinfo);
754}
755
756/**
757 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
758 * the orig_ifinfo (without rcu callback)
759 * @orig_ifinfo: the orig_ifinfo object to release
760 */
761static void
762batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo)
763{
764 if (atomic_dec_and_test(&orig_ifinfo->refcount))
765 batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu);
766}
767
768/**
769 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
770 * the orig_ifinfo
771 * @orig_ifinfo: the orig_ifinfo object to release
772 */
773void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
774{
775 if (atomic_dec_and_test(&orig_ifinfo->refcount))
776 call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu);
777}
778
03fc7f86 779static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
c6c8fea2 780{
b67bfe0d 781 struct hlist_node *node_tmp;
f6c8b711 782 struct batadv_neigh_node *neigh_node;
56303d34 783 struct batadv_orig_node *orig_node;
7351a482 784 struct batadv_orig_ifinfo *orig_ifinfo;
16b1aba8 785
56303d34 786 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
c6c8fea2 787
f987ed6e
ML
788 spin_lock_bh(&orig_node->neigh_list_lock);
789
c6c8fea2 790 /* for all neighbors towards this originator ... */
b67bfe0d 791 hlist_for_each_entry_safe(neigh_node, node_tmp,
9591a79f 792 &orig_node->neigh_list, list) {
f987ed6e 793 hlist_del_rcu(&neigh_node->list);
89652331 794 batadv_neigh_node_free_ref_now(neigh_node);
c6c8fea2
SE
795 }
796
7351a482
SW
797 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
798 &orig_node->ifinfo_list, list) {
799 hlist_del_rcu(&orig_ifinfo->list);
800 batadv_orig_ifinfo_free_ref_now(orig_ifinfo);
801 }
f987ed6e
ML
802 spin_unlock_bh(&orig_node->neigh_list_lock);
803
60432d75
LL
804 batadv_mcast_purge_orig(orig_node);
805
d56b1705
MH
806 /* Free nc_nodes */
807 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
808
610bfc6b
MH
809 batadv_frag_purge_orig(orig_node, NULL);
810
d0015fdd
AQ
811 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
812 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
813
a73105b8 814 kfree(orig_node->tt_buff);
c6c8fea2
SE
815 kfree(orig_node);
816}
817
72822225
LL
818/**
819 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
820 * schedule an rcu callback for freeing it
821 * @orig_node: the orig node to free
822 */
56303d34 823void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
7b36e8ee
ML
824{
825 if (atomic_dec_and_test(&orig_node->refcount))
03fc7f86 826 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
7b36e8ee
ML
827}
828
72822225
LL
829/**
830 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
831 * possibly free it (without rcu callback)
832 * @orig_node: the orig node to free
833 */
834void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
835{
836 if (atomic_dec_and_test(&orig_node->refcount))
837 batadv_orig_node_free_rcu(&orig_node->rcu);
838}
839
56303d34 840void batadv_originator_free(struct batadv_priv *bat_priv)
c6c8fea2 841{
5bf74e9c 842 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 843 struct hlist_node *node_tmp;
16b1aba8 844 struct hlist_head *head;
16b1aba8 845 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 846 struct batadv_orig_node *orig_node;
6b5e971a 847 u32 i;
16b1aba8
ML
848
849 if (!hash)
c6c8fea2
SE
850 return;
851
852 cancel_delayed_work_sync(&bat_priv->orig_work);
853
c6c8fea2 854 bat_priv->orig_hash = NULL;
16b1aba8
ML
855
856 for (i = 0; i < hash->size; i++) {
857 head = &hash->table[i];
858 list_lock = &hash->list_locks[i];
859
860 spin_lock_bh(list_lock);
b67bfe0d 861 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 862 head, hash_entry) {
b67bfe0d 863 hlist_del_rcu(&orig_node->hash_entry);
7d211efc 864 batadv_orig_node_free_ref(orig_node);
16b1aba8
ML
865 }
866 spin_unlock_bh(list_lock);
867 }
868
1a8eaf07 869 batadv_hash_destroy(hash);
c6c8fea2
SE
870}
871
bbad0a5e
AQ
872/**
873 * batadv_orig_node_new - creates a new orig_node
874 * @bat_priv: the bat priv with all the soft interface information
875 * @addr: the mac address of the originator
876 *
877 * Creates a new originator object and initialise all the generic fields.
878 * The new object is not added to the originator list.
879 * Returns the newly created object or NULL on failure.
9cfc7bd6 880 */
bbad0a5e 881struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
6b5e971a 882 const u8 *addr)
c6c8fea2 883{
56303d34 884 struct batadv_orig_node *orig_node;
7ea7b4a1 885 struct batadv_orig_node_vlan *vlan;
42d0b044 886 unsigned long reset_time;
bbad0a5e 887 int i;
c6c8fea2 888
39c75a51
SE
889 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
890 "Creating new originator: %pM\n", addr);
c6c8fea2 891
704509b8 892 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
c6c8fea2
SE
893 if (!orig_node)
894 return NULL;
895
9591a79f 896 INIT_HLIST_HEAD(&orig_node->neigh_list);
d0fa4f3f 897 INIT_HLIST_HEAD(&orig_node->vlan_list);
7351a482 898 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
f3e0008f 899 spin_lock_init(&orig_node->bcast_seqno_lock);
f987ed6e 900 spin_lock_init(&orig_node->neigh_list_lock);
a73105b8 901 spin_lock_init(&orig_node->tt_buff_lock);
a70a9aa9 902 spin_lock_init(&orig_node->tt_lock);
7ea7b4a1 903 spin_lock_init(&orig_node->vlan_list_lock);
7b36e8ee 904
d56b1705
MH
905 batadv_nc_init_orig(orig_node);
906
7b36e8ee
ML
907 /* extra reference for return */
908 atomic_set(&orig_node->refcount, 2);
c6c8fea2 909
16b1aba8 910 orig_node->bat_priv = bat_priv;
8fdd0153 911 ether_addr_copy(orig_node->orig, addr);
785ea114 912 batadv_dat_init_orig_node_addr(orig_node);
c8c991bf 913 atomic_set(&orig_node->last_ttvn, 0);
2dafb49d 914 orig_node->tt_buff = NULL;
a73105b8 915 orig_node->tt_buff_len = 0;
2c667a33 916 orig_node->last_seen = jiffies;
42d0b044
SE
917 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
918 orig_node->bcast_seqno_reset = reset_time;
8a4023c5 919
60432d75
LL
920#ifdef CONFIG_BATMAN_ADV_MCAST
921 orig_node->mcast_flags = BATADV_NO_FLAGS;
8a4023c5
LL
922 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
923 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
924 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
925 spin_lock_init(&orig_node->mcast_handler_lock);
60432d75 926#endif
c6c8fea2 927
7ea7b4a1
AQ
928 /* create a vlan object for the "untagged" LAN */
929 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
930 if (!vlan)
931 goto free_orig_node;
932 /* batadv_orig_node_vlan_new() increases the refcounter.
933 * Immediately release vlan since it is not needed anymore in this
934 * context
935 */
936 batadv_orig_node_vlan_free_ref(vlan);
937
610bfc6b
MH
938 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
939 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
940 spin_lock_init(&orig_node->fragments[i].lock);
941 orig_node->fragments[i].size = 0;
942 }
943
c6c8fea2 944 return orig_node;
c6c8fea2
SE
945free_orig_node:
946 kfree(orig_node);
947 return NULL;
948}
949
709de13f
SW
950/**
951 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
952 * @bat_priv: the bat priv with all the soft interface information
953 * @neigh: orig node which is to be checked
954 */
955static void
956batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
957 struct batadv_neigh_node *neigh)
958{
959 struct batadv_neigh_ifinfo *neigh_ifinfo;
960 struct batadv_hard_iface *if_outgoing;
961 struct hlist_node *node_tmp;
962
963 spin_lock_bh(&neigh->ifinfo_lock);
964
965 /* for all ifinfo objects for this neighinator */
966 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
967 &neigh->ifinfo_list, list) {
968 if_outgoing = neigh_ifinfo->if_outgoing;
969
970 /* always keep the default interface */
971 if (if_outgoing == BATADV_IF_DEFAULT)
972 continue;
973
974 /* don't purge if the interface is not (going) down */
975 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
976 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
977 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
978 continue;
979
980 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
981 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
982 neigh->addr, if_outgoing->net_dev->name);
983
984 hlist_del_rcu(&neigh_ifinfo->list);
985 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
986 }
987
988 spin_unlock_bh(&neigh->ifinfo_lock);
989}
990
7351a482
SW
991/**
992 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
993 * @bat_priv: the bat priv with all the soft interface information
994 * @orig_node: orig node which is to be checked
995 *
996 * Returns true if any ifinfo entry was purged, false otherwise.
997 */
998static bool
999batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1000 struct batadv_orig_node *orig_node)
1001{
1002 struct batadv_orig_ifinfo *orig_ifinfo;
1003 struct batadv_hard_iface *if_outgoing;
1004 struct hlist_node *node_tmp;
1005 bool ifinfo_purged = false;
1006
1007 spin_lock_bh(&orig_node->neigh_list_lock);
1008
1009 /* for all ifinfo objects for this originator */
1010 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1011 &orig_node->ifinfo_list, list) {
1012 if_outgoing = orig_ifinfo->if_outgoing;
1013
1014 /* always keep the default interface */
1015 if (if_outgoing == BATADV_IF_DEFAULT)
1016 continue;
1017
1018 /* don't purge if the interface is not (going) down */
1019 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
1020 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
1021 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
1022 continue;
1023
1024 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1025 "router/ifinfo purge: originator %pM, iface: %s\n",
1026 orig_node->orig, if_outgoing->net_dev->name);
1027
1028 ifinfo_purged = true;
1029
1030 hlist_del_rcu(&orig_ifinfo->list);
1031 batadv_orig_ifinfo_free_ref(orig_ifinfo);
f3b3d901
SW
1032 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1033 orig_node->last_bonding_candidate = NULL;
1034 batadv_orig_ifinfo_free_ref(orig_ifinfo);
1035 }
7351a482
SW
1036 }
1037
1038 spin_unlock_bh(&orig_node->neigh_list_lock);
1039
1040 return ifinfo_purged;
1041}
1042
89652331
SW
1043/**
1044 * batadv_purge_orig_neighbors - purges neighbors from originator
1045 * @bat_priv: the bat priv with all the soft interface information
1046 * @orig_node: orig node which is to be checked
1047 *
1048 * Returns true if any neighbor was purged, false otherwise
1049 */
56303d34
SE
1050static bool
1051batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
89652331 1052 struct batadv_orig_node *orig_node)
c6c8fea2 1053{
b67bfe0d 1054 struct hlist_node *node_tmp;
56303d34 1055 struct batadv_neigh_node *neigh_node;
c6c8fea2 1056 bool neigh_purged = false;
0b0094e0 1057 unsigned long last_seen;
56303d34 1058 struct batadv_hard_iface *if_incoming;
c6c8fea2 1059
f987ed6e
ML
1060 spin_lock_bh(&orig_node->neigh_list_lock);
1061
c6c8fea2 1062 /* for all neighbors towards this originator ... */
b67bfe0d 1063 hlist_for_each_entry_safe(neigh_node, node_tmp,
9591a79f 1064 &orig_node->neigh_list, list) {
1eda58bf
SE
1065 last_seen = neigh_node->last_seen;
1066 if_incoming = neigh_node->if_incoming;
1067
42d0b044 1068 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
e9a4f295
SE
1069 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
1070 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1071 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
e9a4f295
SE
1072 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
1073 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1074 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
39c75a51 1075 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1076 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1077 orig_node->orig, neigh_node->addr,
1078 if_incoming->net_dev->name);
c6c8fea2 1079 else
39c75a51 1080 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1081 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1082 orig_node->orig, neigh_node->addr,
1083 jiffies_to_msecs(last_seen));
c6c8fea2
SE
1084
1085 neigh_purged = true;
9591a79f 1086
f987ed6e 1087 hlist_del_rcu(&neigh_node->list);
7d211efc 1088 batadv_neigh_node_free_ref(neigh_node);
709de13f
SW
1089 } else {
1090 /* only necessary if not the whole neighbor is to be
1091 * deleted, but some interface has been removed.
1092 */
1093 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
c6c8fea2
SE
1094 }
1095 }
f987ed6e
ML
1096
1097 spin_unlock_bh(&orig_node->neigh_list_lock);
c6c8fea2
SE
1098 return neigh_purged;
1099}
1100
89652331
SW
1101/**
1102 * batadv_find_best_neighbor - finds the best neighbor after purging
1103 * @bat_priv: the bat priv with all the soft interface information
1104 * @orig_node: orig node which is to be checked
1105 * @if_outgoing: the interface for which the metric should be compared
1106 *
1107 * Returns the current best neighbor, with refcount increased.
1108 */
1109static struct batadv_neigh_node *
1110batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1111 struct batadv_orig_node *orig_node,
1112 struct batadv_hard_iface *if_outgoing)
1113{
1114 struct batadv_neigh_node *best = NULL, *neigh;
1115 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1116
1117 rcu_read_lock();
1118 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1119 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
1120 best, if_outgoing) <= 0))
1121 continue;
1122
1123 if (!atomic_inc_not_zero(&neigh->refcount))
1124 continue;
1125
1126 if (best)
1127 batadv_neigh_node_free_ref(best);
1128
1129 best = neigh;
1130 }
1131 rcu_read_unlock();
1132
1133 return best;
1134}
1135
7351a482
SW
1136/**
1137 * batadv_purge_orig_node - purges obsolete information from an orig_node
1138 * @bat_priv: the bat priv with all the soft interface information
1139 * @orig_node: orig node which is to be checked
1140 *
1141 * This function checks if the orig_node or substructures of it have become
1142 * obsolete, and purges this information if that's the case.
1143 *
1144 * Returns true if the orig_node is to be removed, false otherwise.
1145 */
56303d34
SE
1146static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1147 struct batadv_orig_node *orig_node)
c6c8fea2 1148{
56303d34 1149 struct batadv_neigh_node *best_neigh_node;
7351a482 1150 struct batadv_hard_iface *hard_iface;
7b955a9f 1151 bool changed_ifinfo, changed_neigh;
c6c8fea2 1152
42d0b044
SE
1153 if (batadv_has_timed_out(orig_node->last_seen,
1154 2 * BATADV_PURGE_TIMEOUT)) {
39c75a51 1155 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
1156 "Originator timeout: originator %pM, last_seen %u\n",
1157 orig_node->orig,
1158 jiffies_to_msecs(orig_node->last_seen));
c6c8fea2 1159 return true;
c6c8fea2 1160 }
7b955a9f
SW
1161 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1162 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
7351a482 1163
7b955a9f 1164 if (!changed_ifinfo && !changed_neigh)
89652331
SW
1165 return false;
1166
7351a482 1167 /* first for NULL ... */
89652331
SW
1168 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1169 BATADV_IF_DEFAULT);
7351a482
SW
1170 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1171 best_neigh_node);
89652331
SW
1172 if (best_neigh_node)
1173 batadv_neigh_node_free_ref(best_neigh_node);
c6c8fea2 1174
7351a482
SW
1175 /* ... then for all other interfaces. */
1176 rcu_read_lock();
1177 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1178 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1179 continue;
1180
1181 if (hard_iface->soft_iface != bat_priv->soft_iface)
1182 continue;
1183
1184 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1185 orig_node,
1186 hard_iface);
1187 batadv_update_route(bat_priv, orig_node, hard_iface,
1188 best_neigh_node);
1189 if (best_neigh_node)
1190 batadv_neigh_node_free_ref(best_neigh_node);
1191 }
1192 rcu_read_unlock();
1193
c6c8fea2
SE
1194 return false;
1195}
1196
56303d34 1197static void _batadv_purge_orig(struct batadv_priv *bat_priv)
c6c8fea2 1198{
5bf74e9c 1199 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 1200 struct hlist_node *node_tmp;
c6c8fea2 1201 struct hlist_head *head;
fb778ea1 1202 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 1203 struct batadv_orig_node *orig_node;
6b5e971a 1204 u32 i;
c6c8fea2
SE
1205
1206 if (!hash)
1207 return;
1208
c6c8fea2
SE
1209 /* for all origins... */
1210 for (i = 0; i < hash->size; i++) {
1211 head = &hash->table[i];
fb778ea1 1212 list_lock = &hash->list_locks[i];
c6c8fea2 1213
fb778ea1 1214 spin_lock_bh(list_lock);
b67bfe0d 1215 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 1216 head, hash_entry) {
03fc7f86 1217 if (batadv_purge_orig_node(bat_priv, orig_node)) {
414254e3 1218 batadv_gw_node_delete(bat_priv, orig_node);
b67bfe0d 1219 hlist_del_rcu(&orig_node->hash_entry);
9d31b3ce
LL
1220 batadv_tt_global_del_orig(orig_node->bat_priv,
1221 orig_node, -1,
1222 "originator timed out");
7d211efc 1223 batadv_orig_node_free_ref(orig_node);
fb778ea1 1224 continue;
c6c8fea2 1225 }
610bfc6b
MH
1226
1227 batadv_frag_purge_orig(orig_node,
1228 batadv_frag_check_entry);
c6c8fea2 1229 }
fb778ea1 1230 spin_unlock_bh(list_lock);
c6c8fea2
SE
1231 }
1232
7cf06bc6 1233 batadv_gw_election(bat_priv);
c6c8fea2
SE
1234}
1235
03fc7f86 1236static void batadv_purge_orig(struct work_struct *work)
c6c8fea2 1237{
56303d34
SE
1238 struct delayed_work *delayed_work;
1239 struct batadv_priv *bat_priv;
c6c8fea2 1240
56303d34
SE
1241 delayed_work = container_of(work, struct delayed_work, work);
1242 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
03fc7f86 1243 _batadv_purge_orig(bat_priv);
72414442
AQ
1244 queue_delayed_work(batadv_event_workqueue,
1245 &bat_priv->orig_work,
1246 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
c6c8fea2
SE
1247}
1248
56303d34 1249void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
c6c8fea2 1250{
03fc7f86 1251 _batadv_purge_orig(bat_priv);
c6c8fea2
SE
1252}
1253
7d211efc 1254int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
1255{
1256 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 1257 struct batadv_priv *bat_priv = netdev_priv(net_dev);
56303d34 1258 struct batadv_hard_iface *primary_if;
32ae9b22 1259
30da63a6
ML
1260 primary_if = batadv_seq_print_text_primary_if_get(seq);
1261 if (!primary_if)
737a2a22 1262 return 0;
c6c8fea2 1263
737a2a22 1264 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
42d0b044 1265 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
737a2a22
AQ
1266 primary_if->net_dev->dev_addr, net_dev->name,
1267 bat_priv->bat_algo_ops->name);
c6c8fea2 1268
737a2a22 1269 batadv_hardif_free_ref(primary_if);
c6c8fea2 1270
737a2a22
AQ
1271 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1272 seq_puts(seq,
1273 "No printing function for this routing protocol\n");
1274 return 0;
c6c8fea2
SE
1275 }
1276
cb1c92ec
SW
1277 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1278 BATADV_IF_DEFAULT);
c6c8fea2 1279
30da63a6 1280 return 0;
c6c8fea2
SE
1281}
1282
cb1c92ec
SW
1283/**
1284 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1285 * outgoing interface
1286 * @seq: debugfs table seq_file struct
1287 * @offset: not used
1288 *
1289 * Returns 0
1290 */
1291int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1292{
1293 struct net_device *net_dev = (struct net_device *)seq->private;
1294 struct batadv_hard_iface *hard_iface;
1295 struct batadv_priv *bat_priv;
1296
1297 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1298
1299 if (!hard_iface || !hard_iface->soft_iface) {
1300 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1301 goto out;
1302 }
1303
1304 bat_priv = netdev_priv(hard_iface->soft_iface);
1305 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1306 seq_puts(seq,
1307 "No printing function for this routing protocol\n");
1308 goto out;
1309 }
1310
1311 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1312 seq_puts(seq, "Interface not active\n");
1313 goto out;
1314 }
1315
1316 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1317 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1318 hard_iface->net_dev->dev_addr,
1319 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1320
1321 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1322
1323out:
16a41423
ML
1324 if (hard_iface)
1325 batadv_hardif_free_ref(hard_iface);
cb1c92ec
SW
1326 return 0;
1327}
1328
56303d34
SE
1329int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1330 int max_if_num)
c6c8fea2 1331{
56303d34 1332 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
d0015fdd 1333 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
5bf74e9c 1334 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 1335 struct hlist_head *head;
56303d34 1336 struct batadv_orig_node *orig_node;
6b5e971a 1337 u32 i;
c90681b8 1338 int ret;
c6c8fea2
SE
1339
1340 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
1341 * if_num
1342 */
c6c8fea2
SE
1343 for (i = 0; i < hash->size; i++) {
1344 head = &hash->table[i];
1345
fb778ea1 1346 rcu_read_lock();
b67bfe0d 1347 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
d0015fdd
AQ
1348 ret = 0;
1349 if (bao->bat_orig_add_if)
1350 ret = bao->bat_orig_add_if(orig_node,
1351 max_if_num);
5346c35e 1352 if (ret == -ENOMEM)
c6c8fea2
SE
1353 goto err;
1354 }
fb778ea1 1355 rcu_read_unlock();
c6c8fea2
SE
1356 }
1357
c6c8fea2
SE
1358 return 0;
1359
1360err:
fb778ea1 1361 rcu_read_unlock();
c6c8fea2
SE
1362 return -ENOMEM;
1363}
1364
56303d34
SE
1365int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1366 int max_if_num)
c6c8fea2 1367{
56303d34 1368 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
5bf74e9c 1369 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 1370 struct hlist_head *head;
56303d34
SE
1371 struct batadv_hard_iface *hard_iface_tmp;
1372 struct batadv_orig_node *orig_node;
d0015fdd 1373 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
6b5e971a 1374 u32 i;
c90681b8 1375 int ret;
c6c8fea2
SE
1376
1377 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
1378 * if_num
1379 */
c6c8fea2
SE
1380 for (i = 0; i < hash->size; i++) {
1381 head = &hash->table[i];
1382
fb778ea1 1383 rcu_read_lock();
b67bfe0d 1384 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
d0015fdd
AQ
1385 ret = 0;
1386 if (bao->bat_orig_del_if)
1387 ret = bao->bat_orig_del_if(orig_node,
1388 max_if_num,
1389 hard_iface->if_num);
5346c35e 1390 if (ret == -ENOMEM)
c6c8fea2
SE
1391 goto err;
1392 }
fb778ea1 1393 rcu_read_unlock();
c6c8fea2
SE
1394 }
1395
1396 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1397 rcu_read_lock();
3193e8fd 1398 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
e9a4f295 1399 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
c6c8fea2
SE
1400 continue;
1401
e6c10f43 1402 if (hard_iface == hard_iface_tmp)
c6c8fea2
SE
1403 continue;
1404
e6c10f43 1405 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
c6c8fea2
SE
1406 continue;
1407
e6c10f43
ML
1408 if (hard_iface_tmp->if_num > hard_iface->if_num)
1409 hard_iface_tmp->if_num--;
c6c8fea2
SE
1410 }
1411 rcu_read_unlock();
1412
e6c10f43 1413 hard_iface->if_num = -1;
c6c8fea2
SE
1414 return 0;
1415
1416err:
fb778ea1 1417 rcu_read_unlock();
c6c8fea2
SE
1418 return -ENOMEM;
1419}
This page took 0.51014 seconds and 5 git commands to generate.