Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Netlink inteface for IEEE 802.15.4 stack |
| 3 | * |
| 4 | * Copyright 2007, 2008 Siemens AG |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License version 2 |
| 8 | * as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Written by: |
| 20 | * Sergey Lapin <slapin@ossfans.org> |
| 21 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> |
| 22 | * Maxim Osipov <maxim.osipov@siemens.com> |
| 23 | */ |
| 24 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/gfp.h> |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 26 | #include <linux/kernel.h> |
| 27 | #include <linux/if_arp.h> |
| 28 | #include <linux/netdevice.h> |
| 29 | #include <net/netlink.h> |
| 30 | #include <net/genetlink.h> |
| 31 | #include <net/sock.h> |
| 32 | #include <linux/nl802154.h> |
Paul Gortmaker | bc3b2d7 | 2011-07-15 11:47:34 -0400 | [diff] [blame] | 33 | #include <linux/export.h> |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 34 | #include <net/af_ieee802154.h> |
| 35 | #include <net/nl802154.h> |
| 36 | #include <net/ieee802154.h> |
| 37 | #include <net/ieee802154_netdev.h> |
Dmitry Eremin-Solenikov | 0a868b26 | 2009-10-29 16:28:52 +0300 | [diff] [blame] | 38 | #include <net/wpan-phy.h> |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 39 | |
| 40 | #include "ieee802154.h" |
| 41 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 42 | static int nla_put_hwaddr(struct sk_buff *msg, int type, __le64 hwaddr) |
| 43 | { |
| 44 | return nla_put_u64(msg, type, swab64((__force u64)hwaddr)); |
| 45 | } |
| 46 | |
| 47 | static __le64 nla_get_hwaddr(const struct nlattr *nla) |
| 48 | { |
| 49 | return ieee802154_devaddr_from_raw(nla_data(nla)); |
| 50 | } |
| 51 | |
| 52 | static int nla_put_shortaddr(struct sk_buff *msg, int type, __le16 addr) |
| 53 | { |
| 54 | return nla_put_u16(msg, type, le16_to_cpu(addr)); |
| 55 | } |
| 56 | |
| 57 | static __le16 nla_get_shortaddr(const struct nlattr *nla) |
| 58 | { |
| 59 | return cpu_to_le16(nla_get_u16(nla)); |
| 60 | } |
| 61 | |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 62 | int ieee802154_nl_assoc_indic(struct net_device *dev, |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 63 | struct ieee802154_addr *addr, u8 cap) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 64 | { |
| 65 | struct sk_buff *msg; |
| 66 | |
| 67 | pr_debug("%s\n", __func__); |
| 68 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 69 | if (addr->mode != IEEE802154_ADDR_LONG) { |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 70 | pr_err("%s: received non-long source address!\n", __func__); |
| 71 | return -EINVAL; |
| 72 | } |
| 73 | |
| 74 | msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_INDIC); |
| 75 | if (!msg) |
| 76 | return -ENOBUFS; |
| 77 | |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 78 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
| 79 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || |
| 80 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, |
| 81 | dev->dev_addr) || |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 82 | nla_put_hwaddr(msg, IEEE802154_ATTR_SRC_HW_ADDR, |
| 83 | addr->extended_addr) || |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 84 | nla_put_u8(msg, IEEE802154_ATTR_CAPABILITY, cap)) |
| 85 | goto nla_put_failure; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 86 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 87 | return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 88 | |
| 89 | nla_put_failure: |
| 90 | nlmsg_free(msg); |
| 91 | return -ENOBUFS; |
| 92 | } |
| 93 | EXPORT_SYMBOL(ieee802154_nl_assoc_indic); |
| 94 | |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 95 | int ieee802154_nl_assoc_confirm(struct net_device *dev, __le16 short_addr, |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 96 | u8 status) |
| 97 | { |
| 98 | struct sk_buff *msg; |
| 99 | |
| 100 | pr_debug("%s\n", __func__); |
| 101 | |
| 102 | msg = ieee802154_nl_create(0, IEEE802154_ASSOCIATE_CONF); |
| 103 | if (!msg) |
| 104 | return -ENOBUFS; |
| 105 | |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 106 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
| 107 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || |
| 108 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, |
| 109 | dev->dev_addr) || |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 110 | nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) || |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 111 | nla_put_u8(msg, IEEE802154_ATTR_STATUS, status)) |
| 112 | goto nla_put_failure; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 113 | return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 114 | |
| 115 | nla_put_failure: |
| 116 | nlmsg_free(msg); |
| 117 | return -ENOBUFS; |
| 118 | } |
| 119 | EXPORT_SYMBOL(ieee802154_nl_assoc_confirm); |
| 120 | |
| 121 | int ieee802154_nl_disassoc_indic(struct net_device *dev, |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 122 | struct ieee802154_addr *addr, u8 reason) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 123 | { |
| 124 | struct sk_buff *msg; |
| 125 | |
| 126 | pr_debug("%s\n", __func__); |
| 127 | |
| 128 | msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_INDIC); |
| 129 | if (!msg) |
| 130 | return -ENOBUFS; |
| 131 | |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 132 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
| 133 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || |
| 134 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, |
| 135 | dev->dev_addr)) |
| 136 | goto nla_put_failure; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 137 | if (addr->mode == IEEE802154_ADDR_LONG) { |
| 138 | if (nla_put_hwaddr(msg, IEEE802154_ATTR_SRC_HW_ADDR, |
| 139 | addr->extended_addr)) |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 140 | goto nla_put_failure; |
| 141 | } else { |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 142 | if (nla_put_shortaddr(msg, IEEE802154_ATTR_SRC_SHORT_ADDR, |
| 143 | addr->short_addr)) |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 144 | goto nla_put_failure; |
| 145 | } |
| 146 | if (nla_put_u8(msg, IEEE802154_ATTR_REASON, reason)) |
| 147 | goto nla_put_failure; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 148 | return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 149 | |
| 150 | nla_put_failure: |
| 151 | nlmsg_free(msg); |
| 152 | return -ENOBUFS; |
| 153 | } |
| 154 | EXPORT_SYMBOL(ieee802154_nl_disassoc_indic); |
| 155 | |
| 156 | int ieee802154_nl_disassoc_confirm(struct net_device *dev, u8 status) |
| 157 | { |
| 158 | struct sk_buff *msg; |
| 159 | |
| 160 | pr_debug("%s\n", __func__); |
| 161 | |
| 162 | msg = ieee802154_nl_create(0, IEEE802154_DISASSOCIATE_CONF); |
| 163 | if (!msg) |
| 164 | return -ENOBUFS; |
| 165 | |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 166 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
| 167 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || |
| 168 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, |
| 169 | dev->dev_addr) || |
| 170 | nla_put_u8(msg, IEEE802154_ATTR_STATUS, status)) |
| 171 | goto nla_put_failure; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 172 | return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 173 | |
| 174 | nla_put_failure: |
| 175 | nlmsg_free(msg); |
| 176 | return -ENOBUFS; |
| 177 | } |
| 178 | EXPORT_SYMBOL(ieee802154_nl_disassoc_confirm); |
| 179 | |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 180 | int ieee802154_nl_beacon_indic(struct net_device *dev, __le16 panid, |
| 181 | __le16 coord_addr) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 182 | { |
| 183 | struct sk_buff *msg; |
| 184 | |
| 185 | pr_debug("%s\n", __func__); |
| 186 | |
| 187 | msg = ieee802154_nl_create(0, IEEE802154_BEACON_NOTIFY_INDIC); |
| 188 | if (!msg) |
| 189 | return -ENOBUFS; |
| 190 | |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 191 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
| 192 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || |
| 193 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, |
| 194 | dev->dev_addr) || |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 195 | nla_put_shortaddr(msg, IEEE802154_ATTR_COORD_SHORT_ADDR, |
| 196 | coord_addr) || |
| 197 | nla_put_shortaddr(msg, IEEE802154_ATTR_COORD_PAN_ID, panid)) |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 198 | goto nla_put_failure; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 199 | return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 200 | |
| 201 | nla_put_failure: |
| 202 | nlmsg_free(msg); |
| 203 | return -ENOBUFS; |
| 204 | } |
| 205 | EXPORT_SYMBOL(ieee802154_nl_beacon_indic); |
| 206 | |
| 207 | int ieee802154_nl_scan_confirm(struct net_device *dev, |
| 208 | u8 status, u8 scan_type, u32 unscanned, u8 page, |
| 209 | u8 *edl/* , struct list_head *pan_desc_list */) |
| 210 | { |
| 211 | struct sk_buff *msg; |
| 212 | |
| 213 | pr_debug("%s\n", __func__); |
| 214 | |
| 215 | msg = ieee802154_nl_create(0, IEEE802154_SCAN_CONF); |
| 216 | if (!msg) |
| 217 | return -ENOBUFS; |
| 218 | |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 219 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
| 220 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || |
| 221 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, |
| 222 | dev->dev_addr) || |
| 223 | nla_put_u8(msg, IEEE802154_ATTR_STATUS, status) || |
| 224 | nla_put_u8(msg, IEEE802154_ATTR_SCAN_TYPE, scan_type) || |
| 225 | nla_put_u32(msg, IEEE802154_ATTR_CHANNELS, unscanned) || |
| 226 | nla_put_u8(msg, IEEE802154_ATTR_PAGE, page) || |
| 227 | (edl && |
| 228 | nla_put(msg, IEEE802154_ATTR_ED_LIST, 27, edl))) |
| 229 | goto nla_put_failure; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 230 | return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 231 | |
| 232 | nla_put_failure: |
| 233 | nlmsg_free(msg); |
| 234 | return -ENOBUFS; |
| 235 | } |
| 236 | EXPORT_SYMBOL(ieee802154_nl_scan_confirm); |
| 237 | |
| 238 | int ieee802154_nl_start_confirm(struct net_device *dev, u8 status) |
| 239 | { |
| 240 | struct sk_buff *msg; |
| 241 | |
| 242 | pr_debug("%s\n", __func__); |
| 243 | |
| 244 | msg = ieee802154_nl_create(0, IEEE802154_START_CONF); |
| 245 | if (!msg) |
| 246 | return -ENOBUFS; |
| 247 | |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 248 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
| 249 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || |
| 250 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, |
| 251 | dev->dev_addr) || |
| 252 | nla_put_u8(msg, IEEE802154_ATTR_STATUS, status)) |
| 253 | goto nla_put_failure; |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 254 | return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 255 | |
| 256 | nla_put_failure: |
| 257 | nlmsg_free(msg); |
| 258 | return -ENOBUFS; |
| 259 | } |
| 260 | EXPORT_SYMBOL(ieee802154_nl_start_confirm); |
| 261 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 262 | static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid, |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 263 | u32 seq, int flags, struct net_device *dev) |
| 264 | { |
| 265 | void *hdr; |
Dmitry Eremin-Solenikov | 0a868b26 | 2009-10-29 16:28:52 +0300 | [diff] [blame] | 266 | struct wpan_phy *phy; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 267 | __le16 short_addr, pan_id; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 268 | |
| 269 | pr_debug("%s\n", __func__); |
| 270 | |
| 271 | hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags, |
| 272 | IEEE802154_LIST_IFACE); |
| 273 | if (!hdr) |
| 274 | goto out; |
| 275 | |
Dmitry Eremin-Solenikov | 0a868b26 | 2009-10-29 16:28:52 +0300 | [diff] [blame] | 276 | phy = ieee802154_mlme_ops(dev)->get_phy(dev); |
| 277 | BUG_ON(!phy); |
| 278 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 279 | short_addr = ieee802154_mlme_ops(dev)->get_short_addr(dev); |
| 280 | pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 281 | |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 282 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
| 283 | nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || |
| 284 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || |
| 285 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, |
| 286 | dev->dev_addr) || |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 287 | nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) || |
| 288 | nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id)) |
David S. Miller | be51da0 | 2012-04-01 20:45:25 -0400 | [diff] [blame] | 289 | goto nla_put_failure; |
Dmitry Eremin-Solenikov | 0a868b26 | 2009-10-29 16:28:52 +0300 | [diff] [blame] | 290 | wpan_phy_put(phy); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 291 | return genlmsg_end(msg, hdr); |
| 292 | |
| 293 | nla_put_failure: |
Dmitry Eremin-Solenikov | 0a868b26 | 2009-10-29 16:28:52 +0300 | [diff] [blame] | 294 | wpan_phy_put(phy); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 295 | genlmsg_cancel(msg, hdr); |
| 296 | out: |
| 297 | return -EMSGSIZE; |
| 298 | } |
| 299 | |
| 300 | /* Requests from userspace */ |
| 301 | static struct net_device *ieee802154_nl_get_dev(struct genl_info *info) |
| 302 | { |
| 303 | struct net_device *dev; |
| 304 | |
| 305 | if (info->attrs[IEEE802154_ATTR_DEV_NAME]) { |
| 306 | char name[IFNAMSIZ + 1]; |
| 307 | nla_strlcpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME], |
| 308 | sizeof(name)); |
| 309 | dev = dev_get_by_name(&init_net, name); |
| 310 | } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) |
| 311 | dev = dev_get_by_index(&init_net, |
| 312 | nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX])); |
| 313 | else |
| 314 | return NULL; |
| 315 | |
| 316 | if (!dev) |
| 317 | return NULL; |
| 318 | |
| 319 | if (dev->type != ARPHRD_IEEE802154) { |
| 320 | dev_put(dev); |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | return dev; |
| 325 | } |
| 326 | |
Johannes Berg | 1c582d9 | 2013-11-14 17:14:41 +0100 | [diff] [blame] | 327 | int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 328 | { |
| 329 | struct net_device *dev; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 330 | struct ieee802154_addr addr; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 331 | u8 page; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 332 | int ret = -EOPNOTSUPP; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 333 | |
| 334 | if (!info->attrs[IEEE802154_ATTR_CHANNEL] || |
| 335 | !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] || |
| 336 | (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] && |
| 337 | !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) || |
| 338 | !info->attrs[IEEE802154_ATTR_CAPABILITY]) |
| 339 | return -EINVAL; |
| 340 | |
| 341 | dev = ieee802154_nl_get_dev(info); |
| 342 | if (!dev) |
| 343 | return -ENODEV; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 344 | if (!ieee802154_mlme_ops(dev)->assoc_req) |
| 345 | goto out; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 346 | |
| 347 | if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) { |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 348 | addr.mode = IEEE802154_ADDR_LONG; |
| 349 | addr.extended_addr = nla_get_hwaddr( |
| 350 | info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 351 | } else { |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 352 | addr.mode = IEEE802154_ADDR_SHORT; |
| 353 | addr.short_addr = nla_get_shortaddr( |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 354 | info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]); |
| 355 | } |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 356 | addr.pan_id = nla_get_shortaddr( |
| 357 | info->attrs[IEEE802154_ATTR_COORD_PAN_ID]); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 358 | |
| 359 | if (info->attrs[IEEE802154_ATTR_PAGE]) |
| 360 | page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]); |
| 361 | else |
| 362 | page = 0; |
| 363 | |
| 364 | ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr, |
| 365 | nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]), |
| 366 | page, |
| 367 | nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY])); |
| 368 | |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 369 | out: |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 370 | dev_put(dev); |
| 371 | return ret; |
| 372 | } |
| 373 | |
Johannes Berg | 1c582d9 | 2013-11-14 17:14:41 +0100 | [diff] [blame] | 374 | int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 375 | { |
| 376 | struct net_device *dev; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 377 | struct ieee802154_addr addr; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 378 | int ret = -EOPNOTSUPP; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 379 | |
| 380 | if (!info->attrs[IEEE802154_ATTR_STATUS] || |
| 381 | !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] || |
| 382 | !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) |
| 383 | return -EINVAL; |
| 384 | |
| 385 | dev = ieee802154_nl_get_dev(info); |
| 386 | if (!dev) |
| 387 | return -ENODEV; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 388 | if (!ieee802154_mlme_ops(dev)->assoc_resp) |
| 389 | goto out; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 390 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 391 | addr.mode = IEEE802154_ADDR_LONG; |
| 392 | addr.extended_addr = nla_get_hwaddr( |
| 393 | info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]); |
| 394 | addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 395 | |
| 396 | ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr, |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 397 | nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]), |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 398 | nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS])); |
| 399 | |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 400 | out: |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 401 | dev_put(dev); |
| 402 | return ret; |
| 403 | } |
| 404 | |
Johannes Berg | 1c582d9 | 2013-11-14 17:14:41 +0100 | [diff] [blame] | 405 | int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 406 | { |
| 407 | struct net_device *dev; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 408 | struct ieee802154_addr addr; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 409 | int ret = -EOPNOTSUPP; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 410 | |
| 411 | if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] && |
| 412 | !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) || |
| 413 | !info->attrs[IEEE802154_ATTR_REASON]) |
| 414 | return -EINVAL; |
| 415 | |
| 416 | dev = ieee802154_nl_get_dev(info); |
| 417 | if (!dev) |
| 418 | return -ENODEV; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 419 | if (!ieee802154_mlme_ops(dev)->disassoc_req) |
| 420 | goto out; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 421 | |
| 422 | if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) { |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 423 | addr.mode = IEEE802154_ADDR_LONG; |
| 424 | addr.extended_addr = nla_get_hwaddr( |
| 425 | info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 426 | } else { |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 427 | addr.mode = IEEE802154_ADDR_SHORT; |
| 428 | addr.short_addr = nla_get_shortaddr( |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 429 | info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]); |
| 430 | } |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 431 | addr.pan_id = ieee802154_mlme_ops(dev)->get_pan_id(dev); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 432 | |
| 433 | ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr, |
| 434 | nla_get_u8(info->attrs[IEEE802154_ATTR_REASON])); |
| 435 | |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 436 | out: |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 437 | dev_put(dev); |
| 438 | return ret; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * PANid, channel, beacon_order = 15, superframe_order = 15, |
| 443 | * PAN_coordinator, battery_life_extension = 0, |
| 444 | * coord_realignment = 0, security_enable = 0 |
| 445 | */ |
Johannes Berg | 1c582d9 | 2013-11-14 17:14:41 +0100 | [diff] [blame] | 446 | int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 447 | { |
| 448 | struct net_device *dev; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 449 | struct ieee802154_addr addr; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 450 | |
| 451 | u8 channel, bcn_ord, sf_ord; |
| 452 | u8 page; |
| 453 | int pan_coord, blx, coord_realign; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 454 | int ret = -EOPNOTSUPP; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 455 | |
| 456 | if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] || |
| 457 | !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] || |
| 458 | !info->attrs[IEEE802154_ATTR_CHANNEL] || |
| 459 | !info->attrs[IEEE802154_ATTR_BCN_ORD] || |
| 460 | !info->attrs[IEEE802154_ATTR_SF_ORD] || |
| 461 | !info->attrs[IEEE802154_ATTR_PAN_COORD] || |
| 462 | !info->attrs[IEEE802154_ATTR_BAT_EXT] || |
| 463 | !info->attrs[IEEE802154_ATTR_COORD_REALIGN] |
| 464 | ) |
| 465 | return -EINVAL; |
| 466 | |
| 467 | dev = ieee802154_nl_get_dev(info); |
| 468 | if (!dev) |
| 469 | return -ENODEV; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 470 | if (!ieee802154_mlme_ops(dev)->start_req) |
| 471 | goto out; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 472 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 473 | addr.mode = IEEE802154_ADDR_SHORT; |
| 474 | addr.short_addr = nla_get_shortaddr( |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 475 | info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]); |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 476 | addr.pan_id = nla_get_shortaddr( |
| 477 | info->attrs[IEEE802154_ATTR_COORD_PAN_ID]); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 478 | |
| 479 | channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]); |
| 480 | bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]); |
| 481 | sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]); |
| 482 | pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]); |
| 483 | blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]); |
| 484 | coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]); |
| 485 | |
| 486 | if (info->attrs[IEEE802154_ATTR_PAGE]) |
| 487 | page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]); |
| 488 | else |
| 489 | page = 0; |
| 490 | |
| 491 | |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame^] | 492 | if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) { |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 493 | ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS); |
| 494 | dev_put(dev); |
| 495 | return -EINVAL; |
| 496 | } |
| 497 | |
| 498 | ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page, |
| 499 | bcn_ord, sf_ord, pan_coord, blx, coord_realign); |
| 500 | |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 501 | out: |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 502 | dev_put(dev); |
| 503 | return ret; |
| 504 | } |
| 505 | |
Johannes Berg | 1c582d9 | 2013-11-14 17:14:41 +0100 | [diff] [blame] | 506 | int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 507 | { |
| 508 | struct net_device *dev; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 509 | int ret = -EOPNOTSUPP; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 510 | u8 type; |
| 511 | u32 channels; |
| 512 | u8 duration; |
| 513 | u8 page; |
| 514 | |
| 515 | if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] || |
| 516 | !info->attrs[IEEE802154_ATTR_CHANNELS] || |
| 517 | !info->attrs[IEEE802154_ATTR_DURATION]) |
| 518 | return -EINVAL; |
| 519 | |
| 520 | dev = ieee802154_nl_get_dev(info); |
| 521 | if (!dev) |
| 522 | return -ENODEV; |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 523 | if (!ieee802154_mlme_ops(dev)->scan_req) |
| 524 | goto out; |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 525 | |
| 526 | type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]); |
| 527 | channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]); |
| 528 | duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]); |
| 529 | |
| 530 | if (info->attrs[IEEE802154_ATTR_PAGE]) |
| 531 | page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]); |
| 532 | else |
| 533 | page = 0; |
| 534 | |
| 535 | |
| 536 | ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels, page, |
| 537 | duration); |
| 538 | |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 539 | out: |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 540 | dev_put(dev); |
| 541 | return ret; |
| 542 | } |
| 543 | |
Johannes Berg | 1c582d9 | 2013-11-14 17:14:41 +0100 | [diff] [blame] | 544 | int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 545 | { |
| 546 | /* Request for interface name, index, type, IEEE address, |
| 547 | PAN Id, short address */ |
| 548 | struct sk_buff *msg; |
| 549 | struct net_device *dev = NULL; |
| 550 | int rc = -ENOBUFS; |
| 551 | |
| 552 | pr_debug("%s\n", __func__); |
| 553 | |
| 554 | dev = ieee802154_nl_get_dev(info); |
| 555 | if (!dev) |
| 556 | return -ENODEV; |
| 557 | |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 558 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 559 | if (!msg) |
| 560 | goto out_dev; |
| 561 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 562 | rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq, |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 563 | 0, dev); |
| 564 | if (rc < 0) |
| 565 | goto out_free; |
| 566 | |
| 567 | dev_put(dev); |
| 568 | |
| 569 | return genlmsg_reply(msg, info); |
| 570 | out_free: |
| 571 | nlmsg_free(msg); |
| 572 | out_dev: |
| 573 | dev_put(dev); |
| 574 | return rc; |
| 575 | |
| 576 | } |
| 577 | |
Johannes Berg | 1c582d9 | 2013-11-14 17:14:41 +0100 | [diff] [blame] | 578 | int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb) |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 579 | { |
| 580 | struct net *net = sock_net(skb->sk); |
| 581 | struct net_device *dev; |
| 582 | int idx; |
| 583 | int s_idx = cb->args[0]; |
| 584 | |
| 585 | pr_debug("%s\n", __func__); |
| 586 | |
| 587 | idx = 0; |
| 588 | for_each_netdev(net, dev) { |
| 589 | if (idx < s_idx || (dev->type != ARPHRD_IEEE802154)) |
| 590 | goto cont; |
| 591 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 592 | if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid, |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 593 | cb->nlh->nlmsg_seq, NLM_F_MULTI, dev) < 0) |
| 594 | break; |
| 595 | cont: |
| 596 | idx++; |
| 597 | } |
| 598 | cb->args[0] = idx; |
| 599 | |
| 600 | return skb->len; |
| 601 | } |