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 <linux/ieee80211.h>
15 #include <asm/unaligned.h>
16 #include <net/mac80211.h>
18 #include "ieee80211_i.h"
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
)
27 struct ieee80211_hdr
*hdr
;
31 hdr
= (struct ieee80211_hdr
*)skb
->data
;
32 fc
= hdr
->frame_control
;
34 hdrlen
= ieee80211_hdrlen(fc
);
36 *sa
= ieee80211_get_SA(hdr
);
37 *da
= ieee80211_get_DA(hdr
);
39 *data
= skb
->data
+ hdrlen
;
40 *data_len
= skb
->len
- hdrlen
;
42 if (ieee80211_is_data_qos(fc
))
43 *qos_tid
= (*ieee80211_get_qos_ctl(hdr
) & IEEE80211_QOS_CTL_TID_MASK
) | 0x80;
47 return skb
->len
< hdrlen
? -1 : 0;
52 ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data
*tx
)
54 u8
*data
, *sa
, *da
, *key
, *mic
, qos_tid
, key_offset
;
57 struct sk_buff
*skb
= tx
->skb
;
64 if (!tx
->key
|| tx
->key
->conf
.alg
!= ALG_TKIP
|| skb
->len
< 24 ||
65 !WLAN_FC_DATA_PRESENT(fc
))
68 if (ieee80211_get_hdr_info(skb
, &sa
, &da
, &qos_tid
, &data
, &data_len
))
71 if ((tx
->key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
) &&
72 !(tx
->flags
& IEEE80211_TX_FRAGMENTED
) &&
73 !(tx
->key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_MMIC
) &&
75 /* hwaccel - with no need for preallocated room for Michael MIC
80 tail
= MICHAEL_MIC_LEN
;
81 if (!(tx
->key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
))
84 if (WARN_ON(skb_tailroom(skb
) < tail
||
85 skb_headroom(skb
) < TKIP_IV_LEN
))
89 authenticator
= fc
& IEEE80211_FCTL_FROMDS
; /* FIX */
93 /* At this point we know we're using ALG_TKIP. To get the MIC key
94 * we now will rely on the offset from the ieee80211_key_conf::key */
95 key_offset
= authenticator
?
96 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY
:
97 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY
;
98 key
= &tx
->key
->conf
.key
[key_offset
];
99 mic
= skb_put(skb
, MICHAEL_MIC_LEN
);
100 michael_mic(key
, da
, sa
, qos_tid
& 0x0f, data
, data_len
, mic
);
107 ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data
*rx
)
109 u8
*data
, *sa
, *da
, *key
= NULL
, qos_tid
, key_offset
;
112 u8 mic
[MICHAEL_MIC_LEN
];
113 struct sk_buff
*skb
= rx
->skb
;
114 int authenticator
= 1, wpa_test
= 0;
115 DECLARE_MAC_BUF(mac
);
120 * No way to verify the MIC if the hardware stripped it
122 if (rx
->status
->flag
& RX_FLAG_MMIC_STRIPPED
)
125 if (!rx
->key
|| rx
->key
->conf
.alg
!= ALG_TKIP
||
126 !(rx
->fc
& IEEE80211_FCTL_PROTECTED
) || !WLAN_FC_DATA_PRESENT(fc
))
129 if (ieee80211_get_hdr_info(skb
, &sa
, &da
, &qos_tid
, &data
, &data_len
)
130 || data_len
< MICHAEL_MIC_LEN
)
131 return RX_DROP_UNUSABLE
;
133 data_len
-= MICHAEL_MIC_LEN
;
136 authenticator
= fc
& IEEE80211_FCTL_TODS
; /* FIX */
140 /* At this point we know we're using ALG_TKIP. To get the MIC key
141 * we now will rely on the offset from the ieee80211_key_conf::key */
142 key_offset
= authenticator
?
143 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY
:
144 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY
;
145 key
= &rx
->key
->conf
.key
[key_offset
];
146 michael_mic(key
, da
, sa
, qos_tid
& 0x0f, data
, data_len
, mic
);
147 if (memcmp(mic
, data
+ data_len
, MICHAEL_MIC_LEN
) != 0 || wpa_test
) {
148 if (!(rx
->flags
& IEEE80211_RX_RA_MATCH
))
149 return RX_DROP_UNUSABLE
;
151 mac80211_ev_michael_mic_failure(rx
->dev
, rx
->key
->conf
.keyidx
,
153 return RX_DROP_UNUSABLE
;
156 /* remove Michael MIC from payload */
157 skb_trim(skb
, skb
->len
- MICHAEL_MIC_LEN
);
159 /* update IV in key information to be able to detect replays */
160 rx
->key
->u
.tkip
.rx
[rx
->queue
].iv32
= rx
->tkip_iv32
;
161 rx
->key
->u
.tkip
.rx
[rx
->queue
].iv16
= rx
->tkip_iv16
;
167 static int tkip_encrypt_skb(struct ieee80211_tx_data
*tx
, struct sk_buff
*skb
)
169 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
170 struct ieee80211_key
*key
= tx
->key
;
171 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
176 info
->control
.icv_len
= TKIP_ICV_LEN
;
177 info
->control
.iv_len
= TKIP_IV_LEN
;
179 if ((tx
->key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
) &&
180 !(tx
->key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_IV
)) {
181 /* hwaccel - with no need for preallocated room for IV/ICV */
182 info
->control
.hw_key
= &tx
->key
->conf
;
186 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
187 len
= skb
->len
- hdrlen
;
189 if (tx
->key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
)
194 if (WARN_ON(skb_tailroom(skb
) < tail
||
195 skb_headroom(skb
) < TKIP_IV_LEN
))
198 pos
= skb_push(skb
, TKIP_IV_LEN
);
199 memmove(pos
, pos
+ TKIP_IV_LEN
, hdrlen
);
202 /* Increase IV for the frame */
203 key
->u
.tkip
.tx
.iv16
++;
204 if (key
->u
.tkip
.tx
.iv16
== 0)
205 key
->u
.tkip
.tx
.iv32
++;
207 if (tx
->key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
) {
208 /* hwaccel - with preallocated room for IV */
209 ieee80211_tkip_add_iv(pos
, key
, key
->u
.tkip
.tx
.iv16
);
211 info
->control
.hw_key
= &tx
->key
->conf
;
215 /* Add room for ICV */
216 skb_put(skb
, TKIP_ICV_LEN
);
218 hdr
= (struct ieee80211_hdr
*) skb
->data
;
219 ieee80211_tkip_encrypt_data(tx
->local
->wep_tx_tfm
,
220 key
, pos
, len
, hdr
->addr2
);
226 ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data
*tx
)
228 struct sk_buff
*skb
= tx
->skb
;
230 ieee80211_tx_set_protected(tx
);
232 if (tkip_encrypt_skb(tx
, skb
) < 0)
235 if (tx
->extra_frag
) {
237 for (i
= 0; i
< tx
->num_extra_frag
; i
++) {
238 if (tkip_encrypt_skb(tx
, tx
->extra_frag
[i
]) < 0)
248 ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data
*rx
)
250 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) rx
->skb
->data
;
251 int hdrlen
, res
, hwaccel
= 0, wpa_test
= 0;
252 struct ieee80211_key
*key
= rx
->key
;
253 struct sk_buff
*skb
= rx
->skb
;
254 DECLARE_MAC_BUF(mac
);
256 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
258 if ((rx
->fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
)
261 if (!rx
->sta
|| skb
->len
- hdrlen
< 12)
262 return RX_DROP_UNUSABLE
;
264 if (rx
->status
->flag
& RX_FLAG_DECRYPTED
) {
265 if (rx
->status
->flag
& RX_FLAG_IV_STRIPPED
) {
267 * Hardware took care of all processing, including
268 * replay protection, and stripped the ICV/IV so
269 * we cannot do any checks here.
274 /* let TKIP code verify IV, but skip decryption */
278 res
= ieee80211_tkip_decrypt_data(rx
->local
->wep_rx_tfm
,
279 key
, skb
->data
+ hdrlen
,
280 skb
->len
- hdrlen
, rx
->sta
->addr
,
281 hdr
->addr1
, hwaccel
, rx
->queue
,
284 if (res
!= TKIP_DECRYPT_OK
|| wpa_test
)
285 return RX_DROP_UNUSABLE
;
288 skb_trim(skb
, skb
->len
- TKIP_ICV_LEN
);
291 memmove(skb
->data
+ TKIP_IV_LEN
, skb
->data
, hdrlen
);
292 skb_pull(skb
, TKIP_IV_LEN
);
298 static void ccmp_special_blocks(struct sk_buff
*skb
, u8
*pn
, u8
*b_0
, u8
*aad
,
306 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*)skb
->data
;
309 * Mask FC: zero subtype b4 b5 b6
310 * Retry, PwrMgt, MoreData; set Protected
312 mask_fc
= hdr
->frame_control
;
313 mask_fc
&= ~cpu_to_le16(0x0070 | IEEE80211_FCTL_RETRY
|
314 IEEE80211_FCTL_PM
| IEEE80211_FCTL_MOREDATA
);
315 mask_fc
|= cpu_to_le16(IEEE80211_FCTL_PROTECTED
);
317 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
319 a4_included
= ieee80211_has_a4(hdr
->frame_control
);
321 if (ieee80211_is_data_qos(hdr
->frame_control
))
322 qos_tid
= *ieee80211_get_qos_ctl(hdr
) & IEEE80211_QOS_CTL_TID_MASK
;
326 data_len
= skb
->len
- hdrlen
- CCMP_HDR_LEN
;
328 data_len
-= CCMP_MIC_LEN
;
330 /* First block, b_0 */
331 b_0
[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
332 /* Nonce: QoS Priority | A2 | PN */
334 memcpy(&b_0
[2], hdr
->addr2
, ETH_ALEN
);
335 memcpy(&b_0
[8], pn
, CCMP_PN_LEN
);
337 put_unaligned_be16(data_len
, &b_0
[14]);
339 /* AAD (extra authenticate-only data) / masked 802.11 header
340 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
341 put_unaligned_be16(len_a
, &aad
[0]);
342 put_unaligned(mask_fc
, (__le16
*)&aad
[2]);
343 memcpy(&aad
[4], &hdr
->addr1
, 3 * ETH_ALEN
);
345 /* Mask Seq#, leave Frag# */
346 aad
[22] = *((u8
*) &hdr
->seq_ctrl
) & 0x0f;
350 memcpy(&aad
[24], hdr
->addr4
, ETH_ALEN
);
354 memset(&aad
[24], 0, ETH_ALEN
+ IEEE80211_QOS_CTL_LEN
);
360 static inline void ccmp_pn2hdr(u8
*hdr
, u8
*pn
, int key_id
)
365 hdr
[3] = 0x20 | (key_id
<< 6);
373 static inline int ccmp_hdr2pn(u8
*pn
, u8
*hdr
)
381 return (hdr
[3] >> 6) & 0x03;
385 static int ccmp_encrypt_skb(struct ieee80211_tx_data
*tx
, struct sk_buff
*skb
)
387 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) skb
->data
;
388 struct ieee80211_key
*key
= tx
->key
;
389 struct ieee80211_tx_info
*info
= IEEE80211_SKB_CB(skb
);
390 int hdrlen
, len
, tail
;
391 u8
*pos
, *pn
, *b_0
, *aad
, *scratch
;
394 info
->control
.icv_len
= CCMP_MIC_LEN
;
395 info
->control
.iv_len
= CCMP_HDR_LEN
;
397 if ((tx
->key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
) &&
398 !(tx
->key
->conf
.flags
& IEEE80211_KEY_FLAG_GENERATE_IV
)) {
399 /* hwaccel - with no need for preallocated room for CCMP "
400 * header or MIC fields */
401 info
->control
.hw_key
= &tx
->key
->conf
;
405 scratch
= key
->u
.ccmp
.tx_crypto_buf
;
406 b_0
= scratch
+ 3 * AES_BLOCK_LEN
;
407 aad
= scratch
+ 4 * AES_BLOCK_LEN
;
409 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
410 len
= skb
->len
- hdrlen
;
412 if (key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
)
417 if (WARN_ON(skb_tailroom(skb
) < tail
||
418 skb_headroom(skb
) < CCMP_HDR_LEN
))
421 pos
= skb_push(skb
, CCMP_HDR_LEN
);
422 memmove(pos
, pos
+ CCMP_HDR_LEN
, hdrlen
);
423 hdr
= (struct ieee80211_hdr
*) pos
;
427 pn
= key
->u
.ccmp
.tx_pn
;
429 for (i
= CCMP_PN_LEN
- 1; i
>= 0; i
--) {
435 ccmp_pn2hdr(pos
, pn
, key
->conf
.keyidx
);
437 if (key
->flags
& KEY_FLAG_UPLOADED_TO_HARDWARE
) {
438 /* hwaccel - with preallocated room for CCMP header */
439 info
->control
.hw_key
= &tx
->key
->conf
;
444 ccmp_special_blocks(skb
, pn
, b_0
, aad
, 0);
445 ieee80211_aes_ccm_encrypt(key
->u
.ccmp
.tfm
, scratch
, b_0
, aad
, pos
, len
,
446 pos
, skb_put(skb
, CCMP_MIC_LEN
));
453 ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data
*tx
)
455 struct sk_buff
*skb
= tx
->skb
;
457 ieee80211_tx_set_protected(tx
);
459 if (ccmp_encrypt_skb(tx
, skb
) < 0)
462 if (tx
->extra_frag
) {
464 for (i
= 0; i
< tx
->num_extra_frag
; i
++) {
465 if (ccmp_encrypt_skb(tx
, tx
->extra_frag
[i
]) < 0)
475 ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data
*rx
)
477 struct ieee80211_hdr
*hdr
= (struct ieee80211_hdr
*) rx
->skb
->data
;
479 struct ieee80211_key
*key
= rx
->key
;
480 struct sk_buff
*skb
= rx
->skb
;
483 DECLARE_MAC_BUF(mac
);
485 hdrlen
= ieee80211_hdrlen(hdr
->frame_control
);
487 if ((rx
->fc
& IEEE80211_FCTL_FTYPE
) != IEEE80211_FTYPE_DATA
)
490 data_len
= skb
->len
- hdrlen
- CCMP_HDR_LEN
- CCMP_MIC_LEN
;
491 if (!rx
->sta
|| data_len
< 0)
492 return RX_DROP_UNUSABLE
;
494 if ((rx
->status
->flag
& RX_FLAG_DECRYPTED
) &&
495 (rx
->status
->flag
& RX_FLAG_IV_STRIPPED
))
498 (void) ccmp_hdr2pn(pn
, skb
->data
+ hdrlen
);
500 if (memcmp(pn
, key
->u
.ccmp
.rx_pn
[rx
->queue
], CCMP_PN_LEN
) <= 0) {
501 key
->u
.ccmp
.replays
++;
502 return RX_DROP_UNUSABLE
;
505 if (!(rx
->status
->flag
& RX_FLAG_DECRYPTED
)) {
506 /* hardware didn't decrypt/verify MIC */
507 u8
*scratch
, *b_0
, *aad
;
509 scratch
= key
->u
.ccmp
.rx_crypto_buf
;
510 b_0
= scratch
+ 3 * AES_BLOCK_LEN
;
511 aad
= scratch
+ 4 * AES_BLOCK_LEN
;
513 ccmp_special_blocks(skb
, pn
, b_0
, aad
, 1);
515 if (ieee80211_aes_ccm_decrypt(
516 key
->u
.ccmp
.tfm
, scratch
, b_0
, aad
,
517 skb
->data
+ hdrlen
+ CCMP_HDR_LEN
, data_len
,
518 skb
->data
+ skb
->len
- CCMP_MIC_LEN
,
519 skb
->data
+ hdrlen
+ CCMP_HDR_LEN
)) {
520 return RX_DROP_UNUSABLE
;
524 memcpy(key
->u
.ccmp
.rx_pn
[rx
->queue
], pn
, CCMP_PN_LEN
);
526 /* Remove CCMP header and MIC */
527 skb_trim(skb
, skb
->len
- CCMP_MIC_LEN
);
528 memmove(skb
->data
+ CCMP_HDR_LEN
, skb
->data
, hdrlen
);
529 skb_pull(skb
, CCMP_HDR_LEN
);