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