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