wpa.c 12 KB

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