ctrl_iface.c 177 KB

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