aboutsummaryrefslogtreecommitdiffstats
path: root/net/mac80211/wpa.c
diff options
context:
space:
mode:
Diffstat (limited to 'net/mac80211/wpa.c')
-rw-r--r--net/mac80211/wpa.c180
1 files changed, 152 insertions, 28 deletions
diff --git a/net/mac80211/wpa.c b/net/mac80211/wpa.c
index 7aa63caf8d50..4f8bfea278f2 100644
--- a/net/mac80211/wpa.c
+++ b/net/mac80211/wpa.c
@@ -1,5 +1,6 @@
1/* 1/*
2 * Copyright 2002-2004, Instant802 Networks, Inc. 2 * Copyright 2002-2004, Instant802 Networks, Inc.
3 * Copyright 2008, Jouni Malinen <j@w1.fi>
3 * 4 *
4 * This program is free software; you can redistribute it and/or modify 5 * 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 * it under the terms of the GNU General Public License version 2 as
@@ -19,6 +20,7 @@
19#include "michael.h" 20#include "michael.h"
20#include "tkip.h" 21#include "tkip.h"
21#include "aes_ccm.h" 22#include "aes_ccm.h"
23#include "aes_cmac.h"
22#include "wpa.h" 24#include "wpa.h"
23 25
24ieee80211_tx_result 26ieee80211_tx_result
@@ -194,19 +196,13 @@ ieee80211_tx_result
194ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx) 196ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
195{ 197{
196 struct sk_buff *skb = tx->skb; 198 struct sk_buff *skb = tx->skb;
197 int i;
198 199
199 ieee80211_tx_set_protected(tx); 200 ieee80211_tx_set_protected(tx);
200 201
201 if (tkip_encrypt_skb(tx, skb) < 0) 202 do {
202 return TX_DROP; 203 if (tkip_encrypt_skb(tx, skb) < 0)
203 204 return TX_DROP;
204 if (tx->extra_frag) { 205 } while ((skb = skb->next));
205 for (i = 0; i < tx->num_extra_frag; i++) {
206 if (tkip_encrypt_skb(tx, tx->extra_frag[i]))
207 return TX_DROP;
208 }
209 }
210 206
211 return TX_CONTINUE; 207 return TX_CONTINUE;
212} 208}
@@ -266,7 +262,7 @@ static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
266 int encrypted) 262 int encrypted)
267{ 263{
268 __le16 mask_fc; 264 __le16 mask_fc;
269 int a4_included; 265 int a4_included, mgmt;
270 u8 qos_tid; 266 u8 qos_tid;
271 u8 *b_0, *aad; 267 u8 *b_0, *aad;
272 u16 data_len, len_a; 268 u16 data_len, len_a;
@@ -277,12 +273,15 @@ static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
277 aad = scratch + 4 * AES_BLOCK_LEN; 273 aad = scratch + 4 * AES_BLOCK_LEN;
278 274
279 /* 275 /*
280 * Mask FC: zero subtype b4 b5 b6 276 * Mask FC: zero subtype b4 b5 b6 (if not mgmt)
281 * Retry, PwrMgt, MoreData; set Protected 277 * Retry, PwrMgt, MoreData; set Protected
282 */ 278 */
279 mgmt = ieee80211_is_mgmt(hdr->frame_control);
283 mask_fc = hdr->frame_control; 280 mask_fc = hdr->frame_control;
284 mask_fc &= ~cpu_to_le16(0x0070 | IEEE80211_FCTL_RETRY | 281 mask_fc &= ~cpu_to_le16(IEEE80211_FCTL_RETRY |
285 IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA); 282 IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
283 if (!mgmt)
284 mask_fc &= ~cpu_to_le16(0x0070);
286 mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED); 285 mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
287 286
288 hdrlen = ieee80211_hdrlen(hdr->frame_control); 287 hdrlen = ieee80211_hdrlen(hdr->frame_control);
@@ -300,8 +299,10 @@ static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *scratch,
300 299
301 /* First block, b_0 */ 300 /* First block, b_0 */
302 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */ 301 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
303 /* Nonce: QoS Priority | A2 | PN */ 302 /* Nonce: Nonce Flags | A2 | PN
304 b_0[1] = qos_tid; 303 * Nonce Flags: Priority (b0..b3) | Management (b4) | Reserved (b5..b7)
304 */
305 b_0[1] = qos_tid | (mgmt << 4);
305 memcpy(&b_0[2], hdr->addr2, ETH_ALEN); 306 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
306 memcpy(&b_0[8], pn, CCMP_PN_LEN); 307 memcpy(&b_0[8], pn, CCMP_PN_LEN);
307 /* l(m) */ 308 /* l(m) */
@@ -360,9 +361,14 @@ static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
360 int hdrlen, len, tail; 361 int hdrlen, len, tail;
361 u8 *pos, *pn; 362 u8 *pos, *pn;
362 int i; 363 int i;
364 bool skip_hw;
365
366 skip_hw = (tx->key->conf.flags & IEEE80211_KEY_FLAG_SW_MGMT) &&
367 ieee80211_is_mgmt(hdr->frame_control);
363 368
364 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) && 369 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
365 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) { 370 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV) &&
371 !skip_hw) {
366 /* hwaccel - with no need for preallocated room for CCMP 372 /* hwaccel - with no need for preallocated room for CCMP
367 * header or MIC fields */ 373 * header or MIC fields */
368 info->control.hw_key = &tx->key->conf; 374 info->control.hw_key = &tx->key->conf;
@@ -397,7 +403,7 @@ static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
397 403
398 ccmp_pn2hdr(pos, pn, key->conf.keyidx); 404 ccmp_pn2hdr(pos, pn, key->conf.keyidx);
399 405
400 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) { 406 if ((key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) && !skip_hw) {
401 /* hwaccel - with preallocated room for CCMP header */ 407 /* hwaccel - with preallocated room for CCMP header */
402 info->control.hw_key = &tx->key->conf; 408 info->control.hw_key = &tx->key->conf;
403 return 0; 409 return 0;
@@ -416,19 +422,13 @@ ieee80211_tx_result
416ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx) 422ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
417{ 423{
418 struct sk_buff *skb = tx->skb; 424 struct sk_buff *skb = tx->skb;
419 int i;
420 425
421 ieee80211_tx_set_protected(tx); 426 ieee80211_tx_set_protected(tx);
422 427
423 if (ccmp_encrypt_skb(tx, skb) < 0) 428 do {
424 return TX_DROP; 429 if (ccmp_encrypt_skb(tx, skb) < 0)
425 430 return TX_DROP;
426 if (tx->extra_frag) { 431 } while ((skb = skb->next));
427 for (i = 0; i < tx->num_extra_frag; i++) {
428 if (ccmp_encrypt_skb(tx, tx->extra_frag[i]))
429 return TX_DROP;
430 }
431 }
432 432
433 return TX_CONTINUE; 433 return TX_CONTINUE;
434} 434}
@@ -446,7 +446,8 @@ ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
446 446
447 hdrlen = ieee80211_hdrlen(hdr->frame_control); 447 hdrlen = ieee80211_hdrlen(hdr->frame_control);
448 448
449 if (!ieee80211_is_data(hdr->frame_control)) 449 if (!ieee80211_is_data(hdr->frame_control) &&
450 !ieee80211_is_robust_mgmt_frame(hdr))
450 return RX_CONTINUE; 451 return RX_CONTINUE;
451 452
452 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN; 453 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
@@ -485,3 +486,126 @@ ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
485 486
486 return RX_CONTINUE; 487 return RX_CONTINUE;
487} 488}
489
490
491static void bip_aad(struct sk_buff *skb, u8 *aad)
492{
493 /* BIP AAD: FC(masked) || A1 || A2 || A3 */
494
495 /* FC type/subtype */
496 aad[0] = skb->data[0];
497 /* Mask FC Retry, PwrMgt, MoreData flags to zero */
498 aad[1] = skb->data[1] & ~(BIT(4) | BIT(5) | BIT(6));
499 /* A1 || A2 || A3 */
500 memcpy(aad + 2, skb->data + 4, 3 * ETH_ALEN);
501}
502
503
504static inline void bip_ipn_swap(u8 *d, const u8 *s)
505{
506 *d++ = s[5];
507 *d++ = s[4];
508 *d++ = s[3];
509 *d++ = s[2];
510 *d++ = s[1];
511 *d = s[0];
512}
513
514
515ieee80211_tx_result
516ieee80211_crypto_aes_cmac_encrypt(struct ieee80211_tx_data *tx)
517{
518 struct sk_buff *skb = tx->skb;
519 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
520 struct ieee80211_key *key = tx->key;
521 struct ieee80211_mmie *mmie;
522 u8 *pn, aad[20];
523 int i;
524
525 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
526 /* hwaccel */
527 info->control.hw_key = &tx->key->conf;
528 return 0;
529 }
530
531 if (WARN_ON(skb_tailroom(skb) < sizeof(*mmie)))
532 return TX_DROP;
533
534 mmie = (struct ieee80211_mmie *) skb_put(skb, sizeof(*mmie));
535 mmie->element_id = WLAN_EID_MMIE;
536 mmie->length = sizeof(*mmie) - 2;
537 mmie->key_id = cpu_to_le16(key->conf.keyidx);
538
539 /* PN = PN + 1 */
540 pn = key->u.aes_cmac.tx_pn;
541
542 for (i = sizeof(key->u.aes_cmac.tx_pn) - 1; i >= 0; i--) {
543 pn[i]++;
544 if (pn[i])
545 break;
546 }
547 bip_ipn_swap(mmie->sequence_number, pn);
548
549 bip_aad(skb, aad);
550
551 /*
552 * MIC = AES-128-CMAC(IGTK, AAD || Management Frame Body || MMIE, 64)
553 */
554 ieee80211_aes_cmac(key->u.aes_cmac.tfm, key->u.aes_cmac.tx_crypto_buf,
555 aad, skb->data + 24, skb->len - 24, mmie->mic);
556
557 return TX_CONTINUE;
558}
559
560
561ieee80211_rx_result
562ieee80211_crypto_aes_cmac_decrypt(struct ieee80211_rx_data *rx)
563{
564 struct sk_buff *skb = rx->skb;
565 struct ieee80211_key *key = rx->key;
566 struct ieee80211_mmie *mmie;
567 u8 aad[20], mic[8], ipn[6];
568 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
569
570 if (!ieee80211_is_mgmt(hdr->frame_control))
571 return RX_CONTINUE;
572
573 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
574 (rx->status->flag & RX_FLAG_IV_STRIPPED))
575 return RX_CONTINUE;
576
577 if (skb->len < 24 + sizeof(*mmie))
578 return RX_DROP_UNUSABLE;
579
580 mmie = (struct ieee80211_mmie *)
581 (skb->data + skb->len - sizeof(*mmie));
582 if (mmie->element_id != WLAN_EID_MMIE ||
583 mmie->length != sizeof(*mmie) - 2)
584 return RX_DROP_UNUSABLE; /* Invalid MMIE */
585
586 bip_ipn_swap(ipn, mmie->sequence_number);
587
588 if (memcmp(ipn, key->u.aes_cmac.rx_pn, 6) <= 0) {
589 key->u.aes_cmac.replays++;
590 return RX_DROP_UNUSABLE;
591 }
592
593 if (!(rx->status->flag & RX_FLAG_DECRYPTED)) {
594 /* hardware didn't decrypt/verify MIC */
595 bip_aad(skb, aad);
596 ieee80211_aes_cmac(key->u.aes_cmac.tfm,
597 key->u.aes_cmac.rx_crypto_buf, aad,
598 skb->data + 24, skb->len - 24, mic);
599 if (memcmp(mic, mmie->mic, sizeof(mmie->mic)) != 0) {
600 key->u.aes_cmac.icverrors++;
601 return RX_DROP_UNUSABLE;
602 }
603 }
604
605 memcpy(key->u.aes_cmac.rx_pn, ipn, 6);
606
607 /* Remove MMIE */
608 skb_trim(skb, skb->len - sizeof(*mmie));
609
610 return RX_CONTINUE;
611}