alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 13 | * Written by: |
| 14 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> |
| 15 | * Sergey Lapin <slapin@ossfans.org> |
| 16 | * Maxim Gorbachyov <maxim.gorbachev@siemens.com> |
| 17 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
| 18 | */ |
| 19 | |
| 20 | #include <linux/netdevice.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/if_arp.h> |
| 23 | |
| 24 | #include <net/rtnetlink.h> |
| 25 | #include <linux/nl802154.h> |
| 26 | #include <net/af_ieee802154.h> |
| 27 | #include <net/mac802154.h> |
| 28 | #include <net/ieee802154_netdev.h> |
| 29 | #include <net/ieee802154.h> |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame^] | 30 | #include <net/cfg802154.h> |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 31 | |
Alexander Aring | 0f1556b | 2014-10-25 09:41:00 +0200 | [diff] [blame] | 32 | #include "ieee802154_i.h" |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 33 | |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 34 | static int mac802154_wpan_update_llsec(struct net_device *dev) |
| 35 | { |
| 36 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
| 37 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); |
| 38 | int rc = 0; |
| 39 | |
| 40 | if (ops->llsec) { |
| 41 | struct ieee802154_llsec_params params; |
| 42 | int changed = 0; |
| 43 | |
| 44 | params.pan_id = priv->pan_id; |
| 45 | changed |= IEEE802154_LLSEC_PARAM_PAN_ID; |
| 46 | |
| 47 | params.hwaddr = priv->extended_addr; |
| 48 | changed |= IEEE802154_LLSEC_PARAM_HWADDR; |
| 49 | |
| 50 | rc = ops->llsec->set_params(dev, ¶ms, changed); |
| 51 | } |
| 52 | |
| 53 | return rc; |
| 54 | } |
| 55 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 56 | static int |
| 57 | mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 58 | { |
| 59 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
| 60 | struct sockaddr_ieee802154 *sa = |
| 61 | (struct sockaddr_ieee802154 *)&ifr->ifr_addr; |
| 62 | int err = -ENOIOCTLCMD; |
| 63 | |
| 64 | spin_lock_bh(&priv->mib_lock); |
| 65 | |
| 66 | switch (cmd) { |
| 67 | case SIOCGIFADDR: |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 68 | { |
| 69 | u16 pan_id, short_addr; |
| 70 | |
| 71 | pan_id = le16_to_cpu(priv->pan_id); |
| 72 | short_addr = le16_to_cpu(priv->short_addr); |
| 73 | if (pan_id == IEEE802154_PANID_BROADCAST || |
| 74 | short_addr == IEEE802154_ADDR_BROADCAST) { |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 75 | err = -EADDRNOTAVAIL; |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | sa->family = AF_IEEE802154; |
| 80 | sa->addr.addr_type = IEEE802154_ADDR_SHORT; |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 81 | sa->addr.pan_id = pan_id; |
| 82 | sa->addr.short_addr = short_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 83 | |
| 84 | err = 0; |
| 85 | break; |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 86 | } |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 87 | case SIOCSIFADDR: |
| 88 | dev_warn(&dev->dev, |
Masanari Iida | 9b13494 | 2014-08-06 22:31:28 +0900 | [diff] [blame] | 89 | "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n"); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 90 | if (sa->family != AF_IEEE802154 || |
| 91 | sa->addr.addr_type != IEEE802154_ADDR_SHORT || |
| 92 | sa->addr.pan_id == IEEE802154_PANID_BROADCAST || |
| 93 | sa->addr.short_addr == IEEE802154_ADDR_BROADCAST || |
| 94 | sa->addr.short_addr == IEEE802154_ADDR_UNDEF) { |
| 95 | err = -EINVAL; |
| 96 | break; |
| 97 | } |
| 98 | |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 99 | priv->pan_id = cpu_to_le16(sa->addr.pan_id); |
| 100 | priv->short_addr = cpu_to_le16(sa->addr.short_addr); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 101 | |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 102 | err = mac802154_wpan_update_llsec(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | |
| 106 | spin_unlock_bh(&priv->mib_lock); |
| 107 | return err; |
| 108 | } |
| 109 | |
| 110 | static int mac802154_wpan_mac_addr(struct net_device *dev, void *p) |
| 111 | { |
| 112 | struct sockaddr *addr = p; |
| 113 | |
| 114 | if (netif_running(dev)) |
| 115 | return -EBUSY; |
| 116 | |
| 117 | /* FIXME: validate addr */ |
| 118 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
| 119 | mac802154_dev_set_ieee_addr(dev); |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 120 | return mac802154_wpan_update_llsec(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 123 | int mac802154_set_mac_params(struct net_device *dev, |
| 124 | const struct ieee802154_mac_params *params) |
| 125 | { |
| 126 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
| 127 | |
| 128 | mutex_lock(&priv->hw->slaves_mtx); |
| 129 | priv->mac_params = *params; |
| 130 | mutex_unlock(&priv->hw->slaves_mtx); |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | void mac802154_get_mac_params(struct net_device *dev, |
| 136 | struct ieee802154_mac_params *params) |
| 137 | { |
| 138 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
| 139 | |
| 140 | mutex_lock(&priv->hw->slaves_mtx); |
| 141 | *params = priv->mac_params; |
| 142 | mutex_unlock(&priv->hw->slaves_mtx); |
| 143 | } |
| 144 | |
Phoebe Buckheister | 6ef0023 | 2014-05-14 17:43:11 +0200 | [diff] [blame] | 145 | static int mac802154_wpan_open(struct net_device *dev) |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 146 | { |
| 147 | int rc; |
| 148 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
| 149 | struct wpan_phy *phy = priv->hw->phy; |
| 150 | |
| 151 | rc = mac802154_slave_open(dev); |
| 152 | if (rc < 0) |
| 153 | return rc; |
| 154 | |
| 155 | mutex_lock(&phy->pib_lock); |
| 156 | |
| 157 | if (phy->set_txpower) { |
| 158 | rc = phy->set_txpower(phy, priv->mac_params.transmit_power); |
| 159 | if (rc < 0) |
| 160 | goto out; |
| 161 | } |
| 162 | |
| 163 | if (phy->set_lbt) { |
| 164 | rc = phy->set_lbt(phy, priv->mac_params.lbt); |
| 165 | if (rc < 0) |
| 166 | goto out; |
| 167 | } |
| 168 | |
| 169 | if (phy->set_cca_mode) { |
| 170 | rc = phy->set_cca_mode(phy, priv->mac_params.cca_mode); |
| 171 | if (rc < 0) |
| 172 | goto out; |
| 173 | } |
| 174 | |
| 175 | if (phy->set_cca_ed_level) { |
| 176 | rc = phy->set_cca_ed_level(phy, priv->mac_params.cca_ed_level); |
| 177 | if (rc < 0) |
| 178 | goto out; |
| 179 | } |
| 180 | |
| 181 | if (phy->set_csma_params) { |
| 182 | rc = phy->set_csma_params(phy, priv->mac_params.min_be, |
| 183 | priv->mac_params.max_be, |
| 184 | priv->mac_params.csma_retries); |
| 185 | if (rc < 0) |
| 186 | goto out; |
| 187 | } |
| 188 | |
| 189 | if (phy->set_frame_retries) { |
| 190 | rc = phy->set_frame_retries(phy, |
| 191 | priv->mac_params.frame_retries); |
| 192 | if (rc < 0) |
| 193 | goto out; |
| 194 | } |
| 195 | |
| 196 | mutex_unlock(&phy->pib_lock); |
| 197 | return 0; |
| 198 | |
| 199 | out: |
| 200 | mutex_unlock(&phy->pib_lock); |
| 201 | return rc; |
| 202 | } |
| 203 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 204 | static int mac802154_set_header_security(struct mac802154_sub_if_data *priv, |
| 205 | struct ieee802154_hdr *hdr, |
| 206 | const struct ieee802154_mac_cb *cb) |
| 207 | { |
| 208 | struct ieee802154_llsec_params params; |
| 209 | u8 level; |
| 210 | |
| 211 | mac802154_llsec_get_params(&priv->sec, ¶ms); |
| 212 | |
| 213 | if (!params.enabled && cb->secen_override && cb->secen) |
| 214 | return -EINVAL; |
| 215 | if (!params.enabled || |
| 216 | (cb->secen_override && !cb->secen) || |
| 217 | !params.out_level) |
| 218 | return 0; |
| 219 | if (cb->seclevel_override && !cb->seclevel) |
| 220 | return -EINVAL; |
| 221 | |
| 222 | level = cb->seclevel_override ? cb->seclevel : params.out_level; |
| 223 | |
| 224 | hdr->fc.security_enabled = 1; |
| 225 | hdr->sec.level = level; |
| 226 | hdr->sec.key_id_mode = params.out_key.mode; |
| 227 | if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX) |
| 228 | hdr->sec.short_src = params.out_key.short_source; |
| 229 | else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX) |
| 230 | hdr->sec.extended_src = params.out_key.extended_source; |
| 231 | hdr->sec.key_id = params.out_key.id; |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 236 | static int mac802154_header_create(struct sk_buff *skb, |
| 237 | struct net_device *dev, |
| 238 | unsigned short type, |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 239 | const void *daddr, |
| 240 | const void *saddr, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 241 | unsigned len) |
| 242 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 243 | struct ieee802154_hdr hdr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 244 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 245 | struct ieee802154_mac_cb *cb = mac_cb(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 246 | int hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 247 | |
| 248 | if (!daddr) |
| 249 | return -EINVAL; |
| 250 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 251 | memset(&hdr.fc, 0, sizeof(hdr.fc)); |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 252 | hdr.fc.type = cb->type; |
| 253 | hdr.fc.security_enabled = cb->secen; |
| 254 | hdr.fc.ack_request = cb->ackreq; |
| 255 | hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 256 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 257 | if (mac802154_set_header_security(priv, &hdr, cb) < 0) |
| 258 | return -EINVAL; |
| 259 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 260 | if (!saddr) { |
| 261 | spin_lock_bh(&priv->mib_lock); |
| 262 | |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 263 | if (priv->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) || |
| 264 | priv->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) || |
| 265 | priv->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 266 | hdr.source.mode = IEEE802154_ADDR_LONG; |
| 267 | hdr.source.extended_addr = priv->extended_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 268 | } else { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 269 | hdr.source.mode = IEEE802154_ADDR_SHORT; |
| 270 | hdr.source.short_addr = priv->short_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 273 | hdr.source.pan_id = priv->pan_id; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 274 | |
| 275 | spin_unlock_bh(&priv->mib_lock); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 276 | } else { |
| 277 | hdr.source = *(const struct ieee802154_addr *)saddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 280 | hdr.dest = *(const struct ieee802154_addr *)daddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 281 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 282 | hlen = ieee802154_hdr_push(skb, &hdr); |
| 283 | if (hlen < 0) |
| 284 | return -EINVAL; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 285 | |
Alexander Aring | 3e69162 | 2013-10-30 09:18:22 +0100 | [diff] [blame] | 286 | skb_reset_mac_header(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 287 | skb->mac_len = hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 288 | |
Phoebe Buckheister | 8c84296 | 2014-05-14 17:43:07 +0200 | [diff] [blame] | 289 | if (len > ieee802154_max_payload(&hdr)) |
Phoebe Buckheister | d1d7358 | 2014-03-14 21:24:04 +0100 | [diff] [blame] | 290 | return -EMSGSIZE; |
| 291 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 292 | return hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static int |
| 296 | mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
| 297 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 298 | struct ieee802154_hdr hdr; |
| 299 | struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 300 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 301 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) { |
| 302 | pr_debug("malformed packet\n"); |
| 303 | return 0; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 306 | *addr = hdr.source; |
| 307 | return sizeof(*addr); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | static netdev_tx_t |
| 311 | mac802154_wpan_xmit(struct sk_buff *skb, struct net_device *dev) |
| 312 | { |
| 313 | struct mac802154_sub_if_data *priv; |
| 314 | u8 chan, page; |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 315 | int rc; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 316 | |
| 317 | priv = netdev_priv(dev); |
| 318 | |
| 319 | spin_lock_bh(&priv->mib_lock); |
| 320 | chan = priv->chan; |
| 321 | page = priv->page; |
| 322 | spin_unlock_bh(&priv->mib_lock); |
| 323 | |
| 324 | if (chan == MAC802154_CHAN_NONE || |
| 325 | page >= WPAN_NUM_PAGES || |
Alan Ott | fcefbe9 | 2012-11-29 18:25:10 +0000 | [diff] [blame] | 326 | chan >= WPAN_NUM_CHANNELS) { |
| 327 | kfree_skb(skb); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 328 | return NETDEV_TX_OK; |
Alan Ott | fcefbe9 | 2012-11-29 18:25:10 +0000 | [diff] [blame] | 329 | } |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 330 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 331 | rc = mac802154_llsec_encrypt(&priv->sec, skb); |
| 332 | if (rc) { |
| 333 | pr_warn("encryption failed: %i\n", rc); |
| 334 | kfree_skb(skb); |
| 335 | return NETDEV_TX_OK; |
| 336 | } |
| 337 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 338 | skb->skb_iif = dev->ifindex; |
| 339 | dev->stats.tx_packets++; |
| 340 | dev->stats.tx_bytes += skb->len; |
| 341 | |
| 342 | return mac802154_tx(priv->hw, skb, page, chan); |
| 343 | } |
| 344 | |
| 345 | static struct header_ops mac802154_header_ops = { |
| 346 | .create = mac802154_header_create, |
| 347 | .parse = mac802154_header_parse, |
| 348 | }; |
| 349 | |
| 350 | static const struct net_device_ops mac802154_wpan_ops = { |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 351 | .ndo_open = mac802154_wpan_open, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 352 | .ndo_stop = mac802154_slave_close, |
| 353 | .ndo_start_xmit = mac802154_wpan_xmit, |
| 354 | .ndo_do_ioctl = mac802154_wpan_ioctl, |
| 355 | .ndo_set_mac_address = mac802154_wpan_mac_addr, |
| 356 | }; |
| 357 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 358 | static void mac802154_wpan_free(struct net_device *dev) |
| 359 | { |
| 360 | struct mac802154_sub_if_data *priv = netdev_priv(dev); |
| 361 | |
| 362 | mac802154_llsec_destroy(&priv->sec); |
| 363 | |
| 364 | free_netdev(dev); |
| 365 | } |
| 366 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 367 | void mac802154_wpan_setup(struct net_device *dev) |
| 368 | { |
| 369 | struct mac802154_sub_if_data *priv; |
| 370 | |
| 371 | dev->addr_len = IEEE802154_ADDR_LEN; |
| 372 | memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); |
| 373 | |
| 374 | dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN; |
| 375 | dev->header_ops = &mac802154_header_ops; |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 376 | dev->needed_tailroom = 2 + 16; /* FCS + MIC */ |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 377 | dev->mtu = IEEE802154_MTU; |
Alan Ott | e937f583 | 2013-04-03 04:00:57 +0000 | [diff] [blame] | 378 | dev->tx_queue_len = 300; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 379 | dev->type = ARPHRD_IEEE802154; |
| 380 | dev->flags = IFF_NOARP | IFF_BROADCAST; |
| 381 | dev->watchdog_timeo = 0; |
| 382 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 383 | dev->destructor = mac802154_wpan_free; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 384 | dev->netdev_ops = &mac802154_wpan_ops; |
| 385 | dev->ml_priv = &mac802154_mlme_wpan; |
| 386 | |
| 387 | priv = netdev_priv(dev); |
| 388 | priv->type = IEEE802154_DEV_WPAN; |
| 389 | |
| 390 | priv->chan = MAC802154_CHAN_NONE; |
| 391 | priv->page = 0; |
| 392 | |
| 393 | spin_lock_init(&priv->mib_lock); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 394 | mutex_init(&priv->sec_mtx); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 395 | |
| 396 | get_random_bytes(&priv->bsn, 1); |
| 397 | get_random_bytes(&priv->dsn, 1); |
| 398 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 399 | /* defaults per 802.15.4-2011 */ |
| 400 | priv->mac_params.min_be = 3; |
| 401 | priv->mac_params.max_be = 5; |
| 402 | priv->mac_params.csma_retries = 4; |
| 403 | priv->mac_params.frame_retries = -1; /* for compatibility, actual default is 3 */ |
| 404 | |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 405 | priv->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); |
| 406 | priv->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 407 | |
| 408 | mac802154_llsec_init(&priv->sec); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static int mac802154_process_data(struct net_device *dev, struct sk_buff *skb) |
| 412 | { |
Alexander Aring | 5ff3fec | 2013-01-02 01:01:10 +0000 | [diff] [blame] | 413 | return netif_rx_ni(skb); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static int |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 417 | mac802154_subif_frame(struct mac802154_sub_if_data *sdata, struct sk_buff *skb, |
| 418 | const struct ieee802154_hdr *hdr) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 419 | { |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 420 | __le16 span, sshort; |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 421 | int rc; |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 422 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 423 | pr_debug("getting packet via slave interface %s\n", sdata->dev->name); |
| 424 | |
| 425 | spin_lock_bh(&sdata->mib_lock); |
| 426 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 427 | span = sdata->pan_id; |
| 428 | sshort = sdata->short_addr; |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 429 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 430 | switch (mac_cb(skb)->dest.mode) { |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 431 | case IEEE802154_ADDR_NONE: |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 432 | if (mac_cb(skb)->dest.mode != IEEE802154_ADDR_NONE) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 433 | /* FIXME: check if we are PAN coordinator */ |
| 434 | skb->pkt_type = PACKET_OTHERHOST; |
| 435 | else |
| 436 | /* ACK comes with both addresses empty */ |
| 437 | skb->pkt_type = PACKET_HOST; |
| 438 | break; |
| 439 | case IEEE802154_ADDR_LONG: |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 440 | if (mac_cb(skb)->dest.pan_id != span && |
| 441 | mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST)) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 442 | skb->pkt_type = PACKET_OTHERHOST; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 443 | else if (mac_cb(skb)->dest.extended_addr == sdata->extended_addr) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 444 | skb->pkt_type = PACKET_HOST; |
| 445 | else |
| 446 | skb->pkt_type = PACKET_OTHERHOST; |
| 447 | break; |
| 448 | case IEEE802154_ADDR_SHORT: |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 449 | if (mac_cb(skb)->dest.pan_id != span && |
| 450 | mac_cb(skb)->dest.pan_id != cpu_to_le16(IEEE802154_PANID_BROADCAST)) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 451 | skb->pkt_type = PACKET_OTHERHOST; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 452 | else if (mac_cb(skb)->dest.short_addr == sshort) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 453 | skb->pkt_type = PACKET_HOST; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 454 | else if (mac_cb(skb)->dest.short_addr == |
| 455 | cpu_to_le16(IEEE802154_ADDR_BROADCAST)) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 456 | skb->pkt_type = PACKET_BROADCAST; |
| 457 | else |
| 458 | skb->pkt_type = PACKET_OTHERHOST; |
| 459 | break; |
| 460 | default: |
Martin Townsend | 6e361d6 | 2014-08-19 19:03:30 +0200 | [diff] [blame] | 461 | spin_unlock_bh(&sdata->mib_lock); |
| 462 | pr_debug("invalid dest mode\n"); |
| 463 | kfree_skb(skb); |
| 464 | return NET_RX_DROP; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | spin_unlock_bh(&sdata->mib_lock); |
| 468 | |
| 469 | skb->dev = sdata->dev; |
| 470 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 471 | rc = mac802154_llsec_decrypt(&sdata->sec, skb); |
| 472 | if (rc) { |
| 473 | pr_debug("decryption failed: %i\n", rc); |
Varka Bhadram | b288a49 | 2014-08-11 13:25:08 +0200 | [diff] [blame] | 474 | goto fail; |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 475 | } |
| 476 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 477 | sdata->dev->stats.rx_packets++; |
| 478 | sdata->dev->stats.rx_bytes += skb->len; |
| 479 | |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 480 | switch (mac_cb(skb)->type) { |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 481 | case IEEE802154_FC_TYPE_DATA: |
| 482 | return mac802154_process_data(sdata->dev, skb); |
| 483 | default: |
Weilong Chen | 2cc33c7 | 2013-12-19 09:22:26 +0800 | [diff] [blame] | 484 | pr_warn("ieee802154: bad frame received (type = %d)\n", |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 485 | mac_cb(skb)->type); |
Varka Bhadram | b288a49 | 2014-08-11 13:25:08 +0200 | [diff] [blame] | 486 | goto fail; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 487 | } |
Varka Bhadram | b288a49 | 2014-08-11 13:25:08 +0200 | [diff] [blame] | 488 | |
| 489 | fail: |
| 490 | kfree_skb(skb); |
| 491 | return NET_RX_DROP; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 494 | static void mac802154_print_addr(const char *name, |
| 495 | const struct ieee802154_addr *addr) |
| 496 | { |
| 497 | if (addr->mode == IEEE802154_ADDR_NONE) |
| 498 | pr_debug("%s not present\n", name); |
| 499 | |
| 500 | pr_debug("%s PAN ID: %04x\n", name, le16_to_cpu(addr->pan_id)); |
| 501 | if (addr->mode == IEEE802154_ADDR_SHORT) { |
| 502 | pr_debug("%s is short: %04x\n", name, |
| 503 | le16_to_cpu(addr->short_addr)); |
| 504 | } else { |
| 505 | u64 hw = swab64((__force u64) addr->extended_addr); |
| 506 | |
| 507 | pr_debug("%s is hardware: %8phC\n", name, &hw); |
| 508 | } |
| 509 | } |
| 510 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 511 | static int mac802154_parse_frame_start(struct sk_buff *skb, |
| 512 | struct ieee802154_hdr *hdr) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 513 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 514 | int hlen; |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 515 | struct ieee802154_mac_cb *cb = mac_cb_init(skb); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 516 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 517 | hlen = ieee802154_hdr_pull(skb, hdr); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 518 | if (hlen < 0) |
| 519 | return -EINVAL; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 520 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 521 | skb->mac_len = hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 522 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 523 | pr_debug("fc: %04x dsn: %02x\n", le16_to_cpup((__le16 *)&hdr->fc), |
| 524 | hdr->seq); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 525 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 526 | cb->type = hdr->fc.type; |
| 527 | cb->ackreq = hdr->fc.ack_request; |
| 528 | cb->secen = hdr->fc.security_enabled; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 529 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 530 | mac802154_print_addr("destination", &hdr->dest); |
| 531 | mac802154_print_addr("source", &hdr->source); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 532 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 533 | cb->source = hdr->source; |
| 534 | cb->dest = hdr->dest; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 535 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 536 | if (hdr->fc.security_enabled) { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 537 | u64 key; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 538 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 539 | pr_debug("seclevel %i\n", hdr->sec.level); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 540 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 541 | switch (hdr->sec.key_id_mode) { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 542 | case IEEE802154_SCF_KEY_IMPLICIT: |
| 543 | pr_debug("implicit key\n"); |
| 544 | break; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 545 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 546 | case IEEE802154_SCF_KEY_INDEX: |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 547 | pr_debug("key %02x\n", hdr->sec.key_id); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 548 | break; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 549 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 550 | case IEEE802154_SCF_KEY_SHORT_INDEX: |
| 551 | pr_debug("key %04x:%04x %02x\n", |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 552 | le32_to_cpu(hdr->sec.short_src) >> 16, |
| 553 | le32_to_cpu(hdr->sec.short_src) & 0xffff, |
| 554 | hdr->sec.key_id); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 555 | break; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 556 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 557 | case IEEE802154_SCF_KEY_HW_INDEX: |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 558 | key = swab64((__force u64) hdr->sec.extended_src); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 559 | pr_debug("key source %8phC %02x\n", &key, |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 560 | hdr->sec.key_id); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 561 | break; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 562 | } |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | return 0; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void mac802154_wpans_rx(struct mac802154_priv *priv, struct sk_buff *skb) |
| 569 | { |
| 570 | int ret; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 571 | struct mac802154_sub_if_data *sdata; |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 572 | struct ieee802154_hdr hdr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 573 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 574 | ret = mac802154_parse_frame_start(skb, &hdr); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 575 | if (ret) { |
| 576 | pr_debug("got invalid frame\n"); |
Martin Townsend | 7629d1e | 2014-08-19 19:03:28 +0200 | [diff] [blame] | 577 | kfree_skb(skb); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 578 | return; |
| 579 | } |
| 580 | |
| 581 | rcu_read_lock(); |
| 582 | list_for_each_entry_rcu(sdata, &priv->slaves, list) { |
Phoebe Buckheister | 2d3b5b0 | 2014-06-11 12:03:07 +0200 | [diff] [blame] | 583 | if (sdata->type != IEEE802154_DEV_WPAN || |
| 584 | !netif_running(sdata->dev)) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 585 | continue; |
| 586 | |
Phoebe Buckheister | 2d3b5b0 | 2014-06-11 12:03:07 +0200 | [diff] [blame] | 587 | mac802154_subif_frame(sdata, skb, &hdr); |
| 588 | skb = NULL; |
| 589 | break; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 590 | } |
| 591 | rcu_read_unlock(); |
Phoebe Buckheister | 2d3b5b0 | 2014-06-11 12:03:07 +0200 | [diff] [blame] | 592 | |
| 593 | if (skb) |
| 594 | kfree_skb(skb); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 595 | } |