scan.c 65 KB

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