1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3 * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5 * Copyright (C) 2016-2017 Intel Deutschland GmbH
6 */
7 #include <linux/etherdevice.h>
8 #include <linux/ip.h>
9 #include <linux/fs.h>
10 #include <net/cfg80211.h>
11 #include <net/ipv6.h>
12 #include <net/tcp.h>
13 #include <net/addrconf.h>
14 #include "iwl-modparams.h"
15 #include "fw-api.h"
16 #include "mvm.h"
17 #include "fw/img.h"
18
iwl_mvm_set_rekey_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct cfg80211_gtk_rekey_data * data)19 void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
20 struct ieee80211_vif *vif,
21 struct cfg80211_gtk_rekey_data *data)
22 {
23 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
24 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
25
26 mutex_lock(&mvm->mutex);
27
28 mvmvif->rekey_data.kek_len = data->kek_len;
29 mvmvif->rekey_data.kck_len = data->kck_len;
30 memcpy(mvmvif->rekey_data.kek, data->kek, data->kek_len);
31 memcpy(mvmvif->rekey_data.kck, data->kck, data->kck_len);
32 mvmvif->rekey_data.akm = data->akm & 0xFF;
33 mvmvif->rekey_data.replay_ctr =
34 cpu_to_le64(be64_to_cpup((const __be64 *)data->replay_ctr));
35 mvmvif->rekey_data.valid = true;
36
37 mutex_unlock(&mvm->mutex);
38 }
39
40 #if IS_ENABLED(CONFIG_IPV6)
iwl_mvm_ipv6_addr_change(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct inet6_dev * idev)41 void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
42 struct ieee80211_vif *vif,
43 struct inet6_dev *idev)
44 {
45 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
46 struct inet6_ifaddr *ifa;
47 int idx = 0;
48
49 memset(mvmvif->tentative_addrs, 0, sizeof(mvmvif->tentative_addrs));
50
51 read_lock_bh(&idev->lock);
52 list_for_each_entry(ifa, &idev->addr_list, if_list) {
53 mvmvif->target_ipv6_addrs[idx] = ifa->addr;
54 if (ifa->flags & IFA_F_TENTATIVE)
55 __set_bit(idx, mvmvif->tentative_addrs);
56 idx++;
57 if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
58 break;
59 }
60 read_unlock_bh(&idev->lock);
61
62 mvmvif->num_target_ipv6_addrs = idx;
63 }
64 #endif
65
iwl_mvm_set_default_unicast_key(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int idx)66 void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
67 struct ieee80211_vif *vif, int idx)
68 {
69 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
70
71 mvmvif->tx_key_idx = idx;
72 }
73
iwl_mvm_convert_p1k(u16 * p1k,__le16 * out)74 static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
75 {
76 int i;
77
78 for (i = 0; i < IWL_P1K_SIZE; i++)
79 out[i] = cpu_to_le16(p1k[i]);
80 }
81
iwl_mvm_find_max_pn(struct ieee80211_key_conf * key,struct iwl_mvm_key_pn * ptk_pn,struct ieee80211_key_seq * seq,int tid,int queues)82 static const u8 *iwl_mvm_find_max_pn(struct ieee80211_key_conf *key,
83 struct iwl_mvm_key_pn *ptk_pn,
84 struct ieee80211_key_seq *seq,
85 int tid, int queues)
86 {
87 const u8 *ret = seq->ccmp.pn;
88 int i;
89
90 /* get the PN from mac80211, used on the default queue */
91 ieee80211_get_key_rx_seq(key, tid, seq);
92
93 /* and use the internal data for the other queues */
94 for (i = 1; i < queues; i++) {
95 const u8 *tmp = ptk_pn->q[i].pn[tid];
96
97 if (memcmp(ret, tmp, IEEE80211_CCMP_PN_LEN) <= 0)
98 ret = tmp;
99 }
100
101 return ret;
102 }
103
104 struct wowlan_key_reprogram_data {
105 bool error;
106 int wep_key_idx;
107 };
108
iwl_mvm_wowlan_program_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)109 static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
110 struct ieee80211_vif *vif,
111 struct ieee80211_sta *sta,
112 struct ieee80211_key_conf *key,
113 void *_data)
114 {
115 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
116 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
117 struct wowlan_key_reprogram_data *data = _data;
118 int ret;
119
120 switch (key->cipher) {
121 case WLAN_CIPHER_SUITE_WEP40:
122 case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
123 struct {
124 struct iwl_mvm_wep_key_cmd wep_key_cmd;
125 struct iwl_mvm_wep_key wep_key;
126 } __packed wkc = {
127 .wep_key_cmd.mac_id_n_color =
128 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
129 mvmvif->color)),
130 .wep_key_cmd.num_keys = 1,
131 /* firmware sets STA_KEY_FLG_WEP_13BYTES */
132 .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
133 .wep_key.key_index = key->keyidx,
134 .wep_key.key_size = key->keylen,
135 };
136
137 /*
138 * This will fail -- the key functions don't set support
139 * pairwise WEP keys. However, that's better than silently
140 * failing WoWLAN. Or maybe not?
141 */
142 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
143 break;
144
145 memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
146 if (key->keyidx == mvmvif->tx_key_idx) {
147 /* TX key must be at offset 0 */
148 wkc.wep_key.key_offset = 0;
149 } else {
150 /* others start at 1 */
151 data->wep_key_idx++;
152 wkc.wep_key.key_offset = data->wep_key_idx;
153 }
154
155 mutex_lock(&mvm->mutex);
156 ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, 0, sizeof(wkc), &wkc);
157 data->error = ret != 0;
158
159 mvm->ptk_ivlen = key->iv_len;
160 mvm->ptk_icvlen = key->icv_len;
161 mvm->gtk_ivlen = key->iv_len;
162 mvm->gtk_icvlen = key->icv_len;
163 mutex_unlock(&mvm->mutex);
164
165 /* don't upload key again */
166 return;
167 }
168 default:
169 data->error = true;
170 return;
171 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
172 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
173 return;
174 case WLAN_CIPHER_SUITE_AES_CMAC:
175 /*
176 * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
177 * but we also shouldn't abort suspend due to that. It does have
178 * support for the IGTK key renewal, but doesn't really use the
179 * IGTK for anything. This means we could spuriously wake up or
180 * be deauthenticated, but that was considered acceptable.
181 */
182 return;
183 case WLAN_CIPHER_SUITE_TKIP:
184 case WLAN_CIPHER_SUITE_CCMP:
185 case WLAN_CIPHER_SUITE_GCMP:
186 case WLAN_CIPHER_SUITE_GCMP_256:
187 break;
188 }
189
190 mutex_lock(&mvm->mutex);
191 /*
192 * The D3 firmware hardcodes the key offset 0 as the key it
193 * uses to transmit packets to the AP, i.e. the PTK.
194 */
195 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
196 mvm->ptk_ivlen = key->iv_len;
197 mvm->ptk_icvlen = key->icv_len;
198 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 0);
199 } else {
200 /*
201 * firmware only supports TSC/RSC for a single key,
202 * so if there are multiple keep overwriting them
203 * with new ones -- this relies on mac80211 doing
204 * list_add_tail().
205 */
206 mvm->gtk_ivlen = key->iv_len;
207 mvm->gtk_icvlen = key->icv_len;
208 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, 1);
209 }
210 mutex_unlock(&mvm->mutex);
211 data->error = ret != 0;
212 }
213
214 struct wowlan_key_rsc_tsc_data {
215 struct iwl_wowlan_rsc_tsc_params_cmd_v4 *rsc_tsc;
216 bool have_rsc_tsc;
217 };
218
iwl_mvm_wowlan_get_rsc_tsc_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)219 static void iwl_mvm_wowlan_get_rsc_tsc_data(struct ieee80211_hw *hw,
220 struct ieee80211_vif *vif,
221 struct ieee80211_sta *sta,
222 struct ieee80211_key_conf *key,
223 void *_data)
224 {
225 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
226 struct wowlan_key_rsc_tsc_data *data = _data;
227 struct aes_sc *aes_sc;
228 struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
229 struct ieee80211_key_seq seq;
230 int i;
231
232 switch (key->cipher) {
233 default:
234 break;
235 case WLAN_CIPHER_SUITE_TKIP:
236 if (sta) {
237 u64 pn64;
238
239 tkip_sc =
240 data->rsc_tsc->params.all_tsc_rsc.tkip.unicast_rsc;
241 tkip_tx_sc =
242 &data->rsc_tsc->params.all_tsc_rsc.tkip.tsc;
243
244 pn64 = atomic64_read(&key->tx_pn);
245 tkip_tx_sc->iv16 = cpu_to_le16(TKIP_PN_TO_IV16(pn64));
246 tkip_tx_sc->iv32 = cpu_to_le32(TKIP_PN_TO_IV32(pn64));
247 } else {
248 tkip_sc =
249 data->rsc_tsc->params.all_tsc_rsc.tkip.multicast_rsc;
250 }
251
252 /*
253 * For non-QoS this relies on the fact that both the uCode and
254 * mac80211 use TID 0 (as they need to to avoid replay attacks)
255 * for checking the IV in the frames.
256 */
257 for (i = 0; i < IWL_NUM_RSC; i++) {
258 ieee80211_get_key_rx_seq(key, i, &seq);
259 tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
260 tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
261 }
262
263 data->have_rsc_tsc = true;
264 break;
265 case WLAN_CIPHER_SUITE_CCMP:
266 case WLAN_CIPHER_SUITE_GCMP:
267 case WLAN_CIPHER_SUITE_GCMP_256:
268 if (sta) {
269 struct aes_sc *aes_tx_sc;
270 u64 pn64;
271
272 aes_sc =
273 data->rsc_tsc->params.all_tsc_rsc.aes.unicast_rsc;
274 aes_tx_sc =
275 &data->rsc_tsc->params.all_tsc_rsc.aes.tsc;
276
277 pn64 = atomic64_read(&key->tx_pn);
278 aes_tx_sc->pn = cpu_to_le64(pn64);
279 } else {
280 aes_sc =
281 data->rsc_tsc->params.all_tsc_rsc.aes.multicast_rsc;
282 }
283
284 /*
285 * For non-QoS this relies on the fact that both the uCode and
286 * mac80211/our RX code use TID 0 for checking the PN.
287 */
288 if (sta && iwl_mvm_has_new_rx_api(mvm)) {
289 struct iwl_mvm_sta *mvmsta;
290 struct iwl_mvm_key_pn *ptk_pn;
291 const u8 *pn;
292
293 mvmsta = iwl_mvm_sta_from_mac80211(sta);
294 rcu_read_lock();
295 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
296 if (WARN_ON(!ptk_pn)) {
297 rcu_read_unlock();
298 break;
299 }
300
301 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
302 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
303 mvm->trans->num_rx_queues);
304 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
305 ((u64)pn[4] << 8) |
306 ((u64)pn[3] << 16) |
307 ((u64)pn[2] << 24) |
308 ((u64)pn[1] << 32) |
309 ((u64)pn[0] << 40));
310 }
311
312 rcu_read_unlock();
313 } else {
314 for (i = 0; i < IWL_NUM_RSC; i++) {
315 u8 *pn = seq.ccmp.pn;
316
317 ieee80211_get_key_rx_seq(key, i, &seq);
318 aes_sc[i].pn = cpu_to_le64((u64)pn[5] |
319 ((u64)pn[4] << 8) |
320 ((u64)pn[3] << 16) |
321 ((u64)pn[2] << 24) |
322 ((u64)pn[1] << 32) |
323 ((u64)pn[0] << 40));
324 }
325 }
326 data->have_rsc_tsc = true;
327 break;
328 }
329 }
330
331 struct wowlan_key_rsc_v5_data {
332 struct iwl_wowlan_rsc_tsc_params_cmd *rsc;
333 bool have_rsc;
334 int gtks;
335 int gtk_ids[4];
336 };
337
iwl_mvm_wowlan_get_rsc_v5_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)338 static void iwl_mvm_wowlan_get_rsc_v5_data(struct ieee80211_hw *hw,
339 struct ieee80211_vif *vif,
340 struct ieee80211_sta *sta,
341 struct ieee80211_key_conf *key,
342 void *_data)
343 {
344 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
345 struct wowlan_key_rsc_v5_data *data = _data;
346 struct ieee80211_key_seq seq;
347 __le64 *rsc;
348 int i;
349
350 /* only for ciphers that can be PTK/GTK */
351 switch (key->cipher) {
352 default:
353 return;
354 case WLAN_CIPHER_SUITE_TKIP:
355 case WLAN_CIPHER_SUITE_CCMP:
356 case WLAN_CIPHER_SUITE_GCMP:
357 case WLAN_CIPHER_SUITE_GCMP_256:
358 break;
359 }
360
361 if (sta) {
362 rsc = data->rsc->ucast_rsc;
363 } else {
364 if (WARN_ON(data->gtks >= ARRAY_SIZE(data->gtk_ids)))
365 return;
366 data->gtk_ids[data->gtks] = key->keyidx;
367 rsc = data->rsc->mcast_rsc[data->gtks % 2];
368 if (WARN_ON(key->keyidx >=
369 ARRAY_SIZE(data->rsc->mcast_key_id_map)))
370 return;
371 data->rsc->mcast_key_id_map[key->keyidx] = data->gtks % 2;
372 if (data->gtks >= 2) {
373 int prev = data->gtks - 2;
374 int prev_idx = data->gtk_ids[prev];
375
376 data->rsc->mcast_key_id_map[prev_idx] =
377 IWL_MCAST_KEY_MAP_INVALID;
378 }
379 data->gtks++;
380 }
381
382 switch (key->cipher) {
383 default:
384 WARN_ON(1);
385 break;
386 case WLAN_CIPHER_SUITE_TKIP:
387
388 /*
389 * For non-QoS this relies on the fact that both the uCode and
390 * mac80211 use TID 0 (as they need to to avoid replay attacks)
391 * for checking the IV in the frames.
392 */
393 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
394 ieee80211_get_key_rx_seq(key, i, &seq);
395
396 rsc[i] = cpu_to_le64(((u64)seq.tkip.iv32 << 16) |
397 seq.tkip.iv16);
398 }
399
400 data->have_rsc = true;
401 break;
402 case WLAN_CIPHER_SUITE_CCMP:
403 case WLAN_CIPHER_SUITE_GCMP:
404 case WLAN_CIPHER_SUITE_GCMP_256:
405 /*
406 * For non-QoS this relies on the fact that both the uCode and
407 * mac80211/our RX code use TID 0 for checking the PN.
408 */
409 if (sta) {
410 struct iwl_mvm_sta *mvmsta;
411 struct iwl_mvm_key_pn *ptk_pn;
412 const u8 *pn;
413
414 mvmsta = iwl_mvm_sta_from_mac80211(sta);
415 rcu_read_lock();
416 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
417 if (WARN_ON(!ptk_pn)) {
418 rcu_read_unlock();
419 break;
420 }
421
422 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
423 pn = iwl_mvm_find_max_pn(key, ptk_pn, &seq, i,
424 mvm->trans->num_rx_queues);
425 rsc[i] = cpu_to_le64((u64)pn[5] |
426 ((u64)pn[4] << 8) |
427 ((u64)pn[3] << 16) |
428 ((u64)pn[2] << 24) |
429 ((u64)pn[1] << 32) |
430 ((u64)pn[0] << 40));
431 }
432
433 rcu_read_unlock();
434 } else {
435 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
436 u8 *pn = seq.ccmp.pn;
437
438 ieee80211_get_key_rx_seq(key, i, &seq);
439 rsc[i] = cpu_to_le64((u64)pn[5] |
440 ((u64)pn[4] << 8) |
441 ((u64)pn[3] << 16) |
442 ((u64)pn[2] << 24) |
443 ((u64)pn[1] << 32) |
444 ((u64)pn[0] << 40));
445 }
446 }
447 data->have_rsc = true;
448 break;
449 }
450 }
451
iwl_mvm_wowlan_config_rsc_tsc(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_vif_link_info * mvm_link)452 static int iwl_mvm_wowlan_config_rsc_tsc(struct iwl_mvm *mvm,
453 struct ieee80211_vif *vif,
454 struct iwl_mvm_vif_link_info *mvm_link)
455 {
456 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TSC_RSC_PARAM,
457 IWL_FW_CMD_VER_UNKNOWN);
458 int ret;
459
460 if (ver == 5) {
461 struct wowlan_key_rsc_v5_data data = {};
462 int i;
463
464 data.rsc = kzalloc(sizeof(*data.rsc), GFP_KERNEL);
465 if (!data.rsc)
466 return -ENOMEM;
467
468 for (i = 0; i < ARRAY_SIZE(data.rsc->mcast_key_id_map); i++)
469 data.rsc->mcast_key_id_map[i] =
470 IWL_MCAST_KEY_MAP_INVALID;
471 data.rsc->sta_id = cpu_to_le32(mvm_link->ap_sta_id);
472
473 ieee80211_iter_keys(mvm->hw, vif,
474 iwl_mvm_wowlan_get_rsc_v5_data,
475 &data);
476
477 if (data.have_rsc)
478 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
479 CMD_ASYNC, sizeof(*data.rsc),
480 data.rsc);
481 else
482 ret = 0;
483 kfree(data.rsc);
484 } else if (ver == 4 || ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN) {
485 struct wowlan_key_rsc_tsc_data data = {};
486 int size;
487
488 data.rsc_tsc = kzalloc(sizeof(*data.rsc_tsc), GFP_KERNEL);
489 if (!data.rsc_tsc)
490 return -ENOMEM;
491
492 if (ver == 4) {
493 size = sizeof(*data.rsc_tsc);
494 data.rsc_tsc->sta_id =
495 cpu_to_le32(mvm_link->ap_sta_id);
496 } else {
497 /* ver == 2 || ver == IWL_FW_CMD_VER_UNKNOWN */
498 size = sizeof(data.rsc_tsc->params);
499 }
500
501 ieee80211_iter_keys(mvm->hw, vif,
502 iwl_mvm_wowlan_get_rsc_tsc_data,
503 &data);
504
505 if (data.have_rsc_tsc)
506 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_TSC_RSC_PARAM,
507 CMD_ASYNC, size,
508 data.rsc_tsc);
509 else
510 ret = 0;
511 kfree(data.rsc_tsc);
512 } else {
513 ret = 0;
514 WARN_ON_ONCE(1);
515 }
516
517 return ret;
518 }
519
520 struct wowlan_key_tkip_data {
521 struct iwl_wowlan_tkip_params_cmd tkip;
522 bool have_tkip_keys;
523 };
524
iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)525 static void iwl_mvm_wowlan_get_tkip_data(struct ieee80211_hw *hw,
526 struct ieee80211_vif *vif,
527 struct ieee80211_sta *sta,
528 struct ieee80211_key_conf *key,
529 void *_data)
530 {
531 struct wowlan_key_tkip_data *data = _data;
532 struct iwl_p1k_cache *rx_p1ks;
533 u8 *rx_mic_key;
534 struct ieee80211_key_seq seq;
535 u32 cur_rx_iv32 = 0;
536 u16 p1k[IWL_P1K_SIZE];
537 int i;
538
539 switch (key->cipher) {
540 default:
541 break;
542 case WLAN_CIPHER_SUITE_TKIP:
543 if (sta) {
544 u64 pn64;
545
546 rx_p1ks = data->tkip.rx_uni;
547
548 pn64 = atomic64_read(&key->tx_pn);
549
550 ieee80211_get_tkip_p1k_iv(key, TKIP_PN_TO_IV32(pn64),
551 p1k);
552 iwl_mvm_convert_p1k(p1k, data->tkip.tx.p1k);
553
554 memcpy(data->tkip.mic_keys.tx,
555 &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
556 IWL_MIC_KEY_SIZE);
557
558 rx_mic_key = data->tkip.mic_keys.rx_unicast;
559 } else {
560 rx_p1ks = data->tkip.rx_multi;
561 rx_mic_key = data->tkip.mic_keys.rx_mcast;
562 }
563
564 for (i = 0; i < IWL_NUM_RSC; i++) {
565 ieee80211_get_key_rx_seq(key, i, &seq);
566 /* wrapping isn't allowed, AP must rekey */
567 if (seq.tkip.iv32 > cur_rx_iv32)
568 cur_rx_iv32 = seq.tkip.iv32;
569 }
570
571 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
572 cur_rx_iv32, p1k);
573 iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
574 ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
575 cur_rx_iv32 + 1, p1k);
576 iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
577
578 memcpy(rx_mic_key,
579 &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
580 IWL_MIC_KEY_SIZE);
581
582 data->have_tkip_keys = true;
583 break;
584 }
585 }
586
587 struct wowlan_key_gtk_type_iter {
588 struct iwl_wowlan_kek_kck_material_cmd_v4 *kek_kck_cmd;
589 };
590
iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)591 static void iwl_mvm_wowlan_gtk_type_iter(struct ieee80211_hw *hw,
592 struct ieee80211_vif *vif,
593 struct ieee80211_sta *sta,
594 struct ieee80211_key_conf *key,
595 void *_data)
596 {
597 struct wowlan_key_gtk_type_iter *data = _data;
598 __le32 *cipher = NULL;
599
600 if (key->keyidx == 4 || key->keyidx == 5)
601 cipher = &data->kek_kck_cmd->igtk_cipher;
602 if (key->keyidx == 6 || key->keyidx == 7)
603 cipher = &data->kek_kck_cmd->bigtk_cipher;
604
605 switch (key->cipher) {
606 default:
607 return;
608 case WLAN_CIPHER_SUITE_TKIP:
609 if (!sta)
610 data->kek_kck_cmd->gtk_cipher =
611 cpu_to_le32(STA_KEY_FLG_TKIP);
612 return;
613 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
614 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
615 if (cipher)
616 *cipher = cpu_to_le32(STA_KEY_FLG_GCMP);
617 return;
618 case WLAN_CIPHER_SUITE_AES_CMAC:
619 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
620 if (cipher)
621 *cipher = cpu_to_le32(STA_KEY_FLG_CCM);
622 return;
623 case WLAN_CIPHER_SUITE_CCMP:
624 if (!sta)
625 data->kek_kck_cmd->gtk_cipher =
626 cpu_to_le32(STA_KEY_FLG_CCM);
627 return;
628 case WLAN_CIPHER_SUITE_GCMP:
629 case WLAN_CIPHER_SUITE_GCMP_256:
630 if (!sta)
631 data->kek_kck_cmd->gtk_cipher =
632 cpu_to_le32(STA_KEY_FLG_GCMP);
633 return;
634 }
635 }
636
iwl_mvm_send_patterns_v1(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan)637 static int iwl_mvm_send_patterns_v1(struct iwl_mvm *mvm,
638 struct cfg80211_wowlan *wowlan)
639 {
640 struct iwl_wowlan_patterns_cmd_v1 *pattern_cmd;
641 struct iwl_host_cmd cmd = {
642 .id = WOWLAN_PATTERNS,
643 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
644 };
645 int i, err;
646
647 if (!wowlan->n_patterns)
648 return 0;
649
650 cmd.len[0] = struct_size(pattern_cmd, patterns, wowlan->n_patterns);
651
652 pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
653 if (!pattern_cmd)
654 return -ENOMEM;
655
656 pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
657
658 for (i = 0; i < wowlan->n_patterns; i++) {
659 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
660
661 memcpy(&pattern_cmd->patterns[i].mask,
662 wowlan->patterns[i].mask, mask_len);
663 memcpy(&pattern_cmd->patterns[i].pattern,
664 wowlan->patterns[i].pattern,
665 wowlan->patterns[i].pattern_len);
666 pattern_cmd->patterns[i].mask_size = mask_len;
667 pattern_cmd->patterns[i].pattern_size =
668 wowlan->patterns[i].pattern_len;
669 }
670
671 cmd.data[0] = pattern_cmd;
672 err = iwl_mvm_send_cmd(mvm, &cmd);
673 kfree(pattern_cmd);
674 return err;
675 }
676
iwl_mvm_send_patterns(struct iwl_mvm * mvm,struct iwl_mvm_vif_link_info * mvm_link,struct cfg80211_wowlan * wowlan)677 static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
678 struct iwl_mvm_vif_link_info *mvm_link,
679 struct cfg80211_wowlan *wowlan)
680 {
681 struct iwl_wowlan_patterns_cmd *pattern_cmd;
682 struct iwl_host_cmd cmd = {
683 .id = WOWLAN_PATTERNS,
684 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
685 };
686 int i, err;
687 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
688 IWL_FW_CMD_VER_UNKNOWN);
689
690 if (!wowlan->n_patterns)
691 return 0;
692
693 cmd.len[0] = sizeof(*pattern_cmd) +
694 wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern_v2);
695
696 pattern_cmd = kzalloc(cmd.len[0], GFP_KERNEL);
697 if (!pattern_cmd)
698 return -ENOMEM;
699
700 pattern_cmd->n_patterns = wowlan->n_patterns;
701 if (ver >= 3)
702 pattern_cmd->sta_id = mvm_link->ap_sta_id;
703
704 for (i = 0; i < wowlan->n_patterns; i++) {
705 int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
706
707 pattern_cmd->patterns[i].pattern_type =
708 WOWLAN_PATTERN_TYPE_BITMASK;
709
710 memcpy(&pattern_cmd->patterns[i].u.bitmask.mask,
711 wowlan->patterns[i].mask, mask_len);
712 memcpy(&pattern_cmd->patterns[i].u.bitmask.pattern,
713 wowlan->patterns[i].pattern,
714 wowlan->patterns[i].pattern_len);
715 pattern_cmd->patterns[i].u.bitmask.mask_size = mask_len;
716 pattern_cmd->patterns[i].u.bitmask.pattern_size =
717 wowlan->patterns[i].pattern_len;
718 }
719
720 cmd.data[0] = pattern_cmd;
721 err = iwl_mvm_send_cmd(mvm, &cmd);
722 kfree(pattern_cmd);
723 return err;
724 }
725
iwl_mvm_d3_reprogram(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct ieee80211_sta * ap_sta)726 static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
727 struct ieee80211_sta *ap_sta)
728 {
729 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
730 struct ieee80211_chanctx_conf *ctx;
731 u8 chains_static, chains_dynamic;
732 struct cfg80211_chan_def chandef, ap_def;
733 int ret, i;
734 struct iwl_binding_cmd_v1 binding_cmd = {};
735 struct iwl_time_quota_cmd quota_cmd = {};
736 struct iwl_time_quota_data *quota;
737 u32 status;
738
739 if (WARN_ON_ONCE(iwl_mvm_is_cdb_supported(mvm) ||
740 ieee80211_vif_is_mld(vif)))
741 return -EINVAL;
742
743 /* add back the PHY */
744 if (WARN_ON(!mvmvif->deflink.phy_ctxt))
745 return -EINVAL;
746
747 rcu_read_lock();
748 ctx = rcu_dereference(vif->bss_conf.chanctx_conf);
749 if (WARN_ON(!ctx)) {
750 rcu_read_unlock();
751 return -EINVAL;
752 }
753 chandef = ctx->def;
754 ap_def = ctx->ap;
755 chains_static = ctx->rx_chains_static;
756 chains_dynamic = ctx->rx_chains_dynamic;
757 rcu_read_unlock();
758
759 ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt, &chandef,
760 &ap_def, chains_static, chains_dynamic);
761 if (ret)
762 return ret;
763
764 /* add back the MAC */
765 mvmvif->uploaded = false;
766
767 if (WARN_ON(!vif->cfg.assoc))
768 return -EINVAL;
769
770 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
771 if (ret)
772 return ret;
773
774 /* add back binding - XXX refactor? */
775 binding_cmd.id_and_color =
776 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
777 mvmvif->deflink.phy_ctxt->color));
778 binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
779 binding_cmd.phy =
780 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
781 mvmvif->deflink.phy_ctxt->color));
782 binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
783 mvmvif->color));
784 for (i = 1; i < MAX_MACS_IN_BINDING; i++)
785 binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
786
787 status = 0;
788 ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
789 IWL_BINDING_CMD_SIZE_V1, &binding_cmd,
790 &status);
791 if (ret) {
792 IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
793 return ret;
794 }
795
796 if (status) {
797 IWL_ERR(mvm, "Binding command failed: %u\n", status);
798 return -EIO;
799 }
800
801 ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false, 0);
802 if (ret)
803 return ret;
804 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->deflink.ap_sta_id],
805 ap_sta);
806
807 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
808 if (ret)
809 return ret;
810
811 /* and some quota */
812 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, 0);
813 quota->id_and_color =
814 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->deflink.phy_ctxt->id,
815 mvmvif->deflink.phy_ctxt->color));
816 quota->quota = cpu_to_le32(IWL_MVM_MAX_QUOTA);
817 quota->max_duration = cpu_to_le32(IWL_MVM_MAX_QUOTA);
818
819 for (i = 1; i < MAX_BINDINGS; i++) {
820 quota = iwl_mvm_quota_cmd_get_quota(mvm, "a_cmd, i);
821 quota->id_and_color = cpu_to_le32(FW_CTXT_INVALID);
822 }
823
824 ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, 0,
825 iwl_mvm_quota_cmd_size(mvm), "a_cmd);
826 if (ret)
827 IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
828
829 if (iwl_mvm_is_lar_supported(mvm) && iwl_mvm_init_fw_regd(mvm, false))
830 IWL_ERR(mvm, "Failed to initialize D3 LAR information\n");
831
832 return 0;
833 }
834
iwl_mvm_get_last_nonqos_seq(struct iwl_mvm * mvm,struct ieee80211_vif * vif)835 static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
836 struct ieee80211_vif *vif)
837 {
838 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
839 struct iwl_nonqos_seq_query_cmd query_cmd = {
840 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
841 .mac_id_n_color =
842 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
843 mvmvif->color)),
844 };
845 struct iwl_host_cmd cmd = {
846 .id = NON_QOS_TX_COUNTER_CMD,
847 .flags = CMD_WANT_SKB,
848 };
849 int err;
850 u32 size;
851
852 cmd.data[0] = &query_cmd;
853 cmd.len[0] = sizeof(query_cmd);
854
855 err = iwl_mvm_send_cmd(mvm, &cmd);
856 if (err)
857 return err;
858
859 size = iwl_rx_packet_payload_len(cmd.resp_pkt);
860 if (size < sizeof(__le16)) {
861 err = -EINVAL;
862 } else {
863 err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
864 /* firmware returns next, not last-used seqno */
865 err = (u16) (err - 0x10);
866 }
867
868 iwl_free_resp(&cmd);
869 return err;
870 }
871
iwl_mvm_set_last_nonqos_seq(struct iwl_mvm * mvm,struct ieee80211_vif * vif)872 void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
873 {
874 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
875 struct iwl_nonqos_seq_query_cmd query_cmd = {
876 .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
877 .mac_id_n_color =
878 cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
879 mvmvif->color)),
880 .value = cpu_to_le16(mvmvif->seqno),
881 };
882
883 /* return if called during restart, not resume from D3 */
884 if (!mvmvif->seqno_valid)
885 return;
886
887 mvmvif->seqno_valid = false;
888
889 if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, 0,
890 sizeof(query_cmd), &query_cmd))
891 IWL_ERR(mvm, "failed to set non-QoS seqno\n");
892 }
893
iwl_mvm_switch_to_d3(struct iwl_mvm * mvm)894 static int iwl_mvm_switch_to_d3(struct iwl_mvm *mvm)
895 {
896 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
897
898 iwl_mvm_stop_device(mvm);
899 /*
900 * Set the HW restart bit -- this is mostly true as we're
901 * going to load new firmware and reprogram that, though
902 * the reprogramming is going to be manual to avoid adding
903 * all the MACs that aren't support.
904 * We don't have to clear up everything though because the
905 * reprogramming is manual. When we resume, we'll actually
906 * go through a proper restart sequence again to switch
907 * back to the runtime firmware image.
908 */
909 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
910
911 /* the fw is reset, so all the keys are cleared */
912 memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
913
914 mvm->ptk_ivlen = 0;
915 mvm->ptk_icvlen = 0;
916 mvm->ptk_ivlen = 0;
917 mvm->ptk_icvlen = 0;
918
919 return iwl_mvm_load_d3_fw(mvm);
920 }
921
922 static int
iwl_mvm_get_wowlan_config(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan,struct iwl_wowlan_config_cmd * wowlan_config_cmd,struct ieee80211_vif * vif,struct iwl_mvm_vif * mvmvif,struct ieee80211_sta * ap_sta)923 iwl_mvm_get_wowlan_config(struct iwl_mvm *mvm,
924 struct cfg80211_wowlan *wowlan,
925 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
926 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
927 struct ieee80211_sta *ap_sta)
928 {
929 struct iwl_mvm_sta *mvm_ap_sta = iwl_mvm_sta_from_mac80211(ap_sta);
930
931 /* TODO: wowlan_config_cmd->wowlan_ba_teardown_tids */
932
933 wowlan_config_cmd->is_11n_connection =
934 ap_sta->deflink.ht_cap.ht_supported;
935 wowlan_config_cmd->flags = ENABLE_L3_FILTERING |
936 ENABLE_NBNS_FILTERING | ENABLE_DHCP_FILTERING;
937
938 if (ap_sta->mfp)
939 wowlan_config_cmd->flags |= IS_11W_ASSOC;
940
941 if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_CONFIGURATION, 0) < 6) {
942 /* Query the last used seqno and set it */
943 int ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
944
945 if (ret < 0)
946 return ret;
947
948 wowlan_config_cmd->non_qos_seq = cpu_to_le16(ret);
949 }
950
951 iwl_mvm_set_wowlan_qos_seq(mvm_ap_sta, wowlan_config_cmd);
952
953 if (wowlan->disconnect)
954 wowlan_config_cmd->wakeup_filter |=
955 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
956 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
957 if (wowlan->magic_pkt)
958 wowlan_config_cmd->wakeup_filter |=
959 cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
960 if (wowlan->gtk_rekey_failure)
961 wowlan_config_cmd->wakeup_filter |=
962 cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
963 if (wowlan->eap_identity_req)
964 wowlan_config_cmd->wakeup_filter |=
965 cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
966 if (wowlan->four_way_handshake)
967 wowlan_config_cmd->wakeup_filter |=
968 cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
969 if (wowlan->n_patterns)
970 wowlan_config_cmd->wakeup_filter |=
971 cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
972
973 if (wowlan->rfkill_release)
974 wowlan_config_cmd->wakeup_filter |=
975 cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
976
977 if (wowlan->tcp) {
978 /*
979 * Set the "link change" (really "link lost") flag as well
980 * since that implies losing the TCP connection.
981 */
982 wowlan_config_cmd->wakeup_filter |=
983 cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
984 IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
985 IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
986 IWL_WOWLAN_WAKEUP_LINK_CHANGE);
987 }
988
989 if (wowlan->any) {
990 wowlan_config_cmd->wakeup_filter |=
991 cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
992 IWL_WOWLAN_WAKEUP_LINK_CHANGE |
993 IWL_WOWLAN_WAKEUP_RX_FRAME |
994 IWL_WOWLAN_WAKEUP_BCN_FILTERING);
995 }
996
997 return 0;
998 }
999
iwl_mvm_wowlan_config_key_params(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_mvm_vif_link_info * mvm_link)1000 static int iwl_mvm_wowlan_config_key_params(struct iwl_mvm *mvm,
1001 struct ieee80211_vif *vif,
1002 struct iwl_mvm_vif_link_info *mvm_link)
1003 {
1004 bool unified = fw_has_capa(&mvm->fw->ucode_capa,
1005 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1006 struct wowlan_key_reprogram_data key_data = {};
1007 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1008 int ret;
1009 u8 cmd_ver;
1010 size_t cmd_size;
1011
1012 if (!unified) {
1013 /*
1014 * if we have to configure keys, call ieee80211_iter_keys(),
1015 * as we need non-atomic context in order to take the
1016 * required locks.
1017 */
1018 /*
1019 * Note that currently we don't use CMD_ASYNC in the iterator.
1020 * In case of key_data.configure_keys, all the configured
1021 * commands are SYNC, and iwl_mvm_wowlan_program_keys() will
1022 * take care of locking/unlocking mvm->mutex.
1023 */
1024 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_program_keys,
1025 &key_data);
1026
1027 if (key_data.error)
1028 return -EIO;
1029 }
1030
1031 ret = iwl_mvm_wowlan_config_rsc_tsc(mvm, vif, mvm_link);
1032 if (ret)
1033 return ret;
1034
1035 if (!fw_has_api(&mvm->fw->ucode_capa,
1036 IWL_UCODE_TLV_API_TKIP_MIC_KEYS)) {
1037 int ver = iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_TKIP_PARAM,
1038 IWL_FW_CMD_VER_UNKNOWN);
1039 struct wowlan_key_tkip_data tkip_data = {};
1040 int size;
1041
1042 if (ver == 2) {
1043 size = sizeof(tkip_data.tkip);
1044 tkip_data.tkip.sta_id =
1045 cpu_to_le32(mvm_link->ap_sta_id);
1046 } else if (ver == 1 || ver == IWL_FW_CMD_VER_UNKNOWN) {
1047 size = sizeof(struct iwl_wowlan_tkip_params_cmd_ver_1);
1048 } else {
1049 WARN_ON_ONCE(1);
1050 return -EINVAL;
1051 }
1052
1053 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_get_tkip_data,
1054 &tkip_data);
1055
1056 if (tkip_data.have_tkip_keys) {
1057 /* send relevant data according to CMD version */
1058 ret = iwl_mvm_send_cmd_pdu(mvm,
1059 WOWLAN_TKIP_PARAM,
1060 CMD_ASYNC, size,
1061 &tkip_data.tkip);
1062 if (ret)
1063 return ret;
1064 }
1065 }
1066
1067 /* configure rekey data only if offloaded rekey is supported (d3) */
1068 if (mvmvif->rekey_data.valid) {
1069 struct iwl_wowlan_kek_kck_material_cmd_v4 kek_kck_cmd = {};
1070 struct iwl_wowlan_kek_kck_material_cmd_v4 *_kek_kck_cmd =
1071 &kek_kck_cmd;
1072 struct wowlan_key_gtk_type_iter gtk_type_data = {
1073 .kek_kck_cmd = _kek_kck_cmd,
1074 };
1075
1076 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
1077 WOWLAN_KEK_KCK_MATERIAL,
1078 IWL_FW_CMD_VER_UNKNOWN);
1079 if (WARN_ON(cmd_ver != 2 && cmd_ver != 3 && cmd_ver != 4 &&
1080 cmd_ver != IWL_FW_CMD_VER_UNKNOWN))
1081 return -EINVAL;
1082
1083 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_wowlan_gtk_type_iter,
1084 >k_type_data);
1085
1086 memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
1087 mvmvif->rekey_data.kck_len);
1088 kek_kck_cmd.kck_len = cpu_to_le16(mvmvif->rekey_data.kck_len);
1089 memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
1090 mvmvif->rekey_data.kek_len);
1091 kek_kck_cmd.kek_len = cpu_to_le16(mvmvif->rekey_data.kek_len);
1092 kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
1093 kek_kck_cmd.akm = cpu_to_le32(mvmvif->rekey_data.akm);
1094 kek_kck_cmd.sta_id = cpu_to_le32(mvm_link->ap_sta_id);
1095
1096 if (cmd_ver == 4) {
1097 cmd_size = sizeof(struct iwl_wowlan_kek_kck_material_cmd_v4);
1098 } else {
1099 if (cmd_ver == 3)
1100 cmd_size =
1101 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v3);
1102 else
1103 cmd_size =
1104 sizeof(struct iwl_wowlan_kek_kck_material_cmd_v2);
1105 /* skip the sta_id at the beginning */
1106 _kek_kck_cmd = (void *)
1107 ((u8 *)_kek_kck_cmd + sizeof(kek_kck_cmd.sta_id));
1108 }
1109
1110 IWL_DEBUG_WOWLAN(mvm, "setting akm %d\n",
1111 mvmvif->rekey_data.akm);
1112
1113 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_KEK_KCK_MATERIAL,
1114 CMD_ASYNC, cmd_size, _kek_kck_cmd);
1115 if (ret)
1116 return ret;
1117 }
1118
1119 return 0;
1120 }
1121
1122 static int
iwl_mvm_wowlan_config(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan,struct iwl_wowlan_config_cmd * wowlan_config_cmd,struct ieee80211_vif * vif,struct iwl_mvm_vif * mvmvif,struct iwl_mvm_vif_link_info * mvm_link,struct ieee80211_sta * ap_sta)1123 iwl_mvm_wowlan_config(struct iwl_mvm *mvm,
1124 struct cfg80211_wowlan *wowlan,
1125 struct iwl_wowlan_config_cmd *wowlan_config_cmd,
1126 struct ieee80211_vif *vif, struct iwl_mvm_vif *mvmvif,
1127 struct iwl_mvm_vif_link_info *mvm_link,
1128 struct ieee80211_sta *ap_sta)
1129 {
1130 int ret;
1131 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1132 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1133
1134 mvm->offload_tid = wowlan_config_cmd->offloading_tid;
1135
1136 if (!unified_image) {
1137 ret = iwl_mvm_switch_to_d3(mvm);
1138 if (ret)
1139 return ret;
1140
1141 ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
1142 if (ret)
1143 return ret;
1144 }
1145
1146 ret = iwl_mvm_wowlan_config_key_params(mvm, vif, mvm_link);
1147 if (ret)
1148 return ret;
1149
1150 ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION, 0,
1151 sizeof(*wowlan_config_cmd),
1152 wowlan_config_cmd);
1153 if (ret)
1154 return ret;
1155
1156 if (fw_has_api(&mvm->fw->ucode_capa,
1157 IWL_UCODE_TLV_API_WOWLAN_TCP_SYN_WAKE))
1158 ret = iwl_mvm_send_patterns(mvm, mvm_link, wowlan);
1159 else
1160 ret = iwl_mvm_send_patterns_v1(mvm, wowlan);
1161 if (ret)
1162 return ret;
1163
1164 return iwl_mvm_send_proto_offload(mvm, vif, false, true, 0,
1165 mvm_link->ap_sta_id);
1166 }
1167
1168 static int
iwl_mvm_netdetect_config(struct iwl_mvm * mvm,struct cfg80211_wowlan * wowlan,struct cfg80211_sched_scan_request * nd_config,struct ieee80211_vif * vif)1169 iwl_mvm_netdetect_config(struct iwl_mvm *mvm,
1170 struct cfg80211_wowlan *wowlan,
1171 struct cfg80211_sched_scan_request *nd_config,
1172 struct ieee80211_vif *vif)
1173 {
1174 int ret;
1175 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1176 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1177
1178 if (!unified_image) {
1179 ret = iwl_mvm_switch_to_d3(mvm);
1180 if (ret)
1181 return ret;
1182 } else {
1183 /* In theory, we wouldn't have to stop a running sched
1184 * scan in order to start another one (for
1185 * net-detect). But in practice this doesn't seem to
1186 * work properly, so stop any running sched_scan now.
1187 */
1188 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
1189 if (ret)
1190 return ret;
1191 }
1192
1193 ret = iwl_mvm_sched_scan_start(mvm, vif, nd_config, &mvm->nd_ies,
1194 IWL_MVM_SCAN_NETDETECT);
1195 if (ret)
1196 return ret;
1197
1198 if (WARN_ON(mvm->nd_match_sets || mvm->nd_channels))
1199 return -EBUSY;
1200
1201 /* save the sched scan matchsets... */
1202 if (nd_config->n_match_sets) {
1203 mvm->nd_match_sets = kmemdup(nd_config->match_sets,
1204 sizeof(*nd_config->match_sets) *
1205 nd_config->n_match_sets,
1206 GFP_KERNEL);
1207 if (mvm->nd_match_sets)
1208 mvm->n_nd_match_sets = nd_config->n_match_sets;
1209 }
1210
1211 /* ...and the sched scan channels for later reporting */
1212 mvm->nd_channels = kmemdup(nd_config->channels,
1213 sizeof(*nd_config->channels) *
1214 nd_config->n_channels,
1215 GFP_KERNEL);
1216 if (mvm->nd_channels)
1217 mvm->n_nd_channels = nd_config->n_channels;
1218
1219 return 0;
1220 }
1221
iwl_mvm_free_nd(struct iwl_mvm * mvm)1222 static void iwl_mvm_free_nd(struct iwl_mvm *mvm)
1223 {
1224 kfree(mvm->nd_match_sets);
1225 mvm->nd_match_sets = NULL;
1226 mvm->n_nd_match_sets = 0;
1227 kfree(mvm->nd_channels);
1228 mvm->nd_channels = NULL;
1229 mvm->n_nd_channels = 0;
1230 }
1231
__iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan,bool test)1232 static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
1233 struct cfg80211_wowlan *wowlan,
1234 bool test)
1235 {
1236 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1237 struct ieee80211_vif *vif = NULL;
1238 struct iwl_mvm_vif *mvmvif = NULL;
1239 struct ieee80211_sta *ap_sta = NULL;
1240 struct iwl_mvm_vif_link_info *mvm_link;
1241 struct iwl_d3_manager_config d3_cfg_cmd_data = {
1242 /*
1243 * Program the minimum sleep time to 10 seconds, as many
1244 * platforms have issues processing a wakeup signal while
1245 * still being in the process of suspending.
1246 */
1247 .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
1248 };
1249 struct iwl_host_cmd d3_cfg_cmd = {
1250 .id = D3_CONFIG_CMD,
1251 .flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
1252 .data[0] = &d3_cfg_cmd_data,
1253 .len[0] = sizeof(d3_cfg_cmd_data),
1254 };
1255 int ret;
1256 int len __maybe_unused;
1257 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
1258 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
1259
1260 if (!wowlan) {
1261 /*
1262 * mac80211 shouldn't get here, but for D3 test
1263 * it doesn't warrant a warning
1264 */
1265 WARN_ON(!test);
1266 return -EINVAL;
1267 }
1268
1269 vif = iwl_mvm_get_bss_vif(mvm);
1270 if (IS_ERR_OR_NULL(vif))
1271 return 1;
1272
1273 ret = iwl_mvm_block_esr_sync(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
1274 if (ret)
1275 return ret;
1276
1277 mutex_lock(&mvm->mutex);
1278
1279 set_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1280
1281 synchronize_net();
1282
1283 mvmvif = iwl_mvm_vif_from_mac80211(vif);
1284
1285 mvm_link = mvmvif->link[iwl_mvm_get_primary_link(vif)];
1286 if (WARN_ON_ONCE(!mvm_link)) {
1287 ret = -EINVAL;
1288 goto out_noreset;
1289 }
1290
1291 if (mvm_link->ap_sta_id == IWL_MVM_INVALID_STA) {
1292 /* if we're not associated, this must be netdetect */
1293 if (!wowlan->nd_config) {
1294 ret = 1;
1295 goto out_noreset;
1296 }
1297
1298 ret = iwl_mvm_netdetect_config(
1299 mvm, wowlan, wowlan->nd_config, vif);
1300 if (ret)
1301 goto out;
1302
1303 mvm->net_detect = true;
1304 } else {
1305 struct iwl_wowlan_config_cmd wowlan_config_cmd = {
1306 .offloading_tid = 0,
1307 };
1308
1309 wowlan_config_cmd.sta_id = mvm_link->ap_sta_id;
1310
1311 ap_sta = rcu_dereference_protected(
1312 mvm->fw_id_to_mac_id[mvm_link->ap_sta_id],
1313 lockdep_is_held(&mvm->mutex));
1314 if (IS_ERR_OR_NULL(ap_sta)) {
1315 ret = -EINVAL;
1316 goto out_noreset;
1317 }
1318
1319 ret = iwl_mvm_sta_ensure_queue(
1320 mvm, ap_sta->txq[wowlan_config_cmd.offloading_tid]);
1321 if (ret)
1322 goto out_noreset;
1323
1324 ret = iwl_mvm_get_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1325 vif, mvmvif, ap_sta);
1326 if (ret)
1327 goto out_noreset;
1328 ret = iwl_mvm_wowlan_config(mvm, wowlan, &wowlan_config_cmd,
1329 vif, mvmvif, mvm_link, ap_sta);
1330 if (ret)
1331 goto out;
1332
1333 mvm->net_detect = false;
1334 }
1335
1336 ret = iwl_mvm_power_update_device(mvm);
1337 if (ret)
1338 goto out;
1339
1340 ret = iwl_mvm_power_update_mac(mvm);
1341 if (ret)
1342 goto out;
1343
1344 #ifdef CONFIG_IWLWIFI_DEBUGFS
1345 if (mvm->d3_wake_sysassert)
1346 d3_cfg_cmd_data.wakeup_flags |=
1347 cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
1348 #endif
1349
1350 /*
1351 * Prior to 9000 device family the driver needs to stop the dbg
1352 * recording before entering D3. In later devices the FW stops the
1353 * recording automatically.
1354 */
1355 if (mvm->trans->trans_cfg->device_family < IWL_DEVICE_FAMILY_9000)
1356 iwl_fw_dbg_stop_restart_recording(&mvm->fwrt, NULL, true);
1357
1358 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_D3;
1359
1360 /* must be last -- this switches firmware state */
1361 ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
1362 if (ret)
1363 goto out;
1364 #ifdef CONFIG_IWLWIFI_DEBUGFS
1365 len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
1366 if (len >= sizeof(u32)) {
1367 mvm->d3_test_pme_ptr =
1368 le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
1369 }
1370 #endif
1371 iwl_free_resp(&d3_cfg_cmd);
1372
1373 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1374
1375 ret = iwl_trans_d3_suspend(mvm->trans, test, !unified_image);
1376 out:
1377 if (ret < 0) {
1378 iwl_mvm_free_nd(mvm);
1379
1380 if (!unified_image) {
1381 if (mvm->fw_restart > 0) {
1382 mvm->fw_restart--;
1383 ieee80211_restart_hw(mvm->hw);
1384 }
1385 }
1386
1387 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
1388 }
1389 out_noreset:
1390 mutex_unlock(&mvm->mutex);
1391
1392 return ret;
1393 }
1394
iwl_mvm_suspend(struct ieee80211_hw * hw,struct cfg80211_wowlan * wowlan)1395 int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
1396 {
1397 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1398
1399 iwl_mvm_pause_tcm(mvm, true);
1400
1401 iwl_fw_runtime_suspend(&mvm->fwrt);
1402
1403 return __iwl_mvm_suspend(hw, wowlan, false);
1404 }
1405
1406 struct iwl_multicast_key_data {
1407 u8 key[WOWLAN_KEY_MAX_SIZE];
1408 u8 len;
1409 u8 flags;
1410 u8 id;
1411 u8 ipn[6];
1412 };
1413
1414 /* converted data from the different status responses */
1415 struct iwl_wowlan_status_data {
1416 u64 replay_ctr;
1417 u32 num_of_gtk_rekeys;
1418 u32 received_beacons;
1419 u32 wakeup_reasons;
1420 u32 wake_packet_length;
1421 u32 wake_packet_bufsize;
1422 u16 pattern_number;
1423 u16 non_qos_seq_ctr;
1424 u16 qos_seq_ctr[8];
1425 u8 tid_tear_down;
1426
1427 struct {
1428 /* including RX MIC key for TKIP */
1429 u8 key[WOWLAN_KEY_MAX_SIZE];
1430 u8 len;
1431 u8 flags;
1432 u8 id;
1433 } gtk[WOWLAN_GTK_KEYS_NUM];
1434
1435 struct {
1436 /*
1437 * We store both the TKIP and AES representations
1438 * coming from the firmware because we decode the
1439 * data from there before we iterate the keys and
1440 * know which one we need.
1441 */
1442 struct {
1443 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1444 } tkip, aes;
1445
1446 /*
1447 * We use -1 for when we have valid data but don't know
1448 * the key ID from firmware, and thus it needs to be
1449 * installed with the last key (depending on rekeying).
1450 */
1451 s8 key_id;
1452 bool valid;
1453 } gtk_seq[2];
1454
1455 struct {
1456 /* Same as above */
1457 struct {
1458 struct ieee80211_key_seq seq[IWL_MAX_TID_COUNT];
1459 u64 tx_pn;
1460 } tkip, aes;
1461 } ptk;
1462
1463 struct iwl_multicast_key_data igtk;
1464 struct iwl_multicast_key_data bigtk[WOWLAN_BIGTK_KEYS_NUM];
1465
1466 int num_mlo_keys;
1467 struct iwl_wowlan_mlo_gtk mlo_keys[WOWLAN_MAX_MLO_KEYS];
1468
1469 u8 *wake_packet;
1470 };
1471
iwl_mvm_report_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)1472 static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
1473 struct ieee80211_vif *vif,
1474 struct iwl_wowlan_status_data *status)
1475 {
1476 struct sk_buff *pkt = NULL;
1477 struct cfg80211_wowlan_wakeup wakeup = {
1478 .pattern_idx = -1,
1479 };
1480 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
1481 u32 reasons = status->wakeup_reasons;
1482
1483 if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
1484 wakeup_report = NULL;
1485 goto report;
1486 }
1487
1488 pm_wakeup_event(mvm->dev, 0);
1489
1490 if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
1491 wakeup.magic_pkt = true;
1492
1493 if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
1494 wakeup.pattern_idx =
1495 status->pattern_number;
1496
1497 if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
1498 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH |
1499 IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE))
1500 wakeup.disconnect = true;
1501
1502 if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
1503 wakeup.gtk_rekey_failure = true;
1504
1505 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
1506 wakeup.rfkill_release = true;
1507
1508 if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
1509 wakeup.eap_identity_req = true;
1510
1511 if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
1512 wakeup.four_way_handshake = true;
1513
1514 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
1515 wakeup.tcp_connlost = true;
1516
1517 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
1518 wakeup.tcp_nomoretokens = true;
1519
1520 if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
1521 wakeup.tcp_match = true;
1522
1523 if (reasons & IWL_WAKEUP_BY_11W_UNPROTECTED_DEAUTH_OR_DISASSOC)
1524 wakeup.unprot_deauth_disassoc = true;
1525
1526 if (status->wake_packet) {
1527 int pktsize = status->wake_packet_bufsize;
1528 int pktlen = status->wake_packet_length;
1529 const u8 *pktdata = status->wake_packet;
1530 const struct ieee80211_hdr *hdr = (const void *)pktdata;
1531 int truncated = pktlen - pktsize;
1532
1533 /* this would be a firmware bug */
1534 if (WARN_ON_ONCE(truncated < 0))
1535 truncated = 0;
1536
1537 if (ieee80211_is_data(hdr->frame_control)) {
1538 int hdrlen = ieee80211_hdrlen(hdr->frame_control);
1539 int ivlen = 0, icvlen = 4; /* also FCS */
1540
1541 pkt = alloc_skb(pktsize, GFP_KERNEL);
1542 if (!pkt)
1543 goto report;
1544
1545 skb_put_data(pkt, pktdata, hdrlen);
1546 pktdata += hdrlen;
1547 pktsize -= hdrlen;
1548
1549 if (ieee80211_has_protected(hdr->frame_control)) {
1550 /*
1551 * This is unlocked and using gtk_i(c)vlen,
1552 * but since everything is under RTNL still
1553 * that's not really a problem - changing
1554 * it would be difficult.
1555 */
1556 if (is_multicast_ether_addr(hdr->addr1)) {
1557 ivlen = mvm->gtk_ivlen;
1558 icvlen += mvm->gtk_icvlen;
1559 } else {
1560 ivlen = mvm->ptk_ivlen;
1561 icvlen += mvm->ptk_icvlen;
1562 }
1563 }
1564
1565 /* if truncated, FCS/ICV is (partially) gone */
1566 if (truncated >= icvlen) {
1567 icvlen = 0;
1568 truncated -= icvlen;
1569 } else {
1570 icvlen -= truncated;
1571 truncated = 0;
1572 }
1573
1574 pktsize -= ivlen + icvlen;
1575 pktdata += ivlen;
1576
1577 skb_put_data(pkt, pktdata, pktsize);
1578
1579 if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
1580 goto report;
1581 wakeup.packet = pkt->data;
1582 wakeup.packet_present_len = pkt->len;
1583 wakeup.packet_len = pkt->len - truncated;
1584 wakeup.packet_80211 = false;
1585 } else {
1586 int fcslen = 4;
1587
1588 if (truncated >= 4) {
1589 truncated -= 4;
1590 fcslen = 0;
1591 } else {
1592 fcslen -= truncated;
1593 truncated = 0;
1594 }
1595 pktsize -= fcslen;
1596 wakeup.packet = status->wake_packet;
1597 wakeup.packet_present_len = pktsize;
1598 wakeup.packet_len = pktlen - truncated;
1599 wakeup.packet_80211 = true;
1600 }
1601 }
1602
1603 report:
1604 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
1605 kfree_skb(pkt);
1606 }
1607
iwl_mvm_le64_to_aes_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1608 static void iwl_mvm_le64_to_aes_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1609 {
1610 u64 pn = le64_to_cpu(le_pn);
1611
1612 seq->ccmp.pn[0] = pn >> 40;
1613 seq->ccmp.pn[1] = pn >> 32;
1614 seq->ccmp.pn[2] = pn >> 24;
1615 seq->ccmp.pn[3] = pn >> 16;
1616 seq->ccmp.pn[4] = pn >> 8;
1617 seq->ccmp.pn[5] = pn;
1618 }
1619
iwl_mvm_aes_sc_to_seq(struct aes_sc * sc,struct ieee80211_key_seq * seq)1620 static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
1621 struct ieee80211_key_seq *seq)
1622 {
1623 iwl_mvm_le64_to_aes_seq(sc->pn, seq);
1624 }
1625
iwl_mvm_le64_to_tkip_seq(__le64 le_pn,struct ieee80211_key_seq * seq)1626 static void iwl_mvm_le64_to_tkip_seq(__le64 le_pn, struct ieee80211_key_seq *seq)
1627 {
1628 u64 pn = le64_to_cpu(le_pn);
1629
1630 seq->tkip.iv16 = (u16)pn;
1631 seq->tkip.iv32 = (u32)(pn >> 16);
1632 }
1633
iwl_mvm_tkip_sc_to_seq(struct tkip_sc * sc,struct ieee80211_key_seq * seq)1634 static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
1635 struct ieee80211_key_seq *seq)
1636 {
1637 seq->tkip.iv32 = le32_to_cpu(sc->iv32);
1638 seq->tkip.iv16 = le16_to_cpu(sc->iv16);
1639 }
1640
iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf * key,struct ieee80211_key_seq * seq)1641 static void iwl_mvm_set_key_rx_seq_tids(struct ieee80211_key_conf *key,
1642 struct ieee80211_key_seq *seq)
1643 {
1644 int tid;
1645
1646 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
1647 ieee80211_set_key_rx_seq(key, tid, &seq[tid]);
1648 }
1649
iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm * mvm,struct iwl_wowlan_status_data * status,struct ieee80211_sta * sta,struct ieee80211_key_conf * key)1650 static void iwl_mvm_set_aes_ptk_rx_seq(struct iwl_mvm *mvm,
1651 struct iwl_wowlan_status_data *status,
1652 struct ieee80211_sta *sta,
1653 struct ieee80211_key_conf *key)
1654 {
1655 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
1656 struct iwl_mvm_key_pn *ptk_pn;
1657 int tid;
1658
1659 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.aes.seq);
1660
1661 if (!iwl_mvm_has_new_rx_api(mvm))
1662 return;
1663
1664
1665 rcu_read_lock();
1666 ptk_pn = rcu_dereference(mvmsta->ptk_pn[key->keyidx]);
1667 if (WARN_ON(!ptk_pn)) {
1668 rcu_read_unlock();
1669 return;
1670 }
1671
1672 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1673 int i;
1674
1675 for (i = 1; i < mvm->trans->num_rx_queues; i++)
1676 memcpy(ptk_pn->q[i].pn[tid],
1677 status->ptk.aes.seq[tid].ccmp.pn,
1678 IEEE80211_CCMP_PN_LEN);
1679 }
1680 rcu_read_unlock();
1681 }
1682
iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data * status,union iwl_all_tsc_rsc * sc)1683 static void iwl_mvm_convert_key_counters(struct iwl_wowlan_status_data *status,
1684 union iwl_all_tsc_rsc *sc)
1685 {
1686 int i;
1687
1688 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1689 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1690
1691 /* GTK RX counters */
1692 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1693 iwl_mvm_tkip_sc_to_seq(&sc->tkip.multicast_rsc[i],
1694 &status->gtk_seq[0].tkip.seq[i]);
1695 iwl_mvm_aes_sc_to_seq(&sc->aes.multicast_rsc[i],
1696 &status->gtk_seq[0].aes.seq[i]);
1697 }
1698 status->gtk_seq[0].valid = true;
1699 status->gtk_seq[0].key_id = -1;
1700
1701 /* PTK TX counter */
1702 status->ptk.tkip.tx_pn = (u64)le16_to_cpu(sc->tkip.tsc.iv16) |
1703 ((u64)le32_to_cpu(sc->tkip.tsc.iv32) << 16);
1704 status->ptk.aes.tx_pn = le64_to_cpu(sc->aes.tsc.pn);
1705
1706 /* PTK RX counters */
1707 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
1708 iwl_mvm_tkip_sc_to_seq(&sc->tkip.unicast_rsc[i],
1709 &status->ptk.tkip.seq[i]);
1710 iwl_mvm_aes_sc_to_seq(&sc->aes.unicast_rsc[i],
1711 &status->ptk.aes.seq[i]);
1712 }
1713 }
1714
1715 static void
iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data * status,struct iwl_wowlan_all_rsc_tsc_v5 * sc,unsigned int idx,unsigned int key_id)1716 iwl_mvm_convert_key_counters_v5_gtk_seq(struct iwl_wowlan_status_data *status,
1717 struct iwl_wowlan_all_rsc_tsc_v5 *sc,
1718 unsigned int idx, unsigned int key_id)
1719 {
1720 int tid;
1721
1722 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1723 iwl_mvm_le64_to_tkip_seq(sc->mcast_rsc[idx][tid],
1724 &status->gtk_seq[idx].tkip.seq[tid]);
1725 iwl_mvm_le64_to_aes_seq(sc->mcast_rsc[idx][tid],
1726 &status->gtk_seq[idx].aes.seq[tid]);
1727 }
1728
1729 status->gtk_seq[idx].valid = true;
1730 status->gtk_seq[idx].key_id = key_id;
1731 }
1732
1733 static void
iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data * status,struct iwl_wowlan_all_rsc_tsc_v5 * sc)1734 iwl_mvm_convert_key_counters_v5(struct iwl_wowlan_status_data *status,
1735 struct iwl_wowlan_all_rsc_tsc_v5 *sc)
1736 {
1737 int i, tid;
1738
1739 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_MAX_TID_COUNT);
1740 BUILD_BUG_ON(IWL_MAX_TID_COUNT > IWL_NUM_RSC);
1741 BUILD_BUG_ON(ARRAY_SIZE(sc->mcast_rsc) != ARRAY_SIZE(status->gtk_seq));
1742
1743 /* GTK RX counters */
1744 for (i = 0; i < ARRAY_SIZE(sc->mcast_key_id_map); i++) {
1745 u8 entry = sc->mcast_key_id_map[i];
1746
1747 if (entry < ARRAY_SIZE(sc->mcast_rsc))
1748 iwl_mvm_convert_key_counters_v5_gtk_seq(status, sc,
1749 entry, i);
1750 }
1751
1752 /* PTK TX counters not needed, assigned in device */
1753
1754 /* PTK RX counters */
1755 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
1756 iwl_mvm_le64_to_tkip_seq(sc->ucast_rsc[tid],
1757 &status->ptk.tkip.seq[tid]);
1758 iwl_mvm_le64_to_aes_seq(sc->ucast_rsc[tid],
1759 &status->ptk.aes.seq[tid]);
1760 }
1761 }
1762
iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status,int idx)1763 static void iwl_mvm_set_key_rx_seq_idx(struct ieee80211_key_conf *key,
1764 struct iwl_wowlan_status_data *status,
1765 int idx)
1766 {
1767 switch (key->cipher) {
1768 case WLAN_CIPHER_SUITE_CCMP:
1769 case WLAN_CIPHER_SUITE_GCMP:
1770 case WLAN_CIPHER_SUITE_GCMP_256:
1771 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].aes.seq);
1772 break;
1773 case WLAN_CIPHER_SUITE_TKIP:
1774 iwl_mvm_set_key_rx_seq_tids(key, status->gtk_seq[idx].tkip.seq);
1775 break;
1776 default:
1777 WARN_ON(1);
1778 }
1779 }
1780
iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf * key,struct iwl_wowlan_status_data * status,bool installed)1781 static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
1782 struct iwl_wowlan_status_data *status,
1783 bool installed)
1784 {
1785 int i;
1786
1787 for (i = 0; i < ARRAY_SIZE(status->gtk_seq); i++) {
1788 if (!status->gtk_seq[i].valid)
1789 continue;
1790
1791 /* Handle the case where we know the key ID */
1792 if (status->gtk_seq[i].key_id == key->keyidx) {
1793 s8 new_key_id = -1;
1794
1795 if (status->num_of_gtk_rekeys)
1796 new_key_id = status->gtk[0].flags &
1797 IWL_WOWLAN_GTK_IDX_MASK;
1798
1799 /* Don't install a new key's value to an old key */
1800 if (new_key_id != key->keyidx)
1801 iwl_mvm_set_key_rx_seq_idx(key, status, i);
1802 continue;
1803 }
1804
1805 /* handle the case where we didn't, last key only */
1806 if (status->gtk_seq[i].key_id == -1 &&
1807 (!status->num_of_gtk_rekeys || installed))
1808 iwl_mvm_set_key_rx_seq_idx(key, status, i);
1809 }
1810 }
1811
1812 struct iwl_mvm_d3_gtk_iter_data {
1813 struct iwl_mvm *mvm;
1814 struct iwl_wowlan_status_data *status;
1815 u32 gtk_cipher, igtk_cipher, bigtk_cipher;
1816 bool unhandled_cipher, igtk_support, bigtk_support;
1817 int num_keys;
1818 };
1819
iwl_mvm_d3_find_last_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1820 static void iwl_mvm_d3_find_last_keys(struct ieee80211_hw *hw,
1821 struct ieee80211_vif *vif,
1822 struct ieee80211_sta *sta,
1823 struct ieee80211_key_conf *key,
1824 void *_data)
1825 {
1826 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1827 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1828
1829 if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1830 return;
1831
1832 if (data->unhandled_cipher)
1833 return;
1834
1835 switch (key->cipher) {
1836 case WLAN_CIPHER_SUITE_WEP40:
1837 case WLAN_CIPHER_SUITE_WEP104:
1838 /* ignore WEP completely, nothing to do */
1839 return;
1840 case WLAN_CIPHER_SUITE_CCMP:
1841 case WLAN_CIPHER_SUITE_GCMP:
1842 case WLAN_CIPHER_SUITE_GCMP_256:
1843 case WLAN_CIPHER_SUITE_TKIP:
1844 /* we support these */
1845 data->gtk_cipher = key->cipher;
1846 break;
1847 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1848 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1849 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1850 case WLAN_CIPHER_SUITE_AES_CMAC:
1851 /* we support these */
1852 if (data->igtk_support &&
1853 (key->keyidx == 4 || key->keyidx == 5)) {
1854 data->igtk_cipher = key->cipher;
1855 } else if (data->bigtk_support &&
1856 (key->keyidx == 6 || key->keyidx == 7)) {
1857 data->bigtk_cipher = key->cipher;
1858 } else {
1859 data->unhandled_cipher = true;
1860 return;
1861 }
1862 break;
1863 default:
1864 /* everything else - disconnect from AP */
1865 data->unhandled_cipher = true;
1866 return;
1867 }
1868
1869 data->num_keys++;
1870 }
1871
1872 static void
iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data * key,struct ieee80211_key_seq * seq,u32 cipher)1873 iwl_mvm_d3_set_igtk_bigtk_ipn(const struct iwl_multicast_key_data *key,
1874 struct ieee80211_key_seq *seq, u32 cipher)
1875 {
1876 switch (cipher) {
1877 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1878 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1879 BUILD_BUG_ON(sizeof(seq->aes_gmac.pn) != sizeof(key->ipn));
1880 memcpy(seq->aes_gmac.pn, key->ipn, sizeof(seq->aes_gmac.pn));
1881 break;
1882 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1883 case WLAN_CIPHER_SUITE_AES_CMAC:
1884 BUILD_BUG_ON(sizeof(seq->aes_cmac.pn) != sizeof(key->ipn));
1885 memcpy(seq->aes_cmac.pn, key->ipn, sizeof(seq->aes_cmac.pn));
1886 break;
1887 default:
1888 WARN_ON(1);
1889 }
1890 }
1891
1892 static void
iwl_mvm_d3_update_igtk_bigtk(struct iwl_wowlan_status_data * status,struct ieee80211_key_conf * key,struct iwl_multicast_key_data * key_data)1893 iwl_mvm_d3_update_igtk_bigtk(struct iwl_wowlan_status_data *status,
1894 struct ieee80211_key_conf *key,
1895 struct iwl_multicast_key_data *key_data)
1896 {
1897 if (status->num_of_gtk_rekeys && key_data->len) {
1898 /* remove rekeyed key */
1899 ieee80211_remove_key(key);
1900 } else {
1901 struct ieee80211_key_seq seq;
1902
1903 iwl_mvm_d3_set_igtk_bigtk_ipn(key_data,
1904 &seq,
1905 key->cipher);
1906 ieee80211_set_key_rx_seq(key, 0, &seq);
1907 }
1908 }
1909
iwl_mvm_d3_update_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * _data)1910 static void iwl_mvm_d3_update_keys(struct ieee80211_hw *hw,
1911 struct ieee80211_vif *vif,
1912 struct ieee80211_sta *sta,
1913 struct ieee80211_key_conf *key,
1914 void *_data)
1915 {
1916 struct iwl_mvm_d3_gtk_iter_data *data = _data;
1917 struct iwl_wowlan_status_data *status = data->status;
1918 s8 keyidx;
1919 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
1920
1921 if (link_id >= 0 && key->link_id >= 0 && link_id != key->link_id)
1922 return;
1923
1924 if (data->unhandled_cipher)
1925 return;
1926
1927 switch (key->cipher) {
1928 case WLAN_CIPHER_SUITE_WEP40:
1929 case WLAN_CIPHER_SUITE_WEP104:
1930 /* ignore WEP completely, nothing to do */
1931 return;
1932 case WLAN_CIPHER_SUITE_CCMP:
1933 case WLAN_CIPHER_SUITE_GCMP:
1934 case WLAN_CIPHER_SUITE_GCMP_256:
1935 if (sta) {
1936 atomic64_set(&key->tx_pn, status->ptk.aes.tx_pn);
1937 iwl_mvm_set_aes_ptk_rx_seq(data->mvm, status, sta, key);
1938 return;
1939 }
1940 fallthrough;
1941 case WLAN_CIPHER_SUITE_TKIP:
1942 if (sta) {
1943 atomic64_set(&key->tx_pn, status->ptk.tkip.tx_pn);
1944 iwl_mvm_set_key_rx_seq_tids(key, status->ptk.tkip.seq);
1945 return;
1946 }
1947 keyidx = key->keyidx;
1948 /* The current key is always sent by the FW, even if it wasn't
1949 * rekeyed during D3.
1950 * We remove an existing key if it has the same index as
1951 * a new key
1952 */
1953 if (status->num_of_gtk_rekeys &&
1954 ((status->gtk[0].len && keyidx == status->gtk[0].id) ||
1955 (status->gtk[1].len && keyidx == status->gtk[1].id))) {
1956 ieee80211_remove_key(key);
1957 } else {
1958 iwl_mvm_set_key_rx_seq(key, data->status, false);
1959 }
1960 break;
1961 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1962 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1963 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1964 case WLAN_CIPHER_SUITE_AES_CMAC:
1965 if (key->keyidx == 4 || key->keyidx == 5) {
1966 iwl_mvm_d3_update_igtk_bigtk(status, key,
1967 &status->igtk);
1968 }
1969 if (key->keyidx == 6 || key->keyidx == 7) {
1970 u8 idx = key->keyidx == status->bigtk[1].id;
1971
1972 iwl_mvm_d3_update_igtk_bigtk(status, key,
1973 &status->bigtk[idx]);
1974 }
1975 }
1976 }
1977
1978 struct iwl_mvm_d3_mlo_old_keys {
1979 u32 cipher[IEEE80211_MLD_MAX_NUM_LINKS][WOWLAN_MLO_GTK_KEY_NUM_TYPES];
1980 struct ieee80211_key_conf *key[IEEE80211_MLD_MAX_NUM_LINKS][8];
1981 };
1982
iwl_mvm_mlo_key_ciphers(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_key_conf * key,void * data)1983 static void iwl_mvm_mlo_key_ciphers(struct ieee80211_hw *hw,
1984 struct ieee80211_vif *vif,
1985 struct ieee80211_sta *sta,
1986 struct ieee80211_key_conf *key,
1987 void *data)
1988 {
1989 struct iwl_mvm_d3_mlo_old_keys *old_keys = data;
1990 enum iwl_wowlan_mlo_gtk_type key_type;
1991
1992 if (key->link_id < 0)
1993 return;
1994
1995 if (WARN_ON(key->link_id >= IEEE80211_MLD_MAX_NUM_LINKS ||
1996 key->keyidx >= 8))
1997 return;
1998
1999 if (WARN_ON(old_keys->key[key->link_id][key->keyidx]))
2000 return;
2001
2002 switch (key->cipher) {
2003 case WLAN_CIPHER_SUITE_CCMP:
2004 case WLAN_CIPHER_SUITE_GCMP:
2005 case WLAN_CIPHER_SUITE_GCMP_256:
2006 key_type = WOWLAN_MLO_GTK_KEY_TYPE_GTK;
2007 break;
2008 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2009 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2010 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2011 case WLAN_CIPHER_SUITE_AES_CMAC:
2012 if (key->keyidx == 4 || key->keyidx == 5) {
2013 key_type = WOWLAN_MLO_GTK_KEY_TYPE_IGTK;
2014 break;
2015 } else if (key->keyidx == 6 || key->keyidx == 7) {
2016 key_type = WOWLAN_MLO_GTK_KEY_TYPE_BIGTK;
2017 break;
2018 }
2019 return;
2020 default:
2021 /* ignore WEP/TKIP or unknown ciphers */
2022 return;
2023 }
2024
2025 old_keys->cipher[key->link_id][key_type] = key->cipher;
2026 old_keys->key[key->link_id][key->keyidx] = key;
2027 }
2028
iwl_mvm_mlo_gtk_rekey(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,struct iwl_mvm * mvm)2029 static bool iwl_mvm_mlo_gtk_rekey(struct iwl_wowlan_status_data *status,
2030 struct ieee80211_vif *vif,
2031 struct iwl_mvm *mvm)
2032 {
2033 int i;
2034 struct iwl_mvm_d3_mlo_old_keys *old_keys;
2035 bool ret = true;
2036
2037 IWL_DEBUG_WOWLAN(mvm, "Num of MLO Keys: %d\n", status->num_mlo_keys);
2038 if (!status->num_mlo_keys)
2039 return true;
2040
2041 old_keys = kzalloc(sizeof(*old_keys), GFP_KERNEL);
2042 if (!old_keys)
2043 return false;
2044
2045 /* find the cipher for each mlo key */
2046 ieee80211_iter_keys(mvm->hw, vif, iwl_mvm_mlo_key_ciphers, old_keys);
2047
2048 for (i = 0; i < status->num_mlo_keys; i++) {
2049 struct iwl_wowlan_mlo_gtk *mlo_key = &status->mlo_keys[i];
2050 struct ieee80211_key_conf *key, *old_key;
2051 struct ieee80211_key_seq seq;
2052 struct {
2053 struct ieee80211_key_conf conf;
2054 u8 key[32];
2055 } conf = {};
2056 u16 flags = le16_to_cpu(mlo_key->flags);
2057 int j, link_id, key_id, key_type;
2058
2059 link_id = u16_get_bits(flags, WOWLAN_MLO_GTK_FLAG_LINK_ID_MSK);
2060 key_id = u16_get_bits(flags, WOWLAN_MLO_GTK_FLAG_KEY_ID_MSK);
2061 key_type = u16_get_bits(flags,
2062 WOWLAN_MLO_GTK_FLAG_KEY_TYPE_MSK);
2063
2064 if (!(vif->valid_links & BIT(link_id)))
2065 continue;
2066
2067 if (WARN_ON(link_id >= IEEE80211_MLD_MAX_NUM_LINKS ||
2068 key_id >= 8 ||
2069 key_type >= WOWLAN_MLO_GTK_KEY_NUM_TYPES))
2070 continue;
2071
2072 conf.conf.cipher = old_keys->cipher[link_id][key_type];
2073 /* WARN_ON? */
2074 if (!conf.conf.cipher)
2075 continue;
2076
2077 conf.conf.keylen = 0;
2078 switch (conf.conf.cipher) {
2079 case WLAN_CIPHER_SUITE_CCMP:
2080 case WLAN_CIPHER_SUITE_GCMP:
2081 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
2082 break;
2083 case WLAN_CIPHER_SUITE_GCMP_256:
2084 conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
2085 break;
2086 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2087 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2088 break;
2089 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2090 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2091 break;
2092 case WLAN_CIPHER_SUITE_AES_CMAC:
2093 conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
2094 break;
2095 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2096 conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2097 break;
2098 }
2099
2100 if (WARN_ON(!conf.conf.keylen ||
2101 conf.conf.keylen > sizeof(conf.key)))
2102 continue;
2103
2104 memcpy(conf.conf.key, mlo_key->key, conf.conf.keylen);
2105 conf.conf.keyidx = key_id;
2106
2107 old_key = old_keys->key[link_id][key_id];
2108 if (old_key) {
2109 IWL_DEBUG_WOWLAN(mvm,
2110 "Remove MLO key id %d, link id %d\n",
2111 key_id, link_id);
2112 ieee80211_remove_key(old_key);
2113 }
2114
2115 IWL_DEBUG_WOWLAN(mvm, "Add MLO key id %d, link id %d\n",
2116 key_id, link_id);
2117 key = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
2118 if (WARN_ON(IS_ERR(key))) {
2119 ret = false;
2120 goto out;
2121 }
2122
2123 /*
2124 * mac80211 expects the pn in big-endian
2125 * also note that seq is a union of all cipher types
2126 * (ccmp, gcmp, cmac, gmac), and they all have the same
2127 * pn field (of length 6) so just copy it to ccmp.pn.
2128 */
2129 for (j = 5; j >= 0; j--)
2130 seq.ccmp.pn[5 - j] = mlo_key->pn[j];
2131
2132 /* group keys are non-QoS and use TID 0 */
2133 ieee80211_set_key_rx_seq(key, 0, &seq);
2134 }
2135
2136 out:
2137 kfree(old_keys);
2138 return ret;
2139 }
2140
iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,struct iwl_mvm * mvm,u32 gtk_cipher)2141 static bool iwl_mvm_gtk_rekey(struct iwl_wowlan_status_data *status,
2142 struct ieee80211_vif *vif,
2143 struct iwl_mvm *mvm, u32 gtk_cipher)
2144 {
2145 int i, j;
2146 struct ieee80211_key_conf *key;
2147 struct {
2148 struct ieee80211_key_conf conf;
2149 u8 key[32];
2150 } conf = {
2151 .conf.cipher = gtk_cipher,
2152 };
2153 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
2154
2155 BUILD_BUG_ON(WLAN_KEY_LEN_CCMP != WLAN_KEY_LEN_GCMP);
2156 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_CCMP);
2157 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_GCMP_256);
2158 BUILD_BUG_ON(sizeof(conf.key) < WLAN_KEY_LEN_TKIP);
2159 BUILD_BUG_ON(sizeof(conf.key) < sizeof(status->gtk[0].key));
2160
2161 switch (gtk_cipher) {
2162 case WLAN_CIPHER_SUITE_CCMP:
2163 case WLAN_CIPHER_SUITE_GCMP:
2164 conf.conf.keylen = WLAN_KEY_LEN_CCMP;
2165 break;
2166 case WLAN_CIPHER_SUITE_GCMP_256:
2167 conf.conf.keylen = WLAN_KEY_LEN_GCMP_256;
2168 break;
2169 case WLAN_CIPHER_SUITE_TKIP:
2170 conf.conf.keylen = WLAN_KEY_LEN_TKIP;
2171 break;
2172 default:
2173 WARN_ON(1);
2174 }
2175
2176 for (i = 0; i < ARRAY_SIZE(status->gtk); i++) {
2177 if (!status->gtk[i].len)
2178 continue;
2179
2180 conf.conf.keyidx = status->gtk[i].id;
2181 IWL_DEBUG_WOWLAN(mvm,
2182 "Received from FW GTK cipher %d, key index %d\n",
2183 conf.conf.cipher, conf.conf.keyidx);
2184 memcpy(conf.conf.key, status->gtk[i].key,
2185 sizeof(status->gtk[i].key));
2186
2187 key = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
2188 if (IS_ERR(key))
2189 return false;
2190
2191 for (j = 0; j < ARRAY_SIZE(status->gtk_seq); j++) {
2192 if (!status->gtk_seq[j].valid ||
2193 status->gtk_seq[j].key_id != key->keyidx)
2194 continue;
2195 iwl_mvm_set_key_rx_seq_idx(key, status, j);
2196 break;
2197 }
2198 WARN_ON(j == ARRAY_SIZE(status->gtk_seq));
2199 }
2200
2201 return true;
2202 }
2203
2204 static bool
iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data * status,struct ieee80211_vif * vif,u32 cipher,struct iwl_multicast_key_data * key_data)2205 iwl_mvm_d3_igtk_bigtk_rekey_add(struct iwl_wowlan_status_data *status,
2206 struct ieee80211_vif *vif, u32 cipher,
2207 struct iwl_multicast_key_data *key_data)
2208 {
2209 struct ieee80211_key_conf *key_config;
2210 struct {
2211 struct ieee80211_key_conf conf;
2212 u8 key[WOWLAN_KEY_MAX_SIZE];
2213 } conf = {
2214 .conf.cipher = cipher,
2215 .conf.keyidx = key_data->id,
2216 };
2217 struct ieee80211_key_seq seq;
2218 int link_id = vif->active_links ? __ffs(vif->active_links) : -1;
2219
2220 if (!key_data->len)
2221 return true;
2222
2223 iwl_mvm_d3_set_igtk_bigtk_ipn(key_data, &seq, conf.conf.cipher);
2224
2225 switch (cipher) {
2226 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
2227 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_128;
2228 break;
2229 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
2230 conf.conf.keylen = WLAN_KEY_LEN_BIP_GMAC_256;
2231 break;
2232 case WLAN_CIPHER_SUITE_AES_CMAC:
2233 conf.conf.keylen = WLAN_KEY_LEN_AES_CMAC;
2234 break;
2235 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
2236 conf.conf.keylen = WLAN_KEY_LEN_BIP_CMAC_256;
2237 break;
2238 default:
2239 WARN_ON(1);
2240 }
2241 BUILD_BUG_ON(sizeof(conf.key) < sizeof(key_data->key));
2242 memcpy(conf.conf.key, key_data->key, conf.conf.keylen);
2243
2244 key_config = ieee80211_gtk_rekey_add(vif, &conf.conf, link_id);
2245 if (IS_ERR(key_config))
2246 return false;
2247 ieee80211_set_key_rx_seq(key_config, 0, &seq);
2248
2249 if (key_config->keyidx == 4 || key_config->keyidx == 5) {
2250 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2251 struct iwl_mvm_vif_link_info *mvm_link;
2252
2253 link_id = link_id < 0 ? 0 : link_id;
2254 mvm_link = mvmvif->link[link_id];
2255 mvm_link->igtk = key_config;
2256 }
2257
2258 return true;
2259 }
2260
iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm * mvm)2261 static int iwl_mvm_lookup_wowlan_status_ver(struct iwl_mvm *mvm)
2262 {
2263 u8 notif_ver;
2264
2265 if (!fw_has_api(&mvm->fw->ucode_capa,
2266 IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL))
2267 return 6;
2268
2269 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2270 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2271 WOWLAN_GET_STATUSES, 0);
2272 if (!notif_ver)
2273 notif_ver = iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
2274 WOWLAN_GET_STATUSES, 7);
2275
2276 return notif_ver;
2277 }
2278
iwl_mvm_setup_connection_keep(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2279 static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
2280 struct ieee80211_vif *vif,
2281 struct iwl_wowlan_status_data *status)
2282 {
2283 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2284 struct iwl_mvm_d3_gtk_iter_data gtkdata = {
2285 .mvm = mvm,
2286 .status = status,
2287 };
2288 int i;
2289 u32 disconnection_reasons =
2290 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
2291 IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
2292
2293 if (!status || !vif->bss_conf.bssid)
2294 return false;
2295
2296 if (iwl_mvm_lookup_wowlan_status_ver(mvm) > 6 ||
2297 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2298 WOWLAN_INFO_NOTIFICATION,
2299 0))
2300 gtkdata.igtk_support = true;
2301
2302 if (iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2303 WOWLAN_INFO_NOTIFICATION,
2304 0) >= 3)
2305 gtkdata.bigtk_support = true;
2306
2307 /* find last GTK that we used initially, if any */
2308 ieee80211_iter_keys(mvm->hw, vif,
2309 iwl_mvm_d3_find_last_keys, >kdata);
2310 /* not trying to keep connections with MFP/unhandled ciphers */
2311 if (gtkdata.unhandled_cipher)
2312 return false;
2313 if (!gtkdata.num_keys)
2314 goto out;
2315
2316 /*
2317 * invalidate all other GTKs that might still exist and update
2318 * the one that we used
2319 */
2320 ieee80211_iter_keys(mvm->hw, vif,
2321 iwl_mvm_d3_update_keys, >kdata);
2322
2323 if (status->num_of_gtk_rekeys) {
2324 __be64 replay_ctr = cpu_to_be64(status->replay_ctr);
2325
2326 IWL_DEBUG_WOWLAN(mvm, "num of GTK rekeying %d\n",
2327 status->num_of_gtk_rekeys);
2328
2329 if (!iwl_mvm_gtk_rekey(status, vif, mvm, gtkdata.gtk_cipher))
2330 return false;
2331
2332 if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2333 gtkdata.igtk_cipher,
2334 &status->igtk))
2335 return false;
2336
2337 for (i = 0; i < ARRAY_SIZE(status->bigtk); i++) {
2338 if (!iwl_mvm_d3_igtk_bigtk_rekey_add(status, vif,
2339 gtkdata.bigtk_cipher,
2340 &status->bigtk[i]))
2341 return false;
2342 }
2343
2344 if (!iwl_mvm_mlo_gtk_rekey(status, vif, mvm))
2345 return false;
2346
2347 ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
2348 (void *)&replay_ctr, GFP_KERNEL);
2349 }
2350
2351 out:
2352 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP,
2353 WOWLAN_GET_STATUSES,
2354 IWL_FW_CMD_VER_UNKNOWN) < 10) {
2355 mvmvif->seqno_valid = true;
2356 /* +0x10 because the set API expects next-to-use, not last-used */
2357 mvmvif->seqno = status->non_qos_seq_ctr + 0x10;
2358 }
2359
2360 if (status->wakeup_reasons & disconnection_reasons)
2361 return false;
2362
2363 return true;
2364 }
2365
iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v2 * data)2366 static void iwl_mvm_convert_gtk_v2(struct iwl_wowlan_status_data *status,
2367 struct iwl_wowlan_gtk_status_v2 *data)
2368 {
2369 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data->key));
2370 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2371 sizeof(data->tkip_mic_key) >
2372 sizeof(status->gtk[0].key));
2373
2374 status->gtk[0].len = data->key_len;
2375 status->gtk[0].flags = data->key_flags;
2376
2377 memcpy(status->gtk[0].key, data->key, sizeof(data->key));
2378
2379 /* if it's as long as the TKIP encryption key, copy MIC key */
2380 if (status->gtk[0].len == NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2381 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2382 data->tkip_mic_key, sizeof(data->tkip_mic_key));
2383 }
2384
iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data * status,struct iwl_wowlan_gtk_status_v3 * data)2385 static void iwl_mvm_convert_gtk_v3(struct iwl_wowlan_status_data *status,
2386 struct iwl_wowlan_gtk_status_v3 *data)
2387 {
2388 int data_idx, status_idx = 0;
2389
2390 BUILD_BUG_ON(sizeof(status->gtk[0].key) < sizeof(data[0].key));
2391 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2392 sizeof(data[0].tkip_mic_key) >
2393 sizeof(status->gtk[0].key));
2394 BUILD_BUG_ON(ARRAY_SIZE(status->gtk) < WOWLAN_GTK_KEYS_NUM);
2395 for (data_idx = 0; data_idx < ARRAY_SIZE(status->gtk); data_idx++) {
2396 if (!(data[data_idx].key_len))
2397 continue;
2398 status->gtk[status_idx].len = data[data_idx].key_len;
2399 status->gtk[status_idx].flags = data[data_idx].key_flags;
2400 status->gtk[status_idx].id = status->gtk[status_idx].flags &
2401 IWL_WOWLAN_GTK_IDX_MASK;
2402
2403 memcpy(status->gtk[status_idx].key, data[data_idx].key,
2404 sizeof(data[data_idx].key));
2405
2406 /* if it's as long as the TKIP encryption key, copy MIC key */
2407 if (status->gtk[status_idx].len ==
2408 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY)
2409 memcpy(status->gtk[status_idx].key +
2410 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2411 data[data_idx].tkip_mic_key,
2412 sizeof(data[data_idx].tkip_mic_key));
2413 status_idx++;
2414 }
2415 }
2416
iwl_mvm_convert_igtk(struct iwl_wowlan_status_data * status,struct iwl_wowlan_igtk_status * data)2417 static void iwl_mvm_convert_igtk(struct iwl_wowlan_status_data *status,
2418 struct iwl_wowlan_igtk_status *data)
2419 {
2420 int i;
2421
2422 BUILD_BUG_ON(sizeof(status->igtk.key) < sizeof(data->key));
2423 BUILD_BUG_ON(sizeof(status->igtk.ipn) != sizeof(data->ipn));
2424
2425 if (!data->key_len)
2426 return;
2427
2428 status->igtk.len = data->key_len;
2429 status->igtk.flags = data->key_flags;
2430 status->igtk.id = u32_get_bits(data->key_flags,
2431 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2432 + WOWLAN_IGTK_MIN_INDEX;
2433
2434 memcpy(status->igtk.key, data->key, sizeof(data->key));
2435
2436 /* mac80211 expects big endian for memcmp() to work, convert */
2437 for (i = 0; i < sizeof(data->ipn); i++)
2438 status->igtk.ipn[i] = data->ipn[sizeof(data->ipn) - i - 1];
2439 }
2440
iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data * status,const struct iwl_wowlan_igtk_status * data)2441 static void iwl_mvm_convert_bigtk(struct iwl_wowlan_status_data *status,
2442 const struct iwl_wowlan_igtk_status *data)
2443 {
2444 int data_idx, status_idx = 0;
2445
2446 BUILD_BUG_ON(ARRAY_SIZE(status->bigtk) < WOWLAN_BIGTK_KEYS_NUM);
2447
2448 for (data_idx = 0; data_idx < WOWLAN_BIGTK_KEYS_NUM; data_idx++) {
2449 if (!data[data_idx].key_len)
2450 continue;
2451
2452 status->bigtk[status_idx].len = data[data_idx].key_len;
2453 status->bigtk[status_idx].flags = data[data_idx].key_flags;
2454 status->bigtk[status_idx].id =
2455 u32_get_bits(data[data_idx].key_flags,
2456 IWL_WOWLAN_IGTK_BIGTK_IDX_MASK)
2457 + WOWLAN_BIGTK_MIN_INDEX;
2458
2459 BUILD_BUG_ON(sizeof(status->bigtk[status_idx].key) <
2460 sizeof(data[data_idx].key));
2461 BUILD_BUG_ON(sizeof(status->bigtk[status_idx].ipn) <
2462 sizeof(data[data_idx].ipn));
2463
2464 memcpy(status->bigtk[status_idx].key, data[data_idx].key,
2465 sizeof(data[data_idx].key));
2466 memcpy(status->bigtk[status_idx].ipn, data[data_idx].ipn,
2467 sizeof(data[data_idx].ipn));
2468 status_idx++;
2469 }
2470 }
2471
iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif * data,struct iwl_wowlan_status_data * status,u32 len,bool has_mlo_keys)2472 static void iwl_mvm_parse_wowlan_info_notif(struct iwl_mvm *mvm,
2473 struct iwl_wowlan_info_notif *data,
2474 struct iwl_wowlan_status_data *status,
2475 u32 len, bool has_mlo_keys)
2476 {
2477 u32 i;
2478 u32 expected_len = sizeof(*data);
2479
2480 if (!data) {
2481 IWL_ERR(mvm, "iwl_wowlan_info_notif data is NULL\n");
2482 status = NULL;
2483 return;
2484 }
2485
2486 if (has_mlo_keys)
2487 expected_len += (data->num_mlo_link_keys *
2488 sizeof(status->mlo_keys[0]));
2489
2490 if (len < expected_len) {
2491 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2492 status = NULL;
2493 return;
2494 }
2495
2496 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2497 iwl_mvm_convert_gtk_v3(status, data->gtk);
2498 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2499 iwl_mvm_convert_bigtk(status, data->bigtk);
2500 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2501 status->pattern_number = le16_to_cpu(data->pattern_number);
2502 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2503 status->qos_seq_ctr[i] =
2504 le16_to_cpu(data->qos_seq_ctr[i]);
2505 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2506 status->num_of_gtk_rekeys =
2507 le32_to_cpu(data->num_of_gtk_rekeys);
2508 status->received_beacons = le32_to_cpu(data->received_beacons);
2509 status->tid_tear_down = data->tid_tear_down;
2510
2511 if (has_mlo_keys && data->num_mlo_link_keys) {
2512 status->num_mlo_keys = data->num_mlo_link_keys;
2513 if (IWL_FW_CHECK(mvm,
2514 status->num_mlo_keys > WOWLAN_MAX_MLO_KEYS,
2515 "Too many mlo keys: %d, max %d\n",
2516 status->num_mlo_keys, WOWLAN_MAX_MLO_KEYS))
2517 status->num_mlo_keys = WOWLAN_MAX_MLO_KEYS;
2518 memcpy(status->mlo_keys, data->mlo_gtks,
2519 status->num_mlo_keys * sizeof(status->mlo_keys[0]));
2520 }
2521 }
2522
2523 static void
iwl_mvm_parse_wowlan_info_notif_v2(struct iwl_mvm * mvm,struct iwl_wowlan_info_notif_v2 * data,struct iwl_wowlan_status_data * status,u32 len)2524 iwl_mvm_parse_wowlan_info_notif_v2(struct iwl_mvm *mvm,
2525 struct iwl_wowlan_info_notif_v2 *data,
2526 struct iwl_wowlan_status_data *status,
2527 u32 len)
2528 {
2529 u32 i;
2530
2531 if (!data) {
2532 IWL_ERR(mvm, "iwl_wowlan_info_notif data is NULL\n");
2533 status = NULL;
2534 return;
2535 }
2536
2537 if (len < sizeof(*data)) {
2538 IWL_ERR(mvm, "Invalid WoWLAN info notification!\n");
2539 status = NULL;
2540 return;
2541 }
2542
2543 iwl_mvm_convert_key_counters_v5(status, &data->gtk[0].sc);
2544 iwl_mvm_convert_gtk_v3(status, data->gtk);
2545 iwl_mvm_convert_igtk(status, &data->igtk[0]);
2546 status->replay_ctr = le64_to_cpu(data->replay_ctr);
2547 status->pattern_number = le16_to_cpu(data->pattern_number);
2548 for (i = 0; i < IWL_MAX_TID_COUNT; i++)
2549 status->qos_seq_ctr[i] =
2550 le16_to_cpu(data->qos_seq_ctr[i]);
2551 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons);
2552 status->num_of_gtk_rekeys =
2553 le32_to_cpu(data->num_of_gtk_rekeys);
2554 status->received_beacons = le32_to_cpu(data->received_beacons);
2555 status->tid_tear_down = data->tid_tear_down;
2556 }
2557
2558 /* Occasionally, templates would be nice. This is one of those times ... */
2559 #define iwl_mvm_parse_wowlan_status_common(_ver) \
2560 static struct iwl_wowlan_status_data * \
2561 iwl_mvm_parse_wowlan_status_common_ ## _ver(struct iwl_mvm *mvm, \
2562 struct iwl_wowlan_status_ ##_ver *data,\
2563 int len) \
2564 { \
2565 struct iwl_wowlan_status_data *status; \
2566 int data_size, i; \
2567 \
2568 if (len < sizeof(*data)) { \
2569 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2570 return NULL; \
2571 } \
2572 \
2573 data_size = ALIGN(le32_to_cpu(data->wake_packet_bufsize), 4); \
2574 if (len != sizeof(*data) + data_size) { \
2575 IWL_ERR(mvm, "Invalid WoWLAN status response!\n"); \
2576 return NULL; \
2577 } \
2578 \
2579 status = kzalloc(sizeof(*status), GFP_KERNEL); \
2580 if (!status) \
2581 return NULL; \
2582 \
2583 /* copy all the common fields */ \
2584 status->replay_ctr = le64_to_cpu(data->replay_ctr); \
2585 status->pattern_number = le16_to_cpu(data->pattern_number); \
2586 status->non_qos_seq_ctr = le16_to_cpu(data->non_qos_seq_ctr); \
2587 for (i = 0; i < 8; i++) \
2588 status->qos_seq_ctr[i] = \
2589 le16_to_cpu(data->qos_seq_ctr[i]); \
2590 status->wakeup_reasons = le32_to_cpu(data->wakeup_reasons); \
2591 status->num_of_gtk_rekeys = \
2592 le32_to_cpu(data->num_of_gtk_rekeys); \
2593 status->received_beacons = le32_to_cpu(data->received_beacons); \
2594 status->wake_packet_length = \
2595 le32_to_cpu(data->wake_packet_length); \
2596 status->wake_packet_bufsize = \
2597 le32_to_cpu(data->wake_packet_bufsize); \
2598 if (status->wake_packet_bufsize) { \
2599 status->wake_packet = \
2600 kmemdup(data->wake_packet, \
2601 status->wake_packet_bufsize, \
2602 GFP_KERNEL); \
2603 if (!status->wake_packet) { \
2604 kfree(status); \
2605 return NULL; \
2606 } \
2607 } else { \
2608 status->wake_packet = NULL; \
2609 } \
2610 \
2611 return status; \
2612 }
2613
2614 iwl_mvm_parse_wowlan_status_common(v6)
iwl_mvm_parse_wowlan_status_common(v7)2615 iwl_mvm_parse_wowlan_status_common(v7)
2616 iwl_mvm_parse_wowlan_status_common(v9)
2617 iwl_mvm_parse_wowlan_status_common(v12)
2618
2619 static struct iwl_wowlan_status_data *
2620 iwl_mvm_send_wowlan_get_status(struct iwl_mvm *mvm, u8 sta_id)
2621 {
2622 struct iwl_wowlan_status_data *status;
2623 struct iwl_wowlan_get_status_cmd get_status_cmd = {
2624 .sta_id = cpu_to_le32(sta_id),
2625 };
2626 struct iwl_host_cmd cmd = {
2627 .id = WOWLAN_GET_STATUSES,
2628 .flags = CMD_WANT_SKB,
2629 .data = { &get_status_cmd, },
2630 .len = { sizeof(get_status_cmd), },
2631 };
2632 int ret, len;
2633 u8 notif_ver;
2634 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd.id,
2635 IWL_FW_CMD_VER_UNKNOWN);
2636
2637 if (cmd_ver == IWL_FW_CMD_VER_UNKNOWN)
2638 cmd.len[0] = 0;
2639
2640 lockdep_assert_held(&mvm->mutex);
2641
2642 ret = iwl_mvm_send_cmd(mvm, &cmd);
2643 if (ret) {
2644 IWL_ERR(mvm, "failed to query wakeup status (%d)\n", ret);
2645 return ERR_PTR(ret);
2646 }
2647
2648 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2649
2650 /* default to 7 (when we have IWL_UCODE_TLV_API_WOWLAN_KEY_MATERIAL) */
2651 notif_ver = iwl_mvm_lookup_wowlan_status_ver(mvm);
2652
2653 if (notif_ver < 7) {
2654 struct iwl_wowlan_status_v6 *v6 = (void *)cmd.resp_pkt->data;
2655
2656 status = iwl_mvm_parse_wowlan_status_common_v6(mvm, v6, len);
2657 if (!status)
2658 goto out_free_resp;
2659
2660 BUILD_BUG_ON(sizeof(v6->gtk.decrypt_key) >
2661 sizeof(status->gtk[0].key));
2662 BUILD_BUG_ON(NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY +
2663 sizeof(v6->gtk.tkip_mic_key) >
2664 sizeof(status->gtk[0].key));
2665
2666 /* copy GTK info to the right place */
2667 memcpy(status->gtk[0].key, v6->gtk.decrypt_key,
2668 sizeof(v6->gtk.decrypt_key));
2669 memcpy(status->gtk[0].key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
2670 v6->gtk.tkip_mic_key,
2671 sizeof(v6->gtk.tkip_mic_key));
2672
2673 iwl_mvm_convert_key_counters(status, &v6->gtk.rsc.all_tsc_rsc);
2674
2675 /* hardcode the key length to 16 since v6 only supports 16 */
2676 status->gtk[0].len = 16;
2677
2678 /*
2679 * The key index only uses 2 bits (values 0 to 3) and
2680 * we always set bit 7 which means this is the
2681 * currently used key.
2682 */
2683 status->gtk[0].flags = v6->gtk.key_index | BIT(7);
2684 } else if (notif_ver == 7) {
2685 struct iwl_wowlan_status_v7 *v7 = (void *)cmd.resp_pkt->data;
2686
2687 status = iwl_mvm_parse_wowlan_status_common_v7(mvm, v7, len);
2688 if (!status)
2689 goto out_free_resp;
2690
2691 iwl_mvm_convert_key_counters(status, &v7->gtk[0].rsc.all_tsc_rsc);
2692 iwl_mvm_convert_gtk_v2(status, &v7->gtk[0]);
2693 iwl_mvm_convert_igtk(status, &v7->igtk[0]);
2694 } else if (notif_ver == 9 || notif_ver == 10 || notif_ver == 11) {
2695 struct iwl_wowlan_status_v9 *v9 = (void *)cmd.resp_pkt->data;
2696
2697 /* these three command versions have same layout and size, the
2698 * difference is only in a few not used (reserved) fields.
2699 */
2700 status = iwl_mvm_parse_wowlan_status_common_v9(mvm, v9, len);
2701 if (!status)
2702 goto out_free_resp;
2703
2704 iwl_mvm_convert_key_counters(status, &v9->gtk[0].rsc.all_tsc_rsc);
2705 iwl_mvm_convert_gtk_v2(status, &v9->gtk[0]);
2706 iwl_mvm_convert_igtk(status, &v9->igtk[0]);
2707
2708 status->tid_tear_down = v9->tid_tear_down;
2709 } else if (notif_ver == 12) {
2710 struct iwl_wowlan_status_v12 *v12 = (void *)cmd.resp_pkt->data;
2711
2712 status = iwl_mvm_parse_wowlan_status_common_v12(mvm, v12, len);
2713 if (!status)
2714 goto out_free_resp;
2715
2716 iwl_mvm_convert_key_counters_v5(status, &v12->gtk[0].sc);
2717 iwl_mvm_convert_gtk_v3(status, v12->gtk);
2718 iwl_mvm_convert_igtk(status, &v12->igtk[0]);
2719
2720 status->tid_tear_down = v12->tid_tear_down;
2721 } else {
2722 IWL_ERR(mvm,
2723 "Firmware advertises unknown WoWLAN status response %d!\n",
2724 notif_ver);
2725 status = NULL;
2726 }
2727
2728 out_free_resp:
2729 iwl_free_resp(&cmd);
2730 return status;
2731 }
2732
2733 /* releases the MVM mutex */
iwl_mvm_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_wowlan_status_data * status)2734 static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
2735 struct ieee80211_vif *vif,
2736 struct iwl_wowlan_status_data *status)
2737 {
2738 int i;
2739 bool keep = false;
2740 struct iwl_mvm_sta *mvm_ap_sta;
2741 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2742 int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
2743 struct iwl_mvm_vif_link_info *mvm_link = mvmvif->link[link_id];
2744
2745 if (WARN_ON(!mvm_link))
2746 goto out_unlock;
2747
2748 if (!status)
2749 goto out_unlock;
2750
2751 IWL_DEBUG_WOWLAN(mvm, "wakeup reason 0x%x\n",
2752 status->wakeup_reasons);
2753
2754 mvm_ap_sta = iwl_mvm_sta_from_staid_protected(mvm, mvm_link->ap_sta_id);
2755 if (!mvm_ap_sta)
2756 goto out_unlock;
2757
2758 for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
2759 u16 seq = status->qos_seq_ctr[i];
2760 /* firmware stores last-used value, we store next value */
2761 seq += 0x10;
2762 mvm_ap_sta->tid_data[i].seq_number = seq;
2763 }
2764
2765 if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
2766 i = mvm->offload_tid;
2767 iwl_trans_set_q_ptrs(mvm->trans,
2768 mvm_ap_sta->tid_data[i].txq_id,
2769 mvm_ap_sta->tid_data[i].seq_number >> 4);
2770 }
2771
2772 iwl_mvm_report_wakeup_reasons(mvm, vif, status);
2773
2774 keep = iwl_mvm_setup_connection_keep(mvm, vif, status);
2775 out_unlock:
2776 mutex_unlock(&mvm->mutex);
2777 return keep;
2778 }
2779
2780 #define ND_QUERY_BUF_LEN (sizeof(struct iwl_scan_offload_profile_match) * \
2781 IWL_SCAN_MAX_PROFILES)
2782
2783 struct iwl_mvm_nd_results {
2784 u32 matched_profiles;
2785 u8 matches[ND_QUERY_BUF_LEN];
2786 };
2787
2788 static int
iwl_mvm_netdetect_query_results(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results)2789 iwl_mvm_netdetect_query_results(struct iwl_mvm *mvm,
2790 struct iwl_mvm_nd_results *results)
2791 {
2792 struct iwl_scan_offload_match_info *query;
2793 struct iwl_host_cmd cmd = {
2794 .id = SCAN_OFFLOAD_PROFILES_QUERY_CMD,
2795 .flags = CMD_WANT_SKB,
2796 };
2797 int ret, len;
2798 size_t query_len, matches_len;
2799 int max_profiles = iwl_umac_scan_get_max_profiles(mvm->fw);
2800
2801 ret = iwl_mvm_send_cmd(mvm, &cmd);
2802 if (ret) {
2803 IWL_ERR(mvm, "failed to query matched profiles (%d)\n", ret);
2804 return ret;
2805 }
2806
2807 if (fw_has_api(&mvm->fw->ucode_capa,
2808 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2809 query_len = sizeof(struct iwl_scan_offload_match_info);
2810 matches_len = sizeof(struct iwl_scan_offload_profile_match) *
2811 max_profiles;
2812 } else {
2813 query_len = sizeof(struct iwl_scan_offload_profiles_query_v1);
2814 matches_len = sizeof(struct iwl_scan_offload_profile_match_v1) *
2815 max_profiles;
2816 }
2817
2818 len = iwl_rx_packet_payload_len(cmd.resp_pkt);
2819 if (len < query_len) {
2820 IWL_ERR(mvm, "Invalid scan offload profiles query response!\n");
2821 ret = -EIO;
2822 goto out_free_resp;
2823 }
2824
2825 query = (void *)cmd.resp_pkt->data;
2826
2827 results->matched_profiles = le32_to_cpu(query->matched_profiles);
2828 memcpy(results->matches, query->matches, matches_len);
2829
2830 #ifdef CONFIG_IWLWIFI_DEBUGFS
2831 mvm->last_netdetect_scans = le32_to_cpu(query->n_scans_done);
2832 #endif
2833
2834 out_free_resp:
2835 iwl_free_resp(&cmd);
2836 return ret;
2837 }
2838
iwl_mvm_query_num_match_chans(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,int idx)2839 static int iwl_mvm_query_num_match_chans(struct iwl_mvm *mvm,
2840 struct iwl_mvm_nd_results *results,
2841 int idx)
2842 {
2843 int n_chans = 0, i;
2844
2845 if (fw_has_api(&mvm->fw->ucode_capa,
2846 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2847 struct iwl_scan_offload_profile_match *matches =
2848 (void *)results->matches;
2849
2850 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN; i++)
2851 n_chans += hweight8(matches[idx].matching_channels[i]);
2852 } else {
2853 struct iwl_scan_offload_profile_match_v1 *matches =
2854 (void *)results->matches;
2855
2856 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1; i++)
2857 n_chans += hweight8(matches[idx].matching_channels[i]);
2858 }
2859
2860 return n_chans;
2861 }
2862
iwl_mvm_query_set_freqs(struct iwl_mvm * mvm,struct iwl_mvm_nd_results * results,struct cfg80211_wowlan_nd_match * match,int idx)2863 static void iwl_mvm_query_set_freqs(struct iwl_mvm *mvm,
2864 struct iwl_mvm_nd_results *results,
2865 struct cfg80211_wowlan_nd_match *match,
2866 int idx)
2867 {
2868 int i;
2869
2870 if (fw_has_api(&mvm->fw->ucode_capa,
2871 IWL_UCODE_TLV_API_SCAN_OFFLOAD_CHANS)) {
2872 struct iwl_scan_offload_profile_match *matches =
2873 (void *)results->matches;
2874
2875 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN * 8; i++)
2876 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2877 match->channels[match->n_channels++] =
2878 mvm->nd_channels[i]->center_freq;
2879 } else {
2880 struct iwl_scan_offload_profile_match_v1 *matches =
2881 (void *)results->matches;
2882
2883 for (i = 0; i < SCAN_OFFLOAD_MATCHING_CHANNELS_LEN_V1 * 8; i++)
2884 if (matches[idx].matching_channels[i / 8] & (BIT(i % 8)))
2885 match->channels[match->n_channels++] =
2886 mvm->nd_channels[i]->center_freq;
2887 }
2888 }
2889
2890 /**
2891 * enum iwl_d3_notif - d3 notifications
2892 * @IWL_D3_NOTIF_WOWLAN_INFO: WOWLAN_INFO_NOTIF was received
2893 * @IWL_D3_NOTIF_WOWLAN_WAKE_PKT: WOWLAN_WAKE_PKT_NOTIF was received
2894 * @IWL_D3_NOTIF_PROT_OFFLOAD: PROT_OFFLOAD_NOTIF was received
2895 * @IWL_D3_ND_MATCH_INFO: OFFLOAD_MATCH_INFO_NOTIF was received
2896 * @IWL_D3_NOTIF_D3_END_NOTIF: D3_END_NOTIF was received
2897 */
2898 enum iwl_d3_notif {
2899 IWL_D3_NOTIF_WOWLAN_INFO = BIT(0),
2900 IWL_D3_NOTIF_WOWLAN_WAKE_PKT = BIT(1),
2901 IWL_D3_NOTIF_PROT_OFFLOAD = BIT(2),
2902 IWL_D3_ND_MATCH_INFO = BIT(3),
2903 IWL_D3_NOTIF_D3_END_NOTIF = BIT(4)
2904 };
2905
2906 /* manage d3 resume data */
2907 struct iwl_d3_data {
2908 struct iwl_wowlan_status_data *status;
2909 bool test;
2910 u32 d3_end_flags;
2911 u32 notif_expected; /* bitmap - see &enum iwl_d3_notif */
2912 u32 notif_received; /* bitmap - see &enum iwl_d3_notif */
2913 struct iwl_mvm_nd_results *nd_results;
2914 bool nd_results_valid;
2915 };
2916
iwl_mvm_query_netdetect_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)2917 static void iwl_mvm_query_netdetect_reasons(struct iwl_mvm *mvm,
2918 struct ieee80211_vif *vif,
2919 struct iwl_d3_data *d3_data)
2920 {
2921 struct cfg80211_wowlan_nd_info *net_detect = NULL;
2922 struct cfg80211_wowlan_wakeup wakeup = {
2923 .pattern_idx = -1,
2924 };
2925 struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
2926 unsigned long matched_profiles;
2927 u32 reasons = 0;
2928 int i, n_matches, ret;
2929
2930 if (WARN_ON(!d3_data || !d3_data->status))
2931 goto out;
2932
2933 reasons = d3_data->status->wakeup_reasons;
2934
2935 if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
2936 wakeup.rfkill_release = true;
2937
2938 if (reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS)
2939 goto out;
2940
2941 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
2942 WOWLAN_INFO_NOTIFICATION, 0)) {
2943 IWL_INFO(mvm, "Query FW for ND results\n");
2944 ret = iwl_mvm_netdetect_query_results(mvm, d3_data->nd_results);
2945
2946 } else {
2947 IWL_INFO(mvm, "Notification based ND results\n");
2948 ret = d3_data->nd_results_valid ? 0 : -1;
2949 }
2950
2951 if (ret || !d3_data->nd_results->matched_profiles) {
2952 wakeup_report = NULL;
2953 goto out;
2954 }
2955
2956 matched_profiles = d3_data->nd_results->matched_profiles;
2957 if (mvm->n_nd_match_sets) {
2958 n_matches = hweight_long(matched_profiles);
2959 } else {
2960 IWL_ERR(mvm, "no net detect match information available\n");
2961 n_matches = 0;
2962 }
2963
2964 net_detect = kzalloc(struct_size(net_detect, matches, n_matches),
2965 GFP_KERNEL);
2966 if (!net_detect || !n_matches)
2967 goto out_report_nd;
2968
2969 for_each_set_bit(i, &matched_profiles, mvm->n_nd_match_sets) {
2970 struct cfg80211_wowlan_nd_match *match;
2971 int idx, n_channels = 0;
2972
2973 n_channels = iwl_mvm_query_num_match_chans(mvm,
2974 d3_data->nd_results,
2975 i);
2976
2977 match = kzalloc(struct_size(match, channels, n_channels),
2978 GFP_KERNEL);
2979 if (!match)
2980 goto out_report_nd;
2981
2982 net_detect->matches[net_detect->n_matches++] = match;
2983
2984 /* We inverted the order of the SSIDs in the scan
2985 * request, so invert the index here.
2986 */
2987 idx = mvm->n_nd_match_sets - i - 1;
2988 match->ssid.ssid_len = mvm->nd_match_sets[idx].ssid.ssid_len;
2989 memcpy(match->ssid.ssid, mvm->nd_match_sets[idx].ssid.ssid,
2990 match->ssid.ssid_len);
2991
2992 if (mvm->n_nd_channels < n_channels)
2993 continue;
2994
2995 iwl_mvm_query_set_freqs(mvm, d3_data->nd_results, match, i);
2996 }
2997
2998 out_report_nd:
2999 wakeup.net_detect = net_detect;
3000 out:
3001 iwl_mvm_free_nd(mvm);
3002
3003 mutex_unlock(&mvm->mutex);
3004 ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
3005
3006 if (net_detect) {
3007 for (i = 0; i < net_detect->n_matches; i++)
3008 kfree(net_detect->matches[i]);
3009 kfree(net_detect);
3010 }
3011 }
3012
iwl_mvm_d3_disconnect_iter(void * data,u8 * mac,struct ieee80211_vif * vif)3013 static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
3014 struct ieee80211_vif *vif)
3015 {
3016 /* skip the one we keep connection on */
3017 if (data == vif)
3018 return;
3019
3020 if (vif->type == NL80211_IFTYPE_STATION)
3021 ieee80211_resume_disconnect(vif);
3022 }
3023
iwl_mvm_rt_status(struct iwl_trans * trans,u32 base,u32 * err_id)3024 static bool iwl_mvm_rt_status(struct iwl_trans *trans, u32 base, u32 *err_id)
3025 {
3026 struct error_table_start {
3027 /* cf. struct iwl_error_event_table */
3028 u32 valid;
3029 __le32 err_id;
3030 } err_info;
3031
3032 if (!base)
3033 return false;
3034
3035 iwl_trans_read_mem_bytes(trans, base,
3036 &err_info, sizeof(err_info));
3037 if (err_info.valid && err_id)
3038 *err_id = le32_to_cpu(err_info.err_id);
3039
3040 return !!err_info.valid;
3041 }
3042
iwl_mvm_check_rt_status(struct iwl_mvm * mvm,struct ieee80211_vif * vif)3043 static bool iwl_mvm_check_rt_status(struct iwl_mvm *mvm,
3044 struct ieee80211_vif *vif)
3045 {
3046 u32 err_id;
3047
3048 /* check for lmac1 error */
3049 if (iwl_mvm_rt_status(mvm->trans,
3050 mvm->trans->dbg.lmac_error_event_table[0],
3051 &err_id)) {
3052 if (err_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
3053 struct cfg80211_wowlan_wakeup wakeup = {
3054 .rfkill_release = true,
3055 };
3056 ieee80211_report_wowlan_wakeup(vif, &wakeup,
3057 GFP_KERNEL);
3058 }
3059 return true;
3060 }
3061
3062 /* check if we have lmac2 set and check for error */
3063 if (iwl_mvm_rt_status(mvm->trans,
3064 mvm->trans->dbg.lmac_error_event_table[1], NULL))
3065 return true;
3066
3067 /* check for umac error */
3068 if (iwl_mvm_rt_status(mvm->trans,
3069 mvm->trans->dbg.umac_error_event_table, NULL))
3070 return true;
3071
3072 return false;
3073 }
3074
3075 /*
3076 * This function assumes:
3077 * 1. The mutex is already held.
3078 * 2. The callee functions unlock the mutex.
3079 */
3080 static bool
iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm * mvm,struct ieee80211_vif * vif,struct iwl_d3_data * d3_data)3081 iwl_mvm_choose_query_wakeup_reasons(struct iwl_mvm *mvm,
3082 struct ieee80211_vif *vif,
3083 struct iwl_d3_data *d3_data)
3084 {
3085 lockdep_assert_held(&mvm->mutex);
3086
3087 /* if FW uses status notification, status shouldn't be NULL here */
3088 if (!d3_data->status) {
3089 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3090 u8 sta_id = mvm->net_detect ? IWL_MVM_INVALID_STA :
3091 mvmvif->deflink.ap_sta_id;
3092
3093 /* bug - FW with MLO has status notification */
3094 WARN_ON(ieee80211_vif_is_mld(vif));
3095
3096 d3_data->status = iwl_mvm_send_wowlan_get_status(mvm, sta_id);
3097 }
3098
3099 if (mvm->net_detect) {
3100 iwl_mvm_query_netdetect_reasons(mvm, vif, d3_data);
3101 } else {
3102 bool keep = iwl_mvm_query_wakeup_reasons(mvm, vif,
3103 d3_data->status);
3104
3105 #ifdef CONFIG_IWLWIFI_DEBUGFS
3106 if (keep)
3107 mvm->keep_vif = vif;
3108 #endif
3109
3110 return keep;
3111 }
3112 return false;
3113 }
3114
3115 #define IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT (IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET | \
3116 IWL_WOWLAN_WAKEUP_BY_PATTERN | \
3117 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN |\
3118 IWL_WAKEUP_BY_PATTERN_IPV4_TCP_SYN_WILDCARD |\
3119 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN |\
3120 IWL_WAKEUP_BY_PATTERN_IPV6_TCP_SYN_WILDCARD)
3121
iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm * mvm,struct iwl_wowlan_wake_pkt_notif * notif,struct iwl_wowlan_status_data * status,u32 len)3122 static int iwl_mvm_wowlan_store_wake_pkt(struct iwl_mvm *mvm,
3123 struct iwl_wowlan_wake_pkt_notif *notif,
3124 struct iwl_wowlan_status_data *status,
3125 u32 len)
3126 {
3127 u32 data_size, packet_len = le32_to_cpu(notif->wake_packet_length);
3128
3129 if (len < sizeof(*notif)) {
3130 IWL_ERR(mvm, "Invalid WoWLAN wake packet notification!\n");
3131 return -EIO;
3132 }
3133
3134 if (WARN_ON(!status)) {
3135 IWL_ERR(mvm, "Got wake packet notification but wowlan status data is NULL\n");
3136 return -EIO;
3137 }
3138
3139 if (WARN_ON(!(status->wakeup_reasons &
3140 IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT))) {
3141 IWL_ERR(mvm, "Got wakeup packet but wakeup reason is %x\n",
3142 status->wakeup_reasons);
3143 return -EIO;
3144 }
3145
3146 data_size = len - offsetof(struct iwl_wowlan_wake_pkt_notif, wake_packet);
3147
3148 /* data_size got the padding from the notification, remove it. */
3149 if (packet_len < data_size)
3150 data_size = packet_len;
3151
3152 status->wake_packet = kmemdup(notif->wake_packet, data_size,
3153 GFP_ATOMIC);
3154
3155 if (!status->wake_packet)
3156 return -ENOMEM;
3157
3158 status->wake_packet_length = packet_len;
3159 status->wake_packet_bufsize = data_size;
3160
3161 return 0;
3162 }
3163
iwl_mvm_nd_match_info_handler(struct iwl_mvm * mvm,struct iwl_d3_data * d3_data,struct iwl_scan_offload_match_info * notif,u32 len)3164 static void iwl_mvm_nd_match_info_handler(struct iwl_mvm *mvm,
3165 struct iwl_d3_data *d3_data,
3166 struct iwl_scan_offload_match_info *notif,
3167 u32 len)
3168 {
3169 struct iwl_wowlan_status_data *status = d3_data->status;
3170 struct ieee80211_vif *vif = iwl_mvm_get_bss_vif(mvm);
3171 struct iwl_mvm_nd_results *results = d3_data->nd_results;
3172 size_t i, matches_len = sizeof(struct iwl_scan_offload_profile_match) *
3173 iwl_umac_scan_get_max_profiles(mvm->fw);
3174
3175 if (IS_ERR_OR_NULL(vif))
3176 return;
3177
3178 if (len < sizeof(struct iwl_scan_offload_match_info)) {
3179 IWL_ERR(mvm, "Invalid scan match info notification\n");
3180 return;
3181 }
3182
3183 if (!mvm->net_detect) {
3184 IWL_ERR(mvm, "Unexpected scan match info notification\n");
3185 return;
3186 }
3187
3188 if (!status || status->wakeup_reasons != IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
3189 IWL_ERR(mvm,
3190 "Ignore scan match info notification: no reason\n");
3191 return;
3192 }
3193
3194 #ifdef CONFIG_IWLWIFI_DEBUGFS
3195 mvm->last_netdetect_scans = le32_to_cpu(notif->n_scans_done);
3196 #endif
3197
3198 results->matched_profiles = le32_to_cpu(notif->matched_profiles);
3199 IWL_INFO(mvm, "number of matched profiles=%u\n",
3200 results->matched_profiles);
3201
3202 if (results->matched_profiles) {
3203 memcpy(results->matches, notif->matches, matches_len);
3204 d3_data->nd_results_valid = true;
3205 }
3206
3207 /* no scan should be active at this point */
3208 mvm->scan_status = 0;
3209 for (i = 0; i < mvm->max_scans; i++)
3210 mvm->scan_uid_status[i] = 0;
3211 }
3212
iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data)3213 static bool iwl_mvm_wait_d3_notif(struct iwl_notif_wait_data *notif_wait,
3214 struct iwl_rx_packet *pkt, void *data)
3215 {
3216 struct iwl_mvm *mvm =
3217 container_of(notif_wait, struct iwl_mvm, notif_wait);
3218 struct iwl_d3_data *d3_data = data;
3219 u32 len = iwl_rx_packet_payload_len(pkt);
3220 int ret;
3221 int wowlan_info_ver = iwl_fw_lookup_notif_ver(mvm->fw,
3222 PROT_OFFLOAD_GROUP,
3223 WOWLAN_INFO_NOTIFICATION,
3224 IWL_FW_CMD_VER_UNKNOWN);
3225
3226
3227 switch (WIDE_ID(pkt->hdr.group_id, pkt->hdr.cmd)) {
3228 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION): {
3229
3230 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_INFO) {
3231 /* We might get two notifications due to dual bss */
3232 IWL_DEBUG_WOWLAN(mvm,
3233 "Got additional wowlan info notification\n");
3234 break;
3235 }
3236
3237 if (wowlan_info_ver < 2) {
3238 struct iwl_wowlan_info_notif_v1 *notif_v1 =
3239 (void *)pkt->data;
3240 struct iwl_wowlan_info_notif_v2 *notif_v2;
3241
3242 notif_v2 = kmemdup(notif_v1, sizeof(*notif_v2), GFP_ATOMIC);
3243
3244 if (!notif_v2)
3245 return false;
3246
3247 notif_v2->tid_tear_down = notif_v1->tid_tear_down;
3248 notif_v2->station_id = notif_v1->station_id;
3249 memset_after(notif_v2, 0, station_id);
3250 iwl_mvm_parse_wowlan_info_notif_v2(mvm, notif_v2,
3251 d3_data->status,
3252 len);
3253 kfree(notif_v2);
3254
3255 } else if (wowlan_info_ver == 2) {
3256 struct iwl_wowlan_info_notif_v2 *notif_v2 =
3257 (void *)pkt->data;
3258
3259 iwl_mvm_parse_wowlan_info_notif_v2(mvm, notif_v2,
3260 d3_data->status,
3261 len);
3262 } else {
3263 struct iwl_wowlan_info_notif *notif =
3264 (void *)pkt->data;
3265
3266 iwl_mvm_parse_wowlan_info_notif(mvm, notif,
3267 d3_data->status, len,
3268 wowlan_info_ver > 3);
3269 }
3270
3271 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_INFO;
3272
3273 if (d3_data->status &&
3274 d3_data->status->wakeup_reasons & IWL_WOWLAN_WAKEUP_REASON_HAS_WAKEUP_PKT)
3275 /* We are supposed to get also wake packet notif */
3276 d3_data->notif_expected |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3277
3278 break;
3279 }
3280 case WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION): {
3281 struct iwl_wowlan_wake_pkt_notif *notif = (void *)pkt->data;
3282
3283 if (d3_data->notif_received & IWL_D3_NOTIF_WOWLAN_WAKE_PKT) {
3284 /* We shouldn't get two wake packet notifications */
3285 IWL_ERR(mvm,
3286 "Got additional wowlan wake packet notification\n");
3287 } else {
3288 d3_data->notif_received |= IWL_D3_NOTIF_WOWLAN_WAKE_PKT;
3289 len = iwl_rx_packet_payload_len(pkt);
3290 ret = iwl_mvm_wowlan_store_wake_pkt(mvm, notif,
3291 d3_data->status,
3292 len);
3293 if (ret)
3294 IWL_ERR(mvm,
3295 "Can't parse WOWLAN_WAKE_PKT_NOTIFICATION\n");
3296 }
3297
3298 break;
3299 }
3300 case WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF): {
3301 struct iwl_scan_offload_match_info *notif = (void *)pkt->data;
3302
3303 if (d3_data->notif_received & IWL_D3_ND_MATCH_INFO) {
3304 IWL_ERR(mvm,
3305 "Got additional netdetect match info\n");
3306 break;
3307 }
3308
3309 d3_data->notif_received |= IWL_D3_ND_MATCH_INFO;
3310
3311 /* explicitly set this in the 'expected' as well */
3312 d3_data->notif_expected |= IWL_D3_ND_MATCH_INFO;
3313
3314 len = iwl_rx_packet_payload_len(pkt);
3315 iwl_mvm_nd_match_info_handler(mvm, d3_data, notif, len);
3316 break;
3317 }
3318 case WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION): {
3319 struct iwl_mvm_d3_end_notif *notif = (void *)pkt->data;
3320
3321 d3_data->d3_end_flags = __le32_to_cpu(notif->flags);
3322 d3_data->notif_received |= IWL_D3_NOTIF_D3_END_NOTIF;
3323
3324 break;
3325 }
3326 default:
3327 WARN_ON(1);
3328 }
3329
3330 return d3_data->notif_received == d3_data->notif_expected;
3331 }
3332
iwl_mvm_resume_firmware(struct iwl_mvm * mvm,bool test)3333 static int iwl_mvm_resume_firmware(struct iwl_mvm *mvm, bool test)
3334 {
3335 int ret;
3336 enum iwl_d3_status d3_status;
3337 struct iwl_host_cmd cmd = {
3338 .id = D0I3_END_CMD,
3339 .flags = CMD_WANT_SKB | CMD_SEND_IN_D3,
3340 };
3341 bool reset = fw_has_capa(&mvm->fw->ucode_capa,
3342 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3343
3344 ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test, !reset);
3345 if (ret)
3346 return ret;
3347
3348 if (d3_status != IWL_D3_STATUS_ALIVE) {
3349 IWL_INFO(mvm, "Device was reset during suspend\n");
3350 return -ENOENT;
3351 }
3352
3353 /*
3354 * We should trigger resume flow using command only for 22000 family
3355 * AX210 and above don't need the command since they have
3356 * the doorbell interrupt.
3357 */
3358 if (mvm->trans->trans_cfg->device_family <= IWL_DEVICE_FAMILY_22000 &&
3359 fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_D0I3_END_FIRST)) {
3360 ret = iwl_mvm_send_cmd(mvm, &cmd);
3361 if (ret < 0)
3362 IWL_ERR(mvm, "Failed to send D0I3_END_CMD first (%d)\n",
3363 ret);
3364 }
3365
3366 return ret;
3367 }
3368
3369 #define IWL_MVM_D3_NOTIF_TIMEOUT (HZ / 5)
3370
iwl_mvm_d3_notif_wait(struct iwl_mvm * mvm,struct iwl_d3_data * d3_data)3371 static int iwl_mvm_d3_notif_wait(struct iwl_mvm *mvm,
3372 struct iwl_d3_data *d3_data)
3373 {
3374 static const u16 d3_resume_notif[] = {
3375 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_INFO_NOTIFICATION),
3376 WIDE_ID(PROT_OFFLOAD_GROUP, WOWLAN_WAKE_PKT_NOTIFICATION),
3377 WIDE_ID(SCAN_GROUP, OFFLOAD_MATCH_INFO_NOTIF),
3378 WIDE_ID(PROT_OFFLOAD_GROUP, D3_END_NOTIFICATION)
3379 };
3380 struct iwl_notification_wait wait_d3_notif;
3381 int ret;
3382
3383 iwl_init_notification_wait(&mvm->notif_wait, &wait_d3_notif,
3384 d3_resume_notif, ARRAY_SIZE(d3_resume_notif),
3385 iwl_mvm_wait_d3_notif, d3_data);
3386
3387 ret = iwl_mvm_resume_firmware(mvm, d3_data->test);
3388 if (ret) {
3389 iwl_remove_notification(&mvm->notif_wait, &wait_d3_notif);
3390 return ret;
3391 }
3392
3393 return iwl_wait_notification(&mvm->notif_wait, &wait_d3_notif,
3394 IWL_MVM_D3_NOTIF_TIMEOUT);
3395 }
3396
iwl_mvm_d3_resume_notif_based(struct iwl_mvm * mvm)3397 static inline bool iwl_mvm_d3_resume_notif_based(struct iwl_mvm *mvm)
3398 {
3399 return iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3400 WOWLAN_INFO_NOTIFICATION, 0) &&
3401 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3402 WOWLAN_WAKE_PKT_NOTIFICATION, 0) &&
3403 iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3404 D3_END_NOTIFICATION, 0);
3405 }
3406
__iwl_mvm_resume(struct iwl_mvm * mvm,bool test)3407 static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
3408 {
3409 struct ieee80211_vif *vif = NULL;
3410 int ret = 1;
3411 struct iwl_mvm_nd_results results = {};
3412 struct iwl_d3_data d3_data = {
3413 .test = test,
3414 .notif_expected =
3415 IWL_D3_NOTIF_WOWLAN_INFO |
3416 IWL_D3_NOTIF_D3_END_NOTIF,
3417 .nd_results_valid = false,
3418 .nd_results = &results,
3419 };
3420 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3421 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3422 bool d0i3_first = fw_has_capa(&mvm->fw->ucode_capa,
3423 IWL_UCODE_TLV_CAPA_D0I3_END_FIRST);
3424 bool resume_notif_based = iwl_mvm_d3_resume_notif_based(mvm);
3425 bool keep = false;
3426
3427 mutex_lock(&mvm->mutex);
3428
3429 mvm->last_reset_or_resume_time_jiffies = jiffies;
3430
3431 /* get the BSS vif pointer again */
3432 vif = iwl_mvm_get_bss_vif(mvm);
3433 if (IS_ERR_OR_NULL(vif))
3434 goto err;
3435
3436 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3437
3438 if (iwl_mvm_check_rt_status(mvm, vif)) {
3439 set_bit(STATUS_FW_ERROR, &mvm->trans->status);
3440 iwl_mvm_dump_nic_error_log(mvm);
3441 iwl_dbg_tlv_time_point(&mvm->fwrt,
3442 IWL_FW_INI_TIME_POINT_FW_ASSERT, NULL);
3443 iwl_fw_dbg_collect_desc(&mvm->fwrt, &iwl_dump_desc_assert,
3444 false, 0);
3445 ret = 1;
3446 goto err;
3447 }
3448
3449 if (resume_notif_based) {
3450 d3_data.status = kzalloc(sizeof(*d3_data.status), GFP_KERNEL);
3451 if (!d3_data.status) {
3452 IWL_ERR(mvm, "Failed to allocate wowlan status\n");
3453 ret = -ENOMEM;
3454 goto err;
3455 }
3456
3457 ret = iwl_mvm_d3_notif_wait(mvm, &d3_data);
3458 if (ret)
3459 goto err;
3460 } else {
3461 ret = iwl_mvm_resume_firmware(mvm, test);
3462 if (ret < 0)
3463 goto err;
3464 }
3465
3466 iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_WOWLAN);
3467
3468 /* after the successful handshake, we're out of D3 */
3469 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3470
3471 /* when reset is required we can't send these following commands */
3472 if (d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)
3473 goto query_wakeup_reasons;
3474
3475 /*
3476 * Query the current location and source from the D3 firmware so we
3477 * can play it back when we re-intiailize the D0 firmware
3478 */
3479 iwl_mvm_update_changed_regdom(mvm);
3480
3481 /* Re-configure PPAG settings */
3482 iwl_mvm_ppag_send_cmd(mvm);
3483
3484 if (!unified_image)
3485 /* Re-configure default SAR profile */
3486 iwl_mvm_sar_select_profile(mvm, 1, 1);
3487
3488 if (mvm->net_detect && unified_image) {
3489 /* If this is a non-unified image, we restart the FW,
3490 * so no need to stop the netdetect scan. If that
3491 * fails, continue and try to get the wake-up reasons,
3492 * but trigger a HW restart by keeping a failure code
3493 * in ret.
3494 */
3495 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_NETDETECT,
3496 false);
3497 }
3498
3499 query_wakeup_reasons:
3500 keep = iwl_mvm_choose_query_wakeup_reasons(mvm, vif, &d3_data);
3501 /* has unlocked the mutex, so skip that */
3502 goto out;
3503
3504 err:
3505 mutex_unlock(&mvm->mutex);
3506 out:
3507 if (d3_data.status)
3508 kfree(d3_data.status->wake_packet);
3509 kfree(d3_data.status);
3510 iwl_mvm_free_nd(mvm);
3511
3512 if (!d3_data.test && !mvm->net_detect)
3513 ieee80211_iterate_active_interfaces_mtx(mvm->hw,
3514 IEEE80211_IFACE_ITER_NORMAL,
3515 iwl_mvm_d3_disconnect_iter,
3516 keep ? vif : NULL);
3517
3518 clear_bit(IWL_MVM_STATUS_IN_D3, &mvm->status);
3519
3520 /* no need to reset the device in unified images, if successful */
3521 if (unified_image && !ret) {
3522 /* nothing else to do if we already sent D0I3_END_CMD */
3523 if (d0i3_first)
3524 return 0;
3525
3526 if (!iwl_fw_lookup_notif_ver(mvm->fw, PROT_OFFLOAD_GROUP,
3527 D3_END_NOTIFICATION, 0)) {
3528 ret = iwl_mvm_send_cmd_pdu(mvm, D0I3_END_CMD, 0, 0, NULL);
3529 if (!ret)
3530 return 0;
3531 } else if (!(d3_data.d3_end_flags & IWL_D0I3_RESET_REQUIRE)) {
3532 return 0;
3533 }
3534 }
3535
3536 /*
3537 * Reconfigure the device in one of the following cases:
3538 * 1. We are not using a unified image
3539 * 2. We are using a unified image but had an error while exiting D3
3540 */
3541 set_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
3542
3543 /* regardless of what happened, we're now out of D3 */
3544 mvm->trans->system_pm_mode = IWL_PLAT_PM_MODE_DISABLED;
3545
3546 return 1;
3547 }
3548
iwl_mvm_resume(struct ieee80211_hw * hw)3549 int iwl_mvm_resume(struct ieee80211_hw *hw)
3550 {
3551 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3552 int ret;
3553
3554 ret = __iwl_mvm_resume(mvm, false);
3555
3556 iwl_mvm_resume_tcm(mvm);
3557
3558 iwl_fw_runtime_resume(&mvm->fwrt);
3559
3560 return ret;
3561 }
3562
iwl_mvm_set_wakeup(struct ieee80211_hw * hw,bool enabled)3563 void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
3564 {
3565 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3566
3567 device_set_wakeup_enable(mvm->trans->dev, enabled);
3568 }
3569
3570 #ifdef CONFIG_IWLWIFI_DEBUGFS
iwl_mvm_d3_test_open(struct inode * inode,struct file * file)3571 static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
3572 {
3573 struct iwl_mvm *mvm = inode->i_private;
3574 int err;
3575
3576 if (mvm->d3_test_active)
3577 return -EBUSY;
3578
3579 file->private_data = inode->i_private;
3580
3581 iwl_mvm_pause_tcm(mvm, true);
3582
3583 iwl_fw_runtime_suspend(&mvm->fwrt);
3584
3585 /* start pseudo D3 */
3586 rtnl_lock();
3587 wiphy_lock(mvm->hw->wiphy);
3588 err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
3589 wiphy_unlock(mvm->hw->wiphy);
3590 rtnl_unlock();
3591 if (err > 0)
3592 err = -EINVAL;
3593 if (err)
3594 return err;
3595
3596 mvm->d3_test_active = true;
3597 mvm->keep_vif = NULL;
3598 return 0;
3599 }
3600
iwl_mvm_d3_test_read(struct file * file,char __user * user_buf,size_t count,loff_t * ppos)3601 static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
3602 size_t count, loff_t *ppos)
3603 {
3604 struct iwl_mvm *mvm = file->private_data;
3605 unsigned long end = jiffies + 60 * HZ;
3606 u32 pme_asserted;
3607
3608 while (true) {
3609 /* read pme_ptr if available */
3610 if (mvm->d3_test_pme_ptr) {
3611 pme_asserted = iwl_trans_read_mem32(mvm->trans,
3612 mvm->d3_test_pme_ptr);
3613 if (pme_asserted)
3614 break;
3615 }
3616
3617 if (msleep_interruptible(100))
3618 break;
3619
3620 if (time_is_before_jiffies(end)) {
3621 IWL_ERR(mvm,
3622 "ending pseudo-D3 with timeout after ~60 seconds\n");
3623 return -ETIMEDOUT;
3624 }
3625 }
3626
3627 return 0;
3628 }
3629
iwl_mvm_d3_test_disconn_work_iter(void * _data,u8 * mac,struct ieee80211_vif * vif)3630 static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
3631 struct ieee80211_vif *vif)
3632 {
3633 /* skip the one we keep connection on */
3634 if (_data == vif)
3635 return;
3636
3637 if (vif->type == NL80211_IFTYPE_STATION)
3638 ieee80211_connection_loss(vif);
3639 }
3640
iwl_mvm_d3_test_release(struct inode * inode,struct file * file)3641 static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
3642 {
3643 struct iwl_mvm *mvm = inode->i_private;
3644 bool unified_image = fw_has_capa(&mvm->fw->ucode_capa,
3645 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
3646
3647 mvm->d3_test_active = false;
3648
3649 iwl_fw_dbg_read_d3_debug_data(&mvm->fwrt);
3650
3651 rtnl_lock();
3652 wiphy_lock(mvm->hw->wiphy);
3653 __iwl_mvm_resume(mvm, true);
3654 wiphy_unlock(mvm->hw->wiphy);
3655 rtnl_unlock();
3656
3657 iwl_mvm_resume_tcm(mvm);
3658
3659 iwl_fw_runtime_resume(&mvm->fwrt);
3660
3661 iwl_abort_notification_waits(&mvm->notif_wait);
3662 if (!unified_image) {
3663 int remaining_time = 10;
3664
3665 ieee80211_restart_hw(mvm->hw);
3666
3667 /* wait for restart and disconnect all interfaces */
3668 while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3669 remaining_time > 0) {
3670 remaining_time--;
3671 msleep(1000);
3672 }
3673
3674 if (remaining_time == 0)
3675 IWL_ERR(mvm, "Timed out waiting for HW restart!\n");
3676 }
3677
3678 ieee80211_iterate_active_interfaces_atomic(
3679 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
3680 iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
3681
3682 return 0;
3683 }
3684
3685 const struct file_operations iwl_dbgfs_d3_test_ops = {
3686 .llseek = no_llseek,
3687 .open = iwl_mvm_d3_test_open,
3688 .read = iwl_mvm_d3_test_read,
3689 .release = iwl_mvm_d3_test_release,
3690 };
3691 #endif
3692