ctrl_iface.c 241 KB

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