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