wpa.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. /*
  2. * Copyright 2002-2004, Instant802 Networks, Inc.
  3. * Copyright 2008, Jouni Malinen <j@w1.fi>
  4. * Copyright (C) 2016 Intel Deutschland GmbH
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/netdevice.h>
  11. #include <linux/types.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/compiler.h>
  14. #include <linux/ieee80211.h>
  15. #include <linux/gfp.h>
  16. #include <asm/unaligned.h>
  17. #include <net/mac80211.h>
  18. #include <crypto/aes.h>
  19. #include <crypto/algapi.h>
  20. #include "ieee80211_i.h"
  21. #include "michael.h"
  22. #include "tkip.h"
  23. #include "aes_ccm.h"
  24. #include "aes_cmac.h"
  25. #include "aes_gmac.h"
  26. #include "aes_gcm.h"
  27. #include "wpa.h"
  28. ieee80211_tx_result
  29. ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
  30. {
  31. u8 *data, *key, *mic;
  32. size_t data_len;
  33. unsigned int hdrlen;
  34. struct ieee80211_hdr *hdr;
  35. struct sk_buff *skb = tx->skb;
  36. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  37. int tail;
  38. hdr = (struct ieee80211_hdr *)skb->data;
  39. if (!tx->key || tx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  40. skb->len < 24 || !ieee80211_is_data_present(hdr->frame_control))
  41. return TX_CONTINUE;
  42. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  43. if (skb->len < hdrlen)
  44. return TX_DROP;
  45. data = skb->data + hdrlen;
  46. data_len = skb->len - hdrlen;
  47. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE)) {
  48. /* Need to use software crypto for the test */
  49. info->control.hw_key = NULL;
  50. }
  51. if (info->control.hw_key &&
  52. (info->flags & IEEE80211_TX_CTL_DONTFRAG ||
  53. tx->local->ops->set_frag_threshold) &&
  54. !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  55. /* hwaccel - with no need for SW-generated MMIC */
  56. return TX_CONTINUE;
  57. }
  58. tail = MICHAEL_MIC_LEN;
  59. if (!info->control.hw_key)
  60. tail += IEEE80211_TKIP_ICV_LEN;
  61. if (WARN(skb_tailroom(skb) < tail ||
  62. skb_headroom(skb) < IEEE80211_TKIP_IV_LEN,
  63. "mmic: not enough head/tail (%d/%d,%d/%d)\n",
  64. skb_headroom(skb), IEEE80211_TKIP_IV_LEN,
  65. skb_tailroom(skb), tail))
  66. return TX_DROP;
  67. key = &tx->key->conf.key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY];
  68. mic = skb_put(skb, MICHAEL_MIC_LEN);
  69. michael_mic(key, hdr, data, data_len, mic);
  70. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE))
  71. mic[0]++;
  72. return TX_CONTINUE;
  73. }
  74. ieee80211_rx_result
  75. ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
  76. {
  77. u8 *data, *key = NULL;
  78. size_t data_len;
  79. unsigned int hdrlen;
  80. u8 mic[MICHAEL_MIC_LEN];
  81. struct sk_buff *skb = rx->skb;
  82. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  83. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  84. /*
  85. * it makes no sense to check for MIC errors on anything other
  86. * than data frames.
  87. */
  88. if (!ieee80211_is_data_present(hdr->frame_control))
  89. return RX_CONTINUE;
  90. /*
  91. * No way to verify the MIC if the hardware stripped it or
  92. * the IV with the key index. In this case we have solely rely
  93. * on the driver to set RX_FLAG_MMIC_ERROR in the event of a
  94. * MIC failure report.
  95. */
  96. if (status->flag & (RX_FLAG_MMIC_STRIPPED | RX_FLAG_IV_STRIPPED)) {
  97. if (status->flag & RX_FLAG_MMIC_ERROR)
  98. goto mic_fail_no_key;
  99. if (!(status->flag & RX_FLAG_IV_STRIPPED) && rx->key &&
  100. rx->key->conf.cipher == WLAN_CIPHER_SUITE_TKIP)
  101. goto update_iv;
  102. return RX_CONTINUE;
  103. }
  104. /*
  105. * Some hardware seems to generate Michael MIC failure reports; even
  106. * though, the frame was not encrypted with TKIP and therefore has no
  107. * MIC. Ignore the flag them to avoid triggering countermeasures.
  108. */
  109. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  110. !(status->flag & RX_FLAG_DECRYPTED))
  111. return RX_CONTINUE;
  112. if (rx->sdata->vif.type == NL80211_IFTYPE_AP && rx->key->conf.keyidx) {
  113. /*
  114. * APs with pairwise keys should never receive Michael MIC
  115. * errors for non-zero keyidx because these are reserved for
  116. * group keys and only the AP is sending real multicast
  117. * frames in the BSS.
  118. */
  119. return RX_DROP_UNUSABLE;
  120. }
  121. if (status->flag & RX_FLAG_MMIC_ERROR)
  122. goto mic_fail;
  123. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  124. if (skb->len < hdrlen + MICHAEL_MIC_LEN)
  125. return RX_DROP_UNUSABLE;
  126. if (skb_linearize(rx->skb))
  127. return RX_DROP_UNUSABLE;
  128. hdr = (void *)skb->data;
  129. data = skb->data + hdrlen;
  130. data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
  131. key = &rx->key->conf.key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
  132. michael_mic(key, hdr, data, data_len, mic);
  133. if (crypto_memneq(mic, data + data_len, MICHAEL_MIC_LEN))
  134. goto mic_fail;
  135. /* remove Michael MIC from payload */
  136. skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
  137. update_iv:
  138. /* update IV in key information to be able to detect replays */
  139. rx->key->u.tkip.rx[rx->security_idx].iv32 = rx->tkip_iv32;
  140. rx->key->u.tkip.rx[rx->security_idx].iv16 = rx->tkip_iv16;
  141. return RX_CONTINUE;
  142. mic_fail:
  143. rx->key->u.tkip.mic_failures++;
  144. mic_fail_no_key:
  145. /*
  146. * In some cases the key can be unset - e.g. a multicast packet, in
  147. * a driver that supports HW encryption. Send up the key idx only if
  148. * the key is set.
  149. */
  150. cfg80211_michael_mic_failure(rx->sdata->dev, hdr->addr2,
  151. is_multicast_ether_addr(hdr->addr1) ?
  152. NL80211_KEYTYPE_GROUP :
  153. NL80211_KEYTYPE_PAIRWISE,
  154. rx->key ? rx->key->conf.keyidx : -1,
  155. NULL, GFP_ATOMIC);
  156. return RX_DROP_UNUSABLE;
  157. }
  158. static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  159. {
  160. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  161. struct ieee80211_key *key = tx->key;
  162. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  163. unsigned int hdrlen;
  164. int len, tail;
  165. u64 pn;
  166. u8 *pos;
  167. if (info->control.hw_key &&
  168. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  169. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  170. /* hwaccel - with no need for software-generated IV */
  171. return 0;
  172. }
  173. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  174. len = skb->len - hdrlen;
  175. if (info->control.hw_key)
  176. tail = 0;
  177. else
  178. tail = IEEE80211_TKIP_ICV_LEN;
  179. if (WARN_ON(skb_tailroom(skb) < tail ||
  180. skb_headroom(skb) < IEEE80211_TKIP_IV_LEN))
  181. return -1;
  182. pos = skb_push(skb, IEEE80211_TKIP_IV_LEN);
  183. memmove(pos, pos + IEEE80211_TKIP_IV_LEN, hdrlen);
  184. pos += hdrlen;
  185. /* the HW only needs room for the IV, but not the actual IV */
  186. if (info->control.hw_key &&
  187. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  188. return 0;
  189. /* Increase IV for the frame */
  190. pn = atomic64_inc_return(&key->conf.tx_pn);
  191. pos = ieee80211_tkip_add_iv(pos, &key->conf, pn);
  192. /* hwaccel - with software IV */
  193. if (info->control.hw_key)
  194. return 0;
  195. /* Add room for ICV */
  196. skb_put(skb, IEEE80211_TKIP_ICV_LEN);
  197. return ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
  198. key, skb, pos, len);
  199. }
  200. ieee80211_tx_result
  201. ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
  202. {
  203. struct sk_buff *skb;
  204. ieee80211_tx_set_protected(tx);
  205. skb_queue_walk(&tx->skbs, skb) {
  206. if (tkip_encrypt_skb(tx, skb) < 0)
  207. return TX_DROP;
  208. }
  209. return TX_CONTINUE;
  210. }
  211. ieee80211_rx_result
  212. ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
  213. {
  214. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  215. int hdrlen, res, hwaccel = 0;
  216. struct ieee80211_key *key = rx->key;
  217. struct sk_buff *skb = rx->skb;
  218. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  219. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  220. if (!ieee80211_is_data(hdr->frame_control))
  221. return RX_CONTINUE;
  222. if (!rx->sta || skb->len - hdrlen < 12)
  223. return RX_DROP_UNUSABLE;
  224. /* it may be possible to optimize this a bit more */
  225. if (skb_linearize(rx->skb))
  226. return RX_DROP_UNUSABLE;
  227. hdr = (void *)skb->data;
  228. /*
  229. * Let TKIP code verify IV, but skip decryption.
  230. * In the case where hardware checks the IV as well,
  231. * we don't even get here, see ieee80211_rx_h_decrypt()
  232. */
  233. if (status->flag & RX_FLAG_DECRYPTED)
  234. hwaccel = 1;
  235. res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
  236. key, skb->data + hdrlen,
  237. skb->len - hdrlen, rx->sta->sta.addr,
  238. hdr->addr1, hwaccel, rx->security_idx,
  239. &rx->tkip_iv32,
  240. &rx->tkip_iv16);
  241. if (res != TKIP_DECRYPT_OK)
  242. return RX_DROP_UNUSABLE;
  243. /* Trim ICV */
  244. skb_trim(skb, skb->len - IEEE80211_TKIP_ICV_LEN);
  245. /* Remove IV */
  246. memmove(skb->data + IEEE80211_TKIP_IV_LEN, skb->data, hdrlen);
  247. skb_pull(skb, IEEE80211_TKIP_IV_LEN);
  248. return RX_CONTINUE;
  249. }
  250. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad)
  251. {
  252. __le16 mask_fc;
  253. int a4_included, mgmt;
  254. u8 qos_tid;
  255. u16 len_a;
  256. unsigned int hdrlen;
  257. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  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. /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
  277. * mode authentication are not allowed to collide, yet both are derived
  278. * from this vector b_0. We only set L := 1 here to indicate that the
  279. * data size can be represented in (L+1) bytes. The CCM layer will take
  280. * care of storing the data length in the top (L+1) bytes and setting
  281. * and clearing the other bits as is required to derive the two IVs.
  282. */
  283. b_0[0] = 0x1;
  284. /* Nonce: Nonce Flags | A2 | PN
  285. * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
  286. */
  287. b_0[1] = qos_tid | (mgmt << 4);
  288. memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
  289. memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
  290. /* AAD (extra authenticate-only data) / masked 802.11 header
  291. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  292. put_unaligned_be16(len_a, &aad[0]);
  293. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  294. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  295. /* Mask Seq#, leave Frag# */
  296. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  297. aad[23] = 0;
  298. if (a4_included) {
  299. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  300. aad[30] = qos_tid;
  301. aad[31] = 0;
  302. } else {
  303. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  304. aad[24] = qos_tid;
  305. }
  306. }
  307. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  308. {
  309. hdr[0] = pn[5];
  310. hdr[1] = pn[4];
  311. hdr[2] = 0;
  312. hdr[3] = 0x20 | (key_id << 6);
  313. hdr[4] = pn[3];
  314. hdr[5] = pn[2];
  315. hdr[6] = pn[1];
  316. hdr[7] = pn[0];
  317. }
  318. static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
  319. {
  320. pn[0] = hdr[7];
  321. pn[1] = hdr[6];
  322. pn[2] = hdr[5];
  323. pn[3] = hdr[4];
  324. pn[4] = hdr[1];
  325. pn[5] = hdr[0];
  326. }
  327. static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb,
  328. unsigned int mic_len)
  329. {
  330. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  331. struct ieee80211_key *key = tx->key;
  332. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  333. int hdrlen, len, tail;
  334. u8 *pos;
  335. u8 pn[6];
  336. u64 pn64;
  337. u8 aad[CCM_AAD_LEN];
  338. u8 b_0[AES_BLOCK_SIZE];
  339. if (info->control.hw_key &&
  340. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  341. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  342. !((info->control.hw_key->flags &
  343. IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) &&
  344. ieee80211_is_mgmt(hdr->frame_control))) {
  345. /*
  346. * hwaccel has no need for preallocated room for CCMP
  347. * header or MIC fields
  348. */
  349. return 0;
  350. }
  351. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  352. len = skb->len - hdrlen;
  353. if (info->control.hw_key)
  354. tail = 0;
  355. else
  356. tail = mic_len;
  357. if (WARN_ON(skb_tailroom(skb) < tail ||
  358. skb_headroom(skb) < IEEE80211_CCMP_HDR_LEN))
  359. return -1;
  360. pos = skb_push(skb, IEEE80211_CCMP_HDR_LEN);
  361. memmove(pos, pos + IEEE80211_CCMP_HDR_LEN, hdrlen);
  362. /* the HW only needs room for the IV, but not the actual IV */
  363. if (info->control.hw_key &&
  364. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  365. return 0;
  366. hdr = (struct ieee80211_hdr *) pos;
  367. pos += hdrlen;
  368. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  369. pn[5] = pn64;
  370. pn[4] = pn64 >> 8;
  371. pn[3] = pn64 >> 16;
  372. pn[2] = pn64 >> 24;
  373. pn[1] = pn64 >> 32;
  374. pn[0] = pn64 >> 40;
  375. ccmp_pn2hdr(pos, pn, key->conf.keyidx);
  376. /* hwaccel - with software CCMP header */
  377. if (info->control.hw_key)
  378. return 0;
  379. pos += IEEE80211_CCMP_HDR_LEN;
  380. ccmp_special_blocks(skb, pn, b_0, aad);
  381. return ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
  382. skb_put(skb, mic_len), mic_len);
  383. }
  384. ieee80211_tx_result
  385. ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx,
  386. unsigned int mic_len)
  387. {
  388. struct sk_buff *skb;
  389. ieee80211_tx_set_protected(tx);
  390. skb_queue_walk(&tx->skbs, skb) {
  391. if (ccmp_encrypt_skb(tx, skb, mic_len) < 0)
  392. return TX_DROP;
  393. }
  394. return TX_CONTINUE;
  395. }
  396. ieee80211_rx_result
  397. ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx,
  398. unsigned int mic_len)
  399. {
  400. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  401. int hdrlen;
  402. struct ieee80211_key *key = rx->key;
  403. struct sk_buff *skb = rx->skb;
  404. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  405. u8 pn[IEEE80211_CCMP_PN_LEN];
  406. int data_len;
  407. int queue;
  408. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  409. if (!ieee80211_is_data(hdr->frame_control) &&
  410. !ieee80211_is_robust_mgmt_frame(skb))
  411. return RX_CONTINUE;
  412. if (status->flag & RX_FLAG_DECRYPTED) {
  413. if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_CCMP_HDR_LEN))
  414. return RX_DROP_UNUSABLE;
  415. if (status->flag & RX_FLAG_MIC_STRIPPED)
  416. mic_len = 0;
  417. } else {
  418. if (skb_linearize(rx->skb))
  419. return RX_DROP_UNUSABLE;
  420. }
  421. data_len = skb->len - hdrlen - IEEE80211_CCMP_HDR_LEN - mic_len;
  422. if (!rx->sta || data_len < 0)
  423. return RX_DROP_UNUSABLE;
  424. if (!(status->flag & RX_FLAG_PN_VALIDATED)) {
  425. int res;
  426. ccmp_hdr2pn(pn, skb->data + hdrlen);
  427. queue = rx->security_idx;
  428. res = memcmp(pn, key->u.ccmp.rx_pn[queue],
  429. IEEE80211_CCMP_PN_LEN);
  430. if (res < 0 ||
  431. (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) {
  432. key->u.ccmp.replays++;
  433. return RX_DROP_UNUSABLE;
  434. }
  435. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  436. u8 aad[2 * AES_BLOCK_SIZE];
  437. u8 b_0[AES_BLOCK_SIZE];
  438. /* hardware didn't decrypt/verify MIC */
  439. ccmp_special_blocks(skb, pn, b_0, aad);
  440. if (ieee80211_aes_ccm_decrypt(
  441. key->u.ccmp.tfm, b_0, aad,
  442. skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN,
  443. data_len,
  444. skb->data + skb->len - mic_len, mic_len))
  445. return RX_DROP_UNUSABLE;
  446. }
  447. memcpy(key->u.ccmp.rx_pn[queue], pn, IEEE80211_CCMP_PN_LEN);
  448. }
  449. /* Remove CCMP header and MIC */
  450. if (pskb_trim(skb, skb->len - mic_len))
  451. return RX_DROP_UNUSABLE;
  452. memmove(skb->data + IEEE80211_CCMP_HDR_LEN, skb->data, hdrlen);
  453. skb_pull(skb, IEEE80211_CCMP_HDR_LEN);
  454. return RX_CONTINUE;
  455. }
  456. static void gcmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *j_0, u8 *aad)
  457. {
  458. __le16 mask_fc;
  459. u8 qos_tid;
  460. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  461. memcpy(j_0, hdr->addr2, ETH_ALEN);
  462. memcpy(&j_0[ETH_ALEN], pn, IEEE80211_GCMP_PN_LEN);
  463. j_0[13] = 0;
  464. j_0[14] = 0;
  465. j_0[AES_BLOCK_SIZE - 1] = 0x01;
  466. /* AAD (extra authenticate-only data) / masked 802.11 header
  467. * FC | A1 | A2 | A3 | SC | [A4] | [QC]
  468. */
  469. put_unaligned_be16(ieee80211_hdrlen(hdr->frame_control) - 2, &aad[0]);
  470. /* Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  471. * Retry, PwrMgt, MoreData; set Protected
  472. */
  473. mask_fc = hdr->frame_control;
  474. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  475. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  476. if (!ieee80211_is_mgmt(hdr->frame_control))
  477. mask_fc &= ~cpu_to_le16(0x0070);
  478. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  479. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  480. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  481. /* Mask Seq#, leave Frag# */
  482. aad[22] = *((u8 *)&hdr->seq_ctrl) & 0x0f;
  483. aad[23] = 0;
  484. if (ieee80211_is_data_qos(hdr->frame_control))
  485. qos_tid = *ieee80211_get_qos_ctl(hdr) &
  486. IEEE80211_QOS_CTL_TID_MASK;
  487. else
  488. qos_tid = 0;
  489. if (ieee80211_has_a4(hdr->frame_control)) {
  490. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  491. aad[30] = qos_tid;
  492. aad[31] = 0;
  493. } else {
  494. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  495. aad[24] = qos_tid;
  496. }
  497. }
  498. static inline void gcmp_pn2hdr(u8 *hdr, const u8 *pn, int key_id)
  499. {
  500. hdr[0] = pn[5];
  501. hdr[1] = pn[4];
  502. hdr[2] = 0;
  503. hdr[3] = 0x20 | (key_id << 6);
  504. hdr[4] = pn[3];
  505. hdr[5] = pn[2];
  506. hdr[6] = pn[1];
  507. hdr[7] = pn[0];
  508. }
  509. static inline void gcmp_hdr2pn(u8 *pn, const u8 *hdr)
  510. {
  511. pn[0] = hdr[7];
  512. pn[1] = hdr[6];
  513. pn[2] = hdr[5];
  514. pn[3] = hdr[4];
  515. pn[4] = hdr[1];
  516. pn[5] = hdr[0];
  517. }
  518. static int gcmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  519. {
  520. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  521. struct ieee80211_key *key = tx->key;
  522. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  523. int hdrlen, len, tail;
  524. u8 *pos;
  525. u8 pn[6];
  526. u64 pn64;
  527. u8 aad[GCM_AAD_LEN];
  528. u8 j_0[AES_BLOCK_SIZE];
  529. if (info->control.hw_key &&
  530. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  531. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  532. !((info->control.hw_key->flags &
  533. IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) &&
  534. ieee80211_is_mgmt(hdr->frame_control))) {
  535. /* hwaccel has no need for preallocated room for GCMP
  536. * header or MIC fields
  537. */
  538. return 0;
  539. }
  540. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  541. len = skb->len - hdrlen;
  542. if (info->control.hw_key)
  543. tail = 0;
  544. else
  545. tail = IEEE80211_GCMP_MIC_LEN;
  546. if (WARN_ON(skb_tailroom(skb) < tail ||
  547. skb_headroom(skb) < IEEE80211_GCMP_HDR_LEN))
  548. return -1;
  549. pos = skb_push(skb, IEEE80211_GCMP_HDR_LEN);
  550. memmove(pos, pos + IEEE80211_GCMP_HDR_LEN, hdrlen);
  551. skb_set_network_header(skb, skb_network_offset(skb) +
  552. IEEE80211_GCMP_HDR_LEN);
  553. /* the HW only needs room for the IV, but not the actual IV */
  554. if (info->control.hw_key &&
  555. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  556. return 0;
  557. hdr = (struct ieee80211_hdr *)pos;
  558. pos += hdrlen;
  559. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  560. pn[5] = pn64;
  561. pn[4] = pn64 >> 8;
  562. pn[3] = pn64 >> 16;
  563. pn[2] = pn64 >> 24;
  564. pn[1] = pn64 >> 32;
  565. pn[0] = pn64 >> 40;
  566. gcmp_pn2hdr(pos, pn, key->conf.keyidx);
  567. /* hwaccel - with software GCMP header */
  568. if (info->control.hw_key)
  569. return 0;
  570. pos += IEEE80211_GCMP_HDR_LEN;
  571. gcmp_special_blocks(skb, pn, j_0, aad);
  572. return ieee80211_aes_gcm_encrypt(key->u.gcmp.tfm, j_0, aad, pos, len,
  573. skb_put(skb, IEEE80211_GCMP_MIC_LEN));
  574. }
  575. ieee80211_tx_result
  576. ieee80211_crypto_gcmp_encrypt(struct ieee80211_tx_data *tx)
  577. {
  578. struct sk_buff *skb;
  579. ieee80211_tx_set_protected(tx);
  580. skb_queue_walk(&tx->skbs, skb) {
  581. if (gcmp_encrypt_skb(tx, skb) < 0)
  582. return TX_DROP;
  583. }
  584. return TX_CONTINUE;
  585. }
  586. ieee80211_rx_result
  587. ieee80211_crypto_gcmp_decrypt(struct ieee80211_rx_data *rx)
  588. {
  589. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  590. int hdrlen;
  591. struct ieee80211_key *key = rx->key;
  592. struct sk_buff *skb = rx->skb;
  593. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  594. u8 pn[IEEE80211_GCMP_PN_LEN];
  595. int data_len, queue, mic_len = IEEE80211_GCMP_MIC_LEN;
  596. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  597. if (!ieee80211_is_data(hdr->frame_control) &&
  598. !ieee80211_is_robust_mgmt_frame(skb))
  599. return RX_CONTINUE;
  600. if (status->flag & RX_FLAG_DECRYPTED) {
  601. if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_GCMP_HDR_LEN))
  602. return RX_DROP_UNUSABLE;
  603. if (status->flag & RX_FLAG_MIC_STRIPPED)
  604. mic_len = 0;
  605. } else {
  606. if (skb_linearize(rx->skb))
  607. return RX_DROP_UNUSABLE;
  608. }
  609. data_len = skb->len - hdrlen - IEEE80211_GCMP_HDR_LEN - mic_len;
  610. if (!rx->sta || data_len < 0)
  611. return RX_DROP_UNUSABLE;
  612. if (!(status->flag & RX_FLAG_PN_VALIDATED)) {
  613. int res;
  614. gcmp_hdr2pn(pn, skb->data + hdrlen);
  615. queue = rx->security_idx;
  616. res = memcmp(pn, key->u.gcmp.rx_pn[queue],
  617. IEEE80211_GCMP_PN_LEN);
  618. if (res < 0 ||
  619. (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) {
  620. key->u.gcmp.replays++;
  621. return RX_DROP_UNUSABLE;
  622. }
  623. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  624. u8 aad[2 * AES_BLOCK_SIZE];
  625. u8 j_0[AES_BLOCK_SIZE];
  626. /* hardware didn't decrypt/verify MIC */
  627. gcmp_special_blocks(skb, pn, j_0, aad);
  628. if (ieee80211_aes_gcm_decrypt(
  629. key->u.gcmp.tfm, j_0, aad,
  630. skb->data + hdrlen + IEEE80211_GCMP_HDR_LEN,
  631. data_len,
  632. skb->data + skb->len -
  633. IEEE80211_GCMP_MIC_LEN))
  634. return RX_DROP_UNUSABLE;
  635. }
  636. memcpy(key->u.gcmp.rx_pn[queue], pn, IEEE80211_GCMP_PN_LEN);
  637. }
  638. /* Remove GCMP header and MIC */
  639. if (pskb_trim(skb, skb->len - mic_len))
  640. return RX_DROP_UNUSABLE;
  641. memmove(skb->data + IEEE80211_GCMP_HDR_LEN, skb->data, hdrlen);
  642. skb_pull(skb, IEEE80211_GCMP_HDR_LEN);
  643. return RX_CONTINUE;
  644. }
  645. static ieee80211_tx_result
  646. ieee80211_crypto_cs_encrypt(struct ieee80211_tx_data *tx,
  647. struct sk_buff *skb)
  648. {
  649. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  650. struct ieee80211_key *key = tx->key;
  651. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  652. int hdrlen;
  653. u8 *pos, iv_len = key->conf.iv_len;
  654. if (info->control.hw_key &&
  655. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  656. /* hwaccel has no need for preallocated head room */
  657. return TX_CONTINUE;
  658. }
  659. if (unlikely(skb_headroom(skb) < iv_len &&
  660. pskb_expand_head(skb, iv_len, 0, GFP_ATOMIC)))
  661. return TX_DROP;
  662. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  663. pos = skb_push(skb, iv_len);
  664. memmove(pos, pos + iv_len, hdrlen);
  665. return TX_CONTINUE;
  666. }
  667. static inline int ieee80211_crypto_cs_pn_compare(u8 *pn1, u8 *pn2, int len)
  668. {
  669. int i;
  670. /* pn is little endian */
  671. for (i = len - 1; i >= 0; i--) {
  672. if (pn1[i] < pn2[i])
  673. return -1;
  674. else if (pn1[i] > pn2[i])
  675. return 1;
  676. }
  677. return 0;
  678. }
  679. static ieee80211_rx_result
  680. ieee80211_crypto_cs_decrypt(struct ieee80211_rx_data *rx)
  681. {
  682. struct ieee80211_key *key = rx->key;
  683. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  684. const struct ieee80211_cipher_scheme *cs = NULL;
  685. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  686. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  687. int data_len;
  688. u8 *rx_pn;
  689. u8 *skb_pn;
  690. u8 qos_tid;
  691. if (!rx->sta || !rx->sta->cipher_scheme ||
  692. !(status->flag & RX_FLAG_DECRYPTED))
  693. return RX_DROP_UNUSABLE;
  694. if (!ieee80211_is_data(hdr->frame_control))
  695. return RX_CONTINUE;
  696. cs = rx->sta->cipher_scheme;
  697. data_len = rx->skb->len - hdrlen - cs->hdr_len;
  698. if (data_len < 0)
  699. return RX_DROP_UNUSABLE;
  700. if (ieee80211_is_data_qos(hdr->frame_control))
  701. qos_tid = *ieee80211_get_qos_ctl(hdr) &
  702. IEEE80211_QOS_CTL_TID_MASK;
  703. else
  704. qos_tid = 0;
  705. if (skb_linearize(rx->skb))
  706. return RX_DROP_UNUSABLE;
  707. hdr = (struct ieee80211_hdr *)rx->skb->data;
  708. rx_pn = key->u.gen.rx_pn[qos_tid];
  709. skb_pn = rx->skb->data + hdrlen + cs->pn_off;
  710. if (ieee80211_crypto_cs_pn_compare(skb_pn, rx_pn, cs->pn_len) <= 0)
  711. return RX_DROP_UNUSABLE;
  712. memcpy(rx_pn, skb_pn, cs->pn_len);
  713. /* remove security header and MIC */
  714. if (pskb_trim(rx->skb, rx->skb->len - cs->mic_len))
  715. return RX_DROP_UNUSABLE;
  716. memmove(rx->skb->data + cs->hdr_len, rx->skb->data, hdrlen);
  717. skb_pull(rx->skb, cs->hdr_len);
  718. return RX_CONTINUE;
  719. }
  720. static void bip_aad(struct sk_buff *skb, u8 *aad)
  721. {
  722. __le16 mask_fc;
  723. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  724. /* BIP AAD: FC(masked) || A1 || A2 || A3 */
  725. /* FC type/subtype */
  726. /* Mask FC Retry, PwrMgt, MoreData flags to zero */
  727. mask_fc = hdr->frame_control;
  728. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY | IEEE80211_FCTL_PM |
  729. IEEE80211_FCTL_MOREDATA);
  730. put_unaligned(mask_fc, (__le16 *) &aad[0]);
  731. /* A1 || A2 || A3 */
  732. memcpy(aad + 2, &hdr->addr1, 3 * ETH_ALEN);
  733. }
  734. static inline void bip_ipn_set64(u8 *d, u64 pn)
  735. {
  736. *d++ = pn;
  737. *d++ = pn >> 8;
  738. *d++ = pn >> 16;
  739. *d++ = pn >> 24;
  740. *d++ = pn >> 32;
  741. *d = pn >> 40;
  742. }
  743. static inline void bip_ipn_swap(u8 *d, const u8 *s)
  744. {
  745. *d++ = s[5];
  746. *d++ = s[4];
  747. *d++ = s[3];
  748. *d++ = s[2];
  749. *d++ = s[1];
  750. *d = s[0];
  751. }
  752. ieee80211_tx_result
  753. ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
  754. {
  755. struct sk_buff *skb;
  756. struct ieee80211_tx_info *info;
  757. struct ieee80211_key *key = tx->key;
  758. struct ieee80211_mmie *mmie;
  759. u8 aad[20];
  760. u64 pn64;
  761. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  762. return TX_DROP;
  763. skb = skb_peek(&tx->skbs);
  764. info = IEEE80211_SKB_CB(skb);
  765. if (info->control.hw_key)
  766. return TX_CONTINUE;
  767. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  768. return TX_DROP;
  769. mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie));
  770. mmie->element_id = WLAN_EID_MMIE;
  771. mmie->length = sizeof(*mmie) - 2;
  772. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  773. /* PN = PN + 1 */
  774. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  775. bip_ipn_set64(mmie->sequence_number, pn64);
  776. bip_aad(skb, aad);
  777. /*
  778. * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
  779. */
  780. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  781. skb->data + 24, skb->len - 24, mmie->mic);
  782. return TX_CONTINUE;
  783. }
  784. ieee80211_tx_result
  785. ieee80211_crypto_aes_cmac_256_encrypt(struct ieee80211_tx_data *tx)
  786. {
  787. struct sk_buff *skb;
  788. struct ieee80211_tx_info *info;
  789. struct ieee80211_key *key = tx->key;
  790. struct ieee80211_mmie_16 *mmie;
  791. u8 aad[20];
  792. u64 pn64;
  793. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  794. return TX_DROP;
  795. skb = skb_peek(&tx->skbs);
  796. info = IEEE80211_SKB_CB(skb);
  797. if (info->control.hw_key)
  798. return TX_CONTINUE;
  799. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  800. return TX_DROP;
  801. mmie = (struct ieee80211_mmie_16 *)skb_put(skb, sizeof(*mmie));
  802. mmie->element_id = WLAN_EID_MMIE;
  803. mmie->length = sizeof(*mmie) - 2;
  804. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  805. /* PN = PN + 1 */
  806. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  807. bip_ipn_set64(mmie->sequence_number, pn64);
  808. bip_aad(skb, aad);
  809. /* MIC = AES-256-CMAC(IGTK, AAD || Management Frame Body || MMIE, 128)
  810. */
  811. ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
  812. skb->data + 24, skb->len - 24, mmie->mic);
  813. return TX_CONTINUE;
  814. }
  815. ieee80211_rx_result
  816. ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
  817. {
  818. struct sk_buff *skb = rx->skb;
  819. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  820. struct ieee80211_key *key = rx->key;
  821. struct ieee80211_mmie *mmie;
  822. u8 aad[20], mic[8], ipn[6];
  823. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  824. if (!ieee80211_is_mgmt(hdr->frame_control))
  825. return RX_CONTINUE;
  826. /* management frames are already linear */
  827. if (skb->len < 24 + sizeof(*mmie))
  828. return RX_DROP_UNUSABLE;
  829. mmie = (struct ieee80211_mmie *)
  830. (skb->data + skb->len - sizeof(*mmie));
  831. if (mmie->element_id != WLAN_EID_MMIE ||
  832. mmie->length != sizeof(*mmie) - 2)
  833. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  834. bip_ipn_swap(ipn, mmie->sequence_number);
  835. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  836. key->u.aes_cmac.replays++;
  837. return RX_DROP_UNUSABLE;
  838. }
  839. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  840. /* hardware didn't decrypt/verify MIC */
  841. bip_aad(skb, aad);
  842. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  843. skb->data + 24, skb->len - 24, mic);
  844. if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  845. key->u.aes_cmac.icverrors++;
  846. return RX_DROP_UNUSABLE;
  847. }
  848. }
  849. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  850. /* Remove MMIE */
  851. skb_trim(skb, skb->len - sizeof(*mmie));
  852. return RX_CONTINUE;
  853. }
  854. ieee80211_rx_result
  855. ieee80211_crypto_aes_cmac_256_decrypt(struct ieee80211_rx_data *rx)
  856. {
  857. struct sk_buff *skb = rx->skb;
  858. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  859. struct ieee80211_key *key = rx->key;
  860. struct ieee80211_mmie_16 *mmie;
  861. u8 aad[20], mic[16], ipn[6];
  862. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  863. if (!ieee80211_is_mgmt(hdr->frame_control))
  864. return RX_CONTINUE;
  865. /* management frames are already linear */
  866. if (skb->len < 24 + sizeof(*mmie))
  867. return RX_DROP_UNUSABLE;
  868. mmie = (struct ieee80211_mmie_16 *)
  869. (skb->data + skb->len - sizeof(*mmie));
  870. if (mmie->element_id != WLAN_EID_MMIE ||
  871. mmie->length != sizeof(*mmie) - 2)
  872. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  873. bip_ipn_swap(ipn, mmie->sequence_number);
  874. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  875. key->u.aes_cmac.replays++;
  876. return RX_DROP_UNUSABLE;
  877. }
  878. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  879. /* hardware didn't decrypt/verify MIC */
  880. bip_aad(skb, aad);
  881. ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
  882. skb->data + 24, skb->len - 24, mic);
  883. if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  884. key->u.aes_cmac.icverrors++;
  885. return RX_DROP_UNUSABLE;
  886. }
  887. }
  888. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  889. /* Remove MMIE */
  890. skb_trim(skb, skb->len - sizeof(*mmie));
  891. return RX_CONTINUE;
  892. }
  893. ieee80211_tx_result
  894. ieee80211_crypto_aes_gmac_encrypt(struct ieee80211_tx_data *tx)
  895. {
  896. struct sk_buff *skb;
  897. struct ieee80211_tx_info *info;
  898. struct ieee80211_key *key = tx->key;
  899. struct ieee80211_mmie_16 *mmie;
  900. struct ieee80211_hdr *hdr;
  901. u8 aad[GMAC_AAD_LEN];
  902. u64 pn64;
  903. u8 nonce[GMAC_NONCE_LEN];
  904. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  905. return TX_DROP;
  906. skb = skb_peek(&tx->skbs);
  907. info = IEEE80211_SKB_CB(skb);
  908. if (info->control.hw_key)
  909. return TX_CONTINUE;
  910. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  911. return TX_DROP;
  912. mmie = (struct ieee80211_mmie_16 *)skb_put(skb, sizeof(*mmie));
  913. mmie->element_id = WLAN_EID_MMIE;
  914. mmie->length = sizeof(*mmie) - 2;
  915. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  916. /* PN = PN + 1 */
  917. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  918. bip_ipn_set64(mmie->sequence_number, pn64);
  919. bip_aad(skb, aad);
  920. hdr = (struct ieee80211_hdr *)skb->data;
  921. memcpy(nonce, hdr->addr2, ETH_ALEN);
  922. bip_ipn_swap(nonce + ETH_ALEN, mmie->sequence_number);
  923. /* MIC = AES-GMAC(IGTK, AAD || Management Frame Body || MMIE, 128) */
  924. if (ieee80211_aes_gmac(key->u.aes_gmac.tfm, aad, nonce,
  925. skb->data + 24, skb->len - 24, mmie->mic) < 0)
  926. return TX_DROP;
  927. return TX_CONTINUE;
  928. }
  929. ieee80211_rx_result
  930. ieee80211_crypto_aes_gmac_decrypt(struct ieee80211_rx_data *rx)
  931. {
  932. struct sk_buff *skb = rx->skb;
  933. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  934. struct ieee80211_key *key = rx->key;
  935. struct ieee80211_mmie_16 *mmie;
  936. u8 aad[GMAC_AAD_LEN], mic[GMAC_MIC_LEN], ipn[6], nonce[GMAC_NONCE_LEN];
  937. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  938. if (!ieee80211_is_mgmt(hdr->frame_control))
  939. return RX_CONTINUE;
  940. /* management frames are already linear */
  941. if (skb->len < 24 + sizeof(*mmie))
  942. return RX_DROP_UNUSABLE;
  943. mmie = (struct ieee80211_mmie_16 *)
  944. (skb->data + skb->len - sizeof(*mmie));
  945. if (mmie->element_id != WLAN_EID_MMIE ||
  946. mmie->length != sizeof(*mmie) - 2)
  947. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  948. bip_ipn_swap(ipn, mmie->sequence_number);
  949. if (memcmp(ipn, key->u.aes_gmac.rx_pn, 6) <= 0) {
  950. key->u.aes_gmac.replays++;
  951. return RX_DROP_UNUSABLE;
  952. }
  953. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  954. /* hardware didn't decrypt/verify MIC */
  955. bip_aad(skb, aad);
  956. memcpy(nonce, hdr->addr2, ETH_ALEN);
  957. memcpy(nonce + ETH_ALEN, ipn, 6);
  958. if (ieee80211_aes_gmac(key->u.aes_gmac.tfm, aad, nonce,
  959. skb->data + 24, skb->len - 24,
  960. mic) < 0 ||
  961. crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  962. key->u.aes_gmac.icverrors++;
  963. return RX_DROP_UNUSABLE;
  964. }
  965. }
  966. memcpy(key->u.aes_gmac.rx_pn, ipn, 6);
  967. /* Remove MMIE */
  968. skb_trim(skb, skb->len - sizeof(*mmie));
  969. return RX_CONTINUE;
  970. }
  971. ieee80211_tx_result
  972. ieee80211_crypto_hw_encrypt(struct ieee80211_tx_data *tx)
  973. {
  974. struct sk_buff *skb;
  975. struct ieee80211_tx_info *info = NULL;
  976. ieee80211_tx_result res;
  977. skb_queue_walk(&tx->skbs, skb) {
  978. info = IEEE80211_SKB_CB(skb);
  979. /* handle hw-only algorithm */
  980. if (!info->control.hw_key)
  981. return TX_DROP;
  982. if (tx->key->flags & KEY_FLAG_CIPHER_SCHEME) {
  983. res = ieee80211_crypto_cs_encrypt(tx, skb);
  984. if (res != TX_CONTINUE)
  985. return res;
  986. }
  987. }
  988. ieee80211_tx_set_protected(tx);
  989. return TX_CONTINUE;
  990. }
  991. ieee80211_rx_result
  992. ieee80211_crypto_hw_decrypt(struct ieee80211_rx_data *rx)
  993. {
  994. if (rx->sta && rx->sta->cipher_scheme)
  995. return ieee80211_crypto_cs_decrypt(rx);
  996. return RX_DROP_UNUSABLE;
  997. }