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