wpa.c 31 KB

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