ctrl_iface.c 224 KB

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