scan.c 45 KB

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