wpa.c 17 KB

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