ctrl_iface.c 165 KB

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