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 * 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> 23 #include <linux/ieee802154.h> 24 25 #include <net/nl802154.h> 26 #include <net/mac802154.h> 27 #include <net/ieee802154_netdev.h> 28 #include <net/cfg802154.h> 29 30 #include "ieee802154_i.h" 31 #include "driver-ops.h" 32 33 static int mac802154_wpan_update_llsec(struct net_device *dev) 34 { 35 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); 36 struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); 37 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 38 int rc = 0; 39 40 if (ops->llsec) { 41 struct ieee802154_llsec_params params; 42 int changed = 0; 43 44 params.pan_id = wpan_dev->pan_id; 45 changed |= IEEE802154_LLSEC_PARAM_PAN_ID; 46 47 params.hwaddr = wpan_dev->extended_addr; 48 changed |= IEEE802154_LLSEC_PARAM_HWADDR; 49 50 rc = ops->llsec->set_params(dev, ¶ms, changed); 51 } 52 53 return rc; 54 } 55 56 static int 57 mac802154_wpan_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd) 58 { 59 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); 60 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 61 struct sockaddr_ieee802154 *sa = 62 (struct sockaddr_ieee802154 *)&ifr->ifr_addr; 63 int err = -ENOIOCTLCMD; 64 65 ASSERT_RTNL(); 66 67 spin_lock_bh(&sdata->mib_lock); 68 69 switch (cmd) { 70 case SIOCGIFADDR: 71 { 72 u16 pan_id, short_addr; 73 74 pan_id = le16_to_cpu(wpan_dev->pan_id); 75 short_addr = le16_to_cpu(wpan_dev->short_addr); 76 if (pan_id == IEEE802154_PANID_BROADCAST || 77 short_addr == IEEE802154_ADDR_BROADCAST) { 78 err = -EADDRNOTAVAIL; 79 break; 80 } 81 82 sa->family = AF_IEEE802154; 83 sa->addr.addr_type = IEEE802154_ADDR_SHORT; 84 sa->addr.pan_id = pan_id; 85 sa->addr.short_addr = short_addr; 86 87 err = 0; 88 break; 89 } 90 case SIOCSIFADDR: 91 if (netif_running(dev)) { 92 spin_unlock_bh(&sdata->mib_lock); 93 return -EBUSY; 94 } 95 96 dev_warn(&dev->dev, 97 "Using DEBUGing ioctl SIOCSIFADDR isn't recommended!\n"); 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 107 wpan_dev->pan_id = cpu_to_le16(sa->addr.pan_id); 108 wpan_dev->short_addr = cpu_to_le16(sa->addr.short_addr); 109 110 err = mac802154_wpan_update_llsec(dev); 111 break; 112 } 113 114 spin_unlock_bh(&sdata->mib_lock); 115 return err; 116 } 117 118 static int mac802154_wpan_mac_addr(struct net_device *dev, void *p) 119 { 120 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); 121 struct sockaddr *addr = p; 122 __le64 extended_addr; 123 124 if (netif_running(dev)) 125 return -EBUSY; 126 127 ieee802154_be64_to_le64(&extended_addr, addr->sa_data); 128 if (!ieee802154_is_valid_extended_addr(extended_addr)) 129 return -EINVAL; 130 131 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len); 132 sdata->wpan_dev.extended_addr = extended_addr; 133 134 return mac802154_wpan_update_llsec(dev); 135 } 136 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_local *local = sdata->local; 141 int res = 0; 142 143 ASSERT_RTNL(); 144 145 set_bit(SDATA_STATE_RUNNING, &sdata->state); 146 147 if (!local->open_count) { 148 res = drv_start(local); 149 WARN_ON(res); 150 if (res) 151 goto err; 152 } 153 154 local->open_count++; 155 netif_start_queue(dev); 156 return 0; 157 err: 158 /* might already be clear but that doesn't matter */ 159 clear_bit(SDATA_STATE_RUNNING, &sdata->state); 160 161 return res; 162 } 163 164 static int 165 ieee802154_check_mac_settings(struct ieee802154_local *local, 166 struct wpan_dev *wpan_dev, 167 struct wpan_dev *nwpan_dev) 168 { 169 ASSERT_RTNL(); 170 171 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) { 172 if (wpan_dev->promiscuous_mode != nwpan_dev->promiscuous_mode) 173 return -EBUSY; 174 } 175 176 if (local->hw.flags & IEEE802154_HW_AFILT) { 177 if (wpan_dev->pan_id != nwpan_dev->pan_id) 178 return -EBUSY; 179 180 if (wpan_dev->short_addr != nwpan_dev->short_addr) 181 return -EBUSY; 182 183 if (wpan_dev->extended_addr != nwpan_dev->extended_addr) 184 return -EBUSY; 185 } 186 187 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) { 188 if (wpan_dev->min_be != nwpan_dev->min_be) 189 return -EBUSY; 190 191 if (wpan_dev->max_be != nwpan_dev->max_be) 192 return -EBUSY; 193 194 if (wpan_dev->csma_retries != nwpan_dev->csma_retries) 195 return -EBUSY; 196 } 197 198 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) { 199 if (wpan_dev->frame_retries != nwpan_dev->frame_retries) 200 return -EBUSY; 201 } 202 203 if (local->hw.flags & IEEE802154_HW_LBT) { 204 if (wpan_dev->lbt != nwpan_dev->lbt) 205 return -EBUSY; 206 } 207 208 return 0; 209 } 210 211 static int 212 ieee802154_check_concurrent_iface(struct ieee802154_sub_if_data *sdata, 213 enum nl802154_iftype iftype) 214 { 215 struct ieee802154_local *local = sdata->local; 216 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 217 struct ieee802154_sub_if_data *nsdata; 218 219 /* we hold the RTNL here so can safely walk the list */ 220 list_for_each_entry(nsdata, &local->interfaces, list) { 221 if (nsdata != sdata && ieee802154_sdata_running(nsdata)) { 222 int ret; 223 224 /* TODO currently we don't support multiple node types 225 * we need to run skb_clone at rx path. Check if there 226 * exist really an use case if we need to support 227 * multiple node types at the same time. 228 */ 229 if (sdata->vif.type == NL802154_IFTYPE_NODE && 230 nsdata->vif.type == NL802154_IFTYPE_NODE) 231 return -EBUSY; 232 233 /* check all phy mac sublayer settings are the same. 234 * We have only one phy, different values makes trouble. 235 */ 236 ret = ieee802154_check_mac_settings(local, wpan_dev, 237 &nsdata->wpan_dev); 238 if (ret < 0) 239 return ret; 240 } 241 } 242 243 return 0; 244 } 245 246 static int mac802154_wpan_open(struct net_device *dev) 247 { 248 int rc; 249 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); 250 struct ieee802154_local *local = sdata->local; 251 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 252 struct wpan_phy *phy = sdata->local->phy; 253 254 rc = ieee802154_check_concurrent_iface(sdata, sdata->vif.type); 255 if (rc < 0) 256 return rc; 257 258 rc = mac802154_slave_open(dev); 259 if (rc < 0) 260 return rc; 261 262 mutex_lock(&phy->pib_lock); 263 264 if (local->hw.flags & IEEE802154_HW_PROMISCUOUS) { 265 rc = drv_set_promiscuous_mode(local, 266 wpan_dev->promiscuous_mode); 267 if (rc < 0) 268 goto out; 269 } 270 271 if (local->hw.flags & IEEE802154_HW_AFILT) { 272 rc = drv_set_pan_id(local, wpan_dev->pan_id); 273 if (rc < 0) 274 goto out; 275 276 rc = drv_set_extended_addr(local, wpan_dev->extended_addr); 277 if (rc < 0) 278 goto out; 279 280 rc = drv_set_short_addr(local, wpan_dev->short_addr); 281 if (rc < 0) 282 goto out; 283 } 284 285 if (local->hw.flags & IEEE802154_HW_LBT) { 286 rc = drv_set_lbt_mode(local, wpan_dev->lbt); 287 if (rc < 0) 288 goto out; 289 } 290 291 if (local->hw.flags & IEEE802154_HW_CSMA_PARAMS) { 292 rc = drv_set_csma_params(local, wpan_dev->min_be, 293 wpan_dev->max_be, 294 wpan_dev->csma_retries); 295 if (rc < 0) 296 goto out; 297 } 298 299 if (local->hw.flags & IEEE802154_HW_FRAME_RETRIES) { 300 rc = drv_set_max_frame_retries(local, wpan_dev->frame_retries); 301 if (rc < 0) 302 goto out; 303 } 304 305 mutex_unlock(&phy->pib_lock); 306 return 0; 307 308 out: 309 mutex_unlock(&phy->pib_lock); 310 return rc; 311 } 312 313 static int mac802154_slave_close(struct net_device *dev) 314 { 315 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); 316 struct ieee802154_local *local = sdata->local; 317 318 ASSERT_RTNL(); 319 320 hrtimer_cancel(&local->ifs_timer); 321 322 netif_stop_queue(dev); 323 local->open_count--; 324 325 clear_bit(SDATA_STATE_RUNNING, &sdata->state); 326 327 if (!local->open_count) 328 drv_stop(local); 329 330 return 0; 331 } 332 333 static int mac802154_set_header_security(struct ieee802154_sub_if_data *sdata, 334 struct ieee802154_hdr *hdr, 335 const struct ieee802154_mac_cb *cb) 336 { 337 struct ieee802154_llsec_params params; 338 u8 level; 339 340 mac802154_llsec_get_params(&sdata->sec, ¶ms); 341 342 if (!params.enabled && cb->secen_override && cb->secen) 343 return -EINVAL; 344 if (!params.enabled || 345 (cb->secen_override && !cb->secen) || 346 !params.out_level) 347 return 0; 348 if (cb->seclevel_override && !cb->seclevel) 349 return -EINVAL; 350 351 level = cb->seclevel_override ? cb->seclevel : params.out_level; 352 353 hdr->fc.security_enabled = 1; 354 hdr->sec.level = level; 355 hdr->sec.key_id_mode = params.out_key.mode; 356 if (params.out_key.mode == IEEE802154_SCF_KEY_SHORT_INDEX) 357 hdr->sec.short_src = params.out_key.short_source; 358 else if (params.out_key.mode == IEEE802154_SCF_KEY_HW_INDEX) 359 hdr->sec.extended_src = params.out_key.extended_source; 360 hdr->sec.key_id = params.out_key.id; 361 362 return 0; 363 } 364 365 static int mac802154_header_create(struct sk_buff *skb, 366 struct net_device *dev, 367 unsigned short type, 368 const void *daddr, 369 const void *saddr, 370 unsigned len) 371 { 372 struct ieee802154_hdr hdr; 373 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); 374 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 375 struct ieee802154_mac_cb *cb = mac_cb(skb); 376 int hlen; 377 378 if (!daddr) 379 return -EINVAL; 380 381 memset(&hdr.fc, 0, sizeof(hdr.fc)); 382 hdr.fc.type = cb->type; 383 hdr.fc.security_enabled = cb->secen; 384 hdr.fc.ack_request = cb->ackreq; 385 hdr.seq = ieee802154_mlme_ops(dev)->get_dsn(dev); 386 387 if (mac802154_set_header_security(sdata, &hdr, cb) < 0) 388 return -EINVAL; 389 390 if (!saddr) { 391 spin_lock_bh(&sdata->mib_lock); 392 393 if (wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST) || 394 wpan_dev->short_addr == cpu_to_le16(IEEE802154_ADDR_UNDEF) || 395 wpan_dev->pan_id == cpu_to_le16(IEEE802154_PANID_BROADCAST)) { 396 hdr.source.mode = IEEE802154_ADDR_LONG; 397 hdr.source.extended_addr = wpan_dev->extended_addr; 398 } else { 399 hdr.source.mode = IEEE802154_ADDR_SHORT; 400 hdr.source.short_addr = wpan_dev->short_addr; 401 } 402 403 hdr.source.pan_id = wpan_dev->pan_id; 404 405 spin_unlock_bh(&sdata->mib_lock); 406 } else { 407 hdr.source = *(const struct ieee802154_addr *)saddr; 408 } 409 410 hdr.dest = *(const struct ieee802154_addr *)daddr; 411 412 hlen = ieee802154_hdr_push(skb, &hdr); 413 if (hlen < 0) 414 return -EINVAL; 415 416 skb_reset_mac_header(skb); 417 skb->mac_len = hlen; 418 419 if (len > ieee802154_max_payload(&hdr)) 420 return -EMSGSIZE; 421 422 return hlen; 423 } 424 425 static int 426 mac802154_header_parse(const struct sk_buff *skb, unsigned char *haddr) 427 { 428 struct ieee802154_hdr hdr; 429 struct ieee802154_addr *addr = (struct ieee802154_addr *)haddr; 430 431 if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) { 432 pr_debug("malformed packet\n"); 433 return 0; 434 } 435 436 *addr = hdr.source; 437 return sizeof(*addr); 438 } 439 440 static struct header_ops mac802154_header_ops = { 441 .create = mac802154_header_create, 442 .parse = mac802154_header_parse, 443 }; 444 445 static const struct net_device_ops mac802154_wpan_ops = { 446 .ndo_open = mac802154_wpan_open, 447 .ndo_stop = mac802154_slave_close, 448 .ndo_start_xmit = ieee802154_subif_start_xmit, 449 .ndo_do_ioctl = mac802154_wpan_ioctl, 450 .ndo_set_mac_address = mac802154_wpan_mac_addr, 451 }; 452 453 static const struct net_device_ops mac802154_monitor_ops = { 454 .ndo_open = mac802154_wpan_open, 455 .ndo_stop = mac802154_slave_close, 456 .ndo_start_xmit = ieee802154_monitor_start_xmit, 457 }; 458 459 static void mac802154_wpan_free(struct net_device *dev) 460 { 461 struct ieee802154_sub_if_data *sdata = IEEE802154_DEV_TO_SUB_IF(dev); 462 463 mac802154_llsec_destroy(&sdata->sec); 464 465 free_netdev(dev); 466 } 467 468 static void ieee802154_if_setup(struct net_device *dev) 469 { 470 dev->addr_len = IEEE802154_EXTENDED_ADDR_LEN; 471 memset(dev->broadcast, 0xff, IEEE802154_EXTENDED_ADDR_LEN); 472 473 dev->hard_header_len = MAC802154_FRAME_HARD_HEADER_LEN; 474 dev->needed_tailroom = 2 + 16; /* FCS + MIC */ 475 dev->mtu = IEEE802154_MTU; 476 dev->tx_queue_len = 300; 477 dev->flags = IFF_NOARP | IFF_BROADCAST; 478 } 479 480 static int 481 ieee802154_setup_sdata(struct ieee802154_sub_if_data *sdata, 482 enum nl802154_iftype type) 483 { 484 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 485 486 /* set some type-dependent values */ 487 sdata->vif.type = type; 488 sdata->wpan_dev.iftype = type; 489 490 get_random_bytes(&wpan_dev->bsn, 1); 491 get_random_bytes(&wpan_dev->dsn, 1); 492 493 /* defaults per 802.15.4-2011 */ 494 wpan_dev->min_be = 3; 495 wpan_dev->max_be = 5; 496 wpan_dev->csma_retries = 4; 497 /* for compatibility, actual default is 3 */ 498 wpan_dev->frame_retries = -1; 499 500 wpan_dev->pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); 501 wpan_dev->short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); 502 503 switch (type) { 504 case NL802154_IFTYPE_NODE: 505 ieee802154_be64_to_le64(&wpan_dev->extended_addr, 506 sdata->dev->dev_addr); 507 508 sdata->dev->header_ops = &mac802154_header_ops; 509 sdata->dev->destructor = mac802154_wpan_free; 510 sdata->dev->netdev_ops = &mac802154_wpan_ops; 511 sdata->dev->ml_priv = &mac802154_mlme_wpan; 512 wpan_dev->promiscuous_mode = false; 513 514 spin_lock_init(&sdata->mib_lock); 515 mutex_init(&sdata->sec_mtx); 516 517 mac802154_llsec_init(&sdata->sec); 518 break; 519 case NL802154_IFTYPE_MONITOR: 520 sdata->dev->destructor = free_netdev; 521 sdata->dev->netdev_ops = &mac802154_monitor_ops; 522 wpan_dev->promiscuous_mode = true; 523 break; 524 default: 525 BUG(); 526 } 527 528 return 0; 529 } 530 531 struct net_device * 532 ieee802154_if_add(struct ieee802154_local *local, const char *name, 533 enum nl802154_iftype type, __le64 extended_addr) 534 { 535 struct net_device *ndev = NULL; 536 struct ieee802154_sub_if_data *sdata = NULL; 537 int ret = -ENOMEM; 538 539 ASSERT_RTNL(); 540 541 ndev = alloc_netdev(sizeof(*sdata) + local->hw.vif_data_size, name, 542 NET_NAME_UNKNOWN, ieee802154_if_setup); 543 if (!ndev) 544 return ERR_PTR(-ENOMEM); 545 546 ndev->needed_headroom = local->hw.extra_tx_headroom; 547 548 ret = dev_alloc_name(ndev, ndev->name); 549 if (ret < 0) 550 goto err; 551 552 ieee802154_le64_to_be64(ndev->perm_addr, 553 &local->hw.phy->perm_extended_addr); 554 switch (type) { 555 case NL802154_IFTYPE_NODE: 556 ndev->type = ARPHRD_IEEE802154; 557 if (ieee802154_is_valid_extended_addr(extended_addr)) 558 ieee802154_le64_to_be64(ndev->dev_addr, &extended_addr); 559 else 560 memcpy(ndev->dev_addr, ndev->perm_addr, 561 IEEE802154_EXTENDED_ADDR_LEN); 562 break; 563 case NL802154_IFTYPE_MONITOR: 564 ndev->type = ARPHRD_IEEE802154_MONITOR; 565 break; 566 default: 567 ret = -EINVAL; 568 goto err; 569 } 570 571 /* TODO check this */ 572 SET_NETDEV_DEV(ndev, &local->phy->dev); 573 sdata = netdev_priv(ndev); 574 ndev->ieee802154_ptr = &sdata->wpan_dev; 575 memcpy(sdata->name, ndev->name, IFNAMSIZ); 576 sdata->dev = ndev; 577 sdata->wpan_dev.wpan_phy = local->hw.phy; 578 sdata->local = local; 579 580 /* setup type-dependent data */ 581 ret = ieee802154_setup_sdata(sdata, type); 582 if (ret) 583 goto err; 584 585 ret = register_netdevice(ndev); 586 if (ret < 0) 587 goto err; 588 589 mutex_lock(&local->iflist_mtx); 590 list_add_tail_rcu(&sdata->list, &local->interfaces); 591 mutex_unlock(&local->iflist_mtx); 592 593 return ndev; 594 595 err: 596 free_netdev(ndev); 597 return ERR_PTR(ret); 598 } 599 600 void ieee802154_if_remove(struct ieee802154_sub_if_data *sdata) 601 { 602 ASSERT_RTNL(); 603 604 mutex_lock(&sdata->local->iflist_mtx); 605 list_del_rcu(&sdata->list); 606 mutex_unlock(&sdata->local->iflist_mtx); 607 608 synchronize_rcu(); 609 unregister_netdevice(sdata->dev); 610 } 611 612 void ieee802154_remove_interfaces(struct ieee802154_local *local) 613 { 614 struct ieee802154_sub_if_data *sdata, *tmp; 615 616 mutex_lock(&local->iflist_mtx); 617 list_for_each_entry_safe(sdata, tmp, &local->interfaces, list) { 618 list_del(&sdata->list); 619 620 unregister_netdevice(sdata->dev); 621 } 622 mutex_unlock(&local->iflist_mtx); 623 } 624 625 static int netdev_notify(struct notifier_block *nb, 626 unsigned long state, void *ptr) 627 { 628 struct net_device *dev = netdev_notifier_info_to_dev(ptr); 629 struct ieee802154_sub_if_data *sdata; 630 631 if (state != NETDEV_CHANGENAME) 632 return NOTIFY_DONE; 633 634 if (!dev->ieee802154_ptr || !dev->ieee802154_ptr->wpan_phy) 635 return NOTIFY_DONE; 636 637 if (dev->ieee802154_ptr->wpan_phy->privid != mac802154_wpan_phy_privid) 638 return NOTIFY_DONE; 639 640 sdata = IEEE802154_DEV_TO_SUB_IF(dev); 641 memcpy(sdata->name, dev->name, IFNAMSIZ); 642 643 return NOTIFY_OK; 644 } 645 646 static struct notifier_block mac802154_netdev_notifier = { 647 .notifier_call = netdev_notify, 648 }; 649 650 int ieee802154_iface_init(void) 651 { 652 return register_netdevice_notifier(&mac802154_netdev_notifier); 653 } 654 655 void ieee802154_iface_exit(void) 656 { 657 unregister_netdevice_notifier(&mac802154_netdev_notifier); 658 } 659