1
2
3
4
5
6
7#include "routing.h"
8#include "main.h"
9
10#include <linux/atomic.h>
11#include <linux/byteorder/generic.h>
12#include <linux/compiler.h>
13#include <linux/errno.h>
14#include <linux/etherdevice.h>
15#include <linux/if_ether.h>
16#include <linux/jiffies.h>
17#include <linux/kref.h>
18#include <linux/netdevice.h>
19#include <linux/printk.h>
20#include <linux/rculist.h>
21#include <linux/rcupdate.h>
22#include <linux/skbuff.h>
23#include <linux/spinlock.h>
24#include <linux/stddef.h>
25#include <uapi/linux/batadv_packet.h>
26
27#include "bitarray.h"
28#include "bridge_loop_avoidance.h"
29#include "distributed-arp-table.h"
30#include "fragmentation.h"
31#include "hard-interface.h"
32#include "log.h"
33#include "network-coding.h"
34#include "originator.h"
35#include "send.h"
36#include "soft-interface.h"
37#include "tp_meter.h"
38#include "translation-table.h"
39#include "tvlv.h"
40
41static int batadv_route_unicast_packet(struct sk_buff *skb,
42 struct batadv_hard_iface *recv_if);
43
44
45
46
47
48
49
50
51
52
53static void _batadv_update_route(struct batadv_priv *bat_priv,
54 struct batadv_orig_node *orig_node,
55 struct batadv_hard_iface *recv_if,
56 struct batadv_neigh_node *neigh_node)
57{
58 struct batadv_orig_ifinfo *orig_ifinfo;
59 struct batadv_neigh_node *curr_router;
60
61 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, recv_if);
62 if (!orig_ifinfo)
63 return;
64
65 spin_lock_bh(&orig_node->neigh_list_lock);
66
67
68
69
70
71
72
73
74
75 if (neigh_node)
76 kref_get(&neigh_node->refcount);
77
78 curr_router = rcu_replace_pointer(orig_ifinfo->router, neigh_node,
79 true);
80 spin_unlock_bh(&orig_node->neigh_list_lock);
81 batadv_orig_ifinfo_put(orig_ifinfo);
82
83
84 if (curr_router && !neigh_node) {
85 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
86 "Deleting route towards: %pM\n", orig_node->orig);
87 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
88 "Deleted route towards originator");
89
90
91 } else if (!curr_router && neigh_node) {
92 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
93 "Adding route towards: %pM (via %pM)\n",
94 orig_node->orig, neigh_node->addr);
95
96 } else if (neigh_node && curr_router) {
97 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
98 "Changing route towards: %pM (now via %pM - was via %pM)\n",
99 orig_node->orig, neigh_node->addr,
100 curr_router->addr);
101 }
102
103
104 batadv_neigh_node_put(curr_router);
105}
106
107
108
109
110
111
112
113
114void batadv_update_route(struct batadv_priv *bat_priv,
115 struct batadv_orig_node *orig_node,
116 struct batadv_hard_iface *recv_if,
117 struct batadv_neigh_node *neigh_node)
118{
119 struct batadv_neigh_node *router = NULL;
120
121 if (!orig_node)
122 goto out;
123
124 router = batadv_orig_router_get(orig_node, recv_if);
125
126 if (router != neigh_node)
127 _batadv_update_route(bat_priv, orig_node, recv_if, neigh_node);
128
129out:
130 batadv_neigh_node_put(router);
131}
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149bool batadv_window_protected(struct batadv_priv *bat_priv, s32 seq_num_diff,
150 s32 seq_old_max_diff, unsigned long *last_reset,
151 bool *protection_started)
152{
153 if (seq_num_diff <= -seq_old_max_diff ||
154 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
155 if (!batadv_has_timed_out(*last_reset,
156 BATADV_RESET_PROTECTION_MS))
157 return true;
158
159 *last_reset = jiffies;
160 if (protection_started)
161 *protection_started = true;
162 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
163 "old packet received, start protection\n");
164 }
165
166 return false;
167}
168
169
170
171
172
173
174
175
176
177bool batadv_check_management_packet(struct sk_buff *skb,
178 struct batadv_hard_iface *hard_iface,
179 int header_len)
180{
181 struct ethhdr *ethhdr;
182
183
184 if (unlikely(!pskb_may_pull(skb, header_len)))
185 return false;
186
187 ethhdr = eth_hdr(skb);
188
189
190 if (!is_broadcast_ether_addr(ethhdr->h_dest))
191 return false;
192
193
194 if (!is_valid_ether_addr(ethhdr->h_source))
195 return false;
196
197
198 if (skb_cow(skb, 0) < 0)
199 return false;
200
201
202 if (skb_linearize(skb) < 0)
203 return false;
204
205 return true;
206}
207
208
209
210
211
212
213
214
215
216static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
217 struct sk_buff *skb)
218{
219 struct batadv_hard_iface *primary_if = NULL;
220 struct batadv_orig_node *orig_node = NULL;
221 struct batadv_icmp_header *icmph;
222 int res, ret = NET_RX_DROP;
223
224 icmph = (struct batadv_icmp_header *)skb->data;
225
226 switch (icmph->msg_type) {
227 case BATADV_ECHO_REQUEST:
228
229 primary_if = batadv_primary_if_get_selected(bat_priv);
230 if (!primary_if)
231 goto out;
232
233
234 orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
235 if (!orig_node)
236 goto out;
237
238
239 if (skb_cow(skb, ETH_HLEN) < 0)
240 goto out;
241
242 icmph = (struct batadv_icmp_header *)skb->data;
243
244 ether_addr_copy(icmph->dst, icmph->orig);
245 ether_addr_copy(icmph->orig, primary_if->net_dev->dev_addr);
246 icmph->msg_type = BATADV_ECHO_REPLY;
247 icmph->ttl = BATADV_TTL;
248
249 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
250 if (res == NET_XMIT_SUCCESS)
251 ret = NET_RX_SUCCESS;
252
253
254 skb = NULL;
255 break;
256 case BATADV_TP:
257 if (!pskb_may_pull(skb, sizeof(struct batadv_icmp_tp_packet)))
258 goto out;
259
260 batadv_tp_meter_recv(bat_priv, skb);
261 ret = NET_RX_SUCCESS;
262
263 skb = NULL;
264 goto out;
265 default:
266
267 goto out;
268 }
269out:
270 batadv_hardif_put(primary_if);
271 batadv_orig_node_put(orig_node);
272
273 kfree_skb(skb);
274
275 return ret;
276}
277
278static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
279 struct sk_buff *skb)
280{
281 struct batadv_hard_iface *primary_if = NULL;
282 struct batadv_orig_node *orig_node = NULL;
283 struct batadv_icmp_packet *icmp_packet;
284 int res, ret = NET_RX_DROP;
285
286 icmp_packet = (struct batadv_icmp_packet *)skb->data;
287
288
289 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
290 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
291 icmp_packet->orig, icmp_packet->dst);
292 goto out;
293 }
294
295 primary_if = batadv_primary_if_get_selected(bat_priv);
296 if (!primary_if)
297 goto out;
298
299
300 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
301 if (!orig_node)
302 goto out;
303
304
305 if (skb_cow(skb, ETH_HLEN) < 0)
306 goto out;
307
308 icmp_packet = (struct batadv_icmp_packet *)skb->data;
309
310 ether_addr_copy(icmp_packet->dst, icmp_packet->orig);
311 ether_addr_copy(icmp_packet->orig, primary_if->net_dev->dev_addr);
312 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
313 icmp_packet->ttl = BATADV_TTL;
314
315 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
316 if (res == NET_RX_SUCCESS)
317 ret = NET_XMIT_SUCCESS;
318
319
320 skb = NULL;
321
322out:
323 batadv_hardif_put(primary_if);
324 batadv_orig_node_put(orig_node);
325
326 kfree_skb(skb);
327
328 return ret;
329}
330
331
332
333
334
335
336
337
338int batadv_recv_icmp_packet(struct sk_buff *skb,
339 struct batadv_hard_iface *recv_if)
340{
341 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
342 struct batadv_icmp_header *icmph;
343 struct batadv_icmp_packet_rr *icmp_packet_rr;
344 struct ethhdr *ethhdr;
345 struct batadv_orig_node *orig_node = NULL;
346 int hdr_size = sizeof(struct batadv_icmp_header);
347 int res, ret = NET_RX_DROP;
348
349
350 if (unlikely(!pskb_may_pull(skb, hdr_size)))
351 goto free_skb;
352
353 ethhdr = eth_hdr(skb);
354
355
356 if (!is_valid_ether_addr(ethhdr->h_dest))
357 goto free_skb;
358
359
360 if (is_multicast_ether_addr(ethhdr->h_source))
361 goto free_skb;
362
363
364 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
365 goto free_skb;
366
367 icmph = (struct batadv_icmp_header *)skb->data;
368
369
370 if ((icmph->msg_type == BATADV_ECHO_REPLY ||
371 icmph->msg_type == BATADV_ECHO_REQUEST) &&
372 skb->len >= sizeof(struct batadv_icmp_packet_rr)) {
373 if (skb_linearize(skb) < 0)
374 goto free_skb;
375
376
377 if (skb_cow(skb, ETH_HLEN) < 0)
378 goto free_skb;
379
380 ethhdr = eth_hdr(skb);
381 icmph = (struct batadv_icmp_header *)skb->data;
382 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
383 if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
384 goto free_skb;
385
386 ether_addr_copy(icmp_packet_rr->rr[icmp_packet_rr->rr_cur],
387 ethhdr->h_dest);
388 icmp_packet_rr->rr_cur++;
389 }
390
391
392 if (batadv_is_my_mac(bat_priv, icmph->dst))
393 return batadv_recv_my_icmp_packet(bat_priv, skb);
394
395
396 if (icmph->ttl < 2)
397 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
398
399
400 orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
401 if (!orig_node)
402 goto free_skb;
403
404
405 if (skb_cow(skb, ETH_HLEN) < 0)
406 goto put_orig_node;
407
408 icmph = (struct batadv_icmp_header *)skb->data;
409
410
411 icmph->ttl--;
412
413
414 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
415 if (res == NET_XMIT_SUCCESS)
416 ret = NET_RX_SUCCESS;
417
418
419 skb = NULL;
420
421put_orig_node:
422 batadv_orig_node_put(orig_node);
423free_skb:
424 kfree_skb(skb);
425
426 return ret;
427}
428
429
430
431
432
433
434
435
436
437
438
439
440
441static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
442 struct sk_buff *skb, int hdr_size)
443{
444 struct ethhdr *ethhdr;
445
446
447 if (unlikely(!pskb_may_pull(skb, hdr_size)))
448 return -ENODATA;
449
450 ethhdr = eth_hdr(skb);
451
452
453 if (!is_valid_ether_addr(ethhdr->h_dest))
454 return -EBADR;
455
456
457 if (is_multicast_ether_addr(ethhdr->h_source))
458 return -EBADR;
459
460
461 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
462 return -EREMOTE;
463
464 return 0;
465}
466
467
468
469
470
471
472
473
474
475static struct batadv_orig_ifinfo *
476batadv_last_bonding_get(struct batadv_orig_node *orig_node)
477{
478 struct batadv_orig_ifinfo *last_bonding_candidate;
479
480 spin_lock_bh(&orig_node->neigh_list_lock);
481 last_bonding_candidate = orig_node->last_bonding_candidate;
482
483 if (last_bonding_candidate)
484 kref_get(&last_bonding_candidate->refcount);
485 spin_unlock_bh(&orig_node->neigh_list_lock);
486
487 return last_bonding_candidate;
488}
489
490
491
492
493
494
495static void
496batadv_last_bonding_replace(struct batadv_orig_node *orig_node,
497 struct batadv_orig_ifinfo *new_candidate)
498{
499 struct batadv_orig_ifinfo *old_candidate;
500
501 spin_lock_bh(&orig_node->neigh_list_lock);
502 old_candidate = orig_node->last_bonding_candidate;
503
504 if (new_candidate)
505 kref_get(&new_candidate->refcount);
506 orig_node->last_bonding_candidate = new_candidate;
507 spin_unlock_bh(&orig_node->neigh_list_lock);
508
509 batadv_orig_ifinfo_put(old_candidate);
510}
511
512
513
514
515
516
517
518
519
520
521struct batadv_neigh_node *
522batadv_find_router(struct batadv_priv *bat_priv,
523 struct batadv_orig_node *orig_node,
524 struct batadv_hard_iface *recv_if)
525{
526 struct batadv_algo_ops *bao = bat_priv->algo_ops;
527 struct batadv_neigh_node *first_candidate_router = NULL;
528 struct batadv_neigh_node *next_candidate_router = NULL;
529 struct batadv_neigh_node *router, *cand_router = NULL;
530 struct batadv_neigh_node *last_cand_router = NULL;
531 struct batadv_orig_ifinfo *cand, *first_candidate = NULL;
532 struct batadv_orig_ifinfo *next_candidate = NULL;
533 struct batadv_orig_ifinfo *last_candidate;
534 bool last_candidate_found = false;
535
536 if (!orig_node)
537 return NULL;
538
539 router = batadv_orig_router_get(orig_node, recv_if);
540
541 if (!router)
542 return router;
543
544
545
546
547 if (!(recv_if == BATADV_IF_DEFAULT && atomic_read(&bat_priv->bonding)))
548 return router;
549
550
551
552
553
554
555
556
557
558 rcu_read_lock();
559 last_candidate = batadv_last_bonding_get(orig_node);
560 if (last_candidate)
561 last_cand_router = rcu_dereference(last_candidate->router);
562
563 hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
564
565 if (!kref_get_unless_zero(&cand->refcount))
566 continue;
567
568 cand_router = rcu_dereference(cand->router);
569 if (!cand_router)
570 goto next;
571
572 if (!kref_get_unless_zero(&cand_router->refcount)) {
573 cand_router = NULL;
574 goto next;
575 }
576
577
578
579
580 if (!bao->neigh.is_similar_or_better(cand_router,
581 cand->if_outgoing, router,
582 recv_if))
583 goto next;
584
585
586 if (last_cand_router == cand_router)
587 goto next;
588
589
590 if (!first_candidate) {
591 kref_get(&cand_router->refcount);
592 kref_get(&cand->refcount);
593 first_candidate = cand;
594 first_candidate_router = cand_router;
595 }
596
597
598
599
600
601 if (!last_candidate || last_candidate_found) {
602 next_candidate = cand;
603 next_candidate_router = cand_router;
604 break;
605 }
606
607 if (last_candidate == cand)
608 last_candidate_found = true;
609next:
610
611 if (cand_router) {
612 batadv_neigh_node_put(cand_router);
613 cand_router = NULL;
614 }
615 batadv_orig_ifinfo_put(cand);
616 }
617 rcu_read_unlock();
618
619
620
621
622
623
624 if (next_candidate) {
625 batadv_neigh_node_put(router);
626
627 kref_get(&next_candidate_router->refcount);
628 router = next_candidate_router;
629 batadv_last_bonding_replace(orig_node, next_candidate);
630 } else if (first_candidate) {
631 batadv_neigh_node_put(router);
632
633 kref_get(&first_candidate_router->refcount);
634 router = first_candidate_router;
635 batadv_last_bonding_replace(orig_node, first_candidate);
636 } else {
637 batadv_last_bonding_replace(orig_node, NULL);
638 }
639
640
641 if (first_candidate) {
642 batadv_neigh_node_put(first_candidate_router);
643 batadv_orig_ifinfo_put(first_candidate);
644 }
645
646 if (next_candidate) {
647 batadv_neigh_node_put(next_candidate_router);
648 batadv_orig_ifinfo_put(next_candidate);
649 }
650
651 batadv_orig_ifinfo_put(last_candidate);
652
653 return router;
654}
655
656static int batadv_route_unicast_packet(struct sk_buff *skb,
657 struct batadv_hard_iface *recv_if)
658{
659 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
660 struct batadv_orig_node *orig_node = NULL;
661 struct batadv_unicast_packet *unicast_packet;
662 struct ethhdr *ethhdr = eth_hdr(skb);
663 int res, hdr_len, ret = NET_RX_DROP;
664 unsigned int len;
665
666 unicast_packet = (struct batadv_unicast_packet *)skb->data;
667
668
669 if (unicast_packet->ttl < 2) {
670 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
671 ethhdr->h_source, unicast_packet->dest);
672 goto free_skb;
673 }
674
675
676 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
677
678 if (!orig_node)
679 goto free_skb;
680
681
682 if (skb_cow(skb, ETH_HLEN) < 0)
683 goto put_orig_node;
684
685
686 unicast_packet = (struct batadv_unicast_packet *)skb->data;
687 unicast_packet->ttl--;
688
689 switch (unicast_packet->packet_type) {
690 case BATADV_UNICAST_4ADDR:
691 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
692 break;
693 case BATADV_UNICAST:
694 hdr_len = sizeof(struct batadv_unicast_packet);
695 break;
696 default:
697
698 hdr_len = -1;
699 break;
700 }
701
702 if (hdr_len > 0)
703 batadv_skb_set_priority(skb, hdr_len);
704
705 len = skb->len;
706 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
707
708
709 if (res == NET_XMIT_SUCCESS) {
710 ret = NET_RX_SUCCESS;
711
712 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
713 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
714 len + ETH_HLEN);
715 }
716
717
718 skb = NULL;
719
720put_orig_node:
721 batadv_orig_node_put(orig_node);
722free_skb:
723 kfree_skb(skb);
724
725 return ret;
726}
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742static bool
743batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
744 struct batadv_unicast_packet *unicast_packet,
745 u8 *dst_addr, unsigned short vid)
746{
747 struct batadv_orig_node *orig_node = NULL;
748 struct batadv_hard_iface *primary_if = NULL;
749 bool ret = false;
750 u8 *orig_addr, orig_ttvn;
751
752 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
753 primary_if = batadv_primary_if_get_selected(bat_priv);
754 if (!primary_if)
755 goto out;
756 orig_addr = primary_if->net_dev->dev_addr;
757 orig_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
758 } else {
759 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
760 vid);
761 if (!orig_node)
762 goto out;
763
764 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
765 goto out;
766
767 orig_addr = orig_node->orig;
768 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
769 }
770
771
772 skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
773 ether_addr_copy(unicast_packet->dest, orig_addr);
774 unicast_packet->ttvn = orig_ttvn;
775 skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
776
777 ret = true;
778out:
779 batadv_hardif_put(primary_if);
780 batadv_orig_node_put(orig_node);
781
782 return ret;
783}
784
785static bool batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
786 struct sk_buff *skb, int hdr_len)
787{
788 struct batadv_unicast_packet *unicast_packet;
789 struct batadv_hard_iface *primary_if;
790 struct batadv_orig_node *orig_node;
791 u8 curr_ttvn, old_ttvn;
792 struct ethhdr *ethhdr;
793 unsigned short vid;
794 int is_old_ttvn;
795
796
797 if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
798 return false;
799
800
801 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
802 return false;
803
804 unicast_packet = (struct batadv_unicast_packet *)skb->data;
805 vid = batadv_get_vid(skb, hdr_len);
806 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
807
808
809 if (is_multicast_ether_addr(ethhdr->h_dest))
810 return true;
811
812
813
814
815
816
817 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
818 if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
819 ethhdr->h_dest, vid))
820 batadv_dbg_ratelimited(BATADV_DBG_TT,
821 bat_priv,
822 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
823 unicast_packet->dest,
824 ethhdr->h_dest);
825
826
827
828
829
830 return true;
831 }
832
833
834
835
836
837 curr_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
838 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
839 orig_node = batadv_orig_hash_find(bat_priv,
840 unicast_packet->dest);
841
842
843
844
845 if (!orig_node)
846 return false;
847
848 curr_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
849 batadv_orig_node_put(orig_node);
850 }
851
852
853
854
855 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
856 if (!is_old_ttvn)
857 return true;
858
859 old_ttvn = unicast_packet->ttvn;
860
861
862
863
864 if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
865 ethhdr->h_dest, vid)) {
866 batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
867 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
868 unicast_packet->dest, ethhdr->h_dest,
869 old_ttvn, curr_ttvn);
870 return true;
871 }
872
873
874
875
876
877 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
878 return false;
879
880
881
882
883 primary_if = batadv_primary_if_get_selected(bat_priv);
884 if (!primary_if)
885 return false;
886
887
888 skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
889 ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
890 unicast_packet->ttvn = curr_ttvn;
891 skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
892
893 batadv_hardif_put(primary_if);
894
895 return true;
896}
897
898
899
900
901
902
903
904
905
906
907int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
908 struct batadv_hard_iface *recv_if)
909{
910 struct batadv_unicast_packet *unicast_packet;
911 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
912 int check, hdr_size = sizeof(*unicast_packet);
913
914 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
915 if (check < 0)
916 goto free_skb;
917
918
919 unicast_packet = (struct batadv_unicast_packet *)skb->data;
920 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
921 goto free_skb;
922
923 return batadv_route_unicast_packet(skb, recv_if);
924
925free_skb:
926 kfree_skb(skb);
927 return NET_RX_DROP;
928}
929
930
931
932
933
934
935
936
937int batadv_recv_unicast_packet(struct sk_buff *skb,
938 struct batadv_hard_iface *recv_if)
939{
940 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
941 struct batadv_unicast_packet *unicast_packet;
942 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
943 u8 *orig_addr, *orig_addr_gw;
944 struct batadv_orig_node *orig_node = NULL, *orig_node_gw = NULL;
945 int check, hdr_size = sizeof(*unicast_packet);
946 enum batadv_subtype subtype;
947 int ret = NET_RX_DROP;
948 bool is4addr, is_gw;
949
950 unicast_packet = (struct batadv_unicast_packet *)skb->data;
951 is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
952
953 if (is4addr)
954 hdr_size = sizeof(*unicast_4addr_packet);
955
956
957 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
958
959
960
961
962 if (check == -EREMOTE)
963 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
964
965 if (check < 0)
966 goto free_skb;
967 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
968 goto free_skb;
969
970 unicast_packet = (struct batadv_unicast_packet *)skb->data;
971
972
973 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
974
975
976
977 orig_addr_gw = eth_hdr(skb)->h_source;
978 orig_node_gw = batadv_orig_hash_find(bat_priv, orig_addr_gw);
979 if (orig_node_gw) {
980 is_gw = batadv_bla_is_backbone_gw(skb, orig_node_gw,
981 hdr_size);
982 batadv_orig_node_put(orig_node_gw);
983 if (is_gw) {
984 batadv_dbg(BATADV_DBG_BLA, bat_priv,
985 "%s(): Dropped unicast pkt received from another backbone gw %pM.\n",
986 __func__, orig_addr_gw);
987 goto free_skb;
988 }
989 }
990
991 if (is4addr) {
992 unicast_4addr_packet =
993 (struct batadv_unicast_4addr_packet *)skb->data;
994 subtype = unicast_4addr_packet->subtype;
995 batadv_dat_inc_counter(bat_priv, subtype);
996
997
998
999
1000
1001
1002
1003 if (subtype == BATADV_P_DATA) {
1004 orig_addr = unicast_4addr_packet->src;
1005 orig_node = batadv_orig_hash_find(bat_priv,
1006 orig_addr);
1007 }
1008 }
1009
1010 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
1011 hdr_size))
1012 goto rx_success;
1013 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
1014 hdr_size))
1015 goto rx_success;
1016
1017 batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1018
1019 batadv_interface_rx(recv_if->soft_iface, skb, hdr_size,
1020 orig_node);
1021
1022rx_success:
1023 batadv_orig_node_put(orig_node);
1024
1025 return NET_RX_SUCCESS;
1026 }
1027
1028 ret = batadv_route_unicast_packet(skb, recv_if);
1029
1030 skb = NULL;
1031
1032free_skb:
1033 kfree_skb(skb);
1034
1035 return ret;
1036}
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046int batadv_recv_unicast_tvlv(struct sk_buff *skb,
1047 struct batadv_hard_iface *recv_if)
1048{
1049 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1050 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1051 unsigned char *tvlv_buff;
1052 u16 tvlv_buff_len;
1053 int hdr_size = sizeof(*unicast_tvlv_packet);
1054 int ret = NET_RX_DROP;
1055
1056 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
1057 goto free_skb;
1058
1059
1060 if (skb_cow(skb, hdr_size) < 0)
1061 goto free_skb;
1062
1063
1064 if (skb_linearize(skb) < 0)
1065 goto free_skb;
1066
1067 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
1068
1069 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1070 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1071
1072 if (tvlv_buff_len > skb->len - hdr_size)
1073 goto free_skb;
1074
1075 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
1076 unicast_tvlv_packet->src,
1077 unicast_tvlv_packet->dst,
1078 tvlv_buff, tvlv_buff_len);
1079
1080 if (ret != NET_RX_SUCCESS) {
1081 ret = batadv_route_unicast_packet(skb, recv_if);
1082
1083 skb = NULL;
1084 }
1085
1086free_skb:
1087 kfree_skb(skb);
1088
1089 return ret;
1090}
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103int batadv_recv_frag_packet(struct sk_buff *skb,
1104 struct batadv_hard_iface *recv_if)
1105{
1106 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1107 struct batadv_orig_node *orig_node_src = NULL;
1108 struct batadv_frag_packet *frag_packet;
1109 int ret = NET_RX_DROP;
1110
1111 if (batadv_check_unicast_packet(bat_priv, skb,
1112 sizeof(*frag_packet)) < 0)
1113 goto free_skb;
1114
1115 frag_packet = (struct batadv_frag_packet *)skb->data;
1116 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
1117 if (!orig_node_src)
1118 goto free_skb;
1119
1120 skb->priority = frag_packet->priority + 256;
1121
1122
1123 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
1124 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
1125
1126 skb = NULL;
1127 ret = NET_RX_SUCCESS;
1128 goto put_orig_node;
1129 }
1130
1131 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
1132 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
1133
1134
1135 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
1136 goto put_orig_node;
1137
1138
1139
1140
1141 if (skb) {
1142 batadv_batman_skb_recv(skb, recv_if->net_dev,
1143 &recv_if->batman_adv_ptype, NULL);
1144
1145 skb = NULL;
1146 }
1147
1148 ret = NET_RX_SUCCESS;
1149
1150put_orig_node:
1151 batadv_orig_node_put(orig_node_src);
1152free_skb:
1153 kfree_skb(skb);
1154
1155 return ret;
1156}
1157
1158
1159
1160
1161
1162
1163
1164
1165int batadv_recv_bcast_packet(struct sk_buff *skb,
1166 struct batadv_hard_iface *recv_if)
1167{
1168 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1169 struct batadv_orig_node *orig_node = NULL;
1170 struct batadv_bcast_packet *bcast_packet;
1171 struct ethhdr *ethhdr;
1172 int hdr_size = sizeof(*bcast_packet);
1173 s32 seq_diff;
1174 u32 seqno;
1175 int ret;
1176
1177
1178 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1179 goto free_skb;
1180
1181 ethhdr = eth_hdr(skb);
1182
1183
1184 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1185 goto free_skb;
1186
1187
1188 if (is_multicast_ether_addr(ethhdr->h_source))
1189 goto free_skb;
1190
1191
1192 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
1193 goto free_skb;
1194
1195 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1196
1197
1198 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
1199 goto free_skb;
1200
1201 if (bcast_packet->ttl-- < 2)
1202 goto free_skb;
1203
1204 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1205
1206 if (!orig_node)
1207 goto free_skb;
1208
1209 spin_lock_bh(&orig_node->bcast_seqno_lock);
1210
1211 seqno = ntohl(bcast_packet->seqno);
1212
1213 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1214 seqno))
1215 goto spin_unlock;
1216
1217 seq_diff = seqno - orig_node->last_bcast_seqno;
1218
1219
1220 if (batadv_window_protected(bat_priv, seq_diff,
1221 BATADV_BCAST_MAX_AGE,
1222 &orig_node->bcast_seqno_reset, NULL))
1223 goto spin_unlock;
1224
1225
1226
1227
1228 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1229 orig_node->last_bcast_seqno = seqno;
1230
1231 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1232
1233
1234 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
1235 goto free_skb;
1236
1237 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1238
1239
1240 ret = batadv_forw_bcast_packet(bat_priv, skb, 0, false);
1241 if (ret == NETDEV_TX_BUSY)
1242 goto free_skb;
1243
1244
1245
1246
1247 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1248 goto free_skb;
1249
1250 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1251 goto rx_success;
1252 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1253 goto rx_success;
1254
1255 batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1256
1257
1258 batadv_interface_rx(recv_if->soft_iface, skb, hdr_size, orig_node);
1259
1260rx_success:
1261 ret = NET_RX_SUCCESS;
1262 goto out;
1263
1264spin_unlock:
1265 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1266free_skb:
1267 kfree_skb(skb);
1268 ret = NET_RX_DROP;
1269out:
1270 batadv_orig_node_put(orig_node);
1271 return ret;
1272}
1273