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