scan.c 63 KB

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