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