mac80211: remove one user of ieee80211_get_hdr_info
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / mac80211 / wpa.c
blob919e30ce2980649a96c1821d49b201645b4ad291
1 /*
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.
7 */
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"
19 #include "michael.h"
20 #include "tkip.h"
21 #include "aes_ccm.h"
22 #include "wpa.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;
28 size_t hdrlen;
29 __le16 fc;
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;
44 else
45 *qos_tid = 0;
47 return skb->len < hdrlen ? -1 : 0;
51 ieee80211_tx_result
52 ieee80211_tx_h_michael_mic_add(struct ieee80211_tx_data *tx)
54 u8 *data, *sa, *da, *key, *mic, qos_tid, key_offset;
55 size_t data_len;
56 u16 fc;
57 struct sk_buff *skb = tx->skb;
58 int authenticator;
59 int wpa_test = 0;
60 int tail;
62 fc = tx->fc;
64 if (!tx->key || tx->key->conf.alg != ALG_TKIP || skb->len < 24 ||
65 !WLAN_FC_DATA_PRESENT(fc))
66 return TX_CONTINUE;
68 if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len))
69 return TX_DROP;
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) &&
74 !wpa_test) {
75 /* hwaccel - with no need for preallocated room for Michael MIC
77 return TX_CONTINUE;
80 tail = MICHAEL_MIC_LEN;
81 if (!(tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE))
82 tail += TKIP_ICV_LEN;
84 if (WARN_ON(skb_tailroom(skb) < tail ||
85 skb_headroom(skb) < TKIP_IV_LEN))
86 return TX_DROP;
88 #if 0
89 authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
90 #else
91 authenticator = 1;
92 #endif
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);
102 return TX_CONTINUE;
106 ieee80211_rx_result
107 ieee80211_rx_h_michael_mic_verify(struct ieee80211_rx_data *rx)
109 u8 *data, *sa, *da, *key = NULL, qos_tid, key_offset;
110 size_t data_len;
111 u16 fc;
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);
117 fc = rx->fc;
120 * No way to verify the MIC if the hardware stripped it
122 if (rx->status->flag & RX_FLAG_MMIC_STRIPPED)
123 return RX_CONTINUE;
125 if (!rx->key || rx->key->conf.alg != ALG_TKIP ||
126 !(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc))
127 return RX_CONTINUE;
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;
135 #if 0
136 authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
137 #else
138 authenticator = 1;
139 #endif
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,
152 (void *) skb->data);
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;
163 return RX_CONTINUE;
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);
172 unsigned int hdrlen;
173 int len, tail;
174 u8 *pos;
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;
183 return 0;
186 hdrlen = ieee80211_hdrlen(hdr->frame_control);
187 len = skb->len - hdrlen;
189 if (tx->key->flags & KEY_FLAG_UPLOADED_TO_HARDWARE)
190 tail = 0;
191 else
192 tail = TKIP_ICV_LEN;
194 if (WARN_ON(skb_tailroom(skb) < tail ||
195 skb_headroom(skb) < TKIP_IV_LEN))
196 return -1;
198 pos = skb_push(skb, TKIP_IV_LEN);
199 memmove(pos, pos + TKIP_IV_LEN, hdrlen);
200 pos += 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;
212 return 0;
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);
221 return 0;
225 ieee80211_tx_result
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)
233 return TX_DROP;
235 if (tx->extra_frag) {
236 int i;
237 for (i = 0; i < tx->num_extra_frag; i++) {
238 if (tkip_encrypt_skb(tx, tx->extra_frag[i]) < 0)
239 return TX_DROP;
243 return TX_CONTINUE;
247 ieee80211_rx_result
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)
259 return RX_CONTINUE;
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.
271 return RX_CONTINUE;
274 /* let TKIP code verify IV, but skip decryption */
275 hwaccel = 1;
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,
282 &rx->tkip_iv32,
283 &rx->tkip_iv16);
284 if (res != TKIP_DECRYPT_OK || wpa_test)
285 return RX_DROP_UNUSABLE;
287 /* Trim ICV */
288 skb_trim(skb, skb->len - TKIP_ICV_LEN);
290 /* Remove IV */
291 memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
292 skb_pull(skb, TKIP_IV_LEN);
294 return RX_CONTINUE;
298 static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
299 int encrypted)
301 __le16 mask_fc;
302 int a4_included;
303 u8 qos_tid;
304 u16 data_len, len_a;
305 unsigned int hdrlen;
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);
318 len_a = hdrlen - 2;
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;
323 else
324 qos_tid = 0;
326 data_len = skb->len - hdrlen - CCMP_HDR_LEN;
327 if (encrypted)
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 */
333 b_0[1] = qos_tid;
334 memcpy(&b_0[2], hdr->addr2, ETH_ALEN);
335 memcpy(&b_0[8], pn, CCMP_PN_LEN);
336 /* l(m) */
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;
347 aad[23] = 0;
349 if (a4_included) {
350 memcpy(&aad[24], hdr->addr4, ETH_ALEN);
351 aad[30] = qos_tid;
352 aad[31] = 0;
353 } else {
354 memset(&aad[24], 0, ETH_ALEN + IEEE80211_QOS_CTL_LEN);
355 aad[24] = qos_tid;
360 static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
362 hdr[0] = pn[5];
363 hdr[1] = pn[4];
364 hdr[2] = 0;
365 hdr[3] = 0x20 | (key_id << 6);
366 hdr[4] = pn[3];
367 hdr[5] = pn[2];
368 hdr[6] = pn[1];
369 hdr[7] = pn[0];
373 static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
375 pn[0] = hdr[7];
376 pn[1] = hdr[6];
377 pn[2] = hdr[5];
378 pn[3] = hdr[4];
379 pn[4] = hdr[1];
380 pn[5] = hdr[0];
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;
392 int i;
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;
402 return 0;
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)
413 tail = 0;
414 else
415 tail = CCMP_MIC_LEN;
417 if (WARN_ON(skb_tailroom(skb) < tail ||
418 skb_headroom(skb) < CCMP_HDR_LEN))
419 return -1;
421 pos = skb_push(skb, CCMP_HDR_LEN);
422 memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
423 hdr = (struct ieee80211_hdr *) pos;
424 pos += hdrlen;
426 /* PN = PN + 1 */
427 pn = key->u.ccmp.tx_pn;
429 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
430 pn[i]++;
431 if (pn[i])
432 break;
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;
440 return 0;
443 pos += CCMP_HDR_LEN;
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));
448 return 0;
452 ieee80211_tx_result
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)
460 return TX_DROP;
462 if (tx->extra_frag) {
463 int i;
464 for (i = 0; i < tx->num_extra_frag; i++) {
465 if (ccmp_encrypt_skb(tx, tx->extra_frag[i]) < 0)
466 return TX_DROP;
470 return TX_CONTINUE;
474 ieee80211_rx_result
475 ieee80211_crypto_ccmp_decrypt(struct ieee80211_rx_data *rx)
477 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
478 int hdrlen;
479 struct ieee80211_key *key = rx->key;
480 struct sk_buff *skb = rx->skb;
481 u8 pn[CCMP_PN_LEN];
482 int data_len;
483 DECLARE_MAC_BUF(mac);
485 hdrlen = ieee80211_hdrlen(hdr->frame_control);
487 if ((rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
488 return RX_CONTINUE;
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))
496 return RX_CONTINUE;
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);
531 return RX_CONTINUE;