wpa.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2002-2004, Instant802 Networks, Inc.
  4. * Copyright 2008, Jouni Malinen <j@w1.fi>
  5. * Copyright (C) 2016-2017 Intel Deutschland GmbH
  6. * Copyright (C) 2020-2023 Intel Corporation
  7. */
  8. #include <linux/netdevice.h>
  9. #include <linux/types.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/compiler.h>
  12. #include <linux/ieee80211.h>
  13. #include <linux/gfp.h>
  14. #include <linux/unaligned.h>
  15. #include <net/mac80211.h>
  16. #include <crypto/aes.h>
  17. #include <crypto/utils.h>
  18. #include "ieee80211_i.h"
  19. #include "michael.h"
  20. #include "tkip.h"
  21. #include "aes_ccm.h"
  22. #include "aes_cmac.h"
  23. #include "aes_gmac.h"
  24. #include "aes_gcm.h"
  25. #include "wpa.h"
  26. ieee80211_tx_result
  27. ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
  28. {
  29. u8 *data, *key, *mic;
  30. size_t data_len;
  31. unsigned int hdrlen;
  32. struct ieee80211_hdr *hdr;
  33. struct sk_buff *skb = tx->skb;
  34. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  35. int tail;
  36. hdr = (struct ieee80211_hdr *)skb->data;
  37. if (!tx->key || tx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  38. skb->len < 24 || !ieee80211_is_data_present(hdr->frame_control))
  39. return TX_CONTINUE;
  40. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  41. if (skb->len < hdrlen)
  42. return TX_DROP;
  43. data = skb->data + hdrlen;
  44. data_len = skb->len - hdrlen;
  45. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE)) {
  46. /* Need to use software crypto for the test */
  47. info->control.hw_key = NULL;
  48. }
  49. if (info->control.hw_key &&
  50. (info->flags & IEEE80211_TX_CTL_DONTFRAG ||
  51. ieee80211_hw_check(&tx->local->hw, SUPPORTS_TX_FRAG)) &&
  52. !(tx->key->conf.flags & (IEEE80211_KEY_FLAG_GENERATE_MMIC |
  53. IEEE80211_KEY_FLAG_PUT_MIC_SPACE))) {
  54. /* hwaccel - with no need for SW-generated MMIC or MIC space */
  55. return TX_CONTINUE;
  56. }
  57. tail = MICHAEL_MIC_LEN;
  58. if (!info->control.hw_key)
  59. tail += IEEE80211_TKIP_ICV_LEN;
  60. if (WARN(skb_tailroom(skb) < tail ||
  61. skb_headroom(skb) < IEEE80211_TKIP_IV_LEN,
  62. "mmic: not enough head/tail (%d/%d,%d/%d)\n",
  63. skb_headroom(skb), IEEE80211_TKIP_IV_LEN,
  64. skb_tailroom(skb), tail))
  65. return TX_DROP;
  66. mic = skb_put(skb, MICHAEL_MIC_LEN);
  67. if (tx->key->conf.flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) {
  68. /* Zeroed MIC can help with debug */
  69. memset(mic, 0, MICHAEL_MIC_LEN);
  70. return TX_CONTINUE;
  71. }
  72. key = &tx->key->conf.key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY];
  73. michael_mic(key, hdr, data, data_len, mic);
  74. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE))
  75. mic[0]++;
  76. return TX_CONTINUE;
  77. }
  78. ieee80211_rx_result
  79. ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
  80. {
  81. u8 *data, *key = NULL;
  82. size_t data_len;
  83. unsigned int hdrlen;
  84. u8 mic[MICHAEL_MIC_LEN];
  85. struct sk_buff *skb = rx->skb;
  86. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  87. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  88. /*
  89. * it makes no sense to check for MIC errors on anything other
  90. * than data frames.
  91. */
  92. if (!ieee80211_is_data_present(hdr->frame_control))
  93. return RX_CONTINUE;
  94. /*
  95. * No way to verify the MIC if the hardware stripped it or
  96. * the IV with the key index. In this case we have solely rely
  97. * on the driver to set RX_FLAG_MMIC_ERROR in the event of a
  98. * MIC failure report.
  99. */
  100. if (status->flag & (RX_FLAG_MMIC_STRIPPED | RX_FLAG_IV_STRIPPED)) {
  101. if (status->flag & RX_FLAG_MMIC_ERROR)
  102. goto mic_fail_no_key;
  103. if (!(status->flag & RX_FLAG_IV_STRIPPED) && rx->key &&
  104. rx->key->conf.cipher == WLAN_CIPHER_SUITE_TKIP)
  105. goto update_iv;
  106. return RX_CONTINUE;
  107. }
  108. /*
  109. * Some hardware seems to generate Michael MIC failure reports; even
  110. * though, the frame was not encrypted with TKIP and therefore has no
  111. * MIC. Ignore the flag them to avoid triggering countermeasures.
  112. */
  113. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  114. !(status->flag & RX_FLAG_DECRYPTED))
  115. return RX_CONTINUE;
  116. if (rx->sdata->vif.type == NL80211_IFTYPE_AP && rx->key->conf.keyidx) {
  117. /*
  118. * APs with pairwise keys should never receive Michael MIC
  119. * errors for non-zero keyidx because these are reserved for
  120. * group keys and only the AP is sending real multicast
  121. * frames in the BSS.
  122. */
  123. return RX_DROP_U_AP_RX_GROUPCAST;
  124. }
  125. if (status->flag & RX_FLAG_MMIC_ERROR)
  126. goto mic_fail;
  127. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  128. if (skb->len < hdrlen + MICHAEL_MIC_LEN)
  129. return RX_DROP_U_SHORT_MMIC;
  130. if (skb_linearize(rx->skb))
  131. return RX_DROP_U_OOM;
  132. hdr = (void *)skb->data;
  133. data = skb->data + hdrlen;
  134. data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
  135. key = &rx->key->conf.key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
  136. michael_mic(key, hdr, data, data_len, mic);
  137. if (crypto_memneq(mic, data + data_len, MICHAEL_MIC_LEN))
  138. goto mic_fail;
  139. /* remove Michael MIC from payload */
  140. skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
  141. update_iv:
  142. /* update IV in key information to be able to detect replays */
  143. rx->key->u.tkip.rx[rx->security_idx].iv32 = rx->tkip.iv32;
  144. rx->key->u.tkip.rx[rx->security_idx].iv16 = rx->tkip.iv16;
  145. return RX_CONTINUE;
  146. mic_fail:
  147. rx->key->u.tkip.mic_failures++;
  148. mic_fail_no_key:
  149. /*
  150. * In some cases the key can be unset - e.g. a multicast packet, in
  151. * a driver that supports HW encryption. Send up the key idx only if
  152. * the key is set.
  153. */
  154. cfg80211_michael_mic_failure(rx->sdata->dev, hdr->addr2,
  155. is_multicast_ether_addr(hdr->addr1) ?
  156. NL80211_KEYTYPE_GROUP :
  157. NL80211_KEYTYPE_PAIRWISE,
  158. rx->key ? rx->key->conf.keyidx : -1,
  159. NULL, GFP_ATOMIC);
  160. return RX_DROP_U_MMIC_FAIL;
  161. }
  162. static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  163. {
  164. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  165. struct ieee80211_key *key = tx->key;
  166. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  167. unsigned int hdrlen;
  168. int len, tail;
  169. u64 pn;
  170. u8 *pos;
  171. if (info->control.hw_key &&
  172. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  173. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  174. /* hwaccel - with no need for software-generated IV */
  175. return 0;
  176. }
  177. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  178. len = skb->len - hdrlen;
  179. if (info->control.hw_key)
  180. tail = 0;
  181. else
  182. tail = IEEE80211_TKIP_ICV_LEN;
  183. if (WARN_ON(skb_tailroom(skb) < tail ||
  184. skb_headroom(skb) < IEEE80211_TKIP_IV_LEN))
  185. return -1;
  186. pos = skb_push(skb, IEEE80211_TKIP_IV_LEN);
  187. memmove(pos, pos + IEEE80211_TKIP_IV_LEN, hdrlen);
  188. pos += hdrlen;
  189. /* the HW only needs room for the IV, but not the actual IV */
  190. if (info->control.hw_key &&
  191. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  192. return 0;
  193. /* Increase IV for the frame */
  194. pn = atomic64_inc_return(&key->conf.tx_pn);
  195. pos = ieee80211_tkip_add_iv(pos, &key->conf, pn);
  196. /* hwaccel - with software IV */
  197. if (info->control.hw_key)
  198. return 0;
  199. /* Add room for ICV */
  200. skb_put(skb, IEEE80211_TKIP_ICV_LEN);
  201. return ieee80211_tkip_encrypt_data(&tx->local->wep_tx_ctx,
  202. key, skb, pos, len);
  203. }
  204. ieee80211_tx_result
  205. ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
  206. {
  207. struct sk_buff *skb;
  208. ieee80211_tx_set_protected(tx);
  209. skb_queue_walk(&tx->skbs, skb) {
  210. if (tkip_encrypt_skb(tx, skb) < 0)
  211. return TX_DROP;
  212. }
  213. return TX_CONTINUE;
  214. }
  215. ieee80211_rx_result
  216. ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
  217. {
  218. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  219. int hdrlen, res, hwaccel = 0;
  220. struct ieee80211_key *key = rx->key;
  221. struct sk_buff *skb = rx->skb;
  222. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  223. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  224. if (!ieee80211_is_data(hdr->frame_control))
  225. return RX_CONTINUE;
  226. if (!rx->sta || skb->len - hdrlen < 12)
  227. return RX_DROP_U_SHORT_TKIP;
  228. /* it may be possible to optimize this a bit more */
  229. if (skb_linearize(rx->skb))
  230. return RX_DROP_U_OOM;
  231. hdr = (void *)skb->data;
  232. /*
  233. * Let TKIP code verify IV, but skip decryption.
  234. * In the case where hardware checks the IV as well,
  235. * we don't even get here, see ieee80211_rx_h_decrypt()
  236. */
  237. if (status->flag & RX_FLAG_DECRYPTED)
  238. hwaccel = 1;
  239. res = ieee80211_tkip_decrypt_data(&rx->local->wep_rx_ctx,
  240. key, skb->data + hdrlen,
  241. skb->len - hdrlen, rx->sta->sta.addr,
  242. hdr->addr1, hwaccel, rx->security_idx,
  243. &rx->tkip.iv32,
  244. &rx->tkip.iv16);
  245. if (res != TKIP_DECRYPT_OK)
  246. return RX_DROP_U_TKIP_FAIL;
  247. /* Trim ICV */
  248. if (!(status->flag & RX_FLAG_ICV_STRIPPED))
  249. skb_trim(skb, skb->len - IEEE80211_TKIP_ICV_LEN);
  250. /* Remove IV */
  251. memmove(skb->data + IEEE80211_TKIP_IV_LEN, skb->data, hdrlen);
  252. skb_pull(skb, IEEE80211_TKIP_IV_LEN);
  253. return RX_CONTINUE;
  254. }
  255. /*
  256. * Calculate AAD for CCMP/GCMP, returning qos_tid since we
  257. * need that in CCMP also for b_0.
  258. */
  259. static u8 ccmp_gcmp_aad(struct sk_buff *skb, u8 *aad, bool spp_amsdu)
  260. {
  261. struct ieee80211_hdr *hdr = (void *)skb->data;
  262. __le16 mask_fc;
  263. int a4_included, mgmt;
  264. u8 qos_tid;
  265. u16 len_a = 22;
  266. /*
  267. * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  268. * Retry, PwrMgt, MoreData, Order (if Qos Data); set Protected
  269. */
  270. mgmt = ieee80211_is_mgmt(hdr->frame_control);
  271. mask_fc = hdr->frame_control;
  272. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  273. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  274. if (!mgmt)
  275. mask_fc &= ~cpu_to_le16(0x0070);
  276. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  277. a4_included = ieee80211_has_a4(hdr->frame_control);
  278. if (a4_included)
  279. len_a += 6;
  280. if (ieee80211_is_data_qos(hdr->frame_control)) {
  281. qos_tid = *ieee80211_get_qos_ctl(hdr);
  282. if (spp_amsdu)
  283. qos_tid &= IEEE80211_QOS_CTL_TID_MASK |
  284. IEEE80211_QOS_CTL_A_MSDU_PRESENT;
  285. else
  286. qos_tid &= IEEE80211_QOS_CTL_TID_MASK;
  287. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_ORDER);
  288. len_a += 2;
  289. } else {
  290. qos_tid = 0;
  291. }
  292. /* AAD (extra authenticate-only data) / masked 802.11 header
  293. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  294. put_unaligned_be16(len_a, &aad[0]);
  295. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  296. memcpy(&aad[4], &hdr->addrs, 3 * ETH_ALEN);
  297. /* Mask Seq#, leave Frag# */
  298. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  299. aad[23] = 0;
  300. if (a4_included) {
  301. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  302. aad[30] = qos_tid;
  303. aad[31] = 0;
  304. } else {
  305. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  306. aad[24] = qos_tid;
  307. }
  308. return qos_tid;
  309. }
  310. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
  311. bool spp_amsdu)
  312. {
  313. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  314. u8 qos_tid = ccmp_gcmp_aad(skb, aad, spp_amsdu);
  315. /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
  316. * mode authentication are not allowed to collide, yet both are derived
  317. * from this vector b_0. We only set L := 1 here to indicate that the
  318. * data size can be represented in (L+1) bytes. The CCM layer will take
  319. * care of storing the data length in the top (L+1) bytes and setting
  320. * and clearing the other bits as is required to derive the two IVs.
  321. */
  322. b_0[0] = 0x1;
  323. /* Nonce: Nonce Flags | A2 | PN
  324. * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
  325. */
  326. b_0[1] = qos_tid | (ieee80211_is_mgmt(hdr->frame_control) << 4);
  327. memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
  328. memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
  329. }
  330. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  331. {
  332. hdr[0] = pn[5];
  333. hdr[1] = pn[4];
  334. hdr[2] = 0;
  335. hdr[3] = 0x20 | (key_id << 6);
  336. hdr[4] = pn[3];
  337. hdr[5] = pn[2];
  338. hdr[6] = pn[1];
  339. hdr[7] = pn[0];
  340. }
  341. static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
  342. {
  343. pn[0] = hdr[7];
  344. pn[1] = hdr[6];
  345. pn[2] = hdr[5];
  346. pn[3] = hdr[4];
  347. pn[4] = hdr[1];
  348. pn[5] = hdr[0];
  349. }
  350. static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb,
  351. unsigned int mic_len)
  352. {
  353. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  354. struct ieee80211_key *key = tx->key;
  355. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  356. int hdrlen, len, tail;
  357. u8 *pos;
  358. u8 pn[6];
  359. u64 pn64;
  360. u8 aad[CCM_AAD_LEN];
  361. u8 b_0[AES_BLOCK_SIZE];
  362. if (info->control.hw_key &&
  363. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  364. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  365. !((info->control.hw_key->flags &
  366. IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) &&
  367. ieee80211_is_mgmt(hdr->frame_control))) {
  368. /*
  369. * hwaccel has no need for preallocated room for CCMP
  370. * header or MIC fields
  371. */
  372. return 0;
  373. }
  374. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  375. len = skb->len - hdrlen;
  376. if (info->control.hw_key)
  377. tail = 0;
  378. else
  379. tail = mic_len;
  380. if (WARN_ON(skb_tailroom(skb) < tail ||
  381. skb_headroom(skb) < IEEE80211_CCMP_HDR_LEN))
  382. return -1;
  383. pos = skb_push(skb, IEEE80211_CCMP_HDR_LEN);
  384. memmove(pos, pos + IEEE80211_CCMP_HDR_LEN, hdrlen);
  385. /* the HW only needs room for the IV, but not the actual IV */
  386. if (info->control.hw_key &&
  387. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  388. return 0;
  389. pos += hdrlen;
  390. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  391. pn[5] = pn64;
  392. pn[4] = pn64 >> 8;
  393. pn[3] = pn64 >> 16;
  394. pn[2] = pn64 >> 24;
  395. pn[1] = pn64 >> 32;
  396. pn[0] = pn64 >> 40;
  397. ccmp_pn2hdr(pos, pn, key->conf.keyidx);
  398. /* hwaccel - with software CCMP header */
  399. if (info->control.hw_key)
  400. return 0;
  401. pos += IEEE80211_CCMP_HDR_LEN;
  402. ccmp_special_blocks(skb, pn, b_0, aad,
  403. key->conf.flags & IEEE80211_KEY_FLAG_SPP_AMSDU);
  404. return ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
  405. skb_put(skb, mic_len));
  406. }
  407. ieee80211_tx_result
  408. ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx,
  409. unsigned int mic_len)
  410. {
  411. struct sk_buff *skb;
  412. ieee80211_tx_set_protected(tx);
  413. skb_queue_walk(&tx->skbs, skb) {
  414. if (ccmp_encrypt_skb(tx, skb, mic_len) < 0)
  415. return TX_DROP;
  416. }
  417. return TX_CONTINUE;
  418. }
  419. ieee80211_rx_result
  420. ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx,
  421. unsigned int mic_len)
  422. {
  423. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  424. int hdrlen;
  425. struct ieee80211_key *key = rx->key;
  426. struct sk_buff *skb = rx->skb;
  427. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  428. u8 pn[IEEE80211_CCMP_PN_LEN];
  429. int data_len;
  430. int queue;
  431. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  432. if (!ieee80211_is_data(hdr->frame_control) &&
  433. !ieee80211_is_robust_mgmt_frame(skb))
  434. return RX_CONTINUE;
  435. if (status->flag & RX_FLAG_DECRYPTED) {
  436. if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_CCMP_HDR_LEN))
  437. return RX_DROP_U_SHORT_CCMP;
  438. if (status->flag & RX_FLAG_MIC_STRIPPED)
  439. mic_len = 0;
  440. } else {
  441. if (skb_linearize(rx->skb))
  442. return RX_DROP_U_OOM;
  443. }
  444. /* reload hdr - skb might have been reallocated */
  445. hdr = (void *)rx->skb->data;
  446. data_len = skb->len - hdrlen - IEEE80211_CCMP_HDR_LEN - mic_len;
  447. if (!rx->sta || data_len < 0)
  448. return RX_DROP_U_SHORT_CCMP;
  449. if (!(status->flag & RX_FLAG_PN_VALIDATED)) {
  450. int res;
  451. ccmp_hdr2pn(pn, skb->data + hdrlen);
  452. queue = rx->security_idx;
  453. res = memcmp(pn, key->u.ccmp.rx_pn[queue],
  454. IEEE80211_CCMP_PN_LEN);
  455. if (res < 0 ||
  456. (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) {
  457. key->u.ccmp.replays++;
  458. return RX_DROP_U_REPLAY;
  459. }
  460. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  461. u8 aad[2 * AES_BLOCK_SIZE];
  462. u8 b_0[AES_BLOCK_SIZE];
  463. /* hardware didn't decrypt/verify MIC */
  464. ccmp_special_blocks(skb, pn, b_0, aad,
  465. key->conf.flags & IEEE80211_KEY_FLAG_SPP_AMSDU);
  466. if (ieee80211_aes_ccm_decrypt(
  467. key->u.ccmp.tfm, b_0, aad,
  468. skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN,
  469. data_len,
  470. skb->data + skb->len - mic_len))
  471. return RX_DROP_U_MIC_FAIL;
  472. }
  473. memcpy(key->u.ccmp.rx_pn[queue], pn, IEEE80211_CCMP_PN_LEN);
  474. if (unlikely(ieee80211_is_frag(hdr)))
  475. memcpy(rx->ccm_gcm.pn, pn, IEEE80211_CCMP_PN_LEN);
  476. }
  477. /* Remove CCMP header and MIC */
  478. if (pskb_trim(skb, skb->len - mic_len))
  479. return RX_DROP_U_SHORT_CCMP_MIC;
  480. memmove(skb->data + IEEE80211_CCMP_HDR_LEN, skb->data, hdrlen);
  481. skb_pull(skb, IEEE80211_CCMP_HDR_LEN);
  482. return RX_CONTINUE;
  483. }
  484. static void gcmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *j_0, u8 *aad,
  485. bool spp_amsdu)
  486. {
  487. struct ieee80211_hdr *hdr = (void *)skb->data;
  488. memcpy(j_0, hdr->addr2, ETH_ALEN);
  489. memcpy(&j_0[ETH_ALEN], pn, IEEE80211_GCMP_PN_LEN);
  490. j_0[13] = 0;
  491. j_0[14] = 0;
  492. j_0[AES_BLOCK_SIZE - 1] = 0x01;
  493. ccmp_gcmp_aad(skb, aad, spp_amsdu);
  494. }
  495. static inline void gcmp_pn2hdr(u8 *hdr, const u8 *pn, int key_id)
  496. {
  497. hdr[0] = pn[5];
  498. hdr[1] = pn[4];
  499. hdr[2] = 0;
  500. hdr[3] = 0x20 | (key_id << 6);
  501. hdr[4] = pn[3];
  502. hdr[5] = pn[2];
  503. hdr[6] = pn[1];
  504. hdr[7] = pn[0];
  505. }
  506. static inline void gcmp_hdr2pn(u8 *pn, const u8 *hdr)
  507. {
  508. pn[0] = hdr[7];
  509. pn[1] = hdr[6];
  510. pn[2] = hdr[5];
  511. pn[3] = hdr[4];
  512. pn[4] = hdr[1];
  513. pn[5] = hdr[0];
  514. }
  515. static int gcmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  516. {
  517. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  518. struct ieee80211_key *key = tx->key;
  519. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  520. int hdrlen, len, tail;
  521. u8 *pos;
  522. u8 pn[6];
  523. u64 pn64;
  524. u8 aad[GCM_AAD_LEN];
  525. u8 j_0[AES_BLOCK_SIZE];
  526. if (info->control.hw_key &&
  527. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  528. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  529. !((info->control.hw_key->flags &
  530. IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) &&
  531. ieee80211_is_mgmt(hdr->frame_control))) {
  532. /* hwaccel has no need for preallocated room for GCMP
  533. * header or MIC fields
  534. */
  535. return 0;
  536. }
  537. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  538. len = skb->len - hdrlen;
  539. if (info->control.hw_key)
  540. tail = 0;
  541. else
  542. tail = IEEE80211_GCMP_MIC_LEN;
  543. if (WARN_ON(skb_tailroom(skb) < tail ||
  544. skb_headroom(skb) < IEEE80211_GCMP_HDR_LEN))
  545. return -1;
  546. pos = skb_push(skb, IEEE80211_GCMP_HDR_LEN);
  547. memmove(pos, pos + IEEE80211_GCMP_HDR_LEN, hdrlen);
  548. skb_set_network_header(skb, skb_network_offset(skb) +
  549. IEEE80211_GCMP_HDR_LEN);
  550. /* the HW only needs room for the IV, but not the actual IV */
  551. if (info->control.hw_key &&
  552. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  553. return 0;
  554. pos += hdrlen;
  555. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  556. pn[5] = pn64;
  557. pn[4] = pn64 >> 8;
  558. pn[3] = pn64 >> 16;
  559. pn[2] = pn64 >> 24;
  560. pn[1] = pn64 >> 32;
  561. pn[0] = pn64 >> 40;
  562. gcmp_pn2hdr(pos, pn, key->conf.keyidx);
  563. /* hwaccel - with software GCMP header */
  564. if (info->control.hw_key)
  565. return 0;
  566. pos += IEEE80211_GCMP_HDR_LEN;
  567. gcmp_special_blocks(skb, pn, j_0, aad,
  568. key->conf.flags & IEEE80211_KEY_FLAG_SPP_AMSDU);
  569. return ieee80211_aes_gcm_encrypt(key->u.gcmp.tfm, j_0, aad, pos, len,
  570. skb_put(skb, IEEE80211_GCMP_MIC_LEN));
  571. }
  572. ieee80211_tx_result
  573. ieee80211_crypto_gcmp_encrypt(struct ieee80211_tx_data *tx)
  574. {
  575. struct sk_buff *skb;
  576. ieee80211_tx_set_protected(tx);
  577. skb_queue_walk(&tx->skbs, skb) {
  578. if (gcmp_encrypt_skb(tx, skb) < 0)
  579. return TX_DROP;
  580. }
  581. return TX_CONTINUE;
  582. }
  583. ieee80211_rx_result
  584. ieee80211_crypto_gcmp_decrypt(struct ieee80211_rx_data *rx)
  585. {
  586. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  587. int hdrlen;
  588. struct ieee80211_key *key = rx->key;
  589. struct sk_buff *skb = rx->skb;
  590. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  591. u8 pn[IEEE80211_GCMP_PN_LEN];
  592. int data_len, queue, mic_len = IEEE80211_GCMP_MIC_LEN;
  593. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  594. if (!ieee80211_is_data(hdr->frame_control) &&
  595. !ieee80211_is_robust_mgmt_frame(skb))
  596. return RX_CONTINUE;
  597. if (status->flag & RX_FLAG_DECRYPTED) {
  598. if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_GCMP_HDR_LEN))
  599. return RX_DROP_U_SHORT_GCMP;
  600. if (status->flag & RX_FLAG_MIC_STRIPPED)
  601. mic_len = 0;
  602. } else {
  603. if (skb_linearize(rx->skb))
  604. return RX_DROP_U_OOM;
  605. }
  606. /* reload hdr - skb might have been reallocated */
  607. hdr = (void *)rx->skb->data;
  608. data_len = skb->len - hdrlen - IEEE80211_GCMP_HDR_LEN - mic_len;
  609. if (!rx->sta || data_len < 0)
  610. return RX_DROP_U_SHORT_GCMP;
  611. if (!(status->flag & RX_FLAG_PN_VALIDATED)) {
  612. int res;
  613. gcmp_hdr2pn(pn, skb->data + hdrlen);
  614. queue = rx->security_idx;
  615. res = memcmp(pn, key->u.gcmp.rx_pn[queue],
  616. IEEE80211_GCMP_PN_LEN);
  617. if (res < 0 ||
  618. (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) {
  619. key->u.gcmp.replays++;
  620. return RX_DROP_U_REPLAY;
  621. }
  622. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  623. u8 aad[2 * AES_BLOCK_SIZE];
  624. u8 j_0[AES_BLOCK_SIZE];
  625. /* hardware didn't decrypt/verify MIC */
  626. gcmp_special_blocks(skb, pn, j_0, aad,
  627. key->conf.flags & IEEE80211_KEY_FLAG_SPP_AMSDU);
  628. if (ieee80211_aes_gcm_decrypt(
  629. key->u.gcmp.tfm, j_0, aad,
  630. skb->data + hdrlen + IEEE80211_GCMP_HDR_LEN,
  631. data_len,
  632. skb->data + skb->len -
  633. IEEE80211_GCMP_MIC_LEN))
  634. return RX_DROP_U_MIC_FAIL;
  635. }
  636. memcpy(key->u.gcmp.rx_pn[queue], pn, IEEE80211_GCMP_PN_LEN);
  637. if (unlikely(ieee80211_is_frag(hdr)))
  638. memcpy(rx->ccm_gcm.pn, pn, IEEE80211_CCMP_PN_LEN);
  639. }
  640. /* Remove GCMP header and MIC */
  641. if (pskb_trim(skb, skb->len - mic_len))
  642. return RX_DROP_U_SHORT_GCMP_MIC;
  643. memmove(skb->data + IEEE80211_GCMP_HDR_LEN, skb->data, hdrlen);
  644. skb_pull(skb, IEEE80211_GCMP_HDR_LEN);
  645. return RX_CONTINUE;
  646. }
  647. static void bip_aad(struct sk_buff *skb, u8 *aad)
  648. {
  649. __le16 mask_fc;
  650. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  651. /* BIP AAD: FC(masked) || A1 || A2 || A3 */
  652. /* FC type/subtype */
  653. /* Mask FC Retry, PwrMgt, MoreData flags to zero */
  654. mask_fc = hdr->frame_control;
  655. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY | IEEE80211_FCTL_PM |
  656. IEEE80211_FCTL_MOREDATA);
  657. put_unaligned(mask_fc, (__le16 *) &aad[0]);
  658. /* A1 || A2 || A3 */
  659. memcpy(aad + 2, &hdr->addrs, 3 * ETH_ALEN);
  660. }
  661. static inline void bip_ipn_set64(u8 *d, u64 pn)
  662. {
  663. *d++ = pn;
  664. *d++ = pn >> 8;
  665. *d++ = pn >> 16;
  666. *d++ = pn >> 24;
  667. *d++ = pn >> 32;
  668. *d = pn >> 40;
  669. }
  670. static inline void bip_ipn_swap(u8 *d, const u8 *s)
  671. {
  672. *d++ = s[5];
  673. *d++ = s[4];
  674. *d++ = s[3];
  675. *d++ = s[2];
  676. *d++ = s[1];
  677. *d = s[0];
  678. }
  679. ieee80211_tx_result
  680. ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
  681. {
  682. struct sk_buff *skb;
  683. struct ieee80211_tx_info *info;
  684. struct ieee80211_key *key = tx->key;
  685. struct ieee80211_mmie *mmie;
  686. u8 aad[20];
  687. u64 pn64;
  688. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  689. return TX_DROP;
  690. skb = skb_peek(&tx->skbs);
  691. info = IEEE80211_SKB_CB(skb);
  692. if (info->control.hw_key &&
  693. !(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIE))
  694. return TX_CONTINUE;
  695. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  696. return TX_DROP;
  697. mmie = skb_put(skb, sizeof(*mmie));
  698. mmie->element_id = WLAN_EID_MMIE;
  699. mmie->length = sizeof(*mmie) - 2;
  700. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  701. /* PN = PN + 1 */
  702. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  703. bip_ipn_set64(mmie->sequence_number, pn64);
  704. if (info->control.hw_key)
  705. return TX_CONTINUE;
  706. bip_aad(skb, aad);
  707. /*
  708. * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
  709. */
  710. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  711. skb->data + 24, skb->len - 24, mmie->mic);
  712. return TX_CONTINUE;
  713. }
  714. ieee80211_tx_result
  715. ieee80211_crypto_aes_cmac_256_encrypt(struct ieee80211_tx_data *tx)
  716. {
  717. struct sk_buff *skb;
  718. struct ieee80211_tx_info *info;
  719. struct ieee80211_key *key = tx->key;
  720. struct ieee80211_mmie_16 *mmie;
  721. u8 aad[20];
  722. u64 pn64;
  723. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  724. return TX_DROP;
  725. skb = skb_peek(&tx->skbs);
  726. info = IEEE80211_SKB_CB(skb);
  727. if (info->control.hw_key &&
  728. !(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIE))
  729. return TX_CONTINUE;
  730. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  731. return TX_DROP;
  732. mmie = skb_put(skb, sizeof(*mmie));
  733. mmie->element_id = WLAN_EID_MMIE;
  734. mmie->length = sizeof(*mmie) - 2;
  735. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  736. /* PN = PN + 1 */
  737. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  738. bip_ipn_set64(mmie->sequence_number, pn64);
  739. if (info->control.hw_key)
  740. return TX_CONTINUE;
  741. bip_aad(skb, aad);
  742. /* MIC = AES-256-CMAC(IGTK, AAD || Management Frame Body || MMIE, 128)
  743. */
  744. ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
  745. skb->data + 24, skb->len - 24, mmie->mic);
  746. return TX_CONTINUE;
  747. }
  748. ieee80211_rx_result
  749. ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
  750. {
  751. struct sk_buff *skb = rx->skb;
  752. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  753. struct ieee80211_key *key = rx->key;
  754. struct ieee80211_mmie *mmie;
  755. u8 aad[20], mic[8], ipn[6];
  756. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  757. if (!ieee80211_is_mgmt(hdr->frame_control))
  758. return RX_CONTINUE;
  759. /* management frames are already linear */
  760. if (skb->len < 24 + sizeof(*mmie))
  761. return RX_DROP_U_SHORT_CMAC;
  762. mmie = (struct ieee80211_mmie *)
  763. (skb->data + skb->len - sizeof(*mmie));
  764. if (mmie->element_id != WLAN_EID_MMIE ||
  765. mmie->length != sizeof(*mmie) - 2)
  766. return RX_DROP_U_BAD_MMIE; /* Invalid MMIE */
  767. bip_ipn_swap(ipn, mmie->sequence_number);
  768. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  769. key->u.aes_cmac.replays++;
  770. return RX_DROP_U_REPLAY;
  771. }
  772. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  773. /* hardware didn't decrypt/verify MIC */
  774. bip_aad(skb, aad);
  775. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  776. skb->data + 24, skb->len - 24, mic);
  777. if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  778. key->u.aes_cmac.icverrors++;
  779. return RX_DROP_U_MIC_FAIL;
  780. }
  781. }
  782. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  783. /* Remove MMIE */
  784. skb_trim(skb, skb->len - sizeof(*mmie));
  785. return RX_CONTINUE;
  786. }
  787. ieee80211_rx_result
  788. ieee80211_crypto_aes_cmac_256_decrypt(struct ieee80211_rx_data *rx)
  789. {
  790. struct sk_buff *skb = rx->skb;
  791. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  792. struct ieee80211_key *key = rx->key;
  793. struct ieee80211_mmie_16 *mmie;
  794. u8 aad[20], mic[16], ipn[6];
  795. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  796. if (!ieee80211_is_mgmt(hdr->frame_control))
  797. return RX_CONTINUE;
  798. /* management frames are already linear */
  799. if (skb->len < 24 + sizeof(*mmie))
  800. return RX_DROP_U_SHORT_CMAC256;
  801. mmie = (struct ieee80211_mmie_16 *)
  802. (skb->data + skb->len - sizeof(*mmie));
  803. if (mmie->element_id != WLAN_EID_MMIE ||
  804. mmie->length != sizeof(*mmie) - 2)
  805. return RX_DROP_U_BAD_MMIE; /* Invalid MMIE */
  806. bip_ipn_swap(ipn, mmie->sequence_number);
  807. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  808. key->u.aes_cmac.replays++;
  809. return RX_DROP_U_REPLAY;
  810. }
  811. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  812. /* hardware didn't decrypt/verify MIC */
  813. bip_aad(skb, aad);
  814. ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
  815. skb->data + 24, skb->len - 24, mic);
  816. if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  817. key->u.aes_cmac.icverrors++;
  818. return RX_DROP_U_MIC_FAIL;
  819. }
  820. }
  821. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  822. /* Remove MMIE */
  823. skb_trim(skb, skb->len - sizeof(*mmie));
  824. return RX_CONTINUE;
  825. }
  826. ieee80211_tx_result
  827. ieee80211_crypto_aes_gmac_encrypt(struct ieee80211_tx_data *tx)
  828. {
  829. struct sk_buff *skb;
  830. struct ieee80211_tx_info *info;
  831. struct ieee80211_key *key = tx->key;
  832. struct ieee80211_mmie_16 *mmie;
  833. struct ieee80211_hdr *hdr;
  834. u8 aad[GMAC_AAD_LEN];
  835. u64 pn64;
  836. u8 nonce[GMAC_NONCE_LEN];
  837. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  838. return TX_DROP;
  839. skb = skb_peek(&tx->skbs);
  840. info = IEEE80211_SKB_CB(skb);
  841. if (info->control.hw_key &&
  842. !(key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIE))
  843. return TX_CONTINUE;
  844. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  845. return TX_DROP;
  846. mmie = skb_put(skb, sizeof(*mmie));
  847. mmie->element_id = WLAN_EID_MMIE;
  848. mmie->length = sizeof(*mmie) - 2;
  849. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  850. /* PN = PN + 1 */
  851. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  852. bip_ipn_set64(mmie->sequence_number, pn64);
  853. if (info->control.hw_key)
  854. return TX_CONTINUE;
  855. bip_aad(skb, aad);
  856. hdr = (struct ieee80211_hdr *)skb->data;
  857. memcpy(nonce, hdr->addr2, ETH_ALEN);
  858. bip_ipn_swap(nonce + ETH_ALEN, mmie->sequence_number);
  859. /* MIC = AES-GMAC(IGTK, AAD || Management Frame Body || MMIE, 128) */
  860. if (ieee80211_aes_gmac(key->u.aes_gmac.tfm, aad, nonce,
  861. skb->data + 24, skb->len - 24, mmie->mic) < 0)
  862. return TX_DROP;
  863. return TX_CONTINUE;
  864. }
  865. ieee80211_rx_result
  866. ieee80211_crypto_aes_gmac_decrypt(struct ieee80211_rx_data *rx)
  867. {
  868. struct sk_buff *skb = rx->skb;
  869. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  870. struct ieee80211_key *key = rx->key;
  871. struct ieee80211_mmie_16 *mmie;
  872. u8 aad[GMAC_AAD_LEN], *mic, ipn[6], nonce[GMAC_NONCE_LEN];
  873. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  874. if (!ieee80211_is_mgmt(hdr->frame_control))
  875. return RX_CONTINUE;
  876. /* management frames are already linear */
  877. if (skb->len < 24 + sizeof(*mmie))
  878. return RX_DROP_U_SHORT_GMAC;
  879. mmie = (struct ieee80211_mmie_16 *)
  880. (skb->data + skb->len - sizeof(*mmie));
  881. if (mmie->element_id != WLAN_EID_MMIE ||
  882. mmie->length != sizeof(*mmie) - 2)
  883. return RX_DROP_U_BAD_MMIE; /* Invalid MMIE */
  884. bip_ipn_swap(ipn, mmie->sequence_number);
  885. if (memcmp(ipn, key->u.aes_gmac.rx_pn, 6) <= 0) {
  886. key->u.aes_gmac.replays++;
  887. return RX_DROP_U_REPLAY;
  888. }
  889. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  890. /* hardware didn't decrypt/verify MIC */
  891. bip_aad(skb, aad);
  892. memcpy(nonce, hdr->addr2, ETH_ALEN);
  893. memcpy(nonce + ETH_ALEN, ipn, 6);
  894. mic = kmalloc(GMAC_MIC_LEN, GFP_ATOMIC);
  895. if (!mic)
  896. return RX_DROP_U_OOM;
  897. if (ieee80211_aes_gmac(key->u.aes_gmac.tfm, aad, nonce,
  898. skb->data + 24, skb->len - 24,
  899. mic) < 0 ||
  900. crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  901. key->u.aes_gmac.icverrors++;
  902. kfree(mic);
  903. return RX_DROP_U_MIC_FAIL;
  904. }
  905. kfree(mic);
  906. }
  907. memcpy(key->u.aes_gmac.rx_pn, ipn, 6);
  908. /* Remove MMIE */
  909. skb_trim(skb, skb->len - sizeof(*mmie));
  910. return RX_CONTINUE;
  911. }