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