Add support for specifying subset of enabled frequencies to scan
[libeap.git] / 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 "driver_i.h"
22 #include "mlme.h"
23 #include "wps_supplicant.h"
24
25
26 static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
27 {
28         struct wpa_ssid *ssid;
29         union wpa_event_data data;
30
31         ssid = wpa_supplicant_get_ssid(wpa_s);
32         if (ssid == NULL)
33                 return;
34
35         if (wpa_s->current_ssid == NULL)
36                 wpa_s->current_ssid = ssid;
37         wpa_supplicant_initiate_eapol(wpa_s);
38         wpa_printf(MSG_DEBUG, "Already associated with a configured network - "
39                    "generating associated event");
40         os_memset(&data, 0, sizeof(data));
41         wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
42 }
43
44
45 #ifdef CONFIG_WPS
46 static int wpas_wps_in_use(struct wpa_config *conf,
47                            enum wps_request_type *req_type)
48 {
49         struct wpa_ssid *ssid;
50         int wps = 0;
51
52         for (ssid = conf->ssid; ssid; ssid = ssid->next) {
53                 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
54                         continue;
55
56                 wps = 1;
57                 *req_type = wpas_wps_get_req_type(ssid);
58                 if (!ssid->eap.phase1)
59                         continue;
60
61                 if (os_strstr(ssid->eap.phase1, "pbc=1"))
62                         return 2;
63         }
64
65         return wps;
66 }
67 #endif /* CONFIG_WPS */
68
69
70 static int wpa_supplicant_enabled_networks(struct wpa_config *conf)
71 {
72         struct wpa_ssid *ssid = conf->ssid;
73         while (ssid) {
74                 if (!ssid->disabled)
75                         return 1;
76                 ssid = ssid->next;
77         }
78         return 0;
79 }
80
81
82 static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
83                                      struct wpa_ssid *ssid)
84 {
85         while (ssid) {
86                 if (!ssid->disabled)
87                         break;
88                 ssid = ssid->next;
89         }
90
91         /* ap_scan=2 mode - try to associate with each SSID. */
92         if (ssid == NULL) {
93                 wpa_printf(MSG_DEBUG, "wpa_supplicant_scan: Reached "
94                            "end of scan list - go back to beginning");
95                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
96                 wpa_supplicant_req_scan(wpa_s, 0, 0);
97                 return;
98         }
99         if (ssid->next) {
100                 /* Continue from the next SSID on the next attempt. */
101                 wpa_s->prev_scan_ssid = ssid;
102         } else {
103                 /* Start from the beginning of the SSID list. */
104                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
105         }
106         wpa_supplicant_associate(wpa_s, NULL, ssid);
107 }
108
109
110 static int int_array_len(const int *a)
111 {
112         int i;
113         for (i = 0; a && a[i]; i++)
114                 ;
115         return i;
116 }
117
118
119 static void int_array_concat(int **res, const int *a)
120 {
121         int reslen, alen, i;
122         int *n;
123
124         reslen = int_array_len(*res);
125         alen = int_array_len(a);
126
127         n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
128         if (n == NULL) {
129                 os_free(*res);
130                 *res = NULL;
131         }
132         for (i = 0; i <= alen; i++)
133                 n[reslen + i] = a[i];
134         *res = n;
135 }
136
137
138 static int freq_cmp(const void *a, const void *b)
139 {
140         int _a = *(int *) a;
141         int _b = *(int *) b;
142
143         if (_a == 0)
144                 return 1;
145         if (_b == 0)
146                 return -1;
147         return _a - _b;
148 }
149
150
151 static void int_array_sort_unique(int *a)
152 {
153         int alen;
154         int i, j;
155
156         if (a == NULL)
157                 return;
158
159         alen = int_array_len(a);
160         qsort(a, alen, sizeof(int), freq_cmp);
161
162         i = 0;
163         j = 1;
164         while (a[i] && a[j]) {
165                 if (a[i] == a[j]) {
166                         j++;
167                         continue;
168                 }
169                 a[++i] = a[j++];
170         }
171         if (a[i])
172                 i++;
173         a[i] = 0;
174 }
175
176
177 static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
178 {
179         struct wpa_supplicant *wpa_s = eloop_ctx;
180         struct wpa_ssid *ssid;
181         int scan_req = 0, ret;
182         struct wpabuf *wps_ie = NULL;
183         int wps = 0;
184 #ifdef CONFIG_WPS
185         enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
186 #endif /* CONFIG_WPS */
187         struct wpa_driver_scan_params params;
188         size_t max_ssids;
189
190         if (wpa_s->disconnected && !wpa_s->scan_req)
191                 return;
192
193         if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
194             !wpa_s->scan_req) {
195                 wpa_printf(MSG_DEBUG, "No enabled networks - do not scan");
196                 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
197                 return;
198         }
199
200         if (wpa_s->conf->ap_scan != 0 &&
201             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
202                 wpa_printf(MSG_DEBUG, "Using wired authentication - "
203                            "overriding ap_scan configuration");
204                 wpa_s->conf->ap_scan = 0;
205         }
206
207         if (wpa_s->conf->ap_scan == 0) {
208                 wpa_supplicant_gen_assoc_event(wpa_s);
209                 return;
210         }
211
212         if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
213             wpa_s->conf->ap_scan == 2)
214                 max_ssids = 1;
215         else {
216                 max_ssids = wpa_s->max_scan_ssids;
217                 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
218                         max_ssids = WPAS_MAX_SCAN_SSIDS;
219         }
220
221 #ifdef CONFIG_WPS
222         wps = wpas_wps_in_use(wpa_s->conf, &req_type);
223 #endif /* CONFIG_WPS */
224
225         if (wpa_s->scan_res_tried == 0 && wpa_s->conf->ap_scan == 1 &&
226             !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) &&
227             wps != 2) {
228                 wpa_s->scan_res_tried++;
229                 wpa_printf(MSG_DEBUG, "Trying to get current scan results "
230                            "first without requesting a new scan to speed up "
231                            "initial association");
232                 wpa_supplicant_event(wpa_s, EVENT_SCAN_RESULTS, NULL);
233                 return;
234         }
235
236         scan_req = wpa_s->scan_req;
237         wpa_s->scan_req = 0;
238
239         os_memset(&params, 0, sizeof(params));
240
241         if (wpa_s->wpa_state == WPA_DISCONNECTED ||
242             wpa_s->wpa_state == WPA_INACTIVE)
243                 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
244
245         /* Find the starting point from which to continue scanning */
246         ssid = wpa_s->conf->ssid;
247         if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
248                 while (ssid) {
249                         if (ssid == wpa_s->prev_scan_ssid) {
250                                 ssid = ssid->next;
251                                 break;
252                         }
253                         ssid = ssid->next;
254                 }
255         }
256
257         if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
258                 wpa_supplicant_assoc_try(wpa_s, ssid);
259                 return;
260         } else if (wpa_s->conf->ap_scan == 2) {
261                 /*
262                  * User-initiated scan request in ap_scan == 2; scan with
263                  * wildcard SSID.
264                  */
265                 ssid = NULL;
266         } else {
267                 struct wpa_ssid *start = ssid;
268                 int freqs_set = 0;
269                 if (ssid == NULL && max_ssids > 1)
270                         ssid = wpa_s->conf->ssid;
271                 while (ssid) {
272                         if (!ssid->disabled && ssid->scan_ssid) {
273                                 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
274                                                   ssid->ssid, ssid->ssid_len);
275                                 params.ssids[params.num_ssids].ssid =
276                                         ssid->ssid;
277                                 params.ssids[params.num_ssids].ssid_len =
278                                         ssid->ssid_len;
279                                 params.num_ssids++;
280                                 if (params.num_ssids + 1 >= max_ssids)
281                                         break;
282                         }
283                         ssid = ssid->next;
284                         if (ssid == start)
285                                 break;
286                         if (ssid == NULL && max_ssids > 1 &&
287                             start != wpa_s->conf->ssid)
288                                 ssid = wpa_s->conf->ssid;
289                 }
290
291                 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
292                         if (ssid->disabled)
293                                 continue;
294                         if ((params.freqs || !freqs_set) && ssid->scan_freq) {
295                                 int_array_concat(&params.freqs,
296                                                  ssid->scan_freq);
297                         } else {
298                                 os_free(params.freqs);
299                                 params.freqs = NULL;
300                         }
301                         freqs_set = 1;
302                 }
303                 int_array_sort_unique(params.freqs);
304         }
305
306         if (ssid) {
307                 wpa_s->prev_scan_ssid = ssid;
308                 if (max_ssids > 1) {
309                         wpa_printf(MSG_DEBUG, "Include wildcard SSID in the "
310                                    "scan request");
311                         params.num_ssids++;
312                 }
313                 wpa_printf(MSG_DEBUG, "Starting AP scan for specific SSID(s)");
314         } else {
315                 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
316                 params.num_ssids++;
317                 wpa_printf(MSG_DEBUG, "Starting AP scan for wildcard SSID");
318         }
319
320 #ifdef CONFIG_WPS
321         if (wps) {
322                 wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
323                                                 wpa_s->wps->uuid, req_type);
324                 if (wps_ie) {
325                         params.extra_ies = wpabuf_head(wps_ie);
326                         params.extra_ies_len = wpabuf_len(wps_ie);
327                 }
328         }
329 #endif /* CONFIG_WPS */
330
331         if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) {
332                 ieee80211_sta_set_probe_req_ie(wpa_s, params.extra_ies,
333                                                params.extra_ies_len);
334                 ret = ieee80211_sta_req_scan(wpa_s, params.ssids[0].ssid,
335                                              params.ssids[0].ssid_len);
336         } else {
337                 wpa_drv_set_probe_req_ie(wpa_s, params.extra_ies,
338                                          params.extra_ies_len);
339                 ret = wpa_drv_scan(wpa_s, &params);
340         }
341
342         wpabuf_free(wps_ie);
343         os_free(params.freqs);
344
345         if (ret) {
346                 wpa_printf(MSG_WARNING, "Failed to initiate AP scan.");
347                 wpa_supplicant_req_scan(wpa_s, 10, 0);
348         } else
349                 wpa_s->scan_runs++;
350 }
351
352
353 /**
354  * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
355  * @wpa_s: Pointer to wpa_supplicant data
356  * @sec: Number of seconds after which to scan
357  * @usec: Number of microseconds after which to scan
358  *
359  * This function is used to schedule a scan for neighboring access points after
360  * the specified time.
361  */
362 void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
363 {
364         /* If there's at least one network that should be specifically scanned
365          * then don't cancel the scan and reschedule.  Some drivers do
366          * background scanning which generates frequent scan results, and that
367          * causes the specific SSID scan to get continually pushed back and
368          * never happen, which causes hidden APs to never get probe-scanned.
369          */
370         if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
371             wpa_s->conf->ap_scan == 1) {
372                 struct wpa_ssid *ssid = wpa_s->conf->ssid;
373
374                 while (ssid) {
375                         if (!ssid->disabled && ssid->scan_ssid)
376                                 break;
377                         ssid = ssid->next;
378                 }
379                 if (ssid) {
380                         wpa_msg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
381                                 "ensure that specific SSID scans occur");
382                         return;
383                 }
384         }
385
386         wpa_msg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
387                 sec, usec);
388         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
389         eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
390 }
391
392
393 /**
394  * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
395  * @wpa_s: Pointer to wpa_supplicant data
396  *
397  * This function is used to cancel a scan request scheduled with
398  * wpa_supplicant_req_scan().
399  */
400 void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
401 {
402         wpa_msg(wpa_s, MSG_DEBUG, "Cancelling scan request");
403         eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
404 }