Merge tag 'pwm/for-3.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/thierry...
[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
50 spin_lock_bh(&sdata->mib_lock);
51
863e88f2
AA
52 span = wpan_dev->pan_id;
53 sshort = wpan_dev->short_addr;
2a9820c9
AA
54
55 switch (mac_cb(skb)->dest.mode) {
56 case IEEE802154_ADDR_NONE:
57 if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE)
58 /* FIXME: check if we are PAN coordinator */
59 skb->pkt_type = PACKET_OTHERHOST;
60 else
61 /* ACK comes with both addresses empty */
62 skb->pkt_type = PACKET_HOST;
63 break;
64 case IEEE802154_ADDR_LONG:
65 if (mac_cb(skb)->dest.pan_id != span &&
66 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
67 skb->pkt_type = PACKET_OTHERHOST;
863e88f2 68 else if (mac_cb(skb)->dest.extended_addr == wpan_dev->extended_addr)
2a9820c9
AA
69 skb->pkt_type = PACKET_HOST;
70 else
71 skb->pkt_type = PACKET_OTHERHOST;
72 break;
73 case IEEE802154_ADDR_SHORT:
74 if (mac_cb(skb)->dest.pan_id != span &&
75 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
76 skb->pkt_type = PACKET_OTHERHOST;
77 else if (mac_cb(skb)->dest.short_addr == sshort)
78 skb->pkt_type = PACKET_HOST;
79 else if (mac_cb(skb)->dest.short_addr ==
80 cpu_to_le16(IEEE802154_ADDR_BROADCAST))
81 skb->pkt_type = PACKET_BROADCAST;
82 else
83 skb->pkt_type = PACKET_OTHERHOST;
84 break;
85 default:
86 spin_unlock_bh(&sdata->mib_lock);
87 pr_debug("invalid dest mode\n");
bcb47aab 88 goto fail;
2a9820c9
AA
89 }
90
91 spin_unlock_bh(&sdata->mib_lock);
92
93 skb->dev = sdata->dev;
94
95 rc = mac802154_llsec_decrypt(&sdata->sec, skb);
96 if (rc) {
97 pr_debug("decryption failed: %i\n", rc);
98 goto fail;
99 }
100
101 sdata->dev->stats.rx_packets++;
102 sdata->dev->stats.rx_bytes += skb->len;
103
104 switch (mac_cb(skb)->type) {
105 case IEEE802154_FC_TYPE_DATA:
08c511a7 106 return ieee802154_deliver_skb(skb);
2a9820c9
AA
107 default:
108 pr_warn("ieee802154: bad frame received (type = %d)\n",
109 mac_cb(skb)->type);
110 goto fail;
111 }
112
113fail:
114 kfree_skb(skb);
115 return NET_RX_DROP;
116}
117
be9d215f
AA
118static void
119ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr)
2a9820c9
AA
120{
121 if (addr->mode == IEEE802154_ADDR_NONE)
122 pr_debug("%s not present\n", name);
123
124 pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
125 if (addr->mode == IEEE802154_ADDR_SHORT) {
126 pr_debug("%s is short: %04x\n", name,
127 le16_to_cpu(addr->short_addr));
128 } else {
129 u64 hw = swab64((__force u64)addr->extended_addr);
130
131 pr_debug("%s is hardware: %8phC\n", name, &hw);
132 }
133}
134
be9d215f
AA
135static int
136ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr)
2a9820c9
AA
137{
138 int hlen;
139 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
140
9cf215d0
AA
141 skb_reset_mac_header(skb);
142
2a9820c9
AA
143 hlen = ieee802154_hdr_pull(skb, hdr);
144 if (hlen < 0)
145 return -EINVAL;
146
147 skb->mac_len = hlen;
148
149 pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
150 hdr->seq);
151
152 cb->type = hdr->fc.type;
153 cb->ackreq = hdr->fc.ack_request;
154 cb->secen = hdr->fc.security_enabled;
155
be9d215f
AA
156 ieee802154_print_addr("destination", &hdr->dest);
157 ieee802154_print_addr("source", &hdr->source);
2a9820c9
AA
158
159 cb->source = hdr->source;
160 cb->dest = hdr->dest;
161
162 if (hdr->fc.security_enabled) {
163 u64 key;
164
165 pr_debug("seclevel %i\n", hdr->sec.level);
166
167 switch (hdr->sec.key_id_mode) {
168 case IEEE802154_SCF_KEY_IMPLICIT:
169 pr_debug("implicit key\n");
170 break;
171
172 case IEEE802154_SCF_KEY_INDEX:
173 pr_debug("key %02x\n", hdr->sec.key_id);
174 break;
175
176 case IEEE802154_SCF_KEY_SHORT_INDEX:
177 pr_debug("key %04x:%04x %02x\n",
178 le32_to_cpu(hdr->sec.short_src) >> 16,
179 le32_to_cpu(hdr->sec.short_src) & 0xffff,
180 hdr->sec.key_id);
181 break;
182
183 case IEEE802154_SCF_KEY_HW_INDEX:
184 key = swab64((__force u64)hdr->sec.extended_src);
185 pr_debug("key source %8phC %02x\n", &key,
186 hdr->sec.key_id);
187 break;
188 }
189 }
190
191 return 0;
192}
193
194static void
be9d215f
AA
195__ieee802154_rx_handle_packet(struct ieee802154_local *local,
196 struct sk_buff *skb)
2a9820c9
AA
197{
198 int ret;
199 struct ieee802154_sub_if_data *sdata;
200 struct ieee802154_hdr hdr;
201
be9d215f 202 ret = ieee802154_parse_frame_start(skb, &hdr);
2a9820c9
AA
203 if (ret) {
204 pr_debug("got invalid frame\n");
205 kfree_skb(skb);
206 return;
207 }
208
2a9820c9 209 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
944742a3 210 if (sdata->vif.type != NL802154_IFTYPE_NODE ||
2a9820c9
AA
211 !netif_running(sdata->dev))
212 continue;
213
be9d215f 214 ieee802154_subif_frame(sdata, skb, &hdr);
2a9820c9
AA
215 skb = NULL;
216 break;
217 }
2a9820c9
AA
218
219 if (skb)
220 kfree_skb(skb);
221}
222
4ca18be5 223static void
be9d215f 224ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb)
2a9820c9
AA
225{
226 struct sk_buff *skb2;
227 struct ieee802154_sub_if_data *sdata;
2a9820c9 228
9cf215d0 229 skb_reset_mac_header(skb);
75a46f0e 230 skb->ip_summed = CHECKSUM_UNNECESSARY;
c9ca6401 231 skb->pkt_type = PACKET_OTHERHOST;
702dcf99
AA
232 skb->protocol = htons(ETH_P_IEEE802154);
233
2a9820c9 234 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
944742a3 235 if (sdata->vif.type != NL802154_IFTYPE_MONITOR)
20b48120
AA
236 continue;
237
238 if (!ieee802154_sdata_running(sdata))
2a9820c9
AA
239 continue;
240
241 skb2 = skb_clone(skb, GFP_ATOMIC);
05f7de67
AA
242 if (skb2) {
243 skb2->dev = sdata->dev;
244 ieee802154_deliver_skb(skb2);
2a9820c9 245
05f7de67
AA
246 sdata->dev->stats.rx_packets++;
247 sdata->dev->stats.rx_bytes += skb->len;
248 }
2a9820c9 249 }
2a9820c9
AA
250}
251
469100d6 252void ieee802154_rx(struct ieee802154_hw *hw, struct sk_buff *skb)
1cd829c8 253{
60741361 254 struct ieee802154_local *local = hw_to_local(hw);
ec718f3d 255 u16 crc;
1cd829c8 256
469100d6
AA
257 WARN_ON_ONCE(softirq_count() == 0);
258
b7889497
AA
259 /* TODO: When a transceiver omits the checksum here, we
260 * add an own calculated one. This is currently an ugly
261 * solution because the monitor needs a crc here.
262 */
263 if (local->hw.flags & IEEE802154_HW_RX_OMIT_CKSUM) {
ec718f3d 264 crc = crc_ccitt(0, skb->data, skb->len);
b7889497 265 put_unaligned_le16(crc, skb_put(skb, 2));
1cd829c8 266 }
267
e176b681
AA
268 rcu_read_lock();
269
be9d215f 270 ieee802154_monitors_rx(local, skb);
2d3b5b0a 271
ec718f3d
AA
272 /* Check if transceiver doesn't validate the checksum.
273 * If not we validate the checksum here.
274 */
275 if (local->hw.flags & IEEE802154_HW_RX_DROP_BAD_CKSUM) {
276 crc = crc_ccitt(0, skb->data, skb->len);
277 if (crc) {
278 rcu_read_unlock();
279 kfree_skb(skb);
280 return;
281 }
282 }
b7889497
AA
283 /* remove crc */
284 skb_trim(skb, skb->len - 2);
e176b681 285
b7889497 286 __ieee802154_rx_handle_packet(local, skb);
2d3b5b0a 287
b7889497 288 rcu_read_unlock();
1cd829c8 289}
c5c47e67 290EXPORT_SYMBOL(ieee802154_rx);
1cd829c8 291
292void
5a504397 293ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
1cd829c8 294{
60741361 295 struct ieee802154_local *local = hw_to_local(hw);
1cd829c8 296
c5c47e67
AA
297 mac_cb(skb)->lqi = lqi;
298 skb->pkt_type = IEEE802154_RX_MSG;
299 skb_queue_tail(&local->skb_queue, skb);
300 tasklet_schedule(&local->tasklet);
1cd829c8 301}
302EXPORT_SYMBOL(ieee802154_rx_irqsafe);
This page took 0.205894 seconds and 5 git commands to generate.