scan.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  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_supplicant *wpa_s)
  69. {
  70. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  71. int count = 0;
  72. while (ssid) {
  73. if (!wpas_network_disabled(wpa_s, 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(wpa_s, 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 = NULL;
  359. struct wpa_driver_scan_params params;
  360. struct wpa_driver_scan_params *scan_params;
  361. size_t max_ssids;
  362. enum wpa_states prev_state;
  363. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  364. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  365. return;
  366. }
  367. if (wpa_s->disconnected && !wpa_s->scan_req) {
  368. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  369. return;
  370. }
  371. if (!wpa_supplicant_enabled_networks(wpa_s) &&
  372. !wpa_s->scan_req) {
  373. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  374. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  375. return;
  376. }
  377. if (wpa_s->conf->ap_scan != 0 &&
  378. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  379. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  380. "overriding ap_scan configuration");
  381. wpa_s->conf->ap_scan = 0;
  382. wpas_notify_ap_scan_changed(wpa_s);
  383. }
  384. if (wpa_s->conf->ap_scan == 0) {
  385. wpa_supplicant_gen_assoc_event(wpa_s);
  386. return;
  387. }
  388. #ifdef CONFIG_P2P
  389. if (wpas_p2p_in_progress(wpa_s)) {
  390. if (wpa_s->wpa_state == WPA_SCANNING) {
  391. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
  392. "while P2P operation is in progress");
  393. wpa_supplicant_req_scan(wpa_s, 5, 0);
  394. } else {
  395. wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
  396. "P2P operation is in progress");
  397. }
  398. return;
  399. }
  400. #endif /* CONFIG_P2P */
  401. if (wpa_s->conf->ap_scan == 2)
  402. max_ssids = 1;
  403. else {
  404. max_ssids = wpa_s->max_scan_ssids;
  405. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  406. max_ssids = WPAS_MAX_SCAN_SSIDS;
  407. }
  408. scan_req = wpa_s->scan_req;
  409. wpa_s->scan_req = 0;
  410. os_memset(&params, 0, sizeof(params));
  411. prev_state = wpa_s->wpa_state;
  412. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  413. wpa_s->wpa_state == WPA_INACTIVE)
  414. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  415. /*
  416. * If autoscan has set its own scanning parameters
  417. */
  418. if (wpa_s->autoscan_params != NULL) {
  419. scan_params = wpa_s->autoscan_params;
  420. goto scan;
  421. }
  422. if (scan_req != 2 && wpa_s->connect_without_scan) {
  423. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  424. if (ssid == wpa_s->connect_without_scan)
  425. break;
  426. }
  427. wpa_s->connect_without_scan = NULL;
  428. if (ssid) {
  429. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  430. "without scan step");
  431. wpa_supplicant_associate(wpa_s, NULL, ssid);
  432. return;
  433. }
  434. }
  435. #ifdef CONFIG_P2P
  436. if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
  437. wpa_s->go_params) {
  438. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during "
  439. "P2P group formation");
  440. params.ssids[0].ssid = wpa_s->go_params->ssid;
  441. params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
  442. params.num_ssids = 1;
  443. goto ssid_list_set;
  444. }
  445. #endif /* CONFIG_P2P */
  446. /* Find the starting point from which to continue scanning */
  447. ssid = wpa_s->conf->ssid;
  448. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  449. while (ssid) {
  450. if (ssid == wpa_s->prev_scan_ssid) {
  451. ssid = ssid->next;
  452. break;
  453. }
  454. ssid = ssid->next;
  455. }
  456. }
  457. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  458. wpa_s->connect_without_scan = NULL;
  459. wpa_s->prev_scan_wildcard = 0;
  460. wpa_supplicant_assoc_try(wpa_s, ssid);
  461. return;
  462. } else if (wpa_s->conf->ap_scan == 2) {
  463. /*
  464. * User-initiated scan request in ap_scan == 2; scan with
  465. * wildcard SSID.
  466. */
  467. ssid = NULL;
  468. } else {
  469. struct wpa_ssid *start = ssid, *tssid;
  470. int freqs_set = 0;
  471. if (ssid == NULL && max_ssids > 1)
  472. ssid = wpa_s->conf->ssid;
  473. while (ssid) {
  474. if (!wpas_network_disabled(wpa_s, ssid) &&
  475. ssid->scan_ssid) {
  476. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  477. ssid->ssid, ssid->ssid_len);
  478. params.ssids[params.num_ssids].ssid =
  479. ssid->ssid;
  480. params.ssids[params.num_ssids].ssid_len =
  481. ssid->ssid_len;
  482. params.num_ssids++;
  483. if (params.num_ssids + 1 >= max_ssids)
  484. break;
  485. }
  486. ssid = ssid->next;
  487. if (ssid == start)
  488. break;
  489. if (ssid == NULL && max_ssids > 1 &&
  490. start != wpa_s->conf->ssid)
  491. ssid = wpa_s->conf->ssid;
  492. }
  493. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  494. if (wpas_network_disabled(wpa_s, tssid))
  495. continue;
  496. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  497. int_array_concat(&params.freqs,
  498. tssid->scan_freq);
  499. } else {
  500. os_free(params.freqs);
  501. params.freqs = NULL;
  502. }
  503. freqs_set = 1;
  504. }
  505. int_array_sort_unique(params.freqs);
  506. }
  507. if (ssid && max_ssids == 1) {
  508. /*
  509. * If the driver is limited to 1 SSID at a time interleave
  510. * wildcard SSID scans with specific SSID scans to avoid
  511. * waiting a long time for a wildcard scan.
  512. */
  513. if (!wpa_s->prev_scan_wildcard) {
  514. params.ssids[0].ssid = NULL;
  515. params.ssids[0].ssid_len = 0;
  516. wpa_s->prev_scan_wildcard = 1;
  517. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  518. "wildcard SSID (Interleave with specific)");
  519. } else {
  520. wpa_s->prev_scan_ssid = ssid;
  521. wpa_s->prev_scan_wildcard = 0;
  522. wpa_dbg(wpa_s, MSG_DEBUG,
  523. "Starting AP scan for specific SSID: %s",
  524. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  525. }
  526. } else if (ssid) {
  527. /* max_ssids > 1 */
  528. wpa_s->prev_scan_ssid = ssid;
  529. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  530. "the scan request");
  531. params.num_ssids++;
  532. } else {
  533. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  534. params.num_ssids++;
  535. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  536. "SSID");
  537. }
  538. #ifdef CONFIG_P2P
  539. ssid_list_set:
  540. #endif /* CONFIG_P2P */
  541. wpa_supplicant_optimize_freqs(wpa_s, &params);
  542. extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  543. #ifdef CONFIG_HS20
  544. if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 6) == 0)
  545. wpas_hs20_add_indication(extra_ie);
  546. #endif /* CONFIG_HS20 */
  547. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  548. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  549. "generated frequency list");
  550. params.freqs = wpa_s->next_scan_freqs;
  551. } else
  552. os_free(wpa_s->next_scan_freqs);
  553. wpa_s->next_scan_freqs = NULL;
  554. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  555. wpa_s->conf, &params.num_filter_ssids);
  556. if (extra_ie) {
  557. params.extra_ies = wpabuf_head(extra_ie);
  558. params.extra_ies_len = wpabuf_len(extra_ie);
  559. }
  560. #ifdef CONFIG_P2P
  561. if (wpa_s->p2p_in_provisioning ||
  562. (wpa_s->show_group_started && wpa_s->go_params)) {
  563. /*
  564. * The interface may not yet be in P2P mode, so we have to
  565. * explicitly request P2P probe to disable CCK rates.
  566. */
  567. params.p2p_probe = 1;
  568. }
  569. #endif /* CONFIG_P2P */
  570. scan_params = &params;
  571. scan:
  572. ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
  573. wpabuf_free(extra_ie);
  574. os_free(params.freqs);
  575. os_free(params.filter_ssids);
  576. if (ret) {
  577. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  578. if (prev_state != wpa_s->wpa_state)
  579. wpa_supplicant_set_state(wpa_s, prev_state);
  580. wpa_supplicant_req_scan(wpa_s, 1, 0);
  581. }
  582. }
  583. /**
  584. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  585. * @wpa_s: Pointer to wpa_supplicant data
  586. * @sec: Number of seconds after which to scan
  587. * @usec: Number of microseconds after which to scan
  588. *
  589. * This function is used to schedule a scan for neighboring access points after
  590. * the specified time.
  591. */
  592. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  593. {
  594. /* If there's at least one network that should be specifically scanned
  595. * then don't cancel the scan and reschedule. Some drivers do
  596. * background scanning which generates frequent scan results, and that
  597. * causes the specific SSID scan to get continually pushed back and
  598. * never happen, which causes hidden APs to never get probe-scanned.
  599. */
  600. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  601. wpa_s->conf->ap_scan == 1) {
  602. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  603. while (ssid) {
  604. if (!wpas_network_disabled(wpa_s, ssid) &&
  605. ssid->scan_ssid)
  606. break;
  607. ssid = ssid->next;
  608. }
  609. if (ssid) {
  610. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  611. "ensure that specific SSID scans occur");
  612. return;
  613. }
  614. }
  615. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  616. sec, usec);
  617. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  618. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  619. }
  620. /**
  621. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  622. * @wpa_s: Pointer to wpa_supplicant data
  623. * @sec: Number of seconds after which to scan
  624. * @usec: Number of microseconds after which to scan
  625. *
  626. * This function is used to schedule periodic scans for neighboring
  627. * access points after the specified time.
  628. */
  629. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  630. int sec, int usec)
  631. {
  632. if (!wpa_s->sched_scan_supported)
  633. return -1;
  634. eloop_register_timeout(sec, usec,
  635. wpa_supplicant_delayed_sched_scan_timeout,
  636. wpa_s, NULL);
  637. return 0;
  638. }
  639. /**
  640. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  641. * @wpa_s: Pointer to wpa_supplicant data
  642. *
  643. * This function is used to schedule periodic scans for neighboring
  644. * access points repeating the scan continuously.
  645. */
  646. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  647. {
  648. struct wpa_driver_scan_params params;
  649. struct wpa_driver_scan_params *scan_params;
  650. enum wpa_states prev_state;
  651. struct wpa_ssid *ssid = NULL;
  652. struct wpabuf *wps_ie = NULL;
  653. int ret;
  654. unsigned int max_sched_scan_ssids;
  655. int wildcard = 0;
  656. int need_ssids;
  657. if (!wpa_s->sched_scan_supported)
  658. return -1;
  659. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  660. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  661. else
  662. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  663. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  664. return -1;
  665. if (wpa_s->sched_scanning) {
  666. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  667. return 0;
  668. }
  669. need_ssids = 0;
  670. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  671. if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
  672. /* Use wildcard SSID to find this network */
  673. wildcard = 1;
  674. } else if (!wpas_network_disabled(wpa_s, ssid) &&
  675. ssid->ssid_len)
  676. need_ssids++;
  677. #ifdef CONFIG_WPS
  678. if (!wpas_network_disabled(wpa_s, ssid) &&
  679. ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  680. /*
  681. * Normal scan is more reliable and faster for WPS
  682. * operations and since these are for short periods of
  683. * time, the benefit of trying to use sched_scan would
  684. * be limited.
  685. */
  686. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  687. "sched_scan for WPS");
  688. return -1;
  689. }
  690. #endif /* CONFIG_WPS */
  691. }
  692. if (wildcard)
  693. need_ssids++;
  694. if (wpa_s->normal_scans < 3 &&
  695. (need_ssids <= wpa_s->max_scan_ssids ||
  696. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  697. /*
  698. * When normal scan can speed up operations, use that for the
  699. * first operations before starting the sched_scan to allow
  700. * user space sleep more. We do this only if the normal scan
  701. * has functionality that is suitable for this or if the
  702. * sched_scan does not have better support for multiple SSIDs.
  703. */
  704. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  705. "sched_scan for initial scans (normal_scans=%d)",
  706. wpa_s->normal_scans);
  707. return -1;
  708. }
  709. os_memset(&params, 0, sizeof(params));
  710. /* If we can't allocate space for the filters, we just don't filter */
  711. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  712. sizeof(struct wpa_driver_scan_filter));
  713. prev_state = wpa_s->wpa_state;
  714. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  715. wpa_s->wpa_state == WPA_INACTIVE)
  716. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  717. if (wpa_s->autoscan_params != NULL) {
  718. scan_params = wpa_s->autoscan_params;
  719. goto scan;
  720. }
  721. /* Find the starting point from which to continue scanning */
  722. ssid = wpa_s->conf->ssid;
  723. if (wpa_s->prev_sched_ssid) {
  724. while (ssid) {
  725. if (ssid == wpa_s->prev_sched_ssid) {
  726. ssid = ssid->next;
  727. break;
  728. }
  729. ssid = ssid->next;
  730. }
  731. }
  732. if (!ssid || !wpa_s->prev_sched_ssid) {
  733. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  734. if (wpa_s->sched_scan_interval == 0)
  735. wpa_s->sched_scan_interval = 10;
  736. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  737. wpa_s->first_sched_scan = 1;
  738. ssid = wpa_s->conf->ssid;
  739. wpa_s->prev_sched_ssid = ssid;
  740. }
  741. if (wildcard) {
  742. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  743. params.num_ssids++;
  744. }
  745. while (ssid) {
  746. if (wpas_network_disabled(wpa_s, ssid))
  747. goto next;
  748. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  749. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  750. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  751. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  752. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  753. ssid->ssid, ssid->ssid_len);
  754. params.filter_ssids[params.num_filter_ssids].ssid_len =
  755. ssid->ssid_len;
  756. params.num_filter_ssids++;
  757. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  758. {
  759. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  760. "filter for sched_scan - drop filter");
  761. os_free(params.filter_ssids);
  762. params.filter_ssids = NULL;
  763. params.num_filter_ssids = 0;
  764. }
  765. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  766. if (params.num_ssids == max_sched_scan_ssids)
  767. break; /* only room for broadcast SSID */
  768. wpa_dbg(wpa_s, MSG_DEBUG,
  769. "add to active scan ssid: %s",
  770. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  771. params.ssids[params.num_ssids].ssid =
  772. ssid->ssid;
  773. params.ssids[params.num_ssids].ssid_len =
  774. ssid->ssid_len;
  775. params.num_ssids++;
  776. if (params.num_ssids >= max_sched_scan_ssids) {
  777. wpa_s->prev_sched_ssid = ssid;
  778. do {
  779. ssid = ssid->next;
  780. } while (ssid &&
  781. (wpas_network_disabled(wpa_s, ssid) ||
  782. !ssid->scan_ssid));
  783. break;
  784. }
  785. }
  786. next:
  787. wpa_s->prev_sched_ssid = ssid;
  788. ssid = ssid->next;
  789. }
  790. if (params.num_filter_ssids == 0) {
  791. os_free(params.filter_ssids);
  792. params.filter_ssids = NULL;
  793. }
  794. if (wpa_s->wps)
  795. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  796. scan_params = &params;
  797. scan:
  798. if (ssid || !wpa_s->first_sched_scan) {
  799. wpa_dbg(wpa_s, MSG_DEBUG,
  800. "Starting sched scan: interval %d timeout %d",
  801. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  802. } else {
  803. wpa_dbg(wpa_s, MSG_DEBUG,
  804. "Starting sched scan: interval %d (no timeout)",
  805. wpa_s->sched_scan_interval);
  806. }
  807. ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
  808. wpa_s->sched_scan_interval);
  809. wpabuf_free(wps_ie);
  810. os_free(params.filter_ssids);
  811. if (ret) {
  812. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  813. if (prev_state != wpa_s->wpa_state)
  814. wpa_supplicant_set_state(wpa_s, prev_state);
  815. return ret;
  816. }
  817. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  818. if (ssid || !wpa_s->first_sched_scan) {
  819. wpa_s->sched_scan_timed_out = 0;
  820. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  821. wpa_supplicant_sched_scan_timeout,
  822. wpa_s, NULL);
  823. wpa_s->first_sched_scan = 0;
  824. wpa_s->sched_scan_timeout /= 2;
  825. wpa_s->sched_scan_interval *= 2;
  826. }
  827. return 0;
  828. }
  829. /**
  830. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  831. * @wpa_s: Pointer to wpa_supplicant data
  832. *
  833. * This function is used to cancel a scan request scheduled with
  834. * wpa_supplicant_req_scan().
  835. */
  836. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  837. {
  838. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  839. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  840. }
  841. /**
  842. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  843. * @wpa_s: Pointer to wpa_supplicant data
  844. *
  845. * This function is used to stop a periodic scheduled scan.
  846. */
  847. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  848. {
  849. if (!wpa_s->sched_scanning)
  850. return;
  851. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  852. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  853. wpa_supplicant_stop_sched_scan(wpa_s);
  854. }
  855. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  856. int scanning)
  857. {
  858. if (wpa_s->scanning != scanning) {
  859. wpa_s->scanning = scanning;
  860. wpas_notify_scanning(wpa_s);
  861. }
  862. }
  863. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  864. {
  865. int rate = 0;
  866. const u8 *ie;
  867. int i;
  868. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  869. for (i = 0; ie && i < ie[1]; i++) {
  870. if ((ie[i + 2] & 0x7f) > rate)
  871. rate = ie[i + 2] & 0x7f;
  872. }
  873. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  874. for (i = 0; ie && i < ie[1]; i++) {
  875. if ((ie[i + 2] & 0x7f) > rate)
  876. rate = ie[i + 2] & 0x7f;
  877. }
  878. return rate;
  879. }
  880. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  881. {
  882. const u8 *end, *pos;
  883. pos = (const u8 *) (res + 1);
  884. end = pos + res->ie_len;
  885. while (pos + 1 < end) {
  886. if (pos + 2 + pos[1] > end)
  887. break;
  888. if (pos[0] == ie)
  889. return pos;
  890. pos += 2 + pos[1];
  891. }
  892. return NULL;
  893. }
  894. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  895. u32 vendor_type)
  896. {
  897. const u8 *end, *pos;
  898. pos = (const u8 *) (res + 1);
  899. end = pos + res->ie_len;
  900. while (pos + 1 < end) {
  901. if (pos + 2 + pos[1] > end)
  902. break;
  903. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  904. vendor_type == WPA_GET_BE32(&pos[2]))
  905. return pos;
  906. pos += 2 + pos[1];
  907. }
  908. return NULL;
  909. }
  910. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  911. u32 vendor_type)
  912. {
  913. struct wpabuf *buf;
  914. const u8 *end, *pos;
  915. buf = wpabuf_alloc(res->ie_len);
  916. if (buf == NULL)
  917. return NULL;
  918. pos = (const u8 *) (res + 1);
  919. end = pos + res->ie_len;
  920. while (pos + 1 < end) {
  921. if (pos + 2 + pos[1] > end)
  922. break;
  923. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  924. vendor_type == WPA_GET_BE32(&pos[2]))
  925. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  926. pos += 2 + pos[1];
  927. }
  928. if (wpabuf_len(buf) == 0) {
  929. wpabuf_free(buf);
  930. buf = NULL;
  931. }
  932. return buf;
  933. }
  934. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  935. const struct wpa_scan_res *res, u32 vendor_type)
  936. {
  937. struct wpabuf *buf;
  938. const u8 *end, *pos;
  939. if (res->beacon_ie_len == 0)
  940. return NULL;
  941. buf = wpabuf_alloc(res->beacon_ie_len);
  942. if (buf == NULL)
  943. return NULL;
  944. pos = (const u8 *) (res + 1);
  945. pos += res->ie_len;
  946. end = pos + res->beacon_ie_len;
  947. while (pos + 1 < end) {
  948. if (pos + 2 + pos[1] > end)
  949. break;
  950. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  951. vendor_type == WPA_GET_BE32(&pos[2]))
  952. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  953. pos += 2 + pos[1];
  954. }
  955. if (wpabuf_len(buf) == 0) {
  956. wpabuf_free(buf);
  957. buf = NULL;
  958. }
  959. return buf;
  960. }
  961. /*
  962. * Channels with a great SNR can operate at full rate. What is a great SNR?
  963. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  964. * rule of thumb is that any SNR above 20 is good." This one
  965. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  966. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  967. * conservative value.
  968. */
  969. #define GREAT_SNR 30
  970. /* Compare function for sorting scan results. Return >0 if @b is considered
  971. * better. */
  972. static int wpa_scan_result_compar(const void *a, const void *b)
  973. {
  974. #define IS_5GHZ(n) (n > 4000)
  975. #define MIN(a,b) a < b ? a : b
  976. struct wpa_scan_res **_wa = (void *) a;
  977. struct wpa_scan_res **_wb = (void *) b;
  978. struct wpa_scan_res *wa = *_wa;
  979. struct wpa_scan_res *wb = *_wb;
  980. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  981. int snr_a, snr_b;
  982. /* WPA/WPA2 support preferred */
  983. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  984. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  985. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  986. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  987. if (wpa_b && !wpa_a)
  988. return 1;
  989. if (!wpa_b && wpa_a)
  990. return -1;
  991. /* privacy support preferred */
  992. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  993. (wb->caps & IEEE80211_CAP_PRIVACY))
  994. return 1;
  995. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  996. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  997. return -1;
  998. if ((wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) &&
  999. !((wa->flags | wb->flags) & WPA_SCAN_NOISE_INVALID)) {
  1000. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  1001. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  1002. } else {
  1003. /* Not suitable information to calculate SNR, so use level */
  1004. snr_a = wa->level;
  1005. snr_b = wb->level;
  1006. }
  1007. /* best/max rate preferred if SNR close enough */
  1008. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  1009. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  1010. maxrate_a = wpa_scan_get_max_rate(wa);
  1011. maxrate_b = wpa_scan_get_max_rate(wb);
  1012. if (maxrate_a != maxrate_b)
  1013. return maxrate_b - maxrate_a;
  1014. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  1015. return IS_5GHZ(wa->freq) ? -1 : 1;
  1016. }
  1017. /* use freq for channel preference */
  1018. /* all things being equal, use SNR; if SNRs are
  1019. * identical, use quality values since some drivers may only report
  1020. * that value and leave the signal level zero */
  1021. if (snr_b == snr_a)
  1022. return wb->qual - wa->qual;
  1023. return snr_b - snr_a;
  1024. #undef MIN
  1025. #undef IS_5GHZ
  1026. }
  1027. #ifdef CONFIG_WPS
  1028. /* Compare function for sorting scan results when searching a WPS AP for
  1029. * provisioning. Return >0 if @b is considered better. */
  1030. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1031. {
  1032. struct wpa_scan_res **_wa = (void *) a;
  1033. struct wpa_scan_res **_wb = (void *) b;
  1034. struct wpa_scan_res *wa = *_wa;
  1035. struct wpa_scan_res *wb = *_wb;
  1036. int uses_wps_a, uses_wps_b;
  1037. struct wpabuf *wps_a, *wps_b;
  1038. int res;
  1039. /* Optimization - check WPS IE existence before allocated memory and
  1040. * doing full reassembly. */
  1041. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1042. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1043. if (uses_wps_a && !uses_wps_b)
  1044. return -1;
  1045. if (!uses_wps_a && uses_wps_b)
  1046. return 1;
  1047. if (uses_wps_a && uses_wps_b) {
  1048. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1049. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1050. res = wps_ap_priority_compar(wps_a, wps_b);
  1051. wpabuf_free(wps_a);
  1052. wpabuf_free(wps_b);
  1053. if (res)
  1054. return res;
  1055. }
  1056. /*
  1057. * Do not use current AP security policy as a sorting criteria during
  1058. * WPS provisioning step since the AP may get reconfigured at the
  1059. * completion of provisioning.
  1060. */
  1061. /* all things being equal, use signal level; if signal levels are
  1062. * identical, use quality values since some drivers may only report
  1063. * that value and leave the signal level zero */
  1064. if (wb->level == wa->level)
  1065. return wb->qual - wa->qual;
  1066. return wb->level - wa->level;
  1067. }
  1068. #endif /* CONFIG_WPS */
  1069. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1070. {
  1071. #ifndef CONFIG_NO_STDOUT_DEBUG
  1072. size_t i;
  1073. if (scan_res->res == NULL || scan_res->num == 0)
  1074. return;
  1075. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1076. for (i = 0; i < scan_res->num; i++) {
  1077. struct wpa_scan_res *r = scan_res->res[i];
  1078. u8 *pos;
  1079. if ((r->flags & (WPA_SCAN_LEVEL_DBM | WPA_SCAN_NOISE_INVALID))
  1080. == WPA_SCAN_LEVEL_DBM) {
  1081. int snr = r->level - r->noise;
  1082. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1083. "noise=%d level=%d snr=%d%s flags=0x%x",
  1084. MAC2STR(r->bssid), r->freq, r->qual,
  1085. r->noise, r->level, snr,
  1086. snr >= GREAT_SNR ? "*" : "", r->flags);
  1087. } else {
  1088. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1089. "noise=%d level=%d flags=0x%x",
  1090. MAC2STR(r->bssid), r->freq, r->qual,
  1091. r->noise, r->level, r->flags);
  1092. }
  1093. pos = (u8 *) (r + 1);
  1094. if (r->ie_len)
  1095. wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
  1096. pos += r->ie_len;
  1097. if (r->beacon_ie_len)
  1098. wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
  1099. pos, r->beacon_ie_len);
  1100. }
  1101. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1102. }
  1103. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1104. const u8 *bssid)
  1105. {
  1106. size_t i;
  1107. if (wpa_s->bssid_filter == NULL)
  1108. return 1;
  1109. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1110. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1111. ETH_ALEN) == 0)
  1112. return 1;
  1113. }
  1114. return 0;
  1115. }
  1116. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1117. struct wpa_scan_results *res)
  1118. {
  1119. size_t i, j;
  1120. if (wpa_s->bssid_filter == NULL)
  1121. return;
  1122. for (i = 0, j = 0; i < res->num; i++) {
  1123. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1124. res->res[i]->bssid)) {
  1125. res->res[j++] = res->res[i];
  1126. } else {
  1127. os_free(res->res[i]);
  1128. res->res[i] = NULL;
  1129. }
  1130. }
  1131. if (res->num != j) {
  1132. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1133. (int) (res->num - j));
  1134. res->num = j;
  1135. }
  1136. }
  1137. /**
  1138. * wpa_supplicant_get_scan_results - Get scan results
  1139. * @wpa_s: Pointer to wpa_supplicant data
  1140. * @info: Information about what was scanned or %NULL if not available
  1141. * @new_scan: Whether a new scan was performed
  1142. * Returns: Scan results, %NULL on failure
  1143. *
  1144. * This function request the current scan results from the driver and updates
  1145. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1146. * results with wpa_scan_results_free().
  1147. */
  1148. struct wpa_scan_results *
  1149. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1150. struct scan_info *info, int new_scan)
  1151. {
  1152. struct wpa_scan_results *scan_res;
  1153. size_t i;
  1154. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1155. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1156. if (scan_res == NULL) {
  1157. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1158. return NULL;
  1159. }
  1160. filter_scan_res(wpa_s, scan_res);
  1161. #ifdef CONFIG_WPS
  1162. if (wpas_wps_in_progress(wpa_s)) {
  1163. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1164. "provisioning rules");
  1165. compar = wpa_scan_result_wps_compar;
  1166. }
  1167. #endif /* CONFIG_WPS */
  1168. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1169. compar);
  1170. dump_scan_res(scan_res);
  1171. wpa_bss_update_start(wpa_s);
  1172. for (i = 0; i < scan_res->num; i++)
  1173. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  1174. wpa_bss_update_end(wpa_s, info, new_scan);
  1175. return scan_res;
  1176. }
  1177. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1178. {
  1179. struct wpa_scan_results *scan_res;
  1180. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1181. if (scan_res == NULL)
  1182. return -1;
  1183. wpa_scan_results_free(scan_res);
  1184. return 0;
  1185. }