ctrl_iface.c 212 KB

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