wpa_supplicant.c 132 KB

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