batman-adv: always run purge_orig_neighbors
[linux-2.6-block.git] / net / batman-adv / originator.c
CommitLineData
e19f9759 1/* Copyright (C) 2009-2014 B.A.T.M.A.N. contributors:
c6c8fea2
SE
2 *
3 * Marek Lindner, Simon Wunderlich
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
ebf38fb7 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
c6c8fea2
SE
16 */
17
c6c8fea2 18#include "main.h"
785ea114 19#include "distributed-arp-table.h"
c6c8fea2
SE
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"
c6c8fea2 26#include "soft-interface.h"
23721387 27#include "bridge_loop_avoidance.h"
d56b1705 28#include "network-coding.h"
610bfc6b 29#include "fragmentation.h"
60432d75 30#include "multicast.h"
c6c8fea2 31
dec05074
AQ
32/* hash class keys */
33static struct lock_class_key batadv_orig_hash_lock_class_key;
34
03fc7f86 35static void batadv_purge_orig(struct work_struct *work);
c6c8fea2 36
b8e2dd13 37/* returns 1 if they are the same originator */
bbad0a5e 38int batadv_compare_orig(const struct hlist_node *node, const void *data2)
b8e2dd13 39{
56303d34
SE
40 const void *data1 = container_of(node, struct batadv_orig_node,
41 hash_entry);
b8e2dd13 42
323813ed 43 return batadv_compare_eth(data1, data2);
b8e2dd13
SE
44}
45
7ea7b4a1
AQ
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
50 *
51 * Returns the vlan object identified by vid and belonging to orig_node or NULL
52 * if it does not exist.
53 */
54struct batadv_orig_node_vlan *
55batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
56 unsigned short vid)
57{
58 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
59
60 rcu_read_lock();
61 list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
62 if (tmp->vid != vid)
63 continue;
64
65 if (!atomic_inc_not_zero(&tmp->refcount))
66 continue;
67
68 vlan = tmp;
69
70 break;
71 }
72 rcu_read_unlock();
73
74 return vlan;
75}
76
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
82 *
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.
86 *
87 * The object is returned with refcounter increased by 1.
88 */
89struct batadv_orig_node_vlan *
90batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
91 unsigned short vid)
92{
93 struct batadv_orig_node_vlan *vlan;
94
95 spin_lock_bh(&orig_node->vlan_list_lock);
96
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;
101
102 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
103 if (!vlan)
104 goto out;
105
106 atomic_set(&vlan->refcount, 2);
107 vlan->vid = vid;
108
109 list_add_rcu(&vlan->list, &orig_node->vlan_list);
110
111out:
112 spin_unlock_bh(&orig_node->vlan_list_lock);
113
114 return vlan;
115}
116
117/**
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
121 */
122void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
123{
124 if (atomic_dec_and_test(&orig_vlan->refcount))
125 kfree_rcu(orig_vlan, rcu);
126}
127
56303d34 128int batadv_originator_init(struct batadv_priv *bat_priv)
c6c8fea2
SE
129{
130 if (bat_priv->orig_hash)
5346c35e 131 return 0;
c6c8fea2 132
1a8eaf07 133 bat_priv->orig_hash = batadv_hash_new(1024);
c6c8fea2
SE
134
135 if (!bat_priv->orig_hash)
136 goto err;
137
dec05074
AQ
138 batadv_hash_set_lock_class(bat_priv->orig_hash,
139 &batadv_orig_hash_lock_class_key);
140
72414442
AQ
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));
145
5346c35e 146 return 0;
c6c8fea2
SE
147
148err:
5346c35e 149 return -ENOMEM;
c6c8fea2
SE
150}
151
89652331
SW
152/**
153 * batadv_neigh_ifinfo_free_rcu - free the neigh_ifinfo object
154 * @rcu: rcu pointer of the neigh_ifinfo object
155 */
156static void batadv_neigh_ifinfo_free_rcu(struct rcu_head *rcu)
157{
158 struct batadv_neigh_ifinfo *neigh_ifinfo;
159
160 neigh_ifinfo = container_of(rcu, struct batadv_neigh_ifinfo, rcu);
161
162 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
163 batadv_hardif_free_ref_now(neigh_ifinfo->if_outgoing);
164
165 kfree(neigh_ifinfo);
166}
167
168/**
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
172 */
173static void
174batadv_neigh_ifinfo_free_ref_now(struct batadv_neigh_ifinfo *neigh_ifinfo)
175{
176 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
177 batadv_neigh_ifinfo_free_rcu(&neigh_ifinfo->rcu);
178}
179
180/**
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
184 */
185void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
186{
187 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
188 call_rcu(&neigh_ifinfo->rcu, batadv_neigh_ifinfo_free_rcu);
189}
190
191/**
192 * batadv_neigh_node_free_rcu - free the neigh_node
193 * @rcu: rcu pointer of the neigh_node
194 */
195static void batadv_neigh_node_free_rcu(struct rcu_head *rcu)
196{
197 struct hlist_node *node_tmp;
198 struct batadv_neigh_node *neigh_node;
199 struct batadv_neigh_ifinfo *neigh_ifinfo;
200
201 neigh_node = container_of(rcu, struct batadv_neigh_node, rcu);
202
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);
206 }
207 batadv_hardif_free_ref_now(neigh_node->if_incoming);
208
209 kfree(neigh_node);
210}
211
212/**
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
216 */
217static void
218batadv_neigh_node_free_ref_now(struct batadv_neigh_node *neigh_node)
219{
220 if (atomic_dec_and_test(&neigh_node->refcount))
221 batadv_neigh_node_free_rcu(&neigh_node->rcu);
222}
223
224/**
225 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
226 * and possibly free it
227 * @neigh_node: neigh neighbor to free
228 */
56303d34 229void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
a4c135c5 230{
44524fcd 231 if (atomic_dec_and_test(&neigh_node->refcount))
89652331 232 call_rcu(&neigh_node->rcu, batadv_neigh_node_free_rcu);
a4c135c5
SW
233}
234
7351a482
SW
235/**
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
240 *
241 * Returns the neighbor which should be router for this orig_node/iface.
242 *
243 * The object is returned with refcounter increased by 1.
244 */
56303d34 245struct batadv_neigh_node *
7351a482
SW
246batadv_orig_router_get(struct batadv_orig_node *orig_node,
247 const struct batadv_hard_iface *if_outgoing)
e1a5382f 248{
7351a482
SW
249 struct batadv_orig_ifinfo *orig_ifinfo;
250 struct batadv_neigh_node *router = NULL;
e1a5382f
LL
251
252 rcu_read_lock();
7351a482
SW
253 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
254 if (orig_ifinfo->if_outgoing != if_outgoing)
255 continue;
256
257 router = rcu_dereference(orig_ifinfo->router);
258 break;
259 }
e1a5382f
LL
260
261 if (router && !atomic_inc_not_zero(&router->refcount))
262 router = NULL;
263
264 rcu_read_unlock();
265 return router;
266}
267
7351a482
SW
268/**
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
272 *
273 * Returns the requested orig_ifinfo or NULL if not found.
274 *
275 * The object is returned with refcounter increased by 1.
276 */
277struct batadv_orig_ifinfo *
278batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
279 struct batadv_hard_iface *if_outgoing)
280{
281 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
282
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;
288
289 if (!atomic_inc_not_zero(&tmp->refcount))
290 continue;
291
292 orig_ifinfo = tmp;
293 break;
294 }
295 rcu_read_unlock();
296
297 return orig_ifinfo;
298}
299
300/**
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
304 *
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.
308 *
309 * The object is returned with refcounter increased by 1.
310 */
311struct batadv_orig_ifinfo *
312batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
313 struct batadv_hard_iface *if_outgoing)
314{
315 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
316 unsigned long reset_time;
317
318 spin_lock_bh(&orig_node->neigh_list_lock);
319
320 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
321 if (orig_ifinfo)
322 goto out;
323
324 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
325 if (!orig_ifinfo)
326 goto out;
327
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;
333 }
334
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);
343out:
344 spin_unlock_bh(&orig_node->neigh_list_lock);
345 return orig_ifinfo;
346}
347
89652331
SW
348/**
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
352 *
353 * The object is returned with refcounter increased by 1.
354 *
355 * Returns the requested neigh_ifinfo or NULL if not found
356 */
357struct batadv_neigh_ifinfo *
358batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
359 struct batadv_hard_iface *if_outgoing)
360{
361 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
362 *tmp_neigh_ifinfo;
363
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;
369
370 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
371 continue;
372
373 neigh_ifinfo = tmp_neigh_ifinfo;
374 break;
375 }
376 rcu_read_unlock();
377
378 return neigh_ifinfo;
379}
380
381/**
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
385 *
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.
389 *
390 * The object is returned with refcounter increased by 1.
391 */
392struct batadv_neigh_ifinfo *
393batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
394 struct batadv_hard_iface *if_outgoing)
395{
396 struct batadv_neigh_ifinfo *neigh_ifinfo;
397
398 spin_lock_bh(&neigh->ifinfo_lock);
399
400 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
401 if (neigh_ifinfo)
402 goto out;
403
404 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
405 if (!neigh_ifinfo)
406 goto out;
407
408 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
409 kfree(neigh_ifinfo);
410 neigh_ifinfo = NULL;
411 goto out;
412 }
413
414 INIT_HLIST_NODE(&neigh_ifinfo->list);
415 atomic_set(&neigh_ifinfo->refcount, 2);
416 neigh_ifinfo->if_outgoing = if_outgoing;
417
418 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
419
420out:
421 spin_unlock_bh(&neigh->ifinfo_lock);
422
423 return neigh_ifinfo;
424}
425
0538f759
AQ
426/**
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
431 *
432 * Allocates a new neigh_node object and initialises all the generic fields.
433 * Returns the new object or NULL on failure.
434 */
56303d34
SE
435struct batadv_neigh_node *
436batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
0538f759
AQ
437 const uint8_t *neigh_addr,
438 struct batadv_orig_node *orig_node)
c6c8fea2 439{
56303d34 440 struct batadv_neigh_node *neigh_node;
c6c8fea2 441
704509b8 442 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
c6c8fea2 443 if (!neigh_node)
7ae8b285 444 goto out;
c6c8fea2 445
9591a79f 446 INIT_HLIST_NODE(&neigh_node->list);
89652331
SW
447 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
448 spin_lock_init(&neigh_node->ifinfo_lock);
c6c8fea2 449
8fdd0153 450 ether_addr_copy(neigh_node->addr, neigh_addr);
0538f759
AQ
451 neigh_node->if_incoming = hard_iface;
452 neigh_node->orig_node = orig_node;
453
1605d0d6
ML
454 /* extra reference for return */
455 atomic_set(&neigh_node->refcount, 2);
c6c8fea2 456
7ae8b285 457out:
c6c8fea2
SE
458 return neigh_node;
459}
460
08bf0ed2
AQ
461/**
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
466 *
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.
470 */
471struct batadv_neigh_node *
472batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
473 const struct batadv_hard_iface *hard_iface,
474 const uint8_t *addr)
475{
476 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
477
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;
482
483 if (tmp_neigh_node->if_incoming != hard_iface)
484 continue;
485
486 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
487 continue;
488
489 res = tmp_neigh_node;
490 break;
491 }
492 rcu_read_unlock();
493
494 return res;
495}
496
7351a482
SW
497/**
498 * batadv_orig_ifinfo_free_rcu - free the orig_ifinfo object
499 * @rcu: rcu pointer of the orig_ifinfo object
500 */
501static void batadv_orig_ifinfo_free_rcu(struct rcu_head *rcu)
502{
503 struct batadv_orig_ifinfo *orig_ifinfo;
000c8dff 504 struct batadv_neigh_node *router;
7351a482
SW
505
506 orig_ifinfo = container_of(rcu, struct batadv_orig_ifinfo, rcu);
507
508 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
509 batadv_hardif_free_ref_now(orig_ifinfo->if_outgoing);
510
000c8dff
SW
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);
7351a482
SW
515 kfree(orig_ifinfo);
516}
517
518/**
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
522 */
523static void
524batadv_orig_ifinfo_free_ref_now(struct batadv_orig_ifinfo *orig_ifinfo)
525{
526 if (atomic_dec_and_test(&orig_ifinfo->refcount))
527 batadv_orig_ifinfo_free_rcu(&orig_ifinfo->rcu);
528}
529
530/**
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
534 */
535void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
536{
537 if (atomic_dec_and_test(&orig_ifinfo->refcount))
538 call_rcu(&orig_ifinfo->rcu, batadv_orig_ifinfo_free_rcu);
539}
540
03fc7f86 541static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
c6c8fea2 542{
b67bfe0d 543 struct hlist_node *node_tmp;
f6c8b711 544 struct batadv_neigh_node *neigh_node;
56303d34 545 struct batadv_orig_node *orig_node;
7351a482 546 struct batadv_orig_ifinfo *orig_ifinfo;
16b1aba8 547
56303d34 548 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
c6c8fea2 549
f987ed6e
ML
550 spin_lock_bh(&orig_node->neigh_list_lock);
551
c6c8fea2 552 /* for all neighbors towards this originator ... */
b67bfe0d 553 hlist_for_each_entry_safe(neigh_node, node_tmp,
9591a79f 554 &orig_node->neigh_list, list) {
f987ed6e 555 hlist_del_rcu(&neigh_node->list);
89652331 556 batadv_neigh_node_free_ref_now(neigh_node);
c6c8fea2
SE
557 }
558
7351a482
SW
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);
563 }
f987ed6e
ML
564 spin_unlock_bh(&orig_node->neigh_list_lock);
565
60432d75
LL
566 batadv_mcast_purge_orig(orig_node);
567
d56b1705
MH
568 /* Free nc_nodes */
569 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
570
610bfc6b
MH
571 batadv_frag_purge_orig(orig_node, NULL);
572
95fb130d 573 batadv_tt_global_del_orig(orig_node->bat_priv, orig_node, -1,
08c36d3e 574 "originator timed out");
c6c8fea2 575
d0015fdd
AQ
576 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
577 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
578
a73105b8 579 kfree(orig_node->tt_buff);
c6c8fea2
SE
580 kfree(orig_node);
581}
582
72822225
LL
583/**
584 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
585 * schedule an rcu callback for freeing it
586 * @orig_node: the orig node to free
587 */
56303d34 588void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
7b36e8ee
ML
589{
590 if (atomic_dec_and_test(&orig_node->refcount))
03fc7f86 591 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
7b36e8ee
ML
592}
593
72822225
LL
594/**
595 * batadv_orig_node_free_ref_now - decrement the orig node refcounter and
596 * possibly free it (without rcu callback)
597 * @orig_node: the orig node to free
598 */
599void batadv_orig_node_free_ref_now(struct batadv_orig_node *orig_node)
600{
601 if (atomic_dec_and_test(&orig_node->refcount))
602 batadv_orig_node_free_rcu(&orig_node->rcu);
603}
604
56303d34 605void batadv_originator_free(struct batadv_priv *bat_priv)
c6c8fea2 606{
5bf74e9c 607 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 608 struct hlist_node *node_tmp;
16b1aba8 609 struct hlist_head *head;
16b1aba8 610 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 611 struct batadv_orig_node *orig_node;
c90681b8 612 uint32_t i;
16b1aba8
ML
613
614 if (!hash)
c6c8fea2
SE
615 return;
616
617 cancel_delayed_work_sync(&bat_priv->orig_work);
618
c6c8fea2 619 bat_priv->orig_hash = NULL;
16b1aba8
ML
620
621 for (i = 0; i < hash->size; i++) {
622 head = &hash->table[i];
623 list_lock = &hash->list_locks[i];
624
625 spin_lock_bh(list_lock);
b67bfe0d 626 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 627 head, hash_entry) {
b67bfe0d 628 hlist_del_rcu(&orig_node->hash_entry);
7d211efc 629 batadv_orig_node_free_ref(orig_node);
16b1aba8
ML
630 }
631 spin_unlock_bh(list_lock);
632 }
633
1a8eaf07 634 batadv_hash_destroy(hash);
c6c8fea2
SE
635}
636
bbad0a5e
AQ
637/**
638 * batadv_orig_node_new - creates a new orig_node
639 * @bat_priv: the bat priv with all the soft interface information
640 * @addr: the mac address of the originator
641 *
642 * Creates a new originator object and initialise all the generic fields.
643 * The new object is not added to the originator list.
644 * Returns the newly created object or NULL on failure.
9cfc7bd6 645 */
bbad0a5e 646struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
56303d34 647 const uint8_t *addr)
c6c8fea2 648{
56303d34 649 struct batadv_orig_node *orig_node;
7ea7b4a1 650 struct batadv_orig_node_vlan *vlan;
42d0b044 651 unsigned long reset_time;
bbad0a5e 652 int i;
c6c8fea2 653
39c75a51
SE
654 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
655 "Creating new originator: %pM\n", addr);
c6c8fea2 656
704509b8 657 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
c6c8fea2
SE
658 if (!orig_node)
659 return NULL;
660
9591a79f 661 INIT_HLIST_HEAD(&orig_node->neigh_list);
7ea7b4a1 662 INIT_LIST_HEAD(&orig_node->vlan_list);
7351a482 663 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
f3e0008f 664 spin_lock_init(&orig_node->bcast_seqno_lock);
f987ed6e 665 spin_lock_init(&orig_node->neigh_list_lock);
a73105b8 666 spin_lock_init(&orig_node->tt_buff_lock);
a70a9aa9 667 spin_lock_init(&orig_node->tt_lock);
7ea7b4a1 668 spin_lock_init(&orig_node->vlan_list_lock);
7b36e8ee 669
d56b1705
MH
670 batadv_nc_init_orig(orig_node);
671
7b36e8ee
ML
672 /* extra reference for return */
673 atomic_set(&orig_node->refcount, 2);
c6c8fea2 674
16b1aba8 675 orig_node->bat_priv = bat_priv;
8fdd0153 676 ether_addr_copy(orig_node->orig, addr);
785ea114 677 batadv_dat_init_orig_node_addr(orig_node);
c8c991bf 678 atomic_set(&orig_node->last_ttvn, 0);
2dafb49d 679 orig_node->tt_buff = NULL;
a73105b8 680 orig_node->tt_buff_len = 0;
42d0b044
SE
681 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
682 orig_node->bcast_seqno_reset = reset_time;
60432d75
LL
683#ifdef CONFIG_BATMAN_ADV_MCAST
684 orig_node->mcast_flags = BATADV_NO_FLAGS;
685#endif
c6c8fea2 686
7ea7b4a1
AQ
687 /* create a vlan object for the "untagged" LAN */
688 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
689 if (!vlan)
690 goto free_orig_node;
691 /* batadv_orig_node_vlan_new() increases the refcounter.
692 * Immediately release vlan since it is not needed anymore in this
693 * context
694 */
695 batadv_orig_node_vlan_free_ref(vlan);
696
610bfc6b
MH
697 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
698 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
699 spin_lock_init(&orig_node->fragments[i].lock);
700 orig_node->fragments[i].size = 0;
701 }
702
c6c8fea2 703 return orig_node;
c6c8fea2
SE
704free_orig_node:
705 kfree(orig_node);
706 return NULL;
707}
708
7351a482
SW
709/**
710 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
711 * @bat_priv: the bat priv with all the soft interface information
712 * @orig_node: orig node which is to be checked
713 *
714 * Returns true if any ifinfo entry was purged, false otherwise.
715 */
716static bool
717batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
718 struct batadv_orig_node *orig_node)
719{
720 struct batadv_orig_ifinfo *orig_ifinfo;
721 struct batadv_hard_iface *if_outgoing;
722 struct hlist_node *node_tmp;
723 bool ifinfo_purged = false;
724
725 spin_lock_bh(&orig_node->neigh_list_lock);
726
727 /* for all ifinfo objects for this originator */
728 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
729 &orig_node->ifinfo_list, list) {
730 if_outgoing = orig_ifinfo->if_outgoing;
731
732 /* always keep the default interface */
733 if (if_outgoing == BATADV_IF_DEFAULT)
734 continue;
735
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;
741
742 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
743 "router/ifinfo purge: originator %pM, iface: %s\n",
744 orig_node->orig, if_outgoing->net_dev->name);
745
746 ifinfo_purged = true;
747
748 hlist_del_rcu(&orig_ifinfo->list);
749 batadv_orig_ifinfo_free_ref(orig_ifinfo);
f3b3d901
SW
750 if (orig_node->last_bonding_candidate == orig_ifinfo) {
751 orig_node->last_bonding_candidate = NULL;
752 batadv_orig_ifinfo_free_ref(orig_ifinfo);
753 }
7351a482
SW
754 }
755
756 spin_unlock_bh(&orig_node->neigh_list_lock);
757
758 return ifinfo_purged;
759}
760
761
89652331
SW
762/**
763 * batadv_purge_orig_neighbors - purges neighbors from originator
764 * @bat_priv: the bat priv with all the soft interface information
765 * @orig_node: orig node which is to be checked
766 *
767 * Returns true if any neighbor was purged, false otherwise
768 */
56303d34
SE
769static bool
770batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
89652331 771 struct batadv_orig_node *orig_node)
c6c8fea2 772{
b67bfe0d 773 struct hlist_node *node_tmp;
56303d34 774 struct batadv_neigh_node *neigh_node;
c6c8fea2 775 bool neigh_purged = false;
0b0094e0 776 unsigned long last_seen;
56303d34 777 struct batadv_hard_iface *if_incoming;
c6c8fea2 778
f987ed6e
ML
779 spin_lock_bh(&orig_node->neigh_list_lock);
780
c6c8fea2 781 /* for all neighbors towards this originator ... */
b67bfe0d 782 hlist_for_each_entry_safe(neigh_node, node_tmp,
9591a79f 783 &orig_node->neigh_list, list) {
1eda58bf
SE
784 last_seen = neigh_node->last_seen;
785 if_incoming = neigh_node->if_incoming;
786
42d0b044 787 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
e9a4f295
SE
788 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
789 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
790 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
e9a4f295
SE
791 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
792 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
793 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
39c75a51 794 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
795 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
796 orig_node->orig, neigh_node->addr,
797 if_incoming->net_dev->name);
c6c8fea2 798 else
39c75a51 799 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
800 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
801 orig_node->orig, neigh_node->addr,
802 jiffies_to_msecs(last_seen));
c6c8fea2
SE
803
804 neigh_purged = true;
9591a79f 805
f987ed6e 806 hlist_del_rcu(&neigh_node->list);
7d211efc 807 batadv_neigh_node_free_ref(neigh_node);
c6c8fea2
SE
808 }
809 }
f987ed6e
ML
810
811 spin_unlock_bh(&orig_node->neigh_list_lock);
c6c8fea2
SE
812 return neigh_purged;
813}
814
89652331
SW
815/**
816 * batadv_find_best_neighbor - finds the best neighbor after purging
817 * @bat_priv: the bat priv with all the soft interface information
818 * @orig_node: orig node which is to be checked
819 * @if_outgoing: the interface for which the metric should be compared
820 *
821 * Returns the current best neighbor, with refcount increased.
822 */
823static struct batadv_neigh_node *
824batadv_find_best_neighbor(struct batadv_priv *bat_priv,
825 struct batadv_orig_node *orig_node,
826 struct batadv_hard_iface *if_outgoing)
827{
828 struct batadv_neigh_node *best = NULL, *neigh;
829 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
830
831 rcu_read_lock();
832 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
833 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
834 best, if_outgoing) <= 0))
835 continue;
836
837 if (!atomic_inc_not_zero(&neigh->refcount))
838 continue;
839
840 if (best)
841 batadv_neigh_node_free_ref(best);
842
843 best = neigh;
844 }
845 rcu_read_unlock();
846
847 return best;
848}
849
7351a482
SW
850/**
851 * batadv_purge_orig_node - purges obsolete information from an orig_node
852 * @bat_priv: the bat priv with all the soft interface information
853 * @orig_node: orig node which is to be checked
854 *
855 * This function checks if the orig_node or substructures of it have become
856 * obsolete, and purges this information if that's the case.
857 *
858 * Returns true if the orig_node is to be removed, false otherwise.
859 */
56303d34
SE
860static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
861 struct batadv_orig_node *orig_node)
c6c8fea2 862{
56303d34 863 struct batadv_neigh_node *best_neigh_node;
7351a482 864 struct batadv_hard_iface *hard_iface;
7b955a9f 865 bool changed_ifinfo, changed_neigh;
c6c8fea2 866
42d0b044
SE
867 if (batadv_has_timed_out(orig_node->last_seen,
868 2 * BATADV_PURGE_TIMEOUT)) {
39c75a51 869 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1eda58bf
SE
870 "Originator timeout: originator %pM, last_seen %u\n",
871 orig_node->orig,
872 jiffies_to_msecs(orig_node->last_seen));
c6c8fea2 873 return true;
c6c8fea2 874 }
7b955a9f
SW
875 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
876 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
7351a482 877
7b955a9f 878 if (!changed_ifinfo && !changed_neigh)
89652331
SW
879 return false;
880
7351a482 881 /* first for NULL ... */
89652331
SW
882 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
883 BATADV_IF_DEFAULT);
7351a482
SW
884 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
885 best_neigh_node);
89652331
SW
886 if (best_neigh_node)
887 batadv_neigh_node_free_ref(best_neigh_node);
c6c8fea2 888
7351a482
SW
889 /* ... then for all other interfaces. */
890 rcu_read_lock();
891 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
892 if (hard_iface->if_status != BATADV_IF_ACTIVE)
893 continue;
894
895 if (hard_iface->soft_iface != bat_priv->soft_iface)
896 continue;
897
898 best_neigh_node = batadv_find_best_neighbor(bat_priv,
899 orig_node,
900 hard_iface);
901 batadv_update_route(bat_priv, orig_node, hard_iface,
902 best_neigh_node);
903 if (best_neigh_node)
904 batadv_neigh_node_free_ref(best_neigh_node);
905 }
906 rcu_read_unlock();
907
c6c8fea2
SE
908 return false;
909}
910
56303d34 911static void _batadv_purge_orig(struct batadv_priv *bat_priv)
c6c8fea2 912{
5bf74e9c 913 struct batadv_hashtable *hash = bat_priv->orig_hash;
b67bfe0d 914 struct hlist_node *node_tmp;
c6c8fea2 915 struct hlist_head *head;
fb778ea1 916 spinlock_t *list_lock; /* spinlock to protect write access */
56303d34 917 struct batadv_orig_node *orig_node;
c90681b8 918 uint32_t i;
c6c8fea2
SE
919
920 if (!hash)
921 return;
922
c6c8fea2
SE
923 /* for all origins... */
924 for (i = 0; i < hash->size; i++) {
925 head = &hash->table[i];
fb778ea1 926 list_lock = &hash->list_locks[i];
c6c8fea2 927
fb778ea1 928 spin_lock_bh(list_lock);
b67bfe0d 929 hlist_for_each_entry_safe(orig_node, node_tmp,
7aadf889 930 head, hash_entry) {
03fc7f86 931 if (batadv_purge_orig_node(bat_priv, orig_node)) {
414254e3 932 batadv_gw_node_delete(bat_priv, orig_node);
b67bfe0d 933 hlist_del_rcu(&orig_node->hash_entry);
7d211efc 934 batadv_orig_node_free_ref(orig_node);
fb778ea1 935 continue;
c6c8fea2 936 }
610bfc6b
MH
937
938 batadv_frag_purge_orig(orig_node,
939 batadv_frag_check_entry);
c6c8fea2 940 }
fb778ea1 941 spin_unlock_bh(list_lock);
c6c8fea2
SE
942 }
943
7cf06bc6
SE
944 batadv_gw_node_purge(bat_priv);
945 batadv_gw_election(bat_priv);
c6c8fea2
SE
946}
947
03fc7f86 948static void batadv_purge_orig(struct work_struct *work)
c6c8fea2 949{
56303d34
SE
950 struct delayed_work *delayed_work;
951 struct batadv_priv *bat_priv;
c6c8fea2 952
56303d34
SE
953 delayed_work = container_of(work, struct delayed_work, work);
954 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
03fc7f86 955 _batadv_purge_orig(bat_priv);
72414442
AQ
956 queue_delayed_work(batadv_event_workqueue,
957 &bat_priv->orig_work,
958 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
c6c8fea2
SE
959}
960
56303d34 961void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
c6c8fea2 962{
03fc7f86 963 _batadv_purge_orig(bat_priv);
c6c8fea2
SE
964}
965
7d211efc 966int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
c6c8fea2
SE
967{
968 struct net_device *net_dev = (struct net_device *)seq->private;
56303d34 969 struct batadv_priv *bat_priv = netdev_priv(net_dev);
56303d34 970 struct batadv_hard_iface *primary_if;
32ae9b22 971
30da63a6
ML
972 primary_if = batadv_seq_print_text_primary_if_get(seq);
973 if (!primary_if)
737a2a22 974 return 0;
c6c8fea2 975
737a2a22 976 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
42d0b044 977 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
737a2a22
AQ
978 primary_if->net_dev->dev_addr, net_dev->name,
979 bat_priv->bat_algo_ops->name);
c6c8fea2 980
737a2a22 981 batadv_hardif_free_ref(primary_if);
c6c8fea2 982
737a2a22
AQ
983 if (!bat_priv->bat_algo_ops->bat_orig_print) {
984 seq_puts(seq,
985 "No printing function for this routing protocol\n");
986 return 0;
c6c8fea2
SE
987 }
988
cb1c92ec
SW
989 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
990 BATADV_IF_DEFAULT);
c6c8fea2 991
30da63a6 992 return 0;
c6c8fea2
SE
993}
994
cb1c92ec
SW
995/**
996 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
997 * outgoing interface
998 * @seq: debugfs table seq_file struct
999 * @offset: not used
1000 *
1001 * Returns 0
1002 */
1003int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1004{
1005 struct net_device *net_dev = (struct net_device *)seq->private;
1006 struct batadv_hard_iface *hard_iface;
1007 struct batadv_priv *bat_priv;
1008
1009 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1010
1011 if (!hard_iface || !hard_iface->soft_iface) {
1012 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1013 goto out;
1014 }
1015
1016 bat_priv = netdev_priv(hard_iface->soft_iface);
1017 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1018 seq_puts(seq,
1019 "No printing function for this routing protocol\n");
1020 goto out;
1021 }
1022
1023 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1024 seq_puts(seq, "Interface not active\n");
1025 goto out;
1026 }
1027
1028 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1029 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1030 hard_iface->net_dev->dev_addr,
1031 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1032
1033 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1034
1035out:
1036 batadv_hardif_free_ref(hard_iface);
1037 return 0;
1038}
1039
56303d34
SE
1040int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1041 int max_if_num)
c6c8fea2 1042{
56303d34 1043 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
d0015fdd 1044 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
5bf74e9c 1045 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 1046 struct hlist_head *head;
56303d34 1047 struct batadv_orig_node *orig_node;
c90681b8
AQ
1048 uint32_t i;
1049 int ret;
c6c8fea2
SE
1050
1051 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
1052 * if_num
1053 */
c6c8fea2
SE
1054 for (i = 0; i < hash->size; i++) {
1055 head = &hash->table[i];
1056
fb778ea1 1057 rcu_read_lock();
b67bfe0d 1058 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
d0015fdd
AQ
1059 ret = 0;
1060 if (bao->bat_orig_add_if)
1061 ret = bao->bat_orig_add_if(orig_node,
1062 max_if_num);
5346c35e 1063 if (ret == -ENOMEM)
c6c8fea2
SE
1064 goto err;
1065 }
fb778ea1 1066 rcu_read_unlock();
c6c8fea2
SE
1067 }
1068
c6c8fea2
SE
1069 return 0;
1070
1071err:
fb778ea1 1072 rcu_read_unlock();
c6c8fea2
SE
1073 return -ENOMEM;
1074}
1075
56303d34
SE
1076int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1077 int max_if_num)
c6c8fea2 1078{
56303d34 1079 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
5bf74e9c 1080 struct batadv_hashtable *hash = bat_priv->orig_hash;
c6c8fea2 1081 struct hlist_head *head;
56303d34
SE
1082 struct batadv_hard_iface *hard_iface_tmp;
1083 struct batadv_orig_node *orig_node;
d0015fdd 1084 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
c90681b8
AQ
1085 uint32_t i;
1086 int ret;
c6c8fea2
SE
1087
1088 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
9cfc7bd6
SE
1089 * if_num
1090 */
c6c8fea2
SE
1091 for (i = 0; i < hash->size; i++) {
1092 head = &hash->table[i];
1093
fb778ea1 1094 rcu_read_lock();
b67bfe0d 1095 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
d0015fdd
AQ
1096 ret = 0;
1097 if (bao->bat_orig_del_if)
1098 ret = bao->bat_orig_del_if(orig_node,
1099 max_if_num,
1100 hard_iface->if_num);
5346c35e 1101 if (ret == -ENOMEM)
c6c8fea2
SE
1102 goto err;
1103 }
fb778ea1 1104 rcu_read_unlock();
c6c8fea2
SE
1105 }
1106
1107 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1108 rcu_read_lock();
3193e8fd 1109 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
e9a4f295 1110 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
c6c8fea2
SE
1111 continue;
1112
e6c10f43 1113 if (hard_iface == hard_iface_tmp)
c6c8fea2
SE
1114 continue;
1115
e6c10f43 1116 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
c6c8fea2
SE
1117 continue;
1118
e6c10f43
ML
1119 if (hard_iface_tmp->if_num > hard_iface->if_num)
1120 hard_iface_tmp->if_num--;
c6c8fea2
SE
1121 }
1122 rcu_read_unlock();
1123
e6c10f43 1124 hard_iface->if_num = -1;
c6c8fea2
SE
1125 return 0;
1126
1127err:
fb778ea1 1128 rcu_read_unlock();
c6c8fea2
SE
1129 return -ENOMEM;
1130}