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