wpa.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  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. #ifdef CONFIG_MAC80211_DEBUG
  267. if (net_ratelimit())
  268. printk(KERN_DEBUG "%s: TKIP decrypt failed for RX "
  269. "frame from %s (res=%d)\n", rx->dev->name,
  270. print_mac(mac, rx->sta->addr), res);
  271. #endif /* CONFIG_MAC80211_DEBUG */
  272. return TXRX_DROP;
  273. }
  274. /* Trim ICV */
  275. skb_trim(skb, skb->len - TKIP_ICV_LEN);
  276. /* Remove IV */
  277. memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
  278. skb_pull(skb, TKIP_IV_LEN);
  279. return TXRX_CONTINUE;
  280. }
  281. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
  282. int encrypted)
  283. {
  284. u16 fc;
  285. int a4_included, qos_included;
  286. u8 qos_tid, *fc_pos, *data, *sa, *da;
  287. int len_a;
  288. size_t data_len;
  289. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  290. fc_pos = (u8 *) &hdr->frame_control;
  291. fc = fc_pos[0] ^ (fc_pos[1] << 8);
  292. a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
  293. (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
  294. ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len);
  295. data_len -= CCMP_HDR_LEN + (encrypted ? CCMP_MIC_LEN : 0);
  296. if (qos_tid & 0x80) {
  297. qos_included = 1;
  298. qos_tid &= 0x0f;
  299. } else
  300. qos_included = 0;
  301. /* First block, b_0 */
  302. b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
  303. /* Nonce: QoS Priority | A2 | PN */
  304. b_0[1] = qos_tid;
  305. memcpy(&b_0[2], hdr->addr2, 6);
  306. memcpy(&b_0[8], pn, CCMP_PN_LEN);
  307. /* l(m) */
  308. b_0[14] = (data_len >> 8) & 0xff;
  309. b_0[15] = data_len & 0xff;
  310. /* AAD (extra authenticate-only data) / masked 802.11 header
  311. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  312. len_a = a4_included ? 28 : 22;
  313. if (qos_included)
  314. len_a += 2;
  315. aad[0] = 0; /* (len_a >> 8) & 0xff; */
  316. aad[1] = len_a & 0xff;
  317. /* Mask FC: zero subtype b4 b5 b6 */
  318. aad[2] = fc_pos[0] & ~(BIT(4) | BIT(5) | BIT(6));
  319. /* Retry, PwrMgt, MoreData; set Protected */
  320. aad[3] = (fc_pos[1] & ~(BIT(3) | BIT(4) | BIT(5))) | BIT(6);
  321. memcpy(&aad[4], &hdr->addr1, 18);
  322. /* Mask Seq#, leave Frag# */
  323. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  324. aad[23] = 0;
  325. if (a4_included) {
  326. memcpy(&aad[24], hdr->addr4, 6);
  327. aad[30] = 0;
  328. aad[31] = 0;
  329. } else
  330. memset(&aad[24], 0, 8);
  331. if (qos_included) {
  332. u8 *dpos = &aad[a4_included ? 30 : 24];
  333. /* Mask QoS Control field */
  334. dpos[0] = qos_tid;
  335. dpos[1] = 0;
  336. }
  337. }
  338. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  339. {
  340. hdr[0] = pn[5];
  341. hdr[1] = pn[4];
  342. hdr[2] = 0;
  343. hdr[3] = 0x20 | (key_id << 6);
  344. hdr[4] = pn[3];
  345. hdr[5] = pn[2];
  346. hdr[6] = pn[1];
  347. hdr[7] = pn[0];
  348. }
  349. static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
  350. {
  351. pn[0] = hdr[7];
  352. pn[1] = hdr[6];
  353. pn[2] = hdr[5];
  354. pn[3] = hdr[4];
  355. pn[4] = hdr[1];
  356. pn[5] = hdr[0];
  357. return (hdr[3] >> 6) & 0x03;
  358. }
  359. static int ccmp_encrypt_skb(struct ieee80211_txrx_data *tx,
  360. struct sk_buff *skb, int test)
  361. {
  362. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  363. struct ieee80211_key *key = tx->key;
  364. int hdrlen, len, tailneed;
  365. u16 fc;
  366. u8 *pos, *pn, *b_0, *aad, *scratch;
  367. int i;
  368. scratch = key->u.ccmp.tx_crypto_buf;
  369. b_0 = scratch + 3 * AES_BLOCK_LEN;
  370. aad = scratch + 4 * AES_BLOCK_LEN;
  371. fc = le16_to_cpu(hdr->frame_control);
  372. hdrlen = ieee80211_get_hdrlen(fc);
  373. len = skb->len - hdrlen;
  374. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
  375. tailneed = 0;
  376. else
  377. tailneed = CCMP_MIC_LEN;
  378. if ((skb_headroom(skb) < CCMP_HDR_LEN ||
  379. skb_tailroom(skb) < tailneed)) {
  380. I802_DEBUG_INC(tx->local->tx_expand_skb_head);
  381. if (unlikely(pskb_expand_head(skb, CCMP_HDR_LEN, tailneed,
  382. GFP_ATOMIC)))
  383. return -1;
  384. }
  385. pos = skb_push(skb, CCMP_HDR_LEN);
  386. memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
  387. hdr = (struct ieee80211_hdr *) pos;
  388. pos += hdrlen;
  389. /* PN = PN + 1 */
  390. pn = key->u.ccmp.tx_pn;
  391. for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
  392. pn[i]++;
  393. if (pn[i])
  394. break;
  395. }
  396. ccmp_pn2hdr(pos, pn, key->conf.keyidx);
  397. if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
  398. /* hwaccel - with preallocated room for CCMP header */
  399. tx->u.tx.control->key_idx = key->conf.hw_key_idx;
  400. return 0;
  401. }
  402. pos += CCMP_HDR_LEN;
  403. ccmp_special_blocks(skb, pn, b_0, aad, 0);
  404. ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
  405. pos, skb_put(skb, CCMP_MIC_LEN));
  406. return 0;
  407. }
  408. ieee80211_txrx_result
  409. ieee80211_crypto_ccmp_encrypt(struct ieee80211_txrx_data *tx)
  410. {
  411. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
  412. u16 fc;
  413. struct sk_buff *skb = tx->skb;
  414. int test = 0;
  415. fc = le16_to_cpu(hdr->frame_control);
  416. if (!WLAN_FC_DATA_PRESENT(fc))
  417. return TXRX_CONTINUE;
  418. tx->u.tx.control->icv_len = CCMP_MIC_LEN;
  419. tx->u.tx.control->iv_len = CCMP_HDR_LEN;
  420. ieee80211_tx_set_iswep(tx);
  421. if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
  422. !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
  423. /* hwaccel - with no need for preallocated room for CCMP "
  424. * header or MIC fields */
  425. tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx;
  426. return TXRX_CONTINUE;
  427. }
  428. if (ccmp_encrypt_skb(tx, skb, test) < 0)
  429. return TXRX_DROP;
  430. if (tx->u.tx.extra_frag) {
  431. int i;
  432. for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
  433. if (ccmp_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
  434. < 0)
  435. return TXRX_DROP;
  436. }
  437. }
  438. return TXRX_CONTINUE;
  439. }
  440. ieee80211_txrx_result
  441. ieee80211_crypto_ccmp_decrypt(struct ieee80211_txrx_data *rx)
  442. {
  443. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  444. u16 fc;
  445. int hdrlen;
  446. struct ieee80211_key *key = rx->key;
  447. struct sk_buff *skb = rx->skb;
  448. u8 pn[CCMP_PN_LEN];
  449. int data_len;
  450. DECLARE_MAC_BUF(mac);
  451. fc = le16_to_cpu(hdr->frame_control);
  452. hdrlen = ieee80211_get_hdrlen(fc);
  453. if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  454. return TXRX_CONTINUE;
  455. data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
  456. if (!rx->sta || data_len < 0)
  457. return TXRX_DROP;
  458. if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
  459. (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
  460. return TXRX_CONTINUE;
  461. (void) ccmp_hdr2pn(pn, skb->data + hdrlen);
  462. if (memcmp(pn, key->u.ccmp.rx_pn[rx->u.rx.queue], CCMP_PN_LEN) <= 0) {
  463. #ifdef CONFIG_MAC80211_DEBUG
  464. u8 *ppn = key->u.ccmp.rx_pn[rx->u.rx.queue];
  465. printk(KERN_DEBUG "%s: CCMP replay detected for RX frame from "
  466. "%s (RX PN %02x%02x%02x%02x%02x%02x <= prev. PN "
  467. "%02x%02x%02x%02x%02x%02x)\n", rx->dev->name,
  468. print_mac(mac, rx->sta->addr),
  469. pn[0], pn[1], pn[2], pn[3], pn[4], pn[5],
  470. ppn[0], ppn[1], ppn[2], ppn[3], ppn[4], ppn[5]);
  471. #endif /* CONFIG_MAC80211_DEBUG */
  472. key->u.ccmp.replays++;
  473. return TXRX_DROP;
  474. }
  475. if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) {
  476. /* hardware didn't decrypt/verify MIC */
  477. u8 *scratch, *b_0, *aad;
  478. scratch = key->u.ccmp.rx_crypto_buf;
  479. b_0 = scratch + 3 * AES_BLOCK_LEN;
  480. aad = scratch + 4 * AES_BLOCK_LEN;
  481. ccmp_special_blocks(skb, pn, b_0, aad, 1);
  482. if (ieee80211_aes_ccm_decrypt(
  483. key->u.ccmp.tfm, scratch, b_0, aad,
  484. skb->data + hdrlen + CCMP_HDR_LEN, data_len,
  485. skb->data + skb->len - CCMP_MIC_LEN,
  486. skb->data + hdrlen + CCMP_HDR_LEN)) {
  487. #ifdef CONFIG_MAC80211_DEBUG
  488. if (net_ratelimit())
  489. printk(KERN_DEBUG "%s: CCMP decrypt failed "
  490. "for RX frame from %s\n", rx->dev->name,
  491. print_mac(mac, rx->sta->addr));
  492. #endif /* CONFIG_MAC80211_DEBUG */
  493. return TXRX_DROP;
  494. }
  495. }
  496. memcpy(key->u.ccmp.rx_pn[rx->u.rx.queue], pn, CCMP_PN_LEN);
  497. /* Remove CCMP header and MIC */
  498. skb_trim(skb, skb->len - CCMP_MIC_LEN);
  499. memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
  500. skb_pull(skb, CCMP_HDR_LEN);
  501. return TXRX_CONTINUE;
  502. }