wpa_supplicant.c 190 KB

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