1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * mac80211_hwsim_nan - NAN software simulation for mac80211_hwsim 4 * Copyright (C) 2025-2026 Intel Corporation 5 */ 6 7 #include <net/cfg80211.h> 8 #include "mac80211_hwsim_i.h" 9 10 /* Defined as the lower 23 bits being zero */ 11 #define DW0_TSF_MASK GENMASK(22, 0) 12 13 /* DWs are repeated every 512 TUs */ 14 #define DWST_TU 512 15 #define DWST_TSF_MASK (ieee80211_tu_to_usec(DWST_TU) - 1) 16 17 #define SLOT_TU 16 18 #define SLOT_TSF_MASK (ieee80211_tu_to_usec(DWST_TU) - 1) 19 20 /* The 2.4 GHz DW is at the start, the 5 GHz is in slot 8 (after 128 TUs) */ 21 #define DW_5G_OFFSET_TU 128 22 23 #define SLOT_24GHZ_DW 0 24 #define SLOT_5GHZ_DW (DW_5G_OFFSET_TU / SLOT_TU) 25 26 /* The special DW0 happens every 16 DWSTs (8192 TUs) */ 27 static_assert(16 * DWST_TU * 1024 == 8192 * 1024); 28 static_assert(DW0_TSF_MASK + 1 == 8192 * 1024); 29 30 /* warmup phase should be 120 seconds, which is approximately 225 DWSTs */ 31 #define NAN_WARMUP_DWST 225 32 33 #define NAN_RSSI_CLOSE (-60) 34 #define NAN_RSSI_MIDDLE (-75) 35 36 /* Size of the NAN vendor element carrying the mandatory attributes */ 37 #define NAN_BEACON_ELEM_LEN 27 38 39 /* Space left in the NAN vendor element for the configured attributes */ 40 #define NAN_BEACON_EXTRA_ATTRS_LEN (255 - (NAN_BEACON_ELEM_LEN - 2)) 41 42 /* Quiet time at the end of each slot where TX is suppressed */ 43 #define NAN_CHAN_SWITCH_TIME_US 256 44 45 struct hwsim_nan_sta_iter_ctx { 46 struct ieee80211_hw *hw; 47 bool can_tx; 48 }; 49 50 struct hwsim_nan_mcast_data_iter_ctx { 51 struct ieee80211_hw *hw; 52 struct ieee80211_vif *vif; 53 size_t n_vif_sta; 54 size_t n_sta_can_tx; 55 }; 56 57 static void mac80211_hwsim_nan_resume_txqs(struct mac80211_hwsim_data *data); 58 59 static u64 hwsim_nan_get_timer_tsf(struct mac80211_hwsim_data *data) 60 { 61 ktime_t expires = hrtimer_get_expires(&data->nan.slot_timer); 62 63 return mac80211_hwsim_boottime_to_tsf(data, expires); 64 } 65 66 static u8 hwsim_nan_slot_from_tsf(u64 tsf) 67 { 68 return (tsf & DWST_TSF_MASK) / ieee80211_tu_to_usec(SLOT_TU); 69 } 70 71 static u64 hwsim_nan_encode_master_rank(u8 master_pref, u8 random_factor, 72 const u8 *addr) 73 { 74 return ((u64)master_pref << 56) + 75 ((u64)random_factor << 48) + 76 ((u64)addr[5] << 40) + 77 ((u64)addr[4] << 32) + 78 ((u64)addr[3] << 24) + 79 ((u64)addr[2] << 16) + 80 ((u64)addr[1] << 8) + 81 ((u64)addr[0] << 0); 82 } 83 84 static u64 hwsim_nan_get_master_rank(struct mac80211_hwsim_data *data) 85 { 86 u8 master_pref = 0; 87 u8 random_factor = 0; 88 89 if (data->nan.phase == MAC80211_HWSIM_NAN_PHASE_UP) { 90 master_pref = data->nan.master_pref; 91 random_factor = data->nan.random_factor; 92 } 93 94 return hwsim_nan_encode_master_rank(master_pref, random_factor, 95 data->nan.device_vif->addr); 96 } 97 98 static bool mac80211_hwsim_nan_is_dw_slot(struct mac80211_hwsim_data *data, 99 u8 slot) 100 { 101 return slot == SLOT_24GHZ_DW || 102 (slot == SLOT_5GHZ_DW && 103 (data->nan.bands & BIT(NL80211_BAND_5GHZ))); 104 } 105 106 static bool 107 hwsim_nan_rx_chandef_compatible(struct mac80211_hwsim_data *data, u8 slot, 108 struct ieee80211_channel *rx_chan, u8 rx_bw) 109 { 110 static const int bw_to_mhz[] = { 111 [RATE_INFO_BW_20] = 20, [RATE_INFO_BW_40] = 40, 112 [RATE_INFO_BW_80] = 80, [RATE_INFO_BW_160] = 160, 113 }; 114 struct cfg80211_chan_def sched_chandef; 115 int rx_mhz, sched_mhz; 116 117 scoped_guard(spinlock_bh, &data->nan.state_lock) 118 sched_chandef = data->nan.local_sched[slot]; 119 120 if (!sched_chandef.chan || 121 sched_chandef.chan->center_freq != rx_chan->center_freq) 122 return false; 123 124 if (rx_bw >= ARRAY_SIZE(bw_to_mhz) || !bw_to_mhz[rx_bw]) 125 return false; 126 127 rx_mhz = bw_to_mhz[rx_bw]; 128 sched_mhz = cfg80211_chandef_get_width(&sched_chandef); 129 130 /* Accept RX at narrower or equal bandwidth */ 131 return rx_mhz <= sched_mhz; 132 } 133 134 static bool hwsim_nan_peer_present_in_dw(struct hwsim_sta_priv *sp, u64 tsf) 135 { 136 u8 slot = hwsim_nan_slot_from_tsf(tsf); 137 u8 cdw = 0; 138 u8 dw_index, wake_interval; 139 u16 committed_dw; 140 141 scoped_guard(spinlock_bh, &sp->nan_sched.lock) 142 committed_dw = sp->nan_sched.committed_dw; 143 144 /* If peer doesn't advertise committed DW, assume presence in 145 * all 2.4 GHz DW slots 146 */ 147 if (!committed_dw) 148 return slot == SLOT_24GHZ_DW; 149 150 /* Get DW index (0-15) within the 16-DWST DW0 cycle */ 151 dw_index = (tsf / ieee80211_tu_to_usec(DWST_TU)) & 0xf; 152 153 /* Extract CDW for the appropriate band (spec Table 80) */ 154 if (slot == SLOT_24GHZ_DW) 155 cdw = committed_dw & 0x7; 156 else if (slot == SLOT_5GHZ_DW) 157 cdw = (committed_dw >> 3) & 0x7; 158 159 if (cdw == 0) 160 return false; 161 162 /* Peer wakes every 2^(cdw-1) DWs: 1, 2, 4, 8, or 16 */ 163 wake_interval = 1 << (cdw - 1); 164 165 return (dw_index % wake_interval) == 0; 166 } 167 168 static bool 169 hwsim_nan_peer_present_in_faw(struct hwsim_sta_priv *sp, 170 struct mac80211_hwsim_data *data, u8 slot) 171 { 172 struct cfg80211_chan_def local_chandef; 173 174 scoped_guard(spinlock_bh, &data->nan.state_lock) 175 local_chandef = data->nan.local_sched[slot]; 176 177 if (!local_chandef.chan) 178 return false; 179 180 scoped_guard(spinlock_bh, &sp->nan_sched.lock) { 181 for (int i = 0; i < CFG80211_NAN_MAX_PEER_MAPS; i++) { 182 struct cfg80211_chan_def *peer_chandef; 183 184 if (sp->nan_sched.maps[i].map_id == 185 CFG80211_NAN_INVALID_MAP_ID) 186 continue; 187 188 peer_chandef = &sp->nan_sched.maps[i].chans[slot]; 189 if (!peer_chandef->chan) 190 continue; 191 192 if (cfg80211_chandef_compatible(&local_chandef, 193 peer_chandef)) 194 return true; 195 } 196 } 197 198 return false; 199 } 200 201 static void 202 mac80211_hwsim_nan_schedule_slot(struct mac80211_hwsim_data *data, u8 slot, 203 bool discontinuity) 204 { 205 u64 tsf; 206 207 if (!discontinuity) 208 tsf = hwsim_nan_get_timer_tsf(data); 209 else 210 tsf = mac80211_hwsim_get_tsf(data->hw, data->nan.device_vif); 211 212 /* Only called by mac80211_hwsim_nan_dw_timer from softirq context */ 213 lockdep_assert_in_softirq(); 214 215 tsf &= ~DWST_TSF_MASK; 216 tsf += ieee80211_tu_to_usec(slot * SLOT_TU); 217 218 hrtimer_set_expires(&data->nan.slot_timer, 219 mac80211_hwsim_tsf_to_boottime(data, tsf)); 220 } 221 222 static bool hwsim_nan_beacon_has_service_ids(struct sk_buff *skb) 223 { 224 const struct ieee80211_mgmt *mgmt = (void *)skb->data; 225 const struct ieee80211_nan_attr *nan_attr; 226 const struct element *elem; 227 size_t len; 228 229 if (skb->len < offsetofend(struct ieee80211_mgmt, u.beacon) || 230 !ieee80211_is_beacon(mgmt->frame_control)) 231 return false; 232 233 len = skb->len - offsetofend(struct ieee80211_mgmt, u.beacon); 234 235 /* TODO: fragmented NAN elements are not supported */ 236 for_each_element_id(elem, WLAN_EID_VENDOR_SPECIFIC, 237 mgmt->u.beacon.variable, len) { 238 if (elem->datalen < 4 || 239 get_unaligned_be32(elem->data) != 240 (WLAN_OUI_WFA << 8 | WLAN_OUI_TYPE_WFA_NAN)) 241 continue; 242 243 for_each_nan_attr(nan_attr, elem->data + 4, elem->datalen - 4) 244 if (nan_attr->attr == NAN_ATTR_SERVICE_ID_LIST || 245 nan_attr->attr == 246 NAN_ATTR_SUBSCRIBE_SERVICE_ID_LIST) 247 return true; 248 } 249 250 return false; 251 } 252 253 void mac80211_hwsim_nan_rx(struct ieee80211_hw *hw, 254 struct sk_buff *skb) 255 { 256 struct mac80211_hwsim_data *data = hw->priv; 257 const struct ieee80211_mgmt *mgmt = (void *)skb->data; 258 struct element *nan_elem = (void *)mgmt->u.beacon.variable; 259 struct ieee80211_nan_anchor_master_info *ami = NULL; 260 const struct ieee80211_nan_attr *nan_attr; 261 struct ieee80211_rx_status rx_status; 262 bool joined_cluster = false; 263 bool adopt_tsf = false; 264 bool is_sync_beacon; 265 bool is_same_cluster; 266 u64 master_rank = 0; 267 ssize_t data_len; 268 u8 slot; 269 270 if (!ieee80211_is_nan_beacon(mgmt, skb->len)) 271 return; 272 273 data_len = skb->len - offsetofend(struct ieee80211_mgmt, u.beacon); 274 275 /* Copy the RX status to add a MAC timestamp if needed */ 276 memcpy(&rx_status, IEEE80211_SKB_RXCB(skb), 277 sizeof(struct ieee80211_rx_status)); 278 279 /* And deal with the lack of mac time stamp */ 280 if ((rx_status.flag & RX_FLAG_MACTIME) != RX_FLAG_MACTIME_START) { 281 u64 tsf = mac80211_hwsim_get_tsf(hw, data->nan.device_vif);; 282 283 /* In that case there should be no timestamp */ 284 WARN_ON_ONCE(rx_status.flag & RX_FLAG_MACTIME); 285 286 /* No mac timestamp, set current TSF for the frame end */ 287 rx_status.flag |= RX_FLAG_MACTIME_END; 288 rx_status.mactime = tsf; 289 290 /* And translate to the start for the rest of the code */ 291 rx_status.mactime = 292 ieee80211_calculate_rx_timestamp(hw, &rx_status, 293 skb->len, 0); 294 rx_status.flag &= ~RX_FLAG_MACTIME; 295 rx_status.flag |= RX_FLAG_MACTIME_START; 296 297 /* Match mac80211_hwsim_nan_receive, see comment there */ 298 slot = hwsim_nan_slot_from_tsf(tsf + 128); 299 } else { 300 slot = hwsim_nan_slot_from_tsf(rx_status.mactime); 301 } 302 303 /* 304 * Only sync on 2.4 GHz, which all NAN devices attend. The slot number 305 * is derived from the local TSF, so before being synchronized a 5 GHz 306 * beacon from a different device may fall into the local 2.4 GHz DW 307 * slot. Filter by frequency to keep such beacons out of master 308 * selection and synchronization. 309 */ 310 if (rx_status.freq != 2437) 311 return; 312 313 /* 314 * Ignore frames outside of the 2.4 GHz DW slot, unless in the initial 315 * SCAN phase or in Instant Communication, where discovery beacons are 316 * received outside the DW. 317 */ 318 if (slot != SLOT_24GHZ_DW) { 319 bool rx_outside_dw; 320 321 scoped_guard(spinlock_bh, &data->nan.state_lock) 322 rx_outside_dw = data->nan.instant_comm || 323 data->nan.phase == 324 MAC80211_HWSIM_NAN_PHASE_SCAN; 325 326 if (!rx_outside_dw) 327 return; 328 } 329 330 /* Just ignore low RSSI beacons that we cannot sync to */ 331 if (rx_status.signal < NAN_RSSI_MIDDLE) 332 return; 333 334 u8 *nan_defragmented __free(kfree) = kzalloc(data_len, GFP_ATOMIC); 335 if (!nan_defragmented) 336 return; 337 338 data_len = cfg80211_defragment_element(nan_elem, 339 mgmt->u.beacon.variable, 340 data_len, 341 nan_defragmented, data_len, 342 WLAN_EID_FRAGMENT); 343 344 if (data_len < 0) 345 return; 346 347 /* Assume it is a synchronization beacon if beacon_int is 512 TUs */ 348 is_sync_beacon = le16_to_cpu(mgmt->u.beacon.beacon_int) == DWST_TU; 349 is_same_cluster = ether_addr_equal(mgmt->bssid, data->nan.cluster_id); 350 351 for_each_nan_attr(nan_attr, nan_defragmented + 4, data_len - 4) { 352 if (nan_attr->attr == NAN_ATTR_MASTER_INDICATION && 353 le16_to_cpu(nan_attr->length) >= 354 sizeof(struct ieee80211_nan_master_indication)) { 355 struct ieee80211_nan_master_indication *mi = 356 (void *)nan_attr->data; 357 358 master_rank = 359 hwsim_nan_encode_master_rank(mi->master_pref, 360 mi->random_factor, 361 mgmt->sa); 362 } 363 364 if (nan_attr->attr == NAN_ATTR_CLUSTER_INFO && 365 le16_to_cpu(nan_attr->length) >= 366 sizeof(struct ieee80211_nan_anchor_master_info)) { 367 ami = (void *)nan_attr->data; 368 369 /* 370 * The AMBTT should be set to the beacon timestamp when 371 * the sender is the anchor master. We can simply 372 * modify the structure because we created a copy when 373 * defragmenting the NAN element. 374 */ 375 if (ami->hop_count == 0) 376 ami->ambtt = cpu_to_le32( 377 le64_to_cpu(mgmt->u.beacon.timestamp)); 378 } 379 } 380 381 /* Do the rest of the processing under lock */ 382 spin_lock_bh(&data->nan.state_lock); 383 384 /* 385 * sync beacon should be discarded if the master rank is the same 386 * and the AMBTT is older than 16 * 512 TUs compared to our own TSF. 387 * 388 * Subtract the AMBTT from the lowered TSF. If the AMBTT is older 389 * (smaller) then the calculation will not underflow. 390 */ 391 if (is_sync_beacon && ami && 392 ami->master_rank == data->nan.current_ami.master_rank && 393 (((u32)rx_status.mactime - 394 ieee80211_tu_to_usec(16 * 512)) - 395 le32_to_cpu(ami->ambtt)) < 0x8000000) { 396 wiphy_dbg(hw->wiphy, 397 "NAN: ignoring sync beacon with old AMBTT\n"); 398 is_sync_beacon = false; 399 } 400 401 if (is_same_cluster && is_sync_beacon && 402 master_rank > hwsim_nan_get_master_rank(data)) { 403 if (rx_status.signal > NAN_RSSI_CLOSE) 404 data->nan.master_transition_score += 3; 405 else 406 data->nan.master_transition_score += 1; 407 } 408 409 if (is_same_cluster && is_sync_beacon && ami && 410 ((ami->master_rank == data->nan.current_ami.master_rank && 411 ami->hop_count < data->nan.current_ami.hop_count) || 412 (master_rank > hwsim_nan_get_master_rank(data) && 413 ami->hop_count == data->nan.current_ami.hop_count))) { 414 if (rx_status.signal > NAN_RSSI_CLOSE) 415 data->nan.sync_transition_score += 3; 416 else 417 data->nan.sync_transition_score += 1; 418 } 419 420 /* 421 * Decide on TSF adjustments before updating any other state 422 */ 423 if (is_same_cluster && is_sync_beacon && ami && 424 data->nan.current_ami.hop_count != 0) { 425 if (le64_to_cpu(ami->master_rank) > 426 le64_to_cpu(data->nan.current_ami.master_rank) && 427 ami->master_rank != data->nan.last_ami.master_rank) 428 adopt_tsf = true; 429 430 if (le64_to_cpu(ami->master_rank) > 431 le64_to_cpu(data->nan.current_ami.master_rank) && 432 ami->master_rank == data->nan.last_ami.master_rank && 433 le32_to_cpu(ami->ambtt) > 434 le32_to_cpu(data->nan.last_ami.ambtt)) 435 adopt_tsf = true; 436 437 if (le64_to_cpu(ami->master_rank) < 438 le64_to_cpu(data->nan.current_ami.master_rank) && 439 le64_to_cpu(ami->master_rank) > 440 hwsim_nan_get_master_rank(data) && 441 ether_addr_equal(ami->master_addr, 442 data->nan.current_ami.master_addr)) 443 adopt_tsf = true; 444 445 if (ami->master_rank == data->nan.current_ami.master_rank && 446 le32_to_cpu(ami->ambtt) > 447 le32_to_cpu(data->nan.current_ami.ambtt)) 448 adopt_tsf = true; 449 450 /* Anchor Master case is handled below */ 451 } 452 453 /* 454 * NAN Cluster merging 455 */ 456 if (!is_same_cluster && ami) { 457 u64 curr_amr; 458 u64 own_cg; 459 u64 frame_amr; 460 u64 cg; 461 462 /* Shifted down by 19 bits compared to spec */ 463 frame_amr = le64_to_cpu(ami->master_rank); 464 cg = (u64)ami->master_pref << (64 - 19); 465 cg += le64_to_cpu(mgmt->u.beacon.timestamp) >> 19; 466 467 curr_amr = le64_to_cpu(data->nan.current_ami.master_rank); 468 own_cg = (u64)data->nan.current_ami.master_pref << (64 - 19); 469 own_cg += rx_status.mactime >> 19; 470 471 /* 472 * Check if the cluster shall be joined 473 * 474 * When in the "scan" phase, just join immediately. 475 */ 476 if (cg > own_cg || 477 (cg == own_cg && frame_amr > curr_amr) || 478 data->nan.phase == MAC80211_HWSIM_NAN_PHASE_SCAN) { 479 /* Avoid a state transition */ 480 data->nan.master_transition_score = 0; 481 data->nan.sync_transition_score = 0; 482 483 /* 484 * NOTE: The spec says we should TX sync beacons on the 485 * old schedule after joining. We do not implement this. 486 */ 487 488 wiphy_dbg(hw->wiphy, "NAN: joining cluster %pM\n", 489 mgmt->bssid); 490 491 joined_cluster = true; 492 adopt_tsf = true; 493 494 memcpy(&data->nan.last_ami, &data->nan.current_ami, 495 sizeof(data->nan.last_ami)); 496 memcpy(&data->nan.current_ami, ami, 497 sizeof(data->nan.last_ami)); 498 data->nan.current_ami.hop_count += 1; 499 500 memcpy(data->nan.cluster_id, mgmt->bssid, ETH_ALEN); 501 502 /* 503 * Assume we are UP if we joined a cluster. 504 * 505 * If the other anchor master is still in the warmup 506 * phase, then we may temporarily become the anchor 507 * master until it sets its own master preference to 508 * be non-zero. 509 */ 510 data->nan.phase = MAC80211_HWSIM_NAN_PHASE_UP; 511 data->nan.random_factor_valid_dwst = 0; 512 } 513 } 514 515 /* 516 * Anchor master selection 517 */ 518 /* We are not anchor master */ 519 if (is_same_cluster && is_sync_beacon && ami && 520 data->nan.current_ami.hop_count != 0) { 521 if (le64_to_cpu(data->nan.current_ami.master_rank) < 522 le64_to_cpu(ami->master_rank)) { 523 if (ami->master_rank == data->nan.last_ami.master_rank && 524 le32_to_cpu(ami->ambtt) <= 525 le32_to_cpu(data->nan.last_ami.ambtt)) { 526 /* disregard frame */ 527 } else { 528 memcpy(&data->nan.last_ami, 529 &data->nan.current_ami, 530 sizeof(data->nan.last_ami)); 531 memcpy(&data->nan.current_ami, ami, 532 sizeof(data->nan.last_ami)); 533 data->nan.current_ami.hop_count += 1; 534 } 535 } 536 537 if (le64_to_cpu(data->nan.current_ami.master_rank) > 538 le64_to_cpu(ami->master_rank)) { 539 if (!ether_addr_equal(data->nan.current_ami.master_addr, 540 ami->master_addr)) { 541 /* disregard frame */ 542 } else { 543 u64 amr = hwsim_nan_get_master_rank(data); 544 545 if (amr > le64_to_cpu(ami->master_rank)) { 546 /* assume ourselves as anchor master */ 547 wiphy_dbg(hw->wiphy, 548 "NAN: assume anchor master role\n"); 549 data->nan.current_ami.master_rank = 550 cpu_to_le64(amr); 551 data->nan.current_ami.hop_count = 0; 552 memset(&data->nan.last_ami, 0, 553 sizeof(data->nan.last_ami)); 554 data->nan.last_ami.ambtt = 555 data->nan.current_ami.ambtt; 556 data->nan.current_ami.ambtt = 0; 557 } else { 558 memcpy(&data->nan.last_ami, 559 &data->nan.current_ami, 560 sizeof(data->nan.last_ami)); 561 memcpy(&data->nan.current_ami, ami, 562 sizeof(data->nan.last_ami)); 563 data->nan.current_ami.hop_count += 1; 564 } 565 } 566 } 567 568 if (data->nan.current_ami.master_rank == ami->master_rank) { 569 if (le32_to_cpu(data->nan.current_ami.ambtt) < 570 le32_to_cpu(ami->ambtt)) { 571 data->nan.current_ami.ambtt = ami->ambtt; 572 } 573 574 if (data->nan.current_ami.hop_count > 575 ami->hop_count + 1) { 576 data->nan.current_ami.hop_count = 577 ami->hop_count + 1; 578 } 579 } 580 } 581 582 /* We are anchor master */ 583 if (is_same_cluster && is_sync_beacon && ami && 584 data->nan.current_ami.hop_count == 0) { 585 WARN_ON_ONCE(!ether_addr_equal(data->nan.current_ami.master_addr, 586 data->nan.device_vif->addr)); 587 588 if (le64_to_cpu(ami->master_rank) < 589 le64_to_cpu(data->nan.current_ami.master_rank) || 590 ether_addr_equal(ami->master_addr, 591 data->nan.current_ami.master_addr)) { 592 /* disregard */ 593 } else { 594 wiphy_dbg(hw->wiphy, "NAN: lost anchor master role\n"); 595 adopt_tsf = true; 596 memcpy(&data->nan.last_ami, &data->nan.current_ami, 597 sizeof(data->nan.last_ami)); 598 memcpy(&data->nan.current_ami, ami, 599 sizeof(data->nan.last_ami)); 600 data->nan.current_ami.hop_count += 1; 601 } 602 } 603 604 if (adopt_tsf && !data->nan.tsf_adjusted) { 605 int threshold = 5; 606 s64 adjustment; 607 608 /* Timestamp is likely inaccurate (and late) in this case */ 609 if (!(IEEE80211_SKB_RXCB(skb)->flag & RX_FLAG_MACTIME)) 610 threshold = 128; 611 612 adjustment = 613 le64_to_cpu(mgmt->u.beacon.timestamp) - 614 ieee80211_calculate_rx_timestamp(hw, &rx_status, 615 skb->len, 24); 616 617 scoped_guard(spinlock_bh, &data->tsf_offset_lock) { 618 if (adjustment < -threshold || adjustment > threshold) { 619 if (adjustment < -(s64)ieee80211_tu_to_usec(4) || 620 adjustment > (s64)ieee80211_tu_to_usec(4)) 621 data->nan.tsf_discontinuity = true; 622 623 wiphy_debug(hw->wiphy, 624 "NAN: Adjusting TSF by +/- %d us or more: %lld us (discontinuity: %d, from: %pM, old offset: %lld)\n", 625 threshold, adjustment, 626 data->nan.tsf_discontinuity, mgmt->sa, 627 data->tsf_offset); 628 } else { 629 /* smooth things out a little bit */ 630 adjustment /= 2; 631 } 632 633 /* 634 * Do the TSF adjustment 635 * The flag prevents further adjustments until the next 636 * 2.4 GHz DW starts to avoid race conditions for 637 * in-flight packets. 638 */ 639 data->nan.tsf_adjusted = true; 640 data->tsf_offset += adjustment; 641 } 642 } 643 644 spin_unlock_bh(&data->nan.state_lock); 645 646 if (joined_cluster) 647 ieee80211_nan_cluster_joined(data->nan.device_vif, 648 data->nan.cluster_id, false, 649 GFP_ATOMIC); 650 } 651 652 static void 653 mac80211_hwsim_nan_exec_state_transitions(struct mac80211_hwsim_data *data) 654 { 655 bool notify_join = false; 656 657 /* 658 * Handle NAN role and state transitions at the end of the DW period 659 * in accordance to Wi-Fi Aware version 4.0 section 3.3.7 point 2, i.e. 660 * end of 5 GHz DW if enabled else at the end of the 2.4 GHz DW. 661 */ 662 663 spin_lock(&data->nan.state_lock); 664 665 /* Handle role transitions, Wi-Fi Aware version 4.0 section 3.3.6 */ 666 if (data->nan.master_transition_score < 3) 667 data->nan.role = MAC80211_HWSIM_NAN_ROLE_MASTER; 668 else if (data->nan.role == MAC80211_HWSIM_NAN_ROLE_MASTER && 669 data->nan.master_transition_score >= 3) 670 data->nan.role = MAC80211_HWSIM_NAN_ROLE_SYNC; 671 else if (data->nan.role == MAC80211_HWSIM_NAN_ROLE_SYNC && 672 data->nan.sync_transition_score >= 3) 673 data->nan.role = MAC80211_HWSIM_NAN_ROLE_NON_SYNC; 674 else if (data->nan.role == MAC80211_HWSIM_NAN_ROLE_NON_SYNC && 675 data->nan.sync_transition_score < 3) 676 data->nan.role = MAC80211_HWSIM_NAN_ROLE_SYNC; 677 678 /* 679 * The discovery beacon timer will stop automatically. Make sure it is 680 * running if we are master or if Instant Communication is enabled. Do 681 * not bother with a proper alignment it will sync itself to the TSF 682 * after the first TX. 683 */ 684 if ((data->nan.role == MAC80211_HWSIM_NAN_ROLE_MASTER || 685 data->nan.instant_comm) && 686 !hrtimer_active(&data->nan.discovery_beacon_timer)) 687 hrtimer_start(&data->nan.discovery_beacon_timer, 688 ns_to_ktime(10 * NSEC_PER_USEC), 689 HRTIMER_MODE_REL_SOFT); 690 691 data->nan.master_transition_score = 0; 692 data->nan.sync_transition_score = 0; 693 694 if (data->nan.random_factor_valid_dwst == 0) { 695 u64 amr; 696 697 if (data->nan.phase == MAC80211_HWSIM_NAN_PHASE_SCAN) { 698 data->nan.phase = MAC80211_HWSIM_NAN_PHASE_WARMUP; 699 data->nan.random_factor_valid_dwst = NAN_WARMUP_DWST; 700 701 notify_join = true; 702 } else { 703 data->nan.phase = MAC80211_HWSIM_NAN_PHASE_UP; 704 data->nan.random_factor_valid_dwst = 705 get_random_u32_inclusive(120, 240); 706 data->nan.random_factor = get_random_u8(); 707 } 708 709 amr = hwsim_nan_get_master_rank(data); 710 711 if (data->nan.current_ami.hop_count == 0) { 712 /* Update if we are already anchor master */ 713 data->nan.current_ami.master_rank = cpu_to_le64(amr); 714 } else if (le64_to_cpu(data->nan.current_ami.master_rank) < amr) { 715 /* assume role if we have a higher rank */ 716 wiphy_dbg(data->hw->wiphy, 717 "NAN: assume anchor master role\n"); 718 data->nan.current_ami.master_rank = cpu_to_le64(amr); 719 data->nan.current_ami.hop_count = 0; 720 memset(&data->nan.last_ami, 0, 721 sizeof(data->nan.last_ami)); 722 data->nan.last_ami.ambtt = data->nan.current_ami.ambtt; 723 data->nan.current_ami.ambtt = 0; 724 } 725 } else { 726 data->nan.random_factor_valid_dwst--; 727 } 728 729 spin_unlock(&data->nan.state_lock); 730 731 if (notify_join) 732 ieee80211_nan_cluster_joined(data->nan.device_vif, 733 data->nan.cluster_id, true, 734 GFP_ATOMIC); 735 } 736 737 static void 738 mac80211_hwsim_nan_tx_beacon(struct mac80211_hwsim_data *data, 739 bool is_discovery, 740 struct ieee80211_channel *channel) 741 { 742 struct ieee80211_vendor_ie nan_ie = { 743 .element_id = WLAN_EID_VENDOR_SPECIFIC, 744 .len = NAN_BEACON_ELEM_LEN - 2, 745 .oui = { u32_get_bits(WLAN_OUI_WFA, 0xff0000), 746 u32_get_bits(WLAN_OUI_WFA, 0xff00), 747 u32_get_bits(WLAN_OUI_WFA, 0xff) }, 748 .oui_type = WLAN_OUI_TYPE_WFA_NAN, 749 }; 750 size_t alloc_size = 751 IEEE80211_TX_STATUS_HEADROOM + 752 offsetofend(struct ieee80211_mgmt, u.beacon) + 753 NAN_BEACON_ELEM_LEN; 754 struct ieee80211_nan_master_indication master_indication; 755 struct ieee80211_nan_attr nan_attr; 756 struct ieee80211_mgmt *mgmt; 757 struct sk_buff *skb; 758 759 /* 760 * TODO: Should the configured vendor elements be included in some of 761 * these beacons? 762 */ 763 764 /* Allocate maximal size for NAN IE */ 765 if (is_discovery) 766 alloc_size += NAN_BEACON_EXTRA_ATTRS_LEN; 767 768 skb = alloc_skb(alloc_size, GFP_ATOMIC); 769 if (!skb) 770 return; 771 772 spin_lock(&data->nan.state_lock); 773 774 /* 775 * The attributes configured by user space are appended to the 776 * mandatory ones. 777 */ 778 if (is_discovery) 779 nan_ie.len += data->nan.extra_nan_attrs_len; 780 781 skb_reserve(skb, IEEE80211_TX_STATUS_HEADROOM); 782 mgmt = skb_put(skb, offsetofend(struct ieee80211_mgmt, u.beacon)); 783 784 memset(mgmt, 0, offsetofend(struct ieee80211_mgmt, u.beacon)); 785 memcpy(mgmt->sa, data->nan.device_vif->addr, ETH_ALEN); 786 memset(mgmt->da, 0xff, ETH_ALEN); 787 memcpy(mgmt->bssid, data->nan.cluster_id, ETH_ALEN); 788 789 mgmt->frame_control = cpu_to_le16(IEEE80211_FTYPE_MGMT | 790 IEEE80211_STYPE_BEACON); 791 mgmt->u.beacon.beacon_int = 792 cpu_to_le16(is_discovery ? data->nan.discovery_beacon_interval : 793 DWST_TU); 794 mgmt->u.beacon.capab_info = 795 cpu_to_le16(WLAN_CAPABILITY_SHORT_SLOT_TIME | 796 WLAN_CAPABILITY_SHORT_PREAMBLE); 797 798 /* FIXME: set these to saner values? */ 799 mgmt->duration = 0; 800 mgmt->seq_ctrl = 0; 801 802 /* Put the NAN element */ 803 skb_put_data(skb, &nan_ie, sizeof(nan_ie)); 804 805 nan_attr.attr = NAN_ATTR_MASTER_INDICATION; 806 nan_attr.length = cpu_to_le16(sizeof(master_indication)); 807 if (data->nan.phase == MAC80211_HWSIM_NAN_PHASE_UP) { 808 master_indication.master_pref = data->nan.master_pref; 809 master_indication.random_factor = data->nan.random_factor; 810 } else { 811 master_indication.master_pref = 0; 812 master_indication.random_factor = 0; 813 } 814 815 skb_put_data(skb, &nan_attr, sizeof(nan_attr)); 816 skb_put_data(skb, &master_indication, sizeof(master_indication)); 817 818 nan_attr.attr = NAN_ATTR_CLUSTER_INFO; 819 nan_attr.length = cpu_to_le16(sizeof(data->nan.current_ami)); 820 skb_put_data(skb, &nan_attr, sizeof(nan_attr)); 821 skb_put_data(skb, &data->nan.current_ami, 822 sizeof(data->nan.current_ami)); 823 824 if (is_discovery && data->nan.extra_nan_attrs_len) 825 skb_put_data(skb, data->nan.extra_nan_attrs, 826 data->nan.extra_nan_attrs_len); 827 828 spin_unlock(&data->nan.state_lock); 829 830 mac80211_hwsim_tx_frame(data->hw, skb, channel); 831 } 832 833 enum hrtimer_restart 834 mac80211_hwsim_nan_slot_timer(struct hrtimer *timer) 835 { 836 struct mac80211_hwsim_data *data = 837 container_of(timer, struct mac80211_hwsim_data, 838 nan.slot_timer); 839 struct ieee80211_hw *hw = data->hw; 840 struct ieee80211_channel *notify_dw_chan = NULL; 841 struct ieee80211_channel *beacon_sync_chan = NULL; 842 u64 tsf = hwsim_nan_get_timer_tsf(data); 843 u8 slot = hwsim_nan_slot_from_tsf(tsf); 844 bool dwst_of_dw0 = false; 845 bool dw_end = false; 846 bool tx_sync_beacon; 847 848 if (!data->nan.device_vif) 849 return HRTIMER_NORESTART; 850 851 if ((tsf & DW0_TSF_MASK & ~DWST_TSF_MASK) == 0) 852 dwst_of_dw0 = true; 853 854 855 scoped_guard(spinlock, &data->nan.state_lock) { 856 if (data->nan.tsf_discontinuity) { 857 data->nan.tsf_discontinuity = false; 858 859 mac80211_hwsim_nan_schedule_slot(data, 32, true); 860 861 return HRTIMER_RESTART; 862 } 863 864 if (slot == SLOT_24GHZ_DW) 865 data->nan.tsf_adjusted = false; 866 867 tx_sync_beacon = 868 data->nan.phase != MAC80211_HWSIM_NAN_PHASE_SCAN && 869 data->nan.role != MAC80211_HWSIM_NAN_ROLE_NON_SYNC; 870 } 871 872 switch (slot) { 873 case SLOT_24GHZ_DW: 874 wiphy_dbg(data->hw->wiphy, "Start of 2.4 GHz DW, is DW0=%d\n", 875 dwst_of_dw0); 876 beacon_sync_chan = ieee80211_get_channel(hw->wiphy, 2437); 877 break; 878 879 case SLOT_24GHZ_DW + 1: 880 if (!(data->nan.bands & BIT(NL80211_BAND_5GHZ))) { 881 notify_dw_chan = ieee80211_get_channel(hw->wiphy, 2437); 882 dw_end = true; 883 } else { 884 notify_dw_chan = ieee80211_get_channel(hw->wiphy, 5745); 885 } 886 break; 887 888 case SLOT_5GHZ_DW: 889 if (data->nan.bands & BIT(NL80211_BAND_5GHZ)) { 890 wiphy_dbg(data->hw->wiphy, "Start of 5 GHz DW\n"); 891 beacon_sync_chan = 892 ieee80211_get_channel(hw->wiphy, 5745); 893 } 894 break; 895 896 case SLOT_5GHZ_DW + 1: 897 if (data->nan.bands & BIT(NL80211_BAND_5GHZ)) { 898 notify_dw_chan = 899 ieee80211_get_channel(hw->wiphy, 2437); 900 dw_end = true; 901 } 902 break; 903 } 904 905 /* TODO: This does not implement DW contention mitigation */ 906 if (beacon_sync_chan && tx_sync_beacon) 907 mac80211_hwsim_nan_tx_beacon(data, false, beacon_sync_chan); 908 909 if (dw_end) 910 mac80211_hwsim_nan_exec_state_transitions(data); 911 912 if (data->nan.notify_dw && notify_dw_chan) { 913 struct wireless_dev *wdev = 914 ieee80211_vif_to_wdev(data->nan.device_vif); 915 916 cfg80211_next_nan_dw_notif(wdev, notify_dw_chan, GFP_ATOMIC); 917 } 918 919 mac80211_hwsim_nan_resume_txqs(data); 920 921 mac80211_hwsim_nan_schedule_slot(data, slot + 1, false); 922 923 return HRTIMER_RESTART; 924 } 925 926 enum hrtimer_restart 927 mac80211_hwsim_nan_discovery_beacon_timer(struct hrtimer *timer) 928 { 929 struct mac80211_hwsim_data *data = 930 container_of(timer, struct mac80211_hwsim_data, 931 nan.discovery_beacon_timer); 932 u32 remainder; 933 u64 tsf_now; 934 u64 tbtt; 935 936 if (!data->nan.device_vif) 937 return HRTIMER_NORESTART; 938 939 scoped_guard(spinlock, &data->nan.state_lock) { 940 if (data->nan.phase == MAC80211_HWSIM_NAN_PHASE_SCAN) 941 return HRTIMER_NORESTART; 942 943 /* 944 * With Instant Communication the discovery beacons are 945 * transmitted disregarding the role and the state, see 946 * Wi-Fi Aware version 4.0 section 13. 947 */ 948 if (!data->nan.instant_comm && 949 data->nan.role != MAC80211_HWSIM_NAN_ROLE_MASTER) 950 return HRTIMER_NORESTART; 951 } 952 953 mac80211_hwsim_nan_tx_beacon( 954 data, true, ieee80211_get_channel(data->hw->wiphy, 2437)); 955 956 if (data->nan.bands & BIT(NL80211_BAND_5GHZ)) 957 mac80211_hwsim_nan_tx_beacon( 958 data, true, 959 ieee80211_get_channel(data->hw->wiphy, 5745)); 960 961 /* Read the TSF from the current time in case of adjustments */ 962 tsf_now = mac80211_hwsim_get_tsf(data->hw, data->nan.device_vif); 963 964 /* Wrap value to be after the next TBTT */ 965 tbtt = tsf_now + ieee80211_tu_to_usec(data->nan.discovery_beacon_interval); 966 967 /* Round TBTT down to the correct time */ 968 div_u64_rem(tbtt, ieee80211_tu_to_usec(data->nan.discovery_beacon_interval), 969 &remainder); 970 tbtt = tbtt - remainder; 971 972 hrtimer_set_expires(&data->nan.discovery_beacon_timer, 973 mac80211_hwsim_tsf_to_boottime(data, tbtt)); 974 975 return HRTIMER_RESTART; 976 } 977 978 static void 979 mac80211_hwsim_nan_sched_update_work(struct wiphy *wiphy, 980 struct wiphy_work *work) 981 { 982 struct mac80211_hwsim_data *data = 983 container_of(work, struct mac80211_hwsim_data, 984 nan.sched_update_work.work); 985 986 if (data->nan.device_vif) 987 ieee80211_nan_sched_update_done(data->nan.device_vif); 988 } 989 990 static int mac80211_hwsim_nan_set_config(struct mac80211_hwsim_data *data, 991 struct cfg80211_nan_conf *conf) 992 { 993 const u8 *extra_nan_attrs = NULL; 994 const u8 *old; 995 996 if (conf->extra_nan_attrs_len > NAN_BEACON_EXTRA_ATTRS_LEN) 997 return -EINVAL; 998 999 if (conf->extra_nan_attrs_len) { 1000 extra_nan_attrs = kmemdup(conf->extra_nan_attrs, 1001 conf->extra_nan_attrs_len, 1002 GFP_KERNEL); 1003 if (!extra_nan_attrs) 1004 return -ENOMEM; 1005 } 1006 1007 data->nan.notify_dw = conf->enable_dw_notification; 1008 1009 scoped_guard(spinlock_bh, &data->nan.state_lock) { 1010 /* 1011 * Fall back to the device default if user space did not 1012 * configure it 1013 */ 1014 data->nan.discovery_beacon_interval = 1015 conf->discovery_beacon_interval ? : 100; 1016 1017 data->nan.instant_comm = conf->instant_comm; 1018 1019 old = data->nan.extra_nan_attrs; 1020 data->nan.extra_nan_attrs = extra_nan_attrs; 1021 data->nan.extra_nan_attrs_len = conf->extra_nan_attrs_len; 1022 } 1023 1024 kfree(old); 1025 1026 return 0; 1027 } 1028 1029 int mac80211_hwsim_nan_start(struct ieee80211_hw *hw, 1030 struct ieee80211_vif *vif, 1031 struct cfg80211_nan_conf *conf) 1032 { 1033 struct mac80211_hwsim_data *data = hw->priv; 1034 int err; 1035 1036 if (vif->type != NL80211_IFTYPE_NAN) 1037 return -EINVAL; 1038 1039 if (data->nan.device_vif) 1040 return -EALREADY; 1041 1042 err = mac80211_hwsim_nan_set_config(data, conf); 1043 if (err) 1044 return err; 1045 1046 /* set this before starting the timer, as preemption might occur */ 1047 data->nan.device_vif = vif; 1048 data->nan.bands = conf->bands; 1049 1050 wiphy_delayed_work_init(&data->nan.sched_update_work, 1051 mac80211_hwsim_nan_sched_update_work); 1052 1053 scoped_guard(spinlock_bh, &data->nan.state_lock) { 1054 /* 1055 * Start in the "scan" phase and stay there for a little bit, 1056 * unless Instant Communication is configured, in which case an 1057 * own cluster is started immediately. 1058 */ 1059 data->nan.phase = MAC80211_HWSIM_NAN_PHASE_SCAN; 1060 data->nan.random_factor_valid_dwst = conf->instant_comm ? 0 : 1; 1061 data->nan.random_factor = 0; 1062 data->nan.master_pref = conf->master_pref; 1063 data->nan.role = MAC80211_HWSIM_NAN_ROLE_MASTER; 1064 memset(&data->nan.current_ami, 0, 1065 sizeof(data->nan.current_ami)); 1066 memset(&data->nan.last_ami, 0, sizeof(data->nan.last_ami)); 1067 data->nan.current_ami.master_rank = 1068 cpu_to_le64(hwsim_nan_get_master_rank(data)); 1069 } 1070 1071 /* Just run this "soon" and start in a random schedule position */ 1072 hrtimer_start(&data->nan.slot_timer, 1073 ns_to_ktime(10 * NSEC_PER_USEC), 1074 HRTIMER_MODE_REL_SOFT); 1075 1076 ether_addr_copy(data->nan.cluster_id, conf->cluster_id); 1077 1078 return 0; 1079 } 1080 1081 int mac80211_hwsim_nan_stop(struct ieee80211_hw *hw, 1082 struct ieee80211_vif *vif) 1083 { 1084 struct mac80211_hwsim_data *data = hw->priv; 1085 const u8 *old; 1086 1087 if (vif->type != NL80211_IFTYPE_NAN || !data->nan.device_vif || 1088 data->nan.device_vif != vif) 1089 return -EINVAL; 1090 1091 hrtimer_cancel(&data->nan.slot_timer); 1092 hrtimer_cancel(&data->nan.resume_txqs_timer); 1093 hrtimer_cancel(&data->nan.discovery_beacon_timer); 1094 wiphy_delayed_work_cancel(hw->wiphy, &data->nan.sched_update_work); 1095 data->nan.device_vif = NULL; 1096 1097 scoped_guard(spinlock_bh, &data->nan.state_lock) { 1098 old = data->nan.extra_nan_attrs; 1099 data->nan.extra_nan_attrs = NULL; 1100 data->nan.extra_nan_attrs_len = 0; 1101 data->nan.instant_comm = false; 1102 } 1103 1104 kfree(old); 1105 1106 return 0; 1107 } 1108 1109 int mac80211_hwsim_nan_change_config(struct ieee80211_hw *hw, 1110 struct ieee80211_vif *vif, 1111 struct cfg80211_nan_conf *conf, 1112 u32 changes) 1113 { 1114 struct mac80211_hwsim_data *data = hw->priv; 1115 1116 if (vif->type != NL80211_IFTYPE_NAN) 1117 return -EINVAL; 1118 1119 if (!data->nan.device_vif) 1120 return -EINVAL; 1121 1122 wiphy_debug(hw->wiphy, "nan_config_changed: changes=0x%x\n", changes); 1123 1124 if (changes & CFG80211_NAN_CONF_CHANGED_CONFIG) { 1125 int err = mac80211_hwsim_nan_set_config(data, conf); 1126 1127 if (err) 1128 return err; 1129 1130 /* 1131 * When Instant Communication is enabled discovery beacons are 1132 * transmitted regardless of the role and the state. 1133 */ 1134 if (conf->instant_comm && 1135 !hrtimer_active(&data->nan.discovery_beacon_timer)) 1136 hrtimer_start(&data->nan.discovery_beacon_timer, 1137 ns_to_ktime(10 * NSEC_PER_USEC), 1138 HRTIMER_MODE_REL_SOFT); 1139 } 1140 1141 /* Handle only the changes we care about for simulation purposes */ 1142 if (changes & CFG80211_NAN_CONF_CHANGED_BANDS) 1143 data->nan.bands = conf->bands; 1144 1145 if (changes & CFG80211_NAN_CONF_CHANGED_PREF) { 1146 scoped_guard(spinlock_bh, &data->nan.state_lock) 1147 data->nan.master_pref = conf->master_pref; 1148 } 1149 1150 return 0; 1151 } 1152 1153 static void hwsim_nan_can_sta_transmit(void *_ctx, struct ieee80211_sta *sta) 1154 { 1155 struct hwsim_nan_sta_iter_ctx *ctx = _ctx; 1156 1157 if (ctx->can_tx) 1158 return; 1159 1160 for (int i = 0; i < ARRAY_SIZE(sta->txq); i++) { 1161 struct ieee80211_txq *txq = sta->txq[i]; 1162 1163 if (!txq) 1164 continue; 1165 1166 if (txq->vif->type != NL80211_IFTYPE_NAN && 1167 txq->vif->type != NL80211_IFTYPE_NAN_DATA) 1168 return; 1169 1170 if (mac80211_hwsim_nan_txq_transmitting(ctx->hw, txq)) { 1171 ctx->can_tx = true; 1172 return; 1173 } 1174 } 1175 } 1176 1177 static void mac80211_hwsim_nan_resume_txqs(struct mac80211_hwsim_data *data) 1178 { 1179 u64 tsf = mac80211_hwsim_get_tsf(data->hw, data->nan.device_vif); 1180 u8 slot = hwsim_nan_slot_from_tsf(tsf); 1181 bool is_dw_slot = mac80211_hwsim_nan_is_dw_slot(data, slot); 1182 struct hwsim_nan_sta_iter_ctx ctx = { 1183 .hw = data->hw, 1184 .can_tx = false, 1185 }; 1186 u32 timeout_ns; 1187 1188 /* Outside DW, require local FAW schedule to proceed */ 1189 if (!is_dw_slot) { 1190 scoped_guard(spinlock_bh, &data->nan.state_lock) { 1191 if (!data->nan.local_sched[slot].chan) 1192 return; 1193 } 1194 } 1195 1196 guard(rcu)(); 1197 1198 /* Check if management queue can transmit */ 1199 if (mac80211_hwsim_nan_txq_transmitting(data->hw, 1200 data->nan.device_vif->txq_mgmt)) 1201 goto resume_txqs_timer; 1202 1203 /* Check if any STA queue can transmit */ 1204 ieee80211_iterate_stations_atomic(data->hw, 1205 hwsim_nan_can_sta_transmit, 1206 &ctx); 1207 1208 if (!ctx.can_tx) 1209 return; 1210 1211 resume_txqs_timer: 1212 /* 1213 * Wait a bit and also randomize things so that not everyone is TXing 1214 * at the same time. Each slot is 16 TU long, this waits between 100 us 1215 * and 5 ms before starting to TX (unless a new frame arrives). 1216 */ 1217 timeout_ns = get_random_u32_inclusive(100 * NSEC_PER_USEC, 1218 5 * NSEC_PER_MSEC); 1219 1220 hrtimer_start(&data->nan.resume_txqs_timer, 1221 ns_to_ktime(timeout_ns), 1222 HRTIMER_MODE_REL_SOFT); 1223 } 1224 1225 static void hwsim_nan_wake_sta_iter(void *_data, struct ieee80211_sta *sta) 1226 { 1227 struct ieee80211_hw *hw = _data; 1228 1229 for (int i = 0; i < ARRAY_SIZE(sta->txq); i++) { 1230 struct ieee80211_txq *txq = sta->txq[i]; 1231 1232 if (!txq) 1233 continue; 1234 1235 /* exit early if non-NAN */ 1236 if (txq->vif->type != NL80211_IFTYPE_NAN && 1237 txq->vif->type != NL80211_IFTYPE_NAN_DATA) 1238 return; 1239 1240 if (mac80211_hwsim_nan_txq_transmitting(hw, txq)) 1241 ieee80211_hwsim_wake_tx_queue(hw, txq); 1242 } 1243 } 1244 1245 enum hrtimer_restart 1246 mac80211_hwsim_nan_resume_txqs_timer(struct hrtimer *timer) 1247 { 1248 struct mac80211_hwsim_data *data = 1249 container_of(timer, struct mac80211_hwsim_data, 1250 nan.resume_txqs_timer); 1251 1252 guard(rcu)(); 1253 1254 /* Wake TX queue for management frames on the NAN device interface */ 1255 if (mac80211_hwsim_nan_txq_transmitting(data->hw, 1256 data->nan.device_vif->txq_mgmt)) 1257 ieee80211_hwsim_wake_tx_queue(data->hw, 1258 data->nan.device_vif->txq_mgmt); 1259 1260 /* Wake TX queues for all stations */ 1261 ieee80211_iterate_stations_atomic(data->hw, 1262 hwsim_nan_wake_sta_iter, 1263 data->hw); 1264 1265 return HRTIMER_NORESTART; 1266 } 1267 1268 static void 1269 hwsim_nan_can_mcast_sta_transmit(void *_ctx, struct ieee80211_sta *sta) 1270 { 1271 struct hwsim_nan_mcast_data_iter_ctx *ctx = _ctx; 1272 struct ieee80211_txq *txq = sta->txq[0]; 1273 1274 if (!txq || txq->vif != ctx->vif) 1275 return; 1276 1277 ctx->n_vif_sta++; 1278 if (mac80211_hwsim_nan_txq_transmitting(ctx->hw, txq)) 1279 ctx->n_sta_can_tx++; 1280 } 1281 1282 static bool 1283 mac80211_hwsim_nan_mcast_data_transmitting(struct ieee80211_hw *hw, 1284 struct ieee80211_txq *txq) 1285 { 1286 struct mac80211_hwsim_data *data = hw->priv; 1287 struct hwsim_nan_mcast_data_iter_ctx ctx = { 1288 .hw = hw, 1289 .vif = txq->vif, 1290 .n_sta_can_tx = 0, 1291 .n_vif_sta = 0, 1292 }; 1293 1294 /* Check if all the stations associated with the current 1295 * interface are available. 1296 */ 1297 ieee80211_iterate_stations_atomic(data->hw, 1298 hwsim_nan_can_mcast_sta_transmit, 1299 &ctx); 1300 1301 return ctx.n_vif_sta && ctx.n_sta_can_tx == ctx.n_vif_sta; 1302 } 1303 1304 bool mac80211_hwsim_nan_txq_transmitting(struct ieee80211_hw *hw, 1305 struct ieee80211_txq *txq) 1306 { 1307 struct mac80211_hwsim_data *data = hw->priv; 1308 struct ieee80211_sta *nmi_sta; 1309 struct hwsim_sta_priv *sp; 1310 bool is_dw_slot; 1311 u64 tsf; 1312 u8 slot; 1313 1314 if (WARN_ON_ONCE(!data->nan.device_vif)) 1315 return true; 1316 1317 tsf = mac80211_hwsim_get_tsf(hw, data->nan.device_vif); 1318 slot = hwsim_nan_slot_from_tsf(tsf); 1319 1320 /* Enforce a maximum channel switch time and guard against TX delays */ 1321 if (slot != hwsim_nan_slot_from_tsf(tsf + NAN_CHAN_SWITCH_TIME_US)) 1322 return false; 1323 1324 is_dw_slot = mac80211_hwsim_nan_is_dw_slot(data, slot); 1325 1326 if (!txq->sta) { 1327 /* Non-STA TXQ: allow management frames during DW or IC */ 1328 if (txq->vif->type == NL80211_IFTYPE_NAN) { 1329 if (is_dw_slot) 1330 return true; 1331 1332 /* Outside the DW the local schedule must allow it */ 1333 guard(spinlock_bh)(&data->nan.state_lock); 1334 return data->nan.instant_comm && 1335 data->nan.local_sched[slot].chan; 1336 } 1337 1338 /* Allow multicast data when all the peers are available 1339 * on this slot 1340 */ 1341 return mac80211_hwsim_nan_mcast_data_transmitting(hw, txq); 1342 } 1343 1344 /* STA TXQ: need peer schedule for availability check */ 1345 nmi_sta = rcu_dereference(txq->sta->nmi) ?: txq->sta; 1346 sp = (void *)nmi_sta->drv_priv; 1347 1348 /* DW slot: NDI can TX only mgmt but not worth checking, 1349 * NMI checks peer's committed DW 1350 */ 1351 if (is_dw_slot) { 1352 if (txq->vif->type == NL80211_IFTYPE_NAN_DATA) 1353 return false; 1354 return hwsim_nan_peer_present_in_dw(sp, tsf); 1355 } 1356 1357 /* FAW slot: verify local schedule and peer availability */ 1358 return hwsim_nan_peer_present_in_faw(sp, data, slot); 1359 } 1360 1361 void mac80211_hwsim_nan_get_tx_chandef(struct ieee80211_hw *hw, 1362 struct cfg80211_chan_def *chandef) 1363 { 1364 struct mac80211_hwsim_data *data = hw->priv; 1365 u64 tsf = mac80211_hwsim_get_tsf(data->hw, data->nan.device_vif); 1366 u8 slot = hwsim_nan_slot_from_tsf(tsf); 1367 1368 /* DW slots are always 20 MHz */ 1369 if (slot == SLOT_24GHZ_DW) { 1370 cfg80211_chandef_create(chandef, 1371 ieee80211_get_channel(hw->wiphy, 2437), 1372 NL80211_CHAN_NO_HT); 1373 return; 1374 } 1375 1376 if (slot == SLOT_5GHZ_DW && data->nan.bands & BIT(NL80211_BAND_5GHZ)) { 1377 cfg80211_chandef_create(chandef, 1378 ieee80211_get_channel(hw->wiphy, 5745), 1379 NL80211_CHAN_NO_HT); 1380 return; 1381 } 1382 1383 /* FAW slot: copy local schedule for this slot */ 1384 scoped_guard(spinlock_bh, &data->nan.state_lock) 1385 *chandef = data->nan.local_sched[slot]; 1386 } 1387 1388 bool mac80211_hwsim_nan_receive(struct ieee80211_hw *hw, 1389 struct sk_buff *skb, 1390 struct ieee80211_channel *channel, 1391 struct ieee80211_rx_status *rx_status) 1392 { 1393 struct mac80211_hwsim_data *data = hw->priv; 1394 bool instant_comm; 1395 u8 slot; 1396 1397 if (WARN_ON_ONCE(!data->nan.device_vif)) 1398 return false; 1399 1400 scoped_guard(spinlock_bh, &data->nan.state_lock) 1401 instant_comm = data->nan.instant_comm; 1402 1403 /* 1404 * During Instant Communication a peer advertises its services in the 1405 * discovery beacons, which are transmitted disregarding the roles and 1406 * the states, see Section 13 in Wi-Fi Aware v4.0. 1407 * Note that while according to the specification Instant communication 1408 * should be enabled only on the NAN discovery channels, do not force 1409 * this here, and allow receiving NAN frames on any channel if Instant 1410 * Communication is enabled. 1411 */ 1412 if (instant_comm && hwsim_nan_beacon_has_service_ids(skb)) 1413 return true; 1414 1415 if (data->nan.phase == MAC80211_HWSIM_NAN_PHASE_SCAN) 1416 return channel->center_freq == 2437; 1417 1418 if (rx_status->flag & RX_FLAG_MACTIME) { 1419 slot = hwsim_nan_slot_from_tsf(rx_status->mactime); 1420 } else { 1421 u64 tsf; 1422 1423 /* 1424 * This is not perfect, but that should be fine. 1425 * 1426 * Assume the frame might be a bit early in relation to our 1427 * own TSF. This is largely because the TSF sync is going to be 1428 * pretty bad when the frame was RXed via NL and the beacon as 1429 * well as RX timestamps are not accurate. 1430 */ 1431 tsf = mac80211_hwsim_get_tsf(data->hw, data->nan.device_vif); 1432 slot = hwsim_nan_slot_from_tsf(tsf + 128); 1433 } 1434 1435 if (slot == SLOT_24GHZ_DW && channel->center_freq == 2437) 1436 return true; 1437 1438 if (slot == SLOT_5GHZ_DW && data->nan.bands & BIT(NL80211_BAND_5GHZ) && 1439 channel->center_freq == 5745) 1440 return true; 1441 1442 /* Accept frames during FAW slots if chandef is compatible */ 1443 return hwsim_nan_rx_chandef_compatible(data, slot, channel, 1444 rx_status->bw); 1445 } 1446 1447 void mac80211_hwsim_nan_local_sched_changed(struct ieee80211_hw *hw, 1448 struct ieee80211_vif *vif) 1449 { 1450 struct mac80211_hwsim_data *data = hw->priv; 1451 struct ieee80211_nan_channel **slots = vif->cfg.nan_sched.schedule; 1452 1453 if (WARN_ON(vif->type != NL80211_IFTYPE_NAN)) 1454 return; 1455 1456 spin_lock_bh(&data->nan.state_lock); 1457 1458 for (int i = 0; i < ARRAY_SIZE(data->nan.local_sched); i++) { 1459 struct ieee80211_chanctx_conf *chanctx; 1460 1461 if (!slots[i] || IS_ERR(slots[i])) { 1462 memset(&data->nan.local_sched[i], 0, 1463 sizeof(data->nan.local_sched[i])); 1464 continue; 1465 } 1466 1467 chanctx = slots[i]->chanctx_conf; 1468 if (!chanctx) { 1469 memset(&data->nan.local_sched[i], 0, 1470 sizeof(data->nan.local_sched[i])); 1471 continue; 1472 } 1473 1474 data->nan.local_sched[i] = chanctx->def; 1475 } 1476 1477 spin_unlock_bh(&data->nan.state_lock); 1478 1479 /* Simulate firmware completing a deferred schedule update */ 1480 if (vif->cfg.nan_sched.deferred) 1481 wiphy_delayed_work_queue(hw->wiphy, 1482 &data->nan.sched_update_work, 1483 msecs_to_jiffies(10)); 1484 } 1485 1486 int mac80211_hwsim_nan_peer_sched_changed(struct ieee80211_hw *hw, 1487 struct ieee80211_sta *sta) 1488 { 1489 struct hwsim_sta_priv *sp = (void *)sta->drv_priv; 1490 struct ieee80211_nan_peer_sched *sched = sta->nan_sched; 1491 1492 spin_lock_bh(&sp->nan_sched.lock); 1493 1494 /* Clear existing schedule */ 1495 sp->nan_sched.committed_dw = 0; 1496 for (int i = 0; i < CFG80211_NAN_MAX_PEER_MAPS; i++) { 1497 sp->nan_sched.maps[i].map_id = CFG80211_NAN_INVALID_MAP_ID; 1498 memset(sp->nan_sched.maps[i].chans, 0, 1499 sizeof(sp->nan_sched.maps[i].chans)); 1500 } 1501 1502 if (!sched) 1503 goto out; 1504 1505 sp->nan_sched.committed_dw = sched->committed_dw; 1506 1507 for (int i = 0; i < CFG80211_NAN_MAX_PEER_MAPS; i++) { 1508 struct ieee80211_nan_peer_map *map = &sched->maps[i]; 1509 1510 if (map->map_id == CFG80211_NAN_INVALID_MAP_ID) 1511 continue; 1512 1513 sp->nan_sched.maps[i].map_id = map->map_id; 1514 1515 for (int j = 0; j < CFG80211_NAN_SCHED_NUM_TIME_SLOTS; j++) { 1516 struct ieee80211_nan_channel *peer_chan = 1517 map->slots[j]; 1518 1519 if (peer_chan && peer_chan->chanreq.oper.chan) 1520 sp->nan_sched.maps[i].chans[j] = 1521 peer_chan->chanreq.oper; 1522 else 1523 memset(&sp->nan_sched.maps[i].chans[j], 0, 1524 sizeof(sp->nan_sched.maps[i].chans[j])); 1525 } 1526 } 1527 1528 out: 1529 spin_unlock_bh(&sp->nan_sched.lock); 1530 return 0; 1531 } 1532