ctrl_iface.c 247 KB

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