Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2007-2012 Siemens AG
4 *
5 * 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>
14#include <linux/netdevice.h>
15#include <linux/crc-ccitt.h>
16#include <asm/unaligned.h>
17
18#include <net/mac802154.h>
19#include <net/ieee802154_netdev.h>
20#include <net/nl802154.h>
21
22#include "ieee802154_i.h"
23
24static int ieee802154_deliver_skb(struct sk_buff *skb)
25{
26 skb->ip_summed = CHECKSUM_UNNECESSARY;
27 skb->protocol = htons(ETH_P_IEEE802154);
28
29 return netif_receive_skb(skb);
30}
31
32static int
33ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata,
34 struct sk_buff *skb, const struct ieee802154_hdr *hdr)
35{
36 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
37 __le16 span, sshort;
38 int rc;
39
40 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
41
42 span = wpan_dev->pan_id;
43 sshort = wpan_dev->short_addr;
44
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;
58 else if (mac_cb(skb)->dest.extended_addr == wpan_dev->extended_addr)
59 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:
76 pr_debug("invalid dest mode\n");
77 goto fail;
78 }
79
80 skb->dev = sdata->dev;
81
82 /* 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 */
86 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) {
96 case IEEE802154_FC_TYPE_BEACON:
97 case IEEE802154_FC_TYPE_ACK:
98 case IEEE802154_FC_TYPE_MAC_CMD:
99 goto fail;
100
101 case IEEE802154_FC_TYPE_DATA:
102 return ieee802154_deliver_skb(skb);
103 default:
104 pr_warn_ratelimited("ieee802154: bad frame received "
105 "(type = %d)\n", mac_cb(skb)->type);
106 goto fail;
107 }
108
109fail:
110 kfree_skb(skb);
111 return NET_RX_DROP;
112}
113
114static void
115ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr)
116{
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
131static int
132ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr)
133{
134 int hlen;
135 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
136
137 skb_reset_mac_header(skb);
138
139 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
152 ieee802154_print_addr("destination", &hdr->dest);
153 ieee802154_print_addr("source", &hdr->source);
154
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
191__ieee802154_rx_handle_packet(struct ieee802154_local *local,
192 struct sk_buff *skb)
193{
194 int ret;
195 struct ieee802154_sub_if_data *sdata;
196 struct ieee802154_hdr hdr;
197
198 ret = ieee802154_parse_frame_start(skb, &hdr);
199 if (ret) {
200 pr_debug("got invalid frame\n");
201 kfree_skb(skb);
202 return;
203 }
204
205 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
206 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_NODE)
207 continue;
208
209 if (!ieee802154_sdata_running(sdata))
210 continue;
211
212 ieee802154_subif_frame(sdata, skb, &hdr);
213 skb = NULL;
214 break;
215 }
216
217 kfree_skb(skb);
218}
219
220static void
221ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb)
222{
223 struct sk_buff *skb2;
224 struct ieee802154_sub_if_data *sdata;
225
226 skb_reset_mac_header(skb);
227 skb->ip_summed = CHECKSUM_UNNECESSARY;
228 skb->pkt_type = PACKET_OTHERHOST;
229 skb->protocol = htons(ETH_P_IEEE802154);
230
231 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
232 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR)
233 continue;
234
235 if (!ieee802154_sdata_running(sdata))
236 continue;
237
238 skb2 = skb_clone(skb, GFP_ATOMIC);
239 if (skb2) {
240 skb2->dev = sdata->dev;
241 ieee802154_deliver_skb(skb2);
242
243 sdata->dev->stats.rx_packets++;
244 sdata->dev->stats.rx_bytes += skb->len;
245 }
246 }
247}
248
249void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb)
250{
251 u16 crc;
252
253 WARN_ON_ONCE(softirq_count() == 0);
254
255 if (local->suspended)
256 goto drop;
257
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) {
263 crc = crc_ccitt(0, skb->data, skb->len);
264 put_unaligned_le16(crc, skb_put(skb, 2));
265 }
266
267 rcu_read_lock();
268
269 ieee802154_monitors_rx(local, skb);
270
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();
278 goto drop;
279 }
280 }
281 /* remove crc */
282 skb_trim(skb, skb->len - 2);
283
284 __ieee802154_rx_handle_packet(local, skb);
285
286 rcu_read_unlock();
287
288 return;
289drop:
290 kfree_skb(skb);
291}
292
293void
294ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
295{
296 struct ieee802154_local *local = hw_to_local(hw);
297
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);
302}
303EXPORT_SYMBOL(ieee802154_rx_irqsafe);
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 *
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>
22#include <linux/netdevice.h>
23#include <linux/crc-ccitt.h>
24#include <asm/unaligned.h>
25
26#include <net/mac802154.h>
27#include <net/ieee802154_netdev.h>
28#include <net/nl802154.h>
29
30#include "ieee802154_i.h"
31
32static int ieee802154_deliver_skb(struct sk_buff *skb)
33{
34 skb->ip_summed = CHECKSUM_UNNECESSARY;
35 skb->protocol = htons(ETH_P_IEEE802154);
36
37 return netif_receive_skb(skb);
38}
39
40static int
41ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata,
42 struct sk_buff *skb, const struct ieee802154_hdr *hdr)
43{
44 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
45 __le16 span, sshort;
46 int rc;
47
48 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
49
50 span = wpan_dev->pan_id;
51 sshort = wpan_dev->short_addr;
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;
66 else if (mac_cb(skb)->dest.extended_addr == wpan_dev->extended_addr)
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:
84 pr_debug("invalid dest mode\n");
85 goto fail;
86 }
87
88 skb->dev = sdata->dev;
89
90 /* TODO this should be moved after netif_receive_skb call, otherwise
91 * wireshark will show a mac header with security fields and the
92 * payload is already decrypted.
93 */
94 rc = mac802154_llsec_decrypt(&sdata->sec, skb);
95 if (rc) {
96 pr_debug("decryption failed: %i\n", rc);
97 goto fail;
98 }
99
100 sdata->dev->stats.rx_packets++;
101 sdata->dev->stats.rx_bytes += skb->len;
102
103 switch (mac_cb(skb)->type) {
104 case IEEE802154_FC_TYPE_BEACON:
105 case IEEE802154_FC_TYPE_ACK:
106 case IEEE802154_FC_TYPE_MAC_CMD:
107 goto fail;
108
109 case IEEE802154_FC_TYPE_DATA:
110 return ieee802154_deliver_skb(skb);
111 default:
112 pr_warn_ratelimited("ieee802154: bad frame received "
113 "(type = %d)\n", mac_cb(skb)->type);
114 goto fail;
115 }
116
117fail:
118 kfree_skb(skb);
119 return NET_RX_DROP;
120}
121
122static void
123ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr)
124{
125 if (addr->mode == IEEE802154_ADDR_NONE)
126 pr_debug("%s not present\n", name);
127
128 pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
129 if (addr->mode == IEEE802154_ADDR_SHORT) {
130 pr_debug("%s is short: %04x\n", name,
131 le16_to_cpu(addr->short_addr));
132 } else {
133 u64 hw = swab64((__force u64)addr->extended_addr);
134
135 pr_debug("%s is hardware: %8phC\n", name, &hw);
136 }
137}
138
139static int
140ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr)
141{
142 int hlen;
143 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
144
145 skb_reset_mac_header(skb);
146
147 hlen = ieee802154_hdr_pull(skb, hdr);
148 if (hlen < 0)
149 return -EINVAL;
150
151 skb->mac_len = hlen;
152
153 pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
154 hdr->seq);
155
156 cb->type = hdr->fc.type;
157 cb->ackreq = hdr->fc.ack_request;
158 cb->secen = hdr->fc.security_enabled;
159
160 ieee802154_print_addr("destination", &hdr->dest);
161 ieee802154_print_addr("source", &hdr->source);
162
163 cb->source = hdr->source;
164 cb->dest = hdr->dest;
165
166 if (hdr->fc.security_enabled) {
167 u64 key;
168
169 pr_debug("seclevel %i\n", hdr->sec.level);
170
171 switch (hdr->sec.key_id_mode) {
172 case IEEE802154_SCF_KEY_IMPLICIT:
173 pr_debug("implicit key\n");
174 break;
175
176 case IEEE802154_SCF_KEY_INDEX:
177 pr_debug("key %02x\n", hdr->sec.key_id);
178 break;
179
180 case IEEE802154_SCF_KEY_SHORT_INDEX:
181 pr_debug("key %04x:%04x %02x\n",
182 le32_to_cpu(hdr->sec.short_src) >> 16,
183 le32_to_cpu(hdr->sec.short_src) & 0xffff,
184 hdr->sec.key_id);
185 break;
186
187 case IEEE802154_SCF_KEY_HW_INDEX:
188 key = swab64((__force u64)hdr->sec.extended_src);
189 pr_debug("key source %8phC %02x\n", &key,
190 hdr->sec.key_id);
191 break;
192 }
193 }
194
195 return 0;
196}
197
198static void
199__ieee802154_rx_handle_packet(struct ieee802154_local *local,
200 struct sk_buff *skb)
201{
202 int ret;
203 struct ieee802154_sub_if_data *sdata;
204 struct ieee802154_hdr hdr;
205
206 ret = ieee802154_parse_frame_start(skb, &hdr);
207 if (ret) {
208 pr_debug("got invalid frame\n");
209 kfree_skb(skb);
210 return;
211 }
212
213 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
214 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_NODE)
215 continue;
216
217 if (!ieee802154_sdata_running(sdata))
218 continue;
219
220 ieee802154_subif_frame(sdata, skb, &hdr);
221 skb = NULL;
222 break;
223 }
224
225 kfree_skb(skb);
226}
227
228static void
229ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb)
230{
231 struct sk_buff *skb2;
232 struct ieee802154_sub_if_data *sdata;
233
234 skb_reset_mac_header(skb);
235 skb->ip_summed = CHECKSUM_UNNECESSARY;
236 skb->pkt_type = PACKET_OTHERHOST;
237 skb->protocol = htons(ETH_P_IEEE802154);
238
239 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
240 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR)
241 continue;
242
243 if (!ieee802154_sdata_running(sdata))
244 continue;
245
246 skb2 = skb_clone(skb, GFP_ATOMIC);
247 if (skb2) {
248 skb2->dev = sdata->dev;
249 ieee802154_deliver_skb(skb2);
250
251 sdata->dev->stats.rx_packets++;
252 sdata->dev->stats.rx_bytes += skb->len;
253 }
254 }
255}
256
257void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb)
258{
259 u16 crc;
260
261 WARN_ON_ONCE(softirq_count() == 0);
262
263 if (local->suspended)
264 goto drop;
265
266 /* TODO: When a transceiver omits the checksum here, we
267 * add an own calculated one. This is currently an ugly
268 * solution because the monitor needs a crc here.
269 */
270 if (local->hw.flags & IEEE802154_HW_RX_OMIT_CKSUM) {
271 crc = crc_ccitt(0, skb->data, skb->len);
272 put_unaligned_le16(crc, skb_put(skb, 2));
273 }
274
275 rcu_read_lock();
276
277 ieee802154_monitors_rx(local, skb);
278
279 /* Check if transceiver doesn't validate the checksum.
280 * If not we validate the checksum here.
281 */
282 if (local->hw.flags & IEEE802154_HW_RX_DROP_BAD_CKSUM) {
283 crc = crc_ccitt(0, skb->data, skb->len);
284 if (crc) {
285 rcu_read_unlock();
286 goto drop;
287 }
288 }
289 /* remove crc */
290 skb_trim(skb, skb->len - 2);
291
292 __ieee802154_rx_handle_packet(local, skb);
293
294 rcu_read_unlock();
295
296 return;
297drop:
298 kfree_skb(skb);
299}
300
301void
302ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
303{
304 struct ieee802154_local *local = hw_to_local(hw);
305
306 mac_cb(skb)->lqi = lqi;
307 skb->pkt_type = IEEE802154_RX_MSG;
308 skb_queue_tail(&local->skb_queue, skb);
309 tasklet_schedule(&local->tasklet);
310}
311EXPORT_SYMBOL(ieee802154_rx_irqsafe);