scan.c 34 KB

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