wpa.c 12 KB

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