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