ctrl_iface.c 232 KB

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