PageRenderTime 63ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/external/bsd/wpa/dist/wpa_supplicant/scan.c

https://github.com/rumpkernel/netbsd-userspace-src
C | 1561 lines | 1103 code | 226 blank | 232 comment | 360 complexity | 431097a64592822f5e65222dfe60f222 MD5 | raw file
  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. /**
  697. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  698. * @wpa_s: Pointer to wpa_supplicant data
  699. * @sec: Number of seconds after which to scan
  700. * @usec: Number of microseconds after which to scan
  701. *
  702. * This function is used to schedule a scan for neighboring access points after
  703. * the specified time.
  704. */
  705. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  706. {
  707. /* If there's at least one network that should be specifically scanned
  708. * then don't cancel the scan and reschedule. Some drivers do
  709. * background scanning which generates frequent scan results, and that
  710. * causes the specific SSID scan to get continually pushed back and
  711. * never happen, which causes hidden APs to never get probe-scanned.
  712. */
  713. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  714. wpa_s->conf->ap_scan == 1) {
  715. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  716. while (ssid) {
  717. if (!wpas_network_disabled(wpa_s, ssid) &&
  718. ssid->scan_ssid)
  719. break;
  720. ssid = ssid->next;
  721. }
  722. if (ssid) {
  723. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  724. "ensure that specific SSID scans occur");
  725. return;
  726. }
  727. }
  728. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  729. sec, usec);
  730. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  731. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  732. }
  733. /**
  734. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  735. * @wpa_s: Pointer to wpa_supplicant data
  736. * @sec: Number of seconds after which to scan
  737. * @usec: Number of microseconds after which to scan
  738. * Returns: 0 on success or -1 otherwise
  739. *
  740. * This function is used to schedule periodic scans for neighboring
  741. * access points after the specified time.
  742. */
  743. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  744. int sec, int usec)
  745. {
  746. if (!wpa_s->sched_scan_supported)
  747. return -1;
  748. eloop_register_timeout(sec, usec,
  749. wpa_supplicant_delayed_sched_scan_timeout,
  750. wpa_s, NULL);
  751. return 0;
  752. }
  753. /**
  754. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  755. * @wpa_s: Pointer to wpa_supplicant data
  756. * Returns: 0 is sched_scan was started or -1 otherwise
  757. *
  758. * This function is used to schedule periodic scans for neighboring
  759. * access points repeating the scan continuously.
  760. */
  761. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  762. {
  763. struct wpa_driver_scan_params params;
  764. struct wpa_driver_scan_params *scan_params;
  765. enum wpa_states prev_state;
  766. struct wpa_ssid *ssid = NULL;
  767. struct wpabuf *extra_ie = NULL;
  768. int ret;
  769. unsigned int max_sched_scan_ssids;
  770. int wildcard = 0;
  771. int need_ssids;
  772. if (!wpa_s->sched_scan_supported)
  773. return -1;
  774. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  775. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  776. else
  777. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  778. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  779. return -1;
  780. if (wpa_s->sched_scanning) {
  781. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  782. return 0;
  783. }
  784. need_ssids = 0;
  785. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  786. if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
  787. /* Use wildcard SSID to find this network */
  788. wildcard = 1;
  789. } else if (!wpas_network_disabled(wpa_s, ssid) &&
  790. ssid->ssid_len)
  791. need_ssids++;
  792. #ifdef CONFIG_WPS
  793. if (!wpas_network_disabled(wpa_s, ssid) &&
  794. ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  795. /*
  796. * Normal scan is more reliable and faster for WPS
  797. * operations and since these are for short periods of
  798. * time, the benefit of trying to use sched_scan would
  799. * be limited.
  800. */
  801. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  802. "sched_scan for WPS");
  803. return -1;
  804. }
  805. #endif /* CONFIG_WPS */
  806. }
  807. if (wildcard)
  808. need_ssids++;
  809. if (wpa_s->normal_scans < 3 &&
  810. (need_ssids <= wpa_s->max_scan_ssids ||
  811. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  812. /*
  813. * When normal scan can speed up operations, use that for the
  814. * first operations before starting the sched_scan to allow
  815. * user space sleep more. We do this only if the normal scan
  816. * has functionality that is suitable for this or if the
  817. * sched_scan does not have better support for multiple SSIDs.
  818. */
  819. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  820. "sched_scan for initial scans (normal_scans=%d)",
  821. wpa_s->normal_scans);
  822. return -1;
  823. }
  824. os_memset(&params, 0, sizeof(params));
  825. /* If we can't allocate space for the filters, we just don't filter */
  826. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  827. sizeof(struct wpa_driver_scan_filter));
  828. prev_state = wpa_s->wpa_state;
  829. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  830. wpa_s->wpa_state == WPA_INACTIVE)
  831. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  832. if (wpa_s->autoscan_params != NULL) {
  833. scan_params = wpa_s->autoscan_params;
  834. goto scan;
  835. }
  836. /* Find the starting point from which to continue scanning */
  837. ssid = wpa_s->conf->ssid;
  838. if (wpa_s->prev_sched_ssid) {
  839. while (ssid) {
  840. if (ssid == wpa_s->prev_sched_ssid) {
  841. ssid = ssid->next;
  842. break;
  843. }
  844. ssid = ssid->next;
  845. }
  846. }
  847. if (!ssid || !wpa_s->prev_sched_ssid) {
  848. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  849. if (wpa_s->sched_scan_interval == 0)
  850. wpa_s->sched_scan_interval = 10;
  851. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  852. wpa_s->first_sched_scan = 1;
  853. ssid = wpa_s->conf->ssid;
  854. wpa_s->prev_sched_ssid = ssid;
  855. }
  856. if (wildcard) {
  857. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  858. params.num_ssids++;
  859. }
  860. while (ssid) {
  861. if (wpas_network_disabled(wpa_s, ssid))
  862. goto next;
  863. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  864. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  865. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  866. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  867. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  868. ssid->ssid, ssid->ssid_len);
  869. params.filter_ssids[params.num_filter_ssids].ssid_len =
  870. ssid->ssid_len;
  871. params.num_filter_ssids++;
  872. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  873. {
  874. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  875. "filter for sched_scan - drop filter");
  876. os_free(params.filter_ssids);
  877. params.filter_ssids = NULL;
  878. params.num_filter_ssids = 0;
  879. }
  880. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  881. if (params.num_ssids == max_sched_scan_ssids)
  882. break; /* only room for broadcast SSID */
  883. wpa_dbg(wpa_s, MSG_DEBUG,
  884. "add to active scan ssid: %s",
  885. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  886. params.ssids[params.num_ssids].ssid =
  887. ssid->ssid;
  888. params.ssids[params.num_ssids].ssid_len =
  889. ssid->ssid_len;
  890. params.num_ssids++;
  891. if (params.num_ssids >= max_sched_scan_ssids) {
  892. wpa_s->prev_sched_ssid = ssid;
  893. do {
  894. ssid = ssid->next;
  895. } while (ssid &&
  896. (wpas_network_disabled(wpa_s, ssid) ||
  897. !ssid->scan_ssid));
  898. break;
  899. }
  900. }
  901. next:
  902. wpa_s->prev_sched_ssid = ssid;
  903. ssid = ssid->next;
  904. }
  905. if (params.num_filter_ssids == 0) {
  906. os_free(params.filter_ssids);
  907. params.filter_ssids = NULL;
  908. }
  909. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  910. if (extra_ie) {
  911. params.extra_ies = wpabuf_head(extra_ie);
  912. params.extra_ies_len = wpabuf_len(extra_ie);
  913. }
  914. scan_params = &params;
  915. scan:
  916. if (ssid || !wpa_s->first_sched_scan) {
  917. wpa_dbg(wpa_s, MSG_DEBUG,
  918. "Starting sched scan: interval %d timeout %d",
  919. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  920. } else {
  921. wpa_dbg(wpa_s, MSG_DEBUG,
  922. "Starting sched scan: interval %d (no timeout)",
  923. wpa_s->sched_scan_interval);
  924. }
  925. ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
  926. wpa_s->sched_scan_interval);
  927. wpabuf_free(extra_ie);
  928. os_free(params.filter_ssids);
  929. if (ret) {
  930. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  931. if (prev_state != wpa_s->wpa_state)
  932. wpa_supplicant_set_state(wpa_s, prev_state);
  933. return ret;
  934. }
  935. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  936. if (ssid || !wpa_s->first_sched_scan) {
  937. wpa_s->sched_scan_timed_out = 0;
  938. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  939. wpa_supplicant_sched_scan_timeout,
  940. wpa_s, NULL);
  941. wpa_s->first_sched_scan = 0;
  942. wpa_s->sched_scan_timeout /= 2;
  943. wpa_s->sched_scan_interval *= 2;
  944. }
  945. return 0;
  946. }
  947. /**
  948. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  949. * @wpa_s: Pointer to wpa_supplicant data
  950. *
  951. * This function is used to cancel a scan request scheduled with
  952. * wpa_supplicant_req_scan().
  953. */
  954. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  955. {
  956. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  957. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  958. }
  959. /**
  960. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  961. * @wpa_s: Pointer to wpa_supplicant data
  962. *
  963. * This function is used to stop a periodic scheduled scan.
  964. */
  965. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  966. {
  967. if (!wpa_s->sched_scanning)
  968. return;
  969. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  970. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  971. wpa_supplicant_stop_sched_scan(wpa_s);
  972. }
  973. /**
  974. * wpa_supplicant_notify_scanning - Indicate possible scan state change
  975. * @wpa_s: Pointer to wpa_supplicant data
  976. * @scanning: Whether scanning is currently in progress
  977. *
  978. * This function is to generate scanning notifycations. It is called whenever
  979. * there may have been a change in scanning (scan started, completed, stopped).
  980. * wpas_notify_scanning() is called whenever the scanning state changed from the
  981. * previously notified state.
  982. */
  983. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  984. int scanning)
  985. {
  986. if (wpa_s->scanning != scanning) {
  987. wpa_s->scanning = scanning;
  988. wpas_notify_scanning(wpa_s);
  989. }
  990. }
  991. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  992. {
  993. int rate = 0;
  994. const u8 *ie;
  995. int i;
  996. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  997. for (i = 0; ie && i < ie[1]; i++) {
  998. if ((ie[i + 2] & 0x7f) > rate)
  999. rate = ie[i + 2] & 0x7f;
  1000. }
  1001. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  1002. for (i = 0; ie && i < ie[1]; i++) {
  1003. if ((ie[i + 2] & 0x7f) > rate)
  1004. rate = ie[i + 2] & 0x7f;
  1005. }
  1006. return rate;
  1007. }
  1008. /**
  1009. * wpa_scan_get_ie - Fetch a specified information element from a scan result
  1010. * @res: Scan result entry
  1011. * @ie: Information element identitifier (WLAN_EID_*)
  1012. * Returns: Pointer to the information element (id field) or %NULL if not found
  1013. *
  1014. * This function returns the first matching information element in the scan
  1015. * result.
  1016. */
  1017. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  1018. {
  1019. const u8 *end, *pos;
  1020. pos = (const u8 *) (res + 1);
  1021. end = pos + res->ie_len;
  1022. while (pos + 1 < end) {
  1023. if (pos + 2 + pos[1] > end)
  1024. break;
  1025. if (pos[0] == ie)
  1026. return pos;
  1027. pos += 2 + pos[1];
  1028. }
  1029. return NULL;
  1030. }
  1031. /**
  1032. * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
  1033. * @res: Scan result entry
  1034. * @vendor_type: Vendor type (four octets starting the IE payload)
  1035. * Returns: Pointer to the information element (id field) or %NULL if not found
  1036. *
  1037. * This function returns the first matching information element in the scan
  1038. * result.
  1039. */
  1040. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  1041. u32 vendor_type)
  1042. {
  1043. const u8 *end, *pos;
  1044. pos = (const u8 *) (res + 1);
  1045. end = pos + res->ie_len;
  1046. while (pos + 1 < end) {
  1047. if (pos + 2 + pos[1] > end)
  1048. break;
  1049. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1050. vendor_type == WPA_GET_BE32(&pos[2]))
  1051. return pos;
  1052. pos += 2 + pos[1];
  1053. }
  1054. return NULL;
  1055. }
  1056. /**
  1057. * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
  1058. * @res: Scan result entry
  1059. * @vendor_type: Vendor type (four octets starting the IE payload)
  1060. * Returns: Pointer to the information element payload or %NULL if not found
  1061. *
  1062. * This function returns concatenated payload of possibly fragmented vendor
  1063. * specific information elements in the scan result. The caller is responsible
  1064. * for freeing the returned buffer.
  1065. */
  1066. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  1067. u32 vendor_type)
  1068. {
  1069. struct wpabuf *buf;
  1070. const u8 *end, *pos;
  1071. buf = wpabuf_alloc(res->ie_len);
  1072. if (buf == NULL)
  1073. return NULL;
  1074. pos = (const u8 *) (res + 1);
  1075. end = pos + res->ie_len;
  1076. while (pos + 1 < end) {
  1077. if (pos + 2 + pos[1] > end)
  1078. break;
  1079. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1080. vendor_type == WPA_GET_BE32(&pos[2]))
  1081. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1082. pos += 2 + pos[1];
  1083. }
  1084. if (wpabuf_len(buf) == 0) {
  1085. wpabuf_free(buf);
  1086. buf = NULL;
  1087. }
  1088. return buf;
  1089. }
  1090. /*
  1091. * Channels with a great SNR can operate at full rate. What is a great SNR?
  1092. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  1093. * rule of thumb is that any SNR above 20 is good." This one
  1094. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  1095. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  1096. * conservative value.
  1097. */
  1098. #define GREAT_SNR 30
  1099. /* Compare function for sorting scan results. Return >0 if @b is considered
  1100. * better. */
  1101. static int wpa_scan_result_compar(const void *a, const void *b)
  1102. {
  1103. #define IS_5GHZ(n) (n > 4000)
  1104. #define MIN(a,b) a < b ? a : b
  1105. struct wpa_scan_res **_wa = (void *) a;
  1106. struct wpa_scan_res **_wb = (void *) b;
  1107. struct wpa_scan_res *wa = *_wa;
  1108. struct wpa_scan_res *wb = *_wb;
  1109. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  1110. int snr_a, snr_b;
  1111. /* WPA/WPA2 support preferred */
  1112. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  1113. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  1114. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  1115. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  1116. if (wpa_b && !wpa_a)
  1117. return 1;
  1118. if (!wpa_b && wpa_a)
  1119. return -1;
  1120. /* privacy support preferred */
  1121. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  1122. (wb->caps & IEEE80211_CAP_PRIVACY))
  1123. return 1;
  1124. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  1125. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  1126. return -1;
  1127. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  1128. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  1129. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  1130. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  1131. } else {
  1132. /* Not suitable information to calculate SNR, so use level */
  1133. snr_a = wa->level;
  1134. snr_b = wb->level;
  1135. }
  1136. /* best/max rate preferred if SNR close enough */
  1137. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  1138. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  1139. maxrate_a = wpa_scan_get_max_rate(wa);
  1140. maxrate_b = wpa_scan_get_max_rate(wb);
  1141. if (maxrate_a != maxrate_b)
  1142. return maxrate_b - maxrate_a;
  1143. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  1144. return IS_5GHZ(wa->freq) ? -1 : 1;
  1145. }
  1146. /* use freq for channel preference */
  1147. /* all things being equal, use SNR; if SNRs are
  1148. * identical, use quality values since some drivers may only report
  1149. * that value and leave the signal level zero */
  1150. if (snr_b == snr_a)
  1151. return wb->qual - wa->qual;
  1152. return snr_b - snr_a;
  1153. #undef MIN
  1154. #undef IS_5GHZ
  1155. }
  1156. #ifdef CONFIG_WPS
  1157. /* Compare function for sorting scan results when searching a WPS AP for
  1158. * provisioning. Return >0 if @b is considered better. */
  1159. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1160. {
  1161. struct wpa_scan_res **_wa = (void *) a;
  1162. struct wpa_scan_res **_wb = (void *) b;
  1163. struct wpa_scan_res *wa = *_wa;
  1164. struct wpa_scan_res *wb = *_wb;
  1165. int uses_wps_a, uses_wps_b;
  1166. struct wpabuf *wps_a, *wps_b;
  1167. int res;
  1168. /* Optimization - check WPS IE existence before allocated memory and
  1169. * doing full reassembly. */
  1170. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1171. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1172. if (uses_wps_a && !uses_wps_b)
  1173. return -1;
  1174. if (!uses_wps_a && uses_wps_b)
  1175. return 1;
  1176. if (uses_wps_a && uses_wps_b) {
  1177. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1178. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1179. res = wps_ap_priority_compar(wps_a, wps_b);
  1180. wpabuf_free(wps_a);
  1181. wpabuf_free(wps_b);
  1182. if (res)
  1183. return res;
  1184. }
  1185. /*
  1186. * Do not use current AP security policy as a sorting criteria during
  1187. * WPS provisioning step since the AP may get reconfigured at the
  1188. * completion of provisioning.
  1189. */
  1190. /* all things being equal, use signal level; if signal levels are
  1191. * identical, use quality values since some drivers may only report
  1192. * that value and leave the signal level zero */
  1193. if (wb->level == wa->level)
  1194. return wb->qual - wa->qual;
  1195. return wb->level - wa->level;
  1196. }
  1197. #endif /* CONFIG_WPS */
  1198. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1199. {
  1200. #ifndef CONFIG_NO_STDOUT_DEBUG
  1201. size_t i;
  1202. if (scan_res->res == NULL || scan_res->num == 0)
  1203. return;
  1204. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1205. for (i = 0; i < scan_res->num; i++) {
  1206. struct wpa_scan_res *r = scan_res->res[i];
  1207. u8 *pos;
  1208. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1209. == WPA_SCAN_LEVEL_DBM) {
  1210. int snr = r->level - r->noise;
  1211. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1212. "noise=%d level=%d snr=%d%s flags=0x%x",
  1213. MAC2STR(r->bssid), r->freq, r->qual,
  1214. r->noise, r->level, snr,
  1215. snr >= GREAT_SNR ? "*" : "", r->flags);
  1216. } else {
  1217. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1218. "noise=%d level=%d flags=0x%x",
  1219. MAC2STR(r->bssid), r->freq, r->qual,
  1220. r->noise, r->level, r->flags);
  1221. }
  1222. pos = (u8 *) (r + 1);
  1223. if (r->ie_len)
  1224. wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
  1225. pos += r->ie_len;
  1226. if (r->beacon_ie_len)
  1227. wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
  1228. pos, r->beacon_ie_len);
  1229. }
  1230. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1231. }
  1232. /**
  1233. * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
  1234. * @wpa_s: Pointer to wpa_supplicant data
  1235. * @bssid: BSSID to check
  1236. * Returns: 0 if the BSSID is filtered or 1 if not
  1237. *
  1238. * This function is used to filter out specific BSSIDs from scan reslts mainly
  1239. * for testing purposes (SET bssid_filter ctrl_iface command).
  1240. */
  1241. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1242. const u8 *bssid)
  1243. {
  1244. size_t i;
  1245. if (wpa_s->bssid_filter == NULL)
  1246. return 1;
  1247. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1248. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1249. ETH_ALEN) == 0)
  1250. return 1;
  1251. }
  1252. return 0;
  1253. }
  1254. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1255. struct wpa_scan_results *res)
  1256. {
  1257. size_t i, j;
  1258. if (wpa_s->bssid_filter == NULL)
  1259. return;
  1260. for (i = 0, j = 0; i < res->num; i++) {
  1261. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1262. res->res[i]->bssid)) {
  1263. res->res[j++] = res->res[i];
  1264. } else {
  1265. os_free(res->res[i]);
  1266. res->res[i] = NULL;
  1267. }
  1268. }
  1269. if (res->num != j) {
  1270. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1271. (int) (res->num - j));
  1272. res->num = j;
  1273. }
  1274. }
  1275. /**
  1276. * wpa_supplicant_get_scan_results - Get scan results
  1277. * @wpa_s: Pointer to wpa_supplicant data
  1278. * @info: Information about what was scanned or %NULL if not available
  1279. * @new_scan: Whether a new scan was performed
  1280. * Returns: Scan results, %NULL on failure
  1281. *
  1282. * This function request the current scan results from the driver and updates
  1283. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1284. * results with wpa_scan_results_free().
  1285. */
  1286. struct wpa_scan_results *
  1287. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1288. struct scan_info *info, int new_scan)
  1289. {
  1290. struct wpa_scan_results *scan_res;
  1291. size_t i;
  1292. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1293. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1294. if (scan_res == NULL) {
  1295. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1296. return NULL;
  1297. }
  1298. filter_scan_res(wpa_s, scan_res);
  1299. #ifdef CONFIG_WPS
  1300. if (wpas_wps_in_progress(wpa_s)) {
  1301. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1302. "provisioning rules");
  1303. compar = wpa_scan_result_wps_compar;
  1304. }
  1305. #endif /* CONFIG_WPS */
  1306. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1307. compar);
  1308. dump_scan_res(scan_res);
  1309. wpa_bss_update_start(wpa_s);
  1310. for (i = 0; i < scan_res->num; i++)
  1311. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  1312. wpa_bss_update_end(wpa_s, info, new_scan);
  1313. return scan_res;
  1314. }
  1315. /**
  1316. * wpa_supplicant_update_scan_results - Update scan results from the driver
  1317. * @wpa_s: Pointer to wpa_supplicant data
  1318. * Returns: 0 on success, -1 on failure
  1319. *
  1320. * This function updates the BSS table within wpa_supplicant based on the
  1321. * currently available scan results from the driver without requesting a new
  1322. * scan. This is used in cases where the driver indicates an association
  1323. * (including roaming within ESS) and wpa_supplicant does not yet have the
  1324. * needed information to complete the connection (e.g., to perform validation
  1325. * steps in 4-way handshake).
  1326. */
  1327. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1328. {
  1329. struct wpa_scan_results *scan_res;
  1330. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1331. if (scan_res == NULL)
  1332. return -1;
  1333. wpa_scan_results_free(scan_res);
  1334. return 0;
  1335. }