wpa.c 15 KB

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