wpa.c 14 KB

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