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