ctrl_iface.c 199 KB

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