blob: ab52811523e992f33f0855cdb711a2752b602e15 [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +00002/*
3 * Copyright 2007-2012 Siemens AG
4 *
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +00005 * 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>
Alexander Aring061ef8f2014-10-27 17:13:28 +010015#include <asm/unaligned.h>
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000016
Alexander Aring6001d522014-10-26 09:37:09 +010017#include <net/rtnetlink.h>
Alan Ottb5992fe2013-04-03 04:00:56 +000018#include <net/ieee802154_netdev.h>
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000019#include <net/mac802154.h>
Alexander Aring5ad60d32014-10-25 09:41:02 +020020#include <net/cfg802154.h>
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000021
Alexander Aring0f1556b2014-10-25 09:41:00 +020022#include "ieee802154_i.h"
Alexander Aring59cb3002014-10-28 18:21:21 +010023#include "driver-ops.h"
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000024
Lennert Buytenhekc22ff7b2015-07-21 17:44:47 +030025void ieee802154_xmit_worker(struct work_struct *work)
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000026{
Lennert Buytenhekc22ff7b2015-07-21 17:44:47 +030027 struct ieee802154_local *local =
28 container_of(work, struct ieee802154_local, tx_work);
29 struct sk_buff *skb = local->tx_skb;
Alexander Aring409c3b02014-10-26 09:37:12 +010030 struct net_device *dev = skb->dev;
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000031 int res;
32
Alexander Aring59cb3002014-10-28 18:21:21 +010033 res = drv_xmit_sync(local, skb);
Alexander Aring6001d522014-10-26 09:37:09 +010034 if (res)
35 goto err_tx;
36
Alexander Aring61f2dcb2014-11-12 19:51:56 +010037 ieee802154_xmit_complete(&local->hw, skb, false);
Alexander Aring6001d522014-10-26 09:37:09 +010038
Alexander Aring409c3b02014-10-26 09:37:12 +010039 dev->stats.tx_packets++;
40 dev->stats.tx_bytes += skb->len;
41
Alexander Aring6001d522014-10-26 09:37:09 +010042 return;
43
44err_tx:
45 /* Restart the netif queue on each sub_if_data object. */
46 ieee802154_wake_queue(&local->hw);
Alexander Aring6001d522014-10-26 09:37:09 +010047 kfree_skb(skb);
Alexander Aring409c3b02014-10-26 09:37:12 +010048 netdev_dbg(dev, "transmission failed\n");
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000049}
50
Alexander Aringdc67c6b2014-10-26 09:37:04 +010051static netdev_tx_t
Alexander Aringe5e584f2014-10-26 09:37:13 +010052ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000053{
Alexander Aring409c3b02014-10-26 09:37:12 +010054 struct net_device *dev = skb->dev;
Alexander Aringed0a5dc2014-10-26 09:37:08 +010055 int ret;
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000056
Alexander Aring90386a72014-10-29 21:34:34 +010057 if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) {
Alexander Aringf9c52832018-07-02 16:32:03 -040058 struct sk_buff *nskb;
59 u16 crc;
Varka Bhadram4710d802014-07-02 09:01:09 +053060
Alexander Aringf9c52832018-07-02 16:32:03 -040061 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);
Alexander Aring061ef8f2014-10-27 17:13:28 +010073 put_unaligned_le16(crc, skb_put(skb, 2));
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000074 }
75
Alan Ottb5992fe2013-04-03 04:00:56 +000076 /* Stop the netif queue on each sub_if_data object. */
Alexander Aring18d60a02014-10-26 09:37:06 +010077 ieee802154_stop_queue(&local->hw);
Alan Ottb5992fe2013-04-03 04:00:56 +000078
Alexander Aringed0a5dc2014-10-26 09:37:08 +010079 /* async is priority, otherwise sync is fallback */
80 if (local->ops->xmit_async) {
Alexander Aring59cb3002014-10-28 18:21:21 +010081 ret = drv_xmit_async(local, skb);
Alexander Aringed0a5dc2014-10-26 09:37:08 +010082 if (ret) {
83 ieee802154_wake_queue(&local->hw);
84 goto err_tx;
85 }
Alexander Aring409c3b02014-10-26 09:37:12 +010086
87 dev->stats.tx_packets++;
88 dev->stats.tx_bytes += skb->len;
Alexander Aringed0a5dc2014-10-26 09:37:08 +010089 } else {
Lennert Buytenhekc22ff7b2015-07-21 17:44:47 +030090 local->tx_skb = skb;
91 queue_work(local->workqueue, &local->tx_work);
Alexander Aringed0a5dc2014-10-26 09:37:08 +010092 }
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000093
94 return NETDEV_TX_OK;
Varka Bhadramf5588912014-08-11 13:25:10 +020095
96err_tx:
97 kfree_skb(skb);
98 return NETDEV_TX_OK;
alex.bluesman.smirnov@gmail.com5b641eb2012-05-15 20:50:22 +000099}
Alexander Aring50c6fb92014-10-26 09:37:01 +0100100
Alexander Aringe5e584f2014-10-26 09:37:13 +0100101netdev_tx_t
102ieee802154_monitor_start_xmit(struct sk_buff *skb, struct net_device *dev)
Alexander Aring50c6fb92014-10-26 09:37:01 +0100103{
104 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100105
106 skb->skb_iif = dev->ifindex;
Alexander Aring50c6fb92014-10-26 09:37:01 +0100107
Alexander Aringe5e584f2014-10-26 09:37:13 +0100108 return ieee802154_tx(sdata->local, skb);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100109}
110
Alexander Aringe5e584f2014-10-26 09:37:13 +0100111netdev_tx_t
112ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
Alexander Aring50c6fb92014-10-26 09:37:01 +0100113{
114 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100115 int rc;
116
Alexander Aringd58a2fa2015-09-28 09:00:26 +0200117 /* 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 */
Alexander Aring50c6fb92014-10-26 09:37:01 +0100121 rc = mac802154_llsec_encrypt(&sdata->sec, skb);
122 if (rc) {
Alexander Aringcfa626c2014-10-26 09:37:10 +0100123 netdev_warn(dev, "encryption failed: %i\n", rc);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100124 kfree_skb(skb);
125 return NETDEV_TX_OK;
126 }
127
128 skb->skb_iif = dev->ifindex;
Alexander Aring50c6fb92014-10-26 09:37:01 +0100129
Alexander Aringe5e584f2014-10-26 09:37:13 +0100130 return ieee802154_tx(sdata->local, skb);
Alexander Aring50c6fb92014-10-26 09:37:01 +0100131}