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