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