tx.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 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. * Written by:
  14. * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  15. * Sergey Lapin <slapin@ossfans.org>
  16. * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
  17. * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
  18. */
  19. #include <linux/netdevice.h>
  20. #include <linux/if_arp.h>
  21. #include <linux/crc-ccitt.h>
  22. #include <asm/unaligned.h>
  23. #include <net/rtnetlink.h>
  24. #include <net/ieee802154_netdev.h>
  25. #include <net/mac802154.h>
  26. #include <net/cfg802154.h>
  27. #include "ieee802154_i.h"
  28. #include "driver-ops.h"
  29. void ieee802154_xmit_worker(struct work_struct *work)
  30. {
  31. struct ieee802154_local *local =
  32. container_of(work, struct ieee802154_local, tx_work);
  33. struct sk_buff *skb = local->tx_skb;
  34. struct net_device *dev = skb->dev;
  35. int res;
  36. res = drv_xmit_sync(local, skb);
  37. if (res)
  38. goto err_tx;
  39. ieee802154_xmit_complete(&local->hw, skb, false);
  40. dev->stats.tx_packets++;
  41. dev->stats.tx_bytes += skb->len;
  42. return;
  43. err_tx:
  44. /* Restart the netif queue on each sub_if_data object. */
  45. ieee802154_wake_queue(&local->hw);
  46. kfree_skb(skb);
  47. netdev_dbg(dev, "transmission failed\n");
  48. }
  49. static netdev_tx_t
  50. ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
  51. {
  52. struct net_device *dev = skb->dev;
  53. int ret;
  54. if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) {
  55. struct sk_buff *nskb;
  56. u16 crc;
  57. if (unlikely(skb_tailroom(skb) < IEEE802154_FCS_LEN)) {
  58. nskb = skb_copy_expand(skb, 0, IEEE802154_FCS_LEN,
  59. GFP_ATOMIC);
  60. if (likely(nskb)) {
  61. consume_skb(skb);
  62. skb = nskb;
  63. } else {
  64. goto err_tx;
  65. }
  66. }
  67. crc = crc_ccitt(0, skb->data, skb->len);
  68. put_unaligned_le16(crc, skb_put(skb, 2));
  69. }
  70. /* Stop the netif queue on each sub_if_data object. */
  71. ieee802154_stop_queue(&local->hw);
  72. /* async is priority, otherwise sync is fallback */
  73. if (local->ops->xmit_async) {
  74. ret = drv_xmit_async(local, skb);
  75. if (ret) {
  76. ieee802154_wake_queue(&local->hw);
  77. goto err_tx;
  78. }
  79. dev->stats.tx_packets++;
  80. dev->stats.tx_bytes += skb->len;
  81. } else {
  82. local->tx_skb = skb;
  83. queue_work(local->workqueue, &local->tx_work);
  84. }
  85. return NETDEV_TX_OK;
  86. err_tx:
  87. kfree_skb(skb);
  88. return NETDEV_TX_OK;
  89. }
  90. netdev_tx_t
  91. ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev)
  92. {
  93. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  94. skb->skb_iif = dev->ifindex;
  95. return ieee802154_tx(sdata->local, skb);
  96. }
  97. netdev_tx_t
  98. ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  99. {
  100. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  101. int rc;
  102. /* TODO we should move it to wpan_dev_hard_header and dev_hard_header
  103. * functions. The reason is wireshark will show a mac header which is
  104. * with security fields but the payload is not encrypted.
  105. */
  106. rc = mac802154_llsec_encrypt(&sdata->sec, skb);
  107. if (rc) {
  108. netdev_warn(dev, "encryption failed: %i\n", rc);
  109. kfree_skb(skb);
  110. return NETDEV_TX_OK;
  111. }
  112. skb->skb_iif = dev->ifindex;
  113. return ieee802154_tx(sdata->local, skb);
  114. }