wpa.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 += IEEE80211_TKIP_ICV_LEN;
  57. if (WARN(skb_tailroom(skb) < tail ||
  58. skb_headroom(skb) < IEEE80211_TKIP_IV_LEN,
  59. "mmic: not enough head/tail (%d/%d,%d/%d)\n",
  60. skb_headroom(skb), IEEE80211_TKIP_IV_LEN,
  61. skb_tailroom(skb), tail))
  62. return TX_DROP;
  63. key = &tx->key->conf.key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY];
  64. mic = skb_put(skb, MICHAEL_MIC_LEN);
  65. michael_mic(key, hdr, data, data_len, mic);
  66. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE))
  67. mic[0]++;
  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;
  74. size_t data_len;
  75. unsigned int hdrlen;
  76. u8 mic[MICHAEL_MIC_LEN];
  77. struct sk_buff *skb = rx->skb;
  78. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  79. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  80. /*
  81. * it makes no sense to check for MIC errors on anything other
  82. * than data frames.
  83. */
  84. if (!ieee80211_is_data_present(hdr->frame_control))
  85. return RX_CONTINUE;
  86. /*
  87. * No way to verify the MIC if the hardware stripped it or
  88. * the IV with the key index. In this case we have solely rely
  89. * on the driver to set RX_FLAG_MMIC_ERROR in the event of a
  90. * MIC failure report.
  91. */
  92. if (status->flag & (RX_FLAG_MMIC_STRIPPED | RX_FLAG_IV_STRIPPED)) {
  93. if (status->flag & RX_FLAG_MMIC_ERROR)
  94. goto mic_fail_no_key;
  95. if (!(status->flag & RX_FLAG_IV_STRIPPED) && rx->key &&
  96. rx->key->conf.cipher == WLAN_CIPHER_SUITE_TKIP)
  97. goto update_iv;
  98. return RX_CONTINUE;
  99. }
  100. /*
  101. * Some hardware seems to generate Michael MIC failure reports; even
  102. * though, the frame was not encrypted with TKIP and therefore has no
  103. * MIC. Ignore the flag them to avoid triggering countermeasures.
  104. */
  105. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  106. !(status->flag & RX_FLAG_DECRYPTED))
  107. return RX_CONTINUE;
  108. if (rx->sdata->vif.type == NL80211_IFTYPE_AP && rx->key->conf.keyidx) {
  109. /*
  110. * APs with pairwise keys should never receive Michael MIC
  111. * errors for non-zero keyidx because these are reserved for
  112. * group keys and only the AP is sending real multicast
  113. * frames in the BSS.
  114. */
  115. return RX_DROP_UNUSABLE;
  116. }
  117. if (status->flag & RX_FLAG_MMIC_ERROR)
  118. goto mic_fail;
  119. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  120. if (skb->len < hdrlen + MICHAEL_MIC_LEN)
  121. return RX_DROP_UNUSABLE;
  122. if (skb_linearize(rx->skb))
  123. return RX_DROP_UNUSABLE;
  124. hdr = (void *)skb->data;
  125. data = skb->data + hdrlen;
  126. data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
  127. key = &rx->key->conf.key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
  128. michael_mic(key, hdr, data, data_len, mic);
  129. if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0)
  130. goto mic_fail;
  131. /* remove Michael MIC from payload */
  132. skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
  133. update_iv:
  134. /* update IV in key information to be able to detect replays */
  135. rx->key->u.tkip.rx[rx->security_idx].iv32 = rx->tkip_iv32;
  136. rx->key->u.tkip.rx[rx->security_idx].iv16 = rx->tkip_iv16;
  137. return RX_CONTINUE;
  138. mic_fail:
  139. rx->key->u.tkip.mic_failures++;
  140. mic_fail_no_key:
  141. /*
  142. * In some cases the key can be unset - e.g. a multicast packet, in
  143. * a driver that supports HW encryption. Send up the key idx only if
  144. * the key is set.
  145. */
  146. mac80211_ev_michael_mic_failure(rx->sdata,
  147. rx->key ? rx->key->conf.keyidx : -1,
  148. (void *) skb->data, NULL, GFP_ATOMIC);
  149. return RX_DROP_UNUSABLE;
  150. }
  151. static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  152. {
  153. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  154. struct ieee80211_key *key = tx->key;
  155. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  156. unsigned int hdrlen;
  157. int len, tail;
  158. u8 *pos;
  159. if (info->control.hw_key &&
  160. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  161. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  162. /* hwaccel - with no need for software-generated IV */
  163. return 0;
  164. }
  165. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  166. len = skb->len - hdrlen;
  167. if (info->control.hw_key)
  168. tail = 0;
  169. else
  170. tail = IEEE80211_TKIP_ICV_LEN;
  171. if (WARN_ON(skb_tailroom(skb) < tail ||
  172. skb_headroom(skb) < IEEE80211_TKIP_IV_LEN))
  173. return -1;
  174. pos = skb_push(skb, IEEE80211_TKIP_IV_LEN);
  175. memmove(pos, pos + IEEE80211_TKIP_IV_LEN, hdrlen);
  176. pos += hdrlen;
  177. /* the HW only needs room for the IV, but not the actual IV */
  178. if (info->control.hw_key &&
  179. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  180. return 0;
  181. /* Increase IV for the frame */
  182. spin_lock(&key->u.tkip.txlock);
  183. key->u.tkip.tx.iv16++;
  184. if (key->u.tkip.tx.iv16 == 0)
  185. key->u.tkip.tx.iv32++;
  186. pos = ieee80211_tkip_add_iv(pos, key);
  187. spin_unlock(&key->u.tkip.txlock);
  188. /* hwaccel - with software IV */
  189. if (info->control.hw_key)
  190. return 0;
  191. /* Add room for ICV */
  192. skb_put(skb, IEEE80211_TKIP_ICV_LEN);
  193. return ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
  194. key, skb, pos, len);
  195. }
  196. ieee80211_tx_result
  197. ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
  198. {
  199. struct sk_buff *skb;
  200. ieee80211_tx_set_protected(tx);
  201. skb_queue_walk(&tx->skbs, skb) {
  202. if (tkip_encrypt_skb(tx, skb) < 0)
  203. return TX_DROP;
  204. }
  205. return TX_CONTINUE;
  206. }
  207. ieee80211_rx_result
  208. ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
  209. {
  210. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  211. int hdrlen, res, hwaccel = 0;
  212. struct ieee80211_key *key = rx->key;
  213. struct sk_buff *skb = rx->skb;
  214. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  215. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  216. if (!ieee80211_is_data(hdr->frame_control))
  217. return RX_CONTINUE;
  218. if (!rx->sta || skb->len - hdrlen < 12)
  219. return RX_DROP_UNUSABLE;
  220. /* it may be possible to optimize this a bit more */
  221. if (skb_linearize(rx->skb))
  222. return RX_DROP_UNUSABLE;
  223. hdr = (void *)skb->data;
  224. /*
  225. * Let TKIP code verify IV, but skip decryption.
  226. * In the case where hardware checks the IV as well,
  227. * we don't even get here, see ieee80211_rx_h_decrypt()
  228. */
  229. if (status->flag & RX_FLAG_DECRYPTED)
  230. hwaccel = 1;
  231. res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
  232. key, skb->data + hdrlen,
  233. skb->len - hdrlen, rx->sta->sta.addr,
  234. hdr->addr1, hwaccel, rx->security_idx,
  235. &rx->tkip_iv32,
  236. &rx->tkip_iv16);
  237. if (res != TKIP_DECRYPT_OK)
  238. return RX_DROP_UNUSABLE;
  239. /* Trim ICV */
  240. skb_trim(skb, skb->len - IEEE80211_TKIP_ICV_LEN);
  241. /* Remove IV */
  242. memmove(skb->data + IEEE80211_TKIP_IV_LEN, skb->data, hdrlen);
  243. skb_pull(skb, IEEE80211_TKIP_IV_LEN);
  244. return RX_CONTINUE;
  245. }
  246. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad)
  247. {
  248. __le16 mask_fc;
  249. int a4_included, mgmt;
  250. u8 qos_tid;
  251. u16 len_a;
  252. unsigned int hdrlen;
  253. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  254. /*
  255. * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  256. * Retry, PwrMgt, MoreData; set Protected
  257. */
  258. mgmt = ieee80211_is_mgmt(hdr->frame_control);
  259. mask_fc = hdr->frame_control;
  260. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  261. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  262. if (!mgmt)
  263. mask_fc &= ~cpu_to_le16(0x0070);
  264. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  265. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  266. len_a = hdrlen - 2;
  267. a4_included = ieee80211_has_a4(hdr->frame_control);
  268. if (ieee80211_is_data_qos(hdr->frame_control))
  269. qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  270. else
  271. qos_tid = 0;
  272. /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
  273. * mode authentication are not allowed to collide, yet both are derived
  274. * from this vector b_0. We only set L := 1 here to indicate that the
  275. * data size can be represented in (L+1) bytes. The CCM layer will take
  276. * care of storing the data length in the top (L+1) bytes and setting
  277. * and clearing the other bits as is required to derive the two IVs.
  278. */
  279. b_0[0] = 0x1;
  280. /* Nonce: Nonce Flags | A2 | PN
  281. * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
  282. */
  283. b_0[1] = qos_tid | (mgmt << 4);
  284. memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
  285. memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
  286. /* AAD (extra authenticate-only data) / masked 802.11 header
  287. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  288. put_unaligned_be16(len_a, &aad[0]);
  289. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  290. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  291. /* Mask Seq#, leave Frag# */
  292. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  293. aad[23] = 0;
  294. if (a4_included) {
  295. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  296. aad[30] = qos_tid;
  297. aad[31] = 0;
  298. } else {
  299. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  300. aad[24] = qos_tid;
  301. }
  302. }
  303. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  304. {
  305. hdr[0] = pn[5];
  306. hdr[1] = pn[4];
  307. hdr[2] = 0;
  308. hdr[3] = 0x20 | (key_id << 6);
  309. hdr[4] = pn[3];
  310. hdr[5] = pn[2];
  311. hdr[6] = pn[1];
  312. hdr[7] = pn[0];
  313. }
  314. static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
  315. {
  316. pn[0] = hdr[7];
  317. pn[1] = hdr[6];
  318. pn[2] = hdr[5];
  319. pn[3] = hdr[4];
  320. pn[4] = hdr[1];
  321. pn[5] = hdr[0];
  322. }
  323. static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  324. {
  325. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  326. struct ieee80211_key *key = tx->key;
  327. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  328. int hdrlen, len, tail;
  329. u8 *pos;
  330. u8 pn[6];
  331. u64 pn64;
  332. u8 aad[2 * AES_BLOCK_SIZE];
  333. u8 b_0[AES_BLOCK_SIZE];
  334. if (info->control.hw_key &&
  335. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  336. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  337. !((info->control.hw_key->flags &
  338. IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) &&
  339. ieee80211_is_mgmt(hdr->frame_control))) {
  340. /*
  341. * hwaccel has no need for preallocated room for CCMP
  342. * header or MIC fields
  343. */
  344. return 0;
  345. }
  346. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  347. len = skb->len - hdrlen;
  348. if (info->control.hw_key)
  349. tail = 0;
  350. else
  351. tail = IEEE80211_CCMP_MIC_LEN;
  352. if (WARN_ON(skb_tailroom(skb) < tail ||
  353. skb_headroom(skb) < IEEE80211_CCMP_HDR_LEN))
  354. return -1;
  355. pos = skb_push(skb, IEEE80211_CCMP_HDR_LEN);
  356. memmove(pos, pos + IEEE80211_CCMP_HDR_LEN, hdrlen);
  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 += IEEE80211_CCMP_HDR_LEN;
  375. ccmp_special_blocks(skb, pn, b_0, aad);
  376. ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
  377. skb_put(skb, IEEE80211_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[IEEE80211_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(skb))
  405. return RX_CONTINUE;
  406. data_len = skb->len - hdrlen - IEEE80211_CCMP_HDR_LEN -
  407. IEEE80211_CCMP_MIC_LEN;
  408. if (!rx->sta || data_len < 0)
  409. return RX_DROP_UNUSABLE;
  410. if (status->flag & RX_FLAG_DECRYPTED) {
  411. if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_CCMP_HDR_LEN))
  412. return RX_DROP_UNUSABLE;
  413. } else {
  414. if (skb_linearize(rx->skb))
  415. return RX_DROP_UNUSABLE;
  416. }
  417. ccmp_hdr2pn(pn, skb->data + hdrlen);
  418. queue = rx->security_idx;
  419. if (memcmp(pn, key->u.ccmp.rx_pn[queue], IEEE80211_CCMP_PN_LEN) <= 0) {
  420. key->u.ccmp.replays++;
  421. return RX_DROP_UNUSABLE;
  422. }
  423. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  424. u8 aad[2 * AES_BLOCK_SIZE];
  425. u8 b_0[AES_BLOCK_SIZE];
  426. /* hardware didn't decrypt/verify MIC */
  427. ccmp_special_blocks(skb, pn, b_0, aad);
  428. if (ieee80211_aes_ccm_decrypt(
  429. key->u.ccmp.tfm, b_0, aad,
  430. skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN,
  431. data_len,
  432. skb->data + skb->len - IEEE80211_CCMP_MIC_LEN))
  433. return RX_DROP_UNUSABLE;
  434. }
  435. memcpy(key->u.ccmp.rx_pn[queue], pn, IEEE80211_CCMP_PN_LEN);
  436. /* Remove CCMP header and MIC */
  437. if (pskb_trim(skb, skb->len - IEEE80211_CCMP_MIC_LEN))
  438. return RX_DROP_UNUSABLE;
  439. memmove(skb->data + IEEE80211_CCMP_HDR_LEN, skb->data, hdrlen);
  440. skb_pull(skb, IEEE80211_CCMP_HDR_LEN);
  441. return RX_CONTINUE;
  442. }
  443. static ieee80211_tx_result
  444. ieee80211_crypto_cs_encrypt(struct ieee80211_tx_data *tx,
  445. struct sk_buff *skb)
  446. {
  447. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  448. struct ieee80211_key *key = tx->key;
  449. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  450. const struct ieee80211_cipher_scheme *cs = key->sta->cipher_scheme;
  451. int hdrlen;
  452. u8 *pos;
  453. if (info->control.hw_key &&
  454. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  455. /* hwaccel has no need for preallocated head room */
  456. return TX_CONTINUE;
  457. }
  458. if (unlikely(skb_headroom(skb) < cs->hdr_len &&
  459. pskb_expand_head(skb, cs->hdr_len, 0, GFP_ATOMIC)))
  460. return TX_DROP;
  461. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  462. pos = skb_push(skb, cs->hdr_len);
  463. memmove(pos, pos + cs->hdr_len, hdrlen);
  464. return TX_CONTINUE;
  465. }
  466. static inline int ieee80211_crypto_cs_pn_compare(u8 *pn1, u8 *pn2, int len)
  467. {
  468. int i;
  469. /* pn is little endian */
  470. for (i = len - 1; i >= 0; i--) {
  471. if (pn1[i] < pn2[i])
  472. return -1;
  473. else if (pn1[i] > pn2[i])
  474. return 1;
  475. }
  476. return 0;
  477. }
  478. static ieee80211_rx_result
  479. ieee80211_crypto_cs_decrypt(struct ieee80211_rx_data *rx)
  480. {
  481. struct ieee80211_key *key = rx->key;
  482. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  483. const struct ieee80211_cipher_scheme *cs = NULL;
  484. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  485. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  486. int data_len;
  487. u8 *rx_pn;
  488. u8 *skb_pn;
  489. u8 qos_tid;
  490. if (!rx->sta || !rx->sta->cipher_scheme ||
  491. !(status->flag & RX_FLAG_DECRYPTED))
  492. return RX_DROP_UNUSABLE;
  493. if (!ieee80211_is_data(hdr->frame_control))
  494. return RX_CONTINUE;
  495. cs = rx->sta->cipher_scheme;
  496. data_len = rx->skb->len - hdrlen - cs->hdr_len;
  497. if (data_len < 0)
  498. return RX_DROP_UNUSABLE;
  499. if (ieee80211_is_data_qos(hdr->frame_control))
  500. qos_tid = *ieee80211_get_qos_ctl(hdr) &
  501. IEEE80211_QOS_CTL_TID_MASK;
  502. else
  503. qos_tid = 0;
  504. if (skb_linearize(rx->skb))
  505. return RX_DROP_UNUSABLE;
  506. hdr = (struct ieee80211_hdr *)rx->skb->data;
  507. rx_pn = key->u.gen.rx_pn[qos_tid];
  508. skb_pn = rx->skb->data + hdrlen + cs->pn_off;
  509. if (ieee80211_crypto_cs_pn_compare(skb_pn, rx_pn, cs->pn_len) <= 0)
  510. return RX_DROP_UNUSABLE;
  511. memcpy(rx_pn, skb_pn, cs->pn_len);
  512. /* remove security header and MIC */
  513. if (pskb_trim(rx->skb, rx->skb->len - cs->mic_len))
  514. return RX_DROP_UNUSABLE;
  515. memmove(rx->skb->data + cs->hdr_len, rx->skb->data, hdrlen);
  516. skb_pull(rx->skb, cs->hdr_len);
  517. return RX_CONTINUE;
  518. }
  519. static void bip_aad(struct sk_buff *skb, u8 *aad)
  520. {
  521. __le16 mask_fc;
  522. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  523. /* BIP AAD: FC(masked) || A1 || A2 || A3 */
  524. /* FC type/subtype */
  525. /* Mask FC Retry, PwrMgt, MoreData flags to zero */
  526. mask_fc = hdr->frame_control;
  527. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY | IEEE80211_FCTL_PM |
  528. IEEE80211_FCTL_MOREDATA);
  529. put_unaligned(mask_fc, (__le16 *) &aad[0]);
  530. /* A1 || A2 || A3 */
  531. memcpy(aad + 2, &hdr->addr1, 3 * ETH_ALEN);
  532. }
  533. static inline void bip_ipn_set64(u8 *d, u64 pn)
  534. {
  535. *d++ = pn;
  536. *d++ = pn >> 8;
  537. *d++ = pn >> 16;
  538. *d++ = pn >> 24;
  539. *d++ = pn >> 32;
  540. *d = pn >> 40;
  541. }
  542. static inline void bip_ipn_swap(u8 *d, const u8 *s)
  543. {
  544. *d++ = s[5];
  545. *d++ = s[4];
  546. *d++ = s[3];
  547. *d++ = s[2];
  548. *d++ = s[1];
  549. *d = s[0];
  550. }
  551. ieee80211_tx_result
  552. ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
  553. {
  554. struct sk_buff *skb;
  555. struct ieee80211_tx_info *info;
  556. struct ieee80211_key *key = tx->key;
  557. struct ieee80211_mmie *mmie;
  558. u8 aad[20];
  559. u64 pn64;
  560. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  561. return TX_DROP;
  562. skb = skb_peek(&tx->skbs);
  563. info = IEEE80211_SKB_CB(skb);
  564. if (info->control.hw_key)
  565. return TX_CONTINUE;
  566. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  567. return TX_DROP;
  568. mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie));
  569. mmie->element_id = WLAN_EID_MMIE;
  570. mmie->length = sizeof(*mmie) - 2;
  571. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  572. /* PN = PN + 1 */
  573. pn64 = atomic64_inc_return(&key->u.aes_cmac.tx_pn);
  574. bip_ipn_set64(mmie->sequence_number, pn64);
  575. bip_aad(skb, aad);
  576. /*
  577. * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
  578. */
  579. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  580. skb->data + 24, skb->len - 24, mmie->mic);
  581. return TX_CONTINUE;
  582. }
  583. ieee80211_rx_result
  584. ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
  585. {
  586. struct sk_buff *skb = rx->skb;
  587. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  588. struct ieee80211_key *key = rx->key;
  589. struct ieee80211_mmie *mmie;
  590. u8 aad[20], mic[8], ipn[6];
  591. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  592. if (!ieee80211_is_mgmt(hdr->frame_control))
  593. return RX_CONTINUE;
  594. /* management frames are already linear */
  595. if (skb->len < 24 + sizeof(*mmie))
  596. return RX_DROP_UNUSABLE;
  597. mmie = (struct ieee80211_mmie *)
  598. (skb->data + skb->len - sizeof(*mmie));
  599. if (mmie->element_id != WLAN_EID_MMIE ||
  600. mmie->length != sizeof(*mmie) - 2)
  601. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  602. bip_ipn_swap(ipn, mmie->sequence_number);
  603. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  604. key->u.aes_cmac.replays++;
  605. return RX_DROP_UNUSABLE;
  606. }
  607. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  608. /* hardware didn't decrypt/verify MIC */
  609. bip_aad(skb, aad);
  610. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  611. skb->data + 24, skb->len - 24, mic);
  612. if (memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) {
  613. key->u.aes_cmac.icverrors++;
  614. return RX_DROP_UNUSABLE;
  615. }
  616. }
  617. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  618. /* Remove MMIE */
  619. skb_trim(skb, skb->len - sizeof(*mmie));
  620. return RX_CONTINUE;
  621. }
  622. ieee80211_tx_result
  623. ieee80211_crypto_hw_encrypt(struct ieee80211_tx_data *tx)
  624. {
  625. struct sk_buff *skb;
  626. struct ieee80211_tx_info *info = NULL;
  627. ieee80211_tx_result res;
  628. skb_queue_walk(&tx->skbs, skb) {
  629. info = IEEE80211_SKB_CB(skb);
  630. /* handle hw-only algorithm */
  631. if (!info->control.hw_key)
  632. return TX_DROP;
  633. if (tx->key->sta->cipher_scheme) {
  634. res = ieee80211_crypto_cs_encrypt(tx, skb);
  635. if (res != TX_CONTINUE)
  636. return res;
  637. }
  638. }
  639. ieee80211_tx_set_protected(tx);
  640. return TX_CONTINUE;
  641. }
  642. ieee80211_rx_result
  643. ieee80211_crypto_hw_decrypt(struct ieee80211_rx_data *rx)
  644. {
  645. if (rx->sta && rx->sta->cipher_scheme)
  646. return ieee80211_crypto_cs_decrypt(rx);
  647. return RX_DROP_UNUSABLE;
  648. }