scan.c 45 KB

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