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> |
Alexander Aring | 4ca24ac | 2014-10-25 09:41:04 +0200 | [diff] [blame] | 23 | #include <linux/ieee802154.h> |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 24 | |
| 25 | #include <net/rtnetlink.h> |
| 26 | #include <linux/nl802154.h> |
| 27 | #include <net/af_ieee802154.h> |
| 28 | #include <net/mac802154.h> |
| 29 | #include <net/ieee802154_netdev.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" |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 33 | #include "driver-ops.h" |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 34 | |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 35 | static int mac802154_wpan_update_llsec(struct net_device *dev) |
| 36 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 37 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 38 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); |
| 39 | int rc = 0; |
| 40 | |
| 41 | if (ops->llsec) { |
| 42 | struct ieee802154_llsec_params params; |
| 43 | int changed = 0; |
| 44 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 45 | params.pan_id = sdata->pan_id; |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 46 | changed |= IEEE802154_LLSEC_PARAM_PAN_ID; |
| 47 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 48 | params.hwaddr = sdata->extended_addr; |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 49 | changed |= IEEE802154_LLSEC_PARAM_HWADDR; |
| 50 | |
| 51 | rc = ops->llsec->set_params(dev, ¶ms, changed); |
| 52 | } |
| 53 | |
| 54 | return rc; |
| 55 | } |
| 56 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 57 | static int |
| 58 | mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 59 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 60 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 61 | struct sockaddr_ieee802154 *sa = |
| 62 | (struct sockaddr_ieee802154 *)&ifr->ifr_addr; |
| 63 | int err = -ENOIOCTLCMD; |
| 64 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 65 | spin_lock_bh(&sdata->mib_lock); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 66 | |
| 67 | switch (cmd) { |
| 68 | case SIOCGIFADDR: |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 69 | { |
| 70 | u16 pan_id, short_addr; |
| 71 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 72 | pan_id = le16_to_cpu(sdata->pan_id); |
| 73 | short_addr = le16_to_cpu(sdata->short_addr); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 74 | if (pan_id == IEEE802154_PANID_BROADCAST || |
| 75 | short_addr == IEEE802154_ADDR_BROADCAST) { |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 76 | err = -EADDRNOTAVAIL; |
| 77 | break; |
| 78 | } |
| 79 | |
| 80 | sa->family = AF_IEEE802154; |
| 81 | sa->addr.addr_type = IEEE802154_ADDR_SHORT; |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 82 | sa->addr.pan_id = pan_id; |
| 83 | sa->addr.short_addr = short_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 84 | |
| 85 | err = 0; |
| 86 | break; |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 87 | } |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 88 | case SIOCSIFADDR: |
| 89 | dev_warn(&dev->dev, |
Masanari Iida | 9b13494 | 2014-08-06 22:31:28 +0900 | [diff] [blame] | 90 | "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n"); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 91 | if (sa->family != AF_IEEE802154 || |
| 92 | sa->addr.addr_type != IEEE802154_ADDR_SHORT || |
| 93 | sa->addr.pan_id == IEEE802154_PANID_BROADCAST || |
| 94 | sa->addr.short_addr == IEEE802154_ADDR_BROADCAST || |
| 95 | sa->addr.short_addr == IEEE802154_ADDR_UNDEF) { |
| 96 | err = -EINVAL; |
| 97 | break; |
| 98 | } |
| 99 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 100 | sdata->pan_id = cpu_to_le16(sa->addr.pan_id); |
| 101 | sdata->short_addr = cpu_to_le16(sa->addr.short_addr); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 102 | |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 103 | err = mac802154_wpan_update_llsec(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 107 | spin_unlock_bh(&sdata->mib_lock); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 108 | return err; |
| 109 | } |
| 110 | |
| 111 | static int mac802154_wpan_mac_addr(struct net_device *dev, void *p) |
| 112 | { |
| 113 | struct sockaddr *addr = p; |
| 114 | |
| 115 | if (netif_running(dev)) |
| 116 | return -EBUSY; |
| 117 | |
| 118 | /* FIXME: validate addr */ |
| 119 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
| 120 | mac802154_dev_set_ieee_addr(dev); |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 121 | return mac802154_wpan_update_llsec(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 124 | int mac802154_set_mac_params(struct net_device *dev, |
| 125 | const struct ieee802154_mac_params *params) |
| 126 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 127 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 128 | |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 129 | mutex_lock(&sdata->local->iflist_mtx); |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 130 | sdata->mac_params = *params; |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 131 | mutex_unlock(&sdata->local->iflist_mtx); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | void mac802154_get_mac_params(struct net_device *dev, |
| 137 | struct ieee802154_mac_params *params) |
| 138 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 139 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 140 | |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 141 | mutex_lock(&sdata->local->iflist_mtx); |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 142 | *params = sdata->mac_params; |
Alexander Aring | d98be45 | 2014-10-25 17:16:38 +0200 | [diff] [blame] | 143 | mutex_unlock(&sdata->local->iflist_mtx); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 146 | static int mac802154_slave_open(struct net_device *dev) |
| 147 | { |
| 148 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 149 | struct ieee802154_sub_if_data *subif; |
| 150 | struct ieee802154_local *local = sdata->local; |
| 151 | int res = 0; |
| 152 | |
| 153 | ASSERT_RTNL(); |
| 154 | |
| 155 | if (sdata->type == IEEE802154_DEV_WPAN) { |
| 156 | mutex_lock(&sdata->local->iflist_mtx); |
| 157 | list_for_each_entry(subif, &sdata->local->interfaces, list) { |
| 158 | if (subif != sdata && subif->type == sdata->type && |
| 159 | subif->running) { |
| 160 | mutex_unlock(&sdata->local->iflist_mtx); |
| 161 | return -EBUSY; |
| 162 | } |
| 163 | } |
| 164 | mutex_unlock(&sdata->local->iflist_mtx); |
| 165 | } |
| 166 | |
| 167 | mutex_lock(&sdata->local->iflist_mtx); |
| 168 | sdata->running = true; |
| 169 | mutex_unlock(&sdata->local->iflist_mtx); |
| 170 | |
| 171 | if (local->open_count++ == 0) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 172 | res = drv_start(local); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 173 | WARN_ON(res); |
| 174 | if (res) |
| 175 | goto err; |
| 176 | } |
| 177 | |
| 178 | netif_start_queue(dev); |
| 179 | return 0; |
| 180 | err: |
| 181 | sdata->local->open_count--; |
| 182 | |
| 183 | return res; |
| 184 | } |
| 185 | |
Phoebe Buckheister | 6ef0023 | 2014-05-14 17:43:11 +0200 | [diff] [blame] | 186 | static int mac802154_wpan_open(struct net_device *dev) |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 187 | { |
| 188 | int rc; |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 189 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 190 | struct ieee802154_local *local = sdata->local; |
Alexander Aring | 04e850f | 2014-10-25 17:16:37 +0200 | [diff] [blame] | 191 | struct wpan_phy *phy = sdata->local->phy; |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 192 | |
| 193 | rc = mac802154_slave_open(dev); |
| 194 | if (rc < 0) |
| 195 | return rc; |
| 196 | |
| 197 | mutex_lock(&phy->pib_lock); |
| 198 | |
| 199 | if (phy->set_txpower) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 200 | rc = drv_set_tx_power(local, sdata->mac_params.transmit_power); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 201 | if (rc < 0) |
| 202 | goto out; |
| 203 | } |
| 204 | |
| 205 | if (phy->set_lbt) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 206 | rc = drv_set_lbt_mode(local, sdata->mac_params.lbt); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 207 | if (rc < 0) |
| 208 | goto out; |
| 209 | } |
| 210 | |
| 211 | if (phy->set_cca_mode) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 212 | rc = drv_set_cca_mode(local, sdata->mac_params.cca_mode); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 213 | if (rc < 0) |
| 214 | goto out; |
| 215 | } |
| 216 | |
| 217 | if (phy->set_cca_ed_level) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 218 | rc = drv_set_cca_ed_level(local, |
| 219 | sdata->mac_params.cca_ed_level); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 220 | if (rc < 0) |
| 221 | goto out; |
| 222 | } |
| 223 | |
| 224 | if (phy->set_csma_params) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 225 | rc = drv_set_csma_params(local, sdata->mac_params.min_be, |
| 226 | sdata->mac_params.max_be, |
| 227 | sdata->mac_params.csma_retries); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 228 | if (rc < 0) |
| 229 | goto out; |
| 230 | } |
| 231 | |
| 232 | if (phy->set_frame_retries) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 233 | rc = drv_set_max_frame_retries(local, |
| 234 | sdata->mac_params.frame_retries); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 235 | if (rc < 0) |
| 236 | goto out; |
| 237 | } |
| 238 | |
| 239 | mutex_unlock(&phy->pib_lock); |
| 240 | return 0; |
| 241 | |
| 242 | out: |
| 243 | mutex_unlock(&phy->pib_lock); |
| 244 | return rc; |
| 245 | } |
| 246 | |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 247 | static int mac802154_slave_close(struct net_device *dev) |
| 248 | { |
| 249 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 250 | struct ieee802154_local *local = sdata->local; |
| 251 | |
| 252 | ASSERT_RTNL(); |
| 253 | |
| 254 | netif_stop_queue(dev); |
| 255 | |
| 256 | mutex_lock(&sdata->local->iflist_mtx); |
| 257 | sdata->running = false; |
| 258 | mutex_unlock(&sdata->local->iflist_mtx); |
| 259 | |
| 260 | if (!--local->open_count) |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 261 | drv_stop(local); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 266 | static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata, |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 267 | struct ieee802154_hdr *hdr, |
| 268 | const struct ieee802154_mac_cb *cb) |
| 269 | { |
| 270 | struct ieee802154_llsec_params params; |
| 271 | u8 level; |
| 272 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 273 | mac802154_llsec_get_params(&sdata->sec, ¶ms); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 274 | |
| 275 | if (!params.enabled && cb->secen_override && cb->secen) |
| 276 | return -EINVAL; |
| 277 | if (!params.enabled || |
| 278 | (cb->secen_override && !cb->secen) || |
| 279 | !params.out_level) |
| 280 | return 0; |
| 281 | if (cb->seclevel_override && !cb->seclevel) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | level = cb->seclevel_override ? cb->seclevel : params.out_level; |
| 285 | |
| 286 | hdr->fc.security_enabled = 1; |
| 287 | hdr->sec.level = level; |
| 288 | hdr->sec.key_id_mode = params.out_key.mode; |
| 289 | if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX) |
| 290 | hdr->sec.short_src = params.out_key.short_source; |
| 291 | else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX) |
| 292 | hdr->sec.extended_src = params.out_key.extended_source; |
| 293 | hdr->sec.key_id = params.out_key.id; |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 298 | static int mac802154_header_create(struct sk_buff *skb, |
| 299 | struct net_device *dev, |
| 300 | unsigned short type, |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 301 | const void *daddr, |
| 302 | const void *saddr, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 303 | unsigned len) |
| 304 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 305 | struct ieee802154_hdr hdr; |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 306 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 307 | struct ieee802154_mac_cb *cb = mac_cb(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 308 | int hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 309 | |
| 310 | if (!daddr) |
| 311 | return -EINVAL; |
| 312 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 313 | memset(&hdr.fc, 0, sizeof(hdr.fc)); |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 314 | hdr.fc.type = cb->type; |
| 315 | hdr.fc.security_enabled = cb->secen; |
| 316 | hdr.fc.ack_request = cb->ackreq; |
| 317 | hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 318 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 319 | if (mac802154_set_header_security(sdata, &hdr, cb) < 0) |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 320 | return -EINVAL; |
| 321 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 322 | if (!saddr) { |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 323 | spin_lock_bh(&sdata->mib_lock); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 324 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 325 | if (sdata->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) || |
| 326 | sdata->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) || |
| 327 | sdata->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 328 | hdr.source.mode = IEEE802154_ADDR_LONG; |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 329 | hdr.source.extended_addr = sdata->extended_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 330 | } else { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 331 | hdr.source.mode = IEEE802154_ADDR_SHORT; |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 332 | hdr.source.short_addr = sdata->short_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 333 | } |
| 334 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 335 | hdr.source.pan_id = sdata->pan_id; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 336 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 337 | spin_unlock_bh(&sdata->mib_lock); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 338 | } else { |
| 339 | hdr.source = *(const struct ieee802154_addr *)saddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 342 | hdr.dest = *(const struct ieee802154_addr *)daddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 343 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 344 | hlen = ieee802154_hdr_push(skb, &hdr); |
| 345 | if (hlen < 0) |
| 346 | return -EINVAL; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 347 | |
Alexander Aring | 3e69162 | 2013-10-30 09:18:22 +0100 | [diff] [blame] | 348 | skb_reset_mac_header(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 349 | skb->mac_len = hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 350 | |
Phoebe Buckheister | 8c84296 | 2014-05-14 17:43:07 +0200 | [diff] [blame] | 351 | if (len > ieee802154_max_payload(&hdr)) |
Phoebe Buckheister | d1d7358 | 2014-03-14 21:24:04 +0100 | [diff] [blame] | 352 | return -EMSGSIZE; |
| 353 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 354 | return hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static int |
| 358 | mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
| 359 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 360 | struct ieee802154_hdr hdr; |
| 361 | struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 362 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 363 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) { |
| 364 | pr_debug("malformed packet\n"); |
| 365 | return 0; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 368 | *addr = hdr.source; |
| 369 | return sizeof(*addr); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 370 | } |
| 371 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 372 | static struct header_ops mac802154_header_ops = { |
| 373 | .create = mac802154_header_create, |
| 374 | .parse = mac802154_header_parse, |
| 375 | }; |
| 376 | |
| 377 | static const struct net_device_ops mac802154_wpan_ops = { |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 378 | .ndo_open = mac802154_wpan_open, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 379 | .ndo_stop = mac802154_slave_close, |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 380 | .ndo_start_xmit = ieee802154_subif_start_xmit, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 381 | .ndo_do_ioctl = mac802154_wpan_ioctl, |
| 382 | .ndo_set_mac_address = mac802154_wpan_mac_addr, |
| 383 | }; |
| 384 | |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 385 | static const struct net_device_ops mac802154_monitor_ops = { |
| 386 | .ndo_open = mac802154_slave_open, |
| 387 | .ndo_stop = mac802154_slave_close, |
| 388 | .ndo_start_xmit = ieee802154_monitor_start_xmit, |
| 389 | }; |
| 390 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 391 | static void mac802154_wpan_free(struct net_device *dev) |
| 392 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 393 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 394 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 395 | mac802154_llsec_destroy(&sdata->sec); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 396 | |
| 397 | free_netdev(dev); |
| 398 | } |
| 399 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 400 | void mac802154_wpan_setup(struct net_device *dev) |
| 401 | { |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 402 | struct ieee802154_sub_if_data *sdata; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 403 | |
| 404 | dev->addr_len = IEEE802154_ADDR_LEN; |
| 405 | memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); |
| 406 | |
| 407 | dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN; |
| 408 | dev->header_ops = &mac802154_header_ops; |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 409 | dev->needed_tailroom = 2 + 16; /* FCS + MIC */ |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 410 | dev->mtu = IEEE802154_MTU; |
Alan Ott | e937f583 | 2013-04-03 04:00:57 +0000 | [diff] [blame] | 411 | dev->tx_queue_len = 300; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 412 | dev->type = ARPHRD_IEEE802154; |
| 413 | dev->flags = IFF_NOARP | IFF_BROADCAST; |
| 414 | dev->watchdog_timeo = 0; |
| 415 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 416 | dev->destructor = mac802154_wpan_free; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 417 | dev->netdev_ops = &mac802154_wpan_ops; |
| 418 | dev->ml_priv = &mac802154_mlme_wpan; |
| 419 | |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 420 | sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 421 | sdata->type = IEEE802154_DEV_WPAN; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 422 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 423 | sdata->chan = MAC802154_CHAN_NONE; |
| 424 | sdata->page = 0; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 425 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 426 | spin_lock_init(&sdata->mib_lock); |
| 427 | mutex_init(&sdata->sec_mtx); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 428 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 429 | get_random_bytes(&sdata->bsn, 1); |
| 430 | get_random_bytes(&sdata->dsn, 1); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 431 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 432 | /* defaults per 802.15.4-2011 */ |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 433 | sdata->mac_params.min_be = 3; |
| 434 | sdata->mac_params.max_be = 5; |
| 435 | sdata->mac_params.csma_retries = 4; |
| 436 | /* for compatibility, actual default is 3 */ |
| 437 | sdata->mac_params.frame_retries = -1; |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 438 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 439 | sdata->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); |
| 440 | sdata->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 441 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 442 | mac802154_llsec_init(&sdata->sec); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 443 | } |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 444 | |
| 445 | void mac802154_monitor_setup(struct net_device *dev) |
| 446 | { |
| 447 | struct ieee802154_sub_if_data *sdata; |
| 448 | |
| 449 | dev->addr_len = 0; |
| 450 | dev->hard_header_len = 0; |
| 451 | dev->needed_tailroom = 2; /* room for FCS */ |
| 452 | dev->mtu = IEEE802154_MTU; |
| 453 | dev->tx_queue_len = 10; |
| 454 | dev->type = ARPHRD_IEEE802154_MONITOR; |
| 455 | dev->flags = IFF_NOARP | IFF_BROADCAST; |
| 456 | dev->watchdog_timeo = 0; |
| 457 | |
| 458 | dev->destructor = free_netdev; |
| 459 | dev->netdev_ops = &mac802154_monitor_ops; |
| 460 | dev->ml_priv = &mac802154_mlme_reduced; |
| 461 | |
| 462 | sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 463 | sdata->type = IEEE802154_DEV_MONITOR; |
| 464 | |
| 465 | sdata->chan = MAC802154_CHAN_NONE; /* not initialized */ |
| 466 | sdata->page = 0; |
| 467 | } |