ctrl_iface.c 94 KB

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