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