scan.c 73 KB

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