scan.c 46 KB

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