scan.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356
  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 (!ssid->disabled)
  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 (!ssid->disabled)
  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 (!ssid->disabled && 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 (tssid->disabled)
  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 (!ssid->disabled && 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)
  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 (!ssid->disabled && !ssid->scan_ssid) {
  654. /* Use wildcard SSID to find this network */
  655. wildcard = 1;
  656. } else if (!ssid->disabled && ssid->ssid_len)
  657. need_ssids++;
  658. #ifdef CONFIG_WPS
  659. if (!ssid->disabled && ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  660. /*
  661. * Normal scan is more reliable and faster for WPS
  662. * operations and since these are for short periods of
  663. * time, the benefit of trying to use sched_scan would
  664. * be limited.
  665. */
  666. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  667. "sched_scan for WPS");
  668. return -1;
  669. }
  670. #endif /* CONFIG_WPS */
  671. }
  672. if (wildcard)
  673. need_ssids++;
  674. if (wpa_s->normal_scans < 3 &&
  675. (need_ssids <= wpa_s->max_scan_ssids ||
  676. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  677. /*
  678. * When normal scan can speed up operations, use that for the
  679. * first operations before starting the sched_scan to allow
  680. * user space sleep more. We do this only if the normal scan
  681. * has functionality that is suitable for this or if the
  682. * sched_scan does not have better support for multiple SSIDs.
  683. */
  684. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  685. "sched_scan for initial scans (normal_scans=%d)",
  686. wpa_s->normal_scans);
  687. return -1;
  688. }
  689. os_memset(&params, 0, sizeof(params));
  690. /* If we can't allocate space for the filters, we just don't filter */
  691. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  692. sizeof(struct wpa_driver_scan_filter));
  693. prev_state = wpa_s->wpa_state;
  694. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  695. wpa_s->wpa_state == WPA_INACTIVE)
  696. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  697. /* Find the starting point from which to continue scanning */
  698. ssid = wpa_s->conf->ssid;
  699. if (wpa_s->prev_sched_ssid) {
  700. while (ssid) {
  701. if (ssid == wpa_s->prev_sched_ssid) {
  702. ssid = ssid->next;
  703. break;
  704. }
  705. ssid = ssid->next;
  706. }
  707. }
  708. if (!ssid || !wpa_s->prev_sched_ssid) {
  709. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  710. wpa_s->sched_scan_interval = 10;
  711. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  712. wpa_s->first_sched_scan = 1;
  713. ssid = wpa_s->conf->ssid;
  714. wpa_s->prev_sched_ssid = ssid;
  715. }
  716. if (wildcard) {
  717. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  718. params.num_ssids++;
  719. }
  720. while (ssid) {
  721. if (ssid->disabled)
  722. goto next;
  723. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  724. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  725. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  726. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  727. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  728. ssid->ssid, ssid->ssid_len);
  729. params.filter_ssids[params.num_filter_ssids].ssid_len =
  730. ssid->ssid_len;
  731. params.num_filter_ssids++;
  732. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  733. {
  734. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  735. "filter for sched_scan - drop filter");
  736. os_free(params.filter_ssids);
  737. params.filter_ssids = NULL;
  738. params.num_filter_ssids = 0;
  739. }
  740. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  741. if (params.num_ssids == max_sched_scan_ssids)
  742. break; /* only room for broadcast SSID */
  743. wpa_dbg(wpa_s, MSG_DEBUG,
  744. "add to active scan ssid: %s",
  745. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  746. params.ssids[params.num_ssids].ssid =
  747. ssid->ssid;
  748. params.ssids[params.num_ssids].ssid_len =
  749. ssid->ssid_len;
  750. params.num_ssids++;
  751. if (params.num_ssids >= max_sched_scan_ssids) {
  752. wpa_s->prev_sched_ssid = ssid;
  753. break;
  754. }
  755. }
  756. next:
  757. wpa_s->prev_sched_ssid = ssid;
  758. ssid = ssid->next;
  759. }
  760. if (params.num_filter_ssids == 0) {
  761. os_free(params.filter_ssids);
  762. params.filter_ssids = NULL;
  763. }
  764. if (wpa_s->wps)
  765. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  766. if (ssid || !wpa_s->first_sched_scan) {
  767. wpa_dbg(wpa_s, MSG_DEBUG,
  768. "Starting sched scan: interval %d (no timeout)",
  769. wpa_s->sched_scan_interval);
  770. } else {
  771. wpa_dbg(wpa_s, MSG_DEBUG,
  772. "Starting sched scan: interval %d timeout %d",
  773. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  774. }
  775. ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
  776. wpa_s->sched_scan_interval);
  777. wpabuf_free(wps_ie);
  778. os_free(params.filter_ssids);
  779. if (ret) {
  780. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  781. if (prev_state != wpa_s->wpa_state)
  782. wpa_supplicant_set_state(wpa_s, prev_state);
  783. return ret;
  784. }
  785. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  786. if (ssid || !wpa_s->first_sched_scan) {
  787. wpa_s->sched_scan_timed_out = 0;
  788. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  789. wpa_supplicant_sched_scan_timeout,
  790. wpa_s, NULL);
  791. wpa_s->first_sched_scan = 0;
  792. wpa_s->sched_scan_timeout /= 2;
  793. wpa_s->sched_scan_interval *= 2;
  794. }
  795. return 0;
  796. }
  797. /**
  798. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  799. * @wpa_s: Pointer to wpa_supplicant data
  800. *
  801. * This function is used to cancel a scan request scheduled with
  802. * wpa_supplicant_req_scan().
  803. */
  804. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  805. {
  806. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  807. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  808. }
  809. /**
  810. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  811. * @wpa_s: Pointer to wpa_supplicant data
  812. *
  813. * This function is used to stop a periodic scheduled scan.
  814. */
  815. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  816. {
  817. if (!wpa_s->sched_scanning)
  818. return;
  819. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  820. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  821. wpa_supplicant_stop_sched_scan(wpa_s);
  822. }
  823. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  824. int scanning)
  825. {
  826. if (wpa_s->scanning != scanning) {
  827. wpa_s->scanning = scanning;
  828. wpas_notify_scanning(wpa_s);
  829. }
  830. }
  831. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  832. {
  833. int rate = 0;
  834. const u8 *ie;
  835. int i;
  836. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  837. for (i = 0; ie && i < ie[1]; i++) {
  838. if ((ie[i + 2] & 0x7f) > rate)
  839. rate = ie[i + 2] & 0x7f;
  840. }
  841. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  842. for (i = 0; ie && i < ie[1]; i++) {
  843. if ((ie[i + 2] & 0x7f) > rate)
  844. rate = ie[i + 2] & 0x7f;
  845. }
  846. return rate;
  847. }
  848. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  849. {
  850. const u8 *end, *pos;
  851. pos = (const u8 *) (res + 1);
  852. end = pos + res->ie_len;
  853. while (pos + 1 < end) {
  854. if (pos + 2 + pos[1] > end)
  855. break;
  856. if (pos[0] == ie)
  857. return pos;
  858. pos += 2 + pos[1];
  859. }
  860. return NULL;
  861. }
  862. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  863. u32 vendor_type)
  864. {
  865. const u8 *end, *pos;
  866. pos = (const u8 *) (res + 1);
  867. end = pos + res->ie_len;
  868. while (pos + 1 < end) {
  869. if (pos + 2 + pos[1] > end)
  870. break;
  871. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  872. vendor_type == WPA_GET_BE32(&pos[2]))
  873. return pos;
  874. pos += 2 + pos[1];
  875. }
  876. return NULL;
  877. }
  878. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  879. u32 vendor_type)
  880. {
  881. struct wpabuf *buf;
  882. const u8 *end, *pos;
  883. buf = wpabuf_alloc(res->ie_len);
  884. if (buf == NULL)
  885. return NULL;
  886. pos = (const u8 *) (res + 1);
  887. end = pos + res->ie_len;
  888. while (pos + 1 < end) {
  889. if (pos + 2 + pos[1] > end)
  890. break;
  891. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  892. vendor_type == WPA_GET_BE32(&pos[2]))
  893. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  894. pos += 2 + pos[1];
  895. }
  896. if (wpabuf_len(buf) == 0) {
  897. wpabuf_free(buf);
  898. buf = NULL;
  899. }
  900. return buf;
  901. }
  902. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  903. const struct wpa_scan_res *res, u32 vendor_type)
  904. {
  905. struct wpabuf *buf;
  906. const u8 *end, *pos;
  907. if (res->beacon_ie_len == 0)
  908. return NULL;
  909. buf = wpabuf_alloc(res->beacon_ie_len);
  910. if (buf == NULL)
  911. return NULL;
  912. pos = (const u8 *) (res + 1);
  913. pos += res->ie_len;
  914. end = pos + res->beacon_ie_len;
  915. while (pos + 1 < end) {
  916. if (pos + 2 + pos[1] > end)
  917. break;
  918. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  919. vendor_type == WPA_GET_BE32(&pos[2]))
  920. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  921. pos += 2 + pos[1];
  922. }
  923. if (wpabuf_len(buf) == 0) {
  924. wpabuf_free(buf);
  925. buf = NULL;
  926. }
  927. return buf;
  928. }
  929. /*
  930. * Channels with a great SNR can operate at full rate. What is a great SNR?
  931. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  932. * rule of thumb is that any SNR above 20 is good." This one
  933. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  934. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  935. * conservative value.
  936. */
  937. #define GREAT_SNR 30
  938. /* Compare function for sorting scan results. Return >0 if @b is considered
  939. * better. */
  940. static int wpa_scan_result_compar(const void *a, const void *b)
  941. {
  942. #define IS_5GHZ(n) (n > 4000)
  943. #define MIN(a,b) a < b ? a : b
  944. struct wpa_scan_res **_wa = (void *) a;
  945. struct wpa_scan_res **_wb = (void *) b;
  946. struct wpa_scan_res *wa = *_wa;
  947. struct wpa_scan_res *wb = *_wb;
  948. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  949. int snr_a, snr_b;
  950. /* WPA/WPA2 support preferred */
  951. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  952. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  953. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  954. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  955. if (wpa_b && !wpa_a)
  956. return 1;
  957. if (!wpa_b && wpa_a)
  958. return -1;
  959. /* privacy support preferred */
  960. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  961. (wb->caps & IEEE80211_CAP_PRIVACY))
  962. return 1;
  963. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  964. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  965. return -1;
  966. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  967. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  968. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  969. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  970. } else {
  971. /* Not suitable information to calculate SNR, so use level */
  972. snr_a = wa->level;
  973. snr_b = wb->level;
  974. }
  975. /* best/max rate preferred if SNR close enough */
  976. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  977. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  978. maxrate_a = wpa_scan_get_max_rate(wa);
  979. maxrate_b = wpa_scan_get_max_rate(wb);
  980. if (maxrate_a != maxrate_b)
  981. return maxrate_b - maxrate_a;
  982. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  983. return IS_5GHZ(wa->freq) ? -1 : 1;
  984. }
  985. /* use freq for channel preference */
  986. /* all things being equal, use SNR; if SNRs are
  987. * identical, use quality values since some drivers may only report
  988. * that value and leave the signal level zero */
  989. if (snr_b == snr_a)
  990. return wb->qual - wa->qual;
  991. return snr_b - snr_a;
  992. #undef MIN
  993. #undef IS_5GHZ
  994. }
  995. #ifdef CONFIG_WPS
  996. /* Compare function for sorting scan results when searching a WPS AP for
  997. * provisioning. Return >0 if @b is considered better. */
  998. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  999. {
  1000. struct wpa_scan_res **_wa = (void *) a;
  1001. struct wpa_scan_res **_wb = (void *) b;
  1002. struct wpa_scan_res *wa = *_wa;
  1003. struct wpa_scan_res *wb = *_wb;
  1004. int uses_wps_a, uses_wps_b;
  1005. struct wpabuf *wps_a, *wps_b;
  1006. int res;
  1007. /* Optimization - check WPS IE existence before allocated memory and
  1008. * doing full reassembly. */
  1009. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1010. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1011. if (uses_wps_a && !uses_wps_b)
  1012. return -1;
  1013. if (!uses_wps_a && uses_wps_b)
  1014. return 1;
  1015. if (uses_wps_a && uses_wps_b) {
  1016. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1017. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1018. res = wps_ap_priority_compar(wps_a, wps_b);
  1019. wpabuf_free(wps_a);
  1020. wpabuf_free(wps_b);
  1021. if (res)
  1022. return res;
  1023. }
  1024. /*
  1025. * Do not use current AP security policy as a sorting criteria during
  1026. * WPS provisioning step since the AP may get reconfigured at the
  1027. * completion of provisioning.
  1028. */
  1029. /* all things being equal, use signal level; if signal levels are
  1030. * identical, use quality values since some drivers may only report
  1031. * that value and leave the signal level zero */
  1032. if (wb->level == wa->level)
  1033. return wb->qual - wa->qual;
  1034. return wb->level - wa->level;
  1035. }
  1036. #endif /* CONFIG_WPS */
  1037. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1038. {
  1039. #ifndef CONFIG_NO_STDOUT_DEBUG
  1040. size_t i;
  1041. if (scan_res->res == NULL || scan_res->num == 0)
  1042. return;
  1043. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1044. for (i = 0; i < scan_res->num; i++) {
  1045. struct wpa_scan_res *r = scan_res->res[i];
  1046. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1047. == WPA_SCAN_LEVEL_DBM) {
  1048. int snr = r->level - r->noise;
  1049. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1050. "noise=%d level=%d snr=%d%s flags=0x%x",
  1051. MAC2STR(r->bssid), r->freq, r->qual,
  1052. r->noise, r->level, snr,
  1053. snr >= GREAT_SNR ? "*" : "", r->flags);
  1054. } else {
  1055. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1056. "noise=%d level=%d flags=0x%x",
  1057. MAC2STR(r->bssid), r->freq, r->qual,
  1058. r->noise, r->level, r->flags);
  1059. }
  1060. }
  1061. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1062. }
  1063. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1064. const u8 *bssid)
  1065. {
  1066. size_t i;
  1067. if (wpa_s->bssid_filter == NULL)
  1068. return 1;
  1069. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1070. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1071. ETH_ALEN) == 0)
  1072. return 1;
  1073. }
  1074. return 0;
  1075. }
  1076. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1077. struct wpa_scan_results *res)
  1078. {
  1079. size_t i, j;
  1080. if (wpa_s->bssid_filter == NULL)
  1081. return;
  1082. for (i = 0, j = 0; i < res->num; i++) {
  1083. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1084. res->res[i]->bssid)) {
  1085. res->res[j++] = res->res[i];
  1086. } else {
  1087. os_free(res->res[i]);
  1088. res->res[i] = NULL;
  1089. }
  1090. }
  1091. if (res->num != j) {
  1092. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1093. (int) (res->num - j));
  1094. res->num = j;
  1095. }
  1096. }
  1097. /**
  1098. * wpa_supplicant_get_scan_results - Get scan results
  1099. * @wpa_s: Pointer to wpa_supplicant data
  1100. * @info: Information about what was scanned or %NULL if not available
  1101. * @new_scan: Whether a new scan was performed
  1102. * Returns: Scan results, %NULL on failure
  1103. *
  1104. * This function request the current scan results from the driver and updates
  1105. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1106. * results with wpa_scan_results_free().
  1107. */
  1108. struct wpa_scan_results *
  1109. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1110. struct scan_info *info, int new_scan)
  1111. {
  1112. struct wpa_scan_results *scan_res;
  1113. size_t i;
  1114. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1115. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1116. if (scan_res == NULL) {
  1117. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1118. return NULL;
  1119. }
  1120. filter_scan_res(wpa_s, scan_res);
  1121. #ifdef CONFIG_WPS
  1122. if (wpas_wps_in_progress(wpa_s)) {
  1123. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1124. "provisioning rules");
  1125. compar = wpa_scan_result_wps_compar;
  1126. }
  1127. #endif /* CONFIG_WPS */
  1128. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1129. compar);
  1130. dump_scan_res(scan_res);
  1131. wpa_bss_update_start(wpa_s);
  1132. for (i = 0; i < scan_res->num; i++)
  1133. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  1134. wpa_bss_update_end(wpa_s, info, new_scan);
  1135. return scan_res;
  1136. }
  1137. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1138. {
  1139. struct wpa_scan_results *scan_res;
  1140. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1141. if (scan_res == NULL)
  1142. return -1;
  1143. wpa_scan_results_free(scan_res);
  1144. return 0;
  1145. }