wpa.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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;
  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 tail;
  33. hdr = (struct ieee80211_hdr *)skb->data;
  34. if (!tx->key || tx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  35. skb->len < 24 || !ieee80211_is_data_present(hdr->frame_control))
  36. return TX_CONTINUE;
  37. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  38. if (skb->len < hdrlen)
  39. return TX_DROP;
  40. data = skb->data + hdrlen;
  41. data_len = skb->len - hdrlen;
  42. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE)) {
  43. /* Need to use software crypto for the test */
  44. info->control.hw_key = NULL;
  45. }
  46. if (info->control.hw_key &&
  47. !(tx->flags & IEEE80211_TX_FRAGMENTED) &&
  48. !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  49. /* hwaccel - with no need for SW-generated MMIC */
  50. return TX_CONTINUE;
  51. }
  52. tail = MICHAEL_MIC_LEN;
  53. if (!info->control.hw_key)
  54. tail += TKIP_ICV_LEN;
  55. if (WARN_ON(skb_tailroom(skb) < tail ||
  56. skb_headroom(skb) < TKIP_IV_LEN))
  57. return TX_DROP;
  58. key = &tx->key->conf.key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY];
  59. mic = skb_put(skb, MICHAEL_MIC_LEN);
  60. michael_mic(key, hdr, data, data_len, mic);
  61. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE))
  62. mic[0]++;
  63. return TX_CONTINUE;
  64. }
  65. ieee80211_rx_result
  66. ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
  67. {
  68. u8 *data, *key = NULL;
  69. size_t data_len;
  70. unsigned int hdrlen;
  71. u8 mic[MICHAEL_MIC_LEN];
  72. struct sk_buff *skb = rx->skb;
  73. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  74. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  75. /*
  76. * it makes no sense to check for MIC errors on anything other
  77. * than data frames.
  78. */
  79. if (!ieee80211_is_data_present(hdr->frame_control))
  80. return RX_CONTINUE;
  81. /*
  82. * No way to verify the MIC if the hardware stripped it or
  83. * the IV with the key index. In this case we have solely rely
  84. * on the driver to set RX_FLAG_MMIC_ERROR in the event of a
  85. * MIC failure report.
  86. */
  87. if (status->flag & (RX_FLAG_MMIC_STRIPPED | RX_FLAG_IV_STRIPPED)) {
  88. if (status->flag & RX_FLAG_MMIC_ERROR)
  89. goto mic_fail;
  90. if (!(status->flag & RX_FLAG_IV_STRIPPED))
  91. goto update_iv;
  92. return RX_CONTINUE;
  93. }
  94. /*
  95. * Some hardware seems to generate Michael MIC failure reports; even
  96. * though, the frame was not encrypted with TKIP and therefore has no
  97. * MIC. Ignore the flag them to avoid triggering countermeasures.
  98. */
  99. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  100. !(status->flag & RX_FLAG_DECRYPTED))
  101. return RX_CONTINUE;
  102. if (rx->sdata->vif.type == NL80211_IFTYPE_AP && rx->key->conf.keyidx) {
  103. /*
  104. * APs with pairwise keys should never receive Michael MIC
  105. * errors for non-zero keyidx because these are reserved for
  106. * group keys and only the AP is sending real multicast
  107. * frames in the BSS. (
  108. */
  109. return RX_DROP_UNUSABLE;
  110. }
  111. if (status->flag & RX_FLAG_MMIC_ERROR)
  112. goto mic_fail;
  113. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  114. if (skb->len < hdrlen + MICHAEL_MIC_LEN)
  115. return RX_DROP_UNUSABLE;
  116. data = skb->data + hdrlen;
  117. data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
  118. key = &rx->key->conf.key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
  119. michael_mic(key, hdr, data, data_len, mic);
  120. if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0)
  121. goto mic_fail;
  122. /* remove Michael MIC from payload */
  123. skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
  124. update_iv:
  125. /* update IV in key information to be able to detect replays */
  126. rx->key->u.tkip.rx[rx->queue].iv32 = rx->tkip_iv32;
  127. rx->key->u.tkip.rx[rx->queue].iv16 = rx->tkip_iv16;
  128. return RX_CONTINUE;
  129. mic_fail:
  130. /*
  131. * In some cases the key can be unset - e.g. a multicast packet, in
  132. * a driver that supports HW encryption. Send up the key idx only if
  133. * the key is set.
  134. */
  135. mac80211_ev_michael_mic_failure(rx->sdata,
  136. rx->key ? rx->key->conf.keyidx : -1,
  137. (void *) skb->data, NULL, GFP_ATOMIC);
  138. return RX_DROP_UNUSABLE;
  139. }
  140. static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  141. {
  142. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  143. struct ieee80211_key *key = tx->key;
  144. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  145. unsigned int hdrlen;
  146. int len, tail;
  147. u8 *pos;
  148. if (info->control.hw_key &&
  149. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
  150. /* hwaccel - with no need for software-generated IV */
  151. return 0;
  152. }
  153. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  154. len = skb->len - hdrlen;
  155. if (info->control.hw_key)
  156. tail = 0;
  157. else
  158. tail = TKIP_ICV_LEN;
  159. if (WARN_ON(skb_tailroom(skb) < tail ||
  160. skb_headroom(skb) < TKIP_IV_LEN))
  161. return -1;
  162. pos = skb_push(skb, TKIP_IV_LEN);
  163. memmove(pos, pos + TKIP_IV_LEN, hdrlen);
  164. pos += hdrlen;
  165. /* Increase IV for the frame */
  166. key->u.tkip.tx.iv16++;
  167. if (key->u.tkip.tx.iv16 == 0)
  168. key->u.tkip.tx.iv32++;
  169. pos = ieee80211_tkip_add_iv(pos, key, key->u.tkip.tx.iv16);
  170. /* hwaccel - with software IV */
  171. if (info->control.hw_key)
  172. return 0;
  173. /* Add room for ICV */
  174. skb_put(skb, TKIP_ICV_LEN);
  175. hdr = (struct ieee80211_hdr *) skb->data;
  176. return ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
  177. key, pos, len, hdr->addr2);
  178. }
  179. ieee80211_tx_result
  180. ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
  181. {
  182. struct sk_buff *skb = tx->skb;
  183. ieee80211_tx_set_protected(tx);
  184. do {
  185. if (tkip_encrypt_skb(tx, skb) < 0)
  186. return TX_DROP;
  187. } while ((skb = skb->next));
  188. return TX_CONTINUE;
  189. }
  190. ieee80211_rx_result
  191. ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
  192. {
  193. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  194. int hdrlen, res, hwaccel = 0;
  195. struct ieee80211_key *key = rx->key;
  196. struct sk_buff *skb = rx->skb;
  197. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  198. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  199. if (!ieee80211_is_data(hdr->frame_control))
  200. return RX_CONTINUE;
  201. if (!rx->sta || skb->len - hdrlen < 12)
  202. return RX_DROP_UNUSABLE;
  203. /*
  204. * Let TKIP code verify IV, but skip decryption.
  205. * In the case where hardware checks the IV as well,
  206. * we don't even get here, see ieee80211_rx_h_decrypt()
  207. */
  208. if (status->flag & RX_FLAG_DECRYPTED)
  209. hwaccel = 1;
  210. res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
  211. key, skb->data + hdrlen,
  212. skb->len - hdrlen, rx->sta->sta.addr,
  213. hdr->addr1, hwaccel, rx->queue,
  214. &rx->tkip_iv32,
  215. &rx->tkip_iv16);
  216. if (res != TKIP_DECRYPT_OK)
  217. return RX_DROP_UNUSABLE;
  218. /* Trim ICV */
  219. skb_trim(skb, skb->len - TKIP_ICV_LEN);
  220. /* Remove IV */
  221. memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
  222. skb_pull(skb, TKIP_IV_LEN);
  223. return RX_CONTINUE;
  224. }
  225. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
  226. int encrypted)
  227. {
  228. __le16 mask_fc;
  229. int a4_included, mgmt;
  230. u8 qos_tid;
  231. u8 *b_0, *aad;
  232. u16 data_len, len_a;
  233. unsigned int hdrlen;
  234. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  235. b_0 = scratch + 3 * AES_BLOCK_LEN;
  236. aad = scratch + 4 * AES_BLOCK_LEN;
  237. /*
  238. * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  239. * Retry, PwrMgt, MoreData; set Protected
  240. */
  241. mgmt = ieee80211_is_mgmt(hdr->frame_control);
  242. mask_fc = hdr->frame_control;
  243. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  244. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  245. if (!mgmt)
  246. mask_fc &= ~cpu_to_le16(0x0070);
  247. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  248. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  249. len_a = hdrlen - 2;
  250. a4_included = ieee80211_has_a4(hdr->frame_control);
  251. if (ieee80211_is_data_qos(hdr->frame_control))
  252. qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  253. else
  254. qos_tid = 0;
  255. data_len = skb->len - hdrlen - CCMP_HDR_LEN;
  256. if (encrypted)
  257. data_len -= CCMP_MIC_LEN;
  258. /* First block, b_0 */
  259. b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
  260. /* Nonce: Nonce Flags | A2 | PN
  261. * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
  262. */
  263. b_0[1] = qos_tid | (mgmt << 4);
  264. memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
  265. memcpy(&b_0[8], pn, CCMP_PN_LEN);
  266. /* l(m) */
  267. put_unaligned_be16(data_len, &b_0[14]);
  268. /* AAD (extra authenticate-only data) / masked 802.11 header
  269. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  270. put_unaligned_be16(len_a, &aad[0]);
  271. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  272. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  273. /* Mask Seq#, leave Frag# */
  274. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  275. aad[23] = 0;
  276. if (a4_included) {
  277. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  278. aad[30] = qos_tid;
  279. aad[31] = 0;
  280. } else {
  281. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  282. aad[24] = qos_tid;
  283. }
  284. }
  285. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  286. {
  287. hdr[0] = pn[5];
  288. hdr[1] = pn[4];
  289. hdr[2] = 0;
  290. hdr[3] = 0x20 | (key_id << 6);
  291. hdr[4] = pn[3];
  292. hdr[5] = pn[2];
  293. hdr[6] = pn[1];
  294. hdr[7] = pn[0];
  295. }
  296. static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
  297. {
  298. pn[0] = hdr[7];
  299. pn[1] = hdr[6];
  300. pn[2] = hdr[5];
  301. pn[3] = hdr[4];
  302. pn[4] = hdr[1];
  303. pn[5] = hdr[0];
  304. }
  305. static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  306. {
  307. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  308. struct ieee80211_key *key = tx->key;
  309. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  310. int hdrlen, len, tail;
  311. u8 *pos, *pn;
  312. int i;
  313. if (info->control.hw_key &&
  314. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
  315. /*
  316. * hwaccel has no need for preallocated room for CCMP
  317. * header or MIC fields
  318. */
  319. return 0;
  320. }
  321. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  322. len = skb->len - hdrlen;
  323. if (info->control.hw_key)
  324. tail = 0;
  325. else
  326. tail = CCMP_MIC_LEN;
  327. if (WARN_ON(skb_tailroom(skb) < tail ||
  328. skb_headroom(skb) < CCMP_HDR_LEN))
  329. return -1;
  330. pos = skb_push(skb, CCMP_HDR_LEN);
  331. memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
  332. hdr = (struct ieee80211_hdr *) pos;
  333. pos += hdrlen;
  334. /* PN = PN + 1 */
  335. pn = key->u.ccmp.tx_pn;
  336. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  337. pn[i]++;
  338. if (pn[i])
  339. break;
  340. }
  341. ccmp_pn2hdr(pos, pn, key->conf.keyidx);
  342. /* hwaccel - with software CCMP header */
  343. if (info->control.hw_key)
  344. return 0;
  345. pos += CCMP_HDR_LEN;
  346. ccmp_special_blocks(skb, pn, key->u.ccmp.tx_crypto_buf, 0);
  347. ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, key->u.ccmp.tx_crypto_buf, pos, len,
  348. pos, skb_put(skb, CCMP_MIC_LEN));
  349. return 0;
  350. }
  351. ieee80211_tx_result
  352. ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
  353. {
  354. struct sk_buff *skb = tx->skb;
  355. ieee80211_tx_set_protected(tx);
  356. do {
  357. if (ccmp_encrypt_skb(tx, skb) < 0)
  358. return TX_DROP;
  359. } while ((skb = skb->next));
  360. return TX_CONTINUE;
  361. }
  362. ieee80211_rx_result
  363. ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
  364. {
  365. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  366. int hdrlen;
  367. struct ieee80211_key *key = rx->key;
  368. struct sk_buff *skb = rx->skb;
  369. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  370. u8 pn[CCMP_PN_LEN];
  371. int data_len;
  372. int queue;
  373. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  374. if (!ieee80211_is_data(hdr->frame_control) &&
  375. !ieee80211_is_robust_mgmt_frame(hdr))
  376. return RX_CONTINUE;
  377. data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
  378. if (!rx->sta || data_len < 0)
  379. return RX_DROP_UNUSABLE;
  380. ccmp_hdr2pn(pn, skb->data + hdrlen);
  381. queue = ieee80211_is_mgmt(hdr->frame_control) ?
  382. NUM_RX_DATA_QUEUES : rx->queue;
  383. if (memcmp(pn, key->u.ccmp.rx_pn[queue], CCMP_PN_LEN) <= 0) {
  384. key->u.ccmp.replays++;
  385. return RX_DROP_UNUSABLE;
  386. }
  387. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  388. /* hardware didn't decrypt/verify MIC */
  389. ccmp_special_blocks(skb, pn, key->u.ccmp.rx_crypto_buf, 1);
  390. if (ieee80211_aes_ccm_decrypt(
  391. key->u.ccmp.tfm, key->u.ccmp.rx_crypto_buf,
  392. skb->data + hdrlen + CCMP_HDR_LEN, data_len,
  393. skb->data + skb->len - CCMP_MIC_LEN,
  394. skb->data + hdrlen + CCMP_HDR_LEN))
  395. return RX_DROP_UNUSABLE;
  396. }
  397. memcpy(key->u.ccmp.rx_pn[queue], pn, CCMP_PN_LEN);
  398. /* Remove CCMP header and MIC */
  399. skb_trim(skb, skb->len - CCMP_MIC_LEN);
  400. memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
  401. skb_pull(skb, CCMP_HDR_LEN);
  402. return RX_CONTINUE;
  403. }
  404. static void bip_aad(struct sk_buff *skb, u8 *aad)
  405. {
  406. /* BIP AAD: FC(masked) || A1 || A2 || A3 */
  407. /* FC type/subtype */
  408. aad[0] = skb->data[0];
  409. /* Mask FC Retry, PwrMgt, MoreData flags to zero */
  410. aad[1] = skb->data[1] & ~(BIT(4) | BIT(5) | BIT(6));
  411. /* A1 || A2 || A3 */
  412. memcpy(aad + 2, skb->data + 4, 3 * ETH_ALEN);
  413. }
  414. static inline void bip_ipn_swap(u8 *d, const u8 *s)
  415. {
  416. *d++ = s[5];
  417. *d++ = s[4];
  418. *d++ = s[3];
  419. *d++ = s[2];
  420. *d++ = s[1];
  421. *d = s[0];
  422. }
  423. ieee80211_tx_result
  424. ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
  425. {
  426. struct sk_buff *skb = tx->skb;
  427. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  428. struct ieee80211_key *key = tx->key;
  429. struct ieee80211_mmie *mmie;
  430. u8 *pn, aad[20];
  431. int i;
  432. if (info->control.hw_key)
  433. return 0;
  434. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  435. return TX_DROP;
  436. mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie));
  437. mmie->element_id = WLAN_EID_MMIE;
  438. mmie->length = sizeof(*mmie) - 2;
  439. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  440. /* PN = PN + 1 */
  441. pn = key->u.aes_cmac.tx_pn;
  442. for (i = sizeof(key->u.aes_cmac.tx_pn) - 1; i >= 0; i--) {
  443. pn[i]++;
  444. if (pn[i])
  445. break;
  446. }
  447. bip_ipn_swap(mmie->sequence_number, pn);
  448. bip_aad(skb, aad);
  449. /*
  450. * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
  451. */
  452. ieee80211_aes_cmac(key->u.aes_cmac.tfm, key->u.aes_cmac.tx_crypto_buf,
  453. aad, skb->data + 24, skb->len - 24, mmie->mic);
  454. return TX_CONTINUE;
  455. }
  456. ieee80211_rx_result
  457. ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
  458. {
  459. struct sk_buff *skb = rx->skb;
  460. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  461. struct ieee80211_key *key = rx->key;
  462. struct ieee80211_mmie *mmie;
  463. u8 aad[20], mic[8], ipn[6];
  464. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  465. if (!ieee80211_is_mgmt(hdr->frame_control))
  466. return RX_CONTINUE;
  467. if (skb->len < 24 + sizeof(*mmie))
  468. return RX_DROP_UNUSABLE;
  469. mmie = (struct ieee80211_mmie *)
  470. (skb->data + skb->len - sizeof(*mmie));
  471. if (mmie->element_id != WLAN_EID_MMIE ||
  472. mmie->length != sizeof(*mmie) - 2)
  473. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  474. bip_ipn_swap(ipn, mmie->sequence_number);
  475. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  476. key->u.aes_cmac.replays++;
  477. return RX_DROP_UNUSABLE;
  478. }
  479. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  480. /* hardware didn't decrypt/verify MIC */
  481. bip_aad(skb, aad);
  482. ieee80211_aes_cmac(key->u.aes_cmac.tfm,
  483. key->u.aes_cmac.rx_crypto_buf, aad,
  484. skb->data + 24, skb->len - 24, mic);
  485. if (memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) {
  486. key->u.aes_cmac.icverrors++;
  487. return RX_DROP_UNUSABLE;
  488. }
  489. }
  490. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  491. /* Remove MMIE */
  492. skb_trim(skb, skb->len - sizeof(*mmie));
  493. return RX_CONTINUE;
  494. }