905bb4aa95dc86aa9acffdf498760a6fe108c546
[libeap.git] / src / rsn_supp / wpa.c
1 /*
2  * WPA Supplicant - WPA state machine and EAPOL-Key processing
3  * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "crypto/aes_wrap.h"
19 #include "crypto/crypto.h"
20 #include "common/ieee802_11_defs.h"
21 #include "eapol_supp/eapol_supp_sm.h"
22 #include "wpa.h"
23 #include "eloop.h"
24 #include "preauth.h"
25 #include "pmksa_cache.h"
26 #include "wpa_i.h"
27 #include "wpa_ie.h"
28 #include "peerkey.h"
29
30
31 /**
32  * wpa_eapol_key_send - Send WPA/RSN EAPOL-Key message
33  * @sm: Pointer to WPA state machine data from wpa_sm_init()
34  * @kck: Key Confirmation Key (KCK, part of PTK)
35  * @ver: Version field from Key Info
36  * @dest: Destination address for the frame
37  * @proto: Ethertype (usually ETH_P_EAPOL)
38  * @msg: EAPOL-Key message
39  * @msg_len: Length of message
40  * @key_mic: Pointer to the buffer to which the EAPOL-Key MIC is written
41  */
42 void wpa_eapol_key_send(struct wpa_sm *sm, const u8 *kck,
43                         int ver, const u8 *dest, u16 proto,
44                         u8 *msg, size_t msg_len, u8 *key_mic)
45 {
46         if (is_zero_ether_addr(dest) && is_zero_ether_addr(sm->bssid)) {
47                 /*
48                  * Association event was not yet received; try to fetch
49                  * BSSID from the driver.
50                  */
51                 if (wpa_sm_get_bssid(sm, sm->bssid) < 0) {
52                         wpa_printf(MSG_DEBUG, "WPA: Failed to read BSSID for "
53                                    "EAPOL-Key destination address");
54                 } else {
55                         dest = sm->bssid;
56                         wpa_printf(MSG_DEBUG, "WPA: Use BSSID (" MACSTR
57                                    ") as the destination for EAPOL-Key",
58                                    MAC2STR(dest));
59                 }
60         }
61         if (key_mic &&
62             wpa_eapol_key_mic(kck, ver, msg, msg_len, key_mic)) {
63                 wpa_printf(MSG_ERROR, "WPA: Failed to generate EAPOL-Key "
64                            "version %d MIC", ver);
65                 goto out;
66         }
67         wpa_hexdump(MSG_MSGDUMP, "WPA: TX EAPOL-Key", msg, msg_len);
68         wpa_sm_ether_send(sm, dest, proto, msg, msg_len);
69         eapol_sm_notify_tx_eapol_key(sm->eapol);
70 out:
71         os_free(msg);
72 }
73
74
75 /**
76  * wpa_sm_key_request - Send EAPOL-Key Request
77  * @sm: Pointer to WPA state machine data from wpa_sm_init()
78  * @error: Indicate whether this is an Michael MIC error report
79  * @pairwise: 1 = error report for pairwise packet, 0 = for group packet
80  *
81  * Send an EAPOL-Key Request to the current authenticator. This function is
82  * used to request rekeying and it is usually called when a local Michael MIC
83  * failure is detected.
84  */
85 void wpa_sm_key_request(struct wpa_sm *sm, int error, int pairwise)
86 {
87         size_t rlen;
88         struct wpa_eapol_key *reply;
89         int key_info, ver;
90         u8 bssid[ETH_ALEN], *rbuf;
91
92         if (wpa_key_mgmt_ft(sm->key_mgmt) || wpa_key_mgmt_sha256(sm->key_mgmt))
93                 ver = WPA_KEY_INFO_TYPE_AES_128_CMAC;
94         else if (sm->pairwise_cipher == WPA_CIPHER_CCMP)
95                 ver = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
96         else
97                 ver = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
98
99         if (wpa_sm_get_bssid(sm, bssid) < 0) {
100                 wpa_printf(MSG_WARNING, "Failed to read BSSID for EAPOL-Key "
101                            "request");
102                 return;
103         }
104
105         rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
106                                   sizeof(*reply), &rlen, (void *) &reply);
107         if (rbuf == NULL)
108                 return;
109
110         reply->type = sm->proto == WPA_PROTO_RSN ?
111                 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
112         key_info = WPA_KEY_INFO_REQUEST | ver;
113         if (sm->ptk_set)
114                 key_info |= WPA_KEY_INFO_MIC;
115         if (error)
116                 key_info |= WPA_KEY_INFO_ERROR;
117         if (pairwise)
118                 key_info |= WPA_KEY_INFO_KEY_TYPE;
119         WPA_PUT_BE16(reply->key_info, key_info);
120         WPA_PUT_BE16(reply->key_length, 0);
121         os_memcpy(reply->replay_counter, sm->request_counter,
122                   WPA_REPLAY_COUNTER_LEN);
123         inc_byte_array(sm->request_counter, WPA_REPLAY_COUNTER_LEN);
124
125         WPA_PUT_BE16(reply->key_data_length, 0);
126
127         wpa_printf(MSG_INFO, "WPA: Sending EAPOL-Key Request (error=%d "
128                    "pairwise=%d ptk_set=%d len=%lu)",
129                    error, pairwise, sm->ptk_set, (unsigned long) rlen);
130         wpa_eapol_key_send(sm, sm->ptk.kck, ver, bssid, ETH_P_EAPOL,
131                            rbuf, rlen, key_info & WPA_KEY_INFO_MIC ?
132                            reply->key_mic : NULL);
133 }
134
135
136 static int wpa_supplicant_get_pmk(struct wpa_sm *sm,
137                                   const unsigned char *src_addr,
138                                   const u8 *pmkid)
139 {
140         int abort_cached = 0;
141
142         if (pmkid && !sm->cur_pmksa) {
143                 /* When using drivers that generate RSN IE, wpa_supplicant may
144                  * not have enough time to get the association information
145                  * event before receiving this 1/4 message, so try to find a
146                  * matching PMKSA cache entry here. */
147                 sm->cur_pmksa = pmksa_cache_get(sm->pmksa, src_addr, pmkid);
148                 if (sm->cur_pmksa) {
149                         wpa_printf(MSG_DEBUG, "RSN: found matching PMKID from "
150                                    "PMKSA cache");
151                 } else {
152                         wpa_printf(MSG_DEBUG, "RSN: no matching PMKID found");
153                         abort_cached = 1;
154                 }
155         }
156
157         if (pmkid && sm->cur_pmksa &&
158             os_memcmp(pmkid, sm->cur_pmksa->pmkid, PMKID_LEN) == 0) {
159                 wpa_hexdump(MSG_DEBUG, "RSN: matched PMKID", pmkid, PMKID_LEN);
160                 wpa_sm_set_pmk_from_pmksa(sm);
161                 wpa_hexdump_key(MSG_DEBUG, "RSN: PMK from PMKSA cache",
162                                 sm->pmk, sm->pmk_len);
163                 eapol_sm_notify_cached(sm->eapol);
164 #ifdef CONFIG_IEEE80211R
165                 sm->xxkey_len = 0;
166 #endif /* CONFIG_IEEE80211R */
167         } else if (wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt) && sm->eapol) {
168                 int res, pmk_len;
169                 pmk_len = PMK_LEN;
170                 res = eapol_sm_get_key(sm->eapol, sm->pmk, PMK_LEN);
171                 if (res) {
172                         /*
173                          * EAP-LEAP is an exception from other EAP methods: it
174                          * uses only 16-byte PMK.
175                          */
176                         res = eapol_sm_get_key(sm->eapol, sm->pmk, 16);
177                         pmk_len = 16;
178                 } else {
179 #ifdef CONFIG_IEEE80211R
180                         u8 buf[2 * PMK_LEN];
181                         if (eapol_sm_get_key(sm->eapol, buf, 2 * PMK_LEN) == 0)
182                         {
183                                 os_memcpy(sm->xxkey, buf + PMK_LEN, PMK_LEN);
184                                 sm->xxkey_len = PMK_LEN;
185                                 os_memset(buf, 0, sizeof(buf));
186                         }
187 #endif /* CONFIG_IEEE80211R */
188                 }
189                 if (res == 0) {
190                         wpa_hexdump_key(MSG_DEBUG, "WPA: PMK from EAPOL state "
191                                         "machines", sm->pmk, pmk_len);
192                         sm->pmk_len = pmk_len;
193                         if (sm->proto == WPA_PROTO_RSN) {
194                                 pmksa_cache_add(sm->pmksa, sm->pmk, pmk_len,
195                                                 src_addr, sm->own_addr,
196                                                 sm->network_ctx, sm->key_mgmt);
197                         }
198                         if (!sm->cur_pmksa && pmkid &&
199                             pmksa_cache_get(sm->pmksa, src_addr, pmkid)) {
200                                 wpa_printf(MSG_DEBUG, "RSN: the new PMK "
201                                            "matches with the PMKID");
202                                 abort_cached = 0;
203                         }
204                 } else {
205                         wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
206                                 "WPA: Failed to get master session key from "
207                                 "EAPOL state machines");
208                         wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
209                                 "WPA: Key handshake aborted");
210                         if (sm->cur_pmksa) {
211                                 wpa_printf(MSG_DEBUG, "RSN: Cancelled PMKSA "
212                                            "caching attempt");
213                                 sm->cur_pmksa = NULL;
214                                 abort_cached = 1;
215                         } else if (!abort_cached) {
216                                 return -1;
217                         }
218                 }
219         }
220
221         if (abort_cached && wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) {
222                 /* Send EAPOL-Start to trigger full EAP authentication. */
223                 u8 *buf;
224                 size_t buflen;
225
226                 wpa_printf(MSG_DEBUG, "RSN: no PMKSA entry found - trigger "
227                            "full EAP authentication");
228                 buf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_START,
229                                          NULL, 0, &buflen, NULL);
230                 if (buf) {
231                         wpa_sm_ether_send(sm, sm->bssid, ETH_P_EAPOL,
232                                           buf, buflen);
233                         os_free(buf);
234                 }
235
236                 return -1;
237         }
238
239         return 0;
240 }
241
242
243 /**
244  * wpa_supplicant_send_2_of_4 - Send message 2 of WPA/RSN 4-Way Handshake
245  * @sm: Pointer to WPA state machine data from wpa_sm_init()
246  * @dst: Destination address for the frame
247  * @key: Pointer to the EAPOL-Key frame header
248  * @ver: Version bits from EAPOL-Key Key Info
249  * @nonce: Nonce value for the EAPOL-Key frame
250  * @wpa_ie: WPA/RSN IE
251  * @wpa_ie_len: Length of the WPA/RSN IE
252  * @ptk: PTK to use for keyed hash and encryption
253  * Returns: 0 on success, -1 on failure
254  */
255 int wpa_supplicant_send_2_of_4(struct wpa_sm *sm, const unsigned char *dst,
256                                const struct wpa_eapol_key *key,
257                                int ver, const u8 *nonce,
258                                const u8 *wpa_ie, size_t wpa_ie_len,
259                                struct wpa_ptk *ptk)
260 {
261         size_t rlen;
262         struct wpa_eapol_key *reply;
263         u8 *rbuf;
264         u8 *rsn_ie_buf = NULL;
265
266         if (wpa_ie == NULL) {
267                 wpa_printf(MSG_WARNING, "WPA: No wpa_ie set - cannot "
268                            "generate msg 2/4");
269                 return -1;
270         }
271
272 #ifdef CONFIG_IEEE80211R
273         if (wpa_key_mgmt_ft(sm->key_mgmt)) {
274                 int res;
275
276                 /* Add PMKR1Name into RSN IE (PMKID-List) */
277                 rsn_ie_buf = os_malloc(wpa_ie_len + 2 + 2 + PMKID_LEN);
278                 if (rsn_ie_buf == NULL)
279                         return -1;
280                 os_memcpy(rsn_ie_buf, wpa_ie, wpa_ie_len);
281                 res = wpa_insert_pmkid(rsn_ie_buf, wpa_ie_len,
282                                        sm->pmk_r1_name);
283                 if (res < 0) {
284                         os_free(rsn_ie_buf);
285                         return -1;
286                 }
287
288                 wpa_ie = rsn_ie_buf;
289                 wpa_ie_len += res;
290         }
291 #endif /* CONFIG_IEEE80211R */
292
293         wpa_hexdump(MSG_DEBUG, "WPA: WPA IE for msg 2/4", wpa_ie, wpa_ie_len);
294
295         rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY,
296                                   NULL, sizeof(*reply) + wpa_ie_len,
297                                   &rlen, (void *) &reply);
298         if (rbuf == NULL) {
299                 os_free(rsn_ie_buf);
300                 return -1;
301         }
302
303         reply->type = sm->proto == WPA_PROTO_RSN ?
304                 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
305         WPA_PUT_BE16(reply->key_info,
306                      ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC);
307         if (sm->proto == WPA_PROTO_RSN)
308                 WPA_PUT_BE16(reply->key_length, 0);
309         else
310                 os_memcpy(reply->key_length, key->key_length, 2);
311         os_memcpy(reply->replay_counter, key->replay_counter,
312                   WPA_REPLAY_COUNTER_LEN);
313
314         WPA_PUT_BE16(reply->key_data_length, wpa_ie_len);
315         os_memcpy(reply + 1, wpa_ie, wpa_ie_len);
316         os_free(rsn_ie_buf);
317
318         os_memcpy(reply->key_nonce, nonce, WPA_NONCE_LEN);
319
320         wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/4");
321         wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
322                            rbuf, rlen, reply->key_mic);
323
324         return 0;
325 }
326
327
328 static int wpa_derive_ptk(struct wpa_sm *sm, const unsigned char *src_addr,
329                           const struct wpa_eapol_key *key,
330                           struct wpa_ptk *ptk)
331 {
332         size_t ptk_len = sm->pairwise_cipher == WPA_CIPHER_CCMP ? 48 : 64;
333 #ifdef CONFIG_IEEE80211R
334         if (wpa_key_mgmt_ft(sm->key_mgmt))
335                 return wpa_derive_ptk_ft(sm, src_addr, key, ptk, ptk_len);
336 #endif /* CONFIG_IEEE80211R */
337
338         wpa_pmk_to_ptk(sm->pmk, sm->pmk_len, "Pairwise key expansion",
339                        sm->own_addr, sm->bssid, sm->snonce, key->key_nonce,
340                        (u8 *) ptk, ptk_len,
341                        wpa_key_mgmt_sha256(sm->key_mgmt));
342         return 0;
343 }
344
345
346 static void wpa_supplicant_process_1_of_4(struct wpa_sm *sm,
347                                           const unsigned char *src_addr,
348                                           const struct wpa_eapol_key *key,
349                                           u16 ver)
350 {
351         struct wpa_eapol_ie_parse ie;
352         struct wpa_ptk *ptk;
353         u8 buf[8];
354
355         if (wpa_sm_get_network_ctx(sm) == NULL) {
356                 wpa_printf(MSG_WARNING, "WPA: No SSID info found (msg 1 of "
357                            "4).");
358                 return;
359         }
360
361         wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
362         wpa_printf(MSG_DEBUG, "WPA: RX message 1 of 4-Way Handshake from "
363                    MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
364
365         os_memset(&ie, 0, sizeof(ie));
366
367 #ifndef CONFIG_NO_WPA2
368         if (sm->proto == WPA_PROTO_RSN) {
369                 /* RSN: msg 1/4 should contain PMKID for the selected PMK */
370                 const u8 *_buf = (const u8 *) (key + 1);
371                 size_t len = WPA_GET_BE16(key->key_data_length);
372                 wpa_hexdump(MSG_DEBUG, "RSN: msg 1/4 key data", _buf, len);
373                 wpa_supplicant_parse_ies(_buf, len, &ie);
374                 if (ie.pmkid) {
375                         wpa_hexdump(MSG_DEBUG, "RSN: PMKID from "
376                                     "Authenticator", ie.pmkid, PMKID_LEN);
377                 }
378         }
379 #endif /* CONFIG_NO_WPA2 */
380
381         if (wpa_supplicant_get_pmk(sm, src_addr, ie.pmkid))
382                 goto failed;
383
384         if (sm->renew_snonce) {
385                 if (os_get_random(sm->snonce, WPA_NONCE_LEN)) {
386                         wpa_msg(sm->ctx->msg_ctx, MSG_WARNING,
387                                 "WPA: Failed to get random data for SNonce");
388                         goto failed;
389                 }
390                 sm->renew_snonce = 0;
391                 wpa_hexdump(MSG_DEBUG, "WPA: Renewed SNonce",
392                             sm->snonce, WPA_NONCE_LEN);
393         }
394
395         /* Calculate PTK which will be stored as a temporary PTK until it has
396          * been verified when processing message 3/4. */
397         ptk = &sm->tptk;
398         wpa_derive_ptk(sm, src_addr, key, ptk);
399         /* Supplicant: swap tx/rx Mic keys */
400         os_memcpy(buf, ptk->u.auth.tx_mic_key, 8);
401         os_memcpy(ptk->u.auth.tx_mic_key, ptk->u.auth.rx_mic_key, 8);
402         os_memcpy(ptk->u.auth.rx_mic_key, buf, 8);
403         sm->tptk_set = 1;
404
405         if (wpa_supplicant_send_2_of_4(sm, sm->bssid, key, ver, sm->snonce,
406                                        sm->assoc_wpa_ie, sm->assoc_wpa_ie_len,
407                                        ptk))
408                 goto failed;
409
410         os_memcpy(sm->anonce, key->key_nonce, WPA_NONCE_LEN);
411         return;
412
413 failed:
414         wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
415 }
416
417
418 static void wpa_sm_start_preauth(void *eloop_ctx, void *timeout_ctx)
419 {
420         struct wpa_sm *sm = eloop_ctx;
421         rsn_preauth_candidate_process(sm);
422 }
423
424
425 static void wpa_supplicant_key_neg_complete(struct wpa_sm *sm,
426                                             const u8 *addr, int secure)
427 {
428         wpa_msg(sm->ctx->msg_ctx, MSG_INFO,
429                 "WPA: Key negotiation completed with "
430                 MACSTR " [PTK=%s GTK=%s]", MAC2STR(addr),
431                 wpa_cipher_txt(sm->pairwise_cipher),
432                 wpa_cipher_txt(sm->group_cipher));
433         wpa_sm_cancel_auth_timeout(sm);
434         wpa_sm_set_state(sm, WPA_COMPLETED);
435
436         if (secure) {
437                 wpa_sm_mlme_setprotection(
438                         sm, addr, MLME_SETPROTECTION_PROTECT_TYPE_RX_TX,
439                         MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
440                 eapol_sm_notify_portValid(sm->eapol, TRUE);
441                 if (wpa_key_mgmt_wpa_psk(sm->key_mgmt))
442                         eapol_sm_notify_eap_success(sm->eapol, TRUE);
443                 /*
444                  * Start preauthentication after a short wait to avoid a
445                  * possible race condition between the data receive and key
446                  * configuration after the 4-Way Handshake. This increases the
447                  * likelyhood of the first preauth EAPOL-Start frame getting to
448                  * the target AP.
449                  */
450                 eloop_register_timeout(1, 0, wpa_sm_start_preauth, sm, NULL);
451         }
452
453         if (sm->cur_pmksa && sm->cur_pmksa->opportunistic) {
454                 wpa_printf(MSG_DEBUG, "RSN: Authenticator accepted "
455                            "opportunistic PMKSA entry - marking it valid");
456                 sm->cur_pmksa->opportunistic = 0;
457         }
458
459 #ifdef CONFIG_IEEE80211R
460         if (wpa_key_mgmt_ft(sm->key_mgmt)) {
461                 /* Prepare for the next transition */
462                 wpa_ft_prepare_auth_request(sm);
463         }
464 #endif /* CONFIG_IEEE80211R */
465 }
466
467
468 static void wpa_sm_rekey_ptk(void *eloop_ctx, void *timeout_ctx)
469 {
470         struct wpa_sm *sm = eloop_ctx;
471         wpa_printf(MSG_DEBUG, "WPA: Request PTK rekeying");
472         wpa_sm_key_request(sm, 0, 1);
473 }
474
475
476 static int wpa_supplicant_install_ptk(struct wpa_sm *sm,
477                                       const struct wpa_eapol_key *key)
478 {
479         int keylen, rsclen;
480         enum wpa_alg alg;
481         const u8 *key_rsc;
482         u8 null_rsc[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
483
484         wpa_printf(MSG_DEBUG, "WPA: Installing PTK to the driver.");
485
486         switch (sm->pairwise_cipher) {
487         case WPA_CIPHER_CCMP:
488                 alg = WPA_ALG_CCMP;
489                 keylen = 16;
490                 rsclen = 6;
491                 break;
492         case WPA_CIPHER_TKIP:
493                 alg = WPA_ALG_TKIP;
494                 keylen = 32;
495                 rsclen = 6;
496                 break;
497         case WPA_CIPHER_NONE:
498                 wpa_printf(MSG_DEBUG, "WPA: Pairwise Cipher Suite: "
499                            "NONE - do not use pairwise keys");
500                 return 0;
501         default:
502                 wpa_printf(MSG_WARNING, "WPA: Unsupported pairwise cipher %d",
503                            sm->pairwise_cipher);
504                 return -1;
505         }
506
507         if (sm->proto == WPA_PROTO_RSN) {
508                 key_rsc = null_rsc;
509         } else {
510                 key_rsc = key->key_rsc;
511                 wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, rsclen);
512         }
513
514         if (wpa_sm_set_key(sm, alg, sm->bssid, 0, 1, key_rsc, rsclen,
515                            (u8 *) sm->ptk.tk1, keylen) < 0) {
516                 wpa_printf(MSG_WARNING, "WPA: Failed to set PTK to the "
517                            "driver (alg=%d keylen=%d bssid=" MACSTR ")",
518                            alg, keylen, MAC2STR(sm->bssid));
519                 return -1;
520         }
521
522         if (sm->wpa_ptk_rekey) {
523                 eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
524                 eloop_register_timeout(sm->wpa_ptk_rekey, 0, wpa_sm_rekey_ptk,
525                                        sm, NULL);
526         }
527
528         return 0;
529 }
530
531
532 static int wpa_supplicant_check_group_cipher(int group_cipher,
533                                              int keylen, int maxkeylen,
534                                              int *key_rsc_len,
535                                              enum wpa_alg *alg)
536 {
537         int ret = 0;
538
539         switch (group_cipher) {
540         case WPA_CIPHER_CCMP:
541                 if (keylen != 16 || maxkeylen < 16) {
542                         ret = -1;
543                         break;
544                 }
545                 *key_rsc_len = 6;
546                 *alg = WPA_ALG_CCMP;
547                 break;
548         case WPA_CIPHER_TKIP:
549                 if (keylen != 32 || maxkeylen < 32) {
550                         ret = -1;
551                         break;
552                 }
553                 *key_rsc_len = 6;
554                 *alg = WPA_ALG_TKIP;
555                 break;
556         case WPA_CIPHER_WEP104:
557                 if (keylen != 13 || maxkeylen < 13) {
558                         ret = -1;
559                         break;
560                 }
561                 *key_rsc_len = 0;
562                 *alg = WPA_ALG_WEP;
563                 break;
564         case WPA_CIPHER_WEP40:
565                 if (keylen != 5 || maxkeylen < 5) {
566                         ret = -1;
567                         break;
568                 }
569                 *key_rsc_len = 0;
570                 *alg = WPA_ALG_WEP;
571                 break;
572         default:
573                 wpa_printf(MSG_WARNING, "WPA: Unsupported Group Cipher %d",
574                            group_cipher);
575                 return -1;
576         }
577
578         if (ret < 0 ) {
579                 wpa_printf(MSG_WARNING, "WPA: Unsupported %s Group Cipher key "
580                            "length %d (%d).",
581                            wpa_cipher_txt(group_cipher), keylen, maxkeylen);
582         }
583
584         return ret;
585 }
586
587
588 struct wpa_gtk_data {
589         enum wpa_alg alg;
590         int tx, key_rsc_len, keyidx;
591         u8 gtk[32];
592         int gtk_len;
593 };
594
595
596 static int wpa_supplicant_install_gtk(struct wpa_sm *sm,
597                                       const struct wpa_gtk_data *gd,
598                                       const u8 *key_rsc)
599 {
600         const u8 *_gtk = gd->gtk;
601         u8 gtk_buf[32];
602
603         wpa_hexdump_key(MSG_DEBUG, "WPA: Group Key", gd->gtk, gd->gtk_len);
604         wpa_printf(MSG_DEBUG, "WPA: Installing GTK to the driver "
605                    "(keyidx=%d tx=%d len=%d).", gd->keyidx, gd->tx,
606                    gd->gtk_len);
607         wpa_hexdump(MSG_DEBUG, "WPA: RSC", key_rsc, gd->key_rsc_len);
608         if (sm->group_cipher == WPA_CIPHER_TKIP) {
609                 /* Swap Tx/Rx keys for Michael MIC */
610                 os_memcpy(gtk_buf, gd->gtk, 16);
611                 os_memcpy(gtk_buf + 16, gd->gtk + 24, 8);
612                 os_memcpy(gtk_buf + 24, gd->gtk + 16, 8);
613                 _gtk = gtk_buf;
614         }
615         if (sm->pairwise_cipher == WPA_CIPHER_NONE) {
616                 if (wpa_sm_set_key(sm, gd->alg,
617                                    (u8 *) "\xff\xff\xff\xff\xff\xff",
618                                    gd->keyidx, 1, key_rsc, gd->key_rsc_len,
619                                    _gtk, gd->gtk_len) < 0) {
620                         wpa_printf(MSG_WARNING, "WPA: Failed to set "
621                                    "GTK to the driver (Group only).");
622                         return -1;
623                 }
624         } else if (wpa_sm_set_key(sm, gd->alg,
625                                   (u8 *) "\xff\xff\xff\xff\xff\xff",
626                                   gd->keyidx, gd->tx, key_rsc, gd->key_rsc_len,
627                                   _gtk, gd->gtk_len) < 0) {
628                 wpa_printf(MSG_WARNING, "WPA: Failed to set GTK to "
629                            "the driver (alg=%d keylen=%d keyidx=%d)",
630                            gd->alg, gd->gtk_len, gd->keyidx);
631                 return -1;
632         }
633
634         return 0;
635 }
636
637
638 static int wpa_supplicant_gtk_tx_bit_workaround(const struct wpa_sm *sm,
639                                                 int tx)
640 {
641         if (tx && sm->pairwise_cipher != WPA_CIPHER_NONE) {
642                 /* Ignore Tx bit for GTK if a pairwise key is used. One AP
643                  * seemed to set this bit (incorrectly, since Tx is only when
644                  * doing Group Key only APs) and without this workaround, the
645                  * data connection does not work because wpa_supplicant
646                  * configured non-zero keyidx to be used for unicast. */
647                 wpa_printf(MSG_INFO, "WPA: Tx bit set for GTK, but pairwise "
648                            "keys are used - ignore Tx bit");
649                 return 0;
650         }
651         return tx;
652 }
653
654
655 static int wpa_supplicant_pairwise_gtk(struct wpa_sm *sm,
656                                        const struct wpa_eapol_key *key,
657                                        const u8 *gtk, size_t gtk_len,
658                                        int key_info)
659 {
660 #ifndef CONFIG_NO_WPA2
661         struct wpa_gtk_data gd;
662
663         /*
664          * IEEE Std 802.11i-2004 - 8.5.2 EAPOL-Key frames - Figure 43x
665          * GTK KDE format:
666          * KeyID[bits 0-1], Tx [bit 2], Reserved [bits 3-7]
667          * Reserved [bits 0-7]
668          * GTK
669          */
670
671         os_memset(&gd, 0, sizeof(gd));
672         wpa_hexdump_key(MSG_DEBUG, "RSN: received GTK in pairwise handshake",
673                         gtk, gtk_len);
674
675         if (gtk_len < 2 || gtk_len - 2 > sizeof(gd.gtk))
676                 return -1;
677
678         gd.keyidx = gtk[0] & 0x3;
679         gd.tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
680                                                      !!(gtk[0] & BIT(2)));
681         gtk += 2;
682         gtk_len -= 2;
683
684         os_memcpy(gd.gtk, gtk, gtk_len);
685         gd.gtk_len = gtk_len;
686
687         if (wpa_supplicant_check_group_cipher(sm->group_cipher,
688                                               gtk_len, gtk_len,
689                                               &gd.key_rsc_len, &gd.alg) ||
690             wpa_supplicant_install_gtk(sm, &gd, key->key_rsc)) {
691                 wpa_printf(MSG_DEBUG, "RSN: Failed to install GTK");
692                 return -1;
693         }
694
695         wpa_supplicant_key_neg_complete(sm, sm->bssid,
696                                         key_info & WPA_KEY_INFO_SECURE);
697         return 0;
698 #else /* CONFIG_NO_WPA2 */
699         return -1;
700 #endif /* CONFIG_NO_WPA2 */
701 }
702
703
704 static int ieee80211w_set_keys(struct wpa_sm *sm,
705                                struct wpa_eapol_ie_parse *ie)
706 {
707 #ifdef CONFIG_IEEE80211W
708         if (sm->mgmt_group_cipher != WPA_CIPHER_AES_128_CMAC)
709                 return 0;
710
711         if (ie->igtk) {
712                 const struct wpa_igtk_kde *igtk;
713                 u16 keyidx;
714                 if (ie->igtk_len != sizeof(*igtk))
715                         return -1;
716                 igtk = (const struct wpa_igtk_kde *) ie->igtk;
717                 keyidx = WPA_GET_LE16(igtk->keyid);
718                 wpa_printf(MSG_DEBUG, "WPA: IGTK keyid %d "
719                            "pn %02x%02x%02x%02x%02x%02x",
720                            keyidx, MAC2STR(igtk->pn));
721                 wpa_hexdump_key(MSG_DEBUG, "WPA: IGTK",
722                                 igtk->igtk, WPA_IGTK_LEN);
723                 if (keyidx > 4095) {
724                         wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KeyID %d",
725                                    keyidx);
726                         return -1;
727                 }
728                 if (wpa_sm_set_key(sm, WPA_ALG_IGTK,
729                                    (u8 *) "\xff\xff\xff\xff\xff\xff",
730                                    keyidx, 0, igtk->pn, sizeof(igtk->pn),
731                                    igtk->igtk, WPA_IGTK_LEN) < 0) {
732                         wpa_printf(MSG_WARNING, "WPA: Failed to configure IGTK"
733                                    " to the driver");
734                         return -1;
735                 }
736         }
737
738         return 0;
739 #else /* CONFIG_IEEE80211W */
740         return 0;
741 #endif /* CONFIG_IEEE80211W */
742 }
743
744
745 static void wpa_report_ie_mismatch(struct wpa_sm *sm,
746                                    const char *reason, const u8 *src_addr,
747                                    const u8 *wpa_ie, size_t wpa_ie_len,
748                                    const u8 *rsn_ie, size_t rsn_ie_len)
749 {
750         wpa_msg(sm->ctx->msg_ctx, MSG_WARNING, "WPA: %s (src=" MACSTR ")",
751                 reason, MAC2STR(src_addr));
752
753         if (sm->ap_wpa_ie) {
754                 wpa_hexdump(MSG_INFO, "WPA: WPA IE in Beacon/ProbeResp",
755                             sm->ap_wpa_ie, sm->ap_wpa_ie_len);
756         }
757         if (wpa_ie) {
758                 if (!sm->ap_wpa_ie) {
759                         wpa_printf(MSG_INFO, "WPA: No WPA IE in "
760                                    "Beacon/ProbeResp");
761                 }
762                 wpa_hexdump(MSG_INFO, "WPA: WPA IE in 3/4 msg",
763                             wpa_ie, wpa_ie_len);
764         }
765
766         if (sm->ap_rsn_ie) {
767                 wpa_hexdump(MSG_INFO, "WPA: RSN IE in Beacon/ProbeResp",
768                             sm->ap_rsn_ie, sm->ap_rsn_ie_len);
769         }
770         if (rsn_ie) {
771                 if (!sm->ap_rsn_ie) {
772                         wpa_printf(MSG_INFO, "WPA: No RSN IE in "
773                                    "Beacon/ProbeResp");
774                 }
775                 wpa_hexdump(MSG_INFO, "WPA: RSN IE in 3/4 msg",
776                             rsn_ie, rsn_ie_len);
777         }
778
779         wpa_sm_disassociate(sm, WLAN_REASON_IE_IN_4WAY_DIFFERS);
780 }
781
782
783 static int wpa_supplicant_validate_ie(struct wpa_sm *sm,
784                                       const unsigned char *src_addr,
785                                       struct wpa_eapol_ie_parse *ie)
786 {
787         if (sm->ap_wpa_ie == NULL && sm->ap_rsn_ie == NULL) {
788                 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE for this AP known. "
789                            "Trying to get from scan results");
790                 if (wpa_sm_get_beacon_ie(sm) < 0) {
791                         wpa_printf(MSG_WARNING, "WPA: Could not find AP from "
792                                    "the scan results");
793                 } else {
794                         wpa_printf(MSG_DEBUG, "WPA: Found the current AP from "
795                                    "updated scan results");
796                 }
797         }
798
799         if (ie->wpa_ie == NULL && ie->rsn_ie == NULL &&
800             (sm->ap_wpa_ie || sm->ap_rsn_ie)) {
801                 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
802                                        "with IE in Beacon/ProbeResp (no IE?)",
803                                        src_addr, ie->wpa_ie, ie->wpa_ie_len,
804                                        ie->rsn_ie, ie->rsn_ie_len);
805                 return -1;
806         }
807
808         if ((ie->wpa_ie && sm->ap_wpa_ie &&
809              (ie->wpa_ie_len != sm->ap_wpa_ie_len ||
810               os_memcmp(ie->wpa_ie, sm->ap_wpa_ie, ie->wpa_ie_len) != 0)) ||
811             (ie->rsn_ie && sm->ap_rsn_ie &&
812              wpa_compare_rsn_ie(wpa_key_mgmt_ft(sm->key_mgmt),
813                                 sm->ap_rsn_ie, sm->ap_rsn_ie_len,
814                                 ie->rsn_ie, ie->rsn_ie_len))) {
815                 wpa_report_ie_mismatch(sm, "IE in 3/4 msg does not match "
816                                        "with IE in Beacon/ProbeResp",
817                                        src_addr, ie->wpa_ie, ie->wpa_ie_len,
818                                        ie->rsn_ie, ie->rsn_ie_len);
819                 return -1;
820         }
821
822         if (sm->proto == WPA_PROTO_WPA &&
823             ie->rsn_ie && sm->ap_rsn_ie == NULL && sm->rsn_enabled) {
824                 wpa_report_ie_mismatch(sm, "Possible downgrade attack "
825                                        "detected - RSN was enabled and RSN IE "
826                                        "was in msg 3/4, but not in "
827                                        "Beacon/ProbeResp",
828                                        src_addr, ie->wpa_ie, ie->wpa_ie_len,
829                                        ie->rsn_ie, ie->rsn_ie_len);
830                 return -1;
831         }
832
833 #ifdef CONFIG_IEEE80211R
834         if (wpa_key_mgmt_ft(sm->key_mgmt)) {
835                 struct rsn_mdie *mdie;
836                 /* TODO: verify that full MDIE matches with the one from scan
837                  * results, not only mobility domain */
838                 mdie = (struct rsn_mdie *) (ie->mdie + 2);
839                 if (ie->mdie == NULL || ie->mdie_len < 2 + sizeof(*mdie) ||
840                     os_memcmp(mdie->mobility_domain, sm->mobility_domain,
841                               MOBILITY_DOMAIN_ID_LEN) != 0) {
842                         wpa_printf(MSG_DEBUG, "FT: MDIE in msg 3/4 did not "
843                                    "match with the current mobility domain");
844                         return -1;
845                 }
846         }
847 #endif /* CONFIG_IEEE80211R */
848
849         return 0;
850 }
851
852
853 /**
854  * wpa_supplicant_send_4_of_4 - Send message 4 of WPA/RSN 4-Way Handshake
855  * @sm: Pointer to WPA state machine data from wpa_sm_init()
856  * @dst: Destination address for the frame
857  * @key: Pointer to the EAPOL-Key frame header
858  * @ver: Version bits from EAPOL-Key Key Info
859  * @key_info: Key Info
860  * @kde: KDEs to include the EAPOL-Key frame
861  * @kde_len: Length of KDEs
862  * @ptk: PTK to use for keyed hash and encryption
863  * Returns: 0 on success, -1 on failure
864  */
865 int wpa_supplicant_send_4_of_4(struct wpa_sm *sm, const unsigned char *dst,
866                                const struct wpa_eapol_key *key,
867                                u16 ver, u16 key_info,
868                                const u8 *kde, size_t kde_len,
869                                struct wpa_ptk *ptk)
870 {
871         size_t rlen;
872         struct wpa_eapol_key *reply;
873         u8 *rbuf;
874
875         if (kde)
876                 wpa_hexdump(MSG_DEBUG, "WPA: KDE for msg 4/4", kde, kde_len);
877
878         rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
879                                   sizeof(*reply) + kde_len,
880                                   &rlen, (void *) &reply);
881         if (rbuf == NULL)
882                 return -1;
883
884         reply->type = sm->proto == WPA_PROTO_RSN ?
885                 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
886         key_info &= WPA_KEY_INFO_SECURE;
887         key_info |= ver | WPA_KEY_INFO_KEY_TYPE | WPA_KEY_INFO_MIC;
888         WPA_PUT_BE16(reply->key_info, key_info);
889         if (sm->proto == WPA_PROTO_RSN)
890                 WPA_PUT_BE16(reply->key_length, 0);
891         else
892                 os_memcpy(reply->key_length, key->key_length, 2);
893         os_memcpy(reply->replay_counter, key->replay_counter,
894                   WPA_REPLAY_COUNTER_LEN);
895
896         WPA_PUT_BE16(reply->key_data_length, kde_len);
897         if (kde)
898                 os_memcpy(reply + 1, kde, kde_len);
899
900         wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 4/4");
901         wpa_eapol_key_send(sm, ptk->kck, ver, dst, ETH_P_EAPOL,
902                            rbuf, rlen, reply->key_mic);
903
904         return 0;
905 }
906
907
908 static void wpa_supplicant_process_3_of_4(struct wpa_sm *sm,
909                                           const struct wpa_eapol_key *key,
910                                           u16 ver)
911 {
912         u16 key_info, keylen, len;
913         const u8 *pos;
914         struct wpa_eapol_ie_parse ie;
915
916         wpa_sm_set_state(sm, WPA_4WAY_HANDSHAKE);
917         wpa_printf(MSG_DEBUG, "WPA: RX message 3 of 4-Way Handshake from "
918                    MACSTR " (ver=%d)", MAC2STR(sm->bssid), ver);
919
920         key_info = WPA_GET_BE16(key->key_info);
921
922         pos = (const u8 *) (key + 1);
923         len = WPA_GET_BE16(key->key_data_length);
924         wpa_hexdump(MSG_DEBUG, "WPA: IE KeyData", pos, len);
925         wpa_supplicant_parse_ies(pos, len, &ie);
926         if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
927                 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
928                 goto failed;
929         }
930 #ifdef CONFIG_IEEE80211W
931         if (ie.igtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
932                 wpa_printf(MSG_WARNING, "WPA: IGTK KDE in unencrypted key "
933                            "data");
934                 goto failed;
935         }
936
937         if (ie.igtk && ie.igtk_len != sizeof(struct wpa_igtk_kde)) {
938                 wpa_printf(MSG_WARNING, "WPA: Invalid IGTK KDE length %lu",
939                            (unsigned long) ie.igtk_len);
940                 goto failed;
941         }
942 #endif /* CONFIG_IEEE80211W */
943
944         if (wpa_supplicant_validate_ie(sm, sm->bssid, &ie) < 0)
945                 goto failed;
946
947 #ifdef CONFIG_IEEE80211R
948         if (wpa_key_mgmt_ft(sm->key_mgmt) && ie.rsn_ie) {
949                 struct wpa_ie_data rsn;
950                 /*
951                  * Verify that PMKR1Name from EAPOL-Key message 3/4 matches
952                  * with the value we derived.
953                  */
954                 if (wpa_parse_wpa_ie_rsn(ie.rsn_ie, ie.rsn_ie_len, &rsn) < 0 ||
955                     rsn.num_pmkid != 1 || rsn.pmkid == NULL) {
956                         wpa_printf(MSG_DEBUG, "FT: No PMKR1Name in "
957                                    "FT 4-way handshake message 3/4");
958                         return;
959                 }
960
961                 if (os_memcmp(rsn.pmkid, sm->pmk_r1_name, WPA_PMK_NAME_LEN) !=
962                     0) {
963                         wpa_printf(MSG_DEBUG, "FT: PMKR1Name mismatch in "
964                                    "FT 4-way handshake message 3/4");
965                         wpa_hexdump(MSG_DEBUG, "FT: PMKR1Name from "
966                                     "Authenticator",
967                                     rsn.pmkid, WPA_PMK_NAME_LEN);
968                         wpa_hexdump(MSG_DEBUG, "FT: Derived PMKR1Name",
969                                     sm->pmk_r1_name, WPA_PMK_NAME_LEN);
970                         return;
971                 }
972         }
973 #endif /* CONFIG_IEEE80211R */
974
975         if (os_memcmp(sm->anonce, key->key_nonce, WPA_NONCE_LEN) != 0) {
976                 wpa_printf(MSG_WARNING, "WPA: ANonce from message 1 of 4-Way "
977                            "Handshake differs from 3 of 4-Way Handshake - drop"
978                            " packet (src=" MACSTR ")", MAC2STR(sm->bssid));
979                 goto failed;
980         }
981
982         keylen = WPA_GET_BE16(key->key_length);
983         switch (sm->pairwise_cipher) {
984         case WPA_CIPHER_CCMP:
985                 if (keylen != 16) {
986                         wpa_printf(MSG_WARNING, "WPA: Invalid CCMP key length "
987                                    "%d (src=" MACSTR ")",
988                                    keylen, MAC2STR(sm->bssid));
989                         goto failed;
990                 }
991                 break;
992         case WPA_CIPHER_TKIP:
993                 if (keylen != 32) {
994                         wpa_printf(MSG_WARNING, "WPA: Invalid TKIP key length "
995                                    "%d (src=" MACSTR ")",
996                                    keylen, MAC2STR(sm->bssid));
997                         goto failed;
998                 }
999                 break;
1000         }
1001
1002         if (wpa_supplicant_send_4_of_4(sm, sm->bssid, key, ver, key_info,
1003                                        NULL, 0, &sm->ptk)) {
1004                 goto failed;
1005         }
1006
1007         /* SNonce was successfully used in msg 3/4, so mark it to be renewed
1008          * for the next 4-Way Handshake. If msg 3 is received again, the old
1009          * SNonce will still be used to avoid changing PTK. */
1010         sm->renew_snonce = 1;
1011
1012         if (key_info & WPA_KEY_INFO_INSTALL) {
1013                 if (wpa_supplicant_install_ptk(sm, key))
1014                         goto failed;
1015         }
1016
1017         if (key_info & WPA_KEY_INFO_SECURE) {
1018                 wpa_sm_mlme_setprotection(
1019                         sm, sm->bssid, MLME_SETPROTECTION_PROTECT_TYPE_RX,
1020                         MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
1021                 eapol_sm_notify_portValid(sm->eapol, TRUE);
1022         }
1023         wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1024
1025         if (ie.gtk &&
1026             wpa_supplicant_pairwise_gtk(sm, key,
1027                                         ie.gtk, ie.gtk_len, key_info) < 0) {
1028                 wpa_printf(MSG_INFO, "RSN: Failed to configure GTK");
1029                 goto failed;
1030         }
1031
1032         if (ieee80211w_set_keys(sm, &ie) < 0) {
1033                 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1034                 goto failed;
1035         }
1036
1037         return;
1038
1039 failed:
1040         wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1041 }
1042
1043
1044 static int wpa_supplicant_process_1_of_2_rsn(struct wpa_sm *sm,
1045                                              const u8 *keydata,
1046                                              size_t keydatalen,
1047                                              u16 key_info,
1048                                              struct wpa_gtk_data *gd)
1049 {
1050         int maxkeylen;
1051         struct wpa_eapol_ie_parse ie;
1052
1053         wpa_hexdump(MSG_DEBUG, "RSN: msg 1/2 key data", keydata, keydatalen);
1054         wpa_supplicant_parse_ies(keydata, keydatalen, &ie);
1055         if (ie.gtk && !(key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1056                 wpa_printf(MSG_WARNING, "WPA: GTK IE in unencrypted key data");
1057                 return -1;
1058         }
1059         if (ie.gtk == NULL) {
1060                 wpa_printf(MSG_INFO, "WPA: No GTK IE in Group Key msg 1/2");
1061                 return -1;
1062         }
1063         maxkeylen = gd->gtk_len = ie.gtk_len - 2;
1064
1065         if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1066                                               gd->gtk_len, maxkeylen,
1067                                               &gd->key_rsc_len, &gd->alg))
1068                 return -1;
1069
1070         wpa_hexdump(MSG_DEBUG, "RSN: received GTK in group key handshake",
1071                     ie.gtk, ie.gtk_len);
1072         gd->keyidx = ie.gtk[0] & 0x3;
1073         gd->tx = wpa_supplicant_gtk_tx_bit_workaround(sm,
1074                                                       !!(ie.gtk[0] & BIT(2)));
1075         if (ie.gtk_len - 2 > sizeof(gd->gtk)) {
1076                 wpa_printf(MSG_INFO, "RSN: Too long GTK in GTK IE "
1077                            "(len=%lu)", (unsigned long) ie.gtk_len - 2);
1078                 return -1;
1079         }
1080         os_memcpy(gd->gtk, ie.gtk + 2, ie.gtk_len - 2);
1081
1082         if (ieee80211w_set_keys(sm, &ie) < 0)
1083                 wpa_printf(MSG_INFO, "RSN: Failed to configure IGTK");
1084
1085         return 0;
1086 }
1087
1088
1089 static int wpa_supplicant_process_1_of_2_wpa(struct wpa_sm *sm,
1090                                              const struct wpa_eapol_key *key,
1091                                              size_t keydatalen, int key_info,
1092                                              size_t extra_len, u16 ver,
1093                                              struct wpa_gtk_data *gd)
1094 {
1095         size_t maxkeylen;
1096         u8 ek[32];
1097
1098         gd->gtk_len = WPA_GET_BE16(key->key_length);
1099         maxkeylen = keydatalen;
1100         if (keydatalen > extra_len) {
1101                 wpa_printf(MSG_INFO, "WPA: Truncated EAPOL-Key packet:"
1102                            " key_data_length=%lu > extra_len=%lu",
1103                            (unsigned long) keydatalen,
1104                            (unsigned long) extra_len);
1105                 return -1;
1106         }
1107         if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1108                 if (maxkeylen < 8) {
1109                         wpa_printf(MSG_INFO, "WPA: Too short maxkeylen (%lu)",
1110                                    (unsigned long) maxkeylen);
1111                         return -1;
1112                 }
1113                 maxkeylen -= 8;
1114         }
1115
1116         if (wpa_supplicant_check_group_cipher(sm->group_cipher,
1117                                               gd->gtk_len, maxkeylen,
1118                                               &gd->key_rsc_len, &gd->alg))
1119                 return -1;
1120
1121         gd->keyidx = (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1122                 WPA_KEY_INFO_KEY_INDEX_SHIFT;
1123         if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1124                 os_memcpy(ek, key->key_iv, 16);
1125                 os_memcpy(ek + 16, sm->ptk.kek, 16);
1126                 if (keydatalen > sizeof(gd->gtk)) {
1127                         wpa_printf(MSG_WARNING, "WPA: RC4 key data "
1128                                    "too long (%lu)",
1129                                    (unsigned long) keydatalen);
1130                         return -1;
1131                 }
1132                 os_memcpy(gd->gtk, key + 1, keydatalen);
1133                 if (rc4_skip(ek, 32, 256, gd->gtk, keydatalen)) {
1134                         wpa_printf(MSG_ERROR, "WPA: RC4 failed");
1135                         return -1;
1136                 }
1137         } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1138                 if (keydatalen % 8) {
1139                         wpa_printf(MSG_WARNING, "WPA: Unsupported AES-WRAP "
1140                                    "len %lu", (unsigned long) keydatalen);
1141                         return -1;
1142                 }
1143                 if (maxkeylen > sizeof(gd->gtk)) {
1144                         wpa_printf(MSG_WARNING, "WPA: AES-WRAP key data "
1145                                    "too long (keydatalen=%lu maxkeylen=%lu)",
1146                                    (unsigned long) keydatalen,
1147                                    (unsigned long) maxkeylen);
1148                         return -1;
1149                 }
1150                 if (aes_unwrap(sm->ptk.kek, maxkeylen / 8,
1151                                (const u8 *) (key + 1), gd->gtk)) {
1152                         wpa_printf(MSG_WARNING, "WPA: AES unwrap "
1153                                    "failed - could not decrypt GTK");
1154                         return -1;
1155                 }
1156         } else {
1157                 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1158                            ver);
1159                 return -1;
1160         }
1161         gd->tx = wpa_supplicant_gtk_tx_bit_workaround(
1162                 sm, !!(key_info & WPA_KEY_INFO_TXRX));
1163         return 0;
1164 }
1165
1166
1167 static int wpa_supplicant_send_2_of_2(struct wpa_sm *sm,
1168                                       const struct wpa_eapol_key *key,
1169                                       int ver, u16 key_info)
1170 {
1171         size_t rlen;
1172         struct wpa_eapol_key *reply;
1173         u8 *rbuf;
1174
1175         rbuf = wpa_sm_alloc_eapol(sm, IEEE802_1X_TYPE_EAPOL_KEY, NULL,
1176                                   sizeof(*reply), &rlen, (void *) &reply);
1177         if (rbuf == NULL)
1178                 return -1;
1179
1180         reply->type = sm->proto == WPA_PROTO_RSN ?
1181                 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1182         key_info &= WPA_KEY_INFO_KEY_INDEX_MASK;
1183         key_info |= ver | WPA_KEY_INFO_MIC | WPA_KEY_INFO_SECURE;
1184         WPA_PUT_BE16(reply->key_info, key_info);
1185         if (sm->proto == WPA_PROTO_RSN)
1186                 WPA_PUT_BE16(reply->key_length, 0);
1187         else
1188                 os_memcpy(reply->key_length, key->key_length, 2);
1189         os_memcpy(reply->replay_counter, key->replay_counter,
1190                   WPA_REPLAY_COUNTER_LEN);
1191
1192         WPA_PUT_BE16(reply->key_data_length, 0);
1193
1194         wpa_printf(MSG_DEBUG, "WPA: Sending EAPOL-Key 2/2");
1195         wpa_eapol_key_send(sm, sm->ptk.kck, ver, sm->bssid, ETH_P_EAPOL,
1196                            rbuf, rlen, reply->key_mic);
1197
1198         return 0;
1199 }
1200
1201
1202 static void wpa_supplicant_process_1_of_2(struct wpa_sm *sm,
1203                                           const unsigned char *src_addr,
1204                                           const struct wpa_eapol_key *key,
1205                                           int extra_len, u16 ver)
1206 {
1207         u16 key_info, keydatalen;
1208         int rekey, ret;
1209         struct wpa_gtk_data gd;
1210
1211         os_memset(&gd, 0, sizeof(gd));
1212
1213         rekey = wpa_sm_get_state(sm) == WPA_COMPLETED;
1214         wpa_printf(MSG_DEBUG, "WPA: RX message 1 of Group Key Handshake from "
1215                    MACSTR " (ver=%d)", MAC2STR(src_addr), ver);
1216
1217         key_info = WPA_GET_BE16(key->key_info);
1218         keydatalen = WPA_GET_BE16(key->key_data_length);
1219
1220         if (sm->proto == WPA_PROTO_RSN) {
1221                 ret = wpa_supplicant_process_1_of_2_rsn(sm,
1222                                                         (const u8 *) (key + 1),
1223                                                         keydatalen, key_info,
1224                                                         &gd);
1225         } else {
1226                 ret = wpa_supplicant_process_1_of_2_wpa(sm, key, keydatalen,
1227                                                         key_info, extra_len,
1228                                                         ver, &gd);
1229         }
1230
1231         wpa_sm_set_state(sm, WPA_GROUP_HANDSHAKE);
1232
1233         if (ret)
1234                 goto failed;
1235
1236         if (wpa_supplicant_install_gtk(sm, &gd, key->key_rsc) ||
1237             wpa_supplicant_send_2_of_2(sm, key, ver, key_info))
1238                 goto failed;
1239
1240         if (rekey) {
1241                 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Group rekeying "
1242                         "completed with " MACSTR " [GTK=%s]",
1243                         MAC2STR(sm->bssid), wpa_cipher_txt(sm->group_cipher));
1244                 wpa_sm_cancel_auth_timeout(sm);
1245                 wpa_sm_set_state(sm, WPA_COMPLETED);
1246         } else {
1247                 wpa_supplicant_key_neg_complete(sm, sm->bssid,
1248                                                 key_info &
1249                                                 WPA_KEY_INFO_SECURE);
1250         }
1251         return;
1252
1253 failed:
1254         wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1255 }
1256
1257
1258 static int wpa_supplicant_verify_eapol_key_mic(struct wpa_sm *sm,
1259                                                struct wpa_eapol_key *key,
1260                                                u16 ver,
1261                                                const u8 *buf, size_t len)
1262 {
1263         u8 mic[16];
1264         int ok = 0;
1265
1266         os_memcpy(mic, key->key_mic, 16);
1267         if (sm->tptk_set) {
1268                 os_memset(key->key_mic, 0, 16);
1269                 wpa_eapol_key_mic(sm->tptk.kck, ver, buf, len,
1270                                   key->key_mic);
1271                 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1272                         wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1273                                    "when using TPTK - ignoring TPTK");
1274                 } else {
1275                         ok = 1;
1276                         sm->tptk_set = 0;
1277                         sm->ptk_set = 1;
1278                         os_memcpy(&sm->ptk, &sm->tptk, sizeof(sm->ptk));
1279                 }
1280         }
1281
1282         if (!ok && sm->ptk_set) {
1283                 os_memset(key->key_mic, 0, 16);
1284                 wpa_eapol_key_mic(sm->ptk.kck, ver, buf, len,
1285                                   key->key_mic);
1286                 if (os_memcmp(mic, key->key_mic, 16) != 0) {
1287                         wpa_printf(MSG_WARNING, "WPA: Invalid EAPOL-Key MIC "
1288                                    "- dropping packet");
1289                         return -1;
1290                 }
1291                 ok = 1;
1292         }
1293
1294         if (!ok) {
1295                 wpa_printf(MSG_WARNING, "WPA: Could not verify EAPOL-Key MIC "
1296                            "- dropping packet");
1297                 return -1;
1298         }
1299
1300         os_memcpy(sm->rx_replay_counter, key->replay_counter,
1301                   WPA_REPLAY_COUNTER_LEN);
1302         sm->rx_replay_counter_set = 1;
1303         return 0;
1304 }
1305
1306
1307 /* Decrypt RSN EAPOL-Key key data (RC4 or AES-WRAP) */
1308 static int wpa_supplicant_decrypt_key_data(struct wpa_sm *sm,
1309                                            struct wpa_eapol_key *key, u16 ver)
1310 {
1311         u16 keydatalen = WPA_GET_BE16(key->key_data_length);
1312
1313         wpa_hexdump(MSG_DEBUG, "RSN: encrypted key data",
1314                     (u8 *) (key + 1), keydatalen);
1315         if (!sm->ptk_set) {
1316                 wpa_printf(MSG_WARNING, "WPA: PTK not available, "
1317                            "cannot decrypt EAPOL-Key key data.");
1318                 return -1;
1319         }
1320
1321         /* Decrypt key data here so that this operation does not need
1322          * to be implemented separately for each message type. */
1323         if (ver == WPA_KEY_INFO_TYPE_HMAC_MD5_RC4) {
1324                 u8 ek[32];
1325                 os_memcpy(ek, key->key_iv, 16);
1326                 os_memcpy(ek + 16, sm->ptk.kek, 16);
1327                 if (rc4_skip(ek, 32, 256, (u8 *) (key + 1), keydatalen)) {
1328                         wpa_printf(MSG_ERROR, "WPA: RC4 failed");
1329                         return -1;
1330                 }
1331         } else if (ver == WPA_KEY_INFO_TYPE_HMAC_SHA1_AES ||
1332                    ver == WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1333                 u8 *buf;
1334                 if (keydatalen % 8) {
1335                         wpa_printf(MSG_WARNING, "WPA: Unsupported "
1336                                    "AES-WRAP len %d", keydatalen);
1337                         return -1;
1338                 }
1339                 keydatalen -= 8; /* AES-WRAP adds 8 bytes */
1340                 buf = os_malloc(keydatalen);
1341                 if (buf == NULL) {
1342                         wpa_printf(MSG_WARNING, "WPA: No memory for "
1343                                    "AES-UNWRAP buffer");
1344                         return -1;
1345                 }
1346                 if (aes_unwrap(sm->ptk.kek, keydatalen / 8,
1347                                (u8 *) (key + 1), buf)) {
1348                         os_free(buf);
1349                         wpa_printf(MSG_WARNING, "WPA: AES unwrap failed - "
1350                                    "could not decrypt EAPOL-Key key data");
1351                         return -1;
1352                 }
1353                 os_memcpy(key + 1, buf, keydatalen);
1354                 os_free(buf);
1355                 WPA_PUT_BE16(key->key_data_length, keydatalen);
1356         } else {
1357                 wpa_printf(MSG_WARNING, "WPA: Unsupported key_info type %d",
1358                            ver);
1359                 return -1;
1360         }
1361         wpa_hexdump_key(MSG_DEBUG, "WPA: decrypted EAPOL-Key key data",
1362                         (u8 *) (key + 1), keydatalen);
1363         return 0;
1364 }
1365
1366
1367 /**
1368  * wpa_sm_aborted_cached - Notify WPA that PMKSA caching was aborted
1369  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1370  */
1371 void wpa_sm_aborted_cached(struct wpa_sm *sm)
1372 {
1373         if (sm && sm->cur_pmksa) {
1374                 wpa_printf(MSG_DEBUG, "RSN: Cancelling PMKSA caching attempt");
1375                 sm->cur_pmksa = NULL;
1376         }
1377 }
1378
1379
1380 static void wpa_eapol_key_dump(const struct wpa_eapol_key *key)
1381 {
1382 #ifndef CONFIG_NO_STDOUT_DEBUG
1383         u16 key_info = WPA_GET_BE16(key->key_info);
1384
1385         wpa_printf(MSG_DEBUG, "  EAPOL-Key type=%d", key->type);
1386         wpa_printf(MSG_DEBUG, "  key_info 0x%x (ver=%d keyidx=%d rsvd=%d %s"
1387                    "%s%s%s%s%s%s%s)",
1388                    key_info, key_info & WPA_KEY_INFO_TYPE_MASK,
1389                    (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) >>
1390                    WPA_KEY_INFO_KEY_INDEX_SHIFT,
1391                    (key_info & (BIT(13) | BIT(14) | BIT(15))) >> 13,
1392                    key_info & WPA_KEY_INFO_KEY_TYPE ? "Pairwise" : "Group",
1393                    key_info & WPA_KEY_INFO_INSTALL ? " Install" : "",
1394                    key_info & WPA_KEY_INFO_ACK ? " Ack" : "",
1395                    key_info & WPA_KEY_INFO_MIC ? " MIC" : "",
1396                    key_info & WPA_KEY_INFO_SECURE ? " Secure" : "",
1397                    key_info & WPA_KEY_INFO_ERROR ? " Error" : "",
1398                    key_info & WPA_KEY_INFO_REQUEST ? " Request" : "",
1399                    key_info & WPA_KEY_INFO_ENCR_KEY_DATA ? " Encr" : "");
1400         wpa_printf(MSG_DEBUG, "  key_length=%u key_data_length=%u",
1401                    WPA_GET_BE16(key->key_length),
1402                    WPA_GET_BE16(key->key_data_length));
1403         wpa_hexdump(MSG_DEBUG, "  replay_counter",
1404                     key->replay_counter, WPA_REPLAY_COUNTER_LEN);
1405         wpa_hexdump(MSG_DEBUG, "  key_nonce", key->key_nonce, WPA_NONCE_LEN);
1406         wpa_hexdump(MSG_DEBUG, "  key_iv", key->key_iv, 16);
1407         wpa_hexdump(MSG_DEBUG, "  key_rsc", key->key_rsc, 8);
1408         wpa_hexdump(MSG_DEBUG, "  key_id (reserved)", key->key_id, 8);
1409         wpa_hexdump(MSG_DEBUG, "  key_mic", key->key_mic, 16);
1410 #endif /* CONFIG_NO_STDOUT_DEBUG */
1411 }
1412
1413
1414 /**
1415  * wpa_sm_rx_eapol - Process received WPA EAPOL frames
1416  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1417  * @src_addr: Source MAC address of the EAPOL packet
1418  * @buf: Pointer to the beginning of the EAPOL data (EAPOL header)
1419  * @len: Length of the EAPOL frame
1420  * Returns: 1 = WPA EAPOL-Key processed, 0 = not a WPA EAPOL-Key, -1 failure
1421  *
1422  * This function is called for each received EAPOL frame. Other than EAPOL-Key
1423  * frames can be skipped if filtering is done elsewhere. wpa_sm_rx_eapol() is
1424  * only processing WPA and WPA2 EAPOL-Key frames.
1425  *
1426  * The received EAPOL-Key packets are validated and valid packets are replied
1427  * to. In addition, key material (PTK, GTK) is configured at the end of a
1428  * successful key handshake.
1429  */
1430 int wpa_sm_rx_eapol(struct wpa_sm *sm, const u8 *src_addr,
1431                     const u8 *buf, size_t len)
1432 {
1433         size_t plen, data_len, extra_len;
1434         struct ieee802_1x_hdr *hdr;
1435         struct wpa_eapol_key *key;
1436         u16 key_info, ver;
1437         u8 *tmp;
1438         int ret = -1;
1439         struct wpa_peerkey *peerkey = NULL;
1440
1441 #ifdef CONFIG_IEEE80211R
1442         sm->ft_completed = 0;
1443 #endif /* CONFIG_IEEE80211R */
1444
1445         if (len < sizeof(*hdr) + sizeof(*key)) {
1446                 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame too short to be a WPA "
1447                            "EAPOL-Key (len %lu, expecting at least %lu)",
1448                            (unsigned long) len,
1449                            (unsigned long) sizeof(*hdr) + sizeof(*key));
1450                 return 0;
1451         }
1452
1453         tmp = os_malloc(len);
1454         if (tmp == NULL)
1455                 return -1;
1456         os_memcpy(tmp, buf, len);
1457
1458         hdr = (struct ieee802_1x_hdr *) tmp;
1459         key = (struct wpa_eapol_key *) (hdr + 1);
1460         plen = be_to_host16(hdr->length);
1461         data_len = plen + sizeof(*hdr);
1462         wpa_printf(MSG_DEBUG, "IEEE 802.1X RX: version=%d type=%d length=%lu",
1463                    hdr->version, hdr->type, (unsigned long) plen);
1464
1465         if (hdr->version < EAPOL_VERSION) {
1466                 /* TODO: backwards compatibility */
1467         }
1468         if (hdr->type != IEEE802_1X_TYPE_EAPOL_KEY) {
1469                 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame (type %u) discarded, "
1470                         "not a Key frame", hdr->type);
1471                 ret = 0;
1472                 goto out;
1473         }
1474         if (plen > len - sizeof(*hdr) || plen < sizeof(*key)) {
1475                 wpa_printf(MSG_DEBUG, "WPA: EAPOL frame payload size %lu "
1476                            "invalid (frame size %lu)",
1477                            (unsigned long) plen, (unsigned long) len);
1478                 ret = 0;
1479                 goto out;
1480         }
1481
1482         if (key->type != EAPOL_KEY_TYPE_WPA && key->type != EAPOL_KEY_TYPE_RSN)
1483         {
1484                 wpa_printf(MSG_DEBUG, "WPA: EAPOL-Key type (%d) unknown, "
1485                            "discarded", key->type);
1486                 ret = 0;
1487                 goto out;
1488         }
1489         wpa_eapol_key_dump(key);
1490
1491         eapol_sm_notify_lower_layer_success(sm->eapol, 0);
1492         wpa_hexdump(MSG_MSGDUMP, "WPA: RX EAPOL-Key", tmp, len);
1493         if (data_len < len) {
1494                 wpa_printf(MSG_DEBUG, "WPA: ignoring %lu bytes after the IEEE "
1495                            "802.1X data", (unsigned long) len - data_len);
1496         }
1497         key_info = WPA_GET_BE16(key->key_info);
1498         ver = key_info & WPA_KEY_INFO_TYPE_MASK;
1499         if (ver != WPA_KEY_INFO_TYPE_HMAC_MD5_RC4 &&
1500 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
1501             ver != WPA_KEY_INFO_TYPE_AES_128_CMAC &&
1502 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
1503             ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1504                 wpa_printf(MSG_INFO, "WPA: Unsupported EAPOL-Key descriptor "
1505                            "version %d.", ver);
1506                 goto out;
1507         }
1508
1509 #ifdef CONFIG_IEEE80211R
1510         if (wpa_key_mgmt_ft(sm->key_mgmt)) {
1511                 /* IEEE 802.11r uses a new key_info type (AES-128-CMAC). */
1512                 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1513                         wpa_printf(MSG_INFO, "FT: AP did not use "
1514                                    "AES-128-CMAC.");
1515                         goto out;
1516                 }
1517         } else
1518 #endif /* CONFIG_IEEE80211R */
1519 #ifdef CONFIG_IEEE80211W
1520         if (wpa_key_mgmt_sha256(sm->key_mgmt)) {
1521                 if (ver != WPA_KEY_INFO_TYPE_AES_128_CMAC) {
1522                         wpa_printf(MSG_INFO, "WPA: AP did not use the "
1523                                    "negotiated AES-128-CMAC.");
1524                         goto out;
1525                 }
1526         } else
1527 #endif /* CONFIG_IEEE80211W */
1528         if (sm->pairwise_cipher == WPA_CIPHER_CCMP &&
1529             ver != WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1530                 wpa_printf(MSG_INFO, "WPA: CCMP is used, but EAPOL-Key "
1531                            "descriptor version (%d) is not 2.", ver);
1532                 if (sm->group_cipher != WPA_CIPHER_CCMP &&
1533                     !(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1534                         /* Earlier versions of IEEE 802.11i did not explicitly
1535                          * require version 2 descriptor for all EAPOL-Key
1536                          * packets, so allow group keys to use version 1 if
1537                          * CCMP is not used for them. */
1538                         wpa_printf(MSG_INFO, "WPA: Backwards compatibility: "
1539                                    "allow invalid version for non-CCMP group "
1540                                    "keys");
1541                 } else
1542                         goto out;
1543         }
1544
1545 #ifdef CONFIG_PEERKEY
1546         for (peerkey = sm->peerkey; peerkey; peerkey = peerkey->next) {
1547                 if (os_memcmp(peerkey->addr, src_addr, ETH_ALEN) == 0)
1548                         break;
1549         }
1550
1551         if (!(key_info & WPA_KEY_INFO_SMK_MESSAGE) && peerkey) {
1552                 if (!peerkey->initiator && peerkey->replay_counter_set &&
1553                     os_memcmp(key->replay_counter, peerkey->replay_counter,
1554                               WPA_REPLAY_COUNTER_LEN) <= 0) {
1555                         wpa_printf(MSG_WARNING, "RSN: EAPOL-Key Replay "
1556                                    "Counter did not increase (STK) - dropping "
1557                                    "packet");
1558                         goto out;
1559                 } else if (peerkey->initiator) {
1560                         u8 _tmp[WPA_REPLAY_COUNTER_LEN];
1561                         os_memcpy(_tmp, key->replay_counter,
1562                                   WPA_REPLAY_COUNTER_LEN);
1563                         inc_byte_array(_tmp, WPA_REPLAY_COUNTER_LEN);
1564                         if (os_memcmp(_tmp, peerkey->replay_counter,
1565                                       WPA_REPLAY_COUNTER_LEN) != 0) {
1566                                 wpa_printf(MSG_DEBUG, "RSN: EAPOL-Key Replay "
1567                                            "Counter did not match (STK) - "
1568                                            "dropping packet");
1569                                 goto out;
1570                         }
1571                 }
1572         }
1573
1574         if (peerkey && peerkey->initiator && (key_info & WPA_KEY_INFO_ACK)) {
1575                 wpa_printf(MSG_INFO, "RSN: Ack bit in key_info from STK peer");
1576                 goto out;
1577         }
1578 #endif /* CONFIG_PEERKEY */
1579
1580         if (!peerkey && sm->rx_replay_counter_set &&
1581             os_memcmp(key->replay_counter, sm->rx_replay_counter,
1582                       WPA_REPLAY_COUNTER_LEN) <= 0) {
1583                 wpa_printf(MSG_WARNING, "WPA: EAPOL-Key Replay Counter did not"
1584                            " increase - dropping packet");
1585                 goto out;
1586         }
1587
1588         if (!(key_info & (WPA_KEY_INFO_ACK | WPA_KEY_INFO_SMK_MESSAGE))
1589 #ifdef CONFIG_PEERKEY
1590             && (peerkey == NULL || !peerkey->initiator)
1591 #endif /* CONFIG_PEERKEY */
1592                 ) {
1593                 wpa_printf(MSG_INFO, "WPA: No Ack bit in key_info");
1594                 goto out;
1595         }
1596
1597         if (key_info & WPA_KEY_INFO_REQUEST) {
1598                 wpa_printf(MSG_INFO, "WPA: EAPOL-Key with Request bit - "
1599                            "dropped");
1600                 goto out;
1601         }
1602
1603         if ((key_info & WPA_KEY_INFO_MIC) && !peerkey &&
1604             wpa_supplicant_verify_eapol_key_mic(sm, key, ver, tmp, data_len))
1605                 goto out;
1606
1607 #ifdef CONFIG_PEERKEY
1608         if ((key_info & WPA_KEY_INFO_MIC) && peerkey &&
1609             peerkey_verify_eapol_key_mic(sm, peerkey, key, ver, tmp, data_len))
1610                 goto out;
1611 #endif /* CONFIG_PEERKEY */
1612
1613         extra_len = data_len - sizeof(*hdr) - sizeof(*key);
1614
1615         if (WPA_GET_BE16(key->key_data_length) > extra_len) {
1616                 wpa_msg(sm->ctx->msg_ctx, MSG_INFO, "WPA: Invalid EAPOL-Key "
1617                         "frame - key_data overflow (%d > %lu)",
1618                         WPA_GET_BE16(key->key_data_length),
1619                         (unsigned long) extra_len);
1620                 goto out;
1621         }
1622         extra_len = WPA_GET_BE16(key->key_data_length);
1623
1624         if (sm->proto == WPA_PROTO_RSN &&
1625             (key_info & WPA_KEY_INFO_ENCR_KEY_DATA)) {
1626                 if (wpa_supplicant_decrypt_key_data(sm, key, ver))
1627                         goto out;
1628                 extra_len = WPA_GET_BE16(key->key_data_length);
1629         }
1630
1631         if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1632                 if (key_info & WPA_KEY_INFO_KEY_INDEX_MASK) {
1633                         wpa_printf(MSG_WARNING, "WPA: Ignored EAPOL-Key "
1634                                    "(Pairwise) with non-zero key index");
1635                         goto out;
1636                 }
1637                 if (peerkey) {
1638                         /* PeerKey 4-Way Handshake */
1639                         peerkey_rx_eapol_4way(sm, peerkey, key, key_info, ver);
1640                 } else if (key_info & WPA_KEY_INFO_MIC) {
1641                         /* 3/4 4-Way Handshake */
1642                         wpa_supplicant_process_3_of_4(sm, key, ver);
1643                 } else {
1644                         /* 1/4 4-Way Handshake */
1645                         wpa_supplicant_process_1_of_4(sm, src_addr, key,
1646                                                       ver);
1647                 }
1648         } else if (key_info & WPA_KEY_INFO_SMK_MESSAGE) {
1649                 /* PeerKey SMK Handshake */
1650                 peerkey_rx_eapol_smk(sm, src_addr, key, extra_len, key_info,
1651                                      ver);
1652         } else {
1653                 if (key_info & WPA_KEY_INFO_MIC) {
1654                         /* 1/2 Group Key Handshake */
1655                         wpa_supplicant_process_1_of_2(sm, src_addr, key,
1656                                                       extra_len, ver);
1657                 } else {
1658                         wpa_printf(MSG_WARNING, "WPA: EAPOL-Key (Group) "
1659                                    "without Mic bit - dropped");
1660                 }
1661         }
1662
1663         ret = 1;
1664
1665 out:
1666         os_free(tmp);
1667         return ret;
1668 }
1669
1670
1671 #ifdef CONFIG_CTRL_IFACE
1672 static int wpa_cipher_bits(int cipher)
1673 {
1674         switch (cipher) {
1675         case WPA_CIPHER_CCMP:
1676                 return 128;
1677         case WPA_CIPHER_TKIP:
1678                 return 256;
1679         case WPA_CIPHER_WEP104:
1680                 return 104;
1681         case WPA_CIPHER_WEP40:
1682                 return 40;
1683         default:
1684                 return 0;
1685         }
1686 }
1687
1688
1689 static u32 wpa_key_mgmt_suite(struct wpa_sm *sm)
1690 {
1691         switch (sm->key_mgmt) {
1692         case WPA_KEY_MGMT_IEEE8021X:
1693                 return (sm->proto == WPA_PROTO_RSN ?
1694                         RSN_AUTH_KEY_MGMT_UNSPEC_802_1X :
1695                         WPA_AUTH_KEY_MGMT_UNSPEC_802_1X);
1696         case WPA_KEY_MGMT_PSK:
1697                 return (sm->proto == WPA_PROTO_RSN ?
1698                         RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X :
1699                         WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X);
1700 #ifdef CONFIG_IEEE80211R
1701         case WPA_KEY_MGMT_FT_IEEE8021X:
1702                 return RSN_AUTH_KEY_MGMT_FT_802_1X;
1703         case WPA_KEY_MGMT_FT_PSK:
1704                 return RSN_AUTH_KEY_MGMT_FT_PSK;
1705 #endif /* CONFIG_IEEE80211R */
1706 #ifdef CONFIG_IEEE80211W
1707         case WPA_KEY_MGMT_IEEE8021X_SHA256:
1708                 return RSN_AUTH_KEY_MGMT_802_1X_SHA256;
1709         case WPA_KEY_MGMT_PSK_SHA256:
1710                 return RSN_AUTH_KEY_MGMT_PSK_SHA256;
1711 #endif /* CONFIG_IEEE80211W */
1712         case WPA_KEY_MGMT_WPA_NONE:
1713                 return WPA_AUTH_KEY_MGMT_NONE;
1714         default:
1715                 return 0;
1716         }
1717 }
1718
1719
1720 static u32 wpa_cipher_suite(struct wpa_sm *sm, int cipher)
1721 {
1722         switch (cipher) {
1723         case WPA_CIPHER_CCMP:
1724                 return (sm->proto == WPA_PROTO_RSN ?
1725                         RSN_CIPHER_SUITE_CCMP : WPA_CIPHER_SUITE_CCMP);
1726         case WPA_CIPHER_TKIP:
1727                 return (sm->proto == WPA_PROTO_RSN ?
1728                         RSN_CIPHER_SUITE_TKIP : WPA_CIPHER_SUITE_TKIP);
1729         case WPA_CIPHER_WEP104:
1730                 return (sm->proto == WPA_PROTO_RSN ?
1731                         RSN_CIPHER_SUITE_WEP104 : WPA_CIPHER_SUITE_WEP104);
1732         case WPA_CIPHER_WEP40:
1733                 return (sm->proto == WPA_PROTO_RSN ?
1734                         RSN_CIPHER_SUITE_WEP40 : WPA_CIPHER_SUITE_WEP40);
1735         case WPA_CIPHER_NONE:
1736                 return (sm->proto == WPA_PROTO_RSN ?
1737                         RSN_CIPHER_SUITE_NONE : WPA_CIPHER_SUITE_NONE);
1738         default:
1739                 return 0;
1740         }
1741 }
1742
1743
1744 #define RSN_SUITE "%02x-%02x-%02x-%d"
1745 #define RSN_SUITE_ARG(s) \
1746 ((s) >> 24) & 0xff, ((s) >> 16) & 0xff, ((s) >> 8) & 0xff, (s) & 0xff
1747
1748 /**
1749  * wpa_sm_get_mib - Dump text list of MIB entries
1750  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1751  * @buf: Buffer for the list
1752  * @buflen: Length of the buffer
1753  * Returns: Number of bytes written to buffer
1754  *
1755  * This function is used fetch dot11 MIB variables.
1756  */
1757 int wpa_sm_get_mib(struct wpa_sm *sm, char *buf, size_t buflen)
1758 {
1759         char pmkid_txt[PMKID_LEN * 2 + 1];
1760         int rsna, ret;
1761         size_t len;
1762
1763         if (sm->cur_pmksa) {
1764                 wpa_snprintf_hex(pmkid_txt, sizeof(pmkid_txt),
1765                                  sm->cur_pmksa->pmkid, PMKID_LEN);
1766         } else
1767                 pmkid_txt[0] = '\0';
1768
1769         if ((wpa_key_mgmt_wpa_psk(sm->key_mgmt) ||
1770              wpa_key_mgmt_wpa_ieee8021x(sm->key_mgmt)) &&
1771             sm->proto == WPA_PROTO_RSN)
1772                 rsna = 1;
1773         else
1774                 rsna = 0;
1775
1776         ret = os_snprintf(buf, buflen,
1777                           "dot11RSNAOptionImplemented=TRUE\n"
1778                           "dot11RSNAPreauthenticationImplemented=TRUE\n"
1779                           "dot11RSNAEnabled=%s\n"
1780                           "dot11RSNAPreauthenticationEnabled=%s\n"
1781                           "dot11RSNAConfigVersion=%d\n"
1782                           "dot11RSNAConfigPairwiseKeysSupported=5\n"
1783                           "dot11RSNAConfigGroupCipherSize=%d\n"
1784                           "dot11RSNAConfigPMKLifetime=%d\n"
1785                           "dot11RSNAConfigPMKReauthThreshold=%d\n"
1786                           "dot11RSNAConfigNumberOfPTKSAReplayCounters=1\n"
1787                           "dot11RSNAConfigSATimeout=%d\n",
1788                           rsna ? "TRUE" : "FALSE",
1789                           rsna ? "TRUE" : "FALSE",
1790                           RSN_VERSION,
1791                           wpa_cipher_bits(sm->group_cipher),
1792                           sm->dot11RSNAConfigPMKLifetime,
1793                           sm->dot11RSNAConfigPMKReauthThreshold,
1794                           sm->dot11RSNAConfigSATimeout);
1795         if (ret < 0 || (size_t) ret >= buflen)
1796                 return 0;
1797         len = ret;
1798
1799         ret = os_snprintf(
1800                 buf + len, buflen - len,
1801                 "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
1802                 "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
1803                 "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
1804                 "dot11RSNAPMKIDUsed=%s\n"
1805                 "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
1806                 "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
1807                 "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
1808                 "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n"
1809                 "dot11RSNA4WayHandshakeFailures=%u\n",
1810                 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1811                 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1812                 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1813                 pmkid_txt,
1814                 RSN_SUITE_ARG(wpa_key_mgmt_suite(sm)),
1815                 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->pairwise_cipher)),
1816                 RSN_SUITE_ARG(wpa_cipher_suite(sm, sm->group_cipher)),
1817                 sm->dot11RSNA4WayHandshakeFailures);
1818         if (ret >= 0 && (size_t) ret < buflen)
1819                 len += ret;
1820
1821         return (int) len;
1822 }
1823 #endif /* CONFIG_CTRL_IFACE */
1824
1825
1826 static void wpa_sm_pmksa_free_cb(struct rsn_pmksa_cache_entry *entry,
1827                                  void *ctx, int replace)
1828 {
1829         struct wpa_sm *sm = ctx;
1830
1831         if (sm->cur_pmksa == entry ||
1832             (sm->pmk_len == entry->pmk_len &&
1833              os_memcmp(sm->pmk, entry->pmk, sm->pmk_len) == 0)) {
1834                 wpa_printf(MSG_DEBUG, "RSN: removed current PMKSA entry");
1835                 sm->cur_pmksa = NULL;
1836
1837                 if (replace) {
1838                         /* A new entry is being added, so no need to
1839                          * deauthenticate in this case. This happens when EAP
1840                          * authentication is completed again (reauth or failed
1841                          * PMKSA caching attempt). */
1842                         return;
1843                 }
1844
1845                 os_memset(sm->pmk, 0, sizeof(sm->pmk));
1846                 wpa_sm_deauthenticate(sm, WLAN_REASON_UNSPECIFIED);
1847         }
1848 }
1849
1850
1851 /**
1852  * wpa_sm_init - Initialize WPA state machine
1853  * @ctx: Context pointer for callbacks; this needs to be an allocated buffer
1854  * Returns: Pointer to the allocated WPA state machine data
1855  *
1856  * This function is used to allocate a new WPA state machine and the returned
1857  * value is passed to all WPA state machine calls.
1858  */
1859 struct wpa_sm * wpa_sm_init(struct wpa_sm_ctx *ctx)
1860 {
1861         struct wpa_sm *sm;
1862
1863         sm = os_zalloc(sizeof(*sm));
1864         if (sm == NULL)
1865                 return NULL;
1866         dl_list_init(&sm->pmksa_candidates);
1867         sm->renew_snonce = 1;
1868         sm->ctx = ctx;
1869
1870         sm->dot11RSNAConfigPMKLifetime = 43200;
1871         sm->dot11RSNAConfigPMKReauthThreshold = 70;
1872         sm->dot11RSNAConfigSATimeout = 60;
1873
1874         sm->pmksa = pmksa_cache_init(wpa_sm_pmksa_free_cb, sm, sm);
1875         if (sm->pmksa == NULL) {
1876                 wpa_printf(MSG_ERROR, "RSN: PMKSA cache initialization "
1877                            "failed");
1878                 os_free(sm);
1879                 return NULL;
1880         }
1881
1882         return sm;
1883 }
1884
1885
1886 /**
1887  * wpa_sm_deinit - Deinitialize WPA state machine
1888  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1889  */
1890 void wpa_sm_deinit(struct wpa_sm *sm)
1891 {
1892         if (sm == NULL)
1893                 return;
1894         pmksa_cache_deinit(sm->pmksa);
1895         eloop_cancel_timeout(wpa_sm_start_preauth, sm, NULL);
1896         eloop_cancel_timeout(wpa_sm_rekey_ptk, sm, NULL);
1897         os_free(sm->assoc_wpa_ie);
1898         os_free(sm->ap_wpa_ie);
1899         os_free(sm->ap_rsn_ie);
1900         os_free(sm->ctx);
1901         peerkey_deinit(sm);
1902         os_free(sm);
1903 }
1904
1905
1906 /**
1907  * wpa_sm_notify_assoc - Notify WPA state machine about association
1908  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1909  * @bssid: The BSSID of the new association
1910  *
1911  * This function is called to let WPA state machine know that the connection
1912  * was established.
1913  */
1914 void wpa_sm_notify_assoc(struct wpa_sm *sm, const u8 *bssid)
1915 {
1916         int clear_ptk = 1;
1917
1918         if (sm == NULL)
1919                 return;
1920
1921         wpa_printf(MSG_DEBUG, "WPA: Association event - clear replay counter");
1922         os_memcpy(sm->bssid, bssid, ETH_ALEN);
1923         os_memset(sm->rx_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
1924         sm->rx_replay_counter_set = 0;
1925         sm->renew_snonce = 1;
1926         if (os_memcmp(sm->preauth_bssid, bssid, ETH_ALEN) == 0)
1927                 rsn_preauth_deinit(sm);
1928
1929 #ifdef CONFIG_IEEE80211R
1930         if (wpa_ft_is_completed(sm)) {
1931                 /*
1932                  * Clear portValid to kick EAPOL state machine to re-enter
1933                  * AUTHENTICATED state to get the EAPOL port Authorized.
1934                  */
1935                 eapol_sm_notify_portValid(sm->eapol, FALSE);
1936                 wpa_supplicant_key_neg_complete(sm, sm->bssid, 1);
1937
1938                 /* Prepare for the next transition */
1939                 wpa_ft_prepare_auth_request(sm);
1940
1941                 clear_ptk = 0;
1942         }
1943 #endif /* CONFIG_IEEE80211R */
1944
1945         if (clear_ptk) {
1946                 /*
1947                  * IEEE 802.11, 8.4.10: Delete PTK SA on (re)association if
1948                  * this is not part of a Fast BSS Transition.
1949                  */
1950                 wpa_printf(MSG_DEBUG, "WPA: Clear old PTK");
1951                 sm->ptk_set = 0;
1952                 sm->tptk_set = 0;
1953         }
1954 }
1955
1956
1957 /**
1958  * wpa_sm_notify_disassoc - Notify WPA state machine about disassociation
1959  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1960  *
1961  * This function is called to let WPA state machine know that the connection
1962  * was lost. This will abort any existing pre-authentication session.
1963  */
1964 void wpa_sm_notify_disassoc(struct wpa_sm *sm)
1965 {
1966         rsn_preauth_deinit(sm);
1967         if (wpa_sm_get_state(sm) == WPA_4WAY_HANDSHAKE)
1968                 sm->dot11RSNA4WayHandshakeFailures++;
1969 }
1970
1971
1972 /**
1973  * wpa_sm_set_pmk - Set PMK
1974  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1975  * @pmk: The new PMK
1976  * @pmk_len: The length of the new PMK in bytes
1977  *
1978  * Configure the PMK for WPA state machine.
1979  */
1980 void wpa_sm_set_pmk(struct wpa_sm *sm, const u8 *pmk, size_t pmk_len)
1981 {
1982         if (sm == NULL)
1983                 return;
1984
1985         sm->pmk_len = pmk_len;
1986         os_memcpy(sm->pmk, pmk, pmk_len);
1987
1988 #ifdef CONFIG_IEEE80211R
1989         /* Set XXKey to be PSK for FT key derivation */
1990         sm->xxkey_len = pmk_len;
1991         os_memcpy(sm->xxkey, pmk, pmk_len);
1992 #endif /* CONFIG_IEEE80211R */
1993 }
1994
1995
1996 /**
1997  * wpa_sm_set_pmk_from_pmksa - Set PMK based on the current PMKSA
1998  * @sm: Pointer to WPA state machine data from wpa_sm_init()
1999  *
2000  * Take the PMK from the current PMKSA into use. If no PMKSA is active, the PMK
2001  * will be cleared.
2002  */
2003 void wpa_sm_set_pmk_from_pmksa(struct wpa_sm *sm)
2004 {
2005         if (sm == NULL)
2006                 return;
2007
2008         if (sm->cur_pmksa) {
2009                 sm->pmk_len = sm->cur_pmksa->pmk_len;
2010                 os_memcpy(sm->pmk, sm->cur_pmksa->pmk, sm->pmk_len);
2011         } else {
2012                 sm->pmk_len = PMK_LEN;
2013                 os_memset(sm->pmk, 0, PMK_LEN);
2014         }
2015 }
2016
2017
2018 /**
2019  * wpa_sm_set_fast_reauth - Set fast reauthentication (EAP) enabled/disabled
2020  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2021  * @fast_reauth: Whether fast reauthentication (EAP) is allowed
2022  */
2023 void wpa_sm_set_fast_reauth(struct wpa_sm *sm, int fast_reauth)
2024 {
2025         if (sm)
2026                 sm->fast_reauth = fast_reauth;
2027 }
2028
2029
2030 /**
2031  * wpa_sm_set_scard_ctx - Set context pointer for smartcard callbacks
2032  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2033  * @scard_ctx: Context pointer for smartcard related callback functions
2034  */
2035 void wpa_sm_set_scard_ctx(struct wpa_sm *sm, void *scard_ctx)
2036 {
2037         if (sm == NULL)
2038                 return;
2039         sm->scard_ctx = scard_ctx;
2040         if (sm->preauth_eapol)
2041                 eapol_sm_register_scard_ctx(sm->preauth_eapol, scard_ctx);
2042 }
2043
2044
2045 /**
2046  * wpa_sm_set_config - Notification of current configration change
2047  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2048  * @config: Pointer to current network configuration
2049  *
2050  * Notify WPA state machine that configuration has changed. config will be
2051  * stored as a backpointer to network configuration. This can be %NULL to clear
2052  * the stored pointed.
2053  */
2054 void wpa_sm_set_config(struct wpa_sm *sm, struct rsn_supp_config *config)
2055 {
2056         if (!sm)
2057                 return;
2058
2059         if (config) {
2060                 sm->network_ctx = config->network_ctx;
2061                 sm->peerkey_enabled = config->peerkey_enabled;
2062                 sm->allowed_pairwise_cipher = config->allowed_pairwise_cipher;
2063                 sm->proactive_key_caching = config->proactive_key_caching;
2064                 sm->eap_workaround = config->eap_workaround;
2065                 sm->eap_conf_ctx = config->eap_conf_ctx;
2066                 if (config->ssid) {
2067                         os_memcpy(sm->ssid, config->ssid, config->ssid_len);
2068                         sm->ssid_len = config->ssid_len;
2069                 } else
2070                         sm->ssid_len = 0;
2071                 sm->wpa_ptk_rekey = config->wpa_ptk_rekey;
2072         } else {
2073                 sm->network_ctx = NULL;
2074                 sm->peerkey_enabled = 0;
2075                 sm->allowed_pairwise_cipher = 0;
2076                 sm->proactive_key_caching = 0;
2077                 sm->eap_workaround = 0;
2078                 sm->eap_conf_ctx = NULL;
2079                 sm->ssid_len = 0;
2080                 sm->wpa_ptk_rekey = 0;
2081         }
2082         if (config == NULL || config->network_ctx != sm->network_ctx)
2083                 pmksa_cache_notify_reconfig(sm->pmksa);
2084 }
2085
2086
2087 /**
2088  * wpa_sm_set_own_addr - Set own MAC address
2089  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2090  * @addr: Own MAC address
2091  */
2092 void wpa_sm_set_own_addr(struct wpa_sm *sm, const u8 *addr)
2093 {
2094         if (sm)
2095                 os_memcpy(sm->own_addr, addr, ETH_ALEN);
2096 }
2097
2098
2099 /**
2100  * wpa_sm_set_ifname - Set network interface name
2101  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2102  * @ifname: Interface name
2103  * @bridge_ifname: Optional bridge interface name (for pre-auth)
2104  */
2105 void wpa_sm_set_ifname(struct wpa_sm *sm, const char *ifname,
2106                        const char *bridge_ifname)
2107 {
2108         if (sm) {
2109                 sm->ifname = ifname;
2110                 sm->bridge_ifname = bridge_ifname;
2111         }
2112 }
2113
2114
2115 /**
2116  * wpa_sm_set_eapol - Set EAPOL state machine pointer
2117  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2118  * @eapol: Pointer to EAPOL state machine allocated with eapol_sm_init()
2119  */
2120 void wpa_sm_set_eapol(struct wpa_sm *sm, struct eapol_sm *eapol)
2121 {
2122         if (sm)
2123                 sm->eapol = eapol;
2124 }
2125
2126
2127 /**
2128  * wpa_sm_set_param - Set WPA state machine parameters
2129  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2130  * @param: Parameter field
2131  * @value: Parameter value
2132  * Returns: 0 on success, -1 on failure
2133  */
2134 int wpa_sm_set_param(struct wpa_sm *sm, enum wpa_sm_conf_params param,
2135                      unsigned int value)
2136 {
2137         int ret = 0;
2138
2139         if (sm == NULL)
2140                 return -1;
2141
2142         switch (param) {
2143         case RSNA_PMK_LIFETIME:
2144                 if (value > 0)
2145                         sm->dot11RSNAConfigPMKLifetime = value;
2146                 else
2147                         ret = -1;
2148                 break;
2149         case RSNA_PMK_REAUTH_THRESHOLD:
2150                 if (value > 0 && value <= 100)
2151                         sm->dot11RSNAConfigPMKReauthThreshold = value;
2152                 else
2153                         ret = -1;
2154                 break;
2155         case RSNA_SA_TIMEOUT:
2156                 if (value > 0)
2157                         sm->dot11RSNAConfigSATimeout = value;
2158                 else
2159                         ret = -1;
2160                 break;
2161         case WPA_PARAM_PROTO:
2162                 sm->proto = value;
2163                 break;
2164         case WPA_PARAM_PAIRWISE:
2165                 sm->pairwise_cipher = value;
2166                 break;
2167         case WPA_PARAM_GROUP:
2168                 sm->group_cipher = value;
2169                 break;
2170         case WPA_PARAM_KEY_MGMT:
2171                 sm->key_mgmt = value;
2172                 break;
2173 #ifdef CONFIG_IEEE80211W
2174         case WPA_PARAM_MGMT_GROUP:
2175                 sm->mgmt_group_cipher = value;
2176                 break;
2177 #endif /* CONFIG_IEEE80211W */
2178         case WPA_PARAM_RSN_ENABLED:
2179                 sm->rsn_enabled = value;
2180                 break;
2181         case WPA_PARAM_MFP:
2182                 sm->mfp = value;
2183                 break;
2184         default:
2185                 break;
2186         }
2187
2188         return ret;
2189 }
2190
2191
2192 /**
2193  * wpa_sm_get_param - Get WPA state machine parameters
2194  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2195  * @param: Parameter field
2196  * Returns: Parameter value
2197  */
2198 unsigned int wpa_sm_get_param(struct wpa_sm *sm, enum wpa_sm_conf_params param)
2199 {
2200         if (sm == NULL)
2201                 return 0;
2202
2203         switch (param) {
2204         case RSNA_PMK_LIFETIME:
2205                 return sm->dot11RSNAConfigPMKLifetime;
2206         case RSNA_PMK_REAUTH_THRESHOLD:
2207                 return sm->dot11RSNAConfigPMKReauthThreshold;
2208         case RSNA_SA_TIMEOUT:
2209                 return sm->dot11RSNAConfigSATimeout;
2210         case WPA_PARAM_PROTO:
2211                 return sm->proto;
2212         case WPA_PARAM_PAIRWISE:
2213                 return sm->pairwise_cipher;
2214         case WPA_PARAM_GROUP:
2215                 return sm->group_cipher;
2216         case WPA_PARAM_KEY_MGMT:
2217                 return sm->key_mgmt;
2218 #ifdef CONFIG_IEEE80211W
2219         case WPA_PARAM_MGMT_GROUP:
2220                 return sm->mgmt_group_cipher;
2221 #endif /* CONFIG_IEEE80211W */
2222         case WPA_PARAM_RSN_ENABLED:
2223                 return sm->rsn_enabled;
2224         default:
2225                 return 0;
2226         }
2227 }
2228
2229
2230 /**
2231  * wpa_sm_get_status - Get WPA state machine
2232  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2233  * @buf: Buffer for status information
2234  * @buflen: Maximum buffer length
2235  * @verbose: Whether to include verbose status information
2236  * Returns: Number of bytes written to buf.
2237  *
2238  * Query WPA state machine for status information. This function fills in
2239  * a text area with current status information. If the buffer (buf) is not
2240  * large enough, status information will be truncated to fit the buffer.
2241  */
2242 int wpa_sm_get_status(struct wpa_sm *sm, char *buf, size_t buflen,
2243                       int verbose)
2244 {
2245         char *pos = buf, *end = buf + buflen;
2246         int ret;
2247
2248         ret = os_snprintf(pos, end - pos,
2249                           "pairwise_cipher=%s\n"
2250                           "group_cipher=%s\n"
2251                           "key_mgmt=%s\n",
2252                           wpa_cipher_txt(sm->pairwise_cipher),
2253                           wpa_cipher_txt(sm->group_cipher),
2254                           wpa_key_mgmt_txt(sm->key_mgmt, sm->proto));
2255         if (ret < 0 || ret >= end - pos)
2256                 return pos - buf;
2257         pos += ret;
2258         return pos - buf;
2259 }
2260
2261
2262 /**
2263  * wpa_sm_set_assoc_wpa_ie_default - Generate own WPA/RSN IE from configuration
2264  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2265  * @wpa_ie: Pointer to buffer for WPA/RSN IE
2266  * @wpa_ie_len: Pointer to the length of the wpa_ie buffer
2267  * Returns: 0 on success, -1 on failure
2268  */
2269 int wpa_sm_set_assoc_wpa_ie_default(struct wpa_sm *sm, u8 *wpa_ie,
2270                                     size_t *wpa_ie_len)
2271 {
2272         int res;
2273
2274         if (sm == NULL)
2275                 return -1;
2276
2277         res = wpa_gen_wpa_ie(sm, wpa_ie, *wpa_ie_len);
2278         if (res < 0)
2279                 return -1;
2280         *wpa_ie_len = res;
2281
2282         wpa_hexdump(MSG_DEBUG, "WPA: Set own WPA IE default",
2283                     wpa_ie, *wpa_ie_len);
2284
2285         if (sm->assoc_wpa_ie == NULL) {
2286                 /*
2287                  * Make a copy of the WPA/RSN IE so that 4-Way Handshake gets
2288                  * the correct version of the IE even if PMKSA caching is
2289                  * aborted (which would remove PMKID from IE generation).
2290                  */
2291                 sm->assoc_wpa_ie = os_malloc(*wpa_ie_len);
2292                 if (sm->assoc_wpa_ie == NULL)
2293                         return -1;
2294
2295                 os_memcpy(sm->assoc_wpa_ie, wpa_ie, *wpa_ie_len);
2296                 sm->assoc_wpa_ie_len = *wpa_ie_len;
2297         }
2298
2299         return 0;
2300 }
2301
2302
2303 /**
2304  * wpa_sm_set_assoc_wpa_ie - Set own WPA/RSN IE from (Re)AssocReq
2305  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2306  * @ie: Pointer to IE data (starting from id)
2307  * @len: IE length
2308  * Returns: 0 on success, -1 on failure
2309  *
2310  * Inform WPA state machine about the WPA/RSN IE used in (Re)Association
2311  * Request frame. The IE will be used to override the default value generated
2312  * with wpa_sm_set_assoc_wpa_ie_default().
2313  */
2314 int wpa_sm_set_assoc_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2315 {
2316         if (sm == NULL)
2317                 return -1;
2318
2319         os_free(sm->assoc_wpa_ie);
2320         if (ie == NULL || len == 0) {
2321                 wpa_printf(MSG_DEBUG, "WPA: clearing own WPA/RSN IE");
2322                 sm->assoc_wpa_ie = NULL;
2323                 sm->assoc_wpa_ie_len = 0;
2324         } else {
2325                 wpa_hexdump(MSG_DEBUG, "WPA: set own WPA/RSN IE", ie, len);
2326                 sm->assoc_wpa_ie = os_malloc(len);
2327                 if (sm->assoc_wpa_ie == NULL)
2328                         return -1;
2329
2330                 os_memcpy(sm->assoc_wpa_ie, ie, len);
2331                 sm->assoc_wpa_ie_len = len;
2332         }
2333
2334         return 0;
2335 }
2336
2337
2338 /**
2339  * wpa_sm_set_ap_wpa_ie - Set AP WPA IE from Beacon/ProbeResp
2340  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2341  * @ie: Pointer to IE data (starting from id)
2342  * @len: IE length
2343  * Returns: 0 on success, -1 on failure
2344  *
2345  * Inform WPA state machine about the WPA IE used in Beacon / Probe Response
2346  * frame.
2347  */
2348 int wpa_sm_set_ap_wpa_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2349 {
2350         if (sm == NULL)
2351                 return -1;
2352
2353         os_free(sm->ap_wpa_ie);
2354         if (ie == NULL || len == 0) {
2355                 wpa_printf(MSG_DEBUG, "WPA: clearing AP WPA IE");
2356                 sm->ap_wpa_ie = NULL;
2357                 sm->ap_wpa_ie_len = 0;
2358         } else {
2359                 wpa_hexdump(MSG_DEBUG, "WPA: set AP WPA IE", ie, len);
2360                 sm->ap_wpa_ie = os_malloc(len);
2361                 if (sm->ap_wpa_ie == NULL)
2362                         return -1;
2363
2364                 os_memcpy(sm->ap_wpa_ie, ie, len);
2365                 sm->ap_wpa_ie_len = len;
2366         }
2367
2368         return 0;
2369 }
2370
2371
2372 /**
2373  * wpa_sm_set_ap_rsn_ie - Set AP RSN IE from Beacon/ProbeResp
2374  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2375  * @ie: Pointer to IE data (starting from id)
2376  * @len: IE length
2377  * Returns: 0 on success, -1 on failure
2378  *
2379  * Inform WPA state machine about the RSN IE used in Beacon / Probe Response
2380  * frame.
2381  */
2382 int wpa_sm_set_ap_rsn_ie(struct wpa_sm *sm, const u8 *ie, size_t len)
2383 {
2384         if (sm == NULL)
2385                 return -1;
2386
2387         os_free(sm->ap_rsn_ie);
2388         if (ie == NULL || len == 0) {
2389                 wpa_printf(MSG_DEBUG, "WPA: clearing AP RSN IE");
2390                 sm->ap_rsn_ie = NULL;
2391                 sm->ap_rsn_ie_len = 0;
2392         } else {
2393                 wpa_hexdump(MSG_DEBUG, "WPA: set AP RSN IE", ie, len);
2394                 sm->ap_rsn_ie = os_malloc(len);
2395                 if (sm->ap_rsn_ie == NULL)
2396                         return -1;
2397
2398                 os_memcpy(sm->ap_rsn_ie, ie, len);
2399                 sm->ap_rsn_ie_len = len;
2400         }
2401
2402         return 0;
2403 }
2404
2405
2406 /**
2407  * wpa_sm_parse_own_wpa_ie - Parse own WPA/RSN IE
2408  * @sm: Pointer to WPA state machine data from wpa_sm_init()
2409  * @data: Pointer to data area for parsing results
2410  * Returns: 0 on success, -1 if IE is not known, or -2 on parsing failure
2411  *
2412  * Parse the contents of the own WPA or RSN IE from (Re)AssocReq and write the
2413  * parsed data into data.
2414  */
2415 int wpa_sm_parse_own_wpa_ie(struct wpa_sm *sm, struct wpa_ie_data *data)
2416 {
2417         if (sm == NULL || sm->assoc_wpa_ie == NULL) {
2418                 wpa_printf(MSG_DEBUG, "WPA: No WPA/RSN IE available from "
2419                            "association info");
2420                 return -1;
2421         }
2422         if (wpa_parse_wpa_ie(sm->assoc_wpa_ie, sm->assoc_wpa_ie_len, data))
2423                 return -2;
2424         return 0;
2425 }
2426
2427
2428 int wpa_sm_pmksa_cache_list(struct wpa_sm *sm, char *buf, size_t len)
2429 {
2430 #ifndef CONFIG_NO_WPA2
2431         return pmksa_cache_list(sm->pmksa, buf, len);
2432 #else /* CONFIG_NO_WPA2 */
2433         return -1;
2434 #endif /* CONFIG_NO_WPA2 */
2435 }
2436
2437
2438 void wpa_sm_drop_sa(struct wpa_sm *sm)
2439 {
2440         wpa_printf(MSG_DEBUG, "WPA: Clear old PMK and PTK");
2441         sm->ptk_set = 0;
2442         sm->tptk_set = 0;
2443         os_memset(sm->pmk, 0, sizeof(sm->pmk));
2444         os_memset(&sm->ptk, 0, sizeof(sm->ptk));
2445         os_memset(&sm->tptk, 0, sizeof(sm->tptk));
2446 }