]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/wpa_auth.c
Add a drop_sa command to allow 802.11w testing
[thirdparty/hostap.git] / src / ap / wpa_auth.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd - IEEE 802.11i-2004 / WPA Authenticator
e6965d4e 3 * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
6fc6879b
JM
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
6226e38d 15#include "utils/includes.h"
6fc6879b
JM
16
17#ifndef CONFIG_NATIVE_WINDOWS
18
6226e38d
JM
19#include "utils/common.h"
20#include "utils/eloop.h"
21#include "utils/state_machine.h"
81f4f619 22#include "common/ieee802_11_defs.h"
03da66bd
JM
23#include "crypto/aes_wrap.h"
24#include "crypto/crypto.h"
25#include "crypto/sha1.h"
26#include "crypto/sha256.h"
281c950b 27#include "eapol_auth/eapol_auth_sm.h"
6226e38d 28#include "ap_config.h"
6fc6879b 29#include "ieee802_11.h"
6226e38d
JM
30#include "wpa_auth.h"
31#include "pmksa_cache_auth.h"
6fc6879b
JM
32#include "wpa_auth_i.h"
33#include "wpa_auth_ie.h"
34
35#define STATE_MACHINE_DATA struct wpa_state_machine
36#define STATE_MACHINE_DEBUG_PREFIX "WPA"
37#define STATE_MACHINE_ADDR sm->addr
38
39
40static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
e4a6ea1d 41static int wpa_sm_step(struct wpa_state_machine *sm);
6fc6879b
JM
42static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
43static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
44static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
45 struct wpa_group *group);
581a8cde 46static void wpa_request_new_ptk(struct wpa_state_machine *sm);
e6965d4e
JM
47static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
48 struct wpa_group *group);
6fc6879b 49
bae61562
JM
50static const u32 dot11RSNAConfigGroupUpdateCount = 4;
51static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
52static const u32 eapol_key_timeout_first = 100; /* ms */
53static const u32 eapol_key_timeout_subseq = 1000; /* ms */
6fc6879b
JM
54
55/* TODO: make these configurable */
56static const int dot11RSNAConfigPMKLifetime = 43200;
57static const int dot11RSNAConfigPMKReauthThreshold = 70;
58static const int dot11RSNAConfigSATimeout = 60;
59
60
61static inline void wpa_auth_mic_failure_report(
62 struct wpa_authenticator *wpa_auth, const u8 *addr)
63{
64 if (wpa_auth->cb.mic_failure_report)
65 wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
66}
67
68
69static inline void wpa_auth_set_eapol(struct wpa_authenticator *wpa_auth,
70 const u8 *addr, wpa_eapol_variable var,
71 int value)
72{
73 if (wpa_auth->cb.set_eapol)
74 wpa_auth->cb.set_eapol(wpa_auth->cb.ctx, addr, var, value);
75}
76
77
78static inline int wpa_auth_get_eapol(struct wpa_authenticator *wpa_auth,
79 const u8 *addr, wpa_eapol_variable var)
80{
81 if (wpa_auth->cb.get_eapol == NULL)
82 return -1;
83 return wpa_auth->cb.get_eapol(wpa_auth->cb.ctx, addr, var);
84}
85
86
87static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
88 const u8 *addr, const u8 *prev_psk)
89{
90 if (wpa_auth->cb.get_psk == NULL)
91 return NULL;
92 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, prev_psk);
93}
94
95
96static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
97 const u8 *addr, u8 *msk, size_t *len)
98{
99 if (wpa_auth->cb.get_msk == NULL)
100 return -1;
101 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
102}
103
104
105static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
106 int vlan_id,
71934751 107 enum wpa_alg alg, const u8 *addr, int idx,
6fc6879b
JM
108 u8 *key, size_t key_len)
109{
110 if (wpa_auth->cb.set_key == NULL)
111 return -1;
112 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
113 key, key_len);
114}
115
116
117static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
118 const u8 *addr, int idx, u8 *seq)
119{
120 if (wpa_auth->cb.get_seqnum == NULL)
121 return -1;
122 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
123}
124
125
6fc6879b
JM
126static inline int
127wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
128 const u8 *data, size_t data_len, int encrypt)
129{
130 if (wpa_auth->cb.send_eapol == NULL)
131 return -1;
132 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
133 encrypt);
134}
135
136
137int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
138 int (*cb)(struct wpa_state_machine *sm, void *ctx),
139 void *cb_ctx)
140{
141 if (wpa_auth->cb.for_each_sta == NULL)
142 return 0;
143 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
144}
145
146
bf98f7f3
JM
147int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
148 int (*cb)(struct wpa_authenticator *a, void *ctx),
149 void *cb_ctx)
150{
151 if (wpa_auth->cb.for_each_auth == NULL)
152 return 0;
153 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
154}
155
156
6fc6879b
JM
157void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
158 logger_level level, const char *txt)
159{
160 if (wpa_auth->cb.logger == NULL)
161 return;
162 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
163}
164
165
166void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
167 logger_level level, const char *fmt, ...)
168{
169 char *format;
170 int maxlen;
171 va_list ap;
172
173 if (wpa_auth->cb.logger == NULL)
174 return;
175
176 maxlen = os_strlen(fmt) + 100;
177 format = os_malloc(maxlen);
178 if (!format)
179 return;
180
181 va_start(ap, fmt);
182 vsnprintf(format, maxlen, fmt, ap);
183 va_end(ap);
184
185 wpa_auth_logger(wpa_auth, addr, level, format);
186
187 os_free(format);
188}
189
190
191static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
192 const u8 *addr)
193{
194 if (wpa_auth->cb.disconnect == NULL)
195 return;
196 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
197 WLAN_REASON_PREV_AUTH_NOT_VALID);
198}
199
200
201static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
202{
56586197 203 int ret = 0;
6fc6879b 204#ifdef CONFIG_IEEE80211R
56586197
JM
205 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
206 ret = 1;
6fc6879b 207#endif /* CONFIG_IEEE80211R */
56586197
JM
208#ifdef CONFIG_IEEE80211W
209 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
210 ret = 1;
211#endif /* CONFIG_IEEE80211W */
212 return ret;
6fc6879b
JM
213}
214
215
216static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
217{
218 struct wpa_authenticator *wpa_auth = eloop_ctx;
219
220 if (os_get_random(wpa_auth->group->GMK, WPA_GMK_LEN)) {
221 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
222 "initialization.");
223 } else {
224 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
225 }
226
227 if (wpa_auth->conf.wpa_gmk_rekey) {
228 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
229 wpa_rekey_gmk, wpa_auth, NULL);
230 }
231}
232
233
234static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
235{
236 struct wpa_authenticator *wpa_auth = eloop_ctx;
237 struct wpa_group *group;
238
239 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
240 for (group = wpa_auth->group; group; group = group->next) {
241 group->GTKReKey = TRUE;
242 do {
243 group->changed = FALSE;
244 wpa_group_sm_step(wpa_auth, group);
245 } while (group->changed);
246 }
247
248 if (wpa_auth->conf.wpa_group_rekey) {
249 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
250 0, wpa_rekey_gtk, wpa_auth, NULL);
251 }
252}
253
254
581a8cde
JM
255static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
256{
257 struct wpa_authenticator *wpa_auth = eloop_ctx;
258 struct wpa_state_machine *sm = timeout_ctx;
259
260 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
261 wpa_request_new_ptk(sm);
262 wpa_sm_step(sm);
263}
264
265
6fc6879b
JM
266static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
267{
268 if (sm->pmksa == ctx)
269 sm->pmksa = NULL;
270 return 0;
271}
272
273
274static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
275 void *ctx)
276{
277 struct wpa_authenticator *wpa_auth = ctx;
278 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
279}
280
281
e6965d4e 282static void wpa_group_set_key_len(struct wpa_group *group, int cipher)
6fc6879b 283{
e6965d4e 284 switch (cipher) {
6fc6879b
JM
285 case WPA_CIPHER_CCMP:
286 group->GTK_len = 16;
287 break;
288 case WPA_CIPHER_TKIP:
289 group->GTK_len = 32;
290 break;
291 case WPA_CIPHER_WEP104:
292 group->GTK_len = 13;
293 break;
294 case WPA_CIPHER_WEP40:
295 group->GTK_len = 5;
296 break;
297 }
e6965d4e
JM
298}
299
300
301static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
302 int vlan_id)
303{
304 struct wpa_group *group;
305 u8 buf[ETH_ALEN + 8 + sizeof(group)];
306 u8 rkey[32];
307
308 group = os_zalloc(sizeof(struct wpa_group));
309 if (group == NULL)
310 return NULL;
311
312 group->GTKAuthenticator = TRUE;
313 group->vlan_id = vlan_id;
314
315 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
6fc6879b
JM
316
317 /* Counter = PRF-256(Random number, "Init Counter",
318 * Local MAC Address || Time)
319 */
320 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
321 wpa_get_ntp_timestamp(buf + ETH_ALEN);
322 os_memcpy(buf + ETH_ALEN + 8, &group, sizeof(group));
323 if (os_get_random(rkey, sizeof(rkey)) ||
324 os_get_random(group->GMK, WPA_GMK_LEN)) {
325 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
326 "initialization.");
327 os_free(group);
328 return NULL;
329 }
330
331 sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
332 group->Counter, WPA_NONCE_LEN);
333
334 group->GInit = TRUE;
335 wpa_group_sm_step(wpa_auth, group);
336 group->GInit = FALSE;
337 wpa_group_sm_step(wpa_auth, group);
338
339 return group;
340}
341
342
343/**
344 * wpa_init - Initialize WPA authenticator
345 * @addr: Authenticator address
346 * @conf: Configuration for WPA authenticator
a17df5fb 347 * @cb: Callback functions for WPA authenticator
6fc6879b
JM
348 * Returns: Pointer to WPA authenticator data or %NULL on failure
349 */
350struct wpa_authenticator * wpa_init(const u8 *addr,
351 struct wpa_auth_config *conf,
352 struct wpa_auth_callbacks *cb)
353{
354 struct wpa_authenticator *wpa_auth;
355
356 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
357 if (wpa_auth == NULL)
358 return NULL;
359 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
360 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
361 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
362
363 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
364 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
365 os_free(wpa_auth);
366 return NULL;
367 }
368
369 wpa_auth->group = wpa_group_init(wpa_auth, 0);
370 if (wpa_auth->group == NULL) {
371 os_free(wpa_auth->wpa_ie);
372 os_free(wpa_auth);
373 return NULL;
374 }
375
4bb081f1
JM
376 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
377 wpa_auth);
6fc6879b
JM
378 if (wpa_auth->pmksa == NULL) {
379 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
380 os_free(wpa_auth->wpa_ie);
381 os_free(wpa_auth);
382 return NULL;
383 }
384
385#ifdef CONFIG_IEEE80211R
386 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
387 if (wpa_auth->ft_pmk_cache == NULL) {
388 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
389 os_free(wpa_auth->wpa_ie);
4bb081f1 390 pmksa_cache_auth_deinit(wpa_auth->pmksa);
6fc6879b
JM
391 os_free(wpa_auth);
392 return NULL;
393 }
394#endif /* CONFIG_IEEE80211R */
395
396 if (wpa_auth->conf.wpa_gmk_rekey) {
397 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
398 wpa_rekey_gmk, wpa_auth, NULL);
399 }
400
401 if (wpa_auth->conf.wpa_group_rekey) {
402 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
403 wpa_rekey_gtk, wpa_auth, NULL);
404 }
405
406 return wpa_auth;
407}
408
409
410/**
411 * wpa_deinit - Deinitialize WPA authenticator
412 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
413 */
414void wpa_deinit(struct wpa_authenticator *wpa_auth)
415{
416 struct wpa_group *group, *prev;
417
418 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
419 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
420
421#ifdef CONFIG_PEERKEY
422 while (wpa_auth->stsl_negotiations)
423 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
424#endif /* CONFIG_PEERKEY */
425
4bb081f1 426 pmksa_cache_auth_deinit(wpa_auth->pmksa);
6fc6879b
JM
427
428#ifdef CONFIG_IEEE80211R
429 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
430 wpa_auth->ft_pmk_cache = NULL;
431#endif /* CONFIG_IEEE80211R */
432
433 os_free(wpa_auth->wpa_ie);
434
435 group = wpa_auth->group;
436 while (group) {
437 prev = group;
438 group = group->next;
439 os_free(prev);
440 }
441
442 os_free(wpa_auth);
443}
444
445
446/**
447 * wpa_reconfig - Update WPA authenticator configuration
448 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
449 * @conf: Configuration for WPA authenticator
450 */
451int wpa_reconfig(struct wpa_authenticator *wpa_auth,
452 struct wpa_auth_config *conf)
453{
e6965d4e 454 struct wpa_group *group;
6fc6879b
JM
455 if (wpa_auth == NULL)
456 return 0;
457
458 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
ad08c363
JM
459 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
460 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
461 return -1;
462 }
463
e6965d4e
JM
464 /*
465 * Reinitialize GTK to make sure it is suitable for the new
466 * configuration.
467 */
468 group = wpa_auth->group;
469 wpa_group_set_key_len(group, wpa_auth->conf.wpa_group);
470 group->GInit = TRUE;
471 wpa_group_sm_step(wpa_auth, group);
472 group->GInit = FALSE;
473 wpa_group_sm_step(wpa_auth, group);
474
6fc6879b
JM
475 return 0;
476}
477
478
479struct wpa_state_machine *
480wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr)
481{
482 struct wpa_state_machine *sm;
483
484 sm = os_zalloc(sizeof(struct wpa_state_machine));
485 if (sm == NULL)
486 return NULL;
487 os_memcpy(sm->addr, addr, ETH_ALEN);
488
489 sm->wpa_auth = wpa_auth;
490 sm->group = wpa_auth->group;
491
492 return sm;
493}
494
495
6f9b5d16
JM
496int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
497 struct wpa_state_machine *sm)
6fc6879b
JM
498{
499 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
6f9b5d16 500 return -1;
6fc6879b
JM
501
502#ifdef CONFIG_IEEE80211R
503 if (sm->ft_completed) {
504 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
505 "FT authentication already completed - do not "
506 "start 4-way handshake");
6f9b5d16 507 return 0;
6fc6879b
JM
508 }
509#endif /* CONFIG_IEEE80211R */
510
511 if (sm->started) {
22a299ee 512 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
6fc6879b 513 sm->ReAuthenticationRequest = TRUE;
6f9b5d16 514 return wpa_sm_step(sm);
6fc6879b
JM
515 }
516
517 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
518 "start authentication");
519 sm->started = 1;
520
521 sm->Init = TRUE;
e4a6ea1d 522 if (wpa_sm_step(sm) == 1)
6f9b5d16 523 return 1; /* should not really happen */
6fc6879b
JM
524 sm->Init = FALSE;
525 sm->AuthenticationRequest = TRUE;
6f9b5d16 526 return wpa_sm_step(sm);
6fc6879b
JM
527}
528
529
a8d05fca
JM
530void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
531{
532 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
533 * reassociates back to the same AP while the previous entry for the
534 * STA has not yet been removed. */
535 if (sm == NULL)
536 return;
537
538 sm->wpa_key_mgmt = 0;
539}
540
541
6fc6879b
JM
542static void wpa_free_sta_sm(struct wpa_state_machine *sm)
543{
544 os_free(sm->last_rx_eapol_key);
545 os_free(sm->wpa_ie);
546 os_free(sm);
547}
548
549
550void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
551{
552 if (sm == NULL)
553 return;
554
555 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
556 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
557 "strict rekeying - force GTK rekey since STA "
558 "is leaving");
559 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
560 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
561 NULL);
562 }
563
564 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
565 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
581a8cde 566 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
6fc6879b
JM
567 if (sm->in_step_loop) {
568 /* Must not free state machine while wpa_sm_step() is running.
569 * Freeing will be completed in the end of wpa_sm_step(). */
570 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
571 "machine deinit for " MACSTR, MAC2STR(sm->addr));
572 sm->pending_deinit = 1;
573 } else
574 wpa_free_sta_sm(sm);
575}
576
577
578static void wpa_request_new_ptk(struct wpa_state_machine *sm)
579{
580 if (sm == NULL)
581 return;
582
583 sm->PTKRequest = TRUE;
584 sm->PTK_valid = 0;
585}
586
587
22a299ee
JM
588static int wpa_replay_counter_valid(struct wpa_state_machine *sm,
589 const u8 *replay_counter)
590{
591 int i;
592 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
593 if (!sm->key_replay[i].valid)
594 break;
595 if (os_memcmp(replay_counter, sm->key_replay[i].counter,
596 WPA_REPLAY_COUNTER_LEN) == 0)
597 return 1;
598 }
599 return 0;
600}
601
602
6fc6879b
JM
603void wpa_receive(struct wpa_authenticator *wpa_auth,
604 struct wpa_state_machine *sm,
605 u8 *data, size_t data_len)
606{
607 struct ieee802_1x_hdr *hdr;
608 struct wpa_eapol_key *key;
609 u16 key_info, key_data_length;
610 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
611 SMK_M1, SMK_M3, SMK_ERROR } msg;
612 char *msgtxt;
613 struct wpa_eapol_ie_parse kde;
614
615 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
616 return;
617
618 if (data_len < sizeof(*hdr) + sizeof(*key))
619 return;
620
621 hdr = (struct ieee802_1x_hdr *) data;
622 key = (struct wpa_eapol_key *) (hdr + 1);
623 key_info = WPA_GET_BE16(key->key_info);
624 key_data_length = WPA_GET_BE16(key->key_data_length);
625 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
626 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
627 "key_data overflow (%d > %lu)",
628 key_data_length,
629 (unsigned long) (data_len - sizeof(*hdr) -
630 sizeof(*key)));
631 return;
632 }
633
f8e96eb6
JM
634 if (sm->wpa == WPA_VERSION_WPA2) {
635 if (key->type != EAPOL_KEY_TYPE_RSN) {
636 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
637 "unexpected type %d in RSN mode",
638 key->type);
639 return;
640 }
641 } else {
642 if (key->type != EAPOL_KEY_TYPE_WPA) {
643 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
644 "unexpected type %d in WPA mode",
645 key->type);
646 return;
647 }
648 }
649
6fc6879b
JM
650 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
651 * are set */
652
653 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
654 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
655 if (key_info & WPA_KEY_INFO_ERROR) {
656 msg = SMK_ERROR;
657 msgtxt = "SMK Error";
658 } else {
659 msg = SMK_M1;
660 msgtxt = "SMK M1";
661 }
662 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
663 msg = SMK_M3;
664 msgtxt = "SMK M3";
665 } else if (key_info & WPA_KEY_INFO_REQUEST) {
666 msg = REQUEST;
667 msgtxt = "Request";
668 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
669 msg = GROUP_2;
670 msgtxt = "2/2 Group";
671 } else if (key_data_length == 0) {
672 msg = PAIRWISE_4;
673 msgtxt = "4/4 Pairwise";
674 } else {
675 msg = PAIRWISE_2;
676 msgtxt = "2/4 Pairwise";
677 }
678
679 /* TODO: key_info type validation for PeerKey */
680 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
681 msg == GROUP_2) {
682 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
683 if (sm->pairwise == WPA_CIPHER_CCMP) {
684 if (wpa_use_aes_cmac(sm) &&
685 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
686 wpa_auth_logger(wpa_auth, sm->addr,
687 LOGGER_WARNING,
688 "advertised support for "
689 "AES-128-CMAC, but did not "
690 "use it");
691 return;
692 }
693
694 if (!wpa_use_aes_cmac(sm) &&
695 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
696 wpa_auth_logger(wpa_auth, sm->addr,
697 LOGGER_WARNING,
698 "did not use HMAC-SHA1-AES "
699 "with CCMP");
700 return;
701 }
702 }
703 }
704
705 if (key_info & WPA_KEY_INFO_REQUEST) {
706 if (sm->req_replay_counter_used &&
707 os_memcmp(key->replay_counter, sm->req_replay_counter,
708 WPA_REPLAY_COUNTER_LEN) <= 0) {
709 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
710 "received EAPOL-Key request with "
711 "replayed counter");
712 return;
713 }
714 }
715
716 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
22a299ee
JM
717 !wpa_replay_counter_valid(sm, key->replay_counter)) {
718 int i;
6fc6879b
JM
719 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
720 "received EAPOL-Key %s with unexpected "
721 "replay counter", msgtxt);
22a299ee
JM
722 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
723 if (!sm->key_replay[i].valid)
724 break;
725 wpa_hexdump(MSG_DEBUG, "pending replay counter",
726 sm->key_replay[i].counter,
727 WPA_REPLAY_COUNTER_LEN);
728 }
6fc6879b
JM
729 wpa_hexdump(MSG_DEBUG, "received replay counter",
730 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
731 return;
732 }
733
734 switch (msg) {
735 case PAIRWISE_2:
736 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
737 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
738 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
739 "received EAPOL-Key msg 2/4 in "
740 "invalid state (%d) - dropped",
741 sm->wpa_ptk_state);
742 return;
743 }
744 if (sm->wpa_ie == NULL ||
745 sm->wpa_ie_len != key_data_length ||
746 os_memcmp(sm->wpa_ie, key + 1, key_data_length) != 0) {
747 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
748 "WPA IE from (Re)AssocReq did not "
749 "match with msg 2/4");
750 if (sm->wpa_ie) {
751 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
752 sm->wpa_ie, sm->wpa_ie_len);
753 }
754 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
755 (u8 *) (key + 1), key_data_length);
756 /* MLME-DEAUTHENTICATE.request */
757 wpa_sta_disconnect(wpa_auth, sm->addr);
758 return;
759 }
760 break;
761 case PAIRWISE_4:
762 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
763 !sm->PTK_valid) {
764 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
765 "received EAPOL-Key msg 4/4 in "
766 "invalid state (%d) - dropped",
767 sm->wpa_ptk_state);
768 return;
769 }
770 break;
771 case GROUP_2:
772 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
773 || !sm->PTK_valid) {
774 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
775 "received EAPOL-Key msg 2/2 in "
776 "invalid state (%d) - dropped",
777 sm->wpa_ptk_group_state);
778 return;
779 }
780 break;
781#ifdef CONFIG_PEERKEY
782 case SMK_M1:
783 case SMK_M3:
784 case SMK_ERROR:
785 if (!wpa_auth->conf.peerkey) {
786 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
787 "PeerKey use disabled - ignoring message");
788 return;
789 }
790 if (!sm->PTK_valid) {
791 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
792 "received EAPOL-Key msg SMK in "
793 "invalid state - dropped");
794 return;
795 }
796 break;
797#else /* CONFIG_PEERKEY */
798 case SMK_M1:
799 case SMK_M3:
800 case SMK_ERROR:
801 return; /* STSL disabled - ignore SMK messages */
802#endif /* CONFIG_PEERKEY */
803 case REQUEST:
804 break;
805 }
806
807 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
808 "received EAPOL-Key frame (%s)", msgtxt);
809
810 if (key_info & WPA_KEY_INFO_ACK) {
811 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
812 "received invalid EAPOL-Key: Key Ack set");
813 return;
814 }
815
816 if (!(key_info & WPA_KEY_INFO_MIC)) {
817 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
818 "received invalid EAPOL-Key: Key MIC not set");
819 return;
820 }
821
822 sm->MICVerified = FALSE;
823 if (sm->PTK_valid) {
824 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
825 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
826 "received EAPOL-Key with invalid MIC");
827 return;
828 }
829 sm->MICVerified = TRUE;
830 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
831 }
832
833 if (key_info & WPA_KEY_INFO_REQUEST) {
834 if (sm->MICVerified) {
835 sm->req_replay_counter_used = 1;
836 os_memcpy(sm->req_replay_counter, key->replay_counter,
837 WPA_REPLAY_COUNTER_LEN);
838 } else {
839 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
840 "received EAPOL-Key request with "
841 "invalid MIC");
842 return;
843 }
844
845 /*
846 * TODO: should decrypt key data field if encryption was used;
847 * even though MAC address KDE is not normally encrypted,
848 * supplicant is allowed to encrypt it.
849 */
850 if (msg == SMK_ERROR) {
851#ifdef CONFIG_PEERKEY
852 wpa_smk_error(wpa_auth, sm, key);
853#endif /* CONFIG_PEERKEY */
854 return;
855 } else if (key_info & WPA_KEY_INFO_ERROR) {
856 /* Supplicant reported a Michael MIC error */
857 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
858 "received EAPOL-Key Error Request "
859 "(STA detected Michael MIC failure)");
860 wpa_auth_mic_failure_report(wpa_auth, sm->addr);
861 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
862 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
863 /* Error report is not a request for a new key
864 * handshake, but since Authenticator may do it, let's
865 * change the keys now anyway. */
866 wpa_request_new_ptk(sm);
867 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
868 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
869 "received EAPOL-Key Request for new "
870 "4-Way Handshake");
871 wpa_request_new_ptk(sm);
872#ifdef CONFIG_PEERKEY
873 } else if (msg == SMK_M1) {
874 wpa_smk_m1(wpa_auth, sm, key);
875#endif /* CONFIG_PEERKEY */
876 } else if (key_data_length > 0 &&
877 wpa_parse_kde_ies((const u8 *) (key + 1),
878 key_data_length, &kde) == 0 &&
879 kde.mac_addr) {
880 } else {
881 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
882 "received EAPOL-Key Request for GTK "
883 "rekeying");
884 /* FIX: why was this triggering PTK rekeying for the
885 * STA that requested Group Key rekeying?? */
886 /* wpa_request_new_ptk(sta->wpa_sm); */
887 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
888 wpa_rekey_gtk(wpa_auth, NULL);
889 }
890 } else {
22a299ee
JM
891 /* Do not allow the same key replay counter to be reused. This
892 * does also invalidate all other pending replay counters if
893 * retransmissions were used, i.e., we will only process one of
894 * the pending replies and ignore rest if more than one is
895 * received. */
896 sm->key_replay[0].valid = FALSE;
6fc6879b
JM
897 }
898
899#ifdef CONFIG_PEERKEY
900 if (msg == SMK_M3) {
901 wpa_smk_m3(wpa_auth, sm, key);
902 return;
903 }
904#endif /* CONFIG_PEERKEY */
905
906 os_free(sm->last_rx_eapol_key);
907 sm->last_rx_eapol_key = os_malloc(data_len);
908 if (sm->last_rx_eapol_key == NULL)
909 return;
910 os_memcpy(sm->last_rx_eapol_key, data, data_len);
911 sm->last_rx_eapol_key_len = data_len;
912
913 sm->EAPOLKeyReceived = TRUE;
914 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
915 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
916 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
917 wpa_sm_step(sm);
918}
919
920
921static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
922 u8 *gtk, size_t gtk_len)
923{
924 u8 data[ETH_ALEN + WPA_NONCE_LEN];
925
926 /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
927 os_memcpy(data, addr, ETH_ALEN);
928 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
929
56586197
JM
930#ifdef CONFIG_IEEE80211W
931 sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
932 data, sizeof(data), gtk, gtk_len);
933#else /* CONFIG_IEEE80211W */
6fc6879b
JM
934 sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
935 data, sizeof(data), gtk, gtk_len);
56586197 936#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
937
938 wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
939 wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
940}
941
942
943static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
944{
945 struct wpa_authenticator *wpa_auth = eloop_ctx;
946 struct wpa_state_machine *sm = timeout_ctx;
947
948 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
949 sm->TimeoutEvt = TRUE;
950 wpa_sm_step(sm);
951}
952
953
954void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
955 struct wpa_state_machine *sm, int key_info,
956 const u8 *key_rsc, const u8 *nonce,
957 const u8 *kde, size_t kde_len,
958 int keyidx, int encr, int force_version)
959{
960 struct ieee802_1x_hdr *hdr;
961 struct wpa_eapol_key *key;
962 size_t len;
963 int alg;
964 int key_data_len, pad_len = 0;
965 u8 *buf, *pos;
966 int version, pairwise;
22a299ee 967 int i;
6fc6879b
JM
968
969 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
970
971 if (force_version)
972 version = force_version;
973 else if (wpa_use_aes_cmac(sm))
974 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
975 else if (sm->pairwise == WPA_CIPHER_CCMP)
976 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
977 else
978 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
979
980 pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
981
982 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
983 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
984 "encr=%d)",
985 version,
986 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
987 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
988 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
989 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
990 pairwise, (unsigned long) kde_len, keyidx, encr);
991
992 key_data_len = kde_len;
993
994 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
995 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
996 pad_len = key_data_len % 8;
997 if (pad_len)
998 pad_len = 8 - pad_len;
999 key_data_len += pad_len + 8;
1000 }
1001
1002 len += key_data_len;
1003
1004 hdr = os_zalloc(len);
1005 if (hdr == NULL)
1006 return;
1007 hdr->version = wpa_auth->conf.eapol_version;
1008 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1009 hdr->length = host_to_be16(len - sizeof(*hdr));
1010 key = (struct wpa_eapol_key *) (hdr + 1);
1011
1012 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1013 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1014 key_info |= version;
1015 if (encr && sm->wpa == WPA_VERSION_WPA2)
1016 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1017 if (sm->wpa != WPA_VERSION_WPA2)
1018 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1019 WPA_PUT_BE16(key->key_info, key_info);
1020
1021 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1022 switch (alg) {
1023 case WPA_CIPHER_CCMP:
1024 WPA_PUT_BE16(key->key_length, 16);
1025 break;
1026 case WPA_CIPHER_TKIP:
1027 WPA_PUT_BE16(key->key_length, 32);
1028 break;
1029 case WPA_CIPHER_WEP40:
1030 WPA_PUT_BE16(key->key_length, 5);
1031 break;
1032 case WPA_CIPHER_WEP104:
1033 WPA_PUT_BE16(key->key_length, 13);
1034 break;
1035 }
1036 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1037 WPA_PUT_BE16(key->key_length, 0);
1038
1039 /* FIX: STSL: what to use as key_replay_counter? */
22a299ee
JM
1040 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1041 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1042 os_memcpy(sm->key_replay[i].counter,
1043 sm->key_replay[i - 1].counter,
1044 WPA_REPLAY_COUNTER_LEN);
1045 }
1046 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1047 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
6fc6879b 1048 WPA_REPLAY_COUNTER_LEN);
22a299ee 1049 sm->key_replay[0].valid = TRUE;
6fc6879b
JM
1050
1051 if (nonce)
1052 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1053
1054 if (key_rsc)
1055 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1056
1057 if (kde && !encr) {
1058 os_memcpy(key + 1, kde, kde_len);
1059 WPA_PUT_BE16(key->key_data_length, kde_len);
1060 } else if (encr && kde) {
1061 buf = os_zalloc(key_data_len);
1062 if (buf == NULL) {
1063 os_free(hdr);
1064 return;
1065 }
1066 pos = buf;
1067 os_memcpy(pos, kde, kde_len);
1068 pos += kde_len;
1069
1070 if (pad_len)
1071 *pos++ = 0xdd;
1072
1073 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1074 buf, key_data_len);
1075 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1076 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1077 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1078 (u8 *) (key + 1))) {
1079 os_free(hdr);
1080 os_free(buf);
1081 return;
1082 }
1083 WPA_PUT_BE16(key->key_data_length, key_data_len);
1084 } else {
1085 u8 ek[32];
1086 os_memcpy(key->key_iv,
1087 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1088 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1089 os_memcpy(ek, key->key_iv, 16);
1090 os_memcpy(ek + 16, sm->PTK.kek, 16);
1091 os_memcpy(key + 1, buf, key_data_len);
1092 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1093 WPA_PUT_BE16(key->key_data_length, key_data_len);
1094 }
1095 os_free(buf);
1096 }
1097
1098 if (key_info & WPA_KEY_INFO_MIC) {
1099 if (!sm->PTK_valid) {
1100 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1101 "PTK not valid when sending EAPOL-Key "
1102 "frame");
1103 os_free(hdr);
1104 return;
1105 }
1106 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1107 key->key_mic);
1108 }
1109
1110 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1111 1);
1112 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1113 sm->pairwise_set);
1114 os_free(hdr);
1115}
1116
1117
1118static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1119 struct wpa_state_machine *sm, int key_info,
1120 const u8 *key_rsc, const u8 *nonce,
1121 const u8 *kde, size_t kde_len,
1122 int keyidx, int encr)
1123{
1124 int timeout_ms;
1125 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
bae61562 1126 int ctr;
6fc6879b
JM
1127
1128 if (sm == NULL)
1129 return;
1130
1131 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1132 keyidx, encr, 0);
1133
bae61562
JM
1134 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1135 if (ctr == 1)
1136 timeout_ms = eapol_key_timeout_first;
1137 else
1138 timeout_ms = eapol_key_timeout_subseq;
6fc6879b
JM
1139 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1140 wpa_send_eapol_timeout, wpa_auth, sm);
1141}
1142
1143
1144static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1145{
1146 struct ieee802_1x_hdr *hdr;
1147 struct wpa_eapol_key *key;
1148 u16 key_info;
1149 int ret = 0;
1150 u8 mic[16];
1151
1152 if (data_len < sizeof(*hdr) + sizeof(*key))
1153 return -1;
1154
1155 hdr = (struct ieee802_1x_hdr *) data;
1156 key = (struct wpa_eapol_key *) (hdr + 1);
1157 key_info = WPA_GET_BE16(key->key_info);
1158 os_memcpy(mic, key->key_mic, 16);
1159 os_memset(key->key_mic, 0, 16);
1160 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1161 data, data_len, key->key_mic) ||
1162 os_memcmp(mic, key->key_mic, 16) != 0)
1163 ret = -1;
1164 os_memcpy(key->key_mic, mic, 16);
1165 return ret;
1166}
1167
1168
1169void wpa_remove_ptk(struct wpa_state_machine *sm)
1170{
1171 sm->PTK_valid = FALSE;
1172 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
89d39d9d
JM
1173 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, (u8 *) "",
1174 0);
6fc6879b 1175 sm->pairwise_set = FALSE;
581a8cde 1176 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
6fc6879b
JM
1177}
1178
1179
6f9b5d16 1180int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
6fc6879b 1181{
5d22a1d5
JM
1182 int remove_ptk = 1;
1183
6fc6879b 1184 if (sm == NULL)
6f9b5d16 1185 return -1;
6fc6879b
JM
1186
1187 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1188 "event %d notification", event);
1189
1190 switch (event) {
1191 case WPA_AUTH:
1192 case WPA_ASSOC:
1193 break;
1194 case WPA_DEAUTH:
1195 case WPA_DISASSOC:
1196 sm->DeauthenticationRequest = TRUE;
1197 break;
1198 case WPA_REAUTH:
1199 case WPA_REAUTH_EAPOL:
9663596f
JM
1200 if (sm->GUpdateStationKeys) {
1201 /*
1202 * Reauthentication cancels the pending group key
1203 * update for this STA.
1204 */
1205 sm->group->GKeyDoneStations--;
1206 sm->GUpdateStationKeys = FALSE;
1207 sm->PtkGroupInit = TRUE;
1208 }
6fc6879b
JM
1209 sm->ReAuthenticationRequest = TRUE;
1210 break;
1211 case WPA_ASSOC_FT:
1212#ifdef CONFIG_IEEE80211R
0e84c254
JM
1213 if (!sm->pairwise_set) {
1214 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1215 "after association");
1216 wpa_ft_install_ptk(sm);
1217 }
6fc6879b
JM
1218 /* Using FT protocol, not WPA auth state machine */
1219 sm->ft_completed = 1;
6f9b5d16 1220 return 0;
6fc6879b
JM
1221#else /* CONFIG_IEEE80211R */
1222 break;
1223#endif /* CONFIG_IEEE80211R */
1224 }
1225
1226#ifdef CONFIG_IEEE80211R
1227 sm->ft_completed = 0;
1228#endif /* CONFIG_IEEE80211R */
1229
5d22a1d5
JM
1230#ifdef CONFIG_IEEE80211W
1231 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1232 remove_ptk = 0;
1233#endif /* CONFIG_IEEE80211W */
6fc6879b 1234
5d22a1d5
JM
1235 if (remove_ptk) {
1236 sm->PTK_valid = FALSE;
1237 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1238
1239 if (event != WPA_REAUTH_EAPOL)
1240 wpa_remove_ptk(sm);
1241 }
6fc6879b 1242
6f9b5d16 1243 return wpa_sm_step(sm);
6fc6879b
JM
1244}
1245
1246
71934751 1247static enum wpa_alg wpa_alg_enum(int alg)
6fc6879b
JM
1248{
1249 switch (alg) {
1250 case WPA_CIPHER_CCMP:
89d39d9d 1251 return WPA_ALG_CCMP;
6fc6879b 1252 case WPA_CIPHER_TKIP:
89d39d9d 1253 return WPA_ALG_TKIP;
6fc6879b
JM
1254 case WPA_CIPHER_WEP104:
1255 case WPA_CIPHER_WEP40:
89d39d9d 1256 return WPA_ALG_WEP;
6fc6879b 1257 default:
89d39d9d 1258 return WPA_ALG_NONE;
6fc6879b
JM
1259 }
1260}
1261
1262
1263SM_STATE(WPA_PTK, INITIALIZE)
1264{
1265 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1266 if (sm->Init) {
1267 /* Init flag is not cleared here, so avoid busy
1268 * loop by claiming nothing changed. */
1269 sm->changed = FALSE;
1270 }
1271
1272 sm->keycount = 0;
1273 if (sm->GUpdateStationKeys)
1274 sm->group->GKeyDoneStations--;
1275 sm->GUpdateStationKeys = FALSE;
1276 if (sm->wpa == WPA_VERSION_WPA)
1277 sm->PInitAKeys = FALSE;
1278 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1279 * Local AA > Remote AA)) */) {
1280 sm->Pair = TRUE;
1281 }
1282 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1283 wpa_remove_ptk(sm);
1284 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1285 sm->TimeoutCtr = 0;
56586197 1286 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
6fc6879b
JM
1287 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1288 WPA_EAPOL_authorized, 0);
1289 }
1290}
1291
1292
1293SM_STATE(WPA_PTK, DISCONNECT)
1294{
1295 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1296 sm->Disconnect = FALSE;
1297 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1298}
1299
1300
1301SM_STATE(WPA_PTK, DISCONNECTED)
1302{
1303 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1304 sm->DeauthenticationRequest = FALSE;
1305}
1306
1307
1308SM_STATE(WPA_PTK, AUTHENTICATION)
1309{
1310 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1311 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1312 sm->PTK_valid = FALSE;
1313 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1314 1);
1315 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1316 sm->AuthenticationRequest = FALSE;
1317}
1318
1319
1320SM_STATE(WPA_PTK, AUTHENTICATION2)
1321{
1322 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1323 os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
1324 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1325 sm->ReAuthenticationRequest = FALSE;
1326 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1327 * logical place than INITIALIZE since AUTHENTICATION2 can be
1328 * re-entered on ReAuthenticationRequest without going through
1329 * INITIALIZE. */
1330 sm->TimeoutCtr = 0;
1331}
1332
1333
1334SM_STATE(WPA_PTK, INITPMK)
1335{
1336 u8 msk[2 * PMK_LEN];
1337 size_t len = 2 * PMK_LEN;
1338
1339 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1340#ifdef CONFIG_IEEE80211R
1341 sm->xxkey_len = 0;
1342#endif /* CONFIG_IEEE80211R */
1343 if (sm->pmksa) {
1344 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1345 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1346 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1347 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1348 "(len=%lu)", (unsigned long) len);
1349 os_memcpy(sm->PMK, msk, PMK_LEN);
1350#ifdef CONFIG_IEEE80211R
1351 if (len >= 2 * PMK_LEN) {
1352 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1353 sm->xxkey_len = PMK_LEN;
1354 }
1355#endif /* CONFIG_IEEE80211R */
1356 } else {
1357 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1358 }
1359
1360 sm->req_replay_counter_used = 0;
1361 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1362 * will break reauthentication since EAPOL state machines may not be
1363 * get into AUTHENTICATING state that clears keyRun before WPA state
1364 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1365 * state and takes PMK from the previously used AAA Key. This will
1366 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1367 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1368 * be good workaround for this issue. */
1369 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1370}
1371
1372
1373SM_STATE(WPA_PTK, INITPSK)
1374{
1375 const u8 *psk;
1376 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1377 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1378 if (psk) {
1379 os_memcpy(sm->PMK, psk, PMK_LEN);
1380#ifdef CONFIG_IEEE80211R
1381 os_memcpy(sm->xxkey, psk, PMK_LEN);
1382 sm->xxkey_len = PMK_LEN;
1383#endif /* CONFIG_IEEE80211R */
1384 }
1385 sm->req_replay_counter_used = 0;
1386}
1387
1388
1389SM_STATE(WPA_PTK, PTKSTART)
1390{
1391 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1392 size_t pmkid_len = 0;
1393
1394 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1395 sm->PTKRequest = FALSE;
1396 sm->TimeoutEvt = FALSE;
bae61562
JM
1397
1398 sm->TimeoutCtr++;
1399 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1400 /* No point in sending the EAPOL-Key - we will disconnect
1401 * immediately following this. */
1402 return;
1403 }
1404
6fc6879b
JM
1405 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1406 "sending 1/4 msg of 4-Way Handshake");
1407 /*
1408 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1409 * one possible PSK for this STA.
1410 */
1411 if (sm->wpa == WPA_VERSION_WPA2 &&
56586197 1412 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
6fc6879b
JM
1413 pmkid = buf;
1414 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1415 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1416 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1417 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1418 if (sm->pmksa)
1419 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1420 sm->pmksa->pmkid, PMKID_LEN);
1421 else {
1422 /*
1423 * Calculate PMKID since no PMKSA cache entry was
1424 * available with pre-calculated PMKID.
1425 */
1426 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
56586197
JM
1427 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1428 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
6fc6879b
JM
1429 }
1430 }
1431 wpa_send_eapol(sm->wpa_auth, sm,
1432 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1433 sm->ANonce, pmkid, pmkid_len, 0, 0);
6fc6879b
JM
1434}
1435
1436
1437static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1438 struct wpa_ptk *ptk)
1439{
c0a61908 1440 size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
6fc6879b 1441#ifdef CONFIG_IEEE80211R
56586197 1442 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
c0a61908 1443 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
6fc6879b
JM
1444#endif /* CONFIG_IEEE80211R */
1445
1446 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1447 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
c0a61908 1448 (u8 *) ptk, ptk_len,
56586197 1449 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
6fc6879b
JM
1450
1451 return 0;
1452}
1453
1454
1455SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1456{
1457 struct wpa_ptk PTK;
1458 int ok = 0;
1459 const u8 *pmk = NULL;
1460
1461 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1462 sm->EAPOLKeyReceived = FALSE;
1463
1464 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1465 * WPA-PSK: iterate through possible PSKs and select the one matching
1466 * the packet */
1467 for (;;) {
56586197 1468 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
6fc6879b
JM
1469 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1470 if (pmk == NULL)
1471 break;
1472 } else
1473 pmk = sm->PMK;
1474
1475 wpa_derive_ptk(sm, pmk, &PTK);
1476
1477 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1478 sm->last_rx_eapol_key_len) == 0) {
1479 ok = 1;
1480 break;
1481 }
1482
56586197 1483 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
6fc6879b
JM
1484 break;
1485 }
1486
1487 if (!ok) {
1488 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1489 "invalid MIC in msg 2/4 of 4-Way Handshake");
1490 return;
1491 }
1492
1493 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1494
56586197 1495 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
6fc6879b
JM
1496 /* PSK may have changed from the previous choice, so update
1497 * state machine data based on whatever PSK was selected here.
1498 */
1499 os_memcpy(sm->PMK, pmk, PMK_LEN);
1500 }
1501
1502 sm->MICVerified = TRUE;
1503
1504 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1505 sm->PTK_valid = TRUE;
1506}
1507
1508
1509SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1510{
1511 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1512 sm->TimeoutCtr = 0;
1513}
1514
1515
1516#ifdef CONFIG_IEEE80211W
1517
1518static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1519{
1520 if (sm->mgmt_frame_prot) {
1521 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1522 }
1523
1524 return 0;
1525}
1526
1527
1528static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1529{
1530 struct wpa_igtk_kde igtk;
1531 struct wpa_group *gsm = sm->group;
1532
1533 if (!sm->mgmt_frame_prot)
1534 return pos;
1535
1536 igtk.keyid[0] = gsm->GN_igtk;
1537 igtk.keyid[1] = 0;
9008a3e4 1538 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0)
6fc6879b
JM
1539 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1540 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1541 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1542 (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1543
1544 return pos;
1545}
1546
1547#else /* CONFIG_IEEE80211W */
1548
1549static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1550{
1551 return 0;
1552}
1553
1554
1555static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1556{
1557 return pos;
1558}
1559
1560#endif /* CONFIG_IEEE80211W */
1561
1562
1563SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1564{
1565 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
1566 size_t gtk_len, kde_len;
1567 struct wpa_group *gsm = sm->group;
1568 u8 *wpa_ie;
1569 int wpa_ie_len, secure, keyidx, encr = 0;
1570
1571 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1572 sm->TimeoutEvt = FALSE;
bae61562
JM
1573
1574 sm->TimeoutCtr++;
1575 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1576 /* No point in sending the EAPOL-Key - we will disconnect
1577 * immediately following this. */
1578 return;
1579 }
1580
6fc6879b
JM
1581 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1582 */
1583 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1584 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1585 wpa_ie = sm->wpa_auth->wpa_ie;
1586 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1587 if (sm->wpa == WPA_VERSION_WPA &&
1588 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1589 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1590 /* WPA-only STA, remove RSN IE */
1591 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1592 wpa_ie_len = wpa_ie[1] + 2;
1593 }
1594 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1595 "sending 3/4 msg of 4-Way Handshake");
1596 if (sm->wpa == WPA_VERSION_WPA2) {
1597 /* WPA2 send GTK in the 4-way handshake */
1598 secure = 1;
1599 gtk = gsm->GTK[gsm->GN - 1];
1600 gtk_len = gsm->GTK_len;
1601 keyidx = gsm->GN;
1602 _rsc = rsc;
1603 encr = 1;
1604 } else {
1605 /* WPA does not include GTK in msg 3/4 */
1606 secure = 0;
1607 gtk = NULL;
1608 gtk_len = 0;
1609 keyidx = 0;
1610 _rsc = NULL;
1611 }
1612
1613 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1614 if (gtk)
1615 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1616 kde = os_malloc(kde_len);
1617 if (kde == NULL)
1618 return;
1619
1620 pos = kde;
1621 os_memcpy(pos, wpa_ie, wpa_ie_len);
1622 pos += wpa_ie_len;
1623 if (gtk) {
1624 u8 hdr[2];
1625 hdr[0] = keyidx & 0x03;
1626 hdr[1] = 0;
1627 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1628 gtk, gtk_len);
1629 }
1630 pos = ieee80211w_kde_add(sm, pos);
1631
1632 wpa_send_eapol(sm->wpa_auth, sm,
1633 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
1634 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
1635 WPA_KEY_INFO_KEY_TYPE,
1636 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
1637 os_free(kde);
6fc6879b
JM
1638}
1639
1640
1641SM_STATE(WPA_PTK, PTKINITDONE)
1642{
1643 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
1644 sm->EAPOLKeyReceived = FALSE;
1645 if (sm->Pair) {
71934751 1646 enum wpa_alg alg;
6fc6879b
JM
1647 int klen;
1648 if (sm->pairwise == WPA_CIPHER_TKIP) {
89d39d9d 1649 alg = WPA_ALG_TKIP;
6fc6879b
JM
1650 klen = 32;
1651 } else {
89d39d9d 1652 alg = WPA_ALG_CCMP;
6fc6879b
JM
1653 klen = 16;
1654 }
1655 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
1656 sm->PTK.tk1, klen)) {
1657 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1658 return;
1659 }
1660 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1661 sm->pairwise_set = TRUE;
1662
581a8cde
JM
1663 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
1664 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1665 eloop_register_timeout(sm->wpa_auth->conf.
1666 wpa_ptk_rekey, 0, wpa_rekey_ptk,
1667 sm->wpa_auth, sm);
1668 }
1669
56586197 1670 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
6fc6879b
JM
1671 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1672 WPA_EAPOL_authorized, 1);
1673 }
1674 }
1675
1676 if (0 /* IBSS == TRUE */) {
1677 sm->keycount++;
1678 if (sm->keycount == 2) {
1679 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1680 WPA_EAPOL_portValid, 1);
1681 }
1682 } else {
1683 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
1684 1);
1685 }
1686 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
1687 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
1688 if (sm->wpa == WPA_VERSION_WPA)
1689 sm->PInitAKeys = TRUE;
1690 else
1691 sm->has_GTK = TRUE;
1692 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1693 "pairwise key handshake completed (%s)",
1694 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1695
1696#ifdef CONFIG_IEEE80211R
1697 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
1698#endif /* CONFIG_IEEE80211R */
1699}
1700
1701
1702SM_STEP(WPA_PTK)
1703{
1704 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1705
1706 if (sm->Init)
1707 SM_ENTER(WPA_PTK, INITIALIZE);
1708 else if (sm->Disconnect
1709 /* || FIX: dot11RSNAConfigSALifetime timeout */)
1710 SM_ENTER(WPA_PTK, DISCONNECT);
1711 else if (sm->DeauthenticationRequest)
1712 SM_ENTER(WPA_PTK, DISCONNECTED);
1713 else if (sm->AuthenticationRequest)
1714 SM_ENTER(WPA_PTK, AUTHENTICATION);
1715 else if (sm->ReAuthenticationRequest)
1716 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1717 else if (sm->PTKRequest)
1718 SM_ENTER(WPA_PTK, PTKSTART);
1719 else switch (sm->wpa_ptk_state) {
1720 case WPA_PTK_INITIALIZE:
1721 break;
1722 case WPA_PTK_DISCONNECT:
1723 SM_ENTER(WPA_PTK, DISCONNECTED);
1724 break;
1725 case WPA_PTK_DISCONNECTED:
1726 SM_ENTER(WPA_PTK, INITIALIZE);
1727 break;
1728 case WPA_PTK_AUTHENTICATION:
1729 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1730 break;
1731 case WPA_PTK_AUTHENTICATION2:
56586197 1732 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
6fc6879b
JM
1733 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1734 WPA_EAPOL_keyRun) > 0)
1735 SM_ENTER(WPA_PTK, INITPMK);
56586197 1736 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
6fc6879b
JM
1737 /* FIX: && 802.1X::keyRun */)
1738 SM_ENTER(WPA_PTK, INITPSK);
1739 break;
1740 case WPA_PTK_INITPMK:
1741 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1742 WPA_EAPOL_keyAvailable) > 0)
1743 SM_ENTER(WPA_PTK, PTKSTART);
1744 else {
1745 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1746 SM_ENTER(WPA_PTK, DISCONNECT);
1747 }
1748 break;
1749 case WPA_PTK_INITPSK:
1750 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
1751 SM_ENTER(WPA_PTK, PTKSTART);
1752 else {
1753 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1754 "no PSK configured for the STA");
1755 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1756 SM_ENTER(WPA_PTK, DISCONNECT);
1757 }
1758 break;
1759 case WPA_PTK_PTKSTART:
1760 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1761 sm->EAPOLKeyPairwise)
1762 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1763 else if (sm->TimeoutCtr >
1764 (int) dot11RSNAConfigPairwiseUpdateCount) {
1765 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1766 SM_ENTER(WPA_PTK, DISCONNECT);
1767 } else if (sm->TimeoutEvt)
1768 SM_ENTER(WPA_PTK, PTKSTART);
1769 break;
1770 case WPA_PTK_PTKCALCNEGOTIATING:
1771 if (sm->MICVerified)
1772 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
1773 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1774 sm->EAPOLKeyPairwise)
1775 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1776 else if (sm->TimeoutEvt)
1777 SM_ENTER(WPA_PTK, PTKSTART);
1778 break;
1779 case WPA_PTK_PTKCALCNEGOTIATING2:
1780 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1781 break;
1782 case WPA_PTK_PTKINITNEGOTIATING:
1783 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1784 sm->EAPOLKeyPairwise && sm->MICVerified)
1785 SM_ENTER(WPA_PTK, PTKINITDONE);
1786 else if (sm->TimeoutCtr >
1787 (int) dot11RSNAConfigPairwiseUpdateCount) {
1788 wpa_auth->dot11RSNA4WayHandshakeFailures++;
1789 SM_ENTER(WPA_PTK, DISCONNECT);
1790 } else if (sm->TimeoutEvt)
1791 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1792 break;
1793 case WPA_PTK_PTKINITDONE:
1794 break;
1795 }
1796}
1797
1798
1799SM_STATE(WPA_PTK_GROUP, IDLE)
1800{
1801 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
1802 if (sm->Init) {
1803 /* Init flag is not cleared here, so avoid busy
1804 * loop by claiming nothing changed. */
1805 sm->changed = FALSE;
1806 }
1807 sm->GTimeoutCtr = 0;
1808}
1809
1810
1811SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
1812{
1813 u8 rsc[WPA_KEY_RSC_LEN];
1814 struct wpa_group *gsm = sm->group;
1815 u8 *kde, *pos, hdr[2];
1816 size_t kde_len;
1817
1818 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
bae61562
JM
1819
1820 sm->GTimeoutCtr++;
1821 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
1822 /* No point in sending the EAPOL-Key - we will disconnect
1823 * immediately following this. */
1824 return;
1825 }
1826
6fc6879b
JM
1827 if (sm->wpa == WPA_VERSION_WPA)
1828 sm->PInitAKeys = FALSE;
1829 sm->TimeoutEvt = FALSE;
1830 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1831 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1832 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
1833 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1834 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1835 "sending 1/2 msg of Group Key Handshake");
1836
1837 if (sm->wpa == WPA_VERSION_WPA2) {
1838 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
1839 ieee80211w_kde_len(sm);
1840 kde = os_malloc(kde_len);
1841 if (kde == NULL)
1842 return;
1843
1844 pos = kde;
1845 hdr[0] = gsm->GN & 0x03;
1846 hdr[1] = 0;
1847 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1848 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
1849 pos = ieee80211w_kde_add(sm, pos);
1850 } else {
1851 kde = gsm->GTK[gsm->GN - 1];
1852 pos = kde + gsm->GTK_len;
1853 }
1854
1855 wpa_send_eapol(sm->wpa_auth, sm,
1856 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
1857 WPA_KEY_INFO_ACK |
1858 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
1859 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
1860 if (sm->wpa == WPA_VERSION_WPA2)
1861 os_free(kde);
6fc6879b
JM
1862}
1863
1864
1865SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
1866{
1867 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
1868 sm->EAPOLKeyReceived = FALSE;
1869 if (sm->GUpdateStationKeys)
1870 sm->group->GKeyDoneStations--;
1871 sm->GUpdateStationKeys = FALSE;
1872 sm->GTimeoutCtr = 0;
1873 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1874 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1875 "group key handshake completed (%s)",
1876 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1877 sm->has_GTK = TRUE;
1878}
1879
1880
1881SM_STATE(WPA_PTK_GROUP, KEYERROR)
1882{
1883 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
1884 if (sm->GUpdateStationKeys)
1885 sm->group->GKeyDoneStations--;
1886 sm->GUpdateStationKeys = FALSE;
1887 sm->Disconnect = TRUE;
1888}
1889
1890
1891SM_STEP(WPA_PTK_GROUP)
1892{
9663596f 1893 if (sm->Init || sm->PtkGroupInit) {
6fc6879b 1894 SM_ENTER(WPA_PTK_GROUP, IDLE);
9663596f
JM
1895 sm->PtkGroupInit = FALSE;
1896 } else switch (sm->wpa_ptk_group_state) {
6fc6879b
JM
1897 case WPA_PTK_GROUP_IDLE:
1898 if (sm->GUpdateStationKeys ||
1899 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
1900 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1901 break;
1902 case WPA_PTK_GROUP_REKEYNEGOTIATING:
1903 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1904 !sm->EAPOLKeyPairwise && sm->MICVerified)
1905 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
1906 else if (sm->GTimeoutCtr >
1907 (int) dot11RSNAConfigGroupUpdateCount)
1908 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
1909 else if (sm->TimeoutEvt)
1910 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1911 break;
1912 case WPA_PTK_GROUP_KEYERROR:
1913 SM_ENTER(WPA_PTK_GROUP, IDLE);
1914 break;
1915 case WPA_PTK_GROUP_REKEYESTABLISHED:
1916 SM_ENTER(WPA_PTK_GROUP, IDLE);
1917 break;
1918 }
1919}
1920
1921
1922static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
1923 struct wpa_group *group)
1924{
1925 int ret = 0;
1926
1927 /* FIX: is this the correct way of getting GNonce? */
1928 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
1929 inc_byte_array(group->Counter, WPA_NONCE_LEN);
1930 wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
1931 group->GTK[group->GN - 1], group->GTK_len);
1932
1933#ifdef CONFIG_IEEE80211W
70f8cc8e 1934 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
6fc6879b
JM
1935 if (os_get_random(group->IGTK[group->GN_igtk - 4],
1936 WPA_IGTK_LEN) < 0) {
1937 wpa_printf(MSG_INFO, "RSN: Failed to get new random "
1938 "IGTK");
1939 ret = -1;
1940 }
1941 wpa_hexdump_key(MSG_DEBUG, "IGTK",
1942 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
1943 }
1944#endif /* CONFIG_IEEE80211W */
1945
1946 return ret;
1947}
1948
1949
1950static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
1951 struct wpa_group *group)
1952{
1953 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
1954 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
1955 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
1956 group->wpa_group_state = WPA_GROUP_GTK_INIT;
1957
1958 /* GTK[0..N] = 0 */
1959 os_memset(group->GTK, 0, sizeof(group->GTK));
1960 group->GN = 1;
1961 group->GM = 2;
1962#ifdef CONFIG_IEEE80211W
1963 group->GN_igtk = 4;
1964 group->GM_igtk = 5;
1965#endif /* CONFIG_IEEE80211W */
1966 /* GTK[GN] = CalcGTK() */
1967 wpa_gtk_update(wpa_auth, group);
1968}
1969
1970
1971static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
1972{
1973 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
1974 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1975 "Not in PTKINITDONE; skip Group Key update");
1976 return 0;
1977 }
9663596f
JM
1978 if (sm->GUpdateStationKeys) {
1979 /*
1980 * This should not really happen, but just in case, make sure
1981 * we do not count the same STA twice in GKeyDoneStations.
1982 */
1983 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1984 "GUpdateStationKeys already set - do not "
1985 "increment GKeyDoneStations");
1986 } else {
1987 sm->group->GKeyDoneStations++;
1988 sm->GUpdateStationKeys = TRUE;
1989 }
6fc6879b
JM
1990 wpa_sm_step(sm);
1991 return 0;
1992}
1993
1994
1995static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
1996 struct wpa_group *group)
1997{
1998 int tmp;
1999
2000 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2001 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2002 group->changed = TRUE;
2003 group->wpa_group_state = WPA_GROUP_SETKEYS;
2004 group->GTKReKey = FALSE;
2005 tmp = group->GM;
2006 group->GM = group->GN;
2007 group->GN = tmp;
2008#ifdef CONFIG_IEEE80211W
2009 tmp = group->GM_igtk;
2010 group->GM_igtk = group->GN_igtk;
2011 group->GN_igtk = tmp;
2012#endif /* CONFIG_IEEE80211W */
2013 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2014 * counting the STAs that are marked with GUpdateStationKeys instead of
2015 * including all STAs that could be in not-yet-completed state. */
2016 wpa_gtk_update(wpa_auth, group);
2017
2018 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
2019 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2020 group->GKeyDoneStations);
2021}
2022
2023
2024static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2025 struct wpa_group *group)
2026{
2027 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2028 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2029 group->changed = TRUE;
2030 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2031 wpa_auth_set_key(wpa_auth, group->vlan_id,
89d39d9d 2032 wpa_alg_enum(wpa_auth->conf.wpa_group),
6fc6879b
JM
2033 NULL, group->GN, group->GTK[group->GN - 1],
2034 group->GTK_len);
2035
2036#ifdef CONFIG_IEEE80211W
70f8cc8e 2037 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
89d39d9d 2038 wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
6fc6879b
JM
2039 NULL, group->GN_igtk,
2040 group->IGTK[group->GN_igtk - 4],
2041 WPA_IGTK_LEN);
2042 }
2043#endif /* CONFIG_IEEE80211W */
2044}
2045
2046
2047static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2048 struct wpa_group *group)
2049{
2050 if (group->GInit) {
2051 wpa_group_gtk_init(wpa_auth, group);
2052 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2053 group->GTKAuthenticator) {
2054 wpa_group_setkeysdone(wpa_auth, group);
2055 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2056 group->GTKReKey) {
2057 wpa_group_setkeys(wpa_auth, group);
2058 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2059 if (group->GKeyDoneStations == 0)
2060 wpa_group_setkeysdone(wpa_auth, group);
2061 else if (group->GTKReKey)
2062 wpa_group_setkeys(wpa_auth, group);
2063 }
2064}
2065
2066
e4a6ea1d 2067static int wpa_sm_step(struct wpa_state_machine *sm)
6fc6879b
JM
2068{
2069 if (sm == NULL)
e4a6ea1d 2070 return 0;
6fc6879b
JM
2071
2072 if (sm->in_step_loop) {
2073 /* This should not happen, but if it does, make sure we do not
2074 * end up freeing the state machine too early by exiting the
2075 * recursive call. */
2076 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
e4a6ea1d 2077 return 0;
6fc6879b
JM
2078 }
2079
2080 sm->in_step_loop = 1;
2081 do {
2082 if (sm->pending_deinit)
2083 break;
2084
2085 sm->changed = FALSE;
2086 sm->wpa_auth->group->changed = FALSE;
2087
2088 SM_STEP_RUN(WPA_PTK);
2089 if (sm->pending_deinit)
2090 break;
2091 SM_STEP_RUN(WPA_PTK_GROUP);
2092 if (sm->pending_deinit)
2093 break;
2094 wpa_group_sm_step(sm->wpa_auth, sm->group);
2095 } while (sm->changed || sm->wpa_auth->group->changed);
2096 sm->in_step_loop = 0;
2097
2098 if (sm->pending_deinit) {
2099 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2100 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2101 wpa_free_sta_sm(sm);
e4a6ea1d 2102 return 1;
6fc6879b 2103 }
e4a6ea1d 2104 return 0;
6fc6879b
JM
2105}
2106
2107
2108static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2109{
2110 struct wpa_state_machine *sm = eloop_ctx;
2111 wpa_sm_step(sm);
2112}
2113
2114
2115void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2116{
2117 if (sm == NULL)
2118 return;
2119 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2120}
2121
2122
2123void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2124{
2125 int tmp, i;
2126 struct wpa_group *group;
2127
2128 if (wpa_auth == NULL)
2129 return;
2130
2131 group = wpa_auth->group;
2132
2133 for (i = 0; i < 2; i++) {
2134 tmp = group->GM;
2135 group->GM = group->GN;
2136 group->GN = tmp;
2137#ifdef CONFIG_IEEE80211W
2138 tmp = group->GM_igtk;
2139 group->GM_igtk = group->GN_igtk;
2140 group->GN_igtk = tmp;
2141#endif /* CONFIG_IEEE80211W */
2142 wpa_gtk_update(wpa_auth, group);
2143 }
2144}
2145
2146
2147static const char * wpa_bool_txt(int bool)
2148{
2149 return bool ? "TRUE" : "FALSE";
2150}
2151
2152
2153static int wpa_cipher_bits(int cipher)
2154{
2155 switch (cipher) {
2156 case WPA_CIPHER_CCMP:
2157 return 128;
2158 case WPA_CIPHER_TKIP:
2159 return 256;
2160 case WPA_CIPHER_WEP104:
2161 return 104;
2162 case WPA_CIPHER_WEP40:
2163 return 40;
2164 default:
2165 return 0;
2166 }
2167}
2168
2169
2170#define RSN_SUITE "%02x-%02x-%02x-%d"
2171#define RSN_SUITE_ARG(s) \
2172((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2173
2174int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2175{
2176 int len = 0, ret;
2177 char pmkid_txt[PMKID_LEN * 2 + 1];
2178
2179 if (wpa_auth == NULL)
2180 return len;
2181
2182 ret = os_snprintf(buf + len, buflen - len,
2183 "dot11RSNAOptionImplemented=TRUE\n"
2184#ifdef CONFIG_RSN_PREAUTH
2185 "dot11RSNAPreauthenticationImplemented=TRUE\n"
2186#else /* CONFIG_RSN_PREAUTH */
2187 "dot11RSNAPreauthenticationImplemented=FALSE\n"
2188#endif /* CONFIG_RSN_PREAUTH */
2189 "dot11RSNAEnabled=%s\n"
2190 "dot11RSNAPreauthenticationEnabled=%s\n",
2191 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2192 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2193 if (ret < 0 || (size_t) ret >= buflen - len)
2194 return len;
2195 len += ret;
2196
2197 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2198 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2199
2200 ret = os_snprintf(
2201 buf + len, buflen - len,
2202 "dot11RSNAConfigVersion=%u\n"
2203 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2204 /* FIX: dot11RSNAConfigGroupCipher */
2205 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2206 /* FIX: dot11RSNAConfigGroupRekeyTime */
2207 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2208 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2209 "dot11RSNAConfigGroupUpdateCount=%u\n"
2210 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2211 "dot11RSNAConfigGroupCipherSize=%u\n"
2212 "dot11RSNAConfigPMKLifetime=%u\n"
2213 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2214 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2215 "dot11RSNAConfigSATimeout=%u\n"
2216 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2217 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2218 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2219 "dot11RSNAPMKIDUsed=%s\n"
2220 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2221 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2222 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2223 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2224 "dot11RSNA4WayHandshakeFailures=%u\n"
2225 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2226 RSN_VERSION,
2227 !!wpa_auth->conf.wpa_strict_rekey,
2228 dot11RSNAConfigGroupUpdateCount,
2229 dot11RSNAConfigPairwiseUpdateCount,
2230 wpa_cipher_bits(wpa_auth->conf.wpa_group),
2231 dot11RSNAConfigPMKLifetime,
2232 dot11RSNAConfigPMKReauthThreshold,
2233 dot11RSNAConfigSATimeout,
2234 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2235 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2236 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2237 pmkid_txt,
2238 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2239 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2240 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2241 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2242 wpa_auth->dot11RSNA4WayHandshakeFailures);
2243 if (ret < 0 || (size_t) ret >= buflen - len)
2244 return len;
2245 len += ret;
2246
2247 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2248 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2249
2250 /* Private MIB */
2251 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2252 wpa_auth->group->wpa_group_state);
2253 if (ret < 0 || (size_t) ret >= buflen - len)
2254 return len;
2255 len += ret;
2256
2257 return len;
2258}
2259
2260
2261int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2262{
2263 int len = 0, ret;
2264 u32 pairwise = 0;
2265
2266 if (sm == NULL)
2267 return 0;
2268
2269 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2270
2271 /* dot11RSNAStatsEntry */
2272
2273 if (sm->wpa == WPA_VERSION_WPA) {
2274 if (sm->pairwise == WPA_CIPHER_CCMP)
2275 pairwise = WPA_CIPHER_SUITE_CCMP;
2276 else if (sm->pairwise == WPA_CIPHER_TKIP)
2277 pairwise = WPA_CIPHER_SUITE_TKIP;
2278 else if (sm->pairwise == WPA_CIPHER_WEP104)
2279 pairwise = WPA_CIPHER_SUITE_WEP104;
2280 else if (sm->pairwise == WPA_CIPHER_WEP40)
2281 pairwise = WPA_CIPHER_SUITE_WEP40;
2282 else if (sm->pairwise == WPA_CIPHER_NONE)
2283 pairwise = WPA_CIPHER_SUITE_NONE;
2284 } else if (sm->wpa == WPA_VERSION_WPA2) {
2285 if (sm->pairwise == WPA_CIPHER_CCMP)
2286 pairwise = RSN_CIPHER_SUITE_CCMP;
2287 else if (sm->pairwise == WPA_CIPHER_TKIP)
2288 pairwise = RSN_CIPHER_SUITE_TKIP;
2289 else if (sm->pairwise == WPA_CIPHER_WEP104)
2290 pairwise = RSN_CIPHER_SUITE_WEP104;
2291 else if (sm->pairwise == WPA_CIPHER_WEP40)
2292 pairwise = RSN_CIPHER_SUITE_WEP40;
2293 else if (sm->pairwise == WPA_CIPHER_NONE)
2294 pairwise = RSN_CIPHER_SUITE_NONE;
2295 } else
2296 return 0;
2297
2298 ret = os_snprintf(
2299 buf + len, buflen - len,
2300 /* TODO: dot11RSNAStatsIndex */
2301 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2302 "dot11RSNAStatsVersion=1\n"
2303 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2304 /* TODO: dot11RSNAStatsTKIPICVErrors */
2305 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2306 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2307 /* TODO: dot11RSNAStatsCCMPReplays */
2308 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2309 /* TODO: dot11RSNAStatsTKIPReplays */,
2310 MAC2STR(sm->addr),
2311 RSN_SUITE_ARG(pairwise),
2312 sm->dot11RSNAStatsTKIPLocalMICFailures,
2313 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2314 if (ret < 0 || (size_t) ret >= buflen - len)
2315 return len;
2316 len += ret;
2317
2318 /* Private MIB */
2319 ret = os_snprintf(buf + len, buflen - len,
2320 "hostapdWPAPTKState=%d\n"
2321 "hostapdWPAPTKGroupState=%d\n",
2322 sm->wpa_ptk_state,
2323 sm->wpa_ptk_group_state);
2324 if (ret < 0 || (size_t) ret >= buflen - len)
2325 return len;
2326 len += ret;
2327
2328 return len;
2329}
2330
2331
2332void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2333{
2334 if (wpa_auth)
2335 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2336}
2337
2338
2339int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2340{
2341 return sm && sm->pairwise_set;
2342}
2343
2344
ff36ff00
JM
2345int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2346{
2347 return sm->pairwise;
2348}
2349
2350
6fc6879b
JM
2351int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2352{
2353 if (sm == NULL)
2354 return -1;
2355 return sm->wpa_key_mgmt;
2356}
2357
2358
2359int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2360{
2361 if (sm == NULL)
2362 return 0;
2363 return sm->wpa;
2364}
2365
2366
2367int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2368 struct rsn_pmksa_cache_entry *entry)
2369{
2370 if (sm == NULL || sm->pmksa != entry)
2371 return -1;
2372 sm->pmksa = NULL;
2373 return 0;
2374}
2375
2376
2377struct rsn_pmksa_cache_entry *
2378wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2379{
2380 return sm ? sm->pmksa : NULL;
2381}
2382
2383
2384void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2385{
2386 if (sm)
2387 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2388}
2389
2390
2391const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2392{
2393 if (wpa_auth == NULL)
2394 return NULL;
2395 *len = wpa_auth->wpa_ie_len;
2396 return wpa_auth->wpa_ie;
2397}
2398
2399
2400int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2401 int session_timeout, struct eapol_state_machine *eapol)
2402{
2403 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
2404 return -1;
2405
4bb081f1
JM
2406 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2407 sm->wpa_auth->addr, sm->addr, session_timeout,
2408 eapol, sm->wpa_key_mgmt))
6fc6879b
JM
2409 return 0;
2410
2411 return -1;
2412}
2413
2414
2415int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2416 const u8 *pmk, size_t len, const u8 *sta_addr,
2417 int session_timeout,
2418 struct eapol_state_machine *eapol)
2419{
2420 if (wpa_auth == NULL)
2421 return -1;
2422
4bb081f1
JM
2423 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2424 sta_addr, session_timeout, eapol,
2425 WPA_KEY_MGMT_IEEE8021X))
6fc6879b
JM
2426 return 0;
2427
2428 return -1;
2429}
2430
2431
2432static struct wpa_group *
2433wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2434{
2435 struct wpa_group *group;
2436
2437 if (wpa_auth == NULL || wpa_auth->group == NULL)
2438 return NULL;
2439
2440 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2441 vlan_id);
2442 group = wpa_group_init(wpa_auth, vlan_id);
2443 if (group == NULL)
2444 return NULL;
2445
2446 group->next = wpa_auth->group->next;
2447 wpa_auth->group->next = group;
2448
2449 return group;
2450}
2451
2452
2453int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2454{
2455 struct wpa_group *group;
2456
2457 if (sm == NULL || sm->wpa_auth == NULL)
2458 return 0;
2459
2460 group = sm->wpa_auth->group;
2461 while (group) {
2462 if (group->vlan_id == vlan_id)
2463 break;
2464 group = group->next;
2465 }
2466
2467 if (group == NULL) {
2468 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2469 if (group == NULL)
2470 return -1;
2471 }
2472
2473 if (sm->group == group)
2474 return 0;
2475
2476 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2477 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
2478
2479 sm->group = group;
2480 return 0;
2481}
2482
2483#endif /* CONFIG_NATIVE_WINDOWS */