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