wpa.c 16 KB

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