ctrl_iface.c 261 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2015, 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. #include "utils/includes.h"
  9. #ifdef CONFIG_TESTING_OPTIONS
  10. #include <net/ethernet.h>
  11. #include <netinet/ip.h>
  12. #endif /* CONFIG_TESTING_OPTIONS */
  13. #include "utils/common.h"
  14. #include "utils/eloop.h"
  15. #include "utils/uuid.h"
  16. #include "utils/module_tests.h"
  17. #include "common/version.h"
  18. #include "common/ieee802_11_defs.h"
  19. #include "common/ieee802_11_common.h"
  20. #include "common/wpa_ctrl.h"
  21. #include "crypto/tls.h"
  22. #include "ap/hostapd.h"
  23. #include "eap_peer/eap.h"
  24. #include "eapol_supp/eapol_supp_sm.h"
  25. #include "rsn_supp/wpa.h"
  26. #include "rsn_supp/preauth.h"
  27. #include "rsn_supp/pmksa_cache.h"
  28. #include "l2_packet/l2_packet.h"
  29. #include "wps/wps.h"
  30. #include "fst/fst.h"
  31. #include "fst/fst_ctrl_iface.h"
  32. #include "config.h"
  33. #include "wpa_supplicant_i.h"
  34. #include "driver_i.h"
  35. #include "wps_supplicant.h"
  36. #include "ibss_rsn.h"
  37. #include "ap.h"
  38. #include "p2p_supplicant.h"
  39. #include "p2p/p2p.h"
  40. #include "hs20_supplicant.h"
  41. #include "wifi_display.h"
  42. #include "notify.h"
  43. #include "bss.h"
  44. #include "scan.h"
  45. #include "ctrl_iface.h"
  46. #include "interworking.h"
  47. #include "blacklist.h"
  48. #include "autoscan.h"
  49. #include "wnm_sta.h"
  50. #include "offchannel.h"
  51. #include "drivers/driver.h"
  52. #include "mesh.h"
  53. #include "dpp_supplicant.h"
  54. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  55. char *buf, int len);
  56. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  57. const char *input,
  58. char *buf, int len);
  59. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
  60. char *val);
  61. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  62. {
  63. char *pos;
  64. u8 addr[ETH_ALEN], *filter = NULL, *n;
  65. size_t count = 0;
  66. pos = val;
  67. while (pos) {
  68. if (*pos == '\0')
  69. break;
  70. if (hwaddr_aton(pos, addr)) {
  71. os_free(filter);
  72. return -1;
  73. }
  74. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  75. if (n == NULL) {
  76. os_free(filter);
  77. return -1;
  78. }
  79. filter = n;
  80. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  81. count++;
  82. pos = os_strchr(pos, ' ');
  83. if (pos)
  84. pos++;
  85. }
  86. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  87. os_free(wpa_s->bssid_filter);
  88. wpa_s->bssid_filter = filter;
  89. wpa_s->bssid_filter_count = count;
  90. return 0;
  91. }
  92. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  93. {
  94. char *pos;
  95. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  96. struct wpa_ssid_value *ssid = NULL, *ns;
  97. size_t count = 0, ssid_count = 0;
  98. struct wpa_ssid *c;
  99. /*
  100. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  101. * SSID_SPEC ::= ssid <SSID_HEX>
  102. * BSSID_SPEC ::= bssid <BSSID_HEX>
  103. */
  104. pos = val;
  105. while (pos) {
  106. if (*pos == '\0')
  107. break;
  108. if (os_strncmp(pos, "bssid ", 6) == 0) {
  109. int res;
  110. pos += 6;
  111. res = hwaddr_aton2(pos, addr);
  112. if (res < 0) {
  113. os_free(ssid);
  114. os_free(bssid);
  115. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  116. "BSSID value '%s'", pos);
  117. return -1;
  118. }
  119. pos += res;
  120. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  121. if (n == NULL) {
  122. os_free(ssid);
  123. os_free(bssid);
  124. return -1;
  125. }
  126. bssid = n;
  127. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  128. count++;
  129. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  130. char *end;
  131. pos += 5;
  132. end = pos;
  133. while (*end) {
  134. if (*end == '\0' || *end == ' ')
  135. break;
  136. end++;
  137. }
  138. ns = os_realloc_array(ssid, ssid_count + 1,
  139. sizeof(struct wpa_ssid_value));
  140. if (ns == NULL) {
  141. os_free(ssid);
  142. os_free(bssid);
  143. return -1;
  144. }
  145. ssid = ns;
  146. if ((end - pos) & 0x01 ||
  147. end - pos > 2 * SSID_MAX_LEN ||
  148. hexstr2bin(pos, ssid[ssid_count].ssid,
  149. (end - pos) / 2) < 0) {
  150. os_free(ssid);
  151. os_free(bssid);
  152. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  153. "SSID value '%s'", pos);
  154. return -1;
  155. }
  156. ssid[ssid_count].ssid_len = (end - pos) / 2;
  157. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  158. ssid[ssid_count].ssid,
  159. ssid[ssid_count].ssid_len);
  160. ssid_count++;
  161. pos = end;
  162. } else {
  163. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  164. "'%s'", pos);
  165. os_free(ssid);
  166. os_free(bssid);
  167. return -1;
  168. }
  169. pos = os_strchr(pos, ' ');
  170. if (pos)
  171. pos++;
  172. }
  173. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  174. os_free(wpa_s->disallow_aps_bssid);
  175. wpa_s->disallow_aps_bssid = bssid;
  176. wpa_s->disallow_aps_bssid_count = count;
  177. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  178. os_free(wpa_s->disallow_aps_ssid);
  179. wpa_s->disallow_aps_ssid = ssid;
  180. wpa_s->disallow_aps_ssid_count = ssid_count;
  181. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  182. return 0;
  183. c = wpa_s->current_ssid;
  184. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  185. return 0;
  186. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  187. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  188. return 0;
  189. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  190. "because current AP was marked disallowed");
  191. #ifdef CONFIG_SME
  192. wpa_s->sme.prev_bssid_set = 0;
  193. #endif /* CONFIG_SME */
  194. wpa_s->reassociate = 1;
  195. wpa_s->own_disconnect_req = 1;
  196. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  197. wpa_supplicant_req_scan(wpa_s, 0, 0);
  198. return 0;
  199. }
  200. #ifndef CONFIG_NO_CONFIG_BLOBS
  201. static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
  202. {
  203. char *name = pos;
  204. struct wpa_config_blob *blob;
  205. size_t len;
  206. pos = os_strchr(pos, ' ');
  207. if (pos == NULL)
  208. return -1;
  209. *pos++ = '\0';
  210. len = os_strlen(pos);
  211. if (len & 1)
  212. return -1;
  213. wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
  214. blob = os_zalloc(sizeof(*blob));
  215. if (blob == NULL)
  216. return -1;
  217. blob->name = os_strdup(name);
  218. blob->data = os_malloc(len / 2);
  219. if (blob->name == NULL || blob->data == NULL) {
  220. wpa_config_free_blob(blob);
  221. return -1;
  222. }
  223. if (hexstr2bin(pos, blob->data, len / 2) < 0) {
  224. wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
  225. wpa_config_free_blob(blob);
  226. return -1;
  227. }
  228. blob->len = len / 2;
  229. wpa_config_set_blob(wpa_s->conf, blob);
  230. return 0;
  231. }
  232. #endif /* CONFIG_NO_CONFIG_BLOBS */
  233. static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
  234. {
  235. char *params;
  236. char *pos;
  237. int *freqs = NULL;
  238. int ret;
  239. if (atoi(cmd)) {
  240. params = os_strchr(cmd, ' ');
  241. os_free(wpa_s->manual_sched_scan_freqs);
  242. if (params) {
  243. params++;
  244. pos = os_strstr(params, "freq=");
  245. if (pos)
  246. freqs = freq_range_to_channel_list(wpa_s,
  247. pos + 5);
  248. }
  249. wpa_s->manual_sched_scan_freqs = freqs;
  250. ret = wpas_start_pno(wpa_s);
  251. } else {
  252. ret = wpas_stop_pno(wpa_s);
  253. }
  254. return ret;
  255. }
  256. static int wpas_ctrl_set_band(struct wpa_supplicant *wpa_s, char *band)
  257. {
  258. union wpa_event_data event;
  259. if (os_strcmp(band, "AUTO") == 0)
  260. wpa_s->setband = WPA_SETBAND_AUTO;
  261. else if (os_strcmp(band, "5G") == 0)
  262. wpa_s->setband = WPA_SETBAND_5G;
  263. else if (os_strcmp(band, "2G") == 0)
  264. wpa_s->setband = WPA_SETBAND_2G;
  265. else
  266. return -1;
  267. if (wpa_drv_setband(wpa_s, wpa_s->setband) == 0) {
  268. os_memset(&event, 0, sizeof(event));
  269. event.channel_list_changed.initiator = REGDOM_SET_BY_USER;
  270. event.channel_list_changed.type = REGDOM_TYPE_UNKNOWN;
  271. wpa_supplicant_event(wpa_s, EVENT_CHANNEL_LIST_CHANGED, &event);
  272. }
  273. return 0;
  274. }
  275. static int wpas_ctrl_iface_set_lci(struct wpa_supplicant *wpa_s,
  276. const char *cmd)
  277. {
  278. struct wpabuf *lci;
  279. if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
  280. wpabuf_free(wpa_s->lci);
  281. wpa_s->lci = NULL;
  282. return 0;
  283. }
  284. lci = wpabuf_parse_bin(cmd);
  285. if (!lci)
  286. return -1;
  287. if (os_get_reltime(&wpa_s->lci_time)) {
  288. wpabuf_free(lci);
  289. return -1;
  290. }
  291. wpabuf_free(wpa_s->lci);
  292. wpa_s->lci = lci;
  293. return 0;
  294. }
  295. static int
  296. wpas_ctrl_set_relative_rssi(struct wpa_supplicant *wpa_s, const char *cmd)
  297. {
  298. int relative_rssi;
  299. if (os_strcmp(cmd, "disable") == 0) {
  300. wpa_s->srp.relative_rssi_set = 0;
  301. return 0;
  302. }
  303. relative_rssi = atoi(cmd);
  304. if (relative_rssi < 0 || relative_rssi > 100)
  305. return -1;
  306. wpa_s->srp.relative_rssi = relative_rssi;
  307. wpa_s->srp.relative_rssi_set = 1;
  308. return 0;
  309. }
  310. static int wpas_ctrl_set_relative_band_adjust(struct wpa_supplicant *wpa_s,
  311. const char *cmd)
  312. {
  313. char *pos;
  314. int adjust_rssi;
  315. /* <band>:adjust_value */
  316. pos = os_strchr(cmd, ':');
  317. if (!pos)
  318. return -1;
  319. pos++;
  320. adjust_rssi = atoi(pos);
  321. if (adjust_rssi < -100 || adjust_rssi > 100)
  322. return -1;
  323. if (os_strncmp(cmd, "2G", 2) == 0)
  324. wpa_s->srp.relative_adjust_band = WPA_SETBAND_2G;
  325. else if (os_strncmp(cmd, "5G", 2) == 0)
  326. wpa_s->srp.relative_adjust_band = WPA_SETBAND_5G;
  327. else
  328. return -1;
  329. wpa_s->srp.relative_adjust_rssi = adjust_rssi;
  330. return 0;
  331. }
  332. static int wpas_ctrl_iface_set_ric_ies(struct wpa_supplicant *wpa_s,
  333. const char *cmd)
  334. {
  335. struct wpabuf *ric_ies;
  336. if (*cmd == '\0' || os_strcmp(cmd, "\"\"") == 0) {
  337. wpabuf_free(wpa_s->ric_ies);
  338. wpa_s->ric_ies = NULL;
  339. return 0;
  340. }
  341. ric_ies = wpabuf_parse_bin(cmd);
  342. if (!ric_ies)
  343. return -1;
  344. wpabuf_free(wpa_s->ric_ies);
  345. wpa_s->ric_ies = ric_ies;
  346. return 0;
  347. }
  348. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  349. char *cmd)
  350. {
  351. char *value;
  352. int ret = 0;
  353. value = os_strchr(cmd, ' ');
  354. if (value == NULL)
  355. return -1;
  356. *value++ = '\0';
  357. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  358. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  359. eapol_sm_configure(wpa_s->eapol,
  360. atoi(value), -1, -1, -1);
  361. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  362. eapol_sm_configure(wpa_s->eapol,
  363. -1, atoi(value), -1, -1);
  364. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  365. eapol_sm_configure(wpa_s->eapol,
  366. -1, -1, atoi(value), -1);
  367. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  368. eapol_sm_configure(wpa_s->eapol,
  369. -1, -1, -1, atoi(value));
  370. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  371. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  372. atoi(value))) {
  373. ret = -1;
  374. } else {
  375. value[-1] = '=';
  376. wpa_config_process_global(wpa_s->conf, cmd, -1);
  377. }
  378. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  379. 0) {
  380. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  381. atoi(value))) {
  382. ret = -1;
  383. } else {
  384. value[-1] = '=';
  385. wpa_config_process_global(wpa_s->conf, cmd, -1);
  386. }
  387. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  388. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT,
  389. atoi(value))) {
  390. ret = -1;
  391. } else {
  392. value[-1] = '=';
  393. wpa_config_process_global(wpa_s->conf, cmd, -1);
  394. }
  395. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  396. wpa_s->wps_fragment_size = atoi(value);
  397. #ifdef CONFIG_WPS_TESTING
  398. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  399. long int val;
  400. val = strtol(value, NULL, 0);
  401. if (val < 0 || val > 0xff) {
  402. ret = -1;
  403. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  404. "wps_version_number %ld", val);
  405. } else {
  406. wps_version_number = val;
  407. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  408. "version %u.%u",
  409. (wps_version_number & 0xf0) >> 4,
  410. wps_version_number & 0x0f);
  411. }
  412. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  413. wps_testing_dummy_cred = atoi(value);
  414. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  415. wps_testing_dummy_cred);
  416. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  417. wps_corrupt_pkhash = atoi(value);
  418. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  419. wps_corrupt_pkhash);
  420. } else if (os_strcasecmp(cmd, "wps_force_auth_types") == 0) {
  421. if (value[0] == '\0') {
  422. wps_force_auth_types_in_use = 0;
  423. } else {
  424. wps_force_auth_types = strtol(value, NULL, 0);
  425. wps_force_auth_types_in_use = 1;
  426. }
  427. } else if (os_strcasecmp(cmd, "wps_force_encr_types") == 0) {
  428. if (value[0] == '\0') {
  429. wps_force_encr_types_in_use = 0;
  430. } else {
  431. wps_force_encr_types = strtol(value, NULL, 0);
  432. wps_force_encr_types_in_use = 1;
  433. }
  434. #endif /* CONFIG_WPS_TESTING */
  435. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  436. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  437. ret = -1;
  438. #ifdef CONFIG_TDLS
  439. #ifdef CONFIG_TDLS_TESTING
  440. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  441. tdls_testing = strtol(value, NULL, 0);
  442. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  443. #endif /* CONFIG_TDLS_TESTING */
  444. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  445. int disabled = atoi(value);
  446. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  447. if (disabled) {
  448. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  449. ret = -1;
  450. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  451. ret = -1;
  452. wpa_tdls_enable(wpa_s->wpa, !disabled);
  453. #endif /* CONFIG_TDLS */
  454. } else if (os_strcasecmp(cmd, "pno") == 0) {
  455. ret = wpas_ctrl_pno(wpa_s, value);
  456. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  457. int disabled = atoi(value);
  458. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  459. ret = -1;
  460. else if (disabled)
  461. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  462. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  463. if (os_strcmp(value, "disable") == 0)
  464. wpa_s->set_sta_uapsd = 0;
  465. else {
  466. int be, bk, vi, vo;
  467. char *pos;
  468. /* format: BE,BK,VI,VO;max SP Length */
  469. be = atoi(value);
  470. pos = os_strchr(value, ',');
  471. if (pos == NULL)
  472. return -1;
  473. pos++;
  474. bk = atoi(pos);
  475. pos = os_strchr(pos, ',');
  476. if (pos == NULL)
  477. return -1;
  478. pos++;
  479. vi = atoi(pos);
  480. pos = os_strchr(pos, ',');
  481. if (pos == NULL)
  482. return -1;
  483. pos++;
  484. vo = atoi(pos);
  485. /* ignore max SP Length for now */
  486. wpa_s->set_sta_uapsd = 1;
  487. wpa_s->sta_uapsd = 0;
  488. if (be)
  489. wpa_s->sta_uapsd |= BIT(0);
  490. if (bk)
  491. wpa_s->sta_uapsd |= BIT(1);
  492. if (vi)
  493. wpa_s->sta_uapsd |= BIT(2);
  494. if (vo)
  495. wpa_s->sta_uapsd |= BIT(3);
  496. }
  497. } else if (os_strcasecmp(cmd, "ps") == 0) {
  498. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  499. #ifdef CONFIG_WIFI_DISPLAY
  500. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  501. int enabled = !!atoi(value);
  502. if (enabled && !wpa_s->global->p2p)
  503. ret = -1;
  504. else
  505. wifi_display_enable(wpa_s->global, enabled);
  506. #endif /* CONFIG_WIFI_DISPLAY */
  507. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  508. ret = set_bssid_filter(wpa_s, value);
  509. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  510. ret = set_disallow_aps(wpa_s, value);
  511. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  512. wpa_s->no_keep_alive = !!atoi(value);
  513. #ifdef CONFIG_TESTING_OPTIONS
  514. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  515. wpa_s->ext_mgmt_frame_handling = !!atoi(value);
  516. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  517. wpa_s->ext_eapol_frame_io = !!atoi(value);
  518. #ifdef CONFIG_AP
  519. if (wpa_s->ap_iface) {
  520. wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
  521. wpa_s->ext_eapol_frame_io;
  522. }
  523. #endif /* CONFIG_AP */
  524. } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
  525. wpa_s->extra_roc_dur = atoi(value);
  526. } else if (os_strcasecmp(cmd, "test_failure") == 0) {
  527. wpa_s->test_failure = atoi(value);
  528. } else if (os_strcasecmp(cmd, "p2p_go_csa_on_inv") == 0) {
  529. wpa_s->p2p_go_csa_on_inv = !!atoi(value);
  530. } else if (os_strcasecmp(cmd, "ignore_auth_resp") == 0) {
  531. wpa_s->ignore_auth_resp = !!atoi(value);
  532. } else if (os_strcasecmp(cmd, "ignore_assoc_disallow") == 0) {
  533. wpa_s->ignore_assoc_disallow = !!atoi(value);
  534. wpa_drv_ignore_assoc_disallow(wpa_s,
  535. wpa_s->ignore_assoc_disallow);
  536. } else if (os_strcasecmp(cmd, "reject_btm_req_reason") == 0) {
  537. wpa_s->reject_btm_req_reason = atoi(value);
  538. } else if (os_strcasecmp(cmd, "get_pref_freq_list_override") == 0) {
  539. os_free(wpa_s->get_pref_freq_list_override);
  540. if (!value[0])
  541. wpa_s->get_pref_freq_list_override = NULL;
  542. else
  543. wpa_s->get_pref_freq_list_override = os_strdup(value);
  544. #ifdef CONFIG_DPP
  545. } else if (os_strcasecmp(cmd, "dpp_config_obj_override") == 0) {
  546. os_free(wpa_s->dpp_config_obj_override);
  547. wpa_s->dpp_config_obj_override = os_strdup(value);
  548. } else if (os_strcasecmp(cmd, "dpp_discovery_override") == 0) {
  549. os_free(wpa_s->dpp_discovery_override);
  550. wpa_s->dpp_discovery_override = os_strdup(value);
  551. } else if (os_strcasecmp(cmd, "dpp_groups_override") == 0) {
  552. os_free(wpa_s->dpp_groups_override);
  553. wpa_s->dpp_groups_override = os_strdup(value);
  554. } else if (os_strcasecmp(cmd, "dpp_devices_override") == 0) {
  555. os_free(wpa_s->dpp_devices_override);
  556. wpa_s->dpp_devices_override = os_strdup(value);
  557. } else if (os_strcasecmp(cmd,
  558. "dpp_ignore_netaccesskey_mismatch") == 0) {
  559. wpa_s->dpp_ignore_netaccesskey_mismatch = atoi(value);
  560. #endif /* CONFIG_DPP */
  561. #endif /* CONFIG_TESTING_OPTIONS */
  562. #ifndef CONFIG_NO_CONFIG_BLOBS
  563. } else if (os_strcmp(cmd, "blob") == 0) {
  564. ret = wpas_ctrl_set_blob(wpa_s, value);
  565. #endif /* CONFIG_NO_CONFIG_BLOBS */
  566. } else if (os_strcasecmp(cmd, "setband") == 0) {
  567. ret = wpas_ctrl_set_band(wpa_s, value);
  568. #ifdef CONFIG_MBO
  569. } else if (os_strcasecmp(cmd, "non_pref_chan") == 0) {
  570. ret = wpas_mbo_update_non_pref_chan(wpa_s, value);
  571. if (ret == 0) {
  572. value[-1] = '=';
  573. wpa_config_process_global(wpa_s->conf, cmd, -1);
  574. }
  575. } else if (os_strcasecmp(cmd, "mbo_cell_capa") == 0) {
  576. wpas_mbo_update_cell_capa(wpa_s, atoi(value));
  577. #endif /* CONFIG_MBO */
  578. } else if (os_strcasecmp(cmd, "lci") == 0) {
  579. ret = wpas_ctrl_iface_set_lci(wpa_s, value);
  580. } else if (os_strcasecmp(cmd, "tdls_trigger_control") == 0) {
  581. ret = wpa_drv_set_tdls_mode(wpa_s, atoi(value));
  582. } else if (os_strcasecmp(cmd, "relative_rssi") == 0) {
  583. ret = wpas_ctrl_set_relative_rssi(wpa_s, value);
  584. } else if (os_strcasecmp(cmd, "relative_band_adjust") == 0) {
  585. ret = wpas_ctrl_set_relative_band_adjust(wpa_s, value);
  586. } else if (os_strcasecmp(cmd, "ric_ies") == 0) {
  587. ret = wpas_ctrl_iface_set_ric_ies(wpa_s, value);
  588. } else if (os_strcasecmp(cmd, "roaming") == 0) {
  589. ret = wpa_drv_roaming(wpa_s, atoi(value), NULL);
  590. } else {
  591. value[-1] = '=';
  592. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  593. if (ret == 0)
  594. wpa_supplicant_update_config(wpa_s);
  595. }
  596. return ret;
  597. }
  598. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  599. char *cmd, char *buf, size_t buflen)
  600. {
  601. int res = -1;
  602. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  603. if (os_strcmp(cmd, "version") == 0) {
  604. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  605. } else if (os_strcasecmp(cmd, "country") == 0) {
  606. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  607. res = os_snprintf(buf, buflen, "%c%c",
  608. wpa_s->conf->country[0],
  609. wpa_s->conf->country[1]);
  610. #ifdef CONFIG_WIFI_DISPLAY
  611. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  612. int enabled;
  613. if (wpa_s->global->p2p == NULL ||
  614. wpa_s->global->p2p_disabled)
  615. enabled = 0;
  616. else
  617. enabled = wpa_s->global->wifi_display;
  618. res = os_snprintf(buf, buflen, "%d", enabled);
  619. #endif /* CONFIG_WIFI_DISPLAY */
  620. #ifdef CONFIG_TESTING_GET_GTK
  621. } else if (os_strcmp(cmd, "gtk") == 0) {
  622. if (wpa_s->last_gtk_len == 0)
  623. return -1;
  624. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  625. wpa_s->last_gtk_len);
  626. return res;
  627. #endif /* CONFIG_TESTING_GET_GTK */
  628. } else if (os_strcmp(cmd, "tls_library") == 0) {
  629. res = tls_get_library_version(buf, buflen);
  630. } else {
  631. res = wpa_config_get_value(cmd, wpa_s->conf, buf, buflen);
  632. }
  633. if (os_snprintf_error(buflen, res))
  634. return -1;
  635. return res;
  636. }
  637. #ifdef IEEE8021X_EAPOL
  638. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  639. char *addr)
  640. {
  641. u8 bssid[ETH_ALEN];
  642. struct wpa_ssid *ssid = wpa_s->current_ssid;
  643. if (hwaddr_aton(addr, bssid)) {
  644. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  645. "'%s'", addr);
  646. return -1;
  647. }
  648. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  649. rsn_preauth_deinit(wpa_s->wpa);
  650. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  651. return -1;
  652. return 0;
  653. }
  654. #endif /* IEEE8021X_EAPOL */
  655. #ifdef CONFIG_PEERKEY
  656. /* MLME-STKSTART.request(peer) */
  657. static int wpa_supplicant_ctrl_iface_stkstart(
  658. struct wpa_supplicant *wpa_s, char *addr)
  659. {
  660. u8 peer[ETH_ALEN];
  661. if (hwaddr_aton(addr, peer)) {
  662. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  663. "address '%s'", addr);
  664. return -1;
  665. }
  666. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  667. MAC2STR(peer));
  668. return wpa_sm_stkstart(wpa_s->wpa, peer);
  669. }
  670. #endif /* CONFIG_PEERKEY */
  671. #ifdef CONFIG_TDLS
  672. static int wpa_supplicant_ctrl_iface_tdls_discover(
  673. struct wpa_supplicant *wpa_s, char *addr)
  674. {
  675. u8 peer[ETH_ALEN];
  676. int ret;
  677. if (hwaddr_aton(addr, peer)) {
  678. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  679. "address '%s'", addr);
  680. return -1;
  681. }
  682. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  683. MAC2STR(peer));
  684. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  685. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  686. else
  687. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  688. return ret;
  689. }
  690. static int wpa_supplicant_ctrl_iface_tdls_setup(
  691. struct wpa_supplicant *wpa_s, char *addr)
  692. {
  693. u8 peer[ETH_ALEN];
  694. int ret;
  695. if (hwaddr_aton(addr, peer)) {
  696. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  697. "address '%s'", addr);
  698. return -1;
  699. }
  700. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  701. MAC2STR(peer));
  702. if ((wpa_s->conf->tdls_external_control) &&
  703. wpa_tdls_is_external_setup(wpa_s->wpa))
  704. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  705. wpa_tdls_remove(wpa_s->wpa, peer);
  706. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  707. ret = wpa_tdls_start(wpa_s->wpa, peer);
  708. else
  709. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  710. return ret;
  711. }
  712. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  713. struct wpa_supplicant *wpa_s, char *addr)
  714. {
  715. u8 peer[ETH_ALEN];
  716. int ret;
  717. if (os_strcmp(addr, "*") == 0) {
  718. /* remove everyone */
  719. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
  720. wpa_tdls_teardown_peers(wpa_s->wpa);
  721. return 0;
  722. }
  723. if (hwaddr_aton(addr, peer)) {
  724. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  725. "address '%s'", addr);
  726. return -1;
  727. }
  728. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  729. MAC2STR(peer));
  730. if ((wpa_s->conf->tdls_external_control) &&
  731. wpa_tdls_is_external_setup(wpa_s->wpa))
  732. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  733. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  734. ret = wpa_tdls_teardown_link(
  735. wpa_s->wpa, peer,
  736. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  737. else
  738. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  739. return ret;
  740. }
  741. static int ctrl_iface_get_capability_tdls(
  742. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  743. {
  744. int ret;
  745. ret = os_snprintf(buf, buflen, "%s\n",
  746. wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
  747. (wpa_s->drv_flags &
  748. WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
  749. "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
  750. if (os_snprintf_error(buflen, ret))
  751. return -1;
  752. return ret;
  753. }
  754. static int wpa_supplicant_ctrl_iface_tdls_chan_switch(
  755. struct wpa_supplicant *wpa_s, char *cmd)
  756. {
  757. u8 peer[ETH_ALEN];
  758. struct hostapd_freq_params freq_params;
  759. u8 oper_class;
  760. char *pos, *end;
  761. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  762. wpa_printf(MSG_INFO,
  763. "tdls_chanswitch: Only supported with external setup");
  764. return -1;
  765. }
  766. os_memset(&freq_params, 0, sizeof(freq_params));
  767. pos = os_strchr(cmd, ' ');
  768. if (pos == NULL)
  769. return -1;
  770. *pos++ = '\0';
  771. oper_class = strtol(pos, &end, 10);
  772. if (pos == end) {
  773. wpa_printf(MSG_INFO,
  774. "tdls_chanswitch: Invalid op class provided");
  775. return -1;
  776. }
  777. pos = end;
  778. freq_params.freq = atoi(pos);
  779. if (freq_params.freq == 0) {
  780. wpa_printf(MSG_INFO, "tdls_chanswitch: Invalid freq provided");
  781. return -1;
  782. }
  783. #define SET_FREQ_SETTING(str) \
  784. do { \
  785. const char *pos2 = os_strstr(pos, " " #str "="); \
  786. if (pos2) { \
  787. pos2 += sizeof(" " #str "=") - 1; \
  788. freq_params.str = atoi(pos2); \
  789. } \
  790. } while (0)
  791. SET_FREQ_SETTING(center_freq1);
  792. SET_FREQ_SETTING(center_freq2);
  793. SET_FREQ_SETTING(bandwidth);
  794. SET_FREQ_SETTING(sec_channel_offset);
  795. #undef SET_FREQ_SETTING
  796. freq_params.ht_enabled = !!os_strstr(pos, " ht");
  797. freq_params.vht_enabled = !!os_strstr(pos, " vht");
  798. if (hwaddr_aton(cmd, peer)) {
  799. wpa_printf(MSG_DEBUG,
  800. "CTRL_IFACE TDLS_CHAN_SWITCH: Invalid address '%s'",
  801. cmd);
  802. return -1;
  803. }
  804. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CHAN_SWITCH " MACSTR
  805. " OP CLASS %d FREQ %d CENTER1 %d CENTER2 %d BW %d SEC_OFFSET %d%s%s",
  806. MAC2STR(peer), oper_class, freq_params.freq,
  807. freq_params.center_freq1, freq_params.center_freq2,
  808. freq_params.bandwidth, freq_params.sec_channel_offset,
  809. freq_params.ht_enabled ? " HT" : "",
  810. freq_params.vht_enabled ? " VHT" : "");
  811. return wpa_tdls_enable_chan_switch(wpa_s->wpa, peer, oper_class,
  812. &freq_params);
  813. }
  814. static int wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(
  815. struct wpa_supplicant *wpa_s, char *cmd)
  816. {
  817. u8 peer[ETH_ALEN];
  818. if (!wpa_tdls_is_external_setup(wpa_s->wpa)) {
  819. wpa_printf(MSG_INFO,
  820. "tdls_chanswitch: Only supported with external setup");
  821. return -1;
  822. }
  823. if (hwaddr_aton(cmd, peer)) {
  824. wpa_printf(MSG_DEBUG,
  825. "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH: Invalid address '%s'",
  826. cmd);
  827. return -1;
  828. }
  829. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_CANCEL_CHAN_SWITCH " MACSTR,
  830. MAC2STR(peer));
  831. return wpa_tdls_disable_chan_switch(wpa_s->wpa, peer);
  832. }
  833. static int wpa_supplicant_ctrl_iface_tdls_link_status(
  834. struct wpa_supplicant *wpa_s, const char *addr,
  835. char *buf, size_t buflen)
  836. {
  837. u8 peer[ETH_ALEN];
  838. const char *tdls_status;
  839. int ret;
  840. if (hwaddr_aton(addr, peer)) {
  841. wpa_printf(MSG_DEBUG,
  842. "CTRL_IFACE TDLS_LINK_STATUS: Invalid address '%s'",
  843. addr);
  844. return -1;
  845. }
  846. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS " MACSTR,
  847. MAC2STR(peer));
  848. tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
  849. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_LINK_STATUS: %s", tdls_status);
  850. ret = os_snprintf(buf, buflen, "TDLS link status: %s\n", tdls_status);
  851. if (os_snprintf_error(buflen, ret))
  852. return -1;
  853. return ret;
  854. }
  855. #endif /* CONFIG_TDLS */
  856. static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
  857. {
  858. char *token, *context = NULL;
  859. struct wmm_ac_ts_setup_params params = {
  860. .tsid = 0xff,
  861. .direction = 0xff,
  862. };
  863. while ((token = str_token(cmd, " ", &context))) {
  864. if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
  865. sscanf(token, "up=%i", &params.user_priority) == 1 ||
  866. sscanf(token, "nominal_msdu_size=%i",
  867. &params.nominal_msdu_size) == 1 ||
  868. sscanf(token, "mean_data_rate=%i",
  869. &params.mean_data_rate) == 1 ||
  870. sscanf(token, "min_phy_rate=%i",
  871. &params.minimum_phy_rate) == 1 ||
  872. sscanf(token, "sba=%i",
  873. &params.surplus_bandwidth_allowance) == 1)
  874. continue;
  875. if (os_strcasecmp(token, "downlink") == 0) {
  876. params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
  877. } else if (os_strcasecmp(token, "uplink") == 0) {
  878. params.direction = WMM_TSPEC_DIRECTION_UPLINK;
  879. } else if (os_strcasecmp(token, "bidi") == 0) {
  880. params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
  881. } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
  882. params.fixed_nominal_msdu = 1;
  883. } else {
  884. wpa_printf(MSG_DEBUG,
  885. "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
  886. token);
  887. return -1;
  888. }
  889. }
  890. return wpas_wmm_ac_addts(wpa_s, &params);
  891. }
  892. static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
  893. {
  894. u8 tsid = atoi(cmd);
  895. return wpas_wmm_ac_delts(wpa_s, tsid);
  896. }
  897. #ifdef CONFIG_IEEE80211R
  898. static int wpa_supplicant_ctrl_iface_ft_ds(
  899. struct wpa_supplicant *wpa_s, char *addr)
  900. {
  901. u8 target_ap[ETH_ALEN];
  902. struct wpa_bss *bss;
  903. const u8 *mdie;
  904. if (hwaddr_aton(addr, target_ap)) {
  905. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  906. "address '%s'", addr);
  907. return -1;
  908. }
  909. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  910. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  911. if (bss)
  912. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  913. else
  914. mdie = NULL;
  915. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  916. }
  917. #endif /* CONFIG_IEEE80211R */
  918. #ifdef CONFIG_WPS
  919. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  920. char *cmd)
  921. {
  922. u8 bssid[ETH_ALEN], *_bssid = bssid;
  923. #ifdef CONFIG_P2P
  924. u8 p2p_dev_addr[ETH_ALEN];
  925. #endif /* CONFIG_P2P */
  926. #ifdef CONFIG_AP
  927. u8 *_p2p_dev_addr = NULL;
  928. #endif /* CONFIG_AP */
  929. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  930. _bssid = NULL;
  931. #ifdef CONFIG_P2P
  932. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  933. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  934. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  935. "P2P Device Address '%s'",
  936. cmd + 13);
  937. return -1;
  938. }
  939. _p2p_dev_addr = p2p_dev_addr;
  940. #endif /* CONFIG_P2P */
  941. } else if (hwaddr_aton(cmd, bssid)) {
  942. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  943. cmd);
  944. return -1;
  945. }
  946. #ifdef CONFIG_AP
  947. if (wpa_s->ap_iface)
  948. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  949. #endif /* CONFIG_AP */
  950. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  951. }
  952. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  953. char *cmd, char *buf,
  954. size_t buflen)
  955. {
  956. u8 bssid[ETH_ALEN], *_bssid = bssid;
  957. char *pin;
  958. int ret;
  959. pin = os_strchr(cmd, ' ');
  960. if (pin)
  961. *pin++ = '\0';
  962. if (os_strcmp(cmd, "any") == 0)
  963. _bssid = NULL;
  964. else if (os_strcmp(cmd, "get") == 0) {
  965. if (wps_generate_pin((unsigned int *) &ret) < 0)
  966. return -1;
  967. goto done;
  968. } else if (hwaddr_aton(cmd, bssid)) {
  969. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  970. cmd);
  971. return -1;
  972. }
  973. #ifdef CONFIG_AP
  974. if (wpa_s->ap_iface) {
  975. int timeout = 0;
  976. char *pos;
  977. if (pin) {
  978. pos = os_strchr(pin, ' ');
  979. if (pos) {
  980. *pos++ = '\0';
  981. timeout = atoi(pos);
  982. }
  983. }
  984. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  985. buf, buflen, timeout);
  986. }
  987. #endif /* CONFIG_AP */
  988. if (pin) {
  989. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  990. DEV_PW_DEFAULT);
  991. if (ret < 0)
  992. return -1;
  993. ret = os_snprintf(buf, buflen, "%s", pin);
  994. if (os_snprintf_error(buflen, ret))
  995. return -1;
  996. return ret;
  997. }
  998. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  999. if (ret < 0)
  1000. return -1;
  1001. done:
  1002. /* Return the generated PIN */
  1003. ret = os_snprintf(buf, buflen, "%08d", ret);
  1004. if (os_snprintf_error(buflen, ret))
  1005. return -1;
  1006. return ret;
  1007. }
  1008. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  1009. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1010. {
  1011. char pin[9];
  1012. size_t len;
  1013. char *pos;
  1014. int ret;
  1015. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  1016. (u8 *) cmd, os_strlen(cmd));
  1017. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  1018. if (*pos < '0' || *pos > '9')
  1019. continue;
  1020. pin[len++] = *pos;
  1021. if (len == 9) {
  1022. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  1023. return -1;
  1024. }
  1025. }
  1026. if (len != 4 && len != 8) {
  1027. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  1028. return -1;
  1029. }
  1030. pin[len] = '\0';
  1031. if (len == 8) {
  1032. unsigned int pin_val;
  1033. pin_val = atoi(pin);
  1034. if (!wps_pin_valid(pin_val)) {
  1035. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  1036. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  1037. if (os_snprintf_error(buflen, ret))
  1038. return -1;
  1039. return ret;
  1040. }
  1041. }
  1042. ret = os_snprintf(buf, buflen, "%s", pin);
  1043. if (os_snprintf_error(buflen, ret))
  1044. return -1;
  1045. return ret;
  1046. }
  1047. #ifdef CONFIG_WPS_NFC
  1048. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  1049. char *cmd)
  1050. {
  1051. u8 bssid[ETH_ALEN], *_bssid = bssid;
  1052. if (cmd == NULL || cmd[0] == '\0')
  1053. _bssid = NULL;
  1054. else if (hwaddr_aton(cmd, bssid))
  1055. return -1;
  1056. return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
  1057. 0, 0);
  1058. }
  1059. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  1060. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1061. {
  1062. int ndef;
  1063. struct wpabuf *buf;
  1064. int res;
  1065. char *pos;
  1066. pos = os_strchr(cmd, ' ');
  1067. if (pos)
  1068. *pos++ = '\0';
  1069. if (os_strcmp(cmd, "WPS") == 0)
  1070. ndef = 0;
  1071. else if (os_strcmp(cmd, "NDEF") == 0)
  1072. ndef = 1;
  1073. else
  1074. return -1;
  1075. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  1076. if (buf == NULL)
  1077. return -1;
  1078. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1079. wpabuf_len(buf));
  1080. reply[res++] = '\n';
  1081. reply[res] = '\0';
  1082. wpabuf_free(buf);
  1083. return res;
  1084. }
  1085. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  1086. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1087. {
  1088. int ndef;
  1089. struct wpabuf *buf;
  1090. int res;
  1091. if (os_strcmp(cmd, "WPS") == 0)
  1092. ndef = 0;
  1093. else if (os_strcmp(cmd, "NDEF") == 0)
  1094. ndef = 1;
  1095. else
  1096. return -1;
  1097. buf = wpas_wps_nfc_token(wpa_s, ndef);
  1098. if (buf == NULL)
  1099. return -1;
  1100. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1101. wpabuf_len(buf));
  1102. reply[res++] = '\n';
  1103. reply[res] = '\0';
  1104. wpabuf_free(buf);
  1105. return res;
  1106. }
  1107. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  1108. struct wpa_supplicant *wpa_s, char *pos)
  1109. {
  1110. size_t len;
  1111. struct wpabuf *buf;
  1112. int ret;
  1113. char *freq;
  1114. int forced_freq = 0;
  1115. freq = strstr(pos, " freq=");
  1116. if (freq) {
  1117. *freq = '\0';
  1118. freq += 6;
  1119. forced_freq = atoi(freq);
  1120. }
  1121. len = os_strlen(pos);
  1122. if (len & 0x01)
  1123. return -1;
  1124. len /= 2;
  1125. buf = wpabuf_alloc(len);
  1126. if (buf == NULL)
  1127. return -1;
  1128. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  1129. wpabuf_free(buf);
  1130. return -1;
  1131. }
  1132. ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
  1133. wpabuf_free(buf);
  1134. return ret;
  1135. }
  1136. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  1137. char *reply, size_t max_len,
  1138. int ndef)
  1139. {
  1140. struct wpabuf *buf;
  1141. int res;
  1142. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  1143. if (buf == NULL)
  1144. return -1;
  1145. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1146. wpabuf_len(buf));
  1147. reply[res++] = '\n';
  1148. reply[res] = '\0';
  1149. wpabuf_free(buf);
  1150. return res;
  1151. }
  1152. #ifdef CONFIG_P2P
  1153. static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
  1154. char *reply, size_t max_len,
  1155. int ndef)
  1156. {
  1157. struct wpabuf *buf;
  1158. int res;
  1159. buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
  1160. if (buf == NULL) {
  1161. wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
  1162. return -1;
  1163. }
  1164. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1165. wpabuf_len(buf));
  1166. reply[res++] = '\n';
  1167. reply[res] = '\0';
  1168. wpabuf_free(buf);
  1169. return res;
  1170. }
  1171. #endif /* CONFIG_P2P */
  1172. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  1173. char *cmd, char *reply,
  1174. size_t max_len)
  1175. {
  1176. char *pos;
  1177. int ndef;
  1178. pos = os_strchr(cmd, ' ');
  1179. if (pos == NULL)
  1180. return -1;
  1181. *pos++ = '\0';
  1182. if (os_strcmp(cmd, "WPS") == 0)
  1183. ndef = 0;
  1184. else if (os_strcmp(cmd, "NDEF") == 0)
  1185. ndef = 1;
  1186. else
  1187. return -1;
  1188. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1189. if (!ndef)
  1190. return -1;
  1191. return wpas_ctrl_nfc_get_handover_req_wps(
  1192. wpa_s, reply, max_len, ndef);
  1193. }
  1194. #ifdef CONFIG_P2P
  1195. if (os_strcmp(pos, "P2P-CR") == 0) {
  1196. return wpas_ctrl_nfc_get_handover_req_p2p(
  1197. wpa_s, reply, max_len, ndef);
  1198. }
  1199. #endif /* CONFIG_P2P */
  1200. return -1;
  1201. }
  1202. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  1203. char *reply, size_t max_len,
  1204. int ndef, int cr, char *uuid)
  1205. {
  1206. struct wpabuf *buf;
  1207. int res;
  1208. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  1209. if (buf == NULL)
  1210. return -1;
  1211. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1212. wpabuf_len(buf));
  1213. reply[res++] = '\n';
  1214. reply[res] = '\0';
  1215. wpabuf_free(buf);
  1216. return res;
  1217. }
  1218. #ifdef CONFIG_P2P
  1219. static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
  1220. char *reply, size_t max_len,
  1221. int ndef, int tag)
  1222. {
  1223. struct wpabuf *buf;
  1224. int res;
  1225. buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
  1226. if (buf == NULL)
  1227. return -1;
  1228. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1229. wpabuf_len(buf));
  1230. reply[res++] = '\n';
  1231. reply[res] = '\0';
  1232. wpabuf_free(buf);
  1233. return res;
  1234. }
  1235. #endif /* CONFIG_P2P */
  1236. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  1237. char *cmd, char *reply,
  1238. size_t max_len)
  1239. {
  1240. char *pos, *pos2;
  1241. int ndef;
  1242. pos = os_strchr(cmd, ' ');
  1243. if (pos == NULL)
  1244. return -1;
  1245. *pos++ = '\0';
  1246. if (os_strcmp(cmd, "WPS") == 0)
  1247. ndef = 0;
  1248. else if (os_strcmp(cmd, "NDEF") == 0)
  1249. ndef = 1;
  1250. else
  1251. return -1;
  1252. pos2 = os_strchr(pos, ' ');
  1253. if (pos2)
  1254. *pos2++ = '\0';
  1255. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  1256. if (!ndef)
  1257. return -1;
  1258. return wpas_ctrl_nfc_get_handover_sel_wps(
  1259. wpa_s, reply, max_len, ndef,
  1260. os_strcmp(pos, "WPS-CR") == 0, pos2);
  1261. }
  1262. #ifdef CONFIG_P2P
  1263. if (os_strcmp(pos, "P2P-CR") == 0) {
  1264. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1265. wpa_s, reply, max_len, ndef, 0);
  1266. }
  1267. if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
  1268. return wpas_ctrl_nfc_get_handover_sel_p2p(
  1269. wpa_s, reply, max_len, ndef, 1);
  1270. }
  1271. #endif /* CONFIG_P2P */
  1272. return -1;
  1273. }
  1274. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  1275. char *cmd)
  1276. {
  1277. size_t len;
  1278. struct wpabuf *req, *sel;
  1279. int ret;
  1280. char *pos, *role, *type, *pos2;
  1281. #ifdef CONFIG_P2P
  1282. char *freq;
  1283. int forced_freq = 0;
  1284. freq = strstr(cmd, " freq=");
  1285. if (freq) {
  1286. *freq = '\0';
  1287. freq += 6;
  1288. forced_freq = atoi(freq);
  1289. }
  1290. #endif /* CONFIG_P2P */
  1291. role = cmd;
  1292. pos = os_strchr(role, ' ');
  1293. if (pos == NULL) {
  1294. wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
  1295. return -1;
  1296. }
  1297. *pos++ = '\0';
  1298. type = pos;
  1299. pos = os_strchr(type, ' ');
  1300. if (pos == NULL) {
  1301. wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
  1302. return -1;
  1303. }
  1304. *pos++ = '\0';
  1305. pos2 = os_strchr(pos, ' ');
  1306. if (pos2 == NULL) {
  1307. wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
  1308. return -1;
  1309. }
  1310. *pos2++ = '\0';
  1311. len = os_strlen(pos);
  1312. if (len & 0x01) {
  1313. wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
  1314. return -1;
  1315. }
  1316. len /= 2;
  1317. req = wpabuf_alloc(len);
  1318. if (req == NULL) {
  1319. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
  1320. return -1;
  1321. }
  1322. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  1323. wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
  1324. wpabuf_free(req);
  1325. return -1;
  1326. }
  1327. len = os_strlen(pos2);
  1328. if (len & 0x01) {
  1329. wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
  1330. wpabuf_free(req);
  1331. return -1;
  1332. }
  1333. len /= 2;
  1334. sel = wpabuf_alloc(len);
  1335. if (sel == NULL) {
  1336. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
  1337. wpabuf_free(req);
  1338. return -1;
  1339. }
  1340. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  1341. wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
  1342. wpabuf_free(req);
  1343. wpabuf_free(sel);
  1344. return -1;
  1345. }
  1346. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
  1347. role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
  1348. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  1349. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  1350. #ifdef CONFIG_AP
  1351. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
  1352. {
  1353. ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
  1354. if (ret < 0)
  1355. ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
  1356. #endif /* CONFIG_AP */
  1357. #ifdef CONFIG_P2P
  1358. } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
  1359. {
  1360. ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
  1361. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
  1362. {
  1363. ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
  1364. forced_freq);
  1365. #endif /* CONFIG_P2P */
  1366. } else {
  1367. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  1368. "reported: role=%s type=%s", role, type);
  1369. ret = -1;
  1370. }
  1371. wpabuf_free(req);
  1372. wpabuf_free(sel);
  1373. if (ret)
  1374. wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
  1375. return ret;
  1376. }
  1377. #endif /* CONFIG_WPS_NFC */
  1378. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  1379. char *cmd)
  1380. {
  1381. u8 bssid[ETH_ALEN];
  1382. char *pin;
  1383. char *new_ssid;
  1384. char *new_auth;
  1385. char *new_encr;
  1386. char *new_key;
  1387. struct wps_new_ap_settings ap;
  1388. pin = os_strchr(cmd, ' ');
  1389. if (pin == NULL)
  1390. return -1;
  1391. *pin++ = '\0';
  1392. if (hwaddr_aton(cmd, bssid)) {
  1393. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  1394. cmd);
  1395. return -1;
  1396. }
  1397. new_ssid = os_strchr(pin, ' ');
  1398. if (new_ssid == NULL)
  1399. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1400. *new_ssid++ = '\0';
  1401. new_auth = os_strchr(new_ssid, ' ');
  1402. if (new_auth == NULL)
  1403. return -1;
  1404. *new_auth++ = '\0';
  1405. new_encr = os_strchr(new_auth, ' ');
  1406. if (new_encr == NULL)
  1407. return -1;
  1408. *new_encr++ = '\0';
  1409. new_key = os_strchr(new_encr, ' ');
  1410. if (new_key == NULL)
  1411. return -1;
  1412. *new_key++ = '\0';
  1413. os_memset(&ap, 0, sizeof(ap));
  1414. ap.ssid_hex = new_ssid;
  1415. ap.auth = new_auth;
  1416. ap.encr = new_encr;
  1417. ap.key_hex = new_key;
  1418. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  1419. }
  1420. #ifdef CONFIG_AP
  1421. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  1422. char *cmd, char *buf,
  1423. size_t buflen)
  1424. {
  1425. int timeout = 300;
  1426. char *pos;
  1427. const char *pin_txt;
  1428. if (!wpa_s->ap_iface)
  1429. return -1;
  1430. pos = os_strchr(cmd, ' ');
  1431. if (pos)
  1432. *pos++ = '\0';
  1433. if (os_strcmp(cmd, "disable") == 0) {
  1434. wpas_wps_ap_pin_disable(wpa_s);
  1435. return os_snprintf(buf, buflen, "OK\n");
  1436. }
  1437. if (os_strcmp(cmd, "random") == 0) {
  1438. if (pos)
  1439. timeout = atoi(pos);
  1440. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1441. if (pin_txt == NULL)
  1442. return -1;
  1443. return os_snprintf(buf, buflen, "%s", pin_txt);
  1444. }
  1445. if (os_strcmp(cmd, "get") == 0) {
  1446. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1447. if (pin_txt == NULL)
  1448. return -1;
  1449. return os_snprintf(buf, buflen, "%s", pin_txt);
  1450. }
  1451. if (os_strcmp(cmd, "set") == 0) {
  1452. char *pin;
  1453. if (pos == NULL)
  1454. return -1;
  1455. pin = pos;
  1456. pos = os_strchr(pos, ' ');
  1457. if (pos) {
  1458. *pos++ = '\0';
  1459. timeout = atoi(pos);
  1460. }
  1461. if (os_strlen(pin) > buflen)
  1462. return -1;
  1463. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1464. return -1;
  1465. return os_snprintf(buf, buflen, "%s", pin);
  1466. }
  1467. return -1;
  1468. }
  1469. #endif /* CONFIG_AP */
  1470. #ifdef CONFIG_WPS_ER
  1471. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1472. char *cmd)
  1473. {
  1474. char *uuid = cmd, *pin, *pos;
  1475. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1476. pin = os_strchr(uuid, ' ');
  1477. if (pin == NULL)
  1478. return -1;
  1479. *pin++ = '\0';
  1480. pos = os_strchr(pin, ' ');
  1481. if (pos) {
  1482. *pos++ = '\0';
  1483. if (hwaddr_aton(pos, addr_buf) == 0)
  1484. addr = addr_buf;
  1485. }
  1486. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1487. }
  1488. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1489. char *cmd)
  1490. {
  1491. char *uuid = cmd, *pin;
  1492. pin = os_strchr(uuid, ' ');
  1493. if (pin == NULL)
  1494. return -1;
  1495. *pin++ = '\0';
  1496. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1497. }
  1498. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1499. struct wpa_supplicant *wpa_s, char *cmd)
  1500. {
  1501. char *uuid = cmd, *id;
  1502. id = os_strchr(uuid, ' ');
  1503. if (id == NULL)
  1504. return -1;
  1505. *id++ = '\0';
  1506. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1507. }
  1508. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1509. struct wpa_supplicant *wpa_s, char *cmd)
  1510. {
  1511. char *pin;
  1512. char *new_ssid;
  1513. char *new_auth;
  1514. char *new_encr;
  1515. char *new_key;
  1516. struct wps_new_ap_settings ap;
  1517. pin = os_strchr(cmd, ' ');
  1518. if (pin == NULL)
  1519. return -1;
  1520. *pin++ = '\0';
  1521. new_ssid = os_strchr(pin, ' ');
  1522. if (new_ssid == NULL)
  1523. return -1;
  1524. *new_ssid++ = '\0';
  1525. new_auth = os_strchr(new_ssid, ' ');
  1526. if (new_auth == NULL)
  1527. return -1;
  1528. *new_auth++ = '\0';
  1529. new_encr = os_strchr(new_auth, ' ');
  1530. if (new_encr == NULL)
  1531. return -1;
  1532. *new_encr++ = '\0';
  1533. new_key = os_strchr(new_encr, ' ');
  1534. if (new_key == NULL)
  1535. return -1;
  1536. *new_key++ = '\0';
  1537. os_memset(&ap, 0, sizeof(ap));
  1538. ap.ssid_hex = new_ssid;
  1539. ap.auth = new_auth;
  1540. ap.encr = new_encr;
  1541. ap.key_hex = new_key;
  1542. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1543. }
  1544. #ifdef CONFIG_WPS_NFC
  1545. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1546. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1547. {
  1548. int ndef;
  1549. struct wpabuf *buf;
  1550. int res;
  1551. char *uuid;
  1552. uuid = os_strchr(cmd, ' ');
  1553. if (uuid == NULL)
  1554. return -1;
  1555. *uuid++ = '\0';
  1556. if (os_strcmp(cmd, "WPS") == 0)
  1557. ndef = 0;
  1558. else if (os_strcmp(cmd, "NDEF") == 0)
  1559. ndef = 1;
  1560. else
  1561. return -1;
  1562. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1563. if (buf == NULL)
  1564. return -1;
  1565. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1566. wpabuf_len(buf));
  1567. reply[res++] = '\n';
  1568. reply[res] = '\0';
  1569. wpabuf_free(buf);
  1570. return res;
  1571. }
  1572. #endif /* CONFIG_WPS_NFC */
  1573. #endif /* CONFIG_WPS_ER */
  1574. #endif /* CONFIG_WPS */
  1575. #ifdef CONFIG_IBSS_RSN
  1576. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1577. struct wpa_supplicant *wpa_s, char *addr)
  1578. {
  1579. u8 peer[ETH_ALEN];
  1580. if (hwaddr_aton(addr, peer)) {
  1581. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1582. "address '%s'", addr);
  1583. return -1;
  1584. }
  1585. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1586. MAC2STR(peer));
  1587. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1588. }
  1589. #endif /* CONFIG_IBSS_RSN */
  1590. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1591. char *rsp)
  1592. {
  1593. #ifdef IEEE8021X_EAPOL
  1594. char *pos, *id_pos;
  1595. int id;
  1596. struct wpa_ssid *ssid;
  1597. pos = os_strchr(rsp, '-');
  1598. if (pos == NULL)
  1599. return -1;
  1600. *pos++ = '\0';
  1601. id_pos = pos;
  1602. pos = os_strchr(pos, ':');
  1603. if (pos == NULL)
  1604. return -1;
  1605. *pos++ = '\0';
  1606. id = atoi(id_pos);
  1607. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1608. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1609. (u8 *) pos, os_strlen(pos));
  1610. ssid = wpa_config_get_network(wpa_s->conf, id);
  1611. if (ssid == NULL) {
  1612. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1613. "to update", id);
  1614. return -1;
  1615. }
  1616. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1617. pos);
  1618. #else /* IEEE8021X_EAPOL */
  1619. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1620. return -1;
  1621. #endif /* IEEE8021X_EAPOL */
  1622. }
  1623. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1624. const char *params,
  1625. char *buf, size_t buflen)
  1626. {
  1627. char *pos, *end, tmp[30];
  1628. int res, verbose, wps, ret;
  1629. #ifdef CONFIG_HS20
  1630. const u8 *hs20;
  1631. #endif /* CONFIG_HS20 */
  1632. const u8 *sess_id;
  1633. size_t sess_id_len;
  1634. if (os_strcmp(params, "-DRIVER") == 0)
  1635. return wpa_drv_status(wpa_s, buf, buflen);
  1636. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1637. wps = os_strcmp(params, "-WPS") == 0;
  1638. pos = buf;
  1639. end = buf + buflen;
  1640. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1641. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1642. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1643. MAC2STR(wpa_s->bssid));
  1644. if (os_snprintf_error(end - pos, ret))
  1645. return pos - buf;
  1646. pos += ret;
  1647. ret = os_snprintf(pos, end - pos, "freq=%u\n",
  1648. wpa_s->assoc_freq);
  1649. if (os_snprintf_error(end - pos, ret))
  1650. return pos - buf;
  1651. pos += ret;
  1652. if (ssid) {
  1653. u8 *_ssid = ssid->ssid;
  1654. size_t ssid_len = ssid->ssid_len;
  1655. u8 ssid_buf[SSID_MAX_LEN];
  1656. if (ssid_len == 0) {
  1657. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1658. if (_res < 0)
  1659. ssid_len = 0;
  1660. else
  1661. ssid_len = _res;
  1662. _ssid = ssid_buf;
  1663. }
  1664. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1665. wpa_ssid_txt(_ssid, ssid_len),
  1666. ssid->id);
  1667. if (os_snprintf_error(end - pos, ret))
  1668. return pos - buf;
  1669. pos += ret;
  1670. if (wps && ssid->passphrase &&
  1671. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1672. (ssid->mode == WPAS_MODE_AP ||
  1673. ssid->mode == WPAS_MODE_P2P_GO)) {
  1674. ret = os_snprintf(pos, end - pos,
  1675. "passphrase=%s\n",
  1676. ssid->passphrase);
  1677. if (os_snprintf_error(end - pos, ret))
  1678. return pos - buf;
  1679. pos += ret;
  1680. }
  1681. if (ssid->id_str) {
  1682. ret = os_snprintf(pos, end - pos,
  1683. "id_str=%s\n",
  1684. ssid->id_str);
  1685. if (os_snprintf_error(end - pos, ret))
  1686. return pos - buf;
  1687. pos += ret;
  1688. }
  1689. switch (ssid->mode) {
  1690. case WPAS_MODE_INFRA:
  1691. ret = os_snprintf(pos, end - pos,
  1692. "mode=station\n");
  1693. break;
  1694. case WPAS_MODE_IBSS:
  1695. ret = os_snprintf(pos, end - pos,
  1696. "mode=IBSS\n");
  1697. break;
  1698. case WPAS_MODE_AP:
  1699. ret = os_snprintf(pos, end - pos,
  1700. "mode=AP\n");
  1701. break;
  1702. case WPAS_MODE_P2P_GO:
  1703. ret = os_snprintf(pos, end - pos,
  1704. "mode=P2P GO\n");
  1705. break;
  1706. case WPAS_MODE_P2P_GROUP_FORMATION:
  1707. ret = os_snprintf(pos, end - pos,
  1708. "mode=P2P GO - group "
  1709. "formation\n");
  1710. break;
  1711. case WPAS_MODE_MESH:
  1712. ret = os_snprintf(pos, end - pos,
  1713. "mode=mesh\n");
  1714. break;
  1715. default:
  1716. ret = 0;
  1717. break;
  1718. }
  1719. if (os_snprintf_error(end - pos, ret))
  1720. return pos - buf;
  1721. pos += ret;
  1722. }
  1723. #ifdef CONFIG_AP
  1724. if (wpa_s->ap_iface) {
  1725. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1726. end - pos,
  1727. verbose);
  1728. } else
  1729. #endif /* CONFIG_AP */
  1730. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1731. }
  1732. #ifdef CONFIG_SME
  1733. #ifdef CONFIG_SAE
  1734. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1735. #ifdef CONFIG_AP
  1736. !wpa_s->ap_iface &&
  1737. #endif /* CONFIG_AP */
  1738. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1739. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1740. wpa_s->sme.sae.group);
  1741. if (os_snprintf_error(end - pos, ret))
  1742. return pos - buf;
  1743. pos += ret;
  1744. }
  1745. #endif /* CONFIG_SAE */
  1746. #endif /* CONFIG_SME */
  1747. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1748. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1749. if (os_snprintf_error(end - pos, ret))
  1750. return pos - buf;
  1751. pos += ret;
  1752. if (wpa_s->l2 &&
  1753. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1754. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1755. if (os_snprintf_error(end - pos, ret))
  1756. return pos - buf;
  1757. pos += ret;
  1758. }
  1759. #ifdef CONFIG_P2P
  1760. if (wpa_s->global->p2p) {
  1761. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1762. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1763. if (os_snprintf_error(end - pos, ret))
  1764. return pos - buf;
  1765. pos += ret;
  1766. }
  1767. #endif /* CONFIG_P2P */
  1768. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1769. MAC2STR(wpa_s->own_addr));
  1770. if (os_snprintf_error(end - pos, ret))
  1771. return pos - buf;
  1772. pos += ret;
  1773. #ifdef CONFIG_HS20
  1774. if (wpa_s->current_bss &&
  1775. (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1776. HS20_IE_VENDOR_TYPE)) &&
  1777. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1778. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1779. int release = 1;
  1780. if (hs20[1] >= 5) {
  1781. u8 rel_num = (hs20[6] & 0xf0) >> 4;
  1782. release = rel_num + 1;
  1783. }
  1784. ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
  1785. if (os_snprintf_error(end - pos, ret))
  1786. return pos - buf;
  1787. pos += ret;
  1788. }
  1789. if (wpa_s->current_ssid) {
  1790. struct wpa_cred *cred;
  1791. char *type;
  1792. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1793. size_t i;
  1794. if (wpa_s->current_ssid->parent_cred != cred)
  1795. continue;
  1796. if (cred->provisioning_sp) {
  1797. ret = os_snprintf(pos, end - pos,
  1798. "provisioning_sp=%s\n",
  1799. cred->provisioning_sp);
  1800. if (os_snprintf_error(end - pos, ret))
  1801. return pos - buf;
  1802. pos += ret;
  1803. }
  1804. if (!cred->domain)
  1805. goto no_domain;
  1806. i = 0;
  1807. if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
  1808. struct wpabuf *names =
  1809. wpa_s->current_bss->anqp->domain_name;
  1810. for (i = 0; names && i < cred->num_domain; i++)
  1811. {
  1812. if (domain_name_list_contains(
  1813. names, cred->domain[i], 1))
  1814. break;
  1815. }
  1816. if (i == cred->num_domain)
  1817. i = 0; /* show first entry by default */
  1818. }
  1819. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1820. cred->domain[i]);
  1821. if (os_snprintf_error(end - pos, ret))
  1822. return pos - buf;
  1823. pos += ret;
  1824. no_domain:
  1825. if (wpa_s->current_bss == NULL ||
  1826. wpa_s->current_bss->anqp == NULL)
  1827. res = -1;
  1828. else
  1829. res = interworking_home_sp_cred(
  1830. wpa_s, cred,
  1831. wpa_s->current_bss->anqp->domain_name);
  1832. if (res > 0)
  1833. type = "home";
  1834. else if (res == 0)
  1835. type = "roaming";
  1836. else
  1837. type = "unknown";
  1838. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1839. if (os_snprintf_error(end - pos, ret))
  1840. return pos - buf;
  1841. pos += ret;
  1842. break;
  1843. }
  1844. }
  1845. #endif /* CONFIG_HS20 */
  1846. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1847. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1848. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1849. verbose);
  1850. if (res >= 0)
  1851. pos += res;
  1852. }
  1853. #ifdef CONFIG_MACSEC
  1854. res = ieee802_1x_kay_get_status(wpa_s->kay, pos, end - pos);
  1855. if (res > 0)
  1856. pos += res;
  1857. #endif /* CONFIG_MACSEC */
  1858. sess_id = eapol_sm_get_session_id(wpa_s->eapol, &sess_id_len);
  1859. if (sess_id) {
  1860. char *start = pos;
  1861. ret = os_snprintf(pos, end - pos, "eap_session_id=");
  1862. if (os_snprintf_error(end - pos, ret))
  1863. return start - buf;
  1864. pos += ret;
  1865. ret = wpa_snprintf_hex(pos, end - pos, sess_id, sess_id_len);
  1866. if (ret <= 0)
  1867. return start - buf;
  1868. pos += ret;
  1869. ret = os_snprintf(pos, end - pos, "\n");
  1870. if (os_snprintf_error(end - pos, ret))
  1871. return start - buf;
  1872. pos += ret;
  1873. }
  1874. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1875. if (res >= 0)
  1876. pos += res;
  1877. #ifdef CONFIG_WPS
  1878. {
  1879. char uuid_str[100];
  1880. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1881. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1882. if (os_snprintf_error(end - pos, ret))
  1883. return pos - buf;
  1884. pos += ret;
  1885. }
  1886. #endif /* CONFIG_WPS */
  1887. #ifdef ANDROID
  1888. /*
  1889. * Allow using the STATUS command with default behavior, say for debug,
  1890. * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
  1891. * events with STATUS-NO_EVENTS.
  1892. */
  1893. if (os_strcmp(params, "-NO_EVENTS")) {
  1894. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1895. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1896. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1897. wpa_s->wpa_state,
  1898. MAC2STR(wpa_s->bssid),
  1899. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1900. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1901. wpa_s->current_ssid->ssid_len) : "");
  1902. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1903. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1904. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1905. "- connection to " MACSTR
  1906. " completed %s [id=%d id_str=%s]",
  1907. MAC2STR(wpa_s->bssid), "(auth)",
  1908. ssid ? ssid->id : -1,
  1909. ssid && ssid->id_str ? ssid->id_str : "");
  1910. }
  1911. }
  1912. #endif /* ANDROID */
  1913. return pos - buf;
  1914. }
  1915. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1916. char *cmd)
  1917. {
  1918. char *pos;
  1919. int id;
  1920. struct wpa_ssid *ssid;
  1921. u8 bssid[ETH_ALEN];
  1922. /* cmd: "<network id> <BSSID>" */
  1923. pos = os_strchr(cmd, ' ');
  1924. if (pos == NULL)
  1925. return -1;
  1926. *pos++ = '\0';
  1927. id = atoi(cmd);
  1928. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1929. if (hwaddr_aton(pos, bssid)) {
  1930. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1931. return -1;
  1932. }
  1933. ssid = wpa_config_get_network(wpa_s->conf, id);
  1934. if (ssid == NULL) {
  1935. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1936. "to update", id);
  1937. return -1;
  1938. }
  1939. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1940. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1941. return 0;
  1942. }
  1943. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1944. char *cmd, char *buf,
  1945. size_t buflen)
  1946. {
  1947. u8 bssid[ETH_ALEN];
  1948. struct wpa_blacklist *e;
  1949. char *pos, *end;
  1950. int ret;
  1951. /* cmd: "BLACKLIST [<BSSID>]" */
  1952. if (*cmd == '\0') {
  1953. pos = buf;
  1954. end = buf + buflen;
  1955. e = wpa_s->blacklist;
  1956. while (e) {
  1957. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1958. MAC2STR(e->bssid));
  1959. if (os_snprintf_error(end - pos, ret))
  1960. return pos - buf;
  1961. pos += ret;
  1962. e = e->next;
  1963. }
  1964. return pos - buf;
  1965. }
  1966. cmd++;
  1967. if (os_strncmp(cmd, "clear", 5) == 0) {
  1968. wpa_blacklist_clear(wpa_s);
  1969. os_memcpy(buf, "OK\n", 3);
  1970. return 3;
  1971. }
  1972. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1973. if (hwaddr_aton(cmd, bssid)) {
  1974. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1975. return -1;
  1976. }
  1977. /*
  1978. * Add the BSSID twice, so its count will be 2, causing it to be
  1979. * skipped when processing scan results.
  1980. */
  1981. ret = wpa_blacklist_add(wpa_s, bssid);
  1982. if (ret < 0)
  1983. return -1;
  1984. ret = wpa_blacklist_add(wpa_s, bssid);
  1985. if (ret < 0)
  1986. return -1;
  1987. os_memcpy(buf, "OK\n", 3);
  1988. return 3;
  1989. }
  1990. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1991. char *cmd, char *buf,
  1992. size_t buflen)
  1993. {
  1994. char *pos, *end, *stamp;
  1995. int ret;
  1996. /* cmd: "LOG_LEVEL [<level>]" */
  1997. if (*cmd == '\0') {
  1998. pos = buf;
  1999. end = buf + buflen;
  2000. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  2001. "Timestamp: %d\n",
  2002. debug_level_str(wpa_debug_level),
  2003. wpa_debug_timestamp);
  2004. if (os_snprintf_error(end - pos, ret))
  2005. ret = 0;
  2006. return ret;
  2007. }
  2008. while (*cmd == ' ')
  2009. cmd++;
  2010. stamp = os_strchr(cmd, ' ');
  2011. if (stamp) {
  2012. *stamp++ = '\0';
  2013. while (*stamp == ' ') {
  2014. stamp++;
  2015. }
  2016. }
  2017. if (os_strlen(cmd)) {
  2018. int level = str_to_debug_level(cmd);
  2019. if (level < 0)
  2020. return -1;
  2021. wpa_debug_level = level;
  2022. }
  2023. if (stamp && os_strlen(stamp))
  2024. wpa_debug_timestamp = atoi(stamp);
  2025. os_memcpy(buf, "OK\n", 3);
  2026. return 3;
  2027. }
  2028. static int wpa_supplicant_ctrl_iface_list_networks(
  2029. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2030. {
  2031. char *pos, *end, *prev;
  2032. struct wpa_ssid *ssid;
  2033. int ret;
  2034. pos = buf;
  2035. end = buf + buflen;
  2036. ret = os_snprintf(pos, end - pos,
  2037. "network id / ssid / bssid / flags\n");
  2038. if (os_snprintf_error(end - pos, ret))
  2039. return pos - buf;
  2040. pos += ret;
  2041. ssid = wpa_s->conf->ssid;
  2042. /* skip over ssids until we find next one */
  2043. if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
  2044. int last_id = atoi(cmd + 8);
  2045. if (last_id != -1) {
  2046. while (ssid != NULL && ssid->id <= last_id) {
  2047. ssid = ssid->next;
  2048. }
  2049. }
  2050. }
  2051. while (ssid) {
  2052. prev = pos;
  2053. ret = os_snprintf(pos, end - pos, "%d\t%s",
  2054. ssid->id,
  2055. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  2056. if (os_snprintf_error(end - pos, ret))
  2057. return prev - buf;
  2058. pos += ret;
  2059. if (ssid->bssid_set) {
  2060. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  2061. MAC2STR(ssid->bssid));
  2062. } else {
  2063. ret = os_snprintf(pos, end - pos, "\tany");
  2064. }
  2065. if (os_snprintf_error(end - pos, ret))
  2066. return prev - buf;
  2067. pos += ret;
  2068. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  2069. ssid == wpa_s->current_ssid ?
  2070. "[CURRENT]" : "",
  2071. ssid->disabled ? "[DISABLED]" : "",
  2072. ssid->disabled_until.sec ?
  2073. "[TEMP-DISABLED]" : "",
  2074. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  2075. "");
  2076. if (os_snprintf_error(end - pos, ret))
  2077. return prev - buf;
  2078. pos += ret;
  2079. ret = os_snprintf(pos, end - pos, "\n");
  2080. if (os_snprintf_error(end - pos, ret))
  2081. return prev - buf;
  2082. pos += ret;
  2083. ssid = ssid->next;
  2084. }
  2085. return pos - buf;
  2086. }
  2087. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  2088. {
  2089. int ret;
  2090. ret = os_snprintf(pos, end - pos, "-");
  2091. if (os_snprintf_error(end - pos, ret))
  2092. return pos;
  2093. pos += ret;
  2094. ret = wpa_write_ciphers(pos, end, cipher, "+");
  2095. if (ret < 0)
  2096. return pos;
  2097. pos += ret;
  2098. return pos;
  2099. }
  2100. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  2101. const u8 *ie, size_t ie_len)
  2102. {
  2103. struct wpa_ie_data data;
  2104. char *start;
  2105. int ret;
  2106. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  2107. if (os_snprintf_error(end - pos, ret))
  2108. return pos;
  2109. pos += ret;
  2110. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  2111. ret = os_snprintf(pos, end - pos, "?]");
  2112. if (os_snprintf_error(end - pos, ret))
  2113. return pos;
  2114. pos += ret;
  2115. return pos;
  2116. }
  2117. start = pos;
  2118. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  2119. ret = os_snprintf(pos, end - pos, "%sEAP",
  2120. pos == start ? "" : "+");
  2121. if (os_snprintf_error(end - pos, ret))
  2122. return pos;
  2123. pos += ret;
  2124. }
  2125. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  2126. ret = os_snprintf(pos, end - pos, "%sPSK",
  2127. pos == start ? "" : "+");
  2128. if (os_snprintf_error(end - pos, ret))
  2129. return pos;
  2130. pos += ret;
  2131. }
  2132. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  2133. ret = os_snprintf(pos, end - pos, "%sNone",
  2134. pos == start ? "" : "+");
  2135. if (os_snprintf_error(end - pos, ret))
  2136. return pos;
  2137. pos += ret;
  2138. }
  2139. if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
  2140. ret = os_snprintf(pos, end - pos, "%sSAE",
  2141. pos == start ? "" : "+");
  2142. if (os_snprintf_error(end - pos, ret))
  2143. return pos;
  2144. pos += ret;
  2145. }
  2146. #ifdef CONFIG_IEEE80211R
  2147. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  2148. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  2149. pos == start ? "" : "+");
  2150. if (os_snprintf_error(end - pos, ret))
  2151. return pos;
  2152. pos += ret;
  2153. }
  2154. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  2155. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  2156. pos == start ? "" : "+");
  2157. if (os_snprintf_error(end - pos, ret))
  2158. return pos;
  2159. pos += ret;
  2160. }
  2161. if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  2162. ret = os_snprintf(pos, end - pos, "%sFT/SAE",
  2163. pos == start ? "" : "+");
  2164. if (os_snprintf_error(end - pos, ret))
  2165. return pos;
  2166. pos += ret;
  2167. }
  2168. #endif /* CONFIG_IEEE80211R */
  2169. #ifdef CONFIG_IEEE80211W
  2170. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  2171. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  2172. pos == start ? "" : "+");
  2173. if (os_snprintf_error(end - pos, ret))
  2174. return pos;
  2175. pos += ret;
  2176. }
  2177. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  2178. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  2179. pos == start ? "" : "+");
  2180. if (os_snprintf_error(end - pos, ret))
  2181. return pos;
  2182. pos += ret;
  2183. }
  2184. #endif /* CONFIG_IEEE80211W */
  2185. #ifdef CONFIG_SUITEB
  2186. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  2187. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
  2188. pos == start ? "" : "+");
  2189. if (os_snprintf_error(end - pos, ret))
  2190. return pos;
  2191. pos += ret;
  2192. }
  2193. #endif /* CONFIG_SUITEB */
  2194. #ifdef CONFIG_SUITEB192
  2195. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B_192) {
  2196. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B-192",
  2197. pos == start ? "" : "+");
  2198. if (os_snprintf_error(end - pos, ret))
  2199. return pos;
  2200. pos += ret;
  2201. }
  2202. #endif /* CONFIG_SUITEB192 */
  2203. #ifdef CONFIG_FILS
  2204. if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA256) {
  2205. ret = os_snprintf(pos, end - pos, "%sFILS-SHA256",
  2206. pos == start ? "" : "+");
  2207. if (os_snprintf_error(end - pos, ret))
  2208. return pos;
  2209. pos += ret;
  2210. }
  2211. if (data.key_mgmt & WPA_KEY_MGMT_FILS_SHA384) {
  2212. ret = os_snprintf(pos, end - pos, "%sFILS-SHA384",
  2213. pos == start ? "" : "+");
  2214. if (os_snprintf_error(end - pos, ret))
  2215. return pos;
  2216. pos += ret;
  2217. }
  2218. #ifdef CONFIG_IEEE80211R
  2219. if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA256) {
  2220. ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA256",
  2221. pos == start ? "" : "+");
  2222. if (os_snprintf_error(end - pos, ret))
  2223. return pos;
  2224. pos += ret;
  2225. }
  2226. if (data.key_mgmt & WPA_KEY_MGMT_FT_FILS_SHA384) {
  2227. ret = os_snprintf(pos, end - pos, "%sFT-FILS-SHA384",
  2228. pos == start ? "" : "+");
  2229. if (os_snprintf_error(end - pos, ret))
  2230. return pos;
  2231. pos += ret;
  2232. }
  2233. #endif /* CONFIG_IEEE80211R */
  2234. #endif /* CONFIG_FILS */
  2235. #ifdef CONFIG_OWE
  2236. if (data.key_mgmt & WPA_KEY_MGMT_OWE) {
  2237. ret = os_snprintf(pos, end - pos, "%sOWE",
  2238. pos == start ? "" : "+");
  2239. if (os_snprintf_error(end - pos, ret))
  2240. return pos;
  2241. pos += ret;
  2242. }
  2243. #endif /* CONFIG_OWE */
  2244. #ifdef CONFIG_DPP
  2245. if (data.key_mgmt & WPA_KEY_MGMT_DPP) {
  2246. ret = os_snprintf(pos, end - pos, "%sDPP",
  2247. pos == start ? "" : "+");
  2248. if (os_snprintf_error(end - pos, ret))
  2249. return pos;
  2250. pos += ret;
  2251. }
  2252. #endif /* CONFIG_DPP */
  2253. if (data.key_mgmt & WPA_KEY_MGMT_OSEN) {
  2254. ret = os_snprintf(pos, end - pos, "%sOSEN",
  2255. pos == start ? "" : "+");
  2256. if (os_snprintf_error(end - pos, ret))
  2257. return pos;
  2258. pos += ret;
  2259. }
  2260. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  2261. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  2262. ret = os_snprintf(pos, end - pos, "-preauth");
  2263. if (os_snprintf_error(end - pos, ret))
  2264. return pos;
  2265. pos += ret;
  2266. }
  2267. ret = os_snprintf(pos, end - pos, "]");
  2268. if (os_snprintf_error(end - pos, ret))
  2269. return pos;
  2270. pos += ret;
  2271. return pos;
  2272. }
  2273. #ifdef CONFIG_WPS
  2274. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  2275. char *pos, char *end,
  2276. struct wpabuf *wps_ie)
  2277. {
  2278. int ret;
  2279. const char *txt;
  2280. if (wps_ie == NULL)
  2281. return pos;
  2282. if (wps_is_selected_pbc_registrar(wps_ie))
  2283. txt = "[WPS-PBC]";
  2284. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  2285. txt = "[WPS-AUTH]";
  2286. else if (wps_is_selected_pin_registrar(wps_ie))
  2287. txt = "[WPS-PIN]";
  2288. else
  2289. txt = "[WPS]";
  2290. ret = os_snprintf(pos, end - pos, "%s", txt);
  2291. if (!os_snprintf_error(end - pos, ret))
  2292. pos += ret;
  2293. wpabuf_free(wps_ie);
  2294. return pos;
  2295. }
  2296. #endif /* CONFIG_WPS */
  2297. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  2298. char *pos, char *end,
  2299. const struct wpa_bss *bss)
  2300. {
  2301. #ifdef CONFIG_WPS
  2302. struct wpabuf *wps_ie;
  2303. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  2304. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  2305. #else /* CONFIG_WPS */
  2306. return pos;
  2307. #endif /* CONFIG_WPS */
  2308. }
  2309. /* Format one result on one text line into a buffer. */
  2310. static int wpa_supplicant_ctrl_iface_scan_result(
  2311. struct wpa_supplicant *wpa_s,
  2312. const struct wpa_bss *bss, char *buf, size_t buflen)
  2313. {
  2314. char *pos, *end;
  2315. int ret;
  2316. const u8 *ie, *ie2, *osen_ie, *p2p, *mesh;
  2317. mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
  2318. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  2319. if (!p2p)
  2320. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  2321. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  2322. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  2323. 0)
  2324. return 0; /* Do not show P2P listen discovery results here */
  2325. pos = buf;
  2326. end = buf + buflen;
  2327. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  2328. MAC2STR(bss->bssid), bss->freq, bss->level);
  2329. if (os_snprintf_error(end - pos, ret))
  2330. return -1;
  2331. pos += ret;
  2332. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2333. if (ie)
  2334. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  2335. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2336. if (ie2) {
  2337. pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
  2338. ie2, 2 + ie2[1]);
  2339. }
  2340. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  2341. if (osen_ie)
  2342. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  2343. osen_ie, 2 + osen_ie[1]);
  2344. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2345. if (!ie && !ie2 && !osen_ie && (bss->caps & IEEE80211_CAP_PRIVACY)) {
  2346. ret = os_snprintf(pos, end - pos, "[WEP]");
  2347. if (os_snprintf_error(end - pos, ret))
  2348. return -1;
  2349. pos += ret;
  2350. }
  2351. if (mesh) {
  2352. ret = os_snprintf(pos, end - pos, "[MESH]");
  2353. if (os_snprintf_error(end - pos, ret))
  2354. return -1;
  2355. pos += ret;
  2356. }
  2357. if (bss_is_dmg(bss)) {
  2358. const char *s;
  2359. ret = os_snprintf(pos, end - pos, "[DMG]");
  2360. if (os_snprintf_error(end - pos, ret))
  2361. return -1;
  2362. pos += ret;
  2363. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  2364. case IEEE80211_CAP_DMG_IBSS:
  2365. s = "[IBSS]";
  2366. break;
  2367. case IEEE80211_CAP_DMG_AP:
  2368. s = "[ESS]";
  2369. break;
  2370. case IEEE80211_CAP_DMG_PBSS:
  2371. s = "[PBSS]";
  2372. break;
  2373. default:
  2374. s = "";
  2375. break;
  2376. }
  2377. ret = os_snprintf(pos, end - pos, "%s", s);
  2378. if (os_snprintf_error(end - pos, ret))
  2379. return -1;
  2380. pos += ret;
  2381. } else {
  2382. if (bss->caps & IEEE80211_CAP_IBSS) {
  2383. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2384. if (os_snprintf_error(end - pos, ret))
  2385. return -1;
  2386. pos += ret;
  2387. }
  2388. if (bss->caps & IEEE80211_CAP_ESS) {
  2389. ret = os_snprintf(pos, end - pos, "[ESS]");
  2390. if (os_snprintf_error(end - pos, ret))
  2391. return -1;
  2392. pos += ret;
  2393. }
  2394. }
  2395. if (p2p) {
  2396. ret = os_snprintf(pos, end - pos, "[P2P]");
  2397. if (os_snprintf_error(end - pos, ret))
  2398. return -1;
  2399. pos += ret;
  2400. }
  2401. #ifdef CONFIG_HS20
  2402. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  2403. ret = os_snprintf(pos, end - pos, "[HS20]");
  2404. if (os_snprintf_error(end - pos, ret))
  2405. return -1;
  2406. pos += ret;
  2407. }
  2408. #endif /* CONFIG_HS20 */
  2409. #ifdef CONFIG_FILS
  2410. if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
  2411. ret = os_snprintf(pos, end - pos, "[FILS]");
  2412. if (os_snprintf_error(end - pos, ret))
  2413. return -1;
  2414. pos += ret;
  2415. }
  2416. #endif /* CONFIG_FILS */
  2417. #ifdef CONFIG_FST
  2418. if (wpa_bss_get_ie(bss, WLAN_EID_MULTI_BAND)) {
  2419. ret = os_snprintf(pos, end - pos, "[FST]");
  2420. if (os_snprintf_error(end - pos, ret))
  2421. return -1;
  2422. pos += ret;
  2423. }
  2424. #endif /* CONFIG_FST */
  2425. ret = os_snprintf(pos, end - pos, "\t%s",
  2426. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2427. if (os_snprintf_error(end - pos, ret))
  2428. return -1;
  2429. pos += ret;
  2430. ret = os_snprintf(pos, end - pos, "\n");
  2431. if (os_snprintf_error(end - pos, ret))
  2432. return -1;
  2433. pos += ret;
  2434. return pos - buf;
  2435. }
  2436. static int wpa_supplicant_ctrl_iface_scan_results(
  2437. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2438. {
  2439. char *pos, *end;
  2440. struct wpa_bss *bss;
  2441. int ret;
  2442. pos = buf;
  2443. end = buf + buflen;
  2444. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  2445. "flags / ssid\n");
  2446. if (os_snprintf_error(end - pos, ret))
  2447. return pos - buf;
  2448. pos += ret;
  2449. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2450. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  2451. end - pos);
  2452. if (ret < 0 || ret >= end - pos)
  2453. return pos - buf;
  2454. pos += ret;
  2455. }
  2456. return pos - buf;
  2457. }
  2458. #ifdef CONFIG_MESH
  2459. static int wpa_supplicant_ctrl_iface_mesh_interface_add(
  2460. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  2461. {
  2462. char *pos, ifname[IFNAMSIZ + 1];
  2463. ifname[0] = '\0';
  2464. pos = os_strstr(cmd, "ifname=");
  2465. if (pos) {
  2466. pos += 7;
  2467. os_strlcpy(ifname, pos, sizeof(ifname));
  2468. }
  2469. if (wpas_mesh_add_interface(wpa_s, ifname, sizeof(ifname)) < 0)
  2470. return -1;
  2471. os_strlcpy(reply, ifname, max_len);
  2472. return os_strlen(ifname);
  2473. }
  2474. static int wpa_supplicant_ctrl_iface_mesh_group_add(
  2475. struct wpa_supplicant *wpa_s, char *cmd)
  2476. {
  2477. int id;
  2478. struct wpa_ssid *ssid;
  2479. id = atoi(cmd);
  2480. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
  2481. ssid = wpa_config_get_network(wpa_s->conf, id);
  2482. if (ssid == NULL) {
  2483. wpa_printf(MSG_DEBUG,
  2484. "CTRL_IFACE: Could not find network id=%d", id);
  2485. return -1;
  2486. }
  2487. if (ssid->mode != WPAS_MODE_MESH) {
  2488. wpa_printf(MSG_DEBUG,
  2489. "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
  2490. return -1;
  2491. }
  2492. if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
  2493. ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
  2494. wpa_printf(MSG_ERROR,
  2495. "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
  2496. return -1;
  2497. }
  2498. /*
  2499. * TODO: If necessary write our own group_add function,
  2500. * for now we can reuse select_network
  2501. */
  2502. wpa_supplicant_select_network(wpa_s, ssid);
  2503. return 0;
  2504. }
  2505. static int wpa_supplicant_ctrl_iface_mesh_group_remove(
  2506. struct wpa_supplicant *wpa_s, char *cmd)
  2507. {
  2508. struct wpa_supplicant *orig;
  2509. struct wpa_global *global;
  2510. int found = 0;
  2511. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
  2512. global = wpa_s->global;
  2513. orig = wpa_s;
  2514. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  2515. if (os_strcmp(wpa_s->ifname, cmd) == 0) {
  2516. found = 1;
  2517. break;
  2518. }
  2519. }
  2520. if (!found) {
  2521. wpa_printf(MSG_ERROR,
  2522. "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s not found",
  2523. cmd);
  2524. return -1;
  2525. }
  2526. if (wpa_s->mesh_if_created && wpa_s == orig) {
  2527. wpa_printf(MSG_ERROR,
  2528. "CTRL_IFACE: MESH_GROUP_REMOVE can't remove itself");
  2529. return -1;
  2530. }
  2531. wpa_s->reassociate = 0;
  2532. wpa_s->disconnected = 1;
  2533. wpa_supplicant_cancel_sched_scan(wpa_s);
  2534. wpa_supplicant_cancel_scan(wpa_s);
  2535. /*
  2536. * TODO: If necessary write our own group_remove function,
  2537. * for now we can reuse deauthenticate
  2538. */
  2539. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2540. if (wpa_s->mesh_if_created)
  2541. wpa_supplicant_remove_iface(global, wpa_s, 0);
  2542. return 0;
  2543. }
  2544. static int wpa_supplicant_ctrl_iface_mesh_peer_remove(
  2545. struct wpa_supplicant *wpa_s, char *cmd)
  2546. {
  2547. u8 addr[ETH_ALEN];
  2548. if (hwaddr_aton(cmd, addr) < 0)
  2549. return -1;
  2550. return wpas_mesh_peer_remove(wpa_s, addr);
  2551. }
  2552. static int wpa_supplicant_ctrl_iface_mesh_peer_add(
  2553. struct wpa_supplicant *wpa_s, char *cmd)
  2554. {
  2555. u8 addr[ETH_ALEN];
  2556. int duration;
  2557. char *pos;
  2558. pos = os_strstr(cmd, " duration=");
  2559. if (pos) {
  2560. *pos = '\0';
  2561. duration = atoi(pos + 10);
  2562. } else {
  2563. duration = -1;
  2564. }
  2565. if (hwaddr_aton(cmd, addr))
  2566. return -1;
  2567. return wpas_mesh_peer_add(wpa_s, addr, duration);
  2568. }
  2569. #endif /* CONFIG_MESH */
  2570. static int wpa_supplicant_ctrl_iface_select_network(
  2571. struct wpa_supplicant *wpa_s, char *cmd)
  2572. {
  2573. int id;
  2574. struct wpa_ssid *ssid;
  2575. char *pos;
  2576. /* cmd: "<network id>" or "any" */
  2577. if (os_strncmp(cmd, "any", 3) == 0) {
  2578. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  2579. ssid = NULL;
  2580. } else {
  2581. id = atoi(cmd);
  2582. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  2583. ssid = wpa_config_get_network(wpa_s->conf, id);
  2584. if (ssid == NULL) {
  2585. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2586. "network id=%d", id);
  2587. return -1;
  2588. }
  2589. if (ssid->disabled == 2) {
  2590. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2591. "SELECT_NETWORK with persistent P2P group");
  2592. return -1;
  2593. }
  2594. }
  2595. pos = os_strstr(cmd, " freq=");
  2596. if (pos) {
  2597. int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
  2598. if (freqs) {
  2599. os_free(wpa_s->select_network_scan_freqs);
  2600. wpa_s->select_network_scan_freqs = freqs;
  2601. }
  2602. }
  2603. wpa_s->scan_min_time.sec = 0;
  2604. wpa_s->scan_min_time.usec = 0;
  2605. wpa_supplicant_select_network(wpa_s, ssid);
  2606. return 0;
  2607. }
  2608. static int wpa_supplicant_ctrl_iface_enable_network(
  2609. struct wpa_supplicant *wpa_s, char *cmd)
  2610. {
  2611. int id;
  2612. struct wpa_ssid *ssid;
  2613. /* cmd: "<network id>" or "all" */
  2614. if (os_strcmp(cmd, "all") == 0) {
  2615. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2616. ssid = NULL;
  2617. } else {
  2618. id = atoi(cmd);
  2619. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2620. ssid = wpa_config_get_network(wpa_s->conf, id);
  2621. if (ssid == NULL) {
  2622. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2623. "network id=%d", id);
  2624. return -1;
  2625. }
  2626. if (ssid->disabled == 2) {
  2627. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2628. "ENABLE_NETWORK with persistent P2P group");
  2629. return -1;
  2630. }
  2631. if (os_strstr(cmd, " no-connect")) {
  2632. ssid->disabled = 0;
  2633. return 0;
  2634. }
  2635. }
  2636. wpa_s->scan_min_time.sec = 0;
  2637. wpa_s->scan_min_time.usec = 0;
  2638. wpa_supplicant_enable_network(wpa_s, ssid);
  2639. return 0;
  2640. }
  2641. static int wpa_supplicant_ctrl_iface_disable_network(
  2642. struct wpa_supplicant *wpa_s, char *cmd)
  2643. {
  2644. int id;
  2645. struct wpa_ssid *ssid;
  2646. /* cmd: "<network id>" or "all" */
  2647. if (os_strcmp(cmd, "all") == 0) {
  2648. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2649. ssid = NULL;
  2650. } else {
  2651. id = atoi(cmd);
  2652. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2653. ssid = wpa_config_get_network(wpa_s->conf, id);
  2654. if (ssid == NULL) {
  2655. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2656. "network id=%d", id);
  2657. return -1;
  2658. }
  2659. if (ssid->disabled == 2) {
  2660. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2661. "DISABLE_NETWORK with persistent P2P "
  2662. "group");
  2663. return -1;
  2664. }
  2665. }
  2666. wpa_supplicant_disable_network(wpa_s, ssid);
  2667. return 0;
  2668. }
  2669. static int wpa_supplicant_ctrl_iface_add_network(
  2670. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2671. {
  2672. struct wpa_ssid *ssid;
  2673. int ret;
  2674. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2675. ssid = wpa_supplicant_add_network(wpa_s);
  2676. if (ssid == NULL)
  2677. return -1;
  2678. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2679. if (os_snprintf_error(buflen, ret))
  2680. return -1;
  2681. return ret;
  2682. }
  2683. static int wpa_supplicant_ctrl_iface_remove_network(
  2684. struct wpa_supplicant *wpa_s, char *cmd)
  2685. {
  2686. int id;
  2687. struct wpa_ssid *ssid;
  2688. int result;
  2689. /* cmd: "<network id>" or "all" */
  2690. if (os_strcmp(cmd, "all") == 0) {
  2691. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2692. if (wpa_s->sched_scanning)
  2693. wpa_supplicant_cancel_sched_scan(wpa_s);
  2694. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2695. if (wpa_s->current_ssid) {
  2696. #ifdef CONFIG_SME
  2697. wpa_s->sme.prev_bssid_set = 0;
  2698. #endif /* CONFIG_SME */
  2699. wpa_sm_set_config(wpa_s->wpa, NULL);
  2700. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2701. if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
  2702. wpa_s->own_disconnect_req = 1;
  2703. wpa_supplicant_deauthenticate(
  2704. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2705. }
  2706. ssid = wpa_s->conf->ssid;
  2707. while (ssid) {
  2708. struct wpa_ssid *remove_ssid = ssid;
  2709. id = ssid->id;
  2710. ssid = ssid->next;
  2711. if (wpa_s->last_ssid == remove_ssid)
  2712. wpa_s->last_ssid = NULL;
  2713. wpas_notify_network_removed(wpa_s, remove_ssid);
  2714. wpa_config_remove_network(wpa_s->conf, id);
  2715. }
  2716. return 0;
  2717. }
  2718. id = atoi(cmd);
  2719. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2720. result = wpa_supplicant_remove_network(wpa_s, id);
  2721. if (result == -1) {
  2722. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2723. "id=%d", id);
  2724. return -1;
  2725. }
  2726. if (result == -2) {
  2727. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2728. "network id=%d", id);
  2729. return -1;
  2730. }
  2731. return 0;
  2732. }
  2733. static int wpa_supplicant_ctrl_iface_update_network(
  2734. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2735. char *name, char *value)
  2736. {
  2737. int ret;
  2738. ret = wpa_config_set(ssid, name, value, 0);
  2739. if (ret < 0) {
  2740. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2741. "variable '%s'", name);
  2742. return -1;
  2743. }
  2744. if (ret == 1)
  2745. return 0; /* No change to the previously configured value */
  2746. if (os_strcmp(name, "bssid") != 0 &&
  2747. os_strcmp(name, "bssid_hint") != 0 &&
  2748. os_strcmp(name, "priority") != 0) {
  2749. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2750. if (wpa_s->current_ssid == ssid ||
  2751. wpa_s->current_ssid == NULL) {
  2752. /*
  2753. * Invalidate the EAP session cache if anything in the
  2754. * current or previously used configuration changes.
  2755. */
  2756. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2757. }
  2758. }
  2759. if ((os_strcmp(name, "psk") == 0 &&
  2760. value[0] == '"' && ssid->ssid_len) ||
  2761. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2762. wpa_config_update_psk(ssid);
  2763. else if (os_strcmp(name, "priority") == 0)
  2764. wpa_config_update_prio_list(wpa_s->conf);
  2765. return 0;
  2766. }
  2767. static int wpa_supplicant_ctrl_iface_set_network(
  2768. struct wpa_supplicant *wpa_s, char *cmd)
  2769. {
  2770. int id, ret, prev_bssid_set, prev_disabled;
  2771. struct wpa_ssid *ssid;
  2772. char *name, *value;
  2773. u8 prev_bssid[ETH_ALEN];
  2774. /* cmd: "<network id> <variable name> <value>" */
  2775. name = os_strchr(cmd, ' ');
  2776. if (name == NULL)
  2777. return -1;
  2778. *name++ = '\0';
  2779. value = os_strchr(name, ' ');
  2780. if (value == NULL)
  2781. return -1;
  2782. *value++ = '\0';
  2783. id = atoi(cmd);
  2784. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2785. id, name);
  2786. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2787. (u8 *) value, os_strlen(value));
  2788. ssid = wpa_config_get_network(wpa_s->conf, id);
  2789. if (ssid == NULL) {
  2790. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2791. "id=%d", id);
  2792. return -1;
  2793. }
  2794. prev_bssid_set = ssid->bssid_set;
  2795. prev_disabled = ssid->disabled;
  2796. os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
  2797. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
  2798. value);
  2799. if (ret == 0 &&
  2800. (ssid->bssid_set != prev_bssid_set ||
  2801. os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
  2802. wpas_notify_network_bssid_set_changed(wpa_s, ssid);
  2803. if (prev_disabled != ssid->disabled &&
  2804. (prev_disabled == 2 || ssid->disabled == 2))
  2805. wpas_notify_network_type_changed(wpa_s, ssid);
  2806. return ret;
  2807. }
  2808. static int wpa_supplicant_ctrl_iface_get_network(
  2809. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2810. {
  2811. int id;
  2812. size_t res;
  2813. struct wpa_ssid *ssid;
  2814. char *name, *value;
  2815. /* cmd: "<network id> <variable name>" */
  2816. name = os_strchr(cmd, ' ');
  2817. if (name == NULL || buflen == 0)
  2818. return -1;
  2819. *name++ = '\0';
  2820. id = atoi(cmd);
  2821. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2822. id, name);
  2823. ssid = wpa_config_get_network(wpa_s->conf, id);
  2824. if (ssid == NULL) {
  2825. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Could not find network "
  2826. "id=%d", id);
  2827. return -1;
  2828. }
  2829. value = wpa_config_get_no_key(ssid, name);
  2830. if (value == NULL) {
  2831. wpa_printf(MSG_EXCESSIVE, "CTRL_IFACE: Failed to get network "
  2832. "variable '%s'", name);
  2833. return -1;
  2834. }
  2835. res = os_strlcpy(buf, value, buflen);
  2836. if (res >= buflen) {
  2837. os_free(value);
  2838. return -1;
  2839. }
  2840. os_free(value);
  2841. return res;
  2842. }
  2843. static int wpa_supplicant_ctrl_iface_dup_network(
  2844. struct wpa_supplicant *wpa_s, char *cmd,
  2845. struct wpa_supplicant *dst_wpa_s)
  2846. {
  2847. struct wpa_ssid *ssid_s, *ssid_d;
  2848. char *name, *id, *value;
  2849. int id_s, id_d, ret;
  2850. /* cmd: "<src network id> <dst network id> <variable name>" */
  2851. id = os_strchr(cmd, ' ');
  2852. if (id == NULL)
  2853. return -1;
  2854. *id++ = '\0';
  2855. name = os_strchr(id, ' ');
  2856. if (name == NULL)
  2857. return -1;
  2858. *name++ = '\0';
  2859. id_s = atoi(cmd);
  2860. id_d = atoi(id);
  2861. wpa_printf(MSG_DEBUG,
  2862. "CTRL_IFACE: DUP_NETWORK ifname=%s->%s id=%d->%d name='%s'",
  2863. wpa_s->ifname, dst_wpa_s->ifname, id_s, id_d, name);
  2864. ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
  2865. if (ssid_s == NULL) {
  2866. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2867. "network id=%d", id_s);
  2868. return -1;
  2869. }
  2870. ssid_d = wpa_config_get_network(dst_wpa_s->conf, id_d);
  2871. if (ssid_d == NULL) {
  2872. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2873. "network id=%d", id_d);
  2874. return -1;
  2875. }
  2876. value = wpa_config_get(ssid_s, name);
  2877. if (value == NULL) {
  2878. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2879. "variable '%s'", name);
  2880. return -1;
  2881. }
  2882. ret = wpa_supplicant_ctrl_iface_update_network(dst_wpa_s, ssid_d, name,
  2883. value);
  2884. os_free(value);
  2885. return ret;
  2886. }
  2887. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2888. char *buf, size_t buflen)
  2889. {
  2890. char *pos, *end;
  2891. struct wpa_cred *cred;
  2892. int ret;
  2893. pos = buf;
  2894. end = buf + buflen;
  2895. ret = os_snprintf(pos, end - pos,
  2896. "cred id / realm / username / domain / imsi\n");
  2897. if (os_snprintf_error(end - pos, ret))
  2898. return pos - buf;
  2899. pos += ret;
  2900. cred = wpa_s->conf->cred;
  2901. while (cred) {
  2902. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2903. cred->id, cred->realm ? cred->realm : "",
  2904. cred->username ? cred->username : "",
  2905. cred->domain ? cred->domain[0] : "",
  2906. cred->imsi ? cred->imsi : "");
  2907. if (os_snprintf_error(end - pos, ret))
  2908. return pos - buf;
  2909. pos += ret;
  2910. cred = cred->next;
  2911. }
  2912. return pos - buf;
  2913. }
  2914. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2915. char *buf, size_t buflen)
  2916. {
  2917. struct wpa_cred *cred;
  2918. int ret;
  2919. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2920. cred = wpa_config_add_cred(wpa_s->conf);
  2921. if (cred == NULL)
  2922. return -1;
  2923. wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
  2924. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2925. if (os_snprintf_error(buflen, ret))
  2926. return -1;
  2927. return ret;
  2928. }
  2929. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2930. struct wpa_cred *cred)
  2931. {
  2932. struct wpa_ssid *ssid;
  2933. char str[20];
  2934. int id;
  2935. if (cred == NULL) {
  2936. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2937. return -1;
  2938. }
  2939. id = cred->id;
  2940. if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  2941. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2942. return -1;
  2943. }
  2944. wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
  2945. /* Remove any network entry created based on the removed credential */
  2946. ssid = wpa_s->conf->ssid;
  2947. while (ssid) {
  2948. if (ssid->parent_cred == cred) {
  2949. int res;
  2950. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2951. "used the removed credential", ssid->id);
  2952. res = os_snprintf(str, sizeof(str), "%d", ssid->id);
  2953. if (os_snprintf_error(sizeof(str), res))
  2954. str[sizeof(str) - 1] = '\0';
  2955. ssid = ssid->next;
  2956. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2957. } else
  2958. ssid = ssid->next;
  2959. }
  2960. return 0;
  2961. }
  2962. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2963. char *cmd)
  2964. {
  2965. int id;
  2966. struct wpa_cred *cred, *prev;
  2967. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2968. * "provisioning_sp=<FQDN> */
  2969. if (os_strcmp(cmd, "all") == 0) {
  2970. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2971. cred = wpa_s->conf->cred;
  2972. while (cred) {
  2973. prev = cred;
  2974. cred = cred->next;
  2975. wpas_ctrl_remove_cred(wpa_s, prev);
  2976. }
  2977. return 0;
  2978. }
  2979. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2980. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2981. cmd + 8);
  2982. cred = wpa_s->conf->cred;
  2983. while (cred) {
  2984. prev = cred;
  2985. cred = cred->next;
  2986. if (prev->domain) {
  2987. size_t i;
  2988. for (i = 0; i < prev->num_domain; i++) {
  2989. if (os_strcmp(prev->domain[i], cmd + 8)
  2990. != 0)
  2991. continue;
  2992. wpas_ctrl_remove_cred(wpa_s, prev);
  2993. break;
  2994. }
  2995. }
  2996. }
  2997. return 0;
  2998. }
  2999. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  3000. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  3001. cmd + 16);
  3002. cred = wpa_s->conf->cred;
  3003. while (cred) {
  3004. prev = cred;
  3005. cred = cred->next;
  3006. if (prev->provisioning_sp &&
  3007. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  3008. wpas_ctrl_remove_cred(wpa_s, prev);
  3009. }
  3010. return 0;
  3011. }
  3012. id = atoi(cmd);
  3013. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  3014. cred = wpa_config_get_cred(wpa_s->conf, id);
  3015. return wpas_ctrl_remove_cred(wpa_s, cred);
  3016. }
  3017. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  3018. char *cmd)
  3019. {
  3020. int id;
  3021. struct wpa_cred *cred;
  3022. char *name, *value;
  3023. /* cmd: "<cred id> <variable name> <value>" */
  3024. name = os_strchr(cmd, ' ');
  3025. if (name == NULL)
  3026. return -1;
  3027. *name++ = '\0';
  3028. value = os_strchr(name, ' ');
  3029. if (value == NULL)
  3030. return -1;
  3031. *value++ = '\0';
  3032. id = atoi(cmd);
  3033. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  3034. id, name);
  3035. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  3036. (u8 *) value, os_strlen(value));
  3037. cred = wpa_config_get_cred(wpa_s->conf, id);
  3038. if (cred == NULL) {
  3039. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  3040. id);
  3041. return -1;
  3042. }
  3043. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  3044. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  3045. "variable '%s'", name);
  3046. return -1;
  3047. }
  3048. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  3049. return 0;
  3050. }
  3051. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  3052. char *cmd, char *buf,
  3053. size_t buflen)
  3054. {
  3055. int id;
  3056. size_t res;
  3057. struct wpa_cred *cred;
  3058. char *name, *value;
  3059. /* cmd: "<cred id> <variable name>" */
  3060. name = os_strchr(cmd, ' ');
  3061. if (name == NULL)
  3062. return -1;
  3063. *name++ = '\0';
  3064. id = atoi(cmd);
  3065. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  3066. id, name);
  3067. cred = wpa_config_get_cred(wpa_s->conf, id);
  3068. if (cred == NULL) {
  3069. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  3070. id);
  3071. return -1;
  3072. }
  3073. value = wpa_config_get_cred_no_key(cred, name);
  3074. if (value == NULL) {
  3075. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  3076. name);
  3077. return -1;
  3078. }
  3079. res = os_strlcpy(buf, value, buflen);
  3080. if (res >= buflen) {
  3081. os_free(value);
  3082. return -1;
  3083. }
  3084. os_free(value);
  3085. return res;
  3086. }
  3087. #ifndef CONFIG_NO_CONFIG_WRITE
  3088. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  3089. {
  3090. int ret;
  3091. if (!wpa_s->conf->update_config) {
  3092. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  3093. "to update configuration (update_config=0)");
  3094. return -1;
  3095. }
  3096. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  3097. if (ret) {
  3098. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  3099. "update configuration");
  3100. } else {
  3101. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  3102. " updated");
  3103. }
  3104. return ret;
  3105. }
  3106. #endif /* CONFIG_NO_CONFIG_WRITE */
  3107. struct cipher_info {
  3108. unsigned int capa;
  3109. const char *name;
  3110. int group_only;
  3111. };
  3112. static const struct cipher_info ciphers[] = {
  3113. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  3114. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  3115. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  3116. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  3117. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  3118. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  3119. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  3120. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  3121. };
  3122. static const struct cipher_info ciphers_group_mgmt[] = {
  3123. { WPA_DRIVER_CAPA_ENC_BIP, "AES-128-CMAC", 1 },
  3124. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_128, "BIP-GMAC-128", 1 },
  3125. { WPA_DRIVER_CAPA_ENC_BIP_GMAC_256, "BIP-GMAC-256", 1 },
  3126. { WPA_DRIVER_CAPA_ENC_BIP_CMAC_256, "BIP-CMAC-256", 1 },
  3127. };
  3128. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  3129. struct wpa_driver_capa *capa,
  3130. char *buf, size_t buflen)
  3131. {
  3132. int ret;
  3133. char *pos, *end;
  3134. size_t len;
  3135. unsigned int i;
  3136. pos = buf;
  3137. end = pos + buflen;
  3138. if (res < 0) {
  3139. if (strict)
  3140. return 0;
  3141. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  3142. if (len >= buflen)
  3143. return -1;
  3144. return len;
  3145. }
  3146. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  3147. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  3148. ret = os_snprintf(pos, end - pos, "%s%s",
  3149. pos == buf ? "" : " ",
  3150. ciphers[i].name);
  3151. if (os_snprintf_error(end - pos, ret))
  3152. return pos - buf;
  3153. pos += ret;
  3154. }
  3155. }
  3156. return pos - buf;
  3157. }
  3158. static int ctrl_iface_get_capability_group(int res, char *strict,
  3159. struct wpa_driver_capa *capa,
  3160. char *buf, size_t buflen)
  3161. {
  3162. int ret;
  3163. char *pos, *end;
  3164. size_t len;
  3165. unsigned int i;
  3166. pos = buf;
  3167. end = pos + buflen;
  3168. if (res < 0) {
  3169. if (strict)
  3170. return 0;
  3171. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  3172. if (len >= buflen)
  3173. return -1;
  3174. return len;
  3175. }
  3176. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  3177. if (capa->enc & ciphers[i].capa) {
  3178. ret = os_snprintf(pos, end - pos, "%s%s",
  3179. pos == buf ? "" : " ",
  3180. ciphers[i].name);
  3181. if (os_snprintf_error(end - pos, ret))
  3182. return pos - buf;
  3183. pos += ret;
  3184. }
  3185. }
  3186. return pos - buf;
  3187. }
  3188. static int ctrl_iface_get_capability_group_mgmt(int res, char *strict,
  3189. struct wpa_driver_capa *capa,
  3190. char *buf, size_t buflen)
  3191. {
  3192. int ret;
  3193. char *pos, *end;
  3194. unsigned int i;
  3195. pos = buf;
  3196. end = pos + buflen;
  3197. if (res < 0)
  3198. return 0;
  3199. for (i = 0; i < ARRAY_SIZE(ciphers_group_mgmt); i++) {
  3200. if (capa->enc & ciphers_group_mgmt[i].capa) {
  3201. ret = os_snprintf(pos, end - pos, "%s%s",
  3202. pos == buf ? "" : " ",
  3203. ciphers_group_mgmt[i].name);
  3204. if (os_snprintf_error(end - pos, ret))
  3205. return pos - buf;
  3206. pos += ret;
  3207. }
  3208. }
  3209. return pos - buf;
  3210. }
  3211. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  3212. struct wpa_driver_capa *capa,
  3213. char *buf, size_t buflen)
  3214. {
  3215. int ret;
  3216. char *pos, *end;
  3217. size_t len;
  3218. pos = buf;
  3219. end = pos + buflen;
  3220. if (res < 0) {
  3221. if (strict)
  3222. return 0;
  3223. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  3224. "NONE", buflen);
  3225. if (len >= buflen)
  3226. return -1;
  3227. return len;
  3228. }
  3229. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  3230. if (os_snprintf_error(end - pos, ret))
  3231. return pos - buf;
  3232. pos += ret;
  3233. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3234. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  3235. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  3236. if (os_snprintf_error(end - pos, ret))
  3237. return pos - buf;
  3238. pos += ret;
  3239. }
  3240. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  3241. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3242. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  3243. if (os_snprintf_error(end - pos, ret))
  3244. return pos - buf;
  3245. pos += ret;
  3246. }
  3247. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  3248. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  3249. if (os_snprintf_error(end - pos, ret))
  3250. return pos - buf;
  3251. pos += ret;
  3252. }
  3253. #ifdef CONFIG_SUITEB
  3254. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B) {
  3255. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B");
  3256. if (os_snprintf_error(end - pos, ret))
  3257. return pos - buf;
  3258. pos += ret;
  3259. }
  3260. #endif /* CONFIG_SUITEB */
  3261. #ifdef CONFIG_SUITEB192
  3262. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_SUITE_B_192) {
  3263. ret = os_snprintf(pos, end - pos, " WPA-EAP-SUITE-B-192");
  3264. if (os_snprintf_error(end - pos, ret))
  3265. return pos - buf;
  3266. pos += ret;
  3267. }
  3268. #endif /* CONFIG_SUITEB192 */
  3269. #ifdef CONFIG_OWE
  3270. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_OWE) {
  3271. ret = os_snprintf(pos, end - pos, " OWE");
  3272. if (os_snprintf_error(end - pos, ret))
  3273. return pos - buf;
  3274. pos += ret;
  3275. }
  3276. #endif /* CONFIG_OWE */
  3277. #ifdef CONFIG_DPP
  3278. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_DPP) {
  3279. ret = os_snprintf(pos, end - pos, " DPP");
  3280. if (os_snprintf_error(end - pos, ret))
  3281. return pos - buf;
  3282. pos += ret;
  3283. }
  3284. #endif /* CONFIG_DPP */
  3285. return pos - buf;
  3286. }
  3287. static int ctrl_iface_get_capability_proto(int res, char *strict,
  3288. struct wpa_driver_capa *capa,
  3289. char *buf, size_t buflen)
  3290. {
  3291. int ret;
  3292. char *pos, *end;
  3293. size_t len;
  3294. pos = buf;
  3295. end = pos + buflen;
  3296. if (res < 0) {
  3297. if (strict)
  3298. return 0;
  3299. len = os_strlcpy(buf, "RSN WPA", buflen);
  3300. if (len >= buflen)
  3301. return -1;
  3302. return len;
  3303. }
  3304. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  3305. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  3306. ret = os_snprintf(pos, end - pos, "%sRSN",
  3307. pos == buf ? "" : " ");
  3308. if (os_snprintf_error(end - pos, ret))
  3309. return pos - buf;
  3310. pos += ret;
  3311. }
  3312. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  3313. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  3314. ret = os_snprintf(pos, end - pos, "%sWPA",
  3315. pos == buf ? "" : " ");
  3316. if (os_snprintf_error(end - pos, ret))
  3317. return pos - buf;
  3318. pos += ret;
  3319. }
  3320. return pos - buf;
  3321. }
  3322. static int ctrl_iface_get_capability_auth_alg(struct wpa_supplicant *wpa_s,
  3323. int res, char *strict,
  3324. struct wpa_driver_capa *capa,
  3325. char *buf, size_t buflen)
  3326. {
  3327. int ret;
  3328. char *pos, *end;
  3329. size_t len;
  3330. pos = buf;
  3331. end = pos + buflen;
  3332. if (res < 0) {
  3333. if (strict)
  3334. return 0;
  3335. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  3336. if (len >= buflen)
  3337. return -1;
  3338. return len;
  3339. }
  3340. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  3341. ret = os_snprintf(pos, end - pos, "%sOPEN",
  3342. pos == buf ? "" : " ");
  3343. if (os_snprintf_error(end - pos, ret))
  3344. return pos - buf;
  3345. pos += ret;
  3346. }
  3347. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  3348. ret = os_snprintf(pos, end - pos, "%sSHARED",
  3349. pos == buf ? "" : " ");
  3350. if (os_snprintf_error(end - pos, ret))
  3351. return pos - buf;
  3352. pos += ret;
  3353. }
  3354. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  3355. ret = os_snprintf(pos, end - pos, "%sLEAP",
  3356. pos == buf ? "" : " ");
  3357. if (os_snprintf_error(end - pos, ret))
  3358. return pos - buf;
  3359. pos += ret;
  3360. }
  3361. #ifdef CONFIG_SAE
  3362. if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SAE) {
  3363. ret = os_snprintf(pos, end - pos, "%sSAE",
  3364. pos == buf ? "" : " ");
  3365. if (os_snprintf_error(end - pos, ret))
  3366. return pos - buf;
  3367. pos += ret;
  3368. }
  3369. #endif /* CONFIG_SAE */
  3370. return pos - buf;
  3371. }
  3372. static int ctrl_iface_get_capability_modes(int res, char *strict,
  3373. struct wpa_driver_capa *capa,
  3374. char *buf, size_t buflen)
  3375. {
  3376. int ret;
  3377. char *pos, *end;
  3378. size_t len;
  3379. pos = buf;
  3380. end = pos + buflen;
  3381. if (res < 0) {
  3382. if (strict)
  3383. return 0;
  3384. len = os_strlcpy(buf, "IBSS AP", buflen);
  3385. if (len >= buflen)
  3386. return -1;
  3387. return len;
  3388. }
  3389. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  3390. ret = os_snprintf(pos, end - pos, "%sIBSS",
  3391. pos == buf ? "" : " ");
  3392. if (os_snprintf_error(end - pos, ret))
  3393. return pos - buf;
  3394. pos += ret;
  3395. }
  3396. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  3397. ret = os_snprintf(pos, end - pos, "%sAP",
  3398. pos == buf ? "" : " ");
  3399. if (os_snprintf_error(end - pos, ret))
  3400. return pos - buf;
  3401. pos += ret;
  3402. }
  3403. #ifdef CONFIG_MESH
  3404. if (capa->flags & WPA_DRIVER_FLAGS_MESH) {
  3405. ret = os_snprintf(pos, end - pos, "%sMESH",
  3406. pos == buf ? "" : " ");
  3407. if (os_snprintf_error(end - pos, ret))
  3408. return pos - buf;
  3409. pos += ret;
  3410. }
  3411. #endif /* CONFIG_MESH */
  3412. return pos - buf;
  3413. }
  3414. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  3415. char *buf, size_t buflen)
  3416. {
  3417. struct hostapd_channel_data *chnl;
  3418. int ret, i, j;
  3419. char *pos, *end, *hmode;
  3420. pos = buf;
  3421. end = pos + buflen;
  3422. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3423. switch (wpa_s->hw.modes[j].mode) {
  3424. case HOSTAPD_MODE_IEEE80211B:
  3425. hmode = "B";
  3426. break;
  3427. case HOSTAPD_MODE_IEEE80211G:
  3428. hmode = "G";
  3429. break;
  3430. case HOSTAPD_MODE_IEEE80211A:
  3431. hmode = "A";
  3432. break;
  3433. case HOSTAPD_MODE_IEEE80211AD:
  3434. hmode = "AD";
  3435. break;
  3436. default:
  3437. continue;
  3438. }
  3439. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  3440. if (os_snprintf_error(end - pos, ret))
  3441. return pos - buf;
  3442. pos += ret;
  3443. chnl = wpa_s->hw.modes[j].channels;
  3444. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3445. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3446. continue;
  3447. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  3448. if (os_snprintf_error(end - pos, ret))
  3449. return pos - buf;
  3450. pos += ret;
  3451. }
  3452. ret = os_snprintf(pos, end - pos, "\n");
  3453. if (os_snprintf_error(end - pos, ret))
  3454. return pos - buf;
  3455. pos += ret;
  3456. }
  3457. return pos - buf;
  3458. }
  3459. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  3460. char *buf, size_t buflen)
  3461. {
  3462. struct hostapd_channel_data *chnl;
  3463. int ret, i, j;
  3464. char *pos, *end, *hmode;
  3465. pos = buf;
  3466. end = pos + buflen;
  3467. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  3468. switch (wpa_s->hw.modes[j].mode) {
  3469. case HOSTAPD_MODE_IEEE80211B:
  3470. hmode = "B";
  3471. break;
  3472. case HOSTAPD_MODE_IEEE80211G:
  3473. hmode = "G";
  3474. break;
  3475. case HOSTAPD_MODE_IEEE80211A:
  3476. hmode = "A";
  3477. break;
  3478. case HOSTAPD_MODE_IEEE80211AD:
  3479. hmode = "AD";
  3480. break;
  3481. default:
  3482. continue;
  3483. }
  3484. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  3485. hmode);
  3486. if (os_snprintf_error(end - pos, ret))
  3487. return pos - buf;
  3488. pos += ret;
  3489. chnl = wpa_s->hw.modes[j].channels;
  3490. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  3491. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  3492. continue;
  3493. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  3494. chnl[i].chan, chnl[i].freq,
  3495. chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
  3496. " (NO_IR)" : "",
  3497. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  3498. " (DFS)" : "");
  3499. if (os_snprintf_error(end - pos, ret))
  3500. return pos - buf;
  3501. pos += ret;
  3502. }
  3503. ret = os_snprintf(pos, end - pos, "\n");
  3504. if (os_snprintf_error(end - pos, ret))
  3505. return pos - buf;
  3506. pos += ret;
  3507. }
  3508. return pos - buf;
  3509. }
  3510. static int wpa_supplicant_ctrl_iface_get_capability(
  3511. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  3512. size_t buflen)
  3513. {
  3514. struct wpa_driver_capa capa;
  3515. int res;
  3516. char *strict;
  3517. char field[30];
  3518. size_t len;
  3519. /* Determine whether or not strict checking was requested */
  3520. len = os_strlcpy(field, _field, sizeof(field));
  3521. if (len >= sizeof(field))
  3522. return -1;
  3523. strict = os_strchr(field, ' ');
  3524. if (strict != NULL) {
  3525. *strict++ = '\0';
  3526. if (os_strcmp(strict, "strict") != 0)
  3527. return -1;
  3528. }
  3529. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  3530. field, strict ? strict : "");
  3531. if (os_strcmp(field, "eap") == 0) {
  3532. return eap_get_names(buf, buflen);
  3533. }
  3534. res = wpa_drv_get_capa(wpa_s, &capa);
  3535. if (os_strcmp(field, "pairwise") == 0)
  3536. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  3537. buf, buflen);
  3538. if (os_strcmp(field, "group") == 0)
  3539. return ctrl_iface_get_capability_group(res, strict, &capa,
  3540. buf, buflen);
  3541. if (os_strcmp(field, "group_mgmt") == 0)
  3542. return ctrl_iface_get_capability_group_mgmt(res, strict, &capa,
  3543. buf, buflen);
  3544. if (os_strcmp(field, "key_mgmt") == 0)
  3545. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  3546. buf, buflen);
  3547. if (os_strcmp(field, "proto") == 0)
  3548. return ctrl_iface_get_capability_proto(res, strict, &capa,
  3549. buf, buflen);
  3550. if (os_strcmp(field, "auth_alg") == 0)
  3551. return ctrl_iface_get_capability_auth_alg(wpa_s, res, strict,
  3552. &capa, buf, buflen);
  3553. if (os_strcmp(field, "modes") == 0)
  3554. return ctrl_iface_get_capability_modes(res, strict, &capa,
  3555. buf, buflen);
  3556. if (os_strcmp(field, "channels") == 0)
  3557. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  3558. if (os_strcmp(field, "freq") == 0)
  3559. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  3560. #ifdef CONFIG_TDLS
  3561. if (os_strcmp(field, "tdls") == 0)
  3562. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  3563. #endif /* CONFIG_TDLS */
  3564. #ifdef CONFIG_ERP
  3565. if (os_strcmp(field, "erp") == 0) {
  3566. res = os_snprintf(buf, buflen, "ERP");
  3567. if (os_snprintf_error(buflen, res))
  3568. return -1;
  3569. return res;
  3570. }
  3571. #endif /* CONFIG_EPR */
  3572. #ifdef CONFIG_FIPS
  3573. if (os_strcmp(field, "fips") == 0) {
  3574. res = os_snprintf(buf, buflen, "FIPS");
  3575. if (os_snprintf_error(buflen, res))
  3576. return -1;
  3577. return res;
  3578. }
  3579. #endif /* CONFIG_FIPS */
  3580. #ifdef CONFIG_ACS
  3581. if (os_strcmp(field, "acs") == 0) {
  3582. res = os_snprintf(buf, buflen, "ACS");
  3583. if (os_snprintf_error(buflen, res))
  3584. return -1;
  3585. return res;
  3586. }
  3587. #endif /* CONFIG_ACS */
  3588. #ifdef CONFIG_FILS
  3589. if (os_strcmp(field, "fils") == 0 &&
  3590. (wpa_s->drv_flags & WPA_DRIVER_FLAGS_SUPPORT_FILS)) {
  3591. #ifdef CONFIG_FILS_SK_PFS
  3592. res = os_snprintf(buf, buflen, "FILS FILS-SK-PFS");
  3593. #else /* CONFIG_FILS_SK_PFS */
  3594. res = os_snprintf(buf, buflen, "FILS");
  3595. #endif /* CONFIG_FILS_SK_PFS */
  3596. if (os_snprintf_error(buflen, res))
  3597. return -1;
  3598. return res;
  3599. }
  3600. #endif /* CONFIG_FILS */
  3601. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  3602. field);
  3603. return -1;
  3604. }
  3605. #ifdef CONFIG_INTERWORKING
  3606. static char * anqp_add_hex(char *pos, char *end, const char *title,
  3607. struct wpabuf *data)
  3608. {
  3609. char *start = pos;
  3610. size_t i;
  3611. int ret;
  3612. const u8 *d;
  3613. if (data == NULL)
  3614. return start;
  3615. ret = os_snprintf(pos, end - pos, "%s=", title);
  3616. if (os_snprintf_error(end - pos, ret))
  3617. return start;
  3618. pos += ret;
  3619. d = wpabuf_head_u8(data);
  3620. for (i = 0; i < wpabuf_len(data); i++) {
  3621. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  3622. if (os_snprintf_error(end - pos, ret))
  3623. return start;
  3624. pos += ret;
  3625. }
  3626. ret = os_snprintf(pos, end - pos, "\n");
  3627. if (os_snprintf_error(end - pos, ret))
  3628. return start;
  3629. pos += ret;
  3630. return pos;
  3631. }
  3632. #endif /* CONFIG_INTERWORKING */
  3633. #ifdef CONFIG_FILS
  3634. static int print_fils_indication(struct wpa_bss *bss, char *pos, char *end)
  3635. {
  3636. char *start = pos;
  3637. const u8 *ie, *ie_end;
  3638. u16 info, realms;
  3639. int ret;
  3640. ie = wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION);
  3641. if (!ie)
  3642. return 0;
  3643. ie_end = ie + 2 + ie[1];
  3644. ie += 2;
  3645. if (ie_end - ie < 2)
  3646. return -1;
  3647. info = WPA_GET_LE16(ie);
  3648. ie += 2;
  3649. ret = os_snprintf(pos, end - pos, "fils_info=%04x\n", info);
  3650. if (os_snprintf_error(end - pos, ret))
  3651. return 0;
  3652. pos += ret;
  3653. if (info & BIT(7)) {
  3654. /* Cache Identifier Included */
  3655. if (ie_end - ie < 2)
  3656. return -1;
  3657. ret = os_snprintf(pos, end - pos, "fils_cache_id=%02x%02x\n",
  3658. ie[0], ie[1]);
  3659. if (os_snprintf_error(end - pos, ret))
  3660. return 0;
  3661. pos += ret;
  3662. ie += 2;
  3663. }
  3664. if (info & BIT(8)) {
  3665. /* HESSID Included */
  3666. if (ie_end - ie < ETH_ALEN)
  3667. return -1;
  3668. ret = os_snprintf(pos, end - pos, "fils_hessid=" MACSTR "\n",
  3669. MAC2STR(ie));
  3670. if (os_snprintf_error(end - pos, ret))
  3671. return 0;
  3672. pos += ret;
  3673. ie += ETH_ALEN;
  3674. }
  3675. realms = (info & (BIT(3) | BIT(4) | BIT(5))) >> 3;
  3676. if (realms) {
  3677. if (ie_end - ie < realms * 2)
  3678. return -1;
  3679. ret = os_snprintf(pos, end - pos, "fils_realms=");
  3680. if (os_snprintf_error(end - pos, ret))
  3681. return 0;
  3682. pos += ret;
  3683. ret = wpa_snprintf_hex(pos, end - pos, ie, realms * 2);
  3684. if (ret <= 0)
  3685. return 0;
  3686. pos += ret;
  3687. ie += realms * 2;
  3688. ret = os_snprintf(pos, end - pos, "\n");
  3689. if (os_snprintf_error(end - pos, ret))
  3690. return 0;
  3691. pos += ret;
  3692. }
  3693. return pos - start;
  3694. }
  3695. #endif /* CONFIG_FILS */
  3696. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  3697. unsigned long mask, char *buf, size_t buflen)
  3698. {
  3699. size_t i;
  3700. int ret;
  3701. char *pos, *end;
  3702. const u8 *ie, *ie2, *osen_ie, *mesh;
  3703. pos = buf;
  3704. end = buf + buflen;
  3705. if (mask & WPA_BSS_MASK_ID) {
  3706. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  3707. if (os_snprintf_error(end - pos, ret))
  3708. return 0;
  3709. pos += ret;
  3710. }
  3711. if (mask & WPA_BSS_MASK_BSSID) {
  3712. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  3713. MAC2STR(bss->bssid));
  3714. if (os_snprintf_error(end - pos, ret))
  3715. return 0;
  3716. pos += ret;
  3717. }
  3718. if (mask & WPA_BSS_MASK_FREQ) {
  3719. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  3720. if (os_snprintf_error(end - pos, ret))
  3721. return 0;
  3722. pos += ret;
  3723. }
  3724. if (mask & WPA_BSS_MASK_BEACON_INT) {
  3725. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  3726. bss->beacon_int);
  3727. if (os_snprintf_error(end - pos, ret))
  3728. return 0;
  3729. pos += ret;
  3730. }
  3731. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  3732. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  3733. bss->caps);
  3734. if (os_snprintf_error(end - pos, ret))
  3735. return 0;
  3736. pos += ret;
  3737. }
  3738. if (mask & WPA_BSS_MASK_QUAL) {
  3739. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  3740. if (os_snprintf_error(end - pos, ret))
  3741. return 0;
  3742. pos += ret;
  3743. }
  3744. if (mask & WPA_BSS_MASK_NOISE) {
  3745. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  3746. if (os_snprintf_error(end - pos, ret))
  3747. return 0;
  3748. pos += ret;
  3749. }
  3750. if (mask & WPA_BSS_MASK_LEVEL) {
  3751. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  3752. if (os_snprintf_error(end - pos, ret))
  3753. return 0;
  3754. pos += ret;
  3755. }
  3756. if (mask & WPA_BSS_MASK_TSF) {
  3757. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  3758. (unsigned long long) bss->tsf);
  3759. if (os_snprintf_error(end - pos, ret))
  3760. return 0;
  3761. pos += ret;
  3762. }
  3763. if (mask & WPA_BSS_MASK_AGE) {
  3764. struct os_reltime now;
  3765. os_get_reltime(&now);
  3766. ret = os_snprintf(pos, end - pos, "age=%d\n",
  3767. (int) (now.sec - bss->last_update.sec));
  3768. if (os_snprintf_error(end - pos, ret))
  3769. return 0;
  3770. pos += ret;
  3771. }
  3772. if (mask & WPA_BSS_MASK_IE) {
  3773. ret = os_snprintf(pos, end - pos, "ie=");
  3774. if (os_snprintf_error(end - pos, ret))
  3775. return 0;
  3776. pos += ret;
  3777. ie = (const u8 *) (bss + 1);
  3778. for (i = 0; i < bss->ie_len; i++) {
  3779. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  3780. if (os_snprintf_error(end - pos, ret))
  3781. return 0;
  3782. pos += ret;
  3783. }
  3784. ret = os_snprintf(pos, end - pos, "\n");
  3785. if (os_snprintf_error(end - pos, ret))
  3786. return 0;
  3787. pos += ret;
  3788. }
  3789. if (mask & WPA_BSS_MASK_FLAGS) {
  3790. ret = os_snprintf(pos, end - pos, "flags=");
  3791. if (os_snprintf_error(end - pos, ret))
  3792. return 0;
  3793. pos += ret;
  3794. mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
  3795. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3796. if (ie)
  3797. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3798. 2 + ie[1]);
  3799. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3800. if (ie2)
  3801. pos = wpa_supplicant_ie_txt(pos, end,
  3802. mesh ? "RSN" : "WPA2", ie2,
  3803. 2 + ie2[1]);
  3804. osen_ie = wpa_bss_get_vendor_ie(bss, OSEN_IE_VENDOR_TYPE);
  3805. if (osen_ie)
  3806. pos = wpa_supplicant_ie_txt(pos, end, "OSEN",
  3807. osen_ie, 2 + osen_ie[1]);
  3808. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3809. if (!ie && !ie2 && !osen_ie &&
  3810. (bss->caps & IEEE80211_CAP_PRIVACY)) {
  3811. ret = os_snprintf(pos, end - pos, "[WEP]");
  3812. if (os_snprintf_error(end - pos, ret))
  3813. return 0;
  3814. pos += ret;
  3815. }
  3816. if (mesh) {
  3817. ret = os_snprintf(pos, end - pos, "[MESH]");
  3818. if (os_snprintf_error(end - pos, ret))
  3819. return 0;
  3820. pos += ret;
  3821. }
  3822. if (bss_is_dmg(bss)) {
  3823. const char *s;
  3824. ret = os_snprintf(pos, end - pos, "[DMG]");
  3825. if (os_snprintf_error(end - pos, ret))
  3826. return 0;
  3827. pos += ret;
  3828. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3829. case IEEE80211_CAP_DMG_IBSS:
  3830. s = "[IBSS]";
  3831. break;
  3832. case IEEE80211_CAP_DMG_AP:
  3833. s = "[ESS]";
  3834. break;
  3835. case IEEE80211_CAP_DMG_PBSS:
  3836. s = "[PBSS]";
  3837. break;
  3838. default:
  3839. s = "";
  3840. break;
  3841. }
  3842. ret = os_snprintf(pos, end - pos, "%s", s);
  3843. if (os_snprintf_error(end - pos, ret))
  3844. return 0;
  3845. pos += ret;
  3846. } else {
  3847. if (bss->caps & IEEE80211_CAP_IBSS) {
  3848. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3849. if (os_snprintf_error(end - pos, ret))
  3850. return 0;
  3851. pos += ret;
  3852. }
  3853. if (bss->caps & IEEE80211_CAP_ESS) {
  3854. ret = os_snprintf(pos, end - pos, "[ESS]");
  3855. if (os_snprintf_error(end - pos, ret))
  3856. return 0;
  3857. pos += ret;
  3858. }
  3859. }
  3860. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  3861. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  3862. ret = os_snprintf(pos, end - pos, "[P2P]");
  3863. if (os_snprintf_error(end - pos, ret))
  3864. return 0;
  3865. pos += ret;
  3866. }
  3867. #ifdef CONFIG_HS20
  3868. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  3869. ret = os_snprintf(pos, end - pos, "[HS20]");
  3870. if (os_snprintf_error(end - pos, ret))
  3871. return 0;
  3872. pos += ret;
  3873. }
  3874. #endif /* CONFIG_HS20 */
  3875. #ifdef CONFIG_FILS
  3876. if (wpa_bss_get_ie(bss, WLAN_EID_FILS_INDICATION)) {
  3877. ret = os_snprintf(pos, end - pos, "[FILS]");
  3878. if (os_snprintf_error(end - pos, ret))
  3879. return 0;
  3880. pos += ret;
  3881. }
  3882. #endif /* CONFIG_FILS */
  3883. ret = os_snprintf(pos, end - pos, "\n");
  3884. if (os_snprintf_error(end - pos, ret))
  3885. return 0;
  3886. pos += ret;
  3887. }
  3888. if (mask & WPA_BSS_MASK_SSID) {
  3889. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  3890. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3891. if (os_snprintf_error(end - pos, ret))
  3892. return 0;
  3893. pos += ret;
  3894. }
  3895. #ifdef CONFIG_WPS
  3896. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  3897. ie = (const u8 *) (bss + 1);
  3898. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  3899. if (ret >= end - pos)
  3900. return 0;
  3901. if (ret > 0)
  3902. pos += ret;
  3903. }
  3904. #endif /* CONFIG_WPS */
  3905. #ifdef CONFIG_P2P
  3906. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  3907. ie = (const u8 *) (bss + 1);
  3908. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  3909. if (ret >= end - pos)
  3910. return 0;
  3911. if (ret > 0)
  3912. pos += ret;
  3913. }
  3914. #endif /* CONFIG_P2P */
  3915. #ifdef CONFIG_WIFI_DISPLAY
  3916. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  3917. struct wpabuf *wfd;
  3918. ie = (const u8 *) (bss + 1);
  3919. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  3920. WFD_IE_VENDOR_TYPE);
  3921. if (wfd) {
  3922. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  3923. if (os_snprintf_error(end - pos, ret)) {
  3924. wpabuf_free(wfd);
  3925. return 0;
  3926. }
  3927. pos += ret;
  3928. pos += wpa_snprintf_hex(pos, end - pos,
  3929. wpabuf_head(wfd),
  3930. wpabuf_len(wfd));
  3931. wpabuf_free(wfd);
  3932. ret = os_snprintf(pos, end - pos, "\n");
  3933. if (os_snprintf_error(end - pos, ret))
  3934. return 0;
  3935. pos += ret;
  3936. }
  3937. }
  3938. #endif /* CONFIG_WIFI_DISPLAY */
  3939. #ifdef CONFIG_INTERWORKING
  3940. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3941. struct wpa_bss_anqp *anqp = bss->anqp;
  3942. struct wpa_bss_anqp_elem *elem;
  3943. pos = anqp_add_hex(pos, end, "anqp_capability_list",
  3944. anqp->capability_list);
  3945. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3946. anqp->venue_name);
  3947. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3948. anqp->network_auth_type);
  3949. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3950. anqp->roaming_consortium);
  3951. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3952. anqp->ip_addr_type_availability);
  3953. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3954. anqp->nai_realm);
  3955. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3956. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3957. anqp->domain_name);
  3958. pos = anqp_add_hex(pos, end, "anqp_fils_realm_info",
  3959. anqp->fils_realm_info);
  3960. #ifdef CONFIG_HS20
  3961. pos = anqp_add_hex(pos, end, "hs20_capability_list",
  3962. anqp->hs20_capability_list);
  3963. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3964. anqp->hs20_operator_friendly_name);
  3965. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3966. anqp->hs20_wan_metrics);
  3967. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3968. anqp->hs20_connection_capability);
  3969. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3970. anqp->hs20_operating_class);
  3971. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3972. anqp->hs20_osu_providers_list);
  3973. #endif /* CONFIG_HS20 */
  3974. dl_list_for_each(elem, &anqp->anqp_elems,
  3975. struct wpa_bss_anqp_elem, list) {
  3976. char title[20];
  3977. os_snprintf(title, sizeof(title), "anqp[%u]",
  3978. elem->infoid);
  3979. pos = anqp_add_hex(pos, end, title, elem->payload);
  3980. }
  3981. }
  3982. #endif /* CONFIG_INTERWORKING */
  3983. #ifdef CONFIG_MESH
  3984. if (mask & WPA_BSS_MASK_MESH_SCAN) {
  3985. ie = (const u8 *) (bss + 1);
  3986. ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
  3987. if (ret >= end - pos)
  3988. return 0;
  3989. if (ret > 0)
  3990. pos += ret;
  3991. }
  3992. #endif /* CONFIG_MESH */
  3993. if (mask & WPA_BSS_MASK_SNR) {
  3994. ret = os_snprintf(pos, end - pos, "snr=%d\n", bss->snr);
  3995. if (os_snprintf_error(end - pos, ret))
  3996. return 0;
  3997. pos += ret;
  3998. }
  3999. if (mask & WPA_BSS_MASK_EST_THROUGHPUT) {
  4000. ret = os_snprintf(pos, end - pos, "est_throughput=%d\n",
  4001. bss->est_throughput);
  4002. if (os_snprintf_error(end - pos, ret))
  4003. return 0;
  4004. pos += ret;
  4005. }
  4006. #ifdef CONFIG_FST
  4007. if (mask & WPA_BSS_MASK_FST) {
  4008. ret = fst_ctrl_iface_mb_info(bss->bssid, pos, end - pos);
  4009. if (ret < 0 || ret >= end - pos)
  4010. return 0;
  4011. pos += ret;
  4012. }
  4013. #endif /* CONFIG_FST */
  4014. if (mask & WPA_BSS_MASK_UPDATE_IDX) {
  4015. ret = os_snprintf(pos, end - pos, "update_idx=%u\n",
  4016. bss->last_update_idx);
  4017. if (os_snprintf_error(end - pos, ret))
  4018. return 0;
  4019. pos += ret;
  4020. }
  4021. if ((mask & WPA_BSS_MASK_BEACON_IE) && bss->beacon_ie_len) {
  4022. ret = os_snprintf(pos, end - pos, "beacon_ie=");
  4023. if (os_snprintf_error(end - pos, ret))
  4024. return 0;
  4025. pos += ret;
  4026. ie = (const u8 *) (bss + 1);
  4027. ie += bss->ie_len;
  4028. for (i = 0; i < bss->beacon_ie_len; i++) {
  4029. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  4030. if (os_snprintf_error(end - pos, ret))
  4031. return 0;
  4032. pos += ret;
  4033. }
  4034. ret = os_snprintf(pos, end - pos, "\n");
  4035. if (os_snprintf_error(end - pos, ret))
  4036. return 0;
  4037. pos += ret;
  4038. }
  4039. #ifdef CONFIG_FILS
  4040. if (mask & WPA_BSS_MASK_FILS_INDICATION) {
  4041. ret = print_fils_indication(bss, pos, end);
  4042. if (ret < 0)
  4043. return 0;
  4044. pos += ret;
  4045. }
  4046. #endif /* CONFIG_FILS */
  4047. if (mask & WPA_BSS_MASK_DELIM) {
  4048. ret = os_snprintf(pos, end - pos, "====\n");
  4049. if (os_snprintf_error(end - pos, ret))
  4050. return 0;
  4051. pos += ret;
  4052. }
  4053. return pos - buf;
  4054. }
  4055. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  4056. const char *cmd, char *buf,
  4057. size_t buflen)
  4058. {
  4059. u8 bssid[ETH_ALEN];
  4060. size_t i;
  4061. struct wpa_bss *bss;
  4062. struct wpa_bss *bsslast = NULL;
  4063. struct dl_list *next;
  4064. int ret = 0;
  4065. int len;
  4066. char *ctmp, *end = buf + buflen;
  4067. unsigned long mask = WPA_BSS_MASK_ALL;
  4068. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  4069. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  4070. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  4071. list_id);
  4072. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  4073. list_id);
  4074. } else { /* N1-N2 */
  4075. unsigned int id1, id2;
  4076. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  4077. wpa_printf(MSG_INFO, "Wrong BSS range "
  4078. "format");
  4079. return 0;
  4080. }
  4081. if (*(cmd + 6) == '-')
  4082. id1 = 0;
  4083. else
  4084. id1 = atoi(cmd + 6);
  4085. ctmp++;
  4086. if (*ctmp >= '0' && *ctmp <= '9')
  4087. id2 = atoi(ctmp);
  4088. else
  4089. id2 = (unsigned int) -1;
  4090. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  4091. if (id2 == (unsigned int) -1)
  4092. bsslast = dl_list_last(&wpa_s->bss_id,
  4093. struct wpa_bss,
  4094. list_id);
  4095. else {
  4096. bsslast = wpa_bss_get_id(wpa_s, id2);
  4097. if (bsslast == NULL && bss && id2 > id1) {
  4098. struct wpa_bss *tmp = bss;
  4099. for (;;) {
  4100. next = tmp->list_id.next;
  4101. if (next == &wpa_s->bss_id)
  4102. break;
  4103. tmp = dl_list_entry(
  4104. next, struct wpa_bss,
  4105. list_id);
  4106. if (tmp->id > id2)
  4107. break;
  4108. bsslast = tmp;
  4109. }
  4110. }
  4111. }
  4112. }
  4113. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  4114. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  4115. else if (os_strncmp(cmd, "LAST", 4) == 0)
  4116. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  4117. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  4118. i = atoi(cmd + 3);
  4119. bss = wpa_bss_get_id(wpa_s, i);
  4120. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  4121. i = atoi(cmd + 5);
  4122. bss = wpa_bss_get_id(wpa_s, i);
  4123. if (bss) {
  4124. next = bss->list_id.next;
  4125. if (next == &wpa_s->bss_id)
  4126. bss = NULL;
  4127. else
  4128. bss = dl_list_entry(next, struct wpa_bss,
  4129. list_id);
  4130. }
  4131. } else if (os_strncmp(cmd, "CURRENT", 7) == 0) {
  4132. bss = wpa_s->current_bss;
  4133. #ifdef CONFIG_P2P
  4134. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  4135. if (hwaddr_aton(cmd + 13, bssid) == 0)
  4136. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  4137. else
  4138. bss = NULL;
  4139. #endif /* CONFIG_P2P */
  4140. } else if (hwaddr_aton(cmd, bssid) == 0)
  4141. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4142. else {
  4143. struct wpa_bss *tmp;
  4144. i = atoi(cmd);
  4145. bss = NULL;
  4146. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  4147. {
  4148. if (i-- == 0) {
  4149. bss = tmp;
  4150. break;
  4151. }
  4152. }
  4153. }
  4154. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  4155. mask = strtoul(ctmp + 5, NULL, 0x10);
  4156. if (mask == 0)
  4157. mask = WPA_BSS_MASK_ALL;
  4158. }
  4159. if (bss == NULL)
  4160. return 0;
  4161. if (bsslast == NULL)
  4162. bsslast = bss;
  4163. do {
  4164. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  4165. ret += len;
  4166. buf += len;
  4167. buflen -= len;
  4168. if (bss == bsslast) {
  4169. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  4170. (bss == dl_list_last(&wpa_s->bss_id,
  4171. struct wpa_bss, list_id))) {
  4172. int res;
  4173. res = os_snprintf(buf - 5, end - buf + 5,
  4174. "####\n");
  4175. if (os_snprintf_error(end - buf + 5, res)) {
  4176. wpa_printf(MSG_DEBUG,
  4177. "Could not add end delim");
  4178. }
  4179. }
  4180. break;
  4181. }
  4182. next = bss->list_id.next;
  4183. if (next == &wpa_s->bss_id)
  4184. break;
  4185. bss = dl_list_entry(next, struct wpa_bss, list_id);
  4186. } while (bss && len);
  4187. return ret;
  4188. }
  4189. static int wpa_supplicant_ctrl_iface_ap_scan(
  4190. struct wpa_supplicant *wpa_s, char *cmd)
  4191. {
  4192. int ap_scan = atoi(cmd);
  4193. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  4194. }
  4195. static int wpa_supplicant_ctrl_iface_scan_interval(
  4196. struct wpa_supplicant *wpa_s, char *cmd)
  4197. {
  4198. int scan_int = atoi(cmd);
  4199. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  4200. }
  4201. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  4202. struct wpa_supplicant *wpa_s, char *cmd)
  4203. {
  4204. int expire_age = atoi(cmd);
  4205. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  4206. }
  4207. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  4208. struct wpa_supplicant *wpa_s, char *cmd)
  4209. {
  4210. int expire_count = atoi(cmd);
  4211. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  4212. }
  4213. static void wpa_supplicant_ctrl_iface_bss_flush(
  4214. struct wpa_supplicant *wpa_s, char *cmd)
  4215. {
  4216. int flush_age = atoi(cmd);
  4217. if (flush_age == 0)
  4218. wpa_bss_flush(wpa_s);
  4219. else
  4220. wpa_bss_flush_by_age(wpa_s, flush_age);
  4221. }
  4222. #ifdef CONFIG_TESTING_OPTIONS
  4223. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  4224. {
  4225. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  4226. /* MLME-DELETEKEYS.request */
  4227. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  4228. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  4229. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  4230. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  4231. #ifdef CONFIG_IEEE80211W
  4232. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  4233. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  4234. #endif /* CONFIG_IEEE80211W */
  4235. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  4236. 0);
  4237. /* MLME-SETPROTECTION.request(None) */
  4238. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  4239. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  4240. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  4241. wpa_sm_drop_sa(wpa_s->wpa);
  4242. }
  4243. #endif /* CONFIG_TESTING_OPTIONS */
  4244. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  4245. char *addr)
  4246. {
  4247. #ifdef CONFIG_NO_SCAN_PROCESSING
  4248. return -1;
  4249. #else /* CONFIG_NO_SCAN_PROCESSING */
  4250. u8 bssid[ETH_ALEN];
  4251. struct wpa_bss *bss;
  4252. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4253. if (hwaddr_aton(addr, bssid)) {
  4254. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  4255. "address '%s'", addr);
  4256. return -1;
  4257. }
  4258. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  4259. if (!ssid) {
  4260. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  4261. "configuration known for the target AP");
  4262. return -1;
  4263. }
  4264. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  4265. if (!bss) {
  4266. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  4267. "from BSS table");
  4268. return -1;
  4269. }
  4270. /*
  4271. * TODO: Find best network configuration block from configuration to
  4272. * allow roaming to other networks
  4273. */
  4274. wpa_s->reassociate = 1;
  4275. wpa_supplicant_connect(wpa_s, bss, ssid);
  4276. return 0;
  4277. #endif /* CONFIG_NO_SCAN_PROCESSING */
  4278. }
  4279. #ifdef CONFIG_P2P
  4280. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  4281. {
  4282. unsigned int timeout = atoi(cmd);
  4283. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  4284. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  4285. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  4286. char *pos;
  4287. unsigned int search_delay;
  4288. const char *_seek[P2P_MAX_QUERY_HASH + 1], **seek = NULL;
  4289. u8 seek_count = 0;
  4290. int freq = 0;
  4291. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4292. wpa_dbg(wpa_s, MSG_INFO,
  4293. "Reject P2P_FIND since interface is disabled");
  4294. return -1;
  4295. }
  4296. if (os_strstr(cmd, "type=social"))
  4297. type = P2P_FIND_ONLY_SOCIAL;
  4298. else if (os_strstr(cmd, "type=progressive"))
  4299. type = P2P_FIND_PROGRESSIVE;
  4300. pos = os_strstr(cmd, "dev_id=");
  4301. if (pos) {
  4302. pos += 7;
  4303. if (hwaddr_aton(pos, dev_id))
  4304. return -1;
  4305. _dev_id = dev_id;
  4306. }
  4307. pos = os_strstr(cmd, "dev_type=");
  4308. if (pos) {
  4309. pos += 9;
  4310. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  4311. return -1;
  4312. _dev_type = dev_type;
  4313. }
  4314. pos = os_strstr(cmd, "delay=");
  4315. if (pos) {
  4316. pos += 6;
  4317. search_delay = atoi(pos);
  4318. } else
  4319. search_delay = wpas_p2p_search_delay(wpa_s);
  4320. pos = os_strstr(cmd, "freq=");
  4321. if (pos) {
  4322. pos += 5;
  4323. freq = atoi(pos);
  4324. if (freq <= 0)
  4325. return -1;
  4326. }
  4327. /* Must be searched for last, because it adds nul termination */
  4328. pos = os_strstr(cmd, " seek=");
  4329. if (pos)
  4330. pos += 6;
  4331. while (pos && seek_count < P2P_MAX_QUERY_HASH + 1) {
  4332. char *term;
  4333. _seek[seek_count++] = pos;
  4334. seek = _seek;
  4335. term = os_strchr(pos, ' ');
  4336. if (!term)
  4337. break;
  4338. *term = '\0';
  4339. pos = os_strstr(term + 1, "seek=");
  4340. if (pos)
  4341. pos += 5;
  4342. }
  4343. if (seek_count > P2P_MAX_QUERY_HASH) {
  4344. seek[0] = NULL;
  4345. seek_count = 1;
  4346. }
  4347. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  4348. _dev_id, search_delay, seek_count, seek, freq);
  4349. }
  4350. static int p2ps_ctrl_parse_cpt_priority(const char *pos, u8 *cpt)
  4351. {
  4352. const char *last = NULL;
  4353. const char *token;
  4354. long int token_len;
  4355. unsigned int i;
  4356. /* Expected predefined CPT names delimited by ':' */
  4357. for (i = 0; (token = cstr_token(pos, ": \t", &last)); i++) {
  4358. if (i >= P2PS_FEATURE_CAPAB_CPT_MAX) {
  4359. wpa_printf(MSG_ERROR,
  4360. "P2PS: CPT name list is too long, expected up to %d names",
  4361. P2PS_FEATURE_CAPAB_CPT_MAX);
  4362. cpt[0] = 0;
  4363. return -1;
  4364. }
  4365. token_len = last - token;
  4366. if (token_len == 3 &&
  4367. os_memcmp(token, "UDP", token_len) == 0) {
  4368. cpt[i] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4369. } else if (token_len == 3 &&
  4370. os_memcmp(token, "MAC", token_len) == 0) {
  4371. cpt[i] = P2PS_FEATURE_CAPAB_MAC_TRANSPORT;
  4372. } else {
  4373. wpa_printf(MSG_ERROR,
  4374. "P2PS: Unsupported CPT name '%s'", token);
  4375. cpt[0] = 0;
  4376. return -1;
  4377. }
  4378. if (isblank((unsigned char) *last)) {
  4379. i++;
  4380. break;
  4381. }
  4382. }
  4383. cpt[i] = 0;
  4384. return 0;
  4385. }
  4386. static struct p2ps_provision * p2p_parse_asp_provision_cmd(const char *cmd)
  4387. {
  4388. struct p2ps_provision *p2ps_prov;
  4389. char *pos;
  4390. size_t info_len = 0;
  4391. char *info = NULL;
  4392. u8 role = P2PS_SETUP_NONE;
  4393. long long unsigned val;
  4394. int i;
  4395. pos = os_strstr(cmd, "info=");
  4396. if (pos) {
  4397. pos += 5;
  4398. info_len = os_strlen(pos);
  4399. if (info_len) {
  4400. info = os_malloc(info_len + 1);
  4401. if (info) {
  4402. info_len = utf8_unescape(pos, info_len,
  4403. info, info_len + 1);
  4404. } else
  4405. info_len = 0;
  4406. }
  4407. }
  4408. p2ps_prov = os_zalloc(sizeof(struct p2ps_provision) + info_len + 1);
  4409. if (p2ps_prov == NULL) {
  4410. os_free(info);
  4411. return NULL;
  4412. }
  4413. if (info) {
  4414. os_memcpy(p2ps_prov->info, info, info_len);
  4415. p2ps_prov->info[info_len] = '\0';
  4416. os_free(info);
  4417. }
  4418. pos = os_strstr(cmd, "status=");
  4419. if (pos)
  4420. p2ps_prov->status = atoi(pos + 7);
  4421. else
  4422. p2ps_prov->status = -1;
  4423. pos = os_strstr(cmd, "adv_id=");
  4424. if (!pos || sscanf(pos + 7, "%llx", &val) != 1 || val > 0xffffffffULL)
  4425. goto invalid_args;
  4426. p2ps_prov->adv_id = val;
  4427. pos = os_strstr(cmd, "method=");
  4428. if (pos)
  4429. p2ps_prov->method = strtol(pos + 7, NULL, 16);
  4430. else
  4431. p2ps_prov->method = 0;
  4432. pos = os_strstr(cmd, "session=");
  4433. if (!pos || sscanf(pos + 8, "%llx", &val) != 1 || val > 0xffffffffULL)
  4434. goto invalid_args;
  4435. p2ps_prov->session_id = val;
  4436. pos = os_strstr(cmd, "adv_mac=");
  4437. if (!pos || hwaddr_aton(pos + 8, p2ps_prov->adv_mac))
  4438. goto invalid_args;
  4439. pos = os_strstr(cmd, "session_mac=");
  4440. if (!pos || hwaddr_aton(pos + 12, p2ps_prov->session_mac))
  4441. goto invalid_args;
  4442. pos = os_strstr(cmd, "cpt=");
  4443. if (pos) {
  4444. if (p2ps_ctrl_parse_cpt_priority(pos + 4,
  4445. p2ps_prov->cpt_priority))
  4446. goto invalid_args;
  4447. } else {
  4448. p2ps_prov->cpt_priority[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4449. }
  4450. for (i = 0; p2ps_prov->cpt_priority[i]; i++)
  4451. p2ps_prov->cpt_mask |= p2ps_prov->cpt_priority[i];
  4452. /* force conncap with tstCap (no sanity checks) */
  4453. pos = os_strstr(cmd, "tstCap=");
  4454. if (pos) {
  4455. role = strtol(pos + 7, NULL, 16);
  4456. } else {
  4457. pos = os_strstr(cmd, "role=");
  4458. if (pos) {
  4459. role = strtol(pos + 5, NULL, 16);
  4460. if (role != P2PS_SETUP_CLIENT &&
  4461. role != P2PS_SETUP_GROUP_OWNER)
  4462. role = P2PS_SETUP_NONE;
  4463. }
  4464. }
  4465. p2ps_prov->role = role;
  4466. return p2ps_prov;
  4467. invalid_args:
  4468. os_free(p2ps_prov);
  4469. return NULL;
  4470. }
  4471. static int p2p_ctrl_asp_provision_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4472. {
  4473. u8 addr[ETH_ALEN];
  4474. struct p2ps_provision *p2ps_prov;
  4475. char *pos;
  4476. /* <addr> id=<adv_id> [role=<conncap>] [info=<infodata>] */
  4477. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4478. if (hwaddr_aton(cmd, addr))
  4479. return -1;
  4480. pos = cmd + 17;
  4481. if (*pos != ' ')
  4482. return -1;
  4483. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4484. if (!p2ps_prov)
  4485. return -1;
  4486. if (p2ps_prov->status < 0) {
  4487. os_free(p2ps_prov);
  4488. return -1;
  4489. }
  4490. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4491. p2ps_prov);
  4492. }
  4493. static int p2p_ctrl_asp_provision(struct wpa_supplicant *wpa_s, char *cmd)
  4494. {
  4495. u8 addr[ETH_ALEN];
  4496. struct p2ps_provision *p2ps_prov;
  4497. char *pos;
  4498. /* <addr> id=<adv_id> adv_mac=<adv_mac> conncap=<conncap>
  4499. * session=<ses_id> mac=<ses_mac> [info=<infodata>]
  4500. */
  4501. wpa_printf(MSG_DEBUG, "%s: %s", __func__, cmd);
  4502. if (hwaddr_aton(cmd, addr))
  4503. return -1;
  4504. pos = cmd + 17;
  4505. if (*pos != ' ')
  4506. return -1;
  4507. p2ps_prov = p2p_parse_asp_provision_cmd(pos);
  4508. if (!p2ps_prov)
  4509. return -1;
  4510. p2ps_prov->pd_seeker = 1;
  4511. return wpas_p2p_prov_disc(wpa_s, addr, NULL, WPAS_P2P_PD_FOR_ASP,
  4512. p2ps_prov);
  4513. }
  4514. static int parse_freq(int chwidth, int freq2)
  4515. {
  4516. if (freq2 < 0)
  4517. return -1;
  4518. if (freq2)
  4519. return VHT_CHANWIDTH_80P80MHZ;
  4520. switch (chwidth) {
  4521. case 0:
  4522. case 20:
  4523. case 40:
  4524. return VHT_CHANWIDTH_USE_HT;
  4525. case 80:
  4526. return VHT_CHANWIDTH_80MHZ;
  4527. case 160:
  4528. return VHT_CHANWIDTH_160MHZ;
  4529. default:
  4530. wpa_printf(MSG_DEBUG, "Unknown max oper bandwidth: %d",
  4531. chwidth);
  4532. return -1;
  4533. }
  4534. }
  4535. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  4536. char *buf, size_t buflen)
  4537. {
  4538. u8 addr[ETH_ALEN];
  4539. char *pos, *pos2;
  4540. char *pin = NULL;
  4541. enum p2p_wps_method wps_method;
  4542. int new_pin;
  4543. int ret;
  4544. int persistent_group, persistent_id = -1;
  4545. int join;
  4546. int auth;
  4547. int automatic;
  4548. int go_intent = -1;
  4549. int freq = 0;
  4550. int pd;
  4551. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  4552. u8 _group_ssid[SSID_MAX_LEN], *group_ssid = NULL;
  4553. size_t group_ssid_len = 0;
  4554. if (!wpa_s->global->p2p_init_wpa_s)
  4555. return -1;
  4556. if (wpa_s->global->p2p_init_wpa_s != wpa_s) {
  4557. wpa_dbg(wpa_s, MSG_DEBUG, "Direct P2P_CONNECT command to %s",
  4558. wpa_s->global->p2p_init_wpa_s->ifname);
  4559. wpa_s = wpa_s->global->p2p_init_wpa_s;
  4560. }
  4561. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad|p2ps]
  4562. * [persistent|persistent=<network id>]
  4563. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  4564. * [ht40] [vht] [auto] [ssid=<hexdump>] */
  4565. if (hwaddr_aton(cmd, addr))
  4566. return -1;
  4567. pos = cmd + 17;
  4568. if (*pos != ' ')
  4569. return -1;
  4570. pos++;
  4571. persistent_group = os_strstr(pos, " persistent") != NULL;
  4572. pos2 = os_strstr(pos, " persistent=");
  4573. if (pos2) {
  4574. struct wpa_ssid *ssid;
  4575. persistent_id = atoi(pos2 + 12);
  4576. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  4577. if (ssid == NULL || ssid->disabled != 2 ||
  4578. ssid->mode != WPAS_MODE_P2P_GO) {
  4579. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  4580. "SSID id=%d for persistent P2P group (GO)",
  4581. persistent_id);
  4582. return -1;
  4583. }
  4584. }
  4585. join = os_strstr(pos, " join") != NULL;
  4586. auth = os_strstr(pos, " auth") != NULL;
  4587. automatic = os_strstr(pos, " auto") != NULL;
  4588. pd = os_strstr(pos, " provdisc") != NULL;
  4589. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4590. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4591. vht;
  4592. pos2 = os_strstr(pos, " go_intent=");
  4593. if (pos2) {
  4594. pos2 += 11;
  4595. go_intent = atoi(pos2);
  4596. if (go_intent < 0 || go_intent > 15)
  4597. return -1;
  4598. }
  4599. pos2 = os_strstr(pos, " freq=");
  4600. if (pos2) {
  4601. pos2 += 6;
  4602. freq = atoi(pos2);
  4603. if (freq <= 0)
  4604. return -1;
  4605. }
  4606. pos2 = os_strstr(pos, " freq2=");
  4607. if (pos2)
  4608. freq2 = atoi(pos2 + 7);
  4609. pos2 = os_strstr(pos, " max_oper_chwidth=");
  4610. if (pos2)
  4611. chwidth = atoi(pos2 + 18);
  4612. max_oper_chwidth = parse_freq(chwidth, freq2);
  4613. if (max_oper_chwidth < 0)
  4614. return -1;
  4615. pos2 = os_strstr(pos, " ssid=");
  4616. if (pos2) {
  4617. char *end;
  4618. pos2 += 6;
  4619. end = os_strchr(pos2, ' ');
  4620. if (!end)
  4621. group_ssid_len = os_strlen(pos2) / 2;
  4622. else
  4623. group_ssid_len = (end - pos2) / 2;
  4624. if (group_ssid_len == 0 || group_ssid_len > SSID_MAX_LEN ||
  4625. hexstr2bin(pos2, _group_ssid, group_ssid_len) < 0)
  4626. return -1;
  4627. group_ssid = _group_ssid;
  4628. }
  4629. if (os_strncmp(pos, "pin", 3) == 0) {
  4630. /* Request random PIN (to be displayed) and enable the PIN */
  4631. wps_method = WPS_PIN_DISPLAY;
  4632. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  4633. wps_method = WPS_PBC;
  4634. } else if (os_strstr(pos, "p2ps") != NULL) {
  4635. wps_method = WPS_P2PS;
  4636. } else {
  4637. pin = pos;
  4638. pos = os_strchr(pin, ' ');
  4639. wps_method = WPS_PIN_KEYPAD;
  4640. if (pos) {
  4641. *pos++ = '\0';
  4642. if (os_strncmp(pos, "display", 7) == 0)
  4643. wps_method = WPS_PIN_DISPLAY;
  4644. }
  4645. if (!wps_pin_str_valid(pin)) {
  4646. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  4647. return 17;
  4648. }
  4649. }
  4650. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  4651. persistent_group, automatic, join,
  4652. auth, go_intent, freq, freq2, persistent_id,
  4653. pd, ht40, vht, max_oper_chwidth,
  4654. group_ssid, group_ssid_len);
  4655. if (new_pin == -2) {
  4656. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  4657. return 25;
  4658. }
  4659. if (new_pin == -3) {
  4660. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  4661. return 25;
  4662. }
  4663. if (new_pin < 0)
  4664. return -1;
  4665. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  4666. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  4667. if (os_snprintf_error(buflen, ret))
  4668. return -1;
  4669. return ret;
  4670. }
  4671. os_memcpy(buf, "OK\n", 3);
  4672. return 3;
  4673. }
  4674. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4675. {
  4676. unsigned int timeout = atoi(cmd);
  4677. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4678. wpa_dbg(wpa_s, MSG_INFO,
  4679. "Reject P2P_LISTEN since interface is disabled");
  4680. return -1;
  4681. }
  4682. return wpas_p2p_listen(wpa_s, timeout);
  4683. }
  4684. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  4685. {
  4686. u8 addr[ETH_ALEN];
  4687. char *pos;
  4688. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  4689. /* <addr> <config method> [join|auto] */
  4690. if (hwaddr_aton(cmd, addr))
  4691. return -1;
  4692. pos = cmd + 17;
  4693. if (*pos != ' ')
  4694. return -1;
  4695. pos++;
  4696. if (os_strstr(pos, " join") != NULL)
  4697. use = WPAS_P2P_PD_FOR_JOIN;
  4698. else if (os_strstr(pos, " auto") != NULL)
  4699. use = WPAS_P2P_PD_AUTO;
  4700. return wpas_p2p_prov_disc(wpa_s, addr, pos, use, NULL);
  4701. }
  4702. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  4703. size_t buflen)
  4704. {
  4705. struct wpa_ssid *ssid = wpa_s->current_ssid;
  4706. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  4707. ssid->passphrase == NULL)
  4708. return -1;
  4709. os_strlcpy(buf, ssid->passphrase, buflen);
  4710. return os_strlen(buf);
  4711. }
  4712. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  4713. char *buf, size_t buflen)
  4714. {
  4715. u64 ref;
  4716. int res;
  4717. u8 dst_buf[ETH_ALEN], *dst;
  4718. struct wpabuf *tlvs;
  4719. char *pos;
  4720. size_t len;
  4721. if (hwaddr_aton(cmd, dst_buf))
  4722. return -1;
  4723. dst = dst_buf;
  4724. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  4725. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  4726. dst = NULL;
  4727. pos = cmd + 17;
  4728. if (*pos != ' ')
  4729. return -1;
  4730. pos++;
  4731. if (os_strncmp(pos, "upnp ", 5) == 0) {
  4732. u8 version;
  4733. pos += 5;
  4734. if (hexstr2bin(pos, &version, 1) < 0)
  4735. return -1;
  4736. pos += 2;
  4737. if (*pos != ' ')
  4738. return -1;
  4739. pos++;
  4740. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  4741. #ifdef CONFIG_WIFI_DISPLAY
  4742. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  4743. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  4744. #endif /* CONFIG_WIFI_DISPLAY */
  4745. } else if (os_strncmp(pos, "asp ", 4) == 0) {
  4746. char *svc_str;
  4747. char *svc_info = NULL;
  4748. u32 id;
  4749. pos += 4;
  4750. if (sscanf(pos, "%x", &id) != 1 || id > 0xff)
  4751. return -1;
  4752. pos = os_strchr(pos, ' ');
  4753. if (pos == NULL || pos[1] == '\0' || pos[1] == ' ')
  4754. return -1;
  4755. svc_str = pos + 1;
  4756. pos = os_strchr(svc_str, ' ');
  4757. if (pos)
  4758. *pos++ = '\0';
  4759. /* All remaining data is the svc_info string */
  4760. if (pos && pos[0] && pos[0] != ' ') {
  4761. len = os_strlen(pos);
  4762. /* Unescape in place */
  4763. len = utf8_unescape(pos, len, pos, len);
  4764. if (len > 0xff)
  4765. return -1;
  4766. svc_info = pos;
  4767. }
  4768. ref = wpas_p2p_sd_request_asp(wpa_s, dst, (u8) id,
  4769. svc_str, svc_info);
  4770. } else {
  4771. len = os_strlen(pos);
  4772. if (len & 1)
  4773. return -1;
  4774. len /= 2;
  4775. tlvs = wpabuf_alloc(len);
  4776. if (tlvs == NULL)
  4777. return -1;
  4778. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  4779. wpabuf_free(tlvs);
  4780. return -1;
  4781. }
  4782. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  4783. wpabuf_free(tlvs);
  4784. }
  4785. if (ref == 0)
  4786. return -1;
  4787. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  4788. if (os_snprintf_error(buflen, res))
  4789. return -1;
  4790. return res;
  4791. }
  4792. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  4793. char *cmd)
  4794. {
  4795. long long unsigned val;
  4796. u64 req;
  4797. if (sscanf(cmd, "%llx", &val) != 1)
  4798. return -1;
  4799. req = val;
  4800. return wpas_p2p_sd_cancel_request(wpa_s, req);
  4801. }
  4802. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  4803. {
  4804. int freq;
  4805. u8 dst[ETH_ALEN];
  4806. u8 dialog_token;
  4807. struct wpabuf *resp_tlvs;
  4808. char *pos, *pos2;
  4809. size_t len;
  4810. pos = os_strchr(cmd, ' ');
  4811. if (pos == NULL)
  4812. return -1;
  4813. *pos++ = '\0';
  4814. freq = atoi(cmd);
  4815. if (freq == 0)
  4816. return -1;
  4817. if (hwaddr_aton(pos, dst))
  4818. return -1;
  4819. pos += 17;
  4820. if (*pos != ' ')
  4821. return -1;
  4822. pos++;
  4823. pos2 = os_strchr(pos, ' ');
  4824. if (pos2 == NULL)
  4825. return -1;
  4826. *pos2++ = '\0';
  4827. dialog_token = atoi(pos);
  4828. len = os_strlen(pos2);
  4829. if (len & 1)
  4830. return -1;
  4831. len /= 2;
  4832. resp_tlvs = wpabuf_alloc(len);
  4833. if (resp_tlvs == NULL)
  4834. return -1;
  4835. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  4836. wpabuf_free(resp_tlvs);
  4837. return -1;
  4838. }
  4839. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  4840. wpabuf_free(resp_tlvs);
  4841. return 0;
  4842. }
  4843. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  4844. char *cmd)
  4845. {
  4846. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  4847. return -1;
  4848. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  4849. return 0;
  4850. }
  4851. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  4852. char *cmd)
  4853. {
  4854. char *pos;
  4855. size_t len;
  4856. struct wpabuf *query, *resp;
  4857. pos = os_strchr(cmd, ' ');
  4858. if (pos == NULL)
  4859. return -1;
  4860. *pos++ = '\0';
  4861. len = os_strlen(cmd);
  4862. if (len & 1)
  4863. return -1;
  4864. len /= 2;
  4865. query = wpabuf_alloc(len);
  4866. if (query == NULL)
  4867. return -1;
  4868. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  4869. wpabuf_free(query);
  4870. return -1;
  4871. }
  4872. len = os_strlen(pos);
  4873. if (len & 1) {
  4874. wpabuf_free(query);
  4875. return -1;
  4876. }
  4877. len /= 2;
  4878. resp = wpabuf_alloc(len);
  4879. if (resp == NULL) {
  4880. wpabuf_free(query);
  4881. return -1;
  4882. }
  4883. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  4884. wpabuf_free(query);
  4885. wpabuf_free(resp);
  4886. return -1;
  4887. }
  4888. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  4889. wpabuf_free(query);
  4890. wpabuf_free(resp);
  4891. return -1;
  4892. }
  4893. return 0;
  4894. }
  4895. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  4896. {
  4897. char *pos;
  4898. u8 version;
  4899. pos = os_strchr(cmd, ' ');
  4900. if (pos == NULL)
  4901. return -1;
  4902. *pos++ = '\0';
  4903. if (hexstr2bin(cmd, &version, 1) < 0)
  4904. return -1;
  4905. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  4906. }
  4907. static int p2p_ctrl_service_add_asp(struct wpa_supplicant *wpa_s,
  4908. u8 replace, char *cmd)
  4909. {
  4910. char *pos;
  4911. char *adv_str;
  4912. u32 auto_accept, adv_id, svc_state, config_methods;
  4913. char *svc_info = NULL;
  4914. char *cpt_prio_str;
  4915. u8 cpt_prio[P2PS_FEATURE_CAPAB_CPT_MAX + 1];
  4916. pos = os_strchr(cmd, ' ');
  4917. if (pos == NULL)
  4918. return -1;
  4919. *pos++ = '\0';
  4920. /* Auto-Accept value is mandatory, and must be one of the
  4921. * single values (0, 1, 2, 4) */
  4922. auto_accept = atoi(cmd);
  4923. switch (auto_accept) {
  4924. case P2PS_SETUP_NONE: /* No auto-accept */
  4925. case P2PS_SETUP_NEW:
  4926. case P2PS_SETUP_CLIENT:
  4927. case P2PS_SETUP_GROUP_OWNER:
  4928. break;
  4929. default:
  4930. return -1;
  4931. }
  4932. /* Advertisement ID is mandatory */
  4933. cmd = pos;
  4934. pos = os_strchr(cmd, ' ');
  4935. if (pos == NULL)
  4936. return -1;
  4937. *pos++ = '\0';
  4938. /* Handle Adv_ID == 0 (wildcard "org.wi-fi.wfds") internally. */
  4939. if (sscanf(cmd, "%x", &adv_id) != 1 || adv_id == 0)
  4940. return -1;
  4941. /* Only allow replacements if exist, and adds if not */
  4942. if (wpas_p2p_service_p2ps_id_exists(wpa_s, adv_id)) {
  4943. if (!replace)
  4944. return -1;
  4945. } else {
  4946. if (replace)
  4947. return -1;
  4948. }
  4949. /* svc_state between 0 - 0xff is mandatory */
  4950. if (sscanf(pos, "%x", &svc_state) != 1 || svc_state > 0xff)
  4951. return -1;
  4952. pos = os_strchr(pos, ' ');
  4953. if (pos == NULL)
  4954. return -1;
  4955. /* config_methods is mandatory */
  4956. pos++;
  4957. if (sscanf(pos, "%x", &config_methods) != 1)
  4958. return -1;
  4959. if (!(config_methods &
  4960. (WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD | WPS_CONFIG_P2PS)))
  4961. return -1;
  4962. pos = os_strchr(pos, ' ');
  4963. if (pos == NULL)
  4964. return -1;
  4965. pos++;
  4966. adv_str = pos;
  4967. /* Advertisement string is mandatory */
  4968. if (!pos[0] || pos[0] == ' ')
  4969. return -1;
  4970. /* Terminate svc string */
  4971. pos = os_strchr(pos, ' ');
  4972. if (pos != NULL)
  4973. *pos++ = '\0';
  4974. cpt_prio_str = (pos && pos[0]) ? os_strstr(pos, "cpt=") : NULL;
  4975. if (cpt_prio_str) {
  4976. pos = os_strchr(pos, ' ');
  4977. if (pos != NULL)
  4978. *pos++ = '\0';
  4979. if (p2ps_ctrl_parse_cpt_priority(cpt_prio_str + 4, cpt_prio))
  4980. return -1;
  4981. } else {
  4982. cpt_prio[0] = P2PS_FEATURE_CAPAB_UDP_TRANSPORT;
  4983. cpt_prio[1] = 0;
  4984. }
  4985. /* Service and Response Information are optional */
  4986. if (pos && pos[0]) {
  4987. size_t len;
  4988. /* Note the bare ' included, which cannot exist legally
  4989. * in unescaped string. */
  4990. svc_info = os_strstr(pos, "svc_info='");
  4991. if (svc_info) {
  4992. svc_info += 9;
  4993. len = os_strlen(svc_info);
  4994. utf8_unescape(svc_info, len, svc_info, len);
  4995. }
  4996. }
  4997. return wpas_p2p_service_add_asp(wpa_s, auto_accept, adv_id, adv_str,
  4998. (u8) svc_state, (u16) config_methods,
  4999. svc_info, cpt_prio);
  5000. }
  5001. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  5002. {
  5003. char *pos;
  5004. pos = os_strchr(cmd, ' ');
  5005. if (pos == NULL)
  5006. return -1;
  5007. *pos++ = '\0';
  5008. if (os_strcmp(cmd, "bonjour") == 0)
  5009. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  5010. if (os_strcmp(cmd, "upnp") == 0)
  5011. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  5012. if (os_strcmp(cmd, "asp") == 0)
  5013. return p2p_ctrl_service_add_asp(wpa_s, 0, pos);
  5014. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  5015. return -1;
  5016. }
  5017. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  5018. char *cmd)
  5019. {
  5020. size_t len;
  5021. struct wpabuf *query;
  5022. int ret;
  5023. len = os_strlen(cmd);
  5024. if (len & 1)
  5025. return -1;
  5026. len /= 2;
  5027. query = wpabuf_alloc(len);
  5028. if (query == NULL)
  5029. return -1;
  5030. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  5031. wpabuf_free(query);
  5032. return -1;
  5033. }
  5034. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  5035. wpabuf_free(query);
  5036. return ret;
  5037. }
  5038. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  5039. {
  5040. char *pos;
  5041. u8 version;
  5042. pos = os_strchr(cmd, ' ');
  5043. if (pos == NULL)
  5044. return -1;
  5045. *pos++ = '\0';
  5046. if (hexstr2bin(cmd, &version, 1) < 0)
  5047. return -1;
  5048. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  5049. }
  5050. static int p2p_ctrl_service_del_asp(struct wpa_supplicant *wpa_s, char *cmd)
  5051. {
  5052. u32 adv_id;
  5053. if (os_strcmp(cmd, "all") == 0) {
  5054. wpas_p2p_service_flush_asp(wpa_s);
  5055. return 0;
  5056. }
  5057. if (sscanf(cmd, "%x", &adv_id) != 1)
  5058. return -1;
  5059. return wpas_p2p_service_del_asp(wpa_s, adv_id);
  5060. }
  5061. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  5062. {
  5063. char *pos;
  5064. pos = os_strchr(cmd, ' ');
  5065. if (pos == NULL)
  5066. return -1;
  5067. *pos++ = '\0';
  5068. if (os_strcmp(cmd, "bonjour") == 0)
  5069. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  5070. if (os_strcmp(cmd, "upnp") == 0)
  5071. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  5072. if (os_strcmp(cmd, "asp") == 0)
  5073. return p2p_ctrl_service_del_asp(wpa_s, pos);
  5074. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  5075. return -1;
  5076. }
  5077. static int p2p_ctrl_service_replace(struct wpa_supplicant *wpa_s, char *cmd)
  5078. {
  5079. char *pos;
  5080. pos = os_strchr(cmd, ' ');
  5081. if (pos == NULL)
  5082. return -1;
  5083. *pos++ = '\0';
  5084. if (os_strcmp(cmd, "asp") == 0)
  5085. return p2p_ctrl_service_add_asp(wpa_s, 1, pos);
  5086. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  5087. return -1;
  5088. }
  5089. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  5090. {
  5091. u8 addr[ETH_ALEN];
  5092. /* <addr> */
  5093. if (hwaddr_aton(cmd, addr))
  5094. return -1;
  5095. return wpas_p2p_reject(wpa_s, addr);
  5096. }
  5097. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  5098. {
  5099. char *pos;
  5100. int id;
  5101. struct wpa_ssid *ssid;
  5102. u8 *_peer = NULL, peer[ETH_ALEN];
  5103. int freq = 0, pref_freq = 0;
  5104. int ht40, vht, max_oper_chwidth, chwidth = 0, freq2 = 0;
  5105. id = atoi(cmd);
  5106. pos = os_strstr(cmd, " peer=");
  5107. if (pos) {
  5108. pos += 6;
  5109. if (hwaddr_aton(pos, peer))
  5110. return -1;
  5111. _peer = peer;
  5112. }
  5113. ssid = wpa_config_get_network(wpa_s->conf, id);
  5114. if (ssid == NULL || ssid->disabled != 2) {
  5115. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  5116. "for persistent P2P group",
  5117. id);
  5118. return -1;
  5119. }
  5120. pos = os_strstr(cmd, " freq=");
  5121. if (pos) {
  5122. pos += 6;
  5123. freq = atoi(pos);
  5124. if (freq <= 0)
  5125. return -1;
  5126. }
  5127. pos = os_strstr(cmd, " pref=");
  5128. if (pos) {
  5129. pos += 6;
  5130. pref_freq = atoi(pos);
  5131. if (pref_freq <= 0)
  5132. return -1;
  5133. }
  5134. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  5135. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  5136. vht;
  5137. pos = os_strstr(cmd, "freq2=");
  5138. if (pos)
  5139. freq2 = atoi(pos + 6);
  5140. pos = os_strstr(cmd, " max_oper_chwidth=");
  5141. if (pos)
  5142. chwidth = atoi(pos + 18);
  5143. max_oper_chwidth = parse_freq(chwidth, freq2);
  5144. if (max_oper_chwidth < 0)
  5145. return -1;
  5146. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, freq2, ht40, vht,
  5147. max_oper_chwidth, pref_freq);
  5148. }
  5149. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  5150. {
  5151. char *pos;
  5152. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  5153. pos = os_strstr(cmd, " peer=");
  5154. if (!pos)
  5155. return -1;
  5156. *pos = '\0';
  5157. pos += 6;
  5158. if (hwaddr_aton(pos, peer)) {
  5159. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  5160. return -1;
  5161. }
  5162. pos = os_strstr(pos, " go_dev_addr=");
  5163. if (pos) {
  5164. pos += 13;
  5165. if (hwaddr_aton(pos, go_dev_addr)) {
  5166. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  5167. pos);
  5168. return -1;
  5169. }
  5170. go_dev = go_dev_addr;
  5171. }
  5172. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  5173. }
  5174. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  5175. {
  5176. if (os_strncmp(cmd, "persistent=", 11) == 0)
  5177. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  5178. if (os_strncmp(cmd, "group=", 6) == 0)
  5179. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  5180. return -1;
  5181. }
  5182. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  5183. int id, int freq, int vht_center_freq2,
  5184. int ht40, int vht, int vht_chwidth)
  5185. {
  5186. struct wpa_ssid *ssid;
  5187. ssid = wpa_config_get_network(wpa_s->conf, id);
  5188. if (ssid == NULL || ssid->disabled != 2) {
  5189. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  5190. "for persistent P2P group",
  5191. id);
  5192. return -1;
  5193. }
  5194. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq,
  5195. vht_center_freq2, 0, ht40, vht,
  5196. vht_chwidth, NULL, 0, 0);
  5197. }
  5198. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  5199. {
  5200. int freq = 0, persistent = 0, group_id = -1;
  5201. int vht = wpa_s->conf->p2p_go_vht;
  5202. int ht40 = wpa_s->conf->p2p_go_ht40 || vht;
  5203. int max_oper_chwidth, chwidth = 0, freq2 = 0;
  5204. char *token, *context = NULL;
  5205. while ((token = str_token(cmd, " ", &context))) {
  5206. if (sscanf(token, "freq=%d", &freq) == 1 ||
  5207. sscanf(token, "freq2=%d", &freq2) == 1 ||
  5208. sscanf(token, "persistent=%d", &group_id) == 1 ||
  5209. sscanf(token, "max_oper_chwidth=%d", &chwidth) == 1) {
  5210. continue;
  5211. } else if (os_strcmp(token, "ht40") == 0) {
  5212. ht40 = 1;
  5213. } else if (os_strcmp(token, "vht") == 0) {
  5214. vht = 1;
  5215. ht40 = 1;
  5216. } else if (os_strcmp(token, "persistent") == 0) {
  5217. persistent = 1;
  5218. } else {
  5219. wpa_printf(MSG_DEBUG,
  5220. "CTRL: Invalid P2P_GROUP_ADD parameter: '%s'",
  5221. token);
  5222. return -1;
  5223. }
  5224. }
  5225. max_oper_chwidth = parse_freq(chwidth, freq2);
  5226. if (max_oper_chwidth < 0)
  5227. return -1;
  5228. if (group_id >= 0)
  5229. return p2p_ctrl_group_add_persistent(wpa_s, group_id,
  5230. freq, freq2, ht40, vht,
  5231. max_oper_chwidth);
  5232. return wpas_p2p_group_add(wpa_s, persistent, freq, freq2, ht40, vht,
  5233. max_oper_chwidth);
  5234. }
  5235. static int p2p_ctrl_group_member(struct wpa_supplicant *wpa_s, const char *cmd,
  5236. char *buf, size_t buflen)
  5237. {
  5238. u8 dev_addr[ETH_ALEN];
  5239. struct wpa_ssid *ssid;
  5240. int res;
  5241. const u8 *iaddr;
  5242. ssid = wpa_s->current_ssid;
  5243. if (!wpa_s->global->p2p || !ssid || ssid->mode != WPAS_MODE_P2P_GO ||
  5244. hwaddr_aton(cmd, dev_addr))
  5245. return -1;
  5246. iaddr = p2p_group_get_client_interface_addr(wpa_s->p2p_group, dev_addr);
  5247. if (!iaddr)
  5248. return -1;
  5249. res = os_snprintf(buf, buflen, MACSTR, MAC2STR(iaddr));
  5250. if (os_snprintf_error(buflen, res))
  5251. return -1;
  5252. return res;
  5253. }
  5254. static int wpas_find_p2p_dev_addr_bss(struct wpa_global *global,
  5255. const u8 *p2p_dev_addr)
  5256. {
  5257. struct wpa_supplicant *wpa_s;
  5258. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5259. if (wpa_bss_get_p2p_dev_addr(wpa_s, p2p_dev_addr))
  5260. return 1;
  5261. }
  5262. return 0;
  5263. }
  5264. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  5265. char *buf, size_t buflen)
  5266. {
  5267. u8 addr[ETH_ALEN], *addr_ptr, group_capab;
  5268. int next, res;
  5269. const struct p2p_peer_info *info;
  5270. char *pos, *end;
  5271. char devtype[WPS_DEV_TYPE_BUFSIZE];
  5272. struct wpa_ssid *ssid;
  5273. size_t i;
  5274. if (!wpa_s->global->p2p)
  5275. return -1;
  5276. if (os_strcmp(cmd, "FIRST") == 0) {
  5277. addr_ptr = NULL;
  5278. next = 0;
  5279. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  5280. if (hwaddr_aton(cmd + 5, addr) < 0)
  5281. return -1;
  5282. addr_ptr = addr;
  5283. next = 1;
  5284. } else {
  5285. if (hwaddr_aton(cmd, addr) < 0)
  5286. return -1;
  5287. addr_ptr = addr;
  5288. next = 0;
  5289. }
  5290. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  5291. if (info == NULL)
  5292. return -1;
  5293. group_capab = info->group_capab;
  5294. if (group_capab &&
  5295. !wpas_find_p2p_dev_addr_bss(wpa_s->global, info->p2p_device_addr)) {
  5296. wpa_printf(MSG_DEBUG,
  5297. "P2P: Could not find any BSS with p2p_dev_addr "
  5298. MACSTR ", hence override group_capab from 0x%x to 0",
  5299. MAC2STR(info->p2p_device_addr), group_capab);
  5300. group_capab = 0;
  5301. }
  5302. pos = buf;
  5303. end = buf + buflen;
  5304. res = os_snprintf(pos, end - pos, MACSTR "\n"
  5305. "pri_dev_type=%s\n"
  5306. "device_name=%s\n"
  5307. "manufacturer=%s\n"
  5308. "model_name=%s\n"
  5309. "model_number=%s\n"
  5310. "serial_number=%s\n"
  5311. "config_methods=0x%x\n"
  5312. "dev_capab=0x%x\n"
  5313. "group_capab=0x%x\n"
  5314. "level=%d\n",
  5315. MAC2STR(info->p2p_device_addr),
  5316. wps_dev_type_bin2str(info->pri_dev_type,
  5317. devtype, sizeof(devtype)),
  5318. info->device_name,
  5319. info->manufacturer,
  5320. info->model_name,
  5321. info->model_number,
  5322. info->serial_number,
  5323. info->config_methods,
  5324. info->dev_capab,
  5325. group_capab,
  5326. info->level);
  5327. if (os_snprintf_error(end - pos, res))
  5328. return pos - buf;
  5329. pos += res;
  5330. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  5331. {
  5332. const u8 *t;
  5333. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  5334. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  5335. wps_dev_type_bin2str(t, devtype,
  5336. sizeof(devtype)));
  5337. if (os_snprintf_error(end - pos, res))
  5338. return pos - buf;
  5339. pos += res;
  5340. }
  5341. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  5342. if (ssid) {
  5343. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  5344. if (os_snprintf_error(end - pos, res))
  5345. return pos - buf;
  5346. pos += res;
  5347. }
  5348. res = p2p_get_peer_info_txt(info, pos, end - pos);
  5349. if (res < 0)
  5350. return pos - buf;
  5351. pos += res;
  5352. if (info->vendor_elems) {
  5353. res = os_snprintf(pos, end - pos, "vendor_elems=");
  5354. if (os_snprintf_error(end - pos, res))
  5355. return pos - buf;
  5356. pos += res;
  5357. pos += wpa_snprintf_hex(pos, end - pos,
  5358. wpabuf_head(info->vendor_elems),
  5359. wpabuf_len(info->vendor_elems));
  5360. res = os_snprintf(pos, end - pos, "\n");
  5361. if (os_snprintf_error(end - pos, res))
  5362. return pos - buf;
  5363. pos += res;
  5364. }
  5365. return pos - buf;
  5366. }
  5367. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  5368. const char *param)
  5369. {
  5370. unsigned int i;
  5371. if (wpa_s->global->p2p == NULL)
  5372. return -1;
  5373. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  5374. return -1;
  5375. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  5376. struct wpa_freq_range *freq;
  5377. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  5378. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  5379. freq->min, freq->max);
  5380. }
  5381. wpas_p2p_update_channel_list(wpa_s, WPAS_P2P_CHANNEL_UPDATE_DISALLOW);
  5382. return 0;
  5383. }
  5384. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  5385. {
  5386. char *param;
  5387. if (wpa_s->global->p2p == NULL)
  5388. return -1;
  5389. param = os_strchr(cmd, ' ');
  5390. if (param == NULL)
  5391. return -1;
  5392. *param++ = '\0';
  5393. if (os_strcmp(cmd, "discoverability") == 0) {
  5394. p2p_set_client_discoverability(wpa_s->global->p2p,
  5395. atoi(param));
  5396. return 0;
  5397. }
  5398. if (os_strcmp(cmd, "managed") == 0) {
  5399. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  5400. return 0;
  5401. }
  5402. if (os_strcmp(cmd, "listen_channel") == 0) {
  5403. char *pos;
  5404. u8 channel, op_class;
  5405. channel = atoi(param);
  5406. pos = os_strchr(param, ' ');
  5407. op_class = pos ? atoi(pos) : 81;
  5408. return p2p_set_listen_channel(wpa_s->global->p2p, op_class,
  5409. channel, 1);
  5410. }
  5411. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  5412. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  5413. os_strlen(param));
  5414. }
  5415. if (os_strcmp(cmd, "noa") == 0) {
  5416. char *pos;
  5417. int count, start, duration;
  5418. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  5419. count = atoi(param);
  5420. pos = os_strchr(param, ',');
  5421. if (pos == NULL)
  5422. return -1;
  5423. pos++;
  5424. start = atoi(pos);
  5425. pos = os_strchr(pos, ',');
  5426. if (pos == NULL)
  5427. return -1;
  5428. pos++;
  5429. duration = atoi(pos);
  5430. if (count < 0 || count > 255 || start < 0 || duration < 0)
  5431. return -1;
  5432. if (count == 0 && duration > 0)
  5433. return -1;
  5434. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  5435. "start=%d duration=%d", count, start, duration);
  5436. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  5437. }
  5438. if (os_strcmp(cmd, "ps") == 0)
  5439. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  5440. if (os_strcmp(cmd, "oppps") == 0)
  5441. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  5442. if (os_strcmp(cmd, "ctwindow") == 0)
  5443. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  5444. if (os_strcmp(cmd, "disabled") == 0) {
  5445. wpa_s->global->p2p_disabled = atoi(param);
  5446. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  5447. wpa_s->global->p2p_disabled ?
  5448. "disabled" : "enabled");
  5449. if (wpa_s->global->p2p_disabled) {
  5450. wpas_p2p_stop_find(wpa_s);
  5451. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5452. p2p_flush(wpa_s->global->p2p);
  5453. }
  5454. return 0;
  5455. }
  5456. if (os_strcmp(cmd, "conc_pref") == 0) {
  5457. if (os_strcmp(param, "sta") == 0)
  5458. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  5459. else if (os_strcmp(param, "p2p") == 0)
  5460. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  5461. else {
  5462. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  5463. return -1;
  5464. }
  5465. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  5466. "%s", param);
  5467. return 0;
  5468. }
  5469. if (os_strcmp(cmd, "force_long_sd") == 0) {
  5470. wpa_s->force_long_sd = atoi(param);
  5471. return 0;
  5472. }
  5473. if (os_strcmp(cmd, "peer_filter") == 0) {
  5474. u8 addr[ETH_ALEN];
  5475. if (hwaddr_aton(param, addr))
  5476. return -1;
  5477. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  5478. return 0;
  5479. }
  5480. if (os_strcmp(cmd, "cross_connect") == 0)
  5481. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  5482. if (os_strcmp(cmd, "go_apsd") == 0) {
  5483. if (os_strcmp(param, "disable") == 0)
  5484. wpa_s->set_ap_uapsd = 0;
  5485. else {
  5486. wpa_s->set_ap_uapsd = 1;
  5487. wpa_s->ap_uapsd = atoi(param);
  5488. }
  5489. return 0;
  5490. }
  5491. if (os_strcmp(cmd, "client_apsd") == 0) {
  5492. if (os_strcmp(param, "disable") == 0)
  5493. wpa_s->set_sta_uapsd = 0;
  5494. else {
  5495. int be, bk, vi, vo;
  5496. char *pos;
  5497. /* format: BE,BK,VI,VO;max SP Length */
  5498. be = atoi(param);
  5499. pos = os_strchr(param, ',');
  5500. if (pos == NULL)
  5501. return -1;
  5502. pos++;
  5503. bk = atoi(pos);
  5504. pos = os_strchr(pos, ',');
  5505. if (pos == NULL)
  5506. return -1;
  5507. pos++;
  5508. vi = atoi(pos);
  5509. pos = os_strchr(pos, ',');
  5510. if (pos == NULL)
  5511. return -1;
  5512. pos++;
  5513. vo = atoi(pos);
  5514. /* ignore max SP Length for now */
  5515. wpa_s->set_sta_uapsd = 1;
  5516. wpa_s->sta_uapsd = 0;
  5517. if (be)
  5518. wpa_s->sta_uapsd |= BIT(0);
  5519. if (bk)
  5520. wpa_s->sta_uapsd |= BIT(1);
  5521. if (vi)
  5522. wpa_s->sta_uapsd |= BIT(2);
  5523. if (vo)
  5524. wpa_s->sta_uapsd |= BIT(3);
  5525. }
  5526. return 0;
  5527. }
  5528. if (os_strcmp(cmd, "disallow_freq") == 0)
  5529. return p2p_ctrl_disallow_freq(wpa_s, param);
  5530. if (os_strcmp(cmd, "disc_int") == 0) {
  5531. int min_disc_int, max_disc_int, max_disc_tu;
  5532. char *pos;
  5533. pos = param;
  5534. min_disc_int = atoi(pos);
  5535. pos = os_strchr(pos, ' ');
  5536. if (pos == NULL)
  5537. return -1;
  5538. *pos++ = '\0';
  5539. max_disc_int = atoi(pos);
  5540. pos = os_strchr(pos, ' ');
  5541. if (pos == NULL)
  5542. return -1;
  5543. *pos++ = '\0';
  5544. max_disc_tu = atoi(pos);
  5545. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  5546. max_disc_int, max_disc_tu);
  5547. }
  5548. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  5549. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  5550. return 0;
  5551. }
  5552. #ifdef CONFIG_WPS_NFC
  5553. if (os_strcmp(cmd, "nfc_tag") == 0)
  5554. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  5555. #endif /* CONFIG_WPS_NFC */
  5556. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  5557. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  5558. return 0;
  5559. }
  5560. if (os_strcmp(cmd, "override_pref_op_chan") == 0) {
  5561. int op_class, chan;
  5562. op_class = atoi(param);
  5563. param = os_strchr(param, ':');
  5564. if (!param)
  5565. return -1;
  5566. param++;
  5567. chan = atoi(param);
  5568. p2p_set_override_pref_op_chan(wpa_s->global->p2p, op_class,
  5569. chan);
  5570. return 0;
  5571. }
  5572. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  5573. cmd);
  5574. return -1;
  5575. }
  5576. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  5577. {
  5578. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  5579. wpa_s->force_long_sd = 0;
  5580. wpas_p2p_stop_find(wpa_s);
  5581. wpa_s->parent->p2ps_method_config_any = 0;
  5582. if (wpa_s->global->p2p)
  5583. p2p_flush(wpa_s->global->p2p);
  5584. }
  5585. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  5586. {
  5587. char *pos, *pos2;
  5588. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  5589. if (cmd[0]) {
  5590. pos = os_strchr(cmd, ' ');
  5591. if (pos == NULL)
  5592. return -1;
  5593. *pos++ = '\0';
  5594. dur1 = atoi(cmd);
  5595. pos2 = os_strchr(pos, ' ');
  5596. if (pos2)
  5597. *pos2++ = '\0';
  5598. int1 = atoi(pos);
  5599. } else
  5600. pos2 = NULL;
  5601. if (pos2) {
  5602. pos = os_strchr(pos2, ' ');
  5603. if (pos == NULL)
  5604. return -1;
  5605. *pos++ = '\0';
  5606. dur2 = atoi(pos2);
  5607. int2 = atoi(pos);
  5608. }
  5609. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  5610. }
  5611. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  5612. {
  5613. char *pos;
  5614. unsigned int period = 0, interval = 0;
  5615. if (cmd[0]) {
  5616. pos = os_strchr(cmd, ' ');
  5617. if (pos == NULL)
  5618. return -1;
  5619. *pos++ = '\0';
  5620. period = atoi(cmd);
  5621. interval = atoi(pos);
  5622. }
  5623. return wpas_p2p_ext_listen(wpa_s, period, interval);
  5624. }
  5625. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  5626. {
  5627. const char *pos;
  5628. u8 peer[ETH_ALEN];
  5629. int iface_addr = 0;
  5630. pos = cmd;
  5631. if (os_strncmp(pos, "iface=", 6) == 0) {
  5632. iface_addr = 1;
  5633. pos += 6;
  5634. }
  5635. if (hwaddr_aton(pos, peer))
  5636. return -1;
  5637. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  5638. return 0;
  5639. }
  5640. static int p2p_ctrl_iface_p2p_lo_start(struct wpa_supplicant *wpa_s, char *cmd)
  5641. {
  5642. int freq = 0, period = 0, interval = 0, count = 0;
  5643. if (sscanf(cmd, "%d %d %d %d", &freq, &period, &interval, &count) != 4)
  5644. {
  5645. wpa_printf(MSG_DEBUG,
  5646. "CTRL: Invalid P2P LO Start parameter: '%s'", cmd);
  5647. return -1;
  5648. }
  5649. return wpas_p2p_lo_start(wpa_s, freq, period, interval, count);
  5650. }
  5651. #endif /* CONFIG_P2P */
  5652. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  5653. {
  5654. struct wpa_freq_range_list ranges;
  5655. int *freqs = NULL;
  5656. struct hostapd_hw_modes *mode;
  5657. u16 i;
  5658. if (wpa_s->hw.modes == NULL)
  5659. return NULL;
  5660. os_memset(&ranges, 0, sizeof(ranges));
  5661. if (freq_range_list_parse(&ranges, val) < 0)
  5662. return NULL;
  5663. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  5664. int j;
  5665. mode = &wpa_s->hw.modes[i];
  5666. for (j = 0; j < mode->num_channels; j++) {
  5667. unsigned int freq;
  5668. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  5669. continue;
  5670. freq = mode->channels[j].freq;
  5671. if (!freq_range_list_includes(&ranges, freq))
  5672. continue;
  5673. int_array_add_unique(&freqs, freq);
  5674. }
  5675. }
  5676. os_free(ranges.range);
  5677. return freqs;
  5678. }
  5679. #ifdef CONFIG_INTERWORKING
  5680. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  5681. {
  5682. int auto_sel = 0;
  5683. int *freqs = NULL;
  5684. if (param) {
  5685. char *pos;
  5686. auto_sel = os_strstr(param, "auto") != NULL;
  5687. pos = os_strstr(param, "freq=");
  5688. if (pos) {
  5689. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  5690. if (freqs == NULL)
  5691. return -1;
  5692. }
  5693. }
  5694. return interworking_select(wpa_s, auto_sel, freqs);
  5695. }
  5696. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst,
  5697. int only_add)
  5698. {
  5699. u8 bssid[ETH_ALEN];
  5700. struct wpa_bss *bss;
  5701. if (hwaddr_aton(dst, bssid)) {
  5702. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  5703. return -1;
  5704. }
  5705. bss = wpa_bss_get_bssid(wpa_s, bssid);
  5706. if (bss == NULL) {
  5707. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  5708. MAC2STR(bssid));
  5709. return -1;
  5710. }
  5711. if (bss->ssid_len == 0) {
  5712. int found = 0;
  5713. wpa_printf(MSG_DEBUG, "Selected BSS entry for " MACSTR
  5714. " does not have SSID information", MAC2STR(bssid));
  5715. dl_list_for_each_reverse(bss, &wpa_s->bss, struct wpa_bss,
  5716. list) {
  5717. if (os_memcmp(bss->bssid, bssid, ETH_ALEN) == 0 &&
  5718. bss->ssid_len > 0) {
  5719. found = 1;
  5720. break;
  5721. }
  5722. }
  5723. if (!found)
  5724. return -1;
  5725. wpa_printf(MSG_DEBUG,
  5726. "Found another matching BSS entry with SSID");
  5727. }
  5728. return interworking_connect(wpa_s, bss, only_add);
  5729. }
  5730. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  5731. {
  5732. u8 dst_addr[ETH_ALEN];
  5733. int used;
  5734. char *pos;
  5735. #define MAX_ANQP_INFO_ID 100
  5736. u16 id[MAX_ANQP_INFO_ID];
  5737. size_t num_id = 0;
  5738. u32 subtypes = 0;
  5739. u32 mbo_subtypes = 0;
  5740. used = hwaddr_aton2(dst, dst_addr);
  5741. if (used < 0)
  5742. return -1;
  5743. pos = dst + used;
  5744. if (*pos == ' ')
  5745. pos++;
  5746. while (num_id < MAX_ANQP_INFO_ID) {
  5747. if (os_strncmp(pos, "hs20:", 5) == 0) {
  5748. #ifdef CONFIG_HS20
  5749. int num = atoi(pos + 5);
  5750. if (num <= 0 || num > 31)
  5751. return -1;
  5752. subtypes |= BIT(num);
  5753. #else /* CONFIG_HS20 */
  5754. return -1;
  5755. #endif /* CONFIG_HS20 */
  5756. } else if (os_strncmp(pos, "mbo:", 4) == 0) {
  5757. #ifdef CONFIG_MBO
  5758. int num = atoi(pos + 4);
  5759. if (num <= 0 || num > MAX_MBO_ANQP_SUBTYPE)
  5760. return -1;
  5761. mbo_subtypes |= BIT(num);
  5762. #else /* CONFIG_MBO */
  5763. return -1;
  5764. #endif /* CONFIG_MBO */
  5765. } else {
  5766. id[num_id] = atoi(pos);
  5767. if (id[num_id])
  5768. num_id++;
  5769. }
  5770. pos = os_strchr(pos + 1, ',');
  5771. if (pos == NULL)
  5772. break;
  5773. pos++;
  5774. }
  5775. if (num_id == 0 && !subtypes && !mbo_subtypes)
  5776. return -1;
  5777. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes,
  5778. mbo_subtypes);
  5779. }
  5780. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  5781. {
  5782. u8 dst_addr[ETH_ALEN];
  5783. struct wpabuf *advproto, *query = NULL;
  5784. int used, ret = -1;
  5785. char *pos, *end;
  5786. size_t len;
  5787. used = hwaddr_aton2(cmd, dst_addr);
  5788. if (used < 0)
  5789. return -1;
  5790. pos = cmd + used;
  5791. while (*pos == ' ')
  5792. pos++;
  5793. /* Advertisement Protocol ID */
  5794. end = os_strchr(pos, ' ');
  5795. if (end)
  5796. len = end - pos;
  5797. else
  5798. len = os_strlen(pos);
  5799. if (len & 0x01)
  5800. return -1;
  5801. len /= 2;
  5802. if (len == 0)
  5803. return -1;
  5804. advproto = wpabuf_alloc(len);
  5805. if (advproto == NULL)
  5806. return -1;
  5807. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  5808. goto fail;
  5809. if (end) {
  5810. /* Optional Query Request */
  5811. pos = end + 1;
  5812. while (*pos == ' ')
  5813. pos++;
  5814. len = os_strlen(pos);
  5815. if (len) {
  5816. if (len & 0x01)
  5817. goto fail;
  5818. len /= 2;
  5819. if (len == 0)
  5820. goto fail;
  5821. query = wpabuf_alloc(len);
  5822. if (query == NULL)
  5823. goto fail;
  5824. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  5825. goto fail;
  5826. }
  5827. }
  5828. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  5829. fail:
  5830. wpabuf_free(advproto);
  5831. wpabuf_free(query);
  5832. return ret;
  5833. }
  5834. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  5835. size_t buflen)
  5836. {
  5837. u8 addr[ETH_ALEN];
  5838. int dialog_token;
  5839. int used;
  5840. char *pos;
  5841. size_t resp_len, start, requested_len;
  5842. struct wpabuf *resp;
  5843. int ret;
  5844. used = hwaddr_aton2(cmd, addr);
  5845. if (used < 0)
  5846. return -1;
  5847. pos = cmd + used;
  5848. while (*pos == ' ')
  5849. pos++;
  5850. dialog_token = atoi(pos);
  5851. if (wpa_s->last_gas_resp &&
  5852. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  5853. dialog_token == wpa_s->last_gas_dialog_token)
  5854. resp = wpa_s->last_gas_resp;
  5855. else if (wpa_s->prev_gas_resp &&
  5856. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  5857. dialog_token == wpa_s->prev_gas_dialog_token)
  5858. resp = wpa_s->prev_gas_resp;
  5859. else
  5860. return -1;
  5861. resp_len = wpabuf_len(resp);
  5862. start = 0;
  5863. requested_len = resp_len;
  5864. pos = os_strchr(pos, ' ');
  5865. if (pos) {
  5866. start = atoi(pos);
  5867. if (start > resp_len)
  5868. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  5869. pos = os_strchr(pos, ',');
  5870. if (pos == NULL)
  5871. return -1;
  5872. pos++;
  5873. requested_len = atoi(pos);
  5874. if (start + requested_len > resp_len)
  5875. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  5876. }
  5877. if (requested_len * 2 + 1 > buflen)
  5878. return os_snprintf(buf, buflen, "FAIL-Too long response");
  5879. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  5880. requested_len);
  5881. if (start + requested_len == resp_len) {
  5882. /*
  5883. * Free memory by dropping the response after it has been
  5884. * fetched.
  5885. */
  5886. if (resp == wpa_s->prev_gas_resp) {
  5887. wpabuf_free(wpa_s->prev_gas_resp);
  5888. wpa_s->prev_gas_resp = NULL;
  5889. } else {
  5890. wpabuf_free(wpa_s->last_gas_resp);
  5891. wpa_s->last_gas_resp = NULL;
  5892. }
  5893. }
  5894. return ret;
  5895. }
  5896. #endif /* CONFIG_INTERWORKING */
  5897. #ifdef CONFIG_HS20
  5898. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  5899. {
  5900. u8 dst_addr[ETH_ALEN];
  5901. int used;
  5902. char *pos;
  5903. u32 subtypes = 0;
  5904. used = hwaddr_aton2(dst, dst_addr);
  5905. if (used < 0)
  5906. return -1;
  5907. pos = dst + used;
  5908. if (*pos == ' ')
  5909. pos++;
  5910. for (;;) {
  5911. int num = atoi(pos);
  5912. if (num <= 0 || num > 31)
  5913. return -1;
  5914. subtypes |= BIT(num);
  5915. pos = os_strchr(pos + 1, ',');
  5916. if (pos == NULL)
  5917. break;
  5918. pos++;
  5919. }
  5920. if (subtypes == 0)
  5921. return -1;
  5922. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0, 0);
  5923. }
  5924. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  5925. const u8 *addr, const char *realm)
  5926. {
  5927. u8 *buf;
  5928. size_t rlen, len;
  5929. int ret;
  5930. rlen = os_strlen(realm);
  5931. len = 3 + rlen;
  5932. buf = os_malloc(len);
  5933. if (buf == NULL)
  5934. return -1;
  5935. buf[0] = 1; /* NAI Home Realm Count */
  5936. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  5937. buf[2] = rlen;
  5938. os_memcpy(buf + 3, realm, rlen);
  5939. ret = hs20_anqp_send_req(wpa_s, addr,
  5940. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  5941. buf, len, 0);
  5942. os_free(buf);
  5943. return ret;
  5944. }
  5945. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  5946. char *dst)
  5947. {
  5948. struct wpa_cred *cred = wpa_s->conf->cred;
  5949. u8 dst_addr[ETH_ALEN];
  5950. int used;
  5951. u8 *buf;
  5952. size_t len;
  5953. int ret;
  5954. used = hwaddr_aton2(dst, dst_addr);
  5955. if (used < 0)
  5956. return -1;
  5957. while (dst[used] == ' ')
  5958. used++;
  5959. if (os_strncmp(dst + used, "realm=", 6) == 0)
  5960. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  5961. dst + used + 6);
  5962. len = os_strlen(dst + used);
  5963. if (len == 0 && cred && cred->realm)
  5964. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  5965. if (len & 1)
  5966. return -1;
  5967. len /= 2;
  5968. buf = os_malloc(len);
  5969. if (buf == NULL)
  5970. return -1;
  5971. if (hexstr2bin(dst + used, buf, len) < 0) {
  5972. os_free(buf);
  5973. return -1;
  5974. }
  5975. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  5976. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  5977. buf, len, 0);
  5978. os_free(buf);
  5979. return ret;
  5980. }
  5981. static int get_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd, char *reply,
  5982. int buflen)
  5983. {
  5984. u8 dst_addr[ETH_ALEN];
  5985. int used;
  5986. char *ctx = NULL, *icon, *poffset, *psize;
  5987. used = hwaddr_aton2(cmd, dst_addr);
  5988. if (used < 0)
  5989. return -1;
  5990. cmd += used;
  5991. icon = str_token(cmd, " ", &ctx);
  5992. poffset = str_token(cmd, " ", &ctx);
  5993. psize = str_token(cmd, " ", &ctx);
  5994. if (!icon || !poffset || !psize)
  5995. return -1;
  5996. wpa_s->fetch_osu_icon_in_progress = 0;
  5997. return hs20_get_icon(wpa_s, dst_addr, icon, atoi(poffset), atoi(psize),
  5998. reply, buflen);
  5999. }
  6000. static int del_hs20_icon(struct wpa_supplicant *wpa_s, char *cmd)
  6001. {
  6002. u8 dst_addr[ETH_ALEN];
  6003. int used;
  6004. char *icon;
  6005. if (!cmd[0])
  6006. return hs20_del_icon(wpa_s, NULL, NULL);
  6007. used = hwaddr_aton2(cmd, dst_addr);
  6008. if (used < 0)
  6009. return -1;
  6010. while (cmd[used] == ' ')
  6011. used++;
  6012. icon = cmd[used] ? &cmd[used] : NULL;
  6013. return hs20_del_icon(wpa_s, dst_addr, icon);
  6014. }
  6015. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd, int inmem)
  6016. {
  6017. u8 dst_addr[ETH_ALEN];
  6018. int used;
  6019. char *icon;
  6020. used = hwaddr_aton2(cmd, dst_addr);
  6021. if (used < 0)
  6022. return -1;
  6023. while (cmd[used] == ' ')
  6024. used++;
  6025. icon = &cmd[used];
  6026. wpa_s->fetch_osu_icon_in_progress = 0;
  6027. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  6028. (u8 *) icon, os_strlen(icon), inmem);
  6029. }
  6030. #endif /* CONFIG_HS20 */
  6031. #ifdef CONFIG_AUTOSCAN
  6032. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  6033. char *cmd)
  6034. {
  6035. enum wpa_states state = wpa_s->wpa_state;
  6036. char *new_params = NULL;
  6037. if (os_strlen(cmd) > 0) {
  6038. new_params = os_strdup(cmd);
  6039. if (new_params == NULL)
  6040. return -1;
  6041. }
  6042. os_free(wpa_s->conf->autoscan);
  6043. wpa_s->conf->autoscan = new_params;
  6044. if (wpa_s->conf->autoscan == NULL)
  6045. autoscan_deinit(wpa_s);
  6046. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  6047. autoscan_init(wpa_s, 1);
  6048. else if (state == WPA_SCANNING)
  6049. wpa_supplicant_reinit_autoscan(wpa_s);
  6050. else
  6051. wpa_printf(MSG_DEBUG, "No autoscan update in state %s",
  6052. wpa_supplicant_state_txt(state));
  6053. return 0;
  6054. }
  6055. #endif /* CONFIG_AUTOSCAN */
  6056. #ifdef CONFIG_WNM
  6057. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  6058. {
  6059. int enter;
  6060. int intval = 0;
  6061. char *pos;
  6062. int ret;
  6063. struct wpabuf *tfs_req = NULL;
  6064. if (os_strncmp(cmd, "enter", 5) == 0)
  6065. enter = 1;
  6066. else if (os_strncmp(cmd, "exit", 4) == 0)
  6067. enter = 0;
  6068. else
  6069. return -1;
  6070. pos = os_strstr(cmd, " interval=");
  6071. if (pos)
  6072. intval = atoi(pos + 10);
  6073. pos = os_strstr(cmd, " tfs_req=");
  6074. if (pos) {
  6075. char *end;
  6076. size_t len;
  6077. pos += 9;
  6078. end = os_strchr(pos, ' ');
  6079. if (end)
  6080. len = end - pos;
  6081. else
  6082. len = os_strlen(pos);
  6083. if (len & 1)
  6084. return -1;
  6085. len /= 2;
  6086. tfs_req = wpabuf_alloc(len);
  6087. if (tfs_req == NULL)
  6088. return -1;
  6089. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  6090. wpabuf_free(tfs_req);
  6091. return -1;
  6092. }
  6093. }
  6094. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  6095. WNM_SLEEP_MODE_EXIT, intval,
  6096. tfs_req);
  6097. wpabuf_free(tfs_req);
  6098. return ret;
  6099. }
  6100. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  6101. {
  6102. int query_reason, list = 0;
  6103. char *btm_candidates = NULL;
  6104. query_reason = atoi(cmd);
  6105. cmd = os_strchr(cmd, ' ');
  6106. if (cmd) {
  6107. if (os_strncmp(cmd, " list", 5) == 0)
  6108. list = 1;
  6109. else
  6110. btm_candidates = cmd;
  6111. }
  6112. wpa_printf(MSG_DEBUG,
  6113. "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d%s",
  6114. query_reason, list ? " candidate list" : "");
  6115. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason,
  6116. btm_candidates,
  6117. list);
  6118. }
  6119. #endif /* CONFIG_WNM */
  6120. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  6121. size_t buflen)
  6122. {
  6123. struct wpa_signal_info si;
  6124. int ret;
  6125. char *pos, *end;
  6126. ret = wpa_drv_signal_poll(wpa_s, &si);
  6127. if (ret)
  6128. return -1;
  6129. pos = buf;
  6130. end = buf + buflen;
  6131. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  6132. "NOISE=%d\nFREQUENCY=%u\n",
  6133. si.current_signal, si.current_txrate / 1000,
  6134. si.current_noise, si.frequency);
  6135. if (os_snprintf_error(end - pos, ret))
  6136. return -1;
  6137. pos += ret;
  6138. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  6139. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  6140. channel_width_to_string(si.chanwidth));
  6141. if (os_snprintf_error(end - pos, ret))
  6142. return -1;
  6143. pos += ret;
  6144. }
  6145. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  6146. ret = os_snprintf(pos, end - pos,
  6147. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  6148. si.center_frq1, si.center_frq2);
  6149. if (os_snprintf_error(end - pos, ret))
  6150. return -1;
  6151. pos += ret;
  6152. }
  6153. if (si.avg_signal) {
  6154. ret = os_snprintf(pos, end - pos,
  6155. "AVG_RSSI=%d\n", si.avg_signal);
  6156. if (os_snprintf_error(end - pos, ret))
  6157. return -1;
  6158. pos += ret;
  6159. }
  6160. if (si.avg_beacon_signal) {
  6161. ret = os_snprintf(pos, end - pos,
  6162. "AVG_BEACON_RSSI=%d\n", si.avg_beacon_signal);
  6163. if (os_snprintf_error(end - pos, ret))
  6164. return -1;
  6165. pos += ret;
  6166. }
  6167. return pos - buf;
  6168. }
  6169. static int wpas_ctrl_iface_signal_monitor(struct wpa_supplicant *wpa_s,
  6170. const char *cmd)
  6171. {
  6172. const char *pos;
  6173. int threshold = 0;
  6174. int hysteresis = 0;
  6175. if (wpa_s->bgscan && wpa_s->bgscan_priv) {
  6176. wpa_printf(MSG_DEBUG,
  6177. "Reject SIGNAL_MONITOR command - bgscan is active");
  6178. return -1;
  6179. }
  6180. pos = os_strstr(cmd, "THRESHOLD=");
  6181. if (pos)
  6182. threshold = atoi(pos + 10);
  6183. pos = os_strstr(cmd, "HYSTERESIS=");
  6184. if (pos)
  6185. hysteresis = atoi(pos + 11);
  6186. return wpa_drv_signal_monitor(wpa_s, threshold, hysteresis);
  6187. }
  6188. #ifdef CONFIG_TESTING_OPTIONS
  6189. int wpas_ctrl_iface_get_pref_freq_list_override(struct wpa_supplicant *wpa_s,
  6190. enum wpa_driver_if_type if_type,
  6191. unsigned int *num,
  6192. unsigned int *freq_list)
  6193. {
  6194. char *pos = wpa_s->get_pref_freq_list_override;
  6195. char *end;
  6196. unsigned int count = 0;
  6197. /* Override string format:
  6198. * <if_type1>:<freq1>,<freq2>,... <if_type2>:... */
  6199. while (pos) {
  6200. if (atoi(pos) == (int) if_type)
  6201. break;
  6202. pos = os_strchr(pos, ' ');
  6203. if (pos)
  6204. pos++;
  6205. }
  6206. if (!pos)
  6207. return -1;
  6208. pos = os_strchr(pos, ':');
  6209. if (!pos)
  6210. return -1;
  6211. pos++;
  6212. end = os_strchr(pos, ' ');
  6213. while (pos && (!end || pos < end) && count < *num) {
  6214. freq_list[count++] = atoi(pos);
  6215. pos = os_strchr(pos, ',');
  6216. if (pos)
  6217. pos++;
  6218. }
  6219. *num = count;
  6220. return 0;
  6221. }
  6222. #endif /* CONFIG_TESTING_OPTIONS */
  6223. static int wpas_ctrl_iface_get_pref_freq_list(
  6224. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  6225. {
  6226. unsigned int freq_list[100], num = 100, i;
  6227. int ret;
  6228. enum wpa_driver_if_type iface_type;
  6229. char *pos, *end;
  6230. pos = buf;
  6231. end = buf + buflen;
  6232. /* buf: "<interface_type>" */
  6233. if (os_strcmp(cmd, "STATION") == 0)
  6234. iface_type = WPA_IF_STATION;
  6235. else if (os_strcmp(cmd, "AP") == 0)
  6236. iface_type = WPA_IF_AP_BSS;
  6237. else if (os_strcmp(cmd, "P2P_GO") == 0)
  6238. iface_type = WPA_IF_P2P_GO;
  6239. else if (os_strcmp(cmd, "P2P_CLIENT") == 0)
  6240. iface_type = WPA_IF_P2P_CLIENT;
  6241. else if (os_strcmp(cmd, "IBSS") == 0)
  6242. iface_type = WPA_IF_IBSS;
  6243. else if (os_strcmp(cmd, "TDLS") == 0)
  6244. iface_type = WPA_IF_TDLS;
  6245. else
  6246. return -1;
  6247. wpa_printf(MSG_DEBUG,
  6248. "CTRL_IFACE: GET_PREF_FREQ_LIST iface_type=%d (%s)",
  6249. iface_type, buf);
  6250. ret = wpa_drv_get_pref_freq_list(wpa_s, iface_type, &num, freq_list);
  6251. if (ret)
  6252. return -1;
  6253. for (i = 0; i < num; i++) {
  6254. ret = os_snprintf(pos, end - pos, "%s%u",
  6255. i > 0 ? "," : "", freq_list[i]);
  6256. if (os_snprintf_error(end - pos, ret))
  6257. return -1;
  6258. pos += ret;
  6259. }
  6260. return pos - buf;
  6261. }
  6262. static int wpas_ctrl_iface_driver_flags(struct wpa_supplicant *wpa_s,
  6263. char *buf, size_t buflen)
  6264. {
  6265. int ret, i;
  6266. char *pos, *end;
  6267. ret = os_snprintf(buf, buflen, "%016llX:\n",
  6268. (long long unsigned) wpa_s->drv_flags);
  6269. if (os_snprintf_error(buflen, ret))
  6270. return -1;
  6271. pos = buf + ret;
  6272. end = buf + buflen;
  6273. for (i = 0; i < 64; i++) {
  6274. if (wpa_s->drv_flags & (1LLU << i)) {
  6275. ret = os_snprintf(pos, end - pos, "%s\n",
  6276. driver_flag_to_string(1LLU << i));
  6277. if (os_snprintf_error(end - pos, ret))
  6278. return -1;
  6279. pos += ret;
  6280. }
  6281. }
  6282. return pos - buf;
  6283. }
  6284. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  6285. size_t buflen)
  6286. {
  6287. struct hostap_sta_driver_data sta;
  6288. int ret;
  6289. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  6290. if (ret)
  6291. return -1;
  6292. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  6293. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  6294. if (os_snprintf_error(buflen, ret))
  6295. return -1;
  6296. return ret;
  6297. }
  6298. #ifdef ANDROID
  6299. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  6300. char *buf, size_t buflen)
  6301. {
  6302. int ret;
  6303. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  6304. if (ret == 0) {
  6305. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  6306. struct p2p_data *p2p = wpa_s->global->p2p;
  6307. if (p2p) {
  6308. char country[3];
  6309. country[0] = cmd[8];
  6310. country[1] = cmd[9];
  6311. country[2] = 0x04;
  6312. p2p_set_country(p2p, country);
  6313. }
  6314. }
  6315. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  6316. if (os_snprintf_error(buflen, ret))
  6317. ret = -1;
  6318. }
  6319. return ret;
  6320. }
  6321. #endif /* ANDROID */
  6322. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  6323. char *buf, size_t buflen)
  6324. {
  6325. int ret;
  6326. char *pos;
  6327. u8 *data = NULL;
  6328. unsigned int vendor_id, subcmd;
  6329. struct wpabuf *reply;
  6330. size_t data_len = 0;
  6331. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  6332. vendor_id = strtoul(cmd, &pos, 16);
  6333. if (!isblank((unsigned char) *pos))
  6334. return -EINVAL;
  6335. subcmd = strtoul(pos, &pos, 10);
  6336. if (*pos != '\0') {
  6337. if (!isblank((unsigned char) *pos++))
  6338. return -EINVAL;
  6339. data_len = os_strlen(pos);
  6340. }
  6341. if (data_len) {
  6342. data_len /= 2;
  6343. data = os_malloc(data_len);
  6344. if (!data)
  6345. return -1;
  6346. if (hexstr2bin(pos, data, data_len)) {
  6347. wpa_printf(MSG_DEBUG,
  6348. "Vendor command: wrong parameter format");
  6349. os_free(data);
  6350. return -EINVAL;
  6351. }
  6352. }
  6353. reply = wpabuf_alloc((buflen - 1) / 2);
  6354. if (!reply) {
  6355. os_free(data);
  6356. return -1;
  6357. }
  6358. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  6359. reply);
  6360. if (ret == 0)
  6361. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  6362. wpabuf_len(reply));
  6363. wpabuf_free(reply);
  6364. os_free(data);
  6365. return ret;
  6366. }
  6367. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  6368. {
  6369. #ifdef CONFIG_P2P
  6370. struct wpa_supplicant *p2p_wpa_s = wpa_s->global->p2p_init_wpa_s ?
  6371. wpa_s->global->p2p_init_wpa_s : wpa_s;
  6372. #endif /* CONFIG_P2P */
  6373. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  6374. if (wpas_abort_ongoing_scan(wpa_s) == 0)
  6375. wpa_s->ignore_post_flush_scan_res = 1;
  6376. if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
  6377. /*
  6378. * Avoid possible auto connect re-connection on getting
  6379. * disconnected due to state flush.
  6380. */
  6381. wpa_supplicant_set_state(wpa_s, WPA_DISCONNECTED);
  6382. }
  6383. #ifdef CONFIG_P2P
  6384. wpas_p2p_group_remove(p2p_wpa_s, "*");
  6385. wpas_p2p_cancel(p2p_wpa_s);
  6386. p2p_ctrl_flush(p2p_wpa_s);
  6387. wpas_p2p_service_flush(p2p_wpa_s);
  6388. p2p_wpa_s->global->p2p_disabled = 0;
  6389. p2p_wpa_s->global->p2p_per_sta_psk = 0;
  6390. p2p_wpa_s->conf->num_sec_device_types = 0;
  6391. p2p_wpa_s->p2p_disable_ip_addr_req = 0;
  6392. os_free(p2p_wpa_s->global->p2p_go_avoid_freq.range);
  6393. p2p_wpa_s->global->p2p_go_avoid_freq.range = NULL;
  6394. p2p_wpa_s->global->p2p_go_avoid_freq.num = 0;
  6395. p2p_wpa_s->global->pending_p2ps_group = 0;
  6396. p2p_wpa_s->global->pending_p2ps_group_freq = 0;
  6397. #endif /* CONFIG_P2P */
  6398. #ifdef CONFIG_WPS_TESTING
  6399. wps_version_number = 0x20;
  6400. wps_testing_dummy_cred = 0;
  6401. wps_corrupt_pkhash = 0;
  6402. wps_force_auth_types_in_use = 0;
  6403. wps_force_encr_types_in_use = 0;
  6404. #endif /* CONFIG_WPS_TESTING */
  6405. #ifdef CONFIG_WPS
  6406. wpa_s->wps_fragment_size = 0;
  6407. wpas_wps_cancel(wpa_s);
  6408. wps_registrar_flush(wpa_s->wps->registrar);
  6409. #endif /* CONFIG_WPS */
  6410. wpa_s->after_wps = 0;
  6411. wpa_s->known_wps_freq = 0;
  6412. #ifdef CONFIG_DPP
  6413. wpas_dpp_deinit(wpa_s);
  6414. #endif /* CONFIG_DPP */
  6415. #ifdef CONFIG_TDLS
  6416. #ifdef CONFIG_TDLS_TESTING
  6417. tdls_testing = 0;
  6418. #endif /* CONFIG_TDLS_TESTING */
  6419. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  6420. wpa_tdls_enable(wpa_s->wpa, 1);
  6421. #endif /* CONFIG_TDLS */
  6422. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  6423. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  6424. wpa_s->no_keep_alive = 0;
  6425. wpa_s->own_disconnect_req = 0;
  6426. os_free(wpa_s->disallow_aps_bssid);
  6427. wpa_s->disallow_aps_bssid = NULL;
  6428. wpa_s->disallow_aps_bssid_count = 0;
  6429. os_free(wpa_s->disallow_aps_ssid);
  6430. wpa_s->disallow_aps_ssid = NULL;
  6431. wpa_s->disallow_aps_ssid_count = 0;
  6432. wpa_s->set_sta_uapsd = 0;
  6433. wpa_s->sta_uapsd = 0;
  6434. wpa_drv_radio_disable(wpa_s, 0);
  6435. wpa_blacklist_clear(wpa_s);
  6436. wpa_s->extra_blacklist_count = 0;
  6437. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  6438. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  6439. wpa_config_flush_blobs(wpa_s->conf);
  6440. wpa_s->conf->auto_interworking = 0;
  6441. wpa_s->conf->okc = 0;
  6442. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  6443. rsn_preauth_deinit(wpa_s->wpa);
  6444. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  6445. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  6446. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  6447. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  6448. radio_remove_works(wpa_s, NULL, 1);
  6449. wpa_s->ext_work_in_progress = 0;
  6450. wpa_s->next_ssid = NULL;
  6451. #ifdef CONFIG_INTERWORKING
  6452. #ifdef CONFIG_HS20
  6453. hs20_cancel_fetch_osu(wpa_s);
  6454. hs20_del_icon(wpa_s, NULL, NULL);
  6455. #endif /* CONFIG_HS20 */
  6456. #endif /* CONFIG_INTERWORKING */
  6457. wpa_s->ext_mgmt_frame_handling = 0;
  6458. wpa_s->ext_eapol_frame_io = 0;
  6459. #ifdef CONFIG_TESTING_OPTIONS
  6460. wpa_s->extra_roc_dur = 0;
  6461. wpa_s->test_failure = WPAS_TEST_FAILURE_NONE;
  6462. wpa_s->p2p_go_csa_on_inv = 0;
  6463. wpa_s->ignore_auth_resp = 0;
  6464. wpa_s->ignore_assoc_disallow = 0;
  6465. wpa_s->reject_btm_req_reason = 0;
  6466. wpa_sm_set_test_assoc_ie(wpa_s->wpa, NULL);
  6467. os_free(wpa_s->get_pref_freq_list_override);
  6468. wpa_s->get_pref_freq_list_override = NULL;
  6469. #endif /* CONFIG_TESTING_OPTIONS */
  6470. wpa_s->disconnected = 0;
  6471. os_free(wpa_s->next_scan_freqs);
  6472. wpa_s->next_scan_freqs = NULL;
  6473. os_free(wpa_s->select_network_scan_freqs);
  6474. wpa_s->select_network_scan_freqs = NULL;
  6475. wpa_bss_flush(wpa_s);
  6476. if (!dl_list_empty(&wpa_s->bss)) {
  6477. wpa_printf(MSG_DEBUG,
  6478. "BSS table not empty after flush: %u entries, current_bss=%p bssid="
  6479. MACSTR " pending_bssid=" MACSTR,
  6480. dl_list_len(&wpa_s->bss), wpa_s->current_bss,
  6481. MAC2STR(wpa_s->bssid),
  6482. MAC2STR(wpa_s->pending_bssid));
  6483. }
  6484. eloop_cancel_timeout(wpas_network_reenabled, wpa_s, NULL);
  6485. wpa_s->wnmsleep_used = 0;
  6486. #ifdef CONFIG_SME
  6487. wpa_s->sme.last_unprot_disconnect.sec = 0;
  6488. #endif /* CONFIG_SME */
  6489. wpabuf_free(wpa_s->ric_ies);
  6490. wpa_s->ric_ies = NULL;
  6491. }
  6492. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  6493. char *buf, size_t buflen)
  6494. {
  6495. struct wpa_radio_work *work;
  6496. char *pos, *end;
  6497. struct os_reltime now, diff;
  6498. pos = buf;
  6499. end = buf + buflen;
  6500. os_get_reltime(&now);
  6501. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  6502. {
  6503. int ret;
  6504. os_reltime_sub(&now, &work->time, &diff);
  6505. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  6506. work->type, work->wpa_s->ifname, work->freq,
  6507. work->started, diff.sec, diff.usec);
  6508. if (os_snprintf_error(end - pos, ret))
  6509. break;
  6510. pos += ret;
  6511. }
  6512. return pos - buf;
  6513. }
  6514. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  6515. {
  6516. struct wpa_radio_work *work = eloop_ctx;
  6517. struct wpa_external_work *ework = work->ctx;
  6518. wpa_dbg(work->wpa_s, MSG_DEBUG,
  6519. "Timing out external radio work %u (%s)",
  6520. ework->id, work->type);
  6521. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  6522. work->wpa_s->ext_work_in_progress = 0;
  6523. radio_work_done(work);
  6524. os_free(ework);
  6525. }
  6526. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  6527. {
  6528. struct wpa_external_work *ework = work->ctx;
  6529. if (deinit) {
  6530. if (work->started)
  6531. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6532. work, NULL);
  6533. /*
  6534. * work->type points to a buffer in ework, so need to replace
  6535. * that here with a fixed string to avoid use of freed memory
  6536. * in debug prints.
  6537. */
  6538. work->type = "freed-ext-work";
  6539. work->ctx = NULL;
  6540. os_free(ework);
  6541. return;
  6542. }
  6543. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  6544. ework->id, ework->type);
  6545. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  6546. work->wpa_s->ext_work_in_progress = 1;
  6547. if (!ework->timeout)
  6548. ework->timeout = 10;
  6549. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  6550. work, NULL);
  6551. }
  6552. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  6553. char *buf, size_t buflen)
  6554. {
  6555. struct wpa_external_work *ework;
  6556. char *pos, *pos2;
  6557. size_t type_len;
  6558. int ret;
  6559. unsigned int freq = 0;
  6560. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  6561. ework = os_zalloc(sizeof(*ework));
  6562. if (ework == NULL)
  6563. return -1;
  6564. pos = os_strchr(cmd, ' ');
  6565. if (pos) {
  6566. type_len = pos - cmd;
  6567. pos++;
  6568. pos2 = os_strstr(pos, "freq=");
  6569. if (pos2)
  6570. freq = atoi(pos2 + 5);
  6571. pos2 = os_strstr(pos, "timeout=");
  6572. if (pos2)
  6573. ework->timeout = atoi(pos2 + 8);
  6574. } else {
  6575. type_len = os_strlen(cmd);
  6576. }
  6577. if (4 + type_len >= sizeof(ework->type))
  6578. type_len = sizeof(ework->type) - 4 - 1;
  6579. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  6580. os_memcpy(ework->type + 4, cmd, type_len);
  6581. ework->type[4 + type_len] = '\0';
  6582. wpa_s->ext_work_id++;
  6583. if (wpa_s->ext_work_id == 0)
  6584. wpa_s->ext_work_id++;
  6585. ework->id = wpa_s->ext_work_id;
  6586. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  6587. ework) < 0) {
  6588. os_free(ework);
  6589. return -1;
  6590. }
  6591. ret = os_snprintf(buf, buflen, "%u", ework->id);
  6592. if (os_snprintf_error(buflen, ret))
  6593. return -1;
  6594. return ret;
  6595. }
  6596. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  6597. {
  6598. struct wpa_radio_work *work;
  6599. unsigned int id = atoi(cmd);
  6600. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  6601. {
  6602. struct wpa_external_work *ework;
  6603. if (os_strncmp(work->type, "ext:", 4) != 0)
  6604. continue;
  6605. ework = work->ctx;
  6606. if (id && ework->id != id)
  6607. continue;
  6608. wpa_dbg(wpa_s, MSG_DEBUG,
  6609. "Completed external radio work %u (%s)",
  6610. ework->id, ework->type);
  6611. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  6612. wpa_s->ext_work_in_progress = 0;
  6613. radio_work_done(work);
  6614. os_free(ework);
  6615. return 3; /* "OK\n" */
  6616. }
  6617. return -1;
  6618. }
  6619. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  6620. char *buf, size_t buflen)
  6621. {
  6622. if (os_strcmp(cmd, "show") == 0)
  6623. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  6624. if (os_strncmp(cmd, "add ", 4) == 0)
  6625. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  6626. if (os_strncmp(cmd, "done ", 5) == 0)
  6627. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  6628. return -1;
  6629. }
  6630. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  6631. {
  6632. struct wpa_radio_work *work, *tmp;
  6633. if (!wpa_s || !wpa_s->radio)
  6634. return;
  6635. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  6636. struct wpa_radio_work, list) {
  6637. struct wpa_external_work *ework;
  6638. if (os_strncmp(work->type, "ext:", 4) != 0)
  6639. continue;
  6640. ework = work->ctx;
  6641. wpa_dbg(wpa_s, MSG_DEBUG,
  6642. "Flushing%s external radio work %u (%s)",
  6643. work->started ? " started" : "", ework->id,
  6644. ework->type);
  6645. if (work->started)
  6646. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  6647. work, NULL);
  6648. radio_work_done(work);
  6649. os_free(ework);
  6650. }
  6651. }
  6652. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  6653. {
  6654. struct wpa_supplicant *wpa_s = eloop_ctx;
  6655. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  6656. }
  6657. static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value,
  6658. unsigned int *scan_id_count, int scan_id[])
  6659. {
  6660. const char *pos = value;
  6661. while (pos) {
  6662. if (*pos == ' ' || *pos == '\0')
  6663. break;
  6664. if (*scan_id_count == MAX_SCAN_ID)
  6665. return -1;
  6666. scan_id[(*scan_id_count)++] = atoi(pos);
  6667. pos = os_strchr(pos, ',');
  6668. if (pos)
  6669. pos++;
  6670. }
  6671. return 0;
  6672. }
  6673. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  6674. char *reply, int reply_size, int *reply_len)
  6675. {
  6676. char *pos;
  6677. unsigned int manual_scan_passive = 0;
  6678. unsigned int manual_scan_use_id = 0;
  6679. unsigned int manual_scan_only_new = 0;
  6680. unsigned int scan_only = 0;
  6681. unsigned int scan_id_count = 0;
  6682. int scan_id[MAX_SCAN_ID];
  6683. void (*scan_res_handler)(struct wpa_supplicant *wpa_s,
  6684. struct wpa_scan_results *scan_res);
  6685. int *manual_scan_freqs = NULL;
  6686. struct wpa_ssid_value *ssid = NULL, *ns;
  6687. unsigned int ssid_count = 0;
  6688. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  6689. *reply_len = -1;
  6690. return;
  6691. }
  6692. if (radio_work_pending(wpa_s, "scan")) {
  6693. wpa_printf(MSG_DEBUG,
  6694. "Pending scan scheduled - reject new request");
  6695. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6696. return;
  6697. }
  6698. #ifdef CONFIG_INTERWORKING
  6699. if (wpa_s->fetch_anqp_in_progress || wpa_s->network_select) {
  6700. wpa_printf(MSG_DEBUG,
  6701. "Interworking select in progress - reject new scan");
  6702. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6703. return;
  6704. }
  6705. #endif /* CONFIG_INTERWORKING */
  6706. if (params) {
  6707. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  6708. scan_only = 1;
  6709. pos = os_strstr(params, "freq=");
  6710. if (pos) {
  6711. manual_scan_freqs = freq_range_to_channel_list(wpa_s,
  6712. pos + 5);
  6713. if (manual_scan_freqs == NULL) {
  6714. *reply_len = -1;
  6715. goto done;
  6716. }
  6717. }
  6718. pos = os_strstr(params, "passive=");
  6719. if (pos)
  6720. manual_scan_passive = !!atoi(pos + 8);
  6721. pos = os_strstr(params, "use_id=");
  6722. if (pos)
  6723. manual_scan_use_id = atoi(pos + 7);
  6724. pos = os_strstr(params, "only_new=1");
  6725. if (pos)
  6726. manual_scan_only_new = 1;
  6727. pos = os_strstr(params, "scan_id=");
  6728. if (pos && scan_id_list_parse(wpa_s, pos + 8, &scan_id_count,
  6729. scan_id) < 0) {
  6730. *reply_len = -1;
  6731. goto done;
  6732. }
  6733. pos = params;
  6734. while (pos && *pos != '\0') {
  6735. if (os_strncmp(pos, "ssid ", 5) == 0) {
  6736. char *end;
  6737. pos += 5;
  6738. end = pos;
  6739. while (*end) {
  6740. if (*end == '\0' || *end == ' ')
  6741. break;
  6742. end++;
  6743. }
  6744. ns = os_realloc_array(
  6745. ssid, ssid_count + 1,
  6746. sizeof(struct wpa_ssid_value));
  6747. if (ns == NULL) {
  6748. *reply_len = -1;
  6749. goto done;
  6750. }
  6751. ssid = ns;
  6752. if ((end - pos) & 0x01 ||
  6753. end - pos > 2 * SSID_MAX_LEN ||
  6754. hexstr2bin(pos, ssid[ssid_count].ssid,
  6755. (end - pos) / 2) < 0) {
  6756. wpa_printf(MSG_DEBUG,
  6757. "Invalid SSID value '%s'",
  6758. pos);
  6759. *reply_len = -1;
  6760. goto done;
  6761. }
  6762. ssid[ssid_count].ssid_len = (end - pos) / 2;
  6763. wpa_hexdump_ascii(MSG_DEBUG, "scan SSID",
  6764. ssid[ssid_count].ssid,
  6765. ssid[ssid_count].ssid_len);
  6766. ssid_count++;
  6767. pos = end;
  6768. }
  6769. pos = os_strchr(pos, ' ');
  6770. if (pos)
  6771. pos++;
  6772. }
  6773. }
  6774. wpa_s->num_ssids_from_scan_req = ssid_count;
  6775. os_free(wpa_s->ssids_from_scan_req);
  6776. if (ssid_count) {
  6777. wpa_s->ssids_from_scan_req = ssid;
  6778. ssid = NULL;
  6779. } else {
  6780. wpa_s->ssids_from_scan_req = NULL;
  6781. }
  6782. if (scan_only)
  6783. scan_res_handler = scan_only_handler;
  6784. else if (wpa_s->scan_res_handler == scan_only_handler)
  6785. scan_res_handler = NULL;
  6786. else
  6787. scan_res_handler = wpa_s->scan_res_handler;
  6788. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  6789. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  6790. (wpa_s->wpa_state == WPA_COMPLETED))) {
  6791. wpa_s->manual_scan_passive = manual_scan_passive;
  6792. wpa_s->manual_scan_use_id = manual_scan_use_id;
  6793. wpa_s->manual_scan_only_new = manual_scan_only_new;
  6794. wpa_s->scan_id_count = scan_id_count;
  6795. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  6796. wpa_s->scan_res_handler = scan_res_handler;
  6797. os_free(wpa_s->manual_scan_freqs);
  6798. wpa_s->manual_scan_freqs = manual_scan_freqs;
  6799. manual_scan_freqs = NULL;
  6800. wpa_s->normal_scans = 0;
  6801. wpa_s->scan_req = MANUAL_SCAN_REQ;
  6802. wpa_s->after_wps = 0;
  6803. wpa_s->known_wps_freq = 0;
  6804. wpa_supplicant_req_scan(wpa_s, 0, 0);
  6805. if (wpa_s->manual_scan_use_id) {
  6806. wpa_s->manual_scan_id++;
  6807. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  6808. wpa_s->manual_scan_id);
  6809. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  6810. wpa_s->manual_scan_id);
  6811. }
  6812. } else if (wpa_s->sched_scanning) {
  6813. wpa_s->manual_scan_passive = manual_scan_passive;
  6814. wpa_s->manual_scan_use_id = manual_scan_use_id;
  6815. wpa_s->manual_scan_only_new = manual_scan_only_new;
  6816. wpa_s->scan_id_count = scan_id_count;
  6817. os_memcpy(wpa_s->scan_id, scan_id, scan_id_count * sizeof(int));
  6818. wpa_s->scan_res_handler = scan_res_handler;
  6819. os_free(wpa_s->manual_scan_freqs);
  6820. wpa_s->manual_scan_freqs = manual_scan_freqs;
  6821. manual_scan_freqs = NULL;
  6822. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  6823. wpa_supplicant_cancel_sched_scan(wpa_s);
  6824. wpa_s->scan_req = MANUAL_SCAN_REQ;
  6825. wpa_supplicant_req_scan(wpa_s, 0, 0);
  6826. if (wpa_s->manual_scan_use_id) {
  6827. wpa_s->manual_scan_id++;
  6828. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  6829. wpa_s->manual_scan_id);
  6830. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  6831. wpa_s->manual_scan_id);
  6832. }
  6833. } else {
  6834. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  6835. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  6836. }
  6837. done:
  6838. os_free(manual_scan_freqs);
  6839. os_free(ssid);
  6840. }
  6841. #ifdef CONFIG_TESTING_OPTIONS
  6842. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  6843. unsigned int freq, const u8 *dst,
  6844. const u8 *src, const u8 *bssid,
  6845. const u8 *data, size_t data_len,
  6846. enum offchannel_send_action_result
  6847. result)
  6848. {
  6849. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  6850. " src=" MACSTR " bssid=" MACSTR " result=%s",
  6851. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  6852. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  6853. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  6854. "NO_ACK" : "FAILED"));
  6855. }
  6856. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  6857. {
  6858. char *pos, *param;
  6859. size_t len;
  6860. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  6861. int res, used;
  6862. int freq = 0, no_cck = 0, wait_time = 0;
  6863. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  6864. * <action=Action frame payload> */
  6865. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  6866. pos = cmd;
  6867. used = hwaddr_aton2(pos, da);
  6868. if (used < 0)
  6869. return -1;
  6870. pos += used;
  6871. while (*pos == ' ')
  6872. pos++;
  6873. used = hwaddr_aton2(pos, bssid);
  6874. if (used < 0)
  6875. return -1;
  6876. pos += used;
  6877. param = os_strstr(pos, " freq=");
  6878. if (param) {
  6879. param += 6;
  6880. freq = atoi(param);
  6881. }
  6882. param = os_strstr(pos, " no_cck=");
  6883. if (param) {
  6884. param += 8;
  6885. no_cck = atoi(param);
  6886. }
  6887. param = os_strstr(pos, " wait_time=");
  6888. if (param) {
  6889. param += 11;
  6890. wait_time = atoi(param);
  6891. }
  6892. param = os_strstr(pos, " action=");
  6893. if (param == NULL)
  6894. return -1;
  6895. param += 8;
  6896. len = os_strlen(param);
  6897. if (len & 1)
  6898. return -1;
  6899. len /= 2;
  6900. buf = os_malloc(len);
  6901. if (buf == NULL)
  6902. return -1;
  6903. if (hexstr2bin(param, buf, len) < 0) {
  6904. os_free(buf);
  6905. return -1;
  6906. }
  6907. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  6908. buf, len, wait_time,
  6909. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  6910. os_free(buf);
  6911. return res;
  6912. }
  6913. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  6914. {
  6915. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  6916. offchannel_send_action_done(wpa_s);
  6917. }
  6918. static int wpas_ctrl_iface_mgmt_rx_process(struct wpa_supplicant *wpa_s,
  6919. char *cmd)
  6920. {
  6921. char *pos, *param;
  6922. size_t len;
  6923. u8 *buf;
  6924. int freq = 0, datarate = 0, ssi_signal = 0;
  6925. union wpa_event_data event;
  6926. if (!wpa_s->ext_mgmt_frame_handling)
  6927. return -1;
  6928. /* freq=<MHz> datarate=<val> ssi_signal=<val> frame=<frame hexdump> */
  6929. wpa_printf(MSG_DEBUG, "External MGMT RX process: %s", cmd);
  6930. pos = cmd;
  6931. param = os_strstr(pos, "freq=");
  6932. if (param) {
  6933. param += 5;
  6934. freq = atoi(param);
  6935. }
  6936. param = os_strstr(pos, " datarate=");
  6937. if (param) {
  6938. param += 10;
  6939. datarate = atoi(param);
  6940. }
  6941. param = os_strstr(pos, " ssi_signal=");
  6942. if (param) {
  6943. param += 12;
  6944. ssi_signal = atoi(param);
  6945. }
  6946. param = os_strstr(pos, " frame=");
  6947. if (param == NULL)
  6948. return -1;
  6949. param += 7;
  6950. len = os_strlen(param);
  6951. if (len & 1)
  6952. return -1;
  6953. len /= 2;
  6954. buf = os_malloc(len);
  6955. if (buf == NULL)
  6956. return -1;
  6957. if (hexstr2bin(param, buf, len) < 0) {
  6958. os_free(buf);
  6959. return -1;
  6960. }
  6961. os_memset(&event, 0, sizeof(event));
  6962. event.rx_mgmt.freq = freq;
  6963. event.rx_mgmt.frame = buf;
  6964. event.rx_mgmt.frame_len = len;
  6965. event.rx_mgmt.ssi_signal = ssi_signal;
  6966. event.rx_mgmt.datarate = datarate;
  6967. wpa_s->ext_mgmt_frame_handling = 0;
  6968. wpa_supplicant_event(wpa_s, EVENT_RX_MGMT, &event);
  6969. wpa_s->ext_mgmt_frame_handling = 1;
  6970. os_free(buf);
  6971. return 0;
  6972. }
  6973. static int wpas_ctrl_iface_driver_scan_res(struct wpa_supplicant *wpa_s,
  6974. char *param)
  6975. {
  6976. struct wpa_scan_res *res;
  6977. struct os_reltime now;
  6978. char *pos, *end;
  6979. int ret = -1;
  6980. if (!param)
  6981. return -1;
  6982. if (os_strcmp(param, "START") == 0) {
  6983. wpa_bss_update_start(wpa_s);
  6984. return 0;
  6985. }
  6986. if (os_strcmp(param, "END") == 0) {
  6987. wpa_bss_update_end(wpa_s, NULL, 1);
  6988. return 0;
  6989. }
  6990. if (os_strncmp(param, "BSS ", 4) != 0)
  6991. return -1;
  6992. param += 3;
  6993. res = os_zalloc(sizeof(*res) + os_strlen(param) / 2);
  6994. if (!res)
  6995. return -1;
  6996. pos = os_strstr(param, " flags=");
  6997. if (pos)
  6998. res->flags = strtol(pos + 7, NULL, 16);
  6999. pos = os_strstr(param, " bssid=");
  7000. if (pos && hwaddr_aton(pos + 7, res->bssid))
  7001. goto fail;
  7002. pos = os_strstr(param, " freq=");
  7003. if (pos)
  7004. res->freq = atoi(pos + 6);
  7005. pos = os_strstr(param, " beacon_int=");
  7006. if (pos)
  7007. res->beacon_int = atoi(pos + 12);
  7008. pos = os_strstr(param, " caps=");
  7009. if (pos)
  7010. res->caps = strtol(pos + 6, NULL, 16);
  7011. pos = os_strstr(param, " qual=");
  7012. if (pos)
  7013. res->qual = atoi(pos + 6);
  7014. pos = os_strstr(param, " noise=");
  7015. if (pos)
  7016. res->noise = atoi(pos + 7);
  7017. pos = os_strstr(param, " level=");
  7018. if (pos)
  7019. res->level = atoi(pos + 7);
  7020. pos = os_strstr(param, " tsf=");
  7021. if (pos)
  7022. res->tsf = strtoll(pos + 5, NULL, 16);
  7023. pos = os_strstr(param, " age=");
  7024. if (pos)
  7025. res->age = atoi(pos + 5);
  7026. pos = os_strstr(param, " est_throughput=");
  7027. if (pos)
  7028. res->est_throughput = atoi(pos + 16);
  7029. pos = os_strstr(param, " snr=");
  7030. if (pos)
  7031. res->snr = atoi(pos + 5);
  7032. pos = os_strstr(param, " parent_tsf=");
  7033. if (pos)
  7034. res->parent_tsf = strtoll(pos + 7, NULL, 16);
  7035. pos = os_strstr(param, " tsf_bssid=");
  7036. if (pos && hwaddr_aton(pos + 11, res->tsf_bssid))
  7037. goto fail;
  7038. pos = os_strstr(param, " ie=");
  7039. if (pos) {
  7040. pos += 4;
  7041. end = os_strchr(pos, ' ');
  7042. if (!end)
  7043. end = pos + os_strlen(pos);
  7044. res->ie_len = (end - pos) / 2;
  7045. if (hexstr2bin(pos, (u8 *) (res + 1), res->ie_len))
  7046. goto fail;
  7047. }
  7048. pos = os_strstr(param, " beacon_ie=");
  7049. if (pos) {
  7050. pos += 11;
  7051. end = os_strchr(pos, ' ');
  7052. if (!end)
  7053. end = pos + os_strlen(pos);
  7054. res->beacon_ie_len = (end - pos) / 2;
  7055. if (hexstr2bin(pos, ((u8 *) (res + 1)) + res->ie_len,
  7056. res->beacon_ie_len))
  7057. goto fail;
  7058. }
  7059. os_get_reltime(&now);
  7060. wpa_bss_update_scan_res(wpa_s, res, &now);
  7061. ret = 0;
  7062. fail:
  7063. os_free(res);
  7064. return ret;
  7065. }
  7066. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  7067. {
  7068. char *pos, *param;
  7069. union wpa_event_data event;
  7070. enum wpa_event_type ev;
  7071. /* <event name> [parameters..] */
  7072. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  7073. pos = cmd;
  7074. param = os_strchr(pos, ' ');
  7075. if (param)
  7076. *param++ = '\0';
  7077. os_memset(&event, 0, sizeof(event));
  7078. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  7079. ev = EVENT_INTERFACE_ENABLED;
  7080. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  7081. ev = EVENT_INTERFACE_DISABLED;
  7082. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  7083. ev = EVENT_AVOID_FREQUENCIES;
  7084. if (param == NULL)
  7085. param = "";
  7086. if (freq_range_list_parse(&event.freq_range, param) < 0)
  7087. return -1;
  7088. wpa_supplicant_event(wpa_s, ev, &event);
  7089. os_free(event.freq_range.range);
  7090. return 0;
  7091. } else if (os_strcmp(cmd, "SCAN_RES") == 0) {
  7092. return wpas_ctrl_iface_driver_scan_res(wpa_s, param);
  7093. } else {
  7094. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  7095. cmd);
  7096. return -1;
  7097. }
  7098. wpa_supplicant_event(wpa_s, ev, &event);
  7099. return 0;
  7100. }
  7101. static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
  7102. {
  7103. char *pos;
  7104. u8 src[ETH_ALEN], *buf;
  7105. int used;
  7106. size_t len;
  7107. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  7108. pos = cmd;
  7109. used = hwaddr_aton2(pos, src);
  7110. if (used < 0)
  7111. return -1;
  7112. pos += used;
  7113. while (*pos == ' ')
  7114. pos++;
  7115. len = os_strlen(pos);
  7116. if (len & 1)
  7117. return -1;
  7118. len /= 2;
  7119. buf = os_malloc(len);
  7120. if (buf == NULL)
  7121. return -1;
  7122. if (hexstr2bin(pos, buf, len) < 0) {
  7123. os_free(buf);
  7124. return -1;
  7125. }
  7126. wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
  7127. os_free(buf);
  7128. return 0;
  7129. }
  7130. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  7131. {
  7132. size_t i;
  7133. u32 sum = 0;
  7134. const u16 *pos = buf;
  7135. for (i = 0; i < len / 2; i++)
  7136. sum += *pos++;
  7137. while (sum >> 16)
  7138. sum = (sum & 0xffff) + (sum >> 16);
  7139. return sum ^ 0xffff;
  7140. }
  7141. #define HWSIM_PACKETLEN 1500
  7142. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  7143. static void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf,
  7144. size_t len)
  7145. {
  7146. struct wpa_supplicant *wpa_s = ctx;
  7147. const struct ether_header *eth;
  7148. struct iphdr ip;
  7149. const u8 *pos;
  7150. unsigned int i;
  7151. if (len != HWSIM_PACKETLEN)
  7152. return;
  7153. eth = (const struct ether_header *) buf;
  7154. os_memcpy(&ip, eth + 1, sizeof(ip));
  7155. pos = &buf[sizeof(*eth) + sizeof(ip)];
  7156. if (ip.ihl != 5 || ip.version != 4 ||
  7157. ntohs(ip.tot_len) != HWSIM_IP_LEN)
  7158. return;
  7159. for (i = 0; i < HWSIM_IP_LEN - sizeof(ip); i++) {
  7160. if (*pos != (u8) i)
  7161. return;
  7162. pos++;
  7163. }
  7164. wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  7165. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  7166. }
  7167. static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
  7168. char *cmd)
  7169. {
  7170. int enabled = atoi(cmd);
  7171. char *pos;
  7172. const char *ifname;
  7173. if (!enabled) {
  7174. if (wpa_s->l2_test) {
  7175. l2_packet_deinit(wpa_s->l2_test);
  7176. wpa_s->l2_test = NULL;
  7177. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
  7178. }
  7179. return 0;
  7180. }
  7181. if (wpa_s->l2_test)
  7182. return 0;
  7183. pos = os_strstr(cmd, " ifname=");
  7184. if (pos)
  7185. ifname = pos + 8;
  7186. else
  7187. ifname = wpa_s->ifname;
  7188. wpa_s->l2_test = l2_packet_init(ifname, wpa_s->own_addr,
  7189. ETHERTYPE_IP, wpas_data_test_rx,
  7190. wpa_s, 1);
  7191. if (wpa_s->l2_test == NULL)
  7192. return -1;
  7193. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
  7194. return 0;
  7195. }
  7196. static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
  7197. {
  7198. u8 dst[ETH_ALEN], src[ETH_ALEN];
  7199. char *pos;
  7200. int used;
  7201. long int val;
  7202. u8 tos;
  7203. u8 buf[2 + HWSIM_PACKETLEN];
  7204. struct ether_header *eth;
  7205. struct iphdr *ip;
  7206. u8 *dpos;
  7207. unsigned int i;
  7208. if (wpa_s->l2_test == NULL)
  7209. return -1;
  7210. /* format: <dst> <src> <tos> */
  7211. pos = cmd;
  7212. used = hwaddr_aton2(pos, dst);
  7213. if (used < 0)
  7214. return -1;
  7215. pos += used;
  7216. while (*pos == ' ')
  7217. pos++;
  7218. used = hwaddr_aton2(pos, src);
  7219. if (used < 0)
  7220. return -1;
  7221. pos += used;
  7222. val = strtol(pos, NULL, 0);
  7223. if (val < 0 || val > 0xff)
  7224. return -1;
  7225. tos = val;
  7226. eth = (struct ether_header *) &buf[2];
  7227. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  7228. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  7229. eth->ether_type = htons(ETHERTYPE_IP);
  7230. ip = (struct iphdr *) (eth + 1);
  7231. os_memset(ip, 0, sizeof(*ip));
  7232. ip->ihl = 5;
  7233. ip->version = 4;
  7234. ip->ttl = 64;
  7235. ip->tos = tos;
  7236. ip->tot_len = htons(HWSIM_IP_LEN);
  7237. ip->protocol = 1;
  7238. ip->saddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 1);
  7239. ip->daddr = htonl(192U << 24 | 168 << 16 | 1 << 8 | 2);
  7240. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  7241. dpos = (u8 *) (ip + 1);
  7242. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  7243. *dpos++ = i;
  7244. if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, &buf[2],
  7245. HWSIM_PACKETLEN) < 0)
  7246. return -1;
  7247. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
  7248. " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  7249. return 0;
  7250. }
  7251. static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
  7252. char *cmd)
  7253. {
  7254. u8 *buf;
  7255. struct ether_header *eth;
  7256. struct l2_packet_data *l2 = NULL;
  7257. size_t len;
  7258. u16 ethertype;
  7259. int res = -1;
  7260. len = os_strlen(cmd);
  7261. if (len & 1 || len < ETH_HLEN * 2)
  7262. return -1;
  7263. len /= 2;
  7264. buf = os_malloc(len);
  7265. if (buf == NULL)
  7266. return -1;
  7267. if (hexstr2bin(cmd, buf, len) < 0)
  7268. goto done;
  7269. eth = (struct ether_header *) buf;
  7270. ethertype = ntohs(eth->ether_type);
  7271. l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
  7272. wpas_data_test_rx, wpa_s, 1);
  7273. if (l2 == NULL)
  7274. goto done;
  7275. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  7276. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
  7277. done:
  7278. if (l2)
  7279. l2_packet_deinit(l2);
  7280. os_free(buf);
  7281. return res < 0 ? -1 : 0;
  7282. }
  7283. static int wpas_ctrl_test_alloc_fail(struct wpa_supplicant *wpa_s, char *cmd)
  7284. {
  7285. #ifdef WPA_TRACE_BFD
  7286. char *pos;
  7287. wpa_trace_fail_after = atoi(cmd);
  7288. pos = os_strchr(cmd, ':');
  7289. if (pos) {
  7290. pos++;
  7291. os_strlcpy(wpa_trace_fail_func, pos,
  7292. sizeof(wpa_trace_fail_func));
  7293. } else {
  7294. wpa_trace_fail_after = 0;
  7295. }
  7296. return 0;
  7297. #else /* WPA_TRACE_BFD */
  7298. return -1;
  7299. #endif /* WPA_TRACE_BFD */
  7300. }
  7301. static int wpas_ctrl_get_alloc_fail(struct wpa_supplicant *wpa_s,
  7302. char *buf, size_t buflen)
  7303. {
  7304. #ifdef WPA_TRACE_BFD
  7305. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_fail_after,
  7306. wpa_trace_fail_func);
  7307. #else /* WPA_TRACE_BFD */
  7308. return -1;
  7309. #endif /* WPA_TRACE_BFD */
  7310. }
  7311. static int wpas_ctrl_test_fail(struct wpa_supplicant *wpa_s, char *cmd)
  7312. {
  7313. #ifdef WPA_TRACE_BFD
  7314. char *pos;
  7315. wpa_trace_test_fail_after = atoi(cmd);
  7316. pos = os_strchr(cmd, ':');
  7317. if (pos) {
  7318. pos++;
  7319. os_strlcpy(wpa_trace_test_fail_func, pos,
  7320. sizeof(wpa_trace_test_fail_func));
  7321. } else {
  7322. wpa_trace_test_fail_after = 0;
  7323. }
  7324. return 0;
  7325. #else /* WPA_TRACE_BFD */
  7326. return -1;
  7327. #endif /* WPA_TRACE_BFD */
  7328. }
  7329. static int wpas_ctrl_get_fail(struct wpa_supplicant *wpa_s,
  7330. char *buf, size_t buflen)
  7331. {
  7332. #ifdef WPA_TRACE_BFD
  7333. return os_snprintf(buf, buflen, "%u:%s", wpa_trace_test_fail_after,
  7334. wpa_trace_test_fail_func);
  7335. #else /* WPA_TRACE_BFD */
  7336. return -1;
  7337. #endif /* WPA_TRACE_BFD */
  7338. }
  7339. static void wpas_ctrl_event_test_cb(void *eloop_ctx, void *timeout_ctx)
  7340. {
  7341. struct wpa_supplicant *wpa_s = eloop_ctx;
  7342. int i, count = (intptr_t) timeout_ctx;
  7343. wpa_printf(MSG_DEBUG, "TEST: Send %d control interface event messages",
  7344. count);
  7345. for (i = 0; i < count; i++) {
  7346. wpa_msg_ctrl(wpa_s, MSG_INFO, "TEST-EVENT-MESSAGE %d/%d",
  7347. i + 1, count);
  7348. }
  7349. }
  7350. static int wpas_ctrl_event_test(struct wpa_supplicant *wpa_s, const char *cmd)
  7351. {
  7352. int count;
  7353. count = atoi(cmd);
  7354. if (count <= 0)
  7355. return -1;
  7356. return eloop_register_timeout(0, 0, wpas_ctrl_event_test_cb, wpa_s,
  7357. (void *) (intptr_t) count);
  7358. }
  7359. static int wpas_ctrl_test_assoc_ie(struct wpa_supplicant *wpa_s,
  7360. const char *cmd)
  7361. {
  7362. struct wpabuf *buf;
  7363. size_t len;
  7364. len = os_strlen(cmd);
  7365. if (len & 1)
  7366. return -1;
  7367. len /= 2;
  7368. if (len == 0) {
  7369. buf = NULL;
  7370. } else {
  7371. buf = wpabuf_alloc(len);
  7372. if (buf == NULL)
  7373. return -1;
  7374. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  7375. wpabuf_free(buf);
  7376. return -1;
  7377. }
  7378. }
  7379. wpa_sm_set_test_assoc_ie(wpa_s->wpa, buf);
  7380. return 0;
  7381. }
  7382. #endif /* CONFIG_TESTING_OPTIONS */
  7383. static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
  7384. {
  7385. char *pos = cmd;
  7386. int frame;
  7387. size_t len;
  7388. struct wpabuf *buf;
  7389. struct ieee802_11_elems elems;
  7390. frame = atoi(pos);
  7391. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  7392. return -1;
  7393. wpa_s = wpas_vendor_elem(wpa_s, frame);
  7394. pos = os_strchr(pos, ' ');
  7395. if (pos == NULL)
  7396. return -1;
  7397. pos++;
  7398. len = os_strlen(pos);
  7399. if (len == 0)
  7400. return 0;
  7401. if (len & 1)
  7402. return -1;
  7403. len /= 2;
  7404. buf = wpabuf_alloc(len);
  7405. if (buf == NULL)
  7406. return -1;
  7407. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  7408. wpabuf_free(buf);
  7409. return -1;
  7410. }
  7411. if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
  7412. ParseFailed) {
  7413. wpabuf_free(buf);
  7414. return -1;
  7415. }
  7416. if (wpa_s->vendor_elem[frame] == NULL) {
  7417. wpa_s->vendor_elem[frame] = buf;
  7418. wpas_vendor_elem_update(wpa_s);
  7419. return 0;
  7420. }
  7421. if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
  7422. wpabuf_free(buf);
  7423. return -1;
  7424. }
  7425. wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
  7426. wpabuf_free(buf);
  7427. wpas_vendor_elem_update(wpa_s);
  7428. return 0;
  7429. }
  7430. static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
  7431. char *buf, size_t buflen)
  7432. {
  7433. int frame = atoi(cmd);
  7434. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  7435. return -1;
  7436. wpa_s = wpas_vendor_elem(wpa_s, frame);
  7437. if (wpa_s->vendor_elem[frame] == NULL)
  7438. return 0;
  7439. return wpa_snprintf_hex(buf, buflen,
  7440. wpabuf_head_u8(wpa_s->vendor_elem[frame]),
  7441. wpabuf_len(wpa_s->vendor_elem[frame]));
  7442. }
  7443. static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
  7444. {
  7445. char *pos = cmd;
  7446. int frame;
  7447. size_t len;
  7448. u8 *buf;
  7449. struct ieee802_11_elems elems;
  7450. int res;
  7451. frame = atoi(pos);
  7452. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  7453. return -1;
  7454. wpa_s = wpas_vendor_elem(wpa_s, frame);
  7455. pos = os_strchr(pos, ' ');
  7456. if (pos == NULL)
  7457. return -1;
  7458. pos++;
  7459. if (*pos == '*') {
  7460. wpabuf_free(wpa_s->vendor_elem[frame]);
  7461. wpa_s->vendor_elem[frame] = NULL;
  7462. wpas_vendor_elem_update(wpa_s);
  7463. return 0;
  7464. }
  7465. if (wpa_s->vendor_elem[frame] == NULL)
  7466. return -1;
  7467. len = os_strlen(pos);
  7468. if (len == 0)
  7469. return 0;
  7470. if (len & 1)
  7471. return -1;
  7472. len /= 2;
  7473. buf = os_malloc(len);
  7474. if (buf == NULL)
  7475. return -1;
  7476. if (hexstr2bin(pos, buf, len) < 0) {
  7477. os_free(buf);
  7478. return -1;
  7479. }
  7480. if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
  7481. os_free(buf);
  7482. return -1;
  7483. }
  7484. res = wpas_vendor_elem_remove(wpa_s, frame, buf, len);
  7485. os_free(buf);
  7486. return res;
  7487. }
  7488. static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
  7489. {
  7490. struct wpa_supplicant *wpa_s = ctx;
  7491. size_t len;
  7492. const u8 *data;
  7493. /*
  7494. * Neighbor Report element (IEEE P802.11-REVmc/D5.0)
  7495. * BSSID[6]
  7496. * BSSID Information[4]
  7497. * Operating Class[1]
  7498. * Channel Number[1]
  7499. * PHY Type[1]
  7500. * Optional Subelements[variable]
  7501. */
  7502. #define NR_IE_MIN_LEN (ETH_ALEN + 4 + 1 + 1 + 1)
  7503. if (!neighbor_rep || wpabuf_len(neighbor_rep) == 0) {
  7504. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
  7505. goto out;
  7506. }
  7507. data = wpabuf_head_u8(neighbor_rep);
  7508. len = wpabuf_len(neighbor_rep);
  7509. while (len >= 2 + NR_IE_MIN_LEN) {
  7510. const u8 *nr;
  7511. char lci[256 * 2 + 1];
  7512. char civic[256 * 2 + 1];
  7513. u8 nr_len = data[1];
  7514. const u8 *pos = data, *end;
  7515. if (pos[0] != WLAN_EID_NEIGHBOR_REPORT ||
  7516. nr_len < NR_IE_MIN_LEN) {
  7517. wpa_printf(MSG_DEBUG,
  7518. "CTRL: Invalid Neighbor Report element: id=%u len=%u",
  7519. data[0], nr_len);
  7520. goto out;
  7521. }
  7522. if (2U + nr_len > len) {
  7523. wpa_printf(MSG_DEBUG,
  7524. "CTRL: Invalid Neighbor Report element: id=%u len=%zu nr_len=%u",
  7525. data[0], len, nr_len);
  7526. goto out;
  7527. }
  7528. pos += 2;
  7529. end = pos + nr_len;
  7530. nr = pos;
  7531. pos += NR_IE_MIN_LEN;
  7532. lci[0] = '\0';
  7533. civic[0] = '\0';
  7534. while (end - pos > 2) {
  7535. u8 s_id, s_len;
  7536. s_id = *pos++;
  7537. s_len = *pos++;
  7538. if (s_len > end - pos)
  7539. goto out;
  7540. if (s_id == WLAN_EID_MEASURE_REPORT && s_len > 3) {
  7541. /* Measurement Token[1] */
  7542. /* Measurement Report Mode[1] */
  7543. /* Measurement Type[1] */
  7544. /* Measurement Report[variable] */
  7545. switch (pos[2]) {
  7546. case MEASURE_TYPE_LCI:
  7547. if (lci[0])
  7548. break;
  7549. wpa_snprintf_hex(lci, sizeof(lci),
  7550. pos, s_len);
  7551. break;
  7552. case MEASURE_TYPE_LOCATION_CIVIC:
  7553. if (civic[0])
  7554. break;
  7555. wpa_snprintf_hex(civic, sizeof(civic),
  7556. pos, s_len);
  7557. break;
  7558. }
  7559. }
  7560. pos += s_len;
  7561. }
  7562. wpa_msg(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
  7563. "bssid=" MACSTR
  7564. " info=0x%x op_class=%u chan=%u phy_type=%u%s%s%s%s",
  7565. MAC2STR(nr), WPA_GET_LE32(nr + ETH_ALEN),
  7566. nr[ETH_ALEN + 4], nr[ETH_ALEN + 5],
  7567. nr[ETH_ALEN + 6],
  7568. lci[0] ? " lci=" : "", lci,
  7569. civic[0] ? " civic=" : "", civic);
  7570. data = end;
  7571. len -= 2 + nr_len;
  7572. }
  7573. out:
  7574. wpabuf_free(neighbor_rep);
  7575. }
  7576. static int wpas_ctrl_iface_send_neighbor_rep(struct wpa_supplicant *wpa_s,
  7577. char *cmd)
  7578. {
  7579. struct wpa_ssid_value ssid, *ssid_p = NULL;
  7580. int ret, lci = 0, civic = 0;
  7581. char *ssid_s;
  7582. ssid_s = os_strstr(cmd, "ssid=");
  7583. if (ssid_s) {
  7584. if (ssid_parse(ssid_s + 5, &ssid)) {
  7585. wpa_printf(MSG_ERROR,
  7586. "CTRL: Send Neighbor Report: bad SSID");
  7587. return -1;
  7588. }
  7589. ssid_p = &ssid;
  7590. /*
  7591. * Move cmd after the SSID text that may include "lci" or
  7592. * "civic".
  7593. */
  7594. cmd = os_strchr(ssid_s + 6, ssid_s[5] == '"' ? '"' : ' ');
  7595. if (cmd)
  7596. cmd++;
  7597. }
  7598. if (cmd && os_strstr(cmd, "lci"))
  7599. lci = 1;
  7600. if (cmd && os_strstr(cmd, "civic"))
  7601. civic = 1;
  7602. ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p, lci, civic,
  7603. wpas_ctrl_neighbor_rep_cb,
  7604. wpa_s);
  7605. return ret;
  7606. }
  7607. static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
  7608. {
  7609. eapol_sm_erp_flush(wpa_s->eapol);
  7610. return 0;
  7611. }
  7612. static int wpas_ctrl_iface_mac_rand_scan(struct wpa_supplicant *wpa_s,
  7613. char *cmd)
  7614. {
  7615. char *token, *context = NULL;
  7616. unsigned int enable = ~0, type = 0;
  7617. u8 _addr[ETH_ALEN], _mask[ETH_ALEN];
  7618. u8 *addr = NULL, *mask = NULL;
  7619. while ((token = str_token(cmd, " ", &context))) {
  7620. if (os_strcasecmp(token, "scan") == 0) {
  7621. type |= MAC_ADDR_RAND_SCAN;
  7622. } else if (os_strcasecmp(token, "sched") == 0) {
  7623. type |= MAC_ADDR_RAND_SCHED_SCAN;
  7624. } else if (os_strcasecmp(token, "pno") == 0) {
  7625. type |= MAC_ADDR_RAND_PNO;
  7626. } else if (os_strcasecmp(token, "all") == 0) {
  7627. type = wpa_s->mac_addr_rand_supported;
  7628. } else if (os_strncasecmp(token, "enable=", 7) == 0) {
  7629. enable = atoi(token + 7);
  7630. } else if (os_strncasecmp(token, "addr=", 5) == 0) {
  7631. addr = _addr;
  7632. if (hwaddr_aton(token + 5, addr)) {
  7633. wpa_printf(MSG_INFO,
  7634. "CTRL: Invalid MAC address: %s",
  7635. token);
  7636. return -1;
  7637. }
  7638. } else if (os_strncasecmp(token, "mask=", 5) == 0) {
  7639. mask = _mask;
  7640. if (hwaddr_aton(token + 5, mask)) {
  7641. wpa_printf(MSG_INFO,
  7642. "CTRL: Invalid MAC address mask: %s",
  7643. token);
  7644. return -1;
  7645. }
  7646. } else {
  7647. wpa_printf(MSG_INFO,
  7648. "CTRL: Invalid MAC_RAND_SCAN parameter: %s",
  7649. token);
  7650. return -1;
  7651. }
  7652. }
  7653. if (!type) {
  7654. wpa_printf(MSG_INFO, "CTRL: MAC_RAND_SCAN no type specified");
  7655. return -1;
  7656. }
  7657. if ((wpa_s->mac_addr_rand_supported & type) != type) {
  7658. wpa_printf(MSG_INFO,
  7659. "CTRL: MAC_RAND_SCAN types=%u != supported=%u",
  7660. type, wpa_s->mac_addr_rand_supported);
  7661. return -1;
  7662. }
  7663. if (enable > 1) {
  7664. wpa_printf(MSG_INFO,
  7665. "CTRL: MAC_RAND_SCAN enable=<0/1> not specified");
  7666. return -1;
  7667. }
  7668. if (!enable) {
  7669. wpas_mac_addr_rand_scan_clear(wpa_s, type);
  7670. if (wpa_s->pno) {
  7671. if (type & MAC_ADDR_RAND_PNO) {
  7672. wpas_stop_pno(wpa_s);
  7673. wpas_start_pno(wpa_s);
  7674. }
  7675. } else if (wpa_s->sched_scanning &&
  7676. (type & MAC_ADDR_RAND_SCHED_SCAN)) {
  7677. wpas_scan_restart_sched_scan(wpa_s);
  7678. }
  7679. return 0;
  7680. }
  7681. if ((addr && !mask) || (!addr && mask)) {
  7682. wpa_printf(MSG_INFO,
  7683. "CTRL: MAC_RAND_SCAN invalid addr/mask combination");
  7684. return -1;
  7685. }
  7686. if (addr && mask && (!(mask[0] & 0x01) || (addr[0] & 0x01))) {
  7687. wpa_printf(MSG_INFO,
  7688. "CTRL: MAC_RAND_SCAN cannot allow multicast address");
  7689. return -1;
  7690. }
  7691. if (type & MAC_ADDR_RAND_SCAN) {
  7692. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCAN,
  7693. addr, mask);
  7694. }
  7695. if (type & MAC_ADDR_RAND_SCHED_SCAN) {
  7696. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_SCHED_SCAN,
  7697. addr, mask);
  7698. if (wpa_s->sched_scanning && !wpa_s->pno)
  7699. wpas_scan_restart_sched_scan(wpa_s);
  7700. }
  7701. if (type & MAC_ADDR_RAND_PNO) {
  7702. wpas_mac_addr_rand_scan_set(wpa_s, MAC_ADDR_RAND_PNO,
  7703. addr, mask);
  7704. if (wpa_s->pno) {
  7705. wpas_stop_pno(wpa_s);
  7706. wpas_start_pno(wpa_s);
  7707. }
  7708. }
  7709. return 0;
  7710. }
  7711. static int wpas_ctrl_iface_pmksa(struct wpa_supplicant *wpa_s,
  7712. char *buf, size_t buflen)
  7713. {
  7714. size_t reply_len;
  7715. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, buf, buflen);
  7716. #ifdef CONFIG_AP
  7717. reply_len += wpas_ap_pmksa_cache_list(wpa_s, &buf[reply_len],
  7718. buflen - reply_len);
  7719. #endif /* CONFIG_AP */
  7720. return reply_len;
  7721. }
  7722. static void wpas_ctrl_iface_pmksa_flush(struct wpa_supplicant *wpa_s)
  7723. {
  7724. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  7725. #ifdef CONFIG_AP
  7726. wpas_ap_pmksa_cache_flush(wpa_s);
  7727. #endif /* CONFIG_AP */
  7728. }
  7729. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  7730. static int wpas_ctrl_iface_pmksa_get(struct wpa_supplicant *wpa_s,
  7731. const char *cmd, char *buf, size_t buflen)
  7732. {
  7733. struct rsn_pmksa_cache_entry *entry;
  7734. struct wpa_ssid *ssid;
  7735. char *pos, *pos2, *end;
  7736. int ret;
  7737. struct os_reltime now;
  7738. ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
  7739. if (!ssid)
  7740. return -1;
  7741. pos = buf;
  7742. end = buf + buflen;
  7743. os_get_reltime(&now);
  7744. /*
  7745. * Entry format:
  7746. * <BSSID> <PMKID> <PMK> <reauth_time in seconds>
  7747. * <expiration in seconds> <akmp> <opportunistic>
  7748. * [FILS Cache Identifier]
  7749. */
  7750. for (entry = wpa_sm_pmksa_cache_head(wpa_s->wpa); entry;
  7751. entry = entry->next) {
  7752. if (entry->network_ctx != ssid)
  7753. continue;
  7754. pos2 = pos;
  7755. ret = os_snprintf(pos2, end - pos2, MACSTR " ",
  7756. MAC2STR(entry->aa));
  7757. if (os_snprintf_error(end - pos2, ret))
  7758. break;
  7759. pos2 += ret;
  7760. pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmkid,
  7761. PMKID_LEN);
  7762. ret = os_snprintf(pos2, end - pos2, " ");
  7763. if (os_snprintf_error(end - pos2, ret))
  7764. break;
  7765. pos2 += ret;
  7766. pos2 += wpa_snprintf_hex(pos2, end - pos2, entry->pmk,
  7767. entry->pmk_len);
  7768. ret = os_snprintf(pos2, end - pos2, " %d %d %d %d",
  7769. (int) (entry->reauth_time - now.sec),
  7770. (int) (entry->expiration - now.sec),
  7771. entry->akmp,
  7772. entry->opportunistic);
  7773. if (os_snprintf_error(end - pos2, ret))
  7774. break;
  7775. pos2 += ret;
  7776. if (entry->fils_cache_id_set) {
  7777. ret = os_snprintf(pos2, end - pos2, " %02x%02x",
  7778. entry->fils_cache_id[0],
  7779. entry->fils_cache_id[1]);
  7780. if (os_snprintf_error(end - pos2, ret))
  7781. break;
  7782. pos2 += ret;
  7783. }
  7784. ret = os_snprintf(pos2, end - pos2, "\n");
  7785. if (os_snprintf_error(end - pos2, ret))
  7786. break;
  7787. pos2 += ret;
  7788. pos = pos2;
  7789. }
  7790. return pos - buf;
  7791. }
  7792. static int wpas_ctrl_iface_pmksa_add(struct wpa_supplicant *wpa_s,
  7793. char *cmd)
  7794. {
  7795. struct rsn_pmksa_cache_entry *entry;
  7796. struct wpa_ssid *ssid;
  7797. char *pos, *pos2;
  7798. int ret = -1;
  7799. struct os_reltime now;
  7800. int reauth_time = 0, expiration = 0, i;
  7801. /*
  7802. * Entry format:
  7803. * <network_id> <BSSID> <PMKID> <PMK> <reauth_time in seconds>
  7804. * <expiration in seconds> <akmp> <opportunistic>
  7805. * [FILS Cache Identifier]
  7806. */
  7807. ssid = wpa_config_get_network(wpa_s->conf, atoi(cmd));
  7808. if (!ssid)
  7809. return -1;
  7810. pos = os_strchr(cmd, ' ');
  7811. if (!pos)
  7812. return -1;
  7813. pos++;
  7814. entry = os_zalloc(sizeof(*entry));
  7815. if (!entry)
  7816. return -1;
  7817. if (hwaddr_aton(pos, entry->aa))
  7818. goto fail;
  7819. pos = os_strchr(pos, ' ');
  7820. if (!pos)
  7821. goto fail;
  7822. pos++;
  7823. if (hexstr2bin(pos, entry->pmkid, PMKID_LEN) < 0)
  7824. goto fail;
  7825. pos = os_strchr(pos, ' ');
  7826. if (!pos)
  7827. goto fail;
  7828. pos++;
  7829. pos2 = os_strchr(pos, ' ');
  7830. if (!pos2)
  7831. goto fail;
  7832. entry->pmk_len = (pos2 - pos) / 2;
  7833. if (entry->pmk_len < PMK_LEN || entry->pmk_len > PMK_LEN_MAX ||
  7834. hexstr2bin(pos, entry->pmk, entry->pmk_len) < 0)
  7835. goto fail;
  7836. pos = os_strchr(pos, ' ');
  7837. if (!pos)
  7838. goto fail;
  7839. pos++;
  7840. if (sscanf(pos, "%d %d %d %d", &reauth_time, &expiration,
  7841. &entry->akmp, &entry->opportunistic) != 4)
  7842. goto fail;
  7843. for (i = 0; i < 4; i++) {
  7844. pos = os_strchr(pos, ' ');
  7845. if (!pos) {
  7846. if (i < 3)
  7847. goto fail;
  7848. break;
  7849. }
  7850. pos++;
  7851. }
  7852. if (pos) {
  7853. if (hexstr2bin(pos, entry->fils_cache_id,
  7854. FILS_CACHE_ID_LEN) < 0)
  7855. goto fail;
  7856. entry->fils_cache_id_set = 1;
  7857. }
  7858. os_get_reltime(&now);
  7859. entry->expiration = now.sec + expiration;
  7860. entry->reauth_time = now.sec + reauth_time;
  7861. entry->network_ctx = ssid;
  7862. wpa_sm_pmksa_cache_add_entry(wpa_s->wpa, entry);
  7863. entry = NULL;
  7864. ret = 0;
  7865. fail:
  7866. os_free(entry);
  7867. return ret;
  7868. }
  7869. #ifdef CONFIG_MESH
  7870. static int wpas_ctrl_iface_mesh_pmksa_get(struct wpa_supplicant *wpa_s,
  7871. const char *cmd, char *buf,
  7872. size_t buflen)
  7873. {
  7874. u8 spa[ETH_ALEN];
  7875. if (!wpa_s->ifmsh)
  7876. return -1;
  7877. if (os_strcasecmp(cmd, "any") == 0)
  7878. return wpas_ap_pmksa_cache_list_mesh(wpa_s, NULL, buf, buflen);
  7879. if (hwaddr_aton(cmd, spa))
  7880. return -1;
  7881. return wpas_ap_pmksa_cache_list_mesh(wpa_s, spa, buf, buflen);
  7882. }
  7883. static int wpas_ctrl_iface_mesh_pmksa_add(struct wpa_supplicant *wpa_s,
  7884. char *cmd)
  7885. {
  7886. /*
  7887. * We do not check mesh interface existance because PMKSA should be
  7888. * stored before wpa_s->ifmsh creation to suppress commit message
  7889. * creation.
  7890. */
  7891. return wpas_ap_pmksa_cache_add_external(wpa_s, cmd);
  7892. }
  7893. #endif /* CONFIG_MESH */
  7894. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  7895. #ifdef CONFIG_FILS
  7896. static int wpas_ctrl_iface_fils_hlp_req_add(struct wpa_supplicant *wpa_s,
  7897. const char *cmd)
  7898. {
  7899. struct fils_hlp_req *req;
  7900. const char *pos;
  7901. /* format: <dst> <packet starting from ethertype> */
  7902. req = os_zalloc(sizeof(*req));
  7903. if (!req)
  7904. return -1;
  7905. if (hwaddr_aton(cmd, req->dst))
  7906. goto fail;
  7907. pos = os_strchr(cmd, ' ');
  7908. if (!pos)
  7909. goto fail;
  7910. pos++;
  7911. req->pkt = wpabuf_parse_bin(pos);
  7912. if (!req->pkt)
  7913. goto fail;
  7914. dl_list_add_tail(&wpa_s->fils_hlp_req, &req->list);
  7915. return 0;
  7916. fail:
  7917. wpabuf_free(req->pkt);
  7918. os_free(req);
  7919. return -1;
  7920. }
  7921. #endif /* CONFIG_FILS */
  7922. static int wpas_ctrl_cmd_debug_level(const char *cmd)
  7923. {
  7924. if (os_strcmp(cmd, "PING") == 0 ||
  7925. os_strncmp(cmd, "BSS ", 4) == 0 ||
  7926. os_strncmp(cmd, "GET_NETWORK ", 12) == 0 ||
  7927. os_strncmp(cmd, "STATUS", 6) == 0 ||
  7928. os_strncmp(cmd, "STA ", 4) == 0 ||
  7929. os_strncmp(cmd, "STA-", 4) == 0)
  7930. return MSG_EXCESSIVE;
  7931. return MSG_DEBUG;
  7932. }
  7933. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  7934. char *buf, size_t *resp_len)
  7935. {
  7936. char *reply;
  7937. const int reply_size = 4096;
  7938. int reply_len;
  7939. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  7940. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  7941. os_strncmp(buf, "PMKSA_ADD ", 10) == 0 ||
  7942. os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
  7943. if (wpa_debug_show_keys)
  7944. wpa_dbg(wpa_s, MSG_DEBUG,
  7945. "Control interface command '%s'", buf);
  7946. else
  7947. wpa_dbg(wpa_s, MSG_DEBUG,
  7948. "Control interface command '%s [REMOVED]'",
  7949. os_strncmp(buf, WPA_CTRL_RSP,
  7950. os_strlen(WPA_CTRL_RSP)) == 0 ?
  7951. WPA_CTRL_RSP :
  7952. (os_strncmp(buf, "SET_NETWORK ", 12) == 0 ?
  7953. "SET_NETWORK" : "key-add"));
  7954. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  7955. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  7956. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  7957. (const u8 *) buf, os_strlen(buf));
  7958. } else {
  7959. int level = wpas_ctrl_cmd_debug_level(buf);
  7960. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  7961. }
  7962. reply = os_malloc(reply_size);
  7963. if (reply == NULL) {
  7964. *resp_len = 1;
  7965. return NULL;
  7966. }
  7967. os_memcpy(reply, "OK\n", 3);
  7968. reply_len = 3;
  7969. if (os_strcmp(buf, "PING") == 0) {
  7970. os_memcpy(reply, "PONG\n", 5);
  7971. reply_len = 5;
  7972. } else if (os_strcmp(buf, "IFNAME") == 0) {
  7973. reply_len = os_strlen(wpa_s->ifname);
  7974. os_memcpy(reply, wpa_s->ifname, reply_len);
  7975. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  7976. if (wpa_debug_reopen_file() < 0)
  7977. reply_len = -1;
  7978. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  7979. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  7980. } else if (os_strcmp(buf, "MIB") == 0) {
  7981. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  7982. if (reply_len >= 0) {
  7983. reply_len += eapol_sm_get_mib(wpa_s->eapol,
  7984. reply + reply_len,
  7985. reply_size - reply_len);
  7986. }
  7987. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  7988. reply_len = wpa_supplicant_ctrl_iface_status(
  7989. wpa_s, buf + 6, reply, reply_size);
  7990. } else if (os_strcmp(buf, "PMKSA") == 0) {
  7991. reply_len = wpas_ctrl_iface_pmksa(wpa_s, reply, reply_size);
  7992. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  7993. wpas_ctrl_iface_pmksa_flush(wpa_s);
  7994. #ifdef CONFIG_PMKSA_CACHE_EXTERNAL
  7995. } else if (os_strncmp(buf, "PMKSA_GET ", 10) == 0) {
  7996. reply_len = wpas_ctrl_iface_pmksa_get(wpa_s, buf + 10,
  7997. reply, reply_size);
  7998. } else if (os_strncmp(buf, "PMKSA_ADD ", 10) == 0) {
  7999. if (wpas_ctrl_iface_pmksa_add(wpa_s, buf + 10) < 0)
  8000. reply_len = -1;
  8001. #ifdef CONFIG_MESH
  8002. } else if (os_strncmp(buf, "MESH_PMKSA_GET ", 15) == 0) {
  8003. reply_len = wpas_ctrl_iface_mesh_pmksa_get(wpa_s, buf + 15,
  8004. reply, reply_size);
  8005. } else if (os_strncmp(buf, "MESH_PMKSA_ADD ", 15) == 0) {
  8006. if (wpas_ctrl_iface_mesh_pmksa_add(wpa_s, buf + 15) < 0)
  8007. reply_len = -1;
  8008. #endif /* CONFIG_MESH */
  8009. #endif /* CONFIG_PMKSA_CACHE_EXTERNAL */
  8010. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  8011. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  8012. reply_len = -1;
  8013. } else if (os_strncmp(buf, "DUMP", 4) == 0) {
  8014. reply_len = wpa_config_dump_values(wpa_s->conf,
  8015. reply, reply_size);
  8016. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  8017. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  8018. reply, reply_size);
  8019. } else if (os_strcmp(buf, "LOGON") == 0) {
  8020. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  8021. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  8022. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  8023. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  8024. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  8025. reply_len = -1;
  8026. else
  8027. wpas_request_connection(wpa_s);
  8028. } else if (os_strcmp(buf, "REATTACH") == 0) {
  8029. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  8030. !wpa_s->current_ssid)
  8031. reply_len = -1;
  8032. else {
  8033. wpa_s->reattach = 1;
  8034. wpas_request_connection(wpa_s);
  8035. }
  8036. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  8037. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  8038. reply_len = -1;
  8039. else if (wpa_s->disconnected)
  8040. wpas_request_connection(wpa_s);
  8041. #ifdef IEEE8021X_EAPOL
  8042. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  8043. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  8044. reply_len = -1;
  8045. #endif /* IEEE8021X_EAPOL */
  8046. #ifdef CONFIG_PEERKEY
  8047. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  8048. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  8049. reply_len = -1;
  8050. #endif /* CONFIG_PEERKEY */
  8051. #ifdef CONFIG_IEEE80211R
  8052. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  8053. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  8054. reply_len = -1;
  8055. #endif /* CONFIG_IEEE80211R */
  8056. #ifdef CONFIG_WPS
  8057. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  8058. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  8059. if (res == -2) {
  8060. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  8061. reply_len = 17;
  8062. } else if (res)
  8063. reply_len = -1;
  8064. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  8065. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  8066. if (res == -2) {
  8067. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  8068. reply_len = 17;
  8069. } else if (res)
  8070. reply_len = -1;
  8071. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  8072. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  8073. reply,
  8074. reply_size);
  8075. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  8076. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  8077. wpa_s, buf + 14, reply, reply_size);
  8078. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  8079. if (wpas_wps_cancel(wpa_s))
  8080. reply_len = -1;
  8081. #ifdef CONFIG_WPS_NFC
  8082. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  8083. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  8084. reply_len = -1;
  8085. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  8086. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  8087. reply_len = -1;
  8088. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  8089. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  8090. wpa_s, buf + 21, reply, reply_size);
  8091. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  8092. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  8093. wpa_s, buf + 14, reply, reply_size);
  8094. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  8095. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  8096. buf + 17))
  8097. reply_len = -1;
  8098. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  8099. reply_len = wpas_ctrl_nfc_get_handover_req(
  8100. wpa_s, buf + 21, reply, reply_size);
  8101. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  8102. reply_len = wpas_ctrl_nfc_get_handover_sel(
  8103. wpa_s, buf + 21, reply, reply_size);
  8104. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  8105. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  8106. reply_len = -1;
  8107. #endif /* CONFIG_WPS_NFC */
  8108. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  8109. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  8110. reply_len = -1;
  8111. #ifdef CONFIG_AP
  8112. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  8113. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  8114. wpa_s, buf + 11, reply, reply_size);
  8115. #endif /* CONFIG_AP */
  8116. #ifdef CONFIG_WPS_ER
  8117. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  8118. if (wpas_wps_er_start(wpa_s, NULL))
  8119. reply_len = -1;
  8120. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  8121. if (wpas_wps_er_start(wpa_s, buf + 13))
  8122. reply_len = -1;
  8123. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  8124. wpas_wps_er_stop(wpa_s);
  8125. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  8126. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  8127. reply_len = -1;
  8128. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  8129. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  8130. if (ret == -2) {
  8131. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  8132. reply_len = 17;
  8133. } else if (ret == -3) {
  8134. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  8135. reply_len = 18;
  8136. } else if (ret == -4) {
  8137. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  8138. reply_len = 20;
  8139. } else if (ret)
  8140. reply_len = -1;
  8141. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  8142. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  8143. reply_len = -1;
  8144. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  8145. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  8146. buf + 18))
  8147. reply_len = -1;
  8148. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  8149. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  8150. reply_len = -1;
  8151. #ifdef CONFIG_WPS_NFC
  8152. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  8153. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  8154. wpa_s, buf + 24, reply, reply_size);
  8155. #endif /* CONFIG_WPS_NFC */
  8156. #endif /* CONFIG_WPS_ER */
  8157. #endif /* CONFIG_WPS */
  8158. #ifdef CONFIG_IBSS_RSN
  8159. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  8160. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  8161. reply_len = -1;
  8162. #endif /* CONFIG_IBSS_RSN */
  8163. #ifdef CONFIG_MESH
  8164. } else if (os_strncmp(buf, "MESH_INTERFACE_ADD ", 19) == 0) {
  8165. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  8166. wpa_s, buf + 19, reply, reply_size);
  8167. } else if (os_strcmp(buf, "MESH_INTERFACE_ADD") == 0) {
  8168. reply_len = wpa_supplicant_ctrl_iface_mesh_interface_add(
  8169. wpa_s, "", reply, reply_size);
  8170. } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
  8171. if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
  8172. reply_len = -1;
  8173. } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
  8174. if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
  8175. buf + 18))
  8176. reply_len = -1;
  8177. } else if (os_strncmp(buf, "MESH_PEER_REMOVE ", 17) == 0) {
  8178. if (wpa_supplicant_ctrl_iface_mesh_peer_remove(wpa_s, buf + 17))
  8179. reply_len = -1;
  8180. } else if (os_strncmp(buf, "MESH_PEER_ADD ", 14) == 0) {
  8181. if (wpa_supplicant_ctrl_iface_mesh_peer_add(wpa_s, buf + 14))
  8182. reply_len = -1;
  8183. #endif /* CONFIG_MESH */
  8184. #ifdef CONFIG_P2P
  8185. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  8186. if (p2p_ctrl_find(wpa_s, buf + 8))
  8187. reply_len = -1;
  8188. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  8189. if (p2p_ctrl_find(wpa_s, ""))
  8190. reply_len = -1;
  8191. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  8192. wpas_p2p_stop_find(wpa_s);
  8193. } else if (os_strncmp(buf, "P2P_ASP_PROVISION ", 18) == 0) {
  8194. if (p2p_ctrl_asp_provision(wpa_s, buf + 18))
  8195. reply_len = -1;
  8196. } else if (os_strncmp(buf, "P2P_ASP_PROVISION_RESP ", 23) == 0) {
  8197. if (p2p_ctrl_asp_provision_resp(wpa_s, buf + 23))
  8198. reply_len = -1;
  8199. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  8200. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  8201. reply_size);
  8202. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  8203. if (p2p_ctrl_listen(wpa_s, buf + 11))
  8204. reply_len = -1;
  8205. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  8206. if (p2p_ctrl_listen(wpa_s, ""))
  8207. reply_len = -1;
  8208. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  8209. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  8210. reply_len = -1;
  8211. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  8212. if (p2p_ctrl_group_add(wpa_s, ""))
  8213. reply_len = -1;
  8214. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  8215. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  8216. reply_len = -1;
  8217. } else if (os_strncmp(buf, "P2P_GROUP_MEMBER ", 17) == 0) {
  8218. reply_len = p2p_ctrl_group_member(wpa_s, buf + 17, reply,
  8219. reply_size);
  8220. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  8221. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  8222. reply_len = -1;
  8223. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  8224. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  8225. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  8226. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  8227. reply_size);
  8228. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  8229. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  8230. reply_len = -1;
  8231. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  8232. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  8233. reply_len = -1;
  8234. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  8235. wpas_p2p_sd_service_update(wpa_s);
  8236. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  8237. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  8238. reply_len = -1;
  8239. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  8240. wpas_p2p_service_flush(wpa_s);
  8241. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  8242. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  8243. reply_len = -1;
  8244. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  8245. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  8246. reply_len = -1;
  8247. } else if (os_strncmp(buf, "P2P_SERVICE_REP ", 16) == 0) {
  8248. if (p2p_ctrl_service_replace(wpa_s, buf + 16) < 0)
  8249. reply_len = -1;
  8250. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  8251. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  8252. reply_len = -1;
  8253. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  8254. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  8255. reply_len = -1;
  8256. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  8257. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  8258. reply_size);
  8259. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  8260. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  8261. reply_len = -1;
  8262. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  8263. p2p_ctrl_flush(wpa_s);
  8264. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  8265. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  8266. reply_len = -1;
  8267. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  8268. if (wpas_p2p_cancel(wpa_s))
  8269. reply_len = -1;
  8270. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  8271. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  8272. reply_len = -1;
  8273. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  8274. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  8275. reply_len = -1;
  8276. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  8277. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  8278. reply_len = -1;
  8279. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  8280. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  8281. reply_len = -1;
  8282. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  8283. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  8284. reply_len = -1;
  8285. } else if (os_strncmp(buf, "P2P_LO_START ", 13) == 0) {
  8286. if (p2p_ctrl_iface_p2p_lo_start(wpa_s, buf + 13))
  8287. reply_len = -1;
  8288. } else if (os_strcmp(buf, "P2P_LO_STOP") == 0) {
  8289. if (wpas_p2p_lo_stop(wpa_s))
  8290. reply_len = -1;
  8291. #endif /* CONFIG_P2P */
  8292. #ifdef CONFIG_WIFI_DISPLAY
  8293. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  8294. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  8295. reply_len = -1;
  8296. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  8297. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  8298. reply, reply_size);
  8299. #endif /* CONFIG_WIFI_DISPLAY */
  8300. #ifdef CONFIG_INTERWORKING
  8301. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  8302. if (interworking_fetch_anqp(wpa_s) < 0)
  8303. reply_len = -1;
  8304. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  8305. interworking_stop_fetch_anqp(wpa_s);
  8306. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  8307. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  8308. reply_len = -1;
  8309. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  8310. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  8311. reply_len = -1;
  8312. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  8313. if (ctrl_interworking_connect(wpa_s, buf + 21, 0) < 0)
  8314. reply_len = -1;
  8315. } else if (os_strncmp(buf, "INTERWORKING_ADD_NETWORK ", 25) == 0) {
  8316. int id;
  8317. id = ctrl_interworking_connect(wpa_s, buf + 25, 1);
  8318. if (id < 0)
  8319. reply_len = -1;
  8320. else {
  8321. reply_len = os_snprintf(reply, reply_size, "%d\n", id);
  8322. if (os_snprintf_error(reply_size, reply_len))
  8323. reply_len = -1;
  8324. }
  8325. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  8326. if (get_anqp(wpa_s, buf + 9) < 0)
  8327. reply_len = -1;
  8328. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  8329. if (gas_request(wpa_s, buf + 12) < 0)
  8330. reply_len = -1;
  8331. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  8332. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  8333. reply_size);
  8334. #endif /* CONFIG_INTERWORKING */
  8335. #ifdef CONFIG_HS20
  8336. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  8337. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  8338. reply_len = -1;
  8339. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  8340. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  8341. reply_len = -1;
  8342. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  8343. if (hs20_icon_request(wpa_s, buf + 18, 0) < 0)
  8344. reply_len = -1;
  8345. } else if (os_strncmp(buf, "REQ_HS20_ICON ", 14) == 0) {
  8346. if (hs20_icon_request(wpa_s, buf + 14, 1) < 0)
  8347. reply_len = -1;
  8348. } else if (os_strncmp(buf, "GET_HS20_ICON ", 14) == 0) {
  8349. reply_len = get_hs20_icon(wpa_s, buf + 14, reply, reply_size);
  8350. } else if (os_strncmp(buf, "DEL_HS20_ICON ", 14) == 0) {
  8351. if (del_hs20_icon(wpa_s, buf + 14) < 0)
  8352. reply_len = -1;
  8353. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  8354. if (hs20_fetch_osu(wpa_s, 0) < 0)
  8355. reply_len = -1;
  8356. } else if (os_strcmp(buf, "FETCH_OSU no-scan") == 0) {
  8357. if (hs20_fetch_osu(wpa_s, 1) < 0)
  8358. reply_len = -1;
  8359. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  8360. hs20_cancel_fetch_osu(wpa_s);
  8361. #endif /* CONFIG_HS20 */
  8362. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  8363. {
  8364. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  8365. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  8366. reply_len = -1;
  8367. else {
  8368. /*
  8369. * Notify response from timeout to allow the control
  8370. * interface response to be sent first.
  8371. */
  8372. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  8373. wpa_s, NULL);
  8374. }
  8375. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  8376. if (wpa_supplicant_reload_configuration(wpa_s))
  8377. reply_len = -1;
  8378. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  8379. wpa_supplicant_terminate_proc(wpa_s->global);
  8380. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  8381. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  8382. reply_len = -1;
  8383. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  8384. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  8385. wpa_s, buf + 9, reply, reply_size);
  8386. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  8387. reply_len = wpa_supplicant_ctrl_iface_log_level(
  8388. wpa_s, buf + 9, reply, reply_size);
  8389. } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
  8390. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  8391. wpa_s, buf + 14, reply, reply_size);
  8392. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  8393. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  8394. wpa_s, NULL, reply, reply_size);
  8395. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  8396. wpas_request_disconnection(wpa_s);
  8397. } else if (os_strcmp(buf, "SCAN") == 0) {
  8398. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  8399. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  8400. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  8401. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  8402. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  8403. wpa_s, reply, reply_size);
  8404. } else if (os_strcmp(buf, "ABORT_SCAN") == 0) {
  8405. if (wpas_abort_ongoing_scan(wpa_s) < 0)
  8406. reply_len = -1;
  8407. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  8408. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  8409. reply_len = -1;
  8410. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  8411. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  8412. reply_len = -1;
  8413. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  8414. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  8415. reply_len = -1;
  8416. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  8417. reply_len = wpa_supplicant_ctrl_iface_add_network(
  8418. wpa_s, reply, reply_size);
  8419. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  8420. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  8421. reply_len = -1;
  8422. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  8423. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  8424. reply_len = -1;
  8425. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  8426. reply_len = wpa_supplicant_ctrl_iface_get_network(
  8427. wpa_s, buf + 12, reply, reply_size);
  8428. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  8429. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12,
  8430. wpa_s))
  8431. reply_len = -1;
  8432. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  8433. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  8434. wpa_s, reply, reply_size);
  8435. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  8436. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  8437. wpa_s, reply, reply_size);
  8438. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  8439. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  8440. reply_len = -1;
  8441. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  8442. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  8443. reply_len = -1;
  8444. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  8445. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  8446. reply,
  8447. reply_size);
  8448. #ifndef CONFIG_NO_CONFIG_WRITE
  8449. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  8450. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  8451. reply_len = -1;
  8452. #endif /* CONFIG_NO_CONFIG_WRITE */
  8453. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  8454. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  8455. wpa_s, buf + 15, reply, reply_size);
  8456. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  8457. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  8458. reply_len = -1;
  8459. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  8460. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  8461. reply_len = -1;
  8462. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  8463. reply_len = wpa_supplicant_global_iface_list(
  8464. wpa_s->global, reply, reply_size);
  8465. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  8466. reply_len = wpa_supplicant_global_iface_interfaces(
  8467. wpa_s->global, buf + 10, reply, reply_size);
  8468. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  8469. reply_len = wpa_supplicant_ctrl_iface_bss(
  8470. wpa_s, buf + 4, reply, reply_size);
  8471. #ifdef CONFIG_AP
  8472. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  8473. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  8474. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  8475. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  8476. reply_size);
  8477. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  8478. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  8479. reply_size);
  8480. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  8481. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  8482. reply_len = -1;
  8483. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  8484. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  8485. reply_len = -1;
  8486. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  8487. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  8488. reply_len = -1;
  8489. } else if (os_strcmp(buf, "STOP_AP") == 0) {
  8490. if (wpas_ap_stop_ap(wpa_s))
  8491. reply_len = -1;
  8492. #endif /* CONFIG_AP */
  8493. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  8494. wpas_notify_suspend(wpa_s->global);
  8495. } else if (os_strcmp(buf, "RESUME") == 0) {
  8496. wpas_notify_resume(wpa_s->global);
  8497. #ifdef CONFIG_TESTING_OPTIONS
  8498. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  8499. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  8500. #endif /* CONFIG_TESTING_OPTIONS */
  8501. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  8502. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  8503. reply_len = -1;
  8504. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  8505. wpa_s->auto_reconnect_disabled = atoi(buf + 16) == 0;
  8506. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  8507. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  8508. reply_len = -1;
  8509. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  8510. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  8511. buf + 17))
  8512. reply_len = -1;
  8513. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  8514. wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10);
  8515. #ifdef CONFIG_TDLS
  8516. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  8517. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  8518. reply_len = -1;
  8519. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  8520. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  8521. reply_len = -1;
  8522. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  8523. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  8524. reply_len = -1;
  8525. } else if (os_strncmp(buf, "TDLS_CHAN_SWITCH ", 17) == 0) {
  8526. if (wpa_supplicant_ctrl_iface_tdls_chan_switch(wpa_s,
  8527. buf + 17))
  8528. reply_len = -1;
  8529. } else if (os_strncmp(buf, "TDLS_CANCEL_CHAN_SWITCH ", 24) == 0) {
  8530. if (wpa_supplicant_ctrl_iface_tdls_cancel_chan_switch(wpa_s,
  8531. buf + 24))
  8532. reply_len = -1;
  8533. } else if (os_strncmp(buf, "TDLS_LINK_STATUS ", 17) == 0) {
  8534. reply_len = wpa_supplicant_ctrl_iface_tdls_link_status(
  8535. wpa_s, buf + 17, reply, reply_size);
  8536. #endif /* CONFIG_TDLS */
  8537. } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
  8538. reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
  8539. } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
  8540. if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
  8541. reply_len = -1;
  8542. } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
  8543. if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
  8544. reply_len = -1;
  8545. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  8546. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  8547. reply_size);
  8548. } else if (os_strncmp(buf, "SIGNAL_MONITOR", 14) == 0) {
  8549. if (wpas_ctrl_iface_signal_monitor(wpa_s, buf + 14))
  8550. reply_len = -1;
  8551. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  8552. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  8553. reply_size);
  8554. #ifdef CONFIG_AUTOSCAN
  8555. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  8556. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  8557. reply_len = -1;
  8558. #endif /* CONFIG_AUTOSCAN */
  8559. } else if (os_strcmp(buf, "DRIVER_FLAGS") == 0) {
  8560. reply_len = wpas_ctrl_iface_driver_flags(wpa_s, reply,
  8561. reply_size);
  8562. #ifdef ANDROID
  8563. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  8564. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  8565. reply_size);
  8566. #endif /* ANDROID */
  8567. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  8568. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  8569. reply_size);
  8570. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  8571. pmksa_cache_clear_current(wpa_s->wpa);
  8572. eapol_sm_request_reauth(wpa_s->eapol);
  8573. #ifdef CONFIG_WNM
  8574. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  8575. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  8576. reply_len = -1;
  8577. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 14) == 0) {
  8578. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 14))
  8579. reply_len = -1;
  8580. #endif /* CONFIG_WNM */
  8581. } else if (os_strcmp(buf, "FLUSH") == 0) {
  8582. wpa_supplicant_ctrl_iface_flush(wpa_s);
  8583. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  8584. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  8585. reply_size);
  8586. #ifdef CONFIG_TESTING_OPTIONS
  8587. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  8588. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  8589. reply_len = -1;
  8590. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  8591. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  8592. } else if (os_strncmp(buf, "MGMT_RX_PROCESS ", 16) == 0) {
  8593. if (wpas_ctrl_iface_mgmt_rx_process(wpa_s, buf + 16) < 0)
  8594. reply_len = -1;
  8595. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  8596. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  8597. reply_len = -1;
  8598. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  8599. if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
  8600. reply_len = -1;
  8601. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  8602. if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
  8603. reply_len = -1;
  8604. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  8605. if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
  8606. reply_len = -1;
  8607. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  8608. if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
  8609. reply_len = -1;
  8610. } else if (os_strncmp(buf, "TEST_ALLOC_FAIL ", 16) == 0) {
  8611. if (wpas_ctrl_test_alloc_fail(wpa_s, buf + 16) < 0)
  8612. reply_len = -1;
  8613. } else if (os_strcmp(buf, "GET_ALLOC_FAIL") == 0) {
  8614. reply_len = wpas_ctrl_get_alloc_fail(wpa_s, reply, reply_size);
  8615. } else if (os_strncmp(buf, "TEST_FAIL ", 10) == 0) {
  8616. if (wpas_ctrl_test_fail(wpa_s, buf + 10) < 0)
  8617. reply_len = -1;
  8618. } else if (os_strcmp(buf, "GET_FAIL") == 0) {
  8619. reply_len = wpas_ctrl_get_fail(wpa_s, reply, reply_size);
  8620. } else if (os_strncmp(buf, "EVENT_TEST ", 11) == 0) {
  8621. if (wpas_ctrl_event_test(wpa_s, buf + 11) < 0)
  8622. reply_len = -1;
  8623. } else if (os_strncmp(buf, "TEST_ASSOC_IE ", 14) == 0) {
  8624. if (wpas_ctrl_test_assoc_ie(wpa_s, buf + 14) < 0)
  8625. reply_len = -1;
  8626. #endif /* CONFIG_TESTING_OPTIONS */
  8627. } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
  8628. if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
  8629. reply_len = -1;
  8630. } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
  8631. reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
  8632. reply_size);
  8633. } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
  8634. if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
  8635. reply_len = -1;
  8636. } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
  8637. if (wpas_ctrl_iface_send_neighbor_rep(wpa_s, buf + 20))
  8638. reply_len = -1;
  8639. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  8640. wpas_ctrl_iface_erp_flush(wpa_s);
  8641. } else if (os_strncmp(buf, "MAC_RAND_SCAN ", 14) == 0) {
  8642. if (wpas_ctrl_iface_mac_rand_scan(wpa_s, buf + 14))
  8643. reply_len = -1;
  8644. } else if (os_strncmp(buf, "GET_PREF_FREQ_LIST ", 19) == 0) {
  8645. reply_len = wpas_ctrl_iface_get_pref_freq_list(
  8646. wpa_s, buf + 19, reply, reply_size);
  8647. #ifdef CONFIG_FILS
  8648. } else if (os_strncmp(buf, "FILS_HLP_REQ_ADD ", 17) == 0) {
  8649. if (wpas_ctrl_iface_fils_hlp_req_add(wpa_s, buf + 17))
  8650. reply_len = -1;
  8651. } else if (os_strcmp(buf, "FILS_HLP_REQ_FLUSH") == 0) {
  8652. wpas_flush_fils_hlp_req(wpa_s);
  8653. #endif /* CONFIG_FILS */
  8654. #ifdef CONFIG_DPP
  8655. } else if (os_strncmp(buf, "DPP_QR_CODE ", 12) == 0) {
  8656. int res;
  8657. res = wpas_dpp_qr_code(wpa_s, buf + 12);
  8658. if (res < 0) {
  8659. reply_len = -1;
  8660. } else {
  8661. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8662. if (os_snprintf_error(reply_size, reply_len))
  8663. reply_len = -1;
  8664. }
  8665. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GEN ", 18) == 0) {
  8666. int res;
  8667. res = wpas_dpp_bootstrap_gen(wpa_s, buf + 18);
  8668. if (res < 0) {
  8669. reply_len = -1;
  8670. } else {
  8671. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8672. if (os_snprintf_error(reply_size, reply_len))
  8673. reply_len = -1;
  8674. }
  8675. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_REMOVE ", 21) == 0) {
  8676. if (wpas_dpp_bootstrap_remove(wpa_s, buf + 21) < 0)
  8677. reply_len = -1;
  8678. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_GET_URI ", 22) == 0) {
  8679. const char *uri;
  8680. uri = wpas_dpp_bootstrap_get_uri(wpa_s, atoi(buf + 22));
  8681. if (!uri) {
  8682. reply_len = -1;
  8683. } else {
  8684. reply_len = os_snprintf(reply, reply_size, "%s", uri);
  8685. if (os_snprintf_error(reply_size, reply_len))
  8686. reply_len = -1;
  8687. }
  8688. } else if (os_strncmp(buf, "DPP_BOOTSTRAP_INFO ", 19) == 0) {
  8689. reply_len = wpas_dpp_bootstrap_info(wpa_s, atoi(buf + 19),
  8690. reply, reply_size);
  8691. } else if (os_strncmp(buf, "DPP_AUTH_INIT ", 14) == 0) {
  8692. if (wpas_dpp_auth_init(wpa_s, buf + 13) < 0)
  8693. reply_len = -1;
  8694. } else if (os_strncmp(buf, "DPP_LISTEN ", 11) == 0) {
  8695. if (wpas_dpp_listen(wpa_s, buf + 11) < 0)
  8696. reply_len = -1;
  8697. } else if (os_strcmp(buf, "DPP_STOP_LISTEN") == 0) {
  8698. wpas_dpp_listen_stop(wpa_s);
  8699. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_ADD", 20) == 0) {
  8700. int res;
  8701. res = wpas_dpp_configurator_add(wpa_s, buf + 20);
  8702. if (res < 0) {
  8703. reply_len = -1;
  8704. } else {
  8705. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8706. if (os_snprintf_error(reply_size, reply_len))
  8707. reply_len = -1;
  8708. }
  8709. } else if (os_strncmp(buf, "DPP_CONFIGURATOR_REMOVE ", 24) == 0) {
  8710. if (wpas_dpp_configurator_remove(wpa_s, buf + 24) < 0)
  8711. reply_len = -1;
  8712. } else if (os_strncmp(buf, "DPP_PKEX_ADD ", 13) == 0) {
  8713. int res;
  8714. res = wpas_dpp_pkex_add(wpa_s, buf + 12);
  8715. if (res < 0) {
  8716. reply_len = -1;
  8717. } else {
  8718. reply_len = os_snprintf(reply, reply_size, "%d", res);
  8719. if (os_snprintf_error(reply_size, reply_len))
  8720. reply_len = -1;
  8721. }
  8722. } else if (os_strncmp(buf, "DPP_PKEX_REMOVE ", 16) == 0) {
  8723. if (wpas_dpp_pkex_remove(wpa_s, buf + 16) < 0)
  8724. reply_len = -1;
  8725. #endif /* CONFIG_DPP */
  8726. } else {
  8727. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  8728. reply_len = 16;
  8729. }
  8730. if (reply_len < 0) {
  8731. os_memcpy(reply, "FAIL\n", 5);
  8732. reply_len = 5;
  8733. }
  8734. *resp_len = reply_len;
  8735. return reply;
  8736. }
  8737. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  8738. char *cmd)
  8739. {
  8740. struct wpa_interface iface;
  8741. char *pos, *extra;
  8742. struct wpa_supplicant *wpa_s;
  8743. unsigned int create_iface = 0;
  8744. u8 mac_addr[ETH_ALEN];
  8745. enum wpa_driver_if_type type = WPA_IF_STATION;
  8746. /*
  8747. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  8748. * TAB<bridge_ifname>[TAB<create>[TAB<interface_type>]]
  8749. */
  8750. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  8751. os_memset(&iface, 0, sizeof(iface));
  8752. do {
  8753. iface.ifname = pos = cmd;
  8754. pos = os_strchr(pos, '\t');
  8755. if (pos)
  8756. *pos++ = '\0';
  8757. if (iface.ifname[0] == '\0')
  8758. return -1;
  8759. if (pos == NULL)
  8760. break;
  8761. iface.confname = pos;
  8762. pos = os_strchr(pos, '\t');
  8763. if (pos)
  8764. *pos++ = '\0';
  8765. if (iface.confname[0] == '\0')
  8766. iface.confname = NULL;
  8767. if (pos == NULL)
  8768. break;
  8769. iface.driver = pos;
  8770. pos = os_strchr(pos, '\t');
  8771. if (pos)
  8772. *pos++ = '\0';
  8773. if (iface.driver[0] == '\0')
  8774. iface.driver = NULL;
  8775. if (pos == NULL)
  8776. break;
  8777. iface.ctrl_interface = pos;
  8778. pos = os_strchr(pos, '\t');
  8779. if (pos)
  8780. *pos++ = '\0';
  8781. if (iface.ctrl_interface[0] == '\0')
  8782. iface.ctrl_interface = NULL;
  8783. if (pos == NULL)
  8784. break;
  8785. iface.driver_param = pos;
  8786. pos = os_strchr(pos, '\t');
  8787. if (pos)
  8788. *pos++ = '\0';
  8789. if (iface.driver_param[0] == '\0')
  8790. iface.driver_param = NULL;
  8791. if (pos == NULL)
  8792. break;
  8793. iface.bridge_ifname = pos;
  8794. pos = os_strchr(pos, '\t');
  8795. if (pos)
  8796. *pos++ = '\0';
  8797. if (iface.bridge_ifname[0] == '\0')
  8798. iface.bridge_ifname = NULL;
  8799. if (pos == NULL)
  8800. break;
  8801. extra = pos;
  8802. pos = os_strchr(pos, '\t');
  8803. if (pos)
  8804. *pos++ = '\0';
  8805. if (!extra[0])
  8806. break;
  8807. if (os_strcmp(extra, "create") == 0) {
  8808. create_iface = 1;
  8809. if (!pos)
  8810. break;
  8811. if (os_strcmp(pos, "sta") == 0) {
  8812. type = WPA_IF_STATION;
  8813. } else if (os_strcmp(pos, "ap") == 0) {
  8814. type = WPA_IF_AP_BSS;
  8815. } else {
  8816. wpa_printf(MSG_DEBUG,
  8817. "INTERFACE_ADD unsupported interface type: '%s'",
  8818. pos);
  8819. return -1;
  8820. }
  8821. } else {
  8822. wpa_printf(MSG_DEBUG,
  8823. "INTERFACE_ADD unsupported extra parameter: '%s'",
  8824. extra);
  8825. return -1;
  8826. }
  8827. } while (0);
  8828. if (create_iface) {
  8829. wpa_printf(MSG_DEBUG, "CTRL_IFACE creating interface '%s'",
  8830. iface.ifname);
  8831. if (!global->ifaces)
  8832. return -1;
  8833. if (wpa_drv_if_add(global->ifaces, type, iface.ifname,
  8834. NULL, NULL, NULL, mac_addr, NULL) < 0) {
  8835. wpa_printf(MSG_ERROR,
  8836. "CTRL_IFACE interface creation failed");
  8837. return -1;
  8838. }
  8839. wpa_printf(MSG_DEBUG,
  8840. "CTRL_IFACE interface '%s' created with MAC addr: "
  8841. MACSTR, iface.ifname, MAC2STR(mac_addr));
  8842. }
  8843. if (wpa_supplicant_get_iface(global, iface.ifname))
  8844. goto fail;
  8845. wpa_s = wpa_supplicant_add_iface(global, &iface, NULL);
  8846. if (!wpa_s)
  8847. goto fail;
  8848. wpa_s->added_vif = create_iface;
  8849. return 0;
  8850. fail:
  8851. if (create_iface)
  8852. wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, iface.ifname);
  8853. return -1;
  8854. }
  8855. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  8856. char *cmd)
  8857. {
  8858. struct wpa_supplicant *wpa_s;
  8859. int ret;
  8860. unsigned int delete_iface;
  8861. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  8862. wpa_s = wpa_supplicant_get_iface(global, cmd);
  8863. if (wpa_s == NULL)
  8864. return -1;
  8865. delete_iface = wpa_s->added_vif;
  8866. ret = wpa_supplicant_remove_iface(global, wpa_s, 0);
  8867. if (!ret && delete_iface) {
  8868. wpa_printf(MSG_DEBUG, "CTRL_IFACE deleting the interface '%s'",
  8869. cmd);
  8870. ret = wpa_drv_if_remove(global->ifaces, WPA_IF_STATION, cmd);
  8871. }
  8872. return ret;
  8873. }
  8874. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  8875. {
  8876. struct wpa_interface_info *prev;
  8877. while (iface) {
  8878. prev = iface;
  8879. iface = iface->next;
  8880. os_free(prev->ifname);
  8881. os_free(prev->desc);
  8882. os_free(prev);
  8883. }
  8884. }
  8885. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  8886. char *buf, int len)
  8887. {
  8888. int i, res;
  8889. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  8890. char *pos, *end;
  8891. for (i = 0; wpa_drivers[i]; i++) {
  8892. const struct wpa_driver_ops *drv = wpa_drivers[i];
  8893. if (drv->get_interfaces == NULL)
  8894. continue;
  8895. tmp = drv->get_interfaces(global->drv_priv[i]);
  8896. if (tmp == NULL)
  8897. continue;
  8898. if (last == NULL)
  8899. iface = last = tmp;
  8900. else
  8901. last->next = tmp;
  8902. while (last->next)
  8903. last = last->next;
  8904. }
  8905. pos = buf;
  8906. end = buf + len;
  8907. for (tmp = iface; tmp; tmp = tmp->next) {
  8908. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  8909. tmp->drv_name, tmp->ifname,
  8910. tmp->desc ? tmp->desc : "");
  8911. if (os_snprintf_error(end - pos, res)) {
  8912. *pos = '\0';
  8913. break;
  8914. }
  8915. pos += res;
  8916. }
  8917. wpa_free_iface_info(iface);
  8918. return pos - buf;
  8919. }
  8920. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  8921. const char *input,
  8922. char *buf, int len)
  8923. {
  8924. int res;
  8925. char *pos, *end;
  8926. struct wpa_supplicant *wpa_s;
  8927. int show_ctrl = 0;
  8928. if (input)
  8929. show_ctrl = !!os_strstr(input, "ctrl");
  8930. wpa_s = global->ifaces;
  8931. pos = buf;
  8932. end = buf + len;
  8933. while (wpa_s) {
  8934. if (show_ctrl)
  8935. res = os_snprintf(pos, end - pos, "%s ctrl_iface=%s\n",
  8936. wpa_s->ifname,
  8937. wpa_s->conf->ctrl_interface ?
  8938. wpa_s->conf->ctrl_interface : "N/A");
  8939. else
  8940. res = os_snprintf(pos, end - pos, "%s\n",
  8941. wpa_s->ifname);
  8942. if (os_snprintf_error(end - pos, res)) {
  8943. *pos = '\0';
  8944. break;
  8945. }
  8946. pos += res;
  8947. wpa_s = wpa_s->next;
  8948. }
  8949. return pos - buf;
  8950. }
  8951. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  8952. const char *ifname,
  8953. char *cmd, size_t *resp_len)
  8954. {
  8955. struct wpa_supplicant *wpa_s;
  8956. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  8957. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  8958. break;
  8959. }
  8960. if (wpa_s == NULL) {
  8961. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  8962. if (resp)
  8963. *resp_len = os_strlen(resp);
  8964. else
  8965. *resp_len = 1;
  8966. return resp;
  8967. }
  8968. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  8969. }
  8970. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  8971. char *buf, size_t *resp_len)
  8972. {
  8973. #ifdef CONFIG_P2P
  8974. static const char * cmd[] = {
  8975. "LIST_NETWORKS",
  8976. "P2P_FIND",
  8977. "P2P_STOP_FIND",
  8978. "P2P_LISTEN",
  8979. "P2P_GROUP_ADD",
  8980. "P2P_GET_PASSPHRASE",
  8981. "P2P_SERVICE_UPDATE",
  8982. "P2P_SERVICE_FLUSH",
  8983. "P2P_FLUSH",
  8984. "P2P_CANCEL",
  8985. "P2P_PRESENCE_REQ",
  8986. "P2P_EXT_LISTEN",
  8987. #ifdef CONFIG_AP
  8988. "STA-FIRST",
  8989. #endif /* CONFIG_AP */
  8990. NULL
  8991. };
  8992. static const char * prefix[] = {
  8993. #ifdef ANDROID
  8994. "DRIVER ",
  8995. #endif /* ANDROID */
  8996. "GET_CAPABILITY ",
  8997. "GET_NETWORK ",
  8998. "REMOVE_NETWORK ",
  8999. "P2P_FIND ",
  9000. "P2P_CONNECT ",
  9001. "P2P_LISTEN ",
  9002. "P2P_GROUP_REMOVE ",
  9003. "P2P_GROUP_ADD ",
  9004. "P2P_GROUP_MEMBER ",
  9005. "P2P_PROV_DISC ",
  9006. "P2P_SERV_DISC_REQ ",
  9007. "P2P_SERV_DISC_CANCEL_REQ ",
  9008. "P2P_SERV_DISC_RESP ",
  9009. "P2P_SERV_DISC_EXTERNAL ",
  9010. "P2P_SERVICE_ADD ",
  9011. "P2P_SERVICE_DEL ",
  9012. "P2P_SERVICE_REP ",
  9013. "P2P_REJECT ",
  9014. "P2P_INVITE ",
  9015. "P2P_PEER ",
  9016. "P2P_SET ",
  9017. "P2P_UNAUTHORIZE ",
  9018. "P2P_PRESENCE_REQ ",
  9019. "P2P_EXT_LISTEN ",
  9020. "P2P_REMOVE_CLIENT ",
  9021. "WPS_NFC_TOKEN ",
  9022. "WPS_NFC_TAG_READ ",
  9023. "NFC_GET_HANDOVER_SEL ",
  9024. "NFC_GET_HANDOVER_REQ ",
  9025. "NFC_REPORT_HANDOVER ",
  9026. "P2P_ASP_PROVISION ",
  9027. "P2P_ASP_PROVISION_RESP ",
  9028. #ifdef CONFIG_AP
  9029. "STA ",
  9030. "STA-NEXT ",
  9031. #endif /* CONFIG_AP */
  9032. NULL
  9033. };
  9034. int found = 0;
  9035. int i;
  9036. if (global->p2p_init_wpa_s == NULL)
  9037. return NULL;
  9038. for (i = 0; !found && cmd[i]; i++) {
  9039. if (os_strcmp(buf, cmd[i]) == 0)
  9040. found = 1;
  9041. }
  9042. for (i = 0; !found && prefix[i]; i++) {
  9043. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  9044. found = 1;
  9045. }
  9046. if (found)
  9047. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  9048. buf, resp_len);
  9049. #endif /* CONFIG_P2P */
  9050. return NULL;
  9051. }
  9052. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  9053. char *buf, size_t *resp_len)
  9054. {
  9055. #ifdef CONFIG_WIFI_DISPLAY
  9056. if (global->p2p_init_wpa_s == NULL)
  9057. return NULL;
  9058. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  9059. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  9060. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  9061. buf, resp_len);
  9062. #endif /* CONFIG_WIFI_DISPLAY */
  9063. return NULL;
  9064. }
  9065. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  9066. char *buf, size_t *resp_len)
  9067. {
  9068. char *ret;
  9069. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  9070. if (ret)
  9071. return ret;
  9072. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  9073. if (ret)
  9074. return ret;
  9075. return NULL;
  9076. }
  9077. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  9078. {
  9079. char *value;
  9080. value = os_strchr(cmd, ' ');
  9081. if (value == NULL)
  9082. return -1;
  9083. *value++ = '\0';
  9084. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  9085. #ifdef CONFIG_WIFI_DISPLAY
  9086. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  9087. wifi_display_enable(global, !!atoi(value));
  9088. return 0;
  9089. }
  9090. #endif /* CONFIG_WIFI_DISPLAY */
  9091. /* Restore cmd to its original value to allow redirection */
  9092. value[-1] = ' ';
  9093. return -1;
  9094. }
  9095. static int wpas_global_ctrl_iface_dup_network(struct wpa_global *global,
  9096. char *cmd)
  9097. {
  9098. struct wpa_supplicant *wpa_s[2]; /* src, dst */
  9099. char *p;
  9100. unsigned int i;
  9101. /* cmd: "<src ifname> <dst ifname> <src network id> <dst network id>
  9102. * <variable name> */
  9103. for (i = 0; i < ARRAY_SIZE(wpa_s) ; i++) {
  9104. p = os_strchr(cmd, ' ');
  9105. if (p == NULL)
  9106. return -1;
  9107. *p = '\0';
  9108. wpa_s[i] = global->ifaces;
  9109. for (; wpa_s[i]; wpa_s[i] = wpa_s[i]->next) {
  9110. if (os_strcmp(cmd, wpa_s[i]->ifname) == 0)
  9111. break;
  9112. }
  9113. if (!wpa_s[i]) {
  9114. wpa_printf(MSG_DEBUG,
  9115. "CTRL_IFACE: Could not find iface=%s", cmd);
  9116. return -1;
  9117. }
  9118. cmd = p + 1;
  9119. }
  9120. return wpa_supplicant_ctrl_iface_dup_network(wpa_s[0], cmd, wpa_s[1]);
  9121. }
  9122. #ifndef CONFIG_NO_CONFIG_WRITE
  9123. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  9124. {
  9125. int ret = 0, saved = 0;
  9126. struct wpa_supplicant *wpa_s;
  9127. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  9128. if (!wpa_s->conf->update_config) {
  9129. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  9130. continue;
  9131. }
  9132. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  9133. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  9134. ret = 1;
  9135. } else {
  9136. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  9137. saved++;
  9138. }
  9139. }
  9140. if (!saved && !ret) {
  9141. wpa_dbg(wpa_s, MSG_DEBUG,
  9142. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  9143. ret = 1;
  9144. }
  9145. return ret;
  9146. }
  9147. #endif /* CONFIG_NO_CONFIG_WRITE */
  9148. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  9149. char *buf, size_t buflen)
  9150. {
  9151. char *pos, *end;
  9152. int ret;
  9153. struct wpa_supplicant *wpa_s;
  9154. pos = buf;
  9155. end = buf + buflen;
  9156. #ifdef CONFIG_P2P
  9157. if (global->p2p && !global->p2p_disabled) {
  9158. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  9159. "\n"
  9160. "p2p_state=%s\n",
  9161. MAC2STR(global->p2p_dev_addr),
  9162. p2p_get_state_txt(global->p2p));
  9163. if (os_snprintf_error(end - pos, ret))
  9164. return pos - buf;
  9165. pos += ret;
  9166. } else if (global->p2p) {
  9167. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  9168. if (os_snprintf_error(end - pos, ret))
  9169. return pos - buf;
  9170. pos += ret;
  9171. }
  9172. #endif /* CONFIG_P2P */
  9173. #ifdef CONFIG_WIFI_DISPLAY
  9174. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  9175. !!global->wifi_display);
  9176. if (os_snprintf_error(end - pos, ret))
  9177. return pos - buf;
  9178. pos += ret;
  9179. #endif /* CONFIG_WIFI_DISPLAY */
  9180. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  9181. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  9182. "address=" MACSTR "\n",
  9183. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  9184. if (os_snprintf_error(end - pos, ret))
  9185. return pos - buf;
  9186. pos += ret;
  9187. }
  9188. return pos - buf;
  9189. }
  9190. #ifdef CONFIG_FST
  9191. static int wpas_global_ctrl_iface_fst_attach(struct wpa_global *global,
  9192. char *cmd, char *buf,
  9193. size_t reply_size)
  9194. {
  9195. char ifname[IFNAMSIZ + 1];
  9196. struct fst_iface_cfg cfg;
  9197. struct wpa_supplicant *wpa_s;
  9198. struct fst_wpa_obj iface_obj;
  9199. if (!fst_parse_attach_command(cmd, ifname, sizeof(ifname), &cfg)) {
  9200. wpa_s = wpa_supplicant_get_iface(global, ifname);
  9201. if (wpa_s) {
  9202. if (wpa_s->fst) {
  9203. wpa_printf(MSG_INFO, "FST: Already attached");
  9204. return -1;
  9205. }
  9206. fst_wpa_supplicant_fill_iface_obj(wpa_s, &iface_obj);
  9207. wpa_s->fst = fst_attach(ifname, wpa_s->own_addr,
  9208. &iface_obj, &cfg);
  9209. if (wpa_s->fst)
  9210. return os_snprintf(buf, reply_size, "OK\n");
  9211. }
  9212. }
  9213. return -1;
  9214. }
  9215. static int wpas_global_ctrl_iface_fst_detach(struct wpa_global *global,
  9216. char *cmd, char *buf,
  9217. size_t reply_size)
  9218. {
  9219. char ifname[IFNAMSIZ + 1];
  9220. struct wpa_supplicant *wpa_s;
  9221. if (!fst_parse_detach_command(cmd, ifname, sizeof(ifname))) {
  9222. wpa_s = wpa_supplicant_get_iface(global, ifname);
  9223. if (wpa_s) {
  9224. if (!fst_iface_detach(ifname)) {
  9225. wpa_s->fst = NULL;
  9226. return os_snprintf(buf, reply_size, "OK\n");
  9227. }
  9228. }
  9229. }
  9230. return -1;
  9231. }
  9232. #endif /* CONFIG_FST */
  9233. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  9234. char *buf, size_t *resp_len)
  9235. {
  9236. char *reply;
  9237. const int reply_size = 2048;
  9238. int reply_len;
  9239. int level = MSG_DEBUG;
  9240. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  9241. char *pos = os_strchr(buf + 7, ' ');
  9242. if (pos) {
  9243. *pos++ = '\0';
  9244. return wpas_global_ctrl_iface_ifname(global,
  9245. buf + 7, pos,
  9246. resp_len);
  9247. }
  9248. }
  9249. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  9250. if (reply)
  9251. return reply;
  9252. if (os_strcmp(buf, "PING") == 0)
  9253. level = MSG_EXCESSIVE;
  9254. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  9255. (const u8 *) buf, os_strlen(buf));
  9256. reply = os_malloc(reply_size);
  9257. if (reply == NULL) {
  9258. *resp_len = 1;
  9259. return NULL;
  9260. }
  9261. os_memcpy(reply, "OK\n", 3);
  9262. reply_len = 3;
  9263. if (os_strcmp(buf, "PING") == 0) {
  9264. os_memcpy(reply, "PONG\n", 5);
  9265. reply_len = 5;
  9266. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  9267. if (wpa_supplicant_global_iface_add(global, buf + 14))
  9268. reply_len = -1;
  9269. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  9270. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  9271. reply_len = -1;
  9272. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  9273. reply_len = wpa_supplicant_global_iface_list(
  9274. global, reply, reply_size);
  9275. } else if (os_strncmp(buf, "INTERFACES", 10) == 0) {
  9276. reply_len = wpa_supplicant_global_iface_interfaces(
  9277. global, buf + 10, reply, reply_size);
  9278. #ifdef CONFIG_FST
  9279. } else if (os_strncmp(buf, "FST-ATTACH ", 11) == 0) {
  9280. reply_len = wpas_global_ctrl_iface_fst_attach(global, buf + 11,
  9281. reply,
  9282. reply_size);
  9283. } else if (os_strncmp(buf, "FST-DETACH ", 11) == 0) {
  9284. reply_len = wpas_global_ctrl_iface_fst_detach(global, buf + 11,
  9285. reply,
  9286. reply_size);
  9287. } else if (os_strncmp(buf, "FST-MANAGER ", 12) == 0) {
  9288. reply_len = fst_ctrl_iface_receive(buf + 12, reply, reply_size);
  9289. #endif /* CONFIG_FST */
  9290. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  9291. wpa_supplicant_terminate_proc(global);
  9292. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  9293. wpas_notify_suspend(global);
  9294. } else if (os_strcmp(buf, "RESUME") == 0) {
  9295. wpas_notify_resume(global);
  9296. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  9297. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  9298. #ifdef CONFIG_P2P
  9299. if (global->p2p_init_wpa_s) {
  9300. os_free(reply);
  9301. /* Check if P2P redirection would work for this
  9302. * command. */
  9303. return wpa_supplicant_ctrl_iface_process(
  9304. global->p2p_init_wpa_s,
  9305. buf, resp_len);
  9306. }
  9307. #endif /* CONFIG_P2P */
  9308. reply_len = -1;
  9309. }
  9310. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  9311. if (wpas_global_ctrl_iface_dup_network(global, buf + 12))
  9312. reply_len = -1;
  9313. #ifndef CONFIG_NO_CONFIG_WRITE
  9314. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  9315. if (wpas_global_ctrl_iface_save_config(global))
  9316. reply_len = -1;
  9317. #endif /* CONFIG_NO_CONFIG_WRITE */
  9318. } else if (os_strcmp(buf, "STATUS") == 0) {
  9319. reply_len = wpas_global_ctrl_iface_status(global, reply,
  9320. reply_size);
  9321. #ifdef CONFIG_MODULE_TESTS
  9322. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  9323. if (wpas_module_tests() < 0)
  9324. reply_len = -1;
  9325. #endif /* CONFIG_MODULE_TESTS */
  9326. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  9327. if (wpa_debug_reopen_file() < 0)
  9328. reply_len = -1;
  9329. } else {
  9330. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  9331. reply_len = 16;
  9332. }
  9333. if (reply_len < 0) {
  9334. os_memcpy(reply, "FAIL\n", 5);
  9335. reply_len = 5;
  9336. }
  9337. *resp_len = reply_len;
  9338. return reply;
  9339. }