wpa_supplicant.c 124 KB

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