Merge remote-tracking branches 'regulator/topic/load', 'regulator/topic/max77802...
[deliverable/linux.git] / net / mac802154 / rx.c
CommitLineData
1cd829c8 1/*
2 * Copyright (C) 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 *
1cd829c8 13 * Written by:
14 * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
15 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
16 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
17 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
18 */
19
20#include <linux/kernel.h>
21#include <linux/module.h>
1cd829c8 22#include <linux/netdevice.h>
23#include <linux/crc-ccitt.h>
b7889497 24#include <asm/unaligned.h>
1cd829c8 25
26#include <net/mac802154.h>
27#include <net/ieee802154_netdev.h>
944742a3 28#include <net/nl802154.h>
1cd829c8 29
0f1556bc 30#include "ieee802154_i.h"
1cd829c8 31
08c511a7 32static int ieee802154_deliver_skb(struct sk_buff *skb)
2a9820c9 33{
75a46f0e 34 skb->ip_summed = CHECKSUM_UNNECESSARY;
702dcf99
AA
35 skb->protocol = htons(ETH_P_IEEE802154);
36
2a9820c9
AA
37 return netif_receive_skb(skb);
38}
39
40static int
be9d215f
AA
41ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata,
42 struct sk_buff *skb, const struct ieee802154_hdr *hdr)
2a9820c9 43{
863e88f2 44 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
2a9820c9
AA
45 __le16 span, sshort;
46 int rc;
47
48 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
49
863e88f2
AA
50 span = wpan_dev->pan_id;
51 sshort = wpan_dev->short_addr;
2a9820c9
AA
52
53 switch (mac_cb(skb)->dest.mode) {
54 case IEEE802154_ADDR_NONE:
55 if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
56 /* FIXME: check if we are PAN coordinator */
57 skb->pkt_type = PACKET_OTHERHOST;
58 else
59 /* ACK comes with both addresses empty */
60 skb->pkt_type = PACKET_HOST;
61 break;
62 case IEEE802154_ADDR_LONG:
63 if (mac_cb(skb)->dest.pan_id != span &&
64 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
65 skb->pkt_type = PACKET_OTHERHOST;
863e88f2 66 else if (mac_cb(skb)->dest.extended_addr == wpan_dev->extended_addr)
2a9820c9
AA
67 skb->pkt_type = PACKET_HOST;
68 else
69 skb->pkt_type = PACKET_OTHERHOST;
70 break;
71 case IEEE802154_ADDR_SHORT:
72 if (mac_cb(skb)->dest.pan_id != span &&
73 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
74 skb->pkt_type = PACKET_OTHERHOST;
75 else if (mac_cb(skb)->dest.short_addr == sshort)
76 skb->pkt_type = PACKET_HOST;
77 else if (mac_cb(skb)->dest.short_addr ==
78 cpu_to_le16(IEEE802154_ADDR_BROADCAST))
79 skb->pkt_type = PACKET_BROADCAST;
80 else
81 skb->pkt_type = PACKET_OTHERHOST;
82 break;
83 default:
2a9820c9 84 pr_debug("invalid dest mode\n");
bcb47aab 85 goto fail;
2a9820c9
AA
86 }
87
2a9820c9
AA
88 skb->dev = sdata->dev;
89
90 rc = mac802154_llsec_decrypt(&sdata->sec, skb);
91 if (rc) {
92 pr_debug("decryption failed: %i\n", rc);
93 goto fail;
94 }
95
96 sdata->dev->stats.rx_packets++;
97 sdata->dev->stats.rx_bytes += skb->len;
98
99 switch (mac_cb(skb)->type) {
100 case IEEE802154_FC_TYPE_DATA:
08c511a7 101 return ieee802154_deliver_skb(skb);
2a9820c9
AA
102 default:
103 pr_warn("ieee802154: bad frame received (type = %d)\n",
104 mac_cb(skb)->type);
105 goto fail;
106 }
107
108fail:
109 kfree_skb(skb);
110 return NET_RX_DROP;
111}
112
be9d215f
AA
113static void
114ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr)
2a9820c9
AA
115{
116 if (addr->mode == IEEE802154_ADDR_NONE)
117 pr_debug("%s not present\n", name);
118
119 pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
120 if (addr->mode == IEEE802154_ADDR_SHORT) {
121 pr_debug("%s is short: %04x\n", name,
122 le16_to_cpu(addr->short_addr));
123 } else {
124 u64 hw = swab64((__force u64)addr->extended_addr);
125
126 pr_debug("%s is hardware: %8phC\n", name, &hw);
127 }
128}
129
be9d215f
AA
130static int
131ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr)
2a9820c9
AA
132{
133 int hlen;
134 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
135
9cf215d0
AA
136 skb_reset_mac_header(skb);
137
2a9820c9
AA
138 hlen = ieee802154_hdr_pull(skb, hdr);
139 if (hlen < 0)
140 return -EINVAL;
141
142 skb->mac_len = hlen;
143
144 pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
145 hdr->seq);
146
147 cb->type = hdr->fc.type;
148 cb->ackreq = hdr->fc.ack_request;
149 cb->secen = hdr->fc.security_enabled;
150
be9d215f
AA
151 ieee802154_print_addr("destination", &hdr->dest);
152 ieee802154_print_addr("source", &hdr->source);
2a9820c9
AA
153
154 cb->source = hdr->source;
155 cb->dest = hdr->dest;
156
157 if (hdr->fc.security_enabled) {
158 u64 key;
159
160 pr_debug("seclevel %i\n", hdr->sec.level);
161
162 switch (hdr->sec.key_id_mode) {
163 case IEEE802154_SCF_KEY_IMPLICIT:
164 pr_debug("implicit key\n");
165 break;
166
167 case IEEE802154_SCF_KEY_INDEX:
168 pr_debug("key %02x\n", hdr->sec.key_id);
169 break;
170
171 case IEEE802154_SCF_KEY_SHORT_INDEX:
172 pr_debug("key %04x:%04x %02x\n",
173 le32_to_cpu(hdr->sec.short_src) >> 16,
174 le32_to_cpu(hdr->sec.short_src) & 0xffff,
175 hdr->sec.key_id);
176 break;
177
178 case IEEE802154_SCF_KEY_HW_INDEX:
179 key = swab64((__force u64)hdr->sec.extended_src);
180 pr_debug("key source %8phC %02x\n", &key,
181 hdr->sec.key_id);
182 break;
183 }
184 }
185
186 return 0;
187}
188
189static void
be9d215f
AA
190__ieee802154_rx_handle_packet(struct ieee802154_local *local,
191 struct sk_buff *skb)
2a9820c9
AA
192{
193 int ret;
194 struct ieee802154_sub_if_data *sdata;
195 struct ieee802154_hdr hdr;
196
be9d215f 197 ret = ieee802154_parse_frame_start(skb, &hdr);
2a9820c9
AA
198 if (ret) {
199 pr_debug("got invalid frame\n");
200 kfree_skb(skb);
201 return;
202 }
203
2a9820c9 204 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
1bc1754e
VB
205 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_NODE)
206 continue;
207
208 if (!ieee802154_sdata_running(sdata))
2a9820c9
AA
209 continue;
210
be9d215f 211 ieee802154_subif_frame(sdata, skb, &hdr);
2a9820c9
AA
212 skb = NULL;
213 break;
214 }
2a9820c9
AA
215
216 if (skb)
217 kfree_skb(skb);
218}
219
4ca18be5 220static void
be9d215f 221ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb)
2a9820c9
AA
222{
223 struct sk_buff *skb2;
224 struct ieee802154_sub_if_data *sdata;
2a9820c9 225
9cf215d0 226 skb_reset_mac_header(skb);
75a46f0e 227 skb->ip_summed = CHECKSUM_UNNECESSARY;
c9ca6401 228 skb->pkt_type = PACKET_OTHERHOST;
702dcf99
AA
229 skb->protocol = htons(ETH_P_IEEE802154);
230
2a9820c9 231 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
ed65963b 232 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR)
20b48120
AA
233 continue;
234
235 if (!ieee802154_sdata_running(sdata))
2a9820c9
AA
236 continue;
237
238 skb2 = skb_clone(skb, GFP_ATOMIC);
05f7de67
AA
239 if (skb2) {
240 skb2->dev = sdata->dev;
241 ieee802154_deliver_skb(skb2);
2a9820c9 242
05f7de67
AA
243 sdata->dev->stats.rx_packets++;
244 sdata->dev->stats.rx_bytes += skb->len;
245 }
2a9820c9 246 }
2a9820c9
AA
247}
248
d10270ce 249void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb)
1cd829c8 250{
ec718f3d 251 u16 crc;
1cd829c8 252
469100d6
AA
253 WARN_ON_ONCE(softirq_count() == 0);
254
3cf24cf8
AA
255 if (local->suspended)
256 goto drop;
257
b7889497
AA
258 /* 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) {
ec718f3d 263 crc = crc_ccitt(0, skb->data, skb->len);
b7889497 264 put_unaligned_le16(crc, skb_put(skb, 2));
1cd829c8 265 }
266
e176b681
AA
267 rcu_read_lock();
268
be9d215f 269 ieee802154_monitors_rx(local, skb);
2d3b5b0a 270
ec718f3d
AA
271 /* 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();
3cf24cf8 278 goto drop;
ec718f3d
AA
279 }
280 }
b7889497
AA
281 /* remove crc */
282 skb_trim(skb, skb->len - 2);
e176b681 283
b7889497 284 __ieee802154_rx_handle_packet(local, skb);
2d3b5b0a 285
b7889497 286 rcu_read_unlock();
3cf24cf8
AA
287
288 return;
289drop:
290 kfree_skb(skb);
1cd829c8 291}
1cd829c8 292
293void
5a504397 294ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
1cd829c8 295{
60741361 296 struct ieee802154_local *local = hw_to_local(hw);
1cd829c8 297
c5c47e67
AA
298 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);
1cd829c8 302}
303EXPORT_SYMBOL(ieee802154_rx_irqsafe);
This page took 0.198742 seconds and 5 git commands to generate.