Add SME support (separate authentication and association)
[wpasupplicant] / wpa_supplicant / scan.c
1 /*
2  * WPA Supplicant - Scanning
3  * Copyright (c) 2003-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "config.h"
20 #include "wpa_supplicant_i.h"
21 #include "mlme.h"
22 #include "wps_supplicant.h"
23
24
25 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
26 {
27         struct wpa_ssid *ssid;
28         union wpa_event_data data;
29
30         ssid = wpa_supplicant_get_ssid(wpa_s);
31         if (ssid == NULL)
32                 return;
33
34         if (wpa_s->current_ssid == NULL)
35                 wpa_s->current_ssid = ssid;
36         wpa_supplicant_initiate_eapol(wpa_s);
37         wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
38                    "generating associated event");
39         os_memset(&data, 0, sizeof(data));
40         wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
41 }
42
43
44 #ifdef CONFIG_WPS
45 static int wpas_wps_in_use(struct wpa_config *conf,
46                            enum wps_request_type *req_type)
47 {
48         struct wpa_ssid *ssid;
49         int wps = 0;
50
51         for (ssid = conf->ssid; ssid; ssid = ssid->next) {
52                 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
53                         continue;
54
55                 wps = 1;
56                 *req_type = wpas_wps_get_req_type(ssid);
57                 if (!ssid->eap.phase1)
58                         continue;
59
60                 if (os_strstr(ssid->eap.phase1, "pbc=1"))
61                         return 2;
62         }
63
64         return wps;
65 }
66 #endif /* CONFIG_WPS */
67
68
69 static int wpa_supplicant_enabled_networks(struct wpa_config *conf)
70 {
71         struct wpa_ssid *ssid = conf->ssid;
72         while (ssid) {
73                 if (!ssid->disabled)
74                         return 1;
75                 ssid = ssid->next;
76         }
77         return 0;
78 }
79
80
81 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
82                                      struct wpa_ssid *ssid)
83 {
84         while (ssid) {
85                 if (!ssid->disabled)
86                         break;
87                 ssid = ssid->next;
88         }
89
90         /* ap_scan=2 mode - try to associate with each SSID. */
91         if (ssid == NULL) {
92                 wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
93                            "end of scan list - go back to beginning");
94                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
95                 wpa_supplicant_req_scan(wpa_s, 0, 0);
96                 return;
97         }
98         if (ssid->next) {
99                 /* Continue from the next SSID on the next attempt. */
100                 wpa_s->prev_scan_ssid = ssid;
101         } else {
102                 /* Start from the beginning of the SSID list. */
103                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
104         }
105         wpa_supplicant_associate(wpa_s, NULL, ssid);
106 }
107
108
109 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
110 {
111         struct wpa_supplicant *wpa_s = eloop_ctx;
112         struct wpa_ssid *ssid;
113         int scan_req = 0, ret;
114         struct wpabuf *wps_ie = NULL;
115         int wps = 0;
116 #ifdef CONFIG_WPS
117         enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
118 #endif /* CONFIG_WPS */
119         struct wpa_driver_scan_params params;
120         size_t max_ssids;
121
122         if (wpa_s->disconnected && !wpa_s->scan_req)
123                 return;
124
125         if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
126             !wpa_s->scan_req) {
127                 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
128                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
129                 return;
130         }
131
132         if (wpa_s->conf->ap_scan != 0 &&
133             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
134                 wpa_printf(MSG_DEBUG, "Using wired authentication - "
135                            "overriding ap_scan configuration");
136                 wpa_s->conf->ap_scan = 0;
137         }
138
139         if (wpa_s->conf->ap_scan == 0) {
140                 wpa_supplicant_gen_assoc_event(wpa_s);
141                 return;
142         }
143
144         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
145             wpa_s->conf->ap_scan == 2)
146                 max_ssids = 1;
147         else {
148                 max_ssids = wpa_s->max_scan_ssids;
149                 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
150                         max_ssids = WPAS_MAX_SCAN_SSIDS;
151         }
152
153 #ifdef CONFIG_WPS
154         wps = wpas_wps_in_use(wpa_s->conf, &req_type);
155 #endif /* CONFIG_WPS */
156
157         if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
158             !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
159             wps != 2) {
160                 wpa_s->scan_res_tried++;
161                 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
162                            "first without requesting a new scan to speed up "
163                            "initial association");
164                 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
165                 return;
166         }
167
168         scan_req = wpa_s->scan_req;
169         wpa_s->scan_req = 0;
170
171         os_memset(&params, 0, sizeof(params));
172
173         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
174             wpa_s->wpa_state == WPA_INACTIVE)
175                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
176
177         /* Find the starting point from which to continue scanning */
178         ssid = wpa_s->conf->ssid;
179         if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
180                 while (ssid) {
181                         if (ssid == wpa_s->prev_scan_ssid) {
182                                 ssid = ssid->next;
183                                 break;
184                         }
185                         ssid = ssid->next;
186                 }
187         }
188
189         if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
190                 wpa_supplicant_assoc_try(wpa_s, ssid);
191                 return;
192         } else if (wpa_s->conf->ap_scan == 2) {
193                 /*
194                  * User-initiated scan request in ap_scan == 2; scan with
195                  * wildcard SSID.
196                  */
197                 ssid = NULL;
198         } else {
199                 struct wpa_ssid *start = ssid;
200                 if (ssid == NULL && max_ssids > 1)
201                         ssid = wpa_s->conf->ssid;
202                 while (ssid) {
203                         if (!ssid->disabled && ssid->scan_ssid) {
204                                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
205                                                   ssid->ssid, ssid->ssid_len);
206                                 params.ssids[params.num_ssids].ssid =
207                                         ssid->ssid;
208                                 params.ssids[params.num_ssids].ssid_len =
209                                         ssid->ssid_len;
210                                 params.num_ssids++;
211                                 if (params.num_ssids + 1 >= max_ssids)
212                                         break;
213                         }
214                         ssid = ssid->next;
215                         if (ssid == start)
216                                 break;
217                         if (ssid == NULL && max_ssids > 1 &&
218                             start != wpa_s->conf->ssid)
219                                 ssid = wpa_s->conf->ssid;
220                 }
221         }
222
223         if (ssid) {
224                 wpa_s->prev_scan_ssid = ssid;
225                 if (max_ssids > 1) {
226                         wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
227                                    "scan request");
228                         params.num_ssids++;
229                 }
230                 wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
231         } else {
232                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
233                 params.num_ssids++;
234                 wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
235         }
236
237 #ifdef CONFIG_WPS
238         if (wps) {
239                 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
240                                                 wpa_s->wps->uuid, req_type);
241                 if (wps_ie) {
242                         params.extra_ies = wpabuf_head(wps_ie);
243                         params.extra_ies_len = wpabuf_len(wps_ie);
244                 }
245         }
246 #endif /* CONFIG_WPS */
247
248         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
249                 ieee80211_sta_set_probe_req_ie(wpa_s, params.extra_ies,
250                                                params.extra_ies_len);
251                 ret = ieee80211_sta_req_scan(wpa_s, params.ssids[0].ssid,
252                                              params.ssids[0].ssid_len);
253         } else {
254                 wpa_drv_set_probe_req_ie(wpa_s, params.extra_ies,
255                                          params.extra_ies_len);
256                 ret = wpa_drv_scan(wpa_s, &params);
257         }
258
259         wpabuf_free(wps_ie);
260
261         if (ret) {
262                 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
263                 wpa_supplicant_req_scan(wpa_s, 10, 0);
264         } else
265                 wpa_s->scan_runs++;
266 }
267
268
269 /**
270  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
271  * @wpa_s: Pointer to wpa_supplicant data
272  * @sec: Number of seconds after which to scan
273  * @usec: Number of microseconds after which to scan
274  *
275  * This function is used to schedule a scan for neighboring access points after
276  * the specified time.
277  */
278 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
279 {
280         /* If there's at least one network that should be specifically scanned
281          * then don't cancel the scan and reschedule.  Some drivers do
282          * background scanning which generates frequent scan results, and that
283          * causes the specific SSID scan to get continually pushed back and
284          * never happen, which causes hidden APs to never get probe-scanned.
285          */
286         if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
287             wpa_s->conf->ap_scan == 1) {
288                 struct wpa_ssid *ssid = wpa_s->conf->ssid;
289
290                 while (ssid) {
291                         if (!ssid->disabled && ssid->scan_ssid)
292                                 break;
293                         ssid = ssid->next;
294                 }
295                 if (ssid) {
296                         wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
297                                 "ensure that specific SSID scans occur");
298                         return;
299                 }
300         }
301
302         wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
303                 sec, usec);
304         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
305         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
306 }
307
308
309 /**
310  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
311  * @wpa_s: Pointer to wpa_supplicant data
312  *
313  * This function is used to cancel a scan request scheduled with
314  * wpa_supplicant_req_scan().
315  */
316 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
317 {
318         wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
319         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
320 }