blob: 360d11e9de153b09104c460e3ce28812a2b45fcc [file] [log] [blame]
Jiri Bencf0706e82007-05-05 11:45:53 -07001/*
2 * Copyright 2002-2004, Instant802 Networks, Inc.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include <linux/netdevice.h>
10#include <linux/types.h>
11#include <linux/slab.h>
12#include <linux/skbuff.h>
13#include <linux/compiler.h>
Jiri Bencf0706e82007-05-05 11:45:53 -070014#include <net/mac80211.h>
Johannes Bergeb063c12007-08-28 17:01:53 -040015
Jiri Bencf0706e82007-05-05 11:45:53 -070016#include "ieee80211_i.h"
17#include "michael.h"
18#include "tkip.h"
19#include "aes_ccm.h"
20#include "wpa.h"
21
22static int ieee80211_get_hdr_info(const struct sk_buff *skb, u8 **sa, u8 **da,
23 u8 *qos_tid, u8 **data, size_t *data_len)
24{
25 struct ieee80211_hdr *hdr;
26 size_t hdrlen;
27 u16 fc;
28 int a4_included;
29 u8 *pos;
30
31 hdr = (struct ieee80211_hdr *) skb->data;
32 fc = le16_to_cpu(hdr->frame_control);
33
34 hdrlen = 24;
35 if ((fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) ==
36 (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
37 hdrlen += ETH_ALEN;
38 *sa = hdr->addr4;
39 *da = hdr->addr3;
40 } else if (fc & IEEE80211_FCTL_FROMDS) {
41 *sa = hdr->addr3;
42 *da = hdr->addr1;
43 } else if (fc & IEEE80211_FCTL_TODS) {
44 *sa = hdr->addr2;
45 *da = hdr->addr3;
46 } else {
47 *sa = hdr->addr2;
48 *da = hdr->addr1;
49 }
50
51 if (fc & 0x80)
52 hdrlen += 2;
53
54 *data = skb->data + hdrlen;
55 *data_len = skb->len - hdrlen;
56
57 a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
58 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
59 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
60 fc & IEEE80211_STYPE_QOS_DATA) {
61 pos = (u8 *) &hdr->addr4;
62 if (a4_included)
63 pos += 6;
64 *qos_tid = pos[0] & 0x0f;
65 *qos_tid |= 0x80; /* qos_included flag */
66 } else
67 *qos_tid = 0;
68
69 return skb->len < hdrlen ? -1 : 0;
70}
71
72
73ieee80211_txrx_result
74ieee80211_tx_h_michael_mic_add(struct ieee80211_txrx_data *tx)
75{
76 u8 *data, *sa, *da, *key, *mic, qos_tid;
77 size_t data_len;
78 u16 fc;
79 struct sk_buff *skb = tx->skb;
80 int authenticator;
81 int wpa_test = 0;
82
83 fc = tx->fc;
84
Johannes Berg8f20fc22007-08-28 17:01:54 -040085 if (!tx->key || tx->key->conf.alg != ALG_TKIP || skb->len < 24 ||
Jiri Bencf0706e82007-05-05 11:45:53 -070086 !WLAN_FC_DATA_PRESENT(fc))
87 return TXRX_CONTINUE;
88
89 if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len))
90 return TXRX_DROP;
91
Johannes Berg11a843b2007-08-28 17:01:55 -040092 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
Jiri Slabybadffb72007-08-28 17:01:54 -040093 !(tx->flags & IEEE80211_TXRXD_FRAGMENTED) &&
Johannes Berg7848ba72007-09-14 11:10:25 -040094 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) &&
Jiri Bencf0706e82007-05-05 11:45:53 -070095 !wpa_test) {
96 /* hwaccel - with no need for preallocated room for Michael MIC
97 */
98 return TXRX_CONTINUE;
99 }
100
101 if (skb_tailroom(skb) < MICHAEL_MIC_LEN) {
102 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
103 if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN,
104 MICHAEL_MIC_LEN + TKIP_ICV_LEN,
105 GFP_ATOMIC))) {
106 printk(KERN_DEBUG "%s: failed to allocate more memory "
107 "for Michael MIC\n", tx->dev->name);
108 return TXRX_DROP;
109 }
110 }
111
112#if 0
113 authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
114#else
115 authenticator = 1;
116#endif
Johannes Berg8f20fc22007-08-28 17:01:54 -0400117 key = &tx->key->conf.key[authenticator ? ALG_TKIP_TEMP_AUTH_TX_MIC_KEY :
118 ALG_TKIP_TEMP_AUTH_RX_MIC_KEY];
Jiri Bencf0706e82007-05-05 11:45:53 -0700119 mic = skb_put(skb, MICHAEL_MIC_LEN);
120 michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
121
122 return TXRX_CONTINUE;
123}
124
125
126ieee80211_txrx_result
127ieee80211_rx_h_michael_mic_verify(struct ieee80211_txrx_data *rx)
128{
129 u8 *data, *sa, *da, *key = NULL, qos_tid;
130 size_t data_len;
131 u16 fc;
132 u8 mic[MICHAEL_MIC_LEN];
133 struct sk_buff *skb = rx->skb;
134 int authenticator = 1, wpa_test = 0;
Joe Perches0795af52007-10-03 17:59:30 -0700135 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700136
137 fc = rx->fc;
138
Johannes Berg3017b802007-08-28 17:01:53 -0400139 /*
140 * No way to verify the MIC if the hardware stripped it
141 */
Johannes Berg7848ba72007-09-14 11:10:25 -0400142 if (rx->u.rx.status->flag & RX_FLAG_MMIC_STRIPPED)
Jiri Bencf0706e82007-05-05 11:45:53 -0700143 return TXRX_CONTINUE;
144
Johannes Berg8f20fc22007-08-28 17:01:54 -0400145 if (!rx->key || rx->key->conf.alg != ALG_TKIP ||
Jiri Bencf0706e82007-05-05 11:45:53 -0700146 !(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc))
147 return TXRX_CONTINUE;
148
Jiri Bencf0706e82007-05-05 11:45:53 -0700149 if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len)
150 || data_len < MICHAEL_MIC_LEN)
151 return TXRX_DROP;
152
153 data_len -= MICHAEL_MIC_LEN;
154
155#if 0
156 authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
157#else
158 authenticator = 1;
159#endif
Johannes Berg8f20fc22007-08-28 17:01:54 -0400160 key = &rx->key->conf.key[authenticator ? ALG_TKIP_TEMP_AUTH_RX_MIC_KEY :
161 ALG_TKIP_TEMP_AUTH_TX_MIC_KEY];
Jiri Bencf0706e82007-05-05 11:45:53 -0700162 michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
163 if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
Jiri Slabybadffb72007-08-28 17:01:54 -0400164 if (!(rx->flags & IEEE80211_TXRXD_RXRA_MATCH))
Jiri Bencf0706e82007-05-05 11:45:53 -0700165 return TXRX_DROP;
166
167 printk(KERN_DEBUG "%s: invalid Michael MIC in data frame from "
Joe Perches0795af52007-10-03 17:59:30 -0700168 "%s\n", rx->dev->name, print_mac(mac, sa));
Jiri Bencf0706e82007-05-05 11:45:53 -0700169
Johannes Berg8f20fc22007-08-28 17:01:54 -0400170 mac80211_ev_michael_mic_failure(rx->dev, rx->key->conf.keyidx,
Johannes Bergeb063c12007-08-28 17:01:53 -0400171 (void *) skb->data);
172 return TXRX_DROP;
Jiri Bencf0706e82007-05-05 11:45:53 -0700173 }
174
Jiri Bencf0706e82007-05-05 11:45:53 -0700175 /* remove Michael MIC from payload */
176 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
177
178 return TXRX_CONTINUE;
179}
180
181
182static int tkip_encrypt_skb(struct ieee80211_txrx_data *tx,
183 struct sk_buff *skb, int test)
184{
185 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
186 struct ieee80211_key *key = tx->key;
187 int hdrlen, len, tailneed;
188 u16 fc;
189 u8 *pos;
190
191 fc = le16_to_cpu(hdr->frame_control);
192 hdrlen = ieee80211_get_hdrlen(fc);
193 len = skb->len - hdrlen;
194
Johannes Berg11a843b2007-08-28 17:01:55 -0400195 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg8f20fc22007-08-28 17:01:54 -0400196 tailneed = 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400197 else
198 tailneed = TKIP_ICV_LEN;
Johannes Berg8f20fc22007-08-28 17:01:54 -0400199
Jiri Bencf0706e82007-05-05 11:45:53 -0700200 if ((skb_headroom(skb) < TKIP_IV_LEN ||
201 skb_tailroom(skb) < tailneed)) {
202 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
203 if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN, tailneed,
204 GFP_ATOMIC)))
205 return -1;
206 }
207
208 pos = skb_push(skb, TKIP_IV_LEN);
209 memmove(pos, pos + TKIP_IV_LEN, hdrlen);
210 pos += hdrlen;
211
212 /* Increase IV for the frame */
213 key->u.tkip.iv16++;
214 if (key->u.tkip.iv16 == 0)
215 key->u.tkip.iv32++;
216
Johannes Berg11a843b2007-08-28 17:01:55 -0400217 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700218 hdr = (struct ieee80211_hdr *)skb->data;
219
220 /* hwaccel - with preallocated room for IV */
221 ieee80211_tkip_add_iv(pos, key,
222 (u8) (key->u.tkip.iv16 >> 8),
223 (u8) (((key->u.tkip.iv16 >> 8) | 0x20) &
224 0x7f),
225 (u8) key->u.tkip.iv16);
226
Johannes Berg8f20fc22007-08-28 17:01:54 -0400227 tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -0700228 return 0;
229 }
230
231 /* Add room for ICV */
232 skb_put(skb, TKIP_ICV_LEN);
233
234 hdr = (struct ieee80211_hdr *) skb->data;
235 ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
236 key, pos, len, hdr->addr2);
237 return 0;
238}
239
240
241ieee80211_txrx_result
242ieee80211_tx_h_tkip_encrypt(struct ieee80211_txrx_data *tx)
243{
244 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
245 u16 fc;
246 struct ieee80211_key *key = tx->key;
247 struct sk_buff *skb = tx->skb;
248 int wpa_test = 0, test = 0;
249
250 fc = le16_to_cpu(hdr->frame_control);
251
Johannes Berg8f20fc22007-08-28 17:01:54 -0400252 if (!key || key->conf.alg != ALG_TKIP || !WLAN_FC_DATA_PRESENT(fc))
Jiri Bencf0706e82007-05-05 11:45:53 -0700253 return TXRX_CONTINUE;
254
255 tx->u.tx.control->icv_len = TKIP_ICV_LEN;
256 tx->u.tx.control->iv_len = TKIP_IV_LEN;
257 ieee80211_tx_set_iswep(tx);
258
Johannes Berg11a843b2007-08-28 17:01:55 -0400259 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
Johannes Berg7848ba72007-09-14 11:10:25 -0400260 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
Jiri Bencf0706e82007-05-05 11:45:53 -0700261 !wpa_test) {
262 /* hwaccel - with no need for preallocated room for IV/ICV */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400263 tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -0700264 return TXRX_CONTINUE;
265 }
266
267 if (tkip_encrypt_skb(tx, skb, test) < 0)
268 return TXRX_DROP;
269
270 if (tx->u.tx.extra_frag) {
271 int i;
272 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
273 if (tkip_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
274 < 0)
275 return TXRX_DROP;
276 }
277 }
278
279 return TXRX_CONTINUE;
280}
281
282
283ieee80211_txrx_result
284ieee80211_rx_h_tkip_decrypt(struct ieee80211_txrx_data *rx)
285{
286 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
287 u16 fc;
288 int hdrlen, res, hwaccel = 0, wpa_test = 0;
289 struct ieee80211_key *key = rx->key;
290 struct sk_buff *skb = rx->skb;
Joe Perches0795af52007-10-03 17:59:30 -0700291 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700292
293 fc = le16_to_cpu(hdr->frame_control);
294 hdrlen = ieee80211_get_hdrlen(fc);
295
Johannes Berg8f20fc22007-08-28 17:01:54 -0400296 if (!rx->key || rx->key->conf.alg != ALG_TKIP ||
Jiri Bencf0706e82007-05-05 11:45:53 -0700297 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
298 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
299 return TXRX_CONTINUE;
300
301 if (!rx->sta || skb->len - hdrlen < 12)
302 return TXRX_DROP;
303
Johannes Berg7848ba72007-09-14 11:10:25 -0400304 if (rx->u.rx.status->flag & RX_FLAG_DECRYPTED) {
305 if (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED) {
306 /*
307 * Hardware took care of all processing, including
308 * replay protection, and stripped the ICV/IV so
309 * we cannot do any checks here.
310 */
Jiri Bencf0706e82007-05-05 11:45:53 -0700311 return TXRX_CONTINUE;
312 }
313
314 /* let TKIP code verify IV, but skip decryption */
315 hwaccel = 1;
316 }
317
318 res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
319 key, skb->data + hdrlen,
320 skb->len - hdrlen, rx->sta->addr,
321 hwaccel, rx->u.rx.queue);
322 if (res != TKIP_DECRYPT_OK || wpa_test) {
323 printk(KERN_DEBUG "%s: TKIP decrypt failed for RX frame from "
Joe Perches0795af52007-10-03 17:59:30 -0700324 "%s (res=%d)\n",
325 rx->dev->name, print_mac(mac, rx->sta->addr), res);
Jiri Bencf0706e82007-05-05 11:45:53 -0700326 return TXRX_DROP;
327 }
328
329 /* Trim ICV */
330 skb_trim(skb, skb->len - TKIP_ICV_LEN);
331
332 /* Remove IV */
333 memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
334 skb_pull(skb, TKIP_IV_LEN);
335
336 return TXRX_CONTINUE;
337}
338
339
340static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
341 int encrypted)
342{
343 u16 fc;
344 int a4_included, qos_included;
345 u8 qos_tid, *fc_pos, *data, *sa, *da;
346 int len_a;
347 size_t data_len;
348 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
349
350 fc_pos = (u8 *) &hdr->frame_control;
351 fc = fc_pos[0] ^ (fc_pos[1] << 8);
352 a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
353 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
354
355 ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len);
356 data_len -= CCMP_HDR_LEN + (encrypted ? CCMP_MIC_LEN : 0);
357 if (qos_tid & 0x80) {
358 qos_included = 1;
359 qos_tid &= 0x0f;
360 } else
361 qos_included = 0;
362 /* First block, b_0 */
363
364 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
365 /* Nonce: QoS Priority | A2 | PN */
366 b_0[1] = qos_tid;
367 memcpy(&b_0[2], hdr->addr2, 6);
368 memcpy(&b_0[8], pn, CCMP_PN_LEN);
369 /* l(m) */
370 b_0[14] = (data_len >> 8) & 0xff;
371 b_0[15] = data_len & 0xff;
372
373
374 /* AAD (extra authenticate-only data) / masked 802.11 header
375 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
376
377 len_a = a4_included ? 28 : 22;
378 if (qos_included)
379 len_a += 2;
380
381 aad[0] = 0; /* (len_a >> 8) & 0xff; */
382 aad[1] = len_a & 0xff;
383 /* Mask FC: zero subtype b4 b5 b6 */
384 aad[2] = fc_pos[0] & ~(BIT(4) | BIT(5) | BIT(6));
385 /* Retry, PwrMgt, MoreData; set Protected */
386 aad[3] = (fc_pos[1] & ~(BIT(3) | BIT(4) | BIT(5))) | BIT(6);
387 memcpy(&aad[4], &hdr->addr1, 18);
388
389 /* Mask Seq#, leave Frag# */
390 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
391 aad[23] = 0;
392 if (a4_included) {
393 memcpy(&aad[24], hdr->addr4, 6);
394 aad[30] = 0;
395 aad[31] = 0;
396 } else
397 memset(&aad[24], 0, 8);
398 if (qos_included) {
399 u8 *dpos = &aad[a4_included ? 30 : 24];
400
401 /* Mask QoS Control field */
402 dpos[0] = qos_tid;
403 dpos[1] = 0;
404 }
405}
406
407
408static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
409{
410 hdr[0] = pn[5];
411 hdr[1] = pn[4];
412 hdr[2] = 0;
413 hdr[3] = 0x20 | (key_id << 6);
414 hdr[4] = pn[3];
415 hdr[5] = pn[2];
416 hdr[6] = pn[1];
417 hdr[7] = pn[0];
418}
419
420
421static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
422{
423 pn[0] = hdr[7];
424 pn[1] = hdr[6];
425 pn[2] = hdr[5];
426 pn[3] = hdr[4];
427 pn[4] = hdr[1];
428 pn[5] = hdr[0];
429 return (hdr[3] >> 6) & 0x03;
430}
431
432
433static int ccmp_encrypt_skb(struct ieee80211_txrx_data *tx,
434 struct sk_buff *skb, int test)
435{
436 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
437 struct ieee80211_key *key = tx->key;
438 int hdrlen, len, tailneed;
439 u16 fc;
440 u8 *pos, *pn, *b_0, *aad, *scratch;
441 int i;
442
443 scratch = key->u.ccmp.tx_crypto_buf;
444 b_0 = scratch + 3 * AES_BLOCK_LEN;
445 aad = scratch + 4 * AES_BLOCK_LEN;
446
447 fc = le16_to_cpu(hdr->frame_control);
448 hdrlen = ieee80211_get_hdrlen(fc);
449 len = skb->len - hdrlen;
450
Johannes Berg11a843b2007-08-28 17:01:55 -0400451 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
Johannes Berg8f20fc22007-08-28 17:01:54 -0400452 tailneed = 0;
Johannes Berg11a843b2007-08-28 17:01:55 -0400453 else
454 tailneed = CCMP_MIC_LEN;
Jiri Bencf0706e82007-05-05 11:45:53 -0700455
456 if ((skb_headroom(skb) < CCMP_HDR_LEN ||
457 skb_tailroom(skb) < tailneed)) {
458 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
459 if (unlikely(pskb_expand_head(skb, CCMP_HDR_LEN, tailneed,
460 GFP_ATOMIC)))
461 return -1;
462 }
463
464 pos = skb_push(skb, CCMP_HDR_LEN);
465 memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
466 hdr = (struct ieee80211_hdr *) pos;
467 pos += hdrlen;
468
469 /* PN = PN + 1 */
470 pn = key->u.ccmp.tx_pn;
471
472 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
473 pn[i]++;
474 if (pn[i])
475 break;
476 }
477
Johannes Berg8f20fc22007-08-28 17:01:54 -0400478 ccmp_pn2hdr(pos, pn, key->conf.keyidx);
Jiri Bencf0706e82007-05-05 11:45:53 -0700479
Johannes Berg11a843b2007-08-28 17:01:55 -0400480 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700481 /* hwaccel - with preallocated room for CCMP header */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400482 tx->u.tx.control->key_idx = key->conf.hw_key_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -0700483 return 0;
484 }
485
486 pos += CCMP_HDR_LEN;
487 ccmp_special_blocks(skb, pn, b_0, aad, 0);
488 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
489 pos, skb_put(skb, CCMP_MIC_LEN));
490
491 return 0;
492}
493
494
495ieee80211_txrx_result
496ieee80211_tx_h_ccmp_encrypt(struct ieee80211_txrx_data *tx)
497{
498 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
499 struct ieee80211_key *key = tx->key;
500 u16 fc;
501 struct sk_buff *skb = tx->skb;
502 int test = 0;
503
504 fc = le16_to_cpu(hdr->frame_control);
505
Johannes Berg8f20fc22007-08-28 17:01:54 -0400506 if (!key || key->conf.alg != ALG_CCMP || !WLAN_FC_DATA_PRESENT(fc))
Jiri Bencf0706e82007-05-05 11:45:53 -0700507 return TXRX_CONTINUE;
508
509 tx->u.tx.control->icv_len = CCMP_MIC_LEN;
510 tx->u.tx.control->iv_len = CCMP_HDR_LEN;
511 ieee80211_tx_set_iswep(tx);
512
Johannes Berg11a843b2007-08-28 17:01:55 -0400513 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
Johannes Berg7848ba72007-09-14 11:10:25 -0400514 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
Jiri Bencf0706e82007-05-05 11:45:53 -0700515 /* hwaccel - with no need for preallocated room for CCMP "
516 * header or MIC fields */
Johannes Berg8f20fc22007-08-28 17:01:54 -0400517 tx->u.tx.control->key_idx = tx->key->conf.hw_key_idx;
Jiri Bencf0706e82007-05-05 11:45:53 -0700518 return TXRX_CONTINUE;
519 }
520
521 if (ccmp_encrypt_skb(tx, skb, test) < 0)
522 return TXRX_DROP;
523
524 if (tx->u.tx.extra_frag) {
525 int i;
Jiri Bencf0706e82007-05-05 11:45:53 -0700526 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
527 if (ccmp_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
528 < 0)
529 return TXRX_DROP;
530 }
531 }
532
533 return TXRX_CONTINUE;
534}
535
536
537ieee80211_txrx_result
538ieee80211_rx_h_ccmp_decrypt(struct ieee80211_txrx_data *rx)
539{
540 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
541 u16 fc;
542 int hdrlen;
543 struct ieee80211_key *key = rx->key;
544 struct sk_buff *skb = rx->skb;
545 u8 pn[CCMP_PN_LEN];
546 int data_len;
Joe Perches0795af52007-10-03 17:59:30 -0700547 DECLARE_MAC_BUF(mac);
Jiri Bencf0706e82007-05-05 11:45:53 -0700548
549 fc = le16_to_cpu(hdr->frame_control);
550 hdrlen = ieee80211_get_hdrlen(fc);
551
Johannes Berg8f20fc22007-08-28 17:01:54 -0400552 if (!key || key->conf.alg != ALG_CCMP ||
Jiri Bencf0706e82007-05-05 11:45:53 -0700553 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
554 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
555 return TXRX_CONTINUE;
556
557 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
558 if (!rx->sta || data_len < 0)
559 return TXRX_DROP;
560
561 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
Johannes Berg7848ba72007-09-14 11:10:25 -0400562 (rx->u.rx.status->flag & RX_FLAG_IV_STRIPPED))
Jiri Bencf0706e82007-05-05 11:45:53 -0700563 return TXRX_CONTINUE;
564
565 (void) ccmp_hdr2pn(pn, skb->data + hdrlen);
566
567 if (memcmp(pn, key->u.ccmp.rx_pn[rx->u.rx.queue], CCMP_PN_LEN) <= 0) {
568#ifdef CONFIG_MAC80211_DEBUG
569 u8 *ppn = key->u.ccmp.rx_pn[rx->u.rx.queue];
Joe Perches0795af52007-10-03 17:59:30 -0700570
Jiri Bencf0706e82007-05-05 11:45:53 -0700571 printk(KERN_DEBUG "%s: CCMP replay detected for RX frame from "
Joe Perches0795af52007-10-03 17:59:30 -0700572 "%s (RX PN %02x%02x%02x%02x%02x%02x <= prev. PN "
Jiri Bencf0706e82007-05-05 11:45:53 -0700573 "%02x%02x%02x%02x%02x%02x)\n", rx->dev->name,
Joe Perches0795af52007-10-03 17:59:30 -0700574 print_mac(mac, rx->sta->addr),
Jiri Bencf0706e82007-05-05 11:45:53 -0700575 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5],
576 ppn[0], ppn[1], ppn[2], ppn[3], ppn[4], ppn[5]);
577#endif /* CONFIG_MAC80211_DEBUG */
578 key->u.ccmp.replays++;
579 return TXRX_DROP;
580 }
581
Johannes Berg7848ba72007-09-14 11:10:25 -0400582 if (!(rx->u.rx.status->flag & RX_FLAG_DECRYPTED)) {
583 /* hardware didn't decrypt/verify MIC */
Jiri Bencf0706e82007-05-05 11:45:53 -0700584 u8 *scratch, *b_0, *aad;
585
586 scratch = key->u.ccmp.rx_crypto_buf;
587 b_0 = scratch + 3 * AES_BLOCK_LEN;
588 aad = scratch + 4 * AES_BLOCK_LEN;
589
590 ccmp_special_blocks(skb, pn, b_0, aad, 1);
591
592 if (ieee80211_aes_ccm_decrypt(
593 key->u.ccmp.tfm, scratch, b_0, aad,
594 skb->data + hdrlen + CCMP_HDR_LEN, data_len,
595 skb->data + skb->len - CCMP_MIC_LEN,
596 skb->data + hdrlen + CCMP_HDR_LEN)) {
597 printk(KERN_DEBUG "%s: CCMP decrypt failed for RX "
Joe Perches0795af52007-10-03 17:59:30 -0700598 "frame from %s\n", rx->dev->name,
599 print_mac(mac, rx->sta->addr));
Jiri Bencf0706e82007-05-05 11:45:53 -0700600 return TXRX_DROP;
601 }
602 }
603
604 memcpy(key->u.ccmp.rx_pn[rx->u.rx.queue], pn, CCMP_PN_LEN);
605
606 /* Remove CCMP header and MIC */
607 skb_trim(skb, skb->len - CCMP_MIC_LEN);
608 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
609 skb_pull(skb, CCMP_HDR_LEN);
610
611 return TXRX_CONTINUE;
612}