wpa.c 31 KB

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