ctrl_iface.c 109 KB

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