wpa.c 12 KB

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