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