scan.c 71 KB

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