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 | |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 25 | #include <net/nl802154.h> |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 26 | #include <net/mac802154.h> |
| 27 | #include <net/ieee802154_netdev.h> |
Alexander Aring | 5ad60d3 | 2014-10-25 09:41:02 +0200 | [diff] [blame] | 28 | #include <net/cfg802154.h> |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 29 | |
Alexander Aring | 0f1556b | 2014-10-25 09:41:00 +0200 | [diff] [blame] | 30 | #include "ieee802154_i.h" |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 31 | #include "driver-ops.h" |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 32 | |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 33 | static int mac802154_wpan_update_llsec(struct net_device *dev) |
| 34 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 35 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 36 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 37 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 38 | int rc = 0; |
| 39 | |
| 40 | if (ops->llsec) { |
| 41 | struct ieee802154_llsec_params params; |
| 42 | int changed = 0; |
| 43 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 44 | params.pan_id = wpan_dev->pan_id; |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 45 | changed |= IEEE802154_LLSEC_PARAM_PAN_ID; |
| 46 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 47 | params.hwaddr = wpan_dev->extended_addr; |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 48 | changed |= IEEE802154_LLSEC_PARAM_HWADDR; |
| 49 | |
| 50 | rc = ops->llsec->set_params(dev, ¶ms, changed); |
| 51 | } |
| 52 | |
| 53 | return rc; |
| 54 | } |
| 55 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 56 | static int |
| 57 | mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) |
| 58 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 59 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 60 | struct wpan_dev *wpan_dev = &sdata->wpan_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 | f7cb96f | 2014-11-09 08:36:59 +0100 | [diff] [blame] | 65 | ASSERT_RTNL(); |
| 66 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 67 | spin_lock_bh(&sdata->mib_lock); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 68 | |
| 69 | switch (cmd) { |
| 70 | case SIOCGIFADDR: |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 71 | { |
| 72 | u16 pan_id, short_addr; |
| 73 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 74 | pan_id = le16_to_cpu(wpan_dev->pan_id); |
| 75 | short_addr = le16_to_cpu(wpan_dev->short_addr); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 76 | if (pan_id == IEEE802154_PANID_BROADCAST || |
| 77 | short_addr == IEEE802154_ADDR_BROADCAST) { |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 78 | err = -EADDRNOTAVAIL; |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | sa->family = AF_IEEE802154; |
| 83 | sa->addr.addr_type = IEEE802154_ADDR_SHORT; |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 84 | sa->addr.pan_id = pan_id; |
| 85 | sa->addr.short_addr = short_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 86 | |
| 87 | err = 0; |
| 88 | break; |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 89 | } |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 90 | case SIOCSIFADDR: |
Alexander Aring | f7cb96f | 2014-11-09 08:36:59 +0100 | [diff] [blame] | 91 | if (netif_running(dev)) { |
| 92 | spin_unlock_bh(&sdata->mib_lock); |
| 93 | return -EBUSY; |
| 94 | } |
| 95 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 96 | dev_warn(&dev->dev, |
Masanari Iida | 9b13494 | 2014-08-06 22:31:28 +0900 | [diff] [blame] | 97 | "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n"); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 98 | if (sa->family != AF_IEEE802154 || |
| 99 | sa->addr.addr_type != IEEE802154_ADDR_SHORT || |
| 100 | sa->addr.pan_id == IEEE802154_PANID_BROADCAST || |
| 101 | sa->addr.short_addr == IEEE802154_ADDR_BROADCAST || |
| 102 | sa->addr.short_addr == IEEE802154_ADDR_UNDEF) { |
| 103 | err = -EINVAL; |
| 104 | break; |
| 105 | } |
| 106 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 107 | wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id); |
| 108 | wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 109 | |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 110 | err = mac802154_wpan_update_llsec(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 114 | spin_unlock_bh(&sdata->mib_lock); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 115 | return err; |
| 116 | } |
| 117 | |
| 118 | static int mac802154_wpan_mac_addr(struct net_device *dev, void *p) |
| 119 | { |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 120 | 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] | 121 | struct sockaddr *addr = p; |
Alexander Aring | ea7053c | 2014-11-02 04:18:46 +0100 | [diff] [blame] | 122 | __le64 extended_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 123 | |
| 124 | if (netif_running(dev)) |
| 125 | return -EBUSY; |
| 126 | |
Alexander Aring | 705cbbb | 2014-11-05 20:51:24 +0100 | [diff] [blame] | 127 | ieee802154_be64_to_le64(&extended_addr, addr->sa_data); |
Alexander Aring | ea7053c | 2014-11-02 04:18:46 +0100 | [diff] [blame] | 128 | if (!ieee802154_is_valid_extended_addr(extended_addr)) |
| 129 | return -EINVAL; |
| 130 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 131 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 132 | sdata->wpan_dev.extended_addr = extended_addr; |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 133 | |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 134 | return mac802154_wpan_update_llsec(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 137 | static int mac802154_slave_open(struct net_device *dev) |
| 138 | { |
| 139 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 140 | struct ieee802154_local *local = sdata->local; |
| 141 | int res = 0; |
| 142 | |
| 143 | ASSERT_RTNL(); |
| 144 | |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 145 | set_bit(SDATA_STATE_RUNNING, &sdata->state); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 146 | |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 147 | if (!local->open_count) { |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 148 | res = drv_start(local); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 149 | WARN_ON(res); |
| 150 | if (res) |
| 151 | goto err; |
| 152 | } |
| 153 | |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 154 | local->open_count++; |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 155 | netif_start_queue(dev); |
| 156 | return 0; |
| 157 | err: |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 158 | /* might already be clear but that doesn't matter */ |
| 159 | clear_bit(SDATA_STATE_RUNNING, &sdata->state); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 160 | |
| 161 | return res; |
| 162 | } |
| 163 | |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 164 | static int |
| 165 | ieee802154_check_mac_settings(struct ieee802154_local *local, |
| 166 | struct wpan_dev *wpan_dev, |
| 167 | struct wpan_dev *nwpan_dev) |
| 168 | { |
| 169 | ASSERT_RTNL(); |
| 170 | |
| 171 | if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) { |
| 172 | if (wpan_dev->promiscuous_mode != nwpan_dev->promiscuous_mode) |
| 173 | return -EBUSY; |
| 174 | } |
| 175 | |
| 176 | if (local->hw.flags & IEEE802154_HW_AFILT) { |
Alexander Aring | 8bf9538 | 2015-03-26 12:46:28 +0100 | [diff] [blame] | 177 | if (wpan_dev->pan_id != nwpan_dev->pan_id || |
| 178 | wpan_dev->short_addr != nwpan_dev->short_addr || |
| 179 | wpan_dev->extended_addr != nwpan_dev->extended_addr) |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 180 | return -EBUSY; |
| 181 | } |
| 182 | |
| 183 | if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) { |
Alexander Aring | 8bf9538 | 2015-03-26 12:46:28 +0100 | [diff] [blame] | 184 | if (wpan_dev->min_be != nwpan_dev->min_be || |
| 185 | wpan_dev->max_be != nwpan_dev->max_be || |
| 186 | wpan_dev->csma_retries != nwpan_dev->csma_retries) |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 187 | return -EBUSY; |
| 188 | } |
| 189 | |
| 190 | if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) { |
| 191 | if (wpan_dev->frame_retries != nwpan_dev->frame_retries) |
| 192 | return -EBUSY; |
| 193 | } |
| 194 | |
| 195 | if (local->hw.flags & IEEE802154_HW_LBT) { |
| 196 | if (wpan_dev->lbt != nwpan_dev->lbt) |
| 197 | return -EBUSY; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | static int |
| 204 | ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata, |
| 205 | enum nl802154_iftype iftype) |
| 206 | { |
| 207 | struct ieee802154_local *local = sdata->local; |
| 208 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
| 209 | struct ieee802154_sub_if_data *nsdata; |
| 210 | |
| 211 | /* we hold the RTNL here so can safely walk the list */ |
| 212 | list_for_each_entry(nsdata, &local->interfaces, list) { |
| 213 | if (nsdata != sdata && ieee802154_sdata_running(nsdata)) { |
| 214 | int ret; |
| 215 | |
Alexander Aring | cd1c566 | 2014-12-19 23:45:59 +0100 | [diff] [blame] | 216 | /* TODO currently we don't support multiple node types |
| 217 | * we need to run skb_clone at rx path. Check if there |
| 218 | * exist really an use case if we need to support |
| 219 | * multiple node types at the same time. |
| 220 | */ |
| 221 | if (sdata->vif.type == NL802154_IFTYPE_NODE && |
| 222 | nsdata->vif.type == NL802154_IFTYPE_NODE) |
| 223 | return -EBUSY; |
| 224 | |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 225 | /* check all phy mac sublayer settings are the same. |
| 226 | * We have only one phy, different values makes trouble. |
| 227 | */ |
| 228 | ret = ieee802154_check_mac_settings(local, wpan_dev, |
| 229 | &nsdata->wpan_dev); |
| 230 | if (ret < 0) |
| 231 | return ret; |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
Phoebe Buckheister | 6ef0023 | 2014-05-14 17:43:11 +0200 | [diff] [blame] | 238 | static int mac802154_wpan_open(struct net_device *dev) |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 239 | { |
| 240 | int rc; |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 241 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 242 | struct ieee802154_local *local = sdata->local; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 243 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
Alexander Aring | 04e850f | 2014-10-25 17:16:37 +0200 | [diff] [blame] | 244 | struct wpan_phy *phy = sdata->local->phy; |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 245 | |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 246 | rc = ieee802154_check_concurrent_iface(sdata, sdata->vif.type); |
| 247 | if (rc < 0) |
| 248 | return rc; |
| 249 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 250 | rc = mac802154_slave_open(dev); |
| 251 | if (rc < 0) |
| 252 | return rc; |
| 253 | |
| 254 | mutex_lock(&phy->pib_lock); |
| 255 | |
Alexander Aring | 38130c3 | 2014-10-29 21:34:44 +0100 | [diff] [blame] | 256 | if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) { |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 257 | rc = drv_set_promiscuous_mode(local, |
| 258 | wpan_dev->promiscuous_mode); |
Alexander Aring | 38130c3 | 2014-10-29 21:34:44 +0100 | [diff] [blame] | 259 | if (rc < 0) |
| 260 | goto out; |
| 261 | } |
| 262 | |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 263 | if (local->hw.flags & IEEE802154_HW_AFILT) { |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 264 | rc = drv_set_pan_id(local, wpan_dev->pan_id); |
Alexander Aring | 50c7907 | 2014-11-02 04:18:43 +0100 | [diff] [blame] | 265 | if (rc < 0) |
| 266 | goto out; |
| 267 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 268 | rc = drv_set_extended_addr(local, wpan_dev->extended_addr); |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 269 | if (rc < 0) |
| 270 | goto out; |
Alexander Aring | 78b4bad16 | 2014-11-02 04:18:42 +0100 | [diff] [blame] | 271 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 272 | rc = drv_set_short_addr(local, wpan_dev->short_addr); |
Alexander Aring | 78b4bad16 | 2014-11-02 04:18:42 +0100 | [diff] [blame] | 273 | if (rc < 0) |
| 274 | goto out; |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 275 | } |
| 276 | |
Alexander Aring | a543c59 | 2014-10-28 18:21:23 +0100 | [diff] [blame] | 277 | if (local->hw.flags & IEEE802154_HW_LBT) { |
Alexander Aring | 5fb3f02 | 2014-11-09 08:36:46 +0100 | [diff] [blame] | 278 | rc = drv_set_lbt_mode(local, wpan_dev->lbt); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 279 | if (rc < 0) |
| 280 | goto out; |
| 281 | } |
| 282 | |
Alexander Aring | a543c59 | 2014-10-28 18:21:23 +0100 | [diff] [blame] | 283 | if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) { |
Alexander Aring | 5fb3f02 | 2014-11-09 08:36:46 +0100 | [diff] [blame] | 284 | rc = drv_set_csma_params(local, wpan_dev->min_be, |
| 285 | wpan_dev->max_be, |
| 286 | wpan_dev->csma_retries); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 287 | if (rc < 0) |
| 288 | goto out; |
| 289 | } |
| 290 | |
Alexander Aring | a543c59 | 2014-10-28 18:21:23 +0100 | [diff] [blame] | 291 | if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) { |
Alexander Aring | 5fb3f02 | 2014-11-09 08:36:46 +0100 | [diff] [blame] | 292 | rc = drv_set_max_frame_retries(local, wpan_dev->frame_retries); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 293 | if (rc < 0) |
| 294 | goto out; |
| 295 | } |
| 296 | |
| 297 | mutex_unlock(&phy->pib_lock); |
| 298 | return 0; |
| 299 | |
| 300 | out: |
| 301 | mutex_unlock(&phy->pib_lock); |
| 302 | return rc; |
| 303 | } |
| 304 | |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 305 | static int mac802154_slave_close(struct net_device *dev) |
| 306 | { |
| 307 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 308 | struct ieee802154_local *local = sdata->local; |
| 309 | |
| 310 | ASSERT_RTNL(); |
| 311 | |
Alexander Aring | 61f2dcb | 2014-11-12 19:51:56 +0100 | [diff] [blame] | 312 | hrtimer_cancel(&local->ifs_timer); |
| 313 | |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 314 | netif_stop_queue(dev); |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 315 | local->open_count--; |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 316 | |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 317 | clear_bit(SDATA_STATE_RUNNING, &sdata->state); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 318 | |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 319 | if (!local->open_count) |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 320 | drv_stop(local); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 325 | static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata, |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 326 | struct ieee802154_hdr *hdr, |
| 327 | const struct ieee802154_mac_cb *cb) |
| 328 | { |
| 329 | struct ieee802154_llsec_params params; |
| 330 | u8 level; |
| 331 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 332 | mac802154_llsec_get_params(&sdata->sec, ¶ms); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 333 | |
| 334 | if (!params.enabled && cb->secen_override && cb->secen) |
| 335 | return -EINVAL; |
| 336 | if (!params.enabled || |
| 337 | (cb->secen_override && !cb->secen) || |
| 338 | !params.out_level) |
| 339 | return 0; |
| 340 | if (cb->seclevel_override && !cb->seclevel) |
| 341 | return -EINVAL; |
| 342 | |
| 343 | level = cb->seclevel_override ? cb->seclevel : params.out_level; |
| 344 | |
| 345 | hdr->fc.security_enabled = 1; |
| 346 | hdr->sec.level = level; |
| 347 | hdr->sec.key_id_mode = params.out_key.mode; |
| 348 | if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX) |
| 349 | hdr->sec.short_src = params.out_key.short_source; |
| 350 | else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX) |
| 351 | hdr->sec.extended_src = params.out_key.extended_source; |
| 352 | hdr->sec.key_id = params.out_key.id; |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 357 | static int mac802154_header_create(struct sk_buff *skb, |
| 358 | struct net_device *dev, |
| 359 | unsigned short type, |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 360 | const void *daddr, |
| 361 | const void *saddr, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 362 | unsigned len) |
| 363 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 364 | struct ieee802154_hdr hdr; |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 365 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 366 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 367 | struct ieee802154_mac_cb *cb = mac_cb(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 368 | int hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 369 | |
| 370 | if (!daddr) |
| 371 | return -EINVAL; |
| 372 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 373 | memset(&hdr.fc, 0, sizeof(hdr.fc)); |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 374 | hdr.fc.type = cb->type; |
| 375 | hdr.fc.security_enabled = cb->secen; |
| 376 | hdr.fc.ack_request = cb->ackreq; |
| 377 | hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 378 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 379 | if (mac802154_set_header_security(sdata, &hdr, cb) < 0) |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 382 | if (!saddr) { |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 383 | spin_lock_bh(&sdata->mib_lock); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 384 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 385 | if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) || |
| 386 | wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) || |
| 387 | wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 388 | hdr.source.mode = IEEE802154_ADDR_LONG; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 389 | hdr.source.extended_addr = wpan_dev->extended_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 390 | } else { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 391 | hdr.source.mode = IEEE802154_ADDR_SHORT; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 392 | hdr.source.short_addr = wpan_dev->short_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 393 | } |
| 394 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 395 | hdr.source.pan_id = wpan_dev->pan_id; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 396 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 397 | spin_unlock_bh(&sdata->mib_lock); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 398 | } else { |
| 399 | hdr.source = *(const struct ieee802154_addr *)saddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 402 | hdr.dest = *(const struct ieee802154_addr *)daddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 403 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 404 | hlen = ieee802154_hdr_push(skb, &hdr); |
| 405 | if (hlen < 0) |
| 406 | return -EINVAL; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 407 | |
Alexander Aring | 3e69162 | 2013-10-30 09:18:22 +0100 | [diff] [blame] | 408 | skb_reset_mac_header(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 409 | skb->mac_len = hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 410 | |
Phoebe Buckheister | 8c84296 | 2014-05-14 17:43:07 +0200 | [diff] [blame] | 411 | if (len > ieee802154_max_payload(&hdr)) |
Phoebe Buckheister | d1d7358 | 2014-03-14 21:24:04 +0100 | [diff] [blame] | 412 | return -EMSGSIZE; |
| 413 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 414 | return hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | static int |
| 418 | mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
| 419 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 420 | struct ieee802154_hdr hdr; |
| 421 | struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 422 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 423 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) { |
| 424 | pr_debug("malformed packet\n"); |
| 425 | return 0; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 428 | *addr = hdr.source; |
| 429 | return sizeof(*addr); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 430 | } |
| 431 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 432 | static struct header_ops mac802154_header_ops = { |
| 433 | .create = mac802154_header_create, |
| 434 | .parse = mac802154_header_parse, |
| 435 | }; |
| 436 | |
| 437 | static const struct net_device_ops mac802154_wpan_ops = { |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 438 | .ndo_open = mac802154_wpan_open, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 439 | .ndo_stop = mac802154_slave_close, |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 440 | .ndo_start_xmit = ieee802154_subif_start_xmit, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 441 | .ndo_do_ioctl = mac802154_wpan_ioctl, |
| 442 | .ndo_set_mac_address = mac802154_wpan_mac_addr, |
| 443 | }; |
| 444 | |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 445 | static const struct net_device_ops mac802154_monitor_ops = { |
Alexander Aring | 38130c3 | 2014-10-29 21:34:44 +0100 | [diff] [blame] | 446 | .ndo_open = mac802154_wpan_open, |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 447 | .ndo_stop = mac802154_slave_close, |
| 448 | .ndo_start_xmit = ieee802154_monitor_start_xmit, |
| 449 | }; |
| 450 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 451 | static void mac802154_wpan_free(struct net_device *dev) |
| 452 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 453 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 454 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 455 | mac802154_llsec_destroy(&sdata->sec); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 456 | |
| 457 | free_netdev(dev); |
| 458 | } |
| 459 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 460 | static void ieee802154_if_setup(struct net_device *dev) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 461 | { |
Alexander Aring | e57a894 | 2014-11-05 20:51:28 +0100 | [diff] [blame] | 462 | dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN; |
| 463 | memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 464 | |
| 465 | dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN; |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 466 | dev->needed_tailroom = 2 + 16; /* FCS + MIC */ |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 467 | dev->mtu = IEEE802154_MTU; |
Alan Ott | e937f583 | 2013-04-03 04:00:57 +0000 | [diff] [blame] | 468 | dev->tx_queue_len = 300; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 469 | dev->flags = IFF_NOARP | IFF_BROADCAST; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 470 | } |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 471 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 472 | static int |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 473 | ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata, |
| 474 | enum nl802154_iftype type) |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 475 | { |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 476 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
| 477 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 478 | /* set some type-dependent values */ |
Alexander Aring | 7c118c1 | 2014-11-05 20:51:20 +0100 | [diff] [blame] | 479 | sdata->vif.type = type; |
Alexander Aring | 190ac1c | 2014-11-09 08:36:49 +0100 | [diff] [blame] | 480 | sdata->wpan_dev.iftype = type; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 481 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 482 | get_random_bytes(&wpan_dev->bsn, 1); |
| 483 | get_random_bytes(&wpan_dev->dsn, 1); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 484 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 485 | /* defaults per 802.15.4-2011 */ |
Alexander Aring | 5fb3f02 | 2014-11-09 08:36:46 +0100 | [diff] [blame] | 486 | wpan_dev->min_be = 3; |
| 487 | wpan_dev->max_be = 5; |
| 488 | wpan_dev->csma_retries = 4; |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 489 | /* for compatibility, actual default is 3 */ |
Alexander Aring | 5fb3f02 | 2014-11-09 08:36:46 +0100 | [diff] [blame] | 490 | wpan_dev->frame_retries = -1; |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 491 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 492 | wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); |
| 493 | wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 494 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 495 | switch (type) { |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 496 | case NL802154_IFTYPE_NODE: |
Alexander Aring | b03c9cc | 2014-11-09 08:36:56 +0100 | [diff] [blame] | 497 | ieee802154_be64_to_le64(&wpan_dev->extended_addr, |
| 498 | sdata->dev->dev_addr); |
| 499 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 500 | sdata->dev->header_ops = &mac802154_header_ops; |
| 501 | sdata->dev->destructor = mac802154_wpan_free; |
| 502 | sdata->dev->netdev_ops = &mac802154_wpan_ops; |
| 503 | sdata->dev->ml_priv = &mac802154_mlme_wpan; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 504 | wpan_dev->promiscuous_mode = false; |
Alexander Aring | 38130c3 | 2014-10-29 21:34:44 +0100 | [diff] [blame] | 505 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 506 | spin_lock_init(&sdata->mib_lock); |
| 507 | mutex_init(&sdata->sec_mtx); |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 508 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 509 | mac802154_llsec_init(&sdata->sec); |
| 510 | break; |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 511 | case NL802154_IFTYPE_MONITOR: |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 512 | sdata->dev->destructor = free_netdev; |
| 513 | sdata->dev->netdev_ops = &mac802154_monitor_ops; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 514 | wpan_dev->promiscuous_mode = true; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 515 | break; |
| 516 | default: |
| 517 | BUG(); |
| 518 | } |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 519 | |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | struct net_device * |
| 524 | ieee802154_if_add(struct ieee802154_local *local, const char *name, |
Varka Bhadram | 5b4a1039 | 2015-04-30 17:44:57 +0200 | [diff] [blame] | 525 | unsigned char name_assign_type, enum nl802154_iftype type, |
| 526 | __le64 extended_addr) |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 527 | { |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 528 | struct net_device *ndev = NULL; |
| 529 | struct ieee802154_sub_if_data *sdata = NULL; |
| 530 | int ret = -ENOMEM; |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 531 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 532 | ASSERT_RTNL(); |
| 533 | |
Alexander Aring | 7c118c1 | 2014-11-05 20:51:20 +0100 | [diff] [blame] | 534 | ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size, name, |
Varka Bhadram | 5b4a1039 | 2015-04-30 17:44:57 +0200 | [diff] [blame] | 535 | name_assign_type, ieee802154_if_setup); |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 536 | if (!ndev) |
| 537 | return ERR_PTR(-ENOMEM); |
| 538 | |
| 539 | ndev->needed_headroom = local->hw.extra_tx_headroom; |
| 540 | |
| 541 | ret = dev_alloc_name(ndev, ndev->name); |
| 542 | if (ret < 0) |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 543 | goto err; |
| 544 | |
Alexander Aring | 0e57547 | 2014-11-17 08:20:52 +0100 | [diff] [blame] | 545 | ieee802154_le64_to_be64(ndev->perm_addr, |
| 546 | &local->hw.phy->perm_extended_addr); |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 547 | switch (type) { |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 548 | case NL802154_IFTYPE_NODE: |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 549 | ndev->type = ARPHRD_IEEE802154; |
Alexander Aring | 0e57547 | 2014-11-17 08:20:52 +0100 | [diff] [blame] | 550 | if (ieee802154_is_valid_extended_addr(extended_addr)) |
| 551 | ieee802154_le64_to_be64(ndev->dev_addr, &extended_addr); |
| 552 | else |
| 553 | memcpy(ndev->dev_addr, ndev->perm_addr, |
| 554 | IEEE802154_EXTENDED_ADDR_LEN); |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 555 | break; |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 556 | case NL802154_IFTYPE_MONITOR: |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 557 | ndev->type = ARPHRD_IEEE802154_MONITOR; |
| 558 | break; |
| 559 | default: |
| 560 | ret = -EINVAL; |
| 561 | goto err; |
| 562 | } |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 563 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 564 | /* TODO check this */ |
| 565 | SET_NETDEV_DEV(ndev, &local->phy->dev); |
| 566 | sdata = netdev_priv(ndev); |
| 567 | ndev->ieee802154_ptr = &sdata->wpan_dev; |
| 568 | memcpy(sdata->name, ndev->name, IFNAMSIZ); |
| 569 | sdata->dev = ndev; |
| 570 | sdata->wpan_dev.wpan_phy = local->hw.phy; |
| 571 | sdata->local = local; |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 572 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 573 | /* setup type-dependent data */ |
| 574 | ret = ieee802154_setup_sdata(sdata, type); |
| 575 | if (ret) |
| 576 | goto err; |
| 577 | |
Varka Bhadram | 473f376 | 2014-11-25 16:34:43 +0530 | [diff] [blame] | 578 | ret = register_netdevice(ndev); |
| 579 | if (ret < 0) |
| 580 | goto err; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 581 | |
| 582 | mutex_lock(&local->iflist_mtx); |
| 583 | list_add_tail_rcu(&sdata->list, &local->interfaces); |
| 584 | mutex_unlock(&local->iflist_mtx); |
| 585 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 586 | return ndev; |
| 587 | |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 588 | err: |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 589 | free_netdev(ndev); |
| 590 | return ERR_PTR(ret); |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Alexander Aring | b210b18 | 2014-11-05 20:51:14 +0100 | [diff] [blame] | 593 | void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata) |
| 594 | { |
| 595 | ASSERT_RTNL(); |
| 596 | |
| 597 | mutex_lock(&sdata->local->iflist_mtx); |
| 598 | list_del_rcu(&sdata->list); |
| 599 | mutex_unlock(&sdata->local->iflist_mtx); |
| 600 | |
| 601 | synchronize_rcu(); |
| 602 | unregister_netdevice(sdata->dev); |
| 603 | } |
Alexander Aring | 592dfbf | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 604 | |
| 605 | void ieee802154_remove_interfaces(struct ieee802154_local *local) |
| 606 | { |
Alexander Aring | d14e1c7 | 2014-11-12 03:36:49 +0100 | [diff] [blame] | 607 | struct ieee802154_sub_if_data *sdata, *tmp; |
Alexander Aring | 592dfbf | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 608 | |
Alexander Aring | 2789e62 | 2014-11-12 03:36:50 +0100 | [diff] [blame] | 609 | mutex_lock(&local->iflist_mtx); |
Alexander Aring | d14e1c7 | 2014-11-12 03:36:49 +0100 | [diff] [blame] | 610 | list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) { |
Alexander Aring | 592dfbf | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 611 | list_del(&sdata->list); |
Alexander Aring | 592dfbf | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 612 | |
| 613 | unregister_netdevice(sdata->dev); |
| 614 | } |
Alexander Aring | 2789e62 | 2014-11-12 03:36:50 +0100 | [diff] [blame] | 615 | mutex_unlock(&local->iflist_mtx); |
Alexander Aring | 592dfbf | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 616 | } |
Alexander Aring | be4fd8e | 2014-11-12 03:36:53 +0100 | [diff] [blame] | 617 | |
| 618 | static int netdev_notify(struct notifier_block *nb, |
| 619 | unsigned long state, void *ptr) |
| 620 | { |
| 621 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 622 | struct ieee802154_sub_if_data *sdata; |
| 623 | |
| 624 | if (state != NETDEV_CHANGENAME) |
| 625 | return NOTIFY_DONE; |
| 626 | |
| 627 | if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy) |
| 628 | return NOTIFY_DONE; |
| 629 | |
| 630 | if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid) |
| 631 | return NOTIFY_DONE; |
| 632 | |
| 633 | sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 634 | memcpy(sdata->name, dev->name, IFNAMSIZ); |
| 635 | |
| 636 | return NOTIFY_OK; |
| 637 | } |
| 638 | |
| 639 | static struct notifier_block mac802154_netdev_notifier = { |
| 640 | .notifier_call = netdev_notify, |
| 641 | }; |
| 642 | |
| 643 | int ieee802154_iface_init(void) |
| 644 | { |
| 645 | return register_netdevice_notifier(&mac802154_netdev_notifier); |
| 646 | } |
| 647 | |
| 648 | void ieee802154_iface_exit(void) |
| 649 | { |
| 650 | unregister_netdevice_notifier(&mac802154_netdev_notifier); |
| 651 | } |