wpa.c 13 KB

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