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