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