rx.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_device(struct sk_buff *skb,
  15. struct net_device *dev)
  16. {
  17. skb->dev = dev->ieee802154_ptr->lowpan_dev;
  18. skb->protocol = htons(ETH_P_IPV6);
  19. skb->pkt_type = PACKET_HOST;
  20. return netif_rx(skb);
  21. }
  22. static int
  23. iphc_decompress(struct sk_buff *skb, const struct ieee802154_hdr *hdr)
  24. {
  25. u8 iphc0, iphc1;
  26. struct ieee802154_addr_sa sa, da;
  27. void *sap, *dap;
  28. raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len);
  29. /* at least two bytes will be used for the encoding */
  30. if (skb->len < 2)
  31. return -EINVAL;
  32. if (lowpan_fetch_skb_u8(skb, &iphc0))
  33. return -EINVAL;
  34. if (lowpan_fetch_skb_u8(skb, &iphc1))
  35. return -EINVAL;
  36. ieee802154_addr_to_sa(&sa, &hdr->source);
  37. ieee802154_addr_to_sa(&da, &hdr->dest);
  38. if (sa.addr_type == IEEE802154_ADDR_SHORT)
  39. sap = &sa.short_addr;
  40. else
  41. sap = &sa.hwaddr;
  42. if (da.addr_type == IEEE802154_ADDR_SHORT)
  43. dap = &da.short_addr;
  44. else
  45. dap = &da.hwaddr;
  46. return lowpan_header_decompress(skb, skb->dev, sap, sa.addr_type,
  47. IEEE802154_ADDR_LEN, dap, da.addr_type,
  48. IEEE802154_ADDR_LEN, iphc0, iphc1);
  49. }
  50. static int lowpan_rcv(struct sk_buff *skb, struct net_device *dev,
  51. struct packet_type *pt, struct net_device *orig_dev)
  52. {
  53. struct ieee802154_hdr hdr;
  54. int ret;
  55. if (dev->type != ARPHRD_IEEE802154 ||
  56. !dev->ieee802154_ptr->lowpan_dev)
  57. goto drop;
  58. skb = skb_share_check(skb, GFP_ATOMIC);
  59. if (!skb)
  60. goto drop;
  61. if (!netif_running(dev))
  62. goto drop_skb;
  63. if (skb->pkt_type == PACKET_OTHERHOST)
  64. goto drop_skb;
  65. if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0)
  66. goto drop_skb;
  67. /* check that it's our buffer */
  68. if (skb->data[0] == LOWPAN_DISPATCH_IPV6) {
  69. /* Pull off the 1-byte of 6lowpan header. */
  70. skb_pull(skb, 1);
  71. return lowpan_give_skb_to_device(skb, dev);
  72. } else {
  73. switch (skb->data[0] & 0xe0) {
  74. case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */
  75. ret = iphc_decompress(skb, &hdr);
  76. if (ret < 0)
  77. goto drop_skb;
  78. return lowpan_give_skb_to_device(skb, dev);
  79. case LOWPAN_DISPATCH_FRAG1: /* first fragment header */
  80. ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1);
  81. if (ret == 1) {
  82. ret = iphc_decompress(skb, &hdr);
  83. if (ret < 0)
  84. goto drop_skb;
  85. return lowpan_give_skb_to_device(skb, dev);
  86. } else if (ret == -1) {
  87. return NET_RX_DROP;
  88. } else {
  89. return NET_RX_SUCCESS;
  90. }
  91. case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */
  92. ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN);
  93. if (ret == 1) {
  94. ret = iphc_decompress(skb, &hdr);
  95. if (ret < 0)
  96. goto drop_skb;
  97. return lowpan_give_skb_to_device(skb, dev);
  98. } else if (ret == -1) {
  99. return NET_RX_DROP;
  100. } else {
  101. return NET_RX_SUCCESS;
  102. }
  103. default:
  104. break;
  105. }
  106. }
  107. drop_skb:
  108. kfree_skb(skb);
  109. drop:
  110. return NET_RX_DROP;
  111. }
  112. static struct packet_type lowpan_packet_type = {
  113. .type = htons(ETH_P_IEEE802154),
  114. .func = lowpan_rcv,
  115. };
  116. void lowpan_rx_init(void)
  117. {
  118. dev_add_pack(&lowpan_packet_type);
  119. }
  120. void lowpan_rx_exit(void)
  121. {
  122. dev_remove_pack(&lowpan_packet_type);
  123. }