d3.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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 - 2013 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 - 2013 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 <net/cfg80211.h>
  66. #include <net/ipv6.h>
  67. #include <net/tcp.h>
  68. #include "iwl-modparams.h"
  69. #include "fw-api.h"
  70. #include "mvm.h"
  71. void iwl_mvm_set_rekey_data(struct ieee80211_hw *hw,
  72. struct ieee80211_vif *vif,
  73. struct cfg80211_gtk_rekey_data *data)
  74. {
  75. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  76. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  77. if (iwlwifi_mod_params.sw_crypto)
  78. return;
  79. mutex_lock(&mvm->mutex);
  80. memcpy(mvmvif->rekey_data.kek, data->kek, NL80211_KEK_LEN);
  81. memcpy(mvmvif->rekey_data.kck, data->kck, NL80211_KCK_LEN);
  82. mvmvif->rekey_data.replay_ctr =
  83. cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  84. mvmvif->rekey_data.valid = true;
  85. mutex_unlock(&mvm->mutex);
  86. }
  87. #if IS_ENABLED(CONFIG_IPV6)
  88. void iwl_mvm_ipv6_addr_change(struct ieee80211_hw *hw,
  89. struct ieee80211_vif *vif,
  90. struct inet6_dev *idev)
  91. {
  92. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  93. struct inet6_ifaddr *ifa;
  94. int idx = 0;
  95. read_lock_bh(&idev->lock);
  96. list_for_each_entry(ifa, &idev->addr_list, if_list) {
  97. mvmvif->target_ipv6_addrs[idx] = ifa->addr;
  98. idx++;
  99. if (idx >= IWL_PROTO_OFFLOAD_NUM_IPV6_ADDRS)
  100. break;
  101. }
  102. read_unlock_bh(&idev->lock);
  103. mvmvif->num_target_ipv6_addrs = idx;
  104. }
  105. #endif
  106. void iwl_mvm_set_default_unicast_key(struct ieee80211_hw *hw,
  107. struct ieee80211_vif *vif, int idx)
  108. {
  109. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  110. mvmvif->tx_key_idx = idx;
  111. }
  112. static void iwl_mvm_convert_p1k(u16 *p1k, __le16 *out)
  113. {
  114. int i;
  115. for (i = 0; i < IWL_P1K_SIZE; i++)
  116. out[i] = cpu_to_le16(p1k[i]);
  117. }
  118. struct wowlan_key_data {
  119. struct iwl_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  120. struct iwl_wowlan_tkip_params_cmd *tkip;
  121. bool error, use_rsc_tsc, use_tkip;
  122. int gtk_key_idx;
  123. };
  124. static void iwl_mvm_wowlan_program_keys(struct ieee80211_hw *hw,
  125. struct ieee80211_vif *vif,
  126. struct ieee80211_sta *sta,
  127. struct ieee80211_key_conf *key,
  128. void *_data)
  129. {
  130. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  131. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  132. struct wowlan_key_data *data = _data;
  133. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  134. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  135. struct iwl_p1k_cache *rx_p1ks;
  136. u8 *rx_mic_key;
  137. struct ieee80211_key_seq seq;
  138. u32 cur_rx_iv32 = 0;
  139. u16 p1k[IWL_P1K_SIZE];
  140. int ret, i;
  141. mutex_lock(&mvm->mutex);
  142. switch (key->cipher) {
  143. case WLAN_CIPHER_SUITE_WEP40:
  144. case WLAN_CIPHER_SUITE_WEP104: { /* hack it for now */
  145. struct {
  146. struct iwl_mvm_wep_key_cmd wep_key_cmd;
  147. struct iwl_mvm_wep_key wep_key;
  148. } __packed wkc = {
  149. .wep_key_cmd.mac_id_n_color =
  150. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  151. mvmvif->color)),
  152. .wep_key_cmd.num_keys = 1,
  153. /* firmware sets STA_KEY_FLG_WEP_13BYTES */
  154. .wep_key_cmd.decryption_type = STA_KEY_FLG_WEP,
  155. .wep_key.key_index = key->keyidx,
  156. .wep_key.key_size = key->keylen,
  157. };
  158. /*
  159. * This will fail -- the key functions don't set support
  160. * pairwise WEP keys. However, that's better than silently
  161. * failing WoWLAN. Or maybe not?
  162. */
  163. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
  164. break;
  165. memcpy(&wkc.wep_key.key[3], key->key, key->keylen);
  166. if (key->keyidx == mvmvif->tx_key_idx) {
  167. /* TX key must be at offset 0 */
  168. wkc.wep_key.key_offset = 0;
  169. } else {
  170. /* others start at 1 */
  171. data->gtk_key_idx++;
  172. wkc.wep_key.key_offset = data->gtk_key_idx;
  173. }
  174. ret = iwl_mvm_send_cmd_pdu(mvm, WEP_KEY, CMD_SYNC,
  175. sizeof(wkc), &wkc);
  176. data->error = ret != 0;
  177. mvm->ptk_ivlen = key->iv_len;
  178. mvm->ptk_icvlen = key->icv_len;
  179. mvm->gtk_ivlen = key->iv_len;
  180. mvm->gtk_icvlen = key->icv_len;
  181. /* don't upload key again */
  182. goto out_unlock;
  183. }
  184. default:
  185. data->error = true;
  186. goto out_unlock;
  187. case WLAN_CIPHER_SUITE_AES_CMAC:
  188. /*
  189. * Ignore CMAC keys -- the WoWLAN firmware doesn't support them
  190. * but we also shouldn't abort suspend due to that. It does have
  191. * support for the IGTK key renewal, but doesn't really use the
  192. * IGTK for anything. This means we could spuriously wake up or
  193. * be deauthenticated, but that was considered acceptable.
  194. */
  195. goto out_unlock;
  196. case WLAN_CIPHER_SUITE_TKIP:
  197. if (sta) {
  198. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  199. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  200. rx_p1ks = data->tkip->rx_uni;
  201. ieee80211_get_key_tx_seq(key, &seq);
  202. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  203. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  204. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  205. iwl_mvm_convert_p1k(p1k, data->tkip->tx.p1k);
  206. memcpy(data->tkip->mic_keys.tx,
  207. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  208. IWL_MIC_KEY_SIZE);
  209. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  210. } else {
  211. tkip_sc =
  212. data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  213. rx_p1ks = data->tkip->rx_multi;
  214. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  215. }
  216. /*
  217. * For non-QoS this relies on the fact that both the uCode and
  218. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  219. * for checking the IV in the frames.
  220. */
  221. for (i = 0; i < IWL_NUM_RSC; i++) {
  222. ieee80211_get_key_rx_seq(key, i, &seq);
  223. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  224. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  225. /* wrapping isn't allowed, AP must rekey */
  226. if (seq.tkip.iv32 > cur_rx_iv32)
  227. cur_rx_iv32 = seq.tkip.iv32;
  228. }
  229. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  230. cur_rx_iv32, p1k);
  231. iwl_mvm_convert_p1k(p1k, rx_p1ks[0].p1k);
  232. ieee80211_get_tkip_rx_p1k(key, vif->bss_conf.bssid,
  233. cur_rx_iv32 + 1, p1k);
  234. iwl_mvm_convert_p1k(p1k, rx_p1ks[1].p1k);
  235. memcpy(rx_mic_key,
  236. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  237. IWL_MIC_KEY_SIZE);
  238. data->use_tkip = true;
  239. data->use_rsc_tsc = true;
  240. break;
  241. case WLAN_CIPHER_SUITE_CCMP:
  242. if (sta) {
  243. u8 *pn = seq.ccmp.pn;
  244. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  245. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  246. ieee80211_get_key_tx_seq(key, &seq);
  247. aes_tx_sc->pn = cpu_to_le64((u64)pn[5] |
  248. ((u64)pn[4] << 8) |
  249. ((u64)pn[3] << 16) |
  250. ((u64)pn[2] << 24) |
  251. ((u64)pn[1] << 32) |
  252. ((u64)pn[0] << 40));
  253. } else {
  254. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  255. }
  256. /*
  257. * For non-QoS this relies on the fact that both the uCode and
  258. * mac80211 use TID 0 for checking the IV in the frames.
  259. */
  260. for (i = 0; i < IWL_NUM_RSC; i++) {
  261. u8 *pn = seq.ccmp.pn;
  262. ieee80211_get_key_rx_seq(key, i, &seq);
  263. aes_sc->pn = cpu_to_le64((u64)pn[5] |
  264. ((u64)pn[4] << 8) |
  265. ((u64)pn[3] << 16) |
  266. ((u64)pn[2] << 24) |
  267. ((u64)pn[1] << 32) |
  268. ((u64)pn[0] << 40));
  269. }
  270. data->use_rsc_tsc = true;
  271. break;
  272. }
  273. /*
  274. * The D3 firmware hardcodes the key offset 0 as the key it uses
  275. * to transmit packets to the AP, i.e. the PTK.
  276. */
  277. if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE) {
  278. key->hw_key_idx = 0;
  279. mvm->ptk_ivlen = key->iv_len;
  280. mvm->ptk_icvlen = key->icv_len;
  281. } else {
  282. data->gtk_key_idx++;
  283. key->hw_key_idx = data->gtk_key_idx;
  284. mvm->gtk_ivlen = key->iv_len;
  285. mvm->gtk_icvlen = key->icv_len;
  286. }
  287. ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, true);
  288. data->error = ret != 0;
  289. out_unlock:
  290. mutex_unlock(&mvm->mutex);
  291. }
  292. static int iwl_mvm_send_patterns(struct iwl_mvm *mvm,
  293. struct cfg80211_wowlan *wowlan)
  294. {
  295. struct iwl_wowlan_patterns_cmd *pattern_cmd;
  296. struct iwl_host_cmd cmd = {
  297. .id = WOWLAN_PATTERNS,
  298. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  299. .flags = CMD_SYNC,
  300. };
  301. int i, err;
  302. if (!wowlan->n_patterns)
  303. return 0;
  304. cmd.len[0] = sizeof(*pattern_cmd) +
  305. wowlan->n_patterns * sizeof(struct iwl_wowlan_pattern);
  306. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  307. if (!pattern_cmd)
  308. return -ENOMEM;
  309. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  310. for (i = 0; i < wowlan->n_patterns; i++) {
  311. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  312. memcpy(&pattern_cmd->patterns[i].mask,
  313. wowlan->patterns[i].mask, mask_len);
  314. memcpy(&pattern_cmd->patterns[i].pattern,
  315. wowlan->patterns[i].pattern,
  316. wowlan->patterns[i].pattern_len);
  317. pattern_cmd->patterns[i].mask_size = mask_len;
  318. pattern_cmd->patterns[i].pattern_size =
  319. wowlan->patterns[i].pattern_len;
  320. }
  321. cmd.data[0] = pattern_cmd;
  322. err = iwl_mvm_send_cmd(mvm, &cmd);
  323. kfree(pattern_cmd);
  324. return err;
  325. }
  326. static int iwl_mvm_send_proto_offload(struct iwl_mvm *mvm,
  327. struct ieee80211_vif *vif)
  328. {
  329. struct iwl_proto_offload_cmd cmd = {};
  330. #if IS_ENABLED(CONFIG_IPV6)
  331. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  332. int i;
  333. if (mvmvif->num_target_ipv6_addrs) {
  334. cmd.enabled |= cpu_to_le32(IWL_D3_PROTO_OFFLOAD_NS);
  335. memcpy(cmd.ndp_mac_addr, vif->addr, ETH_ALEN);
  336. }
  337. BUILD_BUG_ON(sizeof(cmd.target_ipv6_addr[i]) !=
  338. sizeof(mvmvif->target_ipv6_addrs[i]));
  339. for (i = 0; i < mvmvif->num_target_ipv6_addrs; i++)
  340. memcpy(cmd.target_ipv6_addr[i],
  341. &mvmvif->target_ipv6_addrs[i],
  342. sizeof(cmd.target_ipv6_addr[i]));
  343. #endif
  344. if (vif->bss_conf.arp_addr_cnt) {
  345. cmd.enabled |= cpu_to_le32(IWL_D3_PROTO_OFFLOAD_ARP);
  346. cmd.host_ipv4_addr = vif->bss_conf.arp_addr_list[0];
  347. memcpy(cmd.arp_mac_addr, vif->addr, ETH_ALEN);
  348. }
  349. if (!cmd.enabled)
  350. return 0;
  351. return iwl_mvm_send_cmd_pdu(mvm, PROT_OFFLOAD_CONFIG_CMD, CMD_SYNC,
  352. sizeof(cmd), &cmd);
  353. }
  354. enum iwl_mvm_tcp_packet_type {
  355. MVM_TCP_TX_SYN,
  356. MVM_TCP_RX_SYNACK,
  357. MVM_TCP_TX_DATA,
  358. MVM_TCP_RX_ACK,
  359. MVM_TCP_RX_WAKE,
  360. MVM_TCP_TX_FIN,
  361. };
  362. static __le16 pseudo_hdr_check(int len, __be32 saddr, __be32 daddr)
  363. {
  364. __sum16 check = tcp_v4_check(len, saddr, daddr, 0);
  365. return cpu_to_le16(be16_to_cpu((__force __be16)check));
  366. }
  367. static void iwl_mvm_build_tcp_packet(struct iwl_mvm *mvm,
  368. struct ieee80211_vif *vif,
  369. struct cfg80211_wowlan_tcp *tcp,
  370. void *_pkt, u8 *mask,
  371. __le16 *pseudo_hdr_csum,
  372. enum iwl_mvm_tcp_packet_type ptype)
  373. {
  374. struct {
  375. struct ethhdr eth;
  376. struct iphdr ip;
  377. struct tcphdr tcp;
  378. u8 data[];
  379. } __packed *pkt = _pkt;
  380. u16 ip_tot_len = sizeof(struct iphdr) + sizeof(struct tcphdr);
  381. int i;
  382. pkt->eth.h_proto = cpu_to_be16(ETH_P_IP),
  383. pkt->ip.version = 4;
  384. pkt->ip.ihl = 5;
  385. pkt->ip.protocol = IPPROTO_TCP;
  386. switch (ptype) {
  387. case MVM_TCP_TX_SYN:
  388. case MVM_TCP_TX_DATA:
  389. case MVM_TCP_TX_FIN:
  390. memcpy(pkt->eth.h_dest, tcp->dst_mac, ETH_ALEN);
  391. memcpy(pkt->eth.h_source, vif->addr, ETH_ALEN);
  392. pkt->ip.ttl = 128;
  393. pkt->ip.saddr = tcp->src;
  394. pkt->ip.daddr = tcp->dst;
  395. pkt->tcp.source = cpu_to_be16(tcp->src_port);
  396. pkt->tcp.dest = cpu_to_be16(tcp->dst_port);
  397. /* overwritten for TX SYN later */
  398. pkt->tcp.doff = sizeof(struct tcphdr) / 4;
  399. pkt->tcp.window = cpu_to_be16(65000);
  400. break;
  401. case MVM_TCP_RX_SYNACK:
  402. case MVM_TCP_RX_ACK:
  403. case MVM_TCP_RX_WAKE:
  404. memcpy(pkt->eth.h_dest, vif->addr, ETH_ALEN);
  405. memcpy(pkt->eth.h_source, tcp->dst_mac, ETH_ALEN);
  406. pkt->ip.saddr = tcp->dst;
  407. pkt->ip.daddr = tcp->src;
  408. pkt->tcp.source = cpu_to_be16(tcp->dst_port);
  409. pkt->tcp.dest = cpu_to_be16(tcp->src_port);
  410. break;
  411. default:
  412. WARN_ON(1);
  413. return;
  414. }
  415. switch (ptype) {
  416. case MVM_TCP_TX_SYN:
  417. /* firmware assumes 8 option bytes - 8 NOPs for now */
  418. memset(pkt->data, 0x01, 8);
  419. ip_tot_len += 8;
  420. pkt->tcp.doff = (sizeof(struct tcphdr) + 8) / 4;
  421. pkt->tcp.syn = 1;
  422. break;
  423. case MVM_TCP_TX_DATA:
  424. ip_tot_len += tcp->payload_len;
  425. memcpy(pkt->data, tcp->payload, tcp->payload_len);
  426. pkt->tcp.psh = 1;
  427. pkt->tcp.ack = 1;
  428. break;
  429. case MVM_TCP_TX_FIN:
  430. pkt->tcp.fin = 1;
  431. pkt->tcp.ack = 1;
  432. break;
  433. case MVM_TCP_RX_SYNACK:
  434. pkt->tcp.syn = 1;
  435. pkt->tcp.ack = 1;
  436. break;
  437. case MVM_TCP_RX_ACK:
  438. pkt->tcp.ack = 1;
  439. break;
  440. case MVM_TCP_RX_WAKE:
  441. ip_tot_len += tcp->wake_len;
  442. pkt->tcp.psh = 1;
  443. pkt->tcp.ack = 1;
  444. memcpy(pkt->data, tcp->wake_data, tcp->wake_len);
  445. break;
  446. }
  447. switch (ptype) {
  448. case MVM_TCP_TX_SYN:
  449. case MVM_TCP_TX_DATA:
  450. case MVM_TCP_TX_FIN:
  451. pkt->ip.tot_len = cpu_to_be16(ip_tot_len);
  452. pkt->ip.check = ip_fast_csum(&pkt->ip, pkt->ip.ihl);
  453. break;
  454. case MVM_TCP_RX_WAKE:
  455. for (i = 0; i < DIV_ROUND_UP(tcp->wake_len, 8); i++) {
  456. u8 tmp = tcp->wake_mask[i];
  457. mask[i + 6] |= tmp << 6;
  458. if (i + 1 < DIV_ROUND_UP(tcp->wake_len, 8))
  459. mask[i + 7] = tmp >> 2;
  460. }
  461. /* fall through for ethernet/IP/TCP headers mask */
  462. case MVM_TCP_RX_SYNACK:
  463. case MVM_TCP_RX_ACK:
  464. mask[0] = 0xff; /* match ethernet */
  465. /*
  466. * match ethernet, ip.version, ip.ihl
  467. * the ip.ihl half byte is really masked out by firmware
  468. */
  469. mask[1] = 0x7f;
  470. mask[2] = 0x80; /* match ip.protocol */
  471. mask[3] = 0xfc; /* match ip.saddr, ip.daddr */
  472. mask[4] = 0x3f; /* match ip.daddr, tcp.source, tcp.dest */
  473. mask[5] = 0x80; /* match tcp flags */
  474. /* leave rest (0 or set for MVM_TCP_RX_WAKE) */
  475. break;
  476. };
  477. *pseudo_hdr_csum = pseudo_hdr_check(ip_tot_len - sizeof(struct iphdr),
  478. pkt->ip.saddr, pkt->ip.daddr);
  479. }
  480. static int iwl_mvm_send_remote_wake_cfg(struct iwl_mvm *mvm,
  481. struct ieee80211_vif *vif,
  482. struct cfg80211_wowlan_tcp *tcp)
  483. {
  484. struct iwl_wowlan_remote_wake_config *cfg;
  485. struct iwl_host_cmd cmd = {
  486. .id = REMOTE_WAKE_CONFIG_CMD,
  487. .len = { sizeof(*cfg), },
  488. .dataflags = { IWL_HCMD_DFL_NOCOPY, },
  489. .flags = CMD_SYNC,
  490. };
  491. int ret;
  492. if (!tcp)
  493. return 0;
  494. cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
  495. if (!cfg)
  496. return -ENOMEM;
  497. cmd.data[0] = cfg;
  498. cfg->max_syn_retries = 10;
  499. cfg->max_data_retries = 10;
  500. cfg->tcp_syn_ack_timeout = 1; /* seconds */
  501. cfg->tcp_ack_timeout = 1; /* seconds */
  502. /* SYN (TX) */
  503. iwl_mvm_build_tcp_packet(
  504. mvm, vif, tcp, cfg->syn_tx.data, NULL,
  505. &cfg->syn_tx.info.tcp_pseudo_header_checksum,
  506. MVM_TCP_TX_SYN);
  507. cfg->syn_tx.info.tcp_payload_length = 0;
  508. /* SYN/ACK (RX) */
  509. iwl_mvm_build_tcp_packet(
  510. mvm, vif, tcp, cfg->synack_rx.data, cfg->synack_rx.rx_mask,
  511. &cfg->synack_rx.info.tcp_pseudo_header_checksum,
  512. MVM_TCP_RX_SYNACK);
  513. cfg->synack_rx.info.tcp_payload_length = 0;
  514. /* KEEPALIVE/ACK (TX) */
  515. iwl_mvm_build_tcp_packet(
  516. mvm, vif, tcp, cfg->keepalive_tx.data, NULL,
  517. &cfg->keepalive_tx.info.tcp_pseudo_header_checksum,
  518. MVM_TCP_TX_DATA);
  519. cfg->keepalive_tx.info.tcp_payload_length =
  520. cpu_to_le16(tcp->payload_len);
  521. cfg->sequence_number_offset = tcp->payload_seq.offset;
  522. /* length must be 0..4, the field is little endian */
  523. cfg->sequence_number_length = tcp->payload_seq.len;
  524. cfg->initial_sequence_number = cpu_to_le32(tcp->payload_seq.start);
  525. cfg->keepalive_interval = cpu_to_le16(tcp->data_interval);
  526. if (tcp->payload_tok.len) {
  527. cfg->token_offset = tcp->payload_tok.offset;
  528. cfg->token_length = tcp->payload_tok.len;
  529. cfg->num_tokens =
  530. cpu_to_le16(tcp->tokens_size % tcp->payload_tok.len);
  531. memcpy(cfg->tokens, tcp->payload_tok.token_stream,
  532. tcp->tokens_size);
  533. } else {
  534. /* set tokens to max value to almost never run out */
  535. cfg->num_tokens = cpu_to_le16(65535);
  536. }
  537. /* ACK (RX) */
  538. iwl_mvm_build_tcp_packet(
  539. mvm, vif, tcp, cfg->keepalive_ack_rx.data,
  540. cfg->keepalive_ack_rx.rx_mask,
  541. &cfg->keepalive_ack_rx.info.tcp_pseudo_header_checksum,
  542. MVM_TCP_RX_ACK);
  543. cfg->keepalive_ack_rx.info.tcp_payload_length = 0;
  544. /* WAKEUP (RX) */
  545. iwl_mvm_build_tcp_packet(
  546. mvm, vif, tcp, cfg->wake_rx.data, cfg->wake_rx.rx_mask,
  547. &cfg->wake_rx.info.tcp_pseudo_header_checksum,
  548. MVM_TCP_RX_WAKE);
  549. cfg->wake_rx.info.tcp_payload_length =
  550. cpu_to_le16(tcp->wake_len);
  551. /* FIN */
  552. iwl_mvm_build_tcp_packet(
  553. mvm, vif, tcp, cfg->fin_tx.data, NULL,
  554. &cfg->fin_tx.info.tcp_pseudo_header_checksum,
  555. MVM_TCP_TX_FIN);
  556. cfg->fin_tx.info.tcp_payload_length = 0;
  557. ret = iwl_mvm_send_cmd(mvm, &cmd);
  558. kfree(cfg);
  559. return ret;
  560. }
  561. struct iwl_d3_iter_data {
  562. struct iwl_mvm *mvm;
  563. struct ieee80211_vif *vif;
  564. bool error;
  565. };
  566. static void iwl_mvm_d3_iface_iterator(void *_data, u8 *mac,
  567. struct ieee80211_vif *vif)
  568. {
  569. struct iwl_d3_iter_data *data = _data;
  570. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  571. if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
  572. return;
  573. if (mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
  574. return;
  575. if (data->vif) {
  576. IWL_ERR(data->mvm, "More than one managed interface active!\n");
  577. data->error = true;
  578. return;
  579. }
  580. data->vif = vif;
  581. }
  582. static int iwl_mvm_d3_reprogram(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  583. struct ieee80211_sta *ap_sta)
  584. {
  585. struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
  586. struct ieee80211_chanctx_conf *ctx;
  587. u8 chains_static, chains_dynamic;
  588. struct cfg80211_chan_def chandef;
  589. int ret, i;
  590. struct iwl_binding_cmd binding_cmd = {};
  591. struct iwl_time_quota_cmd quota_cmd = {};
  592. u32 status;
  593. /* add back the PHY */
  594. if (WARN_ON(!mvmvif->phy_ctxt))
  595. return -EINVAL;
  596. rcu_read_lock();
  597. ctx = rcu_dereference(vif->chanctx_conf);
  598. if (WARN_ON(!ctx)) {
  599. rcu_read_unlock();
  600. return -EINVAL;
  601. }
  602. chandef = ctx->def;
  603. chains_static = ctx->rx_chains_static;
  604. chains_dynamic = ctx->rx_chains_dynamic;
  605. rcu_read_unlock();
  606. ret = iwl_mvm_phy_ctxt_add(mvm, mvmvif->phy_ctxt, &chandef,
  607. chains_static, chains_dynamic);
  608. if (ret)
  609. return ret;
  610. /* add back the MAC */
  611. mvmvif->uploaded = false;
  612. if (WARN_ON(!vif->bss_conf.assoc))
  613. return -EINVAL;
  614. /* hack */
  615. vif->bss_conf.assoc = false;
  616. ret = iwl_mvm_mac_ctxt_add(mvm, vif);
  617. vif->bss_conf.assoc = true;
  618. if (ret)
  619. return ret;
  620. /* add back binding - XXX refactor? */
  621. binding_cmd.id_and_color =
  622. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  623. mvmvif->phy_ctxt->color));
  624. binding_cmd.action = cpu_to_le32(FW_CTXT_ACTION_ADD);
  625. binding_cmd.phy =
  626. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  627. mvmvif->phy_ctxt->color));
  628. binding_cmd.macs[0] = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
  629. mvmvif->color));
  630. for (i = 1; i < MAX_MACS_IN_BINDING; i++)
  631. binding_cmd.macs[i] = cpu_to_le32(FW_CTXT_INVALID);
  632. status = 0;
  633. ret = iwl_mvm_send_cmd_pdu_status(mvm, BINDING_CONTEXT_CMD,
  634. sizeof(binding_cmd), &binding_cmd,
  635. &status);
  636. if (ret) {
  637. IWL_ERR(mvm, "Failed to add binding: %d\n", ret);
  638. return ret;
  639. }
  640. if (status) {
  641. IWL_ERR(mvm, "Binding command failed: %u\n", status);
  642. return -EIO;
  643. }
  644. ret = iwl_mvm_sta_send_to_fw(mvm, ap_sta, false);
  645. if (ret)
  646. return ret;
  647. rcu_assign_pointer(mvm->fw_id_to_mac_id[mvmvif->ap_sta_id], ap_sta);
  648. ret = iwl_mvm_mac_ctxt_changed(mvm, vif);
  649. if (ret)
  650. return ret;
  651. /* and some quota */
  652. quota_cmd.quotas[0].id_and_color =
  653. cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->phy_ctxt->id,
  654. mvmvif->phy_ctxt->color));
  655. quota_cmd.quotas[0].quota = cpu_to_le32(100);
  656. quota_cmd.quotas[0].max_duration = cpu_to_le32(1000);
  657. for (i = 1; i < MAX_BINDINGS; i++)
  658. quota_cmd.quotas[i].id_and_color = cpu_to_le32(FW_CTXT_INVALID);
  659. ret = iwl_mvm_send_cmd_pdu(mvm, TIME_QUOTA_CMD, CMD_SYNC,
  660. sizeof(quota_cmd), &quota_cmd);
  661. if (ret)
  662. IWL_ERR(mvm, "Failed to send quota: %d\n", ret);
  663. return 0;
  664. }
  665. int iwl_mvm_suspend(struct ieee80211_hw *hw, struct cfg80211_wowlan *wowlan)
  666. {
  667. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  668. struct iwl_d3_iter_data suspend_iter_data = {
  669. .mvm = mvm,
  670. };
  671. struct ieee80211_vif *vif;
  672. struct iwl_mvm_vif *mvmvif;
  673. struct ieee80211_sta *ap_sta;
  674. struct iwl_mvm_sta *mvm_ap_sta;
  675. struct iwl_wowlan_config_cmd wowlan_config_cmd = {};
  676. struct iwl_wowlan_kek_kck_material_cmd kek_kck_cmd = {};
  677. struct iwl_wowlan_tkip_params_cmd tkip_cmd = {};
  678. struct iwl_d3_manager_config d3_cfg_cmd = {
  679. /*
  680. * Program the minimum sleep time to 10 seconds, as many
  681. * platforms have issues processing a wakeup signal while
  682. * still being in the process of suspending.
  683. */
  684. .min_sleep_time = cpu_to_le32(10 * 1000 * 1000),
  685. };
  686. struct wowlan_key_data key_data = {
  687. .use_rsc_tsc = false,
  688. .tkip = &tkip_cmd,
  689. .use_tkip = false,
  690. };
  691. int ret, i;
  692. u16 seq;
  693. u8 old_aux_sta_id, old_ap_sta_id = IWL_MVM_STATION_COUNT;
  694. if (WARN_ON(!wowlan))
  695. return -EINVAL;
  696. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  697. if (!key_data.rsc_tsc)
  698. return -ENOMEM;
  699. mutex_lock(&mvm->mutex);
  700. old_aux_sta_id = mvm->aux_sta.sta_id;
  701. /* see if there's only a single BSS vif and it's associated */
  702. ieee80211_iterate_active_interfaces_atomic(
  703. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  704. iwl_mvm_d3_iface_iterator, &suspend_iter_data);
  705. if (suspend_iter_data.error || !suspend_iter_data.vif) {
  706. ret = 1;
  707. goto out_noreset;
  708. }
  709. vif = suspend_iter_data.vif;
  710. mvmvif = iwl_mvm_vif_from_mac80211(vif);
  711. ap_sta = rcu_dereference_protected(
  712. mvm->fw_id_to_mac_id[mvmvif->ap_sta_id],
  713. lockdep_is_held(&mvm->mutex));
  714. if (IS_ERR_OR_NULL(ap_sta)) {
  715. ret = -EINVAL;
  716. goto out_noreset;
  717. }
  718. mvm_ap_sta = (struct iwl_mvm_sta *)ap_sta->drv_priv;
  719. /*
  720. * The D3 firmware still hardcodes the AP station ID for the
  721. * BSS we're associated with as 0. Store the real STA ID here
  722. * and assign 0. When we leave this function, we'll restore
  723. * the original value for the resume code.
  724. */
  725. old_ap_sta_id = mvm_ap_sta->sta_id;
  726. mvm_ap_sta->sta_id = 0;
  727. mvmvif->ap_sta_id = 0;
  728. /* TODO: wowlan_config_cmd.wowlan_ba_teardown_tids */
  729. wowlan_config_cmd.is_11n_connection = ap_sta->ht_cap.ht_supported;
  730. /*
  731. * We know the last used seqno, and the uCode expects to know that
  732. * one, it will increment before TX.
  733. */
  734. seq = mvm_ap_sta->last_seq_ctl & IEEE80211_SCTL_SEQ;
  735. wowlan_config_cmd.non_qos_seq = cpu_to_le16(seq);
  736. /*
  737. * For QoS counters, we store the one to use next, so subtract 0x10
  738. * since the uCode will add 0x10 *before* using the value while we
  739. * increment after using the value (i.e. store the next value to use).
  740. */
  741. for (i = 0; i < IWL_MAX_TID_COUNT; i++) {
  742. seq = mvm_ap_sta->tid_data[i].seq_number;
  743. seq -= 0x10;
  744. wowlan_config_cmd.qos_seq[i] = cpu_to_le16(seq);
  745. }
  746. if (wowlan->disconnect)
  747. wowlan_config_cmd.wakeup_filter |=
  748. cpu_to_le32(IWL_WOWLAN_WAKEUP_BEACON_MISS |
  749. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  750. if (wowlan->magic_pkt)
  751. wowlan_config_cmd.wakeup_filter |=
  752. cpu_to_le32(IWL_WOWLAN_WAKEUP_MAGIC_PACKET);
  753. if (wowlan->gtk_rekey_failure)
  754. wowlan_config_cmd.wakeup_filter |=
  755. cpu_to_le32(IWL_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  756. if (wowlan->eap_identity_req)
  757. wowlan_config_cmd.wakeup_filter |=
  758. cpu_to_le32(IWL_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  759. if (wowlan->four_way_handshake)
  760. wowlan_config_cmd.wakeup_filter |=
  761. cpu_to_le32(IWL_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  762. if (wowlan->n_patterns)
  763. wowlan_config_cmd.wakeup_filter |=
  764. cpu_to_le32(IWL_WOWLAN_WAKEUP_PATTERN_MATCH);
  765. if (wowlan->rfkill_release)
  766. wowlan_config_cmd.wakeup_filter |=
  767. cpu_to_le32(IWL_WOWLAN_WAKEUP_RF_KILL_DEASSERT);
  768. if (wowlan->tcp) {
  769. /*
  770. * Set the "link change" (really "link lost") flag as well
  771. * since that implies losing the TCP connection.
  772. */
  773. wowlan_config_cmd.wakeup_filter |=
  774. cpu_to_le32(IWL_WOWLAN_WAKEUP_REMOTE_LINK_LOSS |
  775. IWL_WOWLAN_WAKEUP_REMOTE_SIGNATURE_TABLE |
  776. IWL_WOWLAN_WAKEUP_REMOTE_WAKEUP_PACKET |
  777. IWL_WOWLAN_WAKEUP_LINK_CHANGE);
  778. }
  779. iwl_mvm_cancel_scan(mvm);
  780. iwl_trans_stop_device(mvm->trans);
  781. /*
  782. * Set the HW restart bit -- this is mostly true as we're
  783. * going to load new firmware and reprogram that, though
  784. * the reprogramming is going to be manual to avoid adding
  785. * all the MACs that aren't support.
  786. * We don't have to clear up everything though because the
  787. * reprogramming is manual. When we resume, we'll actually
  788. * go through a proper restart sequence again to switch
  789. * back to the runtime firmware image.
  790. */
  791. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  792. /* We reprogram keys and shouldn't allocate new key indices */
  793. memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
  794. mvm->ptk_ivlen = 0;
  795. mvm->ptk_icvlen = 0;
  796. mvm->ptk_ivlen = 0;
  797. mvm->ptk_icvlen = 0;
  798. /*
  799. * The D3 firmware still hardcodes the AP station ID for the
  800. * BSS we're associated with as 0. As a result, we have to move
  801. * the auxiliary station to ID 1 so the ID 0 remains free for
  802. * the AP station for later.
  803. * We set the sta_id to 1 here, and reset it to its previous
  804. * value (that we stored above) later.
  805. */
  806. mvm->aux_sta.sta_id = 1;
  807. ret = iwl_mvm_load_d3_fw(mvm);
  808. if (ret)
  809. goto out;
  810. ret = iwl_mvm_d3_reprogram(mvm, vif, ap_sta);
  811. if (ret)
  812. goto out;
  813. if (!iwlwifi_mod_params.sw_crypto) {
  814. /*
  815. * This needs to be unlocked due to lock ordering
  816. * constraints. Since we're in the suspend path
  817. * that isn't really a problem though.
  818. */
  819. mutex_unlock(&mvm->mutex);
  820. ieee80211_iter_keys(mvm->hw, vif,
  821. iwl_mvm_wowlan_program_keys,
  822. &key_data);
  823. mutex_lock(&mvm->mutex);
  824. if (key_data.error) {
  825. ret = -EIO;
  826. goto out;
  827. }
  828. if (key_data.use_rsc_tsc) {
  829. struct iwl_host_cmd rsc_tsc_cmd = {
  830. .id = WOWLAN_TSC_RSC_PARAM,
  831. .flags = CMD_SYNC,
  832. .data[0] = key_data.rsc_tsc,
  833. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  834. .len[0] = sizeof(*key_data.rsc_tsc),
  835. };
  836. ret = iwl_mvm_send_cmd(mvm, &rsc_tsc_cmd);
  837. if (ret)
  838. goto out;
  839. }
  840. if (key_data.use_tkip) {
  841. ret = iwl_mvm_send_cmd_pdu(mvm,
  842. WOWLAN_TKIP_PARAM,
  843. CMD_SYNC, sizeof(tkip_cmd),
  844. &tkip_cmd);
  845. if (ret)
  846. goto out;
  847. }
  848. if (mvmvif->rekey_data.valid) {
  849. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  850. memcpy(kek_kck_cmd.kck, mvmvif->rekey_data.kck,
  851. NL80211_KCK_LEN);
  852. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  853. memcpy(kek_kck_cmd.kek, mvmvif->rekey_data.kek,
  854. NL80211_KEK_LEN);
  855. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  856. kek_kck_cmd.replay_ctr = mvmvif->rekey_data.replay_ctr;
  857. ret = iwl_mvm_send_cmd_pdu(mvm,
  858. WOWLAN_KEK_KCK_MATERIAL,
  859. CMD_SYNC,
  860. sizeof(kek_kck_cmd),
  861. &kek_kck_cmd);
  862. if (ret)
  863. goto out;
  864. }
  865. }
  866. ret = iwl_mvm_send_cmd_pdu(mvm, WOWLAN_CONFIGURATION,
  867. CMD_SYNC, sizeof(wowlan_config_cmd),
  868. &wowlan_config_cmd);
  869. if (ret)
  870. goto out;
  871. ret = iwl_mvm_send_patterns(mvm, wowlan);
  872. if (ret)
  873. goto out;
  874. ret = iwl_mvm_send_proto_offload(mvm, vif);
  875. if (ret)
  876. goto out;
  877. ret = iwl_mvm_send_remote_wake_cfg(mvm, vif, wowlan->tcp);
  878. if (ret)
  879. goto out;
  880. /* must be last -- this switches firmware state */
  881. ret = iwl_mvm_send_cmd_pdu(mvm, D3_CONFIG_CMD, CMD_SYNC,
  882. sizeof(d3_cfg_cmd), &d3_cfg_cmd);
  883. if (ret)
  884. goto out;
  885. clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  886. iwl_trans_d3_suspend(mvm->trans);
  887. out:
  888. mvm->aux_sta.sta_id = old_aux_sta_id;
  889. mvm_ap_sta->sta_id = old_ap_sta_id;
  890. mvmvif->ap_sta_id = old_ap_sta_id;
  891. out_noreset:
  892. kfree(key_data.rsc_tsc);
  893. if (ret < 0)
  894. ieee80211_restart_hw(mvm->hw);
  895. mutex_unlock(&mvm->mutex);
  896. return ret;
  897. }
  898. static void iwl_mvm_query_wakeup_reasons(struct iwl_mvm *mvm,
  899. struct ieee80211_vif *vif)
  900. {
  901. u32 base = mvm->error_event_table;
  902. struct error_table_start {
  903. /* cf. struct iwl_error_event_table */
  904. u32 valid;
  905. u32 error_id;
  906. } err_info;
  907. struct cfg80211_wowlan_wakeup wakeup = {
  908. .pattern_idx = -1,
  909. };
  910. struct cfg80211_wowlan_wakeup *wakeup_report = &wakeup;
  911. struct iwl_host_cmd cmd = {
  912. .id = WOWLAN_GET_STATUSES,
  913. .flags = CMD_SYNC | CMD_WANT_SKB,
  914. };
  915. struct iwl_wowlan_status *status;
  916. u32 reasons;
  917. int ret, len;
  918. struct sk_buff *pkt = NULL;
  919. iwl_trans_read_mem_bytes(mvm->trans, base,
  920. &err_info, sizeof(err_info));
  921. if (err_info.valid) {
  922. IWL_INFO(mvm, "error table is valid (%d)\n",
  923. err_info.valid);
  924. if (err_info.error_id == RF_KILL_INDICATOR_FOR_WOWLAN) {
  925. wakeup.rfkill_release = true;
  926. ieee80211_report_wowlan_wakeup(vif, &wakeup,
  927. GFP_KERNEL);
  928. }
  929. return;
  930. }
  931. /* only for tracing for now */
  932. ret = iwl_mvm_send_cmd_pdu(mvm, OFFLOADS_QUERY_CMD, CMD_SYNC, 0, NULL);
  933. if (ret)
  934. IWL_ERR(mvm, "failed to query offload statistics (%d)\n", ret);
  935. ret = iwl_mvm_send_cmd(mvm, &cmd);
  936. if (ret) {
  937. IWL_ERR(mvm, "failed to query status (%d)\n", ret);
  938. return;
  939. }
  940. /* RF-kill already asserted again... */
  941. if (!cmd.resp_pkt)
  942. return;
  943. len = le32_to_cpu(cmd.resp_pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  944. if (len - sizeof(struct iwl_cmd_header) < sizeof(*status)) {
  945. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  946. goto out;
  947. }
  948. status = (void *)cmd.resp_pkt->data;
  949. if (len - sizeof(struct iwl_cmd_header) !=
  950. sizeof(*status) +
  951. ALIGN(le32_to_cpu(status->wake_packet_bufsize), 4)) {
  952. IWL_ERR(mvm, "Invalid WoWLAN status response!\n");
  953. goto out;
  954. }
  955. reasons = le32_to_cpu(status->wakeup_reasons);
  956. if (reasons == IWL_WOWLAN_WAKEUP_BY_NON_WIRELESS) {
  957. wakeup_report = NULL;
  958. goto report;
  959. }
  960. if (reasons & IWL_WOWLAN_WAKEUP_BY_MAGIC_PACKET)
  961. wakeup.magic_pkt = true;
  962. if (reasons & IWL_WOWLAN_WAKEUP_BY_PATTERN)
  963. wakeup.pattern_idx =
  964. le16_to_cpu(status->pattern_number);
  965. if (reasons & (IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_MISSED_BEACON |
  966. IWL_WOWLAN_WAKEUP_BY_DISCONNECTION_ON_DEAUTH))
  967. wakeup.disconnect = true;
  968. if (reasons & IWL_WOWLAN_WAKEUP_BY_GTK_REKEY_FAILURE)
  969. wakeup.gtk_rekey_failure = true;
  970. if (reasons & IWL_WOWLAN_WAKEUP_BY_RFKILL_DEASSERTED)
  971. wakeup.rfkill_release = true;
  972. if (reasons & IWL_WOWLAN_WAKEUP_BY_EAPOL_REQUEST)
  973. wakeup.eap_identity_req = true;
  974. if (reasons & IWL_WOWLAN_WAKEUP_BY_FOUR_WAY_HANDSHAKE)
  975. wakeup.four_way_handshake = true;
  976. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_LINK_LOSS)
  977. wakeup.tcp_connlost = true;
  978. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_SIGNATURE_TABLE)
  979. wakeup.tcp_nomoretokens = true;
  980. if (reasons & IWL_WOWLAN_WAKEUP_BY_REM_WAKE_WAKEUP_PACKET)
  981. wakeup.tcp_match = true;
  982. if (status->wake_packet_bufsize) {
  983. int pktsize = le32_to_cpu(status->wake_packet_bufsize);
  984. int pktlen = le32_to_cpu(status->wake_packet_length);
  985. const u8 *pktdata = status->wake_packet;
  986. struct ieee80211_hdr *hdr = (void *)pktdata;
  987. int truncated = pktlen - pktsize;
  988. /* this would be a firmware bug */
  989. if (WARN_ON_ONCE(truncated < 0))
  990. truncated = 0;
  991. if (ieee80211_is_data(hdr->frame_control)) {
  992. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  993. int ivlen = 0, icvlen = 4; /* also FCS */
  994. pkt = alloc_skb(pktsize, GFP_KERNEL);
  995. if (!pkt)
  996. goto report;
  997. memcpy(skb_put(pkt, hdrlen), pktdata, hdrlen);
  998. pktdata += hdrlen;
  999. pktsize -= hdrlen;
  1000. if (ieee80211_has_protected(hdr->frame_control)) {
  1001. if (is_multicast_ether_addr(hdr->addr1)) {
  1002. ivlen = mvm->gtk_ivlen;
  1003. icvlen += mvm->gtk_icvlen;
  1004. } else {
  1005. ivlen = mvm->ptk_ivlen;
  1006. icvlen += mvm->ptk_icvlen;
  1007. }
  1008. }
  1009. /* if truncated, FCS/ICV is (partially) gone */
  1010. if (truncated >= icvlen) {
  1011. icvlen = 0;
  1012. truncated -= icvlen;
  1013. } else {
  1014. icvlen -= truncated;
  1015. truncated = 0;
  1016. }
  1017. pktsize -= ivlen + icvlen;
  1018. pktdata += ivlen;
  1019. memcpy(skb_put(pkt, pktsize), pktdata, pktsize);
  1020. if (ieee80211_data_to_8023(pkt, vif->addr, vif->type))
  1021. goto report;
  1022. wakeup.packet = pkt->data;
  1023. wakeup.packet_present_len = pkt->len;
  1024. wakeup.packet_len = pkt->len - truncated;
  1025. wakeup.packet_80211 = false;
  1026. } else {
  1027. int fcslen = 4;
  1028. if (truncated >= 4) {
  1029. truncated -= 4;
  1030. fcslen = 0;
  1031. } else {
  1032. fcslen -= truncated;
  1033. truncated = 0;
  1034. }
  1035. pktsize -= fcslen;
  1036. wakeup.packet = status->wake_packet;
  1037. wakeup.packet_present_len = pktsize;
  1038. wakeup.packet_len = pktlen - truncated;
  1039. wakeup.packet_80211 = true;
  1040. }
  1041. }
  1042. report:
  1043. ieee80211_report_wowlan_wakeup(vif, wakeup_report, GFP_KERNEL);
  1044. kfree_skb(pkt);
  1045. out:
  1046. iwl_free_resp(&cmd);
  1047. }
  1048. int iwl_mvm_resume(struct ieee80211_hw *hw)
  1049. {
  1050. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1051. struct iwl_d3_iter_data resume_iter_data = {
  1052. .mvm = mvm,
  1053. };
  1054. struct ieee80211_vif *vif = NULL;
  1055. int ret;
  1056. enum iwl_d3_status d3_status;
  1057. mutex_lock(&mvm->mutex);
  1058. /* get the BSS vif pointer again */
  1059. ieee80211_iterate_active_interfaces_atomic(
  1060. mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
  1061. iwl_mvm_d3_iface_iterator, &resume_iter_data);
  1062. if (WARN_ON(resume_iter_data.error || !resume_iter_data.vif))
  1063. goto out_unlock;
  1064. vif = resume_iter_data.vif;
  1065. ret = iwl_trans_d3_resume(mvm->trans, &d3_status);
  1066. if (ret)
  1067. goto out_unlock;
  1068. if (d3_status != IWL_D3_STATUS_ALIVE) {
  1069. IWL_INFO(mvm, "Device was reset during suspend\n");
  1070. goto out_unlock;
  1071. }
  1072. iwl_mvm_query_wakeup_reasons(mvm, vif);
  1073. out_unlock:
  1074. mutex_unlock(&mvm->mutex);
  1075. if (vif)
  1076. ieee80211_resume_disconnect(vif);
  1077. /* return 1 to reconfigure the device */
  1078. set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
  1079. return 1;
  1080. }
  1081. void iwl_mvm_set_wakeup(struct ieee80211_hw *hw, bool enabled)
  1082. {
  1083. struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
  1084. device_set_wakeup_enable(mvm->trans->dev, enabled);
  1085. }