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 | |
Alexander Aring | d77b485 | 2015-06-21 16:45:20 +0200 | [diff] [blame] | 33 | int mac802154_wpan_update_llsec(struct net_device *dev) |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 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 | 09095fd | 2015-08-10 21:15:54 +0200 | [diff] [blame] | 128 | /* lowpan need to be down for update |
| 129 | * SLAAC address after ifup |
| 130 | */ |
| 131 | if (sdata->wpan_dev.lowpan_dev) { |
| 132 | if (netif_running(sdata->wpan_dev.lowpan_dev)) |
| 133 | return -EBUSY; |
| 134 | } |
| 135 | |
Alexander Aring | 705cbbb | 2014-11-05 20:51:24 +0100 | [diff] [blame] | 136 | ieee802154_be64_to_le64(&extended_addr, addr->sa_data); |
Lennert Buytenhek | daf4e2c | 2015-05-28 15:38:43 +0300 | [diff] [blame] | 137 | if (!ieee802154_is_valid_extended_unicast_addr(extended_addr)) |
Alexander Aring | ea7053c | 2014-11-02 04:18:46 +0100 | [diff] [blame] | 138 | return -EINVAL; |
| 139 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 140 | memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 141 | sdata->wpan_dev.extended_addr = extended_addr; |
Alexander Aring | 776e59d | 2014-11-02 04:18:41 +0100 | [diff] [blame] | 142 | |
Alexander Aring | 09095fd | 2015-08-10 21:15:54 +0200 | [diff] [blame] | 143 | /* update lowpan interface mac address when |
| 144 | * wpan mac has been changed |
| 145 | */ |
| 146 | if (sdata->wpan_dev.lowpan_dev) |
| 147 | memcpy(sdata->wpan_dev.lowpan_dev->dev_addr, dev->dev_addr, |
| 148 | dev->addr_len); |
| 149 | |
Phoebe Buckheister | 9b0bb4a | 2014-05-16 17:46:43 +0200 | [diff] [blame] | 150 | return mac802154_wpan_update_llsec(dev); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Alexander Aring | 95c0aa1 | 2015-06-17 15:35:17 +0200 | [diff] [blame] | 153 | static int ieee802154_setup_hw(struct ieee802154_sub_if_data *sdata) |
| 154 | { |
| 155 | struct ieee802154_local *local = sdata->local; |
| 156 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
| 157 | int ret; |
| 158 | |
| 159 | if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) { |
| 160 | ret = drv_set_promiscuous_mode(local, |
| 161 | wpan_dev->promiscuous_mode); |
| 162 | if (ret < 0) |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | if (local->hw.flags & IEEE802154_HW_AFILT) { |
| 167 | ret = drv_set_pan_id(local, wpan_dev->pan_id); |
| 168 | if (ret < 0) |
| 169 | return ret; |
| 170 | |
| 171 | ret = drv_set_extended_addr(local, wpan_dev->extended_addr); |
| 172 | if (ret < 0) |
| 173 | return ret; |
| 174 | |
| 175 | ret = drv_set_short_addr(local, wpan_dev->short_addr); |
| 176 | if (ret < 0) |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | if (local->hw.flags & IEEE802154_HW_LBT) { |
| 181 | ret = drv_set_lbt_mode(local, wpan_dev->lbt); |
| 182 | if (ret < 0) |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) { |
| 187 | ret = drv_set_csma_params(local, wpan_dev->min_be, |
| 188 | wpan_dev->max_be, |
| 189 | wpan_dev->csma_retries); |
| 190 | if (ret < 0) |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) { |
| 195 | ret = drv_set_max_frame_retries(local, wpan_dev->frame_retries); |
| 196 | if (ret < 0) |
| 197 | return ret; |
| 198 | } |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 203 | static int mac802154_slave_open(struct net_device *dev) |
| 204 | { |
| 205 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 206 | struct ieee802154_local *local = sdata->local; |
Alexander Aring | 5c698e8 | 2015-06-17 15:35:18 +0200 | [diff] [blame] | 207 | int res; |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 208 | |
| 209 | ASSERT_RTNL(); |
| 210 | |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 211 | set_bit(SDATA_STATE_RUNNING, &sdata->state); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 212 | |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 213 | if (!local->open_count) { |
Alexander Aring | 95c0aa1 | 2015-06-17 15:35:17 +0200 | [diff] [blame] | 214 | res = ieee802154_setup_hw(sdata); |
| 215 | if (res) |
| 216 | goto err; |
| 217 | |
Alexander Aring | 59cb300 | 2014-10-28 18:21:21 +0100 | [diff] [blame] | 218 | res = drv_start(local); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 219 | if (res) |
| 220 | goto err; |
| 221 | } |
| 222 | |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 223 | local->open_count++; |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 224 | netif_start_queue(dev); |
| 225 | return 0; |
| 226 | err: |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 227 | /* might already be clear but that doesn't matter */ |
| 228 | clear_bit(SDATA_STATE_RUNNING, &sdata->state); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 229 | |
| 230 | return res; |
| 231 | } |
| 232 | |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 233 | static int |
| 234 | ieee802154_check_mac_settings(struct ieee802154_local *local, |
| 235 | struct wpan_dev *wpan_dev, |
| 236 | struct wpan_dev *nwpan_dev) |
| 237 | { |
| 238 | ASSERT_RTNL(); |
| 239 | |
| 240 | if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) { |
| 241 | if (wpan_dev->promiscuous_mode != nwpan_dev->promiscuous_mode) |
| 242 | return -EBUSY; |
| 243 | } |
| 244 | |
| 245 | if (local->hw.flags & IEEE802154_HW_AFILT) { |
Alexander Aring | 8bf9538 | 2015-03-26 12:46:28 +0100 | [diff] [blame] | 246 | if (wpan_dev->pan_id != nwpan_dev->pan_id || |
| 247 | wpan_dev->short_addr != nwpan_dev->short_addr || |
| 248 | wpan_dev->extended_addr != nwpan_dev->extended_addr) |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 249 | return -EBUSY; |
| 250 | } |
| 251 | |
| 252 | if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) { |
Alexander Aring | 8bf9538 | 2015-03-26 12:46:28 +0100 | [diff] [blame] | 253 | if (wpan_dev->min_be != nwpan_dev->min_be || |
| 254 | wpan_dev->max_be != nwpan_dev->max_be || |
| 255 | wpan_dev->csma_retries != nwpan_dev->csma_retries) |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 256 | return -EBUSY; |
| 257 | } |
| 258 | |
| 259 | if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) { |
| 260 | if (wpan_dev->frame_retries != nwpan_dev->frame_retries) |
| 261 | return -EBUSY; |
| 262 | } |
| 263 | |
| 264 | if (local->hw.flags & IEEE802154_HW_LBT) { |
| 265 | if (wpan_dev->lbt != nwpan_dev->lbt) |
| 266 | return -EBUSY; |
| 267 | } |
| 268 | |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | static int |
| 273 | ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata, |
| 274 | enum nl802154_iftype iftype) |
| 275 | { |
| 276 | struct ieee802154_local *local = sdata->local; |
| 277 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
| 278 | struct ieee802154_sub_if_data *nsdata; |
| 279 | |
| 280 | /* we hold the RTNL here so can safely walk the list */ |
| 281 | list_for_each_entry(nsdata, &local->interfaces, list) { |
| 282 | if (nsdata != sdata && ieee802154_sdata_running(nsdata)) { |
| 283 | int ret; |
| 284 | |
Alexander Aring | cd1c566 | 2014-12-19 23:45:59 +0100 | [diff] [blame] | 285 | /* TODO currently we don't support multiple node types |
| 286 | * we need to run skb_clone at rx path. Check if there |
| 287 | * exist really an use case if we need to support |
| 288 | * multiple node types at the same time. |
| 289 | */ |
Alexander Aring | ed65963b | 2015-06-06 17:30:46 +0200 | [diff] [blame] | 290 | if (wpan_dev->iftype == NL802154_IFTYPE_NODE && |
| 291 | nsdata->wpan_dev.iftype == NL802154_IFTYPE_NODE) |
Alexander Aring | cd1c566 | 2014-12-19 23:45:59 +0100 | [diff] [blame] | 292 | return -EBUSY; |
| 293 | |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 294 | /* check all phy mac sublayer settings are the same. |
| 295 | * We have only one phy, different values makes trouble. |
| 296 | */ |
| 297 | ret = ieee802154_check_mac_settings(local, wpan_dev, |
| 298 | &nsdata->wpan_dev); |
| 299 | if (ret < 0) |
| 300 | return ret; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Phoebe Buckheister | 6ef0023 | 2014-05-14 17:43:11 +0200 | [diff] [blame] | 307 | static int mac802154_wpan_open(struct net_device *dev) |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 308 | { |
| 309 | int rc; |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 310 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 311 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 312 | |
Alexander Aring | ed65963b | 2015-06-06 17:30:46 +0200 | [diff] [blame] | 313 | rc = ieee802154_check_concurrent_iface(sdata, wpan_dev->iftype); |
Alexander Aring | bd37a78 | 2014-12-19 23:45:58 +0100 | [diff] [blame] | 314 | if (rc < 0) |
| 315 | return rc; |
| 316 | |
Alexander Aring | 95c0aa1 | 2015-06-17 15:35:17 +0200 | [diff] [blame] | 317 | return mac802154_slave_open(dev); |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 318 | } |
| 319 | |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 320 | static int mac802154_slave_close(struct net_device *dev) |
| 321 | { |
| 322 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 323 | struct ieee802154_local *local = sdata->local; |
| 324 | |
| 325 | ASSERT_RTNL(); |
| 326 | |
| 327 | netif_stop_queue(dev); |
Alexander Aring | 7445764 | 2014-10-28 18:21:31 +0100 | [diff] [blame] | 328 | local->open_count--; |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 329 | |
Alexander Aring | 0ea3da6 | 2014-10-28 18:21:24 +0100 | [diff] [blame] | 330 | clear_bit(SDATA_STATE_RUNNING, &sdata->state); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 331 | |
Alexander Aring | c4227c8 | 2015-06-24 11:36:34 +0200 | [diff] [blame] | 332 | if (!local->open_count) |
| 333 | ieee802154_stop_device(local); |
Alexander Aring | 19ec690 | 2014-10-28 18:21:17 +0100 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 338 | static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata, |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 339 | struct ieee802154_hdr *hdr, |
| 340 | const struct ieee802154_mac_cb *cb) |
| 341 | { |
| 342 | struct ieee802154_llsec_params params; |
| 343 | u8 level; |
| 344 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 345 | mac802154_llsec_get_params(&sdata->sec, ¶ms); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 346 | |
| 347 | if (!params.enabled && cb->secen_override && cb->secen) |
| 348 | return -EINVAL; |
| 349 | if (!params.enabled || |
| 350 | (cb->secen_override && !cb->secen) || |
| 351 | !params.out_level) |
| 352 | return 0; |
| 353 | if (cb->seclevel_override && !cb->seclevel) |
| 354 | return -EINVAL; |
| 355 | |
| 356 | level = cb->seclevel_override ? cb->seclevel : params.out_level; |
| 357 | |
| 358 | hdr->fc.security_enabled = 1; |
| 359 | hdr->sec.level = level; |
| 360 | hdr->sec.key_id_mode = params.out_key.mode; |
| 361 | if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX) |
| 362 | hdr->sec.short_src = params.out_key.short_source; |
| 363 | else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX) |
| 364 | hdr->sec.extended_src = params.out_key.extended_source; |
| 365 | hdr->sec.key_id = params.out_key.id; |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | |
Alexander Aring | 838b83d | 2015-09-18 11:30:42 +0200 | [diff] [blame] | 370 | static int ieee802154_header_create(struct sk_buff *skb, |
| 371 | struct net_device *dev, |
| 372 | const struct ieee802154_addr *daddr, |
| 373 | const struct ieee802154_addr *saddr, |
| 374 | unsigned len) |
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 | struct ieee802154_hdr hdr; |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 377 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 378 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 379 | struct ieee802154_mac_cb *cb = mac_cb(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 380 | int hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 381 | |
| 382 | if (!daddr) |
| 383 | return -EINVAL; |
| 384 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 385 | memset(&hdr.fc, 0, sizeof(hdr.fc)); |
Phoebe Buckheister | 32edc40 | 2014-05-14 17:43:08 +0200 | [diff] [blame] | 386 | hdr.fc.type = cb->type; |
| 387 | hdr.fc.security_enabled = cb->secen; |
| 388 | hdr.fc.ack_request = cb->ackreq; |
Alexander Aring | 344f8c1 | 2015-05-22 17:43:53 +0200 | [diff] [blame] | 389 | 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] | 390 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 391 | if (mac802154_set_header_security(sdata, &hdr, cb) < 0) |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 392 | return -EINVAL; |
| 393 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 394 | if (!saddr) { |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 395 | if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) || |
| 396 | wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) || |
| 397 | wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 398 | hdr.source.mode = IEEE802154_ADDR_LONG; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 399 | hdr.source.extended_addr = wpan_dev->extended_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 400 | } else { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 401 | hdr.source.mode = IEEE802154_ADDR_SHORT; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 402 | hdr.source.short_addr = wpan_dev->short_addr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 405 | hdr.source.pan_id = wpan_dev->pan_id; |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 406 | } else { |
| 407 | hdr.source = *(const struct ieee802154_addr *)saddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 410 | hdr.dest = *(const struct ieee802154_addr *)daddr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 411 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 412 | hlen = ieee802154_hdr_push(skb, &hdr); |
| 413 | if (hlen < 0) |
| 414 | return -EINVAL; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 415 | |
Alexander Aring | 3e69162 | 2013-10-30 09:18:22 +0100 | [diff] [blame] | 416 | skb_reset_mac_header(skb); |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 417 | skb->mac_len = hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 418 | |
Phoebe Buckheister | 8c84296 | 2014-05-14 17:43:07 +0200 | [diff] [blame] | 419 | if (len > ieee802154_max_payload(&hdr)) |
Phoebe Buckheister | d1d7358 | 2014-03-14 21:24:04 +0100 | [diff] [blame] | 420 | return -EMSGSIZE; |
| 421 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 422 | return hlen; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 423 | } |
| 424 | |
Alexander Aring | 838b83d | 2015-09-18 11:30:42 +0200 | [diff] [blame] | 425 | static const struct wpan_dev_header_ops ieee802154_header_ops = { |
| 426 | .create = ieee802154_header_create, |
| 427 | }; |
| 428 | |
| 429 | /* This header create functionality assumes a 8 byte array for |
| 430 | * source and destination pointer at maximum. To adapt this for |
| 431 | * the 802.15.4 dataframe header we use extended address handling |
| 432 | * here only and intra pan connection. fc fields are mostly fallback |
| 433 | * handling. For provide dev_hard_header for dgram sockets. |
| 434 | */ |
| 435 | static int mac802154_header_create(struct sk_buff *skb, |
| 436 | struct net_device *dev, |
| 437 | unsigned short type, |
| 438 | const void *daddr, |
| 439 | const void *saddr, |
| 440 | unsigned len) |
| 441 | { |
| 442 | struct ieee802154_hdr hdr; |
| 443 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 444 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
| 445 | struct ieee802154_mac_cb cb = { }; |
| 446 | int hlen; |
| 447 | |
| 448 | if (!daddr) |
| 449 | return -EINVAL; |
| 450 | |
| 451 | memset(&hdr.fc, 0, sizeof(hdr.fc)); |
| 452 | hdr.fc.type = IEEE802154_FC_TYPE_DATA; |
| 453 | hdr.fc.ack_request = wpan_dev->ackreq; |
| 454 | hdr.seq = atomic_inc_return(&dev->ieee802154_ptr->dsn) & 0xFF; |
| 455 | |
| 456 | /* TODO currently a workaround to give zero cb block to set |
| 457 | * security parameters defaults according MIB. |
| 458 | */ |
| 459 | if (mac802154_set_header_security(sdata, &hdr, &cb) < 0) |
| 460 | return -EINVAL; |
| 461 | |
| 462 | hdr.dest.pan_id = wpan_dev->pan_id; |
| 463 | hdr.dest.mode = IEEE802154_ADDR_LONG; |
Alexander Aring | c2d5ecf | 2015-09-24 09:37:11 +0200 | [diff] [blame] | 464 | ieee802154_be64_to_le64(&hdr.dest.extended_addr, daddr); |
Alexander Aring | 838b83d | 2015-09-18 11:30:42 +0200 | [diff] [blame] | 465 | |
| 466 | hdr.source.pan_id = hdr.dest.pan_id; |
| 467 | hdr.source.mode = IEEE802154_ADDR_LONG; |
| 468 | |
| 469 | if (!saddr) |
| 470 | hdr.source.extended_addr = wpan_dev->extended_addr; |
| 471 | else |
Alexander Aring | c2d5ecf | 2015-09-24 09:37:11 +0200 | [diff] [blame] | 472 | ieee802154_be64_to_le64(&hdr.source.extended_addr, saddr); |
Alexander Aring | 838b83d | 2015-09-18 11:30:42 +0200 | [diff] [blame] | 473 | |
| 474 | hlen = ieee802154_hdr_push(skb, &hdr); |
| 475 | if (hlen < 0) |
| 476 | return -EINVAL; |
| 477 | |
| 478 | skb_reset_mac_header(skb); |
| 479 | skb->mac_len = hlen; |
| 480 | |
| 481 | if (len > ieee802154_max_payload(&hdr)) |
| 482 | return -EMSGSIZE; |
| 483 | |
| 484 | return hlen; |
| 485 | } |
| 486 | |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 487 | static int |
| 488 | mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr) |
| 489 | { |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 490 | struct ieee802154_hdr hdr; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 491 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 492 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) { |
| 493 | pr_debug("malformed packet\n"); |
| 494 | return 0; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Alexander Aring | 838b83d | 2015-09-18 11:30:42 +0200 | [diff] [blame] | 497 | if (hdr.source.mode == IEEE802154_ADDR_LONG) { |
Alexander Aring | c2d5ecf | 2015-09-24 09:37:11 +0200 | [diff] [blame] | 498 | ieee802154_le64_to_be64(haddr, &hdr.source.extended_addr); |
Alexander Aring | 838b83d | 2015-09-18 11:30:42 +0200 | [diff] [blame] | 499 | return IEEE802154_EXTENDED_ADDR_LEN; |
| 500 | } |
| 501 | |
| 502 | return 0; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Alexander Aring | 838b83d | 2015-09-18 11:30:42 +0200 | [diff] [blame] | 505 | static const struct header_ops mac802154_header_ops = { |
| 506 | .create = mac802154_header_create, |
| 507 | .parse = mac802154_header_parse, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 508 | }; |
| 509 | |
| 510 | static const struct net_device_ops mac802154_wpan_ops = { |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 511 | .ndo_open = mac802154_wpan_open, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 512 | .ndo_stop = mac802154_slave_close, |
Alexander Aring | e5e584f | 2014-10-26 09:37:13 +0100 | [diff] [blame] | 513 | .ndo_start_xmit = ieee802154_subif_start_xmit, |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 514 | .ndo_do_ioctl = mac802154_wpan_ioctl, |
| 515 | .ndo_set_mac_address = mac802154_wpan_mac_addr, |
| 516 | }; |
| 517 | |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 518 | static const struct net_device_ops mac802154_monitor_ops = { |
Alexander Aring | 38130c3 | 2014-10-29 21:34:44 +0100 | [diff] [blame] | 519 | .ndo_open = mac802154_wpan_open, |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 520 | .ndo_stop = mac802154_slave_close, |
| 521 | .ndo_start_xmit = ieee802154_monitor_start_xmit, |
| 522 | }; |
| 523 | |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 524 | static void mac802154_wpan_free(struct net_device *dev) |
| 525 | { |
Alexander Aring | 59d19cd | 2014-10-25 17:16:40 +0200 | [diff] [blame] | 526 | struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 527 | |
Alexander Aring | 036562f | 2014-10-25 17:16:36 +0200 | [diff] [blame] | 528 | mac802154_llsec_destroy(&sdata->sec); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 531 | static void ieee802154_if_setup(struct net_device *dev) |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 532 | { |
Alexander Aring | e57a894 | 2014-11-05 20:51:28 +0100 | [diff] [blame] | 533 | dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN; |
| 534 | memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 535 | |
Alexander Aring | 87a93e4 | 2015-09-18 11:30:43 +0200 | [diff] [blame] | 536 | /* Let hard_header_len set to IEEE802154_MIN_HEADER_LEN. AF_PACKET |
| 537 | * will not send frames without any payload, but ack frames |
| 538 | * has no payload, so substract one that we can send a 3 bytes |
| 539 | * frame. The xmit callback assumes at least a hard header where two |
| 540 | * bytes fc and sequence field are set. |
| 541 | */ |
| 542 | dev->hard_header_len = IEEE802154_MIN_HEADER_LEN - 1; |
| 543 | /* The auth_tag header is for security and places in private payload |
| 544 | * room of mac frame which stucks between payload and FCS field. |
| 545 | */ |
| 546 | dev->needed_tailroom = IEEE802154_MAX_AUTH_TAG_LEN + |
| 547 | IEEE802154_FCS_LEN; |
Alexander Aring | b40988c | 2015-09-28 12:36:26 +0200 | [diff] [blame] | 548 | /* The mtu size is the payload without mac header in this case. |
| 549 | * We have a dynamic length header with a minimum header length |
| 550 | * which is hard_header_len. In this case we let mtu to the size |
| 551 | * of maximum payload which is IEEE802154_MTU - IEEE802154_FCS_LEN - |
| 552 | * hard_header_len. The FCS which is set by hardware or ndo_start_xmit |
| 553 | * and the minimum mac header which can be evaluated inside driver |
| 554 | * layer. The rest of mac header will be part of payload if greater |
| 555 | * than hard_header_len. |
| 556 | */ |
| 557 | dev->mtu = IEEE802154_MTU - IEEE802154_FCS_LEN - |
| 558 | dev->hard_header_len; |
Alan Ott | e937f583 | 2013-04-03 04:00:57 +0000 | [diff] [blame] | 559 | dev->tx_queue_len = 300; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 560 | dev->flags = IFF_NOARP | IFF_BROADCAST; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 561 | } |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 562 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 563 | static int |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 564 | ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata, |
| 565 | enum nl802154_iftype type) |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 566 | { |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 567 | struct wpan_dev *wpan_dev = &sdata->wpan_dev; |
Alexander Aring | d77b485 | 2015-06-21 16:45:20 +0200 | [diff] [blame] | 568 | int ret; |
Alexander Aring | 344f8c1 | 2015-05-22 17:43:53 +0200 | [diff] [blame] | 569 | u8 tmp; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 570 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 571 | /* set some type-dependent values */ |
Alexander Aring | 190ac1c | 2014-11-09 08:36:49 +0100 | [diff] [blame] | 572 | sdata->wpan_dev.iftype = type; |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 573 | |
Alexander Aring | 344f8c1 | 2015-05-22 17:43:53 +0200 | [diff] [blame] | 574 | get_random_bytes(&tmp, sizeof(tmp)); |
| 575 | atomic_set(&wpan_dev->bsn, tmp); |
| 576 | get_random_bytes(&tmp, sizeof(tmp)); |
| 577 | atomic_set(&wpan_dev->dsn, tmp); |
alex.bluesman.smirnov@gmail.com | 32bad7e | 2012-06-25 23:24:48 +0000 | [diff] [blame] | 578 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 579 | /* defaults per 802.15.4-2011 */ |
Alexander Aring | 5fb3f02 | 2014-11-09 08:36:46 +0100 | [diff] [blame] | 580 | wpan_dev->min_be = 3; |
| 581 | wpan_dev->max_be = 5; |
| 582 | wpan_dev->csma_retries = 4; |
Alexander Aring | 89c7d78 | 2015-08-10 21:15:56 +0200 | [diff] [blame] | 583 | wpan_dev->frame_retries = 3; |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 584 | |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 585 | wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); |
| 586 | wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); |
Phoebe Buckheister | f30be4d | 2014-05-16 17:46:40 +0200 | [diff] [blame] | 587 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 588 | switch (type) { |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 589 | case NL802154_IFTYPE_NODE: |
Alexander Aring | b03c9cc | 2014-11-09 08:36:56 +0100 | [diff] [blame] | 590 | ieee802154_be64_to_le64(&wpan_dev->extended_addr, |
| 591 | sdata->dev->dev_addr); |
| 592 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 593 | sdata->dev->header_ops = &mac802154_header_ops; |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 594 | sdata->dev->needs_free_netdev = true; |
| 595 | sdata->dev->priv_destructor = mac802154_wpan_free; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 596 | sdata->dev->netdev_ops = &mac802154_wpan_ops; |
| 597 | sdata->dev->ml_priv = &mac802154_mlme_wpan; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 598 | wpan_dev->promiscuous_mode = false; |
Alexander Aring | 838b83d | 2015-09-18 11:30:42 +0200 | [diff] [blame] | 599 | wpan_dev->header_ops = &ieee802154_header_ops; |
Alexander Aring | 38130c3 | 2014-10-29 21:34:44 +0100 | [diff] [blame] | 600 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 601 | mutex_init(&sdata->sec_mtx); |
Alexander Aring | b9ff77e | 2014-10-28 18:21:16 +0100 | [diff] [blame] | 602 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 603 | mac802154_llsec_init(&sdata->sec); |
Alexander Aring | d77b485 | 2015-06-21 16:45:20 +0200 | [diff] [blame] | 604 | ret = mac802154_wpan_update_llsec(sdata->dev); |
| 605 | if (ret < 0) |
| 606 | return ret; |
| 607 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 608 | break; |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 609 | case NL802154_IFTYPE_MONITOR: |
David S. Miller | cf124db | 2017-05-08 12:52:56 -0400 | [diff] [blame] | 610 | sdata->dev->needs_free_netdev = true; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 611 | sdata->dev->netdev_ops = &mac802154_monitor_ops; |
Alexander Aring | 863e88f | 2014-11-09 08:36:45 +0100 | [diff] [blame] | 612 | wpan_dev->promiscuous_mode = true; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 613 | break; |
| 614 | default: |
| 615 | BUG(); |
| 616 | } |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 617 | |
| 618 | return 0; |
| 619 | } |
| 620 | |
| 621 | struct net_device * |
| 622 | ieee802154_if_add(struct ieee802154_local *local, const char *name, |
Varka Bhadram | 5b4a1039 | 2015-04-30 17:44:57 +0200 | [diff] [blame] | 623 | unsigned char name_assign_type, enum nl802154_iftype type, |
| 624 | __le64 extended_addr) |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 625 | { |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 626 | struct net_device *ndev = NULL; |
| 627 | struct ieee802154_sub_if_data *sdata = NULL; |
| 628 | int ret = -ENOMEM; |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 629 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 630 | ASSERT_RTNL(); |
| 631 | |
Alexander Aring | ed65963b | 2015-06-06 17:30:46 +0200 | [diff] [blame] | 632 | ndev = alloc_netdev(sizeof(*sdata), name, |
Varka Bhadram | 5b4a1039 | 2015-04-30 17:44:57 +0200 | [diff] [blame] | 633 | name_assign_type, ieee802154_if_setup); |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 634 | if (!ndev) |
| 635 | return ERR_PTR(-ENOMEM); |
| 636 | |
Alexander Aring | 87a93e4 | 2015-09-18 11:30:43 +0200 | [diff] [blame] | 637 | ndev->needed_headroom = local->hw.extra_tx_headroom + |
| 638 | IEEE802154_MAX_HEADER_LEN; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 639 | |
| 640 | ret = dev_alloc_name(ndev, ndev->name); |
| 641 | if (ret < 0) |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 642 | goto err; |
| 643 | |
Alexander Aring | 0e57547 | 2014-11-17 08:20:52 +0100 | [diff] [blame] | 644 | ieee802154_le64_to_be64(ndev->perm_addr, |
| 645 | &local->hw.phy->perm_extended_addr); |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 646 | switch (type) { |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 647 | case NL802154_IFTYPE_NODE: |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 648 | ndev->type = ARPHRD_IEEE802154; |
Lennert Buytenhek | daf4e2c | 2015-05-28 15:38:43 +0300 | [diff] [blame] | 649 | if (ieee802154_is_valid_extended_unicast_addr(extended_addr)) |
Alexander Aring | 0e57547 | 2014-11-17 08:20:52 +0100 | [diff] [blame] | 650 | ieee802154_le64_to_be64(ndev->dev_addr, &extended_addr); |
| 651 | else |
| 652 | memcpy(ndev->dev_addr, ndev->perm_addr, |
| 653 | IEEE802154_EXTENDED_ADDR_LEN); |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 654 | break; |
Alexander Aring | 944742a | 2014-11-17 08:20:49 +0100 | [diff] [blame] | 655 | case NL802154_IFTYPE_MONITOR: |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 656 | ndev->type = ARPHRD_IEEE802154_MONITOR; |
| 657 | break; |
| 658 | default: |
| 659 | ret = -EINVAL; |
| 660 | goto err; |
| 661 | } |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 662 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 663 | /* TODO check this */ |
| 664 | SET_NETDEV_DEV(ndev, &local->phy->dev); |
Alexander Aring | abbcc34 | 2016-07-24 16:12:24 +0200 | [diff] [blame] | 665 | dev_net_set(ndev, wpan_phy_net(local->hw.phy)); |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 666 | sdata = netdev_priv(ndev); |
| 667 | ndev->ieee802154_ptr = &sdata->wpan_dev; |
| 668 | memcpy(sdata->name, ndev->name, IFNAMSIZ); |
| 669 | sdata->dev = ndev; |
| 670 | sdata->wpan_dev.wpan_phy = local->hw.phy; |
| 671 | sdata->local = local; |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 672 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 673 | /* setup type-dependent data */ |
| 674 | ret = ieee802154_setup_sdata(sdata, type); |
| 675 | if (ret) |
| 676 | goto err; |
| 677 | |
Varka Bhadram | 473f376 | 2014-11-25 16:34:43 +0530 | [diff] [blame] | 678 | ret = register_netdevice(ndev); |
| 679 | if (ret < 0) |
| 680 | goto err; |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 681 | |
| 682 | mutex_lock(&local->iflist_mtx); |
| 683 | list_add_tail_rcu(&sdata->list, &local->interfaces); |
| 684 | mutex_unlock(&local->iflist_mtx); |
| 685 | |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 686 | return ndev; |
| 687 | |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 688 | err: |
Alexander Aring | d5ae67b | 2014-11-05 20:51:17 +0100 | [diff] [blame] | 689 | free_netdev(ndev); |
| 690 | return ERR_PTR(ret); |
Alexander Aring | 986a8ab | 2014-11-05 20:51:15 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Alexander Aring | b210b18 | 2014-11-05 20:51:14 +0100 | [diff] [blame] | 693 | void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata) |
| 694 | { |
| 695 | ASSERT_RTNL(); |
| 696 | |
| 697 | mutex_lock(&sdata->local->iflist_mtx); |
| 698 | list_del_rcu(&sdata->list); |
| 699 | mutex_unlock(&sdata->local->iflist_mtx); |
| 700 | |
| 701 | synchronize_rcu(); |
| 702 | unregister_netdevice(sdata->dev); |
| 703 | } |
Alexander Aring | 592dfbfc | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 704 | |
| 705 | void ieee802154_remove_interfaces(struct ieee802154_local *local) |
| 706 | { |
Alexander Aring | d14e1c7 | 2014-11-12 03:36:49 +0100 | [diff] [blame] | 707 | struct ieee802154_sub_if_data *sdata, *tmp; |
Alexander Aring | 592dfbfc | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 708 | |
Alexander Aring | 2789e62 | 2014-11-12 03:36:50 +0100 | [diff] [blame] | 709 | mutex_lock(&local->iflist_mtx); |
Alexander Aring | d14e1c7 | 2014-11-12 03:36:49 +0100 | [diff] [blame] | 710 | list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) { |
Alexander Aring | 592dfbfc | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 711 | list_del(&sdata->list); |
Alexander Aring | 592dfbfc | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 712 | |
| 713 | unregister_netdevice(sdata->dev); |
| 714 | } |
Alexander Aring | 2789e62 | 2014-11-12 03:36:50 +0100 | [diff] [blame] | 715 | mutex_unlock(&local->iflist_mtx); |
Alexander Aring | 592dfbfc | 2014-11-12 03:36:48 +0100 | [diff] [blame] | 716 | } |
Alexander Aring | be4fd8e | 2014-11-12 03:36:53 +0100 | [diff] [blame] | 717 | |
| 718 | static int netdev_notify(struct notifier_block *nb, |
| 719 | unsigned long state, void *ptr) |
| 720 | { |
| 721 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 722 | struct ieee802154_sub_if_data *sdata; |
| 723 | |
| 724 | if (state != NETDEV_CHANGENAME) |
| 725 | return NOTIFY_DONE; |
| 726 | |
| 727 | if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy) |
| 728 | return NOTIFY_DONE; |
| 729 | |
| 730 | if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid) |
| 731 | return NOTIFY_DONE; |
| 732 | |
| 733 | sdata = IEEE802154_DEV_TO_SUB_IF(dev); |
| 734 | memcpy(sdata->name, dev->name, IFNAMSIZ); |
| 735 | |
| 736 | return NOTIFY_OK; |
| 737 | } |
| 738 | |
| 739 | static struct notifier_block mac802154_netdev_notifier = { |
| 740 | .notifier_call = netdev_notify, |
| 741 | }; |
| 742 | |
| 743 | int ieee802154_iface_init(void) |
| 744 | { |
| 745 | return register_netdevice_notifier(&mac802154_netdev_notifier); |
| 746 | } |
| 747 | |
| 748 | void ieee802154_iface_exit(void) |
| 749 | { |
| 750 | unregister_netdevice_notifier(&mac802154_netdev_notifier); |
| 751 | } |