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