scan.c 34 KB

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