scan.c 76 KB

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