d3.c 54 KB

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