FT: Clean up wpa_sm_set_ft_params() by using common parse
[libeap.git] / src / ap / wpa_auth.c
1 /*
2  * hostapd - IEEE 802.11i-2004 / WPA Authenticator
3  * Copyright (c) 2004-2009, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #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         int ft;
613
614         if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
615                 return;
616
617         if (data_len < sizeof(*hdr) + sizeof(*key))
618                 return;
619
620         hdr = (struct ieee802_1x_hdr *) data;
621         key = (struct wpa_eapol_key *) (hdr + 1);
622         key_info = WPA_GET_BE16(key->key_info);
623         key_data_length = WPA_GET_BE16(key->key_data_length);
624         if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
625                 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
626                            "key_data overflow (%d > %lu)",
627                            key_data_length,
628                            (unsigned long) (data_len - sizeof(*hdr) -
629                                             sizeof(*key)));
630                 return;
631         }
632
633         if (sm->wpa == WPA_VERSION_WPA2) {
634                 if (key->type != EAPOL_KEY_TYPE_RSN) {
635                         wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
636                                    "unexpected type %d in RSN mode",
637                                    key->type);
638                         return;
639                 }
640         } else {
641                 if (key->type != EAPOL_KEY_TYPE_WPA) {
642                         wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
643                                    "unexpected type %d in WPA mode",
644                                    key->type);
645                         return;
646                 }
647         }
648
649         /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
650          * are set */
651
652         if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
653             (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
654                 if (key_info & WPA_KEY_INFO_ERROR) {
655                         msg = SMK_ERROR;
656                         msgtxt = "SMK Error";
657                 } else {
658                         msg = SMK_M1;
659                         msgtxt = "SMK M1";
660                 }
661         } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
662                 msg = SMK_M3;
663                 msgtxt = "SMK M3";
664         } else if (key_info & WPA_KEY_INFO_REQUEST) {
665                 msg = REQUEST;
666                 msgtxt = "Request";
667         } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
668                 msg = GROUP_2;
669                 msgtxt = "2/2 Group";
670         } else if (key_data_length == 0) {
671                 msg = PAIRWISE_4;
672                 msgtxt = "4/4 Pairwise";
673         } else {
674                 msg = PAIRWISE_2;
675                 msgtxt = "2/4 Pairwise";
676         }
677
678         /* TODO: key_info type validation for PeerKey */
679         if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
680             msg == GROUP_2) {
681                 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
682                 if (sm->pairwise == WPA_CIPHER_CCMP) {
683                         if (wpa_use_aes_cmac(sm) &&
684                             ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
685                                 wpa_auth_logger(wpa_auth, sm->addr,
686                                                 LOGGER_WARNING,
687                                                 "advertised support for "
688                                                 "AES-128-CMAC, but did not "
689                                                 "use it");
690                                 return;
691                         }
692
693                         if (!wpa_use_aes_cmac(sm) &&
694                             ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
695                                 wpa_auth_logger(wpa_auth, sm->addr,
696                                                 LOGGER_WARNING,
697                                                 "did not use HMAC-SHA1-AES "
698                                                 "with CCMP");
699                                 return;
700                         }
701                 }
702         }
703
704         if (key_info & WPA_KEY_INFO_REQUEST) {
705                 if (sm->req_replay_counter_used &&
706                     os_memcmp(key->replay_counter, sm->req_replay_counter,
707                               WPA_REPLAY_COUNTER_LEN) <= 0) {
708                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
709                                         "received EAPOL-Key request with "
710                                         "replayed counter");
711                         return;
712                 }
713         }
714
715         if (!(key_info & WPA_KEY_INFO_REQUEST) &&
716             !wpa_replay_counter_valid(sm, key->replay_counter)) {
717                 int i;
718                 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
719                                  "received EAPOL-Key %s with unexpected "
720                                  "replay counter", msgtxt);
721                 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
722                         if (!sm->key_replay[i].valid)
723                                 break;
724                         wpa_hexdump(MSG_DEBUG, "pending replay counter",
725                                     sm->key_replay[i].counter,
726                                     WPA_REPLAY_COUNTER_LEN);
727                 }
728                 wpa_hexdump(MSG_DEBUG, "received replay counter",
729                             key->replay_counter, WPA_REPLAY_COUNTER_LEN);
730                 return;
731         }
732
733         switch (msg) {
734         case PAIRWISE_2:
735                 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
736                     sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
737                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
738                                          "received EAPOL-Key msg 2/4 in "
739                                          "invalid state (%d) - dropped",
740                                          sm->wpa_ptk_state);
741                         return;
742                 }
743                 ft = sm->wpa == WPA_VERSION_WPA2 &&
744                         wpa_key_mgmt_ft(sm->wpa_key_mgmt);
745                 if (sm->wpa_ie == NULL ||
746                     wpa_compare_rsn_ie(ft,
747                                        sm->wpa_ie, sm->wpa_ie_len,
748                                        (u8 *) (key + 1), key_data_length)) {
749                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
750                                         "WPA IE from (Re)AssocReq did not "
751                                         "match with msg 2/4");
752                         if (sm->wpa_ie) {
753                                 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
754                                             sm->wpa_ie, sm->wpa_ie_len);
755                         }
756                         wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
757                                     (u8 *) (key + 1), key_data_length);
758                         /* MLME-DEAUTHENTICATE.request */
759                         wpa_sta_disconnect(wpa_auth, sm->addr);
760                         return;
761                 }
762 #ifdef CONFIG_IEEE80211R
763                 if (ft) {
764                         struct wpa_ie_data ie;
765                         if (wpa_parse_wpa_ie_rsn((u8 *) (key + 1),
766                                                  key_data_length, &ie) < 0 ||
767                             ie.num_pmkid != 1 || ie.pmkid == NULL) {
768                                 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
769                                            "FT 4-way handshake message 2/4");
770                                 wpa_sta_disconnect(wpa_auth, sm->addr);
771                                 return;
772                         }
773                         os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
774                         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
775                                     sm->sup_pmk_r1_name, PMKID_LEN);
776                 }
777 #endif /* CONFIG_IEEE80211R */
778                 break;
779         case PAIRWISE_4:
780                 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
781                     !sm->PTK_valid) {
782                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
783                                          "received EAPOL-Key msg 4/4 in "
784                                          "invalid state (%d) - dropped",
785                                          sm->wpa_ptk_state);
786                         return;
787                 }
788                 break;
789         case GROUP_2:
790                 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
791                     || !sm->PTK_valid) {
792                         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
793                                          "received EAPOL-Key msg 2/2 in "
794                                          "invalid state (%d) - dropped",
795                                          sm->wpa_ptk_group_state);
796                         return;
797                 }
798                 break;
799 #ifdef CONFIG_PEERKEY
800         case SMK_M1:
801         case SMK_M3:
802         case SMK_ERROR:
803                 if (!wpa_auth->conf.peerkey) {
804                         wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
805                                    "PeerKey use disabled - ignoring message");
806                         return;
807                 }
808                 if (!sm->PTK_valid) {
809                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
810                                         "received EAPOL-Key msg SMK in "
811                                         "invalid state - dropped");
812                         return;
813                 }
814                 break;
815 #else /* CONFIG_PEERKEY */
816         case SMK_M1:
817         case SMK_M3:
818         case SMK_ERROR:
819                 return; /* STSL disabled - ignore SMK messages */
820 #endif /* CONFIG_PEERKEY */
821         case REQUEST:
822                 break;
823         }
824
825         wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
826                          "received EAPOL-Key frame (%s)", msgtxt);
827
828         if (key_info & WPA_KEY_INFO_ACK) {
829                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
830                                 "received invalid EAPOL-Key: Key Ack set");
831                 return;
832         }
833
834         if (!(key_info & WPA_KEY_INFO_MIC)) {
835                 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
836                                 "received invalid EAPOL-Key: Key MIC not set");
837                 return;
838         }
839
840         sm->MICVerified = FALSE;
841         if (sm->PTK_valid) {
842                 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
843                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
844                                         "received EAPOL-Key with invalid MIC");
845                         return;
846                 }
847                 sm->MICVerified = TRUE;
848                 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
849         }
850
851         if (key_info & WPA_KEY_INFO_REQUEST) {
852                 if (sm->MICVerified) {
853                         sm->req_replay_counter_used = 1;
854                         os_memcpy(sm->req_replay_counter, key->replay_counter,
855                                   WPA_REPLAY_COUNTER_LEN);
856                 } else {
857                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
858                                         "received EAPOL-Key request with "
859                                         "invalid MIC");
860                         return;
861                 }
862
863                 /*
864                  * TODO: should decrypt key data field if encryption was used;
865                  * even though MAC address KDE is not normally encrypted,
866                  * supplicant is allowed to encrypt it.
867                  */
868                 if (msg == SMK_ERROR) {
869 #ifdef CONFIG_PEERKEY
870                         wpa_smk_error(wpa_auth, sm, key);
871 #endif /* CONFIG_PEERKEY */
872                         return;
873                 } else if (key_info & WPA_KEY_INFO_ERROR) {
874                         /* Supplicant reported a Michael MIC error */
875                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
876                                         "received EAPOL-Key Error Request "
877                                         "(STA detected Michael MIC failure)");
878                         wpa_auth_mic_failure_report(wpa_auth, sm->addr);
879                         sm->dot11RSNAStatsTKIPRemoteMICFailures++;
880                         wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
881                         /* Error report is not a request for a new key
882                          * handshake, but since Authenticator may do it, let's
883                          * change the keys now anyway. */
884                         wpa_request_new_ptk(sm);
885                 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
886                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
887                                         "received EAPOL-Key Request for new "
888                                         "4-Way Handshake");
889                         wpa_request_new_ptk(sm);
890 #ifdef CONFIG_PEERKEY
891                 } else if (msg == SMK_M1) {
892                         wpa_smk_m1(wpa_auth, sm, key);
893 #endif /* CONFIG_PEERKEY */
894                 } else if (key_data_length > 0 &&
895                            wpa_parse_kde_ies((const u8 *) (key + 1),
896                                              key_data_length, &kde) == 0 &&
897                            kde.mac_addr) {
898                 } else {
899                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
900                                         "received EAPOL-Key Request for GTK "
901                                         "rekeying");
902                         /* FIX: why was this triggering PTK rekeying for the
903                          * STA that requested Group Key rekeying?? */
904                         /* wpa_request_new_ptk(sta->wpa_sm); */
905                         eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
906                         wpa_rekey_gtk(wpa_auth, NULL);
907                 }
908         } else {
909                 /* Do not allow the same key replay counter to be reused. This
910                  * does also invalidate all other pending replay counters if
911                  * retransmissions were used, i.e., we will only process one of
912                  * the pending replies and ignore rest if more than one is
913                  * received. */
914                 sm->key_replay[0].valid = FALSE;
915         }
916
917 #ifdef CONFIG_PEERKEY
918         if (msg == SMK_M3) {
919                 wpa_smk_m3(wpa_auth, sm, key);
920                 return;
921         }
922 #endif /* CONFIG_PEERKEY */
923
924         os_free(sm->last_rx_eapol_key);
925         sm->last_rx_eapol_key = os_malloc(data_len);
926         if (sm->last_rx_eapol_key == NULL)
927                 return;
928         os_memcpy(sm->last_rx_eapol_key, data, data_len);
929         sm->last_rx_eapol_key_len = data_len;
930
931         sm->EAPOLKeyReceived = TRUE;
932         sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
933         sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
934         os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
935         wpa_sm_step(sm);
936 }
937
938
939 static void wpa_gmk_to_gtk(const u8 *gmk, const u8 *addr, const u8 *gnonce,
940                            u8 *gtk, size_t gtk_len)
941 {
942         u8 data[ETH_ALEN + WPA_NONCE_LEN];
943
944         /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
945         os_memcpy(data, addr, ETH_ALEN);
946         os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
947
948 #ifdef CONFIG_IEEE80211W
949         sha256_prf(gmk, WPA_GMK_LEN, "Group key expansion",
950                    data, sizeof(data), gtk, gtk_len);
951 #else /* CONFIG_IEEE80211W */
952         sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
953                  data, sizeof(data), gtk, gtk_len);
954 #endif /* CONFIG_IEEE80211W */
955
956         wpa_hexdump_key(MSG_DEBUG, "GMK", gmk, WPA_GMK_LEN);
957         wpa_hexdump_key(MSG_DEBUG, "GTK", gtk, gtk_len);
958 }
959
960
961 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
962 {
963         struct wpa_authenticator *wpa_auth = eloop_ctx;
964         struct wpa_state_machine *sm = timeout_ctx;
965
966         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
967         sm->TimeoutEvt = TRUE;
968         wpa_sm_step(sm);
969 }
970
971
972 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
973                       struct wpa_state_machine *sm, int key_info,
974                       const u8 *key_rsc, const u8 *nonce,
975                       const u8 *kde, size_t kde_len,
976                       int keyidx, int encr, int force_version)
977 {
978         struct ieee802_1x_hdr *hdr;
979         struct wpa_eapol_key *key;
980         size_t len;
981         int alg;
982         int key_data_len, pad_len = 0;
983         u8 *buf, *pos;
984         int version, pairwise;
985         int i;
986
987         len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
988
989         if (force_version)
990                 version = force_version;
991         else if (wpa_use_aes_cmac(sm))
992                 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
993         else if (sm->pairwise == WPA_CIPHER_CCMP)
994                 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
995         else
996                 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
997
998         pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
999
1000         wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1001                    "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1002                    "encr=%d)",
1003                    version,
1004                    (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1005                    (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1006                    (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1007                    (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1008                    pairwise, (unsigned long) kde_len, keyidx, encr);
1009
1010         key_data_len = kde_len;
1011
1012         if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1013              version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1014                 pad_len = key_data_len % 8;
1015                 if (pad_len)
1016                         pad_len = 8 - pad_len;
1017                 key_data_len += pad_len + 8;
1018         }
1019
1020         len += key_data_len;
1021
1022         hdr = os_zalloc(len);
1023         if (hdr == NULL)
1024                 return;
1025         hdr->version = wpa_auth->conf.eapol_version;
1026         hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1027         hdr->length = host_to_be16(len  - sizeof(*hdr));
1028         key = (struct wpa_eapol_key *) (hdr + 1);
1029
1030         key->type = sm->wpa == WPA_VERSION_WPA2 ?
1031                 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1032         key_info |= version;
1033         if (encr && sm->wpa == WPA_VERSION_WPA2)
1034                 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1035         if (sm->wpa != WPA_VERSION_WPA2)
1036                 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1037         WPA_PUT_BE16(key->key_info, key_info);
1038
1039         alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
1040         switch (alg) {
1041         case WPA_CIPHER_CCMP:
1042                 WPA_PUT_BE16(key->key_length, 16);
1043                 break;
1044         case WPA_CIPHER_TKIP:
1045                 WPA_PUT_BE16(key->key_length, 32);
1046                 break;
1047         case WPA_CIPHER_WEP40:
1048                 WPA_PUT_BE16(key->key_length, 5);
1049                 break;
1050         case WPA_CIPHER_WEP104:
1051                 WPA_PUT_BE16(key->key_length, 13);
1052                 break;
1053         }
1054         if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1055                 WPA_PUT_BE16(key->key_length, 0);
1056
1057         /* FIX: STSL: what to use as key_replay_counter? */
1058         for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1059                 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1060                 os_memcpy(sm->key_replay[i].counter,
1061                           sm->key_replay[i - 1].counter,
1062                           WPA_REPLAY_COUNTER_LEN);
1063         }
1064         inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1065         os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1066                   WPA_REPLAY_COUNTER_LEN);
1067         sm->key_replay[0].valid = TRUE;
1068
1069         if (nonce)
1070                 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1071
1072         if (key_rsc)
1073                 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1074
1075         if (kde && !encr) {
1076                 os_memcpy(key + 1, kde, kde_len);
1077                 WPA_PUT_BE16(key->key_data_length, kde_len);
1078         } else if (encr && kde) {
1079                 buf = os_zalloc(key_data_len);
1080                 if (buf == NULL) {
1081                         os_free(hdr);
1082                         return;
1083                 }
1084                 pos = buf;
1085                 os_memcpy(pos, kde, kde_len);
1086                 pos += kde_len;
1087
1088                 if (pad_len)
1089                         *pos++ = 0xdd;
1090
1091                 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1092                                 buf, key_data_len);
1093                 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1094                     version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1095                         if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1096                                      (u8 *) (key + 1))) {
1097                                 os_free(hdr);
1098                                 os_free(buf);
1099                                 return;
1100                         }
1101                         WPA_PUT_BE16(key->key_data_length, key_data_len);
1102                 } else {
1103                         u8 ek[32];
1104                         os_memcpy(key->key_iv,
1105                                   sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1106                         inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1107                         os_memcpy(ek, key->key_iv, 16);
1108                         os_memcpy(ek + 16, sm->PTK.kek, 16);
1109                         os_memcpy(key + 1, buf, key_data_len);
1110                         rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1111                         WPA_PUT_BE16(key->key_data_length, key_data_len);
1112                 }
1113                 os_free(buf);
1114         }
1115
1116         if (key_info & WPA_KEY_INFO_MIC) {
1117                 if (!sm->PTK_valid) {
1118                         wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1119                                         "PTK not valid when sending EAPOL-Key "
1120                                         "frame");
1121                         os_free(hdr);
1122                         return;
1123                 }
1124                 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1125                                   key->key_mic);
1126         }
1127
1128         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1129                            1);
1130         wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1131                             sm->pairwise_set);
1132         os_free(hdr);
1133 }
1134
1135
1136 static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1137                            struct wpa_state_machine *sm, int key_info,
1138                            const u8 *key_rsc, const u8 *nonce,
1139                            const u8 *kde, size_t kde_len,
1140                            int keyidx, int encr)
1141 {
1142         int timeout_ms;
1143         int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1144         int ctr;
1145
1146         if (sm == NULL)
1147                 return;
1148
1149         __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1150                          keyidx, encr, 0);
1151
1152         ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1153         if (ctr == 1)
1154                 timeout_ms = eapol_key_timeout_first;
1155         else
1156                 timeout_ms = eapol_key_timeout_subseq;
1157         eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1158                                wpa_send_eapol_timeout, wpa_auth, sm);
1159 }
1160
1161
1162 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1163 {
1164         struct ieee802_1x_hdr *hdr;
1165         struct wpa_eapol_key *key;
1166         u16 key_info;
1167         int ret = 0;
1168         u8 mic[16];
1169
1170         if (data_len < sizeof(*hdr) + sizeof(*key))
1171                 return -1;
1172
1173         hdr = (struct ieee802_1x_hdr *) data;
1174         key = (struct wpa_eapol_key *) (hdr + 1);
1175         key_info = WPA_GET_BE16(key->key_info);
1176         os_memcpy(mic, key->key_mic, 16);
1177         os_memset(key->key_mic, 0, 16);
1178         if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1179                               data, data_len, key->key_mic) ||
1180             os_memcmp(mic, key->key_mic, 16) != 0)
1181                 ret = -1;
1182         os_memcpy(key->key_mic, mic, 16);
1183         return ret;
1184 }
1185
1186
1187 void wpa_remove_ptk(struct wpa_state_machine *sm)
1188 {
1189         sm->PTK_valid = FALSE;
1190         os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1191         wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, (u8 *) "",
1192                          0);
1193         sm->pairwise_set = FALSE;
1194         eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1195 }
1196
1197
1198 int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1199 {
1200         int remove_ptk = 1;
1201
1202         if (sm == NULL)
1203                 return -1;
1204
1205         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1206                          "event %d notification", event);
1207
1208         switch (event) {
1209         case WPA_AUTH:
1210         case WPA_ASSOC:
1211                 break;
1212         case WPA_DEAUTH:
1213         case WPA_DISASSOC:
1214                 sm->DeauthenticationRequest = TRUE;
1215                 break;
1216         case WPA_REAUTH:
1217         case WPA_REAUTH_EAPOL:
1218                 if (sm->GUpdateStationKeys) {
1219                         /*
1220                          * Reauthentication cancels the pending group key
1221                          * update for this STA.
1222                          */
1223                         sm->group->GKeyDoneStations--;
1224                         sm->GUpdateStationKeys = FALSE;
1225                         sm->PtkGroupInit = TRUE;
1226                 }
1227                 sm->ReAuthenticationRequest = TRUE;
1228                 break;
1229         case WPA_ASSOC_FT:
1230 #ifdef CONFIG_IEEE80211R
1231                 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1232                            "after association");
1233                 wpa_ft_install_ptk(sm);
1234
1235                 /* Using FT protocol, not WPA auth state machine */
1236                 sm->ft_completed = 1;
1237                 return 0;
1238 #else /* CONFIG_IEEE80211R */
1239                 break;
1240 #endif /* CONFIG_IEEE80211R */
1241         }
1242
1243 #ifdef CONFIG_IEEE80211R
1244         sm->ft_completed = 0;
1245 #endif /* CONFIG_IEEE80211R */
1246
1247 #ifdef CONFIG_IEEE80211W
1248         if (sm->mgmt_frame_prot && event == WPA_AUTH)
1249                 remove_ptk = 0;
1250 #endif /* CONFIG_IEEE80211W */
1251
1252         if (remove_ptk) {
1253                 sm->PTK_valid = FALSE;
1254                 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1255
1256                 if (event != WPA_REAUTH_EAPOL)
1257                         wpa_remove_ptk(sm);
1258         }
1259
1260         return wpa_sm_step(sm);
1261 }
1262
1263
1264 static enum wpa_alg wpa_alg_enum(int alg)
1265 {
1266         switch (alg) {
1267         case WPA_CIPHER_CCMP:
1268                 return WPA_ALG_CCMP;
1269         case WPA_CIPHER_TKIP:
1270                 return WPA_ALG_TKIP;
1271         case WPA_CIPHER_WEP104:
1272         case WPA_CIPHER_WEP40:
1273                 return WPA_ALG_WEP;
1274         default:
1275                 return WPA_ALG_NONE;
1276         }
1277 }
1278
1279
1280 SM_STATE(WPA_PTK, INITIALIZE)
1281 {
1282         SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1283         if (sm->Init) {
1284                 /* Init flag is not cleared here, so avoid busy
1285                  * loop by claiming nothing changed. */
1286                 sm->changed = FALSE;
1287         }
1288
1289         sm->keycount = 0;
1290         if (sm->GUpdateStationKeys)
1291                 sm->group->GKeyDoneStations--;
1292         sm->GUpdateStationKeys = FALSE;
1293         if (sm->wpa == WPA_VERSION_WPA)
1294                 sm->PInitAKeys = FALSE;
1295         if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1296                * Local AA > Remote AA)) */) {
1297                 sm->Pair = TRUE;
1298         }
1299         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1300         wpa_remove_ptk(sm);
1301         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1302         sm->TimeoutCtr = 0;
1303         if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1304                 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1305                                    WPA_EAPOL_authorized, 0);
1306         }
1307 }
1308
1309
1310 SM_STATE(WPA_PTK, DISCONNECT)
1311 {
1312         SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1313         sm->Disconnect = FALSE;
1314         wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1315 }
1316
1317
1318 SM_STATE(WPA_PTK, DISCONNECTED)
1319 {
1320         SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1321         sm->DeauthenticationRequest = FALSE;
1322 }
1323
1324
1325 SM_STATE(WPA_PTK, AUTHENTICATION)
1326 {
1327         SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1328         os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1329         sm->PTK_valid = FALSE;
1330         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1331                            1);
1332         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1333         sm->AuthenticationRequest = FALSE;
1334 }
1335
1336
1337 SM_STATE(WPA_PTK, AUTHENTICATION2)
1338 {
1339         SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1340         os_memcpy(sm->ANonce, sm->group->Counter, WPA_NONCE_LEN);
1341         inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1342         sm->ReAuthenticationRequest = FALSE;
1343         /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1344          * logical place than INITIALIZE since AUTHENTICATION2 can be
1345          * re-entered on ReAuthenticationRequest without going through
1346          * INITIALIZE. */
1347         sm->TimeoutCtr = 0;
1348 }
1349
1350
1351 SM_STATE(WPA_PTK, INITPMK)
1352 {
1353         u8 msk[2 * PMK_LEN];
1354         size_t len = 2 * PMK_LEN;
1355
1356         SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1357 #ifdef CONFIG_IEEE80211R
1358         sm->xxkey_len = 0;
1359 #endif /* CONFIG_IEEE80211R */
1360         if (sm->pmksa) {
1361                 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1362                 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1363         } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1364                 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1365                            "(len=%lu)", (unsigned long) len);
1366                 os_memcpy(sm->PMK, msk, PMK_LEN);
1367 #ifdef CONFIG_IEEE80211R
1368                 if (len >= 2 * PMK_LEN) {
1369                         os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1370                         sm->xxkey_len = PMK_LEN;
1371                 }
1372 #endif /* CONFIG_IEEE80211R */
1373         } else {
1374                 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1375         }
1376
1377         sm->req_replay_counter_used = 0;
1378         /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1379          * will break reauthentication since EAPOL state machines may not be
1380          * get into AUTHENTICATING state that clears keyRun before WPA state
1381          * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1382          * state and takes PMK from the previously used AAA Key. This will
1383          * eventually fail in 4-Way Handshake because Supplicant uses PMK
1384          * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1385          * be good workaround for this issue. */
1386         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1387 }
1388
1389
1390 SM_STATE(WPA_PTK, INITPSK)
1391 {
1392         const u8 *psk;
1393         SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
1394         psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL);
1395         if (psk) {
1396                 os_memcpy(sm->PMK, psk, PMK_LEN);
1397 #ifdef CONFIG_IEEE80211R
1398                 os_memcpy(sm->xxkey, psk, PMK_LEN);
1399                 sm->xxkey_len = PMK_LEN;
1400 #endif /* CONFIG_IEEE80211R */
1401         }
1402         sm->req_replay_counter_used = 0;
1403 }
1404
1405
1406 SM_STATE(WPA_PTK, PTKSTART)
1407 {
1408         u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1409         size_t pmkid_len = 0;
1410
1411         SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1412         sm->PTKRequest = FALSE;
1413         sm->TimeoutEvt = FALSE;
1414
1415         sm->TimeoutCtr++;
1416         if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1417                 /* No point in sending the EAPOL-Key - we will disconnect
1418                  * immediately following this. */
1419                 return;
1420         }
1421
1422         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1423                         "sending 1/4 msg of 4-Way Handshake");
1424         /*
1425          * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1426          * one possible PSK for this STA.
1427          */
1428         if (sm->wpa == WPA_VERSION_WPA2 &&
1429             wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt)) {
1430                 pmkid = buf;
1431                 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1432                 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1433                 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1434                 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1435                 if (sm->pmksa)
1436                         os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1437                                   sm->pmksa->pmkid, PMKID_LEN);
1438                 else {
1439                         /*
1440                          * Calculate PMKID since no PMKSA cache entry was
1441                          * available with pre-calculated PMKID.
1442                          */
1443                         rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1444                                   sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1445                                   wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1446                 }
1447         }
1448         wpa_send_eapol(sm->wpa_auth, sm,
1449                        WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1450                        sm->ANonce, pmkid, pmkid_len, 0, 0);
1451 }
1452
1453
1454 static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1455                           struct wpa_ptk *ptk)
1456 {
1457         size_t ptk_len = sm->pairwise == WPA_CIPHER_CCMP ? 48 : 64;
1458 #ifdef CONFIG_IEEE80211R
1459         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1460                 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1461 #endif /* CONFIG_IEEE80211R */
1462
1463         wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1464                        sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1465                        (u8 *) ptk, ptk_len,
1466                        wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1467
1468         return 0;
1469 }
1470
1471
1472 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1473 {
1474         struct wpa_ptk PTK;
1475         int ok = 0;
1476         const u8 *pmk = NULL;
1477
1478         SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1479         sm->EAPOLKeyReceived = FALSE;
1480
1481         /* WPA with IEEE 802.1X: use the derived PMK from EAP
1482          * WPA-PSK: iterate through possible PSKs and select the one matching
1483          * the packet */
1484         for (;;) {
1485                 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1486                         pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, pmk);
1487                         if (pmk == NULL)
1488                                 break;
1489                 } else
1490                         pmk = sm->PMK;
1491
1492                 wpa_derive_ptk(sm, pmk, &PTK);
1493
1494                 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1495                                        sm->last_rx_eapol_key_len) == 0) {
1496                         ok = 1;
1497                         break;
1498                 }
1499
1500                 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1501                         break;
1502         }
1503
1504         if (!ok) {
1505                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1506                                 "invalid MIC in msg 2/4 of 4-Way Handshake");
1507                 return;
1508         }
1509
1510 #ifdef CONFIG_IEEE80211R
1511         if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1512                 /*
1513                  * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1514                  * with the value we derived.
1515                  */
1516                 if (os_memcmp(sm->sup_pmk_r1_name, sm->pmk_r1_name,
1517                               WPA_PMK_NAME_LEN) != 0) {
1518                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1519                                         "PMKR1Name mismatch in FT 4-way "
1520                                         "handshake");
1521                         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
1522                                     "Supplicant",
1523                                     sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
1524                         wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1525                                     sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1526                         return;
1527                 }
1528         }
1529 #endif /* CONFIG_IEEE80211R */
1530
1531         eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1532
1533         if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1534                 /* PSK may have changed from the previous choice, so update
1535                  * state machine data based on whatever PSK was selected here.
1536                  */
1537                 os_memcpy(sm->PMK, pmk, PMK_LEN);
1538         }
1539
1540         sm->MICVerified = TRUE;
1541
1542         os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1543         sm->PTK_valid = TRUE;
1544 }
1545
1546
1547 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1548 {
1549         SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1550         sm->TimeoutCtr = 0;
1551 }
1552
1553
1554 #ifdef CONFIG_IEEE80211W
1555
1556 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1557 {
1558         if (sm->mgmt_frame_prot) {
1559                 return 2 + RSN_SELECTOR_LEN + sizeof(struct wpa_igtk_kde);
1560         }
1561
1562         return 0;
1563 }
1564
1565
1566 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1567 {
1568         struct wpa_igtk_kde igtk;
1569         struct wpa_group *gsm = sm->group;
1570
1571         if (!sm->mgmt_frame_prot)
1572                 return pos;
1573
1574         igtk.keyid[0] = gsm->GN_igtk;
1575         igtk.keyid[1] = 0;
1576         if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
1577             wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, igtk.pn) < 0)
1578                 os_memset(igtk.pn, 0, sizeof(igtk.pn));
1579         os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], WPA_IGTK_LEN);
1580         pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
1581                           (const u8 *) &igtk, sizeof(igtk), NULL, 0);
1582
1583         return pos;
1584 }
1585
1586 #else /* CONFIG_IEEE80211W */
1587
1588 static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1589 {
1590         return 0;
1591 }
1592
1593
1594 static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1595 {
1596         return pos;
1597 }
1598
1599 #endif /* CONFIG_IEEE80211W */
1600
1601
1602 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1603 {
1604         u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos;
1605         size_t gtk_len, kde_len;
1606         struct wpa_group *gsm = sm->group;
1607         u8 *wpa_ie;
1608         int wpa_ie_len, secure, keyidx, encr = 0;
1609
1610         SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1611         sm->TimeoutEvt = FALSE;
1612
1613         sm->TimeoutCtr++;
1614         if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1615                 /* No point in sending the EAPOL-Key - we will disconnect
1616                  * immediately following this. */
1617                 return;
1618         }
1619
1620         /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
1621          */
1622         os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1623         wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1624         wpa_ie = sm->wpa_auth->wpa_ie;
1625         wpa_ie_len = sm->wpa_auth->wpa_ie_len;
1626         if (sm->wpa == WPA_VERSION_WPA &&
1627             (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
1628             wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
1629                 /* WPA-only STA, remove RSN IE */
1630                 wpa_ie = wpa_ie + wpa_ie[1] + 2;
1631                 wpa_ie_len = wpa_ie[1] + 2;
1632         }
1633         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1634                         "sending 3/4 msg of 4-Way Handshake");
1635         if (sm->wpa == WPA_VERSION_WPA2) {
1636                 /* WPA2 send GTK in the 4-way handshake */
1637                 secure = 1;
1638                 gtk = gsm->GTK[gsm->GN - 1];
1639                 gtk_len = gsm->GTK_len;
1640                 keyidx = gsm->GN;
1641                 _rsc = rsc;
1642                 encr = 1;
1643         } else {
1644                 /* WPA does not include GTK in msg 3/4 */
1645                 secure = 0;
1646                 gtk = NULL;
1647                 gtk_len = 0;
1648                 keyidx = 0;
1649                 _rsc = NULL;
1650         }
1651
1652         kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
1653         if (gtk)
1654                 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
1655 #ifdef CONFIG_IEEE80211R
1656         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1657                 kde_len += 2 + PMKID_LEN;
1658 #endif /* CONFIG_IEEE80211R */
1659         kde = os_malloc(kde_len);
1660         if (kde == NULL)
1661                 return;
1662
1663         pos = kde;
1664         os_memcpy(pos, wpa_ie, wpa_ie_len);
1665         pos += wpa_ie_len;
1666 #ifdef CONFIG_IEEE80211R
1667         if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1668                 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
1669                 if (res < 0) {
1670                         wpa_printf(MSG_ERROR, "FT: Failed to insert "
1671                                    "PMKR1Name into RSN IE in EAPOL-Key data");
1672                         os_free(kde);
1673                         return;
1674                 }
1675                 pos += res;
1676         }
1677 #endif /* CONFIG_IEEE80211R */
1678         if (gtk) {
1679                 u8 hdr[2];
1680                 hdr[0] = keyidx & 0x03;
1681                 hdr[1] = 0;
1682                 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1683                                   gtk, gtk_len);
1684         }
1685         pos = ieee80211w_kde_add(sm, pos);
1686
1687         wpa_send_eapol(sm->wpa_auth, sm,
1688                        (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
1689                        WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
1690                        WPA_KEY_INFO_KEY_TYPE,
1691                        _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
1692         os_free(kde);
1693 }
1694
1695
1696 SM_STATE(WPA_PTK, PTKINITDONE)
1697 {
1698         SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
1699         sm->EAPOLKeyReceived = FALSE;
1700         if (sm->Pair) {
1701                 enum wpa_alg alg;
1702                 int klen;
1703                 if (sm->pairwise == WPA_CIPHER_TKIP) {
1704                         alg = WPA_ALG_TKIP;
1705                         klen = 32;
1706                 } else {
1707                         alg = WPA_ALG_CCMP;
1708                         klen = 16;
1709                 }
1710                 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
1711                                      sm->PTK.tk1, klen)) {
1712                         wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1713                         return;
1714                 }
1715                 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
1716                 sm->pairwise_set = TRUE;
1717
1718                 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
1719                         eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1720                         eloop_register_timeout(sm->wpa_auth->conf.
1721                                                wpa_ptk_rekey, 0, wpa_rekey_ptk,
1722                                                sm->wpa_auth, sm);
1723                 }
1724
1725                 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1726                         wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1727                                            WPA_EAPOL_authorized, 1);
1728                 }
1729         }
1730
1731         if (0 /* IBSS == TRUE */) {
1732                 sm->keycount++;
1733                 if (sm->keycount == 2) {
1734                         wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1735                                            WPA_EAPOL_portValid, 1);
1736                 }
1737         } else {
1738                 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
1739                                    1);
1740         }
1741         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
1742         wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
1743         if (sm->wpa == WPA_VERSION_WPA)
1744                 sm->PInitAKeys = TRUE;
1745         else
1746                 sm->has_GTK = TRUE;
1747         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1748                          "pairwise key handshake completed (%s)",
1749                          sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1750
1751 #ifdef CONFIG_IEEE80211R
1752         wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
1753 #endif /* CONFIG_IEEE80211R */
1754 }
1755
1756
1757 SM_STEP(WPA_PTK)
1758 {
1759         struct wpa_authenticator *wpa_auth = sm->wpa_auth;
1760
1761         if (sm->Init)
1762                 SM_ENTER(WPA_PTK, INITIALIZE);
1763         else if (sm->Disconnect
1764                  /* || FIX: dot11RSNAConfigSALifetime timeout */)
1765                 SM_ENTER(WPA_PTK, DISCONNECT);
1766         else if (sm->DeauthenticationRequest)
1767                 SM_ENTER(WPA_PTK, DISCONNECTED);
1768         else if (sm->AuthenticationRequest)
1769                 SM_ENTER(WPA_PTK, AUTHENTICATION);
1770         else if (sm->ReAuthenticationRequest)
1771                 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1772         else if (sm->PTKRequest)
1773                 SM_ENTER(WPA_PTK, PTKSTART);
1774         else switch (sm->wpa_ptk_state) {
1775         case WPA_PTK_INITIALIZE:
1776                 break;
1777         case WPA_PTK_DISCONNECT:
1778                 SM_ENTER(WPA_PTK, DISCONNECTED);
1779                 break;
1780         case WPA_PTK_DISCONNECTED:
1781                 SM_ENTER(WPA_PTK, INITIALIZE);
1782                 break;
1783         case WPA_PTK_AUTHENTICATION:
1784                 SM_ENTER(WPA_PTK, AUTHENTICATION2);
1785                 break;
1786         case WPA_PTK_AUTHENTICATION2:
1787                 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1788                     wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1789                                        WPA_EAPOL_keyRun) > 0)
1790                         SM_ENTER(WPA_PTK, INITPMK);
1791                 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
1792                          /* FIX: && 802.1X::keyRun */)
1793                         SM_ENTER(WPA_PTK, INITPSK);
1794                 break;
1795         case WPA_PTK_INITPMK:
1796                 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
1797                                        WPA_EAPOL_keyAvailable) > 0)
1798                         SM_ENTER(WPA_PTK, PTKSTART);
1799                 else {
1800                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
1801                         SM_ENTER(WPA_PTK, DISCONNECT);
1802                 }
1803                 break;
1804         case WPA_PTK_INITPSK:
1805                 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, NULL))
1806                         SM_ENTER(WPA_PTK, PTKSTART);
1807                 else {
1808                         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1809                                         "no PSK configured for the STA");
1810                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
1811                         SM_ENTER(WPA_PTK, DISCONNECT);
1812                 }
1813                 break;
1814         case WPA_PTK_PTKSTART:
1815                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1816                     sm->EAPOLKeyPairwise)
1817                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1818                 else if (sm->TimeoutCtr >
1819                          (int) dot11RSNAConfigPairwiseUpdateCount) {
1820                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
1821                         SM_ENTER(WPA_PTK, DISCONNECT);
1822                 } else if (sm->TimeoutEvt)
1823                         SM_ENTER(WPA_PTK, PTKSTART);
1824                 break;
1825         case WPA_PTK_PTKCALCNEGOTIATING:
1826                 if (sm->MICVerified)
1827                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
1828                 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1829                          sm->EAPOLKeyPairwise)
1830                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
1831                 else if (sm->TimeoutEvt)
1832                         SM_ENTER(WPA_PTK, PTKSTART);
1833                 break;
1834         case WPA_PTK_PTKCALCNEGOTIATING2:
1835                 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1836                 break;
1837         case WPA_PTK_PTKINITNEGOTIATING:
1838                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1839                     sm->EAPOLKeyPairwise && sm->MICVerified)
1840                         SM_ENTER(WPA_PTK, PTKINITDONE);
1841                 else if (sm->TimeoutCtr >
1842                          (int) dot11RSNAConfigPairwiseUpdateCount) {
1843                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
1844                         SM_ENTER(WPA_PTK, DISCONNECT);
1845                 } else if (sm->TimeoutEvt)
1846                         SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
1847                 break;
1848         case WPA_PTK_PTKINITDONE:
1849                 break;
1850         }
1851 }
1852
1853
1854 SM_STATE(WPA_PTK_GROUP, IDLE)
1855 {
1856         SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
1857         if (sm->Init) {
1858                 /* Init flag is not cleared here, so avoid busy
1859                  * loop by claiming nothing changed. */
1860                 sm->changed = FALSE;
1861         }
1862         sm->GTimeoutCtr = 0;
1863 }
1864
1865
1866 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
1867 {
1868         u8 rsc[WPA_KEY_RSC_LEN];
1869         struct wpa_group *gsm = sm->group;
1870         u8 *kde, *pos, hdr[2];
1871         size_t kde_len;
1872
1873         SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
1874
1875         sm->GTimeoutCtr++;
1876         if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
1877                 /* No point in sending the EAPOL-Key - we will disconnect
1878                  * immediately following this. */
1879                 return;
1880         }
1881
1882         if (sm->wpa == WPA_VERSION_WPA)
1883                 sm->PInitAKeys = FALSE;
1884         sm->TimeoutEvt = FALSE;
1885         /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
1886         os_memset(rsc, 0, WPA_KEY_RSC_LEN);
1887         if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
1888                 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
1889         wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1890                         "sending 1/2 msg of Group Key Handshake");
1891
1892         if (sm->wpa == WPA_VERSION_WPA2) {
1893                 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
1894                         ieee80211w_kde_len(sm);
1895                 kde = os_malloc(kde_len);
1896                 if (kde == NULL)
1897                         return;
1898
1899                 pos = kde;
1900                 hdr[0] = gsm->GN & 0x03;
1901                 hdr[1] = 0;
1902                 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
1903                                   gsm->GTK[gsm->GN - 1], gsm->GTK_len);
1904                 pos = ieee80211w_kde_add(sm, pos);
1905         } else {
1906                 kde = gsm->GTK[gsm->GN - 1];
1907                 pos = kde + gsm->GTK_len;
1908         }
1909
1910         wpa_send_eapol(sm->wpa_auth, sm,
1911                        WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
1912                        WPA_KEY_INFO_ACK |
1913                        (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
1914                        rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
1915         if (sm->wpa == WPA_VERSION_WPA2)
1916                 os_free(kde);
1917 }
1918
1919
1920 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
1921 {
1922         SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
1923         sm->EAPOLKeyReceived = FALSE;
1924         if (sm->GUpdateStationKeys)
1925                 sm->group->GKeyDoneStations--;
1926         sm->GUpdateStationKeys = FALSE;
1927         sm->GTimeoutCtr = 0;
1928         /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
1929         wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
1930                          "group key handshake completed (%s)",
1931                          sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
1932         sm->has_GTK = TRUE;
1933 }
1934
1935
1936 SM_STATE(WPA_PTK_GROUP, KEYERROR)
1937 {
1938         SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
1939         if (sm->GUpdateStationKeys)
1940                 sm->group->GKeyDoneStations--;
1941         sm->GUpdateStationKeys = FALSE;
1942         sm->Disconnect = TRUE;
1943 }
1944
1945
1946 SM_STEP(WPA_PTK_GROUP)
1947 {
1948         if (sm->Init || sm->PtkGroupInit) {
1949                 SM_ENTER(WPA_PTK_GROUP, IDLE);
1950                 sm->PtkGroupInit = FALSE;
1951         } else switch (sm->wpa_ptk_group_state) {
1952         case WPA_PTK_GROUP_IDLE:
1953                 if (sm->GUpdateStationKeys ||
1954                     (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
1955                         SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1956                 break;
1957         case WPA_PTK_GROUP_REKEYNEGOTIATING:
1958                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
1959                     !sm->EAPOLKeyPairwise && sm->MICVerified)
1960                         SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
1961                 else if (sm->GTimeoutCtr >
1962                          (int) dot11RSNAConfigGroupUpdateCount)
1963                         SM_ENTER(WPA_PTK_GROUP, KEYERROR);
1964                 else if (sm->TimeoutEvt)
1965                         SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
1966                 break;
1967         case WPA_PTK_GROUP_KEYERROR:
1968                 SM_ENTER(WPA_PTK_GROUP, IDLE);
1969                 break;
1970         case WPA_PTK_GROUP_REKEYESTABLISHED:
1971                 SM_ENTER(WPA_PTK_GROUP, IDLE);
1972                 break;
1973         }
1974 }
1975
1976
1977 static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
1978                           struct wpa_group *group)
1979 {
1980         int ret = 0;
1981
1982         /* FIX: is this the correct way of getting GNonce? */
1983         os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
1984         inc_byte_array(group->Counter, WPA_NONCE_LEN);
1985         wpa_gmk_to_gtk(group->GMK, wpa_auth->addr, group->GNonce,
1986                        group->GTK[group->GN - 1], group->GTK_len);
1987
1988 #ifdef CONFIG_IEEE80211W
1989         if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
1990                 if (os_get_random(group->IGTK[group->GN_igtk - 4],
1991                                   WPA_IGTK_LEN) < 0) {
1992                         wpa_printf(MSG_INFO, "RSN: Failed to get new random "
1993                                    "IGTK");
1994                         ret = -1;
1995                 }
1996                 wpa_hexdump_key(MSG_DEBUG, "IGTK",
1997                                 group->IGTK[group->GN_igtk - 4], WPA_IGTK_LEN);
1998         }
1999 #endif /* CONFIG_IEEE80211W */
2000
2001         return ret;
2002 }
2003
2004
2005 static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2006                                struct wpa_group *group)
2007 {
2008         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2009                    "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2010         group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2011         group->wpa_group_state = WPA_GROUP_GTK_INIT;
2012
2013         /* GTK[0..N] = 0 */
2014         os_memset(group->GTK, 0, sizeof(group->GTK));
2015         group->GN = 1;
2016         group->GM = 2;
2017 #ifdef CONFIG_IEEE80211W
2018         group->GN_igtk = 4;
2019         group->GM_igtk = 5;
2020 #endif /* CONFIG_IEEE80211W */
2021         /* GTK[GN] = CalcGTK() */
2022         wpa_gtk_update(wpa_auth, group);
2023 }
2024
2025
2026 static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2027 {
2028         if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2029                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2030                                 "Not in PTKINITDONE; skip Group Key update");
2031                 return 0;
2032         }
2033         if (sm->GUpdateStationKeys) {
2034                 /*
2035                  * This should not really happen, but just in case, make sure
2036                  * we do not count the same STA twice in GKeyDoneStations.
2037                  */
2038                 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2039                                 "GUpdateStationKeys already set - do not "
2040                                 "increment GKeyDoneStations");
2041         } else {
2042                 sm->group->GKeyDoneStations++;
2043                 sm->GUpdateStationKeys = TRUE;
2044         }
2045         wpa_sm_step(sm);
2046         return 0;
2047 }
2048
2049
2050 static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2051                               struct wpa_group *group)
2052 {
2053         int tmp;
2054
2055         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2056                    "SETKEYS (VLAN-ID %d)", group->vlan_id);
2057         group->changed = TRUE;
2058         group->wpa_group_state = WPA_GROUP_SETKEYS;
2059         group->GTKReKey = FALSE;
2060         tmp = group->GM;
2061         group->GM = group->GN;
2062         group->GN = tmp;
2063 #ifdef CONFIG_IEEE80211W
2064         tmp = group->GM_igtk;
2065         group->GM_igtk = group->GN_igtk;
2066         group->GN_igtk = tmp;
2067 #endif /* CONFIG_IEEE80211W */
2068         /* "GKeyDoneStations = GNoStations" is done in more robust way by
2069          * counting the STAs that are marked with GUpdateStationKeys instead of
2070          * including all STAs that could be in not-yet-completed state. */
2071         wpa_gtk_update(wpa_auth, group);
2072
2073         wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, NULL);
2074         wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2075                    group->GKeyDoneStations);
2076 }
2077
2078
2079 static void wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2080                                   struct wpa_group *group)
2081 {
2082         wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2083                    "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2084         group->changed = TRUE;
2085         group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2086         wpa_auth_set_key(wpa_auth, group->vlan_id,
2087                          wpa_alg_enum(wpa_auth->conf.wpa_group),
2088                          NULL, group->GN, group->GTK[group->GN - 1],
2089                          group->GTK_len);
2090
2091 #ifdef CONFIG_IEEE80211W
2092         if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2093                 wpa_auth_set_key(wpa_auth, group->vlan_id, WPA_ALG_IGTK,
2094                                  NULL, group->GN_igtk,
2095                                  group->IGTK[group->GN_igtk - 4],
2096                                  WPA_IGTK_LEN);
2097         }
2098 #endif /* CONFIG_IEEE80211W */
2099 }
2100
2101
2102 static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2103                               struct wpa_group *group)
2104 {
2105         if (group->GInit) {
2106                 wpa_group_gtk_init(wpa_auth, group);
2107         } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2108                    group->GTKAuthenticator) {
2109                 wpa_group_setkeysdone(wpa_auth, group);
2110         } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2111                    group->GTKReKey) {
2112                 wpa_group_setkeys(wpa_auth, group);
2113         } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2114                 if (group->GKeyDoneStations == 0)
2115                         wpa_group_setkeysdone(wpa_auth, group);
2116                 else if (group->GTKReKey)
2117                         wpa_group_setkeys(wpa_auth, group);
2118         }
2119 }
2120
2121
2122 static int wpa_sm_step(struct wpa_state_machine *sm)
2123 {
2124         if (sm == NULL)
2125                 return 0;
2126
2127         if (sm->in_step_loop) {
2128                 /* This should not happen, but if it does, make sure we do not
2129                  * end up freeing the state machine too early by exiting the
2130                  * recursive call. */
2131                 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2132                 return 0;
2133         }
2134
2135         sm->in_step_loop = 1;
2136         do {
2137                 if (sm->pending_deinit)
2138                         break;
2139
2140                 sm->changed = FALSE;
2141                 sm->wpa_auth->group->changed = FALSE;
2142
2143                 SM_STEP_RUN(WPA_PTK);
2144                 if (sm->pending_deinit)
2145                         break;
2146                 SM_STEP_RUN(WPA_PTK_GROUP);
2147                 if (sm->pending_deinit)
2148                         break;
2149                 wpa_group_sm_step(sm->wpa_auth, sm->group);
2150         } while (sm->changed || sm->wpa_auth->group->changed);
2151         sm->in_step_loop = 0;
2152
2153         if (sm->pending_deinit) {
2154                 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2155                            "machine deinit for " MACSTR, MAC2STR(sm->addr));
2156                 wpa_free_sta_sm(sm);
2157                 return 1;
2158         }
2159         return 0;
2160 }
2161
2162
2163 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2164 {
2165         struct wpa_state_machine *sm = eloop_ctx;
2166         wpa_sm_step(sm);
2167 }
2168
2169
2170 void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2171 {
2172         if (sm == NULL)
2173                 return;
2174         eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2175 }
2176
2177
2178 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2179 {
2180         int tmp, i;
2181         struct wpa_group *group;
2182
2183         if (wpa_auth == NULL)
2184                 return;
2185
2186         group = wpa_auth->group;
2187
2188         for (i = 0; i < 2; i++) {
2189                 tmp = group->GM;
2190                 group->GM = group->GN;
2191                 group->GN = tmp;
2192 #ifdef CONFIG_IEEE80211W
2193                 tmp = group->GM_igtk;
2194                 group->GM_igtk = group->GN_igtk;
2195                 group->GN_igtk = tmp;
2196 #endif /* CONFIG_IEEE80211W */
2197                 wpa_gtk_update(wpa_auth, group);
2198         }
2199 }
2200
2201
2202 static const char * wpa_bool_txt(int bool)
2203 {
2204         return bool ? "TRUE" : "FALSE";
2205 }
2206
2207
2208 static int wpa_cipher_bits(int cipher)
2209 {
2210         switch (cipher) {
2211         case WPA_CIPHER_CCMP:
2212                 return 128;
2213         case WPA_CIPHER_TKIP:
2214                 return 256;
2215         case WPA_CIPHER_WEP104:
2216                 return 104;
2217         case WPA_CIPHER_WEP40:
2218                 return 40;
2219         default:
2220                 return 0;
2221         }
2222 }
2223
2224
2225 #define RSN_SUITE "%02x-%02x-%02x-%d"
2226 #define RSN_SUITE_ARG(s) \
2227 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2228
2229 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2230 {
2231         int len = 0, ret;
2232         char pmkid_txt[PMKID_LEN * 2 + 1];
2233
2234         if (wpa_auth == NULL)
2235                 return len;
2236
2237         ret = os_snprintf(buf + len, buflen - len,
2238                           "dot11RSNAOptionImplemented=TRUE\n"
2239 #ifdef CONFIG_RSN_PREAUTH
2240                           "dot11RSNAPreauthenticationImplemented=TRUE\n"
2241 #else /* CONFIG_RSN_PREAUTH */
2242                           "dot11RSNAPreauthenticationImplemented=FALSE\n"
2243 #endif /* CONFIG_RSN_PREAUTH */
2244                           "dot11RSNAEnabled=%s\n"
2245                           "dot11RSNAPreauthenticationEnabled=%s\n",
2246                           wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2247                           wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2248         if (ret < 0 || (size_t) ret >= buflen - len)
2249                 return len;
2250         len += ret;
2251
2252         wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2253                          wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2254
2255         ret = os_snprintf(
2256                 buf + len, buflen - len,
2257                 "dot11RSNAConfigVersion=%u\n"
2258                 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2259                 /* FIX: dot11RSNAConfigGroupCipher */
2260                 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2261                 /* FIX: dot11RSNAConfigGroupRekeyTime */
2262                 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2263                 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2264                 "dot11RSNAConfigGroupUpdateCount=%u\n"
2265                 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2266                 "dot11RSNAConfigGroupCipherSize=%u\n"
2267                 "dot11RSNAConfigPMKLifetime=%u\n"
2268                 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2269                 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2270                 "dot11RSNAConfigSATimeout=%u\n"
2271                 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2272                 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2273                 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2274                 "dot11RSNAPMKIDUsed=%s\n"
2275                 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2276                 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2277                 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2278                 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2279                 "dot11RSNA4WayHandshakeFailures=%u\n"
2280                 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2281                 RSN_VERSION,
2282                 !!wpa_auth->conf.wpa_strict_rekey,
2283                 dot11RSNAConfigGroupUpdateCount,
2284                 dot11RSNAConfigPairwiseUpdateCount,
2285                 wpa_cipher_bits(wpa_auth->conf.wpa_group),
2286                 dot11RSNAConfigPMKLifetime,
2287                 dot11RSNAConfigPMKReauthThreshold,
2288                 dot11RSNAConfigSATimeout,
2289                 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2290                 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2291                 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2292                 pmkid_txt,
2293                 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2294                 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2295                 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2296                 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2297                 wpa_auth->dot11RSNA4WayHandshakeFailures);
2298         if (ret < 0 || (size_t) ret >= buflen - len)
2299                 return len;
2300         len += ret;
2301
2302         /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2303         /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2304
2305         /* Private MIB */
2306         ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2307                           wpa_auth->group->wpa_group_state);
2308         if (ret < 0 || (size_t) ret >= buflen - len)
2309                 return len;
2310         len += ret;
2311
2312         return len;
2313 }
2314
2315
2316 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2317 {
2318         int len = 0, ret;
2319         u32 pairwise = 0;
2320
2321         if (sm == NULL)
2322                 return 0;
2323
2324         /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2325
2326         /* dot11RSNAStatsEntry */
2327
2328         if (sm->wpa == WPA_VERSION_WPA) {
2329                 if (sm->pairwise == WPA_CIPHER_CCMP)
2330                         pairwise = WPA_CIPHER_SUITE_CCMP;
2331                 else if (sm->pairwise == WPA_CIPHER_TKIP)
2332                         pairwise = WPA_CIPHER_SUITE_TKIP;
2333                 else if (sm->pairwise == WPA_CIPHER_WEP104)
2334                         pairwise = WPA_CIPHER_SUITE_WEP104;
2335                 else if (sm->pairwise == WPA_CIPHER_WEP40)
2336                         pairwise = WPA_CIPHER_SUITE_WEP40;
2337                 else if (sm->pairwise == WPA_CIPHER_NONE)
2338                         pairwise = WPA_CIPHER_SUITE_NONE;
2339         } else if (sm->wpa == WPA_VERSION_WPA2) {
2340                 if (sm->pairwise == WPA_CIPHER_CCMP)
2341                         pairwise = RSN_CIPHER_SUITE_CCMP;
2342                 else if (sm->pairwise == WPA_CIPHER_TKIP)
2343                         pairwise = RSN_CIPHER_SUITE_TKIP;
2344                 else if (sm->pairwise == WPA_CIPHER_WEP104)
2345                         pairwise = RSN_CIPHER_SUITE_WEP104;
2346                 else if (sm->pairwise == WPA_CIPHER_WEP40)
2347                         pairwise = RSN_CIPHER_SUITE_WEP40;
2348                 else if (sm->pairwise == WPA_CIPHER_NONE)
2349                         pairwise = RSN_CIPHER_SUITE_NONE;
2350         } else
2351                 return 0;
2352
2353         ret = os_snprintf(
2354                 buf + len, buflen - len,
2355                 /* TODO: dot11RSNAStatsIndex */
2356                 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2357                 "dot11RSNAStatsVersion=1\n"
2358                 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2359                 /* TODO: dot11RSNAStatsTKIPICVErrors */
2360                 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2361                 "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2362                 /* TODO: dot11RSNAStatsCCMPReplays */
2363                 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2364                 /* TODO: dot11RSNAStatsTKIPReplays */,
2365                 MAC2STR(sm->addr),
2366                 RSN_SUITE_ARG(pairwise),
2367                 sm->dot11RSNAStatsTKIPLocalMICFailures,
2368                 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2369         if (ret < 0 || (size_t) ret >= buflen - len)
2370                 return len;
2371         len += ret;
2372
2373         /* Private MIB */
2374         ret = os_snprintf(buf + len, buflen - len,
2375                           "hostapdWPAPTKState=%d\n"
2376                           "hostapdWPAPTKGroupState=%d\n",
2377                           sm->wpa_ptk_state,
2378                           sm->wpa_ptk_group_state);
2379         if (ret < 0 || (size_t) ret >= buflen - len)
2380                 return len;
2381         len += ret;
2382
2383         return len;
2384 }
2385
2386
2387 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2388 {
2389         if (wpa_auth)
2390                 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2391 }
2392
2393
2394 int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2395 {
2396         return sm && sm->pairwise_set;
2397 }
2398
2399
2400 int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2401 {
2402         return sm->pairwise;
2403 }
2404
2405
2406 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2407 {
2408         if (sm == NULL)
2409                 return -1;
2410         return sm->wpa_key_mgmt;
2411 }
2412
2413
2414 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
2415 {
2416         if (sm == NULL)
2417                 return 0;
2418         return sm->wpa;
2419 }
2420
2421
2422 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
2423                              struct rsn_pmksa_cache_entry *entry)
2424 {
2425         if (sm == NULL || sm->pmksa != entry)
2426                 return -1;
2427         sm->pmksa = NULL;
2428         return 0;
2429 }
2430
2431
2432 struct rsn_pmksa_cache_entry *
2433 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
2434 {
2435         return sm ? sm->pmksa : NULL;
2436 }
2437
2438
2439 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
2440 {
2441         if (sm)
2442                 sm->dot11RSNAStatsTKIPLocalMICFailures++;
2443 }
2444
2445
2446 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
2447 {
2448         if (wpa_auth == NULL)
2449                 return NULL;
2450         *len = wpa_auth->wpa_ie_len;
2451         return wpa_auth->wpa_ie;
2452 }
2453
2454
2455 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
2456                        int session_timeout, struct eapol_state_machine *eapol)
2457 {
2458         if (sm == NULL || sm->wpa != WPA_VERSION_WPA2)
2459                 return -1;
2460
2461         if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
2462                                  sm->wpa_auth->addr, sm->addr, session_timeout,
2463                                  eapol, sm->wpa_key_mgmt))
2464                 return 0;
2465
2466         return -1;
2467 }
2468
2469
2470 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
2471                                const u8 *pmk, size_t len, const u8 *sta_addr,
2472                                int session_timeout,
2473                                struct eapol_state_machine *eapol)
2474 {
2475         if (wpa_auth == NULL)
2476                 return -1;
2477
2478         if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
2479                                  sta_addr, session_timeout, eapol,
2480                                  WPA_KEY_MGMT_IEEE8021X))
2481                 return 0;
2482
2483         return -1;
2484 }
2485
2486
2487 static struct wpa_group *
2488 wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
2489 {
2490         struct wpa_group *group;
2491
2492         if (wpa_auth == NULL || wpa_auth->group == NULL)
2493                 return NULL;
2494
2495         wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
2496                    vlan_id);
2497         group = wpa_group_init(wpa_auth, vlan_id);
2498         if (group == NULL)
2499                 return NULL;
2500
2501         group->next = wpa_auth->group->next;
2502         wpa_auth->group->next = group;
2503
2504         return group;
2505 }
2506
2507
2508 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
2509 {
2510         struct wpa_group *group;
2511
2512         if (sm == NULL || sm->wpa_auth == NULL)
2513                 return 0;
2514
2515         group = sm->wpa_auth->group;
2516         while (group) {
2517                 if (group->vlan_id == vlan_id)
2518                         break;
2519                 group = group->next;
2520         }
2521
2522         if (group == NULL) {
2523                 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
2524                 if (group == NULL)
2525                         return -1;
2526         }
2527
2528         if (sm->group == group)
2529                 return 0;
2530
2531         wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
2532                    "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
2533
2534         sm->group = group;
2535         return 0;
2536 }