scan.c 70 KB

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