]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/hostapd/wpa.c
This commit was generated by cvs2svn to compensate for changes in r159399,
[FreeBSD/FreeBSD.git] / contrib / hostapd / wpa.c
1 /*
2  * Host AP (software wireless LAN access point) user space daemon for
3  * Host AP kernel driver / WPA Authenticator
4  * Copyright (c) 2004-2005, Jouni Malinen <jkmaline@cc.hut.fi>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Alternatively, this software may be distributed under the terms of BSD
11  * license.
12  *
13  * See README and COPYING for more details.
14  *
15  * $FreeBSD$
16  */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <netinet/in.h>
22 #include <sys/time.h>
23 #include <time.h>
24 #include <unistd.h>
25
26 #include "hostapd.h"
27 #include "eapol_sm.h"
28 #include "wpa.h"
29 #include "driver.h"
30 #include "sha1.h"
31 #include "md5.h"
32 #include "rc4.h"
33 #include "aes_wrap.h"
34 #include "ieee802_1x.h"
35 #include "ieee802_11.h"
36 #include "eloop.h"
37 #include "sta_info.h"
38 #include "l2_packet.h"
39 #include "accounting.h"
40 #include "hostap_common.h"
41
42
43 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx);
44 static void wpa_sm_step(struct wpa_state_machine *sm);
45 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len);
46 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx);
47 static void wpa_group_sm_step(struct hostapd_data *hapd);
48 static void pmksa_cache_free(struct hostapd_data *hapd);
49 static struct rsn_pmksa_cache * pmksa_cache_get(struct hostapd_data *hapd,
50                                                 u8 *spa, u8 *pmkid);
51
52
53 /* Default timeouts are 100 ms, but this seems to be a bit too fast for most
54  * WPA Supplicants, so use a bit longer timeout. */
55 static const u32 dot11RSNAConfigGroupUpdateTimeOut = 1000; /* ms */
56 static const u32 dot11RSNAConfigGroupUpdateCount = 3;
57 static const u32 dot11RSNAConfigPairwiseUpdateTimeOut = 1000; /* ms */
58 static const u32 dot11RSNAConfigPairwiseUpdateCount = 3;
59
60 /* TODO: make these configurable */
61 static const int dot11RSNAConfigPMKLifetime = 43200;
62 static const int dot11RSNAConfigPMKReauthThreshold = 70;
63 static const int dot11RSNAConfigSATimeout = 60;
64 static const int pmksa_cache_max_entries = 1024;
65
66
67 static const int WPA_SELECTOR_LEN = 4;
68 static const u8 WPA_OUI_TYPE[] = { 0x00, 0x50, 0xf2, 1 };
69 static const u16 WPA_VERSION = 1;
70 static const u8 WPA_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x50, 0xf2, 1 };
71 static const u8 WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x50, 0xf2, 2 };
72 static const u8 WPA_CIPHER_SUITE_NONE[] = { 0x00, 0x50, 0xf2, 0 };
73 static const u8 WPA_CIPHER_SUITE_WEP40[] = { 0x00, 0x50, 0xf2, 1 };
74 static const u8 WPA_CIPHER_SUITE_TKIP[] = { 0x00, 0x50, 0xf2, 2 };
75 static const u8 WPA_CIPHER_SUITE_WRAP[] = { 0x00, 0x50, 0xf2, 3 };
76 static const u8 WPA_CIPHER_SUITE_CCMP[] = { 0x00, 0x50, 0xf2, 4 };
77 static const u8 WPA_CIPHER_SUITE_WEP104[] = { 0x00, 0x50, 0xf2, 5 };
78
79 static const int RSN_SELECTOR_LEN = 4;
80 static const u16 RSN_VERSION = 1;
81 static const u8 RSN_AUTH_KEY_MGMT_UNSPEC_802_1X[] = { 0x00, 0x0f, 0xac, 1 };
82 static const u8 RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X[] = { 0x00, 0x0f, 0xac, 2 };
83 static const u8 RSN_CIPHER_SUITE_NONE[] = { 0x00, 0x0f, 0xac, 0 };
84 static const u8 RSN_CIPHER_SUITE_WEP40[] = { 0x00, 0x0f, 0xac, 1 };
85 static const u8 RSN_CIPHER_SUITE_TKIP[] = { 0x00, 0x0f, 0xac, 2 };
86 static const u8 RSN_CIPHER_SUITE_WRAP[] = { 0x00, 0x0f, 0xac, 3 };
87 static const u8 RSN_CIPHER_SUITE_CCMP[] = { 0x00, 0x0f, 0xac, 4 };
88 static const u8 RSN_CIPHER_SUITE_WEP104[] = { 0x00, 0x0f, 0xac, 5 };
89
90 /* EAPOL-Key Key Data Encapsulation
91  * GroupKey and STAKey require encryption, otherwise, encryption is optional.
92  */
93 static const u8 RSN_KEY_DATA_GROUPKEY[] = { 0x00, 0x0f, 0xac, 1 };
94 static const u8 RSN_KEY_DATA_STAKEY[] = { 0x00, 0x0f, 0xac, 2 };
95 static const u8 RSN_KEY_DATA_MAC_ADDR[] = { 0x00, 0x0f, 0xac, 3 };
96 static const u8 RSN_KEY_DATA_PMKID[] = { 0x00, 0x0f, 0xac, 4 };
97
98 /* WPA IE version 1
99  * 00-50-f2:1 (OUI:OUI type)
100  * 0x01 0x00 (version; little endian)
101  * (all following fields are optional:)
102  * Group Suite Selector (4 octets) (default: TKIP)
103  * Pairwise Suite Count (2 octets, little endian) (default: 1)
104  * Pairwise Suite List (4 * n octets) (default: TKIP)
105  * Authenticated Key Management Suite Count (2 octets, little endian)
106  *    (default: 1)
107  * Authenticated Key Management Suite List (4 * n octets)
108  *    (default: unspec 802.1X)
109  * WPA Capabilities (2 octets, little endian) (default: 0)
110  */
111
112 struct wpa_ie_hdr {
113         u8 elem_id;
114         u8 len;
115         u8 oui[3];
116         u8 oui_type;
117         u16 version;
118 } __attribute__ ((packed));
119
120
121 /* RSN IE version 1
122  * 0x01 0x00 (version; little endian)
123  * (all following fields are optional:)
124  * Group Suite Selector (4 octets) (default: CCMP)
125  * Pairwise Suite Count (2 octets, little endian) (default: 1)
126  * Pairwise Suite List (4 * n octets) (default: CCMP)
127  * Authenticated Key Management Suite Count (2 octets, little endian)
128  *    (default: 1)
129  * Authenticated Key Management Suite List (4 * n octets)
130  *    (default: unspec 802.1X)
131  * RSN Capabilities (2 octets, little endian) (default: 0)
132  * PMKID Count (2 octets) (default: 0)
133  * PMKID List (16 * n octets)
134  */
135
136 struct rsn_ie_hdr {
137         u8 elem_id; /* WLAN_EID_RSN */
138         u8 len;
139         u16 version;
140 } __attribute__ ((packed));
141
142
143 static int wpa_write_wpa_ie(struct hostapd_data *hapd, u8 *buf, size_t len)
144 {
145         struct wpa_ie_hdr *hdr;
146         int num_suites;
147         u8 *pos, *count;
148
149         hdr = (struct wpa_ie_hdr *) buf;
150         hdr->elem_id = WLAN_EID_GENERIC;
151         memcpy(&hdr->oui, WPA_OUI_TYPE, WPA_SELECTOR_LEN);
152         hdr->version = host_to_le16(WPA_VERSION);
153         pos = (u8 *) (hdr + 1);
154
155         if (hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
156                 memcpy(pos, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN);
157         } else if (hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
158                 memcpy(pos, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN);
159         } else if (hapd->conf->wpa_group == WPA_CIPHER_WEP104) {
160                 memcpy(pos, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN);
161         } else if (hapd->conf->wpa_group == WPA_CIPHER_WEP40) {
162                 memcpy(pos, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN);
163         } else {
164                 printf("Invalid group cipher (%d).\n", hapd->conf->wpa_group);
165                 return -1;
166         }
167         pos += WPA_SELECTOR_LEN;
168
169         num_suites = 0;
170         count = pos;
171         pos += 2;
172
173         if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
174                 memcpy(pos, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN);
175                 pos += WPA_SELECTOR_LEN;
176                 num_suites++;
177         }
178         if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
179                 memcpy(pos, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN);
180                 pos += WPA_SELECTOR_LEN;
181                 num_suites++;
182         }
183         if (hapd->conf->wpa_pairwise & WPA_CIPHER_NONE) {
184                 memcpy(pos, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN);
185                 pos += WPA_SELECTOR_LEN;
186                 num_suites++;
187         }
188
189         if (num_suites == 0) {
190                 printf("Invalid pairwise cipher (%d).\n",
191                        hapd->conf->wpa_pairwise);
192                 return -1;
193         }
194         *count++ = num_suites & 0xff;
195         *count = (num_suites >> 8) & 0xff;
196
197         num_suites = 0;
198         count = pos;
199         pos += 2;
200
201         if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
202                 memcpy(pos, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X, WPA_SELECTOR_LEN);
203                 pos += WPA_SELECTOR_LEN;
204                 num_suites++;
205         }
206         if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
207                 memcpy(pos, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X,
208                        WPA_SELECTOR_LEN);
209                 pos += WPA_SELECTOR_LEN;
210                 num_suites++;
211         }
212
213         if (num_suites == 0) {
214                 printf("Invalid key management type (%d).\n",
215                        hapd->conf->wpa_key_mgmt);
216                 return -1;
217         }
218         *count++ = num_suites & 0xff;
219         *count = (num_suites >> 8) & 0xff;
220
221         /* WPA Capabilities; use defaults, so no need to include it */
222
223         hdr->len = (pos - buf) - 2;
224
225         return pos - buf;
226 }
227
228
229 static int wpa_write_rsn_ie(struct hostapd_data *hapd, u8 *buf, size_t len)
230 {
231         struct rsn_ie_hdr *hdr;
232         int num_suites;
233         u8 *pos, *count;
234
235         hdr = (struct rsn_ie_hdr *) buf;
236         hdr->elem_id = WLAN_EID_RSN;
237         pos = (u8 *) &hdr->version;
238         *pos++ = RSN_VERSION & 0xff;
239         *pos++ = RSN_VERSION >> 8;
240         pos = (u8 *) (hdr + 1);
241
242         if (hapd->conf->wpa_group == WPA_CIPHER_CCMP) {
243                 memcpy(pos, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN);
244         } else if (hapd->conf->wpa_group == WPA_CIPHER_TKIP) {
245                 memcpy(pos, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN);
246         } else if (hapd->conf->wpa_group == WPA_CIPHER_WEP104) {
247                 memcpy(pos, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN);
248         } else if (hapd->conf->wpa_group == WPA_CIPHER_WEP40) {
249                 memcpy(pos, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN);
250         } else {
251                 printf("Invalid group cipher (%d).\n", hapd->conf->wpa_group);
252                 return -1;
253         }
254         pos += RSN_SELECTOR_LEN;
255
256         num_suites = 0;
257         count = pos;
258         pos += 2;
259
260         if (hapd->conf->wpa_pairwise & WPA_CIPHER_CCMP) {
261                 memcpy(pos, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN);
262                 pos += RSN_SELECTOR_LEN;
263                 num_suites++;
264         }
265         if (hapd->conf->wpa_pairwise & WPA_CIPHER_TKIP) {
266                 memcpy(pos, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN);
267                 pos += RSN_SELECTOR_LEN;
268                 num_suites++;
269         }
270         if (hapd->conf->wpa_pairwise & WPA_CIPHER_NONE) {
271                 memcpy(pos, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN);
272                 pos += RSN_SELECTOR_LEN;
273                 num_suites++;
274         }
275
276         if (num_suites == 0) {
277                 printf("Invalid pairwise cipher (%d).\n",
278                        hapd->conf->wpa_pairwise);
279                 return -1;
280         }
281         *count++ = num_suites & 0xff;
282         *count = (num_suites >> 8) & 0xff;
283
284         num_suites = 0;
285         count = pos;
286         pos += 2;
287
288         if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
289                 memcpy(pos, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X, RSN_SELECTOR_LEN);
290                 pos += RSN_SELECTOR_LEN;
291                 num_suites++;
292         }
293         if (hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK) {
294                 memcpy(pos, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X,
295                        RSN_SELECTOR_LEN);
296                 pos += RSN_SELECTOR_LEN;
297                 num_suites++;
298         }
299
300         if (num_suites == 0) {
301                 printf("Invalid key management type (%d).\n",
302                        hapd->conf->wpa_key_mgmt);
303                 return -1;
304         }
305         *count++ = num_suites & 0xff;
306         *count = (num_suites >> 8) & 0xff;
307
308         /* RSN Capabilities */
309         *pos++ = hapd->conf->rsn_preauth ? BIT(0) : 0;
310         *pos++ = 0;
311
312         hdr->len = (pos - buf) - 2;
313
314         return pos - buf;
315 }
316
317
318 static int wpa_gen_wpa_ie(struct hostapd_data *hapd)
319 {
320         u8 *pos, buf[100];
321         int res;
322
323         pos = buf;
324
325         if (hapd->conf->wpa & HOSTAPD_WPA_VERSION_WPA2) {
326                 res = wpa_write_rsn_ie(hapd, pos, buf + sizeof(buf) - pos);
327                 if (res < 0)
328                         return res;
329                 pos += res;
330         }
331         if (hapd->conf->wpa & HOSTAPD_WPA_VERSION_WPA) {
332                 res = wpa_write_wpa_ie(hapd, pos, buf + sizeof(buf) - pos);
333                 if (res < 0)
334                         return res;
335                 pos += res;
336         }
337
338         free(hapd->wpa_ie);
339         hapd->wpa_ie = malloc(pos - buf);
340         if (hapd->wpa_ie == NULL)
341                 return -1;
342         memcpy(hapd->wpa_ie, buf, pos - buf);
343         hapd->wpa_ie_len = pos - buf;
344
345         return 0;
346 }
347
348
349 static void wpa_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta)
350 {
351         hostapd_sta_deauth(hapd, sta->addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
352         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
353         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
354         eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
355         sta->timeout_next = STA_REMOVE;
356 }
357
358
359 static void wpa_rekey_gmk(void *eloop_ctx, void *timeout_ctx)
360 {
361         struct hostapd_data *hapd = eloop_ctx;
362
363         if (hapd->wpa_auth) {
364                 if (hostapd_get_rand(hapd->wpa_auth->GMK, WPA_GMK_LEN)) {
365                         printf("Failed to get random data for WPA "
366                                "initialization.\n");
367                 } else {
368                         hostapd_logger(hapd, NULL, HOSTAPD_MODULE_WPA,
369                                        HOSTAPD_LEVEL_DEBUG,
370                                        "GMK rekeyd");
371                 }
372         }
373
374         if (hapd->conf->wpa_gmk_rekey) {
375                 eloop_register_timeout(hapd->conf->wpa_gmk_rekey, 0,
376                                        wpa_rekey_gmk, hapd, NULL);
377         }
378 }
379
380
381 static void wpa_rekey_gtk(void *eloop_ctx, void *timeout_ctx)
382 {
383         struct hostapd_data *hapd = eloop_ctx;
384
385         if (hapd->wpa_auth) {
386                 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_WPA,
387                                HOSTAPD_LEVEL_DEBUG, "rekeying GTK");
388                 hapd->wpa_auth->GTKReKey = TRUE;
389                 do {
390                         hapd->wpa_auth->changed = FALSE;
391                         wpa_group_sm_step(hapd);
392                 } while (hapd->wpa_auth->changed);
393         }
394         if (hapd->conf->wpa_group_rekey) {
395                 eloop_register_timeout(hapd->conf->wpa_group_rekey, 0,
396                                        wpa_rekey_gtk, hapd, NULL);
397         }
398 }
399
400
401 #ifdef CONFIG_RSN_PREAUTH
402
403 static void rsn_preauth_receive(void *ctx, const u8 *src_addr,
404                                 const u8 *buf, size_t len)
405 {
406         struct rsn_preauth_interface *piface = ctx;
407         struct hostapd_data *hapd = piface->hapd;
408         struct ieee802_1x_hdr *hdr;
409         struct sta_info *sta;
410         struct l2_ethhdr *ethhdr;
411
412         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: receive pre-auth packet "
413                       "from interface '%s'\n", piface->ifname);
414         if (len < sizeof(*ethhdr) + sizeof(*hdr)) {
415                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: too short pre-auth "
416                               "packet (len=%lu)\n", (unsigned long) len);
417                 return;
418         }
419
420         ethhdr = (struct l2_ethhdr *) buf;
421         hdr = (struct ieee802_1x_hdr *) (ethhdr + 1);
422
423         if (memcmp(ethhdr->h_dest, hapd->own_addr, ETH_ALEN) != 0) {
424                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: pre-auth for "
425                               "foreign address " MACSTR "\n",
426                               MAC2STR(ethhdr->h_dest));
427                 return;
428         }
429
430         sta = ap_get_sta(hapd, ethhdr->h_source);
431         if (sta && (sta->flags & WLAN_STA_ASSOC)) {
432                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: pre-auth for "
433                               "already association STA " MACSTR "\n",
434                               MAC2STR(sta->addr));
435                 return;
436         }
437         if (!sta && hdr->type == IEEE802_1X_TYPE_EAPOL_START) {
438                 sta = (struct sta_info *) malloc(sizeof(struct sta_info));
439                 if (sta == NULL)
440                         return;
441                 memset(sta, 0, sizeof(*sta));
442                 memcpy(sta->addr, ethhdr->h_source, ETH_ALEN);
443                 sta->flags = WLAN_STA_PREAUTH;
444                 sta->next = hapd->sta_list;
445                 sta->wpa = WPA_VERSION_WPA2;
446                 hapd->sta_list = sta;
447                 hapd->num_sta++;
448                 ap_sta_hash_add(hapd, sta);
449
450                 ieee802_1x_new_station(hapd, sta);
451                 if (sta->eapol_sm == NULL) {
452                         ap_free_sta(hapd, sta);
453                         sta = NULL;
454                 } else {
455                         sta->eapol_sm->radius_identifier = -1;
456                         sta->eapol_sm->portValid = TRUE;
457                         sta->eapol_sm->flags |= EAPOL_SM_PREAUTH;
458                 }
459         }
460         if (sta == NULL)
461                 return;
462         sta->preauth_iface = piface;
463         ieee802_1x_receive(hapd, ethhdr->h_source, (u8 *) (ethhdr + 1),
464                            len - sizeof(*ethhdr));
465 }
466
467
468 static int rsn_preauth_iface_add(struct hostapd_data *hapd, const char *ifname)
469 {
470         struct rsn_preauth_interface *piface;
471
472         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN pre-auth interface '%s'\n",
473                       ifname);
474
475         piface = malloc(sizeof(*piface));
476         if (piface == NULL)
477                 return -1;
478         memset(piface, 0, sizeof(*piface));
479         piface->hapd = hapd;
480
481         piface->ifname = strdup(ifname);
482         if (piface->ifname == NULL) {
483                 goto fail1;
484         }
485
486         piface->l2 = l2_packet_init(piface->ifname, NULL, ETH_P_PREAUTH,
487                                     rsn_preauth_receive, piface, 1);
488         if (piface->l2 == NULL) {
489                 printf("Failed to open register layer 2 access to "
490                        "ETH_P_PREAUTH\n");
491                 goto fail2;
492         }
493
494         piface->next = hapd->preauth_iface;
495         hapd->preauth_iface = piface;
496         return 0;
497
498 fail2:
499         free(piface->ifname);
500 fail1:
501         free(piface);
502         return -1;
503 }
504
505
506 static void rsn_preauth_iface_deinit(struct hostapd_data *hapd)
507 {
508         struct rsn_preauth_interface *piface, *prev;
509
510         piface = hapd->preauth_iface;
511         hapd->preauth_iface = NULL;
512         while (piface) {
513                 prev = piface;
514                 piface = piface->next;
515                 l2_packet_deinit(prev->l2);
516                 free(prev->ifname);
517                 free(prev);
518         }
519 }
520
521
522 static int rsn_preauth_iface_init(struct hostapd_data *hapd)
523 {
524         char *tmp, *start, *end;
525
526         if (hapd->conf->rsn_preauth_interfaces == NULL)
527                 return 0;
528
529         tmp = strdup(hapd->conf->rsn_preauth_interfaces);
530         if (tmp == NULL)
531                 return -1;
532         start = tmp;
533         for (;;) {
534                 while (*start == ' ')
535                         start++;
536                 if (*start == '\0')
537                         break;
538                 end = strchr(start, ' ');
539                 if (end)
540                         *end = '\0';
541
542                 if (rsn_preauth_iface_add(hapd, start)) {
543                         rsn_preauth_iface_deinit(hapd);
544                         return -1;
545                 }
546
547                 if (end)
548                         start = end + 1;
549                 else
550                         break;
551         }
552         free(tmp);
553         return 0;
554 }
555
556
557 static void rsn_preauth_finished_cb(void *eloop_ctx, void *timeout_ctx)
558 {
559         struct hostapd_data *hapd = eloop_ctx;
560         struct sta_info *sta = timeout_ctx;
561         wpa_printf(MSG_DEBUG, "RSN: Removing pre-authentication STA entry for "
562                    MACSTR, MAC2STR(sta->addr));
563         ap_free_sta(hapd, sta);
564 }
565
566
567 void rsn_preauth_finished(struct hostapd_data *hapd, struct sta_info *sta,
568                           int success)
569 {
570         u8 *key;
571         size_t len;
572         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
573                        HOSTAPD_LEVEL_INFO, "pre-authentication %s",
574                        success ? "succeeded" : "failed");
575
576         key = ieee802_1x_get_key_crypt(sta->eapol_sm, &len);
577         if (success && key) {
578                 pmksa_cache_add(hapd, sta, key, dot11RSNAConfigPMKLifetime);
579         }
580
581         /*
582          * Finish STA entry removal from timeout in order to avoid freeing
583          * STA data before the caller has finished processing.
584          */
585         eloop_register_timeout(0, 0, rsn_preauth_finished_cb, hapd, sta);
586 }
587
588
589 void rsn_preauth_send(struct hostapd_data *hapd, struct sta_info *sta,
590                       u8 *buf, size_t len)
591 {
592         struct rsn_preauth_interface *piface;
593         struct l2_ethhdr *ethhdr;
594
595         piface = hapd->preauth_iface;
596         while (piface) {
597                 if (piface == sta->preauth_iface)
598                         break;
599                 piface = piface->next;
600         }
601
602         if (piface == NULL) {
603                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "RSN: Could not find "
604                               "pre-authentication interface for " MACSTR "\n",
605                               MAC2STR(sta->addr));
606                 return;
607         }
608
609         ethhdr = malloc(sizeof(*ethhdr) + len);
610         if (ethhdr == NULL)
611                 return;
612
613         memcpy(ethhdr->h_dest, sta->addr, ETH_ALEN);
614         memcpy(ethhdr->h_source, hapd->own_addr, ETH_ALEN);
615         ethhdr->h_proto = htons(ETH_P_PREAUTH);
616         memcpy(ethhdr + 1, buf, len);
617
618         if (l2_packet_send(piface->l2, sta->addr, ETH_P_PREAUTH, (u8 *) ethhdr,
619                            sizeof(*ethhdr) + len) < 0) {
620                 printf("Failed to send preauth packet using l2_packet_send\n");
621         }
622         free(ethhdr);
623 }
624
625 #else /* CONFIG_RSN_PREAUTH */
626
627 static inline int rsn_preauth_iface_init(struct hostapd_data *hapd)
628 {
629         return 0;
630 }
631
632 static inline void rsn_preauth_iface_deinit(struct hostapd_data *hapd)
633 {
634 }
635
636 static void rsn_preauth_finished_cb(void *eloop_ctx, void *timeout_ctx)
637 {
638 }
639
640 void rsn_preauth_finished(struct hostapd_data *hapd, struct sta_info *sta,
641                           int success)
642 {
643 }
644
645 void rsn_preauth_send(struct hostapd_data *hapd, struct sta_info *sta,
646                       u8 *buf, size_t len)
647 {
648 }
649
650 #endif /* CONFIG_RSN_PREAUTH */
651
652
653 int wpa_init(struct hostapd_data *hapd)
654 {
655         u8 rkey[32];
656         u8 buf[ETH_ALEN + 8];
657
658         if (rsn_preauth_iface_init(hapd))
659                 return -1;
660
661         if (hostapd_set_privacy(hapd, 1)) {
662                 printf("Could not set PrivacyInvoked for interface %s\n",
663                        hapd->conf->iface);
664                 return -1;
665         }
666
667         if (wpa_gen_wpa_ie(hapd)) {
668                 printf("Could not generate WPA IE.\n");
669                 return -1;
670         }
671
672         if (hostapd_set_generic_elem(hapd, hapd->wpa_ie, hapd->wpa_ie_len)) {
673                 printf("Failed to configure WPA IE for the kernel driver.\n");
674                 return -1;
675         }
676
677         hapd->wpa_auth = malloc(sizeof(struct wpa_authenticator));
678         if (hapd->wpa_auth == NULL)
679                 return -1;
680         memset(hapd->wpa_auth, 0, sizeof(struct wpa_authenticator));
681         hapd->wpa_auth->GTKAuthenticator = TRUE;
682         switch (hapd->conf->wpa_group) {
683         case WPA_CIPHER_CCMP:
684                 hapd->wpa_auth->GTK_len = 16;
685                 break;
686         case WPA_CIPHER_TKIP:
687                 hapd->wpa_auth->GTK_len = 32;
688                 break;
689         case WPA_CIPHER_WEP104:
690                 hapd->wpa_auth->GTK_len = 13;
691                 break;
692         case WPA_CIPHER_WEP40:
693                 hapd->wpa_auth->GTK_len = 5;
694                 break;
695         }
696
697         /* Counter = PRF-256(Random number, "Init Counter",
698          *                   Local MAC Address || Time)
699          */
700         memcpy(buf, hapd->own_addr, ETH_ALEN);
701         hostapd_get_ntp_timestamp(buf + ETH_ALEN);
702         if (hostapd_get_rand(rkey, sizeof(rkey)) ||
703             hostapd_get_rand(hapd->wpa_auth->GMK, WPA_GMK_LEN)) {
704                 printf("Failed to get random data for WPA initialization.\n");
705                 free(hapd->wpa_auth);
706                 hapd->wpa_auth = NULL;
707                 return -1;
708         }
709
710         sha1_prf(rkey, sizeof(rkey), "Init Counter", buf, sizeof(buf),
711                  hapd->wpa_auth->Counter, WPA_NONCE_LEN);
712
713         if (hapd->conf->wpa_gmk_rekey) {
714                 eloop_register_timeout(hapd->conf->wpa_gmk_rekey, 0,
715                                        wpa_rekey_gmk, hapd, NULL);
716         }
717
718         if (hapd->conf->wpa_group_rekey) {
719                 eloop_register_timeout(hapd->conf->wpa_group_rekey, 0,
720                                        wpa_rekey_gtk, hapd, NULL);
721         }
722
723         hapd->wpa_auth->GInit = TRUE;
724         wpa_group_sm_step(hapd);
725         hapd->wpa_auth->GInit = FALSE;
726         wpa_group_sm_step(hapd);
727
728         return 0;
729 }
730
731
732 void wpa_deinit(struct hostapd_data *hapd)
733 {
734         rsn_preauth_iface_deinit(hapd);
735
736         eloop_cancel_timeout(wpa_rekey_gmk, hapd, NULL);
737         eloop_cancel_timeout(wpa_rekey_gtk, hapd, NULL);
738
739         if (hostapd_set_privacy(hapd, 0)) {
740                 printf("Could not disable PrivacyInvoked for interface %s\n",
741                        hapd->conf->iface);
742         }
743
744         if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
745                 printf("Could not remove generic information element from "
746                        "interface %s\n", hapd->conf->iface);
747         }
748
749         free(hapd->wpa_ie);
750         hapd->wpa_ie = NULL;
751         free(hapd->wpa_auth);
752         hapd->wpa_auth = NULL;
753
754         pmksa_cache_free(hapd);
755 }
756
757
758 static int wpa_selector_to_bitfield(u8 *s)
759 {
760         if (memcmp(s, WPA_CIPHER_SUITE_NONE, WPA_SELECTOR_LEN) == 0)
761                 return WPA_CIPHER_NONE;
762         if (memcmp(s, WPA_CIPHER_SUITE_WEP40, WPA_SELECTOR_LEN) == 0)
763                 return WPA_CIPHER_WEP40;
764         if (memcmp(s, WPA_CIPHER_SUITE_TKIP, WPA_SELECTOR_LEN) == 0)
765                 return WPA_CIPHER_TKIP;
766         if (memcmp(s, WPA_CIPHER_SUITE_CCMP, WPA_SELECTOR_LEN) == 0)
767                 return WPA_CIPHER_CCMP;
768         if (memcmp(s, WPA_CIPHER_SUITE_WEP104, WPA_SELECTOR_LEN) == 0)
769                 return WPA_CIPHER_WEP104;
770         return 0;
771 }
772
773
774 static int wpa_key_mgmt_to_bitfield(u8 *s)
775 {
776         if (memcmp(s, WPA_AUTH_KEY_MGMT_UNSPEC_802_1X, WPA_SELECTOR_LEN) == 0)
777                 return WPA_KEY_MGMT_IEEE8021X;
778         if (memcmp(s, WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X, WPA_SELECTOR_LEN) ==
779             0)
780                 return WPA_KEY_MGMT_PSK;
781         return 0;
782 }
783
784
785 static int rsn_selector_to_bitfield(u8 *s)
786 {
787         if (memcmp(s, RSN_CIPHER_SUITE_NONE, RSN_SELECTOR_LEN) == 0)
788                 return WPA_CIPHER_NONE;
789         if (memcmp(s, RSN_CIPHER_SUITE_WEP40, RSN_SELECTOR_LEN) == 0)
790                 return WPA_CIPHER_WEP40;
791         if (memcmp(s, RSN_CIPHER_SUITE_TKIP, RSN_SELECTOR_LEN) == 0)
792                 return WPA_CIPHER_TKIP;
793         if (memcmp(s, RSN_CIPHER_SUITE_CCMP, RSN_SELECTOR_LEN) == 0)
794                 return WPA_CIPHER_CCMP;
795         if (memcmp(s, RSN_CIPHER_SUITE_WEP104, RSN_SELECTOR_LEN) == 0)
796                 return WPA_CIPHER_WEP104;
797         return 0;
798 }
799
800
801 static int rsn_key_mgmt_to_bitfield(u8 *s)
802 {
803         if (memcmp(s, RSN_AUTH_KEY_MGMT_UNSPEC_802_1X, RSN_SELECTOR_LEN) == 0)
804                 return WPA_KEY_MGMT_IEEE8021X;
805         if (memcmp(s, RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X, RSN_SELECTOR_LEN) ==
806             0)
807                 return WPA_KEY_MGMT_PSK;
808         return 0;
809 }
810
811
812 static void rsn_pmkid(const u8 *pmk, const u8 *aa, const u8 *spa, u8 *pmkid)
813 {
814         char *title = "PMK Name";
815         const u8 *addr[3];
816         const size_t len[3] = { 8, ETH_ALEN, ETH_ALEN };
817         unsigned char hash[SHA1_MAC_LEN];
818
819         addr[0] = (u8 *) title;
820         addr[1] = aa;
821         addr[2] = spa;
822
823         hmac_sha1_vector(pmk, PMK_LEN, 3, addr, len, hash);
824         memcpy(pmkid, hash, PMKID_LEN);
825 }
826
827
828 static void pmksa_cache_set_expiration(struct hostapd_data *hapd);
829
830
831 static void _pmksa_cache_free_entry(struct rsn_pmksa_cache *entry)
832 {
833         if (entry == NULL)
834                 return;
835         free(entry->identity);
836         ieee802_1x_free_radius_class(&entry->radius_class);
837         free(entry);
838 }
839
840
841 static void pmksa_cache_free_entry(struct hostapd_data *hapd,
842                                    struct rsn_pmksa_cache *entry)
843 {
844         struct sta_info *sta;
845         struct rsn_pmksa_cache *pos, *prev;
846         hapd->pmksa_count--;
847         for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
848                 if (sta->pmksa == entry)
849                         sta->pmksa = NULL;
850         }
851         pos = hapd->pmkid[PMKID_HASH(entry->pmkid)];
852         prev = NULL;
853         while (pos) {
854                 if (pos == entry) {
855                         if (prev != NULL) {
856                                 prev->hnext = pos->hnext;
857                         } else {
858                                 hapd->pmkid[PMKID_HASH(entry->pmkid)] =
859                                         pos->hnext;
860                         }
861                         break;
862                 }
863                 prev = pos;
864                 pos = pos->hnext;
865         }
866
867         pos = hapd->pmksa;
868         prev = NULL;
869         while (pos) {
870                 if (pos == entry) {
871                         if (prev != NULL)
872                                 prev->next = pos->next;
873                         else
874                                 hapd->pmksa = pos->next;
875                         break;
876                 }
877                 prev = pos;
878                 pos = pos->next;
879         }
880         _pmksa_cache_free_entry(entry);
881 }
882
883
884 static void pmksa_cache_expire(void *eloop_ctx, void *timeout_ctx)
885 {
886         struct hostapd_data *hapd = eloop_ctx;
887         time_t now;
888
889         time(&now);
890         while (hapd->pmksa && hapd->pmksa->expiration <= now) {
891                 struct rsn_pmksa_cache *entry = hapd->pmksa;
892                 hapd->pmksa = entry->next;
893                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
894                               "RSN: expired PMKSA cache entry for "
895                               MACSTR, MAC2STR(entry->spa));
896                 pmksa_cache_free_entry(hapd, entry);
897         }
898
899         pmksa_cache_set_expiration(hapd);
900 }
901
902
903 static void pmksa_cache_set_expiration(struct hostapd_data *hapd)
904 {
905         int sec;
906         eloop_cancel_timeout(pmksa_cache_expire, hapd, NULL);
907         if (hapd->pmksa == NULL)
908                 return;
909         sec = hapd->pmksa->expiration - time(NULL);
910         if (sec < 0)
911                 sec = 0;
912         eloop_register_timeout(sec + 1, 0, pmksa_cache_expire, hapd, NULL);
913 }
914
915
916 static void pmksa_cache_from_eapol_data(struct rsn_pmksa_cache *entry,
917                                         struct eapol_state_machine *eapol)
918 {
919         if (eapol == NULL)
920                 return;
921
922         if (eapol->identity) {
923                 entry->identity = malloc(eapol->identity_len);
924                 if (entry->identity) {
925                         entry->identity_len = eapol->identity_len;
926                         memcpy(entry->identity, eapol->identity,
927                                eapol->identity_len);
928                 }
929         }
930
931         ieee802_1x_copy_radius_class(&entry->radius_class,
932                                      &eapol->radius_class);
933 }
934
935
936 static void pmksa_cache_to_eapol_data(struct rsn_pmksa_cache *entry,
937                                       struct eapol_state_machine *eapol)
938 {
939         if (entry == NULL || eapol == NULL)
940                 return;
941
942         if (entry->identity) {
943                 free(eapol->identity);
944                 eapol->identity = malloc(entry->identity_len);
945                 if (eapol->identity) {
946                         eapol->identity_len = entry->identity_len;
947                         memcpy(eapol->identity, entry->identity,
948                                entry->identity_len);
949                 }
950                 wpa_hexdump_ascii(MSG_DEBUG, "STA identity from PMKSA",
951                                   eapol->identity, eapol->identity_len);
952         }
953
954         ieee802_1x_free_radius_class(&eapol->radius_class);
955         ieee802_1x_copy_radius_class(&eapol->radius_class,
956                                      &entry->radius_class);
957         if (eapol->radius_class.attr) {
958                 wpa_printf(MSG_DEBUG, "Copied %lu Class attribute(s) from "
959                            "PMKSA", (unsigned long) eapol->radius_class.count);
960         }
961 }
962
963
964 void pmksa_cache_add(struct hostapd_data *hapd, struct sta_info *sta, u8 *pmk,
965                      int session_timeout)
966 {
967         struct rsn_pmksa_cache *entry, *pos, *prev;
968
969         if (sta->wpa != WPA_VERSION_WPA2)
970                 return;
971
972         entry = malloc(sizeof(*entry));
973         if (entry == NULL)
974                 return;
975         memset(entry, 0, sizeof(*entry));
976         memcpy(entry->pmk, pmk, PMK_LEN);
977         rsn_pmkid(pmk, hapd->own_addr, sta->addr, entry->pmkid);
978         time(&entry->expiration);
979         if (session_timeout > 0)
980                 entry->expiration += session_timeout;
981         else
982                 entry->expiration += dot11RSNAConfigPMKLifetime;
983         entry->akmp = WPA_KEY_MGMT_IEEE8021X;
984         memcpy(entry->spa, sta->addr, ETH_ALEN);
985         pmksa_cache_from_eapol_data(entry, sta->eapol_sm);
986
987         /* Replace an old entry for the same STA (if found) with the new entry
988          */
989         pos = pmksa_cache_get(hapd, sta->addr, NULL);
990         if (pos)
991                 pmksa_cache_free_entry(hapd, pos);
992
993         if (hapd->pmksa_count >= pmksa_cache_max_entries && hapd->pmksa) {
994                 /* Remove the oldest entry to make room for the new entry */
995                 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
996                               "RSN: removed the oldest PMKSA cache entry (for "
997                               MACSTR ") to make room for new one",
998                               MAC2STR(hapd->pmksa->spa));
999                 pmksa_cache_free_entry(hapd, hapd->pmksa);
1000         }
1001
1002         /* Add the new entry; order by expiration time */
1003         pos = hapd->pmksa;
1004         prev = NULL;
1005         while (pos) {
1006                 if (pos->expiration > entry->expiration)
1007                         break;
1008                 prev = pos;
1009                 pos = pos->next;
1010         }
1011         if (prev == NULL) {
1012                 entry->next = hapd->pmksa;
1013                 hapd->pmksa = entry;
1014         } else {
1015                 entry->next = prev->next;
1016                 prev->next = entry;
1017         }
1018         entry->hnext = hapd->pmkid[PMKID_HASH(entry->pmkid)];
1019         hapd->pmkid[PMKID_HASH(entry->pmkid)] = entry;
1020
1021         hapd->pmksa_count++;
1022         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1023                        HOSTAPD_LEVEL_DEBUG,
1024                        "added PMKSA cache entry");
1025         if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1026                 hostapd_hexdump("RSN: added PMKID", entry->pmkid, PMKID_LEN);
1027         }
1028 }
1029
1030
1031 static void pmksa_cache_free(struct hostapd_data *hapd)
1032 {
1033         struct rsn_pmksa_cache *entry, *prev;
1034         int i;
1035         struct sta_info *sta;
1036
1037         entry = hapd->pmksa;
1038         hapd->pmksa = NULL;
1039         while (entry) {
1040                 prev = entry;
1041                 entry = entry->next;
1042                 _pmksa_cache_free_entry(prev);
1043         }
1044         eloop_cancel_timeout(pmksa_cache_expire, hapd, NULL);
1045         for (i = 0; i < PMKID_HASH_SIZE; i++)
1046                 hapd->pmkid[i] = NULL;
1047         for (sta = hapd->sta_list; sta; sta = sta->next)
1048                 sta->pmksa = NULL;
1049 }
1050
1051
1052 static struct rsn_pmksa_cache * pmksa_cache_get(struct hostapd_data *hapd,
1053                                                 u8 *spa, u8 *pmkid)
1054 {
1055         struct rsn_pmksa_cache *entry;
1056
1057         if (pmkid)
1058                 entry = hapd->pmkid[PMKID_HASH(pmkid)];
1059         else
1060                 entry = hapd->pmksa;
1061         while (entry) {
1062                 if ((spa == NULL || memcmp(entry->spa, spa, ETH_ALEN) == 0) &&
1063                     (pmkid == NULL ||
1064                      memcmp(entry->pmkid, pmkid, PMKID_LEN) == 0))
1065                         return entry;
1066                 entry = pmkid ? entry->hnext : entry->next;
1067         }
1068         return NULL;
1069 }
1070
1071
1072 struct wpa_ie_data {
1073         int pairwise_cipher;
1074         int group_cipher;
1075         int key_mgmt;
1076         int capabilities;
1077         size_t num_pmkid;
1078         u8 *pmkid;
1079 };
1080
1081
1082 static int wpa_parse_wpa_ie_wpa(const u8 *wpa_ie, size_t wpa_ie_len,
1083                                 struct wpa_ie_data *data)
1084 {
1085         struct wpa_ie_hdr *hdr;
1086         u8 *pos;
1087         int left;
1088         int i, count;
1089
1090         memset(data, 0, sizeof(*data));
1091         data->pairwise_cipher = WPA_CIPHER_TKIP;
1092         data->group_cipher = WPA_CIPHER_TKIP;
1093         data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1094
1095         if (wpa_ie_len < sizeof(struct wpa_ie_hdr))
1096                 return -1;
1097
1098         hdr = (struct wpa_ie_hdr *) wpa_ie;
1099
1100         if (hdr->elem_id != WLAN_EID_GENERIC ||
1101             hdr->len != wpa_ie_len - 2 ||
1102             memcmp(&hdr->oui, WPA_OUI_TYPE, WPA_SELECTOR_LEN) != 0 ||
1103             le_to_host16(hdr->version) != WPA_VERSION) {
1104                 return -2;
1105         }
1106
1107         pos = (u8 *) (hdr + 1);
1108         left = wpa_ie_len - sizeof(*hdr);
1109
1110         if (left >= WPA_SELECTOR_LEN) {
1111                 data->group_cipher = wpa_selector_to_bitfield(pos);
1112                 pos += WPA_SELECTOR_LEN;
1113                 left -= WPA_SELECTOR_LEN;
1114         } else if (left > 0)
1115                   return -3;
1116
1117         if (left >= 2) {
1118                 data->pairwise_cipher = 0;
1119                 count = pos[0] | (pos[1] << 8);
1120                 pos += 2;
1121                 left -= 2;
1122                 if (count == 0 || left < count * WPA_SELECTOR_LEN)
1123                         return -4;
1124                 for (i = 0; i < count; i++) {
1125                         data->pairwise_cipher |= wpa_selector_to_bitfield(pos);
1126                         pos += WPA_SELECTOR_LEN;
1127                         left -= WPA_SELECTOR_LEN;
1128                 }
1129         } else if (left == 1)
1130                 return -5;
1131
1132         if (left >= 2) {
1133                 data->key_mgmt = 0;
1134                 count = pos[0] | (pos[1] << 8);
1135                 pos += 2;
1136                 left -= 2;
1137                 if (count == 0 || left < count * WPA_SELECTOR_LEN)
1138                         return -6;
1139                 for (i = 0; i < count; i++) {
1140                         data->key_mgmt |= wpa_key_mgmt_to_bitfield(pos);
1141                         pos += WPA_SELECTOR_LEN;
1142                         left -= WPA_SELECTOR_LEN;
1143                 }
1144         } else if (left == 1)
1145                 return -7;
1146
1147         if (left >= 2) {
1148                 data->capabilities = pos[0] | (pos[1] << 8);
1149                 pos += 2;
1150                 left -= 2;
1151         }
1152
1153         if (left > 0) {
1154                 return -8;
1155         }
1156
1157         return 0;
1158 }
1159
1160
1161 static int wpa_parse_wpa_ie_rsn(const u8 *rsn_ie, size_t rsn_ie_len,
1162                                 struct wpa_ie_data *data)
1163 {
1164         struct rsn_ie_hdr *hdr;
1165         u8 *pos;
1166         int left;
1167         int i, count;
1168
1169         memset(data, 0, sizeof(*data));
1170         data->pairwise_cipher = WPA_CIPHER_CCMP;
1171         data->group_cipher = WPA_CIPHER_CCMP;
1172         data->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1173
1174         if (rsn_ie_len < sizeof(struct rsn_ie_hdr))
1175                 return -1;
1176
1177         hdr = (struct rsn_ie_hdr *) rsn_ie;
1178
1179         if (hdr->elem_id != WLAN_EID_RSN ||
1180             hdr->len != rsn_ie_len - 2 ||
1181             le_to_host16(hdr->version) != RSN_VERSION) {
1182                 return -2;
1183         }
1184
1185         pos = (u8 *) (hdr + 1);
1186         left = rsn_ie_len - sizeof(*hdr);
1187
1188         if (left >= RSN_SELECTOR_LEN) {
1189                 data->group_cipher = rsn_selector_to_bitfield(pos);
1190                 pos += RSN_SELECTOR_LEN;
1191                 left -= RSN_SELECTOR_LEN;
1192         } else if (left > 0)
1193                   return -3;
1194
1195         if (left >= 2) {
1196                 data->pairwise_cipher = 0;
1197                 count = pos[0] | (pos[1] << 8);
1198                 pos += 2;
1199                 left -= 2;
1200                 if (count == 0 || left < count * RSN_SELECTOR_LEN)
1201                         return -4;
1202                 for (i = 0; i < count; i++) {
1203                         data->pairwise_cipher |= rsn_selector_to_bitfield(pos);
1204                         pos += RSN_SELECTOR_LEN;
1205                         left -= RSN_SELECTOR_LEN;
1206                 }
1207         } else if (left == 1)
1208                 return -5;
1209
1210         if (left >= 2) {
1211                 data->key_mgmt = 0;
1212                 count = pos[0] | (pos[1] << 8);
1213                 pos += 2;
1214                 left -= 2;
1215                 if (count == 0 || left < count * RSN_SELECTOR_LEN)
1216                         return -6;
1217                 for (i = 0; i < count; i++) {
1218                         data->key_mgmt |= rsn_key_mgmt_to_bitfield(pos);
1219                         pos += RSN_SELECTOR_LEN;
1220                         left -= RSN_SELECTOR_LEN;
1221                 }
1222         } else if (left == 1)
1223                 return -7;
1224
1225         if (left >= 2) {
1226                 data->capabilities = pos[0] | (pos[1] << 8);
1227                 pos += 2;
1228                 left -= 2;
1229         }
1230
1231         if (left >= 2) {
1232                 data->num_pmkid = pos[0] | (pos[1] << 8);
1233                 pos += 2;
1234                 left -= 2;
1235                 if (left < data->num_pmkid * PMKID_LEN) {
1236                         printf("RSN: too short RSN IE for PMKIDs "
1237                                "(num=%lu, left=%d)\n",
1238                                (unsigned long) data->num_pmkid, left);
1239                         return -9;
1240                 }
1241                 data->pmkid = pos;
1242                 pos += data->num_pmkid * PMKID_LEN;
1243                 left -= data->num_pmkid * PMKID_LEN;
1244         }
1245
1246         if (left > 0) {
1247                 return -8;
1248         }
1249
1250         return 0;
1251 }
1252
1253
1254 int wpa_validate_wpa_ie(struct hostapd_data *hapd, struct sta_info *sta,
1255                         const u8 *wpa_ie, size_t wpa_ie_len, int version)
1256 {
1257         struct wpa_ie_data data;
1258         int ciphers, key_mgmt, res, i;
1259         const u8 *selector;
1260
1261         if (version == HOSTAPD_WPA_VERSION_WPA2) {
1262                 res = wpa_parse_wpa_ie_rsn(wpa_ie, wpa_ie_len, &data);
1263
1264                 selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
1265                 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1266                         selector = RSN_AUTH_KEY_MGMT_UNSPEC_802_1X;
1267                 else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
1268                         selector = RSN_AUTH_KEY_MGMT_PSK_OVER_802_1X;
1269                 memcpy(hapd->wpa_auth->dot11RSNAAuthenticationSuiteSelected,
1270                        selector, RSN_SELECTOR_LEN);
1271
1272                 selector = RSN_CIPHER_SUITE_CCMP;
1273                 if (data.pairwise_cipher & WPA_CIPHER_CCMP)
1274                         selector = RSN_CIPHER_SUITE_CCMP;
1275                 else if (data.pairwise_cipher & WPA_CIPHER_TKIP)
1276                         selector = RSN_CIPHER_SUITE_TKIP;
1277                 else if (data.pairwise_cipher & WPA_CIPHER_WEP104)
1278                         selector = RSN_CIPHER_SUITE_WEP104;
1279                 else if (data.pairwise_cipher & WPA_CIPHER_WEP40)
1280                         selector = RSN_CIPHER_SUITE_WEP40;
1281                 else if (data.pairwise_cipher & WPA_CIPHER_NONE)
1282                         selector = RSN_CIPHER_SUITE_NONE;
1283                 memcpy(hapd->wpa_auth->dot11RSNAPairwiseCipherSelected,
1284                        selector, RSN_SELECTOR_LEN);
1285
1286                 selector = RSN_CIPHER_SUITE_CCMP;
1287                 if (data.group_cipher & WPA_CIPHER_CCMP)
1288                         selector = RSN_CIPHER_SUITE_CCMP;
1289                 else if (data.group_cipher & WPA_CIPHER_TKIP)
1290                         selector = RSN_CIPHER_SUITE_TKIP;
1291                 else if (data.group_cipher & WPA_CIPHER_WEP104)
1292                         selector = RSN_CIPHER_SUITE_WEP104;
1293                 else if (data.group_cipher & WPA_CIPHER_WEP40)
1294                         selector = RSN_CIPHER_SUITE_WEP40;
1295                 else if (data.group_cipher & WPA_CIPHER_NONE)
1296                         selector = RSN_CIPHER_SUITE_NONE;
1297                 memcpy(hapd->wpa_auth->dot11RSNAGroupCipherSelected,
1298                        selector, RSN_SELECTOR_LEN);
1299         } else {
1300                 res = wpa_parse_wpa_ie_wpa(wpa_ie, wpa_ie_len, &data);
1301
1302                 selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
1303                 if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1304                         selector = WPA_AUTH_KEY_MGMT_UNSPEC_802_1X;
1305                 else if (data.key_mgmt & WPA_KEY_MGMT_PSK)
1306                         selector = WPA_AUTH_KEY_MGMT_PSK_OVER_802_1X;
1307                 memcpy(hapd->wpa_auth->dot11RSNAAuthenticationSuiteSelected,
1308                        selector, WPA_SELECTOR_LEN);
1309
1310                 selector = WPA_CIPHER_SUITE_TKIP;
1311                 if (data.pairwise_cipher & WPA_CIPHER_CCMP)
1312                         selector = WPA_CIPHER_SUITE_CCMP;
1313                 else if (data.pairwise_cipher & WPA_CIPHER_TKIP)
1314                         selector = WPA_CIPHER_SUITE_TKIP;
1315                 else if (data.pairwise_cipher & WPA_CIPHER_WEP104)
1316                         selector = WPA_CIPHER_SUITE_WEP104;
1317                 else if (data.pairwise_cipher & WPA_CIPHER_WEP40)
1318                         selector = WPA_CIPHER_SUITE_WEP40;
1319                 else if (data.pairwise_cipher & WPA_CIPHER_NONE)
1320                         selector = WPA_CIPHER_SUITE_NONE;
1321                 memcpy(hapd->wpa_auth->dot11RSNAPairwiseCipherSelected,
1322                        selector, WPA_SELECTOR_LEN);
1323
1324                 selector = WPA_CIPHER_SUITE_TKIP;
1325                 if (data.group_cipher & WPA_CIPHER_CCMP)
1326                         selector = WPA_CIPHER_SUITE_CCMP;
1327                 else if (data.group_cipher & WPA_CIPHER_TKIP)
1328                         selector = WPA_CIPHER_SUITE_TKIP;
1329                 else if (data.group_cipher & WPA_CIPHER_WEP104)
1330                         selector = WPA_CIPHER_SUITE_WEP104;
1331                 else if (data.group_cipher & WPA_CIPHER_WEP40)
1332                         selector = WPA_CIPHER_SUITE_WEP40;
1333                 else if (data.group_cipher & WPA_CIPHER_NONE)
1334                         selector = WPA_CIPHER_SUITE_NONE;
1335                 memcpy(hapd->wpa_auth->dot11RSNAGroupCipherSelected,
1336                        selector, WPA_SELECTOR_LEN);
1337         }
1338         if (res) {
1339                 printf("Failed to parse WPA/RSN IE from " MACSTR " (res=%d)\n",
1340                        MAC2STR(sta->addr), res);
1341                 hostapd_hexdump("WPA/RSN IE", wpa_ie, wpa_ie_len);
1342                 return WPA_INVALID_IE;
1343         }
1344
1345         if (data.group_cipher != hapd->conf->wpa_group) {
1346                 printf("Invalid WPA group cipher (0x%x) from " MACSTR "\n",
1347                        data.group_cipher, MAC2STR(sta->addr));
1348                 return WPA_INVALID_GROUP;
1349         }
1350
1351         key_mgmt = data.key_mgmt & hapd->conf->wpa_key_mgmt;
1352         if (!key_mgmt) {
1353                 printf("Invalid WPA key mgmt (0x%x) from " MACSTR "\n",
1354                        data.key_mgmt, MAC2STR(sta->addr));
1355                 return WPA_INVALID_AKMP;
1356         }
1357         if (key_mgmt & WPA_KEY_MGMT_IEEE8021X)
1358                 sta->wpa_key_mgmt = WPA_KEY_MGMT_IEEE8021X;
1359         else
1360                 sta->wpa_key_mgmt = WPA_KEY_MGMT_PSK;
1361
1362         ciphers = data.pairwise_cipher & hapd->conf->wpa_pairwise;
1363         if (!ciphers) {
1364                 printf("Invalid WPA pairwise cipher (0x%x) from " MACSTR "\n",
1365                        data.pairwise_cipher, MAC2STR(sta->addr));
1366                 return WPA_INVALID_PAIRWISE;
1367         }
1368
1369         if (ciphers & WPA_CIPHER_CCMP)
1370                 sta->pairwise = WPA_CIPHER_CCMP;
1371         else
1372                 sta->pairwise = WPA_CIPHER_TKIP;
1373
1374         /* TODO: clear WPA/WPA2 state if STA changes from one to another */
1375         if (wpa_ie[0] == WLAN_EID_RSN)
1376                 sta->wpa = WPA_VERSION_WPA2;
1377         else
1378                 sta->wpa = WPA_VERSION_WPA;
1379
1380         for (i = 0; i < data.num_pmkid; i++) {
1381                 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1382                         hostapd_hexdump("RSN IE: STA PMKID",
1383                                         &data.pmkid[i * PMKID_LEN], PMKID_LEN);
1384                 }
1385                 sta->pmksa = pmksa_cache_get(hapd, sta->addr,
1386                                              &data.pmkid[i * PMKID_LEN]);
1387                 if (sta->pmksa) {
1388                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1389                                        HOSTAPD_LEVEL_DEBUG,
1390                                        "PMKID found from PMKSA cache");
1391                         if (hapd->wpa_auth) {
1392                                 memcpy(hapd->wpa_auth->dot11RSNAPMKIDUsed,
1393                                        sta->pmksa->pmkid, PMKID_LEN);
1394                         }
1395                         break;
1396                 }
1397         }
1398
1399         return WPA_IE_OK;
1400 }
1401
1402
1403 void wpa_new_station(struct hostapd_data *hapd, struct sta_info *sta)
1404 {
1405         struct wpa_state_machine *sm;
1406
1407         if (!hapd->conf->wpa)
1408                 return;
1409
1410         if (sta->wpa_sm) {
1411                 sm = sta->wpa_sm;
1412                 memset(sm->key_replay_counter, 0, WPA_REPLAY_COUNTER_LEN);
1413                 sm->ReAuthenticationRequest = TRUE;
1414                 wpa_sm_step(sm);
1415                 return;
1416         }
1417
1418         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1419                        HOSTAPD_LEVEL_DEBUG, "start authentication");
1420         sm = malloc(sizeof(struct wpa_state_machine));
1421         if (sm == NULL)
1422                 return;
1423         memset(sm, 0, sizeof(struct wpa_state_machine));
1424
1425         sm->hapd = hapd;
1426         sm->sta = sta;
1427         sta->wpa_sm = sm;
1428
1429         sm->Init = TRUE;
1430         wpa_sm_step(sm);
1431         sm->Init = FALSE;
1432         sm->AuthenticationRequest = TRUE;
1433         wpa_sm_step(sm);
1434 }
1435
1436
1437 void wpa_free_station(struct sta_info *sta)
1438 {
1439         struct wpa_state_machine *sm = sta->wpa_sm;
1440
1441         if (sm == NULL)
1442                 return;
1443
1444         if (sm->hapd->conf->wpa_strict_rekey && sm->has_GTK) {
1445                 hostapd_logger(sm->hapd, sta->addr, HOSTAPD_MODULE_WPA,
1446                                HOSTAPD_LEVEL_DEBUG, "strict rekeying - force "
1447                                "GTK rekey since STA is leaving");
1448                 eloop_cancel_timeout(wpa_rekey_gtk, sm->hapd, NULL);
1449                 eloop_register_timeout(0, 500000, wpa_rekey_gtk, sm->hapd,
1450                                        NULL);
1451         }
1452
1453         eloop_cancel_timeout(wpa_send_eapol_timeout, sm->hapd, sta);
1454         eloop_cancel_timeout(wpa_sm_call_step, sm->hapd, sta->wpa_sm);
1455         eloop_cancel_timeout(rsn_preauth_finished_cb, sm->hapd, sta);
1456         free(sm->last_rx_eapol_key);
1457         free(sm);
1458         sta->wpa_sm = NULL;
1459 }
1460
1461
1462 static void wpa_request_new_ptk(struct hostapd_data *hapd,
1463                                 struct sta_info *sta)
1464 {
1465         struct wpa_state_machine *sm = sta->wpa_sm;
1466
1467         if (sm == NULL)
1468                 return;
1469
1470         sm->PTKRequest = TRUE;
1471         sm->PTK_valid = 0;
1472 }
1473
1474
1475 void wpa_receive(struct hostapd_data *hapd, struct sta_info *sta,
1476                  u8 *data, size_t data_len)
1477 {
1478         struct wpa_state_machine *sm = sta->wpa_sm;
1479         struct ieee802_1x_hdr *hdr;
1480         struct wpa_eapol_key *key;
1481         u16 key_info, key_data_length;
1482         enum { PAIRWISE_2, PAIRWISE_4, GROUP_2, REQUEST } msg;
1483         char *msgtxt;
1484
1485         if (!hapd->conf->wpa)
1486                 return;
1487
1488         if (sm == NULL)
1489                 return;
1490
1491         if (data_len < sizeof(*hdr) + sizeof(*key))
1492                 return;
1493
1494         hdr = (struct ieee802_1x_hdr *) data;
1495         key = (struct wpa_eapol_key *) (hdr + 1);
1496         key_info = ntohs(key->key_info);
1497         key_data_length = ntohs(key->key_data_length);
1498         if (key_data_length > data_len - sizeof(*hdr) - sizeof(*key)) {
1499                 wpa_printf(MSG_INFO, "WPA: Invalid EAPOL-Key frame - "
1500                            "key_data overflow (%d > %lu)",
1501                            key_data_length,
1502                            (unsigned long) (data_len - sizeof(*hdr) -
1503                                             sizeof(*key)));
1504                 return;
1505         }
1506
1507         /* FIX: verify that the EAPOL-Key frame was encrypted if pairwise keys
1508          * are set */
1509
1510         if (key_info & WPA_KEY_INFO_REQUEST) {
1511                 msg = REQUEST;
1512                 msgtxt = "Request";
1513         } else if (!(key_info & WPA_KEY_INFO_KEY_TYPE)) {
1514                 msg = GROUP_2;
1515                 msgtxt = "2/2 Group";
1516         } else if (key_data_length == 0) {
1517                 msg = PAIRWISE_4;
1518                 msgtxt = "4/4 Pairwise";
1519         } else {
1520                 msg = PAIRWISE_2;
1521                 msgtxt = "2/4 Pairwise";
1522         }
1523
1524         if (key_info & WPA_KEY_INFO_REQUEST) {
1525                 if (sta->req_replay_counter_used &&
1526                     memcmp(key->replay_counter, sta->req_replay_counter,
1527                            WPA_REPLAY_COUNTER_LEN) <= 0) {
1528                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1529                                        HOSTAPD_LEVEL_WARNING,
1530                                        "received EAPOL-Key request with "
1531                                        "replayed counter");
1532                         return;
1533                 }
1534         }
1535
1536         if (!(key_info & WPA_KEY_INFO_REQUEST) &&
1537             (!sm->key_replay_counter_valid ||
1538              memcmp(key->replay_counter, sm->key_replay_counter,
1539                     WPA_REPLAY_COUNTER_LEN) != 0)) {
1540                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1541                                HOSTAPD_LEVEL_INFO,
1542                                "received EAPOL-Key %s with unexpected replay "
1543                                "counter", msgtxt);
1544                 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1545                         hostapd_hexdump("expected replay counter",
1546                                         sm->key_replay_counter,
1547                                         WPA_REPLAY_COUNTER_LEN);
1548                         hostapd_hexdump("received replay counter",
1549                                         key->replay_counter,
1550                                         WPA_REPLAY_COUNTER_LEN);
1551                 }
1552                 return;
1553         }
1554
1555         switch (msg) {
1556         case PAIRWISE_2:
1557                 if (sm->wpa_ptk_state != WPA_PTK_PTKSTART &&
1558                     sm->wpa_ptk_state != WPA_PTK_PTKCALCNEGOTIATING) {
1559                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1560                                        HOSTAPD_LEVEL_INFO,
1561                                        "received EAPOL-Key msg 2/4 in invalid"
1562                                        " state (%d) - dropped",
1563                                        sm->wpa_ptk_state);
1564                         return;
1565                 }
1566                 if (sta->wpa_ie == NULL ||
1567                     sta->wpa_ie_len != key_data_length ||
1568                     memcmp(sta->wpa_ie, key + 1, key_data_length) != 0) {
1569                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1570                                        HOSTAPD_LEVEL_INFO,
1571                                        "WPA IE from (Re)AssocReq did not match"
1572                                        " with msg 2/4");
1573                         if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1574                                 if (sta->wpa_ie) {
1575                                         hostapd_hexdump("WPA IE in AssocReq",
1576                                                         sta->wpa_ie,
1577                                                         sta->wpa_ie_len);
1578                                 }
1579                                 hostapd_hexdump("WPA IE in msg 2/4",
1580                                                 (u8 *) (key + 1),
1581                                                 key_data_length);
1582                         }
1583                         /* MLME-DEAUTHENTICATE.request */
1584                         wpa_sta_disconnect(hapd, sta);
1585                         return;
1586                 }
1587                 break;
1588         case PAIRWISE_4:
1589                 if (sm->wpa_ptk_state != WPA_PTK_PTKINITNEGOTIATING ||
1590                     !sm->PTK_valid) {
1591                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1592                                        HOSTAPD_LEVEL_INFO,
1593                                        "received EAPOL-Key msg 4/4 in invalid"
1594                                        " state (%d) - dropped",
1595                                        sm->wpa_ptk_state);
1596                         return;
1597                 }
1598                 break;
1599         case GROUP_2:
1600                 if (sm->wpa_ptk_group_state != WPA_PTK_GROUP_REKEYNEGOTIATING
1601                     || !sm->PTK_valid) {
1602                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1603                                        HOSTAPD_LEVEL_INFO,
1604                                        "received EAPOL-Key msg 2/2 in invalid"
1605                                        " state (%d) - dropped",
1606                                        sm->wpa_ptk_group_state);
1607                         return;
1608                 }
1609                 break;
1610         case REQUEST:
1611                 break;
1612         }
1613
1614         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1615                        HOSTAPD_LEVEL_DEBUG, "received EAPOL-Key frame (%s)",
1616                        msgtxt);
1617
1618         if (key_info & WPA_KEY_INFO_ACK) {
1619                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1620                                HOSTAPD_LEVEL_INFO,
1621                                "received invalid EAPOL-Key: Key Ack set");
1622                 return;
1623         }
1624
1625         if (!(key_info & WPA_KEY_INFO_MIC)) {
1626                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1627                                HOSTAPD_LEVEL_INFO,
1628                                "received invalid EAPOL-Key: Key MIC not set");
1629                 return;
1630         }
1631
1632         sm->MICVerified = FALSE;
1633         if (sm->PTK_valid) {
1634                 if (wpa_verify_key_mic(&sm->PTK, data, data_len)) {
1635                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1636                                        HOSTAPD_LEVEL_INFO,
1637                                        "received EAPOL-Key with invalid MIC");
1638                         return;
1639                 }
1640                 sm->MICVerified = TRUE;
1641                 eloop_cancel_timeout(wpa_send_eapol_timeout, sta->wpa_sm->hapd,
1642                                      sta);
1643         }
1644
1645         if (key_info & WPA_KEY_INFO_REQUEST) {
1646                 if (sm->MICVerified) {
1647                         sta->req_replay_counter_used = 1;
1648                         memcpy(sta->req_replay_counter, key->replay_counter,
1649                                WPA_REPLAY_COUNTER_LEN);
1650                 } else {
1651                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1652                                        HOSTAPD_LEVEL_INFO,
1653                                        "received EAPOL-Key request with "
1654                                        "invalid MIC");
1655                         return;
1656                 }
1657
1658                 if (key_info & WPA_KEY_INFO_ERROR) {
1659                         /* Supplicant reported a Michael MIC error */
1660                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1661                                        HOSTAPD_LEVEL_INFO,
1662                                        "received EAPOL-Key Error Request "
1663                                        "(STA detected Michael MIC failure)");
1664                         ieee80211_michael_mic_failure(hapd, sta->addr, 0);
1665                         sta->dot11RSNAStatsTKIPRemoteMICFailures++;
1666                         hapd->wpa_auth->dot11RSNAStatsTKIPRemoteMICFailures++;
1667                         /* Error report is not a request for a new key
1668                          * handshake, but since Authenticator may do it, let's
1669                          * change the keys now anyway. */
1670                         wpa_request_new_ptk(hapd, sta);
1671                 } else if (key_info & WPA_KEY_INFO_KEY_TYPE) {
1672                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1673                                        HOSTAPD_LEVEL_INFO,
1674                                        "received EAPOL-Key Request for new "
1675                                        "4-Way Handshake");
1676                         wpa_request_new_ptk(hapd, sta);
1677                 } else {
1678                         /* TODO: this could also be a request for STAKey
1679                          * if Key Data fields contains peer MAC address KDE.
1680                          * STAKey request should have 0xdd <len> 00-0F-AC:2 in
1681                          * the beginning of Key Data */
1682                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1683                                        HOSTAPD_LEVEL_INFO,
1684                                        "received EAPOL-Key Request for GTK "
1685                                        "rekeying");
1686                         wpa_request_new_ptk(hapd, sta);
1687                         eloop_cancel_timeout(wpa_rekey_gtk, hapd, NULL);
1688                         wpa_rekey_gtk(hapd, NULL);
1689                 }
1690         } else {
1691                 /* Do not allow the same key replay counter to be reused. */
1692                 sm->key_replay_counter_valid = FALSE;
1693         }
1694
1695         free(sm->last_rx_eapol_key);
1696         sm->last_rx_eapol_key = malloc(data_len);
1697         if (sm->last_rx_eapol_key == NULL)
1698                 return;
1699         memcpy(sm->last_rx_eapol_key, data, data_len);
1700         sm->last_rx_eapol_key_len = data_len;
1701
1702         sm->EAPOLKeyReceived = TRUE;
1703         sm->EAPOLKeyPairwise = !!(key_info & WPA_KEY_INFO_KEY_TYPE);
1704         sm->EAPOLKeyRequest = !!(key_info & WPA_KEY_INFO_REQUEST);
1705         memcpy(sm->SNonce, key->key_nonce, WPA_NONCE_LEN);
1706         wpa_sm_step(sm);
1707 }
1708
1709
1710 static void wpa_pmk_to_ptk(struct hostapd_data *hapd, const u8 *pmk,
1711                            const u8 *addr1, const u8 *addr2,
1712                            const u8 *nonce1, const u8 *nonce2,
1713                            u8 *ptk, size_t ptk_len)
1714 {
1715         u8 data[2 * ETH_ALEN + 2 * WPA_NONCE_LEN];
1716
1717         /* PTK = PRF-X(PMK, "Pairwise key expansion",
1718          *             Min(AA, SA) || Max(AA, SA) ||
1719          *             Min(ANonce, SNonce) || Max(ANonce, SNonce)) */
1720
1721         if (memcmp(addr1, addr2, ETH_ALEN) < 0) {
1722                 memcpy(data, addr1, ETH_ALEN);
1723                 memcpy(data + ETH_ALEN, addr2, ETH_ALEN);
1724         } else {
1725                 memcpy(data, addr2, ETH_ALEN);
1726                 memcpy(data + ETH_ALEN, addr1, ETH_ALEN);
1727         }
1728
1729         if (memcmp(nonce1, nonce2, WPA_NONCE_LEN) < 0) {
1730                 memcpy(data + 2 * ETH_ALEN, nonce1, WPA_NONCE_LEN);
1731                 memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce2,
1732                        WPA_NONCE_LEN);
1733         } else {
1734                 memcpy(data + 2 * ETH_ALEN, nonce2, WPA_NONCE_LEN);
1735                 memcpy(data + 2 * ETH_ALEN + WPA_NONCE_LEN, nonce1,
1736                        WPA_NONCE_LEN);
1737         }
1738
1739         sha1_prf(pmk, WPA_PMK_LEN, "Pairwise key expansion",
1740                  data, sizeof(data), ptk, ptk_len);
1741
1742         if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1743                 hostapd_hexdump("PMK", pmk, WPA_PMK_LEN);
1744                 hostapd_hexdump("PTK", ptk, ptk_len);
1745         }
1746 }
1747
1748
1749 static void wpa_gmk_to_gtk(struct hostapd_data *hapd, u8 *gmk,
1750                            u8 *addr, u8 *gnonce, u8 *gtk, size_t gtk_len)
1751 {
1752         u8 data[ETH_ALEN + WPA_NONCE_LEN];
1753
1754         /* GTK = PRF-X(GMK, "Group key expansion", AA || GNonce) */
1755         memcpy(data, addr, ETH_ALEN);
1756         memcpy(data + ETH_ALEN, gnonce, WPA_NONCE_LEN);
1757
1758         sha1_prf(gmk, WPA_GMK_LEN, "Group key expansion",
1759                  data, sizeof(data), gtk, gtk_len);
1760
1761         if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1762                 hostapd_hexdump("GMK", gmk, WPA_GMK_LEN);
1763                 hostapd_hexdump("GTK", gtk, gtk_len);
1764         }
1765 }
1766
1767
1768 static void wpa_send_eapol_timeout(void *eloop_ctx, void *timeout_ctx)
1769 {
1770         struct hostapd_data *hapd = eloop_ctx;
1771         struct sta_info *sta = timeout_ctx;
1772
1773         if (!sta->wpa_sm || !(sta->flags & WLAN_STA_ASSOC))
1774                 return;
1775
1776         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1777                        HOSTAPD_LEVEL_DEBUG, "EAPOL-Key timeout");
1778         sta->wpa_sm->TimeoutEvt = TRUE;
1779         wpa_sm_step(sta->wpa_sm);
1780 }
1781
1782
1783 static int wpa_calc_eapol_key_mic(int ver, u8 *key, u8 *data, size_t len,
1784                                   u8 *mic)
1785 {
1786         u8 hash[SHA1_MAC_LEN];
1787
1788         switch (ver) {
1789         case WPA_KEY_INFO_TYPE_HMAC_MD5_RC4:
1790                 hmac_md5(key, 16, data, len, mic);
1791                 break;
1792         case WPA_KEY_INFO_TYPE_HMAC_SHA1_AES:
1793                 hmac_sha1(key, 16, data, len, hash);
1794                 memcpy(mic, hash, MD5_MAC_LEN);
1795                 break;
1796         default:
1797                 return -1;
1798         }
1799         return 0;
1800 }
1801
1802
1803 static void wpa_send_eapol(struct hostapd_data *hapd, struct sta_info *sta,
1804                            int secure, int mic, int ack, int install,
1805                            int pairwise, u8 *key_rsc, u8 *nonce,
1806                            u8 *ie, size_t ie_len, u8 *gtk, size_t gtk_len,
1807                            int keyidx)
1808 {
1809         struct wpa_state_machine *sm = sta->wpa_sm;
1810         struct ieee802_1x_hdr *hdr;
1811         struct wpa_eapol_key *key;
1812         size_t len;
1813         int key_info, alg;
1814         int timeout_ms;
1815         int key_data_len, pad_len = 0;
1816         u8 *buf, *pos;
1817
1818         if (sm == NULL)
1819                 return;
1820
1821         len = sizeof(struct ieee802_1x_hdr) + sizeof(struct wpa_eapol_key);
1822
1823         if (sta->wpa == WPA_VERSION_WPA2) {
1824                 key_data_len = ie_len + gtk_len;
1825                 if (gtk_len)
1826                         key_data_len += 2 + RSN_SELECTOR_LEN + 2;
1827         } else {
1828                 if (pairwise) {
1829                         /* WPA does not include GTK in 4-Way Handshake */
1830                         gtk = NULL;
1831                         gtk_len = 0;
1832
1833                         /* key_rsc is for group key, so mask it out in case of
1834                          * WPA Pairwise key negotiation. */
1835                         key_rsc = NULL;
1836                 }
1837                 key_data_len = ie_len + gtk_len;
1838         }
1839
1840         if (sta->pairwise == WPA_CIPHER_CCMP) {
1841                 key_info = WPA_KEY_INFO_TYPE_HMAC_SHA1_AES;
1842                 if (gtk) {
1843                         pad_len = key_data_len % 8;
1844                         if (pad_len)
1845                                 pad_len = 8 - pad_len;
1846                         key_data_len += pad_len + 8;
1847                 }
1848         } else {
1849                 key_info = WPA_KEY_INFO_TYPE_HMAC_MD5_RC4;
1850         }
1851
1852         len += key_data_len;
1853
1854         hdr = malloc(len);
1855         if (hdr == NULL)
1856                 return;
1857         memset(hdr, 0, len);
1858         hdr->version = EAPOL_VERSION;
1859         hdr->type = IEEE802_1X_TYPE_EAPOL_KEY;
1860         hdr->length = htons(len  - sizeof(*hdr));
1861         key = (struct wpa_eapol_key *) (hdr + 1);
1862
1863         key->type = sta->wpa == WPA_VERSION_WPA2 ?
1864                 EAPOL_KEY_TYPE_RSN : EAPOL_KEY_TYPE_WPA;
1865         if (secure)
1866                 key_info |= WPA_KEY_INFO_SECURE;
1867         if (mic)
1868                 key_info |= WPA_KEY_INFO_MIC;
1869         if (ack)
1870                 key_info |= WPA_KEY_INFO_ACK;
1871         if (install)
1872                 key_info |= WPA_KEY_INFO_INSTALL;
1873         if (pairwise)
1874                 key_info |= WPA_KEY_INFO_KEY_TYPE;
1875         if (gtk && sta->wpa == WPA_VERSION_WPA2)
1876                 key_info |= WPA_KEY_INFO_ENCR_KEY_DATA;
1877         if (sta->wpa != WPA_VERSION_WPA2) {
1878                 if (pairwise)
1879                         keyidx = 0;
1880                 key_info |= keyidx << WPA_KEY_INFO_KEY_INDEX_SHIFT;
1881         }
1882         key->key_info = htons(key_info);
1883
1884         alg = pairwise ? sta->pairwise : hapd->conf->wpa_group;
1885         switch (alg) {
1886         case WPA_CIPHER_CCMP:
1887                 key->key_length = htons(16);
1888                 break;
1889         case WPA_CIPHER_TKIP:
1890                 key->key_length = htons(32);
1891                 break;
1892         case WPA_CIPHER_WEP40:
1893                 key->key_length = htons(5);
1894                 break;
1895         case WPA_CIPHER_WEP104:
1896                 key->key_length = htons(13);
1897                 break;
1898         }
1899
1900         inc_byte_array(sm->key_replay_counter, WPA_REPLAY_COUNTER_LEN);
1901         memcpy(key->replay_counter, sm->key_replay_counter,
1902                WPA_REPLAY_COUNTER_LEN);
1903         sm->key_replay_counter_valid = TRUE;
1904
1905         if (nonce)
1906                 memcpy(key->key_nonce, nonce, WPA_NONCE_LEN);
1907
1908         if (key_rsc)
1909                 memcpy(key->key_rsc, key_rsc, WPA_KEY_RSC_LEN);
1910
1911         if (ie && !gtk) {
1912                 memcpy(key + 1, ie, ie_len);
1913                 key->key_data_length = htons(ie_len);
1914         } else if (gtk) {
1915                 buf = malloc(key_data_len);
1916                 if (buf == NULL) {
1917                         free(hdr);
1918                         return;
1919                 }
1920                 memset(buf, 0, key_data_len);
1921                 pos = buf;
1922                 if (ie) {
1923                         memcpy(pos, ie, ie_len);
1924                         pos += ie_len;
1925                 }
1926                 if (sta->wpa == WPA_VERSION_WPA2) {
1927                         *pos++ = WLAN_EID_GENERIC;
1928                         *pos++ = RSN_SELECTOR_LEN + 2 + gtk_len;
1929                         memcpy(pos, RSN_KEY_DATA_GROUPKEY, RSN_SELECTOR_LEN);
1930                         pos += RSN_SELECTOR_LEN;
1931                         *pos++ = keyidx & 0x03;
1932                         *pos++ = 0;
1933                 }
1934                 memcpy(pos, gtk, gtk_len);
1935                 pos += gtk_len;
1936                 if (pad_len)
1937                         *pos++ = 0xdd;
1938
1939                 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MINIMAL)) {
1940                         hostapd_hexdump("Plaintext EAPOL-Key Key Data",
1941                                         buf, key_data_len);
1942                 }
1943                 if (key_info & WPA_KEY_INFO_TYPE_HMAC_SHA1_AES) {
1944                         aes_wrap(sm->PTK.encr_key, (key_data_len - 8) / 8, buf,
1945                                  (u8 *) (key + 1));
1946                         key->key_data_length = htons(key_data_len);
1947                 } else {
1948                         u8 ek[32];
1949                         memcpy(key->key_iv,
1950                                hapd->wpa_auth->Counter + WPA_NONCE_LEN - 16,
1951                                16);
1952                         inc_byte_array(hapd->wpa_auth->Counter, WPA_NONCE_LEN);
1953                         memcpy(ek, key->key_iv, 16);
1954                         memcpy(ek + 16, sm->PTK.encr_key, 16);
1955                         memcpy(key + 1, buf, key_data_len);
1956                         rc4_skip(ek, 32, 256, (u8 *) (key + 1), key_data_len);
1957                         key->key_data_length = htons(key_data_len);
1958                 }
1959                 free(buf);
1960         }
1961
1962         if (mic) {
1963                 if (!sm->PTK_valid) {
1964                         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
1965                                        HOSTAPD_LEVEL_DEBUG, "PTK not valid "
1966                                        "when sending EAPOL-Key frame");
1967                         free(hdr);
1968                         return;
1969                 }
1970                 wpa_calc_eapol_key_mic(key_info & WPA_KEY_INFO_TYPE_MASK,
1971                                        sm->PTK.mic_key, (u8 *) hdr, len,
1972                                        key->key_mic);
1973         }
1974
1975         if (sta->eapol_sm)
1976                 sta->eapol_sm->dot1xAuthEapolFramesTx++;
1977         hostapd_send_eapol(hapd, sta->addr, (u8 *) hdr, len, sm->pairwise_set);
1978         free(hdr);
1979
1980         timeout_ms = pairwise ? dot11RSNAConfigPairwiseUpdateTimeOut :
1981                 dot11RSNAConfigGroupUpdateTimeOut;
1982         eloop_register_timeout(timeout_ms / 1000, (timeout_ms % 1000) * 1000,
1983                                wpa_send_eapol_timeout, hapd, sta);
1984 }
1985
1986
1987 static int wpa_verify_key_mic(struct wpa_ptk *PTK, u8 *data, size_t data_len)
1988 {
1989         struct ieee802_1x_hdr *hdr;
1990         struct wpa_eapol_key *key;
1991         u16 key_info;
1992         int type, ret = 0;
1993         u8 mic[16];
1994
1995         if (data_len < sizeof(*hdr) + sizeof(*key))
1996                 return -1;
1997
1998         hdr = (struct ieee802_1x_hdr *) data;
1999         key = (struct wpa_eapol_key *) (hdr + 1);
2000         key_info = ntohs(key->key_info);
2001         type = key_info & WPA_KEY_INFO_TYPE_MASK;
2002         memcpy(mic, key->key_mic, 16);
2003         memset(key->key_mic, 0, 16);
2004         if (wpa_calc_eapol_key_mic(key_info & WPA_KEY_INFO_TYPE_MASK,
2005                                    PTK->mic_key, data, data_len, key->key_mic)
2006             || memcmp(mic, key->key_mic, 16) != 0)
2007                 ret = -1;
2008         memcpy(key->key_mic, mic, 16);
2009         return ret;
2010 }
2011
2012
2013 void wpa_sm_event(struct hostapd_data *hapd, struct sta_info *sta,
2014                   wpa_event event)
2015 {
2016         struct wpa_state_machine *sm = sta->wpa_sm;
2017         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_WPA,
2018                        HOSTAPD_LEVEL_DEBUG,
2019                        "event %d notification", event);
2020         if (sm == NULL)
2021                 return;
2022
2023         switch (event) {
2024         case WPA_AUTH:
2025         case WPA_ASSOC:
2026                 break;
2027         case WPA_DEAUTH:
2028         case WPA_DISASSOC:
2029                 sm->DeauthenticationRequest = TRUE;
2030                 break;
2031         case WPA_REAUTH:
2032         case WPA_REAUTH_EAPOL:
2033                 sm->ReAuthenticationRequest = TRUE;
2034                 break;
2035         }
2036
2037         sm->PTK_valid = FALSE;
2038         memset(&sm->PTK, 0, sizeof(sm->PTK));
2039
2040         if (event != WPA_REAUTH_EAPOL) {
2041                 sm->pairwise_set = FALSE;
2042                 hostapd_set_encryption(sm->hapd, "none", sm->sta->addr, 0,
2043                                        (u8 *) "", 0);
2044         }
2045
2046         wpa_sm_step(sm);
2047 }
2048
2049
2050 static const char * wpa_alg_txt(int alg)
2051 {
2052         switch (alg) {
2053         case WPA_CIPHER_CCMP:
2054                 return "CCMP";
2055         case WPA_CIPHER_TKIP:
2056                 return "TKIP";
2057         case WPA_CIPHER_WEP104:
2058         case WPA_CIPHER_WEP40:
2059                 return "WEP";
2060         default:
2061                 return "";
2062         }
2063 }
2064
2065
2066 /* Definitions for clarifying state machine implementation */
2067 #define SM_STATE(machine, state) \
2068 static void sm_ ## machine ## _ ## state ## _Enter(struct wpa_state_machine \
2069 *sm)
2070
2071 #define SM_ENTRY(machine, _state, _data) \
2072 sm->changed = TRUE; \
2073 sm->_data ## _ ## state = machine ## _ ## _state; \
2074 if (sm->hapd->conf->debug >= HOSTAPD_DEBUG_MINIMAL) \
2075         printf("WPA: " MACSTR " " #machine " entering state " #_state \
2076                 "\n", MAC2STR(sm->sta->addr));
2077
2078 #define SM_ENTER(machine, state) sm_ ## machine ## _ ## state ## _Enter(sm)
2079
2080 #define SM_STEP(machine) \
2081 static void sm_ ## machine ## _Step(struct wpa_state_machine *sm)
2082
2083 #define SM_STEP_RUN(machine) sm_ ## machine ## _Step(sm)
2084
2085
2086 SM_STATE(WPA_PTK, INITIALIZE)
2087 {
2088         struct hostapd_data *hapd = sm->hapd;
2089
2090         SM_ENTRY(WPA_PTK, INITIALIZE, wpa_ptk);
2091         if (sm->Init) {
2092                 /* Init flag is not cleared here, so avoid busy
2093                  * loop by claiming nothing changed. */
2094                 sm->changed = FALSE;
2095         }
2096
2097         sm->keycount = 0;
2098         if (sm->GUpdateStationKeys)
2099                 hapd->wpa_auth->GKeyDoneStations--;
2100         sm->GUpdateStationKeys = FALSE;
2101         if (sm->sta->wpa == WPA_VERSION_WPA)
2102                 sm->PInitAKeys = FALSE;
2103         if (1 /* Unicast cipher supported AND (ESS OR ((IBSS or WDS) and
2104                * Local AA > Remote AA)) */) {
2105                 sm->Pair = TRUE;
2106         }
2107         ieee802_1x_notify_port_enabled(sm->sta->eapol_sm, 0);
2108         hostapd_set_encryption(sm->hapd, "none", sm->sta->addr, 0, (u8 *) "",
2109                                0);
2110         sm->pairwise_set = FALSE;
2111         sm->PTK_valid = FALSE;
2112         memset(&sm->PTK, 0, sizeof(sm->PTK));
2113         ieee802_1x_notify_port_valid(sm->sta->eapol_sm, 0);
2114         sm->TimeoutCtr = 0;
2115         if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK)
2116                 ieee802_1x_set_sta_authorized(sm->hapd, sm->sta, 0);
2117 }
2118
2119
2120 SM_STATE(WPA_PTK, DISCONNECT)
2121 {
2122         SM_ENTRY(WPA_PTK, DISCONNECT, wpa_ptk);
2123         sm->Disconnect = FALSE;
2124         wpa_sta_disconnect(sm->hapd, sm->sta);
2125 }
2126
2127
2128 SM_STATE(WPA_PTK, DISCONNECTED)
2129 {
2130         SM_ENTRY(WPA_PTK, DISCONNECTED, wpa_ptk);
2131         sm->hapd->wpa_auth->GNoStations--;
2132         sm->DeauthenticationRequest = FALSE;
2133 }
2134
2135
2136 SM_STATE(WPA_PTK, AUTHENTICATION)
2137 {
2138         SM_ENTRY(WPA_PTK, AUTHENTICATION, wpa_ptk);
2139         sm->hapd->wpa_auth->GNoStations++;
2140         memset(&sm->PTK, 0, sizeof(sm->PTK));
2141         sm->PTK_valid = FALSE;
2142         if (sm->sta->eapol_sm) {
2143                 sm->sta->eapol_sm->portControl = Auto;
2144                 sm->sta->eapol_sm->portEnabled = TRUE;
2145         }
2146         sm->AuthenticationRequest = FALSE;
2147 }
2148
2149
2150 SM_STATE(WPA_PTK, AUTHENTICATION2)
2151 {
2152         SM_ENTRY(WPA_PTK, AUTHENTICATION2, wpa_ptk);
2153         memcpy(sm->ANonce, sm->hapd->wpa_auth->Counter, WPA_NONCE_LEN);
2154         inc_byte_array(sm->hapd->wpa_auth->Counter, WPA_NONCE_LEN);
2155         sm->ReAuthenticationRequest = FALSE;
2156         /* IEEE 802.11i/D9.0 does not clear TimeoutCtr here, but this is more
2157          * logical place than INITIALIZE since AUTHENTICATION2 can be
2158          * re-entered on ReAuthenticationRequest without going through
2159          * INITIALIZE. */
2160         sm->TimeoutCtr = 0;
2161 }
2162
2163
2164 SM_STATE(WPA_PTK, INITPMK)
2165 {
2166         u8 *key;
2167         size_t len;
2168         SM_ENTRY(WPA_PTK, INITPMK, wpa_ptk);
2169         if (sm->sta->pmksa) {
2170                 wpa_printf(MSG_DEBUG, "WPA: PMK from PMKSA cache");
2171                 memcpy(sm->PMK, sm->sta->pmksa->pmk, WPA_PMK_LEN);
2172                 pmksa_cache_to_eapol_data(sm->sta->pmksa, sm->sta->eapol_sm);
2173         } else if ((key = ieee802_1x_get_key_crypt(sm->sta->eapol_sm, &len))) {
2174                 wpa_printf(MSG_DEBUG, "WPA: PMK from EAPOL state machine "
2175                            "(len=%lu)", (unsigned long) len);
2176                 if (len > WPA_PMK_LEN)
2177                         len = WPA_PMK_LEN;
2178                 memcpy(sm->PMK, key, len);
2179         } else {
2180                 wpa_printf(MSG_DEBUG, "WPA: Could not get PMK");
2181         }
2182         sm->sta->req_replay_counter_used = 0;
2183         /* IEEE 802.11i/D9.0 does not set keyRun to FALSE, but not doing this
2184          * will break reauthentication since EAPOL state machines may not be
2185          * get into AUTHENTICATING state that clears keyRun before WPA state
2186          * machine enters AUTHENTICATION2 state and goes immediately to INITPMK
2187          * state and takes PMK from the previously used AAA Key. This will
2188          * eventually fail in 4-Way Handshake because Supplicant uses PMK
2189          * derived from the new AAA Key. Setting keyRun = FALSE here seems to
2190          * be good workaround for this issue. */
2191         if (sm->sta->eapol_sm)
2192                 sm->sta->eapol_sm->keyRun = FALSE;
2193 }
2194
2195
2196 SM_STATE(WPA_PTK, INITPSK)
2197 {
2198         const u8 *psk;
2199         SM_ENTRY(WPA_PTK, INITPSK, wpa_ptk);
2200         psk = hostapd_get_psk(sm->hapd->conf, sm->sta->addr, NULL);
2201         if (psk)
2202                 memcpy(sm->PMK, psk, WPA_PMK_LEN);
2203         sm->sta->req_replay_counter_used = 0;
2204 }
2205
2206
2207 SM_STATE(WPA_PTK, PTKSTART)
2208 {
2209         u8 *pmkid = NULL;
2210         size_t pmkid_len = 0;
2211
2212         SM_ENTRY(WPA_PTK, PTKSTART, wpa_ptk);
2213         sm->PTKRequest = FALSE;
2214         sm->TimeoutEvt = FALSE;
2215         hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2216                        HOSTAPD_LEVEL_DEBUG,
2217                        "sending 1/4 msg of 4-Way Handshake");
2218         if (sm->sta->pmksa &&
2219             (pmkid = malloc(2 + RSN_SELECTOR_LEN + PMKID_LEN))) {
2220                 pmkid_len = 2 + RSN_SELECTOR_LEN + PMKID_LEN;
2221                 pmkid[0] = WLAN_EID_GENERIC;
2222                 pmkid[1] = RSN_SELECTOR_LEN + PMKID_LEN;
2223                 memcpy(&pmkid[2], RSN_KEY_DATA_PMKID, RSN_SELECTOR_LEN);
2224                 memcpy(&pmkid[2 + RSN_SELECTOR_LEN], sm->sta->pmksa->pmkid,
2225                        PMKID_LEN);
2226         }
2227         wpa_send_eapol(sm->hapd, sm->sta, 0, 0, 1, 0, 1, NULL, sm->ANonce,
2228                        pmkid, pmkid_len, NULL, 0, 0);
2229         free(pmkid);
2230         sm->TimeoutCtr++;
2231 }
2232
2233
2234 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING)
2235 {
2236         struct wpa_ptk PTK;
2237         int ok = 0;
2238         const u8 *pmk = NULL;
2239
2240         SM_ENTRY(WPA_PTK, PTKCALCNEGOTIATING, wpa_ptk);
2241         sm->EAPOLKeyReceived = FALSE;
2242
2243         /* WPA with IEEE 802.1X: use the derived PMK from EAP
2244          * WPA-PSK: iterate through possible PSKs and select the one matching
2245          * the packet */
2246         for (;;) {
2247                 if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK) {
2248                         pmk = hostapd_get_psk(sm->hapd->conf, sm->sta->addr,
2249                                               pmk);
2250                         if (pmk == NULL)
2251                                 break;
2252                 } else
2253                         pmk = sm->PMK;
2254
2255                 wpa_pmk_to_ptk(sm->hapd, pmk, sm->hapd->own_addr,
2256                                sm->sta->addr, sm->ANonce, sm->SNonce,
2257                                (u8 *) &PTK, sizeof(PTK));
2258
2259                 if (wpa_verify_key_mic(&PTK, sm->last_rx_eapol_key,
2260                                        sm->last_rx_eapol_key_len) == 0) {
2261                         ok = 1;
2262                         break;
2263                 }
2264
2265                 if (sm->sta->wpa_key_mgmt != WPA_KEY_MGMT_PSK)
2266                         break;
2267         }
2268
2269         if (!ok) {
2270                 hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2271                                HOSTAPD_LEVEL_DEBUG, "invalid MIC in msg 2/4 "
2272                                "of 4-Way Handshake");
2273                 return;
2274         }
2275
2276         eloop_cancel_timeout(wpa_send_eapol_timeout, sm->hapd, sm->sta);
2277
2278         if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK) {
2279                 /* PSK may have changed from the previous choice, so update
2280                  * state machine data based on whatever PSK was selected here.
2281                  */
2282                 memcpy(sm->PMK, pmk, WPA_PMK_LEN);
2283         }
2284
2285         sm->MICVerified = TRUE;
2286
2287         memcpy(&sm->PTK, &PTK, sizeof(PTK));
2288         sm->PTK_valid = TRUE;
2289 }
2290
2291
2292 SM_STATE(WPA_PTK, PTKCALCNEGOTIATING2)
2293 {
2294         SM_ENTRY(WPA_PTK, PTKCALCNEGOTIATING2, wpa_ptk);
2295         sm->TimeoutCtr = 0;
2296 }
2297
2298
2299 SM_STATE(WPA_PTK, PTKINITNEGOTIATING)
2300 {
2301         u8 rsc[WPA_KEY_RSC_LEN];
2302         struct wpa_authenticator *gsm = sm->hapd->wpa_auth;
2303         u8 *wpa_ie;
2304         int wpa_ie_len;
2305
2306         SM_ENTRY(WPA_PTK, PTKINITNEGOTIATING, wpa_ptk);
2307         sm->TimeoutEvt = FALSE;
2308         /* Send EAPOL(1, 1, 1, Pair, P, RSC, ANonce, MIC(PTK), RSNIE, GTK[GN])
2309          */
2310         memset(rsc, 0, WPA_KEY_RSC_LEN);
2311         hostapd_get_seqnum(sm->hapd, NULL, gsm->GN, rsc);
2312         wpa_ie = sm->hapd->wpa_ie;
2313         wpa_ie_len = sm->hapd->wpa_ie_len;
2314         if (sm->sta->wpa == WPA_VERSION_WPA &&
2315             (sm->hapd->conf->wpa & HOSTAPD_WPA_VERSION_WPA2) &&
2316             wpa_ie_len > wpa_ie[1] + 2 && wpa_ie[0] == WLAN_EID_RSN) {
2317                 /* WPA-only STA, remove RSN IE */
2318                 wpa_ie = wpa_ie + wpa_ie[1] + 2;
2319                 wpa_ie_len = wpa_ie[1] + 2;
2320         }
2321         hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2322                        HOSTAPD_LEVEL_DEBUG,
2323                        "sending 3/4 msg of 4-Way Handshake");
2324         wpa_send_eapol(sm->hapd, sm->sta,
2325                        sm->sta->wpa == WPA_VERSION_WPA2 ? 1 : 0,
2326                        1, 1, 1, 1, rsc, sm->ANonce,
2327                        wpa_ie, wpa_ie_len,
2328                        gsm->GTK[gsm->GN - 1], gsm->GTK_len, gsm->GN);
2329         sm->TimeoutCtr++;
2330 }
2331
2332
2333 SM_STATE(WPA_PTK, PTKINITDONE)
2334 {
2335         SM_ENTRY(WPA_PTK, PTKINITDONE, wpa_ptk);
2336         sm->EAPOLKeyReceived = FALSE;
2337         if (sm->Pair) {
2338                 char *alg;
2339                 int klen;
2340                 if (sm->sta->pairwise == WPA_CIPHER_TKIP) {
2341                         alg = "TKIP";
2342                         klen = 32;
2343                 } else {
2344                         alg = "CCMP";
2345                         klen = 16;
2346                 }
2347                 if (hostapd_set_encryption(sm->hapd, alg, sm->sta->addr, 0,
2348                                            sm->PTK.tk1, klen)) {
2349                         wpa_sta_disconnect(sm->hapd, sm->sta);
2350                         return;
2351                 }
2352                 /* FIX: MLME-SetProtection.Request(TA, Tx_Rx) */
2353                 sm->pairwise_set = TRUE;
2354
2355                 if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK)
2356                         ieee802_1x_set_sta_authorized(sm->hapd, sm->sta, 1);
2357         }
2358
2359         if (0 /* IBSS == TRUE */) {
2360                 sm->keycount++;
2361                 if (sm->keycount == 2) {
2362                         ieee802_1x_notify_port_valid(sm->sta->eapol_sm, 1);
2363                 }
2364         } else {
2365                 ieee802_1x_notify_port_valid(sm->sta->eapol_sm, 1);
2366         }
2367         if (sm->sta->eapol_sm) {
2368                 sm->sta->eapol_sm->keyAvailable = FALSE;
2369                 sm->sta->eapol_sm->keyDone = TRUE;
2370         }
2371         if (sm->sta->wpa == WPA_VERSION_WPA)
2372                 sm->PInitAKeys = TRUE;
2373         else
2374                 sm->has_GTK = TRUE;
2375         hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2376                        HOSTAPD_LEVEL_INFO, "pairwise key handshake completed "
2377                        "(%s)",
2378                        sm->sta->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2379         if (sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK)
2380                 accounting_sta_start(sm->hapd, sm->sta);
2381 }
2382
2383
2384 SM_STEP(WPA_PTK)
2385 {
2386         struct wpa_authenticator *wpa_auth = sm->hapd->wpa_auth;
2387
2388         if (sm->Init)
2389                 SM_ENTER(WPA_PTK, INITIALIZE);
2390         else if (sm->Disconnect
2391                  /* || FIX: dot11RSNAConfigSALifetime timeout */)
2392                 SM_ENTER(WPA_PTK, DISCONNECT);
2393         else if (sm->DeauthenticationRequest)
2394                 SM_ENTER(WPA_PTK, DISCONNECTED);
2395         else if (sm->AuthenticationRequest)
2396                 SM_ENTER(WPA_PTK, AUTHENTICATION);
2397         else if (sm->ReAuthenticationRequest)
2398                 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2399         else if (sm->PTKRequest)
2400                 SM_ENTER(WPA_PTK, PTKSTART);
2401         else switch (sm->wpa_ptk_state) {
2402         case WPA_PTK_INITIALIZE:
2403                 break;
2404         case WPA_PTK_DISCONNECT:
2405                 SM_ENTER(WPA_PTK, DISCONNECTED);
2406                 break;
2407         case WPA_PTK_DISCONNECTED:
2408                 SM_ENTER(WPA_PTK, INITIALIZE);
2409                 break;
2410         case WPA_PTK_AUTHENTICATION:
2411                 SM_ENTER(WPA_PTK, AUTHENTICATION2);
2412                 break;
2413         case WPA_PTK_AUTHENTICATION2:
2414                 if ((sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_IEEE8021X) &&
2415                     sm->sta->eapol_sm && sm->sta->eapol_sm->keyRun)
2416                         SM_ENTER(WPA_PTK, INITPMK);
2417                 else if ((sm->sta->wpa_key_mgmt == WPA_KEY_MGMT_PSK)
2418                          /* FIX: && 802.1X::keyRun */)
2419                         SM_ENTER(WPA_PTK, INITPSK);
2420                 break;
2421         case WPA_PTK_INITPMK:
2422                 if (sm->sta->eapol_sm && sm->sta->eapol_sm->keyAvailable)
2423                         SM_ENTER(WPA_PTK, PTKSTART);
2424                 else {
2425                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
2426                         SM_ENTER(WPA_PTK, DISCONNECT);
2427                 }
2428                 break;
2429         case WPA_PTK_INITPSK:
2430                 if (hostapd_get_psk(sm->hapd->conf, sm->sta->addr, NULL))
2431                         SM_ENTER(WPA_PTK, PTKSTART);
2432                 else {
2433                         hostapd_logger(sm->hapd, sm->sta->addr,
2434                                        HOSTAPD_MODULE_WPA,
2435                                        HOSTAPD_LEVEL_INFO,
2436                                        "no PSK configured for the STA");
2437                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
2438                         SM_ENTER(WPA_PTK, DISCONNECT);
2439                 }
2440                 break;
2441         case WPA_PTK_PTKSTART:
2442                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2443                     sm->EAPOLKeyPairwise)
2444                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2445                 else if (sm->TimeoutCtr > dot11RSNAConfigPairwiseUpdateCount) {
2446                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
2447                         SM_ENTER(WPA_PTK, DISCONNECT);
2448                 } else if (sm->TimeoutEvt)
2449                         SM_ENTER(WPA_PTK, PTKSTART);
2450                 break;
2451         case WPA_PTK_PTKCALCNEGOTIATING:
2452                 if (sm->MICVerified)
2453                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING2);
2454                 else if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2455                          sm->EAPOLKeyPairwise)
2456                         SM_ENTER(WPA_PTK, PTKCALCNEGOTIATING);
2457                 else if (sm->TimeoutEvt)
2458                         SM_ENTER(WPA_PTK, PTKSTART);
2459                 break;
2460         case WPA_PTK_PTKCALCNEGOTIATING2:
2461                 SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2462                 break;
2463         case WPA_PTK_PTKINITNEGOTIATING:
2464                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2465                     sm->EAPOLKeyPairwise && sm->MICVerified)
2466                         SM_ENTER(WPA_PTK, PTKINITDONE);
2467                 else if (sm->TimeoutCtr > dot11RSNAConfigPairwiseUpdateCount) {
2468                         wpa_auth->dot11RSNA4WayHandshakeFailures++;
2469                         SM_ENTER(WPA_PTK, DISCONNECT);
2470                 } else if (sm->TimeoutEvt)
2471                         SM_ENTER(WPA_PTK, PTKINITNEGOTIATING);
2472                 break;
2473         case WPA_PTK_PTKINITDONE:
2474                 break;
2475         }
2476 }
2477
2478
2479 SM_STATE(WPA_PTK_GROUP, IDLE)
2480 {
2481         SM_ENTRY(WPA_PTK_GROUP, IDLE, wpa_ptk_group);
2482         if (sm->Init) {
2483                 /* Init flag is not cleared here, so avoid busy
2484                  * loop by claiming nothing changed. */
2485                 sm->changed = FALSE;
2486         }
2487         sm->GTimeoutCtr = 0;
2488 }
2489
2490
2491 SM_STATE(WPA_PTK_GROUP, REKEYNEGOTIATING)
2492 {
2493         u8 rsc[WPA_KEY_RSC_LEN];
2494         struct wpa_authenticator *gsm = sm->hapd->wpa_auth;
2495
2496         SM_ENTRY(WPA_PTK_GROUP, REKEYNEGOTIATING, wpa_ptk_group);
2497         if (sm->sta->wpa == WPA_VERSION_WPA)
2498                 sm->PInitAKeys = FALSE;
2499         sm->TimeoutEvt = FALSE;
2500         /* Send EAPOL(1, 1, 1, !Pair, G, RSC, GNonce, MIC(PTK), GTK[GN]) */
2501         memset(rsc, 0, WPA_KEY_RSC_LEN);
2502         if (gsm->wpa_group_state == WPA_GROUP_SETKEYSDONE)
2503                 hostapd_get_seqnum(sm->hapd, NULL, gsm->GN, rsc);
2504         hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2505                        HOSTAPD_LEVEL_DEBUG,
2506                        "sending 1/2 msg of Group Key Handshake");
2507         wpa_send_eapol(sm->hapd, sm->sta, 1, 1, 1, !sm->Pair, 0, rsc,
2508                        gsm->GNonce, NULL, 0,
2509                        gsm->GTK[gsm->GN - 1], gsm->GTK_len, gsm->GN);
2510         sm->GTimeoutCtr++;
2511 }
2512
2513
2514 SM_STATE(WPA_PTK_GROUP, REKEYESTABLISHED)
2515 {
2516         SM_ENTRY(WPA_PTK_GROUP, REKEYESTABLISHED, wpa_ptk_group);
2517         sm->EAPOLKeyReceived = FALSE;
2518         sm->GUpdateStationKeys = FALSE;
2519         sm->hapd->wpa_auth->GKeyDoneStations--;
2520         sm->GTimeoutCtr = 0;
2521         /* FIX: MLME.SetProtection.Request(TA, Tx_Rx) */
2522         hostapd_logger(sm->hapd, sm->sta->addr, HOSTAPD_MODULE_WPA,
2523                        HOSTAPD_LEVEL_INFO, "group key handshake completed "
2524                        "(%s)",
2525                        sm->sta->wpa == WPA_VERSION_WPA ? "WPA" : "RSN");
2526         sm->has_GTK = TRUE;
2527 }
2528
2529
2530 SM_STATE(WPA_PTK_GROUP, KEYERROR)
2531 {
2532         SM_ENTRY(WPA_PTK_GROUP, KEYERROR, wpa_ptk_group);
2533         sm->hapd->wpa_auth->GKeyDoneStations--;
2534         sm->GUpdateStationKeys = FALSE;
2535         sm->Disconnect = TRUE;
2536 }
2537
2538
2539 SM_STEP(WPA_PTK_GROUP)
2540 {
2541         if (sm->Init)
2542                 SM_ENTER(WPA_PTK_GROUP, IDLE);
2543         else switch (sm->wpa_ptk_group_state) {
2544         case WPA_PTK_GROUP_IDLE:
2545                 if (sm->GUpdateStationKeys ||
2546                     (sm->sta->wpa == WPA_VERSION_WPA && sm->PInitAKeys))
2547                         SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2548                 break;
2549         case WPA_PTK_GROUP_REKEYNEGOTIATING:
2550                 if (sm->EAPOLKeyReceived && !sm->EAPOLKeyRequest &&
2551                     !sm->EAPOLKeyPairwise && sm->MICVerified)
2552                         SM_ENTER(WPA_PTK_GROUP, REKEYESTABLISHED);
2553                 else if (sm->GTimeoutCtr > dot11RSNAConfigGroupUpdateCount)
2554                         SM_ENTER(WPA_PTK_GROUP, KEYERROR);
2555                 else if (sm->TimeoutEvt)
2556                         SM_ENTER(WPA_PTK_GROUP, REKEYNEGOTIATING);
2557                 break;
2558         case WPA_PTK_GROUP_KEYERROR:
2559                 SM_ENTER(WPA_PTK_GROUP, IDLE);
2560                 break;
2561         case WPA_PTK_GROUP_REKEYESTABLISHED:
2562                 SM_ENTER(WPA_PTK_GROUP, IDLE);
2563                 break;
2564         }
2565 }
2566
2567
2568 static void wpa_group_gtk_init(struct hostapd_data *hapd)
2569 {
2570         struct wpa_authenticator *sm = hapd->wpa_auth;
2571         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "WPA: group state machine "
2572                       "entering state GTK_INIT\n");
2573         sm->changed = FALSE; /* GInit is not cleared here; avoid loop */
2574         sm->wpa_group_state = WPA_GROUP_GTK_INIT;
2575
2576         /* GTK[0..N] = 0 */
2577         memset(sm->GTK, 0, sizeof(sm->GTK));
2578         sm->GN = 1;
2579         sm->GM = 2;
2580         /* GTK[GN] = CalcGTK() */
2581         /* FIX: is this the correct way of getting GNonce? */
2582         memcpy(sm->GNonce, sm->Counter, WPA_NONCE_LEN);
2583         inc_byte_array(sm->Counter, WPA_NONCE_LEN);
2584         wpa_gmk_to_gtk(hapd, sm->GMK, hapd->own_addr, sm->GNonce,
2585                        sm->GTK[sm->GN - 1], sm->GTK_len);
2586 }
2587
2588
2589 static void wpa_group_setkeys(struct hostapd_data *hapd)
2590 {
2591         struct wpa_authenticator *sm = hapd->wpa_auth;
2592         struct sta_info *sta;
2593         int tmp;
2594
2595         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "WPA: group state machine "
2596                       "entering state SETKEYS\n");
2597         sm->changed = TRUE;
2598         sm->wpa_group_state = WPA_GROUP_SETKEYS;
2599         sm->GTKReKey = FALSE;
2600         tmp = sm->GM;
2601         sm->GM = sm->GN;
2602         sm->GN = tmp;
2603         sm->GKeyDoneStations = sm->GNoStations;
2604         /* FIX: is this the correct way of getting GNonce? */
2605         memcpy(sm->GNonce, sm->Counter, WPA_NONCE_LEN);
2606         inc_byte_array(sm->Counter, WPA_NONCE_LEN);
2607         wpa_gmk_to_gtk(hapd, sm->GMK, hapd->own_addr, sm->GNonce,
2608                        sm->GTK[sm->GN - 1], sm->GTK_len);
2609
2610         sta = hapd->sta_list;
2611         while (sta) {
2612                 if (sta->wpa_sm) {
2613                         sta->wpa_sm->GUpdateStationKeys = TRUE;
2614                         wpa_sm_step(sta->wpa_sm);
2615                 }
2616                 sta = sta->next;
2617         }
2618 }
2619
2620
2621 static void wpa_group_setkeysdone(struct hostapd_data *hapd)
2622 {
2623         struct wpa_authenticator *sm = hapd->wpa_auth;
2624
2625         HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "WPA: group state machine "
2626                       "entering state SETKEYSDONE\n");
2627         sm->changed = TRUE;
2628         sm->wpa_group_state = WPA_GROUP_SETKEYSDONE;
2629         hostapd_set_encryption(hapd, wpa_alg_txt(hapd->conf->wpa_group),
2630                                NULL, sm->GN, sm->GTK[sm->GN - 1], sm->GTK_len);
2631 }
2632
2633
2634 static void wpa_group_sm_step(struct hostapd_data *hapd)
2635 {
2636         struct wpa_authenticator *sm = hapd->wpa_auth;
2637
2638         if (sm->GInit) {
2639                 wpa_group_gtk_init(hapd);
2640         } else if (sm->wpa_group_state == WPA_GROUP_GTK_INIT &&
2641                    sm->GTKAuthenticator) {
2642                 wpa_group_setkeysdone(hapd);
2643         } else if (sm->wpa_group_state == WPA_GROUP_SETKEYSDONE &&
2644                    sm->GTKReKey) {
2645                 wpa_group_setkeys(hapd);
2646         } else if (sm->wpa_group_state == WPA_GROUP_SETKEYS) {
2647                 if (sm->GKeyDoneStations == 0)
2648                         wpa_group_setkeysdone(hapd);
2649                 else if (sm->GTKReKey)
2650                         wpa_group_setkeys(hapd);
2651         }
2652 }
2653
2654
2655 static int wpa_sm_sta_entry_alive(struct hostapd_data *hapd, u8 *addr)
2656 {
2657         struct sta_info *sta;
2658         sta = ap_get_sta(hapd, addr);
2659         if (sta == NULL || sta->wpa_sm == NULL)
2660                 return 0;
2661         return 1;
2662 }
2663
2664
2665 static void wpa_sm_step(struct wpa_state_machine *sm)
2666 {
2667         struct hostapd_data *hapd = sm->hapd;
2668         u8 addr[6];
2669         if (sm == NULL || sm->sta == NULL || sm->sta->wpa_sm == NULL)
2670                 return;
2671
2672         memcpy(addr, sm->sta->addr, 6);
2673         do {
2674                 sm->changed = FALSE;
2675                 sm->hapd->wpa_auth->changed = FALSE;
2676
2677                 SM_STEP_RUN(WPA_PTK);
2678                 if (!wpa_sm_sta_entry_alive(hapd, addr))
2679                         break;
2680                 SM_STEP_RUN(WPA_PTK_GROUP);
2681                 if (!wpa_sm_sta_entry_alive(hapd, addr))
2682                         break;
2683                 wpa_group_sm_step(sm->hapd);
2684                 if (!wpa_sm_sta_entry_alive(hapd, addr))
2685                         break;
2686         } while (sm->changed || sm->hapd->wpa_auth->changed);
2687 }
2688
2689
2690 static void wpa_sm_call_step(void *eloop_ctx, void *timeout_ctx)
2691 {
2692         struct wpa_state_machine *sm = timeout_ctx;
2693         wpa_sm_step(sm);
2694 }
2695
2696
2697 void wpa_sm_notify(struct hostapd_data *hapd, struct sta_info *sta)
2698 {
2699         if (sta->wpa_sm == NULL)
2700                 return;
2701         eloop_register_timeout(0, 0, wpa_sm_call_step, hapd, sta->wpa_sm);
2702 }
2703
2704
2705 void wpa_gtk_rekey(struct hostapd_data *hapd)
2706 {
2707         struct wpa_authenticator *sm = hapd->wpa_auth;
2708         int tmp, i;
2709
2710         if (sm == NULL)
2711                 return;
2712
2713         for (i = 0; i < 2; i++) {
2714                 tmp = sm->GM;
2715                 sm->GM = sm->GN;
2716                 sm->GN = tmp;
2717                 memcpy(sm->GNonce, sm->Counter, WPA_NONCE_LEN);
2718                 inc_byte_array(sm->Counter, WPA_NONCE_LEN);
2719                 wpa_gmk_to_gtk(hapd, sm->GMK, hapd->own_addr, sm->GNonce,
2720                                sm->GTK[sm->GN - 1], sm->GTK_len);
2721         }
2722 }
2723
2724
2725 static const char * wpa_bool_txt(int bool)
2726 {
2727         return bool ? "TRUE" : "FALSE";
2728 }
2729
2730
2731 static int wpa_cipher_bits(int cipher)
2732 {
2733         switch (cipher) {
2734         case WPA_CIPHER_CCMP:
2735                 return 128;
2736         case WPA_CIPHER_TKIP:
2737                 return 256;
2738         case WPA_CIPHER_WEP104:
2739                 return 104;
2740         case WPA_CIPHER_WEP40:
2741                 return 40;
2742         default:
2743                 return 0;
2744         }
2745 }
2746
2747
2748 #define RSN_SUITE "%02x-%02x-%02x-%d"
2749 #define RSN_SUITE_ARG(s) (s)[0], (s)[1], (s)[2], (s)[3]
2750
2751 int wpa_get_mib(struct hostapd_data *hapd, char *buf, size_t buflen)
2752 {
2753         int len = 0, i;
2754         char pmkid_txt[PMKID_LEN * 2 + 1], *pos;
2755
2756         len += snprintf(buf + len, buflen - len,
2757                         "dot11RSNAOptionImplemented=TRUE\n"
2758                         "dot11RSNAPreauthenticationImplemented=TRUE\n"
2759                         "dot11RSNAEnabled=%s\n"
2760                         "dot11RSNAPreauthenticationEnabled=%s\n",
2761                         wpa_bool_txt(hapd->conf->wpa &
2762                                      HOSTAPD_WPA_VERSION_WPA2),
2763                         wpa_bool_txt(hapd->conf->rsn_preauth));
2764
2765         if (hapd->wpa_auth == NULL)
2766                 return len;
2767
2768         pos = pmkid_txt;
2769         for (i = 0; i < PMKID_LEN; i++) {
2770                 pos += sprintf(pos, "%02x",
2771                                hapd->wpa_auth->dot11RSNAPMKIDUsed[i]);
2772         }
2773
2774         len += snprintf(buf + len, buflen - len,
2775                         "dot11RSNAConfigVersion=%u\n"
2776                         "dot11RSNAConfigPairwiseKeysSupported=9999\n"
2777                         /* FIX: dot11RSNAConfigGroupCipher */
2778                         /* FIX: dot11RSNAConfigGroupRekeyMethod */
2779                         /* FIX: dot11RSNAConfigGroupRekeyTime */
2780                         /* FIX: dot11RSNAConfigGroupRekeyPackets */
2781                         "dot11RSNAConfigGroupRekeyStrict=%u\n"
2782                         "dot11RSNAConfigGroupUpdateCount=%u\n"
2783                         "dot11RSNAConfigPairwiseUpdateCount=%u\n"
2784                         "dot11RSNAConfigGroupCipherSize=%u\n"
2785                         "dot11RSNAConfigPMKLifetime=%u\n"
2786                         "dot11RSNAConfigPMKReauthThreshold=%u\n"
2787                         "dot11RSNAConfigNumberOfPTKSAReplayCounters=0\n"
2788                         "dot11RSNAConfigSATimeout=%u\n"
2789                         "dot11RSNAAuthenticationSuiteSelected=" RSN_SUITE "\n"
2790                         "dot11RSNAPairwiseCipherSelected=" RSN_SUITE "\n"
2791                         "dot11RSNAGroupCipherSelected=" RSN_SUITE "\n"
2792                         "dot11RSNAPMKIDUsed=%s\n"
2793                         "dot11RSNAAuthenticationSuiteRequested=" RSN_SUITE "\n"
2794                         "dot11RSNAPairwiseCipherRequested=" RSN_SUITE "\n"
2795                         "dot11RSNAGroupCipherRequested=" RSN_SUITE "\n"
2796                         "dot11RSNATKIPCounterMeasuresInvoked=%u\n"
2797                         "dot11RSNA4WayHandshakeFailures=%u\n"
2798                         "dot11RSNAConfigNumberOfGTKSAReplayCounters=0\n",
2799                         RSN_VERSION,
2800                         !!hapd->conf->wpa_strict_rekey,
2801                         dot11RSNAConfigGroupUpdateCount,
2802                         dot11RSNAConfigPairwiseUpdateCount,
2803                         wpa_cipher_bits(hapd->conf->wpa_group),
2804                         dot11RSNAConfigPMKLifetime,
2805                         dot11RSNAConfigPMKReauthThreshold,
2806                         dot11RSNAConfigSATimeout,
2807                         RSN_SUITE_ARG(hapd->wpa_auth->
2808                                       dot11RSNAAuthenticationSuiteSelected),
2809                         RSN_SUITE_ARG(hapd->wpa_auth->
2810                                       dot11RSNAPairwiseCipherSelected),
2811                         RSN_SUITE_ARG(hapd->wpa_auth->
2812                                       dot11RSNAGroupCipherSelected),
2813                         pmkid_txt,
2814                         RSN_SUITE_ARG(hapd->wpa_auth->
2815                                       dot11RSNAAuthenticationSuiteRequested),
2816                         RSN_SUITE_ARG(hapd->wpa_auth->
2817                                       dot11RSNAPairwiseCipherRequested),
2818                         RSN_SUITE_ARG(hapd->wpa_auth->
2819                                       dot11RSNAGroupCipherRequested),
2820                         hapd->wpa_auth->dot11RSNATKIPCounterMeasuresInvoked,
2821                         hapd->wpa_auth->dot11RSNA4WayHandshakeFailures);
2822
2823         /* TODO: dot11RSNAConfigPairwiseCiphersTable */
2824         /* TODO: dot11RSNAConfigAuthenticationSuitesTable */
2825
2826         /* Private MIB */
2827         len += snprintf(buf + len, buflen - len,
2828                         "hostapdWPAGroupState=%d\n",
2829                         hapd->wpa_auth->wpa_group_state);
2830
2831         return len;
2832 }
2833
2834
2835 int wpa_get_mib_sta(struct hostapd_data *hapd, struct sta_info *sta,
2836                     char *buf, size_t buflen)
2837 {
2838         int len = 0;
2839         u8 not_used[4] = { 0, 0, 0, 0 };
2840         const u8 *pairwise = not_used;
2841
2842         /* TODO: FF-FF-FF-FF-FF-FF entry for broadcast/multicast stats */
2843
2844         /* dot11RSNAStatsEntry */
2845
2846         if (sta->wpa == WPA_VERSION_WPA) {
2847                 if (sta->pairwise == WPA_CIPHER_CCMP)
2848                         pairwise = WPA_CIPHER_SUITE_CCMP;
2849                 else if (sta->pairwise == WPA_CIPHER_TKIP)
2850                         pairwise = WPA_CIPHER_SUITE_TKIP;
2851                 else if (sta->pairwise == WPA_CIPHER_WEP104)
2852                         pairwise = WPA_CIPHER_SUITE_WEP104;
2853                 else if (sta->pairwise == WPA_CIPHER_WEP40)
2854                         pairwise = WPA_CIPHER_SUITE_WEP40;
2855                 else if (sta->pairwise == WPA_CIPHER_NONE)
2856                         pairwise = WPA_CIPHER_SUITE_NONE;
2857         } else if (sta->wpa == WPA_VERSION_WPA2) {
2858                 if (sta->pairwise == WPA_CIPHER_CCMP)
2859                         pairwise = RSN_CIPHER_SUITE_CCMP;
2860                 else if (sta->pairwise == WPA_CIPHER_TKIP)
2861                         pairwise = RSN_CIPHER_SUITE_TKIP;
2862                 else if (sta->pairwise == WPA_CIPHER_WEP104)
2863                         pairwise = RSN_CIPHER_SUITE_WEP104;
2864                 else if (sta->pairwise == WPA_CIPHER_WEP40)
2865                         pairwise = RSN_CIPHER_SUITE_WEP40;
2866                 else if (sta->pairwise == WPA_CIPHER_NONE)
2867                         pairwise = RSN_CIPHER_SUITE_NONE;
2868         } else
2869                 return 0;
2870
2871         len += snprintf(buf + len, buflen - len,
2872                         /* TODO: dot11RSNAStatsIndex */
2873                         "dot11RSNAStatsSTAAddress=" MACSTR "\n"
2874                         "dot11RSNAStatsVersion=1\n"
2875                         "dot11RSNAStatsSelectedPairwiseCipher=" RSN_SUITE "\n"
2876                         /* TODO: dot11RSNAStatsTKIPICVErrors */
2877                         "dot11RSNAStatsTKIPLocalMICFailures=%u\n"
2878                         "dot11RSNAStatsTKIPRemoveMICFailures=%u\n"
2879                         /* TODO: dot11RSNAStatsCCMPReplays */
2880                         /* TODO: dot11RSNAStatsCCMPDecryptErrors */
2881                         /* TODO: dot11RSNAStatsTKIPReplays */,
2882                         MAC2STR(sta->addr),
2883                         RSN_SUITE_ARG(pairwise),
2884                         sta->dot11RSNAStatsTKIPLocalMICFailures,
2885                         sta->dot11RSNAStatsTKIPRemoteMICFailures);
2886
2887         if (sta->wpa_sm == NULL)
2888                 return len;
2889
2890         /* Private MIB */
2891         len += snprintf(buf + len, buflen - len,
2892                         "hostapdWPAPTKState=%d\n"
2893                         "hostapdWPAPTKGroupState=%d\n",
2894                         sta->wpa_sm->wpa_ptk_state,
2895                         sta->wpa_sm->wpa_ptk_group_state);
2896
2897         return len;
2898 }