blob: 4d19bb0336a8d93426a4ab4e880c359731645c92 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002 * IEEE 802.11 RSN / WPA Authenticator
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003 * Copyright (c) 2004-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "utils/state_machine.h"
Dmitry Shmidtcf32e602014-01-28 10:57:39 -080014#include "utils/bitfield.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "common/ieee802_11_defs.h"
16#include "crypto/aes_wrap.h"
17#include "crypto/crypto.h"
18#include "crypto/sha1.h"
19#include "crypto/sha256.h"
20#include "crypto/random.h"
21#include "eapol_auth/eapol_auth_sm.h"
22#include "ap_config.h"
23#include "ieee802_11.h"
24#include "wpa_auth.h"
25#include "pmksa_cache_auth.h"
26#include "wpa_auth_i.h"
27#include "wpa_auth_ie.h"
28
29#define STATE_MACHINE_DATA struct wpa_state_machine
30#define STATE_MACHINE_DEBUG_PREFIX "WPA"
31#define STATE_MACHINE_ADDR sm->addr
32
33
34static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
35static int wpa_sm_step(struct wpa_state_machine *sm);
36static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
37static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
38static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
39 struct wpa_group *group);
40static void wpa_request_new_ptk(struct wpa_state_machine *sm);
41static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
42 struct wpa_group *group);
43static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
44 struct wpa_group *group);
45
46static const u32 dot11RSNAConfigGroupUpdateCount = 4;
47static const u32 dot11RSNAConfigPairwiseUpdateCount = 4;
48static const u32 eapol_key_timeout_first = 100; /* ms */
49static const u32 eapol_key_timeout_subseq = 1000; /* ms */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static const u32 eapol_key_timeout_first_group = 500; /* ms */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070051
52/* TODO: make these configurable */
53static const int dot11RSNAConfigPMKLifetime = 43200;
54static const int dot11RSNAConfigPMKReauthThreshold = 70;
55static const int dot11RSNAConfigSATimeout = 60;
56
57
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080058static inline int wpa_auth_mic_failure_report(
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070059 struct wpa_authenticator *wpa_auth, const u8 *addr)
60{
61 if (wpa_auth->cb.mic_failure_report)
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080062 return wpa_auth->cb.mic_failure_report(wpa_auth->cb.ctx, addr);
63 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070064}
65
66
67static 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
76static 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
85static inline const u8 * wpa_auth_get_psk(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070086 const u8 *addr,
87 const u8 *p2p_dev_addr,
88 const u8 *prev_psk)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089{
90 if (wpa_auth->cb.get_psk == NULL)
91 return NULL;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070092 return wpa_auth->cb.get_psk(wpa_auth->cb.ctx, addr, p2p_dev_addr,
93 prev_psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070094}
95
96
97static inline int wpa_auth_get_msk(struct wpa_authenticator *wpa_auth,
98 const u8 *addr, u8 *msk, size_t *len)
99{
100 if (wpa_auth->cb.get_msk == NULL)
101 return -1;
102 return wpa_auth->cb.get_msk(wpa_auth->cb.ctx, addr, msk, len);
103}
104
105
106static inline int wpa_auth_set_key(struct wpa_authenticator *wpa_auth,
107 int vlan_id,
108 enum wpa_alg alg, const u8 *addr, int idx,
109 u8 *key, size_t key_len)
110{
111 if (wpa_auth->cb.set_key == NULL)
112 return -1;
113 return wpa_auth->cb.set_key(wpa_auth->cb.ctx, vlan_id, alg, addr, idx,
114 key, key_len);
115}
116
117
118static inline int wpa_auth_get_seqnum(struct wpa_authenticator *wpa_auth,
119 const u8 *addr, int idx, u8 *seq)
120{
121 if (wpa_auth->cb.get_seqnum == NULL)
122 return -1;
123 return wpa_auth->cb.get_seqnum(wpa_auth->cb.ctx, addr, idx, seq);
124}
125
126
127static inline int
128wpa_auth_send_eapol(struct wpa_authenticator *wpa_auth, const u8 *addr,
129 const u8 *data, size_t data_len, int encrypt)
130{
131 if (wpa_auth->cb.send_eapol == NULL)
132 return -1;
133 return wpa_auth->cb.send_eapol(wpa_auth->cb.ctx, addr, data, data_len,
134 encrypt);
135}
136
137
138int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
139 int (*cb)(struct wpa_state_machine *sm, void *ctx),
140 void *cb_ctx)
141{
142 if (wpa_auth->cb.for_each_sta == NULL)
143 return 0;
144 return wpa_auth->cb.for_each_sta(wpa_auth->cb.ctx, cb, cb_ctx);
145}
146
147
148int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
149 int (*cb)(struct wpa_authenticator *a, void *ctx),
150 void *cb_ctx)
151{
152 if (wpa_auth->cb.for_each_auth == NULL)
153 return 0;
154 return wpa_auth->cb.for_each_auth(wpa_auth->cb.ctx, cb, cb_ctx);
155}
156
157
158void wpa_auth_logger(struct wpa_authenticator *wpa_auth, const u8 *addr,
159 logger_level level, const char *txt)
160{
161 if (wpa_auth->cb.logger == NULL)
162 return;
163 wpa_auth->cb.logger(wpa_auth->cb.ctx, addr, level, txt);
164}
165
166
167void wpa_auth_vlogger(struct wpa_authenticator *wpa_auth, const u8 *addr,
168 logger_level level, const char *fmt, ...)
169{
170 char *format;
171 int maxlen;
172 va_list ap;
173
174 if (wpa_auth->cb.logger == NULL)
175 return;
176
177 maxlen = os_strlen(fmt) + 100;
178 format = os_malloc(maxlen);
179 if (!format)
180 return;
181
182 va_start(ap, fmt);
183 vsnprintf(format, maxlen, fmt, ap);
184 va_end(ap);
185
186 wpa_auth_logger(wpa_auth, addr, level, format);
187
188 os_free(format);
189}
190
191
192static void wpa_sta_disconnect(struct wpa_authenticator *wpa_auth,
193 const u8 *addr)
194{
195 if (wpa_auth->cb.disconnect == NULL)
196 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800197 wpa_printf(MSG_DEBUG, "wpa_sta_disconnect STA " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 wpa_auth->cb.disconnect(wpa_auth->cb.ctx, addr,
199 WLAN_REASON_PREV_AUTH_NOT_VALID);
200}
201
202
203static int wpa_use_aes_cmac(struct wpa_state_machine *sm)
204{
205 int ret = 0;
206#ifdef CONFIG_IEEE80211R
207 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
208 ret = 1;
209#endif /* CONFIG_IEEE80211R */
210#ifdef CONFIG_IEEE80211W
211 if (wpa_key_mgmt_sha256(sm->wpa_key_mgmt))
212 ret = 1;
213#endif /* CONFIG_IEEE80211W */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800214 if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
215 ret = 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700216 return ret;
217}
218
219
220static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
221{
222 struct wpa_authenticator *wpa_auth = eloop_ctx;
223
224 if (random_get_bytes(wpa_auth->group->GMK, WPA_GMK_LEN)) {
225 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
226 "initialization.");
227 } else {
228 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "GMK rekeyd");
229 wpa_hexdump_key(MSG_DEBUG, "GMK",
230 wpa_auth->group->GMK, WPA_GMK_LEN);
231 }
232
233 if (wpa_auth->conf.wpa_gmk_rekey) {
234 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
235 wpa_rekey_gmk, wpa_auth, NULL);
236 }
237}
238
239
240static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
241{
242 struct wpa_authenticator *wpa_auth = eloop_ctx;
243 struct wpa_group *group;
244
245 wpa_auth_logger(wpa_auth, NULL, LOGGER_DEBUG, "rekeying GTK");
246 for (group = wpa_auth->group; group; group = group->next) {
247 group->GTKReKey = TRUE;
248 do {
249 group->changed = FALSE;
250 wpa_group_sm_step(wpa_auth, group);
251 } while (group->changed);
252 }
253
254 if (wpa_auth->conf.wpa_group_rekey) {
255 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey,
256 0, wpa_rekey_gtk, wpa_auth, NULL);
257 }
258}
259
260
261static void wpa_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
262{
263 struct wpa_authenticator *wpa_auth = eloop_ctx;
264 struct wpa_state_machine *sm = timeout_ctx;
265
266 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "rekeying PTK");
267 wpa_request_new_ptk(sm);
268 wpa_sm_step(sm);
269}
270
271
272static int wpa_auth_pmksa_clear_cb(struct wpa_state_machine *sm, void *ctx)
273{
274 if (sm->pmksa == ctx)
275 sm->pmksa = NULL;
276 return 0;
277}
278
279
280static void wpa_auth_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
281 void *ctx)
282{
283 struct wpa_authenticator *wpa_auth = ctx;
284 wpa_auth_for_each_sta(wpa_auth, wpa_auth_pmksa_clear_cb, entry);
285}
286
287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288static int wpa_group_init_gmk_and_counter(struct wpa_authenticator *wpa_auth,
289 struct wpa_group *group)
290{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800291 u8 buf[ETH_ALEN + 8 + sizeof(unsigned long)];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 u8 rkey[32];
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800293 unsigned long ptr;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700294
295 if (random_get_bytes(group->GMK, WPA_GMK_LEN) < 0)
296 return -1;
297 wpa_hexdump_key(MSG_DEBUG, "GMK", group->GMK, WPA_GMK_LEN);
298
299 /*
300 * Counter = PRF-256(Random number, "Init Counter",
301 * Local MAC Address || Time)
302 */
303 os_memcpy(buf, wpa_auth->addr, ETH_ALEN);
304 wpa_get_ntp_timestamp(buf + ETH_ALEN);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800305 ptr = (unsigned long) group;
306 os_memcpy(buf + ETH_ALEN + 8, &ptr, sizeof(ptr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 if (random_get_bytes(rkey, sizeof(rkey)) < 0)
308 return -1;
309
310 if (sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
311 group->Counter, WPA_NONCE_LEN) < 0)
312 return -1;
313 wpa_hexdump_key(MSG_DEBUG, "Key Counter",
314 group->Counter, WPA_NONCE_LEN);
315
316 return 0;
317}
318
319
320static struct wpa_group * wpa_group_init(struct wpa_authenticator *wpa_auth,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800321 int vlan_id, int delay_init)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322{
323 struct wpa_group *group;
324
325 group = os_zalloc(sizeof(struct wpa_group));
326 if (group == NULL)
327 return NULL;
328
329 group->GTKAuthenticator = TRUE;
330 group->vlan_id = vlan_id;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700331 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332
333 if (random_pool_ready() != 1) {
334 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
335 "for secure operations - update keys later when "
336 "the first station connects");
337 }
338
339 /*
340 * Set initial GMK/Counter value here. The actual values that will be
341 * used in negotiations will be set once the first station tries to
342 * connect. This allows more time for collecting additional randomness
343 * on embedded devices.
344 */
345 if (wpa_group_init_gmk_and_counter(wpa_auth, group) < 0) {
346 wpa_printf(MSG_ERROR, "Failed to get random data for WPA "
347 "initialization.");
348 os_free(group);
349 return NULL;
350 }
351
352 group->GInit = TRUE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800353 if (delay_init) {
354 wpa_printf(MSG_DEBUG, "WPA: Delay group state machine start "
355 "until Beacon frames have been configured");
356 /* Initialization is completed in wpa_init_keys(). */
357 } else {
358 wpa_group_sm_step(wpa_auth, group);
359 group->GInit = FALSE;
360 wpa_group_sm_step(wpa_auth, group);
361 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362
363 return group;
364}
365
366
367/**
368 * wpa_init - Initialize WPA authenticator
369 * @addr: Authenticator address
370 * @conf: Configuration for WPA authenticator
371 * @cb: Callback functions for WPA authenticator
372 * Returns: Pointer to WPA authenticator data or %NULL on failure
373 */
374struct wpa_authenticator * wpa_init(const u8 *addr,
375 struct wpa_auth_config *conf,
376 struct wpa_auth_callbacks *cb)
377{
378 struct wpa_authenticator *wpa_auth;
379
380 wpa_auth = os_zalloc(sizeof(struct wpa_authenticator));
381 if (wpa_auth == NULL)
382 return NULL;
383 os_memcpy(wpa_auth->addr, addr, ETH_ALEN);
384 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
385 os_memcpy(&wpa_auth->cb, cb, sizeof(*cb));
386
387 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
388 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
389 os_free(wpa_auth);
390 return NULL;
391 }
392
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800393 wpa_auth->group = wpa_group_init(wpa_auth, 0, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700394 if (wpa_auth->group == NULL) {
395 os_free(wpa_auth->wpa_ie);
396 os_free(wpa_auth);
397 return NULL;
398 }
399
400 wpa_auth->pmksa = pmksa_cache_auth_init(wpa_auth_pmksa_free_cb,
401 wpa_auth);
402 if (wpa_auth->pmksa == NULL) {
403 wpa_printf(MSG_ERROR, "PMKSA cache initialization failed.");
404 os_free(wpa_auth->wpa_ie);
405 os_free(wpa_auth);
406 return NULL;
407 }
408
409#ifdef CONFIG_IEEE80211R
410 wpa_auth->ft_pmk_cache = wpa_ft_pmk_cache_init();
411 if (wpa_auth->ft_pmk_cache == NULL) {
412 wpa_printf(MSG_ERROR, "FT PMK cache initialization failed.");
413 os_free(wpa_auth->wpa_ie);
414 pmksa_cache_auth_deinit(wpa_auth->pmksa);
415 os_free(wpa_auth);
416 return NULL;
417 }
418#endif /* CONFIG_IEEE80211R */
419
420 if (wpa_auth->conf.wpa_gmk_rekey) {
421 eloop_register_timeout(wpa_auth->conf.wpa_gmk_rekey, 0,
422 wpa_rekey_gmk, wpa_auth, NULL);
423 }
424
425 if (wpa_auth->conf.wpa_group_rekey) {
426 eloop_register_timeout(wpa_auth->conf.wpa_group_rekey, 0,
427 wpa_rekey_gtk, wpa_auth, NULL);
428 }
429
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800430#ifdef CONFIG_P2P
431 if (WPA_GET_BE32(conf->ip_addr_start)) {
432 int count = WPA_GET_BE32(conf->ip_addr_end) -
433 WPA_GET_BE32(conf->ip_addr_start) + 1;
434 if (count > 1000)
435 count = 1000;
436 if (count > 0)
437 wpa_auth->ip_pool = bitfield_alloc(count);
438 }
439#endif /* CONFIG_P2P */
440
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441 return wpa_auth;
442}
443
444
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800445int wpa_init_keys(struct wpa_authenticator *wpa_auth)
446{
447 struct wpa_group *group = wpa_auth->group;
448
449 wpa_printf(MSG_DEBUG, "WPA: Start group state machine to set initial "
450 "keys");
451 wpa_group_sm_step(wpa_auth, group);
452 group->GInit = FALSE;
453 wpa_group_sm_step(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800454 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
455 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800456 return 0;
457}
458
459
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460/**
461 * wpa_deinit - Deinitialize WPA authenticator
462 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
463 */
464void wpa_deinit(struct wpa_authenticator *wpa_auth)
465{
466 struct wpa_group *group, *prev;
467
468 eloop_cancel_timeout(wpa_rekey_gmk, wpa_auth, NULL);
469 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
470
471#ifdef CONFIG_PEERKEY
472 while (wpa_auth->stsl_negotiations)
473 wpa_stsl_remove(wpa_auth, wpa_auth->stsl_negotiations);
474#endif /* CONFIG_PEERKEY */
475
476 pmksa_cache_auth_deinit(wpa_auth->pmksa);
477
478#ifdef CONFIG_IEEE80211R
479 wpa_ft_pmk_cache_deinit(wpa_auth->ft_pmk_cache);
480 wpa_auth->ft_pmk_cache = NULL;
481#endif /* CONFIG_IEEE80211R */
482
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800483#ifdef CONFIG_P2P
484 bitfield_free(wpa_auth->ip_pool);
485#endif /* CONFIG_P2P */
486
487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700488 os_free(wpa_auth->wpa_ie);
489
490 group = wpa_auth->group;
491 while (group) {
492 prev = group;
493 group = group->next;
494 os_free(prev);
495 }
496
497 os_free(wpa_auth);
498}
499
500
501/**
502 * wpa_reconfig - Update WPA authenticator configuration
503 * @wpa_auth: Pointer to WPA authenticator data from wpa_init()
504 * @conf: Configuration for WPA authenticator
505 */
506int wpa_reconfig(struct wpa_authenticator *wpa_auth,
507 struct wpa_auth_config *conf)
508{
509 struct wpa_group *group;
510 if (wpa_auth == NULL)
511 return 0;
512
513 os_memcpy(&wpa_auth->conf, conf, sizeof(*conf));
514 if (wpa_auth_gen_wpa_ie(wpa_auth)) {
515 wpa_printf(MSG_ERROR, "Could not generate WPA IE.");
516 return -1;
517 }
518
519 /*
520 * Reinitialize GTK to make sure it is suitable for the new
521 * configuration.
522 */
523 group = wpa_auth->group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -0700524 group->GTK_len = wpa_cipher_key_len(wpa_auth->conf.wpa_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 group->GInit = TRUE;
526 wpa_group_sm_step(wpa_auth, group);
527 group->GInit = FALSE;
528 wpa_group_sm_step(wpa_auth, group);
529
530 return 0;
531}
532
533
534struct wpa_state_machine *
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700535wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
536 const u8 *p2p_dev_addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537{
538 struct wpa_state_machine *sm;
539
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800540 if (wpa_auth->group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
541 return NULL;
542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543 sm = os_zalloc(sizeof(struct wpa_state_machine));
544 if (sm == NULL)
545 return NULL;
546 os_memcpy(sm->addr, addr, ETH_ALEN);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700547 if (p2p_dev_addr)
548 os_memcpy(sm->p2p_dev_addr, p2p_dev_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549
550 sm->wpa_auth = wpa_auth;
551 sm->group = wpa_auth->group;
552
553 return sm;
554}
555
556
557int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
558 struct wpa_state_machine *sm)
559{
560 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
561 return -1;
562
563#ifdef CONFIG_IEEE80211R
564 if (sm->ft_completed) {
565 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
566 "FT authentication already completed - do not "
567 "start 4-way handshake");
568 return 0;
569 }
570#endif /* CONFIG_IEEE80211R */
571
572 if (sm->started) {
573 os_memset(&sm->key_replay, 0, sizeof(sm->key_replay));
574 sm->ReAuthenticationRequest = TRUE;
575 return wpa_sm_step(sm);
576 }
577
578 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
579 "start authentication");
580 sm->started = 1;
581
582 sm->Init = TRUE;
583 if (wpa_sm_step(sm) == 1)
584 return 1; /* should not really happen */
585 sm->Init = FALSE;
586 sm->AuthenticationRequest = TRUE;
587 return wpa_sm_step(sm);
588}
589
590
591void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm)
592{
593 /* WPA/RSN was not used - clear WPA state. This is needed if the STA
594 * reassociates back to the same AP while the previous entry for the
595 * STA has not yet been removed. */
596 if (sm == NULL)
597 return;
598
599 sm->wpa_key_mgmt = 0;
600}
601
602
603static void wpa_free_sta_sm(struct wpa_state_machine *sm)
604{
Dmitry Shmidtcf32e602014-01-28 10:57:39 -0800605#ifdef CONFIG_P2P
606 if (WPA_GET_BE32(sm->ip_addr)) {
607 u32 start;
608 wpa_printf(MSG_DEBUG, "P2P: Free assigned IP "
609 "address %u.%u.%u.%u from " MACSTR,
610 sm->ip_addr[0], sm->ip_addr[1],
611 sm->ip_addr[2], sm->ip_addr[3],
612 MAC2STR(sm->addr));
613 start = WPA_GET_BE32(sm->wpa_auth->conf.ip_addr_start);
614 bitfield_clear(sm->wpa_auth->ip_pool,
615 WPA_GET_BE32(sm->ip_addr) - start);
616 }
617#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 if (sm->GUpdateStationKeys) {
619 sm->group->GKeyDoneStations--;
620 sm->GUpdateStationKeys = FALSE;
621 }
622#ifdef CONFIG_IEEE80211R
623 os_free(sm->assoc_resp_ftie);
624#endif /* CONFIG_IEEE80211R */
625 os_free(sm->last_rx_eapol_key);
626 os_free(sm->wpa_ie);
627 os_free(sm);
628}
629
630
631void wpa_auth_sta_deinit(struct wpa_state_machine *sm)
632{
633 if (sm == NULL)
634 return;
635
636 if (sm->wpa_auth->conf.wpa_strict_rekey && sm->has_GTK) {
637 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
638 "strict rekeying - force GTK rekey since STA "
639 "is leaving");
640 eloop_cancel_timeout(wpa_rekey_gtk, sm->wpa_auth, NULL);
641 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->wpa_auth,
642 NULL);
643 }
644
645 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
646 sm->pending_1_of_4_timeout = 0;
647 eloop_cancel_timeout(wpa_sm_call_step, sm, NULL);
648 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
649 if (sm->in_step_loop) {
650 /* Must not free state machine while wpa_sm_step() is running.
651 * Freeing will be completed in the end of wpa_sm_step(). */
652 wpa_printf(MSG_DEBUG, "WPA: Registering pending STA state "
653 "machine deinit for " MACSTR, MAC2STR(sm->addr));
654 sm->pending_deinit = 1;
655 } else
656 wpa_free_sta_sm(sm);
657}
658
659
660static void wpa_request_new_ptk(struct wpa_state_machine *sm)
661{
662 if (sm == NULL)
663 return;
664
665 sm->PTKRequest = TRUE;
666 sm->PTK_valid = 0;
667}
668
669
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800670static int wpa_replay_counter_valid(struct wpa_key_replay_counter *ctr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 const u8 *replay_counter)
672{
673 int i;
674 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800675 if (!ctr[i].valid)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676 break;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800677 if (os_memcmp(replay_counter, ctr[i].counter,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 WPA_REPLAY_COUNTER_LEN) == 0)
679 return 1;
680 }
681 return 0;
682}
683
684
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800685static void wpa_replay_counter_mark_invalid(struct wpa_key_replay_counter *ctr,
686 const u8 *replay_counter)
687{
688 int i;
689 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
690 if (ctr[i].valid &&
691 (replay_counter == NULL ||
692 os_memcmp(replay_counter, ctr[i].counter,
693 WPA_REPLAY_COUNTER_LEN) == 0))
694 ctr[i].valid = FALSE;
695 }
696}
697
698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699#ifdef CONFIG_IEEE80211R
700static int ft_check_msg_2_of_4(struct wpa_authenticator *wpa_auth,
701 struct wpa_state_machine *sm,
702 struct wpa_eapol_ie_parse *kde)
703{
704 struct wpa_ie_data ie;
705 struct rsn_mdie *mdie;
706
707 if (wpa_parse_wpa_ie_rsn(kde->rsn_ie, kde->rsn_ie_len, &ie) < 0 ||
708 ie.num_pmkid != 1 || ie.pmkid == NULL) {
709 wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
710 "FT 4-way handshake message 2/4");
711 return -1;
712 }
713
714 os_memcpy(sm->sup_pmk_r1_name, ie.pmkid, PMKID_LEN);
715 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from Supplicant",
716 sm->sup_pmk_r1_name, PMKID_LEN);
717
718 if (!kde->mdie || !kde->ftie) {
719 wpa_printf(MSG_DEBUG, "FT: No %s in FT 4-way handshake "
720 "message 2/4", kde->mdie ? "FTIE" : "MDIE");
721 return -1;
722 }
723
724 mdie = (struct rsn_mdie *) (kde->mdie + 2);
725 if (kde->mdie[1] < sizeof(struct rsn_mdie) ||
726 os_memcmp(wpa_auth->conf.mobility_domain, mdie->mobility_domain,
727 MOBILITY_DOMAIN_ID_LEN) != 0) {
728 wpa_printf(MSG_DEBUG, "FT: MDIE mismatch");
729 return -1;
730 }
731
732 if (sm->assoc_resp_ftie &&
733 (kde->ftie[1] != sm->assoc_resp_ftie[1] ||
734 os_memcmp(kde->ftie, sm->assoc_resp_ftie,
735 2 + sm->assoc_resp_ftie[1]) != 0)) {
736 wpa_printf(MSG_DEBUG, "FT: FTIE mismatch");
737 wpa_hexdump(MSG_DEBUG, "FT: FTIE in EAPOL-Key msg 2/4",
738 kde->ftie, kde->ftie_len);
739 wpa_hexdump(MSG_DEBUG, "FT: FTIE in (Re)AssocResp",
740 sm->assoc_resp_ftie, 2 + sm->assoc_resp_ftie[1]);
741 return -1;
742 }
743
744 return 0;
745}
746#endif /* CONFIG_IEEE80211R */
747
748
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800749static int wpa_receive_error_report(struct wpa_authenticator *wpa_auth,
750 struct wpa_state_machine *sm, int group)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800751{
752 /* Supplicant reported a Michael MIC error */
753 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
754 "received EAPOL-Key Error Request "
755 "(STA detected Michael MIC failure (group=%d))",
756 group);
757
758 if (group && wpa_auth->conf.wpa_group != WPA_CIPHER_TKIP) {
759 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
760 "ignore Michael MIC failure report since "
761 "group cipher is not TKIP");
762 } else if (!group && sm->pairwise != WPA_CIPHER_TKIP) {
763 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
764 "ignore Michael MIC failure report since "
765 "pairwise cipher is not TKIP");
766 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800767 if (wpa_auth_mic_failure_report(wpa_auth, sm->addr) > 0)
768 return 1; /* STA entry was removed */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800769 sm->dot11RSNAStatsTKIPRemoteMICFailures++;
770 wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
771 }
772
773 /*
774 * Error report is not a request for a new key handshake, but since
775 * Authenticator may do it, let's change the keys now anyway.
776 */
777 wpa_request_new_ptk(sm);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800778 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800779}
780
781
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782void wpa_receive(struct wpa_authenticator *wpa_auth,
783 struct wpa_state_machine *sm,
784 u8 *data, size_t data_len)
785{
786 struct ieee802_1x_hdr *hdr;
787 struct wpa_eapol_key *key;
788 u16 key_info, key_data_length;
789 enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST,
790 SMK_M1, SMK_M3, SMK_ERROR } msg;
791 char *msgtxt;
792 struct wpa_eapol_ie_parse kde;
793 int ft;
794 const u8 *eapol_key_ie;
795 size_t eapol_key_ie_len;
796
797 if (wpa_auth == NULL || !wpa_auth->conf.wpa || sm == NULL)
798 return;
799
800 if (data_len < sizeof(*hdr) + sizeof(*key))
801 return;
802
803 hdr = (struct ieee802_1x_hdr *) data;
804 key = (struct wpa_eapol_key *) (hdr + 1);
805 key_info = WPA_GET_BE16(key->key_info);
806 key_data_length = WPA_GET_BE16(key->key_data_length);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800807 wpa_printf(MSG_DEBUG, "WPA: Received EAPOL-Key from " MACSTR
808 " key_info=0x%x type=%u key_data_length=%u",
809 MAC2STR(sm->addr), key_info, key->type, key_data_length);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
811 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
812 "key_data overflow (%d > %lu)",
813 key_data_length,
814 (unsigned long) (data_len - sizeof(*hdr) -
815 sizeof(*key)));
816 return;
817 }
818
819 if (sm->wpa == WPA_VERSION_WPA2) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800820 if (key->type == EAPOL_KEY_TYPE_WPA) {
821 /*
822 * Some deployed station implementations seem to send
823 * msg 4/4 with incorrect type value in WPA2 mode.
824 */
825 wpa_printf(MSG_DEBUG, "Workaround: Allow EAPOL-Key "
826 "with unexpected WPA type in RSN mode");
827 } else if (key->type != EAPOL_KEY_TYPE_RSN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700828 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
829 "unexpected type %d in RSN mode",
830 key->type);
831 return;
832 }
833 } else {
834 if (key->type != EAPOL_KEY_TYPE_WPA) {
835 wpa_printf(MSG_DEBUG, "Ignore EAPOL-Key with "
836 "unexpected type %d in WPA mode",
837 key->type);
838 return;
839 }
840 }
841
842 wpa_hexdump(MSG_DEBUG, "WPA: Received Key Nonce", key->key_nonce,
843 WPA_NONCE_LEN);
844 wpa_hexdump(MSG_DEBUG, "WPA: Received Replay Counter",
845 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
846
847 /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
848 * are set */
849
850 if ((key_info & (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) ==
851 (WPA_KEY_INFO_SMK_MESSAGE | WPA_KEY_INFO_REQUEST)) {
852 if (key_info & WPA_KEY_INFO_ERROR) {
853 msg = SMK_ERROR;
854 msgtxt = "SMK Error";
855 } else {
856 msg = SMK_M1;
857 msgtxt = "SMK M1";
858 }
859 } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
860 msg = SMK_M3;
861 msgtxt = "SMK M3";
862 } else if (key_info & WPA_KEY_INFO_REQUEST) {
863 msg = REQUEST;
864 msgtxt = "Request";
865 } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
866 msg = GROUP_2;
867 msgtxt = "2/2 Group";
868 } else if (key_data_length == 0) {
869 msg = PAIRWISE_4;
870 msgtxt = "4/4 Pairwise";
871 } else {
872 msg = PAIRWISE_2;
873 msgtxt = "2/4 Pairwise";
874 }
875
876 /* TODO: key_info type validation for PeerKey */
877 if (msg == REQUEST || msg == PAIRWISE_2 || msg == PAIRWISE_4 ||
878 msg == GROUP_2) {
879 u16 ver = key_info & WPA_KEY_INFO_TYPE_MASK;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700880 if (sm->pairwise == WPA_CIPHER_CCMP ||
881 sm->pairwise == WPA_CIPHER_GCMP) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 if (wpa_use_aes_cmac(sm) &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800883 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700884 ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
885 wpa_auth_logger(wpa_auth, sm->addr,
886 LOGGER_WARNING,
887 "advertised support for "
888 "AES-128-CMAC, but did not "
889 "use it");
890 return;
891 }
892
893 if (!wpa_use_aes_cmac(sm) &&
894 ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
895 wpa_auth_logger(wpa_auth, sm->addr,
896 LOGGER_WARNING,
897 "did not use HMAC-SHA1-AES "
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700898 "with CCMP/GCMP");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700899 return;
900 }
901 }
902 }
903
904 if (key_info & WPA_KEY_INFO_REQUEST) {
905 if (sm->req_replay_counter_used &&
906 os_memcmp(key->replay_counter, sm->req_replay_counter,
907 WPA_REPLAY_COUNTER_LEN) <= 0) {
908 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_WARNING,
909 "received EAPOL-Key request with "
910 "replayed counter");
911 return;
912 }
913 }
914
915 if (!(key_info & WPA_KEY_INFO_REQUEST) &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800916 !wpa_replay_counter_valid(sm->key_replay, key->replay_counter)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700917 int i;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800918
919 if (msg == PAIRWISE_2 &&
920 wpa_replay_counter_valid(sm->prev_key_replay,
921 key->replay_counter) &&
922 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING &&
923 os_memcmp(sm->SNonce, key->key_nonce, WPA_NONCE_LEN) != 0)
924 {
925 /*
926 * Some supplicant implementations (e.g., Windows XP
927 * WZC) update SNonce for each EAPOL-Key 2/4. This
928 * breaks the workaround on accepting any of the
929 * pending requests, so allow the SNonce to be updated
930 * even if we have already sent out EAPOL-Key 3/4.
931 */
932 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
933 "Process SNonce update from STA "
934 "based on retransmitted EAPOL-Key "
935 "1/4");
936 sm->update_snonce = 1;
937 wpa_replay_counter_mark_invalid(sm->prev_key_replay,
938 key->replay_counter);
939 goto continue_processing;
940 }
941
942 if (msg == PAIRWISE_2 &&
943 wpa_replay_counter_valid(sm->prev_key_replay,
944 key->replay_counter) &&
945 sm->wpa_ptk_state == WPA_PTK_PTKINITNEGOTIATING) {
946 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
947 "ignore retransmitted EAPOL-Key %s - "
948 "SNonce did not change", msgtxt);
949 } else {
950 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
951 "received EAPOL-Key %s with "
952 "unexpected replay counter", msgtxt);
953 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700954 for (i = 0; i < RSNA_MAX_EAPOL_RETRIES; i++) {
955 if (!sm->key_replay[i].valid)
956 break;
957 wpa_hexdump(MSG_DEBUG, "pending replay counter",
958 sm->key_replay[i].counter,
959 WPA_REPLAY_COUNTER_LEN);
960 }
961 wpa_hexdump(MSG_DEBUG, "received replay counter",
962 key->replay_counter, WPA_REPLAY_COUNTER_LEN);
963 return;
964 }
965
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800966continue_processing:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 switch (msg) {
968 case PAIRWISE_2:
969 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800970 sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING &&
971 (!sm->update_snonce ||
972 sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700973 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
974 "received EAPOL-Key msg 2/4 in "
975 "invalid state (%d) - dropped",
976 sm->wpa_ptk_state);
977 return;
978 }
979 random_add_randomness(key->key_nonce, WPA_NONCE_LEN);
980 if (sm->group->reject_4way_hs_for_entropy) {
981 /*
982 * The system did not have enough entropy to generate
983 * strong random numbers. Reject the first 4-way
984 * handshake(s) and collect some entropy based on the
985 * information from it. Once enough entropy is
986 * available, the next atempt will trigger GMK/Key
987 * Counter update and the station will be allowed to
988 * continue.
989 */
990 wpa_printf(MSG_DEBUG, "WPA: Reject 4-way handshake to "
991 "collect more entropy for random number "
992 "generation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993 random_mark_pool_ready();
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700994 wpa_sta_disconnect(wpa_auth, sm->addr);
995 return;
996 }
997 if (wpa_parse_kde_ies((u8 *) (key + 1), key_data_length,
998 &kde) < 0) {
999 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1000 "received EAPOL-Key msg 2/4 with "
1001 "invalid Key Data contents");
1002 return;
1003 }
1004 if (kde.rsn_ie) {
1005 eapol_key_ie = kde.rsn_ie;
1006 eapol_key_ie_len = kde.rsn_ie_len;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001007 } else if (kde.osen) {
1008 eapol_key_ie = kde.osen;
1009 eapol_key_ie_len = kde.osen_len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 } else {
1011 eapol_key_ie = kde.wpa_ie;
1012 eapol_key_ie_len = kde.wpa_ie_len;
1013 }
1014 ft = sm->wpa == WPA_VERSION_WPA2 &&
1015 wpa_key_mgmt_ft(sm->wpa_key_mgmt);
1016 if (sm->wpa_ie == NULL ||
1017 wpa_compare_rsn_ie(ft,
1018 sm->wpa_ie, sm->wpa_ie_len,
1019 eapol_key_ie, eapol_key_ie_len)) {
1020 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1021 "WPA IE from (Re)AssocReq did not "
1022 "match with msg 2/4");
1023 if (sm->wpa_ie) {
1024 wpa_hexdump(MSG_DEBUG, "WPA IE in AssocReq",
1025 sm->wpa_ie, sm->wpa_ie_len);
1026 }
1027 wpa_hexdump(MSG_DEBUG, "WPA IE in msg 2/4",
1028 eapol_key_ie, eapol_key_ie_len);
1029 /* MLME-DEAUTHENTICATE.request */
1030 wpa_sta_disconnect(wpa_auth, sm->addr);
1031 return;
1032 }
1033#ifdef CONFIG_IEEE80211R
1034 if (ft && ft_check_msg_2_of_4(wpa_auth, sm, &kde) < 0) {
1035 wpa_sta_disconnect(wpa_auth, sm->addr);
1036 return;
1037 }
1038#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001039#ifdef CONFIG_P2P
1040 if (kde.ip_addr_req && kde.ip_addr_req[0] &&
1041 wpa_auth->ip_pool && WPA_GET_BE32(sm->ip_addr) == 0) {
1042 int idx;
1043 wpa_printf(MSG_DEBUG, "P2P: IP address requested in "
1044 "EAPOL-Key exchange");
1045 idx = bitfield_get_first_zero(wpa_auth->ip_pool);
1046 if (idx >= 0) {
1047 u32 start = WPA_GET_BE32(wpa_auth->conf.
1048 ip_addr_start);
1049 bitfield_set(wpa_auth->ip_pool, idx);
1050 WPA_PUT_BE32(sm->ip_addr, start + idx);
1051 wpa_printf(MSG_DEBUG, "P2P: Assigned IP "
1052 "address %u.%u.%u.%u to " MACSTR,
1053 sm->ip_addr[0], sm->ip_addr[1],
1054 sm->ip_addr[2], sm->ip_addr[3],
1055 MAC2STR(sm->addr));
1056 }
1057 }
1058#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001059 break;
1060 case PAIRWISE_4:
1061 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1062 !sm->PTK_valid) {
1063 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1064 "received EAPOL-Key msg 4/4 in "
1065 "invalid state (%d) - dropped",
1066 sm->wpa_ptk_state);
1067 return;
1068 }
1069 break;
1070 case GROUP_2:
1071 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1072 || !sm->PTK_valid) {
1073 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_INFO,
1074 "received EAPOL-Key msg 2/2 in "
1075 "invalid state (%d) - dropped",
1076 sm->wpa_ptk_group_state);
1077 return;
1078 }
1079 break;
1080#ifdef CONFIG_PEERKEY
1081 case SMK_M1:
1082 case SMK_M3:
1083 case SMK_ERROR:
1084 if (!wpa_auth->conf.peerkey) {
1085 wpa_printf(MSG_DEBUG, "RSN: SMK M1/M3/Error, but "
1086 "PeerKey use disabled - ignoring message");
1087 return;
1088 }
1089 if (!sm->PTK_valid) {
1090 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1091 "received EAPOL-Key msg SMK in "
1092 "invalid state - dropped");
1093 return;
1094 }
1095 break;
1096#else /* CONFIG_PEERKEY */
1097 case SMK_M1:
1098 case SMK_M3:
1099 case SMK_ERROR:
1100 return; /* STSL disabled - ignore SMK messages */
1101#endif /* CONFIG_PEERKEY */
1102 case REQUEST:
1103 break;
1104 }
1105
1106 wpa_auth_vlogger(wpa_auth, sm->addr, LOGGER_DEBUG,
1107 "received EAPOL-Key frame (%s)", msgtxt);
1108
1109 if (key_info & WPA_KEY_INFO_ACK) {
1110 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1111 "received invalid EAPOL-Key: Key Ack set");
1112 return;
1113 }
1114
1115 if (!(key_info & WPA_KEY_INFO_MIC)) {
1116 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1117 "received invalid EAPOL-Key: Key MIC not set");
1118 return;
1119 }
1120
1121 sm->MICVerified = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001122 if (sm->PTK_valid && !sm->update_snonce) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001123 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
1124 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1125 "received EAPOL-Key with invalid MIC");
1126 return;
1127 }
1128 sm->MICVerified = TRUE;
1129 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
1130 sm->pending_1_of_4_timeout = 0;
1131 }
1132
1133 if (key_info & WPA_KEY_INFO_REQUEST) {
1134 if (sm->MICVerified) {
1135 sm->req_replay_counter_used = 1;
1136 os_memcpy(sm->req_replay_counter, key->replay_counter,
1137 WPA_REPLAY_COUNTER_LEN);
1138 } else {
1139 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1140 "received EAPOL-Key request with "
1141 "invalid MIC");
1142 return;
1143 }
1144
1145 /*
1146 * TODO: should decrypt key data field if encryption was used;
1147 * even though MAC address KDE is not normally encrypted,
1148 * supplicant is allowed to encrypt it.
1149 */
1150 if (msg == SMK_ERROR) {
1151#ifdef CONFIG_PEERKEY
1152 wpa_smk_error(wpa_auth, sm, key);
1153#endif /* CONFIG_PEERKEY */
1154 return;
1155 } else if (key_info & WPA_KEY_INFO_ERROR) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001156 if (wpa_receive_error_report(
1157 wpa_auth, sm,
1158 !(key_info & WPA_KEY_INFO_KEY_TYPE)) > 0)
1159 return; /* STA entry was removed */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1161 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1162 "received EAPOL-Key Request for new "
1163 "4-Way Handshake");
1164 wpa_request_new_ptk(sm);
1165#ifdef CONFIG_PEERKEY
1166 } else if (msg == SMK_M1) {
1167 wpa_smk_m1(wpa_auth, sm, key);
1168#endif /* CONFIG_PEERKEY */
1169 } else if (key_data_length > 0 &&
1170 wpa_parse_kde_ies((const u8 *) (key + 1),
1171 key_data_length, &kde) == 0 &&
1172 kde.mac_addr) {
1173 } else {
1174 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1175 "received EAPOL-Key Request for GTK "
1176 "rekeying");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 eloop_cancel_timeout(wpa_rekey_gtk, wpa_auth, NULL);
1178 wpa_rekey_gtk(wpa_auth, NULL);
1179 }
1180 } else {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001181 /* Do not allow the same key replay counter to be reused. */
1182 wpa_replay_counter_mark_invalid(sm->key_replay,
1183 key->replay_counter);
1184
1185 if (msg == PAIRWISE_2) {
1186 /*
1187 * Maintain a copy of the pending EAPOL-Key frames in
1188 * case the EAPOL-Key frame was retransmitted. This is
1189 * needed to allow EAPOL-Key msg 2/4 reply to another
1190 * pending msg 1/4 to update the SNonce to work around
1191 * unexpected supplicant behavior.
1192 */
1193 os_memcpy(sm->prev_key_replay, sm->key_replay,
1194 sizeof(sm->key_replay));
1195 } else {
1196 os_memset(sm->prev_key_replay, 0,
1197 sizeof(sm->prev_key_replay));
1198 }
1199
1200 /*
1201 * Make sure old valid counters are not accepted anymore and
1202 * do not get copied again.
1203 */
1204 wpa_replay_counter_mark_invalid(sm->key_replay, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001205 }
1206
1207#ifdef CONFIG_PEERKEY
1208 if (msg == SMK_M3) {
1209 wpa_smk_m3(wpa_auth, sm, key);
1210 return;
1211 }
1212#endif /* CONFIG_PEERKEY */
1213
1214 os_free(sm->last_rx_eapol_key);
1215 sm->last_rx_eapol_key = os_malloc(data_len);
1216 if (sm->last_rx_eapol_key == NULL)
1217 return;
1218 os_memcpy(sm->last_rx_eapol_key, data, data_len);
1219 sm->last_rx_eapol_key_len = data_len;
1220
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001221 sm->rx_eapol_key_secure = !!(key_info & WPA_KEY_INFO_SECURE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001222 sm->EAPOLKeyReceived = TRUE;
1223 sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1224 sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1225 os_memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1226 wpa_sm_step(sm);
1227}
1228
1229
1230static int wpa_gmk_to_gtk(const u8 *gmk, const char *label, const u8 *addr,
1231 const u8 *gnonce, u8 *gtk, size_t gtk_len)
1232{
1233 u8 data[ETH_ALEN + WPA_NONCE_LEN + 8 + 16];
1234 u8 *pos;
1235 int ret = 0;
1236
1237 /* GTK = PRF-X(GMK, "Group key expansion",
1238 * AA || GNonce || Time || random data)
1239 * The example described in the IEEE 802.11 standard uses only AA and
1240 * GNonce as inputs here. Add some more entropy since this derivation
1241 * is done only at the Authenticator and as such, does not need to be
1242 * exactly same.
1243 */
1244 os_memcpy(data, addr, ETH_ALEN);
1245 os_memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1246 pos = data + ETH_ALEN + WPA_NONCE_LEN;
1247 wpa_get_ntp_timestamp(pos);
1248 pos += 8;
1249 if (random_get_bytes(pos, 16) < 0)
1250 ret = -1;
1251
1252#ifdef CONFIG_IEEE80211W
1253 sha256_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len);
1254#else /* CONFIG_IEEE80211W */
1255 if (sha1_prf(gmk, WPA_GMK_LEN, label, data, sizeof(data), gtk, gtk_len)
1256 < 0)
1257 ret = -1;
1258#endif /* CONFIG_IEEE80211W */
1259
1260 return ret;
1261}
1262
1263
1264static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1265{
1266 struct wpa_authenticator *wpa_auth = eloop_ctx;
1267 struct wpa_state_machine *sm = timeout_ctx;
1268
1269 sm->pending_1_of_4_timeout = 0;
1270 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG, "EAPOL-Key timeout");
1271 sm->TimeoutEvt = TRUE;
1272 wpa_sm_step(sm);
1273}
1274
1275
1276void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1277 struct wpa_state_machine *sm, int key_info,
1278 const u8 *key_rsc, const u8 *nonce,
1279 const u8 *kde, size_t kde_len,
1280 int keyidx, int encr, int force_version)
1281{
1282 struct ieee802_1x_hdr *hdr;
1283 struct wpa_eapol_key *key;
1284 size_t len;
1285 int alg;
1286 int key_data_len, pad_len = 0;
1287 u8 *buf, *pos;
1288 int version, pairwise;
1289 int i;
1290
1291 len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
1292
1293 if (force_version)
1294 version = force_version;
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001295 else if (sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN)
1296 version = WPA_KEY_INFO_TYPE_AKM_DEFINED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001297 else if (wpa_use_aes_cmac(sm))
1298 version = WPA_KEY_INFO_TYPE_AES_128_CMAC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001299 else if (sm->pairwise != WPA_CIPHER_TKIP)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001300 version = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1301 else
1302 version = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1303
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001304 pairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001305
1306 wpa_printf(MSG_DEBUG, "WPA: Send EAPOL(version=%d secure=%d mic=%d "
1307 "ack=%d install=%d pairwise=%d kde_len=%lu keyidx=%d "
1308 "encr=%d)",
1309 version,
1310 (key_info & WPA_KEY_INFO_SECURE) ? 1 : 0,
1311 (key_info & WPA_KEY_INFO_MIC) ? 1 : 0,
1312 (key_info & WPA_KEY_INFO_ACK) ? 1 : 0,
1313 (key_info & WPA_KEY_INFO_INSTALL) ? 1 : 0,
1314 pairwise, (unsigned long) kde_len, keyidx, encr);
1315
1316 key_data_len = kde_len;
1317
1318 if ((version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001319 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001320 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) && encr) {
1321 pad_len = key_data_len % 8;
1322 if (pad_len)
1323 pad_len = 8 - pad_len;
1324 key_data_len += pad_len + 8;
1325 }
1326
1327 len += key_data_len;
1328
1329 hdr = os_zalloc(len);
1330 if (hdr == NULL)
1331 return;
1332 hdr->version = wpa_auth->conf.eapol_version;
1333 hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1334 hdr->length = host_to_be16(len - sizeof(*hdr));
1335 key = (struct wpa_eapol_key *) (hdr + 1);
1336
1337 key->type = sm->wpa == WPA_VERSION_WPA2 ?
1338 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1339 key_info |= version;
1340 if (encr && sm->wpa == WPA_VERSION_WPA2)
1341 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1342 if (sm->wpa != WPA_VERSION_WPA2)
1343 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1344 WPA_PUT_BE16(key->key_info, key_info);
1345
1346 alg = pairwise ? sm->pairwise : wpa_auth->conf.wpa_group;
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07001347 WPA_PUT_BE16(key->key_length, wpa_cipher_key_len(alg));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 if (key_info & WPA_KEY_INFO_SMK_MESSAGE)
1349 WPA_PUT_BE16(key->key_length, 0);
1350
1351 /* FIX: STSL: what to use as key_replay_counter? */
1352 for (i = RSNA_MAX_EAPOL_RETRIES - 1; i > 0; i--) {
1353 sm->key_replay[i].valid = sm->key_replay[i - 1].valid;
1354 os_memcpy(sm->key_replay[i].counter,
1355 sm->key_replay[i - 1].counter,
1356 WPA_REPLAY_COUNTER_LEN);
1357 }
1358 inc_byte_array(sm->key_replay[0].counter, WPA_REPLAY_COUNTER_LEN);
1359 os_memcpy(key->replay_counter, sm->key_replay[0].counter,
1360 WPA_REPLAY_COUNTER_LEN);
1361 sm->key_replay[0].valid = TRUE;
1362
1363 if (nonce)
1364 os_memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1365
1366 if (key_rsc)
1367 os_memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1368
1369 if (kde && !encr) {
1370 os_memcpy(key + 1, kde, kde_len);
1371 WPA_PUT_BE16(key->key_data_length, kde_len);
1372 } else if (encr && kde) {
1373 buf = os_zalloc(key_data_len);
1374 if (buf == NULL) {
1375 os_free(hdr);
1376 return;
1377 }
1378 pos = buf;
1379 os_memcpy(pos, kde, kde_len);
1380 pos += kde_len;
1381
1382 if (pad_len)
1383 *pos++ = 0xdd;
1384
1385 wpa_hexdump_key(MSG_DEBUG, "Plaintext EAPOL-Key Key Data",
1386 buf, key_data_len);
1387 if (version == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001388 sm->wpa_key_mgmt == WPA_KEY_MGMT_OSEN ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389 version == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1390 if (aes_wrap(sm->PTK.kek, (key_data_len - 8) / 8, buf,
1391 (u8 *) (key + 1))) {
1392 os_free(hdr);
1393 os_free(buf);
1394 return;
1395 }
1396 WPA_PUT_BE16(key->key_data_length, key_data_len);
1397 } else {
1398 u8 ek[32];
1399 os_memcpy(key->key_iv,
1400 sm->group->Counter + WPA_NONCE_LEN - 16, 16);
1401 inc_byte_array(sm->group->Counter, WPA_NONCE_LEN);
1402 os_memcpy(ek, key->key_iv, 16);
1403 os_memcpy(ek + 16, sm->PTK.kek, 16);
1404 os_memcpy(key + 1, buf, key_data_len);
1405 rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1406 WPA_PUT_BE16(key->key_data_length, key_data_len);
1407 }
1408 os_free(buf);
1409 }
1410
1411 if (key_info & WPA_KEY_INFO_MIC) {
1412 if (!sm->PTK_valid) {
1413 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
1414 "PTK not valid when sending EAPOL-Key "
1415 "frame");
1416 os_free(hdr);
1417 return;
1418 }
1419 wpa_eapol_key_mic(sm->PTK.kck, version, (u8 *) hdr, len,
1420 key->key_mic);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001421#ifdef CONFIG_TESTING_OPTIONS
1422 if (!pairwise &&
1423 wpa_auth->conf.corrupt_gtk_rekey_mic_probability > 0.0d &&
1424 drand48() <
1425 wpa_auth->conf.corrupt_gtk_rekey_mic_probability) {
1426 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_INFO,
1427 "Corrupting group EAPOL-Key Key MIC");
1428 key->key_mic[0]++;
1429 }
1430#endif /* CONFIG_TESTING_OPTIONS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001431 }
1432
1433 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_inc_EapolFramesTx,
1434 1);
1435 wpa_auth_send_eapol(wpa_auth, sm->addr, (u8 *) hdr, len,
1436 sm->pairwise_set);
1437 os_free(hdr);
1438}
1439
1440
1441static void wpa_send_eapol(struct wpa_authenticator *wpa_auth,
1442 struct wpa_state_machine *sm, int key_info,
1443 const u8 *key_rsc, const u8 *nonce,
1444 const u8 *kde, size_t kde_len,
1445 int keyidx, int encr)
1446{
1447 int timeout_ms;
1448 int pairwise = key_info & WPA_KEY_INFO_KEY_TYPE;
1449 int ctr;
1450
1451 if (sm == NULL)
1452 return;
1453
1454 __wpa_send_eapol(wpa_auth, sm, key_info, key_rsc, nonce, kde, kde_len,
1455 keyidx, encr, 0);
1456
1457 ctr = pairwise ? sm->TimeoutCtr : sm->GTimeoutCtr;
1458 if (ctr == 1 && wpa_auth->conf.tx_status)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001459 timeout_ms = pairwise ? eapol_key_timeout_first :
1460 eapol_key_timeout_first_group;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001461 else
1462 timeout_ms = eapol_key_timeout_subseq;
1463 if (pairwise && ctr == 1 && !(key_info & WPA_KEY_INFO_MIC))
1464 sm->pending_1_of_4_timeout = 1;
1465 wpa_printf(MSG_DEBUG, "WPA: Use EAPOL-Key timeout of %u ms (retry "
1466 "counter %d)", timeout_ms, ctr);
1467 eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1468 wpa_send_eapol_timeout, wpa_auth, sm);
1469}
1470
1471
1472static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1473{
1474 struct ieee802_1x_hdr *hdr;
1475 struct wpa_eapol_key *key;
1476 u16 key_info;
1477 int ret = 0;
1478 u8 mic[16];
1479
1480 if (data_len < sizeof(*hdr) + sizeof(*key))
1481 return -1;
1482
1483 hdr = (struct ieee802_1x_hdr *) data;
1484 key = (struct wpa_eapol_key *) (hdr + 1);
1485 key_info = WPA_GET_BE16(key->key_info);
1486 os_memcpy(mic, key->key_mic, 16);
1487 os_memset(key->key_mic, 0, 16);
1488 if (wpa_eapol_key_mic(PTK->kck, key_info & WPA_KEY_INFO_TYPE_MASK,
1489 data, data_len, key->key_mic) ||
1490 os_memcmp(mic, key->key_mic, 16) != 0)
1491 ret = -1;
1492 os_memcpy(key->key_mic, mic, 16);
1493 return ret;
1494}
1495
1496
1497void wpa_remove_ptk(struct wpa_state_machine *sm)
1498{
1499 sm->PTK_valid = FALSE;
1500 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1501 wpa_auth_set_key(sm->wpa_auth, 0, WPA_ALG_NONE, sm->addr, 0, NULL, 0);
1502 sm->pairwise_set = FALSE;
1503 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
1504}
1505
1506
1507int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event)
1508{
1509 int remove_ptk = 1;
1510
1511 if (sm == NULL)
1512 return -1;
1513
1514 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1515 "event %d notification", event);
1516
1517 switch (event) {
1518 case WPA_AUTH:
1519 case WPA_ASSOC:
1520 break;
1521 case WPA_DEAUTH:
1522 case WPA_DISASSOC:
1523 sm->DeauthenticationRequest = TRUE;
1524 break;
1525 case WPA_REAUTH:
1526 case WPA_REAUTH_EAPOL:
1527 if (!sm->started) {
1528 /*
1529 * When using WPS, we may end up here if the STA
1530 * manages to re-associate without the previous STA
1531 * entry getting removed. Consequently, we need to make
1532 * sure that the WPA state machines gets initialized
1533 * properly at this point.
1534 */
1535 wpa_printf(MSG_DEBUG, "WPA state machine had not been "
1536 "started - initialize now");
1537 sm->started = 1;
1538 sm->Init = TRUE;
1539 if (wpa_sm_step(sm) == 1)
1540 return 1; /* should not really happen */
1541 sm->Init = FALSE;
1542 sm->AuthenticationRequest = TRUE;
1543 break;
1544 }
1545 if (sm->GUpdateStationKeys) {
1546 /*
1547 * Reauthentication cancels the pending group key
1548 * update for this STA.
1549 */
1550 sm->group->GKeyDoneStations--;
1551 sm->GUpdateStationKeys = FALSE;
1552 sm->PtkGroupInit = TRUE;
1553 }
1554 sm->ReAuthenticationRequest = TRUE;
1555 break;
1556 case WPA_ASSOC_FT:
1557#ifdef CONFIG_IEEE80211R
1558 wpa_printf(MSG_DEBUG, "FT: Retry PTK configuration "
1559 "after association");
1560 wpa_ft_install_ptk(sm);
1561
1562 /* Using FT protocol, not WPA auth state machine */
1563 sm->ft_completed = 1;
1564 return 0;
1565#else /* CONFIG_IEEE80211R */
1566 break;
1567#endif /* CONFIG_IEEE80211R */
1568 }
1569
1570#ifdef CONFIG_IEEE80211R
1571 sm->ft_completed = 0;
1572#endif /* CONFIG_IEEE80211R */
1573
1574#ifdef CONFIG_IEEE80211W
1575 if (sm->mgmt_frame_prot && event == WPA_AUTH)
1576 remove_ptk = 0;
1577#endif /* CONFIG_IEEE80211W */
1578
1579 if (remove_ptk) {
1580 sm->PTK_valid = FALSE;
1581 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1582
1583 if (event != WPA_REAUTH_EAPOL)
1584 wpa_remove_ptk(sm);
1585 }
1586
1587 return wpa_sm_step(sm);
1588}
1589
1590
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001591SM_STATE(WPA_PTK, INITIALIZE)
1592{
1593 SM_ENTRY_MA(WPA_PTK, INITIALIZE, wpa_ptk);
1594 if (sm->Init) {
1595 /* Init flag is not cleared here, so avoid busy
1596 * loop by claiming nothing changed. */
1597 sm->changed = FALSE;
1598 }
1599
1600 sm->keycount = 0;
1601 if (sm->GUpdateStationKeys)
1602 sm->group->GKeyDoneStations--;
1603 sm->GUpdateStationKeys = FALSE;
1604 if (sm->wpa == WPA_VERSION_WPA)
1605 sm->PInitAKeys = FALSE;
1606 if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
1607 * Local AA > Remote AA)) */) {
1608 sm->Pair = TRUE;
1609 }
1610 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 0);
1611 wpa_remove_ptk(sm);
1612 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid, 0);
1613 sm->TimeoutCtr = 0;
1614 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1615 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
1616 WPA_EAPOL_authorized, 0);
1617 }
1618}
1619
1620
1621SM_STATE(WPA_PTK, DISCONNECT)
1622{
1623 SM_ENTRY_MA(WPA_PTK, DISCONNECT, wpa_ptk);
1624 sm->Disconnect = FALSE;
1625 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
1626}
1627
1628
1629SM_STATE(WPA_PTK, DISCONNECTED)
1630{
1631 SM_ENTRY_MA(WPA_PTK, DISCONNECTED, wpa_ptk);
1632 sm->DeauthenticationRequest = FALSE;
1633}
1634
1635
1636SM_STATE(WPA_PTK, AUTHENTICATION)
1637{
1638 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION, wpa_ptk);
1639 os_memset(&sm->PTK, 0, sizeof(sm->PTK));
1640 sm->PTK_valid = FALSE;
1641 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portControl_Auto,
1642 1);
1643 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portEnabled, 1);
1644 sm->AuthenticationRequest = FALSE;
1645}
1646
1647
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001648static void wpa_group_ensure_init(struct wpa_authenticator *wpa_auth,
1649 struct wpa_group *group)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001650{
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001651 if (group->first_sta_seen)
1652 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001653 /*
1654 * System has run bit further than at the time hostapd was started
1655 * potentially very early during boot up. This provides better chances
1656 * of collecting more randomness on embedded systems. Re-initialize the
1657 * GMK and Counter here to improve their strength if there was not
1658 * enough entropy available immediately after system startup.
1659 */
1660 wpa_printf(MSG_DEBUG, "WPA: Re-initialize GMK/Counter on first "
1661 "station");
1662 if (random_pool_ready() != 1) {
1663 wpa_printf(MSG_INFO, "WPA: Not enough entropy in random pool "
1664 "to proceed - reject first 4-way handshake");
1665 group->reject_4way_hs_for_entropy = TRUE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001666 } else {
1667 group->first_sta_seen = TRUE;
1668 group->reject_4way_hs_for_entropy = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001669 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001670
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001671 wpa_group_init_gmk_and_counter(wpa_auth, group);
1672 wpa_gtk_update(wpa_auth, group);
1673 wpa_group_config_group_keys(wpa_auth, group);
1674}
1675
1676
1677SM_STATE(WPA_PTK, AUTHENTICATION2)
1678{
1679 SM_ENTRY_MA(WPA_PTK, AUTHENTICATION2, wpa_ptk);
1680
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001681 wpa_group_ensure_init(sm->wpa_auth, sm->group);
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001682 sm->ReAuthenticationRequest = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683
Dmitry Shmidt04949592012-07-19 12:16:46 -07001684 /*
1685 * Definition of ANonce selection in IEEE Std 802.11i-2004 is somewhat
1686 * ambiguous. The Authenticator state machine uses a counter that is
1687 * incremented by one for each 4-way handshake. However, the security
1688 * analysis of 4-way handshake points out that unpredictable nonces
1689 * help in preventing precomputation attacks. Instead of the state
1690 * machine definition, use an unpredictable nonce value here to provide
1691 * stronger protection against potential precomputation attacks.
1692 */
1693 if (random_get_bytes(sm->ANonce, WPA_NONCE_LEN)) {
1694 wpa_printf(MSG_ERROR, "WPA: Failed to get random data for "
1695 "ANonce.");
Dmitry Shmidt444d5672013-04-01 13:08:44 -07001696 sm->Disconnect = TRUE;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001697 return;
1698 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 wpa_hexdump(MSG_DEBUG, "WPA: Assign ANonce", sm->ANonce,
1700 WPA_NONCE_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001701 /* IEEE 802.11i does not clear TimeoutCtr here, but this is more
1702 * logical place than INITIALIZE since AUTHENTICATION2 can be
1703 * re-entered on ReAuthenticationRequest without going through
1704 * INITIALIZE. */
1705 sm->TimeoutCtr = 0;
1706}
1707
1708
1709SM_STATE(WPA_PTK, INITPMK)
1710{
1711 u8 msk[2 * PMK_LEN];
1712 size_t len = 2 * PMK_LEN;
1713
1714 SM_ENTRY_MA(WPA_PTK, INITPMK, wpa_ptk);
1715#ifdef CONFIG_IEEE80211R
1716 sm->xxkey_len = 0;
1717#endif /* CONFIG_IEEE80211R */
1718 if (sm->pmksa) {
1719 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
1720 os_memcpy(sm->PMK, sm->pmksa->pmk, PMK_LEN);
1721 } else if (wpa_auth_get_msk(sm->wpa_auth, sm->addr, msk, &len) == 0) {
1722 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
1723 "(len=%lu)", (unsigned long) len);
1724 os_memcpy(sm->PMK, msk, PMK_LEN);
1725#ifdef CONFIG_IEEE80211R
1726 if (len >= 2 * PMK_LEN) {
1727 os_memcpy(sm->xxkey, msk + PMK_LEN, PMK_LEN);
1728 sm->xxkey_len = PMK_LEN;
1729 }
1730#endif /* CONFIG_IEEE80211R */
1731 } else {
1732 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
1733 }
1734
1735 sm->req_replay_counter_used = 0;
1736 /* IEEE 802.11i does not set keyRun to FALSE, but not doing this
1737 * will break reauthentication since EAPOL state machines may not be
1738 * get into AUTHENTICATING state that clears keyRun before WPA state
1739 * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
1740 * state and takes PMK from the previously used AAA Key. This will
1741 * eventually fail in 4-Way Handshake because Supplicant uses PMK
1742 * derived from the new AAA Key. Setting keyRun = FALSE here seems to
1743 * be good workaround for this issue. */
1744 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyRun, 0);
1745}
1746
1747
1748SM_STATE(WPA_PTK, INITPSK)
1749{
1750 const u8 *psk;
1751 SM_ENTRY_MA(WPA_PTK, INITPSK, wpa_ptk);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001752 psk = wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001753 if (psk) {
1754 os_memcpy(sm->PMK, psk, PMK_LEN);
1755#ifdef CONFIG_IEEE80211R
1756 os_memcpy(sm->xxkey, psk, PMK_LEN);
1757 sm->xxkey_len = PMK_LEN;
1758#endif /* CONFIG_IEEE80211R */
1759 }
1760 sm->req_replay_counter_used = 0;
1761}
1762
1763
1764SM_STATE(WPA_PTK, PTKSTART)
1765{
1766 u8 buf[2 + RSN_SELECTOR_LEN + PMKID_LEN], *pmkid = NULL;
1767 size_t pmkid_len = 0;
1768
1769 SM_ENTRY_MA(WPA_PTK, PTKSTART, wpa_ptk);
1770 sm->PTKRequest = FALSE;
1771 sm->TimeoutEvt = FALSE;
1772
1773 sm->TimeoutCtr++;
1774 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1775 /* No point in sending the EAPOL-Key - we will disconnect
1776 * immediately following this. */
1777 return;
1778 }
1779
1780 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1781 "sending 1/4 msg of 4-Way Handshake");
1782 /*
1783 * TODO: Could add PMKID even with WPA2-PSK, but only if there is only
1784 * one possible PSK for this STA.
1785 */
1786 if (sm->wpa == WPA_VERSION_WPA2 &&
Dmitry Shmidtf21452a2014-02-26 10:55:25 -08001787 wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
1788 sm->wpa_key_mgmt != WPA_KEY_MGMT_OSEN) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001789 pmkid = buf;
1790 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
1791 pmkid[0] = WLAN_EID_VENDOR_SPECIFIC;
1792 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
1793 RSN_SELECTOR_PUT(&pmkid[2], RSN_KEY_DATA_PMKID);
1794 if (sm->pmksa)
1795 os_memcpy(&pmkid[2 + RSN_SELECTOR_LEN],
1796 sm->pmksa->pmkid, PMKID_LEN);
1797 else {
1798 /*
1799 * Calculate PMKID since no PMKSA cache entry was
1800 * available with pre-calculated PMKID.
1801 */
1802 rsn_pmkid(sm->PMK, PMK_LEN, sm->wpa_auth->addr,
1803 sm->addr, &pmkid[2 + RSN_SELECTOR_LEN],
1804 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1805 }
1806 }
1807 wpa_send_eapol(sm->wpa_auth, sm,
1808 WPA_KEY_INFO_ACK | WPA_KEY_INFO_KEY_TYPE, NULL,
1809 sm->ANonce, pmkid, pmkid_len, 0, 0);
1810}
1811
1812
1813static int wpa_derive_ptk(struct wpa_state_machine *sm, const u8 *pmk,
1814 struct wpa_ptk *ptk)
1815{
Dmitry Shmidt98660862014-03-11 17:26:21 -07001816 size_t ptk_len = wpa_cipher_key_len(sm->pairwise) + 32;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001817#ifdef CONFIG_IEEE80211R
1818 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt))
1819 return wpa_auth_derive_ptk_ft(sm, pmk, ptk, ptk_len);
1820#endif /* CONFIG_IEEE80211R */
1821
1822 wpa_pmk_to_ptk(pmk, PMK_LEN, "Pairwise key expansion",
1823 sm->wpa_auth->addr, sm->addr, sm->ANonce, sm->SNonce,
1824 (u8 *) ptk, ptk_len,
1825 wpa_key_mgmt_sha256(sm->wpa_key_mgmt));
1826
1827 return 0;
1828}
1829
1830
1831SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
1832{
1833 struct wpa_ptk PTK;
1834 int ok = 0;
1835 const u8 *pmk = NULL;
1836
1837 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
1838 sm->EAPOLKeyReceived = FALSE;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001839 sm->update_snonce = FALSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001840
1841 /* WPA with IEEE 802.1X: use the derived PMK from EAP
1842 * WPA-PSK: iterate through possible PSKs and select the one matching
1843 * the packet */
1844 for (;;) {
1845 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001846 pmk = wpa_auth_get_psk(sm->wpa_auth, sm->addr,
1847 sm->p2p_dev_addr, pmk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001848 if (pmk == NULL)
1849 break;
1850 } else
1851 pmk = sm->PMK;
1852
1853 wpa_derive_ptk(sm, pmk, &PTK);
1854
1855 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
1856 sm->last_rx_eapol_key_len) == 0) {
1857 ok = 1;
1858 break;
1859 }
1860
1861 if (!wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt))
1862 break;
1863 }
1864
1865 if (!ok) {
1866 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1867 "invalid MIC in msg 2/4 of 4-Way Handshake");
1868 return;
1869 }
1870
1871#ifdef CONFIG_IEEE80211R
1872 if (sm->wpa == WPA_VERSION_WPA2 && wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
1873 /*
1874 * Verify that PMKR1Name from EAPOL-Key message 2/4 matches
1875 * with the value we derived.
1876 */
1877 if (os_memcmp(sm->sup_pmk_r1_name, sm->pmk_r1_name,
1878 WPA_PMK_NAME_LEN) != 0) {
1879 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
1880 "PMKR1Name mismatch in FT 4-way "
1881 "handshake");
1882 wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
1883 "Supplicant",
1884 sm->sup_pmk_r1_name, WPA_PMK_NAME_LEN);
1885 wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
1886 sm->pmk_r1_name, WPA_PMK_NAME_LEN);
1887 return;
1888 }
1889 }
1890#endif /* CONFIG_IEEE80211R */
1891
1892 sm->pending_1_of_4_timeout = 0;
1893 eloop_cancel_timeout(wpa_send_eapol_timeout, sm->wpa_auth, sm);
1894
1895 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
1896 /* PSK may have changed from the previous choice, so update
1897 * state machine data based on whatever PSK was selected here.
1898 */
1899 os_memcpy(sm->PMK, pmk, PMK_LEN);
1900 }
1901
1902 sm->MICVerified = TRUE;
1903
1904 os_memcpy(&sm->PTK, &PTK, sizeof(PTK));
1905 sm->PTK_valid = TRUE;
1906}
1907
1908
1909SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
1910{
1911 SM_ENTRY_MA(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
1912 sm->TimeoutCtr = 0;
1913}
1914
1915
1916#ifdef CONFIG_IEEE80211W
1917
1918static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1919{
1920 if (sm->mgmt_frame_prot) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001921 size_t len;
1922 len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
1923 return 2 + RSN_SELECTOR_LEN + WPA_IGTK_KDE_PREFIX_LEN + len;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001924 }
1925
1926 return 0;
1927}
1928
1929
1930static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1931{
1932 struct wpa_igtk_kde igtk;
1933 struct wpa_group *gsm = sm->group;
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001934 u8 rsc[WPA_KEY_RSC_LEN];
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001935 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001936
1937 if (!sm->mgmt_frame_prot)
1938 return pos;
1939
1940 igtk.keyid[0] = gsm->GN_igtk;
1941 igtk.keyid[1] = 0;
1942 if (gsm->wpa_group_state != WPA_GROUP_SETKEYSDONE ||
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001943 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, rsc) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001944 os_memset(igtk.pn, 0, sizeof(igtk.pn));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001945 else
1946 os_memcpy(igtk.pn, rsc, sizeof(igtk.pn));
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001947 os_memcpy(igtk.igtk, gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001948 if (sm->wpa_auth->conf.disable_gtk) {
1949 /*
1950 * Provide unique random IGTK to each STA to prevent use of
1951 * IGTK in the BSS.
1952 */
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001953 if (random_get_bytes(igtk.igtk, len) < 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001954 return pos;
1955 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001956 pos = wpa_add_kde(pos, RSN_KEY_DATA_IGTK,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07001957 (const u8 *) &igtk, WPA_IGTK_KDE_PREFIX_LEN + len,
1958 NULL, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001959
1960 return pos;
1961}
1962
1963#else /* CONFIG_IEEE80211W */
1964
1965static int ieee80211w_kde_len(struct wpa_state_machine *sm)
1966{
1967 return 0;
1968}
1969
1970
1971static u8 * ieee80211w_kde_add(struct wpa_state_machine *sm, u8 *pos)
1972{
1973 return pos;
1974}
1975
1976#endif /* CONFIG_IEEE80211W */
1977
1978
1979SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
1980{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001981 u8 rsc[WPA_KEY_RSC_LEN], *_rsc, *gtk, *kde, *pos, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001982 size_t gtk_len, kde_len;
1983 struct wpa_group *gsm = sm->group;
1984 u8 *wpa_ie;
1985 int wpa_ie_len, secure, keyidx, encr = 0;
1986
1987 SM_ENTRY_MA(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
1988 sm->TimeoutEvt = FALSE;
1989
1990 sm->TimeoutCtr++;
1991 if (sm->TimeoutCtr > (int) dot11RSNAConfigPairwiseUpdateCount) {
1992 /* No point in sending the EAPOL-Key - we will disconnect
1993 * immediately following this. */
1994 return;
1995 }
1996
1997 /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, [MDIE],
1998 GTK[GN], IGTK, [FTIE], [TIE * 2])
1999 */
2000 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2001 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2002 /* If FT is used, wpa_auth->wpa_ie includes both RSNIE and MDIE */
2003 wpa_ie = sm->wpa_auth->wpa_ie;
2004 wpa_ie_len = sm->wpa_auth->wpa_ie_len;
2005 if (sm->wpa == WPA_VERSION_WPA &&
2006 (sm->wpa_auth->conf.wpa & WPA_PROTO_RSN) &&
2007 wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2008 /* WPA-only STA, remove RSN IE */
2009 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2010 wpa_ie_len = wpa_ie[1] + 2;
2011 }
2012 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2013 "sending 3/4 msg of 4-Way Handshake");
2014 if (sm->wpa == WPA_VERSION_WPA2) {
2015 /* WPA2 send GTK in the 4-way handshake */
2016 secure = 1;
2017 gtk = gsm->GTK[gsm->GN - 1];
2018 gtk_len = gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002019 if (sm->wpa_auth->conf.disable_gtk) {
2020 /*
2021 * Provide unique random GTK to each STA to prevent use
2022 * of GTK in the BSS.
2023 */
2024 if (random_get_bytes(dummy_gtk, gtk_len) < 0)
2025 return;
2026 gtk = dummy_gtk;
2027 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002028 keyidx = gsm->GN;
2029 _rsc = rsc;
2030 encr = 1;
2031 } else {
2032 /* WPA does not include GTK in msg 3/4 */
2033 secure = 0;
2034 gtk = NULL;
2035 gtk_len = 0;
2036 keyidx = 0;
2037 _rsc = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002038 if (sm->rx_eapol_key_secure) {
2039 /*
2040 * It looks like Windows 7 supplicant tries to use
2041 * Secure bit in msg 2/4 after having reported Michael
2042 * MIC failure and it then rejects the 4-way handshake
2043 * if msg 3/4 does not set Secure bit. Work around this
2044 * by setting the Secure bit here even in the case of
2045 * WPA if the supplicant used it first.
2046 */
2047 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2048 "STA used Secure bit in WPA msg 2/4 - "
2049 "set Secure for 3/4 as workaround");
2050 secure = 1;
2051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002052 }
2053
2054 kde_len = wpa_ie_len + ieee80211w_kde_len(sm);
2055 if (gtk)
2056 kde_len += 2 + RSN_SELECTOR_LEN + 2 + gtk_len;
2057#ifdef CONFIG_IEEE80211R
2058 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2059 kde_len += 2 + PMKID_LEN; /* PMKR1Name into RSN IE */
2060 kde_len += 300; /* FTIE + 2 * TIE */
2061 }
2062#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002063#ifdef CONFIG_P2P
2064 if (WPA_GET_BE32(sm->ip_addr) > 0)
2065 kde_len += 2 + RSN_SELECTOR_LEN + 3 * 4;
2066#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067 kde = os_malloc(kde_len);
2068 if (kde == NULL)
2069 return;
2070
2071 pos = kde;
2072 os_memcpy(pos, wpa_ie, wpa_ie_len);
2073 pos += wpa_ie_len;
2074#ifdef CONFIG_IEEE80211R
2075 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2076 int res = wpa_insert_pmkid(kde, pos - kde, sm->pmk_r1_name);
2077 if (res < 0) {
2078 wpa_printf(MSG_ERROR, "FT: Failed to insert "
2079 "PMKR1Name into RSN IE in EAPOL-Key data");
2080 os_free(kde);
2081 return;
2082 }
2083 pos += res;
2084 }
2085#endif /* CONFIG_IEEE80211R */
2086 if (gtk) {
2087 u8 hdr[2];
2088 hdr[0] = keyidx & 0x03;
2089 hdr[1] = 0;
2090 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
2091 gtk, gtk_len);
2092 }
2093 pos = ieee80211w_kde_add(sm, pos);
2094
2095#ifdef CONFIG_IEEE80211R
2096 if (wpa_key_mgmt_ft(sm->wpa_key_mgmt)) {
2097 int res;
2098 struct wpa_auth_config *conf;
2099
2100 conf = &sm->wpa_auth->conf;
2101 res = wpa_write_ftie(conf, conf->r0_key_holder,
2102 conf->r0_key_holder_len,
2103 NULL, NULL, pos, kde + kde_len - pos,
2104 NULL, 0);
2105 if (res < 0) {
2106 wpa_printf(MSG_ERROR, "FT: Failed to insert FTIE "
2107 "into EAPOL-Key Key Data");
2108 os_free(kde);
2109 return;
2110 }
2111 pos += res;
2112
2113 /* TIE[ReassociationDeadline] (TU) */
2114 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2115 *pos++ = 5;
2116 *pos++ = WLAN_TIMEOUT_REASSOC_DEADLINE;
2117 WPA_PUT_LE32(pos, conf->reassociation_deadline);
2118 pos += 4;
2119
2120 /* TIE[KeyLifetime] (seconds) */
2121 *pos++ = WLAN_EID_TIMEOUT_INTERVAL;
2122 *pos++ = 5;
2123 *pos++ = WLAN_TIMEOUT_KEY_LIFETIME;
2124 WPA_PUT_LE32(pos, conf->r0_key_lifetime * 60);
2125 pos += 4;
2126 }
2127#endif /* CONFIG_IEEE80211R */
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002128#ifdef CONFIG_P2P
2129 if (WPA_GET_BE32(sm->ip_addr) > 0) {
2130 u8 addr[3 * 4];
2131 os_memcpy(addr, sm->ip_addr, 4);
2132 os_memcpy(addr + 4, sm->wpa_auth->conf.ip_addr_mask, 4);
2133 os_memcpy(addr + 8, sm->wpa_auth->conf.ip_addr_go, 4);
2134 pos = wpa_add_kde(pos, WFA_KEY_DATA_IP_ADDR_ALLOC,
2135 addr, sizeof(addr), NULL, 0);
2136 }
2137#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002138
2139 wpa_send_eapol(sm->wpa_auth, sm,
2140 (secure ? WPA_KEY_INFO_SECURE : 0) | WPA_KEY_INFO_MIC |
2141 WPA_KEY_INFO_ACK | WPA_KEY_INFO_INSTALL |
2142 WPA_KEY_INFO_KEY_TYPE,
2143 _rsc, sm->ANonce, kde, pos - kde, keyidx, encr);
2144 os_free(kde);
2145}
2146
2147
2148SM_STATE(WPA_PTK, PTKINITDONE)
2149{
2150 SM_ENTRY_MA(WPA_PTK, PTKINITDONE, wpa_ptk);
2151 sm->EAPOLKeyReceived = FALSE;
2152 if (sm->Pair) {
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002153 enum wpa_alg alg = wpa_cipher_to_alg(sm->pairwise);
2154 int klen = wpa_cipher_key_len(sm->pairwise);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002155 if (wpa_auth_set_key(sm->wpa_auth, 0, alg, sm->addr, 0,
2156 sm->PTK.tk1, klen)) {
2157 wpa_sta_disconnect(sm->wpa_auth, sm->addr);
2158 return;
2159 }
2160 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2161 sm->pairwise_set = TRUE;
2162
2163 if (sm->wpa_auth->conf.wpa_ptk_rekey) {
2164 eloop_cancel_timeout(wpa_rekey_ptk, sm->wpa_auth, sm);
2165 eloop_register_timeout(sm->wpa_auth->conf.
2166 wpa_ptk_rekey, 0, wpa_rekey_ptk,
2167 sm->wpa_auth, sm);
2168 }
2169
2170 if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)) {
2171 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2172 WPA_EAPOL_authorized, 1);
2173 }
2174 }
2175
2176 if (0 /* IBSS == TRUE */) {
2177 sm->keycount++;
2178 if (sm->keycount == 2) {
2179 wpa_auth_set_eapol(sm->wpa_auth, sm->addr,
2180 WPA_EAPOL_portValid, 1);
2181 }
2182 } else {
2183 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_portValid,
2184 1);
2185 }
2186 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyAvailable, 0);
2187 wpa_auth_set_eapol(sm->wpa_auth, sm->addr, WPA_EAPOL_keyDone, 1);
2188 if (sm->wpa == WPA_VERSION_WPA)
2189 sm->PInitAKeys = TRUE;
2190 else
2191 sm->has_GTK = TRUE;
2192 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2193 "pairwise key handshake completed (%s)",
2194 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2195
2196#ifdef CONFIG_IEEE80211R
2197 wpa_ft_push_pmk_r1(sm->wpa_auth, sm->addr);
2198#endif /* CONFIG_IEEE80211R */
2199}
2200
2201
2202SM_STEP(WPA_PTK)
2203{
2204 struct wpa_authenticator *wpa_auth = sm->wpa_auth;
2205
2206 if (sm->Init)
2207 SM_ENTER(WPA_PTK, INITIALIZE);
2208 else if (sm->Disconnect
2209 /* || FIX: dot11RSNAConfigSALifetime timeout */) {
2210 wpa_auth_logger(wpa_auth, sm->addr, LOGGER_DEBUG,
2211 "WPA_PTK: sm->Disconnect");
2212 SM_ENTER(WPA_PTK, DISCONNECT);
2213 }
2214 else if (sm->DeauthenticationRequest)
2215 SM_ENTER(WPA_PTK, DISCONNECTED);
2216 else if (sm->AuthenticationRequest)
2217 SM_ENTER(WPA_PTK, AUTHENTICATION);
2218 else if (sm->ReAuthenticationRequest)
2219 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2220 else if (sm->PTKRequest)
2221 SM_ENTER(WPA_PTK, PTKSTART);
2222 else switch (sm->wpa_ptk_state) {
2223 case WPA_PTK_INITIALIZE:
2224 break;
2225 case WPA_PTK_DISCONNECT:
2226 SM_ENTER(WPA_PTK, DISCONNECTED);
2227 break;
2228 case WPA_PTK_DISCONNECTED:
2229 SM_ENTER(WPA_PTK, INITIALIZE);
2230 break;
2231 case WPA_PTK_AUTHENTICATION:
2232 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2233 break;
2234 case WPA_PTK_AUTHENTICATION2:
2235 if (wpa_key_mgmt_wpa_ieee8021x(sm->wpa_key_mgmt) &&
2236 wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2237 WPA_EAPOL_keyRun) > 0)
2238 SM_ENTER(WPA_PTK, INITPMK);
2239 else if (wpa_key_mgmt_wpa_psk(sm->wpa_key_mgmt)
2240 /* FIX: && 802.1X::keyRun */)
2241 SM_ENTER(WPA_PTK, INITPSK);
2242 break;
2243 case WPA_PTK_INITPMK:
2244 if (wpa_auth_get_eapol(sm->wpa_auth, sm->addr,
2245 WPA_EAPOL_keyAvailable) > 0)
2246 SM_ENTER(WPA_PTK, PTKSTART);
2247 else {
2248 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2249 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2250 "INITPMK - keyAvailable = false");
2251 SM_ENTER(WPA_PTK, DISCONNECT);
2252 }
2253 break;
2254 case WPA_PTK_INITPSK:
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07002255 if (wpa_auth_get_psk(sm->wpa_auth, sm->addr, sm->p2p_dev_addr,
2256 NULL))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 SM_ENTER(WPA_PTK, PTKSTART);
2258 else {
2259 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2260 "no PSK configured for the STA");
2261 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2262 SM_ENTER(WPA_PTK, DISCONNECT);
2263 }
2264 break;
2265 case WPA_PTK_PTKSTART:
2266 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2267 sm->EAPOLKeyPairwise)
2268 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2269 else if (sm->TimeoutCtr >
2270 (int) dot11RSNAConfigPairwiseUpdateCount) {
2271 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2272 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2273 "PTKSTART: Retry limit %d reached",
2274 dot11RSNAConfigPairwiseUpdateCount);
2275 SM_ENTER(WPA_PTK, DISCONNECT);
2276 } else if (sm->TimeoutEvt)
2277 SM_ENTER(WPA_PTK, PTKSTART);
2278 break;
2279 case WPA_PTK_PTKCALCNEGOTIATING:
2280 if (sm->MICVerified)
2281 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2282 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2283 sm->EAPOLKeyPairwise)
2284 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2285 else if (sm->TimeoutEvt)
2286 SM_ENTER(WPA_PTK, PTKSTART);
2287 break;
2288 case WPA_PTK_PTKCALCNEGOTIATING2:
2289 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2290 break;
2291 case WPA_PTK_PTKINITNEGOTIATING:
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002292 if (sm->update_snonce)
2293 SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2294 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2295 sm->EAPOLKeyPairwise && sm->MICVerified)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002296 SM_ENTER(WPA_PTK, PTKINITDONE);
2297 else if (sm->TimeoutCtr >
2298 (int) dot11RSNAConfigPairwiseUpdateCount) {
2299 wpa_auth->dot11RSNA4WayHandshakeFailures++;
2300 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2301 "PTKINITNEGOTIATING: Retry limit %d "
2302 "reached",
2303 dot11RSNAConfigPairwiseUpdateCount);
2304 SM_ENTER(WPA_PTK, DISCONNECT);
2305 } else if (sm->TimeoutEvt)
2306 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2307 break;
2308 case WPA_PTK_PTKINITDONE:
2309 break;
2310 }
2311}
2312
2313
2314SM_STATE(WPA_PTK_GROUP, IDLE)
2315{
2316 SM_ENTRY_MA(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2317 if (sm->Init) {
2318 /* Init flag is not cleared here, so avoid busy
2319 * loop by claiming nothing changed. */
2320 sm->changed = FALSE;
2321 }
2322 sm->GTimeoutCtr = 0;
2323}
2324
2325
2326SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2327{
2328 u8 rsc[WPA_KEY_RSC_LEN];
2329 struct wpa_group *gsm = sm->group;
2330 u8 *kde, *pos, hdr[2];
2331 size_t kde_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002332 u8 *gtk, dummy_gtk[32];
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002333
2334 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2335
2336 sm->GTimeoutCtr++;
2337 if (sm->GTimeoutCtr > (int) dot11RSNAConfigGroupUpdateCount) {
2338 /* No point in sending the EAPOL-Key - we will disconnect
2339 * immediately following this. */
2340 return;
2341 }
2342
2343 if (sm->wpa == WPA_VERSION_WPA)
2344 sm->PInitAKeys = FALSE;
2345 sm->TimeoutEvt = FALSE;
2346 /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2347 os_memset(rsc, 0, WPA_KEY_RSC_LEN);
2348 if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2349 wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, rsc);
2350 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2351 "sending 1/2 msg of Group Key Handshake");
2352
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002353 gtk = gsm->GTK[gsm->GN - 1];
2354 if (sm->wpa_auth->conf.disable_gtk) {
2355 /*
2356 * Provide unique random GTK to each STA to prevent use
2357 * of GTK in the BSS.
2358 */
2359 if (random_get_bytes(dummy_gtk, gsm->GTK_len) < 0)
2360 return;
2361 gtk = dummy_gtk;
2362 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002363 if (sm->wpa == WPA_VERSION_WPA2) {
2364 kde_len = 2 + RSN_SELECTOR_LEN + 2 + gsm->GTK_len +
2365 ieee80211w_kde_len(sm);
2366 kde = os_malloc(kde_len);
2367 if (kde == NULL)
2368 return;
2369
2370 pos = kde;
2371 hdr[0] = gsm->GN & 0x03;
2372 hdr[1] = 0;
2373 pos = wpa_add_kde(pos, RSN_KEY_DATA_GROUPKEY, hdr, 2,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002374 gtk, gsm->GTK_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002375 pos = ieee80211w_kde_add(sm, pos);
2376 } else {
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002377 kde = gtk;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002378 pos = kde + gsm->GTK_len;
2379 }
2380
2381 wpa_send_eapol(sm->wpa_auth, sm,
2382 WPA_KEY_INFO_SECURE | WPA_KEY_INFO_MIC |
2383 WPA_KEY_INFO_ACK |
2384 (!sm->Pair ? WPA_KEY_INFO_INSTALL : 0),
2385 rsc, gsm->GNonce, kde, pos - kde, gsm->GN, 1);
2386 if (sm->wpa == WPA_VERSION_WPA2)
2387 os_free(kde);
2388}
2389
2390
2391SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2392{
2393 SM_ENTRY_MA(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2394 sm->EAPOLKeyReceived = FALSE;
2395 if (sm->GUpdateStationKeys)
2396 sm->group->GKeyDoneStations--;
2397 sm->GUpdateStationKeys = FALSE;
2398 sm->GTimeoutCtr = 0;
2399 /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2400 wpa_auth_vlogger(sm->wpa_auth, sm->addr, LOGGER_INFO,
2401 "group key handshake completed (%s)",
2402 sm->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2403 sm->has_GTK = TRUE;
2404}
2405
2406
2407SM_STATE(WPA_PTK_GROUP, KEYERROR)
2408{
2409 SM_ENTRY_MA(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2410 if (sm->GUpdateStationKeys)
2411 sm->group->GKeyDoneStations--;
2412 sm->GUpdateStationKeys = FALSE;
2413 sm->Disconnect = TRUE;
2414}
2415
2416
2417SM_STEP(WPA_PTK_GROUP)
2418{
2419 if (sm->Init || sm->PtkGroupInit) {
2420 SM_ENTER(WPA_PTK_GROUP, IDLE);
2421 sm->PtkGroupInit = FALSE;
2422 } else switch (sm->wpa_ptk_group_state) {
2423 case WPA_PTK_GROUP_IDLE:
2424 if (sm->GUpdateStationKeys ||
2425 (sm->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2426 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2427 break;
2428 case WPA_PTK_GROUP_REKEYNEGOTIATING:
2429 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2430 !sm->EAPOLKeyPairwise && sm->MICVerified)
2431 SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2432 else if (sm->GTimeoutCtr >
2433 (int) dot11RSNAConfigGroupUpdateCount)
2434 SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2435 else if (sm->TimeoutEvt)
2436 SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2437 break;
2438 case WPA_PTK_GROUP_KEYERROR:
2439 SM_ENTER(WPA_PTK_GROUP, IDLE);
2440 break;
2441 case WPA_PTK_GROUP_REKEYESTABLISHED:
2442 SM_ENTER(WPA_PTK_GROUP, IDLE);
2443 break;
2444 }
2445}
2446
2447
2448static int wpa_gtk_update(struct wpa_authenticator *wpa_auth,
2449 struct wpa_group *group)
2450{
2451 int ret = 0;
2452
2453 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2454 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2455 if (wpa_gmk_to_gtk(group->GMK, "Group key expansion",
2456 wpa_auth->addr, group->GNonce,
2457 group->GTK[group->GN - 1], group->GTK_len) < 0)
2458 ret = -1;
2459 wpa_hexdump_key(MSG_DEBUG, "GTK",
2460 group->GTK[group->GN - 1], group->GTK_len);
2461
2462#ifdef CONFIG_IEEE80211W
2463 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002464 size_t len;
2465 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002466 os_memcpy(group->GNonce, group->Counter, WPA_NONCE_LEN);
2467 inc_byte_array(group->Counter, WPA_NONCE_LEN);
2468 if (wpa_gmk_to_gtk(group->GMK, "IGTK key expansion",
2469 wpa_auth->addr, group->GNonce,
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002470 group->IGTK[group->GN_igtk - 4], len) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 ret = -1;
2472 wpa_hexdump_key(MSG_DEBUG, "IGTK",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002473 group->IGTK[group->GN_igtk - 4], len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002474 }
2475#endif /* CONFIG_IEEE80211W */
2476
2477 return ret;
2478}
2479
2480
2481static void wpa_group_gtk_init(struct wpa_authenticator *wpa_auth,
2482 struct wpa_group *group)
2483{
2484 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2485 "GTK_INIT (VLAN-ID %d)", group->vlan_id);
2486 group->changed = FALSE; /* GInit is not cleared here; avoid loop */
2487 group->wpa_group_state = WPA_GROUP_GTK_INIT;
2488
2489 /* GTK[0..N] = 0 */
2490 os_memset(group->GTK, 0, sizeof(group->GTK));
2491 group->GN = 1;
2492 group->GM = 2;
2493#ifdef CONFIG_IEEE80211W
2494 group->GN_igtk = 4;
2495 group->GM_igtk = 5;
2496#endif /* CONFIG_IEEE80211W */
2497 /* GTK[GN] = CalcGTK() */
2498 wpa_gtk_update(wpa_auth, group);
2499}
2500
2501
2502static int wpa_group_update_sta(struct wpa_state_machine *sm, void *ctx)
2503{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002504 if (ctx != NULL && ctx != sm->group)
2505 return 0;
2506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002507 if (sm->wpa_ptk_state != WPA_PTK_PTKINITDONE) {
2508 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2509 "Not in PTKINITDONE; skip Group Key update");
2510 sm->GUpdateStationKeys = FALSE;
2511 return 0;
2512 }
2513 if (sm->GUpdateStationKeys) {
2514 /*
2515 * This should not really happen, so add a debug log entry.
2516 * Since we clear the GKeyDoneStations before the loop, the
2517 * station needs to be counted here anyway.
2518 */
2519 wpa_auth_logger(sm->wpa_auth, sm->addr, LOGGER_DEBUG,
2520 "GUpdateStationKeys was already set when "
2521 "marking station for GTK rekeying");
2522 }
2523
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002524 /* Do not rekey GTK/IGTK when STA is in WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002525 if (sm->is_wnmsleep)
2526 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002527
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002528 sm->group->GKeyDoneStations++;
2529 sm->GUpdateStationKeys = TRUE;
2530
2531 wpa_sm_step(sm);
2532 return 0;
2533}
2534
2535
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002536#ifdef CONFIG_WNM
2537/* update GTK when exiting WNM-Sleep Mode */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002538void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm)
2539{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002540 if (sm == NULL || sm->is_wnmsleep)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002541 return;
2542
2543 wpa_group_update_sta(sm, NULL);
2544}
2545
2546
2547void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag)
2548{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002549 if (sm)
2550 sm->is_wnmsleep = !!flag;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002551}
2552
2553
2554int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2555{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002556 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002557 u8 *start = pos;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002558
2559 /*
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002560 * GTK subelement:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002561 * Sub-elem ID[1] | Length[1] | Key Info[2] | Key Length[1] | RSC[8] |
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002562 * Key[5..32]
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002563 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002564 *pos++ = WNM_SLEEP_SUBELEM_GTK;
2565 *pos++ = 11 + gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002566 /* Key ID in B0-B1 of Key Info */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002567 WPA_PUT_LE16(pos, gsm->GN & 0x03);
2568 pos += 2;
2569 *pos++ = gsm->GTK_len;
2570 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002571 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002572 pos += 8;
2573 os_memcpy(pos, gsm->GTK[gsm->GN - 1], gsm->GTK_len);
2574 pos += gsm->GTK_len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002575
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002576 wpa_printf(MSG_DEBUG, "WNM: GTK Key ID %u in WNM-Sleep Mode exit",
2577 gsm->GN);
2578 wpa_hexdump_key(MSG_DEBUG, "WNM: GTK in WNM-Sleep Mode exit",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002579 gsm->GTK[gsm->GN - 1], gsm->GTK_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002580
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002581 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002582}
2583
2584
2585#ifdef CONFIG_IEEE80211W
2586int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos)
2587{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002588 struct wpa_group *gsm = sm->group;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002589 u8 *start = pos;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002590 size_t len = wpa_cipher_key_len(sm->wpa_auth->conf.group_mgmt_cipher);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002591
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002592 /*
2593 * IGTK subelement:
2594 * Sub-elem ID[1] | Length[1] | KeyID[2] | PN[6] | Key[16]
2595 */
2596 *pos++ = WNM_SLEEP_SUBELEM_IGTK;
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002597 *pos++ = 2 + 6 + len;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002598 WPA_PUT_LE16(pos, gsm->GN_igtk);
2599 pos += 2;
2600 if (wpa_auth_get_seqnum(sm->wpa_auth, NULL, gsm->GN_igtk, pos) != 0)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002601 return 0;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002602 pos += 6;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002603
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002604 os_memcpy(pos, gsm->IGTK[gsm->GN_igtk - 4], len);
2605 pos += len;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002606
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002607 wpa_printf(MSG_DEBUG, "WNM: IGTK Key ID %u in WNM-Sleep Mode exit",
2608 gsm->GN_igtk);
2609 wpa_hexdump_key(MSG_DEBUG, "WNM: IGTK in WNM-Sleep Mode exit",
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002610 gsm->IGTK[gsm->GN_igtk - 4], len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002611
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002612 return pos - start;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002613}
2614#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002615#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002616
2617
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002618static void wpa_group_setkeys(struct wpa_authenticator *wpa_auth,
2619 struct wpa_group *group)
2620{
2621 int tmp;
2622
2623 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2624 "SETKEYS (VLAN-ID %d)", group->vlan_id);
2625 group->changed = TRUE;
2626 group->wpa_group_state = WPA_GROUP_SETKEYS;
2627 group->GTKReKey = FALSE;
2628 tmp = group->GM;
2629 group->GM = group->GN;
2630 group->GN = tmp;
2631#ifdef CONFIG_IEEE80211W
2632 tmp = group->GM_igtk;
2633 group->GM_igtk = group->GN_igtk;
2634 group->GN_igtk = tmp;
2635#endif /* CONFIG_IEEE80211W */
2636 /* "GKeyDoneStations = GNoStations" is done in more robust way by
2637 * counting the STAs that are marked with GUpdateStationKeys instead of
2638 * including all STAs that could be in not-yet-completed state. */
2639 wpa_gtk_update(wpa_auth, group);
2640
2641 if (group->GKeyDoneStations) {
2642 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: Unexpected "
2643 "GKeyDoneStations=%d when starting new GTK rekey",
2644 group->GKeyDoneStations);
2645 group->GKeyDoneStations = 0;
2646 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002647 wpa_auth_for_each_sta(wpa_auth, wpa_group_update_sta, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002648 wpa_printf(MSG_DEBUG, "wpa_group_setkeys: GKeyDoneStations=%d",
2649 group->GKeyDoneStations);
2650}
2651
2652
2653static int wpa_group_config_group_keys(struct wpa_authenticator *wpa_auth,
2654 struct wpa_group *group)
2655{
2656 int ret = 0;
2657
2658 if (wpa_auth_set_key(wpa_auth, group->vlan_id,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002659 wpa_cipher_to_alg(wpa_auth->conf.wpa_group),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002660 broadcast_ether_addr, group->GN,
2661 group->GTK[group->GN - 1], group->GTK_len) < 0)
2662 ret = -1;
2663
2664#ifdef CONFIG_IEEE80211W
Dmitry Shmidtb36ed7c2014-03-17 10:57:26 -07002665 if (wpa_auth->conf.ieee80211w != NO_MGMT_FRAME_PROTECTION) {
2666 enum wpa_alg alg;
2667 size_t len;
2668
2669 alg = wpa_cipher_to_alg(wpa_auth->conf.group_mgmt_cipher);
2670 len = wpa_cipher_key_len(wpa_auth->conf.group_mgmt_cipher);
2671
2672 if (ret == 0 &&
2673 wpa_auth_set_key(wpa_auth, group->vlan_id, alg,
2674 broadcast_ether_addr, group->GN_igtk,
2675 group->IGTK[group->GN_igtk - 4], len) < 0)
2676 ret = -1;
2677 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002678#endif /* CONFIG_IEEE80211W */
2679
2680 return ret;
2681}
2682
2683
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002684static int wpa_group_disconnect_cb(struct wpa_state_machine *sm, void *ctx)
2685{
2686 if (sm->group == ctx) {
2687 wpa_printf(MSG_DEBUG, "WPA: Mark STA " MACSTR
2688 " for discconnection due to fatal failure",
2689 MAC2STR(sm->addr));
2690 sm->Disconnect = TRUE;
2691 }
2692
2693 return 0;
2694}
2695
2696
2697static void wpa_group_fatal_failure(struct wpa_authenticator *wpa_auth,
2698 struct wpa_group *group)
2699{
2700 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state FATAL_FAILURE");
2701 group->changed = TRUE;
2702 group->wpa_group_state = WPA_GROUP_FATAL_FAILURE;
2703 wpa_auth_for_each_sta(wpa_auth, wpa_group_disconnect_cb, group);
2704}
2705
2706
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002707static int wpa_group_setkeysdone(struct wpa_authenticator *wpa_auth,
2708 struct wpa_group *group)
2709{
2710 wpa_printf(MSG_DEBUG, "WPA: group state machine entering state "
2711 "SETKEYSDONE (VLAN-ID %d)", group->vlan_id);
2712 group->changed = TRUE;
2713 group->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2714
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002715 if (wpa_group_config_group_keys(wpa_auth, group) < 0) {
2716 wpa_group_fatal_failure(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002717 return -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002718 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002719
2720 return 0;
2721}
2722
2723
2724static void wpa_group_sm_step(struct wpa_authenticator *wpa_auth,
2725 struct wpa_group *group)
2726{
2727 if (group->GInit) {
2728 wpa_group_gtk_init(wpa_auth, group);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002729 } else if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE) {
2730 /* Do not allow group operations */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002731 } else if (group->wpa_group_state == WPA_GROUP_GTK_INIT &&
2732 group->GTKAuthenticator) {
2733 wpa_group_setkeysdone(wpa_auth, group);
2734 } else if (group->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2735 group->GTKReKey) {
2736 wpa_group_setkeys(wpa_auth, group);
2737 } else if (group->wpa_group_state == WPA_GROUP_SETKEYS) {
2738 if (group->GKeyDoneStations == 0)
2739 wpa_group_setkeysdone(wpa_auth, group);
2740 else if (group->GTKReKey)
2741 wpa_group_setkeys(wpa_auth, group);
2742 }
2743}
2744
2745
2746static int wpa_sm_step(struct wpa_state_machine *sm)
2747{
2748 if (sm == NULL)
2749 return 0;
2750
2751 if (sm->in_step_loop) {
2752 /* This should not happen, but if it does, make sure we do not
2753 * end up freeing the state machine too early by exiting the
2754 * recursive call. */
2755 wpa_printf(MSG_ERROR, "WPA: wpa_sm_step() called recursively");
2756 return 0;
2757 }
2758
2759 sm->in_step_loop = 1;
2760 do {
2761 if (sm->pending_deinit)
2762 break;
2763
2764 sm->changed = FALSE;
2765 sm->wpa_auth->group->changed = FALSE;
2766
2767 SM_STEP_RUN(WPA_PTK);
2768 if (sm->pending_deinit)
2769 break;
2770 SM_STEP_RUN(WPA_PTK_GROUP);
2771 if (sm->pending_deinit)
2772 break;
2773 wpa_group_sm_step(sm->wpa_auth, sm->group);
2774 } while (sm->changed || sm->wpa_auth->group->changed);
2775 sm->in_step_loop = 0;
2776
2777 if (sm->pending_deinit) {
2778 wpa_printf(MSG_DEBUG, "WPA: Completing pending STA state "
2779 "machine deinit for " MACSTR, MAC2STR(sm->addr));
2780 wpa_free_sta_sm(sm);
2781 return 1;
2782 }
2783 return 0;
2784}
2785
2786
2787static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2788{
2789 struct wpa_state_machine *sm = eloop_ctx;
2790 wpa_sm_step(sm);
2791}
2792
2793
2794void wpa_auth_sm_notify(struct wpa_state_machine *sm)
2795{
2796 if (sm == NULL)
2797 return;
2798 eloop_register_timeout(0, 0, wpa_sm_call_step, sm, NULL);
2799}
2800
2801
2802void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth)
2803{
2804 int tmp, i;
2805 struct wpa_group *group;
2806
2807 if (wpa_auth == NULL)
2808 return;
2809
2810 group = wpa_auth->group;
2811
2812 for (i = 0; i < 2; i++) {
2813 tmp = group->GM;
2814 group->GM = group->GN;
2815 group->GN = tmp;
2816#ifdef CONFIG_IEEE80211W
2817 tmp = group->GM_igtk;
2818 group->GM_igtk = group->GN_igtk;
2819 group->GN_igtk = tmp;
2820#endif /* CONFIG_IEEE80211W */
2821 wpa_gtk_update(wpa_auth, group);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002822 wpa_group_config_group_keys(wpa_auth, group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002823 }
2824}
2825
2826
2827static const char * wpa_bool_txt(int bool)
2828{
2829 return bool ? "TRUE" : "FALSE";
2830}
2831
2832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833#define RSN_SUITE "%02x-%02x-%02x-%d"
2834#define RSN_SUITE_ARG(s) \
2835((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
2836
2837int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen)
2838{
2839 int len = 0, ret;
2840 char pmkid_txt[PMKID_LEN * 2 + 1];
2841#ifdef CONFIG_RSN_PREAUTH
2842 const int preauth = 1;
2843#else /* CONFIG_RSN_PREAUTH */
2844 const int preauth = 0;
2845#endif /* CONFIG_RSN_PREAUTH */
2846
2847 if (wpa_auth == NULL)
2848 return len;
2849
2850 ret = os_snprintf(buf + len, buflen - len,
2851 "dot11RSNAOptionImplemented=TRUE\n"
2852 "dot11RSNAPreauthenticationImplemented=%s\n"
2853 "dot11RSNAEnabled=%s\n"
2854 "dot11RSNAPreauthenticationEnabled=%s\n",
2855 wpa_bool_txt(preauth),
2856 wpa_bool_txt(wpa_auth->conf.wpa & WPA_PROTO_RSN),
2857 wpa_bool_txt(wpa_auth->conf.rsn_preauth));
2858 if (ret < 0 || (size_t) ret >= buflen - len)
2859 return len;
2860 len += ret;
2861
2862 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
2863 wpa_auth->dot11RSNAPMKIDUsed, PMKID_LEN);
2864
2865 ret = os_snprintf(
2866 buf + len, buflen - len,
2867 "dot11RSNAConfigVersion=%u\n"
2868 "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2869 /* FIX: dot11RSNAConfigGroupCipher */
2870 /* FIX: dot11RSNAConfigGroupRekeyMethod */
2871 /* FIX: dot11RSNAConfigGroupRekeyTime */
2872 /* FIX: dot11RSNAConfigGroupRekeyPackets */
2873 "dot11RSNAConfigGroupRekeyStrict=%u\n"
2874 "dot11RSNAConfigGroupUpdateCount=%u\n"
2875 "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2876 "dot11RSNAConfigGroupCipherSize=%u\n"
2877 "dot11RSNAConfigPMKLifetime=%u\n"
2878 "dot11RSNAConfigPMKReauthThreshold=%u\n"
2879 "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2880 "dot11RSNAConfigSATimeout=%u\n"
2881 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2882 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2883 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2884 "dot11RSNAPMKIDUsed=%s\n"
2885 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2886 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2887 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2888 "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2889 "dot11RSNA4WayHandshakeFailures=%u\n"
2890 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2891 RSN_VERSION,
2892 !!wpa_auth->conf.wpa_strict_rekey,
2893 dot11RSNAConfigGroupUpdateCount,
2894 dot11RSNAConfigPairwiseUpdateCount,
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002895 wpa_cipher_key_len(wpa_auth->conf.wpa_group) * 8,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002896 dot11RSNAConfigPMKLifetime,
2897 dot11RSNAConfigPMKReauthThreshold,
2898 dot11RSNAConfigSATimeout,
2899 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteSelected),
2900 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherSelected),
2901 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherSelected),
2902 pmkid_txt,
2903 RSN_SUITE_ARG(wpa_auth->dot11RSNAAuthenticationSuiteRequested),
2904 RSN_SUITE_ARG(wpa_auth->dot11RSNAPairwiseCipherRequested),
2905 RSN_SUITE_ARG(wpa_auth->dot11RSNAGroupCipherRequested),
2906 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2907 wpa_auth->dot11RSNA4WayHandshakeFailures);
2908 if (ret < 0 || (size_t) ret >= buflen - len)
2909 return len;
2910 len += ret;
2911
2912 /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2913 /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2914
2915 /* Private MIB */
2916 ret = os_snprintf(buf + len, buflen - len, "hostapdWPAGroupState=%d\n",
2917 wpa_auth->group->wpa_group_state);
2918 if (ret < 0 || (size_t) ret >= buflen - len)
2919 return len;
2920 len += ret;
2921
2922 return len;
2923}
2924
2925
2926int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen)
2927{
2928 int len = 0, ret;
2929 u32 pairwise = 0;
2930
2931 if (sm == NULL)
2932 return 0;
2933
2934 /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2935
2936 /* dot11RSNAStatsEntry */
2937
Dmitry Shmidt4530cfd2012-09-09 15:20:40 -07002938 pairwise = wpa_cipher_to_suite(sm->wpa == WPA_VERSION_WPA2 ?
2939 WPA_PROTO_RSN : WPA_PROTO_WPA,
2940 sm->pairwise);
2941 if (pairwise == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002942 return 0;
2943
2944 ret = os_snprintf(
2945 buf + len, buflen - len,
2946 /* TODO: dot11RSNAStatsIndex */
2947 "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2948 "dot11RSNAStatsVersion=1\n"
2949 "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2950 /* TODO: dot11RSNAStatsTKIPICVErrors */
2951 "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2952 "dot11RSNAStatsTKIPRemoteMICFailures=%u\n"
2953 /* TODO: dot11RSNAStatsCCMPReplays */
2954 /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2955 /* TODO: dot11RSNAStatsTKIPReplays */,
2956 MAC2STR(sm->addr),
2957 RSN_SUITE_ARG(pairwise),
2958 sm->dot11RSNAStatsTKIPLocalMICFailures,
2959 sm->dot11RSNAStatsTKIPRemoteMICFailures);
2960 if (ret < 0 || (size_t) ret >= buflen - len)
2961 return len;
2962 len += ret;
2963
2964 /* Private MIB */
2965 ret = os_snprintf(buf + len, buflen - len,
2966 "hostapdWPAPTKState=%d\n"
2967 "hostapdWPAPTKGroupState=%d\n",
2968 sm->wpa_ptk_state,
2969 sm->wpa_ptk_group_state);
2970 if (ret < 0 || (size_t) ret >= buflen - len)
2971 return len;
2972 len += ret;
2973
2974 return len;
2975}
2976
2977
2978void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth)
2979{
2980 if (wpa_auth)
2981 wpa_auth->dot11RSNATKIPCounterMeasuresInvoked++;
2982}
2983
2984
2985int wpa_auth_pairwise_set(struct wpa_state_machine *sm)
2986{
2987 return sm && sm->pairwise_set;
2988}
2989
2990
2991int wpa_auth_get_pairwise(struct wpa_state_machine *sm)
2992{
2993 return sm->pairwise;
2994}
2995
2996
2997int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm)
2998{
2999 if (sm == NULL)
3000 return -1;
3001 return sm->wpa_key_mgmt;
3002}
3003
3004
3005int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm)
3006{
3007 if (sm == NULL)
3008 return 0;
3009 return sm->wpa;
3010}
3011
3012
3013int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
3014 struct rsn_pmksa_cache_entry *entry)
3015{
3016 if (sm == NULL || sm->pmksa != entry)
3017 return -1;
3018 sm->pmksa = NULL;
3019 return 0;
3020}
3021
3022
3023struct rsn_pmksa_cache_entry *
3024wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm)
3025{
3026 return sm ? sm->pmksa : NULL;
3027}
3028
3029
3030void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm)
3031{
3032 if (sm)
3033 sm->dot11RSNAStatsTKIPLocalMICFailures++;
3034}
3035
3036
3037const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth, size_t *len)
3038{
3039 if (wpa_auth == NULL)
3040 return NULL;
3041 *len = wpa_auth->wpa_ie_len;
3042 return wpa_auth->wpa_ie;
3043}
3044
3045
3046int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
3047 int session_timeout, struct eapol_state_machine *eapol)
3048{
Dmitry Shmidtc55524a2011-07-07 11:18:38 -07003049 if (sm == NULL || sm->wpa != WPA_VERSION_WPA2 ||
3050 sm->wpa_auth->conf.disable_pmksa_caching)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003051 return -1;
3052
3053 if (pmksa_cache_auth_add(sm->wpa_auth->pmksa, pmk, PMK_LEN,
3054 sm->wpa_auth->addr, sm->addr, session_timeout,
3055 eapol, sm->wpa_key_mgmt))
3056 return 0;
3057
3058 return -1;
3059}
3060
3061
3062int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
3063 const u8 *pmk, size_t len, const u8 *sta_addr,
3064 int session_timeout,
3065 struct eapol_state_machine *eapol)
3066{
3067 if (wpa_auth == NULL)
3068 return -1;
3069
3070 if (pmksa_cache_auth_add(wpa_auth->pmksa, pmk, len, wpa_auth->addr,
3071 sta_addr, session_timeout, eapol,
3072 WPA_KEY_MGMT_IEEE8021X))
3073 return 0;
3074
3075 return -1;
3076}
3077
3078
Dmitry Shmidtf7e0a992013-05-23 11:03:10 -07003079void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
3080 const u8 *sta_addr)
3081{
3082 struct rsn_pmksa_cache_entry *pmksa;
3083
3084 if (wpa_auth == NULL || wpa_auth->pmksa == NULL)
3085 return;
3086 pmksa = pmksa_cache_auth_get(wpa_auth->pmksa, sta_addr, NULL);
3087 if (pmksa) {
3088 wpa_printf(MSG_DEBUG, "WPA: Remove PMKSA cache entry for "
3089 MACSTR " based on request", MAC2STR(sta_addr));
3090 pmksa_cache_free_entry(wpa_auth->pmksa, pmksa);
3091 }
3092}
3093
3094
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003095static struct wpa_group *
3096wpa_auth_add_group(struct wpa_authenticator *wpa_auth, int vlan_id)
3097{
3098 struct wpa_group *group;
3099
3100 if (wpa_auth == NULL || wpa_auth->group == NULL)
3101 return NULL;
3102
3103 wpa_printf(MSG_DEBUG, "WPA: Add group state machine for VLAN-ID %d",
3104 vlan_id);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003105 group = wpa_group_init(wpa_auth, vlan_id, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003106 if (group == NULL)
3107 return NULL;
3108
3109 group->next = wpa_auth->group->next;
3110 wpa_auth->group->next = group;
3111
3112 return group;
3113}
3114
3115
3116int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id)
3117{
3118 struct wpa_group *group;
3119
3120 if (sm == NULL || sm->wpa_auth == NULL)
3121 return 0;
3122
3123 group = sm->wpa_auth->group;
3124 while (group) {
3125 if (group->vlan_id == vlan_id)
3126 break;
3127 group = group->next;
3128 }
3129
3130 if (group == NULL) {
3131 group = wpa_auth_add_group(sm->wpa_auth, vlan_id);
3132 if (group == NULL)
3133 return -1;
3134 }
3135
3136 if (sm->group == group)
3137 return 0;
3138
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003139 if (group->wpa_group_state == WPA_GROUP_FATAL_FAILURE)
3140 return -1;
3141
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003142 wpa_printf(MSG_DEBUG, "WPA: Moving STA " MACSTR " to use group state "
3143 "machine for VLAN ID %d", MAC2STR(sm->addr), vlan_id);
3144
3145 sm->group = group;
3146 return 0;
3147}
3148
3149
3150void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
3151 struct wpa_state_machine *sm, int ack)
3152{
3153 if (wpa_auth == NULL || sm == NULL)
3154 return;
3155 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key TX status for STA " MACSTR
3156 " ack=%d", MAC2STR(sm->addr), ack);
3157 if (sm->pending_1_of_4_timeout && ack) {
3158 /*
3159 * Some deployed supplicant implementations update their SNonce
3160 * for each EAPOL-Key 2/4 message even within the same 4-way
3161 * handshake and then fail to use the first SNonce when
3162 * deriving the PTK. This results in unsuccessful 4-way
3163 * handshake whenever the relatively short initial timeout is
3164 * reached and EAPOL-Key 1/4 is retransmitted. Try to work
3165 * around this by increasing the timeout now that we know that
3166 * the station has received the frame.
3167 */
3168 int timeout_ms = eapol_key_timeout_subseq;
3169 wpa_printf(MSG_DEBUG, "WPA: Increase initial EAPOL-Key 1/4 "
3170 "timeout by %u ms because of acknowledged frame",
3171 timeout_ms);
3172 eloop_cancel_timeout(wpa_send_eapol_timeout, wpa_auth, sm);
3173 eloop_register_timeout(timeout_ms / 1000,
3174 (timeout_ms % 1000) * 1000,
3175 wpa_send_eapol_timeout, wpa_auth, sm);
3176 }
3177}
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003178
3179
3180int wpa_auth_uses_sae(struct wpa_state_machine *sm)
3181{
3182 if (sm == NULL)
3183 return 0;
3184 return wpa_key_mgmt_sae(sm->wpa_key_mgmt);
3185}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003186
3187
3188int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm)
3189{
3190 if (sm == NULL)
3191 return 0;
3192 return sm->wpa_key_mgmt == WPA_KEY_MGMT_FT_SAE;
3193}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003194
3195
3196#ifdef CONFIG_P2P
3197int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr)
3198{
3199 if (sm == NULL || WPA_GET_BE32(sm->ip_addr) == 0)
3200 return -1;
3201 os_memcpy(addr, sm->ip_addr, 4);
3202 return 0;
3203}
3204#endif /* CONFIG_P2P */