ctrl_iface.c 145 KB

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