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