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