wpa.c 17 KB

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