alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 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 | * |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 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> |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 22 | #include <linux/netdevice.h> |
| 23 | #include <linux/crc-ccitt.h> |
Alexander Aring | b788949 | 2014-10-29 21:34:36 +0100 | [diff] [blame] | 24 | #include <asm/unaligned.h> |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 25 | |
| 26 | #include <net/mac802154.h> |
| 27 | #include <net/ieee802154_netdev.h> |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 28 | #include <net/rtnetlink.h> |
| 29 | #include <linux/nl802154.h> |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 30 | |
Alexander Aring | 0f1556b | 2014-10-25 09:41:00 +0200 | [diff] [blame] | 31 | #include "ieee802154_i.h" |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 32 | |
Alexander Aring | 08c511a | 2014-10-29 21:34:35 +0100 | [diff] [blame] | 33 | static int ieee802154_deliver_skb(struct sk_buff *skb) |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 34 | { |
Alexander Aring | 75a46f0 | 2014-10-27 17:13:36 +0100 | [diff] [blame] | 35 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Alexander Aring | 702dcf9 | 2014-10-27 17:13:35 +0100 | [diff] [blame] | 36 | skb->protocol = htons(ETH_P_IEEE802154); |
| 37 | |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 38 | return netif_receive_skb(skb); |
| 39 | } |
| 40 | |
| 41 | static int |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 42 | ieee802154_subif_frame(struct ieee802154_sub_if_data *sdata, |
| 43 | struct sk_buff *skb, const struct ieee802154_hdr *hdr) |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 44 | { |
| 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 | |
| 52 | span = sdata->pan_id; |
| 53 | sshort = sdata->short_addr; |
| 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; |
| 68 | else if (mac_cb(skb)->dest.extended_addr == sdata->extended_addr) |
| 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"); |
| 88 | kfree_skb(skb); |
| 89 | return NET_RX_DROP; |
| 90 | } |
| 91 | |
| 92 | spin_unlock_bh(&sdata->mib_lock); |
| 93 | |
| 94 | skb->dev = sdata->dev; |
| 95 | |
| 96 | rc = mac802154_llsec_decrypt(&sdata->sec, skb); |
| 97 | if (rc) { |
| 98 | pr_debug("decryption failed: %i\n", rc); |
| 99 | goto fail; |
| 100 | } |
| 101 | |
| 102 | sdata->dev->stats.rx_packets++; |
| 103 | sdata->dev->stats.rx_bytes += skb->len; |
| 104 | |
| 105 | switch (mac_cb(skb)->type) { |
| 106 | case IEEE802154_FC_TYPE_DATA: |
Alexander Aring | 08c511a | 2014-10-29 21:34:35 +0100 | [diff] [blame] | 107 | return ieee802154_deliver_skb(skb); |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 108 | default: |
| 109 | pr_warn("ieee802154: bad frame received (type = %d)\n", |
| 110 | mac_cb(skb)->type); |
| 111 | goto fail; |
| 112 | } |
| 113 | |
| 114 | fail: |
| 115 | kfree_skb(skb); |
| 116 | return NET_RX_DROP; |
| 117 | } |
| 118 | |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 119 | static void |
| 120 | ieee802154_print_addr(const char *name, const struct ieee802154_addr *addr) |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 121 | { |
| 122 | if (addr->mode == IEEE802154_ADDR_NONE) |
| 123 | pr_debug("%s not present\n", name); |
| 124 | |
| 125 | pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id)); |
| 126 | if (addr->mode == IEEE802154_ADDR_SHORT) { |
| 127 | pr_debug("%s is short: %04x\n", name, |
| 128 | le16_to_cpu(addr->short_addr)); |
| 129 | } else { |
| 130 | u64 hw = swab64((__force u64)addr->extended_addr); |
| 131 | |
| 132 | pr_debug("%s is hardware: %8phC\n", name, &hw); |
| 133 | } |
| 134 | } |
| 135 | |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 136 | static int |
| 137 | ieee802154_parse_frame_start(struct sk_buff *skb, struct ieee802154_hdr *hdr) |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 138 | { |
| 139 | int hlen; |
| 140 | struct ieee802154_mac_cb *cb = mac_cb_init(skb); |
| 141 | |
Alexander Aring | 9cf215d | 2014-10-27 17:13:38 +0100 | [diff] [blame] | 142 | skb_reset_mac_header(skb); |
| 143 | |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 144 | hlen = ieee802154_hdr_pull(skb, hdr); |
| 145 | if (hlen < 0) |
| 146 | return -EINVAL; |
| 147 | |
| 148 | skb->mac_len = hlen; |
| 149 | |
| 150 | pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc), |
| 151 | hdr->seq); |
| 152 | |
| 153 | cb->type = hdr->fc.type; |
| 154 | cb->ackreq = hdr->fc.ack_request; |
| 155 | cb->secen = hdr->fc.security_enabled; |
| 156 | |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 157 | ieee802154_print_addr("destination", &hdr->dest); |
| 158 | ieee802154_print_addr("source", &hdr->source); |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 159 | |
| 160 | cb->source = hdr->source; |
| 161 | cb->dest = hdr->dest; |
| 162 | |
| 163 | if (hdr->fc.security_enabled) { |
| 164 | u64 key; |
| 165 | |
| 166 | pr_debug("seclevel %i\n", hdr->sec.level); |
| 167 | |
| 168 | switch (hdr->sec.key_id_mode) { |
| 169 | case IEEE802154_SCF_KEY_IMPLICIT: |
| 170 | pr_debug("implicit key\n"); |
| 171 | break; |
| 172 | |
| 173 | case IEEE802154_SCF_KEY_INDEX: |
| 174 | pr_debug("key %02x\n", hdr->sec.key_id); |
| 175 | break; |
| 176 | |
| 177 | case IEEE802154_SCF_KEY_SHORT_INDEX: |
| 178 | pr_debug("key %04x:%04x %02x\n", |
| 179 | le32_to_cpu(hdr->sec.short_src) >> 16, |
| 180 | le32_to_cpu(hdr->sec.short_src) & 0xffff, |
| 181 | hdr->sec.key_id); |
| 182 | break; |
| 183 | |
| 184 | case IEEE802154_SCF_KEY_HW_INDEX: |
| 185 | key = swab64((__force u64)hdr->sec.extended_src); |
| 186 | pr_debug("key source %8phC %02x\n", &key, |
| 187 | hdr->sec.key_id); |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | static void |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 196 | __ieee802154_rx_handle_packet(struct ieee802154_local *local, |
| 197 | struct sk_buff *skb) |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 198 | { |
| 199 | int ret; |
| 200 | struct ieee802154_sub_if_data *sdata; |
| 201 | struct ieee802154_hdr hdr; |
| 202 | |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 203 | ret = ieee802154_parse_frame_start(skb, &hdr); |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 204 | if (ret) { |
| 205 | pr_debug("got invalid frame\n"); |
| 206 | kfree_skb(skb); |
| 207 | return; |
| 208 | } |
| 209 | |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 210 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
| 211 | if (sdata->type != IEEE802154_DEV_WPAN || |
| 212 | !netif_running(sdata->dev)) |
| 213 | continue; |
| 214 | |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 215 | ieee802154_subif_frame(sdata, skb, &hdr); |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 216 | skb = NULL; |
| 217 | break; |
| 218 | } |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 219 | |
| 220 | if (skb) |
| 221 | kfree_skb(skb); |
| 222 | } |
| 223 | |
Alexander Aring | 4ca18be | 2014-10-27 17:13:33 +0100 | [diff] [blame] | 224 | static void |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 225 | ieee802154_monitors_rx(struct ieee802154_local *local, struct sk_buff *skb) |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 226 | { |
| 227 | struct sk_buff *skb2; |
| 228 | struct ieee802154_sub_if_data *sdata; |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 229 | |
Alexander Aring | 9cf215d | 2014-10-27 17:13:38 +0100 | [diff] [blame] | 230 | skb_reset_mac_header(skb); |
Alexander Aring | 75a46f0 | 2014-10-27 17:13:36 +0100 | [diff] [blame] | 231 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
Alexander Aring | c9ca640 | 2014-10-27 17:13:37 +0100 | [diff] [blame] | 232 | skb->pkt_type = PACKET_OTHERHOST; |
Alexander Aring | 702dcf9 | 2014-10-27 17:13:35 +0100 | [diff] [blame] | 233 | skb->protocol = htons(ETH_P_IEEE802154); |
| 234 | |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 235 | list_for_each_entry_rcu(sdata, &local->interfaces, list) { |
Alexander Aring | 20b4812 | 2014-10-29 21:34:41 +0100 | [diff] [blame] | 236 | if (sdata->type != IEEE802154_DEV_MONITOR) |
| 237 | continue; |
| 238 | |
| 239 | if (!ieee802154_sdata_running(sdata)) |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 240 | continue; |
| 241 | |
| 242 | skb2 = skb_clone(skb, GFP_ATOMIC); |
Alexander Aring | 05f7de6 | 2014-10-29 21:34:42 +0100 | [diff] [blame^] | 243 | if (skb2) { |
| 244 | skb2->dev = sdata->dev; |
| 245 | ieee802154_deliver_skb(skb2); |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 246 | |
Alexander Aring | 05f7de6 | 2014-10-29 21:34:42 +0100 | [diff] [blame^] | 247 | sdata->dev->stats.rx_packets++; |
| 248 | sdata->dev->stats.rx_bytes += skb->len; |
| 249 | } |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 250 | } |
Alexander Aring | 2a9820c | 2014-10-27 17:13:32 +0100 | [diff] [blame] | 251 | } |
| 252 | |
Alexander Aring | 469100d | 2014-10-27 17:13:34 +0100 | [diff] [blame] | 253 | void ieee802154_rx(struct ieee802154_hw *hw, struct sk_buff *skb) |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 254 | { |
Alexander Aring | 6074136 | 2014-10-25 17:16:39 +0200 | [diff] [blame] | 255 | struct ieee802154_local *local = hw_to_local(hw); |
Alexander Aring | ec718f3 | 2014-10-29 21:34:37 +0100 | [diff] [blame] | 256 | u16 crc; |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 257 | |
Alexander Aring | 469100d | 2014-10-27 17:13:34 +0100 | [diff] [blame] | 258 | WARN_ON_ONCE(softirq_count() == 0); |
| 259 | |
Alexander Aring | b788949 | 2014-10-29 21:34:36 +0100 | [diff] [blame] | 260 | /* TODO: When a transceiver omits the checksum here, we |
| 261 | * add an own calculated one. This is currently an ugly |
| 262 | * solution because the monitor needs a crc here. |
| 263 | */ |
| 264 | if (local->hw.flags & IEEE802154_HW_RX_OMIT_CKSUM) { |
Alexander Aring | ec718f3 | 2014-10-29 21:34:37 +0100 | [diff] [blame] | 265 | crc = crc_ccitt(0, skb->data, skb->len); |
Alexander Aring | b788949 | 2014-10-29 21:34:36 +0100 | [diff] [blame] | 266 | put_unaligned_le16(crc, skb_put(skb, 2)); |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Alexander Aring | e176b68 | 2014-10-27 17:13:39 +0100 | [diff] [blame] | 269 | rcu_read_lock(); |
| 270 | |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 271 | ieee802154_monitors_rx(local, skb); |
Alexander Aring | b788949 | 2014-10-29 21:34:36 +0100 | [diff] [blame] | 272 | |
Alexander Aring | ec718f3 | 2014-10-29 21:34:37 +0100 | [diff] [blame] | 273 | /* Check if transceiver doesn't validate the checksum. |
| 274 | * If not we validate the checksum here. |
| 275 | */ |
| 276 | if (local->hw.flags & IEEE802154_HW_RX_DROP_BAD_CKSUM) { |
| 277 | crc = crc_ccitt(0, skb->data, skb->len); |
| 278 | if (crc) { |
| 279 | rcu_read_unlock(); |
| 280 | kfree_skb(skb); |
| 281 | return; |
| 282 | } |
| 283 | } |
Alexander Aring | b788949 | 2014-10-29 21:34:36 +0100 | [diff] [blame] | 284 | /* remove crc */ |
| 285 | skb_trim(skb, skb->len - 2); |
| 286 | |
Alexander Aring | be9d215 | 2014-10-27 17:13:43 +0100 | [diff] [blame] | 287 | __ieee802154_rx_handle_packet(local, skb); |
Phoebe Buckheister | 2d3b5b0 | 2014-06-11 12:03:07 +0200 | [diff] [blame] | 288 | |
Alexander Aring | e176b68 | 2014-10-27 17:13:39 +0100 | [diff] [blame] | 289 | rcu_read_unlock(); |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 290 | } |
Alexander Aring | c5c47e6 | 2014-10-27 17:13:30 +0100 | [diff] [blame] | 291 | EXPORT_SYMBOL(ieee802154_rx); |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 292 | |
| 293 | void |
Alexander Aring | 5a50439 | 2014-10-25 17:16:34 +0200 | [diff] [blame] | 294 | ieee802154_rx_irqsafe(struct ieee802154_hw *hw, struct sk_buff *skb, u8 lqi) |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 295 | { |
Alexander Aring | 6074136 | 2014-10-25 17:16:39 +0200 | [diff] [blame] | 296 | struct ieee802154_local *local = hw_to_local(hw); |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 297 | |
Alexander Aring | c5c47e6 | 2014-10-27 17:13:30 +0100 | [diff] [blame] | 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); |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 302 | } |
| 303 | EXPORT_SYMBOL(ieee802154_rx_irqsafe); |