rx.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2007-2012 Siemens AG
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Written by:
  18. * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
  19. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  20. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  21. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/crc-ccitt.h>
  28. #include <net/mac802154.h>
  29. #include <net/ieee802154_netdev.h>
  30. #include "mac802154.h"
  31. /* The IEEE 802.15.4 standard defines 4 MAC packet types:
  32. * - beacon frame
  33. * - MAC command frame
  34. * - acknowledgement frame
  35. * - data frame
  36. *
  37. * and only the data frame should be pushed to the upper layers, other types
  38. * are just internal MAC layer management information. So only data packets
  39. * are going to be sent to the networking queue, all other will be processed
  40. * right here by using the device workqueue.
  41. */
  42. struct rx_work {
  43. struct sk_buff *skb;
  44. struct work_struct work;
  45. struct ieee802154_dev *dev;
  46. u8 lqi;
  47. };
  48. static void
  49. mac802154_subif_rx(struct ieee802154_dev *hw, struct sk_buff *skb, u8 lqi)
  50. {
  51. struct mac802154_priv *priv = mac802154_to_priv(hw);
  52. mac_cb(skb)->lqi = lqi;
  53. skb->protocol = htons(ETH_P_IEEE802154);
  54. skb_reset_mac_header(skb);
  55. BUILD_BUG_ON(sizeof(struct ieee802154_mac_cb) > sizeof(skb->cb));
  56. if (!(priv->hw.flags & IEEE802154_HW_OMIT_CKSUM)) {
  57. u16 crc;
  58. if (skb->len < 2) {
  59. pr_debug("got invalid frame\n");
  60. goto out;
  61. }
  62. crc = crc_ccitt(0, skb->data, skb->len);
  63. if (crc) {
  64. pr_debug("CRC mismatch\n");
  65. goto out;
  66. }
  67. skb_trim(skb, skb->len - 2); /* CRC */
  68. }
  69. mac802154_monitors_rx(priv, skb);
  70. mac802154_wpans_rx(priv, skb);
  71. out:
  72. dev_kfree_skb(skb);
  73. return;
  74. }
  75. static void mac802154_rx_worker(struct work_struct *work)
  76. {
  77. struct rx_work *rw = container_of(work, struct rx_work, work);
  78. struct sk_buff *skb = rw->skb;
  79. mac802154_subif_rx(rw->dev, skb, rw->lqi);
  80. kfree(rw);
  81. }
  82. void
  83. ieee802154_rx_irqsafe(struct ieee802154_dev *dev, struct sk_buff *skb, u8 lqi)
  84. {
  85. struct mac802154_priv *priv = mac802154_to_priv(dev);
  86. struct rx_work *work;
  87. if (!skb)
  88. return;
  89. work = kzalloc(sizeof(struct rx_work), GFP_ATOMIC);
  90. if (!work)
  91. return;
  92. INIT_WORK(&work->work, mac802154_rx_worker);
  93. work->skb = skb;
  94. work->dev = dev;
  95. work->lqi = lqi;
  96. queue_work(priv->dev_workqueue, &work->work);
  97. }
  98. EXPORT_SYMBOL(ieee802154_rx_irqsafe);