ctrl_iface.c 183 KB

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