Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * WPA Supplicant - Scanning |
| 3 | * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi> |
| 4 | * |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 5 | * This software may be distributed under the terms of the BSD license. |
| 6 | * See README for more details. |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "utils/includes.h" |
| 10 | |
| 11 | #include "utils/common.h" |
| 12 | #include "utils/eloop.h" |
| 13 | #include "common/ieee802_11_defs.h" |
| 14 | #include "config.h" |
| 15 | #include "wpa_supplicant_i.h" |
| 16 | #include "driver_i.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 17 | #include "wps_supplicant.h" |
| 18 | #include "p2p_supplicant.h" |
| 19 | #include "p2p/p2p.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 20 | #include "hs20_supplicant.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 21 | #include "notify.h" |
| 22 | #include "bss.h" |
| 23 | #include "scan.h" |
| 24 | |
| 25 | |
| 26 | static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s) |
| 27 | { |
| 28 | struct wpa_ssid *ssid; |
| 29 | union wpa_event_data data; |
| 30 | |
| 31 | ssid = wpa_supplicant_get_ssid(wpa_s); |
| 32 | if (ssid == NULL) |
| 33 | return; |
| 34 | |
| 35 | if (wpa_s->current_ssid == NULL) { |
| 36 | wpa_s->current_ssid = ssid; |
| 37 | if (wpa_s->current_ssid != NULL) |
| 38 | wpas_notify_network_changed(wpa_s); |
| 39 | } |
| 40 | wpa_supplicant_initiate_eapol(wpa_s); |
| 41 | wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured " |
| 42 | "network - generating associated event"); |
| 43 | os_memset(&data, 0, sizeof(data)); |
| 44 | wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | #ifdef CONFIG_WPS |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 49 | static int wpas_wps_in_use(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 50 | enum wps_request_type *req_type) |
| 51 | { |
| 52 | struct wpa_ssid *ssid; |
| 53 | int wps = 0; |
| 54 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 55 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 56 | if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS)) |
| 57 | continue; |
| 58 | |
| 59 | wps = 1; |
| 60 | *req_type = wpas_wps_get_req_type(ssid); |
| 61 | if (!ssid->eap.phase1) |
| 62 | continue; |
| 63 | |
| 64 | if (os_strstr(ssid->eap.phase1, "pbc=1")) |
| 65 | return 2; |
| 66 | } |
| 67 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 68 | #ifdef CONFIG_P2P |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 69 | if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p) { |
| 70 | wpa_s->wps->dev.p2p = 1; |
| 71 | if (!wps) { |
| 72 | wps = 1; |
| 73 | *req_type = WPS_REQ_ENROLLEE_INFO; |
| 74 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 75 | } |
| 76 | #endif /* CONFIG_P2P */ |
| 77 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 78 | return wps; |
| 79 | } |
| 80 | #endif /* CONFIG_WPS */ |
| 81 | |
| 82 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 83 | int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 84 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 85 | struct wpa_ssid *ssid = wpa_s->conf->ssid; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 86 | int count = 0; |
| 87 | while (ssid) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 88 | if (!wpas_network_disabled(wpa_s, ssid)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 89 | count++; |
| 90 | ssid = ssid->next; |
| 91 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 92 | if (wpa_s->conf->cred && wpa_s->conf->interworking && |
| 93 | wpa_s->conf->auto_interworking) |
| 94 | count++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 95 | return count; |
| 96 | } |
| 97 | |
| 98 | |
| 99 | static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s, |
| 100 | struct wpa_ssid *ssid) |
| 101 | { |
| 102 | while (ssid) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 103 | if (!wpas_network_disabled(wpa_s, ssid)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 104 | break; |
| 105 | ssid = ssid->next; |
| 106 | } |
| 107 | |
| 108 | /* ap_scan=2 mode - try to associate with each SSID. */ |
| 109 | if (ssid == NULL) { |
| 110 | wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached " |
| 111 | "end of scan list - go back to beginning"); |
| 112 | wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; |
| 113 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 114 | return; |
| 115 | } |
| 116 | if (ssid->next) { |
| 117 | /* Continue from the next SSID on the next attempt. */ |
| 118 | wpa_s->prev_scan_ssid = ssid; |
| 119 | } else { |
| 120 | /* Start from the beginning of the SSID list. */ |
| 121 | wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; |
| 122 | } |
| 123 | wpa_supplicant_associate(wpa_s, NULL, ssid); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static int int_array_len(const int *a) |
| 128 | { |
| 129 | int i; |
| 130 | for (i = 0; a && a[i]; i++) |
| 131 | ; |
| 132 | return i; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | static void int_array_concat(int **res, const int *a) |
| 137 | { |
| 138 | int reslen, alen, i; |
| 139 | int *n; |
| 140 | |
| 141 | reslen = int_array_len(*res); |
| 142 | alen = int_array_len(a); |
| 143 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 144 | n = os_realloc_array(*res, reslen + alen + 1, sizeof(int)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 145 | if (n == NULL) { |
| 146 | os_free(*res); |
| 147 | *res = NULL; |
| 148 | return; |
| 149 | } |
| 150 | for (i = 0; i <= alen; i++) |
| 151 | n[reslen + i] = a[i]; |
| 152 | *res = n; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | static int freq_cmp(const void *a, const void *b) |
| 157 | { |
| 158 | int _a = *(int *) a; |
| 159 | int _b = *(int *) b; |
| 160 | |
| 161 | if (_a == 0) |
| 162 | return 1; |
| 163 | if (_b == 0) |
| 164 | return -1; |
| 165 | return _a - _b; |
| 166 | } |
| 167 | |
| 168 | |
| 169 | static void int_array_sort_unique(int *a) |
| 170 | { |
| 171 | int alen; |
| 172 | int i, j; |
| 173 | |
| 174 | if (a == NULL) |
| 175 | return; |
| 176 | |
| 177 | alen = int_array_len(a); |
| 178 | qsort(a, alen, sizeof(int), freq_cmp); |
| 179 | |
| 180 | i = 0; |
| 181 | j = 1; |
| 182 | while (a[i] && a[j]) { |
| 183 | if (a[i] == a[j]) { |
| 184 | j++; |
| 185 | continue; |
| 186 | } |
| 187 | a[++i] = a[j++]; |
| 188 | } |
| 189 | if (a[i]) |
| 190 | i++; |
| 191 | a[i] = 0; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s, |
| 196 | struct wpa_driver_scan_params *params) |
| 197 | { |
| 198 | int ret; |
| 199 | |
| 200 | wpa_supplicant_notify_scanning(wpa_s, 1); |
| 201 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 202 | ret = wpa_drv_scan(wpa_s, params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 203 | if (ret) { |
| 204 | wpa_supplicant_notify_scanning(wpa_s, 0); |
| 205 | wpas_notify_scan_done(wpa_s, 0); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 206 | } else { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 207 | wpa_s->scan_runs++; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 208 | wpa_s->normal_scans++; |
| 209 | } |
| 210 | |
| 211 | return ret; |
| 212 | } |
| 213 | |
| 214 | |
| 215 | static void |
| 216 | wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx) |
| 217 | { |
| 218 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 219 | |
| 220 | wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan"); |
| 221 | |
| 222 | if (wpa_supplicant_req_sched_scan(wpa_s)) |
| 223 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | static void |
| 228 | wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx) |
| 229 | { |
| 230 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 231 | |
| 232 | wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it"); |
| 233 | |
| 234 | wpa_s->sched_scan_timed_out = 1; |
| 235 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | static int |
| 240 | wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s, |
| 241 | struct wpa_driver_scan_params *params, |
| 242 | int interval) |
| 243 | { |
| 244 | int ret; |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 245 | #ifndef ANDROID_P2P |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 246 | wpa_supplicant_notify_scanning(wpa_s, 1); |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 247 | #endif |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 248 | ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000); |
| 249 | if (ret) |
| 250 | wpa_supplicant_notify_scanning(wpa_s, 0); |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 251 | else { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 252 | wpa_s->sched_scanning = 1; |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 253 | #ifdef ANDROID_P2P |
| 254 | wpa_supplicant_notify_scanning(wpa_s, 1); |
| 255 | #endif |
| 256 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 257 | |
| 258 | return ret; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s) |
| 263 | { |
| 264 | int ret; |
| 265 | |
| 266 | ret = wpa_drv_stop_sched_scan(wpa_s); |
| 267 | if (ret) { |
| 268 | wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!"); |
| 269 | /* TODO: what to do if stopping fails? */ |
| 270 | return -1; |
| 271 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 272 | |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | |
| 277 | static struct wpa_driver_scan_filter * |
| 278 | wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids) |
| 279 | { |
| 280 | struct wpa_driver_scan_filter *ssids; |
| 281 | struct wpa_ssid *ssid; |
| 282 | size_t count; |
| 283 | |
| 284 | *num_ssids = 0; |
| 285 | if (!conf->filter_ssids) |
| 286 | return NULL; |
| 287 | |
| 288 | for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) { |
| 289 | if (ssid->ssid && ssid->ssid_len) |
| 290 | count++; |
| 291 | } |
| 292 | if (count == 0) |
| 293 | return NULL; |
| 294 | ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter)); |
| 295 | if (ssids == NULL) |
| 296 | return NULL; |
| 297 | |
| 298 | for (ssid = conf->ssid; ssid; ssid = ssid->next) { |
| 299 | if (!ssid->ssid || !ssid->ssid_len) |
| 300 | continue; |
| 301 | os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len); |
| 302 | ssids[*num_ssids].ssid_len = ssid->ssid_len; |
| 303 | (*num_ssids)++; |
| 304 | } |
| 305 | |
| 306 | return ssids; |
| 307 | } |
| 308 | |
| 309 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 310 | static void wpa_supplicant_optimize_freqs( |
| 311 | struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params) |
| 312 | { |
| 313 | #ifdef CONFIG_P2P |
| 314 | if (params->freqs == NULL && wpa_s->p2p_in_provisioning && |
| 315 | wpa_s->go_params) { |
| 316 | /* Optimize provisioning state scan based on GO information */ |
| 317 | if (wpa_s->p2p_in_provisioning < 5 && |
| 318 | wpa_s->go_params->freq > 0) { |
| 319 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO " |
| 320 | "preferred frequency %d MHz", |
| 321 | wpa_s->go_params->freq); |
| 322 | params->freqs = os_zalloc(2 * sizeof(int)); |
| 323 | if (params->freqs) |
| 324 | params->freqs[0] = wpa_s->go_params->freq; |
| 325 | } else if (wpa_s->p2p_in_provisioning < 8 && |
| 326 | wpa_s->go_params->freq_list[0]) { |
| 327 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common " |
| 328 | "channels"); |
| 329 | int_array_concat(¶ms->freqs, |
| 330 | wpa_s->go_params->freq_list); |
| 331 | if (params->freqs) |
| 332 | int_array_sort_unique(params->freqs); |
| 333 | } |
| 334 | wpa_s->p2p_in_provisioning++; |
| 335 | } |
| 336 | #endif /* CONFIG_P2P */ |
| 337 | |
| 338 | #ifdef CONFIG_WPS |
| 339 | if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) { |
| 340 | /* |
| 341 | * Optimize post-provisioning scan based on channel used |
| 342 | * during provisioning. |
| 343 | */ |
| 344 | wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz " |
| 345 | "that was used during provisioning", wpa_s->wps_freq); |
| 346 | params->freqs = os_zalloc(2 * sizeof(int)); |
| 347 | if (params->freqs) |
| 348 | params->freqs[0] = wpa_s->wps_freq; |
| 349 | wpa_s->after_wps--; |
| 350 | } |
| 351 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 352 | if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq) |
| 353 | { |
| 354 | /* Optimize provisioning scan based on already known channel */ |
| 355 | wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz", |
| 356 | wpa_s->wps_freq); |
| 357 | params->freqs = os_zalloc(2 * sizeof(int)); |
| 358 | if (params->freqs) |
| 359 | params->freqs[0] = wpa_s->wps_freq; |
| 360 | wpa_s->known_wps_freq = 0; /* only do this once */ |
| 361 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 362 | #endif /* CONFIG_WPS */ |
| 363 | } |
| 364 | |
| 365 | |
| 366 | #ifdef CONFIG_INTERWORKING |
| 367 | static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s, |
| 368 | struct wpabuf *buf) |
| 369 | { |
| 370 | if (wpa_s->conf->interworking == 0) |
| 371 | return; |
| 372 | |
| 373 | wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB); |
| 374 | wpabuf_put_u8(buf, 4); |
| 375 | wpabuf_put_u8(buf, 0x00); |
| 376 | wpabuf_put_u8(buf, 0x00); |
| 377 | wpabuf_put_u8(buf, 0x00); |
| 378 | wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */ |
| 379 | |
| 380 | wpabuf_put_u8(buf, WLAN_EID_INTERWORKING); |
| 381 | wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 : |
| 382 | 1 + ETH_ALEN); |
| 383 | wpabuf_put_u8(buf, wpa_s->conf->access_network_type); |
| 384 | /* No Venue Info */ |
| 385 | if (!is_zero_ether_addr(wpa_s->conf->hessid)) |
| 386 | wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN); |
| 387 | } |
| 388 | #endif /* CONFIG_INTERWORKING */ |
| 389 | |
| 390 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 391 | static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 392 | { |
| 393 | struct wpabuf *extra_ie = NULL; |
| 394 | #ifdef CONFIG_WPS |
| 395 | int wps = 0; |
| 396 | enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO; |
| 397 | #endif /* CONFIG_WPS */ |
| 398 | |
| 399 | #ifdef CONFIG_INTERWORKING |
| 400 | if (wpa_s->conf->interworking && |
| 401 | wpabuf_resize(&extra_ie, 100) == 0) |
| 402 | wpas_add_interworking_elements(wpa_s, extra_ie); |
| 403 | #endif /* CONFIG_INTERWORKING */ |
| 404 | |
| 405 | #ifdef CONFIG_WPS |
| 406 | wps = wpas_wps_in_use(wpa_s, &req_type); |
| 407 | |
| 408 | if (wps) { |
| 409 | struct wpabuf *wps_ie; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 410 | wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON : |
| 411 | DEV_PW_DEFAULT, |
| 412 | &wpa_s->wps->dev, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 413 | wpa_s->wps->uuid, req_type, |
| 414 | 0, NULL); |
| 415 | if (wps_ie) { |
| 416 | if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0) |
| 417 | wpabuf_put_buf(extra_ie, wps_ie); |
| 418 | wpabuf_free(wps_ie); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | #ifdef CONFIG_P2P |
| 423 | if (wps) { |
| 424 | size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 425 | if (wpabuf_resize(&extra_ie, ielen) == 0) |
| 426 | wpas_p2p_scan_ie(wpa_s, extra_ie); |
| 427 | } |
| 428 | #endif /* CONFIG_P2P */ |
| 429 | |
| 430 | #endif /* CONFIG_WPS */ |
| 431 | |
| 432 | return extra_ie; |
| 433 | } |
| 434 | |
| 435 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 436 | static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx) |
| 437 | { |
| 438 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 439 | struct wpa_ssid *ssid; |
| 440 | int scan_req = 0, ret; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 441 | struct wpabuf *extra_ie = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 442 | struct wpa_driver_scan_params params; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 443 | struct wpa_driver_scan_params *scan_params; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 444 | size_t max_ssids; |
| 445 | enum wpa_states prev_state; |
| 446 | |
| 447 | if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) { |
| 448 | wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled"); |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | if (wpa_s->disconnected && !wpa_s->scan_req) { |
| 453 | wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED); |
| 454 | return; |
| 455 | } |
Dmitry Shmidt | 5887a9d | 2012-09-14 10:47:43 -0700 | [diff] [blame^] | 456 | #ifdef ANDROID |
| 457 | if (wpa_s->scanning) { |
| 458 | /* If we are already in scanning state, we shall ignore this new scan request*/ |
| 459 | wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - already scanning"); |
| 460 | return; |
| 461 | } |
| 462 | #endif |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 463 | if (!wpa_supplicant_enabled_networks(wpa_s) && |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 464 | !wpa_s->scan_req) { |
| 465 | wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan"); |
| 466 | wpa_supplicant_set_state(wpa_s, WPA_INACTIVE); |
| 467 | return; |
| 468 | } |
| 469 | |
| 470 | if (wpa_s->conf->ap_scan != 0 && |
| 471 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) { |
| 472 | wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - " |
| 473 | "overriding ap_scan configuration"); |
| 474 | wpa_s->conf->ap_scan = 0; |
| 475 | wpas_notify_ap_scan_changed(wpa_s); |
| 476 | } |
| 477 | |
| 478 | if (wpa_s->conf->ap_scan == 0) { |
| 479 | wpa_supplicant_gen_assoc_event(wpa_s); |
| 480 | return; |
| 481 | } |
| 482 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 483 | #ifdef CONFIG_P2P |
| 484 | if (wpas_p2p_in_progress(wpa_s)) { |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 485 | if (wpa_s->sta_scan_pending && |
| 486 | wpas_p2p_in_progress(wpa_s) == 2 && |
| 487 | wpa_s->p2p_cb_on_scan_complete) { |
| 488 | wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station " |
| 489 | "mode scan during P2P search"); |
| 490 | } else { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 491 | wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan " |
| 492 | "while P2P operation is in progress"); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 493 | wpa_s->sta_scan_pending = 1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 494 | wpa_supplicant_req_scan(wpa_s, 5, 0); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 495 | return; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 496 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 497 | } |
| 498 | #endif /* CONFIG_P2P */ |
| 499 | |
| 500 | if (wpa_s->conf->ap_scan == 2) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 501 | max_ssids = 1; |
| 502 | else { |
| 503 | max_ssids = wpa_s->max_scan_ssids; |
| 504 | if (max_ssids > WPAS_MAX_SCAN_SSIDS) |
| 505 | max_ssids = WPAS_MAX_SCAN_SSIDS; |
| 506 | } |
| 507 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 508 | scan_req = wpa_s->scan_req; |
| 509 | wpa_s->scan_req = 0; |
| 510 | |
| 511 | os_memset(¶ms, 0, sizeof(params)); |
| 512 | |
| 513 | prev_state = wpa_s->wpa_state; |
| 514 | if (wpa_s->wpa_state == WPA_DISCONNECTED || |
| 515 | wpa_s->wpa_state == WPA_INACTIVE) |
| 516 | wpa_supplicant_set_state(wpa_s, WPA_SCANNING); |
| 517 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 518 | /* |
| 519 | * If autoscan has set its own scanning parameters |
| 520 | */ |
| 521 | if (wpa_s->autoscan_params != NULL) { |
| 522 | scan_params = wpa_s->autoscan_params; |
| 523 | goto scan; |
| 524 | } |
| 525 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 526 | if (scan_req != 2 && wpa_s->connect_without_scan) { |
| 527 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
| 528 | if (ssid == wpa_s->connect_without_scan) |
| 529 | break; |
| 530 | } |
| 531 | wpa_s->connect_without_scan = NULL; |
| 532 | if (ssid) { |
| 533 | wpa_printf(MSG_DEBUG, "Start a pre-selected network " |
| 534 | "without scan step"); |
| 535 | wpa_supplicant_associate(wpa_s, NULL, ssid); |
| 536 | return; |
| 537 | } |
| 538 | } |
| 539 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 540 | #ifdef CONFIG_P2P |
| 541 | if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) && |
| 542 | wpa_s->go_params) { |
| 543 | wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during " |
| 544 | "P2P group formation"); |
| 545 | params.ssids[0].ssid = wpa_s->go_params->ssid; |
| 546 | params.ssids[0].ssid_len = wpa_s->go_params->ssid_len; |
| 547 | params.num_ssids = 1; |
| 548 | goto ssid_list_set; |
| 549 | } |
| 550 | #endif /* CONFIG_P2P */ |
| 551 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 552 | /* Find the starting point from which to continue scanning */ |
| 553 | ssid = wpa_s->conf->ssid; |
| 554 | if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) { |
| 555 | while (ssid) { |
| 556 | if (ssid == wpa_s->prev_scan_ssid) { |
| 557 | ssid = ssid->next; |
| 558 | break; |
| 559 | } |
| 560 | ssid = ssid->next; |
| 561 | } |
| 562 | } |
| 563 | |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 564 | if (scan_req != 2 && wpa_s->conf->ap_scan == 2) { |
| 565 | wpa_s->connect_without_scan = NULL; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 566 | wpa_s->prev_scan_wildcard = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 567 | wpa_supplicant_assoc_try(wpa_s, ssid); |
| 568 | return; |
Dmitry Shmidt | e25ba15 | 2011-08-22 15:04:04 -0700 | [diff] [blame] | 569 | #ifndef ANDROID |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 570 | } else if (wpa_s->conf->ap_scan == 2) { |
| 571 | /* |
| 572 | * User-initiated scan request in ap_scan == 2; scan with |
| 573 | * wildcard SSID. |
| 574 | */ |
| 575 | ssid = NULL; |
Dmitry Shmidt | e25ba15 | 2011-08-22 15:04:04 -0700 | [diff] [blame] | 576 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 577 | } else { |
| 578 | struct wpa_ssid *start = ssid, *tssid; |
| 579 | int freqs_set = 0; |
| 580 | if (ssid == NULL && max_ssids > 1) |
| 581 | ssid = wpa_s->conf->ssid; |
| 582 | while (ssid) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 583 | if (!wpas_network_disabled(wpa_s, ssid) && |
| 584 | ssid->scan_ssid) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 585 | wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID", |
| 586 | ssid->ssid, ssid->ssid_len); |
| 587 | params.ssids[params.num_ssids].ssid = |
| 588 | ssid->ssid; |
| 589 | params.ssids[params.num_ssids].ssid_len = |
| 590 | ssid->ssid_len; |
| 591 | params.num_ssids++; |
| 592 | if (params.num_ssids + 1 >= max_ssids) |
| 593 | break; |
| 594 | } |
| 595 | ssid = ssid->next; |
| 596 | if (ssid == start) |
| 597 | break; |
| 598 | if (ssid == NULL && max_ssids > 1 && |
| 599 | start != wpa_s->conf->ssid) |
| 600 | ssid = wpa_s->conf->ssid; |
| 601 | } |
| 602 | |
| 603 | for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 604 | if (wpas_network_disabled(wpa_s, tssid)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 605 | continue; |
| 606 | if ((params.freqs || !freqs_set) && tssid->scan_freq) { |
| 607 | int_array_concat(¶ms.freqs, |
| 608 | tssid->scan_freq); |
| 609 | } else { |
| 610 | os_free(params.freqs); |
| 611 | params.freqs = NULL; |
| 612 | } |
| 613 | freqs_set = 1; |
| 614 | } |
| 615 | int_array_sort_unique(params.freqs); |
| 616 | } |
| 617 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 618 | if (ssid && max_ssids == 1) { |
| 619 | /* |
| 620 | * If the driver is limited to 1 SSID at a time interleave |
| 621 | * wildcard SSID scans with specific SSID scans to avoid |
| 622 | * waiting a long time for a wildcard scan. |
| 623 | */ |
| 624 | if (!wpa_s->prev_scan_wildcard) { |
| 625 | params.ssids[0].ssid = NULL; |
| 626 | params.ssids[0].ssid_len = 0; |
| 627 | wpa_s->prev_scan_wildcard = 1; |
| 628 | wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for " |
| 629 | "wildcard SSID (Interleave with specific)"); |
| 630 | } else { |
| 631 | wpa_s->prev_scan_ssid = ssid; |
| 632 | wpa_s->prev_scan_wildcard = 0; |
| 633 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 634 | "Starting AP scan for specific SSID: %s", |
| 635 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 636 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 637 | } else if (ssid) { |
| 638 | /* max_ssids > 1 */ |
| 639 | |
| 640 | wpa_s->prev_scan_ssid = ssid; |
| 641 | wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in " |
| 642 | "the scan request"); |
| 643 | params.num_ssids++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 644 | } else { |
| 645 | wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; |
| 646 | params.num_ssids++; |
| 647 | wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard " |
| 648 | "SSID"); |
| 649 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 650 | #ifdef CONFIG_P2P |
| 651 | ssid_list_set: |
| 652 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 653 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 654 | wpa_supplicant_optimize_freqs(wpa_s, ¶ms); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 655 | extra_ie = wpa_supplicant_extra_ies(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 656 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 657 | #ifdef CONFIG_HS20 |
| 658 | if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0) |
| 659 | wpas_hs20_add_indication(extra_ie); |
| 660 | #endif /* CONFIG_HS20 */ |
| 661 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 662 | if (params.freqs == NULL && wpa_s->next_scan_freqs) { |
| 663 | wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously " |
| 664 | "generated frequency list"); |
| 665 | params.freqs = wpa_s->next_scan_freqs; |
| 666 | } else |
| 667 | os_free(wpa_s->next_scan_freqs); |
| 668 | wpa_s->next_scan_freqs = NULL; |
| 669 | |
| 670 | params.filter_ssids = wpa_supplicant_build_filter_ssids( |
| 671 | wpa_s->conf, ¶ms.num_filter_ssids); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 672 | if (extra_ie) { |
| 673 | params.extra_ies = wpabuf_head(extra_ie); |
| 674 | params.extra_ies_len = wpabuf_len(extra_ie); |
| 675 | } |
| 676 | |
| 677 | #ifdef CONFIG_P2P |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 678 | if (wpa_s->p2p_in_provisioning || |
| 679 | (wpa_s->show_group_started && wpa_s->go_params)) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 680 | /* |
| 681 | * The interface may not yet be in P2P mode, so we have to |
| 682 | * explicitly request P2P probe to disable CCK rates. |
| 683 | */ |
| 684 | params.p2p_probe = 1; |
| 685 | } |
| 686 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 687 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 688 | scan_params = ¶ms; |
| 689 | |
| 690 | scan: |
| 691 | ret = wpa_supplicant_trigger_scan(wpa_s, scan_params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 692 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 693 | wpabuf_free(extra_ie); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 694 | os_free(params.freqs); |
| 695 | os_free(params.filter_ssids); |
| 696 | |
| 697 | if (ret) { |
| 698 | wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan"); |
Dmitry Shmidt | f4f5db3 | 2012-09-11 14:36:56 -0700 | [diff] [blame] | 699 | #ifdef ANDROID_P2P |
Dmitry Shmidt | 9bce59c | 2012-09-11 15:06:38 -0700 | [diff] [blame] | 700 | /* Restore back the wpa_s->scan_req if we failed the scan because of any reason */ |
Dmitry Shmidt | f4f5db3 | 2012-09-11 14:36:56 -0700 | [diff] [blame] | 701 | wpa_msg(wpa_s, MSG_DEBUG, "Restoring back the wpa_s->scan_req " |
| 702 | "to the original value %d", scan_req); |
| 703 | wpa_s->scan_req = scan_req; |
| 704 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 705 | if (prev_state != wpa_s->wpa_state) |
| 706 | wpa_supplicant_set_state(wpa_s, prev_state); |
| 707 | wpa_supplicant_req_scan(wpa_s, 1, 0); |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | |
| 712 | /** |
| 713 | * wpa_supplicant_req_scan - Schedule a scan for neighboring access points |
| 714 | * @wpa_s: Pointer to wpa_supplicant data |
| 715 | * @sec: Number of seconds after which to scan |
| 716 | * @usec: Number of microseconds after which to scan |
| 717 | * |
| 718 | * This function is used to schedule a scan for neighboring access points after |
| 719 | * the specified time. |
| 720 | */ |
| 721 | void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec) |
| 722 | { |
Dmitry Shmidt | 81e9700 | 2011-11-14 15:53:31 -0800 | [diff] [blame] | 723 | #ifndef ANDROID |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 724 | /* If there's at least one network that should be specifically scanned |
| 725 | * then don't cancel the scan and reschedule. Some drivers do |
| 726 | * background scanning which generates frequent scan results, and that |
| 727 | * causes the specific SSID scan to get continually pushed back and |
| 728 | * never happen, which causes hidden APs to never get probe-scanned. |
| 729 | */ |
| 730 | if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) && |
| 731 | wpa_s->conf->ap_scan == 1) { |
| 732 | struct wpa_ssid *ssid = wpa_s->conf->ssid; |
| 733 | |
| 734 | while (ssid) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 735 | if (!wpas_network_disabled(wpa_s, ssid) && |
| 736 | ssid->scan_ssid) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 737 | break; |
| 738 | ssid = ssid->next; |
| 739 | } |
| 740 | if (ssid) { |
| 741 | wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to " |
| 742 | "ensure that specific SSID scans occur"); |
| 743 | return; |
| 744 | } |
| 745 | } |
Dmitry Shmidt | 81e9700 | 2011-11-14 15:53:31 -0800 | [diff] [blame] | 746 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 747 | wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec", |
| 748 | sec, usec); |
| 749 | eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL); |
| 750 | eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL); |
| 751 | } |
| 752 | |
| 753 | |
| 754 | /** |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 755 | * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan |
| 756 | * @wpa_s: Pointer to wpa_supplicant data |
| 757 | * @sec: Number of seconds after which to scan |
| 758 | * @usec: Number of microseconds after which to scan |
| 759 | * |
| 760 | * This function is used to schedule periodic scans for neighboring |
| 761 | * access points after the specified time. |
| 762 | */ |
| 763 | int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s, |
| 764 | int sec, int usec) |
| 765 | { |
| 766 | if (!wpa_s->sched_scan_supported) |
| 767 | return -1; |
| 768 | |
| 769 | eloop_register_timeout(sec, usec, |
| 770 | wpa_supplicant_delayed_sched_scan_timeout, |
| 771 | wpa_s, NULL); |
| 772 | |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | |
| 777 | /** |
| 778 | * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan |
| 779 | * @wpa_s: Pointer to wpa_supplicant data |
| 780 | * |
| 781 | * This function is used to schedule periodic scans for neighboring |
| 782 | * access points repeating the scan continuously. |
| 783 | */ |
| 784 | int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s) |
| 785 | { |
| 786 | struct wpa_driver_scan_params params; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 787 | struct wpa_driver_scan_params *scan_params; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 788 | enum wpa_states prev_state; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 789 | struct wpa_ssid *ssid = NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 790 | struct wpabuf *extra_ie = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 791 | int ret; |
| 792 | unsigned int max_sched_scan_ssids; |
| 793 | int wildcard = 0; |
| 794 | int need_ssids; |
| 795 | |
| 796 | if (!wpa_s->sched_scan_supported) |
| 797 | return -1; |
| 798 | |
| 799 | if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS) |
| 800 | max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS; |
| 801 | else |
| 802 | max_sched_scan_ssids = wpa_s->max_sched_scan_ssids; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 803 | if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 804 | return -1; |
| 805 | |
| 806 | if (wpa_s->sched_scanning) { |
| 807 | wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning"); |
| 808 | return 0; |
| 809 | } |
| 810 | |
| 811 | need_ssids = 0; |
| 812 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 813 | if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 814 | /* Use wildcard SSID to find this network */ |
| 815 | wildcard = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 816 | } else if (!wpas_network_disabled(wpa_s, ssid) && |
| 817 | ssid->ssid_len) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 818 | need_ssids++; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 819 | |
| 820 | #ifdef CONFIG_WPS |
| 821 | if (!wpas_network_disabled(wpa_s, ssid) && |
| 822 | ssid->key_mgmt == WPA_KEY_MGMT_WPS) { |
| 823 | /* |
| 824 | * Normal scan is more reliable and faster for WPS |
| 825 | * operations and since these are for short periods of |
| 826 | * time, the benefit of trying to use sched_scan would |
| 827 | * be limited. |
| 828 | */ |
| 829 | wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of " |
| 830 | "sched_scan for WPS"); |
| 831 | return -1; |
| 832 | } |
| 833 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 834 | } |
| 835 | if (wildcard) |
| 836 | need_ssids++; |
| 837 | |
| 838 | if (wpa_s->normal_scans < 3 && |
| 839 | (need_ssids <= wpa_s->max_scan_ssids || |
| 840 | wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) { |
| 841 | /* |
| 842 | * When normal scan can speed up operations, use that for the |
| 843 | * first operations before starting the sched_scan to allow |
| 844 | * user space sleep more. We do this only if the normal scan |
| 845 | * has functionality that is suitable for this or if the |
| 846 | * sched_scan does not have better support for multiple SSIDs. |
| 847 | */ |
| 848 | wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of " |
| 849 | "sched_scan for initial scans (normal_scans=%d)", |
| 850 | wpa_s->normal_scans); |
| 851 | return -1; |
| 852 | } |
| 853 | |
| 854 | os_memset(¶ms, 0, sizeof(params)); |
| 855 | |
| 856 | /* If we can't allocate space for the filters, we just don't filter */ |
| 857 | params.filter_ssids = os_zalloc(wpa_s->max_match_sets * |
| 858 | sizeof(struct wpa_driver_scan_filter)); |
| 859 | |
| 860 | prev_state = wpa_s->wpa_state; |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 861 | #ifndef ANDROID_P2P |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 862 | if (wpa_s->wpa_state == WPA_DISCONNECTED || |
| 863 | wpa_s->wpa_state == WPA_INACTIVE) |
| 864 | wpa_supplicant_set_state(wpa_s, WPA_SCANNING); |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 865 | #endif |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 866 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 867 | if (wpa_s->autoscan_params != NULL) { |
| 868 | scan_params = wpa_s->autoscan_params; |
| 869 | goto scan; |
| 870 | } |
| 871 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 872 | /* Find the starting point from which to continue scanning */ |
| 873 | ssid = wpa_s->conf->ssid; |
| 874 | if (wpa_s->prev_sched_ssid) { |
| 875 | while (ssid) { |
| 876 | if (ssid == wpa_s->prev_sched_ssid) { |
| 877 | ssid = ssid->next; |
| 878 | break; |
| 879 | } |
| 880 | ssid = ssid->next; |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | if (!ssid || !wpa_s->prev_sched_ssid) { |
| 885 | wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list"); |
| 886 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 887 | if (wpa_s->sched_scan_interval == 0) |
| 888 | wpa_s->sched_scan_interval = 10; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 889 | wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2; |
| 890 | wpa_s->first_sched_scan = 1; |
| 891 | ssid = wpa_s->conf->ssid; |
| 892 | wpa_s->prev_sched_ssid = ssid; |
| 893 | } |
| 894 | |
| 895 | if (wildcard) { |
| 896 | wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan"); |
| 897 | params.num_ssids++; |
| 898 | } |
| 899 | |
| 900 | while (ssid) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 901 | if (wpas_network_disabled(wpa_s, ssid)) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 902 | goto next; |
| 903 | |
| 904 | if (params.num_filter_ssids < wpa_s->max_match_sets && |
| 905 | params.filter_ssids && ssid->ssid && ssid->ssid_len) { |
| 906 | wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s", |
| 907 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
| 908 | os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid, |
| 909 | ssid->ssid, ssid->ssid_len); |
| 910 | params.filter_ssids[params.num_filter_ssids].ssid_len = |
| 911 | ssid->ssid_len; |
| 912 | params.num_filter_ssids++; |
| 913 | } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len) |
| 914 | { |
| 915 | wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID " |
| 916 | "filter for sched_scan - drop filter"); |
| 917 | os_free(params.filter_ssids); |
| 918 | params.filter_ssids = NULL; |
| 919 | params.num_filter_ssids = 0; |
| 920 | } |
| 921 | |
| 922 | if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) { |
| 923 | if (params.num_ssids == max_sched_scan_ssids) |
| 924 | break; /* only room for broadcast SSID */ |
| 925 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 926 | "add to active scan ssid: %s", |
| 927 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
| 928 | params.ssids[params.num_ssids].ssid = |
| 929 | ssid->ssid; |
| 930 | params.ssids[params.num_ssids].ssid_len = |
| 931 | ssid->ssid_len; |
| 932 | params.num_ssids++; |
| 933 | if (params.num_ssids >= max_sched_scan_ssids) { |
| 934 | wpa_s->prev_sched_ssid = ssid; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 935 | do { |
| 936 | ssid = ssid->next; |
| 937 | } while (ssid && |
| 938 | (wpas_network_disabled(wpa_s, ssid) || |
| 939 | !ssid->scan_ssid)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 940 | break; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | next: |
| 945 | wpa_s->prev_sched_ssid = ssid; |
| 946 | ssid = ssid->next; |
| 947 | } |
| 948 | |
| 949 | if (params.num_filter_ssids == 0) { |
| 950 | os_free(params.filter_ssids); |
| 951 | params.filter_ssids = NULL; |
| 952 | } |
| 953 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 954 | extra_ie = wpa_supplicant_extra_ies(wpa_s); |
| 955 | if (extra_ie) { |
| 956 | params.extra_ies = wpabuf_head(extra_ie); |
| 957 | params.extra_ies_len = wpabuf_len(extra_ie); |
| 958 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 959 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 960 | scan_params = ¶ms; |
| 961 | |
| 962 | scan: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 963 | if (ssid || !wpa_s->first_sched_scan) { |
| 964 | wpa_dbg(wpa_s, MSG_DEBUG, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 965 | "Starting sched scan: interval %d timeout %d", |
| 966 | wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 967 | } else { |
| 968 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 969 | "Starting sched scan: interval %d (no timeout)", |
| 970 | wpa_s->sched_scan_interval); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 971 | } |
| 972 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 973 | ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 974 | wpa_s->sched_scan_interval); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 975 | wpabuf_free(extra_ie); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 976 | os_free(params.filter_ssids); |
| 977 | if (ret) { |
| 978 | wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan"); |
| 979 | if (prev_state != wpa_s->wpa_state) |
| 980 | wpa_supplicant_set_state(wpa_s, prev_state); |
| 981 | return ret; |
| 982 | } |
| 983 | |
| 984 | /* If we have more SSIDs to scan, add a timeout so we scan them too */ |
| 985 | if (ssid || !wpa_s->first_sched_scan) { |
| 986 | wpa_s->sched_scan_timed_out = 0; |
| 987 | eloop_register_timeout(wpa_s->sched_scan_timeout, 0, |
| 988 | wpa_supplicant_sched_scan_timeout, |
| 989 | wpa_s, NULL); |
| 990 | wpa_s->first_sched_scan = 0; |
| 991 | wpa_s->sched_scan_timeout /= 2; |
| 992 | wpa_s->sched_scan_interval *= 2; |
| 993 | } |
| 994 | |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | |
| 999 | /** |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1000 | * wpa_supplicant_cancel_scan - Cancel a scheduled scan request |
| 1001 | * @wpa_s: Pointer to wpa_supplicant data |
| 1002 | * |
| 1003 | * This function is used to cancel a scan request scheduled with |
| 1004 | * wpa_supplicant_req_scan(). |
| 1005 | */ |
| 1006 | void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s) |
| 1007 | { |
| 1008 | wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request"); |
| 1009 | eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1010 | #ifdef ANDROID |
Dmitry Shmidt | 20df807 | 2011-04-08 15:35:17 -0700 | [diff] [blame] | 1011 | wpa_supplicant_notify_scanning(wpa_s, 0); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1012 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1016 | /** |
| 1017 | * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans |
| 1018 | * @wpa_s: Pointer to wpa_supplicant data |
| 1019 | * |
| 1020 | * This function is used to stop a periodic scheduled scan. |
| 1021 | */ |
| 1022 | void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s) |
| 1023 | { |
| 1024 | if (!wpa_s->sched_scanning) |
| 1025 | return; |
| 1026 | |
| 1027 | wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan"); |
| 1028 | eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL); |
| 1029 | wpa_supplicant_stop_sched_scan(wpa_s); |
| 1030 | } |
| 1031 | |
| 1032 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1033 | void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s, |
| 1034 | int scanning) |
| 1035 | { |
| 1036 | if (wpa_s->scanning != scanning) { |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 1037 | #ifdef ANDROID_P2P |
| 1038 | if(!wpa_s->sched_scanning) |
| 1039 | wpa_s->scanning = scanning; |
| 1040 | #else |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1041 | wpa_s->scanning = scanning; |
Dmitry Shmidt | 687922c | 2012-03-26 14:02:32 -0700 | [diff] [blame] | 1042 | #endif |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1043 | wpas_notify_scanning(wpa_s); |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | static int wpa_scan_get_max_rate(const struct wpa_scan_res *res) |
| 1049 | { |
| 1050 | int rate = 0; |
| 1051 | const u8 *ie; |
| 1052 | int i; |
| 1053 | |
| 1054 | ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES); |
| 1055 | for (i = 0; ie && i < ie[1]; i++) { |
| 1056 | if ((ie[i + 2] & 0x7f) > rate) |
| 1057 | rate = ie[i + 2] & 0x7f; |
| 1058 | } |
| 1059 | |
| 1060 | ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES); |
| 1061 | for (i = 0; ie && i < ie[1]; i++) { |
| 1062 | if ((ie[i + 2] & 0x7f) > rate) |
| 1063 | rate = ie[i + 2] & 0x7f; |
| 1064 | } |
| 1065 | |
| 1066 | return rate; |
| 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie) |
| 1071 | { |
| 1072 | const u8 *end, *pos; |
| 1073 | |
| 1074 | pos = (const u8 *) (res + 1); |
| 1075 | end = pos + res->ie_len; |
| 1076 | |
| 1077 | while (pos + 1 < end) { |
| 1078 | if (pos + 2 + pos[1] > end) |
| 1079 | break; |
| 1080 | if (pos[0] == ie) |
| 1081 | return pos; |
| 1082 | pos += 2 + pos[1]; |
| 1083 | } |
| 1084 | |
| 1085 | return NULL; |
| 1086 | } |
| 1087 | |
| 1088 | |
| 1089 | const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res, |
| 1090 | u32 vendor_type) |
| 1091 | { |
| 1092 | const u8 *end, *pos; |
| 1093 | |
| 1094 | pos = (const u8 *) (res + 1); |
| 1095 | end = pos + res->ie_len; |
| 1096 | |
| 1097 | while (pos + 1 < end) { |
| 1098 | if (pos + 2 + pos[1] > end) |
| 1099 | break; |
| 1100 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && |
| 1101 | vendor_type == WPA_GET_BE32(&pos[2])) |
| 1102 | return pos; |
| 1103 | pos += 2 + pos[1]; |
| 1104 | } |
| 1105 | |
| 1106 | return NULL; |
| 1107 | } |
| 1108 | |
| 1109 | |
| 1110 | struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res, |
| 1111 | u32 vendor_type) |
| 1112 | { |
| 1113 | struct wpabuf *buf; |
| 1114 | const u8 *end, *pos; |
| 1115 | |
| 1116 | buf = wpabuf_alloc(res->ie_len); |
| 1117 | if (buf == NULL) |
| 1118 | return NULL; |
| 1119 | |
| 1120 | pos = (const u8 *) (res + 1); |
| 1121 | end = pos + res->ie_len; |
| 1122 | |
| 1123 | while (pos + 1 < end) { |
| 1124 | if (pos + 2 + pos[1] > end) |
| 1125 | break; |
| 1126 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && |
| 1127 | vendor_type == WPA_GET_BE32(&pos[2])) |
| 1128 | wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4); |
| 1129 | pos += 2 + pos[1]; |
| 1130 | } |
| 1131 | |
| 1132 | if (wpabuf_len(buf) == 0) { |
| 1133 | wpabuf_free(buf); |
| 1134 | buf = NULL; |
| 1135 | } |
| 1136 | |
| 1137 | return buf; |
| 1138 | } |
| 1139 | |
| 1140 | |
| 1141 | struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon( |
| 1142 | const struct wpa_scan_res *res, u32 vendor_type) |
| 1143 | { |
| 1144 | struct wpabuf *buf; |
| 1145 | const u8 *end, *pos; |
| 1146 | |
| 1147 | if (res->beacon_ie_len == 0) |
| 1148 | return NULL; |
| 1149 | buf = wpabuf_alloc(res->beacon_ie_len); |
| 1150 | if (buf == NULL) |
| 1151 | return NULL; |
| 1152 | |
| 1153 | pos = (const u8 *) (res + 1); |
| 1154 | pos += res->ie_len; |
| 1155 | end = pos + res->beacon_ie_len; |
| 1156 | |
| 1157 | while (pos + 1 < end) { |
| 1158 | if (pos + 2 + pos[1] > end) |
| 1159 | break; |
| 1160 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && |
| 1161 | vendor_type == WPA_GET_BE32(&pos[2])) |
| 1162 | wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4); |
| 1163 | pos += 2 + pos[1]; |
| 1164 | } |
| 1165 | |
| 1166 | if (wpabuf_len(buf) == 0) { |
| 1167 | wpabuf_free(buf); |
| 1168 | buf = NULL; |
| 1169 | } |
| 1170 | |
| 1171 | return buf; |
| 1172 | } |
| 1173 | |
| 1174 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1175 | /* |
| 1176 | * Channels with a great SNR can operate at full rate. What is a great SNR? |
| 1177 | * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general |
| 1178 | * rule of thumb is that any SNR above 20 is good." This one |
| 1179 | * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23 |
| 1180 | * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a |
| 1181 | * conservative value. |
| 1182 | */ |
| 1183 | #define GREAT_SNR 30 |
| 1184 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1185 | /* Compare function for sorting scan results. Return >0 if @b is considered |
| 1186 | * better. */ |
| 1187 | static int wpa_scan_result_compar(const void *a, const void *b) |
| 1188 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1189 | #define IS_5GHZ(n) (n > 4000) |
| 1190 | #define MIN(a,b) a < b ? a : b |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1191 | struct wpa_scan_res **_wa = (void *) a; |
| 1192 | struct wpa_scan_res **_wb = (void *) b; |
| 1193 | struct wpa_scan_res *wa = *_wa; |
| 1194 | struct wpa_scan_res *wb = *_wb; |
| 1195 | int wpa_a, wpa_b, maxrate_a, maxrate_b; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1196 | int snr_a, snr_b; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1197 | |
| 1198 | /* WPA/WPA2 support preferred */ |
| 1199 | wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL || |
| 1200 | wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL; |
| 1201 | wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL || |
| 1202 | wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL; |
| 1203 | |
| 1204 | if (wpa_b && !wpa_a) |
| 1205 | return 1; |
| 1206 | if (!wpa_b && wpa_a) |
| 1207 | return -1; |
| 1208 | |
| 1209 | /* privacy support preferred */ |
| 1210 | if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 && |
| 1211 | (wb->caps & IEEE80211_CAP_PRIVACY)) |
| 1212 | return 1; |
| 1213 | if ((wa->caps & IEEE80211_CAP_PRIVACY) && |
| 1214 | (wb->caps & IEEE80211_CAP_PRIVACY) == 0) |
| 1215 | return -1; |
| 1216 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1217 | if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) && |
| 1218 | !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) { |
| 1219 | snr_a = MIN(wa->level - wa->noise, GREAT_SNR); |
| 1220 | snr_b = MIN(wb->level - wb->noise, GREAT_SNR); |
| 1221 | } else { |
| 1222 | /* Not suitable information to calculate SNR, so use level */ |
| 1223 | snr_a = wa->level; |
| 1224 | snr_b = wb->level; |
| 1225 | } |
| 1226 | |
| 1227 | /* best/max rate preferred if SNR close enough */ |
| 1228 | if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) || |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1229 | (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) { |
| 1230 | maxrate_a = wpa_scan_get_max_rate(wa); |
| 1231 | maxrate_b = wpa_scan_get_max_rate(wb); |
| 1232 | if (maxrate_a != maxrate_b) |
| 1233 | return maxrate_b - maxrate_a; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1234 | if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq)) |
| 1235 | return IS_5GHZ(wa->freq) ? -1 : 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | /* use freq for channel preference */ |
| 1239 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1240 | /* all things being equal, use SNR; if SNRs are |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1241 | * identical, use quality values since some drivers may only report |
| 1242 | * that value and leave the signal level zero */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1243 | if (snr_b == snr_a) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1244 | return wb->qual - wa->qual; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1245 | return snr_b - snr_a; |
| 1246 | #undef MIN |
| 1247 | #undef IS_5GHZ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | |
| 1251 | #ifdef CONFIG_WPS |
| 1252 | /* Compare function for sorting scan results when searching a WPS AP for |
| 1253 | * provisioning. Return >0 if @b is considered better. */ |
| 1254 | static int wpa_scan_result_wps_compar(const void *a, const void *b) |
| 1255 | { |
| 1256 | struct wpa_scan_res **_wa = (void *) a; |
| 1257 | struct wpa_scan_res **_wb = (void *) b; |
| 1258 | struct wpa_scan_res *wa = *_wa; |
| 1259 | struct wpa_scan_res *wb = *_wb; |
| 1260 | int uses_wps_a, uses_wps_b; |
| 1261 | struct wpabuf *wps_a, *wps_b; |
| 1262 | int res; |
| 1263 | |
| 1264 | /* Optimization - check WPS IE existence before allocated memory and |
| 1265 | * doing full reassembly. */ |
| 1266 | uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL; |
| 1267 | uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL; |
| 1268 | if (uses_wps_a && !uses_wps_b) |
| 1269 | return -1; |
| 1270 | if (!uses_wps_a && uses_wps_b) |
| 1271 | return 1; |
| 1272 | |
| 1273 | if (uses_wps_a && uses_wps_b) { |
| 1274 | wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE); |
| 1275 | wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE); |
| 1276 | res = wps_ap_priority_compar(wps_a, wps_b); |
| 1277 | wpabuf_free(wps_a); |
| 1278 | wpabuf_free(wps_b); |
| 1279 | if (res) |
| 1280 | return res; |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | * Do not use current AP security policy as a sorting criteria during |
| 1285 | * WPS provisioning step since the AP may get reconfigured at the |
| 1286 | * completion of provisioning. |
| 1287 | */ |
| 1288 | |
| 1289 | /* all things being equal, use signal level; if signal levels are |
| 1290 | * identical, use quality values since some drivers may only report |
| 1291 | * that value and leave the signal level zero */ |
| 1292 | if (wb->level == wa->level) |
| 1293 | return wb->qual - wa->qual; |
| 1294 | return wb->level - wa->level; |
| 1295 | } |
| 1296 | #endif /* CONFIG_WPS */ |
| 1297 | |
| 1298 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1299 | static void dump_scan_res(struct wpa_scan_results *scan_res) |
| 1300 | { |
| 1301 | #ifndef CONFIG_NO_STDOUT_DEBUG |
| 1302 | size_t i; |
| 1303 | |
| 1304 | if (scan_res->res == NULL || scan_res->num == 0) |
| 1305 | return; |
| 1306 | |
| 1307 | wpa_printf(MSG_EXCESSIVE, "Sorted scan results"); |
| 1308 | |
| 1309 | for (i = 0; i < scan_res->num; i++) { |
| 1310 | struct wpa_scan_res *r = scan_res->res[i]; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1311 | u8 *pos; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1312 | if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID)) |
| 1313 | == WPA_SCAN_LEVEL_DBM) { |
| 1314 | int snr = r->level - r->noise; |
| 1315 | wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d " |
| 1316 | "noise=%d level=%d snr=%d%s flags=0x%x", |
| 1317 | MAC2STR(r->bssid), r->freq, r->qual, |
| 1318 | r->noise, r->level, snr, |
| 1319 | snr >= GREAT_SNR ? "*" : "", r->flags); |
| 1320 | } else { |
| 1321 | wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d " |
| 1322 | "noise=%d level=%d flags=0x%x", |
| 1323 | MAC2STR(r->bssid), r->freq, r->qual, |
| 1324 | r->noise, r->level, r->flags); |
| 1325 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1326 | pos = (u8 *) (r + 1); |
| 1327 | if (r->ie_len) |
| 1328 | wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len); |
| 1329 | pos += r->ie_len; |
| 1330 | if (r->beacon_ie_len) |
| 1331 | wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs", |
| 1332 | pos, r->beacon_ie_len); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1333 | } |
| 1334 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
| 1335 | } |
| 1336 | |
| 1337 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1338 | int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s, |
| 1339 | const u8 *bssid) |
| 1340 | { |
| 1341 | size_t i; |
| 1342 | |
| 1343 | if (wpa_s->bssid_filter == NULL) |
| 1344 | return 1; |
| 1345 | |
| 1346 | for (i = 0; i < wpa_s->bssid_filter_count; i++) { |
| 1347 | if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid, |
| 1348 | ETH_ALEN) == 0) |
| 1349 | return 1; |
| 1350 | } |
| 1351 | |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | |
| 1356 | static void filter_scan_res(struct wpa_supplicant *wpa_s, |
| 1357 | struct wpa_scan_results *res) |
| 1358 | { |
| 1359 | size_t i, j; |
| 1360 | |
| 1361 | if (wpa_s->bssid_filter == NULL) |
| 1362 | return; |
| 1363 | |
| 1364 | for (i = 0, j = 0; i < res->num; i++) { |
| 1365 | if (wpa_supplicant_filter_bssid_match(wpa_s, |
| 1366 | res->res[i]->bssid)) { |
| 1367 | res->res[j++] = res->res[i]; |
| 1368 | } else { |
| 1369 | os_free(res->res[i]); |
| 1370 | res->res[i] = NULL; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | if (res->num != j) { |
| 1375 | wpa_printf(MSG_DEBUG, "Filtered out %d scan results", |
| 1376 | (int) (res->num - j)); |
| 1377 | res->num = j; |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1382 | /** |
| 1383 | * wpa_supplicant_get_scan_results - Get scan results |
| 1384 | * @wpa_s: Pointer to wpa_supplicant data |
| 1385 | * @info: Information about what was scanned or %NULL if not available |
| 1386 | * @new_scan: Whether a new scan was performed |
| 1387 | * Returns: Scan results, %NULL on failure |
| 1388 | * |
| 1389 | * This function request the current scan results from the driver and updates |
| 1390 | * the local BSS list wpa_s->bss. The caller is responsible for freeing the |
| 1391 | * results with wpa_scan_results_free(). |
| 1392 | */ |
| 1393 | struct wpa_scan_results * |
| 1394 | wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s, |
| 1395 | struct scan_info *info, int new_scan) |
| 1396 | { |
| 1397 | struct wpa_scan_results *scan_res; |
| 1398 | size_t i; |
| 1399 | int (*compar)(const void *, const void *) = wpa_scan_result_compar; |
| 1400 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1401 | scan_res = wpa_drv_get_scan_results2(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1402 | if (scan_res == NULL) { |
| 1403 | wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results"); |
| 1404 | return NULL; |
| 1405 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1406 | filter_scan_res(wpa_s, scan_res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1407 | |
| 1408 | #ifdef CONFIG_WPS |
| 1409 | if (wpas_wps_in_progress(wpa_s)) { |
| 1410 | wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS " |
| 1411 | "provisioning rules"); |
| 1412 | compar = wpa_scan_result_wps_compar; |
| 1413 | } |
| 1414 | #endif /* CONFIG_WPS */ |
| 1415 | |
| 1416 | qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *), |
| 1417 | compar); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1418 | dump_scan_res(scan_res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1419 | |
| 1420 | wpa_bss_update_start(wpa_s); |
| 1421 | for (i = 0; i < scan_res->num; i++) |
| 1422 | wpa_bss_update_scan_res(wpa_s, scan_res->res[i]); |
| 1423 | wpa_bss_update_end(wpa_s, info, new_scan); |
| 1424 | |
| 1425 | return scan_res; |
| 1426 | } |
| 1427 | |
| 1428 | |
| 1429 | int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s) |
| 1430 | { |
| 1431 | struct wpa_scan_results *scan_res; |
| 1432 | scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0); |
| 1433 | if (scan_res == NULL) |
| 1434 | return -1; |
| 1435 | wpa_scan_results_free(scan_res); |
| 1436 | |
| 1437 | return 0; |
| 1438 | } |