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