wpa.c 32 KB

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