wpa.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260
  1. /*
  2. * Copyright 2002-2004, Instant802 Networks, Inc.
  3. * Copyright 2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/netdevice.h>
  10. #include <linux/types.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/compiler.h>
  13. #include <linux/ieee80211.h>
  14. #include <linux/gfp.h>
  15. #include <asm/unaligned.h>
  16. #include <net/mac80211.h>
  17. #include <crypto/aes.h>
  18. #include <crypto/algapi.h>
  19. #include "ieee80211_i.h"
  20. #include "michael.h"
  21. #include "tkip.h"
  22. #include "aes_ccm.h"
  23. #include "aes_cmac.h"
  24. #include "aes_gmac.h"
  25. #include "aes_gcm.h"
  26. #include "wpa.h"
  27. ieee80211_tx_result
  28. ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
  29. {
  30. u8 *data, *key, *mic;
  31. size_t data_len;
  32. unsigned int hdrlen;
  33. struct ieee80211_hdr *hdr;
  34. struct sk_buff *skb = tx->skb;
  35. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  36. int tail;
  37. hdr = (struct ieee80211_hdr *)skb->data;
  38. if (!tx->key || tx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  39. skb->len < 24 || !ieee80211_is_data_present(hdr->frame_control))
  40. return TX_CONTINUE;
  41. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  42. if (skb->len < hdrlen)
  43. return TX_DROP;
  44. data = skb->data + hdrlen;
  45. data_len = skb->len - hdrlen;
  46. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE)) {
  47. /* Need to use software crypto for the test */
  48. info->control.hw_key = NULL;
  49. }
  50. if (info->control.hw_key &&
  51. (info->flags & IEEE80211_TX_CTL_DONTFRAG ||
  52. tx->local->ops->set_frag_threshold) &&
  53. !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC)) {
  54. /* hwaccel - with no need for SW-generated MMIC */
  55. return TX_CONTINUE;
  56. }
  57. tail = MICHAEL_MIC_LEN;
  58. if (!info->control.hw_key)
  59. tail += IEEE80211_TKIP_ICV_LEN;
  60. if (WARN(skb_tailroom(skb) < tail ||
  61. skb_headroom(skb) < IEEE80211_TKIP_IV_LEN,
  62. "mmic: not enough head/tail (%d/%d,%d/%d)\n",
  63. skb_headroom(skb), IEEE80211_TKIP_IV_LEN,
  64. skb_tailroom(skb), tail))
  65. return TX_DROP;
  66. key = &tx->key->conf.key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY];
  67. mic = skb_put(skb, MICHAEL_MIC_LEN);
  68. michael_mic(key, hdr, data, data_len, mic);
  69. if (unlikely(info->flags & IEEE80211_TX_INTFL_TKIP_MIC_FAILURE))
  70. mic[0]++;
  71. return TX_CONTINUE;
  72. }
  73. ieee80211_rx_result
  74. ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
  75. {
  76. u8 *data, *key = NULL;
  77. size_t data_len;
  78. unsigned int hdrlen;
  79. u8 mic[MICHAEL_MIC_LEN];
  80. struct sk_buff *skb = rx->skb;
  81. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  82. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  83. /*
  84. * it makes no sense to check for MIC errors on anything other
  85. * than data frames.
  86. */
  87. if (!ieee80211_is_data_present(hdr->frame_control))
  88. return RX_CONTINUE;
  89. /*
  90. * No way to verify the MIC if the hardware stripped it or
  91. * the IV with the key index. In this case we have solely rely
  92. * on the driver to set RX_FLAG_MMIC_ERROR in the event of a
  93. * MIC failure report.
  94. */
  95. if (status->flag & (RX_FLAG_MMIC_STRIPPED | RX_FLAG_IV_STRIPPED)) {
  96. if (status->flag & RX_FLAG_MMIC_ERROR)
  97. goto mic_fail_no_key;
  98. if (!(status->flag & RX_FLAG_IV_STRIPPED) && rx->key &&
  99. rx->key->conf.cipher == WLAN_CIPHER_SUITE_TKIP)
  100. goto update_iv;
  101. return RX_CONTINUE;
  102. }
  103. /*
  104. * Some hardware seems to generate Michael MIC failure reports; even
  105. * though, the frame was not encrypted with TKIP and therefore has no
  106. * MIC. Ignore the flag them to avoid triggering countermeasures.
  107. */
  108. if (!rx->key || rx->key->conf.cipher != WLAN_CIPHER_SUITE_TKIP ||
  109. !(status->flag & RX_FLAG_DECRYPTED))
  110. return RX_CONTINUE;
  111. if (rx->sdata->vif.type == NL80211_IFTYPE_AP && rx->key->conf.keyidx) {
  112. /*
  113. * APs with pairwise keys should never receive Michael MIC
  114. * errors for non-zero keyidx because these are reserved for
  115. * group keys and only the AP is sending real multicast
  116. * frames in the BSS.
  117. */
  118. return RX_DROP_UNUSABLE;
  119. }
  120. if (status->flag & RX_FLAG_MMIC_ERROR)
  121. goto mic_fail;
  122. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  123. if (skb->len < hdrlen + MICHAEL_MIC_LEN)
  124. return RX_DROP_UNUSABLE;
  125. if (skb_linearize(rx->skb))
  126. return RX_DROP_UNUSABLE;
  127. hdr = (void *)skb->data;
  128. data = skb->data + hdrlen;
  129. data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
  130. key = &rx->key->conf.key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY];
  131. michael_mic(key, hdr, data, data_len, mic);
  132. if (crypto_memneq(mic, data + data_len, MICHAEL_MIC_LEN))
  133. goto mic_fail;
  134. /* remove Michael MIC from payload */
  135. skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
  136. update_iv:
  137. /* update IV in key information to be able to detect replays */
  138. rx->key->u.tkip.rx[rx->security_idx].iv32 = rx->tkip_iv32;
  139. rx->key->u.tkip.rx[rx->security_idx].iv16 = rx->tkip_iv16;
  140. return RX_CONTINUE;
  141. mic_fail:
  142. rx->key->u.tkip.mic_failures++;
  143. mic_fail_no_key:
  144. /*
  145. * In some cases the key can be unset - e.g. a multicast packet, in
  146. * a driver that supports HW encryption. Send up the key idx only if
  147. * the key is set.
  148. */
  149. cfg80211_michael_mic_failure(rx->sdata->dev, hdr->addr2,
  150. is_multicast_ether_addr(hdr->addr1) ?
  151. NL80211_KEYTYPE_GROUP :
  152. NL80211_KEYTYPE_PAIRWISE,
  153. rx->key ? rx->key->conf.keyidx : -1,
  154. NULL, GFP_ATOMIC);
  155. return RX_DROP_UNUSABLE;
  156. }
  157. static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  158. {
  159. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  160. struct ieee80211_key *key = tx->key;
  161. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  162. unsigned int hdrlen;
  163. int len, tail;
  164. u8 *pos;
  165. if (info->control.hw_key &&
  166. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  167. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  168. /* hwaccel - with no need for software-generated IV */
  169. return 0;
  170. }
  171. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  172. len = skb->len - hdrlen;
  173. if (info->control.hw_key)
  174. tail = 0;
  175. else
  176. tail = IEEE80211_TKIP_ICV_LEN;
  177. if (WARN_ON(skb_tailroom(skb) < tail ||
  178. skb_headroom(skb) < IEEE80211_TKIP_IV_LEN))
  179. return -1;
  180. pos = skb_push(skb, IEEE80211_TKIP_IV_LEN);
  181. memmove(pos, pos + IEEE80211_TKIP_IV_LEN, hdrlen);
  182. pos += hdrlen;
  183. /* the HW only needs room for the IV, but not the actual IV */
  184. if (info->control.hw_key &&
  185. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  186. return 0;
  187. /* Increase IV for the frame */
  188. spin_lock(&key->u.tkip.txlock);
  189. key->u.tkip.tx.iv16++;
  190. if (key->u.tkip.tx.iv16 == 0)
  191. key->u.tkip.tx.iv32++;
  192. pos = ieee80211_tkip_add_iv(pos, key);
  193. spin_unlock(&key->u.tkip.txlock);
  194. /* hwaccel - with software IV */
  195. if (info->control.hw_key)
  196. return 0;
  197. /* Add room for ICV */
  198. skb_put(skb, IEEE80211_TKIP_ICV_LEN);
  199. return ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
  200. key, skb, pos, len);
  201. }
  202. ieee80211_tx_result
  203. ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
  204. {
  205. struct sk_buff *skb;
  206. ieee80211_tx_set_protected(tx);
  207. skb_queue_walk(&tx->skbs, skb) {
  208. if (tkip_encrypt_skb(tx, skb) < 0)
  209. return TX_DROP;
  210. }
  211. return TX_CONTINUE;
  212. }
  213. ieee80211_rx_result
  214. ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
  215. {
  216. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
  217. int hdrlen, res, hwaccel = 0;
  218. struct ieee80211_key *key = rx->key;
  219. struct sk_buff *skb = rx->skb;
  220. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  221. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  222. if (!ieee80211_is_data(hdr->frame_control))
  223. return RX_CONTINUE;
  224. if (!rx->sta || skb->len - hdrlen < 12)
  225. return RX_DROP_UNUSABLE;
  226. /* it may be possible to optimize this a bit more */
  227. if (skb_linearize(rx->skb))
  228. return RX_DROP_UNUSABLE;
  229. hdr = (void *)skb->data;
  230. /*
  231. * Let TKIP code verify IV, but skip decryption.
  232. * In the case where hardware checks the IV as well,
  233. * we don't even get here, see ieee80211_rx_h_decrypt()
  234. */
  235. if (status->flag & RX_FLAG_DECRYPTED)
  236. hwaccel = 1;
  237. res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
  238. key, skb->data + hdrlen,
  239. skb->len - hdrlen, rx->sta->sta.addr,
  240. hdr->addr1, hwaccel, rx->security_idx,
  241. &rx->tkip_iv32,
  242. &rx->tkip_iv16);
  243. if (res != TKIP_DECRYPT_OK)
  244. return RX_DROP_UNUSABLE;
  245. /* Trim ICV */
  246. if (!(status->flag & RX_FLAG_ICV_STRIPPED))
  247. skb_trim(skb, skb->len - IEEE80211_TKIP_ICV_LEN);
  248. /* Remove IV */
  249. memmove(skb->data + IEEE80211_TKIP_IV_LEN, skb->data, hdrlen);
  250. skb_pull(skb, IEEE80211_TKIP_IV_LEN);
  251. return RX_CONTINUE;
  252. }
  253. static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad)
  254. {
  255. __le16 mask_fc;
  256. int a4_included, mgmt;
  257. u8 qos_tid;
  258. u16 len_a;
  259. unsigned int hdrlen;
  260. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  261. /*
  262. * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  263. * Retry, PwrMgt, MoreData; set Protected
  264. */
  265. mgmt = ieee80211_is_mgmt(hdr->frame_control);
  266. mask_fc = hdr->frame_control;
  267. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  268. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  269. if (!mgmt)
  270. mask_fc &= ~cpu_to_le16(0x0070);
  271. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  272. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  273. len_a = hdrlen - 2;
  274. a4_included = ieee80211_has_a4(hdr->frame_control);
  275. if (ieee80211_is_data_qos(hdr->frame_control))
  276. qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
  277. else
  278. qos_tid = 0;
  279. /* In CCM, the initial vectors (IV) used for CTR mode encryption and CBC
  280. * mode authentication are not allowed to collide, yet both are derived
  281. * from this vector b_0. We only set L := 1 here to indicate that the
  282. * data size can be represented in (L+1) bytes. The CCM layer will take
  283. * care of storing the data length in the top (L+1) bytes and setting
  284. * and clearing the other bits as is required to derive the two IVs.
  285. */
  286. b_0[0] = 0x1;
  287. /* Nonce: Nonce Flags | A2 | PN
  288. * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
  289. */
  290. b_0[1] = qos_tid | (mgmt << 4);
  291. memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
  292. memcpy(&b_0[8], pn, IEEE80211_CCMP_PN_LEN);
  293. /* AAD (extra authenticate-only data) / masked 802.11 header
  294. * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
  295. put_unaligned_be16(len_a, &aad[0]);
  296. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  297. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  298. /* Mask Seq#, leave Frag# */
  299. aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
  300. aad[23] = 0;
  301. if (a4_included) {
  302. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  303. aad[30] = qos_tid;
  304. aad[31] = 0;
  305. } else {
  306. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  307. aad[24] = qos_tid;
  308. }
  309. }
  310. static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
  311. {
  312. hdr[0] = pn[5];
  313. hdr[1] = pn[4];
  314. hdr[2] = 0;
  315. hdr[3] = 0x20 | (key_id << 6);
  316. hdr[4] = pn[3];
  317. hdr[5] = pn[2];
  318. hdr[6] = pn[1];
  319. hdr[7] = pn[0];
  320. }
  321. static inline void ccmp_hdr2pn(u8 *pn, u8 *hdr)
  322. {
  323. pn[0] = hdr[7];
  324. pn[1] = hdr[6];
  325. pn[2] = hdr[5];
  326. pn[3] = hdr[4];
  327. pn[4] = hdr[1];
  328. pn[5] = hdr[0];
  329. }
  330. static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb,
  331. unsigned int mic_len)
  332. {
  333. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  334. struct ieee80211_key *key = tx->key;
  335. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  336. int hdrlen, len, tail;
  337. u8 *pos;
  338. u8 pn[6];
  339. u64 pn64;
  340. u8 aad[2 * AES_BLOCK_SIZE];
  341. u8 b_0[AES_BLOCK_SIZE];
  342. if (info->control.hw_key &&
  343. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  344. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  345. !((info->control.hw_key->flags &
  346. IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) &&
  347. ieee80211_is_mgmt(hdr->frame_control))) {
  348. /*
  349. * hwaccel has no need for preallocated room for CCMP
  350. * header or MIC fields
  351. */
  352. return 0;
  353. }
  354. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  355. len = skb->len - hdrlen;
  356. if (info->control.hw_key)
  357. tail = 0;
  358. else
  359. tail = mic_len;
  360. if (WARN_ON(skb_tailroom(skb) < tail ||
  361. skb_headroom(skb) < IEEE80211_CCMP_HDR_LEN))
  362. return -1;
  363. pos = skb_push(skb, IEEE80211_CCMP_HDR_LEN);
  364. memmove(pos, pos + IEEE80211_CCMP_HDR_LEN, hdrlen);
  365. /* the HW only needs room for the IV, but not the actual IV */
  366. if (info->control.hw_key &&
  367. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  368. return 0;
  369. hdr = (struct ieee80211_hdr *) pos;
  370. pos += hdrlen;
  371. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  372. pn[5] = pn64;
  373. pn[4] = pn64 >> 8;
  374. pn[3] = pn64 >> 16;
  375. pn[2] = pn64 >> 24;
  376. pn[1] = pn64 >> 32;
  377. pn[0] = pn64 >> 40;
  378. ccmp_pn2hdr(pos, pn, key->conf.keyidx);
  379. /* hwaccel - with software CCMP header */
  380. if (info->control.hw_key)
  381. return 0;
  382. pos += IEEE80211_CCMP_HDR_LEN;
  383. ccmp_special_blocks(skb, pn, b_0, aad);
  384. ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, b_0, aad, pos, len,
  385. skb_put(skb, mic_len), mic_len);
  386. return 0;
  387. }
  388. ieee80211_tx_result
  389. ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx,
  390. unsigned int mic_len)
  391. {
  392. struct sk_buff *skb;
  393. ieee80211_tx_set_protected(tx);
  394. skb_queue_walk(&tx->skbs, skb) {
  395. if (ccmp_encrypt_skb(tx, skb, mic_len) < 0)
  396. return TX_DROP;
  397. }
  398. return TX_CONTINUE;
  399. }
  400. ieee80211_rx_result
  401. ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx,
  402. unsigned int mic_len)
  403. {
  404. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  405. int hdrlen;
  406. struct ieee80211_key *key = rx->key;
  407. struct sk_buff *skb = rx->skb;
  408. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  409. u8 pn[IEEE80211_CCMP_PN_LEN];
  410. int data_len;
  411. int queue;
  412. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  413. if (!ieee80211_is_data(hdr->frame_control) &&
  414. !ieee80211_is_robust_mgmt_frame(skb))
  415. return RX_CONTINUE;
  416. if (status->flag & RX_FLAG_DECRYPTED) {
  417. if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_CCMP_HDR_LEN))
  418. return RX_DROP_UNUSABLE;
  419. if (status->flag & RX_FLAG_MIC_STRIPPED)
  420. mic_len = 0;
  421. } else {
  422. if (skb_linearize(rx->skb))
  423. return RX_DROP_UNUSABLE;
  424. }
  425. data_len = skb->len - hdrlen - IEEE80211_CCMP_HDR_LEN - mic_len;
  426. if (!rx->sta || data_len < 0)
  427. return RX_DROP_UNUSABLE;
  428. if (!(status->flag & RX_FLAG_PN_VALIDATED)) {
  429. int res;
  430. ccmp_hdr2pn(pn, skb->data + hdrlen);
  431. queue = rx->security_idx;
  432. res = memcmp(pn, key->u.ccmp.rx_pn[queue],
  433. IEEE80211_CCMP_PN_LEN);
  434. if (res < 0 ||
  435. (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) {
  436. key->u.ccmp.replays++;
  437. return RX_DROP_UNUSABLE;
  438. }
  439. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  440. u8 aad[2 * AES_BLOCK_SIZE];
  441. u8 b_0[AES_BLOCK_SIZE];
  442. /* hardware didn't decrypt/verify MIC */
  443. ccmp_special_blocks(skb, pn, b_0, aad);
  444. if (ieee80211_aes_ccm_decrypt(
  445. key->u.ccmp.tfm, b_0, aad,
  446. skb->data + hdrlen + IEEE80211_CCMP_HDR_LEN,
  447. data_len,
  448. skb->data + skb->len - mic_len, mic_len))
  449. return RX_DROP_UNUSABLE;
  450. }
  451. memcpy(key->u.ccmp.rx_pn[queue], pn, IEEE80211_CCMP_PN_LEN);
  452. }
  453. /* Remove CCMP header and MIC */
  454. if (pskb_trim(skb, skb->len - mic_len))
  455. return RX_DROP_UNUSABLE;
  456. memmove(skb->data + IEEE80211_CCMP_HDR_LEN, skb->data, hdrlen);
  457. skb_pull(skb, IEEE80211_CCMP_HDR_LEN);
  458. return RX_CONTINUE;
  459. }
  460. static void gcmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *j_0, u8 *aad)
  461. {
  462. __le16 mask_fc;
  463. u8 qos_tid;
  464. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  465. memcpy(j_0, hdr->addr2, ETH_ALEN);
  466. memcpy(&j_0[ETH_ALEN], pn, IEEE80211_GCMP_PN_LEN);
  467. j_0[13] = 0;
  468. j_0[14] = 0;
  469. j_0[AES_BLOCK_SIZE - 1] = 0x01;
  470. /* AAD (extra authenticate-only data) / masked 802.11 header
  471. * FC | A1 | A2 | A3 | SC | [A4] | [QC]
  472. */
  473. put_unaligned_be16(ieee80211_hdrlen(hdr->frame_control) - 2, &aad[0]);
  474. /* Mask FC: zero subtype b4 b5 b6 (if not mgmt)
  475. * Retry, PwrMgt, MoreData; set Protected
  476. */
  477. mask_fc = hdr->frame_control;
  478. mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
  479. IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
  480. if (!ieee80211_is_mgmt(hdr->frame_control))
  481. mask_fc &= ~cpu_to_le16(0x0070);
  482. mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
  483. put_unaligned(mask_fc, (__le16 *)&aad[2]);
  484. memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
  485. /* Mask Seq#, leave Frag# */
  486. aad[22] = *((u8 *)&hdr->seq_ctrl) & 0x0f;
  487. aad[23] = 0;
  488. if (ieee80211_is_data_qos(hdr->frame_control))
  489. qos_tid = *ieee80211_get_qos_ctl(hdr) &
  490. IEEE80211_QOS_CTL_TID_MASK;
  491. else
  492. qos_tid = 0;
  493. if (ieee80211_has_a4(hdr->frame_control)) {
  494. memcpy(&aad[24], hdr->addr4, ETH_ALEN);
  495. aad[30] = qos_tid;
  496. aad[31] = 0;
  497. } else {
  498. memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
  499. aad[24] = qos_tid;
  500. }
  501. }
  502. static inline void gcmp_pn2hdr(u8 *hdr, const u8 *pn, int key_id)
  503. {
  504. hdr[0] = pn[5];
  505. hdr[1] = pn[4];
  506. hdr[2] = 0;
  507. hdr[3] = 0x20 | (key_id << 6);
  508. hdr[4] = pn[3];
  509. hdr[5] = pn[2];
  510. hdr[6] = pn[1];
  511. hdr[7] = pn[0];
  512. }
  513. static inline void gcmp_hdr2pn(u8 *pn, const u8 *hdr)
  514. {
  515. pn[0] = hdr[7];
  516. pn[1] = hdr[6];
  517. pn[2] = hdr[5];
  518. pn[3] = hdr[4];
  519. pn[4] = hdr[1];
  520. pn[5] = hdr[0];
  521. }
  522. static int gcmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
  523. {
  524. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  525. struct ieee80211_key *key = tx->key;
  526. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  527. int hdrlen, len, tail;
  528. u8 *pos;
  529. u8 pn[6];
  530. u64 pn64;
  531. u8 aad[2 * AES_BLOCK_SIZE];
  532. u8 j_0[AES_BLOCK_SIZE];
  533. if (info->control.hw_key &&
  534. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
  535. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) &&
  536. !((info->control.hw_key->flags &
  537. IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) &&
  538. ieee80211_is_mgmt(hdr->frame_control))) {
  539. /* hwaccel has no need for preallocated room for GCMP
  540. * header or MIC fields
  541. */
  542. return 0;
  543. }
  544. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  545. len = skb->len - hdrlen;
  546. if (info->control.hw_key)
  547. tail = 0;
  548. else
  549. tail = IEEE80211_GCMP_MIC_LEN;
  550. if (WARN_ON(skb_tailroom(skb) < tail ||
  551. skb_headroom(skb) < IEEE80211_GCMP_HDR_LEN))
  552. return -1;
  553. pos = skb_push(skb, IEEE80211_GCMP_HDR_LEN);
  554. memmove(pos, pos + IEEE80211_GCMP_HDR_LEN, hdrlen);
  555. skb_set_network_header(skb, skb_network_offset(skb) +
  556. IEEE80211_GCMP_HDR_LEN);
  557. /* the HW only needs room for the IV, but not the actual IV */
  558. if (info->control.hw_key &&
  559. (info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE))
  560. return 0;
  561. hdr = (struct ieee80211_hdr *)pos;
  562. pos += hdrlen;
  563. pn64 = atomic64_inc_return(&key->conf.tx_pn);
  564. pn[5] = pn64;
  565. pn[4] = pn64 >> 8;
  566. pn[3] = pn64 >> 16;
  567. pn[2] = pn64 >> 24;
  568. pn[1] = pn64 >> 32;
  569. pn[0] = pn64 >> 40;
  570. gcmp_pn2hdr(pos, pn, key->conf.keyidx);
  571. /* hwaccel - with software GCMP header */
  572. if (info->control.hw_key)
  573. return 0;
  574. pos += IEEE80211_GCMP_HDR_LEN;
  575. gcmp_special_blocks(skb, pn, j_0, aad);
  576. ieee80211_aes_gcm_encrypt(key->u.gcmp.tfm, j_0, aad, pos, len,
  577. skb_put(skb, IEEE80211_GCMP_MIC_LEN));
  578. return 0;
  579. }
  580. ieee80211_tx_result
  581. ieee80211_crypto_gcmp_encrypt(struct ieee80211_tx_data *tx)
  582. {
  583. struct sk_buff *skb;
  584. ieee80211_tx_set_protected(tx);
  585. skb_queue_walk(&tx->skbs, skb) {
  586. if (gcmp_encrypt_skb(tx, skb) < 0)
  587. return TX_DROP;
  588. }
  589. return TX_CONTINUE;
  590. }
  591. ieee80211_rx_result
  592. ieee80211_crypto_gcmp_decrypt(struct ieee80211_rx_data *rx)
  593. {
  594. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  595. int hdrlen;
  596. struct ieee80211_key *key = rx->key;
  597. struct sk_buff *skb = rx->skb;
  598. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(skb);
  599. u8 pn[IEEE80211_GCMP_PN_LEN];
  600. int data_len, queue, mic_len = IEEE80211_GCMP_MIC_LEN;
  601. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  602. if (!ieee80211_is_data(hdr->frame_control) &&
  603. !ieee80211_is_robust_mgmt_frame(skb))
  604. return RX_CONTINUE;
  605. if (status->flag & RX_FLAG_DECRYPTED) {
  606. if (!pskb_may_pull(rx->skb, hdrlen + IEEE80211_GCMP_HDR_LEN))
  607. return RX_DROP_UNUSABLE;
  608. if (status->flag & RX_FLAG_MIC_STRIPPED)
  609. mic_len = 0;
  610. } else {
  611. if (skb_linearize(rx->skb))
  612. return RX_DROP_UNUSABLE;
  613. }
  614. data_len = skb->len - hdrlen - IEEE80211_GCMP_HDR_LEN - mic_len;
  615. if (!rx->sta || data_len < 0)
  616. return RX_DROP_UNUSABLE;
  617. if (!(status->flag & RX_FLAG_PN_VALIDATED)) {
  618. int res;
  619. gcmp_hdr2pn(pn, skb->data + hdrlen);
  620. queue = rx->security_idx;
  621. res = memcmp(pn, key->u.gcmp.rx_pn[queue],
  622. IEEE80211_GCMP_PN_LEN);
  623. if (res < 0 ||
  624. (!res && !(status->flag & RX_FLAG_ALLOW_SAME_PN))) {
  625. key->u.gcmp.replays++;
  626. return RX_DROP_UNUSABLE;
  627. }
  628. if (!(status->flag & RX_FLAG_DECRYPTED)) {
  629. u8 aad[2 * AES_BLOCK_SIZE];
  630. u8 j_0[AES_BLOCK_SIZE];
  631. /* hardware didn't decrypt/verify MIC */
  632. gcmp_special_blocks(skb, pn, j_0, aad);
  633. if (ieee80211_aes_gcm_decrypt(
  634. key->u.gcmp.tfm, j_0, aad,
  635. skb->data + hdrlen + IEEE80211_GCMP_HDR_LEN,
  636. data_len,
  637. skb->data + skb->len -
  638. IEEE80211_GCMP_MIC_LEN))
  639. return RX_DROP_UNUSABLE;
  640. }
  641. memcpy(key->u.gcmp.rx_pn[queue], pn, IEEE80211_GCMP_PN_LEN);
  642. }
  643. /* Remove GCMP header and MIC */
  644. if (pskb_trim(skb, skb->len - mic_len))
  645. return RX_DROP_UNUSABLE;
  646. memmove(skb->data + IEEE80211_GCMP_HDR_LEN, skb->data, hdrlen);
  647. skb_pull(skb, IEEE80211_GCMP_HDR_LEN);
  648. return RX_CONTINUE;
  649. }
  650. static ieee80211_tx_result
  651. ieee80211_crypto_cs_encrypt(struct ieee80211_tx_data *tx,
  652. struct sk_buff *skb)
  653. {
  654. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  655. struct ieee80211_key *key = tx->key;
  656. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  657. int hdrlen;
  658. u8 *pos, iv_len = key->conf.iv_len;
  659. if (info->control.hw_key &&
  660. !(info->control.hw_key->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE)) {
  661. /* hwaccel has no need for preallocated head room */
  662. return TX_CONTINUE;
  663. }
  664. if (unlikely(skb_headroom(skb) < iv_len &&
  665. pskb_expand_head(skb, iv_len, 0, GFP_ATOMIC)))
  666. return TX_DROP;
  667. hdrlen = ieee80211_hdrlen(hdr->frame_control);
  668. pos = skb_push(skb, iv_len);
  669. memmove(pos, pos + iv_len, hdrlen);
  670. return TX_CONTINUE;
  671. }
  672. static inline int ieee80211_crypto_cs_pn_compare(u8 *pn1, u8 *pn2, int len)
  673. {
  674. int i;
  675. /* pn is little endian */
  676. for (i = len - 1; i >= 0; i--) {
  677. if (pn1[i] < pn2[i])
  678. return -1;
  679. else if (pn1[i] > pn2[i])
  680. return 1;
  681. }
  682. return 0;
  683. }
  684. static ieee80211_rx_result
  685. ieee80211_crypto_cs_decrypt(struct ieee80211_rx_data *rx)
  686. {
  687. struct ieee80211_key *key = rx->key;
  688. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)rx->skb->data;
  689. const struct ieee80211_cipher_scheme *cs = NULL;
  690. int hdrlen = ieee80211_hdrlen(hdr->frame_control);
  691. struct ieee80211_rx_status *status = IEEE80211_SKB_RXCB(rx->skb);
  692. int data_len;
  693. u8 *rx_pn;
  694. u8 *skb_pn;
  695. u8 qos_tid;
  696. if (!rx->sta || !rx->sta->cipher_scheme ||
  697. !(status->flag & RX_FLAG_DECRYPTED))
  698. return RX_DROP_UNUSABLE;
  699. if (!ieee80211_is_data(hdr->frame_control))
  700. return RX_CONTINUE;
  701. cs = rx->sta->cipher_scheme;
  702. data_len = rx->skb->len - hdrlen - cs->hdr_len;
  703. if (data_len < 0)
  704. return RX_DROP_UNUSABLE;
  705. if (ieee80211_is_data_qos(hdr->frame_control))
  706. qos_tid = *ieee80211_get_qos_ctl(hdr) &
  707. IEEE80211_QOS_CTL_TID_MASK;
  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 = (struct ieee80211_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 = (struct ieee80211_mmie_16 *)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[20];
  907. u64 pn64;
  908. u8 nonce[12];
  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 = (struct ieee80211_mmie_16 *)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[20], mic[16], ipn[6], nonce[12];
  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. if (ieee80211_aes_gmac(key->u.aes_gmac.tfm, aad, nonce,
  964. skb->data + 24, skb->len - 24,
  965. mic) < 0 ||
  966. crypto_memneq(mic, mmie->mic, sizeof(mmie->mic))) {
  967. key->u.aes_gmac.icverrors++;
  968. return RX_DROP_UNUSABLE;
  969. }
  970. }
  971. memcpy(key->u.aes_gmac.rx_pn, ipn, 6);
  972. /* Remove MMIE */
  973. skb_trim(skb, skb->len - sizeof(*mmie));
  974. return RX_CONTINUE;
  975. }
  976. ieee80211_tx_result
  977. ieee80211_crypto_hw_encrypt(struct ieee80211_tx_data *tx)
  978. {
  979. struct sk_buff *skb;
  980. struct ieee80211_tx_info *info = NULL;
  981. ieee80211_tx_result res;
  982. skb_queue_walk(&tx->skbs, skb) {
  983. info = IEEE80211_SKB_CB(skb);
  984. /* handle hw-only algorithm */
  985. if (!info->control.hw_key)
  986. return TX_DROP;
  987. if (tx->key->flags & KEY_FLAG_CIPHER_SCHEME) {
  988. res = ieee80211_crypto_cs_encrypt(tx, skb);
  989. if (res != TX_CONTINUE)
  990. return res;
  991. }
  992. }
  993. ieee80211_tx_set_protected(tx);
  994. return TX_CONTINUE;
  995. }
  996. ieee80211_rx_result
  997. ieee80211_crypto_hw_decrypt(struct ieee80211_rx_data *rx)
  998. {
  999. if (rx->sta && rx->sta->cipher_scheme)
  1000. return ieee80211_crypto_cs_decrypt(rx);
  1001. return RX_DROP_UNUSABLE;
  1002. }