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