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