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