ctrl_iface.c 147 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2013, 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. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "common/version.h"
  12. #include "common/ieee802_11_defs.h"
  13. #include "common/ieee802_11_common.h"
  14. #include "common/wpa_ctrl.h"
  15. #include "eap_peer/eap.h"
  16. #include "eapol_supp/eapol_supp_sm.h"
  17. #include "rsn_supp/wpa.h"
  18. #include "rsn_supp/preauth.h"
  19. #include "rsn_supp/pmksa_cache.h"
  20. #include "l2_packet/l2_packet.h"
  21. #include "wps/wps.h"
  22. #include "config.h"
  23. #include "wpa_supplicant_i.h"
  24. #include "driver_i.h"
  25. #include "wps_supplicant.h"
  26. #include "ibss_rsn.h"
  27. #include "ap.h"
  28. #include "p2p_supplicant.h"
  29. #include "p2p/p2p.h"
  30. #include "hs20_supplicant.h"
  31. #include "wifi_display.h"
  32. #include "notify.h"
  33. #include "bss.h"
  34. #include "scan.h"
  35. #include "ctrl_iface.h"
  36. #include "interworking.h"
  37. #include "blacklist.h"
  38. #include "autoscan.h"
  39. #include "wnm_sta.h"
  40. extern struct wpa_driver_ops *wpa_drivers[];
  41. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  42. char *buf, int len);
  43. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  44. char *buf, int len);
  45. static int pno_start(struct wpa_supplicant *wpa_s)
  46. {
  47. int ret, interval;
  48. size_t i, num_ssid;
  49. struct wpa_ssid *ssid;
  50. struct wpa_driver_scan_params params;
  51. if (wpa_s->pno)
  52. return 0;
  53. if ((wpa_s->wpa_state > WPA_SCANNING) &&
  54. (wpa_s->wpa_state <= WPA_COMPLETED)) {
  55. wpa_printf(MSG_ERROR, "PNO: In assoc process");
  56. return -EAGAIN;
  57. }
  58. if (wpa_s->wpa_state == WPA_SCANNING) {
  59. wpa_supplicant_cancel_sched_scan(wpa_s);
  60. wpa_supplicant_cancel_scan(wpa_s);
  61. }
  62. os_memset(&params, 0, sizeof(params));
  63. num_ssid = 0;
  64. ssid = wpa_s->conf->ssid;
  65. while (ssid) {
  66. if (!wpas_network_disabled(wpa_s, ssid))
  67. num_ssid++;
  68. ssid = ssid->next;
  69. }
  70. if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
  71. wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
  72. "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
  73. num_ssid = WPAS_MAX_SCAN_SSIDS;
  74. }
  75. if (num_ssid == 0) {
  76. wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
  77. return -1;
  78. }
  79. params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
  80. num_ssid);
  81. if (params.filter_ssids == NULL)
  82. return -1;
  83. i = 0;
  84. ssid = wpa_s->conf->ssid;
  85. while (ssid) {
  86. if (!wpas_network_disabled(wpa_s, ssid)) {
  87. params.ssids[i].ssid = ssid->ssid;
  88. params.ssids[i].ssid_len = ssid->ssid_len;
  89. params.num_ssids++;
  90. os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
  91. ssid->ssid_len);
  92. params.filter_ssids[i].ssid_len = ssid->ssid_len;
  93. params.num_filter_ssids++;
  94. i++;
  95. if (i == num_ssid)
  96. break;
  97. }
  98. ssid = ssid->next;
  99. }
  100. if (wpa_s->conf->filter_rssi)
  101. params.filter_rssi = wpa_s->conf->filter_rssi;
  102. interval = wpa_s->conf->sched_scan_interval ?
  103. wpa_s->conf->sched_scan_interval : 10;
  104. ret = wpa_drv_sched_scan(wpa_s, &params, interval * 1000);
  105. os_free(params.filter_ssids);
  106. if (ret == 0)
  107. wpa_s->pno = 1;
  108. return ret;
  109. }
  110. static int pno_stop(struct wpa_supplicant *wpa_s)
  111. {
  112. int ret = 0;
  113. if (wpa_s->pno) {
  114. wpa_s->pno = 0;
  115. ret = wpa_drv_stop_sched_scan(wpa_s);
  116. }
  117. if (wpa_s->wpa_state == WPA_SCANNING)
  118. wpa_supplicant_req_scan(wpa_s, 0, 0);
  119. return ret;
  120. }
  121. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  122. {
  123. char *pos;
  124. u8 addr[ETH_ALEN], *filter = NULL, *n;
  125. size_t count = 0;
  126. pos = val;
  127. while (pos) {
  128. if (*pos == '\0')
  129. break;
  130. if (hwaddr_aton(pos, addr)) {
  131. os_free(filter);
  132. return -1;
  133. }
  134. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  135. if (n == NULL) {
  136. os_free(filter);
  137. return -1;
  138. }
  139. filter = n;
  140. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  141. count++;
  142. pos = os_strchr(pos, ' ');
  143. if (pos)
  144. pos++;
  145. }
  146. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  147. os_free(wpa_s->bssid_filter);
  148. wpa_s->bssid_filter = filter;
  149. wpa_s->bssid_filter_count = count;
  150. return 0;
  151. }
  152. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  153. {
  154. char *pos;
  155. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  156. struct wpa_ssid_value *ssid = NULL, *ns;
  157. size_t count = 0, ssid_count = 0;
  158. struct wpa_ssid *c;
  159. /*
  160. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  161. * SSID_SPEC ::= ssid <SSID_HEX>
  162. * BSSID_SPEC ::= bssid <BSSID_HEX>
  163. */
  164. pos = val;
  165. while (pos) {
  166. if (*pos == '\0')
  167. break;
  168. if (os_strncmp(pos, "bssid ", 6) == 0) {
  169. int res;
  170. pos += 6;
  171. res = hwaddr_aton2(pos, addr);
  172. if (res < 0) {
  173. os_free(ssid);
  174. os_free(bssid);
  175. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  176. "BSSID value '%s'", pos);
  177. return -1;
  178. }
  179. pos += res;
  180. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  181. if (n == NULL) {
  182. os_free(ssid);
  183. os_free(bssid);
  184. return -1;
  185. }
  186. bssid = n;
  187. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  188. count++;
  189. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  190. char *end;
  191. pos += 5;
  192. end = pos;
  193. while (*end) {
  194. if (*end == '\0' || *end == ' ')
  195. break;
  196. end++;
  197. }
  198. ns = os_realloc_array(ssid, ssid_count + 1,
  199. sizeof(struct wpa_ssid_value));
  200. if (ns == NULL) {
  201. os_free(ssid);
  202. os_free(bssid);
  203. return -1;
  204. }
  205. ssid = ns;
  206. if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
  207. hexstr2bin(pos, ssid[ssid_count].ssid,
  208. (end - pos) / 2) < 0) {
  209. os_free(ssid);
  210. os_free(bssid);
  211. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  212. "SSID value '%s'", pos);
  213. return -1;
  214. }
  215. ssid[ssid_count].ssid_len = (end - pos) / 2;
  216. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  217. ssid[ssid_count].ssid,
  218. ssid[ssid_count].ssid_len);
  219. ssid_count++;
  220. pos = end;
  221. } else {
  222. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  223. "'%s'", pos);
  224. os_free(ssid);
  225. os_free(bssid);
  226. return -1;
  227. }
  228. pos = os_strchr(pos, ' ');
  229. if (pos)
  230. pos++;
  231. }
  232. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  233. os_free(wpa_s->disallow_aps_bssid);
  234. wpa_s->disallow_aps_bssid = bssid;
  235. wpa_s->disallow_aps_bssid_count = count;
  236. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  237. os_free(wpa_s->disallow_aps_ssid);
  238. wpa_s->disallow_aps_ssid = ssid;
  239. wpa_s->disallow_aps_ssid_count = ssid_count;
  240. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  241. return 0;
  242. c = wpa_s->current_ssid;
  243. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  244. return 0;
  245. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  246. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  247. return 0;
  248. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  249. "because current AP was marked disallowed");
  250. #ifdef CONFIG_SME
  251. wpa_s->sme.prev_bssid_set = 0;
  252. #endif /* CONFIG_SME */
  253. wpa_s->reassociate = 1;
  254. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  255. wpa_supplicant_req_scan(wpa_s, 0, 0);
  256. return 0;
  257. }
  258. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  259. char *cmd)
  260. {
  261. char *value;
  262. int ret = 0;
  263. value = os_strchr(cmd, ' ');
  264. if (value == NULL)
  265. return -1;
  266. *value++ = '\0';
  267. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  268. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  269. eapol_sm_configure(wpa_s->eapol,
  270. atoi(value), -1, -1, -1);
  271. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  272. eapol_sm_configure(wpa_s->eapol,
  273. -1, atoi(value), -1, -1);
  274. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  275. eapol_sm_configure(wpa_s->eapol,
  276. -1, -1, atoi(value), -1);
  277. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  278. eapol_sm_configure(wpa_s->eapol,
  279. -1, -1, -1, atoi(value));
  280. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  281. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  282. atoi(value)))
  283. ret = -1;
  284. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  285. 0) {
  286. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  287. atoi(value)))
  288. ret = -1;
  289. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  290. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  291. ret = -1;
  292. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  293. wpa_s->wps_fragment_size = atoi(value);
  294. #ifdef CONFIG_WPS_TESTING
  295. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  296. long int val;
  297. val = strtol(value, NULL, 0);
  298. if (val < 0 || val > 0xff) {
  299. ret = -1;
  300. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  301. "wps_version_number %ld", val);
  302. } else {
  303. wps_version_number = val;
  304. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  305. "version %u.%u",
  306. (wps_version_number & 0xf0) >> 4,
  307. wps_version_number & 0x0f);
  308. }
  309. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  310. wps_testing_dummy_cred = atoi(value);
  311. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  312. wps_testing_dummy_cred);
  313. #endif /* CONFIG_WPS_TESTING */
  314. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  315. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  316. ret = -1;
  317. #ifdef CONFIG_TDLS_TESTING
  318. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  319. extern unsigned int tdls_testing;
  320. tdls_testing = strtol(value, NULL, 0);
  321. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  322. #endif /* CONFIG_TDLS_TESTING */
  323. #ifdef CONFIG_TDLS
  324. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  325. int disabled = atoi(value);
  326. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  327. if (disabled) {
  328. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  329. ret = -1;
  330. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  331. ret = -1;
  332. wpa_tdls_enable(wpa_s->wpa, !disabled);
  333. #endif /* CONFIG_TDLS */
  334. } else if (os_strcasecmp(cmd, "pno") == 0) {
  335. if (atoi(value))
  336. ret = pno_start(wpa_s);
  337. else
  338. ret = pno_stop(wpa_s);
  339. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  340. int disabled = atoi(value);
  341. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  342. ret = -1;
  343. else if (disabled)
  344. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  345. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  346. if (os_strcmp(value, "disable") == 0)
  347. wpa_s->set_sta_uapsd = 0;
  348. else {
  349. int be, bk, vi, vo;
  350. char *pos;
  351. /* format: BE,BK,VI,VO;max SP Length */
  352. be = atoi(value);
  353. pos = os_strchr(value, ',');
  354. if (pos == NULL)
  355. return -1;
  356. pos++;
  357. bk = atoi(pos);
  358. pos = os_strchr(pos, ',');
  359. if (pos == NULL)
  360. return -1;
  361. pos++;
  362. vi = atoi(pos);
  363. pos = os_strchr(pos, ',');
  364. if (pos == NULL)
  365. return -1;
  366. pos++;
  367. vo = atoi(pos);
  368. /* ignore max SP Length for now */
  369. wpa_s->set_sta_uapsd = 1;
  370. wpa_s->sta_uapsd = 0;
  371. if (be)
  372. wpa_s->sta_uapsd |= BIT(0);
  373. if (bk)
  374. wpa_s->sta_uapsd |= BIT(1);
  375. if (vi)
  376. wpa_s->sta_uapsd |= BIT(2);
  377. if (vo)
  378. wpa_s->sta_uapsd |= BIT(3);
  379. }
  380. } else if (os_strcasecmp(cmd, "ps") == 0) {
  381. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  382. #ifdef CONFIG_WIFI_DISPLAY
  383. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  384. wifi_display_enable(wpa_s->global, !!atoi(value));
  385. #endif /* CONFIG_WIFI_DISPLAY */
  386. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  387. ret = set_bssid_filter(wpa_s, value);
  388. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  389. ret = set_disallow_aps(wpa_s, value);
  390. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  391. wpa_s->no_keep_alive = !!atoi(value);
  392. } else {
  393. value[-1] = '=';
  394. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  395. if (ret == 0)
  396. wpa_supplicant_update_config(wpa_s);
  397. }
  398. return ret;
  399. }
  400. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  401. char *cmd, char *buf, size_t buflen)
  402. {
  403. int res = -1;
  404. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  405. if (os_strcmp(cmd, "version") == 0) {
  406. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  407. } else if (os_strcasecmp(cmd, "country") == 0) {
  408. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  409. res = os_snprintf(buf, buflen, "%c%c",
  410. wpa_s->conf->country[0],
  411. wpa_s->conf->country[1]);
  412. #ifdef CONFIG_WIFI_DISPLAY
  413. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  414. res = os_snprintf(buf, buflen, "%d",
  415. wpa_s->global->wifi_display);
  416. if (res < 0 || (unsigned int) res >= buflen)
  417. return -1;
  418. return res;
  419. #endif /* CONFIG_WIFI_DISPLAY */
  420. #ifdef CONFIG_TESTING_GET_GTK
  421. } else if (os_strcmp(cmd, "gtk") == 0) {
  422. if (wpa_s->last_gtk_len == 0)
  423. return -1;
  424. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  425. wpa_s->last_gtk_len);
  426. return res;
  427. #endif /* CONFIG_TESTING_GET_GTK */
  428. }
  429. if (res < 0 || (unsigned int) res >= buflen)
  430. return -1;
  431. return res;
  432. }
  433. #ifdef IEEE8021X_EAPOL
  434. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  435. char *addr)
  436. {
  437. u8 bssid[ETH_ALEN];
  438. struct wpa_ssid *ssid = wpa_s->current_ssid;
  439. if (hwaddr_aton(addr, bssid)) {
  440. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  441. "'%s'", addr);
  442. return -1;
  443. }
  444. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  445. rsn_preauth_deinit(wpa_s->wpa);
  446. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  447. return -1;
  448. return 0;
  449. }
  450. #endif /* IEEE8021X_EAPOL */
  451. #ifdef CONFIG_PEERKEY
  452. /* MLME-STKSTART.request(peer) */
  453. static int wpa_supplicant_ctrl_iface_stkstart(
  454. struct wpa_supplicant *wpa_s, char *addr)
  455. {
  456. u8 peer[ETH_ALEN];
  457. if (hwaddr_aton(addr, peer)) {
  458. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  459. "address '%s'", addr);
  460. return -1;
  461. }
  462. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  463. MAC2STR(peer));
  464. return wpa_sm_stkstart(wpa_s->wpa, peer);
  465. }
  466. #endif /* CONFIG_PEERKEY */
  467. #ifdef CONFIG_TDLS
  468. static int wpa_supplicant_ctrl_iface_tdls_discover(
  469. struct wpa_supplicant *wpa_s, char *addr)
  470. {
  471. u8 peer[ETH_ALEN];
  472. int ret;
  473. if (hwaddr_aton(addr, peer)) {
  474. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  475. "address '%s'", addr);
  476. return -1;
  477. }
  478. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  479. MAC2STR(peer));
  480. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  481. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  482. else
  483. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  484. return ret;
  485. }
  486. static int wpa_supplicant_ctrl_iface_tdls_setup(
  487. struct wpa_supplicant *wpa_s, char *addr)
  488. {
  489. u8 peer[ETH_ALEN];
  490. int ret;
  491. if (hwaddr_aton(addr, peer)) {
  492. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  493. "address '%s'", addr);
  494. return -1;
  495. }
  496. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  497. MAC2STR(peer));
  498. if ((wpa_s->conf->tdls_external_control) &&
  499. wpa_tdls_is_external_setup(wpa_s->wpa))
  500. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  501. wpa_tdls_remove(wpa_s->wpa, peer);
  502. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  503. ret = wpa_tdls_start(wpa_s->wpa, peer);
  504. else
  505. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  506. return ret;
  507. }
  508. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  509. struct wpa_supplicant *wpa_s, char *addr)
  510. {
  511. u8 peer[ETH_ALEN];
  512. int ret;
  513. if (hwaddr_aton(addr, peer)) {
  514. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  515. "address '%s'", addr);
  516. return -1;
  517. }
  518. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  519. MAC2STR(peer));
  520. if ((wpa_s->conf->tdls_external_control) &&
  521. wpa_tdls_is_external_setup(wpa_s->wpa))
  522. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  523. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  524. ret = wpa_tdls_teardown_link(
  525. wpa_s->wpa, peer,
  526. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  527. else
  528. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  529. return ret;
  530. }
  531. #endif /* CONFIG_TDLS */
  532. #ifdef CONFIG_IEEE80211R
  533. static int wpa_supplicant_ctrl_iface_ft_ds(
  534. struct wpa_supplicant *wpa_s, char *addr)
  535. {
  536. u8 target_ap[ETH_ALEN];
  537. struct wpa_bss *bss;
  538. const u8 *mdie;
  539. if (hwaddr_aton(addr, target_ap)) {
  540. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  541. "address '%s'", addr);
  542. return -1;
  543. }
  544. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  545. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  546. if (bss)
  547. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  548. else
  549. mdie = NULL;
  550. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  551. }
  552. #endif /* CONFIG_IEEE80211R */
  553. #ifdef CONFIG_WPS
  554. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  555. char *cmd)
  556. {
  557. u8 bssid[ETH_ALEN], *_bssid = bssid;
  558. #ifdef CONFIG_P2P
  559. u8 p2p_dev_addr[ETH_ALEN];
  560. #endif /* CONFIG_P2P */
  561. #ifdef CONFIG_AP
  562. u8 *_p2p_dev_addr = NULL;
  563. #endif /* CONFIG_AP */
  564. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  565. _bssid = NULL;
  566. #ifdef CONFIG_P2P
  567. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  568. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  569. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  570. "P2P Device Address '%s'",
  571. cmd + 13);
  572. return -1;
  573. }
  574. _p2p_dev_addr = p2p_dev_addr;
  575. #endif /* CONFIG_P2P */
  576. } else if (hwaddr_aton(cmd, bssid)) {
  577. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  578. cmd);
  579. return -1;
  580. }
  581. #ifdef CONFIG_AP
  582. if (wpa_s->ap_iface)
  583. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  584. #endif /* CONFIG_AP */
  585. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  586. }
  587. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  588. char *cmd, char *buf,
  589. size_t buflen)
  590. {
  591. u8 bssid[ETH_ALEN], *_bssid = bssid;
  592. char *pin;
  593. int ret;
  594. pin = os_strchr(cmd, ' ');
  595. if (pin)
  596. *pin++ = '\0';
  597. if (os_strcmp(cmd, "any") == 0)
  598. _bssid = NULL;
  599. else if (os_strcmp(cmd, "get") == 0) {
  600. ret = wps_generate_pin();
  601. goto done;
  602. } else if (hwaddr_aton(cmd, bssid)) {
  603. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  604. cmd);
  605. return -1;
  606. }
  607. #ifdef CONFIG_AP
  608. if (wpa_s->ap_iface) {
  609. int timeout = 0;
  610. char *pos;
  611. if (pin) {
  612. pos = os_strchr(pin, ' ');
  613. if (pos) {
  614. *pos++ = '\0';
  615. timeout = atoi(pos);
  616. }
  617. }
  618. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  619. buf, buflen, timeout);
  620. }
  621. #endif /* CONFIG_AP */
  622. if (pin) {
  623. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  624. DEV_PW_DEFAULT);
  625. if (ret < 0)
  626. return -1;
  627. ret = os_snprintf(buf, buflen, "%s", pin);
  628. if (ret < 0 || (size_t) ret >= buflen)
  629. return -1;
  630. return ret;
  631. }
  632. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  633. if (ret < 0)
  634. return -1;
  635. done:
  636. /* Return the generated PIN */
  637. ret = os_snprintf(buf, buflen, "%08d", ret);
  638. if (ret < 0 || (size_t) ret >= buflen)
  639. return -1;
  640. return ret;
  641. }
  642. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  643. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  644. {
  645. char pin[9];
  646. size_t len;
  647. char *pos;
  648. int ret;
  649. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  650. (u8 *) cmd, os_strlen(cmd));
  651. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  652. if (*pos < '0' || *pos > '9')
  653. continue;
  654. pin[len++] = *pos;
  655. if (len == 9) {
  656. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  657. return -1;
  658. }
  659. }
  660. if (len != 4 && len != 8) {
  661. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  662. return -1;
  663. }
  664. pin[len] = '\0';
  665. if (len == 8) {
  666. unsigned int pin_val;
  667. pin_val = atoi(pin);
  668. if (!wps_pin_valid(pin_val)) {
  669. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  670. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  671. if (ret < 0 || (size_t) ret >= buflen)
  672. return -1;
  673. return ret;
  674. }
  675. }
  676. ret = os_snprintf(buf, buflen, "%s", pin);
  677. if (ret < 0 || (size_t) ret >= buflen)
  678. return -1;
  679. return ret;
  680. }
  681. #ifdef CONFIG_WPS_NFC
  682. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  683. char *cmd)
  684. {
  685. u8 bssid[ETH_ALEN], *_bssid = bssid;
  686. if (cmd == NULL || cmd[0] == '\0')
  687. _bssid = NULL;
  688. else if (hwaddr_aton(cmd, bssid))
  689. return -1;
  690. return wpas_wps_start_nfc(wpa_s, _bssid);
  691. }
  692. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  693. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  694. {
  695. int ndef;
  696. struct wpabuf *buf;
  697. int res;
  698. char *pos;
  699. pos = os_strchr(cmd, ' ');
  700. if (pos)
  701. *pos++ = '\0';
  702. if (os_strcmp(cmd, "WPS") == 0)
  703. ndef = 0;
  704. else if (os_strcmp(cmd, "NDEF") == 0)
  705. ndef = 1;
  706. else
  707. return -1;
  708. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  709. if (buf == NULL)
  710. return -1;
  711. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  712. wpabuf_len(buf));
  713. reply[res++] = '\n';
  714. reply[res] = '\0';
  715. wpabuf_free(buf);
  716. return res;
  717. }
  718. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  719. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  720. {
  721. int ndef;
  722. struct wpabuf *buf;
  723. int res;
  724. if (os_strcmp(cmd, "WPS") == 0)
  725. ndef = 0;
  726. else if (os_strcmp(cmd, "NDEF") == 0)
  727. ndef = 1;
  728. else
  729. return -1;
  730. buf = wpas_wps_nfc_token(wpa_s, ndef);
  731. if (buf == NULL)
  732. return -1;
  733. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  734. wpabuf_len(buf));
  735. reply[res++] = '\n';
  736. reply[res] = '\0';
  737. wpabuf_free(buf);
  738. return res;
  739. }
  740. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  741. struct wpa_supplicant *wpa_s, char *pos)
  742. {
  743. size_t len;
  744. struct wpabuf *buf;
  745. int ret;
  746. len = os_strlen(pos);
  747. if (len & 0x01)
  748. return -1;
  749. len /= 2;
  750. buf = wpabuf_alloc(len);
  751. if (buf == NULL)
  752. return -1;
  753. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  754. wpabuf_free(buf);
  755. return -1;
  756. }
  757. ret = wpas_wps_nfc_tag_read(wpa_s, buf);
  758. wpabuf_free(buf);
  759. return ret;
  760. }
  761. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  762. char *reply, size_t max_len,
  763. int cr)
  764. {
  765. struct wpabuf *buf;
  766. int res;
  767. buf = wpas_wps_nfc_handover_req(wpa_s, cr);
  768. if (buf == NULL)
  769. return -1;
  770. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  771. wpabuf_len(buf));
  772. reply[res++] = '\n';
  773. reply[res] = '\0';
  774. wpabuf_free(buf);
  775. return res;
  776. }
  777. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  778. char *cmd, char *reply,
  779. size_t max_len)
  780. {
  781. char *pos;
  782. pos = os_strchr(cmd, ' ');
  783. if (pos == NULL)
  784. return -1;
  785. *pos++ = '\0';
  786. if (os_strcmp(cmd, "NDEF") != 0)
  787. return -1;
  788. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  789. return wpas_ctrl_nfc_get_handover_req_wps(
  790. wpa_s, reply, max_len, os_strcmp(pos, "WPS-CR") == 0);
  791. }
  792. return -1;
  793. }
  794. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  795. char *reply, size_t max_len,
  796. int ndef, int cr, char *uuid)
  797. {
  798. struct wpabuf *buf;
  799. int res;
  800. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  801. if (buf == NULL)
  802. return -1;
  803. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  804. wpabuf_len(buf));
  805. reply[res++] = '\n';
  806. reply[res] = '\0';
  807. wpabuf_free(buf);
  808. return res;
  809. }
  810. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  811. char *cmd, char *reply,
  812. size_t max_len)
  813. {
  814. char *pos, *pos2;
  815. int ndef;
  816. pos = os_strchr(cmd, ' ');
  817. if (pos == NULL)
  818. return -1;
  819. *pos++ = '\0';
  820. if (os_strcmp(cmd, "WPS") == 0)
  821. ndef = 0;
  822. else if (os_strcmp(cmd, "NDEF") == 0)
  823. ndef = 1;
  824. else
  825. return -1;
  826. pos2 = os_strchr(pos, ' ');
  827. if (pos2)
  828. *pos2++ = '\0';
  829. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  830. return wpas_ctrl_nfc_get_handover_sel_wps(
  831. wpa_s, reply, max_len, ndef,
  832. os_strcmp(pos, "WPS-CR") == 0, pos2);
  833. }
  834. return -1;
  835. }
  836. static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
  837. char *cmd, char *reply,
  838. size_t max_len)
  839. {
  840. size_t len;
  841. struct wpabuf *buf;
  842. int ret;
  843. len = os_strlen(cmd);
  844. if (len & 0x01)
  845. return -1;
  846. len /= 2;
  847. buf = wpabuf_alloc(len);
  848. if (buf == NULL)
  849. return -1;
  850. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  851. wpabuf_free(buf);
  852. return -1;
  853. }
  854. ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
  855. wpabuf_free(buf);
  856. return ret;
  857. }
  858. static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
  859. char *cmd)
  860. {
  861. size_t len;
  862. struct wpabuf *buf;
  863. int ret;
  864. len = os_strlen(cmd);
  865. if (len & 0x01)
  866. return -1;
  867. len /= 2;
  868. buf = wpabuf_alloc(len);
  869. if (buf == NULL)
  870. return -1;
  871. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  872. wpabuf_free(buf);
  873. return -1;
  874. }
  875. ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
  876. wpabuf_free(buf);
  877. return ret;
  878. }
  879. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  880. char *cmd)
  881. {
  882. size_t len;
  883. struct wpabuf *req, *sel;
  884. int ret;
  885. char *pos, *role, *type, *pos2;
  886. role = cmd;
  887. pos = os_strchr(role, ' ');
  888. if (pos == NULL)
  889. return -1;
  890. *pos++ = '\0';
  891. type = pos;
  892. pos = os_strchr(type, ' ');
  893. if (pos == NULL)
  894. return -1;
  895. *pos++ = '\0';
  896. pos2 = os_strchr(pos, ' ');
  897. if (pos2 == NULL)
  898. return -1;
  899. *pos2++ = '\0';
  900. len = os_strlen(pos);
  901. if (len & 0x01)
  902. return -1;
  903. len /= 2;
  904. req = wpabuf_alloc(len);
  905. if (req == NULL)
  906. return -1;
  907. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  908. wpabuf_free(req);
  909. return -1;
  910. }
  911. len = os_strlen(pos2);
  912. if (len & 0x01) {
  913. wpabuf_free(req);
  914. return -1;
  915. }
  916. len /= 2;
  917. sel = wpabuf_alloc(len);
  918. if (sel == NULL) {
  919. wpabuf_free(req);
  920. return -1;
  921. }
  922. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  923. wpabuf_free(req);
  924. wpabuf_free(sel);
  925. return -1;
  926. }
  927. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  928. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  929. } else {
  930. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  931. "reported: role=%s type=%s", role, type);
  932. ret = -1;
  933. }
  934. wpabuf_free(req);
  935. wpabuf_free(sel);
  936. return ret;
  937. }
  938. #endif /* CONFIG_WPS_NFC */
  939. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  940. char *cmd)
  941. {
  942. u8 bssid[ETH_ALEN];
  943. char *pin;
  944. char *new_ssid;
  945. char *new_auth;
  946. char *new_encr;
  947. char *new_key;
  948. struct wps_new_ap_settings ap;
  949. pin = os_strchr(cmd, ' ');
  950. if (pin == NULL)
  951. return -1;
  952. *pin++ = '\0';
  953. if (hwaddr_aton(cmd, bssid)) {
  954. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  955. cmd);
  956. return -1;
  957. }
  958. new_ssid = os_strchr(pin, ' ');
  959. if (new_ssid == NULL)
  960. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  961. *new_ssid++ = '\0';
  962. new_auth = os_strchr(new_ssid, ' ');
  963. if (new_auth == NULL)
  964. return -1;
  965. *new_auth++ = '\0';
  966. new_encr = os_strchr(new_auth, ' ');
  967. if (new_encr == NULL)
  968. return -1;
  969. *new_encr++ = '\0';
  970. new_key = os_strchr(new_encr, ' ');
  971. if (new_key == NULL)
  972. return -1;
  973. *new_key++ = '\0';
  974. os_memset(&ap, 0, sizeof(ap));
  975. ap.ssid_hex = new_ssid;
  976. ap.auth = new_auth;
  977. ap.encr = new_encr;
  978. ap.key_hex = new_key;
  979. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  980. }
  981. #ifdef CONFIG_AP
  982. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  983. char *cmd, char *buf,
  984. size_t buflen)
  985. {
  986. int timeout = 300;
  987. char *pos;
  988. const char *pin_txt;
  989. if (!wpa_s->ap_iface)
  990. return -1;
  991. pos = os_strchr(cmd, ' ');
  992. if (pos)
  993. *pos++ = '\0';
  994. if (os_strcmp(cmd, "disable") == 0) {
  995. wpas_wps_ap_pin_disable(wpa_s);
  996. return os_snprintf(buf, buflen, "OK\n");
  997. }
  998. if (os_strcmp(cmd, "random") == 0) {
  999. if (pos)
  1000. timeout = atoi(pos);
  1001. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1002. if (pin_txt == NULL)
  1003. return -1;
  1004. return os_snprintf(buf, buflen, "%s", pin_txt);
  1005. }
  1006. if (os_strcmp(cmd, "get") == 0) {
  1007. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1008. if (pin_txt == NULL)
  1009. return -1;
  1010. return os_snprintf(buf, buflen, "%s", pin_txt);
  1011. }
  1012. if (os_strcmp(cmd, "set") == 0) {
  1013. char *pin;
  1014. if (pos == NULL)
  1015. return -1;
  1016. pin = pos;
  1017. pos = os_strchr(pos, ' ');
  1018. if (pos) {
  1019. *pos++ = '\0';
  1020. timeout = atoi(pos);
  1021. }
  1022. if (os_strlen(pin) > buflen)
  1023. return -1;
  1024. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1025. return -1;
  1026. return os_snprintf(buf, buflen, "%s", pin);
  1027. }
  1028. return -1;
  1029. }
  1030. #endif /* CONFIG_AP */
  1031. #ifdef CONFIG_WPS_ER
  1032. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1033. char *cmd)
  1034. {
  1035. char *uuid = cmd, *pin, *pos;
  1036. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1037. pin = os_strchr(uuid, ' ');
  1038. if (pin == NULL)
  1039. return -1;
  1040. *pin++ = '\0';
  1041. pos = os_strchr(pin, ' ');
  1042. if (pos) {
  1043. *pos++ = '\0';
  1044. if (hwaddr_aton(pos, addr_buf) == 0)
  1045. addr = addr_buf;
  1046. }
  1047. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1048. }
  1049. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1050. char *cmd)
  1051. {
  1052. char *uuid = cmd, *pin;
  1053. pin = os_strchr(uuid, ' ');
  1054. if (pin == NULL)
  1055. return -1;
  1056. *pin++ = '\0';
  1057. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1058. }
  1059. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1060. struct wpa_supplicant *wpa_s, char *cmd)
  1061. {
  1062. char *uuid = cmd, *id;
  1063. id = os_strchr(uuid, ' ');
  1064. if (id == NULL)
  1065. return -1;
  1066. *id++ = '\0';
  1067. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1068. }
  1069. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1070. struct wpa_supplicant *wpa_s, char *cmd)
  1071. {
  1072. char *pin;
  1073. char *new_ssid;
  1074. char *new_auth;
  1075. char *new_encr;
  1076. char *new_key;
  1077. struct wps_new_ap_settings ap;
  1078. pin = os_strchr(cmd, ' ');
  1079. if (pin == NULL)
  1080. return -1;
  1081. *pin++ = '\0';
  1082. new_ssid = os_strchr(pin, ' ');
  1083. if (new_ssid == NULL)
  1084. return -1;
  1085. *new_ssid++ = '\0';
  1086. new_auth = os_strchr(new_ssid, ' ');
  1087. if (new_auth == NULL)
  1088. return -1;
  1089. *new_auth++ = '\0';
  1090. new_encr = os_strchr(new_auth, ' ');
  1091. if (new_encr == NULL)
  1092. return -1;
  1093. *new_encr++ = '\0';
  1094. new_key = os_strchr(new_encr, ' ');
  1095. if (new_key == NULL)
  1096. return -1;
  1097. *new_key++ = '\0';
  1098. os_memset(&ap, 0, sizeof(ap));
  1099. ap.ssid_hex = new_ssid;
  1100. ap.auth = new_auth;
  1101. ap.encr = new_encr;
  1102. ap.key_hex = new_key;
  1103. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1104. }
  1105. #ifdef CONFIG_WPS_NFC
  1106. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1107. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1108. {
  1109. int ndef;
  1110. struct wpabuf *buf;
  1111. int res;
  1112. char *uuid;
  1113. uuid = os_strchr(cmd, ' ');
  1114. if (uuid == NULL)
  1115. return -1;
  1116. *uuid++ = '\0';
  1117. if (os_strcmp(cmd, "WPS") == 0)
  1118. ndef = 0;
  1119. else if (os_strcmp(cmd, "NDEF") == 0)
  1120. ndef = 1;
  1121. else
  1122. return -1;
  1123. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1124. if (buf == NULL)
  1125. return -1;
  1126. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1127. wpabuf_len(buf));
  1128. reply[res++] = '\n';
  1129. reply[res] = '\0';
  1130. wpabuf_free(buf);
  1131. return res;
  1132. }
  1133. #endif /* CONFIG_WPS_NFC */
  1134. #endif /* CONFIG_WPS_ER */
  1135. #endif /* CONFIG_WPS */
  1136. #ifdef CONFIG_IBSS_RSN
  1137. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1138. struct wpa_supplicant *wpa_s, char *addr)
  1139. {
  1140. u8 peer[ETH_ALEN];
  1141. if (hwaddr_aton(addr, peer)) {
  1142. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1143. "address '%s'", addr);
  1144. return -1;
  1145. }
  1146. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1147. MAC2STR(peer));
  1148. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1149. }
  1150. #endif /* CONFIG_IBSS_RSN */
  1151. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1152. char *rsp)
  1153. {
  1154. #ifdef IEEE8021X_EAPOL
  1155. char *pos, *id_pos;
  1156. int id;
  1157. struct wpa_ssid *ssid;
  1158. pos = os_strchr(rsp, '-');
  1159. if (pos == NULL)
  1160. return -1;
  1161. *pos++ = '\0';
  1162. id_pos = pos;
  1163. pos = os_strchr(pos, ':');
  1164. if (pos == NULL)
  1165. return -1;
  1166. *pos++ = '\0';
  1167. id = atoi(id_pos);
  1168. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1169. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1170. (u8 *) pos, os_strlen(pos));
  1171. ssid = wpa_config_get_network(wpa_s->conf, id);
  1172. if (ssid == NULL) {
  1173. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1174. "to update", id);
  1175. return -1;
  1176. }
  1177. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1178. pos);
  1179. #else /* IEEE8021X_EAPOL */
  1180. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1181. return -1;
  1182. #endif /* IEEE8021X_EAPOL */
  1183. }
  1184. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1185. const char *params,
  1186. char *buf, size_t buflen)
  1187. {
  1188. char *pos, *end, tmp[30];
  1189. int res, verbose, wps, ret;
  1190. if (os_strcmp(params, "-DRIVER") == 0)
  1191. return wpa_drv_status(wpa_s, buf, buflen);
  1192. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1193. wps = os_strcmp(params, "-WPS") == 0;
  1194. pos = buf;
  1195. end = buf + buflen;
  1196. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1197. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1198. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1199. MAC2STR(wpa_s->bssid));
  1200. if (ret < 0 || ret >= end - pos)
  1201. return pos - buf;
  1202. pos += ret;
  1203. if (ssid) {
  1204. u8 *_ssid = ssid->ssid;
  1205. size_t ssid_len = ssid->ssid_len;
  1206. u8 ssid_buf[MAX_SSID_LEN];
  1207. if (ssid_len == 0) {
  1208. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1209. if (_res < 0)
  1210. ssid_len = 0;
  1211. else
  1212. ssid_len = _res;
  1213. _ssid = ssid_buf;
  1214. }
  1215. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1216. wpa_ssid_txt(_ssid, ssid_len),
  1217. ssid->id);
  1218. if (ret < 0 || ret >= end - pos)
  1219. return pos - buf;
  1220. pos += ret;
  1221. if (wps && ssid->passphrase &&
  1222. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1223. (ssid->mode == WPAS_MODE_AP ||
  1224. ssid->mode == WPAS_MODE_P2P_GO)) {
  1225. ret = os_snprintf(pos, end - pos,
  1226. "passphrase=%s\n",
  1227. ssid->passphrase);
  1228. if (ret < 0 || ret >= end - pos)
  1229. return pos - buf;
  1230. pos += ret;
  1231. }
  1232. if (ssid->id_str) {
  1233. ret = os_snprintf(pos, end - pos,
  1234. "id_str=%s\n",
  1235. ssid->id_str);
  1236. if (ret < 0 || ret >= end - pos)
  1237. return pos - buf;
  1238. pos += ret;
  1239. }
  1240. switch (ssid->mode) {
  1241. case WPAS_MODE_INFRA:
  1242. ret = os_snprintf(pos, end - pos,
  1243. "mode=station\n");
  1244. break;
  1245. case WPAS_MODE_IBSS:
  1246. ret = os_snprintf(pos, end - pos,
  1247. "mode=IBSS\n");
  1248. break;
  1249. case WPAS_MODE_AP:
  1250. ret = os_snprintf(pos, end - pos,
  1251. "mode=AP\n");
  1252. break;
  1253. case WPAS_MODE_P2P_GO:
  1254. ret = os_snprintf(pos, end - pos,
  1255. "mode=P2P GO\n");
  1256. break;
  1257. case WPAS_MODE_P2P_GROUP_FORMATION:
  1258. ret = os_snprintf(pos, end - pos,
  1259. "mode=P2P GO - group "
  1260. "formation\n");
  1261. break;
  1262. default:
  1263. ret = 0;
  1264. break;
  1265. }
  1266. if (ret < 0 || ret >= end - pos)
  1267. return pos - buf;
  1268. pos += ret;
  1269. }
  1270. #ifdef CONFIG_AP
  1271. if (wpa_s->ap_iface) {
  1272. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1273. end - pos,
  1274. verbose);
  1275. } else
  1276. #endif /* CONFIG_AP */
  1277. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1278. }
  1279. #ifdef CONFIG_SAE
  1280. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1281. #ifdef CONFIG_AP
  1282. !wpa_s->ap_iface &&
  1283. #endif /* CONFIG_AP */
  1284. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1285. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1286. wpa_s->sme.sae.group);
  1287. if (ret < 0 || ret >= end - pos)
  1288. return pos - buf;
  1289. pos += ret;
  1290. }
  1291. #endif /* CONFIG_SAE */
  1292. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1293. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1294. if (ret < 0 || ret >= end - pos)
  1295. return pos - buf;
  1296. pos += ret;
  1297. if (wpa_s->l2 &&
  1298. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1299. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1300. if (ret < 0 || ret >= end - pos)
  1301. return pos - buf;
  1302. pos += ret;
  1303. }
  1304. #ifdef CONFIG_P2P
  1305. if (wpa_s->global->p2p) {
  1306. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1307. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1308. if (ret < 0 || ret >= end - pos)
  1309. return pos - buf;
  1310. pos += ret;
  1311. }
  1312. #endif /* CONFIG_P2P */
  1313. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1314. MAC2STR(wpa_s->own_addr));
  1315. if (ret < 0 || ret >= end - pos)
  1316. return pos - buf;
  1317. pos += ret;
  1318. #ifdef CONFIG_HS20
  1319. if (wpa_s->current_bss &&
  1320. wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
  1321. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1322. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1323. ret = os_snprintf(pos, end - pos, "hs20=1\n");
  1324. if (ret < 0 || ret >= end - pos)
  1325. return pos - buf;
  1326. pos += ret;
  1327. }
  1328. if (wpa_s->current_ssid) {
  1329. struct wpa_cred *cred;
  1330. char *type;
  1331. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1332. size_t i;
  1333. if (wpa_s->current_ssid->parent_cred != cred)
  1334. continue;
  1335. for (i = 0; cred->domain && i < cred->num_domain; i++) {
  1336. ret = os_snprintf(pos, end - pos,
  1337. "home_sp=%s\n",
  1338. cred->domain[i]);
  1339. if (ret < 0 || ret >= end - pos)
  1340. return pos - buf;
  1341. pos += ret;
  1342. }
  1343. if (wpa_s->current_bss == NULL ||
  1344. wpa_s->current_bss->anqp == NULL)
  1345. res = -1;
  1346. else
  1347. res = interworking_home_sp_cred(
  1348. wpa_s, cred,
  1349. wpa_s->current_bss->anqp->domain_name);
  1350. if (res > 0)
  1351. type = "home";
  1352. else if (res == 0)
  1353. type = "roaming";
  1354. else
  1355. type = "unknown";
  1356. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1357. if (ret < 0 || ret >= end - pos)
  1358. return pos - buf;
  1359. pos += ret;
  1360. break;
  1361. }
  1362. }
  1363. #endif /* CONFIG_HS20 */
  1364. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1365. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1366. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1367. verbose);
  1368. if (res >= 0)
  1369. pos += res;
  1370. }
  1371. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1372. if (res >= 0)
  1373. pos += res;
  1374. #ifdef ANDROID
  1375. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1376. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1377. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1378. wpa_s->wpa_state,
  1379. MAC2STR(wpa_s->bssid),
  1380. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1381. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1382. wpa_s->current_ssid->ssid_len) : "");
  1383. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1384. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1385. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1386. "- connection to " MACSTR
  1387. " completed %s [id=%d id_str=%s]",
  1388. MAC2STR(wpa_s->bssid), "(auth)",
  1389. ssid ? ssid->id : -1,
  1390. ssid && ssid->id_str ? ssid->id_str : "");
  1391. }
  1392. #endif /* ANDROID */
  1393. return pos - buf;
  1394. }
  1395. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1396. char *cmd)
  1397. {
  1398. char *pos;
  1399. int id;
  1400. struct wpa_ssid *ssid;
  1401. u8 bssid[ETH_ALEN];
  1402. /* cmd: "<network id> <BSSID>" */
  1403. pos = os_strchr(cmd, ' ');
  1404. if (pos == NULL)
  1405. return -1;
  1406. *pos++ = '\0';
  1407. id = atoi(cmd);
  1408. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1409. if (hwaddr_aton(pos, bssid)) {
  1410. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1411. return -1;
  1412. }
  1413. ssid = wpa_config_get_network(wpa_s->conf, id);
  1414. if (ssid == NULL) {
  1415. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1416. "to update", id);
  1417. return -1;
  1418. }
  1419. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1420. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1421. return 0;
  1422. }
  1423. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1424. char *cmd, char *buf,
  1425. size_t buflen)
  1426. {
  1427. u8 bssid[ETH_ALEN];
  1428. struct wpa_blacklist *e;
  1429. char *pos, *end;
  1430. int ret;
  1431. /* cmd: "BLACKLIST [<BSSID>]" */
  1432. if (*cmd == '\0') {
  1433. pos = buf;
  1434. end = buf + buflen;
  1435. e = wpa_s->blacklist;
  1436. while (e) {
  1437. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1438. MAC2STR(e->bssid));
  1439. if (ret < 0 || ret >= end - pos)
  1440. return pos - buf;
  1441. pos += ret;
  1442. e = e->next;
  1443. }
  1444. return pos - buf;
  1445. }
  1446. cmd++;
  1447. if (os_strncmp(cmd, "clear", 5) == 0) {
  1448. wpa_blacklist_clear(wpa_s);
  1449. os_memcpy(buf, "OK\n", 3);
  1450. return 3;
  1451. }
  1452. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1453. if (hwaddr_aton(cmd, bssid)) {
  1454. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1455. return -1;
  1456. }
  1457. /*
  1458. * Add the BSSID twice, so its count will be 2, causing it to be
  1459. * skipped when processing scan results.
  1460. */
  1461. ret = wpa_blacklist_add(wpa_s, bssid);
  1462. if (ret != 0)
  1463. return -1;
  1464. ret = wpa_blacklist_add(wpa_s, bssid);
  1465. if (ret != 0)
  1466. return -1;
  1467. os_memcpy(buf, "OK\n", 3);
  1468. return 3;
  1469. }
  1470. extern int wpa_debug_level;
  1471. extern int wpa_debug_timestamp;
  1472. static const char * debug_level_str(int level)
  1473. {
  1474. switch (level) {
  1475. case MSG_EXCESSIVE:
  1476. return "EXCESSIVE";
  1477. case MSG_MSGDUMP:
  1478. return "MSGDUMP";
  1479. case MSG_DEBUG:
  1480. return "DEBUG";
  1481. case MSG_INFO:
  1482. return "INFO";
  1483. case MSG_WARNING:
  1484. return "WARNING";
  1485. case MSG_ERROR:
  1486. return "ERROR";
  1487. default:
  1488. return "?";
  1489. }
  1490. }
  1491. static int str_to_debug_level(const char *s)
  1492. {
  1493. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1494. return MSG_EXCESSIVE;
  1495. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1496. return MSG_MSGDUMP;
  1497. if (os_strcasecmp(s, "DEBUG") == 0)
  1498. return MSG_DEBUG;
  1499. if (os_strcasecmp(s, "INFO") == 0)
  1500. return MSG_INFO;
  1501. if (os_strcasecmp(s, "WARNING") == 0)
  1502. return MSG_WARNING;
  1503. if (os_strcasecmp(s, "ERROR") == 0)
  1504. return MSG_ERROR;
  1505. return -1;
  1506. }
  1507. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1508. char *cmd, char *buf,
  1509. size_t buflen)
  1510. {
  1511. char *pos, *end, *stamp;
  1512. int ret;
  1513. if (cmd == NULL) {
  1514. return -1;
  1515. }
  1516. /* cmd: "LOG_LEVEL [<level>]" */
  1517. if (*cmd == '\0') {
  1518. pos = buf;
  1519. end = buf + buflen;
  1520. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1521. "Timestamp: %d\n",
  1522. debug_level_str(wpa_debug_level),
  1523. wpa_debug_timestamp);
  1524. if (ret < 0 || ret >= end - pos)
  1525. ret = 0;
  1526. return ret;
  1527. }
  1528. while (*cmd == ' ')
  1529. cmd++;
  1530. stamp = os_strchr(cmd, ' ');
  1531. if (stamp) {
  1532. *stamp++ = '\0';
  1533. while (*stamp == ' ') {
  1534. stamp++;
  1535. }
  1536. }
  1537. if (cmd && os_strlen(cmd)) {
  1538. int level = str_to_debug_level(cmd);
  1539. if (level < 0)
  1540. return -1;
  1541. wpa_debug_level = level;
  1542. }
  1543. if (stamp && os_strlen(stamp))
  1544. wpa_debug_timestamp = atoi(stamp);
  1545. os_memcpy(buf, "OK\n", 3);
  1546. return 3;
  1547. }
  1548. static int wpa_supplicant_ctrl_iface_list_networks(
  1549. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1550. {
  1551. char *pos, *end;
  1552. struct wpa_ssid *ssid;
  1553. int ret;
  1554. pos = buf;
  1555. end = buf + buflen;
  1556. ret = os_snprintf(pos, end - pos,
  1557. "network id / ssid / bssid / flags\n");
  1558. if (ret < 0 || ret >= end - pos)
  1559. return pos - buf;
  1560. pos += ret;
  1561. ssid = wpa_s->conf->ssid;
  1562. while (ssid) {
  1563. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1564. ssid->id,
  1565. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1566. if (ret < 0 || ret >= end - pos)
  1567. return pos - buf;
  1568. pos += ret;
  1569. if (ssid->bssid_set) {
  1570. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1571. MAC2STR(ssid->bssid));
  1572. } else {
  1573. ret = os_snprintf(pos, end - pos, "\tany");
  1574. }
  1575. if (ret < 0 || ret >= end - pos)
  1576. return pos - buf;
  1577. pos += ret;
  1578. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1579. ssid == wpa_s->current_ssid ?
  1580. "[CURRENT]" : "",
  1581. ssid->disabled ? "[DISABLED]" : "",
  1582. ssid->disabled_until.sec ?
  1583. "[TEMP-DISABLED]" : "",
  1584. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1585. "");
  1586. if (ret < 0 || ret >= end - pos)
  1587. return pos - buf;
  1588. pos += ret;
  1589. ret = os_snprintf(pos, end - pos, "\n");
  1590. if (ret < 0 || ret >= end - pos)
  1591. return pos - buf;
  1592. pos += ret;
  1593. ssid = ssid->next;
  1594. }
  1595. return pos - buf;
  1596. }
  1597. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1598. {
  1599. int ret;
  1600. ret = os_snprintf(pos, end - pos, "-");
  1601. if (ret < 0 || ret >= end - pos)
  1602. return pos;
  1603. pos += ret;
  1604. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1605. if (ret < 0)
  1606. return pos;
  1607. pos += ret;
  1608. return pos;
  1609. }
  1610. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1611. const u8 *ie, size_t ie_len)
  1612. {
  1613. struct wpa_ie_data data;
  1614. int first, ret;
  1615. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1616. if (ret < 0 || ret >= end - pos)
  1617. return pos;
  1618. pos += ret;
  1619. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1620. ret = os_snprintf(pos, end - pos, "?]");
  1621. if (ret < 0 || ret >= end - pos)
  1622. return pos;
  1623. pos += ret;
  1624. return pos;
  1625. }
  1626. first = 1;
  1627. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1628. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  1629. if (ret < 0 || ret >= end - pos)
  1630. return pos;
  1631. pos += ret;
  1632. first = 0;
  1633. }
  1634. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1635. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  1636. if (ret < 0 || ret >= end - pos)
  1637. return pos;
  1638. pos += ret;
  1639. first = 0;
  1640. }
  1641. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1642. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  1643. if (ret < 0 || ret >= end - pos)
  1644. return pos;
  1645. pos += ret;
  1646. first = 0;
  1647. }
  1648. #ifdef CONFIG_IEEE80211R
  1649. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1650. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1651. first ? "" : "+");
  1652. if (ret < 0 || ret >= end - pos)
  1653. return pos;
  1654. pos += ret;
  1655. first = 0;
  1656. }
  1657. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1658. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1659. first ? "" : "+");
  1660. if (ret < 0 || ret >= end - pos)
  1661. return pos;
  1662. pos += ret;
  1663. first = 0;
  1664. }
  1665. #endif /* CONFIG_IEEE80211R */
  1666. #ifdef CONFIG_IEEE80211W
  1667. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1668. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1669. first ? "" : "+");
  1670. if (ret < 0 || ret >= end - pos)
  1671. return pos;
  1672. pos += ret;
  1673. first = 0;
  1674. }
  1675. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1676. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1677. first ? "" : "+");
  1678. if (ret < 0 || ret >= end - pos)
  1679. return pos;
  1680. pos += ret;
  1681. first = 0;
  1682. }
  1683. #endif /* CONFIG_IEEE80211W */
  1684. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1685. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1686. ret = os_snprintf(pos, end - pos, "-preauth");
  1687. if (ret < 0 || ret >= end - pos)
  1688. return pos;
  1689. pos += ret;
  1690. }
  1691. ret = os_snprintf(pos, end - pos, "]");
  1692. if (ret < 0 || ret >= end - pos)
  1693. return pos;
  1694. pos += ret;
  1695. return pos;
  1696. }
  1697. #ifdef CONFIG_WPS
  1698. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1699. char *pos, char *end,
  1700. struct wpabuf *wps_ie)
  1701. {
  1702. int ret;
  1703. const char *txt;
  1704. if (wps_ie == NULL)
  1705. return pos;
  1706. if (wps_is_selected_pbc_registrar(wps_ie))
  1707. txt = "[WPS-PBC]";
  1708. #ifdef CONFIG_WPS2
  1709. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1710. txt = "[WPS-AUTH]";
  1711. #endif /* CONFIG_WPS2 */
  1712. else if (wps_is_selected_pin_registrar(wps_ie))
  1713. txt = "[WPS-PIN]";
  1714. else
  1715. txt = "[WPS]";
  1716. ret = os_snprintf(pos, end - pos, "%s", txt);
  1717. if (ret >= 0 && ret < end - pos)
  1718. pos += ret;
  1719. wpabuf_free(wps_ie);
  1720. return pos;
  1721. }
  1722. #endif /* CONFIG_WPS */
  1723. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1724. char *pos, char *end,
  1725. const struct wpa_bss *bss)
  1726. {
  1727. #ifdef CONFIG_WPS
  1728. struct wpabuf *wps_ie;
  1729. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1730. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1731. #else /* CONFIG_WPS */
  1732. return pos;
  1733. #endif /* CONFIG_WPS */
  1734. }
  1735. /* Format one result on one text line into a buffer. */
  1736. static int wpa_supplicant_ctrl_iface_scan_result(
  1737. struct wpa_supplicant *wpa_s,
  1738. const struct wpa_bss *bss, char *buf, size_t buflen)
  1739. {
  1740. char *pos, *end;
  1741. int ret;
  1742. const u8 *ie, *ie2, *p2p;
  1743. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1744. if (!p2p)
  1745. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1746. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1747. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1748. 0)
  1749. return 0; /* Do not show P2P listen discovery results here */
  1750. pos = buf;
  1751. end = buf + buflen;
  1752. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1753. MAC2STR(bss->bssid), bss->freq, bss->level);
  1754. if (ret < 0 || ret >= end - pos)
  1755. return -1;
  1756. pos += ret;
  1757. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1758. if (ie)
  1759. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1760. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1761. if (ie2)
  1762. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1763. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1764. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1765. ret = os_snprintf(pos, end - pos, "[WEP]");
  1766. if (ret < 0 || ret >= end - pos)
  1767. return -1;
  1768. pos += ret;
  1769. }
  1770. if (bss->caps & IEEE80211_CAP_IBSS) {
  1771. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1772. if (ret < 0 || ret >= end - pos)
  1773. return -1;
  1774. pos += ret;
  1775. }
  1776. if (bss->caps & IEEE80211_CAP_ESS) {
  1777. ret = os_snprintf(pos, end - pos, "[ESS]");
  1778. if (ret < 0 || ret >= end - pos)
  1779. return -1;
  1780. pos += ret;
  1781. }
  1782. if (p2p) {
  1783. ret = os_snprintf(pos, end - pos, "[P2P]");
  1784. if (ret < 0 || ret >= end - pos)
  1785. return -1;
  1786. pos += ret;
  1787. }
  1788. #ifdef CONFIG_HS20
  1789. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1790. ret = os_snprintf(pos, end - pos, "[HS20]");
  1791. if (ret < 0 || ret >= end - pos)
  1792. return -1;
  1793. pos += ret;
  1794. }
  1795. #endif /* CONFIG_HS20 */
  1796. ret = os_snprintf(pos, end - pos, "\t%s",
  1797. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1798. if (ret < 0 || ret >= end - pos)
  1799. return -1;
  1800. pos += ret;
  1801. ret = os_snprintf(pos, end - pos, "\n");
  1802. if (ret < 0 || ret >= end - pos)
  1803. return -1;
  1804. pos += ret;
  1805. return pos - buf;
  1806. }
  1807. static int wpa_supplicant_ctrl_iface_scan_results(
  1808. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1809. {
  1810. char *pos, *end;
  1811. struct wpa_bss *bss;
  1812. int ret;
  1813. pos = buf;
  1814. end = buf + buflen;
  1815. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1816. "flags / ssid\n");
  1817. if (ret < 0 || ret >= end - pos)
  1818. return pos - buf;
  1819. pos += ret;
  1820. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1821. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1822. end - pos);
  1823. if (ret < 0 || ret >= end - pos)
  1824. return pos - buf;
  1825. pos += ret;
  1826. }
  1827. return pos - buf;
  1828. }
  1829. static int wpa_supplicant_ctrl_iface_select_network(
  1830. struct wpa_supplicant *wpa_s, char *cmd)
  1831. {
  1832. int id;
  1833. struct wpa_ssid *ssid;
  1834. /* cmd: "<network id>" or "any" */
  1835. if (os_strcmp(cmd, "any") == 0) {
  1836. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1837. ssid = NULL;
  1838. } else {
  1839. id = atoi(cmd);
  1840. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1841. ssid = wpa_config_get_network(wpa_s->conf, id);
  1842. if (ssid == NULL) {
  1843. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1844. "network id=%d", id);
  1845. return -1;
  1846. }
  1847. if (ssid->disabled == 2) {
  1848. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1849. "SELECT_NETWORK with persistent P2P group");
  1850. return -1;
  1851. }
  1852. }
  1853. wpa_supplicant_select_network(wpa_s, ssid);
  1854. return 0;
  1855. }
  1856. static int wpa_supplicant_ctrl_iface_enable_network(
  1857. struct wpa_supplicant *wpa_s, char *cmd)
  1858. {
  1859. int id;
  1860. struct wpa_ssid *ssid;
  1861. /* cmd: "<network id>" or "all" */
  1862. if (os_strcmp(cmd, "all") == 0) {
  1863. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  1864. ssid = NULL;
  1865. } else {
  1866. id = atoi(cmd);
  1867. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  1868. ssid = wpa_config_get_network(wpa_s->conf, id);
  1869. if (ssid == NULL) {
  1870. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1871. "network id=%d", id);
  1872. return -1;
  1873. }
  1874. if (ssid->disabled == 2) {
  1875. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1876. "ENABLE_NETWORK with persistent P2P group");
  1877. return -1;
  1878. }
  1879. if (os_strstr(cmd, " no-connect")) {
  1880. ssid->disabled = 0;
  1881. return 0;
  1882. }
  1883. }
  1884. wpa_supplicant_enable_network(wpa_s, ssid);
  1885. return 0;
  1886. }
  1887. static int wpa_supplicant_ctrl_iface_disable_network(
  1888. struct wpa_supplicant *wpa_s, char *cmd)
  1889. {
  1890. int id;
  1891. struct wpa_ssid *ssid;
  1892. /* cmd: "<network id>" or "all" */
  1893. if (os_strcmp(cmd, "all") == 0) {
  1894. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1895. ssid = NULL;
  1896. } else {
  1897. id = atoi(cmd);
  1898. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1899. ssid = wpa_config_get_network(wpa_s->conf, id);
  1900. if (ssid == NULL) {
  1901. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1902. "network id=%d", id);
  1903. return -1;
  1904. }
  1905. if (ssid->disabled == 2) {
  1906. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1907. "DISABLE_NETWORK with persistent P2P "
  1908. "group");
  1909. return -1;
  1910. }
  1911. }
  1912. wpa_supplicant_disable_network(wpa_s, ssid);
  1913. return 0;
  1914. }
  1915. static int wpa_supplicant_ctrl_iface_add_network(
  1916. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1917. {
  1918. struct wpa_ssid *ssid;
  1919. int ret;
  1920. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1921. ssid = wpa_config_add_network(wpa_s->conf);
  1922. if (ssid == NULL)
  1923. return -1;
  1924. wpas_notify_network_added(wpa_s, ssid);
  1925. ssid->disabled = 1;
  1926. wpa_config_set_network_defaults(ssid);
  1927. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1928. if (ret < 0 || (size_t) ret >= buflen)
  1929. return -1;
  1930. return ret;
  1931. }
  1932. static int wpa_supplicant_ctrl_iface_remove_network(
  1933. struct wpa_supplicant *wpa_s, char *cmd)
  1934. {
  1935. int id;
  1936. struct wpa_ssid *ssid;
  1937. int was_disabled;
  1938. /* cmd: "<network id>" or "all" */
  1939. if (os_strcmp(cmd, "all") == 0) {
  1940. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1941. if (wpa_s->sched_scanning)
  1942. wpa_supplicant_cancel_sched_scan(wpa_s);
  1943. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1944. if (wpa_s->current_ssid) {
  1945. #ifdef CONFIG_SME
  1946. wpa_s->sme.prev_bssid_set = 0;
  1947. #endif /* CONFIG_SME */
  1948. wpa_sm_set_config(wpa_s->wpa, NULL);
  1949. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1950. wpa_supplicant_deauthenticate(
  1951. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1952. }
  1953. ssid = wpa_s->conf->ssid;
  1954. while (ssid) {
  1955. struct wpa_ssid *remove_ssid = ssid;
  1956. id = ssid->id;
  1957. ssid = ssid->next;
  1958. wpas_notify_network_removed(wpa_s, remove_ssid);
  1959. wpa_config_remove_network(wpa_s->conf, id);
  1960. }
  1961. return 0;
  1962. }
  1963. id = atoi(cmd);
  1964. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1965. ssid = wpa_config_get_network(wpa_s->conf, id);
  1966. if (ssid)
  1967. wpas_notify_network_removed(wpa_s, ssid);
  1968. if (ssid == NULL) {
  1969. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1970. "id=%d", id);
  1971. return -1;
  1972. }
  1973. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  1974. #ifdef CONFIG_SME
  1975. wpa_s->sme.prev_bssid_set = 0;
  1976. #endif /* CONFIG_SME */
  1977. /*
  1978. * Invalidate the EAP session cache if the current or
  1979. * previously used network is removed.
  1980. */
  1981. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1982. }
  1983. if (ssid == wpa_s->current_ssid) {
  1984. wpa_sm_set_config(wpa_s->wpa, NULL);
  1985. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1986. wpa_supplicant_deauthenticate(wpa_s,
  1987. WLAN_REASON_DEAUTH_LEAVING);
  1988. }
  1989. was_disabled = ssid->disabled;
  1990. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  1991. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  1992. "network id=%d", id);
  1993. return -1;
  1994. }
  1995. if (!was_disabled && wpa_s->sched_scanning) {
  1996. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  1997. "network from filters");
  1998. wpa_supplicant_cancel_sched_scan(wpa_s);
  1999. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2000. }
  2001. return 0;
  2002. }
  2003. static int wpa_supplicant_ctrl_iface_set_network(
  2004. struct wpa_supplicant *wpa_s, char *cmd)
  2005. {
  2006. int id;
  2007. struct wpa_ssid *ssid;
  2008. char *name, *value;
  2009. /* cmd: "<network id> <variable name> <value>" */
  2010. name = os_strchr(cmd, ' ');
  2011. if (name == NULL)
  2012. return -1;
  2013. *name++ = '\0';
  2014. value = os_strchr(name, ' ');
  2015. if (value == NULL)
  2016. return -1;
  2017. *value++ = '\0';
  2018. id = atoi(cmd);
  2019. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2020. id, name);
  2021. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2022. (u8 *) value, os_strlen(value));
  2023. ssid = wpa_config_get_network(wpa_s->conf, id);
  2024. if (ssid == NULL) {
  2025. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2026. "id=%d", id);
  2027. return -1;
  2028. }
  2029. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2030. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2031. "variable '%s'", name);
  2032. return -1;
  2033. }
  2034. if (os_strcmp(name, "bssid") != 0 &&
  2035. os_strcmp(name, "priority") != 0)
  2036. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2037. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2038. /*
  2039. * Invalidate the EAP session cache if anything in the current
  2040. * or previously used configuration changes.
  2041. */
  2042. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2043. }
  2044. if ((os_strcmp(name, "psk") == 0 &&
  2045. value[0] == '"' && ssid->ssid_len) ||
  2046. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2047. wpa_config_update_psk(ssid);
  2048. else if (os_strcmp(name, "priority") == 0)
  2049. wpa_config_update_prio_list(wpa_s->conf);
  2050. return 0;
  2051. }
  2052. static int wpa_supplicant_ctrl_iface_get_network(
  2053. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2054. {
  2055. int id;
  2056. size_t res;
  2057. struct wpa_ssid *ssid;
  2058. char *name, *value;
  2059. /* cmd: "<network id> <variable name>" */
  2060. name = os_strchr(cmd, ' ');
  2061. if (name == NULL || buflen == 0)
  2062. return -1;
  2063. *name++ = '\0';
  2064. id = atoi(cmd);
  2065. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2066. id, name);
  2067. ssid = wpa_config_get_network(wpa_s->conf, id);
  2068. if (ssid == NULL) {
  2069. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2070. "id=%d", id);
  2071. return -1;
  2072. }
  2073. value = wpa_config_get_no_key(ssid, name);
  2074. if (value == NULL) {
  2075. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2076. "variable '%s'", name);
  2077. return -1;
  2078. }
  2079. res = os_strlcpy(buf, value, buflen);
  2080. if (res >= buflen) {
  2081. os_free(value);
  2082. return -1;
  2083. }
  2084. os_free(value);
  2085. return res;
  2086. }
  2087. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2088. char *buf, size_t buflen)
  2089. {
  2090. char *pos, *end;
  2091. struct wpa_cred *cred;
  2092. int ret;
  2093. pos = buf;
  2094. end = buf + buflen;
  2095. ret = os_snprintf(pos, end - pos,
  2096. "cred id / realm / username / domain / imsi\n");
  2097. if (ret < 0 || ret >= end - pos)
  2098. return pos - buf;
  2099. pos += ret;
  2100. cred = wpa_s->conf->cred;
  2101. while (cred) {
  2102. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2103. cred->id, cred->realm ? cred->realm : "",
  2104. cred->username ? cred->username : "",
  2105. cred->domain ? cred->domain[0] : "",
  2106. cred->imsi ? cred->imsi : "");
  2107. if (ret < 0 || ret >= end - pos)
  2108. return pos - buf;
  2109. pos += ret;
  2110. cred = cred->next;
  2111. }
  2112. return pos - buf;
  2113. }
  2114. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2115. char *buf, size_t buflen)
  2116. {
  2117. struct wpa_cred *cred;
  2118. int ret;
  2119. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2120. cred = wpa_config_add_cred(wpa_s->conf);
  2121. if (cred == NULL)
  2122. return -1;
  2123. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2124. if (ret < 0 || (size_t) ret >= buflen)
  2125. return -1;
  2126. return ret;
  2127. }
  2128. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2129. struct wpa_cred *cred)
  2130. {
  2131. struct wpa_ssid *ssid;
  2132. char str[20];
  2133. if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
  2134. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2135. return -1;
  2136. }
  2137. /* Remove any network entry created based on the removed credential */
  2138. ssid = wpa_s->conf->ssid;
  2139. while (ssid) {
  2140. if (ssid->parent_cred == cred) {
  2141. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2142. "used the removed credential", ssid->id);
  2143. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2144. ssid = ssid->next;
  2145. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2146. } else
  2147. ssid = ssid->next;
  2148. }
  2149. return 0;
  2150. }
  2151. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2152. char *cmd)
  2153. {
  2154. int id;
  2155. struct wpa_cred *cred, *prev;
  2156. /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
  2157. if (os_strcmp(cmd, "all") == 0) {
  2158. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2159. cred = wpa_s->conf->cred;
  2160. while (cred) {
  2161. prev = cred;
  2162. cred = cred->next;
  2163. wpas_ctrl_remove_cred(wpa_s, prev);
  2164. }
  2165. return 0;
  2166. }
  2167. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2168. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2169. cmd + 8);
  2170. cred = wpa_s->conf->cred;
  2171. while (cred) {
  2172. prev = cred;
  2173. cred = cred->next;
  2174. if (prev->domain) {
  2175. size_t i;
  2176. for (i = 0; i < prev->num_domain; i++) {
  2177. if (os_strcmp(prev->domain[i], cmd + 8)
  2178. != 0)
  2179. continue;
  2180. wpas_ctrl_remove_cred(wpa_s, prev);
  2181. break;
  2182. }
  2183. }
  2184. }
  2185. return 0;
  2186. }
  2187. id = atoi(cmd);
  2188. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2189. cred = wpa_config_get_cred(wpa_s->conf, id);
  2190. return wpas_ctrl_remove_cred(wpa_s, cred);
  2191. }
  2192. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2193. char *cmd)
  2194. {
  2195. int id;
  2196. struct wpa_cred *cred;
  2197. char *name, *value;
  2198. /* cmd: "<cred id> <variable name> <value>" */
  2199. name = os_strchr(cmd, ' ');
  2200. if (name == NULL)
  2201. return -1;
  2202. *name++ = '\0';
  2203. value = os_strchr(name, ' ');
  2204. if (value == NULL)
  2205. return -1;
  2206. *value++ = '\0';
  2207. id = atoi(cmd);
  2208. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2209. id, name);
  2210. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2211. (u8 *) value, os_strlen(value));
  2212. cred = wpa_config_get_cred(wpa_s->conf, id);
  2213. if (cred == NULL) {
  2214. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2215. id);
  2216. return -1;
  2217. }
  2218. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2219. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2220. "variable '%s'", name);
  2221. return -1;
  2222. }
  2223. return 0;
  2224. }
  2225. #ifndef CONFIG_NO_CONFIG_WRITE
  2226. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2227. {
  2228. int ret;
  2229. if (!wpa_s->conf->update_config) {
  2230. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2231. "to update configuration (update_config=0)");
  2232. return -1;
  2233. }
  2234. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2235. if (ret) {
  2236. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2237. "update configuration");
  2238. } else {
  2239. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2240. " updated");
  2241. }
  2242. return ret;
  2243. }
  2244. #endif /* CONFIG_NO_CONFIG_WRITE */
  2245. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2246. struct wpa_driver_capa *capa,
  2247. char *buf, size_t buflen)
  2248. {
  2249. int ret, first = 1;
  2250. char *pos, *end;
  2251. size_t len;
  2252. pos = buf;
  2253. end = pos + buflen;
  2254. if (res < 0) {
  2255. if (strict)
  2256. return 0;
  2257. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2258. if (len >= buflen)
  2259. return -1;
  2260. return len;
  2261. }
  2262. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2263. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2264. if (ret < 0 || ret >= end - pos)
  2265. return pos - buf;
  2266. pos += ret;
  2267. first = 0;
  2268. }
  2269. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2270. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2271. if (ret < 0 || ret >= end - pos)
  2272. return pos - buf;
  2273. pos += ret;
  2274. first = 0;
  2275. }
  2276. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2277. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2278. if (ret < 0 || ret >= end - pos)
  2279. return pos - buf;
  2280. pos += ret;
  2281. first = 0;
  2282. }
  2283. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2284. ret = os_snprintf(pos, end - pos, "%sNONE", first ? "" : " ");
  2285. if (ret < 0 || ret >= end - pos)
  2286. return pos - buf;
  2287. pos += ret;
  2288. first = 0;
  2289. }
  2290. return pos - buf;
  2291. }
  2292. static int ctrl_iface_get_capability_group(int res, char *strict,
  2293. struct wpa_driver_capa *capa,
  2294. char *buf, size_t buflen)
  2295. {
  2296. int ret, first = 1;
  2297. char *pos, *end;
  2298. size_t len;
  2299. pos = buf;
  2300. end = pos + buflen;
  2301. if (res < 0) {
  2302. if (strict)
  2303. return 0;
  2304. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2305. if (len >= buflen)
  2306. return -1;
  2307. return len;
  2308. }
  2309. if (capa->enc & WPA_DRIVER_CAPA_ENC_CCMP) {
  2310. ret = os_snprintf(pos, end - pos, "%sCCMP", first ? "" : " ");
  2311. if (ret < 0 || ret >= end - pos)
  2312. return pos - buf;
  2313. pos += ret;
  2314. first = 0;
  2315. }
  2316. if (capa->enc & WPA_DRIVER_CAPA_ENC_GCMP) {
  2317. ret = os_snprintf(pos, end - pos, "%sGCMP", first ? "" : " ");
  2318. if (ret < 0 || ret >= end - pos)
  2319. return pos - buf;
  2320. pos += ret;
  2321. first = 0;
  2322. }
  2323. if (capa->enc & WPA_DRIVER_CAPA_ENC_TKIP) {
  2324. ret = os_snprintf(pos, end - pos, "%sTKIP", first ? "" : " ");
  2325. if (ret < 0 || ret >= end - pos)
  2326. return pos - buf;
  2327. pos += ret;
  2328. first = 0;
  2329. }
  2330. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP104) {
  2331. ret = os_snprintf(pos, end - pos, "%sWEP104",
  2332. first ? "" : " ");
  2333. if (ret < 0 || ret >= end - pos)
  2334. return pos - buf;
  2335. pos += ret;
  2336. first = 0;
  2337. }
  2338. if (capa->enc & WPA_DRIVER_CAPA_ENC_WEP40) {
  2339. ret = os_snprintf(pos, end - pos, "%sWEP40", first ? "" : " ");
  2340. if (ret < 0 || ret >= end - pos)
  2341. return pos - buf;
  2342. pos += ret;
  2343. first = 0;
  2344. }
  2345. return pos - buf;
  2346. }
  2347. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2348. struct wpa_driver_capa *capa,
  2349. char *buf, size_t buflen)
  2350. {
  2351. int ret;
  2352. char *pos, *end;
  2353. size_t len;
  2354. pos = buf;
  2355. end = pos + buflen;
  2356. if (res < 0) {
  2357. if (strict)
  2358. return 0;
  2359. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2360. "NONE", buflen);
  2361. if (len >= buflen)
  2362. return -1;
  2363. return len;
  2364. }
  2365. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2366. if (ret < 0 || ret >= end - pos)
  2367. return pos - buf;
  2368. pos += ret;
  2369. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2370. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2371. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2372. if (ret < 0 || ret >= end - pos)
  2373. return pos - buf;
  2374. pos += ret;
  2375. }
  2376. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2377. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2378. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2379. if (ret < 0 || ret >= end - pos)
  2380. return pos - buf;
  2381. pos += ret;
  2382. }
  2383. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2384. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2385. if (ret < 0 || ret >= end - pos)
  2386. return pos - buf;
  2387. pos += ret;
  2388. }
  2389. return pos - buf;
  2390. }
  2391. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2392. struct wpa_driver_capa *capa,
  2393. char *buf, size_t buflen)
  2394. {
  2395. int ret, first = 1;
  2396. char *pos, *end;
  2397. size_t len;
  2398. pos = buf;
  2399. end = pos + buflen;
  2400. if (res < 0) {
  2401. if (strict)
  2402. return 0;
  2403. len = os_strlcpy(buf, "RSN WPA", buflen);
  2404. if (len >= buflen)
  2405. return -1;
  2406. return len;
  2407. }
  2408. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2409. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2410. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  2411. if (ret < 0 || ret >= end - pos)
  2412. return pos - buf;
  2413. pos += ret;
  2414. first = 0;
  2415. }
  2416. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2417. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2418. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  2419. if (ret < 0 || ret >= end - pos)
  2420. return pos - buf;
  2421. pos += ret;
  2422. first = 0;
  2423. }
  2424. return pos - buf;
  2425. }
  2426. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2427. struct wpa_driver_capa *capa,
  2428. char *buf, size_t buflen)
  2429. {
  2430. int ret, first = 1;
  2431. char *pos, *end;
  2432. size_t len;
  2433. pos = buf;
  2434. end = pos + buflen;
  2435. if (res < 0) {
  2436. if (strict)
  2437. return 0;
  2438. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2439. if (len >= buflen)
  2440. return -1;
  2441. return len;
  2442. }
  2443. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2444. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  2445. if (ret < 0 || ret >= end - pos)
  2446. return pos - buf;
  2447. pos += ret;
  2448. first = 0;
  2449. }
  2450. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2451. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2452. first ? "" : " ");
  2453. if (ret < 0 || ret >= end - pos)
  2454. return pos - buf;
  2455. pos += ret;
  2456. first = 0;
  2457. }
  2458. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2459. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  2460. if (ret < 0 || ret >= end - pos)
  2461. return pos - buf;
  2462. pos += ret;
  2463. first = 0;
  2464. }
  2465. return pos - buf;
  2466. }
  2467. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2468. struct wpa_driver_capa *capa,
  2469. char *buf, size_t buflen)
  2470. {
  2471. int ret, first = 1;
  2472. char *pos, *end;
  2473. size_t len;
  2474. pos = buf;
  2475. end = pos + buflen;
  2476. if (res < 0) {
  2477. if (strict)
  2478. return 0;
  2479. len = os_strlcpy(buf, "IBSS AP", buflen);
  2480. if (len >= buflen)
  2481. return -1;
  2482. return len;
  2483. }
  2484. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2485. ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
  2486. if (ret < 0 || ret >= end - pos)
  2487. return pos - buf;
  2488. pos += ret;
  2489. first = 0;
  2490. }
  2491. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2492. ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
  2493. if (ret < 0 || ret >= end - pos)
  2494. return pos - buf;
  2495. pos += ret;
  2496. first = 0;
  2497. }
  2498. return pos - buf;
  2499. }
  2500. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2501. char *buf, size_t buflen)
  2502. {
  2503. struct hostapd_channel_data *chnl;
  2504. int ret, i, j;
  2505. char *pos, *end, *hmode;
  2506. pos = buf;
  2507. end = pos + buflen;
  2508. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2509. switch (wpa_s->hw.modes[j].mode) {
  2510. case HOSTAPD_MODE_IEEE80211B:
  2511. hmode = "B";
  2512. break;
  2513. case HOSTAPD_MODE_IEEE80211G:
  2514. hmode = "G";
  2515. break;
  2516. case HOSTAPD_MODE_IEEE80211A:
  2517. hmode = "A";
  2518. break;
  2519. case HOSTAPD_MODE_IEEE80211AD:
  2520. hmode = "AD";
  2521. break;
  2522. default:
  2523. continue;
  2524. }
  2525. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2526. if (ret < 0 || ret >= end - pos)
  2527. return pos - buf;
  2528. pos += ret;
  2529. chnl = wpa_s->hw.modes[j].channels;
  2530. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2531. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2532. continue;
  2533. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2534. if (ret < 0 || ret >= end - pos)
  2535. return pos - buf;
  2536. pos += ret;
  2537. }
  2538. ret = os_snprintf(pos, end - pos, "\n");
  2539. if (ret < 0 || ret >= end - pos)
  2540. return pos - buf;
  2541. pos += ret;
  2542. }
  2543. return pos - buf;
  2544. }
  2545. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2546. char *buf, size_t buflen)
  2547. {
  2548. struct hostapd_channel_data *chnl;
  2549. int ret, i, j;
  2550. char *pos, *end, *hmode;
  2551. pos = buf;
  2552. end = pos + buflen;
  2553. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2554. switch (wpa_s->hw.modes[j].mode) {
  2555. case HOSTAPD_MODE_IEEE80211B:
  2556. hmode = "B";
  2557. break;
  2558. case HOSTAPD_MODE_IEEE80211G:
  2559. hmode = "G";
  2560. break;
  2561. case HOSTAPD_MODE_IEEE80211A:
  2562. hmode = "A";
  2563. break;
  2564. case HOSTAPD_MODE_IEEE80211AD:
  2565. hmode = "AD";
  2566. break;
  2567. default:
  2568. continue;
  2569. }
  2570. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2571. hmode);
  2572. if (ret < 0 || ret >= end - pos)
  2573. return pos - buf;
  2574. pos += ret;
  2575. chnl = wpa_s->hw.modes[j].channels;
  2576. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2577. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2578. continue;
  2579. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
  2580. chnl[i].chan, chnl[i].freq,
  2581. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2582. " (NO_IBSS)" : "");
  2583. if (ret < 0 || ret >= end - pos)
  2584. return pos - buf;
  2585. pos += ret;
  2586. }
  2587. ret = os_snprintf(pos, end - pos, "\n");
  2588. if (ret < 0 || ret >= end - pos)
  2589. return pos - buf;
  2590. pos += ret;
  2591. }
  2592. return pos - buf;
  2593. }
  2594. static int wpa_supplicant_ctrl_iface_get_capability(
  2595. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2596. size_t buflen)
  2597. {
  2598. struct wpa_driver_capa capa;
  2599. int res;
  2600. char *strict;
  2601. char field[30];
  2602. size_t len;
  2603. /* Determine whether or not strict checking was requested */
  2604. len = os_strlcpy(field, _field, sizeof(field));
  2605. if (len >= sizeof(field))
  2606. return -1;
  2607. strict = os_strchr(field, ' ');
  2608. if (strict != NULL) {
  2609. *strict++ = '\0';
  2610. if (os_strcmp(strict, "strict") != 0)
  2611. return -1;
  2612. }
  2613. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2614. field, strict ? strict : "");
  2615. if (os_strcmp(field, "eap") == 0) {
  2616. return eap_get_names(buf, buflen);
  2617. }
  2618. res = wpa_drv_get_capa(wpa_s, &capa);
  2619. if (os_strcmp(field, "pairwise") == 0)
  2620. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2621. buf, buflen);
  2622. if (os_strcmp(field, "group") == 0)
  2623. return ctrl_iface_get_capability_group(res, strict, &capa,
  2624. buf, buflen);
  2625. if (os_strcmp(field, "key_mgmt") == 0)
  2626. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2627. buf, buflen);
  2628. if (os_strcmp(field, "proto") == 0)
  2629. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2630. buf, buflen);
  2631. if (os_strcmp(field, "auth_alg") == 0)
  2632. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2633. buf, buflen);
  2634. if (os_strcmp(field, "modes") == 0)
  2635. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2636. buf, buflen);
  2637. if (os_strcmp(field, "channels") == 0)
  2638. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2639. if (os_strcmp(field, "freq") == 0)
  2640. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2641. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2642. field);
  2643. return -1;
  2644. }
  2645. #ifdef CONFIG_INTERWORKING
  2646. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2647. struct wpabuf *data)
  2648. {
  2649. char *start = pos;
  2650. size_t i;
  2651. int ret;
  2652. const u8 *d;
  2653. if (data == NULL)
  2654. return start;
  2655. ret = os_snprintf(pos, end - pos, "%s=", title);
  2656. if (ret < 0 || ret >= end - pos)
  2657. return start;
  2658. pos += ret;
  2659. d = wpabuf_head_u8(data);
  2660. for (i = 0; i < wpabuf_len(data); i++) {
  2661. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2662. if (ret < 0 || ret >= end - pos)
  2663. return start;
  2664. pos += ret;
  2665. }
  2666. ret = os_snprintf(pos, end - pos, "\n");
  2667. if (ret < 0 || ret >= end - pos)
  2668. return start;
  2669. pos += ret;
  2670. return pos;
  2671. }
  2672. #endif /* CONFIG_INTERWORKING */
  2673. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2674. unsigned long mask, char *buf, size_t buflen)
  2675. {
  2676. size_t i;
  2677. int ret;
  2678. char *pos, *end;
  2679. const u8 *ie, *ie2;
  2680. pos = buf;
  2681. end = buf + buflen;
  2682. if (mask & WPA_BSS_MASK_ID) {
  2683. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2684. if (ret < 0 || ret >= end - pos)
  2685. return 0;
  2686. pos += ret;
  2687. }
  2688. if (mask & WPA_BSS_MASK_BSSID) {
  2689. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2690. MAC2STR(bss->bssid));
  2691. if (ret < 0 || ret >= end - pos)
  2692. return 0;
  2693. pos += ret;
  2694. }
  2695. if (mask & WPA_BSS_MASK_FREQ) {
  2696. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2697. if (ret < 0 || ret >= end - pos)
  2698. return 0;
  2699. pos += ret;
  2700. }
  2701. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2702. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2703. bss->beacon_int);
  2704. if (ret < 0 || ret >= end - pos)
  2705. return 0;
  2706. pos += ret;
  2707. }
  2708. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2709. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2710. bss->caps);
  2711. if (ret < 0 || ret >= end - pos)
  2712. return 0;
  2713. pos += ret;
  2714. }
  2715. if (mask & WPA_BSS_MASK_QUAL) {
  2716. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2717. if (ret < 0 || ret >= end - pos)
  2718. return 0;
  2719. pos += ret;
  2720. }
  2721. if (mask & WPA_BSS_MASK_NOISE) {
  2722. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2723. if (ret < 0 || ret >= end - pos)
  2724. return 0;
  2725. pos += ret;
  2726. }
  2727. if (mask & WPA_BSS_MASK_LEVEL) {
  2728. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2729. if (ret < 0 || ret >= end - pos)
  2730. return 0;
  2731. pos += ret;
  2732. }
  2733. if (mask & WPA_BSS_MASK_TSF) {
  2734. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2735. (unsigned long long) bss->tsf);
  2736. if (ret < 0 || ret >= end - pos)
  2737. return 0;
  2738. pos += ret;
  2739. }
  2740. if (mask & WPA_BSS_MASK_AGE) {
  2741. struct os_time now;
  2742. os_get_time(&now);
  2743. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2744. (int) (now.sec - bss->last_update.sec));
  2745. if (ret < 0 || ret >= end - pos)
  2746. return 0;
  2747. pos += ret;
  2748. }
  2749. if (mask & WPA_BSS_MASK_IE) {
  2750. ret = os_snprintf(pos, end - pos, "ie=");
  2751. if (ret < 0 || ret >= end - pos)
  2752. return 0;
  2753. pos += ret;
  2754. ie = (const u8 *) (bss + 1);
  2755. for (i = 0; i < bss->ie_len; i++) {
  2756. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2757. if (ret < 0 || ret >= end - pos)
  2758. return 0;
  2759. pos += ret;
  2760. }
  2761. ret = os_snprintf(pos, end - pos, "\n");
  2762. if (ret < 0 || ret >= end - pos)
  2763. return 0;
  2764. pos += ret;
  2765. }
  2766. if (mask & WPA_BSS_MASK_FLAGS) {
  2767. ret = os_snprintf(pos, end - pos, "flags=");
  2768. if (ret < 0 || ret >= end - pos)
  2769. return 0;
  2770. pos += ret;
  2771. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2772. if (ie)
  2773. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2774. 2 + ie[1]);
  2775. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2776. if (ie2)
  2777. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2778. 2 + ie2[1]);
  2779. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2780. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2781. ret = os_snprintf(pos, end - pos, "[WEP]");
  2782. if (ret < 0 || ret >= end - pos)
  2783. return 0;
  2784. pos += ret;
  2785. }
  2786. if (bss->caps & IEEE80211_CAP_IBSS) {
  2787. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2788. if (ret < 0 || ret >= end - pos)
  2789. return 0;
  2790. pos += ret;
  2791. }
  2792. if (bss->caps & IEEE80211_CAP_ESS) {
  2793. ret = os_snprintf(pos, end - pos, "[ESS]");
  2794. if (ret < 0 || ret >= end - pos)
  2795. return 0;
  2796. pos += ret;
  2797. }
  2798. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  2799. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  2800. ret = os_snprintf(pos, end - pos, "[P2P]");
  2801. if (ret < 0 || ret >= end - pos)
  2802. return 0;
  2803. pos += ret;
  2804. }
  2805. #ifdef CONFIG_HS20
  2806. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2807. ret = os_snprintf(pos, end - pos, "[HS20]");
  2808. if (ret < 0 || ret >= end - pos)
  2809. return 0;
  2810. pos += ret;
  2811. }
  2812. #endif /* CONFIG_HS20 */
  2813. ret = os_snprintf(pos, end - pos, "\n");
  2814. if (ret < 0 || ret >= end - pos)
  2815. return 0;
  2816. pos += ret;
  2817. }
  2818. if (mask & WPA_BSS_MASK_SSID) {
  2819. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2820. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2821. if (ret < 0 || ret >= end - pos)
  2822. return 0;
  2823. pos += ret;
  2824. }
  2825. #ifdef CONFIG_WPS
  2826. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2827. ie = (const u8 *) (bss + 1);
  2828. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2829. if (ret < 0 || ret >= end - pos)
  2830. return 0;
  2831. pos += ret;
  2832. }
  2833. #endif /* CONFIG_WPS */
  2834. #ifdef CONFIG_P2P
  2835. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2836. ie = (const u8 *) (bss + 1);
  2837. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2838. if (ret < 0 || ret >= end - pos)
  2839. return 0;
  2840. pos += ret;
  2841. }
  2842. #endif /* CONFIG_P2P */
  2843. #ifdef CONFIG_WIFI_DISPLAY
  2844. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  2845. struct wpabuf *wfd;
  2846. ie = (const u8 *) (bss + 1);
  2847. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  2848. WFD_IE_VENDOR_TYPE);
  2849. if (wfd) {
  2850. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  2851. if (ret < 0 || ret >= end - pos)
  2852. return 0;
  2853. pos += ret;
  2854. pos += wpa_snprintf_hex(pos, end - pos,
  2855. wpabuf_head(wfd),
  2856. wpabuf_len(wfd));
  2857. wpabuf_free(wfd);
  2858. ret = os_snprintf(pos, end - pos, "\n");
  2859. if (ret < 0 || ret >= end - pos)
  2860. return 0;
  2861. pos += ret;
  2862. }
  2863. }
  2864. #endif /* CONFIG_WIFI_DISPLAY */
  2865. #ifdef CONFIG_INTERWORKING
  2866. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  2867. struct wpa_bss_anqp *anqp = bss->anqp;
  2868. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  2869. anqp->venue_name);
  2870. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2871. anqp->network_auth_type);
  2872. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2873. anqp->roaming_consortium);
  2874. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2875. anqp->ip_addr_type_availability);
  2876. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  2877. anqp->nai_realm);
  2878. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  2879. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2880. anqp->domain_name);
  2881. #ifdef CONFIG_HS20
  2882. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  2883. anqp->hs20_operator_friendly_name);
  2884. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  2885. anqp->hs20_wan_metrics);
  2886. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  2887. anqp->hs20_connection_capability);
  2888. #endif /* CONFIG_HS20 */
  2889. }
  2890. #endif /* CONFIG_INTERWORKING */
  2891. if (mask & WPA_BSS_MASK_DELIM) {
  2892. ret = os_snprintf(pos, end - pos, "====\n");
  2893. if (ret < 0 || ret >= end - pos)
  2894. return 0;
  2895. pos += ret;
  2896. }
  2897. return pos - buf;
  2898. }
  2899. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2900. const char *cmd, char *buf,
  2901. size_t buflen)
  2902. {
  2903. u8 bssid[ETH_ALEN];
  2904. size_t i;
  2905. struct wpa_bss *bss;
  2906. struct wpa_bss *bsslast = NULL;
  2907. struct dl_list *next;
  2908. int ret = 0;
  2909. int len;
  2910. char *ctmp;
  2911. unsigned long mask = WPA_BSS_MASK_ALL;
  2912. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  2913. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  2914. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  2915. list_id);
  2916. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  2917. list_id);
  2918. } else { /* N1-N2 */
  2919. unsigned int id1, id2;
  2920. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  2921. wpa_printf(MSG_INFO, "Wrong BSS range "
  2922. "format");
  2923. return 0;
  2924. }
  2925. if (*(cmd + 6) == '-')
  2926. id1 = 0;
  2927. else
  2928. id1 = atoi(cmd + 6);
  2929. ctmp++;
  2930. if (*ctmp >= '0' && *ctmp <= '9')
  2931. id2 = atoi(ctmp);
  2932. else
  2933. id2 = (unsigned int) -1;
  2934. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  2935. if (id2 == (unsigned int) -1)
  2936. bsslast = dl_list_last(&wpa_s->bss_id,
  2937. struct wpa_bss,
  2938. list_id);
  2939. else {
  2940. bsslast = wpa_bss_get_id(wpa_s, id2);
  2941. if (bsslast == NULL && bss && id2 > id1) {
  2942. struct wpa_bss *tmp = bss;
  2943. for (;;) {
  2944. next = tmp->list_id.next;
  2945. if (next == &wpa_s->bss_id)
  2946. break;
  2947. tmp = dl_list_entry(
  2948. next, struct wpa_bss,
  2949. list_id);
  2950. if (tmp->id > id2)
  2951. break;
  2952. bsslast = tmp;
  2953. }
  2954. }
  2955. }
  2956. }
  2957. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  2958. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  2959. else if (os_strncmp(cmd, "LAST", 4) == 0)
  2960. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  2961. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2962. i = atoi(cmd + 3);
  2963. bss = wpa_bss_get_id(wpa_s, i);
  2964. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2965. i = atoi(cmd + 5);
  2966. bss = wpa_bss_get_id(wpa_s, i);
  2967. if (bss) {
  2968. next = bss->list_id.next;
  2969. if (next == &wpa_s->bss_id)
  2970. bss = NULL;
  2971. else
  2972. bss = dl_list_entry(next, struct wpa_bss,
  2973. list_id);
  2974. }
  2975. #ifdef CONFIG_P2P
  2976. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2977. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2978. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2979. else
  2980. bss = NULL;
  2981. #endif /* CONFIG_P2P */
  2982. } else if (hwaddr_aton(cmd, bssid) == 0)
  2983. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2984. else {
  2985. struct wpa_bss *tmp;
  2986. i = atoi(cmd);
  2987. bss = NULL;
  2988. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2989. {
  2990. if (i-- == 0) {
  2991. bss = tmp;
  2992. break;
  2993. }
  2994. }
  2995. }
  2996. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  2997. mask = strtoul(ctmp + 5, NULL, 0x10);
  2998. if (mask == 0)
  2999. mask = WPA_BSS_MASK_ALL;
  3000. }
  3001. if (bss == NULL)
  3002. return 0;
  3003. if (bsslast == NULL)
  3004. bsslast = bss;
  3005. do {
  3006. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3007. ret += len;
  3008. buf += len;
  3009. buflen -= len;
  3010. if (bss == bsslast) {
  3011. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3012. (bss == dl_list_last(&wpa_s->bss_id,
  3013. struct wpa_bss, list_id)))
  3014. os_snprintf(buf - 5, 5, "####\n");
  3015. break;
  3016. }
  3017. next = bss->list_id.next;
  3018. if (next == &wpa_s->bss_id)
  3019. break;
  3020. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3021. } while (bss && len);
  3022. return ret;
  3023. }
  3024. static int wpa_supplicant_ctrl_iface_ap_scan(
  3025. struct wpa_supplicant *wpa_s, char *cmd)
  3026. {
  3027. int ap_scan = atoi(cmd);
  3028. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3029. }
  3030. static int wpa_supplicant_ctrl_iface_scan_interval(
  3031. struct wpa_supplicant *wpa_s, char *cmd)
  3032. {
  3033. int scan_int = atoi(cmd);
  3034. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3035. }
  3036. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3037. struct wpa_supplicant *wpa_s, char *cmd)
  3038. {
  3039. int expire_age = atoi(cmd);
  3040. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3041. }
  3042. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3043. struct wpa_supplicant *wpa_s, char *cmd)
  3044. {
  3045. int expire_count = atoi(cmd);
  3046. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3047. }
  3048. static int wpa_supplicant_ctrl_iface_bss_flush(
  3049. struct wpa_supplicant *wpa_s, char *cmd)
  3050. {
  3051. int flush_age = atoi(cmd);
  3052. if (flush_age == 0)
  3053. wpa_bss_flush(wpa_s);
  3054. else
  3055. wpa_bss_flush_by_age(wpa_s, flush_age);
  3056. return 0;
  3057. }
  3058. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3059. {
  3060. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3061. /* MLME-DELETEKEYS.request */
  3062. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3063. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3064. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3065. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3066. #ifdef CONFIG_IEEE80211W
  3067. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3068. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3069. #endif /* CONFIG_IEEE80211W */
  3070. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3071. 0);
  3072. /* MLME-SETPROTECTION.request(None) */
  3073. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3074. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3075. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3076. wpa_sm_drop_sa(wpa_s->wpa);
  3077. }
  3078. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3079. char *addr)
  3080. {
  3081. #ifdef CONFIG_NO_SCAN_PROCESSING
  3082. return -1;
  3083. #else /* CONFIG_NO_SCAN_PROCESSING */
  3084. u8 bssid[ETH_ALEN];
  3085. struct wpa_bss *bss;
  3086. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3087. if (hwaddr_aton(addr, bssid)) {
  3088. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3089. "address '%s'", addr);
  3090. return -1;
  3091. }
  3092. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3093. if (!ssid) {
  3094. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3095. "configuration known for the target AP");
  3096. return -1;
  3097. }
  3098. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3099. if (!bss) {
  3100. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3101. "from BSS table");
  3102. return -1;
  3103. }
  3104. /*
  3105. * TODO: Find best network configuration block from configuration to
  3106. * allow roaming to other networks
  3107. */
  3108. wpa_s->reassociate = 1;
  3109. wpa_supplicant_connect(wpa_s, bss, ssid);
  3110. return 0;
  3111. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3112. }
  3113. #ifdef CONFIG_P2P
  3114. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3115. {
  3116. unsigned int timeout = atoi(cmd);
  3117. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3118. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3119. char *pos;
  3120. unsigned int search_delay;
  3121. if (os_strstr(cmd, "type=social"))
  3122. type = P2P_FIND_ONLY_SOCIAL;
  3123. else if (os_strstr(cmd, "type=progressive"))
  3124. type = P2P_FIND_PROGRESSIVE;
  3125. pos = os_strstr(cmd, "dev_id=");
  3126. if (pos) {
  3127. pos += 7;
  3128. if (hwaddr_aton(pos, dev_id))
  3129. return -1;
  3130. _dev_id = dev_id;
  3131. }
  3132. pos = os_strstr(cmd, "delay=");
  3133. if (pos) {
  3134. pos += 6;
  3135. search_delay = atoi(pos);
  3136. } else
  3137. search_delay = wpas_p2p_search_delay(wpa_s);
  3138. return wpas_p2p_find(wpa_s, timeout, type, 0, NULL, _dev_id,
  3139. search_delay);
  3140. }
  3141. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3142. char *buf, size_t buflen)
  3143. {
  3144. u8 addr[ETH_ALEN];
  3145. char *pos, *pos2;
  3146. char *pin = NULL;
  3147. enum p2p_wps_method wps_method;
  3148. int new_pin;
  3149. int ret;
  3150. int persistent_group, persistent_id = -1;
  3151. int join;
  3152. int auth;
  3153. int automatic;
  3154. int go_intent = -1;
  3155. int freq = 0;
  3156. int pd;
  3157. int ht40, vht;
  3158. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3159. * [persistent|persistent=<network id>]
  3160. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3161. * [ht40] [vht] */
  3162. if (hwaddr_aton(cmd, addr))
  3163. return -1;
  3164. pos = cmd + 17;
  3165. if (*pos != ' ')
  3166. return -1;
  3167. pos++;
  3168. persistent_group = os_strstr(pos, " persistent") != NULL;
  3169. pos2 = os_strstr(pos, " persistent=");
  3170. if (pos2) {
  3171. struct wpa_ssid *ssid;
  3172. persistent_id = atoi(pos2 + 12);
  3173. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3174. if (ssid == NULL || ssid->disabled != 2 ||
  3175. ssid->mode != WPAS_MODE_P2P_GO) {
  3176. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3177. "SSID id=%d for persistent P2P group (GO)",
  3178. persistent_id);
  3179. return -1;
  3180. }
  3181. }
  3182. join = os_strstr(pos, " join") != NULL;
  3183. auth = os_strstr(pos, " auth") != NULL;
  3184. automatic = os_strstr(pos, " auto") != NULL;
  3185. pd = os_strstr(pos, " provdisc") != NULL;
  3186. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3187. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3188. vht;
  3189. pos2 = os_strstr(pos, " go_intent=");
  3190. if (pos2) {
  3191. pos2 += 11;
  3192. go_intent = atoi(pos2);
  3193. if (go_intent < 0 || go_intent > 15)
  3194. return -1;
  3195. }
  3196. pos2 = os_strstr(pos, " freq=");
  3197. if (pos2) {
  3198. pos2 += 6;
  3199. freq = atoi(pos2);
  3200. if (freq <= 0)
  3201. return -1;
  3202. }
  3203. if (os_strncmp(pos, "pin", 3) == 0) {
  3204. /* Request random PIN (to be displayed) and enable the PIN */
  3205. wps_method = WPS_PIN_DISPLAY;
  3206. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3207. wps_method = WPS_PBC;
  3208. } else {
  3209. pin = pos;
  3210. pos = os_strchr(pin, ' ');
  3211. wps_method = WPS_PIN_KEYPAD;
  3212. if (pos) {
  3213. *pos++ = '\0';
  3214. if (os_strncmp(pos, "display", 7) == 0)
  3215. wps_method = WPS_PIN_DISPLAY;
  3216. }
  3217. if (!wps_pin_str_valid(pin)) {
  3218. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3219. return 17;
  3220. }
  3221. }
  3222. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3223. persistent_group, automatic, join,
  3224. auth, go_intent, freq, persistent_id, pd,
  3225. ht40, vht);
  3226. if (new_pin == -2) {
  3227. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3228. return 25;
  3229. }
  3230. if (new_pin == -3) {
  3231. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3232. return 25;
  3233. }
  3234. if (new_pin < 0)
  3235. return -1;
  3236. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3237. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3238. if (ret < 0 || (size_t) ret >= buflen)
  3239. return -1;
  3240. return ret;
  3241. }
  3242. os_memcpy(buf, "OK\n", 3);
  3243. return 3;
  3244. }
  3245. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3246. {
  3247. unsigned int timeout = atoi(cmd);
  3248. return wpas_p2p_listen(wpa_s, timeout);
  3249. }
  3250. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3251. {
  3252. u8 addr[ETH_ALEN];
  3253. char *pos;
  3254. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3255. /* <addr> <config method> [join|auto] */
  3256. if (hwaddr_aton(cmd, addr))
  3257. return -1;
  3258. pos = cmd + 17;
  3259. if (*pos != ' ')
  3260. return -1;
  3261. pos++;
  3262. if (os_strstr(pos, " join") != NULL)
  3263. use = WPAS_P2P_PD_FOR_JOIN;
  3264. else if (os_strstr(pos, " auto") != NULL)
  3265. use = WPAS_P2P_PD_AUTO;
  3266. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3267. }
  3268. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3269. size_t buflen)
  3270. {
  3271. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3272. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3273. ssid->passphrase == NULL)
  3274. return -1;
  3275. os_strlcpy(buf, ssid->passphrase, buflen);
  3276. return os_strlen(buf);
  3277. }
  3278. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3279. char *buf, size_t buflen)
  3280. {
  3281. u64 ref;
  3282. int res;
  3283. u8 dst_buf[ETH_ALEN], *dst;
  3284. struct wpabuf *tlvs;
  3285. char *pos;
  3286. size_t len;
  3287. if (hwaddr_aton(cmd, dst_buf))
  3288. return -1;
  3289. dst = dst_buf;
  3290. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3291. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3292. dst = NULL;
  3293. pos = cmd + 17;
  3294. if (*pos != ' ')
  3295. return -1;
  3296. pos++;
  3297. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3298. u8 version;
  3299. pos += 5;
  3300. if (hexstr2bin(pos, &version, 1) < 0)
  3301. return -1;
  3302. pos += 2;
  3303. if (*pos != ' ')
  3304. return -1;
  3305. pos++;
  3306. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3307. #ifdef CONFIG_WIFI_DISPLAY
  3308. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3309. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3310. #endif /* CONFIG_WIFI_DISPLAY */
  3311. } else {
  3312. len = os_strlen(pos);
  3313. if (len & 1)
  3314. return -1;
  3315. len /= 2;
  3316. tlvs = wpabuf_alloc(len);
  3317. if (tlvs == NULL)
  3318. return -1;
  3319. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3320. wpabuf_free(tlvs);
  3321. return -1;
  3322. }
  3323. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3324. wpabuf_free(tlvs);
  3325. }
  3326. if (ref == 0)
  3327. return -1;
  3328. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3329. if (res < 0 || (unsigned) res >= buflen)
  3330. return -1;
  3331. return res;
  3332. }
  3333. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3334. char *cmd)
  3335. {
  3336. long long unsigned val;
  3337. u64 req;
  3338. if (sscanf(cmd, "%llx", &val) != 1)
  3339. return -1;
  3340. req = val;
  3341. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3342. }
  3343. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3344. {
  3345. int freq;
  3346. u8 dst[ETH_ALEN];
  3347. u8 dialog_token;
  3348. struct wpabuf *resp_tlvs;
  3349. char *pos, *pos2;
  3350. size_t len;
  3351. pos = os_strchr(cmd, ' ');
  3352. if (pos == NULL)
  3353. return -1;
  3354. *pos++ = '\0';
  3355. freq = atoi(cmd);
  3356. if (freq == 0)
  3357. return -1;
  3358. if (hwaddr_aton(pos, dst))
  3359. return -1;
  3360. pos += 17;
  3361. if (*pos != ' ')
  3362. return -1;
  3363. pos++;
  3364. pos2 = os_strchr(pos, ' ');
  3365. if (pos2 == NULL)
  3366. return -1;
  3367. *pos2++ = '\0';
  3368. dialog_token = atoi(pos);
  3369. len = os_strlen(pos2);
  3370. if (len & 1)
  3371. return -1;
  3372. len /= 2;
  3373. resp_tlvs = wpabuf_alloc(len);
  3374. if (resp_tlvs == NULL)
  3375. return -1;
  3376. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3377. wpabuf_free(resp_tlvs);
  3378. return -1;
  3379. }
  3380. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3381. wpabuf_free(resp_tlvs);
  3382. return 0;
  3383. }
  3384. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3385. char *cmd)
  3386. {
  3387. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3388. return -1;
  3389. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3390. return 0;
  3391. }
  3392. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3393. char *cmd)
  3394. {
  3395. char *pos;
  3396. size_t len;
  3397. struct wpabuf *query, *resp;
  3398. pos = os_strchr(cmd, ' ');
  3399. if (pos == NULL)
  3400. return -1;
  3401. *pos++ = '\0';
  3402. len = os_strlen(cmd);
  3403. if (len & 1)
  3404. return -1;
  3405. len /= 2;
  3406. query = wpabuf_alloc(len);
  3407. if (query == NULL)
  3408. return -1;
  3409. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3410. wpabuf_free(query);
  3411. return -1;
  3412. }
  3413. len = os_strlen(pos);
  3414. if (len & 1) {
  3415. wpabuf_free(query);
  3416. return -1;
  3417. }
  3418. len /= 2;
  3419. resp = wpabuf_alloc(len);
  3420. if (resp == NULL) {
  3421. wpabuf_free(query);
  3422. return -1;
  3423. }
  3424. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3425. wpabuf_free(query);
  3426. wpabuf_free(resp);
  3427. return -1;
  3428. }
  3429. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3430. wpabuf_free(query);
  3431. wpabuf_free(resp);
  3432. return -1;
  3433. }
  3434. return 0;
  3435. }
  3436. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3437. {
  3438. char *pos;
  3439. u8 version;
  3440. pos = os_strchr(cmd, ' ');
  3441. if (pos == NULL)
  3442. return -1;
  3443. *pos++ = '\0';
  3444. if (hexstr2bin(cmd, &version, 1) < 0)
  3445. return -1;
  3446. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3447. }
  3448. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3449. {
  3450. char *pos;
  3451. pos = os_strchr(cmd, ' ');
  3452. if (pos == NULL)
  3453. return -1;
  3454. *pos++ = '\0';
  3455. if (os_strcmp(cmd, "bonjour") == 0)
  3456. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3457. if (os_strcmp(cmd, "upnp") == 0)
  3458. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3459. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3460. return -1;
  3461. }
  3462. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3463. char *cmd)
  3464. {
  3465. size_t len;
  3466. struct wpabuf *query;
  3467. int ret;
  3468. len = os_strlen(cmd);
  3469. if (len & 1)
  3470. return -1;
  3471. len /= 2;
  3472. query = wpabuf_alloc(len);
  3473. if (query == NULL)
  3474. return -1;
  3475. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3476. wpabuf_free(query);
  3477. return -1;
  3478. }
  3479. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3480. wpabuf_free(query);
  3481. return ret;
  3482. }
  3483. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3484. {
  3485. char *pos;
  3486. u8 version;
  3487. pos = os_strchr(cmd, ' ');
  3488. if (pos == NULL)
  3489. return -1;
  3490. *pos++ = '\0';
  3491. if (hexstr2bin(cmd, &version, 1) < 0)
  3492. return -1;
  3493. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3494. }
  3495. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3496. {
  3497. char *pos;
  3498. pos = os_strchr(cmd, ' ');
  3499. if (pos == NULL)
  3500. return -1;
  3501. *pos++ = '\0';
  3502. if (os_strcmp(cmd, "bonjour") == 0)
  3503. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3504. if (os_strcmp(cmd, "upnp") == 0)
  3505. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3506. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3507. return -1;
  3508. }
  3509. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3510. {
  3511. u8 addr[ETH_ALEN];
  3512. /* <addr> */
  3513. if (hwaddr_aton(cmd, addr))
  3514. return -1;
  3515. return wpas_p2p_reject(wpa_s, addr);
  3516. }
  3517. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3518. {
  3519. char *pos;
  3520. int id;
  3521. struct wpa_ssid *ssid;
  3522. u8 *_peer = NULL, peer[ETH_ALEN];
  3523. int freq = 0, pref_freq = 0;
  3524. int ht40, vht;
  3525. id = atoi(cmd);
  3526. pos = os_strstr(cmd, " peer=");
  3527. if (pos) {
  3528. pos += 6;
  3529. if (hwaddr_aton(pos, peer))
  3530. return -1;
  3531. _peer = peer;
  3532. }
  3533. ssid = wpa_config_get_network(wpa_s->conf, id);
  3534. if (ssid == NULL || ssid->disabled != 2) {
  3535. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3536. "for persistent P2P group",
  3537. id);
  3538. return -1;
  3539. }
  3540. pos = os_strstr(cmd, " freq=");
  3541. if (pos) {
  3542. pos += 6;
  3543. freq = atoi(pos);
  3544. if (freq <= 0)
  3545. return -1;
  3546. }
  3547. pos = os_strstr(cmd, " pref=");
  3548. if (pos) {
  3549. pos += 6;
  3550. pref_freq = atoi(pos);
  3551. if (pref_freq <= 0)
  3552. return -1;
  3553. }
  3554. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3555. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3556. vht;
  3557. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  3558. pref_freq);
  3559. }
  3560. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3561. {
  3562. char *pos;
  3563. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3564. pos = os_strstr(cmd, " peer=");
  3565. if (!pos)
  3566. return -1;
  3567. *pos = '\0';
  3568. pos += 6;
  3569. if (hwaddr_aton(pos, peer)) {
  3570. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3571. return -1;
  3572. }
  3573. pos = os_strstr(pos, " go_dev_addr=");
  3574. if (pos) {
  3575. pos += 13;
  3576. if (hwaddr_aton(pos, go_dev_addr)) {
  3577. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3578. pos);
  3579. return -1;
  3580. }
  3581. go_dev = go_dev_addr;
  3582. }
  3583. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3584. }
  3585. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3586. {
  3587. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3588. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3589. if (os_strncmp(cmd, "group=", 6) == 0)
  3590. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3591. return -1;
  3592. }
  3593. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3594. char *cmd, int freq, int ht40,
  3595. int vht)
  3596. {
  3597. int id;
  3598. struct wpa_ssid *ssid;
  3599. id = atoi(cmd);
  3600. ssid = wpa_config_get_network(wpa_s->conf, id);
  3601. if (ssid == NULL || ssid->disabled != 2) {
  3602. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3603. "for persistent P2P group",
  3604. id);
  3605. return -1;
  3606. }
  3607. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, vht,
  3608. NULL, 0);
  3609. }
  3610. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3611. {
  3612. int freq = 0, ht40, vht;
  3613. char *pos;
  3614. pos = os_strstr(cmd, "freq=");
  3615. if (pos)
  3616. freq = atoi(pos + 5);
  3617. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3618. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3619. vht;
  3620. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3621. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3622. ht40, vht);
  3623. if (os_strcmp(cmd, "persistent") == 0 ||
  3624. os_strncmp(cmd, "persistent ", 11) == 0)
  3625. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  3626. if (os_strncmp(cmd, "freq=", 5) == 0)
  3627. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3628. if (ht40)
  3629. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3630. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3631. cmd);
  3632. return -1;
  3633. }
  3634. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3635. char *buf, size_t buflen)
  3636. {
  3637. u8 addr[ETH_ALEN], *addr_ptr;
  3638. int next, res;
  3639. const struct p2p_peer_info *info;
  3640. char *pos, *end;
  3641. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3642. struct wpa_ssid *ssid;
  3643. size_t i;
  3644. if (!wpa_s->global->p2p)
  3645. return -1;
  3646. if (os_strcmp(cmd, "FIRST") == 0) {
  3647. addr_ptr = NULL;
  3648. next = 0;
  3649. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3650. if (hwaddr_aton(cmd + 5, addr) < 0)
  3651. return -1;
  3652. addr_ptr = addr;
  3653. next = 1;
  3654. } else {
  3655. if (hwaddr_aton(cmd, addr) < 0)
  3656. return -1;
  3657. addr_ptr = addr;
  3658. next = 0;
  3659. }
  3660. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3661. if (info == NULL)
  3662. return -1;
  3663. pos = buf;
  3664. end = buf + buflen;
  3665. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3666. "pri_dev_type=%s\n"
  3667. "device_name=%s\n"
  3668. "manufacturer=%s\n"
  3669. "model_name=%s\n"
  3670. "model_number=%s\n"
  3671. "serial_number=%s\n"
  3672. "config_methods=0x%x\n"
  3673. "dev_capab=0x%x\n"
  3674. "group_capab=0x%x\n"
  3675. "level=%d\n",
  3676. MAC2STR(info->p2p_device_addr),
  3677. wps_dev_type_bin2str(info->pri_dev_type,
  3678. devtype, sizeof(devtype)),
  3679. info->device_name,
  3680. info->manufacturer,
  3681. info->model_name,
  3682. info->model_number,
  3683. info->serial_number,
  3684. info->config_methods,
  3685. info->dev_capab,
  3686. info->group_capab,
  3687. info->level);
  3688. if (res < 0 || res >= end - pos)
  3689. return pos - buf;
  3690. pos += res;
  3691. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3692. {
  3693. const u8 *t;
  3694. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3695. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3696. wps_dev_type_bin2str(t, devtype,
  3697. sizeof(devtype)));
  3698. if (res < 0 || res >= end - pos)
  3699. return pos - buf;
  3700. pos += res;
  3701. }
  3702. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3703. if (ssid) {
  3704. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3705. if (res < 0 || res >= end - pos)
  3706. return pos - buf;
  3707. pos += res;
  3708. }
  3709. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3710. if (res < 0)
  3711. return pos - buf;
  3712. pos += res;
  3713. return pos - buf;
  3714. }
  3715. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3716. const char *param)
  3717. {
  3718. unsigned int i;
  3719. if (wpa_s->global->p2p == NULL)
  3720. return -1;
  3721. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  3722. return -1;
  3723. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  3724. struct wpa_freq_range *freq;
  3725. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  3726. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3727. freq->min, freq->max);
  3728. }
  3729. wpas_p2p_update_channel_list(wpa_s);
  3730. return 0;
  3731. }
  3732. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3733. {
  3734. char *param;
  3735. if (wpa_s->global->p2p == NULL)
  3736. return -1;
  3737. param = os_strchr(cmd, ' ');
  3738. if (param == NULL)
  3739. return -1;
  3740. *param++ = '\0';
  3741. if (os_strcmp(cmd, "discoverability") == 0) {
  3742. p2p_set_client_discoverability(wpa_s->global->p2p,
  3743. atoi(param));
  3744. return 0;
  3745. }
  3746. if (os_strcmp(cmd, "managed") == 0) {
  3747. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3748. return 0;
  3749. }
  3750. if (os_strcmp(cmd, "listen_channel") == 0) {
  3751. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3752. atoi(param));
  3753. }
  3754. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3755. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3756. os_strlen(param));
  3757. }
  3758. if (os_strcmp(cmd, "noa") == 0) {
  3759. char *pos;
  3760. int count, start, duration;
  3761. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3762. count = atoi(param);
  3763. pos = os_strchr(param, ',');
  3764. if (pos == NULL)
  3765. return -1;
  3766. pos++;
  3767. start = atoi(pos);
  3768. pos = os_strchr(pos, ',');
  3769. if (pos == NULL)
  3770. return -1;
  3771. pos++;
  3772. duration = atoi(pos);
  3773. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3774. return -1;
  3775. if (count == 0 && duration > 0)
  3776. return -1;
  3777. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3778. "start=%d duration=%d", count, start, duration);
  3779. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3780. }
  3781. if (os_strcmp(cmd, "ps") == 0)
  3782. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3783. if (os_strcmp(cmd, "oppps") == 0)
  3784. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3785. if (os_strcmp(cmd, "ctwindow") == 0)
  3786. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3787. if (os_strcmp(cmd, "disabled") == 0) {
  3788. wpa_s->global->p2p_disabled = atoi(param);
  3789. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3790. wpa_s->global->p2p_disabled ?
  3791. "disabled" : "enabled");
  3792. if (wpa_s->global->p2p_disabled) {
  3793. wpas_p2p_stop_find(wpa_s);
  3794. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3795. p2p_flush(wpa_s->global->p2p);
  3796. }
  3797. return 0;
  3798. }
  3799. if (os_strcmp(cmd, "conc_pref") == 0) {
  3800. if (os_strcmp(param, "sta") == 0)
  3801. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3802. else if (os_strcmp(param, "p2p") == 0)
  3803. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3804. else {
  3805. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3806. return -1;
  3807. }
  3808. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3809. "%s", param);
  3810. return 0;
  3811. }
  3812. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3813. wpa_s->force_long_sd = atoi(param);
  3814. return 0;
  3815. }
  3816. if (os_strcmp(cmd, "peer_filter") == 0) {
  3817. u8 addr[ETH_ALEN];
  3818. if (hwaddr_aton(param, addr))
  3819. return -1;
  3820. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3821. return 0;
  3822. }
  3823. if (os_strcmp(cmd, "cross_connect") == 0)
  3824. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3825. if (os_strcmp(cmd, "go_apsd") == 0) {
  3826. if (os_strcmp(param, "disable") == 0)
  3827. wpa_s->set_ap_uapsd = 0;
  3828. else {
  3829. wpa_s->set_ap_uapsd = 1;
  3830. wpa_s->ap_uapsd = atoi(param);
  3831. }
  3832. return 0;
  3833. }
  3834. if (os_strcmp(cmd, "client_apsd") == 0) {
  3835. if (os_strcmp(param, "disable") == 0)
  3836. wpa_s->set_sta_uapsd = 0;
  3837. else {
  3838. int be, bk, vi, vo;
  3839. char *pos;
  3840. /* format: BE,BK,VI,VO;max SP Length */
  3841. be = atoi(param);
  3842. pos = os_strchr(param, ',');
  3843. if (pos == NULL)
  3844. return -1;
  3845. pos++;
  3846. bk = atoi(pos);
  3847. pos = os_strchr(pos, ',');
  3848. if (pos == NULL)
  3849. return -1;
  3850. pos++;
  3851. vi = atoi(pos);
  3852. pos = os_strchr(pos, ',');
  3853. if (pos == NULL)
  3854. return -1;
  3855. pos++;
  3856. vo = atoi(pos);
  3857. /* ignore max SP Length for now */
  3858. wpa_s->set_sta_uapsd = 1;
  3859. wpa_s->sta_uapsd = 0;
  3860. if (be)
  3861. wpa_s->sta_uapsd |= BIT(0);
  3862. if (bk)
  3863. wpa_s->sta_uapsd |= BIT(1);
  3864. if (vi)
  3865. wpa_s->sta_uapsd |= BIT(2);
  3866. if (vo)
  3867. wpa_s->sta_uapsd |= BIT(3);
  3868. }
  3869. return 0;
  3870. }
  3871. if (os_strcmp(cmd, "disallow_freq") == 0)
  3872. return p2p_ctrl_disallow_freq(wpa_s, param);
  3873. if (os_strcmp(cmd, "disc_int") == 0) {
  3874. int min_disc_int, max_disc_int, max_disc_tu;
  3875. char *pos;
  3876. pos = param;
  3877. min_disc_int = atoi(pos);
  3878. pos = os_strchr(pos, ' ');
  3879. if (pos == NULL)
  3880. return -1;
  3881. *pos++ = '\0';
  3882. max_disc_int = atoi(pos);
  3883. pos = os_strchr(pos, ' ');
  3884. if (pos == NULL)
  3885. return -1;
  3886. *pos++ = '\0';
  3887. max_disc_tu = atoi(pos);
  3888. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  3889. max_disc_int, max_disc_tu);
  3890. }
  3891. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  3892. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  3893. return 0;
  3894. }
  3895. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  3896. cmd);
  3897. return -1;
  3898. }
  3899. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  3900. {
  3901. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3902. wpa_s->force_long_sd = 0;
  3903. if (wpa_s->global->p2p)
  3904. p2p_flush(wpa_s->global->p2p);
  3905. }
  3906. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  3907. {
  3908. char *pos, *pos2;
  3909. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  3910. if (cmd[0]) {
  3911. pos = os_strchr(cmd, ' ');
  3912. if (pos == NULL)
  3913. return -1;
  3914. *pos++ = '\0';
  3915. dur1 = atoi(cmd);
  3916. pos2 = os_strchr(pos, ' ');
  3917. if (pos2)
  3918. *pos2++ = '\0';
  3919. int1 = atoi(pos);
  3920. } else
  3921. pos2 = NULL;
  3922. if (pos2) {
  3923. pos = os_strchr(pos2, ' ');
  3924. if (pos == NULL)
  3925. return -1;
  3926. *pos++ = '\0';
  3927. dur2 = atoi(pos2);
  3928. int2 = atoi(pos);
  3929. }
  3930. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  3931. }
  3932. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3933. {
  3934. char *pos;
  3935. unsigned int period = 0, interval = 0;
  3936. if (cmd[0]) {
  3937. pos = os_strchr(cmd, ' ');
  3938. if (pos == NULL)
  3939. return -1;
  3940. *pos++ = '\0';
  3941. period = atoi(cmd);
  3942. interval = atoi(pos);
  3943. }
  3944. return wpas_p2p_ext_listen(wpa_s, period, interval);
  3945. }
  3946. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  3947. {
  3948. const char *pos;
  3949. u8 peer[ETH_ALEN];
  3950. int iface_addr = 0;
  3951. pos = cmd;
  3952. if (os_strncmp(pos, "iface=", 6) == 0) {
  3953. iface_addr = 1;
  3954. pos += 6;
  3955. }
  3956. if (hwaddr_aton(pos, peer))
  3957. return -1;
  3958. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  3959. return 0;
  3960. }
  3961. #endif /* CONFIG_P2P */
  3962. #ifdef CONFIG_INTERWORKING
  3963. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  3964. {
  3965. u8 bssid[ETH_ALEN];
  3966. struct wpa_bss *bss;
  3967. if (hwaddr_aton(dst, bssid)) {
  3968. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  3969. return -1;
  3970. }
  3971. bss = wpa_bss_get_bssid(wpa_s, bssid);
  3972. if (bss == NULL) {
  3973. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  3974. MAC2STR(bssid));
  3975. return -1;
  3976. }
  3977. return interworking_connect(wpa_s, bss);
  3978. }
  3979. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  3980. {
  3981. u8 dst_addr[ETH_ALEN];
  3982. int used;
  3983. char *pos;
  3984. #define MAX_ANQP_INFO_ID 100
  3985. u16 id[MAX_ANQP_INFO_ID];
  3986. size_t num_id = 0;
  3987. used = hwaddr_aton2(dst, dst_addr);
  3988. if (used < 0)
  3989. return -1;
  3990. pos = dst + used;
  3991. while (num_id < MAX_ANQP_INFO_ID) {
  3992. id[num_id] = atoi(pos);
  3993. if (id[num_id])
  3994. num_id++;
  3995. pos = os_strchr(pos + 1, ',');
  3996. if (pos == NULL)
  3997. break;
  3998. pos++;
  3999. }
  4000. if (num_id == 0)
  4001. return -1;
  4002. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  4003. }
  4004. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  4005. {
  4006. u8 dst_addr[ETH_ALEN];
  4007. struct wpabuf *advproto, *query = NULL;
  4008. int used, ret = -1;
  4009. char *pos, *end;
  4010. size_t len;
  4011. used = hwaddr_aton2(cmd, dst_addr);
  4012. if (used < 0)
  4013. return -1;
  4014. pos = cmd + used;
  4015. while (*pos == ' ')
  4016. pos++;
  4017. /* Advertisement Protocol ID */
  4018. end = os_strchr(pos, ' ');
  4019. if (end)
  4020. len = end - pos;
  4021. else
  4022. len = os_strlen(pos);
  4023. if (len & 0x01)
  4024. return -1;
  4025. len /= 2;
  4026. if (len == 0)
  4027. return -1;
  4028. advproto = wpabuf_alloc(len);
  4029. if (advproto == NULL)
  4030. return -1;
  4031. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4032. goto fail;
  4033. if (end) {
  4034. /* Optional Query Request */
  4035. pos = end + 1;
  4036. while (*pos == ' ')
  4037. pos++;
  4038. len = os_strlen(pos);
  4039. if (len) {
  4040. if (len & 0x01)
  4041. goto fail;
  4042. len /= 2;
  4043. if (len == 0)
  4044. goto fail;
  4045. query = wpabuf_alloc(len);
  4046. if (query == NULL)
  4047. goto fail;
  4048. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4049. goto fail;
  4050. }
  4051. }
  4052. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4053. fail:
  4054. wpabuf_free(advproto);
  4055. wpabuf_free(query);
  4056. return ret;
  4057. }
  4058. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4059. size_t buflen)
  4060. {
  4061. u8 addr[ETH_ALEN];
  4062. int dialog_token;
  4063. int used;
  4064. char *pos;
  4065. size_t resp_len, start, requested_len;
  4066. if (!wpa_s->last_gas_resp)
  4067. return -1;
  4068. used = hwaddr_aton2(cmd, addr);
  4069. if (used < 0)
  4070. return -1;
  4071. pos = cmd + used;
  4072. while (*pos == ' ')
  4073. pos++;
  4074. dialog_token = atoi(pos);
  4075. if (os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) != 0 ||
  4076. dialog_token != wpa_s->last_gas_dialog_token)
  4077. return -1;
  4078. resp_len = wpabuf_len(wpa_s->last_gas_resp);
  4079. start = 0;
  4080. requested_len = resp_len;
  4081. pos = os_strchr(pos, ' ');
  4082. if (pos) {
  4083. start = atoi(pos);
  4084. if (start > resp_len)
  4085. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4086. pos = os_strchr(pos, ',');
  4087. if (pos == NULL)
  4088. return -1;
  4089. pos++;
  4090. requested_len = atoi(pos);
  4091. if (start + requested_len > resp_len)
  4092. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4093. }
  4094. if (requested_len * 2 + 1 > buflen)
  4095. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4096. return wpa_snprintf_hex(buf, buflen,
  4097. wpabuf_head_u8(wpa_s->last_gas_resp) + start,
  4098. requested_len);
  4099. }
  4100. #endif /* CONFIG_INTERWORKING */
  4101. #ifdef CONFIG_HS20
  4102. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4103. {
  4104. u8 dst_addr[ETH_ALEN];
  4105. int used;
  4106. char *pos;
  4107. u32 subtypes = 0;
  4108. used = hwaddr_aton2(dst, dst_addr);
  4109. if (used < 0)
  4110. return -1;
  4111. pos = dst + used;
  4112. for (;;) {
  4113. int num = atoi(pos);
  4114. if (num <= 0 || num > 31)
  4115. return -1;
  4116. subtypes |= BIT(num);
  4117. pos = os_strchr(pos + 1, ',');
  4118. if (pos == NULL)
  4119. break;
  4120. pos++;
  4121. }
  4122. if (subtypes == 0)
  4123. return -1;
  4124. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4125. }
  4126. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4127. const u8 *addr, const char *realm)
  4128. {
  4129. u8 *buf;
  4130. size_t rlen, len;
  4131. int ret;
  4132. rlen = os_strlen(realm);
  4133. len = 3 + rlen;
  4134. buf = os_malloc(len);
  4135. if (buf == NULL)
  4136. return -1;
  4137. buf[0] = 1; /* NAI Home Realm Count */
  4138. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4139. buf[2] = rlen;
  4140. os_memcpy(buf + 3, realm, rlen);
  4141. ret = hs20_anqp_send_req(wpa_s, addr,
  4142. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4143. buf, len);
  4144. os_free(buf);
  4145. return ret;
  4146. }
  4147. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4148. char *dst)
  4149. {
  4150. struct wpa_cred *cred = wpa_s->conf->cred;
  4151. u8 dst_addr[ETH_ALEN];
  4152. int used;
  4153. u8 *buf;
  4154. size_t len;
  4155. int ret;
  4156. used = hwaddr_aton2(dst, dst_addr);
  4157. if (used < 0)
  4158. return -1;
  4159. while (dst[used] == ' ')
  4160. used++;
  4161. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4162. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4163. dst + used + 6);
  4164. len = os_strlen(dst + used);
  4165. if (len == 0 && cred && cred->realm)
  4166. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4167. if (len % 1)
  4168. return -1;
  4169. len /= 2;
  4170. buf = os_malloc(len);
  4171. if (buf == NULL)
  4172. return -1;
  4173. if (hexstr2bin(dst + used, buf, len) < 0) {
  4174. os_free(buf);
  4175. return -1;
  4176. }
  4177. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4178. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4179. buf, len);
  4180. os_free(buf);
  4181. return ret;
  4182. }
  4183. #endif /* CONFIG_HS20 */
  4184. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4185. struct wpa_supplicant *wpa_s, char *cmd)
  4186. {
  4187. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4188. return 0;
  4189. }
  4190. #ifdef CONFIG_AUTOSCAN
  4191. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4192. char *cmd)
  4193. {
  4194. enum wpa_states state = wpa_s->wpa_state;
  4195. char *new_params = NULL;
  4196. if (os_strlen(cmd) > 0) {
  4197. new_params = os_strdup(cmd);
  4198. if (new_params == NULL)
  4199. return -1;
  4200. }
  4201. os_free(wpa_s->conf->autoscan);
  4202. wpa_s->conf->autoscan = new_params;
  4203. if (wpa_s->conf->autoscan == NULL)
  4204. autoscan_deinit(wpa_s);
  4205. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4206. autoscan_init(wpa_s, 1);
  4207. else if (state == WPA_SCANNING)
  4208. wpa_supplicant_reinit_autoscan(wpa_s);
  4209. return 0;
  4210. }
  4211. #endif /* CONFIG_AUTOSCAN */
  4212. #ifdef CONFIG_WNM
  4213. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4214. {
  4215. int enter;
  4216. int intval = 0;
  4217. char *pos;
  4218. int ret;
  4219. struct wpabuf *tfs_req = NULL;
  4220. if (os_strncmp(cmd, "enter", 5) == 0)
  4221. enter = 1;
  4222. else if (os_strncmp(cmd, "exit", 4) == 0)
  4223. enter = 0;
  4224. else
  4225. return -1;
  4226. pos = os_strstr(cmd, " interval=");
  4227. if (pos)
  4228. intval = atoi(pos + 10);
  4229. pos = os_strstr(cmd, " tfs_req=");
  4230. if (pos) {
  4231. char *end;
  4232. size_t len;
  4233. pos += 9;
  4234. end = os_strchr(pos, ' ');
  4235. if (end)
  4236. len = end - pos;
  4237. else
  4238. len = os_strlen(pos);
  4239. if (len & 1)
  4240. return -1;
  4241. len /= 2;
  4242. tfs_req = wpabuf_alloc(len);
  4243. if (tfs_req == NULL)
  4244. return -1;
  4245. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4246. wpabuf_free(tfs_req);
  4247. return -1;
  4248. }
  4249. }
  4250. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4251. WNM_SLEEP_MODE_EXIT, intval,
  4252. tfs_req);
  4253. wpabuf_free(tfs_req);
  4254. return ret;
  4255. }
  4256. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4257. {
  4258. int query_reason;
  4259. query_reason = atoi(cmd);
  4260. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4261. query_reason);
  4262. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4263. }
  4264. #endif /* CONFIG_WNM */
  4265. /* Get string representation of channel width */
  4266. static const char * channel_width_name(enum chan_width width)
  4267. {
  4268. switch (width) {
  4269. case CHAN_WIDTH_20_NOHT:
  4270. return "20 MHz (no HT)";
  4271. case CHAN_WIDTH_20:
  4272. return "20 MHz";
  4273. case CHAN_WIDTH_40:
  4274. return "40 MHz";
  4275. case CHAN_WIDTH_80:
  4276. return "80 MHz";
  4277. case CHAN_WIDTH_80P80:
  4278. return "80+80 MHz";
  4279. case CHAN_WIDTH_160:
  4280. return "160 MHz";
  4281. default:
  4282. return "unknown";
  4283. }
  4284. }
  4285. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4286. size_t buflen)
  4287. {
  4288. struct wpa_signal_info si;
  4289. int ret;
  4290. char *pos, *end;
  4291. ret = wpa_drv_signal_poll(wpa_s, &si);
  4292. if (ret)
  4293. return -1;
  4294. pos = buf;
  4295. end = buf + buflen;
  4296. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4297. "NOISE=%d\nFREQUENCY=%u\n",
  4298. si.current_signal, si.current_txrate / 1000,
  4299. si.current_noise, si.frequency);
  4300. if (ret < 0 || ret > end - pos)
  4301. return -1;
  4302. pos += ret;
  4303. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4304. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4305. channel_width_name(si.chanwidth));
  4306. if (ret < 0 || ret > end - pos)
  4307. return -1;
  4308. pos += ret;
  4309. }
  4310. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4311. ret = os_snprintf(pos, end - pos,
  4312. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4313. si.center_frq1, si.center_frq2);
  4314. if (ret < 0 || ret > end - pos)
  4315. return -1;
  4316. pos += ret;
  4317. }
  4318. if (si.avg_signal) {
  4319. ret = os_snprintf(pos, end - pos,
  4320. "AVG_RSSI=%d\n", si.avg_signal);
  4321. if (ret < 0 || ret >= end - pos)
  4322. return -1;
  4323. pos += ret;
  4324. }
  4325. return pos - buf;
  4326. }
  4327. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4328. size_t buflen)
  4329. {
  4330. struct hostap_sta_driver_data sta;
  4331. int ret;
  4332. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4333. if (ret)
  4334. return -1;
  4335. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4336. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4337. if (ret < 0 || (size_t) ret > buflen)
  4338. return -1;
  4339. return ret;
  4340. }
  4341. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4342. {
  4343. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4344. #ifdef CONFIG_P2P
  4345. wpas_p2p_stop_find(wpa_s);
  4346. p2p_ctrl_flush(wpa_s);
  4347. wpas_p2p_group_remove(wpa_s, "*");
  4348. #endif /* CONFIG_P2P */
  4349. #ifdef CONFIG_WPS_TESTING
  4350. wps_version_number = 0x20;
  4351. wps_testing_dummy_cred = 0;
  4352. #endif /* CONFIG_WPS_TESTING */
  4353. #ifdef CONFIG_WPS
  4354. wpas_wps_cancel(wpa_s);
  4355. #endif /* CONFIG_WPS */
  4356. wpa_s->after_wps = 0;
  4357. wpa_s->known_wps_freq = 0;
  4358. #ifdef CONFIG_TDLS_TESTING
  4359. extern unsigned int tdls_testing;
  4360. tdls_testing = 0;
  4361. #endif /* CONFIG_TDLS_TESTING */
  4362. #ifdef CONFIG_TDLS
  4363. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4364. wpa_tdls_enable(wpa_s->wpa, 1);
  4365. #endif /* CONFIG_TDLS */
  4366. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  4367. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  4368. wpa_s->no_keep_alive = 0;
  4369. os_free(wpa_s->disallow_aps_bssid);
  4370. wpa_s->disallow_aps_bssid = NULL;
  4371. wpa_s->disallow_aps_bssid_count = 0;
  4372. os_free(wpa_s->disallow_aps_ssid);
  4373. wpa_s->disallow_aps_ssid = NULL;
  4374. wpa_s->disallow_aps_ssid_count = 0;
  4375. wpa_s->set_sta_uapsd = 0;
  4376. wpa_s->sta_uapsd = 0;
  4377. wpa_drv_radio_disable(wpa_s, 0);
  4378. wpa_bss_flush(wpa_s);
  4379. wpa_blacklist_clear(wpa_s);
  4380. wpa_s->extra_blacklist_count = 0;
  4381. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4382. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4383. }
  4384. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  4385. {
  4386. struct wpa_supplicant *wpa_s = eloop_ctx;
  4387. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  4388. }
  4389. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  4390. char *buf, size_t *resp_len)
  4391. {
  4392. char *reply;
  4393. const int reply_size = 4096;
  4394. int reply_len;
  4395. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  4396. os_strncmp(buf, "SET_NETWORK ", 12) == 0 ||
  4397. os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  4398. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
  4399. os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
  4400. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  4401. (const u8 *) buf, os_strlen(buf));
  4402. } else {
  4403. int level = MSG_DEBUG;
  4404. if (os_strcmp(buf, "PING") == 0)
  4405. level = MSG_EXCESSIVE;
  4406. wpa_hexdump_ascii(level, "RX ctrl_iface",
  4407. (const u8 *) buf, os_strlen(buf));
  4408. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  4409. }
  4410. reply = os_malloc(reply_size);
  4411. if (reply == NULL) {
  4412. *resp_len = 1;
  4413. return NULL;
  4414. }
  4415. os_memcpy(reply, "OK\n", 3);
  4416. reply_len = 3;
  4417. if (os_strcmp(buf, "PING") == 0) {
  4418. os_memcpy(reply, "PONG\n", 5);
  4419. reply_len = 5;
  4420. } else if (os_strcmp(buf, "IFNAME") == 0) {
  4421. reply_len = os_strlen(wpa_s->ifname);
  4422. os_memcpy(reply, wpa_s->ifname, reply_len);
  4423. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  4424. if (wpa_debug_reopen_file() < 0)
  4425. reply_len = -1;
  4426. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  4427. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  4428. } else if (os_strcmp(buf, "MIB") == 0) {
  4429. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  4430. if (reply_len >= 0) {
  4431. int res;
  4432. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  4433. reply_size - reply_len);
  4434. if (res < 0)
  4435. reply_len = -1;
  4436. else
  4437. reply_len += res;
  4438. }
  4439. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  4440. reply_len = wpa_supplicant_ctrl_iface_status(
  4441. wpa_s, buf + 6, reply, reply_size);
  4442. } else if (os_strcmp(buf, "PMKSA") == 0) {
  4443. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  4444. reply_size);
  4445. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  4446. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  4447. reply_len = -1;
  4448. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  4449. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  4450. reply, reply_size);
  4451. } else if (os_strcmp(buf, "LOGON") == 0) {
  4452. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4453. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  4454. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  4455. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  4456. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4457. reply_len = -1;
  4458. else
  4459. wpas_request_connection(wpa_s);
  4460. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  4461. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4462. reply_len = -1;
  4463. else if (wpa_s->disconnected)
  4464. wpas_request_connection(wpa_s);
  4465. #ifdef IEEE8021X_EAPOL
  4466. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  4467. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  4468. reply_len = -1;
  4469. #endif /* IEEE8021X_EAPOL */
  4470. #ifdef CONFIG_PEERKEY
  4471. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  4472. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  4473. reply_len = -1;
  4474. #endif /* CONFIG_PEERKEY */
  4475. #ifdef CONFIG_IEEE80211R
  4476. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  4477. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  4478. reply_len = -1;
  4479. #endif /* CONFIG_IEEE80211R */
  4480. #ifdef CONFIG_WPS
  4481. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  4482. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  4483. if (res == -2) {
  4484. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4485. reply_len = 17;
  4486. } else if (res)
  4487. reply_len = -1;
  4488. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  4489. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  4490. if (res == -2) {
  4491. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4492. reply_len = 17;
  4493. } else if (res)
  4494. reply_len = -1;
  4495. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  4496. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  4497. reply,
  4498. reply_size);
  4499. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  4500. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  4501. wpa_s, buf + 14, reply, reply_size);
  4502. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  4503. if (wpas_wps_cancel(wpa_s))
  4504. reply_len = -1;
  4505. #ifdef CONFIG_WPS_NFC
  4506. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  4507. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  4508. reply_len = -1;
  4509. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  4510. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  4511. reply_len = -1;
  4512. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  4513. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  4514. wpa_s, buf + 21, reply, reply_size);
  4515. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  4516. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  4517. wpa_s, buf + 14, reply, reply_size);
  4518. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  4519. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  4520. buf + 17))
  4521. reply_len = -1;
  4522. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  4523. reply_len = wpas_ctrl_nfc_get_handover_req(
  4524. wpa_s, buf + 21, reply, reply_size);
  4525. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  4526. reply_len = wpas_ctrl_nfc_get_handover_sel(
  4527. wpa_s, buf + 21, reply, reply_size);
  4528. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
  4529. reply_len = wpas_ctrl_nfc_rx_handover_req(
  4530. wpa_s, buf + 20, reply, reply_size);
  4531. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
  4532. if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
  4533. reply_len = -1;
  4534. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  4535. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  4536. reply_len = -1;
  4537. #endif /* CONFIG_WPS_NFC */
  4538. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  4539. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  4540. reply_len = -1;
  4541. #ifdef CONFIG_AP
  4542. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  4543. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  4544. wpa_s, buf + 11, reply, reply_size);
  4545. #endif /* CONFIG_AP */
  4546. #ifdef CONFIG_WPS_ER
  4547. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  4548. if (wpas_wps_er_start(wpa_s, NULL))
  4549. reply_len = -1;
  4550. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  4551. if (wpas_wps_er_start(wpa_s, buf + 13))
  4552. reply_len = -1;
  4553. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  4554. if (wpas_wps_er_stop(wpa_s))
  4555. reply_len = -1;
  4556. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  4557. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  4558. reply_len = -1;
  4559. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  4560. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  4561. if (ret == -2) {
  4562. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4563. reply_len = 17;
  4564. } else if (ret == -3) {
  4565. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  4566. reply_len = 18;
  4567. } else if (ret == -4) {
  4568. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  4569. reply_len = 20;
  4570. } else if (ret)
  4571. reply_len = -1;
  4572. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  4573. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  4574. reply_len = -1;
  4575. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  4576. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  4577. buf + 18))
  4578. reply_len = -1;
  4579. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  4580. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  4581. reply_len = -1;
  4582. #ifdef CONFIG_WPS_NFC
  4583. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  4584. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  4585. wpa_s, buf + 24, reply, reply_size);
  4586. #endif /* CONFIG_WPS_NFC */
  4587. #endif /* CONFIG_WPS_ER */
  4588. #endif /* CONFIG_WPS */
  4589. #ifdef CONFIG_IBSS_RSN
  4590. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  4591. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  4592. reply_len = -1;
  4593. #endif /* CONFIG_IBSS_RSN */
  4594. #ifdef CONFIG_P2P
  4595. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  4596. if (p2p_ctrl_find(wpa_s, buf + 9))
  4597. reply_len = -1;
  4598. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  4599. if (p2p_ctrl_find(wpa_s, ""))
  4600. reply_len = -1;
  4601. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  4602. wpas_p2p_stop_find(wpa_s);
  4603. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  4604. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  4605. reply_size);
  4606. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  4607. if (p2p_ctrl_listen(wpa_s, buf + 11))
  4608. reply_len = -1;
  4609. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  4610. if (p2p_ctrl_listen(wpa_s, ""))
  4611. reply_len = -1;
  4612. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  4613. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  4614. reply_len = -1;
  4615. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  4616. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  4617. reply_len = -1;
  4618. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  4619. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  4620. reply_len = -1;
  4621. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  4622. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  4623. reply_len = -1;
  4624. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  4625. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  4626. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  4627. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  4628. reply_size);
  4629. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  4630. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  4631. reply_len = -1;
  4632. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  4633. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  4634. reply_len = -1;
  4635. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  4636. wpas_p2p_sd_service_update(wpa_s);
  4637. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  4638. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  4639. reply_len = -1;
  4640. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  4641. wpas_p2p_service_flush(wpa_s);
  4642. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  4643. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  4644. reply_len = -1;
  4645. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  4646. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  4647. reply_len = -1;
  4648. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  4649. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  4650. reply_len = -1;
  4651. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  4652. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  4653. reply_len = -1;
  4654. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  4655. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  4656. reply_size);
  4657. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  4658. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  4659. reply_len = -1;
  4660. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  4661. p2p_ctrl_flush(wpa_s);
  4662. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  4663. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  4664. reply_len = -1;
  4665. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  4666. if (wpas_p2p_cancel(wpa_s))
  4667. reply_len = -1;
  4668. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  4669. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  4670. reply_len = -1;
  4671. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  4672. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  4673. reply_len = -1;
  4674. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  4675. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  4676. reply_len = -1;
  4677. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  4678. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  4679. reply_len = -1;
  4680. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  4681. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  4682. reply_len = -1;
  4683. #endif /* CONFIG_P2P */
  4684. #ifdef CONFIG_WIFI_DISPLAY
  4685. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  4686. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  4687. reply_len = -1;
  4688. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  4689. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  4690. reply, reply_size);
  4691. #endif /* CONFIG_WIFI_DISPLAY */
  4692. #ifdef CONFIG_INTERWORKING
  4693. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  4694. if (interworking_fetch_anqp(wpa_s) < 0)
  4695. reply_len = -1;
  4696. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  4697. interworking_stop_fetch_anqp(wpa_s);
  4698. } else if (os_strncmp(buf, "INTERWORKING_SELECT", 19) == 0) {
  4699. if (interworking_select(wpa_s, os_strstr(buf + 19, "auto") !=
  4700. NULL) < 0)
  4701. reply_len = -1;
  4702. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  4703. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  4704. reply_len = -1;
  4705. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  4706. if (get_anqp(wpa_s, buf + 9) < 0)
  4707. reply_len = -1;
  4708. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  4709. if (gas_request(wpa_s, buf + 12) < 0)
  4710. reply_len = -1;
  4711. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  4712. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  4713. reply_size);
  4714. #endif /* CONFIG_INTERWORKING */
  4715. #ifdef CONFIG_HS20
  4716. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  4717. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  4718. reply_len = -1;
  4719. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  4720. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  4721. reply_len = -1;
  4722. #endif /* CONFIG_HS20 */
  4723. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  4724. {
  4725. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  4726. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  4727. reply_len = -1;
  4728. else {
  4729. /*
  4730. * Notify response from timeout to allow the control
  4731. * interface response to be sent first.
  4732. */
  4733. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  4734. wpa_s, NULL);
  4735. }
  4736. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  4737. if (wpa_supplicant_reload_configuration(wpa_s))
  4738. reply_len = -1;
  4739. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  4740. wpa_supplicant_terminate_proc(wpa_s->global);
  4741. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  4742. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  4743. reply_len = -1;
  4744. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  4745. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  4746. wpa_s, buf + 9, reply, reply_size);
  4747. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  4748. reply_len = wpa_supplicant_ctrl_iface_log_level(
  4749. wpa_s, buf + 9, reply, reply_size);
  4750. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  4751. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  4752. wpa_s, reply, reply_size);
  4753. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  4754. #ifdef CONFIG_SME
  4755. wpa_s->sme.prev_bssid_set = 0;
  4756. #endif /* CONFIG_SME */
  4757. wpa_s->reassociate = 0;
  4758. wpa_s->disconnected = 1;
  4759. wpa_supplicant_cancel_sched_scan(wpa_s);
  4760. wpa_supplicant_cancel_scan(wpa_s);
  4761. wpa_supplicant_deauthenticate(wpa_s,
  4762. WLAN_REASON_DEAUTH_LEAVING);
  4763. } else if (os_strcmp(buf, "SCAN") == 0 ||
  4764. os_strncmp(buf, "SCAN ", 5) == 0) {
  4765. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4766. reply_len = -1;
  4767. else {
  4768. if (os_strlen(buf) > 4 &&
  4769. os_strncasecmp(buf + 5, "TYPE=ONLY", 9) == 0)
  4770. wpa_s->scan_res_handler = scan_only_handler;
  4771. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  4772. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  4773. (wpa_s->wpa_state == WPA_COMPLETED))) {
  4774. wpa_s->normal_scans = 0;
  4775. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4776. wpa_s->after_wps = 0;
  4777. wpa_s->known_wps_freq = 0;
  4778. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4779. } else if (wpa_s->sched_scanning) {
  4780. wpa_printf(MSG_DEBUG, "Stop ongoing "
  4781. "sched_scan to allow requested "
  4782. "full scan to proceed");
  4783. wpa_supplicant_cancel_sched_scan(wpa_s);
  4784. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4785. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4786. } else {
  4787. wpa_printf(MSG_DEBUG, "Ongoing scan action - "
  4788. "reject new request");
  4789. reply_len = os_snprintf(reply, reply_size,
  4790. "FAIL-BUSY\n");
  4791. }
  4792. }
  4793. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  4794. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  4795. wpa_s, reply, reply_size);
  4796. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  4797. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  4798. reply_len = -1;
  4799. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  4800. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  4801. reply_len = -1;
  4802. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  4803. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  4804. reply_len = -1;
  4805. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  4806. reply_len = wpa_supplicant_ctrl_iface_add_network(
  4807. wpa_s, reply, reply_size);
  4808. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  4809. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  4810. reply_len = -1;
  4811. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  4812. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  4813. reply_len = -1;
  4814. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  4815. reply_len = wpa_supplicant_ctrl_iface_get_network(
  4816. wpa_s, buf + 12, reply, reply_size);
  4817. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  4818. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  4819. wpa_s, reply, reply_size);
  4820. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  4821. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  4822. wpa_s, reply, reply_size);
  4823. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  4824. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  4825. reply_len = -1;
  4826. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  4827. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  4828. reply_len = -1;
  4829. #ifndef CONFIG_NO_CONFIG_WRITE
  4830. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  4831. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  4832. reply_len = -1;
  4833. #endif /* CONFIG_NO_CONFIG_WRITE */
  4834. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  4835. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  4836. wpa_s, buf + 15, reply, reply_size);
  4837. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  4838. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  4839. reply_len = -1;
  4840. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  4841. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  4842. reply_len = -1;
  4843. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  4844. reply_len = wpa_supplicant_global_iface_list(
  4845. wpa_s->global, reply, reply_size);
  4846. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  4847. reply_len = wpa_supplicant_global_iface_interfaces(
  4848. wpa_s->global, reply, reply_size);
  4849. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  4850. reply_len = wpa_supplicant_ctrl_iface_bss(
  4851. wpa_s, buf + 4, reply, reply_size);
  4852. #ifdef CONFIG_AP
  4853. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  4854. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  4855. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  4856. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  4857. reply_size);
  4858. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  4859. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  4860. reply_size);
  4861. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  4862. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  4863. reply_len = -1;
  4864. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  4865. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  4866. reply_len = -1;
  4867. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  4868. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  4869. reply_len = -1;
  4870. #endif /* CONFIG_AP */
  4871. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  4872. wpas_notify_suspend(wpa_s->global);
  4873. } else if (os_strcmp(buf, "RESUME") == 0) {
  4874. wpas_notify_resume(wpa_s->global);
  4875. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  4876. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  4877. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  4878. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  4879. reply_len = -1;
  4880. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  4881. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  4882. reply_len = -1;
  4883. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  4884. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  4885. reply_len = -1;
  4886. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  4887. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  4888. buf + 17))
  4889. reply_len = -1;
  4890. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  4891. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  4892. reply_len = -1;
  4893. #ifdef CONFIG_TDLS
  4894. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  4895. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  4896. reply_len = -1;
  4897. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  4898. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  4899. reply_len = -1;
  4900. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  4901. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  4902. reply_len = -1;
  4903. #endif /* CONFIG_TDLS */
  4904. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  4905. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  4906. reply_size);
  4907. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  4908. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  4909. reply_size);
  4910. #ifdef CONFIG_AUTOSCAN
  4911. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  4912. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  4913. reply_len = -1;
  4914. #endif /* CONFIG_AUTOSCAN */
  4915. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  4916. pmksa_cache_clear_current(wpa_s->wpa);
  4917. eapol_sm_request_reauth(wpa_s->eapol);
  4918. #ifdef CONFIG_WNM
  4919. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  4920. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  4921. reply_len = -1;
  4922. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  4923. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  4924. reply_len = -1;
  4925. #endif /* CONFIG_WNM */
  4926. } else if (os_strcmp(buf, "FLUSH") == 0) {
  4927. wpa_supplicant_ctrl_iface_flush(wpa_s);
  4928. } else {
  4929. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  4930. reply_len = 16;
  4931. }
  4932. if (reply_len < 0) {
  4933. os_memcpy(reply, "FAIL\n", 5);
  4934. reply_len = 5;
  4935. }
  4936. *resp_len = reply_len;
  4937. return reply;
  4938. }
  4939. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  4940. char *cmd)
  4941. {
  4942. struct wpa_interface iface;
  4943. char *pos;
  4944. /*
  4945. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  4946. * TAB<bridge_ifname>
  4947. */
  4948. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  4949. os_memset(&iface, 0, sizeof(iface));
  4950. do {
  4951. iface.ifname = pos = cmd;
  4952. pos = os_strchr(pos, '\t');
  4953. if (pos)
  4954. *pos++ = '\0';
  4955. if (iface.ifname[0] == '\0')
  4956. return -1;
  4957. if (pos == NULL)
  4958. break;
  4959. iface.confname = pos;
  4960. pos = os_strchr(pos, '\t');
  4961. if (pos)
  4962. *pos++ = '\0';
  4963. if (iface.confname[0] == '\0')
  4964. iface.confname = NULL;
  4965. if (pos == NULL)
  4966. break;
  4967. iface.driver = pos;
  4968. pos = os_strchr(pos, '\t');
  4969. if (pos)
  4970. *pos++ = '\0';
  4971. if (iface.driver[0] == '\0')
  4972. iface.driver = NULL;
  4973. if (pos == NULL)
  4974. break;
  4975. iface.ctrl_interface = pos;
  4976. pos = os_strchr(pos, '\t');
  4977. if (pos)
  4978. *pos++ = '\0';
  4979. if (iface.ctrl_interface[0] == '\0')
  4980. iface.ctrl_interface = NULL;
  4981. if (pos == NULL)
  4982. break;
  4983. iface.driver_param = pos;
  4984. pos = os_strchr(pos, '\t');
  4985. if (pos)
  4986. *pos++ = '\0';
  4987. if (iface.driver_param[0] == '\0')
  4988. iface.driver_param = NULL;
  4989. if (pos == NULL)
  4990. break;
  4991. iface.bridge_ifname = pos;
  4992. pos = os_strchr(pos, '\t');
  4993. if (pos)
  4994. *pos++ = '\0';
  4995. if (iface.bridge_ifname[0] == '\0')
  4996. iface.bridge_ifname = NULL;
  4997. if (pos == NULL)
  4998. break;
  4999. } while (0);
  5000. if (wpa_supplicant_get_iface(global, iface.ifname))
  5001. return -1;
  5002. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  5003. }
  5004. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  5005. char *cmd)
  5006. {
  5007. struct wpa_supplicant *wpa_s;
  5008. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  5009. wpa_s = wpa_supplicant_get_iface(global, cmd);
  5010. if (wpa_s == NULL)
  5011. return -1;
  5012. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  5013. }
  5014. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  5015. {
  5016. struct wpa_interface_info *prev;
  5017. while (iface) {
  5018. prev = iface;
  5019. iface = iface->next;
  5020. os_free(prev->ifname);
  5021. os_free(prev->desc);
  5022. os_free(prev);
  5023. }
  5024. }
  5025. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  5026. char *buf, int len)
  5027. {
  5028. int i, res;
  5029. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  5030. char *pos, *end;
  5031. for (i = 0; wpa_drivers[i]; i++) {
  5032. struct wpa_driver_ops *drv = wpa_drivers[i];
  5033. if (drv->get_interfaces == NULL)
  5034. continue;
  5035. tmp = drv->get_interfaces(global->drv_priv[i]);
  5036. if (tmp == NULL)
  5037. continue;
  5038. if (last == NULL)
  5039. iface = last = tmp;
  5040. else
  5041. last->next = tmp;
  5042. while (last->next)
  5043. last = last->next;
  5044. }
  5045. pos = buf;
  5046. end = buf + len;
  5047. for (tmp = iface; tmp; tmp = tmp->next) {
  5048. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  5049. tmp->drv_name, tmp->ifname,
  5050. tmp->desc ? tmp->desc : "");
  5051. if (res < 0 || res >= end - pos) {
  5052. *pos = '\0';
  5053. break;
  5054. }
  5055. pos += res;
  5056. }
  5057. wpa_free_iface_info(iface);
  5058. return pos - buf;
  5059. }
  5060. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  5061. char *buf, int len)
  5062. {
  5063. int res;
  5064. char *pos, *end;
  5065. struct wpa_supplicant *wpa_s;
  5066. wpa_s = global->ifaces;
  5067. pos = buf;
  5068. end = buf + len;
  5069. while (wpa_s) {
  5070. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  5071. if (res < 0 || res >= end - pos) {
  5072. *pos = '\0';
  5073. break;
  5074. }
  5075. pos += res;
  5076. wpa_s = wpa_s->next;
  5077. }
  5078. return pos - buf;
  5079. }
  5080. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  5081. const char *ifname,
  5082. char *cmd, size_t *resp_len)
  5083. {
  5084. struct wpa_supplicant *wpa_s;
  5085. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5086. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  5087. break;
  5088. }
  5089. if (wpa_s == NULL) {
  5090. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  5091. if (resp)
  5092. *resp_len = os_strlen(resp);
  5093. else
  5094. *resp_len = 1;
  5095. return resp;
  5096. }
  5097. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  5098. }
  5099. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  5100. char *buf, size_t *resp_len)
  5101. {
  5102. #ifdef CONFIG_P2P
  5103. static const char * cmd[] = {
  5104. "P2P_FIND",
  5105. "P2P_STOP_FIND",
  5106. "P2P_LISTEN",
  5107. "P2P_GROUP_ADD",
  5108. "P2P_GET_PASSPHRASE",
  5109. "P2P_SERVICE_UPDATE",
  5110. "P2P_SERVICE_FLUSH",
  5111. "P2P_FLUSH",
  5112. "P2P_CANCEL",
  5113. "P2P_PRESENCE_REQ",
  5114. "P2P_EXT_LISTEN",
  5115. NULL
  5116. };
  5117. static const char * prefix[] = {
  5118. "P2P_FIND ",
  5119. "P2P_CONNECT ",
  5120. "P2P_LISTEN ",
  5121. "P2P_GROUP_REMOVE ",
  5122. "P2P_GROUP_ADD ",
  5123. "P2P_PROV_DISC ",
  5124. "P2P_SERV_DISC_REQ ",
  5125. "P2P_SERV_DISC_CANCEL_REQ ",
  5126. "P2P_SERV_DISC_RESP ",
  5127. "P2P_SERV_DISC_EXTERNAL ",
  5128. "P2P_SERVICE_ADD ",
  5129. "P2P_SERVICE_DEL ",
  5130. "P2P_REJECT ",
  5131. "P2P_INVITE ",
  5132. "P2P_PEER ",
  5133. "P2P_SET ",
  5134. "P2P_UNAUTHORIZE ",
  5135. "P2P_PRESENCE_REQ ",
  5136. "P2P_EXT_LISTEN ",
  5137. "P2P_REMOVE_CLIENT ",
  5138. NULL
  5139. };
  5140. int found = 0;
  5141. int i;
  5142. if (global->p2p_init_wpa_s == NULL)
  5143. return NULL;
  5144. for (i = 0; !found && cmd[i]; i++) {
  5145. if (os_strcmp(buf, cmd[i]) == 0)
  5146. found = 1;
  5147. }
  5148. for (i = 0; !found && prefix[i]; i++) {
  5149. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  5150. found = 1;
  5151. }
  5152. if (found)
  5153. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5154. buf, resp_len);
  5155. #endif /* CONFIG_P2P */
  5156. return NULL;
  5157. }
  5158. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  5159. char *buf, size_t *resp_len)
  5160. {
  5161. #ifdef CONFIG_WIFI_DISPLAY
  5162. if (global->p2p_init_wpa_s == NULL)
  5163. return NULL;
  5164. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  5165. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  5166. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5167. buf, resp_len);
  5168. #endif /* CONFIG_WIFI_DISPLAY */
  5169. return NULL;
  5170. }
  5171. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  5172. char *buf, size_t *resp_len)
  5173. {
  5174. char *ret;
  5175. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  5176. if (ret)
  5177. return ret;
  5178. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  5179. if (ret)
  5180. return ret;
  5181. return NULL;
  5182. }
  5183. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  5184. {
  5185. char *value;
  5186. value = os_strchr(cmd, ' ');
  5187. if (value == NULL)
  5188. return -1;
  5189. *value++ = '\0';
  5190. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  5191. #ifdef CONFIG_WIFI_DISPLAY
  5192. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  5193. wifi_display_enable(global, !!atoi(value));
  5194. return 0;
  5195. }
  5196. #endif /* CONFIG_WIFI_DISPLAY */
  5197. return -1;
  5198. }
  5199. #ifndef CONFIG_NO_CONFIG_WRITE
  5200. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  5201. {
  5202. int ret = 0;
  5203. struct wpa_supplicant *wpa_s;
  5204. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5205. if (!wpa_s->conf->update_config) {
  5206. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  5207. continue;
  5208. }
  5209. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  5210. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  5211. ret = 1;
  5212. } else {
  5213. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  5214. }
  5215. }
  5216. return ret;
  5217. }
  5218. #endif /* CONFIG_NO_CONFIG_WRITE */
  5219. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  5220. char *buf, size_t buflen)
  5221. {
  5222. char *pos, *end;
  5223. int ret;
  5224. struct wpa_supplicant *wpa_s;
  5225. pos = buf;
  5226. end = buf + buflen;
  5227. #ifdef CONFIG_P2P
  5228. if (global->p2p && !global->p2p_disabled) {
  5229. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  5230. "\n"
  5231. "p2p_state=%s\n",
  5232. MAC2STR(global->p2p_dev_addr),
  5233. p2p_get_state_txt(global->p2p));
  5234. if (ret < 0 || ret >= end - pos)
  5235. return pos - buf;
  5236. pos += ret;
  5237. } else if (global->p2p) {
  5238. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  5239. if (ret < 0 || ret >= end - pos)
  5240. return pos - buf;
  5241. pos += ret;
  5242. }
  5243. #endif /* CONFIG_P2P */
  5244. #ifdef CONFIG_WIFI_DISPLAY
  5245. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  5246. !!global->wifi_display);
  5247. if (ret < 0 || ret >= end - pos)
  5248. return pos - buf;
  5249. pos += ret;
  5250. #endif /* CONFIG_WIFI_DISPLAY */
  5251. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5252. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  5253. "address=" MACSTR "\n",
  5254. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  5255. if (ret < 0 || ret >= end - pos)
  5256. return pos - buf;
  5257. pos += ret;
  5258. }
  5259. return pos - buf;
  5260. }
  5261. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  5262. char *buf, size_t *resp_len)
  5263. {
  5264. char *reply;
  5265. const int reply_size = 2048;
  5266. int reply_len;
  5267. int level = MSG_DEBUG;
  5268. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  5269. char *pos = os_strchr(buf + 7, ' ');
  5270. if (pos) {
  5271. *pos++ = '\0';
  5272. return wpas_global_ctrl_iface_ifname(global,
  5273. buf + 7, pos,
  5274. resp_len);
  5275. }
  5276. }
  5277. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  5278. if (reply)
  5279. return reply;
  5280. if (os_strcmp(buf, "PING") == 0)
  5281. level = MSG_EXCESSIVE;
  5282. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  5283. (const u8 *) buf, os_strlen(buf));
  5284. reply = os_malloc(reply_size);
  5285. if (reply == NULL) {
  5286. *resp_len = 1;
  5287. return NULL;
  5288. }
  5289. os_memcpy(reply, "OK\n", 3);
  5290. reply_len = 3;
  5291. if (os_strcmp(buf, "PING") == 0) {
  5292. os_memcpy(reply, "PONG\n", 5);
  5293. reply_len = 5;
  5294. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  5295. if (wpa_supplicant_global_iface_add(global, buf + 14))
  5296. reply_len = -1;
  5297. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  5298. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  5299. reply_len = -1;
  5300. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5301. reply_len = wpa_supplicant_global_iface_list(
  5302. global, reply, reply_size);
  5303. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5304. reply_len = wpa_supplicant_global_iface_interfaces(
  5305. global, reply, reply_size);
  5306. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5307. wpa_supplicant_terminate_proc(global);
  5308. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5309. wpas_notify_suspend(global);
  5310. } else if (os_strcmp(buf, "RESUME") == 0) {
  5311. wpas_notify_resume(global);
  5312. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5313. if (wpas_global_ctrl_iface_set(global, buf + 4))
  5314. reply_len = -1;
  5315. #ifndef CONFIG_NO_CONFIG_WRITE
  5316. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5317. if (wpas_global_ctrl_iface_save_config(global))
  5318. reply_len = -1;
  5319. #endif /* CONFIG_NO_CONFIG_WRITE */
  5320. } else if (os_strcmp(buf, "STATUS") == 0) {
  5321. reply_len = wpas_global_ctrl_iface_status(global, reply,
  5322. reply_size);
  5323. } else {
  5324. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5325. reply_len = 16;
  5326. }
  5327. if (reply_len < 0) {
  5328. os_memcpy(reply, "FAIL\n", 5);
  5329. reply_len = 5;
  5330. }
  5331. *resp_len = reply_len;
  5332. return reply;
  5333. }