1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/debugfs.h>
19
20#include "main.h"
21#include "hash.h"
22#include "network-coding.h"
23#include "send.h"
24#include "originator.h"
25#include "hard-interface.h"
26#include "routing.h"
27
28static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
29static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
30
31static void batadv_nc_worker(struct work_struct *work);
32static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
33 struct batadv_hard_iface *recv_if);
34
35
36
37
38int __init batadv_nc_init(void)
39{
40 int ret;
41
42
43 ret = batadv_recv_handler_register(BATADV_CODED,
44 batadv_nc_recv_coded_packet);
45
46 return ret;
47}
48
49
50
51
52
53static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
54{
55 queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
56 msecs_to_jiffies(10));
57}
58
59
60
61
62
63
64static void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv)
65{
66 char nc_mode;
67
68 nc_mode = atomic_read(&bat_priv->network_coding);
69
70 switch (nc_mode) {
71 case 0:
72 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
73 break;
74 case 1:
75 batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1,
76 NULL, 0);
77 break;
78 }
79}
80
81
82
83
84
85
86void batadv_nc_status_update(struct net_device *net_dev)
87{
88 struct batadv_priv *bat_priv = netdev_priv(net_dev);
89
90 batadv_nc_tvlv_container_update(bat_priv);
91}
92
93
94
95
96
97
98
99
100
101static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
102 struct batadv_orig_node *orig,
103 uint8_t flags,
104 void *tvlv_value,
105 uint16_t tvlv_value_len)
106{
107 if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
108 orig->capabilities &= ~BATADV_ORIG_CAPA_HAS_NC;
109 else
110 orig->capabilities |= BATADV_ORIG_CAPA_HAS_NC;
111}
112
113
114
115
116
117int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
118{
119 bat_priv->nc.timestamp_fwd_flush = jiffies;
120 bat_priv->nc.timestamp_sniffed_purge = jiffies;
121
122 if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
123 return 0;
124
125 bat_priv->nc.coding_hash = batadv_hash_new(128);
126 if (!bat_priv->nc.coding_hash)
127 goto err;
128
129 batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
130 &batadv_nc_coding_hash_lock_class_key);
131
132 bat_priv->nc.decoding_hash = batadv_hash_new(128);
133 if (!bat_priv->nc.decoding_hash)
134 goto err;
135
136 batadv_hash_set_lock_class(bat_priv->nc.decoding_hash,
137 &batadv_nc_decoding_hash_lock_class_key);
138
139 INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
140 batadv_nc_start_timer(bat_priv);
141
142 batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1,
143 NULL, BATADV_TVLV_NC, 1,
144 BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
145 batadv_nc_tvlv_container_update(bat_priv);
146 return 0;
147
148err:
149 return -ENOMEM;
150}
151
152
153
154
155
156void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
157{
158 atomic_set(&bat_priv->network_coding, 1);
159 bat_priv->nc.min_tq = 200;
160 bat_priv->nc.max_fwd_delay = 10;
161 bat_priv->nc.max_buffer_time = 200;
162}
163
164
165
166
167
168void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
169{
170 INIT_LIST_HEAD(&orig_node->in_coding_list);
171 INIT_LIST_HEAD(&orig_node->out_coding_list);
172 spin_lock_init(&orig_node->in_coding_list_lock);
173 spin_lock_init(&orig_node->out_coding_list_lock);
174}
175
176
177
178
179
180
181static void batadv_nc_node_free_rcu(struct rcu_head *rcu)
182{
183 struct batadv_nc_node *nc_node;
184
185 nc_node = container_of(rcu, struct batadv_nc_node, rcu);
186 batadv_orig_node_free_ref(nc_node->orig_node);
187 kfree(nc_node);
188}
189
190
191
192
193
194
195static void batadv_nc_node_free_ref(struct batadv_nc_node *nc_node)
196{
197 if (atomic_dec_and_test(&nc_node->refcount))
198 call_rcu(&nc_node->rcu, batadv_nc_node_free_rcu);
199}
200
201
202
203
204
205
206static void batadv_nc_path_free_ref(struct batadv_nc_path *nc_path)
207{
208 if (atomic_dec_and_test(&nc_path->refcount))
209 kfree_rcu(nc_path, rcu);
210}
211
212
213
214
215
216static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet)
217{
218 if (nc_packet->skb)
219 kfree_skb(nc_packet->skb);
220
221 batadv_nc_path_free_ref(nc_packet->nc_path);
222 kfree(nc_packet);
223}
224
225
226
227
228
229
230
231
232static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
233 struct batadv_nc_node *nc_node)
234{
235 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
236 return true;
237
238 return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
239}
240
241
242
243
244
245
246
247
248static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
249 struct batadv_nc_path *nc_path)
250{
251 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
252 return true;
253
254
255
256
257 return batadv_has_timed_out(nc_path->last_valid,
258 bat_priv->nc.max_fwd_delay * 10);
259}
260
261
262
263
264
265
266
267
268static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
269 struct batadv_nc_path *nc_path)
270{
271 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
272 return true;
273
274
275
276
277 return batadv_has_timed_out(nc_path->last_valid,
278 bat_priv->nc.max_buffer_time*10);
279}
280
281
282
283
284
285
286
287
288
289
290
291
292static void
293batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
294 struct list_head *list,
295 spinlock_t *lock,
296 bool (*to_purge)(struct batadv_priv *,
297 struct batadv_nc_node *))
298{
299 struct batadv_nc_node *nc_node, *nc_node_tmp;
300
301
302 spin_lock_bh(lock);
303 list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
304
305
306
307 if (to_purge && !to_purge(bat_priv, nc_node))
308 continue;
309
310 batadv_dbg(BATADV_DBG_NC, bat_priv,
311 "Removing nc_node %pM -> %pM\n",
312 nc_node->addr, nc_node->orig_node->orig);
313 list_del_rcu(&nc_node->list);
314 batadv_nc_node_free_ref(nc_node);
315 }
316 spin_unlock_bh(lock);
317}
318
319
320
321
322
323
324
325
326
327
328
329void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
330 struct batadv_orig_node *orig_node,
331 bool (*to_purge)(struct batadv_priv *,
332 struct batadv_nc_node *))
333{
334
335 batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
336 &orig_node->in_coding_list_lock,
337 to_purge);
338
339
340 batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
341 &orig_node->out_coding_list_lock,
342 to_purge);
343}
344
345
346
347
348
349
350static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
351{
352 struct batadv_hashtable *hash = bat_priv->orig_hash;
353 struct hlist_head *head;
354 struct batadv_orig_node *orig_node;
355 uint32_t i;
356
357 if (!hash)
358 return;
359
360
361 for (i = 0; i < hash->size; i++) {
362 head = &hash->table[i];
363
364 rcu_read_lock();
365 hlist_for_each_entry_rcu(orig_node, head, hash_entry)
366 batadv_nc_purge_orig(bat_priv, orig_node,
367 batadv_nc_to_purge_nc_node);
368 rcu_read_unlock();
369 }
370}
371
372
373
374
375
376
377
378
379
380
381
382static void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
383 struct batadv_hashtable *hash,
384 bool (*to_purge)(struct batadv_priv *,
385 struct batadv_nc_path *))
386{
387 struct hlist_head *head;
388 struct hlist_node *node_tmp;
389 struct batadv_nc_path *nc_path;
390 spinlock_t *lock;
391 uint32_t i;
392
393 for (i = 0; i < hash->size; i++) {
394 head = &hash->table[i];
395 lock = &hash->list_locks[i];
396
397
398 spin_lock_bh(lock);
399 hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
400
401
402
403 if (to_purge && !to_purge(bat_priv, nc_path))
404 continue;
405
406
407
408
409
410
411 if (!unlikely(list_empty(&nc_path->packet_list))) {
412 net_ratelimited_function(printk,
413 KERN_WARNING
414 "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
415 nc_path->prev_hop,
416 nc_path->next_hop);
417 continue;
418 }
419
420
421 batadv_dbg(BATADV_DBG_NC, bat_priv,
422 "Remove nc_path %pM -> %pM\n",
423 nc_path->prev_hop, nc_path->next_hop);
424 hlist_del_rcu(&nc_path->hash_entry);
425 batadv_nc_path_free_ref(nc_path);
426 }
427 spin_unlock_bh(lock);
428 }
429}
430
431
432
433
434
435
436
437static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
438 const char *dst)
439{
440 memcpy(key->prev_hop, src, sizeof(key->prev_hop));
441 memcpy(key->next_hop, dst, sizeof(key->next_hop));
442}
443
444
445
446
447
448
449
450
451static uint32_t batadv_nc_hash_choose(const void *data, uint32_t size)
452{
453 const struct batadv_nc_path *nc_path = data;
454 uint32_t hash = 0;
455
456 hash = batadv_hash_bytes(hash, &nc_path->prev_hop,
457 sizeof(nc_path->prev_hop));
458 hash = batadv_hash_bytes(hash, &nc_path->next_hop,
459 sizeof(nc_path->next_hop));
460
461 hash += (hash << 3);
462 hash ^= (hash >> 11);
463 hash += (hash << 15);
464
465 return hash % size;
466}
467
468
469
470
471
472
473
474
475
476static int batadv_nc_hash_compare(const struct hlist_node *node,
477 const void *data2)
478{
479 const struct batadv_nc_path *nc_path1, *nc_path2;
480
481 nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
482 nc_path2 = data2;
483
484
485 if (memcmp(nc_path1->prev_hop, nc_path2->prev_hop,
486 sizeof(nc_path1->prev_hop)) != 0)
487 return 0;
488
489 if (memcmp(nc_path1->next_hop, nc_path2->next_hop,
490 sizeof(nc_path1->next_hop)) != 0)
491 return 0;
492
493 return 1;
494}
495
496
497
498
499
500
501
502
503static struct batadv_nc_path *
504batadv_nc_hash_find(struct batadv_hashtable *hash,
505 void *data)
506{
507 struct hlist_head *head;
508 struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
509 int index;
510
511 if (!hash)
512 return NULL;
513
514 index = batadv_nc_hash_choose(data, hash->size);
515 head = &hash->table[index];
516
517 rcu_read_lock();
518 hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
519 if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
520 continue;
521
522 if (!atomic_inc_not_zero(&nc_path->refcount))
523 continue;
524
525 nc_path_tmp = nc_path;
526 break;
527 }
528 rcu_read_unlock();
529
530 return nc_path_tmp;
531}
532
533
534
535
536
537static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
538{
539 batadv_send_skb_packet(nc_packet->skb,
540 nc_packet->neigh_node->if_incoming,
541 nc_packet->nc_path->next_hop);
542 nc_packet->skb = NULL;
543 batadv_nc_packet_free(nc_packet);
544}
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
560 struct batadv_nc_path *nc_path,
561 struct batadv_nc_packet *nc_packet)
562{
563 unsigned long timeout = bat_priv->nc.max_buffer_time;
564 bool res = false;
565
566
567
568
569 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
570 !batadv_has_timed_out(nc_packet->timestamp, timeout))
571 goto out;
572
573
574 list_del(&nc_packet->list);
575 batadv_nc_packet_free(nc_packet);
576
577 res = true;
578
579out:
580 return res;
581}
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
597 struct batadv_nc_path *nc_path,
598 struct batadv_nc_packet *nc_packet)
599{
600 unsigned long timeout = bat_priv->nc.max_fwd_delay;
601
602
603
604
605 if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
606 !batadv_has_timed_out(nc_packet->timestamp, timeout))
607 return false;
608
609
610 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
611 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
612 nc_packet->skb->len + ETH_HLEN);
613 list_del(&nc_packet->list);
614 batadv_nc_send_packet(nc_packet);
615
616 return true;
617}
618
619
620
621
622
623
624
625
626
627
628static void
629batadv_nc_process_nc_paths(struct batadv_priv *bat_priv,
630 struct batadv_hashtable *hash,
631 bool (*process_fn)(struct batadv_priv *,
632 struct batadv_nc_path *,
633 struct batadv_nc_packet *))
634{
635 struct hlist_head *head;
636 struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
637 struct batadv_nc_path *nc_path;
638 bool ret;
639 int i;
640
641 if (!hash)
642 return;
643
644
645 for (i = 0; i < hash->size; i++) {
646 head = &hash->table[i];
647
648
649 rcu_read_lock();
650 hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
651
652 spin_lock_bh(&nc_path->packet_list_lock);
653 list_for_each_entry_safe(nc_packet, nc_packet_tmp,
654 &nc_path->packet_list, list) {
655 ret = process_fn(bat_priv, nc_path, nc_packet);
656 if (!ret)
657 break;
658 }
659 spin_unlock_bh(&nc_path->packet_list_lock);
660 }
661 rcu_read_unlock();
662 }
663}
664
665
666
667
668
669static void batadv_nc_worker(struct work_struct *work)
670{
671 struct delayed_work *delayed_work;
672 struct batadv_priv_nc *priv_nc;
673 struct batadv_priv *bat_priv;
674 unsigned long timeout;
675
676 delayed_work = container_of(work, struct delayed_work, work);
677 priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
678 bat_priv = container_of(priv_nc, struct batadv_priv, nc);
679
680 batadv_nc_purge_orig_hash(bat_priv);
681 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
682 batadv_nc_to_purge_nc_path_coding);
683 batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
684 batadv_nc_to_purge_nc_path_decoding);
685
686 timeout = bat_priv->nc.max_fwd_delay;
687
688 if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) {
689 batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash,
690 batadv_nc_fwd_flush);
691 bat_priv->nc.timestamp_fwd_flush = jiffies;
692 }
693
694 if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge,
695 bat_priv->nc.max_buffer_time)) {
696 batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash,
697 batadv_nc_sniffed_purge);
698 bat_priv->nc.timestamp_sniffed_purge = jiffies;
699 }
700
701
702 batadv_nc_start_timer(bat_priv);
703}
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
719 struct batadv_orig_node *orig_node,
720 struct batadv_ogm_packet *ogm_packet)
721{
722 struct batadv_orig_ifinfo *orig_ifinfo;
723 uint32_t last_real_seqno;
724 uint8_t last_ttl;
725
726 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, BATADV_IF_DEFAULT);
727 if (!orig_ifinfo)
728 return false;
729
730 last_ttl = orig_ifinfo->last_ttl;
731 last_real_seqno = orig_ifinfo->last_real_seqno;
732 batadv_orig_ifinfo_free_ref(orig_ifinfo);
733
734 if (last_real_seqno != ntohl(ogm_packet->seqno))
735 return false;
736 if (last_ttl != ogm_packet->ttl + 1)
737 return false;
738 if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
739 return false;
740 if (ogm_packet->tq < bat_priv->nc.min_tq)
741 return false;
742
743 return true;
744}
745
746
747
748
749
750
751
752
753
754
755static struct batadv_nc_node
756*batadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
757 struct batadv_orig_node *orig_neigh_node,
758 bool in_coding)
759{
760 struct batadv_nc_node *nc_node, *nc_node_out = NULL;
761 struct list_head *list;
762
763 if (in_coding)
764 list = &orig_neigh_node->in_coding_list;
765 else
766 list = &orig_neigh_node->out_coding_list;
767
768
769 rcu_read_lock();
770 list_for_each_entry_rcu(nc_node, list, list) {
771 if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
772 continue;
773
774 if (!atomic_inc_not_zero(&nc_node->refcount))
775 continue;
776
777
778 nc_node_out = nc_node;
779 break;
780 }
781 rcu_read_unlock();
782
783 return nc_node_out;
784}
785
786
787
788
789
790
791
792
793
794
795
796
797static struct batadv_nc_node
798*batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
799 struct batadv_orig_node *orig_node,
800 struct batadv_orig_node *orig_neigh_node,
801 bool in_coding)
802{
803 struct batadv_nc_node *nc_node;
804 spinlock_t *lock;
805 struct list_head *list;
806
807
808 nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
809
810
811 if (nc_node)
812 return nc_node;
813
814 nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
815 if (!nc_node)
816 return NULL;
817
818 if (!atomic_inc_not_zero(&orig_neigh_node->refcount))
819 goto free;
820
821
822 INIT_LIST_HEAD(&nc_node->list);
823 ether_addr_copy(nc_node->addr, orig_node->orig);
824 nc_node->orig_node = orig_neigh_node;
825 atomic_set(&nc_node->refcount, 2);
826
827
828 if (in_coding) {
829 lock = &orig_neigh_node->in_coding_list_lock;
830 list = &orig_neigh_node->in_coding_list;
831 } else {
832 lock = &orig_neigh_node->out_coding_list_lock;
833 list = &orig_neigh_node->out_coding_list;
834 }
835
836 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
837 nc_node->addr, nc_node->orig_node->orig);
838
839
840 spin_lock_bh(lock);
841 list_add_tail_rcu(&nc_node->list, list);
842 spin_unlock_bh(lock);
843
844 return nc_node;
845
846free:
847 kfree(nc_node);
848 return NULL;
849}
850
851
852
853
854
855
856
857
858
859
860
861void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
862 struct batadv_orig_node *orig_node,
863 struct batadv_orig_node *orig_neigh_node,
864 struct batadv_ogm_packet *ogm_packet,
865 int is_single_hop_neigh)
866{
867 struct batadv_nc_node *in_nc_node = NULL, *out_nc_node = NULL;
868
869
870 if (!atomic_read(&bat_priv->network_coding))
871 goto out;
872
873
874 if (!(orig_node->capabilities & BATADV_ORIG_CAPA_HAS_NC))
875 goto out;
876
877
878 if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
879 !is_single_hop_neigh)
880 goto out;
881
882
883 in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
884 orig_neigh_node, true);
885 if (!in_nc_node)
886 goto out;
887
888 in_nc_node->last_seen = jiffies;
889
890
891 out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
892 orig_node, false);
893 if (!out_nc_node)
894 goto out;
895
896 out_nc_node->last_seen = jiffies;
897
898out:
899 if (in_nc_node)
900 batadv_nc_node_free_ref(in_nc_node);
901 if (out_nc_node)
902 batadv_nc_node_free_ref(out_nc_node);
903}
904
905
906
907
908
909
910
911
912
913
914
915static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
916 struct batadv_hashtable *hash,
917 uint8_t *src,
918 uint8_t *dst)
919{
920 int hash_added;
921 struct batadv_nc_path *nc_path, nc_path_key;
922
923 batadv_nc_hash_key_gen(&nc_path_key, src, dst);
924
925
926 nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
927
928 if (nc_path) {
929
930 nc_path->last_valid = jiffies;
931 return nc_path;
932 }
933
934
935 nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
936
937 if (!nc_path)
938 return NULL;
939
940
941 INIT_LIST_HEAD(&nc_path->packet_list);
942 spin_lock_init(&nc_path->packet_list_lock);
943 atomic_set(&nc_path->refcount, 2);
944 nc_path->last_valid = jiffies;
945 ether_addr_copy(nc_path->next_hop, dst);
946 ether_addr_copy(nc_path->prev_hop, src);
947
948 batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
949 nc_path->prev_hop,
950 nc_path->next_hop);
951
952
953 hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
954 batadv_nc_hash_choose, &nc_path_key,
955 &nc_path->hash_entry);
956
957 if (hash_added < 0) {
958 kfree(nc_path);
959 return NULL;
960 }
961
962 return nc_path;
963}
964
965
966
967
968
969
970static uint8_t batadv_nc_random_weight_tq(uint8_t tq)
971{
972 uint8_t rand_val, rand_tq;
973
974 get_random_bytes(&rand_val, sizeof(rand_val));
975
976
977 rand_tq = rand_val * (BATADV_TQ_MAX_VALUE - tq);
978
979
980 rand_tq /= BATADV_TQ_MAX_VALUE;
981
982
983 return BATADV_TQ_MAX_VALUE - rand_tq;
984}
985
986
987
988
989
990
991
992static void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
993{
994 unsigned int i;
995
996 for (i = 0; i < len; ++i)
997 dst[i] ^= src[i];
998}
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
1012 struct sk_buff *skb,
1013 struct ethhdr *ethhdr,
1014 struct batadv_nc_packet *nc_packet,
1015 struct batadv_neigh_node *neigh_node)
1016{
1017 uint8_t tq_weighted_neigh, tq_weighted_coding, tq_tmp;
1018 struct sk_buff *skb_dest, *skb_src;
1019 struct batadv_unicast_packet *packet1;
1020 struct batadv_unicast_packet *packet2;
1021 struct batadv_coded_packet *coded_packet;
1022 struct batadv_neigh_node *neigh_tmp, *router_neigh;
1023 struct batadv_neigh_node *router_coding = NULL;
1024 struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL;
1025 struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL;
1026 uint8_t *first_source, *first_dest, *second_source, *second_dest;
1027 __be32 packet_id1, packet_id2;
1028 size_t count;
1029 bool res = false;
1030 int coding_len;
1031 int unicast_size = sizeof(*packet1);
1032 int coded_size = sizeof(*coded_packet);
1033 int header_add = coded_size - unicast_size;
1034
1035
1036
1037
1038 router_neigh = batadv_orig_router_get(neigh_node->orig_node,
1039 BATADV_IF_DEFAULT);
1040 if (!router_neigh)
1041 goto out;
1042
1043 router_neigh_ifinfo = batadv_neigh_ifinfo_get(router_neigh,
1044 BATADV_IF_DEFAULT);
1045 if (!router_neigh_ifinfo)
1046 goto out;
1047
1048 neigh_tmp = nc_packet->neigh_node;
1049 router_coding = batadv_orig_router_get(neigh_tmp->orig_node,
1050 BATADV_IF_DEFAULT);
1051 if (!router_coding)
1052 goto out;
1053
1054 router_coding_ifinfo = batadv_neigh_ifinfo_get(router_coding,
1055 BATADV_IF_DEFAULT);
1056 if (!router_coding_ifinfo)
1057 goto out;
1058
1059 tq_tmp = router_neigh_ifinfo->bat_iv.tq_avg;
1060 tq_weighted_neigh = batadv_nc_random_weight_tq(tq_tmp);
1061 tq_tmp = router_coding_ifinfo->bat_iv.tq_avg;
1062 tq_weighted_coding = batadv_nc_random_weight_tq(tq_tmp);
1063
1064
1065
1066
1067 if (tq_weighted_neigh >= tq_weighted_coding) {
1068
1069 first_dest = nc_packet->nc_path->next_hop;
1070 first_source = nc_packet->nc_path->prev_hop;
1071 second_dest = neigh_node->addr;
1072 second_source = ethhdr->h_source;
1073 packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
1074 packet2 = (struct batadv_unicast_packet *)skb->data;
1075 packet_id1 = nc_packet->packet_id;
1076 packet_id2 = batadv_skb_crc32(skb,
1077 skb->data + sizeof(*packet2));
1078 } else {
1079
1080 first_dest = neigh_node->addr;
1081 first_source = ethhdr->h_source;
1082 second_dest = nc_packet->nc_path->next_hop;
1083 second_source = nc_packet->nc_path->prev_hop;
1084 packet1 = (struct batadv_unicast_packet *)skb->data;
1085 packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
1086 packet_id1 = batadv_skb_crc32(skb,
1087 skb->data + sizeof(*packet1));
1088 packet_id2 = nc_packet->packet_id;
1089 }
1090
1091
1092
1093
1094 if (skb->len <= nc_packet->skb->len) {
1095 skb_dest = nc_packet->skb;
1096 skb_src = skb;
1097 } else {
1098 skb_dest = skb;
1099 skb_src = nc_packet->skb;
1100 }
1101
1102
1103 coding_len = skb_src->len - unicast_size;
1104
1105 if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
1106 goto out;
1107
1108 skb_push(skb_dest, header_add);
1109
1110 coded_packet = (struct batadv_coded_packet *)skb_dest->data;
1111 skb_reset_mac_header(skb_dest);
1112
1113 coded_packet->packet_type = BATADV_CODED;
1114 coded_packet->version = BATADV_COMPAT_VERSION;
1115 coded_packet->ttl = packet1->ttl;
1116
1117
1118 ether_addr_copy(coded_packet->first_source, first_source);
1119 ether_addr_copy(coded_packet->first_orig_dest, packet1->dest);
1120 coded_packet->first_crc = packet_id1;
1121 coded_packet->first_ttvn = packet1->ttvn;
1122
1123
1124 ether_addr_copy(coded_packet->second_dest, second_dest);
1125 ether_addr_copy(coded_packet->second_source, second_source);
1126 ether_addr_copy(coded_packet->second_orig_dest, packet2->dest);
1127 coded_packet->second_crc = packet_id2;
1128 coded_packet->second_ttl = packet2->ttl;
1129 coded_packet->second_ttvn = packet2->ttvn;
1130 coded_packet->coded_len = htons(coding_len);
1131
1132
1133 batadv_nc_memxor(skb_dest->data + coded_size,
1134 skb_src->data + unicast_size, coding_len);
1135
1136
1137 if (BATADV_SKB_CB(skb_src)->decoded &&
1138 BATADV_SKB_CB(skb_dest)->decoded) {
1139
1140 count = skb_src->len + ETH_HLEN;
1141 count += skb_dest->len + ETH_HLEN;
1142 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
1143 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
1144 } else if (!BATADV_SKB_CB(skb_src)->decoded &&
1145 !BATADV_SKB_CB(skb_dest)->decoded) {
1146
1147 count = skb_src->len + ETH_HLEN;
1148 count += skb_dest->len + ETH_HLEN;
1149 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
1150 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
1151 } else if (BATADV_SKB_CB(skb_src)->decoded &&
1152 !BATADV_SKB_CB(skb_dest)->decoded) {
1153
1154 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1155 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1156 skb_src->len + ETH_HLEN);
1157 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1158 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1159 skb_dest->len + ETH_HLEN);
1160 } else if (!BATADV_SKB_CB(skb_src)->decoded &&
1161 BATADV_SKB_CB(skb_dest)->decoded) {
1162
1163 batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
1164 batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
1165 skb_src->len + ETH_HLEN);
1166 batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
1167 batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
1168 skb_dest->len + ETH_HLEN);
1169 }
1170
1171
1172 kfree_skb(skb_src);
1173
1174
1175 nc_packet->skb = NULL;
1176 batadv_nc_packet_free(nc_packet);
1177
1178
1179 batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest);
1180 res = true;
1181out:
1182 if (router_neigh)
1183 batadv_neigh_node_free_ref(router_neigh);
1184 if (router_coding)
1185 batadv_neigh_node_free_ref(router_coding);
1186 if (router_neigh_ifinfo)
1187 batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo);
1188 if (router_coding_ifinfo)
1189 batadv_neigh_ifinfo_free_ref(router_coding_ifinfo);
1190 return res;
1191}
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210static bool batadv_nc_skb_coding_possible(struct sk_buff *skb,
1211 uint8_t *dst, uint8_t *src)
1212{
1213 if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
1214 return false;
1215 else
1216 return true;
1217}
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230static struct batadv_nc_packet *
1231batadv_nc_path_search(struct batadv_priv *bat_priv,
1232 struct batadv_nc_node *in_nc_node,
1233 struct batadv_nc_node *out_nc_node,
1234 struct sk_buff *skb,
1235 uint8_t *eth_dst)
1236{
1237 struct batadv_nc_path *nc_path, nc_path_key;
1238 struct batadv_nc_packet *nc_packet_out = NULL;
1239 struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
1240 struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
1241 int idx;
1242
1243 if (!hash)
1244 return NULL;
1245
1246
1247 batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
1248 out_nc_node->addr);
1249 idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
1250
1251
1252 rcu_read_lock();
1253 hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
1254 if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
1255 continue;
1256
1257 if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
1258 continue;
1259
1260 spin_lock_bh(&nc_path->packet_list_lock);
1261 if (list_empty(&nc_path->packet_list)) {
1262 spin_unlock_bh(&nc_path->packet_list_lock);
1263 continue;
1264 }
1265
1266 list_for_each_entry_safe(nc_packet, nc_packet_tmp,
1267 &nc_path->packet_list, list) {
1268 if (!batadv_nc_skb_coding_possible(nc_packet->skb,
1269 eth_dst,
1270 in_nc_node->addr))
1271 continue;
1272
1273
1274 list_del(&nc_packet->list);
1275 nc_packet_out = nc_packet;
1276 break;
1277 }
1278
1279 spin_unlock_bh(&nc_path->packet_list_lock);
1280 break;
1281 }
1282 rcu_read_unlock();
1283
1284 return nc_packet_out;
1285}
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298static struct batadv_nc_packet *
1299batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
1300 struct sk_buff *skb,
1301 uint8_t *eth_dst,
1302 uint8_t *eth_src,
1303 struct batadv_nc_node *in_nc_node)
1304{
1305 struct batadv_orig_node *orig_node;
1306 struct batadv_nc_node *out_nc_node;
1307 struct batadv_nc_packet *nc_packet = NULL;
1308
1309 orig_node = batadv_orig_hash_find(bat_priv, eth_src);
1310 if (!orig_node)
1311 return NULL;
1312
1313 rcu_read_lock();
1314 list_for_each_entry_rcu(out_nc_node,
1315 &orig_node->out_coding_list, list) {
1316
1317 if (!batadv_nc_skb_coding_possible(skb,
1318 out_nc_node->addr, eth_src))
1319 continue;
1320
1321
1322 nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
1323 out_nc_node, skb, eth_dst);
1324 if (nc_packet)
1325 break;
1326 }
1327 rcu_read_unlock();
1328
1329 batadv_orig_node_free_ref(orig_node);
1330 return nc_packet;
1331}
1332
1333
1334
1335
1336
1337
1338
1339
1340static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
1341 struct sk_buff *skb,
1342 uint8_t *eth_dst_new)
1343{
1344 struct ethhdr *ethhdr;
1345
1346
1347 skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
1348 if (!skb)
1349 return;
1350
1351
1352 ethhdr = eth_hdr(skb);
1353 ether_addr_copy(ethhdr->h_source, ethhdr->h_dest);
1354 ether_addr_copy(ethhdr->h_dest, eth_dst_new);
1355
1356
1357 skb_push(skb, ETH_HLEN);
1358
1359
1360 batadv_nc_skb_store_for_decoding(bat_priv, skb);
1361
1362
1363
1364
1365 kfree_skb(skb);
1366}
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
1382 struct batadv_neigh_node *neigh_node,
1383 struct ethhdr *ethhdr)
1384{
1385 struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1386 struct batadv_priv *bat_priv = netdev_priv(netdev);
1387 struct batadv_orig_node *orig_node = neigh_node->orig_node;
1388 struct batadv_nc_node *nc_node;
1389 struct batadv_nc_packet *nc_packet = NULL;
1390
1391 rcu_read_lock();
1392 list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
1393
1394 nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
1395 neigh_node->addr,
1396 ethhdr->h_source, nc_node);
1397
1398
1399 if (nc_packet)
1400 break;
1401 }
1402 rcu_read_unlock();
1403
1404 if (!nc_packet)
1405 return false;
1406
1407
1408 batadv_nc_skb_store_before_coding(bat_priv, skb,
1409 neigh_node->addr);
1410 batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
1411 nc_packet->neigh_node->addr);
1412
1413
1414 if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
1415 neigh_node))
1416 return true;
1417
1418
1419
1420
1421
1422 batadv_nc_send_packet(nc_packet);
1423 return false;
1424}
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
1436 struct batadv_nc_path *nc_path,
1437 struct batadv_neigh_node *neigh_node,
1438 __be32 packet_id)
1439{
1440 struct batadv_nc_packet *nc_packet;
1441
1442 nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
1443 if (!nc_packet)
1444 return false;
1445
1446
1447 nc_packet->timestamp = jiffies;
1448 nc_packet->packet_id = packet_id;
1449 nc_packet->skb = skb;
1450 nc_packet->neigh_node = neigh_node;
1451 nc_packet->nc_path = nc_path;
1452
1453
1454 spin_lock_bh(&nc_path->packet_list_lock);
1455 list_add_tail(&nc_packet->list, &nc_path->packet_list);
1456 spin_unlock_bh(&nc_path->packet_list_lock);
1457
1458 return true;
1459}
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469bool batadv_nc_skb_forward(struct sk_buff *skb,
1470 struct batadv_neigh_node *neigh_node)
1471{
1472 const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
1473 struct batadv_priv *bat_priv = netdev_priv(netdev);
1474 struct batadv_unicast_packet *packet;
1475 struct batadv_nc_path *nc_path;
1476 struct ethhdr *ethhdr = eth_hdr(skb);
1477 __be32 packet_id;
1478 u8 *payload;
1479
1480
1481 if (!atomic_read(&bat_priv->network_coding))
1482 goto out;
1483
1484
1485 payload = skb_network_header(skb);
1486 packet = (struct batadv_unicast_packet *)payload;
1487 if (packet->packet_type != BATADV_UNICAST)
1488 goto out;
1489
1490
1491 if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
1492 return true;
1493
1494
1495 nc_path = batadv_nc_get_path(bat_priv,
1496 bat_priv->nc.coding_hash,
1497 ethhdr->h_source,
1498 neigh_node->addr);
1499
1500 if (!nc_path)
1501 goto out;
1502
1503
1504 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1505 if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
1506 goto free_nc_path;
1507
1508
1509 return true;
1510
1511free_nc_path:
1512 batadv_nc_path_free_ref(nc_path);
1513out:
1514
1515 return false;
1516}
1517
1518
1519
1520
1521
1522
1523
1524void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
1525 struct sk_buff *skb)
1526{
1527 struct batadv_unicast_packet *packet;
1528 struct batadv_nc_path *nc_path;
1529 struct ethhdr *ethhdr = eth_hdr(skb);
1530 __be32 packet_id;
1531 u8 *payload;
1532
1533
1534 if (!atomic_read(&bat_priv->network_coding))
1535 goto out;
1536
1537
1538 payload = skb_network_header(skb);
1539 packet = (struct batadv_unicast_packet *)payload;
1540 if (packet->packet_type != BATADV_UNICAST)
1541 goto out;
1542
1543
1544 nc_path = batadv_nc_get_path(bat_priv,
1545 bat_priv->nc.decoding_hash,
1546 ethhdr->h_source,
1547 ethhdr->h_dest);
1548
1549 if (!nc_path)
1550 goto out;
1551
1552
1553 skb = skb_clone(skb, GFP_ATOMIC);
1554 if (unlikely(!skb))
1555 goto free_nc_path;
1556
1557 if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
1558 goto free_skb;
1559
1560 if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
1561 goto free_skb;
1562
1563
1564 packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
1565 if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
1566 goto free_skb;
1567
1568 batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
1569 return;
1570
1571free_skb:
1572 kfree_skb(skb);
1573free_nc_path:
1574 batadv_nc_path_free_ref(nc_path);
1575out:
1576 return;
1577}
1578
1579
1580
1581
1582
1583
1584
1585void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
1586 struct sk_buff *skb)
1587{
1588 struct ethhdr *ethhdr = eth_hdr(skb);
1589
1590 if (batadv_is_my_mac(bat_priv, ethhdr->h_dest))
1591 return;
1592
1593
1594 skb_push(skb, ETH_HLEN);
1595
1596 batadv_nc_skb_store_for_decoding(bat_priv, skb);
1597}
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609static struct batadv_unicast_packet *
1610batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
1611 struct batadv_nc_packet *nc_packet)
1612{
1613 const int h_size = sizeof(struct batadv_unicast_packet);
1614 const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
1615 struct batadv_unicast_packet *unicast_packet;
1616 struct batadv_coded_packet coded_packet_tmp;
1617 struct ethhdr *ethhdr, ethhdr_tmp;
1618 uint8_t *orig_dest, ttl, ttvn;
1619 unsigned int coding_len;
1620 int err;
1621
1622
1623 memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
1624 memcpy(ðhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
1625
1626 if (skb_cow(skb, 0) < 0)
1627 return NULL;
1628
1629 if (unlikely(!skb_pull_rcsum(skb, h_diff)))
1630 return NULL;
1631
1632
1633
1634
1635 skb_set_mac_header(skb, -ETH_HLEN);
1636 skb_reset_network_header(skb);
1637
1638
1639 ethhdr = eth_hdr(skb);
1640 *ethhdr = ethhdr_tmp;
1641
1642
1643
1644
1645 if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) {
1646
1647
1648
1649
1650 ether_addr_copy(ethhdr->h_dest, coded_packet_tmp.second_dest);
1651 skb->pkt_type = PACKET_HOST;
1652
1653 orig_dest = coded_packet_tmp.second_orig_dest;
1654 ttl = coded_packet_tmp.second_ttl;
1655 ttvn = coded_packet_tmp.second_ttvn;
1656 } else {
1657 orig_dest = coded_packet_tmp.first_orig_dest;
1658 ttl = coded_packet_tmp.ttl;
1659 ttvn = coded_packet_tmp.first_ttvn;
1660 }
1661
1662 coding_len = ntohs(coded_packet_tmp.coded_len);
1663
1664 if (coding_len > skb->len)
1665 return NULL;
1666
1667
1668
1669
1670 batadv_nc_memxor(skb->data + h_size,
1671 nc_packet->skb->data + h_size,
1672 coding_len);
1673
1674
1675 if (nc_packet->skb->len > coding_len + h_size) {
1676 err = pskb_trim_rcsum(skb, coding_len + h_size);
1677 if (err)
1678 return NULL;
1679 }
1680
1681
1682 unicast_packet = (struct batadv_unicast_packet *)skb->data;
1683 unicast_packet->packet_type = BATADV_UNICAST;
1684 unicast_packet->version = BATADV_COMPAT_VERSION;
1685 unicast_packet->ttl = ttl;
1686 ether_addr_copy(unicast_packet->dest, orig_dest);
1687 unicast_packet->ttvn = ttvn;
1688
1689 batadv_nc_packet_free(nc_packet);
1690 return unicast_packet;
1691}
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702static struct batadv_nc_packet *
1703batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv,
1704 struct ethhdr *ethhdr,
1705 struct batadv_coded_packet *coded)
1706{
1707 struct batadv_hashtable *hash = bat_priv->nc.decoding_hash;
1708 struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL;
1709 struct batadv_nc_path *nc_path, nc_path_key;
1710 uint8_t *dest, *source;
1711 __be32 packet_id;
1712 int index;
1713
1714 if (!hash)
1715 return NULL;
1716
1717
1718 dest = ethhdr->h_source;
1719 if (!batadv_is_my_mac(bat_priv, coded->second_dest)) {
1720 source = coded->second_source;
1721 packet_id = coded->second_crc;
1722 } else {
1723 source = coded->first_source;
1724 packet_id = coded->first_crc;
1725 }
1726
1727 batadv_nc_hash_key_gen(&nc_path_key, source, dest);
1728 index = batadv_nc_hash_choose(&nc_path_key, hash->size);
1729
1730
1731 rcu_read_lock();
1732 hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) {
1733
1734 spin_lock_bh(&nc_path->packet_list_lock);
1735 list_for_each_entry(tmp_nc_packet,
1736 &nc_path->packet_list, list) {
1737 if (packet_id == tmp_nc_packet->packet_id) {
1738 list_del(&tmp_nc_packet->list);
1739
1740 nc_packet = tmp_nc_packet;
1741 break;
1742 }
1743 }
1744 spin_unlock_bh(&nc_path->packet_list_lock);
1745
1746 if (nc_packet)
1747 break;
1748 }
1749 rcu_read_unlock();
1750
1751 if (!nc_packet)
1752 batadv_dbg(BATADV_DBG_NC, bat_priv,
1753 "No decoding packet found for %u\n", packet_id);
1754
1755 return nc_packet;
1756}
1757
1758
1759
1760
1761
1762
1763
1764static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
1765 struct batadv_hard_iface *recv_if)
1766{
1767 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1768 struct batadv_unicast_packet *unicast_packet;
1769 struct batadv_coded_packet *coded_packet;
1770 struct batadv_nc_packet *nc_packet;
1771 struct ethhdr *ethhdr;
1772 int hdr_size = sizeof(*coded_packet);
1773
1774
1775 if (!atomic_read(&bat_priv->network_coding))
1776 return NET_RX_DROP;
1777
1778
1779 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1780 return NET_RX_DROP;
1781
1782 coded_packet = (struct batadv_coded_packet *)skb->data;
1783 ethhdr = eth_hdr(skb);
1784
1785
1786 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) &&
1787 !batadv_is_my_mac(bat_priv, coded_packet->second_dest))
1788 return NET_RX_DROP;
1789
1790
1791 if (batadv_is_my_mac(bat_priv, coded_packet->second_dest))
1792 batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED);
1793
1794 nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr,
1795 coded_packet);
1796 if (!nc_packet) {
1797 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
1798 return NET_RX_DROP;
1799 }
1800
1801
1802 if (skb_linearize(skb) < 0)
1803 goto free_nc_packet;
1804
1805 if (skb_linearize(nc_packet->skb) < 0)
1806 goto free_nc_packet;
1807
1808
1809 unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet);
1810 if (!unicast_packet) {
1811 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
1812 goto free_nc_packet;
1813 }
1814
1815
1816 BATADV_SKB_CB(skb)->decoded = true;
1817 batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE);
1818 batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES,
1819 skb->len + ETH_HLEN);
1820 return batadv_recv_unicast_packet(skb, recv_if);
1821
1822free_nc_packet:
1823 batadv_nc_packet_free(nc_packet);
1824 return NET_RX_DROP;
1825}
1826
1827
1828
1829
1830
1831void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
1832{
1833 batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
1834 batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
1835 cancel_delayed_work_sync(&bat_priv->nc.work);
1836
1837 batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
1838 batadv_hash_destroy(bat_priv->nc.coding_hash);
1839 batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
1840 batadv_hash_destroy(bat_priv->nc.decoding_hash);
1841}
1842
1843
1844
1845
1846
1847
1848int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
1849{
1850 struct net_device *net_dev = (struct net_device *)seq->private;
1851 struct batadv_priv *bat_priv = netdev_priv(net_dev);
1852 struct batadv_hashtable *hash = bat_priv->orig_hash;
1853 struct batadv_hard_iface *primary_if;
1854 struct hlist_head *head;
1855 struct batadv_orig_node *orig_node;
1856 struct batadv_nc_node *nc_node;
1857 int i;
1858
1859 primary_if = batadv_seq_print_text_primary_if_get(seq);
1860 if (!primary_if)
1861 goto out;
1862
1863
1864 for (i = 0; i < hash->size; i++) {
1865 head = &hash->table[i];
1866
1867
1868 rcu_read_lock();
1869 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
1870
1871
1872
1873 if (list_empty(&orig_node->in_coding_list) &&
1874 list_empty(&orig_node->out_coding_list))
1875 continue;
1876
1877 seq_printf(seq, "Node: %pM\n", orig_node->orig);
1878
1879 seq_puts(seq, " Ingoing: ");
1880
1881 list_for_each_entry_rcu(nc_node,
1882 &orig_node->in_coding_list,
1883 list)
1884 seq_printf(seq, "%pM ",
1885 nc_node->addr);
1886 seq_puts(seq, "\n");
1887
1888 seq_puts(seq, " Outgoing: ");
1889
1890 list_for_each_entry_rcu(nc_node,
1891 &orig_node->out_coding_list,
1892 list)
1893 seq_printf(seq, "%pM ",
1894 nc_node->addr);
1895 seq_puts(seq, "\n\n");
1896 }
1897 rcu_read_unlock();
1898 }
1899
1900out:
1901 if (primary_if)
1902 batadv_hardif_free_ref(primary_if);
1903 return 0;
1904}
1905
1906
1907
1908
1909
1910int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
1911{
1912 struct dentry *nc_dir, *file;
1913
1914 nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
1915 if (!nc_dir)
1916 goto out;
1917
1918 file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir,
1919 &bat_priv->nc.min_tq);
1920 if (!file)
1921 goto out;
1922
1923 file = debugfs_create_u32("max_fwd_delay", S_IRUGO | S_IWUSR, nc_dir,
1924 &bat_priv->nc.max_fwd_delay);
1925 if (!file)
1926 goto out;
1927
1928 file = debugfs_create_u32("max_buffer_time", S_IRUGO | S_IWUSR, nc_dir,
1929 &bat_priv->nc.max_buffer_time);
1930 if (!file)
1931 goto out;
1932
1933 return 0;
1934
1935out:
1936 return -ENOMEM;
1937}
1938