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