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