d3.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940
  1. /******************************************************************************
  2. *
  3. * This file is provided under a dual BSD/GPLv2 license. When using or
  4. * redistributing this file, you may do so under either license.
  5. *
  6. * GPL LICENSE SUMMARY
  7. *
  8. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  22. * USA
  23. *
  24. * The full GNU General Public License is included in this distribution
  25. * in the file called COPYING.
  26. *
  27. * Contact Information:
  28. * Intel Linux Wireless <ilw@linux.intel.com>
  29. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  30. *
  31. * BSD LICENSE
  32. *
  33. * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
  34. * All rights reserved.
  35. *
  36. * Redistribution and use in source and binary forms, with or without
  37. * modification, are permitted provided that the following conditions
  38. * are met:
  39. *
  40. * * Redistributions of source code must retain the above copyright
  41. * notice, this list of conditions and the following disclaimer.
  42. * * Redistributions in binary form must reproduce the above copyright
  43. * notice, this list of conditions and the following disclaimer in
  44. * the documentation and/or other materials provided with the
  45. * distribution.
  46. * * Neither the name Intel Corporation nor the names of its
  47. * contributors may be used to endorse or promote products derived
  48. * from this software without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  51. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  52. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  53. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  54. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  55. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  56. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  57. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  58. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  59. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. *****************************************************************************/
  63. #include <linux/etherdevice.h>
  64. #include <linux/ip.h>
  65. #include <linux/fs.h>
  66. #include <net/cfg80211.h>
  67. #include <net/ipv6.h>
  68. #include <net/tcp.h>
  69. #include <net/addrconf.h>
  70. #include "iwl-modparams.h"
  71. #include "fw-api.h"
  72. #include "mvm.h"
  73. void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
  74. struct ieee80211_vif *vif,
  75. struct cfg80211_gtk_rekey_data *data)
  76. {
  77. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  78. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  79. if (iwlwifi_mod_params.sw_crypto)
  80. return;
  81. mutex_lock(&mvm->mutex);
  82. memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
  83. memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
  84. mvmvif->rekey_data.replay_ctr =
  85. cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  86. mvmvif->rekey_data.valid = true;
  87. mutex_unlock(&mvm->mutex);
  88. }
  89. #if IS_ENABLED(CONFIG_IPV6)
  90. void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
  91. struct ieee80211_vif *vif,
  92. struct inet6_dev *idev)
  93. {
  94. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  95. struct inet6_ifaddr *ifa;
  96. int idx = 0;
  97. read_lock_bh(&idev->lock);
  98. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  99. mvmvif->target_ipv6_addrs[idx] = ifa->addr;
  100. idx++;
  101. if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_MAX)
  102. break;
  103. }
  104. read_unlock_bh(&idev->lock);
  105. mvmvif->num_target_ipv6_addrs = idx;
  106. }
  107. #endif
  108. void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
  109. struct ieee80211_vif *vif, int idx)
  110. {
  111. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  112. mvmvif->tx_key_idx = idx;
  113. }
  114. static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
  115. {
  116. int i;
  117. for (i = 0; i < IWL_P1K_SIZE; i++)
  118. out[i] = cpu_to_le16(p1k[i]);
  119. }
  120. struct wowlan_key_data {
  121. struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  122. struct iwl_wowlan_tkip_params_cmd *tkip;
  123. bool error, use_rsc_tsc, use_tkip;
  124. int wep_key_idx;
  125. };
  126. static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
  127. struct ieee80211_vif *vif,
  128. struct ieee80211_sta *sta,
  129. struct ieee80211_key_conf *key,
  130. void *_data)
  131. {
  132. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  133. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  134. struct wowlan_key_data *data = _data;
  135. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  136. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  137. struct iwl_p1k_cache *rx_p1ks;
  138. u8 *rx_mic_key;
  139. struct ieee80211_key_seq seq;
  140. u32 cur_rx_iv32 = 0;
  141. u16 p1k[IWL_P1K_SIZE];
  142. int ret, i;
  143. mutex_lock(&mvm->mutex);
  144. switch (key->cipher) {
  145. case WLAN_CIPHER_SUITE_WEP40:
  146. case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
  147. struct {
  148. struct iwl_mvm_wep_key_cmd wep_key_cmd;
  149. struct iwl_mvm_wep_key wep_key;
  150. } __packed wkc = {
  151. .wep_key_cmd.mac_id_n_color =
  152. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  153. mvmvif->color)),
  154. .wep_key_cmd.num_keys = 1,
  155. /* firmware sets STA_KEY_FLG_WEP_13BYTES */
  156. .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
  157. .wep_key.key_index = key->keyidx,
  158. .wep_key.key_size = key->keylen,
  159. };
  160. /*
  161. * This will fail -- the key functions don't set support
  162. * pairwise WEP keys. However, that's better than silently
  163. * failing WoWLAN. Or maybe not?
  164. */
  165. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  166. break;
  167. memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
  168. if (key->keyidx == mvmvif->tx_key_idx) {
  169. /* TX key must be at offset 0 */
  170. wkc.wep_key.key_offset = 0;
  171. } else {
  172. /* others start at 1 */
  173. data->wep_key_idx++;
  174. wkc.wep_key.key_offset = data->wep_key_idx;
  175. }
  176. ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, CMD_SYNC,
  177. sizeof(wkc), &wkc);
  178. data->error = ret != 0;
  179. mvm->ptk_ivlen = key->iv_len;
  180. mvm->ptk_icvlen = key->icv_len;
  181. mvm->gtk_ivlen = key->iv_len;
  182. mvm->gtk_icvlen = key->icv_len;
  183. /* don't upload key again */
  184. goto out_unlock;
  185. }
  186. default:
  187. data->error = true;
  188. goto out_unlock;
  189. case WLAN_CIPHER_SUITE_AES_CMAC:
  190. /*
  191. * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
  192. * but we also shouldn't abort suspend due to that. It does have
  193. * support for the IGTK key renewal, but doesn't really use the
  194. * IGTK for anything. This means we could spuriously wake up or
  195. * be deauthenticated, but that was considered acceptable.
  196. */
  197. goto out_unlock;
  198. case WLAN_CIPHER_SUITE_TKIP:
  199. if (sta) {
  200. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  201. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  202. rx_p1ks = data->tkip->rx_uni;
  203. ieee80211_get_key_tx_seq(key, &seq);
  204. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  205. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  206. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  207. iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
  208. memcpy(data->tkip->mic_keys.tx,
  209. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  210. IWL_MIC_KEY_SIZE);
  211. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  212. } else {
  213. tkip_sc =
  214. data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  215. rx_p1ks = data->tkip->rx_multi;
  216. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  217. }
  218. /*
  219. * For non-QoS this relies on the fact that both the uCode and
  220. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  221. * for checking the IV in the frames.
  222. */
  223. for (i = 0; i < IWL_NUM_RSC; i++) {
  224. ieee80211_get_key_rx_seq(key, i, &seq);
  225. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  226. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  227. /* wrapping isn't allowed, AP must rekey */
  228. if (seq.tkip.iv32 > cur_rx_iv32)
  229. cur_rx_iv32 = seq.tkip.iv32;
  230. }
  231. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  232. cur_rx_iv32, p1k);
  233. iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
  234. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  235. cur_rx_iv32 + 1, p1k);
  236. iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
  237. memcpy(rx_mic_key,
  238. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  239. IWL_MIC_KEY_SIZE);
  240. data->use_tkip = true;
  241. data->use_rsc_tsc = true;
  242. break;
  243. case WLAN_CIPHER_SUITE_CCMP:
  244. if (sta) {
  245. u8 *pn = seq.ccmp.pn;
  246. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  247. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  248. ieee80211_get_key_tx_seq(key, &seq);
  249. aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
  250. ((u64)pn[4] << 8) |
  251. ((u64)pn[3] << 16) |
  252. ((u64)pn[2] << 24) |
  253. ((u64)pn[1] << 32) |
  254. ((u64)pn[0] << 40));
  255. } else {
  256. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  257. }
  258. /*
  259. * For non-QoS this relies on the fact that both the uCode and
  260. * mac80211 use TID 0 for checking the IV in the frames.
  261. */
  262. for (i = 0; i < IWL_NUM_RSC; i++) {
  263. u8 *pn = seq.ccmp.pn;
  264. ieee80211_get_key_rx_seq(key, i, &seq);
  265. aes_sc->pn = cpu_to_le64((u64)pn[5] |
  266. ((u64)pn[4] << 8) |
  267. ((u64)pn[3] << 16) |
  268. ((u64)pn[2] << 24) |
  269. ((u64)pn[1] << 32) |
  270. ((u64)pn[0] << 40));
  271. }
  272. data->use_rsc_tsc = true;
  273. break;
  274. }
  275. /*
  276. * The D3 firmware hardcodes the key offset 0 as the key it uses
  277. * to transmit packets to the AP, i.e. the PTK.
  278. */
  279. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  280. key->hw_key_idx = 0;
  281. mvm->ptk_ivlen = key->iv_len;
  282. mvm->ptk_icvlen = key->icv_len;
  283. } else {
  284. /*
  285. * firmware only supports TSC/RSC for a single key,
  286. * so if there are multiple keep overwriting them
  287. * with new ones -- this relies on mac80211 doing
  288. * list_add_tail().
  289. */
  290. key->hw_key_idx = 1;
  291. mvm->gtk_ivlen = key->iv_len;
  292. mvm->gtk_icvlen = key->icv_len;
  293. }
  294. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
  295. data->error = ret != 0;
  296. out_unlock:
  297. mutex_unlock(&mvm->mutex);
  298. }
  299. static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
  300. struct cfg80211_wowlan *wowlan)
  301. {
  302. struct iwl_wowlan_patterns_cmd *pattern_cmd;
  303. struct iwl_host_cmd cmd = {
  304. .id = WOWLAN_PATTERNS,
  305. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  306. .flags = CMD_SYNC,
  307. };
  308. int i, err;
  309. if (!wowlan->n_patterns)
  310. return 0;
  311. cmd.len[0] = sizeof(*pattern_cmd) +
  312. wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
  313. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  314. if (!pattern_cmd)
  315. return -ENOMEM;
  316. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  317. for (i = 0; i < wowlan->n_patterns; i++) {
  318. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  319. memcpy(&pattern_cmd->patterns[i].mask,
  320. wowlan->patterns[i].mask, mask_len);
  321. memcpy(&pattern_cmd->patterns[i].pattern,
  322. wowlan->patterns[i].pattern,
  323. wowlan->patterns[i].pattern_len);
  324. pattern_cmd->patterns[i].mask_size = mask_len;
  325. pattern_cmd->patterns[i].pattern_size =
  326. wowlan->patterns[i].pattern_len;
  327. }
  328. cmd.data[0] = pattern_cmd;
  329. err = iwl_mvm_send_cmd(mvm, &cmd);
  330. kfree(pattern_cmd);
  331. return err;
  332. }
  333. static int iwl_mvm_send_proto_offload(struct iwl_mvm *mvm,
  334. struct ieee80211_vif *vif)
  335. {
  336. union {
  337. struct iwl_proto_offload_cmd_v1 v1;
  338. struct iwl_proto_offload_cmd_v2 v2;
  339. struct iwl_proto_offload_cmd_v3_small v3s;
  340. struct iwl_proto_offload_cmd_v3_large v3l;
  341. } cmd = {};
  342. struct iwl_host_cmd hcmd = {
  343. .id = PROT_OFFLOAD_CONFIG_CMD,
  344. .flags = CMD_SYNC,
  345. .data[0] = &cmd,
  346. .dataflags[0] = IWL_HCMD_DFL_DUP,
  347. };
  348. struct iwl_proto_offload_cmd_common *common;
  349. u32 enabled = 0, size;
  350. u32 capa_flags = mvm->fw->ucode_capa.flags;
  351. #if IS_ENABLED(CONFIG_IPV6)
  352. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  353. int i;
  354. if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_SMALL ||
  355. capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_LARGE) {
  356. struct iwl_ns_config *nsc;
  357. struct iwl_targ_addr *addrs;
  358. int n_nsc, n_addrs;
  359. int c;
  360. if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_SMALL) {
  361. nsc = cmd.v3s.ns_config;
  362. n_nsc = IWL_PROTO_OFFLOAD_NUM_NS_CONFIG_V3S;
  363. addrs = cmd.v3s.targ_addrs;
  364. n_addrs = IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V3S;
  365. } else {
  366. nsc = cmd.v3l.ns_config;
  367. n_nsc = IWL_PROTO_OFFLOAD_NUM_NS_CONFIG_V3L;
  368. addrs = cmd.v3l.targ_addrs;
  369. n_addrs = IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V3L;
  370. }
  371. if (mvmvif->num_target_ipv6_addrs)
  372. enabled |= IWL_D3_PROTO_OFFLOAD_NS;
  373. /*
  374. * For each address we have (and that will fit) fill a target
  375. * address struct and combine for NS offload structs with the
  376. * solicited node addresses.
  377. */
  378. for (i = 0, c = 0;
  379. i < mvmvif->num_target_ipv6_addrs &&
  380. i < n_addrs && c < n_nsc; i++) {
  381. struct in6_addr solicited_addr;
  382. int j;
  383. addrconf_addr_solict_mult(&mvmvif->target_ipv6_addrs[i],
  384. &solicited_addr);
  385. for (j = 0; j < c; j++)
  386. if (ipv6_addr_cmp(&nsc[j].dest_ipv6_addr,
  387. &solicited_addr) == 0)
  388. break;
  389. if (j == c)
  390. c++;
  391. addrs[i].addr = mvmvif->target_ipv6_addrs[i];
  392. addrs[i].config_num = cpu_to_le32(j);
  393. nsc[j].dest_ipv6_addr = solicited_addr;
  394. memcpy(nsc[j].target_mac_addr, vif->addr, ETH_ALEN);
  395. }
  396. if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_SMALL)
  397. cmd.v3s.num_valid_ipv6_addrs = cpu_to_le32(i);
  398. else
  399. cmd.v3l.num_valid_ipv6_addrs = cpu_to_le32(i);
  400. } else if (capa_flags & IWL_UCODE_TLV_FLAGS_D3_6_IPV6_ADDRS) {
  401. if (mvmvif->num_target_ipv6_addrs) {
  402. enabled |= IWL_D3_PROTO_OFFLOAD_NS;
  403. memcpy(cmd.v2.ndp_mac_addr, vif->addr, ETH_ALEN);
  404. }
  405. BUILD_BUG_ON(sizeof(cmd.v2.target_ipv6_addr[0]) !=
  406. sizeof(mvmvif->target_ipv6_addrs[0]));
  407. for (i = 0; i < min(mvmvif->num_target_ipv6_addrs,
  408. IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V2); i++)
  409. memcpy(cmd.v2.target_ipv6_addr[i],
  410. &mvmvif->target_ipv6_addrs[i],
  411. sizeof(cmd.v2.target_ipv6_addr[i]));
  412. } else {
  413. if (mvmvif->num_target_ipv6_addrs) {
  414. enabled |= IWL_D3_PROTO_OFFLOAD_NS;
  415. memcpy(cmd.v1.ndp_mac_addr, vif->addr, ETH_ALEN);
  416. }
  417. BUILD_BUG_ON(sizeof(cmd.v1.target_ipv6_addr[0]) !=
  418. sizeof(mvmvif->target_ipv6_addrs[0]));
  419. for (i = 0; i < min(mvmvif->num_target_ipv6_addrs,
  420. IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS_V1); i++)
  421. memcpy(cmd.v1.target_ipv6_addr[i],
  422. &mvmvif->target_ipv6_addrs[i],
  423. sizeof(cmd.v1.target_ipv6_addr[i]));
  424. }
  425. #endif
  426. if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_SMALL) {
  427. common = &cmd.v3s.common;
  428. size = sizeof(cmd.v3s);
  429. } else if (capa_flags & IWL_UCODE_TLV_FLAGS_NEW_NSOFFL_LARGE) {
  430. common = &cmd.v3l.common;
  431. size = sizeof(cmd.v3l);
  432. } else if (capa_flags & IWL_UCODE_TLV_FLAGS_D3_6_IPV6_ADDRS) {
  433. common = &cmd.v2.common;
  434. size = sizeof(cmd.v2);
  435. } else {
  436. common = &cmd.v1.common;
  437. size = sizeof(cmd.v1);
  438. }
  439. if (vif->bss_conf.arp_addr_cnt) {
  440. enabled |= IWL_D3_PROTO_OFFLOAD_ARP;
  441. common->host_ipv4_addr = vif->bss_conf.arp_addr_list[0];
  442. memcpy(common->arp_mac_addr, vif->addr, ETH_ALEN);
  443. }
  444. if (!enabled)
  445. return 0;
  446. common->enabled = cpu_to_le32(enabled);
  447. hcmd.len[0] = size;
  448. return iwl_mvm_send_cmd(mvm, &hcmd);
  449. }
  450. enum iwl_mvm_tcp_packet_type {
  451. MVM_TCP_TX_SYN,
  452. MVM_TCP_RX_SYNACK,
  453. MVM_TCP_TX_DATA,
  454. MVM_TCP_RX_ACK,
  455. MVM_TCP_RX_WAKE,
  456. MVM_TCP_TX_FIN,
  457. };
  458. static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
  459. {
  460. __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
  461. return cpu_to_le16(be16_to_cpu((__force __be16)check));
  462. }
  463. static void iwl_mvm_build_tcp_packet(struct ieee80211_vif *vif,
  464. struct cfg80211_wowlan_tcp *tcp,
  465. void *_pkt, u8 *mask,
  466. __le16 *pseudo_hdr_csum,
  467. enum iwl_mvm_tcp_packet_type ptype)
  468. {
  469. struct {
  470. struct ethhdr eth;
  471. struct iphdr ip;
  472. struct tcphdr tcp;
  473. u8 data[];
  474. } __packed *pkt = _pkt;
  475. u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  476. int i;
  477. pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
  478. pkt->ip.version = 4;
  479. pkt->ip.ihl = 5;
  480. pkt->ip.protocol = IPPROTO_TCP;
  481. switch (ptype) {
  482. case MVM_TCP_TX_SYN:
  483. case MVM_TCP_TX_DATA:
  484. case MVM_TCP_TX_FIN:
  485. memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
  486. memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
  487. pkt->ip.ttl = 128;
  488. pkt->ip.saddr = tcp->src;
  489. pkt->ip.daddr = tcp->dst;
  490. pkt->tcp.source = cpu_to_be16(tcp->src_port);
  491. pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
  492. /* overwritten for TX SYN later */
  493. pkt->tcp.doff = sizeof(struct tcphdr) / 4;
  494. pkt->tcp.window = cpu_to_be16(65000);
  495. break;
  496. case MVM_TCP_RX_SYNACK:
  497. case MVM_TCP_RX_ACK:
  498. case MVM_TCP_RX_WAKE:
  499. memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
  500. memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
  501. pkt->ip.saddr = tcp->dst;
  502. pkt->ip.daddr = tcp->src;
  503. pkt->tcp.source = cpu_to_be16(tcp->dst_port);
  504. pkt->tcp.dest = cpu_to_be16(tcp->src_port);
  505. break;
  506. default:
  507. WARN_ON(1);
  508. return;
  509. }
  510. switch (ptype) {
  511. case MVM_TCP_TX_SYN:
  512. /* firmware assumes 8 option bytes - 8 NOPs for now */
  513. memset(pkt->data, 0x01, 8);
  514. ip_tot_len += 8;
  515. pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
  516. pkt->tcp.syn = 1;
  517. break;
  518. case MVM_TCP_TX_DATA:
  519. ip_tot_len += tcp->payload_len;
  520. memcpy(pkt->data, tcp->payload, tcp->payload_len);
  521. pkt->tcp.psh = 1;
  522. pkt->tcp.ack = 1;
  523. break;
  524. case MVM_TCP_TX_FIN:
  525. pkt->tcp.fin = 1;
  526. pkt->tcp.ack = 1;
  527. break;
  528. case MVM_TCP_RX_SYNACK:
  529. pkt->tcp.syn = 1;
  530. pkt->tcp.ack = 1;
  531. break;
  532. case MVM_TCP_RX_ACK:
  533. pkt->tcp.ack = 1;
  534. break;
  535. case MVM_TCP_RX_WAKE:
  536. ip_tot_len += tcp->wake_len;
  537. pkt->tcp.psh = 1;
  538. pkt->tcp.ack = 1;
  539. memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
  540. break;
  541. }
  542. switch (ptype) {
  543. case MVM_TCP_TX_SYN:
  544. case MVM_TCP_TX_DATA:
  545. case MVM_TCP_TX_FIN:
  546. pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
  547. pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
  548. break;
  549. case MVM_TCP_RX_WAKE:
  550. for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
  551. u8 tmp = tcp->wake_mask[i];
  552. mask[i + 6] |= tmp << 6;
  553. if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
  554. mask[i + 7] = tmp >> 2;
  555. }
  556. /* fall through for ethernet/IP/TCP headers mask */
  557. case MVM_TCP_RX_SYNACK:
  558. case MVM_TCP_RX_ACK:
  559. mask[0] = 0xff; /* match ethernet */
  560. /*
  561. * match ethernet, ip.version, ip.ihl
  562. * the ip.ihl half byte is really masked out by firmware
  563. */
  564. mask[1] = 0x7f;
  565. mask[2] = 0x80; /* match ip.protocol */
  566. mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
  567. mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
  568. mask[5] = 0x80; /* match tcp flags */
  569. /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
  570. break;
  571. };
  572. *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
  573. pkt->ip.saddr, pkt->ip.daddr);
  574. }
  575. static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
  576. struct ieee80211_vif *vif,
  577. struct cfg80211_wowlan_tcp *tcp)
  578. {
  579. struct iwl_wowlan_remote_wake_config *cfg;
  580. struct iwl_host_cmd cmd = {
  581. .id = REMOTE_WAKE_CONFIG_CMD,
  582. .len = { sizeof(*cfg), },
  583. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  584. .flags = CMD_SYNC,
  585. };
  586. int ret;
  587. if (!tcp)
  588. return 0;
  589. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  590. if (!cfg)
  591. return -ENOMEM;
  592. cmd.data[0] = cfg;
  593. cfg->max_syn_retries = 10;
  594. cfg->max_data_retries = 10;
  595. cfg->tcp_syn_ack_timeout = 1; /* seconds */
  596. cfg->tcp_ack_timeout = 1; /* seconds */
  597. /* SYN (TX) */
  598. iwl_mvm_build_tcp_packet(
  599. vif, tcp, cfg->syn_tx.data, NULL,
  600. &cfg->syn_tx.info.tcp_pseudo_header_checksum,
  601. MVM_TCP_TX_SYN);
  602. cfg->syn_tx.info.tcp_payload_length = 0;
  603. /* SYN/ACK (RX) */
  604. iwl_mvm_build_tcp_packet(
  605. vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
  606. &cfg->synack_rx.info.tcp_pseudo_header_checksum,
  607. MVM_TCP_RX_SYNACK);
  608. cfg->synack_rx.info.tcp_payload_length = 0;
  609. /* KEEPALIVE/ACK (TX) */
  610. iwl_mvm_build_tcp_packet(
  611. vif, tcp, cfg->keepalive_tx.data, NULL,
  612. &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
  613. MVM_TCP_TX_DATA);
  614. cfg->keepalive_tx.info.tcp_payload_length =
  615. cpu_to_le16(tcp->payload_len);
  616. cfg->sequence_number_offset = tcp->payload_seq.offset;
  617. /* length must be 0..4, the field is little endian */
  618. cfg->sequence_number_length = tcp->payload_seq.len;
  619. cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
  620. cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
  621. if (tcp->payload_tok.len) {
  622. cfg->token_offset = tcp->payload_tok.offset;
  623. cfg->token_length = tcp->payload_tok.len;
  624. cfg->num_tokens =
  625. cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
  626. memcpy(cfg->tokens, tcp->payload_tok.token_stream,
  627. tcp->tokens_size);
  628. } else {
  629. /* set tokens to max value to almost never run out */
  630. cfg->num_tokens = cpu_to_le16(65535);
  631. }
  632. /* ACK (RX) */
  633. iwl_mvm_build_tcp_packet(
  634. vif, tcp, cfg->keepalive_ack_rx.data,
  635. cfg->keepalive_ack_rx.rx_mask,
  636. &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
  637. MVM_TCP_RX_ACK);
  638. cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
  639. /* WAKEUP (RX) */
  640. iwl_mvm_build_tcp_packet(
  641. vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
  642. &cfg->wake_rx.info.tcp_pseudo_header_checksum,
  643. MVM_TCP_RX_WAKE);
  644. cfg->wake_rx.info.tcp_payload_length =
  645. cpu_to_le16(tcp->wake_len);
  646. /* FIN */
  647. iwl_mvm_build_tcp_packet(
  648. vif, tcp, cfg->fin_tx.data, NULL,
  649. &cfg->fin_tx.info.tcp_pseudo_header_checksum,
  650. MVM_TCP_TX_FIN);
  651. cfg->fin_tx.info.tcp_payload_length = 0;
  652. ret = iwl_mvm_send_cmd(mvm, &cmd);
  653. kfree(cfg);
  654. return ret;
  655. }
  656. struct iwl_d3_iter_data {
  657. struct iwl_mvm *mvm;
  658. struct ieee80211_vif *vif;
  659. bool error;
  660. };
  661. static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
  662. struct ieee80211_vif *vif)
  663. {
  664. struct iwl_d3_iter_data *data = _data;
  665. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  666. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  667. return;
  668. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  669. return;
  670. if (data->vif) {
  671. IWL_ERR(data->mvm, "More than one managed interface active!\n");
  672. data->error = true;
  673. return;
  674. }
  675. data->vif = vif;
  676. }
  677. static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  678. struct ieee80211_sta *ap_sta)
  679. {
  680. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  681. struct ieee80211_chanctx_conf *ctx;
  682. u8 chains_static, chains_dynamic;
  683. struct cfg80211_chan_def chandef;
  684. int ret, i;
  685. struct iwl_binding_cmd binding_cmd = {};
  686. struct iwl_time_quota_cmd quota_cmd = {};
  687. u32 status;
  688. /* add back the PHY */
  689. if (WARN_ON(!mvmvif->phy_ctxt))
  690. return -EINVAL;
  691. rcu_read_lock();
  692. ctx = rcu_dereference(vif->chanctx_conf);
  693. if (WARN_ON(!ctx)) {
  694. rcu_read_unlock();
  695. return -EINVAL;
  696. }
  697. chandef = ctx->def;
  698. chains_static = ctx->rx_chains_static;
  699. chains_dynamic = ctx->rx_chains_dynamic;
  700. rcu_read_unlock();
  701. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  702. chains_static, chains_dynamic);
  703. if (ret)
  704. return ret;
  705. /* add back the MAC */
  706. mvmvif->uploaded = false;
  707. if (WARN_ON(!vif->bss_conf.assoc))
  708. return -EINVAL;
  709. /* hack */
  710. vif->bss_conf.assoc = false;
  711. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  712. vif->bss_conf.assoc = true;
  713. if (ret)
  714. return ret;
  715. /* add back binding - XXX refactor? */
  716. binding_cmd.id_and_color =
  717. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  718. mvmvif->phy_ctxt->color));
  719. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  720. binding_cmd.phy =
  721. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  722. mvmvif->phy_ctxt->color));
  723. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  724. mvmvif->color));
  725. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  726. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  727. status = 0;
  728. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  729. sizeof(binding_cmd), &binding_cmd,
  730. &status);
  731. if (ret) {
  732. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  733. return ret;
  734. }
  735. if (status) {
  736. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  737. return -EIO;
  738. }
  739. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
  740. if (ret)
  741. return ret;
  742. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  743. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  744. if (ret)
  745. return ret;
  746. /* and some quota */
  747. quota_cmd.quotas[0].id_and_color =
  748. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  749. mvmvif->phy_ctxt->color));
  750. quota_cmd.quotas[0].quota = cpu_to_le32(100);
  751. quota_cmd.quotas[0].max_duration = cpu_to_le32(1000);
  752. for (i = 1; i < MAX_BINDINGS; i++)
  753. quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  754. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
  755. sizeof(quota_cmd), &quota_cmd);
  756. if (ret)
  757. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  758. return 0;
  759. }
  760. static int iwl_mvm_get_last_nonqos_seq(struct iwl_mvm *mvm,
  761. struct ieee80211_vif *vif)
  762. {
  763. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  764. struct iwl_nonqos_seq_query_cmd query_cmd = {
  765. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_GET),
  766. .mac_id_n_color =
  767. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  768. mvmvif->color)),
  769. };
  770. struct iwl_host_cmd cmd = {
  771. .id = NON_QOS_TX_COUNTER_CMD,
  772. .flags = CMD_SYNC | CMD_WANT_SKB,
  773. };
  774. int err;
  775. u32 size;
  776. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API) {
  777. cmd.data[0] = &query_cmd;
  778. cmd.len[0] = sizeof(query_cmd);
  779. }
  780. err = iwl_mvm_send_cmd(mvm, &cmd);
  781. if (err)
  782. return err;
  783. size = iwl_rx_packet_payload_len(cmd.resp_pkt);
  784. if (size < sizeof(__le16)) {
  785. err = -EINVAL;
  786. } else {
  787. err = le16_to_cpup((__le16 *)cmd.resp_pkt->data);
  788. /* new API returns next, not last-used seqno */
  789. if (mvm->fw->ucode_capa.flags &
  790. IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API)
  791. err = (u16) (err - 0x10);
  792. }
  793. iwl_free_resp(&cmd);
  794. return err;
  795. }
  796. void iwl_mvm_set_last_nonqos_seq(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
  797. {
  798. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  799. struct iwl_nonqos_seq_query_cmd query_cmd = {
  800. .get_set_flag = cpu_to_le32(IWL_NONQOS_SEQ_SET),
  801. .mac_id_n_color =
  802. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  803. mvmvif->color)),
  804. .value = cpu_to_le16(mvmvif->seqno),
  805. };
  806. /* return if called during restart, not resume from D3 */
  807. if (!mvmvif->seqno_valid)
  808. return;
  809. mvmvif->seqno_valid = false;
  810. if (!(mvm->fw->ucode_capa.flags &
  811. IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API))
  812. return;
  813. if (iwl_mvm_send_cmd_pdu(mvm, NON_QOS_TX_COUNTER_CMD, CMD_SYNC,
  814. sizeof(query_cmd), &query_cmd))
  815. IWL_ERR(mvm, "failed to set non-QoS seqno\n");
  816. }
  817. static int __iwl_mvm_suspend(struct ieee80211_hw *hw,
  818. struct cfg80211_wowlan *wowlan,
  819. bool test)
  820. {
  821. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  822. struct iwl_d3_iter_data suspend_iter_data = {
  823. .mvm = mvm,
  824. };
  825. struct ieee80211_vif *vif;
  826. struct iwl_mvm_vif *mvmvif;
  827. struct ieee80211_sta *ap_sta;
  828. struct iwl_mvm_sta *mvm_ap_sta;
  829. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  830. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  831. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  832. struct iwl_d3_manager_config d3_cfg_cmd_data = {
  833. /*
  834. * Program the minimum sleep time to 10 seconds, as many
  835. * platforms have issues processing a wakeup signal while
  836. * still being in the process of suspending.
  837. */
  838. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  839. };
  840. struct iwl_host_cmd d3_cfg_cmd = {
  841. .id = D3_CONFIG_CMD,
  842. .flags = CMD_SYNC | CMD_WANT_SKB,
  843. .data[0] = &d3_cfg_cmd_data,
  844. .len[0] = sizeof(d3_cfg_cmd_data),
  845. };
  846. struct wowlan_key_data key_data = {
  847. .use_rsc_tsc = false,
  848. .tkip = &tkip_cmd,
  849. .use_tkip = false,
  850. };
  851. int ret, i;
  852. int len __maybe_unused;
  853. u8 old_aux_sta_id, old_ap_sta_id = IWL_MVM_STATION_COUNT;
  854. if (!wowlan) {
  855. /*
  856. * mac80211 shouldn't get here, but for D3 test
  857. * it doesn't warrant a warning
  858. */
  859. WARN_ON(!test);
  860. return -EINVAL;
  861. }
  862. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  863. if (!key_data.rsc_tsc)
  864. return -ENOMEM;
  865. mutex_lock(&mvm->mutex);
  866. old_aux_sta_id = mvm->aux_sta.sta_id;
  867. /* see if there's only a single BSS vif and it's associated */
  868. ieee80211_iterate_active_interfaces_atomic(
  869. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  870. iwl_mvm_d3_iface_iterator, &suspend_iter_data);
  871. if (suspend_iter_data.error || !suspend_iter_data.vif) {
  872. ret = 1;
  873. goto out_noreset;
  874. }
  875. vif = suspend_iter_data.vif;
  876. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  877. ap_sta = rcu_dereference_protected(
  878. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  879. lockdep_is_held(&mvm->mutex));
  880. if (IS_ERR_OR_NULL(ap_sta)) {
  881. ret = -EINVAL;
  882. goto out_noreset;
  883. }
  884. mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
  885. /* TODO: wowlan_config_cmd.wowlan_ba_teardown_tids */
  886. wowlan_config_cmd.is_11n_connection = ap_sta->ht_cap.ht_supported;
  887. /* Query the last used seqno and set it */
  888. ret = iwl_mvm_get_last_nonqos_seq(mvm, vif);
  889. if (ret < 0)
  890. goto out_noreset;
  891. wowlan_config_cmd.non_qos_seq = cpu_to_le16(ret);
  892. /*
  893. * For QoS counters, we store the one to use next, so subtract 0x10
  894. * since the uCode will add 0x10 *before* using the value while we
  895. * increment after using the value (i.e. store the next value to use).
  896. */
  897. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  898. u16 seq = mvm_ap_sta->tid_data[i].seq_number;
  899. seq -= 0x10;
  900. wowlan_config_cmd.qos_seq[i] = cpu_to_le16(seq);
  901. }
  902. if (wowlan->disconnect)
  903. wowlan_config_cmd.wakeup_filter |=
  904. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  905. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  906. if (wowlan->magic_pkt)
  907. wowlan_config_cmd.wakeup_filter |=
  908. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  909. if (wowlan->gtk_rekey_failure)
  910. wowlan_config_cmd.wakeup_filter |=
  911. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  912. if (wowlan->eap_identity_req)
  913. wowlan_config_cmd.wakeup_filter |=
  914. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  915. if (wowlan->four_way_handshake)
  916. wowlan_config_cmd.wakeup_filter |=
  917. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  918. if (wowlan->n_patterns)
  919. wowlan_config_cmd.wakeup_filter |=
  920. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  921. if (wowlan->rfkill_release)
  922. wowlan_config_cmd.wakeup_filter |=
  923. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  924. if (wowlan->tcp) {
  925. /*
  926. * Set the "link change" (really "link lost") flag as well
  927. * since that implies losing the TCP connection.
  928. */
  929. wowlan_config_cmd.wakeup_filter |=
  930. cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
  931. IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
  932. IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
  933. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  934. }
  935. iwl_mvm_cancel_scan(mvm);
  936. iwl_trans_stop_device(mvm->trans);
  937. /*
  938. * The D3 firmware still hardcodes the AP station ID for the
  939. * BSS we're associated with as 0. Store the real STA ID here
  940. * and assign 0. When we leave this function, we'll restore
  941. * the original value for the resume code.
  942. */
  943. old_ap_sta_id = mvm_ap_sta->sta_id;
  944. mvm_ap_sta->sta_id = 0;
  945. mvmvif->ap_sta_id = 0;
  946. /*
  947. * Set the HW restart bit -- this is mostly true as we're
  948. * going to load new firmware and reprogram that, though
  949. * the reprogramming is going to be manual to avoid adding
  950. * all the MACs that aren't support.
  951. * We don't have to clear up everything though because the
  952. * reprogramming is manual. When we resume, we'll actually
  953. * go through a proper restart sequence again to switch
  954. * back to the runtime firmware image.
  955. */
  956. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  957. /* We reprogram keys and shouldn't allocate new key indices */
  958. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  959. mvm->ptk_ivlen = 0;
  960. mvm->ptk_icvlen = 0;
  961. mvm->ptk_ivlen = 0;
  962. mvm->ptk_icvlen = 0;
  963. /*
  964. * The D3 firmware still hardcodes the AP station ID for the
  965. * BSS we're associated with as 0. As a result, we have to move
  966. * the auxiliary station to ID 1 so the ID 0 remains free for
  967. * the AP station for later.
  968. * We set the sta_id to 1 here, and reset it to its previous
  969. * value (that we stored above) later.
  970. */
  971. mvm->aux_sta.sta_id = 1;
  972. ret = iwl_mvm_load_d3_fw(mvm);
  973. if (ret)
  974. goto out;
  975. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  976. if (ret)
  977. goto out;
  978. if (!iwlwifi_mod_params.sw_crypto) {
  979. /*
  980. * This needs to be unlocked due to lock ordering
  981. * constraints. Since we're in the suspend path
  982. * that isn't really a problem though.
  983. */
  984. mutex_unlock(&mvm->mutex);
  985. ieee80211_iter_keys(mvm->hw, vif,
  986. iwl_mvm_wowlan_program_keys,
  987. &key_data);
  988. mutex_lock(&mvm->mutex);
  989. if (key_data.error) {
  990. ret = -EIO;
  991. goto out;
  992. }
  993. if (key_data.use_rsc_tsc) {
  994. struct iwl_host_cmd rsc_tsc_cmd = {
  995. .id = WOWLAN_TSC_RSC_PARAM,
  996. .flags = CMD_SYNC,
  997. .data[0] = key_data.rsc_tsc,
  998. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  999. .len[0] = sizeof(*key_data.rsc_tsc),
  1000. };
  1001. ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
  1002. if (ret)
  1003. goto out;
  1004. }
  1005. if (key_data.use_tkip) {
  1006. ret = iwl_mvm_send_cmd_pdu(mvm,
  1007. WOWLAN_TKIP_PARAM,
  1008. CMD_SYNC, sizeof(tkip_cmd),
  1009. &tkip_cmd);
  1010. if (ret)
  1011. goto out;
  1012. }
  1013. if (mvmvif->rekey_data.valid) {
  1014. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  1015. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  1016. NL80211_KCK_LEN);
  1017. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  1018. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  1019. NL80211_KEK_LEN);
  1020. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  1021. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  1022. ret = iwl_mvm_send_cmd_pdu(mvm,
  1023. WOWLAN_KEK_KCK_MATERIAL,
  1024. CMD_SYNC,
  1025. sizeof(kek_kck_cmd),
  1026. &kek_kck_cmd);
  1027. if (ret)
  1028. goto out;
  1029. }
  1030. }
  1031. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION,
  1032. CMD_SYNC, sizeof(wowlan_config_cmd),
  1033. &wowlan_config_cmd);
  1034. if (ret)
  1035. goto out;
  1036. ret = iwl_mvm_send_patterns(mvm, wowlan);
  1037. if (ret)
  1038. goto out;
  1039. ret = iwl_mvm_send_proto_offload(mvm, vif);
  1040. if (ret)
  1041. goto out;
  1042. ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
  1043. if (ret)
  1044. goto out;
  1045. ret = iwl_mvm_power_update_device_mode(mvm);
  1046. if (ret)
  1047. goto out;
  1048. ret = iwl_mvm_power_update_mode(mvm, vif);
  1049. if (ret)
  1050. goto out;
  1051. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1052. if (mvm->d3_wake_sysassert)
  1053. d3_cfg_cmd_data.wakeup_flags |=
  1054. cpu_to_le32(IWL_WAKEUP_D3_CONFIG_FW_ERROR);
  1055. #endif
  1056. /* must be last -- this switches firmware state */
  1057. ret = iwl_mvm_send_cmd(mvm, &d3_cfg_cmd);
  1058. if (ret)
  1059. goto out;
  1060. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1061. len = iwl_rx_packet_payload_len(d3_cfg_cmd.resp_pkt);
  1062. if (len >= sizeof(u32)) {
  1063. mvm->d3_test_pme_ptr =
  1064. le32_to_cpup((__le32 *)d3_cfg_cmd.resp_pkt->data);
  1065. }
  1066. #endif
  1067. iwl_free_resp(&d3_cfg_cmd);
  1068. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1069. iwl_trans_d3_suspend(mvm->trans, test);
  1070. out:
  1071. mvm->aux_sta.sta_id = old_aux_sta_id;
  1072. mvm_ap_sta->sta_id = old_ap_sta_id;
  1073. mvmvif->ap_sta_id = old_ap_sta_id;
  1074. if (ret < 0)
  1075. ieee80211_restart_hw(mvm->hw);
  1076. out_noreset:
  1077. kfree(key_data.rsc_tsc);
  1078. mutex_unlock(&mvm->mutex);
  1079. return ret;
  1080. }
  1081. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  1082. {
  1083. return __iwl_mvm_suspend(hw, wowlan, false);
  1084. }
  1085. /* converted data from the different status responses */
  1086. struct iwl_wowlan_status_data {
  1087. u16 pattern_number;
  1088. u16 qos_seq_ctr[8];
  1089. u32 wakeup_reasons;
  1090. u32 wake_packet_length;
  1091. u32 wake_packet_bufsize;
  1092. const u8 *wake_packet;
  1093. };
  1094. static void iwl_mvm_report_wakeup_reasons(struct iwl_mvm *mvm,
  1095. struct ieee80211_vif *vif,
  1096. struct iwl_wowlan_status_data *status)
  1097. {
  1098. struct sk_buff *pkt = NULL;
  1099. struct cfg80211_wowlan_wakeup wakeup = {
  1100. .pattern_idx = -1,
  1101. };
  1102. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  1103. u32 reasons = status->wakeup_reasons;
  1104. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  1105. wakeup_report = NULL;
  1106. goto report;
  1107. }
  1108. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  1109. wakeup.magic_pkt = true;
  1110. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  1111. wakeup.pattern_idx =
  1112. status->pattern_number;
  1113. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1114. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  1115. wakeup.disconnect = true;
  1116. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  1117. wakeup.gtk_rekey_failure = true;
  1118. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  1119. wakeup.rfkill_release = true;
  1120. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  1121. wakeup.eap_identity_req = true;
  1122. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  1123. wakeup.four_way_handshake = true;
  1124. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  1125. wakeup.tcp_connlost = true;
  1126. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  1127. wakeup.tcp_nomoretokens = true;
  1128. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  1129. wakeup.tcp_match = true;
  1130. if (status->wake_packet_bufsize) {
  1131. int pktsize = status->wake_packet_bufsize;
  1132. int pktlen = status->wake_packet_length;
  1133. const u8 *pktdata = status->wake_packet;
  1134. struct ieee80211_hdr *hdr = (void *)pktdata;
  1135. int truncated = pktlen - pktsize;
  1136. /* this would be a firmware bug */
  1137. if (WARN_ON_ONCE(truncated < 0))
  1138. truncated = 0;
  1139. if (ieee80211_is_data(hdr->frame_control)) {
  1140. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  1141. int ivlen = 0, icvlen = 4; /* also FCS */
  1142. pkt = alloc_skb(pktsize, GFP_KERNEL);
  1143. if (!pkt)
  1144. goto report;
  1145. memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
  1146. pktdata += hdrlen;
  1147. pktsize -= hdrlen;
  1148. if (ieee80211_has_protected(hdr->frame_control)) {
  1149. /*
  1150. * This is unlocked and using gtk_i(c)vlen,
  1151. * but since everything is under RTNL still
  1152. * that's not really a problem - changing
  1153. * it would be difficult.
  1154. */
  1155. if (is_multicast_ether_addr(hdr->addr1)) {
  1156. ivlen = mvm->gtk_ivlen;
  1157. icvlen += mvm->gtk_icvlen;
  1158. } else {
  1159. ivlen = mvm->ptk_ivlen;
  1160. icvlen += mvm->ptk_icvlen;
  1161. }
  1162. }
  1163. /* if truncated, FCS/ICV is (partially) gone */
  1164. if (truncated >= icvlen) {
  1165. icvlen = 0;
  1166. truncated -= icvlen;
  1167. } else {
  1168. icvlen -= truncated;
  1169. truncated = 0;
  1170. }
  1171. pktsize -= ivlen + icvlen;
  1172. pktdata += ivlen;
  1173. memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
  1174. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1175. goto report;
  1176. wakeup.packet = pkt->data;
  1177. wakeup.packet_present_len = pkt->len;
  1178. wakeup.packet_len = pkt->len - truncated;
  1179. wakeup.packet_80211 = false;
  1180. } else {
  1181. int fcslen = 4;
  1182. if (truncated >= 4) {
  1183. truncated -= 4;
  1184. fcslen = 0;
  1185. } else {
  1186. fcslen -= truncated;
  1187. truncated = 0;
  1188. }
  1189. pktsize -= fcslen;
  1190. wakeup.packet = status->wake_packet;
  1191. wakeup.packet_present_len = pktsize;
  1192. wakeup.packet_len = pktlen - truncated;
  1193. wakeup.packet_80211 = true;
  1194. }
  1195. }
  1196. report:
  1197. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1198. kfree_skb(pkt);
  1199. }
  1200. static void iwl_mvm_aes_sc_to_seq(struct aes_sc *sc,
  1201. struct ieee80211_key_seq *seq)
  1202. {
  1203. u64 pn;
  1204. pn = le64_to_cpu(sc->pn);
  1205. seq->ccmp.pn[0] = pn >> 40;
  1206. seq->ccmp.pn[1] = pn >> 32;
  1207. seq->ccmp.pn[2] = pn >> 24;
  1208. seq->ccmp.pn[3] = pn >> 16;
  1209. seq->ccmp.pn[4] = pn >> 8;
  1210. seq->ccmp.pn[5] = pn;
  1211. }
  1212. static void iwl_mvm_tkip_sc_to_seq(struct tkip_sc *sc,
  1213. struct ieee80211_key_seq *seq)
  1214. {
  1215. seq->tkip.iv32 = le32_to_cpu(sc->iv32);
  1216. seq->tkip.iv16 = le16_to_cpu(sc->iv16);
  1217. }
  1218. static void iwl_mvm_set_aes_rx_seq(struct aes_sc *scs,
  1219. struct ieee80211_key_conf *key)
  1220. {
  1221. int tid;
  1222. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1223. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1224. struct ieee80211_key_seq seq = {};
  1225. iwl_mvm_aes_sc_to_seq(&scs[tid], &seq);
  1226. ieee80211_set_key_rx_seq(key, tid, &seq);
  1227. }
  1228. }
  1229. static void iwl_mvm_set_tkip_rx_seq(struct tkip_sc *scs,
  1230. struct ieee80211_key_conf *key)
  1231. {
  1232. int tid;
  1233. BUILD_BUG_ON(IWL_NUM_RSC != IEEE80211_NUM_TIDS);
  1234. for (tid = 0; tid < IWL_NUM_RSC; tid++) {
  1235. struct ieee80211_key_seq seq = {};
  1236. iwl_mvm_tkip_sc_to_seq(&scs[tid], &seq);
  1237. ieee80211_set_key_rx_seq(key, tid, &seq);
  1238. }
  1239. }
  1240. static void iwl_mvm_set_key_rx_seq(struct ieee80211_key_conf *key,
  1241. struct iwl_wowlan_status_v6 *status)
  1242. {
  1243. union iwl_all_tsc_rsc *rsc = &status->gtk.rsc.all_tsc_rsc;
  1244. switch (key->cipher) {
  1245. case WLAN_CIPHER_SUITE_CCMP:
  1246. iwl_mvm_set_aes_rx_seq(rsc->aes.multicast_rsc, key);
  1247. break;
  1248. case WLAN_CIPHER_SUITE_TKIP:
  1249. iwl_mvm_set_tkip_rx_seq(rsc->tkip.multicast_rsc, key);
  1250. break;
  1251. default:
  1252. WARN_ON(1);
  1253. }
  1254. }
  1255. struct iwl_mvm_d3_gtk_iter_data {
  1256. struct iwl_wowlan_status_v6 *status;
  1257. void *last_gtk;
  1258. u32 cipher;
  1259. bool find_phase, unhandled_cipher;
  1260. int num_keys;
  1261. };
  1262. static void iwl_mvm_d3_update_gtks(struct ieee80211_hw *hw,
  1263. struct ieee80211_vif *vif,
  1264. struct ieee80211_sta *sta,
  1265. struct ieee80211_key_conf *key,
  1266. void *_data)
  1267. {
  1268. struct iwl_mvm_d3_gtk_iter_data *data = _data;
  1269. if (data->unhandled_cipher)
  1270. return;
  1271. switch (key->cipher) {
  1272. case WLAN_CIPHER_SUITE_WEP40:
  1273. case WLAN_CIPHER_SUITE_WEP104:
  1274. /* ignore WEP completely, nothing to do */
  1275. return;
  1276. case WLAN_CIPHER_SUITE_CCMP:
  1277. case WLAN_CIPHER_SUITE_TKIP:
  1278. /* we support these */
  1279. break;
  1280. default:
  1281. /* everything else (even CMAC for MFP) - disconnect from AP */
  1282. data->unhandled_cipher = true;
  1283. return;
  1284. }
  1285. data->num_keys++;
  1286. /*
  1287. * pairwise key - update sequence counters only;
  1288. * note that this assumes no TDLS sessions are active
  1289. */
  1290. if (sta) {
  1291. struct ieee80211_key_seq seq = {};
  1292. union iwl_all_tsc_rsc *sc = &data->status->gtk.rsc.all_tsc_rsc;
  1293. if (data->find_phase)
  1294. return;
  1295. switch (key->cipher) {
  1296. case WLAN_CIPHER_SUITE_CCMP:
  1297. iwl_mvm_aes_sc_to_seq(&sc->aes.tsc, &seq);
  1298. iwl_mvm_set_aes_rx_seq(sc->aes.unicast_rsc, key);
  1299. break;
  1300. case WLAN_CIPHER_SUITE_TKIP:
  1301. iwl_mvm_tkip_sc_to_seq(&sc->tkip.tsc, &seq);
  1302. iwl_mvm_set_tkip_rx_seq(sc->tkip.unicast_rsc, key);
  1303. break;
  1304. }
  1305. ieee80211_set_key_tx_seq(key, &seq);
  1306. /* that's it for this key */
  1307. return;
  1308. }
  1309. if (data->find_phase) {
  1310. data->last_gtk = key;
  1311. data->cipher = key->cipher;
  1312. return;
  1313. }
  1314. if (data->status->num_of_gtk_rekeys)
  1315. ieee80211_remove_key(key);
  1316. else if (data->last_gtk == key)
  1317. iwl_mvm_set_key_rx_seq(key, data->status);
  1318. }
  1319. static bool iwl_mvm_setup_connection_keep(struct iwl_mvm *mvm,
  1320. struct ieee80211_vif *vif,
  1321. struct iwl_wowlan_status_v6 *status)
  1322. {
  1323. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  1324. struct iwl_mvm_d3_gtk_iter_data gtkdata = {
  1325. .status = status,
  1326. };
  1327. u32 disconnection_reasons =
  1328. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  1329. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH;
  1330. if (!status || !vif->bss_conf.bssid)
  1331. return false;
  1332. if (le32_to_cpu(status->wakeup_reasons) & disconnection_reasons)
  1333. return false;
  1334. /* find last GTK that we used initially, if any */
  1335. gtkdata.find_phase = true;
  1336. ieee80211_iter_keys(mvm->hw, vif,
  1337. iwl_mvm_d3_update_gtks, &gtkdata);
  1338. /* not trying to keep connections with MFP/unhandled ciphers */
  1339. if (gtkdata.unhandled_cipher)
  1340. return false;
  1341. if (!gtkdata.num_keys)
  1342. goto out;
  1343. if (!gtkdata.last_gtk)
  1344. return false;
  1345. /*
  1346. * invalidate all other GTKs that might still exist and update
  1347. * the one that we used
  1348. */
  1349. gtkdata.find_phase = false;
  1350. ieee80211_iter_keys(mvm->hw, vif,
  1351. iwl_mvm_d3_update_gtks, &gtkdata);
  1352. if (status->num_of_gtk_rekeys) {
  1353. struct ieee80211_key_conf *key;
  1354. struct {
  1355. struct ieee80211_key_conf conf;
  1356. u8 key[32];
  1357. } conf = {
  1358. .conf.cipher = gtkdata.cipher,
  1359. .conf.keyidx = status->gtk.key_index,
  1360. };
  1361. switch (gtkdata.cipher) {
  1362. case WLAN_CIPHER_SUITE_CCMP:
  1363. conf.conf.keylen = WLAN_KEY_LEN_CCMP;
  1364. memcpy(conf.conf.key, status->gtk.decrypt_key,
  1365. WLAN_KEY_LEN_CCMP);
  1366. break;
  1367. case WLAN_CIPHER_SUITE_TKIP:
  1368. conf.conf.keylen = WLAN_KEY_LEN_TKIP;
  1369. memcpy(conf.conf.key, status->gtk.decrypt_key, 16);
  1370. /* leave TX MIC key zeroed, we don't use it anyway */
  1371. memcpy(conf.conf.key +
  1372. NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
  1373. status->gtk.tkip_mic_key, 8);
  1374. break;
  1375. }
  1376. key = ieee80211_gtk_rekey_add(vif, &conf.conf);
  1377. if (IS_ERR(key))
  1378. return false;
  1379. iwl_mvm_set_key_rx_seq(key, status);
  1380. }
  1381. if (status->num_of_gtk_rekeys) {
  1382. __be64 replay_ctr =
  1383. cpu_to_be64(le64_to_cpu(status->replay_ctr));
  1384. ieee80211_gtk_rekey_notify(vif, vif->bss_conf.bssid,
  1385. (void *)&replay_ctr, GFP_KERNEL);
  1386. }
  1387. out:
  1388. mvmvif->seqno_valid = true;
  1389. /* +0x10 because the set API expects next-to-use, not last-used */
  1390. mvmvif->seqno = le16_to_cpu(status->non_qos_seq_ctr) + 0x10;
  1391. return true;
  1392. }
  1393. /* releases the MVM mutex */
  1394. static bool iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  1395. struct ieee80211_vif *vif)
  1396. {
  1397. u32 base = mvm->error_event_table;
  1398. struct error_table_start {
  1399. /* cf. struct iwl_error_event_table */
  1400. u32 valid;
  1401. u32 error_id;
  1402. } err_info;
  1403. struct iwl_host_cmd cmd = {
  1404. .id = WOWLAN_GET_STATUSES,
  1405. .flags = CMD_SYNC | CMD_WANT_SKB,
  1406. };
  1407. struct iwl_wowlan_status_data status;
  1408. struct iwl_wowlan_status_v6 *status_v6;
  1409. int ret, len, status_size, i;
  1410. bool keep;
  1411. struct ieee80211_sta *ap_sta;
  1412. struct iwl_mvm_sta *mvm_ap_sta;
  1413. iwl_trans_read_mem_bytes(mvm->trans, base,
  1414. &err_info, sizeof(err_info));
  1415. if (err_info.valid) {
  1416. IWL_INFO(mvm, "error table is valid (%d)\n",
  1417. err_info.valid);
  1418. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  1419. struct cfg80211_wowlan_wakeup wakeup = {
  1420. .rfkill_release = true,
  1421. };
  1422. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  1423. GFP_KERNEL);
  1424. }
  1425. goto out_unlock;
  1426. }
  1427. /* only for tracing for now */
  1428. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, CMD_SYNC, 0, NULL);
  1429. if (ret)
  1430. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  1431. ret = iwl_mvm_send_cmd(mvm, &cmd);
  1432. if (ret) {
  1433. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  1434. goto out_unlock;
  1435. }
  1436. /* RF-kill already asserted again... */
  1437. if (!cmd.resp_pkt)
  1438. goto out_unlock;
  1439. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API)
  1440. status_size = sizeof(struct iwl_wowlan_status_v6);
  1441. else
  1442. status_size = sizeof(struct iwl_wowlan_status_v4);
  1443. len = iwl_rx_packet_payload_len(cmd.resp_pkt);
  1444. if (len < status_size) {
  1445. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1446. goto out_free_resp;
  1447. }
  1448. if (mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_D3_CONTINUITY_API) {
  1449. status_v6 = (void *)cmd.resp_pkt->data;
  1450. status.pattern_number = le16_to_cpu(status_v6->pattern_number);
  1451. for (i = 0; i < 8; i++)
  1452. status.qos_seq_ctr[i] =
  1453. le16_to_cpu(status_v6->qos_seq_ctr[i]);
  1454. status.wakeup_reasons = le32_to_cpu(status_v6->wakeup_reasons);
  1455. status.wake_packet_length =
  1456. le32_to_cpu(status_v6->wake_packet_length);
  1457. status.wake_packet_bufsize =
  1458. le32_to_cpu(status_v6->wake_packet_bufsize);
  1459. status.wake_packet = status_v6->wake_packet;
  1460. } else {
  1461. struct iwl_wowlan_status_v4 *status_v4;
  1462. status_v6 = NULL;
  1463. status_v4 = (void *)cmd.resp_pkt->data;
  1464. status.pattern_number = le16_to_cpu(status_v4->pattern_number);
  1465. for (i = 0; i < 8; i++)
  1466. status.qos_seq_ctr[i] =
  1467. le16_to_cpu(status_v4->qos_seq_ctr[i]);
  1468. status.wakeup_reasons = le32_to_cpu(status_v4->wakeup_reasons);
  1469. status.wake_packet_length =
  1470. le32_to_cpu(status_v4->wake_packet_length);
  1471. status.wake_packet_bufsize =
  1472. le32_to_cpu(status_v4->wake_packet_bufsize);
  1473. status.wake_packet = status_v4->wake_packet;
  1474. }
  1475. if (len != status_size + ALIGN(status.wake_packet_bufsize, 4)) {
  1476. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  1477. goto out_free_resp;
  1478. }
  1479. /* still at hard-coded place 0 for D3 image */
  1480. ap_sta = rcu_dereference_protected(
  1481. mvm->fw_id_to_mac_id[0],
  1482. lockdep_is_held(&mvm->mutex));
  1483. if (IS_ERR_OR_NULL(ap_sta))
  1484. goto out_free_resp;
  1485. mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
  1486. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  1487. u16 seq = status.qos_seq_ctr[i];
  1488. /* firmware stores last-used value, we store next value */
  1489. seq += 0x10;
  1490. mvm_ap_sta->tid_data[i].seq_number = seq;
  1491. }
  1492. /* now we have all the data we need, unlock to avoid mac80211 issues */
  1493. mutex_unlock(&mvm->mutex);
  1494. iwl_mvm_report_wakeup_reasons(mvm, vif, &status);
  1495. keep = iwl_mvm_setup_connection_keep(mvm, vif, status_v6);
  1496. iwl_free_resp(&cmd);
  1497. return keep;
  1498. out_free_resp:
  1499. iwl_free_resp(&cmd);
  1500. out_unlock:
  1501. mutex_unlock(&mvm->mutex);
  1502. return false;
  1503. }
  1504. static void iwl_mvm_read_d3_sram(struct iwl_mvm *mvm)
  1505. {
  1506. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1507. const struct fw_img *img = &mvm->fw->img[IWL_UCODE_WOWLAN];
  1508. u32 len = img->sec[IWL_UCODE_SECTION_DATA].len;
  1509. u32 offs = img->sec[IWL_UCODE_SECTION_DATA].offset;
  1510. if (!mvm->store_d3_resume_sram)
  1511. return;
  1512. if (!mvm->d3_resume_sram) {
  1513. mvm->d3_resume_sram = kzalloc(len, GFP_KERNEL);
  1514. if (!mvm->d3_resume_sram)
  1515. return;
  1516. }
  1517. iwl_trans_read_mem_bytes(mvm->trans, offs, mvm->d3_resume_sram, len);
  1518. #endif
  1519. }
  1520. static void iwl_mvm_d3_disconnect_iter(void *data, u8 *mac,
  1521. struct ieee80211_vif *vif)
  1522. {
  1523. /* skip the one we keep connection on */
  1524. if (data == vif)
  1525. return;
  1526. if (vif->type == NL80211_IFTYPE_STATION)
  1527. ieee80211_resume_disconnect(vif);
  1528. }
  1529. static int __iwl_mvm_resume(struct iwl_mvm *mvm, bool test)
  1530. {
  1531. struct iwl_d3_iter_data resume_iter_data = {
  1532. .mvm = mvm,
  1533. };
  1534. struct ieee80211_vif *vif = NULL;
  1535. int ret;
  1536. enum iwl_d3_status d3_status;
  1537. bool keep = false;
  1538. mutex_lock(&mvm->mutex);
  1539. /* get the BSS vif pointer again */
  1540. ieee80211_iterate_active_interfaces_atomic(
  1541. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1542. iwl_mvm_d3_iface_iterator, &resume_iter_data);
  1543. if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
  1544. goto out_unlock;
  1545. vif = resume_iter_data.vif;
  1546. ret = iwl_trans_d3_resume(mvm->trans, &d3_status, test);
  1547. if (ret)
  1548. goto out_unlock;
  1549. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1550. IWL_INFO(mvm, "Device was reset during suspend\n");
  1551. goto out_unlock;
  1552. }
  1553. /* query SRAM first in case we want event logging */
  1554. iwl_mvm_read_d3_sram(mvm);
  1555. keep = iwl_mvm_query_wakeup_reasons(mvm, vif);
  1556. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1557. if (keep)
  1558. mvm->keep_vif = vif;
  1559. #endif
  1560. /* has unlocked the mutex, so skip that */
  1561. goto out;
  1562. out_unlock:
  1563. mutex_unlock(&mvm->mutex);
  1564. out:
  1565. if (!test)
  1566. ieee80211_iterate_active_interfaces_rtnl(mvm->hw,
  1567. IEEE80211_IFACE_ITER_NORMAL,
  1568. iwl_mvm_d3_disconnect_iter, keep ? vif : NULL);
  1569. /* return 1 to reconfigure the device */
  1570. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1571. return 1;
  1572. }
  1573. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1574. {
  1575. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1576. return __iwl_mvm_resume(mvm, false);
  1577. }
  1578. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1579. {
  1580. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1581. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1582. }
  1583. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1584. static int iwl_mvm_d3_test_open(struct inode *inode, struct file *file)
  1585. {
  1586. struct iwl_mvm *mvm = inode->i_private;
  1587. int err;
  1588. if (mvm->d3_test_active)
  1589. return -EBUSY;
  1590. file->private_data = inode->i_private;
  1591. ieee80211_stop_queues(mvm->hw);
  1592. synchronize_net();
  1593. /* start pseudo D3 */
  1594. rtnl_lock();
  1595. err = __iwl_mvm_suspend(mvm->hw, mvm->hw->wiphy->wowlan_config, true);
  1596. rtnl_unlock();
  1597. if (err > 0)
  1598. err = -EINVAL;
  1599. if (err) {
  1600. ieee80211_wake_queues(mvm->hw);
  1601. return err;
  1602. }
  1603. mvm->d3_test_active = true;
  1604. mvm->keep_vif = NULL;
  1605. return 0;
  1606. }
  1607. static ssize_t iwl_mvm_d3_test_read(struct file *file, char __user *user_buf,
  1608. size_t count, loff_t *ppos)
  1609. {
  1610. struct iwl_mvm *mvm = file->private_data;
  1611. u32 pme_asserted;
  1612. while (true) {
  1613. /* read pme_ptr if available */
  1614. if (mvm->d3_test_pme_ptr) {
  1615. pme_asserted = iwl_trans_read_mem32(mvm->trans,
  1616. mvm->d3_test_pme_ptr);
  1617. if (pme_asserted)
  1618. break;
  1619. }
  1620. if (msleep_interruptible(100))
  1621. break;
  1622. }
  1623. return 0;
  1624. }
  1625. static void iwl_mvm_d3_test_disconn_work_iter(void *_data, u8 *mac,
  1626. struct ieee80211_vif *vif)
  1627. {
  1628. /* skip the one we keep connection on */
  1629. if (_data == vif)
  1630. return;
  1631. if (vif->type == NL80211_IFTYPE_STATION)
  1632. ieee80211_connection_loss(vif);
  1633. }
  1634. static int iwl_mvm_d3_test_release(struct inode *inode, struct file *file)
  1635. {
  1636. struct iwl_mvm *mvm = inode->i_private;
  1637. int remaining_time = 10;
  1638. mvm->d3_test_active = false;
  1639. __iwl_mvm_resume(mvm, true);
  1640. iwl_abort_notification_waits(&mvm->notif_wait);
  1641. ieee80211_restart_hw(mvm->hw);
  1642. /* wait for restart and disconnect all interfaces */
  1643. while (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
  1644. remaining_time > 0) {
  1645. remaining_time--;
  1646. msleep(1000);
  1647. }
  1648. if (remaining_time == 0)
  1649. IWL_ERR(mvm, "Timed out waiting for HW restart to finish!\n");
  1650. ieee80211_iterate_active_interfaces_atomic(
  1651. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1652. iwl_mvm_d3_test_disconn_work_iter, mvm->keep_vif);
  1653. ieee80211_wake_queues(mvm->hw);
  1654. return 0;
  1655. }
  1656. const struct file_operations iwl_dbgfs_d3_test_ops = {
  1657. .llseek = no_llseek,
  1658. .open = iwl_mvm_d3_test_open,
  1659. .read = iwl_mvm_d3_test_read,
  1660. .release = iwl_mvm_d3_test_release,
  1661. };
  1662. #endif