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