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