1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2024-2025 Intel Corporation 4 */ 5 #include "mld.h" 6 7 #include "d3.h" 8 #include "power.h" 9 #include "hcmd.h" 10 #include "iface.h" 11 #include "mcc.h" 12 #include "sta.h" 13 #include "mlo.h" 14 #include "key.h" 15 16 #include "fw/api/d3.h" 17 #include "fw/api/offload.h" 18 #include "fw/api/sta.h" 19 #include "fw/dbg.h" 20 21 #include <net/ipv6.h> 22 #include <net/addrconf.h> 23 #include <linux/bitops.h> 24 25 /** 26 * enum iwl_mld_d3_notif - d3 notifications 27 * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF is expected/was received 28 * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF is expected/was received 29 * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF is expected/was received 30 * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF is expected/was received 31 * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF is expected/was received 32 */ 33 enum iwl_mld_d3_notif { 34 IWL_D3_NOTIF_WOWLAN_INFO = BIT(0), 35 IWL_D3_NOTIF_WOWLAN_WAKE_PKT = BIT(1), 36 IWL_D3_NOTIF_PROT_OFFLOAD = BIT(2), 37 IWL_D3_ND_MATCH_INFO = BIT(3), 38 IWL_D3_NOTIF_D3_END_NOTIF = BIT(4) 39 }; 40 41 struct iwl_mld_resume_key_iter_data { 42 struct iwl_mld *mld; 43 struct iwl_mld_wowlan_status *wowlan_status; 44 }; 45 46 struct iwl_mld_suspend_key_iter_data { 47 struct iwl_wowlan_rsc_tsc_params_cmd *rsc; 48 bool have_rsc; 49 int gtks; 50 int found_gtk_idx[4]; 51 __le32 gtk_cipher; 52 __le32 igtk_cipher; 53 __le32 bigtk_cipher; 54 }; 55 56 struct iwl_mld_mcast_key_data { 57 u8 key[WOWLAN_KEY_MAX_SIZE]; 58 u8 len; 59 u8 flags; 60 u8 id; 61 union { 62 struct { 63 struct ieee80211_key_seq aes_seq[IWL_MAX_TID_COUNT]; 64 struct ieee80211_key_seq tkip_seq[IWL_MAX_TID_COUNT]; 65 } gtk; 66 struct { 67 struct ieee80211_key_seq cmac_gmac_seq; 68 } igtk_bigtk; 69 }; 70 71 }; 72 73 struct iwl_mld_wowlan_mlo_key { 74 u8 key[WOWLAN_KEY_MAX_SIZE]; 75 u8 idx, type, link_id; 76 u8 pn[6]; 77 }; 78 79 /** 80 * struct iwl_mld_wowlan_status - contains wowlan status data from 81 * all wowlan notifications 82 * @wakeup_reasons: wakeup reasons, see &enum iwl_wowlan_wakeup_reason 83 * @replay_ctr: GTK rekey replay counter 84 * @pattern_number: number of the matched patterns on packets 85 * @last_qos_seq: QoS sequence counter of offloaded tid 86 * @num_of_gtk_rekeys: number of GTK rekeys during D3 87 * @tid_offloaded_tx: tid used by the firmware to transmit data packets 88 * while in wowlan 89 * @wake_packet: wakeup packet received 90 * @wake_packet_length: wake packet length 91 * @wake_packet_bufsize: wake packet bufsize 92 * @gtk: data of the last two used gtk's by the FW upon resume 93 * @igtk: data of the last used igtk by the FW upon resume 94 * @bigtk: data of the last two used gtk's by the FW upon resume 95 * @ptk: last seq numbers per tid passed by the FW, 96 * holds both in tkip and aes formats 97 * @num_mlo_keys: number of &struct iwl_mld_wowlan_mlo_key structs 98 * @mlo_keys: array of MLO keys 99 */ 100 struct iwl_mld_wowlan_status { 101 u32 wakeup_reasons; 102 u64 replay_ctr; 103 u16 pattern_number; 104 u16 last_qos_seq; 105 u32 num_of_gtk_rekeys; 106 u8 tid_offloaded_tx; 107 u8 *wake_packet; 108 u32 wake_packet_length; 109 u32 wake_packet_bufsize; 110 struct iwl_mld_mcast_key_data gtk[WOWLAN_GTK_KEYS_NUM]; 111 struct iwl_mld_mcast_key_data igtk; 112 struct iwl_mld_mcast_key_data bigtk[WOWLAN_BIGTK_KEYS_NUM]; 113 struct { 114 struct ieee80211_key_seq aes_seq[IWL_MAX_TID_COUNT]; 115 struct ieee80211_key_seq tkip_seq[IWL_MAX_TID_COUNT]; 116 117 } ptk; 118 119 int num_mlo_keys; 120 struct iwl_mld_wowlan_mlo_key mlo_keys[WOWLAN_MAX_MLO_KEYS]; 121 }; 122 123 #define NETDETECT_QUERY_BUF_LEN \ 124 (sizeof(struct iwl_scan_offload_profile_match) * \ 125 IWL_SCAN_MAX_PROFILES_V2) 126 127 /** 128 * struct iwl_mld_netdetect_res - contains netdetect results from 129 * match_info_notif 130 * @matched_profiles: bitmap of matched profiles, referencing the 131 * matches passed in the scan offload request 132 * @matches: array of match information, one for each match 133 */ 134 struct iwl_mld_netdetect_res { 135 u32 matched_profiles; 136 u8 matches[NETDETECT_QUERY_BUF_LEN]; 137 }; 138 139 /** 140 * struct iwl_mld_resume_data - d3 resume flow data 141 * @notifs_expected: bitmap of expected notifications from fw, 142 * see &enum iwl_mld_d3_notif 143 * @notifs_received: bitmap of received notifications from fw, 144 * see &enum iwl_mld_d3_notif 145 * @d3_end_flags: bitmap of flags from d3_end_notif 146 * @notif_handling_err: error handling one of the resume notifications 147 * @wowlan_status: wowlan status data from all wowlan notifications 148 * @netdetect_res: contains netdetect results from match_info_notif 149 */ 150 struct iwl_mld_resume_data { 151 u32 notifs_expected; 152 u32 notifs_received; 153 u32 d3_end_flags; 154 bool notif_handling_err; 155 struct iwl_mld_wowlan_status *wowlan_status; 156 struct iwl_mld_netdetect_res *netdetect_res; 157 }; 158 159 #define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT \ 160 (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \ 161 IWL_WOWLAN_WAKEUP_BY_PATTERN | \ 162 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\ 163 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\ 164 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\ 165 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD) 166 167 #define IWL_WOWLAN_OFFLOAD_TID 0 168 169 void iwl_mld_set_rekey_data(struct ieee80211_hw *hw, 170 struct ieee80211_vif *vif, 171 struct cfg80211_gtk_rekey_data *data) 172 { 173 struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw); 174 struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif); 175 struct iwl_mld_wowlan_data *wowlan_data = &mld_vif->wowlan_data; 176 177 lockdep_assert_wiphy(mld->wiphy); 178 179 wowlan_data->rekey_data.kek_len = data->kek_len; 180 wowlan_data->rekey_data.kck_len = data->kck_len; 181 memcpy(wowlan_data->rekey_data.kek, data->kek, data->kek_len); 182 memcpy(wowlan_data->rekey_data.kck, data->kck, data->kck_len); 183 wowlan_data->rekey_data.akm = data->akm & 0xFF; 184 wowlan_data->rekey_data.replay_ctr = 185 cpu_to_le64(be64_to_cpup((const __be64 *)data->replay_ctr)); 186 wowlan_data->rekey_data.valid = true; 187 } 188 189 #if IS_ENABLED(CONFIG_IPV6) 190 void iwl_mld_ipv6_addr_change(struct ieee80211_hw *hw, 191 struct ieee80211_vif *vif, 192 struct inet6_dev *idev) 193 { 194 struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif); 195 struct iwl_mld_wowlan_data *wowlan_data = &mld_vif->wowlan_data; 196 struct inet6_ifaddr *ifa; 197 int idx = 0; 198 199 memset(wowlan_data->tentative_addrs, 0, 200 sizeof(wowlan_data->tentative_addrs)); 201 202 read_lock_bh(&idev->lock); 203 list_for_each_entry(ifa, &idev->addr_list, if_list) { 204 wowlan_data->target_ipv6_addrs[idx] = ifa->addr; 205 if (ifa->flags & IFA_F_TENTATIVE) 206 __set_bit(idx, wowlan_data->tentative_addrs); 207 idx++; 208 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX) 209 break; 210 } 211 read_unlock_bh(&idev->lock); 212 213 wowlan_data->num_target_ipv6_addrs = idx; 214 } 215 #endif 216 217 static int 218 iwl_mld_netdetect_config(struct iwl_mld *mld, 219 struct ieee80211_vif *vif, 220 const struct cfg80211_wowlan *wowlan) 221 { 222 int ret; 223 struct cfg80211_sched_scan_request *netdetect_cfg = 224 wowlan->nd_config; 225 struct ieee80211_scan_ies ies = {}; 226 227 ret = iwl_mld_scan_stop(mld, IWL_MLD_SCAN_SCHED, true); 228 if (ret) 229 return ret; 230 231 ret = iwl_mld_sched_scan_start(mld, vif, netdetect_cfg, &ies, 232 IWL_MLD_SCAN_NETDETECT); 233 return ret; 234 } 235 236 static void 237 iwl_mld_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq) 238 { 239 u64 pn = le64_to_cpu(le_pn); 240 241 seq->tkip.iv16 = (u16)pn; 242 seq->tkip.iv32 = (u32)(pn >> 16); 243 } 244 245 static void 246 iwl_mld_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq) 247 { 248 u64 pn = le64_to_cpu(le_pn); 249 250 seq->ccmp.pn[0] = pn >> 40; 251 seq->ccmp.pn[1] = pn >> 32; 252 seq->ccmp.pn[2] = pn >> 24; 253 seq->ccmp.pn[3] = pn >> 16; 254 seq->ccmp.pn[4] = pn >> 8; 255 seq->ccmp.pn[5] = pn; 256 } 257 258 static void 259 iwl_mld_convert_gtk_resume_seq(struct iwl_mld_mcast_key_data *gtk_data, 260 const struct iwl_wowlan_all_rsc_tsc_v5 *sc, 261 int rsc_idx) 262 { 263 struct ieee80211_key_seq *aes_seq = gtk_data->gtk.aes_seq; 264 struct ieee80211_key_seq *tkip_seq = gtk_data->gtk.tkip_seq; 265 266 if (rsc_idx >= ARRAY_SIZE(sc->mcast_rsc)) 267 return; 268 269 /* We store both the TKIP and AES representations coming from the 270 * FW because we decode the data from there before we iterate 271 * the keys and know which type is used. 272 */ 273 for (int tid = 0; tid < IWL_MAX_TID_COUNT; tid++) { 274 iwl_mld_le64_to_tkip_seq(sc->mcast_rsc[rsc_idx][tid], 275 &tkip_seq[tid]); 276 iwl_mld_le64_to_aes_seq(sc->mcast_rsc[rsc_idx][tid], 277 &aes_seq[tid]); 278 } 279 } 280 281 static void 282 iwl_mld_convert_gtk_resume_data(struct iwl_mld *mld, 283 struct iwl_mld_wowlan_status *wowlan_status, 284 const struct iwl_wowlan_gtk_status_v3 *gtk_data, 285 const struct iwl_wowlan_all_rsc_tsc_v5 *sc) 286 { 287 int status_idx = 0; 288 289 BUILD_BUG_ON(sizeof(wowlan_status->gtk[0].key) < 290 sizeof(gtk_data[0].key)); 291 BUILD_BUG_ON(ARRAY_SIZE(wowlan_status->gtk) < WOWLAN_GTK_KEYS_NUM); 292 293 for (int notif_idx = 0; notif_idx < ARRAY_SIZE(wowlan_status->gtk); 294 notif_idx++) { 295 int rsc_idx; 296 297 if (!(gtk_data[notif_idx].key_len)) 298 continue; 299 300 wowlan_status->gtk[status_idx].len = 301 gtk_data[notif_idx].key_len; 302 wowlan_status->gtk[status_idx].flags = 303 gtk_data[notif_idx].key_flags; 304 wowlan_status->gtk[status_idx].id = 305 wowlan_status->gtk[status_idx].flags & 306 IWL_WOWLAN_GTK_IDX_MASK; 307 memcpy(wowlan_status->gtk[status_idx].key, 308 gtk_data[notif_idx].key, 309 sizeof(gtk_data[notif_idx].key)); 310 311 /* The rsc for both gtk keys are stored in gtk[0]->sc->mcast_rsc 312 * The gtk ids can be any two numbers between 0 and 3, 313 * the id_map maps between the key id and the index in sc->mcast 314 */ 315 rsc_idx = 316 sc->mcast_key_id_map[wowlan_status->gtk[status_idx].id]; 317 iwl_mld_convert_gtk_resume_seq(&wowlan_status->gtk[status_idx], 318 sc, rsc_idx); 319 320 /* if it's as long as the TKIP encryption key, copy MIC key */ 321 if (wowlan_status->gtk[status_idx].len == 322 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY) 323 memcpy(wowlan_status->gtk[status_idx].key + 324 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY, 325 gtk_data[notif_idx].tkip_mic_key, 326 sizeof(gtk_data[notif_idx].tkip_mic_key)); 327 status_idx++; 328 } 329 } 330 331 static void 332 iwl_mld_convert_ptk_resume_seq(struct iwl_mld *mld, 333 struct iwl_mld_wowlan_status *wowlan_status, 334 const struct iwl_wowlan_all_rsc_tsc_v5 *sc) 335 { 336 struct ieee80211_key_seq *aes_seq = wowlan_status->ptk.aes_seq; 337 struct ieee80211_key_seq *tkip_seq = wowlan_status->ptk.tkip_seq; 338 339 BUILD_BUG_ON(ARRAY_SIZE(sc->ucast_rsc) != IWL_MAX_TID_COUNT); 340 341 for (int tid = 0; tid < IWL_MAX_TID_COUNT; tid++) { 342 iwl_mld_le64_to_aes_seq(sc->ucast_rsc[tid], &aes_seq[tid]); 343 iwl_mld_le64_to_tkip_seq(sc->ucast_rsc[tid], &tkip_seq[tid]); 344 } 345 } 346 347 static void 348 iwl_mld_convert_mcast_ipn(struct iwl_mld_mcast_key_data *key_status, 349 const struct iwl_wowlan_igtk_status *key) 350 { 351 struct ieee80211_key_seq *seq = 352 &key_status->igtk_bigtk.cmac_gmac_seq; 353 u8 ipn_len = ARRAY_SIZE(key->ipn); 354 355 BUILD_BUG_ON(ipn_len != ARRAY_SIZE(seq->aes_gmac.pn)); 356 BUILD_BUG_ON(ipn_len != ARRAY_SIZE(seq->aes_cmac.pn)); 357 BUILD_BUG_ON(offsetof(struct ieee80211_key_seq, aes_gmac) != 358 offsetof(struct ieee80211_key_seq, aes_cmac)); 359 360 /* mac80211 expects big endian for memcmp() to work, convert. 361 * We don't have the key cipher yet so copy to both to cmac and gmac 362 */ 363 for (int i = 0; i < ipn_len; i++) { 364 seq->aes_gmac.pn[i] = key->ipn[ipn_len - i - 1]; 365 seq->aes_cmac.pn[i] = key->ipn[ipn_len - i - 1]; 366 } 367 } 368 369 static void 370 iwl_mld_convert_igtk_resume_data(struct iwl_mld_wowlan_status *wowlan_status, 371 const struct iwl_wowlan_igtk_status *igtk) 372 { 373 BUILD_BUG_ON(sizeof(wowlan_status->igtk.key) < sizeof(igtk->key)); 374 375 if (!igtk->key_len) 376 return; 377 378 wowlan_status->igtk.len = igtk->key_len; 379 wowlan_status->igtk.flags = igtk->key_flags; 380 wowlan_status->igtk.id = 381 u32_get_bits(igtk->key_flags, 382 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK) + 383 WOWLAN_IGTK_MIN_INDEX; 384 385 memcpy(wowlan_status->igtk.key, igtk->key, sizeof(igtk->key)); 386 iwl_mld_convert_mcast_ipn(&wowlan_status->igtk, igtk); 387 } 388 389 static void 390 iwl_mld_convert_bigtk_resume_data(struct iwl_mld_wowlan_status *wowlan_status, 391 const struct iwl_wowlan_igtk_status *bigtk) 392 { 393 int status_idx = 0; 394 395 BUILD_BUG_ON(ARRAY_SIZE(wowlan_status->bigtk) < WOWLAN_BIGTK_KEYS_NUM); 396 397 for (int notif_idx = 0; notif_idx < WOWLAN_BIGTK_KEYS_NUM; 398 notif_idx++) { 399 if (!bigtk[notif_idx].key_len) 400 continue; 401 402 wowlan_status->bigtk[status_idx].len = bigtk[notif_idx].key_len; 403 wowlan_status->bigtk[status_idx].flags = 404 bigtk[notif_idx].key_flags; 405 wowlan_status->bigtk[status_idx].id = 406 u32_get_bits(bigtk[notif_idx].key_flags, 407 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK) 408 + WOWLAN_BIGTK_MIN_INDEX; 409 410 BUILD_BUG_ON(sizeof(wowlan_status->bigtk[status_idx].key) < 411 sizeof(bigtk[notif_idx].key)); 412 memcpy(wowlan_status->bigtk[status_idx].key, 413 bigtk[notif_idx].key, sizeof(bigtk[notif_idx].key)); 414 iwl_mld_convert_mcast_ipn(&wowlan_status->bigtk[status_idx], 415 &bigtk[notif_idx]); 416 status_idx++; 417 } 418 } 419 420 static void 421 iwl_mld_convert_mlo_keys(struct iwl_mld *mld, 422 const struct iwl_wowlan_info_notif *notif, 423 struct iwl_mld_wowlan_status *wowlan_status) 424 { 425 if (!notif->num_mlo_link_keys) 426 return; 427 428 wowlan_status->num_mlo_keys = notif->num_mlo_link_keys; 429 430 if (IWL_FW_CHECK(mld, wowlan_status->num_mlo_keys > WOWLAN_MAX_MLO_KEYS, 431 "Too many MLO keys: %d, max %d\n", 432 wowlan_status->num_mlo_keys, WOWLAN_MAX_MLO_KEYS)) 433 wowlan_status->num_mlo_keys = WOWLAN_MAX_MLO_KEYS; 434 435 for (int i = 0; i < wowlan_status->num_mlo_keys; i++) { 436 const struct iwl_wowlan_mlo_gtk *fw_mlo_key = ¬if->mlo_gtks[i]; 437 struct iwl_mld_wowlan_mlo_key *driver_mlo_key = 438 &wowlan_status->mlo_keys[i]; 439 u16 flags = le16_to_cpu(fw_mlo_key->flags); 440 441 driver_mlo_key->link_id = 442 u16_get_bits(flags, WOWLAN_MLO_GTK_FLAG_LINK_ID_MSK); 443 driver_mlo_key->type = 444 u16_get_bits(flags, WOWLAN_MLO_GTK_FLAG_KEY_TYPE_MSK); 445 driver_mlo_key->idx = 446 u16_get_bits(flags, WOWLAN_MLO_GTK_FLAG_KEY_ID_MSK); 447 448 BUILD_BUG_ON(sizeof(driver_mlo_key->key) != sizeof(fw_mlo_key->key)); 449 BUILD_BUG_ON(sizeof(driver_mlo_key->pn) != sizeof(fw_mlo_key->pn)); 450 451 memcpy(driver_mlo_key->key, fw_mlo_key->key, sizeof(fw_mlo_key->key)); 452 memcpy(driver_mlo_key->pn, fw_mlo_key->pn, sizeof(fw_mlo_key->pn)); 453 } 454 } 455 456 static bool 457 iwl_mld_handle_wowlan_info_notif(struct iwl_mld *mld, 458 struct iwl_mld_wowlan_status *wowlan_status, 459 struct iwl_rx_packet *pkt) 460 { 461 const struct iwl_wowlan_info_notif *notif = (void *)pkt->data; 462 u32 len = iwl_rx_packet_payload_len(pkt); 463 u32 len_with_mlo_keys; 464 465 if (IWL_FW_CHECK(mld, len < sizeof(*notif), 466 "Invalid wowlan_info_notif (expected=%zu got=%u)\n", 467 sizeof(*notif), len)) 468 return true; 469 470 /* Now that we know that we have at least sizeof(notif), 471 * check also the variable length part 472 */ 473 len_with_mlo_keys = sizeof(*notif) + 474 notif->num_mlo_link_keys * sizeof(notif->mlo_gtks[0]); 475 476 if (IWL_FW_CHECK(mld, len < len_with_mlo_keys, 477 "Invalid wowlan_info_notif (expected=%ud got=%u)\n", 478 len_with_mlo_keys, len)) 479 return true; 480 481 if (IWL_FW_CHECK(mld, notif->tid_offloaded_tx != IWL_WOWLAN_OFFLOAD_TID, 482 "Invalid tid_offloaded_tx %d\n", 483 wowlan_status->tid_offloaded_tx)) 484 return true; 485 486 iwl_mld_convert_gtk_resume_data(mld, wowlan_status, notif->gtk, 487 ¬if->gtk[0].sc); 488 iwl_mld_convert_ptk_resume_seq(mld, wowlan_status, ¬if->gtk[0].sc); 489 /* only one igtk is passed by FW */ 490 iwl_mld_convert_igtk_resume_data(wowlan_status, ¬if->igtk[0]); 491 iwl_mld_convert_bigtk_resume_data(wowlan_status, notif->bigtk); 492 493 wowlan_status->replay_ctr = le64_to_cpu(notif->replay_ctr); 494 wowlan_status->pattern_number = le16_to_cpu(notif->pattern_number); 495 496 wowlan_status->tid_offloaded_tx = notif->tid_offloaded_tx; 497 wowlan_status->last_qos_seq = le16_to_cpu(notif->qos_seq_ctr); 498 wowlan_status->num_of_gtk_rekeys = 499 le32_to_cpu(notif->num_of_gtk_rekeys); 500 wowlan_status->wakeup_reasons = le32_to_cpu(notif->wakeup_reasons); 501 502 iwl_mld_convert_mlo_keys(mld, notif, wowlan_status); 503 504 return false; 505 } 506 507 static bool 508 iwl_mld_handle_wake_pkt_notif(struct iwl_mld *mld, 509 struct iwl_mld_wowlan_status *wowlan_status, 510 struct iwl_rx_packet *pkt) 511 { 512 const struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data; 513 u32 actual_size, len = iwl_rx_packet_payload_len(pkt); 514 u32 expected_size = le32_to_cpu(notif->wake_packet_length); 515 516 if (IWL_FW_CHECK(mld, len < sizeof(*notif), 517 "Invalid WoWLAN wake packet notification (expected size=%zu got=%u)\n", 518 sizeof(*notif), len)) 519 return true; 520 521 if (IWL_FW_CHECK(mld, !(wowlan_status->wakeup_reasons & 522 IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT), 523 "Got wake packet but wakeup reason is %x\n", 524 wowlan_status->wakeup_reasons)) 525 return true; 526 527 actual_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, 528 wake_packet); 529 530 /* actual_size got the padding from the notification, remove it. */ 531 if (expected_size < actual_size) 532 actual_size = expected_size; 533 wowlan_status->wake_packet = kmemdup(notif->wake_packet, actual_size, 534 GFP_ATOMIC); 535 if (!wowlan_status->wake_packet) 536 return true; 537 538 wowlan_status->wake_packet_length = expected_size; 539 wowlan_status->wake_packet_bufsize = actual_size; 540 541 return false; 542 } 543 544 static void 545 iwl_mld_set_wake_packet(struct iwl_mld *mld, 546 struct ieee80211_vif *vif, 547 const struct iwl_mld_wowlan_status *wowlan_status, 548 struct cfg80211_wowlan_wakeup *wakeup, 549 struct sk_buff **_pkt) 550 { 551 int pkt_bufsize = wowlan_status->wake_packet_bufsize; 552 int expected_pktlen = wowlan_status->wake_packet_length; 553 const u8 *pktdata = wowlan_status->wake_packet; 554 const struct ieee80211_hdr *hdr = (const void *)pktdata; 555 int truncated = expected_pktlen - pkt_bufsize; 556 557 if (ieee80211_is_data(hdr->frame_control)) { 558 int hdrlen = ieee80211_hdrlen(hdr->frame_control); 559 int ivlen = 0, icvlen = 4; /* also FCS */ 560 561 struct sk_buff *pkt = alloc_skb(pkt_bufsize, GFP_KERNEL); 562 *_pkt = pkt; 563 if (!pkt) 564 return; 565 566 skb_put_data(pkt, pktdata, hdrlen); 567 pktdata += hdrlen; 568 pkt_bufsize -= hdrlen; 569 570 /* if truncated, FCS/ICV is (partially) gone */ 571 if (truncated >= icvlen) { 572 truncated -= icvlen; 573 icvlen = 0; 574 } else { 575 icvlen -= truncated; 576 truncated = 0; 577 } 578 579 pkt_bufsize -= ivlen + icvlen; 580 pktdata += ivlen; 581 582 skb_put_data(pkt, pktdata, pkt_bufsize); 583 584 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type)) 585 return; 586 wakeup->packet = pkt->data; 587 wakeup->packet_present_len = pkt->len; 588 wakeup->packet_len = pkt->len - truncated; 589 wakeup->packet_80211 = false; 590 } else { 591 int fcslen = 4; 592 593 if (truncated >= 4) { 594 truncated -= 4; 595 fcslen = 0; 596 } else { 597 fcslen -= truncated; 598 truncated = 0; 599 } 600 pkt_bufsize -= fcslen; 601 wakeup->packet = wowlan_status->wake_packet; 602 wakeup->packet_present_len = pkt_bufsize; 603 wakeup->packet_len = expected_pktlen - truncated; 604 wakeup->packet_80211 = true; 605 } 606 } 607 608 static void 609 iwl_mld_report_wowlan_wakeup(struct iwl_mld *mld, 610 struct ieee80211_vif *vif, 611 struct iwl_mld_wowlan_status *wowlan_status) 612 { 613 struct sk_buff *pkt = NULL; 614 struct cfg80211_wowlan_wakeup wakeup = { 615 .pattern_idx = -1, 616 }; 617 u32 reasons = wowlan_status->wakeup_reasons; 618 619 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) { 620 ieee80211_report_wowlan_wakeup(vif, NULL, GFP_KERNEL); 621 return; 622 } 623 624 pm_wakeup_event(mld->dev, 0); 625 626 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET) 627 wakeup.magic_pkt = true; 628 629 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN) 630 wakeup.pattern_idx = 631 wowlan_status->pattern_number; 632 633 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON | 634 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH | 635 IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)) 636 wakeup.disconnect = true; 637 638 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE) 639 wakeup.gtk_rekey_failure = true; 640 641 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED) 642 wakeup.rfkill_release = true; 643 644 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST) 645 wakeup.eap_identity_req = true; 646 647 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE) 648 wakeup.four_way_handshake = true; 649 650 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS) 651 wakeup.tcp_connlost = true; 652 653 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE) 654 wakeup.tcp_nomoretokens = true; 655 656 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET) 657 wakeup.tcp_match = true; 658 659 if (reasons & IWL_WAKEUP_BY_11W_UNPROTECTED_DEAUTH_OR_DISASSOC) 660 wakeup.unprot_deauth_disassoc = true; 661 662 if (wowlan_status->wake_packet) 663 iwl_mld_set_wake_packet(mld, vif, wowlan_status, &wakeup, &pkt); 664 665 ieee80211_report_wowlan_wakeup(vif, &wakeup, GFP_KERNEL); 666 kfree_skb(pkt); 667 } 668 669 static void 670 iwl_mld_set_key_rx_seq_tids(struct ieee80211_key_conf *key, 671 struct ieee80211_key_seq *seq) 672 { 673 int tid; 674 675 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) 676 ieee80211_set_key_rx_seq(key, tid, &seq[tid]); 677 } 678 679 static void 680 iwl_mld_update_mcast_rx_seq(struct ieee80211_key_conf *key, 681 struct iwl_mld_mcast_key_data *key_data) 682 { 683 switch (key->cipher) { 684 case WLAN_CIPHER_SUITE_CCMP: 685 case WLAN_CIPHER_SUITE_GCMP: 686 case WLAN_CIPHER_SUITE_GCMP_256: 687 iwl_mld_set_key_rx_seq_tids(key, 688 key_data->gtk.aes_seq); 689 break; 690 case WLAN_CIPHER_SUITE_TKIP: 691 iwl_mld_set_key_rx_seq_tids(key, 692 key_data->gtk.tkip_seq); 693 break; 694 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 695 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 696 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 697 case WLAN_CIPHER_SUITE_AES_CMAC: 698 /* igtk/bigtk ciphers*/ 699 ieee80211_set_key_rx_seq(key, 0, 700 &key_data->igtk_bigtk.cmac_gmac_seq); 701 break; 702 default: 703 WARN_ON(1); 704 } 705 } 706 707 static void 708 iwl_mld_update_ptk_rx_seq(struct iwl_mld *mld, 709 struct iwl_mld_wowlan_status *wowlan_status, 710 struct ieee80211_sta *sta, 711 struct ieee80211_key_conf *key, 712 bool is_tkip) 713 { 714 struct iwl_mld_sta *mld_sta = 715 iwl_mld_sta_from_mac80211(sta); 716 struct iwl_mld_ptk_pn *mld_ptk_pn = 717 wiphy_dereference(mld->wiphy, 718 mld_sta->ptk_pn[key->keyidx]); 719 720 iwl_mld_set_key_rx_seq_tids(key, is_tkip ? 721 wowlan_status->ptk.tkip_seq : 722 wowlan_status->ptk.aes_seq); 723 if (is_tkip) 724 return; 725 726 if (WARN_ON(!mld_ptk_pn)) 727 return; 728 729 for (int tid = 0; tid < IWL_MAX_TID_COUNT; tid++) { 730 for (int i = 1; i < mld->trans->info.num_rxqs; i++) 731 memcpy(mld_ptk_pn->q[i].pn[tid], 732 wowlan_status->ptk.aes_seq[tid].ccmp.pn, 733 IEEE80211_CCMP_PN_LEN); 734 } 735 } 736 737 static void 738 iwl_mld_resume_keys_iter(struct ieee80211_hw *hw, 739 struct ieee80211_vif *vif, 740 struct ieee80211_sta *sta, 741 struct ieee80211_key_conf *key, 742 void *_data) 743 { 744 struct iwl_mld_resume_key_iter_data *data = _data; 745 struct iwl_mld_wowlan_status *wowlan_status = data->wowlan_status; 746 u8 status_idx; 747 748 if (key->keyidx >= 0 && key->keyidx <= 3) { 749 /* PTK */ 750 if (sta) { 751 iwl_mld_update_ptk_rx_seq(data->mld, wowlan_status, 752 sta, key, 753 key->cipher == 754 WLAN_CIPHER_SUITE_TKIP); 755 /* GTK */ 756 } else { 757 status_idx = key->keyidx == wowlan_status->gtk[1].id; 758 iwl_mld_update_mcast_rx_seq(key, 759 &wowlan_status->gtk[status_idx]); 760 } 761 } 762 763 /* IGTK */ 764 if (key->keyidx == 4 || key->keyidx == 5) { 765 if (key->keyidx == wowlan_status->igtk.id) 766 iwl_mld_update_mcast_rx_seq(key, &wowlan_status->igtk); 767 } 768 769 /* BIGTK */ 770 if (key->keyidx == 6 || key->keyidx == 7) { 771 status_idx = key->keyidx == wowlan_status->bigtk[1].id; 772 iwl_mld_update_mcast_rx_seq(key, 773 &wowlan_status->bigtk[status_idx]); 774 } 775 } 776 777 static void 778 iwl_mld_add_mcast_rekey(struct ieee80211_vif *vif, 779 struct iwl_mld *mld, 780 struct iwl_mld_mcast_key_data *key_data, 781 struct ieee80211_bss_conf *link_conf) 782 { 783 struct ieee80211_key_conf *key_config; 784 int link_id = vif->active_links ? __ffs(vif->active_links) : -1; 785 786 if (!key_data->len) 787 return; 788 789 key_config = ieee80211_gtk_rekey_add(vif, key_data->id, key_data->key, 790 sizeof(key_data->key), link_id); 791 if (IS_ERR(key_config)) 792 return; 793 794 iwl_mld_update_mcast_rx_seq(key_config, key_data); 795 796 /* The FW holds only one igtk so we keep track of the valid one */ 797 if (key_config->keyidx == 4 || key_config->keyidx == 5) { 798 struct iwl_mld_link *mld_link = 799 iwl_mld_link_from_mac80211(link_conf); 800 801 /* If we had more than one rekey, mac80211 will tell us to 802 * remove the old and add the new so we will update the IGTK in 803 * drv_set_key 804 */ 805 if (mld_link->igtk && mld_link->igtk != key_config) { 806 /* mark the old IGTK as not in FW */ 807 mld_link->igtk->hw_key_idx = STA_KEY_IDX_INVALID; 808 mld_link->igtk = key_config; 809 } 810 } 811 812 /* Also keep track of the new BIGTK */ 813 if (key_config->keyidx == 6 || key_config->keyidx == 7) 814 iwl_mld_track_bigtk(mld, vif, key_config, true); 815 } 816 817 static void 818 iwl_mld_add_all_rekeys(struct iwl_mld *mld, 819 struct ieee80211_vif *vif, 820 struct iwl_mld_wowlan_status *wowlan_status, 821 struct ieee80211_bss_conf *link_conf) 822 { 823 int i; 824 825 for (i = 0; i < ARRAY_SIZE(wowlan_status->gtk); i++) 826 iwl_mld_add_mcast_rekey(vif, mld, &wowlan_status->gtk[i], 827 link_conf); 828 829 iwl_mld_add_mcast_rekey(vif, mld, &wowlan_status->igtk, link_conf); 830 831 for (i = 0; i < ARRAY_SIZE(wowlan_status->bigtk); i++) 832 iwl_mld_add_mcast_rekey(vif, mld, &wowlan_status->bigtk[i], 833 link_conf); 834 } 835 836 static void iwl_mld_mlo_rekey(struct iwl_mld *mld, 837 struct iwl_mld_wowlan_status *wowlan_status, 838 struct ieee80211_vif *vif) 839 { 840 struct iwl_mld_old_mlo_keys *old_keys __free(kfree) = NULL; 841 842 IWL_DEBUG_WOWLAN(mld, "Num of MLO Keys: %d\n", wowlan_status->num_mlo_keys); 843 844 if (!wowlan_status->num_mlo_keys) 845 return; 846 847 for (int i = 0; i < wowlan_status->num_mlo_keys; i++) { 848 struct iwl_mld_wowlan_mlo_key *mlo_key = &wowlan_status->mlo_keys[i]; 849 struct ieee80211_key_conf *key; 850 struct ieee80211_key_seq seq; 851 u8 link_id = mlo_key->link_id; 852 853 if (IWL_FW_CHECK(mld, mlo_key->link_id >= IEEE80211_MLD_MAX_NUM_LINKS || 854 mlo_key->idx >= 8 || 855 mlo_key->type >= WOWLAN_MLO_GTK_KEY_NUM_TYPES, 856 "Invalid MLO key link_id %d, idx %d, type %d\n", 857 mlo_key->link_id, mlo_key->idx, mlo_key->type)) 858 continue; 859 860 if (!(vif->valid_links & BIT(link_id)) || 861 (vif->active_links & BIT(link_id))) 862 continue; 863 864 IWL_DEBUG_WOWLAN(mld, "Add MLO key id %d, link id %d\n", 865 mlo_key->idx, link_id); 866 867 key = ieee80211_gtk_rekey_add(vif, mlo_key->idx, mlo_key->key, 868 sizeof(mlo_key->key), link_id); 869 870 if (IS_ERR(key)) 871 continue; 872 873 /* 874 * mac80211 expects the PN in big-endian 875 * also note that seq is a union of all cipher types 876 * (ccmp, gcmp, cmac, gmac), and they all have the same 877 * pn field (of length 6) so just copy it to ccmp.pn. 878 */ 879 for (int j = 5; j >= 0; j--) 880 seq.ccmp.pn[5 - j] = mlo_key->pn[j]; 881 882 /* group keys are non-QoS and use TID 0 */ 883 ieee80211_set_key_rx_seq(key, 0, &seq); 884 } 885 } 886 887 static bool 888 iwl_mld_update_sec_keys(struct iwl_mld *mld, 889 struct ieee80211_vif *vif, 890 struct iwl_mld_wowlan_status *wowlan_status) 891 { 892 int link_id = vif->active_links ? __ffs(vif->active_links) : 0; 893 struct ieee80211_bss_conf *link_conf = 894 link_conf_dereference_protected(vif, link_id); 895 __be64 replay_ctr = cpu_to_be64(wowlan_status->replay_ctr); 896 struct iwl_mld_resume_key_iter_data key_iter_data = { 897 .mld = mld, 898 .wowlan_status = wowlan_status, 899 }; 900 901 if (WARN_ON(!link_conf)) 902 return false; 903 904 ieee80211_iter_keys(mld->hw, vif, iwl_mld_resume_keys_iter, 905 &key_iter_data); 906 907 IWL_DEBUG_WOWLAN(mld, "Number of rekeys: %d\n", 908 wowlan_status->num_of_gtk_rekeys); 909 910 if (!wowlan_status->num_of_gtk_rekeys) 911 return true; 912 913 iwl_mld_add_all_rekeys(mld, vif, wowlan_status, 914 link_conf); 915 916 iwl_mld_mlo_rekey(mld, wowlan_status, vif); 917 918 ieee80211_gtk_rekey_notify(vif, link_conf->bssid, 919 (void *)&replay_ctr, GFP_KERNEL); 920 return true; 921 } 922 923 static bool 924 iwl_mld_process_wowlan_status(struct iwl_mld *mld, 925 struct ieee80211_vif *vif, 926 struct iwl_mld_wowlan_status *wowlan_status) 927 { 928 struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif); 929 struct ieee80211_sta *ap_sta = mld_vif->ap_sta; 930 struct iwl_mld_txq *mld_txq; 931 932 iwl_mld_report_wowlan_wakeup(mld, vif, wowlan_status); 933 934 if (WARN_ON(!ap_sta)) 935 return false; 936 937 mld_txq = 938 iwl_mld_txq_from_mac80211(ap_sta->txq[wowlan_status->tid_offloaded_tx]); 939 940 /* Update the pointers of the Tx queue that may have moved during 941 * suspend if the firmware sent frames. 942 * The firmware stores last-used value, we store next value. 943 */ 944 WARN_ON(!mld_txq->status.allocated); 945 iwl_trans_set_q_ptrs(mld->trans, mld_txq->fw_id, 946 (wowlan_status->last_qos_seq + 947 0x10) >> 4); 948 949 if (!iwl_mld_update_sec_keys(mld, vif, wowlan_status)) 950 return false; 951 952 if (wowlan_status->wakeup_reasons & 953 (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON | 954 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH | 955 IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)) 956 return false; 957 958 return true; 959 } 960 961 static bool 962 iwl_mld_netdetect_match_info_handler(struct iwl_mld *mld, 963 struct iwl_mld_resume_data *resume_data, 964 struct iwl_rx_packet *pkt) 965 { 966 struct iwl_mld_netdetect_res *results = resume_data->netdetect_res; 967 const struct iwl_scan_offload_match_info *notif = (void *)pkt->data; 968 u32 len = iwl_rx_packet_payload_len(pkt); 969 970 if (IWL_FW_CHECK(mld, !mld->netdetect, 971 "Got scan match info notif when mld->netdetect==%d\n", 972 mld->netdetect)) 973 return true; 974 975 if (IWL_FW_CHECK(mld, len < sizeof(*notif), 976 "Invalid scan offload match notif of length: %d\n", 977 len)) 978 return true; 979 980 if (IWL_FW_CHECK(mld, resume_data->wowlan_status->wakeup_reasons != 981 IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS, 982 "Ignore scan match info: unexpected wakeup reason (expected=0x%x got=0x%x)\n", 983 IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS, 984 resume_data->wowlan_status->wakeup_reasons)) 985 return true; 986 987 results->matched_profiles = le32_to_cpu(notif->matched_profiles); 988 IWL_DEBUG_WOWLAN(mld, "number of matched profiles=%u\n", 989 results->matched_profiles); 990 991 if (results->matched_profiles) 992 memcpy(results->matches, notif->matches, 993 NETDETECT_QUERY_BUF_LEN); 994 995 /* No scan should be active at this point */ 996 mld->scan.status = 0; 997 memset(mld->scan.uid_status, 0, sizeof(mld->scan.uid_status)); 998 return false; 999 } 1000 1001 static void 1002 iwl_mld_set_netdetect_info(struct iwl_mld *mld, 1003 const struct cfg80211_sched_scan_request *netdetect_cfg, 1004 struct cfg80211_wowlan_nd_info *netdetect_info, 1005 struct iwl_mld_netdetect_res *netdetect_res, 1006 unsigned long matched_profiles) 1007 { 1008 int i; 1009 1010 for_each_set_bit(i, &matched_profiles, netdetect_cfg->n_match_sets) { 1011 struct cfg80211_wowlan_nd_match *match; 1012 int idx, j, n_channels = 0; 1013 struct iwl_scan_offload_profile_match *matches = 1014 (void *)netdetect_res->matches; 1015 1016 for (int k = 0; k < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; k++) 1017 n_channels += 1018 hweight8(matches[i].matching_channels[k]); 1019 match = kzalloc(struct_size(match, channels, n_channels), 1020 GFP_KERNEL); 1021 if (!match) 1022 return; 1023 1024 netdetect_info->matches[netdetect_info->n_matches] = match; 1025 netdetect_info->n_matches++; 1026 1027 /* We inverted the order of the SSIDs in the scan 1028 * request, so invert the index here. 1029 */ 1030 idx = netdetect_cfg->n_match_sets - i - 1; 1031 match->ssid.ssid_len = 1032 netdetect_cfg->match_sets[idx].ssid.ssid_len; 1033 memcpy(match->ssid.ssid, 1034 netdetect_cfg->match_sets[idx].ssid.ssid, 1035 match->ssid.ssid_len); 1036 1037 if (netdetect_cfg->n_channels < n_channels) 1038 continue; 1039 1040 for_each_set_bit(j, 1041 (unsigned long *)&matches[i].matching_channels[0], 1042 sizeof(matches[i].matching_channels)) { 1043 match->channels[match->n_channels] = 1044 netdetect_cfg->channels[j]->center_freq; 1045 match->n_channels++; 1046 } 1047 } 1048 } 1049 1050 static void 1051 iwl_mld_process_netdetect_res(struct iwl_mld *mld, 1052 struct ieee80211_vif *vif, 1053 struct iwl_mld_resume_data *resume_data) 1054 { 1055 struct cfg80211_wowlan_nd_info *netdetect_info = NULL; 1056 const struct cfg80211_sched_scan_request *netdetect_cfg; 1057 struct cfg80211_wowlan_wakeup wakeup = { 1058 .pattern_idx = -1, 1059 }; 1060 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup; 1061 unsigned long matched_profiles; 1062 u32 wakeup_reasons; 1063 int n_matches; 1064 1065 lockdep_assert_wiphy(mld->wiphy); 1066 1067 if (WARN_ON(!mld->wiphy->wowlan_config || 1068 !mld->wiphy->wowlan_config->nd_config)) { 1069 IWL_DEBUG_WOWLAN(mld, 1070 "Netdetect isn't configured on resume flow\n"); 1071 goto out; 1072 } 1073 1074 netdetect_cfg = mld->wiphy->wowlan_config->nd_config; 1075 wakeup_reasons = resume_data->wowlan_status->wakeup_reasons; 1076 1077 if (wakeup_reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED) 1078 wakeup.rfkill_release = true; 1079 1080 if (wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) 1081 goto out; 1082 1083 if (!resume_data->netdetect_res->matched_profiles) { 1084 IWL_DEBUG_WOWLAN(mld, 1085 "Netdetect results aren't valid\n"); 1086 wakeup_report = NULL; 1087 goto out; 1088 } 1089 1090 matched_profiles = resume_data->netdetect_res->matched_profiles; 1091 if (!netdetect_cfg->n_match_sets) { 1092 IWL_DEBUG_WOWLAN(mld, 1093 "No netdetect match sets are configured\n"); 1094 goto out; 1095 } 1096 n_matches = hweight_long(matched_profiles); 1097 netdetect_info = kzalloc(struct_size(netdetect_info, matches, 1098 n_matches), GFP_KERNEL); 1099 if (netdetect_info) 1100 iwl_mld_set_netdetect_info(mld, netdetect_cfg, netdetect_info, 1101 resume_data->netdetect_res, 1102 matched_profiles); 1103 1104 wakeup.net_detect = netdetect_info; 1105 out: 1106 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL); 1107 if (netdetect_info) { 1108 for (int i = 0; i < netdetect_info->n_matches; i++) 1109 kfree(netdetect_info->matches[i]); 1110 kfree(netdetect_info); 1111 } 1112 } 1113 1114 static bool iwl_mld_handle_d3_notif(struct iwl_notif_wait_data *notif_wait, 1115 struct iwl_rx_packet *pkt, void *data) 1116 { 1117 struct iwl_mld_resume_data *resume_data = data; 1118 struct iwl_mld *mld = 1119 container_of(notif_wait, struct iwl_mld, notif_wait); 1120 1121 switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) { 1122 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): { 1123 if (resume_data->notifs_received & IWL_D3_NOTIF_WOWLAN_INFO) { 1124 IWL_DEBUG_WOWLAN(mld, 1125 "got additional wowlan_info notif\n"); 1126 break; 1127 } 1128 resume_data->notif_handling_err = 1129 iwl_mld_handle_wowlan_info_notif(mld, 1130 resume_data->wowlan_status, 1131 pkt); 1132 resume_data->notifs_received |= IWL_D3_NOTIF_WOWLAN_INFO; 1133 1134 if (resume_data->wowlan_status->wakeup_reasons & 1135 IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT) 1136 resume_data->notifs_expected |= 1137 IWL_D3_NOTIF_WOWLAN_WAKE_PKT; 1138 break; 1139 } 1140 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): { 1141 if (resume_data->notifs_received & 1142 IWL_D3_NOTIF_WOWLAN_WAKE_PKT) { 1143 /* We shouldn't get two wake packet notifications */ 1144 IWL_DEBUG_WOWLAN(mld, 1145 "Got additional wowlan wake packet notification\n"); 1146 break; 1147 } 1148 resume_data->notif_handling_err = 1149 iwl_mld_handle_wake_pkt_notif(mld, 1150 resume_data->wowlan_status, 1151 pkt); 1152 resume_data->notifs_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT; 1153 break; 1154 } 1155 case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): { 1156 if (resume_data->notifs_received & IWL_D3_ND_MATCH_INFO) { 1157 IWL_ERR(mld, 1158 "Got additional netdetect match info\n"); 1159 break; 1160 } 1161 1162 resume_data->notif_handling_err = 1163 iwl_mld_netdetect_match_info_handler(mld, resume_data, 1164 pkt); 1165 resume_data->notifs_received |= IWL_D3_ND_MATCH_INFO; 1166 break; 1167 } 1168 case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): { 1169 struct iwl_d3_end_notif *notif = (void *)pkt->data; 1170 1171 resume_data->d3_end_flags = le32_to_cpu(notif->flags); 1172 resume_data->notifs_received |= IWL_D3_NOTIF_D3_END_NOTIF; 1173 break; 1174 } 1175 default: 1176 WARN_ON(1); 1177 } 1178 1179 return resume_data->notifs_received == resume_data->notifs_expected; 1180 } 1181 1182 #define IWL_MLD_D3_NOTIF_TIMEOUT (HZ / 3) 1183 1184 static int iwl_mld_wait_d3_notif(struct iwl_mld *mld, 1185 struct iwl_mld_resume_data *resume_data, 1186 bool with_wowlan) 1187 { 1188 static const u16 wowlan_resume_notif[] = { 1189 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION), 1190 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION), 1191 WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF), 1192 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION) 1193 }; 1194 static const u16 d3_resume_notif[] = { 1195 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION) 1196 }; 1197 struct iwl_notification_wait wait_d3_notif; 1198 int ret; 1199 1200 if (with_wowlan) 1201 iwl_init_notification_wait(&mld->notif_wait, &wait_d3_notif, 1202 wowlan_resume_notif, 1203 ARRAY_SIZE(wowlan_resume_notif), 1204 iwl_mld_handle_d3_notif, 1205 resume_data); 1206 else 1207 iwl_init_notification_wait(&mld->notif_wait, &wait_d3_notif, 1208 d3_resume_notif, 1209 ARRAY_SIZE(d3_resume_notif), 1210 iwl_mld_handle_d3_notif, 1211 resume_data); 1212 1213 ret = iwl_trans_d3_resume(mld->trans, false); 1214 if (ret) { 1215 /* Avoid sending commands if the FW is dead */ 1216 iwl_trans_notify_fw_error(mld->trans); 1217 iwl_remove_notification(&mld->notif_wait, &wait_d3_notif); 1218 return ret; 1219 } 1220 1221 ret = iwl_wait_notification(&mld->notif_wait, &wait_d3_notif, 1222 IWL_MLD_D3_NOTIF_TIMEOUT); 1223 if (ret) 1224 IWL_ERR(mld, "Couldn't get the d3 notif %d\n", ret); 1225 1226 if (resume_data->notif_handling_err) 1227 ret = -EIO; 1228 1229 return ret; 1230 } 1231 1232 int iwl_mld_no_wowlan_suspend(struct iwl_mld *mld) 1233 { 1234 struct iwl_d3_manager_config d3_cfg_cmd_data = {}; 1235 int ret; 1236 1237 if (mld->debug_max_sleep) { 1238 d3_cfg_cmd_data.wakeup_host_timer = 1239 cpu_to_le32(mld->debug_max_sleep); 1240 d3_cfg_cmd_data.wakeup_flags = 1241 cpu_to_le32(IWL_WAKEUP_D3_HOST_TIMER); 1242 } 1243 1244 lockdep_assert_wiphy(mld->wiphy); 1245 1246 IWL_DEBUG_WOWLAN(mld, "Starting the no wowlan suspend flow\n"); 1247 1248 iwl_mld_low_latency_stop(mld); 1249 1250 ret = iwl_mld_update_device_power(mld, true); 1251 if (ret) { 1252 IWL_ERR(mld, 1253 "d3 suspend: couldn't send power_device %d\n", ret); 1254 return ret; 1255 } 1256 1257 ret = iwl_mld_send_cmd_pdu(mld, D3_CONFIG_CMD, 1258 &d3_cfg_cmd_data); 1259 if (ret) { 1260 IWL_ERR(mld, 1261 "d3 suspend: couldn't send D3_CONFIG_CMD %d\n", ret); 1262 return ret; 1263 } 1264 1265 ret = iwl_trans_d3_suspend(mld->trans, false); 1266 if (ret) { 1267 IWL_ERR(mld, "d3 suspend: trans_d3_suspend failed %d\n", ret); 1268 /* We are going to stop the FW. Avoid sending commands in that flow */ 1269 iwl_trans_notify_fw_error(mld->trans); 1270 } else { 1271 /* Async notification might send hcmds, which is not allowed in suspend */ 1272 iwl_mld_cancel_async_notifications(mld); 1273 mld->fw_status.in_d3 = true; 1274 } 1275 1276 return ret; 1277 } 1278 1279 int iwl_mld_no_wowlan_resume(struct iwl_mld *mld) 1280 { 1281 struct iwl_mld_resume_data resume_data = { 1282 .notifs_expected = 1283 IWL_D3_NOTIF_D3_END_NOTIF, 1284 }; 1285 int ret; 1286 1287 lockdep_assert_wiphy(mld->wiphy); 1288 1289 IWL_DEBUG_WOWLAN(mld, "Starting the no wowlan resume flow\n"); 1290 1291 mld->fw_status.in_d3 = false; 1292 iwl_fw_dbg_read_d3_debug_data(&mld->fwrt); 1293 1294 ret = iwl_mld_wait_d3_notif(mld, &resume_data, false); 1295 if (ret) 1296 return ret; 1297 1298 if (!ret && (resume_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) 1299 return -ENODEV; 1300 1301 iwl_mld_low_latency_restart(mld); 1302 1303 return iwl_mld_update_device_power(mld, false); 1304 } 1305 1306 static void 1307 iwl_mld_aes_seq_to_le64_pn(struct ieee80211_key_conf *key, 1308 __le64 *key_rsc) 1309 { 1310 for (int i = 0; i < IWL_MAX_TID_COUNT; i++) { 1311 struct ieee80211_key_seq seq; 1312 u8 *pn = key->cipher == WLAN_CIPHER_SUITE_CCMP ? seq.ccmp.pn : 1313 seq.gcmp.pn; 1314 1315 ieee80211_get_key_rx_seq(key, i, &seq); 1316 key_rsc[i] = cpu_to_le64((u64)pn[5] | 1317 ((u64)pn[4] << 8) | 1318 ((u64)pn[3] << 16) | 1319 ((u64)pn[2] << 24) | 1320 ((u64)pn[1] << 32) | 1321 ((u64)pn[0] << 40)); 1322 } 1323 } 1324 1325 static void 1326 iwl_mld_suspend_set_ucast_pn(struct iwl_mld *mld, struct ieee80211_sta *sta, 1327 struct ieee80211_key_conf *key, __le64 *key_rsc) 1328 { 1329 struct iwl_mld_sta *mld_sta = 1330 iwl_mld_sta_from_mac80211(sta); 1331 struct iwl_mld_ptk_pn *mld_ptk_pn; 1332 1333 if (WARN_ON(key->keyidx >= ARRAY_SIZE(mld_sta->ptk_pn))) 1334 return; 1335 1336 mld_ptk_pn = wiphy_dereference(mld->wiphy, 1337 mld_sta->ptk_pn[key->keyidx]); 1338 if (WARN_ON(!mld_ptk_pn)) 1339 return; 1340 1341 for (int tid = 0; tid < IWL_MAX_TID_COUNT; tid++) { 1342 struct ieee80211_key_seq seq; 1343 u8 *max_pn = seq.ccmp.pn; 1344 1345 /* get the PN from mac80211, used on the default queue */ 1346 ieee80211_get_key_rx_seq(key, tid, &seq); 1347 1348 /* and use the internal data for all queues */ 1349 for (int que = 1; que < mld->trans->info.num_rxqs; que++) { 1350 u8 *cur_pn = mld_ptk_pn->q[que].pn[tid]; 1351 1352 if (memcmp(max_pn, cur_pn, IEEE80211_CCMP_PN_LEN) < 0) 1353 max_pn = cur_pn; 1354 } 1355 key_rsc[tid] = cpu_to_le64((u64)max_pn[5] | 1356 ((u64)max_pn[4] << 8) | 1357 ((u64)max_pn[3] << 16) | 1358 ((u64)max_pn[2] << 24) | 1359 ((u64)max_pn[1] << 32) | 1360 ((u64)max_pn[0] << 40)); 1361 } 1362 } 1363 1364 static void 1365 iwl_mld_suspend_convert_tkip_ipn(struct ieee80211_key_conf *key, 1366 __le64 *rsc) 1367 { 1368 struct ieee80211_key_seq seq; 1369 1370 for (int i = 0; i < IWL_MAX_TID_COUNT; i++) { 1371 ieee80211_get_key_rx_seq(key, i, &seq); 1372 rsc[i] = 1373 cpu_to_le64(((u64)seq.tkip.iv32 << 16) | 1374 seq.tkip.iv16); 1375 } 1376 } 1377 1378 static void 1379 iwl_mld_suspend_key_data_iter(struct ieee80211_hw *hw, 1380 struct ieee80211_vif *vif, 1381 struct ieee80211_sta *sta, 1382 struct ieee80211_key_conf *key, 1383 void *_data) 1384 { 1385 struct iwl_mld *mld = IWL_MAC80211_GET_MLD(hw); 1386 struct iwl_mld_suspend_key_iter_data *data = _data; 1387 __le64 *key_rsc; 1388 __le32 cipher = 0; 1389 1390 switch (key->cipher) { 1391 case WLAN_CIPHER_SUITE_CCMP: 1392 cipher = cpu_to_le32(STA_KEY_FLG_CCM); 1393 fallthrough; 1394 case WLAN_CIPHER_SUITE_GCMP: 1395 case WLAN_CIPHER_SUITE_GCMP_256: 1396 if (!cipher) 1397 cipher = cpu_to_le32(STA_KEY_FLG_GCMP); 1398 fallthrough; 1399 case WLAN_CIPHER_SUITE_TKIP: 1400 if (!cipher) 1401 cipher = cpu_to_le32(STA_KEY_FLG_TKIP); 1402 if (sta) { 1403 key_rsc = data->rsc->ucast_rsc; 1404 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) 1405 iwl_mld_suspend_convert_tkip_ipn(key, key_rsc); 1406 else 1407 iwl_mld_suspend_set_ucast_pn(mld, sta, key, 1408 key_rsc); 1409 1410 data->have_rsc = true; 1411 return; 1412 } 1413 /* We're iterating from old to new, there're 4 possible 1414 * gtk ids, and only the last two keys matter 1415 */ 1416 if (WARN_ON(data->gtks >= 1417 ARRAY_SIZE(data->found_gtk_idx))) 1418 return; 1419 1420 if (WARN_ON(key->keyidx >= 1421 ARRAY_SIZE(data->rsc->mcast_key_id_map))) 1422 return; 1423 data->gtk_cipher = cipher; 1424 data->found_gtk_idx[data->gtks] = key->keyidx; 1425 key_rsc = data->rsc->mcast_rsc[data->gtks % 2]; 1426 data->rsc->mcast_key_id_map[key->keyidx] = 1427 data->gtks % 2; 1428 1429 if (data->gtks >= 2) { 1430 int prev = data->gtks % 2; 1431 int prev_idx = data->found_gtk_idx[prev]; 1432 1433 data->rsc->mcast_key_id_map[prev_idx] = 1434 IWL_MCAST_KEY_MAP_INVALID; 1435 } 1436 1437 if (key->cipher == WLAN_CIPHER_SUITE_TKIP) 1438 iwl_mld_suspend_convert_tkip_ipn(key, key_rsc); 1439 else 1440 iwl_mld_aes_seq_to_le64_pn(key, key_rsc); 1441 1442 data->gtks++; 1443 data->have_rsc = true; 1444 break; 1445 case WLAN_CIPHER_SUITE_BIP_GMAC_128: 1446 case WLAN_CIPHER_SUITE_BIP_GMAC_256: 1447 cipher = cpu_to_le32(STA_KEY_FLG_GCMP); 1448 fallthrough; 1449 case WLAN_CIPHER_SUITE_BIP_CMAC_256: 1450 case WLAN_CIPHER_SUITE_AES_CMAC: 1451 if (!cipher) 1452 cipher = cpu_to_le32(STA_KEY_FLG_CCM); 1453 if (key->keyidx == 4 || key->keyidx == 5) 1454 data->igtk_cipher = cipher; 1455 1456 if (key->keyidx == 6 || key->keyidx == 7) 1457 data->bigtk_cipher = cipher; 1458 1459 break; 1460 } 1461 } 1462 1463 static int 1464 iwl_mld_send_kek_kck_cmd(struct iwl_mld *mld, 1465 struct iwl_mld_vif *mld_vif, 1466 struct iwl_mld_suspend_key_iter_data data, 1467 int ap_sta_id) 1468 { 1469 struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {}; 1470 struct iwl_mld_rekey_data *rekey_data = 1471 &mld_vif->wowlan_data.rekey_data; 1472 1473 memcpy(kek_kck_cmd.kck, rekey_data->kck, 1474 rekey_data->kck_len); 1475 kek_kck_cmd.kck_len = cpu_to_le16(rekey_data->kck_len); 1476 memcpy(kek_kck_cmd.kek, rekey_data->kek, 1477 rekey_data->kek_len); 1478 kek_kck_cmd.kek_len = cpu_to_le16(rekey_data->kek_len); 1479 kek_kck_cmd.replay_ctr = rekey_data->replay_ctr; 1480 kek_kck_cmd.akm = cpu_to_le32(rekey_data->akm); 1481 kek_kck_cmd.sta_id = cpu_to_le32(ap_sta_id); 1482 kek_kck_cmd.gtk_cipher = data.gtk_cipher; 1483 kek_kck_cmd.igtk_cipher = data.igtk_cipher; 1484 kek_kck_cmd.bigtk_cipher = data.bigtk_cipher; 1485 1486 IWL_DEBUG_WOWLAN(mld, "setting akm %d\n", 1487 rekey_data->akm); 1488 1489 return iwl_mld_send_cmd_pdu(mld, WOWLAN_KEK_KCK_MATERIAL, 1490 &kek_kck_cmd); 1491 } 1492 1493 static int 1494 iwl_mld_suspend_send_security_cmds(struct iwl_mld *mld, 1495 struct ieee80211_vif *vif, 1496 struct iwl_mld_vif *mld_vif, 1497 int ap_sta_id) 1498 { 1499 struct iwl_mld_suspend_key_iter_data data = {}; 1500 int ret; 1501 1502 data.rsc = kzalloc(sizeof(*data.rsc), GFP_KERNEL); 1503 if (!data.rsc) 1504 return -ENOMEM; 1505 1506 memset(data.rsc->mcast_key_id_map, IWL_MCAST_KEY_MAP_INVALID, 1507 ARRAY_SIZE(data.rsc->mcast_key_id_map)); 1508 1509 data.rsc->sta_id = cpu_to_le32(ap_sta_id); 1510 ieee80211_iter_keys(mld->hw, vif, 1511 iwl_mld_suspend_key_data_iter, 1512 &data); 1513 1514 if (data.have_rsc) 1515 ret = iwl_mld_send_cmd_pdu(mld, WOWLAN_TSC_RSC_PARAM, 1516 data.rsc); 1517 else 1518 ret = 0; 1519 1520 if (!ret && mld_vif->wowlan_data.rekey_data.valid) 1521 ret = iwl_mld_send_kek_kck_cmd(mld, mld_vif, data, ap_sta_id); 1522 1523 kfree(data.rsc); 1524 1525 return ret; 1526 } 1527 1528 static void 1529 iwl_mld_set_wowlan_config_cmd(struct iwl_mld *mld, 1530 struct cfg80211_wowlan *wowlan, 1531 struct iwl_wowlan_config_cmd *wowlan_config_cmd, 1532 struct ieee80211_sta *ap_sta, 1533 struct ieee80211_bss_conf *link) 1534 { 1535 wowlan_config_cmd->is_11n_connection = 1536 ap_sta->deflink.ht_cap.ht_supported; 1537 wowlan_config_cmd->flags = ENABLE_L3_FILTERING | 1538 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING; 1539 1540 if (ap_sta->mfp) 1541 wowlan_config_cmd->flags |= IS_11W_ASSOC; 1542 1543 if (iwl_mld_beacon_protection_enabled(mld, link)) 1544 wowlan_config_cmd->flags |= HAS_BEACON_PROTECTION; 1545 1546 if (wowlan->disconnect) 1547 wowlan_config_cmd->wakeup_filter |= 1548 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS | 1549 IWL_WOWLAN_WAKEUP_LINK_CHANGE); 1550 if (wowlan->magic_pkt) 1551 wowlan_config_cmd->wakeup_filter |= 1552 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET); 1553 if (wowlan->gtk_rekey_failure) 1554 wowlan_config_cmd->wakeup_filter |= 1555 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL); 1556 if (wowlan->eap_identity_req) 1557 wowlan_config_cmd->wakeup_filter |= 1558 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ); 1559 if (wowlan->four_way_handshake) 1560 wowlan_config_cmd->wakeup_filter |= 1561 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE); 1562 if (wowlan->n_patterns) 1563 wowlan_config_cmd->wakeup_filter |= 1564 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH); 1565 1566 if (wowlan->rfkill_release) 1567 wowlan_config_cmd->wakeup_filter |= 1568 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT); 1569 1570 if (wowlan->any) { 1571 wowlan_config_cmd->wakeup_filter |= 1572 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS | 1573 IWL_WOWLAN_WAKEUP_LINK_CHANGE | 1574 IWL_WOWLAN_WAKEUP_RX_FRAME | 1575 IWL_WOWLAN_WAKEUP_BCN_FILTERING); 1576 } 1577 } 1578 1579 static int iwl_mld_send_patterns(struct iwl_mld *mld, 1580 struct cfg80211_wowlan *wowlan, 1581 int ap_sta_id) 1582 { 1583 struct iwl_wowlan_patterns_cmd *pattern_cmd; 1584 struct iwl_host_cmd cmd = { 1585 .id = WOWLAN_PATTERNS, 1586 .dataflags[0] = IWL_HCMD_DFL_NOCOPY, 1587 }; 1588 int ret; 1589 1590 if (!wowlan->n_patterns) 1591 return 0; 1592 1593 cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns); 1594 1595 pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL); 1596 if (!pattern_cmd) 1597 return -ENOMEM; 1598 1599 pattern_cmd->n_patterns = wowlan->n_patterns; 1600 pattern_cmd->sta_id = ap_sta_id; 1601 1602 for (int i = 0; i < wowlan->n_patterns; i++) { 1603 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8); 1604 1605 pattern_cmd->patterns[i].pattern_type = 1606 WOWLAN_PATTERN_TYPE_BITMASK; 1607 1608 memcpy(&pattern_cmd->patterns[i].u.bitmask.mask, 1609 wowlan->patterns[i].mask, mask_len); 1610 memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern, 1611 wowlan->patterns[i].pattern, 1612 wowlan->patterns[i].pattern_len); 1613 pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len; 1614 pattern_cmd->patterns[i].u.bitmask.pattern_size = 1615 wowlan->patterns[i].pattern_len; 1616 } 1617 1618 cmd.data[0] = pattern_cmd; 1619 ret = iwl_mld_send_cmd(mld, &cmd); 1620 kfree(pattern_cmd); 1621 return ret; 1622 } 1623 1624 static int 1625 iwl_mld_send_proto_offload(struct iwl_mld *mld, 1626 struct ieee80211_vif *vif, 1627 u8 ap_sta_id) 1628 { 1629 struct iwl_proto_offload_cmd_v4 *cmd __free(kfree); 1630 struct iwl_host_cmd hcmd = { 1631 .id = PROT_OFFLOAD_CONFIG_CMD, 1632 .dataflags[0] = IWL_HCMD_DFL_NOCOPY, 1633 .len[0] = sizeof(*cmd), 1634 }; 1635 u32 enabled = 0; 1636 1637 cmd = kzalloc(hcmd.len[0], GFP_KERNEL); 1638 1639 #if IS_ENABLED(CONFIG_IPV6) 1640 struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif); 1641 struct iwl_mld_wowlan_data *wowlan_data = &mld_vif->wowlan_data; 1642 struct iwl_ns_config *nsc; 1643 struct iwl_targ_addr *addrs; 1644 int n_nsc, n_addrs; 1645 int i, c; 1646 int num_skipped = 0; 1647 1648 nsc = cmd->ns_config; 1649 n_nsc = IWL_PROTO_OFFLOAD_NUM_NS_CONFIG_V3L; 1650 addrs = cmd->targ_addrs; 1651 n_addrs = IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V3L; 1652 1653 /* For each address we have (and that will fit) fill a target 1654 * address struct and combine for NS offload structs with the 1655 * solicited node addresses. 1656 */ 1657 for (i = 0, c = 0; 1658 i < wowlan_data->num_target_ipv6_addrs && 1659 i < n_addrs && c < n_nsc; i++) { 1660 int j; 1661 struct in6_addr solicited_addr; 1662 1663 /* Because ns is offloaded skip tentative address to avoid 1664 * violating RFC4862. 1665 */ 1666 if (test_bit(i, wowlan_data->tentative_addrs)) { 1667 num_skipped++; 1668 continue; 1669 } 1670 1671 addrconf_addr_solict_mult(&wowlan_data->target_ipv6_addrs[i], 1672 &solicited_addr); 1673 for (j = 0; j < n_nsc && j < c; j++) 1674 if (ipv6_addr_cmp(&nsc[j].dest_ipv6_addr, 1675 &solicited_addr) == 0) 1676 break; 1677 if (j == c) 1678 c++; 1679 addrs[i].addr = wowlan_data->target_ipv6_addrs[i]; 1680 addrs[i].config_num = cpu_to_le32(j); 1681 nsc[j].dest_ipv6_addr = solicited_addr; 1682 memcpy(nsc[j].target_mac_addr, vif->addr, ETH_ALEN); 1683 } 1684 1685 if (wowlan_data->num_target_ipv6_addrs - num_skipped) 1686 enabled |= IWL_D3_PROTO_IPV6_VALID; 1687 1688 cmd->num_valid_ipv6_addrs = cpu_to_le32(i - num_skipped); 1689 if (enabled & IWL_D3_PROTO_IPV6_VALID) 1690 enabled |= IWL_D3_PROTO_OFFLOAD_NS; 1691 #endif 1692 1693 if (vif->cfg.arp_addr_cnt) { 1694 enabled |= IWL_D3_PROTO_OFFLOAD_ARP | IWL_D3_PROTO_IPV4_VALID; 1695 cmd->common.host_ipv4_addr = vif->cfg.arp_addr_list[0]; 1696 ether_addr_copy(cmd->common.arp_mac_addr, vif->addr); 1697 } 1698 1699 enabled |= IWL_D3_PROTO_OFFLOAD_BTM; 1700 cmd->common.enabled = cpu_to_le32(enabled); 1701 cmd->sta_id = cpu_to_le32(ap_sta_id); 1702 hcmd.data[0] = cmd; 1703 return iwl_mld_send_cmd(mld, &hcmd); 1704 } 1705 1706 static int 1707 iwl_mld_wowlan_config(struct iwl_mld *mld, struct ieee80211_vif *bss_vif, 1708 struct cfg80211_wowlan *wowlan) 1709 { 1710 struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(bss_vif); 1711 struct ieee80211_sta *ap_sta = mld_vif->ap_sta; 1712 struct iwl_wowlan_config_cmd wowlan_config_cmd = { 1713 .offloading_tid = IWL_WOWLAN_OFFLOAD_TID, 1714 }; 1715 u32 sta_id_mask; 1716 int ap_sta_id, ret; 1717 int link_id = iwl_mld_get_primary_link(bss_vif); 1718 struct ieee80211_bss_conf *link_conf; 1719 1720 ret = iwl_mld_block_emlsr_sync(mld, bss_vif, 1721 IWL_MLD_EMLSR_BLOCKED_WOWLAN, link_id); 1722 if (ret) 1723 return ret; 1724 1725 link_conf = link_conf_dereference_protected(bss_vif, link_id); 1726 1727 if (WARN_ON(!ap_sta || !link_conf)) 1728 return -EINVAL; 1729 1730 sta_id_mask = iwl_mld_fw_sta_id_mask(mld, ap_sta); 1731 if (WARN_ON(hweight32(sta_id_mask) != 1)) 1732 return -EINVAL; 1733 1734 ap_sta_id = __ffs(sta_id_mask); 1735 wowlan_config_cmd.sta_id = ap_sta_id; 1736 1737 ret = iwl_mld_ensure_queue(mld, 1738 ap_sta->txq[wowlan_config_cmd.offloading_tid]); 1739 if (ret) 1740 return ret; 1741 1742 iwl_mld_set_wowlan_config_cmd(mld, wowlan, 1743 &wowlan_config_cmd, ap_sta, link_conf); 1744 ret = iwl_mld_send_cmd_pdu(mld, WOWLAN_CONFIGURATION, 1745 &wowlan_config_cmd); 1746 if (ret) 1747 return ret; 1748 1749 ret = iwl_mld_suspend_send_security_cmds(mld, bss_vif, mld_vif, 1750 ap_sta_id); 1751 if (ret) 1752 return ret; 1753 1754 ret = iwl_mld_send_patterns(mld, wowlan, ap_sta_id); 1755 if (ret) 1756 return ret; 1757 1758 ret = iwl_mld_send_proto_offload(mld, bss_vif, ap_sta_id); 1759 if (ret) 1760 return ret; 1761 1762 iwl_mld_enable_beacon_filter(mld, link_conf, true); 1763 return iwl_mld_update_mac_power(mld, bss_vif, true); 1764 } 1765 1766 int iwl_mld_wowlan_suspend(struct iwl_mld *mld, struct cfg80211_wowlan *wowlan) 1767 { 1768 struct ieee80211_vif *bss_vif; 1769 1770 lockdep_assert_wiphy(mld->wiphy); 1771 1772 if (WARN_ON(!wowlan)) 1773 return 1; 1774 1775 IWL_DEBUG_WOWLAN(mld, "Starting the wowlan suspend flow\n"); 1776 1777 bss_vif = iwl_mld_get_bss_vif(mld); 1778 if (WARN_ON(!bss_vif)) 1779 return 1; 1780 1781 if (!bss_vif->cfg.assoc) { 1782 int ret; 1783 /* If we're not associated, this must be netdetect */ 1784 if (WARN_ON(!wowlan->nd_config)) 1785 return 1; 1786 1787 ret = iwl_mld_netdetect_config(mld, bss_vif, wowlan); 1788 if (!ret) 1789 mld->netdetect = true; 1790 1791 return ret; 1792 } 1793 1794 return iwl_mld_wowlan_config(mld, bss_vif, wowlan); 1795 } 1796 1797 /* Returns 0 on success, 1 if an error occurred in firmware during d3, 1798 * A negative value is expected only in unrecovreable cases. 1799 */ 1800 int iwl_mld_wowlan_resume(struct iwl_mld *mld) 1801 { 1802 struct ieee80211_vif *bss_vif; 1803 struct ieee80211_bss_conf *link_conf; 1804 struct iwl_mld_netdetect_res netdetect_res; 1805 struct iwl_mld_resume_data resume_data = { 1806 .notifs_expected = 1807 IWL_D3_NOTIF_WOWLAN_INFO | 1808 IWL_D3_NOTIF_D3_END_NOTIF, 1809 .netdetect_res = &netdetect_res, 1810 }; 1811 int link_id; 1812 int ret; 1813 1814 lockdep_assert_wiphy(mld->wiphy); 1815 1816 IWL_DEBUG_WOWLAN(mld, "Starting the wowlan resume flow\n"); 1817 1818 if (!mld->fw_status.in_d3) { 1819 IWL_DEBUG_WOWLAN(mld, 1820 "Device_powered_off() was called during wowlan\n"); 1821 goto err; 1822 } 1823 1824 mld->fw_status.resuming = true; 1825 mld->fw_status.in_d3 = false; 1826 mld->scan.last_start_time_jiffies = jiffies; 1827 1828 bss_vif = iwl_mld_get_bss_vif(mld); 1829 if (WARN_ON(!bss_vif)) 1830 goto err; 1831 1832 /* We can't have several links upon wowlan entry, 1833 * this is enforced in the suspend flow. 1834 */ 1835 WARN_ON(hweight16(bss_vif->active_links) > 1); 1836 link_id = bss_vif->active_links ? __ffs(bss_vif->active_links) : 0; 1837 link_conf = link_conf_dereference_protected(bss_vif, link_id); 1838 1839 if (WARN_ON(!link_conf)) 1840 goto err; 1841 1842 iwl_fw_dbg_read_d3_debug_data(&mld->fwrt); 1843 1844 resume_data.wowlan_status = kzalloc(sizeof(*resume_data.wowlan_status), 1845 GFP_KERNEL); 1846 if (!resume_data.wowlan_status) 1847 return -ENOMEM; 1848 1849 if (mld->netdetect) 1850 resume_data.notifs_expected |= IWL_D3_ND_MATCH_INFO; 1851 1852 ret = iwl_mld_wait_d3_notif(mld, &resume_data, true); 1853 if (ret) { 1854 IWL_ERR(mld, "Couldn't get the d3 notifs %d\n", ret); 1855 goto err; 1856 } 1857 1858 if (resume_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE) { 1859 mld->fw_status.in_hw_restart = true; 1860 goto process_wakeup_results; 1861 } 1862 1863 iwl_mld_update_changed_regdomain(mld); 1864 iwl_mld_update_mac_power(mld, bss_vif, false); 1865 iwl_mld_enable_beacon_filter(mld, link_conf, false); 1866 iwl_mld_update_device_power(mld, false); 1867 1868 if (mld->netdetect) 1869 ret = iwl_mld_scan_stop(mld, IWL_MLD_SCAN_NETDETECT, false); 1870 1871 process_wakeup_results: 1872 if (mld->netdetect) { 1873 iwl_mld_process_netdetect_res(mld, bss_vif, &resume_data); 1874 mld->netdetect = false; 1875 } else { 1876 bool keep_connection = 1877 iwl_mld_process_wowlan_status(mld, bss_vif, 1878 resume_data.wowlan_status); 1879 1880 /* EMLSR state will be cleared if the connection is not kept */ 1881 if (keep_connection) 1882 iwl_mld_unblock_emlsr(mld, bss_vif, 1883 IWL_MLD_EMLSR_BLOCKED_WOWLAN); 1884 else 1885 ieee80211_resume_disconnect(bss_vif); 1886 } 1887 1888 goto out; 1889 1890 err: 1891 mld->fw_status.in_hw_restart = true; 1892 ret = 1; 1893 out: 1894 mld->fw_status.resuming = false; 1895 1896 if (resume_data.wowlan_status) { 1897 kfree(resume_data.wowlan_status->wake_packet); 1898 kfree(resume_data.wowlan_status); 1899 } 1900 1901 return ret; 1902 } 1903