scan.c 77 KB

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