scan.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  1. /*
  2. * WPA Supplicant - Scanning
  3. * Copyright (c) 2003-2010, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "utils/includes.h"
  15. #include "utils/common.h"
  16. #include "utils/eloop.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "config.h"
  19. #include "wpa_supplicant_i.h"
  20. #include "driver_i.h"
  21. #include "mlme.h"
  22. #include "wps_supplicant.h"
  23. #include "p2p_supplicant.h"
  24. #include "p2p/p2p.h"
  25. #include "notify.h"
  26. #include "bss.h"
  27. #include "scan.h"
  28. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  29. {
  30. struct wpa_ssid *ssid;
  31. union wpa_event_data data;
  32. ssid = wpa_supplicant_get_ssid(wpa_s);
  33. if (ssid == NULL)
  34. return;
  35. if (wpa_s->current_ssid == NULL) {
  36. wpa_s->current_ssid = ssid;
  37. if (wpa_s->current_ssid != NULL)
  38. wpas_notify_network_changed(wpa_s);
  39. }
  40. wpa_supplicant_initiate_eapol(wpa_s);
  41. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
  42. "network - generating associated event");
  43. os_memset(&data, 0, sizeof(data));
  44. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  45. }
  46. #ifdef CONFIG_WPS
  47. static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
  48. enum wps_request_type *req_type)
  49. {
  50. struct wpa_ssid *ssid;
  51. int wps = 0;
  52. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  53. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  54. continue;
  55. wps = 1;
  56. *req_type = wpas_wps_get_req_type(ssid);
  57. if (!ssid->eap.phase1)
  58. continue;
  59. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  60. return 2;
  61. }
  62. #ifdef CONFIG_P2P
  63. wpa_s->wps->dev.p2p = 1;
  64. if (!wps) {
  65. wps = 1;
  66. *req_type = WPS_REQ_ENROLLEE_INFO;
  67. }
  68. #endif /* CONFIG_P2P */
  69. return wps;
  70. }
  71. #endif /* CONFIG_WPS */
  72. int wpa_supplicant_enabled_networks(struct wpa_config *conf)
  73. {
  74. struct wpa_ssid *ssid = conf->ssid;
  75. int count = 0;
  76. while (ssid) {
  77. if (!ssid->disabled)
  78. count++;
  79. ssid = ssid->next;
  80. }
  81. return count;
  82. }
  83. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  84. struct wpa_ssid *ssid)
  85. {
  86. while (ssid) {
  87. if (!ssid->disabled)
  88. break;
  89. ssid = ssid->next;
  90. }
  91. /* ap_scan=2 mode - try to associate with each SSID. */
  92. if (ssid == NULL) {
  93. wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
  94. "end of scan list - go back to beginning");
  95. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  96. wpa_supplicant_req_scan(wpa_s, 0, 0);
  97. return;
  98. }
  99. if (ssid->next) {
  100. /* Continue from the next SSID on the next attempt. */
  101. wpa_s->prev_scan_ssid = ssid;
  102. } else {
  103. /* Start from the beginning of the SSID list. */
  104. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  105. }
  106. wpa_supplicant_associate(wpa_s, NULL, ssid);
  107. }
  108. static int int_array_len(const int *a)
  109. {
  110. int i;
  111. for (i = 0; a && a[i]; i++)
  112. ;
  113. return i;
  114. }
  115. static void int_array_concat(int **res, const int *a)
  116. {
  117. int reslen, alen, i;
  118. int *n;
  119. reslen = int_array_len(*res);
  120. alen = int_array_len(a);
  121. n = os_realloc(*res, (reslen + alen + 1) * sizeof(int));
  122. if (n == NULL) {
  123. os_free(*res);
  124. *res = NULL;
  125. return;
  126. }
  127. for (i = 0; i <= alen; i++)
  128. n[reslen + i] = a[i];
  129. *res = n;
  130. }
  131. static int freq_cmp(const void *a, const void *b)
  132. {
  133. int _a = *(int *) a;
  134. int _b = *(int *) b;
  135. if (_a == 0)
  136. return 1;
  137. if (_b == 0)
  138. return -1;
  139. return _a - _b;
  140. }
  141. static void int_array_sort_unique(int *a)
  142. {
  143. int alen;
  144. int i, j;
  145. if (a == NULL)
  146. return;
  147. alen = int_array_len(a);
  148. qsort(a, alen, sizeof(int), freq_cmp);
  149. i = 0;
  150. j = 1;
  151. while (a[i] && a[j]) {
  152. if (a[i] == a[j]) {
  153. j++;
  154. continue;
  155. }
  156. a[++i] = a[j++];
  157. }
  158. if (a[i])
  159. i++;
  160. a[i] = 0;
  161. }
  162. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  163. struct wpa_driver_scan_params *params)
  164. {
  165. int ret;
  166. wpa_supplicant_notify_scanning(wpa_s, 1);
  167. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  168. ret = ieee80211_sta_req_scan(wpa_s, params);
  169. else
  170. ret = wpa_drv_scan(wpa_s, params);
  171. if (ret) {
  172. wpa_supplicant_notify_scanning(wpa_s, 0);
  173. wpas_notify_scan_done(wpa_s, 0);
  174. } else
  175. wpa_s->scan_runs++;
  176. return ret;
  177. }
  178. static void
  179. wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  180. {
  181. struct wpa_supplicant *wpa_s = eloop_ctx;
  182. wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
  183. if (wpa_supplicant_req_sched_scan(wpa_s))
  184. wpa_supplicant_req_scan(wpa_s, 0, 0);
  185. }
  186. static void
  187. wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  188. {
  189. struct wpa_supplicant *wpa_s = eloop_ctx;
  190. wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
  191. wpa_s->sched_scan_timed_out = 1;
  192. wpa_supplicant_cancel_sched_scan(wpa_s);
  193. }
  194. static int
  195. wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
  196. struct wpa_driver_scan_params *params,
  197. int interval)
  198. {
  199. int ret;
  200. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  201. return -1;
  202. wpa_supplicant_notify_scanning(wpa_s, 1);
  203. ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
  204. if (ret)
  205. wpa_supplicant_notify_scanning(wpa_s, 0);
  206. else
  207. wpa_s->sched_scanning = 1;
  208. return ret;
  209. }
  210. static int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
  211. {
  212. int ret;
  213. ret = wpa_drv_stop_sched_scan(wpa_s);
  214. if (ret) {
  215. wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
  216. /* TODO: what to do if stopping fails? */
  217. return -1;
  218. }
  219. return ret;
  220. }
  221. static struct wpa_driver_scan_filter *
  222. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  223. {
  224. struct wpa_driver_scan_filter *ssids;
  225. struct wpa_ssid *ssid;
  226. size_t count;
  227. *num_ssids = 0;
  228. if (!conf->filter_ssids)
  229. return NULL;
  230. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  231. if (ssid->ssid && ssid->ssid_len)
  232. count++;
  233. }
  234. if (count == 0)
  235. return NULL;
  236. ssids = os_zalloc(count * sizeof(struct wpa_driver_scan_filter));
  237. if (ssids == NULL)
  238. return NULL;
  239. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  240. if (!ssid->ssid || !ssid->ssid_len)
  241. continue;
  242. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  243. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  244. (*num_ssids)++;
  245. }
  246. return ssids;
  247. }
  248. static void wpa_supplicant_optimize_freqs(
  249. struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
  250. {
  251. #ifdef CONFIG_P2P
  252. if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
  253. wpa_s->go_params) {
  254. /* Optimize provisioning state scan based on GO information */
  255. if (wpa_s->p2p_in_provisioning < 5 &&
  256. wpa_s->go_params->freq > 0) {
  257. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
  258. "preferred frequency %d MHz",
  259. wpa_s->go_params->freq);
  260. params->freqs = os_zalloc(2 * sizeof(int));
  261. if (params->freqs)
  262. params->freqs[0] = wpa_s->go_params->freq;
  263. } else if (wpa_s->p2p_in_provisioning < 8 &&
  264. wpa_s->go_params->freq_list[0]) {
  265. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
  266. "channels");
  267. int_array_concat(&params->freqs,
  268. wpa_s->go_params->freq_list);
  269. if (params->freqs)
  270. int_array_sort_unique(params->freqs);
  271. }
  272. wpa_s->p2p_in_provisioning++;
  273. }
  274. #endif /* CONFIG_P2P */
  275. #ifdef CONFIG_WPS
  276. if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  277. /*
  278. * Optimize post-provisioning scan based on channel used
  279. * during provisioning.
  280. */
  281. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
  282. "that was used during provisioning", wpa_s->wps_freq);
  283. params->freqs = os_zalloc(2 * sizeof(int));
  284. if (params->freqs)
  285. params->freqs[0] = wpa_s->wps_freq;
  286. wpa_s->after_wps--;
  287. }
  288. #endif /* CONFIG_WPS */
  289. }
  290. #ifdef CONFIG_INTERWORKING
  291. static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
  292. struct wpabuf *buf)
  293. {
  294. if (wpa_s->conf->interworking == 0)
  295. return;
  296. wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
  297. wpabuf_put_u8(buf, 4);
  298. wpabuf_put_u8(buf, 0x00);
  299. wpabuf_put_u8(buf, 0x00);
  300. wpabuf_put_u8(buf, 0x00);
  301. wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
  302. wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
  303. wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
  304. 1 + ETH_ALEN);
  305. wpabuf_put_u8(buf, INTERWORKING_ANT_WILDCARD);
  306. /* No Venue Info */
  307. if (!is_zero_ether_addr(wpa_s->conf->hessid))
  308. wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
  309. }
  310. #endif /* CONFIG_INTERWORKING */
  311. static struct wpabuf *
  312. wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s,
  313. struct wpa_driver_scan_params *params)
  314. {
  315. struct wpabuf *extra_ie = NULL;
  316. #ifdef CONFIG_WPS
  317. int wps = 0;
  318. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  319. #endif /* CONFIG_WPS */
  320. #ifdef CONFIG_INTERWORKING
  321. if (wpa_s->conf->interworking &&
  322. wpabuf_resize(&extra_ie, 100) == 0)
  323. wpas_add_interworking_elements(wpa_s, extra_ie);
  324. #endif /* CONFIG_INTERWORKING */
  325. #ifdef CONFIG_WPS
  326. wps = wpas_wps_in_use(wpa_s, &req_type);
  327. if (wps) {
  328. struct wpabuf *wps_ie;
  329. wps_ie = wps_build_probe_req_ie(wps == 2, &wpa_s->wps->dev,
  330. wpa_s->wps->uuid, req_type,
  331. 0, NULL);
  332. if (wps_ie) {
  333. if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
  334. wpabuf_put_buf(extra_ie, wps_ie);
  335. wpabuf_free(wps_ie);
  336. }
  337. }
  338. #ifdef CONFIG_P2P
  339. if (wps) {
  340. size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  341. if (wpabuf_resize(&extra_ie, ielen) == 0)
  342. wpas_p2p_scan_ie(wpa_s, extra_ie);
  343. }
  344. #endif /* CONFIG_P2P */
  345. #endif /* CONFIG_WPS */
  346. return extra_ie;
  347. }
  348. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  349. {
  350. struct wpa_supplicant *wpa_s = eloop_ctx;
  351. struct wpa_ssid *ssid;
  352. int scan_req = 0, ret;
  353. struct wpabuf *extra_ie;
  354. struct wpa_driver_scan_params params;
  355. size_t max_ssids;
  356. enum wpa_states prev_state;
  357. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  358. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  359. return;
  360. }
  361. if (wpa_s->disconnected && !wpa_s->scan_req) {
  362. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  363. return;
  364. }
  365. if (!wpa_supplicant_enabled_networks(wpa_s->conf) &&
  366. !wpa_s->scan_req) {
  367. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  368. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  369. return;
  370. }
  371. if (wpa_s->conf->ap_scan != 0 &&
  372. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  373. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  374. "overriding ap_scan configuration");
  375. wpa_s->conf->ap_scan = 0;
  376. wpas_notify_ap_scan_changed(wpa_s);
  377. }
  378. if (wpa_s->conf->ap_scan == 0) {
  379. wpa_supplicant_gen_assoc_event(wpa_s);
  380. return;
  381. }
  382. #ifdef CONFIG_P2P
  383. if (wpas_p2p_in_progress(wpa_s)) {
  384. if (wpa_s->wpa_state == WPA_SCANNING) {
  385. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan "
  386. "while P2P operation is in progress");
  387. wpa_supplicant_req_scan(wpa_s, 5, 0);
  388. } else {
  389. wpa_dbg(wpa_s, MSG_DEBUG, "Do not request scan while "
  390. "P2P operation is in progress");
  391. }
  392. return;
  393. }
  394. #endif /* CONFIG_P2P */
  395. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME) ||
  396. wpa_s->conf->ap_scan == 2)
  397. max_ssids = 1;
  398. else {
  399. max_ssids = wpa_s->max_scan_ssids;
  400. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  401. max_ssids = WPAS_MAX_SCAN_SSIDS;
  402. }
  403. scan_req = wpa_s->scan_req;
  404. wpa_s->scan_req = 0;
  405. os_memset(&params, 0, sizeof(params));
  406. prev_state = wpa_s->wpa_state;
  407. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  408. wpa_s->wpa_state == WPA_INACTIVE)
  409. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  410. if (scan_req != 2 && wpa_s->connect_without_scan) {
  411. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  412. if (ssid == wpa_s->connect_without_scan)
  413. break;
  414. }
  415. wpa_s->connect_without_scan = NULL;
  416. if (ssid) {
  417. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  418. "without scan step");
  419. wpa_supplicant_associate(wpa_s, NULL, ssid);
  420. return;
  421. }
  422. }
  423. /* Find the starting point from which to continue scanning */
  424. ssid = wpa_s->conf->ssid;
  425. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  426. while (ssid) {
  427. if (ssid == wpa_s->prev_scan_ssid) {
  428. ssid = ssid->next;
  429. break;
  430. }
  431. ssid = ssid->next;
  432. }
  433. }
  434. if (scan_req != 2 && wpa_s->conf->ap_scan == 2) {
  435. wpa_s->connect_without_scan = NULL;
  436. wpa_supplicant_assoc_try(wpa_s, ssid);
  437. return;
  438. } else if (wpa_s->conf->ap_scan == 2) {
  439. /*
  440. * User-initiated scan request in ap_scan == 2; scan with
  441. * wildcard SSID.
  442. */
  443. ssid = NULL;
  444. } else {
  445. struct wpa_ssid *start = ssid, *tssid;
  446. int freqs_set = 0;
  447. if (ssid == NULL && max_ssids > 1)
  448. ssid = wpa_s->conf->ssid;
  449. while (ssid) {
  450. if (!ssid->disabled && ssid->scan_ssid) {
  451. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  452. ssid->ssid, ssid->ssid_len);
  453. params.ssids[params.num_ssids].ssid =
  454. ssid->ssid;
  455. params.ssids[params.num_ssids].ssid_len =
  456. ssid->ssid_len;
  457. params.num_ssids++;
  458. if (params.num_ssids + 1 >= max_ssids)
  459. break;
  460. }
  461. ssid = ssid->next;
  462. if (ssid == start)
  463. break;
  464. if (ssid == NULL && max_ssids > 1 &&
  465. start != wpa_s->conf->ssid)
  466. ssid = wpa_s->conf->ssid;
  467. }
  468. for (tssid = wpa_s->conf->ssid; tssid; tssid = tssid->next) {
  469. if (tssid->disabled)
  470. continue;
  471. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  472. int_array_concat(&params.freqs,
  473. tssid->scan_freq);
  474. } else {
  475. os_free(params.freqs);
  476. params.freqs = NULL;
  477. }
  478. freqs_set = 1;
  479. }
  480. int_array_sort_unique(params.freqs);
  481. }
  482. if (ssid) {
  483. wpa_s->prev_scan_ssid = ssid;
  484. if (max_ssids > 1) {
  485. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  486. "the scan request");
  487. params.num_ssids++;
  488. }
  489. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for specific "
  490. "SSID(s)");
  491. } else {
  492. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  493. params.num_ssids++;
  494. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  495. "SSID");
  496. }
  497. wpa_supplicant_optimize_freqs(wpa_s, &params);
  498. extra_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  499. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  500. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  501. "generated frequency list");
  502. params.freqs = wpa_s->next_scan_freqs;
  503. } else
  504. os_free(wpa_s->next_scan_freqs);
  505. wpa_s->next_scan_freqs = NULL;
  506. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  507. wpa_s->conf, &params.num_filter_ssids);
  508. if (extra_ie) {
  509. params.extra_ies = wpabuf_head(extra_ie);
  510. params.extra_ies_len = wpabuf_len(extra_ie);
  511. }
  512. ret = wpa_supplicant_trigger_scan(wpa_s, &params);
  513. wpabuf_free(extra_ie);
  514. os_free(params.freqs);
  515. os_free(params.filter_ssids);
  516. if (ret) {
  517. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  518. if (prev_state != wpa_s->wpa_state)
  519. wpa_supplicant_set_state(wpa_s, prev_state);
  520. wpa_supplicant_req_scan(wpa_s, 1, 0);
  521. }
  522. }
  523. /**
  524. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  525. * @wpa_s: Pointer to wpa_supplicant data
  526. * @sec: Number of seconds after which to scan
  527. * @usec: Number of microseconds after which to scan
  528. *
  529. * This function is used to schedule a scan for neighboring access points after
  530. * the specified time.
  531. */
  532. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  533. {
  534. /* If there's at least one network that should be specifically scanned
  535. * then don't cancel the scan and reschedule. Some drivers do
  536. * background scanning which generates frequent scan results, and that
  537. * causes the specific SSID scan to get continually pushed back and
  538. * never happen, which causes hidden APs to never get probe-scanned.
  539. */
  540. if (eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL) &&
  541. wpa_s->conf->ap_scan == 1) {
  542. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  543. while (ssid) {
  544. if (!ssid->disabled && ssid->scan_ssid)
  545. break;
  546. ssid = ssid->next;
  547. }
  548. if (ssid) {
  549. wpa_dbg(wpa_s, MSG_DEBUG, "Not rescheduling scan to "
  550. "ensure that specific SSID scans occur");
  551. return;
  552. }
  553. }
  554. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d sec %d usec",
  555. sec, usec);
  556. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  557. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  558. }
  559. /**
  560. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  561. * @wpa_s: Pointer to wpa_supplicant data
  562. * @sec: Number of seconds after which to scan
  563. * @usec: Number of microseconds after which to scan
  564. *
  565. * This function is used to schedule periodic scans for neighboring
  566. * access points after the specified time.
  567. */
  568. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  569. int sec, int usec)
  570. {
  571. if (!wpa_s->sched_scan_supported)
  572. return -1;
  573. eloop_register_timeout(sec, usec,
  574. wpa_supplicant_delayed_sched_scan_timeout,
  575. wpa_s, NULL);
  576. return 0;
  577. }
  578. /**
  579. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  580. * @wpa_s: Pointer to wpa_supplicant data
  581. *
  582. * This function is used to schedule periodic scans for neighboring
  583. * access points repeating the scan continuously.
  584. */
  585. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  586. {
  587. struct wpa_driver_scan_params params;
  588. enum wpa_states prev_state;
  589. struct wpa_ssid *ssid;
  590. struct wpabuf *wps_ie = NULL;
  591. int ret;
  592. unsigned int max_sched_scan_ssids;
  593. if (!wpa_s->sched_scan_supported)
  594. return -1;
  595. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  596. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  597. else
  598. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  599. if (wpa_s->sched_scanning)
  600. return 0;
  601. os_memset(&params, 0, sizeof(params));
  602. /* If we can't allocate space for the filters, we just don't filter */
  603. params.filter_ssids = os_zalloc(wpa_s->max_match_sets *
  604. sizeof(struct wpa_driver_scan_filter));
  605. prev_state = wpa_s->wpa_state;
  606. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  607. wpa_s->wpa_state == WPA_INACTIVE)
  608. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  609. /* Find the starting point from which to continue scanning */
  610. ssid = wpa_s->conf->ssid;
  611. if (wpa_s->prev_sched_ssid) {
  612. while (ssid) {
  613. if (ssid == wpa_s->prev_sched_ssid) {
  614. ssid = ssid->next;
  615. break;
  616. }
  617. ssid = ssid->next;
  618. }
  619. }
  620. if (!ssid || !wpa_s->prev_sched_ssid) {
  621. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  622. wpa_s->sched_scan_interval = 2;
  623. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  624. wpa_s->first_sched_scan = 1;
  625. ssid = wpa_s->conf->ssid;
  626. wpa_s->prev_sched_ssid = ssid;
  627. }
  628. while (ssid) {
  629. if (ssid->disabled) {
  630. wpa_s->prev_sched_ssid = ssid;
  631. ssid = ssid->next;
  632. continue;
  633. }
  634. if (params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  635. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  636. ssid->ssid, ssid->ssid_len);
  637. params.filter_ssids[params.num_filter_ssids].ssid_len =
  638. ssid->ssid_len;
  639. params.num_filter_ssids++;
  640. }
  641. if (ssid->scan_ssid) {
  642. params.ssids[params.num_ssids].ssid =
  643. ssid->ssid;
  644. params.ssids[params.num_ssids].ssid_len =
  645. ssid->ssid_len;
  646. params.num_ssids++;
  647. if (params.num_ssids >= max_sched_scan_ssids) {
  648. wpa_s->prev_sched_ssid = ssid;
  649. break;
  650. }
  651. }
  652. if (params.num_filter_ssids >= wpa_s->max_match_sets)
  653. break;
  654. wpa_s->prev_sched_ssid = ssid;
  655. ssid = ssid->next;
  656. }
  657. if (!params.num_ssids) {
  658. os_free(params.filter_ssids);
  659. return 0;
  660. }
  661. if (wpa_s->wps)
  662. wps_ie = wpa_supplicant_extra_ies(wpa_s, &params);
  663. wpa_dbg(wpa_s, MSG_DEBUG,
  664. "Starting sched scan: interval %d timeout %d",
  665. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  666. ret = wpa_supplicant_start_sched_scan(wpa_s, &params,
  667. wpa_s->sched_scan_interval);
  668. wpabuf_free(wps_ie);
  669. os_free(params.filter_ssids);
  670. if (ret) {
  671. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  672. if (prev_state != wpa_s->wpa_state)
  673. wpa_supplicant_set_state(wpa_s, prev_state);
  674. return ret;
  675. }
  676. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  677. if (ssid || !wpa_s->first_sched_scan) {
  678. wpa_s->sched_scan_timed_out = 0;
  679. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  680. wpa_supplicant_sched_scan_timeout,
  681. wpa_s, NULL);
  682. wpa_s->first_sched_scan = 0;
  683. wpa_s->sched_scan_timeout /= 2;
  684. wpa_s->sched_scan_interval *= 2;
  685. }
  686. return 0;
  687. }
  688. /**
  689. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  690. * @wpa_s: Pointer to wpa_supplicant data
  691. *
  692. * This function is used to cancel a scan request scheduled with
  693. * wpa_supplicant_req_scan().
  694. */
  695. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  696. {
  697. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  698. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  699. }
  700. /**
  701. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  702. * @wpa_s: Pointer to wpa_supplicant data
  703. *
  704. * This function is used to stop a periodic scheduled scan.
  705. */
  706. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  707. {
  708. if (!wpa_s->sched_scanning)
  709. return;
  710. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  711. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  712. wpa_supplicant_stop_sched_scan(wpa_s);
  713. }
  714. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  715. int scanning)
  716. {
  717. if (wpa_s->scanning != scanning) {
  718. wpa_s->scanning = scanning;
  719. wpas_notify_scanning(wpa_s);
  720. }
  721. }
  722. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  723. {
  724. int rate = 0;
  725. const u8 *ie;
  726. int i;
  727. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  728. for (i = 0; ie && i < ie[1]; i++) {
  729. if ((ie[i + 2] & 0x7f) > rate)
  730. rate = ie[i + 2] & 0x7f;
  731. }
  732. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  733. for (i = 0; ie && i < ie[1]; i++) {
  734. if ((ie[i + 2] & 0x7f) > rate)
  735. rate = ie[i + 2] & 0x7f;
  736. }
  737. return rate;
  738. }
  739. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  740. {
  741. const u8 *end, *pos;
  742. pos = (const u8 *) (res + 1);
  743. end = pos + res->ie_len;
  744. while (pos + 1 < end) {
  745. if (pos + 2 + pos[1] > end)
  746. break;
  747. if (pos[0] == ie)
  748. return pos;
  749. pos += 2 + pos[1];
  750. }
  751. return NULL;
  752. }
  753. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  754. u32 vendor_type)
  755. {
  756. const u8 *end, *pos;
  757. pos = (const u8 *) (res + 1);
  758. end = pos + res->ie_len;
  759. while (pos + 1 < end) {
  760. if (pos + 2 + pos[1] > end)
  761. break;
  762. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  763. vendor_type == WPA_GET_BE32(&pos[2]))
  764. return pos;
  765. pos += 2 + pos[1];
  766. }
  767. return NULL;
  768. }
  769. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  770. u32 vendor_type)
  771. {
  772. struct wpabuf *buf;
  773. const u8 *end, *pos;
  774. buf = wpabuf_alloc(res->ie_len);
  775. if (buf == NULL)
  776. return NULL;
  777. pos = (const u8 *) (res + 1);
  778. end = pos + res->ie_len;
  779. while (pos + 1 < end) {
  780. if (pos + 2 + pos[1] > end)
  781. break;
  782. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  783. vendor_type == WPA_GET_BE32(&pos[2]))
  784. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  785. pos += 2 + pos[1];
  786. }
  787. if (wpabuf_len(buf) == 0) {
  788. wpabuf_free(buf);
  789. buf = NULL;
  790. }
  791. return buf;
  792. }
  793. struct wpabuf * wpa_scan_get_vendor_ie_multi_beacon(
  794. const struct wpa_scan_res *res, u32 vendor_type)
  795. {
  796. struct wpabuf *buf;
  797. const u8 *end, *pos;
  798. if (res->beacon_ie_len == 0)
  799. return NULL;
  800. buf = wpabuf_alloc(res->beacon_ie_len);
  801. if (buf == NULL)
  802. return NULL;
  803. pos = (const u8 *) (res + 1);
  804. pos += res->ie_len;
  805. end = pos + res->beacon_ie_len;
  806. while (pos + 1 < end) {
  807. if (pos + 2 + pos[1] > end)
  808. break;
  809. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  810. vendor_type == WPA_GET_BE32(&pos[2]))
  811. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  812. pos += 2 + pos[1];
  813. }
  814. if (wpabuf_len(buf) == 0) {
  815. wpabuf_free(buf);
  816. buf = NULL;
  817. }
  818. return buf;
  819. }
  820. /* Compare function for sorting scan results. Return >0 if @b is considered
  821. * better. */
  822. static int wpa_scan_result_compar(const void *a, const void *b)
  823. {
  824. struct wpa_scan_res **_wa = (void *) a;
  825. struct wpa_scan_res **_wb = (void *) b;
  826. struct wpa_scan_res *wa = *_wa;
  827. struct wpa_scan_res *wb = *_wb;
  828. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  829. /* WPA/WPA2 support preferred */
  830. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  831. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  832. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  833. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  834. if (wpa_b && !wpa_a)
  835. return 1;
  836. if (!wpa_b && wpa_a)
  837. return -1;
  838. /* privacy support preferred */
  839. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  840. (wb->caps & IEEE80211_CAP_PRIVACY))
  841. return 1;
  842. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  843. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  844. return -1;
  845. /* best/max rate preferred if signal level close enough XXX */
  846. if ((wa->level && wb->level && abs(wb->level - wa->level) < 5) ||
  847. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  848. maxrate_a = wpa_scan_get_max_rate(wa);
  849. maxrate_b = wpa_scan_get_max_rate(wb);
  850. if (maxrate_a != maxrate_b)
  851. return maxrate_b - maxrate_a;
  852. }
  853. /* use freq for channel preference */
  854. /* all things being equal, use signal level; if signal levels are
  855. * identical, use quality values since some drivers may only report
  856. * that value and leave the signal level zero */
  857. if (wb->level == wa->level)
  858. return wb->qual - wa->qual;
  859. return wb->level - wa->level;
  860. }
  861. #ifdef CONFIG_WPS
  862. /* Compare function for sorting scan results when searching a WPS AP for
  863. * provisioning. Return >0 if @b is considered better. */
  864. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  865. {
  866. struct wpa_scan_res **_wa = (void *) a;
  867. struct wpa_scan_res **_wb = (void *) b;
  868. struct wpa_scan_res *wa = *_wa;
  869. struct wpa_scan_res *wb = *_wb;
  870. int uses_wps_a, uses_wps_b;
  871. struct wpabuf *wps_a, *wps_b;
  872. int res;
  873. /* Optimization - check WPS IE existence before allocated memory and
  874. * doing full reassembly. */
  875. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  876. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  877. if (uses_wps_a && !uses_wps_b)
  878. return -1;
  879. if (!uses_wps_a && uses_wps_b)
  880. return 1;
  881. if (uses_wps_a && uses_wps_b) {
  882. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  883. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  884. res = wps_ap_priority_compar(wps_a, wps_b);
  885. wpabuf_free(wps_a);
  886. wpabuf_free(wps_b);
  887. if (res)
  888. return res;
  889. }
  890. /*
  891. * Do not use current AP security policy as a sorting criteria during
  892. * WPS provisioning step since the AP may get reconfigured at the
  893. * completion of provisioning.
  894. */
  895. /* all things being equal, use signal level; if signal levels are
  896. * identical, use quality values since some drivers may only report
  897. * that value and leave the signal level zero */
  898. if (wb->level == wa->level)
  899. return wb->qual - wa->qual;
  900. return wb->level - wa->level;
  901. }
  902. #endif /* CONFIG_WPS */
  903. /**
  904. * wpa_supplicant_get_scan_results - Get scan results
  905. * @wpa_s: Pointer to wpa_supplicant data
  906. * @info: Information about what was scanned or %NULL if not available
  907. * @new_scan: Whether a new scan was performed
  908. * Returns: Scan results, %NULL on failure
  909. *
  910. * This function request the current scan results from the driver and updates
  911. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  912. * results with wpa_scan_results_free().
  913. */
  914. struct wpa_scan_results *
  915. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  916. struct scan_info *info, int new_scan)
  917. {
  918. struct wpa_scan_results *scan_res;
  919. size_t i;
  920. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  921. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_USER_SPACE_MLME)
  922. scan_res = ieee80211_sta_get_scan_results(wpa_s);
  923. else
  924. scan_res = wpa_drv_get_scan_results2(wpa_s);
  925. if (scan_res == NULL) {
  926. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  927. return NULL;
  928. }
  929. #ifdef CONFIG_WPS
  930. if (wpas_wps_in_progress(wpa_s)) {
  931. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  932. "provisioning rules");
  933. compar = wpa_scan_result_wps_compar;
  934. }
  935. #endif /* CONFIG_WPS */
  936. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  937. compar);
  938. wpa_bss_update_start(wpa_s);
  939. for (i = 0; i < scan_res->num; i++)
  940. wpa_bss_update_scan_res(wpa_s, scan_res->res[i]);
  941. wpa_bss_update_end(wpa_s, info, new_scan);
  942. return scan_res;
  943. }
  944. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  945. {
  946. struct wpa_scan_results *scan_res;
  947. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  948. if (scan_res == NULL)
  949. return -1;
  950. wpa_scan_results_free(scan_res);
  951. return 0;
  952. }
  953. void wpa_scan_results_free(struct wpa_scan_results *res)
  954. {
  955. size_t i;
  956. if (res == NULL)
  957. return;
  958. for (i = 0; i < res->num; i++)
  959. os_free(res->res[i]);
  960. os_free(res->res);
  961. os_free(res);
  962. }