ctrl_iface.c 191 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2014, 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 "common/version.h"
  17. #include "common/ieee802_11_defs.h"
  18. #include "common/ieee802_11_common.h"
  19. #include "common/wpa_ctrl.h"
  20. #include "ap/hostapd.h"
  21. #include "eap_peer/eap.h"
  22. #include "eapol_supp/eapol_supp_sm.h"
  23. #include "rsn_supp/wpa.h"
  24. #include "rsn_supp/preauth.h"
  25. #include "rsn_supp/pmksa_cache.h"
  26. #include "l2_packet/l2_packet.h"
  27. #include "wps/wps.h"
  28. #include "config.h"
  29. #include "wpa_supplicant_i.h"
  30. #include "driver_i.h"
  31. #include "wps_supplicant.h"
  32. #include "ibss_rsn.h"
  33. #include "ap.h"
  34. #include "p2p_supplicant.h"
  35. #include "p2p/p2p.h"
  36. #include "hs20_supplicant.h"
  37. #include "wifi_display.h"
  38. #include "notify.h"
  39. #include "bss.h"
  40. #include "scan.h"
  41. #include "ctrl_iface.h"
  42. #include "interworking.h"
  43. #include "blacklist.h"
  44. #include "autoscan.h"
  45. #include "wnm_sta.h"
  46. #include "offchannel.h"
  47. #include "drivers/driver.h"
  48. #include "mesh.h"
  49. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  50. char *buf, int len);
  51. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  52. char *buf, int len);
  53. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s,
  54. char *val);
  55. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  56. {
  57. char *pos;
  58. u8 addr[ETH_ALEN], *filter = NULL, *n;
  59. size_t count = 0;
  60. pos = val;
  61. while (pos) {
  62. if (*pos == '\0')
  63. break;
  64. if (hwaddr_aton(pos, addr)) {
  65. os_free(filter);
  66. return -1;
  67. }
  68. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  69. if (n == NULL) {
  70. os_free(filter);
  71. return -1;
  72. }
  73. filter = n;
  74. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  75. count++;
  76. pos = os_strchr(pos, ' ');
  77. if (pos)
  78. pos++;
  79. }
  80. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  81. os_free(wpa_s->bssid_filter);
  82. wpa_s->bssid_filter = filter;
  83. wpa_s->bssid_filter_count = count;
  84. return 0;
  85. }
  86. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  87. {
  88. char *pos;
  89. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  90. struct wpa_ssid_value *ssid = NULL, *ns;
  91. size_t count = 0, ssid_count = 0;
  92. struct wpa_ssid *c;
  93. /*
  94. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  95. * SSID_SPEC ::= ssid <SSID_HEX>
  96. * BSSID_SPEC ::= bssid <BSSID_HEX>
  97. */
  98. pos = val;
  99. while (pos) {
  100. if (*pos == '\0')
  101. break;
  102. if (os_strncmp(pos, "bssid ", 6) == 0) {
  103. int res;
  104. pos += 6;
  105. res = hwaddr_aton2(pos, addr);
  106. if (res < 0) {
  107. os_free(ssid);
  108. os_free(bssid);
  109. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  110. "BSSID value '%s'", pos);
  111. return -1;
  112. }
  113. pos += res;
  114. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  115. if (n == NULL) {
  116. os_free(ssid);
  117. os_free(bssid);
  118. return -1;
  119. }
  120. bssid = n;
  121. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  122. count++;
  123. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  124. char *end;
  125. pos += 5;
  126. end = pos;
  127. while (*end) {
  128. if (*end == '\0' || *end == ' ')
  129. break;
  130. end++;
  131. }
  132. ns = os_realloc_array(ssid, ssid_count + 1,
  133. sizeof(struct wpa_ssid_value));
  134. if (ns == NULL) {
  135. os_free(ssid);
  136. os_free(bssid);
  137. return -1;
  138. }
  139. ssid = ns;
  140. if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
  141. hexstr2bin(pos, ssid[ssid_count].ssid,
  142. (end - pos) / 2) < 0) {
  143. os_free(ssid);
  144. os_free(bssid);
  145. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  146. "SSID value '%s'", pos);
  147. return -1;
  148. }
  149. ssid[ssid_count].ssid_len = (end - pos) / 2;
  150. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  151. ssid[ssid_count].ssid,
  152. ssid[ssid_count].ssid_len);
  153. ssid_count++;
  154. pos = end;
  155. } else {
  156. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  157. "'%s'", pos);
  158. os_free(ssid);
  159. os_free(bssid);
  160. return -1;
  161. }
  162. pos = os_strchr(pos, ' ');
  163. if (pos)
  164. pos++;
  165. }
  166. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  167. os_free(wpa_s->disallow_aps_bssid);
  168. wpa_s->disallow_aps_bssid = bssid;
  169. wpa_s->disallow_aps_bssid_count = count;
  170. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  171. os_free(wpa_s->disallow_aps_ssid);
  172. wpa_s->disallow_aps_ssid = ssid;
  173. wpa_s->disallow_aps_ssid_count = ssid_count;
  174. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  175. return 0;
  176. c = wpa_s->current_ssid;
  177. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  178. return 0;
  179. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  180. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  181. return 0;
  182. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  183. "because current AP was marked disallowed");
  184. #ifdef CONFIG_SME
  185. wpa_s->sme.prev_bssid_set = 0;
  186. #endif /* CONFIG_SME */
  187. wpa_s->reassociate = 1;
  188. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  189. wpa_supplicant_req_scan(wpa_s, 0, 0);
  190. return 0;
  191. }
  192. #ifndef CONFIG_NO_CONFIG_BLOBS
  193. static int wpas_ctrl_set_blob(struct wpa_supplicant *wpa_s, char *pos)
  194. {
  195. char *name = pos;
  196. struct wpa_config_blob *blob;
  197. size_t len;
  198. pos = os_strchr(pos, ' ');
  199. if (pos == NULL)
  200. return -1;
  201. *pos++ = '\0';
  202. len = os_strlen(pos);
  203. if (len & 1)
  204. return -1;
  205. wpa_printf(MSG_DEBUG, "CTRL: Set blob '%s'", name);
  206. blob = os_zalloc(sizeof(*blob));
  207. if (blob == NULL)
  208. return -1;
  209. blob->name = os_strdup(name);
  210. blob->data = os_malloc(len / 2);
  211. if (blob->name == NULL || blob->data == NULL) {
  212. wpa_config_free_blob(blob);
  213. return -1;
  214. }
  215. if (hexstr2bin(pos, blob->data, len / 2) < 0) {
  216. wpa_printf(MSG_DEBUG, "CTRL: Invalid blob hex data");
  217. wpa_config_free_blob(blob);
  218. return -1;
  219. }
  220. blob->len = len / 2;
  221. wpa_config_set_blob(wpa_s->conf, blob);
  222. return 0;
  223. }
  224. #endif /* CONFIG_NO_CONFIG_BLOBS */
  225. static int wpas_ctrl_pno(struct wpa_supplicant *wpa_s, char *cmd)
  226. {
  227. char *params;
  228. char *pos;
  229. int *freqs = NULL;
  230. int ret;
  231. if (atoi(cmd)) {
  232. params = os_strchr(cmd, ' ');
  233. os_free(wpa_s->manual_sched_scan_freqs);
  234. if (params) {
  235. params++;
  236. pos = os_strstr(params, "freq=");
  237. if (pos)
  238. freqs = freq_range_to_channel_list(wpa_s,
  239. pos + 5);
  240. }
  241. wpa_s->manual_sched_scan_freqs = freqs;
  242. ret = wpas_start_pno(wpa_s);
  243. } else {
  244. ret = wpas_stop_pno(wpa_s);
  245. }
  246. return ret;
  247. }
  248. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  249. char *cmd)
  250. {
  251. char *value;
  252. int ret = 0;
  253. value = os_strchr(cmd, ' ');
  254. if (value == NULL)
  255. return -1;
  256. *value++ = '\0';
  257. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  258. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  259. eapol_sm_configure(wpa_s->eapol,
  260. atoi(value), -1, -1, -1);
  261. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  262. eapol_sm_configure(wpa_s->eapol,
  263. -1, atoi(value), -1, -1);
  264. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  265. eapol_sm_configure(wpa_s->eapol,
  266. -1, -1, atoi(value), -1);
  267. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  268. eapol_sm_configure(wpa_s->eapol,
  269. -1, -1, -1, atoi(value));
  270. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  271. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  272. atoi(value)))
  273. ret = -1;
  274. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  275. 0) {
  276. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  277. atoi(value)))
  278. ret = -1;
  279. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  280. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  281. ret = -1;
  282. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  283. wpa_s->wps_fragment_size = atoi(value);
  284. #ifdef CONFIG_WPS_TESTING
  285. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  286. long int val;
  287. val = strtol(value, NULL, 0);
  288. if (val < 0 || val > 0xff) {
  289. ret = -1;
  290. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  291. "wps_version_number %ld", val);
  292. } else {
  293. wps_version_number = val;
  294. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  295. "version %u.%u",
  296. (wps_version_number & 0xf0) >> 4,
  297. wps_version_number & 0x0f);
  298. }
  299. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  300. wps_testing_dummy_cred = atoi(value);
  301. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  302. wps_testing_dummy_cred);
  303. } else if (os_strcasecmp(cmd, "wps_corrupt_pkhash") == 0) {
  304. wps_corrupt_pkhash = atoi(value);
  305. wpa_printf(MSG_DEBUG, "WPS: Testing - wps_corrupt_pkhash=%d",
  306. wps_corrupt_pkhash);
  307. #endif /* CONFIG_WPS_TESTING */
  308. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  309. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  310. ret = -1;
  311. #ifdef CONFIG_TDLS
  312. #ifdef CONFIG_TDLS_TESTING
  313. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  314. extern unsigned int tdls_testing;
  315. tdls_testing = strtol(value, NULL, 0);
  316. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  317. #endif /* CONFIG_TDLS_TESTING */
  318. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  319. int disabled = atoi(value);
  320. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  321. if (disabled) {
  322. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  323. ret = -1;
  324. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  325. ret = -1;
  326. wpa_tdls_enable(wpa_s->wpa, !disabled);
  327. #endif /* CONFIG_TDLS */
  328. } else if (os_strcasecmp(cmd, "pno") == 0) {
  329. ret = wpas_ctrl_pno(wpa_s, value);
  330. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  331. int disabled = atoi(value);
  332. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  333. ret = -1;
  334. else if (disabled)
  335. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  336. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  337. if (os_strcmp(value, "disable") == 0)
  338. wpa_s->set_sta_uapsd = 0;
  339. else {
  340. int be, bk, vi, vo;
  341. char *pos;
  342. /* format: BE,BK,VI,VO;max SP Length */
  343. be = atoi(value);
  344. pos = os_strchr(value, ',');
  345. if (pos == NULL)
  346. return -1;
  347. pos++;
  348. bk = atoi(pos);
  349. pos = os_strchr(pos, ',');
  350. if (pos == NULL)
  351. return -1;
  352. pos++;
  353. vi = atoi(pos);
  354. pos = os_strchr(pos, ',');
  355. if (pos == NULL)
  356. return -1;
  357. pos++;
  358. vo = atoi(pos);
  359. /* ignore max SP Length for now */
  360. wpa_s->set_sta_uapsd = 1;
  361. wpa_s->sta_uapsd = 0;
  362. if (be)
  363. wpa_s->sta_uapsd |= BIT(0);
  364. if (bk)
  365. wpa_s->sta_uapsd |= BIT(1);
  366. if (vi)
  367. wpa_s->sta_uapsd |= BIT(2);
  368. if (vo)
  369. wpa_s->sta_uapsd |= BIT(3);
  370. }
  371. } else if (os_strcasecmp(cmd, "ps") == 0) {
  372. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  373. #ifdef CONFIG_WIFI_DISPLAY
  374. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  375. int enabled = !!atoi(value);
  376. if (enabled && !wpa_s->global->p2p)
  377. ret = -1;
  378. else
  379. wifi_display_enable(wpa_s->global, enabled);
  380. #endif /* CONFIG_WIFI_DISPLAY */
  381. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  382. ret = set_bssid_filter(wpa_s, value);
  383. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  384. ret = set_disallow_aps(wpa_s, value);
  385. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  386. wpa_s->no_keep_alive = !!atoi(value);
  387. #ifdef CONFIG_TESTING_OPTIONS
  388. } else if (os_strcasecmp(cmd, "ext_mgmt_frame_handling") == 0) {
  389. wpa_s->ext_mgmt_frame_handling = !!atoi(value);
  390. } else if (os_strcasecmp(cmd, "ext_eapol_frame_io") == 0) {
  391. wpa_s->ext_eapol_frame_io = !!atoi(value);
  392. #ifdef CONFIG_AP
  393. if (wpa_s->ap_iface) {
  394. wpa_s->ap_iface->bss[0]->ext_eapol_frame_io =
  395. wpa_s->ext_eapol_frame_io;
  396. }
  397. #endif /* CONFIG_AP */
  398. } else if (os_strcasecmp(cmd, "extra_roc_dur") == 0) {
  399. wpa_s->extra_roc_dur = atoi(value);
  400. #endif /* CONFIG_TESTING_OPTIONS */
  401. #ifndef CONFIG_NO_CONFIG_BLOBS
  402. } else if (os_strcmp(cmd, "blob") == 0) {
  403. ret = wpas_ctrl_set_blob(wpa_s, value);
  404. #endif /* CONFIG_NO_CONFIG_BLOBS */
  405. } else {
  406. value[-1] = '=';
  407. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  408. if (ret == 0)
  409. wpa_supplicant_update_config(wpa_s);
  410. }
  411. return ret;
  412. }
  413. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  414. char *cmd, char *buf, size_t buflen)
  415. {
  416. int res = -1;
  417. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  418. if (os_strcmp(cmd, "version") == 0) {
  419. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  420. } else if (os_strcasecmp(cmd, "country") == 0) {
  421. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  422. res = os_snprintf(buf, buflen, "%c%c",
  423. wpa_s->conf->country[0],
  424. wpa_s->conf->country[1]);
  425. #ifdef CONFIG_WIFI_DISPLAY
  426. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  427. int enabled;
  428. if (wpa_s->global->p2p == NULL ||
  429. wpa_s->global->p2p_disabled)
  430. enabled = 0;
  431. else
  432. enabled = wpa_s->global->wifi_display;
  433. res = os_snprintf(buf, buflen, "%d", enabled);
  434. #endif /* CONFIG_WIFI_DISPLAY */
  435. #ifdef CONFIG_TESTING_GET_GTK
  436. } else if (os_strcmp(cmd, "gtk") == 0) {
  437. if (wpa_s->last_gtk_len == 0)
  438. return -1;
  439. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  440. wpa_s->last_gtk_len);
  441. return res;
  442. #endif /* CONFIG_TESTING_GET_GTK */
  443. }
  444. if (os_snprintf_error(buflen, res))
  445. return -1;
  446. return res;
  447. }
  448. #ifdef IEEE8021X_EAPOL
  449. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  450. char *addr)
  451. {
  452. u8 bssid[ETH_ALEN];
  453. struct wpa_ssid *ssid = wpa_s->current_ssid;
  454. if (hwaddr_aton(addr, bssid)) {
  455. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  456. "'%s'", addr);
  457. return -1;
  458. }
  459. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  460. rsn_preauth_deinit(wpa_s->wpa);
  461. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  462. return -1;
  463. return 0;
  464. }
  465. #endif /* IEEE8021X_EAPOL */
  466. #ifdef CONFIG_PEERKEY
  467. /* MLME-STKSTART.request(peer) */
  468. static int wpa_supplicant_ctrl_iface_stkstart(
  469. struct wpa_supplicant *wpa_s, char *addr)
  470. {
  471. u8 peer[ETH_ALEN];
  472. if (hwaddr_aton(addr, peer)) {
  473. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  474. "address '%s'", addr);
  475. return -1;
  476. }
  477. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  478. MAC2STR(peer));
  479. return wpa_sm_stkstart(wpa_s->wpa, peer);
  480. }
  481. #endif /* CONFIG_PEERKEY */
  482. #ifdef CONFIG_TDLS
  483. static int wpa_supplicant_ctrl_iface_tdls_discover(
  484. struct wpa_supplicant *wpa_s, char *addr)
  485. {
  486. u8 peer[ETH_ALEN];
  487. int ret;
  488. if (hwaddr_aton(addr, peer)) {
  489. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  490. "address '%s'", addr);
  491. return -1;
  492. }
  493. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  494. MAC2STR(peer));
  495. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  496. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  497. else
  498. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  499. return ret;
  500. }
  501. static int wpa_supplicant_ctrl_iface_tdls_setup(
  502. struct wpa_supplicant *wpa_s, char *addr)
  503. {
  504. u8 peer[ETH_ALEN];
  505. int ret;
  506. if (hwaddr_aton(addr, peer)) {
  507. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  508. "address '%s'", addr);
  509. return -1;
  510. }
  511. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  512. MAC2STR(peer));
  513. if ((wpa_s->conf->tdls_external_control) &&
  514. wpa_tdls_is_external_setup(wpa_s->wpa))
  515. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  516. wpa_tdls_remove(wpa_s->wpa, peer);
  517. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  518. ret = wpa_tdls_start(wpa_s->wpa, peer);
  519. else
  520. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  521. return ret;
  522. }
  523. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  524. struct wpa_supplicant *wpa_s, char *addr)
  525. {
  526. u8 peer[ETH_ALEN];
  527. int ret;
  528. if (os_strcmp(addr, "*") == 0) {
  529. /* remove everyone */
  530. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN *");
  531. wpa_tdls_teardown_peers(wpa_s->wpa);
  532. return 0;
  533. }
  534. if (hwaddr_aton(addr, peer)) {
  535. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  536. "address '%s'", addr);
  537. return -1;
  538. }
  539. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  540. MAC2STR(peer));
  541. if ((wpa_s->conf->tdls_external_control) &&
  542. wpa_tdls_is_external_setup(wpa_s->wpa))
  543. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  544. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  545. ret = wpa_tdls_teardown_link(
  546. wpa_s->wpa, peer,
  547. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  548. else
  549. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  550. return ret;
  551. }
  552. static int ctrl_iface_get_capability_tdls(
  553. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  554. {
  555. int ret;
  556. ret = os_snprintf(buf, buflen, "%s\n",
  557. wpa_s->drv_flags & WPA_DRIVER_FLAGS_TDLS_SUPPORT ?
  558. (wpa_s->drv_flags &
  559. WPA_DRIVER_FLAGS_TDLS_EXTERNAL_SETUP ?
  560. "EXTERNAL" : "INTERNAL") : "UNSUPPORTED");
  561. if (os_snprintf_error(buflen, ret))
  562. return -1;
  563. return ret;
  564. }
  565. #endif /* CONFIG_TDLS */
  566. static int wmm_ac_ctrl_addts(struct wpa_supplicant *wpa_s, char *cmd)
  567. {
  568. char *token, *context = NULL;
  569. struct wmm_ac_ts_setup_params params = {
  570. .tsid = 0xff,
  571. .direction = 0xff,
  572. };
  573. while ((token = str_token(cmd, " ", &context))) {
  574. if (sscanf(token, "tsid=%i", &params.tsid) == 1 ||
  575. sscanf(token, "up=%i", &params.user_priority) == 1 ||
  576. sscanf(token, "nominal_msdu_size=%i",
  577. &params.nominal_msdu_size) == 1 ||
  578. sscanf(token, "mean_data_rate=%i",
  579. &params.mean_data_rate) == 1 ||
  580. sscanf(token, "min_phy_rate=%i",
  581. &params.minimum_phy_rate) == 1 ||
  582. sscanf(token, "sba=%i",
  583. &params.surplus_bandwidth_allowance) == 1)
  584. continue;
  585. if (os_strcasecmp(token, "downlink") == 0) {
  586. params.direction = WMM_TSPEC_DIRECTION_DOWNLINK;
  587. } else if (os_strcasecmp(token, "uplink") == 0) {
  588. params.direction = WMM_TSPEC_DIRECTION_UPLINK;
  589. } else if (os_strcasecmp(token, "bidi") == 0) {
  590. params.direction = WMM_TSPEC_DIRECTION_BI_DIRECTIONAL;
  591. } else if (os_strcasecmp(token, "fixed_nominal_msdu") == 0) {
  592. params.fixed_nominal_msdu = 1;
  593. } else {
  594. wpa_printf(MSG_DEBUG,
  595. "CTRL: Invalid WMM_AC_ADDTS parameter: '%s'",
  596. token);
  597. return -1;
  598. }
  599. }
  600. return wpas_wmm_ac_addts(wpa_s, &params);
  601. }
  602. static int wmm_ac_ctrl_delts(struct wpa_supplicant *wpa_s, char *cmd)
  603. {
  604. u8 tsid = atoi(cmd);
  605. return wpas_wmm_ac_delts(wpa_s, tsid);
  606. }
  607. #ifdef CONFIG_IEEE80211R
  608. static int wpa_supplicant_ctrl_iface_ft_ds(
  609. struct wpa_supplicant *wpa_s, char *addr)
  610. {
  611. u8 target_ap[ETH_ALEN];
  612. struct wpa_bss *bss;
  613. const u8 *mdie;
  614. if (hwaddr_aton(addr, target_ap)) {
  615. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  616. "address '%s'", addr);
  617. return -1;
  618. }
  619. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  620. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  621. if (bss)
  622. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  623. else
  624. mdie = NULL;
  625. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  626. }
  627. #endif /* CONFIG_IEEE80211R */
  628. #ifdef CONFIG_WPS
  629. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  630. char *cmd)
  631. {
  632. u8 bssid[ETH_ALEN], *_bssid = bssid;
  633. #ifdef CONFIG_P2P
  634. u8 p2p_dev_addr[ETH_ALEN];
  635. #endif /* CONFIG_P2P */
  636. #ifdef CONFIG_AP
  637. u8 *_p2p_dev_addr = NULL;
  638. #endif /* CONFIG_AP */
  639. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  640. _bssid = NULL;
  641. #ifdef CONFIG_P2P
  642. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  643. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  644. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  645. "P2P Device Address '%s'",
  646. cmd + 13);
  647. return -1;
  648. }
  649. _p2p_dev_addr = p2p_dev_addr;
  650. #endif /* CONFIG_P2P */
  651. } else if (hwaddr_aton(cmd, bssid)) {
  652. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  653. cmd);
  654. return -1;
  655. }
  656. #ifdef CONFIG_AP
  657. if (wpa_s->ap_iface)
  658. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  659. #endif /* CONFIG_AP */
  660. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  661. }
  662. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  663. char *cmd, char *buf,
  664. size_t buflen)
  665. {
  666. u8 bssid[ETH_ALEN], *_bssid = bssid;
  667. char *pin;
  668. int ret;
  669. pin = os_strchr(cmd, ' ');
  670. if (pin)
  671. *pin++ = '\0';
  672. if (os_strcmp(cmd, "any") == 0)
  673. _bssid = NULL;
  674. else if (os_strcmp(cmd, "get") == 0) {
  675. ret = wps_generate_pin();
  676. goto done;
  677. } else if (hwaddr_aton(cmd, bssid)) {
  678. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  679. cmd);
  680. return -1;
  681. }
  682. #ifdef CONFIG_AP
  683. if (wpa_s->ap_iface) {
  684. int timeout = 0;
  685. char *pos;
  686. if (pin) {
  687. pos = os_strchr(pin, ' ');
  688. if (pos) {
  689. *pos++ = '\0';
  690. timeout = atoi(pos);
  691. }
  692. }
  693. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  694. buf, buflen, timeout);
  695. }
  696. #endif /* CONFIG_AP */
  697. if (pin) {
  698. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  699. DEV_PW_DEFAULT);
  700. if (ret < 0)
  701. return -1;
  702. ret = os_snprintf(buf, buflen, "%s", pin);
  703. if (os_snprintf_error(buflen, ret))
  704. return -1;
  705. return ret;
  706. }
  707. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  708. if (ret < 0)
  709. return -1;
  710. done:
  711. /* Return the generated PIN */
  712. ret = os_snprintf(buf, buflen, "%08d", ret);
  713. if (os_snprintf_error(buflen, ret))
  714. return -1;
  715. return ret;
  716. }
  717. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  718. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  719. {
  720. char pin[9];
  721. size_t len;
  722. char *pos;
  723. int ret;
  724. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  725. (u8 *) cmd, os_strlen(cmd));
  726. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  727. if (*pos < '0' || *pos > '9')
  728. continue;
  729. pin[len++] = *pos;
  730. if (len == 9) {
  731. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  732. return -1;
  733. }
  734. }
  735. if (len != 4 && len != 8) {
  736. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  737. return -1;
  738. }
  739. pin[len] = '\0';
  740. if (len == 8) {
  741. unsigned int pin_val;
  742. pin_val = atoi(pin);
  743. if (!wps_pin_valid(pin_val)) {
  744. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  745. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  746. if (os_snprintf_error(buflen, ret))
  747. return -1;
  748. return ret;
  749. }
  750. }
  751. ret = os_snprintf(buf, buflen, "%s", pin);
  752. if (os_snprintf_error(buflen, ret))
  753. return -1;
  754. return ret;
  755. }
  756. #ifdef CONFIG_WPS_NFC
  757. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  758. char *cmd)
  759. {
  760. u8 bssid[ETH_ALEN], *_bssid = bssid;
  761. if (cmd == NULL || cmd[0] == '\0')
  762. _bssid = NULL;
  763. else if (hwaddr_aton(cmd, bssid))
  764. return -1;
  765. return wpas_wps_start_nfc(wpa_s, NULL, _bssid, NULL, 0, 0, NULL, NULL,
  766. 0, 0);
  767. }
  768. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  769. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  770. {
  771. int ndef;
  772. struct wpabuf *buf;
  773. int res;
  774. char *pos;
  775. pos = os_strchr(cmd, ' ');
  776. if (pos)
  777. *pos++ = '\0';
  778. if (os_strcmp(cmd, "WPS") == 0)
  779. ndef = 0;
  780. else if (os_strcmp(cmd, "NDEF") == 0)
  781. ndef = 1;
  782. else
  783. return -1;
  784. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  785. if (buf == NULL)
  786. return -1;
  787. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  788. wpabuf_len(buf));
  789. reply[res++] = '\n';
  790. reply[res] = '\0';
  791. wpabuf_free(buf);
  792. return res;
  793. }
  794. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  795. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  796. {
  797. int ndef;
  798. struct wpabuf *buf;
  799. int res;
  800. if (os_strcmp(cmd, "WPS") == 0)
  801. ndef = 0;
  802. else if (os_strcmp(cmd, "NDEF") == 0)
  803. ndef = 1;
  804. else
  805. return -1;
  806. buf = wpas_wps_nfc_token(wpa_s, ndef);
  807. if (buf == NULL)
  808. return -1;
  809. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  810. wpabuf_len(buf));
  811. reply[res++] = '\n';
  812. reply[res] = '\0';
  813. wpabuf_free(buf);
  814. return res;
  815. }
  816. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  817. struct wpa_supplicant *wpa_s, char *pos)
  818. {
  819. size_t len;
  820. struct wpabuf *buf;
  821. int ret;
  822. char *freq;
  823. int forced_freq = 0;
  824. freq = strstr(pos, " freq=");
  825. if (freq) {
  826. *freq = '\0';
  827. freq += 6;
  828. forced_freq = atoi(freq);
  829. }
  830. len = os_strlen(pos);
  831. if (len & 0x01)
  832. return -1;
  833. len /= 2;
  834. buf = wpabuf_alloc(len);
  835. if (buf == NULL)
  836. return -1;
  837. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  838. wpabuf_free(buf);
  839. return -1;
  840. }
  841. ret = wpas_wps_nfc_tag_read(wpa_s, buf, forced_freq);
  842. wpabuf_free(buf);
  843. return ret;
  844. }
  845. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  846. char *reply, size_t max_len,
  847. int ndef)
  848. {
  849. struct wpabuf *buf;
  850. int res;
  851. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  852. if (buf == NULL)
  853. return -1;
  854. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  855. wpabuf_len(buf));
  856. reply[res++] = '\n';
  857. reply[res] = '\0';
  858. wpabuf_free(buf);
  859. return res;
  860. }
  861. #ifdef CONFIG_P2P
  862. static int wpas_ctrl_nfc_get_handover_req_p2p(struct wpa_supplicant *wpa_s,
  863. char *reply, size_t max_len,
  864. int ndef)
  865. {
  866. struct wpabuf *buf;
  867. int res;
  868. buf = wpas_p2p_nfc_handover_req(wpa_s, ndef);
  869. if (buf == NULL) {
  870. wpa_printf(MSG_DEBUG, "P2P: Could not generate NFC handover request");
  871. return -1;
  872. }
  873. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  874. wpabuf_len(buf));
  875. reply[res++] = '\n';
  876. reply[res] = '\0';
  877. wpabuf_free(buf);
  878. return res;
  879. }
  880. #endif /* CONFIG_P2P */
  881. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  882. char *cmd, char *reply,
  883. size_t max_len)
  884. {
  885. char *pos;
  886. int ndef;
  887. pos = os_strchr(cmd, ' ');
  888. if (pos == NULL)
  889. return -1;
  890. *pos++ = '\0';
  891. if (os_strcmp(cmd, "WPS") == 0)
  892. ndef = 0;
  893. else if (os_strcmp(cmd, "NDEF") == 0)
  894. ndef = 1;
  895. else
  896. return -1;
  897. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  898. if (!ndef)
  899. return -1;
  900. return wpas_ctrl_nfc_get_handover_req_wps(
  901. wpa_s, reply, max_len, ndef);
  902. }
  903. #ifdef CONFIG_P2P
  904. if (os_strcmp(pos, "P2P-CR") == 0) {
  905. return wpas_ctrl_nfc_get_handover_req_p2p(
  906. wpa_s, reply, max_len, ndef);
  907. }
  908. #endif /* CONFIG_P2P */
  909. return -1;
  910. }
  911. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  912. char *reply, size_t max_len,
  913. int ndef, int cr, char *uuid)
  914. {
  915. struct wpabuf *buf;
  916. int res;
  917. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  918. if (buf == NULL)
  919. return -1;
  920. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  921. wpabuf_len(buf));
  922. reply[res++] = '\n';
  923. reply[res] = '\0';
  924. wpabuf_free(buf);
  925. return res;
  926. }
  927. #ifdef CONFIG_P2P
  928. static int wpas_ctrl_nfc_get_handover_sel_p2p(struct wpa_supplicant *wpa_s,
  929. char *reply, size_t max_len,
  930. int ndef, int tag)
  931. {
  932. struct wpabuf *buf;
  933. int res;
  934. buf = wpas_p2p_nfc_handover_sel(wpa_s, ndef, tag);
  935. if (buf == NULL)
  936. return -1;
  937. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  938. wpabuf_len(buf));
  939. reply[res++] = '\n';
  940. reply[res] = '\0';
  941. wpabuf_free(buf);
  942. return res;
  943. }
  944. #endif /* CONFIG_P2P */
  945. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  946. char *cmd, char *reply,
  947. size_t max_len)
  948. {
  949. char *pos, *pos2;
  950. int ndef;
  951. pos = os_strchr(cmd, ' ');
  952. if (pos == NULL)
  953. return -1;
  954. *pos++ = '\0';
  955. if (os_strcmp(cmd, "WPS") == 0)
  956. ndef = 0;
  957. else if (os_strcmp(cmd, "NDEF") == 0)
  958. ndef = 1;
  959. else
  960. return -1;
  961. pos2 = os_strchr(pos, ' ');
  962. if (pos2)
  963. *pos2++ = '\0';
  964. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  965. if (!ndef)
  966. return -1;
  967. return wpas_ctrl_nfc_get_handover_sel_wps(
  968. wpa_s, reply, max_len, ndef,
  969. os_strcmp(pos, "WPS-CR") == 0, pos2);
  970. }
  971. #ifdef CONFIG_P2P
  972. if (os_strcmp(pos, "P2P-CR") == 0) {
  973. return wpas_ctrl_nfc_get_handover_sel_p2p(
  974. wpa_s, reply, max_len, ndef, 0);
  975. }
  976. if (os_strcmp(pos, "P2P-CR-TAG") == 0) {
  977. return wpas_ctrl_nfc_get_handover_sel_p2p(
  978. wpa_s, reply, max_len, ndef, 1);
  979. }
  980. #endif /* CONFIG_P2P */
  981. return -1;
  982. }
  983. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  984. char *cmd)
  985. {
  986. size_t len;
  987. struct wpabuf *req, *sel;
  988. int ret;
  989. char *pos, *role, *type, *pos2;
  990. #ifdef CONFIG_P2P
  991. char *freq;
  992. int forced_freq = 0;
  993. freq = strstr(cmd, " freq=");
  994. if (freq) {
  995. *freq = '\0';
  996. freq += 6;
  997. forced_freq = atoi(freq);
  998. }
  999. #endif /* CONFIG_P2P */
  1000. role = cmd;
  1001. pos = os_strchr(role, ' ');
  1002. if (pos == NULL) {
  1003. wpa_printf(MSG_DEBUG, "NFC: Missing type in handover report");
  1004. return -1;
  1005. }
  1006. *pos++ = '\0';
  1007. type = pos;
  1008. pos = os_strchr(type, ' ');
  1009. if (pos == NULL) {
  1010. wpa_printf(MSG_DEBUG, "NFC: Missing request message in handover report");
  1011. return -1;
  1012. }
  1013. *pos++ = '\0';
  1014. pos2 = os_strchr(pos, ' ');
  1015. if (pos2 == NULL) {
  1016. wpa_printf(MSG_DEBUG, "NFC: Missing select message in handover report");
  1017. return -1;
  1018. }
  1019. *pos2++ = '\0';
  1020. len = os_strlen(pos);
  1021. if (len & 0x01) {
  1022. wpa_printf(MSG_DEBUG, "NFC: Invalid request message length in handover report");
  1023. return -1;
  1024. }
  1025. len /= 2;
  1026. req = wpabuf_alloc(len);
  1027. if (req == NULL) {
  1028. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for request message");
  1029. return -1;
  1030. }
  1031. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  1032. wpa_printf(MSG_DEBUG, "NFC: Invalid request message hexdump in handover report");
  1033. wpabuf_free(req);
  1034. return -1;
  1035. }
  1036. len = os_strlen(pos2);
  1037. if (len & 0x01) {
  1038. wpa_printf(MSG_DEBUG, "NFC: Invalid select message length in handover report");
  1039. wpabuf_free(req);
  1040. return -1;
  1041. }
  1042. len /= 2;
  1043. sel = wpabuf_alloc(len);
  1044. if (sel == NULL) {
  1045. wpa_printf(MSG_DEBUG, "NFC: Failed to allocate memory for select message");
  1046. wpabuf_free(req);
  1047. return -1;
  1048. }
  1049. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  1050. wpa_printf(MSG_DEBUG, "NFC: Invalid select message hexdump in handover report");
  1051. wpabuf_free(req);
  1052. wpabuf_free(sel);
  1053. return -1;
  1054. }
  1055. wpa_printf(MSG_DEBUG, "NFC: Connection handover reported - role=%s type=%s req_len=%d sel_len=%d",
  1056. role, type, (int) wpabuf_len(req), (int) wpabuf_len(sel));
  1057. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  1058. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  1059. #ifdef CONFIG_AP
  1060. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "WPS") == 0)
  1061. {
  1062. ret = wpas_ap_wps_nfc_report_handover(wpa_s, req, sel);
  1063. if (ret < 0)
  1064. ret = wpas_er_wps_nfc_report_handover(wpa_s, req, sel);
  1065. #endif /* CONFIG_AP */
  1066. #ifdef CONFIG_P2P
  1067. } else if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "P2P") == 0)
  1068. {
  1069. ret = wpas_p2p_nfc_report_handover(wpa_s, 1, req, sel, 0);
  1070. } else if (os_strcmp(role, "RESP") == 0 && os_strcmp(type, "P2P") == 0)
  1071. {
  1072. ret = wpas_p2p_nfc_report_handover(wpa_s, 0, req, sel,
  1073. forced_freq);
  1074. #endif /* CONFIG_P2P */
  1075. } else {
  1076. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  1077. "reported: role=%s type=%s", role, type);
  1078. ret = -1;
  1079. }
  1080. wpabuf_free(req);
  1081. wpabuf_free(sel);
  1082. if (ret)
  1083. wpa_printf(MSG_DEBUG, "NFC: Failed to process reported handover messages");
  1084. return ret;
  1085. }
  1086. #endif /* CONFIG_WPS_NFC */
  1087. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  1088. char *cmd)
  1089. {
  1090. u8 bssid[ETH_ALEN];
  1091. char *pin;
  1092. char *new_ssid;
  1093. char *new_auth;
  1094. char *new_encr;
  1095. char *new_key;
  1096. struct wps_new_ap_settings ap;
  1097. pin = os_strchr(cmd, ' ');
  1098. if (pin == NULL)
  1099. return -1;
  1100. *pin++ = '\0';
  1101. if (hwaddr_aton(cmd, bssid)) {
  1102. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  1103. cmd);
  1104. return -1;
  1105. }
  1106. new_ssid = os_strchr(pin, ' ');
  1107. if (new_ssid == NULL)
  1108. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  1109. *new_ssid++ = '\0';
  1110. new_auth = os_strchr(new_ssid, ' ');
  1111. if (new_auth == NULL)
  1112. return -1;
  1113. *new_auth++ = '\0';
  1114. new_encr = os_strchr(new_auth, ' ');
  1115. if (new_encr == NULL)
  1116. return -1;
  1117. *new_encr++ = '\0';
  1118. new_key = os_strchr(new_encr, ' ');
  1119. if (new_key == NULL)
  1120. return -1;
  1121. *new_key++ = '\0';
  1122. os_memset(&ap, 0, sizeof(ap));
  1123. ap.ssid_hex = new_ssid;
  1124. ap.auth = new_auth;
  1125. ap.encr = new_encr;
  1126. ap.key_hex = new_key;
  1127. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  1128. }
  1129. #ifdef CONFIG_AP
  1130. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  1131. char *cmd, char *buf,
  1132. size_t buflen)
  1133. {
  1134. int timeout = 300;
  1135. char *pos;
  1136. const char *pin_txt;
  1137. if (!wpa_s->ap_iface)
  1138. return -1;
  1139. pos = os_strchr(cmd, ' ');
  1140. if (pos)
  1141. *pos++ = '\0';
  1142. if (os_strcmp(cmd, "disable") == 0) {
  1143. wpas_wps_ap_pin_disable(wpa_s);
  1144. return os_snprintf(buf, buflen, "OK\n");
  1145. }
  1146. if (os_strcmp(cmd, "random") == 0) {
  1147. if (pos)
  1148. timeout = atoi(pos);
  1149. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1150. if (pin_txt == NULL)
  1151. return -1;
  1152. return os_snprintf(buf, buflen, "%s", pin_txt);
  1153. }
  1154. if (os_strcmp(cmd, "get") == 0) {
  1155. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1156. if (pin_txt == NULL)
  1157. return -1;
  1158. return os_snprintf(buf, buflen, "%s", pin_txt);
  1159. }
  1160. if (os_strcmp(cmd, "set") == 0) {
  1161. char *pin;
  1162. if (pos == NULL)
  1163. return -1;
  1164. pin = pos;
  1165. pos = os_strchr(pos, ' ');
  1166. if (pos) {
  1167. *pos++ = '\0';
  1168. timeout = atoi(pos);
  1169. }
  1170. if (os_strlen(pin) > buflen)
  1171. return -1;
  1172. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1173. return -1;
  1174. return os_snprintf(buf, buflen, "%s", pin);
  1175. }
  1176. return -1;
  1177. }
  1178. #endif /* CONFIG_AP */
  1179. #ifdef CONFIG_WPS_ER
  1180. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1181. char *cmd)
  1182. {
  1183. char *uuid = cmd, *pin, *pos;
  1184. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1185. pin = os_strchr(uuid, ' ');
  1186. if (pin == NULL)
  1187. return -1;
  1188. *pin++ = '\0';
  1189. pos = os_strchr(pin, ' ');
  1190. if (pos) {
  1191. *pos++ = '\0';
  1192. if (hwaddr_aton(pos, addr_buf) == 0)
  1193. addr = addr_buf;
  1194. }
  1195. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1196. }
  1197. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1198. char *cmd)
  1199. {
  1200. char *uuid = cmd, *pin;
  1201. pin = os_strchr(uuid, ' ');
  1202. if (pin == NULL)
  1203. return -1;
  1204. *pin++ = '\0';
  1205. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1206. }
  1207. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1208. struct wpa_supplicant *wpa_s, char *cmd)
  1209. {
  1210. char *uuid = cmd, *id;
  1211. id = os_strchr(uuid, ' ');
  1212. if (id == NULL)
  1213. return -1;
  1214. *id++ = '\0';
  1215. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1216. }
  1217. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1218. struct wpa_supplicant *wpa_s, char *cmd)
  1219. {
  1220. char *pin;
  1221. char *new_ssid;
  1222. char *new_auth;
  1223. char *new_encr;
  1224. char *new_key;
  1225. struct wps_new_ap_settings ap;
  1226. pin = os_strchr(cmd, ' ');
  1227. if (pin == NULL)
  1228. return -1;
  1229. *pin++ = '\0';
  1230. new_ssid = os_strchr(pin, ' ');
  1231. if (new_ssid == NULL)
  1232. return -1;
  1233. *new_ssid++ = '\0';
  1234. new_auth = os_strchr(new_ssid, ' ');
  1235. if (new_auth == NULL)
  1236. return -1;
  1237. *new_auth++ = '\0';
  1238. new_encr = os_strchr(new_auth, ' ');
  1239. if (new_encr == NULL)
  1240. return -1;
  1241. *new_encr++ = '\0';
  1242. new_key = os_strchr(new_encr, ' ');
  1243. if (new_key == NULL)
  1244. return -1;
  1245. *new_key++ = '\0';
  1246. os_memset(&ap, 0, sizeof(ap));
  1247. ap.ssid_hex = new_ssid;
  1248. ap.auth = new_auth;
  1249. ap.encr = new_encr;
  1250. ap.key_hex = new_key;
  1251. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1252. }
  1253. #ifdef CONFIG_WPS_NFC
  1254. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1255. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1256. {
  1257. int ndef;
  1258. struct wpabuf *buf;
  1259. int res;
  1260. char *uuid;
  1261. uuid = os_strchr(cmd, ' ');
  1262. if (uuid == NULL)
  1263. return -1;
  1264. *uuid++ = '\0';
  1265. if (os_strcmp(cmd, "WPS") == 0)
  1266. ndef = 0;
  1267. else if (os_strcmp(cmd, "NDEF") == 0)
  1268. ndef = 1;
  1269. else
  1270. return -1;
  1271. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1272. if (buf == NULL)
  1273. return -1;
  1274. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1275. wpabuf_len(buf));
  1276. reply[res++] = '\n';
  1277. reply[res] = '\0';
  1278. wpabuf_free(buf);
  1279. return res;
  1280. }
  1281. #endif /* CONFIG_WPS_NFC */
  1282. #endif /* CONFIG_WPS_ER */
  1283. #endif /* CONFIG_WPS */
  1284. #ifdef CONFIG_IBSS_RSN
  1285. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1286. struct wpa_supplicant *wpa_s, char *addr)
  1287. {
  1288. u8 peer[ETH_ALEN];
  1289. if (hwaddr_aton(addr, peer)) {
  1290. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1291. "address '%s'", addr);
  1292. return -1;
  1293. }
  1294. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1295. MAC2STR(peer));
  1296. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1297. }
  1298. #endif /* CONFIG_IBSS_RSN */
  1299. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1300. char *rsp)
  1301. {
  1302. #ifdef IEEE8021X_EAPOL
  1303. char *pos, *id_pos;
  1304. int id;
  1305. struct wpa_ssid *ssid;
  1306. pos = os_strchr(rsp, '-');
  1307. if (pos == NULL)
  1308. return -1;
  1309. *pos++ = '\0';
  1310. id_pos = pos;
  1311. pos = os_strchr(pos, ':');
  1312. if (pos == NULL)
  1313. return -1;
  1314. *pos++ = '\0';
  1315. id = atoi(id_pos);
  1316. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1317. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1318. (u8 *) pos, os_strlen(pos));
  1319. ssid = wpa_config_get_network(wpa_s->conf, id);
  1320. if (ssid == NULL) {
  1321. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1322. "to update", id);
  1323. return -1;
  1324. }
  1325. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1326. pos);
  1327. #else /* IEEE8021X_EAPOL */
  1328. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1329. return -1;
  1330. #endif /* IEEE8021X_EAPOL */
  1331. }
  1332. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1333. const char *params,
  1334. char *buf, size_t buflen)
  1335. {
  1336. char *pos, *end, tmp[30];
  1337. int res, verbose, wps, ret;
  1338. #ifdef CONFIG_HS20
  1339. const u8 *hs20;
  1340. #endif /* CONFIG_HS20 */
  1341. if (os_strcmp(params, "-DRIVER") == 0)
  1342. return wpa_drv_status(wpa_s, buf, buflen);
  1343. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1344. wps = os_strcmp(params, "-WPS") == 0;
  1345. pos = buf;
  1346. end = buf + buflen;
  1347. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1348. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1349. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1350. MAC2STR(wpa_s->bssid));
  1351. if (os_snprintf_error(end - pos, ret))
  1352. return pos - buf;
  1353. pos += ret;
  1354. ret = os_snprintf(pos, end - pos, "freq=%u\n",
  1355. wpa_s->assoc_freq);
  1356. if (os_snprintf_error(end - pos, ret))
  1357. return pos - buf;
  1358. pos += ret;
  1359. if (ssid) {
  1360. u8 *_ssid = ssid->ssid;
  1361. size_t ssid_len = ssid->ssid_len;
  1362. u8 ssid_buf[MAX_SSID_LEN];
  1363. if (ssid_len == 0) {
  1364. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1365. if (_res < 0)
  1366. ssid_len = 0;
  1367. else
  1368. ssid_len = _res;
  1369. _ssid = ssid_buf;
  1370. }
  1371. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1372. wpa_ssid_txt(_ssid, ssid_len),
  1373. ssid->id);
  1374. if (os_snprintf_error(end - pos, ret))
  1375. return pos - buf;
  1376. pos += ret;
  1377. if (wps && ssid->passphrase &&
  1378. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1379. (ssid->mode == WPAS_MODE_AP ||
  1380. ssid->mode == WPAS_MODE_P2P_GO)) {
  1381. ret = os_snprintf(pos, end - pos,
  1382. "passphrase=%s\n",
  1383. ssid->passphrase);
  1384. if (os_snprintf_error(end - pos, ret))
  1385. return pos - buf;
  1386. pos += ret;
  1387. }
  1388. if (ssid->id_str) {
  1389. ret = os_snprintf(pos, end - pos,
  1390. "id_str=%s\n",
  1391. ssid->id_str);
  1392. if (os_snprintf_error(end - pos, ret))
  1393. return pos - buf;
  1394. pos += ret;
  1395. }
  1396. switch (ssid->mode) {
  1397. case WPAS_MODE_INFRA:
  1398. ret = os_snprintf(pos, end - pos,
  1399. "mode=station\n");
  1400. break;
  1401. case WPAS_MODE_IBSS:
  1402. ret = os_snprintf(pos, end - pos,
  1403. "mode=IBSS\n");
  1404. break;
  1405. case WPAS_MODE_AP:
  1406. ret = os_snprintf(pos, end - pos,
  1407. "mode=AP\n");
  1408. break;
  1409. case WPAS_MODE_P2P_GO:
  1410. ret = os_snprintf(pos, end - pos,
  1411. "mode=P2P GO\n");
  1412. break;
  1413. case WPAS_MODE_P2P_GROUP_FORMATION:
  1414. ret = os_snprintf(pos, end - pos,
  1415. "mode=P2P GO - group "
  1416. "formation\n");
  1417. break;
  1418. default:
  1419. ret = 0;
  1420. break;
  1421. }
  1422. if (os_snprintf_error(end - pos, ret))
  1423. return pos - buf;
  1424. pos += ret;
  1425. }
  1426. #ifdef CONFIG_AP
  1427. if (wpa_s->ap_iface) {
  1428. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1429. end - pos,
  1430. verbose);
  1431. } else
  1432. #endif /* CONFIG_AP */
  1433. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1434. }
  1435. #ifdef CONFIG_SAE
  1436. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1437. #ifdef CONFIG_AP
  1438. !wpa_s->ap_iface &&
  1439. #endif /* CONFIG_AP */
  1440. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1441. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1442. wpa_s->sme.sae.group);
  1443. if (os_snprintf_error(end - pos, ret))
  1444. return pos - buf;
  1445. pos += ret;
  1446. }
  1447. #endif /* CONFIG_SAE */
  1448. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1449. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1450. if (os_snprintf_error(end - pos, ret))
  1451. return pos - buf;
  1452. pos += ret;
  1453. if (wpa_s->l2 &&
  1454. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1455. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1456. if (os_snprintf_error(end - pos, ret))
  1457. return pos - buf;
  1458. pos += ret;
  1459. }
  1460. #ifdef CONFIG_P2P
  1461. if (wpa_s->global->p2p) {
  1462. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1463. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1464. if (os_snprintf_error(end - pos, ret))
  1465. return pos - buf;
  1466. pos += ret;
  1467. }
  1468. #endif /* CONFIG_P2P */
  1469. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1470. MAC2STR(wpa_s->own_addr));
  1471. if (os_snprintf_error(end - pos, ret))
  1472. return pos - buf;
  1473. pos += ret;
  1474. #ifdef CONFIG_HS20
  1475. if (wpa_s->current_bss &&
  1476. (hs20 = wpa_bss_get_vendor_ie(wpa_s->current_bss,
  1477. HS20_IE_VENDOR_TYPE)) &&
  1478. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1479. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1480. int release = 1;
  1481. if (hs20[1] >= 5) {
  1482. u8 rel_num = (hs20[6] & 0xf0) >> 4;
  1483. release = rel_num + 1;
  1484. }
  1485. ret = os_snprintf(pos, end - pos, "hs20=%d\n", release);
  1486. if (os_snprintf_error(end - pos, ret))
  1487. return pos - buf;
  1488. pos += ret;
  1489. }
  1490. if (wpa_s->current_ssid) {
  1491. struct wpa_cred *cred;
  1492. char *type;
  1493. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1494. size_t i;
  1495. if (wpa_s->current_ssid->parent_cred != cred)
  1496. continue;
  1497. if (cred->provisioning_sp) {
  1498. ret = os_snprintf(pos, end - pos,
  1499. "provisioning_sp=%s\n",
  1500. cred->provisioning_sp);
  1501. if (os_snprintf_error(end - pos, ret))
  1502. return pos - buf;
  1503. pos += ret;
  1504. }
  1505. if (!cred->domain)
  1506. goto no_domain;
  1507. i = 0;
  1508. if (wpa_s->current_bss && wpa_s->current_bss->anqp) {
  1509. struct wpabuf *names =
  1510. wpa_s->current_bss->anqp->domain_name;
  1511. for (i = 0; names && i < cred->num_domain; i++)
  1512. {
  1513. if (domain_name_list_contains(
  1514. names, cred->domain[i], 1))
  1515. break;
  1516. }
  1517. if (i == cred->num_domain)
  1518. i = 0; /* show first entry by default */
  1519. }
  1520. ret = os_snprintf(pos, end - pos, "home_sp=%s\n",
  1521. cred->domain[i]);
  1522. if (os_snprintf_error(end - pos, ret))
  1523. return pos - buf;
  1524. pos += ret;
  1525. no_domain:
  1526. if (wpa_s->current_bss == NULL ||
  1527. wpa_s->current_bss->anqp == NULL)
  1528. res = -1;
  1529. else
  1530. res = interworking_home_sp_cred(
  1531. wpa_s, cred,
  1532. wpa_s->current_bss->anqp->domain_name);
  1533. if (res > 0)
  1534. type = "home";
  1535. else if (res == 0)
  1536. type = "roaming";
  1537. else
  1538. type = "unknown";
  1539. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1540. if (os_snprintf_error(end - pos, ret))
  1541. return pos - buf;
  1542. pos += ret;
  1543. break;
  1544. }
  1545. }
  1546. #endif /* CONFIG_HS20 */
  1547. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1548. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1549. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1550. verbose);
  1551. if (res >= 0)
  1552. pos += res;
  1553. }
  1554. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1555. if (res >= 0)
  1556. pos += res;
  1557. #ifdef CONFIG_WPS
  1558. {
  1559. char uuid_str[100];
  1560. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1561. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1562. if (os_snprintf_error(end - pos, ret))
  1563. return pos - buf;
  1564. pos += ret;
  1565. }
  1566. #endif /* CONFIG_WPS */
  1567. #ifdef ANDROID
  1568. /*
  1569. * Allow using the STATUS command with default behavior, say for debug,
  1570. * i.e., don't generate a "fake" CONNECTION and SUPPLICANT_STATE_CHANGE
  1571. * events with STATUS-NO_EVENTS.
  1572. */
  1573. if (os_strcmp(params, "-NO_EVENTS")) {
  1574. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1575. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1576. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1577. wpa_s->wpa_state,
  1578. MAC2STR(wpa_s->bssid),
  1579. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1580. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1581. wpa_s->current_ssid->ssid_len) : "");
  1582. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1583. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1584. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1585. "- connection to " MACSTR
  1586. " completed %s [id=%d id_str=%s]",
  1587. MAC2STR(wpa_s->bssid), "(auth)",
  1588. ssid ? ssid->id : -1,
  1589. ssid && ssid->id_str ? ssid->id_str : "");
  1590. }
  1591. }
  1592. #endif /* ANDROID */
  1593. return pos - buf;
  1594. }
  1595. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1596. char *cmd)
  1597. {
  1598. char *pos;
  1599. int id;
  1600. struct wpa_ssid *ssid;
  1601. u8 bssid[ETH_ALEN];
  1602. /* cmd: "<network id> <BSSID>" */
  1603. pos = os_strchr(cmd, ' ');
  1604. if (pos == NULL)
  1605. return -1;
  1606. *pos++ = '\0';
  1607. id = atoi(cmd);
  1608. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1609. if (hwaddr_aton(pos, bssid)) {
  1610. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1611. return -1;
  1612. }
  1613. ssid = wpa_config_get_network(wpa_s->conf, id);
  1614. if (ssid == NULL) {
  1615. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1616. "to update", id);
  1617. return -1;
  1618. }
  1619. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1620. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1621. return 0;
  1622. }
  1623. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1624. char *cmd, char *buf,
  1625. size_t buflen)
  1626. {
  1627. u8 bssid[ETH_ALEN];
  1628. struct wpa_blacklist *e;
  1629. char *pos, *end;
  1630. int ret;
  1631. /* cmd: "BLACKLIST [<BSSID>]" */
  1632. if (*cmd == '\0') {
  1633. pos = buf;
  1634. end = buf + buflen;
  1635. e = wpa_s->blacklist;
  1636. while (e) {
  1637. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1638. MAC2STR(e->bssid));
  1639. if (os_snprintf_error(end - pos, ret))
  1640. return pos - buf;
  1641. pos += ret;
  1642. e = e->next;
  1643. }
  1644. return pos - buf;
  1645. }
  1646. cmd++;
  1647. if (os_strncmp(cmd, "clear", 5) == 0) {
  1648. wpa_blacklist_clear(wpa_s);
  1649. os_memcpy(buf, "OK\n", 3);
  1650. return 3;
  1651. }
  1652. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1653. if (hwaddr_aton(cmd, bssid)) {
  1654. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1655. return -1;
  1656. }
  1657. /*
  1658. * Add the BSSID twice, so its count will be 2, causing it to be
  1659. * skipped when processing scan results.
  1660. */
  1661. ret = wpa_blacklist_add(wpa_s, bssid);
  1662. if (ret < 0)
  1663. return -1;
  1664. ret = wpa_blacklist_add(wpa_s, bssid);
  1665. if (ret < 0)
  1666. return -1;
  1667. os_memcpy(buf, "OK\n", 3);
  1668. return 3;
  1669. }
  1670. static const char * debug_level_str(int level)
  1671. {
  1672. switch (level) {
  1673. case MSG_EXCESSIVE:
  1674. return "EXCESSIVE";
  1675. case MSG_MSGDUMP:
  1676. return "MSGDUMP";
  1677. case MSG_DEBUG:
  1678. return "DEBUG";
  1679. case MSG_INFO:
  1680. return "INFO";
  1681. case MSG_WARNING:
  1682. return "WARNING";
  1683. case MSG_ERROR:
  1684. return "ERROR";
  1685. default:
  1686. return "?";
  1687. }
  1688. }
  1689. static int str_to_debug_level(const char *s)
  1690. {
  1691. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1692. return MSG_EXCESSIVE;
  1693. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1694. return MSG_MSGDUMP;
  1695. if (os_strcasecmp(s, "DEBUG") == 0)
  1696. return MSG_DEBUG;
  1697. if (os_strcasecmp(s, "INFO") == 0)
  1698. return MSG_INFO;
  1699. if (os_strcasecmp(s, "WARNING") == 0)
  1700. return MSG_WARNING;
  1701. if (os_strcasecmp(s, "ERROR") == 0)
  1702. return MSG_ERROR;
  1703. return -1;
  1704. }
  1705. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1706. char *cmd, char *buf,
  1707. size_t buflen)
  1708. {
  1709. char *pos, *end, *stamp;
  1710. int ret;
  1711. /* cmd: "LOG_LEVEL [<level>]" */
  1712. if (*cmd == '\0') {
  1713. pos = buf;
  1714. end = buf + buflen;
  1715. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1716. "Timestamp: %d\n",
  1717. debug_level_str(wpa_debug_level),
  1718. wpa_debug_timestamp);
  1719. if (os_snprintf_error(end - pos, ret))
  1720. ret = 0;
  1721. return ret;
  1722. }
  1723. while (*cmd == ' ')
  1724. cmd++;
  1725. stamp = os_strchr(cmd, ' ');
  1726. if (stamp) {
  1727. *stamp++ = '\0';
  1728. while (*stamp == ' ') {
  1729. stamp++;
  1730. }
  1731. }
  1732. if (cmd && os_strlen(cmd)) {
  1733. int level = str_to_debug_level(cmd);
  1734. if (level < 0)
  1735. return -1;
  1736. wpa_debug_level = level;
  1737. }
  1738. if (stamp && os_strlen(stamp))
  1739. wpa_debug_timestamp = atoi(stamp);
  1740. os_memcpy(buf, "OK\n", 3);
  1741. return 3;
  1742. }
  1743. static int wpa_supplicant_ctrl_iface_list_networks(
  1744. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  1745. {
  1746. char *pos, *end, *prev;
  1747. struct wpa_ssid *ssid;
  1748. int ret;
  1749. pos = buf;
  1750. end = buf + buflen;
  1751. ret = os_snprintf(pos, end - pos,
  1752. "network id / ssid / bssid / flags\n");
  1753. if (os_snprintf_error(end - pos, ret))
  1754. return pos - buf;
  1755. pos += ret;
  1756. ssid = wpa_s->conf->ssid;
  1757. /* skip over ssids until we find next one */
  1758. if (cmd != NULL && os_strncmp(cmd, "LAST_ID=", 8) == 0) {
  1759. int last_id = atoi(cmd + 8);
  1760. if (last_id != -1) {
  1761. while (ssid != NULL && ssid->id <= last_id) {
  1762. ssid = ssid->next;
  1763. }
  1764. }
  1765. }
  1766. while (ssid) {
  1767. prev = pos;
  1768. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1769. ssid->id,
  1770. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1771. if (os_snprintf_error(end - pos, ret))
  1772. return prev - buf;
  1773. pos += ret;
  1774. if (ssid->bssid_set) {
  1775. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1776. MAC2STR(ssid->bssid));
  1777. } else {
  1778. ret = os_snprintf(pos, end - pos, "\tany");
  1779. }
  1780. if (os_snprintf_error(end - pos, ret))
  1781. return prev - buf;
  1782. pos += ret;
  1783. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1784. ssid == wpa_s->current_ssid ?
  1785. "[CURRENT]" : "",
  1786. ssid->disabled ? "[DISABLED]" : "",
  1787. ssid->disabled_until.sec ?
  1788. "[TEMP-DISABLED]" : "",
  1789. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1790. "");
  1791. if (os_snprintf_error(end - pos, ret))
  1792. return prev - buf;
  1793. pos += ret;
  1794. ret = os_snprintf(pos, end - pos, "\n");
  1795. if (os_snprintf_error(end - pos, ret))
  1796. return prev - buf;
  1797. pos += ret;
  1798. ssid = ssid->next;
  1799. }
  1800. return pos - buf;
  1801. }
  1802. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1803. {
  1804. int ret;
  1805. ret = os_snprintf(pos, end - pos, "-");
  1806. if (os_snprintf_error(end - pos, ret))
  1807. return pos;
  1808. pos += ret;
  1809. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1810. if (ret < 0)
  1811. return pos;
  1812. pos += ret;
  1813. return pos;
  1814. }
  1815. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1816. const u8 *ie, size_t ie_len)
  1817. {
  1818. struct wpa_ie_data data;
  1819. char *start;
  1820. int ret;
  1821. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1822. if (os_snprintf_error(end - pos, ret))
  1823. return pos;
  1824. pos += ret;
  1825. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1826. ret = os_snprintf(pos, end - pos, "?]");
  1827. if (os_snprintf_error(end - pos, ret))
  1828. return pos;
  1829. pos += ret;
  1830. return pos;
  1831. }
  1832. start = pos;
  1833. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1834. ret = os_snprintf(pos, end - pos, "%sEAP",
  1835. pos == start ? "" : "+");
  1836. if (os_snprintf_error(end - pos, ret))
  1837. return pos;
  1838. pos += ret;
  1839. }
  1840. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1841. ret = os_snprintf(pos, end - pos, "%sPSK",
  1842. pos == start ? "" : "+");
  1843. if (os_snprintf_error(end - pos, ret))
  1844. return pos;
  1845. pos += ret;
  1846. }
  1847. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1848. ret = os_snprintf(pos, end - pos, "%sNone",
  1849. pos == start ? "" : "+");
  1850. if (os_snprintf_error(end - pos, ret))
  1851. return pos;
  1852. pos += ret;
  1853. }
  1854. if (data.key_mgmt & WPA_KEY_MGMT_SAE) {
  1855. ret = os_snprintf(pos, end - pos, "%sSAE",
  1856. pos == start ? "" : "+");
  1857. if (os_snprintf_error(end - pos, ret))
  1858. return pos;
  1859. pos += ret;
  1860. }
  1861. #ifdef CONFIG_IEEE80211R
  1862. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1863. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1864. pos == start ? "" : "+");
  1865. if (os_snprintf_error(end - pos, ret))
  1866. return pos;
  1867. pos += ret;
  1868. }
  1869. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1870. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1871. pos == start ? "" : "+");
  1872. if (os_snprintf_error(end - pos, ret))
  1873. return pos;
  1874. pos += ret;
  1875. }
  1876. if (data.key_mgmt & WPA_KEY_MGMT_FT_SAE) {
  1877. ret = os_snprintf(pos, end - pos, "%sFT/SAE",
  1878. pos == start ? "" : "+");
  1879. if (os_snprintf_error(end - pos, ret))
  1880. return pos;
  1881. pos += ret;
  1882. }
  1883. #endif /* CONFIG_IEEE80211R */
  1884. #ifdef CONFIG_IEEE80211W
  1885. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1886. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1887. pos == start ? "" : "+");
  1888. if (os_snprintf_error(end - pos, ret))
  1889. return pos;
  1890. pos += ret;
  1891. }
  1892. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1893. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1894. pos == start ? "" : "+");
  1895. if (os_snprintf_error(end - pos, ret))
  1896. return pos;
  1897. pos += ret;
  1898. }
  1899. #endif /* CONFIG_IEEE80211W */
  1900. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SUITE_B) {
  1901. ret = os_snprintf(pos, end - pos, "%sEAP-SUITE-B",
  1902. pos == start ? "" : "+");
  1903. if (os_snprintf_error(end - pos, ret))
  1904. return pos;
  1905. pos += ret;
  1906. }
  1907. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1908. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1909. ret = os_snprintf(pos, end - pos, "-preauth");
  1910. if (os_snprintf_error(end - pos, ret))
  1911. return pos;
  1912. pos += ret;
  1913. }
  1914. ret = os_snprintf(pos, end - pos, "]");
  1915. if (os_snprintf_error(end - pos, ret))
  1916. return pos;
  1917. pos += ret;
  1918. return pos;
  1919. }
  1920. #ifdef CONFIG_WPS
  1921. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1922. char *pos, char *end,
  1923. struct wpabuf *wps_ie)
  1924. {
  1925. int ret;
  1926. const char *txt;
  1927. if (wps_ie == NULL)
  1928. return pos;
  1929. if (wps_is_selected_pbc_registrar(wps_ie))
  1930. txt = "[WPS-PBC]";
  1931. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1932. txt = "[WPS-AUTH]";
  1933. else if (wps_is_selected_pin_registrar(wps_ie))
  1934. txt = "[WPS-PIN]";
  1935. else
  1936. txt = "[WPS]";
  1937. ret = os_snprintf(pos, end - pos, "%s", txt);
  1938. if (!os_snprintf_error(end - pos, ret))
  1939. pos += ret;
  1940. wpabuf_free(wps_ie);
  1941. return pos;
  1942. }
  1943. #endif /* CONFIG_WPS */
  1944. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1945. char *pos, char *end,
  1946. const struct wpa_bss *bss)
  1947. {
  1948. #ifdef CONFIG_WPS
  1949. struct wpabuf *wps_ie;
  1950. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1951. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1952. #else /* CONFIG_WPS */
  1953. return pos;
  1954. #endif /* CONFIG_WPS */
  1955. }
  1956. /* Format one result on one text line into a buffer. */
  1957. static int wpa_supplicant_ctrl_iface_scan_result(
  1958. struct wpa_supplicant *wpa_s,
  1959. const struct wpa_bss *bss, char *buf, size_t buflen)
  1960. {
  1961. char *pos, *end;
  1962. int ret;
  1963. const u8 *ie, *ie2, *p2p, *mesh;
  1964. mesh = wpa_bss_get_ie(bss, WLAN_EID_MESH_ID);
  1965. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1966. if (!p2p)
  1967. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1968. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1969. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1970. 0)
  1971. return 0; /* Do not show P2P listen discovery results here */
  1972. pos = buf;
  1973. end = buf + buflen;
  1974. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1975. MAC2STR(bss->bssid), bss->freq, bss->level);
  1976. if (os_snprintf_error(end - pos, ret))
  1977. return -1;
  1978. pos += ret;
  1979. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1980. if (ie)
  1981. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1982. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1983. if (ie2) {
  1984. pos = wpa_supplicant_ie_txt(pos, end, mesh ? "RSN" : "WPA2",
  1985. ie2, 2 + ie2[1]);
  1986. }
  1987. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1988. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1989. ret = os_snprintf(pos, end - pos, "[WEP]");
  1990. if (os_snprintf_error(end - pos, ret))
  1991. return -1;
  1992. pos += ret;
  1993. }
  1994. if (mesh) {
  1995. ret = os_snprintf(pos, end - pos, "[MESH]");
  1996. if (os_snprintf_error(end - pos, ret))
  1997. return -1;
  1998. pos += ret;
  1999. }
  2000. if (bss_is_dmg(bss)) {
  2001. const char *s;
  2002. ret = os_snprintf(pos, end - pos, "[DMG]");
  2003. if (os_snprintf_error(end - pos, ret))
  2004. return -1;
  2005. pos += ret;
  2006. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  2007. case IEEE80211_CAP_DMG_IBSS:
  2008. s = "[IBSS]";
  2009. break;
  2010. case IEEE80211_CAP_DMG_AP:
  2011. s = "[ESS]";
  2012. break;
  2013. case IEEE80211_CAP_DMG_PBSS:
  2014. s = "[PBSS]";
  2015. break;
  2016. default:
  2017. s = "";
  2018. break;
  2019. }
  2020. ret = os_snprintf(pos, end - pos, "%s", s);
  2021. if (os_snprintf_error(end - pos, ret))
  2022. return -1;
  2023. pos += ret;
  2024. } else {
  2025. if (bss->caps & IEEE80211_CAP_IBSS) {
  2026. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2027. if (os_snprintf_error(end - pos, ret))
  2028. return -1;
  2029. pos += ret;
  2030. }
  2031. if (bss->caps & IEEE80211_CAP_ESS) {
  2032. ret = os_snprintf(pos, end - pos, "[ESS]");
  2033. if (os_snprintf_error(end - pos, ret))
  2034. return -1;
  2035. pos += ret;
  2036. }
  2037. }
  2038. if (p2p) {
  2039. ret = os_snprintf(pos, end - pos, "[P2P]");
  2040. if (os_snprintf_error(end - pos, ret))
  2041. return -1;
  2042. pos += ret;
  2043. }
  2044. #ifdef CONFIG_HS20
  2045. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  2046. ret = os_snprintf(pos, end - pos, "[HS20]");
  2047. if (os_snprintf_error(end - pos, ret))
  2048. return -1;
  2049. pos += ret;
  2050. }
  2051. #endif /* CONFIG_HS20 */
  2052. ret = os_snprintf(pos, end - pos, "\t%s",
  2053. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2054. if (os_snprintf_error(end - pos, ret))
  2055. return -1;
  2056. pos += ret;
  2057. ret = os_snprintf(pos, end - pos, "\n");
  2058. if (os_snprintf_error(end - pos, ret))
  2059. return -1;
  2060. pos += ret;
  2061. return pos - buf;
  2062. }
  2063. static int wpa_supplicant_ctrl_iface_scan_results(
  2064. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2065. {
  2066. char *pos, *end;
  2067. struct wpa_bss *bss;
  2068. int ret;
  2069. pos = buf;
  2070. end = buf + buflen;
  2071. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  2072. "flags / ssid\n");
  2073. if (os_snprintf_error(end - pos, ret))
  2074. return pos - buf;
  2075. pos += ret;
  2076. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  2077. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  2078. end - pos);
  2079. if (ret < 0 || ret >= end - pos)
  2080. return pos - buf;
  2081. pos += ret;
  2082. }
  2083. return pos - buf;
  2084. }
  2085. #ifdef CONFIG_MESH
  2086. static int wpa_supplicant_ctrl_iface_mesh_group_add(
  2087. struct wpa_supplicant *wpa_s, char *cmd)
  2088. {
  2089. int id;
  2090. struct wpa_ssid *ssid;
  2091. id = atoi(cmd);
  2092. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_ADD id=%d", id);
  2093. ssid = wpa_config_get_network(wpa_s->conf, id);
  2094. if (ssid == NULL) {
  2095. wpa_printf(MSG_DEBUG,
  2096. "CTRL_IFACE: Could not find network id=%d", id);
  2097. return -1;
  2098. }
  2099. if (ssid->mode != WPAS_MODE_MESH) {
  2100. wpa_printf(MSG_DEBUG,
  2101. "CTRL_IFACE: Cannot use MESH_GROUP_ADD on a non mesh network");
  2102. return -1;
  2103. }
  2104. if (ssid->key_mgmt != WPA_KEY_MGMT_NONE &&
  2105. ssid->key_mgmt != WPA_KEY_MGMT_SAE) {
  2106. wpa_printf(MSG_ERROR,
  2107. "CTRL_IFACE: key_mgmt for mesh network should be open or SAE");
  2108. return -1;
  2109. }
  2110. /*
  2111. * TODO: If necessary write our own group_add function,
  2112. * for now we can reuse select_network
  2113. */
  2114. wpa_supplicant_select_network(wpa_s, ssid);
  2115. return 0;
  2116. }
  2117. static int wpa_supplicant_ctrl_iface_mesh_group_remove(
  2118. struct wpa_supplicant *wpa_s, char *cmd)
  2119. {
  2120. /*
  2121. * TODO: Support a multiple mesh and other iface type combinations
  2122. */
  2123. if (os_strcmp(cmd, wpa_s->ifname) != 0) {
  2124. wpa_printf(MSG_DEBUG,
  2125. "CTRL_IFACE: MESH_GROUP_REMOVE unknown interface name: %s",
  2126. cmd);
  2127. return -1;
  2128. }
  2129. wpa_printf(MSG_DEBUG, "CTRL_IFACE: MESH_GROUP_REMOVE ifname=%s", cmd);
  2130. wpa_s->reassociate = 0;
  2131. wpa_s->disconnected = 1;
  2132. wpa_supplicant_cancel_sched_scan(wpa_s);
  2133. wpa_supplicant_cancel_scan(wpa_s);
  2134. /*
  2135. * TODO: If necessary write our own group_remove function,
  2136. * for now we can reuse deauthenticate
  2137. */
  2138. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2139. return 0;
  2140. }
  2141. #endif /* CONFIG_MESH */
  2142. static int wpa_supplicant_ctrl_iface_select_network(
  2143. struct wpa_supplicant *wpa_s, char *cmd)
  2144. {
  2145. int id;
  2146. struct wpa_ssid *ssid;
  2147. char *pos;
  2148. /* cmd: "<network id>" or "any" */
  2149. if (os_strncmp(cmd, "any", 3) == 0) {
  2150. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  2151. ssid = NULL;
  2152. } else {
  2153. id = atoi(cmd);
  2154. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  2155. ssid = wpa_config_get_network(wpa_s->conf, id);
  2156. if (ssid == NULL) {
  2157. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2158. "network id=%d", id);
  2159. return -1;
  2160. }
  2161. if (ssid->disabled == 2) {
  2162. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2163. "SELECT_NETWORK with persistent P2P group");
  2164. return -1;
  2165. }
  2166. }
  2167. pos = os_strstr(cmd, " freq=");
  2168. if (pos) {
  2169. int *freqs = freq_range_to_channel_list(wpa_s, pos + 6);
  2170. if (freqs) {
  2171. wpa_s->scan_req = MANUAL_SCAN_REQ;
  2172. os_free(wpa_s->manual_scan_freqs);
  2173. wpa_s->manual_scan_freqs = freqs;
  2174. }
  2175. }
  2176. wpa_supplicant_select_network(wpa_s, ssid);
  2177. return 0;
  2178. }
  2179. static int wpa_supplicant_ctrl_iface_enable_network(
  2180. struct wpa_supplicant *wpa_s, char *cmd)
  2181. {
  2182. int id;
  2183. struct wpa_ssid *ssid;
  2184. /* cmd: "<network id>" or "all" */
  2185. if (os_strcmp(cmd, "all") == 0) {
  2186. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  2187. ssid = NULL;
  2188. } else {
  2189. id = atoi(cmd);
  2190. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  2191. ssid = wpa_config_get_network(wpa_s->conf, id);
  2192. if (ssid == NULL) {
  2193. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2194. "network id=%d", id);
  2195. return -1;
  2196. }
  2197. if (ssid->disabled == 2) {
  2198. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2199. "ENABLE_NETWORK with persistent P2P group");
  2200. return -1;
  2201. }
  2202. if (os_strstr(cmd, " no-connect")) {
  2203. ssid->disabled = 0;
  2204. return 0;
  2205. }
  2206. }
  2207. wpa_supplicant_enable_network(wpa_s, ssid);
  2208. return 0;
  2209. }
  2210. static int wpa_supplicant_ctrl_iface_disable_network(
  2211. struct wpa_supplicant *wpa_s, char *cmd)
  2212. {
  2213. int id;
  2214. struct wpa_ssid *ssid;
  2215. /* cmd: "<network id>" or "all" */
  2216. if (os_strcmp(cmd, "all") == 0) {
  2217. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  2218. ssid = NULL;
  2219. } else {
  2220. id = atoi(cmd);
  2221. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  2222. ssid = wpa_config_get_network(wpa_s->conf, id);
  2223. if (ssid == NULL) {
  2224. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2225. "network id=%d", id);
  2226. return -1;
  2227. }
  2228. if (ssid->disabled == 2) {
  2229. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  2230. "DISABLE_NETWORK with persistent P2P "
  2231. "group");
  2232. return -1;
  2233. }
  2234. }
  2235. wpa_supplicant_disable_network(wpa_s, ssid);
  2236. return 0;
  2237. }
  2238. static int wpa_supplicant_ctrl_iface_add_network(
  2239. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  2240. {
  2241. struct wpa_ssid *ssid;
  2242. int ret;
  2243. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  2244. ssid = wpa_config_add_network(wpa_s->conf);
  2245. if (ssid == NULL)
  2246. return -1;
  2247. wpas_notify_network_added(wpa_s, ssid);
  2248. ssid->disabled = 1;
  2249. wpa_config_set_network_defaults(ssid);
  2250. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  2251. if (os_snprintf_error(buflen, ret))
  2252. return -1;
  2253. return ret;
  2254. }
  2255. static int wpa_supplicant_ctrl_iface_remove_network(
  2256. struct wpa_supplicant *wpa_s, char *cmd)
  2257. {
  2258. int id;
  2259. struct wpa_ssid *ssid;
  2260. int was_disabled;
  2261. /* cmd: "<network id>" or "all" */
  2262. if (os_strcmp(cmd, "all") == 0) {
  2263. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  2264. if (wpa_s->sched_scanning)
  2265. wpa_supplicant_cancel_sched_scan(wpa_s);
  2266. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2267. if (wpa_s->current_ssid) {
  2268. #ifdef CONFIG_SME
  2269. wpa_s->sme.prev_bssid_set = 0;
  2270. #endif /* CONFIG_SME */
  2271. wpa_sm_set_config(wpa_s->wpa, NULL);
  2272. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2273. wpa_supplicant_deauthenticate(
  2274. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  2275. }
  2276. ssid = wpa_s->conf->ssid;
  2277. while (ssid) {
  2278. struct wpa_ssid *remove_ssid = ssid;
  2279. id = ssid->id;
  2280. ssid = ssid->next;
  2281. if (wpa_s->last_ssid == remove_ssid)
  2282. wpa_s->last_ssid = NULL;
  2283. wpas_notify_network_removed(wpa_s, remove_ssid);
  2284. wpa_config_remove_network(wpa_s->conf, id);
  2285. }
  2286. return 0;
  2287. }
  2288. id = atoi(cmd);
  2289. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  2290. ssid = wpa_config_get_network(wpa_s->conf, id);
  2291. if (ssid)
  2292. wpas_notify_network_removed(wpa_s, ssid);
  2293. if (ssid == NULL) {
  2294. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2295. "id=%d", id);
  2296. return -1;
  2297. }
  2298. if (wpa_s->last_ssid == ssid)
  2299. wpa_s->last_ssid = NULL;
  2300. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  2301. #ifdef CONFIG_SME
  2302. wpa_s->sme.prev_bssid_set = 0;
  2303. #endif /* CONFIG_SME */
  2304. /*
  2305. * Invalidate the EAP session cache if the current or
  2306. * previously used network is removed.
  2307. */
  2308. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2309. }
  2310. if (ssid == wpa_s->current_ssid) {
  2311. wpa_sm_set_config(wpa_s->wpa, NULL);
  2312. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2313. wpa_supplicant_deauthenticate(wpa_s,
  2314. WLAN_REASON_DEAUTH_LEAVING);
  2315. }
  2316. was_disabled = ssid->disabled;
  2317. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  2318. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2319. "network id=%d", id);
  2320. return -1;
  2321. }
  2322. if (!was_disabled && wpa_s->sched_scanning) {
  2323. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  2324. "network from filters");
  2325. wpa_supplicant_cancel_sched_scan(wpa_s);
  2326. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2327. }
  2328. return 0;
  2329. }
  2330. static int wpa_supplicant_ctrl_iface_update_network(
  2331. struct wpa_supplicant *wpa_s, struct wpa_ssid *ssid,
  2332. char *name, char *value)
  2333. {
  2334. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2335. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2336. "variable '%s'", name);
  2337. return -1;
  2338. }
  2339. if (os_strcmp(name, "bssid") != 0 &&
  2340. os_strcmp(name, "priority") != 0)
  2341. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2342. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2343. /*
  2344. * Invalidate the EAP session cache if anything in the current
  2345. * or previously used configuration changes.
  2346. */
  2347. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2348. }
  2349. if ((os_strcmp(name, "psk") == 0 &&
  2350. value[0] == '"' && ssid->ssid_len) ||
  2351. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2352. wpa_config_update_psk(ssid);
  2353. else if (os_strcmp(name, "priority") == 0)
  2354. wpa_config_update_prio_list(wpa_s->conf);
  2355. else if (os_strcmp(name, "no_auto_peer") == 0)
  2356. ssid->no_auto_peer = atoi(value);
  2357. return 0;
  2358. }
  2359. static int wpa_supplicant_ctrl_iface_set_network(
  2360. struct wpa_supplicant *wpa_s, char *cmd)
  2361. {
  2362. int id, ret, prev_bssid_set;
  2363. struct wpa_ssid *ssid;
  2364. char *name, *value;
  2365. u8 prev_bssid[ETH_ALEN];
  2366. /* cmd: "<network id> <variable name> <value>" */
  2367. name = os_strchr(cmd, ' ');
  2368. if (name == NULL)
  2369. return -1;
  2370. *name++ = '\0';
  2371. value = os_strchr(name, ' ');
  2372. if (value == NULL)
  2373. return -1;
  2374. *value++ = '\0';
  2375. id = atoi(cmd);
  2376. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2377. id, name);
  2378. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2379. (u8 *) value, os_strlen(value));
  2380. ssid = wpa_config_get_network(wpa_s->conf, id);
  2381. if (ssid == NULL) {
  2382. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2383. "id=%d", id);
  2384. return -1;
  2385. }
  2386. prev_bssid_set = ssid->bssid_set;
  2387. os_memcpy(prev_bssid, ssid->bssid, ETH_ALEN);
  2388. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid, name,
  2389. value);
  2390. if (ret == 0 &&
  2391. (ssid->bssid_set != prev_bssid_set ||
  2392. os_memcmp(ssid->bssid, prev_bssid, ETH_ALEN) != 0))
  2393. wpas_notify_network_bssid_set_changed(wpa_s, ssid);
  2394. return ret;
  2395. }
  2396. static int wpa_supplicant_ctrl_iface_get_network(
  2397. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2398. {
  2399. int id;
  2400. size_t res;
  2401. struct wpa_ssid *ssid;
  2402. char *name, *value;
  2403. /* cmd: "<network id> <variable name>" */
  2404. name = os_strchr(cmd, ' ');
  2405. if (name == NULL || buflen == 0)
  2406. return -1;
  2407. *name++ = '\0';
  2408. id = atoi(cmd);
  2409. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2410. id, name);
  2411. ssid = wpa_config_get_network(wpa_s->conf, id);
  2412. if (ssid == NULL) {
  2413. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2414. "id=%d", id);
  2415. return -1;
  2416. }
  2417. value = wpa_config_get_no_key(ssid, name);
  2418. if (value == NULL) {
  2419. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2420. "variable '%s'", name);
  2421. return -1;
  2422. }
  2423. res = os_strlcpy(buf, value, buflen);
  2424. if (res >= buflen) {
  2425. os_free(value);
  2426. return -1;
  2427. }
  2428. os_free(value);
  2429. return res;
  2430. }
  2431. static int wpa_supplicant_ctrl_iface_dup_network(
  2432. struct wpa_supplicant *wpa_s, char *cmd)
  2433. {
  2434. struct wpa_ssid *ssid_s, *ssid_d;
  2435. char *name, *id, *value;
  2436. int id_s, id_d, ret;
  2437. /* cmd: "<src network id> <dst network id> <variable name>" */
  2438. id = os_strchr(cmd, ' ');
  2439. if (id == NULL)
  2440. return -1;
  2441. *id++ = '\0';
  2442. name = os_strchr(id, ' ');
  2443. if (name == NULL)
  2444. return -1;
  2445. *name++ = '\0';
  2446. id_s = atoi(cmd);
  2447. id_d = atoi(id);
  2448. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DUP_NETWORK id=%d -> %d name='%s'",
  2449. id_s, id_d, name);
  2450. ssid_s = wpa_config_get_network(wpa_s->conf, id_s);
  2451. if (ssid_s == NULL) {
  2452. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2453. "network id=%d", id_s);
  2454. return -1;
  2455. }
  2456. ssid_d = wpa_config_get_network(wpa_s->conf, id_d);
  2457. if (ssid_d == NULL) {
  2458. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  2459. "network id=%d", id_d);
  2460. return -1;
  2461. }
  2462. value = wpa_config_get(ssid_s, name);
  2463. if (value == NULL) {
  2464. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2465. "variable '%s'", name);
  2466. return -1;
  2467. }
  2468. ret = wpa_supplicant_ctrl_iface_update_network(wpa_s, ssid_d, name,
  2469. value);
  2470. os_free(value);
  2471. return ret;
  2472. }
  2473. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2474. char *buf, size_t buflen)
  2475. {
  2476. char *pos, *end;
  2477. struct wpa_cred *cred;
  2478. int ret;
  2479. pos = buf;
  2480. end = buf + buflen;
  2481. ret = os_snprintf(pos, end - pos,
  2482. "cred id / realm / username / domain / imsi\n");
  2483. if (os_snprintf_error(end - pos, ret))
  2484. return pos - buf;
  2485. pos += ret;
  2486. cred = wpa_s->conf->cred;
  2487. while (cred) {
  2488. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2489. cred->id, cred->realm ? cred->realm : "",
  2490. cred->username ? cred->username : "",
  2491. cred->domain ? cred->domain[0] : "",
  2492. cred->imsi ? cred->imsi : "");
  2493. if (os_snprintf_error(end - pos, ret))
  2494. return pos - buf;
  2495. pos += ret;
  2496. cred = cred->next;
  2497. }
  2498. return pos - buf;
  2499. }
  2500. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2501. char *buf, size_t buflen)
  2502. {
  2503. struct wpa_cred *cred;
  2504. int ret;
  2505. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2506. cred = wpa_config_add_cred(wpa_s->conf);
  2507. if (cred == NULL)
  2508. return -1;
  2509. wpa_msg(wpa_s, MSG_INFO, CRED_ADDED "%d", cred->id);
  2510. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2511. if (os_snprintf_error(buflen, ret))
  2512. return -1;
  2513. return ret;
  2514. }
  2515. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2516. struct wpa_cred *cred)
  2517. {
  2518. struct wpa_ssid *ssid;
  2519. char str[20];
  2520. int id;
  2521. if (cred == NULL) {
  2522. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2523. return -1;
  2524. }
  2525. id = cred->id;
  2526. if (wpa_config_remove_cred(wpa_s->conf, id) < 0) {
  2527. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2528. return -1;
  2529. }
  2530. wpa_msg(wpa_s, MSG_INFO, CRED_REMOVED "%d", id);
  2531. /* Remove any network entry created based on the removed credential */
  2532. ssid = wpa_s->conf->ssid;
  2533. while (ssid) {
  2534. if (ssid->parent_cred == cred) {
  2535. int res;
  2536. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2537. "used the removed credential", ssid->id);
  2538. res = os_snprintf(str, sizeof(str), "%d", ssid->id);
  2539. if (os_snprintf_error(sizeof(str), res))
  2540. str[sizeof(str) - 1] = '\0';
  2541. ssid = ssid->next;
  2542. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2543. } else
  2544. ssid = ssid->next;
  2545. }
  2546. return 0;
  2547. }
  2548. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2549. char *cmd)
  2550. {
  2551. int id;
  2552. struct wpa_cred *cred, *prev;
  2553. /* cmd: "<cred id>", "all", "sp_fqdn=<FQDN>", or
  2554. * "provisioning_sp=<FQDN> */
  2555. if (os_strcmp(cmd, "all") == 0) {
  2556. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2557. cred = wpa_s->conf->cred;
  2558. while (cred) {
  2559. prev = cred;
  2560. cred = cred->next;
  2561. wpas_ctrl_remove_cred(wpa_s, prev);
  2562. }
  2563. return 0;
  2564. }
  2565. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2566. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2567. cmd + 8);
  2568. cred = wpa_s->conf->cred;
  2569. while (cred) {
  2570. prev = cred;
  2571. cred = cred->next;
  2572. if (prev->domain) {
  2573. size_t i;
  2574. for (i = 0; i < prev->num_domain; i++) {
  2575. if (os_strcmp(prev->domain[i], cmd + 8)
  2576. != 0)
  2577. continue;
  2578. wpas_ctrl_remove_cred(wpa_s, prev);
  2579. break;
  2580. }
  2581. }
  2582. }
  2583. return 0;
  2584. }
  2585. if (os_strncmp(cmd, "provisioning_sp=", 16) == 0) {
  2586. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED provisioning SP FQDN '%s'",
  2587. cmd + 16);
  2588. cred = wpa_s->conf->cred;
  2589. while (cred) {
  2590. prev = cred;
  2591. cred = cred->next;
  2592. if (prev->provisioning_sp &&
  2593. os_strcmp(prev->provisioning_sp, cmd + 16) == 0)
  2594. wpas_ctrl_remove_cred(wpa_s, prev);
  2595. }
  2596. return 0;
  2597. }
  2598. id = atoi(cmd);
  2599. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2600. cred = wpa_config_get_cred(wpa_s->conf, id);
  2601. return wpas_ctrl_remove_cred(wpa_s, cred);
  2602. }
  2603. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2604. char *cmd)
  2605. {
  2606. int id;
  2607. struct wpa_cred *cred;
  2608. char *name, *value;
  2609. /* cmd: "<cred id> <variable name> <value>" */
  2610. name = os_strchr(cmd, ' ');
  2611. if (name == NULL)
  2612. return -1;
  2613. *name++ = '\0';
  2614. value = os_strchr(name, ' ');
  2615. if (value == NULL)
  2616. return -1;
  2617. *value++ = '\0';
  2618. id = atoi(cmd);
  2619. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2620. id, name);
  2621. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2622. (u8 *) value, os_strlen(value));
  2623. cred = wpa_config_get_cred(wpa_s->conf, id);
  2624. if (cred == NULL) {
  2625. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2626. id);
  2627. return -1;
  2628. }
  2629. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2630. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2631. "variable '%s'", name);
  2632. return -1;
  2633. }
  2634. wpa_msg(wpa_s, MSG_INFO, CRED_MODIFIED "%d %s", cred->id, name);
  2635. return 0;
  2636. }
  2637. static int wpa_supplicant_ctrl_iface_get_cred(struct wpa_supplicant *wpa_s,
  2638. char *cmd, char *buf,
  2639. size_t buflen)
  2640. {
  2641. int id;
  2642. size_t res;
  2643. struct wpa_cred *cred;
  2644. char *name, *value;
  2645. /* cmd: "<cred id> <variable name>" */
  2646. name = os_strchr(cmd, ' ');
  2647. if (name == NULL)
  2648. return -1;
  2649. *name++ = '\0';
  2650. id = atoi(cmd);
  2651. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CRED id=%d name='%s'",
  2652. id, name);
  2653. cred = wpa_config_get_cred(wpa_s->conf, id);
  2654. if (cred == NULL) {
  2655. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2656. id);
  2657. return -1;
  2658. }
  2659. value = wpa_config_get_cred_no_key(cred, name);
  2660. if (value == NULL) {
  2661. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get cred variable '%s'",
  2662. name);
  2663. return -1;
  2664. }
  2665. res = os_strlcpy(buf, value, buflen);
  2666. if (res >= buflen) {
  2667. os_free(value);
  2668. return -1;
  2669. }
  2670. os_free(value);
  2671. return res;
  2672. }
  2673. #ifndef CONFIG_NO_CONFIG_WRITE
  2674. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2675. {
  2676. int ret;
  2677. if (!wpa_s->conf->update_config) {
  2678. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2679. "to update configuration (update_config=0)");
  2680. return -1;
  2681. }
  2682. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2683. if (ret) {
  2684. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2685. "update configuration");
  2686. } else {
  2687. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2688. " updated");
  2689. }
  2690. return ret;
  2691. }
  2692. #endif /* CONFIG_NO_CONFIG_WRITE */
  2693. struct cipher_info {
  2694. unsigned int capa;
  2695. const char *name;
  2696. int group_only;
  2697. };
  2698. static const struct cipher_info ciphers[] = {
  2699. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2700. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2701. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2702. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2703. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2704. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2705. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2706. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2707. };
  2708. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2709. struct wpa_driver_capa *capa,
  2710. char *buf, size_t buflen)
  2711. {
  2712. int ret;
  2713. char *pos, *end;
  2714. size_t len;
  2715. unsigned int i;
  2716. pos = buf;
  2717. end = pos + buflen;
  2718. if (res < 0) {
  2719. if (strict)
  2720. return 0;
  2721. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2722. if (len >= buflen)
  2723. return -1;
  2724. return len;
  2725. }
  2726. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2727. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2728. ret = os_snprintf(pos, end - pos, "%s%s",
  2729. pos == buf ? "" : " ",
  2730. ciphers[i].name);
  2731. if (os_snprintf_error(end - pos, ret))
  2732. return pos - buf;
  2733. pos += ret;
  2734. }
  2735. }
  2736. return pos - buf;
  2737. }
  2738. static int ctrl_iface_get_capability_group(int res, char *strict,
  2739. struct wpa_driver_capa *capa,
  2740. char *buf, size_t buflen)
  2741. {
  2742. int ret;
  2743. char *pos, *end;
  2744. size_t len;
  2745. unsigned int i;
  2746. pos = buf;
  2747. end = pos + buflen;
  2748. if (res < 0) {
  2749. if (strict)
  2750. return 0;
  2751. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2752. if (len >= buflen)
  2753. return -1;
  2754. return len;
  2755. }
  2756. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2757. if (capa->enc & ciphers[i].capa) {
  2758. ret = os_snprintf(pos, end - pos, "%s%s",
  2759. pos == buf ? "" : " ",
  2760. ciphers[i].name);
  2761. if (os_snprintf_error(end - pos, ret))
  2762. return pos - buf;
  2763. pos += ret;
  2764. }
  2765. }
  2766. return pos - buf;
  2767. }
  2768. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2769. struct wpa_driver_capa *capa,
  2770. char *buf, size_t buflen)
  2771. {
  2772. int ret;
  2773. char *pos, *end;
  2774. size_t len;
  2775. pos = buf;
  2776. end = pos + buflen;
  2777. if (res < 0) {
  2778. if (strict)
  2779. return 0;
  2780. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2781. "NONE", buflen);
  2782. if (len >= buflen)
  2783. return -1;
  2784. return len;
  2785. }
  2786. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2787. if (os_snprintf_error(end - pos, ret))
  2788. return pos - buf;
  2789. pos += ret;
  2790. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2791. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2792. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2793. if (os_snprintf_error(end - pos, ret))
  2794. return pos - buf;
  2795. pos += ret;
  2796. }
  2797. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2798. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2799. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2800. if (os_snprintf_error(end - pos, ret))
  2801. return pos - buf;
  2802. pos += ret;
  2803. }
  2804. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2805. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2806. if (os_snprintf_error(end - pos, ret))
  2807. return pos - buf;
  2808. pos += ret;
  2809. }
  2810. return pos - buf;
  2811. }
  2812. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2813. struct wpa_driver_capa *capa,
  2814. char *buf, size_t buflen)
  2815. {
  2816. int ret;
  2817. char *pos, *end;
  2818. size_t len;
  2819. pos = buf;
  2820. end = pos + buflen;
  2821. if (res < 0) {
  2822. if (strict)
  2823. return 0;
  2824. len = os_strlcpy(buf, "RSN WPA", buflen);
  2825. if (len >= buflen)
  2826. return -1;
  2827. return len;
  2828. }
  2829. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2830. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2831. ret = os_snprintf(pos, end - pos, "%sRSN",
  2832. pos == buf ? "" : " ");
  2833. if (os_snprintf_error(end - pos, ret))
  2834. return pos - buf;
  2835. pos += ret;
  2836. }
  2837. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2838. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2839. ret = os_snprintf(pos, end - pos, "%sWPA",
  2840. pos == buf ? "" : " ");
  2841. if (os_snprintf_error(end - pos, ret))
  2842. return pos - buf;
  2843. pos += ret;
  2844. }
  2845. return pos - buf;
  2846. }
  2847. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2848. struct wpa_driver_capa *capa,
  2849. char *buf, size_t buflen)
  2850. {
  2851. int ret;
  2852. char *pos, *end;
  2853. size_t len;
  2854. pos = buf;
  2855. end = pos + buflen;
  2856. if (res < 0) {
  2857. if (strict)
  2858. return 0;
  2859. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2860. if (len >= buflen)
  2861. return -1;
  2862. return len;
  2863. }
  2864. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2865. ret = os_snprintf(pos, end - pos, "%sOPEN",
  2866. pos == buf ? "" : " ");
  2867. if (os_snprintf_error(end - pos, ret))
  2868. return pos - buf;
  2869. pos += ret;
  2870. }
  2871. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2872. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2873. pos == buf ? "" : " ");
  2874. if (os_snprintf_error(end - pos, ret))
  2875. return pos - buf;
  2876. pos += ret;
  2877. }
  2878. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2879. ret = os_snprintf(pos, end - pos, "%sLEAP",
  2880. pos == buf ? "" : " ");
  2881. if (os_snprintf_error(end - pos, ret))
  2882. return pos - buf;
  2883. pos += ret;
  2884. }
  2885. return pos - buf;
  2886. }
  2887. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2888. struct wpa_driver_capa *capa,
  2889. char *buf, size_t buflen)
  2890. {
  2891. int ret;
  2892. char *pos, *end;
  2893. size_t len;
  2894. pos = buf;
  2895. end = pos + buflen;
  2896. if (res < 0) {
  2897. if (strict)
  2898. return 0;
  2899. len = os_strlcpy(buf, "IBSS AP", buflen);
  2900. if (len >= buflen)
  2901. return -1;
  2902. return len;
  2903. }
  2904. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2905. ret = os_snprintf(pos, end - pos, "%sIBSS",
  2906. pos == buf ? "" : " ");
  2907. if (os_snprintf_error(end - pos, ret))
  2908. return pos - buf;
  2909. pos += ret;
  2910. }
  2911. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2912. ret = os_snprintf(pos, end - pos, "%sAP",
  2913. pos == buf ? "" : " ");
  2914. if (os_snprintf_error(end - pos, ret))
  2915. return pos - buf;
  2916. pos += ret;
  2917. }
  2918. return pos - buf;
  2919. }
  2920. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2921. char *buf, size_t buflen)
  2922. {
  2923. struct hostapd_channel_data *chnl;
  2924. int ret, i, j;
  2925. char *pos, *end, *hmode;
  2926. pos = buf;
  2927. end = pos + buflen;
  2928. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2929. switch (wpa_s->hw.modes[j].mode) {
  2930. case HOSTAPD_MODE_IEEE80211B:
  2931. hmode = "B";
  2932. break;
  2933. case HOSTAPD_MODE_IEEE80211G:
  2934. hmode = "G";
  2935. break;
  2936. case HOSTAPD_MODE_IEEE80211A:
  2937. hmode = "A";
  2938. break;
  2939. case HOSTAPD_MODE_IEEE80211AD:
  2940. hmode = "AD";
  2941. break;
  2942. default:
  2943. continue;
  2944. }
  2945. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2946. if (os_snprintf_error(end - pos, ret))
  2947. return pos - buf;
  2948. pos += ret;
  2949. chnl = wpa_s->hw.modes[j].channels;
  2950. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2951. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2952. continue;
  2953. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2954. if (os_snprintf_error(end - pos, ret))
  2955. return pos - buf;
  2956. pos += ret;
  2957. }
  2958. ret = os_snprintf(pos, end - pos, "\n");
  2959. if (os_snprintf_error(end - pos, ret))
  2960. return pos - buf;
  2961. pos += ret;
  2962. }
  2963. return pos - buf;
  2964. }
  2965. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2966. char *buf, size_t buflen)
  2967. {
  2968. struct hostapd_channel_data *chnl;
  2969. int ret, i, j;
  2970. char *pos, *end, *hmode;
  2971. pos = buf;
  2972. end = pos + buflen;
  2973. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2974. switch (wpa_s->hw.modes[j].mode) {
  2975. case HOSTAPD_MODE_IEEE80211B:
  2976. hmode = "B";
  2977. break;
  2978. case HOSTAPD_MODE_IEEE80211G:
  2979. hmode = "G";
  2980. break;
  2981. case HOSTAPD_MODE_IEEE80211A:
  2982. hmode = "A";
  2983. break;
  2984. case HOSTAPD_MODE_IEEE80211AD:
  2985. hmode = "AD";
  2986. break;
  2987. default:
  2988. continue;
  2989. }
  2990. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2991. hmode);
  2992. if (os_snprintf_error(end - pos, ret))
  2993. return pos - buf;
  2994. pos += ret;
  2995. chnl = wpa_s->hw.modes[j].channels;
  2996. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2997. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2998. continue;
  2999. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s%s\n",
  3000. chnl[i].chan, chnl[i].freq,
  3001. chnl[i].flag & HOSTAPD_CHAN_NO_IR ?
  3002. " (NO_IR)" : "",
  3003. chnl[i].flag & HOSTAPD_CHAN_RADAR ?
  3004. " (DFS)" : "");
  3005. if (os_snprintf_error(end - pos, ret))
  3006. return pos - buf;
  3007. pos += ret;
  3008. }
  3009. ret = os_snprintf(pos, end - pos, "\n");
  3010. if (os_snprintf_error(end - pos, ret))
  3011. return pos - buf;
  3012. pos += ret;
  3013. }
  3014. return pos - buf;
  3015. }
  3016. static int wpa_supplicant_ctrl_iface_get_capability(
  3017. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  3018. size_t buflen)
  3019. {
  3020. struct wpa_driver_capa capa;
  3021. int res;
  3022. char *strict;
  3023. char field[30];
  3024. size_t len;
  3025. /* Determine whether or not strict checking was requested */
  3026. len = os_strlcpy(field, _field, sizeof(field));
  3027. if (len >= sizeof(field))
  3028. return -1;
  3029. strict = os_strchr(field, ' ');
  3030. if (strict != NULL) {
  3031. *strict++ = '\0';
  3032. if (os_strcmp(strict, "strict") != 0)
  3033. return -1;
  3034. }
  3035. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  3036. field, strict ? strict : "");
  3037. if (os_strcmp(field, "eap") == 0) {
  3038. return eap_get_names(buf, buflen);
  3039. }
  3040. res = wpa_drv_get_capa(wpa_s, &capa);
  3041. if (os_strcmp(field, "pairwise") == 0)
  3042. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  3043. buf, buflen);
  3044. if (os_strcmp(field, "group") == 0)
  3045. return ctrl_iface_get_capability_group(res, strict, &capa,
  3046. buf, buflen);
  3047. if (os_strcmp(field, "key_mgmt") == 0)
  3048. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  3049. buf, buflen);
  3050. if (os_strcmp(field, "proto") == 0)
  3051. return ctrl_iface_get_capability_proto(res, strict, &capa,
  3052. buf, buflen);
  3053. if (os_strcmp(field, "auth_alg") == 0)
  3054. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  3055. buf, buflen);
  3056. if (os_strcmp(field, "modes") == 0)
  3057. return ctrl_iface_get_capability_modes(res, strict, &capa,
  3058. buf, buflen);
  3059. if (os_strcmp(field, "channels") == 0)
  3060. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  3061. if (os_strcmp(field, "freq") == 0)
  3062. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  3063. #ifdef CONFIG_TDLS
  3064. if (os_strcmp(field, "tdls") == 0)
  3065. return ctrl_iface_get_capability_tdls(wpa_s, buf, buflen);
  3066. #endif /* CONFIG_TDLS */
  3067. #ifdef CONFIG_ERP
  3068. if (os_strcmp(field, "erp") == 0) {
  3069. res = os_snprintf(buf, buflen, "ERP");
  3070. if (os_snprintf_error(buflen, res))
  3071. return -1;
  3072. return res;
  3073. }
  3074. #endif /* CONFIG_EPR */
  3075. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  3076. field);
  3077. return -1;
  3078. }
  3079. #ifdef CONFIG_INTERWORKING
  3080. static char * anqp_add_hex(char *pos, char *end, const char *title,
  3081. struct wpabuf *data)
  3082. {
  3083. char *start = pos;
  3084. size_t i;
  3085. int ret;
  3086. const u8 *d;
  3087. if (data == NULL)
  3088. return start;
  3089. ret = os_snprintf(pos, end - pos, "%s=", title);
  3090. if (os_snprintf_error(end - pos, ret))
  3091. return start;
  3092. pos += ret;
  3093. d = wpabuf_head_u8(data);
  3094. for (i = 0; i < wpabuf_len(data); i++) {
  3095. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  3096. if (os_snprintf_error(end - pos, ret))
  3097. return start;
  3098. pos += ret;
  3099. }
  3100. ret = os_snprintf(pos, end - pos, "\n");
  3101. if (os_snprintf_error(end - pos, ret))
  3102. return start;
  3103. pos += ret;
  3104. return pos;
  3105. }
  3106. #endif /* CONFIG_INTERWORKING */
  3107. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  3108. unsigned long mask, char *buf, size_t buflen)
  3109. {
  3110. size_t i;
  3111. int ret;
  3112. char *pos, *end;
  3113. const u8 *ie, *ie2;
  3114. pos = buf;
  3115. end = buf + buflen;
  3116. if (mask & WPA_BSS_MASK_ID) {
  3117. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  3118. if (os_snprintf_error(end - pos, ret))
  3119. return 0;
  3120. pos += ret;
  3121. }
  3122. if (mask & WPA_BSS_MASK_BSSID) {
  3123. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  3124. MAC2STR(bss->bssid));
  3125. if (os_snprintf_error(end - pos, ret))
  3126. return 0;
  3127. pos += ret;
  3128. }
  3129. if (mask & WPA_BSS_MASK_FREQ) {
  3130. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  3131. if (os_snprintf_error(end - pos, ret))
  3132. return 0;
  3133. pos += ret;
  3134. }
  3135. if (mask & WPA_BSS_MASK_BEACON_INT) {
  3136. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  3137. bss->beacon_int);
  3138. if (os_snprintf_error(end - pos, ret))
  3139. return 0;
  3140. pos += ret;
  3141. }
  3142. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  3143. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  3144. bss->caps);
  3145. if (os_snprintf_error(end - pos, ret))
  3146. return 0;
  3147. pos += ret;
  3148. }
  3149. if (mask & WPA_BSS_MASK_QUAL) {
  3150. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  3151. if (os_snprintf_error(end - pos, ret))
  3152. return 0;
  3153. pos += ret;
  3154. }
  3155. if (mask & WPA_BSS_MASK_NOISE) {
  3156. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  3157. if (os_snprintf_error(end - pos, ret))
  3158. return 0;
  3159. pos += ret;
  3160. }
  3161. if (mask & WPA_BSS_MASK_LEVEL) {
  3162. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  3163. if (os_snprintf_error(end - pos, ret))
  3164. return 0;
  3165. pos += ret;
  3166. }
  3167. if (mask & WPA_BSS_MASK_TSF) {
  3168. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  3169. (unsigned long long) bss->tsf);
  3170. if (os_snprintf_error(end - pos, ret))
  3171. return 0;
  3172. pos += ret;
  3173. }
  3174. if (mask & WPA_BSS_MASK_AGE) {
  3175. struct os_reltime now;
  3176. os_get_reltime(&now);
  3177. ret = os_snprintf(pos, end - pos, "age=%d\n",
  3178. (int) (now.sec - bss->last_update.sec));
  3179. if (os_snprintf_error(end - pos, ret))
  3180. return 0;
  3181. pos += ret;
  3182. }
  3183. if (mask & WPA_BSS_MASK_IE) {
  3184. ret = os_snprintf(pos, end - pos, "ie=");
  3185. if (os_snprintf_error(end - pos, ret))
  3186. return 0;
  3187. pos += ret;
  3188. ie = (const u8 *) (bss + 1);
  3189. for (i = 0; i < bss->ie_len; i++) {
  3190. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  3191. if (os_snprintf_error(end - pos, ret))
  3192. return 0;
  3193. pos += ret;
  3194. }
  3195. ret = os_snprintf(pos, end - pos, "\n");
  3196. if (os_snprintf_error(end - pos, ret))
  3197. return 0;
  3198. pos += ret;
  3199. }
  3200. if (mask & WPA_BSS_MASK_FLAGS) {
  3201. ret = os_snprintf(pos, end - pos, "flags=");
  3202. if (os_snprintf_error(end - pos, ret))
  3203. return 0;
  3204. pos += ret;
  3205. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  3206. if (ie)
  3207. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  3208. 2 + ie[1]);
  3209. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  3210. if (ie2)
  3211. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  3212. 2 + ie2[1]);
  3213. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  3214. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  3215. ret = os_snprintf(pos, end - pos, "[WEP]");
  3216. if (os_snprintf_error(end - pos, ret))
  3217. return 0;
  3218. pos += ret;
  3219. }
  3220. if (bss_is_dmg(bss)) {
  3221. const char *s;
  3222. ret = os_snprintf(pos, end - pos, "[DMG]");
  3223. if (os_snprintf_error(end - pos, ret))
  3224. return 0;
  3225. pos += ret;
  3226. switch (bss->caps & IEEE80211_CAP_DMG_MASK) {
  3227. case IEEE80211_CAP_DMG_IBSS:
  3228. s = "[IBSS]";
  3229. break;
  3230. case IEEE80211_CAP_DMG_AP:
  3231. s = "[ESS]";
  3232. break;
  3233. case IEEE80211_CAP_DMG_PBSS:
  3234. s = "[PBSS]";
  3235. break;
  3236. default:
  3237. s = "";
  3238. break;
  3239. }
  3240. ret = os_snprintf(pos, end - pos, "%s", s);
  3241. if (os_snprintf_error(end - pos, ret))
  3242. return 0;
  3243. pos += ret;
  3244. } else {
  3245. if (bss->caps & IEEE80211_CAP_IBSS) {
  3246. ret = os_snprintf(pos, end - pos, "[IBSS]");
  3247. if (os_snprintf_error(end - pos, ret))
  3248. return 0;
  3249. pos += ret;
  3250. }
  3251. if (bss->caps & IEEE80211_CAP_ESS) {
  3252. ret = os_snprintf(pos, end - pos, "[ESS]");
  3253. if (os_snprintf_error(end - pos, ret))
  3254. return 0;
  3255. pos += ret;
  3256. }
  3257. }
  3258. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  3259. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  3260. ret = os_snprintf(pos, end - pos, "[P2P]");
  3261. if (os_snprintf_error(end - pos, ret))
  3262. return 0;
  3263. pos += ret;
  3264. }
  3265. #ifdef CONFIG_HS20
  3266. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  3267. ret = os_snprintf(pos, end - pos, "[HS20]");
  3268. if (os_snprintf_error(end - pos, ret))
  3269. return 0;
  3270. pos += ret;
  3271. }
  3272. #endif /* CONFIG_HS20 */
  3273. ret = os_snprintf(pos, end - pos, "\n");
  3274. if (os_snprintf_error(end - pos, ret))
  3275. return 0;
  3276. pos += ret;
  3277. }
  3278. if (mask & WPA_BSS_MASK_SSID) {
  3279. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  3280. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  3281. if (os_snprintf_error(end - pos, ret))
  3282. return 0;
  3283. pos += ret;
  3284. }
  3285. #ifdef CONFIG_WPS
  3286. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  3287. ie = (const u8 *) (bss + 1);
  3288. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  3289. if (ret < 0 || ret >= end - pos)
  3290. return 0;
  3291. pos += ret;
  3292. }
  3293. #endif /* CONFIG_WPS */
  3294. #ifdef CONFIG_P2P
  3295. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  3296. ie = (const u8 *) (bss + 1);
  3297. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  3298. if (ret < 0 || ret >= end - pos)
  3299. return 0;
  3300. pos += ret;
  3301. }
  3302. #endif /* CONFIG_P2P */
  3303. #ifdef CONFIG_WIFI_DISPLAY
  3304. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  3305. struct wpabuf *wfd;
  3306. ie = (const u8 *) (bss + 1);
  3307. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  3308. WFD_IE_VENDOR_TYPE);
  3309. if (wfd) {
  3310. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  3311. if (os_snprintf_error(end - pos, ret)) {
  3312. wpabuf_free(wfd);
  3313. return 0;
  3314. }
  3315. pos += ret;
  3316. pos += wpa_snprintf_hex(pos, end - pos,
  3317. wpabuf_head(wfd),
  3318. wpabuf_len(wfd));
  3319. wpabuf_free(wfd);
  3320. ret = os_snprintf(pos, end - pos, "\n");
  3321. if (os_snprintf_error(end - pos, ret))
  3322. return 0;
  3323. pos += ret;
  3324. }
  3325. }
  3326. #endif /* CONFIG_WIFI_DISPLAY */
  3327. #ifdef CONFIG_INTERWORKING
  3328. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  3329. struct wpa_bss_anqp *anqp = bss->anqp;
  3330. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  3331. anqp->venue_name);
  3332. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  3333. anqp->network_auth_type);
  3334. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  3335. anqp->roaming_consortium);
  3336. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  3337. anqp->ip_addr_type_availability);
  3338. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  3339. anqp->nai_realm);
  3340. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  3341. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  3342. anqp->domain_name);
  3343. #ifdef CONFIG_HS20
  3344. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  3345. anqp->hs20_operator_friendly_name);
  3346. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  3347. anqp->hs20_wan_metrics);
  3348. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  3349. anqp->hs20_connection_capability);
  3350. pos = anqp_add_hex(pos, end, "hs20_operating_class",
  3351. anqp->hs20_operating_class);
  3352. pos = anqp_add_hex(pos, end, "hs20_osu_providers_list",
  3353. anqp->hs20_osu_providers_list);
  3354. #endif /* CONFIG_HS20 */
  3355. }
  3356. #endif /* CONFIG_INTERWORKING */
  3357. #ifdef CONFIG_MESH
  3358. if (mask & WPA_BSS_MASK_MESH_SCAN) {
  3359. ie = (const u8 *) (bss + 1);
  3360. ret = wpas_mesh_scan_result_text(ie, bss->ie_len, pos, end);
  3361. if (ret < 0 || ret >= end - pos)
  3362. return 0;
  3363. pos += ret;
  3364. }
  3365. #endif /* CONFIG_MESH */
  3366. if (mask & WPA_BSS_MASK_DELIM) {
  3367. ret = os_snprintf(pos, end - pos, "====\n");
  3368. if (os_snprintf_error(end - pos, ret))
  3369. return 0;
  3370. pos += ret;
  3371. }
  3372. return pos - buf;
  3373. }
  3374. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  3375. const char *cmd, char *buf,
  3376. size_t buflen)
  3377. {
  3378. u8 bssid[ETH_ALEN];
  3379. size_t i;
  3380. struct wpa_bss *bss;
  3381. struct wpa_bss *bsslast = NULL;
  3382. struct dl_list *next;
  3383. int ret = 0;
  3384. int len;
  3385. char *ctmp, *end = buf + buflen;
  3386. unsigned long mask = WPA_BSS_MASK_ALL;
  3387. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  3388. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  3389. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  3390. list_id);
  3391. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  3392. list_id);
  3393. } else { /* N1-N2 */
  3394. unsigned int id1, id2;
  3395. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  3396. wpa_printf(MSG_INFO, "Wrong BSS range "
  3397. "format");
  3398. return 0;
  3399. }
  3400. if (*(cmd + 6) == '-')
  3401. id1 = 0;
  3402. else
  3403. id1 = atoi(cmd + 6);
  3404. ctmp++;
  3405. if (*ctmp >= '0' && *ctmp <= '9')
  3406. id2 = atoi(ctmp);
  3407. else
  3408. id2 = (unsigned int) -1;
  3409. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  3410. if (id2 == (unsigned int) -1)
  3411. bsslast = dl_list_last(&wpa_s->bss_id,
  3412. struct wpa_bss,
  3413. list_id);
  3414. else {
  3415. bsslast = wpa_bss_get_id(wpa_s, id2);
  3416. if (bsslast == NULL && bss && id2 > id1) {
  3417. struct wpa_bss *tmp = bss;
  3418. for (;;) {
  3419. next = tmp->list_id.next;
  3420. if (next == &wpa_s->bss_id)
  3421. break;
  3422. tmp = dl_list_entry(
  3423. next, struct wpa_bss,
  3424. list_id);
  3425. if (tmp->id > id2)
  3426. break;
  3427. bsslast = tmp;
  3428. }
  3429. }
  3430. }
  3431. }
  3432. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  3433. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  3434. else if (os_strncmp(cmd, "LAST", 4) == 0)
  3435. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  3436. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  3437. i = atoi(cmd + 3);
  3438. bss = wpa_bss_get_id(wpa_s, i);
  3439. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3440. i = atoi(cmd + 5);
  3441. bss = wpa_bss_get_id(wpa_s, i);
  3442. if (bss) {
  3443. next = bss->list_id.next;
  3444. if (next == &wpa_s->bss_id)
  3445. bss = NULL;
  3446. else
  3447. bss = dl_list_entry(next, struct wpa_bss,
  3448. list_id);
  3449. }
  3450. #ifdef CONFIG_P2P
  3451. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  3452. if (hwaddr_aton(cmd + 13, bssid) == 0)
  3453. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  3454. else
  3455. bss = NULL;
  3456. #endif /* CONFIG_P2P */
  3457. } else if (hwaddr_aton(cmd, bssid) == 0)
  3458. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3459. else {
  3460. struct wpa_bss *tmp;
  3461. i = atoi(cmd);
  3462. bss = NULL;
  3463. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  3464. {
  3465. if (i-- == 0) {
  3466. bss = tmp;
  3467. break;
  3468. }
  3469. }
  3470. }
  3471. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  3472. mask = strtoul(ctmp + 5, NULL, 0x10);
  3473. if (mask == 0)
  3474. mask = WPA_BSS_MASK_ALL;
  3475. }
  3476. if (bss == NULL)
  3477. return 0;
  3478. if (bsslast == NULL)
  3479. bsslast = bss;
  3480. do {
  3481. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3482. ret += len;
  3483. buf += len;
  3484. buflen -= len;
  3485. if (bss == bsslast) {
  3486. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3487. (bss == dl_list_last(&wpa_s->bss_id,
  3488. struct wpa_bss, list_id))) {
  3489. int res;
  3490. res = os_snprintf(buf - 5, end - buf + 5,
  3491. "####\n");
  3492. if (os_snprintf_error(end - buf + 5, res)) {
  3493. wpa_printf(MSG_DEBUG,
  3494. "Could not add end delim");
  3495. }
  3496. }
  3497. break;
  3498. }
  3499. next = bss->list_id.next;
  3500. if (next == &wpa_s->bss_id)
  3501. break;
  3502. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3503. } while (bss && len);
  3504. return ret;
  3505. }
  3506. static int wpa_supplicant_ctrl_iface_ap_scan(
  3507. struct wpa_supplicant *wpa_s, char *cmd)
  3508. {
  3509. int ap_scan = atoi(cmd);
  3510. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3511. }
  3512. static int wpa_supplicant_ctrl_iface_scan_interval(
  3513. struct wpa_supplicant *wpa_s, char *cmd)
  3514. {
  3515. int scan_int = atoi(cmd);
  3516. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3517. }
  3518. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3519. struct wpa_supplicant *wpa_s, char *cmd)
  3520. {
  3521. int expire_age = atoi(cmd);
  3522. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3523. }
  3524. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3525. struct wpa_supplicant *wpa_s, char *cmd)
  3526. {
  3527. int expire_count = atoi(cmd);
  3528. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3529. }
  3530. static int wpa_supplicant_ctrl_iface_bss_flush(
  3531. struct wpa_supplicant *wpa_s, char *cmd)
  3532. {
  3533. int flush_age = atoi(cmd);
  3534. if (flush_age == 0)
  3535. wpa_bss_flush(wpa_s);
  3536. else
  3537. wpa_bss_flush_by_age(wpa_s, flush_age);
  3538. return 0;
  3539. }
  3540. #ifdef CONFIG_TESTING_OPTIONS
  3541. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3542. {
  3543. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3544. /* MLME-DELETEKEYS.request */
  3545. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3546. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3547. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3548. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3549. #ifdef CONFIG_IEEE80211W
  3550. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3551. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3552. #endif /* CONFIG_IEEE80211W */
  3553. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3554. 0);
  3555. /* MLME-SETPROTECTION.request(None) */
  3556. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3557. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3558. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3559. wpa_sm_drop_sa(wpa_s->wpa);
  3560. }
  3561. #endif /* CONFIG_TESTING_OPTIONS */
  3562. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3563. char *addr)
  3564. {
  3565. #ifdef CONFIG_NO_SCAN_PROCESSING
  3566. return -1;
  3567. #else /* CONFIG_NO_SCAN_PROCESSING */
  3568. u8 bssid[ETH_ALEN];
  3569. struct wpa_bss *bss;
  3570. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3571. if (hwaddr_aton(addr, bssid)) {
  3572. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3573. "address '%s'", addr);
  3574. return -1;
  3575. }
  3576. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3577. if (!ssid) {
  3578. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3579. "configuration known for the target AP");
  3580. return -1;
  3581. }
  3582. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3583. if (!bss) {
  3584. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3585. "from BSS table");
  3586. return -1;
  3587. }
  3588. /*
  3589. * TODO: Find best network configuration block from configuration to
  3590. * allow roaming to other networks
  3591. */
  3592. wpa_s->reassociate = 1;
  3593. wpa_supplicant_connect(wpa_s, bss, ssid);
  3594. return 0;
  3595. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3596. }
  3597. #ifdef CONFIG_P2P
  3598. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3599. {
  3600. unsigned int timeout = atoi(cmd);
  3601. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3602. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3603. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  3604. char *pos;
  3605. unsigned int search_delay;
  3606. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3607. wpa_dbg(wpa_s, MSG_INFO,
  3608. "Reject P2P_FIND since interface is disabled");
  3609. return -1;
  3610. }
  3611. if (os_strstr(cmd, "type=social"))
  3612. type = P2P_FIND_ONLY_SOCIAL;
  3613. else if (os_strstr(cmd, "type=progressive"))
  3614. type = P2P_FIND_PROGRESSIVE;
  3615. pos = os_strstr(cmd, "dev_id=");
  3616. if (pos) {
  3617. pos += 7;
  3618. if (hwaddr_aton(pos, dev_id))
  3619. return -1;
  3620. _dev_id = dev_id;
  3621. }
  3622. pos = os_strstr(cmd, "dev_type=");
  3623. if (pos) {
  3624. pos += 9;
  3625. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  3626. return -1;
  3627. _dev_type = dev_type;
  3628. }
  3629. pos = os_strstr(cmd, "delay=");
  3630. if (pos) {
  3631. pos += 6;
  3632. search_delay = atoi(pos);
  3633. } else
  3634. search_delay = wpas_p2p_search_delay(wpa_s);
  3635. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  3636. _dev_id, search_delay);
  3637. }
  3638. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3639. char *buf, size_t buflen)
  3640. {
  3641. u8 addr[ETH_ALEN];
  3642. char *pos, *pos2;
  3643. char *pin = NULL;
  3644. enum p2p_wps_method wps_method;
  3645. int new_pin;
  3646. int ret;
  3647. int persistent_group, persistent_id = -1;
  3648. int join;
  3649. int auth;
  3650. int automatic;
  3651. int go_intent = -1;
  3652. int freq = 0;
  3653. int pd;
  3654. int ht40, vht;
  3655. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3656. * [persistent|persistent=<network id>]
  3657. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3658. * [ht40] [vht] */
  3659. if (hwaddr_aton(cmd, addr))
  3660. return -1;
  3661. pos = cmd + 17;
  3662. if (*pos != ' ')
  3663. return -1;
  3664. pos++;
  3665. persistent_group = os_strstr(pos, " persistent") != NULL;
  3666. pos2 = os_strstr(pos, " persistent=");
  3667. if (pos2) {
  3668. struct wpa_ssid *ssid;
  3669. persistent_id = atoi(pos2 + 12);
  3670. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3671. if (ssid == NULL || ssid->disabled != 2 ||
  3672. ssid->mode != WPAS_MODE_P2P_GO) {
  3673. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3674. "SSID id=%d for persistent P2P group (GO)",
  3675. persistent_id);
  3676. return -1;
  3677. }
  3678. }
  3679. join = os_strstr(pos, " join") != NULL;
  3680. auth = os_strstr(pos, " auth") != NULL;
  3681. automatic = os_strstr(pos, " auto") != NULL;
  3682. pd = os_strstr(pos, " provdisc") != NULL;
  3683. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3684. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3685. vht;
  3686. pos2 = os_strstr(pos, " go_intent=");
  3687. if (pos2) {
  3688. pos2 += 11;
  3689. go_intent = atoi(pos2);
  3690. if (go_intent < 0 || go_intent > 15)
  3691. return -1;
  3692. }
  3693. pos2 = os_strstr(pos, " freq=");
  3694. if (pos2) {
  3695. pos2 += 6;
  3696. freq = atoi(pos2);
  3697. if (freq <= 0)
  3698. return -1;
  3699. }
  3700. if (os_strncmp(pos, "pin", 3) == 0) {
  3701. /* Request random PIN (to be displayed) and enable the PIN */
  3702. wps_method = WPS_PIN_DISPLAY;
  3703. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3704. wps_method = WPS_PBC;
  3705. } else {
  3706. pin = pos;
  3707. pos = os_strchr(pin, ' ');
  3708. wps_method = WPS_PIN_KEYPAD;
  3709. if (pos) {
  3710. *pos++ = '\0';
  3711. if (os_strncmp(pos, "display", 7) == 0)
  3712. wps_method = WPS_PIN_DISPLAY;
  3713. }
  3714. if (!wps_pin_str_valid(pin)) {
  3715. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3716. return 17;
  3717. }
  3718. }
  3719. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3720. persistent_group, automatic, join,
  3721. auth, go_intent, freq, persistent_id, pd,
  3722. ht40, vht);
  3723. if (new_pin == -2) {
  3724. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3725. return 25;
  3726. }
  3727. if (new_pin == -3) {
  3728. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3729. return 25;
  3730. }
  3731. if (new_pin < 0)
  3732. return -1;
  3733. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3734. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3735. if (os_snprintf_error(buflen, ret))
  3736. return -1;
  3737. return ret;
  3738. }
  3739. os_memcpy(buf, "OK\n", 3);
  3740. return 3;
  3741. }
  3742. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3743. {
  3744. unsigned int timeout = atoi(cmd);
  3745. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  3746. wpa_dbg(wpa_s, MSG_INFO,
  3747. "Reject P2P_LISTEN since interface is disabled");
  3748. return -1;
  3749. }
  3750. return wpas_p2p_listen(wpa_s, timeout);
  3751. }
  3752. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3753. {
  3754. u8 addr[ETH_ALEN];
  3755. char *pos;
  3756. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3757. /* <addr> <config method> [join|auto] */
  3758. if (hwaddr_aton(cmd, addr))
  3759. return -1;
  3760. pos = cmd + 17;
  3761. if (*pos != ' ')
  3762. return -1;
  3763. pos++;
  3764. if (os_strstr(pos, " join") != NULL)
  3765. use = WPAS_P2P_PD_FOR_JOIN;
  3766. else if (os_strstr(pos, " auto") != NULL)
  3767. use = WPAS_P2P_PD_AUTO;
  3768. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3769. }
  3770. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3771. size_t buflen)
  3772. {
  3773. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3774. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3775. ssid->passphrase == NULL)
  3776. return -1;
  3777. os_strlcpy(buf, ssid->passphrase, buflen);
  3778. return os_strlen(buf);
  3779. }
  3780. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3781. char *buf, size_t buflen)
  3782. {
  3783. u64 ref;
  3784. int res;
  3785. u8 dst_buf[ETH_ALEN], *dst;
  3786. struct wpabuf *tlvs;
  3787. char *pos;
  3788. size_t len;
  3789. if (hwaddr_aton(cmd, dst_buf))
  3790. return -1;
  3791. dst = dst_buf;
  3792. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3793. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3794. dst = NULL;
  3795. pos = cmd + 17;
  3796. if (*pos != ' ')
  3797. return -1;
  3798. pos++;
  3799. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3800. u8 version;
  3801. pos += 5;
  3802. if (hexstr2bin(pos, &version, 1) < 0)
  3803. return -1;
  3804. pos += 2;
  3805. if (*pos != ' ')
  3806. return -1;
  3807. pos++;
  3808. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3809. #ifdef CONFIG_WIFI_DISPLAY
  3810. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3811. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3812. #endif /* CONFIG_WIFI_DISPLAY */
  3813. } else {
  3814. len = os_strlen(pos);
  3815. if (len & 1)
  3816. return -1;
  3817. len /= 2;
  3818. tlvs = wpabuf_alloc(len);
  3819. if (tlvs == NULL)
  3820. return -1;
  3821. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3822. wpabuf_free(tlvs);
  3823. return -1;
  3824. }
  3825. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3826. wpabuf_free(tlvs);
  3827. }
  3828. if (ref == 0)
  3829. return -1;
  3830. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3831. if (os_snprintf_error(buflen, res))
  3832. return -1;
  3833. return res;
  3834. }
  3835. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3836. char *cmd)
  3837. {
  3838. long long unsigned val;
  3839. u64 req;
  3840. if (sscanf(cmd, "%llx", &val) != 1)
  3841. return -1;
  3842. req = val;
  3843. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3844. }
  3845. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3846. {
  3847. int freq;
  3848. u8 dst[ETH_ALEN];
  3849. u8 dialog_token;
  3850. struct wpabuf *resp_tlvs;
  3851. char *pos, *pos2;
  3852. size_t len;
  3853. pos = os_strchr(cmd, ' ');
  3854. if (pos == NULL)
  3855. return -1;
  3856. *pos++ = '\0';
  3857. freq = atoi(cmd);
  3858. if (freq == 0)
  3859. return -1;
  3860. if (hwaddr_aton(pos, dst))
  3861. return -1;
  3862. pos += 17;
  3863. if (*pos != ' ')
  3864. return -1;
  3865. pos++;
  3866. pos2 = os_strchr(pos, ' ');
  3867. if (pos2 == NULL)
  3868. return -1;
  3869. *pos2++ = '\0';
  3870. dialog_token = atoi(pos);
  3871. len = os_strlen(pos2);
  3872. if (len & 1)
  3873. return -1;
  3874. len /= 2;
  3875. resp_tlvs = wpabuf_alloc(len);
  3876. if (resp_tlvs == NULL)
  3877. return -1;
  3878. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3879. wpabuf_free(resp_tlvs);
  3880. return -1;
  3881. }
  3882. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3883. wpabuf_free(resp_tlvs);
  3884. return 0;
  3885. }
  3886. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3887. char *cmd)
  3888. {
  3889. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3890. return -1;
  3891. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3892. return 0;
  3893. }
  3894. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3895. char *cmd)
  3896. {
  3897. char *pos;
  3898. size_t len;
  3899. struct wpabuf *query, *resp;
  3900. pos = os_strchr(cmd, ' ');
  3901. if (pos == NULL)
  3902. return -1;
  3903. *pos++ = '\0';
  3904. len = os_strlen(cmd);
  3905. if (len & 1)
  3906. return -1;
  3907. len /= 2;
  3908. query = wpabuf_alloc(len);
  3909. if (query == NULL)
  3910. return -1;
  3911. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3912. wpabuf_free(query);
  3913. return -1;
  3914. }
  3915. len = os_strlen(pos);
  3916. if (len & 1) {
  3917. wpabuf_free(query);
  3918. return -1;
  3919. }
  3920. len /= 2;
  3921. resp = wpabuf_alloc(len);
  3922. if (resp == NULL) {
  3923. wpabuf_free(query);
  3924. return -1;
  3925. }
  3926. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3927. wpabuf_free(query);
  3928. wpabuf_free(resp);
  3929. return -1;
  3930. }
  3931. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3932. wpabuf_free(query);
  3933. wpabuf_free(resp);
  3934. return -1;
  3935. }
  3936. return 0;
  3937. }
  3938. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3939. {
  3940. char *pos;
  3941. u8 version;
  3942. pos = os_strchr(cmd, ' ');
  3943. if (pos == NULL)
  3944. return -1;
  3945. *pos++ = '\0';
  3946. if (hexstr2bin(cmd, &version, 1) < 0)
  3947. return -1;
  3948. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3949. }
  3950. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3951. {
  3952. char *pos;
  3953. pos = os_strchr(cmd, ' ');
  3954. if (pos == NULL)
  3955. return -1;
  3956. *pos++ = '\0';
  3957. if (os_strcmp(cmd, "bonjour") == 0)
  3958. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3959. if (os_strcmp(cmd, "upnp") == 0)
  3960. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3961. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3962. return -1;
  3963. }
  3964. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3965. char *cmd)
  3966. {
  3967. size_t len;
  3968. struct wpabuf *query;
  3969. int ret;
  3970. len = os_strlen(cmd);
  3971. if (len & 1)
  3972. return -1;
  3973. len /= 2;
  3974. query = wpabuf_alloc(len);
  3975. if (query == NULL)
  3976. return -1;
  3977. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3978. wpabuf_free(query);
  3979. return -1;
  3980. }
  3981. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3982. wpabuf_free(query);
  3983. return ret;
  3984. }
  3985. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3986. {
  3987. char *pos;
  3988. u8 version;
  3989. pos = os_strchr(cmd, ' ');
  3990. if (pos == NULL)
  3991. return -1;
  3992. *pos++ = '\0';
  3993. if (hexstr2bin(cmd, &version, 1) < 0)
  3994. return -1;
  3995. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3996. }
  3997. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3998. {
  3999. char *pos;
  4000. pos = os_strchr(cmd, ' ');
  4001. if (pos == NULL)
  4002. return -1;
  4003. *pos++ = '\0';
  4004. if (os_strcmp(cmd, "bonjour") == 0)
  4005. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  4006. if (os_strcmp(cmd, "upnp") == 0)
  4007. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  4008. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  4009. return -1;
  4010. }
  4011. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  4012. {
  4013. u8 addr[ETH_ALEN];
  4014. /* <addr> */
  4015. if (hwaddr_aton(cmd, addr))
  4016. return -1;
  4017. return wpas_p2p_reject(wpa_s, addr);
  4018. }
  4019. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  4020. {
  4021. char *pos;
  4022. int id;
  4023. struct wpa_ssid *ssid;
  4024. u8 *_peer = NULL, peer[ETH_ALEN];
  4025. int freq = 0, pref_freq = 0;
  4026. int ht40, vht;
  4027. id = atoi(cmd);
  4028. pos = os_strstr(cmd, " peer=");
  4029. if (pos) {
  4030. pos += 6;
  4031. if (hwaddr_aton(pos, peer))
  4032. return -1;
  4033. _peer = peer;
  4034. }
  4035. ssid = wpa_config_get_network(wpa_s->conf, id);
  4036. if (ssid == NULL || ssid->disabled != 2) {
  4037. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  4038. "for persistent P2P group",
  4039. id);
  4040. return -1;
  4041. }
  4042. pos = os_strstr(cmd, " freq=");
  4043. if (pos) {
  4044. pos += 6;
  4045. freq = atoi(pos);
  4046. if (freq <= 0)
  4047. return -1;
  4048. }
  4049. pos = os_strstr(cmd, " pref=");
  4050. if (pos) {
  4051. pos += 6;
  4052. pref_freq = atoi(pos);
  4053. if (pref_freq <= 0)
  4054. return -1;
  4055. }
  4056. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4057. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4058. vht;
  4059. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  4060. pref_freq);
  4061. }
  4062. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  4063. {
  4064. char *pos;
  4065. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  4066. pos = os_strstr(cmd, " peer=");
  4067. if (!pos)
  4068. return -1;
  4069. *pos = '\0';
  4070. pos += 6;
  4071. if (hwaddr_aton(pos, peer)) {
  4072. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  4073. return -1;
  4074. }
  4075. pos = os_strstr(pos, " go_dev_addr=");
  4076. if (pos) {
  4077. pos += 13;
  4078. if (hwaddr_aton(pos, go_dev_addr)) {
  4079. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  4080. pos);
  4081. return -1;
  4082. }
  4083. go_dev = go_dev_addr;
  4084. }
  4085. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  4086. }
  4087. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  4088. {
  4089. if (os_strncmp(cmd, "persistent=", 11) == 0)
  4090. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  4091. if (os_strncmp(cmd, "group=", 6) == 0)
  4092. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  4093. return -1;
  4094. }
  4095. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  4096. char *cmd, int freq, int ht40,
  4097. int vht)
  4098. {
  4099. int id;
  4100. struct wpa_ssid *ssid;
  4101. id = atoi(cmd);
  4102. ssid = wpa_config_get_network(wpa_s->conf, id);
  4103. if (ssid == NULL || ssid->disabled != 2) {
  4104. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  4105. "for persistent P2P group",
  4106. id);
  4107. return -1;
  4108. }
  4109. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, 0, ht40, vht,
  4110. NULL, 0);
  4111. }
  4112. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  4113. {
  4114. int freq = 0, ht40, vht;
  4115. char *pos;
  4116. pos = os_strstr(cmd, "freq=");
  4117. if (pos)
  4118. freq = atoi(pos + 5);
  4119. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  4120. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  4121. vht;
  4122. if (os_strncmp(cmd, "persistent=", 11) == 0)
  4123. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  4124. ht40, vht);
  4125. if (os_strcmp(cmd, "persistent") == 0 ||
  4126. os_strncmp(cmd, "persistent ", 11) == 0)
  4127. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  4128. if (os_strncmp(cmd, "freq=", 5) == 0)
  4129. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  4130. if (ht40)
  4131. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  4132. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  4133. cmd);
  4134. return -1;
  4135. }
  4136. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  4137. char *buf, size_t buflen)
  4138. {
  4139. u8 addr[ETH_ALEN], *addr_ptr;
  4140. int next, res;
  4141. const struct p2p_peer_info *info;
  4142. char *pos, *end;
  4143. char devtype[WPS_DEV_TYPE_BUFSIZE];
  4144. struct wpa_ssid *ssid;
  4145. size_t i;
  4146. if (!wpa_s->global->p2p)
  4147. return -1;
  4148. if (os_strcmp(cmd, "FIRST") == 0) {
  4149. addr_ptr = NULL;
  4150. next = 0;
  4151. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  4152. if (hwaddr_aton(cmd + 5, addr) < 0)
  4153. return -1;
  4154. addr_ptr = addr;
  4155. next = 1;
  4156. } else {
  4157. if (hwaddr_aton(cmd, addr) < 0)
  4158. return -1;
  4159. addr_ptr = addr;
  4160. next = 0;
  4161. }
  4162. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  4163. if (info == NULL)
  4164. return -1;
  4165. pos = buf;
  4166. end = buf + buflen;
  4167. res = os_snprintf(pos, end - pos, MACSTR "\n"
  4168. "pri_dev_type=%s\n"
  4169. "device_name=%s\n"
  4170. "manufacturer=%s\n"
  4171. "model_name=%s\n"
  4172. "model_number=%s\n"
  4173. "serial_number=%s\n"
  4174. "config_methods=0x%x\n"
  4175. "dev_capab=0x%x\n"
  4176. "group_capab=0x%x\n"
  4177. "level=%d\n",
  4178. MAC2STR(info->p2p_device_addr),
  4179. wps_dev_type_bin2str(info->pri_dev_type,
  4180. devtype, sizeof(devtype)),
  4181. info->device_name,
  4182. info->manufacturer,
  4183. info->model_name,
  4184. info->model_number,
  4185. info->serial_number,
  4186. info->config_methods,
  4187. info->dev_capab,
  4188. info->group_capab,
  4189. info->level);
  4190. if (os_snprintf_error(end - pos, res))
  4191. return pos - buf;
  4192. pos += res;
  4193. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  4194. {
  4195. const u8 *t;
  4196. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  4197. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  4198. wps_dev_type_bin2str(t, devtype,
  4199. sizeof(devtype)));
  4200. if (os_snprintf_error(end - pos, res))
  4201. return pos - buf;
  4202. pos += res;
  4203. }
  4204. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  4205. if (ssid) {
  4206. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  4207. if (os_snprintf_error(end - pos, res))
  4208. return pos - buf;
  4209. pos += res;
  4210. }
  4211. res = p2p_get_peer_info_txt(info, pos, end - pos);
  4212. if (res < 0)
  4213. return pos - buf;
  4214. pos += res;
  4215. if (info->vendor_elems) {
  4216. res = os_snprintf(pos, end - pos, "vendor_elems=");
  4217. if (os_snprintf_error(end - pos, res))
  4218. return pos - buf;
  4219. pos += res;
  4220. pos += wpa_snprintf_hex(pos, end - pos,
  4221. wpabuf_head(info->vendor_elems),
  4222. wpabuf_len(info->vendor_elems));
  4223. res = os_snprintf(pos, end - pos, "\n");
  4224. if (os_snprintf_error(end - pos, res))
  4225. return pos - buf;
  4226. pos += res;
  4227. }
  4228. return pos - buf;
  4229. }
  4230. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  4231. const char *param)
  4232. {
  4233. unsigned int i;
  4234. if (wpa_s->global->p2p == NULL)
  4235. return -1;
  4236. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  4237. return -1;
  4238. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  4239. struct wpa_freq_range *freq;
  4240. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  4241. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  4242. freq->min, freq->max);
  4243. }
  4244. wpas_p2p_update_channel_list(wpa_s);
  4245. return 0;
  4246. }
  4247. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  4248. {
  4249. char *param;
  4250. if (wpa_s->global->p2p == NULL)
  4251. return -1;
  4252. param = os_strchr(cmd, ' ');
  4253. if (param == NULL)
  4254. return -1;
  4255. *param++ = '\0';
  4256. if (os_strcmp(cmd, "discoverability") == 0) {
  4257. p2p_set_client_discoverability(wpa_s->global->p2p,
  4258. atoi(param));
  4259. return 0;
  4260. }
  4261. if (os_strcmp(cmd, "managed") == 0) {
  4262. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  4263. return 0;
  4264. }
  4265. if (os_strcmp(cmd, "listen_channel") == 0) {
  4266. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  4267. atoi(param), 1);
  4268. }
  4269. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  4270. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  4271. os_strlen(param));
  4272. }
  4273. if (os_strcmp(cmd, "noa") == 0) {
  4274. char *pos;
  4275. int count, start, duration;
  4276. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  4277. count = atoi(param);
  4278. pos = os_strchr(param, ',');
  4279. if (pos == NULL)
  4280. return -1;
  4281. pos++;
  4282. start = atoi(pos);
  4283. pos = os_strchr(pos, ',');
  4284. if (pos == NULL)
  4285. return -1;
  4286. pos++;
  4287. duration = atoi(pos);
  4288. if (count < 0 || count > 255 || start < 0 || duration < 0)
  4289. return -1;
  4290. if (count == 0 && duration > 0)
  4291. return -1;
  4292. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  4293. "start=%d duration=%d", count, start, duration);
  4294. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  4295. }
  4296. if (os_strcmp(cmd, "ps") == 0)
  4297. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  4298. if (os_strcmp(cmd, "oppps") == 0)
  4299. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  4300. if (os_strcmp(cmd, "ctwindow") == 0)
  4301. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  4302. if (os_strcmp(cmd, "disabled") == 0) {
  4303. wpa_s->global->p2p_disabled = atoi(param);
  4304. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  4305. wpa_s->global->p2p_disabled ?
  4306. "disabled" : "enabled");
  4307. if (wpa_s->global->p2p_disabled) {
  4308. wpas_p2p_stop_find(wpa_s);
  4309. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4310. p2p_flush(wpa_s->global->p2p);
  4311. }
  4312. return 0;
  4313. }
  4314. if (os_strcmp(cmd, "conc_pref") == 0) {
  4315. if (os_strcmp(param, "sta") == 0)
  4316. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  4317. else if (os_strcmp(param, "p2p") == 0)
  4318. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  4319. else {
  4320. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  4321. return -1;
  4322. }
  4323. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  4324. "%s", param);
  4325. return 0;
  4326. }
  4327. if (os_strcmp(cmd, "force_long_sd") == 0) {
  4328. wpa_s->force_long_sd = atoi(param);
  4329. return 0;
  4330. }
  4331. if (os_strcmp(cmd, "peer_filter") == 0) {
  4332. u8 addr[ETH_ALEN];
  4333. if (hwaddr_aton(param, addr))
  4334. return -1;
  4335. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  4336. return 0;
  4337. }
  4338. if (os_strcmp(cmd, "cross_connect") == 0)
  4339. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  4340. if (os_strcmp(cmd, "go_apsd") == 0) {
  4341. if (os_strcmp(param, "disable") == 0)
  4342. wpa_s->set_ap_uapsd = 0;
  4343. else {
  4344. wpa_s->set_ap_uapsd = 1;
  4345. wpa_s->ap_uapsd = atoi(param);
  4346. }
  4347. return 0;
  4348. }
  4349. if (os_strcmp(cmd, "client_apsd") == 0) {
  4350. if (os_strcmp(param, "disable") == 0)
  4351. wpa_s->set_sta_uapsd = 0;
  4352. else {
  4353. int be, bk, vi, vo;
  4354. char *pos;
  4355. /* format: BE,BK,VI,VO;max SP Length */
  4356. be = atoi(param);
  4357. pos = os_strchr(param, ',');
  4358. if (pos == NULL)
  4359. return -1;
  4360. pos++;
  4361. bk = atoi(pos);
  4362. pos = os_strchr(pos, ',');
  4363. if (pos == NULL)
  4364. return -1;
  4365. pos++;
  4366. vi = atoi(pos);
  4367. pos = os_strchr(pos, ',');
  4368. if (pos == NULL)
  4369. return -1;
  4370. pos++;
  4371. vo = atoi(pos);
  4372. /* ignore max SP Length for now */
  4373. wpa_s->set_sta_uapsd = 1;
  4374. wpa_s->sta_uapsd = 0;
  4375. if (be)
  4376. wpa_s->sta_uapsd |= BIT(0);
  4377. if (bk)
  4378. wpa_s->sta_uapsd |= BIT(1);
  4379. if (vi)
  4380. wpa_s->sta_uapsd |= BIT(2);
  4381. if (vo)
  4382. wpa_s->sta_uapsd |= BIT(3);
  4383. }
  4384. return 0;
  4385. }
  4386. if (os_strcmp(cmd, "disallow_freq") == 0)
  4387. return p2p_ctrl_disallow_freq(wpa_s, param);
  4388. if (os_strcmp(cmd, "disc_int") == 0) {
  4389. int min_disc_int, max_disc_int, max_disc_tu;
  4390. char *pos;
  4391. pos = param;
  4392. min_disc_int = atoi(pos);
  4393. pos = os_strchr(pos, ' ');
  4394. if (pos == NULL)
  4395. return -1;
  4396. *pos++ = '\0';
  4397. max_disc_int = atoi(pos);
  4398. pos = os_strchr(pos, ' ');
  4399. if (pos == NULL)
  4400. return -1;
  4401. *pos++ = '\0';
  4402. max_disc_tu = atoi(pos);
  4403. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  4404. max_disc_int, max_disc_tu);
  4405. }
  4406. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  4407. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  4408. return 0;
  4409. }
  4410. #ifdef CONFIG_WPS_NFC
  4411. if (os_strcmp(cmd, "nfc_tag") == 0)
  4412. return wpas_p2p_nfc_tag_enabled(wpa_s, !!atoi(param));
  4413. #endif /* CONFIG_WPS_NFC */
  4414. if (os_strcmp(cmd, "disable_ip_addr_req") == 0) {
  4415. wpa_s->p2p_disable_ip_addr_req = !!atoi(param);
  4416. return 0;
  4417. }
  4418. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  4419. cmd);
  4420. return -1;
  4421. }
  4422. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  4423. {
  4424. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  4425. wpa_s->force_long_sd = 0;
  4426. wpas_p2p_stop_find(wpa_s);
  4427. if (wpa_s->global->p2p)
  4428. p2p_flush(wpa_s->global->p2p);
  4429. }
  4430. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  4431. {
  4432. char *pos, *pos2;
  4433. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  4434. if (cmd[0]) {
  4435. pos = os_strchr(cmd, ' ');
  4436. if (pos == NULL)
  4437. return -1;
  4438. *pos++ = '\0';
  4439. dur1 = atoi(cmd);
  4440. pos2 = os_strchr(pos, ' ');
  4441. if (pos2)
  4442. *pos2++ = '\0';
  4443. int1 = atoi(pos);
  4444. } else
  4445. pos2 = NULL;
  4446. if (pos2) {
  4447. pos = os_strchr(pos2, ' ');
  4448. if (pos == NULL)
  4449. return -1;
  4450. *pos++ = '\0';
  4451. dur2 = atoi(pos2);
  4452. int2 = atoi(pos);
  4453. }
  4454. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  4455. }
  4456. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  4457. {
  4458. char *pos;
  4459. unsigned int period = 0, interval = 0;
  4460. if (cmd[0]) {
  4461. pos = os_strchr(cmd, ' ');
  4462. if (pos == NULL)
  4463. return -1;
  4464. *pos++ = '\0';
  4465. period = atoi(cmd);
  4466. interval = atoi(pos);
  4467. }
  4468. return wpas_p2p_ext_listen(wpa_s, period, interval);
  4469. }
  4470. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  4471. {
  4472. const char *pos;
  4473. u8 peer[ETH_ALEN];
  4474. int iface_addr = 0;
  4475. pos = cmd;
  4476. if (os_strncmp(pos, "iface=", 6) == 0) {
  4477. iface_addr = 1;
  4478. pos += 6;
  4479. }
  4480. if (hwaddr_aton(pos, peer))
  4481. return -1;
  4482. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  4483. return 0;
  4484. }
  4485. #endif /* CONFIG_P2P */
  4486. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  4487. {
  4488. struct wpa_freq_range_list ranges;
  4489. int *freqs = NULL;
  4490. struct hostapd_hw_modes *mode;
  4491. u16 i;
  4492. if (wpa_s->hw.modes == NULL)
  4493. return NULL;
  4494. os_memset(&ranges, 0, sizeof(ranges));
  4495. if (freq_range_list_parse(&ranges, val) < 0)
  4496. return NULL;
  4497. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  4498. int j;
  4499. mode = &wpa_s->hw.modes[i];
  4500. for (j = 0; j < mode->num_channels; j++) {
  4501. unsigned int freq;
  4502. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  4503. continue;
  4504. freq = mode->channels[j].freq;
  4505. if (!freq_range_list_includes(&ranges, freq))
  4506. continue;
  4507. int_array_add_unique(&freqs, freq);
  4508. }
  4509. }
  4510. os_free(ranges.range);
  4511. return freqs;
  4512. }
  4513. #ifdef CONFIG_INTERWORKING
  4514. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  4515. {
  4516. int auto_sel = 0;
  4517. int *freqs = NULL;
  4518. if (param) {
  4519. char *pos;
  4520. auto_sel = os_strstr(param, "auto") != NULL;
  4521. pos = os_strstr(param, "freq=");
  4522. if (pos) {
  4523. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  4524. if (freqs == NULL)
  4525. return -1;
  4526. }
  4527. }
  4528. return interworking_select(wpa_s, auto_sel, freqs);
  4529. }
  4530. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  4531. {
  4532. u8 bssid[ETH_ALEN];
  4533. struct wpa_bss *bss;
  4534. if (hwaddr_aton(dst, bssid)) {
  4535. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  4536. return -1;
  4537. }
  4538. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4539. if (bss == NULL) {
  4540. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  4541. MAC2STR(bssid));
  4542. return -1;
  4543. }
  4544. return interworking_connect(wpa_s, bss);
  4545. }
  4546. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4547. {
  4548. u8 dst_addr[ETH_ALEN];
  4549. int used;
  4550. char *pos;
  4551. #define MAX_ANQP_INFO_ID 100
  4552. u16 id[MAX_ANQP_INFO_ID];
  4553. size_t num_id = 0;
  4554. u32 subtypes = 0;
  4555. used = hwaddr_aton2(dst, dst_addr);
  4556. if (used < 0)
  4557. return -1;
  4558. pos = dst + used;
  4559. if (*pos == ' ')
  4560. pos++;
  4561. while (num_id < MAX_ANQP_INFO_ID) {
  4562. if (os_strncmp(pos, "hs20:", 5) == 0) {
  4563. #ifdef CONFIG_HS20
  4564. int num = atoi(pos + 5);
  4565. if (num <= 0 || num > 31)
  4566. return -1;
  4567. subtypes |= BIT(num);
  4568. #else /* CONFIG_HS20 */
  4569. return -1;
  4570. #endif /* CONFIG_HS20 */
  4571. } else {
  4572. id[num_id] = atoi(pos);
  4573. if (id[num_id])
  4574. num_id++;
  4575. }
  4576. pos = os_strchr(pos + 1, ',');
  4577. if (pos == NULL)
  4578. break;
  4579. pos++;
  4580. }
  4581. if (num_id == 0)
  4582. return -1;
  4583. return anqp_send_req(wpa_s, dst_addr, id, num_id, subtypes);
  4584. }
  4585. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  4586. {
  4587. u8 dst_addr[ETH_ALEN];
  4588. struct wpabuf *advproto, *query = NULL;
  4589. int used, ret = -1;
  4590. char *pos, *end;
  4591. size_t len;
  4592. used = hwaddr_aton2(cmd, dst_addr);
  4593. if (used < 0)
  4594. return -1;
  4595. pos = cmd + used;
  4596. while (*pos == ' ')
  4597. pos++;
  4598. /* Advertisement Protocol ID */
  4599. end = os_strchr(pos, ' ');
  4600. if (end)
  4601. len = end - pos;
  4602. else
  4603. len = os_strlen(pos);
  4604. if (len & 0x01)
  4605. return -1;
  4606. len /= 2;
  4607. if (len == 0)
  4608. return -1;
  4609. advproto = wpabuf_alloc(len);
  4610. if (advproto == NULL)
  4611. return -1;
  4612. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4613. goto fail;
  4614. if (end) {
  4615. /* Optional Query Request */
  4616. pos = end + 1;
  4617. while (*pos == ' ')
  4618. pos++;
  4619. len = os_strlen(pos);
  4620. if (len) {
  4621. if (len & 0x01)
  4622. goto fail;
  4623. len /= 2;
  4624. if (len == 0)
  4625. goto fail;
  4626. query = wpabuf_alloc(len);
  4627. if (query == NULL)
  4628. goto fail;
  4629. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4630. goto fail;
  4631. }
  4632. }
  4633. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4634. fail:
  4635. wpabuf_free(advproto);
  4636. wpabuf_free(query);
  4637. return ret;
  4638. }
  4639. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4640. size_t buflen)
  4641. {
  4642. u8 addr[ETH_ALEN];
  4643. int dialog_token;
  4644. int used;
  4645. char *pos;
  4646. size_t resp_len, start, requested_len;
  4647. struct wpabuf *resp;
  4648. int ret;
  4649. used = hwaddr_aton2(cmd, addr);
  4650. if (used < 0)
  4651. return -1;
  4652. pos = cmd + used;
  4653. while (*pos == ' ')
  4654. pos++;
  4655. dialog_token = atoi(pos);
  4656. if (wpa_s->last_gas_resp &&
  4657. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  4658. dialog_token == wpa_s->last_gas_dialog_token)
  4659. resp = wpa_s->last_gas_resp;
  4660. else if (wpa_s->prev_gas_resp &&
  4661. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  4662. dialog_token == wpa_s->prev_gas_dialog_token)
  4663. resp = wpa_s->prev_gas_resp;
  4664. else
  4665. return -1;
  4666. resp_len = wpabuf_len(resp);
  4667. start = 0;
  4668. requested_len = resp_len;
  4669. pos = os_strchr(pos, ' ');
  4670. if (pos) {
  4671. start = atoi(pos);
  4672. if (start > resp_len)
  4673. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4674. pos = os_strchr(pos, ',');
  4675. if (pos == NULL)
  4676. return -1;
  4677. pos++;
  4678. requested_len = atoi(pos);
  4679. if (start + requested_len > resp_len)
  4680. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4681. }
  4682. if (requested_len * 2 + 1 > buflen)
  4683. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4684. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  4685. requested_len);
  4686. if (start + requested_len == resp_len) {
  4687. /*
  4688. * Free memory by dropping the response after it has been
  4689. * fetched.
  4690. */
  4691. if (resp == wpa_s->prev_gas_resp) {
  4692. wpabuf_free(wpa_s->prev_gas_resp);
  4693. wpa_s->prev_gas_resp = NULL;
  4694. } else {
  4695. wpabuf_free(wpa_s->last_gas_resp);
  4696. wpa_s->last_gas_resp = NULL;
  4697. }
  4698. }
  4699. return ret;
  4700. }
  4701. #endif /* CONFIG_INTERWORKING */
  4702. #ifdef CONFIG_HS20
  4703. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4704. {
  4705. u8 dst_addr[ETH_ALEN];
  4706. int used;
  4707. char *pos;
  4708. u32 subtypes = 0;
  4709. used = hwaddr_aton2(dst, dst_addr);
  4710. if (used < 0)
  4711. return -1;
  4712. pos = dst + used;
  4713. if (*pos == ' ')
  4714. pos++;
  4715. for (;;) {
  4716. int num = atoi(pos);
  4717. if (num <= 0 || num > 31)
  4718. return -1;
  4719. subtypes |= BIT(num);
  4720. pos = os_strchr(pos + 1, ',');
  4721. if (pos == NULL)
  4722. break;
  4723. pos++;
  4724. }
  4725. if (subtypes == 0)
  4726. return -1;
  4727. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4728. }
  4729. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4730. const u8 *addr, const char *realm)
  4731. {
  4732. u8 *buf;
  4733. size_t rlen, len;
  4734. int ret;
  4735. rlen = os_strlen(realm);
  4736. len = 3 + rlen;
  4737. buf = os_malloc(len);
  4738. if (buf == NULL)
  4739. return -1;
  4740. buf[0] = 1; /* NAI Home Realm Count */
  4741. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4742. buf[2] = rlen;
  4743. os_memcpy(buf + 3, realm, rlen);
  4744. ret = hs20_anqp_send_req(wpa_s, addr,
  4745. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4746. buf, len);
  4747. os_free(buf);
  4748. return ret;
  4749. }
  4750. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4751. char *dst)
  4752. {
  4753. struct wpa_cred *cred = wpa_s->conf->cred;
  4754. u8 dst_addr[ETH_ALEN];
  4755. int used;
  4756. u8 *buf;
  4757. size_t len;
  4758. int ret;
  4759. used = hwaddr_aton2(dst, dst_addr);
  4760. if (used < 0)
  4761. return -1;
  4762. while (dst[used] == ' ')
  4763. used++;
  4764. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4765. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4766. dst + used + 6);
  4767. len = os_strlen(dst + used);
  4768. if (len == 0 && cred && cred->realm)
  4769. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4770. if (len & 1)
  4771. return -1;
  4772. len /= 2;
  4773. buf = os_malloc(len);
  4774. if (buf == NULL)
  4775. return -1;
  4776. if (hexstr2bin(dst + used, buf, len) < 0) {
  4777. os_free(buf);
  4778. return -1;
  4779. }
  4780. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4781. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4782. buf, len);
  4783. os_free(buf);
  4784. return ret;
  4785. }
  4786. static int hs20_icon_request(struct wpa_supplicant *wpa_s, char *cmd)
  4787. {
  4788. u8 dst_addr[ETH_ALEN];
  4789. int used;
  4790. char *icon;
  4791. used = hwaddr_aton2(cmd, dst_addr);
  4792. if (used < 0)
  4793. return -1;
  4794. while (cmd[used] == ' ')
  4795. used++;
  4796. icon = &cmd[used];
  4797. wpa_s->fetch_osu_icon_in_progress = 0;
  4798. return hs20_anqp_send_req(wpa_s, dst_addr, BIT(HS20_STYPE_ICON_REQUEST),
  4799. (u8 *) icon, os_strlen(icon));
  4800. }
  4801. #endif /* CONFIG_HS20 */
  4802. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4803. struct wpa_supplicant *wpa_s, char *cmd)
  4804. {
  4805. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4806. return 0;
  4807. }
  4808. #ifdef CONFIG_AUTOSCAN
  4809. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4810. char *cmd)
  4811. {
  4812. enum wpa_states state = wpa_s->wpa_state;
  4813. char *new_params = NULL;
  4814. if (os_strlen(cmd) > 0) {
  4815. new_params = os_strdup(cmd);
  4816. if (new_params == NULL)
  4817. return -1;
  4818. }
  4819. os_free(wpa_s->conf->autoscan);
  4820. wpa_s->conf->autoscan = new_params;
  4821. if (wpa_s->conf->autoscan == NULL)
  4822. autoscan_deinit(wpa_s);
  4823. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4824. autoscan_init(wpa_s, 1);
  4825. else if (state == WPA_SCANNING)
  4826. wpa_supplicant_reinit_autoscan(wpa_s);
  4827. return 0;
  4828. }
  4829. #endif /* CONFIG_AUTOSCAN */
  4830. #ifdef CONFIG_WNM
  4831. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4832. {
  4833. int enter;
  4834. int intval = 0;
  4835. char *pos;
  4836. int ret;
  4837. struct wpabuf *tfs_req = NULL;
  4838. if (os_strncmp(cmd, "enter", 5) == 0)
  4839. enter = 1;
  4840. else if (os_strncmp(cmd, "exit", 4) == 0)
  4841. enter = 0;
  4842. else
  4843. return -1;
  4844. pos = os_strstr(cmd, " interval=");
  4845. if (pos)
  4846. intval = atoi(pos + 10);
  4847. pos = os_strstr(cmd, " tfs_req=");
  4848. if (pos) {
  4849. char *end;
  4850. size_t len;
  4851. pos += 9;
  4852. end = os_strchr(pos, ' ');
  4853. if (end)
  4854. len = end - pos;
  4855. else
  4856. len = os_strlen(pos);
  4857. if (len & 1)
  4858. return -1;
  4859. len /= 2;
  4860. tfs_req = wpabuf_alloc(len);
  4861. if (tfs_req == NULL)
  4862. return -1;
  4863. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4864. wpabuf_free(tfs_req);
  4865. return -1;
  4866. }
  4867. }
  4868. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4869. WNM_SLEEP_MODE_EXIT, intval,
  4870. tfs_req);
  4871. wpabuf_free(tfs_req);
  4872. return ret;
  4873. }
  4874. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4875. {
  4876. int query_reason;
  4877. query_reason = atoi(cmd);
  4878. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4879. query_reason);
  4880. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4881. }
  4882. #endif /* CONFIG_WNM */
  4883. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4884. size_t buflen)
  4885. {
  4886. struct wpa_signal_info si;
  4887. int ret;
  4888. char *pos, *end;
  4889. ret = wpa_drv_signal_poll(wpa_s, &si);
  4890. if (ret)
  4891. return -1;
  4892. pos = buf;
  4893. end = buf + buflen;
  4894. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4895. "NOISE=%d\nFREQUENCY=%u\n",
  4896. si.current_signal, si.current_txrate / 1000,
  4897. si.current_noise, si.frequency);
  4898. if (os_snprintf_error(end - pos, ret))
  4899. return -1;
  4900. pos += ret;
  4901. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4902. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4903. channel_width_to_string(si.chanwidth));
  4904. if (os_snprintf_error(end - pos, ret))
  4905. return -1;
  4906. pos += ret;
  4907. }
  4908. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4909. ret = os_snprintf(pos, end - pos,
  4910. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4911. si.center_frq1, si.center_frq2);
  4912. if (os_snprintf_error(end - pos, ret))
  4913. return -1;
  4914. pos += ret;
  4915. }
  4916. if (si.avg_signal) {
  4917. ret = os_snprintf(pos, end - pos,
  4918. "AVG_RSSI=%d\n", si.avg_signal);
  4919. if (os_snprintf_error(end - pos, ret))
  4920. return -1;
  4921. pos += ret;
  4922. }
  4923. return pos - buf;
  4924. }
  4925. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4926. size_t buflen)
  4927. {
  4928. struct hostap_sta_driver_data sta;
  4929. int ret;
  4930. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4931. if (ret)
  4932. return -1;
  4933. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4934. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4935. if (os_snprintf_error(buflen, ret))
  4936. return -1;
  4937. return ret;
  4938. }
  4939. #ifdef ANDROID
  4940. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4941. char *buf, size_t buflen)
  4942. {
  4943. int ret;
  4944. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  4945. if (ret == 0) {
  4946. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  4947. struct p2p_data *p2p = wpa_s->global->p2p;
  4948. if (p2p) {
  4949. char country[3];
  4950. country[0] = cmd[8];
  4951. country[1] = cmd[9];
  4952. country[2] = 0x04;
  4953. p2p_set_country(p2p, country);
  4954. }
  4955. }
  4956. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  4957. if (os_snprintf_error(buflen, ret))
  4958. ret = -1;
  4959. }
  4960. return ret;
  4961. }
  4962. #endif /* ANDROID */
  4963. static int wpa_supplicant_vendor_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4964. char *buf, size_t buflen)
  4965. {
  4966. int ret;
  4967. char *pos;
  4968. u8 *data = NULL;
  4969. unsigned int vendor_id, subcmd;
  4970. struct wpabuf *reply;
  4971. size_t data_len = 0;
  4972. /* cmd: <vendor id> <subcommand id> [<hex formatted data>] */
  4973. vendor_id = strtoul(cmd, &pos, 16);
  4974. if (!isblank(*pos))
  4975. return -EINVAL;
  4976. subcmd = strtoul(pos, &pos, 10);
  4977. if (*pos != '\0') {
  4978. if (!isblank(*pos++))
  4979. return -EINVAL;
  4980. data_len = os_strlen(pos);
  4981. }
  4982. if (data_len) {
  4983. data_len /= 2;
  4984. data = os_malloc(data_len);
  4985. if (!data)
  4986. return -1;
  4987. if (hexstr2bin(pos, data, data_len)) {
  4988. wpa_printf(MSG_DEBUG,
  4989. "Vendor command: wrong parameter format");
  4990. os_free(data);
  4991. return -EINVAL;
  4992. }
  4993. }
  4994. reply = wpabuf_alloc((buflen - 1) / 2);
  4995. if (!reply) {
  4996. os_free(data);
  4997. return -1;
  4998. }
  4999. ret = wpa_drv_vendor_cmd(wpa_s, vendor_id, subcmd, data, data_len,
  5000. reply);
  5001. if (ret == 0)
  5002. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(reply),
  5003. wpabuf_len(reply));
  5004. wpabuf_free(reply);
  5005. os_free(data);
  5006. return ret;
  5007. }
  5008. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  5009. {
  5010. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  5011. #ifdef CONFIG_P2P
  5012. wpas_p2p_cancel(wpa_s);
  5013. wpas_p2p_stop_find(wpa_s);
  5014. p2p_ctrl_flush(wpa_s);
  5015. wpas_p2p_group_remove(wpa_s, "*");
  5016. wpas_p2p_service_flush(wpa_s);
  5017. wpa_s->global->p2p_disabled = 0;
  5018. wpa_s->global->p2p_per_sta_psk = 0;
  5019. wpa_s->conf->num_sec_device_types = 0;
  5020. wpa_s->p2p_disable_ip_addr_req = 0;
  5021. os_free(wpa_s->global->p2p_go_avoid_freq.range);
  5022. wpa_s->global->p2p_go_avoid_freq.range = NULL;
  5023. #endif /* CONFIG_P2P */
  5024. #ifdef CONFIG_WPS_TESTING
  5025. wps_version_number = 0x20;
  5026. wps_testing_dummy_cred = 0;
  5027. wps_corrupt_pkhash = 0;
  5028. #endif /* CONFIG_WPS_TESTING */
  5029. #ifdef CONFIG_WPS
  5030. wpa_s->wps_fragment_size = 0;
  5031. wpas_wps_cancel(wpa_s);
  5032. #endif /* CONFIG_WPS */
  5033. wpa_s->after_wps = 0;
  5034. wpa_s->known_wps_freq = 0;
  5035. #ifdef CONFIG_TDLS
  5036. #ifdef CONFIG_TDLS_TESTING
  5037. extern unsigned int tdls_testing;
  5038. tdls_testing = 0;
  5039. #endif /* CONFIG_TDLS_TESTING */
  5040. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  5041. wpa_tdls_enable(wpa_s->wpa, 1);
  5042. #endif /* CONFIG_TDLS */
  5043. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  5044. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  5045. wpa_s->no_keep_alive = 0;
  5046. os_free(wpa_s->disallow_aps_bssid);
  5047. wpa_s->disallow_aps_bssid = NULL;
  5048. wpa_s->disallow_aps_bssid_count = 0;
  5049. os_free(wpa_s->disallow_aps_ssid);
  5050. wpa_s->disallow_aps_ssid = NULL;
  5051. wpa_s->disallow_aps_ssid_count = 0;
  5052. wpa_s->set_sta_uapsd = 0;
  5053. wpa_s->sta_uapsd = 0;
  5054. wpa_drv_radio_disable(wpa_s, 0);
  5055. wpa_bss_flush(wpa_s);
  5056. wpa_blacklist_clear(wpa_s);
  5057. wpa_s->extra_blacklist_count = 0;
  5058. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  5059. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  5060. wpa_config_flush_blobs(wpa_s->conf);
  5061. wpa_s->conf->auto_interworking = 0;
  5062. wpa_s->conf->okc = 0;
  5063. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  5064. rsn_preauth_deinit(wpa_s->wpa);
  5065. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  5066. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  5067. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  5068. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  5069. radio_remove_works(wpa_s, NULL, 1);
  5070. wpa_s->ext_work_in_progress = 0;
  5071. wpa_s->next_ssid = NULL;
  5072. #ifdef CONFIG_INTERWORKING
  5073. hs20_cancel_fetch_osu(wpa_s);
  5074. #endif /* CONFIG_INTERWORKING */
  5075. wpa_s->ext_mgmt_frame_handling = 0;
  5076. wpa_s->ext_eapol_frame_io = 0;
  5077. #ifdef CONFIG_TESTING_OPTIONS
  5078. wpa_s->extra_roc_dur = 0;
  5079. #endif /* CONFIG_TESTING_OPTIONS */
  5080. wpa_s->disconnected = 0;
  5081. }
  5082. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  5083. char *buf, size_t buflen)
  5084. {
  5085. struct wpa_radio_work *work;
  5086. char *pos, *end;
  5087. struct os_reltime now, diff;
  5088. pos = buf;
  5089. end = buf + buflen;
  5090. os_get_reltime(&now);
  5091. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  5092. {
  5093. int ret;
  5094. os_reltime_sub(&now, &work->time, &diff);
  5095. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  5096. work->type, work->wpa_s->ifname, work->freq,
  5097. work->started, diff.sec, diff.usec);
  5098. if (os_snprintf_error(end - pos, ret))
  5099. break;
  5100. pos += ret;
  5101. }
  5102. return pos - buf;
  5103. }
  5104. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  5105. {
  5106. struct wpa_radio_work *work = eloop_ctx;
  5107. struct wpa_external_work *ework = work->ctx;
  5108. wpa_dbg(work->wpa_s, MSG_DEBUG,
  5109. "Timing out external radio work %u (%s)",
  5110. ework->id, work->type);
  5111. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  5112. work->wpa_s->ext_work_in_progress = 0;
  5113. radio_work_done(work);
  5114. os_free(ework);
  5115. }
  5116. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  5117. {
  5118. struct wpa_external_work *ework = work->ctx;
  5119. if (deinit) {
  5120. if (work->started)
  5121. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  5122. work, NULL);
  5123. os_free(ework);
  5124. return;
  5125. }
  5126. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  5127. ework->id, ework->type);
  5128. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  5129. work->wpa_s->ext_work_in_progress = 1;
  5130. if (!ework->timeout)
  5131. ework->timeout = 10;
  5132. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  5133. work, NULL);
  5134. }
  5135. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  5136. char *buf, size_t buflen)
  5137. {
  5138. struct wpa_external_work *ework;
  5139. char *pos, *pos2;
  5140. size_t type_len;
  5141. int ret;
  5142. unsigned int freq = 0;
  5143. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  5144. ework = os_zalloc(sizeof(*ework));
  5145. if (ework == NULL)
  5146. return -1;
  5147. pos = os_strchr(cmd, ' ');
  5148. if (pos) {
  5149. type_len = pos - cmd;
  5150. pos++;
  5151. pos2 = os_strstr(pos, "freq=");
  5152. if (pos2)
  5153. freq = atoi(pos2 + 5);
  5154. pos2 = os_strstr(pos, "timeout=");
  5155. if (pos2)
  5156. ework->timeout = atoi(pos2 + 8);
  5157. } else {
  5158. type_len = os_strlen(cmd);
  5159. }
  5160. if (4 + type_len >= sizeof(ework->type))
  5161. type_len = sizeof(ework->type) - 4 - 1;
  5162. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  5163. os_memcpy(ework->type + 4, cmd, type_len);
  5164. ework->type[4 + type_len] = '\0';
  5165. wpa_s->ext_work_id++;
  5166. if (wpa_s->ext_work_id == 0)
  5167. wpa_s->ext_work_id++;
  5168. ework->id = wpa_s->ext_work_id;
  5169. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  5170. ework) < 0) {
  5171. os_free(ework);
  5172. return -1;
  5173. }
  5174. ret = os_snprintf(buf, buflen, "%u", ework->id);
  5175. if (os_snprintf_error(buflen, ret))
  5176. return -1;
  5177. return ret;
  5178. }
  5179. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  5180. {
  5181. struct wpa_radio_work *work;
  5182. unsigned int id = atoi(cmd);
  5183. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  5184. {
  5185. struct wpa_external_work *ework;
  5186. if (os_strncmp(work->type, "ext:", 4) != 0)
  5187. continue;
  5188. ework = work->ctx;
  5189. if (id && ework->id != id)
  5190. continue;
  5191. wpa_dbg(wpa_s, MSG_DEBUG,
  5192. "Completed external radio work %u (%s)",
  5193. ework->id, ework->type);
  5194. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  5195. wpa_s->ext_work_in_progress = 0;
  5196. radio_work_done(work);
  5197. os_free(ework);
  5198. return 3; /* "OK\n" */
  5199. }
  5200. return -1;
  5201. }
  5202. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  5203. char *buf, size_t buflen)
  5204. {
  5205. if (os_strcmp(cmd, "show") == 0)
  5206. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  5207. if (os_strncmp(cmd, "add ", 4) == 0)
  5208. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  5209. if (os_strncmp(cmd, "done ", 5) == 0)
  5210. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  5211. return -1;
  5212. }
  5213. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  5214. {
  5215. struct wpa_radio_work *work, *tmp;
  5216. if (!wpa_s || !wpa_s->radio)
  5217. return;
  5218. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  5219. struct wpa_radio_work, list) {
  5220. struct wpa_external_work *ework;
  5221. if (os_strncmp(work->type, "ext:", 4) != 0)
  5222. continue;
  5223. ework = work->ctx;
  5224. wpa_dbg(wpa_s, MSG_DEBUG,
  5225. "Flushing%s external radio work %u (%s)",
  5226. work->started ? " started" : "", ework->id,
  5227. ework->type);
  5228. if (work->started)
  5229. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  5230. work, NULL);
  5231. radio_work_done(work);
  5232. os_free(ework);
  5233. }
  5234. }
  5235. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  5236. {
  5237. struct wpa_supplicant *wpa_s = eloop_ctx;
  5238. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  5239. }
  5240. static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
  5241. {
  5242. int *freqs = NULL;
  5243. freqs = freq_range_to_channel_list(wpa_s, val);
  5244. if (freqs == NULL)
  5245. return -1;
  5246. os_free(wpa_s->manual_scan_freqs);
  5247. wpa_s->manual_scan_freqs = freqs;
  5248. return 0;
  5249. }
  5250. static int scan_id_list_parse(struct wpa_supplicant *wpa_s, const char *value)
  5251. {
  5252. const char *pos = value;
  5253. while (pos) {
  5254. if (*pos == ' ' || *pos == '\0')
  5255. break;
  5256. if (wpa_s->scan_id_count == MAX_SCAN_ID)
  5257. return -1;
  5258. wpa_s->scan_id[wpa_s->scan_id_count++] = atoi(pos);
  5259. pos = os_strchr(pos, ',');
  5260. if (pos)
  5261. pos++;
  5262. }
  5263. return 0;
  5264. }
  5265. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  5266. char *reply, int reply_size, int *reply_len)
  5267. {
  5268. char *pos;
  5269. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  5270. *reply_len = -1;
  5271. return;
  5272. }
  5273. wpa_s->manual_scan_passive = 0;
  5274. wpa_s->manual_scan_use_id = 0;
  5275. wpa_s->manual_scan_only_new = 0;
  5276. wpa_s->scan_id_count = 0;
  5277. if (params) {
  5278. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  5279. wpa_s->scan_res_handler = scan_only_handler;
  5280. pos = os_strstr(params, "freq=");
  5281. if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
  5282. *reply_len = -1;
  5283. return;
  5284. }
  5285. pos = os_strstr(params, "passive=");
  5286. if (pos)
  5287. wpa_s->manual_scan_passive = !!atoi(pos + 8);
  5288. pos = os_strstr(params, "use_id=");
  5289. if (pos)
  5290. wpa_s->manual_scan_use_id = atoi(pos + 7);
  5291. pos = os_strstr(params, "only_new=1");
  5292. if (pos)
  5293. wpa_s->manual_scan_only_new = 1;
  5294. pos = os_strstr(params, "scan_id=");
  5295. if (pos && scan_id_list_parse(wpa_s, pos + 8) < 0) {
  5296. *reply_len = -1;
  5297. return;
  5298. }
  5299. } else {
  5300. os_free(wpa_s->manual_scan_freqs);
  5301. wpa_s->manual_scan_freqs = NULL;
  5302. if (wpa_s->scan_res_handler == scan_only_handler)
  5303. wpa_s->scan_res_handler = NULL;
  5304. }
  5305. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  5306. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  5307. (wpa_s->wpa_state == WPA_COMPLETED))) {
  5308. wpa_s->normal_scans = 0;
  5309. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5310. wpa_s->after_wps = 0;
  5311. wpa_s->known_wps_freq = 0;
  5312. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5313. if (wpa_s->manual_scan_use_id) {
  5314. wpa_s->manual_scan_id++;
  5315. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5316. wpa_s->manual_scan_id);
  5317. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5318. wpa_s->manual_scan_id);
  5319. }
  5320. } else if (wpa_s->sched_scanning) {
  5321. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  5322. wpa_supplicant_cancel_sched_scan(wpa_s);
  5323. wpa_s->scan_req = MANUAL_SCAN_REQ;
  5324. wpa_supplicant_req_scan(wpa_s, 0, 0);
  5325. if (wpa_s->manual_scan_use_id) {
  5326. wpa_s->manual_scan_id++;
  5327. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  5328. wpa_s->manual_scan_id);
  5329. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  5330. wpa_s->manual_scan_id);
  5331. }
  5332. } else {
  5333. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  5334. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  5335. }
  5336. }
  5337. #ifdef CONFIG_TESTING_OPTIONS
  5338. static void wpas_ctrl_iface_mgmt_tx_cb(struct wpa_supplicant *wpa_s,
  5339. unsigned int freq, const u8 *dst,
  5340. const u8 *src, const u8 *bssid,
  5341. const u8 *data, size_t data_len,
  5342. enum offchannel_send_action_result
  5343. result)
  5344. {
  5345. wpa_msg(wpa_s, MSG_INFO, "MGMT-TX-STATUS freq=%u dst=" MACSTR
  5346. " src=" MACSTR " bssid=" MACSTR " result=%s",
  5347. freq, MAC2STR(dst), MAC2STR(src), MAC2STR(bssid),
  5348. result == OFFCHANNEL_SEND_ACTION_SUCCESS ?
  5349. "SUCCESS" : (result == OFFCHANNEL_SEND_ACTION_NO_ACK ?
  5350. "NO_ACK" : "FAILED"));
  5351. }
  5352. static int wpas_ctrl_iface_mgmt_tx(struct wpa_supplicant *wpa_s, char *cmd)
  5353. {
  5354. char *pos, *param;
  5355. size_t len;
  5356. u8 *buf, da[ETH_ALEN], bssid[ETH_ALEN];
  5357. int res, used;
  5358. int freq = 0, no_cck = 0, wait_time = 0;
  5359. /* <DA> <BSSID> [freq=<MHz>] [wait_time=<ms>] [no_cck=1]
  5360. * <action=Action frame payload> */
  5361. wpa_printf(MSG_DEBUG, "External MGMT TX: %s", cmd);
  5362. pos = cmd;
  5363. used = hwaddr_aton2(pos, da);
  5364. if (used < 0)
  5365. return -1;
  5366. pos += used;
  5367. while (*pos == ' ')
  5368. pos++;
  5369. used = hwaddr_aton2(pos, bssid);
  5370. if (used < 0)
  5371. return -1;
  5372. pos += used;
  5373. param = os_strstr(pos, " freq=");
  5374. if (param) {
  5375. param += 6;
  5376. freq = atoi(param);
  5377. }
  5378. param = os_strstr(pos, " no_cck=");
  5379. if (param) {
  5380. param += 8;
  5381. no_cck = atoi(param);
  5382. }
  5383. param = os_strstr(pos, " wait_time=");
  5384. if (param) {
  5385. param += 11;
  5386. wait_time = atoi(param);
  5387. }
  5388. param = os_strstr(pos, " action=");
  5389. if (param == NULL)
  5390. return -1;
  5391. param += 8;
  5392. len = os_strlen(param);
  5393. if (len & 1)
  5394. return -1;
  5395. len /= 2;
  5396. buf = os_malloc(len);
  5397. if (buf == NULL)
  5398. return -1;
  5399. if (hexstr2bin(param, buf, len) < 0) {
  5400. os_free(buf);
  5401. return -1;
  5402. }
  5403. res = offchannel_send_action(wpa_s, freq, da, wpa_s->own_addr, bssid,
  5404. buf, len, wait_time,
  5405. wpas_ctrl_iface_mgmt_tx_cb, no_cck);
  5406. os_free(buf);
  5407. return res;
  5408. }
  5409. static void wpas_ctrl_iface_mgmt_tx_done(struct wpa_supplicant *wpa_s)
  5410. {
  5411. wpa_printf(MSG_DEBUG, "External MGMT TX - done waiting");
  5412. offchannel_send_action_done(wpa_s);
  5413. }
  5414. static int wpas_ctrl_iface_driver_event(struct wpa_supplicant *wpa_s, char *cmd)
  5415. {
  5416. char *pos, *param;
  5417. union wpa_event_data event;
  5418. enum wpa_event_type ev;
  5419. /* <event name> [parameters..] */
  5420. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - external driver event: %s", cmd);
  5421. pos = cmd;
  5422. param = os_strchr(pos, ' ');
  5423. if (param)
  5424. *param++ = '\0';
  5425. os_memset(&event, 0, sizeof(event));
  5426. if (os_strcmp(cmd, "INTERFACE_ENABLED") == 0) {
  5427. ev = EVENT_INTERFACE_ENABLED;
  5428. } else if (os_strcmp(cmd, "INTERFACE_DISABLED") == 0) {
  5429. ev = EVENT_INTERFACE_DISABLED;
  5430. } else if (os_strcmp(cmd, "AVOID_FREQUENCIES") == 0) {
  5431. ev = EVENT_AVOID_FREQUENCIES;
  5432. if (param == NULL)
  5433. param = "";
  5434. if (freq_range_list_parse(&event.freq_range, param) < 0)
  5435. return -1;
  5436. wpa_supplicant_event(wpa_s, ev, &event);
  5437. os_free(event.freq_range.range);
  5438. return 0;
  5439. } else {
  5440. wpa_dbg(wpa_s, MSG_DEBUG, "Testing - unknown driver event: %s",
  5441. cmd);
  5442. return -1;
  5443. }
  5444. wpa_supplicant_event(wpa_s, ev, &event);
  5445. return 0;
  5446. }
  5447. static int wpas_ctrl_iface_eapol_rx(struct wpa_supplicant *wpa_s, char *cmd)
  5448. {
  5449. char *pos;
  5450. u8 src[ETH_ALEN], *buf;
  5451. int used;
  5452. size_t len;
  5453. wpa_printf(MSG_DEBUG, "External EAPOL RX: %s", cmd);
  5454. pos = cmd;
  5455. used = hwaddr_aton2(pos, src);
  5456. if (used < 0)
  5457. return -1;
  5458. pos += used;
  5459. while (*pos == ' ')
  5460. pos++;
  5461. len = os_strlen(pos);
  5462. if (len & 1)
  5463. return -1;
  5464. len /= 2;
  5465. buf = os_malloc(len);
  5466. if (buf == NULL)
  5467. return -1;
  5468. if (hexstr2bin(pos, buf, len) < 0) {
  5469. os_free(buf);
  5470. return -1;
  5471. }
  5472. wpa_supplicant_rx_eapol(wpa_s, src, buf, len);
  5473. os_free(buf);
  5474. return 0;
  5475. }
  5476. static u16 ipv4_hdr_checksum(const void *buf, size_t len)
  5477. {
  5478. size_t i;
  5479. u32 sum = 0;
  5480. const u16 *pos = buf;
  5481. for (i = 0; i < len / 2; i++)
  5482. sum += *pos++;
  5483. while (sum >> 16)
  5484. sum = (sum & 0xffff) + (sum >> 16);
  5485. return sum ^ 0xffff;
  5486. }
  5487. #define HWSIM_PACKETLEN 1500
  5488. #define HWSIM_IP_LEN (HWSIM_PACKETLEN - sizeof(struct ether_header))
  5489. void wpas_data_test_rx(void *ctx, const u8 *src_addr, const u8 *buf, size_t len)
  5490. {
  5491. struct wpa_supplicant *wpa_s = ctx;
  5492. const struct ether_header *eth;
  5493. const struct iphdr *ip;
  5494. const u8 *pos;
  5495. unsigned int i;
  5496. if (len != HWSIM_PACKETLEN)
  5497. return;
  5498. eth = (const struct ether_header *) buf;
  5499. ip = (const struct iphdr *) (eth + 1);
  5500. pos = (const u8 *) (ip + 1);
  5501. if (ip->ihl != 5 || ip->version != 4 ||
  5502. ntohs(ip->tot_len) != HWSIM_IP_LEN)
  5503. return;
  5504. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++) {
  5505. if (*pos != (u8) i)
  5506. return;
  5507. pos++;
  5508. }
  5509. wpa_msg(wpa_s, MSG_INFO, "DATA-TEST-RX " MACSTR " " MACSTR,
  5510. MAC2STR(eth->ether_dhost), MAC2STR(eth->ether_shost));
  5511. }
  5512. static int wpas_ctrl_iface_data_test_config(struct wpa_supplicant *wpa_s,
  5513. char *cmd)
  5514. {
  5515. int enabled = atoi(cmd);
  5516. if (!enabled) {
  5517. if (wpa_s->l2_test) {
  5518. l2_packet_deinit(wpa_s->l2_test);
  5519. wpa_s->l2_test = NULL;
  5520. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Disabled");
  5521. }
  5522. return 0;
  5523. }
  5524. if (wpa_s->l2_test)
  5525. return 0;
  5526. wpa_s->l2_test = l2_packet_init(wpa_s->ifname, wpa_s->own_addr,
  5527. ETHERTYPE_IP, wpas_data_test_rx,
  5528. wpa_s, 1);
  5529. if (wpa_s->l2_test == NULL)
  5530. return -1;
  5531. wpa_dbg(wpa_s, MSG_DEBUG, "test data: Enabled");
  5532. return 0;
  5533. }
  5534. static int wpas_ctrl_iface_data_test_tx(struct wpa_supplicant *wpa_s, char *cmd)
  5535. {
  5536. u8 dst[ETH_ALEN], src[ETH_ALEN];
  5537. char *pos;
  5538. int used;
  5539. long int val;
  5540. u8 tos;
  5541. u8 buf[HWSIM_PACKETLEN];
  5542. struct ether_header *eth;
  5543. struct iphdr *ip;
  5544. u8 *dpos;
  5545. unsigned int i;
  5546. if (wpa_s->l2_test == NULL)
  5547. return -1;
  5548. /* format: <dst> <src> <tos> */
  5549. pos = cmd;
  5550. used = hwaddr_aton2(pos, dst);
  5551. if (used < 0)
  5552. return -1;
  5553. pos += used;
  5554. while (*pos == ' ')
  5555. pos++;
  5556. used = hwaddr_aton2(pos, src);
  5557. if (used < 0)
  5558. return -1;
  5559. pos += used;
  5560. val = strtol(pos, NULL, 0);
  5561. if (val < 0 || val > 0xff)
  5562. return -1;
  5563. tos = val;
  5564. eth = (struct ether_header *) buf;
  5565. os_memcpy(eth->ether_dhost, dst, ETH_ALEN);
  5566. os_memcpy(eth->ether_shost, src, ETH_ALEN);
  5567. eth->ether_type = htons(ETHERTYPE_IP);
  5568. ip = (struct iphdr *) (eth + 1);
  5569. os_memset(ip, 0, sizeof(*ip));
  5570. ip->ihl = 5;
  5571. ip->version = 4;
  5572. ip->ttl = 64;
  5573. ip->tos = tos;
  5574. ip->tot_len = htons(HWSIM_IP_LEN);
  5575. ip->protocol = 1;
  5576. ip->saddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 1);
  5577. ip->daddr = htonl(192 << 24 | 168 << 16 | 1 << 8 | 2);
  5578. ip->check = ipv4_hdr_checksum(ip, sizeof(*ip));
  5579. dpos = (u8 *) (ip + 1);
  5580. for (i = 0; i < HWSIM_IP_LEN - sizeof(*ip); i++)
  5581. *dpos++ = i;
  5582. if (l2_packet_send(wpa_s->l2_test, dst, ETHERTYPE_IP, buf,
  5583. HWSIM_PACKETLEN) < 0)
  5584. return -1;
  5585. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX dst=" MACSTR " src=" MACSTR
  5586. " tos=0x%x", MAC2STR(dst), MAC2STR(src), tos);
  5587. return 0;
  5588. }
  5589. static int wpas_ctrl_iface_data_test_frame(struct wpa_supplicant *wpa_s,
  5590. char *cmd)
  5591. {
  5592. u8 *buf;
  5593. struct ether_header *eth;
  5594. struct l2_packet_data *l2 = NULL;
  5595. size_t len;
  5596. u16 ethertype;
  5597. int res = -1;
  5598. len = os_strlen(cmd);
  5599. if (len & 1 || len < ETH_HLEN * 2)
  5600. return -1;
  5601. len /= 2;
  5602. buf = os_malloc(len);
  5603. if (buf == NULL)
  5604. return -1;
  5605. if (hexstr2bin(cmd, buf, len) < 0)
  5606. goto done;
  5607. eth = (struct ether_header *) buf;
  5608. ethertype = ntohs(eth->ether_type);
  5609. l2 = l2_packet_init(wpa_s->ifname, wpa_s->own_addr, ethertype,
  5610. wpas_data_test_rx, wpa_s, 1);
  5611. if (l2 == NULL)
  5612. goto done;
  5613. res = l2_packet_send(l2, eth->ether_dhost, ethertype, buf, len);
  5614. wpa_dbg(wpa_s, MSG_DEBUG, "test data: TX frame res=%d", res);
  5615. done:
  5616. if (l2)
  5617. l2_packet_deinit(l2);
  5618. os_free(buf);
  5619. return res < 0 ? -1 : 0;
  5620. }
  5621. #endif /* CONFIG_TESTING_OPTIONS */
  5622. static void wpas_ctrl_vendor_elem_update(struct wpa_supplicant *wpa_s)
  5623. {
  5624. unsigned int i;
  5625. char buf[30];
  5626. wpa_printf(MSG_DEBUG, "Update vendor elements");
  5627. for (i = 0; i < NUM_VENDOR_ELEM_FRAMES; i++) {
  5628. if (wpa_s->vendor_elem[i]) {
  5629. int res;
  5630. res = os_snprintf(buf, sizeof(buf), "frame[%u]", i);
  5631. if (!os_snprintf_error(sizeof(buf), res)) {
  5632. wpa_hexdump_buf(MSG_DEBUG, buf,
  5633. wpa_s->vendor_elem[i]);
  5634. }
  5635. }
  5636. }
  5637. #ifdef CONFIG_P2P
  5638. if (wpa_s->parent == wpa_s &&
  5639. wpa_s->global->p2p &&
  5640. !wpa_s->global->p2p_disabled)
  5641. p2p_set_vendor_elems(wpa_s->global->p2p, wpa_s->vendor_elem);
  5642. #endif /* CONFIG_P2P */
  5643. }
  5644. static struct wpa_supplicant *
  5645. wpas_ctrl_vendor_elem_iface(struct wpa_supplicant *wpa_s,
  5646. enum wpa_vendor_elem_frame frame)
  5647. {
  5648. switch (frame) {
  5649. #ifdef CONFIG_P2P
  5650. case VENDOR_ELEM_PROBE_REQ_P2P:
  5651. case VENDOR_ELEM_PROBE_RESP_P2P:
  5652. case VENDOR_ELEM_PROBE_RESP_P2P_GO:
  5653. case VENDOR_ELEM_BEACON_P2P_GO:
  5654. case VENDOR_ELEM_P2P_PD_REQ:
  5655. case VENDOR_ELEM_P2P_PD_RESP:
  5656. case VENDOR_ELEM_P2P_GO_NEG_REQ:
  5657. case VENDOR_ELEM_P2P_GO_NEG_RESP:
  5658. case VENDOR_ELEM_P2P_GO_NEG_CONF:
  5659. case VENDOR_ELEM_P2P_INV_REQ:
  5660. case VENDOR_ELEM_P2P_INV_RESP:
  5661. case VENDOR_ELEM_P2P_ASSOC_REQ:
  5662. return wpa_s->parent;
  5663. #endif /* CONFIG_P2P */
  5664. default:
  5665. return wpa_s;
  5666. }
  5667. }
  5668. static int wpas_ctrl_vendor_elem_add(struct wpa_supplicant *wpa_s, char *cmd)
  5669. {
  5670. char *pos = cmd;
  5671. int frame;
  5672. size_t len;
  5673. struct wpabuf *buf;
  5674. struct ieee802_11_elems elems;
  5675. frame = atoi(pos);
  5676. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5677. return -1;
  5678. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5679. pos = os_strchr(pos, ' ');
  5680. if (pos == NULL)
  5681. return -1;
  5682. pos++;
  5683. len = os_strlen(pos);
  5684. if (len == 0)
  5685. return 0;
  5686. if (len & 1)
  5687. return -1;
  5688. len /= 2;
  5689. buf = wpabuf_alloc(len);
  5690. if (buf == NULL)
  5691. return -1;
  5692. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  5693. wpabuf_free(buf);
  5694. return -1;
  5695. }
  5696. if (ieee802_11_parse_elems(wpabuf_head_u8(buf), len, &elems, 0) ==
  5697. ParseFailed) {
  5698. wpabuf_free(buf);
  5699. return -1;
  5700. }
  5701. if (wpa_s->vendor_elem[frame] == NULL) {
  5702. wpa_s->vendor_elem[frame] = buf;
  5703. wpas_ctrl_vendor_elem_update(wpa_s);
  5704. return 0;
  5705. }
  5706. if (wpabuf_resize(&wpa_s->vendor_elem[frame], len) < 0) {
  5707. wpabuf_free(buf);
  5708. return -1;
  5709. }
  5710. wpabuf_put_buf(wpa_s->vendor_elem[frame], buf);
  5711. wpabuf_free(buf);
  5712. wpas_ctrl_vendor_elem_update(wpa_s);
  5713. return 0;
  5714. }
  5715. static int wpas_ctrl_vendor_elem_get(struct wpa_supplicant *wpa_s, char *cmd,
  5716. char *buf, size_t buflen)
  5717. {
  5718. int frame = atoi(cmd);
  5719. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5720. return -1;
  5721. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5722. if (wpa_s->vendor_elem[frame] == NULL)
  5723. return 0;
  5724. return wpa_snprintf_hex(buf, buflen,
  5725. wpabuf_head_u8(wpa_s->vendor_elem[frame]),
  5726. wpabuf_len(wpa_s->vendor_elem[frame]));
  5727. }
  5728. static int wpas_ctrl_vendor_elem_remove(struct wpa_supplicant *wpa_s, char *cmd)
  5729. {
  5730. char *pos = cmd;
  5731. int frame;
  5732. size_t len;
  5733. u8 *buf;
  5734. struct ieee802_11_elems elems;
  5735. u8 *ie, *end;
  5736. frame = atoi(pos);
  5737. if (frame < 0 || frame >= NUM_VENDOR_ELEM_FRAMES)
  5738. return -1;
  5739. wpa_s = wpas_ctrl_vendor_elem_iface(wpa_s, frame);
  5740. pos = os_strchr(pos, ' ');
  5741. if (pos == NULL)
  5742. return -1;
  5743. pos++;
  5744. if (*pos == '*') {
  5745. wpabuf_free(wpa_s->vendor_elem[frame]);
  5746. wpa_s->vendor_elem[frame] = NULL;
  5747. wpas_ctrl_vendor_elem_update(wpa_s);
  5748. return 0;
  5749. }
  5750. if (wpa_s->vendor_elem[frame] == NULL)
  5751. return -1;
  5752. len = os_strlen(pos);
  5753. if (len == 0)
  5754. return 0;
  5755. if (len & 1)
  5756. return -1;
  5757. len /= 2;
  5758. buf = os_malloc(len);
  5759. if (buf == NULL)
  5760. return -1;
  5761. if (hexstr2bin(pos, buf, len) < 0) {
  5762. os_free(buf);
  5763. return -1;
  5764. }
  5765. if (ieee802_11_parse_elems(buf, len, &elems, 0) == ParseFailed) {
  5766. os_free(buf);
  5767. return -1;
  5768. }
  5769. ie = wpabuf_mhead_u8(wpa_s->vendor_elem[frame]);
  5770. end = ie + wpabuf_len(wpa_s->vendor_elem[frame]);
  5771. for (; ie + 1 < end; ie += 2 + ie[1]) {
  5772. if (ie + len > end)
  5773. break;
  5774. if (os_memcmp(ie, buf, len) != 0)
  5775. continue;
  5776. if (wpabuf_len(wpa_s->vendor_elem[frame]) == len) {
  5777. wpabuf_free(wpa_s->vendor_elem[frame]);
  5778. wpa_s->vendor_elem[frame] = NULL;
  5779. } else {
  5780. os_memmove(ie, ie + len,
  5781. end - (ie + len));
  5782. wpa_s->vendor_elem[frame]->used -= len;
  5783. }
  5784. os_free(buf);
  5785. wpas_ctrl_vendor_elem_update(wpa_s);
  5786. return 0;
  5787. }
  5788. os_free(buf);
  5789. return -1;
  5790. }
  5791. static void wpas_ctrl_neighbor_rep_cb(void *ctx, struct wpabuf *neighbor_rep)
  5792. {
  5793. struct wpa_supplicant *wpa_s = ctx;
  5794. if (neighbor_rep) {
  5795. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_RXED
  5796. "length=%u",
  5797. (unsigned int) wpabuf_len(neighbor_rep));
  5798. wpabuf_free(neighbor_rep);
  5799. } else {
  5800. wpa_msg_ctrl(wpa_s, MSG_INFO, RRM_EVENT_NEIGHBOR_REP_FAILED);
  5801. }
  5802. }
  5803. static int wpas_ctrl_iface_send_neigbor_rep(struct wpa_supplicant *wpa_s,
  5804. char *cmd)
  5805. {
  5806. struct wpa_ssid ssid;
  5807. struct wpa_ssid *ssid_p = NULL;
  5808. int ret = 0;
  5809. if (os_strncmp(cmd, " ssid=", 6) == 0) {
  5810. ssid.ssid_len = os_strlen(cmd + 6);
  5811. if (ssid.ssid_len > 32)
  5812. return -1;
  5813. ssid.ssid = (u8 *) (cmd + 6);
  5814. ssid_p = &ssid;
  5815. }
  5816. ret = wpas_rrm_send_neighbor_rep_request(wpa_s, ssid_p,
  5817. wpas_ctrl_neighbor_rep_cb,
  5818. wpa_s);
  5819. return ret;
  5820. }
  5821. static int wpas_ctrl_iface_erp_flush(struct wpa_supplicant *wpa_s)
  5822. {
  5823. eapol_sm_erp_flush(wpa_s->eapol);
  5824. return 0;
  5825. }
  5826. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  5827. char *buf, size_t *resp_len)
  5828. {
  5829. char *reply;
  5830. const int reply_size = 4096;
  5831. int reply_len;
  5832. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  5833. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5834. if (wpa_debug_show_keys)
  5835. wpa_dbg(wpa_s, MSG_DEBUG,
  5836. "Control interface command '%s'", buf);
  5837. else
  5838. wpa_dbg(wpa_s, MSG_DEBUG,
  5839. "Control interface command '%s [REMOVED]'",
  5840. os_strncmp(buf, WPA_CTRL_RSP,
  5841. os_strlen(WPA_CTRL_RSP)) == 0 ?
  5842. WPA_CTRL_RSP : "SET_NETWORK");
  5843. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  5844. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0) {
  5845. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  5846. (const u8 *) buf, os_strlen(buf));
  5847. } else {
  5848. int level = MSG_DEBUG;
  5849. if (os_strcmp(buf, "PING") == 0)
  5850. level = MSG_EXCESSIVE;
  5851. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  5852. }
  5853. reply = os_malloc(reply_size);
  5854. if (reply == NULL) {
  5855. *resp_len = 1;
  5856. return NULL;
  5857. }
  5858. os_memcpy(reply, "OK\n", 3);
  5859. reply_len = 3;
  5860. if (os_strcmp(buf, "PING") == 0) {
  5861. os_memcpy(reply, "PONG\n", 5);
  5862. reply_len = 5;
  5863. } else if (os_strcmp(buf, "IFNAME") == 0) {
  5864. reply_len = os_strlen(wpa_s->ifname);
  5865. os_memcpy(reply, wpa_s->ifname, reply_len);
  5866. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  5867. if (wpa_debug_reopen_file() < 0)
  5868. reply_len = -1;
  5869. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  5870. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  5871. } else if (os_strcmp(buf, "MIB") == 0) {
  5872. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  5873. if (reply_len >= 0) {
  5874. int res;
  5875. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  5876. reply_size - reply_len);
  5877. if (res < 0)
  5878. reply_len = -1;
  5879. else
  5880. reply_len += res;
  5881. }
  5882. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  5883. reply_len = wpa_supplicant_ctrl_iface_status(
  5884. wpa_s, buf + 6, reply, reply_size);
  5885. } else if (os_strcmp(buf, "PMKSA") == 0) {
  5886. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  5887. reply_size);
  5888. } else if (os_strcmp(buf, "PMKSA_FLUSH") == 0) {
  5889. wpa_sm_pmksa_cache_flush(wpa_s->wpa, NULL);
  5890. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5891. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  5892. reply_len = -1;
  5893. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  5894. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  5895. reply, reply_size);
  5896. } else if (os_strcmp(buf, "LOGON") == 0) {
  5897. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  5898. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  5899. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  5900. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  5901. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5902. reply_len = -1;
  5903. else
  5904. wpas_request_connection(wpa_s);
  5905. } else if (os_strcmp(buf, "REATTACH") == 0) {
  5906. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED ||
  5907. !wpa_s->current_ssid)
  5908. reply_len = -1;
  5909. else {
  5910. wpa_s->reattach = 1;
  5911. wpas_request_connection(wpa_s);
  5912. }
  5913. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  5914. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  5915. reply_len = -1;
  5916. else if (wpa_s->disconnected)
  5917. wpas_request_connection(wpa_s);
  5918. #ifdef IEEE8021X_EAPOL
  5919. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  5920. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  5921. reply_len = -1;
  5922. #endif /* IEEE8021X_EAPOL */
  5923. #ifdef CONFIG_PEERKEY
  5924. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  5925. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  5926. reply_len = -1;
  5927. #endif /* CONFIG_PEERKEY */
  5928. #ifdef CONFIG_IEEE80211R
  5929. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  5930. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  5931. reply_len = -1;
  5932. #endif /* CONFIG_IEEE80211R */
  5933. #ifdef CONFIG_WPS
  5934. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  5935. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  5936. if (res == -2) {
  5937. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5938. reply_len = 17;
  5939. } else if (res)
  5940. reply_len = -1;
  5941. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  5942. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  5943. if (res == -2) {
  5944. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  5945. reply_len = 17;
  5946. } else if (res)
  5947. reply_len = -1;
  5948. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  5949. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  5950. reply,
  5951. reply_size);
  5952. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  5953. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  5954. wpa_s, buf + 14, reply, reply_size);
  5955. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  5956. if (wpas_wps_cancel(wpa_s))
  5957. reply_len = -1;
  5958. #ifdef CONFIG_WPS_NFC
  5959. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  5960. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  5961. reply_len = -1;
  5962. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  5963. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  5964. reply_len = -1;
  5965. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  5966. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  5967. wpa_s, buf + 21, reply, reply_size);
  5968. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  5969. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  5970. wpa_s, buf + 14, reply, reply_size);
  5971. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  5972. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  5973. buf + 17))
  5974. reply_len = -1;
  5975. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  5976. reply_len = wpas_ctrl_nfc_get_handover_req(
  5977. wpa_s, buf + 21, reply, reply_size);
  5978. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  5979. reply_len = wpas_ctrl_nfc_get_handover_sel(
  5980. wpa_s, buf + 21, reply, reply_size);
  5981. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  5982. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  5983. reply_len = -1;
  5984. #endif /* CONFIG_WPS_NFC */
  5985. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  5986. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  5987. reply_len = -1;
  5988. #ifdef CONFIG_AP
  5989. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  5990. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  5991. wpa_s, buf + 11, reply, reply_size);
  5992. #endif /* CONFIG_AP */
  5993. #ifdef CONFIG_WPS_ER
  5994. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  5995. if (wpas_wps_er_start(wpa_s, NULL))
  5996. reply_len = -1;
  5997. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  5998. if (wpas_wps_er_start(wpa_s, buf + 13))
  5999. reply_len = -1;
  6000. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  6001. if (wpas_wps_er_stop(wpa_s))
  6002. reply_len = -1;
  6003. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  6004. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  6005. reply_len = -1;
  6006. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  6007. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  6008. if (ret == -2) {
  6009. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  6010. reply_len = 17;
  6011. } else if (ret == -3) {
  6012. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  6013. reply_len = 18;
  6014. } else if (ret == -4) {
  6015. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  6016. reply_len = 20;
  6017. } else if (ret)
  6018. reply_len = -1;
  6019. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  6020. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  6021. reply_len = -1;
  6022. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  6023. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  6024. buf + 18))
  6025. reply_len = -1;
  6026. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  6027. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  6028. reply_len = -1;
  6029. #ifdef CONFIG_WPS_NFC
  6030. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  6031. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  6032. wpa_s, buf + 24, reply, reply_size);
  6033. #endif /* CONFIG_WPS_NFC */
  6034. #endif /* CONFIG_WPS_ER */
  6035. #endif /* CONFIG_WPS */
  6036. #ifdef CONFIG_IBSS_RSN
  6037. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  6038. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  6039. reply_len = -1;
  6040. #endif /* CONFIG_IBSS_RSN */
  6041. #ifdef CONFIG_MESH
  6042. } else if (os_strncmp(buf, "MESH_GROUP_ADD ", 15) == 0) {
  6043. if (wpa_supplicant_ctrl_iface_mesh_group_add(wpa_s, buf + 15))
  6044. reply_len = -1;
  6045. } else if (os_strncmp(buf, "MESH_GROUP_REMOVE ", 18) == 0) {
  6046. if (wpa_supplicant_ctrl_iface_mesh_group_remove(wpa_s,
  6047. buf + 18))
  6048. reply_len = -1;
  6049. #endif /* CONFIG_MESH */
  6050. #ifdef CONFIG_P2P
  6051. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  6052. if (p2p_ctrl_find(wpa_s, buf + 9))
  6053. reply_len = -1;
  6054. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  6055. if (p2p_ctrl_find(wpa_s, ""))
  6056. reply_len = -1;
  6057. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  6058. wpas_p2p_stop_find(wpa_s);
  6059. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  6060. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  6061. reply_size);
  6062. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  6063. if (p2p_ctrl_listen(wpa_s, buf + 11))
  6064. reply_len = -1;
  6065. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  6066. if (p2p_ctrl_listen(wpa_s, ""))
  6067. reply_len = -1;
  6068. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  6069. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  6070. reply_len = -1;
  6071. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  6072. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  6073. reply_len = -1;
  6074. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  6075. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  6076. reply_len = -1;
  6077. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  6078. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  6079. reply_len = -1;
  6080. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  6081. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  6082. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  6083. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  6084. reply_size);
  6085. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  6086. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  6087. reply_len = -1;
  6088. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  6089. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  6090. reply_len = -1;
  6091. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  6092. wpas_p2p_sd_service_update(wpa_s);
  6093. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  6094. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  6095. reply_len = -1;
  6096. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  6097. wpas_p2p_service_flush(wpa_s);
  6098. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  6099. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  6100. reply_len = -1;
  6101. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  6102. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  6103. reply_len = -1;
  6104. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  6105. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  6106. reply_len = -1;
  6107. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  6108. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  6109. reply_len = -1;
  6110. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  6111. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  6112. reply_size);
  6113. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  6114. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  6115. reply_len = -1;
  6116. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  6117. p2p_ctrl_flush(wpa_s);
  6118. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  6119. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  6120. reply_len = -1;
  6121. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  6122. if (wpas_p2p_cancel(wpa_s))
  6123. reply_len = -1;
  6124. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  6125. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  6126. reply_len = -1;
  6127. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  6128. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  6129. reply_len = -1;
  6130. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  6131. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  6132. reply_len = -1;
  6133. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  6134. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  6135. reply_len = -1;
  6136. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  6137. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  6138. reply_len = -1;
  6139. #endif /* CONFIG_P2P */
  6140. #ifdef CONFIG_WIFI_DISPLAY
  6141. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  6142. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  6143. reply_len = -1;
  6144. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  6145. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  6146. reply, reply_size);
  6147. #endif /* CONFIG_WIFI_DISPLAY */
  6148. #ifdef CONFIG_INTERWORKING
  6149. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  6150. if (interworking_fetch_anqp(wpa_s) < 0)
  6151. reply_len = -1;
  6152. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  6153. interworking_stop_fetch_anqp(wpa_s);
  6154. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  6155. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  6156. reply_len = -1;
  6157. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  6158. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  6159. reply_len = -1;
  6160. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  6161. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  6162. reply_len = -1;
  6163. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  6164. if (get_anqp(wpa_s, buf + 9) < 0)
  6165. reply_len = -1;
  6166. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  6167. if (gas_request(wpa_s, buf + 12) < 0)
  6168. reply_len = -1;
  6169. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  6170. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  6171. reply_size);
  6172. #endif /* CONFIG_INTERWORKING */
  6173. #ifdef CONFIG_HS20
  6174. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  6175. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  6176. reply_len = -1;
  6177. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  6178. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  6179. reply_len = -1;
  6180. } else if (os_strncmp(buf, "HS20_ICON_REQUEST ", 18) == 0) {
  6181. if (hs20_icon_request(wpa_s, buf + 18) < 0)
  6182. reply_len = -1;
  6183. } else if (os_strcmp(buf, "FETCH_OSU") == 0) {
  6184. if (hs20_fetch_osu(wpa_s) < 0)
  6185. reply_len = -1;
  6186. } else if (os_strcmp(buf, "CANCEL_FETCH_OSU") == 0) {
  6187. hs20_cancel_fetch_osu(wpa_s);
  6188. #endif /* CONFIG_HS20 */
  6189. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  6190. {
  6191. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  6192. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  6193. reply_len = -1;
  6194. else {
  6195. /*
  6196. * Notify response from timeout to allow the control
  6197. * interface response to be sent first.
  6198. */
  6199. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  6200. wpa_s, NULL);
  6201. }
  6202. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  6203. if (wpa_supplicant_reload_configuration(wpa_s))
  6204. reply_len = -1;
  6205. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  6206. wpa_supplicant_terminate_proc(wpa_s->global);
  6207. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  6208. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  6209. reply_len = -1;
  6210. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  6211. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  6212. wpa_s, buf + 9, reply, reply_size);
  6213. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  6214. reply_len = wpa_supplicant_ctrl_iface_log_level(
  6215. wpa_s, buf + 9, reply, reply_size);
  6216. } else if (os_strncmp(buf, "LIST_NETWORKS ", 14) == 0) {
  6217. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  6218. wpa_s, buf + 14, reply, reply_size);
  6219. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  6220. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  6221. wpa_s, NULL, reply, reply_size);
  6222. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  6223. #ifdef CONFIG_SME
  6224. wpa_s->sme.prev_bssid_set = 0;
  6225. #endif /* CONFIG_SME */
  6226. wpa_s->reassociate = 0;
  6227. wpa_s->disconnected = 1;
  6228. wpa_supplicant_cancel_sched_scan(wpa_s);
  6229. wpa_supplicant_cancel_scan(wpa_s);
  6230. wpa_supplicant_deauthenticate(wpa_s,
  6231. WLAN_REASON_DEAUTH_LEAVING);
  6232. } else if (os_strcmp(buf, "SCAN") == 0) {
  6233. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  6234. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  6235. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  6236. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  6237. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  6238. wpa_s, reply, reply_size);
  6239. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  6240. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  6241. reply_len = -1;
  6242. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  6243. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  6244. reply_len = -1;
  6245. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  6246. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  6247. reply_len = -1;
  6248. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  6249. reply_len = wpa_supplicant_ctrl_iface_add_network(
  6250. wpa_s, reply, reply_size);
  6251. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  6252. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  6253. reply_len = -1;
  6254. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  6255. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  6256. reply_len = -1;
  6257. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  6258. reply_len = wpa_supplicant_ctrl_iface_get_network(
  6259. wpa_s, buf + 12, reply, reply_size);
  6260. } else if (os_strncmp(buf, "DUP_NETWORK ", 12) == 0) {
  6261. if (wpa_supplicant_ctrl_iface_dup_network(wpa_s, buf + 12))
  6262. reply_len = -1;
  6263. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  6264. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  6265. wpa_s, reply, reply_size);
  6266. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  6267. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  6268. wpa_s, reply, reply_size);
  6269. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  6270. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  6271. reply_len = -1;
  6272. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  6273. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  6274. reply_len = -1;
  6275. } else if (os_strncmp(buf, "GET_CRED ", 9) == 0) {
  6276. reply_len = wpa_supplicant_ctrl_iface_get_cred(wpa_s, buf + 9,
  6277. reply,
  6278. reply_size);
  6279. #ifndef CONFIG_NO_CONFIG_WRITE
  6280. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  6281. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  6282. reply_len = -1;
  6283. #endif /* CONFIG_NO_CONFIG_WRITE */
  6284. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  6285. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  6286. wpa_s, buf + 15, reply, reply_size);
  6287. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  6288. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  6289. reply_len = -1;
  6290. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  6291. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  6292. reply_len = -1;
  6293. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  6294. reply_len = wpa_supplicant_global_iface_list(
  6295. wpa_s->global, reply, reply_size);
  6296. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  6297. reply_len = wpa_supplicant_global_iface_interfaces(
  6298. wpa_s->global, reply, reply_size);
  6299. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  6300. reply_len = wpa_supplicant_ctrl_iface_bss(
  6301. wpa_s, buf + 4, reply, reply_size);
  6302. #ifdef CONFIG_AP
  6303. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  6304. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  6305. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  6306. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  6307. reply_size);
  6308. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  6309. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  6310. reply_size);
  6311. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  6312. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  6313. reply_len = -1;
  6314. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  6315. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  6316. reply_len = -1;
  6317. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  6318. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  6319. reply_len = -1;
  6320. #endif /* CONFIG_AP */
  6321. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  6322. wpas_notify_suspend(wpa_s->global);
  6323. } else if (os_strcmp(buf, "RESUME") == 0) {
  6324. wpas_notify_resume(wpa_s->global);
  6325. #ifdef CONFIG_TESTING_OPTIONS
  6326. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  6327. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  6328. #endif /* CONFIG_TESTING_OPTIONS */
  6329. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  6330. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  6331. reply_len = -1;
  6332. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  6333. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  6334. reply_len = -1;
  6335. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  6336. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  6337. reply_len = -1;
  6338. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  6339. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  6340. buf + 17))
  6341. reply_len = -1;
  6342. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  6343. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  6344. reply_len = -1;
  6345. #ifdef CONFIG_TDLS
  6346. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  6347. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  6348. reply_len = -1;
  6349. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  6350. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  6351. reply_len = -1;
  6352. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  6353. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  6354. reply_len = -1;
  6355. #endif /* CONFIG_TDLS */
  6356. } else if (os_strcmp(buf, "WMM_AC_STATUS") == 0) {
  6357. reply_len = wpas_wmm_ac_status(wpa_s, reply, reply_size);
  6358. } else if (os_strncmp(buf, "WMM_AC_ADDTS ", 13) == 0) {
  6359. if (wmm_ac_ctrl_addts(wpa_s, buf + 13))
  6360. reply_len = -1;
  6361. } else if (os_strncmp(buf, "WMM_AC_DELTS ", 13) == 0) {
  6362. if (wmm_ac_ctrl_delts(wpa_s, buf + 13))
  6363. reply_len = -1;
  6364. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  6365. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  6366. reply_size);
  6367. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  6368. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  6369. reply_size);
  6370. #ifdef CONFIG_AUTOSCAN
  6371. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  6372. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  6373. reply_len = -1;
  6374. #endif /* CONFIG_AUTOSCAN */
  6375. #ifdef ANDROID
  6376. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  6377. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  6378. reply_size);
  6379. #endif /* ANDROID */
  6380. } else if (os_strncmp(buf, "VENDOR ", 7) == 0) {
  6381. reply_len = wpa_supplicant_vendor_cmd(wpa_s, buf + 7, reply,
  6382. reply_size);
  6383. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  6384. pmksa_cache_clear_current(wpa_s->wpa);
  6385. eapol_sm_request_reauth(wpa_s->eapol);
  6386. #ifdef CONFIG_WNM
  6387. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  6388. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  6389. reply_len = -1;
  6390. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  6391. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  6392. reply_len = -1;
  6393. #endif /* CONFIG_WNM */
  6394. } else if (os_strcmp(buf, "FLUSH") == 0) {
  6395. wpa_supplicant_ctrl_iface_flush(wpa_s);
  6396. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  6397. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  6398. reply_size);
  6399. #ifdef CONFIG_TESTING_OPTIONS
  6400. } else if (os_strncmp(buf, "MGMT_TX ", 8) == 0) {
  6401. if (wpas_ctrl_iface_mgmt_tx(wpa_s, buf + 8) < 0)
  6402. reply_len = -1;
  6403. } else if (os_strcmp(buf, "MGMT_TX_DONE") == 0) {
  6404. wpas_ctrl_iface_mgmt_tx_done(wpa_s);
  6405. } else if (os_strncmp(buf, "DRIVER_EVENT ", 13) == 0) {
  6406. if (wpas_ctrl_iface_driver_event(wpa_s, buf + 13) < 0)
  6407. reply_len = -1;
  6408. } else if (os_strncmp(buf, "EAPOL_RX ", 9) == 0) {
  6409. if (wpas_ctrl_iface_eapol_rx(wpa_s, buf + 9) < 0)
  6410. reply_len = -1;
  6411. } else if (os_strncmp(buf, "DATA_TEST_CONFIG ", 17) == 0) {
  6412. if (wpas_ctrl_iface_data_test_config(wpa_s, buf + 17) < 0)
  6413. reply_len = -1;
  6414. } else if (os_strncmp(buf, "DATA_TEST_TX ", 13) == 0) {
  6415. if (wpas_ctrl_iface_data_test_tx(wpa_s, buf + 13) < 0)
  6416. reply_len = -1;
  6417. } else if (os_strncmp(buf, "DATA_TEST_FRAME ", 16) == 0) {
  6418. if (wpas_ctrl_iface_data_test_frame(wpa_s, buf + 16) < 0)
  6419. reply_len = -1;
  6420. #endif /* CONFIG_TESTING_OPTIONS */
  6421. } else if (os_strncmp(buf, "VENDOR_ELEM_ADD ", 16) == 0) {
  6422. if (wpas_ctrl_vendor_elem_add(wpa_s, buf + 16) < 0)
  6423. reply_len = -1;
  6424. } else if (os_strncmp(buf, "VENDOR_ELEM_GET ", 16) == 0) {
  6425. reply_len = wpas_ctrl_vendor_elem_get(wpa_s, buf + 16, reply,
  6426. reply_size);
  6427. } else if (os_strncmp(buf, "VENDOR_ELEM_REMOVE ", 19) == 0) {
  6428. if (wpas_ctrl_vendor_elem_remove(wpa_s, buf + 19) < 0)
  6429. reply_len = -1;
  6430. } else if (os_strncmp(buf, "NEIGHBOR_REP_REQUEST", 20) == 0) {
  6431. if (wpas_ctrl_iface_send_neigbor_rep(wpa_s, buf + 20))
  6432. reply_len = -1;
  6433. } else if (os_strcmp(buf, "ERP_FLUSH") == 0) {
  6434. wpas_ctrl_iface_erp_flush(wpa_s);
  6435. } else {
  6436. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  6437. reply_len = 16;
  6438. }
  6439. if (reply_len < 0) {
  6440. os_memcpy(reply, "FAIL\n", 5);
  6441. reply_len = 5;
  6442. }
  6443. *resp_len = reply_len;
  6444. return reply;
  6445. }
  6446. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  6447. char *cmd)
  6448. {
  6449. struct wpa_interface iface;
  6450. char *pos;
  6451. /*
  6452. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  6453. * TAB<bridge_ifname>
  6454. */
  6455. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  6456. os_memset(&iface, 0, sizeof(iface));
  6457. do {
  6458. iface.ifname = pos = cmd;
  6459. pos = os_strchr(pos, '\t');
  6460. if (pos)
  6461. *pos++ = '\0';
  6462. if (iface.ifname[0] == '\0')
  6463. return -1;
  6464. if (pos == NULL)
  6465. break;
  6466. iface.confname = pos;
  6467. pos = os_strchr(pos, '\t');
  6468. if (pos)
  6469. *pos++ = '\0';
  6470. if (iface.confname[0] == '\0')
  6471. iface.confname = NULL;
  6472. if (pos == NULL)
  6473. break;
  6474. iface.driver = pos;
  6475. pos = os_strchr(pos, '\t');
  6476. if (pos)
  6477. *pos++ = '\0';
  6478. if (iface.driver[0] == '\0')
  6479. iface.driver = NULL;
  6480. if (pos == NULL)
  6481. break;
  6482. iface.ctrl_interface = pos;
  6483. pos = os_strchr(pos, '\t');
  6484. if (pos)
  6485. *pos++ = '\0';
  6486. if (iface.ctrl_interface[0] == '\0')
  6487. iface.ctrl_interface = NULL;
  6488. if (pos == NULL)
  6489. break;
  6490. iface.driver_param = pos;
  6491. pos = os_strchr(pos, '\t');
  6492. if (pos)
  6493. *pos++ = '\0';
  6494. if (iface.driver_param[0] == '\0')
  6495. iface.driver_param = NULL;
  6496. if (pos == NULL)
  6497. break;
  6498. iface.bridge_ifname = pos;
  6499. pos = os_strchr(pos, '\t');
  6500. if (pos)
  6501. *pos++ = '\0';
  6502. if (iface.bridge_ifname[0] == '\0')
  6503. iface.bridge_ifname = NULL;
  6504. if (pos == NULL)
  6505. break;
  6506. } while (0);
  6507. if (wpa_supplicant_get_iface(global, iface.ifname))
  6508. return -1;
  6509. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  6510. }
  6511. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  6512. char *cmd)
  6513. {
  6514. struct wpa_supplicant *wpa_s;
  6515. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  6516. wpa_s = wpa_supplicant_get_iface(global, cmd);
  6517. if (wpa_s == NULL)
  6518. return -1;
  6519. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  6520. }
  6521. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  6522. {
  6523. struct wpa_interface_info *prev;
  6524. while (iface) {
  6525. prev = iface;
  6526. iface = iface->next;
  6527. os_free(prev->ifname);
  6528. os_free(prev->desc);
  6529. os_free(prev);
  6530. }
  6531. }
  6532. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  6533. char *buf, int len)
  6534. {
  6535. int i, res;
  6536. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  6537. char *pos, *end;
  6538. for (i = 0; wpa_drivers[i]; i++) {
  6539. struct wpa_driver_ops *drv = wpa_drivers[i];
  6540. if (drv->get_interfaces == NULL)
  6541. continue;
  6542. tmp = drv->get_interfaces(global->drv_priv[i]);
  6543. if (tmp == NULL)
  6544. continue;
  6545. if (last == NULL)
  6546. iface = last = tmp;
  6547. else
  6548. last->next = tmp;
  6549. while (last->next)
  6550. last = last->next;
  6551. }
  6552. pos = buf;
  6553. end = buf + len;
  6554. for (tmp = iface; tmp; tmp = tmp->next) {
  6555. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  6556. tmp->drv_name, tmp->ifname,
  6557. tmp->desc ? tmp->desc : "");
  6558. if (os_snprintf_error(end - pos, res)) {
  6559. *pos = '\0';
  6560. break;
  6561. }
  6562. pos += res;
  6563. }
  6564. wpa_free_iface_info(iface);
  6565. return pos - buf;
  6566. }
  6567. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  6568. char *buf, int len)
  6569. {
  6570. int res;
  6571. char *pos, *end;
  6572. struct wpa_supplicant *wpa_s;
  6573. wpa_s = global->ifaces;
  6574. pos = buf;
  6575. end = buf + len;
  6576. while (wpa_s) {
  6577. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  6578. if (os_snprintf_error(end - pos, res)) {
  6579. *pos = '\0';
  6580. break;
  6581. }
  6582. pos += res;
  6583. wpa_s = wpa_s->next;
  6584. }
  6585. return pos - buf;
  6586. }
  6587. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  6588. const char *ifname,
  6589. char *cmd, size_t *resp_len)
  6590. {
  6591. struct wpa_supplicant *wpa_s;
  6592. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6593. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  6594. break;
  6595. }
  6596. if (wpa_s == NULL) {
  6597. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  6598. if (resp)
  6599. *resp_len = os_strlen(resp);
  6600. else
  6601. *resp_len = 1;
  6602. return resp;
  6603. }
  6604. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  6605. }
  6606. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  6607. char *buf, size_t *resp_len)
  6608. {
  6609. #ifdef CONFIG_P2P
  6610. static const char * cmd[] = {
  6611. "LIST_NETWORKS",
  6612. "P2P_FIND",
  6613. "P2P_STOP_FIND",
  6614. "P2P_LISTEN",
  6615. "P2P_GROUP_ADD",
  6616. "P2P_GET_PASSPHRASE",
  6617. "P2P_SERVICE_UPDATE",
  6618. "P2P_SERVICE_FLUSH",
  6619. "P2P_FLUSH",
  6620. "P2P_CANCEL",
  6621. "P2P_PRESENCE_REQ",
  6622. "P2P_EXT_LISTEN",
  6623. NULL
  6624. };
  6625. static const char * prefix[] = {
  6626. #ifdef ANDROID
  6627. "DRIVER ",
  6628. #endif /* ANDROID */
  6629. "GET_NETWORK ",
  6630. "REMOVE_NETWORK ",
  6631. "P2P_FIND ",
  6632. "P2P_CONNECT ",
  6633. "P2P_LISTEN ",
  6634. "P2P_GROUP_REMOVE ",
  6635. "P2P_GROUP_ADD ",
  6636. "P2P_PROV_DISC ",
  6637. "P2P_SERV_DISC_REQ ",
  6638. "P2P_SERV_DISC_CANCEL_REQ ",
  6639. "P2P_SERV_DISC_RESP ",
  6640. "P2P_SERV_DISC_EXTERNAL ",
  6641. "P2P_SERVICE_ADD ",
  6642. "P2P_SERVICE_DEL ",
  6643. "P2P_REJECT ",
  6644. "P2P_INVITE ",
  6645. "P2P_PEER ",
  6646. "P2P_SET ",
  6647. "P2P_UNAUTHORIZE ",
  6648. "P2P_PRESENCE_REQ ",
  6649. "P2P_EXT_LISTEN ",
  6650. "P2P_REMOVE_CLIENT ",
  6651. "NFC_GET_HANDOVER_SEL ",
  6652. "NFC_GET_HANDOVER_REQ ",
  6653. "NFC_REPORT_HANDOVER ",
  6654. NULL
  6655. };
  6656. int found = 0;
  6657. int i;
  6658. if (global->p2p_init_wpa_s == NULL)
  6659. return NULL;
  6660. for (i = 0; !found && cmd[i]; i++) {
  6661. if (os_strcmp(buf, cmd[i]) == 0)
  6662. found = 1;
  6663. }
  6664. for (i = 0; !found && prefix[i]; i++) {
  6665. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  6666. found = 1;
  6667. }
  6668. if (found)
  6669. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6670. buf, resp_len);
  6671. #endif /* CONFIG_P2P */
  6672. return NULL;
  6673. }
  6674. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  6675. char *buf, size_t *resp_len)
  6676. {
  6677. #ifdef CONFIG_WIFI_DISPLAY
  6678. if (global->p2p_init_wpa_s == NULL)
  6679. return NULL;
  6680. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  6681. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  6682. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  6683. buf, resp_len);
  6684. #endif /* CONFIG_WIFI_DISPLAY */
  6685. return NULL;
  6686. }
  6687. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  6688. char *buf, size_t *resp_len)
  6689. {
  6690. char *ret;
  6691. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  6692. if (ret)
  6693. return ret;
  6694. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  6695. if (ret)
  6696. return ret;
  6697. return NULL;
  6698. }
  6699. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  6700. {
  6701. char *value;
  6702. value = os_strchr(cmd, ' ');
  6703. if (value == NULL)
  6704. return -1;
  6705. *value++ = '\0';
  6706. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  6707. #ifdef CONFIG_WIFI_DISPLAY
  6708. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  6709. wifi_display_enable(global, !!atoi(value));
  6710. return 0;
  6711. }
  6712. #endif /* CONFIG_WIFI_DISPLAY */
  6713. /* Restore cmd to its original value to allow redirection */
  6714. value[-1] = ' ';
  6715. return -1;
  6716. }
  6717. #ifndef CONFIG_NO_CONFIG_WRITE
  6718. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  6719. {
  6720. int ret = 0, saved = 0;
  6721. struct wpa_supplicant *wpa_s;
  6722. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6723. if (!wpa_s->conf->update_config) {
  6724. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  6725. continue;
  6726. }
  6727. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  6728. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  6729. ret = 1;
  6730. } else {
  6731. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  6732. saved++;
  6733. }
  6734. }
  6735. if (!saved && !ret) {
  6736. wpa_dbg(wpa_s, MSG_DEBUG,
  6737. "CTRL_IFACE: SAVE_CONFIG - No configuration files could be updated");
  6738. ret = 1;
  6739. }
  6740. return ret;
  6741. }
  6742. #endif /* CONFIG_NO_CONFIG_WRITE */
  6743. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  6744. char *buf, size_t buflen)
  6745. {
  6746. char *pos, *end;
  6747. int ret;
  6748. struct wpa_supplicant *wpa_s;
  6749. pos = buf;
  6750. end = buf + buflen;
  6751. #ifdef CONFIG_P2P
  6752. if (global->p2p && !global->p2p_disabled) {
  6753. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  6754. "\n"
  6755. "p2p_state=%s\n",
  6756. MAC2STR(global->p2p_dev_addr),
  6757. p2p_get_state_txt(global->p2p));
  6758. if (os_snprintf_error(end - pos, ret))
  6759. return pos - buf;
  6760. pos += ret;
  6761. } else if (global->p2p) {
  6762. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  6763. if (os_snprintf_error(end - pos, ret))
  6764. return pos - buf;
  6765. pos += ret;
  6766. }
  6767. #endif /* CONFIG_P2P */
  6768. #ifdef CONFIG_WIFI_DISPLAY
  6769. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  6770. !!global->wifi_display);
  6771. if (os_snprintf_error(end - pos, ret))
  6772. return pos - buf;
  6773. pos += ret;
  6774. #endif /* CONFIG_WIFI_DISPLAY */
  6775. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  6776. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  6777. "address=" MACSTR "\n",
  6778. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  6779. if (os_snprintf_error(end - pos, ret))
  6780. return pos - buf;
  6781. pos += ret;
  6782. }
  6783. return pos - buf;
  6784. }
  6785. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  6786. char *buf, size_t *resp_len)
  6787. {
  6788. char *reply;
  6789. const int reply_size = 2048;
  6790. int reply_len;
  6791. int level = MSG_DEBUG;
  6792. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  6793. char *pos = os_strchr(buf + 7, ' ');
  6794. if (pos) {
  6795. *pos++ = '\0';
  6796. return wpas_global_ctrl_iface_ifname(global,
  6797. buf + 7, pos,
  6798. resp_len);
  6799. }
  6800. }
  6801. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  6802. if (reply)
  6803. return reply;
  6804. if (os_strcmp(buf, "PING") == 0)
  6805. level = MSG_EXCESSIVE;
  6806. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  6807. (const u8 *) buf, os_strlen(buf));
  6808. reply = os_malloc(reply_size);
  6809. if (reply == NULL) {
  6810. *resp_len = 1;
  6811. return NULL;
  6812. }
  6813. os_memcpy(reply, "OK\n", 3);
  6814. reply_len = 3;
  6815. if (os_strcmp(buf, "PING") == 0) {
  6816. os_memcpy(reply, "PONG\n", 5);
  6817. reply_len = 5;
  6818. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  6819. if (wpa_supplicant_global_iface_add(global, buf + 14))
  6820. reply_len = -1;
  6821. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  6822. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  6823. reply_len = -1;
  6824. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  6825. reply_len = wpa_supplicant_global_iface_list(
  6826. global, reply, reply_size);
  6827. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  6828. reply_len = wpa_supplicant_global_iface_interfaces(
  6829. global, reply, reply_size);
  6830. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  6831. wpa_supplicant_terminate_proc(global);
  6832. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  6833. wpas_notify_suspend(global);
  6834. } else if (os_strcmp(buf, "RESUME") == 0) {
  6835. wpas_notify_resume(global);
  6836. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  6837. if (wpas_global_ctrl_iface_set(global, buf + 4)) {
  6838. #ifdef CONFIG_P2P
  6839. if (global->p2p_init_wpa_s) {
  6840. os_free(reply);
  6841. /* Check if P2P redirection would work for this
  6842. * command. */
  6843. return wpa_supplicant_ctrl_iface_process(
  6844. global->p2p_init_wpa_s,
  6845. buf, resp_len);
  6846. }
  6847. #endif /* CONFIG_P2P */
  6848. reply_len = -1;
  6849. }
  6850. #ifndef CONFIG_NO_CONFIG_WRITE
  6851. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  6852. if (wpas_global_ctrl_iface_save_config(global))
  6853. reply_len = -1;
  6854. #endif /* CONFIG_NO_CONFIG_WRITE */
  6855. } else if (os_strcmp(buf, "STATUS") == 0) {
  6856. reply_len = wpas_global_ctrl_iface_status(global, reply,
  6857. reply_size);
  6858. #ifdef CONFIG_MODULE_TESTS
  6859. } else if (os_strcmp(buf, "MODULE_TESTS") == 0) {
  6860. int wpas_module_tests(void);
  6861. if (wpas_module_tests() < 0)
  6862. reply_len = -1;
  6863. #endif /* CONFIG_MODULE_TESTS */
  6864. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  6865. if (wpa_debug_reopen_file() < 0)
  6866. reply_len = -1;
  6867. } else {
  6868. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  6869. reply_len = 16;
  6870. }
  6871. if (reply_len < 0) {
  6872. os_memcpy(reply, "FAIL\n", 5);
  6873. reply_len = 5;
  6874. }
  6875. *resp_len = reply_len;
  6876. return reply;
  6877. }