ctrl_iface.c 183 KB

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