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