scan.c 55 KB

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