Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * WPA Supplicant - Scanning |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 3 | * Copyright (c) 2003-2014, Jouni Malinen <j@w1.fi> |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 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" |
Dmitry Shmidt | 3a787e6 | 2013-01-17 10:32:35 -0800 | [diff] [blame] | 14 | #include "common/wpa_ctrl.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 15 | #include "config.h" |
| 16 | #include "wpa_supplicant_i.h" |
| 17 | #include "driver_i.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 18 | #include "wps_supplicant.h" |
| 19 | #include "p2p_supplicant.h" |
| 20 | #include "p2p/p2p.h" |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 21 | #include "hs20_supplicant.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 22 | #include "notify.h" |
| 23 | #include "bss.h" |
| 24 | #include "scan.h" |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 25 | #include "mesh.h" |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 26 | |
| 27 | |
| 28 | static 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; |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 39 | wpas_notify_network_changed(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 40 | } |
| 41 | wpa_supplicant_initiate_eapol(wpa_s); |
| 42 | wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured " |
| 43 | "network - generating associated event"); |
| 44 | os_memset(&data, 0, sizeof(data)); |
| 45 | wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | #ifdef CONFIG_WPS |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 50 | static int wpas_wps_in_use(struct wpa_supplicant *wpa_s, |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 51 | enum wps_request_type *req_type) |
| 52 | { |
| 53 | struct wpa_ssid *ssid; |
| 54 | int wps = 0; |
| 55 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 56 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 57 | if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS)) |
| 58 | continue; |
| 59 | |
| 60 | wps = 1; |
| 61 | *req_type = wpas_wps_get_req_type(ssid); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 62 | if (ssid->eap.phase1 && os_strstr(ssid->eap.phase1, "pbc=1")) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 63 | return 2; |
| 64 | } |
| 65 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 66 | #ifdef CONFIG_P2P |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 67 | if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p && |
| 68 | !wpa_s->conf->p2p_disabled) { |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 69 | wpa_s->wps->dev.p2p = 1; |
| 70 | if (!wps) { |
| 71 | wps = 1; |
| 72 | *req_type = WPS_REQ_ENROLLEE_INFO; |
| 73 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 74 | } |
| 75 | #endif /* CONFIG_P2P */ |
| 76 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 77 | return wps; |
| 78 | } |
| 79 | #endif /* CONFIG_WPS */ |
| 80 | |
| 81 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 82 | /** |
| 83 | * wpa_supplicant_enabled_networks - Check whether there are enabled networks |
| 84 | * @wpa_s: Pointer to wpa_supplicant data |
| 85 | * Returns: 0 if no networks are enabled, >0 if networks are enabled |
| 86 | * |
| 87 | * This function is used to figure out whether any networks (or Interworking |
| 88 | * with enabled credentials and auto_interworking) are present in the current |
| 89 | * configuration. |
| 90 | */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 91 | int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 92 | { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 93 | struct wpa_ssid *ssid = wpa_s->conf->ssid; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 94 | int count = 0, disabled = 0; |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 95 | |
| 96 | if (wpa_s->p2p_mgmt) |
| 97 | return 0; /* no normal network profiles on p2p_mgmt interface */ |
| 98 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 99 | while (ssid) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 100 | if (!wpas_network_disabled(wpa_s, ssid)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 101 | count++; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 102 | else |
| 103 | disabled++; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 104 | ssid = ssid->next; |
| 105 | } |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 106 | if (wpa_s->conf->cred && wpa_s->conf->interworking && |
| 107 | wpa_s->conf->auto_interworking) |
| 108 | count++; |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 109 | if (count == 0 && disabled > 0) { |
| 110 | wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled " |
| 111 | "networks)", disabled); |
| 112 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 113 | return count; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s, |
| 118 | struct wpa_ssid *ssid) |
| 119 | { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 120 | int min_temp_disabled = 0; |
| 121 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 122 | while (ssid) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 123 | if (!wpas_network_disabled(wpa_s, ssid)) { |
| 124 | int temp_disabled = wpas_temp_disabled(wpa_s, ssid); |
| 125 | |
| 126 | if (temp_disabled <= 0) |
| 127 | break; |
| 128 | |
| 129 | if (!min_temp_disabled || |
| 130 | temp_disabled < min_temp_disabled) |
| 131 | min_temp_disabled = temp_disabled; |
| 132 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 133 | ssid = ssid->next; |
| 134 | } |
| 135 | |
| 136 | /* ap_scan=2 mode - try to associate with each SSID. */ |
| 137 | if (ssid == NULL) { |
| 138 | wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached " |
| 139 | "end of scan list - go back to beginning"); |
| 140 | wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 141 | wpa_supplicant_req_scan(wpa_s, min_temp_disabled, 0); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 142 | return; |
| 143 | } |
| 144 | if (ssid->next) { |
| 145 | /* Continue from the next SSID on the next attempt. */ |
| 146 | wpa_s->prev_scan_ssid = ssid; |
| 147 | } else { |
| 148 | /* Start from the beginning of the SSID list. */ |
| 149 | wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; |
| 150 | } |
| 151 | wpa_supplicant_associate(wpa_s, NULL, ssid); |
| 152 | } |
| 153 | |
| 154 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 155 | static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 156 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 157 | struct wpa_supplicant *wpa_s = work->wpa_s; |
| 158 | struct wpa_driver_scan_params *params = work->ctx; |
| 159 | int ret; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 160 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 161 | if (deinit) { |
Dmitry Shmidt | bd14a57 | 2014-02-18 10:33:49 -0800 | [diff] [blame] | 162 | if (!work->started) { |
| 163 | wpa_scan_free_params(params); |
| 164 | return; |
| 165 | } |
| 166 | wpa_supplicant_notify_scanning(wpa_s, 0); |
| 167 | wpas_notify_scan_done(wpa_s, 0); |
| 168 | wpa_s->scan_work = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 169 | return; |
| 170 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 171 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 172 | if (wpas_update_random_addr_disassoc(wpa_s) < 0) { |
| 173 | wpa_msg(wpa_s, MSG_INFO, |
| 174 | "Failed to assign random MAC address for a scan"); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 175 | wpa_scan_free_params(params); |
| 176 | wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1"); |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 177 | radio_work_done(work); |
| 178 | return; |
| 179 | } |
| 180 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 181 | wpa_supplicant_notify_scanning(wpa_s, 1); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 182 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 183 | if (wpa_s->clear_driver_scan_cache) { |
| 184 | wpa_printf(MSG_DEBUG, |
| 185 | "Request driver to clear scan cache due to local BSS flush"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 186 | params->only_new_results = 1; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 187 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 188 | ret = wpa_drv_scan(wpa_s, params); |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 189 | /* |
| 190 | * Store the obtained vendor scan cookie (if any) in wpa_s context. |
| 191 | * The current design is to allow only one scan request on each |
| 192 | * interface, hence having this scan cookie stored in wpa_s context is |
| 193 | * fine for now. |
| 194 | * |
| 195 | * Revisit this logic if concurrent scan operations per interface |
| 196 | * is supported. |
| 197 | */ |
| 198 | if (ret == 0) |
| 199 | wpa_s->curr_scan_cookie = params->scan_cookie; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 200 | wpa_scan_free_params(params); |
| 201 | work->ctx = NULL; |
| 202 | if (ret) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 203 | int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ && |
| 204 | !wpa_s->beacon_rep_data.token; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 205 | |
| 206 | if (wpa_s->disconnected) |
| 207 | retry = 0; |
| 208 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 209 | wpa_supplicant_notify_scanning(wpa_s, 0); |
| 210 | wpas_notify_scan_done(wpa_s, 0); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 211 | if (wpa_s->wpa_state == WPA_SCANNING) |
| 212 | wpa_supplicant_set_state(wpa_s, |
| 213 | wpa_s->scan_prev_wpa_state); |
| 214 | wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s", |
| 215 | ret, retry ? " retry=1" : ""); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 216 | radio_work_done(work); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 217 | |
| 218 | if (retry) { |
| 219 | /* Restore scan_req since we will try to scan again */ |
| 220 | wpa_s->scan_req = wpa_s->last_scan_req; |
| 221 | wpa_supplicant_req_scan(wpa_s, 1, 0); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 222 | } else if (wpa_s->scan_res_handler) { |
| 223 | /* Clear the scan_res_handler */ |
| 224 | wpa_s->scan_res_handler = NULL; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 225 | } |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 226 | |
| 227 | if (wpa_s->beacon_rep_data.token) |
| 228 | wpas_rrm_refuse_request(wpa_s); |
| 229 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 230 | return; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 231 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 232 | |
| 233 | os_get_reltime(&wpa_s->scan_trigger_time); |
| 234 | wpa_s->scan_runs++; |
| 235 | wpa_s->normal_scans++; |
| 236 | wpa_s->own_scan_requested = 1; |
| 237 | wpa_s->clear_driver_scan_cache = 0; |
| 238 | wpa_s->scan_work = work; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 242 | /** |
| 243 | * wpa_supplicant_trigger_scan - Request driver to start a scan |
| 244 | * @wpa_s: Pointer to wpa_supplicant data |
| 245 | * @params: Scan parameters |
| 246 | * Returns: 0 on success, -1 on failure |
| 247 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 248 | int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s, |
| 249 | struct wpa_driver_scan_params *params) |
| 250 | { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 251 | struct wpa_driver_scan_params *ctx; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 252 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 253 | if (wpa_s->scan_work) { |
| 254 | wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending"); |
| 255 | return -1; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 256 | } |
| 257 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 258 | ctx = wpa_scan_clone_params(params); |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 259 | if (!ctx || |
| 260 | radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0) |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 261 | { |
| 262 | wpa_scan_free_params(ctx); |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 263 | wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=-1"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | return 0; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | |
| 271 | static void |
| 272 | wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx) |
| 273 | { |
| 274 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 275 | |
| 276 | wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan"); |
| 277 | |
| 278 | if (wpa_supplicant_req_sched_scan(wpa_s)) |
| 279 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 280 | } |
| 281 | |
| 282 | |
| 283 | static void |
| 284 | wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx) |
| 285 | { |
| 286 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 287 | |
| 288 | wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it"); |
| 289 | |
| 290 | wpa_s->sched_scan_timed_out = 1; |
| 291 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 292 | } |
| 293 | |
| 294 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 295 | static int |
| 296 | wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s, |
| 297 | struct wpa_driver_scan_params *params) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 298 | { |
| 299 | int ret; |
Dmitry Shmidt | b7b4d0e | 2013-08-26 12:09:05 -0700 | [diff] [blame] | 300 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 301 | wpa_supplicant_notify_scanning(wpa_s, 1); |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 302 | ret = wpa_drv_sched_scan(wpa_s, params); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 303 | if (ret) |
| 304 | wpa_supplicant_notify_scanning(wpa_s, 0); |
Dmitry Shmidt | b96dad4 | 2013-11-05 10:07:29 -0800 | [diff] [blame] | 305 | else |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 306 | wpa_s->sched_scanning = 1; |
| 307 | |
| 308 | return ret; |
| 309 | } |
| 310 | |
| 311 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 312 | static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 313 | { |
| 314 | int ret; |
| 315 | |
| 316 | ret = wpa_drv_stop_sched_scan(wpa_s); |
| 317 | if (ret) { |
| 318 | wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!"); |
| 319 | /* TODO: what to do if stopping fails? */ |
| 320 | return -1; |
| 321 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | static struct wpa_driver_scan_filter * |
| 328 | wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids) |
| 329 | { |
| 330 | struct wpa_driver_scan_filter *ssids; |
| 331 | struct wpa_ssid *ssid; |
| 332 | size_t count; |
| 333 | |
| 334 | *num_ssids = 0; |
| 335 | if (!conf->filter_ssids) |
| 336 | return NULL; |
| 337 | |
| 338 | for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) { |
| 339 | if (ssid->ssid && ssid->ssid_len) |
| 340 | count++; |
| 341 | } |
| 342 | if (count == 0) |
| 343 | return NULL; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 344 | ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 345 | if (ssids == NULL) |
| 346 | return NULL; |
| 347 | |
| 348 | for (ssid = conf->ssid; ssid; ssid = ssid->next) { |
| 349 | if (!ssid->ssid || !ssid->ssid_len) |
| 350 | continue; |
| 351 | os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len); |
| 352 | ssids[*num_ssids].ssid_len = ssid->ssid_len; |
| 353 | (*num_ssids)++; |
| 354 | } |
| 355 | |
| 356 | return ssids; |
| 357 | } |
| 358 | |
| 359 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 360 | static void wpa_supplicant_optimize_freqs( |
| 361 | struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params) |
| 362 | { |
| 363 | #ifdef CONFIG_P2P |
| 364 | if (params->freqs == NULL && wpa_s->p2p_in_provisioning && |
| 365 | wpa_s->go_params) { |
| 366 | /* Optimize provisioning state scan based on GO information */ |
| 367 | if (wpa_s->p2p_in_provisioning < 5 && |
| 368 | wpa_s->go_params->freq > 0) { |
| 369 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO " |
| 370 | "preferred frequency %d MHz", |
| 371 | wpa_s->go_params->freq); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 372 | params->freqs = os_calloc(2, sizeof(int)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 373 | if (params->freqs) |
| 374 | params->freqs[0] = wpa_s->go_params->freq; |
| 375 | } else if (wpa_s->p2p_in_provisioning < 8 && |
| 376 | wpa_s->go_params->freq_list[0]) { |
| 377 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common " |
| 378 | "channels"); |
| 379 | int_array_concat(¶ms->freqs, |
| 380 | wpa_s->go_params->freq_list); |
| 381 | if (params->freqs) |
| 382 | int_array_sort_unique(params->freqs); |
| 383 | } |
| 384 | wpa_s->p2p_in_provisioning++; |
| 385 | } |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 386 | |
| 387 | if (params->freqs == NULL && wpa_s->p2p_in_invitation) { |
| 388 | /* |
| 389 | * Optimize scan based on GO information during persistent |
| 390 | * group reinvocation |
| 391 | */ |
Dmitry Shmidt | 6dc03bd | 2014-05-16 10:40:13 -0700 | [diff] [blame] | 392 | if (wpa_s->p2p_in_invitation < 5 && |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 393 | wpa_s->p2p_invite_go_freq > 0) { |
| 394 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation", |
| 395 | wpa_s->p2p_invite_go_freq); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 396 | params->freqs = os_calloc(2, sizeof(int)); |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 397 | if (params->freqs) |
| 398 | params->freqs[0] = wpa_s->p2p_invite_go_freq; |
| 399 | } |
| 400 | wpa_s->p2p_in_invitation++; |
| 401 | if (wpa_s->p2p_in_invitation > 20) { |
| 402 | /* |
| 403 | * This should not really happen since the variable is |
| 404 | * cleared on group removal, but if it does happen, make |
| 405 | * sure we do not get stuck in special invitation scan |
| 406 | * mode. |
| 407 | */ |
| 408 | wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation"); |
| 409 | wpa_s->p2p_in_invitation = 0; |
| 410 | } |
| 411 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 412 | #endif /* CONFIG_P2P */ |
| 413 | |
| 414 | #ifdef CONFIG_WPS |
| 415 | if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) { |
| 416 | /* |
| 417 | * Optimize post-provisioning scan based on channel used |
| 418 | * during provisioning. |
| 419 | */ |
| 420 | wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz " |
| 421 | "that was used during provisioning", wpa_s->wps_freq); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 422 | params->freqs = os_calloc(2, sizeof(int)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 423 | if (params->freqs) |
| 424 | params->freqs[0] = wpa_s->wps_freq; |
| 425 | wpa_s->after_wps--; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 426 | } else if (wpa_s->after_wps) |
| 427 | wpa_s->after_wps--; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 428 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 429 | if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq) |
| 430 | { |
| 431 | /* Optimize provisioning scan based on already known channel */ |
| 432 | wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz", |
| 433 | wpa_s->wps_freq); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 434 | params->freqs = os_calloc(2, sizeof(int)); |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 435 | if (params->freqs) |
| 436 | params->freqs[0] = wpa_s->wps_freq; |
| 437 | wpa_s->known_wps_freq = 0; /* only do this once */ |
| 438 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 439 | #endif /* CONFIG_WPS */ |
| 440 | } |
| 441 | |
| 442 | |
| 443 | #ifdef CONFIG_INTERWORKING |
| 444 | static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s, |
| 445 | struct wpabuf *buf) |
| 446 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 447 | wpabuf_put_u8(buf, WLAN_EID_INTERWORKING); |
| 448 | wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 : |
| 449 | 1 + ETH_ALEN); |
| 450 | wpabuf_put_u8(buf, wpa_s->conf->access_network_type); |
| 451 | /* No Venue Info */ |
| 452 | if (!is_zero_ether_addr(wpa_s->conf->hessid)) |
| 453 | wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN); |
| 454 | } |
| 455 | #endif /* CONFIG_INTERWORKING */ |
| 456 | |
| 457 | |
Hai Shalom | ce48b4a | 2018-09-05 11:41:35 -0700 | [diff] [blame] | 458 | #ifdef CONFIG_MBO |
| 459 | static void wpas_fils_req_param_add_max_channel(struct wpa_supplicant *wpa_s, |
| 460 | struct wpabuf **ie) |
| 461 | { |
| 462 | if (wpabuf_resize(ie, 5)) { |
| 463 | wpa_printf(MSG_DEBUG, |
| 464 | "Failed to allocate space for FILS Request Parameters element"); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | /* FILS Request Parameters element */ |
| 469 | wpabuf_put_u8(*ie, WLAN_EID_EXTENSION); |
| 470 | wpabuf_put_u8(*ie, 3); /* FILS Request attribute length */ |
| 471 | wpabuf_put_u8(*ie, WLAN_EID_EXT_FILS_REQ_PARAMS); |
| 472 | /* Parameter control bitmap */ |
| 473 | wpabuf_put_u8(*ie, 0); |
| 474 | /* Max Channel Time field - contains the value of MaxChannelTime |
| 475 | * parameter of the MLME-SCAN.request primitive represented in units of |
| 476 | * TUs, as an unsigned integer. A Max Channel Time field value of 255 |
| 477 | * is used to indicate any duration of more than 254 TUs, or an |
| 478 | * unspecified or unknown duration. (IEEE Std 802.11ai-2016, 9.4.2.178) |
| 479 | */ |
| 480 | wpabuf_put_u8(*ie, 255); |
| 481 | } |
| 482 | #endif /* CONFIG_MBO */ |
| 483 | |
| 484 | |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 485 | void wpa_supplicant_set_default_scan_ies(struct wpa_supplicant *wpa_s) |
| 486 | { |
| 487 | struct wpabuf *default_ies = NULL; |
| 488 | u8 ext_capab[18]; |
| 489 | int ext_capab_len; |
| 490 | enum wpa_driver_if_type type = WPA_IF_STATION; |
| 491 | |
| 492 | #ifdef CONFIG_P2P |
| 493 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) |
| 494 | type = WPA_IF_P2P_CLIENT; |
| 495 | #endif /* CONFIG_P2P */ |
| 496 | |
| 497 | wpa_drv_get_ext_capa(wpa_s, type); |
| 498 | |
| 499 | ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab, |
| 500 | sizeof(ext_capab)); |
| 501 | if (ext_capab_len > 0 && |
| 502 | wpabuf_resize(&default_ies, ext_capab_len) == 0) |
| 503 | wpabuf_put_data(default_ies, ext_capab, ext_capab_len); |
| 504 | |
| 505 | #ifdef CONFIG_MBO |
Hai Shalom | ce48b4a | 2018-09-05 11:41:35 -0700 | [diff] [blame] | 506 | if (wpa_s->enable_oce & OCE_STA) |
| 507 | wpas_fils_req_param_add_max_channel(wpa_s, &default_ies); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 508 | /* Send MBO and OCE capabilities */ |
| 509 | if (wpabuf_resize(&default_ies, 12) == 0) |
Dmitry Shmidt | 7f2c753 | 2016-08-15 09:48:12 -0700 | [diff] [blame] | 510 | wpas_mbo_scan_ie(wpa_s, default_ies); |
| 511 | #endif /* CONFIG_MBO */ |
| 512 | |
| 513 | if (default_ies) |
| 514 | wpa_drv_set_default_scan_ies(wpa_s, wpabuf_head(default_ies), |
| 515 | wpabuf_len(default_ies)); |
| 516 | wpabuf_free(default_ies); |
| 517 | } |
| 518 | |
| 519 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 520 | static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 521 | { |
| 522 | struct wpabuf *extra_ie = NULL; |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 523 | u8 ext_capab[18]; |
| 524 | int ext_capab_len; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 525 | #ifdef CONFIG_WPS |
| 526 | int wps = 0; |
| 527 | enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO; |
| 528 | #endif /* CONFIG_WPS */ |
| 529 | |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 530 | #ifdef CONFIG_P2P |
| 531 | if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) |
| 532 | wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT); |
| 533 | else |
| 534 | #endif /* CONFIG_P2P */ |
| 535 | wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION); |
| 536 | |
Dmitry Shmidt | dda10c2 | 2015-03-24 16:05:01 -0700 | [diff] [blame] | 537 | ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab, |
| 538 | sizeof(ext_capab)); |
| 539 | if (ext_capab_len > 0 && |
| 540 | wpabuf_resize(&extra_ie, ext_capab_len) == 0) |
| 541 | wpabuf_put_data(extra_ie, ext_capab, ext_capab_len); |
| 542 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 543 | #ifdef CONFIG_INTERWORKING |
| 544 | if (wpa_s->conf->interworking && |
| 545 | wpabuf_resize(&extra_ie, 100) == 0) |
| 546 | wpas_add_interworking_elements(wpa_s, extra_ie); |
| 547 | #endif /* CONFIG_INTERWORKING */ |
| 548 | |
Hai Shalom | ce48b4a | 2018-09-05 11:41:35 -0700 | [diff] [blame] | 549 | #ifdef CONFIG_MBO |
| 550 | if (wpa_s->enable_oce & OCE_STA) |
| 551 | wpas_fils_req_param_add_max_channel(wpa_s, &extra_ie); |
| 552 | #endif /* CONFIG_MBO */ |
| 553 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 554 | #ifdef CONFIG_WPS |
| 555 | wps = wpas_wps_in_use(wpa_s, &req_type); |
| 556 | |
| 557 | if (wps) { |
| 558 | struct wpabuf *wps_ie; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 559 | wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON : |
| 560 | DEV_PW_DEFAULT, |
| 561 | &wpa_s->wps->dev, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 562 | wpa_s->wps->uuid, req_type, |
| 563 | 0, NULL); |
| 564 | if (wps_ie) { |
| 565 | if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0) |
| 566 | wpabuf_put_buf(extra_ie, wps_ie); |
| 567 | wpabuf_free(wps_ie); |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | #ifdef CONFIG_P2P |
| 572 | if (wps) { |
| 573 | size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p); |
| 574 | if (wpabuf_resize(&extra_ie, ielen) == 0) |
| 575 | wpas_p2p_scan_ie(wpa_s, extra_ie); |
| 576 | } |
| 577 | #endif /* CONFIG_P2P */ |
| 578 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 579 | wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie); |
| 580 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 581 | #endif /* CONFIG_WPS */ |
| 582 | |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 583 | #ifdef CONFIG_HS20 |
| 584 | if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0) |
Dmitry Shmidt | f21452a | 2014-02-26 10:55:25 -0800 | [diff] [blame] | 585 | wpas_hs20_add_indication(extra_ie, -1); |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 586 | #endif /* CONFIG_HS20 */ |
| 587 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 588 | #ifdef CONFIG_FST |
| 589 | if (wpa_s->fst_ies && |
| 590 | wpabuf_resize(&extra_ie, wpabuf_len(wpa_s->fst_ies)) == 0) |
| 591 | wpabuf_put_buf(extra_ie, wpa_s->fst_ies); |
| 592 | #endif /* CONFIG_FST */ |
| 593 | |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 594 | #ifdef CONFIG_MBO |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 595 | /* Send MBO and OCE capabilities */ |
| 596 | if (wpabuf_resize(&extra_ie, 12) == 0) |
Dmitry Shmidt | 57c2d39 | 2016-02-23 13:40:19 -0800 | [diff] [blame] | 597 | wpas_mbo_scan_ie(wpa_s, extra_ie); |
| 598 | #endif /* CONFIG_MBO */ |
| 599 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 600 | if (wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]) { |
| 601 | struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_PROBE_REQ]; |
| 602 | |
| 603 | if (wpabuf_resize(&extra_ie, wpabuf_len(buf)) == 0) |
| 604 | wpabuf_put_buf(extra_ie, buf); |
| 605 | } |
| 606 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 607 | return extra_ie; |
| 608 | } |
| 609 | |
| 610 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 611 | #ifdef CONFIG_P2P |
| 612 | |
| 613 | /* |
| 614 | * Check whether there are any enabled networks or credentials that could be |
| 615 | * used for a non-P2P connection. |
| 616 | */ |
| 617 | static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s) |
| 618 | { |
| 619 | struct wpa_ssid *ssid; |
| 620 | |
| 621 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
| 622 | if (wpas_network_disabled(wpa_s, ssid)) |
| 623 | continue; |
| 624 | if (!ssid->p2p_group) |
| 625 | return 1; |
| 626 | } |
| 627 | |
| 628 | if (wpa_s->conf->cred && wpa_s->conf->interworking && |
| 629 | wpa_s->conf->auto_interworking) |
| 630 | return 1; |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 635 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 636 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 637 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 638 | static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s, |
| 639 | enum hostapd_hw_mode band, |
| 640 | struct wpa_driver_scan_params *params) |
| 641 | { |
| 642 | /* Include only supported channels for the specified band */ |
| 643 | struct hostapd_hw_modes *mode; |
| 644 | int count, i; |
| 645 | |
| 646 | mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band); |
| 647 | if (mode == NULL) { |
| 648 | /* No channels supported in this band - use empty list */ |
| 649 | params->freqs = os_zalloc(sizeof(int)); |
| 650 | return; |
| 651 | } |
| 652 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 653 | params->freqs = os_calloc(mode->num_channels + 1, sizeof(int)); |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 654 | if (params->freqs == NULL) |
| 655 | return; |
| 656 | for (count = 0, i = 0; i < mode->num_channels; i++) { |
| 657 | if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED) |
| 658 | continue; |
| 659 | params->freqs[count++] = mode->channels[i].freq; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | |
| 664 | static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s, |
| 665 | struct wpa_driver_scan_params *params) |
| 666 | { |
| 667 | if (wpa_s->hw.modes == NULL) |
| 668 | return; /* unknown what channels the driver supports */ |
| 669 | if (params->freqs) |
| 670 | return; /* already using a limited channel set */ |
| 671 | if (wpa_s->setband == WPA_SETBAND_5G) |
| 672 | wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A, |
| 673 | params); |
| 674 | else if (wpa_s->setband == WPA_SETBAND_2G) |
| 675 | wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G, |
| 676 | params); |
| 677 | } |
| 678 | |
| 679 | |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 680 | static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s, |
| 681 | struct wpa_driver_scan_params *params, |
| 682 | size_t max_ssids) |
| 683 | { |
| 684 | unsigned int i; |
| 685 | struct wpa_ssid *ssid; |
| 686 | |
Dmitry Shmidt | 58d12ad | 2016-07-28 10:07:03 -0700 | [diff] [blame] | 687 | /* |
| 688 | * For devices with max_ssids greater than 1, leave the last slot empty |
| 689 | * for adding the wildcard scan entry. |
| 690 | */ |
| 691 | max_ssids = max_ssids > 1 ? max_ssids - 1 : max_ssids; |
| 692 | |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 693 | for (i = 0; i < wpa_s->scan_id_count; i++) { |
| 694 | unsigned int j; |
| 695 | |
| 696 | ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]); |
| 697 | if (!ssid || !ssid->scan_ssid) |
| 698 | continue; |
| 699 | |
| 700 | for (j = 0; j < params->num_ssids; j++) { |
| 701 | if (params->ssids[j].ssid_len == ssid->ssid_len && |
| 702 | params->ssids[j].ssid && |
| 703 | os_memcmp(params->ssids[j].ssid, ssid->ssid, |
| 704 | ssid->ssid_len) == 0) |
| 705 | break; |
| 706 | } |
| 707 | if (j < params->num_ssids) |
| 708 | continue; /* already in the list */ |
| 709 | |
| 710 | if (params->num_ssids + 1 > max_ssids) { |
| 711 | wpa_printf(MSG_DEBUG, |
| 712 | "Over max scan SSIDs for manual request"); |
| 713 | break; |
| 714 | } |
| 715 | |
| 716 | wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s", |
| 717 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
| 718 | params->ssids[params->num_ssids].ssid = ssid->ssid; |
| 719 | params->ssids[params->num_ssids].ssid_len = ssid->ssid_len; |
| 720 | params->num_ssids++; |
| 721 | } |
| 722 | |
| 723 | wpa_s->scan_id_count = 0; |
| 724 | } |
| 725 | |
| 726 | |
Dmitry Shmidt | 7a53dbb | 2015-06-11 13:13:53 -0700 | [diff] [blame] | 727 | static int wpa_set_ssids_from_scan_req(struct wpa_supplicant *wpa_s, |
| 728 | struct wpa_driver_scan_params *params, |
| 729 | size_t max_ssids) |
| 730 | { |
| 731 | unsigned int i; |
| 732 | |
| 733 | if (wpa_s->ssids_from_scan_req == NULL || |
| 734 | wpa_s->num_ssids_from_scan_req == 0) |
| 735 | return 0; |
| 736 | |
| 737 | if (wpa_s->num_ssids_from_scan_req > max_ssids) { |
| 738 | wpa_s->num_ssids_from_scan_req = max_ssids; |
| 739 | wpa_printf(MSG_DEBUG, "Over max scan SSIDs from scan req: %u", |
| 740 | (unsigned int) max_ssids); |
| 741 | } |
| 742 | |
| 743 | for (i = 0; i < wpa_s->num_ssids_from_scan_req; i++) { |
| 744 | params->ssids[i].ssid = wpa_s->ssids_from_scan_req[i].ssid; |
| 745 | params->ssids[i].ssid_len = |
| 746 | wpa_s->ssids_from_scan_req[i].ssid_len; |
| 747 | wpa_hexdump_ascii(MSG_DEBUG, "specific SSID", |
| 748 | params->ssids[i].ssid, |
| 749 | params->ssids[i].ssid_len); |
| 750 | } |
| 751 | |
| 752 | params->num_ssids = wpa_s->num_ssids_from_scan_req; |
| 753 | wpa_s->num_ssids_from_scan_req = 0; |
| 754 | return 1; |
| 755 | } |
| 756 | |
| 757 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 758 | static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx) |
| 759 | { |
| 760 | struct wpa_supplicant *wpa_s = eloop_ctx; |
| 761 | struct wpa_ssid *ssid; |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 762 | int ret, p2p_in_prog; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 763 | struct wpabuf *extra_ie = NULL; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 764 | struct wpa_driver_scan_params params; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 765 | struct wpa_driver_scan_params *scan_params; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 766 | size_t max_ssids; |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 767 | int connect_without_scan = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 768 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 769 | wpa_s->ignore_post_flush_scan_res = 0; |
| 770 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 771 | if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) { |
| 772 | wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled"); |
| 773 | return; |
| 774 | } |
| 775 | |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 776 | if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) { |
Dmitry Shmidt | aa53251 | 2012-09-24 10:35:31 -0700 | [diff] [blame] | 777 | wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 778 | wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED); |
| 779 | return; |
| 780 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 781 | |
Dmitry Shmidt | 5887a9d | 2012-09-14 10:47:43 -0700 | [diff] [blame] | 782 | if (wpa_s->scanning) { |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 783 | /* |
| 784 | * If we are already in scanning state, we shall reschedule the |
| 785 | * the incoming scan request. |
| 786 | */ |
| 787 | wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req"); |
| 788 | wpa_supplicant_req_scan(wpa_s, 1, 0); |
Dmitry Shmidt | 5887a9d | 2012-09-14 10:47:43 -0700 | [diff] [blame] | 789 | return; |
| 790 | } |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 791 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 792 | if (!wpa_supplicant_enabled_networks(wpa_s) && |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 793 | wpa_s->scan_req == NORMAL_SCAN_REQ) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 794 | wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan"); |
| 795 | wpa_supplicant_set_state(wpa_s, WPA_INACTIVE); |
| 796 | return; |
| 797 | } |
| 798 | |
| 799 | if (wpa_s->conf->ap_scan != 0 && |
| 800 | (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) { |
| 801 | wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - " |
| 802 | "overriding ap_scan configuration"); |
| 803 | wpa_s->conf->ap_scan = 0; |
| 804 | wpas_notify_ap_scan_changed(wpa_s); |
| 805 | } |
| 806 | |
| 807 | if (wpa_s->conf->ap_scan == 0) { |
| 808 | wpa_supplicant_gen_assoc_event(wpa_s); |
| 809 | return; |
| 810 | } |
| 811 | |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 812 | ssid = NULL; |
| 813 | if (wpa_s->scan_req != MANUAL_SCAN_REQ && |
| 814 | wpa_s->connect_without_scan) { |
| 815 | connect_without_scan = 1; |
| 816 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
| 817 | if (ssid == wpa_s->connect_without_scan) |
| 818 | break; |
| 819 | } |
| 820 | } |
| 821 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 822 | p2p_in_prog = wpas_p2p_in_progress(wpa_s); |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 823 | if (p2p_in_prog && p2p_in_prog != 2 && |
| 824 | (!ssid || |
| 825 | (ssid->mode != WPAS_MODE_AP && ssid->mode != WPAS_MODE_P2P_GO))) { |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 826 | wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress"); |
| 827 | wpa_supplicant_req_scan(wpa_s, 5, 0); |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 828 | return; |
| 829 | } |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 830 | |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 831 | /* |
| 832 | * Don't cancel the scan based on ongoing PNO; defer it. Some scans are |
| 833 | * used for changing modes inside wpa_supplicant (roaming, |
| 834 | * auto-reconnect, etc). Discarding the scan might hurt these processes. |
| 835 | * The normal use case for PNO is to suspend the host immediately after |
| 836 | * starting PNO, so the periodic 100 ms attempts to run the scan do not |
| 837 | * normally happen in practice multiple times, i.e., this is simply |
| 838 | * restarting scanning once the host is woken up and PNO stopped. |
| 839 | */ |
| 840 | if (wpa_s->pno || wpa_s->pno_sched_pending) { |
| 841 | wpa_dbg(wpa_s, MSG_DEBUG, "Defer scan - PNO is in progress"); |
| 842 | wpa_supplicant_req_scan(wpa_s, 0, 100000); |
| 843 | return; |
| 844 | } |
| 845 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 846 | if (wpa_s->conf->ap_scan == 2) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 847 | max_ssids = 1; |
| 848 | else { |
| 849 | max_ssids = wpa_s->max_scan_ssids; |
| 850 | if (max_ssids > WPAS_MAX_SCAN_SSIDS) |
| 851 | max_ssids = WPAS_MAX_SCAN_SSIDS; |
| 852 | } |
| 853 | |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 854 | wpa_s->last_scan_req = wpa_s->scan_req; |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 855 | wpa_s->scan_req = NORMAL_SCAN_REQ; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 856 | |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 857 | if (connect_without_scan) { |
| 858 | wpa_s->connect_without_scan = NULL; |
| 859 | if (ssid) { |
| 860 | wpa_printf(MSG_DEBUG, "Start a pre-selected network " |
| 861 | "without scan step"); |
| 862 | wpa_supplicant_associate(wpa_s, NULL, ssid); |
| 863 | return; |
| 864 | } |
| 865 | } |
| 866 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 867 | os_memset(¶ms, 0, sizeof(params)); |
| 868 | |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 869 | wpa_s->scan_prev_wpa_state = wpa_s->wpa_state; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 870 | if (wpa_s->wpa_state == WPA_DISCONNECTED || |
| 871 | wpa_s->wpa_state == WPA_INACTIVE) |
| 872 | wpa_supplicant_set_state(wpa_s, WPA_SCANNING); |
| 873 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 874 | /* |
| 875 | * If autoscan has set its own scanning parameters |
| 876 | */ |
| 877 | if (wpa_s->autoscan_params != NULL) { |
| 878 | scan_params = wpa_s->autoscan_params; |
| 879 | goto scan; |
| 880 | } |
| 881 | |
Dmitry Shmidt | 7a53dbb | 2015-06-11 13:13:53 -0700 | [diff] [blame] | 882 | if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && |
| 883 | wpa_set_ssids_from_scan_req(wpa_s, ¶ms, max_ssids)) { |
| 884 | wpa_printf(MSG_DEBUG, "Use specific SSIDs from SCAN command"); |
| 885 | goto ssid_list_set; |
| 886 | } |
| 887 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 888 | #ifdef CONFIG_P2P |
| 889 | if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) && |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 890 | wpa_s->go_params && !wpa_s->conf->passive_scan) { |
Dmitry Shmidt | cce0666 | 2013-11-04 18:44:24 -0800 | [diff] [blame] | 891 | wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)", |
| 892 | wpa_s->p2p_in_provisioning, |
| 893 | wpa_s->show_group_started); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 894 | params.ssids[0].ssid = wpa_s->go_params->ssid; |
| 895 | params.ssids[0].ssid_len = wpa_s->go_params->ssid_len; |
| 896 | params.num_ssids = 1; |
| 897 | goto ssid_list_set; |
| 898 | } |
Dmitry Shmidt | 1590709 | 2014-03-25 10:42:57 -0700 | [diff] [blame] | 899 | |
| 900 | if (wpa_s->p2p_in_invitation) { |
| 901 | if (wpa_s->current_ssid) { |
| 902 | wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation"); |
| 903 | params.ssids[0].ssid = wpa_s->current_ssid->ssid; |
| 904 | params.ssids[0].ssid_len = |
| 905 | wpa_s->current_ssid->ssid_len; |
| 906 | params.num_ssids = 1; |
| 907 | } else { |
| 908 | wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation"); |
| 909 | } |
| 910 | goto ssid_list_set; |
| 911 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 912 | #endif /* CONFIG_P2P */ |
| 913 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 914 | /* Find the starting point from which to continue scanning */ |
| 915 | ssid = wpa_s->conf->ssid; |
| 916 | if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) { |
| 917 | while (ssid) { |
| 918 | if (ssid == wpa_s->prev_scan_ssid) { |
| 919 | ssid = ssid->next; |
| 920 | break; |
| 921 | } |
| 922 | ssid = ssid->next; |
| 923 | } |
| 924 | } |
| 925 | |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 926 | if (wpa_s->last_scan_req != MANUAL_SCAN_REQ && |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 927 | #ifdef CONFIG_AP |
| 928 | !wpa_s->ap_iface && |
| 929 | #endif /* CONFIG_AP */ |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 930 | wpa_s->conf->ap_scan == 2) { |
Jouni Malinen | 75ecf52 | 2011-06-27 15:19:46 -0700 | [diff] [blame] | 931 | wpa_s->connect_without_scan = NULL; |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 932 | wpa_s->prev_scan_wildcard = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 933 | wpa_supplicant_assoc_try(wpa_s, ssid); |
| 934 | return; |
| 935 | } else if (wpa_s->conf->ap_scan == 2) { |
| 936 | /* |
| 937 | * User-initiated scan request in ap_scan == 2; scan with |
| 938 | * wildcard SSID. |
| 939 | */ |
| 940 | ssid = NULL; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 941 | } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) { |
| 942 | /* |
| 943 | * Perform single-channel single-SSID scan for |
| 944 | * reassociate-to-same-BSS operation. |
| 945 | */ |
| 946 | /* Setup SSID */ |
| 947 | ssid = wpa_s->current_ssid; |
| 948 | wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID", |
| 949 | ssid->ssid, ssid->ssid_len); |
| 950 | params.ssids[0].ssid = ssid->ssid; |
| 951 | params.ssids[0].ssid_len = ssid->ssid_len; |
| 952 | params.num_ssids = 1; |
| 953 | |
| 954 | /* |
| 955 | * Allocate memory for frequency array, allocate one extra |
| 956 | * slot for the zero-terminator. |
| 957 | */ |
| 958 | params.freqs = os_malloc(sizeof(int) * 2); |
Dmitry Shmidt | d5ab1b5 | 2016-06-21 12:38:41 -0700 | [diff] [blame] | 959 | if (params.freqs) { |
| 960 | params.freqs[0] = wpa_s->assoc_freq; |
| 961 | params.freqs[1] = 0; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 962 | } |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 963 | |
| 964 | /* |
| 965 | * Reset the reattach flag so that we fall back to full scan if |
| 966 | * this scan fails. |
| 967 | */ |
| 968 | wpa_s->reattach = 0; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 969 | } else { |
| 970 | struct wpa_ssid *start = ssid, *tssid; |
| 971 | int freqs_set = 0; |
| 972 | if (ssid == NULL && max_ssids > 1) |
| 973 | ssid = wpa_s->conf->ssid; |
| 974 | while (ssid) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 975 | if (!wpas_network_disabled(wpa_s, ssid) && |
| 976 | ssid->scan_ssid) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 977 | wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID", |
| 978 | ssid->ssid, ssid->ssid_len); |
| 979 | params.ssids[params.num_ssids].ssid = |
| 980 | ssid->ssid; |
| 981 | params.ssids[params.num_ssids].ssid_len = |
| 982 | ssid->ssid_len; |
| 983 | params.num_ssids++; |
| 984 | if (params.num_ssids + 1 >= max_ssids) |
| 985 | break; |
| 986 | } |
| 987 | ssid = ssid->next; |
| 988 | if (ssid == start) |
| 989 | break; |
| 990 | if (ssid == NULL && max_ssids > 1 && |
| 991 | start != wpa_s->conf->ssid) |
| 992 | ssid = wpa_s->conf->ssid; |
| 993 | } |
| 994 | |
Dmitry Shmidt | c281702 | 2014-07-02 10:32:10 -0700 | [diff] [blame] | 995 | if (wpa_s->scan_id_count && |
| 996 | wpa_s->last_scan_req == MANUAL_SCAN_REQ) |
| 997 | wpa_set_scan_ssids(wpa_s, ¶ms, max_ssids); |
| 998 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 999 | for (tssid = wpa_s->conf->ssid; |
| 1000 | wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid; |
| 1001 | tssid = tssid->next) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1002 | if (wpas_network_disabled(wpa_s, tssid)) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1003 | continue; |
| 1004 | if ((params.freqs || !freqs_set) && tssid->scan_freq) { |
| 1005 | int_array_concat(¶ms.freqs, |
| 1006 | tssid->scan_freq); |
| 1007 | } else { |
| 1008 | os_free(params.freqs); |
| 1009 | params.freqs = NULL; |
| 1010 | } |
| 1011 | freqs_set = 1; |
| 1012 | } |
| 1013 | int_array_sort_unique(params.freqs); |
| 1014 | } |
| 1015 | |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1016 | if (ssid && max_ssids == 1) { |
| 1017 | /* |
| 1018 | * If the driver is limited to 1 SSID at a time interleave |
| 1019 | * wildcard SSID scans with specific SSID scans to avoid |
| 1020 | * waiting a long time for a wildcard scan. |
| 1021 | */ |
| 1022 | if (!wpa_s->prev_scan_wildcard) { |
| 1023 | params.ssids[0].ssid = NULL; |
| 1024 | params.ssids[0].ssid_len = 0; |
| 1025 | wpa_s->prev_scan_wildcard = 1; |
| 1026 | wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for " |
| 1027 | "wildcard SSID (Interleave with specific)"); |
| 1028 | } else { |
| 1029 | wpa_s->prev_scan_ssid = ssid; |
| 1030 | wpa_s->prev_scan_wildcard = 0; |
| 1031 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1032 | "Starting AP scan for specific SSID: %s", |
| 1033 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1034 | } |
Dmitry Shmidt | c5ec7f5 | 2012-03-06 16:33:24 -0800 | [diff] [blame] | 1035 | } else if (ssid) { |
| 1036 | /* max_ssids > 1 */ |
| 1037 | |
| 1038 | wpa_s->prev_scan_ssid = ssid; |
| 1039 | wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in " |
| 1040 | "the scan request"); |
| 1041 | params.num_ssids++; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1042 | } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && |
| 1043 | wpa_s->manual_scan_passive && params.num_ssids == 0) { |
| 1044 | wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request"); |
Dmitry Shmidt | 807291d | 2015-01-27 13:40:23 -0800 | [diff] [blame] | 1045 | } else if (wpa_s->conf->passive_scan) { |
| 1046 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1047 | "Use passive scan based on configuration"); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1048 | } else { |
| 1049 | wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN; |
| 1050 | params.num_ssids++; |
| 1051 | wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard " |
| 1052 | "SSID"); |
| 1053 | } |
| 1054 | |
Dmitry Shmidt | 7a53dbb | 2015-06-11 13:13:53 -0700 | [diff] [blame] | 1055 | ssid_list_set: |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1056 | wpa_supplicant_optimize_freqs(wpa_s, ¶ms); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1057 | extra_ie = wpa_supplicant_extra_ies(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1058 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1059 | if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1060 | wpa_s->manual_scan_only_new) { |
| 1061 | wpa_printf(MSG_DEBUG, |
| 1062 | "Request driver to clear scan cache due to manual only_new=1 scan"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1063 | params.only_new_results = 1; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1064 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1065 | |
| 1066 | if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL && |
| 1067 | wpa_s->manual_scan_freqs) { |
| 1068 | wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels"); |
| 1069 | params.freqs = wpa_s->manual_scan_freqs; |
| 1070 | wpa_s->manual_scan_freqs = NULL; |
| 1071 | } |
| 1072 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1073 | if (params.freqs == NULL && wpa_s->select_network_scan_freqs) { |
| 1074 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1075 | "Limit select_network scan to specified channels"); |
| 1076 | params.freqs = wpa_s->select_network_scan_freqs; |
| 1077 | wpa_s->select_network_scan_freqs = NULL; |
| 1078 | } |
| 1079 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1080 | if (params.freqs == NULL && wpa_s->next_scan_freqs) { |
| 1081 | wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously " |
| 1082 | "generated frequency list"); |
| 1083 | params.freqs = wpa_s->next_scan_freqs; |
| 1084 | } else |
| 1085 | os_free(wpa_s->next_scan_freqs); |
| 1086 | wpa_s->next_scan_freqs = NULL; |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1087 | wpa_setband_scan_freqs(wpa_s, ¶ms); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1088 | |
Dmitry Shmidt | 51b6ea8 | 2013-05-08 10:42:09 -0700 | [diff] [blame] | 1089 | /* See if user specified frequencies. If so, scan only those. */ |
| 1090 | if (wpa_s->conf->freq_list && !params.freqs) { |
| 1091 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1092 | "Optimize scan based on conf->freq_list"); |
| 1093 | int_array_concat(¶ms.freqs, wpa_s->conf->freq_list); |
| 1094 | } |
| 1095 | |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1096 | /* Use current associated channel? */ |
| 1097 | if (wpa_s->conf->scan_cur_freq && !params.freqs) { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 1098 | unsigned int num = wpa_s->num_multichan_concurrent; |
| 1099 | |
| 1100 | params.freqs = os_calloc(num + 1, sizeof(int)); |
| 1101 | if (params.freqs) { |
| 1102 | num = get_shared_radio_freqs(wpa_s, params.freqs, num); |
| 1103 | if (num > 0) { |
| 1104 | wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the " |
| 1105 | "current operating channels since " |
| 1106 | "scan_cur_freq is enabled"); |
| 1107 | } else { |
| 1108 | os_free(params.freqs); |
| 1109 | params.freqs = NULL; |
| 1110 | } |
Dmitry Shmidt | ea69e84 | 2013-05-13 14:52:28 -0700 | [diff] [blame] | 1111 | } |
| 1112 | } |
| 1113 | |
Hai Shalom | ce48b4a | 2018-09-05 11:41:35 -0700 | [diff] [blame] | 1114 | #ifdef CONFIG_MBO |
| 1115 | if (wpa_s->enable_oce & OCE_STA) |
| 1116 | params.oce_scan = 1; |
| 1117 | #endif /* CONFIG_MBO */ |
| 1118 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1119 | params.filter_ssids = wpa_supplicant_build_filter_ssids( |
| 1120 | wpa_s->conf, ¶ms.num_filter_ssids); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1121 | if (extra_ie) { |
| 1122 | params.extra_ies = wpabuf_head(extra_ie); |
| 1123 | params.extra_ies_len = wpabuf_len(extra_ie); |
| 1124 | } |
| 1125 | |
| 1126 | #ifdef CONFIG_P2P |
Dmitry Shmidt | 413dde7 | 2014-04-11 10:23:22 -0700 | [diff] [blame] | 1127 | if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation || |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1128 | (wpa_s->show_group_started && wpa_s->go_params)) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1129 | /* |
| 1130 | * The interface may not yet be in P2P mode, so we have to |
| 1131 | * explicitly request P2P probe to disable CCK rates. |
| 1132 | */ |
| 1133 | params.p2p_probe = 1; |
| 1134 | } |
| 1135 | #endif /* CONFIG_P2P */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1136 | |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 1137 | if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) && |
| 1138 | wpa_s->wpa_state <= WPA_SCANNING) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1139 | params.mac_addr_rand = 1; |
| 1140 | if (wpa_s->mac_addr_scan) { |
| 1141 | params.mac_addr = wpa_s->mac_addr_scan; |
| 1142 | params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN; |
| 1143 | } |
| 1144 | } |
| 1145 | |
Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1146 | if (!is_zero_ether_addr(wpa_s->next_scan_bssid)) { |
| 1147 | struct wpa_bss *bss; |
| 1148 | |
| 1149 | params.bssid = wpa_s->next_scan_bssid; |
| 1150 | bss = wpa_bss_get_bssid_latest(wpa_s, params.bssid); |
| 1151 | if (bss && bss->ssid_len && params.num_ssids == 1 && |
| 1152 | params.ssids[0].ssid_len == 0) { |
| 1153 | params.ssids[0].ssid = bss->ssid; |
| 1154 | params.ssids[0].ssid_len = bss->ssid_len; |
| 1155 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1156 | "Scan a previously specified BSSID " MACSTR |
| 1157 | " and SSID %s", |
| 1158 | MAC2STR(params.bssid), |
| 1159 | wpa_ssid_txt(bss->ssid, bss->ssid_len)); |
| 1160 | } else { |
| 1161 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1162 | "Scan a previously specified BSSID " MACSTR, |
| 1163 | MAC2STR(params.bssid)); |
| 1164 | } |
| 1165 | } |
| 1166 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1167 | scan_params = ¶ms; |
| 1168 | |
| 1169 | scan: |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1170 | #ifdef CONFIG_P2P |
| 1171 | /* |
| 1172 | * If the driver does not support multi-channel concurrency and a |
| 1173 | * virtual interface that shares the same radio with the wpa_s interface |
| 1174 | * is operating there may not be need to scan other channels apart from |
| 1175 | * the current operating channel on the other virtual interface. Filter |
| 1176 | * out other channels in case we are trying to find a connection for a |
| 1177 | * station interface when we are not configured to prefer station |
| 1178 | * connection and a concurrent operation is already in process. |
| 1179 | */ |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 1180 | if (wpa_s->scan_for_connection && |
| 1181 | wpa_s->last_scan_req == NORMAL_SCAN_REQ && |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1182 | !scan_params->freqs && !params.freqs && |
| 1183 | wpas_is_p2p_prioritized(wpa_s) && |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1184 | wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE && |
| 1185 | non_p2p_network_enabled(wpa_s)) { |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 1186 | unsigned int num = wpa_s->num_multichan_concurrent; |
| 1187 | |
| 1188 | params.freqs = os_calloc(num + 1, sizeof(int)); |
| 1189 | if (params.freqs) { |
| 1190 | num = get_shared_radio_freqs(wpa_s, params.freqs, num); |
| 1191 | if (num > 0 && num == wpa_s->num_multichan_concurrent) { |
| 1192 | wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used"); |
| 1193 | } else { |
| 1194 | os_free(params.freqs); |
| 1195 | params.freqs = NULL; |
| 1196 | } |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1197 | } |
| 1198 | } |
| 1199 | #endif /* CONFIG_P2P */ |
| 1200 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1201 | ret = wpa_supplicant_trigger_scan(wpa_s, scan_params); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1202 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1203 | if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs && |
| 1204 | !wpa_s->manual_scan_freqs) { |
| 1205 | /* Restore manual_scan_freqs for the next attempt */ |
| 1206 | wpa_s->manual_scan_freqs = params.freqs; |
| 1207 | params.freqs = NULL; |
| 1208 | } |
| 1209 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1210 | wpabuf_free(extra_ie); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1211 | os_free(params.freqs); |
| 1212 | os_free(params.filter_ssids); |
| 1213 | |
| 1214 | if (ret) { |
| 1215 | wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan"); |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1216 | if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state) |
| 1217 | wpa_supplicant_set_state(wpa_s, |
| 1218 | wpa_s->scan_prev_wpa_state); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1219 | /* Restore scan_req since we will try to scan again */ |
Dmitry Shmidt | 01904cf | 2013-12-05 11:08:35 -0800 | [diff] [blame] | 1220 | wpa_s->scan_req = wpa_s->last_scan_req; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1221 | wpa_supplicant_req_scan(wpa_s, 1, 0); |
Dmitry Shmidt | d5e4923 | 2012-12-03 15:08:10 -0800 | [diff] [blame] | 1222 | } else { |
| 1223 | wpa_s->scan_for_connection = 0; |
Dmitry Shmidt | 2f74e36 | 2015-01-21 13:19:05 -0800 | [diff] [blame] | 1224 | #ifdef CONFIG_INTERWORKING |
| 1225 | wpa_s->interworking_fast_assoc_tried = 0; |
| 1226 | #endif /* CONFIG_INTERWORKING */ |
Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 1227 | if (params.bssid) |
| 1228 | os_memset(wpa_s->next_scan_bssid, 0, ETH_ALEN); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 1233 | void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec) |
| 1234 | { |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 1235 | struct os_reltime remaining, new_int; |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 1236 | int cancelled; |
| 1237 | |
| 1238 | cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL, |
| 1239 | &remaining); |
| 1240 | |
| 1241 | new_int.sec = sec; |
| 1242 | new_int.usec = 0; |
Dmitry Shmidt | fa3fc4a | 2013-11-21 13:34:38 -0800 | [diff] [blame] | 1243 | if (cancelled && os_reltime_before(&remaining, &new_int)) { |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 1244 | new_int.sec = remaining.sec; |
| 1245 | new_int.usec = remaining.usec; |
| 1246 | } |
| 1247 | |
Dmitry Shmidt | 051af73 | 2013-10-22 13:52:46 -0700 | [diff] [blame] | 1248 | if (cancelled) { |
| 1249 | eloop_register_timeout(new_int.sec, new_int.usec, |
| 1250 | wpa_supplicant_scan, wpa_s, NULL); |
| 1251 | } |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 1252 | wpa_s->scan_interval = sec; |
| 1253 | } |
| 1254 | |
| 1255 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1256 | /** |
| 1257 | * wpa_supplicant_req_scan - Schedule a scan for neighboring access points |
| 1258 | * @wpa_s: Pointer to wpa_supplicant data |
| 1259 | * @sec: Number of seconds after which to scan |
| 1260 | * @usec: Number of microseconds after which to scan |
| 1261 | * |
| 1262 | * This function is used to schedule a scan for neighboring access points after |
| 1263 | * the specified time. |
| 1264 | */ |
| 1265 | void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec) |
| 1266 | { |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 1267 | int res; |
| 1268 | |
| 1269 | if (wpa_s->p2p_mgmt) { |
| 1270 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1271 | "Ignore scan request (%d.%06d sec) on p2p_mgmt interface", |
| 1272 | sec, usec); |
| 1273 | return; |
| 1274 | } |
| 1275 | |
| 1276 | res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s, |
| 1277 | NULL); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1278 | if (res == 1) { |
| 1279 | wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec", |
Dmitry Shmidt | e0e48dc | 2013-11-18 12:00:06 -0800 | [diff] [blame] | 1280 | sec, usec); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 1281 | } else if (res == 0) { |
| 1282 | wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner", |
| 1283 | sec, usec); |
| 1284 | } else { |
| 1285 | wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec", |
| 1286 | sec, usec); |
| 1287 | eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1288 | } |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | |
| 1292 | /** |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1293 | * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan |
| 1294 | * @wpa_s: Pointer to wpa_supplicant data |
| 1295 | * @sec: Number of seconds after which to scan |
| 1296 | * @usec: Number of microseconds after which to scan |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1297 | * Returns: 0 on success or -1 otherwise |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1298 | * |
| 1299 | * This function is used to schedule periodic scans for neighboring |
| 1300 | * access points after the specified time. |
| 1301 | */ |
| 1302 | int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s, |
| 1303 | int sec, int usec) |
| 1304 | { |
| 1305 | if (!wpa_s->sched_scan_supported) |
| 1306 | return -1; |
| 1307 | |
| 1308 | eloop_register_timeout(sec, usec, |
| 1309 | wpa_supplicant_delayed_sched_scan_timeout, |
| 1310 | wpa_s, NULL); |
| 1311 | |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1316 | static void |
| 1317 | wpa_scan_set_relative_rssi_params(struct wpa_supplicant *wpa_s, |
| 1318 | struct wpa_driver_scan_params *params) |
| 1319 | { |
| 1320 | if (wpa_s->wpa_state != WPA_COMPLETED || |
| 1321 | !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SCHED_SCAN_RELATIVE_RSSI) || |
| 1322 | wpa_s->srp.relative_rssi_set == 0) |
| 1323 | return; |
| 1324 | |
| 1325 | params->relative_rssi_set = 1; |
| 1326 | params->relative_rssi = wpa_s->srp.relative_rssi; |
| 1327 | |
| 1328 | if (wpa_s->srp.relative_adjust_rssi == 0) |
| 1329 | return; |
| 1330 | |
| 1331 | params->relative_adjust_band = wpa_s->srp.relative_adjust_band; |
| 1332 | params->relative_adjust_rssi = wpa_s->srp.relative_adjust_rssi; |
| 1333 | } |
| 1334 | |
| 1335 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1336 | /** |
| 1337 | * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan |
| 1338 | * @wpa_s: Pointer to wpa_supplicant data |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1339 | * Returns: 0 is sched_scan was started or -1 otherwise |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1340 | * |
| 1341 | * This function is used to schedule periodic scans for neighboring |
| 1342 | * access points repeating the scan continuously. |
| 1343 | */ |
| 1344 | int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s) |
| 1345 | { |
| 1346 | struct wpa_driver_scan_params params; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1347 | struct wpa_driver_scan_params *scan_params; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1348 | enum wpa_states prev_state; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1349 | struct wpa_ssid *ssid = NULL; |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1350 | struct wpabuf *extra_ie = NULL; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1351 | int ret; |
| 1352 | unsigned int max_sched_scan_ssids; |
| 1353 | int wildcard = 0; |
| 1354 | int need_ssids; |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 1355 | struct sched_scan_plan scan_plan; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1356 | |
| 1357 | if (!wpa_s->sched_scan_supported) |
| 1358 | return -1; |
| 1359 | |
| 1360 | if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS) |
| 1361 | max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS; |
| 1362 | else |
| 1363 | max_sched_scan_ssids = wpa_s->max_sched_scan_ssids; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1364 | if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1365 | return -1; |
| 1366 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1367 | wpa_s->sched_scan_stop_req = 0; |
| 1368 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1369 | if (wpa_s->sched_scanning) { |
| 1370 | wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning"); |
| 1371 | return 0; |
| 1372 | } |
| 1373 | |
| 1374 | need_ssids = 0; |
| 1375 | for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1376 | if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1377 | /* Use wildcard SSID to find this network */ |
| 1378 | wildcard = 1; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1379 | } else if (!wpas_network_disabled(wpa_s, ssid) && |
| 1380 | ssid->ssid_len) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1381 | need_ssids++; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1382 | |
| 1383 | #ifdef CONFIG_WPS |
| 1384 | if (!wpas_network_disabled(wpa_s, ssid) && |
| 1385 | ssid->key_mgmt == WPA_KEY_MGMT_WPS) { |
| 1386 | /* |
| 1387 | * Normal scan is more reliable and faster for WPS |
| 1388 | * operations and since these are for short periods of |
| 1389 | * time, the benefit of trying to use sched_scan would |
| 1390 | * be limited. |
| 1391 | */ |
| 1392 | wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of " |
| 1393 | "sched_scan for WPS"); |
| 1394 | return -1; |
| 1395 | } |
| 1396 | #endif /* CONFIG_WPS */ |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1397 | } |
| 1398 | if (wildcard) |
| 1399 | need_ssids++; |
| 1400 | |
| 1401 | if (wpa_s->normal_scans < 3 && |
| 1402 | (need_ssids <= wpa_s->max_scan_ssids || |
| 1403 | wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) { |
| 1404 | /* |
| 1405 | * When normal scan can speed up operations, use that for the |
| 1406 | * first operations before starting the sched_scan to allow |
| 1407 | * user space sleep more. We do this only if the normal scan |
| 1408 | * has functionality that is suitable for this or if the |
| 1409 | * sched_scan does not have better support for multiple SSIDs. |
| 1410 | */ |
| 1411 | wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of " |
| 1412 | "sched_scan for initial scans (normal_scans=%d)", |
| 1413 | wpa_s->normal_scans); |
| 1414 | return -1; |
| 1415 | } |
| 1416 | |
| 1417 | os_memset(¶ms, 0, sizeof(params)); |
| 1418 | |
| 1419 | /* If we can't allocate space for the filters, we just don't filter */ |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1420 | params.filter_ssids = os_calloc(wpa_s->max_match_sets, |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1421 | sizeof(struct wpa_driver_scan_filter)); |
| 1422 | |
| 1423 | prev_state = wpa_s->wpa_state; |
| 1424 | if (wpa_s->wpa_state == WPA_DISCONNECTED || |
| 1425 | wpa_s->wpa_state == WPA_INACTIVE) |
| 1426 | wpa_supplicant_set_state(wpa_s, WPA_SCANNING); |
| 1427 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1428 | if (wpa_s->autoscan_params != NULL) { |
| 1429 | scan_params = wpa_s->autoscan_params; |
| 1430 | goto scan; |
| 1431 | } |
| 1432 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1433 | /* Find the starting point from which to continue scanning */ |
| 1434 | ssid = wpa_s->conf->ssid; |
| 1435 | if (wpa_s->prev_sched_ssid) { |
| 1436 | while (ssid) { |
| 1437 | if (ssid == wpa_s->prev_sched_ssid) { |
| 1438 | ssid = ssid->next; |
| 1439 | break; |
| 1440 | } |
| 1441 | ssid = ssid->next; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | if (!ssid || !wpa_s->prev_sched_ssid) { |
| 1446 | wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list"); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1447 | wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2; |
| 1448 | wpa_s->first_sched_scan = 1; |
| 1449 | ssid = wpa_s->conf->ssid; |
| 1450 | wpa_s->prev_sched_ssid = ssid; |
| 1451 | } |
| 1452 | |
| 1453 | if (wildcard) { |
| 1454 | wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan"); |
| 1455 | params.num_ssids++; |
| 1456 | } |
| 1457 | |
| 1458 | while (ssid) { |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1459 | if (wpas_network_disabled(wpa_s, ssid)) |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1460 | goto next; |
| 1461 | |
| 1462 | if (params.num_filter_ssids < wpa_s->max_match_sets && |
| 1463 | params.filter_ssids && ssid->ssid && ssid->ssid_len) { |
| 1464 | wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s", |
| 1465 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
| 1466 | os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid, |
| 1467 | ssid->ssid, ssid->ssid_len); |
| 1468 | params.filter_ssids[params.num_filter_ssids].ssid_len = |
| 1469 | ssid->ssid_len; |
| 1470 | params.num_filter_ssids++; |
| 1471 | } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len) |
| 1472 | { |
| 1473 | wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID " |
| 1474 | "filter for sched_scan - drop filter"); |
| 1475 | os_free(params.filter_ssids); |
| 1476 | params.filter_ssids = NULL; |
| 1477 | params.num_filter_ssids = 0; |
| 1478 | } |
| 1479 | |
| 1480 | if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) { |
| 1481 | if (params.num_ssids == max_sched_scan_ssids) |
| 1482 | break; /* only room for broadcast SSID */ |
| 1483 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1484 | "add to active scan ssid: %s", |
| 1485 | wpa_ssid_txt(ssid->ssid, ssid->ssid_len)); |
| 1486 | params.ssids[params.num_ssids].ssid = |
| 1487 | ssid->ssid; |
| 1488 | params.ssids[params.num_ssids].ssid_len = |
| 1489 | ssid->ssid_len; |
| 1490 | params.num_ssids++; |
| 1491 | if (params.num_ssids >= max_sched_scan_ssids) { |
| 1492 | wpa_s->prev_sched_ssid = ssid; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1493 | do { |
| 1494 | ssid = ssid->next; |
| 1495 | } while (ssid && |
| 1496 | (wpas_network_disabled(wpa_s, ssid) || |
| 1497 | !ssid->scan_ssid)); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1498 | break; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | next: |
| 1503 | wpa_s->prev_sched_ssid = ssid; |
| 1504 | ssid = ssid->next; |
| 1505 | } |
| 1506 | |
| 1507 | if (params.num_filter_ssids == 0) { |
| 1508 | os_free(params.filter_ssids); |
| 1509 | params.filter_ssids = NULL; |
| 1510 | } |
| 1511 | |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1512 | extra_ie = wpa_supplicant_extra_ies(wpa_s); |
| 1513 | if (extra_ie) { |
| 1514 | params.extra_ies = wpabuf_head(extra_ie); |
| 1515 | params.extra_ies_len = wpabuf_len(extra_ie); |
| 1516 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1517 | |
Dmitry Shmidt | 1846323 | 2014-01-24 12:29:41 -0800 | [diff] [blame] | 1518 | if (wpa_s->conf->filter_rssi) |
| 1519 | params.filter_rssi = wpa_s->conf->filter_rssi; |
| 1520 | |
Dmitry Shmidt | 661b4f7 | 2014-09-29 14:58:27 -0700 | [diff] [blame] | 1521 | /* See if user specified frequencies. If so, scan only those. */ |
| 1522 | if (wpa_s->conf->freq_list && !params.freqs) { |
| 1523 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1524 | "Optimize scan based on conf->freq_list"); |
| 1525 | int_array_concat(¶ms.freqs, wpa_s->conf->freq_list); |
| 1526 | } |
| 1527 | |
Hai Shalom | ce48b4a | 2018-09-05 11:41:35 -0700 | [diff] [blame] | 1528 | #ifdef CONFIG_MBO |
| 1529 | if (wpa_s->enable_oce & OCE_STA) |
| 1530 | params.oce_scan = 1; |
| 1531 | #endif /* CONFIG_MBO */ |
| 1532 | |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1533 | scan_params = ¶ms; |
| 1534 | |
| 1535 | scan: |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 1536 | wpa_s->sched_scan_timed_out = 0; |
| 1537 | |
| 1538 | /* |
| 1539 | * We cannot support multiple scan plans if the scan request includes |
| 1540 | * too many SSID's, so in this case use only the last scan plan and make |
| 1541 | * it run infinitely. It will be stopped by the timeout. |
| 1542 | */ |
| 1543 | if (wpa_s->sched_scan_plans_num == 1 || |
| 1544 | (wpa_s->sched_scan_plans_num && !ssid && wpa_s->first_sched_scan)) { |
| 1545 | params.sched_scan_plans = wpa_s->sched_scan_plans; |
| 1546 | params.sched_scan_plans_num = wpa_s->sched_scan_plans_num; |
| 1547 | } else if (wpa_s->sched_scan_plans_num > 1) { |
| 1548 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1549 | "Too many SSIDs. Default to using single scheduled_scan plan"); |
| 1550 | params.sched_scan_plans = |
| 1551 | &wpa_s->sched_scan_plans[wpa_s->sched_scan_plans_num - |
| 1552 | 1]; |
| 1553 | params.sched_scan_plans_num = 1; |
| 1554 | } else { |
| 1555 | if (wpa_s->conf->sched_scan_interval) |
| 1556 | scan_plan.interval = wpa_s->conf->sched_scan_interval; |
| 1557 | else |
| 1558 | scan_plan.interval = 10; |
| 1559 | |
| 1560 | if (scan_plan.interval > wpa_s->max_sched_scan_plan_interval) { |
| 1561 | wpa_printf(MSG_WARNING, |
| 1562 | "Scan interval too long(%u), use the maximum allowed(%u)", |
| 1563 | scan_plan.interval, |
| 1564 | wpa_s->max_sched_scan_plan_interval); |
| 1565 | scan_plan.interval = |
| 1566 | wpa_s->max_sched_scan_plan_interval; |
| 1567 | } |
| 1568 | |
| 1569 | scan_plan.iterations = 0; |
| 1570 | params.sched_scan_plans = &scan_plan; |
| 1571 | params.sched_scan_plans_num = 1; |
| 1572 | } |
| 1573 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1574 | params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay; |
| 1575 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1576 | if (ssid || !wpa_s->first_sched_scan) { |
| 1577 | wpa_dbg(wpa_s, MSG_DEBUG, |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1578 | "Starting sched scan after %u seconds: interval %u timeout %d", |
| 1579 | params.sched_scan_start_delay, |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 1580 | params.sched_scan_plans[0].interval, |
| 1581 | wpa_s->sched_scan_timeout); |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1582 | } else { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 1583 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 1584 | "Starting sched scan after %u seconds (no timeout)", |
| 1585 | params.sched_scan_start_delay); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1586 | } |
| 1587 | |
Dmitry Shmidt | b6e9aaf | 2013-05-20 14:49:44 -0700 | [diff] [blame] | 1588 | wpa_setband_scan_freqs(wpa_s, scan_params); |
| 1589 | |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 1590 | if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) && |
| 1591 | wpa_s->wpa_state <= WPA_SCANNING) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 1592 | params.mac_addr_rand = 1; |
| 1593 | if (wpa_s->mac_addr_sched_scan) { |
| 1594 | params.mac_addr = wpa_s->mac_addr_sched_scan; |
| 1595 | params.mac_addr_mask = wpa_s->mac_addr_sched_scan + |
| 1596 | ETH_ALEN; |
| 1597 | } |
| 1598 | } |
| 1599 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1600 | wpa_scan_set_relative_rssi_params(wpa_s, scan_params); |
| 1601 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 1602 | ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params); |
Dmitry Shmidt | 61d9df3 | 2012-08-29 16:22:06 -0700 | [diff] [blame] | 1603 | wpabuf_free(extra_ie); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1604 | os_free(params.filter_ssids); |
| 1605 | if (ret) { |
| 1606 | wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan"); |
| 1607 | if (prev_state != wpa_s->wpa_state) |
| 1608 | wpa_supplicant_set_state(wpa_s, prev_state); |
| 1609 | return ret; |
| 1610 | } |
| 1611 | |
| 1612 | /* If we have more SSIDs to scan, add a timeout so we scan them too */ |
| 1613 | if (ssid || !wpa_s->first_sched_scan) { |
| 1614 | wpa_s->sched_scan_timed_out = 0; |
| 1615 | eloop_register_timeout(wpa_s->sched_scan_timeout, 0, |
| 1616 | wpa_supplicant_sched_scan_timeout, |
| 1617 | wpa_s, NULL); |
| 1618 | wpa_s->first_sched_scan = 0; |
| 1619 | wpa_s->sched_scan_timeout /= 2; |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 1620 | params.sched_scan_plans[0].interval *= 2; |
| 1621 | if ((unsigned int) wpa_s->sched_scan_timeout < |
| 1622 | params.sched_scan_plans[0].interval || |
| 1623 | params.sched_scan_plans[0].interval > |
| 1624 | wpa_s->max_sched_scan_plan_interval) { |
| 1625 | params.sched_scan_plans[0].interval = 10; |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 1626 | wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2; |
| 1627 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1628 | } |
| 1629 | |
Dmitry Shmidt | 2f02319 | 2013-03-12 12:44:17 -0700 | [diff] [blame] | 1630 | /* If there is no more ssids, start next time from the beginning */ |
| 1631 | if (!ssid) |
| 1632 | wpa_s->prev_sched_ssid = NULL; |
| 1633 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | /** |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1639 | * wpa_supplicant_cancel_scan - Cancel a scheduled scan request |
| 1640 | * @wpa_s: Pointer to wpa_supplicant data |
| 1641 | * |
| 1642 | * This function is used to cancel a scan request scheduled with |
| 1643 | * wpa_supplicant_req_scan(). |
| 1644 | */ |
| 1645 | void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s) |
| 1646 | { |
| 1647 | wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request"); |
| 1648 | eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1652 | /** |
Dmitry Shmidt | c2ebb4b | 2013-07-24 12:57:51 -0700 | [diff] [blame] | 1653 | * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan |
| 1654 | * @wpa_s: Pointer to wpa_supplicant data |
| 1655 | * |
| 1656 | * This function is used to stop a delayed scheduled scan. |
| 1657 | */ |
| 1658 | void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s) |
| 1659 | { |
| 1660 | if (!wpa_s->sched_scan_supported) |
| 1661 | return; |
| 1662 | |
| 1663 | wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan"); |
| 1664 | eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout, |
| 1665 | wpa_s, NULL); |
| 1666 | } |
| 1667 | |
| 1668 | |
| 1669 | /** |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1670 | * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans |
| 1671 | * @wpa_s: Pointer to wpa_supplicant data |
| 1672 | * |
| 1673 | * This function is used to stop a periodic scheduled scan. |
| 1674 | */ |
| 1675 | void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s) |
| 1676 | { |
| 1677 | if (!wpa_s->sched_scanning) |
| 1678 | return; |
| 1679 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1680 | if (wpa_s->sched_scanning) |
| 1681 | wpa_s->sched_scan_stop_req = 1; |
| 1682 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1683 | wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan"); |
| 1684 | eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL); |
| 1685 | wpa_supplicant_stop_sched_scan(wpa_s); |
| 1686 | } |
| 1687 | |
| 1688 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1689 | /** |
| 1690 | * wpa_supplicant_notify_scanning - Indicate possible scan state change |
| 1691 | * @wpa_s: Pointer to wpa_supplicant data |
| 1692 | * @scanning: Whether scanning is currently in progress |
| 1693 | * |
| 1694 | * This function is to generate scanning notifycations. It is called whenever |
| 1695 | * there may have been a change in scanning (scan started, completed, stopped). |
| 1696 | * wpas_notify_scanning() is called whenever the scanning state changed from the |
| 1697 | * previously notified state. |
| 1698 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1699 | void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s, |
| 1700 | int scanning) |
| 1701 | { |
| 1702 | if (wpa_s->scanning != scanning) { |
| 1703 | wpa_s->scanning = scanning; |
| 1704 | wpas_notify_scanning(wpa_s); |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | static int wpa_scan_get_max_rate(const struct wpa_scan_res *res) |
| 1710 | { |
| 1711 | int rate = 0; |
| 1712 | const u8 *ie; |
| 1713 | int i; |
| 1714 | |
| 1715 | ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES); |
| 1716 | for (i = 0; ie && i < ie[1]; i++) { |
| 1717 | if ((ie[i + 2] & 0x7f) > rate) |
| 1718 | rate = ie[i + 2] & 0x7f; |
| 1719 | } |
| 1720 | |
| 1721 | ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES); |
| 1722 | for (i = 0; ie && i < ie[1]; i++) { |
| 1723 | if ((ie[i + 2] & 0x7f) > rate) |
| 1724 | rate = ie[i + 2] & 0x7f; |
| 1725 | } |
| 1726 | |
| 1727 | return rate; |
| 1728 | } |
| 1729 | |
| 1730 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1731 | /** |
| 1732 | * wpa_scan_get_ie - Fetch a specified information element from a scan result |
| 1733 | * @res: Scan result entry |
| 1734 | * @ie: Information element identitifier (WLAN_EID_*) |
| 1735 | * Returns: Pointer to the information element (id field) or %NULL if not found |
| 1736 | * |
| 1737 | * This function returns the first matching information element in the scan |
| 1738 | * result. |
| 1739 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1740 | const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie) |
| 1741 | { |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 1742 | size_t ie_len = res->ie_len; |
| 1743 | |
| 1744 | /* Use the Beacon frame IEs if res->ie_len is not available */ |
| 1745 | if (!ie_len) |
| 1746 | ie_len = res->beacon_ie_len; |
| 1747 | |
| 1748 | return get_ie((const u8 *) (res + 1), ie_len, ie); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1749 | } |
| 1750 | |
| 1751 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1752 | /** |
| 1753 | * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result |
| 1754 | * @res: Scan result entry |
| 1755 | * @vendor_type: Vendor type (four octets starting the IE payload) |
| 1756 | * Returns: Pointer to the information element (id field) or %NULL if not found |
| 1757 | * |
| 1758 | * This function returns the first matching information element in the scan |
| 1759 | * result. |
| 1760 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1761 | const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res, |
| 1762 | u32 vendor_type) |
| 1763 | { |
| 1764 | const u8 *end, *pos; |
| 1765 | |
| 1766 | pos = (const u8 *) (res + 1); |
| 1767 | end = pos + res->ie_len; |
| 1768 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1769 | while (end - pos > 1) { |
| 1770 | if (2 + pos[1] > end - pos) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1771 | break; |
| 1772 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && |
| 1773 | vendor_type == WPA_GET_BE32(&pos[2])) |
| 1774 | return pos; |
| 1775 | pos += 2 + pos[1]; |
| 1776 | } |
| 1777 | |
| 1778 | return NULL; |
| 1779 | } |
| 1780 | |
| 1781 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1782 | /** |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1783 | * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result |
| 1784 | * @res: Scan result entry |
| 1785 | * @vendor_type: Vendor type (four octets starting the IE payload) |
| 1786 | * Returns: Pointer to the information element (id field) or %NULL if not found |
| 1787 | * |
| 1788 | * This function returns the first matching information element in the scan |
| 1789 | * result. |
| 1790 | * |
| 1791 | * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only |
| 1792 | * from Beacon frames instead of either Beacon or Probe Response frames. |
| 1793 | */ |
| 1794 | const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res, |
| 1795 | u32 vendor_type) |
| 1796 | { |
| 1797 | const u8 *end, *pos; |
| 1798 | |
| 1799 | if (res->beacon_ie_len == 0) |
| 1800 | return NULL; |
| 1801 | |
| 1802 | pos = (const u8 *) (res + 1); |
| 1803 | pos += res->ie_len; |
| 1804 | end = pos + res->beacon_ie_len; |
| 1805 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1806 | while (end - pos > 1) { |
| 1807 | if (2 + pos[1] > end - pos) |
Dmitry Shmidt | 9657139 | 2013-10-14 12:54:46 -0700 | [diff] [blame] | 1808 | break; |
| 1809 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && |
| 1810 | vendor_type == WPA_GET_BE32(&pos[2])) |
| 1811 | return pos; |
| 1812 | pos += 2 + pos[1]; |
| 1813 | } |
| 1814 | |
| 1815 | return NULL; |
| 1816 | } |
| 1817 | |
| 1818 | |
| 1819 | /** |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 1820 | * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result |
| 1821 | * @res: Scan result entry |
| 1822 | * @vendor_type: Vendor type (four octets starting the IE payload) |
| 1823 | * Returns: Pointer to the information element payload or %NULL if not found |
| 1824 | * |
| 1825 | * This function returns concatenated payload of possibly fragmented vendor |
| 1826 | * specific information elements in the scan result. The caller is responsible |
| 1827 | * for freeing the returned buffer. |
| 1828 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1829 | struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res, |
| 1830 | u32 vendor_type) |
| 1831 | { |
| 1832 | struct wpabuf *buf; |
| 1833 | const u8 *end, *pos; |
| 1834 | |
| 1835 | buf = wpabuf_alloc(res->ie_len); |
| 1836 | if (buf == NULL) |
| 1837 | return NULL; |
| 1838 | |
| 1839 | pos = (const u8 *) (res + 1); |
| 1840 | end = pos + res->ie_len; |
| 1841 | |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1842 | while (end - pos > 1) { |
| 1843 | if (2 + pos[1] > end - pos) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1844 | break; |
| 1845 | if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 && |
| 1846 | vendor_type == WPA_GET_BE32(&pos[2])) |
| 1847 | wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4); |
| 1848 | pos += 2 + pos[1]; |
| 1849 | } |
| 1850 | |
| 1851 | if (wpabuf_len(buf) == 0) { |
| 1852 | wpabuf_free(buf); |
| 1853 | buf = NULL; |
| 1854 | } |
| 1855 | |
| 1856 | return buf; |
| 1857 | } |
| 1858 | |
| 1859 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1860 | /* |
| 1861 | * Channels with a great SNR can operate at full rate. What is a great SNR? |
| 1862 | * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general |
| 1863 | * rule of thumb is that any SNR above 20 is good." This one |
| 1864 | * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23 |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1865 | * recommends 25 as a minimum SNR for 54 Mbps data rate. The estimates used in |
| 1866 | * scan_est_throughput() allow even smaller SNR values for the maximum rates |
| 1867 | * (21 for 54 Mbps, 22 for VHT80 MCS9, 24 for HT40 and HT20 MCS7). Use 25 as a |
| 1868 | * somewhat conservative value here. |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1869 | */ |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1870 | #define GREAT_SNR 25 |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1871 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1872 | #define IS_5GHZ(n) (n > 4000) |
| 1873 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1874 | /* Compare function for sorting scan results. Return >0 if @b is considered |
| 1875 | * better. */ |
| 1876 | static int wpa_scan_result_compar(const void *a, const void *b) |
| 1877 | { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1878 | #define MIN(a,b) a < b ? a : b |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1879 | struct wpa_scan_res **_wa = (void *) a; |
| 1880 | struct wpa_scan_res **_wb = (void *) b; |
| 1881 | struct wpa_scan_res *wa = *_wa; |
| 1882 | struct wpa_scan_res *wb = *_wb; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1883 | int wpa_a, wpa_b; |
| 1884 | int snr_a, snr_b, snr_a_full, snr_b_full; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1885 | |
| 1886 | /* WPA/WPA2 support preferred */ |
| 1887 | wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL || |
| 1888 | wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL; |
| 1889 | wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL || |
| 1890 | wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL; |
| 1891 | |
| 1892 | if (wpa_b && !wpa_a) |
| 1893 | return 1; |
| 1894 | if (!wpa_b && wpa_a) |
| 1895 | return -1; |
| 1896 | |
| 1897 | /* privacy support preferred */ |
| 1898 | if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 && |
| 1899 | (wb->caps & IEEE80211_CAP_PRIVACY)) |
| 1900 | return 1; |
| 1901 | if ((wa->caps & IEEE80211_CAP_PRIVACY) && |
| 1902 | (wb->caps & IEEE80211_CAP_PRIVACY) == 0) |
| 1903 | return -1; |
| 1904 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1905 | if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1906 | snr_a_full = wa->snr; |
| 1907 | snr_a = MIN(wa->snr, GREAT_SNR); |
| 1908 | snr_b_full = wb->snr; |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 1909 | snr_b = MIN(wb->snr, GREAT_SNR); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1910 | } else { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1911 | /* Level is not in dBm, so we can't calculate |
| 1912 | * SNR. Just use raw level (units unknown). */ |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1913 | snr_a = snr_a_full = wa->level; |
| 1914 | snr_b = snr_b_full = wb->level; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1915 | } |
| 1916 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1917 | /* if SNR is close, decide by max rate or frequency band */ |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1918 | if (snr_a && snr_b && abs(snr_b - snr_a) < 7) { |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1919 | if (wa->est_throughput != wb->est_throughput) |
| 1920 | return wb->est_throughput - wa->est_throughput; |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 1921 | } |
| 1922 | if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) || |
| 1923 | (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) { |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1924 | if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq)) |
| 1925 | return IS_5GHZ(wa->freq) ? -1 : 1; |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1926 | } |
| 1927 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1928 | /* all things being equal, use SNR; if SNRs are |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1929 | * identical, use quality values since some drivers may only report |
| 1930 | * that value and leave the signal level zero */ |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1931 | if (snr_b_full == snr_a_full) |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1932 | return wb->qual - wa->qual; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 1933 | return snr_b_full - snr_a_full; |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1934 | #undef MIN |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | |
| 1938 | #ifdef CONFIG_WPS |
| 1939 | /* Compare function for sorting scan results when searching a WPS AP for |
| 1940 | * provisioning. Return >0 if @b is considered better. */ |
| 1941 | static int wpa_scan_result_wps_compar(const void *a, const void *b) |
| 1942 | { |
| 1943 | struct wpa_scan_res **_wa = (void *) a; |
| 1944 | struct wpa_scan_res **_wb = (void *) b; |
| 1945 | struct wpa_scan_res *wa = *_wa; |
| 1946 | struct wpa_scan_res *wb = *_wb; |
| 1947 | int uses_wps_a, uses_wps_b; |
| 1948 | struct wpabuf *wps_a, *wps_b; |
| 1949 | int res; |
| 1950 | |
| 1951 | /* Optimization - check WPS IE existence before allocated memory and |
| 1952 | * doing full reassembly. */ |
| 1953 | uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL; |
| 1954 | uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL; |
| 1955 | if (uses_wps_a && !uses_wps_b) |
| 1956 | return -1; |
| 1957 | if (!uses_wps_a && uses_wps_b) |
| 1958 | return 1; |
| 1959 | |
| 1960 | if (uses_wps_a && uses_wps_b) { |
| 1961 | wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE); |
| 1962 | wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE); |
| 1963 | res = wps_ap_priority_compar(wps_a, wps_b); |
| 1964 | wpabuf_free(wps_a); |
| 1965 | wpabuf_free(wps_b); |
| 1966 | if (res) |
| 1967 | return res; |
| 1968 | } |
| 1969 | |
| 1970 | /* |
| 1971 | * Do not use current AP security policy as a sorting criteria during |
| 1972 | * WPS provisioning step since the AP may get reconfigured at the |
| 1973 | * completion of provisioning. |
| 1974 | */ |
| 1975 | |
| 1976 | /* all things being equal, use signal level; if signal levels are |
| 1977 | * identical, use quality values since some drivers may only report |
| 1978 | * that value and leave the signal level zero */ |
| 1979 | if (wb->level == wa->level) |
| 1980 | return wb->qual - wa->qual; |
| 1981 | return wb->level - wa->level; |
| 1982 | } |
| 1983 | #endif /* CONFIG_WPS */ |
| 1984 | |
| 1985 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 1986 | static void dump_scan_res(struct wpa_scan_results *scan_res) |
| 1987 | { |
| 1988 | #ifndef CONFIG_NO_STDOUT_DEBUG |
| 1989 | size_t i; |
| 1990 | |
| 1991 | if (scan_res->res == NULL || scan_res->num == 0) |
| 1992 | return; |
| 1993 | |
| 1994 | wpa_printf(MSG_EXCESSIVE, "Sorted scan results"); |
| 1995 | |
| 1996 | for (i = 0; i < scan_res->num; i++) { |
| 1997 | struct wpa_scan_res *r = scan_res->res[i]; |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 1998 | u8 *pos; |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 1999 | if (r->flags & WPA_SCAN_LEVEL_DBM) { |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2000 | int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID); |
| 2001 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2002 | wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d " |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2003 | "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u est=%u", |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2004 | MAC2STR(r->bssid), r->freq, r->qual, |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2005 | r->noise, noise_valid ? "" : "~", r->level, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2006 | r->snr, r->snr >= GREAT_SNR ? "*" : "", |
| 2007 | r->flags, |
| 2008 | r->age, r->est_throughput); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2009 | } else { |
| 2010 | wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d " |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2011 | "noise=%d level=%d flags=0x%x age=%u est=%u", |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2012 | MAC2STR(r->bssid), r->freq, r->qual, |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2013 | r->noise, r->level, r->flags, r->age, |
| 2014 | r->est_throughput); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2015 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2016 | pos = (u8 *) (r + 1); |
| 2017 | if (r->ie_len) |
| 2018 | wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len); |
| 2019 | pos += r->ie_len; |
| 2020 | if (r->beacon_ie_len) |
| 2021 | wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs", |
| 2022 | pos, r->beacon_ie_len); |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2023 | } |
| 2024 | #endif /* CONFIG_NO_STDOUT_DEBUG */ |
| 2025 | } |
| 2026 | |
| 2027 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2028 | /** |
| 2029 | * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed |
| 2030 | * @wpa_s: Pointer to wpa_supplicant data |
| 2031 | * @bssid: BSSID to check |
| 2032 | * Returns: 0 if the BSSID is filtered or 1 if not |
| 2033 | * |
| 2034 | * This function is used to filter out specific BSSIDs from scan reslts mainly |
| 2035 | * for testing purposes (SET bssid_filter ctrl_iface command). |
| 2036 | */ |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2037 | int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s, |
| 2038 | const u8 *bssid) |
| 2039 | { |
| 2040 | size_t i; |
| 2041 | |
| 2042 | if (wpa_s->bssid_filter == NULL) |
| 2043 | return 1; |
| 2044 | |
| 2045 | for (i = 0; i < wpa_s->bssid_filter_count; i++) { |
| 2046 | if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid, |
| 2047 | ETH_ALEN) == 0) |
| 2048 | return 1; |
| 2049 | } |
| 2050 | |
| 2051 | return 0; |
| 2052 | } |
| 2053 | |
| 2054 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2055 | void filter_scan_res(struct wpa_supplicant *wpa_s, |
| 2056 | struct wpa_scan_results *res) |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2057 | { |
| 2058 | size_t i, j; |
| 2059 | |
| 2060 | if (wpa_s->bssid_filter == NULL) |
| 2061 | return; |
| 2062 | |
| 2063 | for (i = 0, j = 0; i < res->num; i++) { |
| 2064 | if (wpa_supplicant_filter_bssid_match(wpa_s, |
| 2065 | res->res[i]->bssid)) { |
| 2066 | res->res[j++] = res->res[i]; |
| 2067 | } else { |
| 2068 | os_free(res->res[i]); |
| 2069 | res->res[i] = NULL; |
| 2070 | } |
| 2071 | } |
| 2072 | |
| 2073 | if (res->num != j) { |
| 2074 | wpa_printf(MSG_DEBUG, "Filtered out %d scan results", |
| 2075 | (int) (res->num - j)); |
| 2076 | res->num = j; |
| 2077 | } |
| 2078 | } |
| 2079 | |
| 2080 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2081 | /* |
| 2082 | * Noise floor values to use when we have signal strength |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2083 | * measurements, but no noise floor measurements. These values were |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2084 | * measured in an office environment with many APs. |
| 2085 | */ |
| 2086 | #define DEFAULT_NOISE_FLOOR_2GHZ (-89) |
| 2087 | #define DEFAULT_NOISE_FLOOR_5GHZ (-92) |
| 2088 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2089 | void scan_snr(struct wpa_scan_res *res) |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2090 | { |
| 2091 | if (res->flags & WPA_SCAN_NOISE_INVALID) { |
| 2092 | res->noise = IS_5GHZ(res->freq) ? |
| 2093 | DEFAULT_NOISE_FLOOR_5GHZ : |
| 2094 | DEFAULT_NOISE_FLOOR_2GHZ; |
| 2095 | } |
| 2096 | |
| 2097 | if (res->flags & WPA_SCAN_LEVEL_DBM) { |
| 2098 | res->snr = res->level - res->noise; |
| 2099 | } else { |
| 2100 | /* Level is not in dBm, so we can't calculate |
| 2101 | * SNR. Just use raw level (units unknown). */ |
| 2102 | res->snr = res->level; |
| 2103 | } |
| 2104 | } |
| 2105 | |
| 2106 | |
| 2107 | static unsigned int max_ht20_rate(int snr) |
| 2108 | { |
| 2109 | if (snr < 6) |
| 2110 | return 6500; /* HT20 MCS0 */ |
| 2111 | if (snr < 8) |
| 2112 | return 13000; /* HT20 MCS1 */ |
| 2113 | if (snr < 13) |
| 2114 | return 19500; /* HT20 MCS2 */ |
| 2115 | if (snr < 17) |
| 2116 | return 26000; /* HT20 MCS3 */ |
| 2117 | if (snr < 20) |
| 2118 | return 39000; /* HT20 MCS4 */ |
| 2119 | if (snr < 23) |
| 2120 | return 52000; /* HT20 MCS5 */ |
| 2121 | if (snr < 24) |
| 2122 | return 58500; /* HT20 MCS6 */ |
| 2123 | return 65000; /* HT20 MCS7 */ |
| 2124 | } |
| 2125 | |
| 2126 | |
| 2127 | static unsigned int max_ht40_rate(int snr) |
| 2128 | { |
| 2129 | if (snr < 3) |
| 2130 | return 13500; /* HT40 MCS0 */ |
| 2131 | if (snr < 6) |
| 2132 | return 27000; /* HT40 MCS1 */ |
| 2133 | if (snr < 10) |
| 2134 | return 40500; /* HT40 MCS2 */ |
| 2135 | if (snr < 15) |
| 2136 | return 54000; /* HT40 MCS3 */ |
| 2137 | if (snr < 17) |
| 2138 | return 81000; /* HT40 MCS4 */ |
| 2139 | if (snr < 22) |
| 2140 | return 108000; /* HT40 MCS5 */ |
Dmitry Shmidt | 203eadb | 2015-03-05 14:16:04 -0800 | [diff] [blame] | 2141 | if (snr < 24) |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2142 | return 121500; /* HT40 MCS6 */ |
| 2143 | return 135000; /* HT40 MCS7 */ |
| 2144 | } |
| 2145 | |
| 2146 | |
| 2147 | static unsigned int max_vht80_rate(int snr) |
| 2148 | { |
| 2149 | if (snr < 1) |
| 2150 | return 0; |
| 2151 | if (snr < 2) |
| 2152 | return 29300; /* VHT80 MCS0 */ |
| 2153 | if (snr < 5) |
| 2154 | return 58500; /* VHT80 MCS1 */ |
| 2155 | if (snr < 9) |
| 2156 | return 87800; /* VHT80 MCS2 */ |
| 2157 | if (snr < 11) |
| 2158 | return 117000; /* VHT80 MCS3 */ |
| 2159 | if (snr < 15) |
| 2160 | return 175500; /* VHT80 MCS4 */ |
| 2161 | if (snr < 16) |
| 2162 | return 234000; /* VHT80 MCS5 */ |
| 2163 | if (snr < 18) |
| 2164 | return 263300; /* VHT80 MCS6 */ |
| 2165 | if (snr < 20) |
| 2166 | return 292500; /* VHT80 MCS7 */ |
| 2167 | if (snr < 22) |
| 2168 | return 351000; /* VHT80 MCS8 */ |
| 2169 | return 390000; /* VHT80 MCS9 */ |
| 2170 | } |
| 2171 | |
| 2172 | |
Dmitry Shmidt | 849734c | 2016-05-27 09:59:01 -0700 | [diff] [blame] | 2173 | void scan_est_throughput(struct wpa_supplicant *wpa_s, |
| 2174 | struct wpa_scan_res *res) |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2175 | { |
| 2176 | enum local_hw_capab capab = wpa_s->hw_capab; |
| 2177 | int rate; /* max legacy rate in 500 kb/s units */ |
| 2178 | const u8 *ie; |
| 2179 | unsigned int est, tmp; |
| 2180 | int snr = res->snr; |
| 2181 | |
| 2182 | if (res->est_throughput) |
| 2183 | return; |
| 2184 | |
| 2185 | /* Get maximum legacy rate */ |
| 2186 | rate = wpa_scan_get_max_rate(res); |
| 2187 | |
| 2188 | /* Limit based on estimated SNR */ |
| 2189 | if (rate > 1 * 2 && snr < 1) |
| 2190 | rate = 1 * 2; |
| 2191 | else if (rate > 2 * 2 && snr < 4) |
| 2192 | rate = 2 * 2; |
| 2193 | else if (rate > 6 * 2 && snr < 5) |
| 2194 | rate = 6 * 2; |
| 2195 | else if (rate > 9 * 2 && snr < 6) |
| 2196 | rate = 9 * 2; |
| 2197 | else if (rate > 12 * 2 && snr < 7) |
| 2198 | rate = 12 * 2; |
| 2199 | else if (rate > 18 * 2 && snr < 10) |
| 2200 | rate = 18 * 2; |
| 2201 | else if (rate > 24 * 2 && snr < 11) |
| 2202 | rate = 24 * 2; |
| 2203 | else if (rate > 36 * 2 && snr < 15) |
| 2204 | rate = 36 * 2; |
| 2205 | else if (rate > 48 * 2 && snr < 19) |
| 2206 | rate = 48 * 2; |
| 2207 | else if (rate > 54 * 2 && snr < 21) |
| 2208 | rate = 54 * 2; |
| 2209 | est = rate * 500; |
| 2210 | |
| 2211 | if (capab == CAPAB_HT || capab == CAPAB_HT40 || capab == CAPAB_VHT) { |
| 2212 | ie = wpa_scan_get_ie(res, WLAN_EID_HT_CAP); |
| 2213 | if (ie) { |
| 2214 | tmp = max_ht20_rate(snr); |
| 2215 | if (tmp > est) |
| 2216 | est = tmp; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | if (capab == CAPAB_HT40 || capab == CAPAB_VHT) { |
| 2221 | ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION); |
| 2222 | if (ie && ie[1] >= 2 && |
| 2223 | (ie[3] & HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) { |
| 2224 | tmp = max_ht40_rate(snr); |
| 2225 | if (tmp > est) |
| 2226 | est = tmp; |
| 2227 | } |
| 2228 | } |
| 2229 | |
| 2230 | if (capab == CAPAB_VHT) { |
| 2231 | /* Use +1 to assume VHT is always faster than HT */ |
| 2232 | ie = wpa_scan_get_ie(res, WLAN_EID_VHT_CAP); |
| 2233 | if (ie) { |
| 2234 | tmp = max_ht20_rate(snr) + 1; |
| 2235 | if (tmp > est) |
| 2236 | est = tmp; |
| 2237 | |
| 2238 | ie = wpa_scan_get_ie(res, WLAN_EID_HT_OPERATION); |
| 2239 | if (ie && ie[1] >= 2 && |
| 2240 | (ie[3] & |
| 2241 | HT_INFO_HT_PARAM_SECONDARY_CHNL_OFF_MASK)) { |
| 2242 | tmp = max_ht40_rate(snr) + 1; |
| 2243 | if (tmp > est) |
| 2244 | est = tmp; |
| 2245 | } |
| 2246 | |
| 2247 | ie = wpa_scan_get_ie(res, WLAN_EID_VHT_OPERATION); |
| 2248 | if (ie && ie[1] >= 1 && |
| 2249 | (ie[2] & VHT_OPMODE_CHANNEL_WIDTH_MASK)) { |
| 2250 | tmp = max_vht80_rate(snr) + 1; |
| 2251 | if (tmp > est) |
| 2252 | est = tmp; |
| 2253 | } |
| 2254 | } |
| 2255 | } |
| 2256 | |
| 2257 | /* TODO: channel utilization and AP load (e.g., from AP Beacon) */ |
| 2258 | |
| 2259 | res->est_throughput = est; |
| 2260 | } |
| 2261 | |
| 2262 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2263 | /** |
| 2264 | * wpa_supplicant_get_scan_results - Get scan results |
| 2265 | * @wpa_s: Pointer to wpa_supplicant data |
| 2266 | * @info: Information about what was scanned or %NULL if not available |
| 2267 | * @new_scan: Whether a new scan was performed |
| 2268 | * Returns: Scan results, %NULL on failure |
| 2269 | * |
| 2270 | * This function request the current scan results from the driver and updates |
| 2271 | * the local BSS list wpa_s->bss. The caller is responsible for freeing the |
| 2272 | * results with wpa_scan_results_free(). |
| 2273 | */ |
| 2274 | struct wpa_scan_results * |
| 2275 | wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s, |
| 2276 | struct scan_info *info, int new_scan) |
| 2277 | { |
| 2278 | struct wpa_scan_results *scan_res; |
| 2279 | size_t i; |
| 2280 | int (*compar)(const void *, const void *) = wpa_scan_result_compar; |
| 2281 | |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2282 | scan_res = wpa_drv_get_scan_results2(wpa_s); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2283 | if (scan_res == NULL) { |
| 2284 | wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results"); |
| 2285 | return NULL; |
| 2286 | } |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 2287 | if (scan_res->fetch_time.sec == 0) { |
| 2288 | /* |
| 2289 | * Make sure we have a valid timestamp if the driver wrapper |
| 2290 | * does not set this. |
| 2291 | */ |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2292 | os_get_reltime(&scan_res->fetch_time); |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 2293 | } |
Dmitry Shmidt | 0494959 | 2012-07-19 12:16:46 -0700 | [diff] [blame] | 2294 | filter_scan_res(wpa_s, scan_res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2295 | |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2296 | for (i = 0; i < scan_res->num; i++) { |
| 2297 | struct wpa_scan_res *scan_res_item = scan_res->res[i]; |
| 2298 | |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2299 | scan_snr(scan_res_item); |
| 2300 | scan_est_throughput(wpa_s, scan_res_item); |
Dmitry Shmidt | 216983b | 2015-02-06 10:50:36 -0800 | [diff] [blame] | 2301 | } |
| 2302 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2303 | #ifdef CONFIG_WPS |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2304 | if (wpas_wps_searching(wpa_s)) { |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2305 | wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS " |
| 2306 | "provisioning rules"); |
| 2307 | compar = wpa_scan_result_wps_compar; |
| 2308 | } |
| 2309 | #endif /* CONFIG_WPS */ |
| 2310 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2311 | if (scan_res->res) { |
| 2312 | qsort(scan_res->res, scan_res->num, |
| 2313 | sizeof(struct wpa_scan_res *), compar); |
| 2314 | } |
Dmitry Shmidt | 1f69aa5 | 2012-01-24 16:10:04 -0800 | [diff] [blame] | 2315 | dump_scan_res(scan_res); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2316 | |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 2317 | if (wpa_s->ignore_post_flush_scan_res) { |
| 2318 | /* FLUSH command aborted an ongoing scan and these are the |
| 2319 | * results from the aborted scan. Do not process the results to |
| 2320 | * maintain flushed state. */ |
| 2321 | wpa_dbg(wpa_s, MSG_DEBUG, |
| 2322 | "Do not update BSS table based on pending post-FLUSH scan results"); |
| 2323 | wpa_s->ignore_post_flush_scan_res = 0; |
| 2324 | return scan_res; |
| 2325 | } |
| 2326 | |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2327 | wpa_bss_update_start(wpa_s); |
| 2328 | for (i = 0; i < scan_res->num; i++) |
Dmitry Shmidt | f862328 | 2013-02-20 14:34:59 -0800 | [diff] [blame] | 2329 | wpa_bss_update_scan_res(wpa_s, scan_res->res[i], |
| 2330 | &scan_res->fetch_time); |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2331 | wpa_bss_update_end(wpa_s, info, new_scan); |
| 2332 | |
| 2333 | return scan_res; |
| 2334 | } |
| 2335 | |
| 2336 | |
Dmitry Shmidt | a54fa5f | 2013-01-15 13:53:35 -0800 | [diff] [blame] | 2337 | /** |
| 2338 | * wpa_supplicant_update_scan_results - Update scan results from the driver |
| 2339 | * @wpa_s: Pointer to wpa_supplicant data |
| 2340 | * Returns: 0 on success, -1 on failure |
| 2341 | * |
| 2342 | * This function updates the BSS table within wpa_supplicant based on the |
| 2343 | * currently available scan results from the driver without requesting a new |
| 2344 | * scan. This is used in cases where the driver indicates an association |
| 2345 | * (including roaming within ESS) and wpa_supplicant does not yet have the |
| 2346 | * needed information to complete the connection (e.g., to perform validation |
| 2347 | * steps in 4-way handshake). |
| 2348 | */ |
Dmitry Shmidt | 8d520ff | 2011-05-09 14:06:53 -0700 | [diff] [blame] | 2349 | int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s) |
| 2350 | { |
| 2351 | struct wpa_scan_results *scan_res; |
| 2352 | scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0); |
| 2353 | if (scan_res == NULL) |
| 2354 | return -1; |
| 2355 | wpa_scan_results_free(scan_res); |
| 2356 | |
| 2357 | return 0; |
| 2358 | } |
Dmitry Shmidt | 3a787e6 | 2013-01-17 10:32:35 -0800 | [diff] [blame] | 2359 | |
| 2360 | |
| 2361 | /** |
| 2362 | * scan_only_handler - Reports scan results |
| 2363 | */ |
| 2364 | void scan_only_handler(struct wpa_supplicant *wpa_s, |
| 2365 | struct wpa_scan_results *scan_res) |
| 2366 | { |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 2367 | wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received"); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2368 | if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && |
| 2369 | wpa_s->manual_scan_use_id && wpa_s->own_scan_running) { |
| 2370 | wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u", |
| 2371 | wpa_s->manual_scan_id); |
| 2372 | wpa_s->manual_scan_use_id = 0; |
| 2373 | } else { |
| 2374 | wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS); |
| 2375 | } |
Dmitry Shmidt | 3a787e6 | 2013-01-17 10:32:35 -0800 | [diff] [blame] | 2376 | wpas_notify_scan_results(wpa_s); |
Dmitry Shmidt | 4b9d52f | 2013-02-05 17:44:43 -0800 | [diff] [blame] | 2377 | wpas_notify_scan_done(wpa_s, 1); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2378 | if (wpa_s->scan_work) { |
| 2379 | struct wpa_radio_work *work = wpa_s->scan_work; |
| 2380 | wpa_s->scan_work = NULL; |
| 2381 | radio_work_done(work); |
| 2382 | } |
Dmitry Shmidt | d80a401 | 2015-11-05 16:35:40 -0800 | [diff] [blame] | 2383 | |
| 2384 | if (wpa_s->wpa_state == WPA_SCANNING) |
| 2385 | wpa_supplicant_set_state(wpa_s, wpa_s->scan_prev_wpa_state); |
Dmitry Shmidt | 3a787e6 | 2013-01-17 10:32:35 -0800 | [diff] [blame] | 2386 | } |
Dmitry Shmidt | 37d4d6a | 2013-03-18 13:09:42 -0700 | [diff] [blame] | 2387 | |
| 2388 | |
| 2389 | int wpas_scan_scheduled(struct wpa_supplicant *wpa_s) |
| 2390 | { |
| 2391 | return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL); |
| 2392 | } |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2393 | |
| 2394 | |
| 2395 | struct wpa_driver_scan_params * |
| 2396 | wpa_scan_clone_params(const struct wpa_driver_scan_params *src) |
| 2397 | { |
| 2398 | struct wpa_driver_scan_params *params; |
| 2399 | size_t i; |
| 2400 | u8 *n; |
| 2401 | |
| 2402 | params = os_zalloc(sizeof(*params)); |
| 2403 | if (params == NULL) |
| 2404 | return NULL; |
| 2405 | |
| 2406 | for (i = 0; i < src->num_ssids; i++) { |
| 2407 | if (src->ssids[i].ssid) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2408 | n = os_memdup(src->ssids[i].ssid, |
| 2409 | src->ssids[i].ssid_len); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2410 | if (n == NULL) |
| 2411 | goto failed; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2412 | params->ssids[i].ssid = n; |
| 2413 | params->ssids[i].ssid_len = src->ssids[i].ssid_len; |
| 2414 | } |
| 2415 | } |
| 2416 | params->num_ssids = src->num_ssids; |
| 2417 | |
| 2418 | if (src->extra_ies) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2419 | n = os_memdup(src->extra_ies, src->extra_ies_len); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2420 | if (n == NULL) |
| 2421 | goto failed; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2422 | params->extra_ies = n; |
| 2423 | params->extra_ies_len = src->extra_ies_len; |
| 2424 | } |
| 2425 | |
| 2426 | if (src->freqs) { |
| 2427 | int len = int_array_len(src->freqs); |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2428 | params->freqs = os_memdup(src->freqs, (len + 1) * sizeof(int)); |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2429 | if (params->freqs == NULL) |
| 2430 | goto failed; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2431 | } |
| 2432 | |
| 2433 | if (src->filter_ssids) { |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2434 | params->filter_ssids = os_memdup(src->filter_ssids, |
| 2435 | sizeof(*params->filter_ssids) * |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2436 | src->num_filter_ssids); |
| 2437 | if (params->filter_ssids == NULL) |
| 2438 | goto failed; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2439 | params->num_filter_ssids = src->num_filter_ssids; |
| 2440 | } |
| 2441 | |
| 2442 | params->filter_rssi = src->filter_rssi; |
| 2443 | params->p2p_probe = src->p2p_probe; |
| 2444 | params->only_new_results = src->only_new_results; |
Dmitry Shmidt | 2271d3f | 2014-06-23 12:16:31 -0700 | [diff] [blame] | 2445 | params->low_priority = src->low_priority; |
Dmitry Shmidt | 2933359 | 2017-01-09 12:27:11 -0800 | [diff] [blame] | 2446 | params->duration = src->duration; |
| 2447 | params->duration_mandatory = src->duration_mandatory; |
Hai Shalom | ce48b4a | 2018-09-05 11:41:35 -0700 | [diff] [blame] | 2448 | params->oce_scan = src->oce_scan; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2449 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2450 | if (src->sched_scan_plans_num > 0) { |
| 2451 | params->sched_scan_plans = |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2452 | os_memdup(src->sched_scan_plans, |
| 2453 | sizeof(*src->sched_scan_plans) * |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2454 | src->sched_scan_plans_num); |
| 2455 | if (!params->sched_scan_plans) |
| 2456 | goto failed; |
| 2457 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2458 | params->sched_scan_plans_num = src->sched_scan_plans_num; |
| 2459 | } |
| 2460 | |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2461 | if (src->mac_addr_rand) { |
| 2462 | params->mac_addr_rand = src->mac_addr_rand; |
| 2463 | |
| 2464 | if (src->mac_addr && src->mac_addr_mask) { |
| 2465 | u8 *mac_addr; |
| 2466 | |
| 2467 | mac_addr = os_malloc(2 * ETH_ALEN); |
| 2468 | if (!mac_addr) |
| 2469 | goto failed; |
| 2470 | |
| 2471 | os_memcpy(mac_addr, src->mac_addr, ETH_ALEN); |
| 2472 | os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask, |
| 2473 | ETH_ALEN); |
| 2474 | params->mac_addr = mac_addr; |
| 2475 | params->mac_addr_mask = mac_addr + ETH_ALEN; |
| 2476 | } |
| 2477 | } |
Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2478 | |
| 2479 | if (src->bssid) { |
| 2480 | u8 *bssid; |
| 2481 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2482 | bssid = os_memdup(src->bssid, ETH_ALEN); |
Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2483 | if (!bssid) |
| 2484 | goto failed; |
Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2485 | params->bssid = bssid; |
| 2486 | } |
| 2487 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2488 | params->relative_rssi_set = src->relative_rssi_set; |
| 2489 | params->relative_rssi = src->relative_rssi; |
| 2490 | params->relative_adjust_band = src->relative_adjust_band; |
| 2491 | params->relative_adjust_rssi = src->relative_adjust_rssi; |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2492 | return params; |
| 2493 | |
| 2494 | failed: |
| 2495 | wpa_scan_free_params(params); |
| 2496 | return NULL; |
| 2497 | } |
| 2498 | |
| 2499 | |
| 2500 | void wpa_scan_free_params(struct wpa_driver_scan_params *params) |
| 2501 | { |
| 2502 | size_t i; |
| 2503 | |
| 2504 | if (params == NULL) |
| 2505 | return; |
| 2506 | |
| 2507 | for (i = 0; i < params->num_ssids; i++) |
| 2508 | os_free((u8 *) params->ssids[i].ssid); |
| 2509 | os_free((u8 *) params->extra_ies); |
| 2510 | os_free(params->freqs); |
| 2511 | os_free(params->filter_ssids); |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2512 | os_free(params->sched_scan_plans); |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2513 | |
| 2514 | /* |
| 2515 | * Note: params->mac_addr_mask points to same memory allocation and |
| 2516 | * must not be freed separately. |
| 2517 | */ |
| 2518 | os_free((u8 *) params->mac_addr); |
| 2519 | |
Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2520 | os_free((u8 *) params->bssid); |
| 2521 | |
Dmitry Shmidt | fb79edc | 2014-01-10 10:45:54 -0800 | [diff] [blame] | 2522 | os_free(params); |
| 2523 | } |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2524 | |
| 2525 | |
| 2526 | int wpas_start_pno(struct wpa_supplicant *wpa_s) |
| 2527 | { |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2528 | int ret, prio; |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 2529 | size_t i, num_ssid, num_match_ssid; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2530 | struct wpa_ssid *ssid; |
| 2531 | struct wpa_driver_scan_params params; |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2532 | struct sched_scan_plan scan_plan; |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2533 | unsigned int max_sched_scan_ssids; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2534 | |
| 2535 | if (!wpa_s->sched_scan_supported) |
| 2536 | return -1; |
| 2537 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2538 | if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS) |
| 2539 | max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS; |
| 2540 | else |
| 2541 | max_sched_scan_ssids = wpa_s->max_sched_scan_ssids; |
| 2542 | if (max_sched_scan_ssids < 1) |
| 2543 | return -1; |
| 2544 | |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2545 | if (wpa_s->pno || wpa_s->pno_sched_pending) |
| 2546 | return 0; |
| 2547 | |
| 2548 | if ((wpa_s->wpa_state > WPA_SCANNING) && |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2549 | (wpa_s->wpa_state < WPA_COMPLETED)) { |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2550 | wpa_printf(MSG_ERROR, "PNO: In assoc process"); |
| 2551 | return -EAGAIN; |
| 2552 | } |
| 2553 | |
| 2554 | if (wpa_s->wpa_state == WPA_SCANNING) { |
| 2555 | wpa_supplicant_cancel_scan(wpa_s); |
| 2556 | if (wpa_s->sched_scanning) { |
| 2557 | wpa_printf(MSG_DEBUG, "Schedule PNO on completion of " |
| 2558 | "ongoing sched scan"); |
| 2559 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 2560 | wpa_s->pno_sched_pending = 1; |
| 2561 | return 0; |
| 2562 | } |
| 2563 | } |
| 2564 | |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2565 | if (wpa_s->sched_scan_stop_req) { |
| 2566 | wpa_printf(MSG_DEBUG, |
| 2567 | "Schedule PNO after previous sched scan has stopped"); |
| 2568 | wpa_s->pno_sched_pending = 1; |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2572 | os_memset(¶ms, 0, sizeof(params)); |
| 2573 | |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 2574 | num_ssid = num_match_ssid = 0; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2575 | ssid = wpa_s->conf->ssid; |
| 2576 | while (ssid) { |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 2577 | if (!wpas_network_disabled(wpa_s, ssid)) { |
| 2578 | num_match_ssid++; |
| 2579 | if (ssid->scan_ssid) |
| 2580 | num_ssid++; |
| 2581 | } |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2582 | ssid = ssid->next; |
| 2583 | } |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 2584 | |
| 2585 | if (num_match_ssid == 0) { |
| 2586 | wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs"); |
| 2587 | return -1; |
| 2588 | } |
| 2589 | |
| 2590 | if (num_match_ssid > num_ssid) { |
| 2591 | params.num_ssids++; /* wildcard */ |
| 2592 | num_ssid++; |
| 2593 | } |
| 2594 | |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2595 | if (num_ssid > max_sched_scan_ssids) { |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2596 | wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from " |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 2597 | "%u", max_sched_scan_ssids, (unsigned int) num_ssid); |
| 2598 | num_ssid = max_sched_scan_ssids; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2599 | } |
| 2600 | |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 2601 | if (num_match_ssid > wpa_s->max_match_sets) { |
| 2602 | num_match_ssid = wpa_s->max_match_sets; |
| 2603 | wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match"); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2604 | } |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 2605 | params.filter_ssids = os_calloc(num_match_ssid, |
| 2606 | sizeof(struct wpa_driver_scan_filter)); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2607 | if (params.filter_ssids == NULL) |
| 2608 | return -1; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2609 | |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2610 | i = 0; |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2611 | prio = 0; |
| 2612 | ssid = wpa_s->conf->pssid[prio]; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2613 | while (ssid) { |
| 2614 | if (!wpas_network_disabled(wpa_s, ssid)) { |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 2615 | if (ssid->scan_ssid && params.num_ssids < num_ssid) { |
| 2616 | params.ssids[params.num_ssids].ssid = |
| 2617 | ssid->ssid; |
| 2618 | params.ssids[params.num_ssids].ssid_len = |
| 2619 | ssid->ssid_len; |
| 2620 | params.num_ssids++; |
| 2621 | } |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2622 | os_memcpy(params.filter_ssids[i].ssid, ssid->ssid, |
| 2623 | ssid->ssid_len); |
| 2624 | params.filter_ssids[i].ssid_len = ssid->ssid_len; |
| 2625 | params.num_filter_ssids++; |
| 2626 | i++; |
Dmitry Shmidt | 6aa8ae4 | 2014-07-07 09:31:33 -0700 | [diff] [blame] | 2627 | if (i == num_match_ssid) |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2628 | break; |
| 2629 | } |
Dmitry Shmidt | 7f65602 | 2015-02-25 14:36:37 -0800 | [diff] [blame] | 2630 | if (ssid->pnext) |
| 2631 | ssid = ssid->pnext; |
| 2632 | else if (prio + 1 == wpa_s->conf->num_prio) |
| 2633 | break; |
| 2634 | else |
| 2635 | ssid = wpa_s->conf->pssid[++prio]; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | if (wpa_s->conf->filter_rssi) |
| 2639 | params.filter_rssi = wpa_s->conf->filter_rssi; |
| 2640 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2641 | if (wpa_s->sched_scan_plans_num) { |
| 2642 | params.sched_scan_plans = wpa_s->sched_scan_plans; |
| 2643 | params.sched_scan_plans_num = wpa_s->sched_scan_plans_num; |
| 2644 | } else { |
| 2645 | /* Set one scan plan that will run infinitely */ |
| 2646 | if (wpa_s->conf->sched_scan_interval) |
| 2647 | scan_plan.interval = wpa_s->conf->sched_scan_interval; |
| 2648 | else |
| 2649 | scan_plan.interval = 10; |
| 2650 | |
| 2651 | scan_plan.iterations = 0; |
| 2652 | params.sched_scan_plans = &scan_plan; |
| 2653 | params.sched_scan_plans_num = 1; |
| 2654 | } |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2655 | |
Dmitry Shmidt | d2986c2 | 2017-10-23 14:22:09 -0700 | [diff] [blame] | 2656 | params.sched_scan_start_delay = wpa_s->conf->sched_scan_start_delay; |
| 2657 | |
Dmitry Shmidt | d11f019 | 2014-03-24 12:09:47 -0700 | [diff] [blame] | 2658 | if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) { |
| 2659 | wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels"); |
| 2660 | params.freqs = wpa_s->manual_sched_scan_freqs; |
| 2661 | } |
| 2662 | |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 2663 | if ((wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) && |
| 2664 | wpa_s->wpa_state <= WPA_SCANNING) { |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2665 | params.mac_addr_rand = 1; |
| 2666 | if (wpa_s->mac_addr_pno) { |
| 2667 | params.mac_addr = wpa_s->mac_addr_pno; |
| 2668 | params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN; |
| 2669 | } |
| 2670 | } |
| 2671 | |
Dmitry Shmidt | ebd93af | 2017-02-21 13:40:44 -0800 | [diff] [blame] | 2672 | wpa_scan_set_relative_rssi_params(wpa_s, ¶ms); |
| 2673 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2674 | ret = wpa_supplicant_start_sched_scan(wpa_s, ¶ms); |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2675 | os_free(params.filter_ssids); |
| 2676 | if (ret == 0) |
| 2677 | wpa_s->pno = 1; |
| 2678 | else |
| 2679 | wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO"); |
| 2680 | return ret; |
| 2681 | } |
| 2682 | |
| 2683 | |
| 2684 | int wpas_stop_pno(struct wpa_supplicant *wpa_s) |
| 2685 | { |
| 2686 | int ret = 0; |
| 2687 | |
| 2688 | if (!wpa_s->pno) |
| 2689 | return 0; |
| 2690 | |
| 2691 | ret = wpa_supplicant_stop_sched_scan(wpa_s); |
Dmitry Shmidt | 9839ecd | 2016-11-07 11:05:47 -0800 | [diff] [blame] | 2692 | wpa_s->sched_scan_stop_req = 1; |
Dmitry Shmidt | 9866086 | 2014-03-11 17:26:21 -0700 | [diff] [blame] | 2693 | |
| 2694 | wpa_s->pno = 0; |
| 2695 | wpa_s->pno_sched_pending = 0; |
| 2696 | |
| 2697 | if (wpa_s->wpa_state == WPA_SCANNING) |
| 2698 | wpa_supplicant_req_scan(wpa_s, 0, 0); |
| 2699 | |
| 2700 | return ret; |
| 2701 | } |
Dmitry Shmidt | 6c0da2b | 2015-01-05 13:08:17 -0800 | [diff] [blame] | 2702 | |
| 2703 | |
| 2704 | void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s, |
| 2705 | unsigned int type) |
| 2706 | { |
| 2707 | type &= MAC_ADDR_RAND_ALL; |
| 2708 | wpa_s->mac_addr_rand_enable &= ~type; |
| 2709 | |
| 2710 | if (type & MAC_ADDR_RAND_SCAN) { |
| 2711 | os_free(wpa_s->mac_addr_scan); |
| 2712 | wpa_s->mac_addr_scan = NULL; |
| 2713 | } |
| 2714 | |
| 2715 | if (type & MAC_ADDR_RAND_SCHED_SCAN) { |
| 2716 | os_free(wpa_s->mac_addr_sched_scan); |
| 2717 | wpa_s->mac_addr_sched_scan = NULL; |
| 2718 | } |
| 2719 | |
| 2720 | if (type & MAC_ADDR_RAND_PNO) { |
| 2721 | os_free(wpa_s->mac_addr_pno); |
| 2722 | wpa_s->mac_addr_pno = NULL; |
| 2723 | } |
| 2724 | } |
| 2725 | |
| 2726 | |
| 2727 | int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s, |
| 2728 | unsigned int type, const u8 *addr, |
| 2729 | const u8 *mask) |
| 2730 | { |
| 2731 | u8 *tmp = NULL; |
| 2732 | |
| 2733 | wpas_mac_addr_rand_scan_clear(wpa_s, type); |
| 2734 | |
| 2735 | if (addr) { |
| 2736 | tmp = os_malloc(2 * ETH_ALEN); |
| 2737 | if (!tmp) |
| 2738 | return -1; |
| 2739 | os_memcpy(tmp, addr, ETH_ALEN); |
| 2740 | os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN); |
| 2741 | } |
| 2742 | |
| 2743 | if (type == MAC_ADDR_RAND_SCAN) { |
| 2744 | wpa_s->mac_addr_scan = tmp; |
| 2745 | } else if (type == MAC_ADDR_RAND_SCHED_SCAN) { |
| 2746 | wpa_s->mac_addr_sched_scan = tmp; |
| 2747 | } else if (type == MAC_ADDR_RAND_PNO) { |
| 2748 | wpa_s->mac_addr_pno = tmp; |
| 2749 | } else { |
| 2750 | wpa_printf(MSG_INFO, |
| 2751 | "scan: Invalid MAC randomization type=0x%x", |
| 2752 | type); |
| 2753 | os_free(tmp); |
| 2754 | return -1; |
| 2755 | } |
| 2756 | |
| 2757 | wpa_s->mac_addr_rand_enable |= type; |
| 2758 | return 0; |
| 2759 | } |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2760 | |
| 2761 | |
| 2762 | int wpas_abort_ongoing_scan(struct wpa_supplicant *wpa_s) |
| 2763 | { |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 2764 | struct wpa_radio_work *work; |
| 2765 | struct wpa_radio *radio = wpa_s->radio; |
Dmitry Shmidt | 9c17526 | 2016-03-03 10:20:07 -0800 | [diff] [blame] | 2766 | |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 2767 | dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) { |
| 2768 | if (work->wpa_s != wpa_s || !work->started || |
| 2769 | (os_strcmp(work->type, "scan") != 0 && |
| 2770 | os_strcmp(work->type, "p2p-scan") != 0)) |
| 2771 | continue; |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2772 | wpa_dbg(wpa_s, MSG_DEBUG, "Abort an ongoing scan"); |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 2773 | return wpa_drv_abort_scan(wpa_s, wpa_s->curr_scan_cookie); |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2774 | } |
| 2775 | |
Dmitry Shmidt | abb90a3 | 2016-12-05 15:34:39 -0800 | [diff] [blame] | 2776 | wpa_dbg(wpa_s, MSG_DEBUG, "No ongoing scan/p2p-scan found to abort"); |
| 2777 | return -1; |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | |
| 2781 | int wpas_sched_scan_plans_set(struct wpa_supplicant *wpa_s, const char *cmd) |
| 2782 | { |
| 2783 | struct sched_scan_plan *scan_plans = NULL; |
| 2784 | const char *token, *context = NULL; |
| 2785 | unsigned int num = 0; |
| 2786 | |
| 2787 | if (!cmd) |
| 2788 | return -1; |
| 2789 | |
| 2790 | if (!cmd[0]) { |
| 2791 | wpa_printf(MSG_DEBUG, "Clear sched scan plans"); |
| 2792 | os_free(wpa_s->sched_scan_plans); |
| 2793 | wpa_s->sched_scan_plans = NULL; |
| 2794 | wpa_s->sched_scan_plans_num = 0; |
| 2795 | return 0; |
| 2796 | } |
| 2797 | |
| 2798 | while ((token = cstr_token(cmd, " ", &context))) { |
| 2799 | int ret; |
| 2800 | struct sched_scan_plan *scan_plan, *n; |
| 2801 | |
| 2802 | n = os_realloc_array(scan_plans, num + 1, sizeof(*scan_plans)); |
| 2803 | if (!n) |
| 2804 | goto fail; |
| 2805 | |
| 2806 | scan_plans = n; |
| 2807 | scan_plan = &scan_plans[num]; |
| 2808 | num++; |
| 2809 | |
| 2810 | ret = sscanf(token, "%u:%u", &scan_plan->interval, |
| 2811 | &scan_plan->iterations); |
| 2812 | if (ret <= 0 || ret > 2 || !scan_plan->interval) { |
| 2813 | wpa_printf(MSG_ERROR, |
| 2814 | "Invalid sched scan plan input: %s", token); |
| 2815 | goto fail; |
| 2816 | } |
| 2817 | |
Dmitry Shmidt | d7ff03d | 2015-12-04 14:49:35 -0800 | [diff] [blame] | 2818 | if (scan_plan->interval > wpa_s->max_sched_scan_plan_interval) { |
| 2819 | wpa_printf(MSG_WARNING, |
| 2820 | "scan plan %u: Scan interval too long(%u), use the maximum allowed(%u)", |
| 2821 | num, scan_plan->interval, |
| 2822 | wpa_s->max_sched_scan_plan_interval); |
| 2823 | scan_plan->interval = |
| 2824 | wpa_s->max_sched_scan_plan_interval; |
| 2825 | } |
| 2826 | |
| 2827 | if (ret == 1) { |
| 2828 | scan_plan->iterations = 0; |
| 2829 | break; |
| 2830 | } |
| 2831 | |
| 2832 | if (!scan_plan->iterations) { |
| 2833 | wpa_printf(MSG_ERROR, |
| 2834 | "scan plan %u: Number of iterations cannot be zero", |
| 2835 | num); |
| 2836 | goto fail; |
| 2837 | } |
| 2838 | |
| 2839 | if (scan_plan->iterations > |
| 2840 | wpa_s->max_sched_scan_plan_iterations) { |
| 2841 | wpa_printf(MSG_WARNING, |
| 2842 | "scan plan %u: Too many iterations(%u), use the maximum allowed(%u)", |
| 2843 | num, scan_plan->iterations, |
| 2844 | wpa_s->max_sched_scan_plan_iterations); |
| 2845 | scan_plan->iterations = |
| 2846 | wpa_s->max_sched_scan_plan_iterations; |
| 2847 | } |
| 2848 | |
| 2849 | wpa_printf(MSG_DEBUG, |
| 2850 | "scan plan %u: interval=%u iterations=%u", |
| 2851 | num, scan_plan->interval, scan_plan->iterations); |
| 2852 | } |
| 2853 | |
| 2854 | if (!scan_plans) { |
| 2855 | wpa_printf(MSG_ERROR, "Invalid scan plans entry"); |
| 2856 | goto fail; |
| 2857 | } |
| 2858 | |
| 2859 | if (cstr_token(cmd, " ", &context) || scan_plans[num - 1].iterations) { |
| 2860 | wpa_printf(MSG_ERROR, |
| 2861 | "All scan plans but the last must specify a number of iterations"); |
| 2862 | goto fail; |
| 2863 | } |
| 2864 | |
| 2865 | wpa_printf(MSG_DEBUG, "scan plan %u (last plan): interval=%u", |
| 2866 | num, scan_plans[num - 1].interval); |
| 2867 | |
| 2868 | if (num > wpa_s->max_sched_scan_plans) { |
| 2869 | wpa_printf(MSG_WARNING, |
| 2870 | "Too many scheduled scan plans (only %u supported)", |
| 2871 | wpa_s->max_sched_scan_plans); |
| 2872 | wpa_printf(MSG_WARNING, |
| 2873 | "Use only the first %u scan plans, and the last one (in infinite loop)", |
| 2874 | wpa_s->max_sched_scan_plans - 1); |
| 2875 | os_memcpy(&scan_plans[wpa_s->max_sched_scan_plans - 1], |
| 2876 | &scan_plans[num - 1], sizeof(*scan_plans)); |
| 2877 | num = wpa_s->max_sched_scan_plans; |
| 2878 | } |
| 2879 | |
| 2880 | os_free(wpa_s->sched_scan_plans); |
| 2881 | wpa_s->sched_scan_plans = scan_plans; |
| 2882 | wpa_s->sched_scan_plans_num = num; |
| 2883 | |
| 2884 | return 0; |
| 2885 | |
| 2886 | fail: |
| 2887 | os_free(scan_plans); |
| 2888 | wpa_printf(MSG_ERROR, "invalid scan plans list"); |
| 2889 | return -1; |
| 2890 | } |
Dmitry Shmidt | aca489e | 2016-09-28 15:44:14 -0700 | [diff] [blame] | 2891 | |
| 2892 | |
| 2893 | /** |
| 2894 | * wpas_scan_reset_sched_scan - Reset sched_scan state |
| 2895 | * @wpa_s: Pointer to wpa_supplicant data |
| 2896 | * |
| 2897 | * This function is used to cancel a running scheduled scan and to reset an |
| 2898 | * internal scan state to continue with a regular scan on the following |
| 2899 | * wpa_supplicant_req_scan() calls. |
| 2900 | */ |
| 2901 | void wpas_scan_reset_sched_scan(struct wpa_supplicant *wpa_s) |
| 2902 | { |
| 2903 | wpa_s->normal_scans = 0; |
| 2904 | if (wpa_s->sched_scanning) { |
| 2905 | wpa_s->sched_scan_timed_out = 0; |
| 2906 | wpa_s->prev_sched_ssid = NULL; |
| 2907 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | |
| 2912 | void wpas_scan_restart_sched_scan(struct wpa_supplicant *wpa_s) |
| 2913 | { |
| 2914 | /* simulate timeout to restart the sched scan */ |
| 2915 | wpa_s->sched_scan_timed_out = 1; |
| 2916 | wpa_s->prev_sched_ssid = NULL; |
| 2917 | wpa_supplicant_cancel_sched_scan(wpa_s); |
| 2918 | } |