scan.c 63 KB

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