automake build system
[mech_eap.orig] / wpa_supplicant / sme.c
1 /*
2  * wpa_supplicant - SME
3  * Copyright (c) 2009-2010, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "common/ieee802_11_defs.h"
19 #include "common/ieee802_11_common.h"
20 #include "eapol_supp/eapol_supp_sm.h"
21 #include "common/wpa_common.h"
22 #include "rsn_supp/wpa.h"
23 #include "rsn_supp/pmksa_cache.h"
24 #include "config.h"
25 #include "wpa_supplicant_i.h"
26 #include "driver_i.h"
27 #include "wpas_glue.h"
28 #include "wps_supplicant.h"
29 #include "p2p_supplicant.h"
30 #include "notify.h"
31 #include "blacklist.h"
32 #include "bss.h"
33 #include "scan.h"
34 #include "sme.h"
35
36 void sme_authenticate(struct wpa_supplicant *wpa_s,
37                       struct wpa_bss *bss, struct wpa_ssid *ssid)
38 {
39         struct wpa_driver_auth_params params;
40         struct wpa_ssid *old_ssid;
41 #ifdef CONFIG_IEEE80211R
42         const u8 *ie;
43 #endif /* CONFIG_IEEE80211R */
44 #ifdef CONFIG_IEEE80211R
45         const u8 *md = NULL;
46 #endif /* CONFIG_IEEE80211R */
47         int i, bssid_changed;
48
49         if (bss == NULL) {
50                 wpa_printf(MSG_ERROR, "SME: No scan result available for the "
51                            "network");
52                 return;
53         }
54
55         wpa_s->current_bss = bss;
56
57         os_memset(&params, 0, sizeof(params));
58         wpa_s->reassociate = 0;
59
60         params.freq = bss->freq;
61         params.bssid = bss->bssid;
62         params.ssid = bss->ssid;
63         params.ssid_len = bss->ssid_len;
64
65         if (wpa_s->sme.ssid_len != params.ssid_len ||
66             os_memcmp(wpa_s->sme.ssid, params.ssid, params.ssid_len) != 0)
67                 wpa_s->sme.prev_bssid_set = 0;
68
69         wpa_s->sme.freq = params.freq;
70         os_memcpy(wpa_s->sme.ssid, params.ssid, params.ssid_len);
71         wpa_s->sme.ssid_len = params.ssid_len;
72
73         params.auth_alg = WPA_AUTH_ALG_OPEN;
74 #ifdef IEEE8021X_EAPOL
75         if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
76                 if (ssid->leap) {
77                         if (ssid->non_leap == 0)
78                                 params.auth_alg = WPA_AUTH_ALG_LEAP;
79                         else
80                                 params.auth_alg |= WPA_AUTH_ALG_LEAP;
81                 }
82         }
83 #endif /* IEEE8021X_EAPOL */
84         wpa_printf(MSG_DEBUG, "Automatic auth_alg selection: 0x%x",
85                    params.auth_alg);
86         if (ssid->auth_alg) {
87                 params.auth_alg = ssid->auth_alg;
88                 wpa_printf(MSG_DEBUG, "Overriding auth_alg selection: 0x%x",
89                            params.auth_alg);
90         }
91
92         for (i = 0; i < NUM_WEP_KEYS; i++) {
93                 if (ssid->wep_key_len[i])
94                         params.wep_key[i] = ssid->wep_key[i];
95                 params.wep_key_len[i] = ssid->wep_key_len[i];
96         }
97         params.wep_tx_keyidx = ssid->wep_tx_keyidx;
98
99         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
100         os_memset(wpa_s->bssid, 0, ETH_ALEN);
101         os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
102         if (bssid_changed)
103                 wpas_notify_bssid_changed(wpa_s);
104
105         if ((wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
106              wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
107             (ssid->key_mgmt & (WPA_KEY_MGMT_IEEE8021X | WPA_KEY_MGMT_PSK |
108                                WPA_KEY_MGMT_FT_IEEE8021X |
109                                WPA_KEY_MGMT_FT_PSK |
110                                WPA_KEY_MGMT_IEEE8021X_SHA256 |
111                                WPA_KEY_MGMT_PSK_SHA256))) {
112                 int try_opportunistic;
113                 try_opportunistic = ssid->proactive_key_caching &&
114                         (ssid->proto & WPA_PROTO_RSN);
115                 if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
116                                             wpa_s->current_ssid,
117                                             try_opportunistic) == 0)
118                         eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
119                 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
120                 if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
121                                               wpa_s->sme.assoc_req_ie,
122                                               &wpa_s->sme.assoc_req_ie_len)) {
123                         wpa_printf(MSG_WARNING, "SME: Failed to set WPA key "
124                                    "management and encryption suites");
125                         return;
126                 }
127         } else if (ssid->key_mgmt &
128                    (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_IEEE8021X |
129                     WPA_KEY_MGMT_WPA_NONE | WPA_KEY_MGMT_FT_PSK |
130                     WPA_KEY_MGMT_FT_IEEE8021X | WPA_KEY_MGMT_PSK_SHA256 |
131                     WPA_KEY_MGMT_IEEE8021X_SHA256)) {
132                 wpa_s->sme.assoc_req_ie_len = sizeof(wpa_s->sme.assoc_req_ie);
133                 if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
134                                               wpa_s->sme.assoc_req_ie,
135                                               &wpa_s->sme.assoc_req_ie_len)) {
136                         wpa_printf(MSG_WARNING, "SME: Failed to set WPA key "
137                                    "management and encryption suites (no scan "
138                                    "results)");
139                         return;
140                 }
141 #ifdef CONFIG_WPS
142         } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
143                 struct wpabuf *wps_ie;
144                 wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
145                 if (wps_ie && wpabuf_len(wps_ie) <=
146                     sizeof(wpa_s->sme.assoc_req_ie)) {
147                         wpa_s->sme.assoc_req_ie_len = wpabuf_len(wps_ie);
148                         os_memcpy(wpa_s->sme.assoc_req_ie, wpabuf_head(wps_ie),
149                                   wpa_s->sme.assoc_req_ie_len);
150                 } else
151                         wpa_s->sme.assoc_req_ie_len = 0;
152                 wpabuf_free(wps_ie);
153                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
154 #endif /* CONFIG_WPS */
155         } else {
156                 wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
157                 wpa_s->sme.assoc_req_ie_len = 0;
158         }
159
160 #ifdef CONFIG_IEEE80211R
161         ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
162         if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
163                 md = ie + 2;
164         wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
165         if (md) {
166                 /* Prepare for the next transition */
167                 wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
168         }
169
170         if (md && ssid->key_mgmt & (WPA_KEY_MGMT_FT_PSK |
171                                     WPA_KEY_MGMT_FT_IEEE8021X)) {
172                 if (wpa_s->sme.assoc_req_ie_len + 5 <
173                     sizeof(wpa_s->sme.assoc_req_ie)) {
174                         struct rsn_mdie *mdie;
175                         u8 *pos = wpa_s->sme.assoc_req_ie +
176                                 wpa_s->sme.assoc_req_ie_len;
177                         *pos++ = WLAN_EID_MOBILITY_DOMAIN;
178                         *pos++ = sizeof(*mdie);
179                         mdie = (struct rsn_mdie *) pos;
180                         os_memcpy(mdie->mobility_domain, md,
181                                   MOBILITY_DOMAIN_ID_LEN);
182                         mdie->ft_capab = md[MOBILITY_DOMAIN_ID_LEN];
183                         wpa_s->sme.assoc_req_ie_len += 5;
184                 }
185
186                 if (wpa_s->sme.ft_used &&
187                     os_memcmp(md, wpa_s->sme.mobility_domain, 2) == 0 &&
188                     wpa_sm_has_ptk(wpa_s->wpa)) {
189                         wpa_printf(MSG_DEBUG, "SME: Trying to use FT "
190                                    "over-the-air");
191                         params.auth_alg = WPA_AUTH_ALG_FT;
192                         params.ie = wpa_s->sme.ft_ies;
193                         params.ie_len = wpa_s->sme.ft_ies_len;
194                 }
195         }
196 #endif /* CONFIG_IEEE80211R */
197
198 #ifdef CONFIG_IEEE80211W
199         wpa_s->sme.mfp = ssid->ieee80211w;
200         if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
201                 const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
202                 struct wpa_ie_data _ie;
203                 if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &_ie) == 0 &&
204                     _ie.capabilities &
205                     (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
206                         wpa_printf(MSG_DEBUG, "WPA: Selected AP supports MFP: "
207                                    "require MFP");
208                         wpa_s->sme.mfp = MGMT_FRAME_PROTECTION_REQUIRED;
209                 }
210         }
211 #endif /* CONFIG_IEEE80211W */
212
213 #ifdef CONFIG_P2P
214         if (wpa_s->global->p2p) {
215                 u8 *pos;
216                 size_t len;
217                 int res;
218                 int p2p_group;
219                 p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
220                 pos = wpa_s->sme.assoc_req_ie + wpa_s->sme.assoc_req_ie_len;
221                 len = sizeof(wpa_s->sme.assoc_req_ie) -
222                         wpa_s->sme.assoc_req_ie_len;
223                 res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
224                 if (res >= 0)
225                         wpa_s->sme.assoc_req_ie_len += res;
226         }
227 #endif /* CONFIG_P2P */
228
229         wpa_supplicant_cancel_scan(wpa_s);
230
231         wpa_msg(wpa_s, MSG_INFO, "Trying to authenticate with " MACSTR
232                 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
233                 wpa_ssid_txt(params.ssid, params.ssid_len), params.freq);
234
235         wpa_clear_keys(wpa_s, bss->bssid);
236         wpa_supplicant_set_state(wpa_s, WPA_AUTHENTICATING);
237         old_ssid = wpa_s->current_ssid;
238         wpa_s->current_ssid = ssid;
239         wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
240         wpa_supplicant_initiate_eapol(wpa_s);
241         if (old_ssid != wpa_s->current_ssid)
242                 wpas_notify_network_changed(wpa_s);
243
244         wpa_s->sme.auth_alg = params.auth_alg;
245         if (wpa_drv_authenticate(wpa_s, &params) < 0) {
246                 wpa_msg(wpa_s, MSG_INFO, "Authentication request to the "
247                         "driver failed");
248                 wpa_supplicant_req_scan(wpa_s, 1, 0);
249                 return;
250         }
251
252         /* TODO: add timeout on authentication */
253
254         /*
255          * Association will be started based on the authentication event from
256          * the driver.
257          */
258 }
259
260
261 void sme_event_auth(struct wpa_supplicant *wpa_s, union wpa_event_data *data)
262 {
263         struct wpa_ssid *ssid = wpa_s->current_ssid;
264
265         if (ssid == NULL) {
266                 wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
267                            "network is not selected");
268                 return;
269         }
270
271         if (wpa_s->wpa_state != WPA_AUTHENTICATING) {
272                 wpa_printf(MSG_DEBUG, "SME: Ignore authentication event when "
273                            "not in authenticating state");
274                 return;
275         }
276
277         if (os_memcmp(wpa_s->pending_bssid, data->auth.peer, ETH_ALEN) != 0) {
278                 wpa_printf(MSG_DEBUG, "SME: Ignore authentication with "
279                            "unexpected peer " MACSTR,
280                            MAC2STR(data->auth.peer));
281                 return;
282         }
283
284         wpa_printf(MSG_DEBUG, "SME: Authentication response: peer=" MACSTR
285                    " auth_type=%d status_code=%d",
286                    MAC2STR(data->auth.peer), data->auth.auth_type,
287                    data->auth.status_code);
288         wpa_hexdump(MSG_MSGDUMP, "SME: Authentication response IEs",
289                     data->auth.ies, data->auth.ies_len);
290
291         if (data->auth.status_code != WLAN_STATUS_SUCCESS) {
292                 wpa_printf(MSG_DEBUG, "SME: Authentication failed (status "
293                            "code %d)", data->auth.status_code);
294
295                 if (data->auth.status_code !=
296                     WLAN_STATUS_NOT_SUPPORTED_AUTH_ALG ||
297                     wpa_s->sme.auth_alg == data->auth.auth_type ||
298                     wpa_s->current_ssid->auth_alg == WPA_AUTH_ALG_LEAP)
299                         return;
300
301                 switch (data->auth.auth_type) {
302                 case WLAN_AUTH_OPEN:
303                         wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_SHARED;
304
305                         wpa_printf(MSG_DEBUG, "SME: Trying SHARED auth");
306                         wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
307                                                  wpa_s->current_ssid);
308                         return;
309
310                 case WLAN_AUTH_SHARED_KEY:
311                         wpa_s->current_ssid->auth_alg = WPA_AUTH_ALG_LEAP;
312
313                         wpa_printf(MSG_DEBUG, "SME: Trying LEAP auth");
314                         wpa_supplicant_associate(wpa_s, wpa_s->current_bss,
315                                                  wpa_s->current_ssid);
316                         return;
317
318                 default:
319                         return;
320                 }
321         }
322
323 #ifdef CONFIG_IEEE80211R
324         if (data->auth.auth_type == WLAN_AUTH_FT) {
325                 union wpa_event_data edata;
326                 os_memset(&edata, 0, sizeof(edata));
327                 edata.ft_ies.ies = data->auth.ies;
328                 edata.ft_ies.ies_len = data->auth.ies_len;
329                 os_memcpy(edata.ft_ies.target_ap, data->auth.peer, ETH_ALEN);
330                 wpa_supplicant_event(wpa_s, EVENT_FT_RESPONSE, &edata);
331         }
332 #endif /* CONFIG_IEEE80211R */
333
334         sme_associate(wpa_s, ssid->mode, data->auth.peer,
335                       data->auth.auth_type);
336 }
337
338
339 void sme_associate(struct wpa_supplicant *wpa_s, enum wpas_mode mode,
340                    const u8 *bssid, u16 auth_type)
341 {
342         struct wpa_driver_associate_params params;
343         struct ieee802_11_elems elems;
344
345         os_memset(&params, 0, sizeof(params));
346         params.bssid = bssid;
347         params.ssid = wpa_s->sme.ssid;
348         params.ssid_len = wpa_s->sme.ssid_len;
349         params.freq = wpa_s->sme.freq;
350         params.wpa_ie = wpa_s->sme.assoc_req_ie_len ?
351                 wpa_s->sme.assoc_req_ie : NULL;
352         params.wpa_ie_len = wpa_s->sme.assoc_req_ie_len;
353 #ifdef CONFIG_IEEE80211R
354         if (auth_type == WLAN_AUTH_FT && wpa_s->sme.ft_ies) {
355                 params.wpa_ie = wpa_s->sme.ft_ies;
356                 params.wpa_ie_len = wpa_s->sme.ft_ies_len;
357         }
358 #endif /* CONFIG_IEEE80211R */
359         params.mode = mode;
360         params.mgmt_frame_protection = wpa_s->sme.mfp;
361         if (wpa_s->sme.prev_bssid_set)
362                 params.prev_bssid = wpa_s->sme.prev_bssid;
363
364         wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
365                 " (SSID='%s' freq=%d MHz)", MAC2STR(params.bssid),
366                 params.ssid ? wpa_ssid_txt(params.ssid, params.ssid_len) : "",
367                 params.freq);
368
369         wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
370
371         if (params.wpa_ie == NULL ||
372             ieee802_11_parse_elems(params.wpa_ie, params.wpa_ie_len, &elems, 0)
373             < 0) {
374                 wpa_printf(MSG_DEBUG, "SME: Could not parse own IEs?!");
375                 os_memset(&elems, 0, sizeof(elems));
376         }
377         if (elems.rsn_ie)
378                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.rsn_ie - 2,
379                                         elems.rsn_ie_len + 2);
380         else if (elems.wpa_ie)
381                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, elems.wpa_ie - 2,
382                                         elems.wpa_ie_len + 2);
383         else
384                 wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
385         if (elems.p2p &&
386             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
387                 params.p2p = 1;
388
389         if (wpa_s->parent->set_sta_uapsd)
390                 params.uapsd = wpa_s->parent->sta_uapsd;
391         else
392                 params.uapsd = -1;
393
394         if (wpa_drv_associate(wpa_s, &params) < 0) {
395                 wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
396                         "failed");
397                 wpa_supplicant_req_scan(wpa_s, 5, 0);
398                 return;
399         }
400
401         /* TODO: add timeout on association */
402 }
403
404
405 int sme_update_ft_ies(struct wpa_supplicant *wpa_s, const u8 *md,
406                       const u8 *ies, size_t ies_len)
407 {
408         if (md == NULL || ies == NULL) {
409                 wpa_printf(MSG_DEBUG, "SME: Remove mobility domain");
410                 os_free(wpa_s->sme.ft_ies);
411                 wpa_s->sme.ft_ies = NULL;
412                 wpa_s->sme.ft_ies_len = 0;
413                 wpa_s->sme.ft_used = 0;
414                 return 0;
415         }
416
417         os_memcpy(wpa_s->sme.mobility_domain, md, MOBILITY_DOMAIN_ID_LEN);
418         wpa_hexdump(MSG_DEBUG, "SME: FT IEs", ies, ies_len);
419         os_free(wpa_s->sme.ft_ies);
420         wpa_s->sme.ft_ies = os_malloc(ies_len);
421         if (wpa_s->sme.ft_ies == NULL)
422                 return -1;
423         os_memcpy(wpa_s->sme.ft_ies, ies, ies_len);
424         wpa_s->sme.ft_ies_len = ies_len;
425         return 0;
426 }
427
428
429 void sme_event_assoc_reject(struct wpa_supplicant *wpa_s,
430                             union wpa_event_data *data)
431 {
432         int bssid_changed;
433         int timeout = 5000;
434
435         wpa_printf(MSG_DEBUG, "SME: Association with " MACSTR " failed: "
436                    "status code %d", MAC2STR(wpa_s->pending_bssid),
437                    data->assoc_reject.status_code);
438
439         bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
440
441         /*
442          * For now, unconditionally terminate the previous authentication. In
443          * theory, this should not be needed, but mac80211 gets quite confused
444          * if the authentication is left pending.. Some roaming cases might
445          * benefit from using the previous authentication, so this could be
446          * optimized in the future.
447          */
448         if (wpa_drv_deauthenticate(wpa_s, wpa_s->pending_bssid,
449                                    WLAN_REASON_DEAUTH_LEAVING) < 0) {
450                 wpa_msg(wpa_s, MSG_INFO,
451                         "Deauth request to the driver failed");
452         }
453         wpa_s->sme.prev_bssid_set = 0;
454
455         if (wpa_blacklist_add(wpa_s, wpa_s->pending_bssid) == 0) {
456                 struct wpa_blacklist *b;
457                 b = wpa_blacklist_get(wpa_s, wpa_s->pending_bssid);
458                 if (b && b->count < 3) {
459                         /*
460                          * Speed up next attempt if there could be other APs
461                          * that could accept association.
462                          */
463                         timeout = 100;
464                 }
465         }
466         wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
467         os_memset(wpa_s->bssid, 0, ETH_ALEN);
468         os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
469         if (bssid_changed)
470                 wpas_notify_bssid_changed(wpa_s);
471
472         /*
473          * TODO: if more than one possible AP is available in scan results,
474          * could try the other ones before requesting a new scan.
475          */
476         wpa_supplicant_req_scan(wpa_s, timeout / 1000,
477                                 1000 * (timeout % 1000));
478 }
479
480
481 void sme_event_auth_timed_out(struct wpa_supplicant *wpa_s,
482                               union wpa_event_data *data)
483 {
484         wpa_printf(MSG_DEBUG, "SME: Authentication timed out");
485         wpa_supplicant_req_scan(wpa_s, 5, 0);
486 }
487
488
489 void sme_event_assoc_timed_out(struct wpa_supplicant *wpa_s,
490                                union wpa_event_data *data)
491 {
492         wpa_printf(MSG_DEBUG, "SME: Association timed out");
493         wpa_supplicant_mark_disassoc(wpa_s);
494         wpa_supplicant_req_scan(wpa_s, 5, 0);
495 }
496
497
498 void sme_event_disassoc(struct wpa_supplicant *wpa_s,
499                         union wpa_event_data *data)
500 {
501         wpa_printf(MSG_DEBUG, "SME: Disassociation event received");
502         if (wpa_s->sme.prev_bssid_set &&
503             !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)) {
504                 /*
505                  * cfg80211/mac80211 can get into somewhat confused state if
506                  * the AP only disassociates us and leaves us in authenticated
507                  * state. For now, force the state to be cleared to avoid
508                  * confusing errors if we try to associate with the AP again.
509                  */
510                 wpa_printf(MSG_DEBUG, "SME: Deauthenticate to clear driver "
511                            "state");
512                 wpa_drv_deauthenticate(wpa_s, wpa_s->sme.prev_bssid,
513                                        WLAN_REASON_DEAUTH_LEAVING);
514         }
515 }