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