wpa_supplicant.c 177 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525
  1. /*
  2. * WPA Supplicant
  3. * Copyright (c) 2003-2017, 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. #ifdef CONFIG_MATCH_IFACE
  14. #include <net/if.h>
  15. #include <fnmatch.h>
  16. #endif /* CONFIG_MATCH_IFACE */
  17. #include "common.h"
  18. #include "crypto/random.h"
  19. #include "crypto/sha1.h"
  20. #include "eapol_supp/eapol_supp_sm.h"
  21. #include "eap_peer/eap.h"
  22. #include "eap_peer/eap_proxy.h"
  23. #include "eap_server/eap_methods.h"
  24. #include "rsn_supp/wpa.h"
  25. #include "eloop.h"
  26. #include "config.h"
  27. #include "utils/ext_password.h"
  28. #include "l2_packet/l2_packet.h"
  29. #include "wpa_supplicant_i.h"
  30. #include "driver_i.h"
  31. #include "ctrl_iface.h"
  32. #include "pcsc_funcs.h"
  33. #include "common/version.h"
  34. #include "rsn_supp/preauth.h"
  35. #include "rsn_supp/pmksa_cache.h"
  36. #include "common/wpa_ctrl.h"
  37. #include "common/ieee802_11_defs.h"
  38. #include "common/hw_features_common.h"
  39. #include "p2p/p2p.h"
  40. #include "fst/fst.h"
  41. #include "blacklist.h"
  42. #include "wpas_glue.h"
  43. #include "wps_supplicant.h"
  44. #include "ibss_rsn.h"
  45. #include "sme.h"
  46. #include "gas_query.h"
  47. #include "ap.h"
  48. #include "p2p_supplicant.h"
  49. #include "wifi_display.h"
  50. #include "notify.h"
  51. #include "bgscan.h"
  52. #include "autoscan.h"
  53. #include "bss.h"
  54. #include "scan.h"
  55. #include "offchannel.h"
  56. #include "hs20_supplicant.h"
  57. #include "wnm_sta.h"
  58. #include "wpas_kay.h"
  59. #include "mesh.h"
  60. const char *const wpa_supplicant_version =
  61. "wpa_supplicant v" VERSION_STR "\n"
  62. "Copyright (c) 2003-2017, Jouni Malinen <j@w1.fi> and contributors";
  63. const char *const wpa_supplicant_license =
  64. "This software may be distributed under the terms of the BSD license.\n"
  65. "See README for more details.\n"
  66. #ifdef EAP_TLS_OPENSSL
  67. "\nThis product includes software developed by the OpenSSL Project\n"
  68. "for use in the OpenSSL Toolkit (http://www.openssl.org/)\n"
  69. #endif /* EAP_TLS_OPENSSL */
  70. ;
  71. #ifndef CONFIG_NO_STDOUT_DEBUG
  72. /* Long text divided into parts in order to fit in C89 strings size limits. */
  73. const char *const wpa_supplicant_full_license1 =
  74. "";
  75. const char *const wpa_supplicant_full_license2 =
  76. "This software may be distributed under the terms of the BSD license.\n"
  77. "\n"
  78. "Redistribution and use in source and binary forms, with or without\n"
  79. "modification, are permitted provided that the following conditions are\n"
  80. "met:\n"
  81. "\n";
  82. const char *const wpa_supplicant_full_license3 =
  83. "1. Redistributions of source code must retain the above copyright\n"
  84. " notice, this list of conditions and the following disclaimer.\n"
  85. "\n"
  86. "2. Redistributions in binary form must reproduce the above copyright\n"
  87. " notice, this list of conditions and the following disclaimer in the\n"
  88. " documentation and/or other materials provided with the distribution.\n"
  89. "\n";
  90. const char *const wpa_supplicant_full_license4 =
  91. "3. Neither the name(s) of the above-listed copyright holder(s) nor the\n"
  92. " names of its contributors may be used to endorse or promote products\n"
  93. " derived from this software without specific prior written permission.\n"
  94. "\n"
  95. "THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n"
  96. "\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n"
  97. "LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n"
  98. "A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n";
  99. const char *const wpa_supplicant_full_license5 =
  100. "OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n"
  101. "SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n"
  102. "LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
  103. "DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
  104. "THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
  105. "(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n"
  106. "OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
  107. "\n";
  108. #endif /* CONFIG_NO_STDOUT_DEBUG */
  109. /* Configure default/group WEP keys for static WEP */
  110. int wpa_set_wep_keys(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  111. {
  112. int i, set = 0;
  113. for (i = 0; i < NUM_WEP_KEYS; i++) {
  114. if (ssid->wep_key_len[i] == 0)
  115. continue;
  116. set = 1;
  117. wpa_drv_set_key(wpa_s, WPA_ALG_WEP, NULL,
  118. i, i == ssid->wep_tx_keyidx, NULL, 0,
  119. ssid->wep_key[i], ssid->wep_key_len[i]);
  120. }
  121. return set;
  122. }
  123. int wpa_supplicant_set_wpa_none_key(struct wpa_supplicant *wpa_s,
  124. struct wpa_ssid *ssid)
  125. {
  126. u8 key[32];
  127. size_t keylen;
  128. enum wpa_alg alg;
  129. u8 seq[6] = { 0 };
  130. int ret;
  131. /* IBSS/WPA-None uses only one key (Group) for both receiving and
  132. * sending unicast and multicast packets. */
  133. if (ssid->mode != WPAS_MODE_IBSS) {
  134. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid mode %d (not "
  135. "IBSS/ad-hoc) for WPA-None", ssid->mode);
  136. return -1;
  137. }
  138. if (!ssid->psk_set) {
  139. wpa_msg(wpa_s, MSG_INFO, "WPA: No PSK configured for "
  140. "WPA-None");
  141. return -1;
  142. }
  143. switch (wpa_s->group_cipher) {
  144. case WPA_CIPHER_CCMP:
  145. os_memcpy(key, ssid->psk, 16);
  146. keylen = 16;
  147. alg = WPA_ALG_CCMP;
  148. break;
  149. case WPA_CIPHER_GCMP:
  150. os_memcpy(key, ssid->psk, 16);
  151. keylen = 16;
  152. alg = WPA_ALG_GCMP;
  153. break;
  154. case WPA_CIPHER_TKIP:
  155. /* WPA-None uses the same Michael MIC key for both TX and RX */
  156. os_memcpy(key, ssid->psk, 16 + 8);
  157. os_memcpy(key + 16 + 8, ssid->psk + 16, 8);
  158. keylen = 32;
  159. alg = WPA_ALG_TKIP;
  160. break;
  161. default:
  162. wpa_msg(wpa_s, MSG_INFO, "WPA: Invalid group cipher %d for "
  163. "WPA-None", wpa_s->group_cipher);
  164. return -1;
  165. }
  166. /* TODO: should actually remember the previously used seq#, both for TX
  167. * and RX from each STA.. */
  168. ret = wpa_drv_set_key(wpa_s, alg, NULL, 0, 1, seq, 6, key, keylen);
  169. os_memset(key, 0, sizeof(key));
  170. return ret;
  171. }
  172. static void wpa_supplicant_timeout(void *eloop_ctx, void *timeout_ctx)
  173. {
  174. struct wpa_supplicant *wpa_s = eloop_ctx;
  175. const u8 *bssid = wpa_s->bssid;
  176. if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
  177. (wpa_s->wpa_state == WPA_AUTHENTICATING ||
  178. wpa_s->wpa_state == WPA_ASSOCIATING))
  179. bssid = wpa_s->pending_bssid;
  180. wpa_msg(wpa_s, MSG_INFO, "Authentication with " MACSTR " timed out.",
  181. MAC2STR(bssid));
  182. wpa_blacklist_add(wpa_s, bssid);
  183. wpa_sm_notify_disassoc(wpa_s->wpa);
  184. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  185. wpa_s->reassociate = 1;
  186. /*
  187. * If we timed out, the AP or the local radio may be busy.
  188. * So, wait a second until scanning again.
  189. */
  190. wpa_supplicant_req_scan(wpa_s, 1, 0);
  191. }
  192. /**
  193. * wpa_supplicant_req_auth_timeout - Schedule a timeout for authentication
  194. * @wpa_s: Pointer to wpa_supplicant data
  195. * @sec: Number of seconds after which to time out authentication
  196. * @usec: Number of microseconds after which to time out authentication
  197. *
  198. * This function is used to schedule a timeout for the current authentication
  199. * attempt.
  200. */
  201. void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *wpa_s,
  202. int sec, int usec)
  203. {
  204. if (wpa_s->conf->ap_scan == 0 &&
  205. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED))
  206. return;
  207. wpa_dbg(wpa_s, MSG_DEBUG, "Setting authentication timeout: %d sec "
  208. "%d usec", sec, usec);
  209. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  210. eloop_register_timeout(sec, usec, wpa_supplicant_timeout, wpa_s, NULL);
  211. }
  212. /**
  213. * wpa_supplicant_cancel_auth_timeout - Cancel authentication timeout
  214. * @wpa_s: Pointer to wpa_supplicant data
  215. *
  216. * This function is used to cancel authentication timeout scheduled with
  217. * wpa_supplicant_req_auth_timeout() and it is called when authentication has
  218. * been completed.
  219. */
  220. void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *wpa_s)
  221. {
  222. wpa_dbg(wpa_s, MSG_DEBUG, "Cancelling authentication timeout");
  223. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  224. wpa_blacklist_del(wpa_s, wpa_s->bssid);
  225. }
  226. /**
  227. * wpa_supplicant_initiate_eapol - Configure EAPOL state machine
  228. * @wpa_s: Pointer to wpa_supplicant data
  229. *
  230. * This function is used to configure EAPOL state machine based on the selected
  231. * authentication mode.
  232. */
  233. void wpa_supplicant_initiate_eapol(struct wpa_supplicant *wpa_s)
  234. {
  235. #ifdef IEEE8021X_EAPOL
  236. struct eapol_config eapol_conf;
  237. struct wpa_ssid *ssid = wpa_s->current_ssid;
  238. #ifdef CONFIG_IBSS_RSN
  239. if (ssid->mode == WPAS_MODE_IBSS &&
  240. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  241. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  242. /*
  243. * RSN IBSS authentication is per-STA and we can disable the
  244. * per-BSSID EAPOL authentication.
  245. */
  246. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  247. eapol_sm_notify_eap_success(wpa_s->eapol, TRUE);
  248. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  249. return;
  250. }
  251. #endif /* CONFIG_IBSS_RSN */
  252. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  253. eapol_sm_notify_eap_fail(wpa_s->eapol, FALSE);
  254. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  255. wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE)
  256. eapol_sm_notify_portControl(wpa_s->eapol, ForceAuthorized);
  257. else
  258. eapol_sm_notify_portControl(wpa_s->eapol, Auto);
  259. os_memset(&eapol_conf, 0, sizeof(eapol_conf));
  260. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  261. eapol_conf.accept_802_1x_keys = 1;
  262. eapol_conf.required_keys = 0;
  263. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_UNICAST) {
  264. eapol_conf.required_keys |= EAPOL_REQUIRE_KEY_UNICAST;
  265. }
  266. if (ssid->eapol_flags & EAPOL_FLAG_REQUIRE_KEY_BROADCAST) {
  267. eapol_conf.required_keys |=
  268. EAPOL_REQUIRE_KEY_BROADCAST;
  269. }
  270. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED)
  271. eapol_conf.required_keys = 0;
  272. }
  273. eapol_conf.fast_reauth = wpa_s->conf->fast_reauth;
  274. eapol_conf.workaround = ssid->eap_workaround;
  275. eapol_conf.eap_disabled =
  276. !wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) &&
  277. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA &&
  278. wpa_s->key_mgmt != WPA_KEY_MGMT_WPS;
  279. eapol_conf.external_sim = wpa_s->conf->external_sim;
  280. #ifdef CONFIG_WPS
  281. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  282. eapol_conf.wps |= EAPOL_LOCAL_WPS_IN_USE;
  283. if (wpa_s->current_bss) {
  284. struct wpabuf *ie;
  285. ie = wpa_bss_get_vendor_ie_multi(wpa_s->current_bss,
  286. WPS_IE_VENDOR_TYPE);
  287. if (ie) {
  288. if (wps_is_20(ie))
  289. eapol_conf.wps |=
  290. EAPOL_PEER_IS_WPS20_AP;
  291. wpabuf_free(ie);
  292. }
  293. }
  294. }
  295. #endif /* CONFIG_WPS */
  296. eapol_sm_notify_config(wpa_s->eapol, &ssid->eap, &eapol_conf);
  297. #ifdef CONFIG_MACSEC
  298. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE && ssid->mka_psk_set)
  299. ieee802_1x_create_preshared_mka(wpa_s, ssid);
  300. else
  301. ieee802_1x_alloc_kay_sm(wpa_s, ssid);
  302. #endif /* CONFIG_MACSEC */
  303. #endif /* IEEE8021X_EAPOL */
  304. }
  305. /**
  306. * wpa_supplicant_set_non_wpa_policy - Set WPA parameters to non-WPA mode
  307. * @wpa_s: Pointer to wpa_supplicant data
  308. * @ssid: Configuration data for the network
  309. *
  310. * This function is used to configure WPA state machine and related parameters
  311. * to a mode where WPA is not enabled. This is called as part of the
  312. * authentication configuration when the selected network does not use WPA.
  313. */
  314. void wpa_supplicant_set_non_wpa_policy(struct wpa_supplicant *wpa_s,
  315. struct wpa_ssid *ssid)
  316. {
  317. int i;
  318. if (ssid->key_mgmt & WPA_KEY_MGMT_WPS)
  319. wpa_s->key_mgmt = WPA_KEY_MGMT_WPS;
  320. else if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA)
  321. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_NO_WPA;
  322. else
  323. wpa_s->key_mgmt = WPA_KEY_MGMT_NONE;
  324. wpa_sm_set_ap_wpa_ie(wpa_s->wpa, NULL, 0);
  325. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, NULL, 0);
  326. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  327. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  328. wpa_s->group_cipher = WPA_CIPHER_NONE;
  329. wpa_s->mgmt_group_cipher = 0;
  330. for (i = 0; i < NUM_WEP_KEYS; i++) {
  331. if (ssid->wep_key_len[i] > 5) {
  332. wpa_s->pairwise_cipher = WPA_CIPHER_WEP104;
  333. wpa_s->group_cipher = WPA_CIPHER_WEP104;
  334. break;
  335. } else if (ssid->wep_key_len[i] > 0) {
  336. wpa_s->pairwise_cipher = WPA_CIPHER_WEP40;
  337. wpa_s->group_cipher = WPA_CIPHER_WEP40;
  338. break;
  339. }
  340. }
  341. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED, 0);
  342. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  343. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  344. wpa_s->pairwise_cipher);
  345. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  346. #ifdef CONFIG_IEEE80211W
  347. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  348. wpa_s->mgmt_group_cipher);
  349. #endif /* CONFIG_IEEE80211W */
  350. pmksa_cache_clear_current(wpa_s->wpa);
  351. }
  352. void free_hw_features(struct wpa_supplicant *wpa_s)
  353. {
  354. int i;
  355. if (wpa_s->hw.modes == NULL)
  356. return;
  357. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  358. os_free(wpa_s->hw.modes[i].channels);
  359. os_free(wpa_s->hw.modes[i].rates);
  360. }
  361. os_free(wpa_s->hw.modes);
  362. wpa_s->hw.modes = NULL;
  363. }
  364. static void free_bss_tmp_disallowed(struct wpa_supplicant *wpa_s)
  365. {
  366. struct wpa_bss_tmp_disallowed *bss, *prev;
  367. dl_list_for_each_safe(bss, prev, &wpa_s->bss_tmp_disallowed,
  368. struct wpa_bss_tmp_disallowed, list) {
  369. dl_list_del(&bss->list);
  370. os_free(bss);
  371. }
  372. }
  373. void wpas_flush_fils_hlp_req(struct wpa_supplicant *wpa_s)
  374. {
  375. struct fils_hlp_req *req;
  376. while ((req = dl_list_first(&wpa_s->fils_hlp_req, struct fils_hlp_req,
  377. list)) != NULL) {
  378. dl_list_del(&req->list);
  379. wpabuf_free(req->pkt);
  380. os_free(req);
  381. }
  382. }
  383. static void wpa_supplicant_cleanup(struct wpa_supplicant *wpa_s)
  384. {
  385. int i;
  386. bgscan_deinit(wpa_s);
  387. autoscan_deinit(wpa_s);
  388. scard_deinit(wpa_s->scard);
  389. wpa_s->scard = NULL;
  390. wpa_sm_set_scard_ctx(wpa_s->wpa, NULL);
  391. eapol_sm_register_scard_ctx(wpa_s->eapol, NULL);
  392. l2_packet_deinit(wpa_s->l2);
  393. wpa_s->l2 = NULL;
  394. if (wpa_s->l2_br) {
  395. l2_packet_deinit(wpa_s->l2_br);
  396. wpa_s->l2_br = NULL;
  397. }
  398. #ifdef CONFIG_TESTING_OPTIONS
  399. l2_packet_deinit(wpa_s->l2_test);
  400. wpa_s->l2_test = NULL;
  401. os_free(wpa_s->get_pref_freq_list_override);
  402. wpa_s->get_pref_freq_list_override = NULL;
  403. #endif /* CONFIG_TESTING_OPTIONS */
  404. if (wpa_s->conf != NULL) {
  405. struct wpa_ssid *ssid;
  406. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  407. wpas_notify_network_removed(wpa_s, ssid);
  408. }
  409. os_free(wpa_s->confname);
  410. wpa_s->confname = NULL;
  411. os_free(wpa_s->confanother);
  412. wpa_s->confanother = NULL;
  413. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  414. eapol_sm_deinit(wpa_s->eapol);
  415. wpa_s->eapol = NULL;
  416. rsn_preauth_deinit(wpa_s->wpa);
  417. #ifdef CONFIG_TDLS
  418. wpa_tdls_deinit(wpa_s->wpa);
  419. #endif /* CONFIG_TDLS */
  420. wmm_ac_clear_saved_tspecs(wpa_s);
  421. pmksa_candidate_free(wpa_s->wpa);
  422. wpa_sm_deinit(wpa_s->wpa);
  423. wpa_s->wpa = NULL;
  424. wpa_blacklist_clear(wpa_s);
  425. wpa_bss_deinit(wpa_s);
  426. wpa_supplicant_cancel_delayed_sched_scan(wpa_s);
  427. wpa_supplicant_cancel_scan(wpa_s);
  428. wpa_supplicant_cancel_auth_timeout(wpa_s);
  429. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  430. #ifdef CONFIG_DELAYED_MIC_ERROR_REPORT
  431. eloop_cancel_timeout(wpa_supplicant_delayed_mic_error_report,
  432. wpa_s, NULL);
  433. #endif /* CONFIG_DELAYED_MIC_ERROR_REPORT */
  434. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  435. wpas_wps_deinit(wpa_s);
  436. wpabuf_free(wpa_s->pending_eapol_rx);
  437. wpa_s->pending_eapol_rx = NULL;
  438. #ifdef CONFIG_IBSS_RSN
  439. ibss_rsn_deinit(wpa_s->ibss_rsn);
  440. wpa_s->ibss_rsn = NULL;
  441. #endif /* CONFIG_IBSS_RSN */
  442. sme_deinit(wpa_s);
  443. #ifdef CONFIG_AP
  444. wpa_supplicant_ap_deinit(wpa_s);
  445. #endif /* CONFIG_AP */
  446. wpas_p2p_deinit(wpa_s);
  447. #ifdef CONFIG_OFFCHANNEL
  448. offchannel_deinit(wpa_s);
  449. #endif /* CONFIG_OFFCHANNEL */
  450. wpa_supplicant_cancel_sched_scan(wpa_s);
  451. os_free(wpa_s->next_scan_freqs);
  452. wpa_s->next_scan_freqs = NULL;
  453. os_free(wpa_s->manual_scan_freqs);
  454. wpa_s->manual_scan_freqs = NULL;
  455. os_free(wpa_s->manual_sched_scan_freqs);
  456. wpa_s->manual_sched_scan_freqs = NULL;
  457. wpas_mac_addr_rand_scan_clear(wpa_s, MAC_ADDR_RAND_ALL);
  458. /*
  459. * Need to remove any pending gas-query radio work before the
  460. * gas_query_deinit() call because gas_query::work has not yet been set
  461. * for works that have not been started. gas_query_free() will be unable
  462. * to cancel such pending radio works and once the pending gas-query
  463. * radio work eventually gets removed, the deinit notification call to
  464. * gas_query_start_cb() would result in dereferencing freed memory.
  465. */
  466. if (wpa_s->radio)
  467. radio_remove_works(wpa_s, "gas-query", 0);
  468. gas_query_deinit(wpa_s->gas);
  469. wpa_s->gas = NULL;
  470. free_hw_features(wpa_s);
  471. ieee802_1x_dealloc_kay_sm(wpa_s);
  472. os_free(wpa_s->bssid_filter);
  473. wpa_s->bssid_filter = NULL;
  474. os_free(wpa_s->disallow_aps_bssid);
  475. wpa_s->disallow_aps_bssid = NULL;
  476. os_free(wpa_s->disallow_aps_ssid);
  477. wpa_s->disallow_aps_ssid = NULL;
  478. wnm_bss_keep_alive_deinit(wpa_s);
  479. #ifdef CONFIG_WNM
  480. wnm_deallocate_memory(wpa_s);
  481. #endif /* CONFIG_WNM */
  482. ext_password_deinit(wpa_s->ext_pw);
  483. wpa_s->ext_pw = NULL;
  484. wpabuf_free(wpa_s->last_gas_resp);
  485. wpa_s->last_gas_resp = NULL;
  486. wpabuf_free(wpa_s->prev_gas_resp);
  487. wpa_s->prev_gas_resp = NULL;
  488. os_free(wpa_s->last_scan_res);
  489. wpa_s->last_scan_res = NULL;
  490. #ifdef CONFIG_HS20
  491. if (wpa_s->drv_priv)
  492. wpa_drv_configure_frame_filters(wpa_s, 0);
  493. hs20_deinit(wpa_s);
  494. #endif /* CONFIG_HS20 */
  495. for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
  496. wpabuf_free(wpa_s->vendor_elem[i]);
  497. wpa_s->vendor_elem[i] = NULL;
  498. }
  499. wmm_ac_notify_disassoc(wpa_s);
  500. wpa_s->sched_scan_plans_num = 0;
  501. os_free(wpa_s->sched_scan_plans);
  502. wpa_s->sched_scan_plans = NULL;
  503. #ifdef CONFIG_MBO
  504. wpa_s->non_pref_chan_num = 0;
  505. os_free(wpa_s->non_pref_chan);
  506. wpa_s->non_pref_chan = NULL;
  507. #endif /* CONFIG_MBO */
  508. free_bss_tmp_disallowed(wpa_s);
  509. wpabuf_free(wpa_s->lci);
  510. wpa_s->lci = NULL;
  511. wpas_clear_beacon_rep_data(wpa_s);
  512. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  513. #ifdef CONFIG_MESH
  514. {
  515. struct external_pmksa_cache *entry;
  516. while ((entry = dl_list_last(&wpa_s->mesh_external_pmksa_cache,
  517. struct external_pmksa_cache,
  518. list)) != NULL) {
  519. dl_list_del(&entry->list);
  520. os_free(entry->pmksa_cache);
  521. os_free(entry);
  522. }
  523. }
  524. #endif /* CONFIG_MESH */
  525. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  526. wpas_flush_fils_hlp_req(wpa_s);
  527. wpabuf_free(wpa_s->ric_ies);
  528. wpa_s->ric_ies = NULL;
  529. }
  530. /**
  531. * wpa_clear_keys - Clear keys configured for the driver
  532. * @wpa_s: Pointer to wpa_supplicant data
  533. * @addr: Previously used BSSID or %NULL if not available
  534. *
  535. * This function clears the encryption keys that has been previously configured
  536. * for the driver.
  537. */
  538. void wpa_clear_keys(struct wpa_supplicant *wpa_s, const u8 *addr)
  539. {
  540. int i, max;
  541. #ifdef CONFIG_IEEE80211W
  542. max = 6;
  543. #else /* CONFIG_IEEE80211W */
  544. max = 4;
  545. #endif /* CONFIG_IEEE80211W */
  546. /* MLME-DELETEKEYS.request */
  547. for (i = 0; i < max; i++) {
  548. if (wpa_s->keys_cleared & BIT(i))
  549. continue;
  550. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, i, 0, NULL, 0,
  551. NULL, 0);
  552. }
  553. if (!(wpa_s->keys_cleared & BIT(0)) && addr &&
  554. !is_zero_ether_addr(addr)) {
  555. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, addr, 0, 0, NULL, 0, NULL,
  556. 0);
  557. /* MLME-SETPROTECTION.request(None) */
  558. wpa_drv_mlme_setprotection(
  559. wpa_s, addr,
  560. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  561. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  562. }
  563. wpa_s->keys_cleared = (u32) -1;
  564. }
  565. /**
  566. * wpa_supplicant_state_txt - Get the connection state name as a text string
  567. * @state: State (wpa_state; WPA_*)
  568. * Returns: The state name as a printable text string
  569. */
  570. const char * wpa_supplicant_state_txt(enum wpa_states state)
  571. {
  572. switch (state) {
  573. case WPA_DISCONNECTED:
  574. return "DISCONNECTED";
  575. case WPA_INACTIVE:
  576. return "INACTIVE";
  577. case WPA_INTERFACE_DISABLED:
  578. return "INTERFACE_DISABLED";
  579. case WPA_SCANNING:
  580. return "SCANNING";
  581. case WPA_AUTHENTICATING:
  582. return "AUTHENTICATING";
  583. case WPA_ASSOCIATING:
  584. return "ASSOCIATING";
  585. case WPA_ASSOCIATED:
  586. return "ASSOCIATED";
  587. case WPA_4WAY_HANDSHAKE:
  588. return "4WAY_HANDSHAKE";
  589. case WPA_GROUP_HANDSHAKE:
  590. return "GROUP_HANDSHAKE";
  591. case WPA_COMPLETED:
  592. return "COMPLETED";
  593. default:
  594. return "UNKNOWN";
  595. }
  596. }
  597. #ifdef CONFIG_BGSCAN
  598. static void wpa_supplicant_start_bgscan(struct wpa_supplicant *wpa_s)
  599. {
  600. const char *name;
  601. if (wpa_s->current_ssid && wpa_s->current_ssid->bgscan)
  602. name = wpa_s->current_ssid->bgscan;
  603. else
  604. name = wpa_s->conf->bgscan;
  605. if (name == NULL || name[0] == '\0')
  606. return;
  607. if (wpas_driver_bss_selection(wpa_s))
  608. return;
  609. if (wpa_s->current_ssid == wpa_s->bgscan_ssid)
  610. return;
  611. #ifdef CONFIG_P2P
  612. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
  613. return;
  614. #endif /* CONFIG_P2P */
  615. bgscan_deinit(wpa_s);
  616. if (wpa_s->current_ssid) {
  617. if (bgscan_init(wpa_s, wpa_s->current_ssid, name)) {
  618. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  619. "bgscan");
  620. /*
  621. * Live without bgscan; it is only used as a roaming
  622. * optimization, so the initial connection is not
  623. * affected.
  624. */
  625. } else {
  626. struct wpa_scan_results *scan_res;
  627. wpa_s->bgscan_ssid = wpa_s->current_ssid;
  628. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL,
  629. 0);
  630. if (scan_res) {
  631. bgscan_notify_scan(wpa_s, scan_res);
  632. wpa_scan_results_free(scan_res);
  633. }
  634. }
  635. } else
  636. wpa_s->bgscan_ssid = NULL;
  637. }
  638. static void wpa_supplicant_stop_bgscan(struct wpa_supplicant *wpa_s)
  639. {
  640. if (wpa_s->bgscan_ssid != NULL) {
  641. bgscan_deinit(wpa_s);
  642. wpa_s->bgscan_ssid = NULL;
  643. }
  644. }
  645. #endif /* CONFIG_BGSCAN */
  646. static void wpa_supplicant_start_autoscan(struct wpa_supplicant *wpa_s)
  647. {
  648. if (autoscan_init(wpa_s, 0))
  649. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize autoscan");
  650. }
  651. static void wpa_supplicant_stop_autoscan(struct wpa_supplicant *wpa_s)
  652. {
  653. autoscan_deinit(wpa_s);
  654. }
  655. void wpa_supplicant_reinit_autoscan(struct wpa_supplicant *wpa_s)
  656. {
  657. if (wpa_s->wpa_state == WPA_DISCONNECTED ||
  658. wpa_s->wpa_state == WPA_SCANNING) {
  659. autoscan_deinit(wpa_s);
  660. wpa_supplicant_start_autoscan(wpa_s);
  661. }
  662. }
  663. /**
  664. * wpa_supplicant_set_state - Set current connection state
  665. * @wpa_s: Pointer to wpa_supplicant data
  666. * @state: The new connection state
  667. *
  668. * This function is called whenever the connection state changes, e.g.,
  669. * association is completed for WPA/WPA2 4-Way Handshake is started.
  670. */
  671. void wpa_supplicant_set_state(struct wpa_supplicant *wpa_s,
  672. enum wpa_states state)
  673. {
  674. enum wpa_states old_state = wpa_s->wpa_state;
  675. wpa_dbg(wpa_s, MSG_DEBUG, "State: %s -> %s",
  676. wpa_supplicant_state_txt(wpa_s->wpa_state),
  677. wpa_supplicant_state_txt(state));
  678. if (state == WPA_INTERFACE_DISABLED) {
  679. /* Assure normal scan when interface is restored */
  680. wpa_s->normal_scans = 0;
  681. }
  682. if (state == WPA_COMPLETED) {
  683. wpas_connect_work_done(wpa_s);
  684. /* Reinitialize normal_scan counter */
  685. wpa_s->normal_scans = 0;
  686. }
  687. #ifdef CONFIG_P2P
  688. /*
  689. * P2PS client has to reply to Probe Request frames received on the
  690. * group operating channel. Enable Probe Request frame reporting for
  691. * P2P connected client in case p2p_cli_probe configuration property is
  692. * set to 1.
  693. */
  694. if (wpa_s->conf->p2p_cli_probe && wpa_s->current_ssid &&
  695. wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
  696. wpa_s->current_ssid->p2p_group) {
  697. if (state == WPA_COMPLETED && !wpa_s->p2p_cli_probe) {
  698. wpa_dbg(wpa_s, MSG_DEBUG,
  699. "P2P: Enable CLI Probe Request RX reporting");
  700. wpa_s->p2p_cli_probe =
  701. wpa_drv_probe_req_report(wpa_s, 1) >= 0;
  702. } else if (state != WPA_COMPLETED && wpa_s->p2p_cli_probe) {
  703. wpa_dbg(wpa_s, MSG_DEBUG,
  704. "P2P: Disable CLI Probe Request RX reporting");
  705. wpa_s->p2p_cli_probe = 0;
  706. wpa_drv_probe_req_report(wpa_s, 0);
  707. }
  708. }
  709. #endif /* CONFIG_P2P */
  710. if (state != WPA_SCANNING)
  711. wpa_supplicant_notify_scanning(wpa_s, 0);
  712. if (state == WPA_COMPLETED && wpa_s->new_connection) {
  713. struct wpa_ssid *ssid = wpa_s->current_ssid;
  714. #if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
  715. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED "- Connection to "
  716. MACSTR " completed [id=%d id_str=%s]",
  717. MAC2STR(wpa_s->bssid),
  718. ssid ? ssid->id : -1,
  719. ssid && ssid->id_str ? ssid->id_str : "");
  720. #endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
  721. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  722. wpa_blacklist_clear(wpa_s);
  723. wpa_s->extra_blacklist_count = 0;
  724. wpa_s->new_connection = 0;
  725. wpa_drv_set_operstate(wpa_s, 1);
  726. #ifndef IEEE8021X_EAPOL
  727. wpa_drv_set_supp_port(wpa_s, 1);
  728. #endif /* IEEE8021X_EAPOL */
  729. wpa_s->after_wps = 0;
  730. wpa_s->known_wps_freq = 0;
  731. wpas_p2p_completed(wpa_s);
  732. sme_sched_obss_scan(wpa_s, 1);
  733. } else if (state == WPA_DISCONNECTED || state == WPA_ASSOCIATING ||
  734. state == WPA_ASSOCIATED) {
  735. wpa_s->new_connection = 1;
  736. wpa_drv_set_operstate(wpa_s, 0);
  737. #ifndef IEEE8021X_EAPOL
  738. wpa_drv_set_supp_port(wpa_s, 0);
  739. #endif /* IEEE8021X_EAPOL */
  740. sme_sched_obss_scan(wpa_s, 0);
  741. }
  742. wpa_s->wpa_state = state;
  743. #ifdef CONFIG_BGSCAN
  744. if (state == WPA_COMPLETED)
  745. wpa_supplicant_start_bgscan(wpa_s);
  746. else if (state < WPA_ASSOCIATED)
  747. wpa_supplicant_stop_bgscan(wpa_s);
  748. #endif /* CONFIG_BGSCAN */
  749. if (state == WPA_AUTHENTICATING)
  750. wpa_supplicant_stop_autoscan(wpa_s);
  751. if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  752. wpa_supplicant_start_autoscan(wpa_s);
  753. if (old_state >= WPA_ASSOCIATED && wpa_s->wpa_state < WPA_ASSOCIATED)
  754. wmm_ac_notify_disassoc(wpa_s);
  755. if (wpa_s->wpa_state != old_state) {
  756. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  757. /*
  758. * Notify the P2P Device interface about a state change in one
  759. * of the interfaces.
  760. */
  761. wpas_p2p_indicate_state_change(wpa_s);
  762. if (wpa_s->wpa_state == WPA_COMPLETED ||
  763. old_state == WPA_COMPLETED)
  764. wpas_notify_auth_changed(wpa_s);
  765. }
  766. }
  767. void wpa_supplicant_terminate_proc(struct wpa_global *global)
  768. {
  769. int pending = 0;
  770. #ifdef CONFIG_WPS
  771. struct wpa_supplicant *wpa_s = global->ifaces;
  772. while (wpa_s) {
  773. struct wpa_supplicant *next = wpa_s->next;
  774. if (wpas_wps_terminate_pending(wpa_s) == 1)
  775. pending = 1;
  776. #ifdef CONFIG_P2P
  777. if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE ||
  778. (wpa_s->current_ssid && wpa_s->current_ssid->p2p_group))
  779. wpas_p2p_disconnect(wpa_s);
  780. #endif /* CONFIG_P2P */
  781. wpa_s = next;
  782. }
  783. #endif /* CONFIG_WPS */
  784. if (pending)
  785. return;
  786. eloop_terminate();
  787. }
  788. static void wpa_supplicant_terminate(int sig, void *signal_ctx)
  789. {
  790. struct wpa_global *global = signal_ctx;
  791. wpa_supplicant_terminate_proc(global);
  792. }
  793. void wpa_supplicant_clear_status(struct wpa_supplicant *wpa_s)
  794. {
  795. enum wpa_states old_state = wpa_s->wpa_state;
  796. wpa_s->pairwise_cipher = 0;
  797. wpa_s->group_cipher = 0;
  798. wpa_s->mgmt_group_cipher = 0;
  799. wpa_s->key_mgmt = 0;
  800. if (wpa_s->wpa_state != WPA_INTERFACE_DISABLED)
  801. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  802. if (wpa_s->wpa_state != old_state)
  803. wpas_notify_state_changed(wpa_s, wpa_s->wpa_state, old_state);
  804. }
  805. /**
  806. * wpa_supplicant_reload_configuration - Reload configuration data
  807. * @wpa_s: Pointer to wpa_supplicant data
  808. * Returns: 0 on success or -1 if configuration parsing failed
  809. *
  810. * This function can be used to request that the configuration data is reloaded
  811. * (e.g., after configuration file change). This function is reloading
  812. * configuration only for one interface, so this may need to be called multiple
  813. * times if %wpa_supplicant is controlling multiple interfaces and all
  814. * interfaces need reconfiguration.
  815. */
  816. int wpa_supplicant_reload_configuration(struct wpa_supplicant *wpa_s)
  817. {
  818. struct wpa_config *conf;
  819. int reconf_ctrl;
  820. int old_ap_scan;
  821. if (wpa_s->confname == NULL)
  822. return -1;
  823. conf = wpa_config_read(wpa_s->confname, NULL);
  824. if (conf == NULL) {
  825. wpa_msg(wpa_s, MSG_ERROR, "Failed to parse the configuration "
  826. "file '%s' - exiting", wpa_s->confname);
  827. return -1;
  828. }
  829. wpa_config_read(wpa_s->confanother, conf);
  830. conf->changed_parameters = (unsigned int) -1;
  831. reconf_ctrl = !!conf->ctrl_interface != !!wpa_s->conf->ctrl_interface
  832. || (conf->ctrl_interface && wpa_s->conf->ctrl_interface &&
  833. os_strcmp(conf->ctrl_interface,
  834. wpa_s->conf->ctrl_interface) != 0);
  835. if (reconf_ctrl && wpa_s->ctrl_iface) {
  836. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  837. wpa_s->ctrl_iface = NULL;
  838. }
  839. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  840. if (wpa_s->current_ssid) {
  841. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  842. wpa_s->own_disconnect_req = 1;
  843. wpa_supplicant_deauthenticate(wpa_s,
  844. WLAN_REASON_DEAUTH_LEAVING);
  845. }
  846. /*
  847. * TODO: should notify EAPOL SM about changes in opensc_engine_path,
  848. * pkcs11_engine_path, pkcs11_module_path, openssl_ciphers.
  849. */
  850. if (wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt)) {
  851. /*
  852. * Clear forced success to clear EAP state for next
  853. * authentication.
  854. */
  855. eapol_sm_notify_eap_success(wpa_s->eapol, FALSE);
  856. }
  857. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  858. wpa_sm_set_config(wpa_s->wpa, NULL);
  859. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  860. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  861. rsn_preauth_deinit(wpa_s->wpa);
  862. old_ap_scan = wpa_s->conf->ap_scan;
  863. wpa_config_free(wpa_s->conf);
  864. wpa_s->conf = conf;
  865. if (old_ap_scan != wpa_s->conf->ap_scan)
  866. wpas_notify_ap_scan_changed(wpa_s);
  867. if (reconf_ctrl)
  868. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  869. wpa_supplicant_update_config(wpa_s);
  870. wpa_supplicant_clear_status(wpa_s);
  871. if (wpa_supplicant_enabled_networks(wpa_s)) {
  872. wpa_s->reassociate = 1;
  873. wpa_supplicant_req_scan(wpa_s, 0, 0);
  874. }
  875. wpa_dbg(wpa_s, MSG_DEBUG, "Reconfiguration completed");
  876. return 0;
  877. }
  878. static void wpa_supplicant_reconfig(int sig, void *signal_ctx)
  879. {
  880. struct wpa_global *global = signal_ctx;
  881. struct wpa_supplicant *wpa_s;
  882. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  883. wpa_dbg(wpa_s, MSG_DEBUG, "Signal %d received - reconfiguring",
  884. sig);
  885. if (wpa_supplicant_reload_configuration(wpa_s) < 0) {
  886. wpa_supplicant_terminate_proc(global);
  887. }
  888. }
  889. if (wpa_debug_reopen_file() < 0) {
  890. /* Ignore errors since we cannot really do much to fix this */
  891. wpa_printf(MSG_DEBUG, "Could not reopen debug log file");
  892. }
  893. }
  894. static int wpa_supplicant_suites_from_ai(struct wpa_supplicant *wpa_s,
  895. struct wpa_ssid *ssid,
  896. struct wpa_ie_data *ie)
  897. {
  898. int ret = wpa_sm_parse_own_wpa_ie(wpa_s->wpa, ie);
  899. if (ret) {
  900. if (ret == -2) {
  901. wpa_msg(wpa_s, MSG_INFO, "WPA: Failed to parse WPA IE "
  902. "from association info");
  903. }
  904. return -1;
  905. }
  906. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Using WPA IE from AssocReq to set "
  907. "cipher suites");
  908. if (!(ie->group_cipher & ssid->group_cipher)) {
  909. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled group "
  910. "cipher 0x%x (mask 0x%x) - reject",
  911. ie->group_cipher, ssid->group_cipher);
  912. return -1;
  913. }
  914. if (!(ie->pairwise_cipher & ssid->pairwise_cipher)) {
  915. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled pairwise "
  916. "cipher 0x%x (mask 0x%x) - reject",
  917. ie->pairwise_cipher, ssid->pairwise_cipher);
  918. return -1;
  919. }
  920. if (!(ie->key_mgmt & ssid->key_mgmt)) {
  921. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver used disabled key "
  922. "management 0x%x (mask 0x%x) - reject",
  923. ie->key_mgmt, ssid->key_mgmt);
  924. return -1;
  925. }
  926. #ifdef CONFIG_IEEE80211W
  927. if (!(ie->capabilities & WPA_CAPABILITY_MFPC) &&
  928. wpas_get_ssid_pmf(wpa_s, ssid) == MGMT_FRAME_PROTECTION_REQUIRED) {
  929. wpa_msg(wpa_s, MSG_INFO, "WPA: Driver associated with an AP "
  930. "that does not support management frame protection - "
  931. "reject");
  932. return -1;
  933. }
  934. #endif /* CONFIG_IEEE80211W */
  935. return 0;
  936. }
  937. /**
  938. * wpa_supplicant_set_suites - Set authentication and encryption parameters
  939. * @wpa_s: Pointer to wpa_supplicant data
  940. * @bss: Scan results for the selected BSS, or %NULL if not available
  941. * @ssid: Configuration data for the selected network
  942. * @wpa_ie: Buffer for the WPA/RSN IE
  943. * @wpa_ie_len: Maximum wpa_ie buffer size on input. This is changed to be the
  944. * used buffer length in case the functions returns success.
  945. * Returns: 0 on success or -1 on failure
  946. *
  947. * This function is used to configure authentication and encryption parameters
  948. * based on the network configuration and scan result for the selected BSS (if
  949. * available).
  950. */
  951. int wpa_supplicant_set_suites(struct wpa_supplicant *wpa_s,
  952. struct wpa_bss *bss, struct wpa_ssid *ssid,
  953. u8 *wpa_ie, size_t *wpa_ie_len)
  954. {
  955. struct wpa_ie_data ie;
  956. int sel, proto;
  957. const u8 *bss_wpa, *bss_rsn, *bss_osen;
  958. if (bss) {
  959. bss_wpa = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  960. bss_rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  961. bss_osen = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  962. } else
  963. bss_wpa = bss_rsn = bss_osen = NULL;
  964. if (bss_rsn && (ssid->proto & WPA_PROTO_RSN) &&
  965. wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie) == 0 &&
  966. (ie.group_cipher & ssid->group_cipher) &&
  967. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  968. (ie.key_mgmt & ssid->key_mgmt)) {
  969. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using IEEE 802.11i/D9.0");
  970. proto = WPA_PROTO_RSN;
  971. } else if (bss_wpa && (ssid->proto & WPA_PROTO_WPA) &&
  972. wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie) == 0 &&
  973. (ie.group_cipher & ssid->group_cipher) &&
  974. (ie.pairwise_cipher & ssid->pairwise_cipher) &&
  975. (ie.key_mgmt & ssid->key_mgmt)) {
  976. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using IEEE 802.11i/D3.0");
  977. proto = WPA_PROTO_WPA;
  978. #ifdef CONFIG_HS20
  979. } else if (bss_osen && (ssid->proto & WPA_PROTO_OSEN)) {
  980. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using OSEN");
  981. /* TODO: parse OSEN element */
  982. os_memset(&ie, 0, sizeof(ie));
  983. ie.group_cipher = WPA_CIPHER_CCMP;
  984. ie.pairwise_cipher = WPA_CIPHER_CCMP;
  985. ie.key_mgmt = WPA_KEY_MGMT_OSEN;
  986. proto = WPA_PROTO_OSEN;
  987. #endif /* CONFIG_HS20 */
  988. } else if (bss) {
  989. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select WPA/RSN");
  990. wpa_dbg(wpa_s, MSG_DEBUG,
  991. "WPA: ssid proto=0x%x pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
  992. ssid->proto, ssid->pairwise_cipher, ssid->group_cipher,
  993. ssid->key_mgmt);
  994. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: BSS " MACSTR " ssid='%s'%s%s%s",
  995. MAC2STR(bss->bssid),
  996. wpa_ssid_txt(bss->ssid, bss->ssid_len),
  997. bss_wpa ? " WPA" : "",
  998. bss_rsn ? " RSN" : "",
  999. bss_osen ? " OSEN" : "");
  1000. if (bss_rsn) {
  1001. wpa_hexdump(MSG_DEBUG, "RSN", bss_rsn, 2 + bss_rsn[1]);
  1002. if (wpa_parse_wpa_ie(bss_rsn, 2 + bss_rsn[1], &ie)) {
  1003. wpa_dbg(wpa_s, MSG_DEBUG,
  1004. "Could not parse RSN element");
  1005. } else {
  1006. wpa_dbg(wpa_s, MSG_DEBUG,
  1007. "RSN: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
  1008. ie.pairwise_cipher, ie.group_cipher,
  1009. ie.key_mgmt);
  1010. }
  1011. }
  1012. if (bss_wpa) {
  1013. wpa_hexdump(MSG_DEBUG, "WPA", bss_wpa, 2 + bss_wpa[1]);
  1014. if (wpa_parse_wpa_ie(bss_wpa, 2 + bss_wpa[1], &ie)) {
  1015. wpa_dbg(wpa_s, MSG_DEBUG,
  1016. "Could not parse WPA element");
  1017. } else {
  1018. wpa_dbg(wpa_s, MSG_DEBUG,
  1019. "WPA: pairwise_cipher=0x%x group_cipher=0x%x key_mgmt=0x%x",
  1020. ie.pairwise_cipher, ie.group_cipher,
  1021. ie.key_mgmt);
  1022. }
  1023. }
  1024. return -1;
  1025. } else {
  1026. if (ssid->proto & WPA_PROTO_OSEN)
  1027. proto = WPA_PROTO_OSEN;
  1028. else if (ssid->proto & WPA_PROTO_RSN)
  1029. proto = WPA_PROTO_RSN;
  1030. else
  1031. proto = WPA_PROTO_WPA;
  1032. if (wpa_supplicant_suites_from_ai(wpa_s, ssid, &ie) < 0) {
  1033. os_memset(&ie, 0, sizeof(ie));
  1034. ie.group_cipher = ssid->group_cipher;
  1035. ie.pairwise_cipher = ssid->pairwise_cipher;
  1036. ie.key_mgmt = ssid->key_mgmt;
  1037. #ifdef CONFIG_IEEE80211W
  1038. ie.mgmt_group_cipher =
  1039. ssid->ieee80211w != NO_MGMT_FRAME_PROTECTION ?
  1040. WPA_CIPHER_AES_128_CMAC : 0;
  1041. #endif /* CONFIG_IEEE80211W */
  1042. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Set cipher suites "
  1043. "based on configuration");
  1044. } else
  1045. proto = ie.proto;
  1046. }
  1047. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected cipher suites: group %d "
  1048. "pairwise %d key_mgmt %d proto %d",
  1049. ie.group_cipher, ie.pairwise_cipher, ie.key_mgmt, proto);
  1050. #ifdef CONFIG_IEEE80211W
  1051. if (ssid->ieee80211w) {
  1052. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected mgmt group cipher %d",
  1053. ie.mgmt_group_cipher);
  1054. }
  1055. #endif /* CONFIG_IEEE80211W */
  1056. wpa_s->wpa_proto = proto;
  1057. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PROTO, proto);
  1058. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_RSN_ENABLED,
  1059. !!(ssid->proto & (WPA_PROTO_RSN | WPA_PROTO_OSEN)));
  1060. if (bss || !wpa_s->ap_ies_from_associnfo) {
  1061. if (wpa_sm_set_ap_wpa_ie(wpa_s->wpa, bss_wpa,
  1062. bss_wpa ? 2 + bss_wpa[1] : 0) ||
  1063. wpa_sm_set_ap_rsn_ie(wpa_s->wpa, bss_rsn,
  1064. bss_rsn ? 2 + bss_rsn[1] : 0))
  1065. return -1;
  1066. }
  1067. #ifdef CONFIG_NO_WPA
  1068. wpa_s->group_cipher = WPA_CIPHER_NONE;
  1069. wpa_s->pairwise_cipher = WPA_CIPHER_NONE;
  1070. #else /* CONFIG_NO_WPA */
  1071. sel = ie.group_cipher & ssid->group_cipher;
  1072. wpa_s->group_cipher = wpa_pick_group_cipher(sel);
  1073. if (wpa_s->group_cipher < 0) {
  1074. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select group "
  1075. "cipher");
  1076. return -1;
  1077. }
  1078. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using GTK %s",
  1079. wpa_cipher_txt(wpa_s->group_cipher));
  1080. sel = ie.pairwise_cipher & ssid->pairwise_cipher;
  1081. wpa_s->pairwise_cipher = wpa_pick_pairwise_cipher(sel, 1);
  1082. if (wpa_s->pairwise_cipher < 0) {
  1083. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select pairwise "
  1084. "cipher");
  1085. return -1;
  1086. }
  1087. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using PTK %s",
  1088. wpa_cipher_txt(wpa_s->pairwise_cipher));
  1089. #endif /* CONFIG_NO_WPA */
  1090. sel = ie.key_mgmt & ssid->key_mgmt;
  1091. #ifdef CONFIG_SAE
  1092. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE))
  1093. sel &= ~(WPA_KEY_MGMT_SAE | WPA_KEY_MGMT_FT_SAE);
  1094. #endif /* CONFIG_SAE */
  1095. if (0) {
  1096. #ifdef CONFIG_SUITEB192
  1097. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  1098. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B_192;
  1099. wpa_dbg(wpa_s, MSG_DEBUG,
  1100. "WPA: using KEY_MGMT 802.1X with Suite B (192-bit)");
  1101. #endif /* CONFIG_SUITEB192 */
  1102. #ifdef CONFIG_SUITEB
  1103. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  1104. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SUITE_B;
  1105. wpa_dbg(wpa_s, MSG_DEBUG,
  1106. "WPA: using KEY_MGMT 802.1X with Suite B");
  1107. #endif /* CONFIG_SUITEB */
  1108. #ifdef CONFIG_FILS
  1109. #ifdef CONFIG_IEEE80211R
  1110. } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA384) {
  1111. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA384;
  1112. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA384");
  1113. } else if (sel & WPA_KEY_MGMT_FT_FILS_SHA256) {
  1114. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_FILS_SHA256;
  1115. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT-FILS-SHA256");
  1116. #endif /* CONFIG_IEEE80211R */
  1117. } else if (sel & WPA_KEY_MGMT_FILS_SHA384) {
  1118. wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA384;
  1119. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA384");
  1120. } else if (sel & WPA_KEY_MGMT_FILS_SHA256) {
  1121. wpa_s->key_mgmt = WPA_KEY_MGMT_FILS_SHA256;
  1122. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FILS-SHA256");
  1123. #endif /* CONFIG_FILS */
  1124. #ifdef CONFIG_IEEE80211R
  1125. } else if (sel & WPA_KEY_MGMT_FT_IEEE8021X) {
  1126. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_IEEE8021X;
  1127. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/802.1X");
  1128. } else if (sel & WPA_KEY_MGMT_FT_PSK) {
  1129. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_PSK;
  1130. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT FT/PSK");
  1131. #endif /* CONFIG_IEEE80211R */
  1132. #ifdef CONFIG_SAE
  1133. } else if (sel & WPA_KEY_MGMT_SAE) {
  1134. wpa_s->key_mgmt = WPA_KEY_MGMT_SAE;
  1135. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT SAE");
  1136. } else if (sel & WPA_KEY_MGMT_FT_SAE) {
  1137. wpa_s->key_mgmt = WPA_KEY_MGMT_FT_SAE;
  1138. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: using KEY_MGMT FT/SAE");
  1139. #endif /* CONFIG_SAE */
  1140. #ifdef CONFIG_IEEE80211W
  1141. } else if (sel & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1142. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X_SHA256;
  1143. wpa_dbg(wpa_s, MSG_DEBUG,
  1144. "WPA: using KEY_MGMT 802.1X with SHA256");
  1145. } else if (sel & WPA_KEY_MGMT_PSK_SHA256) {
  1146. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK_SHA256;
  1147. wpa_dbg(wpa_s, MSG_DEBUG,
  1148. "WPA: using KEY_MGMT PSK with SHA256");
  1149. #endif /* CONFIG_IEEE80211W */
  1150. } else if (sel & WPA_KEY_MGMT_IEEE8021X) {
  1151. wpa_s->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
  1152. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT 802.1X");
  1153. } else if (sel & WPA_KEY_MGMT_PSK) {
  1154. wpa_s->key_mgmt = WPA_KEY_MGMT_PSK;
  1155. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-PSK");
  1156. } else if (sel & WPA_KEY_MGMT_WPA_NONE) {
  1157. wpa_s->key_mgmt = WPA_KEY_MGMT_WPA_NONE;
  1158. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using KEY_MGMT WPA-NONE");
  1159. #ifdef CONFIG_HS20
  1160. } else if (sel & WPA_KEY_MGMT_OSEN) {
  1161. wpa_s->key_mgmt = WPA_KEY_MGMT_OSEN;
  1162. wpa_dbg(wpa_s, MSG_DEBUG, "HS 2.0: using KEY_MGMT OSEN");
  1163. #endif /* CONFIG_HS20 */
  1164. } else {
  1165. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to select "
  1166. "authenticated key management type");
  1167. return -1;
  1168. }
  1169. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_KEY_MGMT, wpa_s->key_mgmt);
  1170. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_PAIRWISE,
  1171. wpa_s->pairwise_cipher);
  1172. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_GROUP, wpa_s->group_cipher);
  1173. #ifdef CONFIG_IEEE80211W
  1174. sel = ie.mgmt_group_cipher;
  1175. if (wpas_get_ssid_pmf(wpa_s, ssid) == NO_MGMT_FRAME_PROTECTION ||
  1176. !(ie.capabilities & WPA_CAPABILITY_MFPC))
  1177. sel = 0;
  1178. if (sel & WPA_CIPHER_AES_128_CMAC) {
  1179. wpa_s->mgmt_group_cipher = WPA_CIPHER_AES_128_CMAC;
  1180. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1181. "AES-128-CMAC");
  1182. } else if (sel & WPA_CIPHER_BIP_GMAC_128) {
  1183. wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_128;
  1184. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1185. "BIP-GMAC-128");
  1186. } else if (sel & WPA_CIPHER_BIP_GMAC_256) {
  1187. wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_GMAC_256;
  1188. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1189. "BIP-GMAC-256");
  1190. } else if (sel & WPA_CIPHER_BIP_CMAC_256) {
  1191. wpa_s->mgmt_group_cipher = WPA_CIPHER_BIP_CMAC_256;
  1192. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: using MGMT group cipher "
  1193. "BIP-CMAC-256");
  1194. } else {
  1195. wpa_s->mgmt_group_cipher = 0;
  1196. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: not using MGMT group cipher");
  1197. }
  1198. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MGMT_GROUP,
  1199. wpa_s->mgmt_group_cipher);
  1200. wpa_sm_set_param(wpa_s->wpa, WPA_PARAM_MFP,
  1201. wpas_get_ssid_pmf(wpa_s, ssid));
  1202. #endif /* CONFIG_IEEE80211W */
  1203. if (wpa_sm_set_assoc_wpa_ie_default(wpa_s->wpa, wpa_ie, wpa_ie_len)) {
  1204. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to generate WPA IE");
  1205. return -1;
  1206. }
  1207. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt)) {
  1208. int psk_set = 0;
  1209. if (ssid->psk_set) {
  1210. wpa_sm_set_pmk(wpa_s->wpa, ssid->psk, PMK_LEN, NULL,
  1211. NULL);
  1212. psk_set = 1;
  1213. }
  1214. #ifndef CONFIG_NO_PBKDF2
  1215. if (bss && ssid->bssid_set && ssid->ssid_len == 0 &&
  1216. ssid->passphrase) {
  1217. u8 psk[PMK_LEN];
  1218. pbkdf2_sha1(ssid->passphrase, bss->ssid, bss->ssid_len,
  1219. 4096, psk, PMK_LEN);
  1220. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from passphrase)",
  1221. psk, PMK_LEN);
  1222. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL, NULL);
  1223. psk_set = 1;
  1224. os_memset(psk, 0, sizeof(psk));
  1225. }
  1226. #endif /* CONFIG_NO_PBKDF2 */
  1227. #ifdef CONFIG_EXT_PASSWORD
  1228. if (ssid->ext_psk) {
  1229. struct wpabuf *pw = ext_password_get(wpa_s->ext_pw,
  1230. ssid->ext_psk);
  1231. char pw_str[64 + 1];
  1232. u8 psk[PMK_LEN];
  1233. if (pw == NULL) {
  1234. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No PSK "
  1235. "found from external storage");
  1236. return -1;
  1237. }
  1238. if (wpabuf_len(pw) < 8 || wpabuf_len(pw) > 64) {
  1239. wpa_msg(wpa_s, MSG_INFO, "EXT PW: Unexpected "
  1240. "PSK length %d in external storage",
  1241. (int) wpabuf_len(pw));
  1242. ext_password_free(pw);
  1243. return -1;
  1244. }
  1245. os_memcpy(pw_str, wpabuf_head(pw), wpabuf_len(pw));
  1246. pw_str[wpabuf_len(pw)] = '\0';
  1247. #ifndef CONFIG_NO_PBKDF2
  1248. if (wpabuf_len(pw) >= 8 && wpabuf_len(pw) < 64 && bss)
  1249. {
  1250. pbkdf2_sha1(pw_str, bss->ssid, bss->ssid_len,
  1251. 4096, psk, PMK_LEN);
  1252. os_memset(pw_str, 0, sizeof(pw_str));
  1253. wpa_hexdump_key(MSG_MSGDUMP, "PSK (from "
  1254. "external passphrase)",
  1255. psk, PMK_LEN);
  1256. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
  1257. NULL);
  1258. psk_set = 1;
  1259. os_memset(psk, 0, sizeof(psk));
  1260. } else
  1261. #endif /* CONFIG_NO_PBKDF2 */
  1262. if (wpabuf_len(pw) == 2 * PMK_LEN) {
  1263. if (hexstr2bin(pw_str, psk, PMK_LEN) < 0) {
  1264. wpa_msg(wpa_s, MSG_INFO, "EXT PW: "
  1265. "Invalid PSK hex string");
  1266. os_memset(pw_str, 0, sizeof(pw_str));
  1267. ext_password_free(pw);
  1268. return -1;
  1269. }
  1270. wpa_sm_set_pmk(wpa_s->wpa, psk, PMK_LEN, NULL,
  1271. NULL);
  1272. psk_set = 1;
  1273. os_memset(psk, 0, sizeof(psk));
  1274. } else {
  1275. wpa_msg(wpa_s, MSG_INFO, "EXT PW: No suitable "
  1276. "PSK available");
  1277. os_memset(pw_str, 0, sizeof(pw_str));
  1278. ext_password_free(pw);
  1279. return -1;
  1280. }
  1281. os_memset(pw_str, 0, sizeof(pw_str));
  1282. ext_password_free(pw);
  1283. }
  1284. #endif /* CONFIG_EXT_PASSWORD */
  1285. if (!psk_set) {
  1286. wpa_msg(wpa_s, MSG_INFO,
  1287. "No PSK available for association");
  1288. return -1;
  1289. }
  1290. } else
  1291. wpa_sm_set_pmk_from_pmksa(wpa_s->wpa);
  1292. return 0;
  1293. }
  1294. static void wpas_ext_capab_byte(struct wpa_supplicant *wpa_s, u8 *pos, int idx)
  1295. {
  1296. *pos = 0x00;
  1297. switch (idx) {
  1298. case 0: /* Bits 0-7 */
  1299. break;
  1300. case 1: /* Bits 8-15 */
  1301. break;
  1302. case 2: /* Bits 16-23 */
  1303. #ifdef CONFIG_WNM
  1304. *pos |= 0x02; /* Bit 17 - WNM-Sleep Mode */
  1305. *pos |= 0x08; /* Bit 19 - BSS Transition */
  1306. #endif /* CONFIG_WNM */
  1307. break;
  1308. case 3: /* Bits 24-31 */
  1309. #ifdef CONFIG_WNM
  1310. *pos |= 0x02; /* Bit 25 - SSID List */
  1311. #endif /* CONFIG_WNM */
  1312. #ifdef CONFIG_INTERWORKING
  1313. if (wpa_s->conf->interworking)
  1314. *pos |= 0x80; /* Bit 31 - Interworking */
  1315. #endif /* CONFIG_INTERWORKING */
  1316. break;
  1317. case 4: /* Bits 32-39 */
  1318. #ifdef CONFIG_INTERWORKING
  1319. if (wpa_s->drv_flags / WPA_DRIVER_FLAGS_QOS_MAPPING)
  1320. *pos |= 0x01; /* Bit 32 - QoS Map */
  1321. #endif /* CONFIG_INTERWORKING */
  1322. break;
  1323. case 5: /* Bits 40-47 */
  1324. #ifdef CONFIG_HS20
  1325. if (wpa_s->conf->hs20)
  1326. *pos |= 0x40; /* Bit 46 - WNM-Notification */
  1327. #endif /* CONFIG_HS20 */
  1328. #ifdef CONFIG_MBO
  1329. *pos |= 0x40; /* Bit 46 - WNM-Notification */
  1330. #endif /* CONFIG_MBO */
  1331. break;
  1332. case 6: /* Bits 48-55 */
  1333. break;
  1334. case 7: /* Bits 56-63 */
  1335. break;
  1336. case 8: /* Bits 64-71 */
  1337. if (wpa_s->conf->ftm_responder)
  1338. *pos |= 0x40; /* Bit 70 - FTM responder */
  1339. if (wpa_s->conf->ftm_initiator)
  1340. *pos |= 0x80; /* Bit 71 - FTM initiator */
  1341. break;
  1342. case 9: /* Bits 72-79 */
  1343. #ifdef CONFIG_FILS
  1344. *pos |= 0x01;
  1345. #endif /* CONFIG_FILS */
  1346. break;
  1347. }
  1348. }
  1349. int wpas_build_ext_capab(struct wpa_supplicant *wpa_s, u8 *buf, size_t buflen)
  1350. {
  1351. u8 *pos = buf;
  1352. u8 len = 10, i;
  1353. if (len < wpa_s->extended_capa_len)
  1354. len = wpa_s->extended_capa_len;
  1355. if (buflen < (size_t) len + 2) {
  1356. wpa_printf(MSG_INFO,
  1357. "Not enough room for building extended capabilities element");
  1358. return -1;
  1359. }
  1360. *pos++ = WLAN_EID_EXT_CAPAB;
  1361. *pos++ = len;
  1362. for (i = 0; i < len; i++, pos++) {
  1363. wpas_ext_capab_byte(wpa_s, pos, i);
  1364. if (i < wpa_s->extended_capa_len) {
  1365. *pos &= ~wpa_s->extended_capa_mask[i];
  1366. *pos |= wpa_s->extended_capa[i];
  1367. }
  1368. }
  1369. while (len > 0 && buf[1 + len] == 0) {
  1370. len--;
  1371. buf[1] = len;
  1372. }
  1373. if (len == 0)
  1374. return 0;
  1375. return 2 + len;
  1376. }
  1377. static int wpas_valid_bss(struct wpa_supplicant *wpa_s,
  1378. struct wpa_bss *test_bss)
  1379. {
  1380. struct wpa_bss *bss;
  1381. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  1382. if (bss == test_bss)
  1383. return 1;
  1384. }
  1385. return 0;
  1386. }
  1387. static int wpas_valid_ssid(struct wpa_supplicant *wpa_s,
  1388. struct wpa_ssid *test_ssid)
  1389. {
  1390. struct wpa_ssid *ssid;
  1391. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
  1392. if (ssid == test_ssid)
  1393. return 1;
  1394. }
  1395. return 0;
  1396. }
  1397. int wpas_valid_bss_ssid(struct wpa_supplicant *wpa_s, struct wpa_bss *test_bss,
  1398. struct wpa_ssid *test_ssid)
  1399. {
  1400. if (test_bss && !wpas_valid_bss(wpa_s, test_bss))
  1401. return 0;
  1402. return test_ssid == NULL || wpas_valid_ssid(wpa_s, test_ssid);
  1403. }
  1404. void wpas_connect_work_free(struct wpa_connect_work *cwork)
  1405. {
  1406. if (cwork == NULL)
  1407. return;
  1408. os_free(cwork);
  1409. }
  1410. void wpas_connect_work_done(struct wpa_supplicant *wpa_s)
  1411. {
  1412. struct wpa_connect_work *cwork;
  1413. struct wpa_radio_work *work = wpa_s->connect_work;
  1414. if (!work)
  1415. return;
  1416. wpa_s->connect_work = NULL;
  1417. cwork = work->ctx;
  1418. work->ctx = NULL;
  1419. wpas_connect_work_free(cwork);
  1420. radio_work_done(work);
  1421. }
  1422. int wpas_update_random_addr(struct wpa_supplicant *wpa_s, int style)
  1423. {
  1424. struct os_reltime now;
  1425. u8 addr[ETH_ALEN];
  1426. os_get_reltime(&now);
  1427. if (wpa_s->last_mac_addr_style == style &&
  1428. wpa_s->last_mac_addr_change.sec != 0 &&
  1429. !os_reltime_expired(&now, &wpa_s->last_mac_addr_change,
  1430. wpa_s->conf->rand_addr_lifetime)) {
  1431. wpa_msg(wpa_s, MSG_DEBUG,
  1432. "Previously selected random MAC address has not yet expired");
  1433. return 0;
  1434. }
  1435. switch (style) {
  1436. case 1:
  1437. if (random_mac_addr(addr) < 0)
  1438. return -1;
  1439. break;
  1440. case 2:
  1441. os_memcpy(addr, wpa_s->perm_addr, ETH_ALEN);
  1442. if (random_mac_addr_keep_oui(addr) < 0)
  1443. return -1;
  1444. break;
  1445. default:
  1446. return -1;
  1447. }
  1448. if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
  1449. wpa_msg(wpa_s, MSG_INFO,
  1450. "Failed to set random MAC address");
  1451. return -1;
  1452. }
  1453. os_get_reltime(&wpa_s->last_mac_addr_change);
  1454. wpa_s->mac_addr_changed = 1;
  1455. wpa_s->last_mac_addr_style = style;
  1456. if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
  1457. wpa_msg(wpa_s, MSG_INFO,
  1458. "Could not update MAC address information");
  1459. return -1;
  1460. }
  1461. wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
  1462. MAC2STR(addr));
  1463. return 0;
  1464. }
  1465. int wpas_update_random_addr_disassoc(struct wpa_supplicant *wpa_s)
  1466. {
  1467. if (wpa_s->wpa_state >= WPA_AUTHENTICATING ||
  1468. !wpa_s->conf->preassoc_mac_addr)
  1469. return 0;
  1470. return wpas_update_random_addr(wpa_s, wpa_s->conf->preassoc_mac_addr);
  1471. }
  1472. static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit);
  1473. /**
  1474. * wpa_supplicant_associate - Request association
  1475. * @wpa_s: Pointer to wpa_supplicant data
  1476. * @bss: Scan results for the selected BSS, or %NULL if not available
  1477. * @ssid: Configuration data for the selected network
  1478. *
  1479. * This function is used to request %wpa_supplicant to associate with a BSS.
  1480. */
  1481. void wpa_supplicant_associate(struct wpa_supplicant *wpa_s,
  1482. struct wpa_bss *bss, struct wpa_ssid *ssid)
  1483. {
  1484. struct wpa_connect_work *cwork;
  1485. int rand_style;
  1486. wpa_s->own_disconnect_req = 0;
  1487. /*
  1488. * If we are starting a new connection, any previously pending EAPOL
  1489. * RX cannot be valid anymore.
  1490. */
  1491. wpabuf_free(wpa_s->pending_eapol_rx);
  1492. wpa_s->pending_eapol_rx = NULL;
  1493. if (ssid->mac_addr == -1)
  1494. rand_style = wpa_s->conf->mac_addr;
  1495. else
  1496. rand_style = ssid->mac_addr;
  1497. wmm_ac_clear_saved_tspecs(wpa_s);
  1498. wpa_s->reassoc_same_bss = 0;
  1499. wpa_s->reassoc_same_ess = 0;
  1500. if (wpa_s->last_ssid == ssid) {
  1501. wpa_dbg(wpa_s, MSG_DEBUG, "Re-association to the same ESS");
  1502. wpa_s->reassoc_same_ess = 1;
  1503. if (wpa_s->current_bss && wpa_s->current_bss == bss) {
  1504. wmm_ac_save_tspecs(wpa_s);
  1505. wpa_s->reassoc_same_bss = 1;
  1506. }
  1507. }
  1508. if (rand_style > 0 && !wpa_s->reassoc_same_ess) {
  1509. if (wpas_update_random_addr(wpa_s, rand_style) < 0)
  1510. return;
  1511. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  1512. } else if (rand_style == 0 && wpa_s->mac_addr_changed) {
  1513. if (wpa_drv_set_mac_addr(wpa_s, NULL) < 0) {
  1514. wpa_msg(wpa_s, MSG_INFO,
  1515. "Could not restore permanent MAC address");
  1516. return;
  1517. }
  1518. wpa_s->mac_addr_changed = 0;
  1519. if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
  1520. wpa_msg(wpa_s, MSG_INFO,
  1521. "Could not update MAC address information");
  1522. return;
  1523. }
  1524. wpa_msg(wpa_s, MSG_DEBUG, "Using permanent MAC address");
  1525. }
  1526. wpa_s->last_ssid = ssid;
  1527. #ifdef CONFIG_IBSS_RSN
  1528. ibss_rsn_deinit(wpa_s->ibss_rsn);
  1529. wpa_s->ibss_rsn = NULL;
  1530. #else /* CONFIG_IBSS_RSN */
  1531. if (ssid->mode == WPAS_MODE_IBSS &&
  1532. !(ssid->key_mgmt & (WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPA_NONE))) {
  1533. wpa_msg(wpa_s, MSG_INFO,
  1534. "IBSS RSN not supported in the build");
  1535. return;
  1536. }
  1537. #endif /* CONFIG_IBSS_RSN */
  1538. if (ssid->mode == WPAS_MODE_AP || ssid->mode == WPAS_MODE_P2P_GO ||
  1539. ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
  1540. #ifdef CONFIG_AP
  1541. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_AP)) {
  1542. wpa_msg(wpa_s, MSG_INFO, "Driver does not support AP "
  1543. "mode");
  1544. return;
  1545. }
  1546. if (wpa_supplicant_create_ap(wpa_s, ssid) < 0) {
  1547. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  1548. if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION)
  1549. wpas_p2p_ap_setup_failed(wpa_s);
  1550. return;
  1551. }
  1552. wpa_s->current_bss = bss;
  1553. #else /* CONFIG_AP */
  1554. wpa_msg(wpa_s, MSG_ERROR, "AP mode support not included in "
  1555. "the build");
  1556. #endif /* CONFIG_AP */
  1557. return;
  1558. }
  1559. if (ssid->mode == WPAS_MODE_MESH) {
  1560. #ifdef CONFIG_MESH
  1561. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MESH)) {
  1562. wpa_msg(wpa_s, MSG_INFO,
  1563. "Driver does not support mesh mode");
  1564. return;
  1565. }
  1566. if (bss)
  1567. ssid->frequency = bss->freq;
  1568. if (wpa_supplicant_join_mesh(wpa_s, ssid) < 0) {
  1569. wpa_msg(wpa_s, MSG_ERROR, "Could not join mesh");
  1570. return;
  1571. }
  1572. wpa_s->current_bss = bss;
  1573. wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_STARTED "ssid=\"%s\" id=%d",
  1574. wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  1575. ssid->id);
  1576. #else /* CONFIG_MESH */
  1577. wpa_msg(wpa_s, MSG_ERROR,
  1578. "mesh mode support not included in the build");
  1579. #endif /* CONFIG_MESH */
  1580. return;
  1581. }
  1582. #ifdef CONFIG_TDLS
  1583. if (bss)
  1584. wpa_tdls_ap_ies(wpa_s->wpa, (const u8 *) (bss + 1),
  1585. bss->ie_len);
  1586. #endif /* CONFIG_TDLS */
  1587. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_SME) &&
  1588. ssid->mode == IEEE80211_MODE_INFRA) {
  1589. sme_authenticate(wpa_s, bss, ssid);
  1590. return;
  1591. }
  1592. if (wpa_s->connect_work) {
  1593. wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since connect_work exist");
  1594. return;
  1595. }
  1596. if (radio_work_pending(wpa_s, "connect")) {
  1597. wpa_dbg(wpa_s, MSG_DEBUG, "Reject wpa_supplicant_associate() call since pending work exist");
  1598. return;
  1599. }
  1600. #ifdef CONFIG_SME
  1601. if (ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) {
  1602. /* Clear possibly set auth_alg, if any, from last attempt. */
  1603. wpa_s->sme.auth_alg = WPA_AUTH_ALG_OPEN;
  1604. }
  1605. #endif /* CONFIG_SME */
  1606. wpas_abort_ongoing_scan(wpa_s);
  1607. cwork = os_zalloc(sizeof(*cwork));
  1608. if (cwork == NULL)
  1609. return;
  1610. cwork->bss = bss;
  1611. cwork->ssid = ssid;
  1612. if (radio_add_work(wpa_s, bss ? bss->freq : 0, "connect", 1,
  1613. wpas_start_assoc_cb, cwork) < 0) {
  1614. os_free(cwork);
  1615. }
  1616. }
  1617. static int bss_is_ibss(struct wpa_bss *bss)
  1618. {
  1619. return (bss->caps & (IEEE80211_CAP_ESS | IEEE80211_CAP_IBSS)) ==
  1620. IEEE80211_CAP_IBSS;
  1621. }
  1622. static int drv_supports_vht(struct wpa_supplicant *wpa_s,
  1623. const struct wpa_ssid *ssid)
  1624. {
  1625. enum hostapd_hw_mode hw_mode;
  1626. struct hostapd_hw_modes *mode = NULL;
  1627. u8 channel;
  1628. int i;
  1629. hw_mode = ieee80211_freq_to_chan(ssid->frequency, &channel);
  1630. if (hw_mode == NUM_HOSTAPD_MODES)
  1631. return 0;
  1632. for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
  1633. if (wpa_s->hw.modes[i].mode == hw_mode) {
  1634. mode = &wpa_s->hw.modes[i];
  1635. break;
  1636. }
  1637. }
  1638. if (!mode)
  1639. return 0;
  1640. return mode->vht_capab != 0;
  1641. }
  1642. void ibss_mesh_setup_freq(struct wpa_supplicant *wpa_s,
  1643. const struct wpa_ssid *ssid,
  1644. struct hostapd_freq_params *freq)
  1645. {
  1646. enum hostapd_hw_mode hw_mode;
  1647. struct hostapd_hw_modes *mode = NULL;
  1648. int ht40plus[] = { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157,
  1649. 184, 192 };
  1650. int vht80[] = { 36, 52, 100, 116, 132, 149 };
  1651. struct hostapd_channel_data *pri_chan = NULL, *sec_chan = NULL;
  1652. u8 channel;
  1653. int i, chan_idx, ht40 = -1, res, obss_scan = 1;
  1654. unsigned int j, k;
  1655. struct hostapd_freq_params vht_freq;
  1656. int chwidth, seg0, seg1;
  1657. u32 vht_caps = 0;
  1658. freq->freq = ssid->frequency;
  1659. for (j = 0; j < wpa_s->last_scan_res_used; j++) {
  1660. struct wpa_bss *bss = wpa_s->last_scan_res[j];
  1661. if (ssid->mode != WPAS_MODE_IBSS)
  1662. break;
  1663. /* Don't adjust control freq in case of fixed_freq */
  1664. if (ssid->fixed_freq)
  1665. break;
  1666. if (!bss_is_ibss(bss))
  1667. continue;
  1668. if (ssid->ssid_len == bss->ssid_len &&
  1669. os_memcmp(ssid->ssid, bss->ssid, bss->ssid_len) == 0) {
  1670. wpa_printf(MSG_DEBUG,
  1671. "IBSS already found in scan results, adjust control freq: %d",
  1672. bss->freq);
  1673. freq->freq = bss->freq;
  1674. obss_scan = 0;
  1675. break;
  1676. }
  1677. }
  1678. /* For IBSS check HT_IBSS flag */
  1679. if (ssid->mode == WPAS_MODE_IBSS &&
  1680. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_HT_IBSS))
  1681. return;
  1682. if (wpa_s->group_cipher == WPA_CIPHER_WEP40 ||
  1683. wpa_s->group_cipher == WPA_CIPHER_WEP104 ||
  1684. wpa_s->pairwise_cipher == WPA_CIPHER_TKIP) {
  1685. wpa_printf(MSG_DEBUG,
  1686. "IBSS: WEP/TKIP detected, do not try to enable HT");
  1687. return;
  1688. }
  1689. hw_mode = ieee80211_freq_to_chan(freq->freq, &channel);
  1690. for (i = 0; wpa_s->hw.modes && i < wpa_s->hw.num_modes; i++) {
  1691. if (wpa_s->hw.modes[i].mode == hw_mode) {
  1692. mode = &wpa_s->hw.modes[i];
  1693. break;
  1694. }
  1695. }
  1696. if (!mode)
  1697. return;
  1698. #ifdef CONFIG_HT_OVERRIDES
  1699. if (ssid->disable_ht) {
  1700. freq->ht_enabled = 0;
  1701. return;
  1702. }
  1703. #endif /* CONFIG_HT_OVERRIDES */
  1704. freq->ht_enabled = ht_supported(mode);
  1705. if (!freq->ht_enabled)
  1706. return;
  1707. /* Setup higher BW only for 5 GHz */
  1708. if (mode->mode != HOSTAPD_MODE_IEEE80211A)
  1709. return;
  1710. for (chan_idx = 0; chan_idx < mode->num_channels; chan_idx++) {
  1711. pri_chan = &mode->channels[chan_idx];
  1712. if (pri_chan->chan == channel)
  1713. break;
  1714. pri_chan = NULL;
  1715. }
  1716. if (!pri_chan)
  1717. return;
  1718. /* Check primary channel flags */
  1719. if (pri_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
  1720. return;
  1721. #ifdef CONFIG_HT_OVERRIDES
  1722. if (ssid->disable_ht40)
  1723. return;
  1724. #endif /* CONFIG_HT_OVERRIDES */
  1725. /* Check/setup HT40+/HT40- */
  1726. for (j = 0; j < ARRAY_SIZE(ht40plus); j++) {
  1727. if (ht40plus[j] == channel) {
  1728. ht40 = 1;
  1729. break;
  1730. }
  1731. }
  1732. /* Find secondary channel */
  1733. for (i = 0; i < mode->num_channels; i++) {
  1734. sec_chan = &mode->channels[i];
  1735. if (sec_chan->chan == channel + ht40 * 4)
  1736. break;
  1737. sec_chan = NULL;
  1738. }
  1739. if (!sec_chan)
  1740. return;
  1741. /* Check secondary channel flags */
  1742. if (sec_chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
  1743. return;
  1744. freq->channel = pri_chan->chan;
  1745. if (ht40 == -1) {
  1746. if (!(pri_chan->flag & HOSTAPD_CHAN_HT40MINUS))
  1747. return;
  1748. } else {
  1749. if (!(pri_chan->flag & HOSTAPD_CHAN_HT40PLUS))
  1750. return;
  1751. }
  1752. freq->sec_channel_offset = ht40;
  1753. if (obss_scan) {
  1754. struct wpa_scan_results *scan_res;
  1755. scan_res = wpa_supplicant_get_scan_results(wpa_s, NULL, 0);
  1756. if (scan_res == NULL) {
  1757. /* Back to HT20 */
  1758. freq->sec_channel_offset = 0;
  1759. return;
  1760. }
  1761. res = check_40mhz_5g(mode, scan_res, pri_chan->chan,
  1762. sec_chan->chan);
  1763. switch (res) {
  1764. case 0:
  1765. /* Back to HT20 */
  1766. freq->sec_channel_offset = 0;
  1767. break;
  1768. case 1:
  1769. /* Configuration allowed */
  1770. break;
  1771. case 2:
  1772. /* Switch pri/sec channels */
  1773. freq->freq = hw_get_freq(mode, sec_chan->chan);
  1774. freq->sec_channel_offset = -freq->sec_channel_offset;
  1775. freq->channel = sec_chan->chan;
  1776. break;
  1777. default:
  1778. freq->sec_channel_offset = 0;
  1779. break;
  1780. }
  1781. wpa_scan_results_free(scan_res);
  1782. }
  1783. wpa_printf(MSG_DEBUG,
  1784. "IBSS/mesh: setup freq channel %d, sec_channel_offset %d",
  1785. freq->channel, freq->sec_channel_offset);
  1786. if (!drv_supports_vht(wpa_s, ssid))
  1787. return;
  1788. /* For IBSS check VHT_IBSS flag */
  1789. if (ssid->mode == WPAS_MODE_IBSS &&
  1790. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_VHT_IBSS))
  1791. return;
  1792. vht_freq = *freq;
  1793. #ifdef CONFIG_VHT_OVERRIDES
  1794. if (ssid->disable_vht) {
  1795. freq->vht_enabled = 0;
  1796. return;
  1797. }
  1798. #endif /* CONFIG_VHT_OVERRIDES */
  1799. vht_freq.vht_enabled = vht_supported(mode);
  1800. if (!vht_freq.vht_enabled)
  1801. return;
  1802. /* setup center_freq1, bandwidth */
  1803. for (j = 0; j < ARRAY_SIZE(vht80); j++) {
  1804. if (freq->channel >= vht80[j] &&
  1805. freq->channel < vht80[j] + 16)
  1806. break;
  1807. }
  1808. if (j == ARRAY_SIZE(vht80))
  1809. return;
  1810. for (i = vht80[j]; i < vht80[j] + 16; i += 4) {
  1811. struct hostapd_channel_data *chan;
  1812. chan = hw_get_channel_chan(mode, i, NULL);
  1813. if (!chan)
  1814. return;
  1815. /* Back to HT configuration if channel not usable */
  1816. if (chan->flag & (HOSTAPD_CHAN_DISABLED | HOSTAPD_CHAN_NO_IR))
  1817. return;
  1818. }
  1819. chwidth = VHT_CHANWIDTH_80MHZ;
  1820. seg0 = vht80[j] + 6;
  1821. seg1 = 0;
  1822. if (ssid->max_oper_chwidth == VHT_CHANWIDTH_80P80MHZ) {
  1823. /* setup center_freq2, bandwidth */
  1824. for (k = 0; k < ARRAY_SIZE(vht80); k++) {
  1825. /* Only accept 80 MHz segments separated by a gap */
  1826. if (j == k || abs(vht80[j] - vht80[k]) == 16)
  1827. continue;
  1828. for (i = vht80[k]; i < vht80[k] + 16; i += 4) {
  1829. struct hostapd_channel_data *chan;
  1830. chan = hw_get_channel_chan(mode, i, NULL);
  1831. if (!chan)
  1832. continue;
  1833. if (chan->flag & (HOSTAPD_CHAN_DISABLED |
  1834. HOSTAPD_CHAN_NO_IR |
  1835. HOSTAPD_CHAN_RADAR))
  1836. continue;
  1837. /* Found a suitable second segment for 80+80 */
  1838. chwidth = VHT_CHANWIDTH_80P80MHZ;
  1839. vht_caps |=
  1840. VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
  1841. seg1 = vht80[k] + 6;
  1842. }
  1843. if (chwidth == VHT_CHANWIDTH_80P80MHZ)
  1844. break;
  1845. }
  1846. } else if (ssid->max_oper_chwidth == VHT_CHANWIDTH_160MHZ) {
  1847. if (freq->freq == 5180) {
  1848. chwidth = VHT_CHANWIDTH_160MHZ;
  1849. vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  1850. seg0 = 50;
  1851. } else if (freq->freq == 5520) {
  1852. chwidth = VHT_CHANWIDTH_160MHZ;
  1853. vht_caps |= VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
  1854. seg0 = 114;
  1855. }
  1856. }
  1857. if (hostapd_set_freq_params(&vht_freq, mode->mode, freq->freq,
  1858. freq->channel, freq->ht_enabled,
  1859. vht_freq.vht_enabled,
  1860. freq->sec_channel_offset,
  1861. chwidth, seg0, seg1, vht_caps) != 0)
  1862. return;
  1863. *freq = vht_freq;
  1864. wpa_printf(MSG_DEBUG, "IBSS: VHT setup freq cf1 %d, cf2 %d, bw %d",
  1865. freq->center_freq1, freq->center_freq2, freq->bandwidth);
  1866. }
  1867. static void wpas_start_assoc_cb(struct wpa_radio_work *work, int deinit)
  1868. {
  1869. struct wpa_connect_work *cwork = work->ctx;
  1870. struct wpa_bss *bss = cwork->bss;
  1871. struct wpa_ssid *ssid = cwork->ssid;
  1872. struct wpa_supplicant *wpa_s = work->wpa_s;
  1873. u8 wpa_ie[200];
  1874. size_t wpa_ie_len;
  1875. int use_crypt, ret, i, bssid_changed;
  1876. int algs = WPA_AUTH_ALG_OPEN;
  1877. unsigned int cipher_pairwise, cipher_group;
  1878. struct wpa_driver_associate_params params;
  1879. int wep_keys_set = 0;
  1880. int assoc_failed = 0;
  1881. struct wpa_ssid *old_ssid;
  1882. u8 prev_bssid[ETH_ALEN];
  1883. #ifdef CONFIG_HT_OVERRIDES
  1884. struct ieee80211_ht_capabilities htcaps;
  1885. struct ieee80211_ht_capabilities htcaps_mask;
  1886. #endif /* CONFIG_HT_OVERRIDES */
  1887. #ifdef CONFIG_VHT_OVERRIDES
  1888. struct ieee80211_vht_capabilities vhtcaps;
  1889. struct ieee80211_vht_capabilities vhtcaps_mask;
  1890. #endif /* CONFIG_VHT_OVERRIDES */
  1891. if (deinit) {
  1892. if (work->started) {
  1893. wpa_s->connect_work = NULL;
  1894. /* cancel possible auth. timeout */
  1895. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s,
  1896. NULL);
  1897. }
  1898. wpas_connect_work_free(cwork);
  1899. return;
  1900. }
  1901. wpa_s->connect_work = work;
  1902. if (cwork->bss_removed || !wpas_valid_bss_ssid(wpa_s, bss, ssid) ||
  1903. wpas_network_disabled(wpa_s, ssid)) {
  1904. wpa_dbg(wpa_s, MSG_DEBUG, "BSS/SSID entry for association not valid anymore - drop connection attempt");
  1905. wpas_connect_work_done(wpa_s);
  1906. return;
  1907. }
  1908. os_memcpy(prev_bssid, wpa_s->bssid, ETH_ALEN);
  1909. os_memset(&params, 0, sizeof(params));
  1910. wpa_s->reassociate = 0;
  1911. wpa_s->eap_expected_failure = 0;
  1912. if (bss &&
  1913. (!wpas_driver_bss_selection(wpa_s) || wpas_wps_searching(wpa_s))) {
  1914. #ifdef CONFIG_IEEE80211R
  1915. const u8 *ie, *md = NULL;
  1916. #endif /* CONFIG_IEEE80211R */
  1917. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with " MACSTR
  1918. " (SSID='%s' freq=%d MHz)", MAC2STR(bss->bssid),
  1919. wpa_ssid_txt(bss->ssid, bss->ssid_len), bss->freq);
  1920. bssid_changed = !is_zero_ether_addr(wpa_s->bssid);
  1921. os_memset(wpa_s->bssid, 0, ETH_ALEN);
  1922. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1923. if (bssid_changed)
  1924. wpas_notify_bssid_changed(wpa_s);
  1925. #ifdef CONFIG_IEEE80211R
  1926. ie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  1927. if (ie && ie[1] >= MOBILITY_DOMAIN_ID_LEN)
  1928. md = ie + 2;
  1929. wpa_sm_set_ft_params(wpa_s->wpa, ie, ie ? 2 + ie[1] : 0);
  1930. if (md) {
  1931. /* Prepare for the next transition */
  1932. wpa_ft_prepare_auth_request(wpa_s->wpa, ie);
  1933. }
  1934. #endif /* CONFIG_IEEE80211R */
  1935. #ifdef CONFIG_WPS
  1936. } else if ((ssid->ssid == NULL || ssid->ssid_len == 0) &&
  1937. wpa_s->conf->ap_scan == 2 &&
  1938. (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
  1939. /* Use ap_scan==1 style network selection to find the network
  1940. */
  1941. wpas_connect_work_done(wpa_s);
  1942. wpa_s->scan_req = MANUAL_SCAN_REQ;
  1943. wpa_s->reassociate = 1;
  1944. wpa_supplicant_req_scan(wpa_s, 0, 0);
  1945. return;
  1946. #endif /* CONFIG_WPS */
  1947. } else {
  1948. wpa_msg(wpa_s, MSG_INFO, "Trying to associate with SSID '%s'",
  1949. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1950. if (bss)
  1951. os_memcpy(wpa_s->pending_bssid, bss->bssid, ETH_ALEN);
  1952. else
  1953. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  1954. }
  1955. if (!wpa_s->pno)
  1956. wpa_supplicant_cancel_sched_scan(wpa_s);
  1957. wpa_supplicant_cancel_scan(wpa_s);
  1958. /* Starting new association, so clear the possibly used WPA IE from the
  1959. * previous association. */
  1960. wpa_sm_set_assoc_wpa_ie(wpa_s->wpa, NULL, 0);
  1961. #ifdef IEEE8021X_EAPOL
  1962. if (ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1963. if (ssid->leap) {
  1964. if (ssid->non_leap == 0)
  1965. algs = WPA_AUTH_ALG_LEAP;
  1966. else
  1967. algs |= WPA_AUTH_ALG_LEAP;
  1968. }
  1969. }
  1970. #endif /* IEEE8021X_EAPOL */
  1971. wpa_dbg(wpa_s, MSG_DEBUG, "Automatic auth_alg selection: 0x%x", algs);
  1972. if (ssid->auth_alg) {
  1973. algs = ssid->auth_alg;
  1974. wpa_dbg(wpa_s, MSG_DEBUG, "Overriding auth_alg selection: "
  1975. "0x%x", algs);
  1976. }
  1977. if (bss && (wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE) ||
  1978. wpa_bss_get_ie(bss, WLAN_EID_RSN)) &&
  1979. wpa_key_mgmt_wpa(ssid->key_mgmt)) {
  1980. int try_opportunistic;
  1981. try_opportunistic = (ssid->proactive_key_caching < 0 ?
  1982. wpa_s->conf->okc :
  1983. ssid->proactive_key_caching) &&
  1984. (ssid->proto & WPA_PROTO_RSN);
  1985. if (pmksa_cache_set_current(wpa_s->wpa, NULL, bss->bssid,
  1986. ssid, try_opportunistic, NULL) == 0)
  1987. eapol_sm_notify_pmkid_attempt(wpa_s->eapol);
  1988. wpa_ie_len = sizeof(wpa_ie);
  1989. if (wpa_supplicant_set_suites(wpa_s, bss, ssid,
  1990. wpa_ie, &wpa_ie_len)) {
  1991. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  1992. "key management and encryption suites");
  1993. wpas_connect_work_done(wpa_s);
  1994. return;
  1995. }
  1996. } else if ((ssid->key_mgmt & WPA_KEY_MGMT_IEEE8021X_NO_WPA) && bss &&
  1997. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt)) {
  1998. /*
  1999. * Both WPA and non-WPA IEEE 802.1X enabled in configuration -
  2000. * use non-WPA since the scan results did not indicate that the
  2001. * AP is using WPA or WPA2.
  2002. */
  2003. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  2004. wpa_ie_len = 0;
  2005. wpa_s->wpa_proto = 0;
  2006. } else if (wpa_key_mgmt_wpa_any(ssid->key_mgmt)) {
  2007. wpa_ie_len = sizeof(wpa_ie);
  2008. if (wpa_supplicant_set_suites(wpa_s, NULL, ssid,
  2009. wpa_ie, &wpa_ie_len)) {
  2010. wpa_msg(wpa_s, MSG_WARNING, "WPA: Failed to set WPA "
  2011. "key management and encryption suites (no "
  2012. "scan results)");
  2013. wpas_connect_work_done(wpa_s);
  2014. return;
  2015. }
  2016. #ifdef CONFIG_WPS
  2017. } else if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
  2018. struct wpabuf *wps_ie;
  2019. wps_ie = wps_build_assoc_req_ie(wpas_wps_get_req_type(ssid));
  2020. if (wps_ie && wpabuf_len(wps_ie) <= sizeof(wpa_ie)) {
  2021. wpa_ie_len = wpabuf_len(wps_ie);
  2022. os_memcpy(wpa_ie, wpabuf_head(wps_ie), wpa_ie_len);
  2023. } else
  2024. wpa_ie_len = 0;
  2025. wpabuf_free(wps_ie);
  2026. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  2027. if (!bss || (bss->caps & IEEE80211_CAP_PRIVACY))
  2028. params.wps = WPS_MODE_PRIVACY;
  2029. else
  2030. params.wps = WPS_MODE_OPEN;
  2031. wpa_s->wpa_proto = 0;
  2032. #endif /* CONFIG_WPS */
  2033. } else {
  2034. wpa_supplicant_set_non_wpa_policy(wpa_s, ssid);
  2035. wpa_ie_len = 0;
  2036. wpa_s->wpa_proto = 0;
  2037. }
  2038. #ifdef CONFIG_P2P
  2039. if (wpa_s->global->p2p) {
  2040. u8 *pos;
  2041. size_t len;
  2042. int res;
  2043. pos = wpa_ie + wpa_ie_len;
  2044. len = sizeof(wpa_ie) - wpa_ie_len;
  2045. res = wpas_p2p_assoc_req_ie(wpa_s, bss, pos, len,
  2046. ssid->p2p_group);
  2047. if (res >= 0)
  2048. wpa_ie_len += res;
  2049. }
  2050. wpa_s->cross_connect_disallowed = 0;
  2051. if (bss) {
  2052. struct wpabuf *p2p;
  2053. p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
  2054. if (p2p) {
  2055. wpa_s->cross_connect_disallowed =
  2056. p2p_get_cross_connect_disallowed(p2p);
  2057. wpabuf_free(p2p);
  2058. wpa_dbg(wpa_s, MSG_DEBUG, "P2P: WLAN AP %s cross "
  2059. "connection",
  2060. wpa_s->cross_connect_disallowed ?
  2061. "disallows" : "allows");
  2062. }
  2063. }
  2064. os_memset(wpa_s->p2p_ip_addr_info, 0, sizeof(wpa_s->p2p_ip_addr_info));
  2065. #endif /* CONFIG_P2P */
  2066. if (bss) {
  2067. wpa_ie_len += wpas_supp_op_class_ie(wpa_s, bss->freq,
  2068. wpa_ie + wpa_ie_len,
  2069. sizeof(wpa_ie) -
  2070. wpa_ie_len);
  2071. }
  2072. /*
  2073. * Workaround: Add Extended Capabilities element only if the AP
  2074. * included this element in Beacon/Probe Response frames. Some older
  2075. * APs seem to have interoperability issues if this element is
  2076. * included, so while the standard may require us to include the
  2077. * element in all cases, it is justifiable to skip it to avoid
  2078. * interoperability issues.
  2079. */
  2080. if (ssid->p2p_group)
  2081. wpa_drv_get_ext_capa(wpa_s, WPA_IF_P2P_CLIENT);
  2082. else
  2083. wpa_drv_get_ext_capa(wpa_s, WPA_IF_STATION);
  2084. if (!bss || wpa_bss_get_ie(bss, WLAN_EID_EXT_CAPAB)) {
  2085. u8 ext_capab[18];
  2086. int ext_capab_len;
  2087. ext_capab_len = wpas_build_ext_capab(wpa_s, ext_capab,
  2088. sizeof(ext_capab));
  2089. if (ext_capab_len > 0) {
  2090. u8 *pos = wpa_ie;
  2091. if (wpa_ie_len > 0 && pos[0] == WLAN_EID_RSN)
  2092. pos += 2 + pos[1];
  2093. os_memmove(pos + ext_capab_len, pos,
  2094. wpa_ie_len - (pos - wpa_ie));
  2095. wpa_ie_len += ext_capab_len;
  2096. os_memcpy(pos, ext_capab, ext_capab_len);
  2097. }
  2098. }
  2099. #ifdef CONFIG_HS20
  2100. if (is_hs20_network(wpa_s, ssid, bss)) {
  2101. struct wpabuf *hs20;
  2102. hs20 = wpabuf_alloc(20);
  2103. if (hs20) {
  2104. int pps_mo_id = hs20_get_pps_mo_id(wpa_s, ssid);
  2105. size_t len;
  2106. wpas_hs20_add_indication(hs20, pps_mo_id);
  2107. len = sizeof(wpa_ie) - wpa_ie_len;
  2108. if (wpabuf_len(hs20) <= len) {
  2109. os_memcpy(wpa_ie + wpa_ie_len,
  2110. wpabuf_head(hs20), wpabuf_len(hs20));
  2111. wpa_ie_len += wpabuf_len(hs20);
  2112. }
  2113. wpabuf_free(hs20);
  2114. hs20_configure_frame_filters(wpa_s);
  2115. }
  2116. }
  2117. #endif /* CONFIG_HS20 */
  2118. if (wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ]) {
  2119. struct wpabuf *buf = wpa_s->vendor_elem[VENDOR_ELEM_ASSOC_REQ];
  2120. size_t len;
  2121. len = sizeof(wpa_ie) - wpa_ie_len;
  2122. if (wpabuf_len(buf) <= len) {
  2123. os_memcpy(wpa_ie + wpa_ie_len,
  2124. wpabuf_head(buf), wpabuf_len(buf));
  2125. wpa_ie_len += wpabuf_len(buf);
  2126. }
  2127. }
  2128. #ifdef CONFIG_FST
  2129. if (wpa_s->fst_ies) {
  2130. int fst_ies_len = wpabuf_len(wpa_s->fst_ies);
  2131. if (wpa_ie_len + fst_ies_len <= sizeof(wpa_ie)) {
  2132. os_memcpy(wpa_ie + wpa_ie_len,
  2133. wpabuf_head(wpa_s->fst_ies), fst_ies_len);
  2134. wpa_ie_len += fst_ies_len;
  2135. }
  2136. }
  2137. #endif /* CONFIG_FST */
  2138. #ifdef CONFIG_MBO
  2139. if (bss && wpa_bss_get_vendor_ie(bss, MBO_IE_VENDOR_TYPE)) {
  2140. int len;
  2141. len = wpas_mbo_ie(wpa_s, wpa_ie + wpa_ie_len,
  2142. sizeof(wpa_ie) - wpa_ie_len);
  2143. if (len >= 0)
  2144. wpa_ie_len += len;
  2145. }
  2146. #endif /* CONFIG_MBO */
  2147. wpa_clear_keys(wpa_s, bss ? bss->bssid : NULL);
  2148. use_crypt = 1;
  2149. cipher_pairwise = wpa_s->pairwise_cipher;
  2150. cipher_group = wpa_s->group_cipher;
  2151. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE ||
  2152. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2153. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE)
  2154. use_crypt = 0;
  2155. if (wpa_set_wep_keys(wpa_s, ssid)) {
  2156. use_crypt = 1;
  2157. wep_keys_set = 1;
  2158. }
  2159. }
  2160. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS)
  2161. use_crypt = 0;
  2162. #ifdef IEEE8021X_EAPOL
  2163. if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  2164. if ((ssid->eapol_flags &
  2165. (EAPOL_FLAG_REQUIRE_KEY_UNICAST |
  2166. EAPOL_FLAG_REQUIRE_KEY_BROADCAST)) == 0 &&
  2167. !wep_keys_set) {
  2168. use_crypt = 0;
  2169. } else {
  2170. /* Assume that dynamic WEP-104 keys will be used and
  2171. * set cipher suites in order for drivers to expect
  2172. * encryption. */
  2173. cipher_pairwise = cipher_group = WPA_CIPHER_WEP104;
  2174. }
  2175. }
  2176. #endif /* IEEE8021X_EAPOL */
  2177. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  2178. /* Set the key before (and later after) association */
  2179. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  2180. }
  2181. wpa_supplicant_set_state(wpa_s, WPA_ASSOCIATING);
  2182. if (bss) {
  2183. params.ssid = bss->ssid;
  2184. params.ssid_len = bss->ssid_len;
  2185. if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set ||
  2186. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  2187. wpa_printf(MSG_DEBUG, "Limit connection to BSSID "
  2188. MACSTR " freq=%u MHz based on scan results "
  2189. "(bssid_set=%d wps=%d)",
  2190. MAC2STR(bss->bssid), bss->freq,
  2191. ssid->bssid_set,
  2192. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS);
  2193. params.bssid = bss->bssid;
  2194. params.freq.freq = bss->freq;
  2195. }
  2196. params.bssid_hint = bss->bssid;
  2197. params.freq_hint = bss->freq;
  2198. params.pbss = bss_is_pbss(bss);
  2199. } else {
  2200. params.ssid = ssid->ssid;
  2201. params.ssid_len = ssid->ssid_len;
  2202. params.pbss = (ssid->pbss != 2) ? ssid->pbss : 0;
  2203. }
  2204. if (ssid->mode == WPAS_MODE_IBSS && ssid->bssid_set &&
  2205. wpa_s->conf->ap_scan == 2) {
  2206. params.bssid = ssid->bssid;
  2207. params.fixed_bssid = 1;
  2208. }
  2209. /* Initial frequency for IBSS/mesh */
  2210. if ((ssid->mode == WPAS_MODE_IBSS || ssid->mode == WPAS_MODE_MESH) &&
  2211. ssid->frequency > 0 && params.freq.freq == 0)
  2212. ibss_mesh_setup_freq(wpa_s, ssid, &params.freq);
  2213. if (ssid->mode == WPAS_MODE_IBSS) {
  2214. params.fixed_freq = ssid->fixed_freq;
  2215. if (ssid->beacon_int)
  2216. params.beacon_int = ssid->beacon_int;
  2217. else
  2218. params.beacon_int = wpa_s->conf->beacon_int;
  2219. }
  2220. params.wpa_ie = wpa_ie;
  2221. params.wpa_ie_len = wpa_ie_len;
  2222. params.pairwise_suite = cipher_pairwise;
  2223. params.group_suite = cipher_group;
  2224. params.key_mgmt_suite = wpa_s->key_mgmt;
  2225. params.wpa_proto = wpa_s->wpa_proto;
  2226. params.auth_alg = algs;
  2227. params.mode = ssid->mode;
  2228. params.bg_scan_period = ssid->bg_scan_period;
  2229. for (i = 0; i < NUM_WEP_KEYS; i++) {
  2230. if (ssid->wep_key_len[i])
  2231. params.wep_key[i] = ssid->wep_key[i];
  2232. params.wep_key_len[i] = ssid->wep_key_len[i];
  2233. }
  2234. params.wep_tx_keyidx = ssid->wep_tx_keyidx;
  2235. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  2236. (params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  2237. params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK)) {
  2238. params.passphrase = ssid->passphrase;
  2239. if (ssid->psk_set)
  2240. params.psk = ssid->psk;
  2241. }
  2242. if (wpa_s->conf->key_mgmt_offload) {
  2243. if (params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X ||
  2244. params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SHA256 ||
  2245. params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B ||
  2246. params.key_mgmt_suite == WPA_KEY_MGMT_IEEE8021X_SUITE_B_192)
  2247. params.req_key_mgmt_offload =
  2248. ssid->proactive_key_caching < 0 ?
  2249. wpa_s->conf->okc : ssid->proactive_key_caching;
  2250. else
  2251. params.req_key_mgmt_offload = 1;
  2252. if ((params.key_mgmt_suite == WPA_KEY_MGMT_PSK ||
  2253. params.key_mgmt_suite == WPA_KEY_MGMT_PSK_SHA256 ||
  2254. params.key_mgmt_suite == WPA_KEY_MGMT_FT_PSK) &&
  2255. ssid->psk_set)
  2256. params.psk = ssid->psk;
  2257. }
  2258. params.drop_unencrypted = use_crypt;
  2259. #ifdef CONFIG_IEEE80211W
  2260. params.mgmt_frame_protection = wpas_get_ssid_pmf(wpa_s, ssid);
  2261. if (params.mgmt_frame_protection != NO_MGMT_FRAME_PROTECTION && bss) {
  2262. const u8 *rsn = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2263. struct wpa_ie_data ie;
  2264. if (rsn && wpa_parse_wpa_ie(rsn, 2 + rsn[1], &ie) == 0 &&
  2265. ie.capabilities &
  2266. (WPA_CAPABILITY_MFPC | WPA_CAPABILITY_MFPR)) {
  2267. wpa_dbg(wpa_s, MSG_DEBUG, "WPA: Selected AP supports "
  2268. "MFP: require MFP");
  2269. params.mgmt_frame_protection =
  2270. MGMT_FRAME_PROTECTION_REQUIRED;
  2271. }
  2272. }
  2273. #endif /* CONFIG_IEEE80211W */
  2274. params.p2p = ssid->p2p_group;
  2275. if (wpa_s->p2pdev->set_sta_uapsd)
  2276. params.uapsd = wpa_s->p2pdev->sta_uapsd;
  2277. else
  2278. params.uapsd = -1;
  2279. #ifdef CONFIG_HT_OVERRIDES
  2280. os_memset(&htcaps, 0, sizeof(htcaps));
  2281. os_memset(&htcaps_mask, 0, sizeof(htcaps_mask));
  2282. params.htcaps = (u8 *) &htcaps;
  2283. params.htcaps_mask = (u8 *) &htcaps_mask;
  2284. wpa_supplicant_apply_ht_overrides(wpa_s, ssid, &params);
  2285. #endif /* CONFIG_HT_OVERRIDES */
  2286. #ifdef CONFIG_VHT_OVERRIDES
  2287. os_memset(&vhtcaps, 0, sizeof(vhtcaps));
  2288. os_memset(&vhtcaps_mask, 0, sizeof(vhtcaps_mask));
  2289. params.vhtcaps = &vhtcaps;
  2290. params.vhtcaps_mask = &vhtcaps_mask;
  2291. wpa_supplicant_apply_vht_overrides(wpa_s, ssid, &params);
  2292. #endif /* CONFIG_VHT_OVERRIDES */
  2293. #ifdef CONFIG_P2P
  2294. /*
  2295. * If multi-channel concurrency is not supported, check for any
  2296. * frequency conflict. In case of any frequency conflict, remove the
  2297. * least prioritized connection.
  2298. */
  2299. if (wpa_s->num_multichan_concurrent < 2) {
  2300. int freq, num;
  2301. num = get_shared_radio_freqs(wpa_s, &freq, 1);
  2302. if (num > 0 && freq > 0 && freq != params.freq.freq) {
  2303. wpa_printf(MSG_DEBUG,
  2304. "Assoc conflicting freq found (%d != %d)",
  2305. freq, params.freq.freq);
  2306. if (wpas_p2p_handle_frequency_conflicts(
  2307. wpa_s, params.freq.freq, ssid) < 0) {
  2308. wpas_connect_work_done(wpa_s);
  2309. return;
  2310. }
  2311. }
  2312. }
  2313. #endif /* CONFIG_P2P */
  2314. if (wpa_s->reassoc_same_ess && !is_zero_ether_addr(prev_bssid) &&
  2315. wpa_s->current_ssid)
  2316. params.prev_bssid = prev_bssid;
  2317. ret = wpa_drv_associate(wpa_s, &params);
  2318. if (ret < 0) {
  2319. wpa_msg(wpa_s, MSG_INFO, "Association request to the driver "
  2320. "failed");
  2321. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SANE_ERROR_CODES) {
  2322. /*
  2323. * The driver is known to mean what is saying, so we
  2324. * can stop right here; the association will not
  2325. * succeed.
  2326. */
  2327. wpas_connection_failed(wpa_s, wpa_s->pending_bssid);
  2328. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  2329. os_memset(wpa_s->pending_bssid, 0, ETH_ALEN);
  2330. return;
  2331. }
  2332. /* try to continue anyway; new association will be tried again
  2333. * after timeout */
  2334. assoc_failed = 1;
  2335. }
  2336. if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPA_NONE) {
  2337. /* Set the key after the association just in case association
  2338. * cleared the previously configured key. */
  2339. wpa_supplicant_set_wpa_none_key(wpa_s, ssid);
  2340. /* No need to timeout authentication since there is no key
  2341. * management. */
  2342. wpa_supplicant_cancel_auth_timeout(wpa_s);
  2343. wpa_supplicant_set_state(wpa_s, WPA_COMPLETED);
  2344. #ifdef CONFIG_IBSS_RSN
  2345. } else if (ssid->mode == WPAS_MODE_IBSS &&
  2346. wpa_s->key_mgmt != WPA_KEY_MGMT_NONE &&
  2347. wpa_s->key_mgmt != WPA_KEY_MGMT_WPA_NONE) {
  2348. /*
  2349. * RSN IBSS authentication is per-STA and we can disable the
  2350. * per-BSSID authentication.
  2351. */
  2352. wpa_supplicant_cancel_auth_timeout(wpa_s);
  2353. #endif /* CONFIG_IBSS_RSN */
  2354. } else {
  2355. /* Timeout for IEEE 802.11 authentication and association */
  2356. int timeout = 60;
  2357. if (assoc_failed) {
  2358. /* give IBSS a bit more time */
  2359. timeout = ssid->mode == WPAS_MODE_IBSS ? 10 : 5;
  2360. } else if (wpa_s->conf->ap_scan == 1) {
  2361. /* give IBSS a bit more time */
  2362. timeout = ssid->mode == WPAS_MODE_IBSS ? 20 : 10;
  2363. }
  2364. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  2365. }
  2366. if (wep_keys_set &&
  2367. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SET_KEYS_AFTER_ASSOC)) {
  2368. /* Set static WEP keys again */
  2369. wpa_set_wep_keys(wpa_s, ssid);
  2370. }
  2371. if (wpa_s->current_ssid && wpa_s->current_ssid != ssid) {
  2372. /*
  2373. * Do not allow EAP session resumption between different
  2374. * network configurations.
  2375. */
  2376. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2377. }
  2378. old_ssid = wpa_s->current_ssid;
  2379. wpa_s->current_ssid = ssid;
  2380. if (!wpas_driver_bss_selection(wpa_s) || ssid->bssid_set) {
  2381. wpa_s->current_bss = bss;
  2382. #ifdef CONFIG_HS20
  2383. hs20_configure_frame_filters(wpa_s);
  2384. #endif /* CONFIG_HS20 */
  2385. }
  2386. wpa_supplicant_rsn_supp_set_config(wpa_s, wpa_s->current_ssid);
  2387. wpa_supplicant_initiate_eapol(wpa_s);
  2388. if (old_ssid != wpa_s->current_ssid)
  2389. wpas_notify_network_changed(wpa_s);
  2390. }
  2391. static void wpa_supplicant_clear_connection(struct wpa_supplicant *wpa_s,
  2392. const u8 *addr)
  2393. {
  2394. struct wpa_ssid *old_ssid;
  2395. wpas_connect_work_done(wpa_s);
  2396. wpa_clear_keys(wpa_s, addr);
  2397. old_ssid = wpa_s->current_ssid;
  2398. wpa_supplicant_mark_disassoc(wpa_s);
  2399. wpa_sm_set_config(wpa_s->wpa, NULL);
  2400. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2401. if (old_ssid != wpa_s->current_ssid)
  2402. wpas_notify_network_changed(wpa_s);
  2403. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  2404. }
  2405. /**
  2406. * wpa_supplicant_deauthenticate - Deauthenticate the current connection
  2407. * @wpa_s: Pointer to wpa_supplicant data
  2408. * @reason_code: IEEE 802.11 reason code for the deauthenticate frame
  2409. *
  2410. * This function is used to request %wpa_supplicant to deauthenticate from the
  2411. * current AP.
  2412. */
  2413. void wpa_supplicant_deauthenticate(struct wpa_supplicant *wpa_s,
  2414. int reason_code)
  2415. {
  2416. u8 *addr = NULL;
  2417. union wpa_event_data event;
  2418. int zero_addr = 0;
  2419. wpa_dbg(wpa_s, MSG_DEBUG, "Request to deauthenticate - bssid=" MACSTR
  2420. " pending_bssid=" MACSTR " reason=%d state=%s",
  2421. MAC2STR(wpa_s->bssid), MAC2STR(wpa_s->pending_bssid),
  2422. reason_code, wpa_supplicant_state_txt(wpa_s->wpa_state));
  2423. if (!is_zero_ether_addr(wpa_s->pending_bssid) &&
  2424. (wpa_s->wpa_state == WPA_AUTHENTICATING ||
  2425. wpa_s->wpa_state == WPA_ASSOCIATING))
  2426. addr = wpa_s->pending_bssid;
  2427. else if (!is_zero_ether_addr(wpa_s->bssid))
  2428. addr = wpa_s->bssid;
  2429. else if (wpa_s->wpa_state == WPA_ASSOCIATING) {
  2430. /*
  2431. * When using driver-based BSS selection, we may not know the
  2432. * BSSID with which we are currently trying to associate. We
  2433. * need to notify the driver of this disconnection even in such
  2434. * a case, so use the all zeros address here.
  2435. */
  2436. addr = wpa_s->bssid;
  2437. zero_addr = 1;
  2438. }
  2439. #ifdef CONFIG_TDLS
  2440. wpa_tdls_teardown_peers(wpa_s->wpa);
  2441. #endif /* CONFIG_TDLS */
  2442. #ifdef CONFIG_MESH
  2443. if (wpa_s->ifmsh) {
  2444. wpa_msg(wpa_s, MSG_INFO, MESH_GROUP_REMOVED "%s",
  2445. wpa_s->ifname);
  2446. wpa_supplicant_leave_mesh(wpa_s);
  2447. }
  2448. #endif /* CONFIG_MESH */
  2449. if (addr) {
  2450. wpa_drv_deauthenticate(wpa_s, addr, reason_code);
  2451. os_memset(&event, 0, sizeof(event));
  2452. event.deauth_info.reason_code = (u16) reason_code;
  2453. event.deauth_info.locally_generated = 1;
  2454. wpa_supplicant_event(wpa_s, EVENT_DEAUTH, &event);
  2455. if (zero_addr)
  2456. addr = NULL;
  2457. }
  2458. wpa_supplicant_clear_connection(wpa_s, addr);
  2459. }
  2460. static void wpa_supplicant_enable_one_network(struct wpa_supplicant *wpa_s,
  2461. struct wpa_ssid *ssid)
  2462. {
  2463. if (!ssid || !ssid->disabled || ssid->disabled == 2)
  2464. return;
  2465. ssid->disabled = 0;
  2466. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  2467. wpas_notify_network_enabled_changed(wpa_s, ssid);
  2468. /*
  2469. * Try to reassociate since there is no current configuration and a new
  2470. * network was made available.
  2471. */
  2472. if (!wpa_s->current_ssid && !wpa_s->disconnected)
  2473. wpa_s->reassociate = 1;
  2474. }
  2475. /**
  2476. * wpa_supplicant_add_network - Add a new network
  2477. * @wpa_s: wpa_supplicant structure for a network interface
  2478. * Returns: The new network configuration or %NULL if operation failed
  2479. *
  2480. * This function performs the following operations:
  2481. * 1. Adds a new network.
  2482. * 2. Send network addition notification.
  2483. * 3. Marks the network disabled.
  2484. * 4. Set network default parameters.
  2485. */
  2486. struct wpa_ssid * wpa_supplicant_add_network(struct wpa_supplicant *wpa_s)
  2487. {
  2488. struct wpa_ssid *ssid;
  2489. ssid = wpa_config_add_network(wpa_s->conf);
  2490. if (!ssid)
  2491. return NULL;
  2492. wpas_notify_network_added(wpa_s, ssid);
  2493. ssid->disabled = 1;
  2494. wpa_config_set_network_defaults(ssid);
  2495. return ssid;
  2496. }
  2497. /**
  2498. * wpa_supplicant_remove_network - Remove a configured network based on id
  2499. * @wpa_s: wpa_supplicant structure for a network interface
  2500. * @id: Unique network id to search for
  2501. * Returns: 0 on success, or -1 if the network was not found, -2 if the network
  2502. * could not be removed
  2503. *
  2504. * This function performs the following operations:
  2505. * 1. Removes the network.
  2506. * 2. Send network removal notification.
  2507. * 3. Update internal state machines.
  2508. * 4. Stop any running sched scans.
  2509. */
  2510. int wpa_supplicant_remove_network(struct wpa_supplicant *wpa_s, int id)
  2511. {
  2512. struct wpa_ssid *ssid;
  2513. int was_disabled;
  2514. ssid = wpa_config_get_network(wpa_s->conf, id);
  2515. if (!ssid)
  2516. return -1;
  2517. wpas_notify_network_removed(wpa_s, ssid);
  2518. if (wpa_s->last_ssid == ssid)
  2519. wpa_s->last_ssid = NULL;
  2520. if (ssid == wpa_s->current_ssid || !wpa_s->current_ssid) {
  2521. #ifdef CONFIG_SME
  2522. wpa_s->sme.prev_bssid_set = 0;
  2523. #endif /* CONFIG_SME */
  2524. /*
  2525. * Invalidate the EAP session cache if the current or
  2526. * previously used network is removed.
  2527. */
  2528. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2529. }
  2530. if (ssid == wpa_s->current_ssid) {
  2531. wpa_sm_set_config(wpa_s->wpa, NULL);
  2532. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2533. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2534. wpa_s->own_disconnect_req = 1;
  2535. wpa_supplicant_deauthenticate(wpa_s,
  2536. WLAN_REASON_DEAUTH_LEAVING);
  2537. }
  2538. was_disabled = ssid->disabled;
  2539. if (wpa_config_remove_network(wpa_s->conf, id) < 0)
  2540. return -2;
  2541. if (!was_disabled && wpa_s->sched_scanning) {
  2542. wpa_printf(MSG_DEBUG,
  2543. "Stop ongoing sched_scan to remove network from filters");
  2544. wpa_supplicant_cancel_sched_scan(wpa_s);
  2545. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2546. }
  2547. return 0;
  2548. }
  2549. /**
  2550. * wpa_supplicant_enable_network - Mark a configured network as enabled
  2551. * @wpa_s: wpa_supplicant structure for a network interface
  2552. * @ssid: wpa_ssid structure for a configured network or %NULL
  2553. *
  2554. * Enables the specified network or all networks if no network specified.
  2555. */
  2556. void wpa_supplicant_enable_network(struct wpa_supplicant *wpa_s,
  2557. struct wpa_ssid *ssid)
  2558. {
  2559. if (ssid == NULL) {
  2560. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  2561. wpa_supplicant_enable_one_network(wpa_s, ssid);
  2562. } else
  2563. wpa_supplicant_enable_one_network(wpa_s, ssid);
  2564. if (wpa_s->reassociate && !wpa_s->disconnected &&
  2565. (!wpa_s->current_ssid ||
  2566. wpa_s->wpa_state == WPA_DISCONNECTED ||
  2567. wpa_s->wpa_state == WPA_SCANNING)) {
  2568. if (wpa_s->sched_scanning) {
  2569. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to add "
  2570. "new network to scan filters");
  2571. wpa_supplicant_cancel_sched_scan(wpa_s);
  2572. }
  2573. if (wpa_supplicant_fast_associate(wpa_s) != 1) {
  2574. wpa_s->scan_req = NORMAL_SCAN_REQ;
  2575. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2576. }
  2577. }
  2578. }
  2579. /**
  2580. * wpa_supplicant_disable_network - Mark a configured network as disabled
  2581. * @wpa_s: wpa_supplicant structure for a network interface
  2582. * @ssid: wpa_ssid structure for a configured network or %NULL
  2583. *
  2584. * Disables the specified network or all networks if no network specified.
  2585. */
  2586. void wpa_supplicant_disable_network(struct wpa_supplicant *wpa_s,
  2587. struct wpa_ssid *ssid)
  2588. {
  2589. struct wpa_ssid *other_ssid;
  2590. int was_disabled;
  2591. if (ssid == NULL) {
  2592. if (wpa_s->sched_scanning)
  2593. wpa_supplicant_cancel_sched_scan(wpa_s);
  2594. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  2595. other_ssid = other_ssid->next) {
  2596. was_disabled = other_ssid->disabled;
  2597. if (was_disabled == 2)
  2598. continue; /* do not change persistent P2P group
  2599. * data */
  2600. other_ssid->disabled = 1;
  2601. if (was_disabled != other_ssid->disabled)
  2602. wpas_notify_network_enabled_changed(
  2603. wpa_s, other_ssid);
  2604. }
  2605. if (wpa_s->current_ssid)
  2606. wpa_supplicant_deauthenticate(
  2607. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2608. } else if (ssid->disabled != 2) {
  2609. if (ssid == wpa_s->current_ssid)
  2610. wpa_supplicant_deauthenticate(
  2611. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2612. was_disabled = ssid->disabled;
  2613. ssid->disabled = 1;
  2614. if (was_disabled != ssid->disabled) {
  2615. wpas_notify_network_enabled_changed(wpa_s, ssid);
  2616. if (wpa_s->sched_scanning) {
  2617. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan "
  2618. "to remove network from filters");
  2619. wpa_supplicant_cancel_sched_scan(wpa_s);
  2620. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2621. }
  2622. }
  2623. }
  2624. }
  2625. /**
  2626. * wpa_supplicant_select_network - Attempt association with a network
  2627. * @wpa_s: wpa_supplicant structure for a network interface
  2628. * @ssid: wpa_ssid structure for a configured network or %NULL for any network
  2629. */
  2630. void wpa_supplicant_select_network(struct wpa_supplicant *wpa_s,
  2631. struct wpa_ssid *ssid)
  2632. {
  2633. struct wpa_ssid *other_ssid;
  2634. int disconnected = 0;
  2635. if (ssid && ssid != wpa_s->current_ssid && wpa_s->current_ssid) {
  2636. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2637. wpa_s->own_disconnect_req = 1;
  2638. wpa_supplicant_deauthenticate(
  2639. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2640. disconnected = 1;
  2641. }
  2642. if (ssid)
  2643. wpas_clear_temp_disabled(wpa_s, ssid, 1);
  2644. /*
  2645. * Mark all other networks disabled or mark all networks enabled if no
  2646. * network specified.
  2647. */
  2648. for (other_ssid = wpa_s->conf->ssid; other_ssid;
  2649. other_ssid = other_ssid->next) {
  2650. int was_disabled = other_ssid->disabled;
  2651. if (was_disabled == 2)
  2652. continue; /* do not change persistent P2P group data */
  2653. other_ssid->disabled = ssid ? (ssid->id != other_ssid->id) : 0;
  2654. if (was_disabled && !other_ssid->disabled)
  2655. wpas_clear_temp_disabled(wpa_s, other_ssid, 0);
  2656. if (was_disabled != other_ssid->disabled)
  2657. wpas_notify_network_enabled_changed(wpa_s, other_ssid);
  2658. }
  2659. if (ssid && ssid == wpa_s->current_ssid && wpa_s->current_ssid &&
  2660. wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  2661. /* We are already associated with the selected network */
  2662. wpa_printf(MSG_DEBUG, "Already associated with the "
  2663. "selected network - do nothing");
  2664. return;
  2665. }
  2666. if (ssid) {
  2667. wpa_s->current_ssid = ssid;
  2668. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2669. wpa_s->connect_without_scan =
  2670. (ssid->mode == WPAS_MODE_MESH) ? ssid : NULL;
  2671. /*
  2672. * Don't optimize next scan freqs since a new ESS has been
  2673. * selected.
  2674. */
  2675. os_free(wpa_s->next_scan_freqs);
  2676. wpa_s->next_scan_freqs = NULL;
  2677. } else {
  2678. wpa_s->connect_without_scan = NULL;
  2679. }
  2680. wpa_s->disconnected = 0;
  2681. wpa_s->reassociate = 1;
  2682. if (wpa_s->connect_without_scan ||
  2683. wpa_supplicant_fast_associate(wpa_s) != 1) {
  2684. wpa_s->scan_req = NORMAL_SCAN_REQ;
  2685. wpas_scan_reset_sched_scan(wpa_s);
  2686. wpa_supplicant_req_scan(wpa_s, 0, disconnected ? 100000 : 0);
  2687. }
  2688. if (ssid)
  2689. wpas_notify_network_selected(wpa_s, ssid);
  2690. }
  2691. /**
  2692. * wpas_set_pkcs11_engine_and_module_path - Set PKCS #11 engine and module path
  2693. * @wpa_s: wpa_supplicant structure for a network interface
  2694. * @pkcs11_engine_path: PKCS #11 engine path or NULL
  2695. * @pkcs11_module_path: PKCS #11 module path or NULL
  2696. * Returns: 0 on success; -1 on failure
  2697. *
  2698. * Sets the PKCS #11 engine and module path. Both have to be NULL or a valid
  2699. * path. If resetting the EAPOL state machine with the new PKCS #11 engine and
  2700. * module path fails the paths will be reset to the default value (NULL).
  2701. */
  2702. int wpas_set_pkcs11_engine_and_module_path(struct wpa_supplicant *wpa_s,
  2703. const char *pkcs11_engine_path,
  2704. const char *pkcs11_module_path)
  2705. {
  2706. char *pkcs11_engine_path_copy = NULL;
  2707. char *pkcs11_module_path_copy = NULL;
  2708. if (pkcs11_engine_path != NULL) {
  2709. pkcs11_engine_path_copy = os_strdup(pkcs11_engine_path);
  2710. if (pkcs11_engine_path_copy == NULL)
  2711. return -1;
  2712. }
  2713. if (pkcs11_module_path != NULL) {
  2714. pkcs11_module_path_copy = os_strdup(pkcs11_module_path);
  2715. if (pkcs11_module_path_copy == NULL) {
  2716. os_free(pkcs11_engine_path_copy);
  2717. return -1;
  2718. }
  2719. }
  2720. os_free(wpa_s->conf->pkcs11_engine_path);
  2721. os_free(wpa_s->conf->pkcs11_module_path);
  2722. wpa_s->conf->pkcs11_engine_path = pkcs11_engine_path_copy;
  2723. wpa_s->conf->pkcs11_module_path = pkcs11_module_path_copy;
  2724. wpa_sm_set_eapol(wpa_s->wpa, NULL);
  2725. eapol_sm_deinit(wpa_s->eapol);
  2726. wpa_s->eapol = NULL;
  2727. if (wpa_supplicant_init_eapol(wpa_s)) {
  2728. /* Error -> Reset paths to the default value (NULL) once. */
  2729. if (pkcs11_engine_path != NULL && pkcs11_module_path != NULL)
  2730. wpas_set_pkcs11_engine_and_module_path(wpa_s, NULL,
  2731. NULL);
  2732. return -1;
  2733. }
  2734. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  2735. return 0;
  2736. }
  2737. /**
  2738. * wpa_supplicant_set_ap_scan - Set AP scan mode for interface
  2739. * @wpa_s: wpa_supplicant structure for a network interface
  2740. * @ap_scan: AP scan mode
  2741. * Returns: 0 if succeed or -1 if ap_scan has an invalid value
  2742. *
  2743. */
  2744. int wpa_supplicant_set_ap_scan(struct wpa_supplicant *wpa_s, int ap_scan)
  2745. {
  2746. int old_ap_scan;
  2747. if (ap_scan < 0 || ap_scan > 2)
  2748. return -1;
  2749. if (ap_scan == 2 && os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
  2750. wpa_printf(MSG_INFO,
  2751. "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
  2752. }
  2753. #ifdef ANDROID
  2754. if (ap_scan == 2 && ap_scan != wpa_s->conf->ap_scan &&
  2755. wpa_s->wpa_state >= WPA_ASSOCIATING &&
  2756. wpa_s->wpa_state < WPA_COMPLETED) {
  2757. wpa_printf(MSG_ERROR, "ap_scan = %d (%d) rejected while "
  2758. "associating", wpa_s->conf->ap_scan, ap_scan);
  2759. return 0;
  2760. }
  2761. #endif /* ANDROID */
  2762. old_ap_scan = wpa_s->conf->ap_scan;
  2763. wpa_s->conf->ap_scan = ap_scan;
  2764. if (old_ap_scan != wpa_s->conf->ap_scan)
  2765. wpas_notify_ap_scan_changed(wpa_s);
  2766. return 0;
  2767. }
  2768. /**
  2769. * wpa_supplicant_set_bss_expiration_age - Set BSS entry expiration age
  2770. * @wpa_s: wpa_supplicant structure for a network interface
  2771. * @expire_age: Expiration age in seconds
  2772. * Returns: 0 if succeed or -1 if expire_age has an invalid value
  2773. *
  2774. */
  2775. int wpa_supplicant_set_bss_expiration_age(struct wpa_supplicant *wpa_s,
  2776. unsigned int bss_expire_age)
  2777. {
  2778. if (bss_expire_age < 10) {
  2779. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration age %u",
  2780. bss_expire_age);
  2781. return -1;
  2782. }
  2783. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration age: %d sec",
  2784. bss_expire_age);
  2785. wpa_s->conf->bss_expiration_age = bss_expire_age;
  2786. return 0;
  2787. }
  2788. /**
  2789. * wpa_supplicant_set_bss_expiration_count - Set BSS entry expiration scan count
  2790. * @wpa_s: wpa_supplicant structure for a network interface
  2791. * @expire_count: number of scans after which an unseen BSS is reclaimed
  2792. * Returns: 0 if succeed or -1 if expire_count has an invalid value
  2793. *
  2794. */
  2795. int wpa_supplicant_set_bss_expiration_count(struct wpa_supplicant *wpa_s,
  2796. unsigned int bss_expire_count)
  2797. {
  2798. if (bss_expire_count < 1) {
  2799. wpa_msg(wpa_s, MSG_ERROR, "Invalid bss expiration count %u",
  2800. bss_expire_count);
  2801. return -1;
  2802. }
  2803. wpa_msg(wpa_s, MSG_DEBUG, "Setting bss expiration scan count: %u",
  2804. bss_expire_count);
  2805. wpa_s->conf->bss_expiration_scan_count = bss_expire_count;
  2806. return 0;
  2807. }
  2808. /**
  2809. * wpa_supplicant_set_scan_interval - Set scan interval
  2810. * @wpa_s: wpa_supplicant structure for a network interface
  2811. * @scan_interval: scan interval in seconds
  2812. * Returns: 0 if succeed or -1 if scan_interval has an invalid value
  2813. *
  2814. */
  2815. int wpa_supplicant_set_scan_interval(struct wpa_supplicant *wpa_s,
  2816. int scan_interval)
  2817. {
  2818. if (scan_interval < 0) {
  2819. wpa_msg(wpa_s, MSG_ERROR, "Invalid scan interval %d",
  2820. scan_interval);
  2821. return -1;
  2822. }
  2823. wpa_msg(wpa_s, MSG_DEBUG, "Setting scan interval: %d sec",
  2824. scan_interval);
  2825. wpa_supplicant_update_scan_int(wpa_s, scan_interval);
  2826. return 0;
  2827. }
  2828. /**
  2829. * wpa_supplicant_set_debug_params - Set global debug params
  2830. * @global: wpa_global structure
  2831. * @debug_level: debug level
  2832. * @debug_timestamp: determines if show timestamp in debug data
  2833. * @debug_show_keys: determines if show keys in debug data
  2834. * Returns: 0 if succeed or -1 if debug_level has wrong value
  2835. */
  2836. int wpa_supplicant_set_debug_params(struct wpa_global *global, int debug_level,
  2837. int debug_timestamp, int debug_show_keys)
  2838. {
  2839. int old_level, old_timestamp, old_show_keys;
  2840. /* check for allowed debuglevels */
  2841. if (debug_level != MSG_EXCESSIVE &&
  2842. debug_level != MSG_MSGDUMP &&
  2843. debug_level != MSG_DEBUG &&
  2844. debug_level != MSG_INFO &&
  2845. debug_level != MSG_WARNING &&
  2846. debug_level != MSG_ERROR)
  2847. return -1;
  2848. old_level = wpa_debug_level;
  2849. old_timestamp = wpa_debug_timestamp;
  2850. old_show_keys = wpa_debug_show_keys;
  2851. wpa_debug_level = debug_level;
  2852. wpa_debug_timestamp = debug_timestamp ? 1 : 0;
  2853. wpa_debug_show_keys = debug_show_keys ? 1 : 0;
  2854. if (wpa_debug_level != old_level)
  2855. wpas_notify_debug_level_changed(global);
  2856. if (wpa_debug_timestamp != old_timestamp)
  2857. wpas_notify_debug_timestamp_changed(global);
  2858. if (wpa_debug_show_keys != old_show_keys)
  2859. wpas_notify_debug_show_keys_changed(global);
  2860. return 0;
  2861. }
  2862. /**
  2863. * wpa_supplicant_get_ssid - Get a pointer to the current network structure
  2864. * @wpa_s: Pointer to wpa_supplicant data
  2865. * Returns: A pointer to the current network structure or %NULL on failure
  2866. */
  2867. struct wpa_ssid * wpa_supplicant_get_ssid(struct wpa_supplicant *wpa_s)
  2868. {
  2869. struct wpa_ssid *entry;
  2870. u8 ssid[SSID_MAX_LEN];
  2871. int res;
  2872. size_t ssid_len;
  2873. u8 bssid[ETH_ALEN];
  2874. int wired;
  2875. res = wpa_drv_get_ssid(wpa_s, ssid);
  2876. if (res < 0) {
  2877. wpa_msg(wpa_s, MSG_WARNING, "Could not read SSID from "
  2878. "driver");
  2879. return NULL;
  2880. }
  2881. ssid_len = res;
  2882. if (wpa_drv_get_bssid(wpa_s, bssid) < 0) {
  2883. wpa_msg(wpa_s, MSG_WARNING, "Could not read BSSID from "
  2884. "driver");
  2885. return NULL;
  2886. }
  2887. wired = wpa_s->conf->ap_scan == 0 &&
  2888. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_WIRED);
  2889. entry = wpa_s->conf->ssid;
  2890. while (entry) {
  2891. if (!wpas_network_disabled(wpa_s, entry) &&
  2892. ((ssid_len == entry->ssid_len &&
  2893. os_memcmp(ssid, entry->ssid, ssid_len) == 0) || wired) &&
  2894. (!entry->bssid_set ||
  2895. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  2896. return entry;
  2897. #ifdef CONFIG_WPS
  2898. if (!wpas_network_disabled(wpa_s, entry) &&
  2899. (entry->key_mgmt & WPA_KEY_MGMT_WPS) &&
  2900. (entry->ssid == NULL || entry->ssid_len == 0) &&
  2901. (!entry->bssid_set ||
  2902. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0))
  2903. return entry;
  2904. #endif /* CONFIG_WPS */
  2905. if (!wpas_network_disabled(wpa_s, entry) && entry->bssid_set &&
  2906. entry->ssid_len == 0 &&
  2907. os_memcmp(bssid, entry->bssid, ETH_ALEN) == 0)
  2908. return entry;
  2909. entry = entry->next;
  2910. }
  2911. return NULL;
  2912. }
  2913. static int select_driver(struct wpa_supplicant *wpa_s, int i)
  2914. {
  2915. struct wpa_global *global = wpa_s->global;
  2916. if (wpa_drivers[i]->global_init && global->drv_priv[i] == NULL) {
  2917. global->drv_priv[i] = wpa_drivers[i]->global_init(global);
  2918. if (global->drv_priv[i] == NULL) {
  2919. wpa_printf(MSG_ERROR, "Failed to initialize driver "
  2920. "'%s'", wpa_drivers[i]->name);
  2921. return -1;
  2922. }
  2923. }
  2924. wpa_s->driver = wpa_drivers[i];
  2925. wpa_s->global_drv_priv = global->drv_priv[i];
  2926. return 0;
  2927. }
  2928. static int wpa_supplicant_set_driver(struct wpa_supplicant *wpa_s,
  2929. const char *name)
  2930. {
  2931. int i;
  2932. size_t len;
  2933. const char *pos, *driver = name;
  2934. if (wpa_s == NULL)
  2935. return -1;
  2936. if (wpa_drivers[0] == NULL) {
  2937. wpa_msg(wpa_s, MSG_ERROR, "No driver interfaces build into "
  2938. "wpa_supplicant");
  2939. return -1;
  2940. }
  2941. if (name == NULL) {
  2942. /* default to first driver in the list */
  2943. return select_driver(wpa_s, 0);
  2944. }
  2945. do {
  2946. pos = os_strchr(driver, ',');
  2947. if (pos)
  2948. len = pos - driver;
  2949. else
  2950. len = os_strlen(driver);
  2951. for (i = 0; wpa_drivers[i]; i++) {
  2952. if (os_strlen(wpa_drivers[i]->name) == len &&
  2953. os_strncmp(driver, wpa_drivers[i]->name, len) ==
  2954. 0) {
  2955. /* First driver that succeeds wins */
  2956. if (select_driver(wpa_s, i) == 0)
  2957. return 0;
  2958. }
  2959. }
  2960. driver = pos + 1;
  2961. } while (pos);
  2962. wpa_msg(wpa_s, MSG_ERROR, "Unsupported driver '%s'", name);
  2963. return -1;
  2964. }
  2965. /**
  2966. * wpa_supplicant_rx_eapol - Deliver a received EAPOL frame to wpa_supplicant
  2967. * @ctx: Context pointer (wpa_s); this is the ctx variable registered
  2968. * with struct wpa_driver_ops::init()
  2969. * @src_addr: Source address of the EAPOL frame
  2970. * @buf: EAPOL data starting from the EAPOL header (i.e., no Ethernet header)
  2971. * @len: Length of the EAPOL data
  2972. *
  2973. * This function is called for each received EAPOL frame. Most driver
  2974. * interfaces rely on more generic OS mechanism for receiving frames through
  2975. * l2_packet, but if such a mechanism is not available, the driver wrapper may
  2976. * take care of received EAPOL frames and deliver them to the core supplicant
  2977. * code by calling this function.
  2978. */
  2979. void wpa_supplicant_rx_eapol(void *ctx, const u8 *src_addr,
  2980. const u8 *buf, size_t len)
  2981. {
  2982. struct wpa_supplicant *wpa_s = ctx;
  2983. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR, MAC2STR(src_addr));
  2984. wpa_hexdump(MSG_MSGDUMP, "RX EAPOL", buf, len);
  2985. #ifdef CONFIG_TESTING_OPTIONS
  2986. if (wpa_s->ignore_auth_resp) {
  2987. wpa_printf(MSG_INFO, "RX EAPOL - ignore_auth_resp active!");
  2988. return;
  2989. }
  2990. #endif /* CONFIG_TESTING_OPTIONS */
  2991. #ifdef CONFIG_PEERKEY
  2992. if (wpa_s->wpa_state > WPA_ASSOCIATED && wpa_s->current_ssid &&
  2993. wpa_s->current_ssid->peerkey &&
  2994. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) &&
  2995. wpa_sm_rx_eapol_peerkey(wpa_s->wpa, src_addr, buf, len) == 1) {
  2996. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: Processed PeerKey EAPOL-Key");
  2997. return;
  2998. }
  2999. #endif /* CONFIG_PEERKEY */
  3000. if (wpa_s->wpa_state < WPA_ASSOCIATED ||
  3001. (wpa_s->last_eapol_matches_bssid &&
  3002. #ifdef CONFIG_AP
  3003. !wpa_s->ap_iface &&
  3004. #endif /* CONFIG_AP */
  3005. os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) != 0)) {
  3006. /*
  3007. * There is possible race condition between receiving the
  3008. * association event and the EAPOL frame since they are coming
  3009. * through different paths from the driver. In order to avoid
  3010. * issues in trying to process the EAPOL frame before receiving
  3011. * association information, lets queue it for processing until
  3012. * the association event is received. This may also be needed in
  3013. * driver-based roaming case, so also use src_addr != BSSID as a
  3014. * trigger if we have previously confirmed that the
  3015. * Authenticator uses BSSID as the src_addr (which is not the
  3016. * case with wired IEEE 802.1X).
  3017. */
  3018. wpa_dbg(wpa_s, MSG_DEBUG, "Not associated - Delay processing "
  3019. "of received EAPOL frame (state=%s bssid=" MACSTR ")",
  3020. wpa_supplicant_state_txt(wpa_s->wpa_state),
  3021. MAC2STR(wpa_s->bssid));
  3022. wpabuf_free(wpa_s->pending_eapol_rx);
  3023. wpa_s->pending_eapol_rx = wpabuf_alloc_copy(buf, len);
  3024. if (wpa_s->pending_eapol_rx) {
  3025. os_get_reltime(&wpa_s->pending_eapol_rx_time);
  3026. os_memcpy(wpa_s->pending_eapol_rx_src, src_addr,
  3027. ETH_ALEN);
  3028. }
  3029. return;
  3030. }
  3031. wpa_s->last_eapol_matches_bssid =
  3032. os_memcmp(src_addr, wpa_s->bssid, ETH_ALEN) == 0;
  3033. #ifdef CONFIG_AP
  3034. if (wpa_s->ap_iface) {
  3035. wpa_supplicant_ap_rx_eapol(wpa_s, src_addr, buf, len);
  3036. return;
  3037. }
  3038. #endif /* CONFIG_AP */
  3039. if (wpa_s->key_mgmt == WPA_KEY_MGMT_NONE) {
  3040. wpa_dbg(wpa_s, MSG_DEBUG, "Ignored received EAPOL frame since "
  3041. "no key management is configured");
  3042. return;
  3043. }
  3044. if (wpa_s->eapol_received == 0 &&
  3045. (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE) ||
  3046. !wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) ||
  3047. wpa_s->wpa_state != WPA_COMPLETED) &&
  3048. (wpa_s->current_ssid == NULL ||
  3049. wpa_s->current_ssid->mode != IEEE80211_MODE_IBSS)) {
  3050. /* Timeout for completing IEEE 802.1X and WPA authentication */
  3051. int timeout = 10;
  3052. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  3053. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA ||
  3054. wpa_s->key_mgmt == WPA_KEY_MGMT_WPS) {
  3055. /* Use longer timeout for IEEE 802.1X/EAP */
  3056. timeout = 70;
  3057. }
  3058. #ifdef CONFIG_WPS
  3059. if (wpa_s->current_ssid && wpa_s->current_bss &&
  3060. (wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS) &&
  3061. eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
  3062. /*
  3063. * Use shorter timeout if going through WPS AP iteration
  3064. * for PIN config method with an AP that does not
  3065. * advertise Selected Registrar.
  3066. */
  3067. struct wpabuf *wps_ie;
  3068. wps_ie = wpa_bss_get_vendor_ie_multi(
  3069. wpa_s->current_bss, WPS_IE_VENDOR_TYPE);
  3070. if (wps_ie &&
  3071. !wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 1))
  3072. timeout = 10;
  3073. wpabuf_free(wps_ie);
  3074. }
  3075. #endif /* CONFIG_WPS */
  3076. wpa_supplicant_req_auth_timeout(wpa_s, timeout, 0);
  3077. }
  3078. wpa_s->eapol_received++;
  3079. if (wpa_s->countermeasures) {
  3080. wpa_msg(wpa_s, MSG_INFO, "WPA: Countermeasures - dropped "
  3081. "EAPOL packet");
  3082. return;
  3083. }
  3084. #ifdef CONFIG_IBSS_RSN
  3085. if (wpa_s->current_ssid &&
  3086. wpa_s->current_ssid->mode == WPAS_MODE_IBSS) {
  3087. ibss_rsn_rx_eapol(wpa_s->ibss_rsn, src_addr, buf, len);
  3088. return;
  3089. }
  3090. #endif /* CONFIG_IBSS_RSN */
  3091. /* Source address of the incoming EAPOL frame could be compared to the
  3092. * current BSSID. However, it is possible that a centralized
  3093. * Authenticator could be using another MAC address than the BSSID of
  3094. * an AP, so just allow any address to be used for now. The replies are
  3095. * still sent to the current BSSID (if available), though. */
  3096. os_memcpy(wpa_s->last_eapol_src, src_addr, ETH_ALEN);
  3097. if (!wpa_key_mgmt_wpa_psk(wpa_s->key_mgmt) &&
  3098. eapol_sm_rx_eapol(wpa_s->eapol, src_addr, buf, len) > 0)
  3099. return;
  3100. wpa_drv_poll(wpa_s);
  3101. if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_4WAY_HANDSHAKE))
  3102. wpa_sm_rx_eapol(wpa_s->wpa, src_addr, buf, len);
  3103. else if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  3104. /*
  3105. * Set portValid = TRUE here since we are going to skip 4-way
  3106. * handshake processing which would normally set portValid. We
  3107. * need this to allow the EAPOL state machines to be completed
  3108. * without going through EAPOL-Key handshake.
  3109. */
  3110. eapol_sm_notify_portValid(wpa_s->eapol, TRUE);
  3111. }
  3112. }
  3113. int wpa_supplicant_update_mac_addr(struct wpa_supplicant *wpa_s)
  3114. {
  3115. if ((!wpa_s->p2p_mgmt ||
  3116. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
  3117. !(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)) {
  3118. l2_packet_deinit(wpa_s->l2);
  3119. wpa_s->l2 = l2_packet_init(wpa_s->ifname,
  3120. wpa_drv_get_mac_addr(wpa_s),
  3121. ETH_P_EAPOL,
  3122. wpa_supplicant_rx_eapol, wpa_s, 0);
  3123. if (wpa_s->l2 == NULL)
  3124. return -1;
  3125. } else {
  3126. const u8 *addr = wpa_drv_get_mac_addr(wpa_s);
  3127. if (addr)
  3128. os_memcpy(wpa_s->own_addr, addr, ETH_ALEN);
  3129. }
  3130. if (wpa_s->l2 && l2_packet_get_own_addr(wpa_s->l2, wpa_s->own_addr)) {
  3131. wpa_msg(wpa_s, MSG_ERROR, "Failed to get own L2 address");
  3132. return -1;
  3133. }
  3134. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  3135. return 0;
  3136. }
  3137. static void wpa_supplicant_rx_eapol_bridge(void *ctx, const u8 *src_addr,
  3138. const u8 *buf, size_t len)
  3139. {
  3140. struct wpa_supplicant *wpa_s = ctx;
  3141. const struct l2_ethhdr *eth;
  3142. if (len < sizeof(*eth))
  3143. return;
  3144. eth = (const struct l2_ethhdr *) buf;
  3145. if (os_memcmp(eth->h_dest, wpa_s->own_addr, ETH_ALEN) != 0 &&
  3146. !(eth->h_dest[0] & 0x01)) {
  3147. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  3148. " (bridge - not for this interface - ignore)",
  3149. MAC2STR(src_addr), MAC2STR(eth->h_dest));
  3150. return;
  3151. }
  3152. wpa_dbg(wpa_s, MSG_DEBUG, "RX EAPOL from " MACSTR " to " MACSTR
  3153. " (bridge)", MAC2STR(src_addr), MAC2STR(eth->h_dest));
  3154. wpa_supplicant_rx_eapol(wpa_s, src_addr, buf + sizeof(*eth),
  3155. len - sizeof(*eth));
  3156. }
  3157. /**
  3158. * wpa_supplicant_driver_init - Initialize driver interface parameters
  3159. * @wpa_s: Pointer to wpa_supplicant data
  3160. * Returns: 0 on success, -1 on failure
  3161. *
  3162. * This function is called to initialize driver interface parameters.
  3163. * wpa_drv_init() must have been called before this function to initialize the
  3164. * driver interface.
  3165. */
  3166. int wpa_supplicant_driver_init(struct wpa_supplicant *wpa_s)
  3167. {
  3168. static int interface_count = 0;
  3169. if (wpa_supplicant_update_mac_addr(wpa_s) < 0)
  3170. return -1;
  3171. wpa_dbg(wpa_s, MSG_DEBUG, "Own MAC address: " MACSTR,
  3172. MAC2STR(wpa_s->own_addr));
  3173. os_memcpy(wpa_s->perm_addr, wpa_s->own_addr, ETH_ALEN);
  3174. wpa_sm_set_own_addr(wpa_s->wpa, wpa_s->own_addr);
  3175. if (wpa_s->bridge_ifname[0]) {
  3176. wpa_dbg(wpa_s, MSG_DEBUG, "Receiving packets from bridge "
  3177. "interface '%s'", wpa_s->bridge_ifname);
  3178. wpa_s->l2_br = l2_packet_init_bridge(
  3179. wpa_s->bridge_ifname, wpa_s->ifname, wpa_s->own_addr,
  3180. ETH_P_EAPOL, wpa_supplicant_rx_eapol_bridge, wpa_s, 1);
  3181. if (wpa_s->l2_br == NULL) {
  3182. wpa_msg(wpa_s, MSG_ERROR, "Failed to open l2_packet "
  3183. "connection for the bridge interface '%s'",
  3184. wpa_s->bridge_ifname);
  3185. return -1;
  3186. }
  3187. }
  3188. if (wpa_s->conf->ap_scan == 2 &&
  3189. os_strcmp(wpa_s->driver->name, "nl80211") == 0) {
  3190. wpa_printf(MSG_INFO,
  3191. "Note: nl80211 driver interface is not designed to be used with ap_scan=2; this can result in connection failures");
  3192. }
  3193. wpa_clear_keys(wpa_s, NULL);
  3194. /* Make sure that TKIP countermeasures are not left enabled (could
  3195. * happen if wpa_supplicant is killed during countermeasures. */
  3196. wpa_drv_set_countermeasures(wpa_s, 0);
  3197. wpa_dbg(wpa_s, MSG_DEBUG, "RSN: flushing PMKID list in the driver");
  3198. wpa_drv_flush_pmkid(wpa_s);
  3199. wpa_s->prev_scan_ssid = WILDCARD_SSID_SCAN;
  3200. wpa_s->prev_scan_wildcard = 0;
  3201. if (wpa_supplicant_enabled_networks(wpa_s)) {
  3202. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3203. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  3204. interface_count = 0;
  3205. }
  3206. #ifndef ANDROID
  3207. if (!wpa_s->p2p_mgmt &&
  3208. wpa_supplicant_delayed_sched_scan(wpa_s,
  3209. interface_count % 3,
  3210. 100000))
  3211. wpa_supplicant_req_scan(wpa_s, interface_count % 3,
  3212. 100000);
  3213. #endif /* ANDROID */
  3214. interface_count++;
  3215. } else
  3216. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  3217. return 0;
  3218. }
  3219. static int wpa_supplicant_daemon(const char *pid_file)
  3220. {
  3221. wpa_printf(MSG_DEBUG, "Daemonize..");
  3222. return os_daemonize(pid_file);
  3223. }
  3224. static struct wpa_supplicant *
  3225. wpa_supplicant_alloc(struct wpa_supplicant *parent)
  3226. {
  3227. struct wpa_supplicant *wpa_s;
  3228. wpa_s = os_zalloc(sizeof(*wpa_s));
  3229. if (wpa_s == NULL)
  3230. return NULL;
  3231. wpa_s->scan_req = INITIAL_SCAN_REQ;
  3232. wpa_s->scan_interval = 5;
  3233. wpa_s->new_connection = 1;
  3234. wpa_s->parent = parent ? parent : wpa_s;
  3235. wpa_s->p2pdev = wpa_s->parent;
  3236. wpa_s->sched_scanning = 0;
  3237. dl_list_init(&wpa_s->bss_tmp_disallowed);
  3238. dl_list_init(&wpa_s->fils_hlp_req);
  3239. return wpa_s;
  3240. }
  3241. #ifdef CONFIG_HT_OVERRIDES
  3242. static int wpa_set_htcap_mcs(struct wpa_supplicant *wpa_s,
  3243. struct ieee80211_ht_capabilities *htcaps,
  3244. struct ieee80211_ht_capabilities *htcaps_mask,
  3245. const char *ht_mcs)
  3246. {
  3247. /* parse ht_mcs into hex array */
  3248. int i;
  3249. const char *tmp = ht_mcs;
  3250. char *end = NULL;
  3251. /* If ht_mcs is null, do not set anything */
  3252. if (!ht_mcs)
  3253. return 0;
  3254. /* This is what we are setting in the kernel */
  3255. os_memset(&htcaps->supported_mcs_set, 0, IEEE80211_HT_MCS_MASK_LEN);
  3256. wpa_msg(wpa_s, MSG_DEBUG, "set_htcap, ht_mcs -:%s:-", ht_mcs);
  3257. for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
  3258. long v;
  3259. errno = 0;
  3260. v = strtol(tmp, &end, 16);
  3261. if (errno == 0) {
  3262. wpa_msg(wpa_s, MSG_DEBUG,
  3263. "htcap value[%i]: %ld end: %p tmp: %p",
  3264. i, v, end, tmp);
  3265. if (end == tmp)
  3266. break;
  3267. htcaps->supported_mcs_set[i] = v;
  3268. tmp = end;
  3269. } else {
  3270. wpa_msg(wpa_s, MSG_ERROR,
  3271. "Failed to parse ht-mcs: %s, error: %s\n",
  3272. ht_mcs, strerror(errno));
  3273. return -1;
  3274. }
  3275. }
  3276. /*
  3277. * If we were able to parse any values, then set mask for the MCS set.
  3278. */
  3279. if (i) {
  3280. os_memset(&htcaps_mask->supported_mcs_set, 0xff,
  3281. IEEE80211_HT_MCS_MASK_LEN - 1);
  3282. /* skip the 3 reserved bits */
  3283. htcaps_mask->supported_mcs_set[IEEE80211_HT_MCS_MASK_LEN - 1] =
  3284. 0x1f;
  3285. }
  3286. return 0;
  3287. }
  3288. static int wpa_disable_max_amsdu(struct wpa_supplicant *wpa_s,
  3289. struct ieee80211_ht_capabilities *htcaps,
  3290. struct ieee80211_ht_capabilities *htcaps_mask,
  3291. int disabled)
  3292. {
  3293. le16 msk;
  3294. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_max_amsdu: %d", disabled);
  3295. if (disabled == -1)
  3296. return 0;
  3297. msk = host_to_le16(HT_CAP_INFO_MAX_AMSDU_SIZE);
  3298. htcaps_mask->ht_capabilities_info |= msk;
  3299. if (disabled)
  3300. htcaps->ht_capabilities_info &= msk;
  3301. else
  3302. htcaps->ht_capabilities_info |= msk;
  3303. return 0;
  3304. }
  3305. static int wpa_set_ampdu_factor(struct wpa_supplicant *wpa_s,
  3306. struct ieee80211_ht_capabilities *htcaps,
  3307. struct ieee80211_ht_capabilities *htcaps_mask,
  3308. int factor)
  3309. {
  3310. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_factor: %d", factor);
  3311. if (factor == -1)
  3312. return 0;
  3313. if (factor < 0 || factor > 3) {
  3314. wpa_msg(wpa_s, MSG_ERROR, "ampdu_factor: %d out of range. "
  3315. "Must be 0-3 or -1", factor);
  3316. return -EINVAL;
  3317. }
  3318. htcaps_mask->a_mpdu_params |= 0x3; /* 2 bits for factor */
  3319. htcaps->a_mpdu_params &= ~0x3;
  3320. htcaps->a_mpdu_params |= factor & 0x3;
  3321. return 0;
  3322. }
  3323. static int wpa_set_ampdu_density(struct wpa_supplicant *wpa_s,
  3324. struct ieee80211_ht_capabilities *htcaps,
  3325. struct ieee80211_ht_capabilities *htcaps_mask,
  3326. int density)
  3327. {
  3328. wpa_msg(wpa_s, MSG_DEBUG, "set_ampdu_density: %d", density);
  3329. if (density == -1)
  3330. return 0;
  3331. if (density < 0 || density > 7) {
  3332. wpa_msg(wpa_s, MSG_ERROR,
  3333. "ampdu_density: %d out of range. Must be 0-7 or -1.",
  3334. density);
  3335. return -EINVAL;
  3336. }
  3337. htcaps_mask->a_mpdu_params |= 0x1C;
  3338. htcaps->a_mpdu_params &= ~(0x1C);
  3339. htcaps->a_mpdu_params |= (density << 2) & 0x1C;
  3340. return 0;
  3341. }
  3342. static int wpa_set_disable_ht40(struct wpa_supplicant *wpa_s,
  3343. struct ieee80211_ht_capabilities *htcaps,
  3344. struct ieee80211_ht_capabilities *htcaps_mask,
  3345. int disabled)
  3346. {
  3347. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ht40: %d", disabled);
  3348. set_disable_ht40(htcaps, disabled);
  3349. set_disable_ht40(htcaps_mask, 0);
  3350. return 0;
  3351. }
  3352. static int wpa_set_disable_sgi(struct wpa_supplicant *wpa_s,
  3353. struct ieee80211_ht_capabilities *htcaps,
  3354. struct ieee80211_ht_capabilities *htcaps_mask,
  3355. int disabled)
  3356. {
  3357. /* Masking these out disables SGI */
  3358. le16 msk = host_to_le16(HT_CAP_INFO_SHORT_GI20MHZ |
  3359. HT_CAP_INFO_SHORT_GI40MHZ);
  3360. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_sgi: %d", disabled);
  3361. if (disabled)
  3362. htcaps->ht_capabilities_info &= ~msk;
  3363. else
  3364. htcaps->ht_capabilities_info |= msk;
  3365. htcaps_mask->ht_capabilities_info |= msk;
  3366. return 0;
  3367. }
  3368. static int wpa_set_disable_ldpc(struct wpa_supplicant *wpa_s,
  3369. struct ieee80211_ht_capabilities *htcaps,
  3370. struct ieee80211_ht_capabilities *htcaps_mask,
  3371. int disabled)
  3372. {
  3373. /* Masking these out disables LDPC */
  3374. le16 msk = host_to_le16(HT_CAP_INFO_LDPC_CODING_CAP);
  3375. wpa_msg(wpa_s, MSG_DEBUG, "set_disable_ldpc: %d", disabled);
  3376. if (disabled)
  3377. htcaps->ht_capabilities_info &= ~msk;
  3378. else
  3379. htcaps->ht_capabilities_info |= msk;
  3380. htcaps_mask->ht_capabilities_info |= msk;
  3381. return 0;
  3382. }
  3383. void wpa_supplicant_apply_ht_overrides(
  3384. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  3385. struct wpa_driver_associate_params *params)
  3386. {
  3387. struct ieee80211_ht_capabilities *htcaps;
  3388. struct ieee80211_ht_capabilities *htcaps_mask;
  3389. if (!ssid)
  3390. return;
  3391. params->disable_ht = ssid->disable_ht;
  3392. if (!params->htcaps || !params->htcaps_mask)
  3393. return;
  3394. htcaps = (struct ieee80211_ht_capabilities *) params->htcaps;
  3395. htcaps_mask = (struct ieee80211_ht_capabilities *) params->htcaps_mask;
  3396. wpa_set_htcap_mcs(wpa_s, htcaps, htcaps_mask, ssid->ht_mcs);
  3397. wpa_disable_max_amsdu(wpa_s, htcaps, htcaps_mask,
  3398. ssid->disable_max_amsdu);
  3399. wpa_set_ampdu_factor(wpa_s, htcaps, htcaps_mask, ssid->ampdu_factor);
  3400. wpa_set_ampdu_density(wpa_s, htcaps, htcaps_mask, ssid->ampdu_density);
  3401. wpa_set_disable_ht40(wpa_s, htcaps, htcaps_mask, ssid->disable_ht40);
  3402. wpa_set_disable_sgi(wpa_s, htcaps, htcaps_mask, ssid->disable_sgi);
  3403. wpa_set_disable_ldpc(wpa_s, htcaps, htcaps_mask, ssid->disable_ldpc);
  3404. if (ssid->ht40_intolerant) {
  3405. le16 bit = host_to_le16(HT_CAP_INFO_40MHZ_INTOLERANT);
  3406. htcaps->ht_capabilities_info |= bit;
  3407. htcaps_mask->ht_capabilities_info |= bit;
  3408. }
  3409. }
  3410. #endif /* CONFIG_HT_OVERRIDES */
  3411. #ifdef CONFIG_VHT_OVERRIDES
  3412. void wpa_supplicant_apply_vht_overrides(
  3413. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  3414. struct wpa_driver_associate_params *params)
  3415. {
  3416. struct ieee80211_vht_capabilities *vhtcaps;
  3417. struct ieee80211_vht_capabilities *vhtcaps_mask;
  3418. if (!ssid)
  3419. return;
  3420. params->disable_vht = ssid->disable_vht;
  3421. vhtcaps = (void *) params->vhtcaps;
  3422. vhtcaps_mask = (void *) params->vhtcaps_mask;
  3423. if (!vhtcaps || !vhtcaps_mask)
  3424. return;
  3425. vhtcaps->vht_capabilities_info = host_to_le32(ssid->vht_capa);
  3426. vhtcaps_mask->vht_capabilities_info = host_to_le32(ssid->vht_capa_mask);
  3427. #ifdef CONFIG_HT_OVERRIDES
  3428. /* if max ampdu is <= 3, we have to make the HT cap the same */
  3429. if (ssid->vht_capa_mask & VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) {
  3430. int max_ampdu;
  3431. max_ampdu = (ssid->vht_capa &
  3432. VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX) >>
  3433. VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MAX_SHIFT;
  3434. max_ampdu = max_ampdu < 3 ? max_ampdu : 3;
  3435. wpa_set_ampdu_factor(wpa_s,
  3436. (void *) params->htcaps,
  3437. (void *) params->htcaps_mask,
  3438. max_ampdu);
  3439. }
  3440. #endif /* CONFIG_HT_OVERRIDES */
  3441. #define OVERRIDE_MCS(i) \
  3442. if (ssid->vht_tx_mcs_nss_ ##i >= 0) { \
  3443. vhtcaps_mask->vht_supported_mcs_set.tx_map |= \
  3444. host_to_le16(3 << 2 * (i - 1)); \
  3445. vhtcaps->vht_supported_mcs_set.tx_map |= \
  3446. host_to_le16(ssid->vht_tx_mcs_nss_ ##i << \
  3447. 2 * (i - 1)); \
  3448. } \
  3449. if (ssid->vht_rx_mcs_nss_ ##i >= 0) { \
  3450. vhtcaps_mask->vht_supported_mcs_set.rx_map |= \
  3451. host_to_le16(3 << 2 * (i - 1)); \
  3452. vhtcaps->vht_supported_mcs_set.rx_map |= \
  3453. host_to_le16(ssid->vht_rx_mcs_nss_ ##i << \
  3454. 2 * (i - 1)); \
  3455. }
  3456. OVERRIDE_MCS(1);
  3457. OVERRIDE_MCS(2);
  3458. OVERRIDE_MCS(3);
  3459. OVERRIDE_MCS(4);
  3460. OVERRIDE_MCS(5);
  3461. OVERRIDE_MCS(6);
  3462. OVERRIDE_MCS(7);
  3463. OVERRIDE_MCS(8);
  3464. }
  3465. #endif /* CONFIG_VHT_OVERRIDES */
  3466. static int pcsc_reader_init(struct wpa_supplicant *wpa_s)
  3467. {
  3468. #ifdef PCSC_FUNCS
  3469. size_t len;
  3470. if (!wpa_s->conf->pcsc_reader)
  3471. return 0;
  3472. wpa_s->scard = scard_init(wpa_s->conf->pcsc_reader);
  3473. if (!wpa_s->scard)
  3474. return 1;
  3475. if (wpa_s->conf->pcsc_pin &&
  3476. scard_set_pin(wpa_s->scard, wpa_s->conf->pcsc_pin) < 0) {
  3477. scard_deinit(wpa_s->scard);
  3478. wpa_s->scard = NULL;
  3479. wpa_msg(wpa_s, MSG_ERROR, "PC/SC PIN validation failed");
  3480. return -1;
  3481. }
  3482. len = sizeof(wpa_s->imsi) - 1;
  3483. if (scard_get_imsi(wpa_s->scard, wpa_s->imsi, &len)) {
  3484. scard_deinit(wpa_s->scard);
  3485. wpa_s->scard = NULL;
  3486. wpa_msg(wpa_s, MSG_ERROR, "Could not read IMSI");
  3487. return -1;
  3488. }
  3489. wpa_s->imsi[len] = '\0';
  3490. wpa_s->mnc_len = scard_get_mnc_len(wpa_s->scard);
  3491. wpa_printf(MSG_DEBUG, "SCARD: IMSI %s (MNC length %d)",
  3492. wpa_s->imsi, wpa_s->mnc_len);
  3493. wpa_sm_set_scard_ctx(wpa_s->wpa, wpa_s->scard);
  3494. eapol_sm_register_scard_ctx(wpa_s->eapol, wpa_s->scard);
  3495. #endif /* PCSC_FUNCS */
  3496. return 0;
  3497. }
  3498. int wpas_init_ext_pw(struct wpa_supplicant *wpa_s)
  3499. {
  3500. char *val, *pos;
  3501. ext_password_deinit(wpa_s->ext_pw);
  3502. wpa_s->ext_pw = NULL;
  3503. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, NULL);
  3504. if (!wpa_s->conf->ext_password_backend)
  3505. return 0;
  3506. val = os_strdup(wpa_s->conf->ext_password_backend);
  3507. if (val == NULL)
  3508. return -1;
  3509. pos = os_strchr(val, ':');
  3510. if (pos)
  3511. *pos++ = '\0';
  3512. wpa_printf(MSG_DEBUG, "EXT PW: Initialize backend '%s'", val);
  3513. wpa_s->ext_pw = ext_password_init(val, pos);
  3514. os_free(val);
  3515. if (wpa_s->ext_pw == NULL) {
  3516. wpa_printf(MSG_DEBUG, "EXT PW: Failed to initialize backend");
  3517. return -1;
  3518. }
  3519. eapol_sm_set_ext_pw_ctx(wpa_s->eapol, wpa_s->ext_pw);
  3520. return 0;
  3521. }
  3522. #ifdef CONFIG_FST
  3523. static const u8 * wpas_fst_get_bssid_cb(void *ctx)
  3524. {
  3525. struct wpa_supplicant *wpa_s = ctx;
  3526. return (is_zero_ether_addr(wpa_s->bssid) ||
  3527. wpa_s->wpa_state != WPA_COMPLETED) ? NULL : wpa_s->bssid;
  3528. }
  3529. static void wpas_fst_get_channel_info_cb(void *ctx,
  3530. enum hostapd_hw_mode *hw_mode,
  3531. u8 *channel)
  3532. {
  3533. struct wpa_supplicant *wpa_s = ctx;
  3534. if (wpa_s->current_bss) {
  3535. *hw_mode = ieee80211_freq_to_chan(wpa_s->current_bss->freq,
  3536. channel);
  3537. } else if (wpa_s->hw.num_modes) {
  3538. *hw_mode = wpa_s->hw.modes[0].mode;
  3539. } else {
  3540. WPA_ASSERT(0);
  3541. *hw_mode = 0;
  3542. }
  3543. }
  3544. static int wpas_fst_get_hw_modes(void *ctx, struct hostapd_hw_modes **modes)
  3545. {
  3546. struct wpa_supplicant *wpa_s = ctx;
  3547. *modes = wpa_s->hw.modes;
  3548. return wpa_s->hw.num_modes;
  3549. }
  3550. static void wpas_fst_set_ies_cb(void *ctx, const struct wpabuf *fst_ies)
  3551. {
  3552. struct wpa_supplicant *wpa_s = ctx;
  3553. wpa_hexdump_buf(MSG_DEBUG, "FST: Set IEs", fst_ies);
  3554. wpa_s->fst_ies = fst_ies;
  3555. }
  3556. static int wpas_fst_send_action_cb(void *ctx, const u8 *da, struct wpabuf *data)
  3557. {
  3558. struct wpa_supplicant *wpa_s = ctx;
  3559. if (os_memcmp(wpa_s->bssid, da, ETH_ALEN) != 0) {
  3560. wpa_printf(MSG_INFO, "FST:%s:bssid=" MACSTR " != da=" MACSTR,
  3561. __func__, MAC2STR(wpa_s->bssid), MAC2STR(da));
  3562. return -1;
  3563. }
  3564. return wpa_drv_send_action(wpa_s, wpa_s->assoc_freq, 0, wpa_s->bssid,
  3565. wpa_s->own_addr, wpa_s->bssid,
  3566. wpabuf_head(data), wpabuf_len(data),
  3567. 0);
  3568. }
  3569. static const struct wpabuf * wpas_fst_get_mb_ie_cb(void *ctx, const u8 *addr)
  3570. {
  3571. struct wpa_supplicant *wpa_s = ctx;
  3572. WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
  3573. return wpa_s->received_mb_ies;
  3574. }
  3575. static void wpas_fst_update_mb_ie_cb(void *ctx, const u8 *addr,
  3576. const u8 *buf, size_t size)
  3577. {
  3578. struct wpa_supplicant *wpa_s = ctx;
  3579. struct mb_ies_info info;
  3580. WPA_ASSERT(os_memcmp(wpa_s->bssid, addr, ETH_ALEN) == 0);
  3581. if (!mb_ies_info_by_ies(&info, buf, size)) {
  3582. wpabuf_free(wpa_s->received_mb_ies);
  3583. wpa_s->received_mb_ies = mb_ies_by_info(&info);
  3584. }
  3585. }
  3586. static const u8 * wpas_fst_get_peer_first(void *ctx,
  3587. struct fst_get_peer_ctx **get_ctx,
  3588. Boolean mb_only)
  3589. {
  3590. struct wpa_supplicant *wpa_s = ctx;
  3591. *get_ctx = NULL;
  3592. if (!is_zero_ether_addr(wpa_s->bssid))
  3593. return (wpa_s->received_mb_ies || !mb_only) ?
  3594. wpa_s->bssid : NULL;
  3595. return NULL;
  3596. }
  3597. static const u8 * wpas_fst_get_peer_next(void *ctx,
  3598. struct fst_get_peer_ctx **get_ctx,
  3599. Boolean mb_only)
  3600. {
  3601. return NULL;
  3602. }
  3603. void fst_wpa_supplicant_fill_iface_obj(struct wpa_supplicant *wpa_s,
  3604. struct fst_wpa_obj *iface_obj)
  3605. {
  3606. iface_obj->ctx = wpa_s;
  3607. iface_obj->get_bssid = wpas_fst_get_bssid_cb;
  3608. iface_obj->get_channel_info = wpas_fst_get_channel_info_cb;
  3609. iface_obj->get_hw_modes = wpas_fst_get_hw_modes;
  3610. iface_obj->set_ies = wpas_fst_set_ies_cb;
  3611. iface_obj->send_action = wpas_fst_send_action_cb;
  3612. iface_obj->get_mb_ie = wpas_fst_get_mb_ie_cb;
  3613. iface_obj->update_mb_ie = wpas_fst_update_mb_ie_cb;
  3614. iface_obj->get_peer_first = wpas_fst_get_peer_first;
  3615. iface_obj->get_peer_next = wpas_fst_get_peer_next;
  3616. }
  3617. #endif /* CONFIG_FST */
  3618. static int wpas_set_wowlan_triggers(struct wpa_supplicant *wpa_s,
  3619. const struct wpa_driver_capa *capa)
  3620. {
  3621. struct wowlan_triggers *triggers;
  3622. int ret = 0;
  3623. if (!wpa_s->conf->wowlan_triggers)
  3624. return 0;
  3625. triggers = wpa_get_wowlan_triggers(wpa_s->conf->wowlan_triggers, capa);
  3626. if (triggers) {
  3627. ret = wpa_drv_wowlan(wpa_s, triggers);
  3628. os_free(triggers);
  3629. }
  3630. return ret;
  3631. }
  3632. enum wpa_radio_work_band wpas_freq_to_band(int freq)
  3633. {
  3634. if (freq < 3000)
  3635. return BAND_2_4_GHZ;
  3636. if (freq > 50000)
  3637. return BAND_60_GHZ;
  3638. return BAND_5_GHZ;
  3639. }
  3640. unsigned int wpas_get_bands(struct wpa_supplicant *wpa_s, const int *freqs)
  3641. {
  3642. int i;
  3643. unsigned int band = 0;
  3644. if (freqs) {
  3645. /* freqs are specified for the radio work */
  3646. for (i = 0; freqs[i]; i++)
  3647. band |= wpas_freq_to_band(freqs[i]);
  3648. } else {
  3649. /*
  3650. * freqs are not specified, implies all
  3651. * the supported freqs by HW
  3652. */
  3653. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  3654. if (wpa_s->hw.modes[i].num_channels != 0) {
  3655. if (wpa_s->hw.modes[i].mode ==
  3656. HOSTAPD_MODE_IEEE80211B ||
  3657. wpa_s->hw.modes[i].mode ==
  3658. HOSTAPD_MODE_IEEE80211G)
  3659. band |= BAND_2_4_GHZ;
  3660. else if (wpa_s->hw.modes[i].mode ==
  3661. HOSTAPD_MODE_IEEE80211A)
  3662. band |= BAND_5_GHZ;
  3663. else if (wpa_s->hw.modes[i].mode ==
  3664. HOSTAPD_MODE_IEEE80211AD)
  3665. band |= BAND_60_GHZ;
  3666. else if (wpa_s->hw.modes[i].mode ==
  3667. HOSTAPD_MODE_IEEE80211ANY)
  3668. band = BAND_2_4_GHZ | BAND_5_GHZ |
  3669. BAND_60_GHZ;
  3670. }
  3671. }
  3672. }
  3673. return band;
  3674. }
  3675. static struct wpa_radio * radio_add_interface(struct wpa_supplicant *wpa_s,
  3676. const char *rn)
  3677. {
  3678. struct wpa_supplicant *iface = wpa_s->global->ifaces;
  3679. struct wpa_radio *radio;
  3680. while (rn && iface) {
  3681. radio = iface->radio;
  3682. if (radio && os_strcmp(rn, radio->name) == 0) {
  3683. wpa_printf(MSG_DEBUG, "Add interface %s to existing radio %s",
  3684. wpa_s->ifname, rn);
  3685. dl_list_add(&radio->ifaces, &wpa_s->radio_list);
  3686. return radio;
  3687. }
  3688. iface = iface->next;
  3689. }
  3690. wpa_printf(MSG_DEBUG, "Add interface %s to a new radio %s",
  3691. wpa_s->ifname, rn ? rn : "N/A");
  3692. radio = os_zalloc(sizeof(*radio));
  3693. if (radio == NULL)
  3694. return NULL;
  3695. if (rn)
  3696. os_strlcpy(radio->name, rn, sizeof(radio->name));
  3697. dl_list_init(&radio->ifaces);
  3698. dl_list_init(&radio->work);
  3699. dl_list_add(&radio->ifaces, &wpa_s->radio_list);
  3700. return radio;
  3701. }
  3702. static void radio_work_free(struct wpa_radio_work *work)
  3703. {
  3704. if (work->wpa_s->scan_work == work) {
  3705. /* This should not really happen. */
  3706. wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as scan_work",
  3707. work->type, work, work->started);
  3708. work->wpa_s->scan_work = NULL;
  3709. }
  3710. #ifdef CONFIG_P2P
  3711. if (work->wpa_s->p2p_scan_work == work) {
  3712. /* This should not really happen. */
  3713. wpa_dbg(work->wpa_s, MSG_INFO, "Freeing radio work '%s'@%p (started=%d) that is marked as p2p_scan_work",
  3714. work->type, work, work->started);
  3715. work->wpa_s->p2p_scan_work = NULL;
  3716. }
  3717. #endif /* CONFIG_P2P */
  3718. if (work->started) {
  3719. work->wpa_s->radio->num_active_works--;
  3720. wpa_dbg(work->wpa_s, MSG_DEBUG,
  3721. "radio_work_free('%s'@%p: num_active_works --> %u",
  3722. work->type, work,
  3723. work->wpa_s->radio->num_active_works);
  3724. }
  3725. dl_list_del(&work->list);
  3726. os_free(work);
  3727. }
  3728. static int radio_work_is_connect(struct wpa_radio_work *work)
  3729. {
  3730. return os_strcmp(work->type, "sme-connect") == 0 ||
  3731. os_strcmp(work->type, "connect") == 0;
  3732. }
  3733. static int radio_work_is_scan(struct wpa_radio_work *work)
  3734. {
  3735. return os_strcmp(work->type, "scan") == 0 ||
  3736. os_strcmp(work->type, "p2p-scan") == 0;
  3737. }
  3738. static struct wpa_radio_work * radio_work_get_next_work(struct wpa_radio *radio)
  3739. {
  3740. struct wpa_radio_work *active_work = NULL;
  3741. struct wpa_radio_work *tmp;
  3742. /* Get the active work to know the type and band. */
  3743. dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
  3744. if (tmp->started) {
  3745. active_work = tmp;
  3746. break;
  3747. }
  3748. }
  3749. if (!active_work) {
  3750. /* No active work, start one */
  3751. radio->num_active_works = 0;
  3752. dl_list_for_each(tmp, &radio->work, struct wpa_radio_work,
  3753. list) {
  3754. if (os_strcmp(tmp->type, "scan") == 0 &&
  3755. radio->external_scan_running &&
  3756. (((struct wpa_driver_scan_params *)
  3757. tmp->ctx)->only_new_results ||
  3758. tmp->wpa_s->clear_driver_scan_cache))
  3759. continue;
  3760. return tmp;
  3761. }
  3762. return NULL;
  3763. }
  3764. if (radio_work_is_connect(active_work)) {
  3765. /*
  3766. * If the active work is either connect or sme-connect,
  3767. * do not parallelize them with other radio works.
  3768. */
  3769. wpa_dbg(active_work->wpa_s, MSG_DEBUG,
  3770. "Do not parallelize radio work with %s",
  3771. active_work->type);
  3772. return NULL;
  3773. }
  3774. dl_list_for_each(tmp, &radio->work, struct wpa_radio_work, list) {
  3775. if (tmp->started)
  3776. continue;
  3777. /*
  3778. * If connect or sme-connect are enqueued, parallelize only
  3779. * those operations ahead of them in the queue.
  3780. */
  3781. if (radio_work_is_connect(tmp))
  3782. break;
  3783. /* Serialize parallel scan and p2p_scan operations on the same
  3784. * interface since the driver_nl80211 mechanism for tracking
  3785. * scan cookies does not yet have support for this. */
  3786. if (active_work->wpa_s == tmp->wpa_s &&
  3787. radio_work_is_scan(active_work) &&
  3788. radio_work_is_scan(tmp)) {
  3789. wpa_dbg(active_work->wpa_s, MSG_DEBUG,
  3790. "Do not start work '%s' when another work '%s' is already scheduled",
  3791. tmp->type, active_work->type);
  3792. continue;
  3793. }
  3794. /*
  3795. * Check that the radio works are distinct and
  3796. * on different bands.
  3797. */
  3798. if (os_strcmp(active_work->type, tmp->type) != 0 &&
  3799. (active_work->bands != tmp->bands)) {
  3800. /*
  3801. * If a scan has to be scheduled through nl80211 scan
  3802. * interface and if an external scan is already running,
  3803. * do not schedule the scan since it is likely to get
  3804. * rejected by kernel.
  3805. */
  3806. if (os_strcmp(tmp->type, "scan") == 0 &&
  3807. radio->external_scan_running &&
  3808. (((struct wpa_driver_scan_params *)
  3809. tmp->ctx)->only_new_results ||
  3810. tmp->wpa_s->clear_driver_scan_cache))
  3811. continue;
  3812. wpa_dbg(active_work->wpa_s, MSG_DEBUG,
  3813. "active_work:%s new_work:%s",
  3814. active_work->type, tmp->type);
  3815. return tmp;
  3816. }
  3817. }
  3818. /* Did not find a radio work to schedule in parallel. */
  3819. return NULL;
  3820. }
  3821. static void radio_start_next_work(void *eloop_ctx, void *timeout_ctx)
  3822. {
  3823. struct wpa_radio *radio = eloop_ctx;
  3824. struct wpa_radio_work *work;
  3825. struct os_reltime now, diff;
  3826. struct wpa_supplicant *wpa_s;
  3827. work = dl_list_first(&radio->work, struct wpa_radio_work, list);
  3828. if (work == NULL) {
  3829. radio->num_active_works = 0;
  3830. return;
  3831. }
  3832. wpa_s = dl_list_first(&radio->ifaces, struct wpa_supplicant,
  3833. radio_list);
  3834. if (!(wpa_s &&
  3835. wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)) {
  3836. if (work->started)
  3837. return; /* already started and still in progress */
  3838. if (wpa_s && wpa_s->radio->external_scan_running) {
  3839. wpa_printf(MSG_DEBUG, "Delay radio work start until externally triggered scan completes");
  3840. return;
  3841. }
  3842. } else {
  3843. work = NULL;
  3844. if (radio->num_active_works < MAX_ACTIVE_WORKS) {
  3845. /* get the work to schedule next */
  3846. work = radio_work_get_next_work(radio);
  3847. }
  3848. if (!work)
  3849. return;
  3850. }
  3851. wpa_s = work->wpa_s;
  3852. os_get_reltime(&now);
  3853. os_reltime_sub(&now, &work->time, &diff);
  3854. wpa_dbg(wpa_s, MSG_DEBUG,
  3855. "Starting radio work '%s'@%p after %ld.%06ld second wait",
  3856. work->type, work, diff.sec, diff.usec);
  3857. work->started = 1;
  3858. work->time = now;
  3859. radio->num_active_works++;
  3860. work->cb(work, 0);
  3861. if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS) &&
  3862. radio->num_active_works < MAX_ACTIVE_WORKS)
  3863. radio_work_check_next(wpa_s);
  3864. }
  3865. /*
  3866. * This function removes both started and pending radio works running on
  3867. * the provided interface's radio.
  3868. * Prior to the removal of the radio work, its callback (cb) is called with
  3869. * deinit set to be 1. Each work's callback is responsible for clearing its
  3870. * internal data and restoring to a correct state.
  3871. * @wpa_s: wpa_supplicant data
  3872. * @type: type of works to be removed
  3873. * @remove_all: 1 to remove all the works on this radio, 0 to remove only
  3874. * this interface's works.
  3875. */
  3876. void radio_remove_works(struct wpa_supplicant *wpa_s,
  3877. const char *type, int remove_all)
  3878. {
  3879. struct wpa_radio_work *work, *tmp;
  3880. struct wpa_radio *radio = wpa_s->radio;
  3881. dl_list_for_each_safe(work, tmp, &radio->work, struct wpa_radio_work,
  3882. list) {
  3883. if (type && os_strcmp(type, work->type) != 0)
  3884. continue;
  3885. /* skip other ifaces' works */
  3886. if (!remove_all && work->wpa_s != wpa_s)
  3887. continue;
  3888. wpa_dbg(wpa_s, MSG_DEBUG, "Remove radio work '%s'@%p%s",
  3889. work->type, work, work->started ? " (started)" : "");
  3890. work->cb(work, 1);
  3891. radio_work_free(work);
  3892. }
  3893. /* in case we removed the started work */
  3894. radio_work_check_next(wpa_s);
  3895. }
  3896. static void radio_remove_interface(struct wpa_supplicant *wpa_s)
  3897. {
  3898. struct wpa_radio *radio = wpa_s->radio;
  3899. if (!radio)
  3900. return;
  3901. wpa_printf(MSG_DEBUG, "Remove interface %s from radio %s",
  3902. wpa_s->ifname, radio->name);
  3903. dl_list_del(&wpa_s->radio_list);
  3904. radio_remove_works(wpa_s, NULL, 0);
  3905. wpa_s->radio = NULL;
  3906. if (!dl_list_empty(&radio->ifaces))
  3907. return; /* Interfaces remain for this radio */
  3908. wpa_printf(MSG_DEBUG, "Remove radio %s", radio->name);
  3909. eloop_cancel_timeout(radio_start_next_work, radio, NULL);
  3910. os_free(radio);
  3911. }
  3912. void radio_work_check_next(struct wpa_supplicant *wpa_s)
  3913. {
  3914. struct wpa_radio *radio = wpa_s->radio;
  3915. if (dl_list_empty(&radio->work))
  3916. return;
  3917. if (wpa_s->ext_work_in_progress) {
  3918. wpa_printf(MSG_DEBUG,
  3919. "External radio work in progress - delay start of pending item");
  3920. return;
  3921. }
  3922. eloop_cancel_timeout(radio_start_next_work, radio, NULL);
  3923. eloop_register_timeout(0, 0, radio_start_next_work, radio, NULL);
  3924. }
  3925. /**
  3926. * radio_add_work - Add a radio work item
  3927. * @wpa_s: Pointer to wpa_supplicant data
  3928. * @freq: Frequency of the offchannel operation in MHz or 0
  3929. * @type: Unique identifier for each type of work
  3930. * @next: Force as the next work to be executed
  3931. * @cb: Callback function for indicating when radio is available
  3932. * @ctx: Context pointer for the work (work->ctx in cb())
  3933. * Returns: 0 on success, -1 on failure
  3934. *
  3935. * This function is used to request time for an operation that requires
  3936. * exclusive radio control. Once the radio is available, the registered callback
  3937. * function will be called. radio_work_done() must be called once the exclusive
  3938. * radio operation has been completed, so that the radio is freed for other
  3939. * operations. The special case of deinit=1 is used to free the context data
  3940. * during interface removal. That does not allow the callback function to start
  3941. * the radio operation, i.e., it must free any resources allocated for the radio
  3942. * work and return.
  3943. *
  3944. * The @freq parameter can be used to indicate a single channel on which the
  3945. * offchannel operation will occur. This may allow multiple radio work
  3946. * operations to be performed in parallel if they apply for the same channel.
  3947. * Setting this to 0 indicates that the work item may use multiple channels or
  3948. * requires exclusive control of the radio.
  3949. */
  3950. int radio_add_work(struct wpa_supplicant *wpa_s, unsigned int freq,
  3951. const char *type, int next,
  3952. void (*cb)(struct wpa_radio_work *work, int deinit),
  3953. void *ctx)
  3954. {
  3955. struct wpa_radio *radio = wpa_s->radio;
  3956. struct wpa_radio_work *work;
  3957. int was_empty;
  3958. work = os_zalloc(sizeof(*work));
  3959. if (work == NULL)
  3960. return -1;
  3961. wpa_dbg(wpa_s, MSG_DEBUG, "Add radio work '%s'@%p", type, work);
  3962. os_get_reltime(&work->time);
  3963. work->freq = freq;
  3964. work->type = type;
  3965. work->wpa_s = wpa_s;
  3966. work->cb = cb;
  3967. work->ctx = ctx;
  3968. if (freq)
  3969. work->bands = wpas_freq_to_band(freq);
  3970. else if (os_strcmp(type, "scan") == 0 ||
  3971. os_strcmp(type, "p2p-scan") == 0)
  3972. work->bands = wpas_get_bands(wpa_s,
  3973. ((struct wpa_driver_scan_params *)
  3974. ctx)->freqs);
  3975. else
  3976. work->bands = wpas_get_bands(wpa_s, NULL);
  3977. was_empty = dl_list_empty(&wpa_s->radio->work);
  3978. if (next)
  3979. dl_list_add(&wpa_s->radio->work, &work->list);
  3980. else
  3981. dl_list_add_tail(&wpa_s->radio->work, &work->list);
  3982. if (was_empty) {
  3983. wpa_dbg(wpa_s, MSG_DEBUG, "First radio work item in the queue - schedule start immediately");
  3984. radio_work_check_next(wpa_s);
  3985. } else if ((wpa_s->drv_flags & WPA_DRIVER_FLAGS_OFFCHANNEL_SIMULTANEOUS)
  3986. && radio->num_active_works < MAX_ACTIVE_WORKS) {
  3987. wpa_dbg(wpa_s, MSG_DEBUG,
  3988. "Try to schedule a radio work (num_active_works=%u)",
  3989. radio->num_active_works);
  3990. radio_work_check_next(wpa_s);
  3991. }
  3992. return 0;
  3993. }
  3994. /**
  3995. * radio_work_done - Indicate that a radio work item has been completed
  3996. * @work: Completed work
  3997. *
  3998. * This function is called once the callback function registered with
  3999. * radio_add_work() has completed its work.
  4000. */
  4001. void radio_work_done(struct wpa_radio_work *work)
  4002. {
  4003. struct wpa_supplicant *wpa_s = work->wpa_s;
  4004. struct os_reltime now, diff;
  4005. unsigned int started = work->started;
  4006. os_get_reltime(&now);
  4007. os_reltime_sub(&now, &work->time, &diff);
  4008. wpa_dbg(wpa_s, MSG_DEBUG, "Radio work '%s'@%p %s in %ld.%06ld seconds",
  4009. work->type, work, started ? "done" : "canceled",
  4010. diff.sec, diff.usec);
  4011. radio_work_free(work);
  4012. if (started)
  4013. radio_work_check_next(wpa_s);
  4014. }
  4015. struct wpa_radio_work *
  4016. radio_work_pending(struct wpa_supplicant *wpa_s, const char *type)
  4017. {
  4018. struct wpa_radio_work *work;
  4019. struct wpa_radio *radio = wpa_s->radio;
  4020. dl_list_for_each(work, &radio->work, struct wpa_radio_work, list) {
  4021. if (work->wpa_s == wpa_s && os_strcmp(work->type, type) == 0)
  4022. return work;
  4023. }
  4024. return NULL;
  4025. }
  4026. static int wpas_init_driver(struct wpa_supplicant *wpa_s,
  4027. struct wpa_interface *iface)
  4028. {
  4029. const char *ifname, *driver, *rn;
  4030. driver = iface->driver;
  4031. next_driver:
  4032. if (wpa_supplicant_set_driver(wpa_s, driver) < 0)
  4033. return -1;
  4034. wpa_s->drv_priv = wpa_drv_init(wpa_s, wpa_s->ifname);
  4035. if (wpa_s->drv_priv == NULL) {
  4036. const char *pos;
  4037. pos = driver ? os_strchr(driver, ',') : NULL;
  4038. if (pos) {
  4039. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to initialize "
  4040. "driver interface - try next driver wrapper");
  4041. driver = pos + 1;
  4042. goto next_driver;
  4043. }
  4044. wpa_msg(wpa_s, MSG_ERROR, "Failed to initialize driver "
  4045. "interface");
  4046. return -1;
  4047. }
  4048. if (wpa_drv_set_param(wpa_s, wpa_s->conf->driver_param) < 0) {
  4049. wpa_msg(wpa_s, MSG_ERROR, "Driver interface rejected "
  4050. "driver_param '%s'", wpa_s->conf->driver_param);
  4051. return -1;
  4052. }
  4053. ifname = wpa_drv_get_ifname(wpa_s);
  4054. if (ifname && os_strcmp(ifname, wpa_s->ifname) != 0) {
  4055. wpa_dbg(wpa_s, MSG_DEBUG, "Driver interface replaced "
  4056. "interface name with '%s'", ifname);
  4057. os_strlcpy(wpa_s->ifname, ifname, sizeof(wpa_s->ifname));
  4058. }
  4059. rn = wpa_driver_get_radio_name(wpa_s);
  4060. if (rn && rn[0] == '\0')
  4061. rn = NULL;
  4062. wpa_s->radio = radio_add_interface(wpa_s, rn);
  4063. if (wpa_s->radio == NULL)
  4064. return -1;
  4065. return 0;
  4066. }
  4067. static int wpa_supplicant_init_iface(struct wpa_supplicant *wpa_s,
  4068. struct wpa_interface *iface)
  4069. {
  4070. struct wpa_driver_capa capa;
  4071. int capa_res;
  4072. wpa_printf(MSG_DEBUG, "Initializing interface '%s' conf '%s' driver "
  4073. "'%s' ctrl_interface '%s' bridge '%s'", iface->ifname,
  4074. iface->confname ? iface->confname : "N/A",
  4075. iface->driver ? iface->driver : "default",
  4076. iface->ctrl_interface ? iface->ctrl_interface : "N/A",
  4077. iface->bridge_ifname ? iface->bridge_ifname : "N/A");
  4078. if (iface->confname) {
  4079. #ifdef CONFIG_BACKEND_FILE
  4080. wpa_s->confname = os_rel2abs_path(iface->confname);
  4081. if (wpa_s->confname == NULL) {
  4082. wpa_printf(MSG_ERROR, "Failed to get absolute path "
  4083. "for configuration file '%s'.",
  4084. iface->confname);
  4085. return -1;
  4086. }
  4087. wpa_printf(MSG_DEBUG, "Configuration file '%s' -> '%s'",
  4088. iface->confname, wpa_s->confname);
  4089. #else /* CONFIG_BACKEND_FILE */
  4090. wpa_s->confname = os_strdup(iface->confname);
  4091. #endif /* CONFIG_BACKEND_FILE */
  4092. wpa_s->conf = wpa_config_read(wpa_s->confname, NULL);
  4093. if (wpa_s->conf == NULL) {
  4094. wpa_printf(MSG_ERROR, "Failed to read or parse "
  4095. "configuration '%s'.", wpa_s->confname);
  4096. return -1;
  4097. }
  4098. wpa_s->confanother = os_rel2abs_path(iface->confanother);
  4099. wpa_config_read(wpa_s->confanother, wpa_s->conf);
  4100. /*
  4101. * Override ctrl_interface and driver_param if set on command
  4102. * line.
  4103. */
  4104. if (iface->ctrl_interface) {
  4105. os_free(wpa_s->conf->ctrl_interface);
  4106. wpa_s->conf->ctrl_interface =
  4107. os_strdup(iface->ctrl_interface);
  4108. }
  4109. if (iface->driver_param) {
  4110. os_free(wpa_s->conf->driver_param);
  4111. wpa_s->conf->driver_param =
  4112. os_strdup(iface->driver_param);
  4113. }
  4114. if (iface->p2p_mgmt && !iface->ctrl_interface) {
  4115. os_free(wpa_s->conf->ctrl_interface);
  4116. wpa_s->conf->ctrl_interface = NULL;
  4117. }
  4118. } else
  4119. wpa_s->conf = wpa_config_alloc_empty(iface->ctrl_interface,
  4120. iface->driver_param);
  4121. if (wpa_s->conf == NULL) {
  4122. wpa_printf(MSG_ERROR, "\nNo configuration found.");
  4123. return -1;
  4124. }
  4125. if (iface->ifname == NULL) {
  4126. wpa_printf(MSG_ERROR, "\nInterface name is required.");
  4127. return -1;
  4128. }
  4129. if (os_strlen(iface->ifname) >= sizeof(wpa_s->ifname)) {
  4130. wpa_printf(MSG_ERROR, "\nToo long interface name '%s'.",
  4131. iface->ifname);
  4132. return -1;
  4133. }
  4134. os_strlcpy(wpa_s->ifname, iface->ifname, sizeof(wpa_s->ifname));
  4135. if (iface->bridge_ifname) {
  4136. if (os_strlen(iface->bridge_ifname) >=
  4137. sizeof(wpa_s->bridge_ifname)) {
  4138. wpa_printf(MSG_ERROR, "\nToo long bridge interface "
  4139. "name '%s'.", iface->bridge_ifname);
  4140. return -1;
  4141. }
  4142. os_strlcpy(wpa_s->bridge_ifname, iface->bridge_ifname,
  4143. sizeof(wpa_s->bridge_ifname));
  4144. }
  4145. /* RSNA Supplicant Key Management - INITIALIZE */
  4146. eapol_sm_notify_portEnabled(wpa_s->eapol, FALSE);
  4147. eapol_sm_notify_portValid(wpa_s->eapol, FALSE);
  4148. /* Initialize driver interface and register driver event handler before
  4149. * L2 receive handler so that association events are processed before
  4150. * EAPOL-Key packets if both become available for the same select()
  4151. * call. */
  4152. if (wpas_init_driver(wpa_s, iface) < 0)
  4153. return -1;
  4154. if (wpa_supplicant_init_wpa(wpa_s) < 0)
  4155. return -1;
  4156. wpa_sm_set_ifname(wpa_s->wpa, wpa_s->ifname,
  4157. wpa_s->bridge_ifname[0] ? wpa_s->bridge_ifname :
  4158. NULL);
  4159. wpa_sm_set_fast_reauth(wpa_s->wpa, wpa_s->conf->fast_reauth);
  4160. if (wpa_s->conf->dot11RSNAConfigPMKLifetime &&
  4161. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  4162. wpa_s->conf->dot11RSNAConfigPMKLifetime)) {
  4163. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  4164. "dot11RSNAConfigPMKLifetime");
  4165. return -1;
  4166. }
  4167. if (wpa_s->conf->dot11RSNAConfigPMKReauthThreshold &&
  4168. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  4169. wpa_s->conf->dot11RSNAConfigPMKReauthThreshold)) {
  4170. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  4171. "dot11RSNAConfigPMKReauthThreshold");
  4172. return -1;
  4173. }
  4174. if (wpa_s->conf->dot11RSNAConfigSATimeout &&
  4175. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  4176. wpa_s->conf->dot11RSNAConfigSATimeout)) {
  4177. wpa_msg(wpa_s, MSG_ERROR, "Invalid WPA parameter value for "
  4178. "dot11RSNAConfigSATimeout");
  4179. return -1;
  4180. }
  4181. wpa_s->hw.modes = wpa_drv_get_hw_feature_data(wpa_s,
  4182. &wpa_s->hw.num_modes,
  4183. &wpa_s->hw.flags);
  4184. if (wpa_s->hw.modes) {
  4185. u16 i;
  4186. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  4187. if (wpa_s->hw.modes[i].vht_capab) {
  4188. wpa_s->hw_capab = CAPAB_VHT;
  4189. break;
  4190. }
  4191. if (wpa_s->hw.modes[i].ht_capab &
  4192. HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)
  4193. wpa_s->hw_capab = CAPAB_HT40;
  4194. else if (wpa_s->hw.modes[i].ht_capab &&
  4195. wpa_s->hw_capab == CAPAB_NO_HT_VHT)
  4196. wpa_s->hw_capab = CAPAB_HT;
  4197. }
  4198. }
  4199. capa_res = wpa_drv_get_capa(wpa_s, &capa);
  4200. if (capa_res == 0) {
  4201. wpa_s->drv_capa_known = 1;
  4202. wpa_s->drv_flags = capa.flags;
  4203. wpa_s->drv_enc = capa.enc;
  4204. wpa_s->drv_smps_modes = capa.smps_modes;
  4205. wpa_s->drv_rrm_flags = capa.rrm_flags;
  4206. wpa_s->probe_resp_offloads = capa.probe_resp_offloads;
  4207. wpa_s->max_scan_ssids = capa.max_scan_ssids;
  4208. wpa_s->max_sched_scan_ssids = capa.max_sched_scan_ssids;
  4209. wpa_s->max_sched_scan_plans = capa.max_sched_scan_plans;
  4210. wpa_s->max_sched_scan_plan_interval =
  4211. capa.max_sched_scan_plan_interval;
  4212. wpa_s->max_sched_scan_plan_iterations =
  4213. capa.max_sched_scan_plan_iterations;
  4214. wpa_s->sched_scan_supported = capa.sched_scan_supported;
  4215. wpa_s->max_match_sets = capa.max_match_sets;
  4216. wpa_s->max_remain_on_chan = capa.max_remain_on_chan;
  4217. wpa_s->max_stations = capa.max_stations;
  4218. wpa_s->extended_capa = capa.extended_capa;
  4219. wpa_s->extended_capa_mask = capa.extended_capa_mask;
  4220. wpa_s->extended_capa_len = capa.extended_capa_len;
  4221. wpa_s->num_multichan_concurrent =
  4222. capa.num_multichan_concurrent;
  4223. wpa_s->wmm_ac_supported = capa.wmm_ac_supported;
  4224. if (capa.mac_addr_rand_scan_supported)
  4225. wpa_s->mac_addr_rand_supported |= MAC_ADDR_RAND_SCAN;
  4226. if (wpa_s->sched_scan_supported &&
  4227. capa.mac_addr_rand_sched_scan_supported)
  4228. wpa_s->mac_addr_rand_supported |=
  4229. (MAC_ADDR_RAND_SCHED_SCAN | MAC_ADDR_RAND_PNO);
  4230. }
  4231. if (wpa_s->max_remain_on_chan == 0)
  4232. wpa_s->max_remain_on_chan = 1000;
  4233. /*
  4234. * Only take p2p_mgmt parameters when P2P Device is supported.
  4235. * Doing it here as it determines whether l2_packet_init() will be done
  4236. * during wpa_supplicant_driver_init().
  4237. */
  4238. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)
  4239. wpa_s->p2p_mgmt = iface->p2p_mgmt;
  4240. else
  4241. iface->p2p_mgmt = 1;
  4242. if (wpa_s->num_multichan_concurrent == 0)
  4243. wpa_s->num_multichan_concurrent = 1;
  4244. if (wpa_supplicant_driver_init(wpa_s) < 0)
  4245. return -1;
  4246. #ifdef CONFIG_TDLS
  4247. if ((!iface->p2p_mgmt ||
  4248. !(wpa_s->drv_flags &
  4249. WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE)) &&
  4250. wpa_tdls_init(wpa_s->wpa))
  4251. return -1;
  4252. #endif /* CONFIG_TDLS */
  4253. if (wpa_s->conf->country[0] && wpa_s->conf->country[1] &&
  4254. wpa_drv_set_country(wpa_s, wpa_s->conf->country)) {
  4255. wpa_dbg(wpa_s, MSG_DEBUG, "Failed to set country");
  4256. return -1;
  4257. }
  4258. #ifdef CONFIG_FST
  4259. if (wpa_s->conf->fst_group_id) {
  4260. struct fst_iface_cfg cfg;
  4261. struct fst_wpa_obj iface_obj;
  4262. fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
  4263. os_strlcpy(cfg.group_id, wpa_s->conf->fst_group_id,
  4264. sizeof(cfg.group_id));
  4265. cfg.priority = wpa_s->conf->fst_priority;
  4266. cfg.llt = wpa_s->conf->fst_llt;
  4267. wpa_s->fst = fst_attach(wpa_s->ifname, wpa_s->own_addr,
  4268. &iface_obj, &cfg);
  4269. if (!wpa_s->fst) {
  4270. wpa_msg(wpa_s, MSG_ERROR,
  4271. "FST: Cannot attach iface %s to group %s",
  4272. wpa_s->ifname, cfg.group_id);
  4273. return -1;
  4274. }
  4275. }
  4276. #endif /* CONFIG_FST */
  4277. if (wpas_wps_init(wpa_s))
  4278. return -1;
  4279. if (wpa_supplicant_init_eapol(wpa_s) < 0)
  4280. return -1;
  4281. wpa_sm_set_eapol(wpa_s->wpa, wpa_s->eapol);
  4282. wpa_s->ctrl_iface = wpa_supplicant_ctrl_iface_init(wpa_s);
  4283. if (wpa_s->ctrl_iface == NULL) {
  4284. wpa_printf(MSG_ERROR,
  4285. "Failed to initialize control interface '%s'.\n"
  4286. "You may have another wpa_supplicant process "
  4287. "already running or the file was\n"
  4288. "left by an unclean termination of wpa_supplicant "
  4289. "in which case you will need\n"
  4290. "to manually remove this file before starting "
  4291. "wpa_supplicant again.\n",
  4292. wpa_s->conf->ctrl_interface);
  4293. return -1;
  4294. }
  4295. wpa_s->gas = gas_query_init(wpa_s);
  4296. if (wpa_s->gas == NULL) {
  4297. wpa_printf(MSG_ERROR, "Failed to initialize GAS query");
  4298. return -1;
  4299. }
  4300. if (iface->p2p_mgmt && wpas_p2p_init(wpa_s->global, wpa_s) < 0) {
  4301. wpa_msg(wpa_s, MSG_ERROR, "Failed to init P2P");
  4302. return -1;
  4303. }
  4304. if (wpa_bss_init(wpa_s) < 0)
  4305. return -1;
  4306. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  4307. #ifdef CONFIG_MESH
  4308. dl_list_init(&wpa_s->mesh_external_pmksa_cache);
  4309. #endif /* CONFIG_MESH */
  4310. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  4311. /*
  4312. * Set Wake-on-WLAN triggers, if configured.
  4313. * Note: We don't restore/remove the triggers on shutdown (it doesn't
  4314. * have effect anyway when the interface is down).
  4315. */
  4316. if (capa_res == 0 && wpas_set_wowlan_triggers(wpa_s, &capa) < 0)
  4317. return -1;
  4318. #ifdef CONFIG_EAP_PROXY
  4319. {
  4320. size_t len;
  4321. wpa_s->mnc_len = eapol_sm_get_eap_proxy_imsi(wpa_s->eapol, wpa_s->imsi,
  4322. &len);
  4323. if (wpa_s->mnc_len > 0) {
  4324. wpa_s->imsi[len] = '\0';
  4325. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI %s (MNC length %d)",
  4326. wpa_s->imsi, wpa_s->mnc_len);
  4327. } else {
  4328. wpa_printf(MSG_DEBUG, "eap_proxy: IMSI not available");
  4329. }
  4330. }
  4331. #endif /* CONFIG_EAP_PROXY */
  4332. if (pcsc_reader_init(wpa_s) < 0)
  4333. return -1;
  4334. if (wpas_init_ext_pw(wpa_s) < 0)
  4335. return -1;
  4336. wpas_rrm_reset(wpa_s);
  4337. wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
  4338. #ifdef CONFIG_HS20
  4339. hs20_init(wpa_s);
  4340. #endif /* CONFIG_HS20 */
  4341. #ifdef CONFIG_MBO
  4342. wpas_mbo_update_non_pref_chan(wpa_s, wpa_s->conf->non_pref_chan);
  4343. #endif /* CONFIG_MBO */
  4344. wpa_supplicant_set_default_scan_ies(wpa_s);
  4345. return 0;
  4346. }
  4347. static void wpa_supplicant_deinit_iface(struct wpa_supplicant *wpa_s,
  4348. int notify, int terminate)
  4349. {
  4350. struct wpa_global *global = wpa_s->global;
  4351. struct wpa_supplicant *iface, *prev;
  4352. if (wpa_s == wpa_s->parent)
  4353. wpas_p2p_group_remove(wpa_s, "*");
  4354. iface = global->ifaces;
  4355. while (iface) {
  4356. if (iface->p2pdev == wpa_s)
  4357. iface->p2pdev = iface->parent;
  4358. if (iface == wpa_s || iface->parent != wpa_s) {
  4359. iface = iface->next;
  4360. continue;
  4361. }
  4362. wpa_printf(MSG_DEBUG,
  4363. "Remove remaining child interface %s from parent %s",
  4364. iface->ifname, wpa_s->ifname);
  4365. prev = iface;
  4366. iface = iface->next;
  4367. wpa_supplicant_remove_iface(global, prev, terminate);
  4368. }
  4369. wpa_s->disconnected = 1;
  4370. if (wpa_s->drv_priv) {
  4371. wpa_supplicant_deauthenticate(wpa_s,
  4372. WLAN_REASON_DEAUTH_LEAVING);
  4373. wpa_drv_set_countermeasures(wpa_s, 0);
  4374. wpa_clear_keys(wpa_s, NULL);
  4375. }
  4376. wpa_supplicant_cleanup(wpa_s);
  4377. wpas_p2p_deinit_iface(wpa_s);
  4378. wpas_ctrl_radio_work_flush(wpa_s);
  4379. radio_remove_interface(wpa_s);
  4380. #ifdef CONFIG_FST
  4381. if (wpa_s->fst) {
  4382. fst_detach(wpa_s->fst);
  4383. wpa_s->fst = NULL;
  4384. }
  4385. if (wpa_s->received_mb_ies) {
  4386. wpabuf_free(wpa_s->received_mb_ies);
  4387. wpa_s->received_mb_ies = NULL;
  4388. }
  4389. #endif /* CONFIG_FST */
  4390. if (wpa_s->drv_priv)
  4391. wpa_drv_deinit(wpa_s);
  4392. if (notify)
  4393. wpas_notify_iface_removed(wpa_s);
  4394. if (terminate)
  4395. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TERMINATING);
  4396. if (wpa_s->ctrl_iface) {
  4397. wpa_supplicant_ctrl_iface_deinit(wpa_s->ctrl_iface);
  4398. wpa_s->ctrl_iface = NULL;
  4399. }
  4400. #ifdef CONFIG_MESH
  4401. if (wpa_s->ifmsh) {
  4402. wpa_supplicant_mesh_iface_deinit(wpa_s, wpa_s->ifmsh);
  4403. wpa_s->ifmsh = NULL;
  4404. }
  4405. #endif /* CONFIG_MESH */
  4406. if (wpa_s->conf != NULL) {
  4407. wpa_config_free(wpa_s->conf);
  4408. wpa_s->conf = NULL;
  4409. }
  4410. os_free(wpa_s->ssids_from_scan_req);
  4411. os_free(wpa_s);
  4412. }
  4413. #ifdef CONFIG_MATCH_IFACE
  4414. /**
  4415. * wpa_supplicant_match_iface - Match an interface description to a name
  4416. * @global: Pointer to global data from wpa_supplicant_init()
  4417. * @ifname: Name of the interface to match
  4418. * Returns: Pointer to the created interface description or %NULL on failure
  4419. */
  4420. struct wpa_interface * wpa_supplicant_match_iface(struct wpa_global *global,
  4421. const char *ifname)
  4422. {
  4423. int i;
  4424. struct wpa_interface *iface, *miface;
  4425. for (i = 0; i < global->params.match_iface_count; i++) {
  4426. miface = &global->params.match_ifaces[i];
  4427. if (!miface->ifname ||
  4428. fnmatch(miface->ifname, ifname, 0) == 0) {
  4429. iface = os_zalloc(sizeof(*iface));
  4430. if (!iface)
  4431. return NULL;
  4432. *iface = *miface;
  4433. iface->ifname = ifname;
  4434. return iface;
  4435. }
  4436. }
  4437. return NULL;
  4438. }
  4439. /**
  4440. * wpa_supplicant_match_existing - Match existing interfaces
  4441. * @global: Pointer to global data from wpa_supplicant_init()
  4442. * Returns: 0 on success, -1 on failure
  4443. */
  4444. static int wpa_supplicant_match_existing(struct wpa_global *global)
  4445. {
  4446. struct if_nameindex *ifi, *ifp;
  4447. struct wpa_supplicant *wpa_s;
  4448. struct wpa_interface *iface;
  4449. ifp = if_nameindex();
  4450. if (!ifp) {
  4451. wpa_printf(MSG_ERROR, "if_nameindex: %s", strerror(errno));
  4452. return -1;
  4453. }
  4454. for (ifi = ifp; ifi->if_name; ifi++) {
  4455. wpa_s = wpa_supplicant_get_iface(global, ifi->if_name);
  4456. if (wpa_s)
  4457. continue;
  4458. iface = wpa_supplicant_match_iface(global, ifi->if_name);
  4459. if (iface) {
  4460. wpa_s = wpa_supplicant_add_iface(global, iface, NULL);
  4461. os_free(iface);
  4462. if (wpa_s)
  4463. wpa_s->matched = 1;
  4464. }
  4465. }
  4466. if_freenameindex(ifp);
  4467. return 0;
  4468. }
  4469. #endif /* CONFIG_MATCH_IFACE */
  4470. /**
  4471. * wpa_supplicant_add_iface - Add a new network interface
  4472. * @global: Pointer to global data from wpa_supplicant_init()
  4473. * @iface: Interface configuration options
  4474. * @parent: Parent interface or %NULL to assign new interface as parent
  4475. * Returns: Pointer to the created interface or %NULL on failure
  4476. *
  4477. * This function is used to add new network interfaces for %wpa_supplicant.
  4478. * This can be called before wpa_supplicant_run() to add interfaces before the
  4479. * main event loop has been started. In addition, new interfaces can be added
  4480. * dynamically while %wpa_supplicant is already running. This could happen,
  4481. * e.g., when a hotplug network adapter is inserted.
  4482. */
  4483. struct wpa_supplicant * wpa_supplicant_add_iface(struct wpa_global *global,
  4484. struct wpa_interface *iface,
  4485. struct wpa_supplicant *parent)
  4486. {
  4487. struct wpa_supplicant *wpa_s;
  4488. struct wpa_interface t_iface;
  4489. struct wpa_ssid *ssid;
  4490. if (global == NULL || iface == NULL)
  4491. return NULL;
  4492. wpa_s = wpa_supplicant_alloc(parent);
  4493. if (wpa_s == NULL)
  4494. return NULL;
  4495. wpa_s->global = global;
  4496. t_iface = *iface;
  4497. if (global->params.override_driver) {
  4498. wpa_printf(MSG_DEBUG, "Override interface parameter: driver "
  4499. "('%s' -> '%s')",
  4500. iface->driver, global->params.override_driver);
  4501. t_iface.driver = global->params.override_driver;
  4502. }
  4503. if (global->params.override_ctrl_interface) {
  4504. wpa_printf(MSG_DEBUG, "Override interface parameter: "
  4505. "ctrl_interface ('%s' -> '%s')",
  4506. iface->ctrl_interface,
  4507. global->params.override_ctrl_interface);
  4508. t_iface.ctrl_interface =
  4509. global->params.override_ctrl_interface;
  4510. }
  4511. if (wpa_supplicant_init_iface(wpa_s, &t_iface)) {
  4512. wpa_printf(MSG_DEBUG, "Failed to add interface %s",
  4513. iface->ifname);
  4514. wpa_supplicant_deinit_iface(wpa_s, 0, 0);
  4515. return NULL;
  4516. }
  4517. if (iface->p2p_mgmt == 0) {
  4518. /* Notify the control interfaces about new iface */
  4519. if (wpas_notify_iface_added(wpa_s)) {
  4520. wpa_supplicant_deinit_iface(wpa_s, 1, 0);
  4521. return NULL;
  4522. }
  4523. for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
  4524. wpas_notify_network_added(wpa_s, ssid);
  4525. }
  4526. wpa_s->next = global->ifaces;
  4527. global->ifaces = wpa_s;
  4528. wpa_dbg(wpa_s, MSG_DEBUG, "Added interface %s", wpa_s->ifname);
  4529. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  4530. #ifdef CONFIG_P2P
  4531. if (wpa_s->global->p2p == NULL &&
  4532. !wpa_s->global->p2p_disabled && !wpa_s->conf->p2p_disabled &&
  4533. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
  4534. wpas_p2p_add_p2pdev_interface(
  4535. wpa_s, wpa_s->global->params.conf_p2p_dev) < 0) {
  4536. wpa_printf(MSG_INFO,
  4537. "P2P: Failed to enable P2P Device interface");
  4538. /* Try to continue without. P2P will be disabled. */
  4539. }
  4540. #endif /* CONFIG_P2P */
  4541. return wpa_s;
  4542. }
  4543. /**
  4544. * wpa_supplicant_remove_iface - Remove a network interface
  4545. * @global: Pointer to global data from wpa_supplicant_init()
  4546. * @wpa_s: Pointer to the network interface to be removed
  4547. * Returns: 0 if interface was removed, -1 if interface was not found
  4548. *
  4549. * This function can be used to dynamically remove network interfaces from
  4550. * %wpa_supplicant, e.g., when a hotplug network adapter is ejected. In
  4551. * addition, this function is used to remove all remaining interfaces when
  4552. * %wpa_supplicant is terminated.
  4553. */
  4554. int wpa_supplicant_remove_iface(struct wpa_global *global,
  4555. struct wpa_supplicant *wpa_s,
  4556. int terminate)
  4557. {
  4558. struct wpa_supplicant *prev;
  4559. #ifdef CONFIG_MESH
  4560. unsigned int mesh_if_created = wpa_s->mesh_if_created;
  4561. char *ifname = NULL;
  4562. struct wpa_supplicant *parent = wpa_s->parent;
  4563. #endif /* CONFIG_MESH */
  4564. /* Remove interface from the global list of interfaces */
  4565. prev = global->ifaces;
  4566. if (prev == wpa_s) {
  4567. global->ifaces = wpa_s->next;
  4568. } else {
  4569. while (prev && prev->next != wpa_s)
  4570. prev = prev->next;
  4571. if (prev == NULL)
  4572. return -1;
  4573. prev->next = wpa_s->next;
  4574. }
  4575. wpa_dbg(wpa_s, MSG_DEBUG, "Removing interface %s", wpa_s->ifname);
  4576. #ifdef CONFIG_MESH
  4577. if (mesh_if_created) {
  4578. ifname = os_strdup(wpa_s->ifname);
  4579. if (ifname == NULL) {
  4580. wpa_dbg(wpa_s, MSG_ERROR,
  4581. "mesh: Failed to malloc ifname");
  4582. return -1;
  4583. }
  4584. }
  4585. #endif /* CONFIG_MESH */
  4586. if (global->p2p_group_formation == wpa_s)
  4587. global->p2p_group_formation = NULL;
  4588. if (global->p2p_invite_group == wpa_s)
  4589. global->p2p_invite_group = NULL;
  4590. wpa_supplicant_deinit_iface(wpa_s, 1, terminate);
  4591. #ifdef CONFIG_MESH
  4592. if (mesh_if_created) {
  4593. wpa_drv_if_remove(parent, WPA_IF_MESH, ifname);
  4594. os_free(ifname);
  4595. }
  4596. #endif /* CONFIG_MESH */
  4597. return 0;
  4598. }
  4599. /**
  4600. * wpa_supplicant_get_eap_mode - Get the current EAP mode
  4601. * @wpa_s: Pointer to the network interface
  4602. * Returns: Pointer to the eap mode or the string "UNKNOWN" if not found
  4603. */
  4604. const char * wpa_supplicant_get_eap_mode(struct wpa_supplicant *wpa_s)
  4605. {
  4606. const char *eapol_method;
  4607. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) == 0 &&
  4608. wpa_s->key_mgmt != WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  4609. return "NO-EAP";
  4610. }
  4611. eapol_method = eapol_sm_get_method_name(wpa_s->eapol);
  4612. if (eapol_method == NULL)
  4613. return "UNKNOWN-EAP";
  4614. return eapol_method;
  4615. }
  4616. /**
  4617. * wpa_supplicant_get_iface - Get a new network interface
  4618. * @global: Pointer to global data from wpa_supplicant_init()
  4619. * @ifname: Interface name
  4620. * Returns: Pointer to the interface or %NULL if not found
  4621. */
  4622. struct wpa_supplicant * wpa_supplicant_get_iface(struct wpa_global *global,
  4623. const char *ifname)
  4624. {
  4625. struct wpa_supplicant *wpa_s;
  4626. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4627. if (os_strcmp(wpa_s->ifname, ifname) == 0)
  4628. return wpa_s;
  4629. }
  4630. return NULL;
  4631. }
  4632. #ifndef CONFIG_NO_WPA_MSG
  4633. static const char * wpa_supplicant_msg_ifname_cb(void *ctx)
  4634. {
  4635. struct wpa_supplicant *wpa_s = ctx;
  4636. if (wpa_s == NULL)
  4637. return NULL;
  4638. return wpa_s->ifname;
  4639. }
  4640. #endif /* CONFIG_NO_WPA_MSG */
  4641. #ifndef WPA_SUPPLICANT_CLEANUP_INTERVAL
  4642. #define WPA_SUPPLICANT_CLEANUP_INTERVAL 10
  4643. #endif /* WPA_SUPPLICANT_CLEANUP_INTERVAL */
  4644. /* Periodic cleanup tasks */
  4645. static void wpas_periodic(void *eloop_ctx, void *timeout_ctx)
  4646. {
  4647. struct wpa_global *global = eloop_ctx;
  4648. struct wpa_supplicant *wpa_s;
  4649. eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
  4650. wpas_periodic, global, NULL);
  4651. #ifdef CONFIG_P2P
  4652. if (global->p2p)
  4653. p2p_expire_peers(global->p2p);
  4654. #endif /* CONFIG_P2P */
  4655. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  4656. wpa_bss_flush_by_age(wpa_s, wpa_s->conf->bss_expiration_age);
  4657. #ifdef CONFIG_AP
  4658. ap_periodic(wpa_s);
  4659. #endif /* CONFIG_AP */
  4660. }
  4661. }
  4662. /**
  4663. * wpa_supplicant_init - Initialize %wpa_supplicant
  4664. * @params: Parameters for %wpa_supplicant
  4665. * Returns: Pointer to global %wpa_supplicant data, or %NULL on failure
  4666. *
  4667. * This function is used to initialize %wpa_supplicant. After successful
  4668. * initialization, the returned data pointer can be used to add and remove
  4669. * network interfaces, and eventually, to deinitialize %wpa_supplicant.
  4670. */
  4671. struct wpa_global * wpa_supplicant_init(struct wpa_params *params)
  4672. {
  4673. struct wpa_global *global;
  4674. int ret, i;
  4675. if (params == NULL)
  4676. return NULL;
  4677. #ifdef CONFIG_DRIVER_NDIS
  4678. {
  4679. void driver_ndis_init_ops(void);
  4680. driver_ndis_init_ops();
  4681. }
  4682. #endif /* CONFIG_DRIVER_NDIS */
  4683. #ifndef CONFIG_NO_WPA_MSG
  4684. wpa_msg_register_ifname_cb(wpa_supplicant_msg_ifname_cb);
  4685. #endif /* CONFIG_NO_WPA_MSG */
  4686. if (params->wpa_debug_file_path)
  4687. wpa_debug_open_file(params->wpa_debug_file_path);
  4688. else
  4689. wpa_debug_setup_stdout();
  4690. if (params->wpa_debug_syslog)
  4691. wpa_debug_open_syslog();
  4692. if (params->wpa_debug_tracing) {
  4693. ret = wpa_debug_open_linux_tracing();
  4694. if (ret) {
  4695. wpa_printf(MSG_ERROR,
  4696. "Failed to enable trace logging");
  4697. return NULL;
  4698. }
  4699. }
  4700. ret = eap_register_methods();
  4701. if (ret) {
  4702. wpa_printf(MSG_ERROR, "Failed to register EAP methods");
  4703. if (ret == -2)
  4704. wpa_printf(MSG_ERROR, "Two or more EAP methods used "
  4705. "the same EAP type.");
  4706. return NULL;
  4707. }
  4708. global = os_zalloc(sizeof(*global));
  4709. if (global == NULL)
  4710. return NULL;
  4711. dl_list_init(&global->p2p_srv_bonjour);
  4712. dl_list_init(&global->p2p_srv_upnp);
  4713. global->params.daemonize = params->daemonize;
  4714. global->params.wait_for_monitor = params->wait_for_monitor;
  4715. global->params.dbus_ctrl_interface = params->dbus_ctrl_interface;
  4716. if (params->pid_file)
  4717. global->params.pid_file = os_strdup(params->pid_file);
  4718. if (params->ctrl_interface)
  4719. global->params.ctrl_interface =
  4720. os_strdup(params->ctrl_interface);
  4721. if (params->ctrl_interface_group)
  4722. global->params.ctrl_interface_group =
  4723. os_strdup(params->ctrl_interface_group);
  4724. if (params->override_driver)
  4725. global->params.override_driver =
  4726. os_strdup(params->override_driver);
  4727. if (params->override_ctrl_interface)
  4728. global->params.override_ctrl_interface =
  4729. os_strdup(params->override_ctrl_interface);
  4730. #ifdef CONFIG_MATCH_IFACE
  4731. global->params.match_iface_count = params->match_iface_count;
  4732. if (params->match_iface_count) {
  4733. global->params.match_ifaces =
  4734. os_calloc(params->match_iface_count,
  4735. sizeof(struct wpa_interface));
  4736. os_memcpy(global->params.match_ifaces,
  4737. params->match_ifaces,
  4738. params->match_iface_count *
  4739. sizeof(struct wpa_interface));
  4740. }
  4741. #endif /* CONFIG_MATCH_IFACE */
  4742. #ifdef CONFIG_P2P
  4743. if (params->conf_p2p_dev)
  4744. global->params.conf_p2p_dev =
  4745. os_strdup(params->conf_p2p_dev);
  4746. #endif /* CONFIG_P2P */
  4747. wpa_debug_level = global->params.wpa_debug_level =
  4748. params->wpa_debug_level;
  4749. wpa_debug_show_keys = global->params.wpa_debug_show_keys =
  4750. params->wpa_debug_show_keys;
  4751. wpa_debug_timestamp = global->params.wpa_debug_timestamp =
  4752. params->wpa_debug_timestamp;
  4753. wpa_printf(MSG_DEBUG, "wpa_supplicant v" VERSION_STR);
  4754. if (eloop_init()) {
  4755. wpa_printf(MSG_ERROR, "Failed to initialize event loop");
  4756. wpa_supplicant_deinit(global);
  4757. return NULL;
  4758. }
  4759. random_init(params->entropy_file);
  4760. global->ctrl_iface = wpa_supplicant_global_ctrl_iface_init(global);
  4761. if (global->ctrl_iface == NULL) {
  4762. wpa_supplicant_deinit(global);
  4763. return NULL;
  4764. }
  4765. if (wpas_notify_supplicant_initialized(global)) {
  4766. wpa_supplicant_deinit(global);
  4767. return NULL;
  4768. }
  4769. for (i = 0; wpa_drivers[i]; i++)
  4770. global->drv_count++;
  4771. if (global->drv_count == 0) {
  4772. wpa_printf(MSG_ERROR, "No drivers enabled");
  4773. wpa_supplicant_deinit(global);
  4774. return NULL;
  4775. }
  4776. global->drv_priv = os_calloc(global->drv_count, sizeof(void *));
  4777. if (global->drv_priv == NULL) {
  4778. wpa_supplicant_deinit(global);
  4779. return NULL;
  4780. }
  4781. #ifdef CONFIG_WIFI_DISPLAY
  4782. if (wifi_display_init(global) < 0) {
  4783. wpa_printf(MSG_ERROR, "Failed to initialize Wi-Fi Display");
  4784. wpa_supplicant_deinit(global);
  4785. return NULL;
  4786. }
  4787. #endif /* CONFIG_WIFI_DISPLAY */
  4788. eloop_register_timeout(WPA_SUPPLICANT_CLEANUP_INTERVAL, 0,
  4789. wpas_periodic, global, NULL);
  4790. return global;
  4791. }
  4792. /**
  4793. * wpa_supplicant_run - Run the %wpa_supplicant main event loop
  4794. * @global: Pointer to global data from wpa_supplicant_init()
  4795. * Returns: 0 after successful event loop run, -1 on failure
  4796. *
  4797. * This function starts the main event loop and continues running as long as
  4798. * there are any remaining events. In most cases, this function is running as
  4799. * long as the %wpa_supplicant process in still in use.
  4800. */
  4801. int wpa_supplicant_run(struct wpa_global *global)
  4802. {
  4803. struct wpa_supplicant *wpa_s;
  4804. if (global->params.daemonize &&
  4805. (wpa_supplicant_daemon(global->params.pid_file) ||
  4806. eloop_sock_requeue()))
  4807. return -1;
  4808. #ifdef CONFIG_MATCH_IFACE
  4809. if (wpa_supplicant_match_existing(global))
  4810. return -1;
  4811. #endif
  4812. if (global->params.wait_for_monitor) {
  4813. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
  4814. if (wpa_s->ctrl_iface && !wpa_s->p2p_mgmt)
  4815. wpa_supplicant_ctrl_iface_wait(
  4816. wpa_s->ctrl_iface);
  4817. }
  4818. eloop_register_signal_terminate(wpa_supplicant_terminate, global);
  4819. eloop_register_signal_reconfig(wpa_supplicant_reconfig, global);
  4820. eloop_run();
  4821. return 0;
  4822. }
  4823. /**
  4824. * wpa_supplicant_deinit - Deinitialize %wpa_supplicant
  4825. * @global: Pointer to global data from wpa_supplicant_init()
  4826. *
  4827. * This function is called to deinitialize %wpa_supplicant and to free all
  4828. * allocated resources. Remaining network interfaces will also be removed.
  4829. */
  4830. void wpa_supplicant_deinit(struct wpa_global *global)
  4831. {
  4832. int i;
  4833. if (global == NULL)
  4834. return;
  4835. eloop_cancel_timeout(wpas_periodic, global, NULL);
  4836. #ifdef CONFIG_WIFI_DISPLAY
  4837. wifi_display_deinit(global);
  4838. #endif /* CONFIG_WIFI_DISPLAY */
  4839. while (global->ifaces)
  4840. wpa_supplicant_remove_iface(global, global->ifaces, 1);
  4841. if (global->ctrl_iface)
  4842. wpa_supplicant_global_ctrl_iface_deinit(global->ctrl_iface);
  4843. wpas_notify_supplicant_deinitialized(global);
  4844. eap_peer_unregister_methods();
  4845. #ifdef CONFIG_AP
  4846. eap_server_unregister_methods();
  4847. #endif /* CONFIG_AP */
  4848. for (i = 0; wpa_drivers[i] && global->drv_priv; i++) {
  4849. if (!global->drv_priv[i])
  4850. continue;
  4851. wpa_drivers[i]->global_deinit(global->drv_priv[i]);
  4852. }
  4853. os_free(global->drv_priv);
  4854. random_deinit();
  4855. eloop_destroy();
  4856. if (global->params.pid_file) {
  4857. os_daemonize_terminate(global->params.pid_file);
  4858. os_free(global->params.pid_file);
  4859. }
  4860. os_free(global->params.ctrl_interface);
  4861. os_free(global->params.ctrl_interface_group);
  4862. os_free(global->params.override_driver);
  4863. os_free(global->params.override_ctrl_interface);
  4864. #ifdef CONFIG_MATCH_IFACE
  4865. os_free(global->params.match_ifaces);
  4866. #endif /* CONFIG_MATCH_IFACE */
  4867. #ifdef CONFIG_P2P
  4868. os_free(global->params.conf_p2p_dev);
  4869. #endif /* CONFIG_P2P */
  4870. os_free(global->p2p_disallow_freq.range);
  4871. os_free(global->p2p_go_avoid_freq.range);
  4872. os_free(global->add_psk);
  4873. os_free(global);
  4874. wpa_debug_close_syslog();
  4875. wpa_debug_close_file();
  4876. wpa_debug_close_linux_tracing();
  4877. }
  4878. void wpa_supplicant_update_config(struct wpa_supplicant *wpa_s)
  4879. {
  4880. if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
  4881. wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
  4882. char country[3];
  4883. country[0] = wpa_s->conf->country[0];
  4884. country[1] = wpa_s->conf->country[1];
  4885. country[2] = '\0';
  4886. if (wpa_drv_set_country(wpa_s, country) < 0) {
  4887. wpa_printf(MSG_ERROR, "Failed to set country code "
  4888. "'%s'", country);
  4889. }
  4890. }
  4891. if (wpa_s->conf->changed_parameters & CFG_CHANGED_EXT_PW_BACKEND)
  4892. wpas_init_ext_pw(wpa_s);
  4893. if (wpa_s->conf->changed_parameters & CFG_CHANGED_SCHED_SCAN_PLANS)
  4894. wpas_sched_scan_plans_set(wpa_s, wpa_s->conf->sched_scan_plans);
  4895. #ifdef CONFIG_WPS
  4896. wpas_wps_update_config(wpa_s);
  4897. #endif /* CONFIG_WPS */
  4898. wpas_p2p_update_config(wpa_s);
  4899. wpa_s->conf->changed_parameters = 0;
  4900. }
  4901. void add_freq(int *freqs, int *num_freqs, int freq)
  4902. {
  4903. int i;
  4904. for (i = 0; i < *num_freqs; i++) {
  4905. if (freqs[i] == freq)
  4906. return;
  4907. }
  4908. freqs[*num_freqs] = freq;
  4909. (*num_freqs)++;
  4910. }
  4911. static int * get_bss_freqs_in_ess(struct wpa_supplicant *wpa_s)
  4912. {
  4913. struct wpa_bss *bss, *cbss;
  4914. const int max_freqs = 10;
  4915. int *freqs;
  4916. int num_freqs = 0;
  4917. freqs = os_calloc(max_freqs + 1, sizeof(int));
  4918. if (freqs == NULL)
  4919. return NULL;
  4920. cbss = wpa_s->current_bss;
  4921. dl_list_for_each(bss, &wpa_s->bss, struct wpa_bss, list) {
  4922. if (bss == cbss)
  4923. continue;
  4924. if (bss->ssid_len == cbss->ssid_len &&
  4925. os_memcmp(bss->ssid, cbss->ssid, bss->ssid_len) == 0 &&
  4926. wpa_blacklist_get(wpa_s, bss->bssid) == NULL) {
  4927. add_freq(freqs, &num_freqs, bss->freq);
  4928. if (num_freqs == max_freqs)
  4929. break;
  4930. }
  4931. }
  4932. if (num_freqs == 0) {
  4933. os_free(freqs);
  4934. freqs = NULL;
  4935. }
  4936. return freqs;
  4937. }
  4938. void wpas_connection_failed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  4939. {
  4940. int timeout;
  4941. int count;
  4942. int *freqs = NULL;
  4943. wpas_connect_work_done(wpa_s);
  4944. /*
  4945. * Remove possible authentication timeout since the connection failed.
  4946. */
  4947. eloop_cancel_timeout(wpa_supplicant_timeout, wpa_s, NULL);
  4948. /*
  4949. * There is no point in blacklisting the AP if this event is
  4950. * generated based on local request to disconnect.
  4951. */
  4952. if (wpa_s->own_disconnect_req) {
  4953. wpa_s->own_disconnect_req = 0;
  4954. wpa_dbg(wpa_s, MSG_DEBUG,
  4955. "Ignore connection failure due to local request to disconnect");
  4956. return;
  4957. }
  4958. if (wpa_s->disconnected) {
  4959. wpa_dbg(wpa_s, MSG_DEBUG, "Ignore connection failure "
  4960. "indication since interface has been put into "
  4961. "disconnected state");
  4962. return;
  4963. }
  4964. /*
  4965. * Add the failed BSSID into the blacklist and speed up next scan
  4966. * attempt if there could be other APs that could accept association.
  4967. * The current blacklist count indicates how many times we have tried
  4968. * connecting to this AP and multiple attempts mean that other APs are
  4969. * either not available or has already been tried, so that we can start
  4970. * increasing the delay here to avoid constant scanning.
  4971. */
  4972. count = wpa_blacklist_add(wpa_s, bssid);
  4973. if (count == 1 && wpa_s->current_bss) {
  4974. /*
  4975. * This BSS was not in the blacklist before. If there is
  4976. * another BSS available for the same ESS, we should try that
  4977. * next. Otherwise, we may as well try this one once more
  4978. * before allowing other, likely worse, ESSes to be considered.
  4979. */
  4980. freqs = get_bss_freqs_in_ess(wpa_s);
  4981. if (freqs) {
  4982. wpa_dbg(wpa_s, MSG_DEBUG, "Another BSS in this ESS "
  4983. "has been seen; try it next");
  4984. wpa_blacklist_add(wpa_s, bssid);
  4985. /*
  4986. * On the next scan, go through only the known channels
  4987. * used in this ESS based on previous scans to speed up
  4988. * common load balancing use case.
  4989. */
  4990. os_free(wpa_s->next_scan_freqs);
  4991. wpa_s->next_scan_freqs = freqs;
  4992. }
  4993. }
  4994. /*
  4995. * Add previous failure count in case the temporary blacklist was
  4996. * cleared due to no other BSSes being available.
  4997. */
  4998. count += wpa_s->extra_blacklist_count;
  4999. if (count > 3 && wpa_s->current_ssid) {
  5000. wpa_printf(MSG_DEBUG, "Continuous association failures - "
  5001. "consider temporary network disabling");
  5002. wpas_auth_failed(wpa_s, "CONN_FAILED");
  5003. }
  5004. switch (count) {
  5005. case 1:
  5006. timeout = 100;
  5007. break;
  5008. case 2:
  5009. timeout = 500;
  5010. break;
  5011. case 3:
  5012. timeout = 1000;
  5013. break;
  5014. case 4:
  5015. timeout = 5000;
  5016. break;
  5017. default:
  5018. timeout = 10000;
  5019. break;
  5020. }
  5021. wpa_dbg(wpa_s, MSG_DEBUG, "Blacklist count %d --> request scan in %d "
  5022. "ms", count, timeout);
  5023. /*
  5024. * TODO: if more than one possible AP is available in scan results,
  5025. * could try the other ones before requesting a new scan.
  5026. */
  5027. wpa_supplicant_req_scan(wpa_s, timeout / 1000,
  5028. 1000 * (timeout % 1000));
  5029. }
  5030. int wpas_driver_bss_selection(struct wpa_supplicant *wpa_s)
  5031. {
  5032. return wpa_s->conf->ap_scan == 2 ||
  5033. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_BSS_SELECTION);
  5034. }
  5035. #if defined(CONFIG_CTRL_IFACE) || defined(CONFIG_CTRL_IFACE_DBUS_NEW)
  5036. int wpa_supplicant_ctrl_iface_ctrl_rsp_handle(struct wpa_supplicant *wpa_s,
  5037. struct wpa_ssid *ssid,
  5038. const char *field,
  5039. const char *value)
  5040. {
  5041. #ifdef IEEE8021X_EAPOL
  5042. struct eap_peer_config *eap = &ssid->eap;
  5043. wpa_printf(MSG_DEBUG, "CTRL_IFACE: response handle field=%s", field);
  5044. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: response value",
  5045. (const u8 *) value, os_strlen(value));
  5046. switch (wpa_supplicant_ctrl_req_from_string(field)) {
  5047. case WPA_CTRL_REQ_EAP_IDENTITY:
  5048. os_free(eap->identity);
  5049. eap->identity = (u8 *) os_strdup(value);
  5050. eap->identity_len = os_strlen(value);
  5051. eap->pending_req_identity = 0;
  5052. if (ssid == wpa_s->current_ssid)
  5053. wpa_s->reassociate = 1;
  5054. break;
  5055. case WPA_CTRL_REQ_EAP_PASSWORD:
  5056. bin_clear_free(eap->password, eap->password_len);
  5057. eap->password = (u8 *) os_strdup(value);
  5058. eap->password_len = os_strlen(value);
  5059. eap->pending_req_password = 0;
  5060. if (ssid == wpa_s->current_ssid)
  5061. wpa_s->reassociate = 1;
  5062. break;
  5063. case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
  5064. bin_clear_free(eap->new_password, eap->new_password_len);
  5065. eap->new_password = (u8 *) os_strdup(value);
  5066. eap->new_password_len = os_strlen(value);
  5067. eap->pending_req_new_password = 0;
  5068. if (ssid == wpa_s->current_ssid)
  5069. wpa_s->reassociate = 1;
  5070. break;
  5071. case WPA_CTRL_REQ_EAP_PIN:
  5072. str_clear_free(eap->pin);
  5073. eap->pin = os_strdup(value);
  5074. eap->pending_req_pin = 0;
  5075. if (ssid == wpa_s->current_ssid)
  5076. wpa_s->reassociate = 1;
  5077. break;
  5078. case WPA_CTRL_REQ_EAP_OTP:
  5079. bin_clear_free(eap->otp, eap->otp_len);
  5080. eap->otp = (u8 *) os_strdup(value);
  5081. eap->otp_len = os_strlen(value);
  5082. os_free(eap->pending_req_otp);
  5083. eap->pending_req_otp = NULL;
  5084. eap->pending_req_otp_len = 0;
  5085. break;
  5086. case WPA_CTRL_REQ_EAP_PASSPHRASE:
  5087. str_clear_free(eap->private_key_passwd);
  5088. eap->private_key_passwd = os_strdup(value);
  5089. eap->pending_req_passphrase = 0;
  5090. if (ssid == wpa_s->current_ssid)
  5091. wpa_s->reassociate = 1;
  5092. break;
  5093. case WPA_CTRL_REQ_SIM:
  5094. str_clear_free(eap->external_sim_resp);
  5095. eap->external_sim_resp = os_strdup(value);
  5096. eap->pending_req_sim = 0;
  5097. break;
  5098. case WPA_CTRL_REQ_PSK_PASSPHRASE:
  5099. if (wpa_config_set(ssid, "psk", value, 0) < 0)
  5100. return -1;
  5101. ssid->mem_only_psk = 1;
  5102. if (ssid->passphrase)
  5103. wpa_config_update_psk(ssid);
  5104. if (wpa_s->wpa_state == WPA_SCANNING && !wpa_s->scanning)
  5105. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5106. break;
  5107. case WPA_CTRL_REQ_EXT_CERT_CHECK:
  5108. if (eap->pending_ext_cert_check != PENDING_CHECK)
  5109. return -1;
  5110. if (os_strcmp(value, "good") == 0)
  5111. eap->pending_ext_cert_check = EXT_CERT_CHECK_GOOD;
  5112. else if (os_strcmp(value, "bad") == 0)
  5113. eap->pending_ext_cert_check = EXT_CERT_CHECK_BAD;
  5114. else
  5115. return -1;
  5116. break;
  5117. default:
  5118. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown field '%s'", field);
  5119. return -1;
  5120. }
  5121. return 0;
  5122. #else /* IEEE8021X_EAPOL */
  5123. wpa_printf(MSG_DEBUG, "CTRL_IFACE: IEEE 802.1X not included");
  5124. return -1;
  5125. #endif /* IEEE8021X_EAPOL */
  5126. }
  5127. #endif /* CONFIG_CTRL_IFACE || CONFIG_CTRL_IFACE_DBUS_NEW */
  5128. int wpas_network_disabled(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  5129. {
  5130. int i;
  5131. unsigned int drv_enc;
  5132. if (wpa_s->p2p_mgmt)
  5133. return 1; /* no normal network profiles on p2p_mgmt interface */
  5134. if (ssid == NULL)
  5135. return 1;
  5136. if (ssid->disabled)
  5137. return 1;
  5138. if (wpa_s->drv_capa_known)
  5139. drv_enc = wpa_s->drv_enc;
  5140. else
  5141. drv_enc = (unsigned int) -1;
  5142. for (i = 0; i < NUM_WEP_KEYS; i++) {
  5143. size_t len = ssid->wep_key_len[i];
  5144. if (len == 0)
  5145. continue;
  5146. if (len == 5 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP40))
  5147. continue;
  5148. if (len == 13 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP104))
  5149. continue;
  5150. if (len == 16 && (drv_enc & WPA_DRIVER_CAPA_ENC_WEP128))
  5151. continue;
  5152. return 1; /* invalid WEP key */
  5153. }
  5154. if (wpa_key_mgmt_wpa_psk(ssid->key_mgmt) && !ssid->psk_set &&
  5155. (!ssid->passphrase || ssid->ssid_len != 0) && !ssid->ext_psk &&
  5156. !ssid->mem_only_psk)
  5157. return 1;
  5158. return 0;
  5159. }
  5160. int wpas_get_ssid_pmf(struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid)
  5161. {
  5162. #ifdef CONFIG_IEEE80211W
  5163. if (ssid == NULL || ssid->ieee80211w == MGMT_FRAME_PROTECTION_DEFAULT) {
  5164. if (wpa_s->conf->pmf == MGMT_FRAME_PROTECTION_OPTIONAL &&
  5165. !(wpa_s->drv_enc & WPA_DRIVER_CAPA_ENC_BIP)) {
  5166. /*
  5167. * Driver does not support BIP -- ignore pmf=1 default
  5168. * since the connection with PMF would fail and the
  5169. * configuration does not require PMF to be enabled.
  5170. */
  5171. return NO_MGMT_FRAME_PROTECTION;
  5172. }
  5173. if (ssid &&
  5174. (ssid->key_mgmt &
  5175. ~(WPA_KEY_MGMT_NONE | WPA_KEY_MGMT_WPS |
  5176. WPA_KEY_MGMT_IEEE8021X_NO_WPA)) == 0) {
  5177. /*
  5178. * Do not use the default PMF value for non-RSN networks
  5179. * since PMF is available only with RSN and pmf=2
  5180. * configuration would otherwise prevent connections to
  5181. * all open networks.
  5182. */
  5183. return NO_MGMT_FRAME_PROTECTION;
  5184. }
  5185. return wpa_s->conf->pmf;
  5186. }
  5187. return ssid->ieee80211w;
  5188. #else /* CONFIG_IEEE80211W */
  5189. return NO_MGMT_FRAME_PROTECTION;
  5190. #endif /* CONFIG_IEEE80211W */
  5191. }
  5192. int wpas_is_p2p_prioritized(struct wpa_supplicant *wpa_s)
  5193. {
  5194. if (wpa_s->global->conc_pref == WPA_CONC_PREF_P2P)
  5195. return 1;
  5196. if (wpa_s->global->conc_pref == WPA_CONC_PREF_STA)
  5197. return 0;
  5198. return -1;
  5199. }
  5200. void wpas_auth_failed(struct wpa_supplicant *wpa_s, char *reason)
  5201. {
  5202. struct wpa_ssid *ssid = wpa_s->current_ssid;
  5203. int dur;
  5204. struct os_reltime now;
  5205. if (ssid == NULL) {
  5206. wpa_printf(MSG_DEBUG, "Authentication failure but no known "
  5207. "SSID block");
  5208. return;
  5209. }
  5210. if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
  5211. return;
  5212. ssid->auth_failures++;
  5213. #ifdef CONFIG_P2P
  5214. if (ssid->p2p_group &&
  5215. (wpa_s->p2p_in_provisioning || wpa_s->show_group_started)) {
  5216. /*
  5217. * Skip the wait time since there is a short timeout on the
  5218. * connection to a P2P group.
  5219. */
  5220. return;
  5221. }
  5222. #endif /* CONFIG_P2P */
  5223. if (ssid->auth_failures > 50)
  5224. dur = 300;
  5225. else if (ssid->auth_failures > 10)
  5226. dur = 120;
  5227. else if (ssid->auth_failures > 5)
  5228. dur = 90;
  5229. else if (ssid->auth_failures > 3)
  5230. dur = 60;
  5231. else if (ssid->auth_failures > 2)
  5232. dur = 30;
  5233. else if (ssid->auth_failures > 1)
  5234. dur = 20;
  5235. else
  5236. dur = 10;
  5237. if (ssid->auth_failures > 1 &&
  5238. wpa_key_mgmt_wpa_ieee8021x(ssid->key_mgmt))
  5239. dur += os_random() % (ssid->auth_failures * 10);
  5240. os_get_reltime(&now);
  5241. if (now.sec + dur <= ssid->disabled_until.sec)
  5242. return;
  5243. ssid->disabled_until.sec = now.sec + dur;
  5244. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_TEMP_DISABLED
  5245. "id=%d ssid=\"%s\" auth_failures=%u duration=%d reason=%s",
  5246. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
  5247. ssid->auth_failures, dur, reason);
  5248. }
  5249. void wpas_clear_temp_disabled(struct wpa_supplicant *wpa_s,
  5250. struct wpa_ssid *ssid, int clear_failures)
  5251. {
  5252. if (ssid == NULL)
  5253. return;
  5254. if (ssid->disabled_until.sec) {
  5255. wpa_msg(wpa_s, MSG_INFO, WPA_EVENT_REENABLED
  5256. "id=%d ssid=\"%s\"",
  5257. ssid->id, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  5258. }
  5259. ssid->disabled_until.sec = 0;
  5260. ssid->disabled_until.usec = 0;
  5261. if (clear_failures)
  5262. ssid->auth_failures = 0;
  5263. }
  5264. int disallowed_bssid(struct wpa_supplicant *wpa_s, const u8 *bssid)
  5265. {
  5266. size_t i;
  5267. if (wpa_s->disallow_aps_bssid == NULL)
  5268. return 0;
  5269. for (i = 0; i < wpa_s->disallow_aps_bssid_count; i++) {
  5270. if (os_memcmp(wpa_s->disallow_aps_bssid + i * ETH_ALEN,
  5271. bssid, ETH_ALEN) == 0)
  5272. return 1;
  5273. }
  5274. return 0;
  5275. }
  5276. int disallowed_ssid(struct wpa_supplicant *wpa_s, const u8 *ssid,
  5277. size_t ssid_len)
  5278. {
  5279. size_t i;
  5280. if (wpa_s->disallow_aps_ssid == NULL || ssid == NULL)
  5281. return 0;
  5282. for (i = 0; i < wpa_s->disallow_aps_ssid_count; i++) {
  5283. struct wpa_ssid_value *s = &wpa_s->disallow_aps_ssid[i];
  5284. if (ssid_len == s->ssid_len &&
  5285. os_memcmp(ssid, s->ssid, ssid_len) == 0)
  5286. return 1;
  5287. }
  5288. return 0;
  5289. }
  5290. /**
  5291. * wpas_request_connection - Request a new connection
  5292. * @wpa_s: Pointer to the network interface
  5293. *
  5294. * This function is used to request a new connection to be found. It will mark
  5295. * the interface to allow reassociation and request a new scan to find a
  5296. * suitable network to connect to.
  5297. */
  5298. void wpas_request_connection(struct wpa_supplicant *wpa_s)
  5299. {
  5300. wpa_s->normal_scans = 0;
  5301. wpa_s->scan_req = NORMAL_SCAN_REQ;
  5302. wpa_supplicant_reinit_autoscan(wpa_s);
  5303. wpa_s->extra_blacklist_count = 0;
  5304. wpa_s->disconnected = 0;
  5305. wpa_s->reassociate = 1;
  5306. if (wpa_supplicant_fast_associate(wpa_s) != 1)
  5307. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5308. else
  5309. wpa_s->reattach = 0;
  5310. }
  5311. /**
  5312. * wpas_request_disconnection - Request disconnection
  5313. * @wpa_s: Pointer to the network interface
  5314. *
  5315. * This function is used to request disconnection from the currently connected
  5316. * network. This will stop any ongoing scans and initiate deauthentication.
  5317. */
  5318. void wpas_request_disconnection(struct wpa_supplicant *wpa_s)
  5319. {
  5320. #ifdef CONFIG_SME
  5321. wpa_s->sme.prev_bssid_set = 0;
  5322. #endif /* CONFIG_SME */
  5323. wpa_s->reassociate = 0;
  5324. wpa_s->disconnected = 1;
  5325. wpa_supplicant_cancel_sched_scan(wpa_s);
  5326. wpa_supplicant_cancel_scan(wpa_s);
  5327. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  5328. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  5329. }
  5330. void dump_freq_data(struct wpa_supplicant *wpa_s, const char *title,
  5331. struct wpa_used_freq_data *freqs_data,
  5332. unsigned int len)
  5333. {
  5334. unsigned int i;
  5335. wpa_dbg(wpa_s, MSG_DEBUG, "Shared frequencies (len=%u): %s",
  5336. len, title);
  5337. for (i = 0; i < len; i++) {
  5338. struct wpa_used_freq_data *cur = &freqs_data[i];
  5339. wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d, flags=0x%X",
  5340. i, cur->freq, cur->flags);
  5341. }
  5342. }
  5343. /*
  5344. * Find the operating frequencies of any of the virtual interfaces that
  5345. * are using the same radio as the current interface, and in addition, get
  5346. * information about the interface types that are using the frequency.
  5347. */
  5348. int get_shared_radio_freqs_data(struct wpa_supplicant *wpa_s,
  5349. struct wpa_used_freq_data *freqs_data,
  5350. unsigned int len)
  5351. {
  5352. struct wpa_supplicant *ifs;
  5353. u8 bssid[ETH_ALEN];
  5354. int freq;
  5355. unsigned int idx = 0, i;
  5356. wpa_dbg(wpa_s, MSG_DEBUG,
  5357. "Determining shared radio frequencies (max len %u)", len);
  5358. os_memset(freqs_data, 0, sizeof(struct wpa_used_freq_data) * len);
  5359. dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
  5360. radio_list) {
  5361. if (idx == len)
  5362. break;
  5363. if (ifs->current_ssid == NULL || ifs->assoc_freq == 0)
  5364. continue;
  5365. if (ifs->current_ssid->mode == WPAS_MODE_AP ||
  5366. ifs->current_ssid->mode == WPAS_MODE_P2P_GO ||
  5367. ifs->current_ssid->mode == WPAS_MODE_MESH)
  5368. freq = ifs->current_ssid->frequency;
  5369. else if (wpa_drv_get_bssid(ifs, bssid) == 0)
  5370. freq = ifs->assoc_freq;
  5371. else
  5372. continue;
  5373. /* Hold only distinct freqs */
  5374. for (i = 0; i < idx; i++)
  5375. if (freqs_data[i].freq == freq)
  5376. break;
  5377. if (i == idx)
  5378. freqs_data[idx++].freq = freq;
  5379. if (ifs->current_ssid->mode == WPAS_MODE_INFRA) {
  5380. freqs_data[i].flags |= ifs->current_ssid->p2p_group ?
  5381. WPA_FREQ_USED_BY_P2P_CLIENT :
  5382. WPA_FREQ_USED_BY_INFRA_STATION;
  5383. }
  5384. }
  5385. dump_freq_data(wpa_s, "completed iteration", freqs_data, idx);
  5386. return idx;
  5387. }
  5388. /*
  5389. * Find the operating frequencies of any of the virtual interfaces that
  5390. * are using the same radio as the current interface.
  5391. */
  5392. int get_shared_radio_freqs(struct wpa_supplicant *wpa_s,
  5393. int *freq_array, unsigned int len)
  5394. {
  5395. struct wpa_used_freq_data *freqs_data;
  5396. int num, i;
  5397. os_memset(freq_array, 0, sizeof(int) * len);
  5398. freqs_data = os_calloc(len, sizeof(struct wpa_used_freq_data));
  5399. if (!freqs_data)
  5400. return -1;
  5401. num = get_shared_radio_freqs_data(wpa_s, freqs_data, len);
  5402. for (i = 0; i < num; i++)
  5403. freq_array[i] = freqs_data[i].freq;
  5404. os_free(freqs_data);
  5405. return num;
  5406. }
  5407. struct wpa_supplicant *
  5408. wpas_vendor_elem(struct wpa_supplicant *wpa_s, enum wpa_vendor_elem_frame frame)
  5409. {
  5410. switch (frame) {
  5411. #ifdef CONFIG_P2P
  5412. case VENDOR_ELEM_PROBE_REQ_P2P:
  5413. case VENDOR_ELEM_PROBE_RESP_P2P:
  5414. case VENDOR_ELEM_PROBE_RESP_P2P_GO:
  5415. case VENDOR_ELEM_BEACON_P2P_GO:
  5416. case VENDOR_ELEM_P2P_PD_REQ:
  5417. case VENDOR_ELEM_P2P_PD_RESP:
  5418. case VENDOR_ELEM_P2P_GO_NEG_REQ:
  5419. case VENDOR_ELEM_P2P_GO_NEG_RESP:
  5420. case VENDOR_ELEM_P2P_GO_NEG_CONF:
  5421. case VENDOR_ELEM_P2P_INV_REQ:
  5422. case VENDOR_ELEM_P2P_INV_RESP:
  5423. case VENDOR_ELEM_P2P_ASSOC_REQ:
  5424. case VENDOR_ELEM_P2P_ASSOC_RESP:
  5425. return wpa_s->p2pdev;
  5426. #endif /* CONFIG_P2P */
  5427. default:
  5428. return wpa_s;
  5429. }
  5430. }
  5431. void wpas_vendor_elem_update(struct wpa_supplicant *wpa_s)
  5432. {
  5433. unsigned int i;
  5434. char buf[30];
  5435. wpa_printf(MSG_DEBUG, "Update vendor elements");
  5436. for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
  5437. if (wpa_s->vendor_elem[i]) {
  5438. int res;
  5439. res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
  5440. if (!os_snprintf_error(sizeof(buf), res)) {
  5441. wpa_hexdump_buf(MSG_DEBUG, buf,
  5442. wpa_s->vendor_elem[i]);
  5443. }
  5444. }
  5445. }
  5446. #ifdef CONFIG_P2P
  5447. if (wpa_s->parent == wpa_s &&
  5448. wpa_s->global->p2p &&
  5449. !wpa_s->global->p2p_disabled)
  5450. p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
  5451. #endif /* CONFIG_P2P */
  5452. }
  5453. int wpas_vendor_elem_remove(struct wpa_supplicant *wpa_s, int frame,
  5454. const u8 *elem, size_t len)
  5455. {
  5456. u8 *ie, *end;
  5457. ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
  5458. end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
  5459. for (; ie + 1 < end; ie += 2 + ie[1]) {
  5460. if (ie + len > end)
  5461. break;
  5462. if (os_memcmp(ie, elem, len) != 0)
  5463. continue;
  5464. if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
  5465. wpabuf_free(wpa_s->vendor_elem[frame]);
  5466. wpa_s->vendor_elem[frame] = NULL;
  5467. } else {
  5468. os_memmove(ie, ie + len, end - (ie + len));
  5469. wpa_s->vendor_elem[frame]->used -= len;
  5470. }
  5471. wpas_vendor_elem_update(wpa_s);
  5472. return 0;
  5473. }
  5474. return -1;
  5475. }
  5476. struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
  5477. u16 num_modes, enum hostapd_hw_mode mode)
  5478. {
  5479. u16 i;
  5480. for (i = 0; i < num_modes; i++) {
  5481. if (modes[i].mode == mode)
  5482. return &modes[i];
  5483. }
  5484. return NULL;
  5485. }
  5486. static struct
  5487. wpa_bss_tmp_disallowed * wpas_get_disallowed_bss(struct wpa_supplicant *wpa_s,
  5488. const u8 *bssid)
  5489. {
  5490. struct wpa_bss_tmp_disallowed *bss;
  5491. dl_list_for_each(bss, &wpa_s->bss_tmp_disallowed,
  5492. struct wpa_bss_tmp_disallowed, list) {
  5493. if (os_memcmp(bssid, bss->bssid, ETH_ALEN) == 0)
  5494. return bss;
  5495. }
  5496. return NULL;
  5497. }
  5498. void wpa_bss_tmp_disallow(struct wpa_supplicant *wpa_s, const u8 *bssid,
  5499. unsigned int sec)
  5500. {
  5501. struct wpa_bss_tmp_disallowed *bss;
  5502. struct os_reltime until;
  5503. os_get_reltime(&until);
  5504. until.sec += sec;
  5505. bss = wpas_get_disallowed_bss(wpa_s, bssid);
  5506. if (bss) {
  5507. bss->disallowed_until = until;
  5508. return;
  5509. }
  5510. bss = os_malloc(sizeof(*bss));
  5511. if (!bss) {
  5512. wpa_printf(MSG_DEBUG,
  5513. "Failed to allocate memory for temp disallow BSS");
  5514. return;
  5515. }
  5516. bss->disallowed_until = until;
  5517. os_memcpy(bss->bssid, bssid, ETH_ALEN);
  5518. dl_list_add(&wpa_s->bss_tmp_disallowed, &bss->list);
  5519. }
  5520. int wpa_is_bss_tmp_disallowed(struct wpa_supplicant *wpa_s, const u8 *bssid)
  5521. {
  5522. struct wpa_bss_tmp_disallowed *bss = NULL, *tmp, *prev;
  5523. struct os_reltime now, age;
  5524. os_get_reltime(&now);
  5525. dl_list_for_each_safe(tmp, prev, &wpa_s->bss_tmp_disallowed,
  5526. struct wpa_bss_tmp_disallowed, list) {
  5527. if (!os_reltime_before(&now, &tmp->disallowed_until)) {
  5528. /* This BSS is not disallowed anymore */
  5529. dl_list_del(&tmp->list);
  5530. os_free(tmp);
  5531. continue;
  5532. }
  5533. if (os_memcmp(bssid, tmp->bssid, ETH_ALEN) == 0) {
  5534. bss = tmp;
  5535. break;
  5536. }
  5537. }
  5538. if (!bss)
  5539. return 0;
  5540. os_reltime_sub(&bss->disallowed_until, &now, &age);
  5541. wpa_printf(MSG_DEBUG,
  5542. "BSS " MACSTR " disabled for %ld.%0ld seconds",
  5543. MAC2STR(bss->bssid), age.sec, age.usec);
  5544. return 1;
  5545. }