ctrl_iface.c 127 KB

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