Do not enable IDE for brcm47xx for now
[openwrt.git] / package / d80211 / src / wpa.c
1 /*
2 * Copyright 2002-2004, Instant802 Networks, Inc.
3 *
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 */
8
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>
15
16 #include <net/d80211.h>
17 #include "ieee80211_common.h"
18 #include "ieee80211_i.h"
19 #include "michael.h"
20 #include "tkip.h"
21 #include "aes_ccm.h"
22 #include "wpa.h"
23 #ifdef CONFIG_HOSTAPD_WPA_TESTING
24 #include "hostapd_ioctl.h"
25 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
26
27
28 int ieee80211_get_hdr_info(const struct sk_buff *skb, u8 **sa, u8 **da,
29 u8 *qos_tid, u8 **data, size_t *data_len)
30 {
31 struct ieee80211_hdr *hdr;
32 size_t hdrlen;
33 u16 fc;
34 int a4_included;
35 u8 *pos;
36
37 hdr = (struct ieee80211_hdr *) skb->data;
38 fc = le16_to_cpu(hdr->frame_control);
39
40 hdrlen = 24;
41 if ((fc & (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) ==
42 (IEEE80211_FCTL_FROMDS | IEEE80211_FCTL_TODS)) {
43 hdrlen += ETH_ALEN;
44 *sa = hdr->addr4;
45 *da = hdr->addr3;
46 } else if (fc & IEEE80211_FCTL_FROMDS) {
47 *sa = hdr->addr3;
48 *da = hdr->addr1;
49 } else if (fc & IEEE80211_FCTL_TODS) {
50 *sa = hdr->addr2;
51 *da = hdr->addr3;
52 } else {
53 *sa = hdr->addr2;
54 *da = hdr->addr1;
55 }
56
57 if (fc & 0x80)
58 hdrlen += 2;
59
60 *data = skb->data + hdrlen;
61 *data_len = skb->len - hdrlen;
62
63 a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
64 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
65 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA &&
66 fc & IEEE80211_STYPE_QOS_DATA) {
67 pos = (u8 *) &hdr->addr4;
68 if (a4_included)
69 pos += 6;
70 *qos_tid = pos[0] & 0x0f;
71 *qos_tid |= 0x80; /* qos_included flag */
72 } else
73 *qos_tid = 0;
74
75 return skb->len < hdrlen ? -1 : 0;
76 }
77
78
79 ieee80211_txrx_result
80 ieee80211_tx_h_michael_mic_add(struct ieee80211_txrx_data *tx)
81 {
82 u8 *data, *sa, *da, *key, *mic, qos_tid;
83 size_t data_len;
84 u16 fc;
85 struct sk_buff *skb = tx->skb;
86 int authenticator;
87 int wpa_test = 0;
88
89 fc = tx->fc;
90
91 if (!tx->key || tx->key->alg != ALG_TKIP || skb->len < 24 ||
92 !WLAN_FC_DATA_PRESENT(fc))
93 return TXRX_CONTINUE;
94
95 if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len))
96 return TXRX_DROP;
97
98 #ifdef CONFIG_HOSTAPD_WPA_TESTING
99 if ((tx->sta && tx->sta->wpa_trigger & WPA_TRIGGER_FAIL_TX_MIC) ||
100 (!tx->u.tx.unicast &&
101 tx->local->wpa_trigger & WPA_TRIGGER_FAIL_TX_MIC)) {
102 wpa_test = 1;
103 }
104 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
105
106 if (!tx->key->force_sw_encrypt &&
107 !tx->fragmented &&
108 !(tx->local->hw.flags & IEEE80211_HW_TKIP_INCLUDE_MMIC) &&
109 !wpa_test) {
110 /* hwaccel - with no need for preallocated room for Michael MIC
111 */
112 return TXRX_CONTINUE;
113 }
114
115 if (skb_tailroom(skb) < MICHAEL_MIC_LEN) {
116 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
117 if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN,
118 MICHAEL_MIC_LEN + TKIP_ICV_LEN,
119 GFP_ATOMIC))) {
120 printk(KERN_DEBUG "%s: failed to allocate more memory "
121 "for Michael MIC\n", tx->dev->name);
122 return TXRX_DROP;
123 }
124 }
125
126 #if 0
127 authenticator = fc & IEEE80211_FCTL_FROMDS; /* FIX */
128 #else
129 authenticator = 1;
130 #endif
131 key = &tx->key->key[authenticator ? ALG_TKIP_TEMP_AUTH_TX_MIC_KEY :
132 ALG_TKIP_TEMP_AUTH_RX_MIC_KEY];
133 mic = skb_put(skb, MICHAEL_MIC_LEN);
134 michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
135
136 #ifdef CONFIG_HOSTAPD_WPA_TESTING
137 if (tx->sta && tx->sta->wpa_trigger & WPA_TRIGGER_FAIL_TX_MIC) {
138 printk(KERN_INFO "%s: WPA testing - corrupting TX Michael MIC "
139 "for STA " MAC_FMT "\n",
140 tx->dev->name, MAC_ARG(tx->sta->addr));
141 tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
142 tx->sta->wpa_trigger &= ~WPA_TRIGGER_FAIL_TX_MIC;
143 tx->wpa_test = 1;
144 mic[0]++;
145 } else if (!tx->u.tx.unicast &&
146 tx->local->wpa_trigger & WPA_TRIGGER_FAIL_TX_MIC) {
147 printk(KERN_INFO "%s: WPA testing - corrupting TX Michael MIC "
148 "for Group Key\n", tx->dev->name);
149 tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
150 tx->local->wpa_trigger &= ~WPA_TRIGGER_FAIL_TX_MIC;
151 tx->wpa_test = 1;
152 mic[0]++;
153 }
154 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
155
156 return TXRX_CONTINUE;
157 }
158
159
160 ieee80211_txrx_result
161 ieee80211_rx_h_michael_mic_verify(struct ieee80211_txrx_data *rx)
162 {
163 u8 *data, *sa, *da, *key = NULL, qos_tid;
164 size_t data_len;
165 u16 fc;
166 u8 mic[MICHAEL_MIC_LEN];
167 struct sk_buff *skb = rx->skb;
168 int authenticator = 1, wpa_test = 0;
169
170 fc = rx->fc;
171
172 /* If device handles decryption totally, skip this check */
173 if ((rx->local->hw.flags & IEEE80211_HW_DEVICE_HIDES_WEP) ||
174 (rx->local->hw.flags & IEEE80211_HW_DEVICE_STRIPS_MIC))
175 return TXRX_CONTINUE;
176
177 if (!rx->key || rx->key->alg != ALG_TKIP ||
178 !(rx->fc & IEEE80211_FCTL_PROTECTED) || !WLAN_FC_DATA_PRESENT(fc))
179 return TXRX_CONTINUE;
180
181 #ifdef CONFIG_HOSTAPD_WPA_TESTING
182 if (rx->sta && rx->sta->wpa_trigger & WPA_TRIGGER_FAIL_RX_MIC) {
183 wpa_test = 1;
184 }
185 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
186
187 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
188 !rx->key->force_sw_encrypt) {
189 if (rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) {
190 if (skb->len < MICHAEL_MIC_LEN)
191 return TXRX_DROP;
192 }
193 /* Need to verify Michael MIC sometimes in software even when
194 * hwaccel is used. Atheros ar5212: fragmented frames and QoS
195 * frames. */
196 if (!rx->fragmented && !wpa_test)
197 goto remove_mic;
198 }
199
200 if (ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len)
201 || data_len < MICHAEL_MIC_LEN)
202 return TXRX_DROP;
203
204 data_len -= MICHAEL_MIC_LEN;
205
206 #if 0
207 authenticator = fc & IEEE80211_FCTL_TODS; /* FIX */
208 #else
209 authenticator = 1;
210 #endif
211 key = &rx->key->key[authenticator ? ALG_TKIP_TEMP_AUTH_RX_MIC_KEY :
212 ALG_TKIP_TEMP_AUTH_TX_MIC_KEY];
213 michael_mic(key, da, sa, qos_tid & 0x0f, data, data_len, mic);
214 #ifdef CONFIG_HOSTAPD_WPA_TESTING
215 if (rx->sta && rx->sta->wpa_trigger & WPA_TRIGGER_FAIL_RX_MIC) {
216 printk(KERN_INFO "%s: WPA testing - corrupting RX Michael MIC "
217 "for STA " MAC_FMT "\n",
218 rx->dev->name, MAC_ARG(rx->sta->addr));
219 rx->sta->wpa_trigger &= ~WPA_TRIGGER_FAIL_RX_MIC;
220 mic[0]++;
221 }
222 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
223 if (memcmp(mic, data + data_len, MICHAEL_MIC_LEN) != 0 || wpa_test) {
224 #ifdef CONFIG_HOSTAPD_WPA_TESTING
225 int i;
226 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
227
228 if (!rx->u.rx.ra_match)
229 return TXRX_DROP;
230
231 printk(KERN_DEBUG "%s: invalid Michael MIC in data frame from "
232 MAC_FMT "\n", rx->dev->name, MAC_ARG(sa));
233 #ifdef CONFIG_HOSTAPD_WPA_TESTING
234 printk(KERN_DEBUG " received");
235 for (i = 0; i < MICHAEL_MIC_LEN; i++)
236 printk(" %02x", data[data_len + i]);
237 printk(" expected");
238 for (i = 0; i < MICHAEL_MIC_LEN; i++)
239 printk(" %02x", mic[i]);
240 printk("\n");
241 printk(KERN_DEBUG " SA=" MAC_FMT " DA=" MAC_FMT " key",
242 MAC_ARG(sa), MAC_ARG(da));
243 for (i = 0; i < 8; i++)
244 printk(" %02x", key[i]);
245 printk(" (%d)\n", authenticator);
246 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
247
248 do {
249 struct ieee80211_hdr *hdr;
250 union iwreq_data wrqu;
251 char *buf = kmalloc(128, GFP_ATOMIC);
252 if (!buf)
253 break;
254
255 /* TODO: needed parameters: count, key type, TSC */
256 hdr = (struct ieee80211_hdr *) skb->data;
257 sprintf(buf, "MLME-MICHAELMICFAILURE.indication("
258 "keyid=%d %scast addr=" MAC_FMT ")",
259 rx->key->keyidx,
260 hdr->addr1[0] & 0x01 ? "broad" : "uni",
261 MAC_ARG(hdr->addr2));
262 memset(&wrqu, 0, sizeof(wrqu));
263 wrqu.data.length = strlen(buf);
264 wireless_send_event(rx->dev, IWEVCUSTOM, &wrqu, buf);
265 kfree(buf);
266 } while (0);
267
268 if (!rx->local->apdev)
269 return TXRX_DROP;
270
271 ieee80211_rx_mgmt(rx->local, rx->skb, rx->u.rx.status,
272 ieee80211_msg_michael_mic_failure);
273
274 return TXRX_QUEUED;
275 }
276
277 remove_mic:
278 /* remove Michael MIC from payload */
279 skb_trim(skb, skb->len - MICHAEL_MIC_LEN);
280
281 return TXRX_CONTINUE;
282 }
283
284
285 static int tkip_encrypt_skb(struct ieee80211_txrx_data *tx,
286 struct sk_buff *skb, int test)
287 {
288 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
289 struct ieee80211_key *key = tx->key;
290 int hdrlen, len, tailneed;
291 u16 fc;
292 u8 *pos;
293
294 fc = le16_to_cpu(hdr->frame_control);
295 hdrlen = ieee80211_get_hdrlen(fc);
296 len = skb->len - hdrlen;
297
298 tailneed = !tx->key->force_sw_encrypt ? 0 : TKIP_ICV_LEN;
299 if ((skb_headroom(skb) < TKIP_IV_LEN ||
300 skb_tailroom(skb) < tailneed)) {
301 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
302 if (unlikely(pskb_expand_head(skb, TKIP_IV_LEN, tailneed,
303 GFP_ATOMIC)))
304 return -1;
305 }
306
307 pos = skb_push(skb, TKIP_IV_LEN);
308 memmove(pos, pos + TKIP_IV_LEN, hdrlen);
309 pos += hdrlen;
310
311 #ifdef CONFIG_HOSTAPD_WPA_TESTING
312 if (test & WPA_TRIGGER_TX_REPLAY)
313 goto skip_iv_inc;
314 iv_inc:
315 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
316
317 /* Increase IV for the frame */
318 key->u.tkip.iv16++;
319 if (key->u.tkip.iv16 == 0)
320 key->u.tkip.iv32++;
321
322 #ifdef CONFIG_HOSTAPD_WPA_TESTING
323 if (test & WPA_TRIGGER_TX_SKIP_SEQ) {
324 test = 0;
325 goto iv_inc;
326 }
327 skip_iv_inc:
328 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
329
330 if (!tx->key->force_sw_encrypt
331 #ifdef CONFIG_HOSTAPD_WPA_TESTING
332 && !tx->wpa_test
333 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
334 ) {
335 u32 flags = tx->local->hw.flags;
336 hdr = (struct ieee80211_hdr *)skb->data;
337
338 /* hwaccel - with preallocated room for IV */
339 ieee80211_tkip_add_iv(pos, key,
340 (u8) (key->u.tkip.iv16 >> 8),
341 (u8) (((key->u.tkip.iv16 >> 8) | 0x20) &
342 0x7f),
343 (u8) key->u.tkip.iv16);
344
345 if (flags & IEEE80211_HW_TKIP_REQ_PHASE2_KEY)
346 ieee80211_tkip_gen_rc4key(key, hdr->addr2,
347 tx->u.tx.control->tkip_key);
348 else if (flags & IEEE80211_HW_TKIP_REQ_PHASE1_KEY) {
349 if (key->u.tkip.iv16 == 0 ||
350 !key->u.tkip.tx_initialized) {
351 ieee80211_tkip_gen_phase1key(key, hdr->addr2,
352 (u16 *)tx->u.tx.control->tkip_key);
353 key->u.tkip.tx_initialized = 1;
354 tx->u.tx.control->flags |=
355 IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY;
356 } else
357 tx->u.tx.control->flags &=
358 ~IEEE80211_TXCTL_TKIP_NEW_PHASE1_KEY;
359 }
360
361 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
362 return 0;
363 }
364
365 /* Add room for ICV */
366 skb_put(skb, TKIP_ICV_LEN);
367
368 hdr = (struct ieee80211_hdr *) skb->data;
369 ieee80211_tkip_encrypt_data(tx->local->wep_tx_tfm,
370 key, pos, len, hdr->addr2);
371 return 0;
372 }
373
374
375 ieee80211_txrx_result
376 ieee80211_tx_h_tkip_encrypt(struct ieee80211_txrx_data *tx)
377 {
378 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
379 u16 fc;
380 struct ieee80211_key *key = tx->key;
381 struct sk_buff *skb = tx->skb;
382 int wpa_test = 0, test = 0;
383
384 fc = le16_to_cpu(hdr->frame_control);
385
386 if (!key || key->alg != ALG_TKIP || !WLAN_FC_DATA_PRESENT(fc))
387 return TXRX_CONTINUE;
388
389 tx->u.tx.control->icv_len = TKIP_ICV_LEN;
390 tx->u.tx.control->iv_len = TKIP_IV_LEN;
391 ieee80211_tx_set_iswep(tx);
392
393 #ifdef CONFIG_HOSTAPD_WPA_TESTING
394 if ((tx->sta && tx->sta->wpa_trigger & WPA_TRIGGER_FAIL_TX_ICV) ||
395 (!tx->u.tx.unicast &&
396 tx->local->wpa_trigger & WPA_TRIGGER_FAIL_TX_ICV)) {
397 wpa_test = 1;
398 }
399
400 if (tx->sta) {
401 test = tx->sta->wpa_trigger;
402 tx->sta->wpa_trigger &=
403 ~(WPA_TRIGGER_TX_REPLAY | WPA_TRIGGER_TX_REPLAY_FRAG |
404 WPA_TRIGGER_TX_SKIP_SEQ);
405 } else {
406 test = tx->local->wpa_trigger;
407 tx->local->wpa_trigger &=
408 ~(WPA_TRIGGER_TX_REPLAY | WPA_TRIGGER_TX_REPLAY_FRAG |
409 WPA_TRIGGER_TX_SKIP_SEQ);
410 }
411 if (test &
412 (WPA_TRIGGER_TX_REPLAY | WPA_TRIGGER_TX_REPLAY_FRAG |
413 WPA_TRIGGER_TX_SKIP_SEQ)) {
414 printk(KERN_INFO "%s: WPA testing - TKIP TX packet number "
415 "%s%s%s%s\n", tx->dev->name,
416 tx->sta ? "[UNICAST]" : "[MULTICAST]",
417 test & WPA_TRIGGER_TX_REPLAY ? "[REPLAY]" : "",
418 test & WPA_TRIGGER_TX_REPLAY_FRAG ?
419 "[REPLAY FRAG]" : "",
420 test & WPA_TRIGGER_TX_SKIP_SEQ ? "[SKIP SEQ]" : "");
421 }
422 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
423
424 if (!tx->key->force_sw_encrypt &&
425 !(tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV) &&
426 !wpa_test) {
427 /* hwaccel - with no need for preallocated room for IV/ICV */
428 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
429 return TXRX_CONTINUE;
430 }
431
432 if (tkip_encrypt_skb(tx, skb, test) < 0)
433 return TXRX_DROP;
434
435 if (tx->u.tx.extra_frag) {
436 int i;
437 #ifdef CONFIG_HOSTAPD_WPA_TESTING
438 if (test & WPA_TRIGGER_TX_REPLAY_FRAG)
439 test |= WPA_TRIGGER_TX_REPLAY;
440 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
441 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
442 if (tkip_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
443 < 0)
444 return TXRX_DROP;
445 }
446 }
447
448 #ifdef CONFIG_HOSTAPD_WPA_TESTING
449 if (tx->sta && tx->sta->wpa_trigger & WPA_TRIGGER_FAIL_TX_ICV) {
450 printk(KERN_INFO "%s: WPA testing - corrupting TX TKIP ICV "
451 "for STA " MAC_FMT "\n",
452 tx->dev->name, MAC_ARG(tx->sta->addr));
453 tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
454 tx->sta->wpa_trigger &= ~WPA_TRIGGER_FAIL_TX_ICV;
455 skb->data[skb->len - 1]++;
456 } else if (!tx->u.tx.unicast &&
457 tx->local->wpa_trigger & WPA_TRIGGER_FAIL_TX_ICV) {
458 printk(KERN_INFO "%s: WPA testing - corrupting TX TKIP ICV "
459 "for Group Key\n",
460 tx->dev->name);
461 tx->u.tx.control->key_idx = HW_KEY_IDX_INVALID;
462 tx->local->wpa_trigger &= ~WPA_TRIGGER_FAIL_TX_ICV;
463 skb->data[skb->len - 1]++;
464 }
465 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
466
467 return TXRX_CONTINUE;
468 }
469
470
471 ieee80211_txrx_result
472 ieee80211_rx_h_tkip_decrypt(struct ieee80211_txrx_data *rx)
473 {
474 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
475 u16 fc;
476 int hdrlen, res, hwaccel = 0, wpa_test = 0;
477 struct ieee80211_key *key = rx->key;
478 struct sk_buff *skb = rx->skb;
479
480 fc = le16_to_cpu(hdr->frame_control);
481 hdrlen = ieee80211_get_hdrlen(fc);
482
483 if (!rx->key || rx->key->alg != ALG_TKIP ||
484 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
485 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
486 return TXRX_CONTINUE;
487
488 if (!rx->sta || skb->len - hdrlen < 12)
489 return TXRX_DROP;
490
491 #ifdef CONFIG_HOSTAPD_WPA_TESTING
492 if (rx->sta && rx->sta->wpa_trigger & WPA_TRIGGER_FAIL_RX_ICV) {
493 printk(KERN_INFO "%s: WPA testing - corrupting RX TKIP ICV "
494 "for STA " MAC_FMT "\n",
495 rx->dev->name, MAC_ARG(rx->sta->addr));
496 rx->sta->wpa_trigger &= ~WPA_TRIGGER_FAIL_RX_ICV;
497 skb->data[skb->len - 1]++;
498 wpa_test = 1;
499 }
500 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
501
502 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
503 !rx->key->force_sw_encrypt) {
504 if (!(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV)) {
505 /* Hardware takes care of all processing, including
506 * replay protection, so no need to continue here. */
507 return TXRX_CONTINUE;
508 }
509
510 /* let TKIP code verify IV, but skip decryption */
511 hwaccel = 1;
512 }
513
514 res = ieee80211_tkip_decrypt_data(rx->local->wep_rx_tfm,
515 key, skb->data + hdrlen,
516 skb->len - hdrlen, rx->sta->addr,
517 hwaccel, rx->u.rx.queue);
518 if (res != TKIP_DECRYPT_OK || wpa_test) {
519 printk(KERN_DEBUG "%s: TKIP decrypt failed for RX frame from "
520 MAC_FMT " (res=%d)\n",
521 rx->dev->name, MAC_ARG(rx->sta->addr), res);
522 return TXRX_DROP;
523 }
524
525 /* Trim ICV */
526 skb_trim(skb, skb->len - TKIP_ICV_LEN);
527
528 /* Remove IV */
529 memmove(skb->data + TKIP_IV_LEN, skb->data, hdrlen);
530 skb_pull(skb, TKIP_IV_LEN);
531
532 return TXRX_CONTINUE;
533 }
534
535
536 static void ccmp_special_blocks(struct sk_buff *skb, u8 *pn, u8 *b_0, u8 *aad,
537 int encrypted)
538 {
539 u16 fc;
540 int a4_included, qos_included;
541 u8 qos_tid, *fc_pos, *data, *sa, *da;
542 int len_a;
543 size_t data_len;
544 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
545
546 fc_pos = (u8 *) &hdr->frame_control;
547 fc = fc_pos[0] ^ (fc_pos[1] << 8);
548 a4_included = (fc & (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS)) ==
549 (IEEE80211_FCTL_TODS | IEEE80211_FCTL_FROMDS);
550
551 ieee80211_get_hdr_info(skb, &sa, &da, &qos_tid, &data, &data_len);
552 data_len -= CCMP_HDR_LEN + (encrypted ? CCMP_MIC_LEN : 0);
553 if (qos_tid & 0x80) {
554 qos_included = 1;
555 qos_tid &= 0x0f;
556 } else
557 qos_included = 0;
558 /* First block, b_0 */
559
560 b_0[0] = 0x59; /* flags: Adata: 1, M: 011, L: 001 */
561 /* Nonce: QoS Priority | A2 | PN */
562 b_0[1] = qos_tid;
563 memcpy(&b_0[2], hdr->addr2, 6);
564 memcpy(&b_0[8], pn, CCMP_PN_LEN);
565 /* l(m) */
566 b_0[14] = (data_len >> 8) & 0xff;
567 b_0[15] = data_len & 0xff;
568
569
570 /* AAD (extra authenticate-only data) / masked 802.11 header
571 * FC | A1 | A2 | A3 | SC | [A4] | [QC] */
572
573 len_a = a4_included ? 28 : 22;
574 if (qos_included)
575 len_a += 2;
576
577 aad[0] = 0; /* (len_a >> 8) & 0xff; */
578 aad[1] = len_a & 0xff;
579 /* Mask FC: zero subtype b4 b5 b6 */
580 aad[2] = fc_pos[0] & ~(BIT(4) | BIT(5) | BIT(6));
581 /* Retry, PwrMgt, MoreData; set Protected */
582 aad[3] = (fc_pos[1] & ~(BIT(3) | BIT(4) | BIT(5))) | BIT(6);
583 memcpy(&aad[4], &hdr->addr1, 18);
584
585 /* Mask Seq#, leave Frag# */
586 aad[22] = *((u8 *) &hdr->seq_ctrl) & 0x0f;
587 aad[23] = 0;
588 if (a4_included) {
589 memcpy(&aad[24], hdr->addr4, 6);
590 aad[30] = 0;
591 aad[31] = 0;
592 } else
593 memset(&aad[24], 0, 8);
594 if (qos_included) {
595 u8 *dpos = &aad[a4_included ? 30 : 24];
596
597 /* Mask QoS Control field */
598 dpos[0] = qos_tid;
599 dpos[1] = 0;
600 }
601 }
602
603
604 static inline void ccmp_pn2hdr(u8 *hdr, u8 *pn, int key_id)
605 {
606 hdr[0] = pn[5];
607 hdr[1] = pn[4];
608 hdr[2] = 0;
609 hdr[3] = 0x20 | (key_id << 6);
610 hdr[4] = pn[3];
611 hdr[5] = pn[2];
612 hdr[6] = pn[1];
613 hdr[7] = pn[0];
614 }
615
616
617 static inline int ccmp_hdr2pn(u8 *pn, u8 *hdr)
618 {
619 pn[0] = hdr[7];
620 pn[1] = hdr[6];
621 pn[2] = hdr[5];
622 pn[3] = hdr[4];
623 pn[4] = hdr[1];
624 pn[5] = hdr[0];
625 return (hdr[3] >> 6) & 0x03;
626 }
627
628
629 static int ccmp_encrypt_skb(struct ieee80211_txrx_data *tx,
630 struct sk_buff *skb, int test)
631 {
632 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
633 struct ieee80211_key *key = tx->key;
634 int hdrlen, len, tailneed;
635 u16 fc;
636 u8 *pos, *pn, *b_0, *aad, *scratch;
637 int i;
638
639 scratch = key->u.ccmp.tx_crypto_buf;
640 b_0 = scratch + 3 * AES_BLOCK_LEN;
641 aad = scratch + 4 * AES_BLOCK_LEN;
642
643 fc = le16_to_cpu(hdr->frame_control);
644 hdrlen = ieee80211_get_hdrlen(fc);
645 len = skb->len - hdrlen;
646
647 tailneed = !key->force_sw_encrypt ? 0 : CCMP_MIC_LEN;
648
649 if ((skb_headroom(skb) < CCMP_HDR_LEN ||
650 skb_tailroom(skb) < tailneed)) {
651 I802_DEBUG_INC(tx->local->tx_expand_skb_head);
652 if (unlikely(pskb_expand_head(skb, CCMP_HDR_LEN, tailneed,
653 GFP_ATOMIC)))
654 return -1;
655 }
656
657 pos = skb_push(skb, CCMP_HDR_LEN);
658 memmove(pos, pos + CCMP_HDR_LEN, hdrlen);
659 hdr = (struct ieee80211_hdr *) pos;
660 pos += hdrlen;
661
662 /* PN = PN + 1 */
663 pn = key->u.ccmp.tx_pn;
664
665 #ifdef CONFIG_HOSTAPD_WPA_TESTING
666 if (test & WPA_TRIGGER_TX_REPLAY)
667 goto skip_pn_inc;
668 pn_inc:
669 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
670
671 for (i = CCMP_PN_LEN - 1; i >= 0; i--) {
672 pn[i]++;
673 if (pn[i])
674 break;
675 }
676
677 #ifdef CONFIG_HOSTAPD_WPA_TESTING
678 if (test & WPA_TRIGGER_TX_SKIP_SEQ) {
679 test = 0;
680 goto pn_inc;
681 }
682 skip_pn_inc:
683 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
684
685 ccmp_pn2hdr(pos, pn, key->keyidx);
686
687 if (!key->force_sw_encrypt) {
688 /* hwaccel - with preallocated room for CCMP header */
689 tx->u.tx.control->key_idx = key->hw_key_idx;
690 return 0;
691 }
692
693 pos += CCMP_HDR_LEN;
694 ccmp_special_blocks(skb, pn, b_0, aad, 0);
695 ieee80211_aes_ccm_encrypt(key->u.ccmp.tfm, scratch, b_0, aad, pos, len,
696 pos, skb_put(skb, CCMP_MIC_LEN));
697
698 return 0;
699 }
700
701
702 ieee80211_txrx_result
703 ieee80211_tx_h_ccmp_encrypt(struct ieee80211_txrx_data *tx)
704 {
705 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx->skb->data;
706 struct ieee80211_key *key = tx->key;
707 u16 fc;
708 struct sk_buff *skb = tx->skb;
709 int test = 0;
710
711 fc = le16_to_cpu(hdr->frame_control);
712
713 if (!key || key->alg != ALG_CCMP || !WLAN_FC_DATA_PRESENT(fc))
714 return TXRX_CONTINUE;
715
716 #ifdef CONFIG_HOSTAPD_WPA_TESTING
717 if (tx->sta) {
718 test = tx->sta->wpa_trigger;
719 tx->sta->wpa_trigger = 0;
720 } else {
721 test = tx->local->wpa_trigger;
722 tx->local->wpa_trigger = 0;
723 }
724 if (test &
725 (WPA_TRIGGER_TX_REPLAY | WPA_TRIGGER_TX_REPLAY_FRAG |
726 WPA_TRIGGER_TX_SKIP_SEQ)) {
727 printk(KERN_INFO "%s: WPA testing - CCMP TX packet number "
728 "%s%s%s%s\n", tx->dev->name,
729 tx->sta ? "[UNICAST]" : "[MULTICAST]",
730 test & WPA_TRIGGER_TX_REPLAY ? "[REPLAY]" : "",
731 test & WPA_TRIGGER_TX_REPLAY_FRAG ?
732 "[REPLAY FRAG]" : "",
733 test & WPA_TRIGGER_TX_SKIP_SEQ ? "[SKIP SEQ]" : "");
734 }
735 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
736
737 tx->u.tx.control->icv_len = CCMP_MIC_LEN;
738 tx->u.tx.control->iv_len = CCMP_HDR_LEN;
739 ieee80211_tx_set_iswep(tx);
740
741 if (!tx->key->force_sw_encrypt &&
742 !(tx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV)) {
743 /* hwaccel - with no need for preallocated room for CCMP "
744 * header or MIC fields */
745 tx->u.tx.control->key_idx = tx->key->hw_key_idx;
746 return TXRX_CONTINUE;
747 }
748
749 if (ccmp_encrypt_skb(tx, skb, test) < 0)
750 return TXRX_DROP;
751
752 if (tx->u.tx.extra_frag) {
753 int i;
754 #ifdef CONFIG_HOSTAPD_WPA_TESTING
755 if (test & WPA_TRIGGER_TX_REPLAY_FRAG)
756 test |= WPA_TRIGGER_TX_REPLAY;
757 #endif /* CONFIG_HOSTAPD_WPA_TESTING */
758 for (i = 0; i < tx->u.tx.num_extra_frag; i++) {
759 if (ccmp_encrypt_skb(tx, tx->u.tx.extra_frag[i], test)
760 < 0)
761 return TXRX_DROP;
762 }
763 }
764
765 return TXRX_CONTINUE;
766 }
767
768
769 ieee80211_txrx_result
770 ieee80211_rx_h_ccmp_decrypt(struct ieee80211_txrx_data *rx)
771 {
772 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) rx->skb->data;
773 u16 fc;
774 int hdrlen;
775 struct ieee80211_key *key = rx->key;
776 struct sk_buff *skb = rx->skb;
777 u8 pn[CCMP_PN_LEN];
778 int data_len;
779
780 fc = le16_to_cpu(hdr->frame_control);
781 hdrlen = ieee80211_get_hdrlen(fc);
782
783 if (!key || key->alg != ALG_CCMP ||
784 !(rx->fc & IEEE80211_FCTL_PROTECTED) ||
785 (rx->fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
786 return TXRX_CONTINUE;
787
788 data_len = skb->len - hdrlen - CCMP_HDR_LEN - CCMP_MIC_LEN;
789 if (!rx->sta || data_len < 0)
790 return TXRX_DROP;
791
792 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
793 !key->force_sw_encrypt &&
794 !(rx->local->hw.flags & IEEE80211_HW_WEP_INCLUDE_IV))
795 return TXRX_CONTINUE;
796
797 (void) ccmp_hdr2pn(pn, skb->data + hdrlen);
798
799 if (memcmp(pn, key->u.ccmp.rx_pn[rx->u.rx.queue], CCMP_PN_LEN) <= 0) {
800 #ifdef CONFIG_D80211_DEBUG
801 u8 *ppn = key->u.ccmp.rx_pn[rx->u.rx.queue];
802 printk(KERN_DEBUG "%s: CCMP replay detected for RX frame from "
803 MAC_FMT " (RX PN %02x%02x%02x%02x%02x%02x <= prev. PN "
804 "%02x%02x%02x%02x%02x%02x)\n", rx->dev->name,
805 MAC_ARG(rx->sta->addr),
806 pn[0], pn[1], pn[2], pn[3], pn[4], pn[5],
807 ppn[0], ppn[1], ppn[2], ppn[3], ppn[4], ppn[5]);
808 #endif /* CONFIG_D80211_DEBUG */
809 key->u.ccmp.replays++;
810 return TXRX_DROP;
811 }
812
813 if ((rx->u.rx.status->flag & RX_FLAG_DECRYPTED) &&
814 !key->force_sw_encrypt) {
815 /* hwaccel has already decrypted frame and verified MIC */
816 } else {
817 u8 *scratch, *b_0, *aad;
818
819 scratch = key->u.ccmp.rx_crypto_buf;
820 b_0 = scratch + 3 * AES_BLOCK_LEN;
821 aad = scratch + 4 * AES_BLOCK_LEN;
822
823 ccmp_special_blocks(skb, pn, b_0, aad, 1);
824
825 if (ieee80211_aes_ccm_decrypt(
826 key->u.ccmp.tfm, scratch, b_0, aad,
827 skb->data + hdrlen + CCMP_HDR_LEN, data_len,
828 skb->data + skb->len - CCMP_MIC_LEN,
829 skb->data + hdrlen + CCMP_HDR_LEN)) {
830 printk(KERN_DEBUG "%s: CCMP decrypt failed for RX "
831 "frame from " MAC_FMT "\n", rx->dev->name,
832 MAC_ARG(rx->sta->addr));
833 return TXRX_DROP;
834 }
835 }
836
837 memcpy(key->u.ccmp.rx_pn[rx->u.rx.queue], pn, CCMP_PN_LEN);
838
839 /* Remove CCMP header and MIC */
840 skb_trim(skb, skb->len - CCMP_MIC_LEN);
841 memmove(skb->data + CCMP_HDR_LEN, skb->data, hdrlen);
842 skb_pull(skb, CCMP_HDR_LEN);
843
844 return TXRX_CONTINUE;
845 }
846
This page took 0.078887 seconds and 5 git commands to generate.