ctrl_iface.c 164 KB

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