ctrl_iface.c 144 KB

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