wpa_supplicant.c 191 KB

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