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