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