1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * IEEE 802.15.4 scanning management 4 * 5 * Copyright (C) 2021 Qorvo US, Inc 6 * Authors: 7 * - David Girault <david.girault@qorvo.com> 8 * - Miquel Raynal <miquel.raynal@bootlin.com> 9 */ 10 11 #include <linux/module.h> 12 #include <linux/rtnetlink.h> 13 #include <net/mac802154.h> 14 15 #include "ieee802154_i.h" 16 #include "driver-ops.h" 17 #include "../ieee802154/nl802154.h" 18 19 #define IEEE802154_BEACON_MHR_SZ 13 20 #define IEEE802154_BEACON_PL_SZ 4 21 #define IEEE802154_MAC_CMD_MHR_SZ 23 22 #define IEEE802154_MAC_CMD_PL_SZ 1 23 #define IEEE802154_BEACON_SKB_SZ (IEEE802154_BEACON_MHR_SZ + \ 24 IEEE802154_BEACON_PL_SZ) 25 #define IEEE802154_MAC_CMD_SKB_SZ (IEEE802154_MAC_CMD_MHR_SZ + \ 26 IEEE802154_MAC_CMD_PL_SZ) 27 28 /* mac802154_scan_cleanup_locked() must be called upon scan completion or abort. 29 * - Completions are asynchronous, not locked by the rtnl and decided by the 30 * scan worker. 31 * - Aborts are decided by userspace, and locked by the rtnl. 32 * 33 * Concurrent modifications to the PHY, the interfaces or the hardware is in 34 * general prevented by the rtnl. So in most cases we don't need additional 35 * protection. 36 * 37 * However, the scan worker get's triggered without anybody noticing and thus we 38 * must ensure the presence of the devices as well as data consistency: 39 * - The sub-interface and device driver module get both their reference 40 * counters incremented whenever we start a scan, so they cannot disappear 41 * during operation. 42 * - Data consistency is achieved by the use of rcu protected pointers. 43 */ 44 static int mac802154_scan_cleanup_locked(struct ieee802154_local *local, 45 struct ieee802154_sub_if_data *sdata, 46 bool aborted) 47 { 48 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 49 struct wpan_phy *wpan_phy = local->phy; 50 struct cfg802154_scan_request *request; 51 u8 arg; 52 53 /* Prevent any further use of the scan request */ 54 clear_bit(IEEE802154_IS_SCANNING, &local->ongoing); 55 cancel_delayed_work(&local->scan_work); 56 request = rcu_replace_pointer(local->scan_req, NULL, 1); 57 if (!request) 58 return 0; 59 kvfree_rcu_mightsleep(request); 60 61 /* Advertize first, while we know the devices cannot be removed */ 62 if (aborted) 63 arg = NL802154_SCAN_DONE_REASON_ABORTED; 64 else 65 arg = NL802154_SCAN_DONE_REASON_FINISHED; 66 nl802154_scan_done(wpan_phy, wpan_dev, arg); 67 68 /* Cleanup software stack */ 69 ieee802154_mlme_op_post(local); 70 71 /* Set the hardware back in its original state */ 72 drv_set_channel(local, wpan_phy->current_page, 73 wpan_phy->current_channel); 74 ieee802154_configure_durations(wpan_phy, wpan_phy->current_page, 75 wpan_phy->current_channel); 76 drv_stop(local); 77 synchronize_net(); 78 sdata->required_filtering = sdata->iface_default_filtering; 79 drv_start(local, sdata->required_filtering, &local->addr_filt); 80 81 return 0; 82 } 83 84 int mac802154_abort_scan_locked(struct ieee802154_local *local, 85 struct ieee802154_sub_if_data *sdata) 86 { 87 ASSERT_RTNL(); 88 89 if (!mac802154_is_scanning(local)) 90 return -ESRCH; 91 92 return mac802154_scan_cleanup_locked(local, sdata, true); 93 } 94 95 static unsigned int mac802154_scan_get_channel_time(u8 duration_order, 96 u8 symbol_duration) 97 { 98 u64 base_super_frame_duration = (u64)symbol_duration * 99 IEEE802154_SUPERFRAME_PERIOD * IEEE802154_SLOT_PERIOD; 100 101 return usecs_to_jiffies(base_super_frame_duration * 102 (BIT(duration_order) + 1)); 103 } 104 105 static void mac802154_flush_queued_beacons(struct ieee802154_local *local) 106 { 107 struct cfg802154_mac_pkt *mac_pkt, *tmp; 108 109 list_for_each_entry_safe(mac_pkt, tmp, &local->rx_beacon_list, node) { 110 list_del(&mac_pkt->node); 111 kfree_skb(mac_pkt->skb); 112 kfree(mac_pkt); 113 } 114 } 115 116 static void 117 mac802154_scan_get_next_channel(struct ieee802154_local *local, 118 struct cfg802154_scan_request *scan_req, 119 u8 *channel) 120 { 121 (*channel)++; 122 *channel = find_next_bit((const unsigned long *)&scan_req->channels, 123 IEEE802154_MAX_CHANNEL + 1, 124 *channel); 125 } 126 127 static int mac802154_scan_find_next_chan(struct ieee802154_local *local, 128 struct cfg802154_scan_request *scan_req, 129 u8 page, u8 *channel) 130 { 131 mac802154_scan_get_next_channel(local, scan_req, channel); 132 if (*channel > IEEE802154_MAX_CHANNEL) 133 return -EINVAL; 134 135 return 0; 136 } 137 138 static int mac802154_scan_prepare_beacon_req(struct ieee802154_local *local) 139 { 140 memset(&local->scan_beacon_req, 0, sizeof(local->scan_beacon_req)); 141 local->scan_beacon_req.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD; 142 local->scan_beacon_req.mhr.fc.dest_addr_mode = IEEE802154_SHORT_ADDRESSING; 143 local->scan_beacon_req.mhr.fc.version = IEEE802154_2003_STD; 144 local->scan_beacon_req.mhr.fc.source_addr_mode = IEEE802154_NO_ADDRESSING; 145 local->scan_beacon_req.mhr.dest.mode = IEEE802154_ADDR_SHORT; 146 local->scan_beacon_req.mhr.dest.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); 147 local->scan_beacon_req.mhr.dest.short_addr = cpu_to_le16(IEEE802154_ADDR_BROADCAST); 148 local->scan_beacon_req.mac_pl.cmd_id = IEEE802154_CMD_BEACON_REQ; 149 150 return 0; 151 } 152 153 static int mac802154_transmit_beacon_req(struct ieee802154_local *local, 154 struct ieee802154_sub_if_data *sdata) 155 { 156 struct sk_buff *skb; 157 int ret; 158 159 skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ, GFP_KERNEL); 160 if (!skb) 161 return -ENOBUFS; 162 163 skb->dev = sdata->dev; 164 165 ret = ieee802154_mac_cmd_push(skb, &local->scan_beacon_req, NULL, 0); 166 if (ret) { 167 kfree_skb(skb); 168 return ret; 169 } 170 171 return ieee802154_mlme_tx(local, sdata, skb); 172 } 173 174 void mac802154_scan_worker(struct work_struct *work) 175 { 176 struct ieee802154_local *local = 177 container_of(work, struct ieee802154_local, scan_work.work); 178 struct cfg802154_scan_request *scan_req; 179 enum nl802154_scan_types scan_req_type; 180 struct ieee802154_sub_if_data *sdata; 181 unsigned int scan_duration = 0; 182 netdevice_tracker dev_tracker; 183 struct wpan_phy *wpan_phy; 184 u8 scan_req_duration; 185 u8 page, channel; 186 int ret; 187 188 /* Ensure the device receiver is turned off when changing channels 189 * because there is no atomic way to change the channel and know on 190 * which one a beacon might have been received. 191 */ 192 drv_stop(local); 193 synchronize_net(); 194 mac802154_flush_queued_beacons(local); 195 196 rcu_read_lock(); 197 scan_req = rcu_dereference(local->scan_req); 198 if (unlikely(!scan_req)) { 199 rcu_read_unlock(); 200 return; 201 } 202 203 sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(scan_req->wpan_dev); 204 205 /* Wait an arbitrary amount of time in case we cannot use the device */ 206 if (local->suspended || !ieee802154_sdata_running(sdata)) { 207 rcu_read_unlock(); 208 queue_delayed_work(local->mac_wq, &local->scan_work, 209 msecs_to_jiffies(1000)); 210 return; 211 } 212 213 /* 214 * sdata->dev is dereferenced below after rcu_read_unlock() and outside 215 * the rtnl, and a concurrent DEL_INTERFACE / PHY teardown can free it 216 * asynchronously from netdev_run_todo(). Pin it with a reference taken 217 * while the RCU read lock is still held, and drop it at every exit. 218 */ 219 netdev_hold(sdata->dev, &dev_tracker, GFP_ATOMIC); 220 221 wpan_phy = scan_req->wpan_phy; 222 scan_req_type = scan_req->type; 223 scan_req_duration = scan_req->duration; 224 225 /* Look for the next valid chan */ 226 page = local->scan_page; 227 channel = local->scan_channel; 228 do { 229 ret = mac802154_scan_find_next_chan(local, scan_req, page, &channel); 230 if (ret) { 231 rcu_read_unlock(); 232 goto end_scan; 233 } 234 } while (!ieee802154_chan_is_valid(scan_req->wpan_phy, page, channel)); 235 236 rcu_read_unlock(); 237 238 /* Bypass the stack on purpose when changing the channel */ 239 rtnl_lock(); 240 ret = drv_set_channel(local, page, channel); 241 rtnl_unlock(); 242 if (ret) { 243 dev_err(&sdata->dev->dev, 244 "Channel change failure during scan, aborting (%d)\n", ret); 245 goto end_scan; 246 } 247 248 local->scan_page = page; 249 local->scan_channel = channel; 250 251 rtnl_lock(); 252 ret = drv_start(local, IEEE802154_FILTERING_3_SCAN, &local->addr_filt); 253 rtnl_unlock(); 254 if (ret) { 255 dev_err(&sdata->dev->dev, 256 "Restarting failure after channel change, aborting (%d)\n", ret); 257 goto end_scan; 258 } 259 260 if (scan_req_type == NL802154_SCAN_ACTIVE) { 261 ret = mac802154_transmit_beacon_req(local, sdata); 262 if (ret) 263 dev_err(&sdata->dev->dev, 264 "Error when transmitting beacon request (%d)\n", ret); 265 } 266 267 ieee802154_configure_durations(wpan_phy, page, channel); 268 scan_duration = mac802154_scan_get_channel_time(scan_req_duration, 269 wpan_phy->symbol_duration); 270 dev_dbg(&sdata->dev->dev, 271 "Scan page %u channel %u for %ums\n", 272 page, channel, jiffies_to_msecs(scan_duration)); 273 queue_delayed_work(local->mac_wq, &local->scan_work, scan_duration); 274 netdev_put(sdata->dev, &dev_tracker); 275 return; 276 277 end_scan: 278 rtnl_lock(); 279 mac802154_scan_cleanup_locked(local, sdata, false); 280 rtnl_unlock(); 281 netdev_put(sdata->dev, &dev_tracker); 282 } 283 284 int mac802154_trigger_scan_locked(struct ieee802154_sub_if_data *sdata, 285 struct cfg802154_scan_request *request) 286 { 287 struct ieee802154_local *local = sdata->local; 288 289 ASSERT_RTNL(); 290 291 if (mac802154_is_scanning(local)) 292 return -EBUSY; 293 294 if (request->type != NL802154_SCAN_PASSIVE && 295 request->type != NL802154_SCAN_ACTIVE) 296 return -EOPNOTSUPP; 297 298 /* Store scanning parameters */ 299 rcu_assign_pointer(local->scan_req, request); 300 301 /* Software scanning requires to set promiscuous mode, so we need to 302 * pause the Tx queue during the entire operation. 303 */ 304 ieee802154_mlme_op_pre(local); 305 306 sdata->required_filtering = IEEE802154_FILTERING_3_SCAN; 307 local->scan_page = request->page; 308 local->scan_channel = -1; 309 set_bit(IEEE802154_IS_SCANNING, &local->ongoing); 310 if (request->type == NL802154_SCAN_ACTIVE) 311 mac802154_scan_prepare_beacon_req(local); 312 313 nl802154_scan_started(request->wpan_phy, request->wpan_dev); 314 315 queue_delayed_work(local->mac_wq, &local->scan_work, 0); 316 317 return 0; 318 } 319 320 int mac802154_process_beacon(struct ieee802154_local *local, 321 struct sk_buff *skb, 322 u8 page, u8 channel) 323 { 324 struct ieee802154_beacon_hdr *bh = (void *)skb->data; 325 struct ieee802154_addr *src = &mac_cb(skb)->source; 326 struct cfg802154_scan_request *scan_req; 327 struct ieee802154_coord_desc desc; 328 329 if (skb->len != sizeof(*bh)) 330 return -EINVAL; 331 332 if (unlikely(src->mode == IEEE802154_ADDR_NONE)) 333 return -EINVAL; 334 335 dev_dbg(&skb->dev->dev, 336 "BEACON received on page %u channel %u\n", 337 page, channel); 338 339 memcpy(&desc.addr, src, sizeof(desc.addr)); 340 desc.page = page; 341 desc.channel = channel; 342 desc.link_quality = mac_cb(skb)->lqi; 343 desc.superframe_spec = get_unaligned_le16(skb->data); 344 desc.gts_permit = bh->gts_permit; 345 346 trace_802154_scan_event(&desc); 347 348 rcu_read_lock(); 349 scan_req = rcu_dereference(local->scan_req); 350 if (likely(scan_req)) 351 nl802154_scan_event(scan_req->wpan_phy, scan_req->wpan_dev, &desc); 352 rcu_read_unlock(); 353 354 return 0; 355 } 356 357 static int mac802154_transmit_beacon(struct ieee802154_local *local, 358 struct wpan_dev *wpan_dev) 359 { 360 struct cfg802154_beacon_request *beacon_req; 361 struct ieee802154_sub_if_data *sdata; 362 struct sk_buff *skb; 363 int ret; 364 365 /* Update the sequence number */ 366 local->beacon.mhr.seq = atomic_inc_return(&wpan_dev->bsn) & 0xFF; 367 368 skb = alloc_skb(IEEE802154_BEACON_SKB_SZ, GFP_KERNEL); 369 if (!skb) 370 return -ENOBUFS; 371 372 rcu_read_lock(); 373 beacon_req = rcu_dereference(local->beacon_req); 374 if (unlikely(!beacon_req)) { 375 rcu_read_unlock(); 376 kfree_skb(skb); 377 return -EINVAL; 378 } 379 380 sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev); 381 skb->dev = sdata->dev; 382 383 rcu_read_unlock(); 384 385 ret = ieee802154_beacon_push(skb, &local->beacon); 386 if (ret) { 387 kfree_skb(skb); 388 return ret; 389 } 390 391 /* Using the MLME transmission helper for sending beacons is a bit 392 * overkill because we do not really care about the final outcome. 393 * 394 * Even though, going through the whole net stack with a regular 395 * dev_queue_xmit() is not relevant either because we want beacons to be 396 * sent "now" rather than go through the whole net stack scheduling 397 * (qdisc & co). 398 * 399 * Finally, using ieee802154_subif_start_xmit() would only be an option 400 * if we had a generic transmit helper which would acquire the 401 * HARD_TX_LOCK() to prevent buffer handling conflicts with regular 402 * packets. 403 * 404 * So for now we keep it simple and send beacons with our MLME helper, 405 * even if it stops the ieee802154 queue entirely during these 406 * transmissions, wich anyway does not have a huge impact on the 407 * performances given the current design of the stack. 408 */ 409 return ieee802154_mlme_tx(local, sdata, skb); 410 } 411 412 void mac802154_beacon_worker(struct work_struct *work) 413 { 414 struct ieee802154_local *local = 415 container_of(work, struct ieee802154_local, beacon_work.work); 416 struct cfg802154_beacon_request *beacon_req; 417 struct ieee802154_sub_if_data *sdata; 418 struct wpan_dev *wpan_dev; 419 u8 interval; 420 int ret; 421 422 rcu_read_lock(); 423 beacon_req = rcu_dereference(local->beacon_req); 424 if (unlikely(!beacon_req)) { 425 rcu_read_unlock(); 426 return; 427 } 428 429 sdata = IEEE802154_WPAN_DEV_TO_SUB_IF(beacon_req->wpan_dev); 430 431 /* Wait an arbitrary amount of time in case we cannot use the device */ 432 if (local->suspended || !ieee802154_sdata_running(sdata)) { 433 rcu_read_unlock(); 434 queue_delayed_work(local->mac_wq, &local->beacon_work, 435 msecs_to_jiffies(1000)); 436 return; 437 } 438 439 wpan_dev = beacon_req->wpan_dev; 440 interval = beacon_req->interval; 441 442 rcu_read_unlock(); 443 444 dev_dbg(&sdata->dev->dev, "Sending beacon\n"); 445 ret = mac802154_transmit_beacon(local, wpan_dev); 446 if (ret) 447 dev_err(&sdata->dev->dev, 448 "Beacon could not be transmitted (%d)\n", ret); 449 450 if (interval < IEEE802154_ACTIVE_SCAN_DURATION) 451 queue_delayed_work(local->mac_wq, &local->beacon_work, 452 local->beacon_interval); 453 } 454 455 int mac802154_stop_beacons_locked(struct ieee802154_local *local, 456 struct ieee802154_sub_if_data *sdata) 457 { 458 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 459 struct cfg802154_beacon_request *request; 460 461 ASSERT_RTNL(); 462 463 if (!mac802154_is_beaconing(local)) 464 return -ESRCH; 465 466 clear_bit(IEEE802154_IS_BEACONING, &local->ongoing); 467 cancel_delayed_work(&local->beacon_work); 468 request = rcu_replace_pointer(local->beacon_req, NULL, 1); 469 if (!request) 470 return 0; 471 kvfree_rcu_mightsleep(request); 472 473 nl802154_beaconing_done(wpan_dev); 474 475 return 0; 476 } 477 478 int mac802154_send_beacons_locked(struct ieee802154_sub_if_data *sdata, 479 struct cfg802154_beacon_request *request) 480 { 481 struct ieee802154_local *local = sdata->local; 482 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 483 484 ASSERT_RTNL(); 485 486 if (mac802154_is_beaconing(local)) 487 mac802154_stop_beacons_locked(local, sdata); 488 489 /* Store beaconing parameters */ 490 rcu_assign_pointer(local->beacon_req, request); 491 492 set_bit(IEEE802154_IS_BEACONING, &local->ongoing); 493 494 memset(&local->beacon, 0, sizeof(local->beacon)); 495 local->beacon.mhr.fc.type = IEEE802154_FC_TYPE_BEACON; 496 local->beacon.mhr.fc.security_enabled = 0; 497 local->beacon.mhr.fc.frame_pending = 0; 498 local->beacon.mhr.fc.ack_request = 0; 499 local->beacon.mhr.fc.intra_pan = 0; 500 local->beacon.mhr.fc.dest_addr_mode = IEEE802154_NO_ADDRESSING; 501 local->beacon.mhr.fc.version = IEEE802154_2003_STD; 502 local->beacon.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING; 503 atomic_set(&request->wpan_dev->bsn, -1); 504 local->beacon.mhr.source.mode = IEEE802154_ADDR_LONG; 505 local->beacon.mhr.source.pan_id = request->wpan_dev->pan_id; 506 local->beacon.mhr.source.extended_addr = request->wpan_dev->extended_addr; 507 local->beacon.mac_pl.beacon_order = request->interval; 508 if (request->interval <= IEEE802154_MAX_SCAN_DURATION) 509 local->beacon.mac_pl.superframe_order = request->interval; 510 local->beacon.mac_pl.final_cap_slot = 0xf; 511 local->beacon.mac_pl.battery_life_ext = 0; 512 local->beacon.mac_pl.pan_coordinator = !wpan_dev->parent; 513 local->beacon.mac_pl.assoc_permit = 1; 514 515 if (request->interval == IEEE802154_ACTIVE_SCAN_DURATION) 516 return 0; 517 518 /* Start the beacon work */ 519 local->beacon_interval = 520 mac802154_scan_get_channel_time(request->interval, 521 request->wpan_phy->symbol_duration); 522 queue_delayed_work(local->mac_wq, &local->beacon_work, 0); 523 524 return 0; 525 } 526 527 int mac802154_perform_association(struct ieee802154_sub_if_data *sdata, 528 struct ieee802154_pan_device *coord, 529 __le16 *short_addr) 530 { 531 u64 ceaddr = swab64((__force u64)coord->extended_addr); 532 struct ieee802154_association_req_frame frame = {}; 533 struct ieee802154_local *local = sdata->local; 534 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 535 struct sk_buff *skb; 536 int ret; 537 538 frame.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD; 539 frame.mhr.fc.security_enabled = 0; 540 frame.mhr.fc.frame_pending = 0; 541 frame.mhr.fc.ack_request = 1; /* We always expect an ack here */ 542 frame.mhr.fc.intra_pan = 0; 543 frame.mhr.fc.dest_addr_mode = (coord->mode == IEEE802154_ADDR_LONG) ? 544 IEEE802154_EXTENDED_ADDRESSING : IEEE802154_SHORT_ADDRESSING; 545 frame.mhr.fc.version = IEEE802154_2003_STD; 546 frame.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING; 547 frame.mhr.source.mode = IEEE802154_ADDR_LONG; 548 frame.mhr.source.pan_id = cpu_to_le16(IEEE802154_PANID_BROADCAST); 549 frame.mhr.source.extended_addr = wpan_dev->extended_addr; 550 frame.mhr.dest.mode = coord->mode; 551 frame.mhr.dest.pan_id = coord->pan_id; 552 if (coord->mode == IEEE802154_ADDR_LONG) 553 frame.mhr.dest.extended_addr = coord->extended_addr; 554 else 555 frame.mhr.dest.short_addr = coord->short_addr; 556 frame.mhr.seq = atomic_inc_return(&wpan_dev->dsn) & 0xFF; 557 frame.mac_pl.cmd_id = IEEE802154_CMD_ASSOCIATION_REQ; 558 frame.assoc_req_pl.device_type = 1; 559 frame.assoc_req_pl.power_source = 1; 560 frame.assoc_req_pl.rx_on_when_idle = 1; 561 frame.assoc_req_pl.alloc_addr = 1; 562 563 skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ + sizeof(frame.assoc_req_pl), 564 GFP_KERNEL); 565 if (!skb) 566 return -ENOBUFS; 567 568 skb->dev = sdata->dev; 569 570 ret = ieee802154_mac_cmd_push(skb, &frame, &frame.assoc_req_pl, 571 sizeof(frame.assoc_req_pl)); 572 if (ret) { 573 kfree_skb(skb); 574 return ret; 575 } 576 577 local->assoc_dev = coord; 578 reinit_completion(&local->assoc_done); 579 set_bit(IEEE802154_IS_ASSOCIATING, &local->ongoing); 580 581 ret = ieee802154_mlme_tx_one_locked(local, sdata, skb); 582 if (ret) { 583 if (ret > 0) 584 ret = (ret == IEEE802154_NO_ACK) ? -EREMOTEIO : -EIO; 585 dev_warn(&sdata->dev->dev, 586 "No ASSOC REQ ACK received from %8phC\n", &ceaddr); 587 goto clear_assoc; 588 } 589 590 ret = wait_for_completion_killable_timeout(&local->assoc_done, 10 * HZ); 591 if (ret <= 0) { 592 dev_warn(&sdata->dev->dev, 593 "No ASSOC RESP received from %8phC\n", &ceaddr); 594 ret = -ETIMEDOUT; 595 goto clear_assoc; 596 } 597 598 if (local->assoc_status != IEEE802154_ASSOCIATION_SUCCESSFUL) { 599 if (local->assoc_status == IEEE802154_PAN_AT_CAPACITY) 600 ret = -ERANGE; 601 else 602 ret = -EPERM; 603 604 dev_warn(&sdata->dev->dev, 605 "Negative ASSOC RESP received from %8phC: %s\n", &ceaddr, 606 local->assoc_status == IEEE802154_PAN_AT_CAPACITY ? 607 "PAN at capacity" : "access denied"); 608 goto clear_assoc; 609 } 610 611 ret = 0; 612 *short_addr = local->assoc_addr; 613 614 clear_assoc: 615 clear_bit(IEEE802154_IS_ASSOCIATING, &local->ongoing); 616 local->assoc_dev = NULL; 617 618 return ret; 619 } 620 621 int mac802154_process_association_resp(struct ieee802154_sub_if_data *sdata, 622 struct sk_buff *skb) 623 { 624 struct ieee802154_addr *src = &mac_cb(skb)->source; 625 struct ieee802154_addr *dest = &mac_cb(skb)->dest; 626 u64 deaddr = swab64((__force u64)dest->extended_addr); 627 struct ieee802154_local *local = sdata->local; 628 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 629 struct ieee802154_assoc_resp_pl resp_pl = {}; 630 631 if (skb->len != sizeof(resp_pl)) 632 return -EINVAL; 633 634 if (unlikely(src->mode != IEEE802154_EXTENDED_ADDRESSING || 635 dest->mode != IEEE802154_EXTENDED_ADDRESSING)) 636 return -EINVAL; 637 638 if (unlikely(dest->extended_addr != wpan_dev->extended_addr || 639 src->extended_addr != local->assoc_dev->extended_addr)) 640 return -ENODEV; 641 642 memcpy(&resp_pl, skb->data, sizeof(resp_pl)); 643 local->assoc_addr = resp_pl.short_addr; 644 local->assoc_status = resp_pl.status; 645 646 dev_dbg(&skb->dev->dev, 647 "ASSOC RESP 0x%x received from %8phC, getting short address %04x\n", 648 local->assoc_status, &deaddr, local->assoc_addr); 649 650 complete(&local->assoc_done); 651 652 return 0; 653 } 654 655 int mac802154_send_disassociation_notif(struct ieee802154_sub_if_data *sdata, 656 struct ieee802154_pan_device *target, 657 u8 reason) 658 { 659 struct ieee802154_disassociation_notif_frame frame = {}; 660 u64 teaddr = swab64((__force u64)target->extended_addr); 661 struct ieee802154_local *local = sdata->local; 662 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 663 struct sk_buff *skb; 664 int ret; 665 666 frame.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD; 667 frame.mhr.fc.security_enabled = 0; 668 frame.mhr.fc.frame_pending = 0; 669 frame.mhr.fc.ack_request = 1; 670 frame.mhr.fc.intra_pan = 1; 671 frame.mhr.fc.dest_addr_mode = (target->mode == IEEE802154_ADDR_LONG) ? 672 IEEE802154_EXTENDED_ADDRESSING : IEEE802154_SHORT_ADDRESSING; 673 frame.mhr.fc.version = IEEE802154_2003_STD; 674 frame.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING; 675 frame.mhr.source.mode = IEEE802154_ADDR_LONG; 676 frame.mhr.source.pan_id = wpan_dev->pan_id; 677 frame.mhr.source.extended_addr = wpan_dev->extended_addr; 678 frame.mhr.dest.mode = target->mode; 679 frame.mhr.dest.pan_id = wpan_dev->pan_id; 680 if (target->mode == IEEE802154_ADDR_LONG) 681 frame.mhr.dest.extended_addr = target->extended_addr; 682 else 683 frame.mhr.dest.short_addr = target->short_addr; 684 frame.mhr.seq = atomic_inc_return(&wpan_dev->dsn) & 0xFF; 685 frame.mac_pl.cmd_id = IEEE802154_CMD_DISASSOCIATION_NOTIFY; 686 frame.disassoc_pl = reason; 687 688 skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ + sizeof(frame.disassoc_pl), 689 GFP_KERNEL); 690 if (!skb) 691 return -ENOBUFS; 692 693 skb->dev = sdata->dev; 694 695 ret = ieee802154_mac_cmd_push(skb, &frame, &frame.disassoc_pl, 696 sizeof(frame.disassoc_pl)); 697 if (ret) { 698 kfree_skb(skb); 699 return ret; 700 } 701 702 ret = ieee802154_mlme_tx_one_locked(local, sdata, skb); 703 if (ret) { 704 dev_warn(&sdata->dev->dev, 705 "No DISASSOC ACK received from %8phC\n", &teaddr); 706 if (ret > 0) 707 ret = (ret == IEEE802154_NO_ACK) ? -EREMOTEIO : -EIO; 708 return ret; 709 } 710 711 dev_dbg(&sdata->dev->dev, "DISASSOC ACK received from %8phC\n", &teaddr); 712 return 0; 713 } 714 715 static int 716 mac802154_send_association_resp_locked(struct ieee802154_sub_if_data *sdata, 717 struct ieee802154_pan_device *target, 718 struct ieee802154_assoc_resp_pl *assoc_resp_pl) 719 { 720 u64 teaddr = swab64((__force u64)target->extended_addr); 721 struct ieee802154_association_resp_frame frame = {}; 722 struct ieee802154_local *local = sdata->local; 723 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 724 struct sk_buff *skb; 725 int ret; 726 727 frame.mhr.fc.type = IEEE802154_FC_TYPE_MAC_CMD; 728 frame.mhr.fc.security_enabled = 0; 729 frame.mhr.fc.frame_pending = 0; 730 frame.mhr.fc.ack_request = 1; /* We always expect an ack here */ 731 frame.mhr.fc.intra_pan = 1; 732 frame.mhr.fc.dest_addr_mode = IEEE802154_EXTENDED_ADDRESSING; 733 frame.mhr.fc.version = IEEE802154_2003_STD; 734 frame.mhr.fc.source_addr_mode = IEEE802154_EXTENDED_ADDRESSING; 735 frame.mhr.source.mode = IEEE802154_ADDR_LONG; 736 frame.mhr.source.extended_addr = wpan_dev->extended_addr; 737 frame.mhr.dest.mode = IEEE802154_ADDR_LONG; 738 frame.mhr.dest.pan_id = wpan_dev->pan_id; 739 frame.mhr.dest.extended_addr = target->extended_addr; 740 frame.mhr.seq = atomic_inc_return(&wpan_dev->dsn) & 0xFF; 741 frame.mac_pl.cmd_id = IEEE802154_CMD_ASSOCIATION_RESP; 742 743 skb = alloc_skb(IEEE802154_MAC_CMD_SKB_SZ + sizeof(*assoc_resp_pl), 744 GFP_KERNEL); 745 if (!skb) 746 return -ENOBUFS; 747 748 skb->dev = sdata->dev; 749 750 ret = ieee802154_mac_cmd_push(skb, &frame, assoc_resp_pl, 751 sizeof(*assoc_resp_pl)); 752 if (ret) { 753 kfree_skb(skb); 754 return ret; 755 } 756 757 ret = ieee802154_mlme_tx_locked(local, sdata, skb); 758 if (ret) { 759 dev_warn(&sdata->dev->dev, 760 "No ASSOC RESP ACK received from %8phC\n", &teaddr); 761 if (ret > 0) 762 ret = (ret == IEEE802154_NO_ACK) ? -EREMOTEIO : -EIO; 763 return ret; 764 } 765 766 return 0; 767 } 768 769 int mac802154_process_association_req(struct ieee802154_sub_if_data *sdata, 770 struct sk_buff *skb) 771 { 772 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 773 struct ieee802154_addr *src = &mac_cb(skb)->source; 774 struct ieee802154_addr *dest = &mac_cb(skb)->dest; 775 struct ieee802154_assoc_resp_pl assoc_resp_pl = {}; 776 struct ieee802154_assoc_req_pl assoc_req_pl; 777 struct ieee802154_pan_device *child, *exchild; 778 struct ieee802154_addr tmp = {}; 779 u64 ceaddr; 780 int ret; 781 782 if (skb->len != sizeof(assoc_req_pl)) 783 return -EINVAL; 784 785 if (unlikely(src->mode != IEEE802154_EXTENDED_ADDRESSING)) 786 return -EINVAL; 787 788 if (unlikely(dest->pan_id != wpan_dev->pan_id)) 789 return -ENODEV; 790 791 if (dest->mode == IEEE802154_EXTENDED_ADDRESSING && 792 unlikely(dest->extended_addr != wpan_dev->extended_addr)) 793 return -ENODEV; 794 else if (dest->mode == IEEE802154_SHORT_ADDRESSING && 795 unlikely(dest->short_addr != wpan_dev->short_addr)) 796 return -ENODEV; 797 798 if (wpan_dev->parent) { 799 dev_dbg(&sdata->dev->dev, 800 "Ignoring ASSOC REQ, not the PAN coordinator\n"); 801 return -ENODEV; 802 } 803 804 mutex_lock(&wpan_dev->association_lock); 805 806 memcpy(&assoc_req_pl, skb->data, sizeof(assoc_req_pl)); 807 if (assoc_req_pl.assoc_type) { 808 dev_err(&skb->dev->dev, "Fast associations not supported yet\n"); 809 ret = -EOPNOTSUPP; 810 goto unlock; 811 } 812 813 child = kzalloc_obj(*child); 814 if (!child) { 815 ret = -ENOMEM; 816 goto unlock; 817 } 818 819 child->extended_addr = src->extended_addr; 820 child->mode = IEEE802154_EXTENDED_ADDRESSING; 821 ceaddr = swab64((__force u64)child->extended_addr); 822 823 if (wpan_dev->nchildren >= wpan_dev->max_associations) { 824 if (!wpan_dev->max_associations) 825 assoc_resp_pl.status = IEEE802154_PAN_ACCESS_DENIED; 826 else 827 assoc_resp_pl.status = IEEE802154_PAN_AT_CAPACITY; 828 assoc_resp_pl.short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST); 829 dev_dbg(&sdata->dev->dev, 830 "Refusing ASSOC REQ from child %8phC, %s\n", &ceaddr, 831 assoc_resp_pl.status == IEEE802154_PAN_ACCESS_DENIED ? 832 "access denied" : "too many children"); 833 } else { 834 assoc_resp_pl.status = IEEE802154_ASSOCIATION_SUCCESSFUL; 835 if (assoc_req_pl.alloc_addr) { 836 assoc_resp_pl.short_addr = cfg802154_get_free_short_addr(wpan_dev); 837 child->mode = IEEE802154_SHORT_ADDRESSING; 838 } else { 839 assoc_resp_pl.short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC); 840 } 841 child->short_addr = assoc_resp_pl.short_addr; 842 dev_dbg(&sdata->dev->dev, 843 "Accepting ASSOC REQ from child %8phC, providing short address 0x%04x\n", 844 &ceaddr, le16_to_cpu(child->short_addr)); 845 } 846 847 ret = mac802154_send_association_resp_locked(sdata, child, &assoc_resp_pl); 848 if (ret || assoc_resp_pl.status != IEEE802154_ASSOCIATION_SUCCESSFUL) { 849 kfree(child); 850 goto unlock; 851 } 852 853 dev_dbg(&sdata->dev->dev, 854 "Successful association with new child %8phC\n", &ceaddr); 855 856 /* Ensure this child is not already associated (might happen due to 857 * retransmissions), in this case drop the ex structure. 858 */ 859 tmp.mode = child->mode; 860 tmp.extended_addr = child->extended_addr; 861 exchild = cfg802154_device_is_child(wpan_dev, &tmp); 862 if (exchild) { 863 dev_dbg(&sdata->dev->dev, 864 "Child %8phC was already known\n", &ceaddr); 865 list_del(&exchild->node); 866 } 867 868 list_add(&child->node, &wpan_dev->children); 869 wpan_dev->nchildren++; 870 871 unlock: 872 mutex_unlock(&wpan_dev->association_lock); 873 return ret; 874 } 875 876 int mac802154_process_disassociation_notif(struct ieee802154_sub_if_data *sdata, 877 struct sk_buff *skb) 878 { 879 struct ieee802154_addr *src = &mac_cb(skb)->source; 880 struct ieee802154_addr *dest = &mac_cb(skb)->dest; 881 struct wpan_dev *wpan_dev = &sdata->wpan_dev; 882 struct ieee802154_pan_device *child; 883 struct ieee802154_addr target; 884 bool parent; 885 u64 teaddr; 886 887 if (skb->len != sizeof(u8)) 888 return -EINVAL; 889 890 if (unlikely(src->mode != IEEE802154_EXTENDED_ADDRESSING)) 891 return -EINVAL; 892 893 if (dest->mode == IEEE802154_EXTENDED_ADDRESSING && 894 unlikely(dest->extended_addr != wpan_dev->extended_addr)) 895 return -ENODEV; 896 else if (dest->mode == IEEE802154_SHORT_ADDRESSING && 897 unlikely(dest->short_addr != wpan_dev->short_addr)) 898 return -ENODEV; 899 900 if (dest->pan_id != wpan_dev->pan_id) 901 return -ENODEV; 902 903 target.mode = IEEE802154_EXTENDED_ADDRESSING; 904 target.extended_addr = src->extended_addr; 905 teaddr = swab64((__force u64)target.extended_addr); 906 dev_dbg(&skb->dev->dev, "Processing DISASSOC NOTIF from %8phC\n", &teaddr); 907 908 mutex_lock(&wpan_dev->association_lock); 909 parent = cfg802154_device_is_parent(wpan_dev, &target); 910 if (!parent) 911 child = cfg802154_device_is_child(wpan_dev, &target); 912 if (!parent && !child) { 913 mutex_unlock(&wpan_dev->association_lock); 914 return -EINVAL; 915 } 916 917 if (parent) { 918 kfree(wpan_dev->parent); 919 wpan_dev->parent = NULL; 920 } else { 921 list_del(&child->node); 922 kfree(child); 923 wpan_dev->nchildren--; 924 } 925 926 mutex_unlock(&wpan_dev->association_lock); 927 928 return 0; 929 } 930