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