wpa_supplicant.c 131 KB

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