blob: b8ce84618a55b7dc082e4d1af6720ffbcda7f46c [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +00002/*
3 * Copyright (C) 2007-2012 Siemens AG
4 *
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +00005 * Written by:
6 * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
7 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
8 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
9 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +000014#include <linux/netdevice.h>
15#include <linux/crc-ccitt.h>
Alexander Aringb7889492014-10-29 21:34:36 +010016#include <asm/unaligned.h>
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +000017
18#include <net/mac802154.h>
19#include <net/ieee802154_netdev.h>
Alexander Aring944742a2014-11-17 08:20:49 +010020#include <net/nl802154.h>
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +000021
Alexander Aring0f1556b2014-10-25 09:41:00 +020022#include "ieee802154_i.h"
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +000023
Alexander Aring08c511a2014-10-29 21:34:35 +010024static int ieee802154_deliver_skb(struct sk_buff *skb)
Alexander Aring2a9820c2014-10-27 17:13:32 +010025{
Alexander Aring75a46f02014-10-27 17:13:36 +010026 skb->ip_summed = CHECKSUM_UNNECESSARY;
Alexander Aring702dcf92014-10-27 17:13:35 +010027 skb->protocol = htons(ETH_P_IEEE802154);
28
Alexander Aring2a9820c2014-10-27 17:13:32 +010029 return netif_receive_skb(skb);
30}
31
32static int
Alexander Aringbe9d2152014-10-27 17:13:43 +010033ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata,
34 struct sk_buff *skb, const struct ieee802154_hdr *hdr)
Alexander Aring2a9820c2014-10-27 17:13:32 +010035{
Alexander Aring863e88f2014-11-09 08:36:45 +010036 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
Alexander Aring2a9820c2014-10-27 17:13:32 +010037 __le16 span, sshort;
38 int rc;
39
40 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
41
Alexander Aring863e88f2014-11-09 08:36:45 +010042 span = wpan_dev->pan_id;
43 sshort = wpan_dev->short_addr;
Alexander Aring2a9820c2014-10-27 17:13:32 +010044
45 switch (mac_cb(skb)->dest.mode) {
46 case IEEE802154_ADDR_NONE:
47 if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
48 /* FIXME: check if we are PAN coordinator */
49 skb->pkt_type = PACKET_OTHERHOST;
50 else
51 /* ACK comes with both addresses empty */
52 skb->pkt_type = PACKET_HOST;
53 break;
54 case IEEE802154_ADDR_LONG:
55 if (mac_cb(skb)->dest.pan_id != span &&
56 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
57 skb->pkt_type = PACKET_OTHERHOST;
Alexander Aring863e88f2014-11-09 08:36:45 +010058 else if (mac_cb(skb)->dest.extended_addr == wpan_dev->extended_addr)
Alexander Aring2a9820c2014-10-27 17:13:32 +010059 skb->pkt_type = PACKET_HOST;
60 else
61 skb->pkt_type = PACKET_OTHERHOST;
62 break;
63 case IEEE802154_ADDR_SHORT:
64 if (mac_cb(skb)->dest.pan_id != span &&
65 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
66 skb->pkt_type = PACKET_OTHERHOST;
67 else if (mac_cb(skb)->dest.short_addr == sshort)
68 skb->pkt_type = PACKET_HOST;
69 else if (mac_cb(skb)->dest.short_addr ==
70 cpu_to_le16(IEEE802154_ADDR_BROADCAST))
71 skb->pkt_type = PACKET_BROADCAST;
72 else
73 skb->pkt_type = PACKET_OTHERHOST;
74 break;
75 default:
Alexander Aring2a9820c2014-10-27 17:13:32 +010076 pr_debug("invalid dest mode\n");
Varka Bhadrambcb47aa2014-12-05 17:19:09 +053077 goto fail;
Alexander Aring2a9820c2014-10-27 17:13:32 +010078 }
79
Alexander Aring2a9820c2014-10-27 17:13:32 +010080 skb->dev = sdata->dev;
81
Alexander Aringd58a2fa2015-09-28 09:00:26 +020082 /* TODO this should be moved after netif_receive_skb call, otherwise
83 * wireshark will show a mac header with security fields and the
84 * payload is already decrypted.
85 */
Alexander Aring2a9820c2014-10-27 17:13:32 +010086 rc = mac802154_llsec_decrypt(&sdata->sec, skb);
87 if (rc) {
88 pr_debug("decryption failed: %i\n", rc);
89 goto fail;
90 }
91
92 sdata->dev->stats.rx_packets++;
93 sdata->dev->stats.rx_bytes += skb->len;
94
95 switch (mac_cb(skb)->type) {
Aristeu Rozanskica1de812016-07-25 11:46:40 -040096 case IEEE802154_FC_TYPE_BEACON:
97 case IEEE802154_FC_TYPE_ACK:
98 case IEEE802154_FC_TYPE_MAC_CMD:
99 goto fail;
100
Alexander Aring2a9820c2014-10-27 17:13:32 +0100101 case IEEE802154_FC_TYPE_DATA:
Alexander Aring08c511a2014-10-29 21:34:35 +0100102 return ieee802154_deliver_skb(skb);
Alexander Aring2a9820c2014-10-27 17:13:32 +0100103 default:
Aristeu Rozanskibd89bb62016-07-25 11:46:41 -0400104 pr_warn_ratelimited("ieee802154: bad frame received "
105 "(type = %d)\n", mac_cb(skb)->type);
Alexander Aring2a9820c2014-10-27 17:13:32 +0100106 goto fail;
107 }
108
109fail:
110 kfree_skb(skb);
111 return NET_RX_DROP;
112}
113
Alexander Aringbe9d2152014-10-27 17:13:43 +0100114static void
115ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr)
Alexander Aring2a9820c2014-10-27 17:13:32 +0100116{
117 if (addr->mode == IEEE802154_ADDR_NONE)
118 pr_debug("%s not present\n", name);
119
120 pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
121 if (addr->mode == IEEE802154_ADDR_SHORT) {
122 pr_debug("%s is short: %04x\n", name,
123 le16_to_cpu(addr->short_addr));
124 } else {
125 u64 hw = swab64((__force u64)addr->extended_addr);
126
127 pr_debug("%s is hardware: %8phC\n", name, &hw);
128 }
129}
130
Alexander Aringbe9d2152014-10-27 17:13:43 +0100131static int
132ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr)
Alexander Aring2a9820c2014-10-27 17:13:32 +0100133{
134 int hlen;
135 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
136
Alexander Aring9cf215d2014-10-27 17:13:38 +0100137 skb_reset_mac_header(skb);
138
Alexander Aring2a9820c2014-10-27 17:13:32 +0100139 hlen = ieee802154_hdr_pull(skb, hdr);
140 if (hlen < 0)
141 return -EINVAL;
142
143 skb->mac_len = hlen;
144
145 pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
146 hdr->seq);
147
148 cb->type = hdr->fc.type;
149 cb->ackreq = hdr->fc.ack_request;
150 cb->secen = hdr->fc.security_enabled;
151
Alexander Aringbe9d2152014-10-27 17:13:43 +0100152 ieee802154_print_addr("destination", &hdr->dest);
153 ieee802154_print_addr("source", &hdr->source);
Alexander Aring2a9820c2014-10-27 17:13:32 +0100154
155 cb->source = hdr->source;
156 cb->dest = hdr->dest;
157
158 if (hdr->fc.security_enabled) {
159 u64 key;
160
161 pr_debug("seclevel %i\n", hdr->sec.level);
162
163 switch (hdr->sec.key_id_mode) {
164 case IEEE802154_SCF_KEY_IMPLICIT:
165 pr_debug("implicit key\n");
166 break;
167
168 case IEEE802154_SCF_KEY_INDEX:
169 pr_debug("key %02x\n", hdr->sec.key_id);
170 break;
171
172 case IEEE802154_SCF_KEY_SHORT_INDEX:
173 pr_debug("key %04x:%04x %02x\n",
174 le32_to_cpu(hdr->sec.short_src) >> 16,
175 le32_to_cpu(hdr->sec.short_src) & 0xffff,
176 hdr->sec.key_id);
177 break;
178
179 case IEEE802154_SCF_KEY_HW_INDEX:
180 key = swab64((__force u64)hdr->sec.extended_src);
181 pr_debug("key source %8phC %02x\n", &key,
182 hdr->sec.key_id);
183 break;
184 }
185 }
186
187 return 0;
188}
189
190static void
Alexander Aringbe9d2152014-10-27 17:13:43 +0100191__ieee802154_rx_handle_packet(struct ieee802154_local *local,
192 struct sk_buff *skb)
Alexander Aring2a9820c2014-10-27 17:13:32 +0100193{
194 int ret;
195 struct ieee802154_sub_if_data *sdata;
196 struct ieee802154_hdr hdr;
197
Alexander Aringbe9d2152014-10-27 17:13:43 +0100198 ret = ieee802154_parse_frame_start(skb, &hdr);
Alexander Aring2a9820c2014-10-27 17:13:32 +0100199 if (ret) {
200 pr_debug("got invalid frame\n");
201 kfree_skb(skb);
202 return;
203 }
204
Alexander Aring2a9820c2014-10-27 17:13:32 +0100205 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Varka Bhadram1bc17542015-06-12 12:44:24 +0530206 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_NODE)
207 continue;
208
209 if (!ieee802154_sdata_running(sdata))
Alexander Aring2a9820c2014-10-27 17:13:32 +0100210 continue;
211
Alexander Aringbe9d2152014-10-27 17:13:43 +0100212 ieee802154_subif_frame(sdata, skb, &hdr);
Alexander Aring2a9820c2014-10-27 17:13:32 +0100213 skb = NULL;
214 break;
215 }
Alexander Aring2a9820c2014-10-27 17:13:32 +0100216
Markus Elfring56f9ebe2015-11-14 20:22:41 +0100217 kfree_skb(skb);
Alexander Aring2a9820c2014-10-27 17:13:32 +0100218}
219
Alexander Aring4ca18be2014-10-27 17:13:33 +0100220static void
Alexander Aringbe9d2152014-10-27 17:13:43 +0100221ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb)
Alexander Aring2a9820c2014-10-27 17:13:32 +0100222{
223 struct sk_buff *skb2;
224 struct ieee802154_sub_if_data *sdata;
Alexander Aring2a9820c2014-10-27 17:13:32 +0100225
Alexander Aring9cf215d2014-10-27 17:13:38 +0100226 skb_reset_mac_header(skb);
Alexander Aring75a46f02014-10-27 17:13:36 +0100227 skb->ip_summed = CHECKSUM_UNNECESSARY;
Alexander Aringc9ca6402014-10-27 17:13:37 +0100228 skb->pkt_type = PACKET_OTHERHOST;
Alexander Aring702dcf92014-10-27 17:13:35 +0100229 skb->protocol = htons(ETH_P_IEEE802154);
230
Alexander Aring2a9820c2014-10-27 17:13:32 +0100231 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
Alexander Aringed659632015-06-06 17:30:46 +0200232 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR)
Alexander Aring20b48122014-10-29 21:34:41 +0100233 continue;
234
235 if (!ieee802154_sdata_running(sdata))
Alexander Aring2a9820c2014-10-27 17:13:32 +0100236 continue;
237
238 skb2 = skb_clone(skb, GFP_ATOMIC);
Alexander Aring05f7de62014-10-29 21:34:42 +0100239 if (skb2) {
240 skb2->dev = sdata->dev;
241 ieee802154_deliver_skb(skb2);
Alexander Aring2a9820c2014-10-27 17:13:32 +0100242
Alexander Aring05f7de62014-10-29 21:34:42 +0100243 sdata->dev->stats.rx_packets++;
244 sdata->dev->stats.rx_bytes += skb->len;
245 }
Alexander Aring2a9820c2014-10-27 17:13:32 +0100246 }
Alexander Aring2a9820c2014-10-27 17:13:32 +0100247}
248
Varka Bhadramd10270c2015-07-07 10:50:43 +0530249void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb)
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000250{
Alexander Aringec718f32014-10-29 21:34:37 +0100251 u16 crc;
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000252
Alexander Aring469100d2014-10-27 17:13:34 +0100253 WARN_ON_ONCE(softirq_count() == 0);
254
Alexander Aring3cf24cf2015-06-24 11:36:36 +0200255 if (local->suspended)
256 goto drop;
257
Alexander Aringb7889492014-10-29 21:34:36 +0100258 /* TODO: When a transceiver omits the checksum here, we
259 * add an own calculated one. This is currently an ugly
260 * solution because the monitor needs a crc here.
261 */
262 if (local->hw.flags & IEEE802154_HW_RX_OMIT_CKSUM) {
Alexander Aringec718f32014-10-29 21:34:37 +0100263 crc = crc_ccitt(0, skb->data, skb->len);
Alexander Aringb7889492014-10-29 21:34:36 +0100264 put_unaligned_le16(crc, skb_put(skb, 2));
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000265 }
266
Alexander Aringe176b682014-10-27 17:13:39 +0100267 rcu_read_lock();
268
Alexander Aringbe9d2152014-10-27 17:13:43 +0100269 ieee802154_monitors_rx(local, skb);
Alexander Aringb7889492014-10-29 21:34:36 +0100270
Alexander Aringec718f32014-10-29 21:34:37 +0100271 /* Check if transceiver doesn't validate the checksum.
272 * If not we validate the checksum here.
273 */
274 if (local->hw.flags & IEEE802154_HW_RX_DROP_BAD_CKSUM) {
275 crc = crc_ccitt(0, skb->data, skb->len);
276 if (crc) {
277 rcu_read_unlock();
Alexander Aring3cf24cf2015-06-24 11:36:36 +0200278 goto drop;
Alexander Aringec718f32014-10-29 21:34:37 +0100279 }
280 }
Alexander Aringb7889492014-10-29 21:34:36 +0100281 /* remove crc */
282 skb_trim(skb, skb->len - 2);
283
Alexander Aringbe9d2152014-10-27 17:13:43 +0100284 __ieee802154_rx_handle_packet(local, skb);
Phoebe Buckheister2d3b5b02014-06-11 12:03:07 +0200285
Alexander Aringe176b682014-10-27 17:13:39 +0100286 rcu_read_unlock();
Alexander Aring3cf24cf2015-06-24 11:36:36 +0200287
288 return;
289drop:
290 kfree_skb(skb);
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000291}
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000292
293void
Alexander Aring5a504392014-10-25 17:16:34 +0200294ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000295{
Alexander Aring60741362014-10-25 17:16:39 +0200296 struct ieee802154_local *local = hw_to_local(hw);
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000297
Alexander Aringc5c47e62014-10-27 17:13:30 +0100298 mac_cb(skb)->lqi = lqi;
299 skb->pkt_type = IEEE802154_RX_MSG;
300 skb_queue_tail(&local->skb_queue, skb);
301 tasklet_schedule(&local->tasklet);
alex.bluesman.smirnov@gmail.com1cd829c2012-05-15 20:50:21 +0000302}
303EXPORT_SYMBOL(ieee802154_rx_irqsafe);