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 | { |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 113 | 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] | 114 | struct sockaddr *addr = p; |
Alexander Aring | ea7053c | 2014-11-02 04:18:46 +0100 | [diff] [blame] | 115 | __le64 extended_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 116 | |
| 117 | if (netif_running(dev)) |
| 118 | return -EBUSY; |
| 119 | |
Alexander Aring | 705cbbb | 2014-11-05 20:51:24 +0100 | [diff] [blame] | 120 | ieee802154_be64_to_le64(&extended_addr, addr->sa_data); |
Alexander Aring | ea7053c | 2014-11-02 04:18:46 +0100 | [diff] [blame] | 121 | if (!ieee802154_is_valid_extended_addr(extended_addr)) |
| 122 | return -EINVAL; |
| 123 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 124 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
Alexander Aring | ea7053c | 2014-11-02 04:18:46 +0100 | [diff] [blame] | 125 | sdata->extended_addr = extended_addr; |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 126 | |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 127 | return mac802154_wpan_update_llsec(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 130 | static int mac802154_slave_open(struct net_device *dev) |
| 131 | { |
| 132 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 133 | struct ieee802154_sub_if_data *subif; |
| 134 | struct ieee802154_local *local = sdata->local; |
| 135 | int res = 0; |
| 136 | |
| 137 | ASSERT_RTNL(); |
| 138 | |
Alexander Aring | 7c118c1 | 2014-11-05 20:51:20 +0100 | [diff] [blame] | 139 | if (sdata->vif.type == IEEE802154_DEV_WPAN) { |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 140 | mutex_lock(&sdata->local->iflist_mtx); |
| 141 | list_for_each_entry(subif, &sdata->local->interfaces, list) { |
Alexander Aring | 7c118c1 | 2014-11-05 20:51:20 +0100 | [diff] [blame] | 142 | if (subif != sdata && |
| 143 | subif->vif.type == sdata->vif.type && |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 144 | ieee802154_sdata_running(subif)) { |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 145 | mutex_unlock(&sdata->local->iflist_mtx); |
| 146 | return -EBUSY; |
| 147 | } |
| 148 | } |
| 149 | mutex_unlock(&sdata->local->iflist_mtx); |
| 150 | } |
| 151 | |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 152 | set_bit(SDATA_STATE_RUNNING, &sdata->state); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 153 | |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 154 | if (!local->open_count) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 155 | res = drv_start(local); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 156 | WARN_ON(res); |
| 157 | if (res) |
| 158 | goto err; |
| 159 | } |
| 160 | |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 161 | local->open_count++; |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 162 | netif_start_queue(dev); |
| 163 | return 0; |
| 164 | err: |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 165 | /* might already be clear but that doesn't matter */ |
| 166 | clear_bit(SDATA_STATE_RUNNING, &sdata->state); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 167 | |
| 168 | return res; |
| 169 | } |
| 170 | |
Phoebe Buckheister | 6ef0023 | 2014-05-14 17:43:11 +0200 | [diff] [blame] | 171 | static int mac802154_wpan_open(struct net_device *dev) |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 172 | { |
| 173 | int rc; |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 174 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 175 | struct ieee802154_local *local = sdata->local; |
Alexander Aring | 04e850f | 2014-10-25 17:16:37 +0200 | [diff] [blame] | 176 | struct wpan_phy *phy = sdata->local->phy; |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 177 | |
| 178 | rc = mac802154_slave_open(dev); |
| 179 | if (rc < 0) |
| 180 | return rc; |
| 181 | |
| 182 | mutex_lock(&phy->pib_lock); |
| 183 | |
Alexander Aring | 38130c3 | 2014-10-29 21:34:44 +0100 | [diff] [blame] | 184 | if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) { |
| 185 | rc = drv_set_promiscuous_mode(local, sdata->promisuous_mode); |
| 186 | if (rc < 0) |
| 187 | goto out; |
| 188 | } |
| 189 | |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 190 | if (local->hw.flags & IEEE802154_HW_AFILT) { |
Alexander Aring | 50c7907 | 2014-11-02 04:18:43 +0100 | [diff] [blame] | 191 | rc = drv_set_pan_id(local, sdata->pan_id); |
| 192 | if (rc < 0) |
| 193 | goto out; |
| 194 | |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 195 | rc = drv_set_extended_addr(local, sdata->extended_addr); |
| 196 | if (rc < 0) |
| 197 | goto out; |
Alexander Aring | 78b4bad16 | 2014-11-02 04:18:42 +0100 | [diff] [blame] | 198 | |
| 199 | rc = drv_set_short_addr(local, sdata->short_addr); |
| 200 | if (rc < 0) |
| 201 | goto out; |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Alexander Aring | a543c59 | 2014-10-28 18:21:23 +0100 | [diff] [blame] | 204 | if (local->hw.flags & IEEE802154_HW_LBT) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 205 | rc = drv_set_lbt_mode(local, sdata->mac_params.lbt); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 206 | if (rc < 0) |
| 207 | goto out; |
| 208 | } |
| 209 | |
Alexander Aring | a543c59 | 2014-10-28 18:21:23 +0100 | [diff] [blame] | 210 | if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 211 | rc = drv_set_csma_params(local, sdata->mac_params.min_be, |
| 212 | sdata->mac_params.max_be, |
| 213 | sdata->mac_params.csma_retries); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 214 | if (rc < 0) |
| 215 | goto out; |
| 216 | } |
| 217 | |
Alexander Aring | a543c59 | 2014-10-28 18:21:23 +0100 | [diff] [blame] | 218 | if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 219 | rc = drv_set_max_frame_retries(local, |
| 220 | sdata->mac_params.frame_retries); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 221 | if (rc < 0) |
| 222 | goto out; |
| 223 | } |
| 224 | |
| 225 | mutex_unlock(&phy->pib_lock); |
| 226 | return 0; |
| 227 | |
| 228 | out: |
| 229 | mutex_unlock(&phy->pib_lock); |
| 230 | return rc; |
| 231 | } |
| 232 | |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 233 | static int mac802154_slave_close(struct net_device *dev) |
| 234 | { |
| 235 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 236 | struct ieee802154_local *local = sdata->local; |
| 237 | |
| 238 | ASSERT_RTNL(); |
| 239 | |
| 240 | netif_stop_queue(dev); |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 241 | local->open_count--; |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 242 | |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 243 | clear_bit(SDATA_STATE_RUNNING, &sdata->state); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 244 | |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 245 | if (!local->open_count) |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 246 | drv_stop(local); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 251 | static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata, |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 252 | struct ieee802154_hdr *hdr, |
| 253 | const struct ieee802154_mac_cb *cb) |
| 254 | { |
| 255 | struct ieee802154_llsec_params params; |
| 256 | u8 level; |
| 257 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 258 | mac802154_llsec_get_params(&sdata->sec, ¶ms); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 259 | |
| 260 | if (!params.enabled && cb->secen_override && cb->secen) |
| 261 | return -EINVAL; |
| 262 | if (!params.enabled || |
| 263 | (cb->secen_override && !cb->secen) || |
| 264 | !params.out_level) |
| 265 | return 0; |
| 266 | if (cb->seclevel_override && !cb->seclevel) |
| 267 | return -EINVAL; |
| 268 | |
| 269 | level = cb->seclevel_override ? cb->seclevel : params.out_level; |
| 270 | |
| 271 | hdr->fc.security_enabled = 1; |
| 272 | hdr->sec.level = level; |
| 273 | hdr->sec.key_id_mode = params.out_key.mode; |
| 274 | if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX) |
| 275 | hdr->sec.short_src = params.out_key.short_source; |
| 276 | else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX) |
| 277 | hdr->sec.extended_src = params.out_key.extended_source; |
| 278 | hdr->sec.key_id = params.out_key.id; |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 283 | static int mac802154_header_create(struct sk_buff *skb, |
| 284 | struct net_device *dev, |
| 285 | unsigned short type, |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 286 | const void *daddr, |
| 287 | const void *saddr, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 288 | unsigned len) |
| 289 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 290 | struct ieee802154_hdr hdr; |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 291 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 292 | struct ieee802154_mac_cb *cb = mac_cb(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 293 | int hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 294 | |
| 295 | if (!daddr) |
| 296 | return -EINVAL; |
| 297 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 298 | memset(&hdr.fc, 0, sizeof(hdr.fc)); |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 299 | hdr.fc.type = cb->type; |
| 300 | hdr.fc.security_enabled = cb->secen; |
| 301 | hdr.fc.ack_request = cb->ackreq; |
| 302 | hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 303 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 304 | if (mac802154_set_header_security(sdata, &hdr, cb) < 0) |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 305 | return -EINVAL; |
| 306 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 307 | if (!saddr) { |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 308 | spin_lock_bh(&sdata->mib_lock); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 309 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 310 | if (sdata->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) || |
| 311 | sdata->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) || |
| 312 | sdata->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 313 | hdr.source.mode = IEEE802154_ADDR_LONG; |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 314 | hdr.source.extended_addr = sdata->extended_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 315 | } else { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 316 | hdr.source.mode = IEEE802154_ADDR_SHORT; |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 317 | hdr.source.short_addr = sdata->short_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 320 | hdr.source.pan_id = sdata->pan_id; |
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 | spin_unlock_bh(&sdata->mib_lock); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 323 | } else { |
| 324 | hdr.source = *(const struct ieee802154_addr *)saddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 327 | hdr.dest = *(const struct ieee802154_addr *)daddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 328 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 329 | hlen = ieee802154_hdr_push(skb, &hdr); |
| 330 | if (hlen < 0) |
| 331 | return -EINVAL; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 332 | |
Alexander Aring | 3e69162 | 2013-10-30 09:18:22 +0100 | [diff] [blame] | 333 | skb_reset_mac_header(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 334 | skb->mac_len = hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 335 | |
Phoebe Buckheister | 8c84296 | 2014-05-14 17:43:07 +0200 | [diff] [blame] | 336 | if (len > ieee802154_max_payload(&hdr)) |
Phoebe Buckheister | d1d7358 | 2014-03-14 21:24:04 +0100 | [diff] [blame] | 337 | return -EMSGSIZE; |
| 338 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 339 | return hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | static int |
| 343 | mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
| 344 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 345 | struct ieee802154_hdr hdr; |
| 346 | struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 347 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 348 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) { |
| 349 | pr_debug("malformed packet\n"); |
| 350 | return 0; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 351 | } |
| 352 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 353 | *addr = hdr.source; |
| 354 | return sizeof(*addr); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 355 | } |
| 356 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 357 | static struct header_ops mac802154_header_ops = { |
| 358 | .create = mac802154_header_create, |
| 359 | .parse = mac802154_header_parse, |
| 360 | }; |
| 361 | |
| 362 | static const struct net_device_ops mac802154_wpan_ops = { |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 363 | .ndo_open = mac802154_wpan_open, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 364 | .ndo_stop = mac802154_slave_close, |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 365 | .ndo_start_xmit = ieee802154_subif_start_xmit, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 366 | .ndo_do_ioctl = mac802154_wpan_ioctl, |
| 367 | .ndo_set_mac_address = mac802154_wpan_mac_addr, |
| 368 | }; |
| 369 | |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 370 | static const struct net_device_ops mac802154_monitor_ops = { |
Alexander Aring | 38130c3 | 2014-10-29 21:34:44 +0100 | [diff] [blame] | 371 | .ndo_open = mac802154_wpan_open, |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 372 | .ndo_stop = mac802154_slave_close, |
| 373 | .ndo_start_xmit = ieee802154_monitor_start_xmit, |
| 374 | }; |
| 375 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 376 | static void mac802154_wpan_free(struct net_device *dev) |
| 377 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 378 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 379 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 380 | mac802154_llsec_destroy(&sdata->sec); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 381 | |
| 382 | free_netdev(dev); |
| 383 | } |
| 384 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 385 | static void ieee802154_if_setup(struct net_device *dev) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 386 | { |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 387 | dev->addr_len = IEEE802154_ADDR_LEN; |
| 388 | memset(dev->broadcast, 0xff, IEEE802154_ADDR_LEN); |
| 389 | |
| 390 | dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN; |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 391 | dev->needed_tailroom = 2 + 16; /* FCS + MIC */ |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 392 | dev->mtu = IEEE802154_MTU; |
Alan Ott | e937f583 | 2013-04-03 04:00:57 +0000 | [diff] [blame] | 393 | dev->tx_queue_len = 300; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 394 | dev->flags = IFF_NOARP | IFF_BROADCAST; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 395 | } |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 396 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 397 | static int |
| 398 | ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata, int type) |
| 399 | { |
| 400 | /* set some type-dependent values */ |
Alexander Aring | 7c118c1 | 2014-11-05 20:51:20 +0100 | [diff] [blame] | 401 | sdata->vif.type = type; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 402 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 403 | get_random_bytes(&sdata->bsn, 1); |
| 404 | get_random_bytes(&sdata->dsn, 1); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 405 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 406 | /* defaults per 802.15.4-2011 */ |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 407 | sdata->mac_params.min_be = 3; |
| 408 | sdata->mac_params.max_be = 5; |
| 409 | sdata->mac_params.csma_retries = 4; |
| 410 | /* for compatibility, actual default is 3 */ |
| 411 | sdata->mac_params.frame_retries = -1; |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 412 | |
Alexander Aring | dee56d14 | 2014-11-05 20:51:25 +0100 | [diff] [blame^] | 413 | ieee802154_be64_to_le64(&sdata->extended_addr, sdata->dev->dev_addr); |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 414 | sdata->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); |
| 415 | sdata->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 416 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 417 | switch (type) { |
| 418 | case IEEE802154_DEV_WPAN: |
| 419 | sdata->dev->header_ops = &mac802154_header_ops; |
| 420 | sdata->dev->destructor = mac802154_wpan_free; |
| 421 | sdata->dev->netdev_ops = &mac802154_wpan_ops; |
| 422 | sdata->dev->ml_priv = &mac802154_mlme_wpan; |
| 423 | sdata->promisuous_mode = false; |
Alexander Aring | 38130c3 | 2014-10-29 21:34:44 +0100 | [diff] [blame] | 424 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 425 | spin_lock_init(&sdata->mib_lock); |
| 426 | mutex_init(&sdata->sec_mtx); |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 427 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 428 | mac802154_llsec_init(&sdata->sec); |
| 429 | break; |
| 430 | case IEEE802154_DEV_MONITOR: |
| 431 | sdata->dev->destructor = free_netdev; |
| 432 | sdata->dev->netdev_ops = &mac802154_monitor_ops; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 433 | sdata->promisuous_mode = true; |
| 434 | break; |
| 435 | default: |
| 436 | BUG(); |
| 437 | } |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 438 | |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | struct net_device * |
| 443 | ieee802154_if_add(struct ieee802154_local *local, const char *name, |
| 444 | struct wpan_dev **new_wpan_dev, int type) |
| 445 | { |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 446 | struct net_device *ndev = NULL; |
| 447 | struct ieee802154_sub_if_data *sdata = NULL; |
| 448 | int ret = -ENOMEM; |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 449 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 450 | ASSERT_RTNL(); |
| 451 | |
Alexander Aring | 7c118c1 | 2014-11-05 20:51:20 +0100 | [diff] [blame] | 452 | ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size, name, |
| 453 | NET_NAME_UNKNOWN, ieee802154_if_setup); |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 454 | if (!ndev) |
| 455 | return ERR_PTR(-ENOMEM); |
| 456 | |
| 457 | ndev->needed_headroom = local->hw.extra_tx_headroom; |
| 458 | |
| 459 | ret = dev_alloc_name(ndev, ndev->name); |
| 460 | if (ret < 0) |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 461 | goto err; |
| 462 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 463 | switch (type) { |
| 464 | case IEEE802154_DEV_WPAN: |
| 465 | ndev->type = ARPHRD_IEEE802154; |
| 466 | break; |
| 467 | case IEEE802154_DEV_MONITOR: |
| 468 | ndev->type = ARPHRD_IEEE802154_MONITOR; |
| 469 | break; |
| 470 | default: |
| 471 | ret = -EINVAL; |
| 472 | goto err; |
| 473 | } |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 474 | |
Alexander Aring | dee56d14 | 2014-11-05 20:51:25 +0100 | [diff] [blame^] | 475 | ieee802154_le64_to_be64(ndev->perm_addr, |
| 476 | &local->hw.phy->perm_extended_addr); |
| 477 | memcpy(ndev->dev_addr, ndev->perm_addr, IEEE802154_EXTENDED_ADDR_LEN); |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 478 | /* TODO check this */ |
| 479 | SET_NETDEV_DEV(ndev, &local->phy->dev); |
| 480 | sdata = netdev_priv(ndev); |
| 481 | ndev->ieee802154_ptr = &sdata->wpan_dev; |
| 482 | memcpy(sdata->name, ndev->name, IFNAMSIZ); |
| 483 | sdata->dev = ndev; |
| 484 | sdata->wpan_dev.wpan_phy = local->hw.phy; |
| 485 | sdata->local = local; |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 486 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 487 | /* setup type-dependent data */ |
| 488 | ret = ieee802154_setup_sdata(sdata, type); |
| 489 | if (ret) |
| 490 | goto err; |
| 491 | |
| 492 | if (ndev) { |
| 493 | ret = register_netdevice(ndev); |
| 494 | if (ret < 0) |
| 495 | goto err; |
| 496 | } |
| 497 | |
| 498 | mutex_lock(&local->iflist_mtx); |
| 499 | list_add_tail_rcu(&sdata->list, &local->interfaces); |
| 500 | mutex_unlock(&local->iflist_mtx); |
| 501 | |
| 502 | if (new_wpan_dev) |
| 503 | *new_wpan_dev = &sdata->wpan_dev; |
| 504 | |
| 505 | return ndev; |
| 506 | |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 507 | err: |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 508 | free_netdev(ndev); |
| 509 | return ERR_PTR(ret); |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 510 | } |
| 511 | |
Alexander Aring | b210b18 | 2014-11-05 20:51:14 +0100 | [diff] [blame] | 512 | void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata) |
| 513 | { |
| 514 | ASSERT_RTNL(); |
| 515 | |
| 516 | mutex_lock(&sdata->local->iflist_mtx); |
| 517 | list_del_rcu(&sdata->list); |
| 518 | mutex_unlock(&sdata->local->iflist_mtx); |
| 519 | |
| 520 | synchronize_rcu(); |
| 521 | unregister_netdevice(sdata->dev); |
| 522 | } |