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
ieee802154_deliver_skb(struct sk_buff * skb)24 static 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
mac802154_rx_beacon_worker(struct work_struct * work)32 void mac802154_rx_beacon_worker(struct work_struct *work)
33 {
34 struct ieee802154_local *local =
35 container_of(work, struct ieee802154_local, rx_beacon_work);
36 struct cfg802154_mac_pkt *mac_pkt;
37
38 mac_pkt = list_first_entry_or_null(&local->rx_beacon_list,
39 struct cfg802154_mac_pkt, node);
40 if (!mac_pkt)
41 return;
42
43 mac802154_process_beacon(local, mac_pkt->skb, mac_pkt->page, mac_pkt->channel);
44
45 list_del(&mac_pkt->node);
46 kfree_skb(mac_pkt->skb);
47 kfree(mac_pkt);
48 }
49
50 static int
ieee802154_subif_frame(struct ieee802154_sub_if_data * sdata,struct sk_buff * skb,const struct ieee802154_hdr * hdr)51 ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata,
52 struct sk_buff *skb, const struct ieee802154_hdr *hdr)
53 {
54 struct wpan_phy *wpan_phy = sdata->local->hw.phy;
55 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
56 struct cfg802154_mac_pkt *mac_pkt;
57 __le16 span, sshort;
58 int rc;
59
60 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
61
62 span = wpan_dev->pan_id;
63 sshort = wpan_dev->short_addr;
64
65 /* Level 3 filtering: Only beacons are accepted during scans */
66 if (sdata->required_filtering == IEEE802154_FILTERING_3_SCAN &&
67 sdata->required_filtering > wpan_phy->filtering) {
68 if (mac_cb(skb)->type != IEEE802154_FC_TYPE_BEACON) {
69 dev_dbg(&sdata->dev->dev,
70 "drop non-beacon frame (0x%x) during scan\n",
71 mac_cb(skb)->type);
72 goto fail;
73 }
74 }
75
76 switch (mac_cb(skb)->dest.mode) {
77 case IEEE802154_ADDR_NONE:
78 if (hdr->source.mode != IEEE802154_ADDR_NONE)
79 /* FIXME: check if we are PAN coordinator */
80 skb->pkt_type = PACKET_OTHERHOST;
81 else
82 /* ACK comes with both addresses empty */
83 skb->pkt_type = PACKET_HOST;
84 break;
85 case IEEE802154_ADDR_LONG:
86 if (mac_cb(skb)->dest.pan_id != span &&
87 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
88 skb->pkt_type = PACKET_OTHERHOST;
89 else if (mac_cb(skb)->dest.extended_addr == wpan_dev->extended_addr)
90 skb->pkt_type = PACKET_HOST;
91 else
92 skb->pkt_type = PACKET_OTHERHOST;
93 break;
94 case IEEE802154_ADDR_SHORT:
95 if (mac_cb(skb)->dest.pan_id != span &&
96 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
97 skb->pkt_type = PACKET_OTHERHOST;
98 else if (mac_cb(skb)->dest.short_addr == sshort)
99 skb->pkt_type = PACKET_HOST;
100 else if (mac_cb(skb)->dest.short_addr ==
101 cpu_to_le16(IEEE802154_ADDR_BROADCAST))
102 skb->pkt_type = PACKET_BROADCAST;
103 else
104 skb->pkt_type = PACKET_OTHERHOST;
105 break;
106 default:
107 pr_debug("invalid dest mode\n");
108 goto fail;
109 }
110
111 skb->dev = sdata->dev;
112
113 /* TODO this should be moved after netif_receive_skb call, otherwise
114 * wireshark will show a mac header with security fields and the
115 * payload is already decrypted.
116 */
117 rc = mac802154_llsec_decrypt(&sdata->sec, skb);
118 if (rc) {
119 pr_debug("decryption failed: %i\n", rc);
120 goto fail;
121 }
122
123 sdata->dev->stats.rx_packets++;
124 sdata->dev->stats.rx_bytes += skb->len;
125
126 switch (mac_cb(skb)->type) {
127 case IEEE802154_FC_TYPE_BEACON:
128 dev_dbg(&sdata->dev->dev, "BEACON received\n");
129 if (!mac802154_is_scanning(sdata->local))
130 goto fail;
131
132 mac_pkt = kzalloc(sizeof(*mac_pkt), GFP_ATOMIC);
133 if (!mac_pkt)
134 goto fail;
135
136 mac_pkt->skb = skb_get(skb);
137 mac_pkt->sdata = sdata;
138 mac_pkt->page = sdata->local->scan_page;
139 mac_pkt->channel = sdata->local->scan_channel;
140 list_add_tail(&mac_pkt->node, &sdata->local->rx_beacon_list);
141 queue_work(sdata->local->mac_wq, &sdata->local->rx_beacon_work);
142 return NET_RX_SUCCESS;
143 case IEEE802154_FC_TYPE_ACK:
144 case IEEE802154_FC_TYPE_MAC_CMD:
145 goto fail;
146
147 case IEEE802154_FC_TYPE_DATA:
148 return ieee802154_deliver_skb(skb);
149 default:
150 pr_warn_ratelimited("ieee802154: bad frame received "
151 "(type = %d)\n", mac_cb(skb)->type);
152 goto fail;
153 }
154
155 fail:
156 kfree_skb(skb);
157 return NET_RX_DROP;
158 }
159
160 static void
ieee802154_print_addr(const char * name,const struct ieee802154_addr * addr)161 ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr)
162 {
163 if (addr->mode == IEEE802154_ADDR_NONE) {
164 pr_debug("%s not present\n", name);
165 return;
166 }
167
168 pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
169 if (addr->mode == IEEE802154_ADDR_SHORT) {
170 pr_debug("%s is short: %04x\n", name,
171 le16_to_cpu(addr->short_addr));
172 } else {
173 u64 hw = swab64((__force u64)addr->extended_addr);
174
175 pr_debug("%s is hardware: %8phC\n", name, &hw);
176 }
177 }
178
179 static int
ieee802154_parse_frame_start(struct sk_buff * skb,struct ieee802154_hdr * hdr)180 ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr)
181 {
182 int hlen;
183 struct ieee802154_mac_cb *cb = mac_cb(skb);
184
185 skb_reset_mac_header(skb);
186
187 hlen = ieee802154_hdr_pull(skb, hdr);
188 if (hlen < 0)
189 return -EINVAL;
190
191 skb->mac_len = hlen;
192
193 pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
194 hdr->seq);
195
196 cb->type = hdr->fc.type;
197 cb->ackreq = hdr->fc.ack_request;
198 cb->secen = hdr->fc.security_enabled;
199
200 ieee802154_print_addr("destination", &hdr->dest);
201 ieee802154_print_addr("source", &hdr->source);
202
203 cb->source = hdr->source;
204 cb->dest = hdr->dest;
205
206 if (hdr->fc.security_enabled) {
207 u64 key;
208
209 pr_debug("seclevel %i\n", hdr->sec.level);
210
211 switch (hdr->sec.key_id_mode) {
212 case IEEE802154_SCF_KEY_IMPLICIT:
213 pr_debug("implicit key\n");
214 break;
215
216 case IEEE802154_SCF_KEY_INDEX:
217 pr_debug("key %02x\n", hdr->sec.key_id);
218 break;
219
220 case IEEE802154_SCF_KEY_SHORT_INDEX:
221 pr_debug("key %04x:%04x %02x\n",
222 le32_to_cpu(hdr->sec.short_src) >> 16,
223 le32_to_cpu(hdr->sec.short_src) & 0xffff,
224 hdr->sec.key_id);
225 break;
226
227 case IEEE802154_SCF_KEY_HW_INDEX:
228 key = swab64((__force u64)hdr->sec.extended_src);
229 pr_debug("key source %8phC %02x\n", &key,
230 hdr->sec.key_id);
231 break;
232 }
233 }
234
235 return 0;
236 }
237
238 static void
__ieee802154_rx_handle_packet(struct ieee802154_local * local,struct sk_buff * skb)239 __ieee802154_rx_handle_packet(struct ieee802154_local *local,
240 struct sk_buff *skb)
241 {
242 int ret;
243 struct ieee802154_sub_if_data *sdata;
244 struct ieee802154_hdr hdr;
245 struct sk_buff *skb2;
246
247 ret = ieee802154_parse_frame_start(skb, &hdr);
248 if (ret) {
249 pr_debug("got invalid frame\n");
250 return;
251 }
252
253 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
254 if (sdata->wpan_dev.iftype == NL802154_IFTYPE_MONITOR)
255 continue;
256
257 if (!ieee802154_sdata_running(sdata))
258 continue;
259
260 /* Do not deliver packets received on interfaces expecting
261 * AACK=1 if the address filters where disabled.
262 */
263 if (local->hw.phy->filtering < IEEE802154_FILTERING_4_FRAME_FIELDS &&
264 sdata->required_filtering == IEEE802154_FILTERING_4_FRAME_FIELDS)
265 continue;
266
267 skb2 = skb_clone(skb, GFP_ATOMIC);
268 if (skb2) {
269 skb2->dev = sdata->dev;
270 ieee802154_subif_frame(sdata, skb2, &hdr);
271 }
272 }
273 }
274
275 static void
ieee802154_monitors_rx(struct ieee802154_local * local,struct sk_buff * skb)276 ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb)
277 {
278 struct sk_buff *skb2;
279 struct ieee802154_sub_if_data *sdata;
280
281 skb_reset_mac_header(skb);
282 skb->ip_summed = CHECKSUM_UNNECESSARY;
283 skb->pkt_type = PACKET_OTHERHOST;
284 skb->protocol = htons(ETH_P_IEEE802154);
285
286 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
287 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR)
288 continue;
289
290 if (!ieee802154_sdata_running(sdata))
291 continue;
292
293 skb2 = skb_clone(skb, GFP_ATOMIC);
294 if (skb2) {
295 skb2->dev = sdata->dev;
296 ieee802154_deliver_skb(skb2);
297
298 sdata->dev->stats.rx_packets++;
299 sdata->dev->stats.rx_bytes += skb->len;
300 }
301 }
302 }
303
ieee802154_rx(struct ieee802154_local * local,struct sk_buff * skb)304 void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb)
305 {
306 u16 crc;
307
308 WARN_ON_ONCE(softirq_count() == 0);
309
310 if (local->suspended)
311 goto free_skb;
312
313 /* TODO: When a transceiver omits the checksum here, we
314 * add an own calculated one. This is currently an ugly
315 * solution because the monitor needs a crc here.
316 */
317 if (local->hw.flags & IEEE802154_HW_RX_OMIT_CKSUM) {
318 crc = crc_ccitt(0, skb->data, skb->len);
319 put_unaligned_le16(crc, skb_put(skb, 2));
320 }
321
322 rcu_read_lock();
323
324 ieee802154_monitors_rx(local, skb);
325
326 /* Level 1 filtering: Check the FCS by software when relevant */
327 if (local->hw.phy->filtering == IEEE802154_FILTERING_NONE) {
328 crc = crc_ccitt(0, skb->data, skb->len);
329 if (crc)
330 goto drop;
331 }
332 /* remove crc */
333 skb_trim(skb, skb->len - 2);
334
335 __ieee802154_rx_handle_packet(local, skb);
336
337 drop:
338 rcu_read_unlock();
339 free_skb:
340 kfree_skb(skb);
341 }
342
343 void
ieee802154_rx_irqsafe(struct ieee802154_hw * hw,struct sk_buff * skb,u8 lqi)344 ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
345 {
346 struct ieee802154_local *local = hw_to_local(hw);
347 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
348
349 cb->lqi = lqi;
350 skb->pkt_type = IEEE802154_RX_MSG;
351 skb_queue_tail(&local->skb_queue, skb);
352 tasklet_schedule(&local->tasklet);
353 }
354 EXPORT_SYMBOL(ieee802154_rx_irqsafe);
355