scan.c 69 KB

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