1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2015-2017 Intel Deutschland GmbH 4 * Copyright (C) 2018-2024 Intel Corporation 5 */ 6 #include <linux/etherdevice.h> 7 #include <linux/math64.h> 8 #include <net/cfg80211.h> 9 #include "mvm.h" 10 #include "iwl-io.h" 11 #include "iwl-prph.h" 12 #include "constants.h" 13 14 struct iwl_mvm_loc_entry { 15 struct list_head list; 16 u8 addr[ETH_ALEN]; 17 u8 lci_len, civic_len; 18 u8 buf[]; 19 }; 20 21 struct iwl_mvm_smooth_entry { 22 struct list_head list; 23 u8 addr[ETH_ALEN]; 24 s64 rtt_avg; 25 u64 host_time; 26 }; 27 28 enum iwl_mvm_pasn_flags { 29 IWL_MVM_PASN_FLAG_HAS_HLTK = BIT(0), 30 }; 31 32 struct iwl_mvm_ftm_pasn_entry { 33 struct list_head list; 34 u8 addr[ETH_ALEN]; 35 u8 hltk[HLTK_11AZ_LEN]; 36 u8 tk[TK_11AZ_LEN]; 37 u8 cipher; 38 u8 tx_pn[IEEE80211_CCMP_PN_LEN]; 39 u8 rx_pn[IEEE80211_CCMP_PN_LEN]; 40 u32 flags; 41 }; 42 43 struct iwl_mvm_ftm_iter_data { 44 u8 *cipher; 45 u8 *bssid; 46 u8 *tk; 47 }; 48 49 int iwl_mvm_ftm_add_pasn_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 50 u8 *addr, u32 cipher, u8 *tk, u32 tk_len, 51 u8 *hltk, u32 hltk_len) 52 { 53 struct iwl_mvm_ftm_pasn_entry *pasn = kzalloc(sizeof(*pasn), 54 GFP_KERNEL); 55 u32 expected_tk_len; 56 57 lockdep_assert_held(&mvm->mutex); 58 59 if (!pasn) 60 return -ENOBUFS; 61 62 iwl_mvm_ftm_remove_pasn_sta(mvm, addr); 63 64 pasn->cipher = iwl_mvm_cipher_to_location_cipher(cipher); 65 66 switch (pasn->cipher) { 67 case IWL_LOCATION_CIPHER_CCMP_128: 68 case IWL_LOCATION_CIPHER_GCMP_128: 69 expected_tk_len = WLAN_KEY_LEN_CCMP; 70 break; 71 case IWL_LOCATION_CIPHER_GCMP_256: 72 expected_tk_len = WLAN_KEY_LEN_GCMP_256; 73 break; 74 default: 75 goto out; 76 } 77 78 /* 79 * If associated to this AP and already have security context, 80 * the TK is already configured for this station, so it 81 * shouldn't be set again here. 82 */ 83 if (vif->cfg.assoc) { 84 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 85 struct ieee80211_bss_conf *link_conf; 86 unsigned int link_id; 87 struct ieee80211_sta *sta; 88 u8 sta_id; 89 90 rcu_read_lock(); 91 for_each_vif_active_link(vif, link_conf, link_id) { 92 if (memcmp(addr, link_conf->bssid, ETH_ALEN)) 93 continue; 94 95 sta_id = mvmvif->link[link_id]->ap_sta_id; 96 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]); 97 if (!IS_ERR_OR_NULL(sta) && sta->mfp) 98 expected_tk_len = 0; 99 break; 100 } 101 rcu_read_unlock(); 102 } 103 104 if (tk_len != expected_tk_len || 105 (hltk_len && hltk_len != sizeof(pasn->hltk))) { 106 IWL_ERR(mvm, "Invalid key length: tk_len=%u hltk_len=%u\n", 107 tk_len, hltk_len); 108 goto out; 109 } 110 111 if (!expected_tk_len && !hltk_len) { 112 IWL_ERR(mvm, "TK and HLTK not set\n"); 113 goto out; 114 } 115 116 memcpy(pasn->addr, addr, sizeof(pasn->addr)); 117 118 if (hltk_len) { 119 memcpy(pasn->hltk, hltk, sizeof(pasn->hltk)); 120 pasn->flags |= IWL_MVM_PASN_FLAG_HAS_HLTK; 121 } 122 123 if (tk && tk_len) 124 memcpy(pasn->tk, tk, sizeof(pasn->tk)); 125 126 list_add_tail(&pasn->list, &mvm->ftm_initiator.pasn_list); 127 return 0; 128 out: 129 kfree(pasn); 130 return -EINVAL; 131 } 132 133 void iwl_mvm_ftm_remove_pasn_sta(struct iwl_mvm *mvm, u8 *addr) 134 { 135 struct iwl_mvm_ftm_pasn_entry *entry, *prev; 136 137 lockdep_assert_held(&mvm->mutex); 138 139 list_for_each_entry_safe(entry, prev, &mvm->ftm_initiator.pasn_list, 140 list) { 141 if (memcmp(entry->addr, addr, sizeof(entry->addr))) 142 continue; 143 144 list_del(&entry->list); 145 kfree(entry); 146 return; 147 } 148 } 149 150 static void iwl_mvm_ftm_reset(struct iwl_mvm *mvm) 151 { 152 struct iwl_mvm_loc_entry *e, *t; 153 154 mvm->ftm_initiator.req = NULL; 155 mvm->ftm_initiator.req_wdev = NULL; 156 memset(mvm->ftm_initiator.responses, 0, 157 sizeof(mvm->ftm_initiator.responses)); 158 159 list_for_each_entry_safe(e, t, &mvm->ftm_initiator.loc_list, list) { 160 list_del(&e->list); 161 kfree(e); 162 } 163 } 164 165 void iwl_mvm_ftm_restart(struct iwl_mvm *mvm) 166 { 167 struct cfg80211_pmsr_result result = { 168 .status = NL80211_PMSR_STATUS_FAILURE, 169 .final = 1, 170 .host_time = ktime_get_boottime_ns(), 171 .type = NL80211_PMSR_TYPE_FTM, 172 }; 173 int i; 174 175 lockdep_assert_held(&mvm->mutex); 176 177 if (!mvm->ftm_initiator.req) 178 return; 179 180 for (i = 0; i < mvm->ftm_initiator.req->n_peers; i++) { 181 memcpy(result.addr, mvm->ftm_initiator.req->peers[i].addr, 182 ETH_ALEN); 183 result.ftm.burst_index = mvm->ftm_initiator.responses[i]; 184 185 cfg80211_pmsr_report(mvm->ftm_initiator.req_wdev, 186 mvm->ftm_initiator.req, 187 &result, GFP_KERNEL); 188 } 189 190 cfg80211_pmsr_complete(mvm->ftm_initiator.req_wdev, 191 mvm->ftm_initiator.req, GFP_KERNEL); 192 iwl_mvm_ftm_reset(mvm); 193 } 194 195 void iwl_mvm_ftm_initiator_smooth_config(struct iwl_mvm *mvm) 196 { 197 INIT_LIST_HEAD(&mvm->ftm_initiator.smooth.resp); 198 199 IWL_DEBUG_INFO(mvm, 200 "enable=%u, alpha=%u, age_jiffies=%u, thresh=(%u:%u)\n", 201 IWL_MVM_FTM_INITIATOR_ENABLE_SMOOTH, 202 IWL_MVM_FTM_INITIATOR_SMOOTH_ALPHA, 203 IWL_MVM_FTM_INITIATOR_SMOOTH_AGE_SEC * HZ, 204 IWL_MVM_FTM_INITIATOR_SMOOTH_OVERSHOOT, 205 IWL_MVM_FTM_INITIATOR_SMOOTH_UNDERSHOOT); 206 } 207 208 void iwl_mvm_ftm_initiator_smooth_stop(struct iwl_mvm *mvm) 209 { 210 struct iwl_mvm_smooth_entry *se, *st; 211 212 list_for_each_entry_safe(se, st, &mvm->ftm_initiator.smooth.resp, 213 list) { 214 list_del(&se->list); 215 kfree(se); 216 } 217 } 218 219 static int 220 iwl_ftm_range_request_status_to_err(enum iwl_tof_range_request_status s) 221 { 222 switch (s) { 223 case IWL_TOF_RANGE_REQUEST_STATUS_SUCCESS: 224 return 0; 225 case IWL_TOF_RANGE_REQUEST_STATUS_BUSY: 226 return -EBUSY; 227 default: 228 WARN_ON_ONCE(1); 229 return -EIO; 230 } 231 } 232 233 static void iwl_mvm_ftm_cmd_v5(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 234 struct iwl_tof_range_req_cmd_v5 *cmd, 235 struct cfg80211_pmsr_request *req) 236 { 237 int i; 238 239 cmd->request_id = req->cookie; 240 cmd->num_of_ap = req->n_peers; 241 242 /* use maximum for "no timeout" or bigger than what we can do */ 243 if (!req->timeout || req->timeout > 255 * 100) 244 cmd->req_timeout = 255; 245 else 246 cmd->req_timeout = DIV_ROUND_UP(req->timeout, 100); 247 248 /* 249 * We treat it always as random, since if not we'll 250 * have filled our local address there instead. 251 */ 252 cmd->macaddr_random = 1; 253 memcpy(cmd->macaddr_template, req->mac_addr, ETH_ALEN); 254 for (i = 0; i < ETH_ALEN; i++) 255 cmd->macaddr_mask[i] = ~req->mac_addr_mask[i]; 256 257 if (vif->cfg.assoc) 258 memcpy(cmd->range_req_bssid, vif->bss_conf.bssid, ETH_ALEN); 259 else 260 eth_broadcast_addr(cmd->range_req_bssid); 261 } 262 263 static void iwl_mvm_ftm_cmd_common(struct iwl_mvm *mvm, 264 struct ieee80211_vif *vif, 265 struct iwl_tof_range_req_cmd_v9 *cmd, 266 struct cfg80211_pmsr_request *req) 267 { 268 int i; 269 270 cmd->initiator_flags = 271 cpu_to_le32(IWL_TOF_INITIATOR_FLAGS_MACADDR_RANDOM | 272 IWL_TOF_INITIATOR_FLAGS_NON_ASAP_SUPPORT); 273 cmd->request_id = req->cookie; 274 cmd->num_of_ap = req->n_peers; 275 276 /* 277 * Use a large value for "no timeout". Don't use the maximum value 278 * because of fw limitations. 279 */ 280 if (req->timeout) 281 cmd->req_timeout_ms = cpu_to_le32(req->timeout); 282 else 283 cmd->req_timeout_ms = cpu_to_le32(0xfffff); 284 285 memcpy(cmd->macaddr_template, req->mac_addr, ETH_ALEN); 286 for (i = 0; i < ETH_ALEN; i++) 287 cmd->macaddr_mask[i] = ~req->mac_addr_mask[i]; 288 289 if (vif->cfg.assoc) { 290 memcpy(cmd->range_req_bssid, vif->bss_conf.bssid, ETH_ALEN); 291 292 /* AP's TSF is only relevant if associated */ 293 for (i = 0; i < req->n_peers; i++) { 294 if (req->peers[i].report_ap_tsf) { 295 struct iwl_mvm_vif *mvmvif = 296 iwl_mvm_vif_from_mac80211(vif); 297 298 cmd->tsf_mac_id = cpu_to_le32(mvmvif->id); 299 return; 300 } 301 } 302 } else { 303 eth_broadcast_addr(cmd->range_req_bssid); 304 } 305 306 /* Don't report AP's TSF */ 307 cmd->tsf_mac_id = cpu_to_le32(0xff); 308 } 309 310 static void iwl_mvm_ftm_cmd_v8(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 311 struct iwl_tof_range_req_cmd_v8 *cmd, 312 struct cfg80211_pmsr_request *req) 313 { 314 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)cmd, req); 315 } 316 317 static int 318 iwl_mvm_ftm_target_chandef_v1(struct iwl_mvm *mvm, 319 struct cfg80211_pmsr_request_peer *peer, 320 u8 *channel, u8 *bandwidth, 321 u8 *ctrl_ch_position) 322 { 323 u32 freq = peer->chandef.chan->center_freq; 324 325 *channel = ieee80211_frequency_to_channel(freq); 326 327 switch (peer->chandef.width) { 328 case NL80211_CHAN_WIDTH_20_NOHT: 329 *bandwidth = IWL_TOF_BW_20_LEGACY; 330 break; 331 case NL80211_CHAN_WIDTH_20: 332 *bandwidth = IWL_TOF_BW_20_HT; 333 break; 334 case NL80211_CHAN_WIDTH_40: 335 *bandwidth = IWL_TOF_BW_40; 336 break; 337 case NL80211_CHAN_WIDTH_80: 338 *bandwidth = IWL_TOF_BW_80; 339 break; 340 default: 341 IWL_ERR(mvm, "Unsupported BW in FTM request (%d)\n", 342 peer->chandef.width); 343 return -EINVAL; 344 } 345 346 *ctrl_ch_position = (peer->chandef.width > NL80211_CHAN_WIDTH_20) ? 347 iwl_mvm_get_ctrl_pos(&peer->chandef) : 0; 348 349 return 0; 350 } 351 352 static int 353 iwl_mvm_ftm_target_chandef_v2(struct iwl_mvm *mvm, 354 struct cfg80211_pmsr_request_peer *peer, 355 u8 *channel, u8 *format_bw, 356 u8 *ctrl_ch_position) 357 { 358 u32 freq = peer->chandef.chan->center_freq; 359 u8 cmd_ver; 360 361 *channel = ieee80211_frequency_to_channel(freq); 362 363 switch (peer->chandef.width) { 364 case NL80211_CHAN_WIDTH_20_NOHT: 365 *format_bw = IWL_LOCATION_FRAME_FORMAT_LEGACY; 366 *format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS; 367 break; 368 case NL80211_CHAN_WIDTH_20: 369 *format_bw = IWL_LOCATION_FRAME_FORMAT_HT; 370 *format_bw |= IWL_LOCATION_BW_20MHZ << LOCATION_BW_POS; 371 break; 372 case NL80211_CHAN_WIDTH_40: 373 *format_bw = IWL_LOCATION_FRAME_FORMAT_HT; 374 *format_bw |= IWL_LOCATION_BW_40MHZ << LOCATION_BW_POS; 375 break; 376 case NL80211_CHAN_WIDTH_80: 377 *format_bw = IWL_LOCATION_FRAME_FORMAT_VHT; 378 *format_bw |= IWL_LOCATION_BW_80MHZ << LOCATION_BW_POS; 379 break; 380 case NL80211_CHAN_WIDTH_160: 381 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, 382 WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 383 IWL_FW_CMD_VER_UNKNOWN); 384 385 if (cmd_ver >= 13) { 386 *format_bw = IWL_LOCATION_FRAME_FORMAT_HE; 387 *format_bw |= IWL_LOCATION_BW_160MHZ << LOCATION_BW_POS; 388 break; 389 } 390 fallthrough; 391 default: 392 IWL_ERR(mvm, "Unsupported BW in FTM request (%d)\n", 393 peer->chandef.width); 394 return -EINVAL; 395 } 396 397 /* non EDCA based measurement must use HE preamble */ 398 if (peer->ftm.trigger_based || peer->ftm.non_trigger_based) 399 *format_bw |= IWL_LOCATION_FRAME_FORMAT_HE; 400 401 *ctrl_ch_position = (peer->chandef.width > NL80211_CHAN_WIDTH_20) ? 402 iwl_mvm_get_ctrl_pos(&peer->chandef) : 0; 403 404 return 0; 405 } 406 407 static int 408 iwl_mvm_ftm_put_target_v2(struct iwl_mvm *mvm, 409 struct cfg80211_pmsr_request_peer *peer, 410 struct iwl_tof_range_req_ap_entry_v2 *target) 411 { 412 int ret; 413 414 ret = iwl_mvm_ftm_target_chandef_v1(mvm, peer, &target->channel_num, 415 &target->bandwidth, 416 &target->ctrl_ch_position); 417 if (ret) 418 return ret; 419 420 memcpy(target->bssid, peer->addr, ETH_ALEN); 421 target->burst_period = 422 cpu_to_le16(peer->ftm.burst_period); 423 target->samples_per_burst = peer->ftm.ftms_per_burst; 424 target->num_of_bursts = peer->ftm.num_bursts_exp; 425 target->measure_type = 0; /* regular two-sided FTM */ 426 target->retries_per_sample = peer->ftm.ftmr_retries; 427 target->asap_mode = peer->ftm.asap; 428 target->enable_dyn_ack = IWL_MVM_FTM_INITIATOR_DYNACK; 429 430 if (peer->ftm.request_lci) 431 target->location_req |= IWL_TOF_LOC_LCI; 432 if (peer->ftm.request_civicloc) 433 target->location_req |= IWL_TOF_LOC_CIVIC; 434 435 target->algo_type = IWL_MVM_FTM_INITIATOR_ALGO; 436 437 return 0; 438 } 439 440 #define FTM_SET_FLAG(flag) (*flags |= \ 441 cpu_to_le32(IWL_INITIATOR_AP_FLAGS_##flag)) 442 443 static void 444 iwl_mvm_ftm_set_target_flags(struct iwl_mvm *mvm, 445 struct cfg80211_pmsr_request_peer *peer, 446 __le32 *flags) 447 { 448 *flags = cpu_to_le32(0); 449 450 if (peer->ftm.asap) 451 FTM_SET_FLAG(ASAP); 452 453 if (peer->ftm.request_lci) 454 FTM_SET_FLAG(LCI_REQUEST); 455 456 if (peer->ftm.request_civicloc) 457 FTM_SET_FLAG(CIVIC_REQUEST); 458 459 if (IWL_MVM_FTM_INITIATOR_DYNACK) 460 FTM_SET_FLAG(DYN_ACK); 461 462 if (IWL_MVM_FTM_INITIATOR_ALGO == IWL_TOF_ALGO_TYPE_LINEAR_REG) 463 FTM_SET_FLAG(ALGO_LR); 464 else if (IWL_MVM_FTM_INITIATOR_ALGO == IWL_TOF_ALGO_TYPE_FFT) 465 FTM_SET_FLAG(ALGO_FFT); 466 467 if (peer->ftm.trigger_based) 468 FTM_SET_FLAG(TB); 469 else if (peer->ftm.non_trigger_based) 470 FTM_SET_FLAG(NON_TB); 471 472 if ((peer->ftm.trigger_based || peer->ftm.non_trigger_based) && 473 peer->ftm.lmr_feedback) 474 FTM_SET_FLAG(LMR_FEEDBACK); 475 } 476 477 static void 478 iwl_mvm_ftm_put_target_common(struct iwl_mvm *mvm, 479 struct cfg80211_pmsr_request_peer *peer, 480 struct iwl_tof_range_req_ap_entry_v6 *target) 481 { 482 memcpy(target->bssid, peer->addr, ETH_ALEN); 483 target->burst_period = 484 cpu_to_le16(peer->ftm.burst_period); 485 target->samples_per_burst = peer->ftm.ftms_per_burst; 486 target->num_of_bursts = peer->ftm.num_bursts_exp; 487 target->ftmr_max_retries = peer->ftm.ftmr_retries; 488 iwl_mvm_ftm_set_target_flags(mvm, peer, &target->initiator_ap_flags); 489 } 490 491 static int 492 iwl_mvm_ftm_put_target_v3(struct iwl_mvm *mvm, 493 struct cfg80211_pmsr_request_peer *peer, 494 struct iwl_tof_range_req_ap_entry_v3 *target) 495 { 496 int ret; 497 498 ret = iwl_mvm_ftm_target_chandef_v1(mvm, peer, &target->channel_num, 499 &target->bandwidth, 500 &target->ctrl_ch_position); 501 if (ret) 502 return ret; 503 504 /* 505 * Versions 3 and 4 has some common fields, so 506 * iwl_mvm_ftm_put_target_common() can be used for version 7 too. 507 */ 508 iwl_mvm_ftm_put_target_common(mvm, peer, (void *)target); 509 510 return 0; 511 } 512 513 static int 514 iwl_mvm_ftm_put_target_v4(struct iwl_mvm *mvm, 515 struct cfg80211_pmsr_request_peer *peer, 516 struct iwl_tof_range_req_ap_entry_v4 *target) 517 { 518 int ret; 519 520 ret = iwl_mvm_ftm_target_chandef_v2(mvm, peer, &target->channel_num, 521 &target->format_bw, 522 &target->ctrl_ch_position); 523 if (ret) 524 return ret; 525 526 iwl_mvm_ftm_put_target_common(mvm, peer, (void *)target); 527 528 return 0; 529 } 530 531 static int iwl_mvm_ftm_set_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 532 struct cfg80211_pmsr_request_peer *peer, 533 u8 *sta_id, __le32 *flags) 534 { 535 if (vif->cfg.assoc) { 536 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 537 struct ieee80211_sta *sta; 538 struct ieee80211_bss_conf *link_conf; 539 unsigned int link_id; 540 541 rcu_read_lock(); 542 for_each_vif_active_link(vif, link_conf, link_id) { 543 if (memcmp(peer->addr, link_conf->bssid, ETH_ALEN)) 544 continue; 545 546 *sta_id = mvmvif->link[link_id]->ap_sta_id; 547 sta = rcu_dereference(mvm->fw_id_to_mac_id[*sta_id]); 548 if (WARN_ON_ONCE(IS_ERR_OR_NULL(sta))) { 549 rcu_read_unlock(); 550 return PTR_ERR_OR_ZERO(sta); 551 } 552 553 if (sta->mfp && (peer->ftm.trigger_based || 554 peer->ftm.non_trigger_based)) 555 FTM_SET_FLAG(PMF); 556 break; 557 } 558 rcu_read_unlock(); 559 560 #ifdef CONFIG_IWLWIFI_DEBUGFS 561 if (mvmvif->ftm_unprotected) { 562 *sta_id = IWL_MVM_INVALID_STA; 563 *flags &= ~cpu_to_le32(IWL_INITIATOR_AP_FLAGS_PMF); 564 } 565 #endif 566 } else { 567 *sta_id = IWL_MVM_INVALID_STA; 568 } 569 570 return 0; 571 } 572 573 static int 574 iwl_mvm_ftm_put_target(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 575 struct cfg80211_pmsr_request_peer *peer, 576 struct iwl_tof_range_req_ap_entry_v6 *target) 577 { 578 int ret; 579 580 ret = iwl_mvm_ftm_target_chandef_v2(mvm, peer, &target->channel_num, 581 &target->format_bw, 582 &target->ctrl_ch_position); 583 if (ret) 584 return ret; 585 586 iwl_mvm_ftm_put_target_common(mvm, peer, target); 587 588 iwl_mvm_ftm_set_sta(mvm, vif, peer, &target->sta_id, 589 &target->initiator_ap_flags); 590 591 /* 592 * TODO: Beacon interval is currently unknown, so use the common value 593 * of 100 TUs. 594 */ 595 target->beacon_interval = cpu_to_le16(100); 596 return 0; 597 } 598 599 static int iwl_mvm_ftm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *hcmd) 600 { 601 u32 status; 602 int err = iwl_mvm_send_cmd_status(mvm, hcmd, &status); 603 604 if (!err && status) { 605 IWL_ERR(mvm, "FTM range request command failure, status: %u\n", 606 status); 607 err = iwl_ftm_range_request_status_to_err(status); 608 } 609 610 return err; 611 } 612 613 static int iwl_mvm_ftm_start_v5(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 614 struct cfg80211_pmsr_request *req) 615 { 616 struct iwl_tof_range_req_cmd_v5 cmd_v5; 617 struct iwl_host_cmd hcmd = { 618 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 619 .dataflags[0] = IWL_HCMD_DFL_DUP, 620 .data[0] = &cmd_v5, 621 .len[0] = sizeof(cmd_v5), 622 }; 623 u8 i; 624 int err; 625 626 iwl_mvm_ftm_cmd_v5(mvm, vif, &cmd_v5, req); 627 628 for (i = 0; i < cmd_v5.num_of_ap; i++) { 629 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 630 631 err = iwl_mvm_ftm_put_target_v2(mvm, peer, &cmd_v5.ap[i]); 632 if (err) 633 return err; 634 } 635 636 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 637 } 638 639 static int iwl_mvm_ftm_start_v7(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 640 struct cfg80211_pmsr_request *req) 641 { 642 struct iwl_tof_range_req_cmd_v7 cmd_v7; 643 struct iwl_host_cmd hcmd = { 644 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 645 .dataflags[0] = IWL_HCMD_DFL_DUP, 646 .data[0] = &cmd_v7, 647 .len[0] = sizeof(cmd_v7), 648 }; 649 u8 i; 650 int err; 651 652 /* 653 * Versions 7 and 8 has the same structure except from the responders 654 * list, so iwl_mvm_ftm_cmd() can be used for version 7 too. 655 */ 656 iwl_mvm_ftm_cmd_v8(mvm, vif, (void *)&cmd_v7, req); 657 658 for (i = 0; i < cmd_v7.num_of_ap; i++) { 659 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 660 661 err = iwl_mvm_ftm_put_target_v3(mvm, peer, &cmd_v7.ap[i]); 662 if (err) 663 return err; 664 } 665 666 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 667 } 668 669 static int iwl_mvm_ftm_start_v8(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 670 struct cfg80211_pmsr_request *req) 671 { 672 struct iwl_tof_range_req_cmd_v8 cmd; 673 struct iwl_host_cmd hcmd = { 674 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 675 .dataflags[0] = IWL_HCMD_DFL_DUP, 676 .data[0] = &cmd, 677 .len[0] = sizeof(cmd), 678 }; 679 u8 i; 680 int err; 681 682 iwl_mvm_ftm_cmd_v8(mvm, vif, (void *)&cmd, req); 683 684 for (i = 0; i < cmd.num_of_ap; i++) { 685 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 686 687 err = iwl_mvm_ftm_put_target_v4(mvm, peer, &cmd.ap[i]); 688 if (err) 689 return err; 690 } 691 692 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 693 } 694 695 static int iwl_mvm_ftm_start_v9(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 696 struct cfg80211_pmsr_request *req) 697 { 698 struct iwl_tof_range_req_cmd_v9 cmd; 699 struct iwl_host_cmd hcmd = { 700 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 701 .dataflags[0] = IWL_HCMD_DFL_DUP, 702 .data[0] = &cmd, 703 .len[0] = sizeof(cmd), 704 }; 705 u8 i; 706 int err; 707 708 iwl_mvm_ftm_cmd_common(mvm, vif, &cmd, req); 709 710 for (i = 0; i < cmd.num_of_ap; i++) { 711 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 712 struct iwl_tof_range_req_ap_entry_v6 *target = &cmd.ap[i]; 713 714 err = iwl_mvm_ftm_put_target(mvm, vif, peer, target); 715 if (err) 716 return err; 717 } 718 719 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 720 } 721 722 static void iter(struct ieee80211_hw *hw, 723 struct ieee80211_vif *vif, 724 struct ieee80211_sta *sta, 725 struct ieee80211_key_conf *key, 726 void *data) 727 { 728 struct iwl_mvm_ftm_iter_data *target = data; 729 730 if (!sta || memcmp(sta->addr, target->bssid, ETH_ALEN)) 731 return; 732 733 WARN_ON(!sta->mfp); 734 735 target->tk = key->key; 736 *target->cipher = iwl_mvm_cipher_to_location_cipher(key->cipher); 737 WARN_ON(*target->cipher == IWL_LOCATION_CIPHER_INVALID); 738 } 739 740 static void 741 iwl_mvm_ftm_set_secured_ranging(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 742 u8 *bssid, u8 *cipher, u8 *hltk, u8 *tk, 743 u8 *rx_pn, u8 *tx_pn, __le32 *flags) 744 { 745 struct iwl_mvm_ftm_pasn_entry *entry; 746 #ifdef CONFIG_IWLWIFI_DEBUGFS 747 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 748 749 if (mvmvif->ftm_unprotected) 750 return; 751 #endif 752 753 if (!(le32_to_cpu(*flags) & (IWL_INITIATOR_AP_FLAGS_NON_TB | 754 IWL_INITIATOR_AP_FLAGS_TB))) 755 return; 756 757 lockdep_assert_held(&mvm->mutex); 758 759 list_for_each_entry(entry, &mvm->ftm_initiator.pasn_list, list) { 760 if (memcmp(entry->addr, bssid, sizeof(entry->addr))) 761 continue; 762 763 *cipher = entry->cipher; 764 765 if (entry->flags & IWL_MVM_PASN_FLAG_HAS_HLTK) 766 memcpy(hltk, entry->hltk, sizeof(entry->hltk)); 767 else 768 memset(hltk, 0, sizeof(entry->hltk)); 769 770 if (vif->cfg.assoc && 771 !memcmp(vif->bss_conf.bssid, bssid, ETH_ALEN)) { 772 struct iwl_mvm_ftm_iter_data target; 773 774 target.bssid = bssid; 775 ieee80211_iter_keys(mvm->hw, vif, iter, &target); 776 } else { 777 memcpy(tk, entry->tk, sizeof(entry->tk)); 778 } 779 780 memcpy(rx_pn, entry->rx_pn, sizeof(entry->rx_pn)); 781 memcpy(tx_pn, entry->tx_pn, sizeof(entry->tx_pn)); 782 783 FTM_SET_FLAG(SECURED); 784 return; 785 } 786 } 787 788 static int 789 iwl_mvm_ftm_put_target_v7(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 790 struct cfg80211_pmsr_request_peer *peer, 791 struct iwl_tof_range_req_ap_entry_v7 *target) 792 { 793 int err = iwl_mvm_ftm_put_target(mvm, vif, peer, (void *)target); 794 if (err) 795 return err; 796 797 iwl_mvm_ftm_set_secured_ranging(mvm, vif, target->bssid, 798 &target->cipher, target->hltk, 799 target->tk, target->rx_pn, 800 target->tx_pn, 801 &target->initiator_ap_flags); 802 return err; 803 } 804 805 static int iwl_mvm_ftm_start_v11(struct iwl_mvm *mvm, 806 struct ieee80211_vif *vif, 807 struct cfg80211_pmsr_request *req) 808 { 809 struct iwl_tof_range_req_cmd_v11 cmd; 810 struct iwl_host_cmd hcmd = { 811 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 812 .dataflags[0] = IWL_HCMD_DFL_DUP, 813 .data[0] = &cmd, 814 .len[0] = sizeof(cmd), 815 }; 816 u8 i; 817 int err; 818 819 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)&cmd, req); 820 821 for (i = 0; i < cmd.num_of_ap; i++) { 822 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 823 struct iwl_tof_range_req_ap_entry_v7 *target = &cmd.ap[i]; 824 825 err = iwl_mvm_ftm_put_target_v7(mvm, vif, peer, target); 826 if (err) 827 return err; 828 } 829 830 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 831 } 832 833 static void 834 iwl_mvm_ftm_set_ndp_params(struct iwl_mvm *mvm, 835 struct iwl_tof_range_req_ap_entry_v8 *target) 836 { 837 /* Only 2 STS are supported on Tx */ 838 u32 i2r_max_sts = IWL_MVM_FTM_I2R_MAX_STS > 1 ? 1 : 839 IWL_MVM_FTM_I2R_MAX_STS; 840 841 target->r2i_ndp_params = IWL_MVM_FTM_R2I_MAX_REP | 842 (IWL_MVM_FTM_R2I_MAX_STS << IWL_LOCATION_MAX_STS_POS); 843 target->i2r_ndp_params = IWL_MVM_FTM_I2R_MAX_REP | 844 (i2r_max_sts << IWL_LOCATION_MAX_STS_POS); 845 target->r2i_max_total_ltf = IWL_MVM_FTM_R2I_MAX_TOTAL_LTF; 846 target->i2r_max_total_ltf = IWL_MVM_FTM_I2R_MAX_TOTAL_LTF; 847 } 848 849 static int 850 iwl_mvm_ftm_put_target_v8(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 851 struct cfg80211_pmsr_request_peer *peer, 852 struct iwl_tof_range_req_ap_entry_v8 *target) 853 { 854 u32 flags; 855 int ret = iwl_mvm_ftm_put_target_v7(mvm, vif, peer, (void *)target); 856 857 if (ret) 858 return ret; 859 860 iwl_mvm_ftm_set_ndp_params(mvm, target); 861 862 /* 863 * If secure LTF is turned off, replace the flag with PMF only 864 */ 865 flags = le32_to_cpu(target->initiator_ap_flags); 866 if (flags & IWL_INITIATOR_AP_FLAGS_SECURED) { 867 if (!IWL_MVM_FTM_INITIATOR_SECURE_LTF) 868 flags &= ~IWL_INITIATOR_AP_FLAGS_SECURED; 869 870 flags |= IWL_INITIATOR_AP_FLAGS_PMF; 871 target->initiator_ap_flags = cpu_to_le32(flags); 872 } 873 874 return 0; 875 } 876 877 static int iwl_mvm_ftm_start_v12(struct iwl_mvm *mvm, 878 struct ieee80211_vif *vif, 879 struct cfg80211_pmsr_request *req) 880 { 881 struct iwl_tof_range_req_cmd_v12 cmd; 882 struct iwl_host_cmd hcmd = { 883 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 884 .dataflags[0] = IWL_HCMD_DFL_DUP, 885 .data[0] = &cmd, 886 .len[0] = sizeof(cmd), 887 }; 888 u8 i; 889 int err; 890 891 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)&cmd, req); 892 893 for (i = 0; i < cmd.num_of_ap; i++) { 894 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 895 struct iwl_tof_range_req_ap_entry_v8 *target = &cmd.ap[i]; 896 897 err = iwl_mvm_ftm_put_target_v8(mvm, vif, peer, target); 898 if (err) 899 return err; 900 } 901 902 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 903 } 904 905 static int iwl_mvm_ftm_start_v13(struct iwl_mvm *mvm, 906 struct ieee80211_vif *vif, 907 struct cfg80211_pmsr_request *req) 908 { 909 struct iwl_tof_range_req_cmd_v13 cmd; 910 struct iwl_host_cmd hcmd = { 911 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 912 .dataflags[0] = IWL_HCMD_DFL_DUP, 913 .data[0] = &cmd, 914 .len[0] = sizeof(cmd), 915 }; 916 u8 i; 917 int err; 918 919 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)&cmd, req); 920 921 for (i = 0; i < cmd.num_of_ap; i++) { 922 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 923 struct iwl_tof_range_req_ap_entry_v9 *target = &cmd.ap[i]; 924 925 err = iwl_mvm_ftm_put_target_v8(mvm, vif, peer, (void *)target); 926 if (err) 927 return err; 928 929 if (peer->ftm.trigger_based || peer->ftm.non_trigger_based) 930 target->bss_color = peer->ftm.bss_color; 931 932 if (peer->ftm.non_trigger_based) { 933 target->min_time_between_msr = 934 cpu_to_le16(IWL_MVM_FTM_NON_TB_MIN_TIME_BETWEEN_MSR); 935 target->burst_period = 936 cpu_to_le16(IWL_MVM_FTM_NON_TB_MAX_TIME_BETWEEN_MSR); 937 } else { 938 target->min_time_between_msr = cpu_to_le16(0); 939 } 940 941 target->band = 942 iwl_mvm_phy_band_from_nl80211(peer->chandef.chan->band); 943 } 944 945 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 946 } 947 948 static int 949 iwl_mvm_ftm_put_target_v10(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 950 struct cfg80211_pmsr_request_peer *peer, 951 struct iwl_tof_range_req_ap_entry_v10 *target) 952 { 953 u32 i2r_max_sts, flags; 954 int ret; 955 956 ret = iwl_mvm_ftm_target_chandef_v2(mvm, peer, &target->channel_num, 957 &target->format_bw, 958 &target->ctrl_ch_position); 959 if (ret) 960 return ret; 961 962 memcpy(target->bssid, peer->addr, ETH_ALEN); 963 target->burst_period = 964 cpu_to_le16(peer->ftm.burst_period); 965 target->samples_per_burst = peer->ftm.ftms_per_burst; 966 target->num_of_bursts = peer->ftm.num_bursts_exp; 967 iwl_mvm_ftm_set_target_flags(mvm, peer, &target->initiator_ap_flags); 968 iwl_mvm_ftm_set_sta(mvm, vif, peer, &target->sta_id, 969 &target->initiator_ap_flags); 970 iwl_mvm_ftm_set_secured_ranging(mvm, vif, target->bssid, 971 &target->cipher, target->hltk, 972 target->tk, target->rx_pn, 973 target->tx_pn, 974 &target->initiator_ap_flags); 975 976 i2r_max_sts = IWL_MVM_FTM_I2R_MAX_STS > 1 ? 1 : 977 IWL_MVM_FTM_I2R_MAX_STS; 978 979 target->r2i_ndp_params = IWL_MVM_FTM_R2I_MAX_REP | 980 (IWL_MVM_FTM_R2I_MAX_STS << IWL_LOCATION_MAX_STS_POS) | 981 (IWL_MVM_FTM_R2I_MAX_TOTAL_LTF << IWL_LOCATION_TOTAL_LTF_POS); 982 target->i2r_ndp_params = IWL_MVM_FTM_I2R_MAX_REP | 983 (i2r_max_sts << IWL_LOCATION_MAX_STS_POS) | 984 (IWL_MVM_FTM_I2R_MAX_TOTAL_LTF << IWL_LOCATION_TOTAL_LTF_POS); 985 986 if (peer->ftm.non_trigger_based) { 987 target->min_time_between_msr = 988 cpu_to_le16(IWL_MVM_FTM_NON_TB_MIN_TIME_BETWEEN_MSR); 989 target->burst_period = 990 cpu_to_le16(IWL_MVM_FTM_NON_TB_MAX_TIME_BETWEEN_MSR); 991 } else { 992 target->min_time_between_msr = cpu_to_le16(0); 993 } 994 995 target->band = 996 iwl_mvm_phy_band_from_nl80211(peer->chandef.chan->band); 997 998 /* 999 * TODO: Beacon interval is currently unknown, so use the common value 1000 * of 100 TUs. 1001 */ 1002 target->beacon_interval = cpu_to_le16(100); 1003 1004 /* 1005 * If secure LTF is turned off, replace the flag with PMF only 1006 */ 1007 flags = le32_to_cpu(target->initiator_ap_flags); 1008 if (flags & IWL_INITIATOR_AP_FLAGS_SECURED) { 1009 if (!IWL_MVM_FTM_INITIATOR_SECURE_LTF) 1010 flags &= ~IWL_INITIATOR_AP_FLAGS_SECURED; 1011 1012 flags |= IWL_INITIATOR_AP_FLAGS_PMF; 1013 target->initiator_ap_flags = cpu_to_le32(flags); 1014 } 1015 1016 return 0; 1017 } 1018 1019 static int iwl_mvm_ftm_start_v14(struct iwl_mvm *mvm, 1020 struct ieee80211_vif *vif, 1021 struct cfg80211_pmsr_request *req) 1022 { 1023 struct iwl_tof_range_req_cmd_v14 cmd; 1024 struct iwl_host_cmd hcmd = { 1025 .id = WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 1026 .dataflags[0] = IWL_HCMD_DFL_DUP, 1027 .data[0] = &cmd, 1028 .len[0] = sizeof(cmd), 1029 }; 1030 u8 i; 1031 int err; 1032 1033 iwl_mvm_ftm_cmd_common(mvm, vif, (void *)&cmd, req); 1034 1035 for (i = 0; i < cmd.num_of_ap; i++) { 1036 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 1037 struct iwl_tof_range_req_ap_entry_v10 *target = &cmd.ap[i]; 1038 1039 err = iwl_mvm_ftm_put_target_v10(mvm, vif, peer, target); 1040 if (err) 1041 return err; 1042 } 1043 1044 return iwl_mvm_ftm_send_cmd(mvm, &hcmd); 1045 } 1046 1047 int iwl_mvm_ftm_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif, 1048 struct cfg80211_pmsr_request *req) 1049 { 1050 bool new_api = fw_has_api(&mvm->fw->ucode_capa, 1051 IWL_UCODE_TLV_API_FTM_NEW_RANGE_REQ); 1052 int err; 1053 1054 lockdep_assert_held(&mvm->mutex); 1055 1056 if (mvm->ftm_initiator.req) 1057 return -EBUSY; 1058 1059 if (new_api) { 1060 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, 1061 WIDE_ID(LOCATION_GROUP, TOF_RANGE_REQ_CMD), 1062 IWL_FW_CMD_VER_UNKNOWN); 1063 1064 switch (cmd_ver) { 1065 case 14: 1066 err = iwl_mvm_ftm_start_v14(mvm, vif, req); 1067 break; 1068 case 13: 1069 err = iwl_mvm_ftm_start_v13(mvm, vif, req); 1070 break; 1071 case 12: 1072 err = iwl_mvm_ftm_start_v12(mvm, vif, req); 1073 break; 1074 case 11: 1075 err = iwl_mvm_ftm_start_v11(mvm, vif, req); 1076 break; 1077 case 9: 1078 case 10: 1079 err = iwl_mvm_ftm_start_v9(mvm, vif, req); 1080 break; 1081 case 8: 1082 err = iwl_mvm_ftm_start_v8(mvm, vif, req); 1083 break; 1084 default: 1085 err = iwl_mvm_ftm_start_v7(mvm, vif, req); 1086 break; 1087 } 1088 } else { 1089 err = iwl_mvm_ftm_start_v5(mvm, vif, req); 1090 } 1091 1092 if (!err) { 1093 mvm->ftm_initiator.req = req; 1094 mvm->ftm_initiator.req_wdev = ieee80211_vif_to_wdev(vif); 1095 } 1096 1097 return err; 1098 } 1099 1100 void iwl_mvm_ftm_abort(struct iwl_mvm *mvm, struct cfg80211_pmsr_request *req) 1101 { 1102 struct iwl_tof_range_abort_cmd cmd = { 1103 .request_id = req->cookie, 1104 }; 1105 1106 lockdep_assert_held(&mvm->mutex); 1107 1108 if (req != mvm->ftm_initiator.req) 1109 return; 1110 1111 iwl_mvm_ftm_reset(mvm); 1112 1113 if (iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(LOCATION_GROUP, TOF_RANGE_ABORT_CMD), 1114 0, sizeof(cmd), &cmd)) 1115 IWL_ERR(mvm, "failed to abort FTM process\n"); 1116 } 1117 1118 static int iwl_mvm_ftm_find_peer(struct cfg80211_pmsr_request *req, 1119 const u8 *addr) 1120 { 1121 int i; 1122 1123 for (i = 0; i < req->n_peers; i++) { 1124 struct cfg80211_pmsr_request_peer *peer = &req->peers[i]; 1125 1126 if (ether_addr_equal_unaligned(peer->addr, addr)) 1127 return i; 1128 } 1129 1130 return -ENOENT; 1131 } 1132 1133 static u64 iwl_mvm_ftm_get_host_time(struct iwl_mvm *mvm, __le32 fw_gp2_ts) 1134 { 1135 u32 gp2_ts = le32_to_cpu(fw_gp2_ts); 1136 u32 curr_gp2, diff; 1137 u64 now_from_boot_ns; 1138 1139 iwl_mvm_get_sync_time(mvm, CLOCK_BOOTTIME, &curr_gp2, 1140 &now_from_boot_ns, NULL); 1141 1142 if (curr_gp2 >= gp2_ts) 1143 diff = curr_gp2 - gp2_ts; 1144 else 1145 diff = curr_gp2 + (U32_MAX - gp2_ts + 1); 1146 1147 return now_from_boot_ns - (u64)diff * 1000; 1148 } 1149 1150 static void iwl_mvm_ftm_get_lci_civic(struct iwl_mvm *mvm, 1151 struct cfg80211_pmsr_result *res) 1152 { 1153 struct iwl_mvm_loc_entry *entry; 1154 1155 list_for_each_entry(entry, &mvm->ftm_initiator.loc_list, list) { 1156 if (!ether_addr_equal_unaligned(res->addr, entry->addr)) 1157 continue; 1158 1159 if (entry->lci_len) { 1160 res->ftm.lci_len = entry->lci_len; 1161 res->ftm.lci = entry->buf; 1162 } 1163 1164 if (entry->civic_len) { 1165 res->ftm.civicloc_len = entry->civic_len; 1166 res->ftm.civicloc = entry->buf + entry->lci_len; 1167 } 1168 1169 /* we found the entry we needed */ 1170 break; 1171 } 1172 } 1173 1174 static int iwl_mvm_ftm_range_resp_valid(struct iwl_mvm *mvm, u8 request_id, 1175 u8 num_of_aps) 1176 { 1177 lockdep_assert_held(&mvm->mutex); 1178 1179 if (request_id != (u8)mvm->ftm_initiator.req->cookie) { 1180 IWL_ERR(mvm, "Request ID mismatch, got %u, active %u\n", 1181 request_id, (u8)mvm->ftm_initiator.req->cookie); 1182 return -EINVAL; 1183 } 1184 1185 if (num_of_aps > mvm->ftm_initiator.req->n_peers) { 1186 IWL_ERR(mvm, "FTM range response invalid\n"); 1187 return -EINVAL; 1188 } 1189 1190 return 0; 1191 } 1192 1193 static void iwl_mvm_ftm_rtt_smoothing(struct iwl_mvm *mvm, 1194 struct cfg80211_pmsr_result *res) 1195 { 1196 struct iwl_mvm_smooth_entry *resp = NULL, *iter; 1197 s64 rtt_avg, rtt = res->ftm.rtt_avg; 1198 u32 undershoot, overshoot; 1199 u8 alpha; 1200 1201 if (!IWL_MVM_FTM_INITIATOR_ENABLE_SMOOTH) 1202 return; 1203 1204 WARN_ON(rtt < 0); 1205 1206 if (res->status != NL80211_PMSR_STATUS_SUCCESS) { 1207 IWL_DEBUG_INFO(mvm, 1208 ": %pM: ignore failed measurement. Status=%u\n", 1209 res->addr, res->status); 1210 return; 1211 } 1212 1213 list_for_each_entry(iter, &mvm->ftm_initiator.smooth.resp, list) { 1214 if (!memcmp(res->addr, iter->addr, ETH_ALEN)) { 1215 resp = iter; 1216 break; 1217 } 1218 } 1219 1220 if (!resp) { 1221 resp = kzalloc(sizeof(*resp), GFP_KERNEL); 1222 if (!resp) 1223 return; 1224 1225 memcpy(resp->addr, res->addr, ETH_ALEN); 1226 list_add_tail(&resp->list, &mvm->ftm_initiator.smooth.resp); 1227 1228 resp->rtt_avg = rtt; 1229 1230 IWL_DEBUG_INFO(mvm, "new: %pM: rtt_avg=%lld\n", 1231 resp->addr, resp->rtt_avg); 1232 goto update_time; 1233 } 1234 1235 if (res->host_time - resp->host_time > 1236 IWL_MVM_FTM_INITIATOR_SMOOTH_AGE_SEC * 1000000000) { 1237 resp->rtt_avg = rtt; 1238 1239 IWL_DEBUG_INFO(mvm, "expired: %pM: rtt_avg=%lld\n", 1240 resp->addr, resp->rtt_avg); 1241 goto update_time; 1242 } 1243 1244 /* Smooth the results based on the tracked RTT average */ 1245 undershoot = IWL_MVM_FTM_INITIATOR_SMOOTH_UNDERSHOOT; 1246 overshoot = IWL_MVM_FTM_INITIATOR_SMOOTH_OVERSHOOT; 1247 alpha = IWL_MVM_FTM_INITIATOR_SMOOTH_ALPHA; 1248 1249 rtt_avg = div_s64(alpha * rtt + (100 - alpha) * resp->rtt_avg, 100); 1250 1251 IWL_DEBUG_INFO(mvm, 1252 "%pM: prev rtt_avg=%lld, new rtt_avg=%lld, rtt=%lld\n", 1253 resp->addr, resp->rtt_avg, rtt_avg, rtt); 1254 1255 /* 1256 * update the responder's average RTT results regardless of 1257 * the under/over shoot logic below 1258 */ 1259 resp->rtt_avg = rtt_avg; 1260 1261 /* smooth the results */ 1262 if (rtt_avg > rtt && (rtt_avg - rtt) > undershoot) { 1263 res->ftm.rtt_avg = rtt_avg; 1264 1265 IWL_DEBUG_INFO(mvm, 1266 "undershoot: val=%lld\n", 1267 (rtt_avg - rtt)); 1268 } else if (rtt_avg < rtt && (rtt - rtt_avg) > 1269 overshoot) { 1270 res->ftm.rtt_avg = rtt_avg; 1271 IWL_DEBUG_INFO(mvm, 1272 "overshoot: val=%lld\n", 1273 (rtt - rtt_avg)); 1274 } 1275 1276 update_time: 1277 resp->host_time = res->host_time; 1278 } 1279 1280 static void iwl_mvm_debug_range_resp(struct iwl_mvm *mvm, u8 index, 1281 struct cfg80211_pmsr_result *res) 1282 { 1283 s64 rtt_avg = div_s64(res->ftm.rtt_avg * 100, 6666); 1284 1285 IWL_DEBUG_INFO(mvm, "entry %d\n", index); 1286 IWL_DEBUG_INFO(mvm, "\tstatus: %d\n", res->status); 1287 IWL_DEBUG_INFO(mvm, "\tBSSID: %pM\n", res->addr); 1288 IWL_DEBUG_INFO(mvm, "\thost time: %llu\n", res->host_time); 1289 IWL_DEBUG_INFO(mvm, "\tburst index: %d\n", res->ftm.burst_index); 1290 IWL_DEBUG_INFO(mvm, "\tsuccess num: %u\n", res->ftm.num_ftmr_successes); 1291 IWL_DEBUG_INFO(mvm, "\trssi: %d\n", res->ftm.rssi_avg); 1292 IWL_DEBUG_INFO(mvm, "\trssi spread: %d\n", res->ftm.rssi_spread); 1293 IWL_DEBUG_INFO(mvm, "\trtt: %lld\n", res->ftm.rtt_avg); 1294 IWL_DEBUG_INFO(mvm, "\trtt var: %llu\n", res->ftm.rtt_variance); 1295 IWL_DEBUG_INFO(mvm, "\trtt spread: %llu\n", res->ftm.rtt_spread); 1296 IWL_DEBUG_INFO(mvm, "\tdistance: %lld\n", rtt_avg); 1297 } 1298 1299 static void 1300 iwl_mvm_ftm_pasn_update_pn(struct iwl_mvm *mvm, 1301 struct iwl_tof_range_rsp_ap_entry_ntfy_v6 *fw_ap) 1302 { 1303 struct iwl_mvm_ftm_pasn_entry *entry; 1304 1305 lockdep_assert_held(&mvm->mutex); 1306 1307 list_for_each_entry(entry, &mvm->ftm_initiator.pasn_list, list) { 1308 if (memcmp(fw_ap->bssid, entry->addr, sizeof(entry->addr))) 1309 continue; 1310 1311 memcpy(entry->rx_pn, fw_ap->rx_pn, sizeof(entry->rx_pn)); 1312 memcpy(entry->tx_pn, fw_ap->tx_pn, sizeof(entry->tx_pn)); 1313 return; 1314 } 1315 } 1316 1317 static u8 iwl_mvm_ftm_get_range_resp_ver(struct iwl_mvm *mvm) 1318 { 1319 if (!fw_has_api(&mvm->fw->ucode_capa, 1320 IWL_UCODE_TLV_API_FTM_NEW_RANGE_REQ)) 1321 return 5; 1322 1323 /* Starting from version 8, the FW advertises the version */ 1324 if (mvm->cmd_ver.range_resp >= 8) 1325 return mvm->cmd_ver.range_resp; 1326 else if (fw_has_api(&mvm->fw->ucode_capa, 1327 IWL_UCODE_TLV_API_FTM_RTT_ACCURACY)) 1328 return 7; 1329 1330 /* The first version of the new range request API */ 1331 return 6; 1332 } 1333 1334 static bool iwl_mvm_ftm_resp_size_validation(u8 ver, unsigned int pkt_len) 1335 { 1336 switch (ver) { 1337 case 9: 1338 case 8: 1339 return pkt_len == sizeof(struct iwl_tof_range_rsp_ntfy_v8); 1340 case 7: 1341 return pkt_len == sizeof(struct iwl_tof_range_rsp_ntfy_v7); 1342 case 6: 1343 return pkt_len == sizeof(struct iwl_tof_range_rsp_ntfy_v6); 1344 case 5: 1345 return pkt_len == sizeof(struct iwl_tof_range_rsp_ntfy_v5); 1346 default: 1347 WARN_ONCE(1, "FTM: unsupported range response version %u", ver); 1348 return false; 1349 } 1350 } 1351 1352 void iwl_mvm_ftm_range_resp(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 1353 { 1354 struct iwl_rx_packet *pkt = rxb_addr(rxb); 1355 unsigned int pkt_len = iwl_rx_packet_payload_len(pkt); 1356 struct iwl_tof_range_rsp_ntfy_v5 *fw_resp_v5 = (void *)pkt->data; 1357 struct iwl_tof_range_rsp_ntfy_v6 *fw_resp_v6 = (void *)pkt->data; 1358 struct iwl_tof_range_rsp_ntfy_v7 *fw_resp_v7 = (void *)pkt->data; 1359 struct iwl_tof_range_rsp_ntfy_v8 *fw_resp_v8 = (void *)pkt->data; 1360 int i; 1361 bool new_api = fw_has_api(&mvm->fw->ucode_capa, 1362 IWL_UCODE_TLV_API_FTM_NEW_RANGE_REQ); 1363 u8 num_of_aps, last_in_batch; 1364 u8 notif_ver = iwl_mvm_ftm_get_range_resp_ver(mvm); 1365 1366 lockdep_assert_held(&mvm->mutex); 1367 1368 if (!mvm->ftm_initiator.req) { 1369 return; 1370 } 1371 1372 if (unlikely(!iwl_mvm_ftm_resp_size_validation(notif_ver, pkt_len))) 1373 return; 1374 1375 if (new_api) { 1376 if (iwl_mvm_ftm_range_resp_valid(mvm, fw_resp_v8->request_id, 1377 fw_resp_v8->num_of_aps)) 1378 return; 1379 1380 num_of_aps = fw_resp_v8->num_of_aps; 1381 last_in_batch = fw_resp_v8->last_report; 1382 } else { 1383 if (iwl_mvm_ftm_range_resp_valid(mvm, fw_resp_v5->request_id, 1384 fw_resp_v5->num_of_aps)) 1385 return; 1386 1387 num_of_aps = fw_resp_v5->num_of_aps; 1388 last_in_batch = fw_resp_v5->last_in_batch; 1389 } 1390 1391 IWL_DEBUG_INFO(mvm, "Range response received\n"); 1392 IWL_DEBUG_INFO(mvm, "request id: %lld, num of entries: %u\n", 1393 mvm->ftm_initiator.req->cookie, num_of_aps); 1394 1395 for (i = 0; i < num_of_aps && i < IWL_MVM_TOF_MAX_APS; i++) { 1396 struct cfg80211_pmsr_result result = {}; 1397 struct iwl_tof_range_rsp_ap_entry_ntfy_v6 *fw_ap; 1398 int peer_idx; 1399 1400 if (new_api) { 1401 if (notif_ver >= 8) { 1402 fw_ap = &fw_resp_v8->ap[i]; 1403 iwl_mvm_ftm_pasn_update_pn(mvm, fw_ap); 1404 } else if (notif_ver == 7) { 1405 fw_ap = (void *)&fw_resp_v7->ap[i]; 1406 } else { 1407 fw_ap = (void *)&fw_resp_v6->ap[i]; 1408 } 1409 1410 result.final = fw_ap->last_burst; 1411 result.ap_tsf = le32_to_cpu(fw_ap->start_tsf); 1412 result.ap_tsf_valid = 1; 1413 } else { 1414 /* the first part is the same for old and new APIs */ 1415 fw_ap = (void *)&fw_resp_v5->ap[i]; 1416 /* 1417 * FIXME: the firmware needs to report this, we don't 1418 * even know the number of bursts the responder picked 1419 * (if we asked it to) 1420 */ 1421 result.final = 0; 1422 } 1423 1424 peer_idx = iwl_mvm_ftm_find_peer(mvm->ftm_initiator.req, 1425 fw_ap->bssid); 1426 if (peer_idx < 0) { 1427 IWL_WARN(mvm, 1428 "Unknown address (%pM, target #%d) in FTM response\n", 1429 fw_ap->bssid, i); 1430 continue; 1431 } 1432 1433 switch (fw_ap->measure_status) { 1434 case IWL_TOF_ENTRY_SUCCESS: 1435 result.status = NL80211_PMSR_STATUS_SUCCESS; 1436 break; 1437 case IWL_TOF_ENTRY_TIMING_MEASURE_TIMEOUT: 1438 result.status = NL80211_PMSR_STATUS_TIMEOUT; 1439 break; 1440 case IWL_TOF_ENTRY_NO_RESPONSE: 1441 result.status = NL80211_PMSR_STATUS_FAILURE; 1442 result.ftm.failure_reason = 1443 NL80211_PMSR_FTM_FAILURE_NO_RESPONSE; 1444 break; 1445 case IWL_TOF_ENTRY_REQUEST_REJECTED: 1446 result.status = NL80211_PMSR_STATUS_FAILURE; 1447 result.ftm.failure_reason = 1448 NL80211_PMSR_FTM_FAILURE_PEER_BUSY; 1449 result.ftm.busy_retry_time = fw_ap->refusal_period; 1450 break; 1451 default: 1452 result.status = NL80211_PMSR_STATUS_FAILURE; 1453 result.ftm.failure_reason = 1454 NL80211_PMSR_FTM_FAILURE_UNSPECIFIED; 1455 break; 1456 } 1457 memcpy(result.addr, fw_ap->bssid, ETH_ALEN); 1458 result.host_time = iwl_mvm_ftm_get_host_time(mvm, 1459 fw_ap->timestamp); 1460 result.type = NL80211_PMSR_TYPE_FTM; 1461 result.ftm.burst_index = mvm->ftm_initiator.responses[peer_idx]; 1462 mvm->ftm_initiator.responses[peer_idx]++; 1463 result.ftm.rssi_avg = fw_ap->rssi; 1464 result.ftm.rssi_avg_valid = 1; 1465 result.ftm.rssi_spread = fw_ap->rssi_spread; 1466 result.ftm.rssi_spread_valid = 1; 1467 result.ftm.rtt_avg = (s32)le32_to_cpu(fw_ap->rtt); 1468 result.ftm.rtt_avg_valid = 1; 1469 result.ftm.rtt_variance = le32_to_cpu(fw_ap->rtt_variance); 1470 result.ftm.rtt_variance_valid = 1; 1471 result.ftm.rtt_spread = le32_to_cpu(fw_ap->rtt_spread); 1472 result.ftm.rtt_spread_valid = 1; 1473 1474 iwl_mvm_ftm_get_lci_civic(mvm, &result); 1475 1476 iwl_mvm_ftm_rtt_smoothing(mvm, &result); 1477 1478 cfg80211_pmsr_report(mvm->ftm_initiator.req_wdev, 1479 mvm->ftm_initiator.req, 1480 &result, GFP_KERNEL); 1481 1482 if (fw_has_api(&mvm->fw->ucode_capa, 1483 IWL_UCODE_TLV_API_FTM_RTT_ACCURACY)) 1484 IWL_DEBUG_INFO(mvm, "RTT confidence: %u\n", 1485 fw_ap->rttConfidence); 1486 1487 iwl_mvm_debug_range_resp(mvm, i, &result); 1488 } 1489 1490 if (last_in_batch) { 1491 cfg80211_pmsr_complete(mvm->ftm_initiator.req_wdev, 1492 mvm->ftm_initiator.req, 1493 GFP_KERNEL); 1494 iwl_mvm_ftm_reset(mvm); 1495 } 1496 } 1497 1498 void iwl_mvm_ftm_lc_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 1499 { 1500 struct iwl_rx_packet *pkt = rxb_addr(rxb); 1501 const struct ieee80211_mgmt *mgmt = (void *)pkt->data; 1502 size_t len = iwl_rx_packet_payload_len(pkt); 1503 struct iwl_mvm_loc_entry *entry; 1504 const u8 *ies, *lci, *civic, *msr_ie; 1505 size_t ies_len, lci_len = 0, civic_len = 0; 1506 size_t baselen = IEEE80211_MIN_ACTION_SIZE + 1507 sizeof(mgmt->u.action.u.ftm); 1508 static const u8 rprt_type_lci = IEEE80211_SPCT_MSR_RPRT_TYPE_LCI; 1509 static const u8 rprt_type_civic = IEEE80211_SPCT_MSR_RPRT_TYPE_CIVIC; 1510 1511 if (len <= baselen) 1512 return; 1513 1514 lockdep_assert_held(&mvm->mutex); 1515 1516 ies = mgmt->u.action.u.ftm.variable; 1517 ies_len = len - baselen; 1518 1519 msr_ie = cfg80211_find_ie_match(WLAN_EID_MEASURE_REPORT, ies, ies_len, 1520 &rprt_type_lci, 1, 4); 1521 if (msr_ie) { 1522 lci = msr_ie + 2; 1523 lci_len = msr_ie[1]; 1524 } 1525 1526 msr_ie = cfg80211_find_ie_match(WLAN_EID_MEASURE_REPORT, ies, ies_len, 1527 &rprt_type_civic, 1, 4); 1528 if (msr_ie) { 1529 civic = msr_ie + 2; 1530 civic_len = msr_ie[1]; 1531 } 1532 1533 entry = kmalloc(sizeof(*entry) + lci_len + civic_len, GFP_KERNEL); 1534 if (!entry) 1535 return; 1536 1537 memcpy(entry->addr, mgmt->bssid, ETH_ALEN); 1538 1539 entry->lci_len = lci_len; 1540 if (lci_len) 1541 memcpy(entry->buf, lci, lci_len); 1542 1543 entry->civic_len = civic_len; 1544 if (civic_len) 1545 memcpy(entry->buf + lci_len, civic, civic_len); 1546 1547 list_add_tail(&entry->list, &mvm->ftm_initiator.loc_list); 1548 } 1549