wpa.c 13 KB

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