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