ctrl_iface.c 156 KB

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