tx.c 3.0 KB

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