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/config.h>
10 #include <linux/version.h>
11 #include <linux/module.h>
12 #include <linux/netdevice.h>
13 #include <linux/types.h>
14 #include <linux/slab.h>
15 #include <linux/skbuff.h>
16 #include <linux/compiler.h>
17 #include <linux/wireless.h>
18 #include <net/iw_handler.h>
20 #include <net/ieee80211.h>
21 #include <net/ieee80211_common.h>
22 #include "ieee80211_i.h"
27 #ifdef CONFIG_HOSTAPD_WPA_TESTING
28 #include "hostapd_ioctl.h"
29 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
32 #define MICHAEL_MIC_HWACCEL
35 int ieee80211_get_hdr_info(const struct sk_buff
*skb
, u8
**sa
, u8
**da
,
36 u8
*qos_tid
, u8
**data
, size_t *data_len
)
38 struct ieee80211_hdr
*hdr
;
44 hdr
= (struct ieee80211_hdr
*) skb
->data
;
45 fc
= le16_to_cpu(hdr
->frame_control
);
48 if ((fc
& (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) ==
49 (WLAN_FC_FROMDS
| WLAN_FC_TODS
)) {
53 } else if (fc
& WLAN_FC_FROMDS
) {
56 } else if (fc
& WLAN_FC_TODS
) {
67 *data
= skb
->data
+ hdrlen
;
68 *data_len
= skb
->len
- hdrlen
;
70 a4_included
= (fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
71 (WLAN_FC_TODS
| WLAN_FC_FROMDS
);
72 if (WLAN_FC_GET_TYPE(fc
) == WLAN_FC_TYPE_DATA
&&
73 WLAN_FC_GET_STYPE(fc
) & 0x08) {
74 pos
= (u8
*) &hdr
->addr4
;
77 *qos_tid
= pos
[0] & 0x0f;
78 *qos_tid
|= 0x80; /* qos_included flag */
82 return skb
->len
< hdrlen
? -1 : 0;
87 ieee80211_tx_h_michael_mic_add(struct ieee80211_txrx_data
*tx
)
89 u8
*data
, *sa
, *da
, *key
, *mic
, qos_tid
;
92 struct sk_buff
*skb
= tx
->skb
;
94 #if defined(CONFIG_HOSTAPD_WPA_TESTING) || defined(MICHAEL_MIC_HWACCEL)
100 if (!tx
->key
|| tx
->key
->alg
!= ALG_TKIP
|| skb
->len
< 24 ||
101 !WLAN_FC_DATA_PRESENT(fc
))
102 return TXRX_CONTINUE
;
104 if (ieee80211_get_hdr_info(skb
, &sa
, &da
, &qos_tid
, &data
, &data_len
))
107 #ifdef CONFIG_HOSTAPD_WPA_TESTING
108 if ((tx
->sta
&& tx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_MIC
) ||
109 (!tx
->u
.tx
.unicast
&&
110 tx
->local
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_MIC
)) {
113 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
115 #ifdef MICHAEL_MIC_HWACCEL
116 if (!tx
->key
->force_sw_encrypt
&& !tx
->local
->conf
.sw_decrypt
&&
117 !tx
->fragmented
&& !wpa_test
) {
118 /* hwaccel - with no need for preallocated room for Michael MIC
120 return TXRX_CONTINUE
;
122 #endif /* MICHAEL_MIC_HWACCEL */
124 if (skb_tailroom(skb
) < MICHAEL_MIC_LEN
) {
125 I802_DEBUG_INC(tx
->local
->tx_expand_skb_head
);
126 if (unlikely(pskb_expand_head(skb
, TKIP_IV_LEN
,
127 MICHAEL_MIC_LEN
+ TKIP_ICV_LEN
,
129 printk(KERN_DEBUG
"%s: failed to allocate more memory "
130 "for Michael MIC\n", tx
->dev
->name
);
136 authenticator
= fc
& WLAN_FC_FROMDS
; /* FIX */
140 key
= &tx
->key
->key
[authenticator
? ALG_TKIP_TEMP_AUTH_TX_MIC_KEY
:
141 ALG_TKIP_TEMP_AUTH_RX_MIC_KEY
];
142 mic
= skb_put(skb
, MICHAEL_MIC_LEN
);
143 michael_mic(key
, da
, sa
, qos_tid
& 0x0f, data
, data_len
, mic
);
145 #ifdef CONFIG_HOSTAPD_WPA_TESTING
146 if (tx
->sta
&& tx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_MIC
) {
147 printk(KERN_INFO
"%s: WPA testing - corrupting TX Michael MIC "
148 "for STA " MACSTR
"\n",
149 tx
->dev
->name
, MAC2STR(tx
->sta
->addr
));
150 tx
->u
.tx
.control
->key_idx
= HW_KEY_IDX_INVALID
;
151 tx
->sta
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_TX_MIC
;
154 } else if (!tx
->u
.tx
.unicast
&&
155 tx
->local
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_MIC
) {
156 printk(KERN_INFO
"%s: WPA testing - corrupting TX Michael MIC "
157 "for Group Key\n", tx
->dev
->name
);
158 tx
->u
.tx
.control
->key_idx
= HW_KEY_IDX_INVALID
;
159 tx
->local
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_TX_MIC
;
163 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
165 return TXRX_CONTINUE
;
169 ieee80211_txrx_result
170 ieee80211_rx_h_michael_mic_verify(struct ieee80211_txrx_data
*rx
)
172 u8
*data
, *sa
, *da
, *key
= NULL
, qos_tid
;
175 u8 mic
[MICHAEL_MIC_LEN
];
176 struct sk_buff
*skb
= rx
->skb
;
177 int authenticator
= 1, wpa_test
= 0;
181 /* If device handles decryption totally, skip this check */
182 if (rx
->local
->hw
->device_hides_wep
||
183 rx
->local
->hw
->device_strips_mic
)
184 return TXRX_CONTINUE
;
186 if (!rx
->key
|| rx
->key
->alg
!= ALG_TKIP
||
187 !(rx
->fc
& WLAN_FC_ISWEP
) || !WLAN_FC_DATA_PRESENT(fc
))
188 return TXRX_CONTINUE
;
190 #ifdef CONFIG_HOSTAPD_WPA_TESTING
191 if (rx
->sta
&& rx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_RX_MIC
) {
194 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
196 #ifdef MICHAEL_MIC_HWACCEL
197 if ((rx
->u
.rx
.status
->flag
& RX_FLAG_DECRYPTED
) &&
198 !rx
->key
->force_sw_encrypt
&& !rx
->local
->conf
.sw_decrypt
) {
199 if (rx
->local
->hw
->wep_include_iv
) {
200 if (skb
->len
< MICHAEL_MIC_LEN
)
203 /* Need to verify Michael MIC sometimes in software even when
204 * hwaccel is used. Atheros ar5212: fragmented frames and QoS
206 if (!rx
->fragmented
&& !wpa_test
)
209 #endif /* MICHAEL_MIC_HWACCEL */
211 if (ieee80211_get_hdr_info(skb
, &sa
, &da
, &qos_tid
, &data
, &data_len
)
212 || data_len
< MICHAEL_MIC_LEN
)
215 data_len
-= MICHAEL_MIC_LEN
;
218 authenticator
= fc
& WLAN_FC_TODS
; /* FIX */
222 key
= &rx
->key
->key
[authenticator
? ALG_TKIP_TEMP_AUTH_RX_MIC_KEY
:
223 ALG_TKIP_TEMP_AUTH_TX_MIC_KEY
];
224 michael_mic(key
, da
, sa
, qos_tid
& 0x0f, data
, data_len
, mic
);
225 #ifdef CONFIG_HOSTAPD_WPA_TESTING
226 if (rx
->sta
&& rx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_RX_MIC
) {
227 printk(KERN_INFO
"%s: WPA testing - corrupting RX Michael MIC "
228 "for STA " MACSTR
"\n",
229 rx
->dev
->name
, MAC2STR(rx
->sta
->addr
));
230 rx
->sta
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_RX_MIC
;
233 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
234 if (memcmp(mic
, data
+ data_len
, MICHAEL_MIC_LEN
) != 0 || wpa_test
) {
235 #ifdef CONFIG_HOSTAPD_WPA_TESTING
237 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
238 printk(KERN_DEBUG
"%s: invalid Michael MIC in data frame from "
239 MACSTR
"\n", rx
->dev
->name
, MAC2STR(sa
));
240 #ifdef CONFIG_HOSTAPD_WPA_TESTING
241 printk(KERN_DEBUG
" received");
242 for (i
= 0; i
< MICHAEL_MIC_LEN
; i
++)
243 printk(" %02x", data
[data_len
+ i
]);
245 for (i
= 0; i
< MICHAEL_MIC_LEN
; i
++)
246 printk(" %02x", mic
[i
]);
248 printk(KERN_DEBUG
" SA=" MACSTR
" DA=" MACSTR
" key",
249 MAC2STR(sa
), MAC2STR(da
));
250 for (i
= 0; i
< 8; i
++)
251 printk(" %02x", key
[i
]);
252 printk(" (%d)\n", authenticator
);
253 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
256 struct ieee80211_hdr
*hdr
;
257 union iwreq_data wrqu
;
258 char *buf
= kmalloc(128, GFP_ATOMIC
);
262 /* TODO: needed parameters: count, key type, TSC */
263 hdr
= (struct ieee80211_hdr
*) skb
->data
;
264 sprintf(buf
, "MLME-MICHAELMICFAILURE.indication("
265 "keyid=%d %scast addr=" MACSTR
")",
267 hdr
->addr1
[0] & 0x01 ? "broad" : "uni",
268 MAC2STR(hdr
->addr2
));
269 memset(&wrqu
, 0, sizeof(wrqu
));
270 wrqu
.data
.length
= strlen(buf
);
271 wireless_send_event(rx
->dev
, IWEVCUSTOM
, &wrqu
, buf
);
275 ieee80211_rx_mgmt(rx
->dev
, rx
->skb
, rx
->u
.rx
.status
,
276 ieee80211_msg_michael_mic_failure
);
281 #ifdef MICHAEL_MIC_HWACCEL
283 #endif /* MICHAEL_MIC_HWACCEL */
284 /* remove Michael MIC from payload */
285 skb_trim(skb
, skb
->len
- MICHAEL_MIC_LEN
);
287 return TXRX_CONTINUE
;
291 static int tkip_encrypt_skb(struct ieee80211_txrx_data
*tx
,
292 struct sk_buff
*skb
, int test
)
294 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
295 struct ieee80211_key
*key
= tx
->key
;
296 int hdrlen
, len
, tailneed
;
300 fc
= le16_to_cpu(hdr
->frame_control
);
301 hdrlen
= ieee80211_get_hdrlen(fc
);
302 len
= skb
->len
- hdrlen
;
304 tailneed
= (!tx
->key
->force_sw_encrypt
&& !tx
->local
->conf
.sw_decrypt
)
306 if ((skb_headroom(skb
) < TKIP_IV_LEN
||
307 skb_tailroom(skb
) < tailneed
)) {
308 I802_DEBUG_INC(tx
->local
->tx_expand_skb_head
);
309 if (unlikely(pskb_expand_head(skb
, TKIP_IV_LEN
, tailneed
,
314 pos
= skb_push(skb
, TKIP_IV_LEN
);
315 memmove(pos
, pos
+ TKIP_IV_LEN
, hdrlen
);
318 #ifdef CONFIG_HOSTAPD_WPA_TESTING
319 if (test
& WPA_TRIGGER_TX_REPLAY
)
322 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
324 /* Increase IV for the frame */
326 if (key
->u
.tkip
.iv16
== 0)
329 #ifdef CONFIG_HOSTAPD_WPA_TESTING
330 if (test
& WPA_TRIGGER_TX_SKIP_SEQ
) {
335 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
337 if (!tx
->key
->force_sw_encrypt
&& !tx
->local
->conf
.sw_decrypt
338 #ifdef CONFIG_HOSTAPD_WPA_TESTING
340 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
342 /* hwaccel - with preallocated room for IV */
344 ieee80211_tkip_add_iv(pos
, key
,
345 (u8
) (key
->u
.tkip
.iv16
>> 8),
346 (u8
) (((key
->u
.tkip
.iv16
>> 8) | 0x20) &
348 (u8
) key
->u
.tkip
.iv16
);
350 tx
->u
.tx
.control
->key_idx
= tx
->key
->hw_key_idx
;
354 /* Add room for ICV */
355 skb_put(skb
, TKIP_ICV_LEN
);
357 hdr
= (struct ieee80211_hdr
*) skb
->data
;
358 ieee80211_tkip_encrypt_data(key
, pos
, len
, hdr
->addr2
);
363 ieee80211_txrx_result
364 ieee80211_tx_h_tkip_encrypt(struct ieee80211_txrx_data
*tx
)
366 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) tx
->skb
->data
;
368 struct ieee80211_key
*key
= tx
->key
;
369 struct sk_buff
*skb
= tx
->skb
;
370 int wpa_test
= 0, test
= 0;
372 fc
= le16_to_cpu(hdr
->frame_control
);
374 if (!key
|| key
->alg
!= ALG_TKIP
|| !WLAN_FC_DATA_PRESENT(fc
))
375 return TXRX_CONTINUE
;
377 tx
->u
.tx
.control
->icv_len
= TKIP_ICV_LEN
;
378 tx
->u
.tx
.control
->iv_len
= TKIP_IV_LEN
;
379 ieee80211_tx_set_iswep(tx
);
381 #ifdef CONFIG_HOSTAPD_WPA_TESTING
382 if ((tx
->sta
&& tx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_ICV
) ||
383 (!tx
->u
.tx
.unicast
&&
384 tx
->local
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_ICV
)) {
389 test
= tx
->sta
->wpa_trigger
;
390 tx
->sta
->wpa_trigger
&=
391 ~(WPA_TRIGGER_TX_REPLAY
| WPA_TRIGGER_TX_REPLAY_FRAG
|
392 WPA_TRIGGER_TX_SKIP_SEQ
);
394 test
= tx
->local
->wpa_trigger
;
395 tx
->local
->wpa_trigger
&=
396 ~(WPA_TRIGGER_TX_REPLAY
| WPA_TRIGGER_TX_REPLAY_FRAG
|
397 WPA_TRIGGER_TX_SKIP_SEQ
);
400 (WPA_TRIGGER_TX_REPLAY
| WPA_TRIGGER_TX_REPLAY_FRAG
|
401 WPA_TRIGGER_TX_SKIP_SEQ
)) {
402 printk(KERN_INFO
"%s: WPA testing - TKIP TX packet number "
403 "%s%s%s%s\n", tx
->dev
->name
,
404 tx
->sta
? "[UNICAST]" : "[MULTICAST]",
405 test
& WPA_TRIGGER_TX_REPLAY
? "[REPLAY]" : "",
406 test
& WPA_TRIGGER_TX_REPLAY_FRAG
?
407 "[REPLAY FRAG]" : "",
408 test
& WPA_TRIGGER_TX_SKIP_SEQ
? "[SKIP SEQ]" : "");
410 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
412 if (!tx
->key
->force_sw_encrypt
&& !tx
->local
->conf
.sw_decrypt
&&
413 !tx
->local
->hw
->wep_include_iv
&& !wpa_test
) {
414 /* hwaccel - with no need for preallocated room for IV/ICV */
415 tx
->u
.tx
.control
->key_idx
= tx
->key
->hw_key_idx
;
416 return TXRX_CONTINUE
;
419 if (tkip_encrypt_skb(tx
, skb
, test
) < 0)
422 if (tx
->u
.tx
.extra_frag
) {
424 #ifdef CONFIG_HOSTAPD_WPA_TESTING
425 if (test
& WPA_TRIGGER_TX_REPLAY_FRAG
)
426 test
|= WPA_TRIGGER_TX_REPLAY
;
427 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
428 for (i
= 0; i
< tx
->u
.tx
.num_extra_frag
; i
++) {
429 if (tkip_encrypt_skb(tx
, tx
->u
.tx
.extra_frag
[i
], test
)
435 #ifdef CONFIG_HOSTAPD_WPA_TESTING
436 if (tx
->sta
&& tx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_ICV
) {
437 printk(KERN_INFO
"%s: WPA testing - corrupting TX TKIP ICV "
438 "for STA " MACSTR
"\n",
439 tx
->dev
->name
, MAC2STR(tx
->sta
->addr
));
440 tx
->u
.tx
.control
->key_idx
= HW_KEY_IDX_INVALID
;
441 tx
->sta
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_TX_ICV
;
442 skb
->data
[skb
->len
- 1]++;
443 } else if (!tx
->u
.tx
.unicast
&&
444 tx
->local
->wpa_trigger
& WPA_TRIGGER_FAIL_TX_ICV
) {
445 printk(KERN_INFO
"%s: WPA testing - corrupting TX TKIP ICV "
448 tx
->u
.tx
.control
->key_idx
= HW_KEY_IDX_INVALID
;
449 tx
->local
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_TX_ICV
;
450 skb
->data
[skb
->len
- 1]++;
452 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
454 return TXRX_CONTINUE
;
458 ieee80211_txrx_result
459 ieee80211_rx_h_tkip_decrypt(struct ieee80211_txrx_data
*rx
)
461 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) rx
->skb
->data
;
463 int hdrlen
, res
, hwaccel
= 0, wpa_test
= 0;
464 struct ieee80211_key
*key
= rx
->key
;
465 struct sk_buff
*skb
= rx
->skb
;
467 fc
= le16_to_cpu(hdr
->frame_control
);
468 hdrlen
= ieee80211_get_hdrlen(fc
);
470 if (!rx
->key
|| rx
->key
->alg
!= ALG_TKIP
||
471 !(rx
->fc
& WLAN_FC_ISWEP
) ||
472 WLAN_FC_GET_TYPE(rx
->fc
) != WLAN_FC_TYPE_DATA
)
473 return TXRX_CONTINUE
;
475 if (!rx
->sta
|| skb
->len
- hdrlen
< 12)
478 #ifdef CONFIG_HOSTAPD_WPA_TESTING
479 if (rx
->sta
&& rx
->sta
->wpa_trigger
& WPA_TRIGGER_FAIL_RX_ICV
) {
480 printk(KERN_INFO
"%s: WPA testing - corrupting RX TKIP ICV "
481 "for STA " MACSTR
"\n",
482 rx
->dev
->name
, MAC2STR(rx
->sta
->addr
));
483 rx
->sta
->wpa_trigger
&= ~WPA_TRIGGER_FAIL_RX_ICV
;
484 skb
->data
[skb
->len
- 1]++;
487 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
489 if ((rx
->u
.rx
.status
->flag
& RX_FLAG_DECRYPTED
) &&
490 !rx
->key
->force_sw_encrypt
&& !rx
->local
->conf
.sw_decrypt
) {
491 if (!rx
->local
->hw
->wep_include_iv
) {
492 /* Hardware takes care of all processing, including
493 * replay protection, so no need to continue here. */
494 return TXRX_CONTINUE
;
497 /* let TKIP code verify IV, but skip decryption */
501 res
= ieee80211_tkip_decrypt_data(key
, skb
->data
+ hdrlen
,
502 skb
->len
- hdrlen
, rx
->sta
->addr
,
503 hwaccel
, rx
->u
.rx
.queue
);
504 if (res
!= TKIP_DECRYPT_OK
|| wpa_test
) {
505 printk(KERN_DEBUG
"%s: TKIP decrypt failed for RX frame from "
506 MACSTR
" (res=%d)\n",
507 rx
->dev
->name
, MAC2STR(rx
->sta
->addr
), res
);
512 skb_trim(skb
, skb
->len
- TKIP_ICV_LEN
);
515 memmove(skb
->data
+ TKIP_IV_LEN
, skb
->data
, hdrlen
);
516 skb_pull(skb
, TKIP_IV_LEN
);
518 return TXRX_CONTINUE
;
522 static void ccmp_special_blocks(struct sk_buff
*skb
, u8
*pn
, u8
*b_0
, u8
*aad
,
526 int a4_included
, qos_included
;
527 u8 qos_tid
, *fc_pos
, *data
, *sa
, *da
;
530 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
532 fc_pos
= (u8
*) &hdr
->frame_control
;
533 fc
= fc_pos
[0] ^ (fc_pos
[1] << 8);
534 a4_included
= (fc
& (WLAN_FC_TODS
| WLAN_FC_FROMDS
)) ==
535 (WLAN_FC_TODS
| WLAN_FC_FROMDS
);
537 ieee80211_get_hdr_info(skb
, &sa
, &da
, &qos_tid
, &data
, &data_len
);
538 data_len
-= CCMP_HDR_LEN
+ (encrypted
? CCMP_MIC_LEN
: 0);
539 if (qos_tid
& 0x80) {
544 /* First block, b_0 */
546 b_0
[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
547 /* Nonce: QoS Priority | A2 | PN */
549 memcpy(&b_0
[2], hdr
->addr2
, 6);
550 memcpy(&b_0
[8], pn
, CCMP_PN_LEN
);
552 b_0
[14] = (data_len
>> 8) & 0xff;
553 b_0
[15] = data_len
& 0xff;
556 /* AAD (extra authenticate-only data) / masked 802.11 header
557 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
559 len_a
= a4_included
? 28 : 22;
563 aad
[0] = 0; /* (len_a >> 8) & 0xff; */
564 aad
[1] = len_a
& 0xff;
565 /* Mask FC: zero subtype b4 b5 b6 */
566 aad
[2] = fc_pos
[0] & ~(BIT(4) | BIT(5) | BIT(6));
567 /* Retry, PwrMgt, MoreData; set Protected */
568 aad
[3] = (fc_pos
[1] & ~(BIT(3) | BIT(4) | BIT(5))) | BIT(6);
569 memcpy(&aad
[4], &hdr
->addr1
, 18);
571 /* Mask Seq#, leave Frag# */
572 aad
[22] = *((u8
*) &hdr
->seq_ctrl
) & 0x0f;
575 memcpy(&aad
[24], hdr
->addr4
, 6);
579 memset(&aad
[24], 0, 8);
581 u8
*dpos
= &aad
[a4_included
? 30 : 24];
583 /* Mask QoS Control field */
590 static inline void ccmp_pn2hdr(u8
*hdr
, u8
*pn
, int key_id
)
595 hdr
[3] = 0x20 | (key_id
<< 6);
603 static inline int ccmp_hdr2pn(u8
*pn
, u8
*hdr
)
611 return (hdr
[3] >> 6) & 0x03;
615 static int ccmp_encrypt_skb(struct ieee80211_txrx_data
*tx
,
616 struct sk_buff
*skb
, int test
)
618 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
619 struct ieee80211_key
*key
= tx
->key
;
620 int hdrlen
, len
, tailneed
;
623 u8 b_0
[AES_BLOCK_LEN
], aad
[2 * AES_BLOCK_LEN
];
626 fc
= le16_to_cpu(hdr
->frame_control
);
627 hdrlen
= ieee80211_get_hdrlen(fc
);
628 len
= skb
->len
- hdrlen
;
630 tailneed
= (!tx
->key
->force_sw_encrypt
&& !tx
->local
->conf
.sw_decrypt
)
633 if ((skb_headroom(skb
) < CCMP_HDR_LEN
||
634 skb_tailroom(skb
) < tailneed
)) {
635 I802_DEBUG_INC(tx
->local
->tx_expand_skb_head
);
636 if (unlikely(pskb_expand_head(skb
, CCMP_HDR_LEN
, tailneed
,
641 pos
= skb_push(skb
, CCMP_HDR_LEN
);
642 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdrlen
);
643 hdr
= (struct ieee80211_hdr
*) pos
;
647 pn
= key
->u
.ccmp
.tx_pn
;
649 #ifdef CONFIG_HOSTAPD_WPA_TESTING
650 if (test
& WPA_TRIGGER_TX_REPLAY
)
653 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
655 for (i
= CCMP_PN_LEN
- 1; i
>= 0; i
--) {
661 #ifdef CONFIG_HOSTAPD_WPA_TESTING
662 if (test
& WPA_TRIGGER_TX_SKIP_SEQ
) {
667 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
669 ccmp_pn2hdr(pos
, pn
, key
->keyidx
);
671 if (!tx
->key
->force_sw_encrypt
&& !tx
->local
->conf
.sw_decrypt
) {
672 /* hwaccel - with preallocated room for CCMP header */
673 tx
->u
.tx
.control
->key_idx
= tx
->key
->hw_key_idx
;
678 ccmp_special_blocks(skb
, pn
, b_0
, aad
, 0);
679 ieee80211_aes_ccm_encrypt(key
->u
.ccmp
.aes_state
, b_0
, aad
, pos
, len
,
680 pos
, skb_put(skb
, CCMP_MIC_LEN
));
686 ieee80211_txrx_result
687 ieee80211_tx_h_ccmp_encrypt(struct ieee80211_txrx_data
*tx
)
689 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) tx
->skb
->data
;
690 struct ieee80211_key
*key
= tx
->key
;
692 struct sk_buff
*skb
= tx
->skb
;
695 fc
= le16_to_cpu(hdr
->frame_control
);
697 if (!key
|| key
->alg
!= ALG_CCMP
|| !WLAN_FC_DATA_PRESENT(fc
))
698 return TXRX_CONTINUE
;
700 #ifdef CONFIG_HOSTAPD_WPA_TESTING
702 test
= tx
->sta
->wpa_trigger
;
703 tx
->sta
->wpa_trigger
= 0;
705 test
= tx
->local
->wpa_trigger
;
706 tx
->local
->wpa_trigger
= 0;
709 (WPA_TRIGGER_TX_REPLAY
| WPA_TRIGGER_TX_REPLAY_FRAG
|
710 WPA_TRIGGER_TX_SKIP_SEQ
)) {
711 printk(KERN_INFO
"%s: WPA testing - CCMP TX packet number "
712 "%s%s%s%s\n", tx
->dev
->name
,
713 tx
->sta
? "[UNICAST]" : "[MULTICAST]",
714 test
& WPA_TRIGGER_TX_REPLAY
? "[REPLAY]" : "",
715 test
& WPA_TRIGGER_TX_REPLAY_FRAG
?
716 "[REPLAY FRAG]" : "",
717 test
& WPA_TRIGGER_TX_SKIP_SEQ
? "[SKIP SEQ]" : "");
719 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
721 tx
->u
.tx
.control
->icv_len
= CCMP_MIC_LEN
;
722 tx
->u
.tx
.control
->iv_len
= CCMP_HDR_LEN
;
723 ieee80211_tx_set_iswep(tx
);
725 if (!tx
->key
->force_sw_encrypt
&& !tx
->local
->conf
.sw_decrypt
&&
726 !tx
->local
->hw
->wep_include_iv
) {
727 /* hwaccel - with no need for preallocated room for CCMP "
728 * header or MIC fields */
729 tx
->u
.tx
.control
->key_idx
= tx
->key
->hw_key_idx
;
730 return TXRX_CONTINUE
;
733 if (ccmp_encrypt_skb(tx
, skb
, test
) < 0)
736 if (tx
->u
.tx
.extra_frag
) {
738 #ifdef CONFIG_HOSTAPD_WPA_TESTING
739 if (test
& WPA_TRIGGER_TX_REPLAY_FRAG
)
740 test
|= WPA_TRIGGER_TX_REPLAY
;
741 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
742 for (i
= 0; i
< tx
->u
.tx
.num_extra_frag
; i
++) {
743 if (ccmp_encrypt_skb(tx
, tx
->u
.tx
.extra_frag
[i
], test
)
749 return TXRX_CONTINUE
;
753 ieee80211_txrx_result
754 ieee80211_rx_h_ccmp_decrypt(struct ieee80211_txrx_data
*rx
)
756 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) rx
->skb
->data
;
759 struct ieee80211_key
*key
= rx
->key
;
760 struct sk_buff
*skb
= rx
->skb
;
761 u8 b_0
[AES_BLOCK_LEN
], aad
[2 * AES_BLOCK_LEN
];
765 fc
= le16_to_cpu(hdr
->frame_control
);
766 hdrlen
= ieee80211_get_hdrlen(fc
);
768 if (!rx
->key
|| rx
->key
->alg
!= ALG_CCMP
||
769 !(rx
->fc
& WLAN_FC_ISWEP
) ||
770 WLAN_FC_GET_TYPE(rx
->fc
) != WLAN_FC_TYPE_DATA
)
771 return TXRX_CONTINUE
;
773 data_len
= skb
->len
- hdrlen
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
774 if (!rx
->sta
|| data_len
< 0)
777 if ((rx
->u
.rx
.status
->flag
& RX_FLAG_DECRYPTED
) &&
778 !rx
->key
->force_sw_encrypt
&& !rx
->local
->conf
.sw_decrypt
&&
779 !rx
->local
->hw
->wep_include_iv
)
780 return TXRX_CONTINUE
;
782 (void) ccmp_hdr2pn(pn
, skb
->data
+ hdrlen
);
784 if (memcmp(pn
, key
->u
.ccmp
.rx_pn
[rx
->u
.rx
.queue
], CCMP_PN_LEN
) <= 0) {
785 #ifdef CONFIG_IEEE80211_DEBUG
786 u8
*ppn
= key
->u
.ccmp
.rx_pn
[rx
->u
.rx
.queue
];
787 printk(KERN_DEBUG
"%s: CCMP replay detected for RX frame from "
788 MACSTR
" (RX PN %02x%02x%02x%02x%02x%02x <= prev. PN "
789 "%02x%02x%02x%02x%02x%02x)\n", rx
->dev
->name
,
790 MAC2STR(rx
->sta
->addr
),
791 pn
[0], pn
[1], pn
[2], pn
[3], pn
[4], pn
[5],
792 ppn
[0], ppn
[1], ppn
[2], ppn
[3], ppn
[4], ppn
[5]);
793 #endif /* CONFIG_IEEE80211_DEBUG */
794 key
->u
.ccmp
.replays
++;
798 if ((rx
->u
.rx
.status
->flag
& RX_FLAG_DECRYPTED
) &&
799 !rx
->key
->force_sw_encrypt
&& !rx
->local
->conf
.sw_decrypt
) {
800 /* hwaccel has already decrypted frame and verified MIC */
802 ccmp_special_blocks(skb
, pn
, b_0
, aad
, 1);
804 if (ieee80211_aes_ccm_decrypt(
805 key
->u
.ccmp
.aes_state
, b_0
, aad
,
806 skb
->data
+ hdrlen
+ CCMP_HDR_LEN
, data_len
,
807 skb
->data
+ skb
->len
- CCMP_MIC_LEN
,
808 skb
->data
+ hdrlen
+ CCMP_HDR_LEN
)) {
809 printk(KERN_DEBUG
"%s: CCMP decrypt failed for RX "
810 "frame from " MACSTR
"\n", rx
->dev
->name
,
811 MAC2STR(rx
->sta
->addr
));
816 memcpy(key
->u
.ccmp
.rx_pn
[rx
->u
.rx
.queue
], pn
, CCMP_PN_LEN
);
818 /* Remove CCMP header and MIC */
819 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
820 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdrlen
);
821 skb_pull(skb
, CCMP_HDR_LEN
);
823 return TXRX_CONTINUE
;