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