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