ctrl_iface.c 159 KB

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