ctrl_iface.c 234 KB

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