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