wpa_supplicant.c 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2012, 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. * This file implements functions for registering and unregistering
  15. * %wpa_supplicant interfaces. In addition, this file contains number of
  16. * functions for managing network connections.
  17. */
  18. #include "includes.h"
  19. #include "common.h"
  20. #include "crypto/random.h"
  21. #include "crypto/sha1.h"
  22. #include "eapol_supp/eapol_supp_sm.h"
  23. #include "eap_peer/eap.h"
  24. #include "eap_server/eap_methods.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "eloop.h"
  27. #include "config.h"
  28. #include "l2_packet/l2_packet.h"
  29. #include "wpa_supplicant_i.h"
  30. #include "driver_i.h"
  31. #include "ctrl_iface.h"
  32. #include "pcsc_funcs.h"
  33. #include "common/version.h"
  34. #include "rsn_supp/preauth.h"
  35. #include "rsn_supp/pmksa_cache.h"
  36. #include "common/wpa_ctrl.h"
  37. #include "common/ieee802_11_defs.h"
  38. #include "p2p/p2p.h"
  39. #include "blacklist.h"
  40. #include "wpas_glue.h"
  41. #include "wps_supplicant.h"
  42. #include "ibss_rsn.h"
  43. #include "sme.h"
  44. #include "gas_query.h"
  45. #include "ap.h"
  46. #include "p2p_supplicant.h"
  47. #include "notify.h"
  48. #include "bgscan.h"
  49. #include "bss.h"
  50. #include "scan.h"
  51. #include "offchannel.h"
  52. const char *wpa_supplicant_version =
  53. "wpa_supplicant v" VERSION_STR "\n"
  54. "Copyright (c) 2003-2012, Jouni Malinen <j@w1.fi> and contributors";
  55. const char *wpa_supplicant_license =
  56. "This program is free software. You can distribute it and/or modify it\n"
  57. "under the terms of the GNU General Public License version 2.\n"
  58. "\n"
  59. "Alternatively, this software may be distributed under the terms of the\n"
  60. "BSD license. See README and COPYING for more details.\n"
  61. #ifdef EAP_TLS_OPENSSL
  62. "\nThis product includes software developed by the OpenSSL Project\n"
  63. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  64. #endif /* EAP_TLS_OPENSSL */
  65. ;
  66. #ifndef CONFIG_NO_STDOUT_DEBUG
  67. /* Long text divided into parts in order to fit in C89 strings size limits. */
  68. const char *wpa_supplicant_full_license1 =
  69. "This program is free software; you can redistribute it and/or modify\n"
  70. "it under the terms of the GNU General Public License version 2 as\n"
  71. "published by the Free Software Foundation.\n"
  72. "\n"
  73. "This program is distributed in the hope that it will be useful,\n"
  74. "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  75. "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
  76. "GNU General Public License for more details.\n"
  77. "\n";
  78. const char *wpa_supplicant_full_license2 =
  79. "You should have received a copy of the GNU General Public License\n"
  80. "along with this program; if not, write to the Free Software\n"
  81. "Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA\n"
  82. "\n"
  83. "Alternatively, this software may be distributed under the terms of the\n"
  84. "BSD license.\n"
  85. "\n"
  86. "Redistribution and use in source and binary forms, with or without\n"
  87. "modification, are permitted provided that the following conditions are\n"
  88. "met:\n"
  89. "\n";
  90. const char *wpa_supplicant_full_license3 =
  91. "1. Redistributions of source code must retain the above copyright\n"
  92. " notice, this list of conditions and the following disclaimer.\n"
  93. "\n"
  94. "2. Redistributions in binary form must reproduce the above copyright\n"
  95. " notice, this list of conditions and the following disclaimer in the\n"
  96. " documentation and/or other materials provided with the distribution.\n"
  97. "\n";
  98. const char *wpa_supplicant_full_license4 =
  99. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  100. " names of its contributors may be used to endorse or promote products\n"
  101. " derived from this software without specific prior written permission.\n"
  102. "\n"
  103. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  104. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  105. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  106. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  107. const char *wpa_supplicant_full_license5 =
  108. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  109. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  110. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  111. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  112. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  113. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  114. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  115. "\n";
  116. #endif /* CONFIG_NO_STDOUT_DEBUG */
  117. extern int wpa_debug_level;
  118. extern int wpa_debug_show_keys;
  119. extern int wpa_debug_timestamp;
  120. extern struct wpa_driver_ops *wpa_drivers[];
  121. /* Configure default/group WEP keys for static WEP */
  122. int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  123. {
  124. int i, set = 0;
  125. for (i = 0; i < NUM_WEP_KEYS; i++) {
  126. if (ssid->wep_key_len[i] == 0)
  127. continue;
  128. set = 1;
  129. wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
  130. i, i == ssid->wep_tx_keyidx, NULL, 0,
  131. ssid->wep_key[i], ssid->wep_key_len[i]);
  132. }
  133. return set;
  134. }
  135. static int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  136. struct wpa_ssid *ssid)
  137. {
  138. u8 key[32];
  139. size_t keylen;
  140. enum wpa_alg alg;
  141. u8 seq[6] = { 0 };
  142. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  143. * sending unicast and multicast packets. */
  144. if (ssid->mode != WPAS_MODE_IBSS) {
  145. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
  146. "IBSS/ad-hoc) for WPA-None", ssid->mode);
  147. return -1;
  148. }
  149. if (!ssid->psk_set) {
  150. wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
  151. "WPA-None");
  152. return -1;
  153. }
  154. switch (wpa_s->group_cipher) {
  155. case WPA_CIPHER_CCMP:
  156. os_memcpy(key, ssid->psk, 16);
  157. keylen = 16;
  158. alg = WPA_ALG_CCMP;
  159. break;
  160. case WPA_CIPHER_TKIP:
  161. /* WPA-None uses the same Michael MIC key for both TX and RX */
  162. os_memcpy(key, ssid->psk, 16 + 8);
  163. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  164. keylen = 32;
  165. alg = WPA_ALG_TKIP;
  166. break;
  167. default:
  168. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
  169. "WPA-None", wpa_s->group_cipher);
  170. return -1;
  171. }
  172. /* TODO: should actually remember the previously used seq#, both for TX
  173. * and RX from each STA.. */
  174. return wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
  175. }
  176. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  177. {
  178. struct wpa_supplicant *wpa_s = eloop_ctx;
  179. const u8 *bssid = wpa_s->bssid;
  180. if (is_zero_ether_addr(bssid))
  181. bssid = wpa_s->pending_bssid;
  182. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  183. MAC2STR(bssid));
  184. wpa_blacklist_add(wpa_s, bssid);
  185. wpa_sm_notify_disassoc(wpa_s->wpa);
  186. wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  187. wpa_s->reassociate = 1;
  188. /*
  189. * If we timed out, the AP or the local radio may be busy.
  190. * So, wait a second until scanning again.
  191. */
  192. wpa_supplicant_req_scan(wpa_s, 1, 0);
  193. }
  194. /**
  195. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  196. * @wpa_s: Pointer to wpa_supplicant data
  197. * @sec: Number of seconds after which to time out authentication
  198. * @usec: Number of microseconds after which to time out authentication
  199. *
  200. * This function is used to schedule a timeout for the current authentication
  201. * attempt.
  202. */
  203. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  204. int sec, int usec)
  205. {
  206. if (wpa_s->conf && wpa_s->conf->ap_scan == 0 &&
  207. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  208. return;
  209. wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  210. "%d usec", sec, usec);
  211. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  212. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  213. }
  214. /**
  215. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  216. * @wpa_s: Pointer to wpa_supplicant data
  217. *
  218. * This function is used to cancel authentication timeout scheduled with
  219. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  220. * been completed.
  221. */
  222. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  223. {
  224. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  225. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  226. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  227. }
  228. /**
  229. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  230. * @wpa_s: Pointer to wpa_supplicant data
  231. *
  232. * This function is used to configure EAPOL state machine based on the selected
  233. * authentication mode.
  234. */
  235. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  236. {
  237. #ifdef IEEE8021X_EAPOL
  238. struct eapol_config eapol_conf;
  239. struct wpa_ssid *ssid = wpa_s->current_ssid;
  240. #ifdef CONFIG_IBSS_RSN
  241. if (ssid->mode == WPAS_MODE_IBSS &&
  242. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  243. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  244. /*
  245. * RSN IBSS authentication is per-STA and we can disable the
  246. * per-BSSID EAPOL authentication.
  247. */
  248. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  249. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  250. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  251. return;
  252. }
  253. #endif /* CONFIG_IBSS_RSN */
  254. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  255. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  256. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  257. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  258. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  259. else
  260. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  261. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  262. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  263. eapol_conf.accept_802_1x_keys = 1;
  264. eapol_conf.required_keys = 0;
  265. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  266. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  267. }
  268. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  269. eapol_conf.required_keys |=
  270. EAPOL_REQUIRE_KEY_BROADCAST;
  271. }
  272. if (wpa_s->conf && (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  273. eapol_conf.required_keys = 0;
  274. }
  275. if (wpa_s->conf)
  276. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  277. eapol_conf.workaround = ssid->eap_workaround;
  278. eapol_conf.eap_disabled =
  279. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  280. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  281. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  282. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  283. #endif /* IEEE8021X_EAPOL */
  284. }
  285. /**
  286. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  287. * @wpa_s: Pointer to wpa_supplicant data
  288. * @ssid: Configuration data for the network
  289. *
  290. * This function is used to configure WPA state machine and related parameters
  291. * to a mode where WPA is not enabled. This is called as part of the
  292. * authentication configuration when the selected network does not use WPA.
  293. */
  294. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  295. struct wpa_ssid *ssid)
  296. {
  297. int i;
  298. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  299. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  300. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  301. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  302. else
  303. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  304. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  305. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  306. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  307. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  308. wpa_s->group_cipher = WPA_CIPHER_NONE;
  309. wpa_s->mgmt_group_cipher = 0;
  310. for (i = 0; i < NUM_WEP_KEYS; i++) {
  311. if (ssid->wep_key_len[i] > 5) {
  312. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  313. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  314. break;
  315. } else if (ssid->wep_key_len[i] > 0) {
  316. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  317. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  318. break;
  319. }
  320. }
  321. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  322. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  323. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  324. wpa_s->pairwise_cipher);
  325. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  326. #ifdef CONFIG_IEEE80211W
  327. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  328. wpa_s->mgmt_group_cipher);
  329. #endif /* CONFIG_IEEE80211W */
  330. pmksa_cache_clear_current(wpa_s->wpa);
  331. }
  332. static void free_hw_features(struct wpa_supplicant *wpa_s)
  333. {
  334. int i;
  335. if (wpa_s->hw.modes == NULL)
  336. return;
  337. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  338. os_free(wpa_s->hw.modes[i].channels);
  339. os_free(wpa_s->hw.modes[i].rates);
  340. }
  341. os_free(wpa_s->hw.modes);
  342. wpa_s->hw.modes = NULL;
  343. }
  344. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  345. {
  346. bgscan_deinit(wpa_s);
  347. scard_deinit(wpa_s->scard);
  348. wpa_s->scard = NULL;
  349. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  350. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  351. l2_packet_deinit(wpa_s->l2);
  352. wpa_s->l2 = NULL;
  353. if (wpa_s->l2_br) {
  354. l2_packet_deinit(wpa_s->l2_br);
  355. wpa_s->l2_br = NULL;
  356. }
  357. if (wpa_s->conf != NULL) {
  358. struct wpa_ssid *ssid;
  359. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  360. wpas_notify_network_removed(wpa_s, ssid);
  361. }
  362. os_free(wpa_s->confname);
  363. wpa_s->confname = NULL;
  364. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  365. eapol_sm_deinit(wpa_s->eapol);
  366. wpa_s->eapol = NULL;
  367. rsn_preauth_deinit(wpa_s->wpa);
  368. #ifdef CONFIG_TDLS
  369. wpa_tdls_deinit(wpa_s->wpa);
  370. #endif /* CONFIG_TDLS */
  371. pmksa_candidate_free(wpa_s->wpa);
  372. wpa_sm_deinit(wpa_s->wpa);
  373. wpa_s->wpa = NULL;
  374. wpa_blacklist_clear(wpa_s);
  375. wpa_bss_deinit(wpa_s);
  376. wpa_supplicant_cancel_scan(wpa_s);
  377. wpa_supplicant_cancel_auth_timeout(wpa_s);
  378. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  379. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  380. eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
  381. wpa_s, NULL);
  382. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  383. wpas_wps_deinit(wpa_s);
  384. wpabuf_free(wpa_s->pending_eapol_rx);
  385. wpa_s->pending_eapol_rx = NULL;
  386. #ifdef CONFIG_IBSS_RSN
  387. ibss_rsn_deinit(wpa_s->ibss_rsn);
  388. wpa_s->ibss_rsn = NULL;
  389. #endif /* CONFIG_IBSS_RSN */
  390. sme_deinit(wpa_s);
  391. #ifdef CONFIG_AP
  392. wpa_supplicant_ap_deinit(wpa_s);
  393. #endif /* CONFIG_AP */
  394. #ifdef CONFIG_P2P
  395. wpas_p2p_deinit(wpa_s);
  396. #endif /* CONFIG_P2P */
  397. #ifdef CONFIG_OFFCHANNEL
  398. offchannel_deinit(wpa_s);
  399. #endif /* CONFIG_OFFCHANNEL */
  400. wpa_supplicant_cancel_sched_scan(wpa_s);
  401. os_free(wpa_s->next_scan_freqs);
  402. wpa_s->next_scan_freqs = NULL;
  403. gas_query_deinit(wpa_s->gas);
  404. wpa_s->gas = NULL;
  405. free_hw_features(wpa_s);
  406. }
  407. /**
  408. * wpa_clear_keys - Clear keys configured for the driver
  409. * @wpa_s: Pointer to wpa_supplicant data
  410. * @addr: Previously used BSSID or %NULL if not available
  411. *
  412. * This function clears the encryption keys that has been previously configured
  413. * for the driver.
  414. */
  415. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  416. {
  417. if (wpa_s->keys_cleared) {
  418. /* Some drivers (e.g., ndiswrapper & NDIS drivers) seem to have
  419. * timing issues with keys being cleared just before new keys
  420. * are set or just after association or something similar. This
  421. * shows up in group key handshake failing often because of the
  422. * client not receiving the first encrypted packets correctly.
  423. * Skipping some of the extra key clearing steps seems to help
  424. * in completing group key handshake more reliably. */
  425. wpa_dbg(wpa_s, MSG_DEBUG, "No keys have been configured - "
  426. "skip key clearing");
  427. return;
  428. }
  429. /* MLME-DELETEKEYS.request */
  430. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  431. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  432. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  433. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  434. #ifdef CONFIG_IEEE80211W
  435. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  436. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  437. #endif /* CONFIG_IEEE80211W */
  438. if (addr) {
  439. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  440. 0);
  441. /* MLME-SETPROTECTION.request(None) */
  442. wpa_drv_mlme_setprotection(
  443. wpa_s, addr,
  444. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  445. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  446. }
  447. wpa_s->keys_cleared = 1;
  448. }
  449. /**
  450. * wpa_supplicant_state_txt - Get the connection state name as a text string
  451. * @state: State (wpa_state; WPA_*)
  452. * Returns: The state name as a printable text string
  453. */
  454. const char * wpa_supplicant_state_txt(enum wpa_states state)
  455. {
  456. switch (state) {
  457. case WPA_DISCONNECTED:
  458. return "DISCONNECTED";
  459. case WPA_INACTIVE:
  460. return "INACTIVE";
  461. case WPA_INTERFACE_DISABLED:
  462. return "INTERFACE_DISABLED";
  463. case WPA_SCANNING:
  464. return "SCANNING";
  465. case WPA_AUTHENTICATING:
  466. return "AUTHENTICATING";
  467. case WPA_ASSOCIATING:
  468. return "ASSOCIATING";
  469. case WPA_ASSOCIATED:
  470. return "ASSOCIATED";
  471. case WPA_4WAY_HANDSHAKE:
  472. return "4WAY_HANDSHAKE";
  473. case WPA_GROUP_HANDSHAKE:
  474. return "GROUP_HANDSHAKE";
  475. case WPA_COMPLETED:
  476. return "COMPLETED";
  477. default:
  478. return "UNKNOWN";
  479. }
  480. }
  481. #ifdef CONFIG_BGSCAN
  482. static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
  483. {
  484. if (wpas_driver_bss_selection(wpa_s))
  485. return;
  486. if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
  487. return;
  488. bgscan_deinit(wpa_s);
  489. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan) {
  490. if (bgscan_init(wpa_s, wpa_s->current_ssid)) {
  491. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  492. "bgscan");
  493. /*
  494. * Live without bgscan; it is only used as a roaming
  495. * optimization, so the initial connection is not
  496. * affected.
  497. */
  498. } else
  499. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  500. } else
  501. wpa_s->bgscan_ssid = NULL;
  502. }
  503. static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
  504. {
  505. if (wpa_s->bgscan_ssid != NULL) {
  506. bgscan_deinit(wpa_s);
  507. wpa_s->bgscan_ssid = NULL;
  508. }
  509. }
  510. #endif /* CONFIG_BGSCAN */
  511. /**
  512. * wpa_supplicant_set_state - Set current connection state
  513. * @wpa_s: Pointer to wpa_supplicant data
  514. * @state: The new connection state
  515. *
  516. * This function is called whenever the connection state changes, e.g.,
  517. * association is completed for WPA/WPA2 4-Way Handshake is started.
  518. */
  519. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
  520. enum wpa_states state)
  521. {
  522. enum wpa_states old_state = wpa_s->wpa_state;
  523. wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
  524. wpa_supplicant_state_txt(wpa_s->wpa_state),
  525. wpa_supplicant_state_txt(state));
  526. if (state != WPA_SCANNING)
  527. wpa_supplicant_notify_scanning(wpa_s, 0);
  528. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  529. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  530. struct wpa_ssid *ssid = wpa_s->current_ssid;
  531. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  532. MACSTR " completed %s [id=%d id_str=%s]",
  533. MAC2STR(wpa_s->bssid), wpa_s->reassociated_connection ?
  534. "(reauth)" : "(auth)",
  535. ssid ? ssid->id : -1,
  536. ssid && ssid->id_str ? ssid->id_str : "");
  537. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  538. wpa_s->new_connection = 0;
  539. wpa_s->reassociated_connection = 1;
  540. wpa_drv_set_operstate(wpa_s, 1);
  541. #ifndef IEEE8021X_EAPOL
  542. wpa_drv_set_supp_port(wpa_s, 1);
  543. #endif /* IEEE8021X_EAPOL */
  544. wpa_s->after_wps = 0;
  545. #ifdef CONFIG_P2P
  546. wpas_p2p_completed(wpa_s);
  547. #endif /* CONFIG_P2P */
  548. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  549. state == WPA_ASSOCIATED) {
  550. wpa_s->new_connection = 1;
  551. wpa_drv_set_operstate(wpa_s, 0);
  552. #ifndef IEEE8021X_EAPOL
  553. wpa_drv_set_supp_port(wpa_s, 0);
  554. #endif /* IEEE8021X_EAPOL */
  555. }
  556. wpa_s->wpa_state = state;
  557. #ifdef CONFIG_BGSCAN
  558. if (state == WPA_COMPLETED)
  559. wpa_supplicant_start_bgscan(wpa_s);
  560. else
  561. wpa_supplicant_stop_bgscan(wpa_s);
  562. #endif /* CONFIG_BGSCAN */
  563. if (wpa_s->wpa_state != old_state) {
  564. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  565. if (wpa_s->wpa_state == WPA_COMPLETED ||
  566. old_state == WPA_COMPLETED)
  567. wpas_notify_auth_changed(wpa_s);
  568. }
  569. }
  570. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  571. {
  572. int pending = 0;
  573. #ifdef CONFIG_WPS
  574. struct wpa_supplicant *wpa_s = global->ifaces;
  575. while (wpa_s) {
  576. if (wpas_wps_terminate_pending(wpa_s) == 1)
  577. pending = 1;
  578. wpa_s = wpa_s->next;
  579. }
  580. #endif /* CONFIG_WPS */
  581. if (pending)
  582. return;
  583. eloop_terminate();
  584. }
  585. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  586. {
  587. struct wpa_global *global = signal_ctx;
  588. wpa_supplicant_terminate_proc(global);
  589. }
  590. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  591. {
  592. enum wpa_states old_state = wpa_s->wpa_state;
  593. wpa_s->pairwise_cipher = 0;
  594. wpa_s->group_cipher = 0;
  595. wpa_s->mgmt_group_cipher = 0;
  596. wpa_s->key_mgmt = 0;
  597. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  598. wpa_s->wpa_state = WPA_DISCONNECTED;
  599. if (wpa_s->wpa_state != old_state)
  600. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  601. }
  602. /**
  603. * wpa_supplicant_reload_configuration - Reload configuration data
  604. * @wpa_s: Pointer to wpa_supplicant data
  605. * Returns: 0 on success or -1 if configuration parsing failed
  606. *
  607. * This function can be used to request that the configuration data is reloaded
  608. * (e.g., after configuration file change). This function is reloading
  609. * configuration only for one interface, so this may need to be called multiple
  610. * times if %wpa_supplicant is controlling multiple interfaces and all
  611. * interfaces need reconfiguration.
  612. */
  613. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  614. {
  615. struct wpa_config *conf;
  616. int reconf_ctrl;
  617. int old_ap_scan;
  618. if (wpa_s->confname == NULL)
  619. return -1;
  620. conf = wpa_config_read(wpa_s->confname);
  621. if (conf == NULL) {
  622. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  623. "file '%s' - exiting", wpa_s->confname);
  624. return -1;
  625. }
  626. conf->changed_parameters = (unsigned int) -1;
  627. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  628. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  629. os_strcmp(conf->ctrl_interface,
  630. wpa_s->conf->ctrl_interface) != 0);
  631. if (reconf_ctrl && wpa_s->ctrl_iface) {
  632. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  633. wpa_s->ctrl_iface = NULL;
  634. }
  635. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  636. if (wpa_s->current_ssid) {
  637. wpa_supplicant_deauthenticate(wpa_s,
  638. WLAN_REASON_DEAUTH_LEAVING);
  639. }
  640. /*
  641. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  642. * pkcs11_engine_path, pkcs11_module_path.
  643. */
  644. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  645. /*
  646. * Clear forced success to clear EAP state for next
  647. * authentication.
  648. */
  649. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  650. }
  651. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  652. wpa_sm_set_config(wpa_s->wpa, NULL);
  653. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  654. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  655. rsn_preauth_deinit(wpa_s->wpa);
  656. old_ap_scan = wpa_s->conf->ap_scan;
  657. wpa_config_free(wpa_s->conf);
  658. wpa_s->conf = conf;
  659. if (old_ap_scan != wpa_s->conf->ap_scan)
  660. wpas_notify_ap_scan_changed(wpa_s);
  661. if (reconf_ctrl)
  662. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  663. wpa_supplicant_update_config(wpa_s);
  664. wpa_supplicant_clear_status(wpa_s);
  665. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  666. wpa_s->reassociate = 1;
  667. wpa_supplicant_req_scan(wpa_s, 0, 0);
  668. }
  669. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  670. return 0;
  671. }
  672. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  673. {
  674. struct wpa_global *global = signal_ctx;
  675. struct wpa_supplicant *wpa_s;
  676. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  677. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  678. sig);
  679. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  680. wpa_supplicant_terminate_proc(global);
  681. }
  682. }
  683. }
  684. enum wpa_cipher cipher_suite2driver(int cipher)
  685. {
  686. switch (cipher) {
  687. case WPA_CIPHER_NONE:
  688. return CIPHER_NONE;
  689. case WPA_CIPHER_WEP40:
  690. return CIPHER_WEP40;
  691. case WPA_CIPHER_WEP104:
  692. return CIPHER_WEP104;
  693. case WPA_CIPHER_CCMP:
  694. return CIPHER_CCMP;
  695. case WPA_CIPHER_TKIP:
  696. default:
  697. return CIPHER_TKIP;
  698. }
  699. }
  700. enum wpa_key_mgmt key_mgmt2driver(int key_mgmt)
  701. {
  702. switch (key_mgmt) {
  703. case WPA_KEY_MGMT_NONE:
  704. return KEY_MGMT_NONE;
  705. case WPA_KEY_MGMT_IEEE8021X_NO_WPA:
  706. return KEY_MGMT_802_1X_NO_WPA;
  707. case WPA_KEY_MGMT_IEEE8021X:
  708. return KEY_MGMT_802_1X;
  709. case WPA_KEY_MGMT_WPA_NONE:
  710. return KEY_MGMT_WPA_NONE;
  711. case WPA_KEY_MGMT_FT_IEEE8021X:
  712. return KEY_MGMT_FT_802_1X;
  713. case WPA_KEY_MGMT_FT_PSK:
  714. return KEY_MGMT_FT_PSK;
  715. case WPA_KEY_MGMT_IEEE8021X_SHA256:
  716. return KEY_MGMT_802_1X_SHA256;
  717. case WPA_KEY_MGMT_PSK_SHA256:
  718. return KEY_MGMT_PSK_SHA256;
  719. case WPA_KEY_MGMT_WPS:
  720. return KEY_MGMT_WPS;
  721. case WPA_KEY_MGMT_PSK:
  722. default:
  723. return KEY_MGMT_PSK;
  724. }
  725. }
  726. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  727. struct wpa_ssid *ssid,
  728. struct wpa_ie_data *ie)
  729. {
  730. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  731. if (ret) {
  732. if (ret == -2) {
  733. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  734. "from association info");
  735. }
  736. return -1;
  737. }
  738. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  739. "cipher suites");
  740. if (!(ie->group_cipher & ssid->group_cipher)) {
  741. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  742. "cipher 0x%x (mask 0x%x) - reject",
  743. ie->group_cipher, ssid->group_cipher);
  744. return -1;
  745. }
  746. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  747. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  748. "cipher 0x%x (mask 0x%x) - reject",
  749. ie->pairwise_cipher, ssid->pairwise_cipher);
  750. return -1;
  751. }
  752. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  753. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  754. "management 0x%x (mask 0x%x) - reject",
  755. ie->key_mgmt, ssid->key_mgmt);
  756. return -1;
  757. }
  758. #ifdef CONFIG_IEEE80211W
  759. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  760. ssid->ieee80211w == MGMT_FRAME_PROTECTION_REQUIRED) {
  761. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  762. "that does not support management frame protection - "
  763. "reject");
  764. return -1;
  765. }
  766. #endif /* CONFIG_IEEE80211W */
  767. return 0;
  768. }
  769. /**
  770. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  771. * @wpa_s: Pointer to wpa_supplicant data
  772. * @bss: Scan results for the selected BSS, or %NULL if not available
  773. * @ssid: Configuration data for the selected network
  774. * @wpa_ie: Buffer for the WPA/RSN IE
  775. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  776. * used buffer length in case the functions returns success.
  777. * Returns: 0 on success or -1 on failure
  778. *
  779. * This function is used to configure authentication and encryption parameters
  780. * based on the network configuration and scan result for the selected BSS (if
  781. * available).
  782. */
  783. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  784. struct wpa_bss *bss, struct wpa_ssid *ssid,
  785. u8 *wpa_ie, size_t *wpa_ie_len)
  786. {
  787. struct wpa_ie_data ie;
  788. int sel, proto;
  789. const u8 *bss_wpa, *bss_rsn;
  790. if (bss) {
  791. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  792. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  793. } else
  794. bss_wpa = bss_rsn = NULL;
  795. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  796. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  797. (ie.group_cipher & ssid->group_cipher) &&
  798. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  799. (ie.key_mgmt & ssid->key_mgmt)) {
  800. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  801. proto = WPA_PROTO_RSN;
  802. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  803. wpa_parse_wpa_ie(bss_wpa, 2 +bss_wpa[1], &ie) == 0 &&
  804. (ie.group_cipher & ssid->group_cipher) &&
  805. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  806. (ie.key_mgmt & ssid->key_mgmt)) {
  807. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  808. proto = WPA_PROTO_WPA;
  809. } else if (bss) {
  810. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  811. return -1;
  812. } else {
  813. if (ssid->proto & WPA_PROTO_RSN)
  814. proto = WPA_PROTO_RSN;
  815. else
  816. proto = WPA_PROTO_WPA;
  817. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  818. os_memset(&ie, 0, sizeof(ie));
  819. ie.group_cipher = ssid->group_cipher;
  820. ie.pairwise_cipher = ssid->pairwise_cipher;
  821. ie.key_mgmt = ssid->key_mgmt;
  822. #ifdef CONFIG_IEEE80211W
  823. ie.mgmt_group_cipher =
  824. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  825. WPA_CIPHER_AES_128_CMAC : 0;
  826. #endif /* CONFIG_IEEE80211W */
  827. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  828. "based on configuration");
  829. } else
  830. proto = ie.proto;
  831. }
  832. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  833. "pairwise %d key_mgmt %d proto %d",
  834. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  835. #ifdef CONFIG_IEEE80211W
  836. if (ssid->ieee80211w) {
  837. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  838. ie.mgmt_group_cipher);
  839. }
  840. #endif /* CONFIG_IEEE80211W */
  841. wpa_s->wpa_proto = proto;
  842. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  843. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  844. !!(ssid->proto & WPA_PROTO_RSN));
  845. if (bss || !wpa_s->ap_ies_from_associnfo) {
  846. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  847. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  848. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  849. bss_rsn ? 2 + bss_rsn[1] : 0))
  850. return -1;
  851. }
  852. sel = ie.group_cipher & ssid->group_cipher;
  853. if (sel & WPA_CIPHER_CCMP) {
  854. wpa_s->group_cipher = WPA_CIPHER_CCMP;
  855. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK CCMP");
  856. } else if (sel & WPA_CIPHER_TKIP) {
  857. wpa_s->group_cipher = WPA_CIPHER_TKIP;
  858. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK TKIP");
  859. } else if (sel & WPA_CIPHER_WEP104) {
  860. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  861. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP104");
  862. } else if (sel & WPA_CIPHER_WEP40) {
  863. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  864. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK WEP40");
  865. } else {
  866. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  867. "cipher");
  868. return -1;
  869. }
  870. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  871. if (sel & WPA_CIPHER_CCMP) {
  872. wpa_s->pairwise_cipher = WPA_CIPHER_CCMP;
  873. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK CCMP");
  874. } else if (sel & WPA_CIPHER_TKIP) {
  875. wpa_s->pairwise_cipher = WPA_CIPHER_TKIP;
  876. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK TKIP");
  877. } else if (sel & WPA_CIPHER_NONE) {
  878. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  879. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK NONE");
  880. } else {
  881. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  882. "cipher");
  883. return -1;
  884. }
  885. sel = ie.key_mgmt & ssid->key_mgmt;
  886. if (0) {
  887. #ifdef CONFIG_IEEE80211R
  888. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  889. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  890. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  891. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  892. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  893. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  894. #endif /* CONFIG_IEEE80211R */
  895. #ifdef CONFIG_IEEE80211W
  896. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  897. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  898. wpa_dbg(wpa_s, MSG_DEBUG,
  899. "WPA: using KEY_MGMT 802.1X with SHA256");
  900. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  901. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  902. wpa_dbg(wpa_s, MSG_DEBUG,
  903. "WPA: using KEY_MGMT PSK with SHA256");
  904. #endif /* CONFIG_IEEE80211W */
  905. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  906. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  907. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  908. } else if (sel & WPA_KEY_MGMT_PSK) {
  909. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  910. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  911. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  912. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  913. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  914. } else {
  915. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  916. "authenticated key management type");
  917. return -1;
  918. }
  919. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  920. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  921. wpa_s->pairwise_cipher);
  922. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  923. #ifdef CONFIG_IEEE80211W
  924. sel = ie.mgmt_group_cipher;
  925. if (ssid->ieee80211w == NO_MGMT_FRAME_PROTECTION ||
  926. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  927. sel = 0;
  928. if (sel & WPA_CIPHER_AES_128_CMAC) {
  929. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  930. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  931. "AES-128-CMAC");
  932. } else {
  933. wpa_s->mgmt_group_cipher = 0;
  934. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  935. }
  936. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  937. wpa_s->mgmt_group_cipher);
  938. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP, ssid->ieee80211w);
  939. #endif /* CONFIG_IEEE80211W */
  940. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  941. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  942. return -1;
  943. }
  944. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
  945. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN);
  946. #ifndef CONFIG_NO_PBKDF2
  947. if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
  948. ssid->passphrase) {
  949. u8 psk[PMK_LEN];
  950. pbkdf2_sha1(ssid->passphrase, (char *) bss->ssid,
  951. bss->ssid_len, 4096, psk, PMK_LEN);
  952. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  953. psk, PMK_LEN);
  954. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN);
  955. }
  956. #endif /* CONFIG_NO_PBKDF2 */
  957. } else
  958. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  959. return 0;
  960. }
  961. /**
  962. * wpa_supplicant_associate - Request association
  963. * @wpa_s: Pointer to wpa_supplicant data
  964. * @bss: Scan results for the selected BSS, or %NULL if not available
  965. * @ssid: Configuration data for the selected network
  966. *
  967. * This function is used to request %wpa_supplicant to associate with a BSS.
  968. */
  969. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  970. struct wpa_bss *bss, struct wpa_ssid *ssid)
  971. {
  972. u8 wpa_ie[200];
  973. size_t wpa_ie_len;
  974. int use_crypt, ret, i, bssid_changed;
  975. int algs = WPA_AUTH_ALG_OPEN;
  976. enum wpa_cipher cipher_pairwise, cipher_group;
  977. struct wpa_driver_associate_params params;
  978. int wep_keys_set = 0;
  979. struct wpa_driver_capa capa;
  980. int assoc_failed = 0;
  981. struct wpa_ssid *old_ssid;
  982. #ifdef CONFIG_HT_OVERRIDES
  983. struct ieee80211_ht_capabilities htcaps;
  984. struct ieee80211_ht_capabilities htcaps_mask;
  985. #endif /* CONFIG_HT_OVERRIDES */
  986. #ifdef CONFIG_IBSS_RSN
  987. ibss_rsn_deinit(wpa_s->ibss_rsn);
  988. wpa_s->ibss_rsn = NULL;
  989. #endif /* CONFIG_IBSS_RSN */
  990. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  991. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  992. #ifdef CONFIG_AP
  993. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  994. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  995. "mode");
  996. return;
  997. }
  998. wpa_supplicant_create_ap(wpa_s, ssid);
  999. wpa_s->current_bss = bss;
  1000. #else /* CONFIG_AP */
  1001. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  1002. "the build");
  1003. #endif /* CONFIG_AP */
  1004. return;
  1005. }
  1006. #ifdef CONFIG_TDLS
  1007. if (bss)
  1008. wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
  1009. bss->ie_len);
  1010. #endif /* CONFIG_TDLS */
  1011. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1012. ssid->mode == IEEE80211_MODE_INFRA) {
  1013. sme_authenticate(wpa_s, bss, ssid);
  1014. return;
  1015. }
  1016. os_memset(&params, 0, sizeof(params));
  1017. wpa_s->reassociate = 0;
  1018. if (bss && !wpas_driver_bss_selection(wpa_s)) {
  1019. #ifdef CONFIG_IEEE80211R
  1020. const u8 *ie, *md = NULL;
  1021. #endif /* CONFIG_IEEE80211R */
  1022. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1023. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  1024. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  1025. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1026. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1027. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1028. if (bssid_changed)
  1029. wpas_notify_bssid_changed(wpa_s);
  1030. #ifdef CONFIG_IEEE80211R
  1031. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  1032. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  1033. md = ie + 2;
  1034. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  1035. if (md) {
  1036. /* Prepare for the next transition */
  1037. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  1038. }
  1039. #endif /* CONFIG_IEEE80211R */
  1040. #ifdef CONFIG_WPS
  1041. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  1042. wpa_s->conf->ap_scan == 2 &&
  1043. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  1044. /* Use ap_scan==1 style network selection to find the network
  1045. */
  1046. wpa_s->scan_req = 2;
  1047. wpa_s->reassociate = 1;
  1048. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1049. return;
  1050. #endif /* CONFIG_WPS */
  1051. } else {
  1052. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  1053. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1054. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1055. }
  1056. wpa_supplicant_cancel_sched_scan(wpa_s);
  1057. wpa_supplicant_cancel_scan(wpa_s);
  1058. /* Starting new association, so clear the possibly used WPA IE from the
  1059. * previous association. */
  1060. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1061. #ifdef IEEE8021X_EAPOL
  1062. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1063. if (ssid->leap) {
  1064. if (ssid->non_leap == 0)
  1065. algs = WPA_AUTH_ALG_LEAP;
  1066. else
  1067. algs |= WPA_AUTH_ALG_LEAP;
  1068. }
  1069. }
  1070. #endif /* IEEE8021X_EAPOL */
  1071. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  1072. if (ssid->auth_alg) {
  1073. algs = ssid->auth_alg;
  1074. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  1075. "0x%x", algs);
  1076. }
  1077. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  1078. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  1079. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  1080. int try_opportunistic;
  1081. try_opportunistic = ssid->proactive_key_caching &&
  1082. (ssid->proto & WPA_PROTO_RSN);
  1083. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1084. wpa_s->current_ssid,
  1085. try_opportunistic) == 0)
  1086. eapol_sm_notify_pmkid_attempt(wpa_s->eapol, 1);
  1087. wpa_ie_len = sizeof(wpa_ie);
  1088. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1089. wpa_ie, &wpa_ie_len)) {
  1090. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1091. "key management and encryption suites");
  1092. return;
  1093. }
  1094. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  1095. wpa_ie_len = sizeof(wpa_ie);
  1096. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  1097. wpa_ie, &wpa_ie_len)) {
  1098. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1099. "key management and encryption suites (no "
  1100. "scan results)");
  1101. return;
  1102. }
  1103. #ifdef CONFIG_WPS
  1104. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  1105. struct wpabuf *wps_ie;
  1106. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  1107. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  1108. wpa_ie_len = wpabuf_len(wps_ie);
  1109. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  1110. } else
  1111. wpa_ie_len = 0;
  1112. wpabuf_free(wps_ie);
  1113. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1114. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  1115. params.wps = WPS_MODE_PRIVACY;
  1116. else
  1117. params.wps = WPS_MODE_OPEN;
  1118. wpa_s->wpa_proto = 0;
  1119. #endif /* CONFIG_WPS */
  1120. } else {
  1121. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  1122. wpa_ie_len = 0;
  1123. wpa_s->wpa_proto = 0;
  1124. }
  1125. #ifdef CONFIG_P2P
  1126. if (wpa_s->global->p2p) {
  1127. u8 *pos;
  1128. size_t len;
  1129. int res;
  1130. int p2p_group;
  1131. p2p_group = wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE;
  1132. pos = wpa_ie + wpa_ie_len;
  1133. len = sizeof(wpa_ie) - wpa_ie_len;
  1134. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len, p2p_group);
  1135. if (res >= 0)
  1136. wpa_ie_len += res;
  1137. }
  1138. wpa_s->cross_connect_disallowed = 0;
  1139. if (bss) {
  1140. struct wpabuf *p2p;
  1141. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  1142. if (p2p) {
  1143. wpa_s->cross_connect_disallowed =
  1144. p2p_get_cross_connect_disallowed(p2p);
  1145. wpabuf_free(p2p);
  1146. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  1147. "connection",
  1148. wpa_s->cross_connect_disallowed ?
  1149. "disallows" : "allows");
  1150. }
  1151. }
  1152. #endif /* CONFIG_P2P */
  1153. #ifdef CONFIG_INTERWORKING
  1154. if (wpa_s->conf->interworking) {
  1155. u8 *pos = wpa_ie;
  1156. if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  1157. pos += 2 + pos[1];
  1158. os_memmove(pos + 6, pos, wpa_ie_len - (pos - wpa_ie));
  1159. wpa_ie_len += 6;
  1160. *pos++ = WLAN_EID_EXT_CAPAB;
  1161. *pos++ = 4;
  1162. *pos++ = 0x00;
  1163. *pos++ = 0x00;
  1164. *pos++ = 0x00;
  1165. *pos++ = 0x80; /* Bit 31 - Interworking */
  1166. }
  1167. #endif /* CONFIG_INTERWORKING */
  1168. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  1169. use_crypt = 1;
  1170. cipher_pairwise = cipher_suite2driver(wpa_s->pairwise_cipher);
  1171. cipher_group = cipher_suite2driver(wpa_s->group_cipher);
  1172. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  1173. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1174. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  1175. use_crypt = 0;
  1176. if (wpa_set_wep_keys(wpa_s, ssid)) {
  1177. use_crypt = 1;
  1178. wep_keys_set = 1;
  1179. }
  1180. }
  1181. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  1182. use_crypt = 0;
  1183. #ifdef IEEE8021X_EAPOL
  1184. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1185. if ((ssid->eapol_flags &
  1186. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  1187. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  1188. !wep_keys_set) {
  1189. use_crypt = 0;
  1190. } else {
  1191. /* Assume that dynamic WEP-104 keys will be used and
  1192. * set cipher suites in order for drivers to expect
  1193. * encryption. */
  1194. cipher_pairwise = cipher_group = CIPHER_WEP104;
  1195. }
  1196. }
  1197. #endif /* IEEE8021X_EAPOL */
  1198. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1199. /* Set the key before (and later after) association */
  1200. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1201. }
  1202. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  1203. if (bss) {
  1204. params.ssid = bss->ssid;
  1205. params.ssid_len = bss->ssid_len;
  1206. if (!wpas_driver_bss_selection(wpa_s)) {
  1207. params.bssid = bss->bssid;
  1208. params.freq = bss->freq;
  1209. }
  1210. } else {
  1211. params.ssid = ssid->ssid;
  1212. params.ssid_len = ssid->ssid_len;
  1213. }
  1214. if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
  1215. wpa_s->conf->ap_scan == 2) {
  1216. params.bssid = ssid->bssid;
  1217. params.fixed_bssid = 1;
  1218. }
  1219. if (ssid->mode == WPAS_MODE_IBSS && ssid->frequency > 0 &&
  1220. params.freq == 0)
  1221. params.freq = ssid->frequency; /* Initial channel for IBSS */
  1222. params.wpa_ie = wpa_ie;
  1223. params.wpa_ie_len = wpa_ie_len;
  1224. params.pairwise_suite = cipher_pairwise;
  1225. params.group_suite = cipher_group;
  1226. params.key_mgmt_suite = key_mgmt2driver(wpa_s->key_mgmt);
  1227. params.wpa_proto = wpa_s->wpa_proto;
  1228. params.auth_alg = algs;
  1229. params.mode = ssid->mode;
  1230. for (i = 0; i < NUM_WEP_KEYS; i++) {
  1231. if (ssid->wep_key_len[i])
  1232. params.wep_key[i] = ssid->wep_key[i];
  1233. params.wep_key_len[i] = ssid->wep_key_len[i];
  1234. }
  1235. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  1236. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  1237. (params.key_mgmt_suite == KEY_MGMT_PSK ||
  1238. params.key_mgmt_suite == KEY_MGMT_FT_PSK)) {
  1239. params.passphrase = ssid->passphrase;
  1240. if (ssid->psk_set)
  1241. params.psk = ssid->psk;
  1242. }
  1243. params.drop_unencrypted = use_crypt;
  1244. #ifdef CONFIG_IEEE80211W
  1245. params.mgmt_frame_protection = ssid->ieee80211w;
  1246. if (ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION && bss) {
  1247. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1248. struct wpa_ie_data ie;
  1249. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  1250. ie.capabilities &
  1251. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  1252. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  1253. "MFP: require MFP");
  1254. params.mgmt_frame_protection =
  1255. MGMT_FRAME_PROTECTION_REQUIRED;
  1256. }
  1257. }
  1258. #endif /* CONFIG_IEEE80211W */
  1259. params.p2p = ssid->p2p_group;
  1260. if (wpa_s->parent->set_sta_uapsd)
  1261. params.uapsd = wpa_s->parent->sta_uapsd;
  1262. else
  1263. params.uapsd = -1;
  1264. #ifdef CONFIG_HT_OVERRIDES
  1265. os_memset(&htcaps, 0, sizeof(htcaps));
  1266. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  1267. params.htcaps = (u8 *) &htcaps;
  1268. params.htcaps_mask = (u8 *) &htcaps_mask;
  1269. wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
  1270. #endif /* CONFIG_HT_OVERRIDES */
  1271. ret = wpa_drv_associate(wpa_s, &params);
  1272. if (ret < 0) {
  1273. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  1274. "failed");
  1275. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  1276. /*
  1277. * The driver is known to mean what is saying, so we
  1278. * can stop right here; the association will not
  1279. * succeed.
  1280. */
  1281. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  1282. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1283. return;
  1284. }
  1285. /* try to continue anyway; new association will be tried again
  1286. * after timeout */
  1287. assoc_failed = 1;
  1288. }
  1289. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  1290. /* Set the key after the association just in case association
  1291. * cleared the previously configured key. */
  1292. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  1293. /* No need to timeout authentication since there is no key
  1294. * management. */
  1295. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1296. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  1297. #ifdef CONFIG_IBSS_RSN
  1298. } else if (ssid->mode == WPAS_MODE_IBSS &&
  1299. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  1300. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  1301. /*
  1302. * RSN IBSS authentication is per-STA and we can disable the
  1303. * per-BSSID authentication.
  1304. */
  1305. wpa_supplicant_cancel_auth_timeout(wpa_s);
  1306. #endif /* CONFIG_IBSS_RSN */
  1307. } else {
  1308. /* Timeout for IEEE 802.11 authentication and association */
  1309. int timeout = 60;
  1310. if (assoc_failed) {
  1311. /* give IBSS a bit more time */
  1312. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  1313. } else if (wpa_s->conf->ap_scan == 1) {
  1314. /* give IBSS a bit more time */
  1315. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  1316. }
  1317. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  1318. }
  1319. if (wep_keys_set && wpa_drv_get_capa(wpa_s, &capa) == 0 &&
  1320. capa.flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC) {
  1321. /* Set static WEP keys again */
  1322. wpa_set_wep_keys(wpa_s, ssid);
  1323. }
  1324. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  1325. /*
  1326. * Do not allow EAP session resumption between different
  1327. * network configurations.
  1328. */
  1329. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1330. }
  1331. old_ssid = wpa_s->current_ssid;
  1332. wpa_s->current_ssid = ssid;
  1333. wpa_s->current_bss = bss;
  1334. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  1335. wpa_supplicant_initiate_eapol(wpa_s);
  1336. if (old_ssid != wpa_s->current_ssid)
  1337. wpas_notify_network_changed(wpa_s);
  1338. }
  1339. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  1340. const u8 *addr)
  1341. {
  1342. struct wpa_ssid *old_ssid;
  1343. wpa_clear_keys(wpa_s, addr);
  1344. wpa_supplicant_mark_disassoc(wpa_s);
  1345. old_ssid = wpa_s->current_ssid;
  1346. wpa_s->current_ssid = NULL;
  1347. wpa_s->current_bss = NULL;
  1348. wpa_sm_set_config(wpa_s->wpa, NULL);
  1349. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1350. if (old_ssid != wpa_s->current_ssid)
  1351. wpas_notify_network_changed(wpa_s);
  1352. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  1353. }
  1354. /**
  1355. * wpa_supplicant_disassociate - Disassociate the current connection
  1356. * @wpa_s: Pointer to wpa_supplicant data
  1357. * @reason_code: IEEE 802.11 reason code for the disassociate frame
  1358. *
  1359. * This function is used to request %wpa_supplicant to disassociate with the
  1360. * current AP.
  1361. */
  1362. void wpa_supplicant_disassociate(struct wpa_supplicant *wpa_s,
  1363. int reason_code)
  1364. {
  1365. u8 *addr = NULL;
  1366. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1367. wpa_drv_disassociate(wpa_s, wpa_s->bssid, reason_code);
  1368. addr = wpa_s->bssid;
  1369. }
  1370. wpa_supplicant_clear_connection(wpa_s, addr);
  1371. }
  1372. /**
  1373. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  1374. * @wpa_s: Pointer to wpa_supplicant data
  1375. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  1376. *
  1377. * This function is used to request %wpa_supplicant to deauthenticate from the
  1378. * current AP.
  1379. */
  1380. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  1381. int reason_code)
  1382. {
  1383. u8 *addr = NULL;
  1384. if (!is_zero_ether_addr(wpa_s->bssid)) {
  1385. wpa_drv_deauthenticate(wpa_s, wpa_s->bssid, reason_code);
  1386. addr = wpa_s->bssid;
  1387. }
  1388. wpa_supplicant_clear_connection(wpa_s, addr);
  1389. }
  1390. /**
  1391. * wpa_supplicant_enable_network - Mark a configured network as enabled
  1392. * @wpa_s: wpa_supplicant structure for a network interface
  1393. * @ssid: wpa_ssid structure for a configured network or %NULL
  1394. *
  1395. * Enables the specified network or all networks if no network specified.
  1396. */
  1397. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  1398. struct wpa_ssid *ssid)
  1399. {
  1400. struct wpa_ssid *other_ssid;
  1401. int was_disabled;
  1402. if (ssid == NULL) {
  1403. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1404. other_ssid = other_ssid->next) {
  1405. if (other_ssid->disabled == 2)
  1406. continue; /* do not change persistent P2P group
  1407. * data */
  1408. if (other_ssid == wpa_s->current_ssid &&
  1409. other_ssid->disabled)
  1410. wpa_s->reassociate = 1;
  1411. was_disabled = other_ssid->disabled;
  1412. other_ssid->disabled = 0;
  1413. if (was_disabled != other_ssid->disabled)
  1414. wpas_notify_network_enabled_changed(
  1415. wpa_s, other_ssid);
  1416. }
  1417. if (wpa_s->reassociate)
  1418. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1419. } else if (ssid->disabled && ssid->disabled != 2) {
  1420. if (wpa_s->current_ssid == NULL) {
  1421. /*
  1422. * Try to reassociate since there is no current
  1423. * configuration and a new network was made available.
  1424. */
  1425. wpa_s->reassociate = 1;
  1426. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1427. }
  1428. was_disabled = ssid->disabled;
  1429. ssid->disabled = 0;
  1430. if (was_disabled != ssid->disabled)
  1431. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1432. }
  1433. }
  1434. /**
  1435. * wpa_supplicant_disable_network - Mark a configured network as disabled
  1436. * @wpa_s: wpa_supplicant structure for a network interface
  1437. * @ssid: wpa_ssid structure for a configured network or %NULL
  1438. *
  1439. * Disables the specified network or all networks if no network specified.
  1440. */
  1441. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  1442. struct wpa_ssid *ssid)
  1443. {
  1444. struct wpa_ssid *other_ssid;
  1445. int was_disabled;
  1446. if (ssid == NULL) {
  1447. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1448. other_ssid = other_ssid->next) {
  1449. was_disabled = other_ssid->disabled;
  1450. if (was_disabled == 2)
  1451. continue; /* do not change persistent P2P group
  1452. * data */
  1453. other_ssid->disabled = 1;
  1454. if (was_disabled != other_ssid->disabled)
  1455. wpas_notify_network_enabled_changed(
  1456. wpa_s, other_ssid);
  1457. }
  1458. if (wpa_s->current_ssid)
  1459. wpa_supplicant_disassociate(
  1460. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1461. } else if (ssid->disabled != 2) {
  1462. if (ssid == wpa_s->current_ssid)
  1463. wpa_supplicant_disassociate(
  1464. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1465. was_disabled = ssid->disabled;
  1466. ssid->disabled = 1;
  1467. if (was_disabled != ssid->disabled)
  1468. wpas_notify_network_enabled_changed(wpa_s, ssid);
  1469. }
  1470. }
  1471. /**
  1472. * wpa_supplicant_select_network - Attempt association with a network
  1473. * @wpa_s: wpa_supplicant structure for a network interface
  1474. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  1475. */
  1476. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  1477. struct wpa_ssid *ssid)
  1478. {
  1479. struct wpa_ssid *other_ssid;
  1480. int disconnected = 0;
  1481. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
  1482. wpa_supplicant_disassociate(
  1483. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1484. disconnected = 1;
  1485. }
  1486. /*
  1487. * Mark all other networks disabled or mark all networks enabled if no
  1488. * network specified.
  1489. */
  1490. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  1491. other_ssid = other_ssid->next) {
  1492. int was_disabled = other_ssid->disabled;
  1493. if (was_disabled == 2)
  1494. continue; /* do not change persistent P2P group data */
  1495. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  1496. if (was_disabled != other_ssid->disabled)
  1497. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  1498. }
  1499. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid) {
  1500. /* We are already associated with the selected network */
  1501. wpa_printf(MSG_DEBUG, "Already associated with the "
  1502. "selected network - do nothing");
  1503. return;
  1504. }
  1505. if (ssid)
  1506. wpa_s->current_ssid = ssid;
  1507. wpa_s->connect_without_scan = NULL;
  1508. wpa_s->disconnected = 0;
  1509. wpa_s->reassociate = 1;
  1510. wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
  1511. if (ssid)
  1512. wpas_notify_network_selected(wpa_s, ssid);
  1513. }
  1514. /**
  1515. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  1516. * @wpa_s: wpa_supplicant structure for a network interface
  1517. * @ap_scan: AP scan mode
  1518. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  1519. *
  1520. */
  1521. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  1522. {
  1523. int old_ap_scan;
  1524. if (ap_scan < 0 || ap_scan > 2)
  1525. return -1;
  1526. #ifdef ANDROID
  1527. if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
  1528. wpa_s->wpa_state >= WPA_ASSOCIATING &&
  1529. wpa_s->wpa_state < WPA_COMPLETED) {
  1530. wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
  1531. "associating", wpa_s->conf->ap_scan, ap_scan);
  1532. return 0;
  1533. }
  1534. #endif /* ANDROID */
  1535. old_ap_scan = wpa_s->conf->ap_scan;
  1536. wpa_s->conf->ap_scan = ap_scan;
  1537. if (old_ap_scan != wpa_s->conf->ap_scan)
  1538. wpas_notify_ap_scan_changed(wpa_s);
  1539. return 0;
  1540. }
  1541. /**
  1542. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  1543. * @wpa_s: wpa_supplicant structure for a network interface
  1544. * @expire_age: Expiration age in seconds
  1545. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  1546. *
  1547. */
  1548. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  1549. unsigned int bss_expire_age)
  1550. {
  1551. if (bss_expire_age < 10) {
  1552. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  1553. bss_expire_age);
  1554. return -1;
  1555. }
  1556. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  1557. bss_expire_age);
  1558. wpa_s->conf->bss_expiration_age = bss_expire_age;
  1559. return 0;
  1560. }
  1561. /**
  1562. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  1563. * @wpa_s: wpa_supplicant structure for a network interface
  1564. * @expire_count: number of scans after which an unseen BSS is reclaimed
  1565. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  1566. *
  1567. */
  1568. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  1569. unsigned int bss_expire_count)
  1570. {
  1571. if (bss_expire_count < 1) {
  1572. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  1573. bss_expire_count);
  1574. return -1;
  1575. }
  1576. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  1577. bss_expire_count);
  1578. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  1579. return 0;
  1580. }
  1581. /**
  1582. * wpa_supplicant_set_debug_params - Set global debug params
  1583. * @global: wpa_global structure
  1584. * @debug_level: debug level
  1585. * @debug_timestamp: determines if show timestamp in debug data
  1586. * @debug_show_keys: determines if show keys in debug data
  1587. * Returns: 0 if succeed or -1 if debug_level has wrong value
  1588. */
  1589. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  1590. int debug_timestamp, int debug_show_keys)
  1591. {
  1592. int old_level, old_timestamp, old_show_keys;
  1593. /* check for allowed debuglevels */
  1594. if (debug_level != MSG_EXCESSIVE &&
  1595. debug_level != MSG_MSGDUMP &&
  1596. debug_level != MSG_DEBUG &&
  1597. debug_level != MSG_INFO &&
  1598. debug_level != MSG_WARNING &&
  1599. debug_level != MSG_ERROR)
  1600. return -1;
  1601. old_level = wpa_debug_level;
  1602. old_timestamp = wpa_debug_timestamp;
  1603. old_show_keys = wpa_debug_show_keys;
  1604. wpa_debug_level = debug_level;
  1605. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  1606. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  1607. if (wpa_debug_level != old_level)
  1608. wpas_notify_debug_level_changed(global);
  1609. if (wpa_debug_timestamp != old_timestamp)
  1610. wpas_notify_debug_timestamp_changed(global);
  1611. if (wpa_debug_show_keys != old_show_keys)
  1612. wpas_notify_debug_show_keys_changed(global);
  1613. return 0;
  1614. }
  1615. /**
  1616. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  1617. * @wpa_s: Pointer to wpa_supplicant data
  1618. * Returns: A pointer to the current network structure or %NULL on failure
  1619. */
  1620. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  1621. {
  1622. struct wpa_ssid *entry;
  1623. u8 ssid[MAX_SSID_LEN];
  1624. int res;
  1625. size_t ssid_len;
  1626. u8 bssid[ETH_ALEN];
  1627. int wired;
  1628. res = wpa_drv_get_ssid(wpa_s, ssid);
  1629. if (res < 0) {
  1630. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  1631. "driver");
  1632. return NULL;
  1633. }
  1634. ssid_len = res;
  1635. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  1636. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  1637. "driver");
  1638. return NULL;
  1639. }
  1640. wired = wpa_s->conf->ap_scan == 0 &&
  1641. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  1642. entry = wpa_s->conf->ssid;
  1643. while (entry) {
  1644. if (!entry->disabled &&
  1645. ((ssid_len == entry->ssid_len &&
  1646. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  1647. (!entry->bssid_set ||
  1648. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1649. return entry;
  1650. #ifdef CONFIG_WPS
  1651. if (!entry->disabled &&
  1652. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  1653. (entry->ssid == NULL || entry->ssid_len == 0) &&
  1654. (!entry->bssid_set ||
  1655. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  1656. return entry;
  1657. #endif /* CONFIG_WPS */
  1658. if (!entry->disabled && entry->bssid_set &&
  1659. entry->ssid_len == 0 &&
  1660. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  1661. return entry;
  1662. entry = entry->next;
  1663. }
  1664. return NULL;
  1665. }
  1666. static int select_driver(struct wpa_supplicant *wpa_s, int i)
  1667. {
  1668. struct wpa_global *global = wpa_s->global;
  1669. if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
  1670. global->drv_priv[i] = wpa_drivers[i]->global_init();
  1671. if (global->drv_priv[i] == NULL) {
  1672. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  1673. "'%s'", wpa_drivers[i]->name);
  1674. return -1;
  1675. }
  1676. }
  1677. wpa_s->driver = wpa_drivers[i];
  1678. wpa_s->global_drv_priv = global->drv_priv[i];
  1679. return 0;
  1680. }
  1681. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  1682. const char *name)
  1683. {
  1684. int i;
  1685. size_t len;
  1686. const char *pos, *driver = name;
  1687. if (wpa_s == NULL)
  1688. return -1;
  1689. if (wpa_drivers[0] == NULL) {
  1690. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  1691. "wpa_supplicant");
  1692. return -1;
  1693. }
  1694. if (name == NULL) {
  1695. /* default to first driver in the list */
  1696. return select_driver(wpa_s, 0);
  1697. }
  1698. do {
  1699. pos = os_strchr(driver, ',');
  1700. if (pos)
  1701. len = pos - driver;
  1702. else
  1703. len = os_strlen(driver);
  1704. for (i = 0; wpa_drivers[i]; i++) {
  1705. if (os_strlen(wpa_drivers[i]->name) == len &&
  1706. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  1707. 0)
  1708. return select_driver(wpa_s, i);
  1709. }
  1710. driver = pos + 1;
  1711. } while (pos);
  1712. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  1713. return -1;
  1714. }
  1715. /**
  1716. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  1717. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  1718. * with struct wpa_driver_ops::init()
  1719. * @src_addr: Source address of the EAPOL frame
  1720. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  1721. * @len: Length of the EAPOL data
  1722. *
  1723. * This function is called for each received EAPOL frame. Most driver
  1724. * interfaces rely on more generic OS mechanism for receiving frames through
  1725. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  1726. * take care of received EAPOL frames and deliver them to the core supplicant
  1727. * code by calling this function.
  1728. */
  1729. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  1730. const u8 *buf, size_t len)
  1731. {
  1732. struct wpa_supplicant *wpa_s = ctx;
  1733. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  1734. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  1735. if (wpa_s->wpa_state < WPA_ASSOCIATED) {
  1736. /*
  1737. * There is possible race condition between receiving the
  1738. * association event and the EAPOL frame since they are coming
  1739. * through different paths from the driver. In order to avoid
  1740. * issues in trying to process the EAPOL frame before receiving
  1741. * association information, lets queue it for processing until
  1742. * the association event is received.
  1743. */
  1744. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  1745. "of received EAPOL frame");
  1746. wpabuf_free(wpa_s->pending_eapol_rx);
  1747. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  1748. if (wpa_s->pending_eapol_rx) {
  1749. os_get_time(&wpa_s->pending_eapol_rx_time);
  1750. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  1751. ETH_ALEN);
  1752. }
  1753. return;
  1754. }
  1755. #ifdef CONFIG_AP
  1756. if (wpa_s->ap_iface) {
  1757. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  1758. return;
  1759. }
  1760. #endif /* CONFIG_AP */
  1761. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  1762. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  1763. "no key management is configured");
  1764. return;
  1765. }
  1766. if (wpa_s->eapol_received == 0 &&
  1767. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  1768. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  1769. wpa_s->wpa_state != WPA_COMPLETED) &&
  1770. (wpa_s->current_ssid == NULL ||
  1771. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  1772. /* Timeout for completing IEEE 802.1X and WPA authentication */
  1773. wpa_supplicant_req_auth_timeout(
  1774. wpa_s,
  1775. (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1776. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  1777. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) ?
  1778. 70 : 10, 0);
  1779. }
  1780. wpa_s->eapol_received++;
  1781. if (wpa_s->countermeasures) {
  1782. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  1783. "EAPOL packet");
  1784. return;
  1785. }
  1786. #ifdef CONFIG_IBSS_RSN
  1787. if (wpa_s->current_ssid &&
  1788. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  1789. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  1790. return;
  1791. }
  1792. #endif /* CONFIG_IBSS_RSN */
  1793. /* Source address of the incoming EAPOL frame could be compared to the
  1794. * current BSSID. However, it is possible that a centralized
  1795. * Authenticator could be using another MAC address than the BSSID of
  1796. * an AP, so just allow any address to be used for now. The replies are
  1797. * still sent to the current BSSID (if available), though. */
  1798. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  1799. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  1800. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  1801. return;
  1802. wpa_drv_poll(wpa_s);
  1803. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  1804. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  1805. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1806. /*
  1807. * Set portValid = TRUE here since we are going to skip 4-way
  1808. * handshake processing which would normally set portValid. We
  1809. * need this to allow the EAPOL state machines to be completed
  1810. * without going through EAPOL-Key handshake.
  1811. */
  1812. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  1813. }
  1814. }
  1815. int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
  1816. {
  1817. if (wpa_s->driver->send_eapol) {
  1818. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1819. if (addr)
  1820. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1821. } else if (!(wpa_s->drv_flags &
  1822. WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  1823. l2_packet_deinit(wpa_s->l2);
  1824. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  1825. wpa_drv_get_mac_addr(wpa_s),
  1826. ETH_P_EAPOL,
  1827. wpa_supplicant_rx_eapol, wpa_s, 0);
  1828. if (wpa_s->l2 == NULL)
  1829. return -1;
  1830. } else {
  1831. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  1832. if (addr)
  1833. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  1834. }
  1835. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  1836. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  1837. return -1;
  1838. }
  1839. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  1840. MAC2STR(wpa_s->own_addr));
  1841. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  1842. return 0;
  1843. }
  1844. /**
  1845. * wpa_supplicant_driver_init - Initialize driver interface parameters
  1846. * @wpa_s: Pointer to wpa_supplicant data
  1847. * Returns: 0 on success, -1 on failure
  1848. *
  1849. * This function is called to initialize driver interface parameters.
  1850. * wpa_drv_init() must have been called before this function to initialize the
  1851. * driver interface.
  1852. */
  1853. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  1854. {
  1855. static int interface_count = 0;
  1856. if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
  1857. return -1;
  1858. if (wpa_s->bridge_ifname[0]) {
  1859. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  1860. "interface '%s'", wpa_s->bridge_ifname);
  1861. wpa_s->l2_br = l2_packet_init(wpa_s->bridge_ifname,
  1862. wpa_s->own_addr,
  1863. ETH_P_EAPOL,
  1864. wpa_supplicant_rx_eapol, wpa_s,
  1865. 0);
  1866. if (wpa_s->l2_br == NULL) {
  1867. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  1868. "connection for the bridge interface '%s'",
  1869. wpa_s->bridge_ifname);
  1870. return -1;
  1871. }
  1872. }
  1873. wpa_clear_keys(wpa_s, NULL);
  1874. /* Make sure that TKIP countermeasures are not left enabled (could
  1875. * happen if wpa_supplicant is killed during countermeasures. */
  1876. wpa_drv_set_countermeasures(wpa_s, 0);
  1877. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  1878. wpa_drv_flush_pmkid(wpa_s);
  1879. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  1880. wpa_s->prev_scan_wildcard = 0;
  1881. if (wpa_supplicant_enabled_networks(wpa_s->conf)) {
  1882. if (wpa_supplicant_delayed_sched_scan(wpa_s, interface_count,
  1883. 100000))
  1884. wpa_supplicant_req_scan(wpa_s, interface_count,
  1885. 100000);
  1886. interface_count++;
  1887. } else
  1888. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  1889. return 0;
  1890. }
  1891. static int wpa_supplicant_daemon(const char *pid_file)
  1892. {
  1893. wpa_printf(MSG_DEBUG, "Daemonize..");
  1894. return os_daemonize(pid_file);
  1895. }
  1896. static struct wpa_supplicant * wpa_supplicant_alloc(void)
  1897. {
  1898. struct wpa_supplicant *wpa_s;
  1899. wpa_s = os_zalloc(sizeof(*wpa_s));
  1900. if (wpa_s == NULL)
  1901. return NULL;
  1902. wpa_s->scan_req = 1;
  1903. wpa_s->scan_interval = 5;
  1904. wpa_s->new_connection = 1;
  1905. wpa_s->parent = wpa_s;
  1906. wpa_s->sched_scanning = 0;
  1907. return wpa_s;
  1908. }
  1909. #ifdef CONFIG_HT_OVERRIDES
  1910. static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
  1911. struct ieee80211_ht_capabilities *htcaps,
  1912. struct ieee80211_ht_capabilities *htcaps_mask,
  1913. const char *ht_mcs)
  1914. {
  1915. /* parse ht_mcs into hex array */
  1916. int i;
  1917. const char *tmp = ht_mcs;
  1918. char *end = NULL;
  1919. /* If ht_mcs is null, do not set anything */
  1920. if (!ht_mcs)
  1921. return 0;
  1922. /* This is what we are setting in the kernel */
  1923. os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
  1924. wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
  1925. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  1926. errno = 0;
  1927. long v = strtol(tmp, &end, 16);
  1928. if (errno == 0) {
  1929. wpa_msg(wpa_s, MSG_DEBUG,
  1930. "htcap value[%i]: %ld end: %p tmp: %p",
  1931. i, v, end, tmp);
  1932. if (end == tmp)
  1933. break;
  1934. htcaps->supported_mcs_set[i] = v;
  1935. tmp = end;
  1936. } else {
  1937. wpa_msg(wpa_s, MSG_ERROR,
  1938. "Failed to parse ht-mcs: %s, error: %s\n",
  1939. ht_mcs, strerror(errno));
  1940. return -1;
  1941. }
  1942. }
  1943. /*
  1944. * If we were able to parse any values, then set mask for the MCS set.
  1945. */
  1946. if (i) {
  1947. os_memset(&htcaps_mask->supported_mcs_set, 0xff,
  1948. IEEE80211_HT_MCS_MASK_LEN - 1);
  1949. /* skip the 3 reserved bits */
  1950. htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
  1951. 0x1f;
  1952. }
  1953. return 0;
  1954. }
  1955. static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
  1956. struct ieee80211_ht_capabilities *htcaps,
  1957. struct ieee80211_ht_capabilities *htcaps_mask,
  1958. int disabled)
  1959. {
  1960. u16 msk;
  1961. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
  1962. if (disabled == -1)
  1963. return 0;
  1964. msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
  1965. htcaps_mask->ht_capabilities_info |= msk;
  1966. if (disabled)
  1967. htcaps->ht_capabilities_info &= msk;
  1968. else
  1969. htcaps->ht_capabilities_info |= msk;
  1970. return 0;
  1971. }
  1972. static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
  1973. struct ieee80211_ht_capabilities *htcaps,
  1974. struct ieee80211_ht_capabilities *htcaps_mask,
  1975. int factor)
  1976. {
  1977. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
  1978. if (factor == -1)
  1979. return 0;
  1980. if (factor < 0 || factor > 3) {
  1981. wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
  1982. "Must be 0-3 or -1", factor);
  1983. return -EINVAL;
  1984. }
  1985. htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
  1986. htcaps->a_mpdu_params &= ~0x3;
  1987. htcaps->a_mpdu_params |= factor & 0x3;
  1988. return 0;
  1989. }
  1990. static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
  1991. struct ieee80211_ht_capabilities *htcaps,
  1992. struct ieee80211_ht_capabilities *htcaps_mask,
  1993. int density)
  1994. {
  1995. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
  1996. if (density == -1)
  1997. return 0;
  1998. if (density < 0 || density > 7) {
  1999. wpa_msg(wpa_s, MSG_ERROR,
  2000. "ampdu_density: %d out of range. Must be 0-7 or -1.",
  2001. density);
  2002. return -EINVAL;
  2003. }
  2004. htcaps_mask->a_mpdu_params |= 0x1C;
  2005. htcaps->a_mpdu_params &= ~(0x1C);
  2006. htcaps->a_mpdu_params |= (density << 2) & 0x1C;
  2007. return 0;
  2008. }
  2009. static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
  2010. struct ieee80211_ht_capabilities *htcaps,
  2011. struct ieee80211_ht_capabilities *htcaps_mask,
  2012. int disabled)
  2013. {
  2014. /* Masking these out disables HT40 */
  2015. u16 msk = host_to_le16(HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET |
  2016. HT_CAP_INFO_SHORT_GI40MHZ);
  2017. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
  2018. if (disabled)
  2019. htcaps->ht_capabilities_info &= ~msk;
  2020. else
  2021. htcaps->ht_capabilities_info |= msk;
  2022. htcaps_mask->ht_capabilities_info |= msk;
  2023. return 0;
  2024. }
  2025. void wpa_supplicant_apply_ht_overrides(
  2026. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2027. struct wpa_driver_associate_params *params)
  2028. {
  2029. struct ieee80211_ht_capabilities *htcaps;
  2030. struct ieee80211_ht_capabilities *htcaps_mask;
  2031. if (!ssid)
  2032. return;
  2033. params->disable_ht = ssid->disable_ht;
  2034. if (!params->htcaps || !params->htcaps_mask)
  2035. return;
  2036. htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
  2037. htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
  2038. wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
  2039. wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
  2040. ssid->disable_max_amsdu);
  2041. wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
  2042. wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
  2043. wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
  2044. }
  2045. #endif /* CONFIG_HT_OVERRIDES */
  2046. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  2047. struct wpa_interface *iface)
  2048. {
  2049. const char *ifname, *driver;
  2050. struct wpa_driver_capa capa;
  2051. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  2052. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  2053. iface->confname ? iface->confname : "N/A",
  2054. iface->driver ? iface->driver : "default",
  2055. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  2056. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  2057. if (iface->confname) {
  2058. #ifdef CONFIG_BACKEND_FILE
  2059. wpa_s->confname = os_rel2abs_path(iface->confname);
  2060. if (wpa_s->confname == NULL) {
  2061. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  2062. "for configuration file '%s'.",
  2063. iface->confname);
  2064. return -1;
  2065. }
  2066. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  2067. iface->confname, wpa_s->confname);
  2068. #else /* CONFIG_BACKEND_FILE */
  2069. wpa_s->confname = os_strdup(iface->confname);
  2070. #endif /* CONFIG_BACKEND_FILE */
  2071. wpa_s->conf = wpa_config_read(wpa_s->confname);
  2072. if (wpa_s->conf == NULL) {
  2073. wpa_printf(MSG_ERROR, "Failed to read or parse "
  2074. "configuration '%s'.", wpa_s->confname);
  2075. return -1;
  2076. }
  2077. /*
  2078. * Override ctrl_interface and driver_param if set on command
  2079. * line.
  2080. */
  2081. if (iface->ctrl_interface) {
  2082. os_free(wpa_s->conf->ctrl_interface);
  2083. wpa_s->conf->ctrl_interface =
  2084. os_strdup(iface->ctrl_interface);
  2085. }
  2086. if (iface->driver_param) {
  2087. os_free(wpa_s->conf->driver_param);
  2088. wpa_s->conf->driver_param =
  2089. os_strdup(iface->driver_param);
  2090. }
  2091. } else
  2092. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  2093. iface->driver_param);
  2094. if (wpa_s->conf == NULL) {
  2095. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  2096. return -1;
  2097. }
  2098. if (iface->ifname == NULL) {
  2099. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  2100. return -1;
  2101. }
  2102. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  2103. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  2104. iface->ifname);
  2105. return -1;
  2106. }
  2107. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  2108. if (iface->bridge_ifname) {
  2109. if (os_strlen(iface->bridge_ifname) >=
  2110. sizeof(wpa_s->bridge_ifname)) {
  2111. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  2112. "name '%s'.", iface->bridge_ifname);
  2113. return -1;
  2114. }
  2115. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  2116. sizeof(wpa_s->bridge_ifname));
  2117. }
  2118. /* RSNA Supplicant Key Management - INITIALIZE */
  2119. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  2120. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  2121. /* Initialize driver interface and register driver event handler before
  2122. * L2 receive handler so that association events are processed before
  2123. * EAPOL-Key packets if both become available for the same select()
  2124. * call. */
  2125. driver = iface->driver;
  2126. next_driver:
  2127. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  2128. return -1;
  2129. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  2130. if (wpa_s->drv_priv == NULL) {
  2131. const char *pos;
  2132. pos = driver ? os_strchr(driver, ',') : NULL;
  2133. if (pos) {
  2134. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  2135. "driver interface - try next driver wrapper");
  2136. driver = pos + 1;
  2137. goto next_driver;
  2138. }
  2139. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  2140. "interface");
  2141. return -1;
  2142. }
  2143. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  2144. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  2145. "driver_param '%s'", wpa_s->conf->driver_param);
  2146. return -1;
  2147. }
  2148. ifname = wpa_drv_get_ifname(wpa_s);
  2149. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  2150. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  2151. "interface name with '%s'", ifname);
  2152. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  2153. }
  2154. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  2155. return -1;
  2156. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  2157. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  2158. NULL);
  2159. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  2160. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  2161. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  2162. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  2163. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2164. "dot11RSNAConfigPMKLifetime");
  2165. return -1;
  2166. }
  2167. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  2168. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  2169. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  2170. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2171. "dot11RSNAConfigPMKReauthThreshold");
  2172. return -1;
  2173. }
  2174. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  2175. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  2176. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  2177. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  2178. "dot11RSNAConfigSATimeout");
  2179. return -1;
  2180. }
  2181. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
  2182. &wpa_s->hw.num_modes,
  2183. &wpa_s->hw.flags);
  2184. if (wpa_drv_get_capa(wpa_s, &capa) == 0) {
  2185. wpa_s->drv_capa_known = 1;
  2186. wpa_s->drv_flags = capa.flags;
  2187. wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
  2188. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  2189. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  2190. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  2191. wpa_s->max_match_sets = capa.max_match_sets;
  2192. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  2193. wpa_s->max_stations = capa.max_stations;
  2194. }
  2195. if (wpa_s->max_remain_on_chan == 0)
  2196. wpa_s->max_remain_on_chan = 1000;
  2197. if (wpa_supplicant_driver_init(wpa_s) < 0)
  2198. return -1;
  2199. #ifdef CONFIG_TDLS
  2200. if (wpa_tdls_init(wpa_s->wpa))
  2201. return -1;
  2202. #endif /* CONFIG_TDLS */
  2203. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  2204. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  2205. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  2206. return -1;
  2207. }
  2208. if (wpas_wps_init(wpa_s))
  2209. return -1;
  2210. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  2211. return -1;
  2212. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2213. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  2214. if (wpa_s->ctrl_iface == NULL) {
  2215. wpa_printf(MSG_ERROR,
  2216. "Failed to initialize control interface '%s'.\n"
  2217. "You may have another wpa_supplicant process "
  2218. "already running or the file was\n"
  2219. "left by an unclean termination of wpa_supplicant "
  2220. "in which case you will need\n"
  2221. "to manually remove this file before starting "
  2222. "wpa_supplicant again.\n",
  2223. wpa_s->conf->ctrl_interface);
  2224. return -1;
  2225. }
  2226. wpa_s->gas = gas_query_init(wpa_s);
  2227. if (wpa_s->gas == NULL) {
  2228. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  2229. return -1;
  2230. }
  2231. #ifdef CONFIG_P2P
  2232. if (wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  2233. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  2234. return -1;
  2235. }
  2236. #endif /* CONFIG_P2P */
  2237. if (wpa_bss_init(wpa_s) < 0)
  2238. return -1;
  2239. return 0;
  2240. }
  2241. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  2242. int notify, int terminate)
  2243. {
  2244. if (wpa_s->drv_priv) {
  2245. wpa_supplicant_deauthenticate(wpa_s,
  2246. WLAN_REASON_DEAUTH_LEAVING);
  2247. wpa_drv_set_countermeasures(wpa_s, 0);
  2248. wpa_clear_keys(wpa_s, NULL);
  2249. }
  2250. wpa_supplicant_cleanup(wpa_s);
  2251. if (wpa_s->drv_priv)
  2252. wpa_drv_deinit(wpa_s);
  2253. if (notify)
  2254. wpas_notify_iface_removed(wpa_s);
  2255. if (terminate)
  2256. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
  2257. if (wpa_s->ctrl_iface) {
  2258. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  2259. wpa_s->ctrl_iface = NULL;
  2260. }
  2261. if (wpa_s->conf != NULL) {
  2262. wpa_config_free(wpa_s->conf);
  2263. wpa_s->conf = NULL;
  2264. }
  2265. }
  2266. /**
  2267. * wpa_supplicant_add_iface - Add a new network interface
  2268. * @global: Pointer to global data from wpa_supplicant_init()
  2269. * @iface: Interface configuration options
  2270. * Returns: Pointer to the created interface or %NULL on failure
  2271. *
  2272. * This function is used to add new network interfaces for %wpa_supplicant.
  2273. * This can be called before wpa_supplicant_run() to add interfaces before the
  2274. * main event loop has been started. In addition, new interfaces can be added
  2275. * dynamically while %wpa_supplicant is already running. This could happen,
  2276. * e.g., when a hotplug network adapter is inserted.
  2277. */
  2278. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  2279. struct wpa_interface *iface)
  2280. {
  2281. struct wpa_supplicant *wpa_s;
  2282. struct wpa_interface t_iface;
  2283. struct wpa_ssid *ssid;
  2284. if (global == NULL || iface == NULL)
  2285. return NULL;
  2286. wpa_s = wpa_supplicant_alloc();
  2287. if (wpa_s == NULL)
  2288. return NULL;
  2289. wpa_s->global = global;
  2290. t_iface = *iface;
  2291. if (global->params.override_driver) {
  2292. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  2293. "('%s' -> '%s')",
  2294. iface->driver, global->params.override_driver);
  2295. t_iface.driver = global->params.override_driver;
  2296. }
  2297. if (global->params.override_ctrl_interface) {
  2298. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  2299. "ctrl_interface ('%s' -> '%s')",
  2300. iface->ctrl_interface,
  2301. global->params.override_ctrl_interface);
  2302. t_iface.ctrl_interface =
  2303. global->params.override_ctrl_interface;
  2304. }
  2305. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  2306. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  2307. iface->ifname);
  2308. wpa_supplicant_deinit_iface(wpa_s, 0, 0);
  2309. os_free(wpa_s);
  2310. return NULL;
  2311. }
  2312. /* Notify the control interfaces about new iface */
  2313. if (wpas_notify_iface_added(wpa_s)) {
  2314. wpa_supplicant_deinit_iface(wpa_s, 1, 0);
  2315. os_free(wpa_s);
  2316. return NULL;
  2317. }
  2318. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2319. wpas_notify_network_added(wpa_s, ssid);
  2320. wpa_s->next = global->ifaces;
  2321. global->ifaces = wpa_s;
  2322. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  2323. return wpa_s;
  2324. }
  2325. /**
  2326. * wpa_supplicant_remove_iface - Remove a network interface
  2327. * @global: Pointer to global data from wpa_supplicant_init()
  2328. * @wpa_s: Pointer to the network interface to be removed
  2329. * Returns: 0 if interface was removed, -1 if interface was not found
  2330. *
  2331. * This function can be used to dynamically remove network interfaces from
  2332. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  2333. * addition, this function is used to remove all remaining interfaces when
  2334. * %wpa_supplicant is terminated.
  2335. */
  2336. int wpa_supplicant_remove_iface(struct wpa_global *global,
  2337. struct wpa_supplicant *wpa_s,
  2338. int terminate)
  2339. {
  2340. struct wpa_supplicant *prev;
  2341. /* Remove interface from the global list of interfaces */
  2342. prev = global->ifaces;
  2343. if (prev == wpa_s) {
  2344. global->ifaces = wpa_s->next;
  2345. } else {
  2346. while (prev && prev->next != wpa_s)
  2347. prev = prev->next;
  2348. if (prev == NULL)
  2349. return -1;
  2350. prev->next = wpa_s->next;
  2351. }
  2352. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  2353. if (global->p2p_group_formation == wpa_s)
  2354. global->p2p_group_formation = NULL;
  2355. wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
  2356. os_free(wpa_s);
  2357. return 0;
  2358. }
  2359. /**
  2360. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  2361. * @wpa_s: Pointer to the network interface
  2362. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  2363. */
  2364. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  2365. {
  2366. const char *eapol_method;
  2367. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  2368. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2369. return "NO-EAP";
  2370. }
  2371. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  2372. if (eapol_method == NULL)
  2373. return "UNKNOWN-EAP";
  2374. return eapol_method;
  2375. }
  2376. /**
  2377. * wpa_supplicant_get_iface - Get a new network interface
  2378. * @global: Pointer to global data from wpa_supplicant_init()
  2379. * @ifname: Interface name
  2380. * Returns: Pointer to the interface or %NULL if not found
  2381. */
  2382. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  2383. const char *ifname)
  2384. {
  2385. struct wpa_supplicant *wpa_s;
  2386. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2387. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  2388. return wpa_s;
  2389. }
  2390. return NULL;
  2391. }
  2392. #ifndef CONFIG_NO_WPA_MSG
  2393. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  2394. {
  2395. struct wpa_supplicant *wpa_s = ctx;
  2396. if (wpa_s == NULL)
  2397. return NULL;
  2398. return wpa_s->ifname;
  2399. }
  2400. #endif /* CONFIG_NO_WPA_MSG */
  2401. /**
  2402. * wpa_supplicant_init - Initialize %wpa_supplicant
  2403. * @params: Parameters for %wpa_supplicant
  2404. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  2405. *
  2406. * This function is used to initialize %wpa_supplicant. After successful
  2407. * initialization, the returned data pointer can be used to add and remove
  2408. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  2409. */
  2410. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  2411. {
  2412. struct wpa_global *global;
  2413. int ret, i;
  2414. if (params == NULL)
  2415. return NULL;
  2416. #ifdef CONFIG_DRIVER_NDIS
  2417. {
  2418. void driver_ndis_init_ops(void);
  2419. driver_ndis_init_ops();
  2420. }
  2421. #endif /* CONFIG_DRIVER_NDIS */
  2422. #ifndef CONFIG_NO_WPA_MSG
  2423. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  2424. #endif /* CONFIG_NO_WPA_MSG */
  2425. wpa_debug_open_file(params->wpa_debug_file_path);
  2426. if (params->wpa_debug_syslog)
  2427. wpa_debug_open_syslog();
  2428. ret = eap_register_methods();
  2429. if (ret) {
  2430. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  2431. if (ret == -2)
  2432. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  2433. "the same EAP type.");
  2434. return NULL;
  2435. }
  2436. global = os_zalloc(sizeof(*global));
  2437. if (global == NULL)
  2438. return NULL;
  2439. dl_list_init(&global->p2p_srv_bonjour);
  2440. dl_list_init(&global->p2p_srv_upnp);
  2441. global->params.daemonize = params->daemonize;
  2442. global->params.wait_for_monitor = params->wait_for_monitor;
  2443. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  2444. if (params->pid_file)
  2445. global->params.pid_file = os_strdup(params->pid_file);
  2446. if (params->ctrl_interface)
  2447. global->params.ctrl_interface =
  2448. os_strdup(params->ctrl_interface);
  2449. if (params->override_driver)
  2450. global->params.override_driver =
  2451. os_strdup(params->override_driver);
  2452. if (params->override_ctrl_interface)
  2453. global->params.override_ctrl_interface =
  2454. os_strdup(params->override_ctrl_interface);
  2455. wpa_debug_level = global->params.wpa_debug_level =
  2456. params->wpa_debug_level;
  2457. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  2458. params->wpa_debug_show_keys;
  2459. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  2460. params->wpa_debug_timestamp;
  2461. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  2462. if (eloop_init()) {
  2463. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  2464. wpa_supplicant_deinit(global);
  2465. return NULL;
  2466. }
  2467. random_init(params->entropy_file);
  2468. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  2469. if (global->ctrl_iface == NULL) {
  2470. wpa_supplicant_deinit(global);
  2471. return NULL;
  2472. }
  2473. if (wpas_notify_supplicant_initialized(global)) {
  2474. wpa_supplicant_deinit(global);
  2475. return NULL;
  2476. }
  2477. for (i = 0; wpa_drivers[i]; i++)
  2478. global->drv_count++;
  2479. if (global->drv_count == 0) {
  2480. wpa_printf(MSG_ERROR, "No drivers enabled");
  2481. wpa_supplicant_deinit(global);
  2482. return NULL;
  2483. }
  2484. global->drv_priv = os_zalloc(global->drv_count * sizeof(void *));
  2485. if (global->drv_priv == NULL) {
  2486. wpa_supplicant_deinit(global);
  2487. return NULL;
  2488. }
  2489. return global;
  2490. }
  2491. /**
  2492. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  2493. * @global: Pointer to global data from wpa_supplicant_init()
  2494. * Returns: 0 after successful event loop run, -1 on failure
  2495. *
  2496. * This function starts the main event loop and continues running as long as
  2497. * there are any remaining events. In most cases, this function is running as
  2498. * long as the %wpa_supplicant process in still in use.
  2499. */
  2500. int wpa_supplicant_run(struct wpa_global *global)
  2501. {
  2502. struct wpa_supplicant *wpa_s;
  2503. if (global->params.daemonize &&
  2504. wpa_supplicant_daemon(global->params.pid_file))
  2505. return -1;
  2506. if (global->params.wait_for_monitor) {
  2507. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  2508. if (wpa_s->ctrl_iface)
  2509. wpa_supplicant_ctrl_iface_wait(
  2510. wpa_s->ctrl_iface);
  2511. }
  2512. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  2513. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  2514. eloop_run();
  2515. return 0;
  2516. }
  2517. /**
  2518. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  2519. * @global: Pointer to global data from wpa_supplicant_init()
  2520. *
  2521. * This function is called to deinitialize %wpa_supplicant and to free all
  2522. * allocated resources. Remaining network interfaces will also be removed.
  2523. */
  2524. void wpa_supplicant_deinit(struct wpa_global *global)
  2525. {
  2526. int i;
  2527. if (global == NULL)
  2528. return;
  2529. #ifdef CONFIG_P2P
  2530. wpas_p2p_deinit_global(global);
  2531. #endif /* CONFIG_P2P */
  2532. while (global->ifaces)
  2533. wpa_supplicant_remove_iface(global, global->ifaces, 1);
  2534. if (global->ctrl_iface)
  2535. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  2536. wpas_notify_supplicant_deinitialized(global);
  2537. eap_peer_unregister_methods();
  2538. #ifdef CONFIG_AP
  2539. eap_server_unregister_methods();
  2540. #endif /* CONFIG_AP */
  2541. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  2542. if (!global->drv_priv[i])
  2543. continue;
  2544. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  2545. }
  2546. os_free(global->drv_priv);
  2547. random_deinit();
  2548. eloop_destroy();
  2549. if (global->params.pid_file) {
  2550. os_daemonize_terminate(global->params.pid_file);
  2551. os_free(global->params.pid_file);
  2552. }
  2553. os_free(global->params.ctrl_interface);
  2554. os_free(global->params.override_driver);
  2555. os_free(global->params.override_ctrl_interface);
  2556. os_free(global);
  2557. wpa_debug_close_syslog();
  2558. wpa_debug_close_file();
  2559. }
  2560. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  2561. {
  2562. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  2563. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  2564. char country[3];
  2565. country[0] = wpa_s->conf->country[0];
  2566. country[1] = wpa_s->conf->country[1];
  2567. country[2] = '\0';
  2568. if (wpa_drv_set_country(wpa_s, country) < 0) {
  2569. wpa_printf(MSG_ERROR, "Failed to set country code "
  2570. "'%s'", country);
  2571. }
  2572. }
  2573. #ifdef CONFIG_WPS
  2574. wpas_wps_update_config(wpa_s);
  2575. #endif /* CONFIG_WPS */
  2576. #ifdef CONFIG_P2P
  2577. wpas_p2p_update_config(wpa_s);
  2578. #endif /* CONFIG_P2P */
  2579. wpa_s->conf->changed_parameters = 0;
  2580. }
  2581. static void add_freq(int *freqs, int *num_freqs, int freq)
  2582. {
  2583. int i;
  2584. for (i = 0; i < *num_freqs; i++) {
  2585. if (freqs[i] == freq)
  2586. return;
  2587. }
  2588. freqs[*num_freqs] = freq;
  2589. (*num_freqs)++;
  2590. }
  2591. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  2592. {
  2593. struct wpa_bss *bss, *cbss;
  2594. const int max_freqs = 10;
  2595. int *freqs;
  2596. int num_freqs = 0;
  2597. freqs = os_zalloc(sizeof(int) * (max_freqs + 1));
  2598. if (freqs == NULL)
  2599. return NULL;
  2600. cbss = wpa_s->current_bss;
  2601. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  2602. if (bss == cbss)
  2603. continue;
  2604. if (bss->ssid_len == cbss->ssid_len &&
  2605. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  2606. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  2607. add_freq(freqs, &num_freqs, bss->freq);
  2608. if (num_freqs == max_freqs)
  2609. break;
  2610. }
  2611. }
  2612. if (num_freqs == 0) {
  2613. os_free(freqs);
  2614. freqs = NULL;
  2615. }
  2616. return freqs;
  2617. }
  2618. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  2619. {
  2620. int timeout;
  2621. int count;
  2622. int *freqs = NULL;
  2623. /*
  2624. * Remove possible authentication timeout since the connection failed.
  2625. */
  2626. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  2627. /*
  2628. * Add the failed BSSID into the blacklist and speed up next scan
  2629. * attempt if there could be other APs that could accept association.
  2630. * The current blacklist count indicates how many times we have tried
  2631. * connecting to this AP and multiple attempts mean that other APs are
  2632. * either not available or has already been tried, so that we can start
  2633. * increasing the delay here to avoid constant scanning.
  2634. */
  2635. count = wpa_blacklist_add(wpa_s, bssid);
  2636. if (count == 1 && wpa_s->current_bss) {
  2637. /*
  2638. * This BSS was not in the blacklist before. If there is
  2639. * another BSS available for the same ESS, we should try that
  2640. * next. Otherwise, we may as well try this one once more
  2641. * before allowing other, likely worse, ESSes to be considered.
  2642. */
  2643. freqs = get_bss_freqs_in_ess(wpa_s);
  2644. if (freqs) {
  2645. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  2646. "has been seen; try it next");
  2647. wpa_blacklist_add(wpa_s, bssid);
  2648. /*
  2649. * On the next scan, go through only the known channels
  2650. * used in this ESS based on previous scans to speed up
  2651. * common load balancing use case.
  2652. */
  2653. os_free(wpa_s->next_scan_freqs);
  2654. wpa_s->next_scan_freqs = freqs;
  2655. }
  2656. }
  2657. switch (count) {
  2658. case 1:
  2659. timeout = 100;
  2660. break;
  2661. case 2:
  2662. timeout = 500;
  2663. break;
  2664. case 3:
  2665. timeout = 1000;
  2666. break;
  2667. default:
  2668. timeout = 5000;
  2669. }
  2670. /*
  2671. * TODO: if more than one possible AP is available in scan results,
  2672. * could try the other ones before requesting a new scan.
  2673. */
  2674. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  2675. 1000 * (timeout % 1000));
  2676. }
  2677. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  2678. {
  2679. return wpa_s->conf->ap_scan == 2 ||
  2680. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  2681. }