2 * Copyright 2002-2004, Instant802 Networks, Inc.
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.
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>
16 #include <net/d80211.h>
17 #include <net/d80211_common.h>
18 #include "ieee80211_i.h"
23 #ifdef CONFIG_HOSTAPD_WPA_TESTING
24 #include "hostapd_ioctl.h"
25 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
28 int ieee80211_get_hdr_info(const struct sk_buff
*skb
, u8
**sa
, u8
**da
,
29 u8
*qos_tid
, u8
**data
, size_t *data_len
)
31 struct ieee80211_hdr
*hdr
;
37 hdr
= (struct ieee80211_hdr
*) skb
->data
;
38 fc
= le16_to_cpu(hdr
->frame_control
);
41 if ((fc
& (IEEE80211_FCTL_FROMDS
| IEEE80211_FCTL_TODS
)) ==
42 (IEEE80211_FCTL_FROMDS
| IEEE80211_FCTL_TODS
)) {
46 } else if (fc
& IEEE80211_FCTL_FROMDS
) {
49 } else if (fc
& IEEE80211_FCTL_TODS
) {
60 *data
= skb
->data
+ hdrlen
;
61 *data_len
= skb
->len
- hdrlen
;
63 a4_included
= (fc
& (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) ==
64 (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
);
65 if ((fc
& IEEE80211_FCTL_FTYPE
) == IEEE80211_FTYPE_DATA
&&
66 fc
& IEEE80211_STYPE_QOS_DATA
) {
67 pos
= (u8
*) &hdr
->addr4
;
70 *qos_tid
= pos
[0] & 0x0f;
71 *qos_tid
|= 0x80; /* qos_included flag */
75 return skb
->len
< hdrlen
? -1 : 0;
80 ieee80211_tx_h_michael_mic_add(struct ieee80211_txrx_data
*tx
)
82 u8
*data
, *sa
, *da
, *key
, *mic
, qos_tid
;
85 struct sk_buff
*skb
= tx
->skb
;
91 if (!tx
->key
|| tx
->key
->alg
!= ALG_TKIP
|| skb
->len
< 24 ||
92 !WLAN_FC_DATA_PRESENT(fc
))
95 if (ieee80211_get_hdr_info(skb
, &sa
, &da
, &qos_tid
, &data
, &data_len
))
98 #ifdef CONFIG_HOSTAPD_WPA_TESTING
99 if ((tx
->sta
&& tx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_MIC
) ||
100 (!tx
->u
.tx
.unicast
&&
101 tx
->local
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_MIC
)) {
104 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
106 if (!tx
->key
->force_sw_encrypt
&&
108 !(tx
->local
->hw
.flags
& IEEE80211_HW_TKIP_INCLUDE_MMIC
) &&
110 /* hwaccel - with no need for preallocated room for Michael MIC
112 return TXRX_CONTINUE
;
115 if (skb_tailroom(skb
) < MICHAEL_MIC_LEN
) {
116 I802_DEBUG_INC(tx
->local
->tx_expand_skb_head
);
117 if (unlikely(pskb_expand_head(skb
, TKIP_IV_LEN
,
118 MICHAEL_MIC_LEN
+ TKIP_ICV_LEN
,
120 printk(KERN_DEBUG
"%s: failed to allocate more memory "
121 "for Michael MIC\n", tx
->dev
->name
);
127 authenticator
= fc
& IEEE80211_FCTL_FROMDS
; /* FIX */
131 key
= &tx
->key
->key
[authenticator
? ALG_TKIP_TEMP_AUTH_TX_MIC_KEY
:
132 ALG_TKIP_TEMP_AUTH_RX_MIC_KEY
];
133 mic
= skb_put(skb
, MICHAEL_MIC_LEN
);
134 michael_mic(key
, da
, sa
, qos_tid
& 0x0f, data
, data_len
, mic
);
136 #ifdef CONFIG_HOSTAPD_WPA_TESTING
137 if (tx
->sta
&& tx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_MIC
) {
138 printk(KERN_INFO
"%s: WPA testing - corrupting TX Michael MIC "
139 "for STA " MAC_FMT
"\n",
140 tx
->dev
->name
, MAC_ARG(tx
->sta
->addr
));
141 tx
->u
.tx
.control
->key_idx
= HW_KEY_IDX_INVALID
;
142 tx
->sta
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_TX_MIC
;
145 } else if (!tx
->u
.tx
.unicast
&&
146 tx
->local
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_MIC
) {
147 printk(KERN_INFO
"%s: WPA testing - corrupting TX Michael MIC "
148 "for Group Key\n", tx
->dev
->name
);
149 tx
->u
.tx
.control
->key_idx
= HW_KEY_IDX_INVALID
;
150 tx
->local
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_TX_MIC
;
154 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
156 return TXRX_CONTINUE
;
160 ieee80211_txrx_result
161 ieee80211_rx_h_michael_mic_verify(struct ieee80211_txrx_data
*rx
)
163 u8
*data
, *sa
, *da
, *key
= NULL
, qos_tid
;
166 u8 mic
[MICHAEL_MIC_LEN
];
167 struct sk_buff
*skb
= rx
->skb
;
168 int authenticator
= 1, wpa_test
= 0;
172 /* If device handles decryption totally, skip this check */
173 if ((rx
->local
->hw
.flags
& IEEE80211_HW_DEVICE_HIDES_WEP
) ||
174 (rx
->local
->hw
.flags
& IEEE80211_HW_DEVICE_STRIPS_MIC
))
175 return TXRX_CONTINUE
;
177 if (!rx
->key
|| rx
->key
->alg
!= ALG_TKIP
||
178 !(rx
->fc
& IEEE80211_FCTL_PROTECTED
) || !WLAN_FC_DATA_PRESENT(fc
))
179 return TXRX_CONTINUE
;
181 #ifdef CONFIG_HOSTAPD_WPA_TESTING
182 if (rx
->sta
&& rx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_RX_MIC
) {
185 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
187 if ((rx
->u
.rx
.status
->flag
& RX_FLAG_DECRYPTED
) &&
188 !rx
->key
->force_sw_encrypt
) {
189 if (rx
->local
->hw
.flags
& IEEE80211_HW_WEP_INCLUDE_IV
) {
190 if (skb
->len
< MICHAEL_MIC_LEN
)
193 /* Need to verify Michael MIC sometimes in software even when
194 * hwaccel is used. Atheros ar5212: fragmented frames and QoS
196 if (!rx
->fragmented
&& !wpa_test
)
200 if (ieee80211_get_hdr_info(skb
, &sa
, &da
, &qos_tid
, &data
, &data_len
)
201 || data_len
< MICHAEL_MIC_LEN
)
204 data_len
-= MICHAEL_MIC_LEN
;
207 authenticator
= fc
& IEEE80211_FCTL_TODS
; /* FIX */
211 key
= &rx
->key
->key
[authenticator
? ALG_TKIP_TEMP_AUTH_RX_MIC_KEY
:
212 ALG_TKIP_TEMP_AUTH_TX_MIC_KEY
];
213 michael_mic(key
, da
, sa
, qos_tid
& 0x0f, data
, data_len
, mic
);
214 #ifdef CONFIG_HOSTAPD_WPA_TESTING
215 if (rx
->sta
&& rx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_RX_MIC
) {
216 printk(KERN_INFO
"%s: WPA testing - corrupting RX Michael MIC "
217 "for STA " MAC_FMT
"\n",
218 rx
->dev
->name
, MAC_ARG(rx
->sta
->addr
));
219 rx
->sta
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_RX_MIC
;
222 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
223 if (memcmp(mic
, data
+ data_len
, MICHAEL_MIC_LEN
) != 0 || wpa_test
) {
224 #ifdef CONFIG_HOSTAPD_WPA_TESTING
226 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
228 if (!rx
->u
.rx
.ra_match
)
231 printk(KERN_DEBUG
"%s: invalid Michael MIC in data frame from "
232 MAC_FMT
"\n", rx
->dev
->name
, MAC_ARG(sa
));
233 #ifdef CONFIG_HOSTAPD_WPA_TESTING
234 printk(KERN_DEBUG
" received");
235 for (i
= 0; i
< MICHAEL_MIC_LEN
; i
++)
236 printk(" %02x", data
[data_len
+ i
]);
238 for (i
= 0; i
< MICHAEL_MIC_LEN
; i
++)
239 printk(" %02x", mic
[i
]);
241 printk(KERN_DEBUG
" SA=" MAC_FMT
" DA=" MAC_FMT
" key",
242 MAC_ARG(sa
), MAC_ARG(da
));
243 for (i
= 0; i
< 8; i
++)
244 printk(" %02x", key
[i
]);
245 printk(" (%d)\n", authenticator
);
246 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
249 struct ieee80211_hdr
*hdr
;
250 union iwreq_data wrqu
;
251 char *buf
= kmalloc(128, GFP_ATOMIC
);
255 /* TODO: needed parameters: count, key type, TSC */
256 hdr
= (struct ieee80211_hdr
*) skb
->data
;
257 sprintf(buf
, "MLME-MICHAELMICFAILURE.indication("
258 "keyid=%d %scast addr=" MAC_FMT
")",
260 hdr
->addr1
[0] & 0x01 ? "broad" : "uni",
261 MAC_ARG(hdr
->addr2
));
262 memset(&wrqu
, 0, sizeof(wrqu
));
263 wrqu
.data
.length
= strlen(buf
);
264 wireless_send_event(rx
->dev
, IWEVCUSTOM
, &wrqu
, buf
);
268 if (!rx
->local
->apdev
)
271 ieee80211_rx_mgmt(rx
->local
, rx
->skb
, rx
->u
.rx
.status
,
272 ieee80211_msg_michael_mic_failure
);
278 /* remove Michael MIC from payload */
279 skb_trim(skb
, skb
->len
- MICHAEL_MIC_LEN
);
281 return TXRX_CONTINUE
;
285 static int tkip_encrypt_skb(struct ieee80211_txrx_data
*tx
,
286 struct sk_buff
*skb
, int test
)
288 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
289 struct ieee80211_key
*key
= tx
->key
;
290 int hdrlen
, len
, tailneed
;
294 fc
= le16_to_cpu(hdr
->frame_control
);
295 hdrlen
= ieee80211_get_hdrlen(fc
);
296 len
= skb
->len
- hdrlen
;
298 tailneed
= !tx
->key
->force_sw_encrypt
? 0 : TKIP_ICV_LEN
;
299 if ((skb_headroom(skb
) < TKIP_IV_LEN
||
300 skb_tailroom(skb
) < tailneed
)) {
301 I802_DEBUG_INC(tx
->local
->tx_expand_skb_head
);
302 if (unlikely(pskb_expand_head(skb
, TKIP_IV_LEN
, tailneed
,
307 pos
= skb_push(skb
, TKIP_IV_LEN
);
308 memmove(pos
, pos
+ TKIP_IV_LEN
, hdrlen
);
311 #ifdef CONFIG_HOSTAPD_WPA_TESTING
312 if (test
& WPA_TRIGGER_TX_REPLAY
)
315 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
317 /* Increase IV for the frame */
319 if (key
->u
.tkip
.iv16
== 0)
322 #ifdef CONFIG_HOSTAPD_WPA_TESTING
323 if (test
& WPA_TRIGGER_TX_SKIP_SEQ
) {
328 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
330 if (!tx
->key
->force_sw_encrypt
331 #ifdef CONFIG_HOSTAPD_WPA_TESTING
333 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
335 u32 flags
= tx
->local
->hw
.flags
;
336 hdr
= (struct ieee80211_hdr
*)skb
->data
;
338 /* hwaccel - with preallocated room for IV */
339 ieee80211_tkip_add_iv(pos
, key
,
340 (u8
) (key
->u
.tkip
.iv16
>> 8),
341 (u8
) (((key
->u
.tkip
.iv16
>> 8) | 0x20) &
343 (u8
) key
->u
.tkip
.iv16
);
345 if (flags
& IEEE80211_HW_TKIP_REQ_PHASE2_KEY
)
346 ieee80211_tkip_gen_rc4key(key
, hdr
->addr2
,
347 tx
->u
.tx
.control
->tkip_key
);
348 else if (flags
& IEEE80211_HW_TKIP_REQ_PHASE1_KEY
) {
349 if (key
->u
.tkip
.iv16
== 0 ||
350 !key
->u
.tkip
.tx_initialized
) {
351 ieee80211_tkip_gen_phase1key(key
, hdr
->addr2
,
352 (u16
*)tx
->u
.tx
.control
->tkip_key
);
353 key
->u
.tkip
.tx_initialized
= 1;
354 tx
->u
.tx
.control
->flags
|=
355 IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY
;
357 tx
->u
.tx
.control
->flags
&=
358 ~IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY
;
361 tx
->u
.tx
.control
->key_idx
= tx
->key
->hw_key_idx
;
365 /* Add room for ICV */
366 skb_put(skb
, TKIP_ICV_LEN
);
368 hdr
= (struct ieee80211_hdr
*) skb
->data
;
369 ieee80211_tkip_encrypt_data(tx
->local
->wep_tx_tfm
,
370 key
, pos
, len
, hdr
->addr2
);
375 ieee80211_txrx_result
376 ieee80211_tx_h_tkip_encrypt(struct ieee80211_txrx_data
*tx
)
378 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) tx
->skb
->data
;
380 struct ieee80211_key
*key
= tx
->key
;
381 struct sk_buff
*skb
= tx
->skb
;
382 int wpa_test
= 0, test
= 0;
384 fc
= le16_to_cpu(hdr
->frame_control
);
386 if (!key
|| key
->alg
!= ALG_TKIP
|| !WLAN_FC_DATA_PRESENT(fc
))
387 return TXRX_CONTINUE
;
389 tx
->u
.tx
.control
->icv_len
= TKIP_ICV_LEN
;
390 tx
->u
.tx
.control
->iv_len
= TKIP_IV_LEN
;
391 ieee80211_tx_set_iswep(tx
);
393 #ifdef CONFIG_HOSTAPD_WPA_TESTING
394 if ((tx
->sta
&& tx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_ICV
) ||
395 (!tx
->u
.tx
.unicast
&&
396 tx
->local
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_ICV
)) {
401 test
= tx
->sta
->wpa_trigger
;
402 tx
->sta
->wpa_trigger
&=
403 ~(WPA_TRIGGER_TX_REPLAY
| WPA_TRIGGER_TX_REPLAY_FRAG
|
404 WPA_TRIGGER_TX_SKIP_SEQ
);
406 test
= tx
->local
->wpa_trigger
;
407 tx
->local
->wpa_trigger
&=
408 ~(WPA_TRIGGER_TX_REPLAY
| WPA_TRIGGER_TX_REPLAY_FRAG
|
409 WPA_TRIGGER_TX_SKIP_SEQ
);
412 (WPA_TRIGGER_TX_REPLAY
| WPA_TRIGGER_TX_REPLAY_FRAG
|
413 WPA_TRIGGER_TX_SKIP_SEQ
)) {
414 printk(KERN_INFO
"%s: WPA testing - TKIP TX packet number "
415 "%s%s%s%s\n", tx
->dev
->name
,
416 tx
->sta
? "[UNICAST]" : "[MULTICAST]",
417 test
& WPA_TRIGGER_TX_REPLAY
? "[REPLAY]" : "",
418 test
& WPA_TRIGGER_TX_REPLAY_FRAG
?
419 "[REPLAY FRAG]" : "",
420 test
& WPA_TRIGGER_TX_SKIP_SEQ
? "[SKIP SEQ]" : "");
422 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
424 if (!tx
->key
->force_sw_encrypt
&&
425 !(tx
->local
->hw
.flags
& IEEE80211_HW_WEP_INCLUDE_IV
) &&
427 /* hwaccel - with no need for preallocated room for IV/ICV */
428 tx
->u
.tx
.control
->key_idx
= tx
->key
->hw_key_idx
;
429 return TXRX_CONTINUE
;
432 if (tkip_encrypt_skb(tx
, skb
, test
) < 0)
435 if (tx
->u
.tx
.extra_frag
) {
437 #ifdef CONFIG_HOSTAPD_WPA_TESTING
438 if (test
& WPA_TRIGGER_TX_REPLAY_FRAG
)
439 test
|= WPA_TRIGGER_TX_REPLAY
;
440 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
441 for (i
= 0; i
< tx
->u
.tx
.num_extra_frag
; i
++) {
442 if (tkip_encrypt_skb(tx
, tx
->u
.tx
.extra_frag
[i
], test
)
448 #ifdef CONFIG_HOSTAPD_WPA_TESTING
449 if (tx
->sta
&& tx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_ICV
) {
450 printk(KERN_INFO
"%s: WPA testing - corrupting TX TKIP ICV "
451 "for STA " MAC_FMT
"\n",
452 tx
->dev
->name
, MAC_ARG(tx
->sta
->addr
));
453 tx
->u
.tx
.control
->key_idx
= HW_KEY_IDX_INVALID
;
454 tx
->sta
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_TX_ICV
;
455 skb
->data
[skb
->len
- 1]++;
456 } else if (!tx
->u
.tx
.unicast
&&
457 tx
->local
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_ICV
) {
458 printk(KERN_INFO
"%s: WPA testing - corrupting TX TKIP ICV "
461 tx
->u
.tx
.control
->key_idx
= HW_KEY_IDX_INVALID
;
462 tx
->local
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_TX_ICV
;
463 skb
->data
[skb
->len
- 1]++;
465 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
467 return TXRX_CONTINUE
;
471 ieee80211_txrx_result
472 ieee80211_rx_h_tkip_decrypt(struct ieee80211_txrx_data
*rx
)
474 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) rx
->skb
->data
;
476 int hdrlen
, res
, hwaccel
= 0, wpa_test
= 0;
477 struct ieee80211_key
*key
= rx
->key
;
478 struct sk_buff
*skb
= rx
->skb
;
480 fc
= le16_to_cpu(hdr
->frame_control
);
481 hdrlen
= ieee80211_get_hdrlen(fc
);
483 if (!rx
->key
|| rx
->key
->alg
!= ALG_TKIP
||
484 !(rx
->fc
& IEEE80211_FCTL_PROTECTED
) ||
485 (rx
->fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
)
486 return TXRX_CONTINUE
;
488 if (!rx
->sta
|| skb
->len
- hdrlen
< 12)
491 #ifdef CONFIG_HOSTAPD_WPA_TESTING
492 if (rx
->sta
&& rx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_RX_ICV
) {
493 printk(KERN_INFO
"%s: WPA testing - corrupting RX TKIP ICV "
494 "for STA " MAC_FMT
"\n",
495 rx
->dev
->name
, MAC_ARG(rx
->sta
->addr
));
496 rx
->sta
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_RX_ICV
;
497 skb
->data
[skb
->len
- 1]++;
500 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
502 if ((rx
->u
.rx
.status
->flag
& RX_FLAG_DECRYPTED
) &&
503 !rx
->key
->force_sw_encrypt
) {
504 if (!(rx
->local
->hw
.flags
& IEEE80211_HW_WEP_INCLUDE_IV
)) {
505 /* Hardware takes care of all processing, including
506 * replay protection, so no need to continue here. */
507 return TXRX_CONTINUE
;
510 /* let TKIP code verify IV, but skip decryption */
514 res
= ieee80211_tkip_decrypt_data(rx
->local
->wep_rx_tfm
,
515 key
, skb
->data
+ hdrlen
,
516 skb
->len
- hdrlen
, rx
->sta
->addr
,
517 hwaccel
, rx
->u
.rx
.queue
);
518 if (res
!= TKIP_DECRYPT_OK
|| wpa_test
) {
519 printk(KERN_DEBUG
"%s: TKIP decrypt failed for RX frame from "
520 MAC_FMT
" (res=%d)\n",
521 rx
->dev
->name
, MAC_ARG(rx
->sta
->addr
), res
);
526 skb_trim(skb
, skb
->len
- TKIP_ICV_LEN
);
529 memmove(skb
->data
+ TKIP_IV_LEN
, skb
->data
, hdrlen
);
530 skb_pull(skb
, TKIP_IV_LEN
);
532 return TXRX_CONTINUE
;
536 static void ccmp_special_blocks(struct sk_buff
*skb
, u8
*pn
, u8
*b_0
, u8
*aad
,
540 int a4_included
, qos_included
;
541 u8 qos_tid
, *fc_pos
, *data
, *sa
, *da
;
544 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
546 fc_pos
= (u8
*) &hdr
->frame_control
;
547 fc
= fc_pos
[0] ^ (fc_pos
[1] << 8);
548 a4_included
= (fc
& (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
)) ==
549 (IEEE80211_FCTL_TODS
| IEEE80211_FCTL_FROMDS
);
551 ieee80211_get_hdr_info(skb
, &sa
, &da
, &qos_tid
, &data
, &data_len
);
552 data_len
-= CCMP_HDR_LEN
+ (encrypted
? CCMP_MIC_LEN
: 0);
553 if (qos_tid
& 0x80) {
558 /* First block, b_0 */
560 b_0
[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
561 /* Nonce: QoS Priority | A2 | PN */
563 memcpy(&b_0
[2], hdr
->addr2
, 6);
564 memcpy(&b_0
[8], pn
, CCMP_PN_LEN
);
566 b_0
[14] = (data_len
>> 8) & 0xff;
567 b_0
[15] = data_len
& 0xff;
570 /* AAD (extra authenticate-only data) / masked 802.11 header
571 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
573 len_a
= a4_included
? 28 : 22;
577 aad
[0] = 0; /* (len_a >> 8) & 0xff; */
578 aad
[1] = len_a
& 0xff;
579 /* Mask FC: zero subtype b4 b5 b6 */
580 aad
[2] = fc_pos
[0] & ~(BIT(4) | BIT(5) | BIT(6));
581 /* Retry, PwrMgt, MoreData; set Protected */
582 aad
[3] = (fc_pos
[1] & ~(BIT(3) | BIT(4) | BIT(5))) | BIT(6);
583 memcpy(&aad
[4], &hdr
->addr1
, 18);
585 /* Mask Seq#, leave Frag# */
586 aad
[22] = *((u8
*) &hdr
->seq_ctrl
) & 0x0f;
589 memcpy(&aad
[24], hdr
->addr4
, 6);
593 memset(&aad
[24], 0, 8);
595 u8
*dpos
= &aad
[a4_included
? 30 : 24];
597 /* Mask QoS Control field */
604 static inline void ccmp_pn2hdr(u8
*hdr
, u8
*pn
, int key_id
)
609 hdr
[3] = 0x20 | (key_id
<< 6);
617 static inline int ccmp_hdr2pn(u8
*pn
, u8
*hdr
)
625 return (hdr
[3] >> 6) & 0x03;
629 static int ccmp_encrypt_skb(struct ieee80211_txrx_data
*tx
,
630 struct sk_buff
*skb
, int test
)
632 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
633 struct ieee80211_key
*key
= tx
->key
;
634 int hdrlen
, len
, tailneed
;
636 u8
*pos
, *pn
, *b_0
, *aad
, *scratch
;
639 scratch
= key
->u
.ccmp
.tx_crypto_buf
;
640 b_0
= scratch
+ 3 * AES_BLOCK_LEN
;
641 aad
= scratch
+ 4 * AES_BLOCK_LEN
;
643 fc
= le16_to_cpu(hdr
->frame_control
);
644 hdrlen
= ieee80211_get_hdrlen(fc
);
645 len
= skb
->len
- hdrlen
;
647 tailneed
= !key
->force_sw_encrypt
? 0 : CCMP_MIC_LEN
;
649 if ((skb_headroom(skb
) < CCMP_HDR_LEN
||
650 skb_tailroom(skb
) < tailneed
)) {
651 I802_DEBUG_INC(tx
->local
->tx_expand_skb_head
);
652 if (unlikely(pskb_expand_head(skb
, CCMP_HDR_LEN
, tailneed
,
657 pos
= skb_push(skb
, CCMP_HDR_LEN
);
658 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdrlen
);
659 hdr
= (struct ieee80211_hdr
*) pos
;
663 pn
= key
->u
.ccmp
.tx_pn
;
665 #ifdef CONFIG_HOSTAPD_WPA_TESTING
666 if (test
& WPA_TRIGGER_TX_REPLAY
)
669 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
671 for (i
= CCMP_PN_LEN
- 1; i
>= 0; i
--) {
677 #ifdef CONFIG_HOSTAPD_WPA_TESTING
678 if (test
& WPA_TRIGGER_TX_SKIP_SEQ
) {
683 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
685 ccmp_pn2hdr(pos
, pn
, key
->keyidx
);
687 if (!key
->force_sw_encrypt
) {
688 /* hwaccel - with preallocated room for CCMP header */
689 tx
->u
.tx
.control
->key_idx
= key
->hw_key_idx
;
694 ccmp_special_blocks(skb
, pn
, b_0
, aad
, 0);
695 ieee80211_aes_ccm_encrypt(key
->u
.ccmp
.tfm
, scratch
, b_0
, aad
, pos
, len
,
696 pos
, skb_put(skb
, CCMP_MIC_LEN
));
702 ieee80211_txrx_result
703 ieee80211_tx_h_ccmp_encrypt(struct ieee80211_txrx_data
*tx
)
705 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) tx
->skb
->data
;
706 struct ieee80211_key
*key
= tx
->key
;
708 struct sk_buff
*skb
= tx
->skb
;
711 fc
= le16_to_cpu(hdr
->frame_control
);
713 if (!key
|| key
->alg
!= ALG_CCMP
|| !WLAN_FC_DATA_PRESENT(fc
))
714 return TXRX_CONTINUE
;
716 #ifdef CONFIG_HOSTAPD_WPA_TESTING
718 test
= tx
->sta
->wpa_trigger
;
719 tx
->sta
->wpa_trigger
= 0;
721 test
= tx
->local
->wpa_trigger
;
722 tx
->local
->wpa_trigger
= 0;
725 (WPA_TRIGGER_TX_REPLAY
| WPA_TRIGGER_TX_REPLAY_FRAG
|
726 WPA_TRIGGER_TX_SKIP_SEQ
)) {
727 printk(KERN_INFO
"%s: WPA testing - CCMP TX packet number "
728 "%s%s%s%s\n", tx
->dev
->name
,
729 tx
->sta
? "[UNICAST]" : "[MULTICAST]",
730 test
& WPA_TRIGGER_TX_REPLAY
? "[REPLAY]" : "",
731 test
& WPA_TRIGGER_TX_REPLAY_FRAG
?
732 "[REPLAY FRAG]" : "",
733 test
& WPA_TRIGGER_TX_SKIP_SEQ
? "[SKIP SEQ]" : "");
735 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
737 tx
->u
.tx
.control
->icv_len
= CCMP_MIC_LEN
;
738 tx
->u
.tx
.control
->iv_len
= CCMP_HDR_LEN
;
739 ieee80211_tx_set_iswep(tx
);
741 if (!tx
->key
->force_sw_encrypt
&&
742 !(tx
->local
->hw
.flags
& IEEE80211_HW_WEP_INCLUDE_IV
)) {
743 /* hwaccel - with no need for preallocated room for CCMP "
744 * header or MIC fields */
745 tx
->u
.tx
.control
->key_idx
= tx
->key
->hw_key_idx
;
746 return TXRX_CONTINUE
;
749 if (ccmp_encrypt_skb(tx
, skb
, test
) < 0)
752 if (tx
->u
.tx
.extra_frag
) {
754 #ifdef CONFIG_HOSTAPD_WPA_TESTING
755 if (test
& WPA_TRIGGER_TX_REPLAY_FRAG
)
756 test
|= WPA_TRIGGER_TX_REPLAY
;
757 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
758 for (i
= 0; i
< tx
->u
.tx
.num_extra_frag
; i
++) {
759 if (ccmp_encrypt_skb(tx
, tx
->u
.tx
.extra_frag
[i
], test
)
765 return TXRX_CONTINUE
;
769 ieee80211_txrx_result
770 ieee80211_rx_h_ccmp_decrypt(struct ieee80211_txrx_data
*rx
)
772 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) rx
->skb
->data
;
775 struct ieee80211_key
*key
= rx
->key
;
776 struct sk_buff
*skb
= rx
->skb
;
780 fc
= le16_to_cpu(hdr
->frame_control
);
781 hdrlen
= ieee80211_get_hdrlen(fc
);
783 if (!key
|| key
->alg
!= ALG_CCMP
||
784 !(rx
->fc
& IEEE80211_FCTL_PROTECTED
) ||
785 (rx
->fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
)
786 return TXRX_CONTINUE
;
788 data_len
= skb
->len
- hdrlen
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
789 if (!rx
->sta
|| data_len
< 0)
792 if ((rx
->u
.rx
.status
->flag
& RX_FLAG_DECRYPTED
) &&
793 !key
->force_sw_encrypt
&&
794 !(rx
->local
->hw
.flags
& IEEE80211_HW_WEP_INCLUDE_IV
))
795 return TXRX_CONTINUE
;
797 (void) ccmp_hdr2pn(pn
, skb
->data
+ hdrlen
);
799 if (memcmp(pn
, key
->u
.ccmp
.rx_pn
[rx
->u
.rx
.queue
], CCMP_PN_LEN
) <= 0) {
800 #ifdef CONFIG_D80211_DEBUG
801 u8
*ppn
= key
->u
.ccmp
.rx_pn
[rx
->u
.rx
.queue
];
802 printk(KERN_DEBUG
"%s: CCMP replay detected for RX frame from "
803 MAC_FMT
" (RX PN %02x%02x%02x%02x%02x%02x <= prev. PN "
804 "%02x%02x%02x%02x%02x%02x)\n", rx
->dev
->name
,
805 MAC_ARG(rx
->sta
->addr
),
806 pn
[0], pn
[1], pn
[2], pn
[3], pn
[4], pn
[5],
807 ppn
[0], ppn
[1], ppn
[2], ppn
[3], ppn
[4], ppn
[5]);
808 #endif /* CONFIG_D80211_DEBUG */
809 key
->u
.ccmp
.replays
++;
813 if ((rx
->u
.rx
.status
->flag
& RX_FLAG_DECRYPTED
) &&
814 !key
->force_sw_encrypt
) {
815 /* hwaccel has already decrypted frame and verified MIC */
817 u8
*scratch
, *b_0
, *aad
;
819 scratch
= key
->u
.ccmp
.rx_crypto_buf
;
820 b_0
= scratch
+ 3 * AES_BLOCK_LEN
;
821 aad
= scratch
+ 4 * AES_BLOCK_LEN
;
823 ccmp_special_blocks(skb
, pn
, b_0
, aad
, 1);
825 if (ieee80211_aes_ccm_decrypt(
826 key
->u
.ccmp
.tfm
, scratch
, b_0
, aad
,
827 skb
->data
+ hdrlen
+ CCMP_HDR_LEN
, data_len
,
828 skb
->data
+ skb
->len
- CCMP_MIC_LEN
,
829 skb
->data
+ hdrlen
+ CCMP_HDR_LEN
)) {
830 printk(KERN_DEBUG
"%s: CCMP decrypt failed for RX "
831 "frame from " MAC_FMT
"\n", rx
->dev
->name
,
832 MAC_ARG(rx
->sta
->addr
));
837 memcpy(key
->u
.ccmp
.rx_pn
[rx
->u
.rx
.queue
], pn
, CCMP_PN_LEN
);
839 /* Remove CCMP header and MIC */
840 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
841 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdrlen
);
842 skb_pull(skb
, CCMP_HDR_LEN
);
844 return TXRX_CONTINUE
;