scan.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. /*
  2. * WPA Supplicant - Scanning
  3. * Copyright (c) 2003-2012, 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 "config.h"
  13. #include "wpa_supplicant_i.h"
  14. #include "driver_i.h"
  15. #include "wps_supplicant.h"
  16. #include "p2p_supplicant.h"
  17. #include "p2p/p2p.h"
  18. #include "hs20_supplicant.h"
  19. #include "notify.h"
  20. #include "bss.h"
  21. #include "scan.h"
  22. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  23. {
  24. struct wpa_ssid *ssid;
  25. union wpa_event_data data;
  26. ssid = wpa_supplicant_get_ssid(wpa_s);
  27. if (ssid == NULL)
  28. return;
  29. if (wpa_s->current_ssid == NULL) {
  30. wpa_s->current_ssid = ssid;
  31. if (wpa_s->current_ssid != NULL)
  32. wpas_notify_network_changed(wpa_s);
  33. }
  34. wpa_supplicant_initiate_eapol(wpa_s);
  35. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
  36. "network - generating associated event");
  37. os_memset(&data, 0, sizeof(data));
  38. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  39. }
  40. #ifdef CONFIG_WPS
  41. static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
  42. enum wps_request_type *req_type)
  43. {
  44. struct wpa_ssid *ssid;
  45. int wps = 0;
  46. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  47. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  48. continue;
  49. wps = 1;
  50. *req_type = wpas_wps_get_req_type(ssid);
  51. if (!ssid->eap.phase1)
  52. continue;
  53. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  54. return 2;
  55. }
  56. #ifdef CONFIG_P2P
  57. if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
  58. !wpa_s->conf->p2p_disabled) {
  59. wpa_s->wps->dev.p2p = 1;
  60. if (!wps) {
  61. wps = 1;
  62. *req_type = WPS_REQ_ENROLLEE_INFO;
  63. }
  64. }
  65. #endif /* CONFIG_P2P */
  66. return wps;
  67. }
  68. #endif /* CONFIG_WPS */
  69. /**
  70. * wpa_supplicant_enabled_networks - Check whether there are enabled networks
  71. * @wpa_s: Pointer to wpa_supplicant data
  72. * Returns: 0 if no networks are enabled, >0 if networks are enabled
  73. *
  74. * This function is used to figure out whether any networks (or Interworking
  75. * with enabled credentials and auto_interworking) are present in the current
  76. * configuration.
  77. */
  78. int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
  79. {
  80. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  81. int count = 0, disabled = 0;
  82. while (ssid) {
  83. if (!wpas_network_disabled(wpa_s, ssid))
  84. count++;
  85. else
  86. disabled++;
  87. ssid = ssid->next;
  88. }
  89. if (wpa_s->conf->cred && wpa_s->conf->interworking &&
  90. wpa_s->conf->auto_interworking)
  91. count++;
  92. if (count == 0 && disabled > 0) {
  93. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
  94. "networks)", disabled);
  95. }
  96. return count;
  97. }
  98. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  99. struct wpa_ssid *ssid)
  100. {
  101. while (ssid) {
  102. if (!wpas_network_disabled(wpa_s, ssid))
  103. break;
  104. ssid = ssid->next;
  105. }
  106. /* ap_scan=2 mode - try to associate with each SSID. */
  107. if (ssid == NULL) {
  108. wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
  109. "end of scan list - go back to beginning");
  110. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  111. wpa_supplicant_req_scan(wpa_s, 0, 0);
  112. return;
  113. }
  114. if (ssid->next) {
  115. /* Continue from the next SSID on the next attempt. */
  116. wpa_s->prev_scan_ssid = ssid;
  117. } else {
  118. /* Start from the beginning of the SSID list. */
  119. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  120. }
  121. wpa_supplicant_associate(wpa_s, NULL, ssid);
  122. }
  123. static int int_array_len(const int *a)
  124. {
  125. int i;
  126. for (i = 0; a && a[i]; i++)
  127. ;
  128. return i;
  129. }
  130. static void int_array_concat(int **res, const int *a)
  131. {
  132. int reslen, alen, i;
  133. int *n;
  134. reslen = int_array_len(*res);
  135. alen = int_array_len(a);
  136. n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
  137. if (n == NULL) {
  138. os_free(*res);
  139. *res = NULL;
  140. return;
  141. }
  142. for (i = 0; i <= alen; i++)
  143. n[reslen + i] = a[i];
  144. *res = n;
  145. }
  146. static int freq_cmp(const void *a, const void *b)
  147. {
  148. int _a = *(int *) a;
  149. int _b = *(int *) b;
  150. if (_a == 0)
  151. return 1;
  152. if (_b == 0)
  153. return -1;
  154. return _a - _b;
  155. }
  156. static void int_array_sort_unique(int *a)
  157. {
  158. int alen;
  159. int i, j;
  160. if (a == NULL)
  161. return;
  162. alen = int_array_len(a);
  163. qsort(a, alen, sizeof(int), freq_cmp);
  164. i = 0;
  165. j = 1;
  166. while (a[i] && a[j]) {
  167. if (a[i] == a[j]) {
  168. j++;
  169. continue;
  170. }
  171. a[++i] = a[j++];
  172. }
  173. if (a[i])
  174. i++;
  175. a[i] = 0;
  176. }
  177. /**
  178. * wpa_supplicant_trigger_scan - Request driver to start a scan
  179. * @wpa_s: Pointer to wpa_supplicant data
  180. * @params: Scan parameters
  181. * Returns: 0 on success, -1 on failure
  182. */
  183. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  184. struct wpa_driver_scan_params *params)
  185. {
  186. int ret;
  187. wpa_supplicant_notify_scanning(wpa_s, 1);
  188. ret = wpa_drv_scan(wpa_s, params);
  189. if (ret) {
  190. wpa_supplicant_notify_scanning(wpa_s, 0);
  191. wpas_notify_scan_done(wpa_s, 0);
  192. } else {
  193. wpa_s->scan_runs++;
  194. wpa_s->normal_scans++;
  195. }
  196. return ret;
  197. }
  198. static void
  199. wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  200. {
  201. struct wpa_supplicant *wpa_s = eloop_ctx;
  202. wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
  203. if (wpa_supplicant_req_sched_scan(wpa_s))
  204. wpa_supplicant_req_scan(wpa_s, 0, 0);
  205. }
  206. static void
  207. wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  208. {
  209. struct wpa_supplicant *wpa_s = eloop_ctx;
  210. wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
  211. wpa_s->sched_scan_timed_out = 1;
  212. wpa_supplicant_cancel_sched_scan(wpa_s);
  213. }
  214. static int
  215. wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
  216. struct wpa_driver_scan_params *params,
  217. int interval)
  218. {
  219. int ret;
  220. wpa_supplicant_notify_scanning(wpa_s, 1);
  221. ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
  222. if (ret)
  223. wpa_supplicant_notify_scanning(wpa_s, 0);
  224. else
  225. wpa_s->sched_scanning = 1;
  226. return ret;
  227. }
  228. static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
  229. {
  230. int ret;
  231. ret = wpa_drv_stop_sched_scan(wpa_s);
  232. if (ret) {
  233. wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
  234. /* TODO: what to do if stopping fails? */
  235. return -1;
  236. }
  237. return ret;
  238. }
  239. static struct wpa_driver_scan_filter *
  240. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  241. {
  242. struct wpa_driver_scan_filter *ssids;
  243. struct wpa_ssid *ssid;
  244. size_t count;
  245. *num_ssids = 0;
  246. if (!conf->filter_ssids)
  247. return NULL;
  248. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  249. if (ssid->ssid && ssid->ssid_len)
  250. count++;
  251. }
  252. if (count == 0)
  253. return NULL;
  254. ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
  255. if (ssids == NULL)
  256. return NULL;
  257. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  258. if (!ssid->ssid || !ssid->ssid_len)
  259. continue;
  260. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  261. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  262. (*num_ssids)++;
  263. }
  264. return ssids;
  265. }
  266. static void wpa_supplicant_optimize_freqs(
  267. struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
  268. {
  269. #ifdef CONFIG_P2P
  270. if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
  271. wpa_s->go_params) {
  272. /* Optimize provisioning state scan based on GO information */
  273. if (wpa_s->p2p_in_provisioning < 5 &&
  274. wpa_s->go_params->freq > 0) {
  275. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
  276. "preferred frequency %d MHz",
  277. wpa_s->go_params->freq);
  278. params->freqs = os_zalloc(2 * sizeof(int));
  279. if (params->freqs)
  280. params->freqs[0] = wpa_s->go_params->freq;
  281. } else if (wpa_s->p2p_in_provisioning < 8 &&
  282. wpa_s->go_params->freq_list[0]) {
  283. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
  284. "channels");
  285. int_array_concat(&params->freqs,
  286. wpa_s->go_params->freq_list);
  287. if (params->freqs)
  288. int_array_sort_unique(params->freqs);
  289. }
  290. wpa_s->p2p_in_provisioning++;
  291. }
  292. #endif /* CONFIG_P2P */
  293. #ifdef CONFIG_WPS
  294. if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  295. /*
  296. * Optimize post-provisioning scan based on channel used
  297. * during provisioning.
  298. */
  299. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
  300. "that was used during provisioning", wpa_s->wps_freq);
  301. params->freqs = os_zalloc(2 * sizeof(int));
  302. if (params->freqs)
  303. params->freqs[0] = wpa_s->wps_freq;
  304. wpa_s->after_wps--;
  305. }
  306. if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
  307. {
  308. /* Optimize provisioning scan based on already known channel */
  309. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
  310. wpa_s->wps_freq);
  311. params->freqs = os_zalloc(2 * sizeof(int));
  312. if (params->freqs)
  313. params->freqs[0] = wpa_s->wps_freq;
  314. wpa_s->known_wps_freq = 0; /* only do this once */
  315. }
  316. #endif /* CONFIG_WPS */
  317. }
  318. #ifdef CONFIG_INTERWORKING
  319. static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
  320. struct wpabuf *buf)
  321. {
  322. if (wpa_s->conf->interworking == 0)
  323. return;
  324. wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
  325. wpabuf_put_u8(buf, 4);
  326. wpabuf_put_u8(buf, 0x00);
  327. wpabuf_put_u8(buf, 0x00);
  328. wpabuf_put_u8(buf, 0x00);
  329. wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
  330. wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
  331. wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
  332. 1 + ETH_ALEN);
  333. wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
  334. /* No Venue Info */
  335. if (!is_zero_ether_addr(wpa_s->conf->hessid))
  336. wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
  337. }
  338. #endif /* CONFIG_INTERWORKING */
  339. static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
  340. {
  341. struct wpabuf *extra_ie = NULL;
  342. #ifdef CONFIG_WPS
  343. int wps = 0;
  344. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  345. #endif /* CONFIG_WPS */
  346. #ifdef CONFIG_INTERWORKING
  347. if (wpa_s->conf->interworking &&
  348. wpabuf_resize(&extra_ie, 100) == 0)
  349. wpas_add_interworking_elements(wpa_s, extra_ie);
  350. #endif /* CONFIG_INTERWORKING */
  351. #ifdef CONFIG_WPS
  352. wps = wpas_wps_in_use(wpa_s, &req_type);
  353. if (wps) {
  354. struct wpabuf *wps_ie;
  355. wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
  356. DEV_PW_DEFAULT,
  357. &wpa_s->wps->dev,
  358. wpa_s->wps->uuid, req_type,
  359. 0, NULL);
  360. if (wps_ie) {
  361. if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
  362. wpabuf_put_buf(extra_ie, wps_ie);
  363. wpabuf_free(wps_ie);
  364. }
  365. }
  366. #ifdef CONFIG_P2P
  367. if (wps) {
  368. size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  369. if (wpabuf_resize(&extra_ie, ielen) == 0)
  370. wpas_p2p_scan_ie(wpa_s, extra_ie);
  371. }
  372. #endif /* CONFIG_P2P */
  373. #endif /* CONFIG_WPS */
  374. return extra_ie;
  375. }
  376. #ifdef CONFIG_P2P
  377. /*
  378. * Check whether there are any enabled networks or credentials that could be
  379. * used for a non-P2P connection.
  380. */
  381. static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
  382. {
  383. struct wpa_ssid *ssid;
  384. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  385. if (wpas_network_disabled(wpa_s, ssid))
  386. continue;
  387. if (!ssid->p2p_group)
  388. return 1;
  389. }
  390. if (wpa_s->conf->cred && wpa_s->conf->interworking &&
  391. wpa_s->conf->auto_interworking)
  392. return 1;
  393. return 0;
  394. }
  395. /*
  396. * Find the operating frequency of any other virtual interface that is using
  397. * the same radio concurrently.
  398. */
  399. static int shared_vif_oper_freq(struct wpa_supplicant *wpa_s)
  400. {
  401. const char *rn, *rn2;
  402. struct wpa_supplicant *ifs;
  403. u8 bssid[ETH_ALEN];
  404. if (!wpa_s->driver->get_radio_name)
  405. return -1;
  406. rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
  407. if (rn == NULL || rn[0] == '\0')
  408. return -1;
  409. for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
  410. if (ifs == wpa_s || !ifs->driver->get_radio_name)
  411. continue;
  412. rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
  413. if (!rn2 || os_strcmp(rn, rn2) != 0)
  414. continue;
  415. if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
  416. continue;
  417. if (ifs->current_ssid->mode == WPAS_MODE_AP ||
  418. ifs->current_ssid->mode == WPAS_MODE_P2P_GO)
  419. return ifs->current_ssid->frequency;
  420. if (wpa_drv_get_bssid(ifs, bssid) == 0)
  421. return ifs->assoc_freq;
  422. }
  423. return 0;
  424. }
  425. #endif /* CONFIG_P2P */
  426. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  427. {
  428. struct wpa_supplicant *wpa_s = eloop_ctx;
  429. struct wpa_ssid *ssid;
  430. enum scan_req_type scan_req = NORMAL_SCAN_REQ;
  431. int ret;
  432. struct wpabuf *extra_ie = NULL;
  433. struct wpa_driver_scan_params params;
  434. struct wpa_driver_scan_params *scan_params;
  435. size_t max_ssids;
  436. enum wpa_states prev_state;
  437. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  438. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  439. return;
  440. }
  441. if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
  442. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
  443. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  444. return;
  445. }
  446. if (!wpa_supplicant_enabled_networks(wpa_s) &&
  447. wpa_s->scan_req == NORMAL_SCAN_REQ) {
  448. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  449. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  450. #ifdef CONFIG_P2P
  451. wpa_s->sta_scan_pending = 0;
  452. #endif /* CONFIG_P2P */
  453. return;
  454. }
  455. if (wpa_s->conf->ap_scan != 0 &&
  456. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  457. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  458. "overriding ap_scan configuration");
  459. wpa_s->conf->ap_scan = 0;
  460. wpas_notify_ap_scan_changed(wpa_s);
  461. }
  462. if (wpa_s->conf->ap_scan == 0) {
  463. wpa_supplicant_gen_assoc_event(wpa_s);
  464. return;
  465. }
  466. #ifdef CONFIG_P2P
  467. if (wpas_p2p_in_progress(wpa_s)) {
  468. if (wpa_s->sta_scan_pending &&
  469. wpas_p2p_in_progress(wpa_s) == 2 &&
  470. wpa_s->global->p2p_cb_on_scan_complete) {
  471. wpa_dbg(wpa_s, MSG_DEBUG, "Process pending station "
  472. "mode scan during P2P search");
  473. } else {
  474. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
  475. "while P2P operation is in progress");
  476. wpa_s->sta_scan_pending = 1;
  477. wpa_supplicant_req_scan(wpa_s, 5, 0);
  478. return;
  479. }
  480. }
  481. #endif /* CONFIG_P2P */
  482. if (wpa_s->conf->ap_scan == 2)
  483. max_ssids = 1;
  484. else {
  485. max_ssids = wpa_s->max_scan_ssids;
  486. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  487. max_ssids = WPAS_MAX_SCAN_SSIDS;
  488. }
  489. scan_req = wpa_s->scan_req;
  490. wpa_s->scan_req = NORMAL_SCAN_REQ;
  491. os_memset(&params, 0, sizeof(params));
  492. prev_state = wpa_s->wpa_state;
  493. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  494. wpa_s->wpa_state == WPA_INACTIVE)
  495. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  496. /*
  497. * If autoscan has set its own scanning parameters
  498. */
  499. if (wpa_s->autoscan_params != NULL) {
  500. scan_params = wpa_s->autoscan_params;
  501. goto scan;
  502. }
  503. if (scan_req != MANUAL_SCAN_REQ && wpa_s->connect_without_scan) {
  504. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  505. if (ssid == wpa_s->connect_without_scan)
  506. break;
  507. }
  508. wpa_s->connect_without_scan = NULL;
  509. if (ssid) {
  510. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  511. "without scan step");
  512. wpa_supplicant_associate(wpa_s, NULL, ssid);
  513. return;
  514. }
  515. }
  516. #ifdef CONFIG_P2P
  517. if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
  518. wpa_s->go_params) {
  519. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
  520. "P2P group formation");
  521. params.ssids[0].ssid = wpa_s->go_params->ssid;
  522. params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
  523. params.num_ssids = 1;
  524. goto ssid_list_set;
  525. }
  526. #endif /* CONFIG_P2P */
  527. /* Find the starting point from which to continue scanning */
  528. ssid = wpa_s->conf->ssid;
  529. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  530. while (ssid) {
  531. if (ssid == wpa_s->prev_scan_ssid) {
  532. ssid = ssid->next;
  533. break;
  534. }
  535. ssid = ssid->next;
  536. }
  537. }
  538. if (scan_req != MANUAL_SCAN_REQ && wpa_s->conf->ap_scan == 2) {
  539. wpa_s->connect_without_scan = NULL;
  540. wpa_s->prev_scan_wildcard = 0;
  541. wpa_supplicant_assoc_try(wpa_s, ssid);
  542. return;
  543. } else if (wpa_s->conf->ap_scan == 2) {
  544. /*
  545. * User-initiated scan request in ap_scan == 2; scan with
  546. * wildcard SSID.
  547. */
  548. ssid = NULL;
  549. } else {
  550. struct wpa_ssid *start = ssid, *tssid;
  551. int freqs_set = 0;
  552. if (ssid == NULL && max_ssids > 1)
  553. ssid = wpa_s->conf->ssid;
  554. while (ssid) {
  555. if (!wpas_network_disabled(wpa_s, ssid) &&
  556. ssid->scan_ssid) {
  557. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  558. ssid->ssid, ssid->ssid_len);
  559. params.ssids[params.num_ssids].ssid =
  560. ssid->ssid;
  561. params.ssids[params.num_ssids].ssid_len =
  562. ssid->ssid_len;
  563. params.num_ssids++;
  564. if (params.num_ssids + 1 >= max_ssids)
  565. break;
  566. }
  567. ssid = ssid->next;
  568. if (ssid == start)
  569. break;
  570. if (ssid == NULL && max_ssids > 1 &&
  571. start != wpa_s->conf->ssid)
  572. ssid = wpa_s->conf->ssid;
  573. }
  574. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  575. if (wpas_network_disabled(wpa_s, tssid))
  576. continue;
  577. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  578. int_array_concat(&params.freqs,
  579. tssid->scan_freq);
  580. } else {
  581. os_free(params.freqs);
  582. params.freqs = NULL;
  583. }
  584. freqs_set = 1;
  585. }
  586. int_array_sort_unique(params.freqs);
  587. }
  588. if (ssid && max_ssids == 1) {
  589. /*
  590. * If the driver is limited to 1 SSID at a time interleave
  591. * wildcard SSID scans with specific SSID scans to avoid
  592. * waiting a long time for a wildcard scan.
  593. */
  594. if (!wpa_s->prev_scan_wildcard) {
  595. params.ssids[0].ssid = NULL;
  596. params.ssids[0].ssid_len = 0;
  597. wpa_s->prev_scan_wildcard = 1;
  598. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  599. "wildcard SSID (Interleave with specific)");
  600. } else {
  601. wpa_s->prev_scan_ssid = ssid;
  602. wpa_s->prev_scan_wildcard = 0;
  603. wpa_dbg(wpa_s, MSG_DEBUG,
  604. "Starting AP scan for specific SSID: %s",
  605. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  606. }
  607. } else if (ssid) {
  608. /* max_ssids > 1 */
  609. wpa_s->prev_scan_ssid = ssid;
  610. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  611. "the scan request");
  612. params.num_ssids++;
  613. } else {
  614. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  615. params.num_ssids++;
  616. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  617. "SSID");
  618. }
  619. #ifdef CONFIG_P2P
  620. ssid_list_set:
  621. #endif /* CONFIG_P2P */
  622. wpa_supplicant_optimize_freqs(wpa_s, &params);
  623. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  624. #ifdef CONFIG_HS20
  625. if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
  626. wpas_hs20_add_indication(extra_ie);
  627. #endif /* CONFIG_HS20 */
  628. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  629. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  630. "generated frequency list");
  631. params.freqs = wpa_s->next_scan_freqs;
  632. } else
  633. os_free(wpa_s->next_scan_freqs);
  634. wpa_s->next_scan_freqs = NULL;
  635. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  636. wpa_s->conf, &params.num_filter_ssids);
  637. if (extra_ie) {
  638. params.extra_ies = wpabuf_head(extra_ie);
  639. params.extra_ies_len = wpabuf_len(extra_ie);
  640. }
  641. #ifdef CONFIG_P2P
  642. if (wpa_s->p2p_in_provisioning ||
  643. (wpa_s->show_group_started && wpa_s->go_params)) {
  644. /*
  645. * The interface may not yet be in P2P mode, so we have to
  646. * explicitly request P2P probe to disable CCK rates.
  647. */
  648. params.p2p_probe = 1;
  649. }
  650. #endif /* CONFIG_P2P */
  651. scan_params = &params;
  652. scan:
  653. #ifdef CONFIG_P2P
  654. /*
  655. * If the driver does not support multi-channel concurrency and a
  656. * virtual interface that shares the same radio with the wpa_s interface
  657. * is operating there may not be need to scan other channels apart from
  658. * the current operating channel on the other virtual interface. Filter
  659. * out other channels in case we are trying to find a connection for a
  660. * station interface when we are not configured to prefer station
  661. * connection and a concurrent operation is already in process.
  662. */
  663. if (wpa_s->scan_for_connection && scan_req == NORMAL_SCAN_REQ &&
  664. !scan_params->freqs && !params.freqs &&
  665. wpas_is_p2p_prioritized(wpa_s) &&
  666. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT) &&
  667. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  668. non_p2p_network_enabled(wpa_s)) {
  669. int freq = shared_vif_oper_freq(wpa_s);
  670. if (freq > 0) {
  671. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only the current "
  672. "operating channel (%d MHz) since driver does "
  673. "not support multi-channel concurrency", freq);
  674. params.freqs = os_zalloc(sizeof(int) * 2);
  675. if (params.freqs)
  676. params.freqs[0] = freq;
  677. scan_params->freqs = params.freqs;
  678. }
  679. }
  680. #endif /* CONFIG_P2P */
  681. ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
  682. wpabuf_free(extra_ie);
  683. os_free(params.freqs);
  684. os_free(params.filter_ssids);
  685. if (ret) {
  686. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  687. if (prev_state != wpa_s->wpa_state)
  688. wpa_supplicant_set_state(wpa_s, prev_state);
  689. /* Restore scan_req since we will try to scan again */
  690. wpa_s->scan_req = scan_req;
  691. wpa_supplicant_req_scan(wpa_s, 1, 0);
  692. } else {
  693. wpa_s->scan_for_connection = 0;
  694. }
  695. }
  696. void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
  697. {
  698. struct os_time remaining, new_int;
  699. int cancelled;
  700. cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
  701. &remaining);
  702. new_int.sec = sec;
  703. new_int.usec = 0;
  704. if (cancelled && os_time_before(&remaining, &new_int)) {
  705. new_int.sec = remaining.sec;
  706. new_int.usec = remaining.usec;
  707. }
  708. eloop_register_timeout(new_int.sec, new_int.usec, wpa_supplicant_scan,
  709. wpa_s, NULL);
  710. wpa_s->scan_interval = sec;
  711. }
  712. /**
  713. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  714. * @wpa_s: Pointer to wpa_supplicant data
  715. * @sec: Number of seconds after which to scan
  716. * @usec: Number of microseconds after which to scan
  717. *
  718. * This function is used to schedule a scan for neighboring access points after
  719. * the specified time.
  720. */
  721. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  722. {
  723. /* If there's at least one network that should be specifically scanned
  724. * then don't cancel the scan and reschedule. Some drivers do
  725. * background scanning which generates frequent scan results, and that
  726. * causes the specific SSID scan to get continually pushed back and
  727. * never happen, which causes hidden APs to never get probe-scanned.
  728. */
  729. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  730. wpa_s->conf->ap_scan == 1) {
  731. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  732. while (ssid) {
  733. if (!wpas_network_disabled(wpa_s, ssid) &&
  734. ssid->scan_ssid)
  735. break;
  736. ssid = ssid->next;
  737. }
  738. if (ssid) {
  739. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  740. "ensure that specific SSID scans occur");
  741. return;
  742. }
  743. }
  744. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  745. sec, usec);
  746. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  747. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  748. }
  749. /**
  750. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  751. * @wpa_s: Pointer to wpa_supplicant data
  752. * @sec: Number of seconds after which to scan
  753. * @usec: Number of microseconds after which to scan
  754. * Returns: 0 on success or -1 otherwise
  755. *
  756. * This function is used to schedule periodic scans for neighboring
  757. * access points after the specified time.
  758. */
  759. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  760. int sec, int usec)
  761. {
  762. if (!wpa_s->sched_scan_supported)
  763. return -1;
  764. eloop_register_timeout(sec, usec,
  765. wpa_supplicant_delayed_sched_scan_timeout,
  766. wpa_s, NULL);
  767. return 0;
  768. }
  769. /**
  770. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  771. * @wpa_s: Pointer to wpa_supplicant data
  772. * Returns: 0 is sched_scan was started or -1 otherwise
  773. *
  774. * This function is used to schedule periodic scans for neighboring
  775. * access points repeating the scan continuously.
  776. */
  777. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  778. {
  779. struct wpa_driver_scan_params params;
  780. struct wpa_driver_scan_params *scan_params;
  781. enum wpa_states prev_state;
  782. struct wpa_ssid *ssid = NULL;
  783. struct wpabuf *extra_ie = NULL;
  784. int ret;
  785. unsigned int max_sched_scan_ssids;
  786. int wildcard = 0;
  787. int need_ssids;
  788. if (!wpa_s->sched_scan_supported)
  789. return -1;
  790. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  791. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  792. else
  793. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  794. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  795. return -1;
  796. if (wpa_s->sched_scanning) {
  797. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  798. return 0;
  799. }
  800. need_ssids = 0;
  801. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  802. if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
  803. /* Use wildcard SSID to find this network */
  804. wildcard = 1;
  805. } else if (!wpas_network_disabled(wpa_s, ssid) &&
  806. ssid->ssid_len)
  807. need_ssids++;
  808. #ifdef CONFIG_WPS
  809. if (!wpas_network_disabled(wpa_s, ssid) &&
  810. ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  811. /*
  812. * Normal scan is more reliable and faster for WPS
  813. * operations and since these are for short periods of
  814. * time, the benefit of trying to use sched_scan would
  815. * be limited.
  816. */
  817. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  818. "sched_scan for WPS");
  819. return -1;
  820. }
  821. #endif /* CONFIG_WPS */
  822. }
  823. if (wildcard)
  824. need_ssids++;
  825. if (wpa_s->normal_scans < 3 &&
  826. (need_ssids <= wpa_s->max_scan_ssids ||
  827. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  828. /*
  829. * When normal scan can speed up operations, use that for the
  830. * first operations before starting the sched_scan to allow
  831. * user space sleep more. We do this only if the normal scan
  832. * has functionality that is suitable for this or if the
  833. * sched_scan does not have better support for multiple SSIDs.
  834. */
  835. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  836. "sched_scan for initial scans (normal_scans=%d)",
  837. wpa_s->normal_scans);
  838. return -1;
  839. }
  840. os_memset(&params, 0, sizeof(params));
  841. /* If we can't allocate space for the filters, we just don't filter */
  842. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  843. sizeof(struct wpa_driver_scan_filter));
  844. prev_state = wpa_s->wpa_state;
  845. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  846. wpa_s->wpa_state == WPA_INACTIVE)
  847. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  848. if (wpa_s->autoscan_params != NULL) {
  849. scan_params = wpa_s->autoscan_params;
  850. goto scan;
  851. }
  852. /* Find the starting point from which to continue scanning */
  853. ssid = wpa_s->conf->ssid;
  854. if (wpa_s->prev_sched_ssid) {
  855. while (ssid) {
  856. if (ssid == wpa_s->prev_sched_ssid) {
  857. ssid = ssid->next;
  858. break;
  859. }
  860. ssid = ssid->next;
  861. }
  862. }
  863. if (!ssid || !wpa_s->prev_sched_ssid) {
  864. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  865. if (wpa_s->sched_scan_interval == 0)
  866. wpa_s->sched_scan_interval = 10;
  867. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  868. wpa_s->first_sched_scan = 1;
  869. ssid = wpa_s->conf->ssid;
  870. wpa_s->prev_sched_ssid = ssid;
  871. }
  872. if (wildcard) {
  873. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  874. params.num_ssids++;
  875. }
  876. while (ssid) {
  877. if (wpas_network_disabled(wpa_s, ssid))
  878. goto next;
  879. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  880. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  881. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  882. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  883. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  884. ssid->ssid, ssid->ssid_len);
  885. params.filter_ssids[params.num_filter_ssids].ssid_len =
  886. ssid->ssid_len;
  887. params.num_filter_ssids++;
  888. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  889. {
  890. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  891. "filter for sched_scan - drop filter");
  892. os_free(params.filter_ssids);
  893. params.filter_ssids = NULL;
  894. params.num_filter_ssids = 0;
  895. }
  896. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  897. if (params.num_ssids == max_sched_scan_ssids)
  898. break; /* only room for broadcast SSID */
  899. wpa_dbg(wpa_s, MSG_DEBUG,
  900. "add to active scan ssid: %s",
  901. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  902. params.ssids[params.num_ssids].ssid =
  903. ssid->ssid;
  904. params.ssids[params.num_ssids].ssid_len =
  905. ssid->ssid_len;
  906. params.num_ssids++;
  907. if (params.num_ssids >= max_sched_scan_ssids) {
  908. wpa_s->prev_sched_ssid = ssid;
  909. do {
  910. ssid = ssid->next;
  911. } while (ssid &&
  912. (wpas_network_disabled(wpa_s, ssid) ||
  913. !ssid->scan_ssid));
  914. break;
  915. }
  916. }
  917. next:
  918. wpa_s->prev_sched_ssid = ssid;
  919. ssid = ssid->next;
  920. }
  921. if (params.num_filter_ssids == 0) {
  922. os_free(params.filter_ssids);
  923. params.filter_ssids = NULL;
  924. }
  925. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  926. if (extra_ie) {
  927. params.extra_ies = wpabuf_head(extra_ie);
  928. params.extra_ies_len = wpabuf_len(extra_ie);
  929. }
  930. scan_params = &params;
  931. scan:
  932. if (ssid || !wpa_s->first_sched_scan) {
  933. wpa_dbg(wpa_s, MSG_DEBUG,
  934. "Starting sched scan: interval %d timeout %d",
  935. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  936. } else {
  937. wpa_dbg(wpa_s, MSG_DEBUG,
  938. "Starting sched scan: interval %d (no timeout)",
  939. wpa_s->sched_scan_interval);
  940. }
  941. ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
  942. wpa_s->sched_scan_interval);
  943. wpabuf_free(extra_ie);
  944. os_free(params.filter_ssids);
  945. if (ret) {
  946. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  947. if (prev_state != wpa_s->wpa_state)
  948. wpa_supplicant_set_state(wpa_s, prev_state);
  949. return ret;
  950. }
  951. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  952. if (ssid || !wpa_s->first_sched_scan) {
  953. wpa_s->sched_scan_timed_out = 0;
  954. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  955. wpa_supplicant_sched_scan_timeout,
  956. wpa_s, NULL);
  957. wpa_s->first_sched_scan = 0;
  958. wpa_s->sched_scan_timeout /= 2;
  959. wpa_s->sched_scan_interval *= 2;
  960. }
  961. return 0;
  962. }
  963. /**
  964. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  965. * @wpa_s: Pointer to wpa_supplicant data
  966. *
  967. * This function is used to cancel a scan request scheduled with
  968. * wpa_supplicant_req_scan().
  969. */
  970. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  971. {
  972. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  973. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  974. }
  975. /**
  976. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  977. * @wpa_s: Pointer to wpa_supplicant data
  978. *
  979. * This function is used to stop a periodic scheduled scan.
  980. */
  981. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  982. {
  983. if (!wpa_s->sched_scanning)
  984. return;
  985. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  986. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  987. wpa_supplicant_stop_sched_scan(wpa_s);
  988. }
  989. /**
  990. * wpa_supplicant_notify_scanning - Indicate possible scan state change
  991. * @wpa_s: Pointer to wpa_supplicant data
  992. * @scanning: Whether scanning is currently in progress
  993. *
  994. * This function is to generate scanning notifycations. It is called whenever
  995. * there may have been a change in scanning (scan started, completed, stopped).
  996. * wpas_notify_scanning() is called whenever the scanning state changed from the
  997. * previously notified state.
  998. */
  999. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  1000. int scanning)
  1001. {
  1002. if (wpa_s->scanning != scanning) {
  1003. wpa_s->scanning = scanning;
  1004. wpas_notify_scanning(wpa_s);
  1005. }
  1006. }
  1007. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  1008. {
  1009. int rate = 0;
  1010. const u8 *ie;
  1011. int i;
  1012. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  1013. for (i = 0; ie && i < ie[1]; i++) {
  1014. if ((ie[i + 2] & 0x7f) > rate)
  1015. rate = ie[i + 2] & 0x7f;
  1016. }
  1017. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  1018. for (i = 0; ie && i < ie[1]; i++) {
  1019. if ((ie[i + 2] & 0x7f) > rate)
  1020. rate = ie[i + 2] & 0x7f;
  1021. }
  1022. return rate;
  1023. }
  1024. /**
  1025. * wpa_scan_get_ie - Fetch a specified information element from a scan result
  1026. * @res: Scan result entry
  1027. * @ie: Information element identitifier (WLAN_EID_*)
  1028. * Returns: Pointer to the information element (id field) or %NULL if not found
  1029. *
  1030. * This function returns the first matching information element in the scan
  1031. * result.
  1032. */
  1033. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  1034. {
  1035. const u8 *end, *pos;
  1036. pos = (const u8 *) (res + 1);
  1037. end = pos + res->ie_len;
  1038. while (pos + 1 < end) {
  1039. if (pos + 2 + pos[1] > end)
  1040. break;
  1041. if (pos[0] == ie)
  1042. return pos;
  1043. pos += 2 + pos[1];
  1044. }
  1045. return NULL;
  1046. }
  1047. /**
  1048. * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
  1049. * @res: Scan result entry
  1050. * @vendor_type: Vendor type (four octets starting the IE payload)
  1051. * Returns: Pointer to the information element (id field) or %NULL if not found
  1052. *
  1053. * This function returns the first matching information element in the scan
  1054. * result.
  1055. */
  1056. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  1057. u32 vendor_type)
  1058. {
  1059. const u8 *end, *pos;
  1060. pos = (const u8 *) (res + 1);
  1061. end = pos + res->ie_len;
  1062. while (pos + 1 < end) {
  1063. if (pos + 2 + pos[1] > end)
  1064. break;
  1065. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1066. vendor_type == WPA_GET_BE32(&pos[2]))
  1067. return pos;
  1068. pos += 2 + pos[1];
  1069. }
  1070. return NULL;
  1071. }
  1072. /**
  1073. * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
  1074. * @res: Scan result entry
  1075. * @vendor_type: Vendor type (four octets starting the IE payload)
  1076. * Returns: Pointer to the information element payload or %NULL if not found
  1077. *
  1078. * This function returns concatenated payload of possibly fragmented vendor
  1079. * specific information elements in the scan result. The caller is responsible
  1080. * for freeing the returned buffer.
  1081. */
  1082. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  1083. u32 vendor_type)
  1084. {
  1085. struct wpabuf *buf;
  1086. const u8 *end, *pos;
  1087. buf = wpabuf_alloc(res->ie_len);
  1088. if (buf == NULL)
  1089. return NULL;
  1090. pos = (const u8 *) (res + 1);
  1091. end = pos + res->ie_len;
  1092. while (pos + 1 < end) {
  1093. if (pos + 2 + pos[1] > end)
  1094. break;
  1095. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1096. vendor_type == WPA_GET_BE32(&pos[2]))
  1097. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1098. pos += 2 + pos[1];
  1099. }
  1100. if (wpabuf_len(buf) == 0) {
  1101. wpabuf_free(buf);
  1102. buf = NULL;
  1103. }
  1104. return buf;
  1105. }
  1106. /*
  1107. * Channels with a great SNR can operate at full rate. What is a great SNR?
  1108. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  1109. * rule of thumb is that any SNR above 20 is good." This one
  1110. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  1111. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  1112. * conservative value.
  1113. */
  1114. #define GREAT_SNR 30
  1115. /* Compare function for sorting scan results. Return >0 if @b is considered
  1116. * better. */
  1117. static int wpa_scan_result_compar(const void *a, const void *b)
  1118. {
  1119. #define IS_5GHZ(n) (n > 4000)
  1120. #define MIN(a,b) a < b ? a : b
  1121. struct wpa_scan_res **_wa = (void *) a;
  1122. struct wpa_scan_res **_wb = (void *) b;
  1123. struct wpa_scan_res *wa = *_wa;
  1124. struct wpa_scan_res *wb = *_wb;
  1125. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  1126. int snr_a, snr_b;
  1127. /* WPA/WPA2 support preferred */
  1128. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  1129. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  1130. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  1131. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  1132. if (wpa_b && !wpa_a)
  1133. return 1;
  1134. if (!wpa_b && wpa_a)
  1135. return -1;
  1136. /* privacy support preferred */
  1137. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  1138. (wb->caps & IEEE80211_CAP_PRIVACY))
  1139. return 1;
  1140. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  1141. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  1142. return -1;
  1143. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  1144. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  1145. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  1146. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  1147. } else {
  1148. /* Not suitable information to calculate SNR, so use level */
  1149. snr_a = wa->level;
  1150. snr_b = wb->level;
  1151. }
  1152. /* best/max rate preferred if SNR close enough */
  1153. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  1154. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  1155. maxrate_a = wpa_scan_get_max_rate(wa);
  1156. maxrate_b = wpa_scan_get_max_rate(wb);
  1157. if (maxrate_a != maxrate_b)
  1158. return maxrate_b - maxrate_a;
  1159. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  1160. return IS_5GHZ(wa->freq) ? -1 : 1;
  1161. }
  1162. /* use freq for channel preference */
  1163. /* all things being equal, use SNR; if SNRs are
  1164. * identical, use quality values since some drivers may only report
  1165. * that value and leave the signal level zero */
  1166. if (snr_b == snr_a)
  1167. return wb->qual - wa->qual;
  1168. return snr_b - snr_a;
  1169. #undef MIN
  1170. #undef IS_5GHZ
  1171. }
  1172. #ifdef CONFIG_WPS
  1173. /* Compare function for sorting scan results when searching a WPS AP for
  1174. * provisioning. Return >0 if @b is considered better. */
  1175. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1176. {
  1177. struct wpa_scan_res **_wa = (void *) a;
  1178. struct wpa_scan_res **_wb = (void *) b;
  1179. struct wpa_scan_res *wa = *_wa;
  1180. struct wpa_scan_res *wb = *_wb;
  1181. int uses_wps_a, uses_wps_b;
  1182. struct wpabuf *wps_a, *wps_b;
  1183. int res;
  1184. /* Optimization - check WPS IE existence before allocated memory and
  1185. * doing full reassembly. */
  1186. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1187. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1188. if (uses_wps_a && !uses_wps_b)
  1189. return -1;
  1190. if (!uses_wps_a && uses_wps_b)
  1191. return 1;
  1192. if (uses_wps_a && uses_wps_b) {
  1193. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1194. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1195. res = wps_ap_priority_compar(wps_a, wps_b);
  1196. wpabuf_free(wps_a);
  1197. wpabuf_free(wps_b);
  1198. if (res)
  1199. return res;
  1200. }
  1201. /*
  1202. * Do not use current AP security policy as a sorting criteria during
  1203. * WPS provisioning step since the AP may get reconfigured at the
  1204. * completion of provisioning.
  1205. */
  1206. /* all things being equal, use signal level; if signal levels are
  1207. * identical, use quality values since some drivers may only report
  1208. * that value and leave the signal level zero */
  1209. if (wb->level == wa->level)
  1210. return wb->qual - wa->qual;
  1211. return wb->level - wa->level;
  1212. }
  1213. #endif /* CONFIG_WPS */
  1214. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1215. {
  1216. #ifndef CONFIG_NO_STDOUT_DEBUG
  1217. size_t i;
  1218. if (scan_res->res == NULL || scan_res->num == 0)
  1219. return;
  1220. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1221. for (i = 0; i < scan_res->num; i++) {
  1222. struct wpa_scan_res *r = scan_res->res[i];
  1223. u8 *pos;
  1224. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1225. == WPA_SCAN_LEVEL_DBM) {
  1226. int snr = r->level - r->noise;
  1227. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1228. "noise=%d level=%d snr=%d%s flags=0x%x",
  1229. MAC2STR(r->bssid), r->freq, r->qual,
  1230. r->noise, r->level, snr,
  1231. snr >= GREAT_SNR ? "*" : "", r->flags);
  1232. } else {
  1233. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1234. "noise=%d level=%d flags=0x%x",
  1235. MAC2STR(r->bssid), r->freq, r->qual,
  1236. r->noise, r->level, r->flags);
  1237. }
  1238. pos = (u8 *) (r + 1);
  1239. if (r->ie_len)
  1240. wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
  1241. pos += r->ie_len;
  1242. if (r->beacon_ie_len)
  1243. wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
  1244. pos, r->beacon_ie_len);
  1245. }
  1246. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1247. }
  1248. /**
  1249. * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
  1250. * @wpa_s: Pointer to wpa_supplicant data
  1251. * @bssid: BSSID to check
  1252. * Returns: 0 if the BSSID is filtered or 1 if not
  1253. *
  1254. * This function is used to filter out specific BSSIDs from scan reslts mainly
  1255. * for testing purposes (SET bssid_filter ctrl_iface command).
  1256. */
  1257. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1258. const u8 *bssid)
  1259. {
  1260. size_t i;
  1261. if (wpa_s->bssid_filter == NULL)
  1262. return 1;
  1263. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1264. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1265. ETH_ALEN) == 0)
  1266. return 1;
  1267. }
  1268. return 0;
  1269. }
  1270. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1271. struct wpa_scan_results *res)
  1272. {
  1273. size_t i, j;
  1274. if (wpa_s->bssid_filter == NULL)
  1275. return;
  1276. for (i = 0, j = 0; i < res->num; i++) {
  1277. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1278. res->res[i]->bssid)) {
  1279. res->res[j++] = res->res[i];
  1280. } else {
  1281. os_free(res->res[i]);
  1282. res->res[i] = NULL;
  1283. }
  1284. }
  1285. if (res->num != j) {
  1286. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1287. (int) (res->num - j));
  1288. res->num = j;
  1289. }
  1290. }
  1291. /**
  1292. * wpa_supplicant_get_scan_results - Get scan results
  1293. * @wpa_s: Pointer to wpa_supplicant data
  1294. * @info: Information about what was scanned or %NULL if not available
  1295. * @new_scan: Whether a new scan was performed
  1296. * Returns: Scan results, %NULL on failure
  1297. *
  1298. * This function request the current scan results from the driver and updates
  1299. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1300. * results with wpa_scan_results_free().
  1301. */
  1302. struct wpa_scan_results *
  1303. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1304. struct scan_info *info, int new_scan)
  1305. {
  1306. struct wpa_scan_results *scan_res;
  1307. size_t i;
  1308. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1309. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1310. if (scan_res == NULL) {
  1311. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1312. return NULL;
  1313. }
  1314. filter_scan_res(wpa_s, scan_res);
  1315. #ifdef CONFIG_WPS
  1316. if (wpas_wps_in_progress(wpa_s)) {
  1317. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1318. "provisioning rules");
  1319. compar = wpa_scan_result_wps_compar;
  1320. }
  1321. #endif /* CONFIG_WPS */
  1322. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1323. compar);
  1324. dump_scan_res(scan_res);
  1325. wpa_bss_update_start(wpa_s);
  1326. for (i = 0; i < scan_res->num; i++)
  1327. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  1328. wpa_bss_update_end(wpa_s, info, new_scan);
  1329. return scan_res;
  1330. }
  1331. /**
  1332. * wpa_supplicant_update_scan_results - Update scan results from the driver
  1333. * @wpa_s: Pointer to wpa_supplicant data
  1334. * Returns: 0 on success, -1 on failure
  1335. *
  1336. * This function updates the BSS table within wpa_supplicant based on the
  1337. * currently available scan results from the driver without requesting a new
  1338. * scan. This is used in cases where the driver indicates an association
  1339. * (including roaming within ESS) and wpa_supplicant does not yet have the
  1340. * needed information to complete the connection (e.g., to perform validation
  1341. * steps in 4-way handshake).
  1342. */
  1343. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1344. {
  1345. struct wpa_scan_results *scan_res;
  1346. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1347. if (scan_res == NULL)
  1348. return -1;
  1349. wpa_scan_results_free(scan_res);
  1350. return 0;
  1351. }