scan.c 58 KB

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