Linux 4.1.16
[linux/fpc-iii.git] / net / batman-adv / originator.c
blobdfae97408628dbc4b1b8bef8812b1fd1d73f1c37
1 /* Copyright (C) 2009-2014 B.A.T.M.A.N. contributors:
3 * Marek Lindner, Simon Wunderlich
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.
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.
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/>.
18 #include "main.h"
19 #include "distributed-arp-table.h"
20 #include "originator.h"
21 #include "hash.h"
22 #include "translation-table.h"
23 #include "routing.h"
24 #include "gateway_client.h"
25 #include "hard-interface.h"
26 #include "soft-interface.h"
27 #include "bridge_loop_avoidance.h"
28 #include "network-coding.h"
29 #include "fragmentation.h"
30 #include "multicast.h"
32 /* hash class keys */
33 static struct lock_class_key batadv_orig_hash_lock_class_key;
35 static void batadv_purge_orig(struct work_struct *work);
37 /* returns 1 if they are the same originator */
38 int batadv_compare_orig(const struct hlist_node *node, const void *data2)
40 const void *data1 = container_of(node, struct batadv_orig_node,
41 hash_entry);
43 return batadv_compare_eth(data1, data2);
46 /**
47 * batadv_orig_node_vlan_get - get an orig_node_vlan object
48 * @orig_node: the originator serving the VLAN
49 * @vid: the VLAN identifier
51 * Returns the vlan object identified by vid and belonging to orig_node or NULL
52 * if it does not exist.
54 struct batadv_orig_node_vlan *
55 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
56 unsigned short vid)
58 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
60 rcu_read_lock();
61 list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
62 if (tmp->vid != vid)
63 continue;
65 if (!atomic_inc_not_zero(&tmp->refcount))
66 continue;
68 vlan = tmp;
70 break;
72 rcu_read_unlock();
74 return vlan;
77 /**
78 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
79 * object
80 * @orig_node: the originator serving the VLAN
81 * @vid: the VLAN identifier
83 * Returns NULL in case of failure or the vlan object identified by vid and
84 * belonging to orig_node otherwise. The object is created and added to the list
85 * if it does not exist.
87 * The object is returned with refcounter increased by 1.
89 struct batadv_orig_node_vlan *
90 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
91 unsigned short vid)
93 struct batadv_orig_node_vlan *vlan;
95 spin_lock_bh(&orig_node->vlan_list_lock);
97 /* first look if an object for this vid already exists */
98 vlan = batadv_orig_node_vlan_get(orig_node, vid);
99 if (vlan)
100 goto out;
102 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
103 if (!vlan)
104 goto out;
106 atomic_set(&vlan->refcount, 2);
107 vlan->vid = vid;
109 list_add_rcu(&vlan->list, &orig_node->vlan_list);
111 out:
112 spin_unlock_bh(&orig_node->vlan_list_lock);
114 return vlan;
118 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
119 * the originator-vlan object
120 * @orig_vlan: the originator-vlan object to release
122 void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
124 if (atomic_dec_and_test(&orig_vlan->refcount))
125 kfree_rcu(orig_vlan, rcu);
128 int batadv_originator_init(struct batadv_priv *bat_priv)
130 if (bat_priv->orig_hash)
131 return 0;
133 bat_priv->orig_hash = batadv_hash_new(1024);
135 if (!bat_priv->orig_hash)
136 goto err;
138 batadv_hash_set_lock_class(bat_priv->orig_hash,
139 &batadv_orig_hash_lock_class_key);
141 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
142 queue_delayed_work(batadv_event_workqueue,
143 &bat_priv->orig_work,
144 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
146 return 0;
148 err:
149 return -ENOMEM;
153 * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
154 * @rcu: rcu pointer of the neigh_ifinfo object
156 static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu)
158 struct batadv_neigh_ifinfo *neigh_ifinfo;
160 neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu);
162 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
163 batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing);
165 kfree(neigh_ifinfo);
169 * batadv_neigh_ifinfo_free_now - decrement the refcounter and possibly free
170 * the neigh_ifinfo (without rcu callback)
171 * @neigh_ifinfo: the neigh_ifinfo object to release
173 static void
174 batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo)
176 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
177 batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu);
181 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly free
182 * the neigh_ifinfo
183 * @neigh_ifinfo: the neigh_ifinfo object to release
185 void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
187 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
188 call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu);
192 * batadv_neigh_node_free_rcu - free the neigh_node
193 * @rcu: rcu pointer of the neigh_node
195 static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
197 struct hlist_node *node_tmp;
198 struct batadv_neigh_node *neigh_node;
199 struct batadv_neigh_ifinfo *neigh_ifinfo;
201 neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
203 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
204 &neigh_node->ifinfo_list, list) {
205 batadv_neigh_ifinfo_free_ref_now(neigh_ifinfo);
207 batadv_hardif_free_ref_now(neigh_node->if_incoming);
209 kfree(neigh_node);
213 * batadv_neigh_node_free_ref_now - decrement the neighbors refcounter
214 * and possibly free it (without rcu callback)
215 * @neigh_node: neigh neighbor to free
217 static void
218 batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node)
220 if (atomic_dec_and_test(&neigh_node->refcount))
221 batadv_neigh_node_free_rcu(&neigh_node->rcu);
225 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
226 * and possibly free it
227 * @neigh_node: neigh neighbor to free
229 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
231 if (atomic_dec_and_test(&neigh_node->refcount))
232 call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
236 * batadv_orig_node_get_router - router to the originator depending on iface
237 * @orig_node: the orig node for the router
238 * @if_outgoing: the interface where the payload packet has been received or
239 * the OGM should be sent to
241 * Returns the neighbor which should be router for this orig_node/iface.
243 * The object is returned with refcounter increased by 1.
245 struct batadv_neigh_node *
246 batadv_orig_router_get(struct batadv_orig_node *orig_node,
247 const struct batadv_hard_iface *if_outgoing)
249 struct batadv_orig_ifinfo *orig_ifinfo;
250 struct batadv_neigh_node *router = NULL;
252 rcu_read_lock();
253 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
254 if (orig_ifinfo->if_outgoing != if_outgoing)
255 continue;
257 router = rcu_dereference(orig_ifinfo->router);
258 break;
261 if (router && !atomic_inc_not_zero(&router->refcount))
262 router = NULL;
264 rcu_read_unlock();
265 return router;
269 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
270 * @orig_node: the orig node to be queried
271 * @if_outgoing: the interface for which the ifinfo should be acquired
273 * Returns the requested orig_ifinfo or NULL if not found.
275 * The object is returned with refcounter increased by 1.
277 struct batadv_orig_ifinfo *
278 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
279 struct batadv_hard_iface *if_outgoing)
281 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
283 rcu_read_lock();
284 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
285 list) {
286 if (tmp->if_outgoing != if_outgoing)
287 continue;
289 if (!atomic_inc_not_zero(&tmp->refcount))
290 continue;
292 orig_ifinfo = tmp;
293 break;
295 rcu_read_unlock();
297 return orig_ifinfo;
301 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
302 * @orig_node: the orig node to be queried
303 * @if_outgoing: the interface for which the ifinfo should be acquired
305 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
306 * interface otherwise. The object is created and added to the list
307 * if it does not exist.
309 * The object is returned with refcounter increased by 1.
311 struct batadv_orig_ifinfo *
312 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
313 struct batadv_hard_iface *if_outgoing)
315 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
316 unsigned long reset_time;
318 spin_lock_bh(&orig_node->neigh_list_lock);
320 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
321 if (orig_ifinfo)
322 goto out;
324 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
325 if (!orig_ifinfo)
326 goto out;
328 if (if_outgoing != BATADV_IF_DEFAULT &&
329 !atomic_inc_not_zero(&if_outgoing->refcount)) {
330 kfree(orig_ifinfo);
331 orig_ifinfo = NULL;
332 goto out;
335 reset_time = jiffies - 1;
336 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
337 orig_ifinfo->batman_seqno_reset = reset_time;
338 orig_ifinfo->if_outgoing = if_outgoing;
339 INIT_HLIST_NODE(&orig_ifinfo->list);
340 atomic_set(&orig_ifinfo->refcount, 2);
341 hlist_add_head_rcu(&orig_ifinfo->list,
342 &orig_node->ifinfo_list);
343 out:
344 spin_unlock_bh(&orig_node->neigh_list_lock);
345 return orig_ifinfo;
349 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
350 * @neigh_node: the neigh node to be queried
351 * @if_outgoing: the interface for which the ifinfo should be acquired
353 * The object is returned with refcounter increased by 1.
355 * Returns the requested neigh_ifinfo or NULL if not found
357 struct batadv_neigh_ifinfo *
358 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
359 struct batadv_hard_iface *if_outgoing)
361 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
362 *tmp_neigh_ifinfo;
364 rcu_read_lock();
365 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
366 list) {
367 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
368 continue;
370 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
371 continue;
373 neigh_ifinfo = tmp_neigh_ifinfo;
374 break;
376 rcu_read_unlock();
378 return neigh_ifinfo;
382 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
383 * @neigh_node: the neigh node to be queried
384 * @if_outgoing: the interface for which the ifinfo should be acquired
386 * Returns NULL in case of failure or the neigh_ifinfo object for the
387 * if_outgoing interface otherwise. The object is created and added to the list
388 * if it does not exist.
390 * The object is returned with refcounter increased by 1.
392 struct batadv_neigh_ifinfo *
393 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
394 struct batadv_hard_iface *if_outgoing)
396 struct batadv_neigh_ifinfo *neigh_ifinfo;
398 spin_lock_bh(&neigh->ifinfo_lock);
400 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
401 if (neigh_ifinfo)
402 goto out;
404 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
405 if (!neigh_ifinfo)
406 goto out;
408 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
409 kfree(neigh_ifinfo);
410 neigh_ifinfo = NULL;
411 goto out;
414 INIT_HLIST_NODE(&neigh_ifinfo->list);
415 atomic_set(&neigh_ifinfo->refcount, 2);
416 neigh_ifinfo->if_outgoing = if_outgoing;
418 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
420 out:
421 spin_unlock_bh(&neigh->ifinfo_lock);
423 return neigh_ifinfo;
427 * batadv_neigh_node_new - create and init a new neigh_node object
428 * @hard_iface: the interface where the neighbour is connected to
429 * @neigh_addr: the mac address of the neighbour interface
430 * @orig_node: originator object representing the neighbour
432 * Allocates a new neigh_node object and initialises all the generic fields.
433 * Returns the new object or NULL on failure.
435 struct batadv_neigh_node *
436 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
437 const uint8_t *neigh_addr,
438 struct batadv_orig_node *orig_node)
440 struct batadv_neigh_node *neigh_node;
442 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
443 if (!neigh_node)
444 goto out;
446 INIT_HLIST_NODE(&neigh_node->list);
447 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
448 spin_lock_init(&neigh_node->ifinfo_lock);
450 ether_addr_copy(neigh_node->addr, neigh_addr);
451 neigh_node->if_incoming = hard_iface;
452 neigh_node->orig_node = orig_node;
454 /* extra reference for return */
455 atomic_set(&neigh_node->refcount, 2);
457 out:
458 return neigh_node;
462 * batadv_neigh_node_get - retrieve a neighbour from the list
463 * @orig_node: originator which the neighbour belongs to
464 * @hard_iface: the interface where this neighbour is connected to
465 * @addr: the address of the neighbour
467 * Looks for and possibly returns a neighbour belonging to this originator list
468 * which is connected through the provided hard interface.
469 * Returns NULL if the neighbour is not found.
471 struct batadv_neigh_node *
472 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
473 const struct batadv_hard_iface *hard_iface,
474 const uint8_t *addr)
476 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
478 rcu_read_lock();
479 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
480 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
481 continue;
483 if (tmp_neigh_node->if_incoming != hard_iface)
484 continue;
486 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
487 continue;
489 res = tmp_neigh_node;
490 break;
492 rcu_read_unlock();
494 return res;
498 * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
499 * @rcu: rcu pointer of the orig_ifinfo object
501 static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu)
503 struct batadv_orig_ifinfo *orig_ifinfo;
504 struct batadv_neigh_node *router;
506 orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu);
508 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
509 batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing);
511 /* this is the last reference to this object */
512 router = rcu_dereference_protected(orig_ifinfo->router, true);
513 if (router)
514 batadv_neigh_node_free_ref_now(router);
515 kfree(orig_ifinfo);
519 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
520 * the orig_ifinfo (without rcu callback)
521 * @orig_ifinfo: the orig_ifinfo object to release
523 static void
524 batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo)
526 if (atomic_dec_and_test(&orig_ifinfo->refcount))
527 batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu);
531 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly free
532 * the orig_ifinfo
533 * @orig_ifinfo: the orig_ifinfo object to release
535 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
537 if (atomic_dec_and_test(&orig_ifinfo->refcount))
538 call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu);
541 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
543 struct hlist_node *node_tmp;
544 struct batadv_neigh_node *neigh_node;
545 struct batadv_orig_node *orig_node;
546 struct batadv_orig_ifinfo *orig_ifinfo;
548 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
550 spin_lock_bh(&orig_node->neigh_list_lock);
552 /* for all neighbors towards this originator ... */
553 hlist_for_each_entry_safe(neigh_node, node_tmp,
554 &orig_node->neigh_list, list) {
555 hlist_del_rcu(&neigh_node->list);
556 batadv_neigh_node_free_ref_now(neigh_node);
559 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
560 &orig_node->ifinfo_list, list) {
561 hlist_del_rcu(&orig_ifinfo->list);
562 batadv_orig_ifinfo_free_ref_now(orig_ifinfo);
564 spin_unlock_bh(&orig_node->neigh_list_lock);
566 batadv_mcast_purge_orig(orig_node);
568 /* Free nc_nodes */
569 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
571 batadv_frag_purge_orig(orig_node, NULL);
573 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
574 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
576 kfree(orig_node->tt_buff);
577 kfree(orig_node);
581 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
582 * schedule an rcu callback for freeing it
583 * @orig_node: the orig node to free
585 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
587 if (atomic_dec_and_test(&orig_node->refcount))
588 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
592 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
593 * possibly free it (without rcu callback)
594 * @orig_node: the orig node to free
596 void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
598 if (atomic_dec_and_test(&orig_node->refcount))
599 batadv_orig_node_free_rcu(&orig_node->rcu);
602 void batadv_originator_free(struct batadv_priv *bat_priv)
604 struct batadv_hashtable *hash = bat_priv->orig_hash;
605 struct hlist_node *node_tmp;
606 struct hlist_head *head;
607 spinlock_t *list_lock; /* spinlock to protect write access */
608 struct batadv_orig_node *orig_node;
609 uint32_t i;
611 if (!hash)
612 return;
614 cancel_delayed_work_sync(&bat_priv->orig_work);
616 bat_priv->orig_hash = NULL;
618 for (i = 0; i < hash->size; i++) {
619 head = &hash->table[i];
620 list_lock = &hash->list_locks[i];
622 spin_lock_bh(list_lock);
623 hlist_for_each_entry_safe(orig_node, node_tmp,
624 head, hash_entry) {
625 hlist_del_rcu(&orig_node->hash_entry);
626 batadv_orig_node_free_ref(orig_node);
628 spin_unlock_bh(list_lock);
631 batadv_hash_destroy(hash);
635 * batadv_orig_node_new - creates a new orig_node
636 * @bat_priv: the bat priv with all the soft interface information
637 * @addr: the mac address of the originator
639 * Creates a new originator object and initialise all the generic fields.
640 * The new object is not added to the originator list.
641 * Returns the newly created object or NULL on failure.
643 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
644 const uint8_t *addr)
646 struct batadv_orig_node *orig_node;
647 struct batadv_orig_node_vlan *vlan;
648 unsigned long reset_time;
649 int i;
651 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
652 "Creating new originator: %pM\n", addr);
654 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
655 if (!orig_node)
656 return NULL;
658 INIT_HLIST_HEAD(&orig_node->neigh_list);
659 INIT_LIST_HEAD(&orig_node->vlan_list);
660 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
661 spin_lock_init(&orig_node->bcast_seqno_lock);
662 spin_lock_init(&orig_node->neigh_list_lock);
663 spin_lock_init(&orig_node->tt_buff_lock);
664 spin_lock_init(&orig_node->tt_lock);
665 spin_lock_init(&orig_node->vlan_list_lock);
667 batadv_nc_init_orig(orig_node);
669 /* extra reference for return */
670 atomic_set(&orig_node->refcount, 2);
672 orig_node->bat_priv = bat_priv;
673 ether_addr_copy(orig_node->orig, addr);
674 batadv_dat_init_orig_node_addr(orig_node);
675 atomic_set(&orig_node->last_ttvn, 0);
676 orig_node->tt_buff = NULL;
677 orig_node->tt_buff_len = 0;
678 orig_node->last_seen = jiffies;
679 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
680 orig_node->bcast_seqno_reset = reset_time;
682 #ifdef CONFIG_BATMAN_ADV_MCAST
683 orig_node->mcast_flags = BATADV_NO_FLAGS;
684 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
685 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
686 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
687 spin_lock_init(&orig_node->mcast_handler_lock);
688 #endif
690 /* create a vlan object for the "untagged" LAN */
691 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
692 if (!vlan)
693 goto free_orig_node;
694 /* batadv_orig_node_vlan_new() increases the refcounter.
695 * Immediately release vlan since it is not needed anymore in this
696 * context
698 batadv_orig_node_vlan_free_ref(vlan);
700 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
701 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
702 spin_lock_init(&orig_node->fragments[i].lock);
703 orig_node->fragments[i].size = 0;
706 return orig_node;
707 free_orig_node:
708 kfree(orig_node);
709 return NULL;
713 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
714 * @bat_priv: the bat priv with all the soft interface information
715 * @neigh: orig node which is to be checked
717 static void
718 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
719 struct batadv_neigh_node *neigh)
721 struct batadv_neigh_ifinfo *neigh_ifinfo;
722 struct batadv_hard_iface *if_outgoing;
723 struct hlist_node *node_tmp;
725 spin_lock_bh(&neigh->ifinfo_lock);
727 /* for all ifinfo objects for this neighinator */
728 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
729 &neigh->ifinfo_list, list) {
730 if_outgoing = neigh_ifinfo->if_outgoing;
732 /* always keep the default interface */
733 if (if_outgoing == BATADV_IF_DEFAULT)
734 continue;
736 /* don't purge if the interface is not (going) down */
737 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
738 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
739 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
740 continue;
742 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
743 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
744 neigh->addr, if_outgoing->net_dev->name);
746 hlist_del_rcu(&neigh_ifinfo->list);
747 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
750 spin_unlock_bh(&neigh->ifinfo_lock);
754 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
755 * @bat_priv: the bat priv with all the soft interface information
756 * @orig_node: orig node which is to be checked
758 * Returns true if any ifinfo entry was purged, false otherwise.
760 static bool
761 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
762 struct batadv_orig_node *orig_node)
764 struct batadv_orig_ifinfo *orig_ifinfo;
765 struct batadv_hard_iface *if_outgoing;
766 struct hlist_node *node_tmp;
767 bool ifinfo_purged = false;
769 spin_lock_bh(&orig_node->neigh_list_lock);
771 /* for all ifinfo objects for this originator */
772 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
773 &orig_node->ifinfo_list, list) {
774 if_outgoing = orig_ifinfo->if_outgoing;
776 /* always keep the default interface */
777 if (if_outgoing == BATADV_IF_DEFAULT)
778 continue;
780 /* don't purge if the interface is not (going) down */
781 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
782 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
783 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
784 continue;
786 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
787 "router/ifinfo purge: originator %pM, iface: %s\n",
788 orig_node->orig, if_outgoing->net_dev->name);
790 ifinfo_purged = true;
792 hlist_del_rcu(&orig_ifinfo->list);
793 batadv_orig_ifinfo_free_ref(orig_ifinfo);
794 if (orig_node->last_bonding_candidate == orig_ifinfo) {
795 orig_node->last_bonding_candidate = NULL;
796 batadv_orig_ifinfo_free_ref(orig_ifinfo);
800 spin_unlock_bh(&orig_node->neigh_list_lock);
802 return ifinfo_purged;
806 * batadv_purge_orig_neighbors - purges neighbors from originator
807 * @bat_priv: the bat priv with all the soft interface information
808 * @orig_node: orig node which is to be checked
810 * Returns true if any neighbor was purged, false otherwise
812 static bool
813 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
814 struct batadv_orig_node *orig_node)
816 struct hlist_node *node_tmp;
817 struct batadv_neigh_node *neigh_node;
818 bool neigh_purged = false;
819 unsigned long last_seen;
820 struct batadv_hard_iface *if_incoming;
822 spin_lock_bh(&orig_node->neigh_list_lock);
824 /* for all neighbors towards this originator ... */
825 hlist_for_each_entry_safe(neigh_node, node_tmp,
826 &orig_node->neigh_list, list) {
827 last_seen = neigh_node->last_seen;
828 if_incoming = neigh_node->if_incoming;
830 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
831 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
832 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
833 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
834 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
835 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
836 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
837 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
838 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
839 orig_node->orig, neigh_node->addr,
840 if_incoming->net_dev->name);
841 else
842 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
843 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
844 orig_node->orig, neigh_node->addr,
845 jiffies_to_msecs(last_seen));
847 neigh_purged = true;
849 hlist_del_rcu(&neigh_node->list);
850 batadv_neigh_node_free_ref(neigh_node);
851 } else {
852 /* only necessary if not the whole neighbor is to be
853 * deleted, but some interface has been removed.
855 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
859 spin_unlock_bh(&orig_node->neigh_list_lock);
860 return neigh_purged;
864 * batadv_find_best_neighbor - finds the best neighbor after purging
865 * @bat_priv: the bat priv with all the soft interface information
866 * @orig_node: orig node which is to be checked
867 * @if_outgoing: the interface for which the metric should be compared
869 * Returns the current best neighbor, with refcount increased.
871 static struct batadv_neigh_node *
872 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
873 struct batadv_orig_node *orig_node,
874 struct batadv_hard_iface *if_outgoing)
876 struct batadv_neigh_node *best = NULL, *neigh;
877 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
879 rcu_read_lock();
880 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
881 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
882 best, if_outgoing) <= 0))
883 continue;
885 if (!atomic_inc_not_zero(&neigh->refcount))
886 continue;
888 if (best)
889 batadv_neigh_node_free_ref(best);
891 best = neigh;
893 rcu_read_unlock();
895 return best;
899 * batadv_purge_orig_node - purges obsolete information from an orig_node
900 * @bat_priv: the bat priv with all the soft interface information
901 * @orig_node: orig node which is to be checked
903 * This function checks if the orig_node or substructures of it have become
904 * obsolete, and purges this information if that's the case.
906 * Returns true if the orig_node is to be removed, false otherwise.
908 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
909 struct batadv_orig_node *orig_node)
911 struct batadv_neigh_node *best_neigh_node;
912 struct batadv_hard_iface *hard_iface;
913 bool changed_ifinfo, changed_neigh;
915 if (batadv_has_timed_out(orig_node->last_seen,
916 2 * BATADV_PURGE_TIMEOUT)) {
917 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
918 "Originator timeout: originator %pM, last_seen %u\n",
919 orig_node->orig,
920 jiffies_to_msecs(orig_node->last_seen));
921 return true;
923 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
924 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
926 if (!changed_ifinfo && !changed_neigh)
927 return false;
929 /* first for NULL ... */
930 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
931 BATADV_IF_DEFAULT);
932 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
933 best_neigh_node);
934 if (best_neigh_node)
935 batadv_neigh_node_free_ref(best_neigh_node);
937 /* ... then for all other interfaces. */
938 rcu_read_lock();
939 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
940 if (hard_iface->if_status != BATADV_IF_ACTIVE)
941 continue;
943 if (hard_iface->soft_iface != bat_priv->soft_iface)
944 continue;
946 best_neigh_node = batadv_find_best_neighbor(bat_priv,
947 orig_node,
948 hard_iface);
949 batadv_update_route(bat_priv, orig_node, hard_iface,
950 best_neigh_node);
951 if (best_neigh_node)
952 batadv_neigh_node_free_ref(best_neigh_node);
954 rcu_read_unlock();
956 return false;
959 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
961 struct batadv_hashtable *hash = bat_priv->orig_hash;
962 struct hlist_node *node_tmp;
963 struct hlist_head *head;
964 spinlock_t *list_lock; /* spinlock to protect write access */
965 struct batadv_orig_node *orig_node;
966 uint32_t i;
968 if (!hash)
969 return;
971 /* for all origins... */
972 for (i = 0; i < hash->size; i++) {
973 head = &hash->table[i];
974 list_lock = &hash->list_locks[i];
976 spin_lock_bh(list_lock);
977 hlist_for_each_entry_safe(orig_node, node_tmp,
978 head, hash_entry) {
979 if (batadv_purge_orig_node(bat_priv, orig_node)) {
980 batadv_gw_node_delete(bat_priv, orig_node);
981 hlist_del_rcu(&orig_node->hash_entry);
982 batadv_tt_global_del_orig(orig_node->bat_priv,
983 orig_node, -1,
984 "originator timed out");
985 batadv_orig_node_free_ref(orig_node);
986 continue;
989 batadv_frag_purge_orig(orig_node,
990 batadv_frag_check_entry);
992 spin_unlock_bh(list_lock);
995 batadv_gw_node_purge(bat_priv);
996 batadv_gw_election(bat_priv);
999 static void batadv_purge_orig(struct work_struct *work)
1001 struct delayed_work *delayed_work;
1002 struct batadv_priv *bat_priv;
1004 delayed_work = container_of(work, struct delayed_work, work);
1005 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1006 _batadv_purge_orig(bat_priv);
1007 queue_delayed_work(batadv_event_workqueue,
1008 &bat_priv->orig_work,
1009 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1012 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1014 _batadv_purge_orig(bat_priv);
1017 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1019 struct net_device *net_dev = (struct net_device *)seq->private;
1020 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1021 struct batadv_hard_iface *primary_if;
1023 primary_if = batadv_seq_print_text_primary_if_get(seq);
1024 if (!primary_if)
1025 return 0;
1027 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1028 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1029 primary_if->net_dev->dev_addr, net_dev->name,
1030 bat_priv->bat_algo_ops->name);
1032 batadv_hardif_free_ref(primary_if);
1034 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1035 seq_puts(seq,
1036 "No printing function for this routing protocol\n");
1037 return 0;
1040 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1041 BATADV_IF_DEFAULT);
1043 return 0;
1047 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1048 * outgoing interface
1049 * @seq: debugfs table seq_file struct
1050 * @offset: not used
1052 * Returns 0
1054 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1056 struct net_device *net_dev = (struct net_device *)seq->private;
1057 struct batadv_hard_iface *hard_iface;
1058 struct batadv_priv *bat_priv;
1060 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1062 if (!hard_iface || !hard_iface->soft_iface) {
1063 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1064 goto out;
1067 bat_priv = netdev_priv(hard_iface->soft_iface);
1068 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1069 seq_puts(seq,
1070 "No printing function for this routing protocol\n");
1071 goto out;
1074 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1075 seq_puts(seq, "Interface not active\n");
1076 goto out;
1079 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1080 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1081 hard_iface->net_dev->dev_addr,
1082 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1084 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1086 out:
1087 if (hard_iface)
1088 batadv_hardif_free_ref(hard_iface);
1089 return 0;
1092 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1093 int max_if_num)
1095 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1096 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1097 struct batadv_hashtable *hash = bat_priv->orig_hash;
1098 struct hlist_head *head;
1099 struct batadv_orig_node *orig_node;
1100 uint32_t i;
1101 int ret;
1103 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1104 * if_num
1106 for (i = 0; i < hash->size; i++) {
1107 head = &hash->table[i];
1109 rcu_read_lock();
1110 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1111 ret = 0;
1112 if (bao->bat_orig_add_if)
1113 ret = bao->bat_orig_add_if(orig_node,
1114 max_if_num);
1115 if (ret == -ENOMEM)
1116 goto err;
1118 rcu_read_unlock();
1121 return 0;
1123 err:
1124 rcu_read_unlock();
1125 return -ENOMEM;
1128 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1129 int max_if_num)
1131 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1132 struct batadv_hashtable *hash = bat_priv->orig_hash;
1133 struct hlist_head *head;
1134 struct batadv_hard_iface *hard_iface_tmp;
1135 struct batadv_orig_node *orig_node;
1136 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1137 uint32_t i;
1138 int ret;
1140 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1141 * if_num
1143 for (i = 0; i < hash->size; i++) {
1144 head = &hash->table[i];
1146 rcu_read_lock();
1147 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1148 ret = 0;
1149 if (bao->bat_orig_del_if)
1150 ret = bao->bat_orig_del_if(orig_node,
1151 max_if_num,
1152 hard_iface->if_num);
1153 if (ret == -ENOMEM)
1154 goto err;
1156 rcu_read_unlock();
1159 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1160 rcu_read_lock();
1161 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1162 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1163 continue;
1165 if (hard_iface == hard_iface_tmp)
1166 continue;
1168 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1169 continue;
1171 if (hard_iface_tmp->if_num > hard_iface->if_num)
1172 hard_iface_tmp->if_num--;
1174 rcu_read_unlock();
1176 hard_iface->if_num = -1;
1177 return 0;
1179 err:
1180 rcu_read_unlock();
1181 return -ENOMEM;