ctrl_iface.c 204 KB

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