ctrl_iface.c 163 KB

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