ctrl_iface.c 264 KB

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