ctrl_iface.c 160 KB

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