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