wpa.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * Copyright 2002-2004, Instant802 Networks, Inc.
  3. * Copyright 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/netdevice.h>
  10. #include <linux/types.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/compiler.h>
  13. #include <linux/ieee80211.h>
  14. #include <linux/gfp.h>
  15. #include <asm/unaligned.h>
  16. #include <net/mac80211.h>
  17. #include "ieee80211_i.h"
  18. #include "michael.h"
  19. #include "tkip.h"
  20. #include "aes_ccm.h"
  21. #include "aes_cmac.h"
  22. #include "wpa.h"
  23. ieee80211_tx_result
  24. ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
  25. {
  26. u8 *data, *key, *mic, key_offset;
  27. size_t data_len;
  28. unsigned int hdrlen;
  29. struct ieee80211_hdr *hdr;
  30. struct sk_buff *skb = tx->skb;
  31. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  32. int authenticator;
  33. int tail;
  34. hdr = (struct ieee80211_hdr *)skb->data;
  35. if (!tx->key || tx->key->conf.alg != ALG_TKIP || skb->len < 24 ||
  36. !ieee80211_is_data_present(hdr->frame_control))
  37. return TX_CONTINUE;
  38. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  39. if (skb->len < hdrlen)
  40. return TX_DROP;
  41. data = skb->data + hdrlen;
  42. data_len = skb->len - hdrlen;
  43. if (info->control.hw_key &&
  44. !(tx->flags & IEEE80211_TX_FRAGMENTED) &&
  45. !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  46. /* hwaccel - with no need for SW-generated MMIC */
  47. return TX_CONTINUE;
  48. }
  49. tail = MICHAEL_MIC_LEN;
  50. if (!info->control.hw_key)
  51. tail += TKIP_ICV_LEN;
  52. if (WARN_ON(skb_tailroom(skb) < tail ||
  53. skb_headroom(skb) < TKIP_IV_LEN))
  54. return TX_DROP;
  55. #if 0
  56. authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
  57. #else
  58. authenticator = 1;
  59. #endif
  60. key_offset = authenticator ?
  61. NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY :
  62. NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
  63. key = &tx->key->conf.key[key_offset];
  64. mic = skb_put(skb, MICHAEL_MIC_LEN);
  65. michael_mic(key, hdr, data, data_len, mic);
  66. return TX_CONTINUE;
  67. }
  68. ieee80211_rx_result
  69. ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
  70. {
  71. u8 *data, *key = NULL, key_offset;
  72. size_t data_len;
  73. unsigned int hdrlen;
  74. u8 mic[MICHAEL_MIC_LEN];
  75. struct sk_buff *skb = rx->skb;
  76. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  77. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  78. int authenticator = 1, wpa_test = 0;
  79. /* No way to verify the MIC if the hardware stripped it */
  80. if (status->flag & RX_FLAG_MMIC_STRIPPED)
  81. return RX_CONTINUE;
  82. if (!rx->key || rx->key->conf.alg != ALG_TKIP ||
  83. !ieee80211_has_protected(hdr->frame_control) ||
  84. !ieee80211_is_data_present(hdr->frame_control))
  85. return RX_CONTINUE;
  86. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  87. if (skb->len < hdrlen + MICHAEL_MIC_LEN)
  88. return RX_DROP_UNUSABLE;
  89. data = skb->data + hdrlen;
  90. data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
  91. #if 0
  92. authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
  93. #else
  94. authenticator = 1;
  95. #endif
  96. key_offset = authenticator ?
  97. NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY :
  98. NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
  99. key = &rx->key->conf.key[key_offset];
  100. michael_mic(key, hdr, data, data_len, mic);
  101. if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
  102. if (!(rx->flags & IEEE80211_RX_RA_MATCH))
  103. return RX_DROP_UNUSABLE;
  104. mac80211_ev_michael_mic_failure(rx->sdata, rx->key->conf.keyidx,
  105. (void *) skb->data, NULL,
  106. GFP_ATOMIC);
  107. return RX_DROP_UNUSABLE;
  108. }
  109. /* remove Michael MIC from payload */
  110. skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
  111. /* update IV in key information to be able to detect replays */
  112. rx->key->u.tkip.rx[rx->queue].iv32 = rx->tkip_iv32;
  113. rx->key->u.tkip.rx[rx->queue].iv16 = rx->tkip_iv16;
  114. return RX_CONTINUE;
  115. }
  116. static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  117. {
  118. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  119. struct ieee80211_key *key = tx->key;
  120. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  121. unsigned int hdrlen;
  122. int len, tail;
  123. u8 *pos;
  124. if (info->control.hw_key &&
  125. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
  126. /* hwaccel - with no need for software-generated IV */
  127. return 0;
  128. }
  129. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  130. len = skb->len - hdrlen;
  131. if (info->control.hw_key)
  132. tail = 0;
  133. else
  134. tail = TKIP_ICV_LEN;
  135. if (WARN_ON(skb_tailroom(skb) < tail ||
  136. skb_headroom(skb) < TKIP_IV_LEN))
  137. return -1;
  138. pos = skb_push(skb, TKIP_IV_LEN);
  139. memmove(pos, pos + TKIP_IV_LEN, hdrlen);
  140. pos += hdrlen;
  141. /* Increase IV for the frame */
  142. key->u.tkip.tx.iv16++;
  143. if (key->u.tkip.tx.iv16 == 0)
  144. key->u.tkip.tx.iv32++;
  145. pos = ieee80211_tkip_add_iv(pos, key, key->u.tkip.tx.iv16);
  146. /* hwaccel - with software IV */
  147. if (info->control.hw_key)
  148. return 0;
  149. /* Add room for ICV */
  150. skb_put(skb, TKIP_ICV_LEN);
  151. hdr = (struct ieee80211_hdr *) skb->data;
  152. ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
  153. key, pos, len, hdr->addr2);
  154. return 0;
  155. }
  156. ieee80211_tx_result
  157. ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
  158. {
  159. struct sk_buff *skb = tx->skb;
  160. ieee80211_tx_set_protected(tx);
  161. do {
  162. if (tkip_encrypt_skb(tx, skb) < 0)
  163. return TX_DROP;
  164. } while ((skb = skb->next));
  165. return TX_CONTINUE;
  166. }
  167. ieee80211_rx_result
  168. ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
  169. {
  170. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  171. int hdrlen, res, hwaccel = 0, wpa_test = 0;
  172. struct ieee80211_key *key = rx->key;
  173. struct sk_buff *skb = rx->skb;
  174. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  175. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  176. if (!ieee80211_is_data(hdr->frame_control))
  177. return RX_CONTINUE;
  178. if (!rx->sta || skb->len - hdrlen < 12)
  179. return RX_DROP_UNUSABLE;
  180. if (status->flag & RX_FLAG_DECRYPTED) {
  181. if (status->flag & RX_FLAG_IV_STRIPPED) {
  182. /*
  183. * Hardware took care of all processing, including
  184. * replay protection, and stripped the ICV/IV so
  185. * we cannot do any checks here.
  186. */
  187. return RX_CONTINUE;
  188. }
  189. /* let TKIP code verify IV, but skip decryption */
  190. hwaccel = 1;
  191. }
  192. res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
  193. key, skb->data + hdrlen,
  194. skb->len - hdrlen, rx->sta->sta.addr,
  195. hdr->addr1, hwaccel, rx->queue,
  196. &rx->tkip_iv32,
  197. &rx->tkip_iv16);
  198. if (res != TKIP_DECRYPT_OK || wpa_test)
  199. return RX_DROP_UNUSABLE;
  200. /* Trim ICV */
  201. skb_trim(skb, skb->len - TKIP_ICV_LEN);
  202. /* Remove IV */
  203. memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
  204. skb_pull(skb, TKIP_IV_LEN);
  205. return RX_CONTINUE;
  206. }
  207. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
  208. int encrypted)
  209. {
  210. __le16 mask_fc;
  211. int a4_included, mgmt;
  212. u8 qos_tid;
  213. u8 *b_0, *aad;
  214. u16 data_len, len_a;
  215. unsigned int hdrlen;
  216. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  217. b_0 = scratch + 3 * AES_BLOCK_LEN;
  218. aad = scratch + 4 * AES_BLOCK_LEN;
  219. /*
  220. * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  221. * Retry, PwrMgt, MoreData; set Protected
  222. */
  223. mgmt = ieee80211_is_mgmt(hdr->frame_control);
  224. mask_fc = hdr->frame_control;
  225. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  226. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  227. if (!mgmt)
  228. mask_fc &= ~cpu_to_le16(0x0070);
  229. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  230. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  231. len_a = hdrlen - 2;
  232. a4_included = ieee80211_has_a4(hdr->frame_control);
  233. if (ieee80211_is_data_qos(hdr->frame_control))
  234. qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  235. else
  236. qos_tid = 0;
  237. data_len = skb->len - hdrlen - CCMP_HDR_LEN;
  238. if (encrypted)
  239. data_len -= CCMP_MIC_LEN;
  240. /* First block, b_0 */
  241. b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
  242. /* Nonce: Nonce Flags | A2 | PN
  243. * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
  244. */
  245. b_0[1] = qos_tid | (mgmt << 4);
  246. memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
  247. memcpy(&b_0[8], pn, CCMP_PN_LEN);
  248. /* l(m) */
  249. put_unaligned_be16(data_len, &b_0[14]);
  250. /* AAD (extra authenticate-only data) / masked 802.11 header
  251. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  252. put_unaligned_be16(len_a, &aad[0]);
  253. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  254. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  255. /* Mask Seq#, leave Frag# */
  256. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  257. aad[23] = 0;
  258. if (a4_included) {
  259. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  260. aad[30] = qos_tid;
  261. aad[31] = 0;
  262. } else {
  263. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  264. aad[24] = qos_tid;
  265. }
  266. }
  267. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  268. {
  269. hdr[0] = pn[5];
  270. hdr[1] = pn[4];
  271. hdr[2] = 0;
  272. hdr[3] = 0x20 | (key_id << 6);
  273. hdr[4] = pn[3];
  274. hdr[5] = pn[2];
  275. hdr[6] = pn[1];
  276. hdr[7] = pn[0];
  277. }
  278. static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
  279. {
  280. pn[0] = hdr[7];
  281. pn[1] = hdr[6];
  282. pn[2] = hdr[5];
  283. pn[3] = hdr[4];
  284. pn[4] = hdr[1];
  285. pn[5] = hdr[0];
  286. }
  287. static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  288. {
  289. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  290. struct ieee80211_key *key = tx->key;
  291. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  292. int hdrlen, len, tail;
  293. u8 *pos, *pn;
  294. int i;
  295. if (info->control.hw_key &&
  296. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
  297. /*
  298. * hwaccel has no need for preallocated room for CCMP
  299. * header or MIC fields
  300. */
  301. return 0;
  302. }
  303. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  304. len = skb->len - hdrlen;
  305. if (info->control.hw_key)
  306. tail = 0;
  307. else
  308. tail = CCMP_MIC_LEN;
  309. if (WARN_ON(skb_tailroom(skb) < tail ||
  310. skb_headroom(skb) < CCMP_HDR_LEN))
  311. return -1;
  312. pos = skb_push(skb, CCMP_HDR_LEN);
  313. memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
  314. hdr = (struct ieee80211_hdr *) pos;
  315. pos += hdrlen;
  316. /* PN = PN + 1 */
  317. pn = key->u.ccmp.tx_pn;
  318. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  319. pn[i]++;
  320. if (pn[i])
  321. break;
  322. }
  323. ccmp_pn2hdr(pos, pn, key->conf.keyidx);
  324. /* hwaccel - with software CCMP header */
  325. if (info->control.hw_key)
  326. return 0;
  327. pos += CCMP_HDR_LEN;
  328. ccmp_special_blocks(skb, pn, key->u.ccmp.tx_crypto_buf, 0);
  329. ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, key->u.ccmp.tx_crypto_buf, pos, len,
  330. pos, skb_put(skb, CCMP_MIC_LEN));
  331. return 0;
  332. }
  333. ieee80211_tx_result
  334. ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
  335. {
  336. struct sk_buff *skb = tx->skb;
  337. ieee80211_tx_set_protected(tx);
  338. do {
  339. if (ccmp_encrypt_skb(tx, skb) < 0)
  340. return TX_DROP;
  341. } while ((skb = skb->next));
  342. return TX_CONTINUE;
  343. }
  344. ieee80211_rx_result
  345. ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
  346. {
  347. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  348. int hdrlen;
  349. struct ieee80211_key *key = rx->key;
  350. struct sk_buff *skb = rx->skb;
  351. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  352. u8 pn[CCMP_PN_LEN];
  353. int data_len;
  354. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  355. if (!ieee80211_is_data(hdr->frame_control) &&
  356. !ieee80211_is_robust_mgmt_frame(hdr))
  357. return RX_CONTINUE;
  358. data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
  359. if (!rx->sta || data_len < 0)
  360. return RX_DROP_UNUSABLE;
  361. if ((status->flag & RX_FLAG_DECRYPTED) &&
  362. (status->flag & RX_FLAG_IV_STRIPPED))
  363. return RX_CONTINUE;
  364. ccmp_hdr2pn(pn, skb->data + hdrlen);
  365. if (memcmp(pn, key->u.ccmp.rx_pn[rx->queue], CCMP_PN_LEN) <= 0) {
  366. key->u.ccmp.replays++;
  367. return RX_DROP_UNUSABLE;
  368. }
  369. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  370. /* hardware didn't decrypt/verify MIC */
  371. ccmp_special_blocks(skb, pn, key->u.ccmp.rx_crypto_buf, 1);
  372. if (ieee80211_aes_ccm_decrypt(
  373. key->u.ccmp.tfm, key->u.ccmp.rx_crypto_buf,
  374. skb->data + hdrlen + CCMP_HDR_LEN, data_len,
  375. skb->data + skb->len - CCMP_MIC_LEN,
  376. skb->data + hdrlen + CCMP_HDR_LEN))
  377. return RX_DROP_UNUSABLE;
  378. }
  379. memcpy(key->u.ccmp.rx_pn[rx->queue], pn, CCMP_PN_LEN);
  380. /* Remove CCMP header and MIC */
  381. skb_trim(skb, skb->len - CCMP_MIC_LEN);
  382. memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
  383. skb_pull(skb, CCMP_HDR_LEN);
  384. return RX_CONTINUE;
  385. }
  386. static void bip_aad(struct sk_buff *skb, u8 *aad)
  387. {
  388. /* BIP AAD: FC(masked) || A1 || A2 || A3 */
  389. /* FC type/subtype */
  390. aad[0] = skb->data[0];
  391. /* Mask FC Retry, PwrMgt, MoreData flags to zero */
  392. aad[1] = skb->data[1] & ~(BIT(4) | BIT(5) | BIT(6));
  393. /* A1 || A2 || A3 */
  394. memcpy(aad + 2, skb->data + 4, 3 * ETH_ALEN);
  395. }
  396. static inline void bip_ipn_swap(u8 *d, const u8 *s)
  397. {
  398. *d++ = s[5];
  399. *d++ = s[4];
  400. *d++ = s[3];
  401. *d++ = s[2];
  402. *d++ = s[1];
  403. *d = s[0];
  404. }
  405. ieee80211_tx_result
  406. ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
  407. {
  408. struct sk_buff *skb = tx->skb;
  409. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  410. struct ieee80211_key *key = tx->key;
  411. struct ieee80211_mmie *mmie;
  412. u8 *pn, aad[20];
  413. int i;
  414. if (info->control.hw_key)
  415. return 0;
  416. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  417. return TX_DROP;
  418. mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie));
  419. mmie->element_id = WLAN_EID_MMIE;
  420. mmie->length = sizeof(*mmie) - 2;
  421. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  422. /* PN = PN + 1 */
  423. pn = key->u.aes_cmac.tx_pn;
  424. for (i = sizeof(key->u.aes_cmac.tx_pn) - 1; i >= 0; i--) {
  425. pn[i]++;
  426. if (pn[i])
  427. break;
  428. }
  429. bip_ipn_swap(mmie->sequence_number, pn);
  430. bip_aad(skb, aad);
  431. /*
  432. * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
  433. */
  434. ieee80211_aes_cmac(key->u.aes_cmac.tfm, key->u.aes_cmac.tx_crypto_buf,
  435. aad, skb->data + 24, skb->len - 24, mmie->mic);
  436. return TX_CONTINUE;
  437. }
  438. ieee80211_rx_result
  439. ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
  440. {
  441. struct sk_buff *skb = rx->skb;
  442. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  443. struct ieee80211_key *key = rx->key;
  444. struct ieee80211_mmie *mmie;
  445. u8 aad[20], mic[8], ipn[6];
  446. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  447. if (!ieee80211_is_mgmt(hdr->frame_control))
  448. return RX_CONTINUE;
  449. if ((status->flag & RX_FLAG_DECRYPTED) &&
  450. (status->flag & RX_FLAG_IV_STRIPPED))
  451. return RX_CONTINUE;
  452. if (skb->len < 24 + sizeof(*mmie))
  453. return RX_DROP_UNUSABLE;
  454. mmie = (struct ieee80211_mmie *)
  455. (skb->data + skb->len - sizeof(*mmie));
  456. if (mmie->element_id != WLAN_EID_MMIE ||
  457. mmie->length != sizeof(*mmie) - 2)
  458. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  459. bip_ipn_swap(ipn, mmie->sequence_number);
  460. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  461. key->u.aes_cmac.replays++;
  462. return RX_DROP_UNUSABLE;
  463. }
  464. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  465. /* hardware didn't decrypt/verify MIC */
  466. bip_aad(skb, aad);
  467. ieee80211_aes_cmac(key->u.aes_cmac.tfm,
  468. key->u.aes_cmac.rx_crypto_buf, aad,
  469. skb->data + 24, skb->len - 24, mic);
  470. if (memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) {
  471. key->u.aes_cmac.icverrors++;
  472. return RX_DROP_UNUSABLE;
  473. }
  474. }
  475. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  476. /* Remove MMIE */
  477. skb_trim(skb, skb->len - sizeof(*mmie));
  478. return RX_CONTINUE;
  479. }