tx.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. rtnl_lock();
  37. /* check if ifdown occurred while schedule */
  38. if (!netif_running(dev))
  39. goto err_tx;
  40. res = drv_xmit_sync(local, skb);
  41. if (res)
  42. goto err_tx;
  43. ieee802154_xmit_complete(&local->hw, skb, false);
  44. dev->stats.tx_packets++;
  45. dev->stats.tx_bytes += skb->len;
  46. rtnl_unlock();
  47. return;
  48. err_tx:
  49. /* Restart the netif queue on each sub_if_data object. */
  50. ieee802154_wake_queue(&local->hw);
  51. rtnl_unlock();
  52. kfree_skb(skb);
  53. netdev_dbg(dev, "transmission failed\n");
  54. }
  55. static netdev_tx_t
  56. ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
  57. {
  58. struct net_device *dev = skb->dev;
  59. int ret;
  60. if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) {
  61. struct sk_buff *nskb;
  62. u16 crc;
  63. if (unlikely(skb_tailroom(skb) < IEEE802154_FCS_LEN)) {
  64. nskb = skb_copy_expand(skb, 0, IEEE802154_FCS_LEN,
  65. GFP_ATOMIC);
  66. if (likely(nskb)) {
  67. consume_skb(skb);
  68. skb = nskb;
  69. } else {
  70. goto err_tx;
  71. }
  72. }
  73. crc = crc_ccitt(0, skb->data, skb->len);
  74. put_unaligned_le16(crc, skb_put(skb, 2));
  75. }
  76. /* Stop the netif queue on each sub_if_data object. */
  77. ieee802154_stop_queue(&local->hw);
  78. /* async is priority, otherwise sync is fallback */
  79. if (local->ops->xmit_async) {
  80. ret = drv_xmit_async(local, skb);
  81. if (ret) {
  82. ieee802154_wake_queue(&local->hw);
  83. goto err_tx;
  84. }
  85. dev->stats.tx_packets++;
  86. dev->stats.tx_bytes += skb->len;
  87. } else {
  88. local->tx_skb = skb;
  89. queue_work(local->workqueue, &local->tx_work);
  90. }
  91. return NETDEV_TX_OK;
  92. err_tx:
  93. kfree_skb(skb);
  94. return NETDEV_TX_OK;
  95. }
  96. netdev_tx_t
  97. ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev)
  98. {
  99. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  100. skb->skb_iif = dev->ifindex;
  101. return ieee802154_tx(sdata->local, skb);
  102. }
  103. netdev_tx_t
  104. ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
  105. {
  106. struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
  107. int rc;
  108. /* TODO we should move it to wpan_dev_hard_header and dev_hard_header
  109. * functions. The reason is wireshark will show a mac header which is
  110. * with security fields but the payload is not encrypted.
  111. */
  112. rc = mac802154_llsec_encrypt(&sdata->sec, skb);
  113. if (rc) {
  114. netdev_warn(dev, "encryption failed: %i\n", rc);
  115. kfree_skb(skb);
  116. return NETDEV_TX_OK;
  117. }
  118. skb->skb_iif = dev->ifindex;
  119. return ieee802154_tx(sdata->local, skb);
  120. }