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