ctrl_iface.c 264 KB

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