1 /* Copyright (C) 2009-2014 B.A.T.M.A.N. contributors:
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
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "main.h"
19 #include "distributed-arp-table.h"
20 #include "originator.h"
21 #include "hash.h"
22 #include "translation-table.h"
23 #include "routing.h"
24 #include "gateway_client.h"
25 #include "hard-interface.h"
26 #include "soft-interface.h"
27 #include "bridge_loop_avoidance.h"
28 #include "network-coding.h"
29 #include "fragmentation.h"
30 #include "multicast.h"
31
32 /* hash class keys */
33 static struct lock_class_key batadv_orig_hash_lock_class_key;
34
35 static void batadv_purge_orig(struct work_struct *work);
36
37 /* returns 1 if they are the same originator */
batadv_compare_orig(const struct hlist_node * node,const void * data2)38 int batadv_compare_orig(const struct hlist_node *node, const void *data2)
39 {
40 const void *data1 = container_of(node, struct batadv_orig_node,
41 hash_entry);
42
43 return batadv_compare_eth(data1, data2);
44 }
45
46 /**
47 * batadv_orig_node_vlan_get - get an orig_node_vlan object
48 * @orig_node: the originator serving the VLAN
49 * @vid: the VLAN identifier
50 *
51 * Returns the vlan object identified by vid and belonging to orig_node or NULL
52 * if it does not exist.
53 */
54 struct batadv_orig_node_vlan *
batadv_orig_node_vlan_get(struct batadv_orig_node * orig_node,unsigned short vid)55 batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
56 unsigned short vid)
57 {
58 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
59
60 rcu_read_lock();
61 list_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
62 if (tmp->vid != vid)
63 continue;
64
65 if (!atomic_inc_not_zero(&tmp->refcount))
66 continue;
67
68 vlan = tmp;
69
70 break;
71 }
72 rcu_read_unlock();
73
74 return vlan;
75 }
76
77 /**
78 * batadv_orig_node_vlan_new - search and possibly create an orig_node_vlan
79 * object
80 * @orig_node: the originator serving the VLAN
81 * @vid: the VLAN identifier
82 *
83 * Returns NULL in case of failure or the vlan object identified by vid and
84 * belonging to orig_node otherwise. The object is created and added to the list
85 * if it does not exist.
86 *
87 * The object is returned with refcounter increased by 1.
88 */
89 struct batadv_orig_node_vlan *
batadv_orig_node_vlan_new(struct batadv_orig_node * orig_node,unsigned short vid)90 batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
91 unsigned short vid)
92 {
93 struct batadv_orig_node_vlan *vlan;
94
95 spin_lock_bh(&orig_node->vlan_list_lock);
96
97 /* first look if an object for this vid already exists */
98 vlan = batadv_orig_node_vlan_get(orig_node, vid);
99 if (vlan)
100 goto out;
101
102 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
103 if (!vlan)
104 goto out;
105
106 atomic_set(&vlan->refcount, 2);
107 vlan->vid = vid;
108
109 list_add_rcu(&vlan->list, &orig_node->vlan_list);
110
111 out:
112 spin_unlock_bh(&orig_node->vlan_list_lock);
113
114 return vlan;
115 }
116
117 /**
118 * batadv_orig_node_vlan_free_ref - decrement the refcounter and possibly free
119 * the originator-vlan object
120 * @orig_vlan: the originator-vlan object to release
121 */
batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan * orig_vlan)122 void batadv_orig_node_vlan_free_ref(struct batadv_orig_node_vlan *orig_vlan)
123 {
124 if (atomic_dec_and_test(&orig_vlan->refcount))
125 kfree_rcu(orig_vlan, rcu);
126 }
127
batadv_originator_init(struct batadv_priv * bat_priv)128 int batadv_originator_init(struct batadv_priv *bat_priv)
129 {
130 if (bat_priv->orig_hash)
131 return 0;
132
133 bat_priv->orig_hash = batadv_hash_new(1024);
134
135 if (!bat_priv->orig_hash)
136 goto err;
137
138 batadv_hash_set_lock_class(bat_priv->orig_hash,
139 &batadv_orig_hash_lock_class_key);
140
141 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
142 queue_delayed_work(batadv_event_workqueue,
143 &bat_priv->orig_work,
144 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
145
146 return 0;
147
148 err:
149 return -ENOMEM;
150 }
151
152 /**
153 * batadv_neigh_ifinfo_release - release neigh_ifinfo from lists and queue for
154 * free after rcu grace period
155 * @neigh_ifinfo: the neigh_ifinfo object to release
156 */
157 static void
batadv_neigh_ifinfo_release(struct batadv_neigh_ifinfo * neigh_ifinfo)158 batadv_neigh_ifinfo_release(struct batadv_neigh_ifinfo *neigh_ifinfo)
159 {
160 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
161 batadv_hardif_free_ref(neigh_ifinfo->if_outgoing);
162
163 kfree_rcu(neigh_ifinfo, rcu);
164 }
165
166 /**
167 * batadv_neigh_ifinfo_free_ref - decrement the refcounter and possibly release
168 * the neigh_ifinfo
169 * @neigh_ifinfo: the neigh_ifinfo object to release
170 */
batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo * neigh_ifinfo)171 void batadv_neigh_ifinfo_free_ref(struct batadv_neigh_ifinfo *neigh_ifinfo)
172 {
173 if (atomic_dec_and_test(&neigh_ifinfo->refcount))
174 batadv_neigh_ifinfo_release(neigh_ifinfo);
175 }
176
177 /**
178 * batadv_neigh_node_release - release neigh_node from lists and queue for
179 * free after rcu grace period
180 * @neigh_node: neigh neighbor to free
181 */
batadv_neigh_node_release(struct batadv_neigh_node * neigh_node)182 static void batadv_neigh_node_release(struct batadv_neigh_node *neigh_node)
183 {
184 struct hlist_node *node_tmp;
185 struct batadv_neigh_ifinfo *neigh_ifinfo;
186
187 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
188 &neigh_node->ifinfo_list, list) {
189 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
190 }
191 batadv_hardif_free_ref(neigh_node->if_incoming);
192
193 kfree_rcu(neigh_node, rcu);
194 }
195
196 /**
197 * batadv_neigh_node_free_ref - decrement the neighbors refcounter
198 * and possibly release it
199 * @neigh_node: neigh neighbor to free
200 */
batadv_neigh_node_free_ref(struct batadv_neigh_node * neigh_node)201 void batadv_neigh_node_free_ref(struct batadv_neigh_node *neigh_node)
202 {
203 if (atomic_dec_and_test(&neigh_node->refcount))
204 batadv_neigh_node_release(neigh_node);
205 }
206
207 /**
208 * batadv_orig_node_get_router - router to the originator depending on iface
209 * @orig_node: the orig node for the router
210 * @if_outgoing: the interface where the payload packet has been received or
211 * the OGM should be sent to
212 *
213 * Returns the neighbor which should be router for this orig_node/iface.
214 *
215 * The object is returned with refcounter increased by 1.
216 */
217 struct batadv_neigh_node *
batadv_orig_router_get(struct batadv_orig_node * orig_node,const struct batadv_hard_iface * if_outgoing)218 batadv_orig_router_get(struct batadv_orig_node *orig_node,
219 const struct batadv_hard_iface *if_outgoing)
220 {
221 struct batadv_orig_ifinfo *orig_ifinfo;
222 struct batadv_neigh_node *router = NULL;
223
224 rcu_read_lock();
225 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
226 if (orig_ifinfo->if_outgoing != if_outgoing)
227 continue;
228
229 router = rcu_dereference(orig_ifinfo->router);
230 break;
231 }
232
233 if (router && !atomic_inc_not_zero(&router->refcount))
234 router = NULL;
235
236 rcu_read_unlock();
237 return router;
238 }
239
240 /**
241 * batadv_orig_ifinfo_get - find the ifinfo from an orig_node
242 * @orig_node: the orig node to be queried
243 * @if_outgoing: the interface for which the ifinfo should be acquired
244 *
245 * Returns the requested orig_ifinfo or NULL if not found.
246 *
247 * The object is returned with refcounter increased by 1.
248 */
249 struct batadv_orig_ifinfo *
batadv_orig_ifinfo_get(struct batadv_orig_node * orig_node,struct batadv_hard_iface * if_outgoing)250 batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
251 struct batadv_hard_iface *if_outgoing)
252 {
253 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
254
255 rcu_read_lock();
256 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
257 list) {
258 if (tmp->if_outgoing != if_outgoing)
259 continue;
260
261 if (!atomic_inc_not_zero(&tmp->refcount))
262 continue;
263
264 orig_ifinfo = tmp;
265 break;
266 }
267 rcu_read_unlock();
268
269 return orig_ifinfo;
270 }
271
272 /**
273 * batadv_orig_ifinfo_new - search and possibly create an orig_ifinfo object
274 * @orig_node: the orig node to be queried
275 * @if_outgoing: the interface for which the ifinfo should be acquired
276 *
277 * Returns NULL in case of failure or the orig_ifinfo object for the if_outgoing
278 * interface otherwise. The object is created and added to the list
279 * if it does not exist.
280 *
281 * The object is returned with refcounter increased by 1.
282 */
283 struct batadv_orig_ifinfo *
batadv_orig_ifinfo_new(struct batadv_orig_node * orig_node,struct batadv_hard_iface * if_outgoing)284 batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
285 struct batadv_hard_iface *if_outgoing)
286 {
287 struct batadv_orig_ifinfo *orig_ifinfo = NULL;
288 unsigned long reset_time;
289
290 spin_lock_bh(&orig_node->neigh_list_lock);
291
292 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
293 if (orig_ifinfo)
294 goto out;
295
296 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
297 if (!orig_ifinfo)
298 goto out;
299
300 if (if_outgoing != BATADV_IF_DEFAULT &&
301 !atomic_inc_not_zero(&if_outgoing->refcount)) {
302 kfree(orig_ifinfo);
303 orig_ifinfo = NULL;
304 goto out;
305 }
306
307 reset_time = jiffies - 1;
308 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
309 orig_ifinfo->batman_seqno_reset = reset_time;
310 orig_ifinfo->if_outgoing = if_outgoing;
311 INIT_HLIST_NODE(&orig_ifinfo->list);
312 atomic_set(&orig_ifinfo->refcount, 2);
313 hlist_add_head_rcu(&orig_ifinfo->list,
314 &orig_node->ifinfo_list);
315 out:
316 spin_unlock_bh(&orig_node->neigh_list_lock);
317 return orig_ifinfo;
318 }
319
320 /**
321 * batadv_neigh_ifinfo_get - find the ifinfo from an neigh_node
322 * @neigh_node: the neigh node to be queried
323 * @if_outgoing: the interface for which the ifinfo should be acquired
324 *
325 * The object is returned with refcounter increased by 1.
326 *
327 * Returns the requested neigh_ifinfo or NULL if not found
328 */
329 struct batadv_neigh_ifinfo *
batadv_neigh_ifinfo_get(struct batadv_neigh_node * neigh,struct batadv_hard_iface * if_outgoing)330 batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
331 struct batadv_hard_iface *if_outgoing)
332 {
333 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
334 *tmp_neigh_ifinfo;
335
336 rcu_read_lock();
337 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
338 list) {
339 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
340 continue;
341
342 if (!atomic_inc_not_zero(&tmp_neigh_ifinfo->refcount))
343 continue;
344
345 neigh_ifinfo = tmp_neigh_ifinfo;
346 break;
347 }
348 rcu_read_unlock();
349
350 return neigh_ifinfo;
351 }
352
353 /**
354 * batadv_neigh_ifinfo_new - search and possibly create an neigh_ifinfo object
355 * @neigh_node: the neigh node to be queried
356 * @if_outgoing: the interface for which the ifinfo should be acquired
357 *
358 * Returns NULL in case of failure or the neigh_ifinfo object for the
359 * if_outgoing interface otherwise. The object is created and added to the list
360 * if it does not exist.
361 *
362 * The object is returned with refcounter increased by 1.
363 */
364 struct batadv_neigh_ifinfo *
batadv_neigh_ifinfo_new(struct batadv_neigh_node * neigh,struct batadv_hard_iface * if_outgoing)365 batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
366 struct batadv_hard_iface *if_outgoing)
367 {
368 struct batadv_neigh_ifinfo *neigh_ifinfo;
369
370 spin_lock_bh(&neigh->ifinfo_lock);
371
372 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
373 if (neigh_ifinfo)
374 goto out;
375
376 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
377 if (!neigh_ifinfo)
378 goto out;
379
380 if (if_outgoing && !atomic_inc_not_zero(&if_outgoing->refcount)) {
381 kfree(neigh_ifinfo);
382 neigh_ifinfo = NULL;
383 goto out;
384 }
385
386 INIT_HLIST_NODE(&neigh_ifinfo->list);
387 atomic_set(&neigh_ifinfo->refcount, 2);
388 neigh_ifinfo->if_outgoing = if_outgoing;
389
390 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
391
392 out:
393 spin_unlock_bh(&neigh->ifinfo_lock);
394
395 return neigh_ifinfo;
396 }
397
398 /**
399 * batadv_neigh_node_new - create and init a new neigh_node object
400 * @hard_iface: the interface where the neighbour is connected to
401 * @neigh_addr: the mac address of the neighbour interface
402 * @orig_node: originator object representing the neighbour
403 *
404 * Allocates a new neigh_node object and initialises all the generic fields.
405 * Returns the new object or NULL on failure.
406 */
407 struct batadv_neigh_node *
batadv_neigh_node_new(struct batadv_hard_iface * hard_iface,const uint8_t * neigh_addr,struct batadv_orig_node * orig_node)408 batadv_neigh_node_new(struct batadv_hard_iface *hard_iface,
409 const uint8_t *neigh_addr,
410 struct batadv_orig_node *orig_node)
411 {
412 struct batadv_neigh_node *neigh_node;
413
414 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
415 if (!neigh_node)
416 goto out;
417
418 INIT_HLIST_NODE(&neigh_node->list);
419 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
420 spin_lock_init(&neigh_node->ifinfo_lock);
421
422 ether_addr_copy(neigh_node->addr, neigh_addr);
423 neigh_node->if_incoming = hard_iface;
424 neigh_node->orig_node = orig_node;
425
426 /* extra reference for return */
427 atomic_set(&neigh_node->refcount, 2);
428
429 out:
430 return neigh_node;
431 }
432
433 /**
434 * batadv_neigh_node_get - retrieve a neighbour from the list
435 * @orig_node: originator which the neighbour belongs to
436 * @hard_iface: the interface where this neighbour is connected to
437 * @addr: the address of the neighbour
438 *
439 * Looks for and possibly returns a neighbour belonging to this originator list
440 * which is connected through the provided hard interface.
441 * Returns NULL if the neighbour is not found.
442 */
443 struct batadv_neigh_node *
batadv_neigh_node_get(const struct batadv_orig_node * orig_node,const struct batadv_hard_iface * hard_iface,const uint8_t * addr)444 batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
445 const struct batadv_hard_iface *hard_iface,
446 const uint8_t *addr)
447 {
448 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
449
450 rcu_read_lock();
451 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
452 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
453 continue;
454
455 if (tmp_neigh_node->if_incoming != hard_iface)
456 continue;
457
458 if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
459 continue;
460
461 res = tmp_neigh_node;
462 break;
463 }
464 rcu_read_unlock();
465
466 return res;
467 }
468
469 /**
470 * batadv_orig_ifinfo_release - release orig_ifinfo from lists and queue for
471 * free after rcu grace period
472 * @orig_ifinfo: the orig_ifinfo object to release
473 */
batadv_orig_ifinfo_release(struct batadv_orig_ifinfo * orig_ifinfo)474 static void batadv_orig_ifinfo_release(struct batadv_orig_ifinfo *orig_ifinfo)
475 {
476 struct batadv_neigh_node *router;
477
478 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
479 batadv_hardif_free_ref(orig_ifinfo->if_outgoing);
480
481 /* this is the last reference to this object */
482 router = rcu_dereference_protected(orig_ifinfo->router, true);
483 if (router)
484 batadv_neigh_node_free_ref(router);
485
486 kfree_rcu(orig_ifinfo, rcu);
487 }
488
489 /**
490 * batadv_orig_ifinfo_free_ref - decrement the refcounter and possibly release
491 * the orig_ifinfo
492 * @orig_ifinfo: the orig_ifinfo object to release
493 */
batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo * orig_ifinfo)494 void batadv_orig_ifinfo_free_ref(struct batadv_orig_ifinfo *orig_ifinfo)
495 {
496 if (atomic_dec_and_test(&orig_ifinfo->refcount))
497 batadv_orig_ifinfo_release(orig_ifinfo);
498 }
499
500 /**
501 * batadv_orig_node_free_rcu - free the orig_node
502 * @rcu: rcu pointer of the orig_node
503 */
batadv_orig_node_free_rcu(struct rcu_head * rcu)504 static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
505 {
506 struct batadv_orig_node *orig_node;
507
508 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
509
510 batadv_mcast_purge_orig(orig_node);
511
512 batadv_frag_purge_orig(orig_node, NULL);
513
514 if (orig_node->bat_priv->bat_algo_ops->bat_orig_free)
515 orig_node->bat_priv->bat_algo_ops->bat_orig_free(orig_node);
516
517 kfree(orig_node->tt_buff);
518 kfree(orig_node);
519 }
520
521 /**
522 * batadv_orig_node_release - release orig_node from lists and queue for
523 * free after rcu grace period
524 * @orig_node: the orig node to free
525 */
batadv_orig_node_release(struct batadv_orig_node * orig_node)526 static void batadv_orig_node_release(struct batadv_orig_node *orig_node)
527 {
528 struct hlist_node *node_tmp;
529 struct batadv_neigh_node *neigh_node;
530 struct batadv_orig_ifinfo *orig_ifinfo;
531
532 spin_lock_bh(&orig_node->neigh_list_lock);
533
534 /* for all neighbors towards this originator ... */
535 hlist_for_each_entry_safe(neigh_node, node_tmp,
536 &orig_node->neigh_list, list) {
537 hlist_del_rcu(&neigh_node->list);
538 batadv_neigh_node_free_ref(neigh_node);
539 }
540
541 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
542 &orig_node->ifinfo_list, list) {
543 hlist_del_rcu(&orig_ifinfo->list);
544 batadv_orig_ifinfo_free_ref(orig_ifinfo);
545 }
546 spin_unlock_bh(&orig_node->neigh_list_lock);
547
548 /* Free nc_nodes */
549 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
550
551 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
552 }
553
554 /**
555 * batadv_orig_node_free_ref - decrement the orig node refcounter and possibly
556 * release it
557 * @orig_node: the orig node to free
558 */
batadv_orig_node_free_ref(struct batadv_orig_node * orig_node)559 void batadv_orig_node_free_ref(struct batadv_orig_node *orig_node)
560 {
561 if (atomic_dec_and_test(&orig_node->refcount))
562 batadv_orig_node_release(orig_node);
563 }
564
batadv_originator_free(struct batadv_priv * bat_priv)565 void batadv_originator_free(struct batadv_priv *bat_priv)
566 {
567 struct batadv_hashtable *hash = bat_priv->orig_hash;
568 struct hlist_node *node_tmp;
569 struct hlist_head *head;
570 spinlock_t *list_lock; /* spinlock to protect write access */
571 struct batadv_orig_node *orig_node;
572 uint32_t i;
573
574 if (!hash)
575 return;
576
577 cancel_delayed_work_sync(&bat_priv->orig_work);
578
579 bat_priv->orig_hash = NULL;
580
581 for (i = 0; i < hash->size; i++) {
582 head = &hash->table[i];
583 list_lock = &hash->list_locks[i];
584
585 spin_lock_bh(list_lock);
586 hlist_for_each_entry_safe(orig_node, node_tmp,
587 head, hash_entry) {
588 hlist_del_rcu(&orig_node->hash_entry);
589 batadv_orig_node_free_ref(orig_node);
590 }
591 spin_unlock_bh(list_lock);
592 }
593
594 batadv_hash_destroy(hash);
595 }
596
597 /**
598 * batadv_orig_node_new - creates a new orig_node
599 * @bat_priv: the bat priv with all the soft interface information
600 * @addr: the mac address of the originator
601 *
602 * Creates a new originator object and initialise all the generic fields.
603 * The new object is not added to the originator list.
604 * Returns the newly created object or NULL on failure.
605 */
batadv_orig_node_new(struct batadv_priv * bat_priv,const uint8_t * addr)606 struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
607 const uint8_t *addr)
608 {
609 struct batadv_orig_node *orig_node;
610 struct batadv_orig_node_vlan *vlan;
611 unsigned long reset_time;
612 int i;
613
614 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
615 "Creating new originator: %pM\n", addr);
616
617 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
618 if (!orig_node)
619 return NULL;
620
621 INIT_HLIST_HEAD(&orig_node->neigh_list);
622 INIT_LIST_HEAD(&orig_node->vlan_list);
623 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
624 spin_lock_init(&orig_node->bcast_seqno_lock);
625 spin_lock_init(&orig_node->neigh_list_lock);
626 spin_lock_init(&orig_node->tt_buff_lock);
627 spin_lock_init(&orig_node->tt_lock);
628 spin_lock_init(&orig_node->vlan_list_lock);
629
630 batadv_nc_init_orig(orig_node);
631
632 /* extra reference for return */
633 atomic_set(&orig_node->refcount, 2);
634
635 orig_node->bat_priv = bat_priv;
636 ether_addr_copy(orig_node->orig, addr);
637 batadv_dat_init_orig_node_addr(orig_node);
638 atomic_set(&orig_node->last_ttvn, 0);
639 orig_node->tt_buff = NULL;
640 orig_node->tt_buff_len = 0;
641 orig_node->last_seen = jiffies;
642 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
643 orig_node->bcast_seqno_reset = reset_time;
644
645 #ifdef CONFIG_BATMAN_ADV_MCAST
646 orig_node->mcast_flags = BATADV_NO_FLAGS;
647 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
648 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
649 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
650 spin_lock_init(&orig_node->mcast_handler_lock);
651 #endif
652
653 /* create a vlan object for the "untagged" LAN */
654 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
655 if (!vlan)
656 goto free_orig_node;
657 /* batadv_orig_node_vlan_new() increases the refcounter.
658 * Immediately release vlan since it is not needed anymore in this
659 * context
660 */
661 batadv_orig_node_vlan_free_ref(vlan);
662
663 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
664 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
665 spin_lock_init(&orig_node->fragments[i].lock);
666 orig_node->fragments[i].size = 0;
667 }
668
669 return orig_node;
670 free_orig_node:
671 kfree(orig_node);
672 return NULL;
673 }
674
675 /**
676 * batadv_purge_neigh_ifinfo - purge obsolete ifinfo entries from neighbor
677 * @bat_priv: the bat priv with all the soft interface information
678 * @neigh: orig node which is to be checked
679 */
680 static void
batadv_purge_neigh_ifinfo(struct batadv_priv * bat_priv,struct batadv_neigh_node * neigh)681 batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
682 struct batadv_neigh_node *neigh)
683 {
684 struct batadv_neigh_ifinfo *neigh_ifinfo;
685 struct batadv_hard_iface *if_outgoing;
686 struct hlist_node *node_tmp;
687
688 spin_lock_bh(&neigh->ifinfo_lock);
689
690 /* for all ifinfo objects for this neighinator */
691 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
692 &neigh->ifinfo_list, list) {
693 if_outgoing = neigh_ifinfo->if_outgoing;
694
695 /* always keep the default interface */
696 if (if_outgoing == BATADV_IF_DEFAULT)
697 continue;
698
699 /* don't purge if the interface is not (going) down */
700 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
701 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
702 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
703 continue;
704
705 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
706 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
707 neigh->addr, if_outgoing->net_dev->name);
708
709 hlist_del_rcu(&neigh_ifinfo->list);
710 batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
711 }
712
713 spin_unlock_bh(&neigh->ifinfo_lock);
714 }
715
716 /**
717 * batadv_purge_orig_ifinfo - purge obsolete ifinfo entries from originator
718 * @bat_priv: the bat priv with all the soft interface information
719 * @orig_node: orig node which is to be checked
720 *
721 * Returns true if any ifinfo entry was purged, false otherwise.
722 */
723 static bool
batadv_purge_orig_ifinfo(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node)724 batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
725 struct batadv_orig_node *orig_node)
726 {
727 struct batadv_orig_ifinfo *orig_ifinfo;
728 struct batadv_hard_iface *if_outgoing;
729 struct hlist_node *node_tmp;
730 bool ifinfo_purged = false;
731
732 spin_lock_bh(&orig_node->neigh_list_lock);
733
734 /* for all ifinfo objects for this originator */
735 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
736 &orig_node->ifinfo_list, list) {
737 if_outgoing = orig_ifinfo->if_outgoing;
738
739 /* always keep the default interface */
740 if (if_outgoing == BATADV_IF_DEFAULT)
741 continue;
742
743 /* don't purge if the interface is not (going) down */
744 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
745 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
746 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
747 continue;
748
749 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
750 "router/ifinfo purge: originator %pM, iface: %s\n",
751 orig_node->orig, if_outgoing->net_dev->name);
752
753 ifinfo_purged = true;
754
755 hlist_del_rcu(&orig_ifinfo->list);
756 batadv_orig_ifinfo_free_ref(orig_ifinfo);
757 if (orig_node->last_bonding_candidate == orig_ifinfo) {
758 orig_node->last_bonding_candidate = NULL;
759 batadv_orig_ifinfo_free_ref(orig_ifinfo);
760 }
761 }
762
763 spin_unlock_bh(&orig_node->neigh_list_lock);
764
765 return ifinfo_purged;
766 }
767
768 /**
769 * batadv_purge_orig_neighbors - purges neighbors from originator
770 * @bat_priv: the bat priv with all the soft interface information
771 * @orig_node: orig node which is to be checked
772 *
773 * Returns true if any neighbor was purged, false otherwise
774 */
775 static bool
batadv_purge_orig_neighbors(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node)776 batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
777 struct batadv_orig_node *orig_node)
778 {
779 struct hlist_node *node_tmp;
780 struct batadv_neigh_node *neigh_node;
781 bool neigh_purged = false;
782 unsigned long last_seen;
783 struct batadv_hard_iface *if_incoming;
784
785 spin_lock_bh(&orig_node->neigh_list_lock);
786
787 /* for all neighbors towards this originator ... */
788 hlist_for_each_entry_safe(neigh_node, node_tmp,
789 &orig_node->neigh_list, list) {
790 last_seen = neigh_node->last_seen;
791 if_incoming = neigh_node->if_incoming;
792
793 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
794 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
795 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
796 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
797 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
798 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
799 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
800 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
801 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
802 orig_node->orig, neigh_node->addr,
803 if_incoming->net_dev->name);
804 else
805 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
806 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
807 orig_node->orig, neigh_node->addr,
808 jiffies_to_msecs(last_seen));
809
810 neigh_purged = true;
811
812 hlist_del_rcu(&neigh_node->list);
813 batadv_neigh_node_free_ref(neigh_node);
814 } else {
815 /* only necessary if not the whole neighbor is to be
816 * deleted, but some interface has been removed.
817 */
818 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
819 }
820 }
821
822 spin_unlock_bh(&orig_node->neigh_list_lock);
823 return neigh_purged;
824 }
825
826 /**
827 * batadv_find_best_neighbor - finds the best neighbor after purging
828 * @bat_priv: the bat priv with all the soft interface information
829 * @orig_node: orig node which is to be checked
830 * @if_outgoing: the interface for which the metric should be compared
831 *
832 * Returns the current best neighbor, with refcount increased.
833 */
834 static struct batadv_neigh_node *
batadv_find_best_neighbor(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node,struct batadv_hard_iface * if_outgoing)835 batadv_find_best_neighbor(struct batadv_priv *bat_priv,
836 struct batadv_orig_node *orig_node,
837 struct batadv_hard_iface *if_outgoing)
838 {
839 struct batadv_neigh_node *best = NULL, *neigh;
840 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
841
842 rcu_read_lock();
843 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
844 if (best && (bao->bat_neigh_cmp(neigh, if_outgoing,
845 best, if_outgoing) <= 0))
846 continue;
847
848 if (!atomic_inc_not_zero(&neigh->refcount))
849 continue;
850
851 if (best)
852 batadv_neigh_node_free_ref(best);
853
854 best = neigh;
855 }
856 rcu_read_unlock();
857
858 return best;
859 }
860
861 /**
862 * batadv_purge_orig_node - purges obsolete information from an orig_node
863 * @bat_priv: the bat priv with all the soft interface information
864 * @orig_node: orig node which is to be checked
865 *
866 * This function checks if the orig_node or substructures of it have become
867 * obsolete, and purges this information if that's the case.
868 *
869 * Returns true if the orig_node is to be removed, false otherwise.
870 */
batadv_purge_orig_node(struct batadv_priv * bat_priv,struct batadv_orig_node * orig_node)871 static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
872 struct batadv_orig_node *orig_node)
873 {
874 struct batadv_neigh_node *best_neigh_node;
875 struct batadv_hard_iface *hard_iface;
876 bool changed_ifinfo, changed_neigh;
877
878 if (batadv_has_timed_out(orig_node->last_seen,
879 2 * BATADV_PURGE_TIMEOUT)) {
880 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
881 "Originator timeout: originator %pM, last_seen %u\n",
882 orig_node->orig,
883 jiffies_to_msecs(orig_node->last_seen));
884 return true;
885 }
886 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
887 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
888
889 if (!changed_ifinfo && !changed_neigh)
890 return false;
891
892 /* first for NULL ... */
893 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
894 BATADV_IF_DEFAULT);
895 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
896 best_neigh_node);
897 if (best_neigh_node)
898 batadv_neigh_node_free_ref(best_neigh_node);
899
900 /* ... then for all other interfaces. */
901 rcu_read_lock();
902 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
903 if (hard_iface->if_status != BATADV_IF_ACTIVE)
904 continue;
905
906 if (hard_iface->soft_iface != bat_priv->soft_iface)
907 continue;
908
909 best_neigh_node = batadv_find_best_neighbor(bat_priv,
910 orig_node,
911 hard_iface);
912 batadv_update_route(bat_priv, orig_node, hard_iface,
913 best_neigh_node);
914 if (best_neigh_node)
915 batadv_neigh_node_free_ref(best_neigh_node);
916 }
917 rcu_read_unlock();
918
919 return false;
920 }
921
_batadv_purge_orig(struct batadv_priv * bat_priv)922 static void _batadv_purge_orig(struct batadv_priv *bat_priv)
923 {
924 struct batadv_hashtable *hash = bat_priv->orig_hash;
925 struct hlist_node *node_tmp;
926 struct hlist_head *head;
927 spinlock_t *list_lock; /* spinlock to protect write access */
928 struct batadv_orig_node *orig_node;
929 uint32_t i;
930
931 if (!hash)
932 return;
933
934 /* for all origins... */
935 for (i = 0; i < hash->size; i++) {
936 head = &hash->table[i];
937 list_lock = &hash->list_locks[i];
938
939 spin_lock_bh(list_lock);
940 hlist_for_each_entry_safe(orig_node, node_tmp,
941 head, hash_entry) {
942 if (batadv_purge_orig_node(bat_priv, orig_node)) {
943 batadv_gw_node_delete(bat_priv, orig_node);
944 hlist_del_rcu(&orig_node->hash_entry);
945 batadv_tt_global_del_orig(orig_node->bat_priv,
946 orig_node, -1,
947 "originator timed out");
948 batadv_orig_node_free_ref(orig_node);
949 continue;
950 }
951
952 batadv_frag_purge_orig(orig_node,
953 batadv_frag_check_entry);
954 }
955 spin_unlock_bh(list_lock);
956 }
957
958 batadv_gw_node_purge(bat_priv);
959 batadv_gw_election(bat_priv);
960 }
961
batadv_purge_orig(struct work_struct * work)962 static void batadv_purge_orig(struct work_struct *work)
963 {
964 struct delayed_work *delayed_work;
965 struct batadv_priv *bat_priv;
966
967 delayed_work = container_of(work, struct delayed_work, work);
968 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
969 _batadv_purge_orig(bat_priv);
970 queue_delayed_work(batadv_event_workqueue,
971 &bat_priv->orig_work,
972 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
973 }
974
batadv_purge_orig_ref(struct batadv_priv * bat_priv)975 void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
976 {
977 _batadv_purge_orig(bat_priv);
978 }
979
batadv_orig_seq_print_text(struct seq_file * seq,void * offset)980 int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
981 {
982 struct net_device *net_dev = (struct net_device *)seq->private;
983 struct batadv_priv *bat_priv = netdev_priv(net_dev);
984 struct batadv_hard_iface *primary_if;
985
986 primary_if = batadv_seq_print_text_primary_if_get(seq);
987 if (!primary_if)
988 return 0;
989
990 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
991 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
992 primary_if->net_dev->dev_addr, net_dev->name,
993 bat_priv->bat_algo_ops->name);
994
995 batadv_hardif_free_ref(primary_if);
996
997 if (!bat_priv->bat_algo_ops->bat_orig_print) {
998 seq_puts(seq,
999 "No printing function for this routing protocol\n");
1000 return 0;
1001 }
1002
1003 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq,
1004 BATADV_IF_DEFAULT);
1005
1006 return 0;
1007 }
1008
1009 /**
1010 * batadv_orig_hardif_seq_print_text - writes originator infos for a specific
1011 * outgoing interface
1012 * @seq: debugfs table seq_file struct
1013 * @offset: not used
1014 *
1015 * Returns 0
1016 */
batadv_orig_hardif_seq_print_text(struct seq_file * seq,void * offset)1017 int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1018 {
1019 struct net_device *net_dev = (struct net_device *)seq->private;
1020 struct batadv_hard_iface *hard_iface;
1021 struct batadv_priv *bat_priv;
1022
1023 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1024
1025 if (!hard_iface || !hard_iface->soft_iface) {
1026 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1027 goto out;
1028 }
1029
1030 bat_priv = netdev_priv(hard_iface->soft_iface);
1031 if (!bat_priv->bat_algo_ops->bat_orig_print) {
1032 seq_puts(seq,
1033 "No printing function for this routing protocol\n");
1034 goto out;
1035 }
1036
1037 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1038 seq_puts(seq, "Interface not active\n");
1039 goto out;
1040 }
1041
1042 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1043 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1044 hard_iface->net_dev->dev_addr,
1045 hard_iface->soft_iface->name, bat_priv->bat_algo_ops->name);
1046
1047 bat_priv->bat_algo_ops->bat_orig_print(bat_priv, seq, hard_iface);
1048
1049 out:
1050 if (hard_iface)
1051 batadv_hardif_free_ref(hard_iface);
1052 return 0;
1053 }
1054
batadv_orig_hash_add_if(struct batadv_hard_iface * hard_iface,int max_if_num)1055 int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1056 int max_if_num)
1057 {
1058 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1059 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1060 struct batadv_hashtable *hash = bat_priv->orig_hash;
1061 struct hlist_head *head;
1062 struct batadv_orig_node *orig_node;
1063 uint32_t i;
1064 int ret;
1065
1066 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1067 * if_num
1068 */
1069 for (i = 0; i < hash->size; i++) {
1070 head = &hash->table[i];
1071
1072 rcu_read_lock();
1073 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1074 ret = 0;
1075 if (bao->bat_orig_add_if)
1076 ret = bao->bat_orig_add_if(orig_node,
1077 max_if_num);
1078 if (ret == -ENOMEM)
1079 goto err;
1080 }
1081 rcu_read_unlock();
1082 }
1083
1084 return 0;
1085
1086 err:
1087 rcu_read_unlock();
1088 return -ENOMEM;
1089 }
1090
batadv_orig_hash_del_if(struct batadv_hard_iface * hard_iface,int max_if_num)1091 int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1092 int max_if_num)
1093 {
1094 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1095 struct batadv_hashtable *hash = bat_priv->orig_hash;
1096 struct hlist_head *head;
1097 struct batadv_hard_iface *hard_iface_tmp;
1098 struct batadv_orig_node *orig_node;
1099 struct batadv_algo_ops *bao = bat_priv->bat_algo_ops;
1100 uint32_t i;
1101 int ret;
1102
1103 /* resize all orig nodes because orig_node->bcast_own(_sum) depend on
1104 * if_num
1105 */
1106 for (i = 0; i < hash->size; i++) {
1107 head = &hash->table[i];
1108
1109 rcu_read_lock();
1110 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1111 ret = 0;
1112 if (bao->bat_orig_del_if)
1113 ret = bao->bat_orig_del_if(orig_node,
1114 max_if_num,
1115 hard_iface->if_num);
1116 if (ret == -ENOMEM)
1117 goto err;
1118 }
1119 rcu_read_unlock();
1120 }
1121
1122 /* renumber remaining batman interfaces _inside_ of orig_hash_lock */
1123 rcu_read_lock();
1124 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1125 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1126 continue;
1127
1128 if (hard_iface == hard_iface_tmp)
1129 continue;
1130
1131 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1132 continue;
1133
1134 if (hard_iface_tmp->if_num > hard_iface->if_num)
1135 hard_iface_tmp->if_num--;
1136 }
1137 rcu_read_unlock();
1138
1139 hard_iface->if_num = -1;
1140 return 0;
1141
1142 err:
1143 rcu_read_unlock();
1144 return -ENOMEM;
1145 }
1146