ctrl_iface.c 193 KB

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