Add support for multi-SSID scan requests
[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 = BROADCAST_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 = BROADCAST_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->driver && IS_WIRED(wpa_s->driver)) {
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->use_client_mlme || wpa_s->conf->ap_scan == 2)
145                 max_ssids = 1;
146         else {
147                 max_ssids = wpa_s->max_scan_ssids;
148                 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
149                         max_ssids = WPAS_MAX_SCAN_SSIDS;
150         }
151
152 #ifdef CONFIG_WPS
153         wps = wpas_wps_in_use(wpa_s->conf, &req_type);
154 #endif /* CONFIG_WPS */
155
156         if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
157             !wpa_s->use_client_mlme && wps != 2) {
158                 wpa_s->scan_res_tried++;
159                 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
160                            "first without requesting a new scan to speed up "
161                            "initial association");
162                 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
163                 return;
164         }
165
166         scan_req = wpa_s->scan_req;
167         wpa_s->scan_req = 0;
168
169         os_memset(&params, 0, sizeof(params));
170
171         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
172             wpa_s->wpa_state == WPA_INACTIVE)
173                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
174
175         /* Find the starting point from which to continue scanning */
176         ssid = wpa_s->conf->ssid;
177         if (wpa_s->prev_scan_ssid != BROADCAST_SSID_SCAN) {
178                 while (ssid) {
179                         if (ssid == wpa_s->prev_scan_ssid) {
180                                 ssid = ssid->next;
181                                 break;
182                         }
183                         ssid = ssid->next;
184                 }
185         }
186
187         if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
188                 wpa_supplicant_assoc_try(wpa_s, ssid);
189                 return;
190         } else if (wpa_s->conf->ap_scan == 2) {
191                 /*
192                  * User-initiated scan request in ap_scan == 2; use broadcast
193                  * scan.
194                  */
195                 ssid = NULL;
196         } else {
197                 struct wpa_ssid *start = ssid;
198                 if (ssid == NULL && max_ssids > 1)
199                         ssid = wpa_s->conf->ssid;
200                 while (ssid) {
201                         if (!ssid->disabled && ssid->scan_ssid) {
202                                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
203                                                   ssid->ssid, ssid->ssid_len);
204                                 params.ssids[params.num_ssids].ssid =
205                                         ssid->ssid;
206                                 params.ssids[params.num_ssids].ssid_len =
207                                         ssid->ssid_len;
208                                 params.num_ssids++;
209                                 if (params.num_ssids + 1 >= max_ssids)
210                                         break;
211                         }
212                         ssid = ssid->next;
213                         if (ssid == start)
214                                 break;
215                         if (ssid == NULL && max_ssids > 1 &&
216                             start != wpa_s->conf->ssid)
217                                 ssid = wpa_s->conf->ssid;
218                 }
219         }
220
221         if (ssid) {
222                 wpa_s->prev_scan_ssid = ssid;
223                 if (max_ssids > 1) {
224                         wpa_printf(MSG_DEBUG, "Include broadcast SSID in the "
225                                    "scan request");
226                         params.num_ssids++; /* Broadcast scan */
227                 }
228                 wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
229         } else {
230                 wpa_s->prev_scan_ssid = BROADCAST_SSID_SCAN;
231                 params.num_ssids++;
232                 wpa_printf(MSG_DEBUG, "Starting AP scan for broadcast SSID");
233         }
234
235 #ifdef CONFIG_WPS
236         if (wps) {
237                 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
238                                                 wpa_s->wps->uuid, req_type);
239                 if (wps_ie) {
240                         params.extra_ies = wpabuf_head(wps_ie);
241                         params.extra_ies_len = wpabuf_len(wps_ie);
242                 }
243         }
244 #endif /* CONFIG_WPS */
245
246         if (wpa_s->use_client_mlme) {
247                 ieee80211_sta_set_probe_req_ie(wpa_s, params.extra_ies,
248                                                params.extra_ies_len);
249                 ret = ieee80211_sta_req_scan(wpa_s, params.ssids[0].ssid,
250                                              params.ssids[0].ssid_len);
251         } else {
252                 wpa_drv_set_probe_req_ie(wpa_s, params.extra_ies,
253                                          params.extra_ies_len);
254                 ret = wpa_drv_scan(wpa_s, &params);
255         }
256
257         wpabuf_free(wps_ie);
258
259         if (ret) {
260                 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
261                 wpa_supplicant_req_scan(wpa_s, 10, 0);
262         } else
263                 wpa_s->scan_runs++;
264 }
265
266
267 /**
268  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
269  * @wpa_s: Pointer to wpa_supplicant data
270  * @sec: Number of seconds after which to scan
271  * @usec: Number of microseconds after which to scan
272  *
273  * This function is used to schedule a scan for neighboring access points after
274  * the specified time.
275  */
276 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
277 {
278         /* If there's at least one network that should be specifically scanned
279          * then don't cancel the scan and reschedule.  Some drivers do
280          * background scanning which generates frequent scan results, and that
281          * causes the specific SSID scan to get continually pushed back and
282          * never happen, which causes hidden APs to never get probe-scanned.
283          */
284         if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
285             wpa_s->conf->ap_scan == 1) {
286                 struct wpa_ssid *ssid = wpa_s->conf->ssid;
287
288                 while (ssid) {
289                         if (!ssid->disabled && ssid->scan_ssid)
290                                 break;
291                         ssid = ssid->next;
292                 }
293                 if (ssid) {
294                         wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
295                                 "ensure that specific SSID scans occur");
296                         return;
297                 }
298         }
299
300         wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
301                 sec, usec);
302         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
303         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
304 }
305
306
307 /**
308  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
309  * @wpa_s: Pointer to wpa_supplicant data
310  *
311  * This function is used to cancel a scan request scheduled with
312  * wpa_supplicant_req_scan().
313  */
314 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
315 {
316         wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
317         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
318 }