wpa_supplicant.c 182 KB

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