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 = NULL;
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
518static struct batadv_hardif_neigh_node *
519batadv_hardif_neigh_create(struct batadv_hard_iface *hard_iface,
520 const u8 *neigh_addr)
521{
522 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
523 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
524
525 spin_lock_bh(&hard_iface->neigh_list_lock);
526
527
528 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
529 if (hardif_neigh)
530 goto out;
531
532 hardif_neigh = kzalloc(sizeof(*hardif_neigh), GFP_ATOMIC);
533 if (!hardif_neigh)
534 goto out;
535
536 kref_get(&hard_iface->refcount);
537 INIT_HLIST_NODE(&hardif_neigh->list);
538 ether_addr_copy(hardif_neigh->addr, neigh_addr);
539 hardif_neigh->if_incoming = hard_iface;
540 hardif_neigh->last_seen = jiffies;
541
542 kref_init(&hardif_neigh->refcount);
543
544 if (bat_priv->algo_ops->neigh.hardif_init)
545 bat_priv->algo_ops->neigh.hardif_init(hardif_neigh);
546
547 hlist_add_head_rcu(&hardif_neigh->list, &hard_iface->neigh_list);
548
549out:
550 spin_unlock_bh(&hard_iface->neigh_list_lock);
551 return hardif_neigh;
552}
553
554
555
556
557
558
559
560
561
562static struct batadv_hardif_neigh_node *
563batadv_hardif_neigh_get_or_create(struct batadv_hard_iface *hard_iface,
564 const u8 *neigh_addr)
565{
566 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
567
568
569 hardif_neigh = batadv_hardif_neigh_get(hard_iface, neigh_addr);
570 if (hardif_neigh)
571 return hardif_neigh;
572
573 return batadv_hardif_neigh_create(hard_iface, neigh_addr);
574}
575
576
577
578
579
580
581
582
583
584
585struct batadv_hardif_neigh_node *
586batadv_hardif_neigh_get(const struct batadv_hard_iface *hard_iface,
587 const u8 *neigh_addr)
588{
589 struct batadv_hardif_neigh_node *tmp_hardif_neigh, *hardif_neigh = NULL;
590
591 rcu_read_lock();
592 hlist_for_each_entry_rcu(tmp_hardif_neigh,
593 &hard_iface->neigh_list, list) {
594 if (!batadv_compare_eth(tmp_hardif_neigh->addr, neigh_addr))
595 continue;
596
597 if (!kref_get_unless_zero(&tmp_hardif_neigh->refcount))
598 continue;
599
600 hardif_neigh = tmp_hardif_neigh;
601 break;
602 }
603 rcu_read_unlock();
604
605 return hardif_neigh;
606}
607
608
609
610
611
612
613
614
615
616
617
618static struct batadv_neigh_node *
619batadv_neigh_node_create(struct batadv_orig_node *orig_node,
620 struct batadv_hard_iface *hard_iface,
621 const u8 *neigh_addr)
622{
623 struct batadv_neigh_node *neigh_node;
624 struct batadv_hardif_neigh_node *hardif_neigh = NULL;
625
626 spin_lock_bh(&orig_node->neigh_list_lock);
627
628 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
629 if (neigh_node)
630 goto out;
631
632 hardif_neigh = batadv_hardif_neigh_get_or_create(hard_iface,
633 neigh_addr);
634 if (!hardif_neigh)
635 goto out;
636
637 neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
638 if (!neigh_node)
639 goto out;
640
641 INIT_HLIST_NODE(&neigh_node->list);
642 INIT_HLIST_HEAD(&neigh_node->ifinfo_list);
643 spin_lock_init(&neigh_node->ifinfo_lock);
644
645 kref_get(&hard_iface->refcount);
646 ether_addr_copy(neigh_node->addr, neigh_addr);
647 neigh_node->if_incoming = hard_iface;
648 neigh_node->orig_node = orig_node;
649 neigh_node->last_seen = jiffies;
650
651
652 kref_get(&hardif_neigh->refcount);
653 neigh_node->hardif_neigh = hardif_neigh;
654
655
656 kref_init(&neigh_node->refcount);
657
658 kref_get(&neigh_node->refcount);
659 hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
660
661 batadv_dbg(BATADV_DBG_BATMAN, orig_node->bat_priv,
662 "Creating new neighbor %pM for orig_node %pM on interface %s\n",
663 neigh_addr, orig_node->orig, hard_iface->net_dev->name);
664
665out:
666 spin_unlock_bh(&orig_node->neigh_list_lock);
667
668 if (hardif_neigh)
669 batadv_hardif_neigh_put(hardif_neigh);
670 return neigh_node;
671}
672
673
674
675
676
677
678
679
680
681struct batadv_neigh_node *
682batadv_neigh_node_get_or_create(struct batadv_orig_node *orig_node,
683 struct batadv_hard_iface *hard_iface,
684 const u8 *neigh_addr)
685{
686 struct batadv_neigh_node *neigh_node = NULL;
687
688
689 neigh_node = batadv_neigh_node_get(orig_node, hard_iface, neigh_addr);
690 if (neigh_node)
691 return neigh_node;
692
693 return batadv_neigh_node_create(orig_node, hard_iface, neigh_addr);
694}
695
696#ifdef CONFIG_BATMAN_ADV_DEBUGFS
697
698
699
700
701
702
703
704int batadv_hardif_neigh_seq_print_text(struct seq_file *seq, void *offset)
705{
706 struct net_device *net_dev = (struct net_device *)seq->private;
707 struct batadv_priv *bat_priv = netdev_priv(net_dev);
708 struct batadv_hard_iface *primary_if;
709
710 primary_if = batadv_seq_print_text_primary_if_get(seq);
711 if (!primary_if)
712 return 0;
713
714 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
715 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
716 primary_if->net_dev->dev_addr, net_dev->name,
717 bat_priv->algo_ops->name);
718
719 batadv_hardif_put(primary_if);
720
721 if (!bat_priv->algo_ops->neigh.print) {
722 seq_puts(seq,
723 "No printing function for this routing protocol\n");
724 return 0;
725 }
726
727 bat_priv->algo_ops->neigh.print(bat_priv, seq);
728 return 0;
729}
730#endif
731
732
733
734
735
736
737
738
739
740int batadv_hardif_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb)
741{
742 struct net *net = sock_net(cb->skb->sk);
743 struct net_device *soft_iface;
744 struct net_device *hard_iface = NULL;
745 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
746 struct batadv_priv *bat_priv;
747 struct batadv_hard_iface *primary_if = NULL;
748 int ret;
749 int ifindex, hard_ifindex;
750
751 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
752 if (!ifindex)
753 return -EINVAL;
754
755 soft_iface = dev_get_by_index(net, ifindex);
756 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
757 ret = -ENODEV;
758 goto out;
759 }
760
761 bat_priv = netdev_priv(soft_iface);
762
763 primary_if = batadv_primary_if_get_selected(bat_priv);
764 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
765 ret = -ENOENT;
766 goto out;
767 }
768
769 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
770 BATADV_ATTR_HARD_IFINDEX);
771 if (hard_ifindex) {
772 hard_iface = dev_get_by_index(net, hard_ifindex);
773 if (hard_iface)
774 hardif = batadv_hardif_get_by_netdev(hard_iface);
775
776 if (!hardif) {
777 ret = -ENODEV;
778 goto out;
779 }
780
781 if (hardif->soft_iface != soft_iface) {
782 ret = -ENOENT;
783 goto out;
784 }
785 }
786
787 if (!bat_priv->algo_ops->neigh.dump) {
788 ret = -EOPNOTSUPP;
789 goto out;
790 }
791
792 bat_priv->algo_ops->neigh.dump(msg, cb, bat_priv, hardif);
793
794 ret = msg->len;
795
796 out:
797 if (hardif)
798 batadv_hardif_put(hardif);
799 if (hard_iface)
800 dev_put(hard_iface);
801 if (primary_if)
802 batadv_hardif_put(primary_if);
803 if (soft_iface)
804 dev_put(soft_iface);
805
806 return ret;
807}
808
809
810
811
812
813
814static void batadv_orig_ifinfo_release(struct kref *ref)
815{
816 struct batadv_orig_ifinfo *orig_ifinfo;
817 struct batadv_neigh_node *router;
818
819 orig_ifinfo = container_of(ref, struct batadv_orig_ifinfo, refcount);
820
821 if (orig_ifinfo->if_outgoing != BATADV_IF_DEFAULT)
822 batadv_hardif_put(orig_ifinfo->if_outgoing);
823
824
825 router = rcu_dereference_protected(orig_ifinfo->router, true);
826 if (router)
827 batadv_neigh_node_put(router);
828
829 kfree_rcu(orig_ifinfo, rcu);
830}
831
832
833
834
835
836
837void batadv_orig_ifinfo_put(struct batadv_orig_ifinfo *orig_ifinfo)
838{
839 kref_put(&orig_ifinfo->refcount, batadv_orig_ifinfo_release);
840}
841
842
843
844
845
846static void batadv_orig_node_free_rcu(struct rcu_head *rcu)
847{
848 struct batadv_orig_node *orig_node;
849
850 orig_node = container_of(rcu, struct batadv_orig_node, rcu);
851
852 batadv_mcast_purge_orig(orig_node);
853
854 batadv_frag_purge_orig(orig_node, NULL);
855
856 if (orig_node->bat_priv->algo_ops->orig.free)
857 orig_node->bat_priv->algo_ops->orig.free(orig_node);
858
859 kfree(orig_node->tt_buff);
860 kfree(orig_node);
861}
862
863
864
865
866
867
868static void batadv_orig_node_release(struct kref *ref)
869{
870 struct hlist_node *node_tmp;
871 struct batadv_neigh_node *neigh_node;
872 struct batadv_orig_node *orig_node;
873 struct batadv_orig_ifinfo *orig_ifinfo;
874 struct batadv_orig_node_vlan *vlan;
875 struct batadv_orig_ifinfo *last_candidate;
876
877 orig_node = container_of(ref, struct batadv_orig_node, refcount);
878
879 spin_lock_bh(&orig_node->neigh_list_lock);
880
881
882 hlist_for_each_entry_safe(neigh_node, node_tmp,
883 &orig_node->neigh_list, list) {
884 hlist_del_rcu(&neigh_node->list);
885 batadv_neigh_node_put(neigh_node);
886 }
887
888 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
889 &orig_node->ifinfo_list, list) {
890 hlist_del_rcu(&orig_ifinfo->list);
891 batadv_orig_ifinfo_put(orig_ifinfo);
892 }
893
894 last_candidate = orig_node->last_bonding_candidate;
895 orig_node->last_bonding_candidate = NULL;
896 spin_unlock_bh(&orig_node->neigh_list_lock);
897
898 if (last_candidate)
899 batadv_orig_ifinfo_put(last_candidate);
900
901 spin_lock_bh(&orig_node->vlan_list_lock);
902 hlist_for_each_entry_safe(vlan, node_tmp, &orig_node->vlan_list, list) {
903 hlist_del_rcu(&vlan->list);
904 batadv_orig_node_vlan_put(vlan);
905 }
906 spin_unlock_bh(&orig_node->vlan_list_lock);
907
908
909 batadv_nc_purge_orig(orig_node->bat_priv, orig_node, NULL);
910
911 call_rcu(&orig_node->rcu, batadv_orig_node_free_rcu);
912}
913
914
915
916
917
918
919void batadv_orig_node_put(struct batadv_orig_node *orig_node)
920{
921 kref_put(&orig_node->refcount, batadv_orig_node_release);
922}
923
924void batadv_originator_free(struct batadv_priv *bat_priv)
925{
926 struct batadv_hashtable *hash = bat_priv->orig_hash;
927 struct hlist_node *node_tmp;
928 struct hlist_head *head;
929 spinlock_t *list_lock;
930 struct batadv_orig_node *orig_node;
931 u32 i;
932
933 if (!hash)
934 return;
935
936 cancel_delayed_work_sync(&bat_priv->orig_work);
937
938 bat_priv->orig_hash = NULL;
939
940 for (i = 0; i < hash->size; i++) {
941 head = &hash->table[i];
942 list_lock = &hash->list_locks[i];
943
944 spin_lock_bh(list_lock);
945 hlist_for_each_entry_safe(orig_node, node_tmp,
946 head, hash_entry) {
947 hlist_del_rcu(&orig_node->hash_entry);
948 batadv_orig_node_put(orig_node);
949 }
950 spin_unlock_bh(list_lock);
951 }
952
953 batadv_hash_destroy(hash);
954}
955
956
957
958
959
960
961
962
963
964
965
966struct batadv_orig_node *batadv_orig_node_new(struct batadv_priv *bat_priv,
967 const u8 *addr)
968{
969 struct batadv_orig_node *orig_node;
970 struct batadv_orig_node_vlan *vlan;
971 unsigned long reset_time;
972 int i;
973
974 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
975 "Creating new originator: %pM\n", addr);
976
977 orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
978 if (!orig_node)
979 return NULL;
980
981 INIT_HLIST_HEAD(&orig_node->neigh_list);
982 INIT_HLIST_HEAD(&orig_node->vlan_list);
983 INIT_HLIST_HEAD(&orig_node->ifinfo_list);
984 spin_lock_init(&orig_node->bcast_seqno_lock);
985 spin_lock_init(&orig_node->neigh_list_lock);
986 spin_lock_init(&orig_node->tt_buff_lock);
987 spin_lock_init(&orig_node->tt_lock);
988 spin_lock_init(&orig_node->vlan_list_lock);
989
990 batadv_nc_init_orig(orig_node);
991
992
993 kref_init(&orig_node->refcount);
994
995 orig_node->bat_priv = bat_priv;
996 ether_addr_copy(orig_node->orig, addr);
997 batadv_dat_init_orig_node_addr(orig_node);
998 atomic_set(&orig_node->last_ttvn, 0);
999 orig_node->tt_buff = NULL;
1000 orig_node->tt_buff_len = 0;
1001 orig_node->last_seen = jiffies;
1002 reset_time = jiffies - 1 - msecs_to_jiffies(BATADV_RESET_PROTECTION_MS);
1003 orig_node->bcast_seqno_reset = reset_time;
1004
1005#ifdef CONFIG_BATMAN_ADV_MCAST
1006 orig_node->mcast_flags = BATADV_NO_FLAGS;
1007 INIT_HLIST_NODE(&orig_node->mcast_want_all_unsnoopables_node);
1008 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv4_node);
1009 INIT_HLIST_NODE(&orig_node->mcast_want_all_ipv6_node);
1010 spin_lock_init(&orig_node->mcast_handler_lock);
1011#endif
1012
1013
1014 vlan = batadv_orig_node_vlan_new(orig_node, BATADV_NO_FLAGS);
1015 if (!vlan)
1016 goto free_orig_node;
1017
1018
1019
1020
1021 batadv_orig_node_vlan_put(vlan);
1022
1023 for (i = 0; i < BATADV_FRAG_BUFFER_COUNT; i++) {
1024 INIT_HLIST_HEAD(&orig_node->fragments[i].head);
1025 spin_lock_init(&orig_node->fragments[i].lock);
1026 orig_node->fragments[i].size = 0;
1027 }
1028
1029 return orig_node;
1030free_orig_node:
1031 kfree(orig_node);
1032 return NULL;
1033}
1034
1035
1036
1037
1038
1039
1040static void
1041batadv_purge_neigh_ifinfo(struct batadv_priv *bat_priv,
1042 struct batadv_neigh_node *neigh)
1043{
1044 struct batadv_neigh_ifinfo *neigh_ifinfo;
1045 struct batadv_hard_iface *if_outgoing;
1046 struct hlist_node *node_tmp;
1047
1048 spin_lock_bh(&neigh->ifinfo_lock);
1049
1050
1051 hlist_for_each_entry_safe(neigh_ifinfo, node_tmp,
1052 &neigh->ifinfo_list, list) {
1053 if_outgoing = neigh_ifinfo->if_outgoing;
1054
1055
1056 if (if_outgoing == BATADV_IF_DEFAULT)
1057 continue;
1058
1059
1060 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
1061 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
1062 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
1063 continue;
1064
1065 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1066 "neighbor/ifinfo purge: neighbor %pM, iface: %s\n",
1067 neigh->addr, if_outgoing->net_dev->name);
1068
1069 hlist_del_rcu(&neigh_ifinfo->list);
1070 batadv_neigh_ifinfo_put(neigh_ifinfo);
1071 }
1072
1073 spin_unlock_bh(&neigh->ifinfo_lock);
1074}
1075
1076
1077
1078
1079
1080
1081
1082
1083static bool
1084batadv_purge_orig_ifinfo(struct batadv_priv *bat_priv,
1085 struct batadv_orig_node *orig_node)
1086{
1087 struct batadv_orig_ifinfo *orig_ifinfo;
1088 struct batadv_hard_iface *if_outgoing;
1089 struct hlist_node *node_tmp;
1090 bool ifinfo_purged = false;
1091
1092 spin_lock_bh(&orig_node->neigh_list_lock);
1093
1094
1095 hlist_for_each_entry_safe(orig_ifinfo, node_tmp,
1096 &orig_node->ifinfo_list, list) {
1097 if_outgoing = orig_ifinfo->if_outgoing;
1098
1099
1100 if (if_outgoing == BATADV_IF_DEFAULT)
1101 continue;
1102
1103
1104 if ((if_outgoing->if_status != BATADV_IF_INACTIVE) &&
1105 (if_outgoing->if_status != BATADV_IF_NOT_IN_USE) &&
1106 (if_outgoing->if_status != BATADV_IF_TO_BE_REMOVED))
1107 continue;
1108
1109 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1110 "router/ifinfo purge: originator %pM, iface: %s\n",
1111 orig_node->orig, if_outgoing->net_dev->name);
1112
1113 ifinfo_purged = true;
1114
1115 hlist_del_rcu(&orig_ifinfo->list);
1116 batadv_orig_ifinfo_put(orig_ifinfo);
1117 if (orig_node->last_bonding_candidate == orig_ifinfo) {
1118 orig_node->last_bonding_candidate = NULL;
1119 batadv_orig_ifinfo_put(orig_ifinfo);
1120 }
1121 }
1122
1123 spin_unlock_bh(&orig_node->neigh_list_lock);
1124
1125 return ifinfo_purged;
1126}
1127
1128
1129
1130
1131
1132
1133
1134
1135static bool
1136batadv_purge_orig_neighbors(struct batadv_priv *bat_priv,
1137 struct batadv_orig_node *orig_node)
1138{
1139 struct hlist_node *node_tmp;
1140 struct batadv_neigh_node *neigh_node;
1141 bool neigh_purged = false;
1142 unsigned long last_seen;
1143 struct batadv_hard_iface *if_incoming;
1144
1145 spin_lock_bh(&orig_node->neigh_list_lock);
1146
1147
1148 hlist_for_each_entry_safe(neigh_node, node_tmp,
1149 &orig_node->neigh_list, list) {
1150 last_seen = neigh_node->last_seen;
1151 if_incoming = neigh_node->if_incoming;
1152
1153 if ((batadv_has_timed_out(last_seen, BATADV_PURGE_TIMEOUT)) ||
1154 (if_incoming->if_status == BATADV_IF_INACTIVE) ||
1155 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1156 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED)) {
1157 if ((if_incoming->if_status == BATADV_IF_INACTIVE) ||
1158 (if_incoming->if_status == BATADV_IF_NOT_IN_USE) ||
1159 (if_incoming->if_status == BATADV_IF_TO_BE_REMOVED))
1160 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1161 "neighbor purge: originator %pM, neighbor: %pM, iface: %s\n",
1162 orig_node->orig, neigh_node->addr,
1163 if_incoming->net_dev->name);
1164 else
1165 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1166 "neighbor timeout: originator %pM, neighbor: %pM, last_seen: %u\n",
1167 orig_node->orig, neigh_node->addr,
1168 jiffies_to_msecs(last_seen));
1169
1170 neigh_purged = true;
1171
1172 hlist_del_rcu(&neigh_node->list);
1173 batadv_neigh_node_put(neigh_node);
1174 } else {
1175
1176
1177
1178 batadv_purge_neigh_ifinfo(bat_priv, neigh_node);
1179 }
1180 }
1181
1182 spin_unlock_bh(&orig_node->neigh_list_lock);
1183 return neigh_purged;
1184}
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194static struct batadv_neigh_node *
1195batadv_find_best_neighbor(struct batadv_priv *bat_priv,
1196 struct batadv_orig_node *orig_node,
1197 struct batadv_hard_iface *if_outgoing)
1198{
1199 struct batadv_neigh_node *best = NULL, *neigh;
1200 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1201
1202 rcu_read_lock();
1203 hlist_for_each_entry_rcu(neigh, &orig_node->neigh_list, list) {
1204 if (best && (bao->neigh.cmp(neigh, if_outgoing, best,
1205 if_outgoing) <= 0))
1206 continue;
1207
1208 if (!kref_get_unless_zero(&neigh->refcount))
1209 continue;
1210
1211 if (best)
1212 batadv_neigh_node_put(best);
1213
1214 best = neigh;
1215 }
1216 rcu_read_unlock();
1217
1218 return best;
1219}
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231static bool batadv_purge_orig_node(struct batadv_priv *bat_priv,
1232 struct batadv_orig_node *orig_node)
1233{
1234 struct batadv_neigh_node *best_neigh_node;
1235 struct batadv_hard_iface *hard_iface;
1236 bool changed_ifinfo, changed_neigh;
1237
1238 if (batadv_has_timed_out(orig_node->last_seen,
1239 2 * BATADV_PURGE_TIMEOUT)) {
1240 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
1241 "Originator timeout: originator %pM, last_seen %u\n",
1242 orig_node->orig,
1243 jiffies_to_msecs(orig_node->last_seen));
1244 return true;
1245 }
1246 changed_ifinfo = batadv_purge_orig_ifinfo(bat_priv, orig_node);
1247 changed_neigh = batadv_purge_orig_neighbors(bat_priv, orig_node);
1248
1249 if (!changed_ifinfo && !changed_neigh)
1250 return false;
1251
1252
1253 best_neigh_node = batadv_find_best_neighbor(bat_priv, orig_node,
1254 BATADV_IF_DEFAULT);
1255 batadv_update_route(bat_priv, orig_node, BATADV_IF_DEFAULT,
1256 best_neigh_node);
1257 if (best_neigh_node)
1258 batadv_neigh_node_put(best_neigh_node);
1259
1260
1261 rcu_read_lock();
1262 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
1263 if (hard_iface->if_status != BATADV_IF_ACTIVE)
1264 continue;
1265
1266 if (hard_iface->soft_iface != bat_priv->soft_iface)
1267 continue;
1268
1269 if (!kref_get_unless_zero(&hard_iface->refcount))
1270 continue;
1271
1272 best_neigh_node = batadv_find_best_neighbor(bat_priv,
1273 orig_node,
1274 hard_iface);
1275 batadv_update_route(bat_priv, orig_node, hard_iface,
1276 best_neigh_node);
1277 if (best_neigh_node)
1278 batadv_neigh_node_put(best_neigh_node);
1279
1280 batadv_hardif_put(hard_iface);
1281 }
1282 rcu_read_unlock();
1283
1284 return false;
1285}
1286
1287static void _batadv_purge_orig(struct batadv_priv *bat_priv)
1288{
1289 struct batadv_hashtable *hash = bat_priv->orig_hash;
1290 struct hlist_node *node_tmp;
1291 struct hlist_head *head;
1292 spinlock_t *list_lock;
1293 struct batadv_orig_node *orig_node;
1294 u32 i;
1295
1296 if (!hash)
1297 return;
1298
1299
1300 for (i = 0; i < hash->size; i++) {
1301 head = &hash->table[i];
1302 list_lock = &hash->list_locks[i];
1303
1304 spin_lock_bh(list_lock);
1305 hlist_for_each_entry_safe(orig_node, node_tmp,
1306 head, hash_entry) {
1307 if (batadv_purge_orig_node(bat_priv, orig_node)) {
1308 batadv_gw_node_delete(bat_priv, orig_node);
1309 hlist_del_rcu(&orig_node->hash_entry);
1310 batadv_tt_global_del_orig(orig_node->bat_priv,
1311 orig_node, -1,
1312 "originator timed out");
1313 batadv_orig_node_put(orig_node);
1314 continue;
1315 }
1316
1317 batadv_frag_purge_orig(orig_node,
1318 batadv_frag_check_entry);
1319 }
1320 spin_unlock_bh(list_lock);
1321 }
1322
1323 batadv_gw_election(bat_priv);
1324}
1325
1326static void batadv_purge_orig(struct work_struct *work)
1327{
1328 struct delayed_work *delayed_work;
1329 struct batadv_priv *bat_priv;
1330
1331 delayed_work = to_delayed_work(work);
1332 bat_priv = container_of(delayed_work, struct batadv_priv, orig_work);
1333 _batadv_purge_orig(bat_priv);
1334 queue_delayed_work(batadv_event_workqueue,
1335 &bat_priv->orig_work,
1336 msecs_to_jiffies(BATADV_ORIG_WORK_PERIOD));
1337}
1338
1339void batadv_purge_orig_ref(struct batadv_priv *bat_priv)
1340{
1341 _batadv_purge_orig(bat_priv);
1342}
1343
1344#ifdef CONFIG_BATMAN_ADV_DEBUGFS
1345int batadv_orig_seq_print_text(struct seq_file *seq, void *offset)
1346{
1347 struct net_device *net_dev = (struct net_device *)seq->private;
1348 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1349 struct batadv_hard_iface *primary_if;
1350
1351 primary_if = batadv_seq_print_text_primary_if_get(seq);
1352 if (!primary_if)
1353 return 0;
1354
1355 seq_printf(seq, "[B.A.T.M.A.N. adv %s, MainIF/MAC: %s/%pM (%s %s)]\n",
1356 BATADV_SOURCE_VERSION, primary_if->net_dev->name,
1357 primary_if->net_dev->dev_addr, net_dev->name,
1358 bat_priv->algo_ops->name);
1359
1360 batadv_hardif_put(primary_if);
1361
1362 if (!bat_priv->algo_ops->orig.print) {
1363 seq_puts(seq,
1364 "No printing function for this routing protocol\n");
1365 return 0;
1366 }
1367
1368 bat_priv->algo_ops->orig.print(bat_priv, seq, BATADV_IF_DEFAULT);
1369
1370 return 0;
1371}
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381int batadv_orig_hardif_seq_print_text(struct seq_file *seq, void *offset)
1382{
1383 struct net_device *net_dev = (struct net_device *)seq->private;
1384 struct batadv_hard_iface *hard_iface;
1385 struct batadv_priv *bat_priv;
1386
1387 hard_iface = batadv_hardif_get_by_netdev(net_dev);
1388
1389 if (!hard_iface || !hard_iface->soft_iface) {
1390 seq_puts(seq, "Interface not known to B.A.T.M.A.N.\n");
1391 goto out;
1392 }
1393
1394 bat_priv = netdev_priv(hard_iface->soft_iface);
1395 if (!bat_priv->algo_ops->orig.print) {
1396 seq_puts(seq,
1397 "No printing function for this routing protocol\n");
1398 goto out;
1399 }
1400
1401 if (hard_iface->if_status != BATADV_IF_ACTIVE) {
1402 seq_puts(seq, "Interface not active\n");
1403 goto out;
1404 }
1405
1406 seq_printf(seq, "[B.A.T.M.A.N. adv %s, IF/MAC: %s/%pM (%s %s)]\n",
1407 BATADV_SOURCE_VERSION, hard_iface->net_dev->name,
1408 hard_iface->net_dev->dev_addr,
1409 hard_iface->soft_iface->name, bat_priv->algo_ops->name);
1410
1411 bat_priv->algo_ops->orig.print(bat_priv, seq, hard_iface);
1412
1413out:
1414 if (hard_iface)
1415 batadv_hardif_put(hard_iface);
1416 return 0;
1417}
1418#endif
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428int batadv_orig_dump(struct sk_buff *msg, struct netlink_callback *cb)
1429{
1430 struct net *net = sock_net(cb->skb->sk);
1431 struct net_device *soft_iface;
1432 struct net_device *hard_iface = NULL;
1433 struct batadv_hard_iface *hardif = BATADV_IF_DEFAULT;
1434 struct batadv_priv *bat_priv;
1435 struct batadv_hard_iface *primary_if = NULL;
1436 int ret;
1437 int ifindex, hard_ifindex;
1438
1439 ifindex = batadv_netlink_get_ifindex(cb->nlh, BATADV_ATTR_MESH_IFINDEX);
1440 if (!ifindex)
1441 return -EINVAL;
1442
1443 soft_iface = dev_get_by_index(net, ifindex);
1444 if (!soft_iface || !batadv_softif_is_valid(soft_iface)) {
1445 ret = -ENODEV;
1446 goto out;
1447 }
1448
1449 bat_priv = netdev_priv(soft_iface);
1450
1451 primary_if = batadv_primary_if_get_selected(bat_priv);
1452 if (!primary_if || primary_if->if_status != BATADV_IF_ACTIVE) {
1453 ret = -ENOENT;
1454 goto out;
1455 }
1456
1457 hard_ifindex = batadv_netlink_get_ifindex(cb->nlh,
1458 BATADV_ATTR_HARD_IFINDEX);
1459 if (hard_ifindex) {
1460 hard_iface = dev_get_by_index(net, hard_ifindex);
1461 if (hard_iface)
1462 hardif = batadv_hardif_get_by_netdev(hard_iface);
1463
1464 if (!hardif) {
1465 ret = -ENODEV;
1466 goto out;
1467 }
1468
1469 if (hardif->soft_iface != soft_iface) {
1470 ret = -ENOENT;
1471 goto out;
1472 }
1473 }
1474
1475 if (!bat_priv->algo_ops->orig.dump) {
1476 ret = -EOPNOTSUPP;
1477 goto out;
1478 }
1479
1480 bat_priv->algo_ops->orig.dump(msg, cb, bat_priv, hardif);
1481
1482 ret = msg->len;
1483
1484 out:
1485 if (hardif)
1486 batadv_hardif_put(hardif);
1487 if (hard_iface)
1488 dev_put(hard_iface);
1489 if (primary_if)
1490 batadv_hardif_put(primary_if);
1491 if (soft_iface)
1492 dev_put(soft_iface);
1493
1494 return ret;
1495}
1496
1497int batadv_orig_hash_add_if(struct batadv_hard_iface *hard_iface,
1498 int max_if_num)
1499{
1500 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1501 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1502 struct batadv_hashtable *hash = bat_priv->orig_hash;
1503 struct hlist_head *head;
1504 struct batadv_orig_node *orig_node;
1505 u32 i;
1506 int ret;
1507
1508
1509
1510
1511 for (i = 0; i < hash->size; i++) {
1512 head = &hash->table[i];
1513
1514 rcu_read_lock();
1515 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1516 ret = 0;
1517 if (bao->orig.add_if)
1518 ret = bao->orig.add_if(orig_node, max_if_num);
1519 if (ret == -ENOMEM)
1520 goto err;
1521 }
1522 rcu_read_unlock();
1523 }
1524
1525 return 0;
1526
1527err:
1528 rcu_read_unlock();
1529 return -ENOMEM;
1530}
1531
1532int batadv_orig_hash_del_if(struct batadv_hard_iface *hard_iface,
1533 int max_if_num)
1534{
1535 struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
1536 struct batadv_hashtable *hash = bat_priv->orig_hash;
1537 struct hlist_head *head;
1538 struct batadv_hard_iface *hard_iface_tmp;
1539 struct batadv_orig_node *orig_node;
1540 struct batadv_algo_ops *bao = bat_priv->algo_ops;
1541 u32 i;
1542 int ret;
1543
1544
1545
1546
1547 for (i = 0; i < hash->size; i++) {
1548 head = &hash->table[i];
1549
1550 rcu_read_lock();
1551 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1552 ret = 0;
1553 if (bao->orig.del_if)
1554 ret = bao->orig.del_if(orig_node, max_if_num,
1555 hard_iface->if_num);
1556 if (ret == -ENOMEM)
1557 goto err;
1558 }
1559 rcu_read_unlock();
1560 }
1561
1562
1563 rcu_read_lock();
1564 list_for_each_entry_rcu(hard_iface_tmp, &batadv_hardif_list, list) {
1565 if (hard_iface_tmp->if_status == BATADV_IF_NOT_IN_USE)
1566 continue;
1567
1568 if (hard_iface == hard_iface_tmp)
1569 continue;
1570
1571 if (hard_iface->soft_iface != hard_iface_tmp->soft_iface)
1572 continue;
1573
1574 if (hard_iface_tmp->if_num > hard_iface->if_num)
1575 hard_iface_tmp->if_num--;
1576 }
1577 rcu_read_unlock();
1578
1579 hard_iface->if_num = -1;
1580 return 0;
1581
1582err:
1583 rcu_read_unlock();
1584 return -ENOMEM;
1585}
1586