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 25 #include <linux/kernel.h> 26 #include <linux/slab.h> 27 #include <linux/if_arp.h> 28 #include <net/netlink.h> 29 #include <net/genetlink.h> 30 #include <net/wpan-phy.h> 31 #include <net/af_ieee802154.h> 32 #include <net/ieee802154_netdev.h> 33 #include <net/rtnetlink.h> /* for rtnl_{un,}lock */ 34 #include <linux/nl802154.h> 35 36 #include "ieee802154.h" 37 38 static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid, 39 u32 seq, int flags, struct wpan_phy *phy) 40 { 41 void *hdr; 42 int i, pages = 0; 43 uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL); 44 45 pr_debug("%s\n", __func__); 46 47 if (!buf) 48 return -EMSGSIZE; 49 50 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags, 51 IEEE802154_LIST_PHY); 52 if (!hdr) 53 goto out; 54 55 mutex_lock(&phy->pib_lock); 56 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || 57 nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) || 58 nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel) || 59 nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, phy->transmit_power) || 60 nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, phy->lbt) || 61 nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE, phy->cca_mode) || 62 nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL, phy->cca_ed_level) || 63 nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES, phy->csma_retries) || 64 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE, phy->min_be) || 65 nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE, phy->max_be) || 66 nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES, phy->frame_retries)) 67 goto nla_put_failure; 68 for (i = 0; i < 32; i++) { 69 if (phy->channels_supported[i]) 70 buf[pages++] = phy->channels_supported[i] | (i << 27); 71 } 72 if (pages && 73 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST, 74 pages * sizeof(uint32_t), buf)) 75 goto nla_put_failure; 76 mutex_unlock(&phy->pib_lock); 77 kfree(buf); 78 return genlmsg_end(msg, hdr); 79 80 nla_put_failure: 81 mutex_unlock(&phy->pib_lock); 82 genlmsg_cancel(msg, hdr); 83 out: 84 kfree(buf); 85 return -EMSGSIZE; 86 } 87 88 int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info) 89 { 90 /* Request for interface name, index, type, IEEE address, 91 PAN Id, short address */ 92 struct sk_buff *msg; 93 struct wpan_phy *phy; 94 const char *name; 95 int rc = -ENOBUFS; 96 97 pr_debug("%s\n", __func__); 98 99 if (!info->attrs[IEEE802154_ATTR_PHY_NAME]) 100 return -EINVAL; 101 102 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); 103 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') 104 return -EINVAL; /* phy name should be null-terminated */ 105 106 107 phy = wpan_phy_find(name); 108 if (!phy) 109 return -ENODEV; 110 111 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 112 if (!msg) 113 goto out_dev; 114 115 rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq, 116 0, phy); 117 if (rc < 0) 118 goto out_free; 119 120 wpan_phy_put(phy); 121 122 return genlmsg_reply(msg, info); 123 out_free: 124 nlmsg_free(msg); 125 out_dev: 126 wpan_phy_put(phy); 127 return rc; 128 129 } 130 131 struct dump_phy_data { 132 struct sk_buff *skb; 133 struct netlink_callback *cb; 134 int idx, s_idx; 135 }; 136 137 static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data) 138 { 139 int rc; 140 struct dump_phy_data *data = _data; 141 142 pr_debug("%s\n", __func__); 143 144 if (data->idx++ < data->s_idx) 145 return 0; 146 147 rc = ieee802154_nl_fill_phy(data->skb, 148 NETLINK_CB(data->cb->skb).portid, 149 data->cb->nlh->nlmsg_seq, 150 NLM_F_MULTI, 151 phy); 152 153 if (rc < 0) { 154 data->idx--; 155 return rc; 156 } 157 158 return 0; 159 } 160 161 int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb) 162 { 163 struct dump_phy_data data = { 164 .cb = cb, 165 .skb = skb, 166 .s_idx = cb->args[0], 167 .idx = 0, 168 }; 169 170 pr_debug("%s\n", __func__); 171 172 wpan_phy_for_each(ieee802154_dump_phy_iter, &data); 173 174 cb->args[0] = data.idx; 175 176 return skb->len; 177 } 178 179 int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info) 180 { 181 struct sk_buff *msg; 182 struct wpan_phy *phy; 183 const char *name; 184 const char *devname; 185 int rc = -ENOBUFS; 186 struct net_device *dev; 187 int type = __IEEE802154_DEV_INVALID; 188 189 pr_debug("%s\n", __func__); 190 191 if (!info->attrs[IEEE802154_ATTR_PHY_NAME]) 192 return -EINVAL; 193 194 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); 195 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') 196 return -EINVAL; /* phy name should be null-terminated */ 197 198 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) { 199 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]); 200 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] 201 != '\0') 202 return -EINVAL; /* phy name should be null-terminated */ 203 } else { 204 devname = "wpan%d"; 205 } 206 207 if (strlen(devname) >= IFNAMSIZ) 208 return -ENAMETOOLONG; 209 210 phy = wpan_phy_find(name); 211 if (!phy) 212 return -ENODEV; 213 214 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE); 215 if (!msg) 216 goto out_dev; 217 218 if (!phy->add_iface) { 219 rc = -EINVAL; 220 goto nla_put_failure; 221 } 222 223 if (info->attrs[IEEE802154_ATTR_HW_ADDR] && 224 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) != 225 IEEE802154_ADDR_LEN) { 226 rc = -EINVAL; 227 goto nla_put_failure; 228 } 229 230 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) { 231 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]); 232 if (type >= __IEEE802154_DEV_MAX) { 233 rc = -EINVAL; 234 goto nla_put_failure; 235 } 236 } 237 238 dev = phy->add_iface(phy, devname, type); 239 if (IS_ERR(dev)) { 240 rc = PTR_ERR(dev); 241 goto nla_put_failure; 242 } 243 244 if (info->attrs[IEEE802154_ATTR_HW_ADDR]) { 245 struct sockaddr addr; 246 247 addr.sa_family = ARPHRD_IEEE802154; 248 nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR], 249 IEEE802154_ADDR_LEN); 250 251 /* 252 * strangely enough, some callbacks (inetdev_event) from 253 * dev_set_mac_address require RTNL_LOCK 254 */ 255 rtnl_lock(); 256 rc = dev_set_mac_address(dev, &addr); 257 rtnl_unlock(); 258 if (rc) 259 goto dev_unregister; 260 } 261 262 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || 263 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name)) 264 goto nla_put_failure; 265 dev_put(dev); 266 267 wpan_phy_put(phy); 268 269 return ieee802154_nl_reply(msg, info); 270 271 dev_unregister: 272 rtnl_lock(); /* del_iface must be called with RTNL lock */ 273 phy->del_iface(phy, dev); 274 dev_put(dev); 275 rtnl_unlock(); 276 nla_put_failure: 277 nlmsg_free(msg); 278 out_dev: 279 wpan_phy_put(phy); 280 return rc; 281 } 282 283 int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info) 284 { 285 struct sk_buff *msg; 286 struct wpan_phy *phy; 287 const char *name; 288 int rc; 289 struct net_device *dev; 290 291 pr_debug("%s\n", __func__); 292 293 if (!info->attrs[IEEE802154_ATTR_DEV_NAME]) 294 return -EINVAL; 295 296 name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]); 297 if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0') 298 return -EINVAL; /* name should be null-terminated */ 299 300 dev = dev_get_by_name(genl_info_net(info), name); 301 if (!dev) 302 return -ENODEV; 303 304 phy = ieee802154_mlme_ops(dev)->get_phy(dev); 305 BUG_ON(!phy); 306 307 rc = -EINVAL; 308 /* phy name is optional, but should be checked if it's given */ 309 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) { 310 struct wpan_phy *phy2; 311 312 const char *pname = 313 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); 314 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] 315 != '\0') 316 /* name should be null-terminated */ 317 goto out_dev; 318 319 phy2 = wpan_phy_find(pname); 320 if (!phy2) 321 goto out_dev; 322 323 if (phy != phy2) { 324 wpan_phy_put(phy2); 325 goto out_dev; 326 } 327 } 328 329 rc = -ENOBUFS; 330 331 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE); 332 if (!msg) 333 goto out_dev; 334 335 if (!phy->del_iface) { 336 rc = -EINVAL; 337 goto nla_put_failure; 338 } 339 340 rtnl_lock(); 341 phy->del_iface(phy, dev); 342 343 /* We don't have device anymore */ 344 dev_put(dev); 345 dev = NULL; 346 347 rtnl_unlock(); 348 349 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || 350 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name)) 351 goto nla_put_failure; 352 wpan_phy_put(phy); 353 354 return ieee802154_nl_reply(msg, info); 355 356 nla_put_failure: 357 nlmsg_free(msg); 358 out_dev: 359 wpan_phy_put(phy); 360 if (dev) 361 dev_put(dev); 362 363 return rc; 364 } 365 366 static int phy_set_txpower(struct wpan_phy *phy, struct genl_info *info) 367 { 368 int txpower = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]); 369 int rc; 370 371 rc = phy->set_txpower(phy, txpower); 372 if (rc < 0) 373 return rc; 374 375 phy->transmit_power = txpower; 376 377 return 0; 378 } 379 380 static int phy_set_lbt(struct wpan_phy *phy, struct genl_info *info) 381 { 382 u8 on = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]); 383 int rc; 384 385 rc = phy->set_lbt(phy, on); 386 if (rc < 0) 387 return rc; 388 389 phy->lbt = on; 390 391 return 0; 392 } 393 394 static int phy_set_cca_mode(struct wpan_phy *phy, struct genl_info *info) 395 { 396 u8 mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]); 397 int rc; 398 399 if (mode > 3) 400 return -EINVAL; 401 402 rc = phy->set_cca_mode(phy, mode); 403 if (rc < 0) 404 return rc; 405 406 phy->cca_mode = mode; 407 408 return 0; 409 } 410 411 static int phy_set_cca_ed_level(struct wpan_phy *phy, struct genl_info *info) 412 { 413 s32 level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]); 414 int rc; 415 416 rc = phy->set_cca_ed_level(phy, level); 417 if (rc < 0) 418 return rc; 419 420 phy->cca_ed_level = level; 421 422 return 0; 423 } 424 425 static int phy_set_csma_params(struct wpan_phy *phy, struct genl_info *info) 426 { 427 int rc; 428 u8 min_be = phy->min_be; 429 u8 max_be = phy->max_be; 430 u8 retries = phy->csma_retries; 431 432 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES]) 433 retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]); 434 if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]) 435 min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]); 436 if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]) 437 max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]); 438 439 if (retries > 5 || max_be < 3 || max_be > 8 || min_be > max_be) 440 return -EINVAL; 441 442 rc = phy->set_csma_params(phy, min_be, max_be, retries); 443 if (rc < 0) 444 return rc; 445 446 phy->min_be = min_be; 447 phy->max_be = max_be; 448 phy->csma_retries = retries; 449 450 return 0; 451 } 452 453 static int phy_set_frame_retries(struct wpan_phy *phy, struct genl_info *info) 454 { 455 s8 retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]); 456 int rc; 457 458 if (retries < -1 || retries > 7) 459 return -EINVAL; 460 461 rc = phy->set_frame_retries(phy, retries); 462 if (rc < 0) 463 return rc; 464 465 phy->frame_retries = retries; 466 467 return 0; 468 } 469 470 int ieee802154_set_phyparams(struct sk_buff *skb, struct genl_info *info) 471 { 472 struct wpan_phy *phy; 473 const char *name; 474 int rc = -ENOTSUPP; 475 476 pr_debug("%s\n", __func__); 477 478 if (!info->attrs[IEEE802154_ATTR_PHY_NAME] && 479 !info->attrs[IEEE802154_ATTR_LBT_ENABLED] && 480 !info->attrs[IEEE802154_ATTR_CCA_MODE] && 481 !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] && 482 !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] && 483 !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] && 484 !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] && 485 !info->attrs[IEEE802154_ATTR_FRAME_RETRIES]) 486 return -EINVAL; 487 488 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); 489 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') 490 return -EINVAL; /* phy name should be null-terminated */ 491 492 phy = wpan_phy_find(name); 493 if (!phy) 494 return -ENODEV; 495 496 if ((!phy->set_txpower && info->attrs[IEEE802154_ATTR_TXPOWER]) || 497 (!phy->set_lbt && info->attrs[IEEE802154_ATTR_LBT_ENABLED]) || 498 (!phy->set_cca_mode && info->attrs[IEEE802154_ATTR_CCA_MODE]) || 499 (!phy->set_cca_ed_level && 500 info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL])) 501 goto out; 502 503 mutex_lock(&phy->pib_lock); 504 505 if (info->attrs[IEEE802154_ATTR_TXPOWER]) { 506 rc = phy_set_txpower(phy, info); 507 if (rc < 0) 508 goto error; 509 } 510 511 if (info->attrs[IEEE802154_ATTR_LBT_ENABLED]) { 512 rc = phy_set_lbt(phy, info); 513 if (rc < 0) 514 goto error; 515 } 516 517 if (info->attrs[IEEE802154_ATTR_CCA_MODE]) { 518 rc = phy_set_cca_mode(phy, info); 519 if (rc < 0) 520 goto error; 521 } 522 523 if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) { 524 rc = phy_set_cca_ed_level(phy, info); 525 if (rc < 0) 526 goto error; 527 } 528 529 if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES] || 530 info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] || 531 info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]) { 532 rc = phy_set_csma_params(phy, info); 533 if (rc < 0) 534 goto error; 535 } 536 537 if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES]) { 538 rc = phy_set_frame_retries(phy, info); 539 if (rc < 0) 540 goto error; 541 } 542 543 mutex_unlock(&phy->pib_lock); 544 545 wpan_phy_put(phy); 546 547 return 0; 548 549 error: 550 mutex_unlock(&phy->pib_lock); 551 out: 552 wpan_phy_put(phy); 553 return rc; 554 } 555