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/mac80211.h>
17
#include "ieee80211_common.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;
33
hdr = (struct ieee80211_hdr *) skb->data;
34
fc = le16_to_cpu(hdr->frame_control);
37
if ((fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) ==
38
(IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
42
} else if (fc & IEEE80211_FCTL_FROMDS) {
45
} else if (fc & IEEE80211_FCTL_TODS) {
56
*data = skb->data + hdrlen;
57
*data_len = skb->len - hdrlen;
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;
66
*qos_tid = pos[0] & 0x0f;
67
*qos_tid |= 0x80; /* qos_included flag */
71
return skb->len < hdrlen ? -1 : 0;
76
ieee80211_tx_h_michael_mic_add(struct ieee80211_txrx_data *tx)
78
u8 *data, *sa, *da, *key, *mic, qos_tid;
81
struct sk_buff *skb = tx->skb;
87
if (!tx->key || tx->key->alg != ALG_TKIP || skb->len < 24 ||
88
!WLAN_FC_DATA_PRESENT(fc))
91
if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len))
94
if (!tx->key->force_sw_encrypt &&
96
!(tx->local->hw.flags & IEEE80211_HW_TKIP_INCLUDE_MMIC) &&
98
/* hwaccel - with no need for preallocated room for Michael MIC
100
return TXRX_CONTINUE;
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,
108
printk(KERN_DEBUG "%s: failed to allocate more memory "
109
"for Michael MIC\n", tx->dev->name);
115
authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
119
key = &tx->key->key[authenticator ? ALG_TKIP_TEMP_AUTH_TX_MIC_KEY :
120
ALG_TKIP_TEMP_AUTH_RX_MIC_KEY];
121
mic = skb_put(skb, MICHAEL_MIC_LEN);
122
michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
124
return TXRX_CONTINUE;
128
ieee80211_txrx_result
129
ieee80211_rx_h_michael_mic_verify(struct ieee80211_txrx_data *rx)
131
u8 *data, *sa, *da, *key = NULL, qos_tid;
134
u8 mic[MICHAEL_MIC_LEN];
135
struct sk_buff *skb = rx->skb;
136
int authenticator = 1, wpa_test = 0;
140
/* If device handles decryption totally, skip this check */
141
if ((rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP) ||
142
(rx->local->hw.flags & IEEE80211_HW_DEVICE_STRIPS_MIC))
143
return TXRX_CONTINUE;
145
if (!rx->key || rx->key->alg != ALG_TKIP ||
146
!(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc))
147
return TXRX_CONTINUE;
149
if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
150
!rx->key->force_sw_encrypt) {
151
if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
152
if (skb->len < MICHAEL_MIC_LEN)
155
/* Need to verify Michael MIC sometimes in software even when
156
* hwaccel is used. Atheros ar5212: fragmented frames and QoS
158
if (!rx->fragmented && !wpa_test)
162
if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len)
163
|| data_len < MICHAEL_MIC_LEN)
166
data_len -= MICHAEL_MIC_LEN;
169
authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
173
key = &rx->key->key[authenticator ? ALG_TKIP_TEMP_AUTH_RX_MIC_KEY :
174
ALG_TKIP_TEMP_AUTH_TX_MIC_KEY];
175
michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
176
if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
177
if (!rx->u.rx.ra_match)
180
printk(KERN_DEBUG "%s: invalid Michael MIC in data frame from "
181
MAC_FMT "\n", rx->dev->name, MAC_ARG(sa));
184
struct ieee80211_hdr *hdr;
185
union iwreq_data wrqu;
186
char *buf = kmalloc(128, GFP_ATOMIC);
190
/* TODO: needed parameters: count, key type, TSC */
191
hdr = (struct ieee80211_hdr *) skb->data;
192
sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
193
"keyid=%d %scast addr=" MAC_FMT ")",
195
hdr->addr1[0] & 0x01 ? "broad" : "uni",
196
MAC_ARG(hdr->addr2));
197
memset(&wrqu, 0, sizeof(wrqu));
198
wrqu.data.length = strlen(buf);
199
wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
203
if (!rx->local->apdev)
206
ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
207
ieee80211_msg_michael_mic_failure);
213
/* remove Michael MIC from payload */
214
skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
216
return TXRX_CONTINUE;
220
static int tkip_encrypt_skb(struct ieee80211_txrx_data *tx,
221
struct sk_buff *skb, int test)
223
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
224
struct ieee80211_key *key = tx->key;
225
int hdrlen, len, tailneed;
229
fc = le16_to_cpu(hdr->frame_control);
230
hdrlen = iwlwifi_ieee80211_get_hdrlen(fc);
231
len = skb->len - hdrlen;
233
tailneed = !tx->key->force_sw_encrypt ? 0 : TKIP_ICV_LEN;
234
if ((skb_headroom(skb) < TKIP_IV_LEN ||
235
skb_tailroom(skb) < tailneed)) {
236
I802_DEBUG_INC(tx->local->tx_expand_skb_head);
237
if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN, tailneed,
242
pos = skb_push(skb, TKIP_IV_LEN);
243
memmove(pos, pos + TKIP_IV_LEN, hdrlen);
246
/* Increase IV for the frame */
248
if (key->u.tkip.iv16 == 0)
251
if (!tx->key->force_sw_encrypt) {
252
u32 flags = tx->local->hw.flags;
253
hdr = (struct ieee80211_hdr *)skb->data;
255
/* hwaccel - with preallocated room for IV */
256
ieee80211_tkip_add_iv(pos, key,
257
(u8) (key->u.tkip.iv16 >> 8),
258
(u8) (((key->u.tkip.iv16 >> 8) | 0x20) &
260
(u8) key->u.tkip.iv16);
262
if (flags & IEEE80211_HW_TKIP_REQ_PHASE2_KEY)
263
ieee80211_tkip_gen_rc4key(key, hdr->addr2,
264
tx->u.tx.control->tkip_key);
265
else if (flags & IEEE80211_HW_TKIP_REQ_PHASE1_KEY) {
266
if (key->u.tkip.iv16 == 0 ||
267
!key->u.tkip.tx_initialized) {
268
ieee80211_tkip_gen_phase1key(key, hdr->addr2,
269
(u16 *)tx->u.tx.control->tkip_key);
270
key->u.tkip.tx_initialized = 1;
271
tx->u.tx.control->flags |=
272
IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY;
274
tx->u.tx.control->flags &=
275
~IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY;
278
tx->u.tx.control->key_idx = tx->key->hw_key_idx;
282
/* Add room for ICV */
283
skb_put(skb, TKIP_ICV_LEN);
285
hdr = (struct ieee80211_hdr *) skb->data;
286
ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
287
key, pos, len, hdr->addr2);
292
ieee80211_txrx_result
293
ieee80211_tx_h_tkip_encrypt(struct ieee80211_txrx_data *tx)
295
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
297
struct ieee80211_key *key = tx->key;
298
struct sk_buff *skb = tx->skb;
299
int wpa_test = 0, test = 0;
301
fc = le16_to_cpu(hdr->frame_control);
303
if (!key || key->alg != ALG_TKIP || !WLAN_FC_DATA_PRESENT(fc))
304
return TXRX_CONTINUE;
306
tx->u.tx.control->icv_len = TKIP_ICV_LEN;
307
tx->u.tx.control->iv_len = TKIP_IV_LEN;
308
ieee80211_tx_set_iswep(tx);
310
if (!tx->key->force_sw_encrypt &&
311
!(tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
313
/* hwaccel - with no need for preallocated room for IV/ICV */
314
tx->u.tx.control->key_idx = tx->key->hw_key_idx;
315
return TXRX_CONTINUE;
318
if (tkip_encrypt_skb(tx, skb, test) < 0)
321
if (tx->u.tx.extra_frag) {
323
for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
324
if (tkip_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
330
return TXRX_CONTINUE;
334
ieee80211_txrx_result
335
ieee80211_rx_h_tkip_decrypt(struct ieee80211_txrx_data *rx)
337
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
339
int hdrlen, res, hwaccel = 0, wpa_test = 0;
340
struct ieee80211_key *key = rx->key;
341
struct sk_buff *skb = rx->skb;
343
fc = le16_to_cpu(hdr->frame_control);
344
hdrlen = iwlwifi_ieee80211_get_hdrlen(fc);
346
if (!rx->key || rx->key->alg != ALG_TKIP ||
347
!(rx->fc & IEEE80211_FCTL_PROTECTED) ||
348
(rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
349
return TXRX_CONTINUE;
351
if (!rx->sta || skb->len - hdrlen < 12)
354
if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
355
!rx->key->force_sw_encrypt) {
356
if (!(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV)) {
357
/* Hardware takes care of all processing, including
358
* replay protection, so no need to continue here. */
359
return TXRX_CONTINUE;
362
/* let TKIP code verify IV, but skip decryption */
366
res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
367
key, skb->data + hdrlen,
368
skb->len - hdrlen, rx->sta->addr,
369
hwaccel, rx->u.rx.queue);
370
if (res != TKIP_DECRYPT_OK || wpa_test) {
371
printk(KERN_DEBUG "%s: TKIP decrypt failed for RX frame from "
372
MAC_FMT " (res=%d)\n",
373
rx->dev->name, MAC_ARG(rx->sta->addr), res);
378
skb_trim(skb, skb->len - TKIP_ICV_LEN);
381
memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
382
skb_pull(skb, TKIP_IV_LEN);
384
return TXRX_CONTINUE;
388
static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
392
int a4_included, qos_included;
393
u8 qos_tid, *fc_pos, *data, *sa, *da;
396
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
398
fc_pos = (u8 *) &hdr->frame_control;
399
fc = fc_pos[0] ^ (fc_pos[1] << 8);
400
a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
401
(IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
403
ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len);
404
data_len -= CCMP_HDR_LEN + (encrypted ? CCMP_MIC_LEN : 0);
405
if (qos_tid & 0x80) {
410
/* First block, b_0 */
412
b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
413
/* Nonce: QoS Priority | A2 | PN */
415
memcpy(&b_0[2], hdr->addr2, 6);
416
memcpy(&b_0[8], pn, CCMP_PN_LEN);
418
b_0[14] = (data_len >> 8) & 0xff;
419
b_0[15] = data_len & 0xff;
422
/* AAD (extra authenticate-only data) / masked 802.11 header
423
* FC | A1 | A2 | A3 | SC | [A4] | [QC] */
425
len_a = a4_included ? 28 : 22;
429
aad[0] = 0; /* (len_a >> 8) & 0xff; */
430
aad[1] = len_a & 0xff;
431
/* Mask FC: zero subtype b4 b5 b6 */
432
aad[2] = fc_pos[0] & ~(BIT(4) | BIT(5) | BIT(6));
433
/* Retry, PwrMgt, MoreData; set Protected */
434
aad[3] = (fc_pos[1] & ~(BIT(3) | BIT(4) | BIT(5))) | BIT(6);
435
memcpy(&aad[4], &hdr->addr1, 18);
437
/* Mask Seq#, leave Frag# */
438
aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
441
memcpy(&aad[24], hdr->addr4, 6);
445
memset(&aad[24], 0, 8);
447
u8 *dpos = &aad[a4_included ? 30 : 24];
449
/* Mask QoS Control field */
456
static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
461
hdr[3] = 0x20 | (key_id << 6);
469
static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
477
return (hdr[3] >> 6) & 0x03;
481
static int ccmp_encrypt_skb(struct ieee80211_txrx_data *tx,
482
struct sk_buff *skb, int test)
484
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
485
struct ieee80211_key *key = tx->key;
486
int hdrlen, len, tailneed;
488
u8 *pos, *pn, *b_0, *aad, *scratch;
491
scratch = key->u.ccmp.tx_crypto_buf;
492
b_0 = scratch + 3 * AES_BLOCK_LEN;
493
aad = scratch + 4 * AES_BLOCK_LEN;
495
fc = le16_to_cpu(hdr->frame_control);
496
hdrlen = iwlwifi_ieee80211_get_hdrlen(fc);
497
len = skb->len - hdrlen;
499
tailneed = !key->force_sw_encrypt ? 0 : CCMP_MIC_LEN;
501
if ((skb_headroom(skb) < CCMP_HDR_LEN ||
502
skb_tailroom(skb) < tailneed)) {
503
I802_DEBUG_INC(tx->local->tx_expand_skb_head);
504
if (unlikely(pskb_expand_head(skb, CCMP_HDR_LEN, tailneed,
509
pos = skb_push(skb, CCMP_HDR_LEN);
510
memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
511
hdr = (struct ieee80211_hdr *) pos;
515
pn = key->u.ccmp.tx_pn;
517
for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
523
ccmp_pn2hdr(pos, pn, key->keyidx);
525
if (!key->force_sw_encrypt) {
526
/* hwaccel - with preallocated room for CCMP header */
527
tx->u.tx.control->key_idx = key->hw_key_idx;
532
ccmp_special_blocks(skb, pn, b_0, aad, 0);
533
ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
534
pos, skb_put(skb, CCMP_MIC_LEN));
540
ieee80211_txrx_result
541
ieee80211_tx_h_ccmp_encrypt(struct ieee80211_txrx_data *tx)
543
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
544
struct ieee80211_key *key = tx->key;
546
struct sk_buff *skb = tx->skb;
549
fc = le16_to_cpu(hdr->frame_control);
551
if (!key || key->alg != ALG_CCMP || !WLAN_FC_DATA_PRESENT(fc))
552
return TXRX_CONTINUE;
554
tx->u.tx.control->icv_len = CCMP_MIC_LEN;
555
tx->u.tx.control->iv_len = CCMP_HDR_LEN;
556
ieee80211_tx_set_iswep(tx);
558
if (!tx->key->force_sw_encrypt &&
559
!(tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV)) {
560
/* hwaccel - with no need for preallocated room for CCMP "
561
* header or MIC fields */
562
tx->u.tx.control->key_idx = tx->key->hw_key_idx;
563
return TXRX_CONTINUE;
566
if (ccmp_encrypt_skb(tx, skb, test) < 0)
569
if (tx->u.tx.extra_frag) {
572
for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
573
if (ccmp_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
579
return TXRX_CONTINUE;
583
ieee80211_txrx_result
584
ieee80211_rx_h_ccmp_decrypt(struct ieee80211_txrx_data *rx)
586
struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
589
struct ieee80211_key *key = rx->key;
590
struct sk_buff *skb = rx->skb;
594
fc = le16_to_cpu(hdr->frame_control);
595
hdrlen = iwlwifi_ieee80211_get_hdrlen(fc);
597
if (!key || key->alg != ALG_CCMP ||
598
!(rx->fc & IEEE80211_FCTL_PROTECTED) ||
599
(rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
600
return TXRX_CONTINUE;
602
data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
603
if (!rx->sta || data_len < 0)
606
if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
607
!key->force_sw_encrypt &&
608
!(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV))
609
return TXRX_CONTINUE;
611
(void) ccmp_hdr2pn(pn, skb->data + hdrlen);
613
if (memcmp(pn, key->u.ccmp.rx_pn[rx->u.rx.queue], CCMP_PN_LEN) <= 0) {
614
#ifdef CONFIG_MAC80211_DEBUG
615
u8 *ppn = key->u.ccmp.rx_pn[rx->u.rx.queue];
616
printk(KERN_DEBUG "%s: CCMP replay detected for RX frame from "
617
MAC_FMT " (RX PN %02x%02x%02x%02x%02x%02x <= prev. PN "
618
"%02x%02x%02x%02x%02x%02x)\n", rx->dev->name,
619
MAC_ARG(rx->sta->addr),
620
pn[0], pn[1], pn[2], pn[3], pn[4], pn[5],
621
ppn[0], ppn[1], ppn[2], ppn[3], ppn[4], ppn[5]);
622
#endif /* CONFIG_MAC80211_DEBUG */
623
key->u.ccmp.replays++;
627
if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
628
!key->force_sw_encrypt) {
629
/* hwaccel has already decrypted frame and verified MIC */
631
u8 *scratch, *b_0, *aad;
633
scratch = key->u.ccmp.rx_crypto_buf;
634
b_0 = scratch + 3 * AES_BLOCK_LEN;
635
aad = scratch + 4 * AES_BLOCK_LEN;
637
ccmp_special_blocks(skb, pn, b_0, aad, 1);
639
if (ieee80211_aes_ccm_decrypt(
640
key->u.ccmp.tfm, scratch, b_0, aad,
641
skb->data + hdrlen + CCMP_HDR_LEN, data_len,
642
skb->data + skb->len - CCMP_MIC_LEN,
643
skb->data + hdrlen + CCMP_HDR_LEN)) {
644
printk(KERN_DEBUG "%s: CCMP decrypt failed for RX "
645
"frame from " MAC_FMT "\n", rx->dev->name,
646
MAC_ARG(rx->sta->addr));
651
memcpy(key->u.ccmp.rx_pn[rx->u.rx.queue], pn, CCMP_PN_LEN);
653
/* Remove CCMP header and MIC */
654
skb_trim(skb, skb->len - CCMP_MIC_LEN);
655
memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
656
skb_pull(skb, CCMP_HDR_LEN);
658
return TXRX_CONTINUE;