wpa.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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. if (!(status->flag & RX_FLAG_ICV_STRIPPED))
  245. skb_trim(skb, skb->len - IEEE80211_TKIP_ICV_LEN);
  246. /* Remove IV */
  247. memmove(skb->data + IEEE80211_TKIP_IV_LEN, skb->data, hdrlen);
  248. skb_pull(skb, IEEE80211_TKIP_IV_LEN);
  249. return RX_CONTINUE;
  250. }
  251. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad)
  252. {
  253. __le16 mask_fc;
  254. int a4_included, mgmt;
  255. u8 qos_tid;
  256. u16 len_a;
  257. unsigned int hdrlen;
  258. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  259. /*
  260. * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  261. * Retry, PwrMgt, MoreData; set Protected
  262. */
  263. mgmt = ieee80211_is_mgmt(hdr->frame_control);
  264. mask_fc = hdr->frame_control;
  265. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  266. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  267. if (!mgmt)
  268. mask_fc &= ~cpu_to_le16(0x0070);
  269. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  270. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  271. len_a = hdrlen - 2;
  272. a4_included = ieee80211_has_a4(hdr->frame_control);
  273. if (ieee80211_is_data_qos(hdr->frame_control))
  274. qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  275. else
  276. qos_tid = 0;
  277. /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
  278. * mode authentication are not allowed to collide, yet both are derived
  279. * from this vector b_0. We only set L := 1 here to indicate that the
  280. * data size can be represented in (L+1) bytes. The CCM layer will take
  281. * care of storing the data length in the top (L+1) bytes and setting
  282. * and clearing the other bits as is required to derive the two IVs.
  283. */
  284. b_0[0] = 0x1;
  285. /* Nonce: Nonce Flags | A2 | PN
  286. * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
  287. */
  288. b_0[1] = qos_tid | (mgmt << 4);
  289. memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
  290. memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
  291. /* AAD (extra authenticate-only data) / masked 802.11 header
  292. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  293. put_unaligned_be16(len_a, &aad[0]);
  294. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  295. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  296. /* Mask Seq#, leave Frag# */
  297. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  298. aad[23] = 0;
  299. if (a4_included) {
  300. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  301. aad[30] = qos_tid;
  302. aad[31] = 0;
  303. } else {
  304. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  305. aad[24] = qos_tid;
  306. }
  307. }
  308. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  309. {
  310. hdr[0] = pn[5];
  311. hdr[1] = pn[4];
  312. hdr[2] = 0;
  313. hdr[3] = 0x20 | (key_id << 6);
  314. hdr[4] = pn[3];
  315. hdr[5] = pn[2];
  316. hdr[6] = pn[1];
  317. hdr[7] = pn[0];
  318. }
  319. static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
  320. {
  321. pn[0] = hdr[7];
  322. pn[1] = hdr[6];
  323. pn[2] = hdr[5];
  324. pn[3] = hdr[4];
  325. pn[4] = hdr[1];
  326. pn[5] = hdr[0];
  327. }
  328. static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb,
  329. unsigned int mic_len)
  330. {
  331. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  332. struct ieee80211_key *key = tx->key;
  333. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  334. int hdrlen, len, tail;
  335. u8 *pos;
  336. u8 pn[6];
  337. u64 pn64;
  338. u8 aad[CCM_AAD_LEN];
  339. u8 b_0[AES_BLOCK_SIZE];
  340. if (info->control.hw_key &&
  341. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  342. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  343. !((info->control.hw_key->flags &
  344. IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) &&
  345. ieee80211_is_mgmt(hdr->frame_control))) {
  346. /*
  347. * hwaccel has no need for preallocated room for CCMP
  348. * header or MIC fields
  349. */
  350. return 0;
  351. }
  352. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  353. len = skb->len - hdrlen;
  354. if (info->control.hw_key)
  355. tail = 0;
  356. else
  357. tail = mic_len;
  358. if (WARN_ON(skb_tailroom(skb) < tail ||
  359. skb_headroom(skb) < IEEE80211_CCMP_HDR_LEN))
  360. return -1;
  361. pos = skb_push(skb, IEEE80211_CCMP_HDR_LEN);
  362. memmove(pos, pos + IEEE80211_CCMP_HDR_LEN, hdrlen);
  363. /* the HW only needs room for the IV, but not the actual IV */
  364. if (info->control.hw_key &&
  365. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  366. return 0;
  367. hdr = (struct ieee80211_hdr *) pos;
  368. pos += hdrlen;
  369. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  370. pn[5] = pn64;
  371. pn[4] = pn64 >> 8;
  372. pn[3] = pn64 >> 16;
  373. pn[2] = pn64 >> 24;
  374. pn[1] = pn64 >> 32;
  375. pn[0] = pn64 >> 40;
  376. ccmp_pn2hdr(pos, pn, key->conf.keyidx);
  377. /* hwaccel - with software CCMP header */
  378. if (info->control.hw_key)
  379. return 0;
  380. pos += IEEE80211_CCMP_HDR_LEN;
  381. ccmp_special_blocks(skb, pn, b_0, aad);
  382. return ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
  383. skb_put(skb, mic_len), mic_len);
  384. }
  385. ieee80211_tx_result
  386. ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx,
  387. unsigned int mic_len)
  388. {
  389. struct sk_buff *skb;
  390. ieee80211_tx_set_protected(tx);
  391. skb_queue_walk(&tx->skbs, skb) {
  392. if (ccmp_encrypt_skb(tx, skb, mic_len) < 0)
  393. return TX_DROP;
  394. }
  395. return TX_CONTINUE;
  396. }
  397. ieee80211_rx_result
  398. ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx,
  399. unsigned int mic_len)
  400. {
  401. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  402. int hdrlen;
  403. struct ieee80211_key *key = rx->key;
  404. struct sk_buff *skb = rx->skb;
  405. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  406. u8 pn[IEEE80211_CCMP_PN_LEN];
  407. int data_len;
  408. int queue;
  409. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  410. if (!ieee80211_is_data(hdr->frame_control) &&
  411. !ieee80211_is_robust_mgmt_frame(skb))
  412. return RX_CONTINUE;
  413. if (status->flag & RX_FLAG_DECRYPTED) {
  414. if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_CCMP_HDR_LEN))
  415. return RX_DROP_UNUSABLE;
  416. if (status->flag & RX_FLAG_MIC_STRIPPED)
  417. mic_len = 0;
  418. } else {
  419. if (skb_linearize(rx->skb))
  420. return RX_DROP_UNUSABLE;
  421. }
  422. data_len = skb->len - hdrlen - IEEE80211_CCMP_HDR_LEN - mic_len;
  423. if (!rx->sta || data_len < 0)
  424. return RX_DROP_UNUSABLE;
  425. if (!(status->flag & RX_FLAG_PN_VALIDATED)) {
  426. int res;
  427. ccmp_hdr2pn(pn, skb->data + hdrlen);
  428. queue = rx->security_idx;
  429. res = memcmp(pn, key->u.ccmp.rx_pn[queue],
  430. IEEE80211_CCMP_PN_LEN);
  431. if (res < 0 ||
  432. (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) {
  433. key->u.ccmp.replays++;
  434. return RX_DROP_UNUSABLE;
  435. }
  436. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  437. u8 aad[2 * AES_BLOCK_SIZE];
  438. u8 b_0[AES_BLOCK_SIZE];
  439. /* hardware didn't decrypt/verify MIC */
  440. ccmp_special_blocks(skb, pn, b_0, aad);
  441. if (ieee80211_aes_ccm_decrypt(
  442. key->u.ccmp.tfm, b_0, aad,
  443. skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN,
  444. data_len,
  445. skb->data + skb->len - mic_len, mic_len))
  446. return RX_DROP_UNUSABLE;
  447. }
  448. memcpy(key->u.ccmp.rx_pn[queue], pn, IEEE80211_CCMP_PN_LEN);
  449. }
  450. /* Remove CCMP header and MIC */
  451. if (pskb_trim(skb, skb->len - mic_len))
  452. return RX_DROP_UNUSABLE;
  453. memmove(skb->data + IEEE80211_CCMP_HDR_LEN, skb->data, hdrlen);
  454. skb_pull(skb, IEEE80211_CCMP_HDR_LEN);
  455. return RX_CONTINUE;
  456. }
  457. static void gcmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *j_0, u8 *aad)
  458. {
  459. __le16 mask_fc;
  460. u8 qos_tid;
  461. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  462. memcpy(j_0, hdr->addr2, ETH_ALEN);
  463. memcpy(&j_0[ETH_ALEN], pn, IEEE80211_GCMP_PN_LEN);
  464. j_0[13] = 0;
  465. j_0[14] = 0;
  466. j_0[AES_BLOCK_SIZE - 1] = 0x01;
  467. /* AAD (extra authenticate-only data) / masked 802.11 header
  468. * FC | A1 | A2 | A3 | SC | [A4] | [QC]
  469. */
  470. put_unaligned_be16(ieee80211_hdrlen(hdr->frame_control) - 2, &aad[0]);
  471. /* Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  472. * Retry, PwrMgt, MoreData; set Protected
  473. */
  474. mask_fc = hdr->frame_control;
  475. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  476. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  477. if (!ieee80211_is_mgmt(hdr->frame_control))
  478. mask_fc &= ~cpu_to_le16(0x0070);
  479. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  480. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  481. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  482. /* Mask Seq#, leave Frag# */
  483. aad[22] = *((u8 *)&hdr->seq_ctrl) & 0x0f;
  484. aad[23] = 0;
  485. if (ieee80211_is_data_qos(hdr->frame_control))
  486. qos_tid = *ieee80211_get_qos_ctl(hdr) &
  487. IEEE80211_QOS_CTL_TID_MASK;
  488. else
  489. qos_tid = 0;
  490. if (ieee80211_has_a4(hdr->frame_control)) {
  491. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  492. aad[30] = qos_tid;
  493. aad[31] = 0;
  494. } else {
  495. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  496. aad[24] = qos_tid;
  497. }
  498. }
  499. static inline void gcmp_pn2hdr(u8 *hdr, const u8 *pn, int key_id)
  500. {
  501. hdr[0] = pn[5];
  502. hdr[1] = pn[4];
  503. hdr[2] = 0;
  504. hdr[3] = 0x20 | (key_id << 6);
  505. hdr[4] = pn[3];
  506. hdr[5] = pn[2];
  507. hdr[6] = pn[1];
  508. hdr[7] = pn[0];
  509. }
  510. static inline void gcmp_hdr2pn(u8 *pn, const u8 *hdr)
  511. {
  512. pn[0] = hdr[7];
  513. pn[1] = hdr[6];
  514. pn[2] = hdr[5];
  515. pn[3] = hdr[4];
  516. pn[4] = hdr[1];
  517. pn[5] = hdr[0];
  518. }
  519. static int gcmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  520. {
  521. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  522. struct ieee80211_key *key = tx->key;
  523. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  524. int hdrlen, len, tail;
  525. u8 *pos;
  526. u8 pn[6];
  527. u64 pn64;
  528. u8 aad[GCM_AAD_LEN];
  529. u8 j_0[AES_BLOCK_SIZE];
  530. if (info->control.hw_key &&
  531. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  532. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  533. !((info->control.hw_key->flags &
  534. IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) &&
  535. ieee80211_is_mgmt(hdr->frame_control))) {
  536. /* hwaccel has no need for preallocated room for GCMP
  537. * header or MIC fields
  538. */
  539. return 0;
  540. }
  541. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  542. len = skb->len - hdrlen;
  543. if (info->control.hw_key)
  544. tail = 0;
  545. else
  546. tail = IEEE80211_GCMP_MIC_LEN;
  547. if (WARN_ON(skb_tailroom(skb) < tail ||
  548. skb_headroom(skb) < IEEE80211_GCMP_HDR_LEN))
  549. return -1;
  550. pos = skb_push(skb, IEEE80211_GCMP_HDR_LEN);
  551. memmove(pos, pos + IEEE80211_GCMP_HDR_LEN, hdrlen);
  552. skb_set_network_header(skb, skb_network_offset(skb) +
  553. IEEE80211_GCMP_HDR_LEN);
  554. /* the HW only needs room for the IV, but not the actual IV */
  555. if (info->control.hw_key &&
  556. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  557. return 0;
  558. hdr = (struct ieee80211_hdr *)pos;
  559. pos += hdrlen;
  560. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  561. pn[5] = pn64;
  562. pn[4] = pn64 >> 8;
  563. pn[3] = pn64 >> 16;
  564. pn[2] = pn64 >> 24;
  565. pn[1] = pn64 >> 32;
  566. pn[0] = pn64 >> 40;
  567. gcmp_pn2hdr(pos, pn, key->conf.keyidx);
  568. /* hwaccel - with software GCMP header */
  569. if (info->control.hw_key)
  570. return 0;
  571. pos += IEEE80211_GCMP_HDR_LEN;
  572. gcmp_special_blocks(skb, pn, j_0, aad);
  573. return ieee80211_aes_gcm_encrypt(key->u.gcmp.tfm, j_0, aad, pos, len,
  574. skb_put(skb, IEEE80211_GCMP_MIC_LEN));
  575. }
  576. ieee80211_tx_result
  577. ieee80211_crypto_gcmp_encrypt(struct ieee80211_tx_data *tx)
  578. {
  579. struct sk_buff *skb;
  580. ieee80211_tx_set_protected(tx);
  581. skb_queue_walk(&tx->skbs, skb) {
  582. if (gcmp_encrypt_skb(tx, skb) < 0)
  583. return TX_DROP;
  584. }
  585. return TX_CONTINUE;
  586. }
  587. ieee80211_rx_result
  588. ieee80211_crypto_gcmp_decrypt(struct ieee80211_rx_data *rx)
  589. {
  590. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  591. int hdrlen;
  592. struct ieee80211_key *key = rx->key;
  593. struct sk_buff *skb = rx->skb;
  594. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  595. u8 pn[IEEE80211_GCMP_PN_LEN];
  596. int data_len, queue, mic_len = IEEE80211_GCMP_MIC_LEN;
  597. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  598. if (!ieee80211_is_data(hdr->frame_control) &&
  599. !ieee80211_is_robust_mgmt_frame(skb))
  600. return RX_CONTINUE;
  601. if (status->flag & RX_FLAG_DECRYPTED) {
  602. if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_GCMP_HDR_LEN))
  603. return RX_DROP_UNUSABLE;
  604. if (status->flag & RX_FLAG_MIC_STRIPPED)
  605. mic_len = 0;
  606. } else {
  607. if (skb_linearize(rx->skb))
  608. return RX_DROP_UNUSABLE;
  609. }
  610. data_len = skb->len - hdrlen - IEEE80211_GCMP_HDR_LEN - mic_len;
  611. if (!rx->sta || data_len < 0)
  612. return RX_DROP_UNUSABLE;
  613. if (!(status->flag & RX_FLAG_PN_VALIDATED)) {
  614. int res;
  615. gcmp_hdr2pn(pn, skb->data + hdrlen);
  616. queue = rx->security_idx;
  617. res = memcmp(pn, key->u.gcmp.rx_pn[queue],
  618. IEEE80211_GCMP_PN_LEN);
  619. if (res < 0 ||
  620. (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) {
  621. key->u.gcmp.replays++;
  622. return RX_DROP_UNUSABLE;
  623. }
  624. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  625. u8 aad[2 * AES_BLOCK_SIZE];
  626. u8 j_0[AES_BLOCK_SIZE];
  627. /* hardware didn't decrypt/verify MIC */
  628. gcmp_special_blocks(skb, pn, j_0, aad);
  629. if (ieee80211_aes_gcm_decrypt(
  630. key->u.gcmp.tfm, j_0, aad,
  631. skb->data + hdrlen + IEEE80211_GCMP_HDR_LEN,
  632. data_len,
  633. skb->data + skb->len -
  634. IEEE80211_GCMP_MIC_LEN))
  635. return RX_DROP_UNUSABLE;
  636. }
  637. memcpy(key->u.gcmp.rx_pn[queue], pn, IEEE80211_GCMP_PN_LEN);
  638. }
  639. /* Remove GCMP header and MIC */
  640. if (pskb_trim(skb, skb->len - mic_len))
  641. return RX_DROP_UNUSABLE;
  642. memmove(skb->data + IEEE80211_GCMP_HDR_LEN, skb->data, hdrlen);
  643. skb_pull(skb, IEEE80211_GCMP_HDR_LEN);
  644. return RX_CONTINUE;
  645. }
  646. static ieee80211_tx_result
  647. ieee80211_crypto_cs_encrypt(struct ieee80211_tx_data *tx,
  648. struct sk_buff *skb)
  649. {
  650. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  651. struct ieee80211_key *key = tx->key;
  652. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  653. int hdrlen;
  654. u8 *pos, iv_len = key->conf.iv_len;
  655. if (info->control.hw_key &&
  656. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  657. /* hwaccel has no need for preallocated head room */
  658. return TX_CONTINUE;
  659. }
  660. if (unlikely(skb_headroom(skb) < iv_len &&
  661. pskb_expand_head(skb, iv_len, 0, GFP_ATOMIC)))
  662. return TX_DROP;
  663. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  664. pos = skb_push(skb, iv_len);
  665. memmove(pos, pos + iv_len, hdrlen);
  666. return TX_CONTINUE;
  667. }
  668. static inline int ieee80211_crypto_cs_pn_compare(u8 *pn1, u8 *pn2, int len)
  669. {
  670. int i;
  671. /* pn is little endian */
  672. for (i = len - 1; i >= 0; i--) {
  673. if (pn1[i] < pn2[i])
  674. return -1;
  675. else if (pn1[i] > pn2[i])
  676. return 1;
  677. }
  678. return 0;
  679. }
  680. static ieee80211_rx_result
  681. ieee80211_crypto_cs_decrypt(struct ieee80211_rx_data *rx)
  682. {
  683. struct ieee80211_key *key = rx->key;
  684. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  685. const struct ieee80211_cipher_scheme *cs = NULL;
  686. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  687. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  688. int data_len;
  689. u8 *rx_pn;
  690. u8 *skb_pn;
  691. u8 qos_tid;
  692. if (!rx->sta || !rx->sta->cipher_scheme ||
  693. !(status->flag & RX_FLAG_DECRYPTED))
  694. return RX_DROP_UNUSABLE;
  695. if (!ieee80211_is_data(hdr->frame_control))
  696. return RX_CONTINUE;
  697. cs = rx->sta->cipher_scheme;
  698. data_len = rx->skb->len - hdrlen - cs->hdr_len;
  699. if (data_len < 0)
  700. return RX_DROP_UNUSABLE;
  701. if (ieee80211_is_data_qos(hdr->frame_control))
  702. qos_tid = *ieee80211_get_qos_ctl(hdr) &
  703. IEEE80211_QOS_CTL_TID_MASK;
  704. else
  705. qos_tid = 0;
  706. if (skb_linearize(rx->skb))
  707. return RX_DROP_UNUSABLE;
  708. hdr = (struct ieee80211_hdr *)rx->skb->data;
  709. rx_pn = key->u.gen.rx_pn[qos_tid];
  710. skb_pn = rx->skb->data + hdrlen + cs->pn_off;
  711. if (ieee80211_crypto_cs_pn_compare(skb_pn, rx_pn, cs->pn_len) <= 0)
  712. return RX_DROP_UNUSABLE;
  713. memcpy(rx_pn, skb_pn, cs->pn_len);
  714. /* remove security header and MIC */
  715. if (pskb_trim(rx->skb, rx->skb->len - cs->mic_len))
  716. return RX_DROP_UNUSABLE;
  717. memmove(rx->skb->data + cs->hdr_len, rx->skb->data, hdrlen);
  718. skb_pull(rx->skb, cs->hdr_len);
  719. return RX_CONTINUE;
  720. }
  721. static void bip_aad(struct sk_buff *skb, u8 *aad)
  722. {
  723. __le16 mask_fc;
  724. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  725. /* BIP AAD: FC(masked) || A1 || A2 || A3 */
  726. /* FC type/subtype */
  727. /* Mask FC Retry, PwrMgt, MoreData flags to zero */
  728. mask_fc = hdr->frame_control;
  729. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY | IEEE80211_FCTL_PM |
  730. IEEE80211_FCTL_MOREDATA);
  731. put_unaligned(mask_fc, (__le16 *) &aad[0]);
  732. /* A1 || A2 || A3 */
  733. memcpy(aad + 2, &hdr->addr1, 3 * ETH_ALEN);
  734. }
  735. static inline void bip_ipn_set64(u8 *d, u64 pn)
  736. {
  737. *d++ = pn;
  738. *d++ = pn >> 8;
  739. *d++ = pn >> 16;
  740. *d++ = pn >> 24;
  741. *d++ = pn >> 32;
  742. *d = pn >> 40;
  743. }
  744. static inline void bip_ipn_swap(u8 *d, const u8 *s)
  745. {
  746. *d++ = s[5];
  747. *d++ = s[4];
  748. *d++ = s[3];
  749. *d++ = s[2];
  750. *d++ = s[1];
  751. *d = s[0];
  752. }
  753. ieee80211_tx_result
  754. ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
  755. {
  756. struct sk_buff *skb;
  757. struct ieee80211_tx_info *info;
  758. struct ieee80211_key *key = tx->key;
  759. struct ieee80211_mmie *mmie;
  760. u8 aad[20];
  761. u64 pn64;
  762. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  763. return TX_DROP;
  764. skb = skb_peek(&tx->skbs);
  765. info = IEEE80211_SKB_CB(skb);
  766. if (info->control.hw_key)
  767. return TX_CONTINUE;
  768. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  769. return TX_DROP;
  770. mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie));
  771. mmie->element_id = WLAN_EID_MMIE;
  772. mmie->length = sizeof(*mmie) - 2;
  773. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  774. /* PN = PN + 1 */
  775. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  776. bip_ipn_set64(mmie->sequence_number, pn64);
  777. bip_aad(skb, aad);
  778. /*
  779. * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
  780. */
  781. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  782. skb->data + 24, skb->len - 24, mmie->mic);
  783. return TX_CONTINUE;
  784. }
  785. ieee80211_tx_result
  786. ieee80211_crypto_aes_cmac_256_encrypt(struct ieee80211_tx_data *tx)
  787. {
  788. struct sk_buff *skb;
  789. struct ieee80211_tx_info *info;
  790. struct ieee80211_key *key = tx->key;
  791. struct ieee80211_mmie_16 *mmie;
  792. u8 aad[20];
  793. u64 pn64;
  794. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  795. return TX_DROP;
  796. skb = skb_peek(&tx->skbs);
  797. info = IEEE80211_SKB_CB(skb);
  798. if (info->control.hw_key)
  799. return TX_CONTINUE;
  800. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  801. return TX_DROP;
  802. mmie = (struct ieee80211_mmie_16 *)skb_put(skb, sizeof(*mmie));
  803. mmie->element_id = WLAN_EID_MMIE;
  804. mmie->length = sizeof(*mmie) - 2;
  805. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  806. /* PN = PN + 1 */
  807. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  808. bip_ipn_set64(mmie->sequence_number, pn64);
  809. bip_aad(skb, aad);
  810. /* MIC = AES-256-CMAC(IGTK, AAD || Management Frame Body || MMIE, 128)
  811. */
  812. ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
  813. skb->data + 24, skb->len - 24, mmie->mic);
  814. return TX_CONTINUE;
  815. }
  816. ieee80211_rx_result
  817. ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
  818. {
  819. struct sk_buff *skb = rx->skb;
  820. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  821. struct ieee80211_key *key = rx->key;
  822. struct ieee80211_mmie *mmie;
  823. u8 aad[20], mic[8], ipn[6];
  824. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  825. if (!ieee80211_is_mgmt(hdr->frame_control))
  826. return RX_CONTINUE;
  827. /* management frames are already linear */
  828. if (skb->len < 24 + sizeof(*mmie))
  829. return RX_DROP_UNUSABLE;
  830. mmie = (struct ieee80211_mmie *)
  831. (skb->data + skb->len - sizeof(*mmie));
  832. if (mmie->element_id != WLAN_EID_MMIE ||
  833. mmie->length != sizeof(*mmie) - 2)
  834. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  835. bip_ipn_swap(ipn, mmie->sequence_number);
  836. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  837. key->u.aes_cmac.replays++;
  838. return RX_DROP_UNUSABLE;
  839. }
  840. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  841. /* hardware didn't decrypt/verify MIC */
  842. bip_aad(skb, aad);
  843. ieee80211_aes_cmac(key->u.aes_cmac.tfm, aad,
  844. skb->data + 24, skb->len - 24, mic);
  845. if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  846. key->u.aes_cmac.icverrors++;
  847. return RX_DROP_UNUSABLE;
  848. }
  849. }
  850. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  851. /* Remove MMIE */
  852. skb_trim(skb, skb->len - sizeof(*mmie));
  853. return RX_CONTINUE;
  854. }
  855. ieee80211_rx_result
  856. ieee80211_crypto_aes_cmac_256_decrypt(struct ieee80211_rx_data *rx)
  857. {
  858. struct sk_buff *skb = rx->skb;
  859. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  860. struct ieee80211_key *key = rx->key;
  861. struct ieee80211_mmie_16 *mmie;
  862. u8 aad[20], mic[16], ipn[6];
  863. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  864. if (!ieee80211_is_mgmt(hdr->frame_control))
  865. return RX_CONTINUE;
  866. /* management frames are already linear */
  867. if (skb->len < 24 + sizeof(*mmie))
  868. return RX_DROP_UNUSABLE;
  869. mmie = (struct ieee80211_mmie_16 *)
  870. (skb->data + skb->len - sizeof(*mmie));
  871. if (mmie->element_id != WLAN_EID_MMIE ||
  872. mmie->length != sizeof(*mmie) - 2)
  873. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  874. bip_ipn_swap(ipn, mmie->sequence_number);
  875. if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
  876. key->u.aes_cmac.replays++;
  877. return RX_DROP_UNUSABLE;
  878. }
  879. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  880. /* hardware didn't decrypt/verify MIC */
  881. bip_aad(skb, aad);
  882. ieee80211_aes_cmac_256(key->u.aes_cmac.tfm, aad,
  883. skb->data + 24, skb->len - 24, mic);
  884. if (crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  885. key->u.aes_cmac.icverrors++;
  886. return RX_DROP_UNUSABLE;
  887. }
  888. }
  889. memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
  890. /* Remove MMIE */
  891. skb_trim(skb, skb->len - sizeof(*mmie));
  892. return RX_CONTINUE;
  893. }
  894. ieee80211_tx_result
  895. ieee80211_crypto_aes_gmac_encrypt(struct ieee80211_tx_data *tx)
  896. {
  897. struct sk_buff *skb;
  898. struct ieee80211_tx_info *info;
  899. struct ieee80211_key *key = tx->key;
  900. struct ieee80211_mmie_16 *mmie;
  901. struct ieee80211_hdr *hdr;
  902. u8 aad[GMAC_AAD_LEN];
  903. u64 pn64;
  904. u8 nonce[GMAC_NONCE_LEN];
  905. if (WARN_ON(skb_queue_len(&tx->skbs) != 1))
  906. return TX_DROP;
  907. skb = skb_peek(&tx->skbs);
  908. info = IEEE80211_SKB_CB(skb);
  909. if (info->control.hw_key)
  910. return TX_CONTINUE;
  911. if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
  912. return TX_DROP;
  913. mmie = (struct ieee80211_mmie_16 *)skb_put(skb, sizeof(*mmie));
  914. mmie->element_id = WLAN_EID_MMIE;
  915. mmie->length = sizeof(*mmie) - 2;
  916. mmie->key_id = cpu_to_le16(key->conf.keyidx);
  917. /* PN = PN + 1 */
  918. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  919. bip_ipn_set64(mmie->sequence_number, pn64);
  920. bip_aad(skb, aad);
  921. hdr = (struct ieee80211_hdr *)skb->data;
  922. memcpy(nonce, hdr->addr2, ETH_ALEN);
  923. bip_ipn_swap(nonce + ETH_ALEN, mmie->sequence_number);
  924. /* MIC = AES-GMAC(IGTK, AAD || Management Frame Body || MMIE, 128) */
  925. if (ieee80211_aes_gmac(key->u.aes_gmac.tfm, aad, nonce,
  926. skb->data + 24, skb->len - 24, mmie->mic) < 0)
  927. return TX_DROP;
  928. return TX_CONTINUE;
  929. }
  930. ieee80211_rx_result
  931. ieee80211_crypto_aes_gmac_decrypt(struct ieee80211_rx_data *rx)
  932. {
  933. struct sk_buff *skb = rx->skb;
  934. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  935. struct ieee80211_key *key = rx->key;
  936. struct ieee80211_mmie_16 *mmie;
  937. u8 aad[GMAC_AAD_LEN], mic[GMAC_MIC_LEN], ipn[6], nonce[GMAC_NONCE_LEN];
  938. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  939. if (!ieee80211_is_mgmt(hdr->frame_control))
  940. return RX_CONTINUE;
  941. /* management frames are already linear */
  942. if (skb->len < 24 + sizeof(*mmie))
  943. return RX_DROP_UNUSABLE;
  944. mmie = (struct ieee80211_mmie_16 *)
  945. (skb->data + skb->len - sizeof(*mmie));
  946. if (mmie->element_id != WLAN_EID_MMIE ||
  947. mmie->length != sizeof(*mmie) - 2)
  948. return RX_DROP_UNUSABLE; /* Invalid MMIE */
  949. bip_ipn_swap(ipn, mmie->sequence_number);
  950. if (memcmp(ipn, key->u.aes_gmac.rx_pn, 6) <= 0) {
  951. key->u.aes_gmac.replays++;
  952. return RX_DROP_UNUSABLE;
  953. }
  954. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  955. /* hardware didn't decrypt/verify MIC */
  956. bip_aad(skb, aad);
  957. memcpy(nonce, hdr->addr2, ETH_ALEN);
  958. memcpy(nonce + ETH_ALEN, ipn, 6);
  959. if (ieee80211_aes_gmac(key->u.aes_gmac.tfm, aad, nonce,
  960. skb->data + 24, skb->len - 24,
  961. mic) < 0 ||
  962. crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  963. key->u.aes_gmac.icverrors++;
  964. return RX_DROP_UNUSABLE;
  965. }
  966. }
  967. memcpy(key->u.aes_gmac.rx_pn, ipn, 6);
  968. /* Remove MMIE */
  969. skb_trim(skb, skb->len - sizeof(*mmie));
  970. return RX_CONTINUE;
  971. }
  972. ieee80211_tx_result
  973. ieee80211_crypto_hw_encrypt(struct ieee80211_tx_data *tx)
  974. {
  975. struct sk_buff *skb;
  976. struct ieee80211_tx_info *info = NULL;
  977. ieee80211_tx_result res;
  978. skb_queue_walk(&tx->skbs, skb) {
  979. info = IEEE80211_SKB_CB(skb);
  980. /* handle hw-only algorithm */
  981. if (!info->control.hw_key)
  982. return TX_DROP;
  983. if (tx->key->flags & KEY_FLAG_CIPHER_SCHEME) {
  984. res = ieee80211_crypto_cs_encrypt(tx, skb);
  985. if (res != TX_CONTINUE)
  986. return res;
  987. }
  988. }
  989. ieee80211_tx_set_protected(tx);
  990. return TX_CONTINUE;
  991. }
  992. ieee80211_rx_result
  993. ieee80211_crypto_hw_decrypt(struct ieee80211_rx_data *rx)
  994. {
  995. if (rx->sta && rx->sta->cipher_scheme)
  996. return ieee80211_crypto_cs_decrypt(rx);
  997. return RX_DROP_UNUSABLE;
  998. }