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