rx.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* This program is free software; you can redistribute it and/or modify
  2. * it under the terms of the GNU General Public License version 2
  3. * as published by the Free Software Foundation.
  4. *
  5. * This program is distributed in the hope that it will be useful,
  6. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. * GNU General Public License for more details.
  9. */
  10. #include <linux/if_arp.h>
  11. #include <net/6lowpan.h>
  12. #include <net/ieee802154_netdev.h>
  13. #include "6lowpan_i.h"
  14. static int lowpan_give_skb_to_devices(struct sk_buff *skb,
  15. struct net_device *dev)
  16. {
  17. struct lowpan_dev_record *entry;
  18. struct sk_buff *skb_cp;
  19. int stat = NET_RX_SUCCESS;
  20. skb->protocol = htons(ETH_P_IPV6);
  21. skb->pkt_type = PACKET_HOST;
  22. rcu_read_lock();
  23. list_for_each_entry_rcu(entry, &lowpan_devices, list)
  24. if (lowpan_dev_info(entry->ldev)->real_dev == skb->dev) {
  25. skb_cp = skb_copy(skb, GFP_ATOMIC);
  26. if (!skb_cp) {
  27. kfree_skb(skb);
  28. rcu_read_unlock();
  29. return NET_RX_DROP;
  30. }
  31. skb_cp->dev = entry->ldev;
  32. stat = netif_rx(skb_cp);
  33. if (stat == NET_RX_DROP)
  34. break;
  35. }
  36. rcu_read_unlock();
  37. consume_skb(skb);
  38. return stat;
  39. }
  40. static int
  41. iphc_decompress(struct sk_buff *skb, const struct ieee802154_hdr *hdr)
  42. {
  43. u8 iphc0, iphc1;
  44. struct ieee802154_addr_sa sa, da;
  45. void *sap, *dap;
  46. raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
  47. /* at least two bytes will be used for the encoding */
  48. if (skb->len < 2)
  49. return -EINVAL;
  50. if (lowpan_fetch_skb_u8(skb, &iphc0))
  51. return -EINVAL;
  52. if (lowpan_fetch_skb_u8(skb, &iphc1))
  53. return -EINVAL;
  54. ieee802154_addr_to_sa(&sa, &hdr->source);
  55. ieee802154_addr_to_sa(&da, &hdr->dest);
  56. if (sa.addr_type == IEEE802154_ADDR_SHORT)
  57. sap = &sa.short_addr;
  58. else
  59. sap = &sa.hwaddr;
  60. if (da.addr_type == IEEE802154_ADDR_SHORT)
  61. dap = &da.short_addr;
  62. else
  63. dap = &da.hwaddr;
  64. return lowpan_header_decompress(skb, skb->dev, sap, sa.addr_type,
  65. IEEE802154_ADDR_LEN, dap, da.addr_type,
  66. IEEE802154_ADDR_LEN, iphc0, iphc1);
  67. }
  68. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  69. struct packet_type *pt, struct net_device *orig_dev)
  70. {
  71. struct ieee802154_hdr hdr;
  72. int ret;
  73. skb = skb_share_check(skb, GFP_ATOMIC);
  74. if (!skb)
  75. goto drop;
  76. if (!netif_running(dev))
  77. goto drop_skb;
  78. if (skb->pkt_type == PACKET_OTHERHOST)
  79. goto drop_skb;
  80. if (dev->type != ARPHRD_IEEE802154)
  81. goto drop_skb;
  82. if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
  83. goto drop_skb;
  84. /* check that it's our buffer */
  85. if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
  86. /* Pull off the 1-byte of 6lowpan header. */
  87. skb_pull(skb, 1);
  88. return lowpan_give_skb_to_devices(skb, NULL);
  89. } else {
  90. switch (skb->data[0] & 0xe0) {
  91. case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
  92. ret = iphc_decompress(skb, &hdr);
  93. if (ret < 0)
  94. goto drop_skb;
  95. return lowpan_give_skb_to_devices(skb, NULL);
  96. case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
  97. ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1);
  98. if (ret == 1) {
  99. ret = iphc_decompress(skb, &hdr);
  100. if (ret < 0)
  101. goto drop_skb;
  102. return lowpan_give_skb_to_devices(skb, NULL);
  103. } else if (ret == -1) {
  104. return NET_RX_DROP;
  105. } else {
  106. return NET_RX_SUCCESS;
  107. }
  108. case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
  109. ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN);
  110. if (ret == 1) {
  111. ret = iphc_decompress(skb, &hdr);
  112. if (ret < 0)
  113. goto drop_skb;
  114. return lowpan_give_skb_to_devices(skb, NULL);
  115. } else if (ret == -1) {
  116. return NET_RX_DROP;
  117. } else {
  118. return NET_RX_SUCCESS;
  119. }
  120. default:
  121. break;
  122. }
  123. }
  124. drop_skb:
  125. kfree_skb(skb);
  126. drop:
  127. return NET_RX_DROP;
  128. }
  129. static struct packet_type lowpan_packet_type = {
  130. .type = htons(ETH_P_IEEE802154),
  131. .func = lowpan_rcv,
  132. };
  133. void lowpan_rx_init(void)
  134. {
  135. dev_add_pack(&lowpan_packet_type);
  136. }
  137. void lowpan_rx_exit(void)
  138. {
  139. dev_remove_pack(&lowpan_packet_type);
  140. }