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;
141 * No way to verify the MIC if the hardware stripped it
143 if (rx->local->hw.flags & IEEE80211_HW_DEVICE_STRIPS_MIC)
144 return TXRX_CONTINUE;
146 if (!rx->key || rx->key->alg != ALG_TKIP ||
147 !(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc))
148 return TXRX_CONTINUE;
150 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
151 !rx->key->force_sw_encrypt) {
152 if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
153 if (skb->len < MICHAEL_MIC_LEN)
156 /* Need to verify Michael MIC sometimes in software even when
157 * hwaccel is used. Atheros ar5212: fragmented frames and QoS
159 if (!rx->fragmented && !wpa_test)
163 if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len)
164 || data_len < MICHAEL_MIC_LEN)
167 data_len -= MICHAEL_MIC_LEN;
170 authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
174 key = &rx->key->key[authenticator ? ALG_TKIP_TEMP_AUTH_RX_MIC_KEY :
175 ALG_TKIP_TEMP_AUTH_TX_MIC_KEY];
176 michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
177 if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
178 if (!rx->u.rx.ra_match)
181 printk(KERN_DEBUG "%s: invalid Michael MIC in data frame from "
182 MAC_FMT "\n", rx->dev->name, MAC_ARG(sa));
185 struct ieee80211_hdr *hdr;
186 union iwreq_data wrqu;
187 char *buf = kmalloc(128, GFP_ATOMIC);
191 /* TODO: needed parameters: count, key type, TSC */
192 hdr = (struct ieee80211_hdr *) skb->data;
193 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
194 "keyid=%d %scast addr=" MAC_FMT ")",
196 hdr->addr1[0] & 0x01 ? "broad" : "uni",
197 MAC_ARG(hdr->addr2));
198 memset(&wrqu, 0, sizeof(wrqu));
199 wrqu.data.length = strlen(buf);
200 wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
204 if (!rx->local->apdev)
207 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
208 ieee80211_msg_michael_mic_failure);
214 /* remove Michael MIC from payload */
215 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
217 return TXRX_CONTINUE;
221 static int tkip_encrypt_skb(struct ieee80211_txrx_data *tx,
222 struct sk_buff *skb, int test)
224 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
225 struct ieee80211_key *key = tx->key;
226 int hdrlen, len, tailneed;
230 fc = le16_to_cpu(hdr->frame_control);
231 hdrlen = ieee80211_get_hdrlen(fc);
232 len = skb->len - hdrlen;
234 tailneed = !tx->key->force_sw_encrypt ? 0 : TKIP_ICV_LEN;
235 if ((skb_headroom(skb) < TKIP_IV_LEN ||
236 skb_tailroom(skb) < tailneed)) {
237 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
238 if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN, tailneed,
243 pos = skb_push(skb, TKIP_IV_LEN);
244 memmove(pos, pos + TKIP_IV_LEN, hdrlen);
247 /* Increase IV for the frame */
249 if (key->u.tkip.iv16 == 0)
252 if (!tx->key->force_sw_encrypt) {
253 u32 flags = tx->local->hw.flags;
254 hdr = (struct ieee80211_hdr *)skb->data;
256 /* hwaccel - with preallocated room for IV */
257 ieee80211_tkip_add_iv(pos, key,
258 (u8) (key->u.tkip.iv16 >> 8),
259 (u8) (((key->u.tkip.iv16 >> 8) | 0x20) &
261 (u8) key->u.tkip.iv16);
263 if (flags & IEEE80211_HW_TKIP_REQ_PHASE2_KEY)
264 ieee80211_tkip_gen_rc4key(key, hdr->addr2,
265 tx->u.tx.control->tkip_key);
266 else if (flags & IEEE80211_HW_TKIP_REQ_PHASE1_KEY) {
267 if (key->u.tkip.iv16 == 0 ||
268 !key->u.tkip.tx_initialized) {
269 ieee80211_tkip_gen_phase1key(key, hdr->addr2,
270 (u16 *)tx->u.tx.control->tkip_key);
271 key->u.tkip.tx_initialized = 1;
272 tx->u.tx.control->flags |=
273 IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY;
275 tx->u.tx.control->flags &=
276 ~IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY;
279 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
283 /* Add room for ICV */
284 skb_put(skb, TKIP_ICV_LEN);
286 hdr = (struct ieee80211_hdr *) skb->data;
287 ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
288 key, pos, len, hdr->addr2);
293 ieee80211_txrx_result
294 ieee80211_tx_h_tkip_encrypt(struct ieee80211_txrx_data *tx)
296 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
298 struct ieee80211_key *key = tx->key;
299 struct sk_buff *skb = tx->skb;
300 int wpa_test = 0, test = 0;
302 fc = le16_to_cpu(hdr->frame_control);
304 if (!key || key->alg != ALG_TKIP || !WLAN_FC_DATA_PRESENT(fc))
305 return TXRX_CONTINUE;
307 tx->u.tx.control->icv_len = TKIP_ICV_LEN;
308 tx->u.tx.control->iv_len = TKIP_IV_LEN;
309 ieee80211_tx_set_iswep(tx);
311 if (!tx->key->force_sw_encrypt &&
312 !(tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
314 /* hwaccel - with no need for preallocated room for IV/ICV */
315 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
316 return TXRX_CONTINUE;
319 if (tkip_encrypt_skb(tx, skb, test) < 0)
322 if (tx->u.tx.extra_frag) {
324 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
325 if (tkip_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
331 return TXRX_CONTINUE;
335 ieee80211_txrx_result
336 ieee80211_rx_h_tkip_decrypt(struct ieee80211_txrx_data *rx)
338 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
340 int hdrlen, res, hwaccel = 0, wpa_test = 0;
341 struct ieee80211_key *key = rx->key;
342 struct sk_buff *skb = rx->skb;
344 fc = le16_to_cpu(hdr->frame_control);
345 hdrlen = ieee80211_get_hdrlen(fc);
347 if (!rx->key || rx->key->alg != ALG_TKIP ||
348 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
349 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
350 return TXRX_CONTINUE;
352 if (!rx->sta || skb->len - hdrlen < 12)
355 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
356 !rx->key->force_sw_encrypt) {
357 if (!(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV)) {
358 /* Hardware takes care of all processing, including
359 * replay protection, so no need to continue here. */
360 return TXRX_CONTINUE;
363 /* let TKIP code verify IV, but skip decryption */
367 res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
368 key, skb->data + hdrlen,
369 skb->len - hdrlen, rx->sta->addr,
370 hwaccel, rx->u.rx.queue);
371 if (res != TKIP_DECRYPT_OK || wpa_test) {
372 printk(KERN_DEBUG "%s: TKIP decrypt failed for RX frame from "
373 MAC_FMT " (res=%d)\n",
374 rx->dev->name, MAC_ARG(rx->sta->addr), res);
379 skb_trim(skb, skb->len - TKIP_ICV_LEN);
382 memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
383 skb_pull(skb, TKIP_IV_LEN);
385 return TXRX_CONTINUE;
389 static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
393 int a4_included, qos_included;
394 u8 qos_tid, *fc_pos, *data, *sa, *da;
397 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
399 fc_pos = (u8 *) &hdr->frame_control;
400 fc = fc_pos[0] ^ (fc_pos[1] << 8);
401 a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
402 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
404 ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len);
405 data_len -= CCMP_HDR_LEN + (encrypted ? CCMP_MIC_LEN : 0);
406 if (qos_tid & 0x80) {
411 /* First block, b_0 */
413 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
414 /* Nonce: QoS Priority | A2 | PN */
416 memcpy(&b_0[2], hdr->addr2, 6);
417 memcpy(&b_0[8], pn, CCMP_PN_LEN);
419 b_0[14] = (data_len >> 8) & 0xff;
420 b_0[15] = data_len & 0xff;
423 /* AAD (extra authenticate-only data) / masked 802.11 header
424 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
426 len_a = a4_included ? 28 : 22;
430 aad[0] = 0; /* (len_a >> 8) & 0xff; */
431 aad[1] = len_a & 0xff;
432 /* Mask FC: zero subtype b4 b5 b6 */
433 aad[2] = fc_pos[0] & ~(BIT(4) | BIT(5) | BIT(6));
434 /* Retry, PwrMgt, MoreData; set Protected */
435 aad[3] = (fc_pos[1] & ~(BIT(3) | BIT(4) | BIT(5))) | BIT(6);
436 memcpy(&aad[4], &hdr->addr1, 18);
438 /* Mask Seq#, leave Frag# */
439 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
442 memcpy(&aad[24], hdr->addr4, 6);
446 memset(&aad[24], 0, 8);
448 u8 *dpos = &aad[a4_included ? 30 : 24];
450 /* Mask QoS Control field */
457 static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
462 hdr[3] = 0x20 | (key_id << 6);
470 static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
478 return (hdr[3] >> 6) & 0x03;
482 static int ccmp_encrypt_skb(struct ieee80211_txrx_data *tx,
483 struct sk_buff *skb, int test)
485 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
486 struct ieee80211_key *key = tx->key;
487 int hdrlen, len, tailneed;
489 u8 *pos, *pn, *b_0, *aad, *scratch;
492 scratch = key->u.ccmp.tx_crypto_buf;
493 b_0 = scratch + 3 * AES_BLOCK_LEN;
494 aad = scratch + 4 * AES_BLOCK_LEN;
496 fc = le16_to_cpu(hdr->frame_control);
497 hdrlen = ieee80211_get_hdrlen(fc);
498 len = skb->len - hdrlen;
500 tailneed = !key->force_sw_encrypt ? 0 : CCMP_MIC_LEN;
502 if ((skb_headroom(skb) < CCMP_HDR_LEN ||
503 skb_tailroom(skb) < tailneed)) {
504 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
505 if (unlikely(pskb_expand_head(skb, CCMP_HDR_LEN, tailneed,
510 pos = skb_push(skb, CCMP_HDR_LEN);
511 memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
512 hdr = (struct ieee80211_hdr *) pos;
516 pn = key->u.ccmp.tx_pn;
518 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
524 ccmp_pn2hdr(pos, pn, key->keyidx);
526 if (!key->force_sw_encrypt) {
527 /* hwaccel - with preallocated room for CCMP header */
528 tx->u.tx.control->key_idx = key->hw_key_idx;
533 ccmp_special_blocks(skb, pn, b_0, aad, 0);
534 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
535 pos, skb_put(skb, CCMP_MIC_LEN));
541 ieee80211_txrx_result
542 ieee80211_tx_h_ccmp_encrypt(struct ieee80211_txrx_data *tx)
544 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
545 struct ieee80211_key *key = tx->key;
547 struct sk_buff *skb = tx->skb;
550 fc = le16_to_cpu(hdr->frame_control);
552 if (!key || key->alg != ALG_CCMP || !WLAN_FC_DATA_PRESENT(fc))
553 return TXRX_CONTINUE;
555 tx->u.tx.control->icv_len = CCMP_MIC_LEN;
556 tx->u.tx.control->iv_len = CCMP_HDR_LEN;
557 ieee80211_tx_set_iswep(tx);
559 if (!tx->key->force_sw_encrypt &&
560 !(tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV)) {
561 /* hwaccel - with no need for preallocated room for CCMP "
562 * header or MIC fields */
563 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
564 return TXRX_CONTINUE;
567 if (ccmp_encrypt_skb(tx, skb, test) < 0)
570 if (tx->u.tx.extra_frag) {
573 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
574 if (ccmp_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
580 return TXRX_CONTINUE;
584 ieee80211_txrx_result
585 ieee80211_rx_h_ccmp_decrypt(struct ieee80211_txrx_data *rx)
587 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
590 struct ieee80211_key *key = rx->key;
591 struct sk_buff *skb = rx->skb;
595 fc = le16_to_cpu(hdr->frame_control);
596 hdrlen = ieee80211_get_hdrlen(fc);
598 if (!key || key->alg != ALG_CCMP ||
599 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
600 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
601 return TXRX_CONTINUE;
603 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
604 if (!rx->sta || data_len < 0)
607 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
608 !key->force_sw_encrypt &&
609 !(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV))
610 return TXRX_CONTINUE;
612 (void) ccmp_hdr2pn(pn, skb->data + hdrlen);
614 if (memcmp(pn, key->u.ccmp.rx_pn[rx->u.rx.queue], CCMP_PN_LEN) <= 0) {
615 #ifdef CONFIG_MAC80211_DEBUG
616 u8 *ppn = key->u.ccmp.rx_pn[rx->u.rx.queue];
617 printk(KERN_DEBUG "%s: CCMP replay detected for RX frame from "
618 MAC_FMT " (RX PN %02x%02x%02x%02x%02x%02x <= prev. PN "
619 "%02x%02x%02x%02x%02x%02x)\n", rx->dev->name,
620 MAC_ARG(rx->sta->addr),
621 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5],
622 ppn[0], ppn[1], ppn[2], ppn[3], ppn[4], ppn[5]);
623 #endif /* CONFIG_MAC80211_DEBUG */
624 key->u.ccmp.replays++;
628 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
629 !key->force_sw_encrypt) {
630 /* hwaccel has already decrypted frame and verified MIC */
632 u8 *scratch, *b_0, *aad;
634 scratch = key->u.ccmp.rx_crypto_buf;
635 b_0 = scratch + 3 * AES_BLOCK_LEN;
636 aad = scratch + 4 * AES_BLOCK_LEN;
638 ccmp_special_blocks(skb, pn, b_0, aad, 1);
640 if (ieee80211_aes_ccm_decrypt(
641 key->u.ccmp.tfm, scratch, b_0, aad,
642 skb->data + hdrlen + CCMP_HDR_LEN, data_len,
643 skb->data + skb->len - CCMP_MIC_LEN,
644 skb->data + hdrlen + CCMP_HDR_LEN)) {
645 printk(KERN_DEBUG "%s: CCMP decrypt failed for RX "
646 "frame from " MAC_FMT "\n", rx->dev->name,
647 MAC_ARG(rx->sta->addr));
652 memcpy(key->u.ccmp.rx_pn[rx->u.rx.queue], pn, CCMP_PN_LEN);
654 /* Remove CCMP header and MIC */
655 skb_trim(skb, skb->len - CCMP_MIC_LEN);
656 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
657 skb_pull(skb, CCMP_HDR_LEN);
659 return TXRX_CONTINUE;