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