ctrl_iface.c 169 KB

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