wpa.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * Copyright 2002-2004, Instant802 Networks, Inc.
  3. * Copyright 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/netdevice.h>
  10. #include <linux/types.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/compiler.h>
  13. #include <linux/ieee80211.h>
  14. #include <linux/gfp.h>
  15. #include <asm/unaligned.h>
  16. #include <net/mac80211.h>
  17. #include <crypto/aes.h>
  18. #include "ieee80211_i.h"
  19. #include "michael.h"
  20. #include "tkip.h"
  21. #include "aes_ccm.h"
  22. #include "aes_cmac.h"
  23. #include "wpa.h"
  24. ieee80211_tx_result
  25. ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
  26. {
  27. u8 *data, *key, *mic;
  28. size_t data_len;
  29. unsigned int hdrlen;
  30. struct ieee80211_hdr *hdr;
  31. struct sk_buff *skb = tx->skb;
  32. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  33. int tail;
  34. hdr = (struct ieee80211_hdr *)skb->data;
  35. if (!tx->key || tx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  36. skb->len < 24 || !ieee80211_is_data_present(hdr->frame_control))
  37. return TX_CONTINUE;
  38. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  39. if (skb->len < hdrlen)
  40. return TX_DROP;
  41. data = skb->data + hdrlen;
  42. data_len = skb->len - hdrlen;
  43. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE)) {
  44. /* Need to use software crypto for the test */
  45. info->control.hw_key = NULL;
  46. }
  47. if (info->control.hw_key &&
  48. (info->flags & IEEE80211_TX_CTL_DONTFRAG ||
  49. tx->local->ops->set_frag_threshold) &&
  50. !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  51. /* hwaccel - with no need for SW-generated MMIC */
  52. return TX_CONTINUE;
  53. }
  54. tail = MICHAEL_MIC_LEN;
  55. if (!info->control.hw_key)
  56. tail += TKIP_ICV_LEN;
  57. if (WARN_ON(skb_tailroom(skb) < tail ||
  58. skb_headroom(skb) < TKIP_IV_LEN))
  59. return TX_DROP;
  60. key = &tx->key->conf.key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY];
  61. mic = skb_put(skb, MICHAEL_MIC_LEN);
  62. michael_mic(key, hdr, data, data_len, mic);
  63. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE))
  64. mic[0]++;
  65. return TX_CONTINUE;
  66. }
  67. ieee80211_rx_result
  68. ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
  69. {
  70. u8 *data, *key = NULL;
  71. size_t data_len;
  72. unsigned int hdrlen;
  73. u8 mic[MICHAEL_MIC_LEN];
  74. struct sk_buff *skb = rx->skb;
  75. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  76. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  77. /*
  78. * it makes no sense to check for MIC errors on anything other
  79. * than data frames.
  80. */
  81. if (!ieee80211_is_data_present(hdr->frame_control))
  82. return RX_CONTINUE;
  83. /*
  84. * No way to verify the MIC if the hardware stripped it or
  85. * the IV with the key index. In this case we have solely rely
  86. * on the driver to set RX_FLAG_MMIC_ERROR in the event of a
  87. * MIC failure report.
  88. */
  89. if (status->flag & (RX_FLAG_MMIC_STRIPPED | RX_FLAG_IV_STRIPPED)) {
  90. if (status->flag & RX_FLAG_MMIC_ERROR)
  91. goto mic_fail_no_key;
  92. if (!(status->flag & RX_FLAG_IV_STRIPPED) && rx->key &&
  93. rx->key->conf.cipher == WLAN_CIPHER_SUITE_TKIP)
  94. goto update_iv;
  95. return RX_CONTINUE;
  96. }
  97. /*
  98. * Some hardware seems to generate Michael MIC failure reports; even
  99. * though, the frame was not encrypted with TKIP and therefore has no
  100. * MIC. Ignore the flag them to avoid triggering countermeasures.
  101. */
  102. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  103. !(status->flag & RX_FLAG_DECRYPTED))
  104. return RX_CONTINUE;
  105. if (rx->sdata->vif.type == NL80211_IFTYPE_AP && rx->key->conf.keyidx) {
  106. /*
  107. * APs with pairwise keys should never receive Michael MIC
  108. * errors for non-zero keyidx because these are reserved for
  109. * group keys and only the AP is sending real multicast
  110. * frames in the BSS. (
  111. */
  112. return RX_DROP_UNUSABLE;
  113. }
  114. if (status->flag & RX_FLAG_MMIC_ERROR)
  115. goto mic_fail;
  116. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  117. if (skb->len < hdrlen + MICHAEL_MIC_LEN)
  118. return RX_DROP_UNUSABLE;
  119. if (skb_linearize(rx->skb))
  120. return RX_DROP_UNUSABLE;
  121. hdr = (void *)skb->data;
  122. data = skb->data + hdrlen;
  123. data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
  124. key = &rx->key->conf.key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
  125. michael_mic(key, hdr, data, data_len, mic);
  126. if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0)
  127. goto mic_fail;
  128. /* remove Michael MIC from payload */
  129. skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
  130. update_iv:
  131. /* update IV in key information to be able to detect replays */
  132. rx->key->u.tkip.rx[rx->security_idx].iv32 = rx->tkip_iv32;
  133. rx->key->u.tkip.rx[rx->security_idx].iv16 = rx->tkip_iv16;
  134. return RX_CONTINUE;
  135. mic_fail:
  136. rx->key->u.tkip.mic_failures++;
  137. mic_fail_no_key:
  138. /*
  139. * In some cases the key can be unset - e.g. a multicast packet, in
  140. * a driver that supports HW encryption. Send up the key idx only if
  141. * the key is set.
  142. */
  143. mac80211_ev_michael_mic_failure(rx->sdata,
  144. rx->key ? rx->key->conf.keyidx : -1,
  145. (void *) skb->data, NULL, GFP_ATOMIC);
  146. return RX_DROP_UNUSABLE;
  147. }
  148. static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  149. {
  150. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  151. struct ieee80211_key *key = tx->key;
  152. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  153. unsigned long flags;
  154. unsigned int hdrlen;
  155. int len, tail;
  156. u8 *pos;
  157. if (info->control.hw_key &&
  158. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  159. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  160. /* hwaccel - with no need for software-generated IV */
  161. return 0;
  162. }
  163. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  164. len = skb->len - hdrlen;
  165. if (info->control.hw_key)
  166. tail = 0;
  167. else
  168. tail = TKIP_ICV_LEN;
  169. if (WARN_ON(skb_tailroom(skb) < tail ||
  170. skb_headroom(skb) < TKIP_IV_LEN))
  171. return -1;
  172. pos = skb_push(skb, TKIP_IV_LEN);
  173. memmove(pos, pos + TKIP_IV_LEN, hdrlen);
  174. skb_set_network_header(skb, skb_network_offset(skb) + TKIP_IV_LEN);
  175. pos += hdrlen;
  176. /* the HW only needs room for the IV, but not the actual IV */
  177. if (info->control.hw_key &&
  178. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  179. return 0;
  180. /* Increase IV for the frame */
  181. spin_lock_irqsave(&key->u.tkip.txlock, flags);
  182. key->u.tkip.tx.iv16++;
  183. if (key->u.tkip.tx.iv16 == 0)
  184. key->u.tkip.tx.iv32++;
  185. pos = ieee80211_tkip_add_iv(pos, key);
  186. spin_unlock_irqrestore(&key->u.tkip.txlock, flags);
  187. /* hwaccel - with software IV */
  188. if (info->control.hw_key)
  189. return 0;
  190. /* Add room for ICV */
  191. skb_put(skb, TKIP_ICV_LEN);
  192. return ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
  193. key, skb, pos, len);
  194. }
  195. ieee80211_tx_result
  196. ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
  197. {
  198. struct sk_buff *skb;
  199. ieee80211_tx_set_protected(tx);
  200. skb_queue_walk(&tx->skbs, skb) {
  201. if (tkip_encrypt_skb(tx, skb) < 0)
  202. return TX_DROP;
  203. }
  204. return TX_CONTINUE;
  205. }
  206. ieee80211_rx_result
  207. ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
  208. {
  209. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  210. int hdrlen, res, hwaccel = 0;
  211. struct ieee80211_key *key = rx->key;
  212. struct sk_buff *skb = rx->skb;
  213. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  214. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  215. if (!ieee80211_is_data(hdr->frame_control))
  216. return RX_CONTINUE;
  217. if (!rx->sta || skb->len - hdrlen < 12)
  218. return RX_DROP_UNUSABLE;
  219. /* it may be possible to optimize this a bit more */
  220. if (skb_linearize(rx->skb))
  221. return RX_DROP_UNUSABLE;
  222. hdr = (void *)skb->data;
  223. /*
  224. * Let TKIP code verify IV, but skip decryption.
  225. * In the case where hardware checks the IV as well,
  226. * we don't even get here, see ieee80211_rx_h_decrypt()
  227. */
  228. if (status->flag & RX_FLAG_DECRYPTED)
  229. hwaccel = 1;
  230. res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
  231. key, skb->data + hdrlen,
  232. skb->len - hdrlen, rx->sta->sta.addr,
  233. hdr->addr1, hwaccel, rx->security_idx,
  234. &rx->tkip_iv32,
  235. &rx->tkip_iv16);
  236. if (res != TKIP_DECRYPT_OK)
  237. return RX_DROP_UNUSABLE;
  238. /* Trim ICV */
  239. skb_trim(skb, skb->len - TKIP_ICV_LEN);
  240. /* Remove IV */
  241. memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
  242. skb_pull(skb, TKIP_IV_LEN);
  243. return RX_CONTINUE;
  244. }
  245. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
  246. int encrypted)
  247. {
  248. __le16 mask_fc;
  249. int a4_included, mgmt;
  250. u8 qos_tid;
  251. u8 *b_0, *aad;
  252. u16 data_len, len_a;
  253. unsigned int hdrlen;
  254. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  255. memset(scratch, 0, 6 * AES_BLOCK_SIZE);
  256. b_0 = scratch + 3 * AES_BLOCK_SIZE;
  257. aad = scratch + 4 * AES_BLOCK_SIZE;
  258. /*
  259. * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  260. * Retry, PwrMgt, MoreData; set Protected
  261. */
  262. mgmt = ieee80211_is_mgmt(hdr->frame_control);
  263. mask_fc = hdr->frame_control;
  264. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  265. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  266. if (!mgmt)
  267. mask_fc &= ~cpu_to_le16(0x0070);
  268. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  269. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  270. len_a = hdrlen - 2;
  271. a4_included = ieee80211_has_a4(hdr->frame_control);
  272. if (ieee80211_is_data_qos(hdr->frame_control))
  273. qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  274. else
  275. qos_tid = 0;
  276. data_len = skb->len - hdrlen - CCMP_HDR_LEN;
  277. if (encrypted)
  278. data_len -= CCMP_MIC_LEN;
  279. /* First block, b_0 */
  280. b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
  281. /* Nonce: Nonce Flags | A2 | PN
  282. * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
  283. */
  284. b_0[1] = qos_tid | (mgmt << 4);
  285. memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
  286. memcpy(&b_0[8], pn, CCMP_PN_LEN);
  287. /* l(m) */
  288. put_unaligned_be16(data_len, &b_0[14]);
  289. /* AAD (extra authenticate-only data) / masked 802.11 header
  290. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  291. put_unaligned_be16(len_a, &aad[0]);
  292. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  293. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  294. /* Mask Seq#, leave Frag# */
  295. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  296. aad[23] = 0;
  297. if (a4_included) {
  298. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  299. aad[30] = qos_tid;
  300. aad[31] = 0;
  301. } else {
  302. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  303. aad[24] = qos_tid;
  304. }
  305. }
  306. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  307. {
  308. hdr[0] = pn[5];
  309. hdr[1] = pn[4];
  310. hdr[2] = 0;
  311. hdr[3] = 0x20 | (key_id << 6);
  312. hdr[4] = pn[3];
  313. hdr[5] = pn[2];
  314. hdr[6] = pn[1];
  315. hdr[7] = pn[0];
  316. }
  317. static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
  318. {
  319. pn[0] = hdr[7];
  320. pn[1] = hdr[6];
  321. pn[2] = hdr[5];
  322. pn[3] = hdr[4];
  323. pn[4] = hdr[1];
  324. pn[5] = hdr[0];
  325. }
  326. static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  327. {
  328. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  329. struct ieee80211_key *key = tx->key;
  330. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  331. int hdrlen, len, tail;
  332. u8 *pos;
  333. u8 pn[6];
  334. u64 pn64;
  335. u8 scratch[6 * AES_BLOCK_SIZE];
  336. if (info->control.hw_key &&
  337. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  338. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  339. /*
  340. * hwaccel has no need for preallocated room for CCMP
  341. * header or MIC fields
  342. */
  343. return 0;
  344. }
  345. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  346. len = skb->len - hdrlen;
  347. if (info->control.hw_key)
  348. tail = 0;
  349. else
  350. tail = CCMP_MIC_LEN;
  351. if (WARN_ON(skb_tailroom(skb) < tail ||
  352. skb_headroom(skb) < CCMP_HDR_LEN))
  353. return -1;
  354. pos = skb_push(skb, CCMP_HDR_LEN);
  355. memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
  356. skb_set_network_header(skb, skb_network_offset(skb) + CCMP_HDR_LEN);
  357. /* the HW only needs room for the IV, but not the actual IV */
  358. if (info->control.hw_key &&
  359. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  360. return 0;
  361. hdr = (struct ieee80211_hdr *) pos;
  362. pos += hdrlen;
  363. pn64 = atomic64_inc_return(&key->u.ccmp.tx_pn);
  364. pn[5] = pn64;
  365. pn[4] = pn64 >> 8;
  366. pn[3] = pn64 >> 16;
  367. pn[2] = pn64 >> 24;
  368. pn[1] = pn64 >> 32;
  369. pn[0] = pn64 >> 40;
  370. ccmp_pn2hdr(pos, pn, key->conf.keyidx);
  371. /* hwaccel - with software CCMP header */
  372. if (info->control.hw_key)
  373. return 0;
  374. pos += CCMP_HDR_LEN;
  375. ccmp_special_blocks(skb, pn, scratch, 0);
  376. ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, pos, len,
  377. pos, skb_put(skb, CCMP_MIC_LEN));
  378. return 0;
  379. }
  380. ieee80211_tx_result
  381. ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
  382. {
  383. struct sk_buff *skb;
  384. ieee80211_tx_set_protected(tx);
  385. skb_queue_walk(&tx->skbs, skb) {
  386. if (ccmp_encrypt_skb(tx, skb) < 0)
  387. return TX_DROP;
  388. }
  389. return TX_CONTINUE;
  390. }
  391. ieee80211_rx_result
  392. ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
  393. {
  394. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  395. int hdrlen;
  396. struct ieee80211_key *key = rx->key;
  397. struct sk_buff *skb = rx->skb;
  398. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  399. u8 pn[CCMP_PN_LEN];
  400. int data_len;
  401. int queue;
  402. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  403. if (!ieee80211_is_data(hdr->frame_control) &&
  404. !ieee80211_is_robust_mgmt_frame(hdr))
  405. return RX_CONTINUE;
  406. data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
  407. if (!rx->sta || data_len < 0)
  408. return RX_DROP_UNUSABLE;
  409. if (status->flag & RX_FLAG_DECRYPTED) {
  410. if (!pskb_may_pull(rx->skb, hdrlen + CCMP_HDR_LEN))
  411. return RX_DROP_UNUSABLE;
  412. } else {
  413. if (skb_linearize(rx->skb))
  414. return RX_DROP_UNUSABLE;
  415. }
  416. ccmp_hdr2pn(pn, skb->data + hdrlen);
  417. queue = rx->security_idx;
  418. if (memcmp(pn, key->u.ccmp.rx_pn[queue], CCMP_PN_LEN) <= 0) {
  419. key->u.ccmp.replays++;
  420. return RX_DROP_UNUSABLE;
  421. }
  422. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  423. u8 scratch[6 * AES_BLOCK_SIZE];
  424. /* hardware didn't decrypt/verify MIC */
  425. ccmp_special_blocks(skb, pn, scratch, 1);
  426. if (ieee80211_aes_ccm_decrypt(
  427. key->u.ccmp.tfm, scratch,
  428. skb->data + hdrlen + CCMP_HDR_LEN, data_len,
  429. skb->data + skb->len - CCMP_MIC_LEN,
  430. skb->data + hdrlen + CCMP_HDR_LEN))
  431. return RX_DROP_UNUSABLE;
  432. }
  433. memcpy(key->u.ccmp.rx_pn[queue], pn, CCMP_PN_LEN);
  434. /* Remove CCMP header and MIC */
  435. if (pskb_trim(skb, skb->len - CCMP_MIC_LEN))
  436. return RX_DROP_UNUSABLE;
  437. memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
  438. skb_pull(skb, CCMP_HDR_LEN);
  439. return RX_CONTINUE;
  440. }
  441. static void bip_aad(struct sk_buff *skb, u8 *aad)
  442. {
  443. __le16 mask_fc;
  444. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  445. /* BIP AAD: FC(masked) || A1 || A2 || A3 */
  446. /* FC type/subtype */
  447. /* Mask FC Retry, PwrMgt, MoreData flags to zero */
  448. mask_fc = hdr->frame_control;
  449. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY | IEEE80211_FCTL_PM |
  450. IEEE80211_FCTL_MOREDATA);
  451. put_unaligned(mask_fc, (__le16 *) &aad[0]);
  452. /* A1 || A2 || A3 */
  453. memcpy(aad + 2, &hdr->addr1, 3 * ETH_ALEN);
  454. }
  455. static inline void bip_ipn_set64(u8 *d, u64 pn)
  456. {
  457. *d++ = pn;
  458. *d++ = pn >> 8;
  459. *d++ = pn >> 16;
  460. *d++ = pn >> 24;
  461. *d++ = pn >> 32;
  462. *d = pn >> 40;
  463. }
  464. static inline void bip_ipn_swap(u8 *d, const u8 *s)
  465. {
  466. *d++ = s[5];
  467. *d++ = s[4];
  468. *d++ = s[3];
  469. *d++ = s[2];
  470. *d++ = s[1];
  471. *d = s[0];
  472. }
  473. ieee80211_tx_result
  474. ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
  475. {
  476. struct sk_buff *skb;
  477. struct ieee80211_tx_info *info;
  478. struct ieee80211_key *key = tx->key;
  479. struct ieee80211_mmie *mmie;
  480. u8 aad[20];
  481. u64 pn64;
  482. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  483. return TX_DROP;
  484. skb = skb_peek(&tx->skbs);
  485. info = IEEE80211_SKB_CB(skb);
  486. if (info->control.hw_key)
  487. return TX_CONTINUE;
  488. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  489. return TX_DROP;
  490. mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie));
  491. mmie->element_id = WLAN_EID_MMIE;
  492. mmie->length = sizeof(*mmie) - 2;
  493. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  494. /* PN = PN + 1 */
  495. pn64 = atomic64_inc_return(&key->u.aes_cmac.tx_pn);
  496. bip_ipn_set64(mmie->sequence_number, pn64);
  497. bip_aad(skb, aad);
  498. /*
  499. * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
  500. */
  501. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  502. skb->data + 24, skb->len - 24, mmie->mic);
  503. return TX_CONTINUE;
  504. }
  505. ieee80211_rx_result
  506. ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
  507. {
  508. struct sk_buff *skb = rx->skb;
  509. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  510. struct ieee80211_key *key = rx->key;
  511. struct ieee80211_mmie *mmie;
  512. u8 aad[20], mic[8], ipn[6];
  513. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  514. if (!ieee80211_is_mgmt(hdr->frame_control))
  515. return RX_CONTINUE;
  516. /* management frames are already linear */
  517. if (skb->len < 24 + sizeof(*mmie))
  518. return RX_DROP_UNUSABLE;
  519. mmie = (struct ieee80211_mmie *)
  520. (skb->data + skb->len - sizeof(*mmie));
  521. if (mmie->element_id != WLAN_EID_MMIE ||
  522. mmie->length != sizeof(*mmie) - 2)
  523. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  524. bip_ipn_swap(ipn, mmie->sequence_number);
  525. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  526. key->u.aes_cmac.replays++;
  527. return RX_DROP_UNUSABLE;
  528. }
  529. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  530. /* hardware didn't decrypt/verify MIC */
  531. bip_aad(skb, aad);
  532. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  533. skb->data + 24, skb->len - 24, mic);
  534. if (memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) {
  535. key->u.aes_cmac.icverrors++;
  536. return RX_DROP_UNUSABLE;
  537. }
  538. }
  539. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  540. /* Remove MMIE */
  541. skb_trim(skb, skb->len - sizeof(*mmie));
  542. return RX_CONTINUE;
  543. }
  544. ieee80211_tx_result
  545. ieee80211_crypto_hw_encrypt(struct ieee80211_tx_data *tx)
  546. {
  547. struct sk_buff *skb;
  548. struct ieee80211_tx_info *info = NULL;
  549. skb_queue_walk(&tx->skbs, skb) {
  550. info = IEEE80211_SKB_CB(skb);
  551. /* handle hw-only algorithm */
  552. if (!info->control.hw_key)
  553. return TX_DROP;
  554. }
  555. ieee80211_tx_set_protected(tx);
  556. return TX_CONTINUE;
  557. }