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