ctrl_iface.c 264 KB

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