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