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