scan.c 55 KB

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