wpa.c 15 KB

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