• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <linux/netdevice.h>
21 #include <linux/if_arp.h>
22 #include <linux/crc-ccitt.h>
23 #include <asm/unaligned.h>
24 
25 #include <net/rtnetlink.h>
26 #include <net/ieee802154_netdev.h>
27 #include <net/mac802154.h>
28 #include <net/cfg802154.h>
29 
30 #include "ieee802154_i.h"
31 #include "driver-ops.h"
32 
ieee802154_xmit_worker(struct work_struct * work)33 void ieee802154_xmit_worker(struct work_struct *work)
34 {
35 	struct ieee802154_local *local =
36 		container_of(work, struct ieee802154_local, tx_work);
37 	struct sk_buff *skb = local->tx_skb;
38 	struct net_device *dev = skb->dev;
39 	int res;
40 
41 	res = drv_xmit_sync(local, skb);
42 	if (res)
43 		goto err_tx;
44 
45 	dev->stats.tx_packets++;
46 	dev->stats.tx_bytes += skb->len;
47 
48 	ieee802154_xmit_complete(&local->hw, skb, false);
49 
50 	return;
51 
52 err_tx:
53 	/* Restart the netif queue on each sub_if_data object. */
54 	ieee802154_wake_queue(&local->hw);
55 	kfree_skb(skb);
56 	netdev_dbg(dev, "transmission failed\n");
57 }
58 
59 static netdev_tx_t
ieee802154_tx(struct ieee802154_local * local,struct sk_buff * skb)60 ieee802154_tx(struct ieee802154_local *local, struct sk_buff *skb)
61 {
62 	struct net_device *dev = skb->dev;
63 	int ret;
64 
65 	if (!(local->hw.flags & IEEE802154_HW_TX_OMIT_CKSUM)) {
66 		struct sk_buff *nskb;
67 		u16 crc;
68 
69 		if (unlikely(skb_tailroom(skb) < IEEE802154_FCS_LEN)) {
70 			nskb = skb_copy_expand(skb, 0, IEEE802154_FCS_LEN,
71 					       GFP_ATOMIC);
72 			if (likely(nskb)) {
73 				consume_skb(skb);
74 				skb = nskb;
75 			} else {
76 				goto err_tx;
77 			}
78 		}
79 
80 		crc = crc_ccitt(0, skb->data, skb->len);
81 		put_unaligned_le16(crc, skb_put(skb, 2));
82 	}
83 
84 	/* Stop the netif queue on each sub_if_data object. */
85 	ieee802154_stop_queue(&local->hw);
86 
87 	/* async is priority, otherwise sync is fallback */
88 	if (local->ops->xmit_async) {
89 		unsigned int len = skb->len;
90 
91 		ret = drv_xmit_async(local, skb);
92 		if (ret) {
93 			ieee802154_wake_queue(&local->hw);
94 			goto err_tx;
95 		}
96 
97 		dev->stats.tx_packets++;
98 		dev->stats.tx_bytes += len;
99 	} else {
100 		local->tx_skb = skb;
101 		queue_work(local->workqueue, &local->tx_work);
102 	}
103 
104 	return NETDEV_TX_OK;
105 
106 err_tx:
107 	kfree_skb(skb);
108 	return NETDEV_TX_OK;
109 }
110 
111 netdev_tx_t
ieee802154_monitor_start_xmit(struct sk_buff * skb,struct net_device * dev)112 ieee802154_monitor_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 
116 	skb->skb_iif = dev->ifindex;
117 
118 	return ieee802154_tx(sdata->local, skb);
119 }
120 
121 netdev_tx_t
ieee802154_subif_start_xmit(struct sk_buff * skb,struct net_device * dev)122 ieee802154_subif_start_xmit(struct sk_buff *skb, struct net_device *dev)
123 {
124 	struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev);
125 	int rc;
126 
127 	/* TODO we should move it to wpan_dev_hard_header and dev_hard_header
128 	 * functions. The reason is wireshark will show a mac header which is
129 	 * with security fields but the payload is not encrypted.
130 	 */
131 	rc = mac802154_llsec_encrypt(&sdata->sec, skb);
132 	if (rc) {
133 		netdev_warn(dev, "encryption failed: %i\n", rc);
134 		kfree_skb(skb);
135 		return NETDEV_TX_OK;
136 	}
137 
138 	skb->skb_iif = dev->ifindex;
139 
140 	return ieee802154_tx(sdata->local, skb);
141 }
142