[MAC80211]: fix software decryption
[powerpc.git] / net / mac80211 / wpa.c
1 /*
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>
14 #include <net/iw_handler.h>
15
16 #include <net/mac80211.h>
17 #include "ieee80211_common.h"
18 #include "ieee80211_i.h"
19 #include "michael.h"
20 #include "tkip.h"
21 #include "aes_ccm.h"
22 #include "wpa.h"
23
24 static int ieee80211_get_hdr_info(const struct sk_buff *skb, u8 **sa, u8 **da,
25                                   u8 *qos_tid, u8 **data, size_t *data_len)
26 {
27         struct ieee80211_hdr *hdr;
28         size_t hdrlen;
29         u16 fc;
30         int a4_included;
31         u8 *pos;
32
33         hdr = (struct ieee80211_hdr *) skb->data;
34         fc = le16_to_cpu(hdr->frame_control);
35
36         hdrlen = 24;
37         if ((fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) ==
38             (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
39                 hdrlen += ETH_ALEN;
40                 *sa = hdr->addr4;
41                 *da = hdr->addr3;
42         } else if (fc & IEEE80211_FCTL_FROMDS) {
43                 *sa = hdr->addr3;
44                 *da = hdr->addr1;
45         } else if (fc & IEEE80211_FCTL_TODS) {
46                 *sa = hdr->addr2;
47                 *da = hdr->addr3;
48         } else {
49                 *sa = hdr->addr2;
50                 *da = hdr->addr1;
51         }
52
53         if (fc & 0x80)
54                 hdrlen += 2;
55
56         *data = skb->data + hdrlen;
57         *data_len = skb->len - hdrlen;
58
59         a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
60                 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
61         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
62             fc & IEEE80211_STYPE_QOS_DATA) {
63                 pos = (u8 *) &hdr->addr4;
64                 if (a4_included)
65                         pos += 6;
66                 *qos_tid = pos[0] & 0x0f;
67                 *qos_tid |= 0x80; /* qos_included flag */
68         } else
69                 *qos_tid = 0;
70
71         return skb->len < hdrlen ? -1 : 0;
72 }
73
74
75 ieee80211_txrx_result
76 ieee80211_tx_h_michael_mic_add(struct ieee80211_txrx_data *tx)
77 {
78         u8 *data, *sa, *da, *key, *mic, qos_tid;
79         size_t data_len;
80         u16 fc;
81         struct sk_buff *skb = tx->skb;
82         int authenticator;
83         int wpa_test = 0;
84
85         fc = tx->fc;
86
87         if (!tx->key || tx->key->alg != ALG_TKIP || skb->len < 24 ||
88             !WLAN_FC_DATA_PRESENT(fc))
89                 return TXRX_CONTINUE;
90
91         if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len))
92                 return TXRX_DROP;
93
94         if (!tx->key->force_sw_encrypt &&
95             !tx->fragmented &&
96             !(tx->local->hw.flags & IEEE80211_HW_TKIP_INCLUDE_MMIC) &&
97             !wpa_test) {
98                 /* hwaccel - with no need for preallocated room for Michael MIC
99                  */
100                 return TXRX_CONTINUE;
101         }
102
103         if (skb_tailroom(skb) < MICHAEL_MIC_LEN) {
104                 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
105                 if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN,
106                                               MICHAEL_MIC_LEN + TKIP_ICV_LEN,
107                                               GFP_ATOMIC))) {
108                         printk(KERN_DEBUG "%s: failed to allocate more memory "
109                                "for Michael MIC\n", tx->dev->name);
110                         return TXRX_DROP;
111                 }
112         }
113
114 #if 0
115         authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
116 #else
117         authenticator = 1;
118 #endif
119         key = &tx->key->key[authenticator ? ALG_TKIP_TEMP_AUTH_TX_MIC_KEY :
120                             ALG_TKIP_TEMP_AUTH_RX_MIC_KEY];
121         mic = skb_put(skb, MICHAEL_MIC_LEN);
122         michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
123
124         return TXRX_CONTINUE;
125 }
126
127
128 ieee80211_txrx_result
129 ieee80211_rx_h_michael_mic_verify(struct ieee80211_txrx_data *rx)
130 {
131         u8 *data, *sa, *da, *key = NULL, qos_tid;
132         size_t data_len;
133         u16 fc;
134         u8 mic[MICHAEL_MIC_LEN];
135         struct sk_buff *skb = rx->skb;
136         int authenticator = 1, wpa_test = 0;
137
138         fc = rx->fc;
139
140         /*
141          * No way to verify the MIC if the hardware stripped it
142          */
143         if (rx->local->hw.flags & IEEE80211_HW_DEVICE_STRIPS_MIC)
144                 return TXRX_CONTINUE;
145
146         if (!rx->key || rx->key->alg != ALG_TKIP ||
147             !(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc))
148                 return TXRX_CONTINUE;
149
150         if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
151             !rx->key->force_sw_encrypt) {
152                 if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
153                         if (skb->len < MICHAEL_MIC_LEN)
154                                 return TXRX_DROP;
155                 }
156                 /* Need to verify Michael MIC sometimes in software even when
157                  * hwaccel is used. Atheros ar5212: fragmented frames and QoS
158                  * frames. */
159                 if (!rx->fragmented && !wpa_test)
160                         goto remove_mic;
161         }
162
163         if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len)
164             || data_len < MICHAEL_MIC_LEN)
165                 return TXRX_DROP;
166
167         data_len -= MICHAEL_MIC_LEN;
168
169 #if 0
170         authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
171 #else
172         authenticator = 1;
173 #endif
174         key = &rx->key->key[authenticator ? ALG_TKIP_TEMP_AUTH_RX_MIC_KEY :
175                             ALG_TKIP_TEMP_AUTH_TX_MIC_KEY];
176         michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
177         if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
178                 if (!rx->u.rx.ra_match)
179                         return TXRX_DROP;
180
181                 printk(KERN_DEBUG "%s: invalid Michael MIC in data frame from "
182                        MAC_FMT "\n", rx->dev->name, MAC_ARG(sa));
183
184                 do {
185                         struct ieee80211_hdr *hdr;
186                         union iwreq_data wrqu;
187                         char *buf = kmalloc(128, GFP_ATOMIC);
188                         if (!buf)
189                                 break;
190
191                         /* TODO: needed parameters: count, key type, TSC */
192                         hdr = (struct ieee80211_hdr *) skb->data;
193                         sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
194                                 "keyid=%d %scast addr=" MAC_FMT ")",
195                                 rx->key->keyidx,
196                                 hdr->addr1[0] & 0x01 ? "broad" : "uni",
197                                 MAC_ARG(hdr->addr2));
198                         memset(&wrqu, 0, sizeof(wrqu));
199                         wrqu.data.length = strlen(buf);
200                         wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
201                         kfree(buf);
202                 } while (0);
203
204                 if (!rx->local->apdev)
205                         return TXRX_DROP;
206
207                 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
208                                   ieee80211_msg_michael_mic_failure);
209
210                 return TXRX_QUEUED;
211         }
212
213  remove_mic:
214         /* remove Michael MIC from payload */
215         skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
216
217         return TXRX_CONTINUE;
218 }
219
220
221 static int tkip_encrypt_skb(struct ieee80211_txrx_data *tx,
222                             struct sk_buff *skb, int test)
223 {
224         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
225         struct ieee80211_key *key = tx->key;
226         int hdrlen, len, tailneed;
227         u16 fc;
228         u8 *pos;
229
230         fc = le16_to_cpu(hdr->frame_control);
231         hdrlen = ieee80211_get_hdrlen(fc);
232         len = skb->len - hdrlen;
233
234         tailneed = !tx->key->force_sw_encrypt ? 0 : TKIP_ICV_LEN;
235         if ((skb_headroom(skb) < TKIP_IV_LEN ||
236              skb_tailroom(skb) < tailneed)) {
237                 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
238                 if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN, tailneed,
239                                               GFP_ATOMIC)))
240                         return -1;
241         }
242
243         pos = skb_push(skb, TKIP_IV_LEN);
244         memmove(pos, pos + TKIP_IV_LEN, hdrlen);
245         pos += hdrlen;
246
247         /* Increase IV for the frame */
248         key->u.tkip.iv16++;
249         if (key->u.tkip.iv16 == 0)
250                 key->u.tkip.iv32++;
251
252         if (!tx->key->force_sw_encrypt) {
253                 u32 flags = tx->local->hw.flags;
254                 hdr = (struct ieee80211_hdr *)skb->data;
255
256                 /* hwaccel - with preallocated room for IV */
257                 ieee80211_tkip_add_iv(pos, key,
258                                       (u8) (key->u.tkip.iv16 >> 8),
259                                       (u8) (((key->u.tkip.iv16 >> 8) | 0x20) &
260                                             0x7f),
261                                       (u8) key->u.tkip.iv16);
262
263                 if (flags & IEEE80211_HW_TKIP_REQ_PHASE2_KEY)
264                         ieee80211_tkip_gen_rc4key(key, hdr->addr2,
265                                                   tx->u.tx.control->tkip_key);
266                 else if (flags & IEEE80211_HW_TKIP_REQ_PHASE1_KEY) {
267                         if (key->u.tkip.iv16 == 0 ||
268                             !key->u.tkip.tx_initialized) {
269                                 ieee80211_tkip_gen_phase1key(key, hdr->addr2,
270                                             (u16 *)tx->u.tx.control->tkip_key);
271                                 key->u.tkip.tx_initialized = 1;
272                                 tx->u.tx.control->flags |=
273                                             IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY;
274                         } else
275                                 tx->u.tx.control->flags &=
276                                             ~IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY;
277                 }
278
279                 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
280                 return 0;
281         }
282
283         /* Add room for ICV */
284         skb_put(skb, TKIP_ICV_LEN);
285
286         hdr = (struct ieee80211_hdr *) skb->data;
287         ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
288                                     key, pos, len, hdr->addr2);
289         return 0;
290 }
291
292
293 ieee80211_txrx_result
294 ieee80211_tx_h_tkip_encrypt(struct ieee80211_txrx_data *tx)
295 {
296         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
297         u16 fc;
298         struct ieee80211_key *key = tx->key;
299         struct sk_buff *skb = tx->skb;
300         int wpa_test = 0, test = 0;
301
302         fc = le16_to_cpu(hdr->frame_control);
303
304         if (!key || key->alg != ALG_TKIP || !WLAN_FC_DATA_PRESENT(fc))
305                 return TXRX_CONTINUE;
306
307         tx->u.tx.control->icv_len = TKIP_ICV_LEN;
308         tx->u.tx.control->iv_len = TKIP_IV_LEN;
309         ieee80211_tx_set_iswep(tx);
310
311         if (!tx->key->force_sw_encrypt &&
312             !(tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
313             !wpa_test) {
314                 /* hwaccel - with no need for preallocated room for IV/ICV */
315                 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
316                 return TXRX_CONTINUE;
317         }
318
319         if (tkip_encrypt_skb(tx, skb, test) < 0)
320                 return TXRX_DROP;
321
322         if (tx->u.tx.extra_frag) {
323                 int i;
324                 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
325                         if (tkip_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
326                             < 0)
327                                 return TXRX_DROP;
328                 }
329         }
330
331         return TXRX_CONTINUE;
332 }
333
334
335 ieee80211_txrx_result
336 ieee80211_rx_h_tkip_decrypt(struct ieee80211_txrx_data *rx)
337 {
338         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
339         u16 fc;
340         int hdrlen, res, hwaccel = 0, wpa_test = 0;
341         struct ieee80211_key *key = rx->key;
342         struct sk_buff *skb = rx->skb;
343
344         fc = le16_to_cpu(hdr->frame_control);
345         hdrlen = ieee80211_get_hdrlen(fc);
346
347         if (!rx->key || rx->key->alg != ALG_TKIP ||
348             !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
349             (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
350                 return TXRX_CONTINUE;
351
352         if (!rx->sta || skb->len - hdrlen < 12)
353                 return TXRX_DROP;
354
355         if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
356             !rx->key->force_sw_encrypt) {
357                 if (!(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV)) {
358                         /* Hardware takes care of all processing, including
359                          * replay protection, so no need to continue here. */
360                         return TXRX_CONTINUE;
361                 }
362
363                 /* let TKIP code verify IV, but skip decryption */
364                 hwaccel = 1;
365         }
366
367         res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
368                                           key, skb->data + hdrlen,
369                                           skb->len - hdrlen, rx->sta->addr,
370                                           hwaccel, rx->u.rx.queue);
371         if (res != TKIP_DECRYPT_OK || wpa_test) {
372                 printk(KERN_DEBUG "%s: TKIP decrypt failed for RX frame from "
373                        MAC_FMT " (res=%d)\n",
374                        rx->dev->name, MAC_ARG(rx->sta->addr), res);
375                 return TXRX_DROP;
376         }
377
378         /* Trim ICV */
379         skb_trim(skb, skb->len - TKIP_ICV_LEN);
380
381         /* Remove IV */
382         memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
383         skb_pull(skb, TKIP_IV_LEN);
384
385         return TXRX_CONTINUE;
386 }
387
388
389 static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
390                                 int encrypted)
391 {
392         u16 fc;
393         int a4_included, qos_included;
394         u8 qos_tid, *fc_pos, *data, *sa, *da;
395         int len_a;
396         size_t data_len;
397         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
398
399         fc_pos = (u8 *) &hdr->frame_control;
400         fc = fc_pos[0] ^ (fc_pos[1] << 8);
401         a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
402                 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
403
404         ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len);
405         data_len -= CCMP_HDR_LEN + (encrypted ? CCMP_MIC_LEN : 0);
406         if (qos_tid & 0x80) {
407                 qos_included = 1;
408                 qos_tid &= 0x0f;
409         } else
410                 qos_included = 0;
411         /* First block, b_0 */
412
413         b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
414         /* Nonce: QoS Priority | A2 | PN */
415         b_0[1] = qos_tid;
416         memcpy(&b_0[2], hdr->addr2, 6);
417         memcpy(&b_0[8], pn, CCMP_PN_LEN);
418         /* l(m) */
419         b_0[14] = (data_len >> 8) & 0xff;
420         b_0[15] = data_len & 0xff;
421
422
423         /* AAD (extra authenticate-only data) / masked 802.11 header
424          * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
425
426         len_a = a4_included ? 28 : 22;
427         if (qos_included)
428                 len_a += 2;
429
430         aad[0] = 0; /* (len_a >> 8) & 0xff; */
431         aad[1] = len_a & 0xff;
432         /* Mask FC: zero subtype b4 b5 b6 */
433         aad[2] = fc_pos[0] & ~(BIT(4) | BIT(5) | BIT(6));
434         /* Retry, PwrMgt, MoreData; set Protected */
435         aad[3] = (fc_pos[1] & ~(BIT(3) | BIT(4) | BIT(5))) | BIT(6);
436         memcpy(&aad[4], &hdr->addr1, 18);
437
438         /* Mask Seq#, leave Frag# */
439         aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
440         aad[23] = 0;
441         if (a4_included) {
442                 memcpy(&aad[24], hdr->addr4, 6);
443                 aad[30] = 0;
444                 aad[31] = 0;
445         } else
446                 memset(&aad[24], 0, 8);
447         if (qos_included) {
448                 u8 *dpos = &aad[a4_included ? 30 : 24];
449
450                 /* Mask QoS Control field */
451                 dpos[0] = qos_tid;
452                 dpos[1] = 0;
453         }
454 }
455
456
457 static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
458 {
459         hdr[0] = pn[5];
460         hdr[1] = pn[4];
461         hdr[2] = 0;
462         hdr[3] = 0x20 | (key_id << 6);
463         hdr[4] = pn[3];
464         hdr[5] = pn[2];
465         hdr[6] = pn[1];
466         hdr[7] = pn[0];
467 }
468
469
470 static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
471 {
472         pn[0] = hdr[7];
473         pn[1] = hdr[6];
474         pn[2] = hdr[5];
475         pn[3] = hdr[4];
476         pn[4] = hdr[1];
477         pn[5] = hdr[0];
478         return (hdr[3] >> 6) & 0x03;
479 }
480
481
482 static int ccmp_encrypt_skb(struct ieee80211_txrx_data *tx,
483                             struct sk_buff *skb, int test)
484 {
485         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
486         struct ieee80211_key *key = tx->key;
487         int hdrlen, len, tailneed;
488         u16 fc;
489         u8 *pos, *pn, *b_0, *aad, *scratch;
490         int i;
491
492         scratch = key->u.ccmp.tx_crypto_buf;
493         b_0 = scratch + 3 * AES_BLOCK_LEN;
494         aad = scratch + 4 * AES_BLOCK_LEN;
495
496         fc = le16_to_cpu(hdr->frame_control);
497         hdrlen = ieee80211_get_hdrlen(fc);
498         len = skb->len - hdrlen;
499
500         tailneed = !key->force_sw_encrypt ? 0 : CCMP_MIC_LEN;
501
502         if ((skb_headroom(skb) < CCMP_HDR_LEN ||
503              skb_tailroom(skb) < tailneed)) {
504                 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
505                 if (unlikely(pskb_expand_head(skb, CCMP_HDR_LEN, tailneed,
506                                               GFP_ATOMIC)))
507                         return -1;
508         }
509
510         pos = skb_push(skb, CCMP_HDR_LEN);
511         memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
512         hdr = (struct ieee80211_hdr *) pos;
513         pos += hdrlen;
514
515         /* PN = PN + 1 */
516         pn = key->u.ccmp.tx_pn;
517
518         for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
519                 pn[i]++;
520                 if (pn[i])
521                         break;
522         }
523
524         ccmp_pn2hdr(pos, pn, key->keyidx);
525
526         if (!key->force_sw_encrypt) {
527                 /* hwaccel - with preallocated room for CCMP header */
528                 tx->u.tx.control->key_idx = key->hw_key_idx;
529                 return 0;
530         }
531
532         pos += CCMP_HDR_LEN;
533         ccmp_special_blocks(skb, pn, b_0, aad, 0);
534         ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
535                                   pos, skb_put(skb, CCMP_MIC_LEN));
536
537         return 0;
538 }
539
540
541 ieee80211_txrx_result
542 ieee80211_tx_h_ccmp_encrypt(struct ieee80211_txrx_data *tx)
543 {
544         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
545         struct ieee80211_key *key = tx->key;
546         u16 fc;
547         struct sk_buff *skb = tx->skb;
548         int test = 0;
549
550         fc = le16_to_cpu(hdr->frame_control);
551
552         if (!key || key->alg != ALG_CCMP || !WLAN_FC_DATA_PRESENT(fc))
553                 return TXRX_CONTINUE;
554
555         tx->u.tx.control->icv_len = CCMP_MIC_LEN;
556         tx->u.tx.control->iv_len = CCMP_HDR_LEN;
557         ieee80211_tx_set_iswep(tx);
558
559         if (!tx->key->force_sw_encrypt &&
560             !(tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV)) {
561                 /* hwaccel - with no need for preallocated room for CCMP "
562                  * header or MIC fields */
563                 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
564                 return TXRX_CONTINUE;
565         }
566
567         if (ccmp_encrypt_skb(tx, skb, test) < 0)
568                 return TXRX_DROP;
569
570         if (tx->u.tx.extra_frag) {
571                 int i;
572
573                 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
574                         if (ccmp_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
575                             < 0)
576                                 return TXRX_DROP;
577                 }
578         }
579
580         return TXRX_CONTINUE;
581 }
582
583
584 ieee80211_txrx_result
585 ieee80211_rx_h_ccmp_decrypt(struct ieee80211_txrx_data *rx)
586 {
587         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
588         u16 fc;
589         int hdrlen;
590         struct ieee80211_key *key = rx->key;
591         struct sk_buff *skb = rx->skb;
592         u8 pn[CCMP_PN_LEN];
593         int data_len;
594
595         fc = le16_to_cpu(hdr->frame_control);
596         hdrlen = ieee80211_get_hdrlen(fc);
597
598         if (!key || key->alg != ALG_CCMP ||
599             !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
600             (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
601                 return TXRX_CONTINUE;
602
603         data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
604         if (!rx->sta || data_len < 0)
605                 return TXRX_DROP;
606
607         if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
608             !key->force_sw_encrypt &&
609             !(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV))
610                 return TXRX_CONTINUE;
611
612         (void) ccmp_hdr2pn(pn, skb->data + hdrlen);
613
614         if (memcmp(pn, key->u.ccmp.rx_pn[rx->u.rx.queue], CCMP_PN_LEN) <= 0) {
615 #ifdef CONFIG_MAC80211_DEBUG
616                 u8 *ppn = key->u.ccmp.rx_pn[rx->u.rx.queue];
617                 printk(KERN_DEBUG "%s: CCMP replay detected for RX frame from "
618                        MAC_FMT " (RX PN %02x%02x%02x%02x%02x%02x <= prev. PN "
619                        "%02x%02x%02x%02x%02x%02x)\n", rx->dev->name,
620                        MAC_ARG(rx->sta->addr),
621                        pn[0], pn[1], pn[2], pn[3], pn[4], pn[5],
622                        ppn[0], ppn[1], ppn[2], ppn[3], ppn[4], ppn[5]);
623 #endif /* CONFIG_MAC80211_DEBUG */
624                 key->u.ccmp.replays++;
625                 return TXRX_DROP;
626         }
627
628         if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
629             !key->force_sw_encrypt) {
630                 /* hwaccel has already decrypted frame and verified MIC */
631         } else {
632                 u8 *scratch, *b_0, *aad;
633
634                 scratch = key->u.ccmp.rx_crypto_buf;
635                 b_0 = scratch + 3 * AES_BLOCK_LEN;
636                 aad = scratch + 4 * AES_BLOCK_LEN;
637
638                 ccmp_special_blocks(skb, pn, b_0, aad, 1);
639
640                 if (ieee80211_aes_ccm_decrypt(
641                             key->u.ccmp.tfm, scratch, b_0, aad,
642                             skb->data + hdrlen + CCMP_HDR_LEN, data_len,
643                             skb->data + skb->len - CCMP_MIC_LEN,
644                             skb->data + hdrlen + CCMP_HDR_LEN)) {
645                         printk(KERN_DEBUG "%s: CCMP decrypt failed for RX "
646                                "frame from " MAC_FMT "\n", rx->dev->name,
647                                MAC_ARG(rx->sta->addr));
648                         return TXRX_DROP;
649                 }
650         }
651
652         memcpy(key->u.ccmp.rx_pn[rx->u.rx.queue], pn, CCMP_PN_LEN);
653
654         /* Remove CCMP header and MIC */
655         skb_trim(skb, skb->len - CCMP_MIC_LEN);
656         memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
657         skb_pull(skb, CCMP_HDR_LEN);
658
659         return TXRX_CONTINUE;
660 }
661