1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "originator.h"
19#include "main.h"
20
21#include <linux/atomic.h>
22#include <linux/errno.h>
23#include <linux/etherdevice.h>
24#include <linux/fs.h>
25#include <linux/jiffies.h>
26#include <linux/kernel.h>
27#include <linux/kref.h>
28#include <linux/list.h>
29#include <linux/lockdep.h>
30#include <linux/netdevice.h>
31#include <linux/netlink.h>
32#include <linux/rculist.h>
33#include <linux/seq_file.h>
34#include <linux/skbuff.h>
35#include <linux/slab.h>
36#include <linux/spinlock.h>
37#include <linux/workqueue.h>
38#include <net/sock.h>
39#include <uapi/linux/batman_adv.h>
40
41#include "bat_algo.h"
42#include "distributed-arp-table.h"
43#include "fragmentation.h"
44#include "gateway_client.h"
45#include "hard-interface.h"
46#include "hash.h"
47#include "log.h"
48#include "multicast.h"
49#include "netlink.h"
50#include "network-coding.h"
51#include "routing.h"
52#include "soft-interface.h"
53#include "translation-table.h"
54
55
56static struct lock_class_key batadv_orig_hash_lock_class_key;
57
58static void batadv_purge_orig(struct work_struct *work);
59
60
61
62
63
64
65
66
67bool batadv_compare_orig(const struct hlist_node *node, const void *data2)
68{
69 const void *data1 = container_of(node, struct batadv_orig_node,
70 hash_entry);
71
72 return batadv_compare_eth(data1, data2);
73}
74
75
76
77
78
79
80
81
82
83struct batadv_orig_node_vlan *
84batadv_orig_node_vlan_get(struct batadv_orig_node *orig_node,
85 unsigned short vid)
86{
87 struct batadv_orig_node_vlan *vlan = NULL, *tmp;
88
89 rcu_read_lock();
90 hlist_for_each_entry_rcu(tmp, &orig_node->vlan_list, list) {
91 if (tmp->vid != vid)
92 continue;
93
94 if (!kref_get_unless_zero(&tmp->refcount))
95 continue;
96
97 vlan = tmp;
98
99 break;
100 }
101 rcu_read_unlock();
102
103 return vlan;
104}
105
106
107
108
109
110
111
112
113
114
115
116
117
118struct batadv_orig_node_vlan *
119batadv_orig_node_vlan_new(struct batadv_orig_node *orig_node,
120 unsigned short vid)
121{
122 struct batadv_orig_node_vlan *vlan;
123
124 spin_lock_bh(&orig_node->vlan_list_lock);
125
126
127 vlan = batadv_orig_node_vlan_get(orig_node, vid);
128 if (vlan)
129 goto out;
130
131 vlan = kzalloc(sizeof(*vlan), GFP_ATOMIC);
132 if (!vlan)
133 goto out;
134
135 kref_init(&vlan->refcount);
136 vlan->vid = vid;
137
138 kref_get(&vlan->refcount);
139 hlist_add_head_rcu(&vlan->list, &orig_node->vlan_list);
140
141out:
142 spin_unlock_bh(&orig_node->vlan_list_lock);
143
144 return vlan;
145}
146
147
148
149
150
151
152static void batadv_orig_node_vlan_release(struct kref *ref)
153{
154 struct batadv_orig_node_vlan *orig_vlan;
155
156 orig_vlan = container_of(ref, struct batadv_orig_node_vlan, refcount);
157
158 kfree_rcu(orig_vlan, rcu);
159}
160
161
162
163
164
165
166void batadv_orig_node_vlan_put(struct batadv_orig_node_vlan *orig_vlan)
167{
168 kref_put(&orig_vlan->refcount, batadv_orig_node_vlan_release);
169}
170
171int batadv_originator_init(struct batadv_priv *bat_priv)
172{
173 if (bat_priv->orig_hash)
174 return 0;
175
176 bat_priv->orig_hash = batadv_hash_new(1024);
177
178 if (!bat_priv->orig_hash)
179 goto err;
180
181 batadv_hash_set_lock_class(bat_priv->orig_hash,
182 &batadv_orig_hash_lock_class_key);
183
184 INIT_DELAYED_WORK(&bat_priv->orig_work, batadv_purge_orig);
185 queue_delayed_work(batadv_event_workqueue,
186 &bat_priv->orig_work,
187 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
188
189 return 0;
190
191err:
192 return -ENOMEM;
193}
194
195
196
197
198
199
200static void batadv_neigh_ifinfo_release(struct kref *ref)
201{
202 struct batadv_neigh_ifinfo *neigh_ifinfo;
203
204 neigh_ifinfo = container_of(ref, struct batadv_neigh_ifinfo, refcount);
205
206 if (neigh_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
207 batadv_hardif_put(neigh_ifinfo->if_outgoing);
208
209 kfree_rcu(neigh_ifinfo, rcu);
210}
211
212
213
214
215
216
217void batadv_neigh_ifinfo_put(struct batadv_neigh_ifinfo *neigh_ifinfo)
218{
219 kref_put(&neigh_ifinfo->refcount, batadv_neigh_ifinfo_release);
220}
221
222
223
224
225
226
227static void batadv_hardif_neigh_release(struct kref *ref)
228{
229 struct batadv_hardif_neigh_node *hardif_neigh;
230
231 hardif_neigh = container_of(ref, struct batadv_hardif_neigh_node,
232 refcount);
233
234 spin_lock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
235 hlist_del_init_rcu(&hardif_neigh->list);
236 spin_unlock_bh(&hardif_neigh->if_incoming->neigh_list_lock);
237
238 batadv_hardif_put(hardif_neigh->if_incoming);
239 kfree_rcu(hardif_neigh, rcu);
240}
241
242
243
244
245
246
247void batadv_hardif_neigh_put(struct batadv_hardif_neigh_node *hardif_neigh)
248{
249 kref_put(&hardif_neigh->refcount, batadv_hardif_neigh_release);
250}
251
252
253
254
255
256
257static void batadv_neigh_node_release(struct kref *ref)
258{
259 struct hlist_node *node_tmp;
260 struct batadv_neigh_node *neigh_node;
261 struct batadv_neigh_ifinfo *neigh_ifinfo;
262
263 neigh_node = container_of(ref, struct batadv_neigh_node, refcount);
264
265 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
266 &neigh_node->ifinfo_list, list) {
267 batadv_neigh_ifinfo_put(neigh_ifinfo);
268 }
269
270 batadv_hardif_neigh_put(neigh_node->hardif_neigh);
271
272 batadv_hardif_put(neigh_node->if_incoming);
273
274 kfree_rcu(neigh_node, rcu);
275}
276
277
278
279
280
281
282void batadv_neigh_node_put(struct batadv_neigh_node *neigh_node)
283{
284 kref_put(&neigh_node->refcount, batadv_neigh_node_release);
285}
286
287
288
289
290
291
292
293
294
295
296
297struct batadv_neigh_node *
298batadv_orig_router_get(struct batadv_orig_node *orig_node,
299 const struct batadv_hard_iface *if_outgoing)
300{
301 struct batadv_orig_ifinfo *orig_ifinfo;
302 struct batadv_neigh_node *router = NULL;
303
304 rcu_read_lock();
305 hlist_for_each_entry_rcu(orig_ifinfo, &orig_node->ifinfo_list, list) {
306 if (orig_ifinfo->if_outgoing != if_outgoing)
307 continue;
308
309 router = rcu_dereference(orig_ifinfo->router);
310 break;
311 }
312
313 if (router && !kref_get_unless_zero(&router->refcount))
314 router = NULL;
315
316 rcu_read_unlock();
317 return router;
318}
319
320
321
322
323
324
325
326
327
328
329struct batadv_orig_ifinfo *
330batadv_orig_ifinfo_get(struct batadv_orig_node *orig_node,
331 struct batadv_hard_iface *if_outgoing)
332{
333 struct batadv_orig_ifinfo *tmp, *orig_ifinfo = NULL;
334
335 rcu_read_lock();
336 hlist_for_each_entry_rcu(tmp, &orig_node->ifinfo_list,
337 list) {
338 if (tmp->if_outgoing != if_outgoing)
339 continue;
340
341 if (!kref_get_unless_zero(&tmp->refcount))
342 continue;
343
344 orig_ifinfo = tmp;
345 break;
346 }
347 rcu_read_unlock();
348
349 return orig_ifinfo;
350}
351
352
353
354
355
356
357
358
359
360
361
362
363struct batadv_orig_ifinfo *
364batadv_orig_ifinfo_new(struct batadv_orig_node *orig_node,
365 struct batadv_hard_iface *if_outgoing)
366{
367 struct batadv_orig_ifinfo *orig_ifinfo;
368 unsigned long reset_time;
369
370 spin_lock_bh(&orig_node->neigh_list_lock);
371
372 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, if_outgoing);
373 if (orig_ifinfo)
374 goto out;
375
376 orig_ifinfo = kzalloc(sizeof(*orig_ifinfo), GFP_ATOMIC);
377 if (!orig_ifinfo)
378 goto out;
379
380 if (if_outgoing != BATADV_IF_DEFAULT)
381 kref_get(&if_outgoing->refcount);
382
383 reset_time = jiffies - 1;
384 reset_time -= msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
385 orig_ifinfo->batman_seqno_reset = reset_time;
386 orig_ifinfo->if_outgoing = if_outgoing;
387 INIT_HLIST_NODE(&orig_ifinfo->list);
388 kref_init(&orig_ifinfo->refcount);
389
390 kref_get(&orig_ifinfo->refcount);
391 hlist_add_head_rcu(&orig_ifinfo->list,
392 &orig_node->ifinfo_list);
393out:
394 spin_unlock_bh(&orig_node->neigh_list_lock);
395 return orig_ifinfo;
396}
397
398
399
400
401
402
403
404
405
406
407struct batadv_neigh_ifinfo *
408batadv_neigh_ifinfo_get(struct batadv_neigh_node *neigh,
409 struct batadv_hard_iface *if_outgoing)
410{
411 struct batadv_neigh_ifinfo *neigh_ifinfo = NULL,
412 *tmp_neigh_ifinfo;
413
414 rcu_read_lock();
415 hlist_for_each_entry_rcu(tmp_neigh_ifinfo, &neigh->ifinfo_list,
416 list) {
417 if (tmp_neigh_ifinfo->if_outgoing != if_outgoing)
418 continue;
419
420 if (!kref_get_unless_zero(&tmp_neigh_ifinfo->refcount))
421 continue;
422
423 neigh_ifinfo = tmp_neigh_ifinfo;
424 break;
425 }
426 rcu_read_unlock();
427
428 return neigh_ifinfo;
429}
430
431
432
433
434
435
436
437
438
439
440
441
442struct batadv_neigh_ifinfo *
443batadv_neigh_ifinfo_new(struct batadv_neigh_node *neigh,
444 struct batadv_hard_iface *if_outgoing)
445{
446 struct batadv_neigh_ifinfo *neigh_ifinfo;
447
448 spin_lock_bh(&neigh->ifinfo_lock);
449
450 neigh_ifinfo = batadv_neigh_ifinfo_get(neigh, if_outgoing);
451 if (neigh_ifinfo)
452 goto out;
453
454 neigh_ifinfo = kzalloc(sizeof(*neigh_ifinfo), GFP_ATOMIC);
455 if (!neigh_ifinfo)
456 goto out;
457
458 if (if_outgoing)
459 kref_get(&if_outgoing->refcount);
460
461 INIT_HLIST_NODE(&neigh_ifinfo->list);
462 kref_init(&neigh_ifinfo->refcount);
463 neigh_ifinfo->if_outgoing = if_outgoing;
464
465 kref_get(&neigh_ifinfo->refcount);
466 hlist_add_head_rcu(&neigh_ifinfo->list, &neigh->ifinfo_list);
467
468out:
469 spin_unlock_bh(&neigh->ifinfo_lock);
470
471 return neigh_ifinfo;
472}
473
474
475
476
477
478
479
480
481
482
483
484
485static struct batadv_neigh_node *
486batadv_neigh_node_get(const struct batadv_orig_node *orig_node,
487 const struct batadv_hard_iface *hard_iface,
488 const u8 *addr)
489{
490 struct batadv_neigh_node *tmp_neigh_node, *res = NULL;
491
492 rcu_read_lock();
493 hlist_for_each_entry_rcu(tmp_neigh_node, &orig_node->neigh_list, list) {
494 if (!batadv_compare_eth(tmp_neigh_node->addr, addr))
495 continue;
496
497 if (tmp_neigh_node->if_incoming != hard_iface)
498 continue;
499
500 if (!kref_get_unless_zero(&tmp_neigh_node->refcount))
501 continue;
502
503 res = tmp_neigh_node;
504 break;
505 }
506 rcu_read_unlock();
507
508 return res;
509}
510
511
512
513
514
515
516
517
518
519static struct batadv_hardif_neigh_node *
520batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
521 const u8 *neigh_addr,
522 struct batadv_orig_node *orig_node)
523{
524 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
525 struct batadv_hardif_neigh_node *hardif_neigh;
526
527 spin_lock_bh(&hard_iface->neigh_list_lock);
528
529
530 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
531 if (hardif_neigh)
532 goto out;
533
534 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
535 if (!hardif_neigh)
536 goto out;
537
538 kref_get(&hard_iface->refcount);
539 INIT_HLIST_NODE(&hardif_neigh->list);
540 ether_addr_copy(hardif_neigh->addr, neigh_addr);
541 ether_addr_copy(hardif_neigh->orig, orig_node->orig);
542 hardif_neigh->if_incoming = hard_iface;
543 hardif_neigh->last_seen = jiffies;
544
545 kref_init(&hardif_neigh->refcount);
546
547 if (bat_priv->algo_ops->neigh.hardif_init)
548 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
549
550 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
551
552out:
553 spin_unlock_bh(&hard_iface->neigh_list_lock);
554 return hardif_neigh;
555}
556
557
558
559
560
561
562
563
564
565
566static struct batadv_hardif_neigh_node *
567batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
568 const u8 *neigh_addr,
569 struct batadv_orig_node *orig_node)
570{
571 struct batadv_hardif_neigh_node *hardif_neigh;
572
573
574 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
575 if (hardif_neigh)
576 return hardif_neigh;
577
578 return batadv_hardif_neigh_create(hard_iface, neigh_addr, orig_node);
579}
580
581
582
583
584
585
586
587
588
589
590struct batadv_hardif_neigh_node *
591batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
592 const u8 *neigh_addr)
593{
594 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
595
596 rcu_read_lock();
597 hlist_for_each_entry_rcu(tmp_hardif_neigh,
598 &hard_iface->neigh_list, list) {
599 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
600 continue;
601
602 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
603 continue;
604
605 hardif_neigh = tmp_hardif_neigh;
606 break;
607 }
608 rcu_read_unlock();
609
610 return hardif_neigh;
611}
612
613
614
615
616
617
618
619
620
621
622
623static struct batadv_neigh_node *
624batadv_neigh_node_create(struct batadv_orig_node *orig_node,
625 struct batadv_hard_iface *hard_iface,
626 const u8 *neigh_addr)
627{
628 struct batadv_neigh_node *neigh_node;
629 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
630
631 spin_lock_bh(&orig_node->neigh_list_lock);
632
633 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
634 if (neigh_node)
635 goto out;
636
637 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
638 neigh_addr, orig_node);
639 if (!hardif_neigh)
640 goto out;
641
642 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
643 if (!neigh_node)
644 goto out;
645
646 INIT_HLIST_NODE(&neigh_node->list);
647 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
648 spin_lock_init(&neigh_node->ifinfo_lock);
649
650 kref_get(&hard_iface->refcount);
651 ether_addr_copy(neigh_node->addr, neigh_addr);
652 neigh_node->if_incoming = hard_iface;
653 neigh_node->orig_node = orig_node;
654 neigh_node->last_seen = jiffies;
655
656
657 kref_get(&hardif_neigh->refcount);
658 neigh_node->hardif_neigh = hardif_neigh;
659
660
661 kref_init(&neigh_node->refcount);
662
663 kref_get(&neigh_node->refcount);
664 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
665
666 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
667 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
668 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
669
670out:
671 spin_unlock_bh(&orig_node->neigh_list_lock);
672
673 if (hardif_neigh)
674 batadv_hardif_neigh_put(hardif_neigh);
675 return neigh_node;
676}
677
678
679
680
681
682
683
684
685
686struct batadv_neigh_node *
687batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
688 struct batadv_hard_iface *hard_iface,
689 const u8 *neigh_addr)
690{
691 struct batadv_neigh_node *neigh_node;
692
693
694 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
695 if (neigh_node)
696 return neigh_node;
697
698 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
699}
700
701#ifdef CONFIG_BATMAN_ADV_DEBUGFS
702
703
704
705
706
707
708
709int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
710{
711 struct net_device *net_dev = (struct net_device *)seq->private;
712 struct batadv_priv *bat_priv = netdev_priv(net_dev);
713 struct batadv_hard_iface *primary_if;
714
715 primary_if = batadv_seq_print_text_primary_if_get(seq);
716 if (!primary_if)
717 return 0;
718
719 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
720 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
721 primary_if->net_dev->dev_addr, net_dev->name,
722 bat_priv->algo_ops->name);
723
724 batadv_hardif_put(primary_if);
725
726 if (!bat_priv->algo_ops->neigh.print) {
727 seq_puts(seq,
728 "No printing function for this routing protocol\n");
729 return 0;
730 }
731
732 bat_priv->algo_ops->neigh.print(bat_priv, seq);
733 return 0;
734}
735#endif
736
737
738
739
740
741
742
743
744
745int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
746{
747 struct net *net = sock_net(cb->skb->sk);
748 struct net_device *soft_iface;
749 struct net_device *hard_iface = NULL;
750 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
751 struct batadv_priv *bat_priv;
752 struct batadv_hard_iface *primary_if = NULL;
753 int ret;
754 int ifindex, hard_ifindex;
755
756 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
757 if (!ifindex)
758 return -EINVAL;
759
760 soft_iface = dev_get_by_index(net, ifindex);
761 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
762 ret = -ENODEV;
763 goto out;
764 }
765
766 bat_priv = netdev_priv(soft_iface);
767
768 primary_if = batadv_primary_if_get_selected(bat_priv);
769 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
770 ret = -ENOENT;
771 goto out;
772 }
773
774 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
775 BATADV_ATTR_HARD_IFINDEX);
776 if (hard_ifindex) {
777 hard_iface = dev_get_by_index(net, hard_ifindex);
778 if (hard_iface)
779 hardif = batadv_hardif_get_by_netdev(hard_iface);
780
781 if (!hardif) {
782 ret = -ENODEV;
783 goto out;
784 }
785
786 if (hardif->soft_iface != soft_iface) {
787 ret = -ENOENT;
788 goto out;
789 }
790 }
791
792 if (!bat_priv->algo_ops->neigh.dump) {
793 ret = -EOPNOTSUPP;
794 goto out;
795 }
796
797 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
798
799 ret = msg->len;
800
801 out:
802 if (hardif)
803 batadv_hardif_put(hardif);
804 if (hard_iface)
805 dev_put(hard_iface);
806 if (primary_if)
807 batadv_hardif_put(primary_if);
808 if (soft_iface)
809 dev_put(soft_iface);
810
811 return ret;
812}
813
814
815
816
817
818
819static void batadv_orig_ifinfo_release(struct kref *ref)
820{
821 struct batadv_orig_ifinfo *orig_ifinfo;
822 struct batadv_neigh_node *router;
823
824 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
825
826 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
827 batadv_hardif_put(orig_ifinfo->if_outgoing);
828
829
830 router = rcu_dereference_protected(orig_ifinfo->router, true);
831 if (router)
832 batadv_neigh_node_put(router);
833
834 kfree_rcu(orig_ifinfo, rcu);
835}
836
837
838
839
840
841
842void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
843{
844 kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
845}
846
847
848
849
850
851static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
852{
853 struct batadv_orig_node *orig_node;
854
855 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
856
857 batadv_mcast_purge_orig(orig_node);
858
859 batadv_frag_purge_orig(orig_node, NULL);
860
861 if (orig_node->bat_priv->algo_ops->orig.free)
862 orig_node->bat_priv->algo_ops->orig.free(orig_node);
863
864 kfree(orig_node->tt_buff);
865 kfree(orig_node);
866}
867
868
869
870
871
872
873static void batadv_orig_node_release(struct kref *ref)
874{
875 struct hlist_node *node_tmp;
876 struct batadv_neigh_node *neigh_node;
877 struct batadv_orig_node *orig_node;
878 struct batadv_orig_ifinfo *orig_ifinfo;
879 struct batadv_orig_node_vlan *vlan;
880 struct batadv_orig_ifinfo *last_candidate;
881
882 orig_node = container_of(ref, struct batadv_orig_node, refcount);
883
884 spin_lock_bh(&orig_node->neigh_list_lock);
885
886
887 hlist_for_each_entry_safe(neigh_node, node_tmp,
888 &orig_node->neigh_list, list) {
889 hlist_del_rcu(&neigh_node->list);
890 batadv_neigh_node_put(neigh_node);
891 }
892
893 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
894 &orig_node->ifinfo_list, list) {
895 hlist_del_rcu(&orig_ifinfo->list);
896 batadv_orig_ifinfo_put(orig_ifinfo);
897 }
898
899 last_candidate = orig_node->last_bonding_candidate;
900 orig_node->last_bonding_candidate = NULL;
901 spin_unlock_bh(&orig_node->neigh_list_lock);
902
903 if (last_candidate)
904 batadv_orig_ifinfo_put(last_candidate);
905
906 spin_lock_bh(&orig_node->vlan_list_lock);
907 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
908 hlist_del_rcu(&vlan->list);
909 batadv_orig_node_vlan_put(vlan);
910 }
911 spin_unlock_bh(&orig_node->vlan_list_lock);
912
913
914 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
915
916 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
917}
918
919
920
921
922
923
924void batadv_orig_node_put(struct batadv_orig_node *orig_node)
925{
926 kref_put(&orig_node->refcount, batadv_orig_node_release);
927}
928
929void batadv_originator_free(struct batadv_priv *bat_priv)
930{
931 struct batadv_hashtable *hash = bat_priv->orig_hash;
932 struct hlist_node *node_tmp;
933 struct hlist_head *head;
934 spinlock_t *list_lock;
935 struct batadv_orig_node *orig_node;
936 u32 i;
937
938 if (!hash)
939 return;
940
941 cancel_delayed_work_sync(&bat_priv->orig_work);
942
943 bat_priv->orig_hash = NULL;
944
945 for (i = 0; i < hash->size; i++) {
946 head = &hash->table[i];
947 list_lock = &hash->list_locks[i];
948
949 spin_lock_bh(list_lock);
950 hlist_for_each_entry_safe(orig_node, node_tmp,
951 head, hash_entry) {
952 hlist_del_rcu(&orig_node->hash_entry);
953 batadv_orig_node_put(orig_node);
954 }
955 spin_unlock_bh(list_lock);
956 }
957
958 batadv_hash_destroy(hash);
959}
960
961
962
963
964
965
966
967
968
969
970
971struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
972 const u8 *addr)
973{
974 struct batadv_orig_node *orig_node;
975 struct batadv_orig_node_vlan *vlan;
976 unsigned long reset_time;
977 int i;
978
979 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
980 "Creating new originator: %pM\n", addr);
981
982 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
983 if (!orig_node)
984 return NULL;
985
986 INIT_HLIST_HEAD(&orig_node->neigh_list);
987 INIT_HLIST_HEAD(&orig_node->vlan_list);
988 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
989 spin_lock_init(&orig_node->bcast_seqno_lock);
990 spin_lock_init(&orig_node->neigh_list_lock);
991 spin_lock_init(&orig_node->tt_buff_lock);
992 spin_lock_init(&orig_node->tt_lock);
993 spin_lock_init(&orig_node->vlan_list_lock);
994
995 batadv_nc_init_orig(orig_node);
996
997
998 kref_init(&orig_node->refcount);
999
1000 orig_node->bat_priv = bat_priv;
1001 ether_addr_copy(orig_node->orig, addr);
1002 batadv_dat_init_orig_node_addr(orig_node);
1003 atomic_set(&orig_node->last_ttvn, 0);
1004 orig_node->tt_buff = NULL;
1005 orig_node->tt_buff_len = 0;
1006 orig_node->last_seen = jiffies;
1007 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
1008 orig_node->bcast_seqno_reset = reset_time;
1009
1010#ifdef CONFIG_BATMAN_ADV_MCAST
1011 orig_node->mcast_flags = BATADV_NO_FLAGS;
1012 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
1013 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
1014 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
1015 spin_lock_init(&orig_node->mcast_handler_lock);
1016#endif
1017
1018
1019 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
1020 if (!vlan)
1021 goto free_orig_node;
1022
1023
1024
1025
1026 batadv_orig_node_vlan_put(vlan);
1027
1028 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
1029 INIT_HLIST_HEAD(&orig_node->fragments[i].fragment_list);
1030 spin_lock_init(&orig_node->fragments[i].lock);
1031 orig_node->fragments[i].size = 0;
1032 }
1033
1034 return orig_node;
1035free_orig_node:
1036 kfree(orig_node);
1037 return NULL;
1038}
1039
1040
1041
1042
1043
1044
1045static void
1046batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1047 struct batadv_neigh_node *neigh)
1048{
1049 struct batadv_neigh_ifinfo *neigh_ifinfo;
1050 struct batadv_hard_iface *if_outgoing;
1051 struct hlist_node *node_tmp;
1052
1053 spin_lock_bh(&neigh->ifinfo_lock);
1054
1055
1056 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1057 &neigh->ifinfo_list, list) {
1058 if_outgoing = neigh_ifinfo->if_outgoing;
1059
1060
1061 if (if_outgoing == BATADV_IF_DEFAULT)
1062 continue;
1063
1064
1065 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1066 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1067 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
1068 continue;
1069
1070 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1071 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1072 neigh->addr, if_outgoing->net_dev->name);
1073
1074 hlist_del_rcu(&neigh_ifinfo->list);
1075 batadv_neigh_ifinfo_put(neigh_ifinfo);
1076 }
1077
1078 spin_unlock_bh(&neigh->ifinfo_lock);
1079}
1080
1081
1082
1083
1084
1085
1086
1087
1088static bool
1089batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1090 struct batadv_orig_node *orig_node)
1091{
1092 struct batadv_orig_ifinfo *orig_ifinfo;
1093 struct batadv_hard_iface *if_outgoing;
1094 struct hlist_node *node_tmp;
1095 bool ifinfo_purged = false;
1096
1097 spin_lock_bh(&orig_node->neigh_list_lock);
1098
1099
1100 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1101 &orig_node->ifinfo_list, list) {
1102 if_outgoing = orig_ifinfo->if_outgoing;
1103
1104
1105 if (if_outgoing == BATADV_IF_DEFAULT)
1106 continue;
1107
1108
1109 if (if_outgoing->if_status != BATADV_IF_INACTIVE &&
1110 if_outgoing->if_status != BATADV_IF_NOT_IN_USE &&
1111 if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED)
1112 continue;
1113
1114 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1115 "router/ifinfo purge: originator %pM, iface: %s\n",
1116 orig_node->orig, if_outgoing->net_dev->name);
1117
1118 ifinfo_purged = true;
1119
1120 hlist_del_rcu(&orig_ifinfo->list);
1121 batadv_orig_ifinfo_put(orig_ifinfo);
1122 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1123 orig_node->last_bonding_candidate = NULL;
1124 batadv_orig_ifinfo_put(orig_ifinfo);
1125 }
1126 }
1127
1128 spin_unlock_bh(&orig_node->neigh_list_lock);
1129
1130 return ifinfo_purged;
1131}
1132
1133
1134
1135
1136
1137
1138
1139
1140static bool
1141batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
1142 struct batadv_orig_node *orig_node)
1143{
1144 struct hlist_node *node_tmp;
1145 struct batadv_neigh_node *neigh_node;
1146 bool neigh_purged = false;
1147 unsigned long last_seen;
1148 struct batadv_hard_iface *if_incoming;
1149
1150 spin_lock_bh(&orig_node->neigh_list_lock);
1151
1152
1153 hlist_for_each_entry_safe(neigh_node, node_tmp,
1154 &orig_node->neigh_list, list) {
1155 last_seen = neigh_node->last_seen;
1156 if_incoming = neigh_node->if_incoming;
1157
1158 if (batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT) ||
1159 if_incoming->if_status == BATADV_IF_INACTIVE ||
1160 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1161 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED) {
1162 if (if_incoming->if_status == BATADV_IF_INACTIVE ||
1163 if_incoming->if_status == BATADV_IF_NOT_IN_USE ||
1164 if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)
1165 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1166 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1167 orig_node->orig, neigh_node->addr,
1168 if_incoming->net_dev->name);
1169 else
1170 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1171 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1172 orig_node->orig, neigh_node->addr,
1173 jiffies_to_msecs(last_seen));
1174
1175 neigh_purged = true;
1176
1177 hlist_del_rcu(&neigh_node->list);
1178 batadv_neigh_node_put(neigh_node);
1179 } else {
1180
1181
1182
1183 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
1184 }
1185 }
1186
1187 spin_unlock_bh(&orig_node->neigh_list_lock);
1188 return neigh_purged;
1189}
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199static struct batadv_neigh_node *
1200batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1201 struct batadv_orig_node *orig_node,
1202 struct batadv_hard_iface *if_outgoing)
1203{
1204 struct batadv_neigh_node *best = NULL, *neigh;
1205 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1206
1207 rcu_read_lock();
1208 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1209 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1210 if_outgoing) <= 0))
1211 continue;
1212
1213 if (!kref_get_unless_zero(&neigh->refcount))
1214 continue;
1215
1216 if (best)
1217 batadv_neigh_node_put(best);
1218
1219 best = neigh;
1220 }
1221 rcu_read_unlock();
1222
1223 return best;
1224}
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1237 struct batadv_orig_node *orig_node)
1238{
1239 struct batadv_neigh_node *best_neigh_node;
1240 struct batadv_hard_iface *hard_iface;
1241 bool changed_ifinfo, changed_neigh;
1242
1243 if (batadv_has_timed_out(orig_node->last_seen,
1244 2 * BATADV_PURGE_TIMEOUT)) {
1245 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1246 "Originator timeout: originator %pM, last_seen %u\n",
1247 orig_node->orig,
1248 jiffies_to_msecs(orig_node->last_seen));
1249 return true;
1250 }
1251 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1252 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
1253
1254 if (!changed_ifinfo && !changed_neigh)
1255 return false;
1256
1257
1258 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1259 BATADV_IF_DEFAULT);
1260 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1261 best_neigh_node);
1262 if (best_neigh_node)
1263 batadv_neigh_node_put(best_neigh_node);
1264
1265
1266 rcu_read_lock();
1267 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1268 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1269 continue;
1270
1271 if (hard_iface->soft_iface != bat_priv->soft_iface)
1272 continue;
1273
1274 if (!kref_get_unless_zero(&hard_iface->refcount))
1275 continue;
1276
1277 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1278 orig_node,
1279 hard_iface);
1280 batadv_update_route(bat_priv, orig_node, hard_iface,
1281 best_neigh_node);
1282 if (best_neigh_node)
1283 batadv_neigh_node_put(best_neigh_node);
1284
1285 batadv_hardif_put(hard_iface);
1286 }
1287 rcu_read_unlock();
1288
1289 return false;
1290}
1291
1292static void _batadv_purge_orig(struct batadv_priv *bat_priv)
1293{
1294 struct batadv_hashtable *hash = bat_priv->orig_hash;
1295 struct hlist_node *node_tmp;
1296 struct hlist_head *head;
1297 spinlock_t *list_lock;
1298 struct batadv_orig_node *orig_node;
1299 u32 i;
1300
1301 if (!hash)
1302 return;
1303
1304
1305 for (i = 0; i < hash->size; i++) {
1306 head = &hash->table[i];
1307 list_lock = &hash->list_locks[i];
1308
1309 spin_lock_bh(list_lock);
1310 hlist_for_each_entry_safe(orig_node, node_tmp,
1311 head, hash_entry) {
1312 if (batadv_purge_orig_node(bat_priv, orig_node)) {
1313 batadv_gw_node_delete(bat_priv, orig_node);
1314 hlist_del_rcu(&orig_node->hash_entry);
1315 batadv_tt_global_del_orig(orig_node->bat_priv,
1316 orig_node, -1,
1317 "originator timed out");
1318 batadv_orig_node_put(orig_node);
1319 continue;
1320 }
1321
1322 batadv_frag_purge_orig(orig_node,
1323 batadv_frag_check_entry);
1324 }
1325 spin_unlock_bh(list_lock);
1326 }
1327
1328 batadv_gw_election(bat_priv);
1329}
1330
1331static void batadv_purge_orig(struct work_struct *work)
1332{
1333 struct delayed_work *delayed_work;
1334 struct batadv_priv *bat_priv;
1335
1336 delayed_work = to_delayed_work(work);
1337 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1338 _batadv_purge_orig(bat_priv);
1339 queue_delayed_work(batadv_event_workqueue,
1340 &bat_priv->orig_work,
1341 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1342}
1343
1344void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1345{
1346 _batadv_purge_orig(bat_priv);
1347}
1348
1349#ifdef CONFIG_BATMAN_ADV_DEBUGFS
1350int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1351{
1352 struct net_device *net_dev = (struct net_device *)seq->private;
1353 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1354 struct batadv_hard_iface *primary_if;
1355
1356 primary_if = batadv_seq_print_text_primary_if_get(seq);
1357 if (!primary_if)
1358 return 0;
1359
1360 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1361 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1362 primary_if->net_dev->dev_addr, net_dev->name,
1363 bat_priv->algo_ops->name);
1364
1365 batadv_hardif_put(primary_if);
1366
1367 if (!bat_priv->algo_ops->orig.print) {
1368 seq_puts(seq,
1369 "No printing function for this routing protocol\n");
1370 return 0;
1371 }
1372
1373 bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
1374
1375 return 0;
1376}
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1387{
1388 struct net_device *net_dev = (struct net_device *)seq->private;
1389 struct batadv_hard_iface *hard_iface;
1390 struct batadv_priv *bat_priv;
1391
1392 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1393
1394 if (!hard_iface || !hard_iface->soft_iface) {
1395 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1396 goto out;
1397 }
1398
1399 bat_priv = netdev_priv(hard_iface->soft_iface);
1400 if (!bat_priv->algo_ops->orig.print) {
1401 seq_puts(seq,
1402 "No printing function for this routing protocol\n");
1403 goto out;
1404 }
1405
1406 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1407 seq_puts(seq, "Interface not active\n");
1408 goto out;
1409 }
1410
1411 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1412 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1413 hard_iface->net_dev->dev_addr,
1414 hard_iface->soft_iface->name, bat_priv->algo_ops->name);
1415
1416 bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
1417
1418out:
1419 if (hard_iface)
1420 batadv_hardif_put(hard_iface);
1421 return 0;
1422}
1423#endif
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1434{
1435 struct net *net = sock_net(cb->skb->sk);
1436 struct net_device *soft_iface;
1437 struct net_device *hard_iface = NULL;
1438 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1439 struct batadv_priv *bat_priv;
1440 struct batadv_hard_iface *primary_if = NULL;
1441 int ret;
1442 int ifindex, hard_ifindex;
1443
1444 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1445 if (!ifindex)
1446 return -EINVAL;
1447
1448 soft_iface = dev_get_by_index(net, ifindex);
1449 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1450 ret = -ENODEV;
1451 goto out;
1452 }
1453
1454 bat_priv = netdev_priv(soft_iface);
1455
1456 primary_if = batadv_primary_if_get_selected(bat_priv);
1457 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1458 ret = -ENOENT;
1459 goto out;
1460 }
1461
1462 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1463 BATADV_ATTR_HARD_IFINDEX);
1464 if (hard_ifindex) {
1465 hard_iface = dev_get_by_index(net, hard_ifindex);
1466 if (hard_iface)
1467 hardif = batadv_hardif_get_by_netdev(hard_iface);
1468
1469 if (!hardif) {
1470 ret = -ENODEV;
1471 goto out;
1472 }
1473
1474 if (hardif->soft_iface != soft_iface) {
1475 ret = -ENOENT;
1476 goto out;
1477 }
1478 }
1479
1480 if (!bat_priv->algo_ops->orig.dump) {
1481 ret = -EOPNOTSUPP;
1482 goto out;
1483 }
1484
1485 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1486
1487 ret = msg->len;
1488
1489 out:
1490 if (hardif)
1491 batadv_hardif_put(hardif);
1492 if (hard_iface)
1493 dev_put(hard_iface);
1494 if (primary_if)
1495 batadv_hardif_put(primary_if);
1496 if (soft_iface)
1497 dev_put(soft_iface);
1498
1499 return ret;
1500}
1501
1502int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1503 int max_if_num)
1504{
1505 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1506 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1507 struct batadv_hashtable *hash = bat_priv->orig_hash;
1508 struct hlist_head *head;
1509 struct batadv_orig_node *orig_node;
1510 u32 i;
1511 int ret;
1512
1513
1514
1515
1516 for (i = 0; i < hash->size; i++) {
1517 head = &hash->table[i];
1518
1519 rcu_read_lock();
1520 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1521 ret = 0;
1522 if (bao->orig.add_if)
1523 ret = bao->orig.add_if(orig_node, max_if_num);
1524 if (ret == -ENOMEM)
1525 goto err;
1526 }
1527 rcu_read_unlock();
1528 }
1529
1530 return 0;
1531
1532err:
1533 rcu_read_unlock();
1534 return -ENOMEM;
1535}
1536
1537int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1538 int max_if_num)
1539{
1540 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1541 struct batadv_hashtable *hash = bat_priv->orig_hash;
1542 struct hlist_head *head;
1543 struct batadv_hard_iface *hard_iface_tmp;
1544 struct batadv_orig_node *orig_node;
1545 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1546 u32 i;
1547 int ret;
1548
1549
1550
1551
1552 for (i = 0; i < hash->size; i++) {
1553 head = &hash->table[i];
1554
1555 rcu_read_lock();
1556 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1557 ret = 0;
1558 if (bao->orig.del_if)
1559 ret = bao->orig.del_if(orig_node, max_if_num,
1560 hard_iface->if_num);
1561 if (ret == -ENOMEM)
1562 goto err;
1563 }
1564 rcu_read_unlock();
1565 }
1566
1567
1568 rcu_read_lock();
1569 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1570 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1571 continue;
1572
1573 if (hard_iface == hard_iface_tmp)
1574 continue;
1575
1576 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1577 continue;
1578
1579 if (hard_iface_tmp->if_num > hard_iface->if_num)
1580 hard_iface_tmp->if_num--;
1581 }
1582 rcu_read_unlock();
1583
1584 hard_iface->if_num = -1;
1585 return 0;
1586
1587err:
1588 rcu_read_unlock();
1589 return -ENOMEM;
1590}
1591