0001
0002
0003
0004
0005
0006
0007 #include "routing.h"
0008 #include "main.h"
0009
0010 #include <linux/atomic.h>
0011 #include <linux/byteorder/generic.h>
0012 #include <linux/compiler.h>
0013 #include <linux/errno.h>
0014 #include <linux/etherdevice.h>
0015 #include <linux/if_ether.h>
0016 #include <linux/jiffies.h>
0017 #include <linux/kref.h>
0018 #include <linux/netdevice.h>
0019 #include <linux/printk.h>
0020 #include <linux/rculist.h>
0021 #include <linux/rcupdate.h>
0022 #include <linux/skbuff.h>
0023 #include <linux/spinlock.h>
0024 #include <linux/stddef.h>
0025 #include <uapi/linux/batadv_packet.h>
0026
0027 #include "bitarray.h"
0028 #include "bridge_loop_avoidance.h"
0029 #include "distributed-arp-table.h"
0030 #include "fragmentation.h"
0031 #include "hard-interface.h"
0032 #include "log.h"
0033 #include "network-coding.h"
0034 #include "originator.h"
0035 #include "send.h"
0036 #include "soft-interface.h"
0037 #include "tp_meter.h"
0038 #include "translation-table.h"
0039 #include "tvlv.h"
0040
0041 static int batadv_route_unicast_packet(struct sk_buff *skb,
0042 struct batadv_hard_iface *recv_if);
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 static void _batadv_update_route(struct batadv_priv *bat_priv,
0054 struct batadv_orig_node *orig_node,
0055 struct batadv_hard_iface *recv_if,
0056 struct batadv_neigh_node *neigh_node)
0057 {
0058 struct batadv_orig_ifinfo *orig_ifinfo;
0059 struct batadv_neigh_node *curr_router;
0060
0061 orig_ifinfo = batadv_orig_ifinfo_get(orig_node, recv_if);
0062 if (!orig_ifinfo)
0063 return;
0064
0065 spin_lock_bh(&orig_node->neigh_list_lock);
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075 if (neigh_node)
0076 kref_get(&neigh_node->refcount);
0077
0078 curr_router = rcu_replace_pointer(orig_ifinfo->router, neigh_node,
0079 true);
0080 spin_unlock_bh(&orig_node->neigh_list_lock);
0081 batadv_orig_ifinfo_put(orig_ifinfo);
0082
0083
0084 if (curr_router && !neigh_node) {
0085 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
0086 "Deleting route towards: %pM\n", orig_node->orig);
0087 batadv_tt_global_del_orig(bat_priv, orig_node, -1,
0088 "Deleted route towards originator");
0089
0090
0091 } else if (!curr_router && neigh_node) {
0092 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
0093 "Adding route towards: %pM (via %pM)\n",
0094 orig_node->orig, neigh_node->addr);
0095
0096 } else if (neigh_node && curr_router) {
0097 batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
0098 "Changing route towards: %pM (now via %pM - was via %pM)\n",
0099 orig_node->orig, neigh_node->addr,
0100 curr_router->addr);
0101 }
0102
0103
0104 batadv_neigh_node_put(curr_router);
0105 }
0106
0107
0108
0109
0110
0111
0112
0113
0114 void batadv_update_route(struct batadv_priv *bat_priv,
0115 struct batadv_orig_node *orig_node,
0116 struct batadv_hard_iface *recv_if,
0117 struct batadv_neigh_node *neigh_node)
0118 {
0119 struct batadv_neigh_node *router = NULL;
0120
0121 if (!orig_node)
0122 goto out;
0123
0124 router = batadv_orig_router_get(orig_node, recv_if);
0125
0126 if (router != neigh_node)
0127 _batadv_update_route(bat_priv, orig_node, recv_if, neigh_node);
0128
0129 out:
0130 batadv_neigh_node_put(router);
0131 }
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149 bool batadv_window_protected(struct batadv_priv *bat_priv, s32 seq_num_diff,
0150 s32 seq_old_max_diff, unsigned long *last_reset,
0151 bool *protection_started)
0152 {
0153 if (seq_num_diff <= -seq_old_max_diff ||
0154 seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
0155 if (!batadv_has_timed_out(*last_reset,
0156 BATADV_RESET_PROTECTION_MS))
0157 return true;
0158
0159 *last_reset = jiffies;
0160 if (protection_started)
0161 *protection_started = true;
0162 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
0163 "old packet received, start protection\n");
0164 }
0165
0166 return false;
0167 }
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 bool batadv_check_management_packet(struct sk_buff *skb,
0178 struct batadv_hard_iface *hard_iface,
0179 int header_len)
0180 {
0181 struct ethhdr *ethhdr;
0182
0183
0184 if (unlikely(!pskb_may_pull(skb, header_len)))
0185 return false;
0186
0187 ethhdr = eth_hdr(skb);
0188
0189
0190 if (!is_broadcast_ether_addr(ethhdr->h_dest))
0191 return false;
0192
0193
0194 if (!is_valid_ether_addr(ethhdr->h_source))
0195 return false;
0196
0197
0198 if (skb_cow(skb, 0) < 0)
0199 return false;
0200
0201
0202 if (skb_linearize(skb) < 0)
0203 return false;
0204
0205 return true;
0206 }
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216 static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
0217 struct sk_buff *skb)
0218 {
0219 struct batadv_hard_iface *primary_if = NULL;
0220 struct batadv_orig_node *orig_node = NULL;
0221 struct batadv_icmp_header *icmph;
0222 int res, ret = NET_RX_DROP;
0223
0224 icmph = (struct batadv_icmp_header *)skb->data;
0225
0226 switch (icmph->msg_type) {
0227 case BATADV_ECHO_REQUEST:
0228
0229 primary_if = batadv_primary_if_get_selected(bat_priv);
0230 if (!primary_if)
0231 goto out;
0232
0233
0234 orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
0235 if (!orig_node)
0236 goto out;
0237
0238
0239 if (skb_cow(skb, ETH_HLEN) < 0)
0240 goto out;
0241
0242 icmph = (struct batadv_icmp_header *)skb->data;
0243
0244 ether_addr_copy(icmph->dst, icmph->orig);
0245 ether_addr_copy(icmph->orig, primary_if->net_dev->dev_addr);
0246 icmph->msg_type = BATADV_ECHO_REPLY;
0247 icmph->ttl = BATADV_TTL;
0248
0249 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
0250 if (res == NET_XMIT_SUCCESS)
0251 ret = NET_RX_SUCCESS;
0252
0253
0254 skb = NULL;
0255 break;
0256 case BATADV_TP:
0257 if (!pskb_may_pull(skb, sizeof(struct batadv_icmp_tp_packet)))
0258 goto out;
0259
0260 batadv_tp_meter_recv(bat_priv, skb);
0261 ret = NET_RX_SUCCESS;
0262
0263 skb = NULL;
0264 goto out;
0265 default:
0266
0267 goto out;
0268 }
0269 out:
0270 batadv_hardif_put(primary_if);
0271 batadv_orig_node_put(orig_node);
0272
0273 kfree_skb(skb);
0274
0275 return ret;
0276 }
0277
0278 static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
0279 struct sk_buff *skb)
0280 {
0281 struct batadv_hard_iface *primary_if = NULL;
0282 struct batadv_orig_node *orig_node = NULL;
0283 struct batadv_icmp_packet *icmp_packet;
0284 int res, ret = NET_RX_DROP;
0285
0286 icmp_packet = (struct batadv_icmp_packet *)skb->data;
0287
0288
0289 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
0290 pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
0291 icmp_packet->orig, icmp_packet->dst);
0292 goto out;
0293 }
0294
0295 primary_if = batadv_primary_if_get_selected(bat_priv);
0296 if (!primary_if)
0297 goto out;
0298
0299
0300 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
0301 if (!orig_node)
0302 goto out;
0303
0304
0305 if (skb_cow(skb, ETH_HLEN) < 0)
0306 goto out;
0307
0308 icmp_packet = (struct batadv_icmp_packet *)skb->data;
0309
0310 ether_addr_copy(icmp_packet->dst, icmp_packet->orig);
0311 ether_addr_copy(icmp_packet->orig, primary_if->net_dev->dev_addr);
0312 icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
0313 icmp_packet->ttl = BATADV_TTL;
0314
0315 res = batadv_send_skb_to_orig(skb, orig_node, NULL);
0316 if (res == NET_RX_SUCCESS)
0317 ret = NET_XMIT_SUCCESS;
0318
0319
0320 skb = NULL;
0321
0322 out:
0323 batadv_hardif_put(primary_if);
0324 batadv_orig_node_put(orig_node);
0325
0326 kfree_skb(skb);
0327
0328 return ret;
0329 }
0330
0331
0332
0333
0334
0335
0336
0337
0338 int batadv_recv_icmp_packet(struct sk_buff *skb,
0339 struct batadv_hard_iface *recv_if)
0340 {
0341 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
0342 struct batadv_icmp_header *icmph;
0343 struct batadv_icmp_packet_rr *icmp_packet_rr;
0344 struct ethhdr *ethhdr;
0345 struct batadv_orig_node *orig_node = NULL;
0346 int hdr_size = sizeof(struct batadv_icmp_header);
0347 int res, ret = NET_RX_DROP;
0348
0349
0350 if (unlikely(!pskb_may_pull(skb, hdr_size)))
0351 goto free_skb;
0352
0353 ethhdr = eth_hdr(skb);
0354
0355
0356 if (!is_valid_ether_addr(ethhdr->h_dest))
0357 goto free_skb;
0358
0359
0360 if (is_multicast_ether_addr(ethhdr->h_source))
0361 goto free_skb;
0362
0363
0364 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
0365 goto free_skb;
0366
0367 icmph = (struct batadv_icmp_header *)skb->data;
0368
0369
0370 if ((icmph->msg_type == BATADV_ECHO_REPLY ||
0371 icmph->msg_type == BATADV_ECHO_REQUEST) &&
0372 skb->len >= sizeof(struct batadv_icmp_packet_rr)) {
0373 if (skb_linearize(skb) < 0)
0374 goto free_skb;
0375
0376
0377 if (skb_cow(skb, ETH_HLEN) < 0)
0378 goto free_skb;
0379
0380 ethhdr = eth_hdr(skb);
0381 icmph = (struct batadv_icmp_header *)skb->data;
0382 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
0383 if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
0384 goto free_skb;
0385
0386 ether_addr_copy(icmp_packet_rr->rr[icmp_packet_rr->rr_cur],
0387 ethhdr->h_dest);
0388 icmp_packet_rr->rr_cur++;
0389 }
0390
0391
0392 if (batadv_is_my_mac(bat_priv, icmph->dst))
0393 return batadv_recv_my_icmp_packet(bat_priv, skb);
0394
0395
0396 if (icmph->ttl < 2)
0397 return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
0398
0399
0400 orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
0401 if (!orig_node)
0402 goto free_skb;
0403
0404
0405 if (skb_cow(skb, ETH_HLEN) < 0)
0406 goto put_orig_node;
0407
0408 icmph = (struct batadv_icmp_header *)skb->data;
0409
0410
0411 icmph->ttl--;
0412
0413
0414 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
0415 if (res == NET_XMIT_SUCCESS)
0416 ret = NET_RX_SUCCESS;
0417
0418
0419 skb = NULL;
0420
0421 put_orig_node:
0422 batadv_orig_node_put(orig_node);
0423 free_skb:
0424 kfree_skb(skb);
0425
0426 return ret;
0427 }
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441 static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
0442 struct sk_buff *skb, int hdr_size)
0443 {
0444 struct ethhdr *ethhdr;
0445
0446
0447 if (unlikely(!pskb_may_pull(skb, hdr_size)))
0448 return -ENODATA;
0449
0450 ethhdr = eth_hdr(skb);
0451
0452
0453 if (!is_valid_ether_addr(ethhdr->h_dest))
0454 return -EBADR;
0455
0456
0457 if (is_multicast_ether_addr(ethhdr->h_source))
0458 return -EBADR;
0459
0460
0461 if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
0462 return -EREMOTE;
0463
0464 return 0;
0465 }
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475 static struct batadv_orig_ifinfo *
0476 batadv_last_bonding_get(struct batadv_orig_node *orig_node)
0477 {
0478 struct batadv_orig_ifinfo *last_bonding_candidate;
0479
0480 spin_lock_bh(&orig_node->neigh_list_lock);
0481 last_bonding_candidate = orig_node->last_bonding_candidate;
0482
0483 if (last_bonding_candidate)
0484 kref_get(&last_bonding_candidate->refcount);
0485 spin_unlock_bh(&orig_node->neigh_list_lock);
0486
0487 return last_bonding_candidate;
0488 }
0489
0490
0491
0492
0493
0494
0495 static void
0496 batadv_last_bonding_replace(struct batadv_orig_node *orig_node,
0497 struct batadv_orig_ifinfo *new_candidate)
0498 {
0499 struct batadv_orig_ifinfo *old_candidate;
0500
0501 spin_lock_bh(&orig_node->neigh_list_lock);
0502 old_candidate = orig_node->last_bonding_candidate;
0503
0504 if (new_candidate)
0505 kref_get(&new_candidate->refcount);
0506 orig_node->last_bonding_candidate = new_candidate;
0507 spin_unlock_bh(&orig_node->neigh_list_lock);
0508
0509 batadv_orig_ifinfo_put(old_candidate);
0510 }
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521 struct batadv_neigh_node *
0522 batadv_find_router(struct batadv_priv *bat_priv,
0523 struct batadv_orig_node *orig_node,
0524 struct batadv_hard_iface *recv_if)
0525 {
0526 struct batadv_algo_ops *bao = bat_priv->algo_ops;
0527 struct batadv_neigh_node *first_candidate_router = NULL;
0528 struct batadv_neigh_node *next_candidate_router = NULL;
0529 struct batadv_neigh_node *router, *cand_router = NULL;
0530 struct batadv_neigh_node *last_cand_router = NULL;
0531 struct batadv_orig_ifinfo *cand, *first_candidate = NULL;
0532 struct batadv_orig_ifinfo *next_candidate = NULL;
0533 struct batadv_orig_ifinfo *last_candidate;
0534 bool last_candidate_found = false;
0535
0536 if (!orig_node)
0537 return NULL;
0538
0539 router = batadv_orig_router_get(orig_node, recv_if);
0540
0541 if (!router)
0542 return router;
0543
0544
0545
0546
0547 if (!(recv_if == BATADV_IF_DEFAULT && atomic_read(&bat_priv->bonding)))
0548 return router;
0549
0550
0551
0552
0553
0554
0555
0556
0557
0558 rcu_read_lock();
0559 last_candidate = batadv_last_bonding_get(orig_node);
0560 if (last_candidate)
0561 last_cand_router = rcu_dereference(last_candidate->router);
0562
0563 hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
0564
0565 if (!kref_get_unless_zero(&cand->refcount))
0566 continue;
0567
0568 cand_router = rcu_dereference(cand->router);
0569 if (!cand_router)
0570 goto next;
0571
0572 if (!kref_get_unless_zero(&cand_router->refcount)) {
0573 cand_router = NULL;
0574 goto next;
0575 }
0576
0577
0578
0579
0580 if (!bao->neigh.is_similar_or_better(cand_router,
0581 cand->if_outgoing, router,
0582 recv_if))
0583 goto next;
0584
0585
0586 if (last_cand_router == cand_router)
0587 goto next;
0588
0589
0590 if (!first_candidate) {
0591 kref_get(&cand_router->refcount);
0592 kref_get(&cand->refcount);
0593 first_candidate = cand;
0594 first_candidate_router = cand_router;
0595 }
0596
0597
0598
0599
0600
0601 if (!last_candidate || last_candidate_found) {
0602 next_candidate = cand;
0603 next_candidate_router = cand_router;
0604 break;
0605 }
0606
0607 if (last_candidate == cand)
0608 last_candidate_found = true;
0609 next:
0610
0611 if (cand_router) {
0612 batadv_neigh_node_put(cand_router);
0613 cand_router = NULL;
0614 }
0615 batadv_orig_ifinfo_put(cand);
0616 }
0617 rcu_read_unlock();
0618
0619
0620
0621
0622
0623
0624 if (next_candidate) {
0625 batadv_neigh_node_put(router);
0626
0627 kref_get(&next_candidate_router->refcount);
0628 router = next_candidate_router;
0629 batadv_last_bonding_replace(orig_node, next_candidate);
0630 } else if (first_candidate) {
0631 batadv_neigh_node_put(router);
0632
0633 kref_get(&first_candidate_router->refcount);
0634 router = first_candidate_router;
0635 batadv_last_bonding_replace(orig_node, first_candidate);
0636 } else {
0637 batadv_last_bonding_replace(orig_node, NULL);
0638 }
0639
0640
0641 if (first_candidate) {
0642 batadv_neigh_node_put(first_candidate_router);
0643 batadv_orig_ifinfo_put(first_candidate);
0644 }
0645
0646 if (next_candidate) {
0647 batadv_neigh_node_put(next_candidate_router);
0648 batadv_orig_ifinfo_put(next_candidate);
0649 }
0650
0651 batadv_orig_ifinfo_put(last_candidate);
0652
0653 return router;
0654 }
0655
0656 static int batadv_route_unicast_packet(struct sk_buff *skb,
0657 struct batadv_hard_iface *recv_if)
0658 {
0659 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
0660 struct batadv_orig_node *orig_node = NULL;
0661 struct batadv_unicast_packet *unicast_packet;
0662 struct ethhdr *ethhdr = eth_hdr(skb);
0663 int res, hdr_len, ret = NET_RX_DROP;
0664 unsigned int len;
0665
0666 unicast_packet = (struct batadv_unicast_packet *)skb->data;
0667
0668
0669 if (unicast_packet->ttl < 2) {
0670 pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
0671 ethhdr->h_source, unicast_packet->dest);
0672 goto free_skb;
0673 }
0674
0675
0676 orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
0677
0678 if (!orig_node)
0679 goto free_skb;
0680
0681
0682 if (skb_cow(skb, ETH_HLEN) < 0)
0683 goto put_orig_node;
0684
0685
0686 unicast_packet = (struct batadv_unicast_packet *)skb->data;
0687 unicast_packet->ttl--;
0688
0689 switch (unicast_packet->packet_type) {
0690 case BATADV_UNICAST_4ADDR:
0691 hdr_len = sizeof(struct batadv_unicast_4addr_packet);
0692 break;
0693 case BATADV_UNICAST:
0694 hdr_len = sizeof(struct batadv_unicast_packet);
0695 break;
0696 default:
0697
0698 hdr_len = -1;
0699 break;
0700 }
0701
0702 if (hdr_len > 0)
0703 batadv_skb_set_priority(skb, hdr_len);
0704
0705 len = skb->len;
0706 res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
0707
0708
0709 if (res == NET_XMIT_SUCCESS) {
0710 ret = NET_RX_SUCCESS;
0711
0712 batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
0713 batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
0714 len + ETH_HLEN);
0715 }
0716
0717
0718 skb = NULL;
0719
0720 put_orig_node:
0721 batadv_orig_node_put(orig_node);
0722 free_skb:
0723 kfree_skb(skb);
0724
0725 return ret;
0726 }
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742 static bool
0743 batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
0744 struct batadv_unicast_packet *unicast_packet,
0745 u8 *dst_addr, unsigned short vid)
0746 {
0747 struct batadv_orig_node *orig_node = NULL;
0748 struct batadv_hard_iface *primary_if = NULL;
0749 bool ret = false;
0750 const u8 *orig_addr;
0751 u8 orig_ttvn;
0752
0753 if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
0754 primary_if = batadv_primary_if_get_selected(bat_priv);
0755 if (!primary_if)
0756 goto out;
0757 orig_addr = primary_if->net_dev->dev_addr;
0758 orig_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
0759 } else {
0760 orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
0761 vid);
0762 if (!orig_node)
0763 goto out;
0764
0765 if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
0766 goto out;
0767
0768 orig_addr = orig_node->orig;
0769 orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
0770 }
0771
0772
0773 skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
0774 ether_addr_copy(unicast_packet->dest, orig_addr);
0775 unicast_packet->ttvn = orig_ttvn;
0776 skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
0777
0778 ret = true;
0779 out:
0780 batadv_hardif_put(primary_if);
0781 batadv_orig_node_put(orig_node);
0782
0783 return ret;
0784 }
0785
0786 static bool batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
0787 struct sk_buff *skb, int hdr_len)
0788 {
0789 struct batadv_unicast_packet *unicast_packet;
0790 struct batadv_hard_iface *primary_if;
0791 struct batadv_orig_node *orig_node;
0792 u8 curr_ttvn, old_ttvn;
0793 struct ethhdr *ethhdr;
0794 unsigned short vid;
0795 int is_old_ttvn;
0796
0797
0798 if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
0799 return false;
0800
0801
0802 if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
0803 return false;
0804
0805 unicast_packet = (struct batadv_unicast_packet *)skb->data;
0806 vid = batadv_get_vid(skb, hdr_len);
0807 ethhdr = (struct ethhdr *)(skb->data + hdr_len);
0808
0809
0810 if (is_multicast_ether_addr(ethhdr->h_dest))
0811 return true;
0812
0813
0814
0815
0816
0817
0818 if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
0819 if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
0820 ethhdr->h_dest, vid))
0821 batadv_dbg_ratelimited(BATADV_DBG_TT,
0822 bat_priv,
0823 "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
0824 unicast_packet->dest,
0825 ethhdr->h_dest);
0826
0827
0828
0829
0830
0831 return true;
0832 }
0833
0834
0835
0836
0837
0838 curr_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
0839 if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
0840 orig_node = batadv_orig_hash_find(bat_priv,
0841 unicast_packet->dest);
0842
0843
0844
0845
0846 if (!orig_node)
0847 return false;
0848
0849 curr_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
0850 batadv_orig_node_put(orig_node);
0851 }
0852
0853
0854
0855
0856 is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
0857 if (!is_old_ttvn)
0858 return true;
0859
0860 old_ttvn = unicast_packet->ttvn;
0861
0862
0863
0864
0865 if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
0866 ethhdr->h_dest, vid)) {
0867 batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
0868 "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
0869 unicast_packet->dest, ethhdr->h_dest,
0870 old_ttvn, curr_ttvn);
0871 return true;
0872 }
0873
0874
0875
0876
0877
0878 if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
0879 return false;
0880
0881
0882
0883
0884 primary_if = batadv_primary_if_get_selected(bat_priv);
0885 if (!primary_if)
0886 return false;
0887
0888
0889 skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
0890 ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
0891 unicast_packet->ttvn = curr_ttvn;
0892 skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
0893
0894 batadv_hardif_put(primary_if);
0895
0896 return true;
0897 }
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908 int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
0909 struct batadv_hard_iface *recv_if)
0910 {
0911 struct batadv_unicast_packet *unicast_packet;
0912 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
0913 int check, hdr_size = sizeof(*unicast_packet);
0914
0915 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
0916 if (check < 0)
0917 goto free_skb;
0918
0919
0920 unicast_packet = (struct batadv_unicast_packet *)skb->data;
0921 if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
0922 goto free_skb;
0923
0924 return batadv_route_unicast_packet(skb, recv_if);
0925
0926 free_skb:
0927 kfree_skb(skb);
0928 return NET_RX_DROP;
0929 }
0930
0931
0932
0933
0934
0935
0936
0937
0938 int batadv_recv_unicast_packet(struct sk_buff *skb,
0939 struct batadv_hard_iface *recv_if)
0940 {
0941 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
0942 struct batadv_unicast_packet *unicast_packet;
0943 struct batadv_unicast_4addr_packet *unicast_4addr_packet;
0944 u8 *orig_addr, *orig_addr_gw;
0945 struct batadv_orig_node *orig_node = NULL, *orig_node_gw = NULL;
0946 int check, hdr_size = sizeof(*unicast_packet);
0947 enum batadv_subtype subtype;
0948 int ret = NET_RX_DROP;
0949 bool is4addr, is_gw;
0950
0951 unicast_packet = (struct batadv_unicast_packet *)skb->data;
0952 is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
0953
0954 if (is4addr)
0955 hdr_size = sizeof(*unicast_4addr_packet);
0956
0957
0958 check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
0959
0960
0961
0962
0963 if (check == -EREMOTE)
0964 batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
0965
0966 if (check < 0)
0967 goto free_skb;
0968 if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
0969 goto free_skb;
0970
0971 unicast_packet = (struct batadv_unicast_packet *)skb->data;
0972
0973
0974 if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
0975
0976
0977
0978 orig_addr_gw = eth_hdr(skb)->h_source;
0979 orig_node_gw = batadv_orig_hash_find(bat_priv, orig_addr_gw);
0980 if (orig_node_gw) {
0981 is_gw = batadv_bla_is_backbone_gw(skb, orig_node_gw,
0982 hdr_size);
0983 batadv_orig_node_put(orig_node_gw);
0984 if (is_gw) {
0985 batadv_dbg(BATADV_DBG_BLA, bat_priv,
0986 "%s(): Dropped unicast pkt received from another backbone gw %pM.\n",
0987 __func__, orig_addr_gw);
0988 goto free_skb;
0989 }
0990 }
0991
0992 if (is4addr) {
0993 unicast_4addr_packet =
0994 (struct batadv_unicast_4addr_packet *)skb->data;
0995 subtype = unicast_4addr_packet->subtype;
0996 batadv_dat_inc_counter(bat_priv, subtype);
0997
0998
0999
1000
1001
1002
1003
1004 if (subtype == BATADV_P_DATA) {
1005 orig_addr = unicast_4addr_packet->src;
1006 orig_node = batadv_orig_hash_find(bat_priv,
1007 orig_addr);
1008 }
1009 }
1010
1011 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
1012 hdr_size))
1013 goto rx_success;
1014 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
1015 hdr_size))
1016 goto rx_success;
1017
1018 batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1019
1020 batadv_interface_rx(recv_if->soft_iface, skb, hdr_size,
1021 orig_node);
1022
1023 rx_success:
1024 batadv_orig_node_put(orig_node);
1025
1026 return NET_RX_SUCCESS;
1027 }
1028
1029 ret = batadv_route_unicast_packet(skb, recv_if);
1030
1031 skb = NULL;
1032
1033 free_skb:
1034 kfree_skb(skb);
1035
1036 return ret;
1037 }
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047 int batadv_recv_unicast_tvlv(struct sk_buff *skb,
1048 struct batadv_hard_iface *recv_if)
1049 {
1050 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1051 struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
1052 unsigned char *tvlv_buff;
1053 u16 tvlv_buff_len;
1054 int hdr_size = sizeof(*unicast_tvlv_packet);
1055 int ret = NET_RX_DROP;
1056
1057 if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
1058 goto free_skb;
1059
1060
1061 if (skb_cow(skb, hdr_size) < 0)
1062 goto free_skb;
1063
1064
1065 if (skb_linearize(skb) < 0)
1066 goto free_skb;
1067
1068 unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
1069
1070 tvlv_buff = (unsigned char *)(skb->data + hdr_size);
1071 tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
1072
1073 if (tvlv_buff_len > skb->len - hdr_size)
1074 goto free_skb;
1075
1076 ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
1077 unicast_tvlv_packet->src,
1078 unicast_tvlv_packet->dst,
1079 tvlv_buff, tvlv_buff_len);
1080
1081 if (ret != NET_RX_SUCCESS) {
1082 ret = batadv_route_unicast_packet(skb, recv_if);
1083
1084 skb = NULL;
1085 }
1086
1087 free_skb:
1088 kfree_skb(skb);
1089
1090 return ret;
1091 }
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104 int batadv_recv_frag_packet(struct sk_buff *skb,
1105 struct batadv_hard_iface *recv_if)
1106 {
1107 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1108 struct batadv_orig_node *orig_node_src = NULL;
1109 struct batadv_frag_packet *frag_packet;
1110 int ret = NET_RX_DROP;
1111
1112 if (batadv_check_unicast_packet(bat_priv, skb,
1113 sizeof(*frag_packet)) < 0)
1114 goto free_skb;
1115
1116 frag_packet = (struct batadv_frag_packet *)skb->data;
1117 orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
1118 if (!orig_node_src)
1119 goto free_skb;
1120
1121 skb->priority = frag_packet->priority + 256;
1122
1123
1124 if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
1125 batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
1126
1127 skb = NULL;
1128 ret = NET_RX_SUCCESS;
1129 goto put_orig_node;
1130 }
1131
1132 batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
1133 batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
1134
1135
1136 if (!batadv_frag_skb_buffer(&skb, orig_node_src))
1137 goto put_orig_node;
1138
1139
1140
1141
1142 if (skb) {
1143 batadv_batman_skb_recv(skb, recv_if->net_dev,
1144 &recv_if->batman_adv_ptype, NULL);
1145
1146 skb = NULL;
1147 }
1148
1149 ret = NET_RX_SUCCESS;
1150
1151 put_orig_node:
1152 batadv_orig_node_put(orig_node_src);
1153 free_skb:
1154 kfree_skb(skb);
1155
1156 return ret;
1157 }
1158
1159
1160
1161
1162
1163
1164
1165
1166 int batadv_recv_bcast_packet(struct sk_buff *skb,
1167 struct batadv_hard_iface *recv_if)
1168 {
1169 struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
1170 struct batadv_orig_node *orig_node = NULL;
1171 struct batadv_bcast_packet *bcast_packet;
1172 struct ethhdr *ethhdr;
1173 int hdr_size = sizeof(*bcast_packet);
1174 s32 seq_diff;
1175 u32 seqno;
1176 int ret;
1177
1178
1179 if (unlikely(!pskb_may_pull(skb, hdr_size)))
1180 goto free_skb;
1181
1182 ethhdr = eth_hdr(skb);
1183
1184
1185 if (!is_broadcast_ether_addr(ethhdr->h_dest))
1186 goto free_skb;
1187
1188
1189 if (is_multicast_ether_addr(ethhdr->h_source))
1190 goto free_skb;
1191
1192
1193 if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
1194 goto free_skb;
1195
1196 bcast_packet = (struct batadv_bcast_packet *)skb->data;
1197
1198
1199 if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
1200 goto free_skb;
1201
1202 if (bcast_packet->ttl-- < 2)
1203 goto free_skb;
1204
1205 orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
1206
1207 if (!orig_node)
1208 goto free_skb;
1209
1210 spin_lock_bh(&orig_node->bcast_seqno_lock);
1211
1212 seqno = ntohl(bcast_packet->seqno);
1213
1214 if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
1215 seqno))
1216 goto spin_unlock;
1217
1218 seq_diff = seqno - orig_node->last_bcast_seqno;
1219
1220
1221 if (batadv_window_protected(bat_priv, seq_diff,
1222 BATADV_BCAST_MAX_AGE,
1223 &orig_node->bcast_seqno_reset, NULL))
1224 goto spin_unlock;
1225
1226
1227
1228
1229 if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
1230 orig_node->last_bcast_seqno = seqno;
1231
1232 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1233
1234
1235 if (batadv_bla_check_bcast_duplist(bat_priv, skb))
1236 goto free_skb;
1237
1238 batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
1239
1240
1241 ret = batadv_forw_bcast_packet(bat_priv, skb, 0, false);
1242 if (ret == NETDEV_TX_BUSY)
1243 goto free_skb;
1244
1245
1246
1247
1248 if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
1249 goto free_skb;
1250
1251 if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
1252 goto rx_success;
1253 if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
1254 goto rx_success;
1255
1256 batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
1257
1258
1259 batadv_interface_rx(recv_if->soft_iface, skb, hdr_size, orig_node);
1260
1261 rx_success:
1262 ret = NET_RX_SUCCESS;
1263 goto out;
1264
1265 spin_unlock:
1266 spin_unlock_bh(&orig_node->bcast_seqno_lock);
1267 free_skb:
1268 kfree_skb(skb);
1269 ret = NET_RX_DROP;
1270 out:
1271 batadv_orig_node_put(orig_node);
1272 return ret;
1273 }