ctrl_iface.c 141 KB

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