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