ctrl_iface.c 156 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645
  1. /*
  2. * WPA Supplicant / Control interface (shared code for all backends)
  3. * Copyright (c) 2004-2014, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "utils/includes.h"
  9. #include "utils/common.h"
  10. #include "utils/eloop.h"
  11. #include "utils/uuid.h"
  12. #include "common/version.h"
  13. #include "common/ieee802_11_defs.h"
  14. #include "common/ieee802_11_common.h"
  15. #include "common/wpa_ctrl.h"
  16. #include "eap_peer/eap.h"
  17. #include "eapol_supp/eapol_supp_sm.h"
  18. #include "rsn_supp/wpa.h"
  19. #include "rsn_supp/preauth.h"
  20. #include "rsn_supp/pmksa_cache.h"
  21. #include "l2_packet/l2_packet.h"
  22. #include "wps/wps.h"
  23. #include "config.h"
  24. #include "wpa_supplicant_i.h"
  25. #include "driver_i.h"
  26. #include "wps_supplicant.h"
  27. #include "ibss_rsn.h"
  28. #include "ap.h"
  29. #include "p2p_supplicant.h"
  30. #include "p2p/p2p.h"
  31. #include "hs20_supplicant.h"
  32. #include "wifi_display.h"
  33. #include "notify.h"
  34. #include "bss.h"
  35. #include "scan.h"
  36. #include "ctrl_iface.h"
  37. #include "interworking.h"
  38. #include "blacklist.h"
  39. #include "autoscan.h"
  40. #include "wnm_sta.h"
  41. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  42. char *buf, int len);
  43. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  44. char *buf, int len);
  45. static int pno_start(struct wpa_supplicant *wpa_s)
  46. {
  47. int ret, interval;
  48. size_t i, num_ssid;
  49. struct wpa_ssid *ssid;
  50. struct wpa_driver_scan_params params;
  51. if (wpa_s->pno || wpa_s->pno_sched_pending)
  52. return 0;
  53. if ((wpa_s->wpa_state > WPA_SCANNING) &&
  54. (wpa_s->wpa_state <= WPA_COMPLETED)) {
  55. wpa_printf(MSG_ERROR, "PNO: In assoc process");
  56. return -EAGAIN;
  57. }
  58. if (wpa_s->wpa_state == WPA_SCANNING) {
  59. wpa_supplicant_cancel_scan(wpa_s);
  60. if (wpa_s->sched_scanning) {
  61. wpa_printf(MSG_DEBUG, "Schedule PNO on completion of "
  62. "ongoing sched scan");
  63. wpa_supplicant_cancel_sched_scan(wpa_s);
  64. wpa_s->pno_sched_pending = 1;
  65. return 0;
  66. }
  67. }
  68. os_memset(&params, 0, sizeof(params));
  69. num_ssid = 0;
  70. ssid = wpa_s->conf->ssid;
  71. while (ssid) {
  72. if (!wpas_network_disabled(wpa_s, ssid))
  73. num_ssid++;
  74. ssid = ssid->next;
  75. }
  76. if (num_ssid > WPAS_MAX_SCAN_SSIDS) {
  77. wpa_printf(MSG_DEBUG, "PNO: Use only the first %u SSIDs from "
  78. "%u", WPAS_MAX_SCAN_SSIDS, (unsigned int) num_ssid);
  79. num_ssid = WPAS_MAX_SCAN_SSIDS;
  80. }
  81. if (num_ssid == 0) {
  82. wpa_printf(MSG_DEBUG, "PNO: No configured SSIDs");
  83. return -1;
  84. }
  85. params.filter_ssids = os_malloc(sizeof(struct wpa_driver_scan_filter) *
  86. num_ssid);
  87. if (params.filter_ssids == NULL)
  88. return -1;
  89. i = 0;
  90. ssid = wpa_s->conf->ssid;
  91. while (ssid) {
  92. if (!wpas_network_disabled(wpa_s, ssid)) {
  93. params.ssids[i].ssid = ssid->ssid;
  94. params.ssids[i].ssid_len = ssid->ssid_len;
  95. params.num_ssids++;
  96. os_memcpy(params.filter_ssids[i].ssid, ssid->ssid,
  97. ssid->ssid_len);
  98. params.filter_ssids[i].ssid_len = ssid->ssid_len;
  99. params.num_filter_ssids++;
  100. i++;
  101. if (i == num_ssid)
  102. break;
  103. }
  104. ssid = ssid->next;
  105. }
  106. if (wpa_s->conf->filter_rssi)
  107. params.filter_rssi = wpa_s->conf->filter_rssi;
  108. interval = wpa_s->conf->sched_scan_interval ?
  109. wpa_s->conf->sched_scan_interval : 10;
  110. ret = wpa_supplicant_start_sched_scan(wpa_s, &params, interval);
  111. os_free(params.filter_ssids);
  112. if (ret == 0)
  113. wpa_s->pno = 1;
  114. return ret;
  115. }
  116. static int pno_stop(struct wpa_supplicant *wpa_s)
  117. {
  118. int ret = 0;
  119. if (wpa_s->pno || wpa_s->sched_scanning) {
  120. wpa_s->pno = 0;
  121. ret = wpa_supplicant_stop_sched_scan(wpa_s);
  122. }
  123. wpa_s->pno_sched_pending = 0;
  124. if (wpa_s->wpa_state == WPA_SCANNING)
  125. wpa_supplicant_req_scan(wpa_s, 0, 0);
  126. return ret;
  127. }
  128. static int set_bssid_filter(struct wpa_supplicant *wpa_s, char *val)
  129. {
  130. char *pos;
  131. u8 addr[ETH_ALEN], *filter = NULL, *n;
  132. size_t count = 0;
  133. pos = val;
  134. while (pos) {
  135. if (*pos == '\0')
  136. break;
  137. if (hwaddr_aton(pos, addr)) {
  138. os_free(filter);
  139. return -1;
  140. }
  141. n = os_realloc_array(filter, count + 1, ETH_ALEN);
  142. if (n == NULL) {
  143. os_free(filter);
  144. return -1;
  145. }
  146. filter = n;
  147. os_memcpy(filter + count * ETH_ALEN, addr, ETH_ALEN);
  148. count++;
  149. pos = os_strchr(pos, ' ');
  150. if (pos)
  151. pos++;
  152. }
  153. wpa_hexdump(MSG_DEBUG, "bssid_filter", filter, count * ETH_ALEN);
  154. os_free(wpa_s->bssid_filter);
  155. wpa_s->bssid_filter = filter;
  156. wpa_s->bssid_filter_count = count;
  157. return 0;
  158. }
  159. static int set_disallow_aps(struct wpa_supplicant *wpa_s, char *val)
  160. {
  161. char *pos;
  162. u8 addr[ETH_ALEN], *bssid = NULL, *n;
  163. struct wpa_ssid_value *ssid = NULL, *ns;
  164. size_t count = 0, ssid_count = 0;
  165. struct wpa_ssid *c;
  166. /*
  167. * disallow_list ::= <ssid_spec> | <bssid_spec> | <disallow_list> | ""
  168. * SSID_SPEC ::= ssid <SSID_HEX>
  169. * BSSID_SPEC ::= bssid <BSSID_HEX>
  170. */
  171. pos = val;
  172. while (pos) {
  173. if (*pos == '\0')
  174. break;
  175. if (os_strncmp(pos, "bssid ", 6) == 0) {
  176. int res;
  177. pos += 6;
  178. res = hwaddr_aton2(pos, addr);
  179. if (res < 0) {
  180. os_free(ssid);
  181. os_free(bssid);
  182. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  183. "BSSID value '%s'", pos);
  184. return -1;
  185. }
  186. pos += res;
  187. n = os_realloc_array(bssid, count + 1, ETH_ALEN);
  188. if (n == NULL) {
  189. os_free(ssid);
  190. os_free(bssid);
  191. return -1;
  192. }
  193. bssid = n;
  194. os_memcpy(bssid + count * ETH_ALEN, addr, ETH_ALEN);
  195. count++;
  196. } else if (os_strncmp(pos, "ssid ", 5) == 0) {
  197. char *end;
  198. pos += 5;
  199. end = pos;
  200. while (*end) {
  201. if (*end == '\0' || *end == ' ')
  202. break;
  203. end++;
  204. }
  205. ns = os_realloc_array(ssid, ssid_count + 1,
  206. sizeof(struct wpa_ssid_value));
  207. if (ns == NULL) {
  208. os_free(ssid);
  209. os_free(bssid);
  210. return -1;
  211. }
  212. ssid = ns;
  213. if ((end - pos) & 0x01 || end - pos > 2 * 32 ||
  214. hexstr2bin(pos, ssid[ssid_count].ssid,
  215. (end - pos) / 2) < 0) {
  216. os_free(ssid);
  217. os_free(bssid);
  218. wpa_printf(MSG_DEBUG, "Invalid disallow_aps "
  219. "SSID value '%s'", pos);
  220. return -1;
  221. }
  222. ssid[ssid_count].ssid_len = (end - pos) / 2;
  223. wpa_hexdump_ascii(MSG_DEBUG, "disallow_aps SSID",
  224. ssid[ssid_count].ssid,
  225. ssid[ssid_count].ssid_len);
  226. ssid_count++;
  227. pos = end;
  228. } else {
  229. wpa_printf(MSG_DEBUG, "Unexpected disallow_aps value "
  230. "'%s'", pos);
  231. os_free(ssid);
  232. os_free(bssid);
  233. return -1;
  234. }
  235. pos = os_strchr(pos, ' ');
  236. if (pos)
  237. pos++;
  238. }
  239. wpa_hexdump(MSG_DEBUG, "disallow_aps_bssid", bssid, count * ETH_ALEN);
  240. os_free(wpa_s->disallow_aps_bssid);
  241. wpa_s->disallow_aps_bssid = bssid;
  242. wpa_s->disallow_aps_bssid_count = count;
  243. wpa_printf(MSG_DEBUG, "disallow_aps_ssid_count %d", (int) ssid_count);
  244. os_free(wpa_s->disallow_aps_ssid);
  245. wpa_s->disallow_aps_ssid = ssid;
  246. wpa_s->disallow_aps_ssid_count = ssid_count;
  247. if (!wpa_s->current_ssid || wpa_s->wpa_state < WPA_AUTHENTICATING)
  248. return 0;
  249. c = wpa_s->current_ssid;
  250. if (c->mode != WPAS_MODE_INFRA && c->mode != WPAS_MODE_IBSS)
  251. return 0;
  252. if (!disallowed_bssid(wpa_s, wpa_s->bssid) &&
  253. !disallowed_ssid(wpa_s, c->ssid, c->ssid_len))
  254. return 0;
  255. wpa_printf(MSG_DEBUG, "Disconnect and try to find another network "
  256. "because current AP was marked disallowed");
  257. #ifdef CONFIG_SME
  258. wpa_s->sme.prev_bssid_set = 0;
  259. #endif /* CONFIG_SME */
  260. wpa_s->reassociate = 1;
  261. wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  262. wpa_supplicant_req_scan(wpa_s, 0, 0);
  263. return 0;
  264. }
  265. static int wpa_supplicant_ctrl_iface_set(struct wpa_supplicant *wpa_s,
  266. char *cmd)
  267. {
  268. char *value;
  269. int ret = 0;
  270. value = os_strchr(cmd, ' ');
  271. if (value == NULL)
  272. return -1;
  273. *value++ = '\0';
  274. wpa_printf(MSG_DEBUG, "CTRL_IFACE SET '%s'='%s'", cmd, value);
  275. if (os_strcasecmp(cmd, "EAPOL::heldPeriod") == 0) {
  276. eapol_sm_configure(wpa_s->eapol,
  277. atoi(value), -1, -1, -1);
  278. } else if (os_strcasecmp(cmd, "EAPOL::authPeriod") == 0) {
  279. eapol_sm_configure(wpa_s->eapol,
  280. -1, atoi(value), -1, -1);
  281. } else if (os_strcasecmp(cmd, "EAPOL::startPeriod") == 0) {
  282. eapol_sm_configure(wpa_s->eapol,
  283. -1, -1, atoi(value), -1);
  284. } else if (os_strcasecmp(cmd, "EAPOL::maxStart") == 0) {
  285. eapol_sm_configure(wpa_s->eapol,
  286. -1, -1, -1, atoi(value));
  287. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKLifetime") == 0) {
  288. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME,
  289. atoi(value)))
  290. ret = -1;
  291. } else if (os_strcasecmp(cmd, "dot11RSNAConfigPMKReauthThreshold") ==
  292. 0) {
  293. if (wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD,
  294. atoi(value)))
  295. ret = -1;
  296. } else if (os_strcasecmp(cmd, "dot11RSNAConfigSATimeout") == 0) {
  297. if (wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, atoi(value)))
  298. ret = -1;
  299. } else if (os_strcasecmp(cmd, "wps_fragment_size") == 0) {
  300. wpa_s->wps_fragment_size = atoi(value);
  301. #ifdef CONFIG_WPS_TESTING
  302. } else if (os_strcasecmp(cmd, "wps_version_number") == 0) {
  303. long int val;
  304. val = strtol(value, NULL, 0);
  305. if (val < 0 || val > 0xff) {
  306. ret = -1;
  307. wpa_printf(MSG_DEBUG, "WPS: Invalid "
  308. "wps_version_number %ld", val);
  309. } else {
  310. wps_version_number = val;
  311. wpa_printf(MSG_DEBUG, "WPS: Testing - force WPS "
  312. "version %u.%u",
  313. (wps_version_number & 0xf0) >> 4,
  314. wps_version_number & 0x0f);
  315. }
  316. } else if (os_strcasecmp(cmd, "wps_testing_dummy_cred") == 0) {
  317. wps_testing_dummy_cred = atoi(value);
  318. wpa_printf(MSG_DEBUG, "WPS: Testing - dummy_cred=%d",
  319. wps_testing_dummy_cred);
  320. #endif /* CONFIG_WPS_TESTING */
  321. } else if (os_strcasecmp(cmd, "ampdu") == 0) {
  322. if (wpa_drv_ampdu(wpa_s, atoi(value)) < 0)
  323. ret = -1;
  324. #ifdef CONFIG_TDLS
  325. #ifdef CONFIG_TDLS_TESTING
  326. } else if (os_strcasecmp(cmd, "tdls_testing") == 0) {
  327. extern unsigned int tdls_testing;
  328. tdls_testing = strtol(value, NULL, 0);
  329. wpa_printf(MSG_DEBUG, "TDLS: tdls_testing=0x%x", tdls_testing);
  330. #endif /* CONFIG_TDLS_TESTING */
  331. } else if (os_strcasecmp(cmd, "tdls_disabled") == 0) {
  332. int disabled = atoi(value);
  333. wpa_printf(MSG_DEBUG, "TDLS: tdls_disabled=%d", disabled);
  334. if (disabled) {
  335. if (wpa_drv_tdls_oper(wpa_s, TDLS_DISABLE, NULL) < 0)
  336. ret = -1;
  337. } else if (wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL) < 0)
  338. ret = -1;
  339. wpa_tdls_enable(wpa_s->wpa, !disabled);
  340. #endif /* CONFIG_TDLS */
  341. } else if (os_strcasecmp(cmd, "pno") == 0) {
  342. if (atoi(value))
  343. ret = pno_start(wpa_s);
  344. else
  345. ret = pno_stop(wpa_s);
  346. } else if (os_strcasecmp(cmd, "radio_disabled") == 0) {
  347. int disabled = atoi(value);
  348. if (wpa_drv_radio_disable(wpa_s, disabled) < 0)
  349. ret = -1;
  350. else if (disabled)
  351. wpa_supplicant_set_state(wpa_s, WPA_INACTIVE);
  352. } else if (os_strcasecmp(cmd, "uapsd") == 0) {
  353. if (os_strcmp(value, "disable") == 0)
  354. wpa_s->set_sta_uapsd = 0;
  355. else {
  356. int be, bk, vi, vo;
  357. char *pos;
  358. /* format: BE,BK,VI,VO;max SP Length */
  359. be = atoi(value);
  360. pos = os_strchr(value, ',');
  361. if (pos == NULL)
  362. return -1;
  363. pos++;
  364. bk = atoi(pos);
  365. pos = os_strchr(pos, ',');
  366. if (pos == NULL)
  367. return -1;
  368. pos++;
  369. vi = atoi(pos);
  370. pos = os_strchr(pos, ',');
  371. if (pos == NULL)
  372. return -1;
  373. pos++;
  374. vo = atoi(pos);
  375. /* ignore max SP Length for now */
  376. wpa_s->set_sta_uapsd = 1;
  377. wpa_s->sta_uapsd = 0;
  378. if (be)
  379. wpa_s->sta_uapsd |= BIT(0);
  380. if (bk)
  381. wpa_s->sta_uapsd |= BIT(1);
  382. if (vi)
  383. wpa_s->sta_uapsd |= BIT(2);
  384. if (vo)
  385. wpa_s->sta_uapsd |= BIT(3);
  386. }
  387. } else if (os_strcasecmp(cmd, "ps") == 0) {
  388. ret = wpa_drv_set_p2p_powersave(wpa_s, atoi(value), -1, -1);
  389. #ifdef CONFIG_WIFI_DISPLAY
  390. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  391. wifi_display_enable(wpa_s->global, !!atoi(value));
  392. #endif /* CONFIG_WIFI_DISPLAY */
  393. } else if (os_strcasecmp(cmd, "bssid_filter") == 0) {
  394. ret = set_bssid_filter(wpa_s, value);
  395. } else if (os_strcasecmp(cmd, "disallow_aps") == 0) {
  396. ret = set_disallow_aps(wpa_s, value);
  397. } else if (os_strcasecmp(cmd, "no_keep_alive") == 0) {
  398. wpa_s->no_keep_alive = !!atoi(value);
  399. } else {
  400. value[-1] = '=';
  401. ret = wpa_config_process_global(wpa_s->conf, cmd, -1);
  402. if (ret == 0)
  403. wpa_supplicant_update_config(wpa_s);
  404. }
  405. return ret;
  406. }
  407. static int wpa_supplicant_ctrl_iface_get(struct wpa_supplicant *wpa_s,
  408. char *cmd, char *buf, size_t buflen)
  409. {
  410. int res = -1;
  411. wpa_printf(MSG_DEBUG, "CTRL_IFACE GET '%s'", cmd);
  412. if (os_strcmp(cmd, "version") == 0) {
  413. res = os_snprintf(buf, buflen, "%s", VERSION_STR);
  414. } else if (os_strcasecmp(cmd, "country") == 0) {
  415. if (wpa_s->conf->country[0] && wpa_s->conf->country[1])
  416. res = os_snprintf(buf, buflen, "%c%c",
  417. wpa_s->conf->country[0],
  418. wpa_s->conf->country[1]);
  419. #ifdef CONFIG_WIFI_DISPLAY
  420. } else if (os_strcasecmp(cmd, "wifi_display") == 0) {
  421. res = os_snprintf(buf, buflen, "%d",
  422. wpa_s->global->wifi_display);
  423. if (res < 0 || (unsigned int) res >= buflen)
  424. return -1;
  425. return res;
  426. #endif /* CONFIG_WIFI_DISPLAY */
  427. #ifdef CONFIG_TESTING_GET_GTK
  428. } else if (os_strcmp(cmd, "gtk") == 0) {
  429. if (wpa_s->last_gtk_len == 0)
  430. return -1;
  431. res = wpa_snprintf_hex(buf, buflen, wpa_s->last_gtk,
  432. wpa_s->last_gtk_len);
  433. return res;
  434. #endif /* CONFIG_TESTING_GET_GTK */
  435. }
  436. if (res < 0 || (unsigned int) res >= buflen)
  437. return -1;
  438. return res;
  439. }
  440. #ifdef IEEE8021X_EAPOL
  441. static int wpa_supplicant_ctrl_iface_preauth(struct wpa_supplicant *wpa_s,
  442. char *addr)
  443. {
  444. u8 bssid[ETH_ALEN];
  445. struct wpa_ssid *ssid = wpa_s->current_ssid;
  446. if (hwaddr_aton(addr, bssid)) {
  447. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH: invalid address "
  448. "'%s'", addr);
  449. return -1;
  450. }
  451. wpa_printf(MSG_DEBUG, "CTRL_IFACE PREAUTH " MACSTR, MAC2STR(bssid));
  452. rsn_preauth_deinit(wpa_s->wpa);
  453. if (rsn_preauth_init(wpa_s->wpa, bssid, ssid ? &ssid->eap : NULL))
  454. return -1;
  455. return 0;
  456. }
  457. #endif /* IEEE8021X_EAPOL */
  458. #ifdef CONFIG_PEERKEY
  459. /* MLME-STKSTART.request(peer) */
  460. static int wpa_supplicant_ctrl_iface_stkstart(
  461. struct wpa_supplicant *wpa_s, char *addr)
  462. {
  463. u8 peer[ETH_ALEN];
  464. if (hwaddr_aton(addr, peer)) {
  465. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART: invalid "
  466. "address '%s'", addr);
  467. return -1;
  468. }
  469. wpa_printf(MSG_DEBUG, "CTRL_IFACE STKSTART " MACSTR,
  470. MAC2STR(peer));
  471. return wpa_sm_stkstart(wpa_s->wpa, peer);
  472. }
  473. #endif /* CONFIG_PEERKEY */
  474. #ifdef CONFIG_TDLS
  475. static int wpa_supplicant_ctrl_iface_tdls_discover(
  476. struct wpa_supplicant *wpa_s, char *addr)
  477. {
  478. u8 peer[ETH_ALEN];
  479. int ret;
  480. if (hwaddr_aton(addr, peer)) {
  481. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER: invalid "
  482. "address '%s'", addr);
  483. return -1;
  484. }
  485. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_DISCOVER " MACSTR,
  486. MAC2STR(peer));
  487. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  488. ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
  489. else
  490. ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
  491. return ret;
  492. }
  493. static int wpa_supplicant_ctrl_iface_tdls_setup(
  494. struct wpa_supplicant *wpa_s, char *addr)
  495. {
  496. u8 peer[ETH_ALEN];
  497. int ret;
  498. if (hwaddr_aton(addr, peer)) {
  499. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP: invalid "
  500. "address '%s'", addr);
  501. return -1;
  502. }
  503. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_SETUP " MACSTR,
  504. MAC2STR(peer));
  505. if ((wpa_s->conf->tdls_external_control) &&
  506. wpa_tdls_is_external_setup(wpa_s->wpa))
  507. return wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  508. wpa_tdls_remove(wpa_s->wpa, peer);
  509. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  510. ret = wpa_tdls_start(wpa_s->wpa, peer);
  511. else
  512. ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
  513. return ret;
  514. }
  515. static int wpa_supplicant_ctrl_iface_tdls_teardown(
  516. struct wpa_supplicant *wpa_s, char *addr)
  517. {
  518. u8 peer[ETH_ALEN];
  519. int ret;
  520. if (hwaddr_aton(addr, peer)) {
  521. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN: invalid "
  522. "address '%s'", addr);
  523. return -1;
  524. }
  525. wpa_printf(MSG_DEBUG, "CTRL_IFACE TDLS_TEARDOWN " MACSTR,
  526. MAC2STR(peer));
  527. if ((wpa_s->conf->tdls_external_control) &&
  528. wpa_tdls_is_external_setup(wpa_s->wpa))
  529. return wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  530. if (wpa_tdls_is_external_setup(wpa_s->wpa))
  531. ret = wpa_tdls_teardown_link(
  532. wpa_s->wpa, peer,
  533. WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
  534. else
  535. ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
  536. return ret;
  537. }
  538. #endif /* CONFIG_TDLS */
  539. #ifdef CONFIG_IEEE80211R
  540. static int wpa_supplicant_ctrl_iface_ft_ds(
  541. struct wpa_supplicant *wpa_s, char *addr)
  542. {
  543. u8 target_ap[ETH_ALEN];
  544. struct wpa_bss *bss;
  545. const u8 *mdie;
  546. if (hwaddr_aton(addr, target_ap)) {
  547. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS: invalid "
  548. "address '%s'", addr);
  549. return -1;
  550. }
  551. wpa_printf(MSG_DEBUG, "CTRL_IFACE FT_DS " MACSTR, MAC2STR(target_ap));
  552. bss = wpa_bss_get_bssid(wpa_s, target_ap);
  553. if (bss)
  554. mdie = wpa_bss_get_ie(bss, WLAN_EID_MOBILITY_DOMAIN);
  555. else
  556. mdie = NULL;
  557. return wpa_ft_start_over_ds(wpa_s->wpa, target_ap, mdie);
  558. }
  559. #endif /* CONFIG_IEEE80211R */
  560. #ifdef CONFIG_WPS
  561. static int wpa_supplicant_ctrl_iface_wps_pbc(struct wpa_supplicant *wpa_s,
  562. char *cmd)
  563. {
  564. u8 bssid[ETH_ALEN], *_bssid = bssid;
  565. #ifdef CONFIG_P2P
  566. u8 p2p_dev_addr[ETH_ALEN];
  567. #endif /* CONFIG_P2P */
  568. #ifdef CONFIG_AP
  569. u8 *_p2p_dev_addr = NULL;
  570. #endif /* CONFIG_AP */
  571. if (cmd == NULL || os_strcmp(cmd, "any") == 0) {
  572. _bssid = NULL;
  573. #ifdef CONFIG_P2P
  574. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  575. if (hwaddr_aton(cmd + 13, p2p_dev_addr)) {
  576. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid "
  577. "P2P Device Address '%s'",
  578. cmd + 13);
  579. return -1;
  580. }
  581. _p2p_dev_addr = p2p_dev_addr;
  582. #endif /* CONFIG_P2P */
  583. } else if (hwaddr_aton(cmd, bssid)) {
  584. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PBC: invalid BSSID '%s'",
  585. cmd);
  586. return -1;
  587. }
  588. #ifdef CONFIG_AP
  589. if (wpa_s->ap_iface)
  590. return wpa_supplicant_ap_wps_pbc(wpa_s, _bssid, _p2p_dev_addr);
  591. #endif /* CONFIG_AP */
  592. return wpas_wps_start_pbc(wpa_s, _bssid, 0);
  593. }
  594. static int wpa_supplicant_ctrl_iface_wps_pin(struct wpa_supplicant *wpa_s,
  595. char *cmd, char *buf,
  596. size_t buflen)
  597. {
  598. u8 bssid[ETH_ALEN], *_bssid = bssid;
  599. char *pin;
  600. int ret;
  601. pin = os_strchr(cmd, ' ');
  602. if (pin)
  603. *pin++ = '\0';
  604. if (os_strcmp(cmd, "any") == 0)
  605. _bssid = NULL;
  606. else if (os_strcmp(cmd, "get") == 0) {
  607. ret = wps_generate_pin();
  608. goto done;
  609. } else if (hwaddr_aton(cmd, bssid)) {
  610. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_PIN: invalid BSSID '%s'",
  611. cmd);
  612. return -1;
  613. }
  614. #ifdef CONFIG_AP
  615. if (wpa_s->ap_iface) {
  616. int timeout = 0;
  617. char *pos;
  618. if (pin) {
  619. pos = os_strchr(pin, ' ');
  620. if (pos) {
  621. *pos++ = '\0';
  622. timeout = atoi(pos);
  623. }
  624. }
  625. return wpa_supplicant_ap_wps_pin(wpa_s, _bssid, pin,
  626. buf, buflen, timeout);
  627. }
  628. #endif /* CONFIG_AP */
  629. if (pin) {
  630. ret = wpas_wps_start_pin(wpa_s, _bssid, pin, 0,
  631. DEV_PW_DEFAULT);
  632. if (ret < 0)
  633. return -1;
  634. ret = os_snprintf(buf, buflen, "%s", pin);
  635. if (ret < 0 || (size_t) ret >= buflen)
  636. return -1;
  637. return ret;
  638. }
  639. ret = wpas_wps_start_pin(wpa_s, _bssid, NULL, 0, DEV_PW_DEFAULT);
  640. if (ret < 0)
  641. return -1;
  642. done:
  643. /* Return the generated PIN */
  644. ret = os_snprintf(buf, buflen, "%08d", ret);
  645. if (ret < 0 || (size_t) ret >= buflen)
  646. return -1;
  647. return ret;
  648. }
  649. static int wpa_supplicant_ctrl_iface_wps_check_pin(
  650. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  651. {
  652. char pin[9];
  653. size_t len;
  654. char *pos;
  655. int ret;
  656. wpa_hexdump_ascii_key(MSG_DEBUG, "WPS_CHECK_PIN",
  657. (u8 *) cmd, os_strlen(cmd));
  658. for (pos = cmd, len = 0; *pos != '\0'; pos++) {
  659. if (*pos < '0' || *pos > '9')
  660. continue;
  661. pin[len++] = *pos;
  662. if (len == 9) {
  663. wpa_printf(MSG_DEBUG, "WPS: Too long PIN");
  664. return -1;
  665. }
  666. }
  667. if (len != 4 && len != 8) {
  668. wpa_printf(MSG_DEBUG, "WPS: Invalid PIN length %d", (int) len);
  669. return -1;
  670. }
  671. pin[len] = '\0';
  672. if (len == 8) {
  673. unsigned int pin_val;
  674. pin_val = atoi(pin);
  675. if (!wps_pin_valid(pin_val)) {
  676. wpa_printf(MSG_DEBUG, "WPS: Invalid checksum digit");
  677. ret = os_snprintf(buf, buflen, "FAIL-CHECKSUM\n");
  678. if (ret < 0 || (size_t) ret >= buflen)
  679. return -1;
  680. return ret;
  681. }
  682. }
  683. ret = os_snprintf(buf, buflen, "%s", pin);
  684. if (ret < 0 || (size_t) ret >= buflen)
  685. return -1;
  686. return ret;
  687. }
  688. #ifdef CONFIG_WPS_NFC
  689. static int wpa_supplicant_ctrl_iface_wps_nfc(struct wpa_supplicant *wpa_s,
  690. char *cmd)
  691. {
  692. u8 bssid[ETH_ALEN], *_bssid = bssid;
  693. if (cmd == NULL || cmd[0] == '\0')
  694. _bssid = NULL;
  695. else if (hwaddr_aton(cmd, bssid))
  696. return -1;
  697. return wpas_wps_start_nfc(wpa_s, _bssid, NULL, 0, 0, NULL, NULL, 0);
  698. }
  699. static int wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  700. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  701. {
  702. int ndef;
  703. struct wpabuf *buf;
  704. int res;
  705. char *pos;
  706. pos = os_strchr(cmd, ' ');
  707. if (pos)
  708. *pos++ = '\0';
  709. if (os_strcmp(cmd, "WPS") == 0)
  710. ndef = 0;
  711. else if (os_strcmp(cmd, "NDEF") == 0)
  712. ndef = 1;
  713. else
  714. return -1;
  715. buf = wpas_wps_nfc_config_token(wpa_s, ndef, pos);
  716. if (buf == NULL)
  717. return -1;
  718. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  719. wpabuf_len(buf));
  720. reply[res++] = '\n';
  721. reply[res] = '\0';
  722. wpabuf_free(buf);
  723. return res;
  724. }
  725. static int wpa_supplicant_ctrl_iface_wps_nfc_token(
  726. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  727. {
  728. int ndef;
  729. struct wpabuf *buf;
  730. int res;
  731. if (os_strcmp(cmd, "WPS") == 0)
  732. ndef = 0;
  733. else if (os_strcmp(cmd, "NDEF") == 0)
  734. ndef = 1;
  735. else
  736. return -1;
  737. buf = wpas_wps_nfc_token(wpa_s, ndef);
  738. if (buf == NULL)
  739. return -1;
  740. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  741. wpabuf_len(buf));
  742. reply[res++] = '\n';
  743. reply[res] = '\0';
  744. wpabuf_free(buf);
  745. return res;
  746. }
  747. static int wpa_supplicant_ctrl_iface_wps_nfc_tag_read(
  748. struct wpa_supplicant *wpa_s, char *pos)
  749. {
  750. size_t len;
  751. struct wpabuf *buf;
  752. int ret;
  753. len = os_strlen(pos);
  754. if (len & 0x01)
  755. return -1;
  756. len /= 2;
  757. buf = wpabuf_alloc(len);
  758. if (buf == NULL)
  759. return -1;
  760. if (hexstr2bin(pos, wpabuf_put(buf, len), len) < 0) {
  761. wpabuf_free(buf);
  762. return -1;
  763. }
  764. ret = wpas_wps_nfc_tag_read(wpa_s, buf);
  765. wpabuf_free(buf);
  766. return ret;
  767. }
  768. static int wpas_ctrl_nfc_get_handover_req_wps(struct wpa_supplicant *wpa_s,
  769. char *reply, size_t max_len,
  770. int ndef)
  771. {
  772. struct wpabuf *buf;
  773. int res;
  774. buf = wpas_wps_nfc_handover_req(wpa_s, ndef);
  775. if (buf == NULL)
  776. return -1;
  777. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  778. wpabuf_len(buf));
  779. reply[res++] = '\n';
  780. reply[res] = '\0';
  781. wpabuf_free(buf);
  782. return res;
  783. }
  784. static int wpas_ctrl_nfc_get_handover_req(struct wpa_supplicant *wpa_s,
  785. char *cmd, char *reply,
  786. size_t max_len)
  787. {
  788. char *pos;
  789. int ndef;
  790. pos = os_strchr(cmd, ' ');
  791. if (pos == NULL)
  792. return -1;
  793. *pos++ = '\0';
  794. if (os_strcmp(cmd, "WPS") == 0)
  795. ndef = 0;
  796. else if (os_strcmp(cmd, "NDEF") == 0)
  797. ndef = 1;
  798. else
  799. return -1;
  800. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  801. if (!ndef)
  802. return -1;
  803. return wpas_ctrl_nfc_get_handover_req_wps(
  804. wpa_s, reply, max_len, ndef);
  805. }
  806. return -1;
  807. }
  808. static int wpas_ctrl_nfc_get_handover_sel_wps(struct wpa_supplicant *wpa_s,
  809. char *reply, size_t max_len,
  810. int ndef, int cr, char *uuid)
  811. {
  812. struct wpabuf *buf;
  813. int res;
  814. buf = wpas_wps_nfc_handover_sel(wpa_s, ndef, cr, uuid);
  815. if (buf == NULL)
  816. return -1;
  817. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  818. wpabuf_len(buf));
  819. reply[res++] = '\n';
  820. reply[res] = '\0';
  821. wpabuf_free(buf);
  822. return res;
  823. }
  824. static int wpas_ctrl_nfc_get_handover_sel(struct wpa_supplicant *wpa_s,
  825. char *cmd, char *reply,
  826. size_t max_len)
  827. {
  828. char *pos, *pos2;
  829. int ndef;
  830. pos = os_strchr(cmd, ' ');
  831. if (pos == NULL)
  832. return -1;
  833. *pos++ = '\0';
  834. if (os_strcmp(cmd, "WPS") == 0)
  835. ndef = 0;
  836. else if (os_strcmp(cmd, "NDEF") == 0)
  837. ndef = 1;
  838. else
  839. return -1;
  840. pos2 = os_strchr(pos, ' ');
  841. if (pos2)
  842. *pos2++ = '\0';
  843. if (os_strcmp(pos, "WPS") == 0 || os_strcmp(pos, "WPS-CR") == 0) {
  844. return wpas_ctrl_nfc_get_handover_sel_wps(
  845. wpa_s, reply, max_len, ndef,
  846. os_strcmp(pos, "WPS-CR") == 0, pos2);
  847. }
  848. return -1;
  849. }
  850. static int wpas_ctrl_nfc_rx_handover_req(struct wpa_supplicant *wpa_s,
  851. char *cmd, char *reply,
  852. size_t max_len)
  853. {
  854. size_t len;
  855. struct wpabuf *buf;
  856. int ret;
  857. len = os_strlen(cmd);
  858. if (len & 0x01)
  859. return -1;
  860. len /= 2;
  861. buf = wpabuf_alloc(len);
  862. if (buf == NULL)
  863. return -1;
  864. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  865. wpabuf_free(buf);
  866. return -1;
  867. }
  868. ret = wpas_wps_nfc_rx_handover_req(wpa_s, buf);
  869. wpabuf_free(buf);
  870. return ret;
  871. }
  872. static int wpas_ctrl_nfc_rx_handover_sel(struct wpa_supplicant *wpa_s,
  873. char *cmd)
  874. {
  875. size_t len;
  876. struct wpabuf *buf;
  877. int ret;
  878. len = os_strlen(cmd);
  879. if (len & 0x01)
  880. return -1;
  881. len /= 2;
  882. buf = wpabuf_alloc(len);
  883. if (buf == NULL)
  884. return -1;
  885. if (hexstr2bin(cmd, wpabuf_put(buf, len), len) < 0) {
  886. wpabuf_free(buf);
  887. return -1;
  888. }
  889. ret = wpas_wps_nfc_rx_handover_sel(wpa_s, buf);
  890. wpabuf_free(buf);
  891. return ret;
  892. }
  893. static int wpas_ctrl_nfc_report_handover(struct wpa_supplicant *wpa_s,
  894. char *cmd)
  895. {
  896. size_t len;
  897. struct wpabuf *req, *sel;
  898. int ret;
  899. char *pos, *role, *type, *pos2;
  900. role = cmd;
  901. pos = os_strchr(role, ' ');
  902. if (pos == NULL)
  903. return -1;
  904. *pos++ = '\0';
  905. type = pos;
  906. pos = os_strchr(type, ' ');
  907. if (pos == NULL)
  908. return -1;
  909. *pos++ = '\0';
  910. pos2 = os_strchr(pos, ' ');
  911. if (pos2 == NULL)
  912. return -1;
  913. *pos2++ = '\0';
  914. len = os_strlen(pos);
  915. if (len & 0x01)
  916. return -1;
  917. len /= 2;
  918. req = wpabuf_alloc(len);
  919. if (req == NULL)
  920. return -1;
  921. if (hexstr2bin(pos, wpabuf_put(req, len), len) < 0) {
  922. wpabuf_free(req);
  923. return -1;
  924. }
  925. len = os_strlen(pos2);
  926. if (len & 0x01) {
  927. wpabuf_free(req);
  928. return -1;
  929. }
  930. len /= 2;
  931. sel = wpabuf_alloc(len);
  932. if (sel == NULL) {
  933. wpabuf_free(req);
  934. return -1;
  935. }
  936. if (hexstr2bin(pos2, wpabuf_put(sel, len), len) < 0) {
  937. wpabuf_free(req);
  938. wpabuf_free(sel);
  939. return -1;
  940. }
  941. if (os_strcmp(role, "INIT") == 0 && os_strcmp(type, "WPS") == 0) {
  942. ret = wpas_wps_nfc_report_handover(wpa_s, req, sel);
  943. } else {
  944. wpa_printf(MSG_DEBUG, "NFC: Unsupported connection handover "
  945. "reported: role=%s type=%s", role, type);
  946. ret = -1;
  947. }
  948. wpabuf_free(req);
  949. wpabuf_free(sel);
  950. return ret;
  951. }
  952. #endif /* CONFIG_WPS_NFC */
  953. static int wpa_supplicant_ctrl_iface_wps_reg(struct wpa_supplicant *wpa_s,
  954. char *cmd)
  955. {
  956. u8 bssid[ETH_ALEN];
  957. char *pin;
  958. char *new_ssid;
  959. char *new_auth;
  960. char *new_encr;
  961. char *new_key;
  962. struct wps_new_ap_settings ap;
  963. pin = os_strchr(cmd, ' ');
  964. if (pin == NULL)
  965. return -1;
  966. *pin++ = '\0';
  967. if (hwaddr_aton(cmd, bssid)) {
  968. wpa_printf(MSG_DEBUG, "CTRL_IFACE WPS_REG: invalid BSSID '%s'",
  969. cmd);
  970. return -1;
  971. }
  972. new_ssid = os_strchr(pin, ' ');
  973. if (new_ssid == NULL)
  974. return wpas_wps_start_reg(wpa_s, bssid, pin, NULL);
  975. *new_ssid++ = '\0';
  976. new_auth = os_strchr(new_ssid, ' ');
  977. if (new_auth == NULL)
  978. return -1;
  979. *new_auth++ = '\0';
  980. new_encr = os_strchr(new_auth, ' ');
  981. if (new_encr == NULL)
  982. return -1;
  983. *new_encr++ = '\0';
  984. new_key = os_strchr(new_encr, ' ');
  985. if (new_key == NULL)
  986. return -1;
  987. *new_key++ = '\0';
  988. os_memset(&ap, 0, sizeof(ap));
  989. ap.ssid_hex = new_ssid;
  990. ap.auth = new_auth;
  991. ap.encr = new_encr;
  992. ap.key_hex = new_key;
  993. return wpas_wps_start_reg(wpa_s, bssid, pin, &ap);
  994. }
  995. #ifdef CONFIG_AP
  996. static int wpa_supplicant_ctrl_iface_wps_ap_pin(struct wpa_supplicant *wpa_s,
  997. char *cmd, char *buf,
  998. size_t buflen)
  999. {
  1000. int timeout = 300;
  1001. char *pos;
  1002. const char *pin_txt;
  1003. if (!wpa_s->ap_iface)
  1004. return -1;
  1005. pos = os_strchr(cmd, ' ');
  1006. if (pos)
  1007. *pos++ = '\0';
  1008. if (os_strcmp(cmd, "disable") == 0) {
  1009. wpas_wps_ap_pin_disable(wpa_s);
  1010. return os_snprintf(buf, buflen, "OK\n");
  1011. }
  1012. if (os_strcmp(cmd, "random") == 0) {
  1013. if (pos)
  1014. timeout = atoi(pos);
  1015. pin_txt = wpas_wps_ap_pin_random(wpa_s, timeout);
  1016. if (pin_txt == NULL)
  1017. return -1;
  1018. return os_snprintf(buf, buflen, "%s", pin_txt);
  1019. }
  1020. if (os_strcmp(cmd, "get") == 0) {
  1021. pin_txt = wpas_wps_ap_pin_get(wpa_s);
  1022. if (pin_txt == NULL)
  1023. return -1;
  1024. return os_snprintf(buf, buflen, "%s", pin_txt);
  1025. }
  1026. if (os_strcmp(cmd, "set") == 0) {
  1027. char *pin;
  1028. if (pos == NULL)
  1029. return -1;
  1030. pin = pos;
  1031. pos = os_strchr(pos, ' ');
  1032. if (pos) {
  1033. *pos++ = '\0';
  1034. timeout = atoi(pos);
  1035. }
  1036. if (os_strlen(pin) > buflen)
  1037. return -1;
  1038. if (wpas_wps_ap_pin_set(wpa_s, pin, timeout) < 0)
  1039. return -1;
  1040. return os_snprintf(buf, buflen, "%s", pin);
  1041. }
  1042. return -1;
  1043. }
  1044. #endif /* CONFIG_AP */
  1045. #ifdef CONFIG_WPS_ER
  1046. static int wpa_supplicant_ctrl_iface_wps_er_pin(struct wpa_supplicant *wpa_s,
  1047. char *cmd)
  1048. {
  1049. char *uuid = cmd, *pin, *pos;
  1050. u8 addr_buf[ETH_ALEN], *addr = NULL;
  1051. pin = os_strchr(uuid, ' ');
  1052. if (pin == NULL)
  1053. return -1;
  1054. *pin++ = '\0';
  1055. pos = os_strchr(pin, ' ');
  1056. if (pos) {
  1057. *pos++ = '\0';
  1058. if (hwaddr_aton(pos, addr_buf) == 0)
  1059. addr = addr_buf;
  1060. }
  1061. return wpas_wps_er_add_pin(wpa_s, addr, uuid, pin);
  1062. }
  1063. static int wpa_supplicant_ctrl_iface_wps_er_learn(struct wpa_supplicant *wpa_s,
  1064. char *cmd)
  1065. {
  1066. char *uuid = cmd, *pin;
  1067. pin = os_strchr(uuid, ' ');
  1068. if (pin == NULL)
  1069. return -1;
  1070. *pin++ = '\0';
  1071. return wpas_wps_er_learn(wpa_s, uuid, pin);
  1072. }
  1073. static int wpa_supplicant_ctrl_iface_wps_er_set_config(
  1074. struct wpa_supplicant *wpa_s, char *cmd)
  1075. {
  1076. char *uuid = cmd, *id;
  1077. id = os_strchr(uuid, ' ');
  1078. if (id == NULL)
  1079. return -1;
  1080. *id++ = '\0';
  1081. return wpas_wps_er_set_config(wpa_s, uuid, atoi(id));
  1082. }
  1083. static int wpa_supplicant_ctrl_iface_wps_er_config(
  1084. struct wpa_supplicant *wpa_s, char *cmd)
  1085. {
  1086. char *pin;
  1087. char *new_ssid;
  1088. char *new_auth;
  1089. char *new_encr;
  1090. char *new_key;
  1091. struct wps_new_ap_settings ap;
  1092. pin = os_strchr(cmd, ' ');
  1093. if (pin == NULL)
  1094. return -1;
  1095. *pin++ = '\0';
  1096. new_ssid = os_strchr(pin, ' ');
  1097. if (new_ssid == NULL)
  1098. return -1;
  1099. *new_ssid++ = '\0';
  1100. new_auth = os_strchr(new_ssid, ' ');
  1101. if (new_auth == NULL)
  1102. return -1;
  1103. *new_auth++ = '\0';
  1104. new_encr = os_strchr(new_auth, ' ');
  1105. if (new_encr == NULL)
  1106. return -1;
  1107. *new_encr++ = '\0';
  1108. new_key = os_strchr(new_encr, ' ');
  1109. if (new_key == NULL)
  1110. return -1;
  1111. *new_key++ = '\0';
  1112. os_memset(&ap, 0, sizeof(ap));
  1113. ap.ssid_hex = new_ssid;
  1114. ap.auth = new_auth;
  1115. ap.encr = new_encr;
  1116. ap.key_hex = new_key;
  1117. return wpas_wps_er_config(wpa_s, cmd, pin, &ap);
  1118. }
  1119. #ifdef CONFIG_WPS_NFC
  1120. static int wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  1121. struct wpa_supplicant *wpa_s, char *cmd, char *reply, size_t max_len)
  1122. {
  1123. int ndef;
  1124. struct wpabuf *buf;
  1125. int res;
  1126. char *uuid;
  1127. uuid = os_strchr(cmd, ' ');
  1128. if (uuid == NULL)
  1129. return -1;
  1130. *uuid++ = '\0';
  1131. if (os_strcmp(cmd, "WPS") == 0)
  1132. ndef = 0;
  1133. else if (os_strcmp(cmd, "NDEF") == 0)
  1134. ndef = 1;
  1135. else
  1136. return -1;
  1137. buf = wpas_wps_er_nfc_config_token(wpa_s, ndef, uuid);
  1138. if (buf == NULL)
  1139. return -1;
  1140. res = wpa_snprintf_hex_uppercase(reply, max_len, wpabuf_head(buf),
  1141. wpabuf_len(buf));
  1142. reply[res++] = '\n';
  1143. reply[res] = '\0';
  1144. wpabuf_free(buf);
  1145. return res;
  1146. }
  1147. #endif /* CONFIG_WPS_NFC */
  1148. #endif /* CONFIG_WPS_ER */
  1149. #endif /* CONFIG_WPS */
  1150. #ifdef CONFIG_IBSS_RSN
  1151. static int wpa_supplicant_ctrl_iface_ibss_rsn(
  1152. struct wpa_supplicant *wpa_s, char *addr)
  1153. {
  1154. u8 peer[ETH_ALEN];
  1155. if (hwaddr_aton(addr, peer)) {
  1156. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN: invalid "
  1157. "address '%s'", addr);
  1158. return -1;
  1159. }
  1160. wpa_printf(MSG_DEBUG, "CTRL_IFACE IBSS_RSN " MACSTR,
  1161. MAC2STR(peer));
  1162. return ibss_rsn_start(wpa_s->ibss_rsn, peer);
  1163. }
  1164. #endif /* CONFIG_IBSS_RSN */
  1165. static int wpa_supplicant_ctrl_iface_ctrl_rsp(struct wpa_supplicant *wpa_s,
  1166. char *rsp)
  1167. {
  1168. #ifdef IEEE8021X_EAPOL
  1169. char *pos, *id_pos;
  1170. int id;
  1171. struct wpa_ssid *ssid;
  1172. pos = os_strchr(rsp, '-');
  1173. if (pos == NULL)
  1174. return -1;
  1175. *pos++ = '\0';
  1176. id_pos = pos;
  1177. pos = os_strchr(pos, ':');
  1178. if (pos == NULL)
  1179. return -1;
  1180. *pos++ = '\0';
  1181. id = atoi(id_pos);
  1182. wpa_printf(MSG_DEBUG, "CTRL_IFACE: field=%s id=%d", rsp, id);
  1183. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  1184. (u8 *) pos, os_strlen(pos));
  1185. ssid = wpa_config_get_network(wpa_s->conf, id);
  1186. if (ssid == NULL) {
  1187. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1188. "to update", id);
  1189. return -1;
  1190. }
  1191. return wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid, rsp,
  1192. pos);
  1193. #else /* IEEE8021X_EAPOL */
  1194. wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
  1195. return -1;
  1196. #endif /* IEEE8021X_EAPOL */
  1197. }
  1198. static int wpa_supplicant_ctrl_iface_status(struct wpa_supplicant *wpa_s,
  1199. const char *params,
  1200. char *buf, size_t buflen)
  1201. {
  1202. char *pos, *end, tmp[30];
  1203. int res, verbose, wps, ret;
  1204. if (os_strcmp(params, "-DRIVER") == 0)
  1205. return wpa_drv_status(wpa_s, buf, buflen);
  1206. verbose = os_strcmp(params, "-VERBOSE") == 0;
  1207. wps = os_strcmp(params, "-WPS") == 0;
  1208. pos = buf;
  1209. end = buf + buflen;
  1210. if (wpa_s->wpa_state >= WPA_ASSOCIATED) {
  1211. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1212. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  1213. MAC2STR(wpa_s->bssid));
  1214. if (ret < 0 || ret >= end - pos)
  1215. return pos - buf;
  1216. pos += ret;
  1217. if (ssid) {
  1218. u8 *_ssid = ssid->ssid;
  1219. size_t ssid_len = ssid->ssid_len;
  1220. u8 ssid_buf[MAX_SSID_LEN];
  1221. if (ssid_len == 0) {
  1222. int _res = wpa_drv_get_ssid(wpa_s, ssid_buf);
  1223. if (_res < 0)
  1224. ssid_len = 0;
  1225. else
  1226. ssid_len = _res;
  1227. _ssid = ssid_buf;
  1228. }
  1229. ret = os_snprintf(pos, end - pos, "ssid=%s\nid=%d\n",
  1230. wpa_ssid_txt(_ssid, ssid_len),
  1231. ssid->id);
  1232. if (ret < 0 || ret >= end - pos)
  1233. return pos - buf;
  1234. pos += ret;
  1235. if (wps && ssid->passphrase &&
  1236. wpa_key_mgmt_wpa_psk(ssid->key_mgmt) &&
  1237. (ssid->mode == WPAS_MODE_AP ||
  1238. ssid->mode == WPAS_MODE_P2P_GO)) {
  1239. ret = os_snprintf(pos, end - pos,
  1240. "passphrase=%s\n",
  1241. ssid->passphrase);
  1242. if (ret < 0 || ret >= end - pos)
  1243. return pos - buf;
  1244. pos += ret;
  1245. }
  1246. if (ssid->id_str) {
  1247. ret = os_snprintf(pos, end - pos,
  1248. "id_str=%s\n",
  1249. ssid->id_str);
  1250. if (ret < 0 || ret >= end - pos)
  1251. return pos - buf;
  1252. pos += ret;
  1253. }
  1254. switch (ssid->mode) {
  1255. case WPAS_MODE_INFRA:
  1256. ret = os_snprintf(pos, end - pos,
  1257. "mode=station\n");
  1258. break;
  1259. case WPAS_MODE_IBSS:
  1260. ret = os_snprintf(pos, end - pos,
  1261. "mode=IBSS\n");
  1262. break;
  1263. case WPAS_MODE_AP:
  1264. ret = os_snprintf(pos, end - pos,
  1265. "mode=AP\n");
  1266. break;
  1267. case WPAS_MODE_P2P_GO:
  1268. ret = os_snprintf(pos, end - pos,
  1269. "mode=P2P GO\n");
  1270. break;
  1271. case WPAS_MODE_P2P_GROUP_FORMATION:
  1272. ret = os_snprintf(pos, end - pos,
  1273. "mode=P2P GO - group "
  1274. "formation\n");
  1275. break;
  1276. default:
  1277. ret = 0;
  1278. break;
  1279. }
  1280. if (ret < 0 || ret >= end - pos)
  1281. return pos - buf;
  1282. pos += ret;
  1283. }
  1284. #ifdef CONFIG_AP
  1285. if (wpa_s->ap_iface) {
  1286. pos += ap_ctrl_iface_wpa_get_status(wpa_s, pos,
  1287. end - pos,
  1288. verbose);
  1289. } else
  1290. #endif /* CONFIG_AP */
  1291. pos += wpa_sm_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1292. }
  1293. #ifdef CONFIG_SAE
  1294. if (wpa_s->wpa_state >= WPA_ASSOCIATED &&
  1295. #ifdef CONFIG_AP
  1296. !wpa_s->ap_iface &&
  1297. #endif /* CONFIG_AP */
  1298. wpa_s->sme.sae.state == SAE_ACCEPTED) {
  1299. ret = os_snprintf(pos, end - pos, "sae_group=%d\n",
  1300. wpa_s->sme.sae.group);
  1301. if (ret < 0 || ret >= end - pos)
  1302. return pos - buf;
  1303. pos += ret;
  1304. }
  1305. #endif /* CONFIG_SAE */
  1306. ret = os_snprintf(pos, end - pos, "wpa_state=%s\n",
  1307. wpa_supplicant_state_txt(wpa_s->wpa_state));
  1308. if (ret < 0 || ret >= end - pos)
  1309. return pos - buf;
  1310. pos += ret;
  1311. if (wpa_s->l2 &&
  1312. l2_packet_get_ip_addr(wpa_s->l2, tmp, sizeof(tmp)) >= 0) {
  1313. ret = os_snprintf(pos, end - pos, "ip_address=%s\n", tmp);
  1314. if (ret < 0 || ret >= end - pos)
  1315. return pos - buf;
  1316. pos += ret;
  1317. }
  1318. #ifdef CONFIG_P2P
  1319. if (wpa_s->global->p2p) {
  1320. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  1321. "\n", MAC2STR(wpa_s->global->p2p_dev_addr));
  1322. if (ret < 0 || ret >= end - pos)
  1323. return pos - buf;
  1324. pos += ret;
  1325. }
  1326. #endif /* CONFIG_P2P */
  1327. ret = os_snprintf(pos, end - pos, "address=" MACSTR "\n",
  1328. MAC2STR(wpa_s->own_addr));
  1329. if (ret < 0 || ret >= end - pos)
  1330. return pos - buf;
  1331. pos += ret;
  1332. #ifdef CONFIG_HS20
  1333. if (wpa_s->current_bss &&
  1334. wpa_bss_get_vendor_ie(wpa_s->current_bss, HS20_IE_VENDOR_TYPE) &&
  1335. wpa_s->wpa_proto == WPA_PROTO_RSN &&
  1336. wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt)) {
  1337. ret = os_snprintf(pos, end - pos, "hs20=1\n");
  1338. if (ret < 0 || ret >= end - pos)
  1339. return pos - buf;
  1340. pos += ret;
  1341. }
  1342. if (wpa_s->current_ssid) {
  1343. struct wpa_cred *cred;
  1344. char *type;
  1345. for (cred = wpa_s->conf->cred; cred; cred = cred->next) {
  1346. size_t i;
  1347. if (wpa_s->current_ssid->parent_cred != cred)
  1348. continue;
  1349. for (i = 0; cred->domain && i < cred->num_domain; i++) {
  1350. ret = os_snprintf(pos, end - pos,
  1351. "home_sp=%s\n",
  1352. cred->domain[i]);
  1353. if (ret < 0 || ret >= end - pos)
  1354. return pos - buf;
  1355. pos += ret;
  1356. }
  1357. if (wpa_s->current_bss == NULL ||
  1358. wpa_s->current_bss->anqp == NULL)
  1359. res = -1;
  1360. else
  1361. res = interworking_home_sp_cred(
  1362. wpa_s, cred,
  1363. wpa_s->current_bss->anqp->domain_name);
  1364. if (res > 0)
  1365. type = "home";
  1366. else if (res == 0)
  1367. type = "roaming";
  1368. else
  1369. type = "unknown";
  1370. ret = os_snprintf(pos, end - pos, "sp_type=%s\n", type);
  1371. if (ret < 0 || ret >= end - pos)
  1372. return pos - buf;
  1373. pos += ret;
  1374. break;
  1375. }
  1376. }
  1377. #endif /* CONFIG_HS20 */
  1378. if (wpa_key_mgmt_wpa_ieee8021x(wpa_s->key_mgmt) ||
  1379. wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
  1380. res = eapol_sm_get_status(wpa_s->eapol, pos, end - pos,
  1381. verbose);
  1382. if (res >= 0)
  1383. pos += res;
  1384. }
  1385. res = rsn_preauth_get_status(wpa_s->wpa, pos, end - pos, verbose);
  1386. if (res >= 0)
  1387. pos += res;
  1388. #ifdef CONFIG_WPS
  1389. {
  1390. char uuid_str[100];
  1391. uuid_bin2str(wpa_s->wps->uuid, uuid_str, sizeof(uuid_str));
  1392. ret = os_snprintf(pos, end - pos, "uuid=%s\n", uuid_str);
  1393. if (ret < 0 || ret >= end - pos)
  1394. return pos - buf;
  1395. pos += ret;
  1396. }
  1397. #endif /* CONFIG_WPS */
  1398. #ifdef ANDROID
  1399. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_STATE_CHANGE
  1400. "id=%d state=%d BSSID=" MACSTR " SSID=%s",
  1401. wpa_s->current_ssid ? wpa_s->current_ssid->id : -1,
  1402. wpa_s->wpa_state,
  1403. MAC2STR(wpa_s->bssid),
  1404. wpa_s->current_ssid && wpa_s->current_ssid->ssid ?
  1405. wpa_ssid_txt(wpa_s->current_ssid->ssid,
  1406. wpa_s->current_ssid->ssid_len) : "");
  1407. if (wpa_s->wpa_state == WPA_COMPLETED) {
  1408. struct wpa_ssid *ssid = wpa_s->current_ssid;
  1409. wpa_msg_ctrl(wpa_s, MSG_INFO, WPA_EVENT_CONNECTED
  1410. "- connection to " MACSTR
  1411. " completed %s [id=%d id_str=%s]",
  1412. MAC2STR(wpa_s->bssid), "(auth)",
  1413. ssid ? ssid->id : -1,
  1414. ssid && ssid->id_str ? ssid->id_str : "");
  1415. }
  1416. #endif /* ANDROID */
  1417. return pos - buf;
  1418. }
  1419. static int wpa_supplicant_ctrl_iface_bssid(struct wpa_supplicant *wpa_s,
  1420. char *cmd)
  1421. {
  1422. char *pos;
  1423. int id;
  1424. struct wpa_ssid *ssid;
  1425. u8 bssid[ETH_ALEN];
  1426. /* cmd: "<network id> <BSSID>" */
  1427. pos = os_strchr(cmd, ' ');
  1428. if (pos == NULL)
  1429. return -1;
  1430. *pos++ = '\0';
  1431. id = atoi(cmd);
  1432. wpa_printf(MSG_DEBUG, "CTRL_IFACE: id=%d bssid='%s'", id, pos);
  1433. if (hwaddr_aton(pos, bssid)) {
  1434. wpa_printf(MSG_DEBUG ,"CTRL_IFACE: invalid BSSID '%s'", pos);
  1435. return -1;
  1436. }
  1437. ssid = wpa_config_get_network(wpa_s->conf, id);
  1438. if (ssid == NULL) {
  1439. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  1440. "to update", id);
  1441. return -1;
  1442. }
  1443. os_memcpy(ssid->bssid, bssid, ETH_ALEN);
  1444. ssid->bssid_set = !is_zero_ether_addr(bssid);
  1445. return 0;
  1446. }
  1447. static int wpa_supplicant_ctrl_iface_blacklist(struct wpa_supplicant *wpa_s,
  1448. char *cmd, char *buf,
  1449. size_t buflen)
  1450. {
  1451. u8 bssid[ETH_ALEN];
  1452. struct wpa_blacklist *e;
  1453. char *pos, *end;
  1454. int ret;
  1455. /* cmd: "BLACKLIST [<BSSID>]" */
  1456. if (*cmd == '\0') {
  1457. pos = buf;
  1458. end = buf + buflen;
  1459. e = wpa_s->blacklist;
  1460. while (e) {
  1461. ret = os_snprintf(pos, end - pos, MACSTR "\n",
  1462. MAC2STR(e->bssid));
  1463. if (ret < 0 || ret >= end - pos)
  1464. return pos - buf;
  1465. pos += ret;
  1466. e = e->next;
  1467. }
  1468. return pos - buf;
  1469. }
  1470. cmd++;
  1471. if (os_strncmp(cmd, "clear", 5) == 0) {
  1472. wpa_blacklist_clear(wpa_s);
  1473. os_memcpy(buf, "OK\n", 3);
  1474. return 3;
  1475. }
  1476. wpa_printf(MSG_DEBUG, "CTRL_IFACE: BLACKLIST bssid='%s'", cmd);
  1477. if (hwaddr_aton(cmd, bssid)) {
  1478. wpa_printf(MSG_DEBUG, "CTRL_IFACE: invalid BSSID '%s'", cmd);
  1479. return -1;
  1480. }
  1481. /*
  1482. * Add the BSSID twice, so its count will be 2, causing it to be
  1483. * skipped when processing scan results.
  1484. */
  1485. ret = wpa_blacklist_add(wpa_s, bssid);
  1486. if (ret != 0)
  1487. return -1;
  1488. ret = wpa_blacklist_add(wpa_s, bssid);
  1489. if (ret != 0)
  1490. return -1;
  1491. os_memcpy(buf, "OK\n", 3);
  1492. return 3;
  1493. }
  1494. static const char * debug_level_str(int level)
  1495. {
  1496. switch (level) {
  1497. case MSG_EXCESSIVE:
  1498. return "EXCESSIVE";
  1499. case MSG_MSGDUMP:
  1500. return "MSGDUMP";
  1501. case MSG_DEBUG:
  1502. return "DEBUG";
  1503. case MSG_INFO:
  1504. return "INFO";
  1505. case MSG_WARNING:
  1506. return "WARNING";
  1507. case MSG_ERROR:
  1508. return "ERROR";
  1509. default:
  1510. return "?";
  1511. }
  1512. }
  1513. static int str_to_debug_level(const char *s)
  1514. {
  1515. if (os_strcasecmp(s, "EXCESSIVE") == 0)
  1516. return MSG_EXCESSIVE;
  1517. if (os_strcasecmp(s, "MSGDUMP") == 0)
  1518. return MSG_MSGDUMP;
  1519. if (os_strcasecmp(s, "DEBUG") == 0)
  1520. return MSG_DEBUG;
  1521. if (os_strcasecmp(s, "INFO") == 0)
  1522. return MSG_INFO;
  1523. if (os_strcasecmp(s, "WARNING") == 0)
  1524. return MSG_WARNING;
  1525. if (os_strcasecmp(s, "ERROR") == 0)
  1526. return MSG_ERROR;
  1527. return -1;
  1528. }
  1529. static int wpa_supplicant_ctrl_iface_log_level(struct wpa_supplicant *wpa_s,
  1530. char *cmd, char *buf,
  1531. size_t buflen)
  1532. {
  1533. char *pos, *end, *stamp;
  1534. int ret;
  1535. if (cmd == NULL) {
  1536. return -1;
  1537. }
  1538. /* cmd: "LOG_LEVEL [<level>]" */
  1539. if (*cmd == '\0') {
  1540. pos = buf;
  1541. end = buf + buflen;
  1542. ret = os_snprintf(pos, end - pos, "Current level: %s\n"
  1543. "Timestamp: %d\n",
  1544. debug_level_str(wpa_debug_level),
  1545. wpa_debug_timestamp);
  1546. if (ret < 0 || ret >= end - pos)
  1547. ret = 0;
  1548. return ret;
  1549. }
  1550. while (*cmd == ' ')
  1551. cmd++;
  1552. stamp = os_strchr(cmd, ' ');
  1553. if (stamp) {
  1554. *stamp++ = '\0';
  1555. while (*stamp == ' ') {
  1556. stamp++;
  1557. }
  1558. }
  1559. if (cmd && os_strlen(cmd)) {
  1560. int level = str_to_debug_level(cmd);
  1561. if (level < 0)
  1562. return -1;
  1563. wpa_debug_level = level;
  1564. }
  1565. if (stamp && os_strlen(stamp))
  1566. wpa_debug_timestamp = atoi(stamp);
  1567. os_memcpy(buf, "OK\n", 3);
  1568. return 3;
  1569. }
  1570. static int wpa_supplicant_ctrl_iface_list_networks(
  1571. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1572. {
  1573. char *pos, *end;
  1574. struct wpa_ssid *ssid;
  1575. int ret;
  1576. pos = buf;
  1577. end = buf + buflen;
  1578. ret = os_snprintf(pos, end - pos,
  1579. "network id / ssid / bssid / flags\n");
  1580. if (ret < 0 || ret >= end - pos)
  1581. return pos - buf;
  1582. pos += ret;
  1583. ssid = wpa_s->conf->ssid;
  1584. while (ssid) {
  1585. ret = os_snprintf(pos, end - pos, "%d\t%s",
  1586. ssid->id,
  1587. wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
  1588. if (ret < 0 || ret >= end - pos)
  1589. return pos - buf;
  1590. pos += ret;
  1591. if (ssid->bssid_set) {
  1592. ret = os_snprintf(pos, end - pos, "\t" MACSTR,
  1593. MAC2STR(ssid->bssid));
  1594. } else {
  1595. ret = os_snprintf(pos, end - pos, "\tany");
  1596. }
  1597. if (ret < 0 || ret >= end - pos)
  1598. return pos - buf;
  1599. pos += ret;
  1600. ret = os_snprintf(pos, end - pos, "\t%s%s%s%s",
  1601. ssid == wpa_s->current_ssid ?
  1602. "[CURRENT]" : "",
  1603. ssid->disabled ? "[DISABLED]" : "",
  1604. ssid->disabled_until.sec ?
  1605. "[TEMP-DISABLED]" : "",
  1606. ssid->disabled == 2 ? "[P2P-PERSISTENT]" :
  1607. "");
  1608. if (ret < 0 || ret >= end - pos)
  1609. return pos - buf;
  1610. pos += ret;
  1611. ret = os_snprintf(pos, end - pos, "\n");
  1612. if (ret < 0 || ret >= end - pos)
  1613. return pos - buf;
  1614. pos += ret;
  1615. ssid = ssid->next;
  1616. }
  1617. return pos - buf;
  1618. }
  1619. static char * wpa_supplicant_cipher_txt(char *pos, char *end, int cipher)
  1620. {
  1621. int ret;
  1622. ret = os_snprintf(pos, end - pos, "-");
  1623. if (ret < 0 || ret >= end - pos)
  1624. return pos;
  1625. pos += ret;
  1626. ret = wpa_write_ciphers(pos, end, cipher, "+");
  1627. if (ret < 0)
  1628. return pos;
  1629. pos += ret;
  1630. return pos;
  1631. }
  1632. static char * wpa_supplicant_ie_txt(char *pos, char *end, const char *proto,
  1633. const u8 *ie, size_t ie_len)
  1634. {
  1635. struct wpa_ie_data data;
  1636. int first, ret;
  1637. ret = os_snprintf(pos, end - pos, "[%s-", proto);
  1638. if (ret < 0 || ret >= end - pos)
  1639. return pos;
  1640. pos += ret;
  1641. if (wpa_parse_wpa_ie(ie, ie_len, &data) < 0) {
  1642. ret = os_snprintf(pos, end - pos, "?]");
  1643. if (ret < 0 || ret >= end - pos)
  1644. return pos;
  1645. pos += ret;
  1646. return pos;
  1647. }
  1648. first = 1;
  1649. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X) {
  1650. ret = os_snprintf(pos, end - pos, "%sEAP", first ? "" : "+");
  1651. if (ret < 0 || ret >= end - pos)
  1652. return pos;
  1653. pos += ret;
  1654. first = 0;
  1655. }
  1656. if (data.key_mgmt & WPA_KEY_MGMT_PSK) {
  1657. ret = os_snprintf(pos, end - pos, "%sPSK", first ? "" : "+");
  1658. if (ret < 0 || ret >= end - pos)
  1659. return pos;
  1660. pos += ret;
  1661. first = 0;
  1662. }
  1663. if (data.key_mgmt & WPA_KEY_MGMT_WPA_NONE) {
  1664. ret = os_snprintf(pos, end - pos, "%sNone", first ? "" : "+");
  1665. if (ret < 0 || ret >= end - pos)
  1666. return pos;
  1667. pos += ret;
  1668. first = 0;
  1669. }
  1670. #ifdef CONFIG_IEEE80211R
  1671. if (data.key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X) {
  1672. ret = os_snprintf(pos, end - pos, "%sFT/EAP",
  1673. first ? "" : "+");
  1674. if (ret < 0 || ret >= end - pos)
  1675. return pos;
  1676. pos += ret;
  1677. first = 0;
  1678. }
  1679. if (data.key_mgmt & WPA_KEY_MGMT_FT_PSK) {
  1680. ret = os_snprintf(pos, end - pos, "%sFT/PSK",
  1681. first ? "" : "+");
  1682. if (ret < 0 || ret >= end - pos)
  1683. return pos;
  1684. pos += ret;
  1685. first = 0;
  1686. }
  1687. #endif /* CONFIG_IEEE80211R */
  1688. #ifdef CONFIG_IEEE80211W
  1689. if (data.key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256) {
  1690. ret = os_snprintf(pos, end - pos, "%sEAP-SHA256",
  1691. first ? "" : "+");
  1692. if (ret < 0 || ret >= end - pos)
  1693. return pos;
  1694. pos += ret;
  1695. first = 0;
  1696. }
  1697. if (data.key_mgmt & WPA_KEY_MGMT_PSK_SHA256) {
  1698. ret = os_snprintf(pos, end - pos, "%sPSK-SHA256",
  1699. first ? "" : "+");
  1700. if (ret < 0 || ret >= end - pos)
  1701. return pos;
  1702. pos += ret;
  1703. first = 0;
  1704. }
  1705. #endif /* CONFIG_IEEE80211W */
  1706. pos = wpa_supplicant_cipher_txt(pos, end, data.pairwise_cipher);
  1707. if (data.capabilities & WPA_CAPABILITY_PREAUTH) {
  1708. ret = os_snprintf(pos, end - pos, "-preauth");
  1709. if (ret < 0 || ret >= end - pos)
  1710. return pos;
  1711. pos += ret;
  1712. }
  1713. ret = os_snprintf(pos, end - pos, "]");
  1714. if (ret < 0 || ret >= end - pos)
  1715. return pos;
  1716. pos += ret;
  1717. return pos;
  1718. }
  1719. #ifdef CONFIG_WPS
  1720. static char * wpa_supplicant_wps_ie_txt_buf(struct wpa_supplicant *wpa_s,
  1721. char *pos, char *end,
  1722. struct wpabuf *wps_ie)
  1723. {
  1724. int ret;
  1725. const char *txt;
  1726. if (wps_ie == NULL)
  1727. return pos;
  1728. if (wps_is_selected_pbc_registrar(wps_ie))
  1729. txt = "[WPS-PBC]";
  1730. #ifdef CONFIG_WPS2
  1731. else if (wps_is_addr_authorized(wps_ie, wpa_s->own_addr, 0))
  1732. txt = "[WPS-AUTH]";
  1733. #endif /* CONFIG_WPS2 */
  1734. else if (wps_is_selected_pin_registrar(wps_ie))
  1735. txt = "[WPS-PIN]";
  1736. else
  1737. txt = "[WPS]";
  1738. ret = os_snprintf(pos, end - pos, "%s", txt);
  1739. if (ret >= 0 && ret < end - pos)
  1740. pos += ret;
  1741. wpabuf_free(wps_ie);
  1742. return pos;
  1743. }
  1744. #endif /* CONFIG_WPS */
  1745. static char * wpa_supplicant_wps_ie_txt(struct wpa_supplicant *wpa_s,
  1746. char *pos, char *end,
  1747. const struct wpa_bss *bss)
  1748. {
  1749. #ifdef CONFIG_WPS
  1750. struct wpabuf *wps_ie;
  1751. wps_ie = wpa_bss_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
  1752. return wpa_supplicant_wps_ie_txt_buf(wpa_s, pos, end, wps_ie);
  1753. #else /* CONFIG_WPS */
  1754. return pos;
  1755. #endif /* CONFIG_WPS */
  1756. }
  1757. /* Format one result on one text line into a buffer. */
  1758. static int wpa_supplicant_ctrl_iface_scan_result(
  1759. struct wpa_supplicant *wpa_s,
  1760. const struct wpa_bss *bss, char *buf, size_t buflen)
  1761. {
  1762. char *pos, *end;
  1763. int ret;
  1764. const u8 *ie, *ie2, *p2p;
  1765. p2p = wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE);
  1766. if (!p2p)
  1767. p2p = wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE);
  1768. if (p2p && bss->ssid_len == P2P_WILDCARD_SSID_LEN &&
  1769. os_memcmp(bss->ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) ==
  1770. 0)
  1771. return 0; /* Do not show P2P listen discovery results here */
  1772. pos = buf;
  1773. end = buf + buflen;
  1774. ret = os_snprintf(pos, end - pos, MACSTR "\t%d\t%d\t",
  1775. MAC2STR(bss->bssid), bss->freq, bss->level);
  1776. if (ret < 0 || ret >= end - pos)
  1777. return -1;
  1778. pos += ret;
  1779. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  1780. if (ie)
  1781. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie, 2 + ie[1]);
  1782. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  1783. if (ie2)
  1784. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2, 2 + ie2[1]);
  1785. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  1786. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  1787. ret = os_snprintf(pos, end - pos, "[WEP]");
  1788. if (ret < 0 || ret >= end - pos)
  1789. return -1;
  1790. pos += ret;
  1791. }
  1792. if (bss->caps & IEEE80211_CAP_IBSS) {
  1793. ret = os_snprintf(pos, end - pos, "[IBSS]");
  1794. if (ret < 0 || ret >= end - pos)
  1795. return -1;
  1796. pos += ret;
  1797. }
  1798. if (bss->caps & IEEE80211_CAP_ESS) {
  1799. ret = os_snprintf(pos, end - pos, "[ESS]");
  1800. if (ret < 0 || ret >= end - pos)
  1801. return -1;
  1802. pos += ret;
  1803. }
  1804. if (p2p) {
  1805. ret = os_snprintf(pos, end - pos, "[P2P]");
  1806. if (ret < 0 || ret >= end - pos)
  1807. return -1;
  1808. pos += ret;
  1809. }
  1810. #ifdef CONFIG_HS20
  1811. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE) && ie2) {
  1812. ret = os_snprintf(pos, end - pos, "[HS20]");
  1813. if (ret < 0 || ret >= end - pos)
  1814. return -1;
  1815. pos += ret;
  1816. }
  1817. #endif /* CONFIG_HS20 */
  1818. ret = os_snprintf(pos, end - pos, "\t%s",
  1819. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  1820. if (ret < 0 || ret >= end - pos)
  1821. return -1;
  1822. pos += ret;
  1823. ret = os_snprintf(pos, end - pos, "\n");
  1824. if (ret < 0 || ret >= end - pos)
  1825. return -1;
  1826. pos += ret;
  1827. return pos - buf;
  1828. }
  1829. static int wpa_supplicant_ctrl_iface_scan_results(
  1830. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1831. {
  1832. char *pos, *end;
  1833. struct wpa_bss *bss;
  1834. int ret;
  1835. pos = buf;
  1836. end = buf + buflen;
  1837. ret = os_snprintf(pos, end - pos, "bssid / frequency / signal level / "
  1838. "flags / ssid\n");
  1839. if (ret < 0 || ret >= end - pos)
  1840. return pos - buf;
  1841. pos += ret;
  1842. dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
  1843. ret = wpa_supplicant_ctrl_iface_scan_result(wpa_s, bss, pos,
  1844. end - pos);
  1845. if (ret < 0 || ret >= end - pos)
  1846. return pos - buf;
  1847. pos += ret;
  1848. }
  1849. return pos - buf;
  1850. }
  1851. static int wpa_supplicant_ctrl_iface_select_network(
  1852. struct wpa_supplicant *wpa_s, char *cmd)
  1853. {
  1854. int id;
  1855. struct wpa_ssid *ssid;
  1856. /* cmd: "<network id>" or "any" */
  1857. if (os_strcmp(cmd, "any") == 0) {
  1858. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK any");
  1859. ssid = NULL;
  1860. } else {
  1861. id = atoi(cmd);
  1862. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SELECT_NETWORK id=%d", id);
  1863. ssid = wpa_config_get_network(wpa_s->conf, id);
  1864. if (ssid == NULL) {
  1865. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1866. "network id=%d", id);
  1867. return -1;
  1868. }
  1869. if (ssid->disabled == 2) {
  1870. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1871. "SELECT_NETWORK with persistent P2P group");
  1872. return -1;
  1873. }
  1874. }
  1875. wpa_supplicant_select_network(wpa_s, ssid);
  1876. return 0;
  1877. }
  1878. static int wpa_supplicant_ctrl_iface_enable_network(
  1879. struct wpa_supplicant *wpa_s, char *cmd)
  1880. {
  1881. int id;
  1882. struct wpa_ssid *ssid;
  1883. /* cmd: "<network id>" or "all" */
  1884. if (os_strcmp(cmd, "all") == 0) {
  1885. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK all");
  1886. ssid = NULL;
  1887. } else {
  1888. id = atoi(cmd);
  1889. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ENABLE_NETWORK id=%d", id);
  1890. ssid = wpa_config_get_network(wpa_s->conf, id);
  1891. if (ssid == NULL) {
  1892. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1893. "network id=%d", id);
  1894. return -1;
  1895. }
  1896. if (ssid->disabled == 2) {
  1897. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1898. "ENABLE_NETWORK with persistent P2P group");
  1899. return -1;
  1900. }
  1901. if (os_strstr(cmd, " no-connect")) {
  1902. ssid->disabled = 0;
  1903. return 0;
  1904. }
  1905. }
  1906. wpa_supplicant_enable_network(wpa_s, ssid);
  1907. return 0;
  1908. }
  1909. static int wpa_supplicant_ctrl_iface_disable_network(
  1910. struct wpa_supplicant *wpa_s, char *cmd)
  1911. {
  1912. int id;
  1913. struct wpa_ssid *ssid;
  1914. /* cmd: "<network id>" or "all" */
  1915. if (os_strcmp(cmd, "all") == 0) {
  1916. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK all");
  1917. ssid = NULL;
  1918. } else {
  1919. id = atoi(cmd);
  1920. wpa_printf(MSG_DEBUG, "CTRL_IFACE: DISABLE_NETWORK id=%d", id);
  1921. ssid = wpa_config_get_network(wpa_s->conf, id);
  1922. if (ssid == NULL) {
  1923. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  1924. "network id=%d", id);
  1925. return -1;
  1926. }
  1927. if (ssid->disabled == 2) {
  1928. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Cannot use "
  1929. "DISABLE_NETWORK with persistent P2P "
  1930. "group");
  1931. return -1;
  1932. }
  1933. }
  1934. wpa_supplicant_disable_network(wpa_s, ssid);
  1935. return 0;
  1936. }
  1937. static int wpa_supplicant_ctrl_iface_add_network(
  1938. struct wpa_supplicant *wpa_s, char *buf, size_t buflen)
  1939. {
  1940. struct wpa_ssid *ssid;
  1941. int ret;
  1942. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_NETWORK");
  1943. ssid = wpa_config_add_network(wpa_s->conf);
  1944. if (ssid == NULL)
  1945. return -1;
  1946. wpas_notify_network_added(wpa_s, ssid);
  1947. ssid->disabled = 1;
  1948. wpa_config_set_network_defaults(ssid);
  1949. ret = os_snprintf(buf, buflen, "%d\n", ssid->id);
  1950. if (ret < 0 || (size_t) ret >= buflen)
  1951. return -1;
  1952. return ret;
  1953. }
  1954. static int wpa_supplicant_ctrl_iface_remove_network(
  1955. struct wpa_supplicant *wpa_s, char *cmd)
  1956. {
  1957. int id;
  1958. struct wpa_ssid *ssid;
  1959. int was_disabled;
  1960. /* cmd: "<network id>" or "all" */
  1961. if (os_strcmp(cmd, "all") == 0) {
  1962. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK all");
  1963. if (wpa_s->sched_scanning)
  1964. wpa_supplicant_cancel_sched_scan(wpa_s);
  1965. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  1966. if (wpa_s->current_ssid) {
  1967. #ifdef CONFIG_SME
  1968. wpa_s->sme.prev_bssid_set = 0;
  1969. #endif /* CONFIG_SME */
  1970. wpa_sm_set_config(wpa_s->wpa, NULL);
  1971. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  1972. wpa_supplicant_deauthenticate(
  1973. wpa_s, WLAN_REASON_DEAUTH_LEAVING);
  1974. }
  1975. ssid = wpa_s->conf->ssid;
  1976. while (ssid) {
  1977. struct wpa_ssid *remove_ssid = ssid;
  1978. id = ssid->id;
  1979. ssid = ssid->next;
  1980. wpas_notify_network_removed(wpa_s, remove_ssid);
  1981. wpa_config_remove_network(wpa_s->conf, id);
  1982. }
  1983. return 0;
  1984. }
  1985. id = atoi(cmd);
  1986. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_NETWORK id=%d", id);
  1987. ssid = wpa_config_get_network(wpa_s->conf, id);
  1988. if (ssid)
  1989. wpas_notify_network_removed(wpa_s, ssid);
  1990. if (ssid == NULL) {
  1991. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  1992. "id=%d", id);
  1993. return -1;
  1994. }
  1995. if (ssid == wpa_s->current_ssid || wpa_s->current_ssid == NULL) {
  1996. #ifdef CONFIG_SME
  1997. wpa_s->sme.prev_bssid_set = 0;
  1998. #endif /* CONFIG_SME */
  1999. /*
  2000. * Invalidate the EAP session cache if the current or
  2001. * previously used network is removed.
  2002. */
  2003. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2004. }
  2005. if (ssid == wpa_s->current_ssid) {
  2006. wpa_sm_set_config(wpa_s->wpa, NULL);
  2007. eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
  2008. wpa_supplicant_deauthenticate(wpa_s,
  2009. WLAN_REASON_DEAUTH_LEAVING);
  2010. }
  2011. was_disabled = ssid->disabled;
  2012. if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
  2013. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Not able to remove the "
  2014. "network id=%d", id);
  2015. return -1;
  2016. }
  2017. if (!was_disabled && wpa_s->sched_scanning) {
  2018. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
  2019. "network from filters");
  2020. wpa_supplicant_cancel_sched_scan(wpa_s);
  2021. wpa_supplicant_req_scan(wpa_s, 0, 0);
  2022. }
  2023. return 0;
  2024. }
  2025. static int wpa_supplicant_ctrl_iface_set_network(
  2026. struct wpa_supplicant *wpa_s, char *cmd)
  2027. {
  2028. int id;
  2029. struct wpa_ssid *ssid;
  2030. char *name, *value;
  2031. /* cmd: "<network id> <variable name> <value>" */
  2032. name = os_strchr(cmd, ' ');
  2033. if (name == NULL)
  2034. return -1;
  2035. *name++ = '\0';
  2036. value = os_strchr(name, ' ');
  2037. if (value == NULL)
  2038. return -1;
  2039. *value++ = '\0';
  2040. id = atoi(cmd);
  2041. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_NETWORK id=%d name='%s'",
  2042. id, name);
  2043. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2044. (u8 *) value, os_strlen(value));
  2045. ssid = wpa_config_get_network(wpa_s->conf, id);
  2046. if (ssid == NULL) {
  2047. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2048. "id=%d", id);
  2049. return -1;
  2050. }
  2051. if (wpa_config_set(ssid, name, value, 0) < 0) {
  2052. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set network "
  2053. "variable '%s'", name);
  2054. return -1;
  2055. }
  2056. if (os_strcmp(name, "bssid") != 0 &&
  2057. os_strcmp(name, "priority") != 0)
  2058. wpa_sm_pmksa_cache_flush(wpa_s->wpa, ssid);
  2059. if (wpa_s->current_ssid == ssid || wpa_s->current_ssid == NULL) {
  2060. /*
  2061. * Invalidate the EAP session cache if anything in the current
  2062. * or previously used configuration changes.
  2063. */
  2064. eapol_sm_invalidate_cached_session(wpa_s->eapol);
  2065. }
  2066. if ((os_strcmp(name, "psk") == 0 &&
  2067. value[0] == '"' && ssid->ssid_len) ||
  2068. (os_strcmp(name, "ssid") == 0 && ssid->passphrase))
  2069. wpa_config_update_psk(ssid);
  2070. else if (os_strcmp(name, "priority") == 0)
  2071. wpa_config_update_prio_list(wpa_s->conf);
  2072. return 0;
  2073. }
  2074. static int wpa_supplicant_ctrl_iface_get_network(
  2075. struct wpa_supplicant *wpa_s, char *cmd, char *buf, size_t buflen)
  2076. {
  2077. int id;
  2078. size_t res;
  2079. struct wpa_ssid *ssid;
  2080. char *name, *value;
  2081. /* cmd: "<network id> <variable name>" */
  2082. name = os_strchr(cmd, ' ');
  2083. if (name == NULL || buflen == 0)
  2084. return -1;
  2085. *name++ = '\0';
  2086. id = atoi(cmd);
  2087. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_NETWORK id=%d name='%s'",
  2088. id, name);
  2089. ssid = wpa_config_get_network(wpa_s->conf, id);
  2090. if (ssid == NULL) {
  2091. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find network "
  2092. "id=%d", id);
  2093. return -1;
  2094. }
  2095. value = wpa_config_get_no_key(ssid, name);
  2096. if (value == NULL) {
  2097. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to get network "
  2098. "variable '%s'", name);
  2099. return -1;
  2100. }
  2101. res = os_strlcpy(buf, value, buflen);
  2102. if (res >= buflen) {
  2103. os_free(value);
  2104. return -1;
  2105. }
  2106. os_free(value);
  2107. return res;
  2108. }
  2109. static int wpa_supplicant_ctrl_iface_list_creds(struct wpa_supplicant *wpa_s,
  2110. char *buf, size_t buflen)
  2111. {
  2112. char *pos, *end;
  2113. struct wpa_cred *cred;
  2114. int ret;
  2115. pos = buf;
  2116. end = buf + buflen;
  2117. ret = os_snprintf(pos, end - pos,
  2118. "cred id / realm / username / domain / imsi\n");
  2119. if (ret < 0 || ret >= end - pos)
  2120. return pos - buf;
  2121. pos += ret;
  2122. cred = wpa_s->conf->cred;
  2123. while (cred) {
  2124. ret = os_snprintf(pos, end - pos, "%d\t%s\t%s\t%s\t%s\n",
  2125. cred->id, cred->realm ? cred->realm : "",
  2126. cred->username ? cred->username : "",
  2127. cred->domain ? cred->domain[0] : "",
  2128. cred->imsi ? cred->imsi : "");
  2129. if (ret < 0 || ret >= end - pos)
  2130. return pos - buf;
  2131. pos += ret;
  2132. cred = cred->next;
  2133. }
  2134. return pos - buf;
  2135. }
  2136. static int wpa_supplicant_ctrl_iface_add_cred(struct wpa_supplicant *wpa_s,
  2137. char *buf, size_t buflen)
  2138. {
  2139. struct wpa_cred *cred;
  2140. int ret;
  2141. wpa_printf(MSG_DEBUG, "CTRL_IFACE: ADD_CRED");
  2142. cred = wpa_config_add_cred(wpa_s->conf);
  2143. if (cred == NULL)
  2144. return -1;
  2145. ret = os_snprintf(buf, buflen, "%d\n", cred->id);
  2146. if (ret < 0 || (size_t) ret >= buflen)
  2147. return -1;
  2148. return ret;
  2149. }
  2150. static int wpas_ctrl_remove_cred(struct wpa_supplicant *wpa_s,
  2151. struct wpa_cred *cred)
  2152. {
  2153. struct wpa_ssid *ssid;
  2154. char str[20];
  2155. if (cred == NULL || wpa_config_remove_cred(wpa_s->conf, cred->id) < 0) {
  2156. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred");
  2157. return -1;
  2158. }
  2159. /* Remove any network entry created based on the removed credential */
  2160. ssid = wpa_s->conf->ssid;
  2161. while (ssid) {
  2162. if (ssid->parent_cred == cred) {
  2163. wpa_printf(MSG_DEBUG, "Remove network id %d since it "
  2164. "used the removed credential", ssid->id);
  2165. os_snprintf(str, sizeof(str), "%d", ssid->id);
  2166. ssid = ssid->next;
  2167. wpa_supplicant_ctrl_iface_remove_network(wpa_s, str);
  2168. } else
  2169. ssid = ssid->next;
  2170. }
  2171. return 0;
  2172. }
  2173. static int wpa_supplicant_ctrl_iface_remove_cred(struct wpa_supplicant *wpa_s,
  2174. char *cmd)
  2175. {
  2176. int id;
  2177. struct wpa_cred *cred, *prev;
  2178. /* cmd: "<cred id>", "all", or "sp_fqdn=<FQDN>" */
  2179. if (os_strcmp(cmd, "all") == 0) {
  2180. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED all");
  2181. cred = wpa_s->conf->cred;
  2182. while (cred) {
  2183. prev = cred;
  2184. cred = cred->next;
  2185. wpas_ctrl_remove_cred(wpa_s, prev);
  2186. }
  2187. return 0;
  2188. }
  2189. if (os_strncmp(cmd, "sp_fqdn=", 8) == 0) {
  2190. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED SP FQDN '%s'",
  2191. cmd + 8);
  2192. cred = wpa_s->conf->cred;
  2193. while (cred) {
  2194. prev = cred;
  2195. cred = cred->next;
  2196. if (prev->domain) {
  2197. size_t i;
  2198. for (i = 0; i < prev->num_domain; i++) {
  2199. if (os_strcmp(prev->domain[i], cmd + 8)
  2200. != 0)
  2201. continue;
  2202. wpas_ctrl_remove_cred(wpa_s, prev);
  2203. break;
  2204. }
  2205. }
  2206. }
  2207. return 0;
  2208. }
  2209. id = atoi(cmd);
  2210. wpa_printf(MSG_DEBUG, "CTRL_IFACE: REMOVE_CRED id=%d", id);
  2211. cred = wpa_config_get_cred(wpa_s->conf, id);
  2212. return wpas_ctrl_remove_cred(wpa_s, cred);
  2213. }
  2214. static int wpa_supplicant_ctrl_iface_set_cred(struct wpa_supplicant *wpa_s,
  2215. char *cmd)
  2216. {
  2217. int id;
  2218. struct wpa_cred *cred;
  2219. char *name, *value;
  2220. /* cmd: "<cred id> <variable name> <value>" */
  2221. name = os_strchr(cmd, ' ');
  2222. if (name == NULL)
  2223. return -1;
  2224. *name++ = '\0';
  2225. value = os_strchr(name, ' ');
  2226. if (value == NULL)
  2227. return -1;
  2228. *value++ = '\0';
  2229. id = atoi(cmd);
  2230. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SET_CRED id=%d name='%s'",
  2231. id, name);
  2232. wpa_hexdump_ascii_key(MSG_DEBUG, "CTRL_IFACE: value",
  2233. (u8 *) value, os_strlen(value));
  2234. cred = wpa_config_get_cred(wpa_s->conf, id);
  2235. if (cred == NULL) {
  2236. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find cred id=%d",
  2237. id);
  2238. return -1;
  2239. }
  2240. if (wpa_config_set_cred(cred, name, value, 0) < 0) {
  2241. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Failed to set cred "
  2242. "variable '%s'", name);
  2243. return -1;
  2244. }
  2245. return 0;
  2246. }
  2247. #ifndef CONFIG_NO_CONFIG_WRITE
  2248. static int wpa_supplicant_ctrl_iface_save_config(struct wpa_supplicant *wpa_s)
  2249. {
  2250. int ret;
  2251. if (!wpa_s->conf->update_config) {
  2252. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed "
  2253. "to update configuration (update_config=0)");
  2254. return -1;
  2255. }
  2256. ret = wpa_config_write(wpa_s->confname, wpa_s->conf);
  2257. if (ret) {
  2258. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to "
  2259. "update configuration");
  2260. } else {
  2261. wpa_printf(MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration"
  2262. " updated");
  2263. }
  2264. return ret;
  2265. }
  2266. #endif /* CONFIG_NO_CONFIG_WRITE */
  2267. struct cipher_info {
  2268. unsigned int capa;
  2269. const char *name;
  2270. int group_only;
  2271. };
  2272. static const struct cipher_info ciphers[] = {
  2273. { WPA_DRIVER_CAPA_ENC_CCMP_256, "CCMP-256", 0 },
  2274. { WPA_DRIVER_CAPA_ENC_GCMP_256, "GCMP-256", 0 },
  2275. { WPA_DRIVER_CAPA_ENC_CCMP, "CCMP", 0 },
  2276. { WPA_DRIVER_CAPA_ENC_GCMP, "GCMP", 0 },
  2277. { WPA_DRIVER_CAPA_ENC_TKIP, "TKIP", 0 },
  2278. { WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE, "NONE", 0 },
  2279. { WPA_DRIVER_CAPA_ENC_WEP104, "WEP104", 1 },
  2280. { WPA_DRIVER_CAPA_ENC_WEP40, "WEP40", 1 }
  2281. };
  2282. static int ctrl_iface_get_capability_pairwise(int res, char *strict,
  2283. struct wpa_driver_capa *capa,
  2284. char *buf, size_t buflen)
  2285. {
  2286. int ret, first = 1;
  2287. char *pos, *end;
  2288. size_t len;
  2289. unsigned int i;
  2290. pos = buf;
  2291. end = pos + buflen;
  2292. if (res < 0) {
  2293. if (strict)
  2294. return 0;
  2295. len = os_strlcpy(buf, "CCMP TKIP NONE", buflen);
  2296. if (len >= buflen)
  2297. return -1;
  2298. return len;
  2299. }
  2300. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2301. if (!ciphers[i].group_only && capa->enc & ciphers[i].capa) {
  2302. ret = os_snprintf(pos, end - pos, "%s%s",
  2303. first ? "" : " ", ciphers[i].name);
  2304. if (ret < 0 || ret >= end - pos)
  2305. return pos - buf;
  2306. pos += ret;
  2307. first = 0;
  2308. }
  2309. }
  2310. return pos - buf;
  2311. }
  2312. static int ctrl_iface_get_capability_group(int res, char *strict,
  2313. struct wpa_driver_capa *capa,
  2314. char *buf, size_t buflen)
  2315. {
  2316. int ret, first = 1;
  2317. char *pos, *end;
  2318. size_t len;
  2319. unsigned int i;
  2320. pos = buf;
  2321. end = pos + buflen;
  2322. if (res < 0) {
  2323. if (strict)
  2324. return 0;
  2325. len = os_strlcpy(buf, "CCMP TKIP WEP104 WEP40", buflen);
  2326. if (len >= buflen)
  2327. return -1;
  2328. return len;
  2329. }
  2330. for (i = 0; i < ARRAY_SIZE(ciphers); i++) {
  2331. if (capa->enc & ciphers[i].capa) {
  2332. ret = os_snprintf(pos, end - pos, "%s%s",
  2333. first ? "" : " ", ciphers[i].name);
  2334. if (ret < 0 || ret >= end - pos)
  2335. return pos - buf;
  2336. pos += ret;
  2337. first = 0;
  2338. }
  2339. }
  2340. return pos - buf;
  2341. }
  2342. static int ctrl_iface_get_capability_key_mgmt(int res, char *strict,
  2343. struct wpa_driver_capa *capa,
  2344. char *buf, size_t buflen)
  2345. {
  2346. int ret;
  2347. char *pos, *end;
  2348. size_t len;
  2349. pos = buf;
  2350. end = pos + buflen;
  2351. if (res < 0) {
  2352. if (strict)
  2353. return 0;
  2354. len = os_strlcpy(buf, "WPA-PSK WPA-EAP IEEE8021X WPA-NONE "
  2355. "NONE", buflen);
  2356. if (len >= buflen)
  2357. return -1;
  2358. return len;
  2359. }
  2360. ret = os_snprintf(pos, end - pos, "NONE IEEE8021X");
  2361. if (ret < 0 || ret >= end - pos)
  2362. return pos - buf;
  2363. pos += ret;
  2364. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2365. WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
  2366. ret = os_snprintf(pos, end - pos, " WPA-EAP");
  2367. if (ret < 0 || ret >= end - pos)
  2368. return pos - buf;
  2369. pos += ret;
  2370. }
  2371. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
  2372. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2373. ret = os_snprintf(pos, end - pos, " WPA-PSK");
  2374. if (ret < 0 || ret >= end - pos)
  2375. return pos - buf;
  2376. pos += ret;
  2377. }
  2378. if (capa->key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
  2379. ret = os_snprintf(pos, end - pos, " WPA-NONE");
  2380. if (ret < 0 || ret >= end - pos)
  2381. return pos - buf;
  2382. pos += ret;
  2383. }
  2384. return pos - buf;
  2385. }
  2386. static int ctrl_iface_get_capability_proto(int res, char *strict,
  2387. struct wpa_driver_capa *capa,
  2388. char *buf, size_t buflen)
  2389. {
  2390. int ret, first = 1;
  2391. char *pos, *end;
  2392. size_t len;
  2393. pos = buf;
  2394. end = pos + buflen;
  2395. if (res < 0) {
  2396. if (strict)
  2397. return 0;
  2398. len = os_strlcpy(buf, "RSN WPA", buflen);
  2399. if (len >= buflen)
  2400. return -1;
  2401. return len;
  2402. }
  2403. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
  2404. WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
  2405. ret = os_snprintf(pos, end - pos, "%sRSN", first ? "" : " ");
  2406. if (ret < 0 || ret >= end - pos)
  2407. return pos - buf;
  2408. pos += ret;
  2409. first = 0;
  2410. }
  2411. if (capa->key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
  2412. WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
  2413. ret = os_snprintf(pos, end - pos, "%sWPA", first ? "" : " ");
  2414. if (ret < 0 || ret >= end - pos)
  2415. return pos - buf;
  2416. pos += ret;
  2417. first = 0;
  2418. }
  2419. return pos - buf;
  2420. }
  2421. static int ctrl_iface_get_capability_auth_alg(int res, char *strict,
  2422. struct wpa_driver_capa *capa,
  2423. char *buf, size_t buflen)
  2424. {
  2425. int ret, first = 1;
  2426. char *pos, *end;
  2427. size_t len;
  2428. pos = buf;
  2429. end = pos + buflen;
  2430. if (res < 0) {
  2431. if (strict)
  2432. return 0;
  2433. len = os_strlcpy(buf, "OPEN SHARED LEAP", buflen);
  2434. if (len >= buflen)
  2435. return -1;
  2436. return len;
  2437. }
  2438. if (capa->auth & (WPA_DRIVER_AUTH_OPEN)) {
  2439. ret = os_snprintf(pos, end - pos, "%sOPEN", first ? "" : " ");
  2440. if (ret < 0 || ret >= end - pos)
  2441. return pos - buf;
  2442. pos += ret;
  2443. first = 0;
  2444. }
  2445. if (capa->auth & (WPA_DRIVER_AUTH_SHARED)) {
  2446. ret = os_snprintf(pos, end - pos, "%sSHARED",
  2447. first ? "" : " ");
  2448. if (ret < 0 || ret >= end - pos)
  2449. return pos - buf;
  2450. pos += ret;
  2451. first = 0;
  2452. }
  2453. if (capa->auth & (WPA_DRIVER_AUTH_LEAP)) {
  2454. ret = os_snprintf(pos, end - pos, "%sLEAP", first ? "" : " ");
  2455. if (ret < 0 || ret >= end - pos)
  2456. return pos - buf;
  2457. pos += ret;
  2458. first = 0;
  2459. }
  2460. return pos - buf;
  2461. }
  2462. static int ctrl_iface_get_capability_modes(int res, char *strict,
  2463. struct wpa_driver_capa *capa,
  2464. char *buf, size_t buflen)
  2465. {
  2466. int ret, first = 1;
  2467. char *pos, *end;
  2468. size_t len;
  2469. pos = buf;
  2470. end = pos + buflen;
  2471. if (res < 0) {
  2472. if (strict)
  2473. return 0;
  2474. len = os_strlcpy(buf, "IBSS AP", buflen);
  2475. if (len >= buflen)
  2476. return -1;
  2477. return len;
  2478. }
  2479. if (capa->flags & WPA_DRIVER_FLAGS_IBSS) {
  2480. ret = os_snprintf(pos, end - pos, "%sIBSS", first ? "" : " ");
  2481. if (ret < 0 || ret >= end - pos)
  2482. return pos - buf;
  2483. pos += ret;
  2484. first = 0;
  2485. }
  2486. if (capa->flags & WPA_DRIVER_FLAGS_AP) {
  2487. ret = os_snprintf(pos, end - pos, "%sAP", first ? "" : " ");
  2488. if (ret < 0 || ret >= end - pos)
  2489. return pos - buf;
  2490. pos += ret;
  2491. first = 0;
  2492. }
  2493. return pos - buf;
  2494. }
  2495. static int ctrl_iface_get_capability_channels(struct wpa_supplicant *wpa_s,
  2496. char *buf, size_t buflen)
  2497. {
  2498. struct hostapd_channel_data *chnl;
  2499. int ret, i, j;
  2500. char *pos, *end, *hmode;
  2501. pos = buf;
  2502. end = pos + buflen;
  2503. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2504. switch (wpa_s->hw.modes[j].mode) {
  2505. case HOSTAPD_MODE_IEEE80211B:
  2506. hmode = "B";
  2507. break;
  2508. case HOSTAPD_MODE_IEEE80211G:
  2509. hmode = "G";
  2510. break;
  2511. case HOSTAPD_MODE_IEEE80211A:
  2512. hmode = "A";
  2513. break;
  2514. case HOSTAPD_MODE_IEEE80211AD:
  2515. hmode = "AD";
  2516. break;
  2517. default:
  2518. continue;
  2519. }
  2520. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:", hmode);
  2521. if (ret < 0 || ret >= end - pos)
  2522. return pos - buf;
  2523. pos += ret;
  2524. chnl = wpa_s->hw.modes[j].channels;
  2525. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2526. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2527. continue;
  2528. ret = os_snprintf(pos, end - pos, " %d", chnl[i].chan);
  2529. if (ret < 0 || ret >= end - pos)
  2530. return pos - buf;
  2531. pos += ret;
  2532. }
  2533. ret = os_snprintf(pos, end - pos, "\n");
  2534. if (ret < 0 || ret >= end - pos)
  2535. return pos - buf;
  2536. pos += ret;
  2537. }
  2538. return pos - buf;
  2539. }
  2540. static int ctrl_iface_get_capability_freq(struct wpa_supplicant *wpa_s,
  2541. char *buf, size_t buflen)
  2542. {
  2543. struct hostapd_channel_data *chnl;
  2544. int ret, i, j;
  2545. char *pos, *end, *hmode;
  2546. pos = buf;
  2547. end = pos + buflen;
  2548. for (j = 0; j < wpa_s->hw.num_modes; j++) {
  2549. switch (wpa_s->hw.modes[j].mode) {
  2550. case HOSTAPD_MODE_IEEE80211B:
  2551. hmode = "B";
  2552. break;
  2553. case HOSTAPD_MODE_IEEE80211G:
  2554. hmode = "G";
  2555. break;
  2556. case HOSTAPD_MODE_IEEE80211A:
  2557. hmode = "A";
  2558. break;
  2559. case HOSTAPD_MODE_IEEE80211AD:
  2560. hmode = "AD";
  2561. break;
  2562. default:
  2563. continue;
  2564. }
  2565. ret = os_snprintf(pos, end - pos, "Mode[%s] Channels:\n",
  2566. hmode);
  2567. if (ret < 0 || ret >= end - pos)
  2568. return pos - buf;
  2569. pos += ret;
  2570. chnl = wpa_s->hw.modes[j].channels;
  2571. for (i = 0; i < wpa_s->hw.modes[j].num_channels; i++) {
  2572. if (chnl[i].flag & HOSTAPD_CHAN_DISABLED)
  2573. continue;
  2574. ret = os_snprintf(pos, end - pos, " %d = %d MHz%s\n",
  2575. chnl[i].chan, chnl[i].freq,
  2576. chnl[i].flag & HOSTAPD_CHAN_NO_IBSS ?
  2577. " (NO_IBSS)" : "");
  2578. if (ret < 0 || ret >= end - pos)
  2579. return pos - buf;
  2580. pos += ret;
  2581. }
  2582. ret = os_snprintf(pos, end - pos, "\n");
  2583. if (ret < 0 || ret >= end - pos)
  2584. return pos - buf;
  2585. pos += ret;
  2586. }
  2587. return pos - buf;
  2588. }
  2589. static int wpa_supplicant_ctrl_iface_get_capability(
  2590. struct wpa_supplicant *wpa_s, const char *_field, char *buf,
  2591. size_t buflen)
  2592. {
  2593. struct wpa_driver_capa capa;
  2594. int res;
  2595. char *strict;
  2596. char field[30];
  2597. size_t len;
  2598. /* Determine whether or not strict checking was requested */
  2599. len = os_strlcpy(field, _field, sizeof(field));
  2600. if (len >= sizeof(field))
  2601. return -1;
  2602. strict = os_strchr(field, ' ');
  2603. if (strict != NULL) {
  2604. *strict++ = '\0';
  2605. if (os_strcmp(strict, "strict") != 0)
  2606. return -1;
  2607. }
  2608. wpa_printf(MSG_DEBUG, "CTRL_IFACE: GET_CAPABILITY '%s' %s",
  2609. field, strict ? strict : "");
  2610. if (os_strcmp(field, "eap") == 0) {
  2611. return eap_get_names(buf, buflen);
  2612. }
  2613. res = wpa_drv_get_capa(wpa_s, &capa);
  2614. if (os_strcmp(field, "pairwise") == 0)
  2615. return ctrl_iface_get_capability_pairwise(res, strict, &capa,
  2616. buf, buflen);
  2617. if (os_strcmp(field, "group") == 0)
  2618. return ctrl_iface_get_capability_group(res, strict, &capa,
  2619. buf, buflen);
  2620. if (os_strcmp(field, "key_mgmt") == 0)
  2621. return ctrl_iface_get_capability_key_mgmt(res, strict, &capa,
  2622. buf, buflen);
  2623. if (os_strcmp(field, "proto") == 0)
  2624. return ctrl_iface_get_capability_proto(res, strict, &capa,
  2625. buf, buflen);
  2626. if (os_strcmp(field, "auth_alg") == 0)
  2627. return ctrl_iface_get_capability_auth_alg(res, strict, &capa,
  2628. buf, buflen);
  2629. if (os_strcmp(field, "modes") == 0)
  2630. return ctrl_iface_get_capability_modes(res, strict, &capa,
  2631. buf, buflen);
  2632. if (os_strcmp(field, "channels") == 0)
  2633. return ctrl_iface_get_capability_channels(wpa_s, buf, buflen);
  2634. if (os_strcmp(field, "freq") == 0)
  2635. return ctrl_iface_get_capability_freq(wpa_s, buf, buflen);
  2636. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown GET_CAPABILITY field '%s'",
  2637. field);
  2638. return -1;
  2639. }
  2640. #ifdef CONFIG_INTERWORKING
  2641. static char * anqp_add_hex(char *pos, char *end, const char *title,
  2642. struct wpabuf *data)
  2643. {
  2644. char *start = pos;
  2645. size_t i;
  2646. int ret;
  2647. const u8 *d;
  2648. if (data == NULL)
  2649. return start;
  2650. ret = os_snprintf(pos, end - pos, "%s=", title);
  2651. if (ret < 0 || ret >= end - pos)
  2652. return start;
  2653. pos += ret;
  2654. d = wpabuf_head_u8(data);
  2655. for (i = 0; i < wpabuf_len(data); i++) {
  2656. ret = os_snprintf(pos, end - pos, "%02x", *d++);
  2657. if (ret < 0 || ret >= end - pos)
  2658. return start;
  2659. pos += ret;
  2660. }
  2661. ret = os_snprintf(pos, end - pos, "\n");
  2662. if (ret < 0 || ret >= end - pos)
  2663. return start;
  2664. pos += ret;
  2665. return pos;
  2666. }
  2667. #endif /* CONFIG_INTERWORKING */
  2668. static int print_bss_info(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
  2669. unsigned long mask, char *buf, size_t buflen)
  2670. {
  2671. size_t i;
  2672. int ret;
  2673. char *pos, *end;
  2674. const u8 *ie, *ie2;
  2675. pos = buf;
  2676. end = buf + buflen;
  2677. if (mask & WPA_BSS_MASK_ID) {
  2678. ret = os_snprintf(pos, end - pos, "id=%u\n", bss->id);
  2679. if (ret < 0 || ret >= end - pos)
  2680. return 0;
  2681. pos += ret;
  2682. }
  2683. if (mask & WPA_BSS_MASK_BSSID) {
  2684. ret = os_snprintf(pos, end - pos, "bssid=" MACSTR "\n",
  2685. MAC2STR(bss->bssid));
  2686. if (ret < 0 || ret >= end - pos)
  2687. return 0;
  2688. pos += ret;
  2689. }
  2690. if (mask & WPA_BSS_MASK_FREQ) {
  2691. ret = os_snprintf(pos, end - pos, "freq=%d\n", bss->freq);
  2692. if (ret < 0 || ret >= end - pos)
  2693. return 0;
  2694. pos += ret;
  2695. }
  2696. if (mask & WPA_BSS_MASK_BEACON_INT) {
  2697. ret = os_snprintf(pos, end - pos, "beacon_int=%d\n",
  2698. bss->beacon_int);
  2699. if (ret < 0 || ret >= end - pos)
  2700. return 0;
  2701. pos += ret;
  2702. }
  2703. if (mask & WPA_BSS_MASK_CAPABILITIES) {
  2704. ret = os_snprintf(pos, end - pos, "capabilities=0x%04x\n",
  2705. bss->caps);
  2706. if (ret < 0 || ret >= end - pos)
  2707. return 0;
  2708. pos += ret;
  2709. }
  2710. if (mask & WPA_BSS_MASK_QUAL) {
  2711. ret = os_snprintf(pos, end - pos, "qual=%d\n", bss->qual);
  2712. if (ret < 0 || ret >= end - pos)
  2713. return 0;
  2714. pos += ret;
  2715. }
  2716. if (mask & WPA_BSS_MASK_NOISE) {
  2717. ret = os_snprintf(pos, end - pos, "noise=%d\n", bss->noise);
  2718. if (ret < 0 || ret >= end - pos)
  2719. return 0;
  2720. pos += ret;
  2721. }
  2722. if (mask & WPA_BSS_MASK_LEVEL) {
  2723. ret = os_snprintf(pos, end - pos, "level=%d\n", bss->level);
  2724. if (ret < 0 || ret >= end - pos)
  2725. return 0;
  2726. pos += ret;
  2727. }
  2728. if (mask & WPA_BSS_MASK_TSF) {
  2729. ret = os_snprintf(pos, end - pos, "tsf=%016llu\n",
  2730. (unsigned long long) bss->tsf);
  2731. if (ret < 0 || ret >= end - pos)
  2732. return 0;
  2733. pos += ret;
  2734. }
  2735. if (mask & WPA_BSS_MASK_AGE) {
  2736. struct os_reltime now;
  2737. os_get_reltime(&now);
  2738. ret = os_snprintf(pos, end - pos, "age=%d\n",
  2739. (int) (now.sec - bss->last_update.sec));
  2740. if (ret < 0 || ret >= end - pos)
  2741. return 0;
  2742. pos += ret;
  2743. }
  2744. if (mask & WPA_BSS_MASK_IE) {
  2745. ret = os_snprintf(pos, end - pos, "ie=");
  2746. if (ret < 0 || ret >= end - pos)
  2747. return 0;
  2748. pos += ret;
  2749. ie = (const u8 *) (bss + 1);
  2750. for (i = 0; i < bss->ie_len; i++) {
  2751. ret = os_snprintf(pos, end - pos, "%02x", *ie++);
  2752. if (ret < 0 || ret >= end - pos)
  2753. return 0;
  2754. pos += ret;
  2755. }
  2756. ret = os_snprintf(pos, end - pos, "\n");
  2757. if (ret < 0 || ret >= end - pos)
  2758. return 0;
  2759. pos += ret;
  2760. }
  2761. if (mask & WPA_BSS_MASK_FLAGS) {
  2762. ret = os_snprintf(pos, end - pos, "flags=");
  2763. if (ret < 0 || ret >= end - pos)
  2764. return 0;
  2765. pos += ret;
  2766. ie = wpa_bss_get_vendor_ie(bss, WPA_IE_VENDOR_TYPE);
  2767. if (ie)
  2768. pos = wpa_supplicant_ie_txt(pos, end, "WPA", ie,
  2769. 2 + ie[1]);
  2770. ie2 = wpa_bss_get_ie(bss, WLAN_EID_RSN);
  2771. if (ie2)
  2772. pos = wpa_supplicant_ie_txt(pos, end, "WPA2", ie2,
  2773. 2 + ie2[1]);
  2774. pos = wpa_supplicant_wps_ie_txt(wpa_s, pos, end, bss);
  2775. if (!ie && !ie2 && bss->caps & IEEE80211_CAP_PRIVACY) {
  2776. ret = os_snprintf(pos, end - pos, "[WEP]");
  2777. if (ret < 0 || ret >= end - pos)
  2778. return 0;
  2779. pos += ret;
  2780. }
  2781. if (bss->caps & IEEE80211_CAP_IBSS) {
  2782. ret = os_snprintf(pos, end - pos, "[IBSS]");
  2783. if (ret < 0 || ret >= end - pos)
  2784. return 0;
  2785. pos += ret;
  2786. }
  2787. if (bss->caps & IEEE80211_CAP_ESS) {
  2788. ret = os_snprintf(pos, end - pos, "[ESS]");
  2789. if (ret < 0 || ret >= end - pos)
  2790. return 0;
  2791. pos += ret;
  2792. }
  2793. if (wpa_bss_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) ||
  2794. wpa_bss_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
  2795. ret = os_snprintf(pos, end - pos, "[P2P]");
  2796. if (ret < 0 || ret >= end - pos)
  2797. return 0;
  2798. pos += ret;
  2799. }
  2800. #ifdef CONFIG_HS20
  2801. if (wpa_bss_get_vendor_ie(bss, HS20_IE_VENDOR_TYPE)) {
  2802. ret = os_snprintf(pos, end - pos, "[HS20]");
  2803. if (ret < 0 || ret >= end - pos)
  2804. return 0;
  2805. pos += ret;
  2806. }
  2807. #endif /* CONFIG_HS20 */
  2808. ret = os_snprintf(pos, end - pos, "\n");
  2809. if (ret < 0 || ret >= end - pos)
  2810. return 0;
  2811. pos += ret;
  2812. }
  2813. if (mask & WPA_BSS_MASK_SSID) {
  2814. ret = os_snprintf(pos, end - pos, "ssid=%s\n",
  2815. wpa_ssid_txt(bss->ssid, bss->ssid_len));
  2816. if (ret < 0 || ret >= end - pos)
  2817. return 0;
  2818. pos += ret;
  2819. }
  2820. #ifdef CONFIG_WPS
  2821. if (mask & WPA_BSS_MASK_WPS_SCAN) {
  2822. ie = (const u8 *) (bss + 1);
  2823. ret = wpas_wps_scan_result_text(ie, bss->ie_len, pos, end);
  2824. if (ret < 0 || ret >= end - pos)
  2825. return 0;
  2826. pos += ret;
  2827. }
  2828. #endif /* CONFIG_WPS */
  2829. #ifdef CONFIG_P2P
  2830. if (mask & WPA_BSS_MASK_P2P_SCAN) {
  2831. ie = (const u8 *) (bss + 1);
  2832. ret = wpas_p2p_scan_result_text(ie, bss->ie_len, pos, end);
  2833. if (ret < 0 || ret >= end - pos)
  2834. return 0;
  2835. pos += ret;
  2836. }
  2837. #endif /* CONFIG_P2P */
  2838. #ifdef CONFIG_WIFI_DISPLAY
  2839. if (mask & WPA_BSS_MASK_WIFI_DISPLAY) {
  2840. struct wpabuf *wfd;
  2841. ie = (const u8 *) (bss + 1);
  2842. wfd = ieee802_11_vendor_ie_concat(ie, bss->ie_len,
  2843. WFD_IE_VENDOR_TYPE);
  2844. if (wfd) {
  2845. ret = os_snprintf(pos, end - pos, "wfd_subelems=");
  2846. if (ret < 0 || ret >= end - pos)
  2847. return 0;
  2848. pos += ret;
  2849. pos += wpa_snprintf_hex(pos, end - pos,
  2850. wpabuf_head(wfd),
  2851. wpabuf_len(wfd));
  2852. wpabuf_free(wfd);
  2853. ret = os_snprintf(pos, end - pos, "\n");
  2854. if (ret < 0 || ret >= end - pos)
  2855. return 0;
  2856. pos += ret;
  2857. }
  2858. }
  2859. #endif /* CONFIG_WIFI_DISPLAY */
  2860. #ifdef CONFIG_INTERWORKING
  2861. if ((mask & WPA_BSS_MASK_INTERNETW) && bss->anqp) {
  2862. struct wpa_bss_anqp *anqp = bss->anqp;
  2863. pos = anqp_add_hex(pos, end, "anqp_venue_name",
  2864. anqp->venue_name);
  2865. pos = anqp_add_hex(pos, end, "anqp_network_auth_type",
  2866. anqp->network_auth_type);
  2867. pos = anqp_add_hex(pos, end, "anqp_roaming_consortium",
  2868. anqp->roaming_consortium);
  2869. pos = anqp_add_hex(pos, end, "anqp_ip_addr_type_availability",
  2870. anqp->ip_addr_type_availability);
  2871. pos = anqp_add_hex(pos, end, "anqp_nai_realm",
  2872. anqp->nai_realm);
  2873. pos = anqp_add_hex(pos, end, "anqp_3gpp", anqp->anqp_3gpp);
  2874. pos = anqp_add_hex(pos, end, "anqp_domain_name",
  2875. anqp->domain_name);
  2876. #ifdef CONFIG_HS20
  2877. pos = anqp_add_hex(pos, end, "hs20_operator_friendly_name",
  2878. anqp->hs20_operator_friendly_name);
  2879. pos = anqp_add_hex(pos, end, "hs20_wan_metrics",
  2880. anqp->hs20_wan_metrics);
  2881. pos = anqp_add_hex(pos, end, "hs20_connection_capability",
  2882. anqp->hs20_connection_capability);
  2883. #endif /* CONFIG_HS20 */
  2884. }
  2885. #endif /* CONFIG_INTERWORKING */
  2886. if (mask & WPA_BSS_MASK_DELIM) {
  2887. ret = os_snprintf(pos, end - pos, "====\n");
  2888. if (ret < 0 || ret >= end - pos)
  2889. return 0;
  2890. pos += ret;
  2891. }
  2892. return pos - buf;
  2893. }
  2894. static int wpa_supplicant_ctrl_iface_bss(struct wpa_supplicant *wpa_s,
  2895. const char *cmd, char *buf,
  2896. size_t buflen)
  2897. {
  2898. u8 bssid[ETH_ALEN];
  2899. size_t i;
  2900. struct wpa_bss *bss;
  2901. struct wpa_bss *bsslast = NULL;
  2902. struct dl_list *next;
  2903. int ret = 0;
  2904. int len;
  2905. char *ctmp;
  2906. unsigned long mask = WPA_BSS_MASK_ALL;
  2907. if (os_strncmp(cmd, "RANGE=", 6) == 0) {
  2908. if (os_strncmp(cmd + 6, "ALL", 3) == 0) {
  2909. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss,
  2910. list_id);
  2911. bsslast = dl_list_last(&wpa_s->bss_id, struct wpa_bss,
  2912. list_id);
  2913. } else { /* N1-N2 */
  2914. unsigned int id1, id2;
  2915. if ((ctmp = os_strchr(cmd + 6, '-')) == NULL) {
  2916. wpa_printf(MSG_INFO, "Wrong BSS range "
  2917. "format");
  2918. return 0;
  2919. }
  2920. if (*(cmd + 6) == '-')
  2921. id1 = 0;
  2922. else
  2923. id1 = atoi(cmd + 6);
  2924. ctmp++;
  2925. if (*ctmp >= '0' && *ctmp <= '9')
  2926. id2 = atoi(ctmp);
  2927. else
  2928. id2 = (unsigned int) -1;
  2929. bss = wpa_bss_get_id_range(wpa_s, id1, id2);
  2930. if (id2 == (unsigned int) -1)
  2931. bsslast = dl_list_last(&wpa_s->bss_id,
  2932. struct wpa_bss,
  2933. list_id);
  2934. else {
  2935. bsslast = wpa_bss_get_id(wpa_s, id2);
  2936. if (bsslast == NULL && bss && id2 > id1) {
  2937. struct wpa_bss *tmp = bss;
  2938. for (;;) {
  2939. next = tmp->list_id.next;
  2940. if (next == &wpa_s->bss_id)
  2941. break;
  2942. tmp = dl_list_entry(
  2943. next, struct wpa_bss,
  2944. list_id);
  2945. if (tmp->id > id2)
  2946. break;
  2947. bsslast = tmp;
  2948. }
  2949. }
  2950. }
  2951. }
  2952. } else if (os_strncmp(cmd, "FIRST", 5) == 0)
  2953. bss = dl_list_first(&wpa_s->bss_id, struct wpa_bss, list_id);
  2954. else if (os_strncmp(cmd, "LAST", 4) == 0)
  2955. bss = dl_list_last(&wpa_s->bss_id, struct wpa_bss, list_id);
  2956. else if (os_strncmp(cmd, "ID-", 3) == 0) {
  2957. i = atoi(cmd + 3);
  2958. bss = wpa_bss_get_id(wpa_s, i);
  2959. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  2960. i = atoi(cmd + 5);
  2961. bss = wpa_bss_get_id(wpa_s, i);
  2962. if (bss) {
  2963. next = bss->list_id.next;
  2964. if (next == &wpa_s->bss_id)
  2965. bss = NULL;
  2966. else
  2967. bss = dl_list_entry(next, struct wpa_bss,
  2968. list_id);
  2969. }
  2970. #ifdef CONFIG_P2P
  2971. } else if (os_strncmp(cmd, "p2p_dev_addr=", 13) == 0) {
  2972. if (hwaddr_aton(cmd + 13, bssid) == 0)
  2973. bss = wpa_bss_get_p2p_dev_addr(wpa_s, bssid);
  2974. else
  2975. bss = NULL;
  2976. #endif /* CONFIG_P2P */
  2977. } else if (hwaddr_aton(cmd, bssid) == 0)
  2978. bss = wpa_bss_get_bssid(wpa_s, bssid);
  2979. else {
  2980. struct wpa_bss *tmp;
  2981. i = atoi(cmd);
  2982. bss = NULL;
  2983. dl_list_for_each(tmp, &wpa_s->bss_id, struct wpa_bss, list_id)
  2984. {
  2985. if (i-- == 0) {
  2986. bss = tmp;
  2987. break;
  2988. }
  2989. }
  2990. }
  2991. if ((ctmp = os_strstr(cmd, "MASK=")) != NULL) {
  2992. mask = strtoul(ctmp + 5, NULL, 0x10);
  2993. if (mask == 0)
  2994. mask = WPA_BSS_MASK_ALL;
  2995. }
  2996. if (bss == NULL)
  2997. return 0;
  2998. if (bsslast == NULL)
  2999. bsslast = bss;
  3000. do {
  3001. len = print_bss_info(wpa_s, bss, mask, buf, buflen);
  3002. ret += len;
  3003. buf += len;
  3004. buflen -= len;
  3005. if (bss == bsslast) {
  3006. if ((mask & WPA_BSS_MASK_DELIM) && len &&
  3007. (bss == dl_list_last(&wpa_s->bss_id,
  3008. struct wpa_bss, list_id)))
  3009. os_snprintf(buf - 5, 5, "####\n");
  3010. break;
  3011. }
  3012. next = bss->list_id.next;
  3013. if (next == &wpa_s->bss_id)
  3014. break;
  3015. bss = dl_list_entry(next, struct wpa_bss, list_id);
  3016. } while (bss && len);
  3017. return ret;
  3018. }
  3019. static int wpa_supplicant_ctrl_iface_ap_scan(
  3020. struct wpa_supplicant *wpa_s, char *cmd)
  3021. {
  3022. int ap_scan = atoi(cmd);
  3023. return wpa_supplicant_set_ap_scan(wpa_s, ap_scan);
  3024. }
  3025. static int wpa_supplicant_ctrl_iface_scan_interval(
  3026. struct wpa_supplicant *wpa_s, char *cmd)
  3027. {
  3028. int scan_int = atoi(cmd);
  3029. return wpa_supplicant_set_scan_interval(wpa_s, scan_int);
  3030. }
  3031. static int wpa_supplicant_ctrl_iface_bss_expire_age(
  3032. struct wpa_supplicant *wpa_s, char *cmd)
  3033. {
  3034. int expire_age = atoi(cmd);
  3035. return wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age);
  3036. }
  3037. static int wpa_supplicant_ctrl_iface_bss_expire_count(
  3038. struct wpa_supplicant *wpa_s, char *cmd)
  3039. {
  3040. int expire_count = atoi(cmd);
  3041. return wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count);
  3042. }
  3043. static int wpa_supplicant_ctrl_iface_bss_flush(
  3044. struct wpa_supplicant *wpa_s, char *cmd)
  3045. {
  3046. int flush_age = atoi(cmd);
  3047. if (flush_age == 0)
  3048. wpa_bss_flush(wpa_s);
  3049. else
  3050. wpa_bss_flush_by_age(wpa_s, flush_age);
  3051. return 0;
  3052. }
  3053. static void wpa_supplicant_ctrl_iface_drop_sa(struct wpa_supplicant *wpa_s)
  3054. {
  3055. wpa_printf(MSG_DEBUG, "Dropping SA without deauthentication");
  3056. /* MLME-DELETEKEYS.request */
  3057. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 0, 0, NULL, 0, NULL, 0);
  3058. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 1, 0, NULL, 0, NULL, 0);
  3059. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 2, 0, NULL, 0, NULL, 0);
  3060. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 3, 0, NULL, 0, NULL, 0);
  3061. #ifdef CONFIG_IEEE80211W
  3062. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 4, 0, NULL, 0, NULL, 0);
  3063. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, NULL, 5, 0, NULL, 0, NULL, 0);
  3064. #endif /* CONFIG_IEEE80211W */
  3065. wpa_drv_set_key(wpa_s, WPA_ALG_NONE, wpa_s->bssid, 0, 0, NULL, 0, NULL,
  3066. 0);
  3067. /* MLME-SETPROTECTION.request(None) */
  3068. wpa_drv_mlme_setprotection(wpa_s, wpa_s->bssid,
  3069. MLME_SETPROTECTION_PROTECT_TYPE_NONE,
  3070. MLME_SETPROTECTION_KEY_TYPE_PAIRWISE);
  3071. wpa_sm_drop_sa(wpa_s->wpa);
  3072. }
  3073. static int wpa_supplicant_ctrl_iface_roam(struct wpa_supplicant *wpa_s,
  3074. char *addr)
  3075. {
  3076. #ifdef CONFIG_NO_SCAN_PROCESSING
  3077. return -1;
  3078. #else /* CONFIG_NO_SCAN_PROCESSING */
  3079. u8 bssid[ETH_ALEN];
  3080. struct wpa_bss *bss;
  3081. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3082. if (hwaddr_aton(addr, bssid)) {
  3083. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: invalid "
  3084. "address '%s'", addr);
  3085. return -1;
  3086. }
  3087. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM " MACSTR, MAC2STR(bssid));
  3088. if (!ssid) {
  3089. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: No network "
  3090. "configuration known for the target AP");
  3091. return -1;
  3092. }
  3093. bss = wpa_bss_get(wpa_s, bssid, ssid->ssid, ssid->ssid_len);
  3094. if (!bss) {
  3095. wpa_printf(MSG_DEBUG, "CTRL_IFACE ROAM: Target AP not found "
  3096. "from BSS table");
  3097. return -1;
  3098. }
  3099. /*
  3100. * TODO: Find best network configuration block from configuration to
  3101. * allow roaming to other networks
  3102. */
  3103. wpa_s->reassociate = 1;
  3104. wpa_supplicant_connect(wpa_s, bss, ssid);
  3105. return 0;
  3106. #endif /* CONFIG_NO_SCAN_PROCESSING */
  3107. }
  3108. #ifdef CONFIG_P2P
  3109. static int p2p_ctrl_find(struct wpa_supplicant *wpa_s, char *cmd)
  3110. {
  3111. unsigned int timeout = atoi(cmd);
  3112. enum p2p_discovery_type type = P2P_FIND_START_WITH_FULL;
  3113. u8 dev_id[ETH_ALEN], *_dev_id = NULL;
  3114. u8 dev_type[WPS_DEV_TYPE_LEN], *_dev_type = NULL;
  3115. char *pos;
  3116. unsigned int search_delay;
  3117. if (os_strstr(cmd, "type=social"))
  3118. type = P2P_FIND_ONLY_SOCIAL;
  3119. else if (os_strstr(cmd, "type=progressive"))
  3120. type = P2P_FIND_PROGRESSIVE;
  3121. pos = os_strstr(cmd, "dev_id=");
  3122. if (pos) {
  3123. pos += 7;
  3124. if (hwaddr_aton(pos, dev_id))
  3125. return -1;
  3126. _dev_id = dev_id;
  3127. }
  3128. pos = os_strstr(cmd, "dev_type=");
  3129. if (pos) {
  3130. pos += 9;
  3131. if (wps_dev_type_str2bin(pos, dev_type) < 0)
  3132. return -1;
  3133. _dev_type = dev_type;
  3134. }
  3135. pos = os_strstr(cmd, "delay=");
  3136. if (pos) {
  3137. pos += 6;
  3138. search_delay = atoi(pos);
  3139. } else
  3140. search_delay = wpas_p2p_search_delay(wpa_s);
  3141. return wpas_p2p_find(wpa_s, timeout, type, _dev_type != NULL, _dev_type,
  3142. _dev_id, search_delay);
  3143. }
  3144. static int p2p_ctrl_connect(struct wpa_supplicant *wpa_s, char *cmd,
  3145. char *buf, size_t buflen)
  3146. {
  3147. u8 addr[ETH_ALEN];
  3148. char *pos, *pos2;
  3149. char *pin = NULL;
  3150. enum p2p_wps_method wps_method;
  3151. int new_pin;
  3152. int ret;
  3153. int persistent_group, persistent_id = -1;
  3154. int join;
  3155. int auth;
  3156. int automatic;
  3157. int go_intent = -1;
  3158. int freq = 0;
  3159. int pd;
  3160. int ht40, vht;
  3161. /* <addr> <"pbc" | "pin" | PIN> [label|display|keypad]
  3162. * [persistent|persistent=<network id>]
  3163. * [join] [auth] [go_intent=<0..15>] [freq=<in MHz>] [provdisc]
  3164. * [ht40] [vht] */
  3165. if (hwaddr_aton(cmd, addr))
  3166. return -1;
  3167. pos = cmd + 17;
  3168. if (*pos != ' ')
  3169. return -1;
  3170. pos++;
  3171. persistent_group = os_strstr(pos, " persistent") != NULL;
  3172. pos2 = os_strstr(pos, " persistent=");
  3173. if (pos2) {
  3174. struct wpa_ssid *ssid;
  3175. persistent_id = atoi(pos2 + 12);
  3176. ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
  3177. if (ssid == NULL || ssid->disabled != 2 ||
  3178. ssid->mode != WPAS_MODE_P2P_GO) {
  3179. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find "
  3180. "SSID id=%d for persistent P2P group (GO)",
  3181. persistent_id);
  3182. return -1;
  3183. }
  3184. }
  3185. join = os_strstr(pos, " join") != NULL;
  3186. auth = os_strstr(pos, " auth") != NULL;
  3187. automatic = os_strstr(pos, " auto") != NULL;
  3188. pd = os_strstr(pos, " provdisc") != NULL;
  3189. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3190. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3191. vht;
  3192. pos2 = os_strstr(pos, " go_intent=");
  3193. if (pos2) {
  3194. pos2 += 11;
  3195. go_intent = atoi(pos2);
  3196. if (go_intent < 0 || go_intent > 15)
  3197. return -1;
  3198. }
  3199. pos2 = os_strstr(pos, " freq=");
  3200. if (pos2) {
  3201. pos2 += 6;
  3202. freq = atoi(pos2);
  3203. if (freq <= 0)
  3204. return -1;
  3205. }
  3206. if (os_strncmp(pos, "pin", 3) == 0) {
  3207. /* Request random PIN (to be displayed) and enable the PIN */
  3208. wps_method = WPS_PIN_DISPLAY;
  3209. } else if (os_strncmp(pos, "pbc", 3) == 0) {
  3210. wps_method = WPS_PBC;
  3211. } else {
  3212. pin = pos;
  3213. pos = os_strchr(pin, ' ');
  3214. wps_method = WPS_PIN_KEYPAD;
  3215. if (pos) {
  3216. *pos++ = '\0';
  3217. if (os_strncmp(pos, "display", 7) == 0)
  3218. wps_method = WPS_PIN_DISPLAY;
  3219. }
  3220. if (!wps_pin_str_valid(pin)) {
  3221. os_memcpy(buf, "FAIL-INVALID-PIN\n", 17);
  3222. return 17;
  3223. }
  3224. }
  3225. new_pin = wpas_p2p_connect(wpa_s, addr, pin, wps_method,
  3226. persistent_group, automatic, join,
  3227. auth, go_intent, freq, persistent_id, pd,
  3228. ht40, vht);
  3229. if (new_pin == -2) {
  3230. os_memcpy(buf, "FAIL-CHANNEL-UNAVAILABLE\n", 25);
  3231. return 25;
  3232. }
  3233. if (new_pin == -3) {
  3234. os_memcpy(buf, "FAIL-CHANNEL-UNSUPPORTED\n", 25);
  3235. return 25;
  3236. }
  3237. if (new_pin < 0)
  3238. return -1;
  3239. if (wps_method == WPS_PIN_DISPLAY && pin == NULL) {
  3240. ret = os_snprintf(buf, buflen, "%08d", new_pin);
  3241. if (ret < 0 || (size_t) ret >= buflen)
  3242. return -1;
  3243. return ret;
  3244. }
  3245. os_memcpy(buf, "OK\n", 3);
  3246. return 3;
  3247. }
  3248. static int p2p_ctrl_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3249. {
  3250. unsigned int timeout = atoi(cmd);
  3251. return wpas_p2p_listen(wpa_s, timeout);
  3252. }
  3253. static int p2p_ctrl_prov_disc(struct wpa_supplicant *wpa_s, char *cmd)
  3254. {
  3255. u8 addr[ETH_ALEN];
  3256. char *pos;
  3257. enum wpas_p2p_prov_disc_use use = WPAS_P2P_PD_FOR_GO_NEG;
  3258. /* <addr> <config method> [join|auto] */
  3259. if (hwaddr_aton(cmd, addr))
  3260. return -1;
  3261. pos = cmd + 17;
  3262. if (*pos != ' ')
  3263. return -1;
  3264. pos++;
  3265. if (os_strstr(pos, " join") != NULL)
  3266. use = WPAS_P2P_PD_FOR_JOIN;
  3267. else if (os_strstr(pos, " auto") != NULL)
  3268. use = WPAS_P2P_PD_AUTO;
  3269. return wpas_p2p_prov_disc(wpa_s, addr, pos, use);
  3270. }
  3271. static int p2p_get_passphrase(struct wpa_supplicant *wpa_s, char *buf,
  3272. size_t buflen)
  3273. {
  3274. struct wpa_ssid *ssid = wpa_s->current_ssid;
  3275. if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
  3276. ssid->passphrase == NULL)
  3277. return -1;
  3278. os_strlcpy(buf, ssid->passphrase, buflen);
  3279. return os_strlen(buf);
  3280. }
  3281. static int p2p_ctrl_serv_disc_req(struct wpa_supplicant *wpa_s, char *cmd,
  3282. char *buf, size_t buflen)
  3283. {
  3284. u64 ref;
  3285. int res;
  3286. u8 dst_buf[ETH_ALEN], *dst;
  3287. struct wpabuf *tlvs;
  3288. char *pos;
  3289. size_t len;
  3290. if (hwaddr_aton(cmd, dst_buf))
  3291. return -1;
  3292. dst = dst_buf;
  3293. if (dst[0] == 0 && dst[1] == 0 && dst[2] == 0 &&
  3294. dst[3] == 0 && dst[4] == 0 && dst[5] == 0)
  3295. dst = NULL;
  3296. pos = cmd + 17;
  3297. if (*pos != ' ')
  3298. return -1;
  3299. pos++;
  3300. if (os_strncmp(pos, "upnp ", 5) == 0) {
  3301. u8 version;
  3302. pos += 5;
  3303. if (hexstr2bin(pos, &version, 1) < 0)
  3304. return -1;
  3305. pos += 2;
  3306. if (*pos != ' ')
  3307. return -1;
  3308. pos++;
  3309. ref = wpas_p2p_sd_request_upnp(wpa_s, dst, version, pos);
  3310. #ifdef CONFIG_WIFI_DISPLAY
  3311. } else if (os_strncmp(pos, "wifi-display ", 13) == 0) {
  3312. ref = wpas_p2p_sd_request_wifi_display(wpa_s, dst, pos + 13);
  3313. #endif /* CONFIG_WIFI_DISPLAY */
  3314. } else {
  3315. len = os_strlen(pos);
  3316. if (len & 1)
  3317. return -1;
  3318. len /= 2;
  3319. tlvs = wpabuf_alloc(len);
  3320. if (tlvs == NULL)
  3321. return -1;
  3322. if (hexstr2bin(pos, wpabuf_put(tlvs, len), len) < 0) {
  3323. wpabuf_free(tlvs);
  3324. return -1;
  3325. }
  3326. ref = wpas_p2p_sd_request(wpa_s, dst, tlvs);
  3327. wpabuf_free(tlvs);
  3328. }
  3329. if (ref == 0)
  3330. return -1;
  3331. res = os_snprintf(buf, buflen, "%llx", (long long unsigned) ref);
  3332. if (res < 0 || (unsigned) res >= buflen)
  3333. return -1;
  3334. return res;
  3335. }
  3336. static int p2p_ctrl_serv_disc_cancel_req(struct wpa_supplicant *wpa_s,
  3337. char *cmd)
  3338. {
  3339. long long unsigned val;
  3340. u64 req;
  3341. if (sscanf(cmd, "%llx", &val) != 1)
  3342. return -1;
  3343. req = val;
  3344. return wpas_p2p_sd_cancel_request(wpa_s, req);
  3345. }
  3346. static int p2p_ctrl_serv_disc_resp(struct wpa_supplicant *wpa_s, char *cmd)
  3347. {
  3348. int freq;
  3349. u8 dst[ETH_ALEN];
  3350. u8 dialog_token;
  3351. struct wpabuf *resp_tlvs;
  3352. char *pos, *pos2;
  3353. size_t len;
  3354. pos = os_strchr(cmd, ' ');
  3355. if (pos == NULL)
  3356. return -1;
  3357. *pos++ = '\0';
  3358. freq = atoi(cmd);
  3359. if (freq == 0)
  3360. return -1;
  3361. if (hwaddr_aton(pos, dst))
  3362. return -1;
  3363. pos += 17;
  3364. if (*pos != ' ')
  3365. return -1;
  3366. pos++;
  3367. pos2 = os_strchr(pos, ' ');
  3368. if (pos2 == NULL)
  3369. return -1;
  3370. *pos2++ = '\0';
  3371. dialog_token = atoi(pos);
  3372. len = os_strlen(pos2);
  3373. if (len & 1)
  3374. return -1;
  3375. len /= 2;
  3376. resp_tlvs = wpabuf_alloc(len);
  3377. if (resp_tlvs == NULL)
  3378. return -1;
  3379. if (hexstr2bin(pos2, wpabuf_put(resp_tlvs, len), len) < 0) {
  3380. wpabuf_free(resp_tlvs);
  3381. return -1;
  3382. }
  3383. wpas_p2p_sd_response(wpa_s, freq, dst, dialog_token, resp_tlvs);
  3384. wpabuf_free(resp_tlvs);
  3385. return 0;
  3386. }
  3387. static int p2p_ctrl_serv_disc_external(struct wpa_supplicant *wpa_s,
  3388. char *cmd)
  3389. {
  3390. if (os_strcmp(cmd, "0") && os_strcmp(cmd, "1"))
  3391. return -1;
  3392. wpa_s->p2p_sd_over_ctrl_iface = atoi(cmd);
  3393. return 0;
  3394. }
  3395. static int p2p_ctrl_service_add_bonjour(struct wpa_supplicant *wpa_s,
  3396. char *cmd)
  3397. {
  3398. char *pos;
  3399. size_t len;
  3400. struct wpabuf *query, *resp;
  3401. pos = os_strchr(cmd, ' ');
  3402. if (pos == NULL)
  3403. return -1;
  3404. *pos++ = '\0';
  3405. len = os_strlen(cmd);
  3406. if (len & 1)
  3407. return -1;
  3408. len /= 2;
  3409. query = wpabuf_alloc(len);
  3410. if (query == NULL)
  3411. return -1;
  3412. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3413. wpabuf_free(query);
  3414. return -1;
  3415. }
  3416. len = os_strlen(pos);
  3417. if (len & 1) {
  3418. wpabuf_free(query);
  3419. return -1;
  3420. }
  3421. len /= 2;
  3422. resp = wpabuf_alloc(len);
  3423. if (resp == NULL) {
  3424. wpabuf_free(query);
  3425. return -1;
  3426. }
  3427. if (hexstr2bin(pos, wpabuf_put(resp, len), len) < 0) {
  3428. wpabuf_free(query);
  3429. wpabuf_free(resp);
  3430. return -1;
  3431. }
  3432. if (wpas_p2p_service_add_bonjour(wpa_s, query, resp) < 0) {
  3433. wpabuf_free(query);
  3434. wpabuf_free(resp);
  3435. return -1;
  3436. }
  3437. return 0;
  3438. }
  3439. static int p2p_ctrl_service_add_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3440. {
  3441. char *pos;
  3442. u8 version;
  3443. pos = os_strchr(cmd, ' ');
  3444. if (pos == NULL)
  3445. return -1;
  3446. *pos++ = '\0';
  3447. if (hexstr2bin(cmd, &version, 1) < 0)
  3448. return -1;
  3449. return wpas_p2p_service_add_upnp(wpa_s, version, pos);
  3450. }
  3451. static int p2p_ctrl_service_add(struct wpa_supplicant *wpa_s, char *cmd)
  3452. {
  3453. char *pos;
  3454. pos = os_strchr(cmd, ' ');
  3455. if (pos == NULL)
  3456. return -1;
  3457. *pos++ = '\0';
  3458. if (os_strcmp(cmd, "bonjour") == 0)
  3459. return p2p_ctrl_service_add_bonjour(wpa_s, pos);
  3460. if (os_strcmp(cmd, "upnp") == 0)
  3461. return p2p_ctrl_service_add_upnp(wpa_s, pos);
  3462. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3463. return -1;
  3464. }
  3465. static int p2p_ctrl_service_del_bonjour(struct wpa_supplicant *wpa_s,
  3466. char *cmd)
  3467. {
  3468. size_t len;
  3469. struct wpabuf *query;
  3470. int ret;
  3471. len = os_strlen(cmd);
  3472. if (len & 1)
  3473. return -1;
  3474. len /= 2;
  3475. query = wpabuf_alloc(len);
  3476. if (query == NULL)
  3477. return -1;
  3478. if (hexstr2bin(cmd, wpabuf_put(query, len), len) < 0) {
  3479. wpabuf_free(query);
  3480. return -1;
  3481. }
  3482. ret = wpas_p2p_service_del_bonjour(wpa_s, query);
  3483. wpabuf_free(query);
  3484. return ret;
  3485. }
  3486. static int p2p_ctrl_service_del_upnp(struct wpa_supplicant *wpa_s, char *cmd)
  3487. {
  3488. char *pos;
  3489. u8 version;
  3490. pos = os_strchr(cmd, ' ');
  3491. if (pos == NULL)
  3492. return -1;
  3493. *pos++ = '\0';
  3494. if (hexstr2bin(cmd, &version, 1) < 0)
  3495. return -1;
  3496. return wpas_p2p_service_del_upnp(wpa_s, version, pos);
  3497. }
  3498. static int p2p_ctrl_service_del(struct wpa_supplicant *wpa_s, char *cmd)
  3499. {
  3500. char *pos;
  3501. pos = os_strchr(cmd, ' ');
  3502. if (pos == NULL)
  3503. return -1;
  3504. *pos++ = '\0';
  3505. if (os_strcmp(cmd, "bonjour") == 0)
  3506. return p2p_ctrl_service_del_bonjour(wpa_s, pos);
  3507. if (os_strcmp(cmd, "upnp") == 0)
  3508. return p2p_ctrl_service_del_upnp(wpa_s, pos);
  3509. wpa_printf(MSG_DEBUG, "Unknown service '%s'", cmd);
  3510. return -1;
  3511. }
  3512. static int p2p_ctrl_reject(struct wpa_supplicant *wpa_s, char *cmd)
  3513. {
  3514. u8 addr[ETH_ALEN];
  3515. /* <addr> */
  3516. if (hwaddr_aton(cmd, addr))
  3517. return -1;
  3518. return wpas_p2p_reject(wpa_s, addr);
  3519. }
  3520. static int p2p_ctrl_invite_persistent(struct wpa_supplicant *wpa_s, char *cmd)
  3521. {
  3522. char *pos;
  3523. int id;
  3524. struct wpa_ssid *ssid;
  3525. u8 *_peer = NULL, peer[ETH_ALEN];
  3526. int freq = 0, pref_freq = 0;
  3527. int ht40, vht;
  3528. id = atoi(cmd);
  3529. pos = os_strstr(cmd, " peer=");
  3530. if (pos) {
  3531. pos += 6;
  3532. if (hwaddr_aton(pos, peer))
  3533. return -1;
  3534. _peer = peer;
  3535. }
  3536. ssid = wpa_config_get_network(wpa_s->conf, id);
  3537. if (ssid == NULL || ssid->disabled != 2) {
  3538. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3539. "for persistent P2P group",
  3540. id);
  3541. return -1;
  3542. }
  3543. pos = os_strstr(cmd, " freq=");
  3544. if (pos) {
  3545. pos += 6;
  3546. freq = atoi(pos);
  3547. if (freq <= 0)
  3548. return -1;
  3549. }
  3550. pos = os_strstr(cmd, " pref=");
  3551. if (pos) {
  3552. pos += 6;
  3553. pref_freq = atoi(pos);
  3554. if (pref_freq <= 0)
  3555. return -1;
  3556. }
  3557. vht = (os_strstr(cmd, " vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3558. ht40 = (os_strstr(cmd, " ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3559. vht;
  3560. return wpas_p2p_invite(wpa_s, _peer, ssid, NULL, freq, ht40, vht,
  3561. pref_freq);
  3562. }
  3563. static int p2p_ctrl_invite_group(struct wpa_supplicant *wpa_s, char *cmd)
  3564. {
  3565. char *pos;
  3566. u8 peer[ETH_ALEN], go_dev_addr[ETH_ALEN], *go_dev = NULL;
  3567. pos = os_strstr(cmd, " peer=");
  3568. if (!pos)
  3569. return -1;
  3570. *pos = '\0';
  3571. pos += 6;
  3572. if (hwaddr_aton(pos, peer)) {
  3573. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'", pos);
  3574. return -1;
  3575. }
  3576. pos = os_strstr(pos, " go_dev_addr=");
  3577. if (pos) {
  3578. pos += 13;
  3579. if (hwaddr_aton(pos, go_dev_addr)) {
  3580. wpa_printf(MSG_DEBUG, "P2P: Invalid MAC address '%s'",
  3581. pos);
  3582. return -1;
  3583. }
  3584. go_dev = go_dev_addr;
  3585. }
  3586. return wpas_p2p_invite_group(wpa_s, cmd, peer, go_dev);
  3587. }
  3588. static int p2p_ctrl_invite(struct wpa_supplicant *wpa_s, char *cmd)
  3589. {
  3590. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3591. return p2p_ctrl_invite_persistent(wpa_s, cmd + 11);
  3592. if (os_strncmp(cmd, "group=", 6) == 0)
  3593. return p2p_ctrl_invite_group(wpa_s, cmd + 6);
  3594. return -1;
  3595. }
  3596. static int p2p_ctrl_group_add_persistent(struct wpa_supplicant *wpa_s,
  3597. char *cmd, int freq, int ht40,
  3598. int vht)
  3599. {
  3600. int id;
  3601. struct wpa_ssid *ssid;
  3602. id = atoi(cmd);
  3603. ssid = wpa_config_get_network(wpa_s->conf, id);
  3604. if (ssid == NULL || ssid->disabled != 2) {
  3605. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Could not find SSID id=%d "
  3606. "for persistent P2P group",
  3607. id);
  3608. return -1;
  3609. }
  3610. return wpas_p2p_group_add_persistent(wpa_s, ssid, 0, freq, ht40, vht,
  3611. NULL, 0);
  3612. }
  3613. static int p2p_ctrl_group_add(struct wpa_supplicant *wpa_s, char *cmd)
  3614. {
  3615. int freq = 0, ht40, vht;
  3616. char *pos;
  3617. pos = os_strstr(cmd, "freq=");
  3618. if (pos)
  3619. freq = atoi(pos + 5);
  3620. vht = (os_strstr(cmd, "vht") != NULL) || wpa_s->conf->p2p_go_vht;
  3621. ht40 = (os_strstr(cmd, "ht40") != NULL) || wpa_s->conf->p2p_go_ht40 ||
  3622. vht;
  3623. if (os_strncmp(cmd, "persistent=", 11) == 0)
  3624. return p2p_ctrl_group_add_persistent(wpa_s, cmd + 11, freq,
  3625. ht40, vht);
  3626. if (os_strcmp(cmd, "persistent") == 0 ||
  3627. os_strncmp(cmd, "persistent ", 11) == 0)
  3628. return wpas_p2p_group_add(wpa_s, 1, freq, ht40, vht);
  3629. if (os_strncmp(cmd, "freq=", 5) == 0)
  3630. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3631. if (ht40)
  3632. return wpas_p2p_group_add(wpa_s, 0, freq, ht40, vht);
  3633. wpa_printf(MSG_DEBUG, "CTRL: Invalid P2P_GROUP_ADD parameters '%s'",
  3634. cmd);
  3635. return -1;
  3636. }
  3637. static int p2p_ctrl_peer(struct wpa_supplicant *wpa_s, char *cmd,
  3638. char *buf, size_t buflen)
  3639. {
  3640. u8 addr[ETH_ALEN], *addr_ptr;
  3641. int next, res;
  3642. const struct p2p_peer_info *info;
  3643. char *pos, *end;
  3644. char devtype[WPS_DEV_TYPE_BUFSIZE];
  3645. struct wpa_ssid *ssid;
  3646. size_t i;
  3647. if (!wpa_s->global->p2p)
  3648. return -1;
  3649. if (os_strcmp(cmd, "FIRST") == 0) {
  3650. addr_ptr = NULL;
  3651. next = 0;
  3652. } else if (os_strncmp(cmd, "NEXT-", 5) == 0) {
  3653. if (hwaddr_aton(cmd + 5, addr) < 0)
  3654. return -1;
  3655. addr_ptr = addr;
  3656. next = 1;
  3657. } else {
  3658. if (hwaddr_aton(cmd, addr) < 0)
  3659. return -1;
  3660. addr_ptr = addr;
  3661. next = 0;
  3662. }
  3663. info = p2p_get_peer_info(wpa_s->global->p2p, addr_ptr, next);
  3664. if (info == NULL)
  3665. return -1;
  3666. pos = buf;
  3667. end = buf + buflen;
  3668. res = os_snprintf(pos, end - pos, MACSTR "\n"
  3669. "pri_dev_type=%s\n"
  3670. "device_name=%s\n"
  3671. "manufacturer=%s\n"
  3672. "model_name=%s\n"
  3673. "model_number=%s\n"
  3674. "serial_number=%s\n"
  3675. "config_methods=0x%x\n"
  3676. "dev_capab=0x%x\n"
  3677. "group_capab=0x%x\n"
  3678. "level=%d\n",
  3679. MAC2STR(info->p2p_device_addr),
  3680. wps_dev_type_bin2str(info->pri_dev_type,
  3681. devtype, sizeof(devtype)),
  3682. info->device_name,
  3683. info->manufacturer,
  3684. info->model_name,
  3685. info->model_number,
  3686. info->serial_number,
  3687. info->config_methods,
  3688. info->dev_capab,
  3689. info->group_capab,
  3690. info->level);
  3691. if (res < 0 || res >= end - pos)
  3692. return pos - buf;
  3693. pos += res;
  3694. for (i = 0; i < info->wps_sec_dev_type_list_len / WPS_DEV_TYPE_LEN; i++)
  3695. {
  3696. const u8 *t;
  3697. t = &info->wps_sec_dev_type_list[i * WPS_DEV_TYPE_LEN];
  3698. res = os_snprintf(pos, end - pos, "sec_dev_type=%s\n",
  3699. wps_dev_type_bin2str(t, devtype,
  3700. sizeof(devtype)));
  3701. if (res < 0 || res >= end - pos)
  3702. return pos - buf;
  3703. pos += res;
  3704. }
  3705. ssid = wpas_p2p_get_persistent(wpa_s, info->p2p_device_addr, NULL, 0);
  3706. if (ssid) {
  3707. res = os_snprintf(pos, end - pos, "persistent=%d\n", ssid->id);
  3708. if (res < 0 || res >= end - pos)
  3709. return pos - buf;
  3710. pos += res;
  3711. }
  3712. res = p2p_get_peer_info_txt(info, pos, end - pos);
  3713. if (res < 0)
  3714. return pos - buf;
  3715. pos += res;
  3716. return pos - buf;
  3717. }
  3718. static int p2p_ctrl_disallow_freq(struct wpa_supplicant *wpa_s,
  3719. const char *param)
  3720. {
  3721. unsigned int i;
  3722. if (wpa_s->global->p2p == NULL)
  3723. return -1;
  3724. if (freq_range_list_parse(&wpa_s->global->p2p_disallow_freq, param) < 0)
  3725. return -1;
  3726. for (i = 0; i < wpa_s->global->p2p_disallow_freq.num; i++) {
  3727. struct wpa_freq_range *freq;
  3728. freq = &wpa_s->global->p2p_disallow_freq.range[i];
  3729. wpa_printf(MSG_DEBUG, "P2P: Disallowed frequency range %u-%u",
  3730. freq->min, freq->max);
  3731. }
  3732. wpas_p2p_update_channel_list(wpa_s);
  3733. return 0;
  3734. }
  3735. static int p2p_ctrl_set(struct wpa_supplicant *wpa_s, char *cmd)
  3736. {
  3737. char *param;
  3738. if (wpa_s->global->p2p == NULL)
  3739. return -1;
  3740. param = os_strchr(cmd, ' ');
  3741. if (param == NULL)
  3742. return -1;
  3743. *param++ = '\0';
  3744. if (os_strcmp(cmd, "discoverability") == 0) {
  3745. p2p_set_client_discoverability(wpa_s->global->p2p,
  3746. atoi(param));
  3747. return 0;
  3748. }
  3749. if (os_strcmp(cmd, "managed") == 0) {
  3750. p2p_set_managed_oper(wpa_s->global->p2p, atoi(param));
  3751. return 0;
  3752. }
  3753. if (os_strcmp(cmd, "listen_channel") == 0) {
  3754. return p2p_set_listen_channel(wpa_s->global->p2p, 81,
  3755. atoi(param));
  3756. }
  3757. if (os_strcmp(cmd, "ssid_postfix") == 0) {
  3758. return p2p_set_ssid_postfix(wpa_s->global->p2p, (u8 *) param,
  3759. os_strlen(param));
  3760. }
  3761. if (os_strcmp(cmd, "noa") == 0) {
  3762. char *pos;
  3763. int count, start, duration;
  3764. /* GO NoA parameters: count,start_offset(ms),duration(ms) */
  3765. count = atoi(param);
  3766. pos = os_strchr(param, ',');
  3767. if (pos == NULL)
  3768. return -1;
  3769. pos++;
  3770. start = atoi(pos);
  3771. pos = os_strchr(pos, ',');
  3772. if (pos == NULL)
  3773. return -1;
  3774. pos++;
  3775. duration = atoi(pos);
  3776. if (count < 0 || count > 255 || start < 0 || duration < 0)
  3777. return -1;
  3778. if (count == 0 && duration > 0)
  3779. return -1;
  3780. wpa_printf(MSG_DEBUG, "CTRL_IFACE: P2P_SET GO NoA: count=%d "
  3781. "start=%d duration=%d", count, start, duration);
  3782. return wpas_p2p_set_noa(wpa_s, count, start, duration);
  3783. }
  3784. if (os_strcmp(cmd, "ps") == 0)
  3785. return wpa_drv_set_p2p_powersave(wpa_s, atoi(param), -1, -1);
  3786. if (os_strcmp(cmd, "oppps") == 0)
  3787. return wpa_drv_set_p2p_powersave(wpa_s, -1, atoi(param), -1);
  3788. if (os_strcmp(cmd, "ctwindow") == 0)
  3789. return wpa_drv_set_p2p_powersave(wpa_s, -1, -1, atoi(param));
  3790. if (os_strcmp(cmd, "disabled") == 0) {
  3791. wpa_s->global->p2p_disabled = atoi(param);
  3792. wpa_printf(MSG_DEBUG, "P2P functionality %s",
  3793. wpa_s->global->p2p_disabled ?
  3794. "disabled" : "enabled");
  3795. if (wpa_s->global->p2p_disabled) {
  3796. wpas_p2p_stop_find(wpa_s);
  3797. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3798. p2p_flush(wpa_s->global->p2p);
  3799. }
  3800. return 0;
  3801. }
  3802. if (os_strcmp(cmd, "conc_pref") == 0) {
  3803. if (os_strcmp(param, "sta") == 0)
  3804. wpa_s->global->conc_pref = WPA_CONC_PREF_STA;
  3805. else if (os_strcmp(param, "p2p") == 0)
  3806. wpa_s->global->conc_pref = WPA_CONC_PREF_P2P;
  3807. else {
  3808. wpa_printf(MSG_INFO, "Invalid conc_pref value");
  3809. return -1;
  3810. }
  3811. wpa_printf(MSG_DEBUG, "Single channel concurrency preference: "
  3812. "%s", param);
  3813. return 0;
  3814. }
  3815. if (os_strcmp(cmd, "force_long_sd") == 0) {
  3816. wpa_s->force_long_sd = atoi(param);
  3817. return 0;
  3818. }
  3819. if (os_strcmp(cmd, "peer_filter") == 0) {
  3820. u8 addr[ETH_ALEN];
  3821. if (hwaddr_aton(param, addr))
  3822. return -1;
  3823. p2p_set_peer_filter(wpa_s->global->p2p, addr);
  3824. return 0;
  3825. }
  3826. if (os_strcmp(cmd, "cross_connect") == 0)
  3827. return wpas_p2p_set_cross_connect(wpa_s, atoi(param));
  3828. if (os_strcmp(cmd, "go_apsd") == 0) {
  3829. if (os_strcmp(param, "disable") == 0)
  3830. wpa_s->set_ap_uapsd = 0;
  3831. else {
  3832. wpa_s->set_ap_uapsd = 1;
  3833. wpa_s->ap_uapsd = atoi(param);
  3834. }
  3835. return 0;
  3836. }
  3837. if (os_strcmp(cmd, "client_apsd") == 0) {
  3838. if (os_strcmp(param, "disable") == 0)
  3839. wpa_s->set_sta_uapsd = 0;
  3840. else {
  3841. int be, bk, vi, vo;
  3842. char *pos;
  3843. /* format: BE,BK,VI,VO;max SP Length */
  3844. be = atoi(param);
  3845. pos = os_strchr(param, ',');
  3846. if (pos == NULL)
  3847. return -1;
  3848. pos++;
  3849. bk = atoi(pos);
  3850. pos = os_strchr(pos, ',');
  3851. if (pos == NULL)
  3852. return -1;
  3853. pos++;
  3854. vi = atoi(pos);
  3855. pos = os_strchr(pos, ',');
  3856. if (pos == NULL)
  3857. return -1;
  3858. pos++;
  3859. vo = atoi(pos);
  3860. /* ignore max SP Length for now */
  3861. wpa_s->set_sta_uapsd = 1;
  3862. wpa_s->sta_uapsd = 0;
  3863. if (be)
  3864. wpa_s->sta_uapsd |= BIT(0);
  3865. if (bk)
  3866. wpa_s->sta_uapsd |= BIT(1);
  3867. if (vi)
  3868. wpa_s->sta_uapsd |= BIT(2);
  3869. if (vo)
  3870. wpa_s->sta_uapsd |= BIT(3);
  3871. }
  3872. return 0;
  3873. }
  3874. if (os_strcmp(cmd, "disallow_freq") == 0)
  3875. return p2p_ctrl_disallow_freq(wpa_s, param);
  3876. if (os_strcmp(cmd, "disc_int") == 0) {
  3877. int min_disc_int, max_disc_int, max_disc_tu;
  3878. char *pos;
  3879. pos = param;
  3880. min_disc_int = atoi(pos);
  3881. pos = os_strchr(pos, ' ');
  3882. if (pos == NULL)
  3883. return -1;
  3884. *pos++ = '\0';
  3885. max_disc_int = atoi(pos);
  3886. pos = os_strchr(pos, ' ');
  3887. if (pos == NULL)
  3888. return -1;
  3889. *pos++ = '\0';
  3890. max_disc_tu = atoi(pos);
  3891. return p2p_set_disc_int(wpa_s->global->p2p, min_disc_int,
  3892. max_disc_int, max_disc_tu);
  3893. }
  3894. if (os_strcmp(cmd, "per_sta_psk") == 0) {
  3895. wpa_s->global->p2p_per_sta_psk = !!atoi(param);
  3896. return 0;
  3897. }
  3898. wpa_printf(MSG_DEBUG, "CTRL_IFACE: Unknown P2P_SET field value '%s'",
  3899. cmd);
  3900. return -1;
  3901. }
  3902. static void p2p_ctrl_flush(struct wpa_supplicant *wpa_s)
  3903. {
  3904. os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
  3905. wpa_s->force_long_sd = 0;
  3906. if (wpa_s->global->p2p)
  3907. p2p_flush(wpa_s->global->p2p);
  3908. }
  3909. static int p2p_ctrl_presence_req(struct wpa_supplicant *wpa_s, char *cmd)
  3910. {
  3911. char *pos, *pos2;
  3912. unsigned int dur1 = 0, int1 = 0, dur2 = 0, int2 = 0;
  3913. if (cmd[0]) {
  3914. pos = os_strchr(cmd, ' ');
  3915. if (pos == NULL)
  3916. return -1;
  3917. *pos++ = '\0';
  3918. dur1 = atoi(cmd);
  3919. pos2 = os_strchr(pos, ' ');
  3920. if (pos2)
  3921. *pos2++ = '\0';
  3922. int1 = atoi(pos);
  3923. } else
  3924. pos2 = NULL;
  3925. if (pos2) {
  3926. pos = os_strchr(pos2, ' ');
  3927. if (pos == NULL)
  3928. return -1;
  3929. *pos++ = '\0';
  3930. dur2 = atoi(pos2);
  3931. int2 = atoi(pos);
  3932. }
  3933. return wpas_p2p_presence_req(wpa_s, dur1, int1, dur2, int2);
  3934. }
  3935. static int p2p_ctrl_ext_listen(struct wpa_supplicant *wpa_s, char *cmd)
  3936. {
  3937. char *pos;
  3938. unsigned int period = 0, interval = 0;
  3939. if (cmd[0]) {
  3940. pos = os_strchr(cmd, ' ');
  3941. if (pos == NULL)
  3942. return -1;
  3943. *pos++ = '\0';
  3944. period = atoi(cmd);
  3945. interval = atoi(pos);
  3946. }
  3947. return wpas_p2p_ext_listen(wpa_s, period, interval);
  3948. }
  3949. static int p2p_ctrl_remove_client(struct wpa_supplicant *wpa_s, const char *cmd)
  3950. {
  3951. const char *pos;
  3952. u8 peer[ETH_ALEN];
  3953. int iface_addr = 0;
  3954. pos = cmd;
  3955. if (os_strncmp(pos, "iface=", 6) == 0) {
  3956. iface_addr = 1;
  3957. pos += 6;
  3958. }
  3959. if (hwaddr_aton(pos, peer))
  3960. return -1;
  3961. wpas_p2p_remove_client(wpa_s, peer, iface_addr);
  3962. return 0;
  3963. }
  3964. #endif /* CONFIG_P2P */
  3965. static int * freq_range_to_channel_list(struct wpa_supplicant *wpa_s, char *val)
  3966. {
  3967. struct wpa_freq_range_list ranges;
  3968. int *freqs = NULL;
  3969. struct hostapd_hw_modes *mode;
  3970. u16 i;
  3971. if (wpa_s->hw.modes == NULL)
  3972. return NULL;
  3973. os_memset(&ranges, 0, sizeof(ranges));
  3974. if (freq_range_list_parse(&ranges, val) < 0)
  3975. return NULL;
  3976. for (i = 0; i < wpa_s->hw.num_modes; i++) {
  3977. int j;
  3978. mode = &wpa_s->hw.modes[i];
  3979. for (j = 0; j < mode->num_channels; j++) {
  3980. unsigned int freq;
  3981. if (mode->channels[j].flag & HOSTAPD_CHAN_DISABLED)
  3982. continue;
  3983. freq = mode->channels[j].freq;
  3984. if (!freq_range_list_includes(&ranges, freq))
  3985. continue;
  3986. int_array_add_unique(&freqs, freq);
  3987. }
  3988. }
  3989. os_free(ranges.range);
  3990. return freqs;
  3991. }
  3992. #ifdef CONFIG_INTERWORKING
  3993. static int ctrl_interworking_select(struct wpa_supplicant *wpa_s, char *param)
  3994. {
  3995. int auto_sel = 0;
  3996. int *freqs = NULL;
  3997. if (param) {
  3998. char *pos;
  3999. auto_sel = os_strstr(param, "auto") != NULL;
  4000. pos = os_strstr(param, "freq=");
  4001. if (pos) {
  4002. freqs = freq_range_to_channel_list(wpa_s, pos + 5);
  4003. if (freqs == NULL)
  4004. return -1;
  4005. }
  4006. }
  4007. return interworking_select(wpa_s, auto_sel, freqs);
  4008. }
  4009. static int ctrl_interworking_connect(struct wpa_supplicant *wpa_s, char *dst)
  4010. {
  4011. u8 bssid[ETH_ALEN];
  4012. struct wpa_bss *bss;
  4013. if (hwaddr_aton(dst, bssid)) {
  4014. wpa_printf(MSG_DEBUG, "Invalid BSSID '%s'", dst);
  4015. return -1;
  4016. }
  4017. bss = wpa_bss_get_bssid(wpa_s, bssid);
  4018. if (bss == NULL) {
  4019. wpa_printf(MSG_DEBUG, "Could not find BSS " MACSTR,
  4020. MAC2STR(bssid));
  4021. return -1;
  4022. }
  4023. return interworking_connect(wpa_s, bss);
  4024. }
  4025. static int get_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4026. {
  4027. u8 dst_addr[ETH_ALEN];
  4028. int used;
  4029. char *pos;
  4030. #define MAX_ANQP_INFO_ID 100
  4031. u16 id[MAX_ANQP_INFO_ID];
  4032. size_t num_id = 0;
  4033. used = hwaddr_aton2(dst, dst_addr);
  4034. if (used < 0)
  4035. return -1;
  4036. pos = dst + used;
  4037. while (num_id < MAX_ANQP_INFO_ID) {
  4038. id[num_id] = atoi(pos);
  4039. if (id[num_id])
  4040. num_id++;
  4041. pos = os_strchr(pos + 1, ',');
  4042. if (pos == NULL)
  4043. break;
  4044. pos++;
  4045. }
  4046. if (num_id == 0)
  4047. return -1;
  4048. return anqp_send_req(wpa_s, dst_addr, id, num_id);
  4049. }
  4050. static int gas_request(struct wpa_supplicant *wpa_s, char *cmd)
  4051. {
  4052. u8 dst_addr[ETH_ALEN];
  4053. struct wpabuf *advproto, *query = NULL;
  4054. int used, ret = -1;
  4055. char *pos, *end;
  4056. size_t len;
  4057. used = hwaddr_aton2(cmd, dst_addr);
  4058. if (used < 0)
  4059. return -1;
  4060. pos = cmd + used;
  4061. while (*pos == ' ')
  4062. pos++;
  4063. /* Advertisement Protocol ID */
  4064. end = os_strchr(pos, ' ');
  4065. if (end)
  4066. len = end - pos;
  4067. else
  4068. len = os_strlen(pos);
  4069. if (len & 0x01)
  4070. return -1;
  4071. len /= 2;
  4072. if (len == 0)
  4073. return -1;
  4074. advproto = wpabuf_alloc(len);
  4075. if (advproto == NULL)
  4076. return -1;
  4077. if (hexstr2bin(pos, wpabuf_put(advproto, len), len) < 0)
  4078. goto fail;
  4079. if (end) {
  4080. /* Optional Query Request */
  4081. pos = end + 1;
  4082. while (*pos == ' ')
  4083. pos++;
  4084. len = os_strlen(pos);
  4085. if (len) {
  4086. if (len & 0x01)
  4087. goto fail;
  4088. len /= 2;
  4089. if (len == 0)
  4090. goto fail;
  4091. query = wpabuf_alloc(len);
  4092. if (query == NULL)
  4093. goto fail;
  4094. if (hexstr2bin(pos, wpabuf_put(query, len), len) < 0)
  4095. goto fail;
  4096. }
  4097. }
  4098. ret = gas_send_request(wpa_s, dst_addr, advproto, query);
  4099. fail:
  4100. wpabuf_free(advproto);
  4101. wpabuf_free(query);
  4102. return ret;
  4103. }
  4104. static int gas_response_get(struct wpa_supplicant *wpa_s, char *cmd, char *buf,
  4105. size_t buflen)
  4106. {
  4107. u8 addr[ETH_ALEN];
  4108. int dialog_token;
  4109. int used;
  4110. char *pos;
  4111. size_t resp_len, start, requested_len;
  4112. struct wpabuf *resp;
  4113. int ret;
  4114. used = hwaddr_aton2(cmd, addr);
  4115. if (used < 0)
  4116. return -1;
  4117. pos = cmd + used;
  4118. while (*pos == ' ')
  4119. pos++;
  4120. dialog_token = atoi(pos);
  4121. if (wpa_s->last_gas_resp &&
  4122. os_memcmp(addr, wpa_s->last_gas_addr, ETH_ALEN) == 0 &&
  4123. dialog_token == wpa_s->last_gas_dialog_token)
  4124. resp = wpa_s->last_gas_resp;
  4125. else if (wpa_s->prev_gas_resp &&
  4126. os_memcmp(addr, wpa_s->prev_gas_addr, ETH_ALEN) == 0 &&
  4127. dialog_token == wpa_s->prev_gas_dialog_token)
  4128. resp = wpa_s->prev_gas_resp;
  4129. else
  4130. return -1;
  4131. resp_len = wpabuf_len(resp);
  4132. start = 0;
  4133. requested_len = resp_len;
  4134. pos = os_strchr(pos, ' ');
  4135. if (pos) {
  4136. start = atoi(pos);
  4137. if (start > resp_len)
  4138. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4139. pos = os_strchr(pos, ',');
  4140. if (pos == NULL)
  4141. return -1;
  4142. pos++;
  4143. requested_len = atoi(pos);
  4144. if (start + requested_len > resp_len)
  4145. return os_snprintf(buf, buflen, "FAIL-Invalid range");
  4146. }
  4147. if (requested_len * 2 + 1 > buflen)
  4148. return os_snprintf(buf, buflen, "FAIL-Too long response");
  4149. ret = wpa_snprintf_hex(buf, buflen, wpabuf_head_u8(resp) + start,
  4150. requested_len);
  4151. if (start + requested_len == resp_len) {
  4152. /*
  4153. * Free memory by dropping the response after it has been
  4154. * fetched.
  4155. */
  4156. if (resp == wpa_s->prev_gas_resp) {
  4157. wpabuf_free(wpa_s->prev_gas_resp);
  4158. wpa_s->prev_gas_resp = NULL;
  4159. } else {
  4160. wpabuf_free(wpa_s->last_gas_resp);
  4161. wpa_s->last_gas_resp = NULL;
  4162. }
  4163. }
  4164. return ret;
  4165. }
  4166. #endif /* CONFIG_INTERWORKING */
  4167. #ifdef CONFIG_HS20
  4168. static int get_hs20_anqp(struct wpa_supplicant *wpa_s, char *dst)
  4169. {
  4170. u8 dst_addr[ETH_ALEN];
  4171. int used;
  4172. char *pos;
  4173. u32 subtypes = 0;
  4174. used = hwaddr_aton2(dst, dst_addr);
  4175. if (used < 0)
  4176. return -1;
  4177. pos = dst + used;
  4178. for (;;) {
  4179. int num = atoi(pos);
  4180. if (num <= 0 || num > 31)
  4181. return -1;
  4182. subtypes |= BIT(num);
  4183. pos = os_strchr(pos + 1, ',');
  4184. if (pos == NULL)
  4185. break;
  4186. pos++;
  4187. }
  4188. if (subtypes == 0)
  4189. return -1;
  4190. return hs20_anqp_send_req(wpa_s, dst_addr, subtypes, NULL, 0);
  4191. }
  4192. static int hs20_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4193. const u8 *addr, const char *realm)
  4194. {
  4195. u8 *buf;
  4196. size_t rlen, len;
  4197. int ret;
  4198. rlen = os_strlen(realm);
  4199. len = 3 + rlen;
  4200. buf = os_malloc(len);
  4201. if (buf == NULL)
  4202. return -1;
  4203. buf[0] = 1; /* NAI Home Realm Count */
  4204. buf[1] = 0; /* Formatted in accordance with RFC 4282 */
  4205. buf[2] = rlen;
  4206. os_memcpy(buf + 3, realm, rlen);
  4207. ret = hs20_anqp_send_req(wpa_s, addr,
  4208. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4209. buf, len);
  4210. os_free(buf);
  4211. return ret;
  4212. }
  4213. static int hs20_get_nai_home_realm_list(struct wpa_supplicant *wpa_s,
  4214. char *dst)
  4215. {
  4216. struct wpa_cred *cred = wpa_s->conf->cred;
  4217. u8 dst_addr[ETH_ALEN];
  4218. int used;
  4219. u8 *buf;
  4220. size_t len;
  4221. int ret;
  4222. used = hwaddr_aton2(dst, dst_addr);
  4223. if (used < 0)
  4224. return -1;
  4225. while (dst[used] == ' ')
  4226. used++;
  4227. if (os_strncmp(dst + used, "realm=", 6) == 0)
  4228. return hs20_nai_home_realm_list(wpa_s, dst_addr,
  4229. dst + used + 6);
  4230. len = os_strlen(dst + used);
  4231. if (len == 0 && cred && cred->realm)
  4232. return hs20_nai_home_realm_list(wpa_s, dst_addr, cred->realm);
  4233. if (len % 1)
  4234. return -1;
  4235. len /= 2;
  4236. buf = os_malloc(len);
  4237. if (buf == NULL)
  4238. return -1;
  4239. if (hexstr2bin(dst + used, buf, len) < 0) {
  4240. os_free(buf);
  4241. return -1;
  4242. }
  4243. ret = hs20_anqp_send_req(wpa_s, dst_addr,
  4244. BIT(HS20_STYPE_NAI_HOME_REALM_QUERY),
  4245. buf, len);
  4246. os_free(buf);
  4247. return ret;
  4248. }
  4249. #endif /* CONFIG_HS20 */
  4250. static int wpa_supplicant_ctrl_iface_sta_autoconnect(
  4251. struct wpa_supplicant *wpa_s, char *cmd)
  4252. {
  4253. wpa_s->auto_reconnect_disabled = atoi(cmd) == 0 ? 1 : 0;
  4254. return 0;
  4255. }
  4256. #ifdef CONFIG_AUTOSCAN
  4257. static int wpa_supplicant_ctrl_iface_autoscan(struct wpa_supplicant *wpa_s,
  4258. char *cmd)
  4259. {
  4260. enum wpa_states state = wpa_s->wpa_state;
  4261. char *new_params = NULL;
  4262. if (os_strlen(cmd) > 0) {
  4263. new_params = os_strdup(cmd);
  4264. if (new_params == NULL)
  4265. return -1;
  4266. }
  4267. os_free(wpa_s->conf->autoscan);
  4268. wpa_s->conf->autoscan = new_params;
  4269. if (wpa_s->conf->autoscan == NULL)
  4270. autoscan_deinit(wpa_s);
  4271. else if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
  4272. autoscan_init(wpa_s, 1);
  4273. else if (state == WPA_SCANNING)
  4274. wpa_supplicant_reinit_autoscan(wpa_s);
  4275. return 0;
  4276. }
  4277. #endif /* CONFIG_AUTOSCAN */
  4278. #ifdef CONFIG_WNM
  4279. static int wpas_ctrl_iface_wnm_sleep(struct wpa_supplicant *wpa_s, char *cmd)
  4280. {
  4281. int enter;
  4282. int intval = 0;
  4283. char *pos;
  4284. int ret;
  4285. struct wpabuf *tfs_req = NULL;
  4286. if (os_strncmp(cmd, "enter", 5) == 0)
  4287. enter = 1;
  4288. else if (os_strncmp(cmd, "exit", 4) == 0)
  4289. enter = 0;
  4290. else
  4291. return -1;
  4292. pos = os_strstr(cmd, " interval=");
  4293. if (pos)
  4294. intval = atoi(pos + 10);
  4295. pos = os_strstr(cmd, " tfs_req=");
  4296. if (pos) {
  4297. char *end;
  4298. size_t len;
  4299. pos += 9;
  4300. end = os_strchr(pos, ' ');
  4301. if (end)
  4302. len = end - pos;
  4303. else
  4304. len = os_strlen(pos);
  4305. if (len & 1)
  4306. return -1;
  4307. len /= 2;
  4308. tfs_req = wpabuf_alloc(len);
  4309. if (tfs_req == NULL)
  4310. return -1;
  4311. if (hexstr2bin(pos, wpabuf_put(tfs_req, len), len) < 0) {
  4312. wpabuf_free(tfs_req);
  4313. return -1;
  4314. }
  4315. }
  4316. ret = ieee802_11_send_wnmsleep_req(wpa_s, enter ? WNM_SLEEP_MODE_ENTER :
  4317. WNM_SLEEP_MODE_EXIT, intval,
  4318. tfs_req);
  4319. wpabuf_free(tfs_req);
  4320. return ret;
  4321. }
  4322. static int wpas_ctrl_iface_wnm_bss_query(struct wpa_supplicant *wpa_s, char *cmd)
  4323. {
  4324. int query_reason;
  4325. query_reason = atoi(cmd);
  4326. wpa_printf(MSG_DEBUG, "CTRL_IFACE: WNM_BSS_QUERY query_reason=%d",
  4327. query_reason);
  4328. return wnm_send_bss_transition_mgmt_query(wpa_s, query_reason);
  4329. }
  4330. #endif /* CONFIG_WNM */
  4331. /* Get string representation of channel width */
  4332. static const char * channel_width_name(enum chan_width width)
  4333. {
  4334. switch (width) {
  4335. case CHAN_WIDTH_20_NOHT:
  4336. return "20 MHz (no HT)";
  4337. case CHAN_WIDTH_20:
  4338. return "20 MHz";
  4339. case CHAN_WIDTH_40:
  4340. return "40 MHz";
  4341. case CHAN_WIDTH_80:
  4342. return "80 MHz";
  4343. case CHAN_WIDTH_80P80:
  4344. return "80+80 MHz";
  4345. case CHAN_WIDTH_160:
  4346. return "160 MHz";
  4347. default:
  4348. return "unknown";
  4349. }
  4350. }
  4351. static int wpa_supplicant_signal_poll(struct wpa_supplicant *wpa_s, char *buf,
  4352. size_t buflen)
  4353. {
  4354. struct wpa_signal_info si;
  4355. int ret;
  4356. char *pos, *end;
  4357. ret = wpa_drv_signal_poll(wpa_s, &si);
  4358. if (ret)
  4359. return -1;
  4360. pos = buf;
  4361. end = buf + buflen;
  4362. ret = os_snprintf(pos, end - pos, "RSSI=%d\nLINKSPEED=%d\n"
  4363. "NOISE=%d\nFREQUENCY=%u\n",
  4364. si.current_signal, si.current_txrate / 1000,
  4365. si.current_noise, si.frequency);
  4366. if (ret < 0 || ret > end - pos)
  4367. return -1;
  4368. pos += ret;
  4369. if (si.chanwidth != CHAN_WIDTH_UNKNOWN) {
  4370. ret = os_snprintf(pos, end - pos, "WIDTH=%s\n",
  4371. channel_width_name(si.chanwidth));
  4372. if (ret < 0 || ret > end - pos)
  4373. return -1;
  4374. pos += ret;
  4375. }
  4376. if (si.center_frq1 > 0 && si.center_frq2 > 0) {
  4377. ret = os_snprintf(pos, end - pos,
  4378. "CENTER_FRQ1=%d\nCENTER_FRQ2=%d\n",
  4379. si.center_frq1, si.center_frq2);
  4380. if (ret < 0 || ret > end - pos)
  4381. return -1;
  4382. pos += ret;
  4383. }
  4384. if (si.avg_signal) {
  4385. ret = os_snprintf(pos, end - pos,
  4386. "AVG_RSSI=%d\n", si.avg_signal);
  4387. if (ret < 0 || ret >= end - pos)
  4388. return -1;
  4389. pos += ret;
  4390. }
  4391. return pos - buf;
  4392. }
  4393. static int wpa_supplicant_pktcnt_poll(struct wpa_supplicant *wpa_s, char *buf,
  4394. size_t buflen)
  4395. {
  4396. struct hostap_sta_driver_data sta;
  4397. int ret;
  4398. ret = wpa_drv_pktcnt_poll(wpa_s, &sta);
  4399. if (ret)
  4400. return -1;
  4401. ret = os_snprintf(buf, buflen, "TXGOOD=%lu\nTXBAD=%lu\nRXGOOD=%lu\n",
  4402. sta.tx_packets, sta.tx_retry_failed, sta.rx_packets);
  4403. if (ret < 0 || (size_t) ret > buflen)
  4404. return -1;
  4405. return ret;
  4406. }
  4407. #ifdef ANDROID
  4408. static int wpa_supplicant_driver_cmd(struct wpa_supplicant *wpa_s, char *cmd,
  4409. char *buf, size_t buflen)
  4410. {
  4411. int ret;
  4412. ret = wpa_drv_driver_cmd(wpa_s, cmd, buf, buflen);
  4413. if (ret == 0) {
  4414. if (os_strncasecmp(cmd, "COUNTRY", 7) == 0) {
  4415. struct p2p_data *p2p = wpa_s->global->p2p;
  4416. if (p2p) {
  4417. char country[3];
  4418. country[0] = cmd[8];
  4419. country[1] = cmd[9];
  4420. country[2] = 0x04;
  4421. p2p_set_country(p2p, country);
  4422. }
  4423. }
  4424. ret = os_snprintf(buf, buflen, "%s\n", "OK");
  4425. }
  4426. return ret;
  4427. }
  4428. #endif /* ANDROID */
  4429. static void wpa_supplicant_ctrl_iface_flush(struct wpa_supplicant *wpa_s)
  4430. {
  4431. wpa_dbg(wpa_s, MSG_DEBUG, "Flush all wpa_supplicant state");
  4432. #ifdef CONFIG_P2P
  4433. wpas_p2p_stop_find(wpa_s);
  4434. p2p_ctrl_flush(wpa_s);
  4435. wpas_p2p_group_remove(wpa_s, "*");
  4436. wpas_p2p_service_flush(wpa_s);
  4437. wpa_s->global->p2p_disabled = 0;
  4438. wpa_s->global->p2p_per_sta_psk = 0;
  4439. wpa_s->conf->num_sec_device_types = 0;
  4440. #endif /* CONFIG_P2P */
  4441. #ifdef CONFIG_WPS_TESTING
  4442. wps_version_number = 0x20;
  4443. wps_testing_dummy_cred = 0;
  4444. #endif /* CONFIG_WPS_TESTING */
  4445. #ifdef CONFIG_WPS
  4446. wpa_s->wps_fragment_size = 0;
  4447. wpas_wps_cancel(wpa_s);
  4448. #endif /* CONFIG_WPS */
  4449. wpa_s->after_wps = 0;
  4450. wpa_s->known_wps_freq = 0;
  4451. #ifdef CONFIG_TDLS
  4452. #ifdef CONFIG_TDLS_TESTING
  4453. extern unsigned int tdls_testing;
  4454. tdls_testing = 0;
  4455. #endif /* CONFIG_TDLS_TESTING */
  4456. wpa_drv_tdls_oper(wpa_s, TDLS_ENABLE, NULL);
  4457. wpa_tdls_enable(wpa_s->wpa, 1);
  4458. #endif /* CONFIG_TDLS */
  4459. eloop_cancel_timeout(wpa_supplicant_stop_countermeasures, wpa_s, NULL);
  4460. wpa_supplicant_stop_countermeasures(wpa_s, NULL);
  4461. wpa_s->no_keep_alive = 0;
  4462. os_free(wpa_s->disallow_aps_bssid);
  4463. wpa_s->disallow_aps_bssid = NULL;
  4464. wpa_s->disallow_aps_bssid_count = 0;
  4465. os_free(wpa_s->disallow_aps_ssid);
  4466. wpa_s->disallow_aps_ssid = NULL;
  4467. wpa_s->disallow_aps_ssid_count = 0;
  4468. wpa_s->set_sta_uapsd = 0;
  4469. wpa_s->sta_uapsd = 0;
  4470. wpa_drv_radio_disable(wpa_s, 0);
  4471. wpa_bss_flush(wpa_s);
  4472. wpa_blacklist_clear(wpa_s);
  4473. wpa_s->extra_blacklist_count = 0;
  4474. wpa_supplicant_ctrl_iface_remove_network(wpa_s, "all");
  4475. wpa_supplicant_ctrl_iface_remove_cred(wpa_s, "all");
  4476. wpa_config_flush_blobs(wpa_s->conf);
  4477. wpa_s->conf->auto_interworking = 0;
  4478. wpa_s->conf->okc = 0;
  4479. wpa_s->conf->pmf = 0;
  4480. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_LIFETIME, 43200);
  4481. wpa_sm_set_param(wpa_s->wpa, RSNA_PMK_REAUTH_THRESHOLD, 70);
  4482. wpa_sm_set_param(wpa_s->wpa, RSNA_SA_TIMEOUT, 60);
  4483. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4484. radio_remove_unstarted_work(wpa_s, NULL);
  4485. }
  4486. static int wpas_ctrl_radio_work_show(struct wpa_supplicant *wpa_s,
  4487. char *buf, size_t buflen)
  4488. {
  4489. struct wpa_radio_work *work;
  4490. char *pos, *end;
  4491. struct os_reltime now, diff;
  4492. pos = buf;
  4493. end = buf + buflen;
  4494. os_get_reltime(&now);
  4495. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4496. {
  4497. int ret;
  4498. os_reltime_sub(&now, &work->time, &diff);
  4499. ret = os_snprintf(pos, end - pos, "%s@%s:%u:%u:%ld.%06ld\n",
  4500. work->type, work->wpa_s->ifname, work->freq,
  4501. work->started, diff.sec, diff.usec);
  4502. if (ret < 0 || ret >= end - pos)
  4503. break;
  4504. pos += ret;
  4505. }
  4506. return pos - buf;
  4507. }
  4508. static void wpas_ctrl_radio_work_timeout(void *eloop_ctx, void *timeout_ctx)
  4509. {
  4510. struct wpa_radio_work *work = eloop_ctx;
  4511. struct wpa_external_work *ework = work->ctx;
  4512. wpa_dbg(work->wpa_s, MSG_DEBUG,
  4513. "Timing out external radio work %u (%s)",
  4514. ework->id, work->type);
  4515. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_TIMEOUT "%u", ework->id);
  4516. os_free(ework);
  4517. radio_work_done(work);
  4518. }
  4519. static void wpas_ctrl_radio_work_cb(struct wpa_radio_work *work, int deinit)
  4520. {
  4521. struct wpa_external_work *ework = work->ctx;
  4522. if (deinit) {
  4523. os_free(ework);
  4524. return;
  4525. }
  4526. wpa_dbg(work->wpa_s, MSG_DEBUG, "Starting external radio work %u (%s)",
  4527. ework->id, ework->type);
  4528. wpa_msg(work->wpa_s, MSG_INFO, EXT_RADIO_WORK_START "%u", ework->id);
  4529. if (!ework->timeout)
  4530. ework->timeout = 10;
  4531. eloop_register_timeout(ework->timeout, 0, wpas_ctrl_radio_work_timeout,
  4532. work, NULL);
  4533. }
  4534. static int wpas_ctrl_radio_work_add(struct wpa_supplicant *wpa_s, char *cmd,
  4535. char *buf, size_t buflen)
  4536. {
  4537. struct wpa_external_work *ework;
  4538. char *pos, *pos2;
  4539. size_t type_len;
  4540. int ret;
  4541. unsigned int freq = 0;
  4542. /* format: <name> [freq=<MHz>] [timeout=<seconds>] */
  4543. ework = os_zalloc(sizeof(*ework));
  4544. if (ework == NULL)
  4545. return -1;
  4546. pos = os_strchr(cmd, ' ');
  4547. if (pos) {
  4548. type_len = pos - cmd;
  4549. pos++;
  4550. pos2 = os_strstr(pos, "freq=");
  4551. if (pos2)
  4552. freq = atoi(pos2 + 5);
  4553. pos2 = os_strstr(pos, "timeout=");
  4554. if (pos2)
  4555. ework->timeout = atoi(pos2 + 8);
  4556. } else {
  4557. type_len = os_strlen(cmd);
  4558. }
  4559. if (4 + type_len >= sizeof(ework->type))
  4560. type_len = sizeof(ework->type) - 4 - 1;
  4561. os_strlcpy(ework->type, "ext:", sizeof(ework->type));
  4562. os_memcpy(ework->type + 4, cmd, type_len);
  4563. ework->type[4 + type_len] = '\0';
  4564. wpa_s->ext_work_id++;
  4565. if (wpa_s->ext_work_id == 0)
  4566. wpa_s->ext_work_id++;
  4567. ework->id = wpa_s->ext_work_id;
  4568. if (radio_add_work(wpa_s, freq, ework->type, 0, wpas_ctrl_radio_work_cb,
  4569. ework) < 0) {
  4570. os_free(ework);
  4571. return -1;
  4572. }
  4573. ret = os_snprintf(buf, buflen, "%u", ework->id);
  4574. if (ret < 0 || (size_t) ret >= buflen)
  4575. return -1;
  4576. return ret;
  4577. }
  4578. static int wpas_ctrl_radio_work_done(struct wpa_supplicant *wpa_s, char *cmd)
  4579. {
  4580. struct wpa_radio_work *work;
  4581. unsigned int id = atoi(cmd);
  4582. dl_list_for_each(work, &wpa_s->radio->work, struct wpa_radio_work, list)
  4583. {
  4584. struct wpa_external_work *ework;
  4585. if (os_strncmp(work->type, "ext:", 4) != 0)
  4586. continue;
  4587. ework = work->ctx;
  4588. if (id && ework->id != id)
  4589. continue;
  4590. wpa_dbg(wpa_s, MSG_DEBUG,
  4591. "Completed external radio work %u (%s)",
  4592. ework->id, ework->type);
  4593. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout, work, NULL);
  4594. os_free(ework);
  4595. radio_work_done(work);
  4596. return 3; /* "OK\n" */
  4597. }
  4598. return -1;
  4599. }
  4600. static int wpas_ctrl_radio_work(struct wpa_supplicant *wpa_s, char *cmd,
  4601. char *buf, size_t buflen)
  4602. {
  4603. if (os_strcmp(cmd, "show") == 0)
  4604. return wpas_ctrl_radio_work_show(wpa_s, buf, buflen);
  4605. if (os_strncmp(cmd, "add ", 4) == 0)
  4606. return wpas_ctrl_radio_work_add(wpa_s, cmd + 4, buf, buflen);
  4607. if (os_strncmp(cmd, "done ", 5) == 0)
  4608. return wpas_ctrl_radio_work_done(wpa_s, cmd + 4);
  4609. return -1;
  4610. }
  4611. void wpas_ctrl_radio_work_flush(struct wpa_supplicant *wpa_s)
  4612. {
  4613. struct wpa_radio_work *work, *tmp;
  4614. if (!wpa_s || !wpa_s->radio)
  4615. return;
  4616. dl_list_for_each_safe(work, tmp, &wpa_s->radio->work,
  4617. struct wpa_radio_work, list) {
  4618. struct wpa_external_work *ework;
  4619. if (os_strncmp(work->type, "ext:", 4) != 0)
  4620. continue;
  4621. ework = work->ctx;
  4622. wpa_dbg(wpa_s, MSG_DEBUG,
  4623. "Flushing %sexternal radio work %u (%s)",
  4624. work->started ? " started" : "", ework->id,
  4625. ework->type);
  4626. if (work->started)
  4627. eloop_cancel_timeout(wpas_ctrl_radio_work_timeout,
  4628. work, NULL);
  4629. os_free(ework);
  4630. radio_work_done(work);
  4631. }
  4632. }
  4633. static void wpas_ctrl_eapol_response(void *eloop_ctx, void *timeout_ctx)
  4634. {
  4635. struct wpa_supplicant *wpa_s = eloop_ctx;
  4636. eapol_sm_notify_ctrl_response(wpa_s->eapol);
  4637. }
  4638. static int set_scan_freqs(struct wpa_supplicant *wpa_s, char *val)
  4639. {
  4640. int *freqs = NULL;
  4641. freqs = freq_range_to_channel_list(wpa_s, val);
  4642. if (freqs == NULL)
  4643. return -1;
  4644. os_free(wpa_s->manual_scan_freqs);
  4645. wpa_s->manual_scan_freqs = freqs;
  4646. return 0;
  4647. }
  4648. static void wpas_ctrl_scan(struct wpa_supplicant *wpa_s, char *params,
  4649. char *reply, int reply_size, int *reply_len)
  4650. {
  4651. char *pos;
  4652. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED) {
  4653. *reply_len = -1;
  4654. return;
  4655. }
  4656. wpa_s->manual_scan_passive = 0;
  4657. wpa_s->manual_scan_use_id = 0;
  4658. wpa_s->manual_scan_only_new = 0;
  4659. if (params) {
  4660. if (os_strncasecmp(params, "TYPE=ONLY", 9) == 0)
  4661. wpa_s->scan_res_handler = scan_only_handler;
  4662. pos = os_strstr(params, "freq=");
  4663. if (pos && set_scan_freqs(wpa_s, pos + 5) < 0) {
  4664. *reply_len = -1;
  4665. return;
  4666. }
  4667. pos = os_strstr(params, "passive=");
  4668. if (pos)
  4669. wpa_s->manual_scan_passive = !!atoi(pos + 8);
  4670. pos = os_strstr(params, "use_id=");
  4671. if (pos)
  4672. wpa_s->manual_scan_use_id = atoi(pos + 7);
  4673. pos = os_strstr(params, "only_new=1");
  4674. if (pos)
  4675. wpa_s->manual_scan_only_new = 1;
  4676. } else {
  4677. os_free(wpa_s->manual_scan_freqs);
  4678. wpa_s->manual_scan_freqs = NULL;
  4679. if (wpa_s->scan_res_handler == scan_only_handler)
  4680. wpa_s->scan_res_handler = NULL;
  4681. }
  4682. if (!wpa_s->sched_scanning && !wpa_s->scanning &&
  4683. ((wpa_s->wpa_state <= WPA_SCANNING) ||
  4684. (wpa_s->wpa_state == WPA_COMPLETED))) {
  4685. wpa_s->normal_scans = 0;
  4686. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4687. wpa_s->after_wps = 0;
  4688. wpa_s->known_wps_freq = 0;
  4689. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4690. if (wpa_s->manual_scan_use_id) {
  4691. wpa_s->manual_scan_id++;
  4692. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  4693. wpa_s->manual_scan_id);
  4694. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  4695. wpa_s->manual_scan_id);
  4696. }
  4697. } else if (wpa_s->sched_scanning) {
  4698. wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to allow requested full scan to proceed");
  4699. wpa_supplicant_cancel_sched_scan(wpa_s);
  4700. wpa_s->scan_req = MANUAL_SCAN_REQ;
  4701. wpa_supplicant_req_scan(wpa_s, 0, 0);
  4702. if (wpa_s->manual_scan_use_id) {
  4703. wpa_s->manual_scan_id++;
  4704. *reply_len = os_snprintf(reply, reply_size, "%u\n",
  4705. wpa_s->manual_scan_id);
  4706. wpa_dbg(wpa_s, MSG_DEBUG, "Assigned scan id %u",
  4707. wpa_s->manual_scan_id);
  4708. }
  4709. } else {
  4710. wpa_printf(MSG_DEBUG, "Ongoing scan action - reject new request");
  4711. *reply_len = os_snprintf(reply, reply_size, "FAIL-BUSY\n");
  4712. }
  4713. }
  4714. char * wpa_supplicant_ctrl_iface_process(struct wpa_supplicant *wpa_s,
  4715. char *buf, size_t *resp_len)
  4716. {
  4717. char *reply;
  4718. const int reply_size = 4096;
  4719. int reply_len;
  4720. if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0 ||
  4721. os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  4722. if (wpa_debug_show_keys)
  4723. wpa_dbg(wpa_s, MSG_DEBUG,
  4724. "Control interface command '%s'", buf);
  4725. else
  4726. wpa_dbg(wpa_s, MSG_DEBUG,
  4727. "Control interface command '%s [REMOVED]'",
  4728. os_strncmp(buf, WPA_CTRL_RSP,
  4729. os_strlen(WPA_CTRL_RSP)) == 0 ?
  4730. WPA_CTRL_RSP : "SET_NETWORK");
  4731. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ", 16) == 0 ||
  4732. os_strncmp(buf, "NFC_REPORT_HANDOVER", 19) == 0 ||
  4733. os_strncmp(buf, "NFC_RX_HANDOVER_SEL", 19) == 0) {
  4734. wpa_hexdump_ascii_key(MSG_DEBUG, "RX ctrl_iface",
  4735. (const u8 *) buf, os_strlen(buf));
  4736. } else {
  4737. int level = MSG_DEBUG;
  4738. if (os_strcmp(buf, "PING") == 0)
  4739. level = MSG_EXCESSIVE;
  4740. wpa_dbg(wpa_s, level, "Control interface command '%s'", buf);
  4741. }
  4742. reply = os_malloc(reply_size);
  4743. if (reply == NULL) {
  4744. *resp_len = 1;
  4745. return NULL;
  4746. }
  4747. os_memcpy(reply, "OK\n", 3);
  4748. reply_len = 3;
  4749. if (os_strcmp(buf, "PING") == 0) {
  4750. os_memcpy(reply, "PONG\n", 5);
  4751. reply_len = 5;
  4752. } else if (os_strcmp(buf, "IFNAME") == 0) {
  4753. reply_len = os_strlen(wpa_s->ifname);
  4754. os_memcpy(reply, wpa_s->ifname, reply_len);
  4755. } else if (os_strncmp(buf, "RELOG", 5) == 0) {
  4756. if (wpa_debug_reopen_file() < 0)
  4757. reply_len = -1;
  4758. } else if (os_strncmp(buf, "NOTE ", 5) == 0) {
  4759. wpa_printf(MSG_INFO, "NOTE: %s", buf + 5);
  4760. } else if (os_strcmp(buf, "MIB") == 0) {
  4761. reply_len = wpa_sm_get_mib(wpa_s->wpa, reply, reply_size);
  4762. if (reply_len >= 0) {
  4763. int res;
  4764. res = eapol_sm_get_mib(wpa_s->eapol, reply + reply_len,
  4765. reply_size - reply_len);
  4766. if (res < 0)
  4767. reply_len = -1;
  4768. else
  4769. reply_len += res;
  4770. }
  4771. } else if (os_strncmp(buf, "STATUS", 6) == 0) {
  4772. reply_len = wpa_supplicant_ctrl_iface_status(
  4773. wpa_s, buf + 6, reply, reply_size);
  4774. } else if (os_strcmp(buf, "PMKSA") == 0) {
  4775. reply_len = wpa_sm_pmksa_cache_list(wpa_s->wpa, reply,
  4776. reply_size);
  4777. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  4778. if (wpa_supplicant_ctrl_iface_set(wpa_s, buf + 4))
  4779. reply_len = -1;
  4780. } else if (os_strncmp(buf, "GET ", 4) == 0) {
  4781. reply_len = wpa_supplicant_ctrl_iface_get(wpa_s, buf + 4,
  4782. reply, reply_size);
  4783. } else if (os_strcmp(buf, "LOGON") == 0) {
  4784. eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
  4785. } else if (os_strcmp(buf, "LOGOFF") == 0) {
  4786. eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
  4787. } else if (os_strcmp(buf, "REASSOCIATE") == 0) {
  4788. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4789. reply_len = -1;
  4790. else
  4791. wpas_request_connection(wpa_s);
  4792. } else if (os_strcmp(buf, "RECONNECT") == 0) {
  4793. if (wpa_s->wpa_state == WPA_INTERFACE_DISABLED)
  4794. reply_len = -1;
  4795. else if (wpa_s->disconnected)
  4796. wpas_request_connection(wpa_s);
  4797. #ifdef IEEE8021X_EAPOL
  4798. } else if (os_strncmp(buf, "PREAUTH ", 8) == 0) {
  4799. if (wpa_supplicant_ctrl_iface_preauth(wpa_s, buf + 8))
  4800. reply_len = -1;
  4801. #endif /* IEEE8021X_EAPOL */
  4802. #ifdef CONFIG_PEERKEY
  4803. } else if (os_strncmp(buf, "STKSTART ", 9) == 0) {
  4804. if (wpa_supplicant_ctrl_iface_stkstart(wpa_s, buf + 9))
  4805. reply_len = -1;
  4806. #endif /* CONFIG_PEERKEY */
  4807. #ifdef CONFIG_IEEE80211R
  4808. } else if (os_strncmp(buf, "FT_DS ", 6) == 0) {
  4809. if (wpa_supplicant_ctrl_iface_ft_ds(wpa_s, buf + 6))
  4810. reply_len = -1;
  4811. #endif /* CONFIG_IEEE80211R */
  4812. #ifdef CONFIG_WPS
  4813. } else if (os_strcmp(buf, "WPS_PBC") == 0) {
  4814. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, NULL);
  4815. if (res == -2) {
  4816. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4817. reply_len = 17;
  4818. } else if (res)
  4819. reply_len = -1;
  4820. } else if (os_strncmp(buf, "WPS_PBC ", 8) == 0) {
  4821. int res = wpa_supplicant_ctrl_iface_wps_pbc(wpa_s, buf + 8);
  4822. if (res == -2) {
  4823. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4824. reply_len = 17;
  4825. } else if (res)
  4826. reply_len = -1;
  4827. } else if (os_strncmp(buf, "WPS_PIN ", 8) == 0) {
  4828. reply_len = wpa_supplicant_ctrl_iface_wps_pin(wpa_s, buf + 8,
  4829. reply,
  4830. reply_size);
  4831. } else if (os_strncmp(buf, "WPS_CHECK_PIN ", 14) == 0) {
  4832. reply_len = wpa_supplicant_ctrl_iface_wps_check_pin(
  4833. wpa_s, buf + 14, reply, reply_size);
  4834. } else if (os_strcmp(buf, "WPS_CANCEL") == 0) {
  4835. if (wpas_wps_cancel(wpa_s))
  4836. reply_len = -1;
  4837. #ifdef CONFIG_WPS_NFC
  4838. } else if (os_strcmp(buf, "WPS_NFC") == 0) {
  4839. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, NULL))
  4840. reply_len = -1;
  4841. } else if (os_strncmp(buf, "WPS_NFC ", 8) == 0) {
  4842. if (wpa_supplicant_ctrl_iface_wps_nfc(wpa_s, buf + 8))
  4843. reply_len = -1;
  4844. } else if (os_strncmp(buf, "WPS_NFC_CONFIG_TOKEN ", 21) == 0) {
  4845. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_config_token(
  4846. wpa_s, buf + 21, reply, reply_size);
  4847. } else if (os_strncmp(buf, "WPS_NFC_TOKEN ", 14) == 0) {
  4848. reply_len = wpa_supplicant_ctrl_iface_wps_nfc_token(
  4849. wpa_s, buf + 14, reply, reply_size);
  4850. } else if (os_strncmp(buf, "WPS_NFC_TAG_READ ", 17) == 0) {
  4851. if (wpa_supplicant_ctrl_iface_wps_nfc_tag_read(wpa_s,
  4852. buf + 17))
  4853. reply_len = -1;
  4854. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_REQ ", 21) == 0) {
  4855. reply_len = wpas_ctrl_nfc_get_handover_req(
  4856. wpa_s, buf + 21, reply, reply_size);
  4857. } else if (os_strncmp(buf, "NFC_GET_HANDOVER_SEL ", 21) == 0) {
  4858. reply_len = wpas_ctrl_nfc_get_handover_sel(
  4859. wpa_s, buf + 21, reply, reply_size);
  4860. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_REQ ", 20) == 0) {
  4861. reply_len = wpas_ctrl_nfc_rx_handover_req(
  4862. wpa_s, buf + 20, reply, reply_size);
  4863. } else if (os_strncmp(buf, "NFC_RX_HANDOVER_SEL ", 20) == 0) {
  4864. if (wpas_ctrl_nfc_rx_handover_sel(wpa_s, buf + 20))
  4865. reply_len = -1;
  4866. } else if (os_strncmp(buf, "NFC_REPORT_HANDOVER ", 20) == 0) {
  4867. if (wpas_ctrl_nfc_report_handover(wpa_s, buf + 20))
  4868. reply_len = -1;
  4869. #endif /* CONFIG_WPS_NFC */
  4870. } else if (os_strncmp(buf, "WPS_REG ", 8) == 0) {
  4871. if (wpa_supplicant_ctrl_iface_wps_reg(wpa_s, buf + 8))
  4872. reply_len = -1;
  4873. #ifdef CONFIG_AP
  4874. } else if (os_strncmp(buf, "WPS_AP_PIN ", 11) == 0) {
  4875. reply_len = wpa_supplicant_ctrl_iface_wps_ap_pin(
  4876. wpa_s, buf + 11, reply, reply_size);
  4877. #endif /* CONFIG_AP */
  4878. #ifdef CONFIG_WPS_ER
  4879. } else if (os_strcmp(buf, "WPS_ER_START") == 0) {
  4880. if (wpas_wps_er_start(wpa_s, NULL))
  4881. reply_len = -1;
  4882. } else if (os_strncmp(buf, "WPS_ER_START ", 13) == 0) {
  4883. if (wpas_wps_er_start(wpa_s, buf + 13))
  4884. reply_len = -1;
  4885. } else if (os_strcmp(buf, "WPS_ER_STOP") == 0) {
  4886. if (wpas_wps_er_stop(wpa_s))
  4887. reply_len = -1;
  4888. } else if (os_strncmp(buf, "WPS_ER_PIN ", 11) == 0) {
  4889. if (wpa_supplicant_ctrl_iface_wps_er_pin(wpa_s, buf + 11))
  4890. reply_len = -1;
  4891. } else if (os_strncmp(buf, "WPS_ER_PBC ", 11) == 0) {
  4892. int ret = wpas_wps_er_pbc(wpa_s, buf + 11);
  4893. if (ret == -2) {
  4894. os_memcpy(reply, "FAIL-PBC-OVERLAP\n", 17);
  4895. reply_len = 17;
  4896. } else if (ret == -3) {
  4897. os_memcpy(reply, "FAIL-UNKNOWN-UUID\n", 18);
  4898. reply_len = 18;
  4899. } else if (ret == -4) {
  4900. os_memcpy(reply, "FAIL-NO-AP-SETTINGS\n", 20);
  4901. reply_len = 20;
  4902. } else if (ret)
  4903. reply_len = -1;
  4904. } else if (os_strncmp(buf, "WPS_ER_LEARN ", 13) == 0) {
  4905. if (wpa_supplicant_ctrl_iface_wps_er_learn(wpa_s, buf + 13))
  4906. reply_len = -1;
  4907. } else if (os_strncmp(buf, "WPS_ER_SET_CONFIG ", 18) == 0) {
  4908. if (wpa_supplicant_ctrl_iface_wps_er_set_config(wpa_s,
  4909. buf + 18))
  4910. reply_len = -1;
  4911. } else if (os_strncmp(buf, "WPS_ER_CONFIG ", 14) == 0) {
  4912. if (wpa_supplicant_ctrl_iface_wps_er_config(wpa_s, buf + 14))
  4913. reply_len = -1;
  4914. #ifdef CONFIG_WPS_NFC
  4915. } else if (os_strncmp(buf, "WPS_ER_NFC_CONFIG_TOKEN ", 24) == 0) {
  4916. reply_len = wpa_supplicant_ctrl_iface_wps_er_nfc_config_token(
  4917. wpa_s, buf + 24, reply, reply_size);
  4918. #endif /* CONFIG_WPS_NFC */
  4919. #endif /* CONFIG_WPS_ER */
  4920. #endif /* CONFIG_WPS */
  4921. #ifdef CONFIG_IBSS_RSN
  4922. } else if (os_strncmp(buf, "IBSS_RSN ", 9) == 0) {
  4923. if (wpa_supplicant_ctrl_iface_ibss_rsn(wpa_s, buf + 9))
  4924. reply_len = -1;
  4925. #endif /* CONFIG_IBSS_RSN */
  4926. #ifdef CONFIG_P2P
  4927. } else if (os_strncmp(buf, "P2P_FIND ", 9) == 0) {
  4928. if (p2p_ctrl_find(wpa_s, buf + 9))
  4929. reply_len = -1;
  4930. } else if (os_strcmp(buf, "P2P_FIND") == 0) {
  4931. if (p2p_ctrl_find(wpa_s, ""))
  4932. reply_len = -1;
  4933. } else if (os_strcmp(buf, "P2P_STOP_FIND") == 0) {
  4934. wpas_p2p_stop_find(wpa_s);
  4935. } else if (os_strncmp(buf, "P2P_CONNECT ", 12) == 0) {
  4936. reply_len = p2p_ctrl_connect(wpa_s, buf + 12, reply,
  4937. reply_size);
  4938. } else if (os_strncmp(buf, "P2P_LISTEN ", 11) == 0) {
  4939. if (p2p_ctrl_listen(wpa_s, buf + 11))
  4940. reply_len = -1;
  4941. } else if (os_strcmp(buf, "P2P_LISTEN") == 0) {
  4942. if (p2p_ctrl_listen(wpa_s, ""))
  4943. reply_len = -1;
  4944. } else if (os_strncmp(buf, "P2P_GROUP_REMOVE ", 17) == 0) {
  4945. if (wpas_p2p_group_remove(wpa_s, buf + 17))
  4946. reply_len = -1;
  4947. } else if (os_strcmp(buf, "P2P_GROUP_ADD") == 0) {
  4948. if (wpas_p2p_group_add(wpa_s, 0, 0, 0, 0))
  4949. reply_len = -1;
  4950. } else if (os_strncmp(buf, "P2P_GROUP_ADD ", 14) == 0) {
  4951. if (p2p_ctrl_group_add(wpa_s, buf + 14))
  4952. reply_len = -1;
  4953. } else if (os_strncmp(buf, "P2P_PROV_DISC ", 14) == 0) {
  4954. if (p2p_ctrl_prov_disc(wpa_s, buf + 14))
  4955. reply_len = -1;
  4956. } else if (os_strcmp(buf, "P2P_GET_PASSPHRASE") == 0) {
  4957. reply_len = p2p_get_passphrase(wpa_s, reply, reply_size);
  4958. } else if (os_strncmp(buf, "P2P_SERV_DISC_REQ ", 18) == 0) {
  4959. reply_len = p2p_ctrl_serv_disc_req(wpa_s, buf + 18, reply,
  4960. reply_size);
  4961. } else if (os_strncmp(buf, "P2P_SERV_DISC_CANCEL_REQ ", 25) == 0) {
  4962. if (p2p_ctrl_serv_disc_cancel_req(wpa_s, buf + 25) < 0)
  4963. reply_len = -1;
  4964. } else if (os_strncmp(buf, "P2P_SERV_DISC_RESP ", 19) == 0) {
  4965. if (p2p_ctrl_serv_disc_resp(wpa_s, buf + 19) < 0)
  4966. reply_len = -1;
  4967. } else if (os_strcmp(buf, "P2P_SERVICE_UPDATE") == 0) {
  4968. wpas_p2p_sd_service_update(wpa_s);
  4969. } else if (os_strncmp(buf, "P2P_SERV_DISC_EXTERNAL ", 23) == 0) {
  4970. if (p2p_ctrl_serv_disc_external(wpa_s, buf + 23) < 0)
  4971. reply_len = -1;
  4972. } else if (os_strcmp(buf, "P2P_SERVICE_FLUSH") == 0) {
  4973. wpas_p2p_service_flush(wpa_s);
  4974. } else if (os_strncmp(buf, "P2P_SERVICE_ADD ", 16) == 0) {
  4975. if (p2p_ctrl_service_add(wpa_s, buf + 16) < 0)
  4976. reply_len = -1;
  4977. } else if (os_strncmp(buf, "P2P_SERVICE_DEL ", 16) == 0) {
  4978. if (p2p_ctrl_service_del(wpa_s, buf + 16) < 0)
  4979. reply_len = -1;
  4980. } else if (os_strncmp(buf, "P2P_REJECT ", 11) == 0) {
  4981. if (p2p_ctrl_reject(wpa_s, buf + 11) < 0)
  4982. reply_len = -1;
  4983. } else if (os_strncmp(buf, "P2P_INVITE ", 11) == 0) {
  4984. if (p2p_ctrl_invite(wpa_s, buf + 11) < 0)
  4985. reply_len = -1;
  4986. } else if (os_strncmp(buf, "P2P_PEER ", 9) == 0) {
  4987. reply_len = p2p_ctrl_peer(wpa_s, buf + 9, reply,
  4988. reply_size);
  4989. } else if (os_strncmp(buf, "P2P_SET ", 8) == 0) {
  4990. if (p2p_ctrl_set(wpa_s, buf + 8) < 0)
  4991. reply_len = -1;
  4992. } else if (os_strcmp(buf, "P2P_FLUSH") == 0) {
  4993. p2p_ctrl_flush(wpa_s);
  4994. } else if (os_strncmp(buf, "P2P_UNAUTHORIZE ", 16) == 0) {
  4995. if (wpas_p2p_unauthorize(wpa_s, buf + 16) < 0)
  4996. reply_len = -1;
  4997. } else if (os_strcmp(buf, "P2P_CANCEL") == 0) {
  4998. if (wpas_p2p_cancel(wpa_s))
  4999. reply_len = -1;
  5000. } else if (os_strncmp(buf, "P2P_PRESENCE_REQ ", 17) == 0) {
  5001. if (p2p_ctrl_presence_req(wpa_s, buf + 17) < 0)
  5002. reply_len = -1;
  5003. } else if (os_strcmp(buf, "P2P_PRESENCE_REQ") == 0) {
  5004. if (p2p_ctrl_presence_req(wpa_s, "") < 0)
  5005. reply_len = -1;
  5006. } else if (os_strncmp(buf, "P2P_EXT_LISTEN ", 15) == 0) {
  5007. if (p2p_ctrl_ext_listen(wpa_s, buf + 15) < 0)
  5008. reply_len = -1;
  5009. } else if (os_strcmp(buf, "P2P_EXT_LISTEN") == 0) {
  5010. if (p2p_ctrl_ext_listen(wpa_s, "") < 0)
  5011. reply_len = -1;
  5012. } else if (os_strncmp(buf, "P2P_REMOVE_CLIENT ", 18) == 0) {
  5013. if (p2p_ctrl_remove_client(wpa_s, buf + 18) < 0)
  5014. reply_len = -1;
  5015. #endif /* CONFIG_P2P */
  5016. #ifdef CONFIG_WIFI_DISPLAY
  5017. } else if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0) {
  5018. if (wifi_display_subelem_set(wpa_s->global, buf + 16) < 0)
  5019. reply_len = -1;
  5020. } else if (os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0) {
  5021. reply_len = wifi_display_subelem_get(wpa_s->global, buf + 16,
  5022. reply, reply_size);
  5023. #endif /* CONFIG_WIFI_DISPLAY */
  5024. #ifdef CONFIG_INTERWORKING
  5025. } else if (os_strcmp(buf, "FETCH_ANQP") == 0) {
  5026. if (interworking_fetch_anqp(wpa_s) < 0)
  5027. reply_len = -1;
  5028. } else if (os_strcmp(buf, "STOP_FETCH_ANQP") == 0) {
  5029. interworking_stop_fetch_anqp(wpa_s);
  5030. } else if (os_strcmp(buf, "INTERWORKING_SELECT") == 0) {
  5031. if (ctrl_interworking_select(wpa_s, NULL) < 0)
  5032. reply_len = -1;
  5033. } else if (os_strncmp(buf, "INTERWORKING_SELECT ", 20) == 0) {
  5034. if (ctrl_interworking_select(wpa_s, buf + 20) < 0)
  5035. reply_len = -1;
  5036. } else if (os_strncmp(buf, "INTERWORKING_CONNECT ", 21) == 0) {
  5037. if (ctrl_interworking_connect(wpa_s, buf + 21) < 0)
  5038. reply_len = -1;
  5039. } else if (os_strncmp(buf, "ANQP_GET ", 9) == 0) {
  5040. if (get_anqp(wpa_s, buf + 9) < 0)
  5041. reply_len = -1;
  5042. } else if (os_strncmp(buf, "GAS_REQUEST ", 12) == 0) {
  5043. if (gas_request(wpa_s, buf + 12) < 0)
  5044. reply_len = -1;
  5045. } else if (os_strncmp(buf, "GAS_RESPONSE_GET ", 17) == 0) {
  5046. reply_len = gas_response_get(wpa_s, buf + 17, reply,
  5047. reply_size);
  5048. #endif /* CONFIG_INTERWORKING */
  5049. #ifdef CONFIG_HS20
  5050. } else if (os_strncmp(buf, "HS20_ANQP_GET ", 14) == 0) {
  5051. if (get_hs20_anqp(wpa_s, buf + 14) < 0)
  5052. reply_len = -1;
  5053. } else if (os_strncmp(buf, "HS20_GET_NAI_HOME_REALM_LIST ", 29) == 0) {
  5054. if (hs20_get_nai_home_realm_list(wpa_s, buf + 29) < 0)
  5055. reply_len = -1;
  5056. #endif /* CONFIG_HS20 */
  5057. } else if (os_strncmp(buf, WPA_CTRL_RSP, os_strlen(WPA_CTRL_RSP)) == 0)
  5058. {
  5059. if (wpa_supplicant_ctrl_iface_ctrl_rsp(
  5060. wpa_s, buf + os_strlen(WPA_CTRL_RSP)))
  5061. reply_len = -1;
  5062. else {
  5063. /*
  5064. * Notify response from timeout to allow the control
  5065. * interface response to be sent first.
  5066. */
  5067. eloop_register_timeout(0, 0, wpas_ctrl_eapol_response,
  5068. wpa_s, NULL);
  5069. }
  5070. } else if (os_strcmp(buf, "RECONFIGURE") == 0) {
  5071. if (wpa_supplicant_reload_configuration(wpa_s))
  5072. reply_len = -1;
  5073. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5074. wpa_supplicant_terminate_proc(wpa_s->global);
  5075. } else if (os_strncmp(buf, "BSSID ", 6) == 0) {
  5076. if (wpa_supplicant_ctrl_iface_bssid(wpa_s, buf + 6))
  5077. reply_len = -1;
  5078. } else if (os_strncmp(buf, "BLACKLIST", 9) == 0) {
  5079. reply_len = wpa_supplicant_ctrl_iface_blacklist(
  5080. wpa_s, buf + 9, reply, reply_size);
  5081. } else if (os_strncmp(buf, "LOG_LEVEL", 9) == 0) {
  5082. reply_len = wpa_supplicant_ctrl_iface_log_level(
  5083. wpa_s, buf + 9, reply, reply_size);
  5084. } else if (os_strcmp(buf, "LIST_NETWORKS") == 0) {
  5085. reply_len = wpa_supplicant_ctrl_iface_list_networks(
  5086. wpa_s, reply, reply_size);
  5087. } else if (os_strcmp(buf, "DISCONNECT") == 0) {
  5088. #ifdef CONFIG_SME
  5089. wpa_s->sme.prev_bssid_set = 0;
  5090. #endif /* CONFIG_SME */
  5091. wpa_s->reassociate = 0;
  5092. wpa_s->disconnected = 1;
  5093. wpa_supplicant_cancel_sched_scan(wpa_s);
  5094. wpa_supplicant_cancel_scan(wpa_s);
  5095. wpa_supplicant_deauthenticate(wpa_s,
  5096. WLAN_REASON_DEAUTH_LEAVING);
  5097. } else if (os_strcmp(buf, "SCAN") == 0) {
  5098. wpas_ctrl_scan(wpa_s, NULL, reply, reply_size, &reply_len);
  5099. } else if (os_strncmp(buf, "SCAN ", 5) == 0) {
  5100. wpas_ctrl_scan(wpa_s, buf + 5, reply, reply_size, &reply_len);
  5101. } else if (os_strcmp(buf, "SCAN_RESULTS") == 0) {
  5102. reply_len = wpa_supplicant_ctrl_iface_scan_results(
  5103. wpa_s, reply, reply_size);
  5104. } else if (os_strncmp(buf, "SELECT_NETWORK ", 15) == 0) {
  5105. if (wpa_supplicant_ctrl_iface_select_network(wpa_s, buf + 15))
  5106. reply_len = -1;
  5107. } else if (os_strncmp(buf, "ENABLE_NETWORK ", 15) == 0) {
  5108. if (wpa_supplicant_ctrl_iface_enable_network(wpa_s, buf + 15))
  5109. reply_len = -1;
  5110. } else if (os_strncmp(buf, "DISABLE_NETWORK ", 16) == 0) {
  5111. if (wpa_supplicant_ctrl_iface_disable_network(wpa_s, buf + 16))
  5112. reply_len = -1;
  5113. } else if (os_strcmp(buf, "ADD_NETWORK") == 0) {
  5114. reply_len = wpa_supplicant_ctrl_iface_add_network(
  5115. wpa_s, reply, reply_size);
  5116. } else if (os_strncmp(buf, "REMOVE_NETWORK ", 15) == 0) {
  5117. if (wpa_supplicant_ctrl_iface_remove_network(wpa_s, buf + 15))
  5118. reply_len = -1;
  5119. } else if (os_strncmp(buf, "SET_NETWORK ", 12) == 0) {
  5120. if (wpa_supplicant_ctrl_iface_set_network(wpa_s, buf + 12))
  5121. reply_len = -1;
  5122. } else if (os_strncmp(buf, "GET_NETWORK ", 12) == 0) {
  5123. reply_len = wpa_supplicant_ctrl_iface_get_network(
  5124. wpa_s, buf + 12, reply, reply_size);
  5125. } else if (os_strcmp(buf, "LIST_CREDS") == 0) {
  5126. reply_len = wpa_supplicant_ctrl_iface_list_creds(
  5127. wpa_s, reply, reply_size);
  5128. } else if (os_strcmp(buf, "ADD_CRED") == 0) {
  5129. reply_len = wpa_supplicant_ctrl_iface_add_cred(
  5130. wpa_s, reply, reply_size);
  5131. } else if (os_strncmp(buf, "REMOVE_CRED ", 12) == 0) {
  5132. if (wpa_supplicant_ctrl_iface_remove_cred(wpa_s, buf + 12))
  5133. reply_len = -1;
  5134. } else if (os_strncmp(buf, "SET_CRED ", 9) == 0) {
  5135. if (wpa_supplicant_ctrl_iface_set_cred(wpa_s, buf + 9))
  5136. reply_len = -1;
  5137. #ifndef CONFIG_NO_CONFIG_WRITE
  5138. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5139. if (wpa_supplicant_ctrl_iface_save_config(wpa_s))
  5140. reply_len = -1;
  5141. #endif /* CONFIG_NO_CONFIG_WRITE */
  5142. } else if (os_strncmp(buf, "GET_CAPABILITY ", 15) == 0) {
  5143. reply_len = wpa_supplicant_ctrl_iface_get_capability(
  5144. wpa_s, buf + 15, reply, reply_size);
  5145. } else if (os_strncmp(buf, "AP_SCAN ", 8) == 0) {
  5146. if (wpa_supplicant_ctrl_iface_ap_scan(wpa_s, buf + 8))
  5147. reply_len = -1;
  5148. } else if (os_strncmp(buf, "SCAN_INTERVAL ", 14) == 0) {
  5149. if (wpa_supplicant_ctrl_iface_scan_interval(wpa_s, buf + 14))
  5150. reply_len = -1;
  5151. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5152. reply_len = wpa_supplicant_global_iface_list(
  5153. wpa_s->global, reply, reply_size);
  5154. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5155. reply_len = wpa_supplicant_global_iface_interfaces(
  5156. wpa_s->global, reply, reply_size);
  5157. } else if (os_strncmp(buf, "BSS ", 4) == 0) {
  5158. reply_len = wpa_supplicant_ctrl_iface_bss(
  5159. wpa_s, buf + 4, reply, reply_size);
  5160. #ifdef CONFIG_AP
  5161. } else if (os_strcmp(buf, "STA-FIRST") == 0) {
  5162. reply_len = ap_ctrl_iface_sta_first(wpa_s, reply, reply_size);
  5163. } else if (os_strncmp(buf, "STA ", 4) == 0) {
  5164. reply_len = ap_ctrl_iface_sta(wpa_s, buf + 4, reply,
  5165. reply_size);
  5166. } else if (os_strncmp(buf, "STA-NEXT ", 9) == 0) {
  5167. reply_len = ap_ctrl_iface_sta_next(wpa_s, buf + 9, reply,
  5168. reply_size);
  5169. } else if (os_strncmp(buf, "DEAUTHENTICATE ", 15) == 0) {
  5170. if (ap_ctrl_iface_sta_deauthenticate(wpa_s, buf + 15))
  5171. reply_len = -1;
  5172. } else if (os_strncmp(buf, "DISASSOCIATE ", 13) == 0) {
  5173. if (ap_ctrl_iface_sta_disassociate(wpa_s, buf + 13))
  5174. reply_len = -1;
  5175. } else if (os_strncmp(buf, "CHAN_SWITCH ", 12) == 0) {
  5176. if (ap_ctrl_iface_chanswitch(wpa_s, buf + 12))
  5177. reply_len = -1;
  5178. #endif /* CONFIG_AP */
  5179. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5180. wpas_notify_suspend(wpa_s->global);
  5181. } else if (os_strcmp(buf, "RESUME") == 0) {
  5182. wpas_notify_resume(wpa_s->global);
  5183. } else if (os_strcmp(buf, "DROP_SA") == 0) {
  5184. wpa_supplicant_ctrl_iface_drop_sa(wpa_s);
  5185. } else if (os_strncmp(buf, "ROAM ", 5) == 0) {
  5186. if (wpa_supplicant_ctrl_iface_roam(wpa_s, buf + 5))
  5187. reply_len = -1;
  5188. } else if (os_strncmp(buf, "STA_AUTOCONNECT ", 16) == 0) {
  5189. if (wpa_supplicant_ctrl_iface_sta_autoconnect(wpa_s, buf + 16))
  5190. reply_len = -1;
  5191. } else if (os_strncmp(buf, "BSS_EXPIRE_AGE ", 15) == 0) {
  5192. if (wpa_supplicant_ctrl_iface_bss_expire_age(wpa_s, buf + 15))
  5193. reply_len = -1;
  5194. } else if (os_strncmp(buf, "BSS_EXPIRE_COUNT ", 17) == 0) {
  5195. if (wpa_supplicant_ctrl_iface_bss_expire_count(wpa_s,
  5196. buf + 17))
  5197. reply_len = -1;
  5198. } else if (os_strncmp(buf, "BSS_FLUSH ", 10) == 0) {
  5199. if (wpa_supplicant_ctrl_iface_bss_flush(wpa_s, buf + 10))
  5200. reply_len = -1;
  5201. #ifdef CONFIG_TDLS
  5202. } else if (os_strncmp(buf, "TDLS_DISCOVER ", 14) == 0) {
  5203. if (wpa_supplicant_ctrl_iface_tdls_discover(wpa_s, buf + 14))
  5204. reply_len = -1;
  5205. } else if (os_strncmp(buf, "TDLS_SETUP ", 11) == 0) {
  5206. if (wpa_supplicant_ctrl_iface_tdls_setup(wpa_s, buf + 11))
  5207. reply_len = -1;
  5208. } else if (os_strncmp(buf, "TDLS_TEARDOWN ", 14) == 0) {
  5209. if (wpa_supplicant_ctrl_iface_tdls_teardown(wpa_s, buf + 14))
  5210. reply_len = -1;
  5211. #endif /* CONFIG_TDLS */
  5212. } else if (os_strncmp(buf, "SIGNAL_POLL", 11) == 0) {
  5213. reply_len = wpa_supplicant_signal_poll(wpa_s, reply,
  5214. reply_size);
  5215. } else if (os_strncmp(buf, "PKTCNT_POLL", 11) == 0) {
  5216. reply_len = wpa_supplicant_pktcnt_poll(wpa_s, reply,
  5217. reply_size);
  5218. #ifdef CONFIG_AUTOSCAN
  5219. } else if (os_strncmp(buf, "AUTOSCAN ", 9) == 0) {
  5220. if (wpa_supplicant_ctrl_iface_autoscan(wpa_s, buf + 9))
  5221. reply_len = -1;
  5222. #endif /* CONFIG_AUTOSCAN */
  5223. #ifdef ANDROID
  5224. } else if (os_strncmp(buf, "DRIVER ", 7) == 0) {
  5225. reply_len = wpa_supplicant_driver_cmd(wpa_s, buf + 7, reply,
  5226. reply_size);
  5227. #endif /* ANDROID */
  5228. } else if (os_strcmp(buf, "REAUTHENTICATE") == 0) {
  5229. pmksa_cache_clear_current(wpa_s->wpa);
  5230. eapol_sm_request_reauth(wpa_s->eapol);
  5231. #ifdef CONFIG_WNM
  5232. } else if (os_strncmp(buf, "WNM_SLEEP ", 10) == 0) {
  5233. if (wpas_ctrl_iface_wnm_sleep(wpa_s, buf + 10))
  5234. reply_len = -1;
  5235. } else if (os_strncmp(buf, "WNM_BSS_QUERY ", 10) == 0) {
  5236. if (wpas_ctrl_iface_wnm_bss_query(wpa_s, buf + 10))
  5237. reply_len = -1;
  5238. #endif /* CONFIG_WNM */
  5239. } else if (os_strcmp(buf, "FLUSH") == 0) {
  5240. wpa_supplicant_ctrl_iface_flush(wpa_s);
  5241. } else if (os_strncmp(buf, "RADIO_WORK ", 11) == 0) {
  5242. reply_len = wpas_ctrl_radio_work(wpa_s, buf + 11, reply,
  5243. reply_size);
  5244. } else {
  5245. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5246. reply_len = 16;
  5247. }
  5248. if (reply_len < 0) {
  5249. os_memcpy(reply, "FAIL\n", 5);
  5250. reply_len = 5;
  5251. }
  5252. *resp_len = reply_len;
  5253. return reply;
  5254. }
  5255. static int wpa_supplicant_global_iface_add(struct wpa_global *global,
  5256. char *cmd)
  5257. {
  5258. struct wpa_interface iface;
  5259. char *pos;
  5260. /*
  5261. * <ifname>TAB<confname>TAB<driver>TAB<ctrl_interface>TAB<driver_param>
  5262. * TAB<bridge_ifname>
  5263. */
  5264. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_ADD '%s'", cmd);
  5265. os_memset(&iface, 0, sizeof(iface));
  5266. do {
  5267. iface.ifname = pos = cmd;
  5268. pos = os_strchr(pos, '\t');
  5269. if (pos)
  5270. *pos++ = '\0';
  5271. if (iface.ifname[0] == '\0')
  5272. return -1;
  5273. if (pos == NULL)
  5274. break;
  5275. iface.confname = pos;
  5276. pos = os_strchr(pos, '\t');
  5277. if (pos)
  5278. *pos++ = '\0';
  5279. if (iface.confname[0] == '\0')
  5280. iface.confname = NULL;
  5281. if (pos == NULL)
  5282. break;
  5283. iface.driver = pos;
  5284. pos = os_strchr(pos, '\t');
  5285. if (pos)
  5286. *pos++ = '\0';
  5287. if (iface.driver[0] == '\0')
  5288. iface.driver = NULL;
  5289. if (pos == NULL)
  5290. break;
  5291. iface.ctrl_interface = pos;
  5292. pos = os_strchr(pos, '\t');
  5293. if (pos)
  5294. *pos++ = '\0';
  5295. if (iface.ctrl_interface[0] == '\0')
  5296. iface.ctrl_interface = NULL;
  5297. if (pos == NULL)
  5298. break;
  5299. iface.driver_param = pos;
  5300. pos = os_strchr(pos, '\t');
  5301. if (pos)
  5302. *pos++ = '\0';
  5303. if (iface.driver_param[0] == '\0')
  5304. iface.driver_param = NULL;
  5305. if (pos == NULL)
  5306. break;
  5307. iface.bridge_ifname = pos;
  5308. pos = os_strchr(pos, '\t');
  5309. if (pos)
  5310. *pos++ = '\0';
  5311. if (iface.bridge_ifname[0] == '\0')
  5312. iface.bridge_ifname = NULL;
  5313. if (pos == NULL)
  5314. break;
  5315. } while (0);
  5316. if (wpa_supplicant_get_iface(global, iface.ifname))
  5317. return -1;
  5318. return wpa_supplicant_add_iface(global, &iface) ? 0 : -1;
  5319. }
  5320. static int wpa_supplicant_global_iface_remove(struct wpa_global *global,
  5321. char *cmd)
  5322. {
  5323. struct wpa_supplicant *wpa_s;
  5324. wpa_printf(MSG_DEBUG, "CTRL_IFACE GLOBAL INTERFACE_REMOVE '%s'", cmd);
  5325. wpa_s = wpa_supplicant_get_iface(global, cmd);
  5326. if (wpa_s == NULL)
  5327. return -1;
  5328. return wpa_supplicant_remove_iface(global, wpa_s, 0);
  5329. }
  5330. static void wpa_free_iface_info(struct wpa_interface_info *iface)
  5331. {
  5332. struct wpa_interface_info *prev;
  5333. while (iface) {
  5334. prev = iface;
  5335. iface = iface->next;
  5336. os_free(prev->ifname);
  5337. os_free(prev->desc);
  5338. os_free(prev);
  5339. }
  5340. }
  5341. static int wpa_supplicant_global_iface_list(struct wpa_global *global,
  5342. char *buf, int len)
  5343. {
  5344. int i, res;
  5345. struct wpa_interface_info *iface = NULL, *last = NULL, *tmp;
  5346. char *pos, *end;
  5347. for (i = 0; wpa_drivers[i]; i++) {
  5348. struct wpa_driver_ops *drv = wpa_drivers[i];
  5349. if (drv->get_interfaces == NULL)
  5350. continue;
  5351. tmp = drv->get_interfaces(global->drv_priv[i]);
  5352. if (tmp == NULL)
  5353. continue;
  5354. if (last == NULL)
  5355. iface = last = tmp;
  5356. else
  5357. last->next = tmp;
  5358. while (last->next)
  5359. last = last->next;
  5360. }
  5361. pos = buf;
  5362. end = buf + len;
  5363. for (tmp = iface; tmp; tmp = tmp->next) {
  5364. res = os_snprintf(pos, end - pos, "%s\t%s\t%s\n",
  5365. tmp->drv_name, tmp->ifname,
  5366. tmp->desc ? tmp->desc : "");
  5367. if (res < 0 || res >= end - pos) {
  5368. *pos = '\0';
  5369. break;
  5370. }
  5371. pos += res;
  5372. }
  5373. wpa_free_iface_info(iface);
  5374. return pos - buf;
  5375. }
  5376. static int wpa_supplicant_global_iface_interfaces(struct wpa_global *global,
  5377. char *buf, int len)
  5378. {
  5379. int res;
  5380. char *pos, *end;
  5381. struct wpa_supplicant *wpa_s;
  5382. wpa_s = global->ifaces;
  5383. pos = buf;
  5384. end = buf + len;
  5385. while (wpa_s) {
  5386. res = os_snprintf(pos, end - pos, "%s\n", wpa_s->ifname);
  5387. if (res < 0 || res >= end - pos) {
  5388. *pos = '\0';
  5389. break;
  5390. }
  5391. pos += res;
  5392. wpa_s = wpa_s->next;
  5393. }
  5394. return pos - buf;
  5395. }
  5396. static char * wpas_global_ctrl_iface_ifname(struct wpa_global *global,
  5397. const char *ifname,
  5398. char *cmd, size_t *resp_len)
  5399. {
  5400. struct wpa_supplicant *wpa_s;
  5401. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5402. if (os_strcmp(ifname, wpa_s->ifname) == 0)
  5403. break;
  5404. }
  5405. if (wpa_s == NULL) {
  5406. char *resp = os_strdup("FAIL-NO-IFNAME-MATCH\n");
  5407. if (resp)
  5408. *resp_len = os_strlen(resp);
  5409. else
  5410. *resp_len = 1;
  5411. return resp;
  5412. }
  5413. return wpa_supplicant_ctrl_iface_process(wpa_s, cmd, resp_len);
  5414. }
  5415. static char * wpas_global_ctrl_iface_redir_p2p(struct wpa_global *global,
  5416. char *buf, size_t *resp_len)
  5417. {
  5418. #ifdef CONFIG_P2P
  5419. static const char * cmd[] = {
  5420. "LIST_NETWORKS",
  5421. "SAVE_CONFIG",
  5422. "P2P_FIND",
  5423. "P2P_STOP_FIND",
  5424. "P2P_LISTEN",
  5425. "P2P_GROUP_ADD",
  5426. "P2P_GET_PASSPHRASE",
  5427. "P2P_SERVICE_UPDATE",
  5428. "P2P_SERVICE_FLUSH",
  5429. "P2P_FLUSH",
  5430. "P2P_CANCEL",
  5431. "P2P_PRESENCE_REQ",
  5432. "P2P_EXT_LISTEN",
  5433. NULL
  5434. };
  5435. static const char * prefix[] = {
  5436. #ifdef ANDROID
  5437. "DRIVER ",
  5438. #endif /* ANDROID */
  5439. "GET_NETWORK ",
  5440. "REMOVE_NETWORK ",
  5441. "SET ",
  5442. "P2P_FIND ",
  5443. "P2P_CONNECT ",
  5444. "P2P_LISTEN ",
  5445. "P2P_GROUP_REMOVE ",
  5446. "P2P_GROUP_ADD ",
  5447. "P2P_PROV_DISC ",
  5448. "P2P_SERV_DISC_REQ ",
  5449. "P2P_SERV_DISC_CANCEL_REQ ",
  5450. "P2P_SERV_DISC_RESP ",
  5451. "P2P_SERV_DISC_EXTERNAL ",
  5452. "P2P_SERVICE_ADD ",
  5453. "P2P_SERVICE_DEL ",
  5454. "P2P_REJECT ",
  5455. "P2P_INVITE ",
  5456. "P2P_PEER ",
  5457. "P2P_SET ",
  5458. "P2P_UNAUTHORIZE ",
  5459. "P2P_PRESENCE_REQ ",
  5460. "P2P_EXT_LISTEN ",
  5461. "P2P_REMOVE_CLIENT ",
  5462. NULL
  5463. };
  5464. int found = 0;
  5465. int i;
  5466. if (global->p2p_init_wpa_s == NULL)
  5467. return NULL;
  5468. for (i = 0; !found && cmd[i]; i++) {
  5469. if (os_strcmp(buf, cmd[i]) == 0)
  5470. found = 1;
  5471. }
  5472. for (i = 0; !found && prefix[i]; i++) {
  5473. if (os_strncmp(buf, prefix[i], os_strlen(prefix[i])) == 0)
  5474. found = 1;
  5475. }
  5476. if (found)
  5477. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5478. buf, resp_len);
  5479. #endif /* CONFIG_P2P */
  5480. return NULL;
  5481. }
  5482. static char * wpas_global_ctrl_iface_redir_wfd(struct wpa_global *global,
  5483. char *buf, size_t *resp_len)
  5484. {
  5485. #ifdef CONFIG_WIFI_DISPLAY
  5486. if (global->p2p_init_wpa_s == NULL)
  5487. return NULL;
  5488. if (os_strncmp(buf, "WFD_SUBELEM_SET ", 16) == 0 ||
  5489. os_strncmp(buf, "WFD_SUBELEM_GET ", 16) == 0)
  5490. return wpa_supplicant_ctrl_iface_process(global->p2p_init_wpa_s,
  5491. buf, resp_len);
  5492. #endif /* CONFIG_WIFI_DISPLAY */
  5493. return NULL;
  5494. }
  5495. static char * wpas_global_ctrl_iface_redir(struct wpa_global *global,
  5496. char *buf, size_t *resp_len)
  5497. {
  5498. char *ret;
  5499. ret = wpas_global_ctrl_iface_redir_p2p(global, buf, resp_len);
  5500. if (ret)
  5501. return ret;
  5502. ret = wpas_global_ctrl_iface_redir_wfd(global, buf, resp_len);
  5503. if (ret)
  5504. return ret;
  5505. return NULL;
  5506. }
  5507. static int wpas_global_ctrl_iface_set(struct wpa_global *global, char *cmd)
  5508. {
  5509. char *value;
  5510. value = os_strchr(cmd, ' ');
  5511. if (value == NULL)
  5512. return -1;
  5513. *value++ = '\0';
  5514. wpa_printf(MSG_DEBUG, "GLOBAL_CTRL_IFACE SET '%s'='%s'", cmd, value);
  5515. #ifdef CONFIG_WIFI_DISPLAY
  5516. if (os_strcasecmp(cmd, "wifi_display") == 0) {
  5517. wifi_display_enable(global, !!atoi(value));
  5518. return 0;
  5519. }
  5520. #endif /* CONFIG_WIFI_DISPLAY */
  5521. return -1;
  5522. }
  5523. #ifndef CONFIG_NO_CONFIG_WRITE
  5524. static int wpas_global_ctrl_iface_save_config(struct wpa_global *global)
  5525. {
  5526. int ret = 0;
  5527. struct wpa_supplicant *wpa_s;
  5528. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5529. if (!wpa_s->conf->update_config) {
  5530. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Not allowed to update configuration (update_config=0)");
  5531. continue;
  5532. }
  5533. if (wpa_config_write(wpa_s->confname, wpa_s->conf)) {
  5534. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Failed to update configuration");
  5535. ret = 1;
  5536. } else {
  5537. wpa_dbg(wpa_s, MSG_DEBUG, "CTRL_IFACE: SAVE_CONFIG - Configuration updated");
  5538. }
  5539. }
  5540. return ret;
  5541. }
  5542. #endif /* CONFIG_NO_CONFIG_WRITE */
  5543. static int wpas_global_ctrl_iface_status(struct wpa_global *global,
  5544. char *buf, size_t buflen)
  5545. {
  5546. char *pos, *end;
  5547. int ret;
  5548. struct wpa_supplicant *wpa_s;
  5549. pos = buf;
  5550. end = buf + buflen;
  5551. #ifdef CONFIG_P2P
  5552. if (global->p2p && !global->p2p_disabled) {
  5553. ret = os_snprintf(pos, end - pos, "p2p_device_address=" MACSTR
  5554. "\n"
  5555. "p2p_state=%s\n",
  5556. MAC2STR(global->p2p_dev_addr),
  5557. p2p_get_state_txt(global->p2p));
  5558. if (ret < 0 || ret >= end - pos)
  5559. return pos - buf;
  5560. pos += ret;
  5561. } else if (global->p2p) {
  5562. ret = os_snprintf(pos, end - pos, "p2p_state=DISABLED\n");
  5563. if (ret < 0 || ret >= end - pos)
  5564. return pos - buf;
  5565. pos += ret;
  5566. }
  5567. #endif /* CONFIG_P2P */
  5568. #ifdef CONFIG_WIFI_DISPLAY
  5569. ret = os_snprintf(pos, end - pos, "wifi_display=%d\n",
  5570. !!global->wifi_display);
  5571. if (ret < 0 || ret >= end - pos)
  5572. return pos - buf;
  5573. pos += ret;
  5574. #endif /* CONFIG_WIFI_DISPLAY */
  5575. for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
  5576. ret = os_snprintf(pos, end - pos, "ifname=%s\n"
  5577. "address=" MACSTR "\n",
  5578. wpa_s->ifname, MAC2STR(wpa_s->own_addr));
  5579. if (ret < 0 || ret >= end - pos)
  5580. return pos - buf;
  5581. pos += ret;
  5582. }
  5583. return pos - buf;
  5584. }
  5585. char * wpa_supplicant_global_ctrl_iface_process(struct wpa_global *global,
  5586. char *buf, size_t *resp_len)
  5587. {
  5588. char *reply;
  5589. const int reply_size = 2048;
  5590. int reply_len;
  5591. int level = MSG_DEBUG;
  5592. if (os_strncmp(buf, "IFNAME=", 7) == 0) {
  5593. char *pos = os_strchr(buf + 7, ' ');
  5594. if (pos) {
  5595. *pos++ = '\0';
  5596. return wpas_global_ctrl_iface_ifname(global,
  5597. buf + 7, pos,
  5598. resp_len);
  5599. }
  5600. }
  5601. reply = wpas_global_ctrl_iface_redir(global, buf, resp_len);
  5602. if (reply)
  5603. return reply;
  5604. if (os_strcmp(buf, "PING") == 0)
  5605. level = MSG_EXCESSIVE;
  5606. wpa_hexdump_ascii(level, "RX global ctrl_iface",
  5607. (const u8 *) buf, os_strlen(buf));
  5608. reply = os_malloc(reply_size);
  5609. if (reply == NULL) {
  5610. *resp_len = 1;
  5611. return NULL;
  5612. }
  5613. os_memcpy(reply, "OK\n", 3);
  5614. reply_len = 3;
  5615. if (os_strcmp(buf, "PING") == 0) {
  5616. os_memcpy(reply, "PONG\n", 5);
  5617. reply_len = 5;
  5618. } else if (os_strncmp(buf, "INTERFACE_ADD ", 14) == 0) {
  5619. if (wpa_supplicant_global_iface_add(global, buf + 14))
  5620. reply_len = -1;
  5621. } else if (os_strncmp(buf, "INTERFACE_REMOVE ", 17) == 0) {
  5622. if (wpa_supplicant_global_iface_remove(global, buf + 17))
  5623. reply_len = -1;
  5624. } else if (os_strcmp(buf, "INTERFACE_LIST") == 0) {
  5625. reply_len = wpa_supplicant_global_iface_list(
  5626. global, reply, reply_size);
  5627. } else if (os_strcmp(buf, "INTERFACES") == 0) {
  5628. reply_len = wpa_supplicant_global_iface_interfaces(
  5629. global, reply, reply_size);
  5630. } else if (os_strcmp(buf, "TERMINATE") == 0) {
  5631. wpa_supplicant_terminate_proc(global);
  5632. } else if (os_strcmp(buf, "SUSPEND") == 0) {
  5633. wpas_notify_suspend(global);
  5634. } else if (os_strcmp(buf, "RESUME") == 0) {
  5635. wpas_notify_resume(global);
  5636. } else if (os_strncmp(buf, "SET ", 4) == 0) {
  5637. if (wpas_global_ctrl_iface_set(global, buf + 4))
  5638. reply_len = -1;
  5639. #ifndef CONFIG_NO_CONFIG_WRITE
  5640. } else if (os_strcmp(buf, "SAVE_CONFIG") == 0) {
  5641. if (wpas_global_ctrl_iface_save_config(global))
  5642. reply_len = -1;
  5643. #endif /* CONFIG_NO_CONFIG_WRITE */
  5644. } else if (os_strcmp(buf, "STATUS") == 0) {
  5645. reply_len = wpas_global_ctrl_iface_status(global, reply,
  5646. reply_size);
  5647. } else {
  5648. os_memcpy(reply, "UNKNOWN COMMAND\n", 16);
  5649. reply_len = 16;
  5650. }
  5651. if (reply_len < 0) {
  5652. os_memcpy(reply, "FAIL\n", 5);
  5653. reply_len = 5;
  5654. }
  5655. *resp_len = reply_len;
  5656. return reply;
  5657. }