ctrl_iface.c 190 KB

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