ctrl_iface.c 96 KB

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