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