ctrl_iface.c 265 KB

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