1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2007-2012 Siemens AG
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>
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>
18 #include <net/mac802154.h>
19 #include <net/ieee802154_netdev.h>
20 #include <net/nl802154.h>
22 #include "ieee802154_i.h"
24 static int ieee802154_deliver_skb(struct sk_buff *skb)
26 skb->ip_summed = CHECKSUM_UNNECESSARY;
27 skb->protocol = htons(ETH_P_IEEE802154);
29 return netif_receive_skb(skb);
32 void mac802154_rx_beacon_worker(struct work_struct *work)
34 struct ieee802154_local *local =
35 container_of(work, struct ieee802154_local, rx_beacon_work);
36 struct cfg802154_mac_pkt *mac_pkt;
38 mac_pkt = list_first_entry_or_null(&local->rx_beacon_list,
39 struct cfg802154_mac_pkt, node);
43 mac802154_process_beacon(local, mac_pkt->skb, mac_pkt->page, mac_pkt->channel);
45 list_del(&mac_pkt->node);
46 kfree_skb(mac_pkt->skb);
50 static bool mac802154_should_answer_beacon_req(struct ieee802154_local *local)
52 struct cfg802154_beacon_request *beacon_req;
53 unsigned int interval;
56 beacon_req = rcu_dereference(local->beacon_req);
62 interval = beacon_req->interval;
65 if (!mac802154_is_beaconing(local))
68 return interval == IEEE802154_ACTIVE_SCAN_DURATION;
71 void mac802154_rx_mac_cmd_worker(struct work_struct *work)
73 struct ieee802154_local *local =
74 container_of(work, struct ieee802154_local, rx_mac_cmd_work);
75 struct cfg802154_mac_pkt *mac_pkt;
79 mac_pkt = list_first_entry_or_null(&local->rx_mac_cmd_list,
80 struct cfg802154_mac_pkt, node);
84 rc = ieee802154_get_mac_cmd(mac_pkt->skb, &mac_cmd);
89 case IEEE802154_CMD_BEACON_REQ:
90 dev_dbg(&mac_pkt->sdata->dev->dev, "processing BEACON REQ\n");
91 if (!mac802154_should_answer_beacon_req(local))
94 queue_delayed_work(local->mac_wq, &local->beacon_work, 0);
101 list_del(&mac_pkt->node);
102 kfree_skb(mac_pkt->skb);
107 ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata,
108 struct sk_buff *skb, const struct ieee802154_hdr *hdr)
110 struct wpan_phy *wpan_phy = sdata->local->hw.phy;
111 struct wpan_dev *wpan_dev = &sdata->wpan_dev;
112 struct cfg802154_mac_pkt *mac_pkt;
116 pr_debug("getting packet via slave interface %s\n", sdata->dev->name);
118 span = wpan_dev->pan_id;
119 sshort = wpan_dev->short_addr;
121 /* Level 3 filtering: Only beacons are accepted during scans */
122 if (sdata->required_filtering == IEEE802154_FILTERING_3_SCAN &&
123 sdata->required_filtering > wpan_phy->filtering) {
124 if (mac_cb(skb)->type != IEEE802154_FC_TYPE_BEACON) {
125 dev_dbg(&sdata->dev->dev,
126 "drop non-beacon frame (0x%x) during scan\n",
132 switch (mac_cb(skb)->dest.mode) {
133 case IEEE802154_ADDR_NONE:
134 if (hdr->source.mode != IEEE802154_ADDR_NONE)
135 /* FIXME: check if we are PAN coordinator */
136 skb->pkt_type = PACKET_OTHERHOST;
138 /* ACK comes with both addresses empty */
139 skb->pkt_type = PACKET_HOST;
141 case IEEE802154_ADDR_LONG:
142 if (mac_cb(skb)->dest.pan_id != span &&
143 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
144 skb->pkt_type = PACKET_OTHERHOST;
145 else if (mac_cb(skb)->dest.extended_addr == wpan_dev->extended_addr)
146 skb->pkt_type = PACKET_HOST;
148 skb->pkt_type = PACKET_OTHERHOST;
150 case IEEE802154_ADDR_SHORT:
151 if (mac_cb(skb)->dest.pan_id != span &&
152 mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST))
153 skb->pkt_type = PACKET_OTHERHOST;
154 else if (mac_cb(skb)->dest.short_addr == sshort)
155 skb->pkt_type = PACKET_HOST;
156 else if (mac_cb(skb)->dest.short_addr ==
157 cpu_to_le16(IEEE802154_ADDR_BROADCAST))
158 skb->pkt_type = PACKET_BROADCAST;
160 skb->pkt_type = PACKET_OTHERHOST;
163 pr_debug("invalid dest mode\n");
167 skb->dev = sdata->dev;
169 /* TODO this should be moved after netif_receive_skb call, otherwise
170 * wireshark will show a mac header with security fields and the
171 * payload is already decrypted.
173 rc = mac802154_llsec_decrypt(&sdata->sec, skb);
175 pr_debug("decryption failed: %i\n", rc);
179 sdata->dev->stats.rx_packets++;
180 sdata->dev->stats.rx_bytes += skb->len;
182 switch (mac_cb(skb)->type) {
183 case IEEE802154_FC_TYPE_BEACON:
184 dev_dbg(&sdata->dev->dev, "BEACON received\n");
185 if (!mac802154_is_scanning(sdata->local))
188 mac_pkt = kzalloc(sizeof(*mac_pkt), GFP_ATOMIC);
192 mac_pkt->skb = skb_get(skb);
193 mac_pkt->sdata = sdata;
194 mac_pkt->page = sdata->local->scan_page;
195 mac_pkt->channel = sdata->local->scan_channel;
196 list_add_tail(&mac_pkt->node, &sdata->local->rx_beacon_list);
197 queue_work(sdata->local->mac_wq, &sdata->local->rx_beacon_work);
198 return NET_RX_SUCCESS;
200 case IEEE802154_FC_TYPE_MAC_CMD:
201 dev_dbg(&sdata->dev->dev, "MAC COMMAND received\n");
202 mac_pkt = kzalloc(sizeof(*mac_pkt), GFP_ATOMIC);
206 mac_pkt->skb = skb_get(skb);
207 mac_pkt->sdata = sdata;
208 list_add_tail(&mac_pkt->node, &sdata->local->rx_mac_cmd_list);
209 queue_work(sdata->local->mac_wq, &sdata->local->rx_mac_cmd_work);
210 return NET_RX_SUCCESS;
212 case IEEE802154_FC_TYPE_ACK:
215 case IEEE802154_FC_TYPE_DATA:
216 return ieee802154_deliver_skb(skb);
218 pr_warn_ratelimited("ieee802154: bad frame received "
219 "(type = %d)\n", mac_cb(skb)->type);
229 ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr)
231 if (addr->mode == IEEE802154_ADDR_NONE) {
232 pr_debug("%s not present\n", name);
236 pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id));
237 if (addr->mode == IEEE802154_ADDR_SHORT) {
238 pr_debug("%s is short: %04x\n", name,
239 le16_to_cpu(addr->short_addr));
241 u64 hw = swab64((__force u64)addr->extended_addr);
243 pr_debug("%s is hardware: %8phC\n", name, &hw);
248 ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr)
251 struct ieee802154_mac_cb *cb = mac_cb(skb);
253 skb_reset_mac_header(skb);
255 hlen = ieee802154_hdr_pull(skb, hdr);
261 pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc),
264 cb->type = hdr->fc.type;
265 cb->ackreq = hdr->fc.ack_request;
266 cb->secen = hdr->fc.security_enabled;
268 ieee802154_print_addr("destination", &hdr->dest);
269 ieee802154_print_addr("source", &hdr->source);
271 cb->source = hdr->source;
272 cb->dest = hdr->dest;
274 if (hdr->fc.security_enabled) {
277 pr_debug("seclevel %i\n", hdr->sec.level);
279 switch (hdr->sec.key_id_mode) {
280 case IEEE802154_SCF_KEY_IMPLICIT:
281 pr_debug("implicit key\n");
284 case IEEE802154_SCF_KEY_INDEX:
285 pr_debug("key %02x\n", hdr->sec.key_id);
288 case IEEE802154_SCF_KEY_SHORT_INDEX:
289 pr_debug("key %04x:%04x %02x\n",
290 le32_to_cpu(hdr->sec.short_src) >> 16,
291 le32_to_cpu(hdr->sec.short_src) & 0xffff,
295 case IEEE802154_SCF_KEY_HW_INDEX:
296 key = swab64((__force u64)hdr->sec.extended_src);
297 pr_debug("key source %8phC %02x\n", &key,
307 __ieee802154_rx_handle_packet(struct ieee802154_local *local,
311 struct ieee802154_sub_if_data *sdata;
312 struct ieee802154_hdr hdr;
313 struct sk_buff *skb2;
315 ret = ieee802154_parse_frame_start(skb, &hdr);
317 pr_debug("got invalid frame\n");
321 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
322 if (sdata->wpan_dev.iftype == NL802154_IFTYPE_MONITOR)
325 if (!ieee802154_sdata_running(sdata))
328 /* Do not deliver packets received on interfaces expecting
329 * AACK=1 if the address filters where disabled.
331 if (local->hw.phy->filtering < IEEE802154_FILTERING_4_FRAME_FIELDS &&
332 sdata->required_filtering == IEEE802154_FILTERING_4_FRAME_FIELDS)
335 skb2 = skb_clone(skb, GFP_ATOMIC);
337 skb2->dev = sdata->dev;
338 ieee802154_subif_frame(sdata, skb2, &hdr);
344 ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb)
346 struct sk_buff *skb2;
347 struct ieee802154_sub_if_data *sdata;
349 skb_reset_mac_header(skb);
350 skb->ip_summed = CHECKSUM_UNNECESSARY;
351 skb->pkt_type = PACKET_OTHERHOST;
352 skb->protocol = htons(ETH_P_IEEE802154);
354 list_for_each_entry_rcu(sdata, &local->interfaces, list) {
355 if (sdata->wpan_dev.iftype != NL802154_IFTYPE_MONITOR)
358 if (!ieee802154_sdata_running(sdata))
361 skb2 = skb_clone(skb, GFP_ATOMIC);
363 skb2->dev = sdata->dev;
364 ieee802154_deliver_skb(skb2);
366 sdata->dev->stats.rx_packets++;
367 sdata->dev->stats.rx_bytes += skb->len;
372 void ieee802154_rx(struct ieee802154_local *local, struct sk_buff *skb)
376 WARN_ON_ONCE(softirq_count() == 0);
378 if (local->suspended)
381 /* TODO: When a transceiver omits the checksum here, we
382 * add an own calculated one. This is currently an ugly
383 * solution because the monitor needs a crc here.
385 if (local->hw.flags & IEEE802154_HW_RX_OMIT_CKSUM) {
386 crc = crc_ccitt(0, skb->data, skb->len);
387 put_unaligned_le16(crc, skb_put(skb, 2));
392 ieee802154_monitors_rx(local, skb);
394 /* Level 1 filtering: Check the FCS by software when relevant */
395 if (local->hw.phy->filtering == IEEE802154_FILTERING_NONE) {
396 crc = crc_ccitt(0, skb->data, skb->len);
401 skb_trim(skb, skb->len - 2);
403 __ieee802154_rx_handle_packet(local, skb);
412 ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi)
414 struct ieee802154_local *local = hw_to_local(hw);
415 struct ieee802154_mac_cb *cb = mac_cb_init(skb);
418 skb->pkt_type = IEEE802154_RX_MSG;
419 skb_queue_tail(&local->skb_queue, skb);
420 tasklet_schedule(&local->tasklet);
422 EXPORT_SYMBOL(ieee802154_rx_irqsafe);