scan.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  1. /*
  2. * WPA Supplicant - Scanning
  3. * Copyright (c) 2003-2014, 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 "common/wpa_ctrl.h"
  13. #include "config.h"
  14. #include "wpa_supplicant_i.h"
  15. #include "driver_i.h"
  16. #include "wps_supplicant.h"
  17. #include "p2p_supplicant.h"
  18. #include "p2p/p2p.h"
  19. #include "hs20_supplicant.h"
  20. #include "notify.h"
  21. #include "bss.h"
  22. #include "scan.h"
  23. #include "mesh.h"
  24. static void wpa_supplicant_gen_assoc_event(struct wpa_supplicant *wpa_s)
  25. {
  26. struct wpa_ssid *ssid;
  27. union wpa_event_data data;
  28. ssid = wpa_supplicant_get_ssid(wpa_s);
  29. if (ssid == NULL)
  30. return;
  31. if (wpa_s->current_ssid == NULL) {
  32. wpa_s->current_ssid = ssid;
  33. if (wpa_s->current_ssid != NULL)
  34. wpas_notify_network_changed(wpa_s);
  35. }
  36. wpa_supplicant_initiate_eapol(wpa_s);
  37. wpa_dbg(wpa_s, MSG_DEBUG, "Already associated with a configured "
  38. "network - generating associated event");
  39. os_memset(&data, 0, sizeof(data));
  40. wpa_supplicant_event(wpa_s, EVENT_ASSOC, &data);
  41. }
  42. #ifdef CONFIG_WPS
  43. static int wpas_wps_in_use(struct wpa_supplicant *wpa_s,
  44. enum wps_request_type *req_type)
  45. {
  46. struct wpa_ssid *ssid;
  47. int wps = 0;
  48. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  49. if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
  50. continue;
  51. wps = 1;
  52. *req_type = wpas_wps_get_req_type(ssid);
  53. if (!ssid->eap.phase1)
  54. continue;
  55. if (os_strstr(ssid->eap.phase1, "pbc=1"))
  56. return 2;
  57. }
  58. #ifdef CONFIG_P2P
  59. if (!wpa_s->global->p2p_disabled && wpa_s->global->p2p &&
  60. !wpa_s->conf->p2p_disabled) {
  61. wpa_s->wps->dev.p2p = 1;
  62. if (!wps) {
  63. wps = 1;
  64. *req_type = WPS_REQ_ENROLLEE_INFO;
  65. }
  66. }
  67. #endif /* CONFIG_P2P */
  68. return wps;
  69. }
  70. #endif /* CONFIG_WPS */
  71. /**
  72. * wpa_supplicant_enabled_networks - Check whether there are enabled networks
  73. * @wpa_s: Pointer to wpa_supplicant data
  74. * Returns: 0 if no networks are enabled, >0 if networks are enabled
  75. *
  76. * This function is used to figure out whether any networks (or Interworking
  77. * with enabled credentials and auto_interworking) are present in the current
  78. * configuration.
  79. */
  80. int wpa_supplicant_enabled_networks(struct wpa_supplicant *wpa_s)
  81. {
  82. struct wpa_ssid *ssid = wpa_s->conf->ssid;
  83. int count = 0, disabled = 0;
  84. while (ssid) {
  85. if (!wpas_network_disabled(wpa_s, ssid))
  86. count++;
  87. else
  88. disabled++;
  89. ssid = ssid->next;
  90. }
  91. if (wpa_s->conf->cred && wpa_s->conf->interworking &&
  92. wpa_s->conf->auto_interworking)
  93. count++;
  94. if (count == 0 && disabled > 0) {
  95. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks (%d disabled "
  96. "networks)", disabled);
  97. }
  98. return count;
  99. }
  100. static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
  101. struct wpa_ssid *ssid)
  102. {
  103. while (ssid) {
  104. if (!wpas_network_disabled(wpa_s, ssid))
  105. break;
  106. ssid = ssid->next;
  107. }
  108. /* ap_scan=2 mode - try to associate with each SSID. */
  109. if (ssid == NULL) {
  110. wpa_dbg(wpa_s, MSG_DEBUG, "wpa_supplicant_assoc_try: Reached "
  111. "end of scan list - go back to beginning");
  112. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  113. wpa_supplicant_req_scan(wpa_s, 0, 0);
  114. return;
  115. }
  116. if (ssid->next) {
  117. /* Continue from the next SSID on the next attempt. */
  118. wpa_s->prev_scan_ssid = ssid;
  119. } else {
  120. /* Start from the beginning of the SSID list. */
  121. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  122. }
  123. wpa_supplicant_associate(wpa_s, NULL, ssid);
  124. }
  125. static void wpas_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
  126. {
  127. struct wpa_supplicant *wpa_s = work->wpa_s;
  128. struct wpa_driver_scan_params *params = work->ctx;
  129. int ret;
  130. if (deinit) {
  131. if (!work->started) {
  132. wpa_scan_free_params(params);
  133. return;
  134. }
  135. wpa_supplicant_notify_scanning(wpa_s, 0);
  136. wpas_notify_scan_done(wpa_s, 0);
  137. wpa_s->scan_work = NULL;
  138. return;
  139. }
  140. if (wpas_update_random_addr_disassoc(wpa_s) < 0) {
  141. wpa_msg(wpa_s, MSG_INFO,
  142. "Failed to assign random MAC address for a scan");
  143. radio_work_done(work);
  144. return;
  145. }
  146. wpa_supplicant_notify_scanning(wpa_s, 1);
  147. if (wpa_s->clear_driver_scan_cache) {
  148. wpa_printf(MSG_DEBUG,
  149. "Request driver to clear scan cache due to local BSS flush");
  150. params->only_new_results = 1;
  151. }
  152. ret = wpa_drv_scan(wpa_s, params);
  153. wpa_scan_free_params(params);
  154. work->ctx = NULL;
  155. if (ret) {
  156. int retry = wpa_s->last_scan_req != MANUAL_SCAN_REQ;
  157. if (wpa_s->disconnected)
  158. retry = 0;
  159. wpa_supplicant_notify_scanning(wpa_s, 0);
  160. wpas_notify_scan_done(wpa_s, 0);
  161. if (wpa_s->wpa_state == WPA_SCANNING)
  162. wpa_supplicant_set_state(wpa_s,
  163. wpa_s->scan_prev_wpa_state);
  164. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_SCAN_FAILED "ret=%d%s",
  165. ret, retry ? " retry=1" : "");
  166. radio_work_done(work);
  167. if (retry) {
  168. /* Restore scan_req since we will try to scan again */
  169. wpa_s->scan_req = wpa_s->last_scan_req;
  170. wpa_supplicant_req_scan(wpa_s, 1, 0);
  171. }
  172. return;
  173. }
  174. os_get_reltime(&wpa_s->scan_trigger_time);
  175. wpa_s->scan_runs++;
  176. wpa_s->normal_scans++;
  177. wpa_s->own_scan_requested = 1;
  178. wpa_s->clear_driver_scan_cache = 0;
  179. wpa_s->scan_work = work;
  180. }
  181. /**
  182. * wpa_supplicant_trigger_scan - Request driver to start a scan
  183. * @wpa_s: Pointer to wpa_supplicant data
  184. * @params: Scan parameters
  185. * Returns: 0 on success, -1 on failure
  186. */
  187. int wpa_supplicant_trigger_scan(struct wpa_supplicant *wpa_s,
  188. struct wpa_driver_scan_params *params)
  189. {
  190. struct wpa_driver_scan_params *ctx;
  191. if (wpa_s->scan_work) {
  192. wpa_dbg(wpa_s, MSG_INFO, "Reject scan trigger since one is already pending");
  193. return -1;
  194. }
  195. ctx = wpa_scan_clone_params(params);
  196. if (ctx == NULL)
  197. return -1;
  198. if (radio_add_work(wpa_s, 0, "scan", 0, wpas_trigger_scan_cb, ctx) < 0)
  199. {
  200. wpa_scan_free_params(ctx);
  201. return -1;
  202. }
  203. return 0;
  204. }
  205. static void
  206. wpa_supplicant_delayed_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  207. {
  208. struct wpa_supplicant *wpa_s = eloop_ctx;
  209. wpa_dbg(wpa_s, MSG_DEBUG, "Starting delayed sched scan");
  210. if (wpa_supplicant_req_sched_scan(wpa_s))
  211. wpa_supplicant_req_scan(wpa_s, 0, 0);
  212. }
  213. static void
  214. wpa_supplicant_sched_scan_timeout(void *eloop_ctx, void *timeout_ctx)
  215. {
  216. struct wpa_supplicant *wpa_s = eloop_ctx;
  217. wpa_dbg(wpa_s, MSG_DEBUG, "Sched scan timeout - stopping it");
  218. wpa_s->sched_scan_timed_out = 1;
  219. wpa_supplicant_cancel_sched_scan(wpa_s);
  220. }
  221. int wpa_supplicant_start_sched_scan(struct wpa_supplicant *wpa_s,
  222. struct wpa_driver_scan_params *params,
  223. int interval)
  224. {
  225. int ret;
  226. wpa_supplicant_notify_scanning(wpa_s, 1);
  227. ret = wpa_drv_sched_scan(wpa_s, params, interval * 1000);
  228. if (ret)
  229. wpa_supplicant_notify_scanning(wpa_s, 0);
  230. else
  231. wpa_s->sched_scanning = 1;
  232. return ret;
  233. }
  234. int wpa_supplicant_stop_sched_scan(struct wpa_supplicant *wpa_s)
  235. {
  236. int ret;
  237. ret = wpa_drv_stop_sched_scan(wpa_s);
  238. if (ret) {
  239. wpa_dbg(wpa_s, MSG_DEBUG, "stopping sched_scan failed!");
  240. /* TODO: what to do if stopping fails? */
  241. return -1;
  242. }
  243. return ret;
  244. }
  245. static struct wpa_driver_scan_filter *
  246. wpa_supplicant_build_filter_ssids(struct wpa_config *conf, size_t *num_ssids)
  247. {
  248. struct wpa_driver_scan_filter *ssids;
  249. struct wpa_ssid *ssid;
  250. size_t count;
  251. *num_ssids = 0;
  252. if (!conf->filter_ssids)
  253. return NULL;
  254. for (count = 0, ssid = conf->ssid; ssid; ssid = ssid->next) {
  255. if (ssid->ssid && ssid->ssid_len)
  256. count++;
  257. }
  258. if (count == 0)
  259. return NULL;
  260. ssids = os_calloc(count, sizeof(struct wpa_driver_scan_filter));
  261. if (ssids == NULL)
  262. return NULL;
  263. for (ssid = conf->ssid; ssid; ssid = ssid->next) {
  264. if (!ssid->ssid || !ssid->ssid_len)
  265. continue;
  266. os_memcpy(ssids[*num_ssids].ssid, ssid->ssid, ssid->ssid_len);
  267. ssids[*num_ssids].ssid_len = ssid->ssid_len;
  268. (*num_ssids)++;
  269. }
  270. return ssids;
  271. }
  272. static void wpa_supplicant_optimize_freqs(
  273. struct wpa_supplicant *wpa_s, struct wpa_driver_scan_params *params)
  274. {
  275. #ifdef CONFIG_P2P
  276. if (params->freqs == NULL && wpa_s->p2p_in_provisioning &&
  277. wpa_s->go_params) {
  278. /* Optimize provisioning state scan based on GO information */
  279. if (wpa_s->p2p_in_provisioning < 5 &&
  280. wpa_s->go_params->freq > 0) {
  281. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO "
  282. "preferred frequency %d MHz",
  283. wpa_s->go_params->freq);
  284. params->freqs = os_calloc(2, sizeof(int));
  285. if (params->freqs)
  286. params->freqs[0] = wpa_s->go_params->freq;
  287. } else if (wpa_s->p2p_in_provisioning < 8 &&
  288. wpa_s->go_params->freq_list[0]) {
  289. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only common "
  290. "channels");
  291. int_array_concat(&params->freqs,
  292. wpa_s->go_params->freq_list);
  293. if (params->freqs)
  294. int_array_sort_unique(params->freqs);
  295. }
  296. wpa_s->p2p_in_provisioning++;
  297. }
  298. if (params->freqs == NULL && wpa_s->p2p_in_invitation) {
  299. /*
  300. * Optimize scan based on GO information during persistent
  301. * group reinvocation
  302. */
  303. if (wpa_s->p2p_in_invitation < 5 &&
  304. wpa_s->p2p_invite_go_freq > 0) {
  305. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Scan only GO preferred frequency %d MHz during invitation",
  306. wpa_s->p2p_invite_go_freq);
  307. params->freqs = os_calloc(2, sizeof(int));
  308. if (params->freqs)
  309. params->freqs[0] = wpa_s->p2p_invite_go_freq;
  310. }
  311. wpa_s->p2p_in_invitation++;
  312. if (wpa_s->p2p_in_invitation > 20) {
  313. /*
  314. * This should not really happen since the variable is
  315. * cleared on group removal, but if it does happen, make
  316. * sure we do not get stuck in special invitation scan
  317. * mode.
  318. */
  319. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Clear p2p_in_invitation");
  320. wpa_s->p2p_in_invitation = 0;
  321. }
  322. }
  323. #endif /* CONFIG_P2P */
  324. #ifdef CONFIG_WPS
  325. if (params->freqs == NULL && wpa_s->after_wps && wpa_s->wps_freq) {
  326. /*
  327. * Optimize post-provisioning scan based on channel used
  328. * during provisioning.
  329. */
  330. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz "
  331. "that was used during provisioning", wpa_s->wps_freq);
  332. params->freqs = os_calloc(2, sizeof(int));
  333. if (params->freqs)
  334. params->freqs[0] = wpa_s->wps_freq;
  335. wpa_s->after_wps--;
  336. } else if (wpa_s->after_wps)
  337. wpa_s->after_wps--;
  338. if (params->freqs == NULL && wpa_s->known_wps_freq && wpa_s->wps_freq)
  339. {
  340. /* Optimize provisioning scan based on already known channel */
  341. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Scan only frequency %u MHz",
  342. wpa_s->wps_freq);
  343. params->freqs = os_calloc(2, sizeof(int));
  344. if (params->freqs)
  345. params->freqs[0] = wpa_s->wps_freq;
  346. wpa_s->known_wps_freq = 0; /* only do this once */
  347. }
  348. #endif /* CONFIG_WPS */
  349. }
  350. #ifdef CONFIG_INTERWORKING
  351. static void wpas_add_interworking_elements(struct wpa_supplicant *wpa_s,
  352. struct wpabuf *buf)
  353. {
  354. if (wpa_s->conf->interworking == 0)
  355. return;
  356. wpabuf_put_u8(buf, WLAN_EID_EXT_CAPAB);
  357. wpabuf_put_u8(buf, 6);
  358. wpabuf_put_u8(buf, 0x00);
  359. wpabuf_put_u8(buf, 0x00);
  360. wpabuf_put_u8(buf, 0x00);
  361. wpabuf_put_u8(buf, 0x80); /* Bit 31 - Interworking */
  362. wpabuf_put_u8(buf, 0x00);
  363. #ifdef CONFIG_HS20
  364. wpabuf_put_u8(buf, 0x40); /* Bit 46 - WNM-Notification */
  365. #else /* CONFIG_HS20 */
  366. wpabuf_put_u8(buf, 0x00);
  367. #endif /* CONFIG_HS20 */
  368. wpabuf_put_u8(buf, WLAN_EID_INTERWORKING);
  369. wpabuf_put_u8(buf, is_zero_ether_addr(wpa_s->conf->hessid) ? 1 :
  370. 1 + ETH_ALEN);
  371. wpabuf_put_u8(buf, wpa_s->conf->access_network_type);
  372. /* No Venue Info */
  373. if (!is_zero_ether_addr(wpa_s->conf->hessid))
  374. wpabuf_put_data(buf, wpa_s->conf->hessid, ETH_ALEN);
  375. }
  376. #endif /* CONFIG_INTERWORKING */
  377. static struct wpabuf * wpa_supplicant_extra_ies(struct wpa_supplicant *wpa_s)
  378. {
  379. struct wpabuf *extra_ie = NULL;
  380. #ifdef CONFIG_WPS
  381. int wps = 0;
  382. enum wps_request_type req_type = WPS_REQ_ENROLLEE_INFO;
  383. #endif /* CONFIG_WPS */
  384. #ifdef CONFIG_INTERWORKING
  385. if (wpa_s->conf->interworking &&
  386. wpabuf_resize(&extra_ie, 100) == 0)
  387. wpas_add_interworking_elements(wpa_s, extra_ie);
  388. #endif /* CONFIG_INTERWORKING */
  389. #ifdef CONFIG_WPS
  390. wps = wpas_wps_in_use(wpa_s, &req_type);
  391. if (wps) {
  392. struct wpabuf *wps_ie;
  393. wps_ie = wps_build_probe_req_ie(wps == 2 ? DEV_PW_PUSHBUTTON :
  394. DEV_PW_DEFAULT,
  395. &wpa_s->wps->dev,
  396. wpa_s->wps->uuid, req_type,
  397. 0, NULL);
  398. if (wps_ie) {
  399. if (wpabuf_resize(&extra_ie, wpabuf_len(wps_ie)) == 0)
  400. wpabuf_put_buf(extra_ie, wps_ie);
  401. wpabuf_free(wps_ie);
  402. }
  403. }
  404. #ifdef CONFIG_P2P
  405. if (wps) {
  406. size_t ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
  407. if (wpabuf_resize(&extra_ie, ielen) == 0)
  408. wpas_p2p_scan_ie(wpa_s, extra_ie);
  409. }
  410. #endif /* CONFIG_P2P */
  411. wpa_supplicant_mesh_add_scan_ie(wpa_s, &extra_ie);
  412. #endif /* CONFIG_WPS */
  413. #ifdef CONFIG_HS20
  414. if (wpa_s->conf->hs20 && wpabuf_resize(&extra_ie, 7) == 0)
  415. wpas_hs20_add_indication(extra_ie, -1);
  416. #endif /* CONFIG_HS20 */
  417. return extra_ie;
  418. }
  419. #ifdef CONFIG_P2P
  420. /*
  421. * Check whether there are any enabled networks or credentials that could be
  422. * used for a non-P2P connection.
  423. */
  424. static int non_p2p_network_enabled(struct wpa_supplicant *wpa_s)
  425. {
  426. struct wpa_ssid *ssid;
  427. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  428. if (wpas_network_disabled(wpa_s, ssid))
  429. continue;
  430. if (!ssid->p2p_group)
  431. return 1;
  432. }
  433. if (wpa_s->conf->cred && wpa_s->conf->interworking &&
  434. wpa_s->conf->auto_interworking)
  435. return 1;
  436. return 0;
  437. }
  438. #endif /* CONFIG_P2P */
  439. static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  440. u16 num_modes,
  441. enum hostapd_hw_mode mode)
  442. {
  443. u16 i;
  444. for (i = 0; i < num_modes; i++) {
  445. if (modes[i].mode == mode)
  446. return &modes[i];
  447. }
  448. return NULL;
  449. }
  450. static void wpa_setband_scan_freqs_list(struct wpa_supplicant *wpa_s,
  451. enum hostapd_hw_mode band,
  452. struct wpa_driver_scan_params *params)
  453. {
  454. /* Include only supported channels for the specified band */
  455. struct hostapd_hw_modes *mode;
  456. int count, i;
  457. mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, band);
  458. if (mode == NULL) {
  459. /* No channels supported in this band - use empty list */
  460. params->freqs = os_zalloc(sizeof(int));
  461. return;
  462. }
  463. params->freqs = os_calloc(mode->num_channels + 1, sizeof(int));
  464. if (params->freqs == NULL)
  465. return;
  466. for (count = 0, i = 0; i < mode->num_channels; i++) {
  467. if (mode->channels[i].flag & HOSTAPD_CHAN_DISABLED)
  468. continue;
  469. params->freqs[count++] = mode->channels[i].freq;
  470. }
  471. }
  472. static void wpa_setband_scan_freqs(struct wpa_supplicant *wpa_s,
  473. struct wpa_driver_scan_params *params)
  474. {
  475. if (wpa_s->hw.modes == NULL)
  476. return; /* unknown what channels the driver supports */
  477. if (params->freqs)
  478. return; /* already using a limited channel set */
  479. if (wpa_s->setband == WPA_SETBAND_5G)
  480. wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211A,
  481. params);
  482. else if (wpa_s->setband == WPA_SETBAND_2G)
  483. wpa_setband_scan_freqs_list(wpa_s, HOSTAPD_MODE_IEEE80211G,
  484. params);
  485. }
  486. static void wpa_set_scan_ssids(struct wpa_supplicant *wpa_s,
  487. struct wpa_driver_scan_params *params,
  488. size_t max_ssids)
  489. {
  490. unsigned int i;
  491. struct wpa_ssid *ssid;
  492. for (i = 0; i < wpa_s->scan_id_count; i++) {
  493. unsigned int j;
  494. ssid = wpa_config_get_network(wpa_s->conf, wpa_s->scan_id[i]);
  495. if (!ssid || !ssid->scan_ssid)
  496. continue;
  497. for (j = 0; j < params->num_ssids; j++) {
  498. if (params->ssids[j].ssid_len == ssid->ssid_len &&
  499. params->ssids[j].ssid &&
  500. os_memcmp(params->ssids[j].ssid, ssid->ssid,
  501. ssid->ssid_len) == 0)
  502. break;
  503. }
  504. if (j < params->num_ssids)
  505. continue; /* already in the list */
  506. if (params->num_ssids + 1 > max_ssids) {
  507. wpa_printf(MSG_DEBUG,
  508. "Over max scan SSIDs for manual request");
  509. break;
  510. }
  511. wpa_printf(MSG_DEBUG, "Scan SSID (manual request): %s",
  512. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  513. params->ssids[params->num_ssids].ssid = ssid->ssid;
  514. params->ssids[params->num_ssids].ssid_len = ssid->ssid_len;
  515. params->num_ssids++;
  516. }
  517. wpa_s->scan_id_count = 0;
  518. }
  519. static void wpa_supplicant_scan(void *eloop_ctx, void *timeout_ctx)
  520. {
  521. struct wpa_supplicant *wpa_s = eloop_ctx;
  522. struct wpa_ssid *ssid;
  523. int ret, p2p_in_prog;
  524. struct wpabuf *extra_ie = NULL;
  525. struct wpa_driver_scan_params params;
  526. struct wpa_driver_scan_params *scan_params;
  527. size_t max_ssids;
  528. if (wpa_s->pno || wpa_s->pno_sched_pending) {
  529. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - PNO is in progress");
  530. return;
  531. }
  532. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  533. wpa_dbg(wpa_s, MSG_DEBUG, "Skip scan - interface disabled");
  534. return;
  535. }
  536. if (wpa_s->disconnected && wpa_s->scan_req == NORMAL_SCAN_REQ) {
  537. wpa_dbg(wpa_s, MSG_DEBUG, "Disconnected - do not scan");
  538. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  539. return;
  540. }
  541. if (wpa_s->scanning) {
  542. /*
  543. * If we are already in scanning state, we shall reschedule the
  544. * the incoming scan request.
  545. */
  546. wpa_dbg(wpa_s, MSG_DEBUG, "Already scanning - Reschedule the incoming scan req");
  547. wpa_supplicant_req_scan(wpa_s, 1, 0);
  548. return;
  549. }
  550. if (!wpa_supplicant_enabled_networks(wpa_s) &&
  551. wpa_s->scan_req == NORMAL_SCAN_REQ) {
  552. wpa_dbg(wpa_s, MSG_DEBUG, "No enabled networks - do not scan");
  553. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  554. return;
  555. }
  556. if (wpa_s->conf->ap_scan != 0 &&
  557. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)) {
  558. wpa_dbg(wpa_s, MSG_DEBUG, "Using wired authentication - "
  559. "overriding ap_scan configuration");
  560. wpa_s->conf->ap_scan = 0;
  561. wpas_notify_ap_scan_changed(wpa_s);
  562. }
  563. if (wpa_s->conf->ap_scan == 0) {
  564. wpa_supplicant_gen_assoc_event(wpa_s);
  565. return;
  566. }
  567. p2p_in_prog = wpas_p2p_in_progress(wpa_s);
  568. if (p2p_in_prog && p2p_in_prog != 2) {
  569. wpa_dbg(wpa_s, MSG_DEBUG, "Delay station mode scan while P2P operation is in progress");
  570. wpa_supplicant_req_scan(wpa_s, 5, 0);
  571. return;
  572. }
  573. if (wpa_s->conf->ap_scan == 2)
  574. max_ssids = 1;
  575. else {
  576. max_ssids = wpa_s->max_scan_ssids;
  577. if (max_ssids > WPAS_MAX_SCAN_SSIDS)
  578. max_ssids = WPAS_MAX_SCAN_SSIDS;
  579. }
  580. wpa_s->last_scan_req = wpa_s->scan_req;
  581. wpa_s->scan_req = NORMAL_SCAN_REQ;
  582. os_memset(&params, 0, sizeof(params));
  583. wpa_s->scan_prev_wpa_state = wpa_s->wpa_state;
  584. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  585. wpa_s->wpa_state == WPA_INACTIVE)
  586. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  587. /*
  588. * If autoscan has set its own scanning parameters
  589. */
  590. if (wpa_s->autoscan_params != NULL) {
  591. scan_params = wpa_s->autoscan_params;
  592. goto scan;
  593. }
  594. if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
  595. wpa_s->connect_without_scan) {
  596. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  597. if (ssid == wpa_s->connect_without_scan)
  598. break;
  599. }
  600. wpa_s->connect_without_scan = NULL;
  601. if (ssid) {
  602. wpa_printf(MSG_DEBUG, "Start a pre-selected network "
  603. "without scan step");
  604. wpa_supplicant_associate(wpa_s, NULL, ssid);
  605. return;
  606. }
  607. }
  608. #ifdef CONFIG_P2P
  609. if ((wpa_s->p2p_in_provisioning || wpa_s->show_group_started) &&
  610. wpa_s->go_params && !wpa_s->conf->passive_scan) {
  611. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during P2P group formation (p2p_in_provisioning=%d show_group_started=%d)",
  612. wpa_s->p2p_in_provisioning,
  613. wpa_s->show_group_started);
  614. params.ssids[0].ssid = wpa_s->go_params->ssid;
  615. params.ssids[0].ssid_len = wpa_s->go_params->ssid_len;
  616. params.num_ssids = 1;
  617. goto ssid_list_set;
  618. }
  619. if (wpa_s->p2p_in_invitation) {
  620. if (wpa_s->current_ssid) {
  621. wpa_printf(MSG_DEBUG, "P2P: Use specific SSID for scan during invitation");
  622. params.ssids[0].ssid = wpa_s->current_ssid->ssid;
  623. params.ssids[0].ssid_len =
  624. wpa_s->current_ssid->ssid_len;
  625. params.num_ssids = 1;
  626. } else {
  627. wpa_printf(MSG_DEBUG, "P2P: No specific SSID known for scan during invitation");
  628. }
  629. goto ssid_list_set;
  630. }
  631. #endif /* CONFIG_P2P */
  632. /* Find the starting point from which to continue scanning */
  633. ssid = wpa_s->conf->ssid;
  634. if (wpa_s->prev_scan_ssid != WILDCARD_SSID_SCAN) {
  635. while (ssid) {
  636. if (ssid == wpa_s->prev_scan_ssid) {
  637. ssid = ssid->next;
  638. break;
  639. }
  640. ssid = ssid->next;
  641. }
  642. }
  643. if (wpa_s->last_scan_req != MANUAL_SCAN_REQ &&
  644. wpa_s->conf->ap_scan == 2) {
  645. wpa_s->connect_without_scan = NULL;
  646. wpa_s->prev_scan_wildcard = 0;
  647. wpa_supplicant_assoc_try(wpa_s, ssid);
  648. return;
  649. } else if (wpa_s->conf->ap_scan == 2) {
  650. /*
  651. * User-initiated scan request in ap_scan == 2; scan with
  652. * wildcard SSID.
  653. */
  654. ssid = NULL;
  655. } else if (wpa_s->reattach && wpa_s->current_ssid != NULL) {
  656. /*
  657. * Perform single-channel single-SSID scan for
  658. * reassociate-to-same-BSS operation.
  659. */
  660. /* Setup SSID */
  661. ssid = wpa_s->current_ssid;
  662. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  663. ssid->ssid, ssid->ssid_len);
  664. params.ssids[0].ssid = ssid->ssid;
  665. params.ssids[0].ssid_len = ssid->ssid_len;
  666. params.num_ssids = 1;
  667. /*
  668. * Allocate memory for frequency array, allocate one extra
  669. * slot for the zero-terminator.
  670. */
  671. params.freqs = os_malloc(sizeof(int) * 2);
  672. if (params.freqs == NULL) {
  673. wpa_dbg(wpa_s, MSG_ERROR, "Memory allocation failed");
  674. return;
  675. }
  676. params.freqs[0] = wpa_s->assoc_freq;
  677. params.freqs[1] = 0;
  678. /*
  679. * Reset the reattach flag so that we fall back to full scan if
  680. * this scan fails.
  681. */
  682. wpa_s->reattach = 0;
  683. } else {
  684. struct wpa_ssid *start = ssid, *tssid;
  685. int freqs_set = 0;
  686. if (ssid == NULL && max_ssids > 1)
  687. ssid = wpa_s->conf->ssid;
  688. while (ssid) {
  689. if (!wpas_network_disabled(wpa_s, ssid) &&
  690. ssid->scan_ssid) {
  691. wpa_hexdump_ascii(MSG_DEBUG, "Scan SSID",
  692. ssid->ssid, ssid->ssid_len);
  693. params.ssids[params.num_ssids].ssid =
  694. ssid->ssid;
  695. params.ssids[params.num_ssids].ssid_len =
  696. ssid->ssid_len;
  697. params.num_ssids++;
  698. if (params.num_ssids + 1 >= max_ssids)
  699. break;
  700. }
  701. ssid = ssid->next;
  702. if (ssid == start)
  703. break;
  704. if (ssid == NULL && max_ssids > 1 &&
  705. start != wpa_s->conf->ssid)
  706. ssid = wpa_s->conf->ssid;
  707. }
  708. if (wpa_s->scan_id_count &&
  709. wpa_s->last_scan_req == MANUAL_SCAN_REQ)
  710. wpa_set_scan_ssids(wpa_s, &params, max_ssids);
  711. for (tssid = wpa_s->conf->ssid;
  712. wpa_s->last_scan_req != MANUAL_SCAN_REQ && tssid;
  713. tssid = tssid->next) {
  714. if (wpas_network_disabled(wpa_s, tssid))
  715. continue;
  716. if ((params.freqs || !freqs_set) && tssid->scan_freq) {
  717. int_array_concat(&params.freqs,
  718. tssid->scan_freq);
  719. } else {
  720. os_free(params.freqs);
  721. params.freqs = NULL;
  722. }
  723. freqs_set = 1;
  724. }
  725. int_array_sort_unique(params.freqs);
  726. }
  727. if (ssid && max_ssids == 1) {
  728. /*
  729. * If the driver is limited to 1 SSID at a time interleave
  730. * wildcard SSID scans with specific SSID scans to avoid
  731. * waiting a long time for a wildcard scan.
  732. */
  733. if (!wpa_s->prev_scan_wildcard) {
  734. params.ssids[0].ssid = NULL;
  735. params.ssids[0].ssid_len = 0;
  736. wpa_s->prev_scan_wildcard = 1;
  737. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for "
  738. "wildcard SSID (Interleave with specific)");
  739. } else {
  740. wpa_s->prev_scan_ssid = ssid;
  741. wpa_s->prev_scan_wildcard = 0;
  742. wpa_dbg(wpa_s, MSG_DEBUG,
  743. "Starting AP scan for specific SSID: %s",
  744. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  745. }
  746. } else if (ssid) {
  747. /* max_ssids > 1 */
  748. wpa_s->prev_scan_ssid = ssid;
  749. wpa_dbg(wpa_s, MSG_DEBUG, "Include wildcard SSID in "
  750. "the scan request");
  751. params.num_ssids++;
  752. } else if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  753. wpa_s->manual_scan_passive && params.num_ssids == 0) {
  754. wpa_dbg(wpa_s, MSG_DEBUG, "Use passive scan based on manual request");
  755. } else if (wpa_s->conf->passive_scan) {
  756. wpa_dbg(wpa_s, MSG_DEBUG,
  757. "Use passive scan based on configuration");
  758. } else {
  759. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  760. params.num_ssids++;
  761. wpa_dbg(wpa_s, MSG_DEBUG, "Starting AP scan for wildcard "
  762. "SSID");
  763. }
  764. #ifdef CONFIG_P2P
  765. ssid_list_set:
  766. #endif /* CONFIG_P2P */
  767. wpa_supplicant_optimize_freqs(wpa_s, &params);
  768. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  769. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  770. wpa_s->manual_scan_only_new) {
  771. wpa_printf(MSG_DEBUG,
  772. "Request driver to clear scan cache due to manual only_new=1 scan");
  773. params.only_new_results = 1;
  774. }
  775. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs == NULL &&
  776. wpa_s->manual_scan_freqs) {
  777. wpa_dbg(wpa_s, MSG_DEBUG, "Limit manual scan to specified channels");
  778. params.freqs = wpa_s->manual_scan_freqs;
  779. wpa_s->manual_scan_freqs = NULL;
  780. }
  781. if (params.freqs == NULL && wpa_s->next_scan_freqs) {
  782. wpa_dbg(wpa_s, MSG_DEBUG, "Optimize scan based on previously "
  783. "generated frequency list");
  784. params.freqs = wpa_s->next_scan_freqs;
  785. } else
  786. os_free(wpa_s->next_scan_freqs);
  787. wpa_s->next_scan_freqs = NULL;
  788. wpa_setband_scan_freqs(wpa_s, &params);
  789. /* See if user specified frequencies. If so, scan only those. */
  790. if (wpa_s->conf->freq_list && !params.freqs) {
  791. wpa_dbg(wpa_s, MSG_DEBUG,
  792. "Optimize scan based on conf->freq_list");
  793. int_array_concat(&params.freqs, wpa_s->conf->freq_list);
  794. }
  795. /* Use current associated channel? */
  796. if (wpa_s->conf->scan_cur_freq && !params.freqs) {
  797. unsigned int num = wpa_s->num_multichan_concurrent;
  798. params.freqs = os_calloc(num + 1, sizeof(int));
  799. if (params.freqs) {
  800. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  801. if (num > 0) {
  802. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the "
  803. "current operating channels since "
  804. "scan_cur_freq is enabled");
  805. } else {
  806. os_free(params.freqs);
  807. params.freqs = NULL;
  808. }
  809. }
  810. }
  811. params.filter_ssids = wpa_supplicant_build_filter_ssids(
  812. wpa_s->conf, &params.num_filter_ssids);
  813. if (extra_ie) {
  814. params.extra_ies = wpabuf_head(extra_ie);
  815. params.extra_ies_len = wpabuf_len(extra_ie);
  816. }
  817. #ifdef CONFIG_P2P
  818. if (wpa_s->p2p_in_provisioning || wpa_s->p2p_in_invitation ||
  819. (wpa_s->show_group_started && wpa_s->go_params)) {
  820. /*
  821. * The interface may not yet be in P2P mode, so we have to
  822. * explicitly request P2P probe to disable CCK rates.
  823. */
  824. params.p2p_probe = 1;
  825. }
  826. #endif /* CONFIG_P2P */
  827. if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCAN) {
  828. params.mac_addr_rand = 1;
  829. if (wpa_s->mac_addr_scan) {
  830. params.mac_addr = wpa_s->mac_addr_scan;
  831. params.mac_addr_mask = wpa_s->mac_addr_scan + ETH_ALEN;
  832. }
  833. }
  834. scan_params = &params;
  835. scan:
  836. #ifdef CONFIG_P2P
  837. /*
  838. * If the driver does not support multi-channel concurrency and a
  839. * virtual interface that shares the same radio with the wpa_s interface
  840. * is operating there may not be need to scan other channels apart from
  841. * the current operating channel on the other virtual interface. Filter
  842. * out other channels in case we are trying to find a connection for a
  843. * station interface when we are not configured to prefer station
  844. * connection and a concurrent operation is already in process.
  845. */
  846. if (wpa_s->scan_for_connection &&
  847. wpa_s->last_scan_req == NORMAL_SCAN_REQ &&
  848. !scan_params->freqs && !params.freqs &&
  849. wpas_is_p2p_prioritized(wpa_s) &&
  850. wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
  851. non_p2p_network_enabled(wpa_s)) {
  852. unsigned int num = wpa_s->num_multichan_concurrent;
  853. params.freqs = os_calloc(num + 1, sizeof(int));
  854. if (params.freqs) {
  855. num = get_shared_radio_freqs(wpa_s, params.freqs, num);
  856. if (num > 0 && num == wpa_s->num_multichan_concurrent) {
  857. wpa_dbg(wpa_s, MSG_DEBUG, "Scan only the current operating channels since all channels are already used");
  858. } else {
  859. os_free(params.freqs);
  860. params.freqs = NULL;
  861. }
  862. }
  863. }
  864. #endif /* CONFIG_P2P */
  865. ret = wpa_supplicant_trigger_scan(wpa_s, scan_params);
  866. if (ret && wpa_s->last_scan_req == MANUAL_SCAN_REQ && params.freqs &&
  867. !wpa_s->manual_scan_freqs) {
  868. /* Restore manual_scan_freqs for the next attempt */
  869. wpa_s->manual_scan_freqs = params.freqs;
  870. params.freqs = NULL;
  871. }
  872. wpabuf_free(extra_ie);
  873. os_free(params.freqs);
  874. os_free(params.filter_ssids);
  875. if (ret) {
  876. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate AP scan");
  877. if (wpa_s->scan_prev_wpa_state != wpa_s->wpa_state)
  878. wpa_supplicant_set_state(wpa_s,
  879. wpa_s->scan_prev_wpa_state);
  880. /* Restore scan_req since we will try to scan again */
  881. wpa_s->scan_req = wpa_s->last_scan_req;
  882. wpa_supplicant_req_scan(wpa_s, 1, 0);
  883. } else {
  884. wpa_s->scan_for_connection = 0;
  885. #ifdef CONFIG_INTERWORKING
  886. wpa_s->interworking_fast_assoc_tried = 0;
  887. #endif /* CONFIG_INTERWORKING */
  888. }
  889. }
  890. void wpa_supplicant_update_scan_int(struct wpa_supplicant *wpa_s, int sec)
  891. {
  892. struct os_reltime remaining, new_int;
  893. int cancelled;
  894. cancelled = eloop_cancel_timeout_one(wpa_supplicant_scan, wpa_s, NULL,
  895. &remaining);
  896. new_int.sec = sec;
  897. new_int.usec = 0;
  898. if (cancelled && os_reltime_before(&remaining, &new_int)) {
  899. new_int.sec = remaining.sec;
  900. new_int.usec = remaining.usec;
  901. }
  902. if (cancelled) {
  903. eloop_register_timeout(new_int.sec, new_int.usec,
  904. wpa_supplicant_scan, wpa_s, NULL);
  905. }
  906. wpa_s->scan_interval = sec;
  907. }
  908. /**
  909. * wpa_supplicant_req_scan - Schedule a scan for neighboring access points
  910. * @wpa_s: Pointer to wpa_supplicant data
  911. * @sec: Number of seconds after which to scan
  912. * @usec: Number of microseconds after which to scan
  913. *
  914. * This function is used to schedule a scan for neighboring access points after
  915. * the specified time.
  916. */
  917. void wpa_supplicant_req_scan(struct wpa_supplicant *wpa_s, int sec, int usec)
  918. {
  919. int res = eloop_deplete_timeout(sec, usec, wpa_supplicant_scan, wpa_s,
  920. NULL);
  921. if (res == 1) {
  922. wpa_dbg(wpa_s, MSG_DEBUG, "Rescheduling scan request: %d.%06d sec",
  923. sec, usec);
  924. } else if (res == 0) {
  925. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore new scan request for %d.%06d sec since an earlier request is scheduled to trigger sooner",
  926. sec, usec);
  927. } else {
  928. wpa_dbg(wpa_s, MSG_DEBUG, "Setting scan request: %d.%06d sec",
  929. sec, usec);
  930. eloop_register_timeout(sec, usec, wpa_supplicant_scan, wpa_s, NULL);
  931. }
  932. }
  933. /**
  934. * wpa_supplicant_delayed_sched_scan - Request a delayed scheduled scan
  935. * @wpa_s: Pointer to wpa_supplicant data
  936. * @sec: Number of seconds after which to scan
  937. * @usec: Number of microseconds after which to scan
  938. * Returns: 0 on success or -1 otherwise
  939. *
  940. * This function is used to schedule periodic scans for neighboring
  941. * access points after the specified time.
  942. */
  943. int wpa_supplicant_delayed_sched_scan(struct wpa_supplicant *wpa_s,
  944. int sec, int usec)
  945. {
  946. if (!wpa_s->sched_scan_supported)
  947. return -1;
  948. eloop_register_timeout(sec, usec,
  949. wpa_supplicant_delayed_sched_scan_timeout,
  950. wpa_s, NULL);
  951. return 0;
  952. }
  953. /**
  954. * wpa_supplicant_req_sched_scan - Start a periodic scheduled scan
  955. * @wpa_s: Pointer to wpa_supplicant data
  956. * Returns: 0 is sched_scan was started or -1 otherwise
  957. *
  958. * This function is used to schedule periodic scans for neighboring
  959. * access points repeating the scan continuously.
  960. */
  961. int wpa_supplicant_req_sched_scan(struct wpa_supplicant *wpa_s)
  962. {
  963. struct wpa_driver_scan_params params;
  964. struct wpa_driver_scan_params *scan_params;
  965. enum wpa_states prev_state;
  966. struct wpa_ssid *ssid = NULL;
  967. struct wpabuf *extra_ie = NULL;
  968. int ret;
  969. unsigned int max_sched_scan_ssids;
  970. int wildcard = 0;
  971. int need_ssids;
  972. if (!wpa_s->sched_scan_supported)
  973. return -1;
  974. if (wpa_s->max_sched_scan_ssids > WPAS_MAX_SCAN_SSIDS)
  975. max_sched_scan_ssids = WPAS_MAX_SCAN_SSIDS;
  976. else
  977. max_sched_scan_ssids = wpa_s->max_sched_scan_ssids;
  978. if (max_sched_scan_ssids < 1 || wpa_s->conf->disable_scan_offload)
  979. return -1;
  980. if (wpa_s->sched_scanning) {
  981. wpa_dbg(wpa_s, MSG_DEBUG, "Already sched scanning");
  982. return 0;
  983. }
  984. need_ssids = 0;
  985. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  986. if (!wpas_network_disabled(wpa_s, ssid) && !ssid->scan_ssid) {
  987. /* Use wildcard SSID to find this network */
  988. wildcard = 1;
  989. } else if (!wpas_network_disabled(wpa_s, ssid) &&
  990. ssid->ssid_len)
  991. need_ssids++;
  992. #ifdef CONFIG_WPS
  993. if (!wpas_network_disabled(wpa_s, ssid) &&
  994. ssid->key_mgmt == WPA_KEY_MGMT_WPS) {
  995. /*
  996. * Normal scan is more reliable and faster for WPS
  997. * operations and since these are for short periods of
  998. * time, the benefit of trying to use sched_scan would
  999. * be limited.
  1000. */
  1001. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  1002. "sched_scan for WPS");
  1003. return -1;
  1004. }
  1005. #endif /* CONFIG_WPS */
  1006. }
  1007. if (wildcard)
  1008. need_ssids++;
  1009. if (wpa_s->normal_scans < 3 &&
  1010. (need_ssids <= wpa_s->max_scan_ssids ||
  1011. wpa_s->max_scan_ssids >= (int) max_sched_scan_ssids)) {
  1012. /*
  1013. * When normal scan can speed up operations, use that for the
  1014. * first operations before starting the sched_scan to allow
  1015. * user space sleep more. We do this only if the normal scan
  1016. * has functionality that is suitable for this or if the
  1017. * sched_scan does not have better support for multiple SSIDs.
  1018. */
  1019. wpa_dbg(wpa_s, MSG_DEBUG, "Use normal scan instead of "
  1020. "sched_scan for initial scans (normal_scans=%d)",
  1021. wpa_s->normal_scans);
  1022. return -1;
  1023. }
  1024. os_memset(&params, 0, sizeof(params));
  1025. /* If we can't allocate space for the filters, we just don't filter */
  1026. params.filter_ssids = os_calloc(wpa_s->max_match_sets,
  1027. sizeof(struct wpa_driver_scan_filter));
  1028. prev_state = wpa_s->wpa_state;
  1029. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  1030. wpa_s->wpa_state == WPA_INACTIVE)
  1031. wpa_supplicant_set_state(wpa_s, WPA_SCANNING);
  1032. if (wpa_s->autoscan_params != NULL) {
  1033. scan_params = wpa_s->autoscan_params;
  1034. goto scan;
  1035. }
  1036. /* Find the starting point from which to continue scanning */
  1037. ssid = wpa_s->conf->ssid;
  1038. if (wpa_s->prev_sched_ssid) {
  1039. while (ssid) {
  1040. if (ssid == wpa_s->prev_sched_ssid) {
  1041. ssid = ssid->next;
  1042. break;
  1043. }
  1044. ssid = ssid->next;
  1045. }
  1046. }
  1047. if (!ssid || !wpa_s->prev_sched_ssid) {
  1048. wpa_dbg(wpa_s, MSG_DEBUG, "Beginning of SSID list");
  1049. if (wpa_s->conf->sched_scan_interval)
  1050. wpa_s->sched_scan_interval =
  1051. wpa_s->conf->sched_scan_interval;
  1052. if (wpa_s->sched_scan_interval == 0)
  1053. wpa_s->sched_scan_interval = 10;
  1054. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  1055. wpa_s->first_sched_scan = 1;
  1056. ssid = wpa_s->conf->ssid;
  1057. wpa_s->prev_sched_ssid = ssid;
  1058. }
  1059. if (wildcard) {
  1060. wpa_dbg(wpa_s, MSG_DEBUG, "Add wildcard SSID to sched_scan");
  1061. params.num_ssids++;
  1062. }
  1063. while (ssid) {
  1064. if (wpas_network_disabled(wpa_s, ssid))
  1065. goto next;
  1066. if (params.num_filter_ssids < wpa_s->max_match_sets &&
  1067. params.filter_ssids && ssid->ssid && ssid->ssid_len) {
  1068. wpa_dbg(wpa_s, MSG_DEBUG, "add to filter ssid: %s",
  1069. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1070. os_memcpy(params.filter_ssids[params.num_filter_ssids].ssid,
  1071. ssid->ssid, ssid->ssid_len);
  1072. params.filter_ssids[params.num_filter_ssids].ssid_len =
  1073. ssid->ssid_len;
  1074. params.num_filter_ssids++;
  1075. } else if (params.filter_ssids && ssid->ssid && ssid->ssid_len)
  1076. {
  1077. wpa_dbg(wpa_s, MSG_DEBUG, "Not enough room for SSID "
  1078. "filter for sched_scan - drop filter");
  1079. os_free(params.filter_ssids);
  1080. params.filter_ssids = NULL;
  1081. params.num_filter_ssids = 0;
  1082. }
  1083. if (ssid->scan_ssid && ssid->ssid && ssid->ssid_len) {
  1084. if (params.num_ssids == max_sched_scan_ssids)
  1085. break; /* only room for broadcast SSID */
  1086. wpa_dbg(wpa_s, MSG_DEBUG,
  1087. "add to active scan ssid: %s",
  1088. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1089. params.ssids[params.num_ssids].ssid =
  1090. ssid->ssid;
  1091. params.ssids[params.num_ssids].ssid_len =
  1092. ssid->ssid_len;
  1093. params.num_ssids++;
  1094. if (params.num_ssids >= max_sched_scan_ssids) {
  1095. wpa_s->prev_sched_ssid = ssid;
  1096. do {
  1097. ssid = ssid->next;
  1098. } while (ssid &&
  1099. (wpas_network_disabled(wpa_s, ssid) ||
  1100. !ssid->scan_ssid));
  1101. break;
  1102. }
  1103. }
  1104. next:
  1105. wpa_s->prev_sched_ssid = ssid;
  1106. ssid = ssid->next;
  1107. }
  1108. if (params.num_filter_ssids == 0) {
  1109. os_free(params.filter_ssids);
  1110. params.filter_ssids = NULL;
  1111. }
  1112. extra_ie = wpa_supplicant_extra_ies(wpa_s);
  1113. if (extra_ie) {
  1114. params.extra_ies = wpabuf_head(extra_ie);
  1115. params.extra_ies_len = wpabuf_len(extra_ie);
  1116. }
  1117. if (wpa_s->conf->filter_rssi)
  1118. params.filter_rssi = wpa_s->conf->filter_rssi;
  1119. /* See if user specified frequencies. If so, scan only those. */
  1120. if (wpa_s->conf->freq_list && !params.freqs) {
  1121. wpa_dbg(wpa_s, MSG_DEBUG,
  1122. "Optimize scan based on conf->freq_list");
  1123. int_array_concat(&params.freqs, wpa_s->conf->freq_list);
  1124. }
  1125. scan_params = &params;
  1126. scan:
  1127. if (ssid || !wpa_s->first_sched_scan) {
  1128. wpa_dbg(wpa_s, MSG_DEBUG,
  1129. "Starting sched scan: interval %d timeout %d",
  1130. wpa_s->sched_scan_interval, wpa_s->sched_scan_timeout);
  1131. } else {
  1132. wpa_dbg(wpa_s, MSG_DEBUG,
  1133. "Starting sched scan: interval %d (no timeout)",
  1134. wpa_s->sched_scan_interval);
  1135. }
  1136. wpa_setband_scan_freqs(wpa_s, scan_params);
  1137. if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_SCHED_SCAN) {
  1138. params.mac_addr_rand = 1;
  1139. if (wpa_s->mac_addr_sched_scan) {
  1140. params.mac_addr = wpa_s->mac_addr_sched_scan;
  1141. params.mac_addr_mask = wpa_s->mac_addr_sched_scan +
  1142. ETH_ALEN;
  1143. }
  1144. }
  1145. ret = wpa_supplicant_start_sched_scan(wpa_s, scan_params,
  1146. wpa_s->sched_scan_interval);
  1147. wpabuf_free(extra_ie);
  1148. os_free(params.filter_ssids);
  1149. if (ret) {
  1150. wpa_msg(wpa_s, MSG_WARNING, "Failed to initiate sched scan");
  1151. if (prev_state != wpa_s->wpa_state)
  1152. wpa_supplicant_set_state(wpa_s, prev_state);
  1153. return ret;
  1154. }
  1155. /* If we have more SSIDs to scan, add a timeout so we scan them too */
  1156. if (ssid || !wpa_s->first_sched_scan) {
  1157. wpa_s->sched_scan_timed_out = 0;
  1158. eloop_register_timeout(wpa_s->sched_scan_timeout, 0,
  1159. wpa_supplicant_sched_scan_timeout,
  1160. wpa_s, NULL);
  1161. wpa_s->first_sched_scan = 0;
  1162. wpa_s->sched_scan_timeout /= 2;
  1163. wpa_s->sched_scan_interval *= 2;
  1164. if (wpa_s->sched_scan_timeout < wpa_s->sched_scan_interval) {
  1165. wpa_s->sched_scan_interval = 10;
  1166. wpa_s->sched_scan_timeout = max_sched_scan_ssids * 2;
  1167. }
  1168. }
  1169. /* If there is no more ssids, start next time from the beginning */
  1170. if (!ssid)
  1171. wpa_s->prev_sched_ssid = NULL;
  1172. return 0;
  1173. }
  1174. /**
  1175. * wpa_supplicant_cancel_scan - Cancel a scheduled scan request
  1176. * @wpa_s: Pointer to wpa_supplicant data
  1177. *
  1178. * This function is used to cancel a scan request scheduled with
  1179. * wpa_supplicant_req_scan().
  1180. */
  1181. void wpa_supplicant_cancel_scan(struct wpa_supplicant *wpa_s)
  1182. {
  1183. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling scan request");
  1184. eloop_cancel_timeout(wpa_supplicant_scan, wpa_s, NULL);
  1185. }
  1186. /**
  1187. * wpa_supplicant_cancel_delayed_sched_scan - Stop a delayed scheduled scan
  1188. * @wpa_s: Pointer to wpa_supplicant data
  1189. *
  1190. * This function is used to stop a delayed scheduled scan.
  1191. */
  1192. void wpa_supplicant_cancel_delayed_sched_scan(struct wpa_supplicant *wpa_s)
  1193. {
  1194. if (!wpa_s->sched_scan_supported)
  1195. return;
  1196. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling delayed sched scan");
  1197. eloop_cancel_timeout(wpa_supplicant_delayed_sched_scan_timeout,
  1198. wpa_s, NULL);
  1199. }
  1200. /**
  1201. * wpa_supplicant_cancel_sched_scan - Stop running scheduled scans
  1202. * @wpa_s: Pointer to wpa_supplicant data
  1203. *
  1204. * This function is used to stop a periodic scheduled scan.
  1205. */
  1206. void wpa_supplicant_cancel_sched_scan(struct wpa_supplicant *wpa_s)
  1207. {
  1208. if (!wpa_s->sched_scanning)
  1209. return;
  1210. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling sched scan");
  1211. eloop_cancel_timeout(wpa_supplicant_sched_scan_timeout, wpa_s, NULL);
  1212. wpa_supplicant_stop_sched_scan(wpa_s);
  1213. }
  1214. /**
  1215. * wpa_supplicant_notify_scanning - Indicate possible scan state change
  1216. * @wpa_s: Pointer to wpa_supplicant data
  1217. * @scanning: Whether scanning is currently in progress
  1218. *
  1219. * This function is to generate scanning notifycations. It is called whenever
  1220. * there may have been a change in scanning (scan started, completed, stopped).
  1221. * wpas_notify_scanning() is called whenever the scanning state changed from the
  1222. * previously notified state.
  1223. */
  1224. void wpa_supplicant_notify_scanning(struct wpa_supplicant *wpa_s,
  1225. int scanning)
  1226. {
  1227. if (wpa_s->scanning != scanning) {
  1228. wpa_s->scanning = scanning;
  1229. wpas_notify_scanning(wpa_s);
  1230. }
  1231. }
  1232. static int wpa_scan_get_max_rate(const struct wpa_scan_res *res)
  1233. {
  1234. int rate = 0;
  1235. const u8 *ie;
  1236. int i;
  1237. ie = wpa_scan_get_ie(res, WLAN_EID_SUPP_RATES);
  1238. for (i = 0; ie && i < ie[1]; i++) {
  1239. if ((ie[i + 2] & 0x7f) > rate)
  1240. rate = ie[i + 2] & 0x7f;
  1241. }
  1242. ie = wpa_scan_get_ie(res, WLAN_EID_EXT_SUPP_RATES);
  1243. for (i = 0; ie && i < ie[1]; i++) {
  1244. if ((ie[i + 2] & 0x7f) > rate)
  1245. rate = ie[i + 2] & 0x7f;
  1246. }
  1247. return rate;
  1248. }
  1249. /**
  1250. * wpa_scan_get_ie - Fetch a specified information element from a scan result
  1251. * @res: Scan result entry
  1252. * @ie: Information element identitifier (WLAN_EID_*)
  1253. * Returns: Pointer to the information element (id field) or %NULL if not found
  1254. *
  1255. * This function returns the first matching information element in the scan
  1256. * result.
  1257. */
  1258. const u8 * wpa_scan_get_ie(const struct wpa_scan_res *res, u8 ie)
  1259. {
  1260. const u8 *end, *pos;
  1261. pos = (const u8 *) (res + 1);
  1262. end = pos + res->ie_len;
  1263. while (pos + 1 < end) {
  1264. if (pos + 2 + pos[1] > end)
  1265. break;
  1266. if (pos[0] == ie)
  1267. return pos;
  1268. pos += 2 + pos[1];
  1269. }
  1270. return NULL;
  1271. }
  1272. /**
  1273. * wpa_scan_get_vendor_ie - Fetch vendor information element from a scan result
  1274. * @res: Scan result entry
  1275. * @vendor_type: Vendor type (four octets starting the IE payload)
  1276. * Returns: Pointer to the information element (id field) or %NULL if not found
  1277. *
  1278. * This function returns the first matching information element in the scan
  1279. * result.
  1280. */
  1281. const u8 * wpa_scan_get_vendor_ie(const struct wpa_scan_res *res,
  1282. u32 vendor_type)
  1283. {
  1284. const u8 *end, *pos;
  1285. pos = (const u8 *) (res + 1);
  1286. end = pos + res->ie_len;
  1287. while (pos + 1 < end) {
  1288. if (pos + 2 + pos[1] > end)
  1289. break;
  1290. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1291. vendor_type == WPA_GET_BE32(&pos[2]))
  1292. return pos;
  1293. pos += 2 + pos[1];
  1294. }
  1295. return NULL;
  1296. }
  1297. /**
  1298. * wpa_scan_get_vendor_ie_beacon - Fetch vendor information from a scan result
  1299. * @res: Scan result entry
  1300. * @vendor_type: Vendor type (four octets starting the IE payload)
  1301. * Returns: Pointer to the information element (id field) or %NULL if not found
  1302. *
  1303. * This function returns the first matching information element in the scan
  1304. * result.
  1305. *
  1306. * This function is like wpa_scan_get_vendor_ie(), but uses IE buffer only
  1307. * from Beacon frames instead of either Beacon or Probe Response frames.
  1308. */
  1309. const u8 * wpa_scan_get_vendor_ie_beacon(const struct wpa_scan_res *res,
  1310. u32 vendor_type)
  1311. {
  1312. const u8 *end, *pos;
  1313. if (res->beacon_ie_len == 0)
  1314. return NULL;
  1315. pos = (const u8 *) (res + 1);
  1316. pos += res->ie_len;
  1317. end = pos + res->beacon_ie_len;
  1318. while (pos + 1 < end) {
  1319. if (pos + 2 + pos[1] > end)
  1320. break;
  1321. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1322. vendor_type == WPA_GET_BE32(&pos[2]))
  1323. return pos;
  1324. pos += 2 + pos[1];
  1325. }
  1326. return NULL;
  1327. }
  1328. /**
  1329. * wpa_scan_get_vendor_ie_multi - Fetch vendor IE data from a scan result
  1330. * @res: Scan result entry
  1331. * @vendor_type: Vendor type (four octets starting the IE payload)
  1332. * Returns: Pointer to the information element payload or %NULL if not found
  1333. *
  1334. * This function returns concatenated payload of possibly fragmented vendor
  1335. * specific information elements in the scan result. The caller is responsible
  1336. * for freeing the returned buffer.
  1337. */
  1338. struct wpabuf * wpa_scan_get_vendor_ie_multi(const struct wpa_scan_res *res,
  1339. u32 vendor_type)
  1340. {
  1341. struct wpabuf *buf;
  1342. const u8 *end, *pos;
  1343. buf = wpabuf_alloc(res->ie_len);
  1344. if (buf == NULL)
  1345. return NULL;
  1346. pos = (const u8 *) (res + 1);
  1347. end = pos + res->ie_len;
  1348. while (pos + 1 < end) {
  1349. if (pos + 2 + pos[1] > end)
  1350. break;
  1351. if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
  1352. vendor_type == WPA_GET_BE32(&pos[2]))
  1353. wpabuf_put_data(buf, pos + 2 + 4, pos[1] - 4);
  1354. pos += 2 + pos[1];
  1355. }
  1356. if (wpabuf_len(buf) == 0) {
  1357. wpabuf_free(buf);
  1358. buf = NULL;
  1359. }
  1360. return buf;
  1361. }
  1362. /*
  1363. * Channels with a great SNR can operate at full rate. What is a great SNR?
  1364. * This doc https://supportforums.cisco.com/docs/DOC-12954 says, "the general
  1365. * rule of thumb is that any SNR above 20 is good." This one
  1366. * http://www.cisco.com/en/US/tech/tk722/tk809/technologies_q_and_a_item09186a00805e9a96.shtml#qa23
  1367. * recommends 25 as a minimum SNR for 54 Mbps data rate. 30 is chosen here as a
  1368. * conservative value.
  1369. */
  1370. #define GREAT_SNR 30
  1371. #define IS_5GHZ(n) (n > 4000)
  1372. /* Compare function for sorting scan results. Return >0 if @b is considered
  1373. * better. */
  1374. static int wpa_scan_result_compar(const void *a, const void *b)
  1375. {
  1376. #define MIN(a,b) a < b ? a : b
  1377. struct wpa_scan_res **_wa = (void *) a;
  1378. struct wpa_scan_res **_wb = (void *) b;
  1379. struct wpa_scan_res *wa = *_wa;
  1380. struct wpa_scan_res *wb = *_wb;
  1381. int wpa_a, wpa_b, maxrate_a, maxrate_b;
  1382. int snr_a, snr_b;
  1383. /* WPA/WPA2 support preferred */
  1384. wpa_a = wpa_scan_get_vendor_ie(wa, WPA_IE_VENDOR_TYPE) != NULL ||
  1385. wpa_scan_get_ie(wa, WLAN_EID_RSN) != NULL;
  1386. wpa_b = wpa_scan_get_vendor_ie(wb, WPA_IE_VENDOR_TYPE) != NULL ||
  1387. wpa_scan_get_ie(wb, WLAN_EID_RSN) != NULL;
  1388. if (wpa_b && !wpa_a)
  1389. return 1;
  1390. if (!wpa_b && wpa_a)
  1391. return -1;
  1392. /* privacy support preferred */
  1393. if ((wa->caps & IEEE80211_CAP_PRIVACY) == 0 &&
  1394. (wb->caps & IEEE80211_CAP_PRIVACY))
  1395. return 1;
  1396. if ((wa->caps & IEEE80211_CAP_PRIVACY) &&
  1397. (wb->caps & IEEE80211_CAP_PRIVACY) == 0)
  1398. return -1;
  1399. if (wa->flags & wb->flags & WPA_SCAN_LEVEL_DBM) {
  1400. snr_a = MIN(wa->level - wa->noise, GREAT_SNR);
  1401. snr_b = MIN(wb->level - wb->noise, GREAT_SNR);
  1402. } else {
  1403. /* Level is not in dBm, so we can't calculate
  1404. * SNR. Just use raw level (units unknown). */
  1405. snr_a = wa->level;
  1406. snr_b = wb->level;
  1407. }
  1408. /* if SNR is close, decide by max rate or frequency band */
  1409. if ((snr_a && snr_b && abs(snr_b - snr_a) < 5) ||
  1410. (wa->qual && wb->qual && abs(wb->qual - wa->qual) < 10)) {
  1411. maxrate_a = wpa_scan_get_max_rate(wa);
  1412. maxrate_b = wpa_scan_get_max_rate(wb);
  1413. if (maxrate_a != maxrate_b)
  1414. return maxrate_b - maxrate_a;
  1415. if (IS_5GHZ(wa->freq) ^ IS_5GHZ(wb->freq))
  1416. return IS_5GHZ(wa->freq) ? -1 : 1;
  1417. }
  1418. /* all things being equal, use SNR; if SNRs are
  1419. * identical, use quality values since some drivers may only report
  1420. * that value and leave the signal level zero */
  1421. if (snr_b == snr_a)
  1422. return wb->qual - wa->qual;
  1423. return snr_b - snr_a;
  1424. #undef MIN
  1425. }
  1426. #ifdef CONFIG_WPS
  1427. /* Compare function for sorting scan results when searching a WPS AP for
  1428. * provisioning. Return >0 if @b is considered better. */
  1429. static int wpa_scan_result_wps_compar(const void *a, const void *b)
  1430. {
  1431. struct wpa_scan_res **_wa = (void *) a;
  1432. struct wpa_scan_res **_wb = (void *) b;
  1433. struct wpa_scan_res *wa = *_wa;
  1434. struct wpa_scan_res *wb = *_wb;
  1435. int uses_wps_a, uses_wps_b;
  1436. struct wpabuf *wps_a, *wps_b;
  1437. int res;
  1438. /* Optimization - check WPS IE existence before allocated memory and
  1439. * doing full reassembly. */
  1440. uses_wps_a = wpa_scan_get_vendor_ie(wa, WPS_IE_VENDOR_TYPE) != NULL;
  1441. uses_wps_b = wpa_scan_get_vendor_ie(wb, WPS_IE_VENDOR_TYPE) != NULL;
  1442. if (uses_wps_a && !uses_wps_b)
  1443. return -1;
  1444. if (!uses_wps_a && uses_wps_b)
  1445. return 1;
  1446. if (uses_wps_a && uses_wps_b) {
  1447. wps_a = wpa_scan_get_vendor_ie_multi(wa, WPS_IE_VENDOR_TYPE);
  1448. wps_b = wpa_scan_get_vendor_ie_multi(wb, WPS_IE_VENDOR_TYPE);
  1449. res = wps_ap_priority_compar(wps_a, wps_b);
  1450. wpabuf_free(wps_a);
  1451. wpabuf_free(wps_b);
  1452. if (res)
  1453. return res;
  1454. }
  1455. /*
  1456. * Do not use current AP security policy as a sorting criteria during
  1457. * WPS provisioning step since the AP may get reconfigured at the
  1458. * completion of provisioning.
  1459. */
  1460. /* all things being equal, use signal level; if signal levels are
  1461. * identical, use quality values since some drivers may only report
  1462. * that value and leave the signal level zero */
  1463. if (wb->level == wa->level)
  1464. return wb->qual - wa->qual;
  1465. return wb->level - wa->level;
  1466. }
  1467. #endif /* CONFIG_WPS */
  1468. static void dump_scan_res(struct wpa_scan_results *scan_res)
  1469. {
  1470. #ifndef CONFIG_NO_STDOUT_DEBUG
  1471. size_t i;
  1472. if (scan_res->res == NULL || scan_res->num == 0)
  1473. return;
  1474. wpa_printf(MSG_EXCESSIVE, "Sorted scan results");
  1475. for (i = 0; i < scan_res->num; i++) {
  1476. struct wpa_scan_res *r = scan_res->res[i];
  1477. u8 *pos;
  1478. if (r->flags & WPA_SCAN_LEVEL_DBM) {
  1479. int snr = r->level - r->noise;
  1480. int noise_valid = !(r->flags & WPA_SCAN_NOISE_INVALID);
  1481. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1482. "noise=%d%s level=%d snr=%d%s flags=0x%x age=%u",
  1483. MAC2STR(r->bssid), r->freq, r->qual,
  1484. r->noise, noise_valid ? "" : "~", r->level,
  1485. snr, snr >= GREAT_SNR ? "*" : "", r->flags,
  1486. r->age);
  1487. } else {
  1488. wpa_printf(MSG_EXCESSIVE, MACSTR " freq=%d qual=%d "
  1489. "noise=%d level=%d flags=0x%x age=%u",
  1490. MAC2STR(r->bssid), r->freq, r->qual,
  1491. r->noise, r->level, r->flags, r->age);
  1492. }
  1493. pos = (u8 *) (r + 1);
  1494. if (r->ie_len)
  1495. wpa_hexdump(MSG_EXCESSIVE, "IEs", pos, r->ie_len);
  1496. pos += r->ie_len;
  1497. if (r->beacon_ie_len)
  1498. wpa_hexdump(MSG_EXCESSIVE, "Beacon IEs",
  1499. pos, r->beacon_ie_len);
  1500. }
  1501. #endif /* CONFIG_NO_STDOUT_DEBUG */
  1502. }
  1503. /**
  1504. * wpa_supplicant_filter_bssid_match - Is the specified BSSID allowed
  1505. * @wpa_s: Pointer to wpa_supplicant data
  1506. * @bssid: BSSID to check
  1507. * Returns: 0 if the BSSID is filtered or 1 if not
  1508. *
  1509. * This function is used to filter out specific BSSIDs from scan reslts mainly
  1510. * for testing purposes (SET bssid_filter ctrl_iface command).
  1511. */
  1512. int wpa_supplicant_filter_bssid_match(struct wpa_supplicant *wpa_s,
  1513. const u8 *bssid)
  1514. {
  1515. size_t i;
  1516. if (wpa_s->bssid_filter == NULL)
  1517. return 1;
  1518. for (i = 0; i < wpa_s->bssid_filter_count; i++) {
  1519. if (os_memcmp(wpa_s->bssid_filter + i * ETH_ALEN, bssid,
  1520. ETH_ALEN) == 0)
  1521. return 1;
  1522. }
  1523. return 0;
  1524. }
  1525. static void filter_scan_res(struct wpa_supplicant *wpa_s,
  1526. struct wpa_scan_results *res)
  1527. {
  1528. size_t i, j;
  1529. if (wpa_s->bssid_filter == NULL)
  1530. return;
  1531. for (i = 0, j = 0; i < res->num; i++) {
  1532. if (wpa_supplicant_filter_bssid_match(wpa_s,
  1533. res->res[i]->bssid)) {
  1534. res->res[j++] = res->res[i];
  1535. } else {
  1536. os_free(res->res[i]);
  1537. res->res[i] = NULL;
  1538. }
  1539. }
  1540. if (res->num != j) {
  1541. wpa_printf(MSG_DEBUG, "Filtered out %d scan results",
  1542. (int) (res->num - j));
  1543. res->num = j;
  1544. }
  1545. }
  1546. /*
  1547. * Noise floor values to use when we have signal strength
  1548. * measurements, but no noise floor measurments. These values were
  1549. * measured in an office environment with many APs.
  1550. */
  1551. #define DEFAULT_NOISE_FLOOR_2GHZ (-89)
  1552. #define DEFAULT_NOISE_FLOOR_5GHZ (-92)
  1553. /**
  1554. * wpa_supplicant_get_scan_results - Get scan results
  1555. * @wpa_s: Pointer to wpa_supplicant data
  1556. * @info: Information about what was scanned or %NULL if not available
  1557. * @new_scan: Whether a new scan was performed
  1558. * Returns: Scan results, %NULL on failure
  1559. *
  1560. * This function request the current scan results from the driver and updates
  1561. * the local BSS list wpa_s->bss. The caller is responsible for freeing the
  1562. * results with wpa_scan_results_free().
  1563. */
  1564. struct wpa_scan_results *
  1565. wpa_supplicant_get_scan_results(struct wpa_supplicant *wpa_s,
  1566. struct scan_info *info, int new_scan)
  1567. {
  1568. struct wpa_scan_results *scan_res;
  1569. size_t i;
  1570. int (*compar)(const void *, const void *) = wpa_scan_result_compar;
  1571. scan_res = wpa_drv_get_scan_results2(wpa_s);
  1572. if (scan_res == NULL) {
  1573. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to get scan results");
  1574. return NULL;
  1575. }
  1576. if (scan_res->fetch_time.sec == 0) {
  1577. /*
  1578. * Make sure we have a valid timestamp if the driver wrapper
  1579. * does not set this.
  1580. */
  1581. os_get_reltime(&scan_res->fetch_time);
  1582. }
  1583. filter_scan_res(wpa_s, scan_res);
  1584. for (i = 0; i < scan_res->num; i++) {
  1585. struct wpa_scan_res *scan_res_item = scan_res->res[i];
  1586. if (scan_res_item->flags & WPA_SCAN_NOISE_INVALID) {
  1587. scan_res_item->noise =
  1588. IS_5GHZ(scan_res_item->freq) ?
  1589. DEFAULT_NOISE_FLOOR_5GHZ :
  1590. DEFAULT_NOISE_FLOOR_2GHZ;
  1591. }
  1592. }
  1593. #ifdef CONFIG_WPS
  1594. if (wpas_wps_searching(wpa_s)) {
  1595. wpa_dbg(wpa_s, MSG_DEBUG, "WPS: Order scan results with WPS "
  1596. "provisioning rules");
  1597. compar = wpa_scan_result_wps_compar;
  1598. }
  1599. #endif /* CONFIG_WPS */
  1600. qsort(scan_res->res, scan_res->num, sizeof(struct wpa_scan_res *),
  1601. compar);
  1602. dump_scan_res(scan_res);
  1603. wpa_bss_update_start(wpa_s);
  1604. for (i = 0; i < scan_res->num; i++)
  1605. wpa_bss_update_scan_res(wpa_s, scan_res->res[i],
  1606. &scan_res->fetch_time);
  1607. wpa_bss_update_end(wpa_s, info, new_scan);
  1608. return scan_res;
  1609. }
  1610. /**
  1611. * wpa_supplicant_update_scan_results - Update scan results from the driver
  1612. * @wpa_s: Pointer to wpa_supplicant data
  1613. * Returns: 0 on success, -1 on failure
  1614. *
  1615. * This function updates the BSS table within wpa_supplicant based on the
  1616. * currently available scan results from the driver without requesting a new
  1617. * scan. This is used in cases where the driver indicates an association
  1618. * (including roaming within ESS) and wpa_supplicant does not yet have the
  1619. * needed information to complete the connection (e.g., to perform validation
  1620. * steps in 4-way handshake).
  1621. */
  1622. int wpa_supplicant_update_scan_results(struct wpa_supplicant *wpa_s)
  1623. {
  1624. struct wpa_scan_results *scan_res;
  1625. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1626. if (scan_res == NULL)
  1627. return -1;
  1628. wpa_scan_results_free(scan_res);
  1629. return 0;
  1630. }
  1631. /**
  1632. * scan_only_handler - Reports scan results
  1633. */
  1634. void scan_only_handler(struct wpa_supplicant *wpa_s,
  1635. struct wpa_scan_results *scan_res)
  1636. {
  1637. wpa_dbg(wpa_s, MSG_DEBUG, "Scan-only results received");
  1638. if (wpa_s->last_scan_req == MANUAL_SCAN_REQ &&
  1639. wpa_s->manual_scan_use_id && wpa_s->own_scan_running) {
  1640. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS "id=%u",
  1641. wpa_s->manual_scan_id);
  1642. wpa_s->manual_scan_use_id = 0;
  1643. } else {
  1644. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_SCAN_RESULTS);
  1645. }
  1646. wpas_notify_scan_results(wpa_s);
  1647. wpas_notify_scan_done(wpa_s, 1);
  1648. if (wpa_s->scan_work) {
  1649. struct wpa_radio_work *work = wpa_s->scan_work;
  1650. wpa_s->scan_work = NULL;
  1651. radio_work_done(work);
  1652. }
  1653. }
  1654. int wpas_scan_scheduled(struct wpa_supplicant *wpa_s)
  1655. {
  1656. return eloop_is_timeout_registered(wpa_supplicant_scan, wpa_s, NULL);
  1657. }
  1658. struct wpa_driver_scan_params *
  1659. wpa_scan_clone_params(const struct wpa_driver_scan_params *src)
  1660. {
  1661. struct wpa_driver_scan_params *params;
  1662. size_t i;
  1663. u8 *n;
  1664. params = os_zalloc(sizeof(*params));
  1665. if (params == NULL)
  1666. return NULL;
  1667. for (i = 0; i < src->num_ssids; i++) {
  1668. if (src->ssids[i].ssid) {
  1669. n = os_malloc(src->ssids[i].ssid_len);
  1670. if (n == NULL)
  1671. goto failed;
  1672. os_memcpy(n, src->ssids[i].ssid,
  1673. src->ssids[i].ssid_len);
  1674. params->ssids[i].ssid = n;
  1675. params->ssids[i].ssid_len = src->ssids[i].ssid_len;
  1676. }
  1677. }
  1678. params->num_ssids = src->num_ssids;
  1679. if (src->extra_ies) {
  1680. n = os_malloc(src->extra_ies_len);
  1681. if (n == NULL)
  1682. goto failed;
  1683. os_memcpy(n, src->extra_ies, src->extra_ies_len);
  1684. params->extra_ies = n;
  1685. params->extra_ies_len = src->extra_ies_len;
  1686. }
  1687. if (src->freqs) {
  1688. int len = int_array_len(src->freqs);
  1689. params->freqs = os_malloc((len + 1) * sizeof(int));
  1690. if (params->freqs == NULL)
  1691. goto failed;
  1692. os_memcpy(params->freqs, src->freqs, (len + 1) * sizeof(int));
  1693. }
  1694. if (src->filter_ssids) {
  1695. params->filter_ssids = os_malloc(sizeof(*params->filter_ssids) *
  1696. src->num_filter_ssids);
  1697. if (params->filter_ssids == NULL)
  1698. goto failed;
  1699. os_memcpy(params->filter_ssids, src->filter_ssids,
  1700. sizeof(*params->filter_ssids) *
  1701. src->num_filter_ssids);
  1702. params->num_filter_ssids = src->num_filter_ssids;
  1703. }
  1704. params->filter_rssi = src->filter_rssi;
  1705. params->p2p_probe = src->p2p_probe;
  1706. params->only_new_results = src->only_new_results;
  1707. params->low_priority = src->low_priority;
  1708. if (src->mac_addr_rand) {
  1709. params->mac_addr_rand = src->mac_addr_rand;
  1710. if (src->mac_addr && src->mac_addr_mask) {
  1711. u8 *mac_addr;
  1712. mac_addr = os_malloc(2 * ETH_ALEN);
  1713. if (!mac_addr)
  1714. goto failed;
  1715. os_memcpy(mac_addr, src->mac_addr, ETH_ALEN);
  1716. os_memcpy(mac_addr + ETH_ALEN, src->mac_addr_mask,
  1717. ETH_ALEN);
  1718. params->mac_addr = mac_addr;
  1719. params->mac_addr_mask = mac_addr + ETH_ALEN;
  1720. }
  1721. }
  1722. return params;
  1723. failed:
  1724. wpa_scan_free_params(params);
  1725. return NULL;
  1726. }
  1727. void wpa_scan_free_params(struct wpa_driver_scan_params *params)
  1728. {
  1729. size_t i;
  1730. if (params == NULL)
  1731. return;
  1732. for (i = 0; i < params->num_ssids; i++)
  1733. os_free((u8 *) params->ssids[i].ssid);
  1734. os_free((u8 *) params->extra_ies);
  1735. os_free(params->freqs);
  1736. os_free(params->filter_ssids);
  1737. /*
  1738. * Note: params->mac_addr_mask points to same memory allocation and
  1739. * must not be freed separately.
  1740. */
  1741. os_free((u8 *) params->mac_addr);
  1742. os_free(params);
  1743. }
  1744. int wpas_start_pno(struct wpa_supplicant *wpa_s)
  1745. {
  1746. int ret, interval;
  1747. size_t i, num_ssid, num_match_ssid;
  1748. struct wpa_ssid *ssid;
  1749. struct wpa_driver_scan_params params;
  1750. if (!wpa_s->sched_scan_supported)
  1751. return -1;
  1752. if (wpa_s->pno || wpa_s->pno_sched_pending)
  1753. return 0;
  1754. if ((wpa_s->wpa_state > WPA_SCANNING) &&
  1755. (wpa_s->wpa_state <= WPA_COMPLETED)) {
  1756. wpa_printf(MSG_ERROR, "PNO: In assoc process");
  1757. return -EAGAIN;
  1758. }
  1759. if (wpa_s->wpa_state == WPA_SCANNING) {
  1760. wpa_supplicant_cancel_scan(wpa_s);
  1761. if (wpa_s->sched_scanning) {
  1762. wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
  1763. "ongoing sched scan");
  1764. wpa_supplicant_cancel_sched_scan(wpa_s);
  1765. wpa_s->pno_sched_pending = 1;
  1766. return 0;
  1767. }
  1768. }
  1769. os_memset(&params, 0, sizeof(params));
  1770. num_ssid = num_match_ssid = 0;
  1771. ssid = wpa_s->conf->ssid;
  1772. while (ssid) {
  1773. if (!wpas_network_disabled(wpa_s, ssid)) {
  1774. num_match_ssid++;
  1775. if (ssid->scan_ssid)
  1776. num_ssid++;
  1777. }
  1778. ssid = ssid->next;
  1779. }
  1780. if (num_match_ssid == 0) {
  1781. wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
  1782. return -1;
  1783. }
  1784. if (num_match_ssid > num_ssid) {
  1785. params.num_ssids++; /* wildcard */
  1786. num_ssid++;
  1787. }
  1788. if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
  1789. wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
  1790. "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
  1791. num_ssid = WPAS_MAX_SCAN_SSIDS;
  1792. }
  1793. if (num_match_ssid > wpa_s->max_match_sets) {
  1794. num_match_ssid = wpa_s->max_match_sets;
  1795. wpa_dbg(wpa_s, MSG_DEBUG, "PNO: Too many SSIDs to match");
  1796. }
  1797. params.filter_ssids = os_calloc(num_match_ssid,
  1798. sizeof(struct wpa_driver_scan_filter));
  1799. if (params.filter_ssids == NULL)
  1800. return -1;
  1801. i = 0;
  1802. ssid = wpa_s->conf->ssid;
  1803. while (ssid) {
  1804. if (!wpas_network_disabled(wpa_s, ssid)) {
  1805. if (ssid->scan_ssid && params.num_ssids < num_ssid) {
  1806. params.ssids[params.num_ssids].ssid =
  1807. ssid->ssid;
  1808. params.ssids[params.num_ssids].ssid_len =
  1809. ssid->ssid_len;
  1810. params.num_ssids++;
  1811. }
  1812. os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
  1813. ssid->ssid_len);
  1814. params.filter_ssids[i].ssid_len = ssid->ssid_len;
  1815. params.num_filter_ssids++;
  1816. i++;
  1817. if (i == num_match_ssid)
  1818. break;
  1819. }
  1820. ssid = ssid->next;
  1821. }
  1822. if (wpa_s->conf->filter_rssi)
  1823. params.filter_rssi = wpa_s->conf->filter_rssi;
  1824. interval = wpa_s->conf->sched_scan_interval ?
  1825. wpa_s->conf->sched_scan_interval : 10;
  1826. if (params.freqs == NULL && wpa_s->manual_sched_scan_freqs) {
  1827. wpa_dbg(wpa_s, MSG_DEBUG, "Limit sched scan to specified channels");
  1828. params.freqs = wpa_s->manual_sched_scan_freqs;
  1829. }
  1830. if (wpa_s->mac_addr_rand_enable & MAC_ADDR_RAND_PNO) {
  1831. params.mac_addr_rand = 1;
  1832. if (wpa_s->mac_addr_pno) {
  1833. params.mac_addr = wpa_s->mac_addr_pno;
  1834. params.mac_addr_mask = wpa_s->mac_addr_pno + ETH_ALEN;
  1835. }
  1836. }
  1837. ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
  1838. os_free(params.filter_ssids);
  1839. if (ret == 0)
  1840. wpa_s->pno = 1;
  1841. else
  1842. wpa_msg(wpa_s, MSG_ERROR, "Failed to schedule PNO");
  1843. return ret;
  1844. }
  1845. int wpas_stop_pno(struct wpa_supplicant *wpa_s)
  1846. {
  1847. int ret = 0;
  1848. if (!wpa_s->pno)
  1849. return 0;
  1850. ret = wpa_supplicant_stop_sched_scan(wpa_s);
  1851. wpa_s->pno = 0;
  1852. wpa_s->pno_sched_pending = 0;
  1853. if (wpa_s->wpa_state == WPA_SCANNING)
  1854. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1855. return ret;
  1856. }
  1857. void wpas_mac_addr_rand_scan_clear(struct wpa_supplicant *wpa_s,
  1858. unsigned int type)
  1859. {
  1860. type &= MAC_ADDR_RAND_ALL;
  1861. wpa_s->mac_addr_rand_enable &= ~type;
  1862. if (type & MAC_ADDR_RAND_SCAN) {
  1863. os_free(wpa_s->mac_addr_scan);
  1864. wpa_s->mac_addr_scan = NULL;
  1865. }
  1866. if (type & MAC_ADDR_RAND_SCHED_SCAN) {
  1867. os_free(wpa_s->mac_addr_sched_scan);
  1868. wpa_s->mac_addr_sched_scan = NULL;
  1869. }
  1870. if (type & MAC_ADDR_RAND_PNO) {
  1871. os_free(wpa_s->mac_addr_pno);
  1872. wpa_s->mac_addr_pno = NULL;
  1873. }
  1874. }
  1875. int wpas_mac_addr_rand_scan_set(struct wpa_supplicant *wpa_s,
  1876. unsigned int type, const u8 *addr,
  1877. const u8 *mask)
  1878. {
  1879. u8 *tmp = NULL;
  1880. wpas_mac_addr_rand_scan_clear(wpa_s, type);
  1881. if (addr) {
  1882. tmp = os_malloc(2 * ETH_ALEN);
  1883. if (!tmp)
  1884. return -1;
  1885. os_memcpy(tmp, addr, ETH_ALEN);
  1886. os_memcpy(tmp + ETH_ALEN, mask, ETH_ALEN);
  1887. }
  1888. if (type == MAC_ADDR_RAND_SCAN) {
  1889. wpa_s->mac_addr_scan = tmp;
  1890. } else if (type == MAC_ADDR_RAND_SCHED_SCAN) {
  1891. wpa_s->mac_addr_sched_scan = tmp;
  1892. } else if (type == MAC_ADDR_RAND_PNO) {
  1893. wpa_s->mac_addr_pno = tmp;
  1894. } else {
  1895. wpa_printf(MSG_INFO,
  1896. "scan: Invalid MAC randomization type=0x%x",
  1897. type);
  1898. os_free(tmp);
  1899. return -1;
  1900. }
  1901. wpa_s->mac_addr_rand_enable |= type;
  1902. return 0;
  1903. }