scan.c 48 KB

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