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