ctrl_iface.c 135 KB

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