wpa_supplicant.c 132 KB

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