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"
25 ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
27 u8 *data, *key, *mic, key_offset;
30 struct ieee80211_hdr *hdr;
32 struct sk_buff *skb = tx->skb;
39 if (!tx->key || tx->key->conf.alg != ALG_TKIP || skb->len < 24 ||
40 !WLAN_FC_DATA_PRESENT(fc))
43 hdr = (struct ieee80211_hdr *)skb->data;
44 hdrlen = ieee80211_hdrlen(hdr->frame_control);
45 if (skb->len < hdrlen)
48 data = skb->data + hdrlen;
49 data_len = skb->len - hdrlen;
51 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
52 !(tx->flags & IEEE80211_TX_FRAGMENTED) &&
53 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) &&
55 /* hwaccel - with no need for preallocated room for Michael MIC
60 tail = MICHAEL_MIC_LEN;
61 if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
64 if (WARN_ON(skb_tailroom(skb) < tail ||
65 skb_headroom(skb) < TKIP_IV_LEN))
69 authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
73 /* At this point we know we're using ALG_TKIP. To get the MIC key
74 * we now will rely on the offset from the ieee80211_key_conf::key */
75 key_offset = authenticator ?
76 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY :
77 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY;
78 key = &tx->key->conf.key[key_offset];
79 mic = skb_put(skb, MICHAEL_MIC_LEN);
80 michael_mic(key, hdr, data, data_len, mic);
87 ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
89 u8 *data, *key = NULL, key_offset;
92 struct ieee80211_hdr *hdr;
94 u8 mic[MICHAEL_MIC_LEN];
95 struct sk_buff *skb = rx->skb;
96 int authenticator = 1, wpa_test = 0;
102 * No way to verify the MIC if the hardware stripped it
104 if (rx->status->flag & RX_FLAG_MMIC_STRIPPED)
107 if (!rx->key || rx->key->conf.alg != ALG_TKIP ||
108 !(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc))
111 hdr = (struct ieee80211_hdr *)skb->data;
112 hdrlen = ieee80211_hdrlen(hdr->frame_control);
113 if (skb->len < hdrlen + MICHAEL_MIC_LEN)
114 return RX_DROP_UNUSABLE;
116 data = skb->data + hdrlen;
117 data_len = skb->len - hdrlen - MICHAEL_MIC_LEN;
120 authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
124 /* At this point we know we're using ALG_TKIP. To get the MIC key
125 * we now will rely on the offset from the ieee80211_key_conf::key */
126 key_offset = authenticator ?
127 NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY :
128 NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY;
129 key = &rx->key->conf.key[key_offset];
130 michael_mic(key, hdr, data, data_len, mic);
131 if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
132 if (!(rx->flags & IEEE80211_RX_RA_MATCH))
133 return RX_DROP_UNUSABLE;
135 mac80211_ev_michael_mic_failure(rx->dev, rx->key->conf.keyidx,
137 return RX_DROP_UNUSABLE;
140 /* remove Michael MIC from payload */
141 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
143 /* update IV in key information to be able to detect replays */
144 rx->key->u.tkip.rx[rx->queue].iv32 = rx->tkip_iv32;
145 rx->key->u.tkip.rx[rx->queue].iv16 = rx->tkip_iv16;
151 static int tkip_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
153 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
154 struct ieee80211_key *key = tx->key;
155 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
160 info->control.icv_len = TKIP_ICV_LEN;
161 info->control.iv_len = TKIP_IV_LEN;
163 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
164 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
165 /* hwaccel - with no need for preallocated room for IV/ICV */
166 info->control.hw_key = &tx->key->conf;
170 hdrlen = ieee80211_hdrlen(hdr->frame_control);
171 len = skb->len - hdrlen;
173 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
178 if (WARN_ON(skb_tailroom(skb) < tail ||
179 skb_headroom(skb) < TKIP_IV_LEN))
182 pos = skb_push(skb, TKIP_IV_LEN);
183 memmove(pos, pos + TKIP_IV_LEN, hdrlen);
186 /* Increase IV for the frame */
187 key->u.tkip.tx.iv16++;
188 if (key->u.tkip.tx.iv16 == 0)
189 key->u.tkip.tx.iv32++;
191 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
192 /* hwaccel - with preallocated room for IV */
193 ieee80211_tkip_add_iv(pos, key, key->u.tkip.tx.iv16);
195 info->control.hw_key = &tx->key->conf;
199 /* Add room for ICV */
200 skb_put(skb, TKIP_ICV_LEN);
202 hdr = (struct ieee80211_hdr *) skb->data;
203 ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
204 key, pos, len, hdr->addr2);
210 ieee80211_crypto_tkip_encrypt(struct ieee80211_tx_data *tx)
212 struct sk_buff *skb = tx->skb;
214 ieee80211_tx_set_protected(tx);
216 if (tkip_encrypt_skb(tx, skb) < 0)
219 if (tx->extra_frag) {
221 for (i = 0; i < tx->num_extra_frag; i++) {
222 if (tkip_encrypt_skb(tx, tx->extra_frag[i]) < 0)
232 ieee80211_crypto_tkip_decrypt(struct ieee80211_rx_data *rx)
234 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
235 int hdrlen, res, hwaccel = 0, wpa_test = 0;
236 struct ieee80211_key *key = rx->key;
237 struct sk_buff *skb = rx->skb;
238 DECLARE_MAC_BUF(mac);
240 hdrlen = ieee80211_hdrlen(hdr->frame_control);
242 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
245 if (!rx->sta || skb->len - hdrlen < 12)
246 return RX_DROP_UNUSABLE;
248 if (rx->status->flag & RX_FLAG_DECRYPTED) {
249 if (rx->status->flag & RX_FLAG_IV_STRIPPED) {
251 * Hardware took care of all processing, including
252 * replay protection, and stripped the ICV/IV so
253 * we cannot do any checks here.
258 /* let TKIP code verify IV, but skip decryption */
262 res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
263 key, skb->data + hdrlen,
264 skb->len - hdrlen, rx->sta->addr,
265 hdr->addr1, hwaccel, rx->queue,
268 if (res != TKIP_DECRYPT_OK || wpa_test)
269 return RX_DROP_UNUSABLE;
272 skb_trim(skb, skb->len - TKIP_ICV_LEN);
275 memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
276 skb_pull(skb, TKIP_IV_LEN);
282 static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
290 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
293 * Mask FC: zero subtype b4 b5 b6
294 * Retry, PwrMgt, MoreData; set Protected
296 mask_fc = hdr->frame_control;
297 mask_fc &= ~cpu_to_le16(0x0070 | IEEE80211_FCTL_RETRY |
298 IEEE80211_FCTL_PM | IEEE80211_FCTL_MOREDATA);
299 mask_fc |= cpu_to_le16(IEEE80211_FCTL_PROTECTED);
301 hdrlen = ieee80211_hdrlen(hdr->frame_control);
303 a4_included = ieee80211_has_a4(hdr->frame_control);
305 if (ieee80211_is_data_qos(hdr->frame_control))
306 qos_tid = *ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_TID_MASK;
310 data_len = skb->len - hdrlen - CCMP_HDR_LEN;
312 data_len -= CCMP_MIC_LEN;
314 /* First block, b_0 */
315 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
316 /* Nonce: QoS Priority | A2 | PN */
318 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
319 memcpy(&b_0[8], pn, CCMP_PN_LEN);
321 put_unaligned_be16(data_len, &b_0[14]);
323 /* AAD (extra authenticate-only data) / masked 802.11 header
324 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
325 put_unaligned_be16(len_a, &aad[0]);
326 put_unaligned(mask_fc, (__le16 *)&aad[2]);
327 memcpy(&aad[4], &hdr->addr1, 3 * ETH_ALEN);
329 /* Mask Seq#, leave Frag# */
330 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
334 memcpy(&aad[24], hdr->addr4, ETH_ALEN);
338 memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
344 static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
349 hdr[3] = 0x20 | (key_id << 6);
357 static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
365 return (hdr[3] >> 6) & 0x03;
369 static int ccmp_encrypt_skb(struct ieee80211_tx_data *tx, struct sk_buff *skb)
371 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
372 struct ieee80211_key *key = tx->key;
373 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
374 int hdrlen, len, tail;
375 u8 *pos, *pn, *b_0, *aad, *scratch;
378 info->control.icv_len = CCMP_MIC_LEN;
379 info->control.iv_len = CCMP_HDR_LEN;
381 if ((tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) &&
382 !(tx->key->conf.flags & IEEE80211_KEY_FLAG_GENERATE_IV)) {
383 /* hwaccel - with no need for preallocated room for CCMP "
384 * header or MIC fields */
385 info->control.hw_key = &tx->key->conf;
389 scratch = key->u.ccmp.tx_crypto_buf;
390 b_0 = scratch + 3 * AES_BLOCK_LEN;
391 aad = scratch + 4 * AES_BLOCK_LEN;
393 hdrlen = ieee80211_hdrlen(hdr->frame_control);
394 len = skb->len - hdrlen;
396 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
401 if (WARN_ON(skb_tailroom(skb) < tail ||
402 skb_headroom(skb) < CCMP_HDR_LEN))
405 pos = skb_push(skb, CCMP_HDR_LEN);
406 memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
407 hdr = (struct ieee80211_hdr *) pos;
411 pn = key->u.ccmp.tx_pn;
413 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
419 ccmp_pn2hdr(pos, pn, key->conf.keyidx);
421 if (key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE) {
422 /* hwaccel - with preallocated room for CCMP header */
423 info->control.hw_key = &tx->key->conf;
428 ccmp_special_blocks(skb, pn, b_0, aad, 0);
429 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
430 pos, skb_put(skb, CCMP_MIC_LEN));
437 ieee80211_crypto_ccmp_encrypt(struct ieee80211_tx_data *tx)
439 struct sk_buff *skb = tx->skb;
441 ieee80211_tx_set_protected(tx);
443 if (ccmp_encrypt_skb(tx, skb) < 0)
446 if (tx->extra_frag) {
448 for (i = 0; i < tx->num_extra_frag; i++) {
449 if (ccmp_encrypt_skb(tx, tx->extra_frag[i]) < 0)
459 ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
461 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
463 struct ieee80211_key *key = rx->key;
464 struct sk_buff *skb = rx->skb;
467 DECLARE_MAC_BUF(mac);
469 hdrlen = ieee80211_hdrlen(hdr->frame_control);
471 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
474 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
475 if (!rx->sta || data_len < 0)
476 return RX_DROP_UNUSABLE;
478 if ((rx->status->flag & RX_FLAG_DECRYPTED) &&
479 (rx->status->flag & RX_FLAG_IV_STRIPPED))
482 (void) ccmp_hdr2pn(pn, skb->data + hdrlen);
484 if (memcmp(pn, key->u.ccmp.rx_pn[rx->queue], CCMP_PN_LEN) <= 0) {
485 key->u.ccmp.replays++;
486 return RX_DROP_UNUSABLE;
489 if (!(rx->status->flag & RX_FLAG_DECRYPTED)) {
490 /* hardware didn't decrypt/verify MIC */
491 u8 *scratch, *b_0, *aad;
493 scratch = key->u.ccmp.rx_crypto_buf;
494 b_0 = scratch + 3 * AES_BLOCK_LEN;
495 aad = scratch + 4 * AES_BLOCK_LEN;
497 ccmp_special_blocks(skb, pn, b_0, aad, 1);
499 if (ieee80211_aes_ccm_decrypt(
500 key->u.ccmp.tfm, scratch, b_0, aad,
501 skb->data + hdrlen + CCMP_HDR_LEN, data_len,
502 skb->data + skb->len - CCMP_MIC_LEN,
503 skb->data + hdrlen + CCMP_HDR_LEN)) {
504 return RX_DROP_UNUSABLE;
508 memcpy(key->u.ccmp.rx_pn[rx->queue], pn, CCMP_PN_LEN);
510 /* Remove CCMP header and MIC */
511 skb_trim(skb, skb->len - CCMP_MIC_LEN);
512 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
513 skb_pull(skb, CCMP_HDR_LEN);