2 * Copyright 2002-2004, Instant802 Networks, Inc.
3 * Copyright 2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/netdevice.h>
11 #include <linux/types.h>
12 #include <linux/skbuff.h>
13 #include <linux/compiler.h>
14 #include <linux/ieee80211.h>
15 #include <linux/gfp.h>
16 #include <asm/unaligned.h>
17 #include <net/mac80211.h>
19 #include "ieee80211_i.h"
27 ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data
*tx
)
29 u8
*data
, *key
, *mic
, key_offset
;
32 struct ieee80211_hdr
*hdr
;
33 struct sk_buff
*skb
= tx
->skb
;
34 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
38 hdr
= (struct ieee80211_hdr
*)skb
->data
;
39 if (!tx
->key
|| tx
->key
->conf
.alg
!= ALG_TKIP
|| skb
->len
< 24 ||
40 !ieee80211_is_data_present(hdr
->frame_control
))
43 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
44 if (skb
->len
< hdrlen
)
47 data
= skb
->data
+ hdrlen
;
48 data_len
= skb
->len
- hdrlen
;
50 if (info
->control
.hw_key
&&
51 !(tx
->flags
& IEEE80211_TX_FRAGMENTED
) &&
52 !(tx
->key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
)) {
53 /* hwaccel - with no need for SW-generated MMIC */
57 tail
= MICHAEL_MIC_LEN
;
58 if (!info
->control
.hw_key
)
61 if (WARN_ON(skb_tailroom(skb
) < tail
||
62 skb_headroom(skb
) < TKIP_IV_LEN
))
66 key_offset
= authenticator
?
67 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY
:
68 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY
;
69 key
= &tx
->key
->conf
.key
[key_offset
];
70 mic
= skb_put(skb
, MICHAEL_MIC_LEN
);
71 michael_mic(key
, hdr
, data
, data_len
, mic
);
78 ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data
*rx
)
80 u8
*data
, *key
= NULL
, key_offset
;
83 u8 mic
[MICHAEL_MIC_LEN
];
84 struct sk_buff
*skb
= rx
->skb
;
85 struct ieee80211_rx_status
*status
= IEEE80211_SKB_RXCB(skb
);
86 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
87 int authenticator
= 1, wpa_test
= 0;
89 /* No way to verify the MIC if the hardware stripped it */
90 if (status
->flag
& RX_FLAG_MMIC_STRIPPED
)
93 if (!rx
->key
|| rx
->key
->conf
.alg
!= ALG_TKIP
||
94 !ieee80211_has_protected(hdr
->frame_control
) ||
95 !ieee80211_is_data_present(hdr
->frame_control
))
98 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
99 if (skb
->len
< hdrlen
+ MICHAEL_MIC_LEN
)
100 return RX_DROP_UNUSABLE
;
102 data
= skb
->data
+ hdrlen
;
103 data_len
= skb
->len
- hdrlen
- MICHAEL_MIC_LEN
;
106 key_offset
= authenticator
?
107 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY
:
108 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY
;
109 key
= &rx
->key
->conf
.key
[key_offset
];
110 michael_mic(key
, hdr
, data
, data_len
, mic
);
111 if (memcmp(mic
, data
+ data_len
, MICHAEL_MIC_LEN
) != 0 || wpa_test
) {
112 if (!(rx
->flags
& IEEE80211_RX_RA_MATCH
))
113 return RX_DROP_UNUSABLE
;
115 mac80211_ev_michael_mic_failure(rx
->sdata
, rx
->key
->conf
.keyidx
,
116 (void *) skb
->data
, NULL
,
118 return RX_DROP_UNUSABLE
;
121 /* remove Michael MIC from payload */
122 skb_trim(skb
, skb
->len
- MICHAEL_MIC_LEN
);
124 /* update IV in key information to be able to detect replays */
125 rx
->key
->u
.tkip
.rx
[rx
->queue
].iv32
= rx
->tkip_iv32
;
126 rx
->key
->u
.tkip
.rx
[rx
->queue
].iv16
= rx
->tkip_iv16
;
132 static int tkip_encrypt_skb(struct ieee80211_tx_data
*tx
, struct sk_buff
*skb
)
134 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
135 struct ieee80211_key
*key
= tx
->key
;
136 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
141 if (info
->control
.hw_key
&&
142 !(info
->control
.hw_key
->flags
& IEEE80211_KEY_FLAG_GENERATE_IV
)) {
143 /* hwaccel - with no need for software-generated IV */
147 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
148 len
= skb
->len
- hdrlen
;
150 if (info
->control
.hw_key
)
155 if (WARN_ON(skb_tailroom(skb
) < tail
||
156 skb_headroom(skb
) < TKIP_IV_LEN
))
159 pos
= skb_push(skb
, TKIP_IV_LEN
);
160 memmove(pos
, pos
+ TKIP_IV_LEN
, hdrlen
);
163 /* Increase IV for the frame */
164 key
->u
.tkip
.tx
.iv16
++;
165 if (key
->u
.tkip
.tx
.iv16
== 0)
166 key
->u
.tkip
.tx
.iv32
++;
168 pos
= ieee80211_tkip_add_iv(pos
, key
, key
->u
.tkip
.tx
.iv16
);
170 /* hwaccel - with software IV */
171 if (info
->control
.hw_key
)
174 /* Add room for ICV */
175 skb_put(skb
, TKIP_ICV_LEN
);
177 hdr
= (struct ieee80211_hdr
*) skb
->data
;
178 return ieee80211_tkip_encrypt_data(tx
->local
->wep_tx_tfm
,
179 key
, pos
, len
, hdr
->addr2
);
184 ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data
*tx
)
186 struct sk_buff
*skb
= tx
->skb
;
188 ieee80211_tx_set_protected(tx
);
191 if (tkip_encrypt_skb(tx
, skb
) < 0)
193 } while ((skb
= skb
->next
));
200 ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data
*rx
)
202 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) rx
->skb
->data
;
203 int hdrlen
, res
, hwaccel
= 0, wpa_test
= 0;
204 struct ieee80211_key
*key
= rx
->key
;
205 struct sk_buff
*skb
= rx
->skb
;
206 struct ieee80211_rx_status
*status
= IEEE80211_SKB_RXCB(skb
);
208 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
210 if (!ieee80211_is_data(hdr
->frame_control
))
213 if (!rx
->sta
|| skb
->len
- hdrlen
< 12)
214 return RX_DROP_UNUSABLE
;
216 if (status
->flag
& RX_FLAG_DECRYPTED
) {
217 if (status
->flag
& RX_FLAG_IV_STRIPPED
) {
219 * Hardware took care of all processing, including
220 * replay protection, and stripped the ICV/IV so
221 * we cannot do any checks here.
226 /* let TKIP code verify IV, but skip decryption */
230 res
= ieee80211_tkip_decrypt_data(rx
->local
->wep_rx_tfm
,
231 key
, skb
->data
+ hdrlen
,
232 skb
->len
- hdrlen
, rx
->sta
->sta
.addr
,
233 hdr
->addr1
, hwaccel
, rx
->queue
,
236 if (res
!= TKIP_DECRYPT_OK
|| wpa_test
)
237 return RX_DROP_UNUSABLE
;
240 skb_trim(skb
, skb
->len
- TKIP_ICV_LEN
);
243 memmove(skb
->data
+ TKIP_IV_LEN
, skb
->data
, hdrlen
);
244 skb_pull(skb
, TKIP_IV_LEN
);
250 static void ccmp_special_blocks(struct sk_buff
*skb
, u8
*pn
, u8
*scratch
,
254 int a4_included
, mgmt
;
259 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
261 b_0
= scratch
+ 3 * AES_BLOCK_LEN
;
262 aad
= scratch
+ 4 * AES_BLOCK_LEN
;
265 * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
266 * Retry, PwrMgt, MoreData; set Protected
268 mgmt
= ieee80211_is_mgmt(hdr
->frame_control
);
269 mask_fc
= hdr
->frame_control
;
270 mask_fc
&= ~cpu_to_le16(IEEE80211_FCTL_RETRY
|
271 IEEE80211_FCTL_PM
| IEEE80211_FCTL_MOREDATA
);
273 mask_fc
&= ~cpu_to_le16(0x0070);
274 mask_fc
|= cpu_to_le16(IEEE80211_FCTL_PROTECTED
);
276 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
278 a4_included
= ieee80211_has_a4(hdr
->frame_control
);
280 if (ieee80211_is_data_qos(hdr
->frame_control
))
281 qos_tid
= *ieee80211_get_qos_ctl(hdr
) & IEEE80211_QOS_CTL_TID_MASK
;
285 data_len
= skb
->len
- hdrlen
- CCMP_HDR_LEN
;
287 data_len
-= CCMP_MIC_LEN
;
289 /* First block, b_0 */
290 b_0
[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
291 /* Nonce: Nonce Flags | A2 | PN
292 * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
294 b_0
[1] = qos_tid
| (mgmt
<< 4);
295 memcpy(&b_0
[2], hdr
->addr2
, ETH_ALEN
);
296 memcpy(&b_0
[8], pn
, CCMP_PN_LEN
);
298 put_unaligned_be16(data_len
, &b_0
[14]);
300 /* AAD (extra authenticate-only data) / masked 802.11 header
301 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
302 put_unaligned_be16(len_a
, &aad
[0]);
303 put_unaligned(mask_fc
, (__le16
*)&aad
[2]);
304 memcpy(&aad
[4], &hdr
->addr1
, 3 * ETH_ALEN
);
306 /* Mask Seq#, leave Frag# */
307 aad
[22] = *((u8
*) &hdr
->seq_ctrl
) & 0x0f;
311 memcpy(&aad
[24], hdr
->addr4
, ETH_ALEN
);
315 memset(&aad
[24], 0, ETH_ALEN
+ IEEE80211_QOS_CTL_LEN
);
321 static inline void ccmp_pn2hdr(u8
*hdr
, u8
*pn
, int key_id
)
326 hdr
[3] = 0x20 | (key_id
<< 6);
334 static inline void ccmp_hdr2pn(u8
*pn
, u8
*hdr
)
345 static int ccmp_encrypt_skb(struct ieee80211_tx_data
*tx
, struct sk_buff
*skb
)
347 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
348 struct ieee80211_key
*key
= tx
->key
;
349 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
350 int hdrlen
, len
, tail
;
354 if (info
->control
.hw_key
&&
355 !(info
->control
.hw_key
->flags
& IEEE80211_KEY_FLAG_GENERATE_IV
)) {
357 * hwaccel has no need for preallocated room for CCMP
358 * header or MIC fields
363 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
364 len
= skb
->len
- hdrlen
;
366 if (info
->control
.hw_key
)
371 if (WARN_ON(skb_tailroom(skb
) < tail
||
372 skb_headroom(skb
) < CCMP_HDR_LEN
))
375 pos
= skb_push(skb
, CCMP_HDR_LEN
);
376 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdrlen
);
377 hdr
= (struct ieee80211_hdr
*) pos
;
381 pn
= key
->u
.ccmp
.tx_pn
;
383 for (i
= CCMP_PN_LEN
- 1; i
>= 0; i
--) {
389 ccmp_pn2hdr(pos
, pn
, key
->conf
.keyidx
);
391 /* hwaccel - with software CCMP header */
392 if (info
->control
.hw_key
)
396 ccmp_special_blocks(skb
, pn
, key
->u
.ccmp
.tx_crypto_buf
, 0);
397 ieee80211_aes_ccm_encrypt(key
->u
.ccmp
.tfm
, key
->u
.ccmp
.tx_crypto_buf
, pos
, len
,
398 pos
, skb_put(skb
, CCMP_MIC_LEN
));
405 ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data
*tx
)
407 struct sk_buff
*skb
= tx
->skb
;
409 ieee80211_tx_set_protected(tx
);
412 if (ccmp_encrypt_skb(tx
, skb
) < 0)
414 } while ((skb
= skb
->next
));
421 ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data
*rx
)
423 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)rx
->skb
->data
;
425 struct ieee80211_key
*key
= rx
->key
;
426 struct sk_buff
*skb
= rx
->skb
;
427 struct ieee80211_rx_status
*status
= IEEE80211_SKB_RXCB(skb
);
432 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
434 if (!ieee80211_is_data(hdr
->frame_control
) &&
435 !ieee80211_is_robust_mgmt_frame(hdr
))
438 data_len
= skb
->len
- hdrlen
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
439 if (!rx
->sta
|| data_len
< 0)
440 return RX_DROP_UNUSABLE
;
442 if ((status
->flag
& RX_FLAG_DECRYPTED
) &&
443 (status
->flag
& RX_FLAG_IV_STRIPPED
))
446 ccmp_hdr2pn(pn
, skb
->data
+ hdrlen
);
448 queue
= ieee80211_is_mgmt(hdr
->frame_control
) ?
449 NUM_RX_DATA_QUEUES
: rx
->queue
;
451 if (memcmp(pn
, key
->u
.ccmp
.rx_pn
[queue
], CCMP_PN_LEN
) <= 0) {
452 key
->u
.ccmp
.replays
++;
453 return RX_DROP_UNUSABLE
;
456 if (!(status
->flag
& RX_FLAG_DECRYPTED
)) {
457 /* hardware didn't decrypt/verify MIC */
458 ccmp_special_blocks(skb
, pn
, key
->u
.ccmp
.rx_crypto_buf
, 1);
460 if (ieee80211_aes_ccm_decrypt(
461 key
->u
.ccmp
.tfm
, key
->u
.ccmp
.rx_crypto_buf
,
462 skb
->data
+ hdrlen
+ CCMP_HDR_LEN
, data_len
,
463 skb
->data
+ skb
->len
- CCMP_MIC_LEN
,
464 skb
->data
+ hdrlen
+ CCMP_HDR_LEN
))
465 return RX_DROP_UNUSABLE
;
468 memcpy(key
->u
.ccmp
.rx_pn
[queue
], pn
, CCMP_PN_LEN
);
470 /* Remove CCMP header and MIC */
471 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
472 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdrlen
);
473 skb_pull(skb
, CCMP_HDR_LEN
);
479 static void bip_aad(struct sk_buff
*skb
, u8
*aad
)
481 /* BIP AAD: FC(masked) || A1 || A2 || A3 */
483 /* FC type/subtype */
484 aad
[0] = skb
->data
[0];
485 /* Mask FC Retry, PwrMgt, MoreData flags to zero */
486 aad
[1] = skb
->data
[1] & ~(BIT(4) | BIT(5) | BIT(6));
488 memcpy(aad
+ 2, skb
->data
+ 4, 3 * ETH_ALEN
);
492 static inline void bip_ipn_swap(u8
*d
, const u8
*s
)
504 ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data
*tx
)
506 struct sk_buff
*skb
= tx
->skb
;
507 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
508 struct ieee80211_key
*key
= tx
->key
;
509 struct ieee80211_mmie
*mmie
;
513 if (info
->control
.hw_key
)
516 if (WARN_ON(skb_tailroom(skb
) < sizeof(*mmie
)))
519 mmie
= (struct ieee80211_mmie
*) skb_put(skb
, sizeof(*mmie
));
520 mmie
->element_id
= WLAN_EID_MMIE
;
521 mmie
->length
= sizeof(*mmie
) - 2;
522 mmie
->key_id
= cpu_to_le16(key
->conf
.keyidx
);
525 pn
= key
->u
.aes_cmac
.tx_pn
;
527 for (i
= sizeof(key
->u
.aes_cmac
.tx_pn
) - 1; i
>= 0; i
--) {
532 bip_ipn_swap(mmie
->sequence_number
, pn
);
537 * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
539 ieee80211_aes_cmac(key
->u
.aes_cmac
.tfm
, key
->u
.aes_cmac
.tx_crypto_buf
,
540 aad
, skb
->data
+ 24, skb
->len
- 24, mmie
->mic
);
547 ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data
*rx
)
549 struct sk_buff
*skb
= rx
->skb
;
550 struct ieee80211_rx_status
*status
= IEEE80211_SKB_RXCB(skb
);
551 struct ieee80211_key
*key
= rx
->key
;
552 struct ieee80211_mmie
*mmie
;
553 u8 aad
[20], mic
[8], ipn
[6];
554 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
556 if (!ieee80211_is_mgmt(hdr
->frame_control
))
559 if ((status
->flag
& RX_FLAG_DECRYPTED
) &&
560 (status
->flag
& RX_FLAG_IV_STRIPPED
))
563 if (skb
->len
< 24 + sizeof(*mmie
))
564 return RX_DROP_UNUSABLE
;
566 mmie
= (struct ieee80211_mmie
*)
567 (skb
->data
+ skb
->len
- sizeof(*mmie
));
568 if (mmie
->element_id
!= WLAN_EID_MMIE
||
569 mmie
->length
!= sizeof(*mmie
) - 2)
570 return RX_DROP_UNUSABLE
; /* Invalid MMIE */
572 bip_ipn_swap(ipn
, mmie
->sequence_number
);
574 if (memcmp(ipn
, key
->u
.aes_cmac
.rx_pn
, 6) <= 0) {
575 key
->u
.aes_cmac
.replays
++;
576 return RX_DROP_UNUSABLE
;
579 if (!(status
->flag
& RX_FLAG_DECRYPTED
)) {
580 /* hardware didn't decrypt/verify MIC */
582 ieee80211_aes_cmac(key
->u
.aes_cmac
.tfm
,
583 key
->u
.aes_cmac
.rx_crypto_buf
, aad
,
584 skb
->data
+ 24, skb
->len
- 24, mic
);
585 if (memcmp(mic
, mmie
->mic
, sizeof(mmie
->mic
)) != 0) {
586 key
->u
.aes_cmac
.icverrors
++;
587 return RX_DROP_UNUSABLE
;
591 memcpy(key
->u
.aes_cmac
.rx_pn
, ipn
, 6);
594 skb_trim(skb
, skb
->len
- sizeof(*mmie
));