ctrl_iface.c 201 KB

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