wpa.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. /* update IV in key information to be able to detect replays */
  149. rx->key->u.tkip.iv32_rx[rx->u.rx.queue] = rx->u.rx.tkip_iv32;
  150. rx->key->u.tkip.iv16_rx[rx->u.rx.queue] = rx->u.rx.tkip_iv16;
  151. return TXRX_CONTINUE;
  152. }
  153. static int tkip_encrypt_skb(struct ieee80211_txrx_data *tx,
  154. struct sk_buff *skb, int test)
  155. {
  156. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  157. struct ieee80211_key *key = tx->key;
  158. int hdrlen, len, tailneed;
  159. u16 fc;
  160. u8 *pos;
  161. fc = le16_to_cpu(hdr->frame_control);
  162. hdrlen = ieee80211_get_hdrlen(fc);
  163. len = skb->len - hdrlen;
  164. if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
  165. tailneed = 0;
  166. else
  167. tailneed = TKIP_ICV_LEN;
  168. if ((skb_headroom(skb) < TKIP_IV_LEN ||
  169. skb_tailroom(skb) < tailneed)) {
  170. I802_DEBUG_INC(tx->local->tx_expand_skb_head);
  171. if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN, tailneed,
  172. GFP_ATOMIC)))
  173. return -1;
  174. }
  175. pos = skb_push(skb, TKIP_IV_LEN);
  176. memmove(pos, pos + TKIP_IV_LEN, hdrlen);
  177. pos += hdrlen;
  178. /* Increase IV for the frame */
  179. key->u.tkip.iv16++;
  180. if (key->u.tkip.iv16 == 0)
  181. key->u.tkip.iv32++;
  182. if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  183. hdr = (struct ieee80211_hdr *)skb->data;
  184. /* hwaccel - with preallocated room for IV */
  185. ieee80211_tkip_add_iv(pos, key,
  186. (u8) (key->u.tkip.iv16 >> 8),
  187. (u8) (((key->u.tkip.iv16 >> 8) | 0x20) &
  188. 0x7f),
  189. (u8) key->u.tkip.iv16);
  190. tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx;
  191. return 0;
  192. }
  193. /* Add room for ICV */
  194. skb_put(skb, TKIP_ICV_LEN);
  195. hdr = (struct ieee80211_hdr *) skb->data;
  196. ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
  197. key, pos, len, hdr->addr2);
  198. return 0;
  199. }
  200. ieee80211_txrx_result
  201. ieee80211_crypto_tkip_encrypt(struct ieee80211_txrx_data *tx)
  202. {
  203. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  204. u16 fc;
  205. struct sk_buff *skb = tx->skb;
  206. int wpa_test = 0, test = 0;
  207. fc = le16_to_cpu(hdr->frame_control);
  208. if (!WLAN_FC_DATA_PRESENT(fc))
  209. return TXRX_CONTINUE;
  210. tx->u.tx.control->icv_len = TKIP_ICV_LEN;
  211. tx->u.tx.control->iv_len = TKIP_IV_LEN;
  212. ieee80211_tx_set_iswep(tx);
  213. if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
  214. !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  215. !wpa_test) {
  216. /* hwaccel - with no need for preallocated room for IV/ICV */
  217. tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx;
  218. return TXRX_CONTINUE;
  219. }
  220. if (tkip_encrypt_skb(tx, skb, test) < 0)
  221. return TXRX_DROP;
  222. if (tx->u.tx.extra_frag) {
  223. int i;
  224. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  225. if (tkip_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
  226. < 0)
  227. return TXRX_DROP;
  228. }
  229. }
  230. return TXRX_CONTINUE;
  231. }
  232. ieee80211_txrx_result
  233. ieee80211_crypto_tkip_decrypt(struct ieee80211_txrx_data *rx)
  234. {
  235. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  236. u16 fc;
  237. int hdrlen, res, hwaccel = 0, wpa_test = 0;
  238. struct ieee80211_key *key = rx->key;
  239. struct sk_buff *skb = rx->skb;
  240. DECLARE_MAC_BUF(mac);
  241. fc = le16_to_cpu(hdr->frame_control);
  242. hdrlen = ieee80211_get_hdrlen(fc);
  243. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  244. return TXRX_CONTINUE;
  245. if (!rx->sta || skb->len - hdrlen < 12)
  246. return TXRX_DROP;
  247. if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED) {
  248. if (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) {
  249. /*
  250. * Hardware took care of all processing, including
  251. * replay protection, and stripped the ICV/IV so
  252. * we cannot do any checks here.
  253. */
  254. return TXRX_CONTINUE;
  255. }
  256. /* let TKIP code verify IV, but skip decryption */
  257. hwaccel = 1;
  258. }
  259. res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
  260. key, skb->data + hdrlen,
  261. skb->len - hdrlen, rx->sta->addr,
  262. hwaccel, rx->u.rx.queue,
  263. &rx->u.rx.tkip_iv32,
  264. &rx->u.rx.tkip_iv16);
  265. if (res != TKIP_DECRYPT_OK || wpa_test) {
  266. printk(KERN_DEBUG "%s: TKIP decrypt failed for RX frame from "
  267. "%s (res=%d)\n",
  268. rx->dev->name, print_mac(mac, rx->sta->addr), res);
  269. return TXRX_DROP;
  270. }
  271. /* Trim ICV */
  272. skb_trim(skb, skb->len - TKIP_ICV_LEN);
  273. /* Remove IV */
  274. memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
  275. skb_pull(skb, TKIP_IV_LEN);
  276. return TXRX_CONTINUE;
  277. }
  278. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
  279. int encrypted)
  280. {
  281. u16 fc;
  282. int a4_included, qos_included;
  283. u8 qos_tid, *fc_pos, *data, *sa, *da;
  284. int len_a;
  285. size_t data_len;
  286. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  287. fc_pos = (u8 *) &hdr->frame_control;
  288. fc = fc_pos[0] ^ (fc_pos[1] << 8);
  289. a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  290. (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
  291. ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len);
  292. data_len -= CCMP_HDR_LEN + (encrypted ? CCMP_MIC_LEN : 0);
  293. if (qos_tid & 0x80) {
  294. qos_included = 1;
  295. qos_tid &= 0x0f;
  296. } else
  297. qos_included = 0;
  298. /* First block, b_0 */
  299. b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
  300. /* Nonce: QoS Priority | A2 | PN */
  301. b_0[1] = qos_tid;
  302. memcpy(&b_0[2], hdr->addr2, 6);
  303. memcpy(&b_0[8], pn, CCMP_PN_LEN);
  304. /* l(m) */
  305. b_0[14] = (data_len >> 8) & 0xff;
  306. b_0[15] = data_len & 0xff;
  307. /* AAD (extra authenticate-only data) / masked 802.11 header
  308. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  309. len_a = a4_included ? 28 : 22;
  310. if (qos_included)
  311. len_a += 2;
  312. aad[0] = 0; /* (len_a >> 8) & 0xff; */
  313. aad[1] = len_a & 0xff;
  314. /* Mask FC: zero subtype b4 b5 b6 */
  315. aad[2] = fc_pos[0] & ~(BIT(4) | BIT(5) | BIT(6));
  316. /* Retry, PwrMgt, MoreData; set Protected */
  317. aad[3] = (fc_pos[1] & ~(BIT(3) | BIT(4) | BIT(5))) | BIT(6);
  318. memcpy(&aad[4], &hdr->addr1, 18);
  319. /* Mask Seq#, leave Frag# */
  320. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  321. aad[23] = 0;
  322. if (a4_included) {
  323. memcpy(&aad[24], hdr->addr4, 6);
  324. aad[30] = 0;
  325. aad[31] = 0;
  326. } else
  327. memset(&aad[24], 0, 8);
  328. if (qos_included) {
  329. u8 *dpos = &aad[a4_included ? 30 : 24];
  330. /* Mask QoS Control field */
  331. dpos[0] = qos_tid;
  332. dpos[1] = 0;
  333. }
  334. }
  335. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  336. {
  337. hdr[0] = pn[5];
  338. hdr[1] = pn[4];
  339. hdr[2] = 0;
  340. hdr[3] = 0x20 | (key_id << 6);
  341. hdr[4] = pn[3];
  342. hdr[5] = pn[2];
  343. hdr[6] = pn[1];
  344. hdr[7] = pn[0];
  345. }
  346. static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
  347. {
  348. pn[0] = hdr[7];
  349. pn[1] = hdr[6];
  350. pn[2] = hdr[5];
  351. pn[3] = hdr[4];
  352. pn[4] = hdr[1];
  353. pn[5] = hdr[0];
  354. return (hdr[3] >> 6) & 0x03;
  355. }
  356. static int ccmp_encrypt_skb(struct ieee80211_txrx_data *tx,
  357. struct sk_buff *skb, int test)
  358. {
  359. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  360. struct ieee80211_key *key = tx->key;
  361. int hdrlen, len, tailneed;
  362. u16 fc;
  363. u8 *pos, *pn, *b_0, *aad, *scratch;
  364. int i;
  365. scratch = key->u.ccmp.tx_crypto_buf;
  366. b_0 = scratch + 3 * AES_BLOCK_LEN;
  367. aad = scratch + 4 * AES_BLOCK_LEN;
  368. fc = le16_to_cpu(hdr->frame_control);
  369. hdrlen = ieee80211_get_hdrlen(fc);
  370. len = skb->len - hdrlen;
  371. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
  372. tailneed = 0;
  373. else
  374. tailneed = CCMP_MIC_LEN;
  375. if ((skb_headroom(skb) < CCMP_HDR_LEN ||
  376. skb_tailroom(skb) < tailneed)) {
  377. I802_DEBUG_INC(tx->local->tx_expand_skb_head);
  378. if (unlikely(pskb_expand_head(skb, CCMP_HDR_LEN, tailneed,
  379. GFP_ATOMIC)))
  380. return -1;
  381. }
  382. pos = skb_push(skb, CCMP_HDR_LEN);
  383. memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
  384. hdr = (struct ieee80211_hdr *) pos;
  385. pos += hdrlen;
  386. /* PN = PN + 1 */
  387. pn = key->u.ccmp.tx_pn;
  388. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  389. pn[i]++;
  390. if (pn[i])
  391. break;
  392. }
  393. ccmp_pn2hdr(pos, pn, key->conf.keyidx);
  394. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  395. /* hwaccel - with preallocated room for CCMP header */
  396. tx->u.tx.control->key_idx = key->conf.hw_key_idx;
  397. return 0;
  398. }
  399. pos += CCMP_HDR_LEN;
  400. ccmp_special_blocks(skb, pn, b_0, aad, 0);
  401. ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
  402. pos, skb_put(skb, CCMP_MIC_LEN));
  403. return 0;
  404. }
  405. ieee80211_txrx_result
  406. ieee80211_crypto_ccmp_encrypt(struct ieee80211_txrx_data *tx)
  407. {
  408. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  409. u16 fc;
  410. struct sk_buff *skb = tx->skb;
  411. int test = 0;
  412. fc = le16_to_cpu(hdr->frame_control);
  413. if (!WLAN_FC_DATA_PRESENT(fc))
  414. return TXRX_CONTINUE;
  415. tx->u.tx.control->icv_len = CCMP_MIC_LEN;
  416. tx->u.tx.control->iv_len = CCMP_HDR_LEN;
  417. ieee80211_tx_set_iswep(tx);
  418. if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
  419. !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
  420. /* hwaccel - with no need for preallocated room for CCMP "
  421. * header or MIC fields */
  422. tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx;
  423. return TXRX_CONTINUE;
  424. }
  425. if (ccmp_encrypt_skb(tx, skb, test) < 0)
  426. return TXRX_DROP;
  427. if (tx->u.tx.extra_frag) {
  428. int i;
  429. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  430. if (ccmp_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
  431. < 0)
  432. return TXRX_DROP;
  433. }
  434. }
  435. return TXRX_CONTINUE;
  436. }
  437. ieee80211_txrx_result
  438. ieee80211_crypto_ccmp_decrypt(struct ieee80211_txrx_data *rx)
  439. {
  440. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  441. u16 fc;
  442. int hdrlen;
  443. struct ieee80211_key *key = rx->key;
  444. struct sk_buff *skb = rx->skb;
  445. u8 pn[CCMP_PN_LEN];
  446. int data_len;
  447. DECLARE_MAC_BUF(mac);
  448. fc = le16_to_cpu(hdr->frame_control);
  449. hdrlen = ieee80211_get_hdrlen(fc);
  450. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  451. return TXRX_CONTINUE;
  452. data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
  453. if (!rx->sta || data_len < 0)
  454. return TXRX_DROP;
  455. if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
  456. (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
  457. return TXRX_CONTINUE;
  458. (void) ccmp_hdr2pn(pn, skb->data + hdrlen);
  459. if (memcmp(pn, key->u.ccmp.rx_pn[rx->u.rx.queue], CCMP_PN_LEN) <= 0) {
  460. #ifdef CONFIG_MAC80211_DEBUG
  461. u8 *ppn = key->u.ccmp.rx_pn[rx->u.rx.queue];
  462. printk(KERN_DEBUG "%s: CCMP replay detected for RX frame from "
  463. "%s (RX PN %02x%02x%02x%02x%02x%02x <= prev. PN "
  464. "%02x%02x%02x%02x%02x%02x)\n", rx->dev->name,
  465. print_mac(mac, rx->sta->addr),
  466. pn[0], pn[1], pn[2], pn[3], pn[4], pn[5],
  467. ppn[0], ppn[1], ppn[2], ppn[3], ppn[4], ppn[5]);
  468. #endif /* CONFIG_MAC80211_DEBUG */
  469. key->u.ccmp.replays++;
  470. return TXRX_DROP;
  471. }
  472. if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) {
  473. /* hardware didn't decrypt/verify MIC */
  474. u8 *scratch, *b_0, *aad;
  475. scratch = key->u.ccmp.rx_crypto_buf;
  476. b_0 = scratch + 3 * AES_BLOCK_LEN;
  477. aad = scratch + 4 * AES_BLOCK_LEN;
  478. ccmp_special_blocks(skb, pn, b_0, aad, 1);
  479. if (ieee80211_aes_ccm_decrypt(
  480. key->u.ccmp.tfm, scratch, b_0, aad,
  481. skb->data + hdrlen + CCMP_HDR_LEN, data_len,
  482. skb->data + skb->len - CCMP_MIC_LEN,
  483. skb->data + hdrlen + CCMP_HDR_LEN)) {
  484. printk(KERN_DEBUG "%s: CCMP decrypt failed for RX "
  485. "frame from %s\n", rx->dev->name,
  486. print_mac(mac, rx->sta->addr));
  487. return TXRX_DROP;
  488. }
  489. }
  490. memcpy(key->u.ccmp.rx_pn[rx->u.rx.queue], pn, CCMP_PN_LEN);
  491. /* Remove CCMP header and MIC */
  492. skb_trim(skb, skb->len - CCMP_MIC_LEN);
  493. memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
  494. skb_pull(skb, CCMP_HDR_LEN);
  495. return TXRX_CONTINUE;
  496. }