wpa_supplicant.c 106 KB

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