ctrl_iface.c 185 KB

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