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