ctrl_iface.c 261 KB

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