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