scan.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071
  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. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  24. {
  25. struct wpa_ssid *ssid;
  26. union wpa_event_data data;
  27. ssid = wpa_supplicant_get_ssid(wpa_s);
  28. if (ssid == NULL)
  29. return;
  30. if (wpa_s->current_ssid == NULL) {
  31. wpa_s->current_ssid = ssid;
  32. if (wpa_s->current_ssid != NULL)
  33. wpas_notify_network_changed(wpa_s);
  34. }
  35. wpa_supplicant_initiate_eapol(wpa_s);
  36. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
  37. "network - generating associated event");
  38. os_memset(&data, 0, sizeof(data));
  39. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  40. }
  41. #ifdef CONFIG_WPS
  42. static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
  43. enum wps_request_type *req_type)
  44. {
  45. struct wpa_ssid *ssid;
  46. int wps = 0;
  47. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  48. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  49. continue;
  50. wps = 1;
  51. *req_type = wpas_wps_get_req_type(ssid);
  52. if (!ssid->eap.phase1)
  53. continue;
  54. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  55. return 2;
  56. }
  57. #ifdef CONFIG_P2P
  58. if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
  59. !wpa_s->conf->p2p_disabled) {
  60. wpa_s->wps->dev.p2p = 1;
  61. if (!wps) {
  62. wps = 1;
  63. *req_type = WPS_REQ_ENROLLEE_INFO;
  64. }
  65. }
  66. #endif /* CONFIG_P2P */
  67. return wps;
  68. }
  69. #endif /* CONFIG_WPS */
  70. /**
  71. * wpa_supplicant_enabled_networks - Check whether there are enabled networks
  72. * @wpa_s: Pointer to wpa_supplicant data
  73. * Returns: 0 if no networks are enabled, >0 if networks are enabled
  74. *
  75. * This function is used to figure out whether any networks (or Interworking
  76. * with enabled credentials and auto_interworking) are present in the current
  77. * configuration.
  78. */
  79. int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
  80. {
  81. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  82. int count = 0, disabled = 0;
  83. while (ssid) {
  84. if (!wpas_network_disabled(wpa_s, ssid))
  85. count++;
  86. else
  87. disabled++;
  88. ssid = ssid->next;
  89. }
  90. if (wpa_s->conf->cred && wpa_s->conf->interworking &&
  91. wpa_s->conf->auto_interworking)
  92. count++;
  93. if (count == 0 && disabled > 0) {
  94. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
  95. "networks)", disabled);
  96. }
  97. return count;
  98. }
  99. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  100. struct wpa_ssid *ssid)
  101. {
  102. while (ssid) {
  103. if (!wpas_network_disabled(wpa_s, ssid))
  104. break;
  105. ssid = ssid->next;
  106. }
  107. /* ap_scan=2 mode - try to associate with each SSID. */
  108. if (ssid == NULL) {
  109. wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
  110. "end of scan list - go back to beginning");
  111. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  112. wpa_supplicant_req_scan(wpa_s, 0, 0);
  113. return;
  114. }
  115. if (ssid->next) {
  116. /* Continue from the next SSID on the next attempt. */
  117. wpa_s->prev_scan_ssid = ssid;
  118. } else {
  119. /* Start from the beginning of the SSID list. */
  120. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  121. }
  122. wpa_supplicant_associate(wpa_s, NULL, ssid);
  123. }
  124. static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  125. {
  126. struct wpa_supplicant *wpa_s = work->wpa_s;
  127. struct wpa_driver_scan_params *params = work->ctx;
  128. int ret;
  129. if (deinit) {
  130. if (!work->started) {
  131. wpa_scan_free_params(params);
  132. return;
  133. }
  134. wpa_supplicant_notify_scanning(wpa_s, 0);
  135. wpas_notify_scan_done(wpa_s, 0);
  136. wpa_s->scan_work = NULL;
  137. return;
  138. }
  139. if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
  140. wpa_msg(wpa_s, MSG_INFO,
  141. "Failed to assign random MAC address for a scan");
  142. radio_work_done(work);
  143. return;
  144. }
  145. wpa_supplicant_notify_scanning(wpa_s, 1);
  146. if (wpa_s->clear_driver_scan_cache)
  147. params->only_new_results = 1;
  148. ret = wpa_drv_scan(wpa_s, params);
  149. wpa_scan_free_params(params);
  150. work->ctx = NULL;
  151. if (ret) {
  152. wpa_supplicant_notify_scanning(wpa_s, 0);
  153. wpas_notify_scan_done(wpa_s, 0);
  154. radio_work_done(work);
  155. return;
  156. }
  157. os_get_reltime(&wpa_s->scan_trigger_time);
  158. wpa_s->scan_runs++;
  159. wpa_s->normal_scans++;
  160. wpa_s->own_scan_requested = 1;
  161. wpa_s->clear_driver_scan_cache = 0;
  162. wpa_s->scan_work = work;
  163. }
  164. /**
  165. * wpa_supplicant_trigger_scan - Request driver to start a scan
  166. * @wpa_s: Pointer to wpa_supplicant data
  167. * @params: Scan parameters
  168. * Returns: 0 on success, -1 on failure
  169. */
  170. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  171. struct wpa_driver_scan_params *params)
  172. {
  173. struct wpa_driver_scan_params *ctx;
  174. if (wpa_s->scan_work) {
  175. wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
  176. return -1;
  177. }
  178. ctx = wpa_scan_clone_params(params);
  179. if (ctx == NULL)
  180. return -1;
  181. if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
  182. {
  183. wpa_scan_free_params(ctx);
  184. return -1;
  185. }
  186. return 0;
  187. }
  188. static void
  189. wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  190. {
  191. struct wpa_supplicant *wpa_s = eloop_ctx;
  192. wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
  193. if (wpa_supplicant_req_sched_scan(wpa_s))
  194. wpa_supplicant_req_scan(wpa_s, 0, 0);
  195. }
  196. static void
  197. wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  198. {
  199. struct wpa_supplicant *wpa_s = eloop_ctx;
  200. wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
  201. wpa_s->sched_scan_timed_out = 1;
  202. wpa_supplicant_cancel_sched_scan(wpa_s);
  203. }
  204. int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
  205. struct wpa_driver_scan_params *params,
  206. int interval)
  207. {
  208. int ret;
  209. wpa_supplicant_notify_scanning(wpa_s, 1);
  210. ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
  211. if (ret)
  212. wpa_supplicant_notify_scanning(wpa_s, 0);
  213. else
  214. wpa_s->sched_scanning = 1;
  215. return ret;
  216. }
  217. int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
  218. {
  219. int ret;
  220. ret = wpa_drv_stop_sched_scan(wpa_s);
  221. if (ret) {
  222. wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
  223. /* TODO: what to do if stopping fails? */
  224. return -1;
  225. }
  226. return ret;
  227. }
  228. static struct wpa_driver_scan_filter *
  229. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  230. {
  231. struct wpa_driver_scan_filter *ssids;
  232. struct wpa_ssid *ssid;
  233. size_t count;
  234. *num_ssids = 0;
  235. if (!conf->filter_ssids)
  236. return NULL;
  237. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  238. if (ssid->ssid && ssid->ssid_len)
  239. count++;
  240. }
  241. if (count == 0)
  242. return NULL;
  243. ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
  244. if (ssids == NULL)
  245. return NULL;
  246. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  247. if (!ssid->ssid || !ssid->ssid_len)
  248. continue;
  249. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  250. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  251. (*num_ssids)++;
  252. }
  253. return ssids;
  254. }
  255. static void wpa_supplicant_optimize_freqs(
  256. struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
  257. {
  258. #ifdef CONFIG_P2P
  259. if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
  260. wpa_s->go_params) {
  261. /* Optimize provisioning state scan based on GO information */
  262. if (wpa_s->p2p_in_provisioning < 5 &&
  263. wpa_s->go_params->freq > 0) {
  264. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
  265. "preferred frequency %d MHz",
  266. wpa_s->go_params->freq);
  267. params->freqs = os_zalloc(2 * sizeof(int));
  268. if (params->freqs)
  269. params->freqs[0] = wpa_s->go_params->freq;
  270. } else if (wpa_s->p2p_in_provisioning < 8 &&
  271. wpa_s->go_params->freq_list[0]) {
  272. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
  273. "channels");
  274. int_array_concat(&params->freqs,
  275. wpa_s->go_params->freq_list);
  276. if (params->freqs)
  277. int_array_sort_unique(params->freqs);
  278. }
  279. wpa_s->p2p_in_provisioning++;
  280. }
  281. if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
  282. /*
  283. * Optimize scan based on GO information during persistent
  284. * group reinvocation
  285. */
  286. if (wpa_s->p2p_in_invitation < 5 &&
  287. wpa_s->p2p_invite_go_freq > 0) {
  288. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
  289. wpa_s->p2p_invite_go_freq);
  290. params->freqs = os_zalloc(2 * sizeof(int));
  291. if (params->freqs)
  292. params->freqs[0] = wpa_s->p2p_invite_go_freq;
  293. }
  294. wpa_s->p2p_in_invitation++;
  295. if (wpa_s->p2p_in_invitation > 20) {
  296. /*
  297. * This should not really happen since the variable is
  298. * cleared on group removal, but if it does happen, make
  299. * sure we do not get stuck in special invitation scan
  300. * mode.
  301. */
  302. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
  303. wpa_s->p2p_in_invitation = 0;
  304. }
  305. }
  306. #endif /* CONFIG_P2P */
  307. #ifdef CONFIG_WPS
  308. if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  309. /*
  310. * Optimize post-provisioning scan based on channel used
  311. * during provisioning.
  312. */
  313. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
  314. "that was used during provisioning", wpa_s->wps_freq);
  315. params->freqs = os_zalloc(2 * sizeof(int));
  316. if (params->freqs)
  317. params->freqs[0] = wpa_s->wps_freq;
  318. wpa_s->after_wps--;
  319. } else if (wpa_s->after_wps)
  320. wpa_s->after_wps--;
  321. if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
  322. {
  323. /* Optimize provisioning scan based on already known channel */
  324. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
  325. wpa_s->wps_freq);
  326. params->freqs = os_zalloc(2 * sizeof(int));
  327. if (params->freqs)
  328. params->freqs[0] = wpa_s->wps_freq;
  329. wpa_s->known_wps_freq = 0; /* only do this once */
  330. }
  331. #endif /* CONFIG_WPS */
  332. }
  333. #ifdef CONFIG_INTERWORKING
  334. static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
  335. struct wpabuf *buf)
  336. {
  337. if (wpa_s->conf->interworking == 0)
  338. return;
  339. wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
  340. wpabuf_put_u8(buf, 6);
  341. wpabuf_put_u8(buf, 0x00);
  342. wpabuf_put_u8(buf, 0x00);
  343. wpabuf_put_u8(buf, 0x00);
  344. wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
  345. wpabuf_put_u8(buf, 0x00);
  346. #ifdef CONFIG_HS20
  347. wpabuf_put_u8(buf, 0x40); /* Bit 46 - WNM-Notification */
  348. #else /* CONFIG_HS20 */
  349. wpabuf_put_u8(buf, 0x00);
  350. #endif /* CONFIG_HS20 */
  351. wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
  352. wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
  353. 1 + ETH_ALEN);
  354. wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
  355. /* No Venue Info */
  356. if (!is_zero_ether_addr(wpa_s->conf->hessid))
  357. wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
  358. }
  359. #endif /* CONFIG_INTERWORKING */
  360. static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
  361. {
  362. struct wpabuf *extra_ie = NULL;
  363. #ifdef CONFIG_WPS
  364. int wps = 0;
  365. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  366. #endif /* CONFIG_WPS */
  367. #ifdef CONFIG_INTERWORKING
  368. if (wpa_s->conf->interworking &&
  369. wpabuf_resize(&extra_ie, 100) == 0)
  370. wpas_add_interworking_elements(wpa_s, extra_ie);
  371. #endif /* CONFIG_INTERWORKING */
  372. #ifdef CONFIG_WPS
  373. wps = wpas_wps_in_use(wpa_s, &req_type);
  374. if (wps) {
  375. struct wpabuf *wps_ie;
  376. wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
  377. DEV_PW_DEFAULT,
  378. &wpa_s->wps->dev,
  379. wpa_s->wps->uuid, req_type,
  380. 0, NULL);
  381. if (wps_ie) {
  382. if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
  383. wpabuf_put_buf(extra_ie, wps_ie);
  384. wpabuf_free(wps_ie);
  385. }
  386. }
  387. #ifdef CONFIG_P2P
  388. if (wps) {
  389. size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  390. if (wpabuf_resize(&extra_ie, ielen) == 0)
  391. wpas_p2p_scan_ie(wpa_s, extra_ie);
  392. }
  393. #endif /* CONFIG_P2P */
  394. #endif /* CONFIG_WPS */
  395. #ifdef CONFIG_HS20
  396. if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
  397. wpas_hs20_add_indication(extra_ie, -1);
  398. #endif /* CONFIG_HS20 */
  399. return extra_ie;
  400. }
  401. #ifdef CONFIG_P2P
  402. /*
  403. * Check whether there are any enabled networks or credentials that could be
  404. * used for a non-P2P connection.
  405. */
  406. static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
  407. {
  408. struct wpa_ssid *ssid;
  409. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  410. if (wpas_network_disabled(wpa_s, ssid))
  411. continue;
  412. if (!ssid->p2p_group)
  413. return 1;
  414. }
  415. if (wpa_s->conf->cred && wpa_s->conf->interworking &&
  416. wpa_s->conf->auto_interworking)
  417. return 1;
  418. return 0;
  419. }
  420. #endif /* CONFIG_P2P */
  421. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  422. u16 num_modes,
  423. enum hostapd_hw_mode mode)
  424. {
  425. u16 i;
  426. for (i = 0; i < num_modes; i++) {
  427. if (modes[i].mode == mode)
  428. return &modes[i];
  429. }
  430. return NULL;
  431. }
  432. static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
  433. enum hostapd_hw_mode band,
  434. struct wpa_driver_scan_params *params)
  435. {
  436. /* Include only supported channels for the specified band */
  437. struct hostapd_hw_modes *mode;
  438. int count, i;
  439. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
  440. if (mode == NULL) {
  441. /* No channels supported in this band - use empty list */
  442. params->freqs = os_zalloc(sizeof(int));
  443. return;
  444. }
  445. params->freqs = os_zalloc((mode->num_channels + 1) * sizeof(int));
  446. if (params->freqs == NULL)
  447. return;
  448. for (count = 0, i = 0; i < mode->num_channels; i++) {
  449. if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
  450. continue;
  451. params->freqs[count++] = mode->channels[i].freq;
  452. }
  453. }
  454. static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
  455. struct wpa_driver_scan_params *params)
  456. {
  457. if (wpa_s->hw.modes == NULL)
  458. return; /* unknown what channels the driver supports */
  459. if (params->freqs)
  460. return; /* already using a limited channel set */
  461. if (wpa_s->setband == WPA_SETBAND_5G)
  462. wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
  463. params);
  464. else if (wpa_s->setband == WPA_SETBAND_2G)
  465. wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
  466. params);
  467. }
  468. static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
  469. struct wpa_driver_scan_params *params,
  470. size_t max_ssids)
  471. {
  472. unsigned int i;
  473. struct wpa_ssid *ssid;
  474. for (i = 0; i < wpa_s->scan_id_count; i++) {
  475. unsigned int j;
  476. ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
  477. if (!ssid || !ssid->scan_ssid)
  478. continue;
  479. for (j = 0; j < params->num_ssids; j++) {
  480. if (params->ssids[j].ssid_len == ssid->ssid_len &&
  481. params->ssids[j].ssid &&
  482. os_memcmp(params->ssids[j].ssid, ssid->ssid,
  483. ssid->ssid_len) == 0)
  484. break;
  485. }
  486. if (j < params->num_ssids)
  487. continue; /* already in the list */
  488. if (params->num_ssids + 1 > max_ssids) {
  489. wpa_printf(MSG_DEBUG,
  490. "Over max scan SSIDs for manual request");
  491. break;
  492. }
  493. wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
  494. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  495. params->ssids[params->num_ssids].ssid = ssid->ssid;
  496. params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
  497. params->num_ssids++;
  498. }
  499. wpa_s->scan_id_count = 0;
  500. }
  501. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  502. {
  503. struct wpa_supplicant *wpa_s = eloop_ctx;
  504. struct wpa_ssid *ssid;
  505. int ret;
  506. struct wpabuf *extra_ie = NULL;
  507. struct wpa_driver_scan_params params;
  508. struct wpa_driver_scan_params *scan_params;
  509. size_t max_ssids;
  510. enum wpa_states prev_state;
  511. if (wpa_s->pno || wpa_s->pno_sched_pending) {
  512. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
  513. return;
  514. }
  515. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  516. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  517. return;
  518. }
  519. if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
  520. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
  521. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  522. return;
  523. }
  524. if (wpa_s->scanning) {
  525. /*
  526. * If we are already in scanning state, we shall reschedule the
  527. * the incoming scan request.
  528. */
  529. wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
  530. wpa_supplicant_req_scan(wpa_s, 1, 0);
  531. return;
  532. }
  533. if (!wpa_supplicant_enabled_networks(wpa_s) &&
  534. wpa_s->scan_req == NORMAL_SCAN_REQ) {
  535. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  536. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  537. return;
  538. }
  539. if (wpa_s->conf->ap_scan != 0 &&
  540. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  541. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  542. "overriding ap_scan configuration");
  543. wpa_s->conf->ap_scan = 0;
  544. wpas_notify_ap_scan_changed(wpa_s);
  545. }
  546. if (wpa_s->conf->ap_scan == 0) {
  547. wpa_supplicant_gen_assoc_event(wpa_s);
  548. return;
  549. }
  550. if (wpas_p2p_in_progress(wpa_s)) {
  551. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
  552. wpa_supplicant_req_scan(wpa_s, 5, 0);
  553. return;
  554. }
  555. if (wpa_s->conf->ap_scan == 2)
  556. max_ssids = 1;
  557. else {
  558. max_ssids = wpa_s->max_scan_ssids;
  559. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  560. max_ssids = WPAS_MAX_SCAN_SSIDS;
  561. }
  562. wpa_s->last_scan_req = wpa_s->scan_req;
  563. wpa_s->scan_req = NORMAL_SCAN_REQ;
  564. os_memset(&params, 0, sizeof(params));
  565. prev_state = wpa_s->wpa_state;
  566. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  567. wpa_s->wpa_state == WPA_INACTIVE)
  568. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  569. /*
  570. * If autoscan has set its own scanning parameters
  571. */
  572. if (wpa_s->autoscan_params != NULL) {
  573. scan_params = wpa_s->autoscan_params;
  574. goto scan;
  575. }
  576. if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
  577. wpa_s->connect_without_scan) {
  578. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  579. if (ssid == wpa_s->connect_without_scan)
  580. break;
  581. }
  582. wpa_s->connect_without_scan = NULL;
  583. if (ssid) {
  584. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  585. "without scan step");
  586. wpa_supplicant_associate(wpa_s, NULL, ssid);
  587. return;
  588. }
  589. }
  590. #ifdef CONFIG_P2P
  591. if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
  592. wpa_s->go_params) {
  593. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
  594. wpa_s->p2p_in_provisioning,
  595. wpa_s->show_group_started);
  596. params.ssids[0].ssid = wpa_s->go_params->ssid;
  597. params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
  598. params.num_ssids = 1;
  599. goto ssid_list_set;
  600. }
  601. if (wpa_s->p2p_in_invitation) {
  602. if (wpa_s->current_ssid) {
  603. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
  604. params.ssids[0].ssid = wpa_s->current_ssid->ssid;
  605. params.ssids[0].ssid_len =
  606. wpa_s->current_ssid->ssid_len;
  607. params.num_ssids = 1;
  608. } else {
  609. wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
  610. }
  611. goto ssid_list_set;
  612. }
  613. #endif /* CONFIG_P2P */
  614. /* Find the starting point from which to continue scanning */
  615. ssid = wpa_s->conf->ssid;
  616. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  617. while (ssid) {
  618. if (ssid == wpa_s->prev_scan_ssid) {
  619. ssid = ssid->next;
  620. break;
  621. }
  622. ssid = ssid->next;
  623. }
  624. }
  625. if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
  626. wpa_s->conf->ap_scan == 2) {
  627. wpa_s->connect_without_scan = NULL;
  628. wpa_s->prev_scan_wildcard = 0;
  629. wpa_supplicant_assoc_try(wpa_s, ssid);
  630. return;
  631. } else if (wpa_s->conf->ap_scan == 2) {
  632. /*
  633. * User-initiated scan request in ap_scan == 2; scan with
  634. * wildcard SSID.
  635. */
  636. ssid = NULL;
  637. } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
  638. /*
  639. * Perform single-channel single-SSID scan for
  640. * reassociate-to-same-BSS operation.
  641. */
  642. /* Setup SSID */
  643. ssid = wpa_s->current_ssid;
  644. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  645. ssid->ssid, ssid->ssid_len);
  646. params.ssids[0].ssid = ssid->ssid;
  647. params.ssids[0].ssid_len = ssid->ssid_len;
  648. params.num_ssids = 1;
  649. /*
  650. * Allocate memory for frequency array, allocate one extra
  651. * slot for the zero-terminator.
  652. */
  653. params.freqs = os_malloc(sizeof(int) * 2);
  654. if (params.freqs == NULL) {
  655. wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
  656. return;
  657. }
  658. params.freqs[0] = wpa_s->assoc_freq;
  659. params.freqs[1] = 0;
  660. /*
  661. * Reset the reattach flag so that we fall back to full scan if
  662. * this scan fails.
  663. */
  664. wpa_s->reattach = 0;
  665. } else {
  666. struct wpa_ssid *start = ssid, *tssid;
  667. int freqs_set = 0;
  668. if (ssid == NULL && max_ssids > 1)
  669. ssid = wpa_s->conf->ssid;
  670. while (ssid) {
  671. if (!wpas_network_disabled(wpa_s, ssid) &&
  672. ssid->scan_ssid) {
  673. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  674. ssid->ssid, ssid->ssid_len);
  675. params.ssids[params.num_ssids].ssid =
  676. ssid->ssid;
  677. params.ssids[params.num_ssids].ssid_len =
  678. ssid->ssid_len;
  679. params.num_ssids++;
  680. if (params.num_ssids + 1 >= max_ssids)
  681. break;
  682. }
  683. ssid = ssid->next;
  684. if (ssid == start)
  685. break;
  686. if (ssid == NULL && max_ssids > 1 &&
  687. start != wpa_s->conf->ssid)
  688. ssid = wpa_s->conf->ssid;
  689. }
  690. if (wpa_s->scan_id_count &&
  691. wpa_s->last_scan_req == MANUAL_SCAN_REQ)
  692. wpa_set_scan_ssids(wpa_s, &params, max_ssids);
  693. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  694. if (wpas_network_disabled(wpa_s, tssid))
  695. continue;
  696. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  697. int_array_concat(&params.freqs,
  698. tssid->scan_freq);
  699. } else {
  700. os_free(params.freqs);
  701. params.freqs = NULL;
  702. }
  703. freqs_set = 1;
  704. }
  705. int_array_sort_unique(params.freqs);
  706. }
  707. if (ssid && max_ssids == 1) {
  708. /*
  709. * If the driver is limited to 1 SSID at a time interleave
  710. * wildcard SSID scans with specific SSID scans to avoid
  711. * waiting a long time for a wildcard scan.
  712. */
  713. if (!wpa_s->prev_scan_wildcard) {
  714. params.ssids[0].ssid = NULL;
  715. params.ssids[0].ssid_len = 0;
  716. wpa_s->prev_scan_wildcard = 1;
  717. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  718. "wildcard SSID (Interleave with specific)");
  719. } else {
  720. wpa_s->prev_scan_ssid = ssid;
  721. wpa_s->prev_scan_wildcard = 0;
  722. wpa_dbg(wpa_s, MSG_DEBUG,
  723. "Starting AP scan for specific SSID: %s",
  724. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  725. }
  726. } else if (ssid) {
  727. /* max_ssids > 1 */
  728. wpa_s->prev_scan_ssid = ssid;
  729. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  730. "the scan request");
  731. params.num_ssids++;
  732. } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  733. wpa_s->manual_scan_passive && params.num_ssids == 0) {
  734. wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
  735. } else {
  736. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  737. params.num_ssids++;
  738. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  739. "SSID");
  740. }
  741. #ifdef CONFIG_P2P
  742. ssid_list_set:
  743. #endif /* CONFIG_P2P */
  744. wpa_supplicant_optimize_freqs(wpa_s, &params);
  745. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  746. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  747. wpa_s->manual_scan_only_new)
  748. params.only_new_results = 1;
  749. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
  750. wpa_s->manual_scan_freqs) {
  751. wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
  752. params.freqs = wpa_s->manual_scan_freqs;
  753. wpa_s->manual_scan_freqs = NULL;
  754. }
  755. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  756. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  757. "generated frequency list");
  758. params.freqs = wpa_s->next_scan_freqs;
  759. } else
  760. os_free(wpa_s->next_scan_freqs);
  761. wpa_s->next_scan_freqs = NULL;
  762. wpa_setband_scan_freqs(wpa_s, &params);
  763. /* See if user specified frequencies. If so, scan only those. */
  764. if (wpa_s->conf->freq_list && !params.freqs) {
  765. wpa_dbg(wpa_s, MSG_DEBUG,
  766. "Optimize scan based on conf->freq_list");
  767. int_array_concat(&params.freqs, wpa_s->conf->freq_list);
  768. }
  769. /* Use current associated channel? */
  770. if (wpa_s->conf->scan_cur_freq && !params.freqs) {
  771. unsigned int num = wpa_s->num_multichan_concurrent;
  772. params.freqs = os_calloc(num + 1, sizeof(int));
  773. if (params.freqs) {
  774. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  775. if (num > 0) {
  776. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
  777. "current operating channels since "
  778. "scan_cur_freq is enabled");
  779. } else {
  780. os_free(params.freqs);
  781. params.freqs = NULL;
  782. }
  783. }
  784. }
  785. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  786. wpa_s->conf, &params.num_filter_ssids);
  787. if (extra_ie) {
  788. params.extra_ies = wpabuf_head(extra_ie);
  789. params.extra_ies_len = wpabuf_len(extra_ie);
  790. }
  791. #ifdef CONFIG_P2P
  792. if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
  793. (wpa_s->show_group_started && wpa_s->go_params)) {
  794. /*
  795. * The interface may not yet be in P2P mode, so we have to
  796. * explicitly request P2P probe to disable CCK rates.
  797. */
  798. params.p2p_probe = 1;
  799. }
  800. #endif /* CONFIG_P2P */
  801. scan_params = &params;
  802. scan:
  803. #ifdef CONFIG_P2P
  804. /*
  805. * If the driver does not support multi-channel concurrency and a
  806. * virtual interface that shares the same radio with the wpa_s interface
  807. * is operating there may not be need to scan other channels apart from
  808. * the current operating channel on the other virtual interface. Filter
  809. * out other channels in case we are trying to find a connection for a
  810. * station interface when we are not configured to prefer station
  811. * connection and a concurrent operation is already in process.
  812. */
  813. if (wpa_s->scan_for_connection &&
  814. wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
  815. !scan_params->freqs && !params.freqs &&
  816. wpas_is_p2p_prioritized(wpa_s) &&
  817. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  818. non_p2p_network_enabled(wpa_s)) {
  819. unsigned int num = wpa_s->num_multichan_concurrent;
  820. params.freqs = os_calloc(num + 1, sizeof(int));
  821. if (params.freqs) {
  822. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  823. if (num > 0 && num == wpa_s->num_multichan_concurrent) {
  824. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
  825. } else {
  826. os_free(params.freqs);
  827. params.freqs = NULL;
  828. }
  829. }
  830. }
  831. #endif /* CONFIG_P2P */
  832. ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
  833. if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
  834. !wpa_s->manual_scan_freqs) {
  835. /* Restore manual_scan_freqs for the next attempt */
  836. wpa_s->manual_scan_freqs = params.freqs;
  837. params.freqs = NULL;
  838. }
  839. wpabuf_free(extra_ie);
  840. os_free(params.freqs);
  841. os_free(params.filter_ssids);
  842. if (ret) {
  843. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  844. if (prev_state != wpa_s->wpa_state)
  845. wpa_supplicant_set_state(wpa_s, prev_state);
  846. /* Restore scan_req since we will try to scan again */
  847. wpa_s->scan_req = wpa_s->last_scan_req;
  848. wpa_supplicant_req_scan(wpa_s, 1, 0);
  849. } else {
  850. wpa_s->scan_for_connection = 0;
  851. }
  852. }
  853. void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
  854. {
  855. struct os_reltime remaining, new_int;
  856. int cancelled;
  857. cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
  858. &remaining);
  859. new_int.sec = sec;
  860. new_int.usec = 0;
  861. if (cancelled && os_reltime_before(&remaining, &new_int)) {
  862. new_int.sec = remaining.sec;
  863. new_int.usec = remaining.usec;
  864. }
  865. if (cancelled) {
  866. eloop_register_timeout(new_int.sec, new_int.usec,
  867. wpa_supplicant_scan, wpa_s, NULL);
  868. }
  869. wpa_s->scan_interval = sec;
  870. }
  871. /**
  872. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  873. * @wpa_s: Pointer to wpa_supplicant data
  874. * @sec: Number of seconds after which to scan
  875. * @usec: Number of microseconds after which to scan
  876. *
  877. * This function is used to schedule a scan for neighboring access points after
  878. * the specified time.
  879. */
  880. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  881. {
  882. int res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
  883. NULL);
  884. if (res == 1) {
  885. wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
  886. sec, usec);
  887. } else if (res == 0) {
  888. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
  889. sec, usec);
  890. } else {
  891. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
  892. sec, usec);
  893. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  894. }
  895. }
  896. /**
  897. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  898. * @wpa_s: Pointer to wpa_supplicant data
  899. * @sec: Number of seconds after which to scan
  900. * @usec: Number of microseconds after which to scan
  901. * Returns: 0 on success or -1 otherwise
  902. *
  903. * This function is used to schedule periodic scans for neighboring
  904. * access points after the specified time.
  905. */
  906. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  907. int sec, int usec)
  908. {
  909. if (!wpa_s->sched_scan_supported)
  910. return -1;
  911. eloop_register_timeout(sec, usec,
  912. wpa_supplicant_delayed_sched_scan_timeout,
  913. wpa_s, NULL);
  914. return 0;
  915. }
  916. /**
  917. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  918. * @wpa_s: Pointer to wpa_supplicant data
  919. * Returns: 0 is sched_scan was started or -1 otherwise
  920. *
  921. * This function is used to schedule periodic scans for neighboring
  922. * access points repeating the scan continuously.
  923. */
  924. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  925. {
  926. struct wpa_driver_scan_params params;
  927. struct wpa_driver_scan_params *scan_params;
  928. enum wpa_states prev_state;
  929. struct wpa_ssid *ssid = NULL;
  930. struct wpabuf *extra_ie = NULL;
  931. int ret;
  932. unsigned int max_sched_scan_ssids;
  933. int wildcard = 0;
  934. int need_ssids;
  935. if (!wpa_s->sched_scan_supported)
  936. return -1;
  937. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  938. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  939. else
  940. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  941. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  942. return -1;
  943. if (wpa_s->sched_scanning) {
  944. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  945. return 0;
  946. }
  947. need_ssids = 0;
  948. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  949. if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
  950. /* Use wildcard SSID to find this network */
  951. wildcard = 1;
  952. } else if (!wpas_network_disabled(wpa_s, ssid) &&
  953. ssid->ssid_len)
  954. need_ssids++;
  955. #ifdef CONFIG_WPS
  956. if (!wpas_network_disabled(wpa_s, ssid) &&
  957. ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  958. /*
  959. * Normal scan is more reliable and faster for WPS
  960. * operations and since these are for short periods of
  961. * time, the benefit of trying to use sched_scan would
  962. * be limited.
  963. */
  964. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  965. "sched_scan for WPS");
  966. return -1;
  967. }
  968. #endif /* CONFIG_WPS */
  969. }
  970. if (wildcard)
  971. need_ssids++;
  972. if (wpa_s->normal_scans < 3 &&
  973. (need_ssids <= wpa_s->max_scan_ssids ||
  974. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  975. /*
  976. * When normal scan can speed up operations, use that for the
  977. * first operations before starting the sched_scan to allow
  978. * user space sleep more. We do this only if the normal scan
  979. * has functionality that is suitable for this or if the
  980. * sched_scan does not have better support for multiple SSIDs.
  981. */
  982. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  983. "sched_scan for initial scans (normal_scans=%d)",
  984. wpa_s->normal_scans);
  985. return -1;
  986. }
  987. os_memset(&params, 0, sizeof(params));
  988. /* If we can't allocate space for the filters, we just don't filter */
  989. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  990. sizeof(struct wpa_driver_scan_filter));
  991. prev_state = wpa_s->wpa_state;
  992. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  993. wpa_s->wpa_state == WPA_INACTIVE)
  994. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  995. if (wpa_s->autoscan_params != NULL) {
  996. scan_params = wpa_s->autoscan_params;
  997. goto scan;
  998. }
  999. /* Find the starting point from which to continue scanning */
  1000. ssid = wpa_s->conf->ssid;
  1001. if (wpa_s->prev_sched_ssid) {
  1002. while (ssid) {
  1003. if (ssid == wpa_s->prev_sched_ssid) {
  1004. ssid = ssid->next;
  1005. break;
  1006. }
  1007. ssid = ssid->next;
  1008. }
  1009. }
  1010. if (!ssid || !wpa_s->prev_sched_ssid) {
  1011. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  1012. if (wpa_s->conf->sched_scan_interval)
  1013. wpa_s->sched_scan_interval =
  1014. wpa_s->conf->sched_scan_interval;
  1015. if (wpa_s->sched_scan_interval == 0)
  1016. wpa_s->sched_scan_interval = 10;
  1017. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  1018. wpa_s->first_sched_scan = 1;
  1019. ssid = wpa_s->conf->ssid;
  1020. wpa_s->prev_sched_ssid = ssid;
  1021. }
  1022. if (wildcard) {
  1023. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  1024. params.num_ssids++;
  1025. }
  1026. while (ssid) {
  1027. if (wpas_network_disabled(wpa_s, ssid))
  1028. goto next;
  1029. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  1030. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  1031. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  1032. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1033. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  1034. ssid->ssid, ssid->ssid_len);
  1035. params.filter_ssids[params.num_filter_ssids].ssid_len =
  1036. ssid->ssid_len;
  1037. params.num_filter_ssids++;
  1038. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  1039. {
  1040. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  1041. "filter for sched_scan - drop filter");
  1042. os_free(params.filter_ssids);
  1043. params.filter_ssids = NULL;
  1044. params.num_filter_ssids = 0;
  1045. }
  1046. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  1047. if (params.num_ssids == max_sched_scan_ssids)
  1048. break; /* only room for broadcast SSID */
  1049. wpa_dbg(wpa_s, MSG_DEBUG,
  1050. "add to active scan ssid: %s",
  1051. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1052. params.ssids[params.num_ssids].ssid =
  1053. ssid->ssid;
  1054. params.ssids[params.num_ssids].ssid_len =
  1055. ssid->ssid_len;
  1056. params.num_ssids++;
  1057. if (params.num_ssids >= max_sched_scan_ssids) {
  1058. wpa_s->prev_sched_ssid = ssid;
  1059. do {
  1060. ssid = ssid->next;
  1061. } while (ssid &&
  1062. (wpas_network_disabled(wpa_s, ssid) ||
  1063. !ssid->scan_ssid));
  1064. break;
  1065. }
  1066. }
  1067. next:
  1068. wpa_s->prev_sched_ssid = ssid;
  1069. ssid = ssid->next;
  1070. }
  1071. if (params.num_filter_ssids == 0) {
  1072. os_free(params.filter_ssids);
  1073. params.filter_ssids = NULL;
  1074. }
  1075. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  1076. if (extra_ie) {
  1077. params.extra_ies = wpabuf_head(extra_ie);
  1078. params.extra_ies_len = wpabuf_len(extra_ie);
  1079. }
  1080. if (wpa_s->conf->filter_rssi)
  1081. params.filter_rssi = wpa_s->conf->filter_rssi;
  1082. /* See if user specified frequencies. If so, scan only those. */
  1083. if (wpa_s->conf->freq_list && !params.freqs) {
  1084. wpa_dbg(wpa_s, MSG_DEBUG,
  1085. "Optimize scan based on conf->freq_list");
  1086. int_array_concat(&params.freqs, wpa_s->conf->freq_list);
  1087. }
  1088. scan_params = &params;
  1089. scan:
  1090. if (ssid || !wpa_s->first_sched_scan) {
  1091. wpa_dbg(wpa_s, MSG_DEBUG,
  1092. "Starting sched scan: interval %d timeout %d",
  1093. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  1094. } else {
  1095. wpa_dbg(wpa_s, MSG_DEBUG,
  1096. "Starting sched scan: interval %d (no timeout)",
  1097. wpa_s->sched_scan_interval);
  1098. }
  1099. wpa_setband_scan_freqs(wpa_s, scan_params);
  1100. ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
  1101. wpa_s->sched_scan_interval);
  1102. wpabuf_free(extra_ie);
  1103. os_free(params.filter_ssids);
  1104. if (ret) {
  1105. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  1106. if (prev_state != wpa_s->wpa_state)
  1107. wpa_supplicant_set_state(wpa_s, prev_state);
  1108. return ret;
  1109. }
  1110. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  1111. if (ssid || !wpa_s->first_sched_scan) {
  1112. wpa_s->sched_scan_timed_out = 0;
  1113. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  1114. wpa_supplicant_sched_scan_timeout,
  1115. wpa_s, NULL);
  1116. wpa_s->first_sched_scan = 0;
  1117. wpa_s->sched_scan_timeout /= 2;
  1118. wpa_s->sched_scan_interval *= 2;
  1119. if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
  1120. wpa_s->sched_scan_interval = 10;
  1121. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  1122. }
  1123. }
  1124. /* If there is no more ssids, start next time from the beginning */
  1125. if (!ssid)
  1126. wpa_s->prev_sched_ssid = NULL;
  1127. return 0;
  1128. }
  1129. /**
  1130. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  1131. * @wpa_s: Pointer to wpa_supplicant data
  1132. *
  1133. * This function is used to cancel a scan request scheduled with
  1134. * wpa_supplicant_req_scan().
  1135. */
  1136. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  1137. {
  1138. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  1139. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  1140. }
  1141. /**
  1142. * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
  1143. * @wpa_s: Pointer to wpa_supplicant data
  1144. *
  1145. * This function is used to stop a delayed scheduled scan.
  1146. */
  1147. void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
  1148. {
  1149. if (!wpa_s->sched_scan_supported)
  1150. return;
  1151. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
  1152. eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
  1153. wpa_s, NULL);
  1154. }
  1155. /**
  1156. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  1157. * @wpa_s: Pointer to wpa_supplicant data
  1158. *
  1159. * This function is used to stop a periodic scheduled scan.
  1160. */
  1161. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  1162. {
  1163. if (!wpa_s->sched_scanning)
  1164. return;
  1165. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  1166. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  1167. wpa_supplicant_stop_sched_scan(wpa_s);
  1168. }
  1169. /**
  1170. * wpa_supplicant_notify_scanning - Indicate possible scan state change
  1171. * @wpa_s: Pointer to wpa_supplicant data
  1172. * @scanning: Whether scanning is currently in progress
  1173. *
  1174. * This function is to generate scanning notifycations. It is called whenever
  1175. * there may have been a change in scanning (scan started, completed, stopped).
  1176. * wpas_notify_scanning() is called whenever the scanning state changed from the
  1177. * previously notified state.
  1178. */
  1179. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  1180. int scanning)
  1181. {
  1182. if (wpa_s->scanning != scanning) {
  1183. wpa_s->scanning = scanning;
  1184. wpas_notify_scanning(wpa_s);
  1185. }
  1186. }
  1187. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  1188. {
  1189. int rate = 0;
  1190. const u8 *ie;
  1191. int i;
  1192. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  1193. for (i = 0; ie && i < ie[1]; i++) {
  1194. if ((ie[i + 2] & 0x7f) > rate)
  1195. rate = ie[i + 2] & 0x7f;
  1196. }
  1197. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  1198. for (i = 0; ie && i < ie[1]; i++) {
  1199. if ((ie[i + 2] & 0x7f) > rate)
  1200. rate = ie[i + 2] & 0x7f;
  1201. }
  1202. return rate;
  1203. }
  1204. /**
  1205. * wpa_scan_get_ie - Fetch a specified information element from a scan result
  1206. * @res: Scan result entry
  1207. * @ie: Information element identitifier (WLAN_EID_*)
  1208. * Returns: Pointer to the information element (id field) or %NULL if not found
  1209. *
  1210. * This function returns the first matching information element in the scan
  1211. * result.
  1212. */
  1213. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  1214. {
  1215. const u8 *end, *pos;
  1216. pos = (const u8 *) (res + 1);
  1217. end = pos + res->ie_len;
  1218. while (pos + 1 < end) {
  1219. if (pos + 2 + pos[1] > end)
  1220. break;
  1221. if (pos[0] == ie)
  1222. return pos;
  1223. pos += 2 + pos[1];
  1224. }
  1225. return NULL;
  1226. }
  1227. /**
  1228. * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
  1229. * @res: Scan result entry
  1230. * @vendor_type: Vendor type (four octets starting the IE payload)
  1231. * Returns: Pointer to the information element (id field) or %NULL if not found
  1232. *
  1233. * This function returns the first matching information element in the scan
  1234. * result.
  1235. */
  1236. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  1237. u32 vendor_type)
  1238. {
  1239. const u8 *end, *pos;
  1240. pos = (const u8 *) (res + 1);
  1241. end = pos + res->ie_len;
  1242. while (pos + 1 < end) {
  1243. if (pos + 2 + pos[1] > end)
  1244. break;
  1245. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1246. vendor_type == WPA_GET_BE32(&pos[2]))
  1247. return pos;
  1248. pos += 2 + pos[1];
  1249. }
  1250. return NULL;
  1251. }
  1252. /**
  1253. * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
  1254. * @res: Scan result entry
  1255. * @vendor_type: Vendor type (four octets starting the IE payload)
  1256. * Returns: Pointer to the information element (id field) or %NULL if not found
  1257. *
  1258. * This function returns the first matching information element in the scan
  1259. * result.
  1260. *
  1261. * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
  1262. * from Beacon frames instead of either Beacon or Probe Response frames.
  1263. */
  1264. const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
  1265. u32 vendor_type)
  1266. {
  1267. const u8 *end, *pos;
  1268. if (res->beacon_ie_len == 0)
  1269. return NULL;
  1270. pos = (const u8 *) (res + 1);
  1271. pos += res->ie_len;
  1272. end = pos + res->beacon_ie_len;
  1273. while (pos + 1 < end) {
  1274. if (pos + 2 + pos[1] > end)
  1275. break;
  1276. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1277. vendor_type == WPA_GET_BE32(&pos[2]))
  1278. return pos;
  1279. pos += 2 + pos[1];
  1280. }
  1281. return NULL;
  1282. }
  1283. /**
  1284. * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
  1285. * @res: Scan result entry
  1286. * @vendor_type: Vendor type (four octets starting the IE payload)
  1287. * Returns: Pointer to the information element payload or %NULL if not found
  1288. *
  1289. * This function returns concatenated payload of possibly fragmented vendor
  1290. * specific information elements in the scan result. The caller is responsible
  1291. * for freeing the returned buffer.
  1292. */
  1293. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  1294. u32 vendor_type)
  1295. {
  1296. struct wpabuf *buf;
  1297. const u8 *end, *pos;
  1298. buf = wpabuf_alloc(res->ie_len);
  1299. if (buf == NULL)
  1300. return NULL;
  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. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1309. pos += 2 + pos[1];
  1310. }
  1311. if (wpabuf_len(buf) == 0) {
  1312. wpabuf_free(buf);
  1313. buf = NULL;
  1314. }
  1315. return buf;
  1316. }
  1317. /*
  1318. * Channels with a great SNR can operate at full rate. What is a great SNR?
  1319. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  1320. * rule of thumb is that any SNR above 20 is good." This one
  1321. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  1322. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  1323. * conservative value.
  1324. */
  1325. #define GREAT_SNR 30
  1326. /* Compare function for sorting scan results. Return >0 if @b is considered
  1327. * better. */
  1328. static int wpa_scan_result_compar(const void *a, const void *b)
  1329. {
  1330. #define IS_5GHZ(n) (n > 4000)
  1331. #define MIN(a,b) a < b ? a : b
  1332. struct wpa_scan_res **_wa = (void *) a;
  1333. struct wpa_scan_res **_wb = (void *) b;
  1334. struct wpa_scan_res *wa = *_wa;
  1335. struct wpa_scan_res *wb = *_wb;
  1336. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  1337. int snr_a, snr_b;
  1338. /* WPA/WPA2 support preferred */
  1339. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  1340. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  1341. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  1342. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  1343. if (wpa_b && !wpa_a)
  1344. return 1;
  1345. if (!wpa_b && wpa_a)
  1346. return -1;
  1347. /* privacy support preferred */
  1348. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  1349. (wb->caps & IEEE80211_CAP_PRIVACY))
  1350. return 1;
  1351. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  1352. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  1353. return -1;
  1354. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  1355. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  1356. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  1357. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  1358. } else {
  1359. /* Not suitable information to calculate SNR, so use level */
  1360. snr_a = wa->level;
  1361. snr_b = wb->level;
  1362. }
  1363. /* best/max rate preferred if SNR close enough */
  1364. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  1365. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  1366. maxrate_a = wpa_scan_get_max_rate(wa);
  1367. maxrate_b = wpa_scan_get_max_rate(wb);
  1368. if (maxrate_a != maxrate_b)
  1369. return maxrate_b - maxrate_a;
  1370. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  1371. return IS_5GHZ(wa->freq) ? -1 : 1;
  1372. }
  1373. /* use freq for channel preference */
  1374. /* all things being equal, use SNR; if SNRs are
  1375. * identical, use quality values since some drivers may only report
  1376. * that value and leave the signal level zero */
  1377. if (snr_b == snr_a)
  1378. return wb->qual - wa->qual;
  1379. return snr_b - snr_a;
  1380. #undef MIN
  1381. #undef IS_5GHZ
  1382. }
  1383. #ifdef CONFIG_WPS
  1384. /* Compare function for sorting scan results when searching a WPS AP for
  1385. * provisioning. Return >0 if @b is considered better. */
  1386. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1387. {
  1388. struct wpa_scan_res **_wa = (void *) a;
  1389. struct wpa_scan_res **_wb = (void *) b;
  1390. struct wpa_scan_res *wa = *_wa;
  1391. struct wpa_scan_res *wb = *_wb;
  1392. int uses_wps_a, uses_wps_b;
  1393. struct wpabuf *wps_a, *wps_b;
  1394. int res;
  1395. /* Optimization - check WPS IE existence before allocated memory and
  1396. * doing full reassembly. */
  1397. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1398. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1399. if (uses_wps_a && !uses_wps_b)
  1400. return -1;
  1401. if (!uses_wps_a && uses_wps_b)
  1402. return 1;
  1403. if (uses_wps_a && uses_wps_b) {
  1404. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1405. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1406. res = wps_ap_priority_compar(wps_a, wps_b);
  1407. wpabuf_free(wps_a);
  1408. wpabuf_free(wps_b);
  1409. if (res)
  1410. return res;
  1411. }
  1412. /*
  1413. * Do not use current AP security policy as a sorting criteria during
  1414. * WPS provisioning step since the AP may get reconfigured at the
  1415. * completion of provisioning.
  1416. */
  1417. /* all things being equal, use signal level; if signal levels are
  1418. * identical, use quality values since some drivers may only report
  1419. * that value and leave the signal level zero */
  1420. if (wb->level == wa->level)
  1421. return wb->qual - wa->qual;
  1422. return wb->level - wa->level;
  1423. }
  1424. #endif /* CONFIG_WPS */
  1425. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1426. {
  1427. #ifndef CONFIG_NO_STDOUT_DEBUG
  1428. size_t i;
  1429. if (scan_res->res == NULL || scan_res->num == 0)
  1430. return;
  1431. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1432. for (i = 0; i < scan_res->num; i++) {
  1433. struct wpa_scan_res *r = scan_res->res[i];
  1434. u8 *pos;
  1435. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1436. == WPA_SCAN_LEVEL_DBM) {
  1437. int snr = r->level - r->noise;
  1438. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1439. "noise=%d level=%d snr=%d%s flags=0x%x "
  1440. "age=%u",
  1441. MAC2STR(r->bssid), r->freq, r->qual,
  1442. r->noise, r->level, snr,
  1443. snr >= GREAT_SNR ? "*" : "", r->flags,
  1444. r->age);
  1445. } else {
  1446. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1447. "noise=%d level=%d flags=0x%x age=%u",
  1448. MAC2STR(r->bssid), r->freq, r->qual,
  1449. r->noise, r->level, r->flags, r->age);
  1450. }
  1451. pos = (u8 *) (r + 1);
  1452. if (r->ie_len)
  1453. wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
  1454. pos += r->ie_len;
  1455. if (r->beacon_ie_len)
  1456. wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
  1457. pos, r->beacon_ie_len);
  1458. }
  1459. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1460. }
  1461. /**
  1462. * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
  1463. * @wpa_s: Pointer to wpa_supplicant data
  1464. * @bssid: BSSID to check
  1465. * Returns: 0 if the BSSID is filtered or 1 if not
  1466. *
  1467. * This function is used to filter out specific BSSIDs from scan reslts mainly
  1468. * for testing purposes (SET bssid_filter ctrl_iface command).
  1469. */
  1470. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1471. const u8 *bssid)
  1472. {
  1473. size_t i;
  1474. if (wpa_s->bssid_filter == NULL)
  1475. return 1;
  1476. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1477. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1478. ETH_ALEN) == 0)
  1479. return 1;
  1480. }
  1481. return 0;
  1482. }
  1483. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1484. struct wpa_scan_results *res)
  1485. {
  1486. size_t i, j;
  1487. if (wpa_s->bssid_filter == NULL)
  1488. return;
  1489. for (i = 0, j = 0; i < res->num; i++) {
  1490. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1491. res->res[i]->bssid)) {
  1492. res->res[j++] = res->res[i];
  1493. } else {
  1494. os_free(res->res[i]);
  1495. res->res[i] = NULL;
  1496. }
  1497. }
  1498. if (res->num != j) {
  1499. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1500. (int) (res->num - j));
  1501. res->num = j;
  1502. }
  1503. }
  1504. /**
  1505. * wpa_supplicant_get_scan_results - Get scan results
  1506. * @wpa_s: Pointer to wpa_supplicant data
  1507. * @info: Information about what was scanned or %NULL if not available
  1508. * @new_scan: Whether a new scan was performed
  1509. * Returns: Scan results, %NULL on failure
  1510. *
  1511. * This function request the current scan results from the driver and updates
  1512. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1513. * results with wpa_scan_results_free().
  1514. */
  1515. struct wpa_scan_results *
  1516. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1517. struct scan_info *info, int new_scan)
  1518. {
  1519. struct wpa_scan_results *scan_res;
  1520. size_t i;
  1521. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1522. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1523. if (scan_res == NULL) {
  1524. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1525. return NULL;
  1526. }
  1527. if (scan_res->fetch_time.sec == 0) {
  1528. /*
  1529. * Make sure we have a valid timestamp if the driver wrapper
  1530. * does not set this.
  1531. */
  1532. os_get_reltime(&scan_res->fetch_time);
  1533. }
  1534. filter_scan_res(wpa_s, scan_res);
  1535. #ifdef CONFIG_WPS
  1536. if (wpas_wps_searching(wpa_s)) {
  1537. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1538. "provisioning rules");
  1539. compar = wpa_scan_result_wps_compar;
  1540. }
  1541. #endif /* CONFIG_WPS */
  1542. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1543. compar);
  1544. dump_scan_res(scan_res);
  1545. wpa_bss_update_start(wpa_s);
  1546. for (i = 0; i < scan_res->num; i++)
  1547. wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
  1548. &scan_res->fetch_time);
  1549. wpa_bss_update_end(wpa_s, info, new_scan);
  1550. return scan_res;
  1551. }
  1552. /**
  1553. * wpa_supplicant_update_scan_results - Update scan results from the driver
  1554. * @wpa_s: Pointer to wpa_supplicant data
  1555. * Returns: 0 on success, -1 on failure
  1556. *
  1557. * This function updates the BSS table within wpa_supplicant based on the
  1558. * currently available scan results from the driver without requesting a new
  1559. * scan. This is used in cases where the driver indicates an association
  1560. * (including roaming within ESS) and wpa_supplicant does not yet have the
  1561. * needed information to complete the connection (e.g., to perform validation
  1562. * steps in 4-way handshake).
  1563. */
  1564. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1565. {
  1566. struct wpa_scan_results *scan_res;
  1567. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1568. if (scan_res == NULL)
  1569. return -1;
  1570. wpa_scan_results_free(scan_res);
  1571. return 0;
  1572. }
  1573. /**
  1574. * scan_only_handler - Reports scan results
  1575. */
  1576. void scan_only_handler(struct wpa_supplicant *wpa_s,
  1577. struct wpa_scan_results *scan_res)
  1578. {
  1579. wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
  1580. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  1581. wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
  1582. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
  1583. wpa_s->manual_scan_id);
  1584. wpa_s->manual_scan_use_id = 0;
  1585. } else {
  1586. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  1587. }
  1588. wpas_notify_scan_results(wpa_s);
  1589. wpas_notify_scan_done(wpa_s, 1);
  1590. if (wpa_s->scan_work) {
  1591. struct wpa_radio_work *work = wpa_s->scan_work;
  1592. wpa_s->scan_work = NULL;
  1593. radio_work_done(work);
  1594. }
  1595. }
  1596. int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
  1597. {
  1598. return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
  1599. }
  1600. struct wpa_driver_scan_params *
  1601. wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
  1602. {
  1603. struct wpa_driver_scan_params *params;
  1604. size_t i;
  1605. u8 *n;
  1606. params = os_zalloc(sizeof(*params));
  1607. if (params == NULL)
  1608. return NULL;
  1609. for (i = 0; i < src->num_ssids; i++) {
  1610. if (src->ssids[i].ssid) {
  1611. n = os_malloc(src->ssids[i].ssid_len);
  1612. if (n == NULL)
  1613. goto failed;
  1614. os_memcpy(n, src->ssids[i].ssid,
  1615. src->ssids[i].ssid_len);
  1616. params->ssids[i].ssid = n;
  1617. params->ssids[i].ssid_len = src->ssids[i].ssid_len;
  1618. }
  1619. }
  1620. params->num_ssids = src->num_ssids;
  1621. if (src->extra_ies) {
  1622. n = os_malloc(src->extra_ies_len);
  1623. if (n == NULL)
  1624. goto failed;
  1625. os_memcpy(n, src->extra_ies, src->extra_ies_len);
  1626. params->extra_ies = n;
  1627. params->extra_ies_len = src->extra_ies_len;
  1628. }
  1629. if (src->freqs) {
  1630. int len = int_array_len(src->freqs);
  1631. params->freqs = os_malloc((len + 1) * sizeof(int));
  1632. if (params->freqs == NULL)
  1633. goto failed;
  1634. os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
  1635. }
  1636. if (src->filter_ssids) {
  1637. params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
  1638. src->num_filter_ssids);
  1639. if (params->filter_ssids == NULL)
  1640. goto failed;
  1641. os_memcpy(params->filter_ssids, src->filter_ssids,
  1642. sizeof(*params->filter_ssids) *
  1643. src->num_filter_ssids);
  1644. params->num_filter_ssids = src->num_filter_ssids;
  1645. }
  1646. params->filter_rssi = src->filter_rssi;
  1647. params->p2p_probe = src->p2p_probe;
  1648. params->only_new_results = src->only_new_results;
  1649. params->low_priority = src->low_priority;
  1650. return params;
  1651. failed:
  1652. wpa_scan_free_params(params);
  1653. return NULL;
  1654. }
  1655. void wpa_scan_free_params(struct wpa_driver_scan_params *params)
  1656. {
  1657. size_t i;
  1658. if (params == NULL)
  1659. return;
  1660. for (i = 0; i < params->num_ssids; i++)
  1661. os_free((u8 *) params->ssids[i].ssid);
  1662. os_free((u8 *) params->extra_ies);
  1663. os_free(params->freqs);
  1664. os_free(params->filter_ssids);
  1665. os_free(params);
  1666. }
  1667. int wpas_start_pno(struct wpa_supplicant *wpa_s)
  1668. {
  1669. int ret, interval;
  1670. size_t i, num_ssid, num_match_ssid;
  1671. struct wpa_ssid *ssid;
  1672. struct wpa_driver_scan_params params;
  1673. if (!wpa_s->sched_scan_supported)
  1674. return -1;
  1675. if (wpa_s->pno || wpa_s->pno_sched_pending)
  1676. return 0;
  1677. if ((wpa_s->wpa_state > WPA_SCANNING) &&
  1678. (wpa_s->wpa_state <= WPA_COMPLETED)) {
  1679. wpa_printf(MSG_ERROR, "PNO: In assoc process");
  1680. return -EAGAIN;
  1681. }
  1682. if (wpa_s->wpa_state == WPA_SCANNING) {
  1683. wpa_supplicant_cancel_scan(wpa_s);
  1684. if (wpa_s->sched_scanning) {
  1685. wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
  1686. "ongoing sched scan");
  1687. wpa_supplicant_cancel_sched_scan(wpa_s);
  1688. wpa_s->pno_sched_pending = 1;
  1689. return 0;
  1690. }
  1691. }
  1692. os_memset(&params, 0, sizeof(params));
  1693. num_ssid = num_match_ssid = 0;
  1694. ssid = wpa_s->conf->ssid;
  1695. while (ssid) {
  1696. if (!wpas_network_disabled(wpa_s, ssid)) {
  1697. num_match_ssid++;
  1698. if (ssid->scan_ssid)
  1699. num_ssid++;
  1700. }
  1701. ssid = ssid->next;
  1702. }
  1703. if (num_match_ssid == 0) {
  1704. wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
  1705. return -1;
  1706. }
  1707. if (num_match_ssid > num_ssid) {
  1708. params.num_ssids++; /* wildcard */
  1709. num_ssid++;
  1710. }
  1711. if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
  1712. wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
  1713. "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
  1714. num_ssid = WPAS_MAX_SCAN_SSIDS;
  1715. }
  1716. if (num_match_ssid > wpa_s->max_match_sets) {
  1717. num_match_ssid = wpa_s->max_match_sets;
  1718. wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
  1719. }
  1720. params.filter_ssids = os_calloc(num_match_ssid,
  1721. sizeof(struct wpa_driver_scan_filter));
  1722. if (params.filter_ssids == NULL)
  1723. return -1;
  1724. i = 0;
  1725. ssid = wpa_s->conf->ssid;
  1726. while (ssid) {
  1727. if (!wpas_network_disabled(wpa_s, ssid)) {
  1728. if (ssid->scan_ssid && params.num_ssids < num_ssid) {
  1729. params.ssids[params.num_ssids].ssid =
  1730. ssid->ssid;
  1731. params.ssids[params.num_ssids].ssid_len =
  1732. ssid->ssid_len;
  1733. params.num_ssids++;
  1734. }
  1735. os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
  1736. ssid->ssid_len);
  1737. params.filter_ssids[i].ssid_len = ssid->ssid_len;
  1738. params.num_filter_ssids++;
  1739. i++;
  1740. if (i == num_match_ssid)
  1741. break;
  1742. }
  1743. ssid = ssid->next;
  1744. }
  1745. if (wpa_s->conf->filter_rssi)
  1746. params.filter_rssi = wpa_s->conf->filter_rssi;
  1747. interval = wpa_s->conf->sched_scan_interval ?
  1748. wpa_s->conf->sched_scan_interval : 10;
  1749. if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
  1750. wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
  1751. params.freqs = wpa_s->manual_sched_scan_freqs;
  1752. }
  1753. ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
  1754. os_free(params.filter_ssids);
  1755. if (ret == 0)
  1756. wpa_s->pno = 1;
  1757. else
  1758. wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
  1759. return ret;
  1760. }
  1761. int wpas_stop_pno(struct wpa_supplicant *wpa_s)
  1762. {
  1763. int ret = 0;
  1764. if (!wpa_s->pno)
  1765. return 0;
  1766. ret = wpa_supplicant_stop_sched_scan(wpa_s);
  1767. wpa_s->pno = 0;
  1768. wpa_s->pno_sched_pending = 0;
  1769. if (wpa_s->wpa_state == WPA_SCANNING)
  1770. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1771. return ret;
  1772. }