blob: cb2c8d6312d7324d72cf2bb1145b82e595726475 [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * WPA Supplicant - Scanning
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003 * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt3a787e62013-01-17 10:32:35 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "config.h"
16#include "wpa_supplicant_i.h"
17#include "driver_i.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "wps_supplicant.h"
19#include "p2p_supplicant.h"
20#include "p2p/p2p.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070021#include "hs20_supplicant.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "notify.h"
23#include "bss.h"
24#include "scan.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080025#include "mesh.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026
27
28static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
29{
30 struct wpa_ssid *ssid;
31 union wpa_event_data data;
32
33 ssid = wpa_supplicant_get_ssid(wpa_s);
34 if (ssid == NULL)
35 return;
36
37 if (wpa_s->current_ssid == NULL) {
38 wpa_s->current_ssid = ssid;
39 if (wpa_s->current_ssid != NULL)
40 wpas_notify_network_changed(wpa_s);
41 }
42 wpa_supplicant_initiate_eapol(wpa_s);
43 wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
44 "network - generating associated event");
45 os_memset(&data, 0, sizeof(data));
46 wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
47}
48
49
50#ifdef CONFIG_WPS
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080051static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052 enum wps_request_type *req_type)
53{
54 struct wpa_ssid *ssid;
55 int wps = 0;
56
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080057 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058 if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
59 continue;
60
61 wps = 1;
62 *req_type = wpas_wps_get_req_type(ssid);
63 if (!ssid->eap.phase1)
64 continue;
65
66 if (os_strstr(ssid->eap.phase1, "pbc=1"))
67 return 2;
68 }
69
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080070#ifdef CONFIG_P2P
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080071 if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
72 !wpa_s->conf->p2p_disabled) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080073 wpa_s->wps->dev.p2p = 1;
74 if (!wps) {
75 wps = 1;
76 *req_type = WPS_REQ_ENROLLEE_INFO;
77 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080078 }
79#endif /* CONFIG_P2P */
80
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070081 return wps;
82}
83#endif /* CONFIG_WPS */
84
85
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080086/**
87 * wpa_supplicant_enabled_networks - Check whether there are enabled networks
88 * @wpa_s: Pointer to wpa_supplicant data
89 * Returns: 0 if no networks are enabled, >0 if networks are enabled
90 *
91 * This function is used to figure out whether any networks (or Interworking
92 * with enabled credentials and auto_interworking) are present in the current
93 * configuration.
94 */
Dmitry Shmidt04949592012-07-19 12:16:46 -070095int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096{
Dmitry Shmidt04949592012-07-19 12:16:46 -070097 struct wpa_ssid *ssid = wpa_s->conf->ssid;
Dmitry Shmidtaa532512012-09-24 10:35:31 -070098 int count = 0, disabled = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700100 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700102 else
103 disabled++;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104 ssid = ssid->next;
105 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700106 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
107 wpa_s->conf->auto_interworking)
108 count++;
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700109 if (count == 0 && disabled > 0) {
110 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
111 "networks)", disabled);
112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 return count;
114}
115
116
117static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
118 struct wpa_ssid *ssid)
119{
120 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700121 if (!wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700122 break;
123 ssid = ssid->next;
124 }
125
126 /* ap_scan=2 mode - try to associate with each SSID. */
127 if (ssid == NULL) {
128 wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
129 "end of scan list - go back to beginning");
130 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
131 wpa_supplicant_req_scan(wpa_s, 0, 0);
132 return;
133 }
134 if (ssid->next) {
135 /* Continue from the next SSID on the next attempt. */
136 wpa_s->prev_scan_ssid = ssid;
137 } else {
138 /* Start from the beginning of the SSID list. */
139 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
140 }
141 wpa_supplicant_associate(wpa_s, NULL, ssid);
142}
143
144
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800145static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700146{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800147 struct wpa_supplicant *wpa_s = work->wpa_s;
148 struct wpa_driver_scan_params *params = work->ctx;
149 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700150
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800151 if (deinit) {
Dmitry Shmidtbd14a572014-02-18 10:33:49 -0800152 if (!work->started) {
153 wpa_scan_free_params(params);
154 return;
155 }
156 wpa_supplicant_notify_scanning(wpa_s, 0);
157 wpas_notify_scan_done(wpa_s, 0);
158 wpa_s->scan_work = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159 return;
160 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700162 if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
163 wpa_msg(wpa_s, MSG_INFO,
164 "Failed to assign random MAC address for a scan");
165 radio_work_done(work);
166 return;
167 }
168
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800169 wpa_supplicant_notify_scanning(wpa_s, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700170
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800171 if (wpa_s->clear_driver_scan_cache)
172 params->only_new_results = 1;
173 ret = wpa_drv_scan(wpa_s, params);
174 wpa_scan_free_params(params);
175 work->ctx = NULL;
176 if (ret) {
177 wpa_supplicant_notify_scanning(wpa_s, 0);
178 wpas_notify_scan_done(wpa_s, 0);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800179 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d",
180 ret);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800181 radio_work_done(work);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700183 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800184
185 os_get_reltime(&wpa_s->scan_trigger_time);
186 wpa_s->scan_runs++;
187 wpa_s->normal_scans++;
188 wpa_s->own_scan_requested = 1;
189 wpa_s->clear_driver_scan_cache = 0;
190 wpa_s->scan_work = work;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191}
192
193
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800194/**
195 * wpa_supplicant_trigger_scan - Request driver to start a scan
196 * @wpa_s: Pointer to wpa_supplicant data
197 * @params: Scan parameters
198 * Returns: 0 on success, -1 on failure
199 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
201 struct wpa_driver_scan_params *params)
202{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800203 struct wpa_driver_scan_params *ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800205 if (wpa_s->scan_work) {
206 wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
207 return -1;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800208 }
209
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800210 ctx = wpa_scan_clone_params(params);
211 if (ctx == NULL)
212 return -1;
213
214 if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
215 {
216 wpa_scan_free_params(ctx);
217 return -1;
218 }
219
220 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800221}
222
223
224static void
225wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
226{
227 struct wpa_supplicant *wpa_s = eloop_ctx;
228
229 wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
230
231 if (wpa_supplicant_req_sched_scan(wpa_s))
232 wpa_supplicant_req_scan(wpa_s, 0, 0);
233}
234
235
236static void
237wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
238{
239 struct wpa_supplicant *wpa_s = eloop_ctx;
240
241 wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
242
243 wpa_s->sched_scan_timed_out = 1;
244 wpa_supplicant_cancel_sched_scan(wpa_s);
245}
246
247
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800248int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
249 struct wpa_driver_scan_params *params,
250 int interval)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800251{
252 int ret;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700253
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800254 wpa_supplicant_notify_scanning(wpa_s, 1);
255 ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
256 if (ret)
257 wpa_supplicant_notify_scanning(wpa_s, 0);
Dmitry Shmidtb96dad42013-11-05 10:07:29 -0800258 else
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800259 wpa_s->sched_scanning = 1;
260
261 return ret;
262}
263
264
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800265int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800266{
267 int ret;
268
269 ret = wpa_drv_stop_sched_scan(wpa_s);
270 if (ret) {
271 wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
272 /* TODO: what to do if stopping fails? */
273 return -1;
274 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275
276 return ret;
277}
278
279
280static struct wpa_driver_scan_filter *
281wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
282{
283 struct wpa_driver_scan_filter *ssids;
284 struct wpa_ssid *ssid;
285 size_t count;
286
287 *num_ssids = 0;
288 if (!conf->filter_ssids)
289 return NULL;
290
291 for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
292 if (ssid->ssid && ssid->ssid_len)
293 count++;
294 }
295 if (count == 0)
296 return NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800297 ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 if (ssids == NULL)
299 return NULL;
300
301 for (ssid = conf->ssid; ssid; ssid = ssid->next) {
302 if (!ssid->ssid || !ssid->ssid_len)
303 continue;
304 os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
305 ssids[*num_ssids].ssid_len = ssid->ssid_len;
306 (*num_ssids)++;
307 }
308
309 return ssids;
310}
311
312
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313static void wpa_supplicant_optimize_freqs(
314 struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
315{
316#ifdef CONFIG_P2P
317 if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
318 wpa_s->go_params) {
319 /* Optimize provisioning state scan based on GO information */
320 if (wpa_s->p2p_in_provisioning < 5 &&
321 wpa_s->go_params->freq > 0) {
322 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
323 "preferred frequency %d MHz",
324 wpa_s->go_params->freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800325 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800326 if (params->freqs)
327 params->freqs[0] = wpa_s->go_params->freq;
328 } else if (wpa_s->p2p_in_provisioning < 8 &&
329 wpa_s->go_params->freq_list[0]) {
330 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
331 "channels");
332 int_array_concat(&params->freqs,
333 wpa_s->go_params->freq_list);
334 if (params->freqs)
335 int_array_sort_unique(params->freqs);
336 }
337 wpa_s->p2p_in_provisioning++;
338 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700339
340 if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
341 /*
342 * Optimize scan based on GO information during persistent
343 * group reinvocation
344 */
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -0700345 if (wpa_s->p2p_in_invitation < 5 &&
Dmitry Shmidt15907092014-03-25 10:42:57 -0700346 wpa_s->p2p_invite_go_freq > 0) {
347 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
348 wpa_s->p2p_invite_go_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800349 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt15907092014-03-25 10:42:57 -0700350 if (params->freqs)
351 params->freqs[0] = wpa_s->p2p_invite_go_freq;
352 }
353 wpa_s->p2p_in_invitation++;
354 if (wpa_s->p2p_in_invitation > 20) {
355 /*
356 * This should not really happen since the variable is
357 * cleared on group removal, but if it does happen, make
358 * sure we do not get stuck in special invitation scan
359 * mode.
360 */
361 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
362 wpa_s->p2p_in_invitation = 0;
363 }
364 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800365#endif /* CONFIG_P2P */
366
367#ifdef CONFIG_WPS
368 if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
369 /*
370 * Optimize post-provisioning scan based on channel used
371 * during provisioning.
372 */
373 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
374 "that was used during provisioning", wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800375 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800376 if (params->freqs)
377 params->freqs[0] = wpa_s->wps_freq;
378 wpa_s->after_wps--;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800379 } else if (wpa_s->after_wps)
380 wpa_s->after_wps--;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800381
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800382 if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
383 {
384 /* Optimize provisioning scan based on already known channel */
385 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
386 wpa_s->wps_freq);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800387 params->freqs = os_calloc(2, sizeof(int));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800388 if (params->freqs)
389 params->freqs[0] = wpa_s->wps_freq;
390 wpa_s->known_wps_freq = 0; /* only do this once */
391 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800392#endif /* CONFIG_WPS */
393}
394
395
396#ifdef CONFIG_INTERWORKING
397static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
398 struct wpabuf *buf)
399{
400 if (wpa_s->conf->interworking == 0)
401 return;
402
403 wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800404 wpabuf_put_u8(buf, 6);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800405 wpabuf_put_u8(buf, 0x00);
406 wpabuf_put_u8(buf, 0x00);
407 wpabuf_put_u8(buf, 0x00);
408 wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800409 wpabuf_put_u8(buf, 0x00);
410#ifdef CONFIG_HS20
411 wpabuf_put_u8(buf, 0x40); /* Bit 46 - WNM-Notification */
412#else /* CONFIG_HS20 */
413 wpabuf_put_u8(buf, 0x00);
414#endif /* CONFIG_HS20 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800415
416 wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
417 wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
418 1 + ETH_ALEN);
419 wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
420 /* No Venue Info */
421 if (!is_zero_ether_addr(wpa_s->conf->hessid))
422 wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
423}
424#endif /* CONFIG_INTERWORKING */
425
426
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700427static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800428{
429 struct wpabuf *extra_ie = NULL;
430#ifdef CONFIG_WPS
431 int wps = 0;
432 enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
433#endif /* CONFIG_WPS */
434
435#ifdef CONFIG_INTERWORKING
436 if (wpa_s->conf->interworking &&
437 wpabuf_resize(&extra_ie, 100) == 0)
438 wpas_add_interworking_elements(wpa_s, extra_ie);
439#endif /* CONFIG_INTERWORKING */
440
441#ifdef CONFIG_WPS
442 wps = wpas_wps_in_use(wpa_s, &req_type);
443
444 if (wps) {
445 struct wpabuf *wps_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700446 wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
447 DEV_PW_DEFAULT,
448 &wpa_s->wps->dev,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800449 wpa_s->wps->uuid, req_type,
450 0, NULL);
451 if (wps_ie) {
452 if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
453 wpabuf_put_buf(extra_ie, wps_ie);
454 wpabuf_free(wps_ie);
455 }
456 }
457
458#ifdef CONFIG_P2P
459 if (wps) {
460 size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
461 if (wpabuf_resize(&extra_ie, ielen) == 0)
462 wpas_p2p_scan_ie(wpa_s, extra_ie);
463 }
464#endif /* CONFIG_P2P */
465
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800466 wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
467
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800468#endif /* CONFIG_WPS */
469
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700470#ifdef CONFIG_HS20
471 if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800472 wpas_hs20_add_indication(extra_ie, -1);
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700473#endif /* CONFIG_HS20 */
474
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800475 return extra_ie;
476}
477
478
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800479#ifdef CONFIG_P2P
480
481/*
482 * Check whether there are any enabled networks or credentials that could be
483 * used for a non-P2P connection.
484 */
485static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
486{
487 struct wpa_ssid *ssid;
488
489 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
490 if (wpas_network_disabled(wpa_s, ssid))
491 continue;
492 if (!ssid->p2p_group)
493 return 1;
494 }
495
496 if (wpa_s->conf->cred && wpa_s->conf->interworking &&
497 wpa_s->conf->auto_interworking)
498 return 1;
499
500 return 0;
501}
502
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700503#endif /* CONFIG_P2P */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800504
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800505
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700506static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
507 u16 num_modes,
508 enum hostapd_hw_mode mode)
509{
510 u16 i;
511
512 for (i = 0; i < num_modes; i++) {
513 if (modes[i].mode == mode)
514 return &modes[i];
515 }
516
517 return NULL;
518}
519
520
521static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
522 enum hostapd_hw_mode band,
523 struct wpa_driver_scan_params *params)
524{
525 /* Include only supported channels for the specified band */
526 struct hostapd_hw_modes *mode;
527 int count, i;
528
529 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
530 if (mode == NULL) {
531 /* No channels supported in this band - use empty list */
532 params->freqs = os_zalloc(sizeof(int));
533 return;
534 }
535
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800536 params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700537 if (params->freqs == NULL)
538 return;
539 for (count = 0, i = 0; i < mode->num_channels; i++) {
540 if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
541 continue;
542 params->freqs[count++] = mode->channels[i].freq;
543 }
544}
545
546
547static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
548 struct wpa_driver_scan_params *params)
549{
550 if (wpa_s->hw.modes == NULL)
551 return; /* unknown what channels the driver supports */
552 if (params->freqs)
553 return; /* already using a limited channel set */
554 if (wpa_s->setband == WPA_SETBAND_5G)
555 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
556 params);
557 else if (wpa_s->setband == WPA_SETBAND_2G)
558 wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
559 params);
560}
561
562
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700563static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
564 struct wpa_driver_scan_params *params,
565 size_t max_ssids)
566{
567 unsigned int i;
568 struct wpa_ssid *ssid;
569
570 for (i = 0; i < wpa_s->scan_id_count; i++) {
571 unsigned int j;
572
573 ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
574 if (!ssid || !ssid->scan_ssid)
575 continue;
576
577 for (j = 0; j < params->num_ssids; j++) {
578 if (params->ssids[j].ssid_len == ssid->ssid_len &&
579 params->ssids[j].ssid &&
580 os_memcmp(params->ssids[j].ssid, ssid->ssid,
581 ssid->ssid_len) == 0)
582 break;
583 }
584 if (j < params->num_ssids)
585 continue; /* already in the list */
586
587 if (params->num_ssids + 1 > max_ssids) {
588 wpa_printf(MSG_DEBUG,
589 "Over max scan SSIDs for manual request");
590 break;
591 }
592
593 wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
594 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
595 params->ssids[params->num_ssids].ssid = ssid->ssid;
596 params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
597 params->num_ssids++;
598 }
599
600 wpa_s->scan_id_count = 0;
601}
602
603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
605{
606 struct wpa_supplicant *wpa_s = eloop_ctx;
607 struct wpa_ssid *ssid;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800608 int ret, p2p_in_prog;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700609 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700611 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 size_t max_ssids;
613 enum wpa_states prev_state;
614
Dmitry Shmidt18463232014-01-24 12:29:41 -0800615 if (wpa_s->pno || wpa_s->pno_sched_pending) {
616 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
617 return;
618 }
619
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700620 if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
621 wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
622 return;
623 }
624
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800625 if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidtaa532512012-09-24 10:35:31 -0700626 wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
628 return;
629 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800630
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700631 if (wpa_s->scanning) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800632 /*
633 * If we are already in scanning state, we shall reschedule the
634 * the incoming scan request.
635 */
636 wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
637 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidt5887a9d2012-09-14 10:47:43 -0700638 return;
639 }
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800640
Dmitry Shmidt04949592012-07-19 12:16:46 -0700641 if (!wpa_supplicant_enabled_networks(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800642 wpa_s->scan_req == NORMAL_SCAN_REQ) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643 wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
644 wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
645 return;
646 }
647
648 if (wpa_s->conf->ap_scan != 0 &&
649 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
650 wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
651 "overriding ap_scan configuration");
652 wpa_s->conf->ap_scan = 0;
653 wpas_notify_ap_scan_changed(wpa_s);
654 }
655
656 if (wpa_s->conf->ap_scan == 0) {
657 wpa_supplicant_gen_assoc_event(wpa_s);
658 return;
659 }
660
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800661 p2p_in_prog = wpas_p2p_in_progress(wpa_s);
662 if (p2p_in_prog && p2p_in_prog != 2) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800663 wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
664 wpa_supplicant_req_scan(wpa_s, 5, 0);
Dmitry Shmidt051af732013-10-22 13:52:46 -0700665 return;
666 }
Dmitry Shmidt051af732013-10-22 13:52:46 -0700667
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800668 if (wpa_s->conf->ap_scan == 2)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 max_ssids = 1;
670 else {
671 max_ssids = wpa_s->max_scan_ssids;
672 if (max_ssids > WPAS_MAX_SCAN_SSIDS)
673 max_ssids = WPAS_MAX_SCAN_SSIDS;
674 }
675
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800676 wpa_s->last_scan_req = wpa_s->scan_req;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800677 wpa_s->scan_req = NORMAL_SCAN_REQ;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678
679 os_memset(&params, 0, sizeof(params));
680
681 prev_state = wpa_s->wpa_state;
682 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
683 wpa_s->wpa_state == WPA_INACTIVE)
684 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
685
Dmitry Shmidt04949592012-07-19 12:16:46 -0700686 /*
687 * If autoscan has set its own scanning parameters
688 */
689 if (wpa_s->autoscan_params != NULL) {
690 scan_params = wpa_s->autoscan_params;
691 goto scan;
692 }
693
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800694 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
695 wpa_s->connect_without_scan) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700696 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
697 if (ssid == wpa_s->connect_without_scan)
698 break;
699 }
700 wpa_s->connect_without_scan = NULL;
701 if (ssid) {
702 wpa_printf(MSG_DEBUG, "Start a pre-selected network "
703 "without scan step");
704 wpa_supplicant_associate(wpa_s, NULL, ssid);
705 return;
706 }
707 }
708
Dmitry Shmidt04949592012-07-19 12:16:46 -0700709#ifdef CONFIG_P2P
710 if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
711 wpa_s->go_params) {
Dmitry Shmidtcce06662013-11-04 18:44:24 -0800712 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
713 wpa_s->p2p_in_provisioning,
714 wpa_s->show_group_started);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700715 params.ssids[0].ssid = wpa_s->go_params->ssid;
716 params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
717 params.num_ssids = 1;
718 goto ssid_list_set;
719 }
Dmitry Shmidt15907092014-03-25 10:42:57 -0700720
721 if (wpa_s->p2p_in_invitation) {
722 if (wpa_s->current_ssid) {
723 wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
724 params.ssids[0].ssid = wpa_s->current_ssid->ssid;
725 params.ssids[0].ssid_len =
726 wpa_s->current_ssid->ssid_len;
727 params.num_ssids = 1;
728 } else {
729 wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
730 }
731 goto ssid_list_set;
732 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700733#endif /* CONFIG_P2P */
734
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735 /* Find the starting point from which to continue scanning */
736 ssid = wpa_s->conf->ssid;
737 if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
738 while (ssid) {
739 if (ssid == wpa_s->prev_scan_ssid) {
740 ssid = ssid->next;
741 break;
742 }
743 ssid = ssid->next;
744 }
745 }
746
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800747 if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
748 wpa_s->conf->ap_scan == 2) {
Jouni Malinen75ecf522011-06-27 15:19:46 -0700749 wpa_s->connect_without_scan = NULL;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800750 wpa_s->prev_scan_wildcard = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751 wpa_supplicant_assoc_try(wpa_s, ssid);
752 return;
753 } else if (wpa_s->conf->ap_scan == 2) {
754 /*
755 * User-initiated scan request in ap_scan == 2; scan with
756 * wildcard SSID.
757 */
758 ssid = NULL;
Dmitry Shmidt98660862014-03-11 17:26:21 -0700759 } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
760 /*
761 * Perform single-channel single-SSID scan for
762 * reassociate-to-same-BSS operation.
763 */
764 /* Setup SSID */
765 ssid = wpa_s->current_ssid;
766 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
767 ssid->ssid, ssid->ssid_len);
768 params.ssids[0].ssid = ssid->ssid;
769 params.ssids[0].ssid_len = ssid->ssid_len;
770 params.num_ssids = 1;
771
772 /*
773 * Allocate memory for frequency array, allocate one extra
774 * slot for the zero-terminator.
775 */
776 params.freqs = os_malloc(sizeof(int) * 2);
777 if (params.freqs == NULL) {
778 wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
779 return;
780 }
781 params.freqs[0] = wpa_s->assoc_freq;
782 params.freqs[1] = 0;
783
784 /*
785 * Reset the reattach flag so that we fall back to full scan if
786 * this scan fails.
787 */
788 wpa_s->reattach = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700789 } else {
790 struct wpa_ssid *start = ssid, *tssid;
791 int freqs_set = 0;
792 if (ssid == NULL && max_ssids > 1)
793 ssid = wpa_s->conf->ssid;
794 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700795 if (!wpas_network_disabled(wpa_s, ssid) &&
796 ssid->scan_ssid) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
798 ssid->ssid, ssid->ssid_len);
799 params.ssids[params.num_ssids].ssid =
800 ssid->ssid;
801 params.ssids[params.num_ssids].ssid_len =
802 ssid->ssid_len;
803 params.num_ssids++;
804 if (params.num_ssids + 1 >= max_ssids)
805 break;
806 }
807 ssid = ssid->next;
808 if (ssid == start)
809 break;
810 if (ssid == NULL && max_ssids > 1 &&
811 start != wpa_s->conf->ssid)
812 ssid = wpa_s->conf->ssid;
813 }
814
Dmitry Shmidtc2817022014-07-02 10:32:10 -0700815 if (wpa_s->scan_id_count &&
816 wpa_s->last_scan_req == MANUAL_SCAN_REQ)
817 wpa_set_scan_ssids(wpa_s, &params, max_ssids);
818
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800819 for (tssid = wpa_s->conf->ssid;
820 wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
821 tssid = tssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700822 if (wpas_network_disabled(wpa_s, tssid))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823 continue;
824 if ((params.freqs || !freqs_set) && tssid->scan_freq) {
825 int_array_concat(&params.freqs,
826 tssid->scan_freq);
827 } else {
828 os_free(params.freqs);
829 params.freqs = NULL;
830 }
831 freqs_set = 1;
832 }
833 int_array_sort_unique(params.freqs);
834 }
835
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800836 if (ssid && max_ssids == 1) {
837 /*
838 * If the driver is limited to 1 SSID at a time interleave
839 * wildcard SSID scans with specific SSID scans to avoid
840 * waiting a long time for a wildcard scan.
841 */
842 if (!wpa_s->prev_scan_wildcard) {
843 params.ssids[0].ssid = NULL;
844 params.ssids[0].ssid_len = 0;
845 wpa_s->prev_scan_wildcard = 1;
846 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
847 "wildcard SSID (Interleave with specific)");
848 } else {
849 wpa_s->prev_scan_ssid = ssid;
850 wpa_s->prev_scan_wildcard = 0;
851 wpa_dbg(wpa_s, MSG_DEBUG,
852 "Starting AP scan for specific SSID: %s",
853 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 }
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800855 } else if (ssid) {
856 /* max_ssids > 1 */
857
858 wpa_s->prev_scan_ssid = ssid;
859 wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
860 "the scan request");
861 params.num_ssids++;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800862 } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
863 wpa_s->manual_scan_passive && params.num_ssids == 0) {
864 wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865 } else {
866 wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
867 params.num_ssids++;
868 wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
869 "SSID");
870 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700871#ifdef CONFIG_P2P
872ssid_list_set:
873#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700874
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800875 wpa_supplicant_optimize_freqs(wpa_s, &params);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700876 extra_ie = wpa_supplicant_extra_ies(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800878 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
879 wpa_s->manual_scan_only_new)
880 params.only_new_results = 1;
881
882 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
883 wpa_s->manual_scan_freqs) {
884 wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
885 params.freqs = wpa_s->manual_scan_freqs;
886 wpa_s->manual_scan_freqs = NULL;
887 }
888
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700889 if (params.freqs == NULL && wpa_s->next_scan_freqs) {
890 wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
891 "generated frequency list");
892 params.freqs = wpa_s->next_scan_freqs;
893 } else
894 os_free(wpa_s->next_scan_freqs);
895 wpa_s->next_scan_freqs = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700896 wpa_setband_scan_freqs(wpa_s, &params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700897
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -0700898 /* See if user specified frequencies. If so, scan only those. */
899 if (wpa_s->conf->freq_list && !params.freqs) {
900 wpa_dbg(wpa_s, MSG_DEBUG,
901 "Optimize scan based on conf->freq_list");
902 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
903 }
904
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700905 /* Use current associated channel? */
906 if (wpa_s->conf->scan_cur_freq && !params.freqs) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700907 unsigned int num = wpa_s->num_multichan_concurrent;
908
909 params.freqs = os_calloc(num + 1, sizeof(int));
910 if (params.freqs) {
911 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
912 if (num > 0) {
913 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
914 "current operating channels since "
915 "scan_cur_freq is enabled");
916 } else {
917 os_free(params.freqs);
918 params.freqs = NULL;
919 }
Dmitry Shmidtea69e842013-05-13 14:52:28 -0700920 }
921 }
922
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700923 params.filter_ssids = wpa_supplicant_build_filter_ssids(
924 wpa_s->conf, &params.num_filter_ssids);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800925 if (extra_ie) {
926 params.extra_ies = wpabuf_head(extra_ie);
927 params.extra_ies_len = wpabuf_len(extra_ie);
928 }
929
930#ifdef CONFIG_P2P
Dmitry Shmidt413dde72014-04-11 10:23:22 -0700931 if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
Dmitry Shmidt04949592012-07-19 12:16:46 -0700932 (wpa_s->show_group_started && wpa_s->go_params)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800933 /*
934 * The interface may not yet be in P2P mode, so we have to
935 * explicitly request P2P probe to disable CCK rates.
936 */
937 params.p2p_probe = 1;
938 }
939#endif /* CONFIG_P2P */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800941 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
942 params.mac_addr_rand = 1;
943 if (wpa_s->mac_addr_scan) {
944 params.mac_addr = wpa_s->mac_addr_scan;
945 params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
946 }
947 }
948
Dmitry Shmidt04949592012-07-19 12:16:46 -0700949 scan_params = &params;
950
951scan:
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800952#ifdef CONFIG_P2P
953 /*
954 * If the driver does not support multi-channel concurrency and a
955 * virtual interface that shares the same radio with the wpa_s interface
956 * is operating there may not be need to scan other channels apart from
957 * the current operating channel on the other virtual interface. Filter
958 * out other channels in case we are trying to find a connection for a
959 * station interface when we are not configured to prefer station
960 * connection and a concurrent operation is already in process.
961 */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800962 if (wpa_s->scan_for_connection &&
963 wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800964 !scan_params->freqs && !params.freqs &&
965 wpas_is_p2p_prioritized(wpa_s) &&
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800966 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
967 non_p2p_network_enabled(wpa_s)) {
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700968 unsigned int num = wpa_s->num_multichan_concurrent;
969
970 params.freqs = os_calloc(num + 1, sizeof(int));
971 if (params.freqs) {
972 num = get_shared_radio_freqs(wpa_s, params.freqs, num);
973 if (num > 0 && num == wpa_s->num_multichan_concurrent) {
974 wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
975 } else {
976 os_free(params.freqs);
977 params.freqs = NULL;
978 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800979 }
980 }
981#endif /* CONFIG_P2P */
982
Dmitry Shmidt04949592012-07-19 12:16:46 -0700983 ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700984
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800985 if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
986 !wpa_s->manual_scan_freqs) {
987 /* Restore manual_scan_freqs for the next attempt */
988 wpa_s->manual_scan_freqs = params.freqs;
989 params.freqs = NULL;
990 }
991
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800992 wpabuf_free(extra_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700993 os_free(params.freqs);
994 os_free(params.filter_ssids);
995
996 if (ret) {
997 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
998 if (prev_state != wpa_s->wpa_state)
999 wpa_supplicant_set_state(wpa_s, prev_state);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001000 /* Restore scan_req since we will try to scan again */
Dmitry Shmidt01904cf2013-12-05 11:08:35 -08001001 wpa_s->scan_req = wpa_s->last_scan_req;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001002 wpa_supplicant_req_scan(wpa_s, 1, 0);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001003 } else {
1004 wpa_s->scan_for_connection = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001005 }
1006}
1007
1008
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001009void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
1010{
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001011 struct os_reltime remaining, new_int;
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001012 int cancelled;
1013
1014 cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
1015 &remaining);
1016
1017 new_int.sec = sec;
1018 new_int.usec = 0;
Dmitry Shmidtfa3fc4a2013-11-21 13:34:38 -08001019 if (cancelled && os_reltime_before(&remaining, &new_int)) {
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001020 new_int.sec = remaining.sec;
1021 new_int.usec = remaining.usec;
1022 }
1023
Dmitry Shmidt051af732013-10-22 13:52:46 -07001024 if (cancelled) {
1025 eloop_register_timeout(new_int.sec, new_int.usec,
1026 wpa_supplicant_scan, wpa_s, NULL);
1027 }
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001028 wpa_s->scan_interval = sec;
1029}
1030
1031
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001032/**
1033 * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
1034 * @wpa_s: Pointer to wpa_supplicant data
1035 * @sec: Number of seconds after which to scan
1036 * @usec: Number of microseconds after which to scan
1037 *
1038 * This function is used to schedule a scan for neighboring access points after
1039 * the specified time.
1040 */
1041void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
1042{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001043 int res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
1044 NULL);
1045 if (res == 1) {
1046 wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001047 sec, usec);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001048 } else if (res == 0) {
1049 wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
1050 sec, usec);
1051 } else {
1052 wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
1053 sec, usec);
1054 eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001055 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001056}
1057
1058
1059/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001060 * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
1061 * @wpa_s: Pointer to wpa_supplicant data
1062 * @sec: Number of seconds after which to scan
1063 * @usec: Number of microseconds after which to scan
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001064 * Returns: 0 on success or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001065 *
1066 * This function is used to schedule periodic scans for neighboring
1067 * access points after the specified time.
1068 */
1069int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
1070 int sec, int usec)
1071{
1072 if (!wpa_s->sched_scan_supported)
1073 return -1;
1074
1075 eloop_register_timeout(sec, usec,
1076 wpa_supplicant_delayed_sched_scan_timeout,
1077 wpa_s, NULL);
1078
1079 return 0;
1080}
1081
1082
1083/**
1084 * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
1085 * @wpa_s: Pointer to wpa_supplicant data
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001086 * Returns: 0 is sched_scan was started or -1 otherwise
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001087 *
1088 * This function is used to schedule periodic scans for neighboring
1089 * access points repeating the scan continuously.
1090 */
1091int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
1092{
1093 struct wpa_driver_scan_params params;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001094 struct wpa_driver_scan_params *scan_params;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001095 enum wpa_states prev_state;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001096 struct wpa_ssid *ssid = NULL;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001097 struct wpabuf *extra_ie = NULL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001098 int ret;
1099 unsigned int max_sched_scan_ssids;
1100 int wildcard = 0;
1101 int need_ssids;
1102
1103 if (!wpa_s->sched_scan_supported)
1104 return -1;
1105
1106 if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
1107 max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
1108 else
1109 max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001110 if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001111 return -1;
1112
1113 if (wpa_s->sched_scanning) {
1114 wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
1115 return 0;
1116 }
1117
1118 need_ssids = 0;
1119 for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001120 if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001121 /* Use wildcard SSID to find this network */
1122 wildcard = 1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001123 } else if (!wpas_network_disabled(wpa_s, ssid) &&
1124 ssid->ssid_len)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001125 need_ssids++;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001126
1127#ifdef CONFIG_WPS
1128 if (!wpas_network_disabled(wpa_s, ssid) &&
1129 ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
1130 /*
1131 * Normal scan is more reliable and faster for WPS
1132 * operations and since these are for short periods of
1133 * time, the benefit of trying to use sched_scan would
1134 * be limited.
1135 */
1136 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1137 "sched_scan for WPS");
1138 return -1;
1139 }
1140#endif /* CONFIG_WPS */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001141 }
1142 if (wildcard)
1143 need_ssids++;
1144
1145 if (wpa_s->normal_scans < 3 &&
1146 (need_ssids <= wpa_s->max_scan_ssids ||
1147 wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
1148 /*
1149 * When normal scan can speed up operations, use that for the
1150 * first operations before starting the sched_scan to allow
1151 * user space sleep more. We do this only if the normal scan
1152 * has functionality that is suitable for this or if the
1153 * sched_scan does not have better support for multiple SSIDs.
1154 */
1155 wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
1156 "sched_scan for initial scans (normal_scans=%d)",
1157 wpa_s->normal_scans);
1158 return -1;
1159 }
1160
1161 os_memset(&params, 0, sizeof(params));
1162
1163 /* If we can't allocate space for the filters, we just don't filter */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001164 params.filter_ssids = os_calloc(wpa_s->max_match_sets,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001165 sizeof(struct wpa_driver_scan_filter));
1166
1167 prev_state = wpa_s->wpa_state;
1168 if (wpa_s->wpa_state == WPA_DISCONNECTED ||
1169 wpa_s->wpa_state == WPA_INACTIVE)
1170 wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
1171
Dmitry Shmidt04949592012-07-19 12:16:46 -07001172 if (wpa_s->autoscan_params != NULL) {
1173 scan_params = wpa_s->autoscan_params;
1174 goto scan;
1175 }
1176
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001177 /* Find the starting point from which to continue scanning */
1178 ssid = wpa_s->conf->ssid;
1179 if (wpa_s->prev_sched_ssid) {
1180 while (ssid) {
1181 if (ssid == wpa_s->prev_sched_ssid) {
1182 ssid = ssid->next;
1183 break;
1184 }
1185 ssid = ssid->next;
1186 }
1187 }
1188
1189 if (!ssid || !wpa_s->prev_sched_ssid) {
1190 wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
Dmitry Shmidt51b6ea82013-05-08 10:42:09 -07001191 if (wpa_s->conf->sched_scan_interval)
1192 wpa_s->sched_scan_interval =
1193 wpa_s->conf->sched_scan_interval;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001194 if (wpa_s->sched_scan_interval == 0)
1195 wpa_s->sched_scan_interval = 10;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001196 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1197 wpa_s->first_sched_scan = 1;
1198 ssid = wpa_s->conf->ssid;
1199 wpa_s->prev_sched_ssid = ssid;
1200 }
1201
1202 if (wildcard) {
1203 wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
1204 params.num_ssids++;
1205 }
1206
1207 while (ssid) {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001208 if (wpas_network_disabled(wpa_s, ssid))
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001209 goto next;
1210
1211 if (params.num_filter_ssids < wpa_s->max_match_sets &&
1212 params.filter_ssids && ssid->ssid && ssid->ssid_len) {
1213 wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
1214 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1215 os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
1216 ssid->ssid, ssid->ssid_len);
1217 params.filter_ssids[params.num_filter_ssids].ssid_len =
1218 ssid->ssid_len;
1219 params.num_filter_ssids++;
1220 } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
1221 {
1222 wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
1223 "filter for sched_scan - drop filter");
1224 os_free(params.filter_ssids);
1225 params.filter_ssids = NULL;
1226 params.num_filter_ssids = 0;
1227 }
1228
1229 if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
1230 if (params.num_ssids == max_sched_scan_ssids)
1231 break; /* only room for broadcast SSID */
1232 wpa_dbg(wpa_s, MSG_DEBUG,
1233 "add to active scan ssid: %s",
1234 wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
1235 params.ssids[params.num_ssids].ssid =
1236 ssid->ssid;
1237 params.ssids[params.num_ssids].ssid_len =
1238 ssid->ssid_len;
1239 params.num_ssids++;
1240 if (params.num_ssids >= max_sched_scan_ssids) {
1241 wpa_s->prev_sched_ssid = ssid;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001242 do {
1243 ssid = ssid->next;
1244 } while (ssid &&
1245 (wpas_network_disabled(wpa_s, ssid) ||
1246 !ssid->scan_ssid));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001247 break;
1248 }
1249 }
1250
1251 next:
1252 wpa_s->prev_sched_ssid = ssid;
1253 ssid = ssid->next;
1254 }
1255
1256 if (params.num_filter_ssids == 0) {
1257 os_free(params.filter_ssids);
1258 params.filter_ssids = NULL;
1259 }
1260
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001261 extra_ie = wpa_supplicant_extra_ies(wpa_s);
1262 if (extra_ie) {
1263 params.extra_ies = wpabuf_head(extra_ie);
1264 params.extra_ies_len = wpabuf_len(extra_ie);
1265 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001266
Dmitry Shmidt18463232014-01-24 12:29:41 -08001267 if (wpa_s->conf->filter_rssi)
1268 params.filter_rssi = wpa_s->conf->filter_rssi;
1269
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001270 /* See if user specified frequencies. If so, scan only those. */
1271 if (wpa_s->conf->freq_list && !params.freqs) {
1272 wpa_dbg(wpa_s, MSG_DEBUG,
1273 "Optimize scan based on conf->freq_list");
1274 int_array_concat(&params.freqs, wpa_s->conf->freq_list);
1275 }
1276
Dmitry Shmidt04949592012-07-19 12:16:46 -07001277 scan_params = &params;
1278
1279scan:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001280 if (ssid || !wpa_s->first_sched_scan) {
1281 wpa_dbg(wpa_s, MSG_DEBUG,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001282 "Starting sched scan: interval %d timeout %d",
1283 wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001284 } else {
1285 wpa_dbg(wpa_s, MSG_DEBUG,
1286 "Starting sched scan: interval %d (no timeout)",
1287 wpa_s->sched_scan_interval);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001288 }
1289
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001290 wpa_setband_scan_freqs(wpa_s, scan_params);
1291
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001292 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
1293 params.mac_addr_rand = 1;
1294 if (wpa_s->mac_addr_sched_scan) {
1295 params.mac_addr = wpa_s->mac_addr_sched_scan;
1296 params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
1297 ETH_ALEN;
1298 }
1299 }
1300
Dmitry Shmidt04949592012-07-19 12:16:46 -07001301 ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001302 wpa_s->sched_scan_interval);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001303 wpabuf_free(extra_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001304 os_free(params.filter_ssids);
1305 if (ret) {
1306 wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
1307 if (prev_state != wpa_s->wpa_state)
1308 wpa_supplicant_set_state(wpa_s, prev_state);
1309 return ret;
1310 }
1311
1312 /* If we have more SSIDs to scan, add a timeout so we scan them too */
1313 if (ssid || !wpa_s->first_sched_scan) {
1314 wpa_s->sched_scan_timed_out = 0;
1315 eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
1316 wpa_supplicant_sched_scan_timeout,
1317 wpa_s, NULL);
1318 wpa_s->first_sched_scan = 0;
1319 wpa_s->sched_scan_timeout /= 2;
1320 wpa_s->sched_scan_interval *= 2;
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001321 if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
1322 wpa_s->sched_scan_interval = 10;
1323 wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
1324 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001325 }
1326
Dmitry Shmidt2f023192013-03-12 12:44:17 -07001327 /* If there is no more ssids, start next time from the beginning */
1328 if (!ssid)
1329 wpa_s->prev_sched_ssid = NULL;
1330
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001331 return 0;
1332}
1333
1334
1335/**
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001336 * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
1337 * @wpa_s: Pointer to wpa_supplicant data
1338 *
1339 * This function is used to cancel a scan request scheduled with
1340 * wpa_supplicant_req_scan().
1341 */
1342void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
1343{
1344 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
1345 eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346}
1347
1348
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001349/**
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -07001350 * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
1351 * @wpa_s: Pointer to wpa_supplicant data
1352 *
1353 * This function is used to stop a delayed scheduled scan.
1354 */
1355void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
1356{
1357 if (!wpa_s->sched_scan_supported)
1358 return;
1359
1360 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
1361 eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
1362 wpa_s, NULL);
1363}
1364
1365
1366/**
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001367 * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
1368 * @wpa_s: Pointer to wpa_supplicant data
1369 *
1370 * This function is used to stop a periodic scheduled scan.
1371 */
1372void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
1373{
1374 if (!wpa_s->sched_scanning)
1375 return;
1376
1377 wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
1378 eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
1379 wpa_supplicant_stop_sched_scan(wpa_s);
1380}
1381
1382
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001383/**
1384 * wpa_supplicant_notify_scanning - Indicate possible scan state change
1385 * @wpa_s: Pointer to wpa_supplicant data
1386 * @scanning: Whether scanning is currently in progress
1387 *
1388 * This function is to generate scanning notifycations. It is called whenever
1389 * there may have been a change in scanning (scan started, completed, stopped).
1390 * wpas_notify_scanning() is called whenever the scanning state changed from the
1391 * previously notified state.
1392 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
1394 int scanning)
1395{
1396 if (wpa_s->scanning != scanning) {
1397 wpa_s->scanning = scanning;
1398 wpas_notify_scanning(wpa_s);
1399 }
1400}
1401
1402
1403static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
1404{
1405 int rate = 0;
1406 const u8 *ie;
1407 int i;
1408
1409 ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
1410 for (i = 0; ie && i < ie[1]; i++) {
1411 if ((ie[i + 2] & 0x7f) > rate)
1412 rate = ie[i + 2] & 0x7f;
1413 }
1414
1415 ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
1416 for (i = 0; ie && i < ie[1]; i++) {
1417 if ((ie[i + 2] & 0x7f) > rate)
1418 rate = ie[i + 2] & 0x7f;
1419 }
1420
1421 return rate;
1422}
1423
1424
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001425/**
1426 * wpa_scan_get_ie - Fetch a specified information element from a scan result
1427 * @res: Scan result entry
1428 * @ie: Information element identitifier (WLAN_EID_*)
1429 * Returns: Pointer to the information element (id field) or %NULL if not found
1430 *
1431 * This function returns the first matching information element in the scan
1432 * result.
1433 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001434const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
1435{
1436 const u8 *end, *pos;
1437
1438 pos = (const u8 *) (res + 1);
1439 end = pos + res->ie_len;
1440
1441 while (pos + 1 < end) {
1442 if (pos + 2 + pos[1] > end)
1443 break;
1444 if (pos[0] == ie)
1445 return pos;
1446 pos += 2 + pos[1];
1447 }
1448
1449 return NULL;
1450}
1451
1452
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001453/**
1454 * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
1455 * @res: Scan result entry
1456 * @vendor_type: Vendor type (four octets starting the IE payload)
1457 * Returns: Pointer to the information element (id field) or %NULL if not found
1458 *
1459 * This function returns the first matching information element in the scan
1460 * result.
1461 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001462const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
1463 u32 vendor_type)
1464{
1465 const u8 *end, *pos;
1466
1467 pos = (const u8 *) (res + 1);
1468 end = pos + res->ie_len;
1469
1470 while (pos + 1 < end) {
1471 if (pos + 2 + pos[1] > end)
1472 break;
1473 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1474 vendor_type == WPA_GET_BE32(&pos[2]))
1475 return pos;
1476 pos += 2 + pos[1];
1477 }
1478
1479 return NULL;
1480}
1481
1482
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001483/**
Dmitry Shmidt96571392013-10-14 12:54:46 -07001484 * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
1485 * @res: Scan result entry
1486 * @vendor_type: Vendor type (four octets starting the IE payload)
1487 * Returns: Pointer to the information element (id field) or %NULL if not found
1488 *
1489 * This function returns the first matching information element in the scan
1490 * result.
1491 *
1492 * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
1493 * from Beacon frames instead of either Beacon or Probe Response frames.
1494 */
1495const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
1496 u32 vendor_type)
1497{
1498 const u8 *end, *pos;
1499
1500 if (res->beacon_ie_len == 0)
1501 return NULL;
1502
1503 pos = (const u8 *) (res + 1);
1504 pos += res->ie_len;
1505 end = pos + res->beacon_ie_len;
1506
1507 while (pos + 1 < end) {
1508 if (pos + 2 + pos[1] > end)
1509 break;
1510 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1511 vendor_type == WPA_GET_BE32(&pos[2]))
1512 return pos;
1513 pos += 2 + pos[1];
1514 }
1515
1516 return NULL;
1517}
1518
1519
1520/**
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001521 * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
1522 * @res: Scan result entry
1523 * @vendor_type: Vendor type (four octets starting the IE payload)
1524 * Returns: Pointer to the information element payload or %NULL if not found
1525 *
1526 * This function returns concatenated payload of possibly fragmented vendor
1527 * specific information elements in the scan result. The caller is responsible
1528 * for freeing the returned buffer.
1529 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001530struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
1531 u32 vendor_type)
1532{
1533 struct wpabuf *buf;
1534 const u8 *end, *pos;
1535
1536 buf = wpabuf_alloc(res->ie_len);
1537 if (buf == NULL)
1538 return NULL;
1539
1540 pos = (const u8 *) (res + 1);
1541 end = pos + res->ie_len;
1542
1543 while (pos + 1 < end) {
1544 if (pos + 2 + pos[1] > end)
1545 break;
1546 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
1547 vendor_type == WPA_GET_BE32(&pos[2]))
1548 wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
1549 pos += 2 + pos[1];
1550 }
1551
1552 if (wpabuf_len(buf) == 0) {
1553 wpabuf_free(buf);
1554 buf = NULL;
1555 }
1556
1557 return buf;
1558}
1559
1560
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001561/*
1562 * Channels with a great SNR can operate at full rate. What is a great SNR?
1563 * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
1564 * rule of thumb is that any SNR above 20 is good." This one
1565 * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
1566 * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
1567 * conservative value.
1568 */
1569#define GREAT_SNR 30
1570
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001571/* Compare function for sorting scan results. Return >0 if @b is considered
1572 * better. */
1573static int wpa_scan_result_compar(const void *a, const void *b)
1574{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001575#define IS_5GHZ(n) (n > 4000)
1576#define MIN(a,b) a < b ? a : b
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001577 struct wpa_scan_res **_wa = (void *) a;
1578 struct wpa_scan_res **_wb = (void *) b;
1579 struct wpa_scan_res *wa = *_wa;
1580 struct wpa_scan_res *wb = *_wb;
1581 int wpa_a, wpa_b, maxrate_a, maxrate_b;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001582 int snr_a, snr_b;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583
1584 /* WPA/WPA2 support preferred */
1585 wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
1586 wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
1587 wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
1588 wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
1589
1590 if (wpa_b && !wpa_a)
1591 return 1;
1592 if (!wpa_b && wpa_a)
1593 return -1;
1594
1595 /* privacy support preferred */
1596 if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
1597 (wb->caps & IEEE80211_CAP_PRIVACY))
1598 return 1;
1599 if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
1600 (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
1601 return -1;
1602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001603 if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
1604 !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
1605 snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
1606 snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
1607 } else {
1608 /* Not suitable information to calculate SNR, so use level */
1609 snr_a = wa->level;
1610 snr_b = wb->level;
1611 }
1612
1613 /* best/max rate preferred if SNR close enough */
1614 if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001615 (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
1616 maxrate_a = wpa_scan_get_max_rate(wa);
1617 maxrate_b = wpa_scan_get_max_rate(wb);
1618 if (maxrate_a != maxrate_b)
1619 return maxrate_b - maxrate_a;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001620 if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
1621 return IS_5GHZ(wa->freq) ? -1 : 1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001622 }
1623
1624 /* use freq for channel preference */
1625
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001626 /* all things being equal, use SNR; if SNRs are
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001627 * identical, use quality values since some drivers may only report
1628 * that value and leave the signal level zero */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001629 if (snr_b == snr_a)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001630 return wb->qual - wa->qual;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001631 return snr_b - snr_a;
1632#undef MIN
1633#undef IS_5GHZ
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001634}
1635
1636
1637#ifdef CONFIG_WPS
1638/* Compare function for sorting scan results when searching a WPS AP for
1639 * provisioning. Return >0 if @b is considered better. */
1640static int wpa_scan_result_wps_compar(const void *a, const void *b)
1641{
1642 struct wpa_scan_res **_wa = (void *) a;
1643 struct wpa_scan_res **_wb = (void *) b;
1644 struct wpa_scan_res *wa = *_wa;
1645 struct wpa_scan_res *wb = *_wb;
1646 int uses_wps_a, uses_wps_b;
1647 struct wpabuf *wps_a, *wps_b;
1648 int res;
1649
1650 /* Optimization - check WPS IE existence before allocated memory and
1651 * doing full reassembly. */
1652 uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
1653 uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
1654 if (uses_wps_a && !uses_wps_b)
1655 return -1;
1656 if (!uses_wps_a && uses_wps_b)
1657 return 1;
1658
1659 if (uses_wps_a && uses_wps_b) {
1660 wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
1661 wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
1662 res = wps_ap_priority_compar(wps_a, wps_b);
1663 wpabuf_free(wps_a);
1664 wpabuf_free(wps_b);
1665 if (res)
1666 return res;
1667 }
1668
1669 /*
1670 * Do not use current AP security policy as a sorting criteria during
1671 * WPS provisioning step since the AP may get reconfigured at the
1672 * completion of provisioning.
1673 */
1674
1675 /* all things being equal, use signal level; if signal levels are
1676 * identical, use quality values since some drivers may only report
1677 * that value and leave the signal level zero */
1678 if (wb->level == wa->level)
1679 return wb->qual - wa->qual;
1680 return wb->level - wa->level;
1681}
1682#endif /* CONFIG_WPS */
1683
1684
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001685static void dump_scan_res(struct wpa_scan_results *scan_res)
1686{
1687#ifndef CONFIG_NO_STDOUT_DEBUG
1688 size_t i;
1689
1690 if (scan_res->res == NULL || scan_res->num == 0)
1691 return;
1692
1693 wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
1694
1695 for (i = 0; i < scan_res->num; i++) {
1696 struct wpa_scan_res *r = scan_res->res[i];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001697 u8 *pos;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001698 if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
1699 == WPA_SCAN_LEVEL_DBM) {
1700 int snr = r->level - r->noise;
1701 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001702 "noise=%d level=%d snr=%d%s flags=0x%x "
1703 "age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001704 MAC2STR(r->bssid), r->freq, r->qual,
1705 r->noise, r->level, snr,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001706 snr >= GREAT_SNR ? "*" : "", r->flags,
1707 r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001708 } else {
1709 wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001710 "noise=%d level=%d flags=0x%x age=%u",
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001711 MAC2STR(r->bssid), r->freq, r->qual,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001712 r->noise, r->level, r->flags, r->age);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001713 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001714 pos = (u8 *) (r + 1);
1715 if (r->ie_len)
1716 wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
1717 pos += r->ie_len;
1718 if (r->beacon_ie_len)
1719 wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
1720 pos, r->beacon_ie_len);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001721 }
1722#endif /* CONFIG_NO_STDOUT_DEBUG */
1723}
1724
1725
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001726/**
1727 * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
1728 * @wpa_s: Pointer to wpa_supplicant data
1729 * @bssid: BSSID to check
1730 * Returns: 0 if the BSSID is filtered or 1 if not
1731 *
1732 * This function is used to filter out specific BSSIDs from scan reslts mainly
1733 * for testing purposes (SET bssid_filter ctrl_iface command).
1734 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07001735int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
1736 const u8 *bssid)
1737{
1738 size_t i;
1739
1740 if (wpa_s->bssid_filter == NULL)
1741 return 1;
1742
1743 for (i = 0; i < wpa_s->bssid_filter_count; i++) {
1744 if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
1745 ETH_ALEN) == 0)
1746 return 1;
1747 }
1748
1749 return 0;
1750}
1751
1752
1753static void filter_scan_res(struct wpa_supplicant *wpa_s,
1754 struct wpa_scan_results *res)
1755{
1756 size_t i, j;
1757
1758 if (wpa_s->bssid_filter == NULL)
1759 return;
1760
1761 for (i = 0, j = 0; i < res->num; i++) {
1762 if (wpa_supplicant_filter_bssid_match(wpa_s,
1763 res->res[i]->bssid)) {
1764 res->res[j++] = res->res[i];
1765 } else {
1766 os_free(res->res[i]);
1767 res->res[i] = NULL;
1768 }
1769 }
1770
1771 if (res->num != j) {
1772 wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
1773 (int) (res->num - j));
1774 res->num = j;
1775 }
1776}
1777
1778
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001779/**
1780 * wpa_supplicant_get_scan_results - Get scan results
1781 * @wpa_s: Pointer to wpa_supplicant data
1782 * @info: Information about what was scanned or %NULL if not available
1783 * @new_scan: Whether a new scan was performed
1784 * Returns: Scan results, %NULL on failure
1785 *
1786 * This function request the current scan results from the driver and updates
1787 * the local BSS list wpa_s->bss. The caller is responsible for freeing the
1788 * results with wpa_scan_results_free().
1789 */
1790struct wpa_scan_results *
1791wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
1792 struct scan_info *info, int new_scan)
1793{
1794 struct wpa_scan_results *scan_res;
1795 size_t i;
1796 int (*compar)(const void *, const void *) = wpa_scan_result_compar;
1797
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001798 scan_res = wpa_drv_get_scan_results2(wpa_s);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001799 if (scan_res == NULL) {
1800 wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
1801 return NULL;
1802 }
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001803 if (scan_res->fetch_time.sec == 0) {
1804 /*
1805 * Make sure we have a valid timestamp if the driver wrapper
1806 * does not set this.
1807 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001808 os_get_reltime(&scan_res->fetch_time);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001809 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001810 filter_scan_res(wpa_s, scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001811
1812#ifdef CONFIG_WPS
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001813 if (wpas_wps_searching(wpa_s)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001814 wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
1815 "provisioning rules");
1816 compar = wpa_scan_result_wps_compar;
1817 }
1818#endif /* CONFIG_WPS */
1819
1820 qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
1821 compar);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001822 dump_scan_res(scan_res);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001823
1824 wpa_bss_update_start(wpa_s);
1825 for (i = 0; i < scan_res->num; i++)
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001826 wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
1827 &scan_res->fetch_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001828 wpa_bss_update_end(wpa_s, info, new_scan);
1829
1830 return scan_res;
1831}
1832
1833
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001834/**
1835 * wpa_supplicant_update_scan_results - Update scan results from the driver
1836 * @wpa_s: Pointer to wpa_supplicant data
1837 * Returns: 0 on success, -1 on failure
1838 *
1839 * This function updates the BSS table within wpa_supplicant based on the
1840 * currently available scan results from the driver without requesting a new
1841 * scan. This is used in cases where the driver indicates an association
1842 * (including roaming within ESS) and wpa_supplicant does not yet have the
1843 * needed information to complete the connection (e.g., to perform validation
1844 * steps in 4-way handshake).
1845 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001846int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
1847{
1848 struct wpa_scan_results *scan_res;
1849 scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
1850 if (scan_res == NULL)
1851 return -1;
1852 wpa_scan_results_free(scan_res);
1853
1854 return 0;
1855}
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001856
1857
1858/**
1859 * scan_only_handler - Reports scan results
1860 */
1861void scan_only_handler(struct wpa_supplicant *wpa_s,
1862 struct wpa_scan_results *scan_res)
1863{
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001864 wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001865 if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
1866 wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
1867 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
1868 wpa_s->manual_scan_id);
1869 wpa_s->manual_scan_use_id = 0;
1870 } else {
1871 wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
1872 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001873 wpas_notify_scan_results(wpa_s);
Dmitry Shmidt4b9d52f2013-02-05 17:44:43 -08001874 wpas_notify_scan_done(wpa_s, 1);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001875 if (wpa_s->scan_work) {
1876 struct wpa_radio_work *work = wpa_s->scan_work;
1877 wpa_s->scan_work = NULL;
1878 radio_work_done(work);
1879 }
Dmitry Shmidt3a787e62013-01-17 10:32:35 -08001880}
Dmitry Shmidt37d4d6a2013-03-18 13:09:42 -07001881
1882
1883int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
1884{
1885 return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
1886}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001887
1888
1889struct wpa_driver_scan_params *
1890wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
1891{
1892 struct wpa_driver_scan_params *params;
1893 size_t i;
1894 u8 *n;
1895
1896 params = os_zalloc(sizeof(*params));
1897 if (params == NULL)
1898 return NULL;
1899
1900 for (i = 0; i < src->num_ssids; i++) {
1901 if (src->ssids[i].ssid) {
1902 n = os_malloc(src->ssids[i].ssid_len);
1903 if (n == NULL)
1904 goto failed;
1905 os_memcpy(n, src->ssids[i].ssid,
1906 src->ssids[i].ssid_len);
1907 params->ssids[i].ssid = n;
1908 params->ssids[i].ssid_len = src->ssids[i].ssid_len;
1909 }
1910 }
1911 params->num_ssids = src->num_ssids;
1912
1913 if (src->extra_ies) {
1914 n = os_malloc(src->extra_ies_len);
1915 if (n == NULL)
1916 goto failed;
1917 os_memcpy(n, src->extra_ies, src->extra_ies_len);
1918 params->extra_ies = n;
1919 params->extra_ies_len = src->extra_ies_len;
1920 }
1921
1922 if (src->freqs) {
1923 int len = int_array_len(src->freqs);
1924 params->freqs = os_malloc((len + 1) * sizeof(int));
1925 if (params->freqs == NULL)
1926 goto failed;
1927 os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
1928 }
1929
1930 if (src->filter_ssids) {
Dmitry Shmidt97672262014-02-03 13:02:54 -08001931 params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001932 src->num_filter_ssids);
1933 if (params->filter_ssids == NULL)
1934 goto failed;
1935 os_memcpy(params->filter_ssids, src->filter_ssids,
Dmitry Shmidt97672262014-02-03 13:02:54 -08001936 sizeof(*params->filter_ssids) *
1937 src->num_filter_ssids);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001938 params->num_filter_ssids = src->num_filter_ssids;
1939 }
1940
1941 params->filter_rssi = src->filter_rssi;
1942 params->p2p_probe = src->p2p_probe;
1943 params->only_new_results = src->only_new_results;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001944 params->low_priority = src->low_priority;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001945
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001946 if (src->mac_addr_rand) {
1947 params->mac_addr_rand = src->mac_addr_rand;
1948
1949 if (src->mac_addr && src->mac_addr_mask) {
1950 u8 *mac_addr;
1951
1952 mac_addr = os_malloc(2 * ETH_ALEN);
1953 if (!mac_addr)
1954 goto failed;
1955
1956 os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
1957 os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
1958 ETH_ALEN);
1959 params->mac_addr = mac_addr;
1960 params->mac_addr_mask = mac_addr + ETH_ALEN;
1961 }
1962 }
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001963 return params;
1964
1965failed:
1966 wpa_scan_free_params(params);
1967 return NULL;
1968}
1969
1970
1971void wpa_scan_free_params(struct wpa_driver_scan_params *params)
1972{
1973 size_t i;
1974
1975 if (params == NULL)
1976 return;
1977
1978 for (i = 0; i < params->num_ssids; i++)
1979 os_free((u8 *) params->ssids[i].ssid);
1980 os_free((u8 *) params->extra_ies);
1981 os_free(params->freqs);
1982 os_free(params->filter_ssids);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001983
1984 /*
1985 * Note: params->mac_addr_mask points to same memory allocation and
1986 * must not be freed separately.
1987 */
1988 os_free((u8 *) params->mac_addr);
1989
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001990 os_free(params);
1991}
Dmitry Shmidt98660862014-03-11 17:26:21 -07001992
1993
1994int wpas_start_pno(struct wpa_supplicant *wpa_s)
1995{
1996 int ret, interval;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07001997 size_t i, num_ssid, num_match_ssid;
Dmitry Shmidt98660862014-03-11 17:26:21 -07001998 struct wpa_ssid *ssid;
1999 struct wpa_driver_scan_params params;
2000
2001 if (!wpa_s->sched_scan_supported)
2002 return -1;
2003
2004 if (wpa_s->pno || wpa_s->pno_sched_pending)
2005 return 0;
2006
2007 if ((wpa_s->wpa_state > WPA_SCANNING) &&
2008 (wpa_s->wpa_state <= WPA_COMPLETED)) {
2009 wpa_printf(MSG_ERROR, "PNO: In assoc process");
2010 return -EAGAIN;
2011 }
2012
2013 if (wpa_s->wpa_state == WPA_SCANNING) {
2014 wpa_supplicant_cancel_scan(wpa_s);
2015 if (wpa_s->sched_scanning) {
2016 wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
2017 "ongoing sched scan");
2018 wpa_supplicant_cancel_sched_scan(wpa_s);
2019 wpa_s->pno_sched_pending = 1;
2020 return 0;
2021 }
2022 }
2023
2024 os_memset(&params, 0, sizeof(params));
2025
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002026 num_ssid = num_match_ssid = 0;
Dmitry Shmidt98660862014-03-11 17:26:21 -07002027 ssid = wpa_s->conf->ssid;
2028 while (ssid) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002029 if (!wpas_network_disabled(wpa_s, ssid)) {
2030 num_match_ssid++;
2031 if (ssid->scan_ssid)
2032 num_ssid++;
2033 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002034 ssid = ssid->next;
2035 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002036
2037 if (num_match_ssid == 0) {
2038 wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
2039 return -1;
2040 }
2041
2042 if (num_match_ssid > num_ssid) {
2043 params.num_ssids++; /* wildcard */
2044 num_ssid++;
2045 }
2046
Dmitry Shmidt98660862014-03-11 17:26:21 -07002047 if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
2048 wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
2049 "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
2050 num_ssid = WPAS_MAX_SCAN_SSIDS;
2051 }
2052
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002053 if (num_match_ssid > wpa_s->max_match_sets) {
2054 num_match_ssid = wpa_s->max_match_sets;
2055 wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
Dmitry Shmidt98660862014-03-11 17:26:21 -07002056 }
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002057 params.filter_ssids = os_calloc(num_match_ssid,
2058 sizeof(struct wpa_driver_scan_filter));
Dmitry Shmidt98660862014-03-11 17:26:21 -07002059 if (params.filter_ssids == NULL)
2060 return -1;
2061 i = 0;
2062 ssid = wpa_s->conf->ssid;
2063 while (ssid) {
2064 if (!wpas_network_disabled(wpa_s, ssid)) {
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002065 if (ssid->scan_ssid && params.num_ssids < num_ssid) {
2066 params.ssids[params.num_ssids].ssid =
2067 ssid->ssid;
2068 params.ssids[params.num_ssids].ssid_len =
2069 ssid->ssid_len;
2070 params.num_ssids++;
2071 }
Dmitry Shmidt98660862014-03-11 17:26:21 -07002072 os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
2073 ssid->ssid_len);
2074 params.filter_ssids[i].ssid_len = ssid->ssid_len;
2075 params.num_filter_ssids++;
2076 i++;
Dmitry Shmidt6aa8ae42014-07-07 09:31:33 -07002077 if (i == num_match_ssid)
Dmitry Shmidt98660862014-03-11 17:26:21 -07002078 break;
2079 }
2080 ssid = ssid->next;
2081 }
2082
2083 if (wpa_s->conf->filter_rssi)
2084 params.filter_rssi = wpa_s->conf->filter_rssi;
2085
2086 interval = wpa_s->conf->sched_scan_interval ?
2087 wpa_s->conf->sched_scan_interval : 10;
2088
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07002089 if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
2090 wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
2091 params.freqs = wpa_s->manual_sched_scan_freqs;
2092 }
2093
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002094 if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
2095 params.mac_addr_rand = 1;
2096 if (wpa_s->mac_addr_pno) {
2097 params.mac_addr = wpa_s->mac_addr_pno;
2098 params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
2099 }
2100 }
2101
Dmitry Shmidt98660862014-03-11 17:26:21 -07002102 ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
2103 os_free(params.filter_ssids);
2104 if (ret == 0)
2105 wpa_s->pno = 1;
2106 else
2107 wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
2108 return ret;
2109}
2110
2111
2112int wpas_stop_pno(struct wpa_supplicant *wpa_s)
2113{
2114 int ret = 0;
2115
2116 if (!wpa_s->pno)
2117 return 0;
2118
2119 ret = wpa_supplicant_stop_sched_scan(wpa_s);
2120
2121 wpa_s->pno = 0;
2122 wpa_s->pno_sched_pending = 0;
2123
2124 if (wpa_s->wpa_state == WPA_SCANNING)
2125 wpa_supplicant_req_scan(wpa_s, 0, 0);
2126
2127 return ret;
2128}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002129
2130
2131void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
2132 unsigned int type)
2133{
2134 type &= MAC_ADDR_RAND_ALL;
2135 wpa_s->mac_addr_rand_enable &= ~type;
2136
2137 if (type & MAC_ADDR_RAND_SCAN) {
2138 os_free(wpa_s->mac_addr_scan);
2139 wpa_s->mac_addr_scan = NULL;
2140 }
2141
2142 if (type & MAC_ADDR_RAND_SCHED_SCAN) {
2143 os_free(wpa_s->mac_addr_sched_scan);
2144 wpa_s->mac_addr_sched_scan = NULL;
2145 }
2146
2147 if (type & MAC_ADDR_RAND_PNO) {
2148 os_free(wpa_s->mac_addr_pno);
2149 wpa_s->mac_addr_pno = NULL;
2150 }
2151}
2152
2153
2154int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
2155 unsigned int type, const u8 *addr,
2156 const u8 *mask)
2157{
2158 u8 *tmp = NULL;
2159
2160 wpas_mac_addr_rand_scan_clear(wpa_s, type);
2161
2162 if (addr) {
2163 tmp = os_malloc(2 * ETH_ALEN);
2164 if (!tmp)
2165 return -1;
2166 os_memcpy(tmp, addr, ETH_ALEN);
2167 os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
2168 }
2169
2170 if (type == MAC_ADDR_RAND_SCAN) {
2171 wpa_s->mac_addr_scan = tmp;
2172 } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
2173 wpa_s->mac_addr_sched_scan = tmp;
2174 } else if (type == MAC_ADDR_RAND_PNO) {
2175 wpa_s->mac_addr_pno = tmp;
2176 } else {
2177 wpa_printf(MSG_INFO,
2178 "scan: Invalid MAC randomization type=0x%x",
2179 type);
2180 os_free(tmp);
2181 return -1;
2182 }
2183
2184 wpa_s->mac_addr_rand_enable |= type;
2185 return 0;
2186}